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