pci_64.c 38 KB

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