blob: 637014ab89654e7549df6e9752e1c975617856ef [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>
Will Deacon941a8022015-08-11 16:25:10 +010031#include <linux/of_platform.h>
Will Deacon48ec83b2015-05-27 17:25:59 +010032#include <linux/pci.h>
33#include <linux/platform_device.h>
34
35#include "io-pgtable.h"
36
37/* MMIO registers */
38#define ARM_SMMU_IDR0 0x0
39#define IDR0_ST_LVL_SHIFT 27
40#define IDR0_ST_LVL_MASK 0x3
41#define IDR0_ST_LVL_2LVL (1 << IDR0_ST_LVL_SHIFT)
42#define IDR0_STALL_MODEL (3 << 24)
43#define IDR0_TTENDIAN_SHIFT 21
44#define IDR0_TTENDIAN_MASK 0x3
45#define IDR0_TTENDIAN_LE (2 << IDR0_TTENDIAN_SHIFT)
46#define IDR0_TTENDIAN_BE (3 << IDR0_TTENDIAN_SHIFT)
47#define IDR0_TTENDIAN_MIXED (0 << IDR0_TTENDIAN_SHIFT)
48#define IDR0_CD2L (1 << 19)
49#define IDR0_VMID16 (1 << 18)
50#define IDR0_PRI (1 << 16)
51#define IDR0_SEV (1 << 14)
52#define IDR0_MSI (1 << 13)
53#define IDR0_ASID16 (1 << 12)
54#define IDR0_ATS (1 << 10)
55#define IDR0_HYP (1 << 9)
56#define IDR0_COHACC (1 << 4)
57#define IDR0_TTF_SHIFT 2
58#define IDR0_TTF_MASK 0x3
59#define IDR0_TTF_AARCH64 (2 << IDR0_TTF_SHIFT)
60#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;
Will Deacon48ec83b2015-05-27 17:25:59 +0100572};
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
Zhen Lei5e929462015-07-07 04:30:18 +0100606struct arm_smmu_option_prop {
607 u32 opt;
608 const char *prop;
609};
610
611static struct arm_smmu_option_prop arm_smmu_options[] = {
612 { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
613 { 0, NULL},
614};
615
Will Deacon48ec83b2015-05-27 17:25:59 +0100616static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
617{
618 return container_of(dom, struct arm_smmu_domain, domain);
619}
620
Zhen Lei5e929462015-07-07 04:30:18 +0100621static void parse_driver_options(struct arm_smmu_device *smmu)
622{
623 int i = 0;
624
625 do {
626 if (of_property_read_bool(smmu->dev->of_node,
627 arm_smmu_options[i].prop)) {
628 smmu->options |= arm_smmu_options[i].opt;
629 dev_notice(smmu->dev, "option %s\n",
630 arm_smmu_options[i].prop);
631 }
632 } while (arm_smmu_options[++i].opt);
633}
634
Will Deacon48ec83b2015-05-27 17:25:59 +0100635/* Low-level queue manipulation functions */
636static bool queue_full(struct arm_smmu_queue *q)
637{
638 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
639 Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
640}
641
642static bool queue_empty(struct arm_smmu_queue *q)
643{
644 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
645 Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
646}
647
648static void queue_sync_cons(struct arm_smmu_queue *q)
649{
650 q->cons = readl_relaxed(q->cons_reg);
651}
652
653static void queue_inc_cons(struct arm_smmu_queue *q)
654{
655 u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
656
657 q->cons = Q_OVF(q, q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
658 writel(q->cons, q->cons_reg);
659}
660
661static int queue_sync_prod(struct arm_smmu_queue *q)
662{
663 int ret = 0;
664 u32 prod = readl_relaxed(q->prod_reg);
665
666 if (Q_OVF(q, prod) != Q_OVF(q, q->prod))
667 ret = -EOVERFLOW;
668
669 q->prod = prod;
670 return ret;
671}
672
673static void queue_inc_prod(struct arm_smmu_queue *q)
674{
675 u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + 1;
676
677 q->prod = Q_OVF(q, q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
678 writel(q->prod, q->prod_reg);
679}
680
681static bool __queue_cons_before(struct arm_smmu_queue *q, u32 until)
682{
683 if (Q_WRP(q, q->cons) == Q_WRP(q, until))
684 return Q_IDX(q, q->cons) < Q_IDX(q, until);
685
686 return Q_IDX(q, q->cons) >= Q_IDX(q, until);
687}
688
689static int queue_poll_cons(struct arm_smmu_queue *q, u32 until, bool wfe)
690{
691 ktime_t timeout = ktime_add_us(ktime_get(), ARM_SMMU_POLL_TIMEOUT_US);
692
693 while (queue_sync_cons(q), __queue_cons_before(q, until)) {
694 if (ktime_compare(ktime_get(), timeout) > 0)
695 return -ETIMEDOUT;
696
697 if (wfe) {
698 wfe();
699 } else {
700 cpu_relax();
701 udelay(1);
702 }
703 }
704
705 return 0;
706}
707
708static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
709{
710 int i;
711
712 for (i = 0; i < n_dwords; ++i)
713 *dst++ = cpu_to_le64(*src++);
714}
715
716static int queue_insert_raw(struct arm_smmu_queue *q, u64 *ent)
717{
718 if (queue_full(q))
719 return -ENOSPC;
720
721 queue_write(Q_ENT(q, q->prod), ent, q->ent_dwords);
722 queue_inc_prod(q);
723 return 0;
724}
725
726static void queue_read(__le64 *dst, u64 *src, size_t n_dwords)
727{
728 int i;
729
730 for (i = 0; i < n_dwords; ++i)
731 *dst++ = le64_to_cpu(*src++);
732}
733
734static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent)
735{
736 if (queue_empty(q))
737 return -EAGAIN;
738
739 queue_read(ent, Q_ENT(q, q->cons), q->ent_dwords);
740 queue_inc_cons(q);
741 return 0;
742}
743
744/* High-level queue accessors */
745static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
746{
747 memset(cmd, 0, CMDQ_ENT_DWORDS << 3);
748 cmd[0] |= (ent->opcode & CMDQ_0_OP_MASK) << CMDQ_0_OP_SHIFT;
749
750 switch (ent->opcode) {
751 case CMDQ_OP_TLBI_EL2_ALL:
752 case CMDQ_OP_TLBI_NSNH_ALL:
753 break;
754 case CMDQ_OP_PREFETCH_CFG:
755 cmd[0] |= (u64)ent->prefetch.sid << CMDQ_PREFETCH_0_SID_SHIFT;
756 cmd[1] |= ent->prefetch.size << CMDQ_PREFETCH_1_SIZE_SHIFT;
757 cmd[1] |= ent->prefetch.addr & CMDQ_PREFETCH_1_ADDR_MASK;
758 break;
759 case CMDQ_OP_CFGI_STE:
760 cmd[0] |= (u64)ent->cfgi.sid << CMDQ_CFGI_0_SID_SHIFT;
761 cmd[1] |= ent->cfgi.leaf ? CMDQ_CFGI_1_LEAF : 0;
762 break;
763 case CMDQ_OP_CFGI_ALL:
764 /* Cover the entire SID range */
765 cmd[1] |= CMDQ_CFGI_1_RANGE_MASK << CMDQ_CFGI_1_RANGE_SHIFT;
766 break;
767 case CMDQ_OP_TLBI_NH_VA:
768 cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
769 /* Fallthrough */
770 case CMDQ_OP_TLBI_S2_IPA:
771 cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
772 cmd[1] |= ent->tlbi.leaf ? CMDQ_TLBI_1_LEAF : 0;
773 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_ADDR_MASK;
774 break;
775 case CMDQ_OP_TLBI_NH_ASID:
776 cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
777 /* Fallthrough */
778 case CMDQ_OP_TLBI_S12_VMALL:
779 cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
780 break;
781 case CMDQ_OP_PRI_RESP:
782 cmd[0] |= ent->substream_valid ? CMDQ_0_SSV : 0;
783 cmd[0] |= ent->pri.ssid << CMDQ_PRI_0_SSID_SHIFT;
784 cmd[0] |= (u64)ent->pri.sid << CMDQ_PRI_0_SID_SHIFT;
785 cmd[1] |= ent->pri.grpid << CMDQ_PRI_1_GRPID_SHIFT;
786 switch (ent->pri.resp) {
787 case PRI_RESP_DENY:
788 cmd[1] |= CMDQ_PRI_1_RESP_DENY;
789 break;
790 case PRI_RESP_FAIL:
791 cmd[1] |= CMDQ_PRI_1_RESP_FAIL;
792 break;
793 case PRI_RESP_SUCC:
794 cmd[1] |= CMDQ_PRI_1_RESP_SUCC;
795 break;
796 default:
797 return -EINVAL;
798 }
799 break;
800 case CMDQ_OP_CMD_SYNC:
801 cmd[0] |= CMDQ_SYNC_0_CS_SEV;
802 break;
803 default:
804 return -ENOENT;
805 }
806
807 return 0;
808}
809
810static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu)
811{
812 static const char *cerror_str[] = {
813 [CMDQ_ERR_CERROR_NONE_IDX] = "No error",
814 [CMDQ_ERR_CERROR_ILL_IDX] = "Illegal command",
815 [CMDQ_ERR_CERROR_ABT_IDX] = "Abort on command fetch",
816 };
817
818 int i;
819 u64 cmd[CMDQ_ENT_DWORDS];
820 struct arm_smmu_queue *q = &smmu->cmdq.q;
821 u32 cons = readl_relaxed(q->cons_reg);
822 u32 idx = cons >> CMDQ_ERR_SHIFT & CMDQ_ERR_MASK;
823 struct arm_smmu_cmdq_ent cmd_sync = {
824 .opcode = CMDQ_OP_CMD_SYNC,
825 };
826
827 dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons,
828 cerror_str[idx]);
829
830 switch (idx) {
831 case CMDQ_ERR_CERROR_ILL_IDX:
832 break;
833 case CMDQ_ERR_CERROR_ABT_IDX:
834 dev_err(smmu->dev, "retrying command fetch\n");
835 case CMDQ_ERR_CERROR_NONE_IDX:
836 return;
837 }
838
839 /*
840 * We may have concurrent producers, so we need to be careful
841 * not to touch any of the shadow cmdq state.
842 */
843 queue_read(cmd, Q_ENT(q, idx), q->ent_dwords);
844 dev_err(smmu->dev, "skipping command in error state:\n");
845 for (i = 0; i < ARRAY_SIZE(cmd); ++i)
846 dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
847
848 /* Convert the erroneous command into a CMD_SYNC */
849 if (arm_smmu_cmdq_build_cmd(cmd, &cmd_sync)) {
850 dev_err(smmu->dev, "failed to convert to CMD_SYNC\n");
851 return;
852 }
853
854 queue_write(cmd, Q_ENT(q, idx), q->ent_dwords);
855}
856
857static void arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
858 struct arm_smmu_cmdq_ent *ent)
859{
860 u32 until;
861 u64 cmd[CMDQ_ENT_DWORDS];
862 bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
863 struct arm_smmu_queue *q = &smmu->cmdq.q;
864
865 if (arm_smmu_cmdq_build_cmd(cmd, ent)) {
866 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
867 ent->opcode);
868 return;
869 }
870
871 spin_lock(&smmu->cmdq.lock);
872 while (until = q->prod + 1, queue_insert_raw(q, cmd) == -ENOSPC) {
873 /*
874 * Keep the queue locked, otherwise the producer could wrap
875 * twice and we could see a future consumer pointer that looks
876 * like it's behind us.
877 */
878 if (queue_poll_cons(q, until, wfe))
879 dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
880 }
881
882 if (ent->opcode == CMDQ_OP_CMD_SYNC && queue_poll_cons(q, until, wfe))
883 dev_err_ratelimited(smmu->dev, "CMD_SYNC timeout\n");
884 spin_unlock(&smmu->cmdq.lock);
885}
886
887/* Context descriptor manipulation functions */
888static u64 arm_smmu_cpu_tcr_to_cd(u64 tcr)
889{
890 u64 val = 0;
891
892 /* Repack the TCR. Just care about TTBR0 for now */
893 val |= ARM_SMMU_TCR2CD(tcr, T0SZ);
894 val |= ARM_SMMU_TCR2CD(tcr, TG0);
895 val |= ARM_SMMU_TCR2CD(tcr, IRGN0);
896 val |= ARM_SMMU_TCR2CD(tcr, ORGN0);
897 val |= ARM_SMMU_TCR2CD(tcr, SH0);
898 val |= ARM_SMMU_TCR2CD(tcr, EPD0);
899 val |= ARM_SMMU_TCR2CD(tcr, EPD1);
900 val |= ARM_SMMU_TCR2CD(tcr, IPS);
901 val |= ARM_SMMU_TCR2CD(tcr, TBI0);
902
903 return val;
904}
905
906static void arm_smmu_write_ctx_desc(struct arm_smmu_device *smmu,
907 struct arm_smmu_s1_cfg *cfg)
908{
909 u64 val;
910
911 /*
912 * We don't need to issue any invalidation here, as we'll invalidate
913 * the STE when installing the new entry anyway.
914 */
915 val = arm_smmu_cpu_tcr_to_cd(cfg->cd.tcr) |
916#ifdef __BIG_ENDIAN
917 CTXDESC_CD_0_ENDI |
918#endif
919 CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET_PRIVATE |
920 CTXDESC_CD_0_AA64 | (u64)cfg->cd.asid << CTXDESC_CD_0_ASID_SHIFT |
921 CTXDESC_CD_0_V;
922 cfg->cdptr[0] = cpu_to_le64(val);
923
924 val = cfg->cd.ttbr & CTXDESC_CD_1_TTB0_MASK << CTXDESC_CD_1_TTB0_SHIFT;
925 cfg->cdptr[1] = cpu_to_le64(val);
926
927 cfg->cdptr[3] = cpu_to_le64(cfg->cd.mair << CTXDESC_CD_3_MAIR_SHIFT);
928}
929
930/* Stream table manipulation functions */
931static void
932arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc)
933{
934 u64 val = 0;
935
936 val |= (desc->span & STRTAB_L1_DESC_SPAN_MASK)
937 << STRTAB_L1_DESC_SPAN_SHIFT;
938 val |= desc->l2ptr_dma &
939 STRTAB_L1_DESC_L2PTR_MASK << STRTAB_L1_DESC_L2PTR_SHIFT;
940
941 *dst = cpu_to_le64(val);
942}
943
944static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid)
945{
946 struct arm_smmu_cmdq_ent cmd = {
947 .opcode = CMDQ_OP_CFGI_STE,
948 .cfgi = {
949 .sid = sid,
950 .leaf = true,
951 },
952 };
953
954 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
955 cmd.opcode = CMDQ_OP_CMD_SYNC;
956 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
957}
958
959static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
960 __le64 *dst, struct arm_smmu_strtab_ent *ste)
961{
962 /*
963 * This is hideously complicated, but we only really care about
964 * three cases at the moment:
965 *
966 * 1. Invalid (all zero) -> bypass (init)
967 * 2. Bypass -> translation (attach)
968 * 3. Translation -> bypass (detach)
969 *
970 * Given that we can't update the STE atomically and the SMMU
971 * doesn't read the thing in a defined order, that leaves us
972 * with the following maintenance requirements:
973 *
974 * 1. Update Config, return (init time STEs aren't live)
975 * 2. Write everything apart from dword 0, sync, write dword 0, sync
976 * 3. Update Config, sync
977 */
978 u64 val = le64_to_cpu(dst[0]);
979 bool ste_live = false;
980 struct arm_smmu_cmdq_ent prefetch_cmd = {
981 .opcode = CMDQ_OP_PREFETCH_CFG,
982 .prefetch = {
983 .sid = sid,
984 },
985 };
986
987 if (val & STRTAB_STE_0_V) {
988 u64 cfg;
989
990 cfg = val & STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT;
991 switch (cfg) {
992 case STRTAB_STE_0_CFG_BYPASS:
993 break;
994 case STRTAB_STE_0_CFG_S1_TRANS:
995 case STRTAB_STE_0_CFG_S2_TRANS:
996 ste_live = true;
997 break;
998 default:
999 BUG(); /* STE corruption */
1000 }
1001 }
1002
1003 /* Nuke the existing Config, as we're going to rewrite it */
1004 val &= ~(STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT);
1005
1006 if (ste->valid)
1007 val |= STRTAB_STE_0_V;
1008 else
1009 val &= ~STRTAB_STE_0_V;
1010
1011 if (ste->bypass) {
1012 val |= disable_bypass ? STRTAB_STE_0_CFG_ABORT
1013 : STRTAB_STE_0_CFG_BYPASS;
1014 dst[0] = cpu_to_le64(val);
1015 dst[2] = 0; /* Nuke the VMID */
1016 if (ste_live)
1017 arm_smmu_sync_ste_for_sid(smmu, sid);
1018 return;
1019 }
1020
1021 if (ste->s1_cfg) {
1022 BUG_ON(ste_live);
1023 dst[1] = cpu_to_le64(
1024 STRTAB_STE_1_S1C_CACHE_WBRA
1025 << STRTAB_STE_1_S1CIR_SHIFT |
1026 STRTAB_STE_1_S1C_CACHE_WBRA
1027 << STRTAB_STE_1_S1COR_SHIFT |
1028 STRTAB_STE_1_S1C_SH_ISH << STRTAB_STE_1_S1CSH_SHIFT |
1029 STRTAB_STE_1_S1STALLD |
1030#ifdef CONFIG_PCI_ATS
1031 STRTAB_STE_1_EATS_TRANS << STRTAB_STE_1_EATS_SHIFT |
1032#endif
1033 STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT);
1034
1035 val |= (ste->s1_cfg->cdptr_dma & STRTAB_STE_0_S1CTXPTR_MASK
1036 << STRTAB_STE_0_S1CTXPTR_SHIFT) |
1037 STRTAB_STE_0_CFG_S1_TRANS;
1038
1039 }
1040
1041 if (ste->s2_cfg) {
1042 BUG_ON(ste_live);
1043 dst[2] = cpu_to_le64(
1044 ste->s2_cfg->vmid << STRTAB_STE_2_S2VMID_SHIFT |
1045 (ste->s2_cfg->vtcr & STRTAB_STE_2_VTCR_MASK)
1046 << STRTAB_STE_2_VTCR_SHIFT |
1047#ifdef __BIG_ENDIAN
1048 STRTAB_STE_2_S2ENDI |
1049#endif
1050 STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 |
1051 STRTAB_STE_2_S2R);
1052
1053 dst[3] = cpu_to_le64(ste->s2_cfg->vttbr &
1054 STRTAB_STE_3_S2TTB_MASK << STRTAB_STE_3_S2TTB_SHIFT);
1055
1056 val |= STRTAB_STE_0_CFG_S2_TRANS;
1057 }
1058
1059 arm_smmu_sync_ste_for_sid(smmu, sid);
1060 dst[0] = cpu_to_le64(val);
1061 arm_smmu_sync_ste_for_sid(smmu, sid);
1062
1063 /* It's likely that we'll want to use the new STE soon */
Zhen Lei5e929462015-07-07 04:30:18 +01001064 if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
1065 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
Will Deacon48ec83b2015-05-27 17:25:59 +01001066}
1067
1068static void arm_smmu_init_bypass_stes(u64 *strtab, unsigned int nent)
1069{
1070 unsigned int i;
1071 struct arm_smmu_strtab_ent ste = {
1072 .valid = true,
1073 .bypass = true,
1074 };
1075
1076 for (i = 0; i < nent; ++i) {
1077 arm_smmu_write_strtab_ent(NULL, -1, strtab, &ste);
1078 strtab += STRTAB_STE_DWORDS;
1079 }
1080}
1081
1082static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1083{
1084 size_t size;
1085 void *strtab;
1086 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1087 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[sid >> STRTAB_SPLIT];
1088
1089 if (desc->l2ptr)
1090 return 0;
1091
1092 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
Zhen Lei69146e72015-06-26 09:32:58 +01001093 strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
Will Deacon48ec83b2015-05-27 17:25:59 +01001094
1095 desc->span = STRTAB_SPLIT + 1;
1096 desc->l2ptr = dma_zalloc_coherent(smmu->dev, size, &desc->l2ptr_dma,
1097 GFP_KERNEL);
1098 if (!desc->l2ptr) {
1099 dev_err(smmu->dev,
1100 "failed to allocate l2 stream table for SID %u\n",
1101 sid);
1102 return -ENOMEM;
1103 }
1104
1105 arm_smmu_init_bypass_stes(desc->l2ptr, 1 << STRTAB_SPLIT);
1106 arm_smmu_write_strtab_l1_desc(strtab, desc);
1107 return 0;
1108}
1109
1110/* IRQ and event handlers */
1111static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
1112{
1113 int i;
1114 struct arm_smmu_device *smmu = dev;
1115 struct arm_smmu_queue *q = &smmu->evtq.q;
1116 u64 evt[EVTQ_ENT_DWORDS];
1117
1118 while (!queue_remove_raw(q, evt)) {
1119 u8 id = evt[0] >> EVTQ_0_ID_SHIFT & EVTQ_0_ID_MASK;
1120
1121 dev_info(smmu->dev, "event 0x%02x received:\n", id);
1122 for (i = 0; i < ARRAY_SIZE(evt); ++i)
1123 dev_info(smmu->dev, "\t0x%016llx\n",
1124 (unsigned long long)evt[i]);
1125 }
1126
1127 /* Sync our overflow flag, as we believe we're up to speed */
1128 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1129 return IRQ_HANDLED;
1130}
1131
1132static irqreturn_t arm_smmu_evtq_handler(int irq, void *dev)
1133{
1134 irqreturn_t ret = IRQ_WAKE_THREAD;
1135 struct arm_smmu_device *smmu = dev;
1136 struct arm_smmu_queue *q = &smmu->evtq.q;
1137
1138 /*
1139 * Not much we can do on overflow, so scream and pretend we're
1140 * trying harder.
1141 */
1142 if (queue_sync_prod(q) == -EOVERFLOW)
1143 dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
1144 else if (queue_empty(q))
1145 ret = IRQ_NONE;
1146
1147 return ret;
1148}
1149
1150static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
1151{
1152 struct arm_smmu_device *smmu = dev;
1153 struct arm_smmu_queue *q = &smmu->priq.q;
1154 u64 evt[PRIQ_ENT_DWORDS];
1155
1156 while (!queue_remove_raw(q, evt)) {
1157 u32 sid, ssid;
1158 u16 grpid;
1159 bool ssv, last;
1160
1161 sid = evt[0] >> PRIQ_0_SID_SHIFT & PRIQ_0_SID_MASK;
1162 ssv = evt[0] & PRIQ_0_SSID_V;
1163 ssid = ssv ? evt[0] >> PRIQ_0_SSID_SHIFT & PRIQ_0_SSID_MASK : 0;
1164 last = evt[0] & PRIQ_0_PRG_LAST;
1165 grpid = evt[1] >> PRIQ_1_PRG_IDX_SHIFT & PRIQ_1_PRG_IDX_MASK;
1166
1167 dev_info(smmu->dev, "unexpected PRI request received:\n");
1168 dev_info(smmu->dev,
1169 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
1170 sid, ssid, grpid, last ? "L" : "",
1171 evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
1172 evt[0] & PRIQ_0_PERM_READ ? "R" : "",
1173 evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
1174 evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
1175 evt[1] & PRIQ_1_ADDR_MASK << PRIQ_1_ADDR_SHIFT);
1176
1177 if (last) {
1178 struct arm_smmu_cmdq_ent cmd = {
1179 .opcode = CMDQ_OP_PRI_RESP,
1180 .substream_valid = ssv,
1181 .pri = {
1182 .sid = sid,
1183 .ssid = ssid,
1184 .grpid = grpid,
1185 .resp = PRI_RESP_DENY,
1186 },
1187 };
1188
1189 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1190 }
1191 }
1192
1193 /* Sync our overflow flag, as we believe we're up to speed */
1194 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1195 return IRQ_HANDLED;
1196}
1197
1198static irqreturn_t arm_smmu_priq_handler(int irq, void *dev)
1199{
1200 irqreturn_t ret = IRQ_WAKE_THREAD;
1201 struct arm_smmu_device *smmu = dev;
1202 struct arm_smmu_queue *q = &smmu->priq.q;
1203
1204 /* PRIQ overflow indicates a programming error */
1205 if (queue_sync_prod(q) == -EOVERFLOW)
1206 dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
1207 else if (queue_empty(q))
1208 ret = IRQ_NONE;
1209
1210 return ret;
1211}
1212
1213static irqreturn_t arm_smmu_cmdq_sync_handler(int irq, void *dev)
1214{
1215 /* We don't actually use CMD_SYNC interrupts for anything */
1216 return IRQ_HANDLED;
1217}
1218
1219static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
1220
1221static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
1222{
1223 u32 gerror, gerrorn;
1224 struct arm_smmu_device *smmu = dev;
1225
1226 gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
1227 gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
1228
1229 gerror ^= gerrorn;
1230 if (!(gerror & GERROR_ERR_MASK))
1231 return IRQ_NONE; /* No errors pending */
1232
1233 dev_warn(smmu->dev,
1234 "unexpected global error reported (0x%08x), this could be serious\n",
1235 gerror);
1236
1237 if (gerror & GERROR_SFM_ERR) {
1238 dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
1239 arm_smmu_device_disable(smmu);
1240 }
1241
1242 if (gerror & GERROR_MSI_GERROR_ABT_ERR)
1243 dev_warn(smmu->dev, "GERROR MSI write aborted\n");
1244
1245 if (gerror & GERROR_MSI_PRIQ_ABT_ERR) {
1246 dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
1247 arm_smmu_priq_handler(irq, smmu->dev);
1248 }
1249
1250 if (gerror & GERROR_MSI_EVTQ_ABT_ERR) {
1251 dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
1252 arm_smmu_evtq_handler(irq, smmu->dev);
1253 }
1254
1255 if (gerror & GERROR_MSI_CMDQ_ABT_ERR) {
1256 dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
1257 arm_smmu_cmdq_sync_handler(irq, smmu->dev);
1258 }
1259
1260 if (gerror & GERROR_PRIQ_ABT_ERR)
1261 dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
1262
1263 if (gerror & GERROR_EVTQ_ABT_ERR)
1264 dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
1265
1266 if (gerror & GERROR_CMDQ_ERR)
1267 arm_smmu_cmdq_skip_err(smmu);
1268
1269 writel(gerror, smmu->base + ARM_SMMU_GERRORN);
1270 return IRQ_HANDLED;
1271}
1272
1273/* IO_PGTABLE API */
1274static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
1275{
1276 struct arm_smmu_cmdq_ent cmd;
1277
1278 cmd.opcode = CMDQ_OP_CMD_SYNC;
1279 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1280}
1281
1282static void arm_smmu_tlb_sync(void *cookie)
1283{
1284 struct arm_smmu_domain *smmu_domain = cookie;
1285 __arm_smmu_tlb_sync(smmu_domain->smmu);
1286}
1287
1288static void arm_smmu_tlb_inv_context(void *cookie)
1289{
1290 struct arm_smmu_domain *smmu_domain = cookie;
1291 struct arm_smmu_device *smmu = smmu_domain->smmu;
1292 struct arm_smmu_cmdq_ent cmd;
1293
1294 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1295 cmd.opcode = CMDQ_OP_TLBI_NH_ASID;
1296 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1297 cmd.tlbi.vmid = 0;
1298 } else {
1299 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL;
1300 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1301 }
1302
1303 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1304 __arm_smmu_tlb_sync(smmu);
1305}
1306
1307static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
1308 bool leaf, void *cookie)
1309{
1310 struct arm_smmu_domain *smmu_domain = cookie;
1311 struct arm_smmu_device *smmu = smmu_domain->smmu;
1312 struct arm_smmu_cmdq_ent cmd = {
1313 .tlbi = {
1314 .leaf = leaf,
1315 .addr = iova,
1316 },
1317 };
1318
1319 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1320 cmd.opcode = CMDQ_OP_TLBI_NH_VA;
1321 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1322 } else {
1323 cmd.opcode = CMDQ_OP_TLBI_S2_IPA;
1324 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1325 }
1326
1327 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1328}
1329
Will Deacon48ec83b2015-05-27 17:25:59 +01001330static struct iommu_gather_ops arm_smmu_gather_ops = {
1331 .tlb_flush_all = arm_smmu_tlb_inv_context,
1332 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
1333 .tlb_sync = arm_smmu_tlb_sync,
Will Deacon48ec83b2015-05-27 17:25:59 +01001334};
1335
1336/* IOMMU API */
1337static bool arm_smmu_capable(enum iommu_cap cap)
1338{
1339 switch (cap) {
1340 case IOMMU_CAP_CACHE_COHERENCY:
1341 return true;
1342 case IOMMU_CAP_INTR_REMAP:
1343 return true; /* MSIs are just memory writes */
1344 case IOMMU_CAP_NOEXEC:
1345 return true;
1346 default:
1347 return false;
1348 }
1349}
1350
1351static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
1352{
1353 struct arm_smmu_domain *smmu_domain;
1354
1355 if (type != IOMMU_DOMAIN_UNMANAGED)
1356 return NULL;
1357
1358 /*
1359 * Allocate the domain and initialise some of its data structures.
1360 * We can't really do anything meaningful until we've added a
1361 * master.
1362 */
1363 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1364 if (!smmu_domain)
1365 return NULL;
1366
1367 mutex_init(&smmu_domain->init_mutex);
1368 spin_lock_init(&smmu_domain->pgtbl_lock);
1369 return &smmu_domain->domain;
1370}
1371
1372static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
1373{
1374 int idx, size = 1 << span;
1375
1376 do {
1377 idx = find_first_zero_bit(map, size);
1378 if (idx == size)
1379 return -ENOSPC;
1380 } while (test_and_set_bit(idx, map));
1381
1382 return idx;
1383}
1384
1385static void arm_smmu_bitmap_free(unsigned long *map, int idx)
1386{
1387 clear_bit(idx, map);
1388}
1389
1390static void arm_smmu_domain_free(struct iommu_domain *domain)
1391{
1392 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1393 struct arm_smmu_device *smmu = smmu_domain->smmu;
1394
Markus Elfringa6e08fb2015-06-29 17:47:43 +01001395 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon48ec83b2015-05-27 17:25:59 +01001396
1397 /* Free the CD and ASID, if we allocated them */
1398 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1399 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1400
1401 if (cfg->cdptr) {
1402 dma_free_coherent(smmu_domain->smmu->dev,
1403 CTXDESC_CD_DWORDS << 3,
1404 cfg->cdptr,
1405 cfg->cdptr_dma);
1406
1407 arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
1408 }
1409 } else {
1410 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1411 if (cfg->vmid)
1412 arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
1413 }
1414
1415 kfree(smmu_domain);
1416}
1417
1418static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
1419 struct io_pgtable_cfg *pgtbl_cfg)
1420{
1421 int ret;
1422 u16 asid;
1423 struct arm_smmu_device *smmu = smmu_domain->smmu;
1424 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1425
1426 asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
1427 if (IS_ERR_VALUE(asid))
1428 return asid;
1429
1430 cfg->cdptr = dma_zalloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
1431 &cfg->cdptr_dma, GFP_KERNEL);
1432 if (!cfg->cdptr) {
1433 dev_warn(smmu->dev, "failed to allocate context descriptor\n");
1434 goto out_free_asid;
1435 }
1436
1437 cfg->cd.asid = asid;
1438 cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
1439 cfg->cd.tcr = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
1440 cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
1441 return 0;
1442
1443out_free_asid:
1444 arm_smmu_bitmap_free(smmu->asid_map, asid);
1445 return ret;
1446}
1447
1448static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
1449 struct io_pgtable_cfg *pgtbl_cfg)
1450{
1451 u16 vmid;
1452 struct arm_smmu_device *smmu = smmu_domain->smmu;
1453 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1454
1455 vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
1456 if (IS_ERR_VALUE(vmid))
1457 return vmid;
1458
1459 cfg->vmid = vmid;
1460 cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
1461 cfg->vtcr = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
1462 return 0;
1463}
1464
1465static struct iommu_ops arm_smmu_ops;
1466
1467static int arm_smmu_domain_finalise(struct iommu_domain *domain)
1468{
1469 int ret;
1470 unsigned long ias, oas;
1471 enum io_pgtable_fmt fmt;
1472 struct io_pgtable_cfg pgtbl_cfg;
1473 struct io_pgtable_ops *pgtbl_ops;
1474 int (*finalise_stage_fn)(struct arm_smmu_domain *,
1475 struct io_pgtable_cfg *);
1476 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1477 struct arm_smmu_device *smmu = smmu_domain->smmu;
1478
1479 /* Restrict the stage to what we can actually support */
1480 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
1481 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
1482 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
1483 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1484
1485 switch (smmu_domain->stage) {
1486 case ARM_SMMU_DOMAIN_S1:
1487 ias = VA_BITS;
1488 oas = smmu->ias;
1489 fmt = ARM_64_LPAE_S1;
1490 finalise_stage_fn = arm_smmu_domain_finalise_s1;
1491 break;
1492 case ARM_SMMU_DOMAIN_NESTED:
1493 case ARM_SMMU_DOMAIN_S2:
1494 ias = smmu->ias;
1495 oas = smmu->oas;
1496 fmt = ARM_64_LPAE_S2;
1497 finalise_stage_fn = arm_smmu_domain_finalise_s2;
1498 break;
1499 default:
1500 return -EINVAL;
1501 }
1502
1503 pgtbl_cfg = (struct io_pgtable_cfg) {
1504 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
1505 .ias = ias,
1506 .oas = oas,
1507 .tlb = &arm_smmu_gather_ops,
Robin Murphybdc6d972015-07-29 19:46:07 +01001508 .iommu_dev = smmu->dev,
Will Deacon48ec83b2015-05-27 17:25:59 +01001509 };
1510
1511 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
1512 if (!pgtbl_ops)
1513 return -ENOMEM;
1514
1515 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
1516 smmu_domain->pgtbl_ops = pgtbl_ops;
1517
1518 ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
1519 if (IS_ERR_VALUE(ret))
1520 free_io_pgtable_ops(pgtbl_ops);
1521
1522 return ret;
1523}
1524
1525static struct arm_smmu_group *arm_smmu_group_get(struct device *dev)
1526{
1527 struct iommu_group *group;
1528 struct arm_smmu_group *smmu_group;
1529
1530 group = iommu_group_get(dev);
1531 if (!group)
1532 return NULL;
1533
1534 smmu_group = iommu_group_get_iommudata(group);
1535 iommu_group_put(group);
1536 return smmu_group;
1537}
1538
1539static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
1540{
1541 __le64 *step;
1542 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1543
1544 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1545 struct arm_smmu_strtab_l1_desc *l1_desc;
1546 int idx;
1547
1548 /* Two-level walk */
1549 idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
1550 l1_desc = &cfg->l1_desc[idx];
1551 idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
1552 step = &l1_desc->l2ptr[idx];
1553 } else {
1554 /* Simple linear lookup */
1555 step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
1556 }
1557
1558 return step;
1559}
1560
1561static int arm_smmu_install_ste_for_group(struct arm_smmu_group *smmu_group)
1562{
1563 int i;
1564 struct arm_smmu_domain *smmu_domain = smmu_group->domain;
1565 struct arm_smmu_strtab_ent *ste = &smmu_group->ste;
1566 struct arm_smmu_device *smmu = smmu_group->smmu;
1567
1568 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1569 ste->s1_cfg = &smmu_domain->s1_cfg;
1570 ste->s2_cfg = NULL;
1571 arm_smmu_write_ctx_desc(smmu, ste->s1_cfg);
1572 } else {
1573 ste->s1_cfg = NULL;
1574 ste->s2_cfg = &smmu_domain->s2_cfg;
1575 }
1576
1577 for (i = 0; i < smmu_group->num_sids; ++i) {
1578 u32 sid = smmu_group->sids[i];
1579 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
1580
1581 arm_smmu_write_strtab_ent(smmu, sid, step, ste);
1582 }
1583
1584 return 0;
1585}
1586
1587static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1588{
1589 int ret = 0;
1590 struct arm_smmu_device *smmu;
1591 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1592 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1593
1594 if (!smmu_group)
1595 return -ENOENT;
1596
1597 /* Already attached to a different domain? */
1598 if (smmu_group->domain && smmu_group->domain != smmu_domain)
1599 return -EEXIST;
1600
1601 smmu = smmu_group->smmu;
1602 mutex_lock(&smmu_domain->init_mutex);
1603
1604 if (!smmu_domain->smmu) {
1605 smmu_domain->smmu = smmu;
1606 ret = arm_smmu_domain_finalise(domain);
1607 if (ret) {
1608 smmu_domain->smmu = NULL;
1609 goto out_unlock;
1610 }
1611 } else if (smmu_domain->smmu != smmu) {
1612 dev_err(dev,
1613 "cannot attach to SMMU %s (upstream of %s)\n",
1614 dev_name(smmu_domain->smmu->dev),
1615 dev_name(smmu->dev));
1616 ret = -ENXIO;
1617 goto out_unlock;
1618 }
1619
1620 /* Group already attached to this domain? */
1621 if (smmu_group->domain)
1622 goto out_unlock;
1623
1624 smmu_group->domain = smmu_domain;
1625 smmu_group->ste.bypass = false;
1626
1627 ret = arm_smmu_install_ste_for_group(smmu_group);
1628 if (IS_ERR_VALUE(ret))
1629 smmu_group->domain = NULL;
1630
1631out_unlock:
1632 mutex_unlock(&smmu_domain->init_mutex);
1633 return ret;
1634}
1635
1636static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1637{
1638 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1639 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1640
1641 BUG_ON(!smmu_domain);
1642 BUG_ON(!smmu_group);
1643
1644 mutex_lock(&smmu_domain->init_mutex);
1645 BUG_ON(smmu_group->domain != smmu_domain);
1646
1647 smmu_group->ste.bypass = true;
1648 if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group)))
1649 dev_warn(dev, "failed to install bypass STE\n");
1650
1651 smmu_group->domain = NULL;
1652 mutex_unlock(&smmu_domain->init_mutex);
1653}
1654
1655static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1656 phys_addr_t paddr, size_t size, int prot)
1657{
1658 int ret;
1659 unsigned long flags;
1660 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1661 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1662
1663 if (!ops)
1664 return -ENODEV;
1665
1666 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1667 ret = ops->map(ops, iova, paddr, size, prot);
1668 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1669 return ret;
1670}
1671
1672static size_t
1673arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1674{
1675 size_t ret;
1676 unsigned long flags;
1677 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1678 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1679
1680 if (!ops)
1681 return 0;
1682
1683 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1684 ret = ops->unmap(ops, iova, size);
1685 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1686 return ret;
1687}
1688
1689static phys_addr_t
1690arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1691{
1692 phys_addr_t ret;
1693 unsigned long flags;
1694 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1695 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1696
1697 if (!ops)
1698 return 0;
1699
1700 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1701 ret = ops->iova_to_phys(ops, iova);
1702 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1703
1704 return ret;
1705}
1706
1707static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *sidp)
1708{
1709 *(u32 *)sidp = alias;
1710 return 0; /* Continue walking */
1711}
1712
1713static void __arm_smmu_release_pci_iommudata(void *data)
1714{
1715 kfree(data);
1716}
1717
1718static struct arm_smmu_device *arm_smmu_get_for_pci_dev(struct pci_dev *pdev)
1719{
1720 struct device_node *of_node;
Will Deacon941a8022015-08-11 16:25:10 +01001721 struct platform_device *smmu_pdev;
1722 struct arm_smmu_device *smmu = NULL;
Will Deacon48ec83b2015-05-27 17:25:59 +01001723 struct pci_bus *bus = pdev->bus;
1724
1725 /* Walk up to the root bus */
1726 while (!pci_is_root_bus(bus))
1727 bus = bus->parent;
1728
1729 /* Follow the "iommus" phandle from the host controller */
1730 of_node = of_parse_phandle(bus->bridge->parent->of_node, "iommus", 0);
1731 if (!of_node)
1732 return NULL;
1733
1734 /* See if we can find an SMMU corresponding to the phandle */
Will Deacon941a8022015-08-11 16:25:10 +01001735 smmu_pdev = of_find_device_by_node(of_node);
1736 if (smmu_pdev)
1737 smmu = platform_get_drvdata(smmu_pdev);
1738
Will Deacon48ec83b2015-05-27 17:25:59 +01001739 of_node_put(of_node);
1740 return smmu;
1741}
1742
1743static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
1744{
1745 unsigned long limit = smmu->strtab_cfg.num_l1_ents;
1746
1747 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
1748 limit *= 1UL << STRTAB_SPLIT;
1749
1750 return sid < limit;
1751}
1752
1753static int arm_smmu_add_device(struct device *dev)
1754{
1755 int i, ret;
1756 u32 sid, *sids;
1757 struct pci_dev *pdev;
1758 struct iommu_group *group;
1759 struct arm_smmu_group *smmu_group;
1760 struct arm_smmu_device *smmu;
1761
1762 /* We only support PCI, for now */
1763 if (!dev_is_pci(dev))
1764 return -ENODEV;
1765
1766 pdev = to_pci_dev(dev);
1767 group = iommu_group_get_for_dev(dev);
1768 if (IS_ERR(group))
1769 return PTR_ERR(group);
1770
1771 smmu_group = iommu_group_get_iommudata(group);
1772 if (!smmu_group) {
1773 smmu = arm_smmu_get_for_pci_dev(pdev);
1774 if (!smmu) {
1775 ret = -ENOENT;
1776 goto out_put_group;
1777 }
1778
1779 smmu_group = kzalloc(sizeof(*smmu_group), GFP_KERNEL);
1780 if (!smmu_group) {
1781 ret = -ENOMEM;
1782 goto out_put_group;
1783 }
1784
1785 smmu_group->ste.valid = true;
1786 smmu_group->smmu = smmu;
1787 iommu_group_set_iommudata(group, smmu_group,
1788 __arm_smmu_release_pci_iommudata);
1789 } else {
1790 smmu = smmu_group->smmu;
1791 }
1792
1793 /* Assume SID == RID until firmware tells us otherwise */
1794 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1795 for (i = 0; i < smmu_group->num_sids; ++i) {
1796 /* If we already know about this SID, then we're done */
1797 if (smmu_group->sids[i] == sid)
1798 return 0;
1799 }
1800
1801 /* Check the SID is in range of the SMMU and our stream table */
1802 if (!arm_smmu_sid_in_range(smmu, sid)) {
1803 ret = -ERANGE;
1804 goto out_put_group;
1805 }
1806
1807 /* Ensure l2 strtab is initialised */
1808 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1809 ret = arm_smmu_init_l2_strtab(smmu, sid);
1810 if (ret)
1811 goto out_put_group;
1812 }
1813
1814 /* Resize the SID array for the group */
1815 smmu_group->num_sids++;
1816 sids = krealloc(smmu_group->sids, smmu_group->num_sids * sizeof(*sids),
1817 GFP_KERNEL);
1818 if (!sids) {
1819 smmu_group->num_sids--;
1820 ret = -ENOMEM;
1821 goto out_put_group;
1822 }
1823
1824 /* Add the new SID */
1825 sids[smmu_group->num_sids - 1] = sid;
1826 smmu_group->sids = sids;
1827 return 0;
1828
1829out_put_group:
1830 iommu_group_put(group);
1831 return ret;
1832}
1833
1834static void arm_smmu_remove_device(struct device *dev)
1835{
1836 iommu_group_remove_device(dev);
1837}
1838
1839static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1840 enum iommu_attr attr, void *data)
1841{
1842 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1843
1844 switch (attr) {
1845 case DOMAIN_ATTR_NESTING:
1846 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1847 return 0;
1848 default:
1849 return -ENODEV;
1850 }
1851}
1852
1853static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1854 enum iommu_attr attr, void *data)
1855{
1856 int ret = 0;
1857 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1858
1859 mutex_lock(&smmu_domain->init_mutex);
1860
1861 switch (attr) {
1862 case DOMAIN_ATTR_NESTING:
1863 if (smmu_domain->smmu) {
1864 ret = -EPERM;
1865 goto out_unlock;
1866 }
1867
1868 if (*(int *)data)
1869 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1870 else
1871 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1872
1873 break;
1874 default:
1875 ret = -ENODEV;
1876 }
1877
1878out_unlock:
1879 mutex_unlock(&smmu_domain->init_mutex);
1880 return ret;
1881}
1882
1883static struct iommu_ops arm_smmu_ops = {
1884 .capable = arm_smmu_capable,
1885 .domain_alloc = arm_smmu_domain_alloc,
1886 .domain_free = arm_smmu_domain_free,
1887 .attach_dev = arm_smmu_attach_dev,
1888 .detach_dev = arm_smmu_detach_dev,
1889 .map = arm_smmu_map,
1890 .unmap = arm_smmu_unmap,
1891 .iova_to_phys = arm_smmu_iova_to_phys,
1892 .add_device = arm_smmu_add_device,
1893 .remove_device = arm_smmu_remove_device,
1894 .domain_get_attr = arm_smmu_domain_get_attr,
1895 .domain_set_attr = arm_smmu_domain_set_attr,
1896 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1897};
1898
1899/* Probing and initialisation functions */
1900static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
1901 struct arm_smmu_queue *q,
1902 unsigned long prod_off,
1903 unsigned long cons_off,
1904 size_t dwords)
1905{
1906 size_t qsz = ((1 << q->max_n_shift) * dwords) << 3;
1907
1908 q->base = dma_alloc_coherent(smmu->dev, qsz, &q->base_dma, GFP_KERNEL);
1909 if (!q->base) {
1910 dev_err(smmu->dev, "failed to allocate queue (0x%zx bytes)\n",
1911 qsz);
1912 return -ENOMEM;
1913 }
1914
1915 q->prod_reg = smmu->base + prod_off;
1916 q->cons_reg = smmu->base + cons_off;
1917 q->ent_dwords = dwords;
1918
1919 q->q_base = Q_BASE_RWA;
1920 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK << Q_BASE_ADDR_SHIFT;
1921 q->q_base |= (q->max_n_shift & Q_BASE_LOG2SIZE_MASK)
1922 << Q_BASE_LOG2SIZE_SHIFT;
1923
1924 q->prod = q->cons = 0;
1925 return 0;
1926}
1927
1928static void arm_smmu_free_one_queue(struct arm_smmu_device *smmu,
1929 struct arm_smmu_queue *q)
1930{
1931 size_t qsz = ((1 << q->max_n_shift) * q->ent_dwords) << 3;
1932
1933 dma_free_coherent(smmu->dev, qsz, q->base, q->base_dma);
1934}
1935
1936static void arm_smmu_free_queues(struct arm_smmu_device *smmu)
1937{
1938 arm_smmu_free_one_queue(smmu, &smmu->cmdq.q);
1939 arm_smmu_free_one_queue(smmu, &smmu->evtq.q);
1940
1941 if (smmu->features & ARM_SMMU_FEAT_PRI)
1942 arm_smmu_free_one_queue(smmu, &smmu->priq.q);
1943}
1944
1945static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
1946{
1947 int ret;
1948
1949 /* cmdq */
1950 spin_lock_init(&smmu->cmdq.lock);
1951 ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
1952 ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS);
1953 if (ret)
1954 goto out;
1955
1956 /* evtq */
1957 ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
1958 ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS);
1959 if (ret)
1960 goto out_free_cmdq;
1961
1962 /* priq */
1963 if (!(smmu->features & ARM_SMMU_FEAT_PRI))
1964 return 0;
1965
1966 ret = arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
1967 ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS);
1968 if (ret)
1969 goto out_free_evtq;
1970
1971 return 0;
1972
1973out_free_evtq:
1974 arm_smmu_free_one_queue(smmu, &smmu->evtq.q);
1975out_free_cmdq:
1976 arm_smmu_free_one_queue(smmu, &smmu->cmdq.q);
1977out:
1978 return ret;
1979}
1980
1981static void arm_smmu_free_l2_strtab(struct arm_smmu_device *smmu)
1982{
1983 int i;
1984 size_t size;
1985 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1986
1987 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
1988 for (i = 0; i < cfg->num_l1_ents; ++i) {
1989 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[i];
1990
1991 if (!desc->l2ptr)
1992 continue;
1993
1994 dma_free_coherent(smmu->dev, size, desc->l2ptr,
1995 desc->l2ptr_dma);
1996 }
1997}
1998
1999static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
2000{
2001 unsigned int i;
2002 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2003 size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
2004 void *strtab = smmu->strtab_cfg.strtab;
2005
2006 cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
2007 if (!cfg->l1_desc) {
2008 dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
2009 return -ENOMEM;
2010 }
2011
2012 for (i = 0; i < cfg->num_l1_ents; ++i) {
2013 arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
2014 strtab += STRTAB_L1_DESC_DWORDS << 3;
2015 }
2016
2017 return 0;
2018}
2019
2020static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
2021{
2022 void *strtab;
2023 u64 reg;
Will Deacond2e88e72015-06-30 10:02:28 +01002024 u32 size, l1size;
Will Deacon48ec83b2015-05-27 17:25:59 +01002025 int ret;
2026 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2027
Will Deacon28c8b402015-07-16 17:50:12 +01002028 /*
2029 * If we can resolve everything with a single L2 table, then we
2030 * just need a single L1 descriptor. Otherwise, calculate the L1
2031 * size, capped to the SIDSIZE.
2032 */
2033 if (smmu->sid_bits < STRTAB_SPLIT) {
2034 size = 0;
2035 } else {
2036 size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
2037 size = min(size, smmu->sid_bits - STRTAB_SPLIT);
2038 }
Will Deacond2e88e72015-06-30 10:02:28 +01002039 cfg->num_l1_ents = 1 << size;
2040
2041 size += STRTAB_SPLIT;
2042 if (size < smmu->sid_bits)
Will Deacon48ec83b2015-05-27 17:25:59 +01002043 dev_warn(smmu->dev,
2044 "2-level strtab only covers %u/%u bits of SID\n",
Will Deacond2e88e72015-06-30 10:02:28 +01002045 size, smmu->sid_bits);
Will Deacon48ec83b2015-05-27 17:25:59 +01002046
Will Deacond2e88e72015-06-30 10:02:28 +01002047 l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
2048 strtab = dma_zalloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
Will Deacon48ec83b2015-05-27 17:25:59 +01002049 GFP_KERNEL);
2050 if (!strtab) {
2051 dev_err(smmu->dev,
2052 "failed to allocate l1 stream table (%u bytes)\n",
2053 size);
2054 return -ENOMEM;
2055 }
2056 cfg->strtab = strtab;
2057
2058 /* Configure strtab_base_cfg for 2 levels */
2059 reg = STRTAB_BASE_CFG_FMT_2LVL;
2060 reg |= (size & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2061 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2062 reg |= (STRTAB_SPLIT & STRTAB_BASE_CFG_SPLIT_MASK)
2063 << STRTAB_BASE_CFG_SPLIT_SHIFT;
2064 cfg->strtab_base_cfg = reg;
2065
2066 ret = arm_smmu_init_l1_strtab(smmu);
2067 if (ret)
2068 dma_free_coherent(smmu->dev,
Will Deacond2e88e72015-06-30 10:02:28 +01002069 l1size,
Will Deacon48ec83b2015-05-27 17:25:59 +01002070 strtab,
2071 cfg->strtab_dma);
2072 return ret;
2073}
2074
2075static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
2076{
2077 void *strtab;
2078 u64 reg;
2079 u32 size;
2080 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2081
2082 size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
2083 strtab = dma_zalloc_coherent(smmu->dev, size, &cfg->strtab_dma,
2084 GFP_KERNEL);
2085 if (!strtab) {
2086 dev_err(smmu->dev,
2087 "failed to allocate linear stream table (%u bytes)\n",
2088 size);
2089 return -ENOMEM;
2090 }
2091 cfg->strtab = strtab;
2092 cfg->num_l1_ents = 1 << smmu->sid_bits;
2093
2094 /* Configure strtab_base_cfg for a linear table covering all SIDs */
2095 reg = STRTAB_BASE_CFG_FMT_LINEAR;
2096 reg |= (smmu->sid_bits & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2097 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2098 cfg->strtab_base_cfg = reg;
2099
2100 arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
2101 return 0;
2102}
2103
2104static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
2105{
2106 u64 reg;
2107 int ret;
2108
2109 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
2110 ret = arm_smmu_init_strtab_2lvl(smmu);
2111 else
2112 ret = arm_smmu_init_strtab_linear(smmu);
2113
2114 if (ret)
2115 return ret;
2116
2117 /* Set the strtab base address */
2118 reg = smmu->strtab_cfg.strtab_dma &
2119 STRTAB_BASE_ADDR_MASK << STRTAB_BASE_ADDR_SHIFT;
2120 reg |= STRTAB_BASE_RA;
2121 smmu->strtab_cfg.strtab_base = reg;
2122
2123 /* Allocate the first VMID for stage-2 bypass STEs */
2124 set_bit(0, smmu->vmid_map);
2125 return 0;
2126}
2127
2128static void arm_smmu_free_strtab(struct arm_smmu_device *smmu)
2129{
2130 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2131 u32 size = cfg->num_l1_ents;
2132
2133 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
2134 arm_smmu_free_l2_strtab(smmu);
2135 size *= STRTAB_L1_DESC_DWORDS << 3;
2136 } else {
2137 size *= STRTAB_STE_DWORDS * 3;
2138 }
2139
2140 dma_free_coherent(smmu->dev, size, cfg->strtab, cfg->strtab_dma);
2141}
2142
2143static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
2144{
2145 int ret;
2146
2147 ret = arm_smmu_init_queues(smmu);
2148 if (ret)
2149 return ret;
2150
2151 ret = arm_smmu_init_strtab(smmu);
2152 if (ret)
2153 goto out_free_queues;
2154
2155 return 0;
2156
2157out_free_queues:
2158 arm_smmu_free_queues(smmu);
2159 return ret;
2160}
2161
2162static void arm_smmu_free_structures(struct arm_smmu_device *smmu)
2163{
2164 arm_smmu_free_strtab(smmu);
2165 arm_smmu_free_queues(smmu);
2166}
2167
2168static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
2169 unsigned int reg_off, unsigned int ack_off)
2170{
2171 u32 reg;
2172
2173 writel_relaxed(val, smmu->base + reg_off);
2174 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
2175 1, ARM_SMMU_POLL_TIMEOUT_US);
2176}
2177
2178static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
2179{
2180 int ret, irq;
Marc Zyngierccd63852015-07-15 11:55:18 +01002181 u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
Will Deacon48ec83b2015-05-27 17:25:59 +01002182
2183 /* Disable IRQs first */
2184 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
2185 ARM_SMMU_IRQ_CTRLACK);
2186 if (ret) {
2187 dev_err(smmu->dev, "failed to disable irqs\n");
2188 return ret;
2189 }
2190
2191 /* Clear the MSI address regs */
2192 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
2193 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
2194
2195 /* Request wired interrupt lines */
2196 irq = smmu->evtq.q.irq;
2197 if (irq) {
2198 ret = devm_request_threaded_irq(smmu->dev, irq,
2199 arm_smmu_evtq_handler,
2200 arm_smmu_evtq_thread,
2201 0, "arm-smmu-v3-evtq", smmu);
2202 if (IS_ERR_VALUE(ret))
2203 dev_warn(smmu->dev, "failed to enable evtq irq\n");
2204 }
2205
2206 irq = smmu->cmdq.q.irq;
2207 if (irq) {
2208 ret = devm_request_irq(smmu->dev, irq,
2209 arm_smmu_cmdq_sync_handler, 0,
2210 "arm-smmu-v3-cmdq-sync", smmu);
2211 if (IS_ERR_VALUE(ret))
2212 dev_warn(smmu->dev, "failed to enable cmdq-sync irq\n");
2213 }
2214
2215 irq = smmu->gerr_irq;
2216 if (irq) {
2217 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
2218 0, "arm-smmu-v3-gerror", smmu);
2219 if (IS_ERR_VALUE(ret))
2220 dev_warn(smmu->dev, "failed to enable gerror irq\n");
2221 }
2222
2223 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2224 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
2225
2226 irq = smmu->priq.q.irq;
2227 if (irq) {
2228 ret = devm_request_threaded_irq(smmu->dev, irq,
2229 arm_smmu_priq_handler,
2230 arm_smmu_priq_thread,
2231 0, "arm-smmu-v3-priq",
2232 smmu);
2233 if (IS_ERR_VALUE(ret))
2234 dev_warn(smmu->dev,
2235 "failed to enable priq irq\n");
Marc Zyngierccd63852015-07-15 11:55:18 +01002236 else
2237 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
Will Deacon48ec83b2015-05-27 17:25:59 +01002238 }
2239 }
2240
2241 /* Enable interrupt generation on the SMMU */
Marc Zyngierccd63852015-07-15 11:55:18 +01002242 ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
Will Deacon48ec83b2015-05-27 17:25:59 +01002243 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
2244 if (ret)
2245 dev_warn(smmu->dev, "failed to enable irqs\n");
2246
2247 return 0;
2248}
2249
2250static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
2251{
2252 int ret;
2253
2254 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
2255 if (ret)
2256 dev_err(smmu->dev, "failed to clear cr0\n");
2257
2258 return ret;
2259}
2260
2261static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
2262{
2263 int ret;
2264 u32 reg, enables;
2265 struct arm_smmu_cmdq_ent cmd;
2266
2267 /* Clear CR0 and sync (disables SMMU and queue processing) */
2268 reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
2269 if (reg & CR0_SMMUEN)
2270 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
2271
2272 ret = arm_smmu_device_disable(smmu);
2273 if (ret)
2274 return ret;
2275
2276 /* CR1 (table and queue memory attributes) */
2277 reg = (CR1_SH_ISH << CR1_TABLE_SH_SHIFT) |
2278 (CR1_CACHE_WB << CR1_TABLE_OC_SHIFT) |
2279 (CR1_CACHE_WB << CR1_TABLE_IC_SHIFT) |
2280 (CR1_SH_ISH << CR1_QUEUE_SH_SHIFT) |
2281 (CR1_CACHE_WB << CR1_QUEUE_OC_SHIFT) |
2282 (CR1_CACHE_WB << CR1_QUEUE_IC_SHIFT);
2283 writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
2284
2285 /* CR2 (random crap) */
2286 reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
2287 writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
2288
2289 /* Stream table */
2290 writeq_relaxed(smmu->strtab_cfg.strtab_base,
2291 smmu->base + ARM_SMMU_STRTAB_BASE);
2292 writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
2293 smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
2294
2295 /* Command queue */
2296 writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
2297 writel_relaxed(smmu->cmdq.q.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
2298 writel_relaxed(smmu->cmdq.q.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
2299
2300 enables = CR0_CMDQEN;
2301 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2302 ARM_SMMU_CR0ACK);
2303 if (ret) {
2304 dev_err(smmu->dev, "failed to enable command queue\n");
2305 return ret;
2306 }
2307
2308 /* Invalidate any cached configuration */
2309 cmd.opcode = CMDQ_OP_CFGI_ALL;
2310 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2311 cmd.opcode = CMDQ_OP_CMD_SYNC;
2312 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2313
2314 /* Invalidate any stale TLB entries */
2315 if (smmu->features & ARM_SMMU_FEAT_HYP) {
2316 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
2317 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2318 }
2319
2320 cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
2321 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2322 cmd.opcode = CMDQ_OP_CMD_SYNC;
2323 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2324
2325 /* Event queue */
2326 writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
2327 writel_relaxed(smmu->evtq.q.prod, smmu->base + ARM_SMMU_EVTQ_PROD);
2328 writel_relaxed(smmu->evtq.q.cons, smmu->base + ARM_SMMU_EVTQ_CONS);
2329
2330 enables |= CR0_EVTQEN;
2331 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2332 ARM_SMMU_CR0ACK);
2333 if (ret) {
2334 dev_err(smmu->dev, "failed to enable event queue\n");
2335 return ret;
2336 }
2337
2338 /* PRI queue */
2339 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2340 writeq_relaxed(smmu->priq.q.q_base,
2341 smmu->base + ARM_SMMU_PRIQ_BASE);
2342 writel_relaxed(smmu->priq.q.prod,
2343 smmu->base + ARM_SMMU_PRIQ_PROD);
2344 writel_relaxed(smmu->priq.q.cons,
2345 smmu->base + ARM_SMMU_PRIQ_CONS);
2346
2347 enables |= CR0_PRIQEN;
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 PRI queue\n");
2352 return ret;
2353 }
2354 }
2355
2356 ret = arm_smmu_setup_irqs(smmu);
2357 if (ret) {
2358 dev_err(smmu->dev, "failed to setup irqs\n");
2359 return ret;
2360 }
2361
2362 /* Enable the SMMU interface */
2363 enables |= CR0_SMMUEN;
2364 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2365 ARM_SMMU_CR0ACK);
2366 if (ret) {
2367 dev_err(smmu->dev, "failed to enable SMMU interface\n");
2368 return ret;
2369 }
2370
2371 return 0;
2372}
2373
2374static int arm_smmu_device_probe(struct arm_smmu_device *smmu)
2375{
2376 u32 reg;
2377 bool coherent;
2378 unsigned long pgsize_bitmap = 0;
2379
2380 /* IDR0 */
2381 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
2382
2383 /* 2-level structures */
2384 if ((reg & IDR0_ST_LVL_MASK << IDR0_ST_LVL_SHIFT) == IDR0_ST_LVL_2LVL)
2385 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
2386
2387 if (reg & IDR0_CD2L)
2388 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
2389
2390 /*
2391 * Translation table endianness.
2392 * We currently require the same endianness as the CPU, but this
2393 * could be changed later by adding a new IO_PGTABLE_QUIRK.
2394 */
2395 switch (reg & IDR0_TTENDIAN_MASK << IDR0_TTENDIAN_SHIFT) {
2396 case IDR0_TTENDIAN_MIXED:
2397 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
2398 break;
2399#ifdef __BIG_ENDIAN
2400 case IDR0_TTENDIAN_BE:
2401 smmu->features |= ARM_SMMU_FEAT_TT_BE;
2402 break;
2403#else
2404 case IDR0_TTENDIAN_LE:
2405 smmu->features |= ARM_SMMU_FEAT_TT_LE;
2406 break;
2407#endif
2408 default:
2409 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
2410 return -ENXIO;
2411 }
2412
2413 /* Boolean feature flags */
2414 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
2415 smmu->features |= ARM_SMMU_FEAT_PRI;
2416
2417 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
2418 smmu->features |= ARM_SMMU_FEAT_ATS;
2419
2420 if (reg & IDR0_SEV)
2421 smmu->features |= ARM_SMMU_FEAT_SEV;
2422
2423 if (reg & IDR0_MSI)
2424 smmu->features |= ARM_SMMU_FEAT_MSI;
2425
2426 if (reg & IDR0_HYP)
2427 smmu->features |= ARM_SMMU_FEAT_HYP;
2428
2429 /*
2430 * The dma-coherent property is used in preference to the ID
2431 * register, but warn on mismatch.
2432 */
2433 coherent = of_dma_is_coherent(smmu->dev->of_node);
2434 if (coherent)
2435 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2436
2437 if (!!(reg & IDR0_COHACC) != coherent)
2438 dev_warn(smmu->dev, "IDR0.COHACC overridden by dma-coherent property (%s)\n",
2439 coherent ? "true" : "false");
2440
2441 if (reg & IDR0_STALL_MODEL)
2442 smmu->features |= ARM_SMMU_FEAT_STALLS;
2443
2444 if (reg & IDR0_S1P)
2445 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
2446
2447 if (reg & IDR0_S2P)
2448 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
2449
2450 if (!(reg & (IDR0_S1P | IDR0_S2P))) {
2451 dev_err(smmu->dev, "no translation support!\n");
2452 return -ENXIO;
2453 }
2454
2455 /* We only support the AArch64 table format at present */
2456 if ((reg & IDR0_TTF_MASK << IDR0_TTF_SHIFT) < IDR0_TTF_AARCH64) {
2457 dev_err(smmu->dev, "AArch64 table format not supported!\n");
2458 return -ENXIO;
2459 }
2460
2461 /* ASID/VMID sizes */
2462 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
2463 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
2464
2465 /* IDR1 */
2466 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
2467 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
2468 dev_err(smmu->dev, "embedded implementation not supported\n");
2469 return -ENXIO;
2470 }
2471
2472 /* Queue sizes, capped at 4k */
2473 smmu->cmdq.q.max_n_shift = min((u32)CMDQ_MAX_SZ_SHIFT,
2474 reg >> IDR1_CMDQ_SHIFT & IDR1_CMDQ_MASK);
2475 if (!smmu->cmdq.q.max_n_shift) {
2476 /* Odd alignment restrictions on the base, so ignore for now */
2477 dev_err(smmu->dev, "unit-length command queue not supported\n");
2478 return -ENXIO;
2479 }
2480
2481 smmu->evtq.q.max_n_shift = min((u32)EVTQ_MAX_SZ_SHIFT,
2482 reg >> IDR1_EVTQ_SHIFT & IDR1_EVTQ_MASK);
2483 smmu->priq.q.max_n_shift = min((u32)PRIQ_MAX_SZ_SHIFT,
2484 reg >> IDR1_PRIQ_SHIFT & IDR1_PRIQ_MASK);
2485
2486 /* SID/SSID sizes */
2487 smmu->ssid_bits = reg >> IDR1_SSID_SHIFT & IDR1_SSID_MASK;
2488 smmu->sid_bits = reg >> IDR1_SID_SHIFT & IDR1_SID_MASK;
2489
2490 /* IDR5 */
2491 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
2492
2493 /* Maximum number of outstanding stalls */
2494 smmu->evtq.max_stalls = reg >> IDR5_STALL_MAX_SHIFT
2495 & IDR5_STALL_MAX_MASK;
2496
2497 /* Page sizes */
2498 if (reg & IDR5_GRAN64K)
2499 pgsize_bitmap |= SZ_64K | SZ_512M;
2500 if (reg & IDR5_GRAN16K)
2501 pgsize_bitmap |= SZ_16K | SZ_32M;
2502 if (reg & IDR5_GRAN4K)
2503 pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
2504
2505 arm_smmu_ops.pgsize_bitmap &= pgsize_bitmap;
2506
2507 /* Output address size */
2508 switch (reg & IDR5_OAS_MASK << IDR5_OAS_SHIFT) {
2509 case IDR5_OAS_32_BIT:
2510 smmu->oas = 32;
2511 break;
2512 case IDR5_OAS_36_BIT:
2513 smmu->oas = 36;
2514 break;
2515 case IDR5_OAS_40_BIT:
2516 smmu->oas = 40;
2517 break;
2518 case IDR5_OAS_42_BIT:
2519 smmu->oas = 42;
2520 break;
2521 case IDR5_OAS_44_BIT:
2522 smmu->oas = 44;
2523 break;
Will Deacon85430962015-08-03 10:35:40 +01002524 default:
2525 dev_info(smmu->dev,
2526 "unknown output address size. Truncating to 48-bit\n");
2527 /* Fallthrough */
Will Deacon48ec83b2015-05-27 17:25:59 +01002528 case IDR5_OAS_48_BIT:
2529 smmu->oas = 48;
Will Deacon48ec83b2015-05-27 17:25:59 +01002530 }
2531
2532 /* Set the DMA mask for our table walker */
2533 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
2534 dev_warn(smmu->dev,
2535 "failed to set DMA mask for table walker\n");
2536
2537 if (!smmu->ias)
2538 smmu->ias = smmu->oas;
2539
2540 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
2541 smmu->ias, smmu->oas, smmu->features);
2542 return 0;
2543}
2544
2545static int arm_smmu_device_dt_probe(struct platform_device *pdev)
2546{
2547 int irq, ret;
2548 struct resource *res;
2549 struct arm_smmu_device *smmu;
2550 struct device *dev = &pdev->dev;
2551
2552 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2553 if (!smmu) {
2554 dev_err(dev, "failed to allocate arm_smmu_device\n");
2555 return -ENOMEM;
2556 }
2557 smmu->dev = dev;
2558
2559 /* Base address */
2560 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2561 if (resource_size(res) + 1 < SZ_128K) {
2562 dev_err(dev, "MMIO region too small (%pr)\n", res);
2563 return -EINVAL;
2564 }
2565
2566 smmu->base = devm_ioremap_resource(dev, res);
2567 if (IS_ERR(smmu->base))
2568 return PTR_ERR(smmu->base);
2569
2570 /* Interrupt lines */
2571 irq = platform_get_irq_byname(pdev, "eventq");
2572 if (irq > 0)
2573 smmu->evtq.q.irq = irq;
2574
2575 irq = platform_get_irq_byname(pdev, "priq");
2576 if (irq > 0)
2577 smmu->priq.q.irq = irq;
2578
2579 irq = platform_get_irq_byname(pdev, "cmdq-sync");
2580 if (irq > 0)
2581 smmu->cmdq.q.irq = irq;
2582
2583 irq = platform_get_irq_byname(pdev, "gerror");
2584 if (irq > 0)
2585 smmu->gerr_irq = irq;
2586
Zhen Lei5e929462015-07-07 04:30:18 +01002587 parse_driver_options(smmu);
2588
Will Deacon48ec83b2015-05-27 17:25:59 +01002589 /* Probe the h/w */
2590 ret = arm_smmu_device_probe(smmu);
2591 if (ret)
2592 return ret;
2593
2594 /* Initialise in-memory data structures */
2595 ret = arm_smmu_init_structures(smmu);
2596 if (ret)
2597 return ret;
2598
2599 /* Reset the device */
2600 ret = arm_smmu_device_reset(smmu);
2601 if (ret)
2602 goto out_free_structures;
2603
2604 /* Record our private device structure */
Will Deacon941a8022015-08-11 16:25:10 +01002605 platform_set_drvdata(pdev, smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002606 return 0;
2607
2608out_free_structures:
2609 arm_smmu_free_structures(smmu);
2610 return ret;
2611}
2612
2613static int arm_smmu_device_remove(struct platform_device *pdev)
2614{
Will Deacon941a8022015-08-11 16:25:10 +01002615 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
Will Deacon48ec83b2015-05-27 17:25:59 +01002616
2617 arm_smmu_device_disable(smmu);
2618 arm_smmu_free_structures(smmu);
2619 return 0;
2620}
2621
2622static struct of_device_id arm_smmu_of_match[] = {
2623 { .compatible = "arm,smmu-v3", },
2624 { },
2625};
2626MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2627
2628static struct platform_driver arm_smmu_driver = {
2629 .driver = {
2630 .name = "arm-smmu-v3",
2631 .of_match_table = of_match_ptr(arm_smmu_of_match),
2632 },
2633 .probe = arm_smmu_device_dt_probe,
2634 .remove = arm_smmu_device_remove,
2635};
2636
2637static int __init arm_smmu_init(void)
2638{
2639 struct device_node *np;
2640 int ret;
2641
2642 np = of_find_matching_node(NULL, arm_smmu_of_match);
2643 if (!np)
2644 return 0;
2645
2646 of_node_put(np);
2647
2648 ret = platform_driver_register(&arm_smmu_driver);
2649 if (ret)
2650 return ret;
2651
2652 return bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2653}
2654
2655static void __exit arm_smmu_exit(void)
2656{
2657 return platform_driver_unregister(&arm_smmu_driver);
2658}
2659
2660subsys_initcall(arm_smmu_init);
2661module_exit(arm_smmu_exit);
2662
2663MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
2664MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2665MODULE_LICENSE("GPL v2");