pci_64.c 36 KB

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