blob: 4c5ef4e5da98ebf9dfda7d63b5895fb6d8206592 [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,
Will Deacona0d5c042015-12-04 12:00:29 +0000860 idx < ARRAY_SIZE(cerror_str) ? cerror_str[idx] : "Unknown");
Will Deacon48ec83b2015-05-27 17:25:59 +0100861
862 switch (idx) {
Will Deacon48ec83b2015-05-27 17:25:59 +0100863 case CMDQ_ERR_CERROR_ABT_IDX:
864 dev_err(smmu->dev, "retrying command fetch\n");
865 case CMDQ_ERR_CERROR_NONE_IDX:
866 return;
Will Deacona0d5c042015-12-04 12:00:29 +0000867 case CMDQ_ERR_CERROR_ILL_IDX:
868 /* Fallthrough */
869 default:
870 break;
Will Deacon48ec83b2015-05-27 17:25:59 +0100871 }
872
873 /*
874 * We may have concurrent producers, so we need to be careful
875 * not to touch any of the shadow cmdq state.
876 */
877 queue_read(cmd, Q_ENT(q, idx), q->ent_dwords);
878 dev_err(smmu->dev, "skipping command in error state:\n");
879 for (i = 0; i < ARRAY_SIZE(cmd); ++i)
880 dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
881
882 /* Convert the erroneous command into a CMD_SYNC */
883 if (arm_smmu_cmdq_build_cmd(cmd, &cmd_sync)) {
884 dev_err(smmu->dev, "failed to convert to CMD_SYNC\n");
885 return;
886 }
887
888 queue_write(cmd, Q_ENT(q, idx), q->ent_dwords);
889}
890
891static void arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
892 struct arm_smmu_cmdq_ent *ent)
893{
894 u32 until;
895 u64 cmd[CMDQ_ENT_DWORDS];
896 bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
897 struct arm_smmu_queue *q = &smmu->cmdq.q;
898
899 if (arm_smmu_cmdq_build_cmd(cmd, ent)) {
900 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
901 ent->opcode);
902 return;
903 }
904
905 spin_lock(&smmu->cmdq.lock);
906 while (until = q->prod + 1, queue_insert_raw(q, cmd) == -ENOSPC) {
907 /*
908 * Keep the queue locked, otherwise the producer could wrap
909 * twice and we could see a future consumer pointer that looks
910 * like it's behind us.
911 */
912 if (queue_poll_cons(q, until, wfe))
913 dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
914 }
915
916 if (ent->opcode == CMDQ_OP_CMD_SYNC && queue_poll_cons(q, until, wfe))
917 dev_err_ratelimited(smmu->dev, "CMD_SYNC timeout\n");
918 spin_unlock(&smmu->cmdq.lock);
919}
920
921/* Context descriptor manipulation functions */
922static u64 arm_smmu_cpu_tcr_to_cd(u64 tcr)
923{
924 u64 val = 0;
925
926 /* Repack the TCR. Just care about TTBR0 for now */
927 val |= ARM_SMMU_TCR2CD(tcr, T0SZ);
928 val |= ARM_SMMU_TCR2CD(tcr, TG0);
929 val |= ARM_SMMU_TCR2CD(tcr, IRGN0);
930 val |= ARM_SMMU_TCR2CD(tcr, ORGN0);
931 val |= ARM_SMMU_TCR2CD(tcr, SH0);
932 val |= ARM_SMMU_TCR2CD(tcr, EPD0);
933 val |= ARM_SMMU_TCR2CD(tcr, EPD1);
934 val |= ARM_SMMU_TCR2CD(tcr, IPS);
935 val |= ARM_SMMU_TCR2CD(tcr, TBI0);
936
937 return val;
938}
939
940static void arm_smmu_write_ctx_desc(struct arm_smmu_device *smmu,
941 struct arm_smmu_s1_cfg *cfg)
942{
943 u64 val;
944
945 /*
946 * We don't need to issue any invalidation here, as we'll invalidate
947 * the STE when installing the new entry anyway.
948 */
949 val = arm_smmu_cpu_tcr_to_cd(cfg->cd.tcr) |
950#ifdef __BIG_ENDIAN
951 CTXDESC_CD_0_ENDI |
952#endif
953 CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET_PRIVATE |
954 CTXDESC_CD_0_AA64 | (u64)cfg->cd.asid << CTXDESC_CD_0_ASID_SHIFT |
955 CTXDESC_CD_0_V;
956 cfg->cdptr[0] = cpu_to_le64(val);
957
958 val = cfg->cd.ttbr & CTXDESC_CD_1_TTB0_MASK << CTXDESC_CD_1_TTB0_SHIFT;
959 cfg->cdptr[1] = cpu_to_le64(val);
960
961 cfg->cdptr[3] = cpu_to_le64(cfg->cd.mair << CTXDESC_CD_3_MAIR_SHIFT);
962}
963
964/* Stream table manipulation functions */
965static void
966arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc)
967{
968 u64 val = 0;
969
970 val |= (desc->span & STRTAB_L1_DESC_SPAN_MASK)
971 << STRTAB_L1_DESC_SPAN_SHIFT;
972 val |= desc->l2ptr_dma &
973 STRTAB_L1_DESC_L2PTR_MASK << STRTAB_L1_DESC_L2PTR_SHIFT;
974
975 *dst = cpu_to_le64(val);
976}
977
978static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid)
979{
980 struct arm_smmu_cmdq_ent cmd = {
981 .opcode = CMDQ_OP_CFGI_STE,
982 .cfgi = {
983 .sid = sid,
984 .leaf = true,
985 },
986 };
987
988 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
989 cmd.opcode = CMDQ_OP_CMD_SYNC;
990 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
991}
992
993static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
994 __le64 *dst, struct arm_smmu_strtab_ent *ste)
995{
996 /*
997 * This is hideously complicated, but we only really care about
998 * three cases at the moment:
999 *
1000 * 1. Invalid (all zero) -> bypass (init)
1001 * 2. Bypass -> translation (attach)
1002 * 3. Translation -> bypass (detach)
1003 *
1004 * Given that we can't update the STE atomically and the SMMU
1005 * doesn't read the thing in a defined order, that leaves us
1006 * with the following maintenance requirements:
1007 *
1008 * 1. Update Config, return (init time STEs aren't live)
1009 * 2. Write everything apart from dword 0, sync, write dword 0, sync
1010 * 3. Update Config, sync
1011 */
1012 u64 val = le64_to_cpu(dst[0]);
1013 bool ste_live = false;
1014 struct arm_smmu_cmdq_ent prefetch_cmd = {
1015 .opcode = CMDQ_OP_PREFETCH_CFG,
1016 .prefetch = {
1017 .sid = sid,
1018 },
1019 };
1020
1021 if (val & STRTAB_STE_0_V) {
1022 u64 cfg;
1023
1024 cfg = val & STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT;
1025 switch (cfg) {
1026 case STRTAB_STE_0_CFG_BYPASS:
1027 break;
1028 case STRTAB_STE_0_CFG_S1_TRANS:
1029 case STRTAB_STE_0_CFG_S2_TRANS:
1030 ste_live = true;
1031 break;
1032 default:
1033 BUG(); /* STE corruption */
1034 }
1035 }
1036
1037 /* Nuke the existing Config, as we're going to rewrite it */
1038 val &= ~(STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT);
1039
1040 if (ste->valid)
1041 val |= STRTAB_STE_0_V;
1042 else
1043 val &= ~STRTAB_STE_0_V;
1044
1045 if (ste->bypass) {
1046 val |= disable_bypass ? STRTAB_STE_0_CFG_ABORT
1047 : STRTAB_STE_0_CFG_BYPASS;
1048 dst[0] = cpu_to_le64(val);
Will Deacona0eacd82015-11-18 18:15:51 +00001049 dst[1] = cpu_to_le64(STRTAB_STE_1_SHCFG_INCOMING
1050 << STRTAB_STE_1_SHCFG_SHIFT);
Will Deacon48ec83b2015-05-27 17:25:59 +01001051 dst[2] = 0; /* Nuke the VMID */
1052 if (ste_live)
1053 arm_smmu_sync_ste_for_sid(smmu, sid);
1054 return;
1055 }
1056
1057 if (ste->s1_cfg) {
1058 BUG_ON(ste_live);
1059 dst[1] = cpu_to_le64(
1060 STRTAB_STE_1_S1C_CACHE_WBRA
1061 << STRTAB_STE_1_S1CIR_SHIFT |
1062 STRTAB_STE_1_S1C_CACHE_WBRA
1063 << STRTAB_STE_1_S1COR_SHIFT |
1064 STRTAB_STE_1_S1C_SH_ISH << STRTAB_STE_1_S1CSH_SHIFT |
1065 STRTAB_STE_1_S1STALLD |
1066#ifdef CONFIG_PCI_ATS
1067 STRTAB_STE_1_EATS_TRANS << STRTAB_STE_1_EATS_SHIFT |
1068#endif
1069 STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT);
1070
1071 val |= (ste->s1_cfg->cdptr_dma & STRTAB_STE_0_S1CTXPTR_MASK
1072 << STRTAB_STE_0_S1CTXPTR_SHIFT) |
1073 STRTAB_STE_0_CFG_S1_TRANS;
1074
1075 }
1076
1077 if (ste->s2_cfg) {
1078 BUG_ON(ste_live);
1079 dst[2] = cpu_to_le64(
1080 ste->s2_cfg->vmid << STRTAB_STE_2_S2VMID_SHIFT |
1081 (ste->s2_cfg->vtcr & STRTAB_STE_2_VTCR_MASK)
1082 << STRTAB_STE_2_VTCR_SHIFT |
1083#ifdef __BIG_ENDIAN
1084 STRTAB_STE_2_S2ENDI |
1085#endif
1086 STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 |
1087 STRTAB_STE_2_S2R);
1088
1089 dst[3] = cpu_to_le64(ste->s2_cfg->vttbr &
1090 STRTAB_STE_3_S2TTB_MASK << STRTAB_STE_3_S2TTB_SHIFT);
1091
1092 val |= STRTAB_STE_0_CFG_S2_TRANS;
1093 }
1094
1095 arm_smmu_sync_ste_for_sid(smmu, sid);
1096 dst[0] = cpu_to_le64(val);
1097 arm_smmu_sync_ste_for_sid(smmu, sid);
1098
1099 /* It's likely that we'll want to use the new STE soon */
Zhen Lei5e929462015-07-07 04:30:18 +01001100 if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
1101 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
Will Deacon48ec83b2015-05-27 17:25:59 +01001102}
1103
1104static void arm_smmu_init_bypass_stes(u64 *strtab, unsigned int nent)
1105{
1106 unsigned int i;
1107 struct arm_smmu_strtab_ent ste = {
1108 .valid = true,
1109 .bypass = true,
1110 };
1111
1112 for (i = 0; i < nent; ++i) {
1113 arm_smmu_write_strtab_ent(NULL, -1, strtab, &ste);
1114 strtab += STRTAB_STE_DWORDS;
1115 }
1116}
1117
1118static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1119{
1120 size_t size;
1121 void *strtab;
1122 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1123 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[sid >> STRTAB_SPLIT];
1124
1125 if (desc->l2ptr)
1126 return 0;
1127
1128 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
Zhen Lei69146e72015-06-26 09:32:58 +01001129 strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
Will Deacon48ec83b2015-05-27 17:25:59 +01001130
1131 desc->span = STRTAB_SPLIT + 1;
Will Deacon04fa26c2015-10-30 18:12:41 +00001132 desc->l2ptr = dmam_alloc_coherent(smmu->dev, size, &desc->l2ptr_dma,
1133 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01001134 if (!desc->l2ptr) {
1135 dev_err(smmu->dev,
1136 "failed to allocate l2 stream table for SID %u\n",
1137 sid);
1138 return -ENOMEM;
1139 }
1140
1141 arm_smmu_init_bypass_stes(desc->l2ptr, 1 << STRTAB_SPLIT);
1142 arm_smmu_write_strtab_l1_desc(strtab, desc);
1143 return 0;
1144}
1145
1146/* IRQ and event handlers */
1147static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
1148{
1149 int i;
1150 struct arm_smmu_device *smmu = dev;
1151 struct arm_smmu_queue *q = &smmu->evtq.q;
1152 u64 evt[EVTQ_ENT_DWORDS];
1153
1154 while (!queue_remove_raw(q, evt)) {
1155 u8 id = evt[0] >> EVTQ_0_ID_SHIFT & EVTQ_0_ID_MASK;
1156
1157 dev_info(smmu->dev, "event 0x%02x received:\n", id);
1158 for (i = 0; i < ARRAY_SIZE(evt); ++i)
1159 dev_info(smmu->dev, "\t0x%016llx\n",
1160 (unsigned long long)evt[i]);
1161 }
1162
1163 /* Sync our overflow flag, as we believe we're up to speed */
1164 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1165 return IRQ_HANDLED;
1166}
1167
1168static irqreturn_t arm_smmu_evtq_handler(int irq, void *dev)
1169{
1170 irqreturn_t ret = IRQ_WAKE_THREAD;
1171 struct arm_smmu_device *smmu = dev;
1172 struct arm_smmu_queue *q = &smmu->evtq.q;
1173
1174 /*
1175 * Not much we can do on overflow, so scream and pretend we're
1176 * trying harder.
1177 */
1178 if (queue_sync_prod(q) == -EOVERFLOW)
1179 dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
1180 else if (queue_empty(q))
1181 ret = IRQ_NONE;
1182
1183 return ret;
1184}
1185
1186static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
1187{
1188 struct arm_smmu_device *smmu = dev;
1189 struct arm_smmu_queue *q = &smmu->priq.q;
1190 u64 evt[PRIQ_ENT_DWORDS];
1191
1192 while (!queue_remove_raw(q, evt)) {
1193 u32 sid, ssid;
1194 u16 grpid;
1195 bool ssv, last;
1196
1197 sid = evt[0] >> PRIQ_0_SID_SHIFT & PRIQ_0_SID_MASK;
1198 ssv = evt[0] & PRIQ_0_SSID_V;
1199 ssid = ssv ? evt[0] >> PRIQ_0_SSID_SHIFT & PRIQ_0_SSID_MASK : 0;
1200 last = evt[0] & PRIQ_0_PRG_LAST;
1201 grpid = evt[1] >> PRIQ_1_PRG_IDX_SHIFT & PRIQ_1_PRG_IDX_MASK;
1202
1203 dev_info(smmu->dev, "unexpected PRI request received:\n");
1204 dev_info(smmu->dev,
1205 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
1206 sid, ssid, grpid, last ? "L" : "",
1207 evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
1208 evt[0] & PRIQ_0_PERM_READ ? "R" : "",
1209 evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
1210 evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
1211 evt[1] & PRIQ_1_ADDR_MASK << PRIQ_1_ADDR_SHIFT);
1212
1213 if (last) {
1214 struct arm_smmu_cmdq_ent cmd = {
1215 .opcode = CMDQ_OP_PRI_RESP,
1216 .substream_valid = ssv,
1217 .pri = {
1218 .sid = sid,
1219 .ssid = ssid,
1220 .grpid = grpid,
1221 .resp = PRI_RESP_DENY,
1222 },
1223 };
1224
1225 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1226 }
1227 }
1228
1229 /* Sync our overflow flag, as we believe we're up to speed */
1230 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1231 return IRQ_HANDLED;
1232}
1233
1234static irqreturn_t arm_smmu_priq_handler(int irq, void *dev)
1235{
1236 irqreturn_t ret = IRQ_WAKE_THREAD;
1237 struct arm_smmu_device *smmu = dev;
1238 struct arm_smmu_queue *q = &smmu->priq.q;
1239
1240 /* PRIQ overflow indicates a programming error */
1241 if (queue_sync_prod(q) == -EOVERFLOW)
1242 dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
1243 else if (queue_empty(q))
1244 ret = IRQ_NONE;
1245
1246 return ret;
1247}
1248
1249static irqreturn_t arm_smmu_cmdq_sync_handler(int irq, void *dev)
1250{
1251 /* We don't actually use CMD_SYNC interrupts for anything */
1252 return IRQ_HANDLED;
1253}
1254
1255static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
1256
1257static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
1258{
1259 u32 gerror, gerrorn;
1260 struct arm_smmu_device *smmu = dev;
1261
1262 gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
1263 gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
1264
1265 gerror ^= gerrorn;
1266 if (!(gerror & GERROR_ERR_MASK))
1267 return IRQ_NONE; /* No errors pending */
1268
1269 dev_warn(smmu->dev,
1270 "unexpected global error reported (0x%08x), this could be serious\n",
1271 gerror);
1272
1273 if (gerror & GERROR_SFM_ERR) {
1274 dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
1275 arm_smmu_device_disable(smmu);
1276 }
1277
1278 if (gerror & GERROR_MSI_GERROR_ABT_ERR)
1279 dev_warn(smmu->dev, "GERROR MSI write aborted\n");
1280
1281 if (gerror & GERROR_MSI_PRIQ_ABT_ERR) {
1282 dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
1283 arm_smmu_priq_handler(irq, smmu->dev);
1284 }
1285
1286 if (gerror & GERROR_MSI_EVTQ_ABT_ERR) {
1287 dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
1288 arm_smmu_evtq_handler(irq, smmu->dev);
1289 }
1290
1291 if (gerror & GERROR_MSI_CMDQ_ABT_ERR) {
1292 dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
1293 arm_smmu_cmdq_sync_handler(irq, smmu->dev);
1294 }
1295
1296 if (gerror & GERROR_PRIQ_ABT_ERR)
1297 dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
1298
1299 if (gerror & GERROR_EVTQ_ABT_ERR)
1300 dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
1301
1302 if (gerror & GERROR_CMDQ_ERR)
1303 arm_smmu_cmdq_skip_err(smmu);
1304
1305 writel(gerror, smmu->base + ARM_SMMU_GERRORN);
1306 return IRQ_HANDLED;
1307}
1308
1309/* IO_PGTABLE API */
1310static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
1311{
1312 struct arm_smmu_cmdq_ent cmd;
1313
1314 cmd.opcode = CMDQ_OP_CMD_SYNC;
1315 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1316}
1317
1318static void arm_smmu_tlb_sync(void *cookie)
1319{
1320 struct arm_smmu_domain *smmu_domain = cookie;
1321 __arm_smmu_tlb_sync(smmu_domain->smmu);
1322}
1323
1324static void arm_smmu_tlb_inv_context(void *cookie)
1325{
1326 struct arm_smmu_domain *smmu_domain = cookie;
1327 struct arm_smmu_device *smmu = smmu_domain->smmu;
1328 struct arm_smmu_cmdq_ent cmd;
1329
1330 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1331 cmd.opcode = CMDQ_OP_TLBI_NH_ASID;
1332 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1333 cmd.tlbi.vmid = 0;
1334 } else {
1335 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL;
1336 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1337 }
1338
1339 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1340 __arm_smmu_tlb_sync(smmu);
1341}
1342
1343static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
1344 bool leaf, void *cookie)
1345{
1346 struct arm_smmu_domain *smmu_domain = cookie;
1347 struct arm_smmu_device *smmu = smmu_domain->smmu;
1348 struct arm_smmu_cmdq_ent cmd = {
1349 .tlbi = {
1350 .leaf = leaf,
1351 .addr = iova,
1352 },
1353 };
1354
1355 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1356 cmd.opcode = CMDQ_OP_TLBI_NH_VA;
1357 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1358 } else {
1359 cmd.opcode = CMDQ_OP_TLBI_S2_IPA;
1360 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1361 }
1362
1363 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1364}
1365
Will Deacon48ec83b2015-05-27 17:25:59 +01001366static struct iommu_gather_ops arm_smmu_gather_ops = {
1367 .tlb_flush_all = arm_smmu_tlb_inv_context,
1368 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
1369 .tlb_sync = arm_smmu_tlb_sync,
Will Deacon48ec83b2015-05-27 17:25:59 +01001370};
1371
1372/* IOMMU API */
1373static bool arm_smmu_capable(enum iommu_cap cap)
1374{
1375 switch (cap) {
1376 case IOMMU_CAP_CACHE_COHERENCY:
1377 return true;
1378 case IOMMU_CAP_INTR_REMAP:
1379 return true; /* MSIs are just memory writes */
1380 case IOMMU_CAP_NOEXEC:
1381 return true;
1382 default:
1383 return false;
1384 }
1385}
1386
1387static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
1388{
1389 struct arm_smmu_domain *smmu_domain;
1390
1391 if (type != IOMMU_DOMAIN_UNMANAGED)
1392 return NULL;
1393
1394 /*
1395 * Allocate the domain and initialise some of its data structures.
1396 * We can't really do anything meaningful until we've added a
1397 * master.
1398 */
1399 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1400 if (!smmu_domain)
1401 return NULL;
1402
1403 mutex_init(&smmu_domain->init_mutex);
1404 spin_lock_init(&smmu_domain->pgtbl_lock);
1405 return &smmu_domain->domain;
1406}
1407
1408static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
1409{
1410 int idx, size = 1 << span;
1411
1412 do {
1413 idx = find_first_zero_bit(map, size);
1414 if (idx == size)
1415 return -ENOSPC;
1416 } while (test_and_set_bit(idx, map));
1417
1418 return idx;
1419}
1420
1421static void arm_smmu_bitmap_free(unsigned long *map, int idx)
1422{
1423 clear_bit(idx, map);
1424}
1425
1426static void arm_smmu_domain_free(struct iommu_domain *domain)
1427{
1428 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1429 struct arm_smmu_device *smmu = smmu_domain->smmu;
1430
Markus Elfringa6e08fb2015-06-29 17:47:43 +01001431 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon48ec83b2015-05-27 17:25:59 +01001432
1433 /* Free the CD and ASID, if we allocated them */
1434 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1435 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1436
1437 if (cfg->cdptr) {
Will Deacon04fa26c2015-10-30 18:12:41 +00001438 dmam_free_coherent(smmu_domain->smmu->dev,
1439 CTXDESC_CD_DWORDS << 3,
1440 cfg->cdptr,
1441 cfg->cdptr_dma);
Will Deacon48ec83b2015-05-27 17:25:59 +01001442
1443 arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
1444 }
1445 } else {
1446 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1447 if (cfg->vmid)
1448 arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
1449 }
1450
1451 kfree(smmu_domain);
1452}
1453
1454static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
1455 struct io_pgtable_cfg *pgtbl_cfg)
1456{
1457 int ret;
Will Deaconc0733a22015-10-13 17:51:14 +01001458 int asid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001459 struct arm_smmu_device *smmu = smmu_domain->smmu;
1460 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1461
1462 asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
1463 if (IS_ERR_VALUE(asid))
1464 return asid;
1465
Will Deacon04fa26c2015-10-30 18:12:41 +00001466 cfg->cdptr = dmam_alloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
1467 &cfg->cdptr_dma,
1468 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01001469 if (!cfg->cdptr) {
1470 dev_warn(smmu->dev, "failed to allocate context descriptor\n");
Will Deaconc0733a22015-10-13 17:51:14 +01001471 ret = -ENOMEM;
Will Deacon48ec83b2015-05-27 17:25:59 +01001472 goto out_free_asid;
1473 }
1474
Will Deaconc0733a22015-10-13 17:51:14 +01001475 cfg->cd.asid = (u16)asid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001476 cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
1477 cfg->cd.tcr = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
1478 cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
1479 return 0;
1480
1481out_free_asid:
1482 arm_smmu_bitmap_free(smmu->asid_map, asid);
1483 return ret;
1484}
1485
1486static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
1487 struct io_pgtable_cfg *pgtbl_cfg)
1488{
Will Deaconc0733a22015-10-13 17:51:14 +01001489 int vmid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001490 struct arm_smmu_device *smmu = smmu_domain->smmu;
1491 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1492
1493 vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
1494 if (IS_ERR_VALUE(vmid))
1495 return vmid;
1496
Will Deaconc0733a22015-10-13 17:51:14 +01001497 cfg->vmid = (u16)vmid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001498 cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
1499 cfg->vtcr = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
1500 return 0;
1501}
1502
1503static struct iommu_ops arm_smmu_ops;
1504
1505static int arm_smmu_domain_finalise(struct iommu_domain *domain)
1506{
1507 int ret;
1508 unsigned long ias, oas;
1509 enum io_pgtable_fmt fmt;
1510 struct io_pgtable_cfg pgtbl_cfg;
1511 struct io_pgtable_ops *pgtbl_ops;
1512 int (*finalise_stage_fn)(struct arm_smmu_domain *,
1513 struct io_pgtable_cfg *);
1514 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1515 struct arm_smmu_device *smmu = smmu_domain->smmu;
1516
1517 /* Restrict the stage to what we can actually support */
1518 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
1519 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
1520 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
1521 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1522
1523 switch (smmu_domain->stage) {
1524 case ARM_SMMU_DOMAIN_S1:
1525 ias = VA_BITS;
1526 oas = smmu->ias;
1527 fmt = ARM_64_LPAE_S1;
1528 finalise_stage_fn = arm_smmu_domain_finalise_s1;
1529 break;
1530 case ARM_SMMU_DOMAIN_NESTED:
1531 case ARM_SMMU_DOMAIN_S2:
1532 ias = smmu->ias;
1533 oas = smmu->oas;
1534 fmt = ARM_64_LPAE_S2;
1535 finalise_stage_fn = arm_smmu_domain_finalise_s2;
1536 break;
1537 default:
1538 return -EINVAL;
1539 }
1540
1541 pgtbl_cfg = (struct io_pgtable_cfg) {
1542 .pgsize_bitmap = arm_smmu_ops.pgsize_bitmap,
1543 .ias = ias,
1544 .oas = oas,
1545 .tlb = &arm_smmu_gather_ops,
Robin Murphybdc6d972015-07-29 19:46:07 +01001546 .iommu_dev = smmu->dev,
Will Deacon48ec83b2015-05-27 17:25:59 +01001547 };
1548
1549 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
1550 if (!pgtbl_ops)
1551 return -ENOMEM;
1552
1553 arm_smmu_ops.pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
1554 smmu_domain->pgtbl_ops = pgtbl_ops;
1555
1556 ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
1557 if (IS_ERR_VALUE(ret))
1558 free_io_pgtable_ops(pgtbl_ops);
1559
1560 return ret;
1561}
1562
1563static struct arm_smmu_group *arm_smmu_group_get(struct device *dev)
1564{
1565 struct iommu_group *group;
1566 struct arm_smmu_group *smmu_group;
1567
1568 group = iommu_group_get(dev);
1569 if (!group)
1570 return NULL;
1571
1572 smmu_group = iommu_group_get_iommudata(group);
1573 iommu_group_put(group);
1574 return smmu_group;
1575}
1576
1577static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
1578{
1579 __le64 *step;
1580 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1581
1582 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1583 struct arm_smmu_strtab_l1_desc *l1_desc;
1584 int idx;
1585
1586 /* Two-level walk */
1587 idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
1588 l1_desc = &cfg->l1_desc[idx];
1589 idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
1590 step = &l1_desc->l2ptr[idx];
1591 } else {
1592 /* Simple linear lookup */
1593 step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
1594 }
1595
1596 return step;
1597}
1598
1599static int arm_smmu_install_ste_for_group(struct arm_smmu_group *smmu_group)
1600{
1601 int i;
1602 struct arm_smmu_domain *smmu_domain = smmu_group->domain;
1603 struct arm_smmu_strtab_ent *ste = &smmu_group->ste;
1604 struct arm_smmu_device *smmu = smmu_group->smmu;
1605
1606 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1607 ste->s1_cfg = &smmu_domain->s1_cfg;
1608 ste->s2_cfg = NULL;
1609 arm_smmu_write_ctx_desc(smmu, ste->s1_cfg);
1610 } else {
1611 ste->s1_cfg = NULL;
1612 ste->s2_cfg = &smmu_domain->s2_cfg;
1613 }
1614
1615 for (i = 0; i < smmu_group->num_sids; ++i) {
1616 u32 sid = smmu_group->sids[i];
1617 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
1618
1619 arm_smmu_write_strtab_ent(smmu, sid, step, ste);
1620 }
1621
1622 return 0;
1623}
1624
1625static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1626{
1627 int ret = 0;
1628 struct arm_smmu_device *smmu;
1629 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1630 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1631
1632 if (!smmu_group)
1633 return -ENOENT;
1634
1635 /* Already attached to a different domain? */
1636 if (smmu_group->domain && smmu_group->domain != smmu_domain)
1637 return -EEXIST;
1638
1639 smmu = smmu_group->smmu;
1640 mutex_lock(&smmu_domain->init_mutex);
1641
1642 if (!smmu_domain->smmu) {
1643 smmu_domain->smmu = smmu;
1644 ret = arm_smmu_domain_finalise(domain);
1645 if (ret) {
1646 smmu_domain->smmu = NULL;
1647 goto out_unlock;
1648 }
1649 } else if (smmu_domain->smmu != smmu) {
1650 dev_err(dev,
1651 "cannot attach to SMMU %s (upstream of %s)\n",
1652 dev_name(smmu_domain->smmu->dev),
1653 dev_name(smmu->dev));
1654 ret = -ENXIO;
1655 goto out_unlock;
1656 }
1657
1658 /* Group already attached to this domain? */
1659 if (smmu_group->domain)
1660 goto out_unlock;
1661
1662 smmu_group->domain = smmu_domain;
1663 smmu_group->ste.bypass = false;
1664
1665 ret = arm_smmu_install_ste_for_group(smmu_group);
1666 if (IS_ERR_VALUE(ret))
1667 smmu_group->domain = NULL;
1668
1669out_unlock:
1670 mutex_unlock(&smmu_domain->init_mutex);
1671 return ret;
1672}
1673
1674static void arm_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
1675{
1676 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1677 struct arm_smmu_group *smmu_group = arm_smmu_group_get(dev);
1678
1679 BUG_ON(!smmu_domain);
1680 BUG_ON(!smmu_group);
1681
1682 mutex_lock(&smmu_domain->init_mutex);
1683 BUG_ON(smmu_group->domain != smmu_domain);
1684
1685 smmu_group->ste.bypass = true;
1686 if (IS_ERR_VALUE(arm_smmu_install_ste_for_group(smmu_group)))
1687 dev_warn(dev, "failed to install bypass STE\n");
1688
1689 smmu_group->domain = NULL;
1690 mutex_unlock(&smmu_domain->init_mutex);
1691}
1692
1693static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1694 phys_addr_t paddr, size_t size, int prot)
1695{
1696 int ret;
1697 unsigned long flags;
1698 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1699 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1700
1701 if (!ops)
1702 return -ENODEV;
1703
1704 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1705 ret = ops->map(ops, iova, paddr, size, prot);
1706 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1707 return ret;
1708}
1709
1710static size_t
1711arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1712{
1713 size_t ret;
1714 unsigned long flags;
1715 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1716 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1717
1718 if (!ops)
1719 return 0;
1720
1721 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1722 ret = ops->unmap(ops, iova, size);
1723 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1724 return ret;
1725}
1726
1727static phys_addr_t
1728arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1729{
1730 phys_addr_t ret;
1731 unsigned long flags;
1732 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1733 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1734
1735 if (!ops)
1736 return 0;
1737
1738 spin_lock_irqsave(&smmu_domain->pgtbl_lock, flags);
1739 ret = ops->iova_to_phys(ops, iova);
1740 spin_unlock_irqrestore(&smmu_domain->pgtbl_lock, flags);
1741
1742 return ret;
1743}
1744
1745static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *sidp)
1746{
1747 *(u32 *)sidp = alias;
1748 return 0; /* Continue walking */
1749}
1750
1751static void __arm_smmu_release_pci_iommudata(void *data)
1752{
1753 kfree(data);
1754}
1755
1756static struct arm_smmu_device *arm_smmu_get_for_pci_dev(struct pci_dev *pdev)
1757{
1758 struct device_node *of_node;
Will Deacon941a8022015-08-11 16:25:10 +01001759 struct platform_device *smmu_pdev;
1760 struct arm_smmu_device *smmu = NULL;
Will Deacon48ec83b2015-05-27 17:25:59 +01001761 struct pci_bus *bus = pdev->bus;
1762
1763 /* Walk up to the root bus */
1764 while (!pci_is_root_bus(bus))
1765 bus = bus->parent;
1766
1767 /* Follow the "iommus" phandle from the host controller */
1768 of_node = of_parse_phandle(bus->bridge->parent->of_node, "iommus", 0);
1769 if (!of_node)
1770 return NULL;
1771
1772 /* See if we can find an SMMU corresponding to the phandle */
Will Deacon941a8022015-08-11 16:25:10 +01001773 smmu_pdev = of_find_device_by_node(of_node);
1774 if (smmu_pdev)
1775 smmu = platform_get_drvdata(smmu_pdev);
1776
Will Deacon48ec83b2015-05-27 17:25:59 +01001777 of_node_put(of_node);
1778 return smmu;
1779}
1780
1781static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
1782{
1783 unsigned long limit = smmu->strtab_cfg.num_l1_ents;
1784
1785 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
1786 limit *= 1UL << STRTAB_SPLIT;
1787
1788 return sid < limit;
1789}
1790
1791static int arm_smmu_add_device(struct device *dev)
1792{
1793 int i, ret;
1794 u32 sid, *sids;
1795 struct pci_dev *pdev;
1796 struct iommu_group *group;
1797 struct arm_smmu_group *smmu_group;
1798 struct arm_smmu_device *smmu;
1799
1800 /* We only support PCI, for now */
1801 if (!dev_is_pci(dev))
1802 return -ENODEV;
1803
1804 pdev = to_pci_dev(dev);
1805 group = iommu_group_get_for_dev(dev);
1806 if (IS_ERR(group))
1807 return PTR_ERR(group);
1808
1809 smmu_group = iommu_group_get_iommudata(group);
1810 if (!smmu_group) {
1811 smmu = arm_smmu_get_for_pci_dev(pdev);
1812 if (!smmu) {
1813 ret = -ENOENT;
Peng Fan9a4a9d82015-11-20 16:56:18 +08001814 goto out_remove_dev;
Will Deacon48ec83b2015-05-27 17:25:59 +01001815 }
1816
1817 smmu_group = kzalloc(sizeof(*smmu_group), GFP_KERNEL);
1818 if (!smmu_group) {
1819 ret = -ENOMEM;
Peng Fan9a4a9d82015-11-20 16:56:18 +08001820 goto out_remove_dev;
Will Deacon48ec83b2015-05-27 17:25:59 +01001821 }
1822
1823 smmu_group->ste.valid = true;
1824 smmu_group->smmu = smmu;
1825 iommu_group_set_iommudata(group, smmu_group,
1826 __arm_smmu_release_pci_iommudata);
1827 } else {
1828 smmu = smmu_group->smmu;
1829 }
1830
1831 /* Assume SID == RID until firmware tells us otherwise */
1832 pci_for_each_dma_alias(pdev, __arm_smmu_get_pci_sid, &sid);
1833 for (i = 0; i < smmu_group->num_sids; ++i) {
1834 /* If we already know about this SID, then we're done */
1835 if (smmu_group->sids[i] == sid)
Peng Fan9a4a9d82015-11-20 16:56:18 +08001836 goto out_put_group;
Will Deacon48ec83b2015-05-27 17:25:59 +01001837 }
1838
1839 /* Check the SID is in range of the SMMU and our stream table */
1840 if (!arm_smmu_sid_in_range(smmu, sid)) {
1841 ret = -ERANGE;
Peng Fan9a4a9d82015-11-20 16:56:18 +08001842 goto out_remove_dev;
Will Deacon48ec83b2015-05-27 17:25:59 +01001843 }
1844
1845 /* Ensure l2 strtab is initialised */
1846 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1847 ret = arm_smmu_init_l2_strtab(smmu, sid);
1848 if (ret)
Peng Fan9a4a9d82015-11-20 16:56:18 +08001849 goto out_remove_dev;
Will Deacon48ec83b2015-05-27 17:25:59 +01001850 }
1851
1852 /* Resize the SID array for the group */
1853 smmu_group->num_sids++;
1854 sids = krealloc(smmu_group->sids, smmu_group->num_sids * sizeof(*sids),
1855 GFP_KERNEL);
1856 if (!sids) {
1857 smmu_group->num_sids--;
1858 ret = -ENOMEM;
Peng Fan9a4a9d82015-11-20 16:56:18 +08001859 goto out_remove_dev;
Will Deacon48ec83b2015-05-27 17:25:59 +01001860 }
1861
1862 /* Add the new SID */
1863 sids[smmu_group->num_sids - 1] = sid;
1864 smmu_group->sids = sids;
Will Deacon48ec83b2015-05-27 17:25:59 +01001865
1866out_put_group:
1867 iommu_group_put(group);
Peng Fan9a4a9d82015-11-20 16:56:18 +08001868 return 0;
1869
1870out_remove_dev:
1871 iommu_group_remove_device(dev);
1872 iommu_group_put(group);
Will Deacon48ec83b2015-05-27 17:25:59 +01001873 return ret;
1874}
1875
1876static void arm_smmu_remove_device(struct device *dev)
1877{
1878 iommu_group_remove_device(dev);
1879}
1880
1881static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1882 enum iommu_attr attr, void *data)
1883{
1884 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1885
1886 switch (attr) {
1887 case DOMAIN_ATTR_NESTING:
1888 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1889 return 0;
1890 default:
1891 return -ENODEV;
1892 }
1893}
1894
1895static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1896 enum iommu_attr attr, void *data)
1897{
1898 int ret = 0;
1899 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1900
1901 mutex_lock(&smmu_domain->init_mutex);
1902
1903 switch (attr) {
1904 case DOMAIN_ATTR_NESTING:
1905 if (smmu_domain->smmu) {
1906 ret = -EPERM;
1907 goto out_unlock;
1908 }
1909
1910 if (*(int *)data)
1911 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1912 else
1913 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1914
1915 break;
1916 default:
1917 ret = -ENODEV;
1918 }
1919
1920out_unlock:
1921 mutex_unlock(&smmu_domain->init_mutex);
1922 return ret;
1923}
1924
1925static struct iommu_ops arm_smmu_ops = {
1926 .capable = arm_smmu_capable,
1927 .domain_alloc = arm_smmu_domain_alloc,
1928 .domain_free = arm_smmu_domain_free,
1929 .attach_dev = arm_smmu_attach_dev,
1930 .detach_dev = arm_smmu_detach_dev,
1931 .map = arm_smmu_map,
1932 .unmap = arm_smmu_unmap,
1933 .iova_to_phys = arm_smmu_iova_to_phys,
1934 .add_device = arm_smmu_add_device,
1935 .remove_device = arm_smmu_remove_device,
Joerg Roedelaf659932015-10-21 23:51:41 +02001936 .device_group = pci_device_group,
Will Deacon48ec83b2015-05-27 17:25:59 +01001937 .domain_get_attr = arm_smmu_domain_get_attr,
1938 .domain_set_attr = arm_smmu_domain_set_attr,
1939 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1940};
1941
1942/* Probing and initialisation functions */
1943static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
1944 struct arm_smmu_queue *q,
1945 unsigned long prod_off,
1946 unsigned long cons_off,
1947 size_t dwords)
1948{
1949 size_t qsz = ((1 << q->max_n_shift) * dwords) << 3;
1950
Will Deacon04fa26c2015-10-30 18:12:41 +00001951 q->base = dmam_alloc_coherent(smmu->dev, qsz, &q->base_dma, GFP_KERNEL);
Will Deacon48ec83b2015-05-27 17:25:59 +01001952 if (!q->base) {
1953 dev_err(smmu->dev, "failed to allocate queue (0x%zx bytes)\n",
1954 qsz);
1955 return -ENOMEM;
1956 }
1957
1958 q->prod_reg = smmu->base + prod_off;
1959 q->cons_reg = smmu->base + cons_off;
1960 q->ent_dwords = dwords;
1961
1962 q->q_base = Q_BASE_RWA;
1963 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK << Q_BASE_ADDR_SHIFT;
1964 q->q_base |= (q->max_n_shift & Q_BASE_LOG2SIZE_MASK)
1965 << Q_BASE_LOG2SIZE_SHIFT;
1966
1967 q->prod = q->cons = 0;
1968 return 0;
1969}
1970
Will Deacon48ec83b2015-05-27 17:25:59 +01001971static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
1972{
1973 int ret;
1974
1975 /* cmdq */
1976 spin_lock_init(&smmu->cmdq.lock);
1977 ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
1978 ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS);
1979 if (ret)
Will Deacon04fa26c2015-10-30 18:12:41 +00001980 return ret;
Will Deacon48ec83b2015-05-27 17:25:59 +01001981
1982 /* evtq */
1983 ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
1984 ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS);
1985 if (ret)
Will Deacon04fa26c2015-10-30 18:12:41 +00001986 return ret;
Will Deacon48ec83b2015-05-27 17:25:59 +01001987
1988 /* priq */
1989 if (!(smmu->features & ARM_SMMU_FEAT_PRI))
1990 return 0;
1991
Will Deacon04fa26c2015-10-30 18:12:41 +00001992 return arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
1993 ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS);
Will Deacon48ec83b2015-05-27 17:25:59 +01001994}
1995
1996static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
1997{
1998 unsigned int i;
1999 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2000 size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
2001 void *strtab = smmu->strtab_cfg.strtab;
2002
2003 cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
2004 if (!cfg->l1_desc) {
2005 dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
2006 return -ENOMEM;
2007 }
2008
2009 for (i = 0; i < cfg->num_l1_ents; ++i) {
2010 arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
2011 strtab += STRTAB_L1_DESC_DWORDS << 3;
2012 }
2013
2014 return 0;
2015}
2016
2017static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
2018{
2019 void *strtab;
2020 u64 reg;
Will Deacond2e88e72015-06-30 10:02:28 +01002021 u32 size, l1size;
Will Deacon48ec83b2015-05-27 17:25:59 +01002022 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2023
Will Deacon28c8b402015-07-16 17:50:12 +01002024 /*
2025 * If we can resolve everything with a single L2 table, then we
2026 * just need a single L1 descriptor. Otherwise, calculate the L1
2027 * size, capped to the SIDSIZE.
2028 */
2029 if (smmu->sid_bits < STRTAB_SPLIT) {
2030 size = 0;
2031 } else {
2032 size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
2033 size = min(size, smmu->sid_bits - STRTAB_SPLIT);
2034 }
Will Deacond2e88e72015-06-30 10:02:28 +01002035 cfg->num_l1_ents = 1 << size;
2036
2037 size += STRTAB_SPLIT;
2038 if (size < smmu->sid_bits)
Will Deacon48ec83b2015-05-27 17:25:59 +01002039 dev_warn(smmu->dev,
2040 "2-level strtab only covers %u/%u bits of SID\n",
Will Deacond2e88e72015-06-30 10:02:28 +01002041 size, smmu->sid_bits);
Will Deacon48ec83b2015-05-27 17:25:59 +01002042
Will Deacond2e88e72015-06-30 10:02:28 +01002043 l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
Will Deacon04fa26c2015-10-30 18:12:41 +00002044 strtab = dmam_alloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
2045 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01002046 if (!strtab) {
2047 dev_err(smmu->dev,
2048 "failed to allocate l1 stream table (%u bytes)\n",
2049 size);
2050 return -ENOMEM;
2051 }
2052 cfg->strtab = strtab;
2053
2054 /* Configure strtab_base_cfg for 2 levels */
2055 reg = STRTAB_BASE_CFG_FMT_2LVL;
2056 reg |= (size & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2057 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2058 reg |= (STRTAB_SPLIT & STRTAB_BASE_CFG_SPLIT_MASK)
2059 << STRTAB_BASE_CFG_SPLIT_SHIFT;
2060 cfg->strtab_base_cfg = reg;
2061
Will Deacon04fa26c2015-10-30 18:12:41 +00002062 return arm_smmu_init_l1_strtab(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002063}
2064
2065static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
2066{
2067 void *strtab;
2068 u64 reg;
2069 u32 size;
2070 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2071
2072 size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
Will Deacon04fa26c2015-10-30 18:12:41 +00002073 strtab = dmam_alloc_coherent(smmu->dev, size, &cfg->strtab_dma,
2074 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01002075 if (!strtab) {
2076 dev_err(smmu->dev,
2077 "failed to allocate linear stream table (%u bytes)\n",
2078 size);
2079 return -ENOMEM;
2080 }
2081 cfg->strtab = strtab;
2082 cfg->num_l1_ents = 1 << smmu->sid_bits;
2083
2084 /* Configure strtab_base_cfg for a linear table covering all SIDs */
2085 reg = STRTAB_BASE_CFG_FMT_LINEAR;
2086 reg |= (smmu->sid_bits & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2087 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2088 cfg->strtab_base_cfg = reg;
2089
2090 arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
2091 return 0;
2092}
2093
2094static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
2095{
2096 u64 reg;
2097 int ret;
2098
2099 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
2100 ret = arm_smmu_init_strtab_2lvl(smmu);
2101 else
2102 ret = arm_smmu_init_strtab_linear(smmu);
2103
2104 if (ret)
2105 return ret;
2106
2107 /* Set the strtab base address */
2108 reg = smmu->strtab_cfg.strtab_dma &
2109 STRTAB_BASE_ADDR_MASK << STRTAB_BASE_ADDR_SHIFT;
2110 reg |= STRTAB_BASE_RA;
2111 smmu->strtab_cfg.strtab_base = reg;
2112
2113 /* Allocate the first VMID for stage-2 bypass STEs */
2114 set_bit(0, smmu->vmid_map);
2115 return 0;
2116}
2117
Will Deacon48ec83b2015-05-27 17:25:59 +01002118static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
2119{
2120 int ret;
2121
2122 ret = arm_smmu_init_queues(smmu);
2123 if (ret)
2124 return ret;
2125
Will Deacon04fa26c2015-10-30 18:12:41 +00002126 return arm_smmu_init_strtab(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002127}
2128
2129static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
2130 unsigned int reg_off, unsigned int ack_off)
2131{
2132 u32 reg;
2133
2134 writel_relaxed(val, smmu->base + reg_off);
2135 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
2136 1, ARM_SMMU_POLL_TIMEOUT_US);
2137}
2138
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002139static void arm_smmu_free_msis(void *data)
2140{
2141 struct device *dev = data;
2142 platform_msi_domain_free_irqs(dev);
2143}
2144
2145static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
2146{
2147 phys_addr_t doorbell;
2148 struct device *dev = msi_desc_to_dev(desc);
2149 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2150 phys_addr_t *cfg = arm_smmu_msi_cfg[desc->platform.msi_index];
2151
2152 doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
2153 doorbell &= MSI_CFG0_ADDR_MASK << MSI_CFG0_ADDR_SHIFT;
2154
2155 writeq_relaxed(doorbell, smmu->base + cfg[0]);
2156 writel_relaxed(msg->data, smmu->base + cfg[1]);
2157 writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]);
2158}
2159
2160static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
2161{
2162 struct msi_desc *desc;
2163 int ret, nvec = ARM_SMMU_MAX_MSIS;
2164 struct device *dev = smmu->dev;
2165
2166 /* Clear the MSI address regs */
2167 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
2168 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
2169
2170 if (smmu->features & ARM_SMMU_FEAT_PRI)
2171 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
2172 else
2173 nvec--;
2174
2175 if (!(smmu->features & ARM_SMMU_FEAT_MSI))
2176 return;
2177
2178 /* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */
2179 ret = platform_msi_domain_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg);
2180 if (ret) {
2181 dev_warn(dev, "failed to allocate MSIs\n");
2182 return;
2183 }
2184
2185 for_each_msi_entry(desc, dev) {
2186 switch (desc->platform.msi_index) {
2187 case EVTQ_MSI_INDEX:
2188 smmu->evtq.q.irq = desc->irq;
2189 break;
2190 case GERROR_MSI_INDEX:
2191 smmu->gerr_irq = desc->irq;
2192 break;
2193 case PRIQ_MSI_INDEX:
2194 smmu->priq.q.irq = desc->irq;
2195 break;
2196 default: /* Unknown */
2197 continue;
2198 }
2199 }
2200
2201 /* Add callback to free MSIs on teardown */
2202 devm_add_action(dev, arm_smmu_free_msis, dev);
2203}
2204
Will Deacon48ec83b2015-05-27 17:25:59 +01002205static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
2206{
2207 int ret, irq;
Marc Zyngierccd63852015-07-15 11:55:18 +01002208 u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
Will Deacon48ec83b2015-05-27 17:25:59 +01002209
2210 /* Disable IRQs first */
2211 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
2212 ARM_SMMU_IRQ_CTRLACK);
2213 if (ret) {
2214 dev_err(smmu->dev, "failed to disable irqs\n");
2215 return ret;
2216 }
2217
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002218 arm_smmu_setup_msis(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002219
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002220 /* Request interrupt lines */
Will Deacon48ec83b2015-05-27 17:25:59 +01002221 irq = smmu->evtq.q.irq;
2222 if (irq) {
2223 ret = devm_request_threaded_irq(smmu->dev, irq,
2224 arm_smmu_evtq_handler,
2225 arm_smmu_evtq_thread,
2226 0, "arm-smmu-v3-evtq", smmu);
2227 if (IS_ERR_VALUE(ret))
2228 dev_warn(smmu->dev, "failed to enable evtq irq\n");
2229 }
2230
2231 irq = smmu->cmdq.q.irq;
2232 if (irq) {
2233 ret = devm_request_irq(smmu->dev, irq,
2234 arm_smmu_cmdq_sync_handler, 0,
2235 "arm-smmu-v3-cmdq-sync", smmu);
2236 if (IS_ERR_VALUE(ret))
2237 dev_warn(smmu->dev, "failed to enable cmdq-sync irq\n");
2238 }
2239
2240 irq = smmu->gerr_irq;
2241 if (irq) {
2242 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
2243 0, "arm-smmu-v3-gerror", smmu);
2244 if (IS_ERR_VALUE(ret))
2245 dev_warn(smmu->dev, "failed to enable gerror irq\n");
2246 }
2247
2248 if (smmu->features & ARM_SMMU_FEAT_PRI) {
Will Deacon48ec83b2015-05-27 17:25:59 +01002249 irq = smmu->priq.q.irq;
2250 if (irq) {
2251 ret = devm_request_threaded_irq(smmu->dev, irq,
2252 arm_smmu_priq_handler,
2253 arm_smmu_priq_thread,
2254 0, "arm-smmu-v3-priq",
2255 smmu);
2256 if (IS_ERR_VALUE(ret))
2257 dev_warn(smmu->dev,
2258 "failed to enable priq irq\n");
Marc Zyngierccd63852015-07-15 11:55:18 +01002259 else
2260 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
Will Deacon48ec83b2015-05-27 17:25:59 +01002261 }
2262 }
2263
2264 /* Enable interrupt generation on the SMMU */
Marc Zyngierccd63852015-07-15 11:55:18 +01002265 ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
Will Deacon48ec83b2015-05-27 17:25:59 +01002266 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
2267 if (ret)
2268 dev_warn(smmu->dev, "failed to enable irqs\n");
2269
2270 return 0;
2271}
2272
2273static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
2274{
2275 int ret;
2276
2277 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
2278 if (ret)
2279 dev_err(smmu->dev, "failed to clear cr0\n");
2280
2281 return ret;
2282}
2283
2284static int arm_smmu_device_reset(struct arm_smmu_device *smmu)
2285{
2286 int ret;
2287 u32 reg, enables;
2288 struct arm_smmu_cmdq_ent cmd;
2289
2290 /* Clear CR0 and sync (disables SMMU and queue processing) */
2291 reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
2292 if (reg & CR0_SMMUEN)
2293 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
2294
2295 ret = arm_smmu_device_disable(smmu);
2296 if (ret)
2297 return ret;
2298
2299 /* CR1 (table and queue memory attributes) */
2300 reg = (CR1_SH_ISH << CR1_TABLE_SH_SHIFT) |
2301 (CR1_CACHE_WB << CR1_TABLE_OC_SHIFT) |
2302 (CR1_CACHE_WB << CR1_TABLE_IC_SHIFT) |
2303 (CR1_SH_ISH << CR1_QUEUE_SH_SHIFT) |
2304 (CR1_CACHE_WB << CR1_QUEUE_OC_SHIFT) |
2305 (CR1_CACHE_WB << CR1_QUEUE_IC_SHIFT);
2306 writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
2307
2308 /* CR2 (random crap) */
2309 reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
2310 writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
2311
2312 /* Stream table */
2313 writeq_relaxed(smmu->strtab_cfg.strtab_base,
2314 smmu->base + ARM_SMMU_STRTAB_BASE);
2315 writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
2316 smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
2317
2318 /* Command queue */
2319 writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
2320 writel_relaxed(smmu->cmdq.q.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
2321 writel_relaxed(smmu->cmdq.q.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
2322
2323 enables = CR0_CMDQEN;
2324 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2325 ARM_SMMU_CR0ACK);
2326 if (ret) {
2327 dev_err(smmu->dev, "failed to enable command queue\n");
2328 return ret;
2329 }
2330
2331 /* Invalidate any cached configuration */
2332 cmd.opcode = CMDQ_OP_CFGI_ALL;
2333 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2334 cmd.opcode = CMDQ_OP_CMD_SYNC;
2335 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2336
2337 /* Invalidate any stale TLB entries */
2338 if (smmu->features & ARM_SMMU_FEAT_HYP) {
2339 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
2340 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2341 }
2342
2343 cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
2344 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2345 cmd.opcode = CMDQ_OP_CMD_SYNC;
2346 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2347
2348 /* Event queue */
2349 writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
2350 writel_relaxed(smmu->evtq.q.prod, smmu->base + ARM_SMMU_EVTQ_PROD);
2351 writel_relaxed(smmu->evtq.q.cons, smmu->base + ARM_SMMU_EVTQ_CONS);
2352
2353 enables |= CR0_EVTQEN;
2354 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2355 ARM_SMMU_CR0ACK);
2356 if (ret) {
2357 dev_err(smmu->dev, "failed to enable event queue\n");
2358 return ret;
2359 }
2360
2361 /* PRI queue */
2362 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2363 writeq_relaxed(smmu->priq.q.q_base,
2364 smmu->base + ARM_SMMU_PRIQ_BASE);
2365 writel_relaxed(smmu->priq.q.prod,
2366 smmu->base + ARM_SMMU_PRIQ_PROD);
2367 writel_relaxed(smmu->priq.q.cons,
2368 smmu->base + ARM_SMMU_PRIQ_CONS);
2369
2370 enables |= CR0_PRIQEN;
2371 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2372 ARM_SMMU_CR0ACK);
2373 if (ret) {
2374 dev_err(smmu->dev, "failed to enable PRI queue\n");
2375 return ret;
2376 }
2377 }
2378
2379 ret = arm_smmu_setup_irqs(smmu);
2380 if (ret) {
2381 dev_err(smmu->dev, "failed to setup irqs\n");
2382 return ret;
2383 }
2384
2385 /* Enable the SMMU interface */
2386 enables |= CR0_SMMUEN;
2387 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2388 ARM_SMMU_CR0ACK);
2389 if (ret) {
2390 dev_err(smmu->dev, "failed to enable SMMU interface\n");
2391 return ret;
2392 }
2393
2394 return 0;
2395}
2396
2397static int arm_smmu_device_probe(struct arm_smmu_device *smmu)
2398{
2399 u32 reg;
2400 bool coherent;
2401 unsigned long pgsize_bitmap = 0;
2402
2403 /* IDR0 */
2404 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
2405
2406 /* 2-level structures */
2407 if ((reg & IDR0_ST_LVL_MASK << IDR0_ST_LVL_SHIFT) == IDR0_ST_LVL_2LVL)
2408 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
2409
2410 if (reg & IDR0_CD2L)
2411 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
2412
2413 /*
2414 * Translation table endianness.
2415 * We currently require the same endianness as the CPU, but this
2416 * could be changed later by adding a new IO_PGTABLE_QUIRK.
2417 */
2418 switch (reg & IDR0_TTENDIAN_MASK << IDR0_TTENDIAN_SHIFT) {
2419 case IDR0_TTENDIAN_MIXED:
2420 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
2421 break;
2422#ifdef __BIG_ENDIAN
2423 case IDR0_TTENDIAN_BE:
2424 smmu->features |= ARM_SMMU_FEAT_TT_BE;
2425 break;
2426#else
2427 case IDR0_TTENDIAN_LE:
2428 smmu->features |= ARM_SMMU_FEAT_TT_LE;
2429 break;
2430#endif
2431 default:
2432 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
2433 return -ENXIO;
2434 }
2435
2436 /* Boolean feature flags */
2437 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
2438 smmu->features |= ARM_SMMU_FEAT_PRI;
2439
2440 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
2441 smmu->features |= ARM_SMMU_FEAT_ATS;
2442
2443 if (reg & IDR0_SEV)
2444 smmu->features |= ARM_SMMU_FEAT_SEV;
2445
2446 if (reg & IDR0_MSI)
2447 smmu->features |= ARM_SMMU_FEAT_MSI;
2448
2449 if (reg & IDR0_HYP)
2450 smmu->features |= ARM_SMMU_FEAT_HYP;
2451
2452 /*
2453 * The dma-coherent property is used in preference to the ID
2454 * register, but warn on mismatch.
2455 */
2456 coherent = of_dma_is_coherent(smmu->dev->of_node);
2457 if (coherent)
2458 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2459
2460 if (!!(reg & IDR0_COHACC) != coherent)
2461 dev_warn(smmu->dev, "IDR0.COHACC overridden by dma-coherent property (%s)\n",
2462 coherent ? "true" : "false");
2463
2464 if (reg & IDR0_STALL_MODEL)
2465 smmu->features |= ARM_SMMU_FEAT_STALLS;
2466
2467 if (reg & IDR0_S1P)
2468 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
2469
2470 if (reg & IDR0_S2P)
2471 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
2472
2473 if (!(reg & (IDR0_S1P | IDR0_S2P))) {
2474 dev_err(smmu->dev, "no translation support!\n");
2475 return -ENXIO;
2476 }
2477
2478 /* We only support the AArch64 table format at present */
Will Deaconf0c453d2015-08-20 12:12:32 +01002479 switch (reg & IDR0_TTF_MASK << IDR0_TTF_SHIFT) {
2480 case IDR0_TTF_AARCH32_64:
2481 smmu->ias = 40;
2482 /* Fallthrough */
2483 case IDR0_TTF_AARCH64:
2484 break;
2485 default:
Will Deacon48ec83b2015-05-27 17:25:59 +01002486 dev_err(smmu->dev, "AArch64 table format not supported!\n");
2487 return -ENXIO;
2488 }
2489
2490 /* ASID/VMID sizes */
2491 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
2492 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
2493
2494 /* IDR1 */
2495 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
2496 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
2497 dev_err(smmu->dev, "embedded implementation not supported\n");
2498 return -ENXIO;
2499 }
2500
2501 /* Queue sizes, capped at 4k */
2502 smmu->cmdq.q.max_n_shift = min((u32)CMDQ_MAX_SZ_SHIFT,
2503 reg >> IDR1_CMDQ_SHIFT & IDR1_CMDQ_MASK);
2504 if (!smmu->cmdq.q.max_n_shift) {
2505 /* Odd alignment restrictions on the base, so ignore for now */
2506 dev_err(smmu->dev, "unit-length command queue not supported\n");
2507 return -ENXIO;
2508 }
2509
2510 smmu->evtq.q.max_n_shift = min((u32)EVTQ_MAX_SZ_SHIFT,
2511 reg >> IDR1_EVTQ_SHIFT & IDR1_EVTQ_MASK);
2512 smmu->priq.q.max_n_shift = min((u32)PRIQ_MAX_SZ_SHIFT,
2513 reg >> IDR1_PRIQ_SHIFT & IDR1_PRIQ_MASK);
2514
2515 /* SID/SSID sizes */
2516 smmu->ssid_bits = reg >> IDR1_SSID_SHIFT & IDR1_SSID_MASK;
2517 smmu->sid_bits = reg >> IDR1_SID_SHIFT & IDR1_SID_MASK;
2518
2519 /* IDR5 */
2520 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
2521
2522 /* Maximum number of outstanding stalls */
2523 smmu->evtq.max_stalls = reg >> IDR5_STALL_MAX_SHIFT
2524 & IDR5_STALL_MAX_MASK;
2525
2526 /* Page sizes */
2527 if (reg & IDR5_GRAN64K)
2528 pgsize_bitmap |= SZ_64K | SZ_512M;
2529 if (reg & IDR5_GRAN16K)
2530 pgsize_bitmap |= SZ_16K | SZ_32M;
2531 if (reg & IDR5_GRAN4K)
2532 pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
2533
2534 arm_smmu_ops.pgsize_bitmap &= pgsize_bitmap;
2535
2536 /* Output address size */
2537 switch (reg & IDR5_OAS_MASK << IDR5_OAS_SHIFT) {
2538 case IDR5_OAS_32_BIT:
2539 smmu->oas = 32;
2540 break;
2541 case IDR5_OAS_36_BIT:
2542 smmu->oas = 36;
2543 break;
2544 case IDR5_OAS_40_BIT:
2545 smmu->oas = 40;
2546 break;
2547 case IDR5_OAS_42_BIT:
2548 smmu->oas = 42;
2549 break;
2550 case IDR5_OAS_44_BIT:
2551 smmu->oas = 44;
2552 break;
Will Deacon85430962015-08-03 10:35:40 +01002553 default:
2554 dev_info(smmu->dev,
2555 "unknown output address size. Truncating to 48-bit\n");
2556 /* Fallthrough */
Will Deacon48ec83b2015-05-27 17:25:59 +01002557 case IDR5_OAS_48_BIT:
2558 smmu->oas = 48;
Will Deacon48ec83b2015-05-27 17:25:59 +01002559 }
2560
2561 /* Set the DMA mask for our table walker */
2562 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
2563 dev_warn(smmu->dev,
2564 "failed to set DMA mask for table walker\n");
2565
Will Deaconf0c453d2015-08-20 12:12:32 +01002566 smmu->ias = max(smmu->ias, smmu->oas);
Will Deacon48ec83b2015-05-27 17:25:59 +01002567
2568 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
2569 smmu->ias, smmu->oas, smmu->features);
2570 return 0;
2571}
2572
2573static int arm_smmu_device_dt_probe(struct platform_device *pdev)
2574{
2575 int irq, ret;
2576 struct resource *res;
2577 struct arm_smmu_device *smmu;
2578 struct device *dev = &pdev->dev;
2579
2580 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2581 if (!smmu) {
2582 dev_err(dev, "failed to allocate arm_smmu_device\n");
2583 return -ENOMEM;
2584 }
2585 smmu->dev = dev;
2586
2587 /* Base address */
2588 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2589 if (resource_size(res) + 1 < SZ_128K) {
2590 dev_err(dev, "MMIO region too small (%pr)\n", res);
2591 return -EINVAL;
2592 }
2593
2594 smmu->base = devm_ioremap_resource(dev, res);
2595 if (IS_ERR(smmu->base))
2596 return PTR_ERR(smmu->base);
2597
2598 /* Interrupt lines */
2599 irq = platform_get_irq_byname(pdev, "eventq");
2600 if (irq > 0)
2601 smmu->evtq.q.irq = irq;
2602
2603 irq = platform_get_irq_byname(pdev, "priq");
2604 if (irq > 0)
2605 smmu->priq.q.irq = irq;
2606
2607 irq = platform_get_irq_byname(pdev, "cmdq-sync");
2608 if (irq > 0)
2609 smmu->cmdq.q.irq = irq;
2610
2611 irq = platform_get_irq_byname(pdev, "gerror");
2612 if (irq > 0)
2613 smmu->gerr_irq = irq;
2614
Zhen Lei5e929462015-07-07 04:30:18 +01002615 parse_driver_options(smmu);
2616
Will Deacon48ec83b2015-05-27 17:25:59 +01002617 /* Probe the h/w */
2618 ret = arm_smmu_device_probe(smmu);
2619 if (ret)
2620 return ret;
2621
2622 /* Initialise in-memory data structures */
2623 ret = arm_smmu_init_structures(smmu);
2624 if (ret)
2625 return ret;
2626
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002627 /* Record our private device structure */
2628 platform_set_drvdata(pdev, smmu);
2629
Will Deacon48ec83b2015-05-27 17:25:59 +01002630 /* Reset the device */
Will Deacon04fa26c2015-10-30 18:12:41 +00002631 return arm_smmu_device_reset(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002632}
2633
2634static int arm_smmu_device_remove(struct platform_device *pdev)
2635{
Will Deacon941a8022015-08-11 16:25:10 +01002636 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
Will Deacon48ec83b2015-05-27 17:25:59 +01002637
2638 arm_smmu_device_disable(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002639 return 0;
2640}
2641
2642static struct of_device_id arm_smmu_of_match[] = {
2643 { .compatible = "arm,smmu-v3", },
2644 { },
2645};
2646MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2647
2648static struct platform_driver arm_smmu_driver = {
2649 .driver = {
2650 .name = "arm-smmu-v3",
2651 .of_match_table = of_match_ptr(arm_smmu_of_match),
2652 },
2653 .probe = arm_smmu_device_dt_probe,
2654 .remove = arm_smmu_device_remove,
2655};
2656
2657static int __init arm_smmu_init(void)
2658{
2659 struct device_node *np;
2660 int ret;
2661
2662 np = of_find_matching_node(NULL, arm_smmu_of_match);
2663 if (!np)
2664 return 0;
2665
2666 of_node_put(np);
2667
2668 ret = platform_driver_register(&arm_smmu_driver);
2669 if (ret)
2670 return ret;
2671
2672 return bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2673}
2674
2675static void __exit arm_smmu_exit(void)
2676{
2677 return platform_driver_unregister(&arm_smmu_driver);
2678}
2679
2680subsys_initcall(arm_smmu_init);
2681module_exit(arm_smmu_exit);
2682
2683MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
2684MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2685MODULE_LICENSE("GPL v2");