2 * IOMMU API for ARM architected SMMUv3 implementations.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2015 ARM Limited
18 * Author: Will Deacon <will.deacon@arm.com>
20 * This driver is powered by bad coffee and bombay mix.
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/interrupt.h>
26 #include <linux/iommu.h>
27 #include <linux/iopoll.h>
28 #include <linux/module.h>
30 #include <linux/of_address.h>
31 #include <linux/pci.h>
32 #include <linux/platform_device.h>
34 #include "io-pgtable.h"
37 #define ARM_SMMU_IDR0 0x0
38 #define IDR0_ST_LVL_SHIFT 27
39 #define IDR0_ST_LVL_MASK 0x3
40 #define IDR0_ST_LVL_2LVL (1 << IDR0_ST_LVL_SHIFT)
41 #define IDR0_STALL_MODEL (3 << 24)
42 #define IDR0_TTENDIAN_SHIFT 21
43 #define IDR0_TTENDIAN_MASK 0x3
44 #define IDR0_TTENDIAN_LE (2 << IDR0_TTENDIAN_SHIFT)
45 #define IDR0_TTENDIAN_BE (3 << IDR0_TTENDIAN_SHIFT)
46 #define IDR0_TTENDIAN_MIXED (0 << IDR0_TTENDIAN_SHIFT)
47 #define IDR0_CD2L (1 << 19)
48 #define IDR0_VMID16 (1 << 18)
49 #define IDR0_PRI (1 << 16)
50 #define IDR0_SEV (1 << 14)
51 #define IDR0_MSI (1 << 13)
52 #define IDR0_ASID16 (1 << 12)
53 #define IDR0_ATS (1 << 10)
54 #define IDR0_HYP (1 << 9)
55 #define IDR0_COHACC (1 << 4)
56 #define IDR0_TTF_SHIFT 2
57 #define IDR0_TTF_MASK 0x3
58 #define IDR0_TTF_AARCH64 (2 << IDR0_TTF_SHIFT)
59 #define IDR0_TTF_AARCH32_64 (3 << IDR0_TTF_SHIFT)
60 #define IDR0_S1P (1 << 1)
61 #define IDR0_S2P (1 << 0)
63 #define ARM_SMMU_IDR1 0x4
64 #define IDR1_TABLES_PRESET (1 << 30)
65 #define IDR1_QUEUES_PRESET (1 << 29)
66 #define IDR1_REL (1 << 28)
67 #define IDR1_CMDQ_SHIFT 21
68 #define IDR1_CMDQ_MASK 0x1f
69 #define IDR1_EVTQ_SHIFT 16
70 #define IDR1_EVTQ_MASK 0x1f
71 #define IDR1_PRIQ_SHIFT 11
72 #define IDR1_PRIQ_MASK 0x1f
73 #define IDR1_SSID_SHIFT 6
74 #define IDR1_SSID_MASK 0x1f
75 #define IDR1_SID_SHIFT 0
76 #define IDR1_SID_MASK 0x3f
78 #define ARM_SMMU_IDR5 0x14
79 #define IDR5_STALL_MAX_SHIFT 16
80 #define IDR5_STALL_MAX_MASK 0xffff
81 #define IDR5_GRAN64K (1 << 6)
82 #define IDR5_GRAN16K (1 << 5)
83 #define IDR5_GRAN4K (1 << 4)
84 #define IDR5_OAS_SHIFT 0
85 #define IDR5_OAS_MASK 0x7
86 #define IDR5_OAS_32_BIT (0 << IDR5_OAS_SHIFT)
87 #define IDR5_OAS_36_BIT (1 << IDR5_OAS_SHIFT)
88 #define IDR5_OAS_40_BIT (2 << IDR5_OAS_SHIFT)
89 #define IDR5_OAS_42_BIT (3 << IDR5_OAS_SHIFT)
90 #define IDR5_OAS_44_BIT (4 << IDR5_OAS_SHIFT)
91 #define IDR5_OAS_48_BIT (5 << IDR5_OAS_SHIFT)
93 #define ARM_SMMU_CR0 0x20
94 #define CR0_CMDQEN (1 << 3)
95 #define CR0_EVTQEN (1 << 2)
96 #define CR0_PRIQEN (1 << 1)
97 #define CR0_SMMUEN (1 << 0)
99 #define ARM_SMMU_CR0ACK 0x24
101 #define ARM_SMMU_CR1 0x28
105 #define CR1_CACHE_NC 0
106 #define CR1_CACHE_WB 1
107 #define CR1_CACHE_WT 2
108 #define CR1_TABLE_SH_SHIFT 10
109 #define CR1_TABLE_OC_SHIFT 8
110 #define CR1_TABLE_IC_SHIFT 6
111 #define CR1_QUEUE_SH_SHIFT 4
112 #define CR1_QUEUE_OC_SHIFT 2
113 #define CR1_QUEUE_IC_SHIFT 0
115 #define ARM_SMMU_CR2 0x2c
116 #define CR2_PTM (1 << 2)
117 #define CR2_RECINVSID (1 << 1)
118 #define CR2_E2H (1 << 0)
120 #define ARM_SMMU_IRQ_CTRL 0x50
121 #define IRQ_CTRL_EVTQ_IRQEN (1 << 2)
122 #define IRQ_CTRL_PRIQ_IRQEN (1 << 1)
123 #define IRQ_CTRL_GERROR_IRQEN (1 << 0)
125 #define ARM_SMMU_IRQ_CTRLACK 0x54
127 #define ARM_SMMU_GERROR 0x60
128 #define GERROR_SFM_ERR (1 << 8)
129 #define GERROR_MSI_GERROR_ABT_ERR (1 << 7)
130 #define GERROR_MSI_PRIQ_ABT_ERR (1 << 6)
131 #define GERROR_MSI_EVTQ_ABT_ERR (1 << 5)
132 #define GERROR_MSI_CMDQ_ABT_ERR (1 << 4)
133 #define GERROR_PRIQ_ABT_ERR (1 << 3)
134 #define GERROR_EVTQ_ABT_ERR (1 << 2)
135 #define GERROR_CMDQ_ERR (1 << 0)
136 #define GERROR_ERR_MASK 0xfd
138 #define ARM_SMMU_GERRORN 0x64
140 #define ARM_SMMU_GERROR_IRQ_CFG0 0x68
141 #define ARM_SMMU_GERROR_IRQ_CFG1 0x70
142 #define ARM_SMMU_GERROR_IRQ_CFG2 0x74
144 #define ARM_SMMU_STRTAB_BASE 0x80
145 #define STRTAB_BASE_RA (1UL << 62)
146 #define STRTAB_BASE_ADDR_SHIFT 6
147 #define STRTAB_BASE_ADDR_MASK 0x3ffffffffffUL
149 #define ARM_SMMU_STRTAB_BASE_CFG 0x88
150 #define STRTAB_BASE_CFG_LOG2SIZE_SHIFT 0
151 #define STRTAB_BASE_CFG_LOG2SIZE_MASK 0x3f
152 #define STRTAB_BASE_CFG_SPLIT_SHIFT 6
153 #define STRTAB_BASE_CFG_SPLIT_MASK 0x1f
154 #define STRTAB_BASE_CFG_FMT_SHIFT 16
155 #define STRTAB_BASE_CFG_FMT_MASK 0x3
156 #define STRTAB_BASE_CFG_FMT_LINEAR (0 << STRTAB_BASE_CFG_FMT_SHIFT)
157 #define STRTAB_BASE_CFG_FMT_2LVL (1 << STRTAB_BASE_CFG_FMT_SHIFT)
159 #define ARM_SMMU_CMDQ_BASE 0x90
160 #define ARM_SMMU_CMDQ_PROD 0x98
161 #define ARM_SMMU_CMDQ_CONS 0x9c
163 #define ARM_SMMU_EVTQ_BASE 0xa0
164 #define ARM_SMMU_EVTQ_PROD 0x100a8
165 #define ARM_SMMU_EVTQ_CONS 0x100ac
166 #define ARM_SMMU_EVTQ_IRQ_CFG0 0xb0
167 #define ARM_SMMU_EVTQ_IRQ_CFG1 0xb8
168 #define ARM_SMMU_EVTQ_IRQ_CFG2 0xbc
170 #define ARM_SMMU_PRIQ_BASE 0xc0
171 #define ARM_SMMU_PRIQ_PROD 0x100c8
172 #define ARM_SMMU_PRIQ_CONS 0x100cc
173 #define ARM_SMMU_PRIQ_IRQ_CFG0 0xd0
174 #define ARM_SMMU_PRIQ_IRQ_CFG1 0xd8
175 #define ARM_SMMU_PRIQ_IRQ_CFG2 0xdc
177 /* Common MSI config fields */
178 #define MSI_CFG0_ADDR_SHIFT 2
179 #define MSI_CFG0_ADDR_MASK 0x3fffffffffffUL
180 #define MSI_CFG2_SH_SHIFT 4
181 #define MSI_CFG2_SH_NSH (0UL << MSI_CFG2_SH_SHIFT)
182 #define MSI_CFG2_SH_OSH (2UL << MSI_CFG2_SH_SHIFT)
183 #define MSI_CFG2_SH_ISH (3UL << MSI_CFG2_SH_SHIFT)
184 #define MSI_CFG2_MEMATTR_SHIFT 0
185 #define MSI_CFG2_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG2_MEMATTR_SHIFT)
187 #define Q_IDX(q, p) ((p) & ((1 << (q)->max_n_shift) - 1))
188 #define Q_WRP(q, p) ((p) & (1 << (q)->max_n_shift))
189 #define Q_OVERFLOW_FLAG (1 << 31)
190 #define Q_OVF(q, p) ((p) & Q_OVERFLOW_FLAG)
191 #define Q_ENT(q, p) ((q)->base + \
192 Q_IDX(q, p) * (q)->ent_dwords)
194 #define Q_BASE_RWA (1UL << 62)
195 #define Q_BASE_ADDR_SHIFT 5
196 #define Q_BASE_ADDR_MASK 0xfffffffffffUL
197 #define Q_BASE_LOG2SIZE_SHIFT 0
198 #define Q_BASE_LOG2SIZE_MASK 0x1fUL
203 * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
204 * 2lvl: 128k L1 entries,
205 * 256 lazy entries per table (each table covers a PCI bus)
207 #define STRTAB_L1_SZ_SHIFT 20
208 #define STRTAB_SPLIT 8
210 #define STRTAB_L1_DESC_DWORDS 1
211 #define STRTAB_L1_DESC_SPAN_SHIFT 0
212 #define STRTAB_L1_DESC_SPAN_MASK 0x1fUL
213 #define STRTAB_L1_DESC_L2PTR_SHIFT 6
214 #define STRTAB_L1_DESC_L2PTR_MASK 0x3ffffffffffUL
216 #define STRTAB_STE_DWORDS 8
217 #define STRTAB_STE_0_V (1UL << 0)
218 #define STRTAB_STE_0_CFG_SHIFT 1
219 #define STRTAB_STE_0_CFG_MASK 0x7UL
220 #define STRTAB_STE_0_CFG_ABORT (0UL << STRTAB_STE_0_CFG_SHIFT)
221 #define STRTAB_STE_0_CFG_BYPASS (4UL << STRTAB_STE_0_CFG_SHIFT)
222 #define STRTAB_STE_0_CFG_S1_TRANS (5UL << STRTAB_STE_0_CFG_SHIFT)
223 #define STRTAB_STE_0_CFG_S2_TRANS (6UL << STRTAB_STE_0_CFG_SHIFT)
225 #define STRTAB_STE_0_S1FMT_SHIFT 4
226 #define STRTAB_STE_0_S1FMT_LINEAR (0UL << STRTAB_STE_0_S1FMT_SHIFT)
227 #define STRTAB_STE_0_S1CTXPTR_SHIFT 6
228 #define STRTAB_STE_0_S1CTXPTR_MASK 0x3ffffffffffUL
229 #define STRTAB_STE_0_S1CDMAX_SHIFT 59
230 #define STRTAB_STE_0_S1CDMAX_MASK 0x1fUL
232 #define STRTAB_STE_1_S1C_CACHE_NC 0UL
233 #define STRTAB_STE_1_S1C_CACHE_WBRA 1UL
234 #define STRTAB_STE_1_S1C_CACHE_WT 2UL
235 #define STRTAB_STE_1_S1C_CACHE_WB 3UL
236 #define STRTAB_STE_1_S1C_SH_NSH 0UL
237 #define STRTAB_STE_1_S1C_SH_OSH 2UL
238 #define STRTAB_STE_1_S1C_SH_ISH 3UL
239 #define STRTAB_STE_1_S1CIR_SHIFT 2
240 #define STRTAB_STE_1_S1COR_SHIFT 4
241 #define STRTAB_STE_1_S1CSH_SHIFT 6
243 #define STRTAB_STE_1_S1STALLD (1UL << 27)
245 #define STRTAB_STE_1_EATS_ABT 0UL
246 #define STRTAB_STE_1_EATS_TRANS 1UL
247 #define STRTAB_STE_1_EATS_S1CHK 2UL
248 #define STRTAB_STE_1_EATS_SHIFT 28
250 #define STRTAB_STE_1_STRW_NSEL1 0UL
251 #define STRTAB_STE_1_STRW_EL2 2UL
252 #define STRTAB_STE_1_STRW_SHIFT 30
254 #define STRTAB_STE_2_S2VMID_SHIFT 0
255 #define STRTAB_STE_2_S2VMID_MASK 0xffffUL
256 #define STRTAB_STE_2_VTCR_SHIFT 32
257 #define STRTAB_STE_2_VTCR_MASK 0x7ffffUL
258 #define STRTAB_STE_2_S2AA64 (1UL << 51)
259 #define STRTAB_STE_2_S2ENDI (1UL << 52)
260 #define STRTAB_STE_2_S2PTW (1UL << 54)
261 #define STRTAB_STE_2_S2R (1UL << 58)
263 #define STRTAB_STE_3_S2TTB_SHIFT 4
264 #define STRTAB_STE_3_S2TTB_MASK 0xfffffffffffUL
266 /* Context descriptor (stage-1 only) */
267 #define CTXDESC_CD_DWORDS 8
268 #define CTXDESC_CD_0_TCR_T0SZ_SHIFT 0
269 #define ARM64_TCR_T0SZ_SHIFT 0
270 #define ARM64_TCR_T0SZ_MASK 0x1fUL
271 #define CTXDESC_CD_0_TCR_TG0_SHIFT 6
272 #define ARM64_TCR_TG0_SHIFT 14
273 #define ARM64_TCR_TG0_MASK 0x3UL
274 #define CTXDESC_CD_0_TCR_IRGN0_SHIFT 8
275 #define ARM64_TCR_IRGN0_SHIFT 8
276 #define ARM64_TCR_IRGN0_MASK 0x3UL
277 #define CTXDESC_CD_0_TCR_ORGN0_SHIFT 10
278 #define ARM64_TCR_ORGN0_SHIFT 10
279 #define ARM64_TCR_ORGN0_MASK 0x3UL
280 #define CTXDESC_CD_0_TCR_SH0_SHIFT 12
281 #define ARM64_TCR_SH0_SHIFT 12
282 #define ARM64_TCR_SH0_MASK 0x3UL
283 #define CTXDESC_CD_0_TCR_EPD0_SHIFT 14
284 #define ARM64_TCR_EPD0_SHIFT 7
285 #define ARM64_TCR_EPD0_MASK 0x1UL
286 #define CTXDESC_CD_0_TCR_EPD1_SHIFT 30
287 #define ARM64_TCR_EPD1_SHIFT 23
288 #define ARM64_TCR_EPD1_MASK 0x1UL
290 #define CTXDESC_CD_0_ENDI (1UL << 15)
291 #define CTXDESC_CD_0_V (1UL << 31)
293 #define CTXDESC_CD_0_TCR_IPS_SHIFT 32
294 #define ARM64_TCR_IPS_SHIFT 32
295 #define ARM64_TCR_IPS_MASK 0x7UL
296 #define CTXDESC_CD_0_TCR_TBI0_SHIFT 38
297 #define ARM64_TCR_TBI0_SHIFT 37
298 #define ARM64_TCR_TBI0_MASK 0x1UL
300 #define CTXDESC_CD_0_AA64 (1UL << 41)
301 #define CTXDESC_CD_0_R (1UL << 45)
302 #define CTXDESC_CD_0_A (1UL << 46)
303 #define CTXDESC_CD_0_ASET_SHIFT 47
304 #define CTXDESC_CD_0_ASET_SHARED (0UL << CTXDESC_CD_0_ASET_SHIFT)
305 #define CTXDESC_CD_0_ASET_PRIVATE (1UL << CTXDESC_CD_0_ASET_SHIFT)
306 #define CTXDESC_CD_0_ASID_SHIFT 48
307 #define CTXDESC_CD_0_ASID_MASK 0xffffUL
309 #define CTXDESC_CD_1_TTB0_SHIFT 4
310 #define CTXDESC_CD_1_TTB0_MASK 0xfffffffffffUL
312 #define CTXDESC_CD_3_MAIR_SHIFT 0
314 /* Convert between AArch64 (CPU) TCR format and SMMU CD format */
315 #define ARM_SMMU_TCR2CD(tcr, fld) \
316 (((tcr) >> ARM64_TCR_##fld##_SHIFT & ARM64_TCR_##fld##_MASK) \
317 << CTXDESC_CD_0_TCR_##fld##_SHIFT)
320 #define CMDQ_ENT_DWORDS 2
321 #define CMDQ_MAX_SZ_SHIFT 8
323 #define CMDQ_ERR_SHIFT 24
324 #define CMDQ_ERR_MASK 0x7f
325 #define CMDQ_ERR_CERROR_NONE_IDX 0
326 #define CMDQ_ERR_CERROR_ILL_IDX 1
327 #define CMDQ_ERR_CERROR_ABT_IDX 2
329 #define CMDQ_0_OP_SHIFT 0
330 #define CMDQ_0_OP_MASK 0xffUL
331 #define CMDQ_0_SSV (1UL << 11)
333 #define CMDQ_PREFETCH_0_SID_SHIFT 32
334 #define CMDQ_PREFETCH_1_SIZE_SHIFT 0
335 #define CMDQ_PREFETCH_1_ADDR_MASK ~0xfffUL
337 #define CMDQ_CFGI_0_SID_SHIFT 32
338 #define CMDQ_CFGI_0_SID_MASK 0xffffffffUL
339 #define CMDQ_CFGI_1_LEAF (1UL << 0)
340 #define CMDQ_CFGI_1_RANGE_SHIFT 0
341 #define CMDQ_CFGI_1_RANGE_MASK 0x1fUL
343 #define CMDQ_TLBI_0_VMID_SHIFT 32
344 #define CMDQ_TLBI_0_ASID_SHIFT 48
345 #define CMDQ_TLBI_1_LEAF (1UL << 0)
346 #define CMDQ_TLBI_1_ADDR_MASK ~0xfffUL
348 #define CMDQ_PRI_0_SSID_SHIFT 12
349 #define CMDQ_PRI_0_SSID_MASK 0xfffffUL
350 #define CMDQ_PRI_0_SID_SHIFT 32
351 #define CMDQ_PRI_0_SID_MASK 0xffffffffUL
352 #define CMDQ_PRI_1_GRPID_SHIFT 0
353 #define CMDQ_PRI_1_GRPID_MASK 0x1ffUL
354 #define CMDQ_PRI_1_RESP_SHIFT 12
355 #define CMDQ_PRI_1_RESP_DENY (0UL << CMDQ_PRI_1_RESP_SHIFT)
356 #define CMDQ_PRI_1_RESP_FAIL (1UL << CMDQ_PRI_1_RESP_SHIFT)
357 #define CMDQ_PRI_1_RESP_SUCC (2UL << CMDQ_PRI_1_RESP_SHIFT)
359 #define CMDQ_SYNC_0_CS_SHIFT 12
360 #define CMDQ_SYNC_0_CS_NONE (0UL << CMDQ_SYNC_0_CS_SHIFT)
361 #define CMDQ_SYNC_0_CS_SEV (2UL << CMDQ_SYNC_0_CS_SHIFT)
364 #define EVTQ_ENT_DWORDS 4
365 #define EVTQ_MAX_SZ_SHIFT 7
367 #define EVTQ_0_ID_SHIFT 0
368 #define EVTQ_0_ID_MASK 0xffUL
371 #define PRIQ_ENT_DWORDS 2
372 #define PRIQ_MAX_SZ_SHIFT 8
374 #define PRIQ_0_SID_SHIFT 0
375 #define PRIQ_0_SID_MASK 0xffffffffUL
376 #define PRIQ_0_SSID_SHIFT 32
377 #define PRIQ_0_SSID_MASK 0xfffffUL
378 #define PRIQ_0_OF (1UL << 57)
379 #define PRIQ_0_PERM_PRIV (1UL << 58)
380 #define PRIQ_0_PERM_EXEC (1UL << 59)
381 #define PRIQ_0_PERM_READ (1UL << 60)
382 #define PRIQ_0_PERM_WRITE (1UL << 61)
383 #define PRIQ_0_PRG_LAST (1UL << 62)
384 #define PRIQ_0_SSID_V (1UL << 63)
386 #define PRIQ_1_PRG_IDX_SHIFT 0
387 #define PRIQ_1_PRG_IDX_MASK 0x1ffUL
388 #define PRIQ_1_ADDR_SHIFT 12
389 #define PRIQ_1_ADDR_MASK 0xfffffffffffffUL
391 /* High-level queue structures */
392 #define ARM_SMMU_POLL_TIMEOUT_US 100
394 static bool disable_bypass
;
395 module_param_named(disable_bypass
, disable_bypass
, bool, S_IRUGO
);
396 MODULE_PARM_DESC(disable_bypass
,
397 "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
405 struct arm_smmu_cmdq_ent
{
408 bool substream_valid
;
410 /* Command-specific fields */
412 #define CMDQ_OP_PREFETCH_CFG 0x1
419 #define CMDQ_OP_CFGI_STE 0x3
420 #define CMDQ_OP_CFGI_ALL 0x4
429 #define CMDQ_OP_TLBI_NH_ASID 0x11
430 #define CMDQ_OP_TLBI_NH_VA 0x12
431 #define CMDQ_OP_TLBI_EL2_ALL 0x20
432 #define CMDQ_OP_TLBI_S12_VMALL 0x28
433 #define CMDQ_OP_TLBI_S2_IPA 0x2a
434 #define CMDQ_OP_TLBI_NSNH_ALL 0x30
442 #define CMDQ_OP_PRI_RESP 0x41
450 #define CMDQ_OP_CMD_SYNC 0x46
454 struct arm_smmu_queue
{
455 int irq
; /* Wired interrupt */
466 u32 __iomem
*prod_reg
;
467 u32 __iomem
*cons_reg
;
470 struct arm_smmu_cmdq
{
471 struct arm_smmu_queue q
;
475 struct arm_smmu_evtq
{
476 struct arm_smmu_queue q
;
480 struct arm_smmu_priq
{
481 struct arm_smmu_queue q
;
484 /* High-level stream table and context descriptor structures */
485 struct arm_smmu_strtab_l1_desc
{
489 dma_addr_t l2ptr_dma
;
492 struct arm_smmu_s1_cfg
{
494 dma_addr_t cdptr_dma
;
496 struct arm_smmu_ctx_desc
{
504 struct arm_smmu_s2_cfg
{
510 struct arm_smmu_strtab_ent
{
513 bool bypass
; /* Overrides s1/s2 config */
514 struct arm_smmu_s1_cfg
*s1_cfg
;
515 struct arm_smmu_s2_cfg
*s2_cfg
;
518 struct arm_smmu_strtab_cfg
{
520 dma_addr_t strtab_dma
;
521 struct arm_smmu_strtab_l1_desc
*l1_desc
;
522 unsigned int num_l1_ents
;
528 /* An SMMUv3 instance */
529 struct arm_smmu_device
{
533 #define ARM_SMMU_FEAT_2_LVL_STRTAB (1 << 0)
534 #define ARM_SMMU_FEAT_2_LVL_CDTAB (1 << 1)
535 #define ARM_SMMU_FEAT_TT_LE (1 << 2)
536 #define ARM_SMMU_FEAT_TT_BE (1 << 3)
537 #define ARM_SMMU_FEAT_PRI (1 << 4)
538 #define ARM_SMMU_FEAT_ATS (1 << 5)
539 #define ARM_SMMU_FEAT_SEV (1 << 6)
540 #define ARM_SMMU_FEAT_MSI (1 << 7)
541 #define ARM_SMMU_FEAT_COHERENCY (1 << 8)
542 #define ARM_SMMU_FEAT_TRANS_S1 (1 << 9)
543 #define ARM_SMMU_FEAT_TRANS_S2 (1 << 10)
544 #define ARM_SMMU_FEAT_STALLS (1 << 11)
545 #define ARM_SMMU_FEAT_HYP (1 << 12)
548 #define ARM_SMMU_OPT_SKIP_PREFETCH (1 << 0)
551 struct arm_smmu_cmdq cmdq
;
552 struct arm_smmu_evtq evtq
;
553 struct arm_smmu_priq priq
;
557 unsigned long ias
; /* IPA */
558 unsigned long oas
; /* PA */
560 #define ARM_SMMU_MAX_ASIDS (1 << 16)
561 unsigned int asid_bits
;
562 DECLARE_BITMAP(asid_map
, ARM_SMMU_MAX_ASIDS
);
564 #define ARM_SMMU_MAX_VMIDS (1 << 16)
565 unsigned int vmid_bits
;
566 DECLARE_BITMAP(vmid_map
, ARM_SMMU_MAX_VMIDS
);
568 unsigned int ssid_bits
;
569 unsigned int sid_bits
;
571 struct arm_smmu_strtab_cfg strtab_cfg
;
572 struct list_head list
;
575 /* SMMU private data for an IOMMU group */
576 struct arm_smmu_group
{
577 struct arm_smmu_device
*smmu
;
578 struct arm_smmu_domain
*domain
;
581 struct arm_smmu_strtab_ent ste
;
584 /* SMMU private data for an IOMMU domain */
585 enum arm_smmu_domain_stage
{
586 ARM_SMMU_DOMAIN_S1
= 0,
588 ARM_SMMU_DOMAIN_NESTED
,
591 struct arm_smmu_domain
{
592 struct arm_smmu_device
*smmu
;
593 struct mutex init_mutex
; /* Protects smmu pointer */
595 struct io_pgtable_ops
*pgtbl_ops
;
596 spinlock_t pgtbl_lock
;
598 enum arm_smmu_domain_stage stage
;
600 struct arm_smmu_s1_cfg s1_cfg
;
601 struct arm_smmu_s2_cfg s2_cfg
;
604 struct iommu_domain domain
;
607 /* Our list of SMMU instances */
608 static DEFINE_SPINLOCK(arm_smmu_devices_lock
);
609 static LIST_HEAD(arm_smmu_devices
);
611 struct arm_smmu_option_prop
{
616 static struct arm_smmu_option_prop arm_smmu_options
[] = {
617 { ARM_SMMU_OPT_SKIP_PREFETCH
, "hisilicon,broken-prefetch-cmd" },
621 static struct arm_smmu_domain
*to_smmu_domain(struct iommu_domain
*dom
)
623 return container_of(dom
, struct arm_smmu_domain
, domain
);
626 static void parse_driver_options(struct arm_smmu_device
*smmu
)
631 if (of_property_read_bool(smmu
->dev
->of_node
,
632 arm_smmu_options
[i
].prop
)) {
633 smmu
->options
|= arm_smmu_options
[i
].opt
;
634 dev_notice(smmu
->dev
, "option %s\n",
635 arm_smmu_options
[i
].prop
);
637 } while (arm_smmu_options
[++i
].opt
);
640 /* Low-level queue manipulation functions */
641 static bool queue_full(struct arm_smmu_queue
*q
)
643 return Q_IDX(q
, q
->prod
) == Q_IDX(q
, q
->cons
) &&
644 Q_WRP(q
, q
->prod
) != Q_WRP(q
, q
->cons
);
647 static bool queue_empty(struct arm_smmu_queue
*q
)
649 return Q_IDX(q
, q
->prod
) == Q_IDX(q
, q
->cons
) &&
650 Q_WRP(q
, q
->prod
) == Q_WRP(q
, q
->cons
);
653 static void queue_sync_cons(struct arm_smmu_queue
*q
)
655 q
->cons
= readl_relaxed(q
->cons_reg
);
658 static void queue_inc_cons(struct arm_smmu_queue
*q
)
660 u32 cons
= (Q_WRP(q
, q
->cons
) | Q_IDX(q
, q
->cons
)) + 1;
662 q
->cons
= Q_OVF(q
, q
->cons
) | Q_WRP(q
, cons
) | Q_IDX(q
, cons
);
663 writel(q
->cons
, q
->cons_reg
);
666 static int queue_sync_prod(struct arm_smmu_queue
*q
)
669 u32 prod
= readl_relaxed(q
->prod_reg
);
671 if (Q_OVF(q
, prod
) != Q_OVF(q
, q
->prod
))
678 static void queue_inc_prod(struct arm_smmu_queue
*q
)
680 u32 prod
= (Q_WRP(q
, q
->prod
) | Q_IDX(q
, q
->prod
)) + 1;
682 q
->prod
= Q_OVF(q
, q
->prod
) | Q_WRP(q
, prod
) | Q_IDX(q
, prod
);
683 writel(q
->prod
, q
->prod_reg
);
686 static bool __queue_cons_before(struct arm_smmu_queue
*q
, u32 until
)
688 if (Q_WRP(q
, q
->cons
) == Q_WRP(q
, until
))
689 return Q_IDX(q
, q
->cons
) < Q_IDX(q
, until
);
691 return Q_IDX(q
, q
->cons
) >= Q_IDX(q
, until
);
694 static int queue_poll_cons(struct arm_smmu_queue
*q
, u32 until
, bool wfe
)
696 ktime_t timeout
= ktime_add_us(ktime_get(), ARM_SMMU_POLL_TIMEOUT_US
);
698 while (queue_sync_cons(q
), __queue_cons_before(q
, until
)) {
699 if (ktime_compare(ktime_get(), timeout
) > 0)
713 static void queue_write(__le64
*dst
, u64
*src
, size_t n_dwords
)
717 for (i
= 0; i
< n_dwords
; ++i
)
718 *dst
++ = cpu_to_le64(*src
++);
721 static int queue_insert_raw(struct arm_smmu_queue
*q
, u64
*ent
)
726 queue_write(Q_ENT(q
, q
->prod
), ent
, q
->ent_dwords
);
731 static void queue_read(__le64
*dst
, u64
*src
, size_t n_dwords
)
735 for (i
= 0; i
< n_dwords
; ++i
)
736 *dst
++ = le64_to_cpu(*src
++);
739 static int queue_remove_raw(struct arm_smmu_queue
*q
, u64
*ent
)
744 queue_read(ent
, Q_ENT(q
, q
->cons
), q
->ent_dwords
);
749 /* High-level queue accessors */
750 static int arm_smmu_cmdq_build_cmd(u64
*cmd
, struct arm_smmu_cmdq_ent
*ent
)
752 memset(cmd
, 0, CMDQ_ENT_DWORDS
<< 3);
753 cmd
[0] |= (ent
->opcode
& CMDQ_0_OP_MASK
) << CMDQ_0_OP_SHIFT
;
755 switch (ent
->opcode
) {
756 case CMDQ_OP_TLBI_EL2_ALL
:
757 case CMDQ_OP_TLBI_NSNH_ALL
:
759 case CMDQ_OP_PREFETCH_CFG
:
760 cmd
[0] |= (u64
)ent
->prefetch
.sid
<< CMDQ_PREFETCH_0_SID_SHIFT
;
761 cmd
[1] |= ent
->prefetch
.size
<< CMDQ_PREFETCH_1_SIZE_SHIFT
;
762 cmd
[1] |= ent
->prefetch
.addr
& CMDQ_PREFETCH_1_ADDR_MASK
;
764 case CMDQ_OP_CFGI_STE
:
765 cmd
[0] |= (u64
)ent
->cfgi
.sid
<< CMDQ_CFGI_0_SID_SHIFT
;
766 cmd
[1] |= ent
->cfgi
.leaf
? CMDQ_CFGI_1_LEAF
: 0;
768 case CMDQ_OP_CFGI_ALL
:
769 /* Cover the entire SID range */
770 cmd
[1] |= CMDQ_CFGI_1_RANGE_MASK
<< CMDQ_CFGI_1_RANGE_SHIFT
;
772 case CMDQ_OP_TLBI_NH_VA
:
773 cmd
[0] |= (u64
)ent
->tlbi
.asid
<< CMDQ_TLBI_0_ASID_SHIFT
;
775 case CMDQ_OP_TLBI_S2_IPA
:
776 cmd
[0] |= (u64
)ent
->tlbi
.vmid
<< CMDQ_TLBI_0_VMID_SHIFT
;
777 cmd
[1] |= ent
->tlbi
.leaf
? CMDQ_TLBI_1_LEAF
: 0;
778 cmd
[1] |= ent
->tlbi
.addr
& CMDQ_TLBI_1_ADDR_MASK
;
780 case CMDQ_OP_TLBI_NH_ASID
:
781 cmd
[0] |= (u64
)ent
->tlbi
.asid
<< CMDQ_TLBI_0_ASID_SHIFT
;
783 case CMDQ_OP_TLBI_S12_VMALL
:
784 cmd
[0] |= (u64
)ent
->tlbi
.vmid
<< CMDQ_TLBI_0_VMID_SHIFT
;
786 case CMDQ_OP_PRI_RESP
:
787 cmd
[0] |= ent
->substream_valid
? CMDQ_0_SSV
: 0;
788 cmd
[0] |= ent
->pri
.ssid
<< CMDQ_PRI_0_SSID_SHIFT
;
789 cmd
[0] |= (u64
)ent
->pri
.sid
<< CMDQ_PRI_0_SID_SHIFT
;
790 cmd
[1] |= ent
->pri
.grpid
<< CMDQ_PRI_1_GRPID_SHIFT
;
791 switch (ent
->pri
.resp
) {
793 cmd
[1] |= CMDQ_PRI_1_RESP_DENY
;
796 cmd
[1] |= CMDQ_PRI_1_RESP_FAIL
;
799 cmd
[1] |= CMDQ_PRI_1_RESP_SUCC
;
805 case CMDQ_OP_CMD_SYNC
:
806 cmd
[0] |= CMDQ_SYNC_0_CS_SEV
;
815 static void arm_smmu_cmdq_skip_err(struct arm_smmu_device
*smmu
)
817 static const char *cerror_str
[] = {
818 [CMDQ_ERR_CERROR_NONE_IDX
] = "No error",
819 [CMDQ_ERR_CERROR_ILL_IDX
] = "Illegal command",
820 [CMDQ_ERR_CERROR_ABT_IDX
] = "Abort on command fetch",
824 u64 cmd
[CMDQ_ENT_DWORDS
];
825 struct arm_smmu_queue
*q
= &smmu
->cmdq
.q
;
826 u32 cons
= readl_relaxed(q
->cons_reg
);
827 u32 idx
= cons
>> CMDQ_ERR_SHIFT
& CMDQ_ERR_MASK
;
828 struct arm_smmu_cmdq_ent cmd_sync
= {
829 .opcode
= CMDQ_OP_CMD_SYNC
,
832 dev_err(smmu
->dev
, "CMDQ error (cons 0x%08x): %s\n", cons
,
836 case CMDQ_ERR_CERROR_ILL_IDX
:
838 case CMDQ_ERR_CERROR_ABT_IDX
:
839 dev_err(smmu
->dev
, "retrying command fetch\n");
840 case CMDQ_ERR_CERROR_NONE_IDX
:
845 * We may have concurrent producers, so we need to be careful
846 * not to touch any of the shadow cmdq state.
848 queue_read(cmd
, Q_ENT(q
, idx
), q
->ent_dwords
);
849 dev_err(smmu
->dev
, "skipping command in error state:\n");
850 for (i
= 0; i
< ARRAY_SIZE(cmd
); ++i
)
851 dev_err(smmu
->dev
, "\t0x%016llx\n", (unsigned long long)cmd
[i
]);
853 /* Convert the erroneous command into a CMD_SYNC */
854 if (arm_smmu_cmdq_build_cmd(cmd
, &cmd_sync
)) {
855 dev_err(smmu
->dev
, "failed to convert to CMD_SYNC\n");
859 queue_write(cmd
, Q_ENT(q
, idx
), q
->ent_dwords
);
862 static void arm_smmu_cmdq_issue_cmd(struct arm_smmu_device
*smmu
,
863 struct arm_smmu_cmdq_ent
*ent
)
866 u64 cmd
[CMDQ_ENT_DWORDS
];
867 bool wfe
= !!(smmu
->features
& ARM_SMMU_FEAT_SEV
);
868 struct arm_smmu_queue
*q
= &smmu
->cmdq
.q
;
870 if (arm_smmu_cmdq_build_cmd(cmd
, ent
)) {
871 dev_warn(smmu
->dev
, "ignoring unknown CMDQ opcode 0x%x\n",
876 spin_lock(&smmu
->cmdq
.lock
);
877 while (until
= q
->prod
+ 1, queue_insert_raw(q
, cmd
) == -ENOSPC
) {
879 * Keep the queue locked, otherwise the producer could wrap
880 * twice and we could see a future consumer pointer that looks
881 * like it's behind us.
883 if (queue_poll_cons(q
, until
, wfe
))
884 dev_err_ratelimited(smmu
->dev
, "CMDQ timeout\n");
887 if (ent
->opcode
== CMDQ_OP_CMD_SYNC
&& queue_poll_cons(q
, until
, wfe
))
888 dev_err_ratelimited(smmu
->dev
, "CMD_SYNC timeout\n");
889 spin_unlock(&smmu
->cmdq
.lock
);
892 /* Context descriptor manipulation functions */
893 static u64
arm_smmu_cpu_tcr_to_cd(u64 tcr
)
897 /* Repack the TCR. Just care about TTBR0 for now */
898 val
|= ARM_SMMU_TCR2CD(tcr
, T0SZ
);
899 val
|= ARM_SMMU_TCR2CD(tcr
, TG0
);
900 val
|= ARM_SMMU_TCR2CD(tcr
, IRGN0
);
901 val
|= ARM_SMMU_TCR2CD(tcr
, ORGN0
);
902 val
|= ARM_SMMU_TCR2CD(tcr
, SH0
);
903 val
|= ARM_SMMU_TCR2CD(tcr
, EPD0
);
904 val
|= ARM_SMMU_TCR2CD(tcr
, EPD1
);
905 val
|= ARM_SMMU_TCR2CD(tcr
, IPS
);
906 val
|= ARM_SMMU_TCR2CD(tcr
, TBI0
);
911 static void arm_smmu_write_ctx_desc(struct arm_smmu_device
*smmu
,
912 struct arm_smmu_s1_cfg
*cfg
)
917 * We don't need to issue any invalidation here, as we'll invalidate
918 * the STE when installing the new entry anyway.
920 val
= arm_smmu_cpu_tcr_to_cd(cfg
->cd
.tcr
) |
924 CTXDESC_CD_0_R
| CTXDESC_CD_0_A
| CTXDESC_CD_0_ASET_PRIVATE
|
925 CTXDESC_CD_0_AA64
| (u64
)cfg
->cd
.asid
<< CTXDESC_CD_0_ASID_SHIFT
|
927 cfg
->cdptr
[0] = cpu_to_le64(val
);
929 val
= cfg
->cd
.ttbr
& CTXDESC_CD_1_TTB0_MASK
<< CTXDESC_CD_1_TTB0_SHIFT
;
930 cfg
->cdptr
[1] = cpu_to_le64(val
);
932 cfg
->cdptr
[3] = cpu_to_le64(cfg
->cd
.mair
<< CTXDESC_CD_3_MAIR_SHIFT
);
935 /* Stream table manipulation functions */
937 arm_smmu_write_strtab_l1_desc(__le64
*dst
, struct arm_smmu_strtab_l1_desc
*desc
)
941 val
|= (desc
->span
& STRTAB_L1_DESC_SPAN_MASK
)
942 << STRTAB_L1_DESC_SPAN_SHIFT
;
943 val
|= desc
->l2ptr_dma
&
944 STRTAB_L1_DESC_L2PTR_MASK
<< STRTAB_L1_DESC_L2PTR_SHIFT
;
946 *dst
= cpu_to_le64(val
);
949 static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device
*smmu
, u32 sid
)
951 struct arm_smmu_cmdq_ent cmd
= {
952 .opcode
= CMDQ_OP_CFGI_STE
,
959 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
960 cmd
.opcode
= CMDQ_OP_CMD_SYNC
;
961 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
964 static void arm_smmu_write_strtab_ent(struct arm_smmu_device
*smmu
, u32 sid
,
965 __le64
*dst
, struct arm_smmu_strtab_ent
*ste
)
968 * This is hideously complicated, but we only really care about
969 * three cases at the moment:
971 * 1. Invalid (all zero) -> bypass (init)
972 * 2. Bypass -> translation (attach)
973 * 3. Translation -> bypass (detach)
975 * Given that we can't update the STE atomically and the SMMU
976 * doesn't read the thing in a defined order, that leaves us
977 * with the following maintenance requirements:
979 * 1. Update Config, return (init time STEs aren't live)
980 * 2. Write everything apart from dword 0, sync, write dword 0, sync
981 * 3. Update Config, sync
983 u64 val
= le64_to_cpu(dst
[0]);
984 bool ste_live
= false;
985 struct arm_smmu_cmdq_ent prefetch_cmd
= {
986 .opcode
= CMDQ_OP_PREFETCH_CFG
,
992 if (val
& STRTAB_STE_0_V
) {
995 cfg
= val
& STRTAB_STE_0_CFG_MASK
<< STRTAB_STE_0_CFG_SHIFT
;
997 case STRTAB_STE_0_CFG_BYPASS
:
999 case STRTAB_STE_0_CFG_S1_TRANS
:
1000 case STRTAB_STE_0_CFG_S2_TRANS
:
1004 BUG(); /* STE corruption */
1008 /* Nuke the existing Config, as we're going to rewrite it */
1009 val
&= ~(STRTAB_STE_0_CFG_MASK
<< STRTAB_STE_0_CFG_SHIFT
);
1012 val
|= STRTAB_STE_0_V
;
1014 val
&= ~STRTAB_STE_0_V
;
1017 val
|= disable_bypass
? STRTAB_STE_0_CFG_ABORT
1018 : STRTAB_STE_0_CFG_BYPASS
;
1019 dst
[0] = cpu_to_le64(val
);
1020 dst
[2] = 0; /* Nuke the VMID */
1022 arm_smmu_sync_ste_for_sid(smmu
, sid
);
1028 dst
[1] = cpu_to_le64(
1029 STRTAB_STE_1_S1C_CACHE_WBRA
1030 << STRTAB_STE_1_S1CIR_SHIFT
|
1031 STRTAB_STE_1_S1C_CACHE_WBRA
1032 << STRTAB_STE_1_S1COR_SHIFT
|
1033 STRTAB_STE_1_S1C_SH_ISH
<< STRTAB_STE_1_S1CSH_SHIFT
|
1034 STRTAB_STE_1_S1STALLD
|
1035 #ifdef CONFIG_PCI_ATS
1036 STRTAB_STE_1_EATS_TRANS
<< STRTAB_STE_1_EATS_SHIFT
|
1038 STRTAB_STE_1_STRW_NSEL1
<< STRTAB_STE_1_STRW_SHIFT
);
1040 val
|= (ste
->s1_cfg
->cdptr_dma
& STRTAB_STE_0_S1CTXPTR_MASK
1041 << STRTAB_STE_0_S1CTXPTR_SHIFT
) |
1042 STRTAB_STE_0_CFG_S1_TRANS
;
1048 dst
[2] = cpu_to_le64(
1049 ste
->s2_cfg
->vmid
<< STRTAB_STE_2_S2VMID_SHIFT
|
1050 (ste
->s2_cfg
->vtcr
& STRTAB_STE_2_VTCR_MASK
)
1051 << STRTAB_STE_2_VTCR_SHIFT
|
1053 STRTAB_STE_2_S2ENDI
|
1055 STRTAB_STE_2_S2PTW
| STRTAB_STE_2_S2AA64
|
1058 dst
[3] = cpu_to_le64(ste
->s2_cfg
->vttbr
&
1059 STRTAB_STE_3_S2TTB_MASK
<< STRTAB_STE_3_S2TTB_SHIFT
);
1061 val
|= STRTAB_STE_0_CFG_S2_TRANS
;
1064 arm_smmu_sync_ste_for_sid(smmu
, sid
);
1065 dst
[0] = cpu_to_le64(val
);
1066 arm_smmu_sync_ste_for_sid(smmu
, sid
);
1068 /* It's likely that we'll want to use the new STE soon */
1069 if (!(smmu
->options
& ARM_SMMU_OPT_SKIP_PREFETCH
))
1070 arm_smmu_cmdq_issue_cmd(smmu
, &prefetch_cmd
);
1073 static void arm_smmu_init_bypass_stes(u64
*strtab
, unsigned int nent
)
1076 struct arm_smmu_strtab_ent ste
= {
1081 for (i
= 0; i
< nent
; ++i
) {
1082 arm_smmu_write_strtab_ent(NULL
, -1, strtab
, &ste
);
1083 strtab
+= STRTAB_STE_DWORDS
;
1087 static int arm_smmu_init_l2_strtab(struct arm_smmu_device
*smmu
, u32 sid
)
1091 struct arm_smmu_strtab_cfg
*cfg
= &smmu
->strtab_cfg
;
1092 struct arm_smmu_strtab_l1_desc
*desc
= &cfg
->l1_desc
[sid
>> STRTAB_SPLIT
];
1097 size
= 1 << (STRTAB_SPLIT
+ ilog2(STRTAB_STE_DWORDS
) + 3);
1098 strtab
= &cfg
->strtab
[(sid
>> STRTAB_SPLIT
) * STRTAB_L1_DESC_DWORDS
];
1100 desc
->span
= STRTAB_SPLIT
+ 1;
1101 desc
->l2ptr
= dma_zalloc_coherent(smmu
->dev
, size
, &desc
->l2ptr_dma
,
1105 "failed to allocate l2 stream table for SID %u\n",
1110 arm_smmu_init_bypass_stes(desc
->l2ptr
, 1 << STRTAB_SPLIT
);
1111 arm_smmu_write_strtab_l1_desc(strtab
, desc
);
1115 /* IRQ and event handlers */
1116 static irqreturn_t
arm_smmu_evtq_thread(int irq
, void *dev
)
1119 struct arm_smmu_device
*smmu
= dev
;
1120 struct arm_smmu_queue
*q
= &smmu
->evtq
.q
;
1121 u64 evt
[EVTQ_ENT_DWORDS
];
1123 while (!queue_remove_raw(q
, evt
)) {
1124 u8 id
= evt
[0] >> EVTQ_0_ID_SHIFT
& EVTQ_0_ID_MASK
;
1126 dev_info(smmu
->dev
, "event 0x%02x received:\n", id
);
1127 for (i
= 0; i
< ARRAY_SIZE(evt
); ++i
)
1128 dev_info(smmu
->dev
, "\t0x%016llx\n",
1129 (unsigned long long)evt
[i
]);
1132 /* Sync our overflow flag, as we believe we're up to speed */
1133 q
->cons
= Q_OVF(q
, q
->prod
) | Q_WRP(q
, q
->cons
) | Q_IDX(q
, q
->cons
);
1137 static irqreturn_t
arm_smmu_evtq_handler(int irq
, void *dev
)
1139 irqreturn_t ret
= IRQ_WAKE_THREAD
;
1140 struct arm_smmu_device
*smmu
= dev
;
1141 struct arm_smmu_queue
*q
= &smmu
->evtq
.q
;
1144 * Not much we can do on overflow, so scream and pretend we're
1147 if (queue_sync_prod(q
) == -EOVERFLOW
)
1148 dev_err(smmu
->dev
, "EVTQ overflow detected -- events lost\n");
1149 else if (queue_empty(q
))
1155 static irqreturn_t
arm_smmu_priq_thread(int irq
, void *dev
)
1157 struct arm_smmu_device
*smmu
= dev
;
1158 struct arm_smmu_queue
*q
= &smmu
->priq
.q
;
1159 u64 evt
[PRIQ_ENT_DWORDS
];
1161 while (!queue_remove_raw(q
, evt
)) {
1166 sid
= evt
[0] >> PRIQ_0_SID_SHIFT
& PRIQ_0_SID_MASK
;
1167 ssv
= evt
[0] & PRIQ_0_SSID_V
;
1168 ssid
= ssv
? evt
[0] >> PRIQ_0_SSID_SHIFT
& PRIQ_0_SSID_MASK
: 0;
1169 last
= evt
[0] & PRIQ_0_PRG_LAST
;
1170 grpid
= evt
[1] >> PRIQ_1_PRG_IDX_SHIFT
& PRIQ_1_PRG_IDX_MASK
;
1172 dev_info(smmu
->dev
, "unexpected PRI request received:\n");
1174 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
1175 sid
, ssid
, grpid
, last
? "L" : "",
1176 evt
[0] & PRIQ_0_PERM_PRIV
? "" : "un",
1177 evt
[0] & PRIQ_0_PERM_READ
? "R" : "",
1178 evt
[0] & PRIQ_0_PERM_WRITE
? "W" : "",
1179 evt
[0] & PRIQ_0_PERM_EXEC
? "X" : "",
1180 evt
[1] & PRIQ_1_ADDR_MASK
<< PRIQ_1_ADDR_SHIFT
);
1183 struct arm_smmu_cmdq_ent cmd
= {
1184 .opcode
= CMDQ_OP_PRI_RESP
,
1185 .substream_valid
= ssv
,
1190 .resp
= PRI_RESP_DENY
,
1194 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
1198 /* Sync our overflow flag, as we believe we're up to speed */
1199 q
->cons
= Q_OVF(q
, q
->prod
) | Q_WRP(q
, q
->cons
) | Q_IDX(q
, q
->cons
);
1203 static irqreturn_t
arm_smmu_priq_handler(int irq
, void *dev
)
1205 irqreturn_t ret
= IRQ_WAKE_THREAD
;
1206 struct arm_smmu_device
*smmu
= dev
;
1207 struct arm_smmu_queue
*q
= &smmu
->priq
.q
;
1209 /* PRIQ overflow indicates a programming error */
1210 if (queue_sync_prod(q
) == -EOVERFLOW
)
1211 dev_err(smmu
->dev
, "PRIQ overflow detected -- requests lost\n");
1212 else if (queue_empty(q
))
1218 static irqreturn_t
arm_smmu_cmdq_sync_handler(int irq
, void *dev
)
1220 /* We don't actually use CMD_SYNC interrupts for anything */
1224 static int arm_smmu_device_disable(struct arm_smmu_device
*smmu
);
1226 static irqreturn_t
arm_smmu_gerror_handler(int irq
, void *dev
)
1228 u32 gerror
, gerrorn
;
1229 struct arm_smmu_device
*smmu
= dev
;
1231 gerror
= readl_relaxed(smmu
->base
+ ARM_SMMU_GERROR
);
1232 gerrorn
= readl_relaxed(smmu
->base
+ ARM_SMMU_GERRORN
);
1235 if (!(gerror
& GERROR_ERR_MASK
))
1236 return IRQ_NONE
; /* No errors pending */
1239 "unexpected global error reported (0x%08x), this could be serious\n",
1242 if (gerror
& GERROR_SFM_ERR
) {
1243 dev_err(smmu
->dev
, "device has entered Service Failure Mode!\n");
1244 arm_smmu_device_disable(smmu
);
1247 if (gerror
& GERROR_MSI_GERROR_ABT_ERR
)
1248 dev_warn(smmu
->dev
, "GERROR MSI write aborted\n");
1250 if (gerror
& GERROR_MSI_PRIQ_ABT_ERR
) {
1251 dev_warn(smmu
->dev
, "PRIQ MSI write aborted\n");
1252 arm_smmu_priq_handler(irq
, smmu
->dev
);
1255 if (gerror
& GERROR_MSI_EVTQ_ABT_ERR
) {
1256 dev_warn(smmu
->dev
, "EVTQ MSI write aborted\n");
1257 arm_smmu_evtq_handler(irq
, smmu
->dev
);
1260 if (gerror
& GERROR_MSI_CMDQ_ABT_ERR
) {
1261 dev_warn(smmu
->dev
, "CMDQ MSI write aborted\n");
1262 arm_smmu_cmdq_sync_handler(irq
, smmu
->dev
);
1265 if (gerror
& GERROR_PRIQ_ABT_ERR
)
1266 dev_err(smmu
->dev
, "PRIQ write aborted -- events may have been lost\n");
1268 if (gerror
& GERROR_EVTQ_ABT_ERR
)
1269 dev_err(smmu
->dev
, "EVTQ write aborted -- events may have been lost\n");
1271 if (gerror
& GERROR_CMDQ_ERR
)
1272 arm_smmu_cmdq_skip_err(smmu
);
1274 writel(gerror
, smmu
->base
+ ARM_SMMU_GERRORN
);
1278 /* IO_PGTABLE API */
1279 static void __arm_smmu_tlb_sync(struct arm_smmu_device
*smmu
)
1281 struct arm_smmu_cmdq_ent cmd
;
1283 cmd
.opcode
= CMDQ_OP_CMD_SYNC
;
1284 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
1287 static void arm_smmu_tlb_sync(void *cookie
)
1289 struct arm_smmu_domain
*smmu_domain
= cookie
;
1290 __arm_smmu_tlb_sync(smmu_domain
->smmu
);
1293 static void arm_smmu_tlb_inv_context(void *cookie
)
1295 struct arm_smmu_domain
*smmu_domain
= cookie
;
1296 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1297 struct arm_smmu_cmdq_ent cmd
;
1299 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
) {
1300 cmd
.opcode
= CMDQ_OP_TLBI_NH_ASID
;
1301 cmd
.tlbi
.asid
= smmu_domain
->s1_cfg
.cd
.asid
;
1304 cmd
.opcode
= CMDQ_OP_TLBI_S12_VMALL
;
1305 cmd
.tlbi
.vmid
= smmu_domain
->s2_cfg
.vmid
;
1308 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
1309 __arm_smmu_tlb_sync(smmu
);
1312 static void arm_smmu_tlb_inv_range_nosync(unsigned long iova
, size_t size
,
1313 bool leaf
, void *cookie
)
1315 struct arm_smmu_domain
*smmu_domain
= cookie
;
1316 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1317 struct arm_smmu_cmdq_ent cmd
= {
1324 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
) {
1325 cmd
.opcode
= CMDQ_OP_TLBI_NH_VA
;
1326 cmd
.tlbi
.asid
= smmu_domain
->s1_cfg
.cd
.asid
;
1328 cmd
.opcode
= CMDQ_OP_TLBI_S2_IPA
;
1329 cmd
.tlbi
.vmid
= smmu_domain
->s2_cfg
.vmid
;
1332 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
1335 static struct iommu_gather_ops arm_smmu_gather_ops
= {
1336 .tlb_flush_all
= arm_smmu_tlb_inv_context
,
1337 .tlb_add_flush
= arm_smmu_tlb_inv_range_nosync
,
1338 .tlb_sync
= arm_smmu_tlb_sync
,
1342 static bool arm_smmu_capable(enum iommu_cap cap
)
1345 case IOMMU_CAP_CACHE_COHERENCY
:
1347 case IOMMU_CAP_INTR_REMAP
:
1348 return true; /* MSIs are just memory writes */
1349 case IOMMU_CAP_NOEXEC
:
1356 static struct iommu_domain
*arm_smmu_domain_alloc(unsigned type
)
1358 struct arm_smmu_domain
*smmu_domain
;
1360 if (type
!= IOMMU_DOMAIN_UNMANAGED
)
1364 * Allocate the domain and initialise some of its data structures.
1365 * We can't really do anything meaningful until we've added a
1368 smmu_domain
= kzalloc(sizeof(*smmu_domain
), GFP_KERNEL
);
1372 mutex_init(&smmu_domain
->init_mutex
);
1373 spin_lock_init(&smmu_domain
->pgtbl_lock
);
1374 return &smmu_domain
->domain
;
1377 static int arm_smmu_bitmap_alloc(unsigned long *map
, int span
)
1379 int idx
, size
= 1 << span
;
1382 idx
= find_first_zero_bit(map
, size
);
1385 } while (test_and_set_bit(idx
, map
));
1390 static void arm_smmu_bitmap_free(unsigned long *map
, int idx
)
1392 clear_bit(idx
, map
);
1395 static void arm_smmu_domain_free(struct iommu_domain
*domain
)
1397 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1398 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1400 free_io_pgtable_ops(smmu_domain
->pgtbl_ops
);
1402 /* Free the CD and ASID, if we allocated them */
1403 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
) {
1404 struct arm_smmu_s1_cfg
*cfg
= &smmu_domain
->s1_cfg
;
1407 dma_free_coherent(smmu_domain
->smmu
->dev
,
1408 CTXDESC_CD_DWORDS
<< 3,
1412 arm_smmu_bitmap_free(smmu
->asid_map
, cfg
->cd
.asid
);
1415 struct arm_smmu_s2_cfg
*cfg
= &smmu_domain
->s2_cfg
;
1417 arm_smmu_bitmap_free(smmu
->vmid_map
, cfg
->vmid
);
1423 static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain
*smmu_domain
,
1424 struct io_pgtable_cfg
*pgtbl_cfg
)
1428 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1429 struct arm_smmu_s1_cfg
*cfg
= &smmu_domain
->s1_cfg
;
1431 asid
= arm_smmu_bitmap_alloc(smmu
->asid_map
, smmu
->asid_bits
);
1432 if (IS_ERR_VALUE(asid
))
1435 cfg
->cdptr
= dma_zalloc_coherent(smmu
->dev
, CTXDESC_CD_DWORDS
<< 3,
1436 &cfg
->cdptr_dma
, GFP_KERNEL
);
1438 dev_warn(smmu
->dev
, "failed to allocate context descriptor\n");
1442 cfg
->cd
.asid
= asid
;
1443 cfg
->cd
.ttbr
= pgtbl_cfg
->arm_lpae_s1_cfg
.ttbr
[0];
1444 cfg
->cd
.tcr
= pgtbl_cfg
->arm_lpae_s1_cfg
.tcr
;
1445 cfg
->cd
.mair
= pgtbl_cfg
->arm_lpae_s1_cfg
.mair
[0];
1449 arm_smmu_bitmap_free(smmu
->asid_map
, asid
);
1453 static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain
*smmu_domain
,
1454 struct io_pgtable_cfg
*pgtbl_cfg
)
1457 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1458 struct arm_smmu_s2_cfg
*cfg
= &smmu_domain
->s2_cfg
;
1460 vmid
= arm_smmu_bitmap_alloc(smmu
->vmid_map
, smmu
->vmid_bits
);
1461 if (IS_ERR_VALUE(vmid
))
1465 cfg
->vttbr
= pgtbl_cfg
->arm_lpae_s2_cfg
.vttbr
;
1466 cfg
->vtcr
= pgtbl_cfg
->arm_lpae_s2_cfg
.vtcr
;
1470 static struct iommu_ops arm_smmu_ops
;
1472 static int arm_smmu_domain_finalise(struct iommu_domain
*domain
)
1475 unsigned long ias
, oas
;
1476 enum io_pgtable_fmt fmt
;
1477 struct io_pgtable_cfg pgtbl_cfg
;
1478 struct io_pgtable_ops
*pgtbl_ops
;
1479 int (*finalise_stage_fn
)(struct arm_smmu_domain
*,
1480 struct io_pgtable_cfg
*);
1481 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1482 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1484 /* Restrict the stage to what we can actually support */
1485 if (!(smmu
->features
& ARM_SMMU_FEAT_TRANS_S1
))
1486 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S2
;
1487 if (!(smmu
->features
& ARM_SMMU_FEAT_TRANS_S2
))
1488 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S1
;
1490 switch (smmu_domain
->stage
) {
1491 case ARM_SMMU_DOMAIN_S1
:
1494 fmt
= ARM_64_LPAE_S1
;
1495 finalise_stage_fn
= arm_smmu_domain_finalise_s1
;
1497 case ARM_SMMU_DOMAIN_NESTED
:
1498 case ARM_SMMU_DOMAIN_S2
:
1501 fmt
= ARM_64_LPAE_S2
;
1502 finalise_stage_fn
= arm_smmu_domain_finalise_s2
;
1508 pgtbl_cfg
= (struct io_pgtable_cfg
) {
1509 .pgsize_bitmap
= arm_smmu_ops
.pgsize_bitmap
,
1512 .tlb
= &arm_smmu_gather_ops
,
1513 .iommu_dev
= smmu
->dev
,
1516 pgtbl_ops
= alloc_io_pgtable_ops(fmt
, &pgtbl_cfg
, smmu_domain
);
1520 arm_smmu_ops
.pgsize_bitmap
= pgtbl_cfg
.pgsize_bitmap
;
1521 smmu_domain
->pgtbl_ops
= pgtbl_ops
;
1523 ret
= finalise_stage_fn(smmu_domain
, &pgtbl_cfg
);
1524 if (IS_ERR_VALUE(ret
))
1525 free_io_pgtable_ops(pgtbl_ops
);
1530 static struct arm_smmu_group
*arm_smmu_group_get(struct device
*dev
)
1532 struct iommu_group
*group
;
1533 struct arm_smmu_group
*smmu_group
;
1535 group
= iommu_group_get(dev
);
1539 smmu_group
= iommu_group_get_iommudata(group
);
1540 iommu_group_put(group
);
1544 static __le64
*arm_smmu_get_step_for_sid(struct arm_smmu_device
*smmu
, u32 sid
)
1547 struct arm_smmu_strtab_cfg
*cfg
= &smmu
->strtab_cfg
;
1549 if (smmu
->features
& ARM_SMMU_FEAT_2_LVL_STRTAB
) {
1550 struct arm_smmu_strtab_l1_desc
*l1_desc
;
1553 /* Two-level walk */
1554 idx
= (sid
>> STRTAB_SPLIT
) * STRTAB_L1_DESC_DWORDS
;
1555 l1_desc
= &cfg
->l1_desc
[idx
];
1556 idx
= (sid
& ((1 << STRTAB_SPLIT
) - 1)) * STRTAB_STE_DWORDS
;
1557 step
= &l1_desc
->l2ptr
[idx
];
1559 /* Simple linear lookup */
1560 step
= &cfg
->strtab
[sid
* STRTAB_STE_DWORDS
];
1566 static int arm_smmu_install_ste_for_group(struct arm_smmu_group
*smmu_group
)
1569 struct arm_smmu_domain
*smmu_domain
= smmu_group
->domain
;
1570 struct arm_smmu_strtab_ent
*ste
= &smmu_group
->ste
;
1571 struct arm_smmu_device
*smmu
= smmu_group
->smmu
;
1573 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
) {
1574 ste
->s1_cfg
= &smmu_domain
->s1_cfg
;
1576 arm_smmu_write_ctx_desc(smmu
, ste
->s1_cfg
);
1579 ste
->s2_cfg
= &smmu_domain
->s2_cfg
;
1582 for (i
= 0; i
< smmu_group
->num_sids
; ++i
) {
1583 u32 sid
= smmu_group
->sids
[i
];
1584 __le64
*step
= arm_smmu_get_step_for_sid(smmu
, sid
);
1586 arm_smmu_write_strtab_ent(smmu
, sid
, step
, ste
);
1592 static int arm_smmu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1595 struct arm_smmu_device
*smmu
;
1596 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1597 struct arm_smmu_group
*smmu_group
= arm_smmu_group_get(dev
);
1602 /* Already attached to a different domain? */
1603 if (smmu_group
->domain
&& smmu_group
->domain
!= smmu_domain
)
1606 smmu
= smmu_group
->smmu
;
1607 mutex_lock(&smmu_domain
->init_mutex
);
1609 if (!smmu_domain
->smmu
) {
1610 smmu_domain
->smmu
= smmu
;
1611 ret
= arm_smmu_domain_finalise(domain
);
1613 smmu_domain
->smmu
= NULL
;
1616 } else if (smmu_domain
->smmu
!= smmu
) {
1618 "cannot attach to SMMU %s (upstream of %s)\n",
1619 dev_name(smmu_domain
->smmu
->dev
),
1620 dev_name(smmu
->dev
));
1625 /* Group already attached to this domain? */
1626 if (smmu_group
->domain
)
1629 smmu_group
->domain
= smmu_domain
;
1630 smmu_group
->ste
.bypass
= false;
1632 ret
= arm_smmu_install_ste_for_group(smmu_group
);
1633 if (IS_ERR_VALUE(ret
))
1634 smmu_group
->domain
= NULL
;
1637 mutex_unlock(&smmu_domain
->init_mutex
);
1641 static void arm_smmu_detach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1643 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1644 struct arm_smmu_group
*smmu_group
= arm_smmu_group_get(dev
);
1646 BUG_ON(!smmu_domain
);
1647 BUG_ON(!smmu_group
);
1649 mutex_lock(&smmu_domain
->init_mutex
);
1650 BUG_ON(smmu_group
->domain
!= smmu_domain
);
1652 smmu_group
->ste
.bypass
= true;
1653 if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group
)))
1654 dev_warn(dev
, "failed to install bypass STE\n");
1656 smmu_group
->domain
= NULL
;
1657 mutex_unlock(&smmu_domain
->init_mutex
);
1660 static int arm_smmu_map(struct iommu_domain
*domain
, unsigned long iova
,
1661 phys_addr_t paddr
, size_t size
, int prot
)
1664 unsigned long flags
;
1665 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1666 struct io_pgtable_ops
*ops
= smmu_domain
->pgtbl_ops
;
1671 spin_lock_irqsave(&smmu_domain
->pgtbl_lock
, flags
);
1672 ret
= ops
->map(ops
, iova
, paddr
, size
, prot
);
1673 spin_unlock_irqrestore(&smmu_domain
->pgtbl_lock
, flags
);
1678 arm_smmu_unmap(struct iommu_domain
*domain
, unsigned long iova
, size_t size
)
1681 unsigned long flags
;
1682 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1683 struct io_pgtable_ops
*ops
= smmu_domain
->pgtbl_ops
;
1688 spin_lock_irqsave(&smmu_domain
->pgtbl_lock
, flags
);
1689 ret
= ops
->unmap(ops
, iova
, size
);
1690 spin_unlock_irqrestore(&smmu_domain
->pgtbl_lock
, flags
);
1695 arm_smmu_iova_to_phys(struct iommu_domain
*domain
, dma_addr_t iova
)
1698 unsigned long flags
;
1699 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1700 struct io_pgtable_ops
*ops
= smmu_domain
->pgtbl_ops
;
1705 spin_lock_irqsave(&smmu_domain
->pgtbl_lock
, flags
);
1706 ret
= ops
->iova_to_phys(ops
, iova
);
1707 spin_unlock_irqrestore(&smmu_domain
->pgtbl_lock
, flags
);
1712 static int __arm_smmu_get_pci_sid(struct pci_dev
*pdev
, u16 alias
, void *sidp
)
1714 *(u32
*)sidp
= alias
;
1715 return 0; /* Continue walking */
1718 static void __arm_smmu_release_pci_iommudata(void *data
)
1723 static struct arm_smmu_device
*arm_smmu_get_for_pci_dev(struct pci_dev
*pdev
)
1725 struct device_node
*of_node
;
1726 struct arm_smmu_device
*curr
, *smmu
= NULL
;
1727 struct pci_bus
*bus
= pdev
->bus
;
1729 /* Walk up to the root bus */
1730 while (!pci_is_root_bus(bus
))
1733 /* Follow the "iommus" phandle from the host controller */
1734 of_node
= of_parse_phandle(bus
->bridge
->parent
->of_node
, "iommus", 0);
1738 /* See if we can find an SMMU corresponding to the phandle */
1739 spin_lock(&arm_smmu_devices_lock
);
1740 list_for_each_entry(curr
, &arm_smmu_devices
, list
) {
1741 if (curr
->dev
->of_node
== of_node
) {
1746 spin_unlock(&arm_smmu_devices_lock
);
1747 of_node_put(of_node
);
1751 static bool arm_smmu_sid_in_range(struct arm_smmu_device
*smmu
, u32 sid
)
1753 unsigned long limit
= smmu
->strtab_cfg
.num_l1_ents
;
1755 if (smmu
->features
& ARM_SMMU_FEAT_2_LVL_STRTAB
)
1756 limit
*= 1UL << STRTAB_SPLIT
;
1761 static int arm_smmu_add_device(struct device
*dev
)
1765 struct pci_dev
*pdev
;
1766 struct iommu_group
*group
;
1767 struct arm_smmu_group
*smmu_group
;
1768 struct arm_smmu_device
*smmu
;
1770 /* We only support PCI, for now */
1771 if (!dev_is_pci(dev
))
1774 pdev
= to_pci_dev(dev
);
1775 group
= iommu_group_get_for_dev(dev
);
1777 return PTR_ERR(group
);
1779 smmu_group
= iommu_group_get_iommudata(group
);
1781 smmu
= arm_smmu_get_for_pci_dev(pdev
);
1787 smmu_group
= kzalloc(sizeof(*smmu_group
), GFP_KERNEL
);
1793 smmu_group
->ste
.valid
= true;
1794 smmu_group
->smmu
= smmu
;
1795 iommu_group_set_iommudata(group
, smmu_group
,
1796 __arm_smmu_release_pci_iommudata
);
1798 smmu
= smmu_group
->smmu
;
1801 /* Assume SID == RID until firmware tells us otherwise */
1802 pci_for_each_dma_alias(pdev
, __arm_smmu_get_pci_sid
, &sid
);
1803 for (i
= 0; i
< smmu_group
->num_sids
; ++i
) {
1804 /* If we already know about this SID, then we're done */
1805 if (smmu_group
->sids
[i
] == sid
)
1809 /* Check the SID is in range of the SMMU and our stream table */
1810 if (!arm_smmu_sid_in_range(smmu
, sid
)) {
1815 /* Ensure l2 strtab is initialised */
1816 if (smmu
->features
& ARM_SMMU_FEAT_2_LVL_STRTAB
) {
1817 ret
= arm_smmu_init_l2_strtab(smmu
, sid
);
1822 /* Resize the SID array for the group */
1823 smmu_group
->num_sids
++;
1824 sids
= krealloc(smmu_group
->sids
, smmu_group
->num_sids
* sizeof(*sids
),
1827 smmu_group
->num_sids
--;
1832 /* Add the new SID */
1833 sids
[smmu_group
->num_sids
- 1] = sid
;
1834 smmu_group
->sids
= sids
;
1838 iommu_group_put(group
);
1842 static void arm_smmu_remove_device(struct device
*dev
)
1844 iommu_group_remove_device(dev
);
1847 static int arm_smmu_domain_get_attr(struct iommu_domain
*domain
,
1848 enum iommu_attr attr
, void *data
)
1850 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1853 case DOMAIN_ATTR_NESTING
:
1854 *(int *)data
= (smmu_domain
->stage
== ARM_SMMU_DOMAIN_NESTED
);
1861 static int arm_smmu_domain_set_attr(struct iommu_domain
*domain
,
1862 enum iommu_attr attr
, void *data
)
1865 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1867 mutex_lock(&smmu_domain
->init_mutex
);
1870 case DOMAIN_ATTR_NESTING
:
1871 if (smmu_domain
->smmu
) {
1877 smmu_domain
->stage
= ARM_SMMU_DOMAIN_NESTED
;
1879 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S1
;
1887 mutex_unlock(&smmu_domain
->init_mutex
);
1891 static struct iommu_ops arm_smmu_ops
= {
1892 .capable
= arm_smmu_capable
,
1893 .domain_alloc
= arm_smmu_domain_alloc
,
1894 .domain_free
= arm_smmu_domain_free
,
1895 .attach_dev
= arm_smmu_attach_dev
,
1896 .detach_dev
= arm_smmu_detach_dev
,
1897 .map
= arm_smmu_map
,
1898 .unmap
= arm_smmu_unmap
,
1899 .iova_to_phys
= arm_smmu_iova_to_phys
,
1900 .add_device
= arm_smmu_add_device
,
1901 .remove_device
= arm_smmu_remove_device
,
1902 .domain_get_attr
= arm_smmu_domain_get_attr
,
1903 .domain_set_attr
= arm_smmu_domain_set_attr
,
1904 .pgsize_bitmap
= -1UL, /* Restricted during device attach */
1907 /* Probing and initialisation functions */
1908 static int arm_smmu_init_one_queue(struct arm_smmu_device
*smmu
,
1909 struct arm_smmu_queue
*q
,
1910 unsigned long prod_off
,
1911 unsigned long cons_off
,
1914 size_t qsz
= ((1 << q
->max_n_shift
) * dwords
) << 3;
1916 q
->base
= dma_alloc_coherent(smmu
->dev
, qsz
, &q
->base_dma
, GFP_KERNEL
);
1918 dev_err(smmu
->dev
, "failed to allocate queue (0x%zx bytes)\n",
1923 q
->prod_reg
= smmu
->base
+ prod_off
;
1924 q
->cons_reg
= smmu
->base
+ cons_off
;
1925 q
->ent_dwords
= dwords
;
1927 q
->q_base
= Q_BASE_RWA
;
1928 q
->q_base
|= q
->base_dma
& Q_BASE_ADDR_MASK
<< Q_BASE_ADDR_SHIFT
;
1929 q
->q_base
|= (q
->max_n_shift
& Q_BASE_LOG2SIZE_MASK
)
1930 << Q_BASE_LOG2SIZE_SHIFT
;
1932 q
->prod
= q
->cons
= 0;
1936 static void arm_smmu_free_one_queue(struct arm_smmu_device
*smmu
,
1937 struct arm_smmu_queue
*q
)
1939 size_t qsz
= ((1 << q
->max_n_shift
) * q
->ent_dwords
) << 3;
1941 dma_free_coherent(smmu
->dev
, qsz
, q
->base
, q
->base_dma
);
1944 static void arm_smmu_free_queues(struct arm_smmu_device
*smmu
)
1946 arm_smmu_free_one_queue(smmu
, &smmu
->cmdq
.q
);
1947 arm_smmu_free_one_queue(smmu
, &smmu
->evtq
.q
);
1949 if (smmu
->features
& ARM_SMMU_FEAT_PRI
)
1950 arm_smmu_free_one_queue(smmu
, &smmu
->priq
.q
);
1953 static int arm_smmu_init_queues(struct arm_smmu_device
*smmu
)
1958 spin_lock_init(&smmu
->cmdq
.lock
);
1959 ret
= arm_smmu_init_one_queue(smmu
, &smmu
->cmdq
.q
, ARM_SMMU_CMDQ_PROD
,
1960 ARM_SMMU_CMDQ_CONS
, CMDQ_ENT_DWORDS
);
1965 ret
= arm_smmu_init_one_queue(smmu
, &smmu
->evtq
.q
, ARM_SMMU_EVTQ_PROD
,
1966 ARM_SMMU_EVTQ_CONS
, EVTQ_ENT_DWORDS
);
1971 if (!(smmu
->features
& ARM_SMMU_FEAT_PRI
))
1974 ret
= arm_smmu_init_one_queue(smmu
, &smmu
->priq
.q
, ARM_SMMU_PRIQ_PROD
,
1975 ARM_SMMU_PRIQ_CONS
, PRIQ_ENT_DWORDS
);
1982 arm_smmu_free_one_queue(smmu
, &smmu
->evtq
.q
);
1984 arm_smmu_free_one_queue(smmu
, &smmu
->cmdq
.q
);
1989 static void arm_smmu_free_l2_strtab(struct arm_smmu_device
*smmu
)
1993 struct arm_smmu_strtab_cfg
*cfg
= &smmu
->strtab_cfg
;
1995 size
= 1 << (STRTAB_SPLIT
+ ilog2(STRTAB_STE_DWORDS
) + 3);
1996 for (i
= 0; i
< cfg
->num_l1_ents
; ++i
) {
1997 struct arm_smmu_strtab_l1_desc
*desc
= &cfg
->l1_desc
[i
];
2002 dma_free_coherent(smmu
->dev
, size
, desc
->l2ptr
,
2007 static int arm_smmu_init_l1_strtab(struct arm_smmu_device
*smmu
)
2010 struct arm_smmu_strtab_cfg
*cfg
= &smmu
->strtab_cfg
;
2011 size_t size
= sizeof(*cfg
->l1_desc
) * cfg
->num_l1_ents
;
2012 void *strtab
= smmu
->strtab_cfg
.strtab
;
2014 cfg
->l1_desc
= devm_kzalloc(smmu
->dev
, size
, GFP_KERNEL
);
2015 if (!cfg
->l1_desc
) {
2016 dev_err(smmu
->dev
, "failed to allocate l1 stream table desc\n");
2020 for (i
= 0; i
< cfg
->num_l1_ents
; ++i
) {
2021 arm_smmu_write_strtab_l1_desc(strtab
, &cfg
->l1_desc
[i
]);
2022 strtab
+= STRTAB_L1_DESC_DWORDS
<< 3;
2028 static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device
*smmu
)
2034 struct arm_smmu_strtab_cfg
*cfg
= &smmu
->strtab_cfg
;
2037 * If we can resolve everything with a single L2 table, then we
2038 * just need a single L1 descriptor. Otherwise, calculate the L1
2039 * size, capped to the SIDSIZE.
2041 if (smmu
->sid_bits
< STRTAB_SPLIT
) {
2044 size
= STRTAB_L1_SZ_SHIFT
- (ilog2(STRTAB_L1_DESC_DWORDS
) + 3);
2045 size
= min(size
, smmu
->sid_bits
- STRTAB_SPLIT
);
2047 cfg
->num_l1_ents
= 1 << size
;
2049 size
+= STRTAB_SPLIT
;
2050 if (size
< smmu
->sid_bits
)
2052 "2-level strtab only covers %u/%u bits of SID\n",
2053 size
, smmu
->sid_bits
);
2055 l1size
= cfg
->num_l1_ents
* (STRTAB_L1_DESC_DWORDS
<< 3);
2056 strtab
= dma_zalloc_coherent(smmu
->dev
, l1size
, &cfg
->strtab_dma
,
2060 "failed to allocate l1 stream table (%u bytes)\n",
2064 cfg
->strtab
= strtab
;
2066 /* Configure strtab_base_cfg for 2 levels */
2067 reg
= STRTAB_BASE_CFG_FMT_2LVL
;
2068 reg
|= (size
& STRTAB_BASE_CFG_LOG2SIZE_MASK
)
2069 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT
;
2070 reg
|= (STRTAB_SPLIT
& STRTAB_BASE_CFG_SPLIT_MASK
)
2071 << STRTAB_BASE_CFG_SPLIT_SHIFT
;
2072 cfg
->strtab_base_cfg
= reg
;
2074 ret
= arm_smmu_init_l1_strtab(smmu
);
2076 dma_free_coherent(smmu
->dev
,
2083 static int arm_smmu_init_strtab_linear(struct arm_smmu_device
*smmu
)
2088 struct arm_smmu_strtab_cfg
*cfg
= &smmu
->strtab_cfg
;
2090 size
= (1 << smmu
->sid_bits
) * (STRTAB_STE_DWORDS
<< 3);
2091 strtab
= dma_zalloc_coherent(smmu
->dev
, size
, &cfg
->strtab_dma
,
2095 "failed to allocate linear stream table (%u bytes)\n",
2099 cfg
->strtab
= strtab
;
2100 cfg
->num_l1_ents
= 1 << smmu
->sid_bits
;
2102 /* Configure strtab_base_cfg for a linear table covering all SIDs */
2103 reg
= STRTAB_BASE_CFG_FMT_LINEAR
;
2104 reg
|= (smmu
->sid_bits
& STRTAB_BASE_CFG_LOG2SIZE_MASK
)
2105 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT
;
2106 cfg
->strtab_base_cfg
= reg
;
2108 arm_smmu_init_bypass_stes(strtab
, cfg
->num_l1_ents
);
2112 static int arm_smmu_init_strtab(struct arm_smmu_device
*smmu
)
2117 if (smmu
->features
& ARM_SMMU_FEAT_2_LVL_STRTAB
)
2118 ret
= arm_smmu_init_strtab_2lvl(smmu
);
2120 ret
= arm_smmu_init_strtab_linear(smmu
);
2125 /* Set the strtab base address */
2126 reg
= smmu
->strtab_cfg
.strtab_dma
&
2127 STRTAB_BASE_ADDR_MASK
<< STRTAB_BASE_ADDR_SHIFT
;
2128 reg
|= STRTAB_BASE_RA
;
2129 smmu
->strtab_cfg
.strtab_base
= reg
;
2131 /* Allocate the first VMID for stage-2 bypass STEs */
2132 set_bit(0, smmu
->vmid_map
);
2136 static void arm_smmu_free_strtab(struct arm_smmu_device
*smmu
)
2138 struct arm_smmu_strtab_cfg
*cfg
= &smmu
->strtab_cfg
;
2139 u32 size
= cfg
->num_l1_ents
;
2141 if (smmu
->features
& ARM_SMMU_FEAT_2_LVL_STRTAB
) {
2142 arm_smmu_free_l2_strtab(smmu
);
2143 size
*= STRTAB_L1_DESC_DWORDS
<< 3;
2145 size
*= STRTAB_STE_DWORDS
* 3;
2148 dma_free_coherent(smmu
->dev
, size
, cfg
->strtab
, cfg
->strtab_dma
);
2151 static int arm_smmu_init_structures(struct arm_smmu_device
*smmu
)
2155 ret
= arm_smmu_init_queues(smmu
);
2159 ret
= arm_smmu_init_strtab(smmu
);
2161 goto out_free_queues
;
2166 arm_smmu_free_queues(smmu
);
2170 static void arm_smmu_free_structures(struct arm_smmu_device
*smmu
)
2172 arm_smmu_free_strtab(smmu
);
2173 arm_smmu_free_queues(smmu
);
2176 static int arm_smmu_write_reg_sync(struct arm_smmu_device
*smmu
, u32 val
,
2177 unsigned int reg_off
, unsigned int ack_off
)
2181 writel_relaxed(val
, smmu
->base
+ reg_off
);
2182 return readl_relaxed_poll_timeout(smmu
->base
+ ack_off
, reg
, reg
== val
,
2183 1, ARM_SMMU_POLL_TIMEOUT_US
);
2186 static int arm_smmu_setup_irqs(struct arm_smmu_device
*smmu
)
2189 u32 irqen_flags
= IRQ_CTRL_EVTQ_IRQEN
| IRQ_CTRL_GERROR_IRQEN
;
2191 /* Disable IRQs first */
2192 ret
= arm_smmu_write_reg_sync(smmu
, 0, ARM_SMMU_IRQ_CTRL
,
2193 ARM_SMMU_IRQ_CTRLACK
);
2195 dev_err(smmu
->dev
, "failed to disable irqs\n");
2199 /* Clear the MSI address regs */
2200 writeq_relaxed(0, smmu
->base
+ ARM_SMMU_GERROR_IRQ_CFG0
);
2201 writeq_relaxed(0, smmu
->base
+ ARM_SMMU_EVTQ_IRQ_CFG0
);
2203 /* Request wired interrupt lines */
2204 irq
= smmu
->evtq
.q
.irq
;
2206 ret
= devm_request_threaded_irq(smmu
->dev
, irq
,
2207 arm_smmu_evtq_handler
,
2208 arm_smmu_evtq_thread
,
2209 0, "arm-smmu-v3-evtq", smmu
);
2210 if (IS_ERR_VALUE(ret
))
2211 dev_warn(smmu
->dev
, "failed to enable evtq irq\n");
2214 irq
= smmu
->cmdq
.q
.irq
;
2216 ret
= devm_request_irq(smmu
->dev
, irq
,
2217 arm_smmu_cmdq_sync_handler
, 0,
2218 "arm-smmu-v3-cmdq-sync", smmu
);
2219 if (IS_ERR_VALUE(ret
))
2220 dev_warn(smmu
->dev
, "failed to enable cmdq-sync irq\n");
2223 irq
= smmu
->gerr_irq
;
2225 ret
= devm_request_irq(smmu
->dev
, irq
, arm_smmu_gerror_handler
,
2226 0, "arm-smmu-v3-gerror", smmu
);
2227 if (IS_ERR_VALUE(ret
))
2228 dev_warn(smmu
->dev
, "failed to enable gerror irq\n");
2231 if (smmu
->features
& ARM_SMMU_FEAT_PRI
) {
2232 writeq_relaxed(0, smmu
->base
+ ARM_SMMU_PRIQ_IRQ_CFG0
);
2234 irq
= smmu
->priq
.q
.irq
;
2236 ret
= devm_request_threaded_irq(smmu
->dev
, irq
,
2237 arm_smmu_priq_handler
,
2238 arm_smmu_priq_thread
,
2239 0, "arm-smmu-v3-priq",
2241 if (IS_ERR_VALUE(ret
))
2243 "failed to enable priq irq\n");
2245 irqen_flags
|= IRQ_CTRL_PRIQ_IRQEN
;
2249 /* Enable interrupt generation on the SMMU */
2250 ret
= arm_smmu_write_reg_sync(smmu
, irqen_flags
,
2251 ARM_SMMU_IRQ_CTRL
, ARM_SMMU_IRQ_CTRLACK
);
2253 dev_warn(smmu
->dev
, "failed to enable irqs\n");
2258 static int arm_smmu_device_disable(struct arm_smmu_device
*smmu
)
2262 ret
= arm_smmu_write_reg_sync(smmu
, 0, ARM_SMMU_CR0
, ARM_SMMU_CR0ACK
);
2264 dev_err(smmu
->dev
, "failed to clear cr0\n");
2269 static int arm_smmu_device_reset(struct arm_smmu_device
*smmu
)
2273 struct arm_smmu_cmdq_ent cmd
;
2275 /* Clear CR0 and sync (disables SMMU and queue processing) */
2276 reg
= readl_relaxed(smmu
->base
+ ARM_SMMU_CR0
);
2277 if (reg
& CR0_SMMUEN
)
2278 dev_warn(smmu
->dev
, "SMMU currently enabled! Resetting...\n");
2280 ret
= arm_smmu_device_disable(smmu
);
2284 /* CR1 (table and queue memory attributes) */
2285 reg
= (CR1_SH_ISH
<< CR1_TABLE_SH_SHIFT
) |
2286 (CR1_CACHE_WB
<< CR1_TABLE_OC_SHIFT
) |
2287 (CR1_CACHE_WB
<< CR1_TABLE_IC_SHIFT
) |
2288 (CR1_SH_ISH
<< CR1_QUEUE_SH_SHIFT
) |
2289 (CR1_CACHE_WB
<< CR1_QUEUE_OC_SHIFT
) |
2290 (CR1_CACHE_WB
<< CR1_QUEUE_IC_SHIFT
);
2291 writel_relaxed(reg
, smmu
->base
+ ARM_SMMU_CR1
);
2293 /* CR2 (random crap) */
2294 reg
= CR2_PTM
| CR2_RECINVSID
| CR2_E2H
;
2295 writel_relaxed(reg
, smmu
->base
+ ARM_SMMU_CR2
);
2298 writeq_relaxed(smmu
->strtab_cfg
.strtab_base
,
2299 smmu
->base
+ ARM_SMMU_STRTAB_BASE
);
2300 writel_relaxed(smmu
->strtab_cfg
.strtab_base_cfg
,
2301 smmu
->base
+ ARM_SMMU_STRTAB_BASE_CFG
);
2304 writeq_relaxed(smmu
->cmdq
.q
.q_base
, smmu
->base
+ ARM_SMMU_CMDQ_BASE
);
2305 writel_relaxed(smmu
->cmdq
.q
.prod
, smmu
->base
+ ARM_SMMU_CMDQ_PROD
);
2306 writel_relaxed(smmu
->cmdq
.q
.cons
, smmu
->base
+ ARM_SMMU_CMDQ_CONS
);
2308 enables
= CR0_CMDQEN
;
2309 ret
= arm_smmu_write_reg_sync(smmu
, enables
, ARM_SMMU_CR0
,
2312 dev_err(smmu
->dev
, "failed to enable command queue\n");
2316 /* Invalidate any cached configuration */
2317 cmd
.opcode
= CMDQ_OP_CFGI_ALL
;
2318 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
2319 cmd
.opcode
= CMDQ_OP_CMD_SYNC
;
2320 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
2322 /* Invalidate any stale TLB entries */
2323 if (smmu
->features
& ARM_SMMU_FEAT_HYP
) {
2324 cmd
.opcode
= CMDQ_OP_TLBI_EL2_ALL
;
2325 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
2328 cmd
.opcode
= CMDQ_OP_TLBI_NSNH_ALL
;
2329 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
2330 cmd
.opcode
= CMDQ_OP_CMD_SYNC
;
2331 arm_smmu_cmdq_issue_cmd(smmu
, &cmd
);
2334 writeq_relaxed(smmu
->evtq
.q
.q_base
, smmu
->base
+ ARM_SMMU_EVTQ_BASE
);
2335 writel_relaxed(smmu
->evtq
.q
.prod
, smmu
->base
+ ARM_SMMU_EVTQ_PROD
);
2336 writel_relaxed(smmu
->evtq
.q
.cons
, smmu
->base
+ ARM_SMMU_EVTQ_CONS
);
2338 enables
|= CR0_EVTQEN
;
2339 ret
= arm_smmu_write_reg_sync(smmu
, enables
, ARM_SMMU_CR0
,
2342 dev_err(smmu
->dev
, "failed to enable event queue\n");
2347 if (smmu
->features
& ARM_SMMU_FEAT_PRI
) {
2348 writeq_relaxed(smmu
->priq
.q
.q_base
,
2349 smmu
->base
+ ARM_SMMU_PRIQ_BASE
);
2350 writel_relaxed(smmu
->priq
.q
.prod
,
2351 smmu
->base
+ ARM_SMMU_PRIQ_PROD
);
2352 writel_relaxed(smmu
->priq
.q
.cons
,
2353 smmu
->base
+ ARM_SMMU_PRIQ_CONS
);
2355 enables
|= CR0_PRIQEN
;
2356 ret
= arm_smmu_write_reg_sync(smmu
, enables
, ARM_SMMU_CR0
,
2359 dev_err(smmu
->dev
, "failed to enable PRI queue\n");
2364 ret
= arm_smmu_setup_irqs(smmu
);
2366 dev_err(smmu
->dev
, "failed to setup irqs\n");
2370 /* Enable the SMMU interface */
2371 enables
|= CR0_SMMUEN
;
2372 ret
= arm_smmu_write_reg_sync(smmu
, enables
, ARM_SMMU_CR0
,
2375 dev_err(smmu
->dev
, "failed to enable SMMU interface\n");
2382 static int arm_smmu_device_probe(struct arm_smmu_device
*smmu
)
2386 unsigned long pgsize_bitmap
= 0;
2389 reg
= readl_relaxed(smmu
->base
+ ARM_SMMU_IDR0
);
2391 /* 2-level structures */
2392 if ((reg
& IDR0_ST_LVL_MASK
<< IDR0_ST_LVL_SHIFT
) == IDR0_ST_LVL_2LVL
)
2393 smmu
->features
|= ARM_SMMU_FEAT_2_LVL_STRTAB
;
2395 if (reg
& IDR0_CD2L
)
2396 smmu
->features
|= ARM_SMMU_FEAT_2_LVL_CDTAB
;
2399 * Translation table endianness.
2400 * We currently require the same endianness as the CPU, but this
2401 * could be changed later by adding a new IO_PGTABLE_QUIRK.
2403 switch (reg
& IDR0_TTENDIAN_MASK
<< IDR0_TTENDIAN_SHIFT
) {
2404 case IDR0_TTENDIAN_MIXED
:
2405 smmu
->features
|= ARM_SMMU_FEAT_TT_LE
| ARM_SMMU_FEAT_TT_BE
;
2408 case IDR0_TTENDIAN_BE
:
2409 smmu
->features
|= ARM_SMMU_FEAT_TT_BE
;
2412 case IDR0_TTENDIAN_LE
:
2413 smmu
->features
|= ARM_SMMU_FEAT_TT_LE
;
2417 dev_err(smmu
->dev
, "unknown/unsupported TT endianness!\n");
2421 /* Boolean feature flags */
2422 if (IS_ENABLED(CONFIG_PCI_PRI
) && reg
& IDR0_PRI
)
2423 smmu
->features
|= ARM_SMMU_FEAT_PRI
;
2425 if (IS_ENABLED(CONFIG_PCI_ATS
) && reg
& IDR0_ATS
)
2426 smmu
->features
|= ARM_SMMU_FEAT_ATS
;
2429 smmu
->features
|= ARM_SMMU_FEAT_SEV
;
2432 smmu
->features
|= ARM_SMMU_FEAT_MSI
;
2435 smmu
->features
|= ARM_SMMU_FEAT_HYP
;
2438 * The dma-coherent property is used in preference to the ID
2439 * register, but warn on mismatch.
2441 coherent
= of_dma_is_coherent(smmu
->dev
->of_node
);
2443 smmu
->features
|= ARM_SMMU_FEAT_COHERENCY
;
2445 if (!!(reg
& IDR0_COHACC
) != coherent
)
2446 dev_warn(smmu
->dev
, "IDR0.COHACC overridden by dma-coherent property (%s)\n",
2447 coherent
? "true" : "false");
2449 if (reg
& IDR0_STALL_MODEL
)
2450 smmu
->features
|= ARM_SMMU_FEAT_STALLS
;
2453 smmu
->features
|= ARM_SMMU_FEAT_TRANS_S1
;
2456 smmu
->features
|= ARM_SMMU_FEAT_TRANS_S2
;
2458 if (!(reg
& (IDR0_S1P
| IDR0_S2P
))) {
2459 dev_err(smmu
->dev
, "no translation support!\n");
2463 /* We only support the AArch64 table format at present */
2464 switch (reg
& IDR0_TTF_MASK
<< IDR0_TTF_SHIFT
) {
2465 case IDR0_TTF_AARCH32_64
:
2468 case IDR0_TTF_AARCH64
:
2471 dev_err(smmu
->dev
, "AArch64 table format not supported!\n");
2475 /* ASID/VMID sizes */
2476 smmu
->asid_bits
= reg
& IDR0_ASID16
? 16 : 8;
2477 smmu
->vmid_bits
= reg
& IDR0_VMID16
? 16 : 8;
2480 reg
= readl_relaxed(smmu
->base
+ ARM_SMMU_IDR1
);
2481 if (reg
& (IDR1_TABLES_PRESET
| IDR1_QUEUES_PRESET
| IDR1_REL
)) {
2482 dev_err(smmu
->dev
, "embedded implementation not supported\n");
2486 /* Queue sizes, capped at 4k */
2487 smmu
->cmdq
.q
.max_n_shift
= min((u32
)CMDQ_MAX_SZ_SHIFT
,
2488 reg
>> IDR1_CMDQ_SHIFT
& IDR1_CMDQ_MASK
);
2489 if (!smmu
->cmdq
.q
.max_n_shift
) {
2490 /* Odd alignment restrictions on the base, so ignore for now */
2491 dev_err(smmu
->dev
, "unit-length command queue not supported\n");
2495 smmu
->evtq
.q
.max_n_shift
= min((u32
)EVTQ_MAX_SZ_SHIFT
,
2496 reg
>> IDR1_EVTQ_SHIFT
& IDR1_EVTQ_MASK
);
2497 smmu
->priq
.q
.max_n_shift
= min((u32
)PRIQ_MAX_SZ_SHIFT
,
2498 reg
>> IDR1_PRIQ_SHIFT
& IDR1_PRIQ_MASK
);
2500 /* SID/SSID sizes */
2501 smmu
->ssid_bits
= reg
>> IDR1_SSID_SHIFT
& IDR1_SSID_MASK
;
2502 smmu
->sid_bits
= reg
>> IDR1_SID_SHIFT
& IDR1_SID_MASK
;
2505 reg
= readl_relaxed(smmu
->base
+ ARM_SMMU_IDR5
);
2507 /* Maximum number of outstanding stalls */
2508 smmu
->evtq
.max_stalls
= reg
>> IDR5_STALL_MAX_SHIFT
2509 & IDR5_STALL_MAX_MASK
;
2512 if (reg
& IDR5_GRAN64K
)
2513 pgsize_bitmap
|= SZ_64K
| SZ_512M
;
2514 if (reg
& IDR5_GRAN16K
)
2515 pgsize_bitmap
|= SZ_16K
| SZ_32M
;
2516 if (reg
& IDR5_GRAN4K
)
2517 pgsize_bitmap
|= SZ_4K
| SZ_2M
| SZ_1G
;
2519 arm_smmu_ops
.pgsize_bitmap
&= pgsize_bitmap
;
2521 /* Output address size */
2522 switch (reg
& IDR5_OAS_MASK
<< IDR5_OAS_SHIFT
) {
2523 case IDR5_OAS_32_BIT
:
2526 case IDR5_OAS_36_BIT
:
2529 case IDR5_OAS_40_BIT
:
2532 case IDR5_OAS_42_BIT
:
2535 case IDR5_OAS_44_BIT
:
2540 "unknown output address size. Truncating to 48-bit\n");
2542 case IDR5_OAS_48_BIT
:
2546 /* Set the DMA mask for our table walker */
2547 if (dma_set_mask_and_coherent(smmu
->dev
, DMA_BIT_MASK(smmu
->oas
)))
2549 "failed to set DMA mask for table walker\n");
2551 smmu
->ias
= max(smmu
->ias
, smmu
->oas
);
2553 dev_info(smmu
->dev
, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
2554 smmu
->ias
, smmu
->oas
, smmu
->features
);
2558 static int arm_smmu_device_dt_probe(struct platform_device
*pdev
)
2561 struct resource
*res
;
2562 struct arm_smmu_device
*smmu
;
2563 struct device
*dev
= &pdev
->dev
;
2565 smmu
= devm_kzalloc(dev
, sizeof(*smmu
), GFP_KERNEL
);
2567 dev_err(dev
, "failed to allocate arm_smmu_device\n");
2573 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2574 if (resource_size(res
) + 1 < SZ_128K
) {
2575 dev_err(dev
, "MMIO region too small (%pr)\n", res
);
2579 smmu
->base
= devm_ioremap_resource(dev
, res
);
2580 if (IS_ERR(smmu
->base
))
2581 return PTR_ERR(smmu
->base
);
2583 /* Interrupt lines */
2584 irq
= platform_get_irq_byname(pdev
, "eventq");
2586 smmu
->evtq
.q
.irq
= irq
;
2588 irq
= platform_get_irq_byname(pdev
, "priq");
2590 smmu
->priq
.q
.irq
= irq
;
2592 irq
= platform_get_irq_byname(pdev
, "cmdq-sync");
2594 smmu
->cmdq
.q
.irq
= irq
;
2596 irq
= platform_get_irq_byname(pdev
, "gerror");
2598 smmu
->gerr_irq
= irq
;
2600 parse_driver_options(smmu
);
2603 ret
= arm_smmu_device_probe(smmu
);
2607 /* Initialise in-memory data structures */
2608 ret
= arm_smmu_init_structures(smmu
);
2612 /* Reset the device */
2613 ret
= arm_smmu_device_reset(smmu
);
2615 goto out_free_structures
;
2617 /* Record our private device structure */
2618 INIT_LIST_HEAD(&smmu
->list
);
2619 spin_lock(&arm_smmu_devices_lock
);
2620 list_add(&smmu
->list
, &arm_smmu_devices
);
2621 spin_unlock(&arm_smmu_devices_lock
);
2624 out_free_structures
:
2625 arm_smmu_free_structures(smmu
);
2629 static int arm_smmu_device_remove(struct platform_device
*pdev
)
2631 struct arm_smmu_device
*curr
, *smmu
= NULL
;
2632 struct device
*dev
= &pdev
->dev
;
2634 spin_lock(&arm_smmu_devices_lock
);
2635 list_for_each_entry(curr
, &arm_smmu_devices
, list
) {
2636 if (curr
->dev
== dev
) {
2638 list_del(&smmu
->list
);
2642 spin_unlock(&arm_smmu_devices_lock
);
2647 arm_smmu_device_disable(smmu
);
2648 arm_smmu_free_structures(smmu
);
2652 static struct of_device_id arm_smmu_of_match
[] = {
2653 { .compatible
= "arm,smmu-v3", },
2656 MODULE_DEVICE_TABLE(of
, arm_smmu_of_match
);
2658 static struct platform_driver arm_smmu_driver
= {
2660 .name
= "arm-smmu-v3",
2661 .of_match_table
= of_match_ptr(arm_smmu_of_match
),
2663 .probe
= arm_smmu_device_dt_probe
,
2664 .remove
= arm_smmu_device_remove
,
2667 static int __init
arm_smmu_init(void)
2669 struct device_node
*np
;
2672 np
= of_find_matching_node(NULL
, arm_smmu_of_match
);
2678 ret
= platform_driver_register(&arm_smmu_driver
);
2682 return bus_set_iommu(&pci_bus_type
, &arm_smmu_ops
);
2685 static void __exit
arm_smmu_exit(void)
2687 return platform_driver_unregister(&arm_smmu_driver
);
2690 subsys_initcall(arm_smmu_init
);
2691 module_exit(arm_smmu_exit
);
2693 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
2694 MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2695 MODULE_LICENSE("GPL v2");