1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
4 */
5
6 #include <linux/adreno-smmu-priv.h>
7 #include <linux/bitfield.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 };
18
qcom_sdm845_smmu500_cfg_probe(struct arm_smmu_device * smmu)19 static int qcom_sdm845_smmu500_cfg_probe(struct arm_smmu_device *smmu)
20 {
21 u32 s2cr;
22 u32 smr;
23 int i;
24
25 for (i = 0; i < smmu->num_mapping_groups; i++) {
26 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
27 s2cr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_S2CR(i));
28
29 smmu->smrs[i].mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
30 smmu->smrs[i].id = FIELD_GET(ARM_SMMU_SMR_ID, smr);
31 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
32 smmu->smrs[i].valid = FIELD_GET(
33 ARM_SMMU_S2CR_EXIDVALID,
34 s2cr);
35 else
36 smmu->smrs[i].valid = FIELD_GET(
37 ARM_SMMU_SMR_VALID,
38 smr);
39
40 smmu->s2crs[i].group = NULL;
41 smmu->s2crs[i].count = 0;
42 smmu->s2crs[i].type = FIELD_GET(ARM_SMMU_S2CR_TYPE, s2cr);
43 smmu->s2crs[i].privcfg = FIELD_GET(ARM_SMMU_S2CR_PRIVCFG, s2cr);
44 smmu->s2crs[i].cbndx = FIELD_GET(ARM_SMMU_S2CR_CBNDX, s2cr);
45
46 if (!smmu->smrs[i].valid)
47 continue;
48
49 smmu->s2crs[i].pinned = true;
50 bitmap_set(smmu->context_map, smmu->s2crs[i].cbndx, 1);
51 }
52
53 return 0;
54 }
55
56 #define QCOM_ADRENO_SMMU_GPU_SID 0
57
qcom_adreno_smmu_is_gpu_device(struct device * dev)58 static bool qcom_adreno_smmu_is_gpu_device(struct device *dev)
59 {
60 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
61 int i;
62
63 /*
64 * The GPU will always use SID 0 so that is a handy way to uniquely
65 * identify it and configure it for per-instance pagetables
66 */
67 for (i = 0; i < fwspec->num_ids; i++) {
68 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
69
70 if (sid == QCOM_ADRENO_SMMU_GPU_SID)
71 return true;
72 }
73
74 return false;
75 }
76
qcom_adreno_smmu_get_ttbr1_cfg(const void * cookie)77 static const struct io_pgtable_cfg *qcom_adreno_smmu_get_ttbr1_cfg(
78 const void *cookie)
79 {
80 struct arm_smmu_domain *smmu_domain = (void *)cookie;
81 struct io_pgtable *pgtable =
82 io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops);
83 return &pgtable->cfg;
84 }
85
86 /*
87 * Local implementation to configure TTBR0 with the specified pagetable config.
88 * The GPU driver will call this to enable TTBR0 when per-instance pagetables
89 * are active
90 */
91
qcom_adreno_smmu_set_ttbr0_cfg(const void * cookie,const struct io_pgtable_cfg * pgtbl_cfg)92 static int qcom_adreno_smmu_set_ttbr0_cfg(const void *cookie,
93 const struct io_pgtable_cfg *pgtbl_cfg)
94 {
95 struct arm_smmu_domain *smmu_domain = (void *)cookie;
96 struct io_pgtable *pgtable = io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops);
97 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
98 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
99
100 /* The domain must have split pagetables already enabled */
101 if (cb->tcr[0] & ARM_SMMU_TCR_EPD1)
102 return -EINVAL;
103
104 /* If the pagetable config is NULL, disable TTBR0 */
105 if (!pgtbl_cfg) {
106 /* Do nothing if it is already disabled */
107 if ((cb->tcr[0] & ARM_SMMU_TCR_EPD0))
108 return -EINVAL;
109
110 /* Set TCR to the original configuration */
111 cb->tcr[0] = arm_smmu_lpae_tcr(&pgtable->cfg);
112 cb->ttbr[0] = FIELD_PREP(ARM_SMMU_TTBRn_ASID, cb->cfg->asid);
113 } else {
114 u32 tcr = cb->tcr[0];
115
116 /* Don't call this again if TTBR0 is already enabled */
117 if (!(cb->tcr[0] & ARM_SMMU_TCR_EPD0))
118 return -EINVAL;
119
120 tcr |= arm_smmu_lpae_tcr(pgtbl_cfg);
121 tcr &= ~(ARM_SMMU_TCR_EPD0 | ARM_SMMU_TCR_EPD1);
122
123 cb->tcr[0] = tcr;
124 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
125 cb->ttbr[0] |= FIELD_PREP(ARM_SMMU_TTBRn_ASID, cb->cfg->asid);
126 }
127
128 arm_smmu_write_context_bank(smmu_domain->smmu, cb->cfg->cbndx);
129
130 return 0;
131 }
132
qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain * smmu_domain,struct arm_smmu_device * smmu,struct device * dev,int start)133 static int qcom_adreno_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,
134 struct arm_smmu_device *smmu,
135 struct device *dev, int start)
136 {
137 int count;
138
139 /*
140 * Assign context bank 0 to the GPU device so the GPU hardware can
141 * switch pagetables
142 */
143 if (qcom_adreno_smmu_is_gpu_device(dev)) {
144 start = 0;
145 count = 1;
146 } else {
147 start = 1;
148 count = smmu->num_context_banks;
149 }
150
151 return __arm_smmu_alloc_bitmap(smmu->context_map, start, count);
152 }
153
qcom_adreno_smmu_init_context(struct arm_smmu_domain * smmu_domain,struct io_pgtable_cfg * pgtbl_cfg,struct device * dev)154 static int qcom_adreno_smmu_init_context(struct arm_smmu_domain *smmu_domain,
155 struct io_pgtable_cfg *pgtbl_cfg, struct device *dev)
156 {
157 struct adreno_smmu_priv *priv;
158
159 /* Only enable split pagetables for the GPU device (SID 0) */
160 if (!qcom_adreno_smmu_is_gpu_device(dev))
161 return 0;
162
163 /*
164 * All targets that use the qcom,adreno-smmu compatible string *should*
165 * be AARCH64 stage 1 but double check because the arm-smmu code assumes
166 * that is the case when the TTBR1 quirk is enabled
167 */
168 if ((smmu_domain->stage == ARM_SMMU_DOMAIN_S1) &&
169 (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64))
170 pgtbl_cfg->quirks |= IO_PGTABLE_QUIRK_ARM_TTBR1;
171
172 /*
173 * Initialize private interface with GPU:
174 */
175
176 priv = dev_get_drvdata(dev);
177 priv->cookie = smmu_domain;
178 priv->get_ttbr1_cfg = qcom_adreno_smmu_get_ttbr1_cfg;
179 priv->set_ttbr0_cfg = qcom_adreno_smmu_set_ttbr0_cfg;
180
181 return 0;
182 }
183
to_qcom_smmu(struct arm_smmu_device * smmu)184 static struct qcom_smmu *to_qcom_smmu(struct arm_smmu_device *smmu)
185 {
186 return container_of(smmu, struct qcom_smmu, smmu);
187 }
188
189 static const struct of_device_id qcom_smmu_client_of_match[] __maybe_unused = {
190 { .compatible = "qcom,adreno" },
191 { .compatible = "qcom,adreno-gmu" },
192 { .compatible = "qcom,mdp4" },
193 { .compatible = "qcom,mdss" },
194 { .compatible = "qcom,sc7180-mdss" },
195 { .compatible = "qcom,sc7180-mss-pil" },
196 { .compatible = "qcom,sdm845-mdss" },
197 { .compatible = "qcom,sdm845-mss-pil" },
198 { }
199 };
200
qcom_smmu_cfg_probe(struct arm_smmu_device * smmu)201 static int qcom_smmu_cfg_probe(struct arm_smmu_device *smmu)
202 {
203 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
204 unsigned int last_s2cr;
205 u32 reg;
206 u32 smr;
207 int i;
208
209 /*
210 * Some platforms support more than the Arm SMMU architected maximum of
211 * 128 stream matching groups. For unknown reasons, the additional
212 * groups don't exhibit the same behavior as the architected registers,
213 * so limit the groups to 128 until the behavior is fixed for the other
214 * groups.
215 */
216 if (smmu->num_mapping_groups > 128) {
217 dev_notice(smmu->dev, "\tLimiting the stream matching groups to 128\n");
218 smmu->num_mapping_groups = 128;
219 }
220
221 last_s2cr = ARM_SMMU_GR0_S2CR(smmu->num_mapping_groups - 1);
222
223 /*
224 * With some firmware versions writes to S2CR of type FAULT are
225 * ignored, and writing BYPASS will end up written as FAULT in the
226 * register. Perform a write to S2CR to detect if this is the case and
227 * if so reserve a context bank to emulate bypass streams.
228 */
229 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, S2CR_TYPE_BYPASS) |
230 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, 0xff) |
231 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, S2CR_PRIVCFG_DEFAULT);
232 arm_smmu_gr0_write(smmu, last_s2cr, reg);
233 reg = arm_smmu_gr0_read(smmu, last_s2cr);
234 if (FIELD_GET(ARM_SMMU_S2CR_TYPE, reg) != S2CR_TYPE_BYPASS) {
235 qsmmu->bypass_quirk = true;
236 qsmmu->bypass_cbndx = smmu->num_context_banks - 1;
237
238 set_bit(qsmmu->bypass_cbndx, smmu->context_map);
239
240 arm_smmu_cb_write(smmu, qsmmu->bypass_cbndx, ARM_SMMU_CB_SCTLR, 0);
241
242 reg = FIELD_PREP(ARM_SMMU_CBAR_TYPE, CBAR_TYPE_S1_TRANS_S2_BYPASS);
243 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(qsmmu->bypass_cbndx), reg);
244 }
245
246 for (i = 0; i < smmu->num_mapping_groups; i++) {
247 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
248
249 if (FIELD_GET(ARM_SMMU_SMR_VALID, smr)) {
250 /* Ignore valid bit for SMR mask extraction. */
251 smr &= ~ARM_SMMU_SMR_VALID;
252 smmu->smrs[i].id = FIELD_GET(ARM_SMMU_SMR_ID, smr);
253 smmu->smrs[i].mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
254 smmu->smrs[i].valid = true;
255
256 smmu->s2crs[i].type = S2CR_TYPE_BYPASS;
257 smmu->s2crs[i].privcfg = S2CR_PRIVCFG_DEFAULT;
258 smmu->s2crs[i].cbndx = 0xff;
259 }
260 }
261
262 return 0;
263 }
264
qcom_smmu_write_s2cr(struct arm_smmu_device * smmu,int idx)265 static void qcom_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
266 {
267 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
268 struct qcom_smmu *qsmmu = to_qcom_smmu(smmu);
269 u32 cbndx = s2cr->cbndx;
270 u32 type = s2cr->type;
271 u32 reg;
272
273 if (qsmmu->bypass_quirk) {
274 if (type == S2CR_TYPE_BYPASS) {
275 /*
276 * Firmware with quirky S2CR handling will substitute
277 * BYPASS writes with FAULT, so point the stream to the
278 * reserved context bank and ask for translation on the
279 * stream
280 */
281 type = S2CR_TYPE_TRANS;
282 cbndx = qsmmu->bypass_cbndx;
283 } else if (type == S2CR_TYPE_FAULT) {
284 /*
285 * Firmware with quirky S2CR handling will ignore FAULT
286 * writes, so trick it to write FAULT by asking for a
287 * BYPASS.
288 */
289 type = S2CR_TYPE_BYPASS;
290 cbndx = 0xff;
291 }
292 }
293
294 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, type) |
295 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, cbndx) |
296 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, s2cr->privcfg);
297 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
298 }
299
qcom_smmu_def_domain_type(struct device * dev)300 static int qcom_smmu_def_domain_type(struct device *dev)
301 {
302 const struct of_device_id *match =
303 of_match_device(qcom_smmu_client_of_match, dev);
304
305 return match ? IOMMU_DOMAIN_IDENTITY : 0;
306 }
307
qcom_sdm845_smmu500_reset(struct arm_smmu_device * smmu)308 static int qcom_sdm845_smmu500_reset(struct arm_smmu_device *smmu)
309 {
310 int ret;
311
312 /*
313 * To address performance degradation in non-real time clients,
314 * such as USB and UFS, turn off wait-for-safe on sdm845 based boards,
315 * such as MTP and db845, whose firmwares implement secure monitor
316 * call handlers to turn on/off the wait-for-safe logic.
317 */
318 ret = qcom_scm_qsmmu500_wait_safe_toggle(0);
319 if (ret)
320 dev_warn(smmu->dev, "Failed to turn off SAFE logic\n");
321
322 return ret;
323 }
324
qcom_smmu500_reset(struct arm_smmu_device * smmu)325 static int qcom_smmu500_reset(struct arm_smmu_device *smmu)
326 {
327 const struct device_node *np = smmu->dev->of_node;
328
329 arm_mmu500_reset(smmu);
330
331 if (of_device_is_compatible(np, "qcom,sdm845-smmu-500"))
332 return qcom_sdm845_smmu500_reset(smmu);
333
334 return 0;
335 }
336
337 static const struct arm_smmu_impl qcom_smmu_impl = {
338 .cfg_probe = qcom_smmu_cfg_probe,
339 .def_domain_type = qcom_smmu_def_domain_type,
340 .cfg_probe = qcom_sdm845_smmu500_cfg_probe,
341 .reset = qcom_smmu500_reset,
342 .write_s2cr = qcom_smmu_write_s2cr,
343 };
344
345 static const struct arm_smmu_impl qcom_adreno_smmu_impl = {
346 .init_context = qcom_adreno_smmu_init_context,
347 .def_domain_type = qcom_smmu_def_domain_type,
348 .reset = qcom_smmu500_reset,
349 .alloc_context_bank = qcom_adreno_smmu_alloc_context_bank,
350 };
351
qcom_smmu_create(struct arm_smmu_device * smmu,const struct arm_smmu_impl * impl)352 static struct arm_smmu_device *qcom_smmu_create(struct arm_smmu_device *smmu,
353 const struct arm_smmu_impl *impl)
354 {
355 struct qcom_smmu *qsmmu;
356
357 /* Check to make sure qcom_scm has finished probing */
358 if (!qcom_scm_is_available())
359 return ERR_PTR(-EPROBE_DEFER);
360
361 qsmmu = devm_kzalloc(smmu->dev, sizeof(*qsmmu), GFP_KERNEL);
362 if (!qsmmu)
363 return ERR_PTR(-ENOMEM);
364
365 qsmmu->smmu = *smmu;
366
367 qsmmu->smmu.impl = impl;
368 devm_kfree(smmu->dev, smmu);
369
370 return &qsmmu->smmu;
371 }
372
qcom_smmu_impl_init(struct arm_smmu_device * smmu)373 struct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu)
374 {
375 return qcom_smmu_create(smmu, &qcom_smmu_impl);
376 }
377
qcom_adreno_smmu_impl_init(struct arm_smmu_device * smmu)378 struct arm_smmu_device *qcom_adreno_smmu_impl_init(struct arm_smmu_device *smmu)
379 {
380 return qcom_smmu_create(smmu, &qcom_adreno_smmu_impl);
381 }
382