pci_64.c 38 KB

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