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