pci_64.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /*
  2. * Port for PPC64 David Engebretsen, IBM Corp.
  3. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  4. *
  5. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  6. * Rework, based on alpha PCI code.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #undef DEBUG
  14. #include <linux/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/pci.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/mm.h>
  21. #include <linux/list.h>
  22. #include <linux/syscalls.h>
  23. #include <asm/processor.h>
  24. #include <asm/io.h>
  25. #include <asm/prom.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/irq.h>
  29. #include <asm/machdep.h>
  30. #include <asm/ppc-pci.h>
  31. #ifdef DEBUG
  32. #include <asm/udbg.h>
  33. #define DBG(fmt...) udbg_printf(fmt)
  34. #else
  35. #define DBG(fmt...)
  36. #endif
  37. unsigned long pci_probe_only = 1;
  38. unsigned long pci_assign_all_buses = 0;
  39. /*
  40. * legal IO pages under MAX_ISA_PORT. This is to ensure we don't touch
  41. * devices we don't have access to.
  42. */
  43. unsigned long io_page_mask;
  44. EXPORT_SYMBOL(io_page_mask);
  45. #ifdef CONFIG_PPC_MULTIPLATFORM
  46. static void fixup_resource(struct resource *res, struct pci_dev *dev);
  47. static void do_bus_setup(struct pci_bus *bus);
  48. #endif
  49. unsigned int pcibios_assign_all_busses(void)
  50. {
  51. return pci_assign_all_buses;
  52. }
  53. /* pci_io_base -- the base address from which io bars are offsets.
  54. * This is the lowest I/O base address (so bar values are always positive),
  55. * and it *must* be the start of ISA space if an ISA bus exists because
  56. * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
  57. * page is mapped and isa_io_limit prevents access to it.
  58. */
  59. unsigned long isa_io_base; /* NULL if no ISA bus */
  60. EXPORT_SYMBOL(isa_io_base);
  61. unsigned long pci_io_base;
  62. EXPORT_SYMBOL(pci_io_base);
  63. void iSeries_pcibios_init(void);
  64. LIST_HEAD(hose_list);
  65. struct dma_mapping_ops pci_dma_ops;
  66. EXPORT_SYMBOL(pci_dma_ops);
  67. int global_phb_number; /* Global phb counter */
  68. /* Cached ISA bridge dev. */
  69. struct pci_dev *ppc64_isabridge_dev = NULL;
  70. static void fixup_broken_pcnet32(struct pci_dev* dev)
  71. {
  72. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  73. dev->vendor = PCI_VENDOR_ID_AMD;
  74. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  75. }
  76. }
  77. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  78. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  79. struct resource *res)
  80. {
  81. unsigned long offset = 0;
  82. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  83. if (!hose)
  84. return;
  85. if (res->flags & IORESOURCE_IO)
  86. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  87. if (res->flags & IORESOURCE_MEM)
  88. offset = hose->pci_mem_offset;
  89. region->start = res->start - offset;
  90. region->end = res->end - offset;
  91. }
  92. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  93. struct pci_bus_region *region)
  94. {
  95. unsigned long offset = 0;
  96. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  97. if (!hose)
  98. return;
  99. if (res->flags & IORESOURCE_IO)
  100. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  101. if (res->flags & IORESOURCE_MEM)
  102. offset = hose->pci_mem_offset;
  103. res->start = region->start + offset;
  104. res->end = region->end + offset;
  105. }
  106. #ifdef CONFIG_HOTPLUG
  107. EXPORT_SYMBOL(pcibios_resource_to_bus);
  108. EXPORT_SYMBOL(pcibios_bus_to_resource);
  109. #endif
  110. /*
  111. * We need to avoid collisions with `mirrored' VGA ports
  112. * and other strange ISA hardware, so we always want the
  113. * addresses to be allocated in the 0x000-0x0ff region
  114. * modulo 0x400.
  115. *
  116. * Why? Because some silly external IO cards only decode
  117. * the low 10 bits of the IO address. The 0x00-0xff region
  118. * is reserved for motherboard devices that decode all 16
  119. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  120. * but we want to try to avoid allocating at 0x2900-0x2bff
  121. * which might have be mirrored at 0x0100-0x03ff..
  122. */
  123. void pcibios_align_resource(void *data, struct resource *res,
  124. unsigned long size, unsigned long align)
  125. {
  126. struct pci_dev *dev = data;
  127. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  128. unsigned long start = res->start;
  129. unsigned long alignto;
  130. if (res->flags & IORESOURCE_IO) {
  131. unsigned long offset = (unsigned long)hose->io_base_virt -
  132. pci_io_base;
  133. /* Make sure we start at our min on all hoses */
  134. if (start - offset < PCIBIOS_MIN_IO)
  135. start = PCIBIOS_MIN_IO + offset;
  136. /*
  137. * Put everything into 0x00-0xff region modulo 0x400
  138. */
  139. if (start & 0x300)
  140. start = (start + 0x3ff) & ~0x3ff;
  141. } else if (res->flags & IORESOURCE_MEM) {
  142. /* Make sure we start at our min on all hoses */
  143. if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
  144. start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
  145. /* Align to multiple of size of minimum base. */
  146. alignto = max(0x1000UL, align);
  147. start = ALIGN(start, alignto);
  148. }
  149. res->start = start;
  150. }
  151. static DEFINE_SPINLOCK(hose_spinlock);
  152. /*
  153. * pci_controller(phb) initialized common variables.
  154. */
  155. static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
  156. {
  157. memset(hose, 0, sizeof(struct pci_controller));
  158. spin_lock(&hose_spinlock);
  159. hose->global_number = global_phb_number++;
  160. list_add_tail(&hose->list_node, &hose_list);
  161. spin_unlock(&hose_spinlock);
  162. }
  163. static void add_linux_pci_domain(struct device_node *dev,
  164. struct pci_controller *phb)
  165. {
  166. struct property *of_prop;
  167. unsigned int size;
  168. of_prop = (struct property *)
  169. get_property(dev, "linux,pci-domain", &size);
  170. if (of_prop != NULL)
  171. return;
  172. WARN_ON(of_prop && size < sizeof(int));
  173. if (of_prop && size < sizeof(int))
  174. of_prop = NULL;
  175. size = sizeof(struct property) + sizeof(int);
  176. if (of_prop == NULL) {
  177. if (mem_init_done)
  178. of_prop = kmalloc(size, GFP_KERNEL);
  179. else
  180. of_prop = alloc_bootmem(size);
  181. }
  182. memset(of_prop, 0, sizeof(struct property));
  183. of_prop->name = "linux,pci-domain";
  184. of_prop->length = sizeof(int);
  185. of_prop->value = (unsigned char *)&of_prop[1];
  186. *((int *)of_prop->value) = phb->global_number;
  187. prom_add_property(dev, of_prop);
  188. }
  189. struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
  190. {
  191. struct pci_controller *phb;
  192. if (mem_init_done)
  193. phb = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  194. else
  195. phb = alloc_bootmem(sizeof (struct pci_controller));
  196. if (phb == NULL)
  197. return NULL;
  198. pci_setup_pci_controller(phb);
  199. phb->arch_data = dev;
  200. phb->is_dynamic = mem_init_done;
  201. if (dev)
  202. add_linux_pci_domain(dev, phb);
  203. return phb;
  204. }
  205. void pcibios_free_controller(struct pci_controller *phb)
  206. {
  207. if (phb->arch_data) {
  208. struct device_node *np = phb->arch_data;
  209. int *domain = (int *)get_property(np,
  210. "linux,pci-domain", NULL);
  211. if (domain)
  212. *domain = -1;
  213. }
  214. if (phb->is_dynamic)
  215. kfree(phb);
  216. }
  217. static void __init pcibios_claim_one_bus(struct pci_bus *b)
  218. {
  219. struct pci_dev *dev;
  220. struct pci_bus *child_bus;
  221. list_for_each_entry(dev, &b->devices, bus_list) {
  222. int i;
  223. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  224. struct resource *r = &dev->resource[i];
  225. if (r->parent || !r->start || !r->flags)
  226. continue;
  227. pci_claim_resource(dev, i);
  228. }
  229. }
  230. list_for_each_entry(child_bus, &b->children, node)
  231. pcibios_claim_one_bus(child_bus);
  232. }
  233. #ifndef CONFIG_PPC_ISERIES
  234. static void __init pcibios_claim_of_setup(void)
  235. {
  236. struct pci_bus *b;
  237. list_for_each_entry(b, &pci_root_buses, node)
  238. pcibios_claim_one_bus(b);
  239. }
  240. #endif
  241. #ifdef CONFIG_PPC_MULTIPLATFORM
  242. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  243. {
  244. u32 *prop;
  245. int len;
  246. prop = (u32 *) get_property(np, name, &len);
  247. if (prop && len >= 4)
  248. return *prop;
  249. return def;
  250. }
  251. static unsigned int pci_parse_of_flags(u32 addr0)
  252. {
  253. unsigned int flags = 0;
  254. if (addr0 & 0x02000000) {
  255. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  256. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  257. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  258. if (addr0 & 0x40000000)
  259. flags |= IORESOURCE_PREFETCH
  260. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  261. } else if (addr0 & 0x01000000)
  262. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  263. return flags;
  264. }
  265. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  266. static void pci_parse_of_addrs(struct device_node *node, struct pci_dev *dev)
  267. {
  268. u64 base, size;
  269. unsigned int flags;
  270. struct resource *res;
  271. u32 *addrs, i;
  272. int proplen;
  273. addrs = (u32 *) get_property(node, "assigned-addresses", &proplen);
  274. if (!addrs)
  275. return;
  276. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  277. flags = pci_parse_of_flags(addrs[0]);
  278. if (!flags)
  279. continue;
  280. base = GET_64BIT(addrs, 1);
  281. size = GET_64BIT(addrs, 3);
  282. if (!size)
  283. continue;
  284. i = addrs[0] & 0xff;
  285. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  286. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  287. } else if (i == dev->rom_base_reg) {
  288. res = &dev->resource[PCI_ROM_RESOURCE];
  289. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  290. } else {
  291. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  292. continue;
  293. }
  294. res->start = base;
  295. res->end = base + size - 1;
  296. res->flags = flags;
  297. res->name = pci_name(dev);
  298. fixup_resource(res, dev);
  299. }
  300. }
  301. struct pci_dev *of_create_pci_dev(struct device_node *node,
  302. struct pci_bus *bus, int devfn)
  303. {
  304. struct pci_dev *dev;
  305. const char *type;
  306. dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  307. if (!dev)
  308. return NULL;
  309. type = get_property(node, "device_type", NULL);
  310. if (type == NULL)
  311. type = "";
  312. memset(dev, 0, sizeof(struct pci_dev));
  313. dev->bus = bus;
  314. dev->sysdata = node;
  315. dev->dev.parent = bus->bridge;
  316. dev->dev.bus = &pci_bus_type;
  317. dev->devfn = devfn;
  318. dev->multifunction = 0; /* maybe a lie? */
  319. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  320. dev->device = get_int_prop(node, "device-id", 0xffff);
  321. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  322. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  323. dev->cfg_size = 256; /*pci_cfg_space_size(dev);*/
  324. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  325. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  326. dev->class = get_int_prop(node, "class-code", 0);
  327. dev->current_state = 4; /* unknown power state */
  328. if (!strcmp(type, "pci")) {
  329. /* a PCI-PCI bridge */
  330. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  331. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  332. } else if (!strcmp(type, "cardbus")) {
  333. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  334. } else {
  335. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  336. dev->rom_base_reg = PCI_ROM_ADDRESS;
  337. dev->irq = NO_IRQ;
  338. if (node->n_intrs > 0) {
  339. dev->irq = node->intrs[0].line;
  340. pci_write_config_byte(dev, PCI_INTERRUPT_LINE,
  341. dev->irq);
  342. }
  343. }
  344. pci_parse_of_addrs(node, dev);
  345. pci_device_add(dev, bus);
  346. /* XXX pci_scan_msi_device(dev); */
  347. return dev;
  348. }
  349. EXPORT_SYMBOL(of_create_pci_dev);
  350. void __devinit of_scan_bus(struct device_node *node,
  351. struct pci_bus *bus)
  352. {
  353. struct device_node *child = NULL;
  354. u32 *reg;
  355. int reglen, devfn;
  356. struct pci_dev *dev;
  357. while ((child = of_get_next_child(node, child)) != NULL) {
  358. reg = (u32 *) get_property(child, "reg", &reglen);
  359. if (reg == NULL || reglen < 20)
  360. continue;
  361. devfn = (reg[0] >> 8) & 0xff;
  362. /* create a new pci_dev for this device */
  363. dev = of_create_pci_dev(child, bus, devfn);
  364. if (!dev)
  365. continue;
  366. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  367. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  368. of_scan_pci_bridge(child, dev);
  369. }
  370. do_bus_setup(bus);
  371. }
  372. EXPORT_SYMBOL(of_scan_bus);
  373. void __devinit of_scan_pci_bridge(struct device_node *node,
  374. struct pci_dev *dev)
  375. {
  376. struct pci_bus *bus;
  377. u32 *busrange, *ranges;
  378. int len, i, mode;
  379. struct resource *res;
  380. unsigned int flags;
  381. u64 size;
  382. /* parse bus-range property */
  383. busrange = (u32 *) get_property(node, "bus-range", &len);
  384. if (busrange == NULL || len != 8) {
  385. printk(KERN_ERR "Can't get bus-range for PCI-PCI bridge %s\n",
  386. node->full_name);
  387. return;
  388. }
  389. ranges = (u32 *) get_property(node, "ranges", &len);
  390. if (ranges == NULL) {
  391. printk(KERN_ERR "Can't get ranges for PCI-PCI bridge %s\n",
  392. node->full_name);
  393. return;
  394. }
  395. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  396. if (!bus) {
  397. printk(KERN_ERR "Failed to create pci bus for %s\n",
  398. node->full_name);
  399. return;
  400. }
  401. bus->primary = dev->bus->number;
  402. bus->subordinate = busrange[1];
  403. bus->bridge_ctl = 0;
  404. bus->sysdata = node;
  405. /* parse ranges property */
  406. /* PCI #address-cells == 3 and #size-cells == 2 always */
  407. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  408. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  409. res->flags = 0;
  410. bus->resource[i] = res;
  411. ++res;
  412. }
  413. i = 1;
  414. for (; len >= 32; len -= 32, ranges += 8) {
  415. flags = pci_parse_of_flags(ranges[0]);
  416. size = GET_64BIT(ranges, 6);
  417. if (flags == 0 || size == 0)
  418. continue;
  419. if (flags & IORESOURCE_IO) {
  420. res = bus->resource[0];
  421. if (res->flags) {
  422. printk(KERN_ERR "PCI: ignoring extra I/O range"
  423. " for bridge %s\n", node->full_name);
  424. continue;
  425. }
  426. } else {
  427. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  428. printk(KERN_ERR "PCI: too many memory ranges"
  429. " for bridge %s\n", node->full_name);
  430. continue;
  431. }
  432. res = bus->resource[i];
  433. ++i;
  434. }
  435. res->start = GET_64BIT(ranges, 1);
  436. res->end = res->start + size - 1;
  437. res->flags = flags;
  438. fixup_resource(res, dev);
  439. }
  440. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  441. bus->number);
  442. mode = PCI_PROBE_NORMAL;
  443. if (ppc_md.pci_probe_mode)
  444. mode = ppc_md.pci_probe_mode(bus);
  445. if (mode == PCI_PROBE_DEVTREE)
  446. of_scan_bus(node, bus);
  447. else if (mode == PCI_PROBE_NORMAL)
  448. pci_scan_child_bus(bus);
  449. }
  450. EXPORT_SYMBOL(of_scan_pci_bridge);
  451. #endif /* CONFIG_PPC_MULTIPLATFORM */
  452. void __devinit scan_phb(struct pci_controller *hose)
  453. {
  454. struct pci_bus *bus;
  455. struct device_node *node = hose->arch_data;
  456. int i, mode;
  457. struct resource *res;
  458. bus = pci_create_bus(NULL, hose->first_busno, hose->ops, node);
  459. if (bus == NULL) {
  460. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  461. hose->global_number);
  462. return;
  463. }
  464. bus->secondary = hose->first_busno;
  465. hose->bus = bus;
  466. bus->resource[0] = res = &hose->io_resource;
  467. if (res->flags && request_resource(&ioport_resource, res))
  468. printk(KERN_ERR "Failed to request PCI IO region "
  469. "on PCI domain %04x\n", hose->global_number);
  470. for (i = 0; i < 3; ++i) {
  471. res = &hose->mem_resources[i];
  472. bus->resource[i+1] = res;
  473. if (res->flags && request_resource(&iomem_resource, res))
  474. printk(KERN_ERR "Failed to request PCI memory region "
  475. "on PCI domain %04x\n", hose->global_number);
  476. }
  477. mode = PCI_PROBE_NORMAL;
  478. #ifdef CONFIG_PPC_MULTIPLATFORM
  479. if (ppc_md.pci_probe_mode)
  480. mode = ppc_md.pci_probe_mode(bus);
  481. if (mode == PCI_PROBE_DEVTREE) {
  482. bus->subordinate = hose->last_busno;
  483. of_scan_bus(node, bus);
  484. }
  485. #endif /* CONFIG_PPC_MULTIPLATFORM */
  486. if (mode == PCI_PROBE_NORMAL)
  487. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  488. pci_bus_add_devices(bus);
  489. }
  490. static int __init pcibios_init(void)
  491. {
  492. struct pci_controller *hose, *tmp;
  493. /* For now, override phys_mem_access_prot. If we need it,
  494. * later, we may move that initialization to each ppc_md
  495. */
  496. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  497. #ifdef CONFIG_PPC_ISERIES
  498. iSeries_pcibios_init();
  499. #endif
  500. printk("PCI: Probing PCI hardware\n");
  501. /* Scan all of the recorded PCI controllers. */
  502. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  503. scan_phb(hose);
  504. #ifndef CONFIG_PPC_ISERIES
  505. if (pci_probe_only)
  506. pcibios_claim_of_setup();
  507. else
  508. /* FIXME: `else' will be removed when
  509. pci_assign_unassigned_resources() is able to work
  510. correctly with [partially] allocated PCI tree. */
  511. pci_assign_unassigned_resources();
  512. #endif /* !CONFIG_PPC_ISERIES */
  513. /* Call machine dependent final fixup */
  514. if (ppc_md.pcibios_fixup)
  515. ppc_md.pcibios_fixup();
  516. /* Cache the location of the ISA bridge (if we have one) */
  517. ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  518. if (ppc64_isabridge_dev != NULL)
  519. printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
  520. #ifdef CONFIG_PPC_MULTIPLATFORM
  521. /* map in PCI I/O space */
  522. phbs_remap_io();
  523. #endif
  524. printk("PCI: Probing PCI hardware done\n");
  525. return 0;
  526. }
  527. subsys_initcall(pcibios_init);
  528. char __init *pcibios_setup(char *str)
  529. {
  530. return str;
  531. }
  532. int pcibios_enable_device(struct pci_dev *dev, int mask)
  533. {
  534. u16 cmd, oldcmd;
  535. int i;
  536. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  537. oldcmd = cmd;
  538. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  539. struct resource *res = &dev->resource[i];
  540. /* Only set up the requested stuff */
  541. if (!(mask & (1<<i)))
  542. continue;
  543. if (res->flags & IORESOURCE_IO)
  544. cmd |= PCI_COMMAND_IO;
  545. if (res->flags & IORESOURCE_MEM)
  546. cmd |= PCI_COMMAND_MEMORY;
  547. }
  548. if (cmd != oldcmd) {
  549. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  550. pci_name(dev), cmd);
  551. /* Enable the appropriate bits in the PCI command register. */
  552. pci_write_config_word(dev, PCI_COMMAND, cmd);
  553. }
  554. return 0;
  555. }
  556. /*
  557. * Return the domain number for this bus.
  558. */
  559. int pci_domain_nr(struct pci_bus *bus)
  560. {
  561. #ifdef CONFIG_PPC_ISERIES
  562. return 0;
  563. #else
  564. struct pci_controller *hose = pci_bus_to_host(bus);
  565. return hose->global_number;
  566. #endif
  567. }
  568. EXPORT_SYMBOL(pci_domain_nr);
  569. /* Decide whether to display the domain number in /proc */
  570. int pci_proc_domain(struct pci_bus *bus)
  571. {
  572. #ifdef CONFIG_PPC_ISERIES
  573. return 0;
  574. #else
  575. struct pci_controller *hose = pci_bus_to_host(bus);
  576. return hose->buid;
  577. #endif
  578. }
  579. /*
  580. * Platform support for /proc/bus/pci/X/Y mmap()s,
  581. * modelled on the sparc64 implementation by Dave Miller.
  582. * -- paulus.
  583. */
  584. /*
  585. * Adjust vm_pgoff of VMA such that it is the physical page offset
  586. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  587. *
  588. * Basically, the user finds the base address for his device which he wishes
  589. * to mmap. They read the 32-bit value from the config space base register,
  590. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  591. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  592. *
  593. * Returns negative error code on failure, zero on success.
  594. */
  595. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  596. unsigned long *offset,
  597. enum pci_mmap_state mmap_state)
  598. {
  599. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  600. unsigned long io_offset = 0;
  601. int i, res_bit;
  602. if (hose == 0)
  603. return NULL; /* should never happen */
  604. /* If memory, add on the PCI bridge address offset */
  605. if (mmap_state == pci_mmap_mem) {
  606. *offset += hose->pci_mem_offset;
  607. res_bit = IORESOURCE_MEM;
  608. } else {
  609. io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  610. *offset += io_offset;
  611. res_bit = IORESOURCE_IO;
  612. }
  613. /*
  614. * Check that the offset requested corresponds to one of the
  615. * resources of the device.
  616. */
  617. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  618. struct resource *rp = &dev->resource[i];
  619. int flags = rp->flags;
  620. /* treat ROM as memory (should be already) */
  621. if (i == PCI_ROM_RESOURCE)
  622. flags |= IORESOURCE_MEM;
  623. /* Active and same type? */
  624. if ((flags & res_bit) == 0)
  625. continue;
  626. /* In the range of this resource? */
  627. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  628. continue;
  629. /* found it! construct the final physical address */
  630. if (mmap_state == pci_mmap_io)
  631. *offset += hose->io_base_phys - io_offset;
  632. return rp;
  633. }
  634. return NULL;
  635. }
  636. /*
  637. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  638. * device mapping.
  639. */
  640. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  641. pgprot_t protection,
  642. enum pci_mmap_state mmap_state,
  643. int write_combine)
  644. {
  645. unsigned long prot = pgprot_val(protection);
  646. /* Write combine is always 0 on non-memory space mappings. On
  647. * memory space, if the user didn't pass 1, we check for a
  648. * "prefetchable" resource. This is a bit hackish, but we use
  649. * this to workaround the inability of /sysfs to provide a write
  650. * combine bit
  651. */
  652. if (mmap_state != pci_mmap_mem)
  653. write_combine = 0;
  654. else if (write_combine == 0) {
  655. if (rp->flags & IORESOURCE_PREFETCH)
  656. write_combine = 1;
  657. }
  658. /* XXX would be nice to have a way to ask for write-through */
  659. prot |= _PAGE_NO_CACHE;
  660. if (write_combine)
  661. prot &= ~_PAGE_GUARDED;
  662. else
  663. prot |= _PAGE_GUARDED;
  664. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  665. prot);
  666. return __pgprot(prot);
  667. }
  668. /*
  669. * This one is used by /dev/mem and fbdev who have no clue about the
  670. * PCI device, it tries to find the PCI device first and calls the
  671. * above routine
  672. */
  673. pgprot_t pci_phys_mem_access_prot(struct file *file,
  674. unsigned long pfn,
  675. unsigned long size,
  676. pgprot_t protection)
  677. {
  678. struct pci_dev *pdev = NULL;
  679. struct resource *found = NULL;
  680. unsigned long prot = pgprot_val(protection);
  681. unsigned long offset = pfn << PAGE_SHIFT;
  682. int i;
  683. if (page_is_ram(pfn))
  684. return __pgprot(prot);
  685. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  686. for_each_pci_dev(pdev) {
  687. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  688. struct resource *rp = &pdev->resource[i];
  689. int flags = rp->flags;
  690. /* Active and same type? */
  691. if ((flags & IORESOURCE_MEM) == 0)
  692. continue;
  693. /* In the range of this resource? */
  694. if (offset < (rp->start & PAGE_MASK) ||
  695. offset > rp->end)
  696. continue;
  697. found = rp;
  698. break;
  699. }
  700. if (found)
  701. break;
  702. }
  703. if (found) {
  704. if (found->flags & IORESOURCE_PREFETCH)
  705. prot &= ~_PAGE_GUARDED;
  706. pci_dev_put(pdev);
  707. }
  708. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  709. return __pgprot(prot);
  710. }
  711. /*
  712. * Perform the actual remap of the pages for a PCI device mapping, as
  713. * appropriate for this architecture. The region in the process to map
  714. * is described by vm_start and vm_end members of VMA, the base physical
  715. * address is found in vm_pgoff.
  716. * The pci device structure is provided so that architectures may make mapping
  717. * decisions on a per-device or per-bus basis.
  718. *
  719. * Returns a negative error code on failure, zero on success.
  720. */
  721. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  722. enum pci_mmap_state mmap_state,
  723. int write_combine)
  724. {
  725. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  726. struct resource *rp;
  727. int ret;
  728. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  729. if (rp == NULL)
  730. return -EINVAL;
  731. vma->vm_pgoff = offset >> PAGE_SHIFT;
  732. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  733. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  734. vma->vm_page_prot,
  735. mmap_state, write_combine);
  736. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  737. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  738. return ret;
  739. }
  740. #ifdef CONFIG_PPC_MULTIPLATFORM
  741. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  742. {
  743. struct pci_dev *pdev;
  744. struct device_node *np;
  745. pdev = to_pci_dev (dev);
  746. np = pci_device_to_OF_node(pdev);
  747. if (np == NULL || np->full_name == NULL)
  748. return 0;
  749. return sprintf(buf, "%s", np->full_name);
  750. }
  751. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  752. #endif /* CONFIG_PPC_MULTIPLATFORM */
  753. void pcibios_add_platform_entries(struct pci_dev *pdev)
  754. {
  755. #ifdef CONFIG_PPC_MULTIPLATFORM
  756. device_create_file(&pdev->dev, &dev_attr_devspec);
  757. #endif /* CONFIG_PPC_MULTIPLATFORM */
  758. }
  759. #ifdef CONFIG_PPC_MULTIPLATFORM
  760. #define ISA_SPACE_MASK 0x1
  761. #define ISA_SPACE_IO 0x1
  762. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  763. unsigned long phb_io_base_phys,
  764. void __iomem * phb_io_base_virt)
  765. {
  766. struct isa_range *range;
  767. unsigned long pci_addr;
  768. unsigned int isa_addr;
  769. unsigned int size;
  770. int rlen = 0;
  771. range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
  772. if (range == NULL || (rlen < sizeof(struct isa_range))) {
  773. printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
  774. "mapping 64k\n");
  775. __ioremap_explicit(phb_io_base_phys,
  776. (unsigned long)phb_io_base_virt,
  777. 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
  778. return;
  779. }
  780. /* From "ISA Binding to 1275"
  781. * The ranges property is laid out as an array of elements,
  782. * each of which comprises:
  783. * cells 0 - 1: an ISA address
  784. * cells 2 - 4: a PCI address
  785. * (size depending on dev->n_addr_cells)
  786. * cell 5: the size of the range
  787. */
  788. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
  789. isa_addr = range->isa_addr.a_lo;
  790. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  791. range->pci_addr.a_lo;
  792. /* Assume these are both zero */
  793. if ((pci_addr != 0) || (isa_addr != 0)) {
  794. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  795. __FUNCTION__);
  796. return;
  797. }
  798. size = PAGE_ALIGN(range->size);
  799. __ioremap_explicit(phb_io_base_phys,
  800. (unsigned long) phb_io_base_virt,
  801. size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  802. }
  803. }
  804. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  805. struct device_node *dev, int prim)
  806. {
  807. unsigned int *ranges, pci_space;
  808. unsigned long size;
  809. int rlen = 0;
  810. int memno = 0;
  811. struct resource *res;
  812. int np, na = prom_n_addr_cells(dev);
  813. unsigned long pci_addr, cpu_phys_addr;
  814. np = na + 5;
  815. /* From "PCI Binding to 1275"
  816. * The ranges property is laid out as an array of elements,
  817. * each of which comprises:
  818. * cells 0 - 2: a PCI address
  819. * cells 3 or 3+4: a CPU physical address
  820. * (size depending on dev->n_addr_cells)
  821. * cells 4+5 or 5+6: the size of the range
  822. */
  823. ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  824. if (ranges == NULL)
  825. return;
  826. hose->io_base_phys = 0;
  827. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  828. res = NULL;
  829. pci_space = ranges[0];
  830. pci_addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  831. cpu_phys_addr = ranges[3];
  832. if (na >= 2)
  833. cpu_phys_addr = (cpu_phys_addr << 32) | ranges[4];
  834. size = ((unsigned long)ranges[na+3] << 32) | ranges[na+4];
  835. ranges += np;
  836. if (size == 0)
  837. continue;
  838. /* Now consume following elements while they are contiguous */
  839. while (rlen >= np * sizeof(unsigned int)) {
  840. unsigned long addr, phys;
  841. if (ranges[0] != pci_space)
  842. break;
  843. addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  844. phys = ranges[3];
  845. if (na >= 2)
  846. phys = (phys << 32) | ranges[4];
  847. if (addr != pci_addr + size ||
  848. phys != cpu_phys_addr + size)
  849. break;
  850. size += ((unsigned long)ranges[na+3] << 32)
  851. | ranges[na+4];
  852. ranges += np;
  853. rlen -= np * sizeof(unsigned int);
  854. }
  855. switch ((pci_space >> 24) & 0x3) {
  856. case 1: /* I/O space */
  857. hose->io_base_phys = cpu_phys_addr;
  858. hose->pci_io_size = size;
  859. res = &hose->io_resource;
  860. res->flags = IORESOURCE_IO;
  861. res->start = pci_addr;
  862. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  863. res->start, res->start + size - 1);
  864. break;
  865. case 2: /* memory space */
  866. memno = 0;
  867. while (memno < 3 && hose->mem_resources[memno].flags)
  868. ++memno;
  869. if (memno == 0)
  870. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  871. if (memno < 3) {
  872. res = &hose->mem_resources[memno];
  873. res->flags = IORESOURCE_MEM;
  874. res->start = cpu_phys_addr;
  875. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  876. res->start, res->start + size - 1);
  877. }
  878. break;
  879. }
  880. if (res != NULL) {
  881. res->name = dev->full_name;
  882. res->end = res->start + size - 1;
  883. res->parent = NULL;
  884. res->sibling = NULL;
  885. res->child = NULL;
  886. }
  887. }
  888. }
  889. void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
  890. {
  891. unsigned long size = hose->pci_io_size;
  892. unsigned long io_virt_offset;
  893. struct resource *res;
  894. struct device_node *isa_dn;
  895. hose->io_base_virt = reserve_phb_iospace(size);
  896. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  897. hose->global_number, hose->io_base_phys,
  898. (unsigned long) hose->io_base_virt);
  899. if (primary) {
  900. pci_io_base = (unsigned long)hose->io_base_virt;
  901. isa_dn = of_find_node_by_type(NULL, "isa");
  902. if (isa_dn) {
  903. isa_io_base = pci_io_base;
  904. pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
  905. hose->io_base_virt);
  906. of_node_put(isa_dn);
  907. /* Allow all IO */
  908. io_page_mask = -1;
  909. }
  910. }
  911. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  912. res = &hose->io_resource;
  913. res->start += io_virt_offset;
  914. res->end += io_virt_offset;
  915. }
  916. void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
  917. int primary)
  918. {
  919. unsigned long size = hose->pci_io_size;
  920. unsigned long io_virt_offset;
  921. struct resource *res;
  922. hose->io_base_virt = __ioremap(hose->io_base_phys, size,
  923. _PAGE_NO_CACHE | _PAGE_GUARDED);
  924. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  925. hose->global_number, hose->io_base_phys,
  926. (unsigned long) hose->io_base_virt);
  927. if (primary)
  928. pci_io_base = (unsigned long)hose->io_base_virt;
  929. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  930. res = &hose->io_resource;
  931. res->start += io_virt_offset;
  932. res->end += io_virt_offset;
  933. }
  934. static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
  935. unsigned long *start_virt, unsigned long *size)
  936. {
  937. struct pci_controller *hose = pci_bus_to_host(bus);
  938. struct pci_bus_region region;
  939. struct resource *res;
  940. if (bus->self) {
  941. res = bus->resource[0];
  942. pcibios_resource_to_bus(bus->self, &region, res);
  943. *start_phys = hose->io_base_phys + region.start;
  944. *start_virt = (unsigned long) hose->io_base_virt +
  945. region.start;
  946. if (region.end > region.start)
  947. *size = region.end - region.start + 1;
  948. else {
  949. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  950. __FUNCTION__, region.start, region.end);
  951. return 1;
  952. }
  953. } else {
  954. /* Root Bus */
  955. res = &hose->io_resource;
  956. *start_phys = hose->io_base_phys;
  957. *start_virt = (unsigned long) hose->io_base_virt;
  958. if (res->end > res->start)
  959. *size = res->end - res->start + 1;
  960. else {
  961. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  962. __FUNCTION__, res->start, res->end);
  963. return 1;
  964. }
  965. }
  966. return 0;
  967. }
  968. int unmap_bus_range(struct pci_bus *bus)
  969. {
  970. unsigned long start_phys;
  971. unsigned long start_virt;
  972. unsigned long size;
  973. if (!bus) {
  974. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  975. return 1;
  976. }
  977. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  978. return 1;
  979. if (iounmap_explicit((void __iomem *) start_virt, size))
  980. return 1;
  981. return 0;
  982. }
  983. EXPORT_SYMBOL(unmap_bus_range);
  984. int remap_bus_range(struct pci_bus *bus)
  985. {
  986. unsigned long start_phys;
  987. unsigned long start_virt;
  988. unsigned long size;
  989. if (!bus) {
  990. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  991. return 1;
  992. }
  993. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  994. return 1;
  995. if (start_phys == 0)
  996. return 1;
  997. printk("mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
  998. if (__ioremap_explicit(start_phys, start_virt, size,
  999. _PAGE_NO_CACHE | _PAGE_GUARDED))
  1000. return 1;
  1001. return 0;
  1002. }
  1003. EXPORT_SYMBOL(remap_bus_range);
  1004. void phbs_remap_io(void)
  1005. {
  1006. struct pci_controller *hose, *tmp;
  1007. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  1008. remap_bus_range(hose->bus);
  1009. }
  1010. /*
  1011. * ppc64 can have multifunction devices that do not respond to function 0.
  1012. * In this case we must scan all functions.
  1013. * XXX this can go now, we use the OF device tree in all the
  1014. * cases that caused problems. -- paulus
  1015. */
  1016. int pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
  1017. {
  1018. return 0;
  1019. }
  1020. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  1021. {
  1022. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1023. unsigned long start, end, mask, offset;
  1024. if (res->flags & IORESOURCE_IO) {
  1025. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  1026. start = res->start += offset;
  1027. end = res->end += offset;
  1028. /* Need to allow IO access to pages that are in the
  1029. ISA range */
  1030. if (start < MAX_ISA_PORT) {
  1031. if (end > MAX_ISA_PORT)
  1032. end = MAX_ISA_PORT;
  1033. start >>= PAGE_SHIFT;
  1034. end >>= PAGE_SHIFT;
  1035. /* get the range of pages for the map */
  1036. mask = ((1 << (end+1)) - 1) ^ ((1 << start) - 1);
  1037. io_page_mask |= mask;
  1038. }
  1039. } else if (res->flags & IORESOURCE_MEM) {
  1040. res->start += hose->pci_mem_offset;
  1041. res->end += hose->pci_mem_offset;
  1042. }
  1043. }
  1044. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  1045. struct pci_bus *bus)
  1046. {
  1047. /* Update device resources. */
  1048. int i;
  1049. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  1050. if (dev->resource[i].flags)
  1051. fixup_resource(&dev->resource[i], dev);
  1052. }
  1053. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  1054. static void __devinit do_bus_setup(struct pci_bus *bus)
  1055. {
  1056. struct pci_dev *dev;
  1057. ppc_md.iommu_bus_setup(bus);
  1058. list_for_each_entry(dev, &bus->devices, bus_list)
  1059. ppc_md.iommu_dev_setup(dev);
  1060. if (ppc_md.irq_bus_setup)
  1061. ppc_md.irq_bus_setup(bus);
  1062. }
  1063. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  1064. {
  1065. struct pci_dev *dev = bus->self;
  1066. if (dev && pci_probe_only &&
  1067. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1068. /* This is a subordinate bridge */
  1069. pci_read_bridge_bases(bus);
  1070. pcibios_fixup_device_resources(dev, bus);
  1071. }
  1072. do_bus_setup(bus);
  1073. if (!pci_probe_only)
  1074. return;
  1075. list_for_each_entry(dev, &bus->devices, bus_list)
  1076. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1077. pcibios_fixup_device_resources(dev, bus);
  1078. }
  1079. EXPORT_SYMBOL(pcibios_fixup_bus);
  1080. /*
  1081. * Reads the interrupt pin to determine if interrupt is use by card.
  1082. * If the interrupt is used, then gets the interrupt line from the
  1083. * openfirmware and sets it in the pci_dev and pci_config line.
  1084. */
  1085. int pci_read_irq_line(struct pci_dev *pci_dev)
  1086. {
  1087. u8 intpin;
  1088. struct device_node *node;
  1089. pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
  1090. if (intpin == 0)
  1091. return 0;
  1092. node = pci_device_to_OF_node(pci_dev);
  1093. if (node == NULL)
  1094. return -1;
  1095. if (node->n_intrs == 0)
  1096. return -1;
  1097. pci_dev->irq = node->intrs[0].line;
  1098. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  1099. return 0;
  1100. }
  1101. EXPORT_SYMBOL(pci_read_irq_line);
  1102. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1103. const struct resource *rsrc,
  1104. u64 *start, u64 *end)
  1105. {
  1106. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1107. unsigned long offset = 0;
  1108. if (hose == NULL)
  1109. return;
  1110. if (rsrc->flags & IORESOURCE_IO)
  1111. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  1112. hose->io_base_phys;
  1113. *start = rsrc->start + offset;
  1114. *end = rsrc->end + offset;
  1115. }
  1116. #endif /* CONFIG_PPC_MULTIPLATFORM */
  1117. #define IOBASE_BRIDGE_NUMBER 0
  1118. #define IOBASE_MEMORY 1
  1119. #define IOBASE_IO 2
  1120. #define IOBASE_ISA_IO 3
  1121. #define IOBASE_ISA_MEM 4
  1122. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  1123. unsigned long in_devfn)
  1124. {
  1125. struct pci_controller* hose;
  1126. struct list_head *ln;
  1127. struct pci_bus *bus = NULL;
  1128. struct device_node *hose_node;
  1129. /* Argh ! Please forgive me for that hack, but that's the
  1130. * simplest way to get existing XFree to not lockup on some
  1131. * G5 machines... So when something asks for bus 0 io base
  1132. * (bus 0 is HT root), we return the AGP one instead.
  1133. */
  1134. if (machine_is_compatible("MacRISC4"))
  1135. if (in_bus == 0)
  1136. in_bus = 0xf0;
  1137. /* That syscall isn't quite compatible with PCI domains, but it's
  1138. * used on pre-domains setup. We return the first match
  1139. */
  1140. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  1141. bus = pci_bus_b(ln);
  1142. if (in_bus >= bus->number && in_bus < (bus->number + bus->subordinate))
  1143. break;
  1144. bus = NULL;
  1145. }
  1146. if (bus == NULL || bus->sysdata == NULL)
  1147. return -ENODEV;
  1148. hose_node = (struct device_node *)bus->sysdata;
  1149. hose = PCI_DN(hose_node)->phb;
  1150. switch (which) {
  1151. case IOBASE_BRIDGE_NUMBER:
  1152. return (long)hose->first_busno;
  1153. case IOBASE_MEMORY:
  1154. return (long)hose->pci_mem_offset;
  1155. case IOBASE_IO:
  1156. return (long)hose->io_base_phys;
  1157. case IOBASE_ISA_IO:
  1158. return (long)isa_io_base;
  1159. case IOBASE_ISA_MEM:
  1160. return -EINVAL;
  1161. }
  1162. return -EOPNOTSUPP;
  1163. }