u3_iommu.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * arch/powerpc/sysdev/u3_iommu.c
  3. *
  4. * Copyright (C) 2004 Olof Johansson <olof@austin.ibm.com>, IBM Corporation
  5. *
  6. * Based on pSeries_iommu.c:
  7. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  8. * Copyright (C) 2004 Olof Johansson <olof@austin.ibm.com>, IBM Corporation
  9. *
  10. * Dynamic DMA mapping support, Apple U3 & IBM CPC925 "DART" iommu.
  11. *
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include <linux/config.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/slab.h>
  31. #include <linux/mm.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/string.h>
  34. #include <linux/pci.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/vmalloc.h>
  37. #include <asm/io.h>
  38. #include <asm/prom.h>
  39. #include <asm/iommu.h>
  40. #include <asm/pci-bridge.h>
  41. #include <asm/machdep.h>
  42. #include <asm/abs_addr.h>
  43. #include <asm/cacheflush.h>
  44. #include <asm/lmb.h>
  45. #include <asm/ppc-pci.h>
  46. #include "dart.h"
  47. extern int iommu_force_on;
  48. /* Physical base address and size of the DART table */
  49. unsigned long dart_tablebase; /* exported to htab_initialize */
  50. static unsigned long dart_tablesize;
  51. /* Virtual base address of the DART table */
  52. static u32 *dart_vbase;
  53. /* Mapped base address for the dart */
  54. static unsigned int *dart;
  55. /* Dummy val that entries are set to when unused */
  56. static unsigned int dart_emptyval;
  57. static struct iommu_table iommu_table_u3;
  58. static int iommu_table_u3_inited;
  59. static int dart_dirty;
  60. #define DBG(...)
  61. static inline void dart_tlb_invalidate_all(void)
  62. {
  63. unsigned long l = 0;
  64. unsigned int reg;
  65. unsigned long limit;
  66. DBG("dart: flush\n");
  67. /* To invalidate the DART, set the DARTCNTL_FLUSHTLB bit in the
  68. * control register and wait for it to clear.
  69. *
  70. * Gotcha: Sometimes, the DART won't detect that the bit gets
  71. * set. If so, clear it and set it again.
  72. */
  73. limit = 0;
  74. retry:
  75. reg = in_be32((unsigned int *)dart+DARTCNTL);
  76. reg |= DARTCNTL_FLUSHTLB;
  77. out_be32((unsigned int *)dart+DARTCNTL, reg);
  78. l = 0;
  79. while ((in_be32((unsigned int *)dart+DARTCNTL) & DARTCNTL_FLUSHTLB) &&
  80. l < (1L<<limit)) {
  81. l++;
  82. }
  83. if (l == (1L<<limit)) {
  84. if (limit < 4) {
  85. limit++;
  86. reg = in_be32((unsigned int *)dart+DARTCNTL);
  87. reg &= ~DARTCNTL_FLUSHTLB;
  88. out_be32((unsigned int *)dart+DARTCNTL, reg);
  89. goto retry;
  90. } else
  91. panic("U3-DART: TLB did not flush after waiting a long "
  92. "time. Buggy U3 ?");
  93. }
  94. }
  95. static void dart_flush(struct iommu_table *tbl)
  96. {
  97. if (dart_dirty)
  98. dart_tlb_invalidate_all();
  99. dart_dirty = 0;
  100. }
  101. static void dart_build(struct iommu_table *tbl, long index,
  102. long npages, unsigned long uaddr,
  103. enum dma_data_direction direction)
  104. {
  105. unsigned int *dp;
  106. unsigned int rpn;
  107. DBG("dart: build at: %lx, %lx, addr: %x\n", index, npages, uaddr);
  108. index <<= DART_PAGE_FACTOR;
  109. npages <<= DART_PAGE_FACTOR;
  110. dp = ((unsigned int*)tbl->it_base) + index;
  111. /* On U3, all memory is contigous, so we can move this
  112. * out of the loop.
  113. */
  114. while (npages--) {
  115. rpn = virt_to_abs(uaddr) >> DART_PAGE_SHIFT;
  116. *(dp++) = DARTMAP_VALID | (rpn & DARTMAP_RPNMASK);
  117. rpn++;
  118. uaddr += DART_PAGE_SIZE;
  119. }
  120. dart_dirty = 1;
  121. }
  122. static void dart_free(struct iommu_table *tbl, long index, long npages)
  123. {
  124. unsigned int *dp;
  125. /* We don't worry about flushing the TLB cache. The only drawback of
  126. * not doing it is that we won't catch buggy device drivers doing
  127. * bad DMAs, but then no 32-bit architecture ever does either.
  128. */
  129. DBG("dart: free at: %lx, %lx\n", index, npages);
  130. index <<= DART_PAGE_FACTOR;
  131. npages <<= DART_PAGE_FACTOR;
  132. dp = ((unsigned int *)tbl->it_base) + index;
  133. while (npages--)
  134. *(dp++) = dart_emptyval;
  135. }
  136. static int dart_init(struct device_node *dart_node)
  137. {
  138. unsigned int regword;
  139. unsigned int i;
  140. unsigned long tmp;
  141. if (dart_tablebase == 0 || dart_tablesize == 0) {
  142. printk(KERN_INFO "U3-DART: table not allocated, using direct DMA\n");
  143. return -ENODEV;
  144. }
  145. /* Make sure nothing from the DART range remains in the CPU cache
  146. * from a previous mapping that existed before the kernel took
  147. * over
  148. */
  149. flush_dcache_phys_range(dart_tablebase, dart_tablebase + dart_tablesize);
  150. /* Allocate a spare page to map all invalid DART pages. We need to do
  151. * that to work around what looks like a problem with the HT bridge
  152. * prefetching into invalid pages and corrupting data
  153. */
  154. tmp = lmb_alloc(DART_PAGE_SIZE, DART_PAGE_SIZE);
  155. if (!tmp)
  156. panic("U3-DART: Cannot allocate spare page!");
  157. dart_emptyval = DARTMAP_VALID | ((tmp >> DART_PAGE_SHIFT) & DARTMAP_RPNMASK);
  158. /* Map in DART registers. FIXME: Use device node to get base address */
  159. dart = ioremap(DART_BASE, 0x7000);
  160. if (dart == NULL)
  161. panic("U3-DART: Cannot map registers!");
  162. /* Set initial control register contents: table base,
  163. * table size and enable bit
  164. */
  165. regword = DARTCNTL_ENABLE |
  166. ((dart_tablebase >> DART_PAGE_SHIFT) << DARTCNTL_BASE_SHIFT) |
  167. (((dart_tablesize >> DART_PAGE_SHIFT) & DARTCNTL_SIZE_MASK)
  168. << DARTCNTL_SIZE_SHIFT);
  169. dart_vbase = ioremap(virt_to_abs(dart_tablebase), dart_tablesize);
  170. /* Fill initial table */
  171. for (i = 0; i < dart_tablesize/4; i++)
  172. dart_vbase[i] = dart_emptyval;
  173. /* Initialize DART with table base and enable it. */
  174. out_be32((unsigned int *)dart, regword);
  175. /* Invalidate DART to get rid of possible stale TLBs */
  176. dart_tlb_invalidate_all();
  177. printk(KERN_INFO "U3/CPC925 DART IOMMU initialized\n");
  178. return 0;
  179. }
  180. static void iommu_table_u3_setup(void)
  181. {
  182. iommu_table_u3.it_busno = 0;
  183. iommu_table_u3.it_offset = 0;
  184. /* it_size is in number of entries */
  185. iommu_table_u3.it_size = dart_tablesize / sizeof(u32);
  186. /* Initialize the common IOMMU code */
  187. iommu_table_u3.it_base = (unsigned long)dart_vbase;
  188. iommu_table_u3.it_index = 0;
  189. iommu_table_u3.it_blocksize = 1;
  190. iommu_init_table(&iommu_table_u3);
  191. /* Reserve the last page of the DART to avoid possible prefetch
  192. * past the DART mapped area
  193. */
  194. set_bit(iommu_table_u3.it_size - 1, iommu_table_u3.it_map);
  195. }
  196. static void iommu_dev_setup_u3(struct pci_dev *dev)
  197. {
  198. struct device_node *dn;
  199. /* We only have one iommu table on the mac for now, which makes
  200. * things simple. Setup all PCI devices to point to this table
  201. *
  202. * We must use pci_device_to_OF_node() to make sure that
  203. * we get the real "final" pointer to the device in the
  204. * pci_dev sysdata and not the temporary PHB one
  205. */
  206. dn = pci_device_to_OF_node(dev);
  207. if (dn)
  208. PCI_DN(dn)->iommu_table = &iommu_table_u3;
  209. }
  210. static void iommu_bus_setup_u3(struct pci_bus *bus)
  211. {
  212. struct device_node *dn;
  213. if (!iommu_table_u3_inited) {
  214. iommu_table_u3_inited = 1;
  215. iommu_table_u3_setup();
  216. }
  217. dn = pci_bus_to_OF_node(bus);
  218. if (dn)
  219. PCI_DN(dn)->iommu_table = &iommu_table_u3;
  220. }
  221. static void iommu_dev_setup_null(struct pci_dev *dev) { }
  222. static void iommu_bus_setup_null(struct pci_bus *bus) { }
  223. void iommu_init_early_u3(void)
  224. {
  225. struct device_node *dn;
  226. /* Find the DART in the device-tree */
  227. dn = of_find_compatible_node(NULL, "dart", "u3-dart");
  228. if (dn == NULL)
  229. return;
  230. /* Setup low level TCE operations for the core IOMMU code */
  231. ppc_md.tce_build = dart_build;
  232. ppc_md.tce_free = dart_free;
  233. ppc_md.tce_flush = dart_flush;
  234. /* Initialize the DART HW */
  235. if (dart_init(dn)) {
  236. /* If init failed, use direct iommu and null setup functions */
  237. ppc_md.iommu_dev_setup = iommu_dev_setup_null;
  238. ppc_md.iommu_bus_setup = iommu_bus_setup_null;
  239. /* Setup pci_dma ops */
  240. pci_direct_iommu_init();
  241. } else {
  242. ppc_md.iommu_dev_setup = iommu_dev_setup_u3;
  243. ppc_md.iommu_bus_setup = iommu_bus_setup_u3;
  244. /* Setup pci_dma ops */
  245. pci_iommu_init();
  246. }
  247. }
  248. void __init alloc_u3_dart_table(void)
  249. {
  250. /* Only reserve DART space if machine has more than 2GB of RAM
  251. * or if requested with iommu=on on cmdline.
  252. */
  253. if (lmb_end_of_DRAM() <= 0x80000000ull && !iommu_force_on)
  254. return;
  255. /* 512 pages (2MB) is max DART tablesize. */
  256. dart_tablesize = 1UL << 21;
  257. /* 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
  258. * will blow up an entire large page anyway in the kernel mapping
  259. */
  260. dart_tablebase = (unsigned long)
  261. abs_to_virt(lmb_alloc_base(1UL<<24, 1UL<<24, 0x80000000L));
  262. printk(KERN_INFO "U3-DART allocated at: %lx\n", dart_tablebase);
  263. }