pci.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * Port for PPC64 David Engebretsen, IBM Corp.
  3. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  4. *
  5. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  6. * Rework, based on alpha PCI code.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #undef DEBUG
  14. #include <linux/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/pci.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/mm.h>
  21. #include <linux/list.h>
  22. #include <linux/syscalls.h>
  23. #include <asm/processor.h>
  24. #include <asm/io.h>
  25. #include <asm/prom.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/irq.h>
  29. #include <asm/machdep.h>
  30. #include <asm/udbg.h>
  31. #include "pci.h"
  32. #ifdef DEBUG
  33. #define DBG(fmt...) udbg_printf(fmt)
  34. #else
  35. #define DBG(fmt...)
  36. #endif
  37. unsigned long pci_probe_only = 1;
  38. unsigned long pci_assign_all_buses = 0;
  39. /*
  40. * legal IO pages under MAX_ISA_PORT. This is to ensure we don't touch
  41. * devices we don't have access to.
  42. */
  43. unsigned long io_page_mask;
  44. EXPORT_SYMBOL(io_page_mask);
  45. unsigned int pcibios_assign_all_busses(void)
  46. {
  47. return pci_assign_all_buses;
  48. }
  49. /* pci_io_base -- the base address from which io bars are offsets.
  50. * This is the lowest I/O base address (so bar values are always positive),
  51. * and it *must* be the start of ISA space if an ISA bus exists because
  52. * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
  53. * page is mapped and isa_io_limit prevents access to it.
  54. */
  55. unsigned long isa_io_base; /* NULL if no ISA bus */
  56. EXPORT_SYMBOL(isa_io_base);
  57. unsigned long pci_io_base;
  58. EXPORT_SYMBOL(pci_io_base);
  59. void iSeries_pcibios_init(void);
  60. LIST_HEAD(hose_list);
  61. struct dma_mapping_ops pci_dma_ops;
  62. EXPORT_SYMBOL(pci_dma_ops);
  63. int global_phb_number; /* Global phb counter */
  64. /* Cached ISA bridge dev. */
  65. struct pci_dev *ppc64_isabridge_dev = NULL;
  66. static void fixup_broken_pcnet32(struct pci_dev* dev)
  67. {
  68. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  69. dev->vendor = PCI_VENDOR_ID_AMD;
  70. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  71. }
  72. }
  73. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_TRIDENT, PCI_ANY_ID, fixup_broken_pcnet32);
  74. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  75. struct resource *res)
  76. {
  77. unsigned long offset = 0;
  78. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  79. if (!hose)
  80. return;
  81. if (res->flags & IORESOURCE_IO)
  82. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  83. if (res->flags & IORESOURCE_MEM)
  84. offset = hose->pci_mem_offset;
  85. region->start = res->start - offset;
  86. region->end = res->end - offset;
  87. }
  88. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  89. struct pci_bus_region *region)
  90. {
  91. unsigned long offset = 0;
  92. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  93. if (!hose)
  94. return;
  95. if (res->flags & IORESOURCE_IO)
  96. offset = (unsigned long)hose->io_base_virt - pci_io_base;
  97. if (res->flags & IORESOURCE_MEM)
  98. offset = hose->pci_mem_offset;
  99. res->start = region->start + offset;
  100. res->end = region->end + offset;
  101. }
  102. #ifdef CONFIG_HOTPLUG
  103. EXPORT_SYMBOL(pcibios_resource_to_bus);
  104. EXPORT_SYMBOL(pcibios_bus_to_resource);
  105. #endif
  106. /*
  107. * We need to avoid collisions with `mirrored' VGA ports
  108. * and other strange ISA hardware, so we always want the
  109. * addresses to be allocated in the 0x000-0x0ff region
  110. * modulo 0x400.
  111. *
  112. * Why? Because some silly external IO cards only decode
  113. * the low 10 bits of the IO address. The 0x00-0xff region
  114. * is reserved for motherboard devices that decode all 16
  115. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  116. * but we want to try to avoid allocating at 0x2900-0x2bff
  117. * which might have be mirrored at 0x0100-0x03ff..
  118. */
  119. void pcibios_align_resource(void *data, struct resource *res,
  120. unsigned long size, unsigned long align)
  121. {
  122. struct pci_dev *dev = data;
  123. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  124. unsigned long start = res->start;
  125. unsigned long alignto;
  126. if (res->flags & IORESOURCE_IO) {
  127. unsigned long offset = (unsigned long)hose->io_base_virt -
  128. pci_io_base;
  129. /* Make sure we start at our min on all hoses */
  130. if (start - offset < PCIBIOS_MIN_IO)
  131. start = PCIBIOS_MIN_IO + offset;
  132. /*
  133. * Put everything into 0x00-0xff region modulo 0x400
  134. */
  135. if (start & 0x300)
  136. start = (start + 0x3ff) & ~0x3ff;
  137. } else if (res->flags & IORESOURCE_MEM) {
  138. /* Make sure we start at our min on all hoses */
  139. if (start - hose->pci_mem_offset < PCIBIOS_MIN_MEM)
  140. start = PCIBIOS_MIN_MEM + hose->pci_mem_offset;
  141. /* Align to multiple of size of minimum base. */
  142. alignto = max(0x1000UL, align);
  143. start = ALIGN(start, alignto);
  144. }
  145. res->start = start;
  146. }
  147. static DEFINE_SPINLOCK(hose_spinlock);
  148. /*
  149. * pci_controller(phb) initialized common variables.
  150. */
  151. void __devinit pci_setup_pci_controller(struct pci_controller *hose)
  152. {
  153. memset(hose, 0, sizeof(struct pci_controller));
  154. spin_lock(&hose_spinlock);
  155. hose->global_number = global_phb_number++;
  156. list_add_tail(&hose->list_node, &hose_list);
  157. spin_unlock(&hose_spinlock);
  158. }
  159. static void __init pcibios_claim_one_bus(struct pci_bus *b)
  160. {
  161. struct pci_dev *dev;
  162. struct pci_bus *child_bus;
  163. list_for_each_entry(dev, &b->devices, bus_list) {
  164. int i;
  165. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  166. struct resource *r = &dev->resource[i];
  167. if (r->parent || !r->start || !r->flags)
  168. continue;
  169. pci_claim_resource(dev, i);
  170. }
  171. }
  172. list_for_each_entry(child_bus, &b->children, node)
  173. pcibios_claim_one_bus(child_bus);
  174. }
  175. #ifndef CONFIG_PPC_ISERIES
  176. static void __init pcibios_claim_of_setup(void)
  177. {
  178. struct pci_bus *b;
  179. list_for_each_entry(b, &pci_root_buses, node)
  180. pcibios_claim_one_bus(b);
  181. }
  182. #endif
  183. static int __init pcibios_init(void)
  184. {
  185. struct pci_controller *hose, *tmp;
  186. struct pci_bus *bus;
  187. /* For now, override phys_mem_access_prot. If we need it,
  188. * later, we may move that initialization to each ppc_md
  189. */
  190. ppc_md.phys_mem_access_prot = pci_phys_mem_access_prot;
  191. #ifdef CONFIG_PPC_ISERIES
  192. iSeries_pcibios_init();
  193. #endif
  194. printk("PCI: Probing PCI hardware\n");
  195. /* Scan all of the recorded PCI controllers. */
  196. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  197. hose->last_busno = 0xff;
  198. bus = pci_scan_bus(hose->first_busno, hose->ops,
  199. hose->arch_data);
  200. hose->bus = bus;
  201. hose->last_busno = bus->subordinate;
  202. }
  203. #ifndef CONFIG_PPC_ISERIES
  204. if (pci_probe_only)
  205. pcibios_claim_of_setup();
  206. else
  207. /* FIXME: `else' will be removed when
  208. pci_assign_unassigned_resources() is able to work
  209. correctly with [partially] allocated PCI tree. */
  210. pci_assign_unassigned_resources();
  211. #endif /* !CONFIG_PPC_ISERIES */
  212. /* Call machine dependent final fixup */
  213. if (ppc_md.pcibios_fixup)
  214. ppc_md.pcibios_fixup();
  215. /* Cache the location of the ISA bridge (if we have one) */
  216. ppc64_isabridge_dev = pci_get_class(PCI_CLASS_BRIDGE_ISA << 8, NULL);
  217. if (ppc64_isabridge_dev != NULL)
  218. printk("ISA bridge at %s\n", pci_name(ppc64_isabridge_dev));
  219. printk("PCI: Probing PCI hardware done\n");
  220. return 0;
  221. }
  222. subsys_initcall(pcibios_init);
  223. char __init *pcibios_setup(char *str)
  224. {
  225. return str;
  226. }
  227. int pcibios_enable_device(struct pci_dev *dev, int mask)
  228. {
  229. u16 cmd, oldcmd;
  230. int i;
  231. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  232. oldcmd = cmd;
  233. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  234. struct resource *res = &dev->resource[i];
  235. /* Only set up the requested stuff */
  236. if (!(mask & (1<<i)))
  237. continue;
  238. if (res->flags & IORESOURCE_IO)
  239. cmd |= PCI_COMMAND_IO;
  240. if (res->flags & IORESOURCE_MEM)
  241. cmd |= PCI_COMMAND_MEMORY;
  242. }
  243. if (cmd != oldcmd) {
  244. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  245. pci_name(dev), cmd);
  246. /* Enable the appropriate bits in the PCI command register. */
  247. pci_write_config_word(dev, PCI_COMMAND, cmd);
  248. }
  249. return 0;
  250. }
  251. /*
  252. * Return the domain number for this bus.
  253. */
  254. int pci_domain_nr(struct pci_bus *bus)
  255. {
  256. #ifdef CONFIG_PPC_ISERIES
  257. return 0;
  258. #else
  259. struct pci_controller *hose = pci_bus_to_host(bus);
  260. return hose->global_number;
  261. #endif
  262. }
  263. EXPORT_SYMBOL(pci_domain_nr);
  264. /* Decide whether to display the domain number in /proc */
  265. int pci_proc_domain(struct pci_bus *bus)
  266. {
  267. #ifdef CONFIG_PPC_ISERIES
  268. return 0;
  269. #else
  270. struct pci_controller *hose = pci_bus_to_host(bus);
  271. return hose->buid;
  272. #endif
  273. }
  274. /*
  275. * Platform support for /proc/bus/pci/X/Y mmap()s,
  276. * modelled on the sparc64 implementation by Dave Miller.
  277. * -- paulus.
  278. */
  279. /*
  280. * Adjust vm_pgoff of VMA such that it is the physical page offset
  281. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  282. *
  283. * Basically, the user finds the base address for his device which he wishes
  284. * to mmap. They read the 32-bit value from the config space base register,
  285. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  286. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  287. *
  288. * Returns negative error code on failure, zero on success.
  289. */
  290. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  291. unsigned long *offset,
  292. enum pci_mmap_state mmap_state)
  293. {
  294. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  295. unsigned long io_offset = 0;
  296. int i, res_bit;
  297. if (hose == 0)
  298. return NULL; /* should never happen */
  299. /* If memory, add on the PCI bridge address offset */
  300. if (mmap_state == pci_mmap_mem) {
  301. *offset += hose->pci_mem_offset;
  302. res_bit = IORESOURCE_MEM;
  303. } else {
  304. io_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  305. *offset += io_offset;
  306. res_bit = IORESOURCE_IO;
  307. }
  308. /*
  309. * Check that the offset requested corresponds to one of the
  310. * resources of the device.
  311. */
  312. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  313. struct resource *rp = &dev->resource[i];
  314. int flags = rp->flags;
  315. /* treat ROM as memory (should be already) */
  316. if (i == PCI_ROM_RESOURCE)
  317. flags |= IORESOURCE_MEM;
  318. /* Active and same type? */
  319. if ((flags & res_bit) == 0)
  320. continue;
  321. /* In the range of this resource? */
  322. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  323. continue;
  324. /* found it! construct the final physical address */
  325. if (mmap_state == pci_mmap_io)
  326. *offset += hose->io_base_phys - io_offset;
  327. return rp;
  328. }
  329. return NULL;
  330. }
  331. /*
  332. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  333. * device mapping.
  334. */
  335. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  336. pgprot_t protection,
  337. enum pci_mmap_state mmap_state,
  338. int write_combine)
  339. {
  340. unsigned long prot = pgprot_val(protection);
  341. /* Write combine is always 0 on non-memory space mappings. On
  342. * memory space, if the user didn't pass 1, we check for a
  343. * "prefetchable" resource. This is a bit hackish, but we use
  344. * this to workaround the inability of /sysfs to provide a write
  345. * combine bit
  346. */
  347. if (mmap_state != pci_mmap_mem)
  348. write_combine = 0;
  349. else if (write_combine == 0) {
  350. if (rp->flags & IORESOURCE_PREFETCH)
  351. write_combine = 1;
  352. }
  353. /* XXX would be nice to have a way to ask for write-through */
  354. prot |= _PAGE_NO_CACHE;
  355. if (write_combine)
  356. prot &= ~_PAGE_GUARDED;
  357. else
  358. prot |= _PAGE_GUARDED;
  359. printk("PCI map for %s:%lx, prot: %lx\n", pci_name(dev), rp->start,
  360. prot);
  361. return __pgprot(prot);
  362. }
  363. /*
  364. * This one is used by /dev/mem and fbdev who have no clue about the
  365. * PCI device, it tries to find the PCI device first and calls the
  366. * above routine
  367. */
  368. pgprot_t pci_phys_mem_access_prot(struct file *file,
  369. unsigned long offset,
  370. unsigned long size,
  371. pgprot_t protection)
  372. {
  373. struct pci_dev *pdev = NULL;
  374. struct resource *found = NULL;
  375. unsigned long prot = pgprot_val(protection);
  376. int i;
  377. if (page_is_ram(offset >> PAGE_SHIFT))
  378. return __pgprot(prot);
  379. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  380. for_each_pci_dev(pdev) {
  381. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  382. struct resource *rp = &pdev->resource[i];
  383. int flags = rp->flags;
  384. /* Active and same type? */
  385. if ((flags & IORESOURCE_MEM) == 0)
  386. continue;
  387. /* In the range of this resource? */
  388. if (offset < (rp->start & PAGE_MASK) ||
  389. offset > rp->end)
  390. continue;
  391. found = rp;
  392. break;
  393. }
  394. if (found)
  395. break;
  396. }
  397. if (found) {
  398. if (found->flags & IORESOURCE_PREFETCH)
  399. prot &= ~_PAGE_GUARDED;
  400. pci_dev_put(pdev);
  401. }
  402. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  403. return __pgprot(prot);
  404. }
  405. /*
  406. * Perform the actual remap of the pages for a PCI device mapping, as
  407. * appropriate for this architecture. The region in the process to map
  408. * is described by vm_start and vm_end members of VMA, the base physical
  409. * address is found in vm_pgoff.
  410. * The pci device structure is provided so that architectures may make mapping
  411. * decisions on a per-device or per-bus basis.
  412. *
  413. * Returns a negative error code on failure, zero on success.
  414. */
  415. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  416. enum pci_mmap_state mmap_state,
  417. int write_combine)
  418. {
  419. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  420. struct resource *rp;
  421. int ret;
  422. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  423. if (rp == NULL)
  424. return -EINVAL;
  425. vma->vm_pgoff = offset >> PAGE_SHIFT;
  426. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  427. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  428. vma->vm_page_prot,
  429. mmap_state, write_combine);
  430. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  431. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  432. return ret;
  433. }
  434. #ifdef CONFIG_PPC_MULTIPLATFORM
  435. static ssize_t pci_show_devspec(struct device *dev, struct device_attribute *attr, char *buf)
  436. {
  437. struct pci_dev *pdev;
  438. struct device_node *np;
  439. pdev = to_pci_dev (dev);
  440. np = pci_device_to_OF_node(pdev);
  441. if (np == NULL || np->full_name == NULL)
  442. return 0;
  443. return sprintf(buf, "%s", np->full_name);
  444. }
  445. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  446. #endif /* CONFIG_PPC_MULTIPLATFORM */
  447. void pcibios_add_platform_entries(struct pci_dev *pdev)
  448. {
  449. #ifdef CONFIG_PPC_MULTIPLATFORM
  450. device_create_file(&pdev->dev, &dev_attr_devspec);
  451. #endif /* CONFIG_PPC_MULTIPLATFORM */
  452. }
  453. #ifdef CONFIG_PPC_MULTIPLATFORM
  454. #define ISA_SPACE_MASK 0x1
  455. #define ISA_SPACE_IO 0x1
  456. static void __devinit pci_process_ISA_OF_ranges(struct device_node *isa_node,
  457. unsigned long phb_io_base_phys,
  458. void __iomem * phb_io_base_virt)
  459. {
  460. struct isa_range *range;
  461. unsigned long pci_addr;
  462. unsigned int isa_addr;
  463. unsigned int size;
  464. int rlen = 0;
  465. range = (struct isa_range *) get_property(isa_node, "ranges", &rlen);
  466. if (range == NULL || (rlen < sizeof(struct isa_range))) {
  467. printk(KERN_ERR "no ISA ranges or unexpected isa range size,"
  468. "mapping 64k\n");
  469. __ioremap_explicit(phb_io_base_phys,
  470. (unsigned long)phb_io_base_virt,
  471. 0x10000, _PAGE_NO_CACHE | _PAGE_GUARDED);
  472. return;
  473. }
  474. /* From "ISA Binding to 1275"
  475. * The ranges property is laid out as an array of elements,
  476. * each of which comprises:
  477. * cells 0 - 1: an ISA address
  478. * cells 2 - 4: a PCI address
  479. * (size depending on dev->n_addr_cells)
  480. * cell 5: the size of the range
  481. */
  482. if ((range->isa_addr.a_hi && ISA_SPACE_MASK) == ISA_SPACE_IO) {
  483. isa_addr = range->isa_addr.a_lo;
  484. pci_addr = (unsigned long) range->pci_addr.a_mid << 32 |
  485. range->pci_addr.a_lo;
  486. /* Assume these are both zero */
  487. if ((pci_addr != 0) || (isa_addr != 0)) {
  488. printk(KERN_ERR "unexpected isa to pci mapping: %s\n",
  489. __FUNCTION__);
  490. return;
  491. }
  492. size = PAGE_ALIGN(range->size);
  493. __ioremap_explicit(phb_io_base_phys,
  494. (unsigned long) phb_io_base_virt,
  495. size, _PAGE_NO_CACHE | _PAGE_GUARDED);
  496. }
  497. }
  498. void __devinit pci_process_bridge_OF_ranges(struct pci_controller *hose,
  499. struct device_node *dev)
  500. {
  501. unsigned int *ranges;
  502. unsigned long size;
  503. int rlen = 0;
  504. int memno = 0;
  505. struct resource *res;
  506. int np, na = prom_n_addr_cells(dev);
  507. unsigned long pci_addr, cpu_phys_addr;
  508. np = na + 5;
  509. /* From "PCI Binding to 1275"
  510. * The ranges property is laid out as an array of elements,
  511. * each of which comprises:
  512. * cells 0 - 2: a PCI address
  513. * cells 3 or 3+4: a CPU physical address
  514. * (size depending on dev->n_addr_cells)
  515. * cells 4+5 or 5+6: the size of the range
  516. */
  517. rlen = 0;
  518. hose->io_base_phys = 0;
  519. ranges = (unsigned int *) get_property(dev, "ranges", &rlen);
  520. while ((rlen -= np * sizeof(unsigned int)) >= 0) {
  521. res = NULL;
  522. pci_addr = (unsigned long)ranges[1] << 32 | ranges[2];
  523. cpu_phys_addr = ranges[3];
  524. if (na == 2)
  525. cpu_phys_addr = cpu_phys_addr << 32 | ranges[4];
  526. size = (unsigned long)ranges[na+3] << 32 | ranges[na+4];
  527. if (size == 0)
  528. continue;
  529. switch ((ranges[0] >> 24) & 0x3) {
  530. case 1: /* I/O space */
  531. hose->io_base_phys = cpu_phys_addr;
  532. hose->pci_io_size = size;
  533. res = &hose->io_resource;
  534. res->flags = IORESOURCE_IO;
  535. res->start = pci_addr;
  536. DBG("phb%d: IO 0x%lx -> 0x%lx\n", hose->global_number,
  537. res->start, res->start + size - 1);
  538. break;
  539. case 2: /* memory space */
  540. memno = 0;
  541. while (memno < 3 && hose->mem_resources[memno].flags)
  542. ++memno;
  543. if (memno == 0)
  544. hose->pci_mem_offset = cpu_phys_addr - pci_addr;
  545. if (memno < 3) {
  546. res = &hose->mem_resources[memno];
  547. res->flags = IORESOURCE_MEM;
  548. res->start = cpu_phys_addr;
  549. DBG("phb%d: MEM 0x%lx -> 0x%lx\n", hose->global_number,
  550. res->start, res->start + size - 1);
  551. }
  552. break;
  553. }
  554. if (res != NULL) {
  555. res->name = dev->full_name;
  556. res->end = res->start + size - 1;
  557. res->parent = NULL;
  558. res->sibling = NULL;
  559. res->child = NULL;
  560. }
  561. ranges += np;
  562. }
  563. }
  564. void __init pci_setup_phb_io(struct pci_controller *hose, int primary)
  565. {
  566. unsigned long size = hose->pci_io_size;
  567. unsigned long io_virt_offset;
  568. struct resource *res;
  569. struct device_node *isa_dn;
  570. hose->io_base_virt = reserve_phb_iospace(size);
  571. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  572. hose->global_number, hose->io_base_phys,
  573. (unsigned long) hose->io_base_virt);
  574. if (primary) {
  575. pci_io_base = (unsigned long)hose->io_base_virt;
  576. isa_dn = of_find_node_by_type(NULL, "isa");
  577. if (isa_dn) {
  578. isa_io_base = pci_io_base;
  579. pci_process_ISA_OF_ranges(isa_dn, hose->io_base_phys,
  580. hose->io_base_virt);
  581. of_node_put(isa_dn);
  582. /* Allow all IO */
  583. io_page_mask = -1;
  584. }
  585. }
  586. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  587. res = &hose->io_resource;
  588. res->start += io_virt_offset;
  589. res->end += io_virt_offset;
  590. }
  591. void __devinit pci_setup_phb_io_dynamic(struct pci_controller *hose,
  592. int primary)
  593. {
  594. unsigned long size = hose->pci_io_size;
  595. unsigned long io_virt_offset;
  596. struct resource *res;
  597. hose->io_base_virt = __ioremap(hose->io_base_phys, size,
  598. _PAGE_NO_CACHE | _PAGE_GUARDED);
  599. DBG("phb%d io_base_phys 0x%lx io_base_virt 0x%lx\n",
  600. hose->global_number, hose->io_base_phys,
  601. (unsigned long) hose->io_base_virt);
  602. if (primary)
  603. pci_io_base = (unsigned long)hose->io_base_virt;
  604. io_virt_offset = (unsigned long)hose->io_base_virt - pci_io_base;
  605. res = &hose->io_resource;
  606. res->start += io_virt_offset;
  607. res->end += io_virt_offset;
  608. }
  609. static int get_bus_io_range(struct pci_bus *bus, unsigned long *start_phys,
  610. unsigned long *start_virt, unsigned long *size)
  611. {
  612. struct pci_controller *hose = pci_bus_to_host(bus);
  613. struct pci_bus_region region;
  614. struct resource *res;
  615. if (bus->self) {
  616. res = bus->resource[0];
  617. pcibios_resource_to_bus(bus->self, &region, res);
  618. *start_phys = hose->io_base_phys + region.start;
  619. *start_virt = (unsigned long) hose->io_base_virt +
  620. region.start;
  621. if (region.end > region.start)
  622. *size = region.end - region.start + 1;
  623. else {
  624. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  625. __FUNCTION__, region.start, region.end);
  626. return 1;
  627. }
  628. } else {
  629. /* Root Bus */
  630. res = &hose->io_resource;
  631. *start_phys = hose->io_base_phys;
  632. *start_virt = (unsigned long) hose->io_base_virt;
  633. if (res->end > res->start)
  634. *size = res->end - res->start + 1;
  635. else {
  636. printk("%s(): unexpected region 0x%lx->0x%lx\n",
  637. __FUNCTION__, res->start, res->end);
  638. return 1;
  639. }
  640. }
  641. return 0;
  642. }
  643. int unmap_bus_range(struct pci_bus *bus)
  644. {
  645. unsigned long start_phys;
  646. unsigned long start_virt;
  647. unsigned long size;
  648. if (!bus) {
  649. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  650. return 1;
  651. }
  652. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  653. return 1;
  654. if (iounmap_explicit((void __iomem *) start_virt, size))
  655. return 1;
  656. return 0;
  657. }
  658. EXPORT_SYMBOL(unmap_bus_range);
  659. int remap_bus_range(struct pci_bus *bus)
  660. {
  661. unsigned long start_phys;
  662. unsigned long start_virt;
  663. unsigned long size;
  664. if (!bus) {
  665. printk(KERN_ERR "%s() expected bus\n", __FUNCTION__);
  666. return 1;
  667. }
  668. if (get_bus_io_range(bus, &start_phys, &start_virt, &size))
  669. return 1;
  670. printk("mapping IO %lx -> %lx, size: %lx\n", start_phys, start_virt, size);
  671. if (__ioremap_explicit(start_phys, start_virt, size,
  672. _PAGE_NO_CACHE | _PAGE_GUARDED))
  673. return 1;
  674. return 0;
  675. }
  676. EXPORT_SYMBOL(remap_bus_range);
  677. void phbs_remap_io(void)
  678. {
  679. struct pci_controller *hose, *tmp;
  680. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  681. remap_bus_range(hose->bus);
  682. }
  683. /*
  684. * ppc64 can have multifunction devices that do not respond to function 0.
  685. * In this case we must scan all functions.
  686. */
  687. int pcibios_scan_all_fns(struct pci_bus *bus, int devfn)
  688. {
  689. struct device_node *busdn, *dn;
  690. if (bus->self)
  691. busdn = pci_device_to_OF_node(bus->self);
  692. else
  693. busdn = bus->sysdata; /* must be a phb */
  694. if (busdn == NULL)
  695. return 0;
  696. /*
  697. * Check to see if there is any of the 8 functions are in the
  698. * device tree. If they are then we need to scan all the
  699. * functions of this slot.
  700. */
  701. for (dn = busdn->child; dn; dn = dn->sibling) {
  702. struct pci_dn *pdn = dn->data;
  703. if (pdn && (pdn->devfn >> 3) == (devfn >> 3))
  704. return 1;
  705. }
  706. return 0;
  707. }
  708. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  709. struct pci_bus *bus)
  710. {
  711. /* Update device resources. */
  712. struct pci_controller *hose = pci_bus_to_host(bus);
  713. int i;
  714. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  715. if (dev->resource[i].flags & IORESOURCE_IO) {
  716. unsigned long offset = (unsigned long)hose->io_base_virt
  717. - pci_io_base;
  718. unsigned long start, end, mask;
  719. start = dev->resource[i].start += offset;
  720. end = dev->resource[i].end += offset;
  721. /* Need to allow IO access to pages that are in the
  722. ISA range */
  723. if (start < MAX_ISA_PORT) {
  724. if (end > MAX_ISA_PORT)
  725. end = MAX_ISA_PORT;
  726. start >>= PAGE_SHIFT;
  727. end >>= PAGE_SHIFT;
  728. /* get the range of pages for the map */
  729. mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
  730. io_page_mask |= mask;
  731. }
  732. }
  733. else if (dev->resource[i].flags & IORESOURCE_MEM) {
  734. dev->resource[i].start += hose->pci_mem_offset;
  735. dev->resource[i].end += hose->pci_mem_offset;
  736. }
  737. }
  738. }
  739. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  740. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  741. {
  742. struct pci_controller *hose = pci_bus_to_host(bus);
  743. struct pci_dev *dev = bus->self;
  744. struct resource *res;
  745. int i;
  746. if (!dev) {
  747. /* Root bus. */
  748. hose->bus = bus;
  749. bus->resource[0] = res = &hose->io_resource;
  750. if (res->flags && request_resource(&ioport_resource, res))
  751. printk(KERN_ERR "Failed to request IO on "
  752. "PCI domain %d\n", pci_domain_nr(bus));
  753. for (i = 0; i < 3; ++i) {
  754. res = &hose->mem_resources[i];
  755. bus->resource[i+1] = res;
  756. if (res->flags && request_resource(&iomem_resource, res))
  757. printk(KERN_ERR "Failed to request MEM on "
  758. "PCI domain %d\n",
  759. pci_domain_nr(bus));
  760. }
  761. } else if (pci_probe_only &&
  762. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  763. /* This is a subordinate bridge */
  764. pci_read_bridge_bases(bus);
  765. pcibios_fixup_device_resources(dev, bus);
  766. }
  767. ppc_md.iommu_bus_setup(bus);
  768. list_for_each_entry(dev, &bus->devices, bus_list)
  769. ppc_md.iommu_dev_setup(dev);
  770. if (ppc_md.irq_bus_setup)
  771. ppc_md.irq_bus_setup(bus);
  772. if (!pci_probe_only)
  773. return;
  774. list_for_each_entry(dev, &bus->devices, bus_list) {
  775. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  776. pcibios_fixup_device_resources(dev, bus);
  777. }
  778. }
  779. EXPORT_SYMBOL(pcibios_fixup_bus);
  780. /*
  781. * Reads the interrupt pin to determine if interrupt is use by card.
  782. * If the interrupt is used, then gets the interrupt line from the
  783. * openfirmware and sets it in the pci_dev and pci_config line.
  784. */
  785. int pci_read_irq_line(struct pci_dev *pci_dev)
  786. {
  787. u8 intpin;
  788. struct device_node *node;
  789. pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
  790. if (intpin == 0)
  791. return 0;
  792. node = pci_device_to_OF_node(pci_dev);
  793. if (node == NULL)
  794. return -1;
  795. if (node->n_intrs == 0)
  796. return -1;
  797. pci_dev->irq = node->intrs[0].line;
  798. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  799. return 0;
  800. }
  801. EXPORT_SYMBOL(pci_read_irq_line);
  802. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  803. const struct resource *rsrc,
  804. u64 *start, u64 *end)
  805. {
  806. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  807. unsigned long offset = 0;
  808. if (hose == NULL)
  809. return;
  810. if (rsrc->flags & IORESOURCE_IO)
  811. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  812. hose->io_base_phys;
  813. *start = rsrc->start + offset;
  814. *end = rsrc->end + offset;
  815. }
  816. #endif /* CONFIG_PPC_MULTIPLATFORM */
  817. #define IOBASE_BRIDGE_NUMBER 0
  818. #define IOBASE_MEMORY 1
  819. #define IOBASE_IO 2
  820. #define IOBASE_ISA_IO 3
  821. #define IOBASE_ISA_MEM 4
  822. long sys_pciconfig_iobase(long which, unsigned long in_bus,
  823. unsigned long in_devfn)
  824. {
  825. struct pci_controller* hose;
  826. struct list_head *ln;
  827. struct pci_bus *bus = NULL;
  828. struct device_node *hose_node;
  829. /* Argh ! Please forgive me for that hack, but that's the
  830. * simplest way to get existing XFree to not lockup on some
  831. * G5 machines... So when something asks for bus 0 io base
  832. * (bus 0 is HT root), we return the AGP one instead.
  833. */
  834. #ifdef CONFIG_PPC_PMAC
  835. if (systemcfg->platform == PLATFORM_POWERMAC &&
  836. machine_is_compatible("MacRISC4"))
  837. if (in_bus == 0)
  838. in_bus = 0xf0;
  839. #endif /* CONFIG_PPC_PMAC */
  840. /* That syscall isn't quite compatible with PCI domains, but it's
  841. * used on pre-domains setup. We return the first match
  842. */
  843. for (ln = pci_root_buses.next; ln != &pci_root_buses; ln = ln->next) {
  844. bus = pci_bus_b(ln);
  845. if (in_bus >= bus->number && in_bus < (bus->number + bus->subordinate))
  846. break;
  847. bus = NULL;
  848. }
  849. if (bus == NULL || bus->sysdata == NULL)
  850. return -ENODEV;
  851. hose_node = (struct device_node *)bus->sysdata;
  852. hose = PCI_DN(hose_node)->phb;
  853. switch (which) {
  854. case IOBASE_BRIDGE_NUMBER:
  855. return (long)hose->first_busno;
  856. case IOBASE_MEMORY:
  857. return (long)hose->pci_mem_offset;
  858. case IOBASE_IO:
  859. return (long)hose->io_base_phys;
  860. case IOBASE_ISA_IO:
  861. return (long)isa_io_base;
  862. case IOBASE_ISA_MEM:
  863. return -EINVAL;
  864. }
  865. return -EOPNOTSUPP;
  866. }