Linux 4.3-rc5
[linux-2.6-block.git] / drivers / iommu / arm-smmu-v3.c
CommitLineData
48ec83bc
WD
1/*
2 * IOMMU API for ARM architected SMMUv3 implementations.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 * Copyright (C) 2015 ARM Limited
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 *
20 * This driver is powered by bad coffee and bombay mix.
21 */
22
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>
29#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/pci.h>
32#include <linux/platform_device.h>
33
34#include "io-pgtable.h"
35
36/* MMIO registers */
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_S1P (1 << 1)
60#define IDR0_S2P (1 << 0)
61
62#define ARM_SMMU_IDR1 0x4
63#define IDR1_TABLES_PRESET (1 << 30)
64#define IDR1_QUEUES_PRESET (1 << 29)
65#define IDR1_REL (1 << 28)
66#define IDR1_CMDQ_SHIFT 21
67#define IDR1_CMDQ_MASK 0x1f
68#define IDR1_EVTQ_SHIFT 16
69#define IDR1_EVTQ_MASK 0x1f
70#define IDR1_PRIQ_SHIFT 11
71#define IDR1_PRIQ_MASK 0x1f
72#define IDR1_SSID_SHIFT 6
73#define IDR1_SSID_MASK 0x1f
74#define IDR1_SID_SHIFT 0
75#define IDR1_SID_MASK 0x3f
76
77#define ARM_SMMU_IDR5 0x14
78#define IDR5_STALL_MAX_SHIFT 16
79#define IDR5_STALL_MAX_MASK 0xffff
80#define IDR5_GRAN64K (1 << 6)
81#define IDR5_GRAN16K (1 << 5)
82#define IDR5_GRAN4K (1 << 4)
83#define IDR5_OAS_SHIFT 0
84#define IDR5_OAS_MASK 0x7
85#define IDR5_OAS_32_BIT (0 << IDR5_OAS_SHIFT)
86#define IDR5_OAS_36_BIT (1 << IDR5_OAS_SHIFT)
87#define IDR5_OAS_40_BIT (2 << IDR5_OAS_SHIFT)
88#define IDR5_OAS_42_BIT (3 << IDR5_OAS_SHIFT)
89#define IDR5_OAS_44_BIT (4 << IDR5_OAS_SHIFT)
90#define IDR5_OAS_48_BIT (5 << IDR5_OAS_SHIFT)
91
92#define ARM_SMMU_CR0 0x20
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_SH_NSH 0
102#define CR1_SH_OSH 2
103#define CR1_SH_ISH 3
104#define CR1_CACHE_NC 0
105#define CR1_CACHE_WB 1
106#define CR1_CACHE_WT 2
107#define CR1_TABLE_SH_SHIFT 10
108#define CR1_TABLE_OC_SHIFT 8
109#define CR1_TABLE_IC_SHIFT 6
110#define CR1_QUEUE_SH_SHIFT 4
111#define CR1_QUEUE_OC_SHIFT 2
112#define CR1_QUEUE_IC_SHIFT 0
113
114#define ARM_SMMU_CR2 0x2c
115#define CR2_PTM (1 << 2)
116#define CR2_RECINVSID (1 << 1)
117#define CR2_E2H (1 << 0)
118
119#define ARM_SMMU_IRQ_CTRL 0x50
120#define IRQ_CTRL_EVTQ_IRQEN (1 << 2)
ccd6385d 121#define IRQ_CTRL_PRIQ_IRQEN (1 << 1)
48ec83bc
WD
122#define IRQ_CTRL_GERROR_IRQEN (1 << 0)
123
124#define ARM_SMMU_IRQ_CTRLACK 0x54
125
126#define ARM_SMMU_GERROR 0x60
127#define GERROR_SFM_ERR (1 << 8)
128#define GERROR_MSI_GERROR_ABT_ERR (1 << 7)
129#define GERROR_MSI_PRIQ_ABT_ERR (1 << 6)
130#define GERROR_MSI_EVTQ_ABT_ERR (1 << 5)
131#define GERROR_MSI_CMDQ_ABT_ERR (1 << 4)
132#define GERROR_PRIQ_ABT_ERR (1 << 3)
133#define GERROR_EVTQ_ABT_ERR (1 << 2)
134#define GERROR_CMDQ_ERR (1 << 0)
135#define GERROR_ERR_MASK 0xfd
136
137#define ARM_SMMU_GERRORN 0x64
138
139#define ARM_SMMU_GERROR_IRQ_CFG0 0x68
140#define ARM_SMMU_GERROR_IRQ_CFG1 0x70
141#define ARM_SMMU_GERROR_IRQ_CFG2 0x74
142
143#define ARM_SMMU_STRTAB_BASE 0x80
144#define STRTAB_BASE_RA (1UL << 62)
145#define STRTAB_BASE_ADDR_SHIFT 6
146#define STRTAB_BASE_ADDR_MASK 0x3ffffffffffUL
147
148#define ARM_SMMU_STRTAB_BASE_CFG 0x88
149#define STRTAB_BASE_CFG_LOG2SIZE_SHIFT 0
150#define STRTAB_BASE_CFG_LOG2SIZE_MASK 0x3f
151#define STRTAB_BASE_CFG_SPLIT_SHIFT 6
152#define STRTAB_BASE_CFG_SPLIT_MASK 0x1f
153#define STRTAB_BASE_CFG_FMT_SHIFT 16
154#define STRTAB_BASE_CFG_FMT_MASK 0x3
155#define STRTAB_BASE_CFG_FMT_LINEAR (0 << STRTAB_BASE_CFG_FMT_SHIFT)
156#define STRTAB_BASE_CFG_FMT_2LVL (1 << STRTAB_BASE_CFG_FMT_SHIFT)
157
158#define ARM_SMMU_CMDQ_BASE 0x90
159#define ARM_SMMU_CMDQ_PROD 0x98
160#define ARM_SMMU_CMDQ_CONS 0x9c
161
162#define ARM_SMMU_EVTQ_BASE 0xa0
163#define ARM_SMMU_EVTQ_PROD 0x100a8
164#define ARM_SMMU_EVTQ_CONS 0x100ac
165#define ARM_SMMU_EVTQ_IRQ_CFG0 0xb0
166#define ARM_SMMU_EVTQ_IRQ_CFG1 0xb8
167#define ARM_SMMU_EVTQ_IRQ_CFG2 0xbc
168
169#define ARM_SMMU_PRIQ_BASE 0xc0
170#define ARM_SMMU_PRIQ_PROD 0x100c8
171#define ARM_SMMU_PRIQ_CONS 0x100cc
172#define ARM_SMMU_PRIQ_IRQ_CFG0 0xd0
173#define ARM_SMMU_PRIQ_IRQ_CFG1 0xd8
174#define ARM_SMMU_PRIQ_IRQ_CFG2 0xdc
175
176/* Common MSI config fields */
48ec83bc
WD
177#define MSI_CFG0_ADDR_SHIFT 2
178#define MSI_CFG0_ADDR_MASK 0x3fffffffffffUL
ec11d63c
MZ
179#define MSI_CFG2_SH_SHIFT 4
180#define MSI_CFG2_SH_NSH (0UL << MSI_CFG2_SH_SHIFT)
181#define MSI_CFG2_SH_OSH (2UL << MSI_CFG2_SH_SHIFT)
182#define MSI_CFG2_SH_ISH (3UL << MSI_CFG2_SH_SHIFT)
183#define MSI_CFG2_MEMATTR_SHIFT 0
184#define MSI_CFG2_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG2_MEMATTR_SHIFT)
48ec83bc
WD
185
186#define Q_IDX(q, p) ((p) & ((1 << (q)->max_n_shift) - 1))
187#define Q_WRP(q, p) ((p) & (1 << (q)->max_n_shift))
188#define Q_OVERFLOW_FLAG (1 << 31)
189#define Q_OVF(q, p) ((p) & Q_OVERFLOW_FLAG)
190#define Q_ENT(q, p) ((q)->base + \
191 Q_IDX(q, p) * (q)->ent_dwords)
192
193#define Q_BASE_RWA (1UL << 62)
194#define Q_BASE_ADDR_SHIFT 5
195#define Q_BASE_ADDR_MASK 0xfffffffffffUL
196#define Q_BASE_LOG2SIZE_SHIFT 0
197#define Q_BASE_LOG2SIZE_MASK 0x1fUL
198
199/*
200 * Stream table.
201 *
202 * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
e2f4c233
ZL
203 * 2lvl: 128k L1 entries,
204 * 256 lazy entries per table (each table covers a PCI bus)
48ec83bc 205 */
e2f4c233 206#define STRTAB_L1_SZ_SHIFT 20
48ec83bc
WD
207#define STRTAB_SPLIT 8
208
209#define STRTAB_L1_DESC_DWORDS 1
210#define STRTAB_L1_DESC_SPAN_SHIFT 0
211#define STRTAB_L1_DESC_SPAN_MASK 0x1fUL
212#define STRTAB_L1_DESC_L2PTR_SHIFT 6
213#define STRTAB_L1_DESC_L2PTR_MASK 0x3ffffffffffUL
214
215#define STRTAB_STE_DWORDS 8
216#define STRTAB_STE_0_V (1UL << 0)
217#define STRTAB_STE_0_CFG_SHIFT 1
218#define STRTAB_STE_0_CFG_MASK 0x7UL
219#define STRTAB_STE_0_CFG_ABORT (0UL << STRTAB_STE_0_CFG_SHIFT)
220#define STRTAB_STE_0_CFG_BYPASS (4UL << STRTAB_STE_0_CFG_SHIFT)
221#define STRTAB_STE_0_CFG_S1_TRANS (5UL << STRTAB_STE_0_CFG_SHIFT)
222#define STRTAB_STE_0_CFG_S2_TRANS (6UL << STRTAB_STE_0_CFG_SHIFT)
223
224#define STRTAB_STE_0_S1FMT_SHIFT 4
225#define STRTAB_STE_0_S1FMT_LINEAR (0UL << STRTAB_STE_0_S1FMT_SHIFT)
226#define STRTAB_STE_0_S1CTXPTR_SHIFT 6
227#define STRTAB_STE_0_S1CTXPTR_MASK 0x3ffffffffffUL
228#define STRTAB_STE_0_S1CDMAX_SHIFT 59
229#define STRTAB_STE_0_S1CDMAX_MASK 0x1fUL
230
231#define STRTAB_STE_1_S1C_CACHE_NC 0UL
232#define STRTAB_STE_1_S1C_CACHE_WBRA 1UL
233#define STRTAB_STE_1_S1C_CACHE_WT 2UL
234#define STRTAB_STE_1_S1C_CACHE_WB 3UL
235#define STRTAB_STE_1_S1C_SH_NSH 0UL
236#define STRTAB_STE_1_S1C_SH_OSH 2UL
237#define STRTAB_STE_1_S1C_SH_ISH 3UL
238#define STRTAB_STE_1_S1CIR_SHIFT 2
239#define STRTAB_STE_1_S1COR_SHIFT 4
240#define STRTAB_STE_1_S1CSH_SHIFT 6
241
242#define STRTAB_STE_1_S1STALLD (1UL << 27)
243
244#define STRTAB_STE_1_EATS_ABT 0UL
245#define STRTAB_STE_1_EATS_TRANS 1UL
246#define STRTAB_STE_1_EATS_S1CHK 2UL
247#define STRTAB_STE_1_EATS_SHIFT 28
248
249#define STRTAB_STE_1_STRW_NSEL1 0UL
250#define STRTAB_STE_1_STRW_EL2 2UL
251#define STRTAB_STE_1_STRW_SHIFT 30
252
253#define STRTAB_STE_2_S2VMID_SHIFT 0
254#define STRTAB_STE_2_S2VMID_MASK 0xffffUL
255#define STRTAB_STE_2_VTCR_SHIFT 32
256#define STRTAB_STE_2_VTCR_MASK 0x7ffffUL
257#define STRTAB_STE_2_S2AA64 (1UL << 51)
258#define STRTAB_STE_2_S2ENDI (1UL << 52)
259#define STRTAB_STE_2_S2PTW (1UL << 54)
260#define STRTAB_STE_2_S2R (1UL << 58)
261
262#define STRTAB_STE_3_S2TTB_SHIFT 4
263#define STRTAB_STE_3_S2TTB_MASK 0xfffffffffffUL
264
265/* Context descriptor (stage-1 only) */
266#define CTXDESC_CD_DWORDS 8
267#define CTXDESC_CD_0_TCR_T0SZ_SHIFT 0
268#define ARM64_TCR_T0SZ_SHIFT 0
269#define ARM64_TCR_T0SZ_MASK 0x1fUL
270#define CTXDESC_CD_0_TCR_TG0_SHIFT 6
271#define ARM64_TCR_TG0_SHIFT 14
272#define ARM64_TCR_TG0_MASK 0x3UL
273#define CTXDESC_CD_0_TCR_IRGN0_SHIFT 8
5d58c620 274#define ARM64_TCR_IRGN0_SHIFT 8
48ec83bc
WD
275#define ARM64_TCR_IRGN0_MASK 0x3UL
276#define CTXDESC_CD_0_TCR_ORGN0_SHIFT 10
5d58c620 277#define ARM64_TCR_ORGN0_SHIFT 10
48ec83bc
WD
278#define ARM64_TCR_ORGN0_MASK 0x3UL
279#define CTXDESC_CD_0_TCR_SH0_SHIFT 12
280#define ARM64_TCR_SH0_SHIFT 12
281#define ARM64_TCR_SH0_MASK 0x3UL
282#define CTXDESC_CD_0_TCR_EPD0_SHIFT 14
283#define ARM64_TCR_EPD0_SHIFT 7
284#define ARM64_TCR_EPD0_MASK 0x1UL
285#define CTXDESC_CD_0_TCR_EPD1_SHIFT 30
286#define ARM64_TCR_EPD1_SHIFT 23
287#define ARM64_TCR_EPD1_MASK 0x1UL
288
289#define CTXDESC_CD_0_ENDI (1UL << 15)
290#define CTXDESC_CD_0_V (1UL << 31)
291
292#define CTXDESC_CD_0_TCR_IPS_SHIFT 32
293#define ARM64_TCR_IPS_SHIFT 32
294#define ARM64_TCR_IPS_MASK 0x7UL
295#define CTXDESC_CD_0_TCR_TBI0_SHIFT 38
296#define ARM64_TCR_TBI0_SHIFT 37
297#define ARM64_TCR_TBI0_MASK 0x1UL
298
299#define CTXDESC_CD_0_AA64 (1UL << 41)
300#define CTXDESC_CD_0_R (1UL << 45)
301#define CTXDESC_CD_0_A (1UL << 46)
302#define CTXDESC_CD_0_ASET_SHIFT 47
303#define CTXDESC_CD_0_ASET_SHARED (0UL << CTXDESC_CD_0_ASET_SHIFT)
304#define CTXDESC_CD_0_ASET_PRIVATE (1UL << CTXDESC_CD_0_ASET_SHIFT)
305#define CTXDESC_CD_0_ASID_SHIFT 48
306#define CTXDESC_CD_0_ASID_MASK 0xffffUL
307
308#define CTXDESC_CD_1_TTB0_SHIFT 4
309#define CTXDESC_CD_1_TTB0_MASK 0xfffffffffffUL
310
311#define CTXDESC_CD_3_MAIR_SHIFT 0
312
313/* Convert between AArch64 (CPU) TCR format and SMMU CD format */
314#define ARM_SMMU_TCR2CD(tcr, fld) \
315 (((tcr) >> ARM64_TCR_##fld##_SHIFT & ARM64_TCR_##fld##_MASK) \
316 << CTXDESC_CD_0_TCR_##fld##_SHIFT)
317
318/* Command queue */
319#define CMDQ_ENT_DWORDS 2
320#define CMDQ_MAX_SZ_SHIFT 8
321
322#define CMDQ_ERR_SHIFT 24
323#define CMDQ_ERR_MASK 0x7f
324#define CMDQ_ERR_CERROR_NONE_IDX 0
325#define CMDQ_ERR_CERROR_ILL_IDX 1
326#define CMDQ_ERR_CERROR_ABT_IDX 2
327
328#define CMDQ_0_OP_SHIFT 0
329#define CMDQ_0_OP_MASK 0xffUL
330#define CMDQ_0_SSV (1UL << 11)
331
332#define CMDQ_PREFETCH_0_SID_SHIFT 32
333#define CMDQ_PREFETCH_1_SIZE_SHIFT 0
334#define CMDQ_PREFETCH_1_ADDR_MASK ~0xfffUL
335
336#define CMDQ_CFGI_0_SID_SHIFT 32
337#define CMDQ_CFGI_0_SID_MASK 0xffffffffUL
338#define CMDQ_CFGI_1_LEAF (1UL << 0)
339#define CMDQ_CFGI_1_RANGE_SHIFT 0
340#define CMDQ_CFGI_1_RANGE_MASK 0x1fUL
341
342#define CMDQ_TLBI_0_VMID_SHIFT 32
343#define CMDQ_TLBI_0_ASID_SHIFT 48
344#define CMDQ_TLBI_1_LEAF (1UL << 0)
345#define CMDQ_TLBI_1_ADDR_MASK ~0xfffUL
346
347#define CMDQ_PRI_0_SSID_SHIFT 12
348#define CMDQ_PRI_0_SSID_MASK 0xfffffUL
349#define CMDQ_PRI_0_SID_SHIFT 32
350#define CMDQ_PRI_0_SID_MASK 0xffffffffUL
351#define CMDQ_PRI_1_GRPID_SHIFT 0
352#define CMDQ_PRI_1_GRPID_MASK 0x1ffUL
353#define CMDQ_PRI_1_RESP_SHIFT 12
354#define CMDQ_PRI_1_RESP_DENY (0UL << CMDQ_PRI_1_RESP_SHIFT)
355#define CMDQ_PRI_1_RESP_FAIL (1UL << CMDQ_PRI_1_RESP_SHIFT)
356#define CMDQ_PRI_1_RESP_SUCC (2UL << CMDQ_PRI_1_RESP_SHIFT)
357
358#define CMDQ_SYNC_0_CS_SHIFT 12
359#define CMDQ_SYNC_0_CS_NONE (0UL << CMDQ_SYNC_0_CS_SHIFT)
360#define CMDQ_SYNC_0_CS_SEV (2UL << CMDQ_SYNC_0_CS_SHIFT)
361
362/* Event queue */
363#define EVTQ_ENT_DWORDS 4
364#define EVTQ_MAX_SZ_SHIFT 7
365
366#define EVTQ_0_ID_SHIFT 0
367#define EVTQ_0_ID_MASK 0xffUL
368
369/* PRI queue */
370#define PRIQ_ENT_DWORDS 2
371#define PRIQ_MAX_SZ_SHIFT 8
372
373#define PRIQ_0_SID_SHIFT 0
374#define PRIQ_0_SID_MASK 0xffffffffUL
375#define PRIQ_0_SSID_SHIFT 32
376#define PRIQ_0_SSID_MASK 0xfffffUL
377#define PRIQ_0_OF (1UL << 57)
378#define PRIQ_0_PERM_PRIV (1UL << 58)
379#define PRIQ_0_PERM_EXEC (1UL << 59)
380#define PRIQ_0_PERM_READ (1UL << 60)
381#define PRIQ_0_PERM_WRITE (1UL << 61)
382#define PRIQ_0_PRG_LAST (1UL << 62)
383#define PRIQ_0_SSID_V (1UL << 63)
384
385#define PRIQ_1_PRG_IDX_SHIFT 0
386#define PRIQ_1_PRG_IDX_MASK 0x1ffUL
387#define PRIQ_1_ADDR_SHIFT 12
388#define PRIQ_1_ADDR_MASK 0xfffffffffffffUL
389
390/* High-level queue structures */
391#define ARM_SMMU_POLL_TIMEOUT_US 100
392
393static bool disable_bypass;
394module_param_named(disable_bypass, disable_bypass, bool, S_IRUGO);
395MODULE_PARM_DESC(disable_bypass,
396 "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.");
397
398enum pri_resp {
399 PRI_RESP_DENY,
400 PRI_RESP_FAIL,
401 PRI_RESP_SUCC,
402};
403
404struct arm_smmu_cmdq_ent {
405 /* Common fields */
406 u8 opcode;
407 bool substream_valid;
408
409 /* Command-specific fields */
410 union {
411 #define CMDQ_OP_PREFETCH_CFG 0x1
412 struct {
413 u32 sid;
414 u8 size;
415 u64 addr;
416 } prefetch;
417
418 #define CMDQ_OP_CFGI_STE 0x3
419 #define CMDQ_OP_CFGI_ALL 0x4
420 struct {
421 u32 sid;
422 union {
423 bool leaf;
424 u8 span;
425 };
426 } cfgi;
427
428 #define CMDQ_OP_TLBI_NH_ASID 0x11
429 #define CMDQ_OP_TLBI_NH_VA 0x12
430 #define CMDQ_OP_TLBI_EL2_ALL 0x20
431 #define CMDQ_OP_TLBI_S12_VMALL 0x28
432 #define CMDQ_OP_TLBI_S2_IPA 0x2a
433 #define CMDQ_OP_TLBI_NSNH_ALL 0x30
434 struct {
435 u16 asid;
436 u16 vmid;
437 bool leaf;
438 u64 addr;
439 } tlbi;
440
441 #define CMDQ_OP_PRI_RESP 0x41
442 struct {
443 u32 sid;
444 u32 ssid;
445 u16 grpid;
446 enum pri_resp resp;
447 } pri;
448
449 #define CMDQ_OP_CMD_SYNC 0x46
450 };
451};
452
453struct arm_smmu_queue {
454 int irq; /* Wired interrupt */
455
456 __le64 *base;
457 dma_addr_t base_dma;
458 u64 q_base;
459
460 size_t ent_dwords;
461 u32 max_n_shift;
462 u32 prod;
463 u32 cons;
464
465 u32 __iomem *prod_reg;
466 u32 __iomem *cons_reg;
467};
468
469struct arm_smmu_cmdq {
470 struct arm_smmu_queue q;
471 spinlock_t lock;
472};
473
474struct arm_smmu_evtq {
475 struct arm_smmu_queue q;
476 u32 max_stalls;
477};
478
479struct arm_smmu_priq {
480 struct arm_smmu_queue q;
481};
482
483/* High-level stream table and context descriptor structures */
484struct arm_smmu_strtab_l1_desc {
485 u8 span;
486
487 __le64 *l2ptr;
488 dma_addr_t l2ptr_dma;
489};
490
491struct arm_smmu_s1_cfg {
492 __le64 *cdptr;
493 dma_addr_t cdptr_dma;
494
495 struct arm_smmu_ctx_desc {
496 u16 asid;
497 u64 ttbr;
498 u64 tcr;
499 u64 mair;
500 } cd;
501};
502
503struct arm_smmu_s2_cfg {
504 u16 vmid;
505 u64 vttbr;
506 u64 vtcr;
507};
508
509struct arm_smmu_strtab_ent {
510 bool valid;
511
512 bool bypass; /* Overrides s1/s2 config */
513 struct arm_smmu_s1_cfg *s1_cfg;
514 struct arm_smmu_s2_cfg *s2_cfg;
515};
516
517struct arm_smmu_strtab_cfg {
518 __le64 *strtab;
519 dma_addr_t strtab_dma;
520 struct arm_smmu_strtab_l1_desc *l1_desc;
521 unsigned int num_l1_ents;
522
523 u64 strtab_base;
524 u32 strtab_base_cfg;
525};
526
527/* An SMMUv3 instance */
528struct arm_smmu_device {
529 struct device *dev;
530 void __iomem *base;
531
532#define ARM_SMMU_FEAT_2_LVL_STRTAB (1 << 0)
533#define ARM_SMMU_FEAT_2_LVL_CDTAB (1 << 1)
534#define ARM_SMMU_FEAT_TT_LE (1 << 2)
535#define ARM_SMMU_FEAT_TT_BE (1 << 3)
536#define ARM_SMMU_FEAT_PRI (1 << 4)
537#define ARM_SMMU_FEAT_ATS (1 << 5)
538#define ARM_SMMU_FEAT_SEV (1 << 6)
539#define ARM_SMMU_FEAT_MSI (1 << 7)
540#define ARM_SMMU_FEAT_COHERENCY (1 << 8)
541#define ARM_SMMU_FEAT_TRANS_S1 (1 << 9)
542#define ARM_SMMU_FEAT_TRANS_S2 (1 << 10)
543#define ARM_SMMU_FEAT_STALLS (1 << 11)
544#define ARM_SMMU_FEAT_HYP (1 << 12)
545 u32 features;
546
5e92946c
ZL
547#define ARM_SMMU_OPT_SKIP_PREFETCH (1 << 0)
548 u32 options;
549
48ec83bc
WD
550 struct arm_smmu_cmdq cmdq;
551 struct arm_smmu_evtq evtq;
552 struct arm_smmu_priq priq;
553
554 int gerr_irq;
555
556 unsigned long ias; /* IPA */
557 unsigned long oas; /* PA */
558
559#define ARM_SMMU_MAX_ASIDS (1 << 16)
560 unsigned int asid_bits;
561 DECLARE_BITMAP(asid_map, ARM_SMMU_MAX_ASIDS);
562
563#define ARM_SMMU_MAX_VMIDS (1 << 16)
564 unsigned int vmid_bits;
565 DECLARE_BITMAP(vmid_map, ARM_SMMU_MAX_VMIDS);
566
567 unsigned int ssid_bits;
568 unsigned int sid_bits;
569
570 struct arm_smmu_strtab_cfg strtab_cfg;
571 struct list_head list;
572};
573
574/* SMMU private data for an IOMMU group */
575struct arm_smmu_group {
576 struct arm_smmu_device *smmu;
577 struct arm_smmu_domain *domain;
578 int num_sids;
579 u32 *sids;
580 struct arm_smmu_strtab_ent ste;
581};
582
583/* SMMU private data for an IOMMU domain */
584enum arm_smmu_domain_stage {
585 ARM_SMMU_DOMAIN_S1 = 0,
586 ARM_SMMU_DOMAIN_S2,
587 ARM_SMMU_DOMAIN_NESTED,
588};
589
590struct arm_smmu_domain {
591 struct arm_smmu_device *smmu;
592 struct mutex init_mutex; /* Protects smmu pointer */
593
594 struct io_pgtable_ops *pgtbl_ops;
595 spinlock_t pgtbl_lock;
596
597 enum arm_smmu_domain_stage stage;
598 union {
599 struct arm_smmu_s1_cfg s1_cfg;
600 struct arm_smmu_s2_cfg s2_cfg;
601 };
602
603 struct iommu_domain domain;
604};
605
606/* Our list of SMMU instances */
607static DEFINE_SPINLOCK(arm_smmu_devices_lock);
608static LIST_HEAD(arm_smmu_devices);
609
5e92946c
ZL
610struct arm_smmu_option_prop {
611 u32 opt;
612 const char *prop;
613};
614
615static struct arm_smmu_option_prop arm_smmu_options[] = {
616 { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
617 { 0, NULL},
618};
619
48ec83bc
WD
620static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
621{
622 return container_of(dom, struct arm_smmu_domain, domain);
623}
624
5e92946c
ZL
625static void parse_driver_options(struct arm_smmu_device *smmu)
626{
627 int i = 0;
628
629 do {
630 if (of_property_read_bool(smmu->dev->of_node,
631 arm_smmu_options[i].prop)) {
632 smmu->options |= arm_smmu_options[i].opt;
633 dev_notice(smmu->dev, "option %s\n",
634 arm_smmu_options[i].prop);
635 }
636 } while (arm_smmu_options[++i].opt);
637}
638
48ec83bc
WD
639/* Low-level queue manipulation functions */
640static bool queue_full(struct arm_smmu_queue *q)
641{
642 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
643 Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
644}
645
646static bool queue_empty(struct arm_smmu_queue *q)
647{
648 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
649 Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
650}
651
652static void queue_sync_cons(struct arm_smmu_queue *q)
653{
654 q->cons = readl_relaxed(q->cons_reg);
655}
656
657static void queue_inc_cons(struct arm_smmu_queue *q)
658{
659 u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
660
661 q->cons = Q_OVF(q, q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
662 writel(q->cons, q->cons_reg);
663}
664
665static int queue_sync_prod(struct arm_smmu_queue *q)
666{
667 int ret = 0;
668 u32 prod = readl_relaxed(q->prod_reg);
669
670 if (Q_OVF(q, prod) != Q_OVF(q, q->prod))
671 ret = -EOVERFLOW;
672
673 q->prod = prod;
674 return ret;
675}
676
677static void queue_inc_prod(struct arm_smmu_queue *q)
678{
679 u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + 1;
680
681 q->prod = Q_OVF(q, q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
682 writel(q->prod, q->prod_reg);
683}
684
685static bool __queue_cons_before(struct arm_smmu_queue *q, u32 until)
686{
687 if (Q_WRP(q, q->cons) == Q_WRP(q, until))
688 return Q_IDX(q, q->cons) < Q_IDX(q, until);
689
690 return Q_IDX(q, q->cons) >= Q_IDX(q, until);
691}
692
693static int queue_poll_cons(struct arm_smmu_queue *q, u32 until, bool wfe)
694{
695 ktime_t timeout = ktime_add_us(ktime_get(), ARM_SMMU_POLL_TIMEOUT_US);
696
697 while (queue_sync_cons(q), __queue_cons_before(q, until)) {
698 if (ktime_compare(ktime_get(), timeout) > 0)
699 return -ETIMEDOUT;
700
701 if (wfe) {
702 wfe();
703 } else {
704 cpu_relax();
705 udelay(1);
706 }
707 }
708
709 return 0;
710}
711
712static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
713{
714 int i;
715
716 for (i = 0; i < n_dwords; ++i)
717 *dst++ = cpu_to_le64(*src++);
718}
719
720static int queue_insert_raw(struct arm_smmu_queue *q, u64 *ent)
721{
722 if (queue_full(q))
723 return -ENOSPC;
724
725 queue_write(Q_ENT(q, q->prod), ent, q->ent_dwords);
726 queue_inc_prod(q);
727 return 0;
728}
729
730static void queue_read(__le64 *dst, u64 *src, size_t n_dwords)
731{
732 int i;
733
734 for (i = 0; i < n_dwords; ++i)
735 *dst++ = le64_to_cpu(*src++);
736}
737
738static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent)
739{
740 if (queue_empty(q))
741 return -EAGAIN;
742
743 queue_read(ent, Q_ENT(q, q->cons), q->ent_dwords);
744 queue_inc_cons(q);
745 return 0;
746}
747
748/* High-level queue accessors */
749static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
750{
751 memset(cmd, 0, CMDQ_ENT_DWORDS << 3);
752 cmd[0] |= (ent->opcode & CMDQ_0_OP_MASK) << CMDQ_0_OP_SHIFT;
753
754 switch (ent->opcode) {
755 case CMDQ_OP_TLBI_EL2_ALL:
756 case CMDQ_OP_TLBI_NSNH_ALL:
757 break;
758 case CMDQ_OP_PREFETCH_CFG:
759 cmd[0] |= (u64)ent->prefetch.sid << CMDQ_PREFETCH_0_SID_SHIFT;
760 cmd[1] |= ent->prefetch.size << CMDQ_PREFETCH_1_SIZE_SHIFT;
761 cmd[1] |= ent->prefetch.addr & CMDQ_PREFETCH_1_ADDR_MASK;
762 break;
763 case CMDQ_OP_CFGI_STE:
764 cmd[0] |= (u64)ent->cfgi.sid << CMDQ_CFGI_0_SID_SHIFT;
765 cmd[1] |= ent->cfgi.leaf ? CMDQ_CFGI_1_LEAF : 0;
766 break;
767 case CMDQ_OP_CFGI_ALL:
768 /* Cover the entire SID range */
769 cmd[1] |= CMDQ_CFGI_1_RANGE_MASK << CMDQ_CFGI_1_RANGE_SHIFT;
770 break;
771 case CMDQ_OP_TLBI_NH_VA:
772 cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
773 /* Fallthrough */
774 case CMDQ_OP_TLBI_S2_IPA:
775 cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
776 cmd[1] |= ent->tlbi.leaf ? CMDQ_TLBI_1_LEAF : 0;
777 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_ADDR_MASK;
778 break;
779 case CMDQ_OP_TLBI_NH_ASID:
780 cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
781 /* Fallthrough */
782 case CMDQ_OP_TLBI_S12_VMALL:
783 cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
784 break;
785 case CMDQ_OP_PRI_RESP:
786 cmd[0] |= ent->substream_valid ? CMDQ_0_SSV : 0;
787 cmd[0] |= ent->pri.ssid << CMDQ_PRI_0_SSID_SHIFT;
788 cmd[0] |= (u64)ent->pri.sid << CMDQ_PRI_0_SID_SHIFT;
789 cmd[1] |= ent->pri.grpid << CMDQ_PRI_1_GRPID_SHIFT;
790 switch (ent->pri.resp) {
791 case PRI_RESP_DENY:
792 cmd[1] |= CMDQ_PRI_1_RESP_DENY;
793 break;
794 case PRI_RESP_FAIL:
795 cmd[1] |= CMDQ_PRI_1_RESP_FAIL;
796 break;
797 case PRI_RESP_SUCC:
798 cmd[1] |= CMDQ_PRI_1_RESP_SUCC;
799 break;
800 default:
801 return -EINVAL;
802 }
803 break;
804 case CMDQ_OP_CMD_SYNC:
805 cmd[0] |= CMDQ_SYNC_0_CS_SEV;
806 break;
807 default:
808 return -ENOENT;
809 }
810
811 return 0;
812}
813
814static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu)
815{
816 static const char *cerror_str[] = {
817 [CMDQ_ERR_CERROR_NONE_IDX] = "No error",
818 [CMDQ_ERR_CERROR_ILL_IDX] = "Illegal command",
819 [CMDQ_ERR_CERROR_ABT_IDX] = "Abort on command fetch",
820 };
821
822 int i;
823 u64 cmd[CMDQ_ENT_DWORDS];
824 struct arm_smmu_queue *q = &smmu->cmdq.q;
825 u32 cons = readl_relaxed(q->cons_reg);
826 u32 idx = cons >> CMDQ_ERR_SHIFT & CMDQ_ERR_MASK;
827 struct arm_smmu_cmdq_ent cmd_sync = {
828 .opcode = CMDQ_OP_CMD_SYNC,
829 };
830
831 dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons,
832 cerror_str[idx]);
833
834 switch (idx) {
835 case CMDQ_ERR_CERROR_ILL_IDX:
836 break;
837 case CMDQ_ERR_CERROR_ABT_IDX:
838 dev_err(smmu->dev, "retrying command fetch\n");
839 case CMDQ_ERR_CERROR_NONE_IDX:
840 return;
841 }
842
843 /*
844 * We may have concurrent producers, so we need to be careful
845 * not to touch any of the shadow cmdq state.
846 */
847 queue_read(cmd, Q_ENT(q, idx), q->ent_dwords);
848 dev_err(smmu->dev, "skipping command in error state:\n");
849 for (i = 0; i < ARRAY_SIZE(cmd); ++i)
850 dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
851
852 /* Convert the erroneous command into a CMD_SYNC */
853 if (arm_smmu_cmdq_build_cmd(cmd, &cmd_sync)) {
854 dev_err(smmu->dev, "failed to convert to CMD_SYNC\n");
855 return;
856 }
857
858 queue_write(cmd, Q_ENT(q, idx), q->ent_dwords);
859}
860
861static void arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
862 struct arm_smmu_cmdq_ent *ent)
863{
864 u32 until;
865 u64 cmd[CMDQ_ENT_DWORDS];
866 bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
867 struct arm_smmu_queue *q = &smmu->cmdq.q;
868
869 if (arm_smmu_cmdq_build_cmd(cmd, ent)) {
870 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
871 ent->opcode);
872 return;
873 }
874
875 spin_lock(&smmu->cmdq.lock);
876 while (until = q->prod + 1, queue_insert_raw(q, cmd) == -ENOSPC) {
877 /*
878 * Keep the queue locked, otherwise the producer could wrap
879 * twice and we could see a future consumer pointer that looks
880 * like it's behind us.
881 */
882 if (queue_poll_cons(q, until, wfe))
883 dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
884 }
885
886 if (ent->opcode == CMDQ_OP_CMD_SYNC && queue_poll_cons(q, until, wfe))
887 dev_err_ratelimited(smmu->dev, "CMD_SYNC timeout\n");
888 spin_unlock(&smmu->cmdq.lock);
889}
890
891/* Context descriptor manipulation functions */
892static u64 arm_smmu_cpu_tcr_to_cd(u64 tcr)
893{
894 u64 val = 0;
895
896 /* Repack the TCR. Just care about TTBR0 for now */
897 val |= ARM_SMMU_TCR2CD(tcr, T0SZ);
898 val |= ARM_SMMU_TCR2CD(tcr, TG0);
899 val |= ARM_SMMU_TCR2CD(tcr, IRGN0);
900 val |= ARM_SMMU_TCR2CD(tcr, ORGN0);
901 val |= ARM_SMMU_TCR2CD(tcr, SH0);
902 val |= ARM_SMMU_TCR2CD(tcr, EPD0);
903 val |= ARM_SMMU_TCR2CD(tcr, EPD1);
904 val |= ARM_SMMU_TCR2CD(tcr, IPS);
905 val |= ARM_SMMU_TCR2CD(tcr, TBI0);
906
907 return val;
908}
909
910static void arm_smmu_write_ctx_desc(struct arm_smmu_device *smmu,
911 struct arm_smmu_s1_cfg *cfg)
912{
913 u64 val;
914
915 /*
916 * We don't need to issue any invalidation here, as we'll invalidate
917 * the STE when installing the new entry anyway.
918 */
919 val = arm_smmu_cpu_tcr_to_cd(cfg->cd.tcr) |
920#ifdef __BIG_ENDIAN
921 CTXDESC_CD_0_ENDI |
922#endif
923 CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET_PRIVATE |
924 CTXDESC_CD_0_AA64 | (u64)cfg->cd.asid << CTXDESC_CD_0_ASID_SHIFT |
925 CTXDESC_CD_0_V;
926 cfg->cdptr[0] = cpu_to_le64(val);
927
928 val = cfg->cd.ttbr & CTXDESC_CD_1_TTB0_MASK << CTXDESC_CD_1_TTB0_SHIFT;
929 cfg->cdptr[1] = cpu_to_le64(val);
930
931 cfg->cdptr[3] = cpu_to_le64(cfg->cd.mair << CTXDESC_CD_3_MAIR_SHIFT);
932}
933
934/* Stream table manipulation functions */
935static void
936arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc)
937{
938 u64 val = 0;
939
940 val |= (desc->span & STRTAB_L1_DESC_SPAN_MASK)
941 << STRTAB_L1_DESC_SPAN_SHIFT;
942 val |= desc->l2ptr_dma &
943 STRTAB_L1_DESC_L2PTR_MASK << STRTAB_L1_DESC_L2PTR_SHIFT;
944
945 *dst = cpu_to_le64(val);
946}
947
948static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid)
949{
950 struct arm_smmu_cmdq_ent cmd = {
951 .opcode = CMDQ_OP_CFGI_STE,
952 .cfgi = {
953 .sid = sid,
954 .leaf = true,
955 },
956 };
957
958 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
959 cmd.opcode = CMDQ_OP_CMD_SYNC;
960 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
961}
962
963static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
964 __le64 *dst, struct arm_smmu_strtab_ent *ste)
965{
966 /*
967 * This is hideously complicated, but we only really care about
968 * three cases at the moment:
969 *
970 * 1. Invalid (all zero) -> bypass (init)
971 * 2. Bypass -> translation (attach)
972 * 3. Translation -> bypass (detach)
973 *
974 * Given that we can't update the STE atomically and the SMMU
975 * doesn't read the thing in a defined order, that leaves us
976 * with the following maintenance requirements:
977 *
978 * 1. Update Config, return (init time STEs aren't live)
979 * 2. Write everything apart from dword 0, sync, write dword 0, sync
980 * 3. Update Config, sync
981 */
982 u64 val = le64_to_cpu(dst[0]);
983 bool ste_live = false;
984 struct arm_smmu_cmdq_ent prefetch_cmd = {
985 .opcode = CMDQ_OP_PREFETCH_CFG,
986 .prefetch = {
987 .sid = sid,
988 },
989 };
990
991 if (val & STRTAB_STE_0_V) {
992 u64 cfg;
993
994 cfg = val & STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT;
995 switch (cfg) {
996 case STRTAB_STE_0_CFG_BYPASS:
997 break;
998 case STRTAB_STE_0_CFG_S1_TRANS:
999 case STRTAB_STE_0_CFG_S2_TRANS:
1000 ste_live = true;
1001 break;
1002 default:
1003 BUG(); /* STE corruption */
1004 }
1005 }
1006
1007 /* Nuke the existing Config, as we're going to rewrite it */
1008 val &= ~(STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT);
1009
1010 if (ste->valid)
1011 val |= STRTAB_STE_0_V;
1012 else
1013 val &= ~STRTAB_STE_0_V;
1014
1015 if (ste->bypass) {
1016 val |= disable_bypass ? STRTAB_STE_0_CFG_ABORT
1017 : STRTAB_STE_0_CFG_BYPASS;
1018 dst[0] = cpu_to_le64(val);
1019 dst[2] = 0; /* Nuke the VMID */
1020 if (ste_live)
1021 arm_smmu_sync_ste_for_sid(smmu, sid);
1022 return;
1023 }
1024
1025 if (ste->s1_cfg) {
1026 BUG_ON(ste_live);
1027 dst[1] = cpu_to_le64(
1028 STRTAB_STE_1_S1C_CACHE_WBRA
1029 << STRTAB_STE_1_S1CIR_SHIFT |
1030 STRTAB_STE_1_S1C_CACHE_WBRA
1031 << STRTAB_STE_1_S1COR_SHIFT |
1032 STRTAB_STE_1_S1C_SH_ISH << STRTAB_STE_1_S1CSH_SHIFT |
1033 STRTAB_STE_1_S1STALLD |
1034#ifdef CONFIG_PCI_ATS
1035 STRTAB_STE_1_EATS_TRANS << STRTAB_STE_1_EATS_SHIFT |
1036#endif
1037 STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT);
1038
1039 val |= (ste->s1_cfg->cdptr_dma & STRTAB_STE_0_S1CTXPTR_MASK
1040 << STRTAB_STE_0_S1CTXPTR_SHIFT) |
1041 STRTAB_STE_0_CFG_S1_TRANS;
1042
1043 }
1044
1045 if (ste->s2_cfg) {
1046 BUG_ON(ste_live);
1047 dst[2] = cpu_to_le64(
1048 ste->s2_cfg->vmid << STRTAB_STE_2_S2VMID_SHIFT |
1049 (ste->s2_cfg->vtcr & STRTAB_STE_2_VTCR_MASK)
1050 << STRTAB_STE_2_VTCR_SHIFT |
1051#ifdef __BIG_ENDIAN
1052 STRTAB_STE_2_S2ENDI |
1053#endif
1054 STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 |
1055 STRTAB_STE_2_S2R);
1056
1057 dst[3] = cpu_to_le64(ste->s2_cfg->vttbr &
1058 STRTAB_STE_3_S2TTB_MASK << STRTAB_STE_3_S2TTB_SHIFT);
1059
1060 val |= STRTAB_STE_0_CFG_S2_TRANS;
1061 }
1062
1063 arm_smmu_sync_ste_for_sid(smmu, sid);
1064 dst[0] = cpu_to_le64(val);
1065 arm_smmu_sync_ste_for_sid(smmu, sid);
1066
1067 /* It's likely that we'll want to use the new STE soon */
5e92946c
ZL
1068 if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
1069 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
48ec83bc
WD
1070}
1071
1072static void arm_smmu_init_bypass_stes(u64 *strtab, unsigned int nent)
1073{
1074 unsigned int i;
1075 struct arm_smmu_strtab_ent ste = {
1076 .valid = true,
1077 .bypass = true,
1078 };
1079
1080 for (i = 0; i < nent; ++i) {
1081 arm_smmu_write_strtab_ent(NULL, -1, strtab, &ste);
1082 strtab += STRTAB_STE_DWORDS;
1083 }
1084}
1085
1086static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1087{
1088 size_t size;
1089 void *strtab;
1090 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1091 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[sid >> STRTAB_SPLIT];
1092
1093 if (desc->l2ptr)
1094 return 0;
1095
1096 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
69146e7b 1097 strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
48ec83bc
WD
1098
1099 desc->span = STRTAB_SPLIT + 1;
1100 desc->l2ptr = dma_zalloc_coherent(smmu->dev, size, &desc->l2ptr_dma,
1101 GFP_KERNEL);
1102 if (!desc->l2ptr) {
1103 dev_err(smmu->dev,
1104 "failed to allocate l2 stream table for SID %u\n",
1105 sid);
1106 return -ENOMEM;
1107 }
1108
1109 arm_smmu_init_bypass_stes(desc->l2ptr, 1 << STRTAB_SPLIT);
1110 arm_smmu_write_strtab_l1_desc(strtab, desc);
1111 return 0;
1112}
1113
1114/* IRQ and event handlers */
1115static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
1116{
1117 int i;
1118 struct arm_smmu_device *smmu = dev;
1119 struct arm_smmu_queue *q = &smmu->evtq.q;
1120 u64 evt[EVTQ_ENT_DWORDS];
1121
1122 while (!queue_remove_raw(q, evt)) {
1123 u8 id = evt[0] >> EVTQ_0_ID_SHIFT & EVTQ_0_ID_MASK;
1124
1125 dev_info(smmu->dev, "event 0x%02x received:\n", id);
1126 for (i = 0; i < ARRAY_SIZE(evt); ++i)
1127 dev_info(smmu->dev, "\t0x%016llx\n",
1128 (unsigned long long)evt[i]);
1129 }
1130
1131 /* Sync our overflow flag, as we believe we're up to speed */
1132 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1133 return IRQ_HANDLED;
1134}
1135
1136static irqreturn_t arm_smmu_evtq_handler(int irq, void *dev)
1137{
1138 irqreturn_t ret = IRQ_WAKE_THREAD;
1139 struct arm_smmu_device *smmu = dev;
1140 struct arm_smmu_queue *q = &smmu->evtq.q;
1141
1142 /*
1143 * Not much we can do on overflow, so scream and pretend we're
1144 * trying harder.
1145 */
1146 if (queue_sync_prod(q) == -EOVERFLOW)
1147 dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
1148 else if (queue_empty(q))
1149 ret = IRQ_NONE;
1150
1151 return ret;
1152}
1153
1154static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
1155{
1156 struct arm_smmu_device *smmu = dev;
1157 struct arm_smmu_queue *q = &smmu->priq.q;
1158 u64 evt[PRIQ_ENT_DWORDS];
1159
1160 while (!queue_remove_raw(q, evt)) {
1161 u32 sid, ssid;
1162 u16 grpid;
1163 bool ssv, last;
1164
1165 sid = evt[0] >> PRIQ_0_SID_SHIFT & PRIQ_0_SID_MASK;
1166 ssv = evt[0] & PRIQ_0_SSID_V;
1167 ssid = ssv ? evt[0] >> PRIQ_0_SSID_SHIFT & PRIQ_0_SSID_MASK : 0;
1168 last = evt[0] & PRIQ_0_PRG_LAST;
1169 grpid = evt[1] >> PRIQ_1_PRG_IDX_SHIFT & PRIQ_1_PRG_IDX_MASK;
1170
1171 dev_info(smmu->dev, "unexpected PRI request received:\n");
1172 dev_info(smmu->dev,
1173 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
1174 sid, ssid, grpid, last ? "L" : "",
1175 evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
1176 evt[0] & PRIQ_0_PERM_READ ? "R" : "",
1177 evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
1178 evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
1179 evt[1] & PRIQ_1_ADDR_MASK << PRIQ_1_ADDR_SHIFT);
1180
1181 if (last) {
1182 struct arm_smmu_cmdq_ent cmd = {
1183 .opcode = CMDQ_OP_PRI_RESP,
1184 .substream_valid = ssv,
1185 .pri = {
1186 .sid = sid,
1187 .ssid = ssid,
1188 .grpid = grpid,
1189 .resp = PRI_RESP_DENY,
1190 },
1191 };
1192
1193 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1194 }
1195 }
1196
1197 /* Sync our overflow flag, as we believe we're up to speed */
1198 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1199 return IRQ_HANDLED;
1200}
1201
1202static irqreturn_t arm_smmu_priq_handler(int irq, void *dev)
1203{
1204 irqreturn_t ret = IRQ_WAKE_THREAD;
1205 struct arm_smmu_device *smmu = dev;
1206 struct arm_smmu_queue *q = &smmu->priq.q;
1207
1208 /* PRIQ overflow indicates a programming error */
1209 if (queue_sync_prod(q) == -EOVERFLOW)
1210 dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
1211 else if (queue_empty(q))
1212 ret = IRQ_NONE;
1213
1214 return ret;
1215}
1216
1217static irqreturn_t arm_smmu_cmdq_sync_handler(int irq, void *dev)
1218{
1219 /* We don't actually use CMD_SYNC interrupts for anything */
1220 return IRQ_HANDLED;
1221}
1222
1223static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
1224
1225static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
1226{
1227 u32 gerror, gerrorn;
1228 struct arm_smmu_device *smmu = dev;
1229
1230 gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
1231 gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
1232
1233 gerror ^= gerrorn;
1234 if (!(gerror & GERROR_ERR_MASK))
1235 return IRQ_NONE; /* No errors pending */
1236
1237 dev_warn(smmu->dev,
1238 "unexpected global error reported (0x%08x), this could be serious\n",
1239 gerror);
1240
1241 if (gerror & GERROR_SFM_ERR) {
1242 dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
1243 arm_smmu_device_disable(smmu);
1244 }
1245
1246 if (gerror & GERROR_MSI_GERROR_ABT_ERR)
1247 dev_warn(smmu->dev, "GERROR MSI write aborted\n");
1248
1249 if (gerror & GERROR_MSI_PRIQ_ABT_ERR) {
1250 dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
1251 arm_smmu_priq_handler(irq, smmu->dev);
1252 }
1253
1254 if (gerror & GERROR_MSI_EVTQ_ABT_ERR) {
1255 dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
1256 arm_smmu_evtq_handler(irq, smmu->dev);
1257 }
1258
1259 if (gerror & GERROR_MSI_CMDQ_ABT_ERR) {
1260 dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
1261 arm_smmu_cmdq_sync_handler(irq, smmu->dev);
1262 }
1263
1264 if (gerror & GERROR_PRIQ_ABT_ERR)
1265 dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
1266
1267 if (gerror & GERROR_EVTQ_ABT_ERR)
1268 dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
1269
1270 if (gerror & GERROR_CMDQ_ERR)
1271 arm_smmu_cmdq_skip_err(smmu);
1272
1273 writel(gerror, smmu->base + ARM_SMMU_GERRORN);
1274 return IRQ_HANDLED;
1275}
1276
1277/* IO_PGTABLE API */
1278static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
1279{
1280 struct arm_smmu_cmdq_ent cmd;
1281
1282 cmd.opcode = CMDQ_OP_CMD_SYNC;
1283 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1284}
1285
1286static void arm_smmu_tlb_sync(void *cookie)
1287{
1288 struct arm_smmu_domain *smmu_domain = cookie;
1289 __arm_smmu_tlb_sync(smmu_domain->smmu);
1290}
1291
1292static void arm_smmu_tlb_inv_context(void *cookie)
1293{
1294 struct arm_smmu_domain *smmu_domain = cookie;
1295 struct arm_smmu_device *smmu = smmu_domain->smmu;
1296 struct arm_smmu_cmdq_ent cmd;
1297
1298 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1299 cmd.opcode = CMDQ_OP_TLBI_NH_ASID;
1300 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1301 cmd.tlbi.vmid = 0;
1302 } else {
1303 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL;
1304 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1305 }
1306
1307 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1308 __arm_smmu_tlb_sync(smmu);
1309}
1310
1311static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
1312 bool leaf, void *cookie)
1313{
1314 struct arm_smmu_domain *smmu_domain = cookie;
1315 struct arm_smmu_device *smmu = smmu_domain->smmu;
1316 struct arm_smmu_cmdq_ent cmd = {
1317 .tlbi = {
1318 .leaf = leaf,
1319 .addr = iova,
1320 },
1321 };
1322
1323 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1324 cmd.opcode = CMDQ_OP_TLBI_NH_VA;
1325 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1326 } else {
1327 cmd.opcode = CMDQ_OP_TLBI_S2_IPA;
1328 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1329 }
1330
1331 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1332}
1333
48ec83bc
WD
1334static struct iommu_gather_ops arm_smmu_gather_ops = {
1335 .tlb_flush_all = arm_smmu_tlb_inv_context,
1336 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
1337 .tlb_sync = arm_smmu_tlb_sync,
48ec83bc
WD
1338};
1339
1340/* IOMMU API */
1341static bool arm_smmu_capable(enum iommu_cap cap)
1342{
1343 switch (cap) {
1344 case IOMMU_CAP_CACHE_COHERENCY:
1345 return true;
1346 case IOMMU_CAP_INTR_REMAP:
1347 return true; /* MSIs are just memory writes */
1348 case IOMMU_CAP_NOEXEC:
1349 return true;
1350 default:
1351 return false;
1352 }
1353}
1354
1355static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
1356{
1357 struct arm_smmu_domain *smmu_domain;
1358
1359 if (type != IOMMU_DOMAIN_UNMANAGED)
1360 return NULL;
1361
1362 /*
1363 * Allocate the domain and initialise some of its data structures.
1364 * We can't really do anything meaningful until we've added a
1365 * master.
1366 */
1367 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1368 if (!smmu_domain)
1369 return NULL;
1370
1371 mutex_init(&smmu_domain->init_mutex);
1372 spin_lock_init(&smmu_domain->pgtbl_lock);
1373 return &smmu_domain->domain;
1374}
1375
1376static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
1377{
1378 int idx, size = 1 << span;
1379
1380 do {
1381 idx = find_first_zero_bit(map, size);
1382 if (idx == size)
1383 return -ENOSPC;
1384 } while (test_and_set_bit(idx, map));
1385
1386 return idx;
1387}
1388
1389static void arm_smmu_bitmap_free(unsigned long *map, int idx)
1390{
1391 clear_bit(idx, map);
1392}
1393
1394static void arm_smmu_domain_free(struct iommu_domain *domain)
1395{
1396 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1397 struct arm_smmu_device *smmu = smmu_domain->smmu;
1398
a6e08fb2 1399 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
48ec83bc
WD
1400
1401 /* Free the CD and ASID, if we allocated them */
1402 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1403 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1404
1405 if (cfg->cdptr) {
1406 dma_free_coherent(smmu_domain->smmu->dev,
1407 CTXDESC_CD_DWORDS << 3,
1408 cfg->cdptr,
1409 cfg->cdptr_dma);
1410
1411 arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
1412 }
1413 } else {
1414 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1415 if (cfg->vmid)
1416 arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
1417 }
1418
1419 kfree(smmu_domain);
1420}
1421
1422static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
1423 struct io_pgtable_cfg *pgtbl_cfg)
1424{
1425 int ret;
1426 u16 asid;
1427 struct arm_smmu_device *smmu = smmu_domain->smmu;
1428 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1429
1430 asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
1431 if (IS_ERR_VALUE(asid))
1432 return asid;
1433
1434 cfg->cdptr = dma_zalloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
1435 &cfg->cdptr_dma, GFP_KERNEL);
1436 if (!cfg->cdptr) {
1437 dev_warn(smmu->dev, "failed to allocate context descriptor\n");
1438 goto out_free_asid;
1439 }
1440
1441 cfg->cd.asid = asid;
1442 cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
1443 cfg->cd.tcr = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
1444 cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
1445 return 0;
1446
1447out_free_asid:
1448 arm_smmu_bitmap_free(smmu->asid_map, asid);
1449 return ret;
1450}
1451
1452static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
1453 struct io_pgtable_cfg *pgtbl_cfg)
1454{
1455 u16 vmid;
1456 struct arm_smmu_device *smmu = smmu_domain->smmu;
1457 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1458
1459 vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
1460 if (IS_ERR_VALUE(vmid))
1461 return vmid;
1462
1463 cfg->vmid = vmid;
1464 cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
1465 cfg->vtcr = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
1466 return 0;
1467}
1468
1469static struct iommu_ops arm_smmu_ops;
1470
1471static int arm_smmu_domain_finalise(struct iommu_domain *domain)
1472{
1473 int ret;
1474 unsigned long ias, oas;
1475 enum io_pgtable_fmt fmt;
1476 struct io_pgtable_cfg pgtbl_cfg;
1477 struct io_pgtable_ops *pgtbl_ops;
1478 int (*finalise_stage_fn)(struct arm_smmu_domain *,
1479 struct io_pgtable_cfg *);
1480 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1481 struct arm_smmu_device *smmu = smmu_domain->smmu;
1482
1483 /* Restrict the stage to what we can actually support */
1484 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
1485 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
1486 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
1487 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1488
1489 switch (smmu_domain->stage) {
1490 case ARM_SMMU_DOMAIN_S1:
1491 ias = VA_BITS;
1492 oas = smmu->ias;
1493 fmt = ARM_64_LPAE_S1;
1494 finalise_stage_fn = arm_smmu_domain_finalise_s1;
1495 break;
1496 case ARM_SMMU_DOMAIN_NESTED:
1497 case ARM_SMMU_DOMAIN_S2:
1498 ias = smmu->ias;
1499 oas = smmu->oas;
1500 fmt = ARM_64_LPAE_S2;
1501 finalise_stage_fn = arm_smmu_domain_finalise_s2;
1502 break;
1503 default:
1504 return -EINVAL;
1505 }
1506
1507 pgtbl_cfg = (struct io_pgtable_cfg) {
1508 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
1509 .ias = ias,
1510 .oas = oas,
1511 .tlb = &arm_smmu_gather_ops,
bdc6d973 1512 .iommu_dev = smmu->dev,
48ec83bc
WD
1513 };
1514
1515 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
1516 if (!pgtbl_ops)
1517 return -ENOMEM;
1518
1519 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
1520 smmu_domain->pgtbl_ops = pgtbl_ops;
1521
1522 ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
1523 if (IS_ERR_VALUE(ret))
1524 free_io_pgtable_ops(pgtbl_ops);
1525
1526 return ret;
1527}
1528
1529static struct arm_smmu_group *arm_smmu_group_get(struct device *dev)
1530{
1531 struct iommu_group *group;
1532 struct arm_smmu_group *smmu_group;
1533
1534 group = iommu_group_get(dev);
1535 if (!group)
1536 return NULL;
1537
1538 smmu_group = iommu_group_get_iommudata(group);
1539 iommu_group_put(group);
1540 return smmu_group;
1541}
1542
1543static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
1544{
1545 __le64 *step;
1546 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1547
1548 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1549 struct arm_smmu_strtab_l1_desc *l1_desc;
1550 int idx;
1551
1552 /* Two-level walk */
1553 idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
1554 l1_desc = &cfg->l1_desc[idx];
1555 idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
1556 step = &l1_desc->l2ptr[idx];
1557 } else {
1558 /* Simple linear lookup */
1559 step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
1560 }
1561
1562 return step;
1563}
1564
1565static int arm_smmu_install_ste_for_group(struct arm_smmu_group *smmu_group)
1566{
1567 int i;
1568 struct arm_smmu_domain *smmu_domain = smmu_group->domain;
1569 struct arm_smmu_strtab_ent *ste = &smmu_group->ste;
1570 struct arm_smmu_device *smmu = smmu_group->smmu;
1571
1572 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1573 ste->s1_cfg = &smmu_domain->s1_cfg;
1574 ste->s2_cfg = NULL;
1575 arm_smmu_write_ctx_desc(smmu, ste->s1_cfg);
1576 } else {
1577 ste->s1_cfg = NULL;
1578 ste->s2_cfg = &smmu_domain->s2_cfg;
1579 }
1580
1581 for (i = 0; i < smmu_group->num_sids; ++i) {
1582 u32 sid = smmu_group->sids[i];
1583 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
1584
1585 arm_smmu_write_strtab_ent(smmu, sid, step, ste);
1586 }
1587
1588 return 0;
1589}
1590
1591static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1592{
1593 int ret = 0;
1594 struct arm_smmu_device *smmu;
1595 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1596 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1597
1598 if (!smmu_group)
1599 return -ENOENT;
1600
1601 /* Already attached to a different domain? */
1602 if (smmu_group->domain && smmu_group->domain != smmu_domain)
1603 return -EEXIST;
1604
1605 smmu = smmu_group->smmu;
1606 mutex_lock(&smmu_domain->init_mutex);
1607
1608 if (!smmu_domain->smmu) {
1609 smmu_domain->smmu = smmu;
1610 ret = arm_smmu_domain_finalise(domain);
1611 if (ret) {
1612 smmu_domain->smmu = NULL;
1613 goto out_unlock;
1614 }
1615 } else if (smmu_domain->smmu != smmu) {
1616 dev_err(dev,
1617 "cannot attach to SMMU %s (upstream of %s)\n",
1618 dev_name(smmu_domain->smmu->dev),
1619 dev_name(smmu->dev));
1620 ret = -ENXIO;
1621 goto out_unlock;
1622 }
1623
1624 /* Group already attached to this domain? */
1625 if (smmu_group->domain)
1626 goto out_unlock;
1627
1628 smmu_group->domain = smmu_domain;
1629 smmu_group->ste.bypass = false;
1630
1631 ret = arm_smmu_install_ste_for_group(smmu_group);
1632 if (IS_ERR_VALUE(ret))
1633 smmu_group->domain = NULL;
1634
1635out_unlock:
1636 mutex_unlock(&smmu_domain->init_mutex);
1637 return ret;
1638}
1639
1640static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1641{
1642 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1643 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1644
1645 BUG_ON(!smmu_domain);
1646 BUG_ON(!smmu_group);
1647
1648 mutex_lock(&smmu_domain->init_mutex);
1649 BUG_ON(smmu_group->domain != smmu_domain);
1650
1651 smmu_group->ste.bypass = true;
1652 if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group)))
1653 dev_warn(dev, "failed to install bypass STE\n");
1654
1655 smmu_group->domain = NULL;
1656 mutex_unlock(&smmu_domain->init_mutex);
1657}
1658
1659static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1660 phys_addr_t paddr, size_t size, int prot)
1661{
1662 int ret;
1663 unsigned long flags;
1664 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1665 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1666
1667 if (!ops)
1668 return -ENODEV;
1669
1670 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1671 ret = ops->map(ops, iova, paddr, size, prot);
1672 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1673 return ret;
1674}
1675
1676static size_t
1677arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1678{
1679 size_t ret;
1680 unsigned long flags;
1681 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1682 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1683
1684 if (!ops)
1685 return 0;
1686
1687 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1688 ret = ops->unmap(ops, iova, size);
1689 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1690 return ret;
1691}
1692
1693static phys_addr_t
1694arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1695{
1696 phys_addr_t ret;
1697 unsigned long flags;
1698 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1699 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1700
1701 if (!ops)
1702 return 0;
1703
1704 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1705 ret = ops->iova_to_phys(ops, iova);
1706 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1707
1708 return ret;
1709}
1710
1711static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *sidp)
1712{
1713 *(u32 *)sidp = alias;
1714 return 0; /* Continue walking */
1715}
1716
1717static void __arm_smmu_release_pci_iommudata(void *data)
1718{
1719 kfree(data);
1720}
1721
1722static struct arm_smmu_device *arm_smmu_get_for_pci_dev(struct pci_dev *pdev)
1723{
1724 struct device_node *of_node;
1725 struct arm_smmu_device *curr, *smmu = NULL;
1726 struct pci_bus *bus = pdev->bus;
1727
1728 /* Walk up to the root bus */
1729 while (!pci_is_root_bus(bus))
1730 bus = bus->parent;
1731
1732 /* Follow the "iommus" phandle from the host controller */
1733 of_node = of_parse_phandle(bus->bridge->parent->of_node, "iommus", 0);
1734 if (!of_node)
1735 return NULL;
1736
1737 /* See if we can find an SMMU corresponding to the phandle */
1738 spin_lock(&arm_smmu_devices_lock);
1739 list_for_each_entry(curr, &arm_smmu_devices, list) {
1740 if (curr->dev->of_node == of_node) {
1741 smmu = curr;
1742 break;
1743 }
1744 }
1745 spin_unlock(&arm_smmu_devices_lock);
1746 of_node_put(of_node);
1747 return smmu;
1748}
1749
1750static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
1751{
1752 unsigned long limit = smmu->strtab_cfg.num_l1_ents;
1753
1754 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
1755 limit *= 1UL << STRTAB_SPLIT;
1756
1757 return sid < limit;
1758}
1759
1760static int arm_smmu_add_device(struct device *dev)
1761{
1762 int i, ret;
1763 u32 sid, *sids;
1764 struct pci_dev *pdev;
1765 struct iommu_group *group;
1766 struct arm_smmu_group *smmu_group;
1767 struct arm_smmu_device *smmu;
1768
1769 /* We only support PCI, for now */
1770 if (!dev_is_pci(dev))
1771 return -ENODEV;
1772
1773 pdev = to_pci_dev(dev);
1774 group = iommu_group_get_for_dev(dev);
1775 if (IS_ERR(group))
1776 return PTR_ERR(group);
1777
1778 smmu_group = iommu_group_get_iommudata(group);
1779 if (!smmu_group) {
1780 smmu = arm_smmu_get_for_pci_dev(pdev);
1781 if (!smmu) {
1782 ret = -ENOENT;
1783 goto out_put_group;
1784 }
1785
1786 smmu_group = kzalloc(sizeof(*smmu_group), GFP_KERNEL);
1787 if (!smmu_group) {
1788 ret = -ENOMEM;
1789 goto out_put_group;
1790 }
1791
1792 smmu_group->ste.valid = true;
1793 smmu_group->smmu = smmu;
1794 iommu_group_set_iommudata(group, smmu_group,
1795 __arm_smmu_release_pci_iommudata);
1796 } else {
1797 smmu = smmu_group->smmu;
1798 }
1799
1800 /* Assume SID == RID until firmware tells us otherwise */
1801 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1802 for (i = 0; i < smmu_group->num_sids; ++i) {
1803 /* If we already know about this SID, then we're done */
1804 if (smmu_group->sids[i] == sid)
1805 return 0;
1806 }
1807
1808 /* Check the SID is in range of the SMMU and our stream table */
1809 if (!arm_smmu_sid_in_range(smmu, sid)) {
1810 ret = -ERANGE;
1811 goto out_put_group;
1812 }
1813
1814 /* Ensure l2 strtab is initialised */
1815 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1816 ret = arm_smmu_init_l2_strtab(smmu, sid);
1817 if (ret)
1818 goto out_put_group;
1819 }
1820
1821 /* Resize the SID array for the group */
1822 smmu_group->num_sids++;
1823 sids = krealloc(smmu_group->sids, smmu_group->num_sids * sizeof(*sids),
1824 GFP_KERNEL);
1825 if (!sids) {
1826 smmu_group->num_sids--;
1827 ret = -ENOMEM;
1828 goto out_put_group;
1829 }
1830
1831 /* Add the new SID */
1832 sids[smmu_group->num_sids - 1] = sid;
1833 smmu_group->sids = sids;
1834 return 0;
1835
1836out_put_group:
1837 iommu_group_put(group);
1838 return ret;
1839}
1840
1841static void arm_smmu_remove_device(struct device *dev)
1842{
1843 iommu_group_remove_device(dev);
1844}
1845
1846static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1847 enum iommu_attr attr, void *data)
1848{
1849 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1850
1851 switch (attr) {
1852 case DOMAIN_ATTR_NESTING:
1853 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1854 return 0;
1855 default:
1856 return -ENODEV;
1857 }
1858}
1859
1860static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1861 enum iommu_attr attr, void *data)
1862{
1863 int ret = 0;
1864 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1865
1866 mutex_lock(&smmu_domain->init_mutex);
1867
1868 switch (attr) {
1869 case DOMAIN_ATTR_NESTING:
1870 if (smmu_domain->smmu) {
1871 ret = -EPERM;
1872 goto out_unlock;
1873 }
1874
1875 if (*(int *)data)
1876 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1877 else
1878 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1879
1880 break;
1881 default:
1882 ret = -ENODEV;
1883 }
1884
1885out_unlock:
1886 mutex_unlock(&smmu_domain->init_mutex);
1887 return ret;
1888}
1889
1890static struct iommu_ops arm_smmu_ops = {
1891 .capable = arm_smmu_capable,
1892 .domain_alloc = arm_smmu_domain_alloc,
1893 .domain_free = arm_smmu_domain_free,
1894 .attach_dev = arm_smmu_attach_dev,
1895 .detach_dev = arm_smmu_detach_dev,
1896 .map = arm_smmu_map,
1897 .unmap = arm_smmu_unmap,
1898 .iova_to_phys = arm_smmu_iova_to_phys,
1899 .add_device = arm_smmu_add_device,
1900 .remove_device = arm_smmu_remove_device,
1901 .domain_get_attr = arm_smmu_domain_get_attr,
1902 .domain_set_attr = arm_smmu_domain_set_attr,
1903 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1904};
1905
1906/* Probing and initialisation functions */
1907static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
1908 struct arm_smmu_queue *q,
1909 unsigned long prod_off,
1910 unsigned long cons_off,
1911 size_t dwords)
1912{
1913 size_t qsz = ((1 << q->max_n_shift) * dwords) << 3;
1914
1915 q->base = dma_alloc_coherent(smmu->dev, qsz, &q->base_dma, GFP_KERNEL);
1916 if (!q->base) {
1917 dev_err(smmu->dev, "failed to allocate queue (0x%zx bytes)\n",
1918 qsz);
1919 return -ENOMEM;
1920 }
1921
1922 q->prod_reg = smmu->base + prod_off;
1923 q->cons_reg = smmu->base + cons_off;
1924 q->ent_dwords = dwords;
1925
1926 q->q_base = Q_BASE_RWA;
1927 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK << Q_BASE_ADDR_SHIFT;
1928 q->q_base |= (q->max_n_shift & Q_BASE_LOG2SIZE_MASK)
1929 << Q_BASE_LOG2SIZE_SHIFT;
1930
1931 q->prod = q->cons = 0;
1932 return 0;
1933}
1934
1935static void arm_smmu_free_one_queue(struct arm_smmu_device *smmu,
1936 struct arm_smmu_queue *q)
1937{
1938 size_t qsz = ((1 << q->max_n_shift) * q->ent_dwords) << 3;
1939
1940 dma_free_coherent(smmu->dev, qsz, q->base, q->base_dma);
1941}
1942
1943static void arm_smmu_free_queues(struct arm_smmu_device *smmu)
1944{
1945 arm_smmu_free_one_queue(smmu, &smmu->cmdq.q);
1946 arm_smmu_free_one_queue(smmu, &smmu->evtq.q);
1947
1948 if (smmu->features & ARM_SMMU_FEAT_PRI)
1949 arm_smmu_free_one_queue(smmu, &smmu->priq.q);
1950}
1951
1952static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
1953{
1954 int ret;
1955
1956 /* cmdq */
1957 spin_lock_init(&smmu->cmdq.lock);
1958 ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
1959 ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS);
1960 if (ret)
1961 goto out;
1962
1963 /* evtq */
1964 ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
1965 ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS);
1966 if (ret)
1967 goto out_free_cmdq;
1968
1969 /* priq */
1970 if (!(smmu->features & ARM_SMMU_FEAT_PRI))
1971 return 0;
1972
1973 ret = arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
1974 ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS);
1975 if (ret)
1976 goto out_free_evtq;
1977
1978 return 0;
1979
1980out_free_evtq:
1981 arm_smmu_free_one_queue(smmu, &smmu->evtq.q);
1982out_free_cmdq:
1983 arm_smmu_free_one_queue(smmu, &smmu->cmdq.q);
1984out:
1985 return ret;
1986}
1987
1988static void arm_smmu_free_l2_strtab(struct arm_smmu_device *smmu)
1989{
1990 int i;
1991 size_t size;
1992 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1993
1994 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
1995 for (i = 0; i < cfg->num_l1_ents; ++i) {
1996 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[i];
1997
1998 if (!desc->l2ptr)
1999 continue;
2000
2001 dma_free_coherent(smmu->dev, size, desc->l2ptr,
2002 desc->l2ptr_dma);
2003 }
2004}
2005
2006static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
2007{
2008 unsigned int i;
2009 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2010 size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
2011 void *strtab = smmu->strtab_cfg.strtab;
2012
2013 cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
2014 if (!cfg->l1_desc) {
2015 dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
2016 return -ENOMEM;
2017 }
2018
2019 for (i = 0; i < cfg->num_l1_ents; ++i) {
2020 arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
2021 strtab += STRTAB_L1_DESC_DWORDS << 3;
2022 }
2023
2024 return 0;
2025}
2026
2027static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
2028{
2029 void *strtab;
2030 u64 reg;
d2e88e7c 2031 u32 size, l1size;
48ec83bc
WD
2032 int ret;
2033 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2034
28c8b404
WD
2035 /*
2036 * If we can resolve everything with a single L2 table, then we
2037 * just need a single L1 descriptor. Otherwise, calculate the L1
2038 * size, capped to the SIDSIZE.
2039 */
2040 if (smmu->sid_bits < STRTAB_SPLIT) {
2041 size = 0;
2042 } else {
2043 size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
2044 size = min(size, smmu->sid_bits - STRTAB_SPLIT);
2045 }
d2e88e7c
WD
2046 cfg->num_l1_ents = 1 << size;
2047
2048 size += STRTAB_SPLIT;
2049 if (size < smmu->sid_bits)
48ec83bc
WD
2050 dev_warn(smmu->dev,
2051 "2-level strtab only covers %u/%u bits of SID\n",
d2e88e7c 2052 size, smmu->sid_bits);
48ec83bc 2053
d2e88e7c
WD
2054 l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
2055 strtab = dma_zalloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
48ec83bc
WD
2056 GFP_KERNEL);
2057 if (!strtab) {
2058 dev_err(smmu->dev,
2059 "failed to allocate l1 stream table (%u bytes)\n",
2060 size);
2061 return -ENOMEM;
2062 }
2063 cfg->strtab = strtab;
2064
2065 /* Configure strtab_base_cfg for 2 levels */
2066 reg = STRTAB_BASE_CFG_FMT_2LVL;
2067 reg |= (size & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2068 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2069 reg |= (STRTAB_SPLIT & STRTAB_BASE_CFG_SPLIT_MASK)
2070 << STRTAB_BASE_CFG_SPLIT_SHIFT;
2071 cfg->strtab_base_cfg = reg;
2072
2073 ret = arm_smmu_init_l1_strtab(smmu);
2074 if (ret)
2075 dma_free_coherent(smmu->dev,
d2e88e7c 2076 l1size,
48ec83bc
WD
2077 strtab,
2078 cfg->strtab_dma);
2079 return ret;
2080}
2081
2082static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
2083{
2084 void *strtab;
2085 u64 reg;
2086 u32 size;
2087 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2088
2089 size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
2090 strtab = dma_zalloc_coherent(smmu->dev, size, &cfg->strtab_dma,
2091 GFP_KERNEL);
2092 if (!strtab) {
2093 dev_err(smmu->dev,
2094 "failed to allocate linear stream table (%u bytes)\n",
2095 size);
2096 return -ENOMEM;
2097 }
2098 cfg->strtab = strtab;
2099 cfg->num_l1_ents = 1 << smmu->sid_bits;
2100
2101 /* Configure strtab_base_cfg for a linear table covering all SIDs */
2102 reg = STRTAB_BASE_CFG_FMT_LINEAR;
2103 reg |= (smmu->sid_bits & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2104 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2105 cfg->strtab_base_cfg = reg;
2106
2107 arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
2108 return 0;
2109}
2110
2111static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
2112{
2113 u64 reg;
2114 int ret;
2115
2116 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
2117 ret = arm_smmu_init_strtab_2lvl(smmu);
2118 else
2119 ret = arm_smmu_init_strtab_linear(smmu);
2120
2121 if (ret)
2122 return ret;
2123
2124 /* Set the strtab base address */
2125 reg = smmu->strtab_cfg.strtab_dma &
2126 STRTAB_BASE_ADDR_MASK << STRTAB_BASE_ADDR_SHIFT;
2127 reg |= STRTAB_BASE_RA;
2128 smmu->strtab_cfg.strtab_base = reg;
2129
2130 /* Allocate the first VMID for stage-2 bypass STEs */
2131 set_bit(0, smmu->vmid_map);
2132 return 0;
2133}
2134
2135static void arm_smmu_free_strtab(struct arm_smmu_device *smmu)
2136{
2137 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2138 u32 size = cfg->num_l1_ents;
2139
2140 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
2141 arm_smmu_free_l2_strtab(smmu);
2142 size *= STRTAB_L1_DESC_DWORDS << 3;
2143 } else {
2144 size *= STRTAB_STE_DWORDS * 3;
2145 }
2146
2147 dma_free_coherent(smmu->dev, size, cfg->strtab, cfg->strtab_dma);
2148}
2149
2150static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
2151{
2152 int ret;
2153
2154 ret = arm_smmu_init_queues(smmu);
2155 if (ret)
2156 return ret;
2157
2158 ret = arm_smmu_init_strtab(smmu);
2159 if (ret)
2160 goto out_free_queues;
2161
2162 return 0;
2163
2164out_free_queues:
2165 arm_smmu_free_queues(smmu);
2166 return ret;
2167}
2168
2169static void arm_smmu_free_structures(struct arm_smmu_device *smmu)
2170{
2171 arm_smmu_free_strtab(smmu);
2172 arm_smmu_free_queues(smmu);
2173}
2174
2175static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
2176 unsigned int reg_off, unsigned int ack_off)
2177{
2178 u32 reg;
2179
2180 writel_relaxed(val, smmu->base + reg_off);
2181 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
2182 1, ARM_SMMU_POLL_TIMEOUT_US);
2183}
2184
2185static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
2186{
2187 int ret, irq;
ccd6385d 2188 u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
48ec83bc
WD
2189
2190 /* Disable IRQs first */
2191 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
2192 ARM_SMMU_IRQ_CTRLACK);
2193 if (ret) {
2194 dev_err(smmu->dev, "failed to disable irqs\n");
2195 return ret;
2196 }
2197
2198 /* Clear the MSI address regs */
2199 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
2200 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
2201
2202 /* Request wired interrupt lines */
2203 irq = smmu->evtq.q.irq;
2204 if (irq) {
2205 ret = devm_request_threaded_irq(smmu->dev, irq,
2206 arm_smmu_evtq_handler,
2207 arm_smmu_evtq_thread,
2208 0, "arm-smmu-v3-evtq", smmu);
2209 if (IS_ERR_VALUE(ret))
2210 dev_warn(smmu->dev, "failed to enable evtq irq\n");
2211 }
2212
2213 irq = smmu->cmdq.q.irq;
2214 if (irq) {
2215 ret = devm_request_irq(smmu->dev, irq,
2216 arm_smmu_cmdq_sync_handler, 0,
2217 "arm-smmu-v3-cmdq-sync", smmu);
2218 if (IS_ERR_VALUE(ret))
2219 dev_warn(smmu->dev, "failed to enable cmdq-sync irq\n");
2220 }
2221
2222 irq = smmu->gerr_irq;
2223 if (irq) {
2224 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
2225 0, "arm-smmu-v3-gerror", smmu);
2226 if (IS_ERR_VALUE(ret))
2227 dev_warn(smmu->dev, "failed to enable gerror irq\n");
2228 }
2229
2230 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2231 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
2232
2233 irq = smmu->priq.q.irq;
2234 if (irq) {
2235 ret = devm_request_threaded_irq(smmu->dev, irq,
2236 arm_smmu_priq_handler,
2237 arm_smmu_priq_thread,
2238 0, "arm-smmu-v3-priq",
2239 smmu);
2240 if (IS_ERR_VALUE(ret))
2241 dev_warn(smmu->dev,
2242 "failed to enable priq irq\n");
ccd6385d
MZ
2243 else
2244 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
48ec83bc
WD
2245 }
2246 }
2247
2248 /* Enable interrupt generation on the SMMU */
ccd6385d 2249 ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
48ec83bc
WD
2250 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
2251 if (ret)
2252 dev_warn(smmu->dev, "failed to enable irqs\n");
2253
2254 return 0;
2255}
2256
2257static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
2258{
2259 int ret;
2260
2261 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
2262 if (ret)
2263 dev_err(smmu->dev, "failed to clear cr0\n");
2264
2265 return ret;
2266}
2267
2268static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
2269{
2270 int ret;
2271 u32 reg, enables;
2272 struct arm_smmu_cmdq_ent cmd;
2273
2274 /* Clear CR0 and sync (disables SMMU and queue processing) */
2275 reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
2276 if (reg & CR0_SMMUEN)
2277 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
2278
2279 ret = arm_smmu_device_disable(smmu);
2280 if (ret)
2281 return ret;
2282
2283 /* CR1 (table and queue memory attributes) */
2284 reg = (CR1_SH_ISH << CR1_TABLE_SH_SHIFT) |
2285 (CR1_CACHE_WB << CR1_TABLE_OC_SHIFT) |
2286 (CR1_CACHE_WB << CR1_TABLE_IC_SHIFT) |
2287 (CR1_SH_ISH << CR1_QUEUE_SH_SHIFT) |
2288 (CR1_CACHE_WB << CR1_QUEUE_OC_SHIFT) |
2289 (CR1_CACHE_WB << CR1_QUEUE_IC_SHIFT);
2290 writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
2291
2292 /* CR2 (random crap) */
2293 reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
2294 writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
2295
2296 /* Stream table */
2297 writeq_relaxed(smmu->strtab_cfg.strtab_base,
2298 smmu->base + ARM_SMMU_STRTAB_BASE);
2299 writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
2300 smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
2301
2302 /* Command queue */
2303 writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
2304 writel_relaxed(smmu->cmdq.q.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
2305 writel_relaxed(smmu->cmdq.q.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
2306
2307 enables = CR0_CMDQEN;
2308 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2309 ARM_SMMU_CR0ACK);
2310 if (ret) {
2311 dev_err(smmu->dev, "failed to enable command queue\n");
2312 return ret;
2313 }
2314
2315 /* Invalidate any cached configuration */
2316 cmd.opcode = CMDQ_OP_CFGI_ALL;
2317 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2318 cmd.opcode = CMDQ_OP_CMD_SYNC;
2319 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2320
2321 /* Invalidate any stale TLB entries */
2322 if (smmu->features & ARM_SMMU_FEAT_HYP) {
2323 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
2324 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2325 }
2326
2327 cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
2328 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2329 cmd.opcode = CMDQ_OP_CMD_SYNC;
2330 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2331
2332 /* Event queue */
2333 writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
2334 writel_relaxed(smmu->evtq.q.prod, smmu->base + ARM_SMMU_EVTQ_PROD);
2335 writel_relaxed(smmu->evtq.q.cons, smmu->base + ARM_SMMU_EVTQ_CONS);
2336
2337 enables |= CR0_EVTQEN;
2338 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2339 ARM_SMMU_CR0ACK);
2340 if (ret) {
2341 dev_err(smmu->dev, "failed to enable event queue\n");
2342 return ret;
2343 }
2344
2345 /* PRI queue */
2346 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2347 writeq_relaxed(smmu->priq.q.q_base,
2348 smmu->base + ARM_SMMU_PRIQ_BASE);
2349 writel_relaxed(smmu->priq.q.prod,
2350 smmu->base + ARM_SMMU_PRIQ_PROD);
2351 writel_relaxed(smmu->priq.q.cons,
2352 smmu->base + ARM_SMMU_PRIQ_CONS);
2353
2354 enables |= CR0_PRIQEN;
2355 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2356 ARM_SMMU_CR0ACK);
2357 if (ret) {
2358 dev_err(smmu->dev, "failed to enable PRI queue\n");
2359 return ret;
2360 }
2361 }
2362
2363 ret = arm_smmu_setup_irqs(smmu);
2364 if (ret) {
2365 dev_err(smmu->dev, "failed to setup irqs\n");
2366 return ret;
2367 }
2368
2369 /* Enable the SMMU interface */
2370 enables |= CR0_SMMUEN;
2371 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2372 ARM_SMMU_CR0ACK);
2373 if (ret) {
2374 dev_err(smmu->dev, "failed to enable SMMU interface\n");
2375 return ret;
2376 }
2377
2378 return 0;
2379}
2380
2381static int arm_smmu_device_probe(struct arm_smmu_device *smmu)
2382{
2383 u32 reg;
2384 bool coherent;
2385 unsigned long pgsize_bitmap = 0;
2386
2387 /* IDR0 */
2388 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
2389
2390 /* 2-level structures */
2391 if ((reg & IDR0_ST_LVL_MASK << IDR0_ST_LVL_SHIFT) == IDR0_ST_LVL_2LVL)
2392 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
2393
2394 if (reg & IDR0_CD2L)
2395 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
2396
2397 /*
2398 * Translation table endianness.
2399 * We currently require the same endianness as the CPU, but this
2400 * could be changed later by adding a new IO_PGTABLE_QUIRK.
2401 */
2402 switch (reg & IDR0_TTENDIAN_MASK << IDR0_TTENDIAN_SHIFT) {
2403 case IDR0_TTENDIAN_MIXED:
2404 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
2405 break;
2406#ifdef __BIG_ENDIAN
2407 case IDR0_TTENDIAN_BE:
2408 smmu->features |= ARM_SMMU_FEAT_TT_BE;
2409 break;
2410#else
2411 case IDR0_TTENDIAN_LE:
2412 smmu->features |= ARM_SMMU_FEAT_TT_LE;
2413 break;
2414#endif
2415 default:
2416 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
2417 return -ENXIO;
2418 }
2419
2420 /* Boolean feature flags */
2421 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
2422 smmu->features |= ARM_SMMU_FEAT_PRI;
2423
2424 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
2425 smmu->features |= ARM_SMMU_FEAT_ATS;
2426
2427 if (reg & IDR0_SEV)
2428 smmu->features |= ARM_SMMU_FEAT_SEV;
2429
2430 if (reg & IDR0_MSI)
2431 smmu->features |= ARM_SMMU_FEAT_MSI;
2432
2433 if (reg & IDR0_HYP)
2434 smmu->features |= ARM_SMMU_FEAT_HYP;
2435
2436 /*
2437 * The dma-coherent property is used in preference to the ID
2438 * register, but warn on mismatch.
2439 */
2440 coherent = of_dma_is_coherent(smmu->dev->of_node);
2441 if (coherent)
2442 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2443
2444 if (!!(reg & IDR0_COHACC) != coherent)
2445 dev_warn(smmu->dev, "IDR0.COHACC overridden by dma-coherent property (%s)\n",
2446 coherent ? "true" : "false");
2447
2448 if (reg & IDR0_STALL_MODEL)
2449 smmu->features |= ARM_SMMU_FEAT_STALLS;
2450
2451 if (reg & IDR0_S1P)
2452 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
2453
2454 if (reg & IDR0_S2P)
2455 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
2456
2457 if (!(reg & (IDR0_S1P | IDR0_S2P))) {
2458 dev_err(smmu->dev, "no translation support!\n");
2459 return -ENXIO;
2460 }
2461
2462 /* We only support the AArch64 table format at present */
2463 if ((reg & IDR0_TTF_MASK << IDR0_TTF_SHIFT) < IDR0_TTF_AARCH64) {
2464 dev_err(smmu->dev, "AArch64 table format not supported!\n");
2465 return -ENXIO;
2466 }
2467
2468 /* ASID/VMID sizes */
2469 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
2470 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
2471
2472 /* IDR1 */
2473 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
2474 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
2475 dev_err(smmu->dev, "embedded implementation not supported\n");
2476 return -ENXIO;
2477 }
2478
2479 /* Queue sizes, capped at 4k */
2480 smmu->cmdq.q.max_n_shift = min((u32)CMDQ_MAX_SZ_SHIFT,
2481 reg >> IDR1_CMDQ_SHIFT & IDR1_CMDQ_MASK);
2482 if (!smmu->cmdq.q.max_n_shift) {
2483 /* Odd alignment restrictions on the base, so ignore for now */
2484 dev_err(smmu->dev, "unit-length command queue not supported\n");
2485 return -ENXIO;
2486 }
2487
2488 smmu->evtq.q.max_n_shift = min((u32)EVTQ_MAX_SZ_SHIFT,
2489 reg >> IDR1_EVTQ_SHIFT & IDR1_EVTQ_MASK);
2490 smmu->priq.q.max_n_shift = min((u32)PRIQ_MAX_SZ_SHIFT,
2491 reg >> IDR1_PRIQ_SHIFT & IDR1_PRIQ_MASK);
2492
2493 /* SID/SSID sizes */
2494 smmu->ssid_bits = reg >> IDR1_SSID_SHIFT & IDR1_SSID_MASK;
2495 smmu->sid_bits = reg >> IDR1_SID_SHIFT & IDR1_SID_MASK;
2496
2497 /* IDR5 */
2498 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
2499
2500 /* Maximum number of outstanding stalls */
2501 smmu->evtq.max_stalls = reg >> IDR5_STALL_MAX_SHIFT
2502 & IDR5_STALL_MAX_MASK;
2503
2504 /* Page sizes */
2505 if (reg & IDR5_GRAN64K)
2506 pgsize_bitmap |= SZ_64K | SZ_512M;
2507 if (reg & IDR5_GRAN16K)
2508 pgsize_bitmap |= SZ_16K | SZ_32M;
2509 if (reg & IDR5_GRAN4K)
2510 pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
2511
2512 arm_smmu_ops.pgsize_bitmap &= pgsize_bitmap;
2513
2514 /* Output address size */
2515 switch (reg & IDR5_OAS_MASK << IDR5_OAS_SHIFT) {
2516 case IDR5_OAS_32_BIT:
2517 smmu->oas = 32;
2518 break;
2519 case IDR5_OAS_36_BIT:
2520 smmu->oas = 36;
2521 break;
2522 case IDR5_OAS_40_BIT:
2523 smmu->oas = 40;
2524 break;
2525 case IDR5_OAS_42_BIT:
2526 smmu->oas = 42;
2527 break;
2528 case IDR5_OAS_44_BIT:
2529 smmu->oas = 44;
2530 break;
85430968
WD
2531 default:
2532 dev_info(smmu->dev,
2533 "unknown output address size. Truncating to 48-bit\n");
2534 /* Fallthrough */
48ec83bc
WD
2535 case IDR5_OAS_48_BIT:
2536 smmu->oas = 48;
48ec83bc
WD
2537 }
2538
2539 /* Set the DMA mask for our table walker */
2540 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
2541 dev_warn(smmu->dev,
2542 "failed to set DMA mask for table walker\n");
2543
2544 if (!smmu->ias)
2545 smmu->ias = smmu->oas;
2546
2547 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
2548 smmu->ias, smmu->oas, smmu->features);
2549 return 0;
2550}
2551
2552static int arm_smmu_device_dt_probe(struct platform_device *pdev)
2553{
2554 int irq, ret;
2555 struct resource *res;
2556 struct arm_smmu_device *smmu;
2557 struct device *dev = &pdev->dev;
2558
2559 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2560 if (!smmu) {
2561 dev_err(dev, "failed to allocate arm_smmu_device\n");
2562 return -ENOMEM;
2563 }
2564 smmu->dev = dev;
2565
2566 /* Base address */
2567 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2568 if (resource_size(res) + 1 < SZ_128K) {
2569 dev_err(dev, "MMIO region too small (%pr)\n", res);
2570 return -EINVAL;
2571 }
2572
2573 smmu->base = devm_ioremap_resource(dev, res);
2574 if (IS_ERR(smmu->base))
2575 return PTR_ERR(smmu->base);
2576
2577 /* Interrupt lines */
2578 irq = platform_get_irq_byname(pdev, "eventq");
2579 if (irq > 0)
2580 smmu->evtq.q.irq = irq;
2581
2582 irq = platform_get_irq_byname(pdev, "priq");
2583 if (irq > 0)
2584 smmu->priq.q.irq = irq;
2585
2586 irq = platform_get_irq_byname(pdev, "cmdq-sync");
2587 if (irq > 0)
2588 smmu->cmdq.q.irq = irq;
2589
2590 irq = platform_get_irq_byname(pdev, "gerror");
2591 if (irq > 0)
2592 smmu->gerr_irq = irq;
2593
5e92946c
ZL
2594 parse_driver_options(smmu);
2595
48ec83bc
WD
2596 /* Probe the h/w */
2597 ret = arm_smmu_device_probe(smmu);
2598 if (ret)
2599 return ret;
2600
2601 /* Initialise in-memory data structures */
2602 ret = arm_smmu_init_structures(smmu);
2603 if (ret)
2604 return ret;
2605
2606 /* Reset the device */
2607 ret = arm_smmu_device_reset(smmu);
2608 if (ret)
2609 goto out_free_structures;
2610
2611 /* Record our private device structure */
2612 INIT_LIST_HEAD(&smmu->list);
2613 spin_lock(&arm_smmu_devices_lock);
2614 list_add(&smmu->list, &arm_smmu_devices);
2615 spin_unlock(&arm_smmu_devices_lock);
2616 return 0;
2617
2618out_free_structures:
2619 arm_smmu_free_structures(smmu);
2620 return ret;
2621}
2622
2623static int arm_smmu_device_remove(struct platform_device *pdev)
2624{
2625 struct arm_smmu_device *curr, *smmu = NULL;
2626 struct device *dev = &pdev->dev;
2627
2628 spin_lock(&arm_smmu_devices_lock);
2629 list_for_each_entry(curr, &arm_smmu_devices, list) {
2630 if (curr->dev == dev) {
2631 smmu = curr;
2632 list_del(&smmu->list);
2633 break;
2634 }
2635 }
2636 spin_unlock(&arm_smmu_devices_lock);
2637
2638 if (!smmu)
2639 return -ENODEV;
2640
2641 arm_smmu_device_disable(smmu);
2642 arm_smmu_free_structures(smmu);
2643 return 0;
2644}
2645
2646static struct of_device_id arm_smmu_of_match[] = {
2647 { .compatible = "arm,smmu-v3", },
2648 { },
2649};
2650MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2651
2652static struct platform_driver arm_smmu_driver = {
2653 .driver = {
2654 .name = "arm-smmu-v3",
2655 .of_match_table = of_match_ptr(arm_smmu_of_match),
2656 },
2657 .probe = arm_smmu_device_dt_probe,
2658 .remove = arm_smmu_device_remove,
2659};
2660
2661static int __init arm_smmu_init(void)
2662{
2663 struct device_node *np;
2664 int ret;
2665
2666 np = of_find_matching_node(NULL, arm_smmu_of_match);
2667 if (!np)
2668 return 0;
2669
2670 of_node_put(np);
2671
2672 ret = platform_driver_register(&arm_smmu_driver);
2673 if (ret)
2674 return ret;
2675
2676 return bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2677}
2678
2679static void __exit arm_smmu_exit(void)
2680{
2681 return platform_driver_unregister(&arm_smmu_driver);
2682}
2683
2684subsys_initcall(arm_smmu_init);
2685module_exit(arm_smmu_exit);
2686
2687MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
2688MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2689MODULE_LICENSE("GPL v2");