xref: /linux/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c (revision 189f164e573e18d9f8876dbd3ad8fcbe11f93037)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * IOMMU API for ARM architected SMMUv3 implementations.
4  *
5  * Copyright (C) 2015 ARM Limited
6  *
7  * Author: Will Deacon <will.deacon@arm.com>
8  *
9  * This driver is powered by bad coffee and bombay mix.
10  */
11 
12 #include <linux/acpi.h>
13 #include <linux/acpi_iort.h>
14 #include <linux/bitops.h>
15 #include <linux/crash_dump.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
19 #include <linux/io-pgtable.h>
20 #include <linux/iopoll.h>
21 #include <linux/module.h>
22 #include <linux/msi.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_platform.h>
26 #include <linux/pci.h>
27 #include <linux/pci-ats.h>
28 #include <linux/platform_device.h>
29 #include <linux/string_choices.h>
30 #include <kunit/visibility.h>
31 #include <uapi/linux/iommufd.h>
32 
33 #include "arm-smmu-v3.h"
34 #include "../../dma-iommu.h"
35 
36 static bool disable_msipolling;
37 module_param(disable_msipolling, bool, 0444);
38 MODULE_PARM_DESC(disable_msipolling,
39 	"Disable MSI-based polling for CMD_SYNC completion.");
40 
41 static const struct iommu_ops arm_smmu_ops;
42 static struct iommu_dirty_ops arm_smmu_dirty_ops;
43 
44 enum arm_smmu_msi_index {
45 	EVTQ_MSI_INDEX,
46 	GERROR_MSI_INDEX,
47 	PRIQ_MSI_INDEX,
48 	ARM_SMMU_MAX_MSIS,
49 };
50 
51 #define NUM_ENTRY_QWORDS 8
52 static_assert(sizeof(struct arm_smmu_ste) == NUM_ENTRY_QWORDS * sizeof(u64));
53 static_assert(sizeof(struct arm_smmu_cd) == NUM_ENTRY_QWORDS * sizeof(u64));
54 
55 static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {
56 	[EVTQ_MSI_INDEX] = {
57 		ARM_SMMU_EVTQ_IRQ_CFG0,
58 		ARM_SMMU_EVTQ_IRQ_CFG1,
59 		ARM_SMMU_EVTQ_IRQ_CFG2,
60 	},
61 	[GERROR_MSI_INDEX] = {
62 		ARM_SMMU_GERROR_IRQ_CFG0,
63 		ARM_SMMU_GERROR_IRQ_CFG1,
64 		ARM_SMMU_GERROR_IRQ_CFG2,
65 	},
66 	[PRIQ_MSI_INDEX] = {
67 		ARM_SMMU_PRIQ_IRQ_CFG0,
68 		ARM_SMMU_PRIQ_IRQ_CFG1,
69 		ARM_SMMU_PRIQ_IRQ_CFG2,
70 	},
71 };
72 
73 struct arm_smmu_option_prop {
74 	u32 opt;
75 	const char *prop;
76 };
77 
78 DEFINE_XARRAY_ALLOC1(arm_smmu_asid_xa);
79 DEFINE_MUTEX(arm_smmu_asid_lock);
80 
81 static struct arm_smmu_option_prop arm_smmu_options[] = {
82 	{ ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
83 	{ ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"},
84 	{ 0, NULL},
85 };
86 
87 static const char * const event_str[] = {
88 	[EVT_ID_BAD_STREAMID_CONFIG] = "C_BAD_STREAMID",
89 	[EVT_ID_STE_FETCH_FAULT] = "F_STE_FETCH",
90 	[EVT_ID_BAD_STE_CONFIG] = "C_BAD_STE",
91 	[EVT_ID_STREAM_DISABLED_FAULT] = "F_STREAM_DISABLED",
92 	[EVT_ID_BAD_SUBSTREAMID_CONFIG] = "C_BAD_SUBSTREAMID",
93 	[EVT_ID_CD_FETCH_FAULT] = "F_CD_FETCH",
94 	[EVT_ID_BAD_CD_CONFIG] = "C_BAD_CD",
95 	[EVT_ID_TRANSLATION_FAULT] = "F_TRANSLATION",
96 	[EVT_ID_ADDR_SIZE_FAULT] = "F_ADDR_SIZE",
97 	[EVT_ID_ACCESS_FAULT] = "F_ACCESS",
98 	[EVT_ID_PERMISSION_FAULT] = "F_PERMISSION",
99 	[EVT_ID_VMS_FETCH_FAULT] = "F_VMS_FETCH",
100 };
101 
102 static const char * const event_class_str[] = {
103 	[0] = "CD fetch",
104 	[1] = "Stage 1 translation table fetch",
105 	[2] = "Input address caused fault",
106 	[3] = "Reserved",
107 };
108 
109 static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master);
110 
parse_driver_options(struct arm_smmu_device * smmu)111 static void parse_driver_options(struct arm_smmu_device *smmu)
112 {
113 	int i = 0;
114 
115 	do {
116 		if (of_property_read_bool(smmu->dev->of_node,
117 						arm_smmu_options[i].prop)) {
118 			smmu->options |= arm_smmu_options[i].opt;
119 			dev_notice(smmu->dev, "option %s\n",
120 				arm_smmu_options[i].prop);
121 		}
122 	} while (arm_smmu_options[++i].opt);
123 }
124 
125 /* Low-level queue manipulation functions */
queue_has_space(struct arm_smmu_ll_queue * q,u32 n)126 static bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
127 {
128 	u32 space, prod, cons;
129 
130 	prod = Q_IDX(q, q->prod);
131 	cons = Q_IDX(q, q->cons);
132 
133 	if (Q_WRP(q, q->prod) == Q_WRP(q, q->cons))
134 		space = (1 << q->max_n_shift) - (prod - cons);
135 	else
136 		space = cons - prod;
137 
138 	return space >= n;
139 }
140 
queue_full(struct arm_smmu_ll_queue * q)141 static bool queue_full(struct arm_smmu_ll_queue *q)
142 {
143 	return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
144 	       Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
145 }
146 
queue_empty(struct arm_smmu_ll_queue * q)147 static bool queue_empty(struct arm_smmu_ll_queue *q)
148 {
149 	return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
150 	       Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
151 }
152 
queue_consumed(struct arm_smmu_ll_queue * q,u32 prod)153 static bool queue_consumed(struct arm_smmu_ll_queue *q, u32 prod)
154 {
155 	return ((Q_WRP(q, q->cons) == Q_WRP(q, prod)) &&
156 		(Q_IDX(q, q->cons) > Q_IDX(q, prod))) ||
157 	       ((Q_WRP(q, q->cons) != Q_WRP(q, prod)) &&
158 		(Q_IDX(q, q->cons) <= Q_IDX(q, prod)));
159 }
160 
queue_sync_cons_out(struct arm_smmu_queue * q)161 static void queue_sync_cons_out(struct arm_smmu_queue *q)
162 {
163 	/*
164 	 * Ensure that all CPU accesses (reads and writes) to the queue
165 	 * are complete before we update the cons pointer.
166 	 */
167 	__iomb();
168 	writel_relaxed(q->llq.cons, q->cons_reg);
169 }
170 
queue_inc_cons(struct arm_smmu_ll_queue * q)171 static void queue_inc_cons(struct arm_smmu_ll_queue *q)
172 {
173 	u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
174 	q->cons = Q_OVF(q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
175 }
176 
queue_sync_cons_ovf(struct arm_smmu_queue * q)177 static void queue_sync_cons_ovf(struct arm_smmu_queue *q)
178 {
179 	struct arm_smmu_ll_queue *llq = &q->llq;
180 
181 	if (likely(Q_OVF(llq->prod) == Q_OVF(llq->cons)))
182 		return;
183 
184 	llq->cons = Q_OVF(llq->prod) | Q_WRP(llq, llq->cons) |
185 		      Q_IDX(llq, llq->cons);
186 	queue_sync_cons_out(q);
187 }
188 
queue_sync_prod_in(struct arm_smmu_queue * q)189 static int queue_sync_prod_in(struct arm_smmu_queue *q)
190 {
191 	u32 prod;
192 	int ret = 0;
193 
194 	/*
195 	 * We can't use the _relaxed() variant here, as we must prevent
196 	 * speculative reads of the queue before we have determined that
197 	 * prod has indeed moved.
198 	 */
199 	prod = readl(q->prod_reg);
200 
201 	if (Q_OVF(prod) != Q_OVF(q->llq.prod))
202 		ret = -EOVERFLOW;
203 
204 	q->llq.prod = prod;
205 	return ret;
206 }
207 
queue_inc_prod_n(struct arm_smmu_ll_queue * q,int n)208 static u32 queue_inc_prod_n(struct arm_smmu_ll_queue *q, int n)
209 {
210 	u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + n;
211 	return Q_OVF(q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
212 }
213 
queue_poll_init(struct arm_smmu_device * smmu,struct arm_smmu_queue_poll * qp)214 static void queue_poll_init(struct arm_smmu_device *smmu,
215 			    struct arm_smmu_queue_poll *qp)
216 {
217 	qp->delay = 1;
218 	qp->spin_cnt = 0;
219 	qp->wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
220 	qp->timeout = ktime_add_us(ktime_get(), ARM_SMMU_POLL_TIMEOUT_US);
221 }
222 
queue_poll(struct arm_smmu_queue_poll * qp)223 static int queue_poll(struct arm_smmu_queue_poll *qp)
224 {
225 	if (ktime_compare(ktime_get(), qp->timeout) > 0)
226 		return -ETIMEDOUT;
227 
228 	if (qp->wfe) {
229 		wfe();
230 	} else if (++qp->spin_cnt < ARM_SMMU_POLL_SPIN_COUNT) {
231 		cpu_relax();
232 	} else {
233 		udelay(qp->delay);
234 		qp->delay *= 2;
235 		qp->spin_cnt = 0;
236 	}
237 
238 	return 0;
239 }
240 
queue_write(__le64 * dst,u64 * src,size_t n_dwords)241 static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
242 {
243 	int i;
244 
245 	for (i = 0; i < n_dwords; ++i)
246 		*dst++ = cpu_to_le64(*src++);
247 }
248 
queue_read(u64 * dst,__le64 * src,size_t n_dwords)249 static void queue_read(u64 *dst, __le64 *src, size_t n_dwords)
250 {
251 	int i;
252 
253 	for (i = 0; i < n_dwords; ++i)
254 		*dst++ = le64_to_cpu(*src++);
255 }
256 
queue_remove_raw(struct arm_smmu_queue * q,u64 * ent)257 static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent)
258 {
259 	if (queue_empty(&q->llq))
260 		return -EAGAIN;
261 
262 	queue_read(ent, Q_ENT(q, q->llq.cons), q->ent_dwords);
263 	queue_inc_cons(&q->llq);
264 	queue_sync_cons_out(q);
265 	return 0;
266 }
267 
268 /* High-level queue accessors */
arm_smmu_cmdq_build_cmd(u64 * cmd,struct arm_smmu_cmdq_ent * ent)269 static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
270 {
271 	memset(cmd, 0, 1 << CMDQ_ENT_SZ_SHIFT);
272 	cmd[0] |= FIELD_PREP(CMDQ_0_OP, ent->opcode);
273 
274 	switch (ent->opcode) {
275 	case CMDQ_OP_TLBI_EL2_ALL:
276 	case CMDQ_OP_TLBI_NSNH_ALL:
277 		break;
278 	case CMDQ_OP_PREFETCH_CFG:
279 		cmd[0] |= FIELD_PREP(CMDQ_PREFETCH_0_SID, ent->prefetch.sid);
280 		break;
281 	case CMDQ_OP_CFGI_CD:
282 		cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SSID, ent->cfgi.ssid);
283 		fallthrough;
284 	case CMDQ_OP_CFGI_STE:
285 		cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, ent->cfgi.sid);
286 		cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_LEAF, ent->cfgi.leaf);
287 		break;
288 	case CMDQ_OP_CFGI_CD_ALL:
289 		cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, ent->cfgi.sid);
290 		break;
291 	case CMDQ_OP_CFGI_ALL:
292 		/* Cover the entire SID range */
293 		cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_RANGE, 31);
294 		break;
295 	case CMDQ_OP_TLBI_NH_VA:
296 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
297 		fallthrough;
298 	case CMDQ_OP_TLBI_EL2_VA:
299 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_NUM, ent->tlbi.num);
300 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_SCALE, ent->tlbi.scale);
301 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid);
302 		cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf);
303 		cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TTL, ent->tlbi.ttl);
304 		cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TG, ent->tlbi.tg);
305 		cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_VA_MASK;
306 		break;
307 	case CMDQ_OP_TLBI_S2_IPA:
308 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_NUM, ent->tlbi.num);
309 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_SCALE, ent->tlbi.scale);
310 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
311 		cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf);
312 		cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TTL, ent->tlbi.ttl);
313 		cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_TG, ent->tlbi.tg);
314 		cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_IPA_MASK;
315 		break;
316 	case CMDQ_OP_TLBI_NH_ASID:
317 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid);
318 		fallthrough;
319 	case CMDQ_OP_TLBI_NH_ALL:
320 	case CMDQ_OP_TLBI_S12_VMALL:
321 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
322 		break;
323 	case CMDQ_OP_TLBI_EL2_ASID:
324 		cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid);
325 		break;
326 	case CMDQ_OP_ATC_INV:
327 		cmd[0] |= FIELD_PREP(CMDQ_0_SSV, ent->substream_valid);
328 		cmd[0] |= FIELD_PREP(CMDQ_ATC_0_GLOBAL, ent->atc.global);
329 		cmd[0] |= FIELD_PREP(CMDQ_ATC_0_SSID, ent->atc.ssid);
330 		cmd[0] |= FIELD_PREP(CMDQ_ATC_0_SID, ent->atc.sid);
331 		cmd[1] |= FIELD_PREP(CMDQ_ATC_1_SIZE, ent->atc.size);
332 		cmd[1] |= ent->atc.addr & CMDQ_ATC_1_ADDR_MASK;
333 		break;
334 	case CMDQ_OP_PRI_RESP:
335 		cmd[0] |= FIELD_PREP(CMDQ_0_SSV, ent->substream_valid);
336 		cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SSID, ent->pri.ssid);
337 		cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SID, ent->pri.sid);
338 		cmd[1] |= FIELD_PREP(CMDQ_PRI_1_GRPID, ent->pri.grpid);
339 		switch (ent->pri.resp) {
340 		case PRI_RESP_DENY:
341 		case PRI_RESP_FAIL:
342 		case PRI_RESP_SUCC:
343 			break;
344 		default:
345 			return -EINVAL;
346 		}
347 		cmd[1] |= FIELD_PREP(CMDQ_PRI_1_RESP, ent->pri.resp);
348 		break;
349 	case CMDQ_OP_RESUME:
350 		cmd[0] |= FIELD_PREP(CMDQ_RESUME_0_SID, ent->resume.sid);
351 		cmd[0] |= FIELD_PREP(CMDQ_RESUME_0_RESP, ent->resume.resp);
352 		cmd[1] |= FIELD_PREP(CMDQ_RESUME_1_STAG, ent->resume.stag);
353 		break;
354 	case CMDQ_OP_CMD_SYNC:
355 		if (ent->sync.msiaddr) {
356 			cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_IRQ);
357 			cmd[1] |= ent->sync.msiaddr & CMDQ_SYNC_1_MSIADDR_MASK;
358 		} else {
359 			cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV);
360 		}
361 		cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSH, ARM_SMMU_SH_ISH);
362 		cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSIATTR, ARM_SMMU_MEMATTR_OIWB);
363 		break;
364 	default:
365 		return -ENOENT;
366 	}
367 
368 	return 0;
369 }
370 
arm_smmu_get_cmdq(struct arm_smmu_device * smmu,struct arm_smmu_cmdq_ent * ent)371 static struct arm_smmu_cmdq *arm_smmu_get_cmdq(struct arm_smmu_device *smmu,
372 					       struct arm_smmu_cmdq_ent *ent)
373 {
374 	struct arm_smmu_cmdq *cmdq = NULL;
375 
376 	if (smmu->impl_ops && smmu->impl_ops->get_secondary_cmdq)
377 		cmdq = smmu->impl_ops->get_secondary_cmdq(smmu, ent);
378 
379 	return cmdq ?: &smmu->cmdq;
380 }
381 
arm_smmu_cmdq_needs_busy_polling(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq)382 static bool arm_smmu_cmdq_needs_busy_polling(struct arm_smmu_device *smmu,
383 					     struct arm_smmu_cmdq *cmdq)
384 {
385 	if (cmdq == &smmu->cmdq)
386 		return false;
387 
388 	return smmu->options & ARM_SMMU_OPT_TEGRA241_CMDQV;
389 }
390 
arm_smmu_cmdq_build_sync_cmd(u64 * cmd,struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq,u32 prod)391 static void arm_smmu_cmdq_build_sync_cmd(u64 *cmd, struct arm_smmu_device *smmu,
392 					 struct arm_smmu_cmdq *cmdq, u32 prod)
393 {
394 	struct arm_smmu_queue *q = &cmdq->q;
395 	struct arm_smmu_cmdq_ent ent = {
396 		.opcode = CMDQ_OP_CMD_SYNC,
397 	};
398 
399 	/*
400 	 * Beware that Hi16xx adds an extra 32 bits of goodness to its MSI
401 	 * payload, so the write will zero the entire command on that platform.
402 	 */
403 	if (smmu->options & ARM_SMMU_OPT_MSIPOLL) {
404 		ent.sync.msiaddr = q->base_dma + Q_IDX(&q->llq, prod) *
405 				   q->ent_dwords * 8;
406 	}
407 
408 	arm_smmu_cmdq_build_cmd(cmd, &ent);
409 	if (arm_smmu_cmdq_needs_busy_polling(smmu, cmdq))
410 		u64p_replace_bits(cmd, CMDQ_SYNC_0_CS_NONE, CMDQ_SYNC_0_CS);
411 }
412 
__arm_smmu_cmdq_skip_err(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq)413 void __arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu,
414 			      struct arm_smmu_cmdq *cmdq)
415 {
416 	static const char * const cerror_str[] = {
417 		[CMDQ_ERR_CERROR_NONE_IDX]	= "No error",
418 		[CMDQ_ERR_CERROR_ILL_IDX]	= "Illegal command",
419 		[CMDQ_ERR_CERROR_ABT_IDX]	= "Abort on command fetch",
420 		[CMDQ_ERR_CERROR_ATC_INV_IDX]	= "ATC invalidate timeout",
421 	};
422 	struct arm_smmu_queue *q = &cmdq->q;
423 
424 	int i;
425 	u64 cmd[CMDQ_ENT_DWORDS];
426 	u32 cons = readl_relaxed(q->cons_reg);
427 	u32 idx = FIELD_GET(CMDQ_CONS_ERR, cons);
428 	struct arm_smmu_cmdq_ent cmd_sync = {
429 		.opcode = CMDQ_OP_CMD_SYNC,
430 	};
431 
432 	dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons,
433 		idx < ARRAY_SIZE(cerror_str) ?  cerror_str[idx] : "Unknown");
434 
435 	switch (idx) {
436 	case CMDQ_ERR_CERROR_ABT_IDX:
437 		dev_err(smmu->dev, "retrying command fetch\n");
438 		return;
439 	case CMDQ_ERR_CERROR_NONE_IDX:
440 		return;
441 	case CMDQ_ERR_CERROR_ATC_INV_IDX:
442 		/*
443 		 * ATC Invalidation Completion timeout. CONS is still pointing
444 		 * at the CMD_SYNC. Attempt to complete other pending commands
445 		 * by repeating the CMD_SYNC, though we might well end up back
446 		 * here since the ATC invalidation may still be pending.
447 		 */
448 		return;
449 	case CMDQ_ERR_CERROR_ILL_IDX:
450 	default:
451 		break;
452 	}
453 
454 	/*
455 	 * We may have concurrent producers, so we need to be careful
456 	 * not to touch any of the shadow cmdq state.
457 	 */
458 	queue_read(cmd, Q_ENT(q, cons), q->ent_dwords);
459 	dev_err(smmu->dev, "skipping command in error state:\n");
460 	for (i = 0; i < ARRAY_SIZE(cmd); ++i)
461 		dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
462 
463 	/* Convert the erroneous command into a CMD_SYNC */
464 	arm_smmu_cmdq_build_cmd(cmd, &cmd_sync);
465 	if (arm_smmu_cmdq_needs_busy_polling(smmu, cmdq))
466 		u64p_replace_bits(cmd, CMDQ_SYNC_0_CS_NONE, CMDQ_SYNC_0_CS);
467 
468 	queue_write(Q_ENT(q, cons), cmd, q->ent_dwords);
469 }
470 
arm_smmu_cmdq_skip_err(struct arm_smmu_device * smmu)471 static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu)
472 {
473 	__arm_smmu_cmdq_skip_err(smmu, &smmu->cmdq);
474 }
475 
476 /*
477  * Command queue locking.
478  * This is a form of bastardised rwlock with the following major changes:
479  *
480  * - The only LOCK routines are exclusive_trylock() and shared_lock().
481  *   Neither have barrier semantics, and instead provide only a control
482  *   dependency.
483  *
484  * - The UNLOCK routines are supplemented with shared_tryunlock(), which
485  *   fails if the caller appears to be the last lock holder (yes, this is
486  *   racy). All successful UNLOCK routines have RELEASE semantics.
487  */
arm_smmu_cmdq_shared_lock(struct arm_smmu_cmdq * cmdq)488 static void arm_smmu_cmdq_shared_lock(struct arm_smmu_cmdq *cmdq)
489 {
490 	/*
491 	 * When held in exclusive state, the lock counter is set to INT_MIN
492 	 * so these increments won't hurt as the value will remain negative.
493 	 * The increment will also signal the exclusive locker that there are
494 	 * shared waiters.
495 	 */
496 	if (atomic_fetch_inc_relaxed(&cmdq->lock) >= 0)
497 		return;
498 
499 	/*
500 	 * Someone else is holding the lock in exclusive state, so wait
501 	 * for them to finish. Since we already incremented the lock counter,
502 	 * no exclusive lock can be acquired until we finish. We don't need
503 	 * the return value since we only care that the exclusive lock is
504 	 * released (i.e. the lock counter is non-negative).
505 	 * Once the exclusive locker releases the lock, the sign bit will
506 	 * be cleared and our increment will make the lock counter positive,
507 	 * allowing us to proceed.
508 	 */
509 	atomic_cond_read_relaxed(&cmdq->lock, VAL > 0);
510 }
511 
arm_smmu_cmdq_shared_unlock(struct arm_smmu_cmdq * cmdq)512 static void arm_smmu_cmdq_shared_unlock(struct arm_smmu_cmdq *cmdq)
513 {
514 	(void)atomic_dec_return_release(&cmdq->lock);
515 }
516 
arm_smmu_cmdq_shared_tryunlock(struct arm_smmu_cmdq * cmdq)517 static bool arm_smmu_cmdq_shared_tryunlock(struct arm_smmu_cmdq *cmdq)
518 {
519 	if (atomic_read(&cmdq->lock) == 1)
520 		return false;
521 
522 	arm_smmu_cmdq_shared_unlock(cmdq);
523 	return true;
524 }
525 
526 #define arm_smmu_cmdq_exclusive_trylock_irqsave(cmdq, flags)		\
527 ({									\
528 	bool __ret;							\
529 	local_irq_save(flags);						\
530 	__ret = !atomic_cmpxchg_relaxed(&cmdq->lock, 0, INT_MIN);	\
531 	if (!__ret)							\
532 		local_irq_restore(flags);				\
533 	__ret;								\
534 })
535 
536 /*
537  * Only clear the sign bit when releasing the exclusive lock this will
538  * allow any shared_lock() waiters to proceed without the possibility
539  * of entering the exclusive lock in a tight loop.
540  */
541 #define arm_smmu_cmdq_exclusive_unlock_irqrestore(cmdq, flags)		\
542 ({									\
543 	atomic_fetch_andnot_release(INT_MIN, &cmdq->lock);		\
544 	local_irq_restore(flags);					\
545 })
546 
547 
548 /*
549  * Command queue insertion.
550  * This is made fiddly by our attempts to achieve some sort of scalability
551  * since there is one queue shared amongst all of the CPUs in the system.  If
552  * you like mixed-size concurrency, dependency ordering and relaxed atomics,
553  * then you'll *love* this monstrosity.
554  *
555  * The basic idea is to split the queue up into ranges of commands that are
556  * owned by a given CPU; the owner may not have written all of the commands
557  * itself, but is responsible for advancing the hardware prod pointer when
558  * the time comes. The algorithm is roughly:
559  *
560  * 	1. Allocate some space in the queue. At this point we also discover
561  *	   whether the head of the queue is currently owned by another CPU,
562  *	   or whether we are the owner.
563  *
564  *	2. Write our commands into our allocated slots in the queue.
565  *
566  *	3. Mark our slots as valid in arm_smmu_cmdq.valid_map.
567  *
568  *	4. If we are an owner:
569  *		a. Wait for the previous owner to finish.
570  *		b. Mark the queue head as unowned, which tells us the range
571  *		   that we are responsible for publishing.
572  *		c. Wait for all commands in our owned range to become valid.
573  *		d. Advance the hardware prod pointer.
574  *		e. Tell the next owner we've finished.
575  *
576  *	5. If we are inserting a CMD_SYNC (we may or may not have been an
577  *	   owner), then we need to stick around until it has completed:
578  *		a. If we have MSIs, the SMMU can write back into the CMD_SYNC
579  *		   to clear the first 4 bytes.
580  *		b. Otherwise, we spin waiting for the hardware cons pointer to
581  *		   advance past our command.
582  *
583  * The devil is in the details, particularly the use of locking for handling
584  * SYNC completion and freeing up space in the queue before we think that it is
585  * full.
586  */
__arm_smmu_cmdq_poll_set_valid_map(struct arm_smmu_cmdq * cmdq,u32 sprod,u32 eprod,bool set)587 static void __arm_smmu_cmdq_poll_set_valid_map(struct arm_smmu_cmdq *cmdq,
588 					       u32 sprod, u32 eprod, bool set)
589 {
590 	u32 swidx, sbidx, ewidx, ebidx;
591 	struct arm_smmu_ll_queue llq = {
592 		.max_n_shift	= cmdq->q.llq.max_n_shift,
593 		.prod		= sprod,
594 	};
595 
596 	ewidx = BIT_WORD(Q_IDX(&llq, eprod));
597 	ebidx = Q_IDX(&llq, eprod) % BITS_PER_LONG;
598 
599 	while (llq.prod != eprod) {
600 		unsigned long mask;
601 		atomic_long_t *ptr;
602 		u32 limit = BITS_PER_LONG;
603 
604 		swidx = BIT_WORD(Q_IDX(&llq, llq.prod));
605 		sbidx = Q_IDX(&llq, llq.prod) % BITS_PER_LONG;
606 
607 		ptr = &cmdq->valid_map[swidx];
608 
609 		if ((swidx == ewidx) && (sbidx < ebidx))
610 			limit = ebidx;
611 
612 		mask = GENMASK(limit - 1, sbidx);
613 
614 		/*
615 		 * The valid bit is the inverse of the wrap bit. This means
616 		 * that a zero-initialised queue is invalid and, after marking
617 		 * all entries as valid, they become invalid again when we
618 		 * wrap.
619 		 */
620 		if (set) {
621 			atomic_long_xor(mask, ptr);
622 		} else { /* Poll */
623 			unsigned long valid;
624 
625 			valid = (ULONG_MAX + !!Q_WRP(&llq, llq.prod)) & mask;
626 			atomic_long_cond_read_relaxed(ptr, (VAL & mask) == valid);
627 		}
628 
629 		llq.prod = queue_inc_prod_n(&llq, limit - sbidx);
630 	}
631 }
632 
633 /* Mark all entries in the range [sprod, eprod) as valid */
arm_smmu_cmdq_set_valid_map(struct arm_smmu_cmdq * cmdq,u32 sprod,u32 eprod)634 static void arm_smmu_cmdq_set_valid_map(struct arm_smmu_cmdq *cmdq,
635 					u32 sprod, u32 eprod)
636 {
637 	__arm_smmu_cmdq_poll_set_valid_map(cmdq, sprod, eprod, true);
638 }
639 
640 /* Wait for all entries in the range [sprod, eprod) to become valid */
arm_smmu_cmdq_poll_valid_map(struct arm_smmu_cmdq * cmdq,u32 sprod,u32 eprod)641 static void arm_smmu_cmdq_poll_valid_map(struct arm_smmu_cmdq *cmdq,
642 					 u32 sprod, u32 eprod)
643 {
644 	__arm_smmu_cmdq_poll_set_valid_map(cmdq, sprod, eprod, false);
645 }
646 
647 /* Wait for the command queue to become non-full */
arm_smmu_cmdq_poll_until_not_full(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq,struct arm_smmu_ll_queue * llq)648 static int arm_smmu_cmdq_poll_until_not_full(struct arm_smmu_device *smmu,
649 					     struct arm_smmu_cmdq *cmdq,
650 					     struct arm_smmu_ll_queue *llq)
651 {
652 	unsigned long flags;
653 	struct arm_smmu_queue_poll qp;
654 	int ret = 0;
655 
656 	/*
657 	 * Try to update our copy of cons by grabbing exclusive cmdq access. If
658 	 * that fails, spin until somebody else updates it for us.
659 	 */
660 	if (arm_smmu_cmdq_exclusive_trylock_irqsave(cmdq, flags)) {
661 		WRITE_ONCE(cmdq->q.llq.cons, readl_relaxed(cmdq->q.cons_reg));
662 		arm_smmu_cmdq_exclusive_unlock_irqrestore(cmdq, flags);
663 		llq->val = READ_ONCE(cmdq->q.llq.val);
664 		return 0;
665 	}
666 
667 	queue_poll_init(smmu, &qp);
668 	do {
669 		llq->val = READ_ONCE(cmdq->q.llq.val);
670 		if (!queue_full(llq))
671 			break;
672 
673 		ret = queue_poll(&qp);
674 	} while (!ret);
675 
676 	return ret;
677 }
678 
679 /*
680  * Wait until the SMMU signals a CMD_SYNC completion MSI.
681  * Must be called with the cmdq lock held in some capacity.
682  */
__arm_smmu_cmdq_poll_until_msi(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq,struct arm_smmu_ll_queue * llq)683 static int __arm_smmu_cmdq_poll_until_msi(struct arm_smmu_device *smmu,
684 					  struct arm_smmu_cmdq *cmdq,
685 					  struct arm_smmu_ll_queue *llq)
686 {
687 	int ret = 0;
688 	struct arm_smmu_queue_poll qp;
689 	u32 *cmd = (u32 *)(Q_ENT(&cmdq->q, llq->prod));
690 
691 	queue_poll_init(smmu, &qp);
692 
693 	/*
694 	 * The MSI won't generate an event, since it's being written back
695 	 * into the command queue.
696 	 */
697 	qp.wfe = false;
698 	smp_cond_load_relaxed(cmd, !VAL || (ret = queue_poll(&qp)));
699 	llq->cons = ret ? llq->prod : queue_inc_prod_n(llq, 1);
700 	return ret;
701 }
702 
703 /*
704  * Wait until the SMMU cons index passes llq->prod.
705  * Must be called with the cmdq lock held in some capacity.
706  */
__arm_smmu_cmdq_poll_until_consumed(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq,struct arm_smmu_ll_queue * llq)707 static int __arm_smmu_cmdq_poll_until_consumed(struct arm_smmu_device *smmu,
708 					       struct arm_smmu_cmdq *cmdq,
709 					       struct arm_smmu_ll_queue *llq)
710 {
711 	struct arm_smmu_queue_poll qp;
712 	u32 prod = llq->prod;
713 	int ret = 0;
714 
715 	queue_poll_init(smmu, &qp);
716 	llq->val = READ_ONCE(cmdq->q.llq.val);
717 	do {
718 		if (queue_consumed(llq, prod))
719 			break;
720 
721 		ret = queue_poll(&qp);
722 
723 		/*
724 		 * This needs to be a readl() so that our subsequent call
725 		 * to arm_smmu_cmdq_shared_tryunlock() can fail accurately.
726 		 *
727 		 * Specifically, we need to ensure that we observe all
728 		 * shared_lock()s by other CMD_SYNCs that share our owner,
729 		 * so that a failing call to tryunlock() means that we're
730 		 * the last one out and therefore we can safely advance
731 		 * cmdq->q.llq.cons. Roughly speaking:
732 		 *
733 		 * CPU 0		CPU1			CPU2 (us)
734 		 *
735 		 * if (sync)
736 		 * 	shared_lock();
737 		 *
738 		 * dma_wmb();
739 		 * set_valid_map();
740 		 *
741 		 * 			if (owner) {
742 		 *				poll_valid_map();
743 		 *				<control dependency>
744 		 *				writel(prod_reg);
745 		 *
746 		 *						readl(cons_reg);
747 		 *						tryunlock();
748 		 *
749 		 * Requires us to see CPU 0's shared_lock() acquisition.
750 		 */
751 		llq->cons = readl(cmdq->q.cons_reg);
752 	} while (!ret);
753 
754 	return ret;
755 }
756 
arm_smmu_cmdq_poll_until_sync(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq,struct arm_smmu_ll_queue * llq)757 static int arm_smmu_cmdq_poll_until_sync(struct arm_smmu_device *smmu,
758 					 struct arm_smmu_cmdq *cmdq,
759 					 struct arm_smmu_ll_queue *llq)
760 {
761 	if (smmu->options & ARM_SMMU_OPT_MSIPOLL &&
762 	    !arm_smmu_cmdq_needs_busy_polling(smmu, cmdq))
763 		return __arm_smmu_cmdq_poll_until_msi(smmu, cmdq, llq);
764 
765 	return __arm_smmu_cmdq_poll_until_consumed(smmu, cmdq, llq);
766 }
767 
arm_smmu_cmdq_write_entries(struct arm_smmu_cmdq * cmdq,u64 * cmds,u32 prod,int n)768 static void arm_smmu_cmdq_write_entries(struct arm_smmu_cmdq *cmdq, u64 *cmds,
769 					u32 prod, int n)
770 {
771 	int i;
772 	struct arm_smmu_ll_queue llq = {
773 		.max_n_shift	= cmdq->q.llq.max_n_shift,
774 		.prod		= prod,
775 	};
776 
777 	for (i = 0; i < n; ++i) {
778 		u64 *cmd = &cmds[i * CMDQ_ENT_DWORDS];
779 
780 		prod = queue_inc_prod_n(&llq, i);
781 		queue_write(Q_ENT(&cmdq->q, prod), cmd, CMDQ_ENT_DWORDS);
782 	}
783 }
784 
785 /*
786  * This is the actual insertion function, and provides the following
787  * ordering guarantees to callers:
788  *
789  * - There is a dma_wmb() before publishing any commands to the queue.
790  *   This can be relied upon to order prior writes to data structures
791  *   in memory (such as a CD or an STE) before the command.
792  *
793  * - On completion of a CMD_SYNC, there is a control dependency.
794  *   This can be relied upon to order subsequent writes to memory (e.g.
795  *   freeing an IOVA) after completion of the CMD_SYNC.
796  *
797  * - Command insertion is totally ordered, so if two CPUs each race to
798  *   insert their own list of commands then all of the commands from one
799  *   CPU will appear before any of the commands from the other CPU.
800  */
arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq,u64 * cmds,int n,bool sync)801 int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
802 				struct arm_smmu_cmdq *cmdq, u64 *cmds, int n,
803 				bool sync)
804 {
805 	u64 cmd_sync[CMDQ_ENT_DWORDS];
806 	u32 prod;
807 	unsigned long flags;
808 	bool owner;
809 	struct arm_smmu_ll_queue llq, head;
810 	int ret = 0;
811 
812 	llq.max_n_shift = cmdq->q.llq.max_n_shift;
813 
814 	/* 1. Allocate some space in the queue */
815 	local_irq_save(flags);
816 	llq.val = READ_ONCE(cmdq->q.llq.val);
817 	do {
818 		u64 old;
819 
820 		while (!queue_has_space(&llq, n + sync)) {
821 			local_irq_restore(flags);
822 			if (arm_smmu_cmdq_poll_until_not_full(smmu, cmdq, &llq))
823 				dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
824 			local_irq_save(flags);
825 		}
826 
827 		head.cons = llq.cons;
828 		head.prod = queue_inc_prod_n(&llq, n + sync) |
829 					     CMDQ_PROD_OWNED_FLAG;
830 
831 		old = cmpxchg_relaxed(&cmdq->q.llq.val, llq.val, head.val);
832 		if (old == llq.val)
833 			break;
834 
835 		llq.val = old;
836 	} while (1);
837 	owner = !(llq.prod & CMDQ_PROD_OWNED_FLAG);
838 	head.prod &= ~CMDQ_PROD_OWNED_FLAG;
839 	llq.prod &= ~CMDQ_PROD_OWNED_FLAG;
840 
841 	/*
842 	 * 2. Write our commands into the queue
843 	 * Dependency ordering from the cmpxchg() loop above.
844 	 */
845 	arm_smmu_cmdq_write_entries(cmdq, cmds, llq.prod, n);
846 	if (sync) {
847 		prod = queue_inc_prod_n(&llq, n);
848 		arm_smmu_cmdq_build_sync_cmd(cmd_sync, smmu, cmdq, prod);
849 		queue_write(Q_ENT(&cmdq->q, prod), cmd_sync, CMDQ_ENT_DWORDS);
850 
851 		/*
852 		 * In order to determine completion of our CMD_SYNC, we must
853 		 * ensure that the queue can't wrap twice without us noticing.
854 		 * We achieve that by taking the cmdq lock as shared before
855 		 * marking our slot as valid.
856 		 */
857 		arm_smmu_cmdq_shared_lock(cmdq);
858 	}
859 
860 	/* 3. Mark our slots as valid, ensuring commands are visible first */
861 	dma_wmb();
862 	arm_smmu_cmdq_set_valid_map(cmdq, llq.prod, head.prod);
863 
864 	/* 4. If we are the owner, take control of the SMMU hardware */
865 	if (owner) {
866 		/* a. Wait for previous owner to finish */
867 		atomic_cond_read_relaxed(&cmdq->owner_prod, VAL == llq.prod);
868 
869 		/* b. Stop gathering work by clearing the owned flag */
870 		prod = atomic_fetch_andnot_relaxed(CMDQ_PROD_OWNED_FLAG,
871 						   &cmdq->q.llq.atomic.prod);
872 		prod &= ~CMDQ_PROD_OWNED_FLAG;
873 
874 		/*
875 		 * c. Wait for any gathered work to be written to the queue.
876 		 * Note that we read our own entries so that we have the control
877 		 * dependency required by (d).
878 		 */
879 		arm_smmu_cmdq_poll_valid_map(cmdq, llq.prod, prod);
880 
881 		/*
882 		 * d. Advance the hardware prod pointer
883 		 * Control dependency ordering from the entries becoming valid.
884 		 */
885 		writel_relaxed(prod, cmdq->q.prod_reg);
886 
887 		/*
888 		 * e. Tell the next owner we're done
889 		 * Make sure we've updated the hardware first, so that we don't
890 		 * race to update prod and potentially move it backwards.
891 		 */
892 		atomic_set_release(&cmdq->owner_prod, prod);
893 	}
894 
895 	/* 5. If we are inserting a CMD_SYNC, we must wait for it to complete */
896 	if (sync) {
897 		llq.prod = queue_inc_prod_n(&llq, n);
898 		ret = arm_smmu_cmdq_poll_until_sync(smmu, cmdq, &llq);
899 		if (ret) {
900 			dev_err_ratelimited(smmu->dev,
901 					    "CMD_SYNC timeout at 0x%08x [hwprod 0x%08x, hwcons 0x%08x]\n",
902 					    llq.prod,
903 					    readl_relaxed(cmdq->q.prod_reg),
904 					    readl_relaxed(cmdq->q.cons_reg));
905 		}
906 
907 		/*
908 		 * Try to unlock the cmdq lock. This will fail if we're the last
909 		 * reader, in which case we can safely update cmdq->q.llq.cons
910 		 */
911 		if (!arm_smmu_cmdq_shared_tryunlock(cmdq)) {
912 			WRITE_ONCE(cmdq->q.llq.cons, llq.cons);
913 			arm_smmu_cmdq_shared_unlock(cmdq);
914 		}
915 	}
916 
917 	local_irq_restore(flags);
918 	return ret;
919 }
920 
__arm_smmu_cmdq_issue_cmd(struct arm_smmu_device * smmu,struct arm_smmu_cmdq_ent * ent,bool sync)921 static int __arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
922 				     struct arm_smmu_cmdq_ent *ent,
923 				     bool sync)
924 {
925 	u64 cmd[CMDQ_ENT_DWORDS];
926 
927 	if (unlikely(arm_smmu_cmdq_build_cmd(cmd, ent))) {
928 		dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
929 			 ent->opcode);
930 		return -EINVAL;
931 	}
932 
933 	return arm_smmu_cmdq_issue_cmdlist(
934 		smmu, arm_smmu_get_cmdq(smmu, ent), cmd, 1, sync);
935 }
936 
arm_smmu_cmdq_issue_cmd(struct arm_smmu_device * smmu,struct arm_smmu_cmdq_ent * ent)937 static int arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
938 				   struct arm_smmu_cmdq_ent *ent)
939 {
940 	return __arm_smmu_cmdq_issue_cmd(smmu, ent, false);
941 }
942 
arm_smmu_cmdq_issue_cmd_with_sync(struct arm_smmu_device * smmu,struct arm_smmu_cmdq_ent * ent)943 static int arm_smmu_cmdq_issue_cmd_with_sync(struct arm_smmu_device *smmu,
944 					     struct arm_smmu_cmdq_ent *ent)
945 {
946 	return __arm_smmu_cmdq_issue_cmd(smmu, ent, true);
947 }
948 
arm_smmu_cmdq_batch_init(struct arm_smmu_device * smmu,struct arm_smmu_cmdq_batch * cmds,struct arm_smmu_cmdq_ent * ent)949 static void arm_smmu_cmdq_batch_init(struct arm_smmu_device *smmu,
950 				     struct arm_smmu_cmdq_batch *cmds,
951 				     struct arm_smmu_cmdq_ent *ent)
952 {
953 	cmds->num = 0;
954 	cmds->cmdq = arm_smmu_get_cmdq(smmu, ent);
955 }
956 
arm_smmu_cmdq_batch_add(struct arm_smmu_device * smmu,struct arm_smmu_cmdq_batch * cmds,struct arm_smmu_cmdq_ent * cmd)957 static void arm_smmu_cmdq_batch_add(struct arm_smmu_device *smmu,
958 				    struct arm_smmu_cmdq_batch *cmds,
959 				    struct arm_smmu_cmdq_ent *cmd)
960 {
961 	bool unsupported_cmd = !arm_smmu_cmdq_supports_cmd(cmds->cmdq, cmd);
962 	bool force_sync = (cmds->num == CMDQ_BATCH_ENTRIES - 1) &&
963 			  (smmu->options & ARM_SMMU_OPT_CMDQ_FORCE_SYNC);
964 	int index;
965 
966 	if (force_sync || unsupported_cmd) {
967 		arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmdq, cmds->cmds,
968 					    cmds->num, true);
969 		arm_smmu_cmdq_batch_init(smmu, cmds, cmd);
970 	}
971 
972 	if (cmds->num == CMDQ_BATCH_ENTRIES) {
973 		arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmdq, cmds->cmds,
974 					    cmds->num, false);
975 		arm_smmu_cmdq_batch_init(smmu, cmds, cmd);
976 	}
977 
978 	index = cmds->num * CMDQ_ENT_DWORDS;
979 	if (unlikely(arm_smmu_cmdq_build_cmd(&cmds->cmds[index], cmd))) {
980 		dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
981 			 cmd->opcode);
982 		return;
983 	}
984 
985 	cmds->num++;
986 }
987 
arm_smmu_cmdq_batch_submit(struct arm_smmu_device * smmu,struct arm_smmu_cmdq_batch * cmds)988 static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu,
989 				      struct arm_smmu_cmdq_batch *cmds)
990 {
991 	return arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmdq, cmds->cmds,
992 					   cmds->num, true);
993 }
994 
arm_smmu_page_response(struct device * dev,struct iopf_fault * unused,struct iommu_page_response * resp)995 static void arm_smmu_page_response(struct device *dev, struct iopf_fault *unused,
996 				   struct iommu_page_response *resp)
997 {
998 	struct arm_smmu_cmdq_ent cmd = {0};
999 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
1000 	int sid = master->streams[0].id;
1001 
1002 	if (WARN_ON(!master->stall_enabled))
1003 		return;
1004 
1005 	cmd.opcode		= CMDQ_OP_RESUME;
1006 	cmd.resume.sid		= sid;
1007 	cmd.resume.stag		= resp->grpid;
1008 	switch (resp->code) {
1009 	case IOMMU_PAGE_RESP_INVALID:
1010 	case IOMMU_PAGE_RESP_FAILURE:
1011 		cmd.resume.resp = CMDQ_RESUME_0_RESP_ABORT;
1012 		break;
1013 	case IOMMU_PAGE_RESP_SUCCESS:
1014 		cmd.resume.resp = CMDQ_RESUME_0_RESP_RETRY;
1015 		break;
1016 	default:
1017 		break;
1018 	}
1019 
1020 	arm_smmu_cmdq_issue_cmd(master->smmu, &cmd);
1021 	/*
1022 	 * Don't send a SYNC, it doesn't do anything for RESUME or PRI_RESP.
1023 	 * RESUME consumption guarantees that the stalled transaction will be
1024 	 * terminated... at some point in the future. PRI_RESP is fire and
1025 	 * forget.
1026 	 */
1027 }
1028 
1029 /* Context descriptor manipulation functions */
arm_smmu_tlb_inv_asid(struct arm_smmu_device * smmu,u16 asid)1030 void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid)
1031 {
1032 	struct arm_smmu_cmdq_ent cmd = {
1033 		.opcode	= smmu->features & ARM_SMMU_FEAT_E2H ?
1034 			CMDQ_OP_TLBI_EL2_ASID : CMDQ_OP_TLBI_NH_ASID,
1035 		.tlbi.asid = asid,
1036 	};
1037 
1038 	arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd);
1039 }
1040 
1041 /*
1042  * Based on the value of ent report which bits of the STE the HW will access. It
1043  * would be nice if this was complete according to the spec, but minimally it
1044  * has to capture the bits this driver uses.
1045  */
1046 VISIBLE_IF_KUNIT
arm_smmu_get_ste_used(const __le64 * ent,__le64 * used_bits)1047 void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits)
1048 {
1049 	unsigned int cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(ent[0]));
1050 
1051 	used_bits[0] = cpu_to_le64(STRTAB_STE_0_V);
1052 	if (!(ent[0] & cpu_to_le64(STRTAB_STE_0_V)))
1053 		return;
1054 
1055 	used_bits[0] |= cpu_to_le64(STRTAB_STE_0_CFG);
1056 
1057 	/* S1 translates */
1058 	if (cfg & BIT(0)) {
1059 		used_bits[0] |= cpu_to_le64(STRTAB_STE_0_S1FMT |
1060 					    STRTAB_STE_0_S1CTXPTR_MASK |
1061 					    STRTAB_STE_0_S1CDMAX);
1062 		used_bits[1] |=
1063 			cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR |
1064 				    STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH |
1065 				    STRTAB_STE_1_S1STALLD | STRTAB_STE_1_STRW |
1066 				    STRTAB_STE_1_EATS | STRTAB_STE_1_MEV);
1067 		used_bits[2] |= cpu_to_le64(STRTAB_STE_2_S2VMID);
1068 
1069 		/*
1070 		 * See 13.5 Summary of attribute/permission configuration fields
1071 		 * for the SHCFG behavior.
1072 		 */
1073 		if (FIELD_GET(STRTAB_STE_1_S1DSS, le64_to_cpu(ent[1])) ==
1074 		    STRTAB_STE_1_S1DSS_BYPASS)
1075 			used_bits[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG);
1076 	}
1077 
1078 	/* S2 translates */
1079 	if (cfg & BIT(1)) {
1080 		used_bits[1] |=
1081 			cpu_to_le64(STRTAB_STE_1_S2FWB | STRTAB_STE_1_EATS |
1082 				    STRTAB_STE_1_SHCFG | STRTAB_STE_1_MEV);
1083 		used_bits[2] |=
1084 			cpu_to_le64(STRTAB_STE_2_S2VMID | STRTAB_STE_2_VTCR |
1085 				    STRTAB_STE_2_S2AA64 | STRTAB_STE_2_S2ENDI |
1086 				    STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2S |
1087 				    STRTAB_STE_2_S2R);
1088 		used_bits[3] |= cpu_to_le64(STRTAB_STE_3_S2TTB_MASK);
1089 	}
1090 
1091 	if (cfg == STRTAB_STE_0_CFG_BYPASS)
1092 		used_bits[1] |= cpu_to_le64(STRTAB_STE_1_SHCFG);
1093 }
1094 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_get_ste_used);
1095 
1096 VISIBLE_IF_KUNIT
arm_smmu_get_ste_update_safe(const __le64 * cur,const __le64 * target,__le64 * safe_bits)1097 void arm_smmu_get_ste_update_safe(const __le64 *cur, const __le64 *target,
1098 				  __le64 *safe_bits)
1099 {
1100 	const __le64 eats_s1chk =
1101 		FIELD_PREP(STRTAB_STE_1_EATS, STRTAB_STE_1_EATS_S1CHK);
1102 	const __le64 eats_trans =
1103 		FIELD_PREP(STRTAB_STE_1_EATS, STRTAB_STE_1_EATS_TRANS);
1104 
1105 	/*
1106 	 * When an STE changes EATS_TRANS, the sequencing code in the attach
1107 	 * logic already will have the PCI cap for ATS disabled. Thus at this
1108 	 * moment we can expect that the device will not generate ATS queries
1109 	 * and so we don't care about the sequencing of EATS. The purpose of
1110 	 * EATS_TRANS is to protect the system from hostile untrusted devices
1111 	 * that issue ATS when the PCI config space is disabled. However, if
1112 	 * EATS_TRANS is being changed, then we must have already trusted the
1113 	 * device as the EATS_TRANS security block is being disabled.
1114 	 *
1115 	 *  Note: now the EATS_TRANS update is moved to the first entry_set().
1116 	 *  Changing S2S and EATS might transiently result in S2S=1 and EATS=1
1117 	 *  which is a bad STE (see "5.2 Stream Table Entry"). In such a case,
1118 	 *  we can't do a hitless update. Also, it should not be added to the
1119 	 *  safe bits with STRTAB_STE_1_EATS_S1CHK, because EATS=0b11 would be
1120 	 *  effectively an errant 0b00 configuration.
1121 	 */
1122 	if (!((cur[1] | target[1]) & cpu_to_le64(eats_s1chk)) &&
1123 	    !((cur[2] | target[2]) & cpu_to_le64(STRTAB_STE_2_S2S)))
1124 		safe_bits[1] |= cpu_to_le64(eats_trans);
1125 
1126 	/*
1127 	 * MEV does not meaningfully impact the operation of the HW, it only
1128 	 * changes how many fault events are generated, thus we can relax it
1129 	 * when computing the ordering. The spec notes the device can act like
1130 	 * MEV=1 anyhow:
1131 	 *
1132 	 *  Note: Software must expect, and be able to deal with, coalesced
1133 	 *  fault records even when MEV == 0.
1134 	 */
1135 	safe_bits[1] |= cpu_to_le64(STRTAB_STE_1_MEV);
1136 }
1137 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_get_ste_update_safe);
1138 
1139 /*
1140  * Figure out if we can do a hitless update of entry to become target. Returns a
1141  * bit mask where 1 indicates that qword needs to be set disruptively.
1142  * unused_update is an intermediate value of entry that has unused bits set to
1143  * their new values.
1144  */
arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer * writer,const __le64 * entry,const __le64 * target,__le64 * unused_update)1145 static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer,
1146 				    const __le64 *entry, const __le64 *target,
1147 				    __le64 *unused_update)
1148 {
1149 	__le64 target_used[NUM_ENTRY_QWORDS] = {};
1150 	__le64 cur_used[NUM_ENTRY_QWORDS] = {};
1151 	__le64 safe[NUM_ENTRY_QWORDS] = {};
1152 	u8 used_qword_diff = 0;
1153 	unsigned int i;
1154 
1155 	writer->ops->get_used(entry, cur_used);
1156 	writer->ops->get_used(target, target_used);
1157 	if (writer->ops->get_update_safe)
1158 		writer->ops->get_update_safe(entry, target, safe);
1159 
1160 	for (i = 0; i != NUM_ENTRY_QWORDS; i++) {
1161 		/*
1162 		 * Safe is only used for bits that are used by both entries,
1163 		 * otherwise it is sequenced according to the unused entry.
1164 		 */
1165 		safe[i] &= target_used[i] & cur_used[i];
1166 
1167 		/*
1168 		 * Check that masks are up to date, the make functions are not
1169 		 * allowed to set a bit to 1 if the used function doesn't say it
1170 		 * is used.
1171 		 */
1172 		WARN_ON_ONCE(target[i] & ~target_used[i]);
1173 
1174 		/* Bits can change because they are not currently being used */
1175 		cur_used[i] &= ~safe[i];
1176 		unused_update[i] = (entry[i] & cur_used[i]) |
1177 				   (target[i] & ~cur_used[i]);
1178 		/*
1179 		 * Each bit indicates that a used bit in a qword needs to be
1180 		 * changed after unused_update is applied.
1181 		 */
1182 		if ((unused_update[i] & target_used[i]) != target[i])
1183 			used_qword_diff |= 1 << i;
1184 	}
1185 	return used_qword_diff;
1186 }
1187 
entry_set(struct arm_smmu_entry_writer * writer,__le64 * entry,const __le64 * target,unsigned int start,unsigned int len)1188 static void entry_set(struct arm_smmu_entry_writer *writer, __le64 *entry,
1189 		      const __le64 *target, unsigned int start,
1190 		      unsigned int len)
1191 {
1192 	bool changed = false;
1193 	unsigned int i;
1194 
1195 	for (i = start; len != 0; len--, i++) {
1196 		if (entry[i] != target[i]) {
1197 			WRITE_ONCE(entry[i], target[i]);
1198 			changed = true;
1199 		}
1200 	}
1201 
1202 	if (changed)
1203 		writer->ops->sync(writer);
1204 }
1205 
1206 /*
1207  * Update the STE/CD to the target configuration. The transition from the
1208  * current entry to the target entry takes place over multiple steps that
1209  * attempts to make the transition hitless if possible. This function takes care
1210  * not to create a situation where the HW can perceive a corrupted entry. HW is
1211  * only required to have a 64 bit atomicity with stores from the CPU, while
1212  * entries are many 64 bit values big.
1213  *
1214  * The difference between the current value and the target value is analyzed to
1215  * determine which of three updates are required - disruptive, hitless or no
1216  * change.
1217  *
1218  * In the most general disruptive case we can make any update in three steps:
1219  *  - Disrupting the entry (V=0)
1220  *  - Fill now unused qwords, execpt qword 0 which contains V
1221  *  - Make qword 0 have the final value and valid (V=1) with a single 64
1222  *    bit store
1223  *
1224  * However this disrupts the HW while it is happening. There are several
1225  * interesting cases where a STE/CD can be updated without disturbing the HW
1226  * because only a small number of bits are changing (S1DSS, CONFIG, etc) or
1227  * because the used bits don't intersect. We can detect this by calculating how
1228  * many 64 bit values need update after adjusting the unused bits and skip the
1229  * V=0 process. This relies on the IGNORED behavior described in the
1230  * specification.
1231  */
1232 VISIBLE_IF_KUNIT
arm_smmu_write_entry(struct arm_smmu_entry_writer * writer,__le64 * entry,const __le64 * target)1233 void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *entry,
1234 			  const __le64 *target)
1235 {
1236 	__le64 unused_update[NUM_ENTRY_QWORDS];
1237 	u8 used_qword_diff;
1238 
1239 	used_qword_diff =
1240 		arm_smmu_entry_qword_diff(writer, entry, target, unused_update);
1241 	if (hweight8(used_qword_diff) == 1) {
1242 		/*
1243 		 * Only one qword needs its used bits to be changed. This is a
1244 		 * hitless update, update all bits the current STE/CD is
1245 		 * ignoring to their new values, then update a single "critical
1246 		 * qword" to change the STE/CD and finally 0 out any bits that
1247 		 * are now unused in the target configuration.
1248 		 */
1249 		unsigned int critical_qword_index = ffs(used_qword_diff) - 1;
1250 
1251 		/*
1252 		 * Skip writing unused bits in the critical qword since we'll be
1253 		 * writing it in the next step anyways. This can save a sync
1254 		 * when the only change is in that qword.
1255 		 */
1256 		unused_update[critical_qword_index] =
1257 			entry[critical_qword_index];
1258 		entry_set(writer, entry, unused_update, 0, NUM_ENTRY_QWORDS);
1259 		entry_set(writer, entry, target, critical_qword_index, 1);
1260 		entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS);
1261 	} else if (used_qword_diff) {
1262 		/*
1263 		 * At least two qwords need their inuse bits to be changed. This
1264 		 * requires a breaking update, zero the V bit, write all qwords
1265 		 * but 0, then set qword 0
1266 		 */
1267 		unused_update[0] = 0;
1268 		entry_set(writer, entry, unused_update, 0, 1);
1269 		entry_set(writer, entry, target, 1, NUM_ENTRY_QWORDS - 1);
1270 		entry_set(writer, entry, target, 0, 1);
1271 	} else {
1272 		/*
1273 		 * No inuse bit changed, though safe bits may have changed.
1274 		 */
1275 		entry_set(writer, entry, target, 0, NUM_ENTRY_QWORDS);
1276 	}
1277 }
1278 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_write_entry);
1279 
arm_smmu_sync_cd(struct arm_smmu_master * master,int ssid,bool leaf)1280 static void arm_smmu_sync_cd(struct arm_smmu_master *master,
1281 			     int ssid, bool leaf)
1282 {
1283 	size_t i;
1284 	struct arm_smmu_cmdq_batch cmds;
1285 	struct arm_smmu_device *smmu = master->smmu;
1286 	struct arm_smmu_cmdq_ent cmd = {
1287 		.opcode	= CMDQ_OP_CFGI_CD,
1288 		.cfgi	= {
1289 			.ssid	= ssid,
1290 			.leaf	= leaf,
1291 		},
1292 	};
1293 
1294 	arm_smmu_cmdq_batch_init(smmu, &cmds, &cmd);
1295 	for (i = 0; i < master->num_streams; i++) {
1296 		cmd.cfgi.sid = master->streams[i].id;
1297 		arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd);
1298 	}
1299 
1300 	arm_smmu_cmdq_batch_submit(smmu, &cmds);
1301 }
1302 
arm_smmu_write_cd_l1_desc(struct arm_smmu_cdtab_l1 * dst,dma_addr_t l2ptr_dma)1303 static void arm_smmu_write_cd_l1_desc(struct arm_smmu_cdtab_l1 *dst,
1304 				      dma_addr_t l2ptr_dma)
1305 {
1306 	u64 val = (l2ptr_dma & CTXDESC_L1_DESC_L2PTR_MASK) | CTXDESC_L1_DESC_V;
1307 
1308 	/* The HW has 64 bit atomicity with stores to the L2 CD table */
1309 	WRITE_ONCE(dst->l2ptr, cpu_to_le64(val));
1310 }
1311 
arm_smmu_cd_l1_get_desc(const struct arm_smmu_cdtab_l1 * src)1312 static dma_addr_t arm_smmu_cd_l1_get_desc(const struct arm_smmu_cdtab_l1 *src)
1313 {
1314 	return le64_to_cpu(src->l2ptr) & CTXDESC_L1_DESC_L2PTR_MASK;
1315 }
1316 
arm_smmu_get_cd_ptr(struct arm_smmu_master * master,u32 ssid)1317 struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master,
1318 					u32 ssid)
1319 {
1320 	struct arm_smmu_cdtab_l2 *l2;
1321 	struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;
1322 
1323 	if (!arm_smmu_cdtab_allocated(cd_table))
1324 		return NULL;
1325 
1326 	if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR)
1327 		return &cd_table->linear.table[ssid];
1328 
1329 	l2 = cd_table->l2.l2ptrs[arm_smmu_cdtab_l1_idx(ssid)];
1330 	if (!l2)
1331 		return NULL;
1332 	return &l2->cds[arm_smmu_cdtab_l2_idx(ssid)];
1333 }
1334 
arm_smmu_alloc_cd_ptr(struct arm_smmu_master * master,u32 ssid)1335 static struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master,
1336 						 u32 ssid)
1337 {
1338 	struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;
1339 	struct arm_smmu_device *smmu = master->smmu;
1340 
1341 	might_sleep();
1342 	iommu_group_mutex_assert(master->dev);
1343 
1344 	if (!arm_smmu_cdtab_allocated(cd_table)) {
1345 		if (arm_smmu_alloc_cd_tables(master))
1346 			return NULL;
1347 	}
1348 
1349 	if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_64K_L2) {
1350 		unsigned int idx = arm_smmu_cdtab_l1_idx(ssid);
1351 		struct arm_smmu_cdtab_l2 **l2ptr = &cd_table->l2.l2ptrs[idx];
1352 
1353 		if (!*l2ptr) {
1354 			dma_addr_t l2ptr_dma;
1355 
1356 			*l2ptr = dma_alloc_coherent(smmu->dev, sizeof(**l2ptr),
1357 						    &l2ptr_dma, GFP_KERNEL);
1358 			if (!*l2ptr)
1359 				return NULL;
1360 
1361 			arm_smmu_write_cd_l1_desc(&cd_table->l2.l1tab[idx],
1362 						  l2ptr_dma);
1363 			/* An invalid L1CD can be cached */
1364 			arm_smmu_sync_cd(master, ssid, false);
1365 		}
1366 	}
1367 	return arm_smmu_get_cd_ptr(master, ssid);
1368 }
1369 
1370 struct arm_smmu_cd_writer {
1371 	struct arm_smmu_entry_writer writer;
1372 	unsigned int ssid;
1373 };
1374 
1375 VISIBLE_IF_KUNIT
arm_smmu_get_cd_used(const __le64 * ent,__le64 * used_bits)1376 void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits)
1377 {
1378 	used_bits[0] = cpu_to_le64(CTXDESC_CD_0_V);
1379 	if (!(ent[0] & cpu_to_le64(CTXDESC_CD_0_V)))
1380 		return;
1381 	memset(used_bits, 0xFF, sizeof(struct arm_smmu_cd));
1382 
1383 	/*
1384 	 * If EPD0 is set by the make function it means
1385 	 * T0SZ/TG0/IR0/OR0/SH0/TTB0 are IGNORED
1386 	 */
1387 	if (ent[0] & cpu_to_le64(CTXDESC_CD_0_TCR_EPD0)) {
1388 		used_bits[0] &= ~cpu_to_le64(
1389 			CTXDESC_CD_0_TCR_T0SZ | CTXDESC_CD_0_TCR_TG0 |
1390 			CTXDESC_CD_0_TCR_IRGN0 | CTXDESC_CD_0_TCR_ORGN0 |
1391 			CTXDESC_CD_0_TCR_SH0);
1392 		used_bits[1] &= ~cpu_to_le64(CTXDESC_CD_1_TTB0_MASK);
1393 	}
1394 }
1395 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_get_cd_used);
1396 
arm_smmu_cd_writer_sync_entry(struct arm_smmu_entry_writer * writer)1397 static void arm_smmu_cd_writer_sync_entry(struct arm_smmu_entry_writer *writer)
1398 {
1399 	struct arm_smmu_cd_writer *cd_writer =
1400 		container_of(writer, struct arm_smmu_cd_writer, writer);
1401 
1402 	arm_smmu_sync_cd(writer->master, cd_writer->ssid, true);
1403 }
1404 
1405 static const struct arm_smmu_entry_writer_ops arm_smmu_cd_writer_ops = {
1406 	.sync = arm_smmu_cd_writer_sync_entry,
1407 	.get_used = arm_smmu_get_cd_used,
1408 };
1409 
arm_smmu_write_cd_entry(struct arm_smmu_master * master,int ssid,struct arm_smmu_cd * cdptr,const struct arm_smmu_cd * target)1410 void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,
1411 			     struct arm_smmu_cd *cdptr,
1412 			     const struct arm_smmu_cd *target)
1413 {
1414 	bool target_valid = target->data[0] & cpu_to_le64(CTXDESC_CD_0_V);
1415 	bool cur_valid = cdptr->data[0] & cpu_to_le64(CTXDESC_CD_0_V);
1416 	struct arm_smmu_cd_writer cd_writer = {
1417 		.writer = {
1418 			.ops = &arm_smmu_cd_writer_ops,
1419 			.master = master,
1420 		},
1421 		.ssid = ssid,
1422 	};
1423 
1424 	if (ssid != IOMMU_NO_PASID && cur_valid != target_valid) {
1425 		if (cur_valid)
1426 			master->cd_table.used_ssids--;
1427 		else
1428 			master->cd_table.used_ssids++;
1429 	}
1430 
1431 	arm_smmu_write_entry(&cd_writer.writer, cdptr->data, target->data);
1432 }
1433 
arm_smmu_make_s1_cd(struct arm_smmu_cd * target,struct arm_smmu_master * master,struct arm_smmu_domain * smmu_domain)1434 void arm_smmu_make_s1_cd(struct arm_smmu_cd *target,
1435 			 struct arm_smmu_master *master,
1436 			 struct arm_smmu_domain *smmu_domain)
1437 {
1438 	struct arm_smmu_ctx_desc *cd = &smmu_domain->cd;
1439 	const struct io_pgtable_cfg *pgtbl_cfg =
1440 		&io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops)->cfg;
1441 	typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr =
1442 		&pgtbl_cfg->arm_lpae_s1_cfg.tcr;
1443 
1444 	memset(target, 0, sizeof(*target));
1445 
1446 	target->data[0] = cpu_to_le64(
1447 		FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) |
1448 		FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) |
1449 		FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) |
1450 		FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) |
1451 		FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) |
1452 #ifdef __BIG_ENDIAN
1453 		CTXDESC_CD_0_ENDI |
1454 #endif
1455 		CTXDESC_CD_0_TCR_EPD1 |
1456 		CTXDESC_CD_0_V |
1457 		FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) |
1458 		CTXDESC_CD_0_AA64 |
1459 		(master->stall_enabled ? CTXDESC_CD_0_S : 0) |
1460 		CTXDESC_CD_0_R |
1461 		CTXDESC_CD_0_A |
1462 		CTXDESC_CD_0_ASET |
1463 		FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid)
1464 		);
1465 
1466 	/* To enable dirty flag update, set both Access flag and dirty state update */
1467 	if (pgtbl_cfg->quirks & IO_PGTABLE_QUIRK_ARM_HD)
1468 		target->data[0] |= cpu_to_le64(CTXDESC_CD_0_TCR_HA |
1469 					       CTXDESC_CD_0_TCR_HD);
1470 
1471 	target->data[1] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.ttbr &
1472 				      CTXDESC_CD_1_TTB0_MASK);
1473 	target->data[3] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.mair);
1474 }
1475 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_s1_cd);
1476 
arm_smmu_clear_cd(struct arm_smmu_master * master,ioasid_t ssid)1477 void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid)
1478 {
1479 	struct arm_smmu_cd target = {};
1480 	struct arm_smmu_cd *cdptr;
1481 
1482 	if (!arm_smmu_cdtab_allocated(&master->cd_table))
1483 		return;
1484 	cdptr = arm_smmu_get_cd_ptr(master, ssid);
1485 	if (WARN_ON(!cdptr))
1486 		return;
1487 	arm_smmu_write_cd_entry(master, ssid, cdptr, &target);
1488 }
1489 
arm_smmu_alloc_cd_tables(struct arm_smmu_master * master)1490 static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master)
1491 {
1492 	int ret;
1493 	size_t l1size;
1494 	size_t max_contexts;
1495 	struct arm_smmu_device *smmu = master->smmu;
1496 	struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;
1497 
1498 	cd_table->s1cdmax = master->ssid_bits;
1499 	max_contexts = 1 << cd_table->s1cdmax;
1500 
1501 	if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB) ||
1502 	    max_contexts <= CTXDESC_L2_ENTRIES) {
1503 		cd_table->s1fmt = STRTAB_STE_0_S1FMT_LINEAR;
1504 		cd_table->linear.num_ents = max_contexts;
1505 
1506 		l1size = max_contexts * sizeof(struct arm_smmu_cd);
1507 		cd_table->linear.table = dma_alloc_coherent(smmu->dev, l1size,
1508 							    &cd_table->cdtab_dma,
1509 							    GFP_KERNEL);
1510 		if (!cd_table->linear.table)
1511 			return -ENOMEM;
1512 	} else {
1513 		cd_table->s1fmt = STRTAB_STE_0_S1FMT_64K_L2;
1514 		cd_table->l2.num_l1_ents =
1515 			DIV_ROUND_UP(max_contexts, CTXDESC_L2_ENTRIES);
1516 
1517 		cd_table->l2.l2ptrs = kzalloc_objs(*cd_table->l2.l2ptrs,
1518 						   cd_table->l2.num_l1_ents);
1519 		if (!cd_table->l2.l2ptrs)
1520 			return -ENOMEM;
1521 
1522 		l1size = cd_table->l2.num_l1_ents * sizeof(struct arm_smmu_cdtab_l1);
1523 		cd_table->l2.l1tab = dma_alloc_coherent(smmu->dev, l1size,
1524 							&cd_table->cdtab_dma,
1525 							GFP_KERNEL);
1526 		if (!cd_table->l2.l1tab) {
1527 			ret = -ENOMEM;
1528 			goto err_free_l2ptrs;
1529 		}
1530 	}
1531 	return 0;
1532 
1533 err_free_l2ptrs:
1534 	kfree(cd_table->l2.l2ptrs);
1535 	cd_table->l2.l2ptrs = NULL;
1536 	return ret;
1537 }
1538 
arm_smmu_free_cd_tables(struct arm_smmu_master * master)1539 static void arm_smmu_free_cd_tables(struct arm_smmu_master *master)
1540 {
1541 	int i;
1542 	struct arm_smmu_device *smmu = master->smmu;
1543 	struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;
1544 
1545 	if (cd_table->s1fmt != STRTAB_STE_0_S1FMT_LINEAR) {
1546 		for (i = 0; i < cd_table->l2.num_l1_ents; i++) {
1547 			if (!cd_table->l2.l2ptrs[i])
1548 				continue;
1549 
1550 			dma_free_coherent(smmu->dev,
1551 					  sizeof(*cd_table->l2.l2ptrs[i]),
1552 					  cd_table->l2.l2ptrs[i],
1553 					  arm_smmu_cd_l1_get_desc(&cd_table->l2.l1tab[i]));
1554 		}
1555 		kfree(cd_table->l2.l2ptrs);
1556 
1557 		dma_free_coherent(smmu->dev,
1558 				  cd_table->l2.num_l1_ents *
1559 					  sizeof(struct arm_smmu_cdtab_l1),
1560 				  cd_table->l2.l1tab, cd_table->cdtab_dma);
1561 	} else {
1562 		dma_free_coherent(smmu->dev,
1563 				  cd_table->linear.num_ents *
1564 					  sizeof(struct arm_smmu_cd),
1565 				  cd_table->linear.table, cd_table->cdtab_dma);
1566 	}
1567 }
1568 
1569 /* Stream table manipulation functions */
arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 * dst,dma_addr_t l2ptr_dma)1570 static void arm_smmu_write_strtab_l1_desc(struct arm_smmu_strtab_l1 *dst,
1571 					  dma_addr_t l2ptr_dma)
1572 {
1573 	u64 val = 0;
1574 
1575 	val |= FIELD_PREP(STRTAB_L1_DESC_SPAN, STRTAB_SPLIT + 1);
1576 	val |= l2ptr_dma & STRTAB_L1_DESC_L2PTR_MASK;
1577 
1578 	/* The HW has 64 bit atomicity with stores to the L2 STE table */
1579 	WRITE_ONCE(dst->l2ptr, cpu_to_le64(val));
1580 }
1581 
1582 struct arm_smmu_ste_writer {
1583 	struct arm_smmu_entry_writer writer;
1584 	u32 sid;
1585 };
1586 
arm_smmu_ste_writer_sync_entry(struct arm_smmu_entry_writer * writer)1587 static void arm_smmu_ste_writer_sync_entry(struct arm_smmu_entry_writer *writer)
1588 {
1589 	struct arm_smmu_ste_writer *ste_writer =
1590 		container_of(writer, struct arm_smmu_ste_writer, writer);
1591 	struct arm_smmu_cmdq_ent cmd = {
1592 		.opcode	= CMDQ_OP_CFGI_STE,
1593 		.cfgi	= {
1594 			.sid	= ste_writer->sid,
1595 			.leaf	= true,
1596 		},
1597 	};
1598 
1599 	arm_smmu_cmdq_issue_cmd_with_sync(writer->master->smmu, &cmd);
1600 }
1601 
1602 static const struct arm_smmu_entry_writer_ops arm_smmu_ste_writer_ops = {
1603 	.sync = arm_smmu_ste_writer_sync_entry,
1604 	.get_used = arm_smmu_get_ste_used,
1605 	.get_update_safe = arm_smmu_get_ste_update_safe,
1606 };
1607 
arm_smmu_write_ste(struct arm_smmu_master * master,u32 sid,struct arm_smmu_ste * ste,const struct arm_smmu_ste * target)1608 static void arm_smmu_write_ste(struct arm_smmu_master *master, u32 sid,
1609 			       struct arm_smmu_ste *ste,
1610 			       const struct arm_smmu_ste *target)
1611 {
1612 	struct arm_smmu_device *smmu = master->smmu;
1613 	struct arm_smmu_ste_writer ste_writer = {
1614 		.writer = {
1615 			.ops = &arm_smmu_ste_writer_ops,
1616 			.master = master,
1617 		},
1618 		.sid = sid,
1619 	};
1620 
1621 	arm_smmu_write_entry(&ste_writer.writer, ste->data, target->data);
1622 
1623 	/* It's likely that we'll want to use the new STE soon */
1624 	if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH)) {
1625 		struct arm_smmu_cmdq_ent
1626 			prefetch_cmd = { .opcode = CMDQ_OP_PREFETCH_CFG,
1627 					 .prefetch = {
1628 						 .sid = sid,
1629 					 } };
1630 
1631 		arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
1632 	}
1633 }
1634 
arm_smmu_make_abort_ste(struct arm_smmu_ste * target)1635 void arm_smmu_make_abort_ste(struct arm_smmu_ste *target)
1636 {
1637 	memset(target, 0, sizeof(*target));
1638 	target->data[0] = cpu_to_le64(
1639 		STRTAB_STE_0_V |
1640 		FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT));
1641 }
1642 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_abort_ste);
1643 
1644 VISIBLE_IF_KUNIT
arm_smmu_make_bypass_ste(struct arm_smmu_device * smmu,struct arm_smmu_ste * target)1645 void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu,
1646 			      struct arm_smmu_ste *target)
1647 {
1648 	memset(target, 0, sizeof(*target));
1649 	target->data[0] = cpu_to_le64(
1650 		STRTAB_STE_0_V |
1651 		FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_BYPASS));
1652 
1653 	if (smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR)
1654 		target->data[1] = cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG,
1655 							 STRTAB_STE_1_SHCFG_INCOMING));
1656 }
1657 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_bypass_ste);
1658 
1659 VISIBLE_IF_KUNIT
arm_smmu_make_cdtable_ste(struct arm_smmu_ste * target,struct arm_smmu_master * master,bool ats_enabled,unsigned int s1dss)1660 void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target,
1661 			       struct arm_smmu_master *master, bool ats_enabled,
1662 			       unsigned int s1dss)
1663 {
1664 	struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table;
1665 	struct arm_smmu_device *smmu = master->smmu;
1666 
1667 	memset(target, 0, sizeof(*target));
1668 	target->data[0] = cpu_to_le64(
1669 		STRTAB_STE_0_V |
1670 		FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S1_TRANS) |
1671 		FIELD_PREP(STRTAB_STE_0_S1FMT, cd_table->s1fmt) |
1672 		(cd_table->cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) |
1673 		FIELD_PREP(STRTAB_STE_0_S1CDMAX, cd_table->s1cdmax));
1674 
1675 	target->data[1] = cpu_to_le64(
1676 		FIELD_PREP(STRTAB_STE_1_S1DSS, s1dss) |
1677 		FIELD_PREP(STRTAB_STE_1_S1CIR, STRTAB_STE_1_S1C_CACHE_WBRA) |
1678 		FIELD_PREP(STRTAB_STE_1_S1COR, STRTAB_STE_1_S1C_CACHE_WBRA) |
1679 		FIELD_PREP(STRTAB_STE_1_S1CSH, ARM_SMMU_SH_ISH) |
1680 		((smmu->features & ARM_SMMU_FEAT_STALLS &&
1681 		  !master->stall_enabled) ?
1682 			 STRTAB_STE_1_S1STALLD :
1683 			 0) |
1684 		FIELD_PREP(STRTAB_STE_1_EATS,
1685 			   ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0));
1686 
1687 	if ((smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR) &&
1688 	    s1dss == STRTAB_STE_1_S1DSS_BYPASS)
1689 		target->data[1] |= cpu_to_le64(FIELD_PREP(
1690 			STRTAB_STE_1_SHCFG, STRTAB_STE_1_SHCFG_INCOMING));
1691 
1692 	if (smmu->features & ARM_SMMU_FEAT_E2H) {
1693 		/*
1694 		 * To support BTM the streamworld needs to match the
1695 		 * configuration of the CPU so that the ASID broadcasts are
1696 		 * properly matched. This means either S/NS-EL2-E2H (hypervisor)
1697 		 * or NS-EL1 (guest). Since an SVA domain can be installed in a
1698 		 * PASID this should always use a BTM compatible configuration
1699 		 * if the HW supports it.
1700 		 */
1701 		target->data[1] |= cpu_to_le64(
1702 			FIELD_PREP(STRTAB_STE_1_STRW, STRTAB_STE_1_STRW_EL2));
1703 	} else {
1704 		target->data[1] |= cpu_to_le64(
1705 			FIELD_PREP(STRTAB_STE_1_STRW, STRTAB_STE_1_STRW_NSEL1));
1706 
1707 		/*
1708 		 * VMID 0 is reserved for stage-2 bypass EL1 STEs, see
1709 		 * arm_smmu_domain_alloc_id()
1710 		 */
1711 		target->data[2] =
1712 			cpu_to_le64(FIELD_PREP(STRTAB_STE_2_S2VMID, 0));
1713 	}
1714 }
1715 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_cdtable_ste);
1716 
arm_smmu_make_s2_domain_ste(struct arm_smmu_ste * target,struct arm_smmu_master * master,struct arm_smmu_domain * smmu_domain,bool ats_enabled)1717 void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target,
1718 				 struct arm_smmu_master *master,
1719 				 struct arm_smmu_domain *smmu_domain,
1720 				 bool ats_enabled)
1721 {
1722 	struct arm_smmu_s2_cfg *s2_cfg = &smmu_domain->s2_cfg;
1723 	const struct io_pgtable_cfg *pgtbl_cfg =
1724 		&io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops)->cfg;
1725 	typeof(&pgtbl_cfg->arm_lpae_s2_cfg.vtcr) vtcr =
1726 		&pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
1727 	u64 vtcr_val;
1728 	struct arm_smmu_device *smmu = master->smmu;
1729 
1730 	memset(target, 0, sizeof(*target));
1731 	target->data[0] = cpu_to_le64(
1732 		STRTAB_STE_0_V |
1733 		FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S2_TRANS));
1734 
1735 	target->data[1] = cpu_to_le64(
1736 		FIELD_PREP(STRTAB_STE_1_EATS,
1737 			   ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0));
1738 
1739 	if (pgtbl_cfg->quirks & IO_PGTABLE_QUIRK_ARM_S2FWB)
1740 		target->data[1] |= cpu_to_le64(STRTAB_STE_1_S2FWB);
1741 	if (smmu->features & ARM_SMMU_FEAT_ATTR_TYPES_OVR)
1742 		target->data[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG,
1743 							  STRTAB_STE_1_SHCFG_INCOMING));
1744 
1745 	vtcr_val = FIELD_PREP(STRTAB_STE_2_VTCR_S2T0SZ, vtcr->tsz) |
1746 		   FIELD_PREP(STRTAB_STE_2_VTCR_S2SL0, vtcr->sl) |
1747 		   FIELD_PREP(STRTAB_STE_2_VTCR_S2IR0, vtcr->irgn) |
1748 		   FIELD_PREP(STRTAB_STE_2_VTCR_S2OR0, vtcr->orgn) |
1749 		   FIELD_PREP(STRTAB_STE_2_VTCR_S2SH0, vtcr->sh) |
1750 		   FIELD_PREP(STRTAB_STE_2_VTCR_S2TG, vtcr->tg) |
1751 		   FIELD_PREP(STRTAB_STE_2_VTCR_S2PS, vtcr->ps);
1752 	target->data[2] = cpu_to_le64(
1753 		FIELD_PREP(STRTAB_STE_2_S2VMID, s2_cfg->vmid) |
1754 		FIELD_PREP(STRTAB_STE_2_VTCR, vtcr_val) |
1755 		STRTAB_STE_2_S2AA64 |
1756 #ifdef __BIG_ENDIAN
1757 		STRTAB_STE_2_S2ENDI |
1758 #endif
1759 		STRTAB_STE_2_S2PTW |
1760 		(master->stall_enabled ? STRTAB_STE_2_S2S : 0) |
1761 		STRTAB_STE_2_S2R);
1762 
1763 	target->data[3] = cpu_to_le64(pgtbl_cfg->arm_lpae_s2_cfg.vttbr &
1764 				      STRTAB_STE_3_S2TTB_MASK);
1765 }
1766 EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_s2_domain_ste);
1767 
1768 /*
1769  * This can safely directly manipulate the STE memory without a sync sequence
1770  * because the STE table has not been installed in the SMMU yet.
1771  */
arm_smmu_init_initial_stes(struct arm_smmu_ste * strtab,unsigned int nent)1772 static void arm_smmu_init_initial_stes(struct arm_smmu_ste *strtab,
1773 				       unsigned int nent)
1774 {
1775 	unsigned int i;
1776 
1777 	for (i = 0; i < nent; ++i) {
1778 		arm_smmu_make_abort_ste(strtab);
1779 		strtab++;
1780 	}
1781 }
1782 
arm_smmu_init_l2_strtab(struct arm_smmu_device * smmu,u32 sid)1783 static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1784 {
1785 	dma_addr_t l2ptr_dma;
1786 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1787 	struct arm_smmu_strtab_l2 **l2table;
1788 
1789 	l2table = &cfg->l2.l2ptrs[arm_smmu_strtab_l1_idx(sid)];
1790 	if (*l2table)
1791 		return 0;
1792 
1793 	*l2table = dmam_alloc_coherent(smmu->dev, sizeof(**l2table),
1794 				       &l2ptr_dma, GFP_KERNEL);
1795 	if (!*l2table) {
1796 		dev_err(smmu->dev,
1797 			"failed to allocate l2 stream table for SID %u\n",
1798 			sid);
1799 		return -ENOMEM;
1800 	}
1801 
1802 	arm_smmu_init_initial_stes((*l2table)->stes,
1803 				   ARRAY_SIZE((*l2table)->stes));
1804 	arm_smmu_write_strtab_l1_desc(&cfg->l2.l1tab[arm_smmu_strtab_l1_idx(sid)],
1805 				      l2ptr_dma);
1806 	return 0;
1807 }
1808 
arm_smmu_streams_cmp_key(const void * lhs,const struct rb_node * rhs)1809 static int arm_smmu_streams_cmp_key(const void *lhs, const struct rb_node *rhs)
1810 {
1811 	struct arm_smmu_stream *stream_rhs =
1812 		rb_entry(rhs, struct arm_smmu_stream, node);
1813 	const u32 *sid_lhs = lhs;
1814 
1815 	if (*sid_lhs < stream_rhs->id)
1816 		return -1;
1817 	if (*sid_lhs > stream_rhs->id)
1818 		return 1;
1819 	return 0;
1820 }
1821 
arm_smmu_streams_cmp_node(struct rb_node * lhs,const struct rb_node * rhs)1822 static int arm_smmu_streams_cmp_node(struct rb_node *lhs,
1823 				     const struct rb_node *rhs)
1824 {
1825 	return arm_smmu_streams_cmp_key(
1826 		&rb_entry(lhs, struct arm_smmu_stream, node)->id, rhs);
1827 }
1828 
1829 static struct arm_smmu_master *
arm_smmu_find_master(struct arm_smmu_device * smmu,u32 sid)1830 arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
1831 {
1832 	struct rb_node *node;
1833 
1834 	lockdep_assert_held(&smmu->streams_mutex);
1835 
1836 	node = rb_find(&sid, &smmu->streams, arm_smmu_streams_cmp_key);
1837 	if (!node)
1838 		return NULL;
1839 	return rb_entry(node, struct arm_smmu_stream, node)->master;
1840 }
1841 
1842 /* IRQ and event handlers */
arm_smmu_decode_event(struct arm_smmu_device * smmu,u64 * raw,struct arm_smmu_event * event)1843 static void arm_smmu_decode_event(struct arm_smmu_device *smmu, u64 *raw,
1844 				  struct arm_smmu_event *event)
1845 {
1846 	struct arm_smmu_master *master;
1847 
1848 	event->id = FIELD_GET(EVTQ_0_ID, raw[0]);
1849 	event->sid = FIELD_GET(EVTQ_0_SID, raw[0]);
1850 	event->ssv = FIELD_GET(EVTQ_0_SSV, raw[0]);
1851 	event->ssid = event->ssv ? FIELD_GET(EVTQ_0_SSID, raw[0]) : IOMMU_NO_PASID;
1852 	event->privileged = FIELD_GET(EVTQ_1_PnU, raw[1]);
1853 	event->instruction = FIELD_GET(EVTQ_1_InD, raw[1]);
1854 	event->s2 = FIELD_GET(EVTQ_1_S2, raw[1]);
1855 	event->read = FIELD_GET(EVTQ_1_RnW, raw[1]);
1856 	event->stag = FIELD_GET(EVTQ_1_STAG, raw[1]);
1857 	event->stall = FIELD_GET(EVTQ_1_STALL, raw[1]);
1858 	event->class = FIELD_GET(EVTQ_1_CLASS, raw[1]);
1859 	event->iova = FIELD_GET(EVTQ_2_ADDR, raw[2]);
1860 	event->ipa = raw[3] & EVTQ_3_IPA;
1861 	event->fetch_addr = raw[3] & EVTQ_3_FETCH_ADDR;
1862 	event->ttrnw = FIELD_GET(EVTQ_1_TT_READ, raw[1]);
1863 	event->class_tt = false;
1864 	event->dev = NULL;
1865 
1866 	if (event->id == EVT_ID_PERMISSION_FAULT)
1867 		event->class_tt = (event->class == EVTQ_1_CLASS_TT);
1868 
1869 	mutex_lock(&smmu->streams_mutex);
1870 	master = arm_smmu_find_master(smmu, event->sid);
1871 	if (master)
1872 		event->dev = get_device(master->dev);
1873 	mutex_unlock(&smmu->streams_mutex);
1874 }
1875 
arm_smmu_handle_event(struct arm_smmu_device * smmu,u64 * evt,struct arm_smmu_event * event)1876 static int arm_smmu_handle_event(struct arm_smmu_device *smmu, u64 *evt,
1877 				 struct arm_smmu_event *event)
1878 {
1879 	int ret = 0;
1880 	u32 perm = 0;
1881 	struct arm_smmu_master *master;
1882 	struct iopf_fault fault_evt = { };
1883 	struct iommu_fault *flt = &fault_evt.fault;
1884 
1885 	switch (event->id) {
1886 	case EVT_ID_BAD_STE_CONFIG:
1887 	case EVT_ID_STREAM_DISABLED_FAULT:
1888 	case EVT_ID_BAD_SUBSTREAMID_CONFIG:
1889 	case EVT_ID_BAD_CD_CONFIG:
1890 	case EVT_ID_TRANSLATION_FAULT:
1891 	case EVT_ID_ADDR_SIZE_FAULT:
1892 	case EVT_ID_ACCESS_FAULT:
1893 	case EVT_ID_PERMISSION_FAULT:
1894 		break;
1895 	default:
1896 		return -EOPNOTSUPP;
1897 	}
1898 
1899 	if (event->stall) {
1900 		if (event->read)
1901 			perm |= IOMMU_FAULT_PERM_READ;
1902 		else
1903 			perm |= IOMMU_FAULT_PERM_WRITE;
1904 
1905 		if (event->instruction)
1906 			perm |= IOMMU_FAULT_PERM_EXEC;
1907 
1908 		if (event->privileged)
1909 			perm |= IOMMU_FAULT_PERM_PRIV;
1910 
1911 		flt->type = IOMMU_FAULT_PAGE_REQ;
1912 		flt->prm = (struct iommu_fault_page_request){
1913 			.flags = IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE,
1914 			.grpid = event->stag,
1915 			.perm = perm,
1916 			.addr = event->iova,
1917 		};
1918 
1919 		if (event->ssv) {
1920 			flt->prm.flags |= IOMMU_FAULT_PAGE_REQUEST_PASID_VALID;
1921 			flt->prm.pasid = event->ssid;
1922 		}
1923 	}
1924 
1925 	mutex_lock(&smmu->streams_mutex);
1926 	master = arm_smmu_find_master(smmu, event->sid);
1927 	if (!master) {
1928 		ret = -EINVAL;
1929 		goto out_unlock;
1930 	}
1931 
1932 	if (event->stall)
1933 		ret = iommu_report_device_fault(master->dev, &fault_evt);
1934 	else if (master->vmaster && !event->s2)
1935 		ret = arm_vmaster_report_event(master->vmaster, evt);
1936 	else
1937 		ret = -EOPNOTSUPP; /* Unhandled events should be pinned */
1938 out_unlock:
1939 	mutex_unlock(&smmu->streams_mutex);
1940 	return ret;
1941 }
1942 
arm_smmu_dump_raw_event(struct arm_smmu_device * smmu,u64 * raw,struct arm_smmu_event * event)1943 static void arm_smmu_dump_raw_event(struct arm_smmu_device *smmu, u64 *raw,
1944 				    struct arm_smmu_event *event)
1945 {
1946 	int i;
1947 
1948 	dev_err(smmu->dev, "event 0x%02x received:\n", event->id);
1949 
1950 	for (i = 0; i < EVTQ_ENT_DWORDS; ++i)
1951 		dev_err(smmu->dev, "\t0x%016llx\n", raw[i]);
1952 }
1953 
1954 #define ARM_SMMU_EVT_KNOWN(e)	((e)->id < ARRAY_SIZE(event_str) && event_str[(e)->id])
1955 #define ARM_SMMU_LOG_EVT_STR(e) ARM_SMMU_EVT_KNOWN(e) ? event_str[(e)->id] : "UNKNOWN"
1956 #define ARM_SMMU_LOG_CLIENT(e)	(e)->dev ? dev_name((e)->dev) : "(unassigned sid)"
1957 
arm_smmu_dump_event(struct arm_smmu_device * smmu,u64 * raw,struct arm_smmu_event * evt,struct ratelimit_state * rs)1958 static void arm_smmu_dump_event(struct arm_smmu_device *smmu, u64 *raw,
1959 				struct arm_smmu_event *evt,
1960 				struct ratelimit_state *rs)
1961 {
1962 	if (!__ratelimit(rs))
1963 		return;
1964 
1965 	arm_smmu_dump_raw_event(smmu, raw, evt);
1966 
1967 	switch (evt->id) {
1968 	case EVT_ID_TRANSLATION_FAULT:
1969 	case EVT_ID_ADDR_SIZE_FAULT:
1970 	case EVT_ID_ACCESS_FAULT:
1971 	case EVT_ID_PERMISSION_FAULT:
1972 		dev_err(smmu->dev, "event: %s client: %s sid: %#x ssid: %#x iova: %#llx ipa: %#llx",
1973 			ARM_SMMU_LOG_EVT_STR(evt), ARM_SMMU_LOG_CLIENT(evt),
1974 			evt->sid, evt->ssid, evt->iova, evt->ipa);
1975 
1976 		dev_err(smmu->dev, "%s %s %s %s \"%s\"%s%s stag: %#x",
1977 			evt->privileged ? "priv" : "unpriv",
1978 			evt->instruction ? "inst" : "data",
1979 			str_read_write(evt->read),
1980 			evt->s2 ? "s2" : "s1", event_class_str[evt->class],
1981 			evt->class_tt ? (evt->ttrnw ? " ttd_read" : " ttd_write") : "",
1982 			evt->stall ? " stall" : "", evt->stag);
1983 
1984 		break;
1985 
1986 	case EVT_ID_STE_FETCH_FAULT:
1987 	case EVT_ID_CD_FETCH_FAULT:
1988 	case EVT_ID_VMS_FETCH_FAULT:
1989 		dev_err(smmu->dev, "event: %s client: %s sid: %#x ssid: %#x fetch_addr: %#llx",
1990 			ARM_SMMU_LOG_EVT_STR(evt), ARM_SMMU_LOG_CLIENT(evt),
1991 			evt->sid, evt->ssid, evt->fetch_addr);
1992 
1993 		break;
1994 
1995 	default:
1996 		dev_err(smmu->dev, "event: %s client: %s sid: %#x ssid: %#x",
1997 			ARM_SMMU_LOG_EVT_STR(evt), ARM_SMMU_LOG_CLIENT(evt),
1998 			evt->sid, evt->ssid);
1999 	}
2000 }
2001 
arm_smmu_evtq_thread(int irq,void * dev)2002 static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
2003 {
2004 	u64 evt[EVTQ_ENT_DWORDS];
2005 	struct arm_smmu_event event = {0};
2006 	struct arm_smmu_device *smmu = dev;
2007 	struct arm_smmu_queue *q = &smmu->evtq.q;
2008 	struct arm_smmu_ll_queue *llq = &q->llq;
2009 	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
2010 				      DEFAULT_RATELIMIT_BURST);
2011 
2012 	do {
2013 		while (!queue_remove_raw(q, evt)) {
2014 			arm_smmu_decode_event(smmu, evt, &event);
2015 			if (arm_smmu_handle_event(smmu, evt, &event))
2016 				arm_smmu_dump_event(smmu, evt, &event, &rs);
2017 
2018 			put_device(event.dev);
2019 			cond_resched();
2020 		}
2021 
2022 		/*
2023 		 * Not much we can do on overflow, so scream and pretend we're
2024 		 * trying harder.
2025 		 */
2026 		if (queue_sync_prod_in(q) == -EOVERFLOW)
2027 			dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
2028 	} while (!queue_empty(llq));
2029 
2030 	/* Sync our overflow flag, as we believe we're up to speed */
2031 	queue_sync_cons_ovf(q);
2032 	return IRQ_HANDLED;
2033 }
2034 
arm_smmu_handle_ppr(struct arm_smmu_device * smmu,u64 * evt)2035 static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt)
2036 {
2037 	u32 sid, ssid;
2038 	u16 grpid;
2039 	bool ssv, last;
2040 
2041 	sid = FIELD_GET(PRIQ_0_SID, evt[0]);
2042 	ssv = FIELD_GET(PRIQ_0_SSID_V, evt[0]);
2043 	ssid = ssv ? FIELD_GET(PRIQ_0_SSID, evt[0]) : IOMMU_NO_PASID;
2044 	last = FIELD_GET(PRIQ_0_PRG_LAST, evt[0]);
2045 	grpid = FIELD_GET(PRIQ_1_PRG_IDX, evt[1]);
2046 
2047 	dev_info(smmu->dev, "unexpected PRI request received:\n");
2048 	dev_info(smmu->dev,
2049 		 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
2050 		 sid, ssid, grpid, last ? "L" : "",
2051 		 evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
2052 		 evt[0] & PRIQ_0_PERM_READ ? "R" : "",
2053 		 evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
2054 		 evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
2055 		 evt[1] & PRIQ_1_ADDR_MASK);
2056 
2057 	if (last) {
2058 		struct arm_smmu_cmdq_ent cmd = {
2059 			.opcode			= CMDQ_OP_PRI_RESP,
2060 			.substream_valid	= ssv,
2061 			.pri			= {
2062 				.sid	= sid,
2063 				.ssid	= ssid,
2064 				.grpid	= grpid,
2065 				.resp	= PRI_RESP_DENY,
2066 			},
2067 		};
2068 
2069 		arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2070 	}
2071 }
2072 
arm_smmu_priq_thread(int irq,void * dev)2073 static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
2074 {
2075 	struct arm_smmu_device *smmu = dev;
2076 	struct arm_smmu_queue *q = &smmu->priq.q;
2077 	struct arm_smmu_ll_queue *llq = &q->llq;
2078 	u64 evt[PRIQ_ENT_DWORDS];
2079 
2080 	do {
2081 		while (!queue_remove_raw(q, evt))
2082 			arm_smmu_handle_ppr(smmu, evt);
2083 
2084 		if (queue_sync_prod_in(q) == -EOVERFLOW)
2085 			dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
2086 	} while (!queue_empty(llq));
2087 
2088 	/* Sync our overflow flag, as we believe we're up to speed */
2089 	queue_sync_cons_ovf(q);
2090 	return IRQ_HANDLED;
2091 }
2092 
2093 static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
2094 
arm_smmu_gerror_handler(int irq,void * dev)2095 static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
2096 {
2097 	u32 gerror, gerrorn, active;
2098 	struct arm_smmu_device *smmu = dev;
2099 
2100 	gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
2101 	gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
2102 
2103 	active = gerror ^ gerrorn;
2104 	if (!(active & GERROR_ERR_MASK))
2105 		return IRQ_NONE; /* No errors pending */
2106 
2107 	dev_warn(smmu->dev,
2108 		 "unexpected global error reported (0x%08x), this could be serious\n",
2109 		 active);
2110 
2111 	if (active & GERROR_SFM_ERR) {
2112 		dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
2113 		arm_smmu_device_disable(smmu);
2114 	}
2115 
2116 	if (active & GERROR_MSI_GERROR_ABT_ERR)
2117 		dev_warn(smmu->dev, "GERROR MSI write aborted\n");
2118 
2119 	if (active & GERROR_MSI_PRIQ_ABT_ERR)
2120 		dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
2121 
2122 	if (active & GERROR_MSI_EVTQ_ABT_ERR)
2123 		dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
2124 
2125 	if (active & GERROR_MSI_CMDQ_ABT_ERR)
2126 		dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
2127 
2128 	if (active & GERROR_PRIQ_ABT_ERR)
2129 		dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
2130 
2131 	if (active & GERROR_EVTQ_ABT_ERR)
2132 		dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
2133 
2134 	if (active & GERROR_CMDQ_ERR)
2135 		arm_smmu_cmdq_skip_err(smmu);
2136 
2137 	writel(gerror, smmu->base + ARM_SMMU_GERRORN);
2138 	return IRQ_HANDLED;
2139 }
2140 
arm_smmu_combined_irq_thread(int irq,void * dev)2141 static irqreturn_t arm_smmu_combined_irq_thread(int irq, void *dev)
2142 {
2143 	struct arm_smmu_device *smmu = dev;
2144 
2145 	arm_smmu_evtq_thread(irq, dev);
2146 	if (smmu->features & ARM_SMMU_FEAT_PRI)
2147 		arm_smmu_priq_thread(irq, dev);
2148 
2149 	return IRQ_HANDLED;
2150 }
2151 
arm_smmu_combined_irq_handler(int irq,void * dev)2152 static irqreturn_t arm_smmu_combined_irq_handler(int irq, void *dev)
2153 {
2154 	arm_smmu_gerror_handler(irq, dev);
2155 	return IRQ_WAKE_THREAD;
2156 }
2157 
2158 static void
arm_smmu_atc_inv_to_cmd(int ssid,unsigned long iova,size_t size,struct arm_smmu_cmdq_ent * cmd)2159 arm_smmu_atc_inv_to_cmd(int ssid, unsigned long iova, size_t size,
2160 			struct arm_smmu_cmdq_ent *cmd)
2161 {
2162 	size_t log2_span;
2163 	size_t span_mask;
2164 	/* ATC invalidates are always on 4096-bytes pages */
2165 	size_t inval_grain_shift = 12;
2166 	unsigned long page_start, page_end;
2167 
2168 	/*
2169 	 * ATS and PASID:
2170 	 *
2171 	 * If substream_valid is clear, the PCIe TLP is sent without a PASID
2172 	 * prefix. In that case all ATC entries within the address range are
2173 	 * invalidated, including those that were requested with a PASID! There
2174 	 * is no way to invalidate only entries without PASID.
2175 	 *
2176 	 * When using STRTAB_STE_1_S1DSS_SSID0 (reserving CD 0 for non-PASID
2177 	 * traffic), translation requests without PASID create ATC entries
2178 	 * without PASID, which must be invalidated with substream_valid clear.
2179 	 * This has the unpleasant side-effect of invalidating all PASID-tagged
2180 	 * ATC entries within the address range.
2181 	 */
2182 	*cmd = (struct arm_smmu_cmdq_ent) {
2183 		.opcode			= CMDQ_OP_ATC_INV,
2184 		.substream_valid	= (ssid != IOMMU_NO_PASID),
2185 		.atc.ssid		= ssid,
2186 	};
2187 
2188 	if (!size) {
2189 		cmd->atc.size = ATC_INV_SIZE_ALL;
2190 		return;
2191 	}
2192 
2193 	page_start	= iova >> inval_grain_shift;
2194 	page_end	= (iova + size - 1) >> inval_grain_shift;
2195 
2196 	/*
2197 	 * In an ATS Invalidate Request, the address must be aligned on the
2198 	 * range size, which must be a power of two number of page sizes. We
2199 	 * thus have to choose between grossly over-invalidating the region, or
2200 	 * splitting the invalidation into multiple commands. For simplicity
2201 	 * we'll go with the first solution, but should refine it in the future
2202 	 * if multiple commands are shown to be more efficient.
2203 	 *
2204 	 * Find the smallest power of two that covers the range. The most
2205 	 * significant differing bit between the start and end addresses,
2206 	 * fls(start ^ end), indicates the required span. For example:
2207 	 *
2208 	 * We want to invalidate pages [8; 11]. This is already the ideal range:
2209 	 *		x = 0b1000 ^ 0b1011 = 0b11
2210 	 *		span = 1 << fls(x) = 4
2211 	 *
2212 	 * To invalidate pages [7; 10], we need to invalidate [0; 15]:
2213 	 *		x = 0b0111 ^ 0b1010 = 0b1101
2214 	 *		span = 1 << fls(x) = 16
2215 	 */
2216 	log2_span	= fls_long(page_start ^ page_end);
2217 	span_mask	= (1ULL << log2_span) - 1;
2218 
2219 	page_start	&= ~span_mask;
2220 
2221 	cmd->atc.addr	= page_start << inval_grain_shift;
2222 	cmd->atc.size	= log2_span;
2223 }
2224 
arm_smmu_atc_inv_master(struct arm_smmu_master * master,ioasid_t ssid)2225 static int arm_smmu_atc_inv_master(struct arm_smmu_master *master,
2226 				   ioasid_t ssid)
2227 {
2228 	int i;
2229 	struct arm_smmu_cmdq_ent cmd;
2230 	struct arm_smmu_cmdq_batch cmds;
2231 
2232 	arm_smmu_atc_inv_to_cmd(ssid, 0, 0, &cmd);
2233 
2234 	arm_smmu_cmdq_batch_init(master->smmu, &cmds, &cmd);
2235 	for (i = 0; i < master->num_streams; i++) {
2236 		cmd.atc.sid = master->streams[i].id;
2237 		arm_smmu_cmdq_batch_add(master->smmu, &cmds, &cmd);
2238 	}
2239 
2240 	return arm_smmu_cmdq_batch_submit(master->smmu, &cmds);
2241 }
2242 
arm_smmu_atc_inv_domain(struct arm_smmu_domain * smmu_domain,unsigned long iova,size_t size)2243 int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain,
2244 			    unsigned long iova, size_t size)
2245 {
2246 	struct arm_smmu_master_domain *master_domain;
2247 	int i;
2248 	unsigned long flags;
2249 	struct arm_smmu_cmdq_ent cmd = {
2250 		.opcode = CMDQ_OP_ATC_INV,
2251 	};
2252 	struct arm_smmu_cmdq_batch cmds;
2253 
2254 	if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_ATS))
2255 		return 0;
2256 
2257 	/*
2258 	 * Ensure that we've completed prior invalidation of the main TLBs
2259 	 * before we read 'nr_ats_masters' in case of a concurrent call to
2260 	 * arm_smmu_enable_ats():
2261 	 *
2262 	 *	// unmap()			// arm_smmu_enable_ats()
2263 	 *	TLBI+SYNC			atomic_inc(&nr_ats_masters);
2264 	 *	smp_mb();			[...]
2265 	 *	atomic_read(&nr_ats_masters);	pci_enable_ats() // writel()
2266 	 *
2267 	 * Ensures that we always see the incremented 'nr_ats_masters' count if
2268 	 * ATS was enabled at the PCI device before completion of the TLBI.
2269 	 */
2270 	smp_mb();
2271 	if (!atomic_read(&smmu_domain->nr_ats_masters))
2272 		return 0;
2273 
2274 	arm_smmu_cmdq_batch_init(smmu_domain->smmu, &cmds, &cmd);
2275 
2276 	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2277 	list_for_each_entry(master_domain, &smmu_domain->devices,
2278 			    devices_elm) {
2279 		struct arm_smmu_master *master = master_domain->master;
2280 
2281 		if (!master->ats_enabled)
2282 			continue;
2283 
2284 		if (master_domain->nested_ats_flush) {
2285 			/*
2286 			 * If a S2 used as a nesting parent is changed we have
2287 			 * no option but to completely flush the ATC.
2288 			 */
2289 			arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd);
2290 		} else {
2291 			arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size,
2292 						&cmd);
2293 		}
2294 
2295 		for (i = 0; i < master->num_streams; i++) {
2296 			cmd.atc.sid = master->streams[i].id;
2297 			arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd);
2298 		}
2299 	}
2300 	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2301 
2302 	return arm_smmu_cmdq_batch_submit(smmu_domain->smmu, &cmds);
2303 }
2304 
2305 /* IO_PGTABLE API */
arm_smmu_tlb_inv_context(void * cookie)2306 static void arm_smmu_tlb_inv_context(void *cookie)
2307 {
2308 	struct arm_smmu_domain *smmu_domain = cookie;
2309 	struct arm_smmu_device *smmu = smmu_domain->smmu;
2310 	struct arm_smmu_cmdq_ent cmd;
2311 
2312 	/*
2313 	 * NOTE: when io-pgtable is in non-strict mode, we may get here with
2314 	 * PTEs previously cleared by unmaps on the current CPU not yet visible
2315 	 * to the SMMU. We are relying on the dma_wmb() implicit during cmd
2316 	 * insertion to guarantee those are observed before the TLBI. Do be
2317 	 * careful, 007.
2318 	 */
2319 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
2320 		arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid);
2321 	} else {
2322 		cmd.opcode	= CMDQ_OP_TLBI_S12_VMALL;
2323 		cmd.tlbi.vmid	= smmu_domain->s2_cfg.vmid;
2324 		arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd);
2325 	}
2326 	arm_smmu_atc_inv_domain(smmu_domain, 0, 0);
2327 }
2328 
__arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent * cmd,unsigned long iova,size_t size,size_t granule,struct arm_smmu_domain * smmu_domain)2329 static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd,
2330 				     unsigned long iova, size_t size,
2331 				     size_t granule,
2332 				     struct arm_smmu_domain *smmu_domain)
2333 {
2334 	struct arm_smmu_device *smmu = smmu_domain->smmu;
2335 	unsigned long end = iova + size, num_pages = 0, tg = 0;
2336 	size_t inv_range = granule;
2337 	struct arm_smmu_cmdq_batch cmds;
2338 
2339 	if (!size)
2340 		return;
2341 
2342 	if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) {
2343 		/* Get the leaf page size */
2344 		tg = __ffs(smmu_domain->domain.pgsize_bitmap);
2345 
2346 		num_pages = size >> tg;
2347 
2348 		/* Convert page size of 12,14,16 (log2) to 1,2,3 */
2349 		cmd->tlbi.tg = (tg - 10) / 2;
2350 
2351 		/*
2352 		 * Determine what level the granule is at. For non-leaf, both
2353 		 * io-pgtable and SVA pass a nominal last-level granule because
2354 		 * they don't know what level(s) actually apply, so ignore that
2355 		 * and leave TTL=0. However for various errata reasons we still
2356 		 * want to use a range command, so avoid the SVA corner case
2357 		 * where both scale and num could be 0 as well.
2358 		 */
2359 		if (cmd->tlbi.leaf)
2360 			cmd->tlbi.ttl = 4 - ((ilog2(granule) - 3) / (tg - 3));
2361 		else if ((num_pages & CMDQ_TLBI_RANGE_NUM_MAX) == 1)
2362 			num_pages++;
2363 	}
2364 
2365 	arm_smmu_cmdq_batch_init(smmu, &cmds, cmd);
2366 
2367 	while (iova < end) {
2368 		if (smmu->features & ARM_SMMU_FEAT_RANGE_INV) {
2369 			/*
2370 			 * On each iteration of the loop, the range is 5 bits
2371 			 * worth of the aligned size remaining.
2372 			 * The range in pages is:
2373 			 *
2374 			 * range = (num_pages & (0x1f << __ffs(num_pages)))
2375 			 */
2376 			unsigned long scale, num;
2377 
2378 			/* Determine the power of 2 multiple number of pages */
2379 			scale = __ffs(num_pages);
2380 			cmd->tlbi.scale = scale;
2381 
2382 			/* Determine how many chunks of 2^scale size we have */
2383 			num = (num_pages >> scale) & CMDQ_TLBI_RANGE_NUM_MAX;
2384 			cmd->tlbi.num = num - 1;
2385 
2386 			/* range is num * 2^scale * pgsize */
2387 			inv_range = num << (scale + tg);
2388 
2389 			/* Clear out the lower order bits for the next iteration */
2390 			num_pages -= num << scale;
2391 		}
2392 
2393 		cmd->tlbi.addr = iova;
2394 		arm_smmu_cmdq_batch_add(smmu, &cmds, cmd);
2395 		iova += inv_range;
2396 	}
2397 	arm_smmu_cmdq_batch_submit(smmu, &cmds);
2398 }
2399 
arm_smmu_tlb_inv_range_domain(unsigned long iova,size_t size,size_t granule,bool leaf,struct arm_smmu_domain * smmu_domain)2400 static void arm_smmu_tlb_inv_range_domain(unsigned long iova, size_t size,
2401 					  size_t granule, bool leaf,
2402 					  struct arm_smmu_domain *smmu_domain)
2403 {
2404 	struct arm_smmu_cmdq_ent cmd = {
2405 		.tlbi = {
2406 			.leaf	= leaf,
2407 		},
2408 	};
2409 
2410 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
2411 		cmd.opcode	= smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ?
2412 				  CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA;
2413 		cmd.tlbi.asid	= smmu_domain->cd.asid;
2414 	} else {
2415 		cmd.opcode	= CMDQ_OP_TLBI_S2_IPA;
2416 		cmd.tlbi.vmid	= smmu_domain->s2_cfg.vmid;
2417 	}
2418 	__arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain);
2419 
2420 	if (smmu_domain->nest_parent) {
2421 		/*
2422 		 * When the S2 domain changes all the nested S1 ASIDs have to be
2423 		 * flushed too.
2424 		 */
2425 		cmd.opcode = CMDQ_OP_TLBI_NH_ALL;
2426 		arm_smmu_cmdq_issue_cmd_with_sync(smmu_domain->smmu, &cmd);
2427 	}
2428 
2429 	/*
2430 	 * Unfortunately, this can't be leaf-only since we may have
2431 	 * zapped an entire table.
2432 	 */
2433 	arm_smmu_atc_inv_domain(smmu_domain, iova, size);
2434 }
2435 
arm_smmu_tlb_inv_range_asid(unsigned long iova,size_t size,int asid,size_t granule,bool leaf,struct arm_smmu_domain * smmu_domain)2436 void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,
2437 				 size_t granule, bool leaf,
2438 				 struct arm_smmu_domain *smmu_domain)
2439 {
2440 	struct arm_smmu_cmdq_ent cmd = {
2441 		.opcode	= smmu_domain->smmu->features & ARM_SMMU_FEAT_E2H ?
2442 			  CMDQ_OP_TLBI_EL2_VA : CMDQ_OP_TLBI_NH_VA,
2443 		.tlbi = {
2444 			.asid	= asid,
2445 			.leaf	= leaf,
2446 		},
2447 	};
2448 
2449 	__arm_smmu_tlb_inv_range(&cmd, iova, size, granule, smmu_domain);
2450 }
2451 
arm_smmu_tlb_inv_page_nosync(struct iommu_iotlb_gather * gather,unsigned long iova,size_t granule,void * cookie)2452 static void arm_smmu_tlb_inv_page_nosync(struct iommu_iotlb_gather *gather,
2453 					 unsigned long iova, size_t granule,
2454 					 void *cookie)
2455 {
2456 	struct arm_smmu_domain *smmu_domain = cookie;
2457 	struct iommu_domain *domain = &smmu_domain->domain;
2458 
2459 	iommu_iotlb_gather_add_page(domain, gather, iova, granule);
2460 }
2461 
arm_smmu_tlb_inv_walk(unsigned long iova,size_t size,size_t granule,void * cookie)2462 static void arm_smmu_tlb_inv_walk(unsigned long iova, size_t size,
2463 				  size_t granule, void *cookie)
2464 {
2465 	arm_smmu_tlb_inv_range_domain(iova, size, granule, false, cookie);
2466 }
2467 
2468 static const struct iommu_flush_ops arm_smmu_flush_ops = {
2469 	.tlb_flush_all	= arm_smmu_tlb_inv_context,
2470 	.tlb_flush_walk = arm_smmu_tlb_inv_walk,
2471 	.tlb_add_page	= arm_smmu_tlb_inv_page_nosync,
2472 };
2473 
arm_smmu_dbm_capable(struct arm_smmu_device * smmu)2474 static bool arm_smmu_dbm_capable(struct arm_smmu_device *smmu)
2475 {
2476 	u32 features = (ARM_SMMU_FEAT_HD | ARM_SMMU_FEAT_COHERENCY);
2477 
2478 	return (smmu->features & features) == features;
2479 }
2480 
2481 /* IOMMU API */
arm_smmu_capable(struct device * dev,enum iommu_cap cap)2482 static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap)
2483 {
2484 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
2485 
2486 	switch (cap) {
2487 	case IOMMU_CAP_CACHE_COHERENCY:
2488 		/* Assume that a coherent TCU implies coherent TBUs */
2489 		return master->smmu->features & ARM_SMMU_FEAT_COHERENCY;
2490 	case IOMMU_CAP_ENFORCE_CACHE_COHERENCY:
2491 		return arm_smmu_master_canwbs(master);
2492 	case IOMMU_CAP_NOEXEC:
2493 	case IOMMU_CAP_DEFERRED_FLUSH:
2494 		return true;
2495 	case IOMMU_CAP_DIRTY_TRACKING:
2496 		return arm_smmu_dbm_capable(master->smmu);
2497 	default:
2498 		return false;
2499 	}
2500 }
2501 
arm_smmu_enforce_cache_coherency(struct iommu_domain * domain)2502 static bool arm_smmu_enforce_cache_coherency(struct iommu_domain *domain)
2503 {
2504 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2505 	struct arm_smmu_master_domain *master_domain;
2506 	unsigned long flags;
2507 	bool ret = true;
2508 
2509 	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2510 	list_for_each_entry(master_domain, &smmu_domain->devices,
2511 			    devices_elm) {
2512 		if (!arm_smmu_master_canwbs(master_domain->master)) {
2513 			ret = false;
2514 			break;
2515 		}
2516 	}
2517 	smmu_domain->enforce_cache_coherency = ret;
2518 	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2519 	return ret;
2520 }
2521 
arm_smmu_domain_alloc(void)2522 struct arm_smmu_domain *arm_smmu_domain_alloc(void)
2523 {
2524 	struct arm_smmu_domain *smmu_domain;
2525 
2526 	smmu_domain = kzalloc_obj(*smmu_domain);
2527 	if (!smmu_domain)
2528 		return ERR_PTR(-ENOMEM);
2529 
2530 	INIT_LIST_HEAD(&smmu_domain->devices);
2531 	spin_lock_init(&smmu_domain->devices_lock);
2532 
2533 	return smmu_domain;
2534 }
2535 
arm_smmu_domain_free_paging(struct iommu_domain * domain)2536 static void arm_smmu_domain_free_paging(struct iommu_domain *domain)
2537 {
2538 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2539 	struct arm_smmu_device *smmu = smmu_domain->smmu;
2540 
2541 	free_io_pgtable_ops(smmu_domain->pgtbl_ops);
2542 
2543 	/* Free the ASID or VMID */
2544 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
2545 		/* Prevent SVA from touching the CD while we're freeing it */
2546 		mutex_lock(&arm_smmu_asid_lock);
2547 		xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid);
2548 		mutex_unlock(&arm_smmu_asid_lock);
2549 	} else {
2550 		struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
2551 		if (cfg->vmid)
2552 			ida_free(&smmu->vmid_map, cfg->vmid);
2553 	}
2554 
2555 	kfree(smmu_domain);
2556 }
2557 
arm_smmu_domain_finalise_s1(struct arm_smmu_device * smmu,struct arm_smmu_domain * smmu_domain)2558 static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu,
2559 				       struct arm_smmu_domain *smmu_domain)
2560 {
2561 	int ret;
2562 	u32 asid = 0;
2563 	struct arm_smmu_ctx_desc *cd = &smmu_domain->cd;
2564 
2565 	/* Prevent SVA from modifying the ASID until it is written to the CD */
2566 	mutex_lock(&arm_smmu_asid_lock);
2567 	ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain,
2568 		       XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL);
2569 	cd->asid	= (u16)asid;
2570 	mutex_unlock(&arm_smmu_asid_lock);
2571 	return ret;
2572 }
2573 
arm_smmu_domain_finalise_s2(struct arm_smmu_device * smmu,struct arm_smmu_domain * smmu_domain)2574 static int arm_smmu_domain_finalise_s2(struct arm_smmu_device *smmu,
2575 				       struct arm_smmu_domain *smmu_domain)
2576 {
2577 	int vmid;
2578 	struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
2579 
2580 	/* Reserve VMID 0 for stage-2 bypass STEs */
2581 	vmid = ida_alloc_range(&smmu->vmid_map, 1, (1 << smmu->vmid_bits) - 1,
2582 			       GFP_KERNEL);
2583 	if (vmid < 0)
2584 		return vmid;
2585 
2586 	cfg->vmid	= (u16)vmid;
2587 	return 0;
2588 }
2589 
arm_smmu_domain_finalise(struct arm_smmu_domain * smmu_domain,struct arm_smmu_device * smmu,u32 flags)2590 static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain,
2591 				    struct arm_smmu_device *smmu, u32 flags)
2592 {
2593 	int ret;
2594 	enum io_pgtable_fmt fmt;
2595 	struct io_pgtable_cfg pgtbl_cfg;
2596 	struct io_pgtable_ops *pgtbl_ops;
2597 	int (*finalise_stage_fn)(struct arm_smmu_device *smmu,
2598 				 struct arm_smmu_domain *smmu_domain);
2599 	bool enable_dirty = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
2600 
2601 	pgtbl_cfg = (struct io_pgtable_cfg) {
2602 		.pgsize_bitmap	= smmu->pgsize_bitmap,
2603 		.coherent_walk	= smmu->features & ARM_SMMU_FEAT_COHERENCY,
2604 		.tlb		= &arm_smmu_flush_ops,
2605 		.iommu_dev	= smmu->dev,
2606 	};
2607 
2608 	switch (smmu_domain->stage) {
2609 	case ARM_SMMU_DOMAIN_S1: {
2610 		unsigned long ias = (smmu->features &
2611 				     ARM_SMMU_FEAT_VAX) ? 52 : 48;
2612 
2613 		pgtbl_cfg.ias = min_t(unsigned long, ias, VA_BITS);
2614 		pgtbl_cfg.oas = smmu->oas;
2615 		if (enable_dirty)
2616 			pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_ARM_HD;
2617 		fmt = ARM_64_LPAE_S1;
2618 		finalise_stage_fn = arm_smmu_domain_finalise_s1;
2619 		break;
2620 	}
2621 	case ARM_SMMU_DOMAIN_S2:
2622 		if (enable_dirty)
2623 			return -EOPNOTSUPP;
2624 		pgtbl_cfg.ias = smmu->oas;
2625 		pgtbl_cfg.oas = smmu->oas;
2626 		fmt = ARM_64_LPAE_S2;
2627 		finalise_stage_fn = arm_smmu_domain_finalise_s2;
2628 		if ((smmu->features & ARM_SMMU_FEAT_S2FWB) &&
2629 		    (flags & IOMMU_HWPT_ALLOC_NEST_PARENT))
2630 			pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_ARM_S2FWB;
2631 		break;
2632 	default:
2633 		return -EINVAL;
2634 	}
2635 
2636 	pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
2637 	if (!pgtbl_ops)
2638 		return -ENOMEM;
2639 
2640 	smmu_domain->domain.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
2641 	smmu_domain->domain.geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1;
2642 	smmu_domain->domain.geometry.force_aperture = true;
2643 	if (enable_dirty && smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
2644 		smmu_domain->domain.dirty_ops = &arm_smmu_dirty_ops;
2645 
2646 	ret = finalise_stage_fn(smmu, smmu_domain);
2647 	if (ret < 0) {
2648 		free_io_pgtable_ops(pgtbl_ops);
2649 		return ret;
2650 	}
2651 
2652 	smmu_domain->pgtbl_ops = pgtbl_ops;
2653 	smmu_domain->smmu = smmu;
2654 	return 0;
2655 }
2656 
2657 static struct arm_smmu_ste *
arm_smmu_get_step_for_sid(struct arm_smmu_device * smmu,u32 sid)2658 arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
2659 {
2660 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2661 
2662 	if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
2663 		/* Two-level walk */
2664 		return &cfg->l2.l2ptrs[arm_smmu_strtab_l1_idx(sid)]
2665 				->stes[arm_smmu_strtab_l2_idx(sid)];
2666 	} else {
2667 		/* Simple linear lookup */
2668 		return &cfg->linear.table[sid];
2669 	}
2670 }
2671 
arm_smmu_install_ste_for_dev(struct arm_smmu_master * master,const struct arm_smmu_ste * target)2672 void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master,
2673 				  const struct arm_smmu_ste *target)
2674 {
2675 	int i, j;
2676 	struct arm_smmu_device *smmu = master->smmu;
2677 
2678 	master->cd_table.in_ste =
2679 		FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(target->data[0])) ==
2680 		STRTAB_STE_0_CFG_S1_TRANS;
2681 	master->ste_ats_enabled =
2682 		FIELD_GET(STRTAB_STE_1_EATS, le64_to_cpu(target->data[1])) ==
2683 		STRTAB_STE_1_EATS_TRANS;
2684 
2685 	for (i = 0; i < master->num_streams; ++i) {
2686 		u32 sid = master->streams[i].id;
2687 		struct arm_smmu_ste *step =
2688 			arm_smmu_get_step_for_sid(smmu, sid);
2689 
2690 		/* Bridged PCI devices may end up with duplicated IDs */
2691 		for (j = 0; j < i; j++)
2692 			if (master->streams[j].id == sid)
2693 				break;
2694 		if (j < i)
2695 			continue;
2696 
2697 		arm_smmu_write_ste(master, sid, step, target);
2698 	}
2699 }
2700 
arm_smmu_ats_supported(struct arm_smmu_master * master)2701 static bool arm_smmu_ats_supported(struct arm_smmu_master *master)
2702 {
2703 	struct device *dev = master->dev;
2704 	struct arm_smmu_device *smmu = master->smmu;
2705 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2706 
2707 	if (!(smmu->features & ARM_SMMU_FEAT_ATS))
2708 		return false;
2709 
2710 	if (!(fwspec->flags & IOMMU_FWSPEC_PCI_RC_ATS))
2711 		return false;
2712 
2713 	return dev_is_pci(dev) && pci_ats_supported(to_pci_dev(dev));
2714 }
2715 
arm_smmu_enable_ats(struct arm_smmu_master * master)2716 static void arm_smmu_enable_ats(struct arm_smmu_master *master)
2717 {
2718 	size_t stu;
2719 	struct pci_dev *pdev;
2720 	struct arm_smmu_device *smmu = master->smmu;
2721 
2722 	/* Smallest Translation Unit: log2 of the smallest supported granule */
2723 	stu = __ffs(smmu->pgsize_bitmap);
2724 	pdev = to_pci_dev(master->dev);
2725 
2726 	/*
2727 	 * ATC invalidation of PASID 0 causes the entire ATC to be flushed.
2728 	 */
2729 	arm_smmu_atc_inv_master(master, IOMMU_NO_PASID);
2730 	if (pci_enable_ats(pdev, stu))
2731 		dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu);
2732 }
2733 
arm_smmu_enable_pasid(struct arm_smmu_master * master)2734 static int arm_smmu_enable_pasid(struct arm_smmu_master *master)
2735 {
2736 	int ret;
2737 	int features;
2738 	int num_pasids;
2739 	struct pci_dev *pdev;
2740 
2741 	if (!dev_is_pci(master->dev))
2742 		return -ENODEV;
2743 
2744 	pdev = to_pci_dev(master->dev);
2745 
2746 	features = pci_pasid_features(pdev);
2747 	if (features < 0)
2748 		return features;
2749 
2750 	num_pasids = pci_max_pasids(pdev);
2751 	if (num_pasids <= 0)
2752 		return num_pasids;
2753 
2754 	ret = pci_enable_pasid(pdev, features);
2755 	if (ret) {
2756 		dev_err(&pdev->dev, "Failed to enable PASID\n");
2757 		return ret;
2758 	}
2759 
2760 	master->ssid_bits = min_t(u8, ilog2(num_pasids),
2761 				  master->smmu->ssid_bits);
2762 	return 0;
2763 }
2764 
arm_smmu_disable_pasid(struct arm_smmu_master * master)2765 static void arm_smmu_disable_pasid(struct arm_smmu_master *master)
2766 {
2767 	struct pci_dev *pdev;
2768 
2769 	if (!dev_is_pci(master->dev))
2770 		return;
2771 
2772 	pdev = to_pci_dev(master->dev);
2773 
2774 	if (!pdev->pasid_enabled)
2775 		return;
2776 
2777 	master->ssid_bits = 0;
2778 	pci_disable_pasid(pdev);
2779 }
2780 
2781 static struct arm_smmu_master_domain *
arm_smmu_find_master_domain(struct arm_smmu_domain * smmu_domain,struct iommu_domain * domain,struct arm_smmu_master * master,ioasid_t ssid,bool nested_ats_flush)2782 arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain,
2783 			    struct iommu_domain *domain,
2784 			    struct arm_smmu_master *master,
2785 			    ioasid_t ssid, bool nested_ats_flush)
2786 {
2787 	struct arm_smmu_master_domain *master_domain;
2788 
2789 	lockdep_assert_held(&smmu_domain->devices_lock);
2790 
2791 	list_for_each_entry(master_domain, &smmu_domain->devices,
2792 			    devices_elm) {
2793 		if (master_domain->master == master &&
2794 		    master_domain->domain == domain &&
2795 		    master_domain->ssid == ssid &&
2796 		    master_domain->nested_ats_flush == nested_ats_flush)
2797 			return master_domain;
2798 	}
2799 	return NULL;
2800 }
2801 
2802 /*
2803  * If the domain uses the smmu_domain->devices list return the arm_smmu_domain
2804  * structure, otherwise NULL. These domains track attached devices so they can
2805  * issue invalidations.
2806  */
2807 static struct arm_smmu_domain *
to_smmu_domain_devices(struct iommu_domain * domain)2808 to_smmu_domain_devices(struct iommu_domain *domain)
2809 {
2810 	/* The domain can be NULL only when processing the first attach */
2811 	if (!domain)
2812 		return NULL;
2813 	if ((domain->type & __IOMMU_DOMAIN_PAGING) ||
2814 	    domain->type == IOMMU_DOMAIN_SVA)
2815 		return to_smmu_domain(domain);
2816 	if (domain->type == IOMMU_DOMAIN_NESTED)
2817 		return to_smmu_nested_domain(domain)->vsmmu->s2_parent;
2818 	return NULL;
2819 }
2820 
arm_smmu_enable_iopf(struct arm_smmu_master * master,struct arm_smmu_master_domain * master_domain)2821 static int arm_smmu_enable_iopf(struct arm_smmu_master *master,
2822 				struct arm_smmu_master_domain *master_domain)
2823 {
2824 	int ret;
2825 
2826 	iommu_group_mutex_assert(master->dev);
2827 
2828 	if (!IS_ENABLED(CONFIG_ARM_SMMU_V3_SVA))
2829 		return -EOPNOTSUPP;
2830 
2831 	/*
2832 	 * Drivers for devices supporting PRI or stall require iopf others have
2833 	 * device-specific fault handlers and don't need IOPF, so this is not a
2834 	 * failure.
2835 	 */
2836 	if (!master->stall_enabled)
2837 		return 0;
2838 
2839 	/* We're not keeping track of SIDs in fault events */
2840 	if (master->num_streams != 1)
2841 		return -EOPNOTSUPP;
2842 
2843 	if (master->iopf_refcount) {
2844 		master->iopf_refcount++;
2845 		master_domain->using_iopf = true;
2846 		return 0;
2847 	}
2848 
2849 	ret = iopf_queue_add_device(master->smmu->evtq.iopf, master->dev);
2850 	if (ret)
2851 		return ret;
2852 	master->iopf_refcount = 1;
2853 	master_domain->using_iopf = true;
2854 	return 0;
2855 }
2856 
arm_smmu_disable_iopf(struct arm_smmu_master * master,struct arm_smmu_master_domain * master_domain)2857 static void arm_smmu_disable_iopf(struct arm_smmu_master *master,
2858 				  struct arm_smmu_master_domain *master_domain)
2859 {
2860 	iommu_group_mutex_assert(master->dev);
2861 
2862 	if (!IS_ENABLED(CONFIG_ARM_SMMU_V3_SVA))
2863 		return;
2864 
2865 	if (!master_domain || !master_domain->using_iopf)
2866 		return;
2867 
2868 	master->iopf_refcount--;
2869 	if (master->iopf_refcount == 0)
2870 		iopf_queue_remove_device(master->smmu->evtq.iopf, master->dev);
2871 }
2872 
arm_smmu_remove_master_domain(struct arm_smmu_master * master,struct iommu_domain * domain,ioasid_t ssid)2873 static void arm_smmu_remove_master_domain(struct arm_smmu_master *master,
2874 					  struct iommu_domain *domain,
2875 					  ioasid_t ssid)
2876 {
2877 	struct arm_smmu_domain *smmu_domain = to_smmu_domain_devices(domain);
2878 	struct arm_smmu_master_domain *master_domain;
2879 	bool nested_ats_flush = false;
2880 	unsigned long flags;
2881 
2882 	if (!smmu_domain)
2883 		return;
2884 
2885 	if (domain->type == IOMMU_DOMAIN_NESTED)
2886 		nested_ats_flush = to_smmu_nested_domain(domain)->enable_ats;
2887 
2888 	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2889 	master_domain = arm_smmu_find_master_domain(smmu_domain, domain, master,
2890 						    ssid, nested_ats_flush);
2891 	if (master_domain) {
2892 		list_del(&master_domain->devices_elm);
2893 		if (master->ats_enabled)
2894 			atomic_dec(&smmu_domain->nr_ats_masters);
2895 	}
2896 	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2897 
2898 	arm_smmu_disable_iopf(master, master_domain);
2899 	kfree(master_domain);
2900 }
2901 
2902 /*
2903  * Start the sequence to attach a domain to a master. The sequence contains three
2904  * steps:
2905  *  arm_smmu_attach_prepare()
2906  *  arm_smmu_install_ste_for_dev()
2907  *  arm_smmu_attach_commit()
2908  *
2909  * If prepare succeeds then the sequence must be completed. The STE installed
2910  * must set the STE.EATS field according to state.ats_enabled.
2911  *
2912  * If the device supports ATS then this determines if EATS should be enabled
2913  * in the STE, and starts sequencing EATS disable if required.
2914  *
2915  * The change of the EATS in the STE and the PCI ATS config space is managed by
2916  * this sequence to be in the right order so that if PCI ATS is enabled then
2917  * STE.ETAS is enabled.
2918  *
2919  * new_domain can be a non-paging domain. In this case ATS will not be enabled,
2920  * and invalidations won't be tracked.
2921  */
arm_smmu_attach_prepare(struct arm_smmu_attach_state * state,struct iommu_domain * new_domain)2922 int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state,
2923 			    struct iommu_domain *new_domain)
2924 {
2925 	struct arm_smmu_master *master = state->master;
2926 	struct arm_smmu_master_domain *master_domain;
2927 	struct arm_smmu_domain *smmu_domain =
2928 		to_smmu_domain_devices(new_domain);
2929 	unsigned long flags;
2930 	int ret;
2931 
2932 	/*
2933 	 * arm_smmu_share_asid() must not see two domains pointing to the same
2934 	 * arm_smmu_master_domain contents otherwise it could randomly write one
2935 	 * or the other to the CD.
2936 	 */
2937 	lockdep_assert_held(&arm_smmu_asid_lock);
2938 
2939 	if (smmu_domain || state->cd_needs_ats) {
2940 		/*
2941 		 * The SMMU does not support enabling ATS with bypass/abort.
2942 		 * When the STE is in bypass (STE.Config[2:0] == 0b100), ATS
2943 		 * Translation Requests and Translated transactions are denied
2944 		 * as though ATS is disabled for the stream (STE.EATS == 0b00),
2945 		 * causing F_BAD_ATS_TREQ and F_TRANSL_FORBIDDEN events
2946 		 * (IHI0070Ea 5.2 Stream Table Entry).
2947 		 *
2948 		 * However, if we have installed a CD table and are using S1DSS
2949 		 * then ATS will work in S1DSS bypass. See "13.6.4 Full ATS
2950 		 * skipping stage 1".
2951 		 *
2952 		 * Disable ATS if we are going to create a normal 0b100 bypass
2953 		 * STE.
2954 		 */
2955 		state->ats_enabled = !state->disable_ats &&
2956 				     arm_smmu_ats_supported(master);
2957 	}
2958 
2959 	if (smmu_domain) {
2960 		if (new_domain->type == IOMMU_DOMAIN_NESTED) {
2961 			ret = arm_smmu_attach_prepare_vmaster(
2962 				state, to_smmu_nested_domain(new_domain));
2963 			if (ret)
2964 				return ret;
2965 		}
2966 
2967 		master_domain = kzalloc_obj(*master_domain);
2968 		if (!master_domain) {
2969 			ret = -ENOMEM;
2970 			goto err_free_vmaster;
2971 		}
2972 		master_domain->domain = new_domain;
2973 		master_domain->master = master;
2974 		master_domain->ssid = state->ssid;
2975 		if (new_domain->type == IOMMU_DOMAIN_NESTED)
2976 			master_domain->nested_ats_flush =
2977 				to_smmu_nested_domain(new_domain)->enable_ats;
2978 
2979 		if (new_domain->iopf_handler) {
2980 			ret = arm_smmu_enable_iopf(master, master_domain);
2981 			if (ret)
2982 				goto err_free_master_domain;
2983 		}
2984 
2985 		/*
2986 		 * During prepare we want the current smmu_domain and new
2987 		 * smmu_domain to be in the devices list before we change any
2988 		 * HW. This ensures that both domains will send ATS
2989 		 * invalidations to the master until we are done.
2990 		 *
2991 		 * It is tempting to make this list only track masters that are
2992 		 * using ATS, but arm_smmu_share_asid() also uses this to change
2993 		 * the ASID of a domain, unrelated to ATS.
2994 		 *
2995 		 * Notice if we are re-attaching the same domain then the list
2996 		 * will have two identical entries and commit will remove only
2997 		 * one of them.
2998 		 */
2999 		spin_lock_irqsave(&smmu_domain->devices_lock, flags);
3000 		if (smmu_domain->enforce_cache_coherency &&
3001 		    !arm_smmu_master_canwbs(master)) {
3002 			spin_unlock_irqrestore(&smmu_domain->devices_lock,
3003 					       flags);
3004 			ret = -EINVAL;
3005 			goto err_iopf;
3006 		}
3007 
3008 		if (state->ats_enabled)
3009 			atomic_inc(&smmu_domain->nr_ats_masters);
3010 		list_add(&master_domain->devices_elm, &smmu_domain->devices);
3011 		spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
3012 	}
3013 
3014 	if (!state->ats_enabled && master->ats_enabled) {
3015 		pci_disable_ats(to_pci_dev(master->dev));
3016 		/*
3017 		 * This is probably overkill, but the config write for disabling
3018 		 * ATS should complete before the STE is configured to generate
3019 		 * UR to avoid AER noise.
3020 		 */
3021 		wmb();
3022 	}
3023 	return 0;
3024 
3025 err_iopf:
3026 	arm_smmu_disable_iopf(master, master_domain);
3027 err_free_master_domain:
3028 	kfree(master_domain);
3029 err_free_vmaster:
3030 	kfree(state->vmaster);
3031 	return ret;
3032 }
3033 
3034 /*
3035  * Commit is done after the STE/CD are configured with the EATS setting. It
3036  * completes synchronizing the PCI device's ATC and finishes manipulating the
3037  * smmu_domain->devices list.
3038  */
arm_smmu_attach_commit(struct arm_smmu_attach_state * state)3039 void arm_smmu_attach_commit(struct arm_smmu_attach_state *state)
3040 {
3041 	struct arm_smmu_master *master = state->master;
3042 
3043 	lockdep_assert_held(&arm_smmu_asid_lock);
3044 
3045 	arm_smmu_attach_commit_vmaster(state);
3046 
3047 	if (state->ats_enabled && !master->ats_enabled) {
3048 		arm_smmu_enable_ats(master);
3049 	} else if (state->ats_enabled && master->ats_enabled) {
3050 		/*
3051 		 * The translation has changed, flush the ATC. At this point the
3052 		 * SMMU is translating for the new domain and both the old&new
3053 		 * domain will issue invalidations.
3054 		 */
3055 		arm_smmu_atc_inv_master(master, state->ssid);
3056 	} else if (!state->ats_enabled && master->ats_enabled) {
3057 		/* ATS is being switched off, invalidate the entire ATC */
3058 		arm_smmu_atc_inv_master(master, IOMMU_NO_PASID);
3059 	}
3060 
3061 	arm_smmu_remove_master_domain(master, state->old_domain, state->ssid);
3062 	master->ats_enabled = state->ats_enabled;
3063 }
3064 
arm_smmu_attach_dev(struct iommu_domain * domain,struct device * dev,struct iommu_domain * old_domain)3065 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev,
3066 			       struct iommu_domain *old_domain)
3067 {
3068 	int ret = 0;
3069 	struct arm_smmu_ste target;
3070 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3071 	struct arm_smmu_device *smmu;
3072 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
3073 	struct arm_smmu_attach_state state = {
3074 		.old_domain = old_domain,
3075 		.ssid = IOMMU_NO_PASID,
3076 	};
3077 	struct arm_smmu_master *master;
3078 	struct arm_smmu_cd *cdptr;
3079 
3080 	if (!fwspec)
3081 		return -ENOENT;
3082 
3083 	state.master = master = dev_iommu_priv_get(dev);
3084 	smmu = master->smmu;
3085 
3086 	if (smmu_domain->smmu != smmu)
3087 		return -EINVAL;
3088 
3089 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
3090 		cdptr = arm_smmu_alloc_cd_ptr(master, IOMMU_NO_PASID);
3091 		if (!cdptr)
3092 			return -ENOMEM;
3093 	} else if (arm_smmu_ssids_in_use(&master->cd_table))
3094 		return -EBUSY;
3095 
3096 	/*
3097 	 * Prevent arm_smmu_share_asid() from trying to change the ASID
3098 	 * of either the old or new domain while we are working on it.
3099 	 * This allows the STE and the smmu_domain->devices list to
3100 	 * be inconsistent during this routine.
3101 	 */
3102 	mutex_lock(&arm_smmu_asid_lock);
3103 
3104 	ret = arm_smmu_attach_prepare(&state, domain);
3105 	if (ret) {
3106 		mutex_unlock(&arm_smmu_asid_lock);
3107 		return ret;
3108 	}
3109 
3110 	switch (smmu_domain->stage) {
3111 	case ARM_SMMU_DOMAIN_S1: {
3112 		struct arm_smmu_cd target_cd;
3113 
3114 		arm_smmu_make_s1_cd(&target_cd, master, smmu_domain);
3115 		arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr,
3116 					&target_cd);
3117 		arm_smmu_make_cdtable_ste(&target, master, state.ats_enabled,
3118 					  STRTAB_STE_1_S1DSS_SSID0);
3119 		arm_smmu_install_ste_for_dev(master, &target);
3120 		break;
3121 	}
3122 	case ARM_SMMU_DOMAIN_S2:
3123 		arm_smmu_make_s2_domain_ste(&target, master, smmu_domain,
3124 					    state.ats_enabled);
3125 		arm_smmu_install_ste_for_dev(master, &target);
3126 		arm_smmu_clear_cd(master, IOMMU_NO_PASID);
3127 		break;
3128 	}
3129 
3130 	arm_smmu_attach_commit(&state);
3131 	mutex_unlock(&arm_smmu_asid_lock);
3132 	return 0;
3133 }
3134 
arm_smmu_s1_set_dev_pasid(struct iommu_domain * domain,struct device * dev,ioasid_t id,struct iommu_domain * old)3135 static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain,
3136 				     struct device *dev, ioasid_t id,
3137 				     struct iommu_domain *old)
3138 {
3139 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
3140 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
3141 	struct arm_smmu_device *smmu = master->smmu;
3142 	struct arm_smmu_cd target_cd;
3143 
3144 	if (smmu_domain->smmu != smmu)
3145 		return -EINVAL;
3146 
3147 	if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1)
3148 		return -EINVAL;
3149 
3150 	/*
3151 	 * We can read cd.asid outside the lock because arm_smmu_set_pasid()
3152 	 * will fix it
3153 	 */
3154 	arm_smmu_make_s1_cd(&target_cd, master, smmu_domain);
3155 	return arm_smmu_set_pasid(master, to_smmu_domain(domain), id,
3156 				  &target_cd, old);
3157 }
3158 
arm_smmu_update_ste(struct arm_smmu_master * master,struct iommu_domain * sid_domain,bool ats_enabled)3159 static void arm_smmu_update_ste(struct arm_smmu_master *master,
3160 				struct iommu_domain *sid_domain,
3161 				bool ats_enabled)
3162 {
3163 	unsigned int s1dss = STRTAB_STE_1_S1DSS_TERMINATE;
3164 	struct arm_smmu_ste ste;
3165 
3166 	if (master->cd_table.in_ste && master->ste_ats_enabled == ats_enabled)
3167 		return;
3168 
3169 	if (sid_domain->type == IOMMU_DOMAIN_IDENTITY)
3170 		s1dss = STRTAB_STE_1_S1DSS_BYPASS;
3171 	else
3172 		WARN_ON(sid_domain->type != IOMMU_DOMAIN_BLOCKED);
3173 
3174 	/*
3175 	 * Change the STE into a cdtable one with SID IDENTITY/BLOCKED behavior
3176 	 * using s1dss if necessary. If the cd_table is already installed then
3177 	 * the S1DSS is correct and this will just update the EATS. Otherwise it
3178 	 * installs the entire thing. This will be hitless.
3179 	 */
3180 	arm_smmu_make_cdtable_ste(&ste, master, ats_enabled, s1dss);
3181 	arm_smmu_install_ste_for_dev(master, &ste);
3182 }
3183 
arm_smmu_set_pasid(struct arm_smmu_master * master,struct arm_smmu_domain * smmu_domain,ioasid_t pasid,struct arm_smmu_cd * cd,struct iommu_domain * old)3184 int arm_smmu_set_pasid(struct arm_smmu_master *master,
3185 		       struct arm_smmu_domain *smmu_domain, ioasid_t pasid,
3186 		       struct arm_smmu_cd *cd, struct iommu_domain *old)
3187 {
3188 	struct iommu_domain *sid_domain =
3189 		iommu_driver_get_domain_for_dev(master->dev);
3190 	struct arm_smmu_attach_state state = {
3191 		.master = master,
3192 		.ssid = pasid,
3193 		.old_domain = old,
3194 	};
3195 	struct arm_smmu_cd *cdptr;
3196 	int ret;
3197 
3198 	/* The core code validates pasid */
3199 
3200 	if (smmu_domain->smmu != master->smmu)
3201 		return -EINVAL;
3202 
3203 	if (!master->cd_table.in_ste &&
3204 	    sid_domain->type != IOMMU_DOMAIN_IDENTITY &&
3205 	    sid_domain->type != IOMMU_DOMAIN_BLOCKED)
3206 		return -EINVAL;
3207 
3208 	cdptr = arm_smmu_alloc_cd_ptr(master, pasid);
3209 	if (!cdptr)
3210 		return -ENOMEM;
3211 
3212 	mutex_lock(&arm_smmu_asid_lock);
3213 	ret = arm_smmu_attach_prepare(&state, &smmu_domain->domain);
3214 	if (ret)
3215 		goto out_unlock;
3216 
3217 	/*
3218 	 * We don't want to obtain to the asid_lock too early, so fix up the
3219 	 * caller set ASID under the lock in case it changed.
3220 	 */
3221 	cd->data[0] &= ~cpu_to_le64(CTXDESC_CD_0_ASID);
3222 	cd->data[0] |= cpu_to_le64(
3223 		FIELD_PREP(CTXDESC_CD_0_ASID, smmu_domain->cd.asid));
3224 
3225 	arm_smmu_write_cd_entry(master, pasid, cdptr, cd);
3226 	arm_smmu_update_ste(master, sid_domain, state.ats_enabled);
3227 
3228 	arm_smmu_attach_commit(&state);
3229 
3230 out_unlock:
3231 	mutex_unlock(&arm_smmu_asid_lock);
3232 	return ret;
3233 }
3234 
arm_smmu_blocking_set_dev_pasid(struct iommu_domain * new_domain,struct device * dev,ioasid_t pasid,struct iommu_domain * old_domain)3235 static int arm_smmu_blocking_set_dev_pasid(struct iommu_domain *new_domain,
3236 					   struct device *dev, ioasid_t pasid,
3237 					   struct iommu_domain *old_domain)
3238 {
3239 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(old_domain);
3240 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
3241 
3242 	mutex_lock(&arm_smmu_asid_lock);
3243 	arm_smmu_clear_cd(master, pasid);
3244 	if (master->ats_enabled)
3245 		arm_smmu_atc_inv_master(master, pasid);
3246 	arm_smmu_remove_master_domain(master, &smmu_domain->domain, pasid);
3247 	mutex_unlock(&arm_smmu_asid_lock);
3248 
3249 	/*
3250 	 * When the last user of the CD table goes away downgrade the STE back
3251 	 * to a non-cd_table one, by re-attaching its sid_domain.
3252 	 */
3253 	if (!arm_smmu_ssids_in_use(&master->cd_table)) {
3254 		struct iommu_domain *sid_domain =
3255 			iommu_driver_get_domain_for_dev(master->dev);
3256 
3257 		if (sid_domain->type == IOMMU_DOMAIN_IDENTITY ||
3258 		    sid_domain->type == IOMMU_DOMAIN_BLOCKED)
3259 			sid_domain->ops->attach_dev(sid_domain, dev,
3260 						    sid_domain);
3261 	}
3262 	return 0;
3263 }
3264 
arm_smmu_attach_dev_ste(struct iommu_domain * domain,struct iommu_domain * old_domain,struct device * dev,struct arm_smmu_ste * ste,unsigned int s1dss)3265 static void arm_smmu_attach_dev_ste(struct iommu_domain *domain,
3266 				    struct iommu_domain *old_domain,
3267 				    struct device *dev,
3268 				    struct arm_smmu_ste *ste,
3269 				    unsigned int s1dss)
3270 {
3271 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
3272 	struct arm_smmu_attach_state state = {
3273 		.master = master,
3274 		.old_domain = old_domain,
3275 		.ssid = IOMMU_NO_PASID,
3276 	};
3277 
3278 	/*
3279 	 * Do not allow any ASID to be changed while are working on the STE,
3280 	 * otherwise we could miss invalidations.
3281 	 */
3282 	mutex_lock(&arm_smmu_asid_lock);
3283 
3284 	/*
3285 	 * If the CD table is not in use we can use the provided STE, otherwise
3286 	 * we use a cdtable STE with the provided S1DSS.
3287 	 */
3288 	if (arm_smmu_ssids_in_use(&master->cd_table)) {
3289 		/*
3290 		 * If a CD table has to be present then we need to run with ATS
3291 		 * on because we have to assume a PASID is using ATS. For
3292 		 * IDENTITY this will setup things so that S1DSS=bypass which
3293 		 * follows the explanation in "13.6.4 Full ATS skipping stage 1"
3294 		 * and allows for ATS on the RID to work.
3295 		 */
3296 		state.cd_needs_ats = true;
3297 		arm_smmu_attach_prepare(&state, domain);
3298 		arm_smmu_make_cdtable_ste(ste, master, state.ats_enabled, s1dss);
3299 	} else {
3300 		arm_smmu_attach_prepare(&state, domain);
3301 	}
3302 	arm_smmu_install_ste_for_dev(master, ste);
3303 	arm_smmu_attach_commit(&state);
3304 	mutex_unlock(&arm_smmu_asid_lock);
3305 
3306 	/*
3307 	 * This has to be done after removing the master from the
3308 	 * arm_smmu_domain->devices to avoid races updating the same context
3309 	 * descriptor from arm_smmu_share_asid().
3310 	 */
3311 	arm_smmu_clear_cd(master, IOMMU_NO_PASID);
3312 }
3313 
arm_smmu_attach_dev_identity(struct iommu_domain * domain,struct device * dev,struct iommu_domain * old_domain)3314 static int arm_smmu_attach_dev_identity(struct iommu_domain *domain,
3315 					struct device *dev,
3316 					struct iommu_domain *old_domain)
3317 {
3318 	struct arm_smmu_ste ste;
3319 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
3320 
3321 	arm_smmu_master_clear_vmaster(master);
3322 	arm_smmu_make_bypass_ste(master->smmu, &ste);
3323 	arm_smmu_attach_dev_ste(domain, old_domain, dev, &ste,
3324 				STRTAB_STE_1_S1DSS_BYPASS);
3325 	return 0;
3326 }
3327 
3328 static const struct iommu_domain_ops arm_smmu_identity_ops = {
3329 	.attach_dev = arm_smmu_attach_dev_identity,
3330 };
3331 
3332 static struct iommu_domain arm_smmu_identity_domain = {
3333 	.type = IOMMU_DOMAIN_IDENTITY,
3334 	.ops = &arm_smmu_identity_ops,
3335 };
3336 
arm_smmu_attach_dev_blocked(struct iommu_domain * domain,struct device * dev,struct iommu_domain * old_domain)3337 static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain,
3338 				       struct device *dev,
3339 				       struct iommu_domain *old_domain)
3340 {
3341 	struct arm_smmu_ste ste;
3342 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
3343 
3344 	arm_smmu_master_clear_vmaster(master);
3345 	arm_smmu_make_abort_ste(&ste);
3346 	arm_smmu_attach_dev_ste(domain, old_domain, dev, &ste,
3347 				STRTAB_STE_1_S1DSS_TERMINATE);
3348 	return 0;
3349 }
3350 
3351 static const struct iommu_domain_ops arm_smmu_blocked_ops = {
3352 	.attach_dev = arm_smmu_attach_dev_blocked,
3353 	.set_dev_pasid = arm_smmu_blocking_set_dev_pasid,
3354 };
3355 
3356 static struct iommu_domain arm_smmu_blocked_domain = {
3357 	.type = IOMMU_DOMAIN_BLOCKED,
3358 	.ops = &arm_smmu_blocked_ops,
3359 };
3360 
3361 static struct iommu_domain *
arm_smmu_domain_alloc_paging_flags(struct device * dev,u32 flags,const struct iommu_user_data * user_data)3362 arm_smmu_domain_alloc_paging_flags(struct device *dev, u32 flags,
3363 				   const struct iommu_user_data *user_data)
3364 {
3365 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
3366 	struct arm_smmu_device *smmu = master->smmu;
3367 	const u32 PAGING_FLAGS = IOMMU_HWPT_ALLOC_DIRTY_TRACKING |
3368 				 IOMMU_HWPT_ALLOC_PASID |
3369 				 IOMMU_HWPT_ALLOC_NEST_PARENT;
3370 	struct arm_smmu_domain *smmu_domain;
3371 	int ret;
3372 
3373 	if (flags & ~PAGING_FLAGS)
3374 		return ERR_PTR(-EOPNOTSUPP);
3375 	if (user_data)
3376 		return ERR_PTR(-EOPNOTSUPP);
3377 
3378 	smmu_domain = arm_smmu_domain_alloc();
3379 	if (IS_ERR(smmu_domain))
3380 		return ERR_CAST(smmu_domain);
3381 
3382 	switch (flags) {
3383 	case 0:
3384 		/* Prefer S1 if available */
3385 		if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
3386 			smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
3387 		else
3388 			smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
3389 		break;
3390 	case IOMMU_HWPT_ALLOC_NEST_PARENT:
3391 		if (!(smmu->features & ARM_SMMU_FEAT_NESTING)) {
3392 			ret = -EOPNOTSUPP;
3393 			goto err_free;
3394 		}
3395 		smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
3396 		smmu_domain->nest_parent = true;
3397 		break;
3398 	case IOMMU_HWPT_ALLOC_DIRTY_TRACKING:
3399 	case IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_ALLOC_PASID:
3400 	case IOMMU_HWPT_ALLOC_PASID:
3401 		if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1)) {
3402 			ret = -EOPNOTSUPP;
3403 			goto err_free;
3404 		}
3405 		smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
3406 		break;
3407 	default:
3408 		ret = -EOPNOTSUPP;
3409 		goto err_free;
3410 	}
3411 
3412 	smmu_domain->domain.type = IOMMU_DOMAIN_UNMANAGED;
3413 	smmu_domain->domain.ops = arm_smmu_ops.default_domain_ops;
3414 	ret = arm_smmu_domain_finalise(smmu_domain, smmu, flags);
3415 	if (ret)
3416 		goto err_free;
3417 	return &smmu_domain->domain;
3418 
3419 err_free:
3420 	kfree(smmu_domain);
3421 	return ERR_PTR(ret);
3422 }
3423 
arm_smmu_map_pages(struct iommu_domain * domain,unsigned long iova,phys_addr_t paddr,size_t pgsize,size_t pgcount,int prot,gfp_t gfp,size_t * mapped)3424 static int arm_smmu_map_pages(struct iommu_domain *domain, unsigned long iova,
3425 			      phys_addr_t paddr, size_t pgsize, size_t pgcount,
3426 			      int prot, gfp_t gfp, size_t *mapped)
3427 {
3428 	struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
3429 
3430 	if (!ops)
3431 		return -ENODEV;
3432 
3433 	return ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp, mapped);
3434 }
3435 
arm_smmu_unmap_pages(struct iommu_domain * domain,unsigned long iova,size_t pgsize,size_t pgcount,struct iommu_iotlb_gather * gather)3436 static size_t arm_smmu_unmap_pages(struct iommu_domain *domain, unsigned long iova,
3437 				   size_t pgsize, size_t pgcount,
3438 				   struct iommu_iotlb_gather *gather)
3439 {
3440 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
3441 	struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
3442 
3443 	if (!ops)
3444 		return 0;
3445 
3446 	return ops->unmap_pages(ops, iova, pgsize, pgcount, gather);
3447 }
3448 
arm_smmu_flush_iotlb_all(struct iommu_domain * domain)3449 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
3450 {
3451 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
3452 
3453 	if (smmu_domain->smmu)
3454 		arm_smmu_tlb_inv_context(smmu_domain);
3455 }
3456 
arm_smmu_iotlb_sync(struct iommu_domain * domain,struct iommu_iotlb_gather * gather)3457 static void arm_smmu_iotlb_sync(struct iommu_domain *domain,
3458 				struct iommu_iotlb_gather *gather)
3459 {
3460 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
3461 
3462 	if (!gather->pgsize)
3463 		return;
3464 
3465 	arm_smmu_tlb_inv_range_domain(gather->start,
3466 				      gather->end - gather->start + 1,
3467 				      gather->pgsize, true, smmu_domain);
3468 }
3469 
3470 static phys_addr_t
arm_smmu_iova_to_phys(struct iommu_domain * domain,dma_addr_t iova)3471 arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
3472 {
3473 	struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
3474 
3475 	if (!ops)
3476 		return 0;
3477 
3478 	return ops->iova_to_phys(ops, iova);
3479 }
3480 
3481 static struct platform_driver arm_smmu_driver;
3482 
3483 static
arm_smmu_get_by_fwnode(struct fwnode_handle * fwnode)3484 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
3485 {
3486 	struct device *dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode);
3487 
3488 	put_device(dev);
3489 	return dev ? dev_get_drvdata(dev) : NULL;
3490 }
3491 
arm_smmu_sid_in_range(struct arm_smmu_device * smmu,u32 sid)3492 static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
3493 {
3494 	if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
3495 		return arm_smmu_strtab_l1_idx(sid) < smmu->strtab_cfg.l2.num_l1_ents;
3496 	return sid < smmu->strtab_cfg.linear.num_ents;
3497 }
3498 
arm_smmu_init_sid_strtab(struct arm_smmu_device * smmu,u32 sid)3499 static int arm_smmu_init_sid_strtab(struct arm_smmu_device *smmu, u32 sid)
3500 {
3501 	/* Check the SIDs are in range of the SMMU and our stream table */
3502 	if (!arm_smmu_sid_in_range(smmu, sid))
3503 		return -ERANGE;
3504 
3505 	/* Ensure l2 strtab is initialised */
3506 	if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
3507 		return arm_smmu_init_l2_strtab(smmu, sid);
3508 
3509 	return 0;
3510 }
3511 
arm_smmu_insert_master(struct arm_smmu_device * smmu,struct arm_smmu_master * master)3512 static int arm_smmu_insert_master(struct arm_smmu_device *smmu,
3513 				  struct arm_smmu_master *master)
3514 {
3515 	int i;
3516 	int ret = 0;
3517 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
3518 
3519 	master->streams = kzalloc_objs(*master->streams, fwspec->num_ids);
3520 	if (!master->streams)
3521 		return -ENOMEM;
3522 	master->num_streams = fwspec->num_ids;
3523 
3524 	mutex_lock(&smmu->streams_mutex);
3525 	for (i = 0; i < fwspec->num_ids; i++) {
3526 		struct arm_smmu_stream *new_stream = &master->streams[i];
3527 		struct rb_node *existing;
3528 		u32 sid = fwspec->ids[i];
3529 
3530 		new_stream->id = sid;
3531 		new_stream->master = master;
3532 
3533 		ret = arm_smmu_init_sid_strtab(smmu, sid);
3534 		if (ret)
3535 			break;
3536 
3537 		/* Insert into SID tree */
3538 		existing = rb_find_add(&new_stream->node, &smmu->streams,
3539 				       arm_smmu_streams_cmp_node);
3540 		if (existing) {
3541 			struct arm_smmu_master *existing_master =
3542 				rb_entry(existing, struct arm_smmu_stream, node)
3543 					->master;
3544 
3545 			/* Bridged PCI devices may end up with duplicated IDs */
3546 			if (existing_master == master)
3547 				continue;
3548 
3549 			dev_warn(master->dev,
3550 				 "Aliasing StreamID 0x%x (from %s) unsupported, expect DMA to be broken\n",
3551 				 sid, dev_name(existing_master->dev));
3552 			ret = -ENODEV;
3553 			break;
3554 		}
3555 	}
3556 
3557 	if (ret) {
3558 		for (i--; i >= 0; i--)
3559 			rb_erase(&master->streams[i].node, &smmu->streams);
3560 		kfree(master->streams);
3561 	}
3562 	mutex_unlock(&smmu->streams_mutex);
3563 
3564 	return ret;
3565 }
3566 
arm_smmu_remove_master(struct arm_smmu_master * master)3567 static void arm_smmu_remove_master(struct arm_smmu_master *master)
3568 {
3569 	int i;
3570 	struct arm_smmu_device *smmu = master->smmu;
3571 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
3572 
3573 	if (!smmu || !master->streams)
3574 		return;
3575 
3576 	mutex_lock(&smmu->streams_mutex);
3577 	for (i = 0; i < fwspec->num_ids; i++)
3578 		rb_erase(&master->streams[i].node, &smmu->streams);
3579 	mutex_unlock(&smmu->streams_mutex);
3580 
3581 	kfree(master->streams);
3582 }
3583 
arm_smmu_probe_device(struct device * dev)3584 static struct iommu_device *arm_smmu_probe_device(struct device *dev)
3585 {
3586 	int ret;
3587 	struct arm_smmu_device *smmu;
3588 	struct arm_smmu_master *master;
3589 	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3590 
3591 	if (WARN_ON_ONCE(dev_iommu_priv_get(dev)))
3592 		return ERR_PTR(-EBUSY);
3593 
3594 	smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
3595 	if (!smmu)
3596 		return ERR_PTR(-ENODEV);
3597 
3598 	master = kzalloc_obj(*master);
3599 	if (!master)
3600 		return ERR_PTR(-ENOMEM);
3601 
3602 	master->dev = dev;
3603 	master->smmu = smmu;
3604 	dev_iommu_priv_set(dev, master);
3605 
3606 	ret = arm_smmu_insert_master(smmu, master);
3607 	if (ret)
3608 		goto err_free_master;
3609 
3610 	device_property_read_u32(dev, "pasid-num-bits", &master->ssid_bits);
3611 	master->ssid_bits = min(smmu->ssid_bits, master->ssid_bits);
3612 
3613 	/*
3614 	 * Note that PASID must be enabled before, and disabled after ATS:
3615 	 * PCI Express Base 4.0r1.0 - 10.5.1.3 ATS Control Register
3616 	 *
3617 	 *   Behavior is undefined if this bit is Set and the value of the PASID
3618 	 *   Enable, Execute Requested Enable, or Privileged Mode Requested bits
3619 	 *   are changed.
3620 	 */
3621 	arm_smmu_enable_pasid(master);
3622 
3623 	if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB))
3624 		master->ssid_bits = min_t(u8, master->ssid_bits,
3625 					  CTXDESC_LINEAR_CDMAX);
3626 
3627 	if ((smmu->features & ARM_SMMU_FEAT_STALLS &&
3628 	     device_property_read_bool(dev, "dma-can-stall")) ||
3629 	    smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
3630 		master->stall_enabled = true;
3631 
3632 	if (dev_is_pci(dev)) {
3633 		unsigned int stu = __ffs(smmu->pgsize_bitmap);
3634 
3635 		pci_prepare_ats(to_pci_dev(dev), stu);
3636 	}
3637 
3638 	return &smmu->iommu;
3639 
3640 err_free_master:
3641 	kfree(master);
3642 	return ERR_PTR(ret);
3643 }
3644 
arm_smmu_release_device(struct device * dev)3645 static void arm_smmu_release_device(struct device *dev)
3646 {
3647 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
3648 
3649 	WARN_ON(master->iopf_refcount);
3650 
3651 	arm_smmu_disable_pasid(master);
3652 	arm_smmu_remove_master(master);
3653 	if (arm_smmu_cdtab_allocated(&master->cd_table))
3654 		arm_smmu_free_cd_tables(master);
3655 	kfree(master);
3656 }
3657 
arm_smmu_read_and_clear_dirty(struct iommu_domain * domain,unsigned long iova,size_t size,unsigned long flags,struct iommu_dirty_bitmap * dirty)3658 static int arm_smmu_read_and_clear_dirty(struct iommu_domain *domain,
3659 					 unsigned long iova, size_t size,
3660 					 unsigned long flags,
3661 					 struct iommu_dirty_bitmap *dirty)
3662 {
3663 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
3664 	struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
3665 
3666 	return ops->read_and_clear_dirty(ops, iova, size, flags, dirty);
3667 }
3668 
arm_smmu_set_dirty_tracking(struct iommu_domain * domain,bool enabled)3669 static int arm_smmu_set_dirty_tracking(struct iommu_domain *domain,
3670 				       bool enabled)
3671 {
3672 	/*
3673 	 * Always enabled and the dirty bitmap is cleared prior to
3674 	 * set_dirty_tracking().
3675 	 */
3676 	return 0;
3677 }
3678 
arm_smmu_device_group(struct device * dev)3679 static struct iommu_group *arm_smmu_device_group(struct device *dev)
3680 {
3681 	struct iommu_group *group;
3682 
3683 	/*
3684 	 * We don't support devices sharing stream IDs other than PCI RID
3685 	 * aliases, since the necessary ID-to-device lookup becomes rather
3686 	 * impractical given a potential sparse 32-bit stream ID space.
3687 	 */
3688 	if (dev_is_pci(dev))
3689 		group = pci_device_group(dev);
3690 	else
3691 		group = generic_device_group(dev);
3692 
3693 	return group;
3694 }
3695 
arm_smmu_of_xlate(struct device * dev,const struct of_phandle_args * args)3696 static int arm_smmu_of_xlate(struct device *dev,
3697 			     const struct of_phandle_args *args)
3698 {
3699 	return iommu_fwspec_add_ids(dev, args->args, 1);
3700 }
3701 
arm_smmu_get_resv_regions(struct device * dev,struct list_head * head)3702 static void arm_smmu_get_resv_regions(struct device *dev,
3703 				      struct list_head *head)
3704 {
3705 	struct iommu_resv_region *region;
3706 	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
3707 
3708 	region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
3709 					 prot, IOMMU_RESV_SW_MSI, GFP_KERNEL);
3710 	if (!region)
3711 		return;
3712 
3713 	list_add_tail(&region->list, head);
3714 
3715 	iommu_dma_get_resv_regions(dev, head);
3716 }
3717 
3718 /*
3719  * HiSilicon PCIe tune and trace device can be used to trace TLP headers on the
3720  * PCIe link and save the data to memory by DMA. The hardware is restricted to
3721  * use identity mapping only.
3722  */
3723 #define IS_HISI_PTT_DEVICE(pdev)	((pdev)->vendor == PCI_VENDOR_ID_HUAWEI && \
3724 					 (pdev)->device == 0xa12e)
3725 
arm_smmu_def_domain_type(struct device * dev)3726 static int arm_smmu_def_domain_type(struct device *dev)
3727 {
3728 	if (dev_is_pci(dev)) {
3729 		struct pci_dev *pdev = to_pci_dev(dev);
3730 
3731 		if (IS_HISI_PTT_DEVICE(pdev))
3732 			return IOMMU_DOMAIN_IDENTITY;
3733 	}
3734 
3735 	return 0;
3736 }
3737 
3738 static const struct iommu_ops arm_smmu_ops = {
3739 	.identity_domain	= &arm_smmu_identity_domain,
3740 	.blocked_domain		= &arm_smmu_blocked_domain,
3741 	.release_domain		= &arm_smmu_blocked_domain,
3742 	.capable		= arm_smmu_capable,
3743 	.hw_info		= arm_smmu_hw_info,
3744 	.domain_alloc_sva       = arm_smmu_sva_domain_alloc,
3745 	.domain_alloc_paging_flags = arm_smmu_domain_alloc_paging_flags,
3746 	.probe_device		= arm_smmu_probe_device,
3747 	.release_device		= arm_smmu_release_device,
3748 	.device_group		= arm_smmu_device_group,
3749 	.of_xlate		= arm_smmu_of_xlate,
3750 	.get_resv_regions	= arm_smmu_get_resv_regions,
3751 	.page_response		= arm_smmu_page_response,
3752 	.def_domain_type	= arm_smmu_def_domain_type,
3753 	.get_viommu_size	= arm_smmu_get_viommu_size,
3754 	.viommu_init		= arm_vsmmu_init,
3755 	.user_pasid_table	= 1,
3756 	.owner			= THIS_MODULE,
3757 	.default_domain_ops = &(const struct iommu_domain_ops) {
3758 		.attach_dev		= arm_smmu_attach_dev,
3759 		.enforce_cache_coherency = arm_smmu_enforce_cache_coherency,
3760 		.set_dev_pasid		= arm_smmu_s1_set_dev_pasid,
3761 		.map_pages		= arm_smmu_map_pages,
3762 		.unmap_pages		= arm_smmu_unmap_pages,
3763 		.flush_iotlb_all	= arm_smmu_flush_iotlb_all,
3764 		.iotlb_sync		= arm_smmu_iotlb_sync,
3765 		.iova_to_phys		= arm_smmu_iova_to_phys,
3766 		.free			= arm_smmu_domain_free_paging,
3767 	}
3768 };
3769 
3770 static struct iommu_dirty_ops arm_smmu_dirty_ops = {
3771 	.read_and_clear_dirty	= arm_smmu_read_and_clear_dirty,
3772 	.set_dirty_tracking     = arm_smmu_set_dirty_tracking,
3773 };
3774 
3775 /* Probing and initialisation functions */
arm_smmu_init_one_queue(struct arm_smmu_device * smmu,struct arm_smmu_queue * q,void __iomem * page,unsigned long prod_off,unsigned long cons_off,size_t dwords,const char * name)3776 int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
3777 			    struct arm_smmu_queue *q, void __iomem *page,
3778 			    unsigned long prod_off, unsigned long cons_off,
3779 			    size_t dwords, const char *name)
3780 {
3781 	size_t qsz;
3782 
3783 	do {
3784 		qsz = ((1 << q->llq.max_n_shift) * dwords) << 3;
3785 		q->base = dmam_alloc_coherent(smmu->dev, qsz, &q->base_dma,
3786 					      GFP_KERNEL);
3787 		if (q->base || qsz < PAGE_SIZE)
3788 			break;
3789 
3790 		q->llq.max_n_shift--;
3791 	} while (1);
3792 
3793 	if (!q->base) {
3794 		dev_err(smmu->dev,
3795 			"failed to allocate queue (0x%zx bytes) for %s\n",
3796 			qsz, name);
3797 		return -ENOMEM;
3798 	}
3799 
3800 	if (!WARN_ON(q->base_dma & (qsz - 1))) {
3801 		dev_info(smmu->dev, "allocated %u entries for %s\n",
3802 			 1 << q->llq.max_n_shift, name);
3803 	}
3804 
3805 	q->prod_reg	= page + prod_off;
3806 	q->cons_reg	= page + cons_off;
3807 	q->ent_dwords	= dwords;
3808 
3809 	q->q_base  = Q_BASE_RWA;
3810 	q->q_base |= q->base_dma & Q_BASE_ADDR_MASK;
3811 	q->q_base |= FIELD_PREP(Q_BASE_LOG2SIZE, q->llq.max_n_shift);
3812 
3813 	q->llq.prod = q->llq.cons = 0;
3814 	return 0;
3815 }
3816 
arm_smmu_cmdq_init(struct arm_smmu_device * smmu,struct arm_smmu_cmdq * cmdq)3817 int arm_smmu_cmdq_init(struct arm_smmu_device *smmu,
3818 		       struct arm_smmu_cmdq *cmdq)
3819 {
3820 	unsigned int nents = 1 << cmdq->q.llq.max_n_shift;
3821 
3822 	atomic_set(&cmdq->owner_prod, 0);
3823 	atomic_set(&cmdq->lock, 0);
3824 
3825 	cmdq->valid_map = (atomic_long_t *)devm_bitmap_zalloc(smmu->dev, nents,
3826 							      GFP_KERNEL);
3827 	if (!cmdq->valid_map)
3828 		return -ENOMEM;
3829 
3830 	return 0;
3831 }
3832 
arm_smmu_init_queues(struct arm_smmu_device * smmu)3833 static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
3834 {
3835 	int ret;
3836 
3837 	/* cmdq */
3838 	ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, smmu->base,
3839 				      ARM_SMMU_CMDQ_PROD, ARM_SMMU_CMDQ_CONS,
3840 				      CMDQ_ENT_DWORDS, "cmdq");
3841 	if (ret)
3842 		return ret;
3843 
3844 	ret = arm_smmu_cmdq_init(smmu, &smmu->cmdq);
3845 	if (ret)
3846 		return ret;
3847 
3848 	/* evtq */
3849 	ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, smmu->page1,
3850 				      ARM_SMMU_EVTQ_PROD, ARM_SMMU_EVTQ_CONS,
3851 				      EVTQ_ENT_DWORDS, "evtq");
3852 	if (ret)
3853 		return ret;
3854 
3855 	if ((smmu->features & ARM_SMMU_FEAT_SVA) &&
3856 	    (smmu->features & ARM_SMMU_FEAT_STALLS)) {
3857 		smmu->evtq.iopf = iopf_queue_alloc(dev_name(smmu->dev));
3858 		if (!smmu->evtq.iopf)
3859 			return -ENOMEM;
3860 	}
3861 
3862 	/* priq */
3863 	if (!(smmu->features & ARM_SMMU_FEAT_PRI))
3864 		return 0;
3865 
3866 	return arm_smmu_init_one_queue(smmu, &smmu->priq.q, smmu->page1,
3867 				       ARM_SMMU_PRIQ_PROD, ARM_SMMU_PRIQ_CONS,
3868 				       PRIQ_ENT_DWORDS, "priq");
3869 }
3870 
arm_smmu_init_strtab_2lvl(struct arm_smmu_device * smmu)3871 static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
3872 {
3873 	u32 l1size;
3874 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
3875 	unsigned int last_sid_idx =
3876 		arm_smmu_strtab_l1_idx((1ULL << smmu->sid_bits) - 1);
3877 
3878 	/* Calculate the L1 size, capped to the SIDSIZE. */
3879 	cfg->l2.num_l1_ents = min(last_sid_idx + 1, STRTAB_MAX_L1_ENTRIES);
3880 	if (cfg->l2.num_l1_ents <= last_sid_idx)
3881 		dev_warn(smmu->dev,
3882 			 "2-level strtab only covers %u/%u bits of SID\n",
3883 			 ilog2(cfg->l2.num_l1_ents * STRTAB_NUM_L2_STES),
3884 			 smmu->sid_bits);
3885 
3886 	l1size = cfg->l2.num_l1_ents * sizeof(struct arm_smmu_strtab_l1);
3887 	cfg->l2.l1tab = dmam_alloc_coherent(smmu->dev, l1size, &cfg->l2.l1_dma,
3888 					    GFP_KERNEL);
3889 	if (!cfg->l2.l1tab) {
3890 		dev_err(smmu->dev,
3891 			"failed to allocate l1 stream table (%u bytes)\n",
3892 			l1size);
3893 		return -ENOMEM;
3894 	}
3895 
3896 	cfg->l2.l2ptrs = devm_kcalloc(smmu->dev, cfg->l2.num_l1_ents,
3897 				      sizeof(*cfg->l2.l2ptrs), GFP_KERNEL);
3898 	if (!cfg->l2.l2ptrs)
3899 		return -ENOMEM;
3900 
3901 	return 0;
3902 }
3903 
arm_smmu_init_strtab_linear(struct arm_smmu_device * smmu)3904 static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
3905 {
3906 	u32 size;
3907 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
3908 
3909 	size = (1 << smmu->sid_bits) * sizeof(struct arm_smmu_ste);
3910 	cfg->linear.table = dmam_alloc_coherent(smmu->dev, size,
3911 						&cfg->linear.ste_dma,
3912 						GFP_KERNEL);
3913 	if (!cfg->linear.table) {
3914 		dev_err(smmu->dev,
3915 			"failed to allocate linear stream table (%u bytes)\n",
3916 			size);
3917 		return -ENOMEM;
3918 	}
3919 	cfg->linear.num_ents = 1 << smmu->sid_bits;
3920 
3921 	arm_smmu_init_initial_stes(cfg->linear.table, cfg->linear.num_ents);
3922 	return 0;
3923 }
3924 
arm_smmu_init_strtab(struct arm_smmu_device * smmu)3925 static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
3926 {
3927 	int ret;
3928 
3929 	if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
3930 		ret = arm_smmu_init_strtab_2lvl(smmu);
3931 	else
3932 		ret = arm_smmu_init_strtab_linear(smmu);
3933 	if (ret)
3934 		return ret;
3935 
3936 	ida_init(&smmu->vmid_map);
3937 
3938 	return 0;
3939 }
3940 
arm_smmu_init_structures(struct arm_smmu_device * smmu)3941 static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
3942 {
3943 	int ret;
3944 
3945 	mutex_init(&smmu->streams_mutex);
3946 	smmu->streams = RB_ROOT;
3947 
3948 	ret = arm_smmu_init_queues(smmu);
3949 	if (ret)
3950 		return ret;
3951 
3952 	ret = arm_smmu_init_strtab(smmu);
3953 	if (ret)
3954 		return ret;
3955 
3956 	if (smmu->impl_ops && smmu->impl_ops->init_structures)
3957 		return smmu->impl_ops->init_structures(smmu);
3958 
3959 	return 0;
3960 }
3961 
arm_smmu_write_reg_sync(struct arm_smmu_device * smmu,u32 val,unsigned int reg_off,unsigned int ack_off)3962 static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
3963 				   unsigned int reg_off, unsigned int ack_off)
3964 {
3965 	u32 reg;
3966 
3967 	writel_relaxed(val, smmu->base + reg_off);
3968 	return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
3969 					  1, ARM_SMMU_POLL_TIMEOUT_US);
3970 }
3971 
3972 /* GBPA is "special" */
arm_smmu_update_gbpa(struct arm_smmu_device * smmu,u32 set,u32 clr)3973 static int arm_smmu_update_gbpa(struct arm_smmu_device *smmu, u32 set, u32 clr)
3974 {
3975 	int ret;
3976 	u32 reg, __iomem *gbpa = smmu->base + ARM_SMMU_GBPA;
3977 
3978 	ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
3979 					 1, ARM_SMMU_POLL_TIMEOUT_US);
3980 	if (ret)
3981 		return ret;
3982 
3983 	reg &= ~clr;
3984 	reg |= set;
3985 	writel_relaxed(reg | GBPA_UPDATE, gbpa);
3986 	ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
3987 					 1, ARM_SMMU_POLL_TIMEOUT_US);
3988 
3989 	if (ret)
3990 		dev_err(smmu->dev, "GBPA not responding to update\n");
3991 	return ret;
3992 }
3993 
arm_smmu_free_msis(void * data)3994 static void arm_smmu_free_msis(void *data)
3995 {
3996 	struct device *dev = data;
3997 
3998 	platform_device_msi_free_irqs_all(dev);
3999 }
4000 
arm_smmu_write_msi_msg(struct msi_desc * desc,struct msi_msg * msg)4001 static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
4002 {
4003 	phys_addr_t doorbell;
4004 	struct device *dev = msi_desc_to_dev(desc);
4005 	struct arm_smmu_device *smmu = dev_get_drvdata(dev);
4006 	phys_addr_t *cfg = arm_smmu_msi_cfg[desc->msi_index];
4007 
4008 	doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
4009 	doorbell &= MSI_CFG0_ADDR_MASK;
4010 
4011 	writeq_relaxed(doorbell, smmu->base + cfg[0]);
4012 	writel_relaxed(msg->data, smmu->base + cfg[1]);
4013 	writel_relaxed(ARM_SMMU_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]);
4014 }
4015 
arm_smmu_setup_msis(struct arm_smmu_device * smmu)4016 static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
4017 {
4018 	int ret, nvec = ARM_SMMU_MAX_MSIS;
4019 	struct device *dev = smmu->dev;
4020 
4021 	/* Clear the MSI address regs */
4022 	writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
4023 	writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
4024 
4025 	if (smmu->features & ARM_SMMU_FEAT_PRI)
4026 		writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
4027 	else
4028 		nvec--;
4029 
4030 	if (!(smmu->features & ARM_SMMU_FEAT_MSI))
4031 		return;
4032 
4033 	if (!dev->msi.domain) {
4034 		dev_info(smmu->dev, "msi_domain absent - falling back to wired irqs\n");
4035 		return;
4036 	}
4037 
4038 	/* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */
4039 	ret = platform_device_msi_init_and_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg);
4040 	if (ret) {
4041 		dev_warn(dev, "failed to allocate MSIs - falling back to wired irqs\n");
4042 		return;
4043 	}
4044 
4045 	smmu->evtq.q.irq = msi_get_virq(dev, EVTQ_MSI_INDEX);
4046 	smmu->gerr_irq = msi_get_virq(dev, GERROR_MSI_INDEX);
4047 	smmu->priq.q.irq = msi_get_virq(dev, PRIQ_MSI_INDEX);
4048 
4049 	/* Add callback to free MSIs on teardown */
4050 	devm_add_action_or_reset(dev, arm_smmu_free_msis, dev);
4051 }
4052 
arm_smmu_setup_unique_irqs(struct arm_smmu_device * smmu)4053 static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
4054 {
4055 	int irq, ret;
4056 
4057 	arm_smmu_setup_msis(smmu);
4058 
4059 	/* Request interrupt lines */
4060 	irq = smmu->evtq.q.irq;
4061 	if (irq) {
4062 		ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
4063 						arm_smmu_evtq_thread,
4064 						IRQF_ONESHOT,
4065 						"arm-smmu-v3-evtq", smmu);
4066 		if (ret < 0)
4067 			dev_warn(smmu->dev, "failed to enable evtq irq\n");
4068 	} else {
4069 		dev_warn(smmu->dev, "no evtq irq - events will not be reported!\n");
4070 	}
4071 
4072 	irq = smmu->gerr_irq;
4073 	if (irq) {
4074 		ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
4075 				       0, "arm-smmu-v3-gerror", smmu);
4076 		if (ret < 0)
4077 			dev_warn(smmu->dev, "failed to enable gerror irq\n");
4078 	} else {
4079 		dev_warn(smmu->dev, "no gerr irq - errors will not be reported!\n");
4080 	}
4081 
4082 	if (smmu->features & ARM_SMMU_FEAT_PRI) {
4083 		irq = smmu->priq.q.irq;
4084 		if (irq) {
4085 			ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
4086 							arm_smmu_priq_thread,
4087 							IRQF_ONESHOT,
4088 							"arm-smmu-v3-priq",
4089 							smmu);
4090 			if (ret < 0)
4091 				dev_warn(smmu->dev,
4092 					 "failed to enable priq irq\n");
4093 		} else {
4094 			dev_warn(smmu->dev, "no priq irq - PRI will be broken\n");
4095 		}
4096 	}
4097 }
4098 
arm_smmu_setup_irqs(struct arm_smmu_device * smmu)4099 static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
4100 {
4101 	int ret, irq;
4102 	u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
4103 
4104 	/* Disable IRQs first */
4105 	ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
4106 				      ARM_SMMU_IRQ_CTRLACK);
4107 	if (ret) {
4108 		dev_err(smmu->dev, "failed to disable irqs\n");
4109 		return ret;
4110 	}
4111 
4112 	irq = smmu->combined_irq;
4113 	if (irq) {
4114 		/*
4115 		 * Cavium ThunderX2 implementation doesn't support unique irq
4116 		 * lines. Use a single irq line for all the SMMUv3 interrupts.
4117 		 */
4118 		ret = devm_request_threaded_irq(smmu->dev, irq,
4119 					arm_smmu_combined_irq_handler,
4120 					arm_smmu_combined_irq_thread,
4121 					IRQF_ONESHOT,
4122 					"arm-smmu-v3-combined-irq", smmu);
4123 		if (ret < 0)
4124 			dev_warn(smmu->dev, "failed to enable combined irq\n");
4125 	} else
4126 		arm_smmu_setup_unique_irqs(smmu);
4127 
4128 	if (smmu->features & ARM_SMMU_FEAT_PRI)
4129 		irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
4130 
4131 	/* Enable interrupt generation on the SMMU */
4132 	ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
4133 				      ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
4134 	if (ret)
4135 		dev_warn(smmu->dev, "failed to enable irqs\n");
4136 
4137 	return 0;
4138 }
4139 
arm_smmu_device_disable(struct arm_smmu_device * smmu)4140 static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
4141 {
4142 	int ret;
4143 
4144 	ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
4145 	if (ret)
4146 		dev_err(smmu->dev, "failed to clear cr0\n");
4147 
4148 	return ret;
4149 }
4150 
arm_smmu_write_strtab(struct arm_smmu_device * smmu)4151 static void arm_smmu_write_strtab(struct arm_smmu_device *smmu)
4152 {
4153 	struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
4154 	dma_addr_t dma;
4155 	u32 reg;
4156 
4157 	if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
4158 		reg = FIELD_PREP(STRTAB_BASE_CFG_FMT,
4159 				 STRTAB_BASE_CFG_FMT_2LVL) |
4160 		      FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE,
4161 				 ilog2(cfg->l2.num_l1_ents) + STRTAB_SPLIT) |
4162 		      FIELD_PREP(STRTAB_BASE_CFG_SPLIT, STRTAB_SPLIT);
4163 		dma = cfg->l2.l1_dma;
4164 	} else {
4165 		reg = FIELD_PREP(STRTAB_BASE_CFG_FMT,
4166 				 STRTAB_BASE_CFG_FMT_LINEAR) |
4167 		      FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, smmu->sid_bits);
4168 		dma = cfg->linear.ste_dma;
4169 	}
4170 	writeq_relaxed((dma & STRTAB_BASE_ADDR_MASK) | STRTAB_BASE_RA,
4171 		       smmu->base + ARM_SMMU_STRTAB_BASE);
4172 	writel_relaxed(reg, smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
4173 }
4174 
arm_smmu_device_reset(struct arm_smmu_device * smmu)4175 static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
4176 {
4177 	int ret;
4178 	u32 reg, enables;
4179 	struct arm_smmu_cmdq_ent cmd;
4180 
4181 	/* Clear CR0 and sync (disables SMMU and queue processing) */
4182 	reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
4183 	if (reg & CR0_SMMUEN) {
4184 		dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
4185 		arm_smmu_update_gbpa(smmu, GBPA_ABORT, 0);
4186 	}
4187 
4188 	ret = arm_smmu_device_disable(smmu);
4189 	if (ret)
4190 		return ret;
4191 
4192 	/* CR1 (table and queue memory attributes) */
4193 	reg = FIELD_PREP(CR1_TABLE_SH, ARM_SMMU_SH_ISH) |
4194 	      FIELD_PREP(CR1_TABLE_OC, CR1_CACHE_WB) |
4195 	      FIELD_PREP(CR1_TABLE_IC, CR1_CACHE_WB) |
4196 	      FIELD_PREP(CR1_QUEUE_SH, ARM_SMMU_SH_ISH) |
4197 	      FIELD_PREP(CR1_QUEUE_OC, CR1_CACHE_WB) |
4198 	      FIELD_PREP(CR1_QUEUE_IC, CR1_CACHE_WB);
4199 	writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
4200 
4201 	/* CR2 (random crap) */
4202 	reg = CR2_PTM | CR2_RECINVSID;
4203 
4204 	if (smmu->features & ARM_SMMU_FEAT_E2H)
4205 		reg |= CR2_E2H;
4206 
4207 	writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
4208 
4209 	/* Stream table */
4210 	arm_smmu_write_strtab(smmu);
4211 
4212 	/* Command queue */
4213 	writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
4214 	writel_relaxed(smmu->cmdq.q.llq.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
4215 	writel_relaxed(smmu->cmdq.q.llq.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
4216 
4217 	enables = CR0_CMDQEN;
4218 	ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
4219 				      ARM_SMMU_CR0ACK);
4220 	if (ret) {
4221 		dev_err(smmu->dev, "failed to enable command queue\n");
4222 		return ret;
4223 	}
4224 
4225 	/* Invalidate any cached configuration */
4226 	cmd.opcode = CMDQ_OP_CFGI_ALL;
4227 	arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd);
4228 
4229 	/* Invalidate any stale TLB entries */
4230 	if (smmu->features & ARM_SMMU_FEAT_HYP) {
4231 		cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
4232 		arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd);
4233 	}
4234 
4235 	cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
4236 	arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd);
4237 
4238 	/* Event queue */
4239 	writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
4240 	writel_relaxed(smmu->evtq.q.llq.prod, smmu->page1 + ARM_SMMU_EVTQ_PROD);
4241 	writel_relaxed(smmu->evtq.q.llq.cons, smmu->page1 + ARM_SMMU_EVTQ_CONS);
4242 
4243 	enables |= CR0_EVTQEN;
4244 	ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
4245 				      ARM_SMMU_CR0ACK);
4246 	if (ret) {
4247 		dev_err(smmu->dev, "failed to enable event queue\n");
4248 		return ret;
4249 	}
4250 
4251 	/* PRI queue */
4252 	if (smmu->features & ARM_SMMU_FEAT_PRI) {
4253 		writeq_relaxed(smmu->priq.q.q_base,
4254 			       smmu->base + ARM_SMMU_PRIQ_BASE);
4255 		writel_relaxed(smmu->priq.q.llq.prod,
4256 			       smmu->page1 + ARM_SMMU_PRIQ_PROD);
4257 		writel_relaxed(smmu->priq.q.llq.cons,
4258 			       smmu->page1 + ARM_SMMU_PRIQ_CONS);
4259 
4260 		enables |= CR0_PRIQEN;
4261 		ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
4262 					      ARM_SMMU_CR0ACK);
4263 		if (ret) {
4264 			dev_err(smmu->dev, "failed to enable PRI queue\n");
4265 			return ret;
4266 		}
4267 	}
4268 
4269 	if (smmu->features & ARM_SMMU_FEAT_ATS) {
4270 		enables |= CR0_ATSCHK;
4271 		ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
4272 					      ARM_SMMU_CR0ACK);
4273 		if (ret) {
4274 			dev_err(smmu->dev, "failed to enable ATS check\n");
4275 			return ret;
4276 		}
4277 	}
4278 
4279 	ret = arm_smmu_setup_irqs(smmu);
4280 	if (ret) {
4281 		dev_err(smmu->dev, "failed to setup irqs\n");
4282 		return ret;
4283 	}
4284 
4285 	if (is_kdump_kernel())
4286 		enables &= ~(CR0_EVTQEN | CR0_PRIQEN);
4287 
4288 	/* Enable the SMMU interface */
4289 	enables |= CR0_SMMUEN;
4290 	ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
4291 				      ARM_SMMU_CR0ACK);
4292 	if (ret) {
4293 		dev_err(smmu->dev, "failed to enable SMMU interface\n");
4294 		return ret;
4295 	}
4296 
4297 	if (smmu->impl_ops && smmu->impl_ops->device_reset) {
4298 		ret = smmu->impl_ops->device_reset(smmu);
4299 		if (ret) {
4300 			dev_err(smmu->dev, "failed to reset impl\n");
4301 			return ret;
4302 		}
4303 	}
4304 
4305 	return 0;
4306 }
4307 
4308 #define IIDR_IMPLEMENTER_ARM		0x43b
4309 #define IIDR_PRODUCTID_ARM_MMU_600	0x483
4310 #define IIDR_PRODUCTID_ARM_MMU_700	0x487
4311 
arm_smmu_device_iidr_probe(struct arm_smmu_device * smmu)4312 static void arm_smmu_device_iidr_probe(struct arm_smmu_device *smmu)
4313 {
4314 	u32 reg;
4315 	unsigned int implementer, productid, variant, revision;
4316 
4317 	reg = readl_relaxed(smmu->base + ARM_SMMU_IIDR);
4318 	implementer = FIELD_GET(IIDR_IMPLEMENTER, reg);
4319 	productid = FIELD_GET(IIDR_PRODUCTID, reg);
4320 	variant = FIELD_GET(IIDR_VARIANT, reg);
4321 	revision = FIELD_GET(IIDR_REVISION, reg);
4322 
4323 	switch (implementer) {
4324 	case IIDR_IMPLEMENTER_ARM:
4325 		switch (productid) {
4326 		case IIDR_PRODUCTID_ARM_MMU_600:
4327 			/* Arm erratum 1076982 */
4328 			if (variant == 0 && revision <= 2)
4329 				smmu->features &= ~ARM_SMMU_FEAT_SEV;
4330 			/* Arm erratum 1209401 */
4331 			if (variant < 2)
4332 				smmu->features &= ~ARM_SMMU_FEAT_NESTING;
4333 			break;
4334 		case IIDR_PRODUCTID_ARM_MMU_700:
4335 			/* Arm erratum 2812531 */
4336 			smmu->features &= ~ARM_SMMU_FEAT_BTM;
4337 			smmu->options |= ARM_SMMU_OPT_CMDQ_FORCE_SYNC;
4338 			/* Arm errata 2268618, 2812531 */
4339 			smmu->features &= ~ARM_SMMU_FEAT_NESTING;
4340 			break;
4341 		}
4342 		break;
4343 	}
4344 }
4345 
arm_smmu_get_httu(struct arm_smmu_device * smmu,u32 reg)4346 static void arm_smmu_get_httu(struct arm_smmu_device *smmu, u32 reg)
4347 {
4348 	u32 fw_features = smmu->features & (ARM_SMMU_FEAT_HA | ARM_SMMU_FEAT_HD);
4349 	u32 hw_features = 0;
4350 
4351 	switch (FIELD_GET(IDR0_HTTU, reg)) {
4352 	case IDR0_HTTU_ACCESS_DIRTY:
4353 		hw_features |= ARM_SMMU_FEAT_HD;
4354 		fallthrough;
4355 	case IDR0_HTTU_ACCESS:
4356 		hw_features |= ARM_SMMU_FEAT_HA;
4357 	}
4358 
4359 	if (smmu->dev->of_node)
4360 		smmu->features |= hw_features;
4361 	else if (hw_features != fw_features)
4362 		/* ACPI IORT sets the HTTU bits */
4363 		dev_warn(smmu->dev,
4364 			 "IDR0.HTTU features(0x%x) overridden by FW configuration (0x%x)\n",
4365 			  hw_features, fw_features);
4366 }
4367 
arm_smmu_device_hw_probe(struct arm_smmu_device * smmu)4368 static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
4369 {
4370 	u32 reg;
4371 	bool coherent = smmu->features & ARM_SMMU_FEAT_COHERENCY;
4372 
4373 	/* IDR0 */
4374 	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
4375 
4376 	/* 2-level structures */
4377 	if (FIELD_GET(IDR0_ST_LVL, reg) == IDR0_ST_LVL_2LVL)
4378 		smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
4379 
4380 	if (reg & IDR0_CD2L)
4381 		smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
4382 
4383 	/*
4384 	 * Translation table endianness.
4385 	 * We currently require the same endianness as the CPU, but this
4386 	 * could be changed later by adding a new IO_PGTABLE_QUIRK.
4387 	 */
4388 	switch (FIELD_GET(IDR0_TTENDIAN, reg)) {
4389 	case IDR0_TTENDIAN_MIXED:
4390 		smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
4391 		break;
4392 #ifdef __BIG_ENDIAN
4393 	case IDR0_TTENDIAN_BE:
4394 		smmu->features |= ARM_SMMU_FEAT_TT_BE;
4395 		break;
4396 #else
4397 	case IDR0_TTENDIAN_LE:
4398 		smmu->features |= ARM_SMMU_FEAT_TT_LE;
4399 		break;
4400 #endif
4401 	default:
4402 		dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
4403 		return -ENXIO;
4404 	}
4405 
4406 	/* Boolean feature flags */
4407 	if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
4408 		smmu->features |= ARM_SMMU_FEAT_PRI;
4409 
4410 	if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
4411 		smmu->features |= ARM_SMMU_FEAT_ATS;
4412 
4413 	if (reg & IDR0_SEV)
4414 		smmu->features |= ARM_SMMU_FEAT_SEV;
4415 
4416 	if (reg & IDR0_MSI) {
4417 		smmu->features |= ARM_SMMU_FEAT_MSI;
4418 		if (coherent && !disable_msipolling)
4419 			smmu->options |= ARM_SMMU_OPT_MSIPOLL;
4420 	}
4421 
4422 	if (reg & IDR0_HYP) {
4423 		smmu->features |= ARM_SMMU_FEAT_HYP;
4424 		if (cpus_have_cap(ARM64_HAS_VIRT_HOST_EXTN))
4425 			smmu->features |= ARM_SMMU_FEAT_E2H;
4426 	}
4427 
4428 	arm_smmu_get_httu(smmu, reg);
4429 
4430 	/*
4431 	 * The coherency feature as set by FW is used in preference to the ID
4432 	 * register, but warn on mismatch.
4433 	 */
4434 	if (!!(reg & IDR0_COHACC) != coherent)
4435 		dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n",
4436 			 str_true_false(coherent));
4437 
4438 	switch (FIELD_GET(IDR0_STALL_MODEL, reg)) {
4439 	case IDR0_STALL_MODEL_FORCE:
4440 		smmu->features |= ARM_SMMU_FEAT_STALL_FORCE;
4441 		fallthrough;
4442 	case IDR0_STALL_MODEL_STALL:
4443 		smmu->features |= ARM_SMMU_FEAT_STALLS;
4444 	}
4445 
4446 	if (reg & IDR0_S1P)
4447 		smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
4448 
4449 	if (reg & IDR0_S2P)
4450 		smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
4451 
4452 	if (!(reg & (IDR0_S1P | IDR0_S2P))) {
4453 		dev_err(smmu->dev, "no translation support!\n");
4454 		return -ENXIO;
4455 	}
4456 
4457 	/* We only support the AArch64 table format at present */
4458 	if (!(FIELD_GET(IDR0_TTF, reg) & IDR0_TTF_AARCH64)) {
4459 		dev_err(smmu->dev, "AArch64 table format not supported!\n");
4460 		return -ENXIO;
4461 	}
4462 
4463 	/* ASID/VMID sizes */
4464 	smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
4465 	smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
4466 
4467 	/* IDR1 */
4468 	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
4469 	if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
4470 		dev_err(smmu->dev, "embedded implementation not supported\n");
4471 		return -ENXIO;
4472 	}
4473 
4474 	if (reg & IDR1_ATTR_TYPES_OVR)
4475 		smmu->features |= ARM_SMMU_FEAT_ATTR_TYPES_OVR;
4476 
4477 	/* Queue sizes, capped to ensure natural alignment */
4478 	smmu->cmdq.q.llq.max_n_shift = min_t(u32, CMDQ_MAX_SZ_SHIFT,
4479 					     FIELD_GET(IDR1_CMDQS, reg));
4480 	if (smmu->cmdq.q.llq.max_n_shift <= ilog2(CMDQ_BATCH_ENTRIES)) {
4481 		/*
4482 		 * We don't support splitting up batches, so one batch of
4483 		 * commands plus an extra sync needs to fit inside the command
4484 		 * queue. There's also no way we can handle the weird alignment
4485 		 * restrictions on the base pointer for a unit-length queue.
4486 		 */
4487 		dev_err(smmu->dev, "command queue size <= %d entries not supported\n",
4488 			CMDQ_BATCH_ENTRIES);
4489 		return -ENXIO;
4490 	}
4491 
4492 	smmu->evtq.q.llq.max_n_shift = min_t(u32, EVTQ_MAX_SZ_SHIFT,
4493 					     FIELD_GET(IDR1_EVTQS, reg));
4494 	smmu->priq.q.llq.max_n_shift = min_t(u32, PRIQ_MAX_SZ_SHIFT,
4495 					     FIELD_GET(IDR1_PRIQS, reg));
4496 
4497 	/* SID/SSID sizes */
4498 	smmu->ssid_bits = FIELD_GET(IDR1_SSIDSIZE, reg);
4499 	smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg);
4500 	smmu->iommu.max_pasids = 1UL << smmu->ssid_bits;
4501 
4502 	/*
4503 	 * If the SMMU supports fewer bits than would fill a single L2 stream
4504 	 * table, use a linear table instead.
4505 	 */
4506 	if (smmu->sid_bits <= STRTAB_SPLIT)
4507 		smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB;
4508 
4509 	/* IDR3 */
4510 	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR3);
4511 	if (FIELD_GET(IDR3_RIL, reg))
4512 		smmu->features |= ARM_SMMU_FEAT_RANGE_INV;
4513 	if (FIELD_GET(IDR3_FWB, reg))
4514 		smmu->features |= ARM_SMMU_FEAT_S2FWB;
4515 
4516 	if (FIELD_GET(IDR3_BBM, reg) == 2)
4517 		smmu->features |= ARM_SMMU_FEAT_BBML2;
4518 
4519 	/* IDR5 */
4520 	reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
4521 
4522 	/* Maximum number of outstanding stalls */
4523 	smmu->evtq.max_stalls = FIELD_GET(IDR5_STALL_MAX, reg);
4524 
4525 	/* Page sizes */
4526 	if (reg & IDR5_GRAN64K)
4527 		smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
4528 	if (reg & IDR5_GRAN16K)
4529 		smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
4530 	if (reg & IDR5_GRAN4K)
4531 		smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
4532 
4533 	/* Input address size */
4534 	if (FIELD_GET(IDR5_VAX, reg) == IDR5_VAX_52_BIT)
4535 		smmu->features |= ARM_SMMU_FEAT_VAX;
4536 
4537 	/* Output address size */
4538 	switch (FIELD_GET(IDR5_OAS, reg)) {
4539 	case IDR5_OAS_32_BIT:
4540 		smmu->oas = 32;
4541 		break;
4542 	case IDR5_OAS_36_BIT:
4543 		smmu->oas = 36;
4544 		break;
4545 	case IDR5_OAS_40_BIT:
4546 		smmu->oas = 40;
4547 		break;
4548 	case IDR5_OAS_42_BIT:
4549 		smmu->oas = 42;
4550 		break;
4551 	case IDR5_OAS_44_BIT:
4552 		smmu->oas = 44;
4553 		break;
4554 	case IDR5_OAS_52_BIT:
4555 		smmu->oas = 52;
4556 		smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
4557 		break;
4558 	default:
4559 		dev_info(smmu->dev,
4560 			"unknown output address size. Truncating to 48-bit\n");
4561 		fallthrough;
4562 	case IDR5_OAS_48_BIT:
4563 		smmu->oas = 48;
4564 	}
4565 
4566 	/* Set the DMA mask for our table walker */
4567 	if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
4568 		dev_warn(smmu->dev,
4569 			 "failed to set DMA mask for table walker\n");
4570 
4571 	if ((smmu->features & ARM_SMMU_FEAT_TRANS_S1) &&
4572 	    (smmu->features & ARM_SMMU_FEAT_TRANS_S2))
4573 		smmu->features |= ARM_SMMU_FEAT_NESTING;
4574 
4575 	arm_smmu_device_iidr_probe(smmu);
4576 
4577 	if (arm_smmu_sva_supported(smmu))
4578 		smmu->features |= ARM_SMMU_FEAT_SVA;
4579 
4580 	dev_info(smmu->dev, "oas %lu-bit (features 0x%08x)\n",
4581 		 smmu->oas, smmu->features);
4582 	return 0;
4583 }
4584 
4585 #ifdef CONFIG_TEGRA241_CMDQV
tegra_cmdqv_dt_probe(struct device_node * smmu_node,struct arm_smmu_device * smmu)4586 static void tegra_cmdqv_dt_probe(struct device_node *smmu_node,
4587 				 struct arm_smmu_device *smmu)
4588 {
4589 	struct platform_device *pdev;
4590 	struct device_node *np;
4591 
4592 	np = of_parse_phandle(smmu_node, "nvidia,cmdqv", 0);
4593 	if (!np)
4594 		return;
4595 
4596 	/* Tegra241 CMDQV driver is responsible for put_device() */
4597 	pdev = of_find_device_by_node(np);
4598 	of_node_put(np);
4599 	if (!pdev)
4600 		return;
4601 
4602 	smmu->impl_dev = &pdev->dev;
4603 	smmu->options |= ARM_SMMU_OPT_TEGRA241_CMDQV;
4604 	dev_dbg(smmu->dev, "found companion CMDQV device: %s\n",
4605 		dev_name(smmu->impl_dev));
4606 }
4607 #else
tegra_cmdqv_dt_probe(struct device_node * smmu_node,struct arm_smmu_device * smmu)4608 static void tegra_cmdqv_dt_probe(struct device_node *smmu_node,
4609 				 struct arm_smmu_device *smmu)
4610 {
4611 }
4612 #endif
4613 
4614 #ifdef CONFIG_ACPI
4615 #ifdef CONFIG_TEGRA241_CMDQV
acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node * node,struct arm_smmu_device * smmu)4616 static void acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node *node,
4617 						struct arm_smmu_device *smmu)
4618 {
4619 	const char *uid = kasprintf(GFP_KERNEL, "%u", node->identifier);
4620 	struct acpi_device *adev;
4621 
4622 	/* Look for an NVDA200C node whose _UID matches the SMMU node ID */
4623 	adev = acpi_dev_get_first_match_dev("NVDA200C", uid, -1);
4624 	if (adev) {
4625 		/* Tegra241 CMDQV driver is responsible for put_device() */
4626 		smmu->impl_dev = get_device(acpi_get_first_physical_node(adev));
4627 		smmu->options |= ARM_SMMU_OPT_TEGRA241_CMDQV;
4628 		dev_info(smmu->dev, "found companion CMDQV device: %s\n",
4629 			 dev_name(smmu->impl_dev));
4630 		acpi_dev_put(adev);
4631 	}
4632 	kfree(uid);
4633 }
4634 #else
acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node * node,struct arm_smmu_device * smmu)4635 static void acpi_smmu_dsdt_probe_tegra241_cmdqv(struct acpi_iort_node *node,
4636 						struct arm_smmu_device *smmu)
4637 {
4638 }
4639 #endif
4640 
acpi_smmu_iort_probe_model(struct acpi_iort_node * node,struct arm_smmu_device * smmu)4641 static int acpi_smmu_iort_probe_model(struct acpi_iort_node *node,
4642 				      struct arm_smmu_device *smmu)
4643 {
4644 	struct acpi_iort_smmu_v3 *iort_smmu =
4645 		(struct acpi_iort_smmu_v3 *)node->node_data;
4646 
4647 	switch (iort_smmu->model) {
4648 	case ACPI_IORT_SMMU_V3_CAVIUM_CN99XX:
4649 		smmu->options |= ARM_SMMU_OPT_PAGE0_REGS_ONLY;
4650 		break;
4651 	case ACPI_IORT_SMMU_V3_HISILICON_HI161X:
4652 		smmu->options |= ARM_SMMU_OPT_SKIP_PREFETCH;
4653 		break;
4654 	case ACPI_IORT_SMMU_V3_GENERIC:
4655 		/*
4656 		 * Tegra241 implementation stores its SMMU options and impl_dev
4657 		 * in DSDT. Thus, go through the ACPI tables unconditionally.
4658 		 */
4659 		acpi_smmu_dsdt_probe_tegra241_cmdqv(node, smmu);
4660 		break;
4661 	}
4662 
4663 	dev_notice(smmu->dev, "option mask 0x%x\n", smmu->options);
4664 	return 0;
4665 }
4666 
arm_smmu_device_acpi_probe(struct platform_device * pdev,struct arm_smmu_device * smmu)4667 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
4668 				      struct arm_smmu_device *smmu)
4669 {
4670 	struct acpi_iort_smmu_v3 *iort_smmu;
4671 	struct device *dev = smmu->dev;
4672 	struct acpi_iort_node *node;
4673 
4674 	node = *(struct acpi_iort_node **)dev_get_platdata(dev);
4675 
4676 	/* Retrieve SMMUv3 specific data */
4677 	iort_smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
4678 
4679 	if (iort_smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE)
4680 		smmu->features |= ARM_SMMU_FEAT_COHERENCY;
4681 
4682 	switch (FIELD_GET(ACPI_IORT_SMMU_V3_HTTU_OVERRIDE, iort_smmu->flags)) {
4683 	case IDR0_HTTU_ACCESS_DIRTY:
4684 		smmu->features |= ARM_SMMU_FEAT_HD;
4685 		fallthrough;
4686 	case IDR0_HTTU_ACCESS:
4687 		smmu->features |= ARM_SMMU_FEAT_HA;
4688 	}
4689 
4690 	return acpi_smmu_iort_probe_model(node, smmu);
4691 }
4692 #else
arm_smmu_device_acpi_probe(struct platform_device * pdev,struct arm_smmu_device * smmu)4693 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
4694 					     struct arm_smmu_device *smmu)
4695 {
4696 	return -ENODEV;
4697 }
4698 #endif
4699 
arm_smmu_device_dt_probe(struct platform_device * pdev,struct arm_smmu_device * smmu)4700 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
4701 				    struct arm_smmu_device *smmu)
4702 {
4703 	struct device *dev = &pdev->dev;
4704 	u32 cells;
4705 	int ret = -EINVAL;
4706 
4707 	if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells))
4708 		dev_err(dev, "missing #iommu-cells property\n");
4709 	else if (cells != 1)
4710 		dev_err(dev, "invalid #iommu-cells value (%d)\n", cells);
4711 	else
4712 		ret = 0;
4713 
4714 	parse_driver_options(smmu);
4715 
4716 	if (of_dma_is_coherent(dev->of_node))
4717 		smmu->features |= ARM_SMMU_FEAT_COHERENCY;
4718 
4719 	if (of_device_is_compatible(dev->of_node, "nvidia,tegra264-smmu"))
4720 		tegra_cmdqv_dt_probe(dev->of_node, smmu);
4721 
4722 	return ret;
4723 }
4724 
arm_smmu_resource_size(struct arm_smmu_device * smmu)4725 static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)
4726 {
4727 	if (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY)
4728 		return SZ_64K;
4729 	else
4730 		return SZ_128K;
4731 }
4732 
arm_smmu_ioremap(struct device * dev,resource_size_t start,resource_size_t size)4733 static void __iomem *arm_smmu_ioremap(struct device *dev, resource_size_t start,
4734 				      resource_size_t size)
4735 {
4736 	struct resource res = DEFINE_RES_MEM(start, size);
4737 
4738 	return devm_ioremap_resource(dev, &res);
4739 }
4740 
arm_smmu_rmr_install_bypass_ste(struct arm_smmu_device * smmu)4741 static void arm_smmu_rmr_install_bypass_ste(struct arm_smmu_device *smmu)
4742 {
4743 	struct list_head rmr_list;
4744 	struct iommu_resv_region *e;
4745 
4746 	INIT_LIST_HEAD(&rmr_list);
4747 	iort_get_rmr_sids(dev_fwnode(smmu->dev), &rmr_list);
4748 
4749 	list_for_each_entry(e, &rmr_list, list) {
4750 		struct iommu_iort_rmr_data *rmr;
4751 		int ret, i;
4752 
4753 		rmr = container_of(e, struct iommu_iort_rmr_data, rr);
4754 		for (i = 0; i < rmr->num_sids; i++) {
4755 			ret = arm_smmu_init_sid_strtab(smmu, rmr->sids[i]);
4756 			if (ret) {
4757 				dev_err(smmu->dev, "RMR SID(0x%x) bypass failed\n",
4758 					rmr->sids[i]);
4759 				continue;
4760 			}
4761 
4762 			/*
4763 			 * STE table is not programmed to HW, see
4764 			 * arm_smmu_initial_bypass_stes()
4765 			 */
4766 			arm_smmu_make_bypass_ste(smmu,
4767 				arm_smmu_get_step_for_sid(smmu, rmr->sids[i]));
4768 		}
4769 	}
4770 
4771 	iort_put_rmr_sids(dev_fwnode(smmu->dev), &rmr_list);
4772 }
4773 
arm_smmu_impl_remove(void * data)4774 static void arm_smmu_impl_remove(void *data)
4775 {
4776 	struct arm_smmu_device *smmu = data;
4777 
4778 	if (smmu->impl_ops && smmu->impl_ops->device_remove)
4779 		smmu->impl_ops->device_remove(smmu);
4780 }
4781 
4782 /*
4783  * Probe all the compiled in implementations. Each one checks to see if it
4784  * matches this HW and if so returns a devm_krealloc'd arm_smmu_device which
4785  * replaces the callers. Otherwise the original is returned or ERR_PTR.
4786  */
arm_smmu_impl_probe(struct arm_smmu_device * smmu)4787 static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu)
4788 {
4789 	struct arm_smmu_device *new_smmu = ERR_PTR(-ENODEV);
4790 	const struct arm_smmu_impl_ops *ops;
4791 	int ret;
4792 
4793 	if (smmu->impl_dev && (smmu->options & ARM_SMMU_OPT_TEGRA241_CMDQV))
4794 		new_smmu = tegra241_cmdqv_probe(smmu);
4795 
4796 	if (new_smmu == ERR_PTR(-ENODEV))
4797 		return smmu;
4798 	if (IS_ERR(new_smmu))
4799 		return new_smmu;
4800 
4801 	ops = new_smmu->impl_ops;
4802 	if (ops) {
4803 		/* get_viommu_size and vsmmu_init ops must be paired */
4804 		if (WARN_ON(!ops->get_viommu_size != !ops->vsmmu_init)) {
4805 			ret = -EINVAL;
4806 			goto err_remove;
4807 		}
4808 	}
4809 
4810 	ret = devm_add_action_or_reset(new_smmu->dev, arm_smmu_impl_remove,
4811 				       new_smmu);
4812 	if (ret)
4813 		return ERR_PTR(ret);
4814 	return new_smmu;
4815 
4816 err_remove:
4817 	arm_smmu_impl_remove(new_smmu);
4818 	return ERR_PTR(ret);
4819 }
4820 
arm_smmu_device_probe(struct platform_device * pdev)4821 static int arm_smmu_device_probe(struct platform_device *pdev)
4822 {
4823 	int irq, ret;
4824 	struct resource *res;
4825 	resource_size_t ioaddr;
4826 	struct arm_smmu_device *smmu;
4827 	struct device *dev = &pdev->dev;
4828 
4829 	smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
4830 	if (!smmu)
4831 		return -ENOMEM;
4832 	smmu->dev = dev;
4833 
4834 	if (dev->of_node) {
4835 		ret = arm_smmu_device_dt_probe(pdev, smmu);
4836 	} else {
4837 		ret = arm_smmu_device_acpi_probe(pdev, smmu);
4838 	}
4839 	if (ret)
4840 		return ret;
4841 
4842 	smmu = arm_smmu_impl_probe(smmu);
4843 	if (IS_ERR(smmu))
4844 		return PTR_ERR(smmu);
4845 
4846 	/* Base address */
4847 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4848 	if (!res)
4849 		return -EINVAL;
4850 	if (resource_size(res) < arm_smmu_resource_size(smmu)) {
4851 		dev_err(dev, "MMIO region too small (%pr)\n", res);
4852 		return -EINVAL;
4853 	}
4854 	ioaddr = res->start;
4855 
4856 	/*
4857 	 * Don't map the IMPLEMENTATION DEFINED regions, since they may contain
4858 	 * the PMCG registers which are reserved by the PMU driver.
4859 	 */
4860 	smmu->base = arm_smmu_ioremap(dev, ioaddr, ARM_SMMU_REG_SZ);
4861 	if (IS_ERR(smmu->base))
4862 		return PTR_ERR(smmu->base);
4863 
4864 	if (arm_smmu_resource_size(smmu) > SZ_64K) {
4865 		smmu->page1 = arm_smmu_ioremap(dev, ioaddr + SZ_64K,
4866 					       ARM_SMMU_REG_SZ);
4867 		if (IS_ERR(smmu->page1))
4868 			return PTR_ERR(smmu->page1);
4869 	} else {
4870 		smmu->page1 = smmu->base;
4871 	}
4872 
4873 	/* Interrupt lines */
4874 
4875 	irq = platform_get_irq_byname_optional(pdev, "combined");
4876 	if (irq > 0)
4877 		smmu->combined_irq = irq;
4878 	else {
4879 		irq = platform_get_irq_byname_optional(pdev, "eventq");
4880 		if (irq > 0)
4881 			smmu->evtq.q.irq = irq;
4882 
4883 		irq = platform_get_irq_byname_optional(pdev, "priq");
4884 		if (irq > 0)
4885 			smmu->priq.q.irq = irq;
4886 
4887 		irq = platform_get_irq_byname_optional(pdev, "gerror");
4888 		if (irq > 0)
4889 			smmu->gerr_irq = irq;
4890 	}
4891 	/* Probe the h/w */
4892 	ret = arm_smmu_device_hw_probe(smmu);
4893 	if (ret)
4894 		return ret;
4895 
4896 	/* Initialise in-memory data structures */
4897 	ret = arm_smmu_init_structures(smmu);
4898 	if (ret)
4899 		goto err_free_iopf;
4900 
4901 	/* Record our private device structure */
4902 	platform_set_drvdata(pdev, smmu);
4903 
4904 	/* Check for RMRs and install bypass STEs if any */
4905 	arm_smmu_rmr_install_bypass_ste(smmu);
4906 
4907 	/* Reset the device */
4908 	ret = arm_smmu_device_reset(smmu);
4909 	if (ret)
4910 		goto err_disable;
4911 
4912 	/* And we're up. Go go go! */
4913 	ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL,
4914 				     "smmu3.%pa", &ioaddr);
4915 	if (ret)
4916 		goto err_disable;
4917 
4918 	ret = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
4919 	if (ret) {
4920 		dev_err(dev, "Failed to register iommu\n");
4921 		goto err_free_sysfs;
4922 	}
4923 
4924 	return 0;
4925 
4926 err_free_sysfs:
4927 	iommu_device_sysfs_remove(&smmu->iommu);
4928 err_disable:
4929 	arm_smmu_device_disable(smmu);
4930 err_free_iopf:
4931 	iopf_queue_free(smmu->evtq.iopf);
4932 	return ret;
4933 }
4934 
arm_smmu_device_remove(struct platform_device * pdev)4935 static void arm_smmu_device_remove(struct platform_device *pdev)
4936 {
4937 	struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
4938 
4939 	iommu_device_unregister(&smmu->iommu);
4940 	iommu_device_sysfs_remove(&smmu->iommu);
4941 	arm_smmu_device_disable(smmu);
4942 	iopf_queue_free(smmu->evtq.iopf);
4943 	ida_destroy(&smmu->vmid_map);
4944 }
4945 
arm_smmu_device_shutdown(struct platform_device * pdev)4946 static void arm_smmu_device_shutdown(struct platform_device *pdev)
4947 {
4948 	struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
4949 
4950 	arm_smmu_device_disable(smmu);
4951 }
4952 
4953 static const struct of_device_id arm_smmu_of_match[] = {
4954 	{ .compatible = "arm,smmu-v3", },
4955 	{ },
4956 };
4957 MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
4958 
arm_smmu_driver_unregister(struct platform_driver * drv)4959 static void arm_smmu_driver_unregister(struct platform_driver *drv)
4960 {
4961 	arm_smmu_sva_notifier_synchronize();
4962 	platform_driver_unregister(drv);
4963 }
4964 
4965 static struct platform_driver arm_smmu_driver = {
4966 	.driver	= {
4967 		.name			= "arm-smmu-v3",
4968 		.of_match_table		= arm_smmu_of_match,
4969 		.suppress_bind_attrs	= true,
4970 	},
4971 	.probe	= arm_smmu_device_probe,
4972 	.remove = arm_smmu_device_remove,
4973 	.shutdown = arm_smmu_device_shutdown,
4974 };
4975 module_driver(arm_smmu_driver, platform_driver_register,
4976 	      arm_smmu_driver_unregister);
4977 
4978 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
4979 MODULE_AUTHOR("Will Deacon <will@kernel.org>");
4980 MODULE_ALIAS("platform:arm-smmu-v3");
4981 MODULE_LICENSE("GPL v2");
4982