pci.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. static 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. static void of_scan_pci_bridge(struct device_node *node, struct pci_dev *dev);
  296. static 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. static void __devinit of_scan_pci_bridge(struct device_node *node,
  319. struct pci_dev *dev)
  320. {
  321. struct pci_bus *bus;
  322. u32 *busrange, *ranges;
  323. int len, i, mode;
  324. struct resource *res;
  325. unsigned int flags;
  326. u64 size;
  327. /* parse bus-range property */
  328. busrange = (u32 *) get_property(node, "bus-range", &len);
  329. if (busrange == NULL || len != 8) {
  330. printk(KERN_ERR "Can't get bus-range for PCI-PCI bridge %s\n",
  331. node->full_name);
  332. return;
  333. }
  334. ranges = (u32 *) get_property(node, "ranges", &len);
  335. if (ranges == NULL) {
  336. printk(KERN_ERR "Can't get ranges for PCI-PCI bridge %s\n",
  337. node->full_name);
  338. return;
  339. }
  340. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  341. if (!bus) {
  342. printk(KERN_ERR "Failed to create pci bus for %s\n",
  343. node->full_name);
  344. return;
  345. }
  346. bus->primary = dev->bus->number;
  347. bus->subordinate = busrange[1];
  348. bus->bridge_ctl = 0;
  349. bus->sysdata = node;
  350. /* parse ranges property */
  351. /* PCI #address-cells == 3 and #size-cells == 2 always */
  352. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  353. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  354. res->flags = 0;
  355. bus->resource[i] = res;
  356. ++res;
  357. }
  358. i = 1;
  359. for (; len >= 32; len -= 32, ranges += 8) {
  360. flags = pci_parse_of_flags(ranges[0]);
  361. size = GET_64BIT(ranges, 6);
  362. if (flags == 0 || size == 0)
  363. continue;
  364. if (flags & IORESOURCE_IO) {
  365. res = bus->resource[0];
  366. if (res->flags) {
  367. printk(KERN_ERR "PCI: ignoring extra I/O range"
  368. " for bridge %s\n", node->full_name);
  369. continue;
  370. }
  371. } else {
  372. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  373. printk(KERN_ERR "PCI: too many memory ranges"
  374. " for bridge %s\n", node->full_name);
  375. continue;
  376. }
  377. res = bus->resource[i];
  378. ++i;
  379. }
  380. res->start = GET_64BIT(ranges, 1);
  381. res->end = res->start + size - 1;
  382. res->flags = flags;
  383. fixup_resource(res, dev);
  384. }
  385. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  386. bus->number);
  387. mode = PCI_PROBE_NORMAL;
  388. if (ppc_md.pci_probe_mode)
  389. mode = ppc_md.pci_probe_mode(bus);
  390. if (mode == PCI_PROBE_DEVTREE)
  391. of_scan_bus(node, bus);
  392. else if (mode == PCI_PROBE_NORMAL)
  393. pci_scan_child_bus(bus);
  394. }
  395. #endif /* CONFIG_PPC_MULTIPLATFORM */
  396. static void __devinit scan_phb(struct pci_controller *hose)
  397. {
  398. struct pci_bus *bus;
  399. struct device_node *node = hose->arch_data;
  400. int i, mode;
  401. struct resource *res;
  402. bus = pci_create_bus(NULL, hose->first_busno, hose->ops, node);
  403. if (bus == NULL) {
  404. printk(KERN_ERR "Failed to create bus for PCI domain %04x\n",
  405. hose->global_number);
  406. return;
  407. }
  408. bus->secondary = hose->first_busno;
  409. hose->bus = bus;
  410. bus->resource[0] = res = &hose->io_resource;
  411. if (res->flags && request_resource(&ioport_resource, res))
  412. printk(KERN_ERR "Failed to request PCI IO region "
  413. "on PCI domain %04x\n", hose->global_number);
  414. for (i = 0; i < 3; ++i) {
  415. res = &hose->mem_resources[i];
  416. bus->resource[i+1] = res;
  417. if (res->flags && request_resource(&iomem_resource, res))
  418. printk(KERN_ERR "Failed to request PCI memory region "
  419. "on PCI domain %04x\n", hose->global_number);
  420. }
  421. mode = PCI_PROBE_NORMAL;
  422. #ifdef CONFIG_PPC_MULTIPLATFORM
  423. if (ppc_md.pci_probe_mode)
  424. mode = ppc_md.pci_probe_mode(bus);
  425. if (mode == PCI_PROBE_DEVTREE) {
  426. bus->subordinate = hose->last_busno;
  427. of_scan_bus(node, bus);
  428. }
  429. #endif /* CONFIG_PPC_MULTIPLATFORM */
  430. if (mode == PCI_PROBE_NORMAL)
  431. hose->last_busno = bus->subordinate = pci_scan_child_bus(bus);
  432. pci_bus_add_devices(bus);
  433. }
  434. static int __init pcibios_init(void)
  435. {
  436. struct pci_controller *hose, *tmp;
  437. /* For now, override phys_mem_access_prot. If we need it,
  438. * later, we may move that initialization to each ppc_md
  439. */
  440. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  441. #ifdef CONFIG_PPC_ISERIES
  442. iSeries_pcibios_init();
  443. #endif
  444. printk("PCI: Probing PCI hardware\n");
  445. /* Scan all of the recorded PCI controllers. */
  446. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  447. scan_phb(hose);
  448. #ifndef CONFIG_PPC_ISERIES
  449. if (pci_probe_only)
  450. pcibios_claim_of_setup();
  451. else
  452. /* FIXME: `else' will be removed when
  453. pci_assign_unassigned_resources() is able to work
  454. correctly with [partially] allocated PCI tree. */
  455. pci_assign_unassigned_resources();
  456. #endif /* !CONFIG_PPC_ISERIES */
  457. /* Call machine dependent final fixup */
  458. if (ppc_md.pcibios_fixup)
  459. ppc_md.pcibios_fixup();
  460. /* Cache the location of the ISA bridge (if we have one) */
  461. ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  462. if (ppc64_isabridge_dev != NULL)
  463. printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
  464. printk("PCI: Probing PCI hardware done\n");
  465. return 0;
  466. }
  467. subsys_initcall(pcibios_init);
  468. char __init *pcibios_setup(char *str)
  469. {
  470. return str;
  471. }
  472. int pcibios_enable_device(struct pci_dev *dev, int mask)
  473. {
  474. u16 cmd, oldcmd;
  475. int i;
  476. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  477. oldcmd = cmd;
  478. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  479. struct resource *res = &dev->resource[i];
  480. /* Only set up the requested stuff */
  481. if (!(mask & (1<<i)))
  482. continue;
  483. if (res->flags & IORESOURCE_IO)
  484. cmd |= PCI_COMMAND_IO;
  485. if (res->flags & IORESOURCE_MEM)
  486. cmd |= PCI_COMMAND_MEMORY;
  487. }
  488. if (cmd != oldcmd) {
  489. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  490. pci_name(dev), cmd);
  491. /* Enable the appropriate bits in the PCI command register. */
  492. pci_write_config_word(dev, PCI_COMMAND, cmd);
  493. }
  494. return 0;
  495. }
  496. /*
  497. * Return the domain number for this bus.
  498. */
  499. int pci_domain_nr(struct pci_bus *bus)
  500. {
  501. #ifdef CONFIG_PPC_ISERIES
  502. return 0;
  503. #else
  504. struct pci_controller *hose = pci_bus_to_host(bus);
  505. return hose->global_number;
  506. #endif
  507. }
  508. EXPORT_SYMBOL(pci_domain_nr);
  509. /* Decide whether to display the domain number in /proc */
  510. int pci_proc_domain(struct pci_bus *bus)
  511. {
  512. #ifdef CONFIG_PPC_ISERIES
  513. return 0;
  514. #else
  515. struct pci_controller *hose = pci_bus_to_host(bus);
  516. return hose->buid;
  517. #endif
  518. }
  519. /*
  520. * Platform support for /proc/bus/pci/X/Y mmap()s,
  521. * modelled on the sparc64 implementation by Dave Miller.
  522. * -- paulus.
  523. */
  524. /*
  525. * Adjust vm_pgoff of VMA such that it is the physical page offset
  526. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  527. *
  528. * Basically, the user finds the base address for his device which he wishes
  529. * to mmap. They read the 32-bit value from the config space base register,
  530. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  531. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  532. *
  533. * Returns negative error code on failure, zero on success.
  534. */
  535. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  536. unsigned long *offset,
  537. enum pci_mmap_state mmap_state)
  538. {
  539. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  540. unsigned long io_offset = 0;
  541. int i, res_bit;
  542. if (hose == 0)
  543. return NULL; /* should never happen */
  544. /* If memory, add on the PCI bridge address offset */
  545. if (mmap_state == pci_mmap_mem) {
  546. *offset += hose->pci_mem_offset;
  547. res_bit = IORESOURCE_MEM;
  548. } else {
  549. io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  550. *offset += io_offset;
  551. res_bit = IORESOURCE_IO;
  552. }
  553. /*
  554. * Check that the offset requested corresponds to one of the
  555. * resources of the device.
  556. */
  557. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  558. struct resource *rp = &dev->resource[i];
  559. int flags = rp->flags;
  560. /* treat ROM as memory (should be already) */
  561. if (i == PCI_ROM_RESOURCE)
  562. flags |= IORESOURCE_MEM;
  563. /* Active and same type? */
  564. if ((flags & res_bit) == 0)
  565. continue;
  566. /* In the range of this resource? */
  567. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  568. continue;
  569. /* found it! construct the final physical address */
  570. if (mmap_state == pci_mmap_io)
  571. *offset += hose->io_base_phys - io_offset;
  572. return rp;
  573. }
  574. return NULL;
  575. }
  576. /*
  577. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  578. * device mapping.
  579. */
  580. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  581. pgprot_t protection,
  582. enum pci_mmap_state mmap_state,
  583. int write_combine)
  584. {
  585. unsigned long prot = pgprot_val(protection);
  586. /* Write combine is always 0 on non-memory space mappings. On
  587. * memory space, if the user didn't pass 1, we check for a
  588. * "prefetchable" resource. This is a bit hackish, but we use
  589. * this to workaround the inability of /sysfs to provide a write
  590. * combine bit
  591. */
  592. if (mmap_state != pci_mmap_mem)
  593. write_combine = 0;
  594. else if (write_combine == 0) {
  595. if (rp->flags & IORESOURCE_PREFETCH)
  596. write_combine = 1;
  597. }
  598. /* XXX would be nice to have a way to ask for write-through */
  599. prot |= _PAGE_NO_CACHE;
  600. if (write_combine)
  601. prot &= ~_PAGE_GUARDED;
  602. else
  603. prot |= _PAGE_GUARDED;
  604. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  605. prot);
  606. return __pgprot(prot);
  607. }
  608. /*
  609. * This one is used by /dev/mem and fbdev who have no clue about the
  610. * PCI device, it tries to find the PCI device first and calls the
  611. * above routine
  612. */
  613. pgprot_t pci_phys_mem_access_prot(struct file *file,
  614. unsigned long pfn,
  615. unsigned long size,
  616. pgprot_t protection)
  617. {
  618. struct pci_dev *pdev = NULL;
  619. struct resource *found = NULL;
  620. unsigned long prot = pgprot_val(protection);
  621. unsigned long offset = pfn << PAGE_SHIFT;
  622. int i;
  623. if (page_is_ram(pfn))
  624. return __pgprot(prot);
  625. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  626. for_each_pci_dev(pdev) {
  627. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  628. struct resource *rp = &pdev->resource[i];
  629. int flags = rp->flags;
  630. /* Active and same type? */
  631. if ((flags & IORESOURCE_MEM) == 0)
  632. continue;
  633. /* In the range of this resource? */
  634. if (offset < (rp->start & PAGE_MASK) ||
  635. offset > rp->end)
  636. continue;
  637. found = rp;
  638. break;
  639. }
  640. if (found)
  641. break;
  642. }
  643. if (found) {
  644. if (found->flags & IORESOURCE_PREFETCH)
  645. prot &= ~_PAGE_GUARDED;
  646. pci_dev_put(pdev);
  647. }
  648. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  649. return __pgprot(prot);
  650. }
  651. /*
  652. * Perform the actual remap of the pages for a PCI device mapping, as
  653. * appropriate for this architecture. The region in the process to map
  654. * is described by vm_start and vm_end members of VMA, the base physical
  655. * address is found in vm_pgoff.
  656. * The pci device structure is provided so that architectures may make mapping
  657. * decisions on a per-device or per-bus basis.
  658. *
  659. * Returns a negative error code on failure, zero on success.
  660. */
  661. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  662. enum pci_mmap_state mmap_state,
  663. int write_combine)
  664. {
  665. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  666. struct resource *rp;
  667. int ret;
  668. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  669. if (rp == NULL)
  670. return -EINVAL;
  671. vma->vm_pgoff = offset >> PAGE_SHIFT;
  672. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  673. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  674. vma->vm_page_prot,
  675. mmap_state, write_combine);
  676. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  677. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  678. return ret;
  679. }
  680. #ifdef CONFIG_PPC_MULTIPLATFORM
  681. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  682. {
  683. struct pci_dev *pdev;
  684. struct device_node *np;
  685. pdev = to_pci_dev (dev);
  686. np = pci_device_to_OF_node(pdev);
  687. if (np == NULL || np->full_name == NULL)
  688. return 0;
  689. return sprintf(buf, "%s", np->full_name);
  690. }
  691. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  692. #endif /* CONFIG_PPC_MULTIPLATFORM */
  693. void pcibios_add_platform_entries(struct pci_dev *pdev)
  694. {
  695. #ifdef CONFIG_PPC_MULTIPLATFORM
  696. device_create_file(&pdev->dev, &dev_attr_devspec);
  697. #endif /* CONFIG_PPC_MULTIPLATFORM */
  698. }
  699. #ifdef CONFIG_PPC_MULTIPLATFORM
  700. #define ISA_SPACE_MASK 0x1
  701. #define ISA_SPACE_IO 0x1
  702. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  703. unsigned long phb_io_base_phys,
  704. void __iomem * phb_io_base_virt)
  705. {
  706. struct isa_range *range;
  707. unsigned long pci_addr;
  708. unsigned int isa_addr;
  709. unsigned int size;
  710. int rlen = 0;
  711. range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
  712. if (range == NULL || (rlen < sizeof(struct isa_range))) {
  713. printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
  714. "mapping 64k\n");
  715. __ioremap_explicit(phb_io_base_phys,
  716. (unsigned long)phb_io_base_virt,
  717. 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
  718. return;
  719. }
  720. /* From "ISA Binding to 1275"
  721. * The ranges property is laid out as an array of elements,
  722. * each of which comprises:
  723. * cells 0 - 1: an ISA address
  724. * cells 2 - 4: a PCI address
  725. * (size depending on dev->n_addr_cells)
  726. * cell 5: the size of the range
  727. */
  728. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
  729. isa_addr = range->isa_addr.a_lo;
  730. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  731. range->pci_addr.a_lo;
  732. /* Assume these are both zero */
  733. if ((pci_addr != 0) || (isa_addr != 0)) {
  734. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  735. __FUNCTION__);
  736. return;
  737. }
  738. size = PAGE_ALIGN(range->size);
  739. __ioremap_explicit(phb_io_base_phys,
  740. (unsigned long) phb_io_base_virt,
  741. size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  742. }
  743. }
  744. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  745. struct device_node *dev, int prim)
  746. {
  747. unsigned int *ranges, pci_space;
  748. unsigned long size;
  749. int rlen = 0;
  750. int memno = 0;
  751. struct resource *res;
  752. int np, na = prom_n_addr_cells(dev);
  753. unsigned long pci_addr, cpu_phys_addr;
  754. np = na + 5;
  755. /* From "PCI Binding to 1275"
  756. * The ranges property is laid out as an array of elements,
  757. * each of which comprises:
  758. * cells 0 - 2: a PCI address
  759. * cells 3 or 3+4: a CPU physical address
  760. * (size depending on dev->n_addr_cells)
  761. * cells 4+5 or 5+6: the size of the range
  762. */
  763. rlen = 0;
  764. hose->io_base_phys = 0;
  765. ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  766. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  767. res = NULL;
  768. pci_space = ranges[0];
  769. pci_addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  770. cpu_phys_addr = ranges[3];
  771. if (na >= 2)
  772. cpu_phys_addr = (cpu_phys_addr << 32) | ranges[4];
  773. size = ((unsigned long)ranges[na+3] << 32) | ranges[na+4];
  774. ranges += np;
  775. if (size == 0)
  776. continue;
  777. /* Now consume following elements while they are contiguous */
  778. while (rlen >= np * sizeof(unsigned int)) {
  779. unsigned long addr, phys;
  780. if (ranges[0] != pci_space)
  781. break;
  782. addr = ((unsigned long)ranges[1] << 32) | ranges[2];
  783. phys = ranges[3];
  784. if (na >= 2)
  785. phys = (phys << 32) | ranges[4];
  786. if (addr != pci_addr + size ||
  787. phys != cpu_phys_addr + size)
  788. break;
  789. size += ((unsigned long)ranges[na+3] << 32)
  790. | ranges[na+4];
  791. ranges += np;
  792. rlen -= np * sizeof(unsigned int);
  793. }
  794. switch ((pci_space >> 24) & 0x3) {
  795. case 1: /* I/O space */
  796. hose->io_base_phys = cpu_phys_addr;
  797. hose->pci_io_size = size;
  798. res = &hose->io_resource;
  799. res->flags = IORESOURCE_IO;
  800. res->start = pci_addr;
  801. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  802. res->start, res->start + size - 1);
  803. break;
  804. case 2: /* memory space */
  805. memno = 0;
  806. while (memno < 3 && hose->mem_resources[memno].flags)
  807. ++memno;
  808. if (memno == 0)
  809. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  810. if (memno < 3) {
  811. res = &hose->mem_resources[memno];
  812. res->flags = IORESOURCE_MEM;
  813. res->start = cpu_phys_addr;
  814. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  815. res->start, res->start + size - 1);
  816. }
  817. break;
  818. }
  819. if (res != NULL) {
  820. res->name = dev->full_name;
  821. res->end = res->start + size - 1;
  822. res->parent = NULL;
  823. res->sibling = NULL;
  824. res->child = NULL;
  825. }
  826. }
  827. }
  828. void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
  829. {
  830. unsigned long size = hose->pci_io_size;
  831. unsigned long io_virt_offset;
  832. struct resource *res;
  833. struct device_node *isa_dn;
  834. hose->io_base_virt = reserve_phb_iospace(size);
  835. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  836. hose->global_number, hose->io_base_phys,
  837. (unsigned long) hose->io_base_virt);
  838. if (primary) {
  839. pci_io_base = (unsigned long)hose->io_base_virt;
  840. isa_dn = of_find_node_by_type(NULL, "isa");
  841. if (isa_dn) {
  842. isa_io_base = pci_io_base;
  843. pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
  844. hose->io_base_virt);
  845. of_node_put(isa_dn);
  846. /* Allow all IO */
  847. io_page_mask = -1;
  848. }
  849. }
  850. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  851. res = &hose->io_resource;
  852. res->start += io_virt_offset;
  853. res->end += io_virt_offset;
  854. }
  855. void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
  856. int primary)
  857. {
  858. unsigned long size = hose->pci_io_size;
  859. unsigned long io_virt_offset;
  860. struct resource *res;
  861. hose->io_base_virt = __ioremap(hose->io_base_phys, size,
  862. _PAGE_NO_CACHE | _PAGE_GUARDED);
  863. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  864. hose->global_number, hose->io_base_phys,
  865. (unsigned long) hose->io_base_virt);
  866. if (primary)
  867. pci_io_base = (unsigned long)hose->io_base_virt;
  868. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  869. res = &hose->io_resource;
  870. res->start += io_virt_offset;
  871. res->end += io_virt_offset;
  872. }
  873. static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
  874. unsigned long *start_virt, unsigned long *size)
  875. {
  876. struct pci_controller *hose = pci_bus_to_host(bus);
  877. struct pci_bus_region region;
  878. struct resource *res;
  879. if (bus->self) {
  880. res = bus->resource[0];
  881. pcibios_resource_to_bus(bus->self, &region, res);
  882. *start_phys = hose->io_base_phys + region.start;
  883. *start_virt = (unsigned long) hose->io_base_virt +
  884. region.start;
  885. if (region.end > region.start)
  886. *size = region.end - region.start + 1;
  887. else {
  888. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  889. __FUNCTION__, region.start, region.end);
  890. return 1;
  891. }
  892. } else {
  893. /* Root Bus */
  894. res = &hose->io_resource;
  895. *start_phys = hose->io_base_phys;
  896. *start_virt = (unsigned long) hose->io_base_virt;
  897. if (res->end > res->start)
  898. *size = res->end - res->start + 1;
  899. else {
  900. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  901. __FUNCTION__, res->start, res->end);
  902. return 1;
  903. }
  904. }
  905. return 0;
  906. }
  907. int unmap_bus_range(struct pci_bus *bus)
  908. {
  909. unsigned long start_phys;
  910. unsigned long start_virt;
  911. unsigned long size;
  912. if (!bus) {
  913. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  914. return 1;
  915. }
  916. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  917. return 1;
  918. if (iounmap_explicit((void __iomem *) start_virt, size))
  919. return 1;
  920. return 0;
  921. }
  922. EXPORT_SYMBOL(unmap_bus_range);
  923. int remap_bus_range(struct pci_bus *bus)
  924. {
  925. unsigned long start_phys;
  926. unsigned long start_virt;
  927. unsigned long size;
  928. if (!bus) {
  929. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  930. return 1;
  931. }
  932. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  933. return 1;
  934. printk("mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
  935. if (__ioremap_explicit(start_phys, start_virt, size,
  936. _PAGE_NO_CACHE | _PAGE_GUARDED))
  937. return 1;
  938. return 0;
  939. }
  940. EXPORT_SYMBOL(remap_bus_range);
  941. void phbs_remap_io(void)
  942. {
  943. struct pci_controller *hose, *tmp;
  944. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  945. remap_bus_range(hose->bus);
  946. }
  947. /*
  948. * ppc64 can have multifunction devices that do not respond to function 0.
  949. * In this case we must scan all functions.
  950. * XXX this can go now, we use the OF device tree in all the
  951. * cases that caused problems. -- paulus
  952. */
  953. int pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
  954. {
  955. return 0;
  956. }
  957. static void __devinit fixup_resource(struct resource *res, struct pci_dev *dev)
  958. {
  959. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  960. unsigned long start, end, mask, offset;
  961. if (res->flags & IORESOURCE_IO) {
  962. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  963. start = res->start += offset;
  964. end = res->end += offset;
  965. /* Need to allow IO access to pages that are in the
  966. ISA range */
  967. if (start < MAX_ISA_PORT) {
  968. if (end > MAX_ISA_PORT)
  969. end = MAX_ISA_PORT;
  970. start >>= PAGE_SHIFT;
  971. end >>= PAGE_SHIFT;
  972. /* get the range of pages for the map */
  973. mask = ((1 << (end+1)) - 1) ^ ((1 << start) - 1);
  974. io_page_mask |= mask;
  975. }
  976. } else if (res->flags & IORESOURCE_MEM) {
  977. res->start += hose->pci_mem_offset;
  978. res->end += hose->pci_mem_offset;
  979. }
  980. }
  981. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  982. struct pci_bus *bus)
  983. {
  984. /* Update device resources. */
  985. int i;
  986. for (i = 0; i < PCI_NUM_RESOURCES; i++)
  987. if (dev->resource[i].flags)
  988. fixup_resource(&dev->resource[i], dev);
  989. }
  990. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  991. static void __devinit do_bus_setup(struct pci_bus *bus)
  992. {
  993. struct pci_dev *dev;
  994. ppc_md.iommu_bus_setup(bus);
  995. list_for_each_entry(dev, &bus->devices, bus_list)
  996. ppc_md.iommu_dev_setup(dev);
  997. if (ppc_md.irq_bus_setup)
  998. ppc_md.irq_bus_setup(bus);
  999. }
  1000. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  1001. {
  1002. struct pci_dev *dev = bus->self;
  1003. if (dev && pci_probe_only &&
  1004. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1005. /* This is a subordinate bridge */
  1006. pci_read_bridge_bases(bus);
  1007. pcibios_fixup_device_resources(dev, bus);
  1008. }
  1009. do_bus_setup(bus);
  1010. if (!pci_probe_only)
  1011. return;
  1012. list_for_each_entry(dev, &bus->devices, bus_list)
  1013. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  1014. pcibios_fixup_device_resources(dev, bus);
  1015. }
  1016. EXPORT_SYMBOL(pcibios_fixup_bus);
  1017. /*
  1018. * Reads the interrupt pin to determine if interrupt is use by card.
  1019. * If the interrupt is used, then gets the interrupt line from the
  1020. * openfirmware and sets it in the pci_dev and pci_config line.
  1021. */
  1022. int pci_read_irq_line(struct pci_dev *pci_dev)
  1023. {
  1024. u8 intpin;
  1025. struct device_node *node;
  1026. pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
  1027. if (intpin == 0)
  1028. return 0;
  1029. node = pci_device_to_OF_node(pci_dev);
  1030. if (node == NULL)
  1031. return -1;
  1032. if (node->n_intrs == 0)
  1033. return -1;
  1034. pci_dev->irq = node->intrs[0].line;
  1035. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  1036. return 0;
  1037. }
  1038. EXPORT_SYMBOL(pci_read_irq_line);
  1039. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  1040. const struct resource *rsrc,
  1041. u64 *start, u64 *end)
  1042. {
  1043. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1044. unsigned long offset = 0;
  1045. if (hose == NULL)
  1046. return;
  1047. if (rsrc->flags & IORESOURCE_IO)
  1048. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  1049. hose->io_base_phys;
  1050. *start = rsrc->start + offset;
  1051. *end = rsrc->end + offset;
  1052. }
  1053. #endif /* CONFIG_PPC_MULTIPLATFORM */
  1054. #define IOBASE_BRIDGE_NUMBER 0
  1055. #define IOBASE_MEMORY 1
  1056. #define IOBASE_IO 2
  1057. #define IOBASE_ISA_IO 3
  1058. #define IOBASE_ISA_MEM 4
  1059. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  1060. unsigned long in_devfn)
  1061. {
  1062. struct pci_controller* hose;
  1063. struct list_head *ln;
  1064. struct pci_bus *bus = NULL;
  1065. struct device_node *hose_node;
  1066. /* Argh ! Please forgive me for that hack, but that's the
  1067. * simplest way to get existing XFree to not lockup on some
  1068. * G5 machines... So when something asks for bus 0 io base
  1069. * (bus 0 is HT root), we return the AGP one instead.
  1070. */
  1071. #ifdef CONFIG_PPC_PMAC
  1072. if (systemcfg->platform == PLATFORM_POWERMAC &&
  1073. machine_is_compatible("MacRISC4"))
  1074. if (in_bus == 0)
  1075. in_bus = 0xf0;
  1076. #endif /* CONFIG_PPC_PMAC */
  1077. /* That syscall isn't quite compatible with PCI domains, but it's
  1078. * used on pre-domains setup. We return the first match
  1079. */
  1080. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  1081. bus = pci_bus_b(ln);
  1082. if (in_bus >= bus->number && in_bus < (bus->number + bus->subordinate))
  1083. break;
  1084. bus = NULL;
  1085. }
  1086. if (bus == NULL || bus->sysdata == NULL)
  1087. return -ENODEV;
  1088. hose_node = (struct device_node *)bus->sysdata;
  1089. hose = PCI_DN(hose_node)->phb;
  1090. switch (which) {
  1091. case IOBASE_BRIDGE_NUMBER:
  1092. return (long)hose->first_busno;
  1093. case IOBASE_MEMORY:
  1094. return (long)hose->pci_mem_offset;
  1095. case IOBASE_IO:
  1096. return (long)hose->io_base_phys;
  1097. case IOBASE_ISA_IO:
  1098. return (long)isa_io_base;
  1099. case IOBASE_ISA_MEM:
  1100. return -EINVAL;
  1101. }
  1102. return -EOPNOTSUPP;
  1103. }