xref: /linux/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h (revision 8477ab143069c6b05d6da4a8184ded8b969240f5)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * IOMMU API for ARM architected SMMUv3 implementations.
4  *
5  * Copyright (C) 2015 ARM Limited
6  */
7 
8 #ifndef _ARM_SMMU_V3_H
9 #define _ARM_SMMU_V3_H
10 
11 #include <linux/bitfield.h>
12 #include <linux/iommu.h>
13 #include <linux/iommufd.h>
14 #include <linux/kernel.h>
15 #include <linux/mmzone.h>
16 #include <linux/sizes.h>
17 
18 struct arm_smmu_device;
19 
20 /* MMIO registers */
21 #define ARM_SMMU_IDR0			0x0
22 #define IDR0_ST_LVL			GENMASK(28, 27)
23 #define IDR0_ST_LVL_2LVL		1
24 #define IDR0_STALL_MODEL		GENMASK(25, 24)
25 #define IDR0_STALL_MODEL_STALL		0
26 #define IDR0_STALL_MODEL_FORCE		2
27 #define IDR0_TTENDIAN			GENMASK(22, 21)
28 #define IDR0_TTENDIAN_MIXED		0
29 #define IDR0_TTENDIAN_LE		2
30 #define IDR0_TTENDIAN_BE		3
31 #define IDR0_CD2L			(1 << 19)
32 #define IDR0_VMID16			(1 << 18)
33 #define IDR0_PRI			(1 << 16)
34 #define IDR0_SEV			(1 << 14)
35 #define IDR0_MSI			(1 << 13)
36 #define IDR0_ASID16			(1 << 12)
37 #define IDR0_ATS			(1 << 10)
38 #define IDR0_HYP			(1 << 9)
39 #define IDR0_HTTU			GENMASK(7, 6)
40 #define IDR0_HTTU_ACCESS		1
41 #define IDR0_HTTU_ACCESS_DIRTY		2
42 #define IDR0_COHACC			(1 << 4)
43 #define IDR0_TTF			GENMASK(3, 2)
44 #define IDR0_TTF_AARCH64		2
45 #define IDR0_TTF_AARCH32_64		3
46 #define IDR0_S1P			(1 << 1)
47 #define IDR0_S2P			(1 << 0)
48 
49 #define ARM_SMMU_IDR1			0x4
50 #define IDR1_TABLES_PRESET		(1 << 30)
51 #define IDR1_QUEUES_PRESET		(1 << 29)
52 #define IDR1_REL			(1 << 28)
53 #define IDR1_ATTR_TYPES_OVR		(1 << 27)
54 #define IDR1_CMDQS			GENMASK(25, 21)
55 #define IDR1_EVTQS			GENMASK(20, 16)
56 #define IDR1_PRIQS			GENMASK(15, 11)
57 #define IDR1_SSIDSIZE			GENMASK(10, 6)
58 #define IDR1_SIDSIZE			GENMASK(5, 0)
59 
60 #define ARM_SMMU_IDR3			0xc
61 #define IDR3_FWB			(1 << 8)
62 #define IDR3_RIL			(1 << 10)
63 
64 #define ARM_SMMU_IDR5			0x14
65 #define IDR5_STALL_MAX			GENMASK(31, 16)
66 #define IDR5_GRAN64K			(1 << 6)
67 #define IDR5_GRAN16K			(1 << 5)
68 #define IDR5_GRAN4K			(1 << 4)
69 #define IDR5_OAS			GENMASK(2, 0)
70 #define IDR5_OAS_32_BIT			0
71 #define IDR5_OAS_36_BIT			1
72 #define IDR5_OAS_40_BIT			2
73 #define IDR5_OAS_42_BIT			3
74 #define IDR5_OAS_44_BIT			4
75 #define IDR5_OAS_48_BIT			5
76 #define IDR5_OAS_52_BIT			6
77 #define IDR5_VAX			GENMASK(11, 10)
78 #define IDR5_VAX_52_BIT			1
79 
80 #define ARM_SMMU_IIDR			0x18
81 #define IIDR_PRODUCTID			GENMASK(31, 20)
82 #define IIDR_VARIANT			GENMASK(19, 16)
83 #define IIDR_REVISION			GENMASK(15, 12)
84 #define IIDR_IMPLEMENTER		GENMASK(11, 0)
85 
86 #define ARM_SMMU_AIDR			0x1C
87 
88 #define ARM_SMMU_CR0			0x20
89 #define CR0_ATSCHK			(1 << 4)
90 #define CR0_CMDQEN			(1 << 3)
91 #define CR0_EVTQEN			(1 << 2)
92 #define CR0_PRIQEN			(1 << 1)
93 #define CR0_SMMUEN			(1 << 0)
94 
95 #define ARM_SMMU_CR0ACK			0x24
96 
97 #define ARM_SMMU_CR1			0x28
98 #define CR1_TABLE_SH			GENMASK(11, 10)
99 #define CR1_TABLE_OC			GENMASK(9, 8)
100 #define CR1_TABLE_IC			GENMASK(7, 6)
101 #define CR1_QUEUE_SH			GENMASK(5, 4)
102 #define CR1_QUEUE_OC			GENMASK(3, 2)
103 #define CR1_QUEUE_IC			GENMASK(1, 0)
104 /* CR1 cacheability fields don't quite follow the usual TCR-style encoding */
105 #define CR1_CACHE_NC			0
106 #define CR1_CACHE_WB			1
107 #define CR1_CACHE_WT			2
108 
109 #define ARM_SMMU_CR2			0x2c
110 #define CR2_PTM				(1 << 2)
111 #define CR2_RECINVSID			(1 << 1)
112 #define CR2_E2H				(1 << 0)
113 
114 #define ARM_SMMU_GBPA			0x44
115 #define GBPA_UPDATE			(1 << 31)
116 #define GBPA_ABORT			(1 << 20)
117 
118 #define ARM_SMMU_IRQ_CTRL		0x50
119 #define IRQ_CTRL_EVTQ_IRQEN		(1 << 2)
120 #define IRQ_CTRL_PRIQ_IRQEN		(1 << 1)
121 #define IRQ_CTRL_GERROR_IRQEN		(1 << 0)
122 
123 #define ARM_SMMU_IRQ_CTRLACK		0x54
124 
125 #define ARM_SMMU_GERROR			0x60
126 #define GERROR_SFM_ERR			(1 << 8)
127 #define GERROR_MSI_GERROR_ABT_ERR	(1 << 7)
128 #define GERROR_MSI_PRIQ_ABT_ERR		(1 << 6)
129 #define GERROR_MSI_EVTQ_ABT_ERR		(1 << 5)
130 #define GERROR_MSI_CMDQ_ABT_ERR		(1 << 4)
131 #define GERROR_PRIQ_ABT_ERR		(1 << 3)
132 #define GERROR_EVTQ_ABT_ERR		(1 << 2)
133 #define GERROR_CMDQ_ERR			(1 << 0)
134 #define GERROR_ERR_MASK			0x1fd
135 
136 #define ARM_SMMU_GERRORN		0x64
137 
138 #define ARM_SMMU_GERROR_IRQ_CFG0	0x68
139 #define ARM_SMMU_GERROR_IRQ_CFG1	0x70
140 #define ARM_SMMU_GERROR_IRQ_CFG2	0x74
141 
142 #define ARM_SMMU_STRTAB_BASE		0x80
143 #define STRTAB_BASE_RA			(1UL << 62)
144 #define STRTAB_BASE_ADDR_MASK		GENMASK_ULL(51, 6)
145 
146 #define ARM_SMMU_STRTAB_BASE_CFG	0x88
147 #define STRTAB_BASE_CFG_FMT		GENMASK(17, 16)
148 #define STRTAB_BASE_CFG_FMT_LINEAR	0
149 #define STRTAB_BASE_CFG_FMT_2LVL	1
150 #define STRTAB_BASE_CFG_SPLIT		GENMASK(10, 6)
151 #define STRTAB_BASE_CFG_LOG2SIZE	GENMASK(5, 0)
152 
153 #define ARM_SMMU_CMDQ_BASE		0x90
154 #define ARM_SMMU_CMDQ_PROD		0x98
155 #define ARM_SMMU_CMDQ_CONS		0x9c
156 
157 #define ARM_SMMU_EVTQ_BASE		0xa0
158 #define ARM_SMMU_EVTQ_PROD		0xa8
159 #define ARM_SMMU_EVTQ_CONS		0xac
160 #define ARM_SMMU_EVTQ_IRQ_CFG0		0xb0
161 #define ARM_SMMU_EVTQ_IRQ_CFG1		0xb8
162 #define ARM_SMMU_EVTQ_IRQ_CFG2		0xbc
163 
164 #define ARM_SMMU_PRIQ_BASE		0xc0
165 #define ARM_SMMU_PRIQ_PROD		0xc8
166 #define ARM_SMMU_PRIQ_CONS		0xcc
167 #define ARM_SMMU_PRIQ_IRQ_CFG0		0xd0
168 #define ARM_SMMU_PRIQ_IRQ_CFG1		0xd8
169 #define ARM_SMMU_PRIQ_IRQ_CFG2		0xdc
170 
171 #define ARM_SMMU_REG_SZ			0xe00
172 
173 /* Common MSI config fields */
174 #define MSI_CFG0_ADDR_MASK		GENMASK_ULL(51, 2)
175 #define MSI_CFG2_SH			GENMASK(5, 4)
176 #define MSI_CFG2_MEMATTR		GENMASK(3, 0)
177 
178 /* Common memory attribute values */
179 #define ARM_SMMU_SH_NSH			0
180 #define ARM_SMMU_SH_OSH			2
181 #define ARM_SMMU_SH_ISH			3
182 #define ARM_SMMU_MEMATTR_DEVICE_nGnRE	0x1
183 #define ARM_SMMU_MEMATTR_OIWB		0xf
184 
185 #define Q_IDX(llq, p)			((p) & ((1 << (llq)->max_n_shift) - 1))
186 #define Q_WRP(llq, p)			((p) & (1 << (llq)->max_n_shift))
187 #define Q_OVERFLOW_FLAG			(1U << 31)
188 #define Q_OVF(p)			((p) & Q_OVERFLOW_FLAG)
189 #define Q_ENT(q, p)			((q)->base +			\
190 					 Q_IDX(&((q)->llq), p) *	\
191 					 (q)->ent_dwords)
192 
193 #define Q_BASE_RWA			(1UL << 62)
194 #define Q_BASE_ADDR_MASK		GENMASK_ULL(51, 5)
195 #define Q_BASE_LOG2SIZE			GENMASK(4, 0)
196 
197 /* Ensure DMA allocations are naturally aligned */
198 #ifdef CONFIG_CMA_ALIGNMENT
199 #define Q_MAX_SZ_SHIFT			(PAGE_SHIFT + CONFIG_CMA_ALIGNMENT)
200 #else
201 #define Q_MAX_SZ_SHIFT			(PAGE_SHIFT + MAX_PAGE_ORDER)
202 #endif
203 
204 /*
205  * Stream table.
206  *
207  * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
208  * 2lvl: 128k L1 entries,
209  *       256 lazy entries per table (each table covers a PCI bus)
210  */
211 #define STRTAB_SPLIT			8
212 
213 #define STRTAB_L1_DESC_SPAN		GENMASK_ULL(4, 0)
214 #define STRTAB_L1_DESC_L2PTR_MASK	GENMASK_ULL(51, 6)
215 
216 #define STRTAB_STE_DWORDS		8
217 
218 struct arm_smmu_ste {
219 	__le64 data[STRTAB_STE_DWORDS];
220 };
221 
222 #define STRTAB_NUM_L2_STES		(1 << STRTAB_SPLIT)
223 struct arm_smmu_strtab_l2 {
224 	struct arm_smmu_ste stes[STRTAB_NUM_L2_STES];
225 };
226 
227 struct arm_smmu_strtab_l1 {
228 	__le64 l2ptr;
229 };
230 #define STRTAB_MAX_L1_ENTRIES		(1 << 17)
231 
arm_smmu_strtab_l1_idx(u32 sid)232 static inline u32 arm_smmu_strtab_l1_idx(u32 sid)
233 {
234 	return sid / STRTAB_NUM_L2_STES;
235 }
236 
arm_smmu_strtab_l2_idx(u32 sid)237 static inline u32 arm_smmu_strtab_l2_idx(u32 sid)
238 {
239 	return sid % STRTAB_NUM_L2_STES;
240 }
241 
242 #define STRTAB_STE_0_V			(1UL << 0)
243 #define STRTAB_STE_0_CFG		GENMASK_ULL(3, 1)
244 #define STRTAB_STE_0_CFG_ABORT		0
245 #define STRTAB_STE_0_CFG_BYPASS		4
246 #define STRTAB_STE_0_CFG_S1_TRANS	5
247 #define STRTAB_STE_0_CFG_S2_TRANS	6
248 #define STRTAB_STE_0_CFG_NESTED		7
249 
250 #define STRTAB_STE_0_S1FMT		GENMASK_ULL(5, 4)
251 #define STRTAB_STE_0_S1FMT_LINEAR	0
252 #define STRTAB_STE_0_S1FMT_64K_L2	2
253 #define STRTAB_STE_0_S1CTXPTR_MASK	GENMASK_ULL(51, 6)
254 #define STRTAB_STE_0_S1CDMAX		GENMASK_ULL(63, 59)
255 
256 #define STRTAB_STE_1_S1DSS		GENMASK_ULL(1, 0)
257 #define STRTAB_STE_1_S1DSS_TERMINATE	0x0
258 #define STRTAB_STE_1_S1DSS_BYPASS	0x1
259 #define STRTAB_STE_1_S1DSS_SSID0	0x2
260 
261 #define STRTAB_STE_1_S1C_CACHE_NC	0UL
262 #define STRTAB_STE_1_S1C_CACHE_WBRA	1UL
263 #define STRTAB_STE_1_S1C_CACHE_WT	2UL
264 #define STRTAB_STE_1_S1C_CACHE_WB	3UL
265 #define STRTAB_STE_1_S1CIR		GENMASK_ULL(3, 2)
266 #define STRTAB_STE_1_S1COR		GENMASK_ULL(5, 4)
267 #define STRTAB_STE_1_S1CSH		GENMASK_ULL(7, 6)
268 
269 #define STRTAB_STE_1_MEV		(1UL << 19)
270 #define STRTAB_STE_1_S2FWB		(1UL << 25)
271 #define STRTAB_STE_1_S1STALLD		(1UL << 27)
272 
273 #define STRTAB_STE_1_EATS		GENMASK_ULL(29, 28)
274 #define STRTAB_STE_1_EATS_ABT		0UL
275 #define STRTAB_STE_1_EATS_TRANS		1UL
276 #define STRTAB_STE_1_EATS_S1CHK		2UL
277 
278 #define STRTAB_STE_1_STRW		GENMASK_ULL(31, 30)
279 #define STRTAB_STE_1_STRW_NSEL1		0UL
280 #define STRTAB_STE_1_STRW_EL2		2UL
281 
282 #define STRTAB_STE_1_SHCFG		GENMASK_ULL(45, 44)
283 #define STRTAB_STE_1_SHCFG_INCOMING	1UL
284 
285 #define STRTAB_STE_2_S2VMID		GENMASK_ULL(15, 0)
286 #define STRTAB_STE_2_VTCR		GENMASK_ULL(50, 32)
287 #define STRTAB_STE_2_VTCR_S2T0SZ	GENMASK_ULL(5, 0)
288 #define STRTAB_STE_2_VTCR_S2SL0		GENMASK_ULL(7, 6)
289 #define STRTAB_STE_2_VTCR_S2IR0		GENMASK_ULL(9, 8)
290 #define STRTAB_STE_2_VTCR_S2OR0		GENMASK_ULL(11, 10)
291 #define STRTAB_STE_2_VTCR_S2SH0		GENMASK_ULL(13, 12)
292 #define STRTAB_STE_2_VTCR_S2TG		GENMASK_ULL(15, 14)
293 #define STRTAB_STE_2_VTCR_S2PS		GENMASK_ULL(18, 16)
294 #define STRTAB_STE_2_S2AA64		(1UL << 51)
295 #define STRTAB_STE_2_S2ENDI		(1UL << 52)
296 #define STRTAB_STE_2_S2PTW		(1UL << 54)
297 #define STRTAB_STE_2_S2S		(1UL << 57)
298 #define STRTAB_STE_2_S2R		(1UL << 58)
299 
300 #define STRTAB_STE_3_S2TTB_MASK		GENMASK_ULL(51, 4)
301 
302 /* These bits can be controlled by userspace for STRTAB_STE_0_CFG_NESTED */
303 #define STRTAB_STE_0_NESTING_ALLOWED                                         \
304 	cpu_to_le64(STRTAB_STE_0_V | STRTAB_STE_0_CFG | STRTAB_STE_0_S1FMT | \
305 		    STRTAB_STE_0_S1CTXPTR_MASK | STRTAB_STE_0_S1CDMAX)
306 #define STRTAB_STE_1_NESTING_ALLOWED                            \
307 	cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR |   \
308 		    STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH |   \
309 		    STRTAB_STE_1_S1STALLD | STRTAB_STE_1_EATS)
310 
311 /*
312  * Context descriptors.
313  *
314  * Linear: when less than 1024 SSIDs are supported
315  * 2lvl: at most 1024 L1 entries,
316  *       1024 lazy entries per table.
317  */
318 #define CTXDESC_L2_ENTRIES		1024
319 
320 #define CTXDESC_L1_DESC_V		(1UL << 0)
321 #define CTXDESC_L1_DESC_L2PTR_MASK	GENMASK_ULL(51, 12)
322 
323 #define CTXDESC_CD_DWORDS		8
324 
325 struct arm_smmu_cd {
326 	__le64 data[CTXDESC_CD_DWORDS];
327 };
328 
329 struct arm_smmu_cdtab_l2 {
330 	struct arm_smmu_cd cds[CTXDESC_L2_ENTRIES];
331 };
332 
333 struct arm_smmu_cdtab_l1 {
334 	__le64 l2ptr;
335 };
336 
arm_smmu_cdtab_l1_idx(unsigned int ssid)337 static inline unsigned int arm_smmu_cdtab_l1_idx(unsigned int ssid)
338 {
339 	return ssid / CTXDESC_L2_ENTRIES;
340 }
341 
arm_smmu_cdtab_l2_idx(unsigned int ssid)342 static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid)
343 {
344 	return ssid % CTXDESC_L2_ENTRIES;
345 }
346 
347 #define CTXDESC_CD_0_TCR_T0SZ		GENMASK_ULL(5, 0)
348 #define CTXDESC_CD_0_TCR_TG0		GENMASK_ULL(7, 6)
349 #define CTXDESC_CD_0_TCR_IRGN0		GENMASK_ULL(9, 8)
350 #define CTXDESC_CD_0_TCR_ORGN0		GENMASK_ULL(11, 10)
351 #define CTXDESC_CD_0_TCR_SH0		GENMASK_ULL(13, 12)
352 #define CTXDESC_CD_0_TCR_EPD0		(1ULL << 14)
353 #define CTXDESC_CD_0_TCR_EPD1		(1ULL << 30)
354 
355 #define CTXDESC_CD_0_ENDI		(1UL << 15)
356 #define CTXDESC_CD_0_V			(1UL << 31)
357 
358 #define CTXDESC_CD_0_TCR_IPS		GENMASK_ULL(34, 32)
359 #define CTXDESC_CD_0_TCR_TBI0		(1ULL << 38)
360 
361 #define CTXDESC_CD_0_TCR_HA            (1UL << 43)
362 #define CTXDESC_CD_0_TCR_HD            (1UL << 42)
363 
364 #define CTXDESC_CD_0_AA64		(1UL << 41)
365 #define CTXDESC_CD_0_S			(1UL << 44)
366 #define CTXDESC_CD_0_R			(1UL << 45)
367 #define CTXDESC_CD_0_A			(1UL << 46)
368 #define CTXDESC_CD_0_ASET		(1UL << 47)
369 #define CTXDESC_CD_0_ASID		GENMASK_ULL(63, 48)
370 
371 #define CTXDESC_CD_1_TTB0_MASK		GENMASK_ULL(51, 4)
372 
373 /*
374  * When the SMMU only supports linear context descriptor tables, pick a
375  * reasonable size limit (64kB).
376  */
377 #define CTXDESC_LINEAR_CDMAX		ilog2(SZ_64K / sizeof(struct arm_smmu_cd))
378 
379 /* Command queue */
380 #define CMDQ_ENT_SZ_SHIFT		4
381 #define CMDQ_ENT_DWORDS			((1 << CMDQ_ENT_SZ_SHIFT) >> 3)
382 #define CMDQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - CMDQ_ENT_SZ_SHIFT)
383 
384 #define CMDQ_CONS_ERR			GENMASK(30, 24)
385 #define CMDQ_ERR_CERROR_NONE_IDX	0
386 #define CMDQ_ERR_CERROR_ILL_IDX		1
387 #define CMDQ_ERR_CERROR_ABT_IDX		2
388 #define CMDQ_ERR_CERROR_ATC_INV_IDX	3
389 
390 #define CMDQ_PROD_OWNED_FLAG		Q_OVERFLOW_FLAG
391 
392 /*
393  * This is used to size the command queue and therefore must be at least
394  * BITS_PER_LONG so that the valid_map works correctly (it relies on the
395  * total number of queue entries being a multiple of BITS_PER_LONG).
396  */
397 #define CMDQ_BATCH_ENTRIES		BITS_PER_LONG
398 
399 #define CMDQ_0_OP			GENMASK_ULL(7, 0)
400 #define CMDQ_0_SSV			(1UL << 11)
401 
402 #define CMDQ_PREFETCH_0_SID		GENMASK_ULL(63, 32)
403 #define CMDQ_PREFETCH_1_SIZE		GENMASK_ULL(4, 0)
404 #define CMDQ_PREFETCH_1_ADDR_MASK	GENMASK_ULL(63, 12)
405 
406 #define CMDQ_CFGI_0_SSID		GENMASK_ULL(31, 12)
407 #define CMDQ_CFGI_0_SID			GENMASK_ULL(63, 32)
408 #define CMDQ_CFGI_1_LEAF		(1UL << 0)
409 #define CMDQ_CFGI_1_RANGE		GENMASK_ULL(4, 0)
410 
411 #define CMDQ_TLBI_0_NUM			GENMASK_ULL(16, 12)
412 #define CMDQ_TLBI_RANGE_NUM_MAX		31
413 #define CMDQ_TLBI_0_SCALE		GENMASK_ULL(24, 20)
414 #define CMDQ_TLBI_0_VMID		GENMASK_ULL(47, 32)
415 #define CMDQ_TLBI_0_ASID		GENMASK_ULL(63, 48)
416 #define CMDQ_TLBI_1_LEAF		(1UL << 0)
417 #define CMDQ_TLBI_1_TTL			GENMASK_ULL(9, 8)
418 #define CMDQ_TLBI_1_TG			GENMASK_ULL(11, 10)
419 #define CMDQ_TLBI_1_VA_MASK		GENMASK_ULL(63, 12)
420 #define CMDQ_TLBI_1_IPA_MASK		GENMASK_ULL(51, 12)
421 
422 #define CMDQ_ATC_0_SSID			GENMASK_ULL(31, 12)
423 #define CMDQ_ATC_0_SID			GENMASK_ULL(63, 32)
424 #define CMDQ_ATC_0_GLOBAL		(1UL << 9)
425 #define CMDQ_ATC_1_SIZE			GENMASK_ULL(5, 0)
426 #define CMDQ_ATC_1_ADDR_MASK		GENMASK_ULL(63, 12)
427 
428 #define CMDQ_PRI_0_SSID			GENMASK_ULL(31, 12)
429 #define CMDQ_PRI_0_SID			GENMASK_ULL(63, 32)
430 #define CMDQ_PRI_1_GRPID		GENMASK_ULL(8, 0)
431 #define CMDQ_PRI_1_RESP			GENMASK_ULL(13, 12)
432 
433 #define CMDQ_RESUME_0_RESP_TERM		0UL
434 #define CMDQ_RESUME_0_RESP_RETRY	1UL
435 #define CMDQ_RESUME_0_RESP_ABORT	2UL
436 #define CMDQ_RESUME_0_RESP		GENMASK_ULL(13, 12)
437 #define CMDQ_RESUME_0_SID		GENMASK_ULL(63, 32)
438 #define CMDQ_RESUME_1_STAG		GENMASK_ULL(15, 0)
439 
440 #define CMDQ_SYNC_0_CS			GENMASK_ULL(13, 12)
441 #define CMDQ_SYNC_0_CS_NONE		0
442 #define CMDQ_SYNC_0_CS_IRQ		1
443 #define CMDQ_SYNC_0_CS_SEV		2
444 #define CMDQ_SYNC_0_MSH			GENMASK_ULL(23, 22)
445 #define CMDQ_SYNC_0_MSIATTR		GENMASK_ULL(27, 24)
446 #define CMDQ_SYNC_0_MSIDATA		GENMASK_ULL(63, 32)
447 #define CMDQ_SYNC_1_MSIADDR_MASK	GENMASK_ULL(51, 2)
448 
449 /* Event queue */
450 #define EVTQ_ENT_SZ_SHIFT		5
451 #define EVTQ_ENT_DWORDS			((1 << EVTQ_ENT_SZ_SHIFT) >> 3)
452 #define EVTQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - EVTQ_ENT_SZ_SHIFT)
453 
454 #define EVTQ_0_ID			GENMASK_ULL(7, 0)
455 
456 #define EVT_ID_BAD_STREAMID_CONFIG	0x02
457 #define EVT_ID_STE_FETCH_FAULT		0x03
458 #define EVT_ID_BAD_STE_CONFIG		0x04
459 #define EVT_ID_STREAM_DISABLED_FAULT	0x06
460 #define EVT_ID_BAD_SUBSTREAMID_CONFIG	0x08
461 #define EVT_ID_CD_FETCH_FAULT		0x09
462 #define EVT_ID_BAD_CD_CONFIG		0x0a
463 #define EVT_ID_TRANSLATION_FAULT	0x10
464 #define EVT_ID_ADDR_SIZE_FAULT		0x11
465 #define EVT_ID_ACCESS_FAULT		0x12
466 #define EVT_ID_PERMISSION_FAULT		0x13
467 #define EVT_ID_VMS_FETCH_FAULT		0x25
468 
469 #define EVTQ_0_SSV			(1UL << 11)
470 #define EVTQ_0_SSID			GENMASK_ULL(31, 12)
471 #define EVTQ_0_SID			GENMASK_ULL(63, 32)
472 #define EVTQ_1_STAG			GENMASK_ULL(15, 0)
473 #define EVTQ_1_STALL			(1UL << 31)
474 #define EVTQ_1_PnU			(1UL << 33)
475 #define EVTQ_1_InD			(1UL << 34)
476 #define EVTQ_1_RnW			(1UL << 35)
477 #define EVTQ_1_S2			(1UL << 39)
478 #define EVTQ_1_CLASS			GENMASK_ULL(41, 40)
479 #define EVTQ_1_CLASS_TT			0x01
480 #define EVTQ_1_TT_READ			(1UL << 44)
481 #define EVTQ_2_ADDR			GENMASK_ULL(63, 0)
482 #define EVTQ_3_IPA			GENMASK_ULL(51, 12)
483 #define EVTQ_3_FETCH_ADDR		GENMASK_ULL(51, 3)
484 
485 /* PRI queue */
486 #define PRIQ_ENT_SZ_SHIFT		4
487 #define PRIQ_ENT_DWORDS			((1 << PRIQ_ENT_SZ_SHIFT) >> 3)
488 #define PRIQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - PRIQ_ENT_SZ_SHIFT)
489 
490 #define PRIQ_0_SID			GENMASK_ULL(31, 0)
491 #define PRIQ_0_SSID			GENMASK_ULL(51, 32)
492 #define PRIQ_0_PERM_PRIV		(1UL << 58)
493 #define PRIQ_0_PERM_EXEC		(1UL << 59)
494 #define PRIQ_0_PERM_READ		(1UL << 60)
495 #define PRIQ_0_PERM_WRITE		(1UL << 61)
496 #define PRIQ_0_PRG_LAST			(1UL << 62)
497 #define PRIQ_0_SSID_V			(1UL << 63)
498 
499 #define PRIQ_1_PRG_IDX			GENMASK_ULL(8, 0)
500 #define PRIQ_1_ADDR_MASK		GENMASK_ULL(63, 12)
501 
502 /* High-level queue structures */
503 #define ARM_SMMU_POLL_TIMEOUT_US	1000000 /* 1s! */
504 #define ARM_SMMU_POLL_SPIN_COUNT	10
505 
506 #define MSI_IOVA_BASE			0x8000000
507 #define MSI_IOVA_LENGTH			0x100000
508 
509 enum pri_resp {
510 	PRI_RESP_DENY = 0,
511 	PRI_RESP_FAIL = 1,
512 	PRI_RESP_SUCC = 2,
513 };
514 
515 struct arm_smmu_cmdq_ent {
516 	/* Common fields */
517 	u8				opcode;
518 	bool				substream_valid;
519 
520 	/* Command-specific fields */
521 	union {
522 		#define CMDQ_OP_PREFETCH_CFG	0x1
523 		struct {
524 			u32			sid;
525 		} prefetch;
526 
527 		#define CMDQ_OP_CFGI_STE	0x3
528 		#define CMDQ_OP_CFGI_ALL	0x4
529 		#define CMDQ_OP_CFGI_CD		0x5
530 		#define CMDQ_OP_CFGI_CD_ALL	0x6
531 		struct {
532 			u32			sid;
533 			u32			ssid;
534 			union {
535 				bool		leaf;
536 				u8		span;
537 			};
538 		} cfgi;
539 
540 		#define CMDQ_OP_TLBI_NH_ALL     0x10
541 		#define CMDQ_OP_TLBI_NH_ASID	0x11
542 		#define CMDQ_OP_TLBI_NH_VA	0x12
543 		#define CMDQ_OP_TLBI_NH_VAA	0x13
544 		#define CMDQ_OP_TLBI_EL2_ALL	0x20
545 		#define CMDQ_OP_TLBI_EL2_ASID	0x21
546 		#define CMDQ_OP_TLBI_EL2_VA	0x22
547 		#define CMDQ_OP_TLBI_S12_VMALL	0x28
548 		#define CMDQ_OP_TLBI_S2_IPA	0x2a
549 		#define CMDQ_OP_TLBI_NSNH_ALL	0x30
550 		struct {
551 			u8			num;
552 			u8			scale;
553 			u16			asid;
554 			u16			vmid;
555 			bool			leaf;
556 			u8			ttl;
557 			u8			tg;
558 			u64			addr;
559 		} tlbi;
560 
561 		#define CMDQ_OP_ATC_INV		0x40
562 		#define ATC_INV_SIZE_ALL	52
563 		struct {
564 			u32			sid;
565 			u32			ssid;
566 			u64			addr;
567 			u8			size;
568 			bool			global;
569 		} atc;
570 
571 		#define CMDQ_OP_PRI_RESP	0x41
572 		struct {
573 			u32			sid;
574 			u32			ssid;
575 			u16			grpid;
576 			enum pri_resp		resp;
577 		} pri;
578 
579 		#define CMDQ_OP_RESUME		0x44
580 		struct {
581 			u32			sid;
582 			u16			stag;
583 			u8			resp;
584 		} resume;
585 
586 		#define CMDQ_OP_CMD_SYNC	0x46
587 		struct {
588 			u64			msiaddr;
589 		} sync;
590 	};
591 };
592 
593 struct arm_smmu_ll_queue {
594 	union {
595 		u64			val;
596 		struct {
597 			u32		prod;
598 			u32		cons;
599 		};
600 		struct {
601 			atomic_t	prod;
602 			atomic_t	cons;
603 		} atomic;
604 		u8			__pad[SMP_CACHE_BYTES];
605 	} ____cacheline_aligned_in_smp;
606 	u32				max_n_shift;
607 };
608 
609 struct arm_smmu_queue {
610 	struct arm_smmu_ll_queue	llq;
611 	int				irq; /* Wired interrupt */
612 
613 	__le64				*base;
614 	dma_addr_t			base_dma;
615 	u64				q_base;
616 
617 	size_t				ent_dwords;
618 
619 	u32 __iomem			*prod_reg;
620 	u32 __iomem			*cons_reg;
621 };
622 
623 struct arm_smmu_queue_poll {
624 	ktime_t				timeout;
625 	unsigned int			delay;
626 	unsigned int			spin_cnt;
627 	bool				wfe;
628 };
629 
630 struct arm_smmu_cmdq {
631 	struct arm_smmu_queue		q;
632 	atomic_long_t			*valid_map;
633 	atomic_t			owner_prod;
634 	atomic_t			lock;
635 	bool				(*supports_cmd)(struct arm_smmu_cmdq_ent *ent);
636 };
637 
arm_smmu_cmdq_supports_cmd(struct arm_smmu_cmdq * cmdq,struct arm_smmu_cmdq_ent * ent)638 static inline bool arm_smmu_cmdq_supports_cmd(struct arm_smmu_cmdq *cmdq,
639 					      struct arm_smmu_cmdq_ent *ent)
640 {
641 	return cmdq->supports_cmd ? cmdq->supports_cmd(ent) : true;
642 }
643 
644 struct arm_smmu_cmdq_batch {
645 	u64				cmds[CMDQ_BATCH_ENTRIES * CMDQ_ENT_DWORDS];
646 	struct arm_smmu_cmdq		*cmdq;
647 	int				num;
648 };
649 
650 struct arm_smmu_evtq {
651 	struct arm_smmu_queue		q;
652 	struct iopf_queue		*iopf;
653 	u32				max_stalls;
654 };
655 
656 struct arm_smmu_priq {
657 	struct arm_smmu_queue		q;
658 };
659 
660 /* High-level stream table and context descriptor structures */
661 struct arm_smmu_ctx_desc {
662 	u16				asid;
663 };
664 
665 struct arm_smmu_ctx_desc_cfg {
666 	union {
667 		struct {
668 			struct arm_smmu_cd *table;
669 			unsigned int num_ents;
670 		} linear;
671 		struct {
672 			struct arm_smmu_cdtab_l1 *l1tab;
673 			struct arm_smmu_cdtab_l2 **l2ptrs;
674 			unsigned int num_l1_ents;
675 		} l2;
676 	};
677 	dma_addr_t			cdtab_dma;
678 	unsigned int			used_ssids;
679 	u8				in_ste;
680 	u8				s1fmt;
681 	/* log2 of the maximum number of CDs supported by this table */
682 	u8				s1cdmax;
683 };
684 
685 static inline bool
arm_smmu_cdtab_allocated(struct arm_smmu_ctx_desc_cfg * cfg)686 arm_smmu_cdtab_allocated(struct arm_smmu_ctx_desc_cfg *cfg)
687 {
688 	return cfg->linear.table || cfg->l2.l1tab;
689 }
690 
691 /* True if the cd table has SSIDS > 0 in use. */
arm_smmu_ssids_in_use(struct arm_smmu_ctx_desc_cfg * cd_table)692 static inline bool arm_smmu_ssids_in_use(struct arm_smmu_ctx_desc_cfg *cd_table)
693 {
694 	return cd_table->used_ssids;
695 }
696 
697 struct arm_smmu_s2_cfg {
698 	u16				vmid;
699 };
700 
701 struct arm_smmu_strtab_cfg {
702 	union {
703 		struct {
704 			struct arm_smmu_ste *table;
705 			dma_addr_t ste_dma;
706 			unsigned int num_ents;
707 		} linear;
708 		struct {
709 			struct arm_smmu_strtab_l1 *l1tab;
710 			struct arm_smmu_strtab_l2 **l2ptrs;
711 			dma_addr_t l1_dma;
712 			unsigned int num_l1_ents;
713 		} l2;
714 	};
715 };
716 
717 struct arm_smmu_impl_ops {
718 	int (*device_reset)(struct arm_smmu_device *smmu);
719 	void (*device_remove)(struct arm_smmu_device *smmu);
720 	int (*init_structures)(struct arm_smmu_device *smmu);
721 	struct arm_smmu_cmdq *(*get_secondary_cmdq)(
722 		struct arm_smmu_device *smmu, struct arm_smmu_cmdq_ent *ent);
723 };
724 
725 /* An SMMUv3 instance */
726 struct arm_smmu_device {
727 	struct device			*dev;
728 	struct device			*impl_dev;
729 	const struct arm_smmu_impl_ops	*impl_ops;
730 
731 	void __iomem			*base;
732 	void __iomem			*page1;
733 
734 #define ARM_SMMU_FEAT_2_LVL_STRTAB	(1 << 0)
735 #define ARM_SMMU_FEAT_2_LVL_CDTAB	(1 << 1)
736 #define ARM_SMMU_FEAT_TT_LE		(1 << 2)
737 #define ARM_SMMU_FEAT_TT_BE		(1 << 3)
738 #define ARM_SMMU_FEAT_PRI		(1 << 4)
739 #define ARM_SMMU_FEAT_ATS		(1 << 5)
740 #define ARM_SMMU_FEAT_SEV		(1 << 6)
741 #define ARM_SMMU_FEAT_MSI		(1 << 7)
742 #define ARM_SMMU_FEAT_COHERENCY		(1 << 8)
743 #define ARM_SMMU_FEAT_TRANS_S1		(1 << 9)
744 #define ARM_SMMU_FEAT_TRANS_S2		(1 << 10)
745 #define ARM_SMMU_FEAT_STALLS		(1 << 11)
746 #define ARM_SMMU_FEAT_HYP		(1 << 12)
747 #define ARM_SMMU_FEAT_STALL_FORCE	(1 << 13)
748 #define ARM_SMMU_FEAT_VAX		(1 << 14)
749 #define ARM_SMMU_FEAT_RANGE_INV		(1 << 15)
750 #define ARM_SMMU_FEAT_BTM		(1 << 16)
751 #define ARM_SMMU_FEAT_SVA		(1 << 17)
752 #define ARM_SMMU_FEAT_E2H		(1 << 18)
753 #define ARM_SMMU_FEAT_NESTING		(1 << 19)
754 #define ARM_SMMU_FEAT_ATTR_TYPES_OVR	(1 << 20)
755 #define ARM_SMMU_FEAT_HA		(1 << 21)
756 #define ARM_SMMU_FEAT_HD		(1 << 22)
757 #define ARM_SMMU_FEAT_S2FWB		(1 << 23)
758 	u32				features;
759 
760 #define ARM_SMMU_OPT_SKIP_PREFETCH	(1 << 0)
761 #define ARM_SMMU_OPT_PAGE0_REGS_ONLY	(1 << 1)
762 #define ARM_SMMU_OPT_MSIPOLL		(1 << 2)
763 #define ARM_SMMU_OPT_CMDQ_FORCE_SYNC	(1 << 3)
764 #define ARM_SMMU_OPT_TEGRA241_CMDQV	(1 << 4)
765 	u32				options;
766 
767 	struct arm_smmu_cmdq		cmdq;
768 	struct arm_smmu_evtq		evtq;
769 	struct arm_smmu_priq		priq;
770 
771 	int				gerr_irq;
772 	int				combined_irq;
773 
774 	unsigned long			ias; /* IPA */
775 	unsigned long			oas; /* PA */
776 	unsigned long			pgsize_bitmap;
777 
778 #define ARM_SMMU_MAX_ASIDS		(1 << 16)
779 	unsigned int			asid_bits;
780 
781 #define ARM_SMMU_MAX_VMIDS		(1 << 16)
782 	unsigned int			vmid_bits;
783 	struct ida			vmid_map;
784 
785 	unsigned int			ssid_bits;
786 	unsigned int			sid_bits;
787 
788 	struct arm_smmu_strtab_cfg	strtab_cfg;
789 
790 	/* IOMMU core code handle */
791 	struct iommu_device		iommu;
792 
793 	struct rb_root			streams;
794 	struct mutex			streams_mutex;
795 };
796 
797 struct arm_smmu_stream {
798 	u32				id;
799 	struct arm_smmu_master		*master;
800 	struct rb_node			node;
801 };
802 
803 struct arm_smmu_vmaster {
804 	struct arm_vsmmu		*vsmmu;
805 	unsigned long			vsid;
806 };
807 
808 struct arm_smmu_event {
809 	u8				stall : 1,
810 					ssv : 1,
811 					privileged : 1,
812 					instruction : 1,
813 					s2 : 1,
814 					read : 1,
815 					ttrnw : 1,
816 					class_tt : 1;
817 	u8				id;
818 	u8				class;
819 	u16				stag;
820 	u32				sid;
821 	u32				ssid;
822 	u64				iova;
823 	u64				ipa;
824 	u64				fetch_addr;
825 	struct device			*dev;
826 };
827 
828 /* SMMU private data for each master */
829 struct arm_smmu_master {
830 	struct arm_smmu_device		*smmu;
831 	struct device			*dev;
832 	struct arm_smmu_stream		*streams;
833 	struct arm_smmu_vmaster		*vmaster; /* use smmu->streams_mutex */
834 	/* Locked by the iommu core using the group mutex */
835 	struct arm_smmu_ctx_desc_cfg	cd_table;
836 	unsigned int			num_streams;
837 	bool				ats_enabled : 1;
838 	bool				ste_ats_enabled : 1;
839 	bool				stall_enabled;
840 	unsigned int			ssid_bits;
841 	unsigned int			iopf_refcount;
842 };
843 
844 /* SMMU private data for an IOMMU domain */
845 enum arm_smmu_domain_stage {
846 	ARM_SMMU_DOMAIN_S1 = 0,
847 	ARM_SMMU_DOMAIN_S2,
848 };
849 
850 struct arm_smmu_domain {
851 	struct arm_smmu_device		*smmu;
852 
853 	struct io_pgtable_ops		*pgtbl_ops;
854 	atomic_t			nr_ats_masters;
855 
856 	enum arm_smmu_domain_stage	stage;
857 	union {
858 		struct arm_smmu_ctx_desc	cd;
859 		struct arm_smmu_s2_cfg		s2_cfg;
860 	};
861 
862 	struct iommu_domain		domain;
863 
864 	/* List of struct arm_smmu_master_domain */
865 	struct list_head		devices;
866 	spinlock_t			devices_lock;
867 	bool				enforce_cache_coherency : 1;
868 	bool				nest_parent : 1;
869 
870 	struct mmu_notifier		mmu_notifier;
871 };
872 
873 struct arm_smmu_nested_domain {
874 	struct iommu_domain domain;
875 	struct arm_vsmmu *vsmmu;
876 	bool enable_ats : 1;
877 
878 	__le64 ste[2];
879 };
880 
881 /* The following are exposed for testing purposes. */
882 struct arm_smmu_entry_writer_ops;
883 struct arm_smmu_entry_writer {
884 	const struct arm_smmu_entry_writer_ops *ops;
885 	struct arm_smmu_master *master;
886 };
887 
888 struct arm_smmu_entry_writer_ops {
889 	void (*get_used)(const __le64 *entry, __le64 *used);
890 	void (*sync)(struct arm_smmu_entry_writer *writer);
891 };
892 
893 void arm_smmu_make_abort_ste(struct arm_smmu_ste *target);
894 void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target,
895 				 struct arm_smmu_master *master,
896 				 struct arm_smmu_domain *smmu_domain,
897 				 bool ats_enabled);
898 
899 #if IS_ENABLED(CONFIG_KUNIT)
900 void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits);
901 void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *cur,
902 			  const __le64 *target);
903 void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits);
904 void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu,
905 			      struct arm_smmu_ste *target);
906 void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target,
907 			       struct arm_smmu_master *master, bool ats_enabled,
908 			       unsigned int s1dss);
909 void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
910 			  struct arm_smmu_master *master, struct mm_struct *mm,
911 			  u16 asid);
912 #endif
913 
914 struct arm_smmu_master_domain {
915 	struct list_head devices_elm;
916 	struct arm_smmu_master *master;
917 	/*
918 	 * For nested domains the master_domain is threaded onto the S2 parent,
919 	 * this points to the IOMMU_DOMAIN_NESTED to disambiguate the masters.
920 	 */
921 	struct iommu_domain *domain;
922 	ioasid_t ssid;
923 	bool nested_ats_flush : 1;
924 	bool using_iopf : 1;
925 };
926 
to_smmu_domain(struct iommu_domain * dom)927 static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
928 {
929 	return container_of(dom, struct arm_smmu_domain, domain);
930 }
931 
932 static inline struct arm_smmu_nested_domain *
to_smmu_nested_domain(struct iommu_domain * dom)933 to_smmu_nested_domain(struct iommu_domain *dom)
934 {
935 	return container_of(dom, struct arm_smmu_nested_domain, domain);
936 }
937 
938 extern struct xarray arm_smmu_asid_xa;
939 extern struct mutex arm_smmu_asid_lock;
940 
941 struct arm_smmu_domain *arm_smmu_domain_alloc(void);
942 
943 void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid);
944 struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master,
945 					u32 ssid);
946 void arm_smmu_make_s1_cd(struct arm_smmu_cd *target,
947 			 struct arm_smmu_master *master,
948 			 struct arm_smmu_domain *smmu_domain);
949 void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,
950 			     struct arm_smmu_cd *cdptr,
951 			     const struct arm_smmu_cd *target);
952 
953 int arm_smmu_set_pasid(struct arm_smmu_master *master,
954 		       struct arm_smmu_domain *smmu_domain, ioasid_t pasid,
955 		       struct arm_smmu_cd *cd, struct iommu_domain *old);
956 
957 void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid);
958 void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,
959 				 size_t granule, bool leaf,
960 				 struct arm_smmu_domain *smmu_domain);
961 int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain,
962 			    unsigned long iova, size_t size);
963 
964 void __arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu,
965 			      struct arm_smmu_cmdq *cmdq);
966 int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
967 			    struct arm_smmu_queue *q, void __iomem *page,
968 			    unsigned long prod_off, unsigned long cons_off,
969 			    size_t dwords, const char *name);
970 int arm_smmu_cmdq_init(struct arm_smmu_device *smmu,
971 		       struct arm_smmu_cmdq *cmdq);
972 
arm_smmu_master_canwbs(struct arm_smmu_master * master)973 static inline bool arm_smmu_master_canwbs(struct arm_smmu_master *master)
974 {
975 	return dev_iommu_fwspec_get(master->dev)->flags &
976 	       IOMMU_FWSPEC_PCI_RC_CANWBS;
977 }
978 
979 struct arm_smmu_attach_state {
980 	/* Inputs */
981 	struct iommu_domain *old_domain;
982 	struct arm_smmu_master *master;
983 	bool cd_needs_ats;
984 	bool disable_ats;
985 	ioasid_t ssid;
986 	/* Resulting state */
987 	struct arm_smmu_vmaster *vmaster;
988 	bool ats_enabled;
989 };
990 
991 int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state,
992 			    struct iommu_domain *new_domain);
993 void arm_smmu_attach_commit(struct arm_smmu_attach_state *state);
994 void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master,
995 				  const struct arm_smmu_ste *target);
996 
997 int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
998 				struct arm_smmu_cmdq *cmdq, u64 *cmds, int n,
999 				bool sync);
1000 
1001 #ifdef CONFIG_ARM_SMMU_V3_SVA
1002 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu);
1003 void arm_smmu_sva_notifier_synchronize(void);
1004 struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev,
1005 					       struct mm_struct *mm);
1006 #else /* CONFIG_ARM_SMMU_V3_SVA */
arm_smmu_sva_supported(struct arm_smmu_device * smmu)1007 static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
1008 {
1009 	return false;
1010 }
1011 
arm_smmu_sva_notifier_synchronize(void)1012 static inline void arm_smmu_sva_notifier_synchronize(void) {}
1013 
1014 #define arm_smmu_sva_domain_alloc NULL
1015 
1016 #endif /* CONFIG_ARM_SMMU_V3_SVA */
1017 
1018 #ifdef CONFIG_TEGRA241_CMDQV
1019 struct arm_smmu_device *tegra241_cmdqv_probe(struct arm_smmu_device *smmu);
1020 #else /* CONFIG_TEGRA241_CMDQV */
1021 static inline struct arm_smmu_device *
tegra241_cmdqv_probe(struct arm_smmu_device * smmu)1022 tegra241_cmdqv_probe(struct arm_smmu_device *smmu)
1023 {
1024 	return ERR_PTR(-ENODEV);
1025 }
1026 #endif /* CONFIG_TEGRA241_CMDQV */
1027 
1028 struct arm_vsmmu {
1029 	struct iommufd_viommu core;
1030 	struct arm_smmu_device *smmu;
1031 	struct arm_smmu_domain *s2_parent;
1032 	u16 vmid;
1033 };
1034 
1035 #if IS_ENABLED(CONFIG_ARM_SMMU_V3_IOMMUFD)
1036 void *arm_smmu_hw_info(struct device *dev, u32 *length, u32 *type);
1037 struct iommufd_viommu *arm_vsmmu_alloc(struct device *dev,
1038 				       struct iommu_domain *parent,
1039 				       struct iommufd_ctx *ictx,
1040 				       unsigned int viommu_type);
1041 int arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state *state,
1042 				    struct arm_smmu_nested_domain *nested_domain);
1043 void arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state *state);
1044 void arm_smmu_master_clear_vmaster(struct arm_smmu_master *master);
1045 int arm_vmaster_report_event(struct arm_smmu_vmaster *vmaster, u64 *evt);
1046 #else
1047 #define arm_smmu_hw_info NULL
1048 #define arm_vsmmu_alloc NULL
1049 
1050 static inline int
arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state * state,struct arm_smmu_nested_domain * nested_domain)1051 arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state *state,
1052 				struct arm_smmu_nested_domain *nested_domain)
1053 {
1054 	return 0;
1055 }
1056 
1057 static inline void
arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state * state)1058 arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state *state)
1059 {
1060 }
1061 
1062 static inline void
arm_smmu_master_clear_vmaster(struct arm_smmu_master * master)1063 arm_smmu_master_clear_vmaster(struct arm_smmu_master *master)
1064 {
1065 }
1066 
arm_vmaster_report_event(struct arm_smmu_vmaster * vmaster,u64 * evt)1067 static inline int arm_vmaster_report_event(struct arm_smmu_vmaster *vmaster,
1068 					   u64 *evt)
1069 {
1070 	return -EOPNOTSUPP;
1071 }
1072 #endif /* CONFIG_ARM_SMMU_V3_IOMMUFD */
1073 
1074 #endif /* _ARM_SMMU_V3_H */
1075