pci_64.c 36 KB

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