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