blob: 611ac7cd6deb4fccd45ccfe8cbb9b3e36e2af753 [file] [log] [blame]
Mark Kettenis357a2562021-10-23 16:58:05 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2021 Mark Kettenis <kettenis@openbsd.org>
4 */
5
Mark Kettenis357a2562021-10-23 16:58:05 +02006#include <cpu_func.h>
7#include <dm.h>
Mark Ketteniscedd24c2023-01-21 20:27:54 +01008#include <iommu.h>
9#include <lmb.h>
10#include <memalign.h>
Mark Kettenis357a2562021-10-23 16:58:05 +020011#include <asm/io.h>
12
13#define DART_PARAMS2 0x0004
14#define DART_PARAMS2_BYPASS_SUPPORT BIT(0)
Mark Kettenis357a2562021-10-23 16:58:05 +020015
Mark Ketteniscedd24c2023-01-21 20:27:54 +010016#define DART_T8020_TLB_CMD 0x0020
17#define DART_T8020_TLB_CMD_FLUSH BIT(20)
18#define DART_T8020_TLB_CMD_BUSY BIT(2)
19#define DART_T8020_TLB_SIDMASK 0x0034
20#define DART_T8020_ERROR 0x0040
21#define DART_T8020_ERROR_ADDR_LO 0x0050
22#define DART_T8020_ERROR_ADDR_HI 0x0054
23#define DART_T8020_CONFIG 0x0060
24#define DART_T8020_CONFIG_LOCK BIT(15)
25#define DART_T8020_SID_ENABLE 0x00fc
26#define DART_T8020_TCR_BASE 0x0100
27#define DART_T8020_TCR_TRANSLATE_ENABLE BIT(7)
28#define DART_T8020_TCR_BYPASS_DART BIT(8)
29#define DART_T8020_TCR_BYPASS_DAPF BIT(12)
30#define DART_T8020_TTBR_BASE 0x0200
31#define DART_T8020_TTBR_VALID BIT(31)
32
33#define DART_T8110_PARAMS4 0x000c
34#define DART_T8110_PARAMS4_NSID_MASK (0x1ff << 0)
35#define DART_T8110_TLB_CMD 0x0080
36#define DART_T8110_TLB_CMD_BUSY BIT(31)
37#define DART_T8110_TLB_CMD_FLUSH_ALL BIT(8)
38#define DART_T8110_ERROR 0x0100
39#define DART_T8110_ERROR_MASK 0x0104
40#define DART_T8110_ERROR_ADDR_LO 0x0170
41#define DART_T8110_ERROR_ADDR_HI 0x0174
42#define DART_T8110_PROTECT 0x0200
43#define DART_T8110_PROTECT_TTBR_TCR BIT(0)
44#define DART_T8110_SID_ENABLE_BASE 0x0c00
45#define DART_T8110_TCR_BASE 0x1000
Janne Grunau06c61d52022-07-01 00:06:16 +020046#define DART_T8110_TCR_BYPASS_DAPF BIT(2)
47#define DART_T8110_TCR_BYPASS_DART BIT(1)
48#define DART_T8110_TCR_TRANSLATE_ENABLE BIT(0)
Mark Ketteniscedd24c2023-01-21 20:27:54 +010049#define DART_T8110_TTBR_BASE 0x1400
50#define DART_T8110_TTBR_VALID BIT(0)
51
52#define DART_SID_ENABLE(priv, idx) \
53 ((priv)->sid_enable_base + 4 * (idx))
54#define DART_TCR(priv, sid) ((priv)->tcr_base + 4 * (sid))
55#define DART_TTBR(priv, sid, idx) \
56 ((priv)->ttbr_base + 4 * (priv)->nttbr * (sid) + 4 * (idx))
57#define DART_TTBR_SHIFT 12
58
59#define DART_ALL_STREAMS(priv) ((1U << (priv)->nsid) - 1)
60
61#define DART_PAGE_SIZE SZ_16K
62#define DART_PAGE_MASK (DART_PAGE_SIZE - 1)
63
64#define DART_L1_TABLE 0x3
65#define DART_L2_INVAL 0
66#define DART_L2_VALID BIT(0)
67#define DART_L2_FULL_PAGE BIT(1)
68#define DART_L2_START(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 52)
69#define DART_L2_END(addr) ((((addr) & DART_PAGE_MASK) >> 2) << 40)
70
71struct apple_dart_priv {
72 void *base;
Mark Ketteniscedd24c2023-01-21 20:27:54 +010073 u64 *l1, *l2;
74 int bypass, shift;
75
76 dma_addr_t dvabase;
77 dma_addr_t dvaend;
78
79 int nsid;
80 int nttbr;
81 int sid_enable_base;
82 int tcr_base;
83 u32 tcr_translate_enable;
84 u32 tcr_bypass;
85 int ttbr_base;
86 u32 ttbr_valid;
87 void (*flush_tlb)(struct apple_dart_priv *priv);
88};
89
90static void apple_dart_t8020_flush_tlb(struct apple_dart_priv *priv)
91{
92 dsb();
93
94 writel(DART_ALL_STREAMS(priv), priv->base + DART_T8020_TLB_SIDMASK);
95 writel(DART_T8020_TLB_CMD_FLUSH, priv->base + DART_T8020_TLB_CMD);
96 while (readl(priv->base + DART_T8020_TLB_CMD) &
97 DART_T8020_TLB_CMD_BUSY)
98 continue;
99}
100
101static void apple_dart_t8110_flush_tlb(struct apple_dart_priv *priv)
102{
103 dsb();
104
105 writel(DART_T8110_TLB_CMD_FLUSH_ALL,
106 priv->base + DART_T8110_TLB_CMD_FLUSH_ALL);
107 while (readl(priv->base + DART_T8110_TLB_CMD) &
108 DART_T8110_TLB_CMD_BUSY)
109 continue;
110}
111
112static dma_addr_t apple_dart_map(struct udevice *dev, void *addr, size_t size)
113{
114 struct apple_dart_priv *priv = dev_get_priv(dev);
115 phys_addr_t paddr, dva;
116 phys_size_t psize, off;
117 int i, idx;
118
119 if (priv->bypass)
120 return (phys_addr_t)addr;
121
122 paddr = ALIGN_DOWN((phys_addr_t)addr, DART_PAGE_SIZE);
123 off = (phys_addr_t)addr - paddr;
124 psize = ALIGN(size + off, DART_PAGE_SIZE);
125
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530126 dva = lmb_alloc(psize, DART_PAGE_SIZE);
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100127
128 idx = dva / DART_PAGE_SIZE;
129 for (i = 0; i < psize / DART_PAGE_SIZE; i++) {
130 priv->l2[idx + i] = (paddr >> priv->shift) | DART_L2_VALID |
131 DART_L2_START(0LL) | DART_L2_END(~0LL);
132 paddr += DART_PAGE_SIZE;
133 }
134 flush_dcache_range((unsigned long)&priv->l2[idx],
135 (unsigned long)&priv->l2[idx + i]);
136 priv->flush_tlb(priv);
137
138 return dva + off;
139}
140
141static void apple_dart_unmap(struct udevice *dev, dma_addr_t addr, size_t size)
142{
143 struct apple_dart_priv *priv = dev_get_priv(dev);
144 phys_addr_t dva;
145 phys_size_t psize;
146 int i, idx;
147
148 if (priv->bypass)
149 return;
150
151 dva = ALIGN_DOWN(addr, DART_PAGE_SIZE);
152 psize = size + (addr - dva);
153 psize = ALIGN(psize, DART_PAGE_SIZE);
154
155 idx = dva / DART_PAGE_SIZE;
156 for (i = 0; i < psize / DART_PAGE_SIZE; i++)
157 priv->l2[idx + i] = DART_L2_INVAL;
158 flush_dcache_range((unsigned long)&priv->l2[idx],
159 (unsigned long)&priv->l2[idx + i]);
160 priv->flush_tlb(priv);
161
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530162 lmb_free(dva, psize);
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100163}
164
165static struct iommu_ops apple_dart_ops = {
166 .map = apple_dart_map,
167 .unmap = apple_dart_unmap,
168};
Janne Grunau06c61d52022-07-01 00:06:16 +0200169
Mark Kettenis357a2562021-10-23 16:58:05 +0200170static int apple_dart_probe(struct udevice *dev)
171{
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100172 struct apple_dart_priv *priv = dev_get_priv(dev);
173 dma_addr_t addr;
174 phys_addr_t l2;
175 int ntte, nl1, nl2;
Mark Kettenis357a2562021-10-23 16:58:05 +0200176 int sid, i;
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100177 u32 params2, params4;
Mark Kettenis357a2562021-10-23 16:58:05 +0200178
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100179 priv->base = dev_read_addr_ptr(dev);
180 if (!priv->base)
Mark Kettenis357a2562021-10-23 16:58:05 +0200181 return -EINVAL;
182
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100183 if (device_is_compatible(dev, "apple,t8110-dart")) {
184 params4 = readl(priv->base + DART_T8110_PARAMS4);
185 priv->nsid = params4 & DART_T8110_PARAMS4_NSID_MASK;
186 priv->nttbr = 1;
187 priv->sid_enable_base = DART_T8110_SID_ENABLE_BASE;
188 priv->tcr_base = DART_T8110_TCR_BASE;
189 priv->tcr_translate_enable = DART_T8110_TCR_TRANSLATE_ENABLE;
190 priv->tcr_bypass =
191 DART_T8110_TCR_BYPASS_DAPF | DART_T8110_TCR_BYPASS_DART;
192 priv->ttbr_base = DART_T8110_TTBR_BASE;
193 priv->ttbr_valid = DART_T8110_TTBR_VALID;
194 priv->flush_tlb = apple_dart_t8110_flush_tlb;
195 } else {
196 priv->nsid = 16;
197 priv->nttbr = 4;
198 priv->sid_enable_base = DART_T8020_SID_ENABLE;
199 priv->tcr_base = DART_T8020_TCR_BASE;
200 priv->tcr_translate_enable = DART_T8020_TCR_TRANSLATE_ENABLE;
201 priv->tcr_bypass =
202 DART_T8020_TCR_BYPASS_DAPF | DART_T8020_TCR_BYPASS_DART;
203 priv->ttbr_base = DART_T8020_TTBR_BASE;
204 priv->ttbr_valid = DART_T8020_TTBR_VALID;
205 priv->flush_tlb = apple_dart_t8020_flush_tlb;
206 }
207
208 if (device_is_compatible(dev, "apple,t6000-dart") ||
209 device_is_compatible(dev, "apple,t8110-dart"))
210 priv->shift = 4;
Janne Grunau06c61d52022-07-01 00:06:16 +0200211
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100212 priv->dvabase = DART_PAGE_SIZE;
213 priv->dvaend = SZ_4G - DART_PAGE_SIZE;
214
Sughosh Ganu291bf9c2024-08-26 17:29:18 +0530215 lmb_add(priv->dvabase, priv->dvaend - priv->dvabase);
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100216
217 /* Disable translations. */
218 for (sid = 0; sid < priv->nsid; sid++)
219 writel(0, priv->base + DART_TCR(priv, sid));
220
221 /* Remove page tables. */
222 for (sid = 0; sid < priv->nsid; sid++) {
223 for (i = 0; i < priv->nttbr; i++)
224 writel(0, priv->base + DART_TTBR(priv, sid, i));
225 }
226 priv->flush_tlb(priv);
227
228 params2 = readl(priv->base + DART_PARAMS2);
229 if (params2 & DART_PARAMS2_BYPASS_SUPPORT) {
230 for (sid = 0; sid < priv->nsid; sid++) {
231 writel(priv->tcr_bypass,
232 priv->base + DART_TCR(priv, sid));
Janne Grunau06c61d52022-07-01 00:06:16 +0200233 }
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100234 priv->bypass = 1;
235 return 0;
236 }
237
238 ntte = DIV_ROUND_UP(priv->dvaend, DART_PAGE_SIZE);
239 nl2 = DIV_ROUND_UP(ntte, DART_PAGE_SIZE / sizeof(u64));
240 nl1 = DIV_ROUND_UP(nl2, DART_PAGE_SIZE / sizeof(u64));
241
242 priv->l2 = memalign(DART_PAGE_SIZE, nl2 * DART_PAGE_SIZE);
243 memset(priv->l2, 0, nl2 * DART_PAGE_SIZE);
244 flush_dcache_range((unsigned long)priv->l2,
245 (unsigned long)priv->l2 + nl2 * DART_PAGE_SIZE);
246
247 priv->l1 = memalign(DART_PAGE_SIZE, nl1 * DART_PAGE_SIZE);
248 memset(priv->l1, 0, nl1 * DART_PAGE_SIZE);
249 l2 = (phys_addr_t)priv->l2;
250 for (i = 0; i < nl2; i++) {
251 priv->l1[i] = (l2 >> priv->shift) | DART_L1_TABLE;
252 l2 += DART_PAGE_SIZE;
253 }
254 flush_dcache_range((unsigned long)priv->l1,
255 (unsigned long)priv->l1 + nl1 * DART_PAGE_SIZE);
256
257 /* Install page tables. */
258 for (sid = 0; sid < priv->nsid; sid++) {
259 addr = (phys_addr_t)priv->l1;
260 for (i = 0; i < nl1; i++) {
261 writel(addr >> DART_TTBR_SHIFT | priv->ttbr_valid,
262 priv->base + DART_TTBR(priv, sid, i));
263 addr += DART_PAGE_SIZE;
Mark Kettenis357a2562021-10-23 16:58:05 +0200264 }
265 }
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100266 priv->flush_tlb(priv);
267
268 /* Enable all streams. */
269 for (i = 0; i < priv->nsid / 32; i++)
270 writel(~0, priv->base + DART_SID_ENABLE(priv, i));
271
272 /* Enable translations. */
273 for (sid = 0; sid < priv->nsid; sid++) {
274 writel(priv->tcr_translate_enable,
275 priv->base + DART_TCR(priv, sid));
276 }
277
278 return 0;
279}
280
281static int apple_dart_remove(struct udevice *dev)
282{
283 struct apple_dart_priv *priv = dev_get_priv(dev);
284 int sid, i;
285
286 /* Disable translations. */
287 for (sid = 0; sid < priv->nsid; sid++)
288 writel(0, priv->base + DART_TCR(priv, sid));
289
290 /* Remove page tables. */
291 for (sid = 0; sid < priv->nsid; sid++) {
292 for (i = 0; i < priv->nttbr; i++)
293 writel(0, priv->base + DART_TTBR(priv, sid, i));
294 }
295 priv->flush_tlb(priv);
Mark Kettenis357a2562021-10-23 16:58:05 +0200296
297 return 0;
298}
299
300static const struct udevice_id apple_dart_ids[] = {
301 { .compatible = "apple,t8103-dart" },
Janne Grunau622a0582022-02-08 22:27:49 +0100302 { .compatible = "apple,t6000-dart" },
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100303 { .compatible = "apple,t8110-dart" },
Mark Kettenis357a2562021-10-23 16:58:05 +0200304 { /* sentinel */ }
305};
306
307U_BOOT_DRIVER(apple_dart) = {
308 .name = "apple_dart",
309 .id = UCLASS_IOMMU,
310 .of_match = apple_dart_ids,
Mark Ketteniscedd24c2023-01-21 20:27:54 +0100311 .priv_auto = sizeof(struct apple_dart_priv),
312 .ops = &apple_dart_ops,
313 .probe = apple_dart_probe,
314 .remove = apple_dart_remove,
315 .flags = DM_FLAG_OS_PREPARE
Mark Kettenis357a2562021-10-23 16:58:05 +0200316};