pci_64.c 36 KB

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