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