dart_iommu.c 11 KB

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