Debian Codegraph @

drivers/iommu/arm/arm-smmu/arm-smmu-qcom.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
4 */
5
6 #include <linux/acpi.h>
7 #include <linux/adreno-smmu-priv.h>
8 #include <linux/delay.h>
9 #include <linux/of_device.h>
10 #include <linux/firmware/qcom/qcom_scm.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13
14 #include "arm-smmu.h"
15 #include "arm-smmu-qcom.h"
16
17 #define QCOM_DUMMY_VAL -1
18
19 /*
20 * SMMU-500 TRM defines BIT(0) as CMTLB (Enable context caching in the
21 * macro TLB) and BIT(1) as CPRE (Enable context caching in the prefetch
22 * buffer). The remaining bits are implementation defined and vary across
23 * SoCs.
24 */
25
26 #define CPRE (1 << 1)
27 #define CMTLB (1 << 0)
28 #define PREFETCH_SHIFT 8
29 #define PREFETCH_DEFAULT 0
30 #define PREFETCH_SHALLOW (1 << PREFETCH_SHIFT)
31 #define PREFETCH_MODERATE (2 << PREFETCH_SHIFT)
32 #define PREFETCH_DEEP (3 << PREFETCH_SHIFT)
33 #define GFX_ACTLR_PRR (1 << 5)
34
35 static const struct of_device_id qcom_smmu_actlr_client_of_match[] = {
36 { .compatible = "qcom,adreno",
37 .data = (const void *) (PREFETCH_DEEP | CPRE | CMTLB) },
38 { .compatible = "qcom,adreno-gmu",
39 .data = (const void *) (PREFETCH_DEEP | CPRE | CMTLB) },
40 { .compatible = "qcom,adreno-smmu",
41 .data = (const void *) (PREFETCH_DEEP | CPRE | CMTLB) },
42 { .compatible = "qcom,fastrpc",
43 .data = (const void *) (PREFETCH_DEEP | CPRE | CMTLB) },
44 { .compatible = "qcom,qcm2290-mdss",
45 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
46 { .compatible = "qcom,sa8775p-mdss",
47 .data = (const void *) (PREFETCH_DEFAULT | CMTLB) },
48 { .compatible = "qcom,sc7280-mdss",
49 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
50 { .compatible = "qcom,sc7280-venus",
51 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
52 { .compatible = "qcom,sc8180x-mdss",
53 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
54 { .compatible = "qcom,sc8280xp-mdss",
55 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
56 { .compatible = "qcom,sm6115-mdss",
57 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
58 { .compatible = "qcom,sm6125-mdss",
59 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
60 { .compatible = "qcom,sm6350-mdss",
61 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
62 { .compatible = "qcom,sm8150-mdss",
63 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
64 { .compatible = "qcom,sm8250-mdss",
65 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
66 { .compatible = "qcom,sm8350-mdss",
67 .data = (const void *) (PREFETCH_SHALLOW | CPRE | CMTLB) },
68 { .compatible = "qcom,sm8450-mdss",
69 .data = (const void *) (PREFETCH_DEFAULT | CMTLB) },
70 { .compatible = "qcom,sm8550-mdss",
71 .data = (const void *) (PREFETCH_DEFAULT | CMTLB) },
72 { .compatible = "qcom,sm8650-mdss",
73 .data = (const void *) (PREFETCH_DEFAULT | CMTLB) },
74 { .compatible = "qcom,sm8750-mdss",
75 .data = (const void *) (PREFETCH_DEFAULT | CMTLB) },
76 { .compatible = "qcom,x1e80100-mdss",
77 .data = (const void *) (PREFETCH_DEFAULT | CMTLB) },
78 { }
79 };
80
81 static struct qcom_smmu *to_qcom_smmu(struct arm_smmu_device *smmu)
82 {
83 return container_of(smmu, struct qcom_smmu, smmu);
84 }
85
86 static void qcom_smmu_tlb_sync(struct arm_smmu_device *smmu, int page,
87 int sync, int status)
88 {
89 unsigned int spin_cnt, delay;
90 u32 reg;
91
92 arm_smmu_writel(smmu, page, sync, QCOM_DUMMY_VAL);
93 for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
94 for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
95 reg = arm_smmu_readl(smmu, page, status);
96 if (!(reg & ARM_SMMU_sTLBGSTATUS_GSACTIVE))
97 return;
98 cpu_relax();
99 }
100 udelay(delay);
101 }
102
103 qcom_smmu_tlb_sync_debug(smmu);
104 }
105
106 static void qcom_adreno_smmu_write_sctlr(struct arm_smmu_device *smmu, int idx,
107 u32 reg)
108 {
109 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
110
111 /*
112 * On the GPU device we want to process subsequent transactions after a
113 * fault to keep the GPU from hanging
114 */
115 reg |= ARM_SMMU_SCTLR_HUPCF;
116
117 if (qsmmu->stall_enabled & BIT(idx))
118 reg |= ARM_SMMU_SCTLR_CFCFG;
119
120 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
121 }
122
123 static void qcom_adreno_smmu_get_fault_info(const void *cookie,
124 struct adreno_smmu_fault_info *info)
125 {
126 struct arm_smmu_domain *smmu_domain = (void *)cookie;
127 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
128 struct arm_smmu_device *smmu = smmu_domain->smmu;
129
130 info->fsr = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSR);
131 info->fsynr0 = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSYNR0);
132 info->fsynr1 = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSYNR1);
133 info->far = arm_smmu_cb_readq(smmu, cfg->cbndx, ARM_SMMU_CB_FAR);
134 info->cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(cfg->cbndx));
135 info->ttbr0 = arm_smmu_cb_readq(smmu, cfg->cbndx, ARM_SMMU_CB_TTBR0);
136 info->contextidr = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_CONTEXTIDR);
137 }
138
139 static void qcom_adreno_smmu_set_stall(const void *cookie, bool enabled)
140 {
141 struct arm_smmu_domain *smmu_domain = (void *)cookie;
142 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
143 struct arm_smmu_device *smmu = smmu_domain->smmu;
144 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
145 u32 mask = BIT(cfg->cbndx);
146 bool stall_changed = !!(qsmmu->stall_enabled & mask) != enabled;
147 unsigned long flags;
148
149 if (enabled)
150 qsmmu->stall_enabled |= mask;
151 else
152 qsmmu->stall_enabled &= ~mask;
153
154 /*
155 * If the device is on and we changed the setting, update the register.
156 * The spec pseudocode says that CFCFG is resampled after a fault, and
157 * we believe that no implementations cache it in the TLB, so it should
158 * be safe to change it without a TLB invalidation.
159 */
160 if (stall_changed && pm_runtime_get_if_active(smmu->dev) > 0) {
161 u32 reg;
162
163 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
164 reg = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_SCTLR);
165
166 if (enabled)
167 reg |= ARM_SMMU_SCTLR_CFCFG;
168 else
169 reg &= ~ARM_SMMU_SCTLR_CFCFG;
170
171 arm_smmu_cb_write(smmu, cfg->cbndx, ARM_SMMU_CB_SCTLR, reg);
172 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
173
174 pm_runtime_put_autosuspend(smmu->dev);
175 }
176 }
177
178 static void qcom_adreno_smmu_set_prr_bit(const void *cookie, bool set)
179 {
180 struct arm_smmu_domain *smmu_domain = (void *)cookie;
181 struct arm_smmu_device *smmu = smmu_domain->smmu;
182 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
183 u32 reg = 0;
184 int ret;
185
186 ret = pm_runtime_resume_and_get(smmu->dev);
187 if (ret < 0) {
188 dev_err(smmu->dev, "failed to get runtime PM: %d\n", ret);
189 return;
190 }
191
192 reg = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_ACTLR);
193 reg &= ~GFX_ACTLR_PRR;
194 if (set)
195 reg |= FIELD_PREP(GFX_ACTLR_PRR, 1);
196 arm_smmu_cb_write(smmu, cfg->cbndx, ARM_SMMU_CB_ACTLR, reg);
197 pm_runtime_put_autosuspend(smmu->dev);
198 }
199
200 static void qcom_adreno_smmu_set_prr_addr(const void *cookie, phys_addr_t page_addr)
201 {
202 struct arm_smmu_domain *smmu_domain = (void *)cookie;
203 struct arm_smmu_device *smmu = smmu_domain->smmu;
204 int ret;
205
206 ret = pm_runtime_resume_and_get(smmu->dev);
207 if (ret < 0) {
208 dev_err(smmu->dev, "failed to get runtime PM: %d\n", ret);
209 return;
210 }
211
212 writel_relaxed(lower_32_bits(page_addr),
213 smmu->base + ARM_SMMU_GFX_PRR_CFG_LADDR);
214 writel_relaxed(upper_32_bits(page_addr),
215 smmu->base + ARM_SMMU_GFX_PRR_CFG_UADDR);
216 pm_runtime_put_autosuspend(smmu->dev);
217 }
218
219 #define QCOM_ADRENO_SMMU_GPU_SID 0
220
221 static bool qcom_adreno_smmu_is_gpu_device(struct device *dev)
222 {
223 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
224 int i;
225
226 /*
227 * The GPU will always use SID 0 so that is a handy way to uniquely
228 * identify it and configure it for per-instance pagetables
229 */
230 for (i = 0; i < fwspec->num_ids; i++) {
231 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
232
233 if (sid == QCOM_ADRENO_SMMU_GPU_SID)
234 return true;
235 }
236
237 return false;
238 }
239
240 static const struct io_pgtable_cfg *qcom_adreno_smmu_get_ttbr1_cfg(
241 const void *cookie)
242 {
243 struct arm_smmu_domain *smmu_domain = (void *)cookie;
244 struct io_pgtable *pgtable =
245 io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops);
246 return &pgtable->cfg;
247 }
248
249 /*
250 * Local implementation to configure TTBR0 with the specified pagetable config.
251 * The GPU driver will call this to enable TTBR0 when per-instance pagetables
252 * are active
253 */
254
255 static int qcom_adreno_smmu_set_ttbr0_cfg(const void *cookie,
256 const struct io_pgtable_cfg *pgtbl_cfg)
257 {
258 struct arm_smmu_domain *smmu_domain = (void *)cookie;
259 struct io_pgtable *pgtable = io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops);
260 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
261 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
262
263 /* The domain must have split pagetables already enabled */
264 if (cb->tcr[0] & ARM_SMMU_TCR_EPD1)
265 return -EINVAL;
266
267 /* If the pagetable config is NULL, disable TTBR0 */
268 if (!pgtbl_cfg) {
269 /* Do nothing if it is already disabled */
270 if ((cb->tcr[0] & ARM_SMMU_TCR_EPD0))
271 return -EINVAL;
272
273 /* Set TCR to the original configuration */
274 cb->tcr[0] = arm_smmu_lpae_tcr(&pgtable->cfg);
275 cb->ttbr[0] = FIELD_PREP(ARM_SMMU_TTBRn_ASID, cb->cfg->asid);
276 } else {
277 u32 tcr = cb->tcr[0];
278
279 /* Don't call this again if TTBR0 is already enabled */
280 if (!(cb->tcr[0] & ARM_SMMU_TCR_EPD0))
281 return -EINVAL;
282
283 tcr |= arm_smmu_lpae_tcr(pgtbl_cfg);
284 tcr &= ~(ARM_SMMU_TCR_EPD0 | ARM_SMMU_TCR_EPD1);
285
286 cb->tcr[0] = tcr;
287 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
288 cb->ttbr[0] |= FIELD_PREP(ARM_SMMU_TTBRn_ASID, cb->cfg->asid);
289 }
290
291 arm_smmu_write_context_bank(smmu_domain->smmu, cb->cfg->cbndx);
292
293 return 0;
294 }
295
296 static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,
297 struct arm_smmu_device *smmu,
298 struct device *dev, int start)
299 {
300 int count;
301
302 /*
303 * Assign context bank 0 to the GPU device so the GPU hardware can
304 * switch pagetables
305 */
306 if (qcom_adreno_smmu_is_gpu_device(dev)) {
307 start = 0;
308 count = 1;
309 } else {
310 start = 1;
311 count = smmu->num_context_banks;
312 }
313
314 return __arm_smmu_alloc_bitmap(smmu->context_map, start, count);
315 }
316
317 static bool qcom_adreno_can_do_ttbr1(struct arm_smmu_device *smmu)
318 {
319 const struct device_node *np = smmu->dev->of_node;
320
321 if (of_device_is_compatible(np, "qcom,msm8996-smmu-v2"))
322 return false;
323
324 return true;
325 }
326
327 static void qcom_smmu_set_actlr_dev(struct device *dev, struct arm_smmu_device *smmu, int cbndx,
328 const struct of_device_id *client_match)
329 {
330 const struct of_device_id *match =
331 of_match_device(client_match, dev);
332
333 if (!match) {
334 dev_dbg(dev, "no ACTLR settings present\n");
335 return;
336 }
337
338 arm_smmu_cb_write(smmu, cbndx, ARM_SMMU_CB_ACTLR, (unsigned long)match->data);
339 }
340
341 static int qcom_adreno_smmu_init_context(struct arm_smmu_domain *smmu_domain,
342 struct io_pgtable_cfg *pgtbl_cfg, struct device *dev)
343 {
344 const struct device_node *np = smmu_domain->smmu->dev->of_node;
345 struct arm_smmu_device *smmu = smmu_domain->smmu;
346 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
347 const struct of_device_id *client_match;
348 int cbndx = smmu_domain->cfg.cbndx;
349 struct adreno_smmu_priv *priv;
350
351 smmu_domain->cfg.flush_walk_prefer_tlbiasid = true;
352
353 client_match = qsmmu->data->client_match;
354
355 if (client_match)
356 qcom_smmu_set_actlr_dev(dev, smmu, cbndx, client_match);
357
358 /* Only enable split pagetables for the GPU device (SID 0) */
359 if (!qcom_adreno_smmu_is_gpu_device(dev))
360 return 0;
361
362 /*
363 * All targets that use the qcom,adreno-smmu compatible string *should*
364 * be AARCH64 stage 1 but double check because the arm-smmu code assumes
365 * that is the case when the TTBR1 quirk is enabled
366 */
367 if (qcom_adreno_can_do_ttbr1(smmu_domain->smmu) &&
368 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) &&
369 (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64))
370 pgtbl_cfg->quirks |= IO_PGTABLE_QUIRK_ARM_TTBR1;
371
372 /*
373 * Initialize private interface with GPU:
374 */
375
376 priv = dev_get_drvdata(dev);
377 priv->cookie = smmu_domain;
378 priv->get_ttbr1_cfg = qcom_adreno_smmu_get_ttbr1_cfg;
379 priv->set_ttbr0_cfg = qcom_adreno_smmu_set_ttbr0_cfg;
380 priv->get_fault_info = qcom_adreno_smmu_get_fault_info;
381 priv->set_stall = qcom_adreno_smmu_set_stall;
382 priv->set_prr_bit = NULL;
383 priv->set_prr_addr = NULL;
384
385 if (of_device_is_compatible(np, "qcom,smmu-500") &&
386 !of_device_is_compatible(np, "qcom,sm8250-smmu-500") &&
387 of_device_is_compatible(np, "qcom,adreno-smmu")) {
388 priv->set_prr_bit = qcom_adreno_smmu_set_prr_bit;
389 priv->set_prr_addr = qcom_adreno_smmu_set_prr_addr;
390 }
391
392 return 0;
393 }
394
395 static const struct of_device_id qcom_smmu_client_of_match[] __maybe_unused = {
396 { .compatible = "qcom,adreno" },
397 { .compatible = "qcom,adreno-gmu" },
398 { .compatible = "qcom,glymur-mdss" },
399 { .compatible = "qcom,mdp4" },
400 { .compatible = "qcom,mdss" },
401 { .compatible = "qcom,qcm2290-mdss" },
402 { .compatible = "qcom,sar2130p-mdss" },
403 { .compatible = "qcom,sc7180-mdss" },
404 { .compatible = "qcom,sc7180-mss-pil" },
405 { .compatible = "qcom,sc7280-mdss" },
406 { .compatible = "qcom,sc7280-mss-pil" },
407 { .compatible = "qcom,sc8180x-mdss" },
408 { .compatible = "qcom,sc8280xp-mdss" },
409 { .compatible = "qcom,sdm670-mdss" },
410 { .compatible = "qcom,sdm845-mdss" },
411 { .compatible = "qcom,sdm845-mss-pil" },
412 { .compatible = "qcom,sm6115-mdss" },
413 { .compatible = "qcom,sm6350-mdss" },
414 { .compatible = "qcom,sm6375-mdss" },
415 { .compatible = "qcom,sm8150-mdss" },
416 { .compatible = "qcom,sm8250-mdss" },
417 { .compatible = "qcom,x1e80100-mdss" },
418 { }
419 };
420
421 static int qcom_smmu_init_context(struct arm_smmu_domain *smmu_domain,
422 struct io_pgtable_cfg *pgtbl_cfg, struct device *dev)
423 {
424 struct arm_smmu_device *smmu = smmu_domain->smmu;
425 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
426 const struct of_device_id *client_match;
427 int cbndx = smmu_domain->cfg.cbndx;
428
429 smmu_domain->cfg.flush_walk_prefer_tlbiasid = true;
430
431 client_match = qsmmu->data->client_match;
432
433 if (client_match)
434 qcom_smmu_set_actlr_dev(dev, smmu, cbndx, client_match);
435
436 return 0;
437 }
438
439 static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu)
440 {
441 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
442 unsigned int last_s2cr;
443 u32 reg;
444 u32 smr;
445 int i;
446
447 /*
448 * MSM8998 LPASS SMMU reports 13 context banks, but accessing
449 * the last context bank crashes the system.
450 */
451 if (of_device_is_compatible(smmu->dev->of_node, "qcom,msm8998-smmu-v2") &&
452 smmu->num_context_banks == 13) {
453 smmu->num_context_banks = 12;
454 } else if (of_device_is_compatible(smmu->dev->of_node, "qcom,sdm630-smmu-v2")) {
455 if (smmu->num_context_banks == 21) /* SDM630 / SDM660 A2NOC SMMU */
456 smmu->num_context_banks = 7;
457 else if (smmu->num_context_banks == 14) /* SDM630 / SDM660 LPASS SMMU */
458 smmu->num_context_banks = 13;
459 }
460
461 /*
462 * Some platforms support more than the Arm SMMU architected maximum of
463 * 128 stream matching groups. The additional registers appear to have
464 * the same behavior as the architected registers in the hardware.
465 * However, on some firmware versions, the hypervisor does not
466 * correctly trap and emulate accesses to the additional registers,
467 * resulting in unexpected behavior.
468 *
469 * If there are more than 128 groups, use the last reliable group to
470 * detect if we need to apply the bypass quirk.
471 */
472 if (smmu->num_mapping_groups > 128)
473 last_s2cr = ARM_SMMU_GR0_S2CR(127);
474 else
475 last_s2cr = ARM_SMMU_GR0_S2CR(smmu->num_mapping_groups - 1);
476
477 /*
478 * With some firmware versions writes to S2CR of type FAULT are
479 * ignored, and writing BYPASS will end up written as FAULT in the
480 * register. Perform a write to S2CR to detect if this is the case and
481 * if so reserve a context bank to emulate bypass streams.
482 */
483 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, S2CR_TYPE_BYPASS) |
484 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, 0xff) |
485 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, S2CR_PRIVCFG_DEFAULT);
486 arm_smmu_gr0_write(smmu, last_s2cr, reg);
487 reg = arm_smmu_gr0_read(smmu, last_s2cr);
488 if (FIELD_GET(ARM_SMMU_S2CR_TYPE, reg) != S2CR_TYPE_BYPASS) {
489 qsmmu->bypass_quirk = true;
490 qsmmu->bypass_cbndx = smmu->num_context_banks - 1;
491
492 set_bit(qsmmu->bypass_cbndx, smmu->context_map);
493
494 arm_smmu_cb_write(smmu, qsmmu->bypass_cbndx, ARM_SMMU_CB_SCTLR, 0);
495
496 reg = FIELD_PREP(ARM_SMMU_CBAR_TYPE, CBAR_TYPE_S1_TRANS_S2_BYPASS);
497 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(qsmmu->bypass_cbndx), reg);
498
499 if (smmu->num_mapping_groups > 128) {
500 dev_notice(smmu->dev, "\tLimiting the stream matching groups to 128\n");
501 smmu->num_mapping_groups = 128;
502 }
503 }
504
505 for (i = 0; i < smmu->num_mapping_groups; i++) {
506 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
507
508 if (FIELD_GET(ARM_SMMU_SMR_VALID, smr)) {
509 /* Ignore valid bit for SMR mask extraction. */
510 smr &= ~ARM_SMMU_SMR_VALID;
511 smmu->smrs[i].id = FIELD_GET(ARM_SMMU_SMR_ID, smr);
512 smmu->smrs[i].mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
513 smmu->smrs[i].valid = true;
514
515 smmu->s2crs[i].type = S2CR_TYPE_BYPASS;
516 smmu->s2crs[i].privcfg = S2CR_PRIVCFG_DEFAULT;
517 smmu->s2crs[i].cbndx = 0xff;
518 }
519 }
520
521 return 0;
522 }
523
524 static int qcom_adreno_smmuv2_cfg_probe(struct arm_smmu_device *smmu)
525 {
526 /* Support for 16K pages is advertised on some SoCs, but it doesn't seem to work */
527 smmu->features &= ~ARM_SMMU_FEAT_FMT_AARCH64_16K;
528
529 /* TZ protects several last context banks, hide them from Linux */
530 if (of_device_is_compatible(smmu->dev->of_node, "qcom,sdm630-smmu-v2") &&
531 smmu->num_context_banks == 5)
532 smmu->num_context_banks = 2;
533
534 return 0;
535 }
536
537 static void qcom_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
538 {
539 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
540 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
541 u32 cbndx = s2cr->cbndx;
542 u32 type = s2cr->type;
543 u32 reg;
544
545 if (qsmmu->bypass_quirk) {
546 if (type == S2CR_TYPE_BYPASS) {
547 /*
548 * Firmware with quirky S2CR handling will substitute
549 * BYPASS writes with FAULT, so point the stream to the
550 * reserved context bank and ask for translation on the
551 * stream
552 */
553 type = S2CR_TYPE_TRANS;
554 cbndx = qsmmu->bypass_cbndx;
555 } else if (type == S2CR_TYPE_FAULT) {
556 /*
557 * Firmware with quirky S2CR handling will ignore FAULT
558 * writes, so trick it to write FAULT by asking for a
559 * BYPASS.
560 */
561 type = S2CR_TYPE_BYPASS;
562 cbndx = 0xff;
563 }
564 }
565
566 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, type) |
567 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, cbndx) |
568 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, s2cr->privcfg);
569 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
570 }
571
572 static int qcom_smmu_def_domain_type(struct device *dev)
573 {
574 const struct of_device_id *match =
575 of_match_device(qcom_smmu_client_of_match, dev);
576
577 return match ? IOMMU_DOMAIN_IDENTITY : 0;
578 }
579
580 static int qcom_sdm845_smmu500_reset(struct arm_smmu_device *smmu)
581 {
582 int ret;
583
584 arm_mmu500_reset(smmu);
585
586 /*
587 * To address performance degradation in non-real time clients,
588 * such as USB and UFS, turn off wait-for-safe on sdm845 based boards,
589 * such as MTP and db845, whose firmwares implement secure monitor
590 * call handlers to turn on/off the wait-for-safe logic.
591 */
592 ret = qcom_scm_qsmmu500_wait_safe_toggle(0);
593 if (ret)
594 dev_warn(smmu->dev, "Failed to turn off SAFE logic\n");
595
596 return ret;
597 }
598
599 static const struct arm_smmu_impl qcom_smmu_v2_impl = {
600 .init_context = qcom_smmu_init_context,
601 .cfg_probe = qcom_smmu_cfg_probe,
602 .def_domain_type = qcom_smmu_def_domain_type,
603 .write_s2cr = qcom_smmu_write_s2cr,
604 .tlb_sync = qcom_smmu_tlb_sync,
605 };
606
607 static const struct arm_smmu_impl qcom_smmu_500_impl = {
608 .init_context = qcom_smmu_init_context,
609 .cfg_probe = qcom_smmu_cfg_probe,
610 .def_domain_type = qcom_smmu_def_domain_type,
611 .reset = arm_mmu500_reset,
612 .write_s2cr = qcom_smmu_write_s2cr,
613 .tlb_sync = qcom_smmu_tlb_sync,
614 #ifdef CONFIG_ARM_SMMU_QCOM_DEBUG
615 .context_fault = qcom_smmu_context_fault,
616 .context_fault_needs_threaded_irq = true,
617 #endif
618 };
619
620 static const struct arm_smmu_impl sdm845_smmu_500_impl = {
621 .init_context = qcom_smmu_init_context,
622 .cfg_probe = qcom_smmu_cfg_probe,
623 .def_domain_type = qcom_smmu_def_domain_type,
624 .reset = qcom_sdm845_smmu500_reset,
625 .write_s2cr = qcom_smmu_write_s2cr,
626 .tlb_sync = qcom_smmu_tlb_sync,
627 #ifdef CONFIG_ARM_SMMU_QCOM_DEBUG
628 .context_fault = qcom_smmu_context_fault,
629 .context_fault_needs_threaded_irq = true,
630 #endif
631 };
632
633 static const struct arm_smmu_impl qcom_adreno_smmu_v2_impl = {
634 .init_context = qcom_adreno_smmu_init_context,
635 .cfg_probe = qcom_adreno_smmuv2_cfg_probe,
636 .def_domain_type = qcom_smmu_def_domain_type,
637 .alloc_context_bank = qcom_adreno_smmu_alloc_context_bank,
638 .write_sctlr = qcom_adreno_smmu_write_sctlr,
639 .tlb_sync = qcom_smmu_tlb_sync,
640 .context_fault_needs_threaded_irq = true,
641 };
642
643 static const struct arm_smmu_impl qcom_adreno_smmu_500_impl = {
644 .init_context = qcom_adreno_smmu_init_context,
645 .def_domain_type = qcom_smmu_def_domain_type,
646 .reset = arm_mmu500_reset,
647 .alloc_context_bank = qcom_adreno_smmu_alloc_context_bank,
648 .write_sctlr = qcom_adreno_smmu_write_sctlr,
649 .tlb_sync = qcom_smmu_tlb_sync,
650 .context_fault_needs_threaded_irq = true,
651 };
652
653 static struct arm_smmu_device *qcom_smmu_create(struct arm_smmu_device *smmu,
654 const struct qcom_smmu_match_data *data)
655 {
656 const struct device_node *np = smmu->dev->of_node;
657 const struct arm_smmu_impl *impl;
658 struct qcom_smmu *qsmmu;
659
660 if (!data)
661 return ERR_PTR(-EINVAL);
662
663 if (np && of_device_is_compatible(np, "qcom,adreno-smmu"))
664 impl = data->adreno_impl;
665 else
666 impl = data->impl;
667
668 if (!impl)
669 return smmu;
670
671 /* Check to make sure qcom_scm has finished probing */
672 if (!qcom_scm_is_available())
673 return ERR_PTR(dev_err_probe(smmu->dev, -EPROBE_DEFER,
674 "qcom_scm not ready\n"));
675
676 qsmmu = devm_krealloc(smmu->dev, smmu, sizeof(*qsmmu), GFP_KERNEL);
677 if (!qsmmu)
678 return ERR_PTR(-ENOMEM);
679
680 qsmmu->smmu.impl = impl;
681 qsmmu->data = data;
682
683 return &qsmmu->smmu;
684 }
685
686 /* Implementation Defined Register Space 0 register offsets */
687 static const u32 qcom_smmu_impl0_reg_offset[] = {
688 [QCOM_SMMU_TBU_PWR_STATUS] = 0x2204,
689 [QCOM_SMMU_STATS_SYNC_INV_TBU_ACK] = 0x25dc,
690 [QCOM_SMMU_MMU2QSS_AND_SAFE_WAIT_CNTR] = 0x2670,
691 };
692
693 static const struct qcom_smmu_config qcom_smmu_impl0_cfg = {
694 .reg_offset = qcom_smmu_impl0_reg_offset,
695 };
696
697 /*
698 * It is not yet possible to use MDP SMMU with the bypass quirk on the msm8996,
699 * there are not enough context banks.
700 */
701 static const struct qcom_smmu_match_data msm8996_smmu_data = {
702 .impl = NULL,
703 .adreno_impl = &qcom_adreno_smmu_v2_impl,
704 };
705
706 static const struct qcom_smmu_match_data qcom_smmu_v2_data = {
707 .impl = &qcom_smmu_v2_impl,
708 .adreno_impl = &qcom_adreno_smmu_v2_impl,
709 };
710
711 static const struct qcom_smmu_match_data sdm845_smmu_500_data = {
712 .impl = &sdm845_smmu_500_impl,
713 /*
714 * No need for adreno impl here. On sdm845 the Adreno SMMU is handled
715 * by the separate sdm845-smmu-v2 device.
716 */
717 /* Also no debug configuration. */
718 };
719
720 static const struct qcom_smmu_match_data qcom_smmu_500_impl0_data = {
721 .impl = &qcom_smmu_500_impl,
722 .adreno_impl = &qcom_adreno_smmu_500_impl,
723 .cfg = &qcom_smmu_impl0_cfg,
724 .client_match = qcom_smmu_actlr_client_of_match,
725 };
726
727 /*
728 * Do not add any more qcom,SOC-smmu-500 entries to this list, unless they need
729 * special handling and can not be covered by the qcom,smmu-500 entry.
730 */
731 static const struct of_device_id __maybe_unused qcom_smmu_impl_of_match[] = {
732 { .compatible = "qcom,msm8996-smmu-v2", .data = &msm8996_smmu_data },
733 { .compatible = "qcom,msm8998-smmu-v2", .data = &qcom_smmu_v2_data },
734 { .compatible = "qcom,qcm2290-smmu-500", .data = &qcom_smmu_500_impl0_data },
735 { .compatible = "qcom,qdu1000-smmu-500", .data = &qcom_smmu_500_impl0_data },
736 { .compatible = "qcom,sc7180-smmu-500", .data = &qcom_smmu_500_impl0_data },
737 { .compatible = "qcom,sc7180-smmu-v2", .data = &qcom_smmu_v2_data },
738 { .compatible = "qcom,sc7280-smmu-500", .data = &qcom_smmu_500_impl0_data },
739 { .compatible = "qcom,sc8180x-smmu-500", .data = &qcom_smmu_500_impl0_data },
740 { .compatible = "qcom,sc8280xp-smmu-500", .data = &qcom_smmu_500_impl0_data },
741 { .compatible = "qcom,sdm630-smmu-v2", .data = &qcom_smmu_v2_data },
742 { .compatible = "qcom,sdm670-smmu-v2", .data = &qcom_smmu_v2_data },
743 { .compatible = "qcom,sdm845-smmu-v2", .data = &qcom_smmu_v2_data },
744 { .compatible = "qcom,sdm845-smmu-500", .data = &sdm845_smmu_500_data },
745 { .compatible = "qcom,sm6115-smmu-500", .data = &qcom_smmu_500_impl0_data},
746 { .compatible = "qcom,sm6125-smmu-500", .data = &qcom_smmu_500_impl0_data },
747 { .compatible = "qcom,sm6350-smmu-v2", .data = &qcom_smmu_v2_data },
748 { .compatible = "qcom,sm6350-smmu-500", .data = &qcom_smmu_500_impl0_data },
749 { .compatible = "qcom,sm6375-smmu-v2", .data = &qcom_smmu_v2_data },
750 { .compatible = "qcom,sm6375-smmu-500", .data = &qcom_smmu_500_impl0_data },
751 { .compatible = "qcom,sm7150-smmu-v2", .data = &qcom_smmu_v2_data },
752 { .compatible = "qcom,sm8150-smmu-500", .data = &qcom_smmu_500_impl0_data },
753 { .compatible = "qcom,sm8250-smmu-500", .data = &qcom_smmu_500_impl0_data },
754 { .compatible = "qcom,sm8350-smmu-500", .data = &qcom_smmu_500_impl0_data },
755 { .compatible = "qcom,sm8450-smmu-500", .data = &qcom_smmu_500_impl0_data },
756 { .compatible = "qcom,smmu-500", .data = &qcom_smmu_500_impl0_data },
757 { }
758 };
759
760 #ifdef CONFIG_ACPI
761 static struct acpi_platform_list qcom_acpi_platlist[] = {
762 { "LENOVO", "CB-01 ", 0x8180, ACPI_SIG_IORT, equal, "QCOM SMMU" },
763 { "QCOM ", "QCOMEDK2", 0x8180, ACPI_SIG_IORT, equal, "QCOM SMMU" },
764 { }
765 };
766 #endif
767
768 static int qcom_smmu_tbu_probe(struct platform_device *pdev)
769 {
770 struct device *dev = &pdev->dev;
771 int ret;
772
773 if (IS_ENABLED(CONFIG_ARM_SMMU_QCOM_DEBUG)) {
774 ret = qcom_tbu_probe(pdev);
775 if (ret)
776 return ret;
777 }
778
779 if (dev->pm_domain) {
780 pm_runtime_set_active(dev);
781 pm_runtime_enable(dev);
782 }
783
784 return 0;
785 }
786
787 static const struct of_device_id qcom_smmu_tbu_of_match[] = {
788 { .compatible = "qcom,sc7280-tbu" },
789 { .compatible = "qcom,sdm845-tbu" },
790 { }
791 };
792
793 static struct platform_driver qcom_smmu_tbu_driver = {
794 .driver = {
795 .name = "qcom_tbu",
796 .of_match_table = qcom_smmu_tbu_of_match,
797 },
798 .probe = qcom_smmu_tbu_probe,
799 };
800
801 struct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu)
802 {
803 const struct device_node *np = smmu->dev->of_node;
804 const struct of_device_id *match;
805
806 #ifdef CONFIG_ACPI
807 if (np == NULL) {
808 /* Match platform for ACPI boot */
809 if (acpi_match_platform_list(qcom_acpi_platlist) >= 0)
810 return qcom_smmu_create(smmu, &qcom_smmu_500_impl0_data);
811 }
812 #endif
813
814 match = of_match_node(qcom_smmu_impl_of_match, np);
815 if (match)
816 return qcom_smmu_create(smmu, match->data);
817
818 /*
819 * If you hit this WARN_ON() you are missing an entry in the
820 * qcom_smmu_impl_of_match[] table, and GPU per-process page-
821 * tables will be broken.
822 */
823 WARN(of_device_is_compatible(np, "qcom,adreno-smmu"),
824 "Missing qcom_smmu_impl_of_match entry for: %s",
825 dev_name(smmu->dev));
826
827 return smmu;
828 }
829
830 int __init qcom_smmu_module_init(void)
831 {
832 return platform_driver_register(&qcom_smmu_tbu_driver);
833 }
834
835 void __exit qcom_smmu_module_exit(void)
836 {
837 platform_driver_unregister(&qcom_smmu_tbu_driver);
838 }