pci_64.c 35 KB

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