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