xref: /linux/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h (revision a1c3be890440a1769ed6f822376a3e3ab0d42994)
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/kernel.h>
14 #include <linux/mmzone.h>
15 #include <linux/sizes.h>
16 
17 /* MMIO registers */
18 #define ARM_SMMU_IDR0			0x0
19 #define IDR0_ST_LVL			GENMASK(28, 27)
20 #define IDR0_ST_LVL_2LVL		1
21 #define IDR0_STALL_MODEL		GENMASK(25, 24)
22 #define IDR0_STALL_MODEL_STALL		0
23 #define IDR0_STALL_MODEL_FORCE		2
24 #define IDR0_TTENDIAN			GENMASK(22, 21)
25 #define IDR0_TTENDIAN_MIXED		0
26 #define IDR0_TTENDIAN_LE		2
27 #define IDR0_TTENDIAN_BE		3
28 #define IDR0_CD2L			(1 << 19)
29 #define IDR0_VMID16			(1 << 18)
30 #define IDR0_PRI			(1 << 16)
31 #define IDR0_SEV			(1 << 14)
32 #define IDR0_MSI			(1 << 13)
33 #define IDR0_ASID16			(1 << 12)
34 #define IDR0_ATS			(1 << 10)
35 #define IDR0_HYP			(1 << 9)
36 #define IDR0_COHACC			(1 << 4)
37 #define IDR0_TTF			GENMASK(3, 2)
38 #define IDR0_TTF_AARCH64		2
39 #define IDR0_TTF_AARCH32_64		3
40 #define IDR0_S1P			(1 << 1)
41 #define IDR0_S2P			(1 << 0)
42 
43 #define ARM_SMMU_IDR1			0x4
44 #define IDR1_TABLES_PRESET		(1 << 30)
45 #define IDR1_QUEUES_PRESET		(1 << 29)
46 #define IDR1_REL			(1 << 28)
47 #define IDR1_CMDQS			GENMASK(25, 21)
48 #define IDR1_EVTQS			GENMASK(20, 16)
49 #define IDR1_PRIQS			GENMASK(15, 11)
50 #define IDR1_SSIDSIZE			GENMASK(10, 6)
51 #define IDR1_SIDSIZE			GENMASK(5, 0)
52 
53 #define ARM_SMMU_IDR3			0xc
54 #define IDR3_RIL			(1 << 10)
55 
56 #define ARM_SMMU_IDR5			0x14
57 #define IDR5_STALL_MAX			GENMASK(31, 16)
58 #define IDR5_GRAN64K			(1 << 6)
59 #define IDR5_GRAN16K			(1 << 5)
60 #define IDR5_GRAN4K			(1 << 4)
61 #define IDR5_OAS			GENMASK(2, 0)
62 #define IDR5_OAS_32_BIT			0
63 #define IDR5_OAS_36_BIT			1
64 #define IDR5_OAS_40_BIT			2
65 #define IDR5_OAS_42_BIT			3
66 #define IDR5_OAS_44_BIT			4
67 #define IDR5_OAS_48_BIT			5
68 #define IDR5_OAS_52_BIT			6
69 #define IDR5_VAX			GENMASK(11, 10)
70 #define IDR5_VAX_52_BIT			1
71 
72 #define ARM_SMMU_CR0			0x20
73 #define CR0_ATSCHK			(1 << 4)
74 #define CR0_CMDQEN			(1 << 3)
75 #define CR0_EVTQEN			(1 << 2)
76 #define CR0_PRIQEN			(1 << 1)
77 #define CR0_SMMUEN			(1 << 0)
78 
79 #define ARM_SMMU_CR0ACK			0x24
80 
81 #define ARM_SMMU_CR1			0x28
82 #define CR1_TABLE_SH			GENMASK(11, 10)
83 #define CR1_TABLE_OC			GENMASK(9, 8)
84 #define CR1_TABLE_IC			GENMASK(7, 6)
85 #define CR1_QUEUE_SH			GENMASK(5, 4)
86 #define CR1_QUEUE_OC			GENMASK(3, 2)
87 #define CR1_QUEUE_IC			GENMASK(1, 0)
88 /* CR1 cacheability fields don't quite follow the usual TCR-style encoding */
89 #define CR1_CACHE_NC			0
90 #define CR1_CACHE_WB			1
91 #define CR1_CACHE_WT			2
92 
93 #define ARM_SMMU_CR2			0x2c
94 #define CR2_PTM				(1 << 2)
95 #define CR2_RECINVSID			(1 << 1)
96 #define CR2_E2H				(1 << 0)
97 
98 #define ARM_SMMU_GBPA			0x44
99 #define GBPA_UPDATE			(1 << 31)
100 #define GBPA_ABORT			(1 << 20)
101 
102 #define ARM_SMMU_IRQ_CTRL		0x50
103 #define IRQ_CTRL_EVTQ_IRQEN		(1 << 2)
104 #define IRQ_CTRL_PRIQ_IRQEN		(1 << 1)
105 #define IRQ_CTRL_GERROR_IRQEN		(1 << 0)
106 
107 #define ARM_SMMU_IRQ_CTRLACK		0x54
108 
109 #define ARM_SMMU_GERROR			0x60
110 #define GERROR_SFM_ERR			(1 << 8)
111 #define GERROR_MSI_GERROR_ABT_ERR	(1 << 7)
112 #define GERROR_MSI_PRIQ_ABT_ERR		(1 << 6)
113 #define GERROR_MSI_EVTQ_ABT_ERR		(1 << 5)
114 #define GERROR_MSI_CMDQ_ABT_ERR		(1 << 4)
115 #define GERROR_PRIQ_ABT_ERR		(1 << 3)
116 #define GERROR_EVTQ_ABT_ERR		(1 << 2)
117 #define GERROR_CMDQ_ERR			(1 << 0)
118 #define GERROR_ERR_MASK			0xfd
119 
120 #define ARM_SMMU_GERRORN		0x64
121 
122 #define ARM_SMMU_GERROR_IRQ_CFG0	0x68
123 #define ARM_SMMU_GERROR_IRQ_CFG1	0x70
124 #define ARM_SMMU_GERROR_IRQ_CFG2	0x74
125 
126 #define ARM_SMMU_STRTAB_BASE		0x80
127 #define STRTAB_BASE_RA			(1UL << 62)
128 #define STRTAB_BASE_ADDR_MASK		GENMASK_ULL(51, 6)
129 
130 #define ARM_SMMU_STRTAB_BASE_CFG	0x88
131 #define STRTAB_BASE_CFG_FMT		GENMASK(17, 16)
132 #define STRTAB_BASE_CFG_FMT_LINEAR	0
133 #define STRTAB_BASE_CFG_FMT_2LVL	1
134 #define STRTAB_BASE_CFG_SPLIT		GENMASK(10, 6)
135 #define STRTAB_BASE_CFG_LOG2SIZE	GENMASK(5, 0)
136 
137 #define ARM_SMMU_CMDQ_BASE		0x90
138 #define ARM_SMMU_CMDQ_PROD		0x98
139 #define ARM_SMMU_CMDQ_CONS		0x9c
140 
141 #define ARM_SMMU_EVTQ_BASE		0xa0
142 #define ARM_SMMU_EVTQ_PROD		0xa8
143 #define ARM_SMMU_EVTQ_CONS		0xac
144 #define ARM_SMMU_EVTQ_IRQ_CFG0		0xb0
145 #define ARM_SMMU_EVTQ_IRQ_CFG1		0xb8
146 #define ARM_SMMU_EVTQ_IRQ_CFG2		0xbc
147 
148 #define ARM_SMMU_PRIQ_BASE		0xc0
149 #define ARM_SMMU_PRIQ_PROD		0xc8
150 #define ARM_SMMU_PRIQ_CONS		0xcc
151 #define ARM_SMMU_PRIQ_IRQ_CFG0		0xd0
152 #define ARM_SMMU_PRIQ_IRQ_CFG1		0xd8
153 #define ARM_SMMU_PRIQ_IRQ_CFG2		0xdc
154 
155 #define ARM_SMMU_REG_SZ			0xe00
156 
157 /* Common MSI config fields */
158 #define MSI_CFG0_ADDR_MASK		GENMASK_ULL(51, 2)
159 #define MSI_CFG2_SH			GENMASK(5, 4)
160 #define MSI_CFG2_MEMATTR		GENMASK(3, 0)
161 
162 /* Common memory attribute values */
163 #define ARM_SMMU_SH_NSH			0
164 #define ARM_SMMU_SH_OSH			2
165 #define ARM_SMMU_SH_ISH			3
166 #define ARM_SMMU_MEMATTR_DEVICE_nGnRE	0x1
167 #define ARM_SMMU_MEMATTR_OIWB		0xf
168 
169 #define Q_IDX(llq, p)			((p) & ((1 << (llq)->max_n_shift) - 1))
170 #define Q_WRP(llq, p)			((p) & (1 << (llq)->max_n_shift))
171 #define Q_OVERFLOW_FLAG			(1U << 31)
172 #define Q_OVF(p)			((p) & Q_OVERFLOW_FLAG)
173 #define Q_ENT(q, p)			((q)->base +			\
174 					 Q_IDX(&((q)->llq), p) *	\
175 					 (q)->ent_dwords)
176 
177 #define Q_BASE_RWA			(1UL << 62)
178 #define Q_BASE_ADDR_MASK		GENMASK_ULL(51, 5)
179 #define Q_BASE_LOG2SIZE			GENMASK(4, 0)
180 
181 /* Ensure DMA allocations are naturally aligned */
182 #ifdef CONFIG_CMA_ALIGNMENT
183 #define Q_MAX_SZ_SHIFT			(PAGE_SHIFT + CONFIG_CMA_ALIGNMENT)
184 #else
185 #define Q_MAX_SZ_SHIFT			(PAGE_SHIFT + MAX_ORDER - 1)
186 #endif
187 
188 /*
189  * Stream table.
190  *
191  * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
192  * 2lvl: 128k L1 entries,
193  *       256 lazy entries per table (each table covers a PCI bus)
194  */
195 #define STRTAB_L1_SZ_SHIFT		20
196 #define STRTAB_SPLIT			8
197 
198 #define STRTAB_L1_DESC_DWORDS		1
199 #define STRTAB_L1_DESC_SPAN		GENMASK_ULL(4, 0)
200 #define STRTAB_L1_DESC_L2PTR_MASK	GENMASK_ULL(51, 6)
201 
202 #define STRTAB_STE_DWORDS		8
203 #define STRTAB_STE_0_V			(1UL << 0)
204 #define STRTAB_STE_0_CFG		GENMASK_ULL(3, 1)
205 #define STRTAB_STE_0_CFG_ABORT		0
206 #define STRTAB_STE_0_CFG_BYPASS		4
207 #define STRTAB_STE_0_CFG_S1_TRANS	5
208 #define STRTAB_STE_0_CFG_S2_TRANS	6
209 
210 #define STRTAB_STE_0_S1FMT		GENMASK_ULL(5, 4)
211 #define STRTAB_STE_0_S1FMT_LINEAR	0
212 #define STRTAB_STE_0_S1FMT_64K_L2	2
213 #define STRTAB_STE_0_S1CTXPTR_MASK	GENMASK_ULL(51, 6)
214 #define STRTAB_STE_0_S1CDMAX		GENMASK_ULL(63, 59)
215 
216 #define STRTAB_STE_1_S1DSS		GENMASK_ULL(1, 0)
217 #define STRTAB_STE_1_S1DSS_TERMINATE	0x0
218 #define STRTAB_STE_1_S1DSS_BYPASS	0x1
219 #define STRTAB_STE_1_S1DSS_SSID0	0x2
220 
221 #define STRTAB_STE_1_S1C_CACHE_NC	0UL
222 #define STRTAB_STE_1_S1C_CACHE_WBRA	1UL
223 #define STRTAB_STE_1_S1C_CACHE_WT	2UL
224 #define STRTAB_STE_1_S1C_CACHE_WB	3UL
225 #define STRTAB_STE_1_S1CIR		GENMASK_ULL(3, 2)
226 #define STRTAB_STE_1_S1COR		GENMASK_ULL(5, 4)
227 #define STRTAB_STE_1_S1CSH		GENMASK_ULL(7, 6)
228 
229 #define STRTAB_STE_1_S1STALLD		(1UL << 27)
230 
231 #define STRTAB_STE_1_EATS		GENMASK_ULL(29, 28)
232 #define STRTAB_STE_1_EATS_ABT		0UL
233 #define STRTAB_STE_1_EATS_TRANS		1UL
234 #define STRTAB_STE_1_EATS_S1CHK		2UL
235 
236 #define STRTAB_STE_1_STRW		GENMASK_ULL(31, 30)
237 #define STRTAB_STE_1_STRW_NSEL1		0UL
238 #define STRTAB_STE_1_STRW_EL2		2UL
239 
240 #define STRTAB_STE_1_SHCFG		GENMASK_ULL(45, 44)
241 #define STRTAB_STE_1_SHCFG_INCOMING	1UL
242 
243 #define STRTAB_STE_2_S2VMID		GENMASK_ULL(15, 0)
244 #define STRTAB_STE_2_VTCR		GENMASK_ULL(50, 32)
245 #define STRTAB_STE_2_VTCR_S2T0SZ	GENMASK_ULL(5, 0)
246 #define STRTAB_STE_2_VTCR_S2SL0		GENMASK_ULL(7, 6)
247 #define STRTAB_STE_2_VTCR_S2IR0		GENMASK_ULL(9, 8)
248 #define STRTAB_STE_2_VTCR_S2OR0		GENMASK_ULL(11, 10)
249 #define STRTAB_STE_2_VTCR_S2SH0		GENMASK_ULL(13, 12)
250 #define STRTAB_STE_2_VTCR_S2TG		GENMASK_ULL(15, 14)
251 #define STRTAB_STE_2_VTCR_S2PS		GENMASK_ULL(18, 16)
252 #define STRTAB_STE_2_S2AA64		(1UL << 51)
253 #define STRTAB_STE_2_S2ENDI		(1UL << 52)
254 #define STRTAB_STE_2_S2PTW		(1UL << 54)
255 #define STRTAB_STE_2_S2R		(1UL << 58)
256 
257 #define STRTAB_STE_3_S2TTB_MASK		GENMASK_ULL(51, 4)
258 
259 /*
260  * Context descriptors.
261  *
262  * Linear: when less than 1024 SSIDs are supported
263  * 2lvl: at most 1024 L1 entries,
264  *       1024 lazy entries per table.
265  */
266 #define CTXDESC_SPLIT			10
267 #define CTXDESC_L2_ENTRIES		(1 << CTXDESC_SPLIT)
268 
269 #define CTXDESC_L1_DESC_DWORDS		1
270 #define CTXDESC_L1_DESC_V		(1UL << 0)
271 #define CTXDESC_L1_DESC_L2PTR_MASK	GENMASK_ULL(51, 12)
272 
273 #define CTXDESC_CD_DWORDS		8
274 #define CTXDESC_CD_0_TCR_T0SZ		GENMASK_ULL(5, 0)
275 #define CTXDESC_CD_0_TCR_TG0		GENMASK_ULL(7, 6)
276 #define CTXDESC_CD_0_TCR_IRGN0		GENMASK_ULL(9, 8)
277 #define CTXDESC_CD_0_TCR_ORGN0		GENMASK_ULL(11, 10)
278 #define CTXDESC_CD_0_TCR_SH0		GENMASK_ULL(13, 12)
279 #define CTXDESC_CD_0_TCR_EPD0		(1ULL << 14)
280 #define CTXDESC_CD_0_TCR_EPD1		(1ULL << 30)
281 
282 #define CTXDESC_CD_0_ENDI		(1UL << 15)
283 #define CTXDESC_CD_0_V			(1UL << 31)
284 
285 #define CTXDESC_CD_0_TCR_IPS		GENMASK_ULL(34, 32)
286 #define CTXDESC_CD_0_TCR_TBI0		(1ULL << 38)
287 
288 #define CTXDESC_CD_0_AA64		(1UL << 41)
289 #define CTXDESC_CD_0_S			(1UL << 44)
290 #define CTXDESC_CD_0_R			(1UL << 45)
291 #define CTXDESC_CD_0_A			(1UL << 46)
292 #define CTXDESC_CD_0_ASET		(1UL << 47)
293 #define CTXDESC_CD_0_ASID		GENMASK_ULL(63, 48)
294 
295 #define CTXDESC_CD_1_TTB0_MASK		GENMASK_ULL(51, 4)
296 
297 /*
298  * When the SMMU only supports linear context descriptor tables, pick a
299  * reasonable size limit (64kB).
300  */
301 #define CTXDESC_LINEAR_CDMAX		ilog2(SZ_64K / (CTXDESC_CD_DWORDS << 3))
302 
303 /* Command queue */
304 #define CMDQ_ENT_SZ_SHIFT		4
305 #define CMDQ_ENT_DWORDS			((1 << CMDQ_ENT_SZ_SHIFT) >> 3)
306 #define CMDQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - CMDQ_ENT_SZ_SHIFT)
307 
308 #define CMDQ_CONS_ERR			GENMASK(30, 24)
309 #define CMDQ_ERR_CERROR_NONE_IDX	0
310 #define CMDQ_ERR_CERROR_ILL_IDX		1
311 #define CMDQ_ERR_CERROR_ABT_IDX		2
312 #define CMDQ_ERR_CERROR_ATC_INV_IDX	3
313 
314 #define CMDQ_PROD_OWNED_FLAG		Q_OVERFLOW_FLAG
315 
316 /*
317  * This is used to size the command queue and therefore must be at least
318  * BITS_PER_LONG so that the valid_map works correctly (it relies on the
319  * total number of queue entries being a multiple of BITS_PER_LONG).
320  */
321 #define CMDQ_BATCH_ENTRIES		BITS_PER_LONG
322 
323 #define CMDQ_0_OP			GENMASK_ULL(7, 0)
324 #define CMDQ_0_SSV			(1UL << 11)
325 
326 #define CMDQ_PREFETCH_0_SID		GENMASK_ULL(63, 32)
327 #define CMDQ_PREFETCH_1_SIZE		GENMASK_ULL(4, 0)
328 #define CMDQ_PREFETCH_1_ADDR_MASK	GENMASK_ULL(63, 12)
329 
330 #define CMDQ_CFGI_0_SSID		GENMASK_ULL(31, 12)
331 #define CMDQ_CFGI_0_SID			GENMASK_ULL(63, 32)
332 #define CMDQ_CFGI_1_LEAF		(1UL << 0)
333 #define CMDQ_CFGI_1_RANGE		GENMASK_ULL(4, 0)
334 
335 #define CMDQ_TLBI_0_NUM			GENMASK_ULL(16, 12)
336 #define CMDQ_TLBI_RANGE_NUM_MAX		31
337 #define CMDQ_TLBI_0_SCALE		GENMASK_ULL(24, 20)
338 #define CMDQ_TLBI_0_VMID		GENMASK_ULL(47, 32)
339 #define CMDQ_TLBI_0_ASID		GENMASK_ULL(63, 48)
340 #define CMDQ_TLBI_1_LEAF		(1UL << 0)
341 #define CMDQ_TLBI_1_TTL			GENMASK_ULL(9, 8)
342 #define CMDQ_TLBI_1_TG			GENMASK_ULL(11, 10)
343 #define CMDQ_TLBI_1_VA_MASK		GENMASK_ULL(63, 12)
344 #define CMDQ_TLBI_1_IPA_MASK		GENMASK_ULL(51, 12)
345 
346 #define CMDQ_ATC_0_SSID			GENMASK_ULL(31, 12)
347 #define CMDQ_ATC_0_SID			GENMASK_ULL(63, 32)
348 #define CMDQ_ATC_0_GLOBAL		(1UL << 9)
349 #define CMDQ_ATC_1_SIZE			GENMASK_ULL(5, 0)
350 #define CMDQ_ATC_1_ADDR_MASK		GENMASK_ULL(63, 12)
351 
352 #define CMDQ_PRI_0_SSID			GENMASK_ULL(31, 12)
353 #define CMDQ_PRI_0_SID			GENMASK_ULL(63, 32)
354 #define CMDQ_PRI_1_GRPID		GENMASK_ULL(8, 0)
355 #define CMDQ_PRI_1_RESP			GENMASK_ULL(13, 12)
356 
357 #define CMDQ_SYNC_0_CS			GENMASK_ULL(13, 12)
358 #define CMDQ_SYNC_0_CS_NONE		0
359 #define CMDQ_SYNC_0_CS_IRQ		1
360 #define CMDQ_SYNC_0_CS_SEV		2
361 #define CMDQ_SYNC_0_MSH			GENMASK_ULL(23, 22)
362 #define CMDQ_SYNC_0_MSIATTR		GENMASK_ULL(27, 24)
363 #define CMDQ_SYNC_0_MSIDATA		GENMASK_ULL(63, 32)
364 #define CMDQ_SYNC_1_MSIADDR_MASK	GENMASK_ULL(51, 2)
365 
366 /* Event queue */
367 #define EVTQ_ENT_SZ_SHIFT		5
368 #define EVTQ_ENT_DWORDS			((1 << EVTQ_ENT_SZ_SHIFT) >> 3)
369 #define EVTQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - EVTQ_ENT_SZ_SHIFT)
370 
371 #define EVTQ_0_ID			GENMASK_ULL(7, 0)
372 
373 /* PRI queue */
374 #define PRIQ_ENT_SZ_SHIFT		4
375 #define PRIQ_ENT_DWORDS			((1 << PRIQ_ENT_SZ_SHIFT) >> 3)
376 #define PRIQ_MAX_SZ_SHIFT		(Q_MAX_SZ_SHIFT - PRIQ_ENT_SZ_SHIFT)
377 
378 #define PRIQ_0_SID			GENMASK_ULL(31, 0)
379 #define PRIQ_0_SSID			GENMASK_ULL(51, 32)
380 #define PRIQ_0_PERM_PRIV		(1UL << 58)
381 #define PRIQ_0_PERM_EXEC		(1UL << 59)
382 #define PRIQ_0_PERM_READ		(1UL << 60)
383 #define PRIQ_0_PERM_WRITE		(1UL << 61)
384 #define PRIQ_0_PRG_LAST			(1UL << 62)
385 #define PRIQ_0_SSID_V			(1UL << 63)
386 
387 #define PRIQ_1_PRG_IDX			GENMASK_ULL(8, 0)
388 #define PRIQ_1_ADDR_MASK		GENMASK_ULL(63, 12)
389 
390 /* High-level queue structures */
391 #define ARM_SMMU_POLL_TIMEOUT_US	1000000 /* 1s! */
392 #define ARM_SMMU_POLL_SPIN_COUNT	10
393 
394 #define MSI_IOVA_BASE			0x8000000
395 #define MSI_IOVA_LENGTH			0x100000
396 
397 enum pri_resp {
398 	PRI_RESP_DENY = 0,
399 	PRI_RESP_FAIL = 1,
400 	PRI_RESP_SUCC = 2,
401 };
402 
403 struct arm_smmu_cmdq_ent {
404 	/* Common fields */
405 	u8				opcode;
406 	bool				substream_valid;
407 
408 	/* Command-specific fields */
409 	union {
410 		#define CMDQ_OP_PREFETCH_CFG	0x1
411 		struct {
412 			u32			sid;
413 			u8			size;
414 			u64			addr;
415 		} prefetch;
416 
417 		#define CMDQ_OP_CFGI_STE	0x3
418 		#define CMDQ_OP_CFGI_ALL	0x4
419 		#define CMDQ_OP_CFGI_CD		0x5
420 		#define CMDQ_OP_CFGI_CD_ALL	0x6
421 		struct {
422 			u32			sid;
423 			u32			ssid;
424 			union {
425 				bool		leaf;
426 				u8		span;
427 			};
428 		} cfgi;
429 
430 		#define CMDQ_OP_TLBI_NH_ASID	0x11
431 		#define CMDQ_OP_TLBI_NH_VA	0x12
432 		#define CMDQ_OP_TLBI_EL2_ALL	0x20
433 		#define CMDQ_OP_TLBI_EL2_ASID	0x21
434 		#define CMDQ_OP_TLBI_EL2_VA	0x22
435 		#define CMDQ_OP_TLBI_S12_VMALL	0x28
436 		#define CMDQ_OP_TLBI_S2_IPA	0x2a
437 		#define CMDQ_OP_TLBI_NSNH_ALL	0x30
438 		struct {
439 			u8			num;
440 			u8			scale;
441 			u16			asid;
442 			u16			vmid;
443 			bool			leaf;
444 			u8			ttl;
445 			u8			tg;
446 			u64			addr;
447 		} tlbi;
448 
449 		#define CMDQ_OP_ATC_INV		0x40
450 		#define ATC_INV_SIZE_ALL	52
451 		struct {
452 			u32			sid;
453 			u32			ssid;
454 			u64			addr;
455 			u8			size;
456 			bool			global;
457 		} atc;
458 
459 		#define CMDQ_OP_PRI_RESP	0x41
460 		struct {
461 			u32			sid;
462 			u32			ssid;
463 			u16			grpid;
464 			enum pri_resp		resp;
465 		} pri;
466 
467 		#define CMDQ_OP_CMD_SYNC	0x46
468 		struct {
469 			u64			msiaddr;
470 		} sync;
471 	};
472 };
473 
474 struct arm_smmu_ll_queue {
475 	union {
476 		u64			val;
477 		struct {
478 			u32		prod;
479 			u32		cons;
480 		};
481 		struct {
482 			atomic_t	prod;
483 			atomic_t	cons;
484 		} atomic;
485 		u8			__pad[SMP_CACHE_BYTES];
486 	} ____cacheline_aligned_in_smp;
487 	u32				max_n_shift;
488 };
489 
490 struct arm_smmu_queue {
491 	struct arm_smmu_ll_queue	llq;
492 	int				irq; /* Wired interrupt */
493 
494 	__le64				*base;
495 	dma_addr_t			base_dma;
496 	u64				q_base;
497 
498 	size_t				ent_dwords;
499 
500 	u32 __iomem			*prod_reg;
501 	u32 __iomem			*cons_reg;
502 };
503 
504 struct arm_smmu_queue_poll {
505 	ktime_t				timeout;
506 	unsigned int			delay;
507 	unsigned int			spin_cnt;
508 	bool				wfe;
509 };
510 
511 struct arm_smmu_cmdq {
512 	struct arm_smmu_queue		q;
513 	atomic_long_t			*valid_map;
514 	atomic_t			owner_prod;
515 	atomic_t			lock;
516 };
517 
518 struct arm_smmu_cmdq_batch {
519 	u64				cmds[CMDQ_BATCH_ENTRIES * CMDQ_ENT_DWORDS];
520 	int				num;
521 };
522 
523 struct arm_smmu_evtq {
524 	struct arm_smmu_queue		q;
525 	u32				max_stalls;
526 };
527 
528 struct arm_smmu_priq {
529 	struct arm_smmu_queue		q;
530 };
531 
532 /* High-level stream table and context descriptor structures */
533 struct arm_smmu_strtab_l1_desc {
534 	u8				span;
535 
536 	__le64				*l2ptr;
537 	dma_addr_t			l2ptr_dma;
538 };
539 
540 struct arm_smmu_ctx_desc {
541 	u16				asid;
542 	u64				ttbr;
543 	u64				tcr;
544 	u64				mair;
545 
546 	refcount_t			refs;
547 	struct mm_struct		*mm;
548 };
549 
550 struct arm_smmu_l1_ctx_desc {
551 	__le64				*l2ptr;
552 	dma_addr_t			l2ptr_dma;
553 };
554 
555 struct arm_smmu_ctx_desc_cfg {
556 	__le64				*cdtab;
557 	dma_addr_t			cdtab_dma;
558 	struct arm_smmu_l1_ctx_desc	*l1_desc;
559 	unsigned int			num_l1_ents;
560 };
561 
562 struct arm_smmu_s1_cfg {
563 	struct arm_smmu_ctx_desc_cfg	cdcfg;
564 	struct arm_smmu_ctx_desc	cd;
565 	u8				s1fmt;
566 	u8				s1cdmax;
567 };
568 
569 struct arm_smmu_s2_cfg {
570 	u16				vmid;
571 	u64				vttbr;
572 	u64				vtcr;
573 };
574 
575 struct arm_smmu_strtab_cfg {
576 	__le64				*strtab;
577 	dma_addr_t			strtab_dma;
578 	struct arm_smmu_strtab_l1_desc	*l1_desc;
579 	unsigned int			num_l1_ents;
580 
581 	u64				strtab_base;
582 	u32				strtab_base_cfg;
583 };
584 
585 /* An SMMUv3 instance */
586 struct arm_smmu_device {
587 	struct device			*dev;
588 	void __iomem			*base;
589 	void __iomem			*page1;
590 
591 #define ARM_SMMU_FEAT_2_LVL_STRTAB	(1 << 0)
592 #define ARM_SMMU_FEAT_2_LVL_CDTAB	(1 << 1)
593 #define ARM_SMMU_FEAT_TT_LE		(1 << 2)
594 #define ARM_SMMU_FEAT_TT_BE		(1 << 3)
595 #define ARM_SMMU_FEAT_PRI		(1 << 4)
596 #define ARM_SMMU_FEAT_ATS		(1 << 5)
597 #define ARM_SMMU_FEAT_SEV		(1 << 6)
598 #define ARM_SMMU_FEAT_MSI		(1 << 7)
599 #define ARM_SMMU_FEAT_COHERENCY		(1 << 8)
600 #define ARM_SMMU_FEAT_TRANS_S1		(1 << 9)
601 #define ARM_SMMU_FEAT_TRANS_S2		(1 << 10)
602 #define ARM_SMMU_FEAT_STALLS		(1 << 11)
603 #define ARM_SMMU_FEAT_HYP		(1 << 12)
604 #define ARM_SMMU_FEAT_STALL_FORCE	(1 << 13)
605 #define ARM_SMMU_FEAT_VAX		(1 << 14)
606 #define ARM_SMMU_FEAT_RANGE_INV		(1 << 15)
607 #define ARM_SMMU_FEAT_BTM		(1 << 16)
608 #define ARM_SMMU_FEAT_SVA		(1 << 17)
609 #define ARM_SMMU_FEAT_E2H		(1 << 18)
610 	u32				features;
611 
612 #define ARM_SMMU_OPT_SKIP_PREFETCH	(1 << 0)
613 #define ARM_SMMU_OPT_PAGE0_REGS_ONLY	(1 << 1)
614 #define ARM_SMMU_OPT_MSIPOLL		(1 << 2)
615 	u32				options;
616 
617 	struct arm_smmu_cmdq		cmdq;
618 	struct arm_smmu_evtq		evtq;
619 	struct arm_smmu_priq		priq;
620 
621 	int				gerr_irq;
622 	int				combined_irq;
623 
624 	unsigned long			ias; /* IPA */
625 	unsigned long			oas; /* PA */
626 	unsigned long			pgsize_bitmap;
627 
628 #define ARM_SMMU_MAX_ASIDS		(1 << 16)
629 	unsigned int			asid_bits;
630 
631 #define ARM_SMMU_MAX_VMIDS		(1 << 16)
632 	unsigned int			vmid_bits;
633 	DECLARE_BITMAP(vmid_map, ARM_SMMU_MAX_VMIDS);
634 
635 	unsigned int			ssid_bits;
636 	unsigned int			sid_bits;
637 
638 	struct arm_smmu_strtab_cfg	strtab_cfg;
639 
640 	/* IOMMU core code handle */
641 	struct iommu_device		iommu;
642 };
643 
644 /* SMMU private data for each master */
645 struct arm_smmu_master {
646 	struct arm_smmu_device		*smmu;
647 	struct device			*dev;
648 	struct arm_smmu_domain		*domain;
649 	struct list_head		domain_head;
650 	u32				*sids;
651 	unsigned int			num_sids;
652 	bool				ats_enabled;
653 	bool				sva_enabled;
654 	struct list_head		bonds;
655 	unsigned int			ssid_bits;
656 };
657 
658 /* SMMU private data for an IOMMU domain */
659 enum arm_smmu_domain_stage {
660 	ARM_SMMU_DOMAIN_S1 = 0,
661 	ARM_SMMU_DOMAIN_S2,
662 	ARM_SMMU_DOMAIN_NESTED,
663 	ARM_SMMU_DOMAIN_BYPASS,
664 };
665 
666 struct arm_smmu_domain {
667 	struct arm_smmu_device		*smmu;
668 	struct mutex			init_mutex; /* Protects smmu pointer */
669 
670 	struct io_pgtable_ops		*pgtbl_ops;
671 	bool				non_strict;
672 	atomic_t			nr_ats_masters;
673 
674 	enum arm_smmu_domain_stage	stage;
675 	union {
676 		struct arm_smmu_s1_cfg	s1_cfg;
677 		struct arm_smmu_s2_cfg	s2_cfg;
678 	};
679 
680 	struct iommu_domain		domain;
681 
682 	struct list_head		devices;
683 	spinlock_t			devices_lock;
684 
685 	struct list_head		mmu_notifiers;
686 };
687 
688 static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
689 {
690 	return container_of(dom, struct arm_smmu_domain, domain);
691 }
692 
693 extern struct xarray arm_smmu_asid_xa;
694 extern struct mutex arm_smmu_asid_lock;
695 extern struct arm_smmu_ctx_desc quiet_cd;
696 
697 int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain, int ssid,
698 			    struct arm_smmu_ctx_desc *cd);
699 void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid);
700 void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,
701 				 size_t granule, bool leaf,
702 				 struct arm_smmu_domain *smmu_domain);
703 bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd);
704 int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid,
705 			    unsigned long iova, size_t size);
706 
707 #ifdef CONFIG_ARM_SMMU_V3_SVA
708 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu);
709 bool arm_smmu_master_sva_supported(struct arm_smmu_master *master);
710 bool arm_smmu_master_sva_enabled(struct arm_smmu_master *master);
711 int arm_smmu_master_enable_sva(struct arm_smmu_master *master);
712 int arm_smmu_master_disable_sva(struct arm_smmu_master *master);
713 struct iommu_sva *arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm,
714 				    void *drvdata);
715 void arm_smmu_sva_unbind(struct iommu_sva *handle);
716 u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle);
717 void arm_smmu_sva_notifier_synchronize(void);
718 #else /* CONFIG_ARM_SMMU_V3_SVA */
719 static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
720 {
721 	return false;
722 }
723 
724 static inline bool arm_smmu_master_sva_supported(struct arm_smmu_master *master)
725 {
726 	return false;
727 }
728 
729 static inline bool arm_smmu_master_sva_enabled(struct arm_smmu_master *master)
730 {
731 	return false;
732 }
733 
734 static inline int arm_smmu_master_enable_sva(struct arm_smmu_master *master)
735 {
736 	return -ENODEV;
737 }
738 
739 static inline int arm_smmu_master_disable_sva(struct arm_smmu_master *master)
740 {
741 	return -ENODEV;
742 }
743 
744 static inline struct iommu_sva *
745 arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
746 {
747 	return ERR_PTR(-ENODEV);
748 }
749 
750 static inline void arm_smmu_sva_unbind(struct iommu_sva *handle) {}
751 
752 static inline u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle)
753 {
754 	return IOMMU_PASID_INVALID;
755 }
756 
757 static inline void arm_smmu_sva_notifier_synchronize(void) {}
758 #endif /* CONFIG_ARM_SMMU_V3_SVA */
759 #endif /* _ARM_SMMU_V3_H */
760