apple_dart.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
  4. */
  5. #include <common.h>
  6. #include <cpu_func.h>
  7. #include <dm.h>
  8. #include <iommu.h>
  9. #include <lmb.h>
  10. #include <memalign.h>
  11. #include <asm/io.h>
  12. #define DART_PARAMS2 0x0004
  13. #define DART_PARAMS2_BYPASS_SUPPORT BIT(0)
  14. #define DART_T8020_TLB_CMD 0x0020
  15. #define DART_T8020_TLB_CMD_FLUSH BIT(20)
  16. #define DART_T8020_TLB_CMD_BUSY BIT(2)
  17. #define DART_T8020_TLB_SIDMASK 0x0034
  18. #define DART_T8020_ERROR 0x0040
  19. #define DART_T8020_ERROR_ADDR_LO 0x0050
  20. #define DART_T8020_ERROR_ADDR_HI 0x0054
  21. #define DART_T8020_CONFIG 0x0060
  22. #define DART_T8020_CONFIG_LOCK BIT(15)
  23. #define DART_T8020_SID_ENABLE 0x00fc
  24. #define DART_T8020_TCR_BASE 0x0100
  25. #define DART_T8020_TCR_TRANSLATE_ENABLE BIT(7)
  26. #define DART_T8020_TCR_BYPASS_DART BIT(8)
  27. #define DART_T8020_TCR_BYPASS_DAPF BIT(12)
  28. #define DART_T8020_TTBR_BASE 0x0200
  29. #define DART_T8020_TTBR_VALID BIT(31)
  30. #define DART_T8110_PARAMS4 0x000c
  31. #define DART_T8110_PARAMS4_NSID_MASK (0x1ff << 0)
  32. #define DART_T8110_TLB_CMD 0x0080
  33. #define DART_T8110_TLB_CMD_BUSY BIT(31)
  34. #define DART_T8110_TLB_CMD_FLUSH_ALL BIT(8)
  35. #define DART_T8110_ERROR 0x0100
  36. #define DART_T8110_ERROR_MASK 0x0104
  37. #define DART_T8110_ERROR_ADDR_LO 0x0170
  38. #define DART_T8110_ERROR_ADDR_HI 0x0174
  39. #define DART_T8110_PROTECT 0x0200
  40. #define DART_T8110_PROTECT_TTBR_TCR BIT(0)
  41. #define DART_T8110_SID_ENABLE_BASE 0x0c00
  42. #define DART_T8110_TCR_BASE 0x1000
  43. #define DART_T8110_TCR_BYPASS_DAPF BIT(2)
  44. #define DART_T8110_TCR_BYPASS_DART BIT(1)
  45. #define DART_T8110_TCR_TRANSLATE_ENABLE BIT(0)
  46. #define DART_T8110_TTBR_BASE 0x1400
  47. #define DART_T8110_TTBR_VALID BIT(0)
  48. #define DART_SID_ENABLE(priv, idx) \
  49. ((priv)->sid_enable_base + 4 * (idx))
  50. #define DART_TCR(priv, sid) ((priv)->tcr_base + 4 * (sid))
  51. #define DART_TTBR(priv, sid, idx) \
  52. ((priv)->ttbr_base + 4 * (priv)->nttbr * (sid) + 4 * (idx))
  53. #define DART_TTBR_SHIFT 12
  54. #define DART_ALL_STREAMS(priv) ((1U << (priv)->nsid) - 1)
  55. #define DART_PAGE_SIZE SZ_16K
  56. #define DART_PAGE_MASK (DART_PAGE_SIZE - 1)
  57. #define DART_L1_TABLE 0x3
  58. #define DART_L2_INVAL 0
  59. #define DART_L2_VALID BIT(0)
  60. #define DART_L2_FULL_PAGE BIT(1)
  61. #define DART_L2_START(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 52)
  62. #define DART_L2_END(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 40)
  63. struct apple_dart_priv {
  64. void *base;
  65. struct lmb lmb;
  66. u64 *l1, *l2;
  67. int bypass, shift;
  68. dma_addr_t dvabase;
  69. dma_addr_t dvaend;
  70. int nsid;
  71. int nttbr;
  72. int sid_enable_base;
  73. int tcr_base;
  74. u32 tcr_translate_enable;
  75. u32 tcr_bypass;
  76. int ttbr_base;
  77. u32 ttbr_valid;
  78. void (*flush_tlb)(struct apple_dart_priv *priv);
  79. };
  80. static void apple_dart_t8020_flush_tlb(struct apple_dart_priv *priv)
  81. {
  82. dsb();
  83. writel(DART_ALL_STREAMS(priv), priv->base + DART_T8020_TLB_SIDMASK);
  84. writel(DART_T8020_TLB_CMD_FLUSH, priv->base + DART_T8020_TLB_CMD);
  85. while (readl(priv->base + DART_T8020_TLB_CMD) &
  86. DART_T8020_TLB_CMD_BUSY)
  87. continue;
  88. }
  89. static void apple_dart_t8110_flush_tlb(struct apple_dart_priv *priv)
  90. {
  91. dsb();
  92. writel(DART_T8110_TLB_CMD_FLUSH_ALL,
  93. priv->base + DART_T8110_TLB_CMD_FLUSH_ALL);
  94. while (readl(priv->base + DART_T8110_TLB_CMD) &
  95. DART_T8110_TLB_CMD_BUSY)
  96. continue;
  97. }
  98. static dma_addr_t apple_dart_map(struct udevice *dev, void *addr, size_t size)
  99. {
  100. struct apple_dart_priv *priv = dev_get_priv(dev);
  101. phys_addr_t paddr, dva;
  102. phys_size_t psize, off;
  103. int i, idx;
  104. if (priv->bypass)
  105. return (phys_addr_t)addr;
  106. paddr = ALIGN_DOWN((phys_addr_t)addr, DART_PAGE_SIZE);
  107. off = (phys_addr_t)addr - paddr;
  108. psize = ALIGN(size + off, DART_PAGE_SIZE);
  109. dva = lmb_alloc(&priv->lmb, psize, DART_PAGE_SIZE);
  110. idx = dva / DART_PAGE_SIZE;
  111. for (i = 0; i < psize / DART_PAGE_SIZE; i++) {
  112. priv->l2[idx + i] = (paddr >> priv->shift) | DART_L2_VALID |
  113. DART_L2_START(0LL) | DART_L2_END(~0LL);
  114. paddr += DART_PAGE_SIZE;
  115. }
  116. flush_dcache_range((unsigned long)&priv->l2[idx],
  117. (unsigned long)&priv->l2[idx + i]);
  118. priv->flush_tlb(priv);
  119. return dva + off;
  120. }
  121. static void apple_dart_unmap(struct udevice *dev, dma_addr_t addr, size_t size)
  122. {
  123. struct apple_dart_priv *priv = dev_get_priv(dev);
  124. phys_addr_t dva;
  125. phys_size_t psize;
  126. int i, idx;
  127. if (priv->bypass)
  128. return;
  129. dva = ALIGN_DOWN(addr, DART_PAGE_SIZE);
  130. psize = size + (addr - dva);
  131. psize = ALIGN(psize, DART_PAGE_SIZE);
  132. idx = dva / DART_PAGE_SIZE;
  133. for (i = 0; i < psize / DART_PAGE_SIZE; i++)
  134. priv->l2[idx + i] = DART_L2_INVAL;
  135. flush_dcache_range((unsigned long)&priv->l2[idx],
  136. (unsigned long)&priv->l2[idx + i]);
  137. priv->flush_tlb(priv);
  138. lmb_free(&priv->lmb, dva, psize);
  139. }
  140. static struct iommu_ops apple_dart_ops = {
  141. .map = apple_dart_map,
  142. .unmap = apple_dart_unmap,
  143. };
  144. static int apple_dart_probe(struct udevice *dev)
  145. {
  146. struct apple_dart_priv *priv = dev_get_priv(dev);
  147. dma_addr_t addr;
  148. phys_addr_t l2;
  149. int ntte, nl1, nl2;
  150. int sid, i;
  151. u32 params2, params4;
  152. priv->base = dev_read_addr_ptr(dev);
  153. if (!priv->base)
  154. return -EINVAL;
  155. if (device_is_compatible(dev, "apple,t8110-dart")) {
  156. params4 = readl(priv->base + DART_T8110_PARAMS4);
  157. priv->nsid = params4 & DART_T8110_PARAMS4_NSID_MASK;
  158. priv->nttbr = 1;
  159. priv->sid_enable_base = DART_T8110_SID_ENABLE_BASE;
  160. priv->tcr_base = DART_T8110_TCR_BASE;
  161. priv->tcr_translate_enable = DART_T8110_TCR_TRANSLATE_ENABLE;
  162. priv->tcr_bypass =
  163. DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART;
  164. priv->ttbr_base = DART_T8110_TTBR_BASE;
  165. priv->ttbr_valid = DART_T8110_TTBR_VALID;
  166. priv->flush_tlb = apple_dart_t8110_flush_tlb;
  167. } else {
  168. priv->nsid = 16;
  169. priv->nttbr = 4;
  170. priv->sid_enable_base = DART_T8020_SID_ENABLE;
  171. priv->tcr_base = DART_T8020_TCR_BASE;
  172. priv->tcr_translate_enable = DART_T8020_TCR_TRANSLATE_ENABLE;
  173. priv->tcr_bypass =
  174. DART_T8020_TCR_BYPASS_DAPF | DART_T8020_TCR_BYPASS_DART;
  175. priv->ttbr_base = DART_T8020_TTBR_BASE;
  176. priv->ttbr_valid = DART_T8020_TTBR_VALID;
  177. priv->flush_tlb = apple_dart_t8020_flush_tlb;
  178. }
  179. if (device_is_compatible(dev, "apple,t6000-dart") ||
  180. device_is_compatible(dev, "apple,t8110-dart"))
  181. priv->shift = 4;
  182. priv->dvabase = DART_PAGE_SIZE;
  183. priv->dvaend = SZ_4G - DART_PAGE_SIZE;
  184. lmb_init(&priv->lmb);
  185. lmb_add(&priv->lmb, priv->dvabase, priv->dvaend - priv->dvabase);
  186. /* Disable translations. */
  187. for (sid = 0; sid < priv->nsid; sid++)
  188. writel(0, priv->base + DART_TCR(priv, sid));
  189. /* Remove page tables. */
  190. for (sid = 0; sid < priv->nsid; sid++) {
  191. for (i = 0; i < priv->nttbr; i++)
  192. writel(0, priv->base + DART_TTBR(priv, sid, i));
  193. }
  194. priv->flush_tlb(priv);
  195. params2 = readl(priv->base + DART_PARAMS2);
  196. if (params2 & DART_PARAMS2_BYPASS_SUPPORT) {
  197. for (sid = 0; sid < priv->nsid; sid++) {
  198. writel(priv->tcr_bypass,
  199. priv->base + DART_TCR(priv, sid));
  200. }
  201. priv->bypass = 1;
  202. return 0;
  203. }
  204. ntte = DIV_ROUND_UP(priv->dvaend, DART_PAGE_SIZE);
  205. nl2 = DIV_ROUND_UP(ntte, DART_PAGE_SIZE / sizeof(u64));
  206. nl1 = DIV_ROUND_UP(nl2, DART_PAGE_SIZE / sizeof(u64));
  207. priv->l2 = memalign(DART_PAGE_SIZE, nl2 * DART_PAGE_SIZE);
  208. memset(priv->l2, 0, nl2 * DART_PAGE_SIZE);
  209. flush_dcache_range((unsigned long)priv->l2,
  210. (unsigned long)priv->l2 + nl2 * DART_PAGE_SIZE);
  211. priv->l1 = memalign(DART_PAGE_SIZE, nl1 * DART_PAGE_SIZE);
  212. memset(priv->l1, 0, nl1 * DART_PAGE_SIZE);
  213. l2 = (phys_addr_t)priv->l2;
  214. for (i = 0; i < nl2; i++) {
  215. priv->l1[i] = (l2 >> priv->shift) | DART_L1_TABLE;
  216. l2 += DART_PAGE_SIZE;
  217. }
  218. flush_dcache_range((unsigned long)priv->l1,
  219. (unsigned long)priv->l1 + nl1 * DART_PAGE_SIZE);
  220. /* Install page tables. */
  221. for (sid = 0; sid < priv->nsid; sid++) {
  222. addr = (phys_addr_t)priv->l1;
  223. for (i = 0; i < nl1; i++) {
  224. writel(addr >> DART_TTBR_SHIFT | priv->ttbr_valid,
  225. priv->base + DART_TTBR(priv, sid, i));
  226. addr += DART_PAGE_SIZE;
  227. }
  228. }
  229. priv->flush_tlb(priv);
  230. /* Enable all streams. */
  231. for (i = 0; i < priv->nsid / 32; i++)
  232. writel(~0, priv->base + DART_SID_ENABLE(priv, i));
  233. /* Enable translations. */
  234. for (sid = 0; sid < priv->nsid; sid++) {
  235. writel(priv->tcr_translate_enable,
  236. priv->base + DART_TCR(priv, sid));
  237. }
  238. return 0;
  239. }
  240. static int apple_dart_remove(struct udevice *dev)
  241. {
  242. struct apple_dart_priv *priv = dev_get_priv(dev);
  243. int sid, i;
  244. /* Disable translations. */
  245. for (sid = 0; sid < priv->nsid; sid++)
  246. writel(0, priv->base + DART_TCR(priv, sid));
  247. /* Remove page tables. */
  248. for (sid = 0; sid < priv->nsid; sid++) {
  249. for (i = 0; i < priv->nttbr; i++)
  250. writel(0, priv->base + DART_TTBR(priv, sid, i));
  251. }
  252. priv->flush_tlb(priv);
  253. return 0;
  254. }
  255. static const struct udevice_id apple_dart_ids[] = {
  256. { .compatible = "apple,t8103-dart" },
  257. { .compatible = "apple,t6000-dart" },
  258. { .compatible = "apple,t8110-dart" },
  259. { /* sentinel */ }
  260. };
  261. U_BOOT_DRIVER(apple_dart) = {
  262. .name = "apple_dart",
  263. .id = UCLASS_IOMMU,
  264. .of_match = apple_dart_ids,
  265. .priv_auto = sizeof(struct apple_dart_priv),
  266. .ops = &apple_dart_ops,
  267. .probe = apple_dart_probe,
  268. .remove = apple_dart_remove,
  269. .flags = DM_FLAG_OS_PREPARE
  270. };