pci-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Contains common pci routines for ALL ppc platform
  3. * (based on pci_32.c and pci_64.c)
  4. *
  5. * Port for PPC64 David Engebretsen, IBM Corp.
  6. * Contains common pci routines for ppc64 platform, pSeries and iSeries brands.
  7. *
  8. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  9. * Rework, based on alpha PCI code.
  10. *
  11. * Common pmac/prep/chrp pci routines. -- Cort
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #undef DEBUG
  19. #include <linux/kernel.h>
  20. #include <linux/pci.h>
  21. #include <linux/string.h>
  22. #include <linux/init.h>
  23. #include <linux/bootmem.h>
  24. #include <linux/mm.h>
  25. #include <linux/list.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/irq.h>
  28. #include <linux/vmalloc.h>
  29. #include <asm/processor.h>
  30. #include <asm/io.h>
  31. #include <asm/prom.h>
  32. #include <asm/pci-bridge.h>
  33. #include <asm/byteorder.h>
  34. #include <asm/machdep.h>
  35. #include <asm/ppc-pci.h>
  36. #include <asm/firmware.h>
  37. #ifdef DEBUG
  38. #include <asm/udbg.h>
  39. #define DBG(fmt...) printk(fmt)
  40. #else
  41. #define DBG(fmt...)
  42. #endif
  43. static DEFINE_SPINLOCK(hose_spinlock);
  44. /* XXX kill that some day ... */
  45. int global_phb_number; /* Global phb counter */
  46. extern struct list_head hose_list;
  47. /*
  48. * pci_controller(phb) initialized common variables.
  49. */
  50. static void __devinit pci_setup_pci_controller(struct pci_controller *hose)
  51. {
  52. memset(hose, 0, sizeof(struct pci_controller));
  53. spin_lock(&hose_spinlock);
  54. hose->global_number = global_phb_number++;
  55. list_add_tail(&hose->list_node, &hose_list);
  56. spin_unlock(&hose_spinlock);
  57. }
  58. __init_refok struct pci_controller * pcibios_alloc_controller(struct device_node *dev)
  59. {
  60. struct pci_controller *phb;
  61. if (mem_init_done)
  62. phb = kmalloc(sizeof(struct pci_controller), GFP_KERNEL);
  63. else
  64. phb = alloc_bootmem(sizeof (struct pci_controller));
  65. if (phb == NULL)
  66. return NULL;
  67. pci_setup_pci_controller(phb);
  68. phb->arch_data = dev;
  69. phb->is_dynamic = mem_init_done;
  70. #ifdef CONFIG_PPC64
  71. if (dev) {
  72. int nid = of_node_to_nid(dev);
  73. if (nid < 0 || !node_online(nid))
  74. nid = -1;
  75. PHB_SET_NODE(phb, nid);
  76. }
  77. #endif
  78. return phb;
  79. }
  80. void pcibios_free_controller(struct pci_controller *phb)
  81. {
  82. spin_lock(&hose_spinlock);
  83. list_del(&phb->list_node);
  84. spin_unlock(&hose_spinlock);
  85. if (phb->is_dynamic)
  86. kfree(phb);
  87. }
  88. int pcibios_vaddr_is_ioport(void __iomem *address)
  89. {
  90. int ret = 0;
  91. struct pci_controller *hose;
  92. unsigned long size;
  93. spin_lock(&hose_spinlock);
  94. list_for_each_entry(hose, &hose_list, list_node) {
  95. #ifdef CONFIG_PPC64
  96. size = hose->pci_io_size;
  97. #else
  98. size = hose->io_resource.end - hose->io_resource.start + 1;
  99. #endif
  100. if (address >= hose->io_base_virt &&
  101. address < (hose->io_base_virt + size)) {
  102. ret = 1;
  103. break;
  104. }
  105. }
  106. spin_unlock(&hose_spinlock);
  107. return ret;
  108. }
  109. /*
  110. * Return the domain number for this bus.
  111. */
  112. int pci_domain_nr(struct pci_bus *bus)
  113. {
  114. if (firmware_has_feature(FW_FEATURE_ISERIES))
  115. return 0;
  116. else {
  117. struct pci_controller *hose = pci_bus_to_host(bus);
  118. return hose->global_number;
  119. }
  120. }
  121. EXPORT_SYMBOL(pci_domain_nr);
  122. #ifdef CONFIG_PPC_OF
  123. /* This routine is meant to be used early during boot, when the
  124. * PCI bus numbers have not yet been assigned, and you need to
  125. * issue PCI config cycles to an OF device.
  126. * It could also be used to "fix" RTAS config cycles if you want
  127. * to set pci_assign_all_buses to 1 and still use RTAS for PCI
  128. * config cycles.
  129. */
  130. struct pci_controller* pci_find_hose_for_OF_device(struct device_node* node)
  131. {
  132. if (!have_of)
  133. return NULL;
  134. while(node) {
  135. struct pci_controller *hose, *tmp;
  136. list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
  137. if (hose->arch_data == node)
  138. return hose;
  139. node = node->parent;
  140. }
  141. return NULL;
  142. }
  143. static ssize_t pci_show_devspec(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct pci_dev *pdev;
  147. struct device_node *np;
  148. pdev = to_pci_dev (dev);
  149. np = pci_device_to_OF_node(pdev);
  150. if (np == NULL || np->full_name == NULL)
  151. return 0;
  152. return sprintf(buf, "%s", np->full_name);
  153. }
  154. static DEVICE_ATTR(devspec, S_IRUGO, pci_show_devspec, NULL);
  155. #endif /* CONFIG_PPC_OF */
  156. /* Add sysfs properties */
  157. int pcibios_add_platform_entries(struct pci_dev *pdev)
  158. {
  159. #ifdef CONFIG_PPC_OF
  160. return device_create_file(&pdev->dev, &dev_attr_devspec);
  161. #else
  162. return 0;
  163. #endif /* CONFIG_PPC_OF */
  164. }
  165. char __devinit *pcibios_setup(char *str)
  166. {
  167. return str;
  168. }
  169. /*
  170. * Reads the interrupt pin to determine if interrupt is use by card.
  171. * If the interrupt is used, then gets the interrupt line from the
  172. * openfirmware and sets it in the pci_dev and pci_config line.
  173. */
  174. int pci_read_irq_line(struct pci_dev *pci_dev)
  175. {
  176. struct of_irq oirq;
  177. unsigned int virq;
  178. DBG("Try to map irq for %s...\n", pci_name(pci_dev));
  179. #ifdef DEBUG
  180. memset(&oirq, 0xff, sizeof(oirq));
  181. #endif
  182. /* Try to get a mapping from the device-tree */
  183. if (of_irq_map_pci(pci_dev, &oirq)) {
  184. u8 line, pin;
  185. /* If that fails, lets fallback to what is in the config
  186. * space and map that through the default controller. We
  187. * also set the type to level low since that's what PCI
  188. * interrupts are. If your platform does differently, then
  189. * either provide a proper interrupt tree or don't use this
  190. * function.
  191. */
  192. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_PIN, &pin))
  193. return -1;
  194. if (pin == 0)
  195. return -1;
  196. if (pci_read_config_byte(pci_dev, PCI_INTERRUPT_LINE, &line) ||
  197. line == 0xff) {
  198. return -1;
  199. }
  200. DBG(" -> no map ! Using irq line %d from PCI config\n", line);
  201. virq = irq_create_mapping(NULL, line);
  202. if (virq != NO_IRQ)
  203. set_irq_type(virq, IRQ_TYPE_LEVEL_LOW);
  204. } else {
  205. DBG(" -> got one, spec %d cells (0x%08x 0x%08x...) on %s\n",
  206. oirq.size, oirq.specifier[0], oirq.specifier[1],
  207. oirq.controller->full_name);
  208. virq = irq_create_of_mapping(oirq.controller, oirq.specifier,
  209. oirq.size);
  210. }
  211. if(virq == NO_IRQ) {
  212. DBG(" -> failed to map !\n");
  213. return -1;
  214. }
  215. DBG(" -> mapped to linux irq %d\n", virq);
  216. pci_dev->irq = virq;
  217. return 0;
  218. }
  219. EXPORT_SYMBOL(pci_read_irq_line);
  220. /*
  221. * Platform support for /proc/bus/pci/X/Y mmap()s,
  222. * modelled on the sparc64 implementation by Dave Miller.
  223. * -- paulus.
  224. */
  225. /*
  226. * Adjust vm_pgoff of VMA such that it is the physical page offset
  227. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  228. *
  229. * Basically, the user finds the base address for his device which he wishes
  230. * to mmap. They read the 32-bit value from the config space base register,
  231. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  232. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  233. *
  234. * Returns negative error code on failure, zero on success.
  235. */
  236. static struct resource *__pci_mmap_make_offset(struct pci_dev *dev,
  237. resource_size_t *offset,
  238. enum pci_mmap_state mmap_state)
  239. {
  240. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  241. unsigned long io_offset = 0;
  242. int i, res_bit;
  243. if (hose == 0)
  244. return NULL; /* should never happen */
  245. /* If memory, add on the PCI bridge address offset */
  246. if (mmap_state == pci_mmap_mem) {
  247. #if 0 /* See comment in pci_resource_to_user() for why this is disabled */
  248. *offset += hose->pci_mem_offset;
  249. #endif
  250. res_bit = IORESOURCE_MEM;
  251. } else {
  252. io_offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  253. *offset += io_offset;
  254. res_bit = IORESOURCE_IO;
  255. }
  256. /*
  257. * Check that the offset requested corresponds to one of the
  258. * resources of the device.
  259. */
  260. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  261. struct resource *rp = &dev->resource[i];
  262. int flags = rp->flags;
  263. /* treat ROM as memory (should be already) */
  264. if (i == PCI_ROM_RESOURCE)
  265. flags |= IORESOURCE_MEM;
  266. /* Active and same type? */
  267. if ((flags & res_bit) == 0)
  268. continue;
  269. /* In the range of this resource? */
  270. if (*offset < (rp->start & PAGE_MASK) || *offset > rp->end)
  271. continue;
  272. /* found it! construct the final physical address */
  273. if (mmap_state == pci_mmap_io)
  274. *offset += hose->io_base_phys - io_offset;
  275. return rp;
  276. }
  277. return NULL;
  278. }
  279. /*
  280. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  281. * device mapping.
  282. */
  283. static pgprot_t __pci_mmap_set_pgprot(struct pci_dev *dev, struct resource *rp,
  284. pgprot_t protection,
  285. enum pci_mmap_state mmap_state,
  286. int write_combine)
  287. {
  288. unsigned long prot = pgprot_val(protection);
  289. /* Write combine is always 0 on non-memory space mappings. On
  290. * memory space, if the user didn't pass 1, we check for a
  291. * "prefetchable" resource. This is a bit hackish, but we use
  292. * this to workaround the inability of /sysfs to provide a write
  293. * combine bit
  294. */
  295. if (mmap_state != pci_mmap_mem)
  296. write_combine = 0;
  297. else if (write_combine == 0) {
  298. if (rp->flags & IORESOURCE_PREFETCH)
  299. write_combine = 1;
  300. }
  301. /* XXX would be nice to have a way to ask for write-through */
  302. prot |= _PAGE_NO_CACHE;
  303. if (write_combine)
  304. prot &= ~_PAGE_GUARDED;
  305. else
  306. prot |= _PAGE_GUARDED;
  307. return __pgprot(prot);
  308. }
  309. /*
  310. * This one is used by /dev/mem and fbdev who have no clue about the
  311. * PCI device, it tries to find the PCI device first and calls the
  312. * above routine
  313. */
  314. pgprot_t pci_phys_mem_access_prot(struct file *file,
  315. unsigned long pfn,
  316. unsigned long size,
  317. pgprot_t protection)
  318. {
  319. struct pci_dev *pdev = NULL;
  320. struct resource *found = NULL;
  321. unsigned long prot = pgprot_val(protection);
  322. unsigned long offset = pfn << PAGE_SHIFT;
  323. int i;
  324. if (page_is_ram(pfn))
  325. return __pgprot(prot);
  326. prot |= _PAGE_NO_CACHE | _PAGE_GUARDED;
  327. for_each_pci_dev(pdev) {
  328. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  329. struct resource *rp = &pdev->resource[i];
  330. int flags = rp->flags;
  331. /* Active and same type? */
  332. if ((flags & IORESOURCE_MEM) == 0)
  333. continue;
  334. /* In the range of this resource? */
  335. if (offset < (rp->start & PAGE_MASK) ||
  336. offset > rp->end)
  337. continue;
  338. found = rp;
  339. break;
  340. }
  341. if (found)
  342. break;
  343. }
  344. if (found) {
  345. if (found->flags & IORESOURCE_PREFETCH)
  346. prot &= ~_PAGE_GUARDED;
  347. pci_dev_put(pdev);
  348. }
  349. DBG("non-PCI map for %lx, prot: %lx\n", offset, prot);
  350. return __pgprot(prot);
  351. }
  352. /*
  353. * Perform the actual remap of the pages for a PCI device mapping, as
  354. * appropriate for this architecture. The region in the process to map
  355. * is described by vm_start and vm_end members of VMA, the base physical
  356. * address is found in vm_pgoff.
  357. * The pci device structure is provided so that architectures may make mapping
  358. * decisions on a per-device or per-bus basis.
  359. *
  360. * Returns a negative error code on failure, zero on success.
  361. */
  362. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  363. enum pci_mmap_state mmap_state, int write_combine)
  364. {
  365. resource_size_t offset = vma->vm_pgoff << PAGE_SHIFT;
  366. struct resource *rp;
  367. int ret;
  368. rp = __pci_mmap_make_offset(dev, &offset, mmap_state);
  369. if (rp == NULL)
  370. return -EINVAL;
  371. vma->vm_pgoff = offset >> PAGE_SHIFT;
  372. vma->vm_page_prot = __pci_mmap_set_pgprot(dev, rp,
  373. vma->vm_page_prot,
  374. mmap_state, write_combine);
  375. ret = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  376. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  377. return ret;
  378. }
  379. void pci_resource_to_user(const struct pci_dev *dev, int bar,
  380. const struct resource *rsrc,
  381. resource_size_t *start, resource_size_t *end)
  382. {
  383. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  384. resource_size_t offset = 0;
  385. if (hose == NULL)
  386. return;
  387. if (rsrc->flags & IORESOURCE_IO)
  388. offset = (unsigned long)hose->io_base_virt - _IO_BASE;
  389. /* We pass a fully fixed up address to userland for MMIO instead of
  390. * a BAR value because X is lame and expects to be able to use that
  391. * to pass to /dev/mem !
  392. *
  393. * That means that we'll have potentially 64 bits values where some
  394. * userland apps only expect 32 (like X itself since it thinks only
  395. * Sparc has 64 bits MMIO) but if we don't do that, we break it on
  396. * 32 bits CHRPs :-(
  397. *
  398. * Hopefully, the sysfs insterface is immune to that gunk. Once X
  399. * has been fixed (and the fix spread enough), we can re-enable the
  400. * 2 lines below and pass down a BAR value to userland. In that case
  401. * we'll also have to re-enable the matching code in
  402. * __pci_mmap_make_offset().
  403. *
  404. * BenH.
  405. */
  406. #if 0
  407. else if (rsrc->flags & IORESOURCE_MEM)
  408. offset = hose->pci_mem_offset;
  409. #endif
  410. *start = rsrc->start - offset;
  411. *end = rsrc->end - offset;
  412. }