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