pci_64.c 36 KB

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