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