blob: 4f093373f4c30ee8394dc536ff9cfbde53d143e7 [file] [log] [blame]
Will Deacon48ec83b2015-05-27 17:25:59 +01001/*
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)
Marc Zyngierccd63852015-07-15 11:55:18 +0100121#define IRQ_CTRL_PRIQ_IRQEN (1 << 1)
Will Deacon48ec83b2015-05-27 17:25:59 +0100122#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 */
Will Deacon48ec83b2015-05-27 17:25:59 +0100177#define MSI_CFG0_ADDR_SHIFT 2
178#define MSI_CFG0_ADDR_MASK 0x3fffffffffffUL
Marc Zyngierec11d632015-07-15 11:55:19 +0100179#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)
Will Deacon48ec83b2015-05-27 17:25:59 +0100185
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
Zhen Leie2f4c232015-07-07 04:30:17 +0100203 * 2lvl: 128k L1 entries,
204 * 256 lazy entries per table (each table covers a PCI bus)
Will Deacon48ec83b2015-05-27 17:25:59 +0100205 */
Zhen Leie2f4c232015-07-07 04:30:17 +0100206#define STRTAB_L1_SZ_SHIFT 20
Will Deacon48ec83b2015-05-27 17:25:59 +0100207#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
Zhen Lei5d58c622015-06-26 09:32:59 +0100274#define ARM64_TCR_IRGN0_SHIFT 8
Will Deacon48ec83b2015-05-27 17:25:59 +0100275#define ARM64_TCR_IRGN0_MASK 0x3UL
276#define CTXDESC_CD_0_TCR_ORGN0_SHIFT 10
Zhen Lei5d58c622015-06-26 09:32:59 +0100277#define ARM64_TCR_ORGN0_SHIFT 10
Will Deacon48ec83b2015-05-27 17:25:59 +0100278#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
Zhen Lei5e929462015-07-07 04:30:18 +0100547#define ARM_SMMU_OPT_SKIP_PREFETCH (1 << 0)
548 u32 options;
549
Will Deacon48ec83b2015-05-27 17:25:59 +0100550 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
Zhen Lei5e929462015-07-07 04:30:18 +0100610struct 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
Will Deacon48ec83b2015-05-27 17:25:59 +0100620static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
621{
622 return container_of(dom, struct arm_smmu_domain, domain);
623}
624
Zhen Lei5e929462015-07-07 04:30:18 +0100625static 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
Will Deacon48ec83b2015-05-27 17:25:59 +0100639/* 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 */
Zhen Lei5e929462015-07-07 04:30:18 +01001068 if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
1069 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
Will Deacon48ec83b2015-05-27 17:25:59 +01001070}
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);
Zhen Lei69146e72015-06-26 09:32:58 +01001097 strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
Will Deacon48ec83b2015-05-27 17:25:59 +01001098
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
1334static void arm_smmu_flush_pgtable(void *addr, size_t size, void *cookie)
1335{
1336 struct arm_smmu_domain *smmu_domain = cookie;
1337 struct arm_smmu_device *smmu = smmu_domain->smmu;
1338 unsigned long offset = (unsigned long)addr & ~PAGE_MASK;
1339
1340 if (smmu->features & ARM_SMMU_FEAT_COHERENCY) {
1341 dsb(ishst);
1342 } else {
1343 dma_addr_t dma_addr;
1344 struct device *dev = smmu->dev;
1345
1346 dma_addr = dma_map_page(dev, virt_to_page(addr), offset, size,
1347 DMA_TO_DEVICE);
1348
1349 if (dma_mapping_error(dev, dma_addr))
1350 dev_err(dev, "failed to flush pgtable at %p\n", addr);
1351 else
1352 dma_unmap_page(dev, dma_addr, size, DMA_TO_DEVICE);
1353 }
1354}
1355
1356static struct iommu_gather_ops arm_smmu_gather_ops = {
1357 .tlb_flush_all = arm_smmu_tlb_inv_context,
1358 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
1359 .tlb_sync = arm_smmu_tlb_sync,
1360 .flush_pgtable = arm_smmu_flush_pgtable,
1361};
1362
1363/* IOMMU API */
1364static bool arm_smmu_capable(enum iommu_cap cap)
1365{
1366 switch (cap) {
1367 case IOMMU_CAP_CACHE_COHERENCY:
1368 return true;
1369 case IOMMU_CAP_INTR_REMAP:
1370 return true; /* MSIs are just memory writes */
1371 case IOMMU_CAP_NOEXEC:
1372 return true;
1373 default:
1374 return false;
1375 }
1376}
1377
1378static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
1379{
1380 struct arm_smmu_domain *smmu_domain;
1381
1382 if (type != IOMMU_DOMAIN_UNMANAGED)
1383 return NULL;
1384
1385 /*
1386 * Allocate the domain and initialise some of its data structures.
1387 * We can't really do anything meaningful until we've added a
1388 * master.
1389 */
1390 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1391 if (!smmu_domain)
1392 return NULL;
1393
1394 mutex_init(&smmu_domain->init_mutex);
1395 spin_lock_init(&smmu_domain->pgtbl_lock);
1396 return &smmu_domain->domain;
1397}
1398
1399static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
1400{
1401 int idx, size = 1 << span;
1402
1403 do {
1404 idx = find_first_zero_bit(map, size);
1405 if (idx == size)
1406 return -ENOSPC;
1407 } while (test_and_set_bit(idx, map));
1408
1409 return idx;
1410}
1411
1412static void arm_smmu_bitmap_free(unsigned long *map, int idx)
1413{
1414 clear_bit(idx, map);
1415}
1416
1417static void arm_smmu_domain_free(struct iommu_domain *domain)
1418{
1419 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1420 struct arm_smmu_device *smmu = smmu_domain->smmu;
1421
Markus Elfringa6e08fb2015-06-29 17:47:43 +01001422 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon48ec83b2015-05-27 17:25:59 +01001423
1424 /* Free the CD and ASID, if we allocated them */
1425 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1426 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1427
1428 if (cfg->cdptr) {
1429 dma_free_coherent(smmu_domain->smmu->dev,
1430 CTXDESC_CD_DWORDS << 3,
1431 cfg->cdptr,
1432 cfg->cdptr_dma);
1433
1434 arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
1435 }
1436 } else {
1437 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1438 if (cfg->vmid)
1439 arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
1440 }
1441
1442 kfree(smmu_domain);
1443}
1444
1445static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
1446 struct io_pgtable_cfg *pgtbl_cfg)
1447{
1448 int ret;
1449 u16 asid;
1450 struct arm_smmu_device *smmu = smmu_domain->smmu;
1451 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1452
1453 asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
1454 if (IS_ERR_VALUE(asid))
1455 return asid;
1456
1457 cfg->cdptr = dma_zalloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
1458 &cfg->cdptr_dma, GFP_KERNEL);
1459 if (!cfg->cdptr) {
1460 dev_warn(smmu->dev, "failed to allocate context descriptor\n");
1461 goto out_free_asid;
1462 }
1463
1464 cfg->cd.asid = asid;
1465 cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
1466 cfg->cd.tcr = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
1467 cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
1468 return 0;
1469
1470out_free_asid:
1471 arm_smmu_bitmap_free(smmu->asid_map, asid);
1472 return ret;
1473}
1474
1475static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
1476 struct io_pgtable_cfg *pgtbl_cfg)
1477{
1478 u16 vmid;
1479 struct arm_smmu_device *smmu = smmu_domain->smmu;
1480 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1481
1482 vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
1483 if (IS_ERR_VALUE(vmid))
1484 return vmid;
1485
1486 cfg->vmid = vmid;
1487 cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
1488 cfg->vtcr = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
1489 return 0;
1490}
1491
1492static struct iommu_ops arm_smmu_ops;
1493
1494static int arm_smmu_domain_finalise(struct iommu_domain *domain)
1495{
1496 int ret;
1497 unsigned long ias, oas;
1498 enum io_pgtable_fmt fmt;
1499 struct io_pgtable_cfg pgtbl_cfg;
1500 struct io_pgtable_ops *pgtbl_ops;
1501 int (*finalise_stage_fn)(struct arm_smmu_domain *,
1502 struct io_pgtable_cfg *);
1503 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1504 struct arm_smmu_device *smmu = smmu_domain->smmu;
1505
1506 /* Restrict the stage to what we can actually support */
1507 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
1508 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
1509 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
1510 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1511
1512 switch (smmu_domain->stage) {
1513 case ARM_SMMU_DOMAIN_S1:
1514 ias = VA_BITS;
1515 oas = smmu->ias;
1516 fmt = ARM_64_LPAE_S1;
1517 finalise_stage_fn = arm_smmu_domain_finalise_s1;
1518 break;
1519 case ARM_SMMU_DOMAIN_NESTED:
1520 case ARM_SMMU_DOMAIN_S2:
1521 ias = smmu->ias;
1522 oas = smmu->oas;
1523 fmt = ARM_64_LPAE_S2;
1524 finalise_stage_fn = arm_smmu_domain_finalise_s2;
1525 break;
1526 default:
1527 return -EINVAL;
1528 }
1529
1530 pgtbl_cfg = (struct io_pgtable_cfg) {
1531 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
1532 .ias = ias,
1533 .oas = oas,
1534 .tlb = &arm_smmu_gather_ops,
1535 };
1536
1537 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
1538 if (!pgtbl_ops)
1539 return -ENOMEM;
1540
1541 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
1542 smmu_domain->pgtbl_ops = pgtbl_ops;
1543
1544 ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
1545 if (IS_ERR_VALUE(ret))
1546 free_io_pgtable_ops(pgtbl_ops);
1547
1548 return ret;
1549}
1550
1551static struct arm_smmu_group *arm_smmu_group_get(struct device *dev)
1552{
1553 struct iommu_group *group;
1554 struct arm_smmu_group *smmu_group;
1555
1556 group = iommu_group_get(dev);
1557 if (!group)
1558 return NULL;
1559
1560 smmu_group = iommu_group_get_iommudata(group);
1561 iommu_group_put(group);
1562 return smmu_group;
1563}
1564
1565static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
1566{
1567 __le64 *step;
1568 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1569
1570 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1571 struct arm_smmu_strtab_l1_desc *l1_desc;
1572 int idx;
1573
1574 /* Two-level walk */
1575 idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
1576 l1_desc = &cfg->l1_desc[idx];
1577 idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
1578 step = &l1_desc->l2ptr[idx];
1579 } else {
1580 /* Simple linear lookup */
1581 step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
1582 }
1583
1584 return step;
1585}
1586
1587static int arm_smmu_install_ste_for_group(struct arm_smmu_group *smmu_group)
1588{
1589 int i;
1590 struct arm_smmu_domain *smmu_domain = smmu_group->domain;
1591 struct arm_smmu_strtab_ent *ste = &smmu_group->ste;
1592 struct arm_smmu_device *smmu = smmu_group->smmu;
1593
1594 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1595 ste->s1_cfg = &smmu_domain->s1_cfg;
1596 ste->s2_cfg = NULL;
1597 arm_smmu_write_ctx_desc(smmu, ste->s1_cfg);
1598 } else {
1599 ste->s1_cfg = NULL;
1600 ste->s2_cfg = &smmu_domain->s2_cfg;
1601 }
1602
1603 for (i = 0; i < smmu_group->num_sids; ++i) {
1604 u32 sid = smmu_group->sids[i];
1605 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
1606
1607 arm_smmu_write_strtab_ent(smmu, sid, step, ste);
1608 }
1609
1610 return 0;
1611}
1612
1613static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1614{
1615 int ret = 0;
1616 struct arm_smmu_device *smmu;
1617 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1618 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1619
1620 if (!smmu_group)
1621 return -ENOENT;
1622
1623 /* Already attached to a different domain? */
1624 if (smmu_group->domain && smmu_group->domain != smmu_domain)
1625 return -EEXIST;
1626
1627 smmu = smmu_group->smmu;
1628 mutex_lock(&smmu_domain->init_mutex);
1629
1630 if (!smmu_domain->smmu) {
1631 smmu_domain->smmu = smmu;
1632 ret = arm_smmu_domain_finalise(domain);
1633 if (ret) {
1634 smmu_domain->smmu = NULL;
1635 goto out_unlock;
1636 }
1637 } else if (smmu_domain->smmu != smmu) {
1638 dev_err(dev,
1639 "cannot attach to SMMU %s (upstream of %s)\n",
1640 dev_name(smmu_domain->smmu->dev),
1641 dev_name(smmu->dev));
1642 ret = -ENXIO;
1643 goto out_unlock;
1644 }
1645
1646 /* Group already attached to this domain? */
1647 if (smmu_group->domain)
1648 goto out_unlock;
1649
1650 smmu_group->domain = smmu_domain;
1651 smmu_group->ste.bypass = false;
1652
1653 ret = arm_smmu_install_ste_for_group(smmu_group);
1654 if (IS_ERR_VALUE(ret))
1655 smmu_group->domain = NULL;
1656
1657out_unlock:
1658 mutex_unlock(&smmu_domain->init_mutex);
1659 return ret;
1660}
1661
1662static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1663{
1664 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1665 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1666
1667 BUG_ON(!smmu_domain);
1668 BUG_ON(!smmu_group);
1669
1670 mutex_lock(&smmu_domain->init_mutex);
1671 BUG_ON(smmu_group->domain != smmu_domain);
1672
1673 smmu_group->ste.bypass = true;
1674 if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group)))
1675 dev_warn(dev, "failed to install bypass STE\n");
1676
1677 smmu_group->domain = NULL;
1678 mutex_unlock(&smmu_domain->init_mutex);
1679}
1680
1681static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1682 phys_addr_t paddr, size_t size, int prot)
1683{
1684 int ret;
1685 unsigned long flags;
1686 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1687 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1688
1689 if (!ops)
1690 return -ENODEV;
1691
1692 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1693 ret = ops->map(ops, iova, paddr, size, prot);
1694 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1695 return ret;
1696}
1697
1698static size_t
1699arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1700{
1701 size_t ret;
1702 unsigned long flags;
1703 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1704 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1705
1706 if (!ops)
1707 return 0;
1708
1709 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1710 ret = ops->unmap(ops, iova, size);
1711 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1712 return ret;
1713}
1714
1715static phys_addr_t
1716arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1717{
1718 phys_addr_t ret;
1719 unsigned long flags;
1720 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1721 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1722
1723 if (!ops)
1724 return 0;
1725
1726 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1727 ret = ops->iova_to_phys(ops, iova);
1728 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1729
1730 return ret;
1731}
1732
1733static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *sidp)
1734{
1735 *(u32 *)sidp = alias;
1736 return 0; /* Continue walking */
1737}
1738
1739static void __arm_smmu_release_pci_iommudata(void *data)
1740{
1741 kfree(data);
1742}
1743
1744static struct arm_smmu_device *arm_smmu_get_for_pci_dev(struct pci_dev *pdev)
1745{
1746 struct device_node *of_node;
1747 struct arm_smmu_device *curr, *smmu = NULL;
1748 struct pci_bus *bus = pdev->bus;
1749
1750 /* Walk up to the root bus */
1751 while (!pci_is_root_bus(bus))
1752 bus = bus->parent;
1753
1754 /* Follow the "iommus" phandle from the host controller */
1755 of_node = of_parse_phandle(bus->bridge->parent->of_node, "iommus", 0);
1756 if (!of_node)
1757 return NULL;
1758
1759 /* See if we can find an SMMU corresponding to the phandle */
1760 spin_lock(&arm_smmu_devices_lock);
1761 list_for_each_entry(curr, &arm_smmu_devices, list) {
1762 if (curr->dev->of_node == of_node) {
1763 smmu = curr;
1764 break;
1765 }
1766 }
1767 spin_unlock(&arm_smmu_devices_lock);
1768 of_node_put(of_node);
1769 return smmu;
1770}
1771
1772static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
1773{
1774 unsigned long limit = smmu->strtab_cfg.num_l1_ents;
1775
1776 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
1777 limit *= 1UL << STRTAB_SPLIT;
1778
1779 return sid < limit;
1780}
1781
1782static int arm_smmu_add_device(struct device *dev)
1783{
1784 int i, ret;
1785 u32 sid, *sids;
1786 struct pci_dev *pdev;
1787 struct iommu_group *group;
1788 struct arm_smmu_group *smmu_group;
1789 struct arm_smmu_device *smmu;
1790
1791 /* We only support PCI, for now */
1792 if (!dev_is_pci(dev))
1793 return -ENODEV;
1794
1795 pdev = to_pci_dev(dev);
1796 group = iommu_group_get_for_dev(dev);
1797 if (IS_ERR(group))
1798 return PTR_ERR(group);
1799
1800 smmu_group = iommu_group_get_iommudata(group);
1801 if (!smmu_group) {
1802 smmu = arm_smmu_get_for_pci_dev(pdev);
1803 if (!smmu) {
1804 ret = -ENOENT;
1805 goto out_put_group;
1806 }
1807
1808 smmu_group = kzalloc(sizeof(*smmu_group), GFP_KERNEL);
1809 if (!smmu_group) {
1810 ret = -ENOMEM;
1811 goto out_put_group;
1812 }
1813
1814 smmu_group->ste.valid = true;
1815 smmu_group->smmu = smmu;
1816 iommu_group_set_iommudata(group, smmu_group,
1817 __arm_smmu_release_pci_iommudata);
1818 } else {
1819 smmu = smmu_group->smmu;
1820 }
1821
1822 /* Assume SID == RID until firmware tells us otherwise */
1823 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1824 for (i = 0; i < smmu_group->num_sids; ++i) {
1825 /* If we already know about this SID, then we're done */
1826 if (smmu_group->sids[i] == sid)
1827 return 0;
1828 }
1829
1830 /* Check the SID is in range of the SMMU and our stream table */
1831 if (!arm_smmu_sid_in_range(smmu, sid)) {
1832 ret = -ERANGE;
1833 goto out_put_group;
1834 }
1835
1836 /* Ensure l2 strtab is initialised */
1837 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1838 ret = arm_smmu_init_l2_strtab(smmu, sid);
1839 if (ret)
1840 goto out_put_group;
1841 }
1842
1843 /* Resize the SID array for the group */
1844 smmu_group->num_sids++;
1845 sids = krealloc(smmu_group->sids, smmu_group->num_sids * sizeof(*sids),
1846 GFP_KERNEL);
1847 if (!sids) {
1848 smmu_group->num_sids--;
1849 ret = -ENOMEM;
1850 goto out_put_group;
1851 }
1852
1853 /* Add the new SID */
1854 sids[smmu_group->num_sids - 1] = sid;
1855 smmu_group->sids = sids;
1856 return 0;
1857
1858out_put_group:
1859 iommu_group_put(group);
1860 return ret;
1861}
1862
1863static void arm_smmu_remove_device(struct device *dev)
1864{
1865 iommu_group_remove_device(dev);
1866}
1867
1868static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1869 enum iommu_attr attr, void *data)
1870{
1871 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1872
1873 switch (attr) {
1874 case DOMAIN_ATTR_NESTING:
1875 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1876 return 0;
1877 default:
1878 return -ENODEV;
1879 }
1880}
1881
1882static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1883 enum iommu_attr attr, void *data)
1884{
1885 int ret = 0;
1886 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1887
1888 mutex_lock(&smmu_domain->init_mutex);
1889
1890 switch (attr) {
1891 case DOMAIN_ATTR_NESTING:
1892 if (smmu_domain->smmu) {
1893 ret = -EPERM;
1894 goto out_unlock;
1895 }
1896
1897 if (*(int *)data)
1898 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1899 else
1900 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1901
1902 break;
1903 default:
1904 ret = -ENODEV;
1905 }
1906
1907out_unlock:
1908 mutex_unlock(&smmu_domain->init_mutex);
1909 return ret;
1910}
1911
1912static struct iommu_ops arm_smmu_ops = {
1913 .capable = arm_smmu_capable,
1914 .domain_alloc = arm_smmu_domain_alloc,
1915 .domain_free = arm_smmu_domain_free,
1916 .attach_dev = arm_smmu_attach_dev,
1917 .detach_dev = arm_smmu_detach_dev,
1918 .map = arm_smmu_map,
1919 .unmap = arm_smmu_unmap,
1920 .iova_to_phys = arm_smmu_iova_to_phys,
1921 .add_device = arm_smmu_add_device,
1922 .remove_device = arm_smmu_remove_device,
1923 .domain_get_attr = arm_smmu_domain_get_attr,
1924 .domain_set_attr = arm_smmu_domain_set_attr,
1925 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1926};
1927
1928/* Probing and initialisation functions */
1929static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
1930 struct arm_smmu_queue *q,
1931 unsigned long prod_off,
1932 unsigned long cons_off,
1933 size_t dwords)
1934{
1935 size_t qsz = ((1 << q->max_n_shift) * dwords) << 3;
1936
1937 q->base = dma_alloc_coherent(smmu->dev, qsz, &q->base_dma, GFP_KERNEL);
1938 if (!q->base) {
1939 dev_err(smmu->dev, "failed to allocate queue (0x%zx bytes)\n",
1940 qsz);
1941 return -ENOMEM;
1942 }
1943
1944 q->prod_reg = smmu->base + prod_off;
1945 q->cons_reg = smmu->base + cons_off;
1946 q->ent_dwords = dwords;
1947
1948 q->q_base = Q_BASE_RWA;
1949 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK << Q_BASE_ADDR_SHIFT;
1950 q->q_base |= (q->max_n_shift & Q_BASE_LOG2SIZE_MASK)
1951 << Q_BASE_LOG2SIZE_SHIFT;
1952
1953 q->prod = q->cons = 0;
1954 return 0;
1955}
1956
1957static void arm_smmu_free_one_queue(struct arm_smmu_device *smmu,
1958 struct arm_smmu_queue *q)
1959{
1960 size_t qsz = ((1 << q->max_n_shift) * q->ent_dwords) << 3;
1961
1962 dma_free_coherent(smmu->dev, qsz, q->base, q->base_dma);
1963}
1964
1965static void arm_smmu_free_queues(struct arm_smmu_device *smmu)
1966{
1967 arm_smmu_free_one_queue(smmu, &smmu->cmdq.q);
1968 arm_smmu_free_one_queue(smmu, &smmu->evtq.q);
1969
1970 if (smmu->features & ARM_SMMU_FEAT_PRI)
1971 arm_smmu_free_one_queue(smmu, &smmu->priq.q);
1972}
1973
1974static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
1975{
1976 int ret;
1977
1978 /* cmdq */
1979 spin_lock_init(&smmu->cmdq.lock);
1980 ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
1981 ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS);
1982 if (ret)
1983 goto out;
1984
1985 /* evtq */
1986 ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
1987 ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS);
1988 if (ret)
1989 goto out_free_cmdq;
1990
1991 /* priq */
1992 if (!(smmu->features & ARM_SMMU_FEAT_PRI))
1993 return 0;
1994
1995 ret = arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
1996 ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS);
1997 if (ret)
1998 goto out_free_evtq;
1999
2000 return 0;
2001
2002out_free_evtq:
2003 arm_smmu_free_one_queue(smmu, &smmu->evtq.q);
2004out_free_cmdq:
2005 arm_smmu_free_one_queue(smmu, &smmu->cmdq.q);
2006out:
2007 return ret;
2008}
2009
2010static void arm_smmu_free_l2_strtab(struct arm_smmu_device *smmu)
2011{
2012 int i;
2013 size_t size;
2014 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2015
2016 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
2017 for (i = 0; i < cfg->num_l1_ents; ++i) {
2018 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[i];
2019
2020 if (!desc->l2ptr)
2021 continue;
2022
2023 dma_free_coherent(smmu->dev, size, desc->l2ptr,
2024 desc->l2ptr_dma);
2025 }
2026}
2027
2028static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
2029{
2030 unsigned int i;
2031 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2032 size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
2033 void *strtab = smmu->strtab_cfg.strtab;
2034
2035 cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
2036 if (!cfg->l1_desc) {
2037 dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
2038 return -ENOMEM;
2039 }
2040
2041 for (i = 0; i < cfg->num_l1_ents; ++i) {
2042 arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
2043 strtab += STRTAB_L1_DESC_DWORDS << 3;
2044 }
2045
2046 return 0;
2047}
2048
2049static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
2050{
2051 void *strtab;
2052 u64 reg;
Will Deacond2e88e72015-06-30 10:02:28 +01002053 u32 size, l1size;
Will Deacon48ec83b2015-05-27 17:25:59 +01002054 int ret;
2055 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2056
Will Deacon28c8b402015-07-16 17:50:12 +01002057 /*
2058 * If we can resolve everything with a single L2 table, then we
2059 * just need a single L1 descriptor. Otherwise, calculate the L1
2060 * size, capped to the SIDSIZE.
2061 */
2062 if (smmu->sid_bits < STRTAB_SPLIT) {
2063 size = 0;
2064 } else {
2065 size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
2066 size = min(size, smmu->sid_bits - STRTAB_SPLIT);
2067 }
Will Deacond2e88e72015-06-30 10:02:28 +01002068 cfg->num_l1_ents = 1 << size;
2069
2070 size += STRTAB_SPLIT;
2071 if (size < smmu->sid_bits)
Will Deacon48ec83b2015-05-27 17:25:59 +01002072 dev_warn(smmu->dev,
2073 "2-level strtab only covers %u/%u bits of SID\n",
Will Deacond2e88e72015-06-30 10:02:28 +01002074 size, smmu->sid_bits);
Will Deacon48ec83b2015-05-27 17:25:59 +01002075
Will Deacond2e88e72015-06-30 10:02:28 +01002076 l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
2077 strtab = dma_zalloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
Will Deacon48ec83b2015-05-27 17:25:59 +01002078 GFP_KERNEL);
2079 if (!strtab) {
2080 dev_err(smmu->dev,
2081 "failed to allocate l1 stream table (%u bytes)\n",
2082 size);
2083 return -ENOMEM;
2084 }
2085 cfg->strtab = strtab;
2086
2087 /* Configure strtab_base_cfg for 2 levels */
2088 reg = STRTAB_BASE_CFG_FMT_2LVL;
2089 reg |= (size & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2090 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2091 reg |= (STRTAB_SPLIT & STRTAB_BASE_CFG_SPLIT_MASK)
2092 << STRTAB_BASE_CFG_SPLIT_SHIFT;
2093 cfg->strtab_base_cfg = reg;
2094
2095 ret = arm_smmu_init_l1_strtab(smmu);
2096 if (ret)
2097 dma_free_coherent(smmu->dev,
Will Deacond2e88e72015-06-30 10:02:28 +01002098 l1size,
Will Deacon48ec83b2015-05-27 17:25:59 +01002099 strtab,
2100 cfg->strtab_dma);
2101 return ret;
2102}
2103
2104static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
2105{
2106 void *strtab;
2107 u64 reg;
2108 u32 size;
2109 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2110
2111 size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
2112 strtab = dma_zalloc_coherent(smmu->dev, size, &cfg->strtab_dma,
2113 GFP_KERNEL);
2114 if (!strtab) {
2115 dev_err(smmu->dev,
2116 "failed to allocate linear stream table (%u bytes)\n",
2117 size);
2118 return -ENOMEM;
2119 }
2120 cfg->strtab = strtab;
2121 cfg->num_l1_ents = 1 << smmu->sid_bits;
2122
2123 /* Configure strtab_base_cfg for a linear table covering all SIDs */
2124 reg = STRTAB_BASE_CFG_FMT_LINEAR;
2125 reg |= (smmu->sid_bits & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2126 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2127 cfg->strtab_base_cfg = reg;
2128
2129 arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
2130 return 0;
2131}
2132
2133static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
2134{
2135 u64 reg;
2136 int ret;
2137
2138 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
2139 ret = arm_smmu_init_strtab_2lvl(smmu);
2140 else
2141 ret = arm_smmu_init_strtab_linear(smmu);
2142
2143 if (ret)
2144 return ret;
2145
2146 /* Set the strtab base address */
2147 reg = smmu->strtab_cfg.strtab_dma &
2148 STRTAB_BASE_ADDR_MASK << STRTAB_BASE_ADDR_SHIFT;
2149 reg |= STRTAB_BASE_RA;
2150 smmu->strtab_cfg.strtab_base = reg;
2151
2152 /* Allocate the first VMID for stage-2 bypass STEs */
2153 set_bit(0, smmu->vmid_map);
2154 return 0;
2155}
2156
2157static void arm_smmu_free_strtab(struct arm_smmu_device *smmu)
2158{
2159 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2160 u32 size = cfg->num_l1_ents;
2161
2162 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
2163 arm_smmu_free_l2_strtab(smmu);
2164 size *= STRTAB_L1_DESC_DWORDS << 3;
2165 } else {
2166 size *= STRTAB_STE_DWORDS * 3;
2167 }
2168
2169 dma_free_coherent(smmu->dev, size, cfg->strtab, cfg->strtab_dma);
2170}
2171
2172static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
2173{
2174 int ret;
2175
2176 ret = arm_smmu_init_queues(smmu);
2177 if (ret)
2178 return ret;
2179
2180 ret = arm_smmu_init_strtab(smmu);
2181 if (ret)
2182 goto out_free_queues;
2183
2184 return 0;
2185
2186out_free_queues:
2187 arm_smmu_free_queues(smmu);
2188 return ret;
2189}
2190
2191static void arm_smmu_free_structures(struct arm_smmu_device *smmu)
2192{
2193 arm_smmu_free_strtab(smmu);
2194 arm_smmu_free_queues(smmu);
2195}
2196
2197static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
2198 unsigned int reg_off, unsigned int ack_off)
2199{
2200 u32 reg;
2201
2202 writel_relaxed(val, smmu->base + reg_off);
2203 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
2204 1, ARM_SMMU_POLL_TIMEOUT_US);
2205}
2206
2207static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
2208{
2209 int ret, irq;
Marc Zyngierccd63852015-07-15 11:55:18 +01002210 u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
Will Deacon48ec83b2015-05-27 17:25:59 +01002211
2212 /* Disable IRQs first */
2213 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
2214 ARM_SMMU_IRQ_CTRLACK);
2215 if (ret) {
2216 dev_err(smmu->dev, "failed to disable irqs\n");
2217 return ret;
2218 }
2219
2220 /* Clear the MSI address regs */
2221 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
2222 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
2223
2224 /* Request wired interrupt lines */
2225 irq = smmu->evtq.q.irq;
2226 if (irq) {
2227 ret = devm_request_threaded_irq(smmu->dev, irq,
2228 arm_smmu_evtq_handler,
2229 arm_smmu_evtq_thread,
2230 0, "arm-smmu-v3-evtq", smmu);
2231 if (IS_ERR_VALUE(ret))
2232 dev_warn(smmu->dev, "failed to enable evtq irq\n");
2233 }
2234
2235 irq = smmu->cmdq.q.irq;
2236 if (irq) {
2237 ret = devm_request_irq(smmu->dev, irq,
2238 arm_smmu_cmdq_sync_handler, 0,
2239 "arm-smmu-v3-cmdq-sync", smmu);
2240 if (IS_ERR_VALUE(ret))
2241 dev_warn(smmu->dev, "failed to enable cmdq-sync irq\n");
2242 }
2243
2244 irq = smmu->gerr_irq;
2245 if (irq) {
2246 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
2247 0, "arm-smmu-v3-gerror", smmu);
2248 if (IS_ERR_VALUE(ret))
2249 dev_warn(smmu->dev, "failed to enable gerror irq\n");
2250 }
2251
2252 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2253 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
2254
2255 irq = smmu->priq.q.irq;
2256 if (irq) {
2257 ret = devm_request_threaded_irq(smmu->dev, irq,
2258 arm_smmu_priq_handler,
2259 arm_smmu_priq_thread,
2260 0, "arm-smmu-v3-priq",
2261 smmu);
2262 if (IS_ERR_VALUE(ret))
2263 dev_warn(smmu->dev,
2264 "failed to enable priq irq\n");
Marc Zyngierccd63852015-07-15 11:55:18 +01002265 else
2266 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
Will Deacon48ec83b2015-05-27 17:25:59 +01002267 }
2268 }
2269
2270 /* Enable interrupt generation on the SMMU */
Marc Zyngierccd63852015-07-15 11:55:18 +01002271 ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
Will Deacon48ec83b2015-05-27 17:25:59 +01002272 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
2273 if (ret)
2274 dev_warn(smmu->dev, "failed to enable irqs\n");
2275
2276 return 0;
2277}
2278
2279static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
2280{
2281 int ret;
2282
2283 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
2284 if (ret)
2285 dev_err(smmu->dev, "failed to clear cr0\n");
2286
2287 return ret;
2288}
2289
2290static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
2291{
2292 int ret;
2293 u32 reg, enables;
2294 struct arm_smmu_cmdq_ent cmd;
2295
2296 /* Clear CR0 and sync (disables SMMU and queue processing) */
2297 reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
2298 if (reg & CR0_SMMUEN)
2299 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
2300
2301 ret = arm_smmu_device_disable(smmu);
2302 if (ret)
2303 return ret;
2304
2305 /* CR1 (table and queue memory attributes) */
2306 reg = (CR1_SH_ISH << CR1_TABLE_SH_SHIFT) |
2307 (CR1_CACHE_WB << CR1_TABLE_OC_SHIFT) |
2308 (CR1_CACHE_WB << CR1_TABLE_IC_SHIFT) |
2309 (CR1_SH_ISH << CR1_QUEUE_SH_SHIFT) |
2310 (CR1_CACHE_WB << CR1_QUEUE_OC_SHIFT) |
2311 (CR1_CACHE_WB << CR1_QUEUE_IC_SHIFT);
2312 writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
2313
2314 /* CR2 (random crap) */
2315 reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
2316 writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
2317
2318 /* Stream table */
2319 writeq_relaxed(smmu->strtab_cfg.strtab_base,
2320 smmu->base + ARM_SMMU_STRTAB_BASE);
2321 writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
2322 smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
2323
2324 /* Command queue */
2325 writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
2326 writel_relaxed(smmu->cmdq.q.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
2327 writel_relaxed(smmu->cmdq.q.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
2328
2329 enables = CR0_CMDQEN;
2330 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2331 ARM_SMMU_CR0ACK);
2332 if (ret) {
2333 dev_err(smmu->dev, "failed to enable command queue\n");
2334 return ret;
2335 }
2336
2337 /* Invalidate any cached configuration */
2338 cmd.opcode = CMDQ_OP_CFGI_ALL;
2339 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2340 cmd.opcode = CMDQ_OP_CMD_SYNC;
2341 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2342
2343 /* Invalidate any stale TLB entries */
2344 if (smmu->features & ARM_SMMU_FEAT_HYP) {
2345 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
2346 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2347 }
2348
2349 cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
2350 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2351 cmd.opcode = CMDQ_OP_CMD_SYNC;
2352 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2353
2354 /* Event queue */
2355 writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
2356 writel_relaxed(smmu->evtq.q.prod, smmu->base + ARM_SMMU_EVTQ_PROD);
2357 writel_relaxed(smmu->evtq.q.cons, smmu->base + ARM_SMMU_EVTQ_CONS);
2358
2359 enables |= CR0_EVTQEN;
2360 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2361 ARM_SMMU_CR0ACK);
2362 if (ret) {
2363 dev_err(smmu->dev, "failed to enable event queue\n");
2364 return ret;
2365 }
2366
2367 /* PRI queue */
2368 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2369 writeq_relaxed(smmu->priq.q.q_base,
2370 smmu->base + ARM_SMMU_PRIQ_BASE);
2371 writel_relaxed(smmu->priq.q.prod,
2372 smmu->base + ARM_SMMU_PRIQ_PROD);
2373 writel_relaxed(smmu->priq.q.cons,
2374 smmu->base + ARM_SMMU_PRIQ_CONS);
2375
2376 enables |= CR0_PRIQEN;
2377 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2378 ARM_SMMU_CR0ACK);
2379 if (ret) {
2380 dev_err(smmu->dev, "failed to enable PRI queue\n");
2381 return ret;
2382 }
2383 }
2384
2385 ret = arm_smmu_setup_irqs(smmu);
2386 if (ret) {
2387 dev_err(smmu->dev, "failed to setup irqs\n");
2388 return ret;
2389 }
2390
2391 /* Enable the SMMU interface */
2392 enables |= CR0_SMMUEN;
2393 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2394 ARM_SMMU_CR0ACK);
2395 if (ret) {
2396 dev_err(smmu->dev, "failed to enable SMMU interface\n");
2397 return ret;
2398 }
2399
2400 return 0;
2401}
2402
2403static int arm_smmu_device_probe(struct arm_smmu_device *smmu)
2404{
2405 u32 reg;
2406 bool coherent;
2407 unsigned long pgsize_bitmap = 0;
2408
2409 /* IDR0 */
2410 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
2411
2412 /* 2-level structures */
2413 if ((reg & IDR0_ST_LVL_MASK << IDR0_ST_LVL_SHIFT) == IDR0_ST_LVL_2LVL)
2414 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
2415
2416 if (reg & IDR0_CD2L)
2417 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
2418
2419 /*
2420 * Translation table endianness.
2421 * We currently require the same endianness as the CPU, but this
2422 * could be changed later by adding a new IO_PGTABLE_QUIRK.
2423 */
2424 switch (reg & IDR0_TTENDIAN_MASK << IDR0_TTENDIAN_SHIFT) {
2425 case IDR0_TTENDIAN_MIXED:
2426 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
2427 break;
2428#ifdef __BIG_ENDIAN
2429 case IDR0_TTENDIAN_BE:
2430 smmu->features |= ARM_SMMU_FEAT_TT_BE;
2431 break;
2432#else
2433 case IDR0_TTENDIAN_LE:
2434 smmu->features |= ARM_SMMU_FEAT_TT_LE;
2435 break;
2436#endif
2437 default:
2438 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
2439 return -ENXIO;
2440 }
2441
2442 /* Boolean feature flags */
2443 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
2444 smmu->features |= ARM_SMMU_FEAT_PRI;
2445
2446 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
2447 smmu->features |= ARM_SMMU_FEAT_ATS;
2448
2449 if (reg & IDR0_SEV)
2450 smmu->features |= ARM_SMMU_FEAT_SEV;
2451
2452 if (reg & IDR0_MSI)
2453 smmu->features |= ARM_SMMU_FEAT_MSI;
2454
2455 if (reg & IDR0_HYP)
2456 smmu->features |= ARM_SMMU_FEAT_HYP;
2457
2458 /*
2459 * The dma-coherent property is used in preference to the ID
2460 * register, but warn on mismatch.
2461 */
2462 coherent = of_dma_is_coherent(smmu->dev->of_node);
2463 if (coherent)
2464 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2465
2466 if (!!(reg & IDR0_COHACC) != coherent)
2467 dev_warn(smmu->dev, "IDR0.COHACC overridden by dma-coherent property (%s)\n",
2468 coherent ? "true" : "false");
2469
2470 if (reg & IDR0_STALL_MODEL)
2471 smmu->features |= ARM_SMMU_FEAT_STALLS;
2472
2473 if (reg & IDR0_S1P)
2474 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
2475
2476 if (reg & IDR0_S2P)
2477 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
2478
2479 if (!(reg & (IDR0_S1P | IDR0_S2P))) {
2480 dev_err(smmu->dev, "no translation support!\n");
2481 return -ENXIO;
2482 }
2483
2484 /* We only support the AArch64 table format at present */
2485 if ((reg & IDR0_TTF_MASK << IDR0_TTF_SHIFT) < IDR0_TTF_AARCH64) {
2486 dev_err(smmu->dev, "AArch64 table format not supported!\n");
2487 return -ENXIO;
2488 }
2489
2490 /* ASID/VMID sizes */
2491 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
2492 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
2493
2494 /* IDR1 */
2495 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
2496 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
2497 dev_err(smmu->dev, "embedded implementation not supported\n");
2498 return -ENXIO;
2499 }
2500
2501 /* Queue sizes, capped at 4k */
2502 smmu->cmdq.q.max_n_shift = min((u32)CMDQ_MAX_SZ_SHIFT,
2503 reg >> IDR1_CMDQ_SHIFT & IDR1_CMDQ_MASK);
2504 if (!smmu->cmdq.q.max_n_shift) {
2505 /* Odd alignment restrictions on the base, so ignore for now */
2506 dev_err(smmu->dev, "unit-length command queue not supported\n");
2507 return -ENXIO;
2508 }
2509
2510 smmu->evtq.q.max_n_shift = min((u32)EVTQ_MAX_SZ_SHIFT,
2511 reg >> IDR1_EVTQ_SHIFT & IDR1_EVTQ_MASK);
2512 smmu->priq.q.max_n_shift = min((u32)PRIQ_MAX_SZ_SHIFT,
2513 reg >> IDR1_PRIQ_SHIFT & IDR1_PRIQ_MASK);
2514
2515 /* SID/SSID sizes */
2516 smmu->ssid_bits = reg >> IDR1_SSID_SHIFT & IDR1_SSID_MASK;
2517 smmu->sid_bits = reg >> IDR1_SID_SHIFT & IDR1_SID_MASK;
2518
2519 /* IDR5 */
2520 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
2521
2522 /* Maximum number of outstanding stalls */
2523 smmu->evtq.max_stalls = reg >> IDR5_STALL_MAX_SHIFT
2524 & IDR5_STALL_MAX_MASK;
2525
2526 /* Page sizes */
2527 if (reg & IDR5_GRAN64K)
2528 pgsize_bitmap |= SZ_64K | SZ_512M;
2529 if (reg & IDR5_GRAN16K)
2530 pgsize_bitmap |= SZ_16K | SZ_32M;
2531 if (reg & IDR5_GRAN4K)
2532 pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
2533
2534 arm_smmu_ops.pgsize_bitmap &= pgsize_bitmap;
2535
2536 /* Output address size */
2537 switch (reg & IDR5_OAS_MASK << IDR5_OAS_SHIFT) {
2538 case IDR5_OAS_32_BIT:
2539 smmu->oas = 32;
2540 break;
2541 case IDR5_OAS_36_BIT:
2542 smmu->oas = 36;
2543 break;
2544 case IDR5_OAS_40_BIT:
2545 smmu->oas = 40;
2546 break;
2547 case IDR5_OAS_42_BIT:
2548 smmu->oas = 42;
2549 break;
2550 case IDR5_OAS_44_BIT:
2551 smmu->oas = 44;
2552 break;
2553 case IDR5_OAS_48_BIT:
2554 smmu->oas = 48;
2555 break;
2556 default:
2557 dev_err(smmu->dev, "unknown output address size!\n");
2558 return -ENXIO;
2559 }
2560
2561 /* Set the DMA mask for our table walker */
2562 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
2563 dev_warn(smmu->dev,
2564 "failed to set DMA mask for table walker\n");
2565
2566 if (!smmu->ias)
2567 smmu->ias = smmu->oas;
2568
2569 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
2570 smmu->ias, smmu->oas, smmu->features);
2571 return 0;
2572}
2573
2574static int arm_smmu_device_dt_probe(struct platform_device *pdev)
2575{
2576 int irq, ret;
2577 struct resource *res;
2578 struct arm_smmu_device *smmu;
2579 struct device *dev = &pdev->dev;
2580
2581 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2582 if (!smmu) {
2583 dev_err(dev, "failed to allocate arm_smmu_device\n");
2584 return -ENOMEM;
2585 }
2586 smmu->dev = dev;
2587
2588 /* Base address */
2589 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2590 if (resource_size(res) + 1 < SZ_128K) {
2591 dev_err(dev, "MMIO region too small (%pr)\n", res);
2592 return -EINVAL;
2593 }
2594
2595 smmu->base = devm_ioremap_resource(dev, res);
2596 if (IS_ERR(smmu->base))
2597 return PTR_ERR(smmu->base);
2598
2599 /* Interrupt lines */
2600 irq = platform_get_irq_byname(pdev, "eventq");
2601 if (irq > 0)
2602 smmu->evtq.q.irq = irq;
2603
2604 irq = platform_get_irq_byname(pdev, "priq");
2605 if (irq > 0)
2606 smmu->priq.q.irq = irq;
2607
2608 irq = platform_get_irq_byname(pdev, "cmdq-sync");
2609 if (irq > 0)
2610 smmu->cmdq.q.irq = irq;
2611
2612 irq = platform_get_irq_byname(pdev, "gerror");
2613 if (irq > 0)
2614 smmu->gerr_irq = irq;
2615
Zhen Lei5e929462015-07-07 04:30:18 +01002616 parse_driver_options(smmu);
2617
Will Deacon48ec83b2015-05-27 17:25:59 +01002618 /* Probe the h/w */
2619 ret = arm_smmu_device_probe(smmu);
2620 if (ret)
2621 return ret;
2622
2623 /* Initialise in-memory data structures */
2624 ret = arm_smmu_init_structures(smmu);
2625 if (ret)
2626 return ret;
2627
2628 /* Reset the device */
2629 ret = arm_smmu_device_reset(smmu);
2630 if (ret)
2631 goto out_free_structures;
2632
2633 /* Record our private device structure */
2634 INIT_LIST_HEAD(&smmu->list);
2635 spin_lock(&arm_smmu_devices_lock);
2636 list_add(&smmu->list, &arm_smmu_devices);
2637 spin_unlock(&arm_smmu_devices_lock);
2638 return 0;
2639
2640out_free_structures:
2641 arm_smmu_free_structures(smmu);
2642 return ret;
2643}
2644
2645static int arm_smmu_device_remove(struct platform_device *pdev)
2646{
2647 struct arm_smmu_device *curr, *smmu = NULL;
2648 struct device *dev = &pdev->dev;
2649
2650 spin_lock(&arm_smmu_devices_lock);
2651 list_for_each_entry(curr, &arm_smmu_devices, list) {
2652 if (curr->dev == dev) {
2653 smmu = curr;
2654 list_del(&smmu->list);
2655 break;
2656 }
2657 }
2658 spin_unlock(&arm_smmu_devices_lock);
2659
2660 if (!smmu)
2661 return -ENODEV;
2662
2663 arm_smmu_device_disable(smmu);
2664 arm_smmu_free_structures(smmu);
2665 return 0;
2666}
2667
2668static struct of_device_id arm_smmu_of_match[] = {
2669 { .compatible = "arm,smmu-v3", },
2670 { },
2671};
2672MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2673
2674static struct platform_driver arm_smmu_driver = {
2675 .driver = {
2676 .name = "arm-smmu-v3",
2677 .of_match_table = of_match_ptr(arm_smmu_of_match),
2678 },
2679 .probe = arm_smmu_device_dt_probe,
2680 .remove = arm_smmu_device_remove,
2681};
2682
2683static int __init arm_smmu_init(void)
2684{
2685 struct device_node *np;
2686 int ret;
2687
2688 np = of_find_matching_node(NULL, arm_smmu_of_match);
2689 if (!np)
2690 return 0;
2691
2692 of_node_put(np);
2693
2694 ret = platform_driver_register(&arm_smmu_driver);
2695 if (ret)
2696 return ret;
2697
2698 return bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2699}
2700
2701static void __exit arm_smmu_exit(void)
2702{
2703 return platform_driver_unregister(&arm_smmu_driver);
2704}
2705
2706subsys_initcall(arm_smmu_init);
2707module_exit(arm_smmu_exit);
2708
2709MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
2710MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2711MODULE_LICENSE("GPL v2");