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