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