pci.c 34 KB

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