pci.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /* pci.c: UltraSparc PCI controller support.
  2. *
  3. * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
  4. * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
  6. *
  7. * OF tree based PCI bus probing taken from the PowerPC port
  8. * with minor modifications, see there for credits.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/sched.h>
  14. #include <linux/capability.h>
  15. #include <linux/errno.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/msi.h>
  18. #include <linux/irq.h>
  19. #include <linux/init.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pbm.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/irq.h>
  24. #include <asm/ebus.h>
  25. #include <asm/isa.h>
  26. #include <asm/prom.h>
  27. #include <asm/apb.h>
  28. #include "pci_impl.h"
  29. unsigned long pci_memspace_mask = 0xffffffffUL;
  30. #ifndef CONFIG_PCI
  31. /* A "nop" PCI implementation. */
  32. asmlinkage int sys_pciconfig_read(unsigned long bus, unsigned long dfn,
  33. unsigned long off, unsigned long len,
  34. unsigned char *buf)
  35. {
  36. return 0;
  37. }
  38. asmlinkage int sys_pciconfig_write(unsigned long bus, unsigned long dfn,
  39. unsigned long off, unsigned long len,
  40. unsigned char *buf)
  41. {
  42. return 0;
  43. }
  44. #else
  45. /* List of all PCI controllers found in the system. */
  46. struct pci_controller_info *pci_controller_root = NULL;
  47. /* Each PCI controller found gets a unique index. */
  48. int pci_num_controllers = 0;
  49. volatile int pci_poke_in_progress;
  50. volatile int pci_poke_cpu = -1;
  51. volatile int pci_poke_faulted;
  52. static DEFINE_SPINLOCK(pci_poke_lock);
  53. void pci_config_read8(u8 *addr, u8 *ret)
  54. {
  55. unsigned long flags;
  56. u8 byte;
  57. spin_lock_irqsave(&pci_poke_lock, flags);
  58. pci_poke_cpu = smp_processor_id();
  59. pci_poke_in_progress = 1;
  60. pci_poke_faulted = 0;
  61. __asm__ __volatile__("membar #Sync\n\t"
  62. "lduba [%1] %2, %0\n\t"
  63. "membar #Sync"
  64. : "=r" (byte)
  65. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  66. : "memory");
  67. pci_poke_in_progress = 0;
  68. pci_poke_cpu = -1;
  69. if (!pci_poke_faulted)
  70. *ret = byte;
  71. spin_unlock_irqrestore(&pci_poke_lock, flags);
  72. }
  73. void pci_config_read16(u16 *addr, u16 *ret)
  74. {
  75. unsigned long flags;
  76. u16 word;
  77. spin_lock_irqsave(&pci_poke_lock, flags);
  78. pci_poke_cpu = smp_processor_id();
  79. pci_poke_in_progress = 1;
  80. pci_poke_faulted = 0;
  81. __asm__ __volatile__("membar #Sync\n\t"
  82. "lduha [%1] %2, %0\n\t"
  83. "membar #Sync"
  84. : "=r" (word)
  85. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  86. : "memory");
  87. pci_poke_in_progress = 0;
  88. pci_poke_cpu = -1;
  89. if (!pci_poke_faulted)
  90. *ret = word;
  91. spin_unlock_irqrestore(&pci_poke_lock, flags);
  92. }
  93. void pci_config_read32(u32 *addr, u32 *ret)
  94. {
  95. unsigned long flags;
  96. u32 dword;
  97. spin_lock_irqsave(&pci_poke_lock, flags);
  98. pci_poke_cpu = smp_processor_id();
  99. pci_poke_in_progress = 1;
  100. pci_poke_faulted = 0;
  101. __asm__ __volatile__("membar #Sync\n\t"
  102. "lduwa [%1] %2, %0\n\t"
  103. "membar #Sync"
  104. : "=r" (dword)
  105. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  106. : "memory");
  107. pci_poke_in_progress = 0;
  108. pci_poke_cpu = -1;
  109. if (!pci_poke_faulted)
  110. *ret = dword;
  111. spin_unlock_irqrestore(&pci_poke_lock, flags);
  112. }
  113. void pci_config_write8(u8 *addr, u8 val)
  114. {
  115. unsigned long flags;
  116. spin_lock_irqsave(&pci_poke_lock, flags);
  117. pci_poke_cpu = smp_processor_id();
  118. pci_poke_in_progress = 1;
  119. pci_poke_faulted = 0;
  120. __asm__ __volatile__("membar #Sync\n\t"
  121. "stba %0, [%1] %2\n\t"
  122. "membar #Sync"
  123. : /* no outputs */
  124. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  125. : "memory");
  126. pci_poke_in_progress = 0;
  127. pci_poke_cpu = -1;
  128. spin_unlock_irqrestore(&pci_poke_lock, flags);
  129. }
  130. void pci_config_write16(u16 *addr, u16 val)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&pci_poke_lock, flags);
  134. pci_poke_cpu = smp_processor_id();
  135. pci_poke_in_progress = 1;
  136. pci_poke_faulted = 0;
  137. __asm__ __volatile__("membar #Sync\n\t"
  138. "stha %0, [%1] %2\n\t"
  139. "membar #Sync"
  140. : /* no outputs */
  141. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  142. : "memory");
  143. pci_poke_in_progress = 0;
  144. pci_poke_cpu = -1;
  145. spin_unlock_irqrestore(&pci_poke_lock, flags);
  146. }
  147. void pci_config_write32(u32 *addr, u32 val)
  148. {
  149. unsigned long flags;
  150. spin_lock_irqsave(&pci_poke_lock, flags);
  151. pci_poke_cpu = smp_processor_id();
  152. pci_poke_in_progress = 1;
  153. pci_poke_faulted = 0;
  154. __asm__ __volatile__("membar #Sync\n\t"
  155. "stwa %0, [%1] %2\n\t"
  156. "membar #Sync"
  157. : /* no outputs */
  158. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  159. : "memory");
  160. pci_poke_in_progress = 0;
  161. pci_poke_cpu = -1;
  162. spin_unlock_irqrestore(&pci_poke_lock, flags);
  163. }
  164. /* Probe for all PCI controllers in the system. */
  165. extern void sabre_init(struct device_node *, const char *);
  166. extern void psycho_init(struct device_node *, const char *);
  167. extern void schizo_init(struct device_node *, const char *);
  168. extern void schizo_plus_init(struct device_node *, const char *);
  169. extern void tomatillo_init(struct device_node *, const char *);
  170. extern void sun4v_pci_init(struct device_node *, const char *);
  171. static struct {
  172. char *model_name;
  173. void (*init)(struct device_node *, const char *);
  174. } pci_controller_table[] __initdata = {
  175. { "SUNW,sabre", sabre_init },
  176. { "pci108e,a000", sabre_init },
  177. { "pci108e,a001", sabre_init },
  178. { "SUNW,psycho", psycho_init },
  179. { "pci108e,8000", psycho_init },
  180. { "SUNW,schizo", schizo_init },
  181. { "pci108e,8001", schizo_init },
  182. { "SUNW,schizo+", schizo_plus_init },
  183. { "pci108e,8002", schizo_plus_init },
  184. { "SUNW,tomatillo", tomatillo_init },
  185. { "pci108e,a801", tomatillo_init },
  186. { "SUNW,sun4v-pci", sun4v_pci_init },
  187. };
  188. #define PCI_NUM_CONTROLLER_TYPES (sizeof(pci_controller_table) / \
  189. sizeof(pci_controller_table[0]))
  190. static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp)
  191. {
  192. int i;
  193. for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
  194. if (!strncmp(model_name,
  195. pci_controller_table[i].model_name,
  196. namelen)) {
  197. pci_controller_table[i].init(dp, model_name);
  198. return 1;
  199. }
  200. }
  201. return 0;
  202. }
  203. static int __init pci_is_controller(const char *model_name, int namelen, struct device_node *dp)
  204. {
  205. int i;
  206. for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) {
  207. if (!strncmp(model_name,
  208. pci_controller_table[i].model_name,
  209. namelen)) {
  210. return 1;
  211. }
  212. }
  213. return 0;
  214. }
  215. static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *))
  216. {
  217. struct device_node *dp;
  218. int count = 0;
  219. for_each_node_by_name(dp, "pci") {
  220. struct property *prop;
  221. int len;
  222. prop = of_find_property(dp, "model", &len);
  223. if (!prop)
  224. prop = of_find_property(dp, "compatible", &len);
  225. if (prop) {
  226. const char *model = prop->value;
  227. int item_len = 0;
  228. /* Our value may be a multi-valued string in the
  229. * case of some compatible properties. For sanity,
  230. * only try the first one.
  231. */
  232. while (model[item_len] && len) {
  233. len--;
  234. item_len++;
  235. }
  236. if (handler(model, item_len, dp))
  237. count++;
  238. }
  239. }
  240. return count;
  241. }
  242. /* Is there some PCI controller in the system? */
  243. int __init pcic_present(void)
  244. {
  245. return pci_controller_scan(pci_is_controller);
  246. }
  247. const struct pci_iommu_ops *pci_iommu_ops;
  248. EXPORT_SYMBOL(pci_iommu_ops);
  249. extern const struct pci_iommu_ops pci_sun4u_iommu_ops,
  250. pci_sun4v_iommu_ops;
  251. /* Find each controller in the system, attach and initialize
  252. * software state structure for each and link into the
  253. * pci_controller_root. Setup the controller enough such
  254. * that bus scanning can be done.
  255. */
  256. static void __init pci_controller_probe(void)
  257. {
  258. if (tlb_type == hypervisor)
  259. pci_iommu_ops = &pci_sun4v_iommu_ops;
  260. else
  261. pci_iommu_ops = &pci_sun4u_iommu_ops;
  262. printk("PCI: Probing for controllers.\n");
  263. pci_controller_scan(pci_controller_init);
  264. }
  265. static unsigned long pci_parse_of_flags(u32 addr0)
  266. {
  267. unsigned long flags = 0;
  268. if (addr0 & 0x02000000) {
  269. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  270. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  271. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  272. if (addr0 & 0x40000000)
  273. flags |= IORESOURCE_PREFETCH
  274. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  275. } else if (addr0 & 0x01000000)
  276. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  277. return flags;
  278. }
  279. /* The of_device layer has translated all of the assigned-address properties
  280. * into physical address resources, we only have to figure out the register
  281. * mapping.
  282. */
  283. static void pci_parse_of_addrs(struct of_device *op,
  284. struct device_node *node,
  285. struct pci_dev *dev)
  286. {
  287. struct resource *op_res;
  288. const u32 *addrs;
  289. int proplen;
  290. addrs = of_get_property(node, "assigned-addresses", &proplen);
  291. if (!addrs)
  292. return;
  293. printk(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  294. op_res = &op->resource[0];
  295. for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
  296. struct resource *res;
  297. unsigned long flags;
  298. int i;
  299. flags = pci_parse_of_flags(addrs[0]);
  300. if (!flags)
  301. continue;
  302. i = addrs[0] & 0xff;
  303. printk(" start: %lx, end: %lx, i: %x\n",
  304. op_res->start, op_res->end, i);
  305. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  306. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  307. } else if (i == dev->rom_base_reg) {
  308. res = &dev->resource[PCI_ROM_RESOURCE];
  309. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  310. } else {
  311. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  312. continue;
  313. }
  314. res->start = op_res->start;
  315. res->end = op_res->end;
  316. res->flags = flags;
  317. res->name = pci_name(dev);
  318. }
  319. }
  320. struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  321. struct device_node *node,
  322. struct pci_bus *bus, int devfn)
  323. {
  324. struct dev_archdata *sd;
  325. struct pci_dev *dev;
  326. const char *type;
  327. u32 class;
  328. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  329. if (!dev)
  330. return NULL;
  331. sd = &dev->dev.archdata;
  332. sd->iommu = pbm->iommu;
  333. sd->stc = &pbm->stc;
  334. sd->host_controller = pbm;
  335. sd->prom_node = node;
  336. sd->op = of_find_device_by_node(node);
  337. sd->msi_num = 0xffffffff;
  338. type = of_get_property(node, "device_type", NULL);
  339. if (type == NULL)
  340. type = "";
  341. printk(" create device, devfn: %x, type: %s\n", devfn, type);
  342. dev->bus = bus;
  343. dev->sysdata = node;
  344. dev->dev.parent = bus->bridge;
  345. dev->dev.bus = &pci_bus_type;
  346. dev->devfn = devfn;
  347. dev->multifunction = 0; /* maybe a lie? */
  348. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  349. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  350. dev->subsystem_vendor =
  351. of_getintprop_default(node, "subsystem-vendor-id", 0);
  352. dev->subsystem_device =
  353. of_getintprop_default(node, "subsystem-id", 0);
  354. dev->cfg_size = pci_cfg_space_size(dev);
  355. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  356. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  357. /* dev->class = of_getintprop_default(node, "class-code", 0); */
  358. /* We can't actually use the firmware value, we have to read what
  359. * is in the register right now. One reason is that in the case
  360. * of IDE interfaces the firmware can sample the value before the
  361. * the IDE interface is programmed into native mode.
  362. */
  363. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  364. dev->class = class >> 8;
  365. printk(" class: 0x%x\n", dev->class);
  366. dev->current_state = 4; /* unknown power state */
  367. dev->error_state = pci_channel_io_normal;
  368. if (!strcmp(type, "pci") || !strcmp(type, "pciex")) {
  369. /* a PCI-PCI bridge */
  370. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  371. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  372. } else if (!strcmp(type, "cardbus")) {
  373. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  374. } else {
  375. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  376. dev->rom_base_reg = PCI_ROM_ADDRESS;
  377. dev->irq = sd->op->irqs[0];
  378. if (dev->irq == 0xffffffff)
  379. dev->irq = PCI_IRQ_NONE;
  380. }
  381. pci_parse_of_addrs(sd->op, node, dev);
  382. printk(" adding to system ...\n");
  383. pci_device_add(dev, bus);
  384. return dev;
  385. }
  386. static void __init apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
  387. {
  388. u32 idx, first, last;
  389. first = 8;
  390. last = 0;
  391. for (idx = 0; idx < 8; idx++) {
  392. if ((map & (1 << idx)) != 0) {
  393. if (first > idx)
  394. first = idx;
  395. if (last < idx)
  396. last = idx;
  397. }
  398. }
  399. *first_p = first;
  400. *last_p = last;
  401. }
  402. static void __init pci_resource_adjust(struct resource *res,
  403. struct resource *root)
  404. {
  405. res->start += root->start;
  406. res->end += root->start;
  407. }
  408. /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  409. * a proper 'ranges' property.
  410. */
  411. static void __init apb_fake_ranges(struct pci_dev *dev,
  412. struct pci_bus *bus,
  413. struct pci_pbm_info *pbm)
  414. {
  415. struct resource *res;
  416. u32 first, last;
  417. u8 map;
  418. pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
  419. apb_calc_first_last(map, &first, &last);
  420. res = bus->resource[0];
  421. res->start = (first << 21);
  422. res->end = (last << 21) + ((1 << 21) - 1);
  423. res->flags = IORESOURCE_IO;
  424. pci_resource_adjust(res, &pbm->io_space);
  425. pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
  426. apb_calc_first_last(map, &first, &last);
  427. res = bus->resource[1];
  428. res->start = (first << 21);
  429. res->end = (last << 21) + ((1 << 21) - 1);
  430. res->flags = IORESOURCE_MEM;
  431. pci_resource_adjust(res, &pbm->mem_space);
  432. }
  433. static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
  434. struct device_node *node,
  435. struct pci_bus *bus);
  436. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  437. void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
  438. struct device_node *node,
  439. struct pci_dev *dev)
  440. {
  441. struct pci_bus *bus;
  442. const u32 *busrange, *ranges;
  443. int len, i, simba;
  444. struct resource *res;
  445. unsigned int flags;
  446. u64 size;
  447. printk("of_scan_pci_bridge(%s)\n", node->full_name);
  448. /* parse bus-range property */
  449. busrange = of_get_property(node, "bus-range", &len);
  450. if (busrange == NULL || len != 8) {
  451. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  452. node->full_name);
  453. return;
  454. }
  455. ranges = of_get_property(node, "ranges", &len);
  456. simba = 0;
  457. if (ranges == NULL) {
  458. char *model = of_get_property(node, "model", NULL);
  459. if (model && !strcmp(model, "SUNW,simba")) {
  460. simba = 1;
  461. } else {
  462. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %s\n",
  463. node->full_name);
  464. return;
  465. }
  466. }
  467. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  468. if (!bus) {
  469. printk(KERN_ERR "Failed to create pci bus for %s\n",
  470. node->full_name);
  471. return;
  472. }
  473. bus->primary = dev->bus->number;
  474. bus->subordinate = busrange[1];
  475. bus->bridge_ctl = 0;
  476. /* parse ranges property, or cook one up by hand for Simba */
  477. /* PCI #address-cells == 3 and #size-cells == 2 always */
  478. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  479. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  480. res->flags = 0;
  481. bus->resource[i] = res;
  482. ++res;
  483. }
  484. if (simba) {
  485. apb_fake_ranges(dev, bus, pbm);
  486. goto simba_cont;
  487. }
  488. i = 1;
  489. for (; len >= 32; len -= 32, ranges += 8) {
  490. struct resource *root;
  491. flags = pci_parse_of_flags(ranges[0]);
  492. size = GET_64BIT(ranges, 6);
  493. if (flags == 0 || size == 0)
  494. continue;
  495. if (flags & IORESOURCE_IO) {
  496. res = bus->resource[0];
  497. if (res->flags) {
  498. printk(KERN_ERR "PCI: ignoring extra I/O range"
  499. " for bridge %s\n", node->full_name);
  500. continue;
  501. }
  502. root = &pbm->io_space;
  503. } else {
  504. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  505. printk(KERN_ERR "PCI: too many memory ranges"
  506. " for bridge %s\n", node->full_name);
  507. continue;
  508. }
  509. res = bus->resource[i];
  510. ++i;
  511. root = &pbm->mem_space;
  512. }
  513. res->start = GET_64BIT(ranges, 1);
  514. res->end = res->start + size - 1;
  515. res->flags = flags;
  516. /* Another way to implement this would be to add an of_device
  517. * layer routine that can calculate a resource for a given
  518. * range property value in a PCI device.
  519. */
  520. pci_resource_adjust(res, root);
  521. }
  522. simba_cont:
  523. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  524. bus->number);
  525. printk(" bus name: %s\n", bus->name);
  526. pci_of_scan_bus(pbm, node, bus);
  527. }
  528. static void __init pci_of_scan_bus(struct pci_pbm_info *pbm,
  529. struct device_node *node,
  530. struct pci_bus *bus)
  531. {
  532. struct device_node *child;
  533. const u32 *reg;
  534. int reglen, devfn;
  535. struct pci_dev *dev;
  536. printk("PCI: scan_bus[%s] bus no %d\n",
  537. node->full_name, bus->number);
  538. child = NULL;
  539. while ((child = of_get_next_child(node, child)) != NULL) {
  540. printk(" * %s\n", child->full_name);
  541. reg = of_get_property(child, "reg", &reglen);
  542. if (reg == NULL || reglen < 20)
  543. continue;
  544. devfn = (reg[0] >> 8) & 0xff;
  545. /* create a new pci_dev for this device */
  546. dev = of_create_pci_dev(pbm, child, bus, devfn);
  547. if (!dev)
  548. continue;
  549. printk("PCI: dev header type: %x\n", dev->hdr_type);
  550. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  551. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  552. of_scan_pci_bridge(pbm, child, dev);
  553. }
  554. }
  555. static ssize_t
  556. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  557. {
  558. struct pci_dev *pdev;
  559. struct device_node *dp;
  560. pdev = to_pci_dev(dev);
  561. dp = pdev->dev.archdata.prom_node;
  562. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  563. }
  564. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  565. static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
  566. {
  567. struct pci_dev *dev;
  568. struct pci_bus *child_bus;
  569. int err;
  570. list_for_each_entry(dev, &bus->devices, bus_list) {
  571. /* we don't really care if we can create this file or
  572. * not, but we need to assign the result of the call
  573. * or the world will fall under alien invasion and
  574. * everybody will be frozen on a spaceship ready to be
  575. * eaten on alpha centauri by some green and jelly
  576. * humanoid.
  577. */
  578. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  579. }
  580. list_for_each_entry(child_bus, &bus->children, node)
  581. pci_bus_register_of_sysfs(child_bus);
  582. }
  583. struct pci_bus * __init pci_scan_one_pbm(struct pci_pbm_info *pbm)
  584. {
  585. struct pci_controller_info *p = pbm->parent;
  586. struct device_node *node = pbm->prom_node;
  587. struct pci_bus *bus;
  588. printk("PCI: Scanning PBM %s\n", node->full_name);
  589. /* XXX parent device? XXX */
  590. bus = pci_create_bus(NULL, pbm->pci_first_busno, p->pci_ops, pbm);
  591. if (!bus) {
  592. printk(KERN_ERR "Failed to create bus for %s\n",
  593. node->full_name);
  594. return NULL;
  595. }
  596. bus->secondary = pbm->pci_first_busno;
  597. bus->subordinate = pbm->pci_last_busno;
  598. bus->resource[0] = &pbm->io_space;
  599. bus->resource[1] = &pbm->mem_space;
  600. pci_of_scan_bus(pbm, node, bus);
  601. pci_bus_add_devices(bus);
  602. pci_bus_register_of_sysfs(bus);
  603. return bus;
  604. }
  605. static void __init pci_scan_each_controller_bus(void)
  606. {
  607. struct pci_controller_info *p;
  608. for (p = pci_controller_root; p; p = p->next)
  609. p->scan_bus(p);
  610. }
  611. extern void power_init(void);
  612. static int __init pcibios_init(void)
  613. {
  614. pci_controller_probe();
  615. if (pci_controller_root == NULL)
  616. return 0;
  617. pci_scan_each_controller_bus();
  618. isa_init();
  619. ebus_init();
  620. power_init();
  621. return 0;
  622. }
  623. subsys_initcall(pcibios_init);
  624. void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
  625. {
  626. struct pci_pbm_info *pbm = pbus->sysdata;
  627. /* Generic PCI bus probing sets these to point at
  628. * &io{port,mem}_resouce which is wrong for us.
  629. */
  630. pbus->resource[0] = &pbm->io_space;
  631. pbus->resource[1] = &pbm->mem_space;
  632. }
  633. struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
  634. {
  635. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  636. struct resource *root = NULL;
  637. if (r->flags & IORESOURCE_IO)
  638. root = &pbm->io_space;
  639. if (r->flags & IORESOURCE_MEM)
  640. root = &pbm->mem_space;
  641. return root;
  642. }
  643. void pcibios_update_irq(struct pci_dev *pdev, int irq)
  644. {
  645. }
  646. void pcibios_align_resource(void *data, struct resource *res,
  647. resource_size_t size, resource_size_t align)
  648. {
  649. }
  650. int pcibios_enable_device(struct pci_dev *dev, int mask)
  651. {
  652. u16 cmd, oldcmd;
  653. int i;
  654. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  655. oldcmd = cmd;
  656. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  657. struct resource *res = &dev->resource[i];
  658. /* Only set up the requested stuff */
  659. if (!(mask & (1<<i)))
  660. continue;
  661. if (res->flags & IORESOURCE_IO)
  662. cmd |= PCI_COMMAND_IO;
  663. if (res->flags & IORESOURCE_MEM)
  664. cmd |= PCI_COMMAND_MEMORY;
  665. }
  666. if (cmd != oldcmd) {
  667. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  668. pci_name(dev), cmd);
  669. /* Enable the appropriate bits in the PCI command register. */
  670. pci_write_config_word(dev, PCI_COMMAND, cmd);
  671. }
  672. return 0;
  673. }
  674. void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
  675. struct resource *res)
  676. {
  677. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  678. struct resource zero_res, *root;
  679. zero_res.start = 0;
  680. zero_res.end = 0;
  681. zero_res.flags = res->flags;
  682. if (res->flags & IORESOURCE_IO)
  683. root = &pbm->io_space;
  684. else
  685. root = &pbm->mem_space;
  686. pci_resource_adjust(&zero_res, root);
  687. region->start = res->start - zero_res.start;
  688. region->end = res->end - zero_res.start;
  689. }
  690. EXPORT_SYMBOL(pcibios_resource_to_bus);
  691. void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
  692. struct pci_bus_region *region)
  693. {
  694. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  695. struct resource *root;
  696. res->start = region->start;
  697. res->end = region->end;
  698. if (res->flags & IORESOURCE_IO)
  699. root = &pbm->io_space;
  700. else
  701. root = &pbm->mem_space;
  702. pci_resource_adjust(res, root);
  703. }
  704. EXPORT_SYMBOL(pcibios_bus_to_resource);
  705. char * __devinit pcibios_setup(char *str)
  706. {
  707. return str;
  708. }
  709. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  710. /* If the user uses a host-bridge as the PCI device, he may use
  711. * this to perform a raw mmap() of the I/O or MEM space behind
  712. * that controller.
  713. *
  714. * This can be useful for execution of x86 PCI bios initialization code
  715. * on a PCI card, like the xfree86 int10 stuff does.
  716. */
  717. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  718. enum pci_mmap_state mmap_state)
  719. {
  720. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  721. struct pci_controller_info *p;
  722. unsigned long space_size, user_offset, user_size;
  723. p = pbm->parent;
  724. if (mmap_state == pci_mmap_io) {
  725. space_size = (pbm->io_space.end -
  726. pbm->io_space.start) + 1;
  727. } else {
  728. space_size = (pbm->mem_space.end -
  729. pbm->mem_space.start) + 1;
  730. }
  731. /* Make sure the request is in range. */
  732. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  733. user_size = vma->vm_end - vma->vm_start;
  734. if (user_offset >= space_size ||
  735. (user_offset + user_size) > space_size)
  736. return -EINVAL;
  737. if (mmap_state == pci_mmap_io) {
  738. vma->vm_pgoff = (pbm->io_space.start +
  739. user_offset) >> PAGE_SHIFT;
  740. } else {
  741. vma->vm_pgoff = (pbm->mem_space.start +
  742. user_offset) >> PAGE_SHIFT;
  743. }
  744. return 0;
  745. }
  746. /* Adjust vm_pgoff of VMA such that it is the physical page offset corresponding
  747. * to the 32-bit pci bus offset for DEV requested by the user.
  748. *
  749. * Basically, the user finds the base address for his device which he wishes
  750. * to mmap. They read the 32-bit value from the config space base register,
  751. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  752. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  753. *
  754. * Returns negative error code on failure, zero on success.
  755. */
  756. static int __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
  757. enum pci_mmap_state mmap_state)
  758. {
  759. unsigned long user_offset = vma->vm_pgoff << PAGE_SHIFT;
  760. unsigned long user32 = user_offset & pci_memspace_mask;
  761. unsigned long largest_base, this_base, addr32;
  762. int i;
  763. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  764. return __pci_mmap_make_offset_bus(dev, vma, mmap_state);
  765. /* Figure out which base address this is for. */
  766. largest_base = 0UL;
  767. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  768. struct resource *rp = &dev->resource[i];
  769. /* Active? */
  770. if (!rp->flags)
  771. continue;
  772. /* Same type? */
  773. if (i == PCI_ROM_RESOURCE) {
  774. if (mmap_state != pci_mmap_mem)
  775. continue;
  776. } else {
  777. if ((mmap_state == pci_mmap_io &&
  778. (rp->flags & IORESOURCE_IO) == 0) ||
  779. (mmap_state == pci_mmap_mem &&
  780. (rp->flags & IORESOURCE_MEM) == 0))
  781. continue;
  782. }
  783. this_base = rp->start;
  784. addr32 = (this_base & PAGE_MASK) & pci_memspace_mask;
  785. if (mmap_state == pci_mmap_io)
  786. addr32 &= 0xffffff;
  787. if (addr32 <= user32 && this_base > largest_base)
  788. largest_base = this_base;
  789. }
  790. if (largest_base == 0UL)
  791. return -EINVAL;
  792. /* Now construct the final physical address. */
  793. if (mmap_state == pci_mmap_io)
  794. vma->vm_pgoff = (((largest_base & ~0xffffffUL) | user32) >> PAGE_SHIFT);
  795. else
  796. vma->vm_pgoff = (((largest_base & ~(pci_memspace_mask)) | user32) >> PAGE_SHIFT);
  797. return 0;
  798. }
  799. /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  800. * mapping.
  801. */
  802. static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  803. enum pci_mmap_state mmap_state)
  804. {
  805. vma->vm_flags |= (VM_IO | VM_RESERVED);
  806. }
  807. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  808. * device mapping.
  809. */
  810. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  811. enum pci_mmap_state mmap_state)
  812. {
  813. /* Our io_remap_pfn_range takes care of this, do nothing. */
  814. }
  815. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  816. * for this architecture. The region in the process to map is described by vm_start
  817. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  818. * The pci device structure is provided so that architectures may make mapping
  819. * decisions on a per-device or per-bus basis.
  820. *
  821. * Returns a negative error code on failure, zero on success.
  822. */
  823. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  824. enum pci_mmap_state mmap_state,
  825. int write_combine)
  826. {
  827. int ret;
  828. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  829. if (ret < 0)
  830. return ret;
  831. __pci_mmap_set_flags(dev, vma, mmap_state);
  832. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  833. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  834. ret = io_remap_pfn_range(vma, vma->vm_start,
  835. vma->vm_pgoff,
  836. vma->vm_end - vma->vm_start,
  837. vma->vm_page_prot);
  838. if (ret)
  839. return ret;
  840. return 0;
  841. }
  842. /* Return the domain nuber for this pci bus */
  843. int pci_domain_nr(struct pci_bus *pbus)
  844. {
  845. struct pci_pbm_info *pbm = pbus->sysdata;
  846. int ret;
  847. if (pbm == NULL || pbm->parent == NULL) {
  848. ret = -ENXIO;
  849. } else {
  850. struct pci_controller_info *p = pbm->parent;
  851. ret = p->index;
  852. ret = ((ret << 1) +
  853. ((pbm == &pbm->parent->pbm_B) ? 1 : 0));
  854. }
  855. return ret;
  856. }
  857. EXPORT_SYMBOL(pci_domain_nr);
  858. #ifdef CONFIG_PCI_MSI
  859. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  860. {
  861. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  862. struct pci_controller_info *p = pbm->parent;
  863. int virt_irq, err;
  864. if (!pbm->msi_num || !p->setup_msi_irq)
  865. return -EINVAL;
  866. err = p->setup_msi_irq(&virt_irq, pdev, desc);
  867. if (err < 0)
  868. return err;
  869. return virt_irq;
  870. }
  871. void arch_teardown_msi_irq(unsigned int virt_irq)
  872. {
  873. struct msi_desc *entry = get_irq_msi(virt_irq);
  874. struct pci_dev *pdev = entry->dev;
  875. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  876. struct pci_controller_info *p = pbm->parent;
  877. if (!pbm->msi_num || !p->setup_msi_irq)
  878. return;
  879. return p->teardown_msi_irq(virt_irq, pdev);
  880. }
  881. #endif /* !(CONFIG_PCI_MSI) */
  882. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  883. {
  884. return pdev->dev.archdata.prom_node;
  885. }
  886. EXPORT_SYMBOL(pci_device_to_OF_node);
  887. #endif /* !(CONFIG_PCI) */