blob: c1b95bc8b8ca7463399c9cb801e7a9a6d004af53 [file] [log] [blame]
Caleb Connollya95de052023-12-11 18:41:42 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2023 Linaro Ltd.
4 * Basic ARM SMMU-500 driver, assuming a pre-initialised SMMU and only IDENTITY domains
5 * this driver only implements the bare minimum to configure stream mappings for periphals
6 * used by u-boot on platforms where the SMMU can't be disabled.
7 */
8
9#include <log.h>
10#include <cpu_func.h>
11#include <dm.h>
12#include <iommu.h>
13#include <linux/bitfield.h>
14#include <linux/list.h>
15#include <linux/err.h>
16#include <lmb.h>
17#include <memalign.h>
18#include <asm/io.h>
19
20#define ARM_SMMU_GR0 0
21#define ARM_SMMU_GR1 1
22
23#define ARM_SMMU_GR0_ID0 0x20
24#define ARM_SMMU_ID0_NUMSMRG GENMASK(7, 0) /* Number of stream mapping groups */
25#define ARM_SMMU_GR0_ID1 0x24
26#define ARM_SMMU_ID1_PAGESIZE \
27 BIT(31) /* Page shift is 16 bits when set, otherwise 23 */
28#define ARM_SMMU_ID1_NUMPAGENDXB \
29 GENMASK(30, 28) /* Number of pages before context banks */
30#define ARM_SMMU_ID1_NUMCB GENMASK(7, 0) /* Number of context banks supported */
31
32#define ARM_SMMU_GR1_CBAR(n) (0x0 + ((n) << 2))
33#define ARM_SMMU_CBAR_TYPE GENMASK(17, 16)
34#define ARM_SMMU_CBAR_VMID GENMASK(7, 0)
35enum arm_smmu_cbar_type {
36 CBAR_TYPE_S2_TRANS,
37 CBAR_TYPE_S1_TRANS_S2_BYPASS,
38 CBAR_TYPE_S1_TRANS_S2_FAULT,
39 CBAR_TYPE_S1_TRANS_S2_TRANS,
40};
41
42#define ARM_SMMU_GR1_CBA2R(n) (0x800 + ((n) << 2))
43#define ARM_SMMU_CBA2R_VA64 BIT(0)
44
45/* Per-CB system control register */
46#define ARM_SMMU_CB_SCTLR 0x0
47#define ARM_SMMU_SCTLR_CFCFG BIT(7) /* Stall on context fault */
48#define ARM_SMMU_SCTLR_CFIE BIT(6) /* Context fault interrupt enable */
49#define ARM_SMMU_SCTLR_CFRE BIT(5) /* Abort on context fault */
50
51/* Translation Table Base, holds address of translation table in memory to be used
52 * for this context bank. Or 0 for bypass
53 */
54#define ARM_SMMU_CB_TTBR0 0x20
55#define ARM_SMMU_CB_TTBR1 0x28
56/* Translation Control Register, configured TTBR/TLB behaviour (0 for bypass) */
57#define ARM_SMMU_CB_TCR 0x30
58/* Memory Attribute Indirection, also 0 for bypass */
59#define ARM_SMMU_CB_S1_MAIR0 0x38
60#define ARM_SMMU_CB_S1_MAIR1 0x3c
61
62#define ARM_SMMU_GR0_SMR(n) (0x800 + ((n) << 2))
63#define ARM_SMMU_SMR_VALID BIT(31)
64#define ARM_SMMU_SMR_MASK GENMASK(31, 16) // Always 0 for now??
65#define ARM_SMMU_SMR_ID GENMASK(15, 0)
66
67#define ARM_SMMU_GR0_S2CR(n) (0xc00 + ((n) << 2))
68#define ARM_SMMU_S2CR_PRIVCFG GENMASK(25, 24)
69
70enum arm_smmu_s2cr_privcfg {
71 S2CR_PRIVCFG_DEFAULT,
72 S2CR_PRIVCFG_DIPAN,
73 S2CR_PRIVCFG_UNPRIV,
74 S2CR_PRIVCFG_PRIV,
75};
76
77#define ARM_SMMU_S2CR_TYPE GENMASK(17, 16)
78
79enum arm_smmu_s2cr_type {
80 S2CR_TYPE_TRANS,
81 S2CR_TYPE_BYPASS,
82 S2CR_TYPE_FAULT,
83};
84
85#define ARM_SMMU_S2CR_EXIDVALID BIT(10)
86#define ARM_SMMU_S2CR_CBNDX GENMASK(7, 0)
87
88#define VMID_UNUSED 0xff
89
90struct qcom_smmu_priv {
91 phys_addr_t base;
92 struct list_head devices;
93 struct udevice *dev;
Caleb Connollybd59d5d2024-11-13 06:09:24 +010094 /* SMMU is not needed when running in EL2 */
95 bool disable;
Caleb Connollya95de052023-12-11 18:41:42 +000096
97 /* Read-once config */
98 int num_cb;
99 int num_smr;
100 u32 pgshift;
101 u32 cb_pg_offset;
102};
103
104struct mmu_dev {
105 struct list_head li;
106 struct udevice *dev;
107 u16 sid;
108 u16 cbx;
109 u16 smr;
110};
111
112#define page_addr(priv, page) ((priv)->base + ((page) << (priv)->pgshift))
113
114#define smmu_readl(priv, page, offset) readl(page_addr(priv, page) + offset)
115#define gr0_readl(priv, offset) smmu_readl(priv, ARM_SMMU_GR0, offset)
116#define gr1_readl(priv, offset) smmu_readl(priv, ARM_SMMU_GR1, offset)
117#define cbx_readl(priv, cbx, offset) \
118 smmu_readl(priv, (priv->cb_pg_offset) + cbx, offset)
119
120#define smmu_writel(priv, page, offset, value) \
121 writel((value), page_addr(priv, page) + offset)
122#define gr0_writel(priv, offset, value) \
123 smmu_writel(priv, ARM_SMMU_GR0, offset, (value))
124#define gr1_writel(priv, offset, value) \
125 smmu_writel(priv, ARM_SMMU_GR1, offset, (value))
126#define cbx_writel(priv, cbx, offset, value) \
127 smmu_writel(priv, (priv->cb_pg_offset) + cbx, offset, value)
128
129#define gr1_setbits(priv, offset, value) \
130 gr1_writel(priv, offset, gr1_readl(priv, offset) | (value))
131
132static int get_stream_id(struct udevice *dev)
133{
134 ofnode node = dev_ofnode(dev);
135 struct ofnode_phandle_args args;
136 int count = ofnode_parse_phandle_with_args(node, "iommus",
137 "#iommu-cells", 0, 0, &args);
138
Caleb Connolly61c3cff2024-11-13 06:00:56 +0100139 if (count < 0) {
Caleb Connollya95de052023-12-11 18:41:42 +0000140 printf("Error: %s: iommus property not found or wrong number of cells\n",
141 __func__);
142 return -EINVAL;
143 }
144
145 return args.args[0]; // Some mask from bit 16 onward?
146}
147
148static struct mmu_dev *alloc_dev(struct udevice *dev)
149{
150 struct qcom_smmu_priv *priv = dev_get_priv(dev->iommu);
151 struct mmu_dev *mmu_dev;
152 int sid;
153
154 sid = get_stream_id(dev);
155 debug("%s %s has SID %#x\n", dev->iommu->name, dev->name, sid);
156 if (sid < 0 || sid > 0xffff) {
157 printf("\tSMMU: Invalid stream ID for %s\n", dev->name);
158 return ERR_PTR(-EINVAL);
159 }
160
161 /* We only support a single SID per device for now */
162 list_for_each_entry(mmu_dev, &priv->devices, li) {
163 if (mmu_dev->sid == sid)
164 return ERR_PTR(-EEXIST);
165 }
166
167 mmu_dev = calloc(sizeof(*mmu_dev), 1);
168 if (!mmu_dev)
169 return ERR_PTR(-ENOMEM);
170
171 mmu_dev->dev = dev;
172 mmu_dev->sid = sid;
173
174 list_add_tail(&mmu_dev->li, &priv->devices);
175
176 return mmu_dev;
177}
178
179/* Find and init the first free context bank */
180static int alloc_cb(struct qcom_smmu_priv *priv)
181{
182 u32 cbar, type, vmid, val;
183
184 for (int i = 0; i < priv->num_cb; i++) {
185 cbar = gr1_readl(priv, ARM_SMMU_GR1_CBAR(i));
186 type = FIELD_GET(ARM_SMMU_CBAR_TYPE, cbar);
187 vmid = FIELD_GET(ARM_SMMU_CBAR_VMID, cbar);
188
189 /* Check that the context bank is available. We haven't reset the SMMU so
190 * we just make a best guess.
191 */
192 if (type != CBAR_TYPE_S2_TRANS &&
193 (type != CBAR_TYPE_S1_TRANS_S2_BYPASS ||
194 vmid != VMID_UNUSED))
195 continue;
196
197 debug("%s: Found free context bank %d (cbar %#x)\n",
198 priv->dev->name, i, cbar);
199 type = CBAR_TYPE_S1_TRANS_S2_BYPASS;
200 vmid = 0;
201 cbar &= ~ARM_SMMU_CBAR_TYPE & ~ARM_SMMU_CBAR_VMID;
202 cbar |= FIELD_PREP(ARM_SMMU_CBAR_TYPE, type) |
203 FIELD_PREP(ARM_SMMU_CBAR_VMID, vmid);
204 gr1_writel(priv, ARM_SMMU_GR1_CBAR(i), cbar);
205
206 val = IS_ENABLED(CONFIG_ARM64) == 1 ? ARM_SMMU_CBA2R_VA64 : 0;
207 gr1_setbits(priv, ARM_SMMU_GR1_CBA2R(i), val);
208 return i;
209 }
210
211 return -1;
212}
213
214/* Search for a context bank that is already configured for this stream
215 * returns the context bank index or -ENOENT
216 */
217static int find_smr(struct qcom_smmu_priv *priv, u16 stream_id)
218{
219 u32 val;
220 int i;
221
222 for (i = 0; i < priv->num_smr; i++) {
223 val = gr0_readl(priv, ARM_SMMU_GR0_SMR(i));
224 if (!(val & ARM_SMMU_SMR_VALID) ||
225 FIELD_GET(ARM_SMMU_SMR_ID, val) != stream_id)
226 continue;
227
228 return i;
229 }
230
231 return -ENOENT;
232}
233
234static int configure_smr_s2cr(struct qcom_smmu_priv *priv, struct mmu_dev *mdev)
235{
236 u32 val;
237 int i;
238
239 for (i = 0; i < priv->num_smr; i++) {
240 /* Configure SMR */
241 val = gr0_readl(priv, ARM_SMMU_GR0_SMR(i));
242 if (val & ARM_SMMU_SMR_VALID)
243 continue;
244
245 val = mdev->sid | ARM_SMMU_SMR_VALID;
246 gr0_writel(priv, ARM_SMMU_GR0_SMR(i), val);
247
248 /*
249 * WARNING: Don't change this to use S2CR_TYPE_BYPASS!
250 * Some Qualcomm boards have angry hypervisor firmware
251 * that converts S2CR type BYPASS to type FAULT on write.
252 * We don't use virtual addressing for these boards in
253 * u-boot so we can get away with using S2CR_TYPE_TRANS
254 * instead
255 */
256 val = FIELD_PREP(ARM_SMMU_S2CR_TYPE, S2CR_TYPE_TRANS) |
257 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, mdev->cbx);
258 gr0_writel(priv, ARM_SMMU_GR0_S2CR(i), val);
259
260 mdev->smr = i;
261 break;
262 }
263
264 /* Make sure our writes went through */
265 mb();
266
267 return 0;
268}
269
270static int qcom_smmu_connect(struct udevice *dev)
271{
272 struct mmu_dev *mdev;
273 struct qcom_smmu_priv *priv;
274 int ret;
275
276 debug("%s: %s -> %s\n", __func__, dev->name, dev->iommu->name);
277
278 priv = dev_get_priv(dev->iommu);
279 if (WARN_ON(!priv))
280 return -EINVAL;
281
Caleb Connollybd59d5d2024-11-13 06:09:24 +0100282 if (priv->disable)
283 return 0;
284
Caleb Connollya95de052023-12-11 18:41:42 +0000285 mdev = alloc_dev(dev);
286 if (IS_ERR(mdev) && PTR_ERR(mdev) != -EEXIST) {
287 printf("%s: %s Couldn't create mmu context\n", __func__,
288 dev->name);
289 return PTR_ERR(mdev);
290 } else if (IS_ERR(mdev)) { // -EEXIST
291 return 0;
292 }
293
294 if (find_smr(priv, mdev->sid) >= 0) {
295 debug("Found existing context bank for %s, skipping init\n",
296 dev->name);
297 return 0;
298 }
299
300 ret = alloc_cb(priv);
301 if (ret < 0 || ret > 0xff) {
302 printf("Error: %s: failed to allocate context bank for %s\n",
303 __func__, dev->name);
304 return 0;
305 }
306 mdev->cbx = ret;
307
308 /* Configure context bank registers */
309 cbx_writel(priv, mdev->cbx, ARM_SMMU_CB_TTBR0, 0x0);
310 cbx_writel(priv, mdev->cbx, ARM_SMMU_CB_TTBR1, 0x0);
311 cbx_writel(priv, mdev->cbx, ARM_SMMU_CB_S1_MAIR0, 0x0);
312 cbx_writel(priv, mdev->cbx, ARM_SMMU_CB_S1_MAIR1, 0x0);
313 cbx_writel(priv, mdev->cbx, ARM_SMMU_CB_SCTLR,
314 ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE |
315 ARM_SMMU_SCTLR_CFCFG);
316 cbx_writel(priv, mdev->cbx, ARM_SMMU_CB_TCR, 0x0);
317
318 /* Ensure that our writes went through */
319 mb();
320
321 configure_smr_s2cr(priv, mdev);
322
323 return 0;
324}
325
326#ifdef DEBUG
Caleb Connollyf33bbd32024-03-20 14:30:51 +0000327static inline void dump_boot_mappings(struct qcom_smmu_priv *priv)
Caleb Connollya95de052023-12-11 18:41:42 +0000328{
329 u32 val;
330 int i;
331
332 debug(" SMMU dump boot mappings:\n");
333 for (i = 0; i < priv->num_smr; i++) {
334 val = gr0_readl(priv, ARM_SMMU_GR0_SMR(i));
335 if (val & ARM_SMMU_SMR_VALID)
336 debug("\tSMR %3d: SID: %#lx\n", i,
337 FIELD_GET(ARM_SMMU_SMR_ID, val));
338 }
339}
340#else
341#define dump_boot_mappings(priv) \
342 do { \
343 } while (0)
344#endif
345
346static int qcom_smmu_probe(struct udevice *dev)
347{
348 struct qcom_smmu_priv *priv;
349 u32 val;
350
351 priv = dev_get_priv(dev);
352 priv->dev = dev;
353 priv->base = dev_read_addr(dev);
354 INIT_LIST_HEAD(&priv->devices);
355
Caleb Connollybd59d5d2024-11-13 06:09:24 +0100356 priv->disable = current_el() > 1;
357
Caleb Connollya95de052023-12-11 18:41:42 +0000358 /* Read SMMU config */
359 val = gr0_readl(priv, ARM_SMMU_GR0_ID0);
360 priv->num_smr = FIELD_GET(ARM_SMMU_ID0_NUMSMRG, val);
361
362 val = gr0_readl(priv, ARM_SMMU_GR0_ID1);
363 priv->num_cb = FIELD_GET(ARM_SMMU_ID1_NUMCB, val);
364 priv->pgshift = FIELD_GET(ARM_SMMU_ID1_PAGESIZE, val) ? 16 : 12;
365 priv->cb_pg_offset = 1
366 << (FIELD_GET(ARM_SMMU_ID1_NUMPAGENDXB, val) + 1);
367
368 dump_boot_mappings(priv);
369
370 return 0;
371}
372
373static int qcom_smmu_remove(struct udevice *dev)
374{
375 (void)dev;
376 /*
377 * We should probably try and de-configure things here,
378 * however I'm yet to find a way to do it without crashing
379 * and it seems like Linux doesn't care at all anyway.
380 */
381
382 return 0;
383}
384
385static struct iommu_ops qcom_smmu_ops = {
386 .connect = qcom_smmu_connect,
387};
388
389static const struct udevice_id qcom_smmu500_ids[] = {
390 { .compatible = "qcom,sdm845-smmu-500" },
Caleb Connollyb59ba462024-08-21 15:41:49 +0200391 { .compatible = "qcom,sc7280-smmu-500" },
Caleb Connollyfc133192024-04-18 18:25:46 +0100392 { .compatible = "qcom,smmu-500", },
Caleb Connollya95de052023-12-11 18:41:42 +0000393 { /* sentinel */ }
394};
395
396U_BOOT_DRIVER(qcom_smmu500) = {
397 .name = "qcom_smmu500",
398 .id = UCLASS_IOMMU,
399 .of_match = qcom_smmu500_ids,
400 .priv_auto = sizeof(struct qcom_smmu_priv),
401 .ops = &qcom_smmu_ops,
402 .probe = qcom_smmu_probe,
403 .remove = qcom_smmu_remove,
404 .flags = DM_FLAG_OS_PREPARE,
405};