xref: /linux/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h (revision 02b5c4639bcd3a07f1fad7662da32b8b73371597)
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 struct arm_vsmmu;
20 
21 /* MMIO registers */
22 #define ARM_SMMU_IDR0			0x0
23 #define IDR0_ST_LVL			GENMASK(28, 27)
24 #define IDR0_ST_LVL_2LVL		1
25 #define IDR0_STALL_MODEL		GENMASK(25, 24)
26 #define IDR0_STALL_MODEL_STALL		0
27 #define IDR0_STALL_MODEL_FORCE		2
28 #define IDR0_TTENDIAN			GENMASK(22, 21)
29 #define IDR0_TTENDIAN_MIXED		0
30 #define IDR0_TTENDIAN_LE		2
31 #define IDR0_TTENDIAN_BE		3
32 #define IDR0_CD2L			(1 << 19)
33 #define IDR0_VMID16			(1 << 18)
34 #define IDR0_PRI			(1 << 16)
35 #define IDR0_SEV			(1 << 14)
36 #define IDR0_MSI			(1 << 13)
37 #define IDR0_ASID16			(1 << 12)
38 #define IDR0_ATS			(1 << 10)
39 #define IDR0_HYP			(1 << 9)
40 #define IDR0_HTTU			GENMASK(7, 6)
41 #define IDR0_HTTU_ACCESS		1
42 #define IDR0_HTTU_ACCESS_DIRTY		2
43 #define IDR0_HTTU_ACCESS_DIRTY_HAFT	3
44 #define IDR0_COHACC			(1 << 4)
45 #define IDR0_TTF			GENMASK(3, 2)
46 #define IDR0_TTF_AARCH64		2
47 #define IDR0_S1P			(1 << 1)
48 #define IDR0_S2P			(1 << 0)
49 
50 #define ARM_SMMU_IDR1			0x4
51 #define IDR1_TABLES_PRESET		(1 << 30)
52 #define IDR1_QUEUES_PRESET		(1 << 29)
53 #define IDR1_REL			(1 << 28)
54 #define IDR1_ATTR_TYPES_OVR		(1 << 27)
55 #define IDR1_ATTR_PERMS_OVR		(1 << 26)
56 #define IDR1_CMDQS			GENMASK(25, 21)
57 #define IDR1_EVTQS			GENMASK(20, 16)
58 #define IDR1_PRIQS			GENMASK(15, 11)
59 #define IDR1_SSIDSIZE			GENMASK(10, 6)
60 #define IDR1_SIDSIZE			GENMASK(5, 0)
61 
62 #define ARM_SMMU_IDR3			0xc
63 #define IDR3_FWB			(1 << 8)
64 #define IDR3_RIL			(1 << 10)
65 #define IDR3_BBM			GENMASK(12, 11)
66 
67 #define ARM_SMMU_IDR5			0x14
68 #define IDR5_STALL_MAX			GENMASK(31, 16)
69 #define IDR5_DS				(1 << 7)
70 #define IDR5_GRAN64K			(1 << 6)
71 #define IDR5_GRAN16K			(1 << 5)
72 #define IDR5_GRAN4K			(1 << 4)
73 #define IDR5_OAS			GENMASK(2, 0)
74 #define IDR5_OAS_32_BIT			0
75 #define IDR5_OAS_36_BIT			1
76 #define IDR5_OAS_40_BIT			2
77 #define IDR5_OAS_42_BIT			3
78 #define IDR5_OAS_44_BIT			4
79 #define IDR5_OAS_48_BIT			5
80 #define IDR5_OAS_52_BIT			6
81 #define IDR5_VAX			GENMASK(11, 10)
82 #define IDR5_VAX_52_BIT			1
83 
84 #define ARM_SMMU_IIDR			0x18
85 #define IIDR_PRODUCTID			GENMASK(31, 20)
86 #define IIDR_VARIANT			GENMASK(19, 16)
87 #define IIDR_REVISION			GENMASK(15, 12)
88 #define IIDR_IMPLEMENTER		GENMASK(11, 0)
89 
90 #define ARM_SMMU_AIDR			0x1C
91 
92 #define ARM_SMMU_CR0			0x20
93 #define CR0_ATSCHK			(1 << 4)
94 #define CR0_CMDQEN			(1 << 3)
95 #define CR0_EVTQEN			(1 << 2)
96 #define CR0_PRIQEN			(1 << 1)
97 #define CR0_SMMUEN			(1 << 0)
98 
99 #define ARM_SMMU_CR0ACK			0x24
100 
101 #define ARM_SMMU_CR1			0x28
102 #define CR1_TABLE_SH			GENMASK(11, 10)
103 #define CR1_TABLE_OC			GENMASK(9, 8)
104 #define CR1_TABLE_IC			GENMASK(7, 6)
105 #define CR1_QUEUE_SH			GENMASK(5, 4)
106 #define CR1_QUEUE_OC			GENMASK(3, 2)
107 #define CR1_QUEUE_IC			GENMASK(1, 0)
108 /* CR1 cacheability fields don't quite follow the usual TCR-style encoding */
109 #define CR1_CACHE_NC			0
110 #define CR1_CACHE_WB			1
111 #define CR1_CACHE_WT			2
112 
113 #define ARM_SMMU_CR2			0x2c
114 #define CR2_PTM				(1 << 2)
115 #define CR2_RECINVSID			(1 << 1)
116 #define CR2_E2H				(1 << 0)
117 
118 #define ARM_SMMU_GBPA			0x44
119 #define GBPA_UPDATE			(1 << 31)
120 #define GBPA_ABORT			(1 << 20)
121 
122 #define ARM_SMMU_IRQ_CTRL		0x50
123 #define IRQ_CTRL_EVTQ_IRQEN		(1 << 2)
124 #define IRQ_CTRL_PRIQ_IRQEN		(1 << 1)
125 #define IRQ_CTRL_GERROR_IRQEN		(1 << 0)
126 
127 #define ARM_SMMU_IRQ_CTRLACK		0x54
128 
129 #define ARM_SMMU_GERROR			0x60
130 #define GERROR_SFM_ERR			(1 << 8)
131 #define GERROR_MSI_GERROR_ABT_ERR	(1 << 7)
132 #define GERROR_MSI_PRIQ_ABT_ERR		(1 << 6)
133 #define GERROR_MSI_EVTQ_ABT_ERR		(1 << 5)
134 #define GERROR_MSI_CMDQ_ABT_ERR		(1 << 4)
135 #define GERROR_PRIQ_ABT_ERR		(1 << 3)
136 #define GERROR_EVTQ_ABT_ERR		(1 << 2)
137 #define GERROR_CMDQ_ERR			(1 << 0)
138 #define GERROR_ERR_MASK			0x1fd
139 
140 #define ARM_SMMU_GERRORN		0x64
141 
142 #define ARM_SMMU_GERROR_IRQ_CFG0	0x68
143 #define ARM_SMMU_GERROR_IRQ_CFG1	0x70
144 #define ARM_SMMU_GERROR_IRQ_CFG2	0x74
145 
146 #define ARM_SMMU_STRTAB_BASE		0x80
147 #define STRTAB_BASE_RA			(1UL << 62)
148 #define STRTAB_BASE_ADDR_MASK		GENMASK_ULL(51, 6)
149 
150 #define ARM_SMMU_STRTAB_BASE_CFG	0x88
151 #define STRTAB_BASE_CFG_FMT		GENMASK(17, 16)
152 #define STRTAB_BASE_CFG_FMT_LINEAR	0
153 #define STRTAB_BASE_CFG_FMT_2LVL	1
154 #define STRTAB_BASE_CFG_SPLIT		GENMASK(10, 6)
155 #define STRTAB_BASE_CFG_LOG2SIZE	GENMASK(5, 0)
156 
157 #define ARM_SMMU_CMDQ_BASE		0x90
158 #define ARM_SMMU_CMDQ_PROD		0x98
159 #define ARM_SMMU_CMDQ_CONS		0x9c
160 
161 #define ARM_SMMU_EVTQ_BASE		0xa0
162 #define ARM_SMMU_EVTQ_PROD		0xa8
163 #define ARM_SMMU_EVTQ_CONS		0xac
164 #define ARM_SMMU_EVTQ_IRQ_CFG0		0xb0
165 #define ARM_SMMU_EVTQ_IRQ_CFG1		0xb8
166 #define ARM_SMMU_EVTQ_IRQ_CFG2		0xbc
167 
168 #define ARM_SMMU_PRIQ_BASE		0xc0
169 #define ARM_SMMU_PRIQ_PROD		0xc8
170 #define ARM_SMMU_PRIQ_CONS		0xcc
171 #define ARM_SMMU_PRIQ_IRQ_CFG0		0xd0
172 #define ARM_SMMU_PRIQ_IRQ_CFG1		0xd8
173 #define ARM_SMMU_PRIQ_IRQ_CFG2		0xdc
174 
175 #define ARM_SMMU_REG_SZ			0xe00
176 
177 /* Common MSI config fields */
178 #define MSI_CFG0_ADDR_MASK		GENMASK_ULL(51, 2)
179 #define MSI_CFG2_SH			GENMASK(5, 4)
180 #define MSI_CFG2_MEMATTR		GENMASK(3, 0)
181 
182 /* Common memory attribute values */
183 #define ARM_SMMU_SH_NSH			0
184 #define ARM_SMMU_SH_OSH			2
185 #define ARM_SMMU_SH_ISH			3
186 #define ARM_SMMU_MEMATTR_DEVICE_nGnRE	0x1
187 #define ARM_SMMU_MEMATTR_OIWB		0xf
188 
189 #define Q_IDX(llq, p)			((p) & ((1 << (llq)->max_n_shift) - 1))
190 #define Q_WRP(llq, p)			((p) & (1 << (llq)->max_n_shift))
191 /* A position is Q_WRP | Q_IDX, wrapping at twice the queue capacity */
192 #define Q_POS(llq, p)			(Q_WRP(llq, p) | Q_IDX(llq, p))
193 #define Q_OVERFLOW_FLAG			(1U << 31)
194 #define Q_OVF(p)			((p) & Q_OVERFLOW_FLAG)
195 #define Q_ENT(q, p)			((q)->base +			\
196 					 Q_IDX(&((q)->llq), p) *	\
197 					 (q)->ent_dwords)
198 
199 #define Q_BASE_RWA			(1UL << 62)
200 #define Q_BASE_ADDR_MASK		GENMASK_ULL(51, 5)
201 #define Q_BASE_LOG2SIZE			GENMASK(4, 0)
202 
203 /* Ensure DMA allocations are naturally aligned */
204 #ifdef CONFIG_CMA_ALIGNMENT
205 #define Q_MAX_SZ_SHIFT			(PAGE_SHIFT + CONFIG_CMA_ALIGNMENT)
206 #else
207 #define Q_MAX_SZ_SHIFT			(PAGE_SHIFT + MAX_PAGE_ORDER)
208 #endif
209 
210 /*
211  * Stream table.
212  *
213  * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
214  * 2lvl: 128k L1 entries,
215  *       256 lazy entries per table (each table covers a PCI bus)
216  */
217 #define STRTAB_SPLIT			8
218 
219 #define STRTAB_L1_DESC_SPAN		GENMASK_ULL(4, 0)
220 #define STRTAB_L1_DESC_L2PTR_MASK	GENMASK_ULL(51, 6)
221 
222 #define STRTAB_STE_DWORDS		8
223 
224 struct arm_smmu_ste {
225 	__le64 data[STRTAB_STE_DWORDS];
226 };
227 
228 #define STRTAB_NUM_L2_STES		(1 << STRTAB_SPLIT)
229 struct arm_smmu_strtab_l2 {
230 	struct arm_smmu_ste stes[STRTAB_NUM_L2_STES];
231 };
232 
233 struct arm_smmu_strtab_l1 {
234 	__le64 l2ptr;
235 };
236 #define STRTAB_MAX_L1_ENTRIES		(1 << 17)
237 
238 static inline u32 arm_smmu_strtab_l1_idx(u32 sid)
239 {
240 	return sid / STRTAB_NUM_L2_STES;
241 }
242 
243 static inline u32 arm_smmu_strtab_l2_idx(u32 sid)
244 {
245 	return sid % STRTAB_NUM_L2_STES;
246 }
247 
248 #define STRTAB_STE_0_V			(1UL << 0)
249 #define STRTAB_STE_0_CFG		GENMASK_ULL(3, 1)
250 #define STRTAB_STE_0_CFG_ABORT		0
251 #define STRTAB_STE_0_CFG_BYPASS		4
252 #define STRTAB_STE_0_CFG_S1_TRANS	5
253 #define STRTAB_STE_0_CFG_S2_TRANS	6
254 #define STRTAB_STE_0_CFG_NESTED		7
255 
256 #define STRTAB_STE_0_S1FMT		GENMASK_ULL(5, 4)
257 #define STRTAB_STE_0_S1FMT_LINEAR	0
258 #define STRTAB_STE_0_S1FMT_64K_L2	2
259 #define STRTAB_STE_0_S1CTXPTR_MASK	GENMASK_ULL(51, 6)
260 #define STRTAB_STE_0_S1CDMAX		GENMASK_ULL(63, 59)
261 
262 #define STRTAB_STE_1_S1DSS		GENMASK_ULL(1, 0)
263 #define STRTAB_STE_1_S1DSS_TERMINATE	0x0
264 #define STRTAB_STE_1_S1DSS_BYPASS	0x1
265 #define STRTAB_STE_1_S1DSS_SSID0	0x2
266 
267 #define STRTAB_STE_1_S1C_CACHE_NC	0UL
268 #define STRTAB_STE_1_S1C_CACHE_WBRA	1UL
269 #define STRTAB_STE_1_S1C_CACHE_WT	2UL
270 #define STRTAB_STE_1_S1C_CACHE_WB	3UL
271 #define STRTAB_STE_1_S1CIR		GENMASK_ULL(3, 2)
272 #define STRTAB_STE_1_S1COR		GENMASK_ULL(5, 4)
273 #define STRTAB_STE_1_S1CSH		GENMASK_ULL(7, 6)
274 
275 #define STRTAB_STE_1_MEV		(1UL << 19)
276 #define STRTAB_STE_1_S2FWB		(1UL << 25)
277 #define STRTAB_STE_1_S1STALLD		(1UL << 27)
278 
279 #define STRTAB_STE_1_EATS		GENMASK_ULL(29, 28)
280 #define STRTAB_STE_1_EATS_ABT		0UL
281 #define STRTAB_STE_1_EATS_TRANS		1UL
282 #define STRTAB_STE_1_EATS_S1CHK		2UL
283 
284 #define STRTAB_STE_1_STRW		GENMASK_ULL(31, 30)
285 #define STRTAB_STE_1_STRW_NSEL1		0UL
286 #define STRTAB_STE_1_STRW_EL2		2UL
287 
288 #define STRTAB_STE_1_SHCFG		GENMASK_ULL(45, 44)
289 #define STRTAB_STE_1_SHCFG_INCOMING	1UL
290 
291 #define STRTAB_STE_1_INSTCFG		GENMASK_ULL(51, 50)
292 #define STRTAB_STE_1_INSTCFG_INCOMING	0UL
293 #define STRTAB_STE_1_INSTCFG_DATA	2UL
294 
295 #define STRTAB_STE_2_S2VMID		GENMASK_ULL(15, 0)
296 #define STRTAB_STE_2_VTCR		GENMASK_ULL(50, 32)
297 #define STRTAB_STE_2_VTCR_S2T0SZ	GENMASK_ULL(5, 0)
298 #define STRTAB_STE_2_VTCR_S2SL0		GENMASK_ULL(7, 6)
299 #define STRTAB_STE_2_VTCR_S2IR0		GENMASK_ULL(9, 8)
300 #define STRTAB_STE_2_VTCR_S2OR0		GENMASK_ULL(11, 10)
301 #define STRTAB_STE_2_VTCR_S2SH0		GENMASK_ULL(13, 12)
302 #define STRTAB_STE_2_VTCR_S2TG		GENMASK_ULL(15, 14)
303 #define STRTAB_STE_2_VTCR_S2PS		GENMASK_ULL(18, 16)
304 #define STRTAB_STE_2_S2AA64		(1UL << 51)
305 #define STRTAB_STE_2_S2ENDI		(1UL << 52)
306 #define STRTAB_STE_2_S2PTW		(1UL << 54)
307 #define STRTAB_STE_2_S2S		(1UL << 57)
308 #define STRTAB_STE_2_S2R		(1UL << 58)
309 
310 #define STRTAB_STE_3_S2TTB_MASK		GENMASK_ULL(51, 4)
311 
312 /* These bits can be controlled by userspace for STRTAB_STE_0_CFG_NESTED */
313 #define STRTAB_STE_0_NESTING_ALLOWED                                         \
314 	cpu_to_le64(STRTAB_STE_0_V | STRTAB_STE_0_CFG | STRTAB_STE_0_S1FMT | \
315 		    STRTAB_STE_0_S1CTXPTR_MASK | STRTAB_STE_0_S1CDMAX)
316 #define STRTAB_STE_1_NESTING_ALLOWED                            \
317 	cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR |   \
318 		    STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH |   \
319 		    STRTAB_STE_1_S1STALLD | STRTAB_STE_1_EATS)
320 
321 /*
322  * Context descriptors.
323  *
324  * Linear: when less than 1024 SSIDs are supported
325  * 2lvl: at most 1024 L1 entries,
326  *       1024 lazy entries per table.
327  */
328 #define CTXDESC_L2_ENTRIES		1024
329 
330 #define CTXDESC_L1_DESC_V		(1UL << 0)
331 #define CTXDESC_L1_DESC_L2PTR_MASK	GENMASK_ULL(51, 12)
332 
333 #define CTXDESC_CD_DWORDS		8
334 
335 struct arm_smmu_cd {
336 	__le64 data[CTXDESC_CD_DWORDS];
337 };
338 
339 struct arm_smmu_cdtab_l2 {
340 	struct arm_smmu_cd cds[CTXDESC_L2_ENTRIES];
341 };
342 
343 struct arm_smmu_cdtab_l1 {
344 	__le64 l2ptr;
345 };
346 
347 static inline unsigned int arm_smmu_cdtab_l1_idx(unsigned int ssid)
348 {
349 	return ssid / CTXDESC_L2_ENTRIES;
350 }
351 
352 static inline unsigned int arm_smmu_cdtab_l2_idx(unsigned int ssid)
353 {
354 	return ssid % CTXDESC_L2_ENTRIES;
355 }
356 
357 #define CTXDESC_CD_0_TCR_T0SZ		GENMASK_ULL(5, 0)
358 #define CTXDESC_CD_0_TCR_TG0		GENMASK_ULL(7, 6)
359 #define CTXDESC_CD_0_TCR_IRGN0		GENMASK_ULL(9, 8)
360 #define CTXDESC_CD_0_TCR_ORGN0		GENMASK_ULL(11, 10)
361 #define CTXDESC_CD_0_TCR_SH0		GENMASK_ULL(13, 12)
362 #define CTXDESC_CD_0_TCR_EPD0		(1ULL << 14)
363 #define CTXDESC_CD_0_TCR_EPD1		(1ULL << 30)
364 
365 #define CTXDESC_CD_0_ENDI		(1UL << 15)
366 #define CTXDESC_CD_0_V			(1UL << 31)
367 
368 #define CTXDESC_CD_0_TCR_IPS		GENMASK_ULL(34, 32)
369 #define CTXDESC_CD_0_TCR_TBI0		(1ULL << 38)
370 
371 #define CTXDESC_CD_0_TCR_HA            (1UL << 43)
372 #define CTXDESC_CD_0_TCR_HD            (1UL << 42)
373 
374 #define CTXDESC_CD_0_AA64		(1UL << 41)
375 #define CTXDESC_CD_0_S			(1UL << 44)
376 #define CTXDESC_CD_0_R			(1UL << 45)
377 #define CTXDESC_CD_0_A			(1UL << 46)
378 #define CTXDESC_CD_0_ASET		(1UL << 47)
379 #define CTXDESC_CD_0_ASID		GENMASK_ULL(63, 48)
380 
381 #define CTXDESC_CD_1_HAFT		(1UL << 3)
382 #define CTXDESC_CD_1_TTB0_MASK		GENMASK_ULL(51, 4)
383 
384 /*
385  * When the SMMU only supports linear context descriptor tables, pick a
386  * reasonable size limit (64kB).
387  */
388 #define CTXDESC_LINEAR_CDMAX		ilog2(SZ_64K / sizeof(struct arm_smmu_cd))
389 
390 /* Command queue */
391 #define CMDQ_ENT_SZ_SHIFT		4
392 #define CMDQ_ENT_DWORDS			((1 << CMDQ_ENT_SZ_SHIFT) >> 3)
393 #define CMDQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - CMDQ_ENT_SZ_SHIFT)
394 
395 #define CMDQ_CONS_ERR			GENMASK(30, 24)
396 #define CMDQ_ERR_CERROR_NONE_IDX	0
397 #define CMDQ_ERR_CERROR_ILL_IDX		1
398 #define CMDQ_ERR_CERROR_ABT_IDX		2
399 #define CMDQ_ERR_CERROR_ATC_INV_IDX	3
400 
401 #define CMDQ_PROD_OWNED_FLAG		Q_OVERFLOW_FLAG
402 
403 struct arm_smmu_cmd {
404 	u64 data[CMDQ_ENT_DWORDS];
405 };
406 
407 /*
408  * This is used to size the command queue and therefore must be at least
409  * BITS_PER_LONG so that the valid_map works correctly (it relies on the
410  * total number of queue entries being a multiple of BITS_PER_LONG).
411  */
412 #define CMDQ_BATCH_ENTRIES		BITS_PER_LONG
413 
414 #define CMDQ_0_OP			GENMASK_ULL(7, 0)
415 #define CMDQ_0_SSV			(1UL << 11)
416 
417 #define CMDQ_PREFETCH_0_SID		GENMASK_ULL(63, 32)
418 #define CMDQ_PREFETCH_1_SIZE		GENMASK_ULL(4, 0)
419 #define CMDQ_PREFETCH_1_ADDR_MASK	GENMASK_ULL(63, 12)
420 
421 #define CMDQ_CFGI_0_SSID		GENMASK_ULL(31, 12)
422 #define CMDQ_CFGI_0_SID			GENMASK_ULL(63, 32)
423 #define CMDQ_CFGI_1_LEAF		(1UL << 0)
424 #define CMDQ_CFGI_1_RANGE		GENMASK_ULL(4, 0)
425 
426 #define CMDQ_TLBI_0_NUM			GENMASK_ULL(16, 12)
427 #define CMDQ_TLBI_RANGE_NUM_MAX		31
428 #define CMDQ_TLBI_0_SCALE		GENMASK_ULL(25, 20)
429 #define CMDQ_TLBI_0_VMID		GENMASK_ULL(47, 32)
430 #define CMDQ_TLBI_0_ASID		GENMASK_ULL(63, 48)
431 #define CMDQ_TLBI_1_LEAF		(1UL << 0)
432 #define CMDQ_TLBI_1_TTL			GENMASK_ULL(9, 8)
433 #define CMDQ_TLBI_1_TG			GENMASK_ULL(11, 10)
434 #define CMDQ_TLBI_1_VA_MASK		GENMASK_ULL(63, 12)
435 #define CMDQ_TLBI_1_IPA_MASK		GENMASK_ULL(51, 12)
436 
437 #define CMDQ_ATC_0_SSID			GENMASK_ULL(31, 12)
438 #define CMDQ_ATC_0_SID			GENMASK_ULL(63, 32)
439 #define CMDQ_ATC_0_GLOBAL		(1UL << 9)
440 #define CMDQ_ATC_1_SIZE			GENMASK_ULL(5, 0)
441 #define CMDQ_ATC_1_ADDR_MASK		GENMASK_ULL(63, 12)
442 
443 #define ATC_INV_SIZE_ALL 52
444 
445 #define CMDQ_PRI_0_SSID			GENMASK_ULL(31, 12)
446 #define CMDQ_PRI_0_SID			GENMASK_ULL(63, 32)
447 #define CMDQ_PRI_1_GRPID		GENMASK_ULL(8, 0)
448 #define CMDQ_PRI_1_RESP			GENMASK_ULL(13, 12)
449 
450 enum pri_resp {
451 	PRI_RESP_DENY = 0,
452 	PRI_RESP_FAIL = 1,
453 	PRI_RESP_SUCC = 2,
454 };
455 
456 #define CMDQ_RESUME_0_RESP_TERM		0UL
457 #define CMDQ_RESUME_0_RESP_RETRY	1UL
458 #define CMDQ_RESUME_0_RESP_ABORT	2UL
459 #define CMDQ_RESUME_0_RESP		GENMASK_ULL(13, 12)
460 #define CMDQ_RESUME_0_SID		GENMASK_ULL(63, 32)
461 #define CMDQ_RESUME_1_STAG		GENMASK_ULL(15, 0)
462 
463 #define CMDQ_SYNC_0_CS			GENMASK_ULL(13, 12)
464 #define CMDQ_SYNC_0_CS_NONE		0
465 #define CMDQ_SYNC_0_CS_IRQ		1
466 #define CMDQ_SYNC_0_CS_SEV		2
467 #define CMDQ_SYNC_0_MSH			GENMASK_ULL(23, 22)
468 #define CMDQ_SYNC_0_MSIATTR		GENMASK_ULL(27, 24)
469 #define CMDQ_SYNC_0_MSIDATA		GENMASK_ULL(63, 32)
470 #define CMDQ_SYNC_1_MSIADDR_MASK	GENMASK_ULL(51, 2)
471 
472 enum arm_smmu_cmdq_opcode {
473 	CMDQ_OP_PREFETCH_CFG = 0x1,
474 	CMDQ_OP_CFGI_STE = 0x3,
475 	CMDQ_OP_CFGI_ALL = 0x4,
476 	CMDQ_OP_CFGI_CD = 0x5,
477 	CMDQ_OP_CFGI_CD_ALL = 0x6,
478 	CMDQ_OP_TLBI_NH_ALL = 0x10,
479 	CMDQ_OP_TLBI_NH_ASID = 0x11,
480 	CMDQ_OP_TLBI_NH_VA = 0x12,
481 	CMDQ_OP_TLBI_NH_VAA = 0x13,
482 	CMDQ_OP_TLBI_EL2_ALL = 0x20,
483 	CMDQ_OP_TLBI_EL2_ASID = 0x21,
484 	CMDQ_OP_TLBI_EL2_VA = 0x22,
485 	CMDQ_OP_TLBI_S12_VMALL = 0x28,
486 	CMDQ_OP_TLBI_S2_IPA = 0x2a,
487 	CMDQ_OP_TLBI_NSNH_ALL = 0x30,
488 	CMDQ_OP_ATC_INV = 0x40,
489 	CMDQ_OP_PRI_RESP = 0x41,
490 	CMDQ_OP_RESUME = 0x44,
491 	CMDQ_OP_CMD_SYNC = 0x46,
492 };
493 
494 static inline struct arm_smmu_cmd
495 arm_smmu_make_cmd_op(enum arm_smmu_cmdq_opcode op)
496 {
497 	struct arm_smmu_cmd cmd = {};
498 
499 	cmd.data[0] = FIELD_PREP(CMDQ_0_OP, op);
500 	return cmd;
501 }
502 
503 static inline struct arm_smmu_cmd arm_smmu_make_cmd_cfgi_all(void)
504 {
505 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_CFGI_ALL);
506 
507 	cmd.data[1] |= FIELD_PREP(CMDQ_CFGI_1_RANGE, 31);
508 	return cmd;
509 }
510 
511 static inline struct arm_smmu_cmd arm_smmu_make_cmd_prefetch_cfg(u32 sid)
512 {
513 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_PREFETCH_CFG);
514 
515 	cmd.data[0] |= FIELD_PREP(CMDQ_PREFETCH_0_SID, sid);
516 	return cmd;
517 }
518 
519 static inline struct arm_smmu_cmd arm_smmu_make_cmd_cfgi_ste(u32 sid, bool leaf)
520 {
521 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_CFGI_STE);
522 
523 	cmd.data[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, sid);
524 	cmd.data[1] |= FIELD_PREP(CMDQ_CFGI_1_LEAF, leaf);
525 	return cmd;
526 }
527 
528 static inline struct arm_smmu_cmd arm_smmu_make_cmd_cfgi_cd(u32 sid, u32 ssid,
529 							    bool leaf)
530 {
531 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_CFGI_CD);
532 
533 	cmd.data[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, sid) |
534 		       FIELD_PREP(CMDQ_CFGI_0_SSID, ssid);
535 	cmd.data[1] |= FIELD_PREP(CMDQ_CFGI_1_LEAF, leaf);
536 	return cmd;
537 }
538 
539 static inline struct arm_smmu_cmd arm_smmu_make_cmd_resume(u32 sid, u16 stag,
540 							   u8 resp)
541 {
542 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_RESUME);
543 
544 	cmd.data[0] |= FIELD_PREP(CMDQ_RESUME_0_SID, sid) |
545 		       FIELD_PREP(CMDQ_RESUME_0_RESP, resp);
546 	cmd.data[1] |= FIELD_PREP(CMDQ_RESUME_1_STAG, stag);
547 	return cmd;
548 }
549 
550 static inline struct arm_smmu_cmd arm_smmu_make_cmd_pri_resp(u32 sid, u32 ssid,
551 							     bool ssv,
552 							     u16 grpid,
553 							     enum pri_resp resp)
554 {
555 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_PRI_RESP);
556 
557 	cmd.data[0] |= FIELD_PREP(CMDQ_0_SSV, ssv) |
558 		       FIELD_PREP(CMDQ_PRI_0_SID, sid) |
559 		       FIELD_PREP(CMDQ_PRI_0_SSID, ssid);
560 	cmd.data[1] |= FIELD_PREP(CMDQ_PRI_1_GRPID, grpid) |
561 		       FIELD_PREP(CMDQ_PRI_1_RESP, resp);
562 	return cmd;
563 }
564 
565 static inline struct arm_smmu_cmd arm_smmu_make_cmd_atc_inv(u32 sid, u32 ssid,
566 							    u64 addr, u8 size)
567 {
568 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_ATC_INV);
569 
570 	cmd.data[0] |= FIELD_PREP(CMDQ_0_SSV, ssid != IOMMU_NO_PASID) |
571 		       FIELD_PREP(CMDQ_ATC_0_SSID, ssid) |
572 		       FIELD_PREP(CMDQ_ATC_0_SID, sid);
573 	cmd.data[1] |= FIELD_PREP(CMDQ_ATC_1_SIZE, size) |
574 		       (addr & CMDQ_ATC_1_ADDR_MASK);
575 	return cmd;
576 }
577 
578 static inline struct arm_smmu_cmd arm_smmu_make_cmd_atc_inv_all(u32 sid,
579 								u32 ssid)
580 {
581 	return arm_smmu_make_cmd_atc_inv(sid, ssid, 0, ATC_INV_SIZE_ALL);
582 }
583 
584 static inline struct arm_smmu_cmd arm_smmu_make_cmd_sync(unsigned int cs,
585 							 u64 msiaddr)
586 {
587 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(CMDQ_OP_CMD_SYNC);
588 
589 	cmd.data[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, cs) |
590 		       FIELD_PREP(CMDQ_SYNC_0_MSH, ARM_SMMU_SH_ISH) |
591 		       FIELD_PREP(CMDQ_SYNC_0_MSIATTR, ARM_SMMU_MEMATTR_OIWB);
592 	cmd.data[1] |= msiaddr & CMDQ_SYNC_1_MSIADDR_MASK;
593 	return cmd;
594 }
595 
596 /*
597  * TLBI commands - the non-sized variants just need opcode + asid/vmid.
598  * For sized variants the caller sets up data[0] with the immutable fields
599  * (opcode + asid/vmid) and the range loop fills in per-iteration fields.
600  */
601 static inline struct arm_smmu_cmd
602 arm_smmu_make_cmd_tlbi(enum arm_smmu_cmdq_opcode op, u16 asid, u16 vmid)
603 {
604 	struct arm_smmu_cmd cmd = arm_smmu_make_cmd_op(op);
605 
606 	cmd.data[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, asid) |
607 		       FIELD_PREP(CMDQ_TLBI_0_VMID, vmid);
608 	return cmd;
609 }
610 
611 /* Event queue */
612 #define EVTQ_ENT_SZ_SHIFT		5
613 #define EVTQ_ENT_DWORDS			((1 << EVTQ_ENT_SZ_SHIFT) >> 3)
614 #define EVTQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - EVTQ_ENT_SZ_SHIFT)
615 
616 #define EVTQ_0_ID			GENMASK_ULL(7, 0)
617 
618 #define EVT_ID_BAD_STREAMID_CONFIG	0x02
619 #define EVT_ID_STE_FETCH_FAULT		0x03
620 #define EVT_ID_BAD_STE_CONFIG		0x04
621 #define EVT_ID_STREAM_DISABLED_FAULT	0x06
622 #define EVT_ID_BAD_SUBSTREAMID_CONFIG	0x08
623 #define EVT_ID_CD_FETCH_FAULT		0x09
624 #define EVT_ID_BAD_CD_CONFIG		0x0a
625 #define EVT_ID_TRANSLATION_FAULT	0x10
626 #define EVT_ID_ADDR_SIZE_FAULT		0x11
627 #define EVT_ID_ACCESS_FAULT		0x12
628 #define EVT_ID_PERMISSION_FAULT		0x13
629 #define EVT_ID_VMS_FETCH_FAULT		0x25
630 
631 #define EVTQ_0_SSV			(1UL << 11)
632 #define EVTQ_0_SSID			GENMASK_ULL(31, 12)
633 #define EVTQ_0_SID			GENMASK_ULL(63, 32)
634 #define EVTQ_1_STAG			GENMASK_ULL(15, 0)
635 #define EVTQ_1_STALL			(1UL << 31)
636 #define EVTQ_1_PnU			(1UL << 33)
637 #define EVTQ_1_InD			(1UL << 34)
638 #define EVTQ_1_RnW			(1UL << 35)
639 #define EVTQ_1_S2			(1UL << 39)
640 #define EVTQ_1_CLASS			GENMASK_ULL(41, 40)
641 #define EVTQ_1_CLASS_TT			0x01
642 #define EVTQ_1_TT_READ			(1UL << 44)
643 #define EVTQ_2_ADDR			GENMASK_ULL(63, 0)
644 #define EVTQ_3_IPA			GENMASK_ULL(51, 12)
645 #define EVTQ_3_FETCH_ADDR		GENMASK_ULL(51, 3)
646 
647 /* PRI queue */
648 #define PRIQ_ENT_SZ_SHIFT		4
649 #define PRIQ_ENT_DWORDS			((1 << PRIQ_ENT_SZ_SHIFT) >> 3)
650 #define PRIQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - PRIQ_ENT_SZ_SHIFT)
651 
652 #define PRIQ_0_SID			GENMASK_ULL(31, 0)
653 #define PRIQ_0_SSID			GENMASK_ULL(51, 32)
654 #define PRIQ_0_PERM_PRIV		(1UL << 58)
655 #define PRIQ_0_PERM_EXEC		(1UL << 59)
656 #define PRIQ_0_PERM_READ		(1UL << 60)
657 #define PRIQ_0_PERM_WRITE		(1UL << 61)
658 #define PRIQ_0_PRG_LAST			(1UL << 62)
659 #define PRIQ_0_SSID_V			(1UL << 63)
660 
661 #define PRIQ_1_PRG_IDX			GENMASK_ULL(8, 0)
662 #define PRIQ_1_ADDR_MASK		GENMASK_ULL(63, 12)
663 
664 /* High-level queue structures */
665 #define ARM_SMMU_POLL_TIMEOUT_US	1000000 /* 1s! */
666 #define ARM_SMMU_POLL_SPIN_COUNT	10
667 
668 #define MSI_IOVA_BASE			0x8000000
669 #define MSI_IOVA_LENGTH			0x100000
670 
671 struct arm_smmu_ll_queue {
672 	union {
673 		u64			val;
674 		struct {
675 			u32		prod;
676 			u32		cons;
677 		};
678 		struct {
679 			atomic_t	prod;
680 			atomic_t	cons;
681 		} atomic;
682 		u8			__pad[SMP_CACHE_BYTES];
683 	} ____cacheline_aligned_in_smp;
684 	u32				max_n_shift;
685 };
686 
687 struct arm_smmu_queue {
688 	struct arm_smmu_ll_queue	llq;
689 	int				irq; /* Wired interrupt */
690 
691 	__le64				*base;
692 	dma_addr_t			base_dma;
693 	u64				q_base;
694 
695 	size_t				ent_dwords;
696 
697 	u32 __iomem			*prod_reg;
698 	u32 __iomem			*cons_reg;
699 };
700 
701 struct arm_smmu_queue_poll {
702 	ktime_t				timeout;
703 	unsigned int			delay;
704 	unsigned int			spin_cnt;
705 	bool				wfe;
706 };
707 
708 struct arm_smmu_cmdq {
709 	struct arm_smmu_queue		q;
710 	atomic_long_t			*valid_map;
711 	atomic_t			owner_prod;
712 	atomic_t			lock;
713 	bool				(*supports_cmd)(struct arm_smmu_cmd *cmd);
714 };
715 
716 static inline bool arm_smmu_cmdq_supports_cmd(struct arm_smmu_cmdq *cmdq,
717 					      struct arm_smmu_cmd *cmd)
718 {
719 	return cmdq->supports_cmd ? cmdq->supports_cmd(cmd) : true;
720 }
721 
722 struct arm_smmu_cmdq_batch {
723 	struct arm_smmu_cmd		cmds[CMDQ_BATCH_ENTRIES];
724 	struct arm_smmu_cmdq		*cmdq;
725 	int				num;
726 };
727 
728 /*
729  * The order here also determines the sequence in which commands are sent to the
730  * command queue. E.g. TLBI must be done before ATC_INV.
731  */
732 enum arm_smmu_inv_type {
733 	INV_TYPE_S1_ASID,
734 	INV_TYPE_S2_VMID,
735 	INV_TYPE_S2_VMID_S1_CLEAR,
736 	INV_TYPE_ATS,
737 	INV_TYPE_ATS_FULL,
738 };
739 
740 struct arm_smmu_inv {
741 	struct arm_smmu_device *smmu;
742 	u8 type;
743 	u8 size_opcode;
744 	u8 nsize_opcode;
745 	u32 id; /* ASID or VMID or SID */
746 
747 	/* Only used by INV_TYPE_ATS */
748 	u32 ssid;
749 
750 	int users; /* users=0 to mark as a trash to be purged */
751 };
752 
753 static inline bool arm_smmu_inv_is_ats(const struct arm_smmu_inv *inv)
754 {
755 	return inv->type == INV_TYPE_ATS || inv->type == INV_TYPE_ATS_FULL;
756 }
757 
758 /**
759  * struct arm_smmu_invs - Per-domain invalidation array
760  * @max_invs: maximum capacity of the flexible array
761  * @num_invs: number of invalidations in the flexible array. May be smaller than
762  *            @max_invs after a tailing trash entry is excluded, but must not be
763  *            greater than @max_invs
764  * @num_trashes: number of trash entries in the array for arm_smmu_invs_purge().
765  *               Must not be greater than @num_invs
766  * @rwlock: optional rwlock to fence ATS operations
767  * @has_ats: flag if the array contains an INV_TYPE_ATS or INV_TYPE_ATS_FULL
768  * @has_range_inv: flag if any entry's SMMU supports range invalidation
769  * @has_full_cont_range_inv: flag if any entry's SMMU requires the CONT range
770  *                           invalidation workaround
771  * @rcu: rcu head for kfree_rcu()
772  * @inv: flexible invalidation array
773  *
774  * The arm_smmu_invs is an RCU data structure. During a ->attach_dev callback,
775  * arm_smmu_invs_merge(), arm_smmu_invs_unref() and arm_smmu_invs_purge() will
776  * be used to allocate a new copy of an old array for addition and deletion in
777  * the old domain's and new domain's invs arrays.
778  *
779  * The arm_smmu_invs_unref() mutates a given array, by internally reducing the
780  * users counts of some given entries. This exists to support a no-fail routine
781  * like attaching to an IOMMU_DOMAIN_BLOCKED. And it could pair with a followup
782  * arm_smmu_invs_purge() call to generate a new clean array.
783  *
784  * Concurrent invalidation thread will push every invalidation described in the
785  * array into the command queue for each invalidation event. It is designed like
786  * this to optimize the invalidation fast path by avoiding locks.
787  *
788  * A domain can be shared across SMMU instances. When an instance gets removed,
789  * it would delete all the entries that belong to that SMMU instance. Then, a
790  * synchronize_rcu() would have to be called to sync the array, to prevent any
791  * concurrent invalidation thread accessing the old array from issuing commands
792  * to the command queue of a removed SMMU instance.
793  */
794 struct arm_smmu_invs {
795 	size_t max_invs;
796 	size_t num_invs;
797 	size_t num_trashes;
798 	rwlock_t rwlock;
799 	bool has_ats;
800 	bool has_range_inv;
801 	bool has_full_cont_range_inv;
802 	struct rcu_head rcu;
803 	struct arm_smmu_inv inv[] __counted_by(max_invs);
804 };
805 
806 static inline struct arm_smmu_invs *arm_smmu_invs_alloc(size_t num_invs)
807 {
808 	struct arm_smmu_invs *new_invs;
809 
810 	new_invs = kzalloc_flex(*new_invs, inv, num_invs);
811 	if (!new_invs)
812 		return NULL;
813 	new_invs->max_invs = num_invs;
814 	new_invs->num_invs = num_invs;
815 	rwlock_init(&new_invs->rwlock);
816 	return new_invs;
817 }
818 
819 /* Generic page-table level 0 is the leaf-only level. */
820 static inline unsigned int arm_smmu_pt_level_to_lg2sz(unsigned int tgsz_lg2,
821 						      unsigned int level)
822 {
823 	return tgsz_lg2 + (tgsz_lg2 - ilog2(sizeof(u64))) * level;
824 }
825 
826 static inline unsigned int arm_smmu_pt_lg2sz_to_level(unsigned int tgsz_lg2,
827 						      unsigned int lg2sz)
828 {
829 	return (lg2sz - tgsz_lg2) / (tgsz_lg2 - ilog2(sizeof(u64)));
830 }
831 
832 struct arm_smmu_tlbi {
833 	unsigned long iova;
834 	size_t size;
835 	/* page or block size of the leaf iopte */
836 	unsigned int iopte_size;
837 	/* Base Translation Granule of the page table */
838 	u8 tgsz_lg2;
839 	bool leaf_only;
840 
841 	struct {
842 		bool use_full_inv;
843 		u16 num;
844 	} single;
845 
846 	struct {
847 		bool use_full_inv;
848 		u8 num_cmds;
849 		struct arm_smmu_cmd cmds[2];
850 	} range;
851 };
852 
853 struct arm_smmu_evtq {
854 	struct arm_smmu_queue		q;
855 	struct iopf_queue		*iopf;
856 	u32				max_stalls;
857 };
858 
859 struct arm_smmu_priq {
860 	struct arm_smmu_queue		q;
861 };
862 
863 /* High-level stream table and context descriptor structures */
864 struct arm_smmu_ctx_desc {
865 	u16				asid;
866 };
867 
868 struct arm_smmu_ctx_desc_cfg {
869 	union {
870 		struct {
871 			struct arm_smmu_cd *table;
872 			unsigned int num_ents;
873 		} linear;
874 		struct {
875 			struct arm_smmu_cdtab_l1 *l1tab;
876 			struct arm_smmu_cdtab_l2 **l2ptrs;
877 			unsigned int num_l1_ents;
878 		} l2;
879 	};
880 	dma_addr_t			cdtab_dma;
881 	unsigned int			used_ssids;
882 	u8				in_ste;
883 	u8				s1fmt;
884 	/* log2 of the maximum number of CDs supported by this table */
885 	u8				s1cdmax;
886 };
887 
888 static inline bool
889 arm_smmu_cdtab_allocated(struct arm_smmu_ctx_desc_cfg *cfg)
890 {
891 	return cfg->linear.table || cfg->l2.l1tab;
892 }
893 
894 /* True if the cd table has SSIDS > 0 in use. */
895 static inline bool arm_smmu_ssids_in_use(struct arm_smmu_ctx_desc_cfg *cd_table)
896 {
897 	return cd_table->used_ssids;
898 }
899 
900 struct arm_smmu_s2_cfg {
901 	u16				vmid;
902 };
903 
904 struct arm_smmu_strtab_cfg {
905 	union {
906 		struct {
907 			struct arm_smmu_ste *table;
908 			dma_addr_t ste_dma;
909 			unsigned int num_ents;
910 		} linear;
911 		struct {
912 			struct arm_smmu_strtab_l1 *l1tab;
913 			struct arm_smmu_strtab_l2 **l2ptrs;
914 			dma_addr_t l1_dma;
915 			unsigned int num_l1_ents;
916 		} l2;
917 	};
918 };
919 
920 struct arm_smmu_impl_ops {
921 	int (*device_reset)(struct arm_smmu_device *smmu);
922 	void (*device_disable)(struct arm_smmu_device *smmu);
923 	void (*device_remove)(struct arm_smmu_device *smmu);
924 	int (*init_structures)(struct arm_smmu_device *smmu);
925 	struct arm_smmu_cmdq *(*get_secondary_cmdq)(
926 		struct arm_smmu_device *smmu, struct arm_smmu_cmd *cmd);
927 	/*
928 	 * An implementation should define its own type other than the default
929 	 * IOMMU_HW_INFO_TYPE_ARM_SMMUV3. And it must validate the input @type
930 	 * to return its own structure.
931 	 */
932 	void *(*hw_info)(struct arm_smmu_device *smmu, u32 *length,
933 			 enum iommu_hw_info_type *type);
934 	size_t (*get_viommu_size)(enum iommu_viommu_type viommu_type);
935 	int (*vsmmu_init)(struct arm_vsmmu *vsmmu,
936 			  const struct iommu_user_data *user_data);
937 };
938 
939 /* An SMMUv3 instance */
940 struct arm_smmu_device {
941 	struct device			*dev;
942 	struct device			*impl_dev;
943 	const struct arm_smmu_impl_ops	*impl_ops;
944 
945 	void __iomem			*base;
946 	void __iomem			*page1;
947 
948 #define ARM_SMMU_FEAT_2_LVL_STRTAB	(1 << 0)
949 #define ARM_SMMU_FEAT_2_LVL_CDTAB	(1 << 1)
950 #define ARM_SMMU_FEAT_TT_LE		(1 << 2)
951 #define ARM_SMMU_FEAT_TT_BE		(1 << 3)
952 #define ARM_SMMU_FEAT_PRI		(1 << 4)
953 #define ARM_SMMU_FEAT_ATS		(1 << 5)
954 #define ARM_SMMU_FEAT_SEV		(1 << 6)
955 #define ARM_SMMU_FEAT_MSI		(1 << 7)
956 #define ARM_SMMU_FEAT_COHERENCY		(1 << 8)
957 #define ARM_SMMU_FEAT_TRANS_S1		(1 << 9)
958 #define ARM_SMMU_FEAT_TRANS_S2		(1 << 10)
959 #define ARM_SMMU_FEAT_STALLS		(1 << 11)
960 #define ARM_SMMU_FEAT_HYP		(1 << 12)
961 #define ARM_SMMU_FEAT_STALL_FORCE	(1 << 13)
962 #define ARM_SMMU_FEAT_VAX		(1 << 14)
963 #define ARM_SMMU_FEAT_RANGE_INV		(1 << 15)
964 #define ARM_SMMU_FEAT_BTM		(1 << 16)
965 #define ARM_SMMU_FEAT_SVA		(1 << 17)
966 #define ARM_SMMU_FEAT_E2H		(1 << 18)
967 #define ARM_SMMU_FEAT_NESTING		(1 << 19)
968 #define ARM_SMMU_FEAT_ATTR_TYPES_OVR	(1 << 20)
969 #define ARM_SMMU_FEAT_HA		(1 << 21)
970 #define ARM_SMMU_FEAT_HD		(1 << 22)
971 #define ARM_SMMU_FEAT_S2FWB		(1 << 23)
972 #define ARM_SMMU_FEAT_BBML2		(1 << 24)
973 #define ARM_SMMU_FEAT_HAFT		(1 << 25)
974 #define ARM_SMMU_FEAT_DS		(1 << 26)
975 	u32				features;
976 
977 #define ARM_SMMU_OPT_SKIP_PREFETCH	(1 << 0)
978 #define ARM_SMMU_OPT_PAGE0_REGS_ONLY	(1 << 1)
979 #define ARM_SMMU_OPT_MSIPOLL		(1 << 2)
980 #define ARM_SMMU_OPT_CMDQ_FORCE_SYNC	(1 << 3)
981 #define ARM_SMMU_OPT_TEGRA241_CMDQV	(1 << 4)
982 #define ARM_SMMU_OPT_OVR_INSTCFG_DATA	(1 << 5)
983 /*
984  * Range invalidation is mandatory and one range invalidation must fully span an
985  * invalidated CONT
986  */
987 #define ARM_SMMU_OPT_FULL_CONT_RANGE_INV (1 << 6)
988 	u32				options;
989 
990 	struct arm_smmu_cmdq		cmdq;
991 	struct arm_smmu_evtq		evtq;
992 	struct arm_smmu_priq		priq;
993 
994 	int				gerr_irq;
995 	int				combined_irq;
996 
997 	unsigned long			oas; /* PA */
998 	unsigned long			pgsize_bitmap;
999 
1000 #define ARM_SMMU_MAX_ASIDS		(1 << 16)
1001 	unsigned int			asid_bits;
1002 
1003 #define ARM_SMMU_MAX_VMIDS		(1 << 16)
1004 	unsigned int			vmid_bits;
1005 	struct ida			vmid_map;
1006 
1007 	unsigned int			ssid_bits;
1008 	unsigned int			sid_bits;
1009 
1010 	struct arm_smmu_strtab_cfg	strtab_cfg;
1011 
1012 	/* IOMMU core code handle */
1013 	struct iommu_device		iommu;
1014 
1015 	struct rb_root			streams;
1016 	struct mutex			streams_mutex;
1017 };
1018 
1019 struct arm_smmu_stream {
1020 	u32				id;
1021 	struct arm_smmu_master		*master;
1022 	struct rb_node			node;
1023 };
1024 
1025 struct arm_smmu_vmaster {
1026 	struct arm_vsmmu		*vsmmu;
1027 	unsigned long			vsid;
1028 };
1029 
1030 struct arm_smmu_event {
1031 	u8				stall : 1,
1032 					ssv : 1,
1033 					privileged : 1,
1034 					instruction : 1,
1035 					s2 : 1,
1036 					read : 1,
1037 					ttrnw : 1,
1038 					class_tt : 1;
1039 	u8				id;
1040 	u8				class;
1041 	u16				stag;
1042 	u32				sid;
1043 	u32				ssid;
1044 	u64				iova;
1045 	u64				ipa;
1046 	u64				fetch_addr;
1047 	struct device			*dev;
1048 };
1049 
1050 /* SMMU private data for each master */
1051 struct arm_smmu_master {
1052 	struct arm_smmu_device		*smmu;
1053 	struct device			*dev;
1054 	struct arm_smmu_stream		*streams;
1055 	/*
1056 	 * Scratch memory for a to_merge or to_unref array to build a per-domain
1057 	 * invalidation array. It'll be pre-allocated with enough enries for all
1058 	 * possible build scenarios. It can be used by only one caller at a time
1059 	 * until the arm_smmu_invs_merge/unref() finishes. Must be locked by the
1060 	 * iommu_group mutex.
1061 	 */
1062 	struct arm_smmu_invs		*build_invs;
1063 	struct arm_smmu_vmaster		*vmaster; /* use smmu->streams_mutex */
1064 	/* Locked by the iommu core using the group mutex */
1065 	struct arm_smmu_ctx_desc_cfg	cd_table;
1066 	unsigned int			num_streams;
1067 	bool				ats_enabled : 1;
1068 	bool				ste_ats_enabled : 1;
1069 	bool				stall_enabled;
1070 	bool				ats_always_on;
1071 	unsigned int			ssid_bits;
1072 	unsigned int			iopf_refcount;
1073 };
1074 
1075 /* SMMU private data for an IOMMU domain */
1076 enum arm_smmu_domain_stage {
1077 	ARM_SMMU_DOMAIN_S1 = 0,
1078 	ARM_SMMU_DOMAIN_S2,
1079 	ARM_SMMU_DOMAIN_SVA,
1080 };
1081 
1082 struct arm_smmu_domain {
1083 	struct arm_smmu_device		*smmu;
1084 
1085 	struct io_pgtable_ops		*pgtbl_ops;
1086 	atomic_t			nr_ats_masters;
1087 
1088 	enum arm_smmu_domain_stage	stage;
1089 	union {
1090 		struct arm_smmu_ctx_desc	cd;
1091 		struct arm_smmu_s2_cfg		s2_cfg;
1092 	};
1093 
1094 	struct iommu_domain		domain;
1095 
1096 	struct arm_smmu_invs __rcu	*invs;
1097 
1098 	/* List of struct arm_smmu_master_domain */
1099 	struct list_head		devices;
1100 	spinlock_t			devices_lock;
1101 	bool				enforce_cache_coherency : 1;
1102 	bool				nest_parent : 1;
1103 	u8				tgsz_lg2;
1104 
1105 	struct mmu_notifier		mmu_notifier;
1106 };
1107 
1108 struct arm_smmu_nested_domain {
1109 	struct iommu_domain domain;
1110 	struct arm_vsmmu *vsmmu;
1111 	bool enable_ats : 1;
1112 
1113 	__le64 ste[2];
1114 };
1115 
1116 /* The following are exposed for testing purposes. */
1117 struct arm_smmu_entry_writer_ops;
1118 struct arm_smmu_entry_writer {
1119 	const struct arm_smmu_entry_writer_ops *ops;
1120 	struct arm_smmu_master *master;
1121 };
1122 
1123 struct arm_smmu_entry_writer_ops {
1124 	void (*get_used)(const __le64 *entry, __le64 *used);
1125 	void (*get_update_safe)(const __le64 *cur, const __le64 *target,
1126 				__le64 *safe_bits);
1127 	void (*sync)(struct arm_smmu_entry_writer *writer);
1128 };
1129 
1130 void arm_smmu_make_abort_ste(struct arm_smmu_ste *target);
1131 void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target,
1132 				 struct arm_smmu_master *master,
1133 				 struct arm_smmu_domain *smmu_domain,
1134 				 bool ats_enabled);
1135 
1136 #if IS_ENABLED(CONFIG_KUNIT)
1137 void arm_smmu_get_ste_used(const __le64 *ent, __le64 *used_bits);
1138 void arm_smmu_get_ste_update_safe(const __le64 *cur, const __le64 *target,
1139 				  __le64 *safe_bits);
1140 void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, __le64 *cur,
1141 			  const __le64 *target);
1142 void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits);
1143 void arm_smmu_make_bypass_ste(struct arm_smmu_device *smmu,
1144 			      struct arm_smmu_ste *target);
1145 void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target,
1146 			       struct arm_smmu_master *master, bool ats_enabled,
1147 			       unsigned int s1dss);
1148 void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
1149 			  struct arm_smmu_master *master, struct mm_struct *mm,
1150 			  u16 asid);
1151 
1152 struct arm_smmu_invs *arm_smmu_invs_merge(struct arm_smmu_invs *invs,
1153 					  struct arm_smmu_invs *to_merge);
1154 void arm_smmu_invs_unref(struct arm_smmu_invs *invs,
1155 			 struct arm_smmu_invs *to_unref);
1156 struct arm_smmu_invs *arm_smmu_invs_purge(struct arm_smmu_invs *invs);
1157 #endif
1158 
1159 struct arm_smmu_master_domain {
1160 	struct list_head devices_elm;
1161 	struct arm_smmu_master *master;
1162 	/*
1163 	 * For nested domains the master_domain is threaded onto the S2 parent,
1164 	 * this points to the IOMMU_DOMAIN_NESTED to disambiguate the masters.
1165 	 */
1166 	struct iommu_domain *domain;
1167 	ioasid_t ssid;
1168 	bool nested_ats_flush : 1;
1169 	bool using_iopf : 1;
1170 };
1171 
1172 static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
1173 {
1174 	return container_of(dom, struct arm_smmu_domain, domain);
1175 }
1176 
1177 static inline struct arm_smmu_nested_domain *
1178 to_smmu_nested_domain(struct iommu_domain *dom)
1179 {
1180 	return container_of(dom, struct arm_smmu_nested_domain, domain);
1181 }
1182 
1183 extern struct xarray arm_smmu_asid_xa;
1184 extern struct mutex arm_smmu_asid_lock;
1185 
1186 struct arm_smmu_domain *arm_smmu_domain_alloc(void);
1187 
1188 static inline void arm_smmu_domain_free(struct arm_smmu_domain *smmu_domain)
1189 {
1190 	/* No concurrency with invalidation is possible at this point */
1191 	kfree(rcu_dereference_protected(smmu_domain->invs, true));
1192 	kfree(smmu_domain);
1193 }
1194 
1195 void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid);
1196 struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master,
1197 					u32 ssid);
1198 void arm_smmu_make_s1_cd(struct arm_smmu_cd *target,
1199 			 struct arm_smmu_master *master,
1200 			 struct arm_smmu_domain *smmu_domain);
1201 void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid,
1202 			     struct arm_smmu_cd *cdptr,
1203 			     const struct arm_smmu_cd *target);
1204 
1205 int arm_smmu_set_pasid(struct arm_smmu_master *master,
1206 		       struct arm_smmu_domain *smmu_domain, ioasid_t pasid,
1207 		       struct arm_smmu_cd *cd, struct iommu_domain *old);
1208 
1209 void arm_smmu_domain_tlbi(struct arm_smmu_tlbi *tlbi,
1210 			  struct arm_smmu_domain *smmu_domain);
1211 
1212 static inline void arm_smmu_domain_inv(struct arm_smmu_domain *smmu_domain)
1213 {
1214 	/* Prefilled for invalidate all */
1215 	struct arm_smmu_tlbi tlbi = {
1216 		.tgsz_lg2 = smmu_domain->tgsz_lg2,
1217 		.single.use_full_inv = true,
1218 		.range.use_full_inv = true,
1219 	};
1220 
1221 	arm_smmu_domain_tlbi(&tlbi, smmu_domain);
1222 }
1223 
1224 void __arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu,
1225 			      struct arm_smmu_cmdq *cmdq);
1226 u32 arm_smmu_cmdq_max_n_shift(u32 hw_max_n_shift);
1227 int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
1228 			    struct arm_smmu_queue *q, void __iomem *page,
1229 			    unsigned long prod_off, unsigned long cons_off,
1230 			    size_t dwords, const char *name);
1231 int arm_smmu_cmdq_init(struct arm_smmu_device *smmu,
1232 		       struct arm_smmu_cmdq *cmdq);
1233 
1234 static inline bool arm_smmu_master_canwbs(struct arm_smmu_master *master)
1235 {
1236 	return dev_iommu_fwspec_get(master->dev)->flags &
1237 	       IOMMU_FWSPEC_PCI_RC_CANWBS;
1238 }
1239 
1240 /**
1241  * struct arm_smmu_inv_state - Per-domain invalidation array state
1242  * @invs_ptr: points to the domain->invs (unwinding nesting/etc.) or is NULL if
1243  *            no change should be made
1244  * @old_invs: the original invs array
1245  * @new_invs: for new domain, this is the new invs array to update domain->invs;
1246  *            for old domain, this is the master->build_invs to pass in as the
1247  *            to_unref argument to an arm_smmu_invs_unref() call
1248  */
1249 struct arm_smmu_inv_state {
1250 	struct arm_smmu_invs __rcu **invs_ptr;
1251 	struct arm_smmu_invs *old_invs;
1252 	struct arm_smmu_invs *new_invs;
1253 };
1254 
1255 struct arm_smmu_attach_state {
1256 	/* Inputs */
1257 	struct iommu_domain *old_domain;
1258 	struct arm_smmu_master *master;
1259 	bool cd_needs_ats;
1260 	bool disable_ats;
1261 	ioasid_t ssid;
1262 	/* Resulting state */
1263 	struct arm_smmu_vmaster *vmaster;
1264 	struct arm_smmu_inv_state old_domain_invst;
1265 	struct arm_smmu_inv_state new_domain_invst;
1266 	struct arm_smmu_master_domain *old_master_domain;
1267 	bool ats_enabled;
1268 };
1269 
1270 int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state,
1271 			    struct iommu_domain *new_domain);
1272 void arm_smmu_attach_commit(struct arm_smmu_attach_state *state);
1273 void arm_smmu_attach_release(struct arm_smmu_attach_state *state);
1274 void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master,
1275 				  const struct arm_smmu_ste *target);
1276 
1277 int __arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
1278 				  struct arm_smmu_cmdq *cmdq,
1279 				  struct arm_smmu_cmd *cmds, int n,
1280 				  bool sync);
1281 int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
1282 				struct arm_smmu_cmdq *cmdq,
1283 				struct arm_smmu_cmd *cmds, int n,
1284 				bool sync);
1285 bool arm_smmu_erratum_repeat_tlbi_cfgi(void);
1286 
1287 #ifdef CONFIG_ARM_SMMU_V3_SVA
1288 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu);
1289 void arm_smmu_sva_notifier_synchronize(void);
1290 struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev,
1291 					       struct mm_struct *mm);
1292 #else /* CONFIG_ARM_SMMU_V3_SVA */
1293 static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
1294 {
1295 	return false;
1296 }
1297 
1298 static inline void arm_smmu_sva_notifier_synchronize(void) {}
1299 
1300 #define arm_smmu_sva_domain_alloc NULL
1301 
1302 #endif /* CONFIG_ARM_SMMU_V3_SVA */
1303 
1304 #ifdef CONFIG_TEGRA241_CMDQV
1305 struct arm_smmu_device *tegra241_cmdqv_probe(struct arm_smmu_device *smmu);
1306 #else /* CONFIG_TEGRA241_CMDQV */
1307 static inline struct arm_smmu_device *
1308 tegra241_cmdqv_probe(struct arm_smmu_device *smmu)
1309 {
1310 	return ERR_PTR(-ENODEV);
1311 }
1312 #endif /* CONFIG_TEGRA241_CMDQV */
1313 
1314 struct arm_vsmmu {
1315 	struct iommufd_viommu core;
1316 	struct arm_smmu_device *smmu;
1317 	struct arm_smmu_domain *s2_parent;
1318 	u16 vmid;
1319 };
1320 
1321 #if IS_ENABLED(CONFIG_ARM_SMMU_V3_IOMMUFD)
1322 void *arm_smmu_hw_info(struct device *dev, u32 *length,
1323 		       enum iommu_hw_info_type *type);
1324 size_t arm_smmu_get_viommu_size(struct device *dev,
1325 				enum iommu_viommu_type viommu_type);
1326 int arm_vsmmu_init(struct iommufd_viommu *viommu,
1327 		   struct iommu_domain *parent_domain,
1328 		   const struct iommu_user_data *user_data);
1329 int arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state *state,
1330 				    struct arm_smmu_nested_domain *nested_domain);
1331 void arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state *state);
1332 void arm_smmu_master_clear_vmaster(struct arm_smmu_master *master);
1333 int arm_vmaster_report_event(struct arm_smmu_vmaster *vmaster, u64 *evt);
1334 struct iommu_domain *
1335 arm_vsmmu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
1336 			      const struct iommu_user_data *user_data);
1337 int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,
1338 			       struct iommu_user_data_array *array);
1339 #else
1340 #define arm_smmu_get_viommu_size NULL
1341 #define arm_smmu_hw_info NULL
1342 #define arm_vsmmu_init NULL
1343 #define arm_vsmmu_alloc_domain_nested NULL
1344 #define arm_vsmmu_cache_invalidate NULL
1345 
1346 static inline int
1347 arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state *state,
1348 				struct arm_smmu_nested_domain *nested_domain)
1349 {
1350 	return 0;
1351 }
1352 
1353 static inline void
1354 arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state *state)
1355 {
1356 }
1357 
1358 static inline void
1359 arm_smmu_master_clear_vmaster(struct arm_smmu_master *master)
1360 {
1361 }
1362 
1363 static inline int arm_vmaster_report_event(struct arm_smmu_vmaster *vmaster,
1364 					   u64 *evt)
1365 {
1366 	return -EOPNOTSUPP;
1367 }
1368 #endif /* CONFIG_ARM_SMMU_V3_IOMMUFD */
1369 
1370 #endif /* _ARM_SMMU_V3_H */
1371