pci_64.c 38 KB

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