pci_64.c 36 KB

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