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/of_device.h>
9 #include <linux/qcom_scm.h>
10
11 #include "arm-smmu.h"
12
13 struct qcom_smmu {
14 struct arm_smmu_device smmu;
15 bool bypass_quirk;
16 u8 bypass_cbndx;
17 u32 stall_enabled;
18 };
19
to_qcom_smmu(struct arm_smmu_device * smmu)20 static struct qcom_smmu *to_qcom_smmu(struct arm_smmu_device *smmu)
21 {
22 return container_of(smmu, struct qcom_smmu, smmu);
23 }
24
qcom_adreno_smmu_write_sctlr(struct arm_smmu_device * smmu,int idx,u32 reg)25 static void qcom_adreno_smmu_write_sctlr(struct arm_smmu_device *smmu, int idx,
26 u32 reg)
27 {
28 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
29
30 /*
31 * On the GPU device we want to process subsequent transactions after a
32 * fault to keep the GPU from hanging
33 */
34 reg |= ARM_SMMU_SCTLR_HUPCF;
35
36 if (qsmmu->stall_enabled & BIT(idx))
37 reg |= ARM_SMMU_SCTLR_CFCFG;
38
39 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
40 }
41
qcom_adreno_smmu_get_fault_info(const void * cookie,struct adreno_smmu_fault_info * info)42 static void qcom_adreno_smmu_get_fault_info(const void *cookie,
43 struct adreno_smmu_fault_info *info)
44 {
45 struct arm_smmu_domain *smmu_domain = (void *)cookie;
46 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
47 struct arm_smmu_device *smmu = smmu_domain->smmu;
48
49 info->fsr = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSR);
50 info->fsynr0 = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSYNR0);
51 info->fsynr1 = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_FSYNR1);
52 info->far = arm_smmu_cb_readq(smmu, cfg->cbndx, ARM_SMMU_CB_FAR);
53 info->cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(cfg->cbndx));
54 info->ttbr0 = arm_smmu_cb_readq(smmu, cfg->cbndx, ARM_SMMU_CB_TTBR0);
55 info->contextidr = arm_smmu_cb_read(smmu, cfg->cbndx, ARM_SMMU_CB_CONTEXTIDR);
56 }
57
qcom_adreno_smmu_set_stall(const void * cookie,bool enabled)58 static void qcom_adreno_smmu_set_stall(const void *cookie, bool enabled)
59 {
60 struct arm_smmu_domain *smmu_domain = (void *)cookie;
61 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
62 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu_domain->smmu);
63
64 if (enabled)
65 qsmmu->stall_enabled |= BIT(cfg->cbndx);
66 else
67 qsmmu->stall_enabled &= ~BIT(cfg->cbndx);
68 }
69
qcom_adreno_smmu_resume_translation(const void * cookie,bool terminate)70 static void qcom_adreno_smmu_resume_translation(const void *cookie, bool terminate)
71 {
72 struct arm_smmu_domain *smmu_domain = (void *)cookie;
73 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
74 struct arm_smmu_device *smmu = smmu_domain->smmu;
75 u32 reg = 0;
76
77 if (terminate)
78 reg |= ARM_SMMU_RESUME_TERMINATE;
79
80 arm_smmu_cb_write(smmu, cfg->cbndx, ARM_SMMU_CB_RESUME, reg);
81 }
82
83 #define QCOM_ADRENO_SMMU_GPU_SID 0
84
qcom_adreno_smmu_is_gpu_device(struct device * dev)85 static bool qcom_adreno_smmu_is_gpu_device(struct device *dev)
86 {
87 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
88 int i;
89
90 /*
91 * The GPU will always use SID 0 so that is a handy way to uniquely
92 * identify it and configure it for per-instance pagetables
93 */
94 for (i = 0; i < fwspec->num_ids; i++) {
95 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
96
97 if (sid == QCOM_ADRENO_SMMU_GPU_SID)
98 return true;
99 }
100
101 return false;
102 }
103
qcom_adreno_smmu_get_ttbr1_cfg(const void * cookie)104 static const struct io_pgtable_cfg *qcom_adreno_smmu_get_ttbr1_cfg(
105 const void *cookie)
106 {
107 struct arm_smmu_domain *smmu_domain = (void *)cookie;
108 struct io_pgtable *pgtable =
109 io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops);
110 return &pgtable->cfg;
111 }
112
113 /*
114 * Local implementation to configure TTBR0 with the specified pagetable config.
115 * The GPU driver will call this to enable TTBR0 when per-instance pagetables
116 * are active
117 */
118
qcom_adreno_smmu_set_ttbr0_cfg(const void * cookie,const struct io_pgtable_cfg * pgtbl_cfg)119 static int qcom_adreno_smmu_set_ttbr0_cfg(const void *cookie,
120 const struct io_pgtable_cfg *pgtbl_cfg)
121 {
122 struct arm_smmu_domain *smmu_domain = (void *)cookie;
123 struct io_pgtable *pgtable = io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops);
124 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
125 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
126
127 /* The domain must have split pagetables already enabled */
128 if (cb->tcr[0] & ARM_SMMU_TCR_EPD1)
129 return -EINVAL;
130
131 /* If the pagetable config is NULL, disable TTBR0 */
132 if (!pgtbl_cfg) {
133 /* Do nothing if it is already disabled */
134 if ((cb->tcr[0] & ARM_SMMU_TCR_EPD0))
135 return -EINVAL;
136
137 /* Set TCR to the original configuration */
138 cb->tcr[0] = arm_smmu_lpae_tcr(&pgtable->cfg);
139 cb->ttbr[0] = FIELD_PREP(ARM_SMMU_TTBRn_ASID, cb->cfg->asid);
140 } else {
141 u32 tcr = cb->tcr[0];
142
143 /* Don't call this again if TTBR0 is already enabled */
144 if (!(cb->tcr[0] & ARM_SMMU_TCR_EPD0))
145 return -EINVAL;
146
147 tcr |= arm_smmu_lpae_tcr(pgtbl_cfg);
148 tcr &= ~(ARM_SMMU_TCR_EPD0 | ARM_SMMU_TCR_EPD1);
149
150 cb->tcr[0] = tcr;
151 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
152 cb->ttbr[0] |= FIELD_PREP(ARM_SMMU_TTBRn_ASID, cb->cfg->asid);
153 }
154
155 arm_smmu_write_context_bank(smmu_domain->smmu, cb->cfg->cbndx);
156
157 return 0;
158 }
159
qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain * smmu_domain,struct arm_smmu_device * smmu,struct device * dev,int start)160 static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,
161 struct arm_smmu_device *smmu,
162 struct device *dev, int start)
163 {
164 int count;
165
166 /*
167 * Assign context bank 0 to the GPU device so the GPU hardware can
168 * switch pagetables
169 */
170 if (qcom_adreno_smmu_is_gpu_device(dev)) {
171 start = 0;
172 count = 1;
173 } else {
174 start = 1;
175 count = smmu->num_context_banks;
176 }
177
178 return __arm_smmu_alloc_bitmap(smmu->context_map, start, count);
179 }
180
qcom_adreno_can_do_ttbr1(struct arm_smmu_device * smmu)181 static bool qcom_adreno_can_do_ttbr1(struct arm_smmu_device *smmu)
182 {
183 const struct device_node *np = smmu->dev->of_node;
184
185 if (of_device_is_compatible(np, "qcom,msm8996-smmu-v2"))
186 return false;
187
188 return true;
189 }
190
qcom_adreno_smmu_init_context(struct arm_smmu_domain * smmu_domain,struct io_pgtable_cfg * pgtbl_cfg,struct device * dev)191 static int qcom_adreno_smmu_init_context(struct arm_smmu_domain *smmu_domain,
192 struct io_pgtable_cfg *pgtbl_cfg, struct device *dev)
193 {
194 struct adreno_smmu_priv *priv;
195
196 smmu_domain->cfg.flush_walk_prefer_tlbiasid = true;
197
198 /* Only enable split pagetables for the GPU device (SID 0) */
199 if (!qcom_adreno_smmu_is_gpu_device(dev))
200 return 0;
201
202 /*
203 * All targets that use the qcom,adreno-smmu compatible string *should*
204 * be AARCH64 stage 1 but double check because the arm-smmu code assumes
205 * that is the case when the TTBR1 quirk is enabled
206 */
207 if (qcom_adreno_can_do_ttbr1(smmu_domain->smmu) &&
208 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) &&
209 (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64))
210 pgtbl_cfg->quirks |= IO_PGTABLE_QUIRK_ARM_TTBR1;
211
212 /*
213 * Initialize private interface with GPU:
214 */
215
216 priv = dev_get_drvdata(dev);
217 priv->cookie = smmu_domain;
218 priv->get_ttbr1_cfg = qcom_adreno_smmu_get_ttbr1_cfg;
219 priv->set_ttbr0_cfg = qcom_adreno_smmu_set_ttbr0_cfg;
220 priv->get_fault_info = qcom_adreno_smmu_get_fault_info;
221 priv->set_stall = qcom_adreno_smmu_set_stall;
222 priv->resume_translation = qcom_adreno_smmu_resume_translation;
223
224 return 0;
225 }
226
227 static const struct of_device_id qcom_smmu_client_of_match[] __maybe_unused = {
228 { .compatible = "qcom,adreno" },
229 { .compatible = "qcom,adreno-gmu" },
230 { .compatible = "qcom,mdp4" },
231 { .compatible = "qcom,mdss" },
232 { .compatible = "qcom,sc7180-mdss" },
233 { .compatible = "qcom,sc7180-mss-pil" },
234 { .compatible = "qcom,sc7280-mdss" },
235 { .compatible = "qcom,sc8180x-mdss" },
236 { .compatible = "qcom,sdm845-mdss" },
237 { .compatible = "qcom,sdm845-mss-pil" },
238 { }
239 };
240
qcom_smmu_init_context(struct arm_smmu_domain * smmu_domain,struct io_pgtable_cfg * pgtbl_cfg,struct device * dev)241 static int qcom_smmu_init_context(struct arm_smmu_domain *smmu_domain,
242 struct io_pgtable_cfg *pgtbl_cfg, struct device *dev)
243 {
244 smmu_domain->cfg.flush_walk_prefer_tlbiasid = true;
245
246 return 0;
247 }
248
qcom_smmu_cfg_probe(struct arm_smmu_device * smmu)249 static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu)
250 {
251 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
252 unsigned int last_s2cr;
253 u32 reg;
254 u32 smr;
255 int i;
256
257 /*
258 * Some platforms support more than the Arm SMMU architected maximum of
259 * 128 stream matching groups. For unknown reasons, the additional
260 * groups don't exhibit the same behavior as the architected registers,
261 * so limit the groups to 128 until the behavior is fixed for the other
262 * groups.
263 */
264 if (smmu->num_mapping_groups > 128) {
265 dev_notice(smmu->dev, "\tLimiting the stream matching groups to 128\n");
266 smmu->num_mapping_groups = 128;
267 }
268
269 last_s2cr = ARM_SMMU_GR0_S2CR(smmu->num_mapping_groups - 1);
270
271 /*
272 * With some firmware versions writes to S2CR of type FAULT are
273 * ignored, and writing BYPASS will end up written as FAULT in the
274 * register. Perform a write to S2CR to detect if this is the case and
275 * if so reserve a context bank to emulate bypass streams.
276 */
277 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, S2CR_TYPE_BYPASS) |
278 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, 0xff) |
279 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, S2CR_PRIVCFG_DEFAULT);
280 arm_smmu_gr0_write(smmu, last_s2cr, reg);
281 reg = arm_smmu_gr0_read(smmu, last_s2cr);
282 if (FIELD_GET(ARM_SMMU_S2CR_TYPE, reg) != S2CR_TYPE_BYPASS) {
283 qsmmu->bypass_quirk = true;
284 qsmmu->bypass_cbndx = smmu->num_context_banks - 1;
285
286 set_bit(qsmmu->bypass_cbndx, smmu->context_map);
287
288 arm_smmu_cb_write(smmu, qsmmu->bypass_cbndx, ARM_SMMU_CB_SCTLR, 0);
289
290 reg = FIELD_PREP(ARM_SMMU_CBAR_TYPE, CBAR_TYPE_S1_TRANS_S2_BYPASS);
291 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(qsmmu->bypass_cbndx), reg);
292 }
293
294 for (i = 0; i < smmu->num_mapping_groups; i++) {
295 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
296
297 if (FIELD_GET(ARM_SMMU_SMR_VALID, smr)) {
298 /* Ignore valid bit for SMR mask extraction. */
299 smr &= ~ARM_SMMU_SMR_VALID;
300 smmu->smrs[i].id = FIELD_GET(ARM_SMMU_SMR_ID, smr);
301 smmu->smrs[i].mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
302 smmu->smrs[i].valid = true;
303
304 smmu->s2crs[i].type = S2CR_TYPE_BYPASS;
305 smmu->s2crs[i].privcfg = S2CR_PRIVCFG_DEFAULT;
306 smmu->s2crs[i].cbndx = 0xff;
307 }
308 }
309
310 return 0;
311 }
312
qcom_smmu_write_s2cr(struct arm_smmu_device * smmu,int idx)313 static void qcom_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
314 {
315 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
316 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
317 u32 cbndx = s2cr->cbndx;
318 u32 type = s2cr->type;
319 u32 reg;
320
321 if (qsmmu->bypass_quirk) {
322 if (type == S2CR_TYPE_BYPASS) {
323 /*
324 * Firmware with quirky S2CR handling will substitute
325 * BYPASS writes with FAULT, so point the stream to the
326 * reserved context bank and ask for translation on the
327 * stream
328 */
329 type = S2CR_TYPE_TRANS;
330 cbndx = qsmmu->bypass_cbndx;
331 } else if (type == S2CR_TYPE_FAULT) {
332 /*
333 * Firmware with quirky S2CR handling will ignore FAULT
334 * writes, so trick it to write FAULT by asking for a
335 * BYPASS.
336 */
337 type = S2CR_TYPE_BYPASS;
338 cbndx = 0xff;
339 }
340 }
341
342 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, type) |
343 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, cbndx) |
344 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, s2cr->privcfg);
345 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
346 }
347
qcom_smmu_def_domain_type(struct device * dev)348 static int qcom_smmu_def_domain_type(struct device *dev)
349 {
350 const struct of_device_id *match =
351 of_match_device(qcom_smmu_client_of_match, dev);
352
353 return match ? IOMMU_DOMAIN_IDENTITY : 0;
354 }
355
qcom_sdm845_smmu500_reset(struct arm_smmu_device * smmu)356 static int qcom_sdm845_smmu500_reset(struct arm_smmu_device *smmu)
357 {
358 int ret;
359
360 /*
361 * To address performance degradation in non-real time clients,
362 * such as USB and UFS, turn off wait-for-safe on sdm845 based boards,
363 * such as MTP and db845, whose firmwares implement secure monitor
364 * call handlers to turn on/off the wait-for-safe logic.
365 */
366 ret = qcom_scm_qsmmu500_wait_safe_toggle(0);
367 if (ret)
368 dev_warn(smmu->dev, "Failed to turn off SAFE logic\n");
369
370 return ret;
371 }
372
qcom_smmu500_reset(struct arm_smmu_device * smmu)373 static int qcom_smmu500_reset(struct arm_smmu_device *smmu)
374 {
375 const struct device_node *np = smmu->dev->of_node;
376
377 arm_mmu500_reset(smmu);
378
379 if (of_device_is_compatible(np, "qcom,sdm845-smmu-500"))
380 return qcom_sdm845_smmu500_reset(smmu);
381
382 return 0;
383 }
384
385 static const struct arm_smmu_impl qcom_smmu_impl = {
386 .init_context = qcom_smmu_init_context,
387 .cfg_probe = qcom_smmu_cfg_probe,
388 .def_domain_type = qcom_smmu_def_domain_type,
389 .reset = qcom_smmu500_reset,
390 .write_s2cr = qcom_smmu_write_s2cr,
391 };
392
393 static const struct arm_smmu_impl qcom_adreno_smmu_impl = {
394 .init_context = qcom_adreno_smmu_init_context,
395 .def_domain_type = qcom_smmu_def_domain_type,
396 .reset = qcom_smmu500_reset,
397 .alloc_context_bank = qcom_adreno_smmu_alloc_context_bank,
398 .write_sctlr = qcom_adreno_smmu_write_sctlr,
399 };
400
qcom_smmu_create(struct arm_smmu_device * smmu,const struct arm_smmu_impl * impl)401 static struct arm_smmu_device *qcom_smmu_create(struct arm_smmu_device *smmu,
402 const struct arm_smmu_impl *impl)
403 {
404 struct qcom_smmu *qsmmu;
405
406 /* Check to make sure qcom_scm has finished probing */
407 if (!qcom_scm_is_available())
408 return ERR_PTR(-EPROBE_DEFER);
409
410 qsmmu = devm_krealloc(smmu->dev, smmu, sizeof(*qsmmu), GFP_KERNEL);
411 if (!qsmmu)
412 return ERR_PTR(-ENOMEM);
413
414 qsmmu->smmu.impl = impl;
415
416 return &qsmmu->smmu;
417 }
418
419 static const struct of_device_id __maybe_unused qcom_smmu_impl_of_match[] = {
420 { .compatible = "qcom,msm8998-smmu-v2" },
421 { .compatible = "qcom,sc7180-smmu-500" },
422 { .compatible = "qcom,sc7280-smmu-500" },
423 { .compatible = "qcom,sc8180x-smmu-500" },
424 { .compatible = "qcom,sdm630-smmu-v2" },
425 { .compatible = "qcom,sdm845-smmu-500" },
426 { .compatible = "qcom,sm6125-smmu-500" },
427 { .compatible = "qcom,sm8150-smmu-500" },
428 { .compatible = "qcom,sm8250-smmu-500" },
429 { .compatible = "qcom,sm8350-smmu-500" },
430 { }
431 };
432
433 #ifdef CONFIG_ACPI
434 static struct acpi_platform_list qcom_acpi_platlist[] = {
435 { "LENOVO", "CB-01 ", 0x8180, ACPI_SIG_IORT, equal, "QCOM SMMU" },
436 { "QCOM ", "QCOMEDK2", 0x8180, ACPI_SIG_IORT, equal, "QCOM SMMU" },
437 { }
438 };
439 #endif
440
qcom_smmu_impl_init(struct arm_smmu_device * smmu)441 struct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu)
442 {
443 const struct device_node *np = smmu->dev->of_node;
444
445 #ifdef CONFIG_ACPI
446 if (np == NULL) {
447 /* Match platform for ACPI boot */
448 if (acpi_match_platform_list(qcom_acpi_platlist) >= 0)
449 return qcom_smmu_create(smmu, &qcom_smmu_impl);
450 }
451 #endif
452
453 /*
454 * Do not change this order of implementation, i.e., first adreno
455 * smmu impl and then apss smmu since we can have both implementing
456 * arm,mmu-500 in which case we will miss setting adreno smmu specific
457 * features if the order is changed.
458 */
459 if (of_device_is_compatible(np, "qcom,adreno-smmu"))
460 return qcom_smmu_create(smmu, &qcom_adreno_smmu_impl);
461
462 if (of_match_node(qcom_smmu_impl_of_match, np))
463 return qcom_smmu_create(smmu, &qcom_smmu_impl);
464
465 return smmu;
466 }
467