dart_iommu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * arch/powerpc/sysdev/dart_iommu.c
  4. *
  5. * Copyright (C) 2004 Olof Johansson <[email protected]>, IBM Corporation
  6. * Copyright (C) 2005 Benjamin Herrenschmidt <[email protected]>,
  7. * IBM Corporation
  8. *
  9. * Based on pSeries_iommu.c:
  10. * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
  11. * Copyright (C) 2004 Olof Johansson <[email protected]>, IBM Corporation
  12. *
  13. * Dynamic DMA mapping support, Apple U3, U4 & IBM CPC925 "DART" iommu.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/types.h>
  17. #include <linux/mm.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/string.h>
  20. #include <linux/pci.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/suspend.h>
  24. #include <linux/memblock.h>
  25. #include <linux/gfp.h>
  26. #include <linux/kmemleak.h>
  27. #include <linux/of_address.h>
  28. #include <asm/io.h>
  29. #include <asm/iommu.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/machdep.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/ppc-pci.h>
  34. #include "dart.h"
  35. /* DART table address and size */
  36. static u32 *dart_tablebase;
  37. static unsigned long dart_tablesize;
  38. /* Mapped base address for the dart */
  39. static unsigned int __iomem *dart;
  40. /* Dummy val that entries are set to when unused */
  41. static unsigned int dart_emptyval;
  42. static struct iommu_table iommu_table_dart;
  43. static int iommu_table_dart_inited;
  44. static int dart_dirty;
  45. static int dart_is_u4;
  46. #define DART_U4_BYPASS_BASE 0x8000000000ull
  47. #define DBG(...)
  48. static DEFINE_SPINLOCK(invalidate_lock);
  49. static inline void dart_tlb_invalidate_all(void)
  50. {
  51. unsigned long l = 0;
  52. unsigned int reg, inv_bit;
  53. unsigned long limit;
  54. unsigned long flags;
  55. spin_lock_irqsave(&invalidate_lock, flags);
  56. DBG("dart: flush\n");
  57. /* To invalidate the DART, set the DARTCNTL_FLUSHTLB bit in the
  58. * control register and wait for it to clear.
  59. *
  60. * Gotcha: Sometimes, the DART won't detect that the bit gets
  61. * set. If so, clear it and set it again.
  62. */
  63. limit = 0;
  64. inv_bit = dart_is_u4 ? DART_CNTL_U4_FLUSHTLB : DART_CNTL_U3_FLUSHTLB;
  65. retry:
  66. l = 0;
  67. reg = DART_IN(DART_CNTL);
  68. reg |= inv_bit;
  69. DART_OUT(DART_CNTL, reg);
  70. while ((DART_IN(DART_CNTL) & inv_bit) && l < (1L << limit))
  71. l++;
  72. if (l == (1L << limit)) {
  73. if (limit < 4) {
  74. limit++;
  75. reg = DART_IN(DART_CNTL);
  76. reg &= ~inv_bit;
  77. DART_OUT(DART_CNTL, reg);
  78. goto retry;
  79. } else
  80. panic("DART: TLB did not flush after waiting a long "
  81. "time. Buggy U3 ?");
  82. }
  83. spin_unlock_irqrestore(&invalidate_lock, flags);
  84. }
  85. static inline void dart_tlb_invalidate_one(unsigned long bus_rpn)
  86. {
  87. unsigned int reg;
  88. unsigned int l, limit;
  89. unsigned long flags;
  90. spin_lock_irqsave(&invalidate_lock, flags);
  91. reg = DART_CNTL_U4_ENABLE | DART_CNTL_U4_IONE |
  92. (bus_rpn & DART_CNTL_U4_IONE_MASK);
  93. DART_OUT(DART_CNTL, reg);
  94. limit = 0;
  95. wait_more:
  96. l = 0;
  97. while ((DART_IN(DART_CNTL) & DART_CNTL_U4_IONE) && l < (1L << limit)) {
  98. rmb();
  99. l++;
  100. }
  101. if (l == (1L << limit)) {
  102. if (limit < 4) {
  103. limit++;
  104. goto wait_more;
  105. } else
  106. panic("DART: TLB did not flush after waiting a long "
  107. "time. Buggy U4 ?");
  108. }
  109. spin_unlock_irqrestore(&invalidate_lock, flags);
  110. }
  111. static void dart_cache_sync(unsigned int *base, unsigned int count)
  112. {
  113. /*
  114. * We add 1 to the number of entries to flush, following a
  115. * comment in Darwin indicating that the memory controller
  116. * can prefetch unmapped memory under some circumstances.
  117. */
  118. unsigned long start = (unsigned long)base;
  119. unsigned long end = start + (count + 1) * sizeof(unsigned int);
  120. unsigned int tmp;
  121. /* Perform a standard cache flush */
  122. flush_dcache_range(start, end);
  123. /*
  124. * Perform the sequence described in the CPC925 manual to
  125. * ensure all the data gets to a point the cache incoherent
  126. * DART hardware will see.
  127. */
  128. asm volatile(" sync;"
  129. " isync;"
  130. " dcbf 0,%1;"
  131. " sync;"
  132. " isync;"
  133. " lwz %0,0(%1);"
  134. " isync" : "=r" (tmp) : "r" (end) : "memory");
  135. }
  136. static void dart_flush(struct iommu_table *tbl)
  137. {
  138. mb();
  139. if (dart_dirty) {
  140. dart_tlb_invalidate_all();
  141. dart_dirty = 0;
  142. }
  143. }
  144. static int dart_build(struct iommu_table *tbl, long index,
  145. long npages, unsigned long uaddr,
  146. enum dma_data_direction direction,
  147. unsigned long attrs)
  148. {
  149. unsigned int *dp, *orig_dp;
  150. unsigned int rpn;
  151. long l;
  152. DBG("dart: build at: %lx, %lx, addr: %x\n", index, npages, uaddr);
  153. orig_dp = dp = ((unsigned int*)tbl->it_base) + index;
  154. /* On U3, all memory is contiguous, so we can move this
  155. * out of the loop.
  156. */
  157. l = npages;
  158. while (l--) {
  159. rpn = __pa(uaddr) >> DART_PAGE_SHIFT;
  160. *(dp++) = DARTMAP_VALID | (rpn & DARTMAP_RPNMASK);
  161. uaddr += DART_PAGE_SIZE;
  162. }
  163. dart_cache_sync(orig_dp, npages);
  164. if (dart_is_u4) {
  165. rpn = index;
  166. while (npages--)
  167. dart_tlb_invalidate_one(rpn++);
  168. } else {
  169. dart_dirty = 1;
  170. }
  171. return 0;
  172. }
  173. static void dart_free(struct iommu_table *tbl, long index, long npages)
  174. {
  175. unsigned int *dp, *orig_dp;
  176. long orig_npages = npages;
  177. /* We don't worry about flushing the TLB cache. The only drawback of
  178. * not doing it is that we won't catch buggy device drivers doing
  179. * bad DMAs, but then no 32-bit architecture ever does either.
  180. */
  181. DBG("dart: free at: %lx, %lx\n", index, npages);
  182. orig_dp = dp = ((unsigned int *)tbl->it_base) + index;
  183. while (npages--)
  184. *(dp++) = dart_emptyval;
  185. dart_cache_sync(orig_dp, orig_npages);
  186. }
  187. static void __init allocate_dart(void)
  188. {
  189. unsigned long tmp;
  190. /* 512 pages (2MB) is max DART tablesize. */
  191. dart_tablesize = 1UL << 21;
  192. /*
  193. * 16MB (1 << 24) alignment. We allocate a full 16Mb chuck since we
  194. * will blow up an entire large page anyway in the kernel mapping.
  195. */
  196. dart_tablebase = memblock_alloc_try_nid_raw(SZ_16M, SZ_16M,
  197. MEMBLOCK_LOW_LIMIT, SZ_2G,
  198. NUMA_NO_NODE);
  199. if (!dart_tablebase)
  200. panic("Failed to allocate 16MB below 2GB for DART table\n");
  201. /* There is no point scanning the DART space for leaks*/
  202. kmemleak_no_scan((void *)dart_tablebase);
  203. /* Allocate a spare page to map all invalid DART pages. We need to do
  204. * that to work around what looks like a problem with the HT bridge
  205. * prefetching into invalid pages and corrupting data
  206. */
  207. tmp = memblock_phys_alloc(DART_PAGE_SIZE, DART_PAGE_SIZE);
  208. if (!tmp)
  209. panic("DART: table allocation failed\n");
  210. dart_emptyval = DARTMAP_VALID | ((tmp >> DART_PAGE_SHIFT) &
  211. DARTMAP_RPNMASK);
  212. printk(KERN_INFO "DART table allocated at: %p\n", dart_tablebase);
  213. }
  214. static int __init dart_init(struct device_node *dart_node)
  215. {
  216. unsigned int i;
  217. unsigned long base, size;
  218. struct resource r;
  219. /* IOMMU disabled by the user ? bail out */
  220. if (iommu_is_off)
  221. return -ENODEV;
  222. /*
  223. * Only use the DART if the machine has more than 1GB of RAM
  224. * or if requested with iommu=on on cmdline.
  225. *
  226. * 1GB of RAM is picked as limit because some default devices
  227. * (i.e. Airport Extreme) have 30 bit address range limits.
  228. */
  229. if (!iommu_force_on && memblock_end_of_DRAM() <= 0x40000000ull)
  230. return -ENODEV;
  231. /* Get DART registers */
  232. if (of_address_to_resource(dart_node, 0, &r))
  233. panic("DART: can't get register base ! ");
  234. /* Map in DART registers */
  235. dart = ioremap(r.start, resource_size(&r));
  236. if (dart == NULL)
  237. panic("DART: Cannot map registers!");
  238. /* Allocate the DART and dummy page */
  239. allocate_dart();
  240. /* Fill initial table */
  241. for (i = 0; i < dart_tablesize/4; i++)
  242. dart_tablebase[i] = dart_emptyval;
  243. /* Push to memory */
  244. dart_cache_sync(dart_tablebase, dart_tablesize / sizeof(u32));
  245. /* Initialize DART with table base and enable it. */
  246. base = ((unsigned long)dart_tablebase) >> DART_PAGE_SHIFT;
  247. size = dart_tablesize >> DART_PAGE_SHIFT;
  248. if (dart_is_u4) {
  249. size &= DART_SIZE_U4_SIZE_MASK;
  250. DART_OUT(DART_BASE_U4, base);
  251. DART_OUT(DART_SIZE_U4, size);
  252. DART_OUT(DART_CNTL, DART_CNTL_U4_ENABLE);
  253. } else {
  254. size &= DART_CNTL_U3_SIZE_MASK;
  255. DART_OUT(DART_CNTL,
  256. DART_CNTL_U3_ENABLE |
  257. (base << DART_CNTL_U3_BASE_SHIFT) |
  258. (size << DART_CNTL_U3_SIZE_SHIFT));
  259. }
  260. /* Invalidate DART to get rid of possible stale TLBs */
  261. dart_tlb_invalidate_all();
  262. printk(KERN_INFO "DART IOMMU initialized for %s type chipset\n",
  263. dart_is_u4 ? "U4" : "U3");
  264. return 0;
  265. }
  266. static struct iommu_table_ops iommu_dart_ops = {
  267. .set = dart_build,
  268. .clear = dart_free,
  269. .flush = dart_flush,
  270. };
  271. static void iommu_table_dart_setup(void)
  272. {
  273. iommu_table_dart.it_busno = 0;
  274. iommu_table_dart.it_offset = 0;
  275. /* it_size is in number of entries */
  276. iommu_table_dart.it_size = dart_tablesize / sizeof(u32);
  277. iommu_table_dart.it_page_shift = IOMMU_PAGE_SHIFT_4K;
  278. /* Initialize the common IOMMU code */
  279. iommu_table_dart.it_base = (unsigned long)dart_tablebase;
  280. iommu_table_dart.it_index = 0;
  281. iommu_table_dart.it_blocksize = 1;
  282. iommu_table_dart.it_ops = &iommu_dart_ops;
  283. if (!iommu_init_table(&iommu_table_dart, -1, 0, 0))
  284. panic("Failed to initialize iommu table");
  285. /* Reserve the last page of the DART to avoid possible prefetch
  286. * past the DART mapped area
  287. */
  288. set_bit(iommu_table_dart.it_size - 1, iommu_table_dart.it_map);
  289. }
  290. static void pci_dma_bus_setup_dart(struct pci_bus *bus)
  291. {
  292. if (!iommu_table_dart_inited) {
  293. iommu_table_dart_inited = 1;
  294. iommu_table_dart_setup();
  295. }
  296. }
  297. static bool dart_device_on_pcie(struct device *dev)
  298. {
  299. struct device_node *np = of_node_get(dev->of_node);
  300. while(np) {
  301. if (of_device_is_compatible(np, "U4-pcie") ||
  302. of_device_is_compatible(np, "u4-pcie")) {
  303. of_node_put(np);
  304. return true;
  305. }
  306. np = of_get_next_parent(np);
  307. }
  308. return false;
  309. }
  310. static void pci_dma_dev_setup_dart(struct pci_dev *dev)
  311. {
  312. if (dart_is_u4 && dart_device_on_pcie(&dev->dev))
  313. dev->dev.archdata.dma_offset = DART_U4_BYPASS_BASE;
  314. set_iommu_table_base(&dev->dev, &iommu_table_dart);
  315. }
  316. static bool iommu_bypass_supported_dart(struct pci_dev *dev, u64 mask)
  317. {
  318. return dart_is_u4 &&
  319. dart_device_on_pcie(&dev->dev) &&
  320. mask >= DMA_BIT_MASK(40);
  321. }
  322. void __init iommu_init_early_dart(struct pci_controller_ops *controller_ops)
  323. {
  324. struct device_node *dn;
  325. /* Find the DART in the device-tree */
  326. dn = of_find_compatible_node(NULL, "dart", "u3-dart");
  327. if (dn == NULL) {
  328. dn = of_find_compatible_node(NULL, "dart", "u4-dart");
  329. if (dn == NULL)
  330. return; /* use default direct_dma_ops */
  331. dart_is_u4 = 1;
  332. }
  333. /* Initialize the DART HW */
  334. if (dart_init(dn) != 0) {
  335. of_node_put(dn);
  336. return;
  337. }
  338. /*
  339. * U4 supports a DART bypass, we use it for 64-bit capable devices to
  340. * improve performance. However, that only works for devices connected
  341. * to the U4 own PCIe interface, not bridged through hypertransport.
  342. * We need the device to support at least 40 bits of addresses.
  343. */
  344. controller_ops->dma_dev_setup = pci_dma_dev_setup_dart;
  345. controller_ops->dma_bus_setup = pci_dma_bus_setup_dart;
  346. controller_ops->iommu_bypass_supported = iommu_bypass_supported_dart;
  347. /* Setup pci_dma ops */
  348. set_pci_dma_ops(&dma_iommu_ops);
  349. of_node_put(dn);
  350. }
  351. #ifdef CONFIG_PM
  352. static void iommu_dart_restore(void)
  353. {
  354. dart_cache_sync(dart_tablebase, dart_tablesize / sizeof(u32));
  355. dart_tlb_invalidate_all();
  356. }
  357. static int __init iommu_init_late_dart(void)
  358. {
  359. if (!dart_tablebase)
  360. return 0;
  361. ppc_md.iommu_restore = iommu_dart_restore;
  362. return 0;
  363. }
  364. late_initcall(iommu_init_late_dart);
  365. #endif /* CONFIG_PM */