pci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 <asm/processor.h>
  23. #include <asm/io.h>
  24. #include <asm/prom.h>
  25. #include <asm/pci-bridge.h>
  26. #include <asm/byteorder.h>
  27. #include <asm/irq.h>
  28. #include <asm/machdep.h>
  29. #include <asm/udbg.h>
  30. #include "pci.h"
  31. #ifdef DEBUG
  32. #define DBG(fmt...) udbg_printf(fmt)
  33. #else
  34. #define DBG(fmt...)
  35. #endif
  36. unsigned long pci_probe_only = 1;
  37. unsigned long pci_assign_all_buses = 0;
  38. /*
  39. * legal IO pages under MAX_ISA_PORT. This is to ensure we don't touch
  40. * devices we don't have access to.
  41. */
  42. unsigned long io_page_mask;
  43. EXPORT_SYMBOL(io_page_mask);
  44. unsigned int pcibios_assign_all_busses(void)
  45. {
  46. return pci_assign_all_buses;
  47. }
  48. /* pci_io_base -- the base address from which io bars are offsets.
  49. * This is the lowest I/O base address (so bar values are always positive),
  50. * and it *must* be the start of ISA space if an ISA bus exists because
  51. * ISA drivers use hard coded offsets. If no ISA bus exists a dummy
  52. * page is mapped and isa_io_limit prevents access to it.
  53. */
  54. unsigned long isa_io_base; /* NULL if no ISA bus */
  55. EXPORT_SYMBOL(isa_io_base);
  56. unsigned long pci_io_base;
  57. EXPORT_SYMBOL(pci_io_base);
  58. void iSeries_pcibios_init(void);
  59. LIST_HEAD(hose_list);
  60. struct dma_mapping_ops pci_dma_ops;
  61. EXPORT_SYMBOL(pci_dma_ops);
  62. int global_phb_number; /* Global phb counter */
  63. /* Cached ISA bridge dev. */
  64. struct pci_dev *ppc64_isabridge_dev = NULL;
  65. static void fixup_broken_pcnet32(struct pci_dev* dev)
  66. {
  67. if ((dev->class>>8 == PCI_CLASS_NETWORK_ETHERNET)) {
  68. dev->vendor = PCI_VENDOR_ID_AMD;
  69. pci_write_config_word(dev, PCI_VENDOR_ID, PCI_VENDOR_ID_AMD);
  70. pci_name_device(dev);
  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. if ((dn->devfn >> 3) == (devfn >> 3))
  703. return 1;
  704. return 0;
  705. }
  706. void __devinit pcibios_fixup_device_resources(struct pci_dev *dev,
  707. struct pci_bus *bus)
  708. {
  709. /* Update device resources. */
  710. struct pci_controller *hose = pci_bus_to_host(bus);
  711. int i;
  712. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  713. if (dev->resource[i].flags & IORESOURCE_IO) {
  714. unsigned long offset = (unsigned long)hose->io_base_virt
  715. - pci_io_base;
  716. unsigned long start, end, mask;
  717. start = dev->resource[i].start += offset;
  718. end = dev->resource[i].end += offset;
  719. /* Need to allow IO access to pages that are in the
  720. ISA range */
  721. if (start < MAX_ISA_PORT) {
  722. if (end > MAX_ISA_PORT)
  723. end = MAX_ISA_PORT;
  724. start >>= PAGE_SHIFT;
  725. end >>= PAGE_SHIFT;
  726. /* get the range of pages for the map */
  727. mask = ((1 << (end+1))-1) ^ ((1 << start)-1);
  728. io_page_mask |= mask;
  729. }
  730. }
  731. else if (dev->resource[i].flags & IORESOURCE_MEM) {
  732. dev->resource[i].start += hose->pci_mem_offset;
  733. dev->resource[i].end += hose->pci_mem_offset;
  734. }
  735. }
  736. }
  737. EXPORT_SYMBOL(pcibios_fixup_device_resources);
  738. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  739. {
  740. struct pci_controller *hose = pci_bus_to_host(bus);
  741. struct pci_dev *dev = bus->self;
  742. struct resource *res;
  743. int i;
  744. if (!dev) {
  745. /* Root bus. */
  746. hose->bus = bus;
  747. bus->resource[0] = res = &hose->io_resource;
  748. if (res->flags && request_resource(&ioport_resource, res))
  749. printk(KERN_ERR "Failed to request IO on "
  750. "PCI domain %d\n", pci_domain_nr(bus));
  751. for (i = 0; i < 3; ++i) {
  752. res = &hose->mem_resources[i];
  753. bus->resource[i+1] = res;
  754. if (res->flags && request_resource(&iomem_resource, res))
  755. printk(KERN_ERR "Failed to request MEM on "
  756. "PCI domain %d\n",
  757. pci_domain_nr(bus));
  758. }
  759. } else if (pci_probe_only &&
  760. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  761. /* This is a subordinate bridge */
  762. pci_read_bridge_bases(bus);
  763. pcibios_fixup_device_resources(dev, bus);
  764. }
  765. ppc_md.iommu_bus_setup(bus);
  766. list_for_each_entry(dev, &bus->devices, bus_list)
  767. ppc_md.iommu_dev_setup(dev);
  768. if (ppc_md.irq_bus_setup)
  769. ppc_md.irq_bus_setup(bus);
  770. if (!pci_probe_only)
  771. return;
  772. list_for_each_entry(dev, &bus->devices, bus_list) {
  773. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  774. pcibios_fixup_device_resources(dev, bus);
  775. }
  776. }
  777. EXPORT_SYMBOL(pcibios_fixup_bus);
  778. /*
  779. * Reads the interrupt pin to determine if interrupt is use by card.
  780. * If the interrupt is used, then gets the interrupt line from the
  781. * openfirmware and sets it in the pci_dev and pci_config line.
  782. */
  783. int pci_read_irq_line(struct pci_dev *pci_dev)
  784. {
  785. u8 intpin;
  786. struct device_node *node;
  787. pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &intpin);
  788. if (intpin == 0)
  789. return 0;
  790. node = pci_device_to_OF_node(pci_dev);
  791. if (node == NULL)
  792. return -1;
  793. if (node->n_intrs == 0)
  794. return -1;
  795. pci_dev->irq = node->intrs[0].line;
  796. pci_write_config_byte(pci_dev, PCI_INTERRUPT_LINE, pci_dev->irq);
  797. return 0;
  798. }
  799. EXPORT_SYMBOL(pci_read_irq_line);
  800. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  801. const struct resource *rsrc,
  802. u64 *start, u64 *end)
  803. {
  804. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  805. unsigned long offset = 0;
  806. if (hose == NULL)
  807. return;
  808. if (rsrc->flags & IORESOURCE_IO)
  809. offset = pci_io_base - (unsigned long)hose->io_base_virt +
  810. hose->io_base_phys;
  811. *start = rsrc->start + offset;
  812. *end = rsrc->end + offset;
  813. }
  814. #endif /* CONFIG_PPC_MULTIPLATFORM */