blob: ceb8f9ef4badf537cb7c30634c7e25a4c6f30ccf [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
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +000023#include <linux/acpi.h>
24#include <linux/acpi_iort.h>
Will Deacon48ec83b2015-05-27 17:25:59 +010025#include <linux/delay.h>
Robin Murphy9adb9592016-01-26 18:06:36 +000026#include <linux/dma-iommu.h>
Will Deacon48ec83b2015-05-27 17:25:59 +010027#include <linux/err.h>
28#include <linux/interrupt.h>
29#include <linux/iommu.h>
30#include <linux/iopoll.h>
31#include <linux/module.h>
Marc Zyngier166bdbd2015-10-13 18:32:30 +010032#include <linux/msi.h>
Will Deacon48ec83b2015-05-27 17:25:59 +010033#include <linux/of.h>
34#include <linux/of_address.h>
Robin Murphy8f785152016-09-12 17:13:45 +010035#include <linux/of_iommu.h>
Will Deacon941a8022015-08-11 16:25:10 +010036#include <linux/of_platform.h>
Will Deacon48ec83b2015-05-27 17:25:59 +010037#include <linux/pci.h>
38#include <linux/platform_device.h>
39
Robin Murphy08d4ca22016-09-12 17:13:46 +010040#include <linux/amba/bus.h>
41
Will Deacon48ec83b2015-05-27 17:25:59 +010042#include "io-pgtable.h"
43
44/* MMIO registers */
45#define ARM_SMMU_IDR0 0x0
46#define IDR0_ST_LVL_SHIFT 27
47#define IDR0_ST_LVL_MASK 0x3
48#define IDR0_ST_LVL_2LVL (1 << IDR0_ST_LVL_SHIFT)
Prem Mallappa6380be02015-12-14 22:01:23 +053049#define IDR0_STALL_MODEL_SHIFT 24
50#define IDR0_STALL_MODEL_MASK 0x3
51#define IDR0_STALL_MODEL_STALL (0 << IDR0_STALL_MODEL_SHIFT)
52#define IDR0_STALL_MODEL_FORCE (2 << IDR0_STALL_MODEL_SHIFT)
Will Deacon48ec83b2015-05-27 17:25:59 +010053#define IDR0_TTENDIAN_SHIFT 21
54#define IDR0_TTENDIAN_MASK 0x3
55#define IDR0_TTENDIAN_LE (2 << IDR0_TTENDIAN_SHIFT)
56#define IDR0_TTENDIAN_BE (3 << IDR0_TTENDIAN_SHIFT)
57#define IDR0_TTENDIAN_MIXED (0 << IDR0_TTENDIAN_SHIFT)
58#define IDR0_CD2L (1 << 19)
59#define IDR0_VMID16 (1 << 18)
60#define IDR0_PRI (1 << 16)
61#define IDR0_SEV (1 << 14)
62#define IDR0_MSI (1 << 13)
63#define IDR0_ASID16 (1 << 12)
64#define IDR0_ATS (1 << 10)
65#define IDR0_HYP (1 << 9)
66#define IDR0_COHACC (1 << 4)
67#define IDR0_TTF_SHIFT 2
68#define IDR0_TTF_MASK 0x3
69#define IDR0_TTF_AARCH64 (2 << IDR0_TTF_SHIFT)
Will Deaconf0c453d2015-08-20 12:12:32 +010070#define IDR0_TTF_AARCH32_64 (3 << IDR0_TTF_SHIFT)
Will Deacon48ec83b2015-05-27 17:25:59 +010071#define IDR0_S1P (1 << 1)
72#define IDR0_S2P (1 << 0)
73
74#define ARM_SMMU_IDR1 0x4
75#define IDR1_TABLES_PRESET (1 << 30)
76#define IDR1_QUEUES_PRESET (1 << 29)
77#define IDR1_REL (1 << 28)
78#define IDR1_CMDQ_SHIFT 21
79#define IDR1_CMDQ_MASK 0x1f
80#define IDR1_EVTQ_SHIFT 16
81#define IDR1_EVTQ_MASK 0x1f
82#define IDR1_PRIQ_SHIFT 11
83#define IDR1_PRIQ_MASK 0x1f
84#define IDR1_SSID_SHIFT 6
85#define IDR1_SSID_MASK 0x1f
86#define IDR1_SID_SHIFT 0
87#define IDR1_SID_MASK 0x3f
88
89#define ARM_SMMU_IDR5 0x14
90#define IDR5_STALL_MAX_SHIFT 16
91#define IDR5_STALL_MAX_MASK 0xffff
92#define IDR5_GRAN64K (1 << 6)
93#define IDR5_GRAN16K (1 << 5)
94#define IDR5_GRAN4K (1 << 4)
95#define IDR5_OAS_SHIFT 0
96#define IDR5_OAS_MASK 0x7
97#define IDR5_OAS_32_BIT (0 << IDR5_OAS_SHIFT)
98#define IDR5_OAS_36_BIT (1 << IDR5_OAS_SHIFT)
99#define IDR5_OAS_40_BIT (2 << IDR5_OAS_SHIFT)
100#define IDR5_OAS_42_BIT (3 << IDR5_OAS_SHIFT)
101#define IDR5_OAS_44_BIT (4 << IDR5_OAS_SHIFT)
102#define IDR5_OAS_48_BIT (5 << IDR5_OAS_SHIFT)
103
104#define ARM_SMMU_CR0 0x20
105#define CR0_CMDQEN (1 << 3)
106#define CR0_EVTQEN (1 << 2)
107#define CR0_PRIQEN (1 << 1)
108#define CR0_SMMUEN (1 << 0)
109
110#define ARM_SMMU_CR0ACK 0x24
111
112#define ARM_SMMU_CR1 0x28
113#define CR1_SH_NSH 0
114#define CR1_SH_OSH 2
115#define CR1_SH_ISH 3
116#define CR1_CACHE_NC 0
117#define CR1_CACHE_WB 1
118#define CR1_CACHE_WT 2
119#define CR1_TABLE_SH_SHIFT 10
120#define CR1_TABLE_OC_SHIFT 8
121#define CR1_TABLE_IC_SHIFT 6
122#define CR1_QUEUE_SH_SHIFT 4
123#define CR1_QUEUE_OC_SHIFT 2
124#define CR1_QUEUE_IC_SHIFT 0
125
126#define ARM_SMMU_CR2 0x2c
127#define CR2_PTM (1 << 2)
128#define CR2_RECINVSID (1 << 1)
129#define CR2_E2H (1 << 0)
130
Robin Murphydc87a982016-09-12 17:13:44 +0100131#define ARM_SMMU_GBPA 0x44
132#define GBPA_ABORT (1 << 20)
133#define GBPA_UPDATE (1 << 31)
134
Will Deacon48ec83b2015-05-27 17:25:59 +0100135#define ARM_SMMU_IRQ_CTRL 0x50
136#define IRQ_CTRL_EVTQ_IRQEN (1 << 2)
Marc Zyngierccd63852015-07-15 11:55:18 +0100137#define IRQ_CTRL_PRIQ_IRQEN (1 << 1)
Will Deacon48ec83b2015-05-27 17:25:59 +0100138#define IRQ_CTRL_GERROR_IRQEN (1 << 0)
139
140#define ARM_SMMU_IRQ_CTRLACK 0x54
141
142#define ARM_SMMU_GERROR 0x60
143#define GERROR_SFM_ERR (1 << 8)
144#define GERROR_MSI_GERROR_ABT_ERR (1 << 7)
145#define GERROR_MSI_PRIQ_ABT_ERR (1 << 6)
146#define GERROR_MSI_EVTQ_ABT_ERR (1 << 5)
147#define GERROR_MSI_CMDQ_ABT_ERR (1 << 4)
148#define GERROR_PRIQ_ABT_ERR (1 << 3)
149#define GERROR_EVTQ_ABT_ERR (1 << 2)
150#define GERROR_CMDQ_ERR (1 << 0)
151#define GERROR_ERR_MASK 0xfd
152
153#define ARM_SMMU_GERRORN 0x64
154
155#define ARM_SMMU_GERROR_IRQ_CFG0 0x68
156#define ARM_SMMU_GERROR_IRQ_CFG1 0x70
157#define ARM_SMMU_GERROR_IRQ_CFG2 0x74
158
159#define ARM_SMMU_STRTAB_BASE 0x80
160#define STRTAB_BASE_RA (1UL << 62)
161#define STRTAB_BASE_ADDR_SHIFT 6
162#define STRTAB_BASE_ADDR_MASK 0x3ffffffffffUL
163
164#define ARM_SMMU_STRTAB_BASE_CFG 0x88
165#define STRTAB_BASE_CFG_LOG2SIZE_SHIFT 0
166#define STRTAB_BASE_CFG_LOG2SIZE_MASK 0x3f
167#define STRTAB_BASE_CFG_SPLIT_SHIFT 6
168#define STRTAB_BASE_CFG_SPLIT_MASK 0x1f
169#define STRTAB_BASE_CFG_FMT_SHIFT 16
170#define STRTAB_BASE_CFG_FMT_MASK 0x3
171#define STRTAB_BASE_CFG_FMT_LINEAR (0 << STRTAB_BASE_CFG_FMT_SHIFT)
172#define STRTAB_BASE_CFG_FMT_2LVL (1 << STRTAB_BASE_CFG_FMT_SHIFT)
173
174#define ARM_SMMU_CMDQ_BASE 0x90
175#define ARM_SMMU_CMDQ_PROD 0x98
176#define ARM_SMMU_CMDQ_CONS 0x9c
177
178#define ARM_SMMU_EVTQ_BASE 0xa0
179#define ARM_SMMU_EVTQ_PROD 0x100a8
180#define ARM_SMMU_EVTQ_CONS 0x100ac
181#define ARM_SMMU_EVTQ_IRQ_CFG0 0xb0
182#define ARM_SMMU_EVTQ_IRQ_CFG1 0xb8
183#define ARM_SMMU_EVTQ_IRQ_CFG2 0xbc
184
185#define ARM_SMMU_PRIQ_BASE 0xc0
186#define ARM_SMMU_PRIQ_PROD 0x100c8
187#define ARM_SMMU_PRIQ_CONS 0x100cc
188#define ARM_SMMU_PRIQ_IRQ_CFG0 0xd0
189#define ARM_SMMU_PRIQ_IRQ_CFG1 0xd8
190#define ARM_SMMU_PRIQ_IRQ_CFG2 0xdc
191
192/* Common MSI config fields */
Will Deacon48ec83b2015-05-27 17:25:59 +0100193#define MSI_CFG0_ADDR_SHIFT 2
194#define MSI_CFG0_ADDR_MASK 0x3fffffffffffUL
Marc Zyngierec11d632015-07-15 11:55:19 +0100195#define MSI_CFG2_SH_SHIFT 4
196#define MSI_CFG2_SH_NSH (0UL << MSI_CFG2_SH_SHIFT)
197#define MSI_CFG2_SH_OSH (2UL << MSI_CFG2_SH_SHIFT)
198#define MSI_CFG2_SH_ISH (3UL << MSI_CFG2_SH_SHIFT)
199#define MSI_CFG2_MEMATTR_SHIFT 0
200#define MSI_CFG2_MEMATTR_DEVICE_nGnRE (0x1 << MSI_CFG2_MEMATTR_SHIFT)
Will Deacon48ec83b2015-05-27 17:25:59 +0100201
202#define Q_IDX(q, p) ((p) & ((1 << (q)->max_n_shift) - 1))
203#define Q_WRP(q, p) ((p) & (1 << (q)->max_n_shift))
204#define Q_OVERFLOW_FLAG (1 << 31)
205#define Q_OVF(q, p) ((p) & Q_OVERFLOW_FLAG)
206#define Q_ENT(q, p) ((q)->base + \
207 Q_IDX(q, p) * (q)->ent_dwords)
208
209#define Q_BASE_RWA (1UL << 62)
210#define Q_BASE_ADDR_SHIFT 5
211#define Q_BASE_ADDR_MASK 0xfffffffffffUL
212#define Q_BASE_LOG2SIZE_SHIFT 0
213#define Q_BASE_LOG2SIZE_MASK 0x1fUL
214
215/*
216 * Stream table.
217 *
218 * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
Zhen Leie2f4c232015-07-07 04:30:17 +0100219 * 2lvl: 128k L1 entries,
220 * 256 lazy entries per table (each table covers a PCI bus)
Will Deacon48ec83b2015-05-27 17:25:59 +0100221 */
Zhen Leie2f4c232015-07-07 04:30:17 +0100222#define STRTAB_L1_SZ_SHIFT 20
Will Deacon48ec83b2015-05-27 17:25:59 +0100223#define STRTAB_SPLIT 8
224
225#define STRTAB_L1_DESC_DWORDS 1
226#define STRTAB_L1_DESC_SPAN_SHIFT 0
227#define STRTAB_L1_DESC_SPAN_MASK 0x1fUL
228#define STRTAB_L1_DESC_L2PTR_SHIFT 6
229#define STRTAB_L1_DESC_L2PTR_MASK 0x3ffffffffffUL
230
231#define STRTAB_STE_DWORDS 8
232#define STRTAB_STE_0_V (1UL << 0)
233#define STRTAB_STE_0_CFG_SHIFT 1
234#define STRTAB_STE_0_CFG_MASK 0x7UL
235#define STRTAB_STE_0_CFG_ABORT (0UL << STRTAB_STE_0_CFG_SHIFT)
236#define STRTAB_STE_0_CFG_BYPASS (4UL << STRTAB_STE_0_CFG_SHIFT)
237#define STRTAB_STE_0_CFG_S1_TRANS (5UL << STRTAB_STE_0_CFG_SHIFT)
238#define STRTAB_STE_0_CFG_S2_TRANS (6UL << STRTAB_STE_0_CFG_SHIFT)
239
240#define STRTAB_STE_0_S1FMT_SHIFT 4
241#define STRTAB_STE_0_S1FMT_LINEAR (0UL << STRTAB_STE_0_S1FMT_SHIFT)
242#define STRTAB_STE_0_S1CTXPTR_SHIFT 6
243#define STRTAB_STE_0_S1CTXPTR_MASK 0x3ffffffffffUL
244#define STRTAB_STE_0_S1CDMAX_SHIFT 59
245#define STRTAB_STE_0_S1CDMAX_MASK 0x1fUL
246
247#define STRTAB_STE_1_S1C_CACHE_NC 0UL
248#define STRTAB_STE_1_S1C_CACHE_WBRA 1UL
249#define STRTAB_STE_1_S1C_CACHE_WT 2UL
250#define STRTAB_STE_1_S1C_CACHE_WB 3UL
251#define STRTAB_STE_1_S1C_SH_NSH 0UL
252#define STRTAB_STE_1_S1C_SH_OSH 2UL
253#define STRTAB_STE_1_S1C_SH_ISH 3UL
254#define STRTAB_STE_1_S1CIR_SHIFT 2
255#define STRTAB_STE_1_S1COR_SHIFT 4
256#define STRTAB_STE_1_S1CSH_SHIFT 6
257
258#define STRTAB_STE_1_S1STALLD (1UL << 27)
259
260#define STRTAB_STE_1_EATS_ABT 0UL
261#define STRTAB_STE_1_EATS_TRANS 1UL
262#define STRTAB_STE_1_EATS_S1CHK 2UL
263#define STRTAB_STE_1_EATS_SHIFT 28
264
265#define STRTAB_STE_1_STRW_NSEL1 0UL
266#define STRTAB_STE_1_STRW_EL2 2UL
267#define STRTAB_STE_1_STRW_SHIFT 30
268
Will Deacona0eacd82015-11-18 18:15:51 +0000269#define STRTAB_STE_1_SHCFG_INCOMING 1UL
270#define STRTAB_STE_1_SHCFG_SHIFT 44
271
Will Deacon48ec83b2015-05-27 17:25:59 +0100272#define STRTAB_STE_2_S2VMID_SHIFT 0
273#define STRTAB_STE_2_S2VMID_MASK 0xffffUL
274#define STRTAB_STE_2_VTCR_SHIFT 32
275#define STRTAB_STE_2_VTCR_MASK 0x7ffffUL
276#define STRTAB_STE_2_S2AA64 (1UL << 51)
277#define STRTAB_STE_2_S2ENDI (1UL << 52)
278#define STRTAB_STE_2_S2PTW (1UL << 54)
279#define STRTAB_STE_2_S2R (1UL << 58)
280
281#define STRTAB_STE_3_S2TTB_SHIFT 4
282#define STRTAB_STE_3_S2TTB_MASK 0xfffffffffffUL
283
284/* Context descriptor (stage-1 only) */
285#define CTXDESC_CD_DWORDS 8
286#define CTXDESC_CD_0_TCR_T0SZ_SHIFT 0
287#define ARM64_TCR_T0SZ_SHIFT 0
288#define ARM64_TCR_T0SZ_MASK 0x1fUL
289#define CTXDESC_CD_0_TCR_TG0_SHIFT 6
290#define ARM64_TCR_TG0_SHIFT 14
291#define ARM64_TCR_TG0_MASK 0x3UL
292#define CTXDESC_CD_0_TCR_IRGN0_SHIFT 8
Zhen Lei5d58c622015-06-26 09:32:59 +0100293#define ARM64_TCR_IRGN0_SHIFT 8
Will Deacon48ec83b2015-05-27 17:25:59 +0100294#define ARM64_TCR_IRGN0_MASK 0x3UL
295#define CTXDESC_CD_0_TCR_ORGN0_SHIFT 10
Zhen Lei5d58c622015-06-26 09:32:59 +0100296#define ARM64_TCR_ORGN0_SHIFT 10
Will Deacon48ec83b2015-05-27 17:25:59 +0100297#define ARM64_TCR_ORGN0_MASK 0x3UL
298#define CTXDESC_CD_0_TCR_SH0_SHIFT 12
299#define ARM64_TCR_SH0_SHIFT 12
300#define ARM64_TCR_SH0_MASK 0x3UL
301#define CTXDESC_CD_0_TCR_EPD0_SHIFT 14
302#define ARM64_TCR_EPD0_SHIFT 7
303#define ARM64_TCR_EPD0_MASK 0x1UL
304#define CTXDESC_CD_0_TCR_EPD1_SHIFT 30
305#define ARM64_TCR_EPD1_SHIFT 23
306#define ARM64_TCR_EPD1_MASK 0x1UL
307
308#define CTXDESC_CD_0_ENDI (1UL << 15)
309#define CTXDESC_CD_0_V (1UL << 31)
310
311#define CTXDESC_CD_0_TCR_IPS_SHIFT 32
312#define ARM64_TCR_IPS_SHIFT 32
313#define ARM64_TCR_IPS_MASK 0x7UL
314#define CTXDESC_CD_0_TCR_TBI0_SHIFT 38
315#define ARM64_TCR_TBI0_SHIFT 37
316#define ARM64_TCR_TBI0_MASK 0x1UL
317
318#define CTXDESC_CD_0_AA64 (1UL << 41)
Yisheng Xie9cff86fd22017-09-21 20:36:07 +0800319#define CTXDESC_CD_0_S (1UL << 44)
Will Deacon48ec83b2015-05-27 17:25:59 +0100320#define CTXDESC_CD_0_R (1UL << 45)
321#define CTXDESC_CD_0_A (1UL << 46)
322#define CTXDESC_CD_0_ASET_SHIFT 47
323#define CTXDESC_CD_0_ASET_SHARED (0UL << CTXDESC_CD_0_ASET_SHIFT)
324#define CTXDESC_CD_0_ASET_PRIVATE (1UL << CTXDESC_CD_0_ASET_SHIFT)
325#define CTXDESC_CD_0_ASID_SHIFT 48
326#define CTXDESC_CD_0_ASID_MASK 0xffffUL
327
328#define CTXDESC_CD_1_TTB0_SHIFT 4
329#define CTXDESC_CD_1_TTB0_MASK 0xfffffffffffUL
330
331#define CTXDESC_CD_3_MAIR_SHIFT 0
332
333/* Convert between AArch64 (CPU) TCR format and SMMU CD format */
334#define ARM_SMMU_TCR2CD(tcr, fld) \
335 (((tcr) >> ARM64_TCR_##fld##_SHIFT & ARM64_TCR_##fld##_MASK) \
336 << CTXDESC_CD_0_TCR_##fld##_SHIFT)
337
338/* Command queue */
339#define CMDQ_ENT_DWORDS 2
340#define CMDQ_MAX_SZ_SHIFT 8
341
342#define CMDQ_ERR_SHIFT 24
343#define CMDQ_ERR_MASK 0x7f
344#define CMDQ_ERR_CERROR_NONE_IDX 0
345#define CMDQ_ERR_CERROR_ILL_IDX 1
346#define CMDQ_ERR_CERROR_ABT_IDX 2
347
348#define CMDQ_0_OP_SHIFT 0
349#define CMDQ_0_OP_MASK 0xffUL
350#define CMDQ_0_SSV (1UL << 11)
351
352#define CMDQ_PREFETCH_0_SID_SHIFT 32
353#define CMDQ_PREFETCH_1_SIZE_SHIFT 0
354#define CMDQ_PREFETCH_1_ADDR_MASK ~0xfffUL
355
356#define CMDQ_CFGI_0_SID_SHIFT 32
357#define CMDQ_CFGI_0_SID_MASK 0xffffffffUL
358#define CMDQ_CFGI_1_LEAF (1UL << 0)
359#define CMDQ_CFGI_1_RANGE_SHIFT 0
360#define CMDQ_CFGI_1_RANGE_MASK 0x1fUL
361
362#define CMDQ_TLBI_0_VMID_SHIFT 32
363#define CMDQ_TLBI_0_ASID_SHIFT 48
364#define CMDQ_TLBI_1_LEAF (1UL << 0)
Will Deacon1c27df12015-09-18 16:12:56 +0100365#define CMDQ_TLBI_1_VA_MASK ~0xfffUL
366#define CMDQ_TLBI_1_IPA_MASK 0xfffffffff000UL
Will Deacon48ec83b2015-05-27 17:25:59 +0100367
368#define CMDQ_PRI_0_SSID_SHIFT 12
369#define CMDQ_PRI_0_SSID_MASK 0xfffffUL
370#define CMDQ_PRI_0_SID_SHIFT 32
371#define CMDQ_PRI_0_SID_MASK 0xffffffffUL
372#define CMDQ_PRI_1_GRPID_SHIFT 0
373#define CMDQ_PRI_1_GRPID_MASK 0x1ffUL
374#define CMDQ_PRI_1_RESP_SHIFT 12
375#define CMDQ_PRI_1_RESP_DENY (0UL << CMDQ_PRI_1_RESP_SHIFT)
376#define CMDQ_PRI_1_RESP_FAIL (1UL << CMDQ_PRI_1_RESP_SHIFT)
377#define CMDQ_PRI_1_RESP_SUCC (2UL << CMDQ_PRI_1_RESP_SHIFT)
378
379#define CMDQ_SYNC_0_CS_SHIFT 12
380#define CMDQ_SYNC_0_CS_NONE (0UL << CMDQ_SYNC_0_CS_SHIFT)
Robin Murphy37de98f2017-10-18 15:04:26 +0100381#define CMDQ_SYNC_0_CS_IRQ (1UL << CMDQ_SYNC_0_CS_SHIFT)
Will Deacon48ec83b2015-05-27 17:25:59 +0100382#define CMDQ_SYNC_0_CS_SEV (2UL << CMDQ_SYNC_0_CS_SHIFT)
Robin Murphy37de98f2017-10-18 15:04:26 +0100383#define CMDQ_SYNC_0_MSH_SHIFT 22
384#define CMDQ_SYNC_0_MSH_ISH (3UL << CMDQ_SYNC_0_MSH_SHIFT)
385#define CMDQ_SYNC_0_MSIATTR_SHIFT 24
386#define CMDQ_SYNC_0_MSIATTR_OIWB (0xfUL << CMDQ_SYNC_0_MSIATTR_SHIFT)
387#define CMDQ_SYNC_0_MSIDATA_SHIFT 32
388#define CMDQ_SYNC_0_MSIDATA_MASK 0xffffffffUL
389#define CMDQ_SYNC_1_MSIADDR_SHIFT 0
390#define CMDQ_SYNC_1_MSIADDR_MASK 0xffffffffffffcUL
Will Deacon48ec83b2015-05-27 17:25:59 +0100391
392/* Event queue */
393#define EVTQ_ENT_DWORDS 4
394#define EVTQ_MAX_SZ_SHIFT 7
395
396#define EVTQ_0_ID_SHIFT 0
397#define EVTQ_0_ID_MASK 0xffUL
398
399/* PRI queue */
400#define PRIQ_ENT_DWORDS 2
401#define PRIQ_MAX_SZ_SHIFT 8
402
403#define PRIQ_0_SID_SHIFT 0
404#define PRIQ_0_SID_MASK 0xffffffffUL
405#define PRIQ_0_SSID_SHIFT 32
406#define PRIQ_0_SSID_MASK 0xfffffUL
Will Deacon48ec83b2015-05-27 17:25:59 +0100407#define PRIQ_0_PERM_PRIV (1UL << 58)
408#define PRIQ_0_PERM_EXEC (1UL << 59)
409#define PRIQ_0_PERM_READ (1UL << 60)
410#define PRIQ_0_PERM_WRITE (1UL << 61)
411#define PRIQ_0_PRG_LAST (1UL << 62)
412#define PRIQ_0_SSID_V (1UL << 63)
413
414#define PRIQ_1_PRG_IDX_SHIFT 0
415#define PRIQ_1_PRG_IDX_MASK 0x1ffUL
416#define PRIQ_1_ADDR_SHIFT 12
417#define PRIQ_1_ADDR_MASK 0xfffffffffffffUL
418
419/* High-level queue structures */
420#define ARM_SMMU_POLL_TIMEOUT_US 100
Sunil Gouthamb847de42017-05-05 16:47:46 +0530421#define ARM_SMMU_CMDQ_DRAIN_TIMEOUT_US 1000000 /* 1s! */
Robin Murphy37de98f2017-10-18 15:04:26 +0100422#define ARM_SMMU_SYNC_TIMEOUT_US 1000000 /* 1s! */
Will Deacon48ec83b2015-05-27 17:25:59 +0100423
Eric Auger50019f02017-01-19 20:57:56 +0000424#define MSI_IOVA_BASE 0x8000000
425#define MSI_IOVA_LENGTH 0x100000
426
Will Deacon48ec83b2015-05-27 17:25:59 +0100427static bool disable_bypass;
428module_param_named(disable_bypass, disable_bypass, bool, S_IRUGO);
429MODULE_PARM_DESC(disable_bypass,
430 "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.");
431
432enum pri_resp {
433 PRI_RESP_DENY,
434 PRI_RESP_FAIL,
435 PRI_RESP_SUCC,
436};
437
Marc Zyngier166bdbd2015-10-13 18:32:30 +0100438enum arm_smmu_msi_index {
439 EVTQ_MSI_INDEX,
440 GERROR_MSI_INDEX,
441 PRIQ_MSI_INDEX,
442 ARM_SMMU_MAX_MSIS,
443};
444
445static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {
446 [EVTQ_MSI_INDEX] = {
447 ARM_SMMU_EVTQ_IRQ_CFG0,
448 ARM_SMMU_EVTQ_IRQ_CFG1,
449 ARM_SMMU_EVTQ_IRQ_CFG2,
450 },
451 [GERROR_MSI_INDEX] = {
452 ARM_SMMU_GERROR_IRQ_CFG0,
453 ARM_SMMU_GERROR_IRQ_CFG1,
454 ARM_SMMU_GERROR_IRQ_CFG2,
455 },
456 [PRIQ_MSI_INDEX] = {
457 ARM_SMMU_PRIQ_IRQ_CFG0,
458 ARM_SMMU_PRIQ_IRQ_CFG1,
459 ARM_SMMU_PRIQ_IRQ_CFG2,
460 },
461};
462
Will Deacon48ec83b2015-05-27 17:25:59 +0100463struct arm_smmu_cmdq_ent {
464 /* Common fields */
465 u8 opcode;
466 bool substream_valid;
467
468 /* Command-specific fields */
469 union {
470 #define CMDQ_OP_PREFETCH_CFG 0x1
471 struct {
472 u32 sid;
473 u8 size;
474 u64 addr;
475 } prefetch;
476
477 #define CMDQ_OP_CFGI_STE 0x3
478 #define CMDQ_OP_CFGI_ALL 0x4
479 struct {
480 u32 sid;
481 union {
482 bool leaf;
483 u8 span;
484 };
485 } cfgi;
486
487 #define CMDQ_OP_TLBI_NH_ASID 0x11
488 #define CMDQ_OP_TLBI_NH_VA 0x12
489 #define CMDQ_OP_TLBI_EL2_ALL 0x20
490 #define CMDQ_OP_TLBI_S12_VMALL 0x28
491 #define CMDQ_OP_TLBI_S2_IPA 0x2a
492 #define CMDQ_OP_TLBI_NSNH_ALL 0x30
493 struct {
494 u16 asid;
495 u16 vmid;
496 bool leaf;
497 u64 addr;
498 } tlbi;
499
500 #define CMDQ_OP_PRI_RESP 0x41
501 struct {
502 u32 sid;
503 u32 ssid;
504 u16 grpid;
505 enum pri_resp resp;
506 } pri;
507
508 #define CMDQ_OP_CMD_SYNC 0x46
Robin Murphy37de98f2017-10-18 15:04:26 +0100509 struct {
510 u32 msidata;
511 u64 msiaddr;
512 } sync;
Will Deacon48ec83b2015-05-27 17:25:59 +0100513 };
514};
515
516struct arm_smmu_queue {
517 int irq; /* Wired interrupt */
518
519 __le64 *base;
520 dma_addr_t base_dma;
521 u64 q_base;
522
523 size_t ent_dwords;
524 u32 max_n_shift;
525 u32 prod;
526 u32 cons;
527
528 u32 __iomem *prod_reg;
529 u32 __iomem *cons_reg;
530};
531
532struct arm_smmu_cmdq {
533 struct arm_smmu_queue q;
534 spinlock_t lock;
535};
536
537struct arm_smmu_evtq {
538 struct arm_smmu_queue q;
539 u32 max_stalls;
540};
541
542struct arm_smmu_priq {
543 struct arm_smmu_queue q;
544};
545
546/* High-level stream table and context descriptor structures */
547struct arm_smmu_strtab_l1_desc {
548 u8 span;
549
550 __le64 *l2ptr;
551 dma_addr_t l2ptr_dma;
552};
553
554struct arm_smmu_s1_cfg {
555 __le64 *cdptr;
556 dma_addr_t cdptr_dma;
557
558 struct arm_smmu_ctx_desc {
559 u16 asid;
560 u64 ttbr;
561 u64 tcr;
562 u64 mair;
563 } cd;
564};
565
566struct arm_smmu_s2_cfg {
567 u16 vmid;
568 u64 vttbr;
569 u64 vtcr;
570};
571
572struct arm_smmu_strtab_ent {
Will Deaconbeb3c6a2017-01-06 16:27:30 +0000573 /*
574 * An STE is "assigned" if the master emitting the corresponding SID
575 * is attached to a domain. The behaviour of an unassigned STE is
576 * determined by the disable_bypass parameter, whereas an assigned
577 * STE behaves according to s1_cfg/s2_cfg, which themselves are
578 * configured according to the domain type.
579 */
580 bool assigned;
Will Deacon48ec83b2015-05-27 17:25:59 +0100581 struct arm_smmu_s1_cfg *s1_cfg;
582 struct arm_smmu_s2_cfg *s2_cfg;
583};
584
585struct arm_smmu_strtab_cfg {
586 __le64 *strtab;
587 dma_addr_t strtab_dma;
588 struct arm_smmu_strtab_l1_desc *l1_desc;
589 unsigned int num_l1_ents;
590
591 u64 strtab_base;
592 u32 strtab_base_cfg;
593};
594
595/* An SMMUv3 instance */
596struct arm_smmu_device {
597 struct device *dev;
598 void __iomem *base;
599
600#define ARM_SMMU_FEAT_2_LVL_STRTAB (1 << 0)
601#define ARM_SMMU_FEAT_2_LVL_CDTAB (1 << 1)
602#define ARM_SMMU_FEAT_TT_LE (1 << 2)
603#define ARM_SMMU_FEAT_TT_BE (1 << 3)
604#define ARM_SMMU_FEAT_PRI (1 << 4)
605#define ARM_SMMU_FEAT_ATS (1 << 5)
606#define ARM_SMMU_FEAT_SEV (1 << 6)
607#define ARM_SMMU_FEAT_MSI (1 << 7)
608#define ARM_SMMU_FEAT_COHERENCY (1 << 8)
609#define ARM_SMMU_FEAT_TRANS_S1 (1 << 9)
610#define ARM_SMMU_FEAT_TRANS_S2 (1 << 10)
611#define ARM_SMMU_FEAT_STALLS (1 << 11)
612#define ARM_SMMU_FEAT_HYP (1 << 12)
Yisheng Xie9cff86fd22017-09-21 20:36:07 +0800613#define ARM_SMMU_FEAT_STALL_FORCE (1 << 13)
Will Deacon48ec83b2015-05-27 17:25:59 +0100614 u32 features;
615
Zhen Lei5e929462015-07-07 04:30:18 +0100616#define ARM_SMMU_OPT_SKIP_PREFETCH (1 << 0)
Linu Cheriane5b829d2017-06-22 17:35:37 +0530617#define ARM_SMMU_OPT_PAGE0_REGS_ONLY (1 << 1)
Zhen Lei5e929462015-07-07 04:30:18 +0100618 u32 options;
619
Will Deacon48ec83b2015-05-27 17:25:59 +0100620 struct arm_smmu_cmdq cmdq;
621 struct arm_smmu_evtq evtq;
622 struct arm_smmu_priq priq;
623
624 int gerr_irq;
Geetha Sowjanyaf9354482017-06-23 19:04:36 +0530625 int combined_irq;
Robin Murphy37de98f2017-10-18 15:04:26 +0100626 atomic_t sync_nr;
Will Deacon48ec83b2015-05-27 17:25:59 +0100627
628 unsigned long ias; /* IPA */
629 unsigned long oas; /* PA */
Robin Murphyd5466352016-05-09 17:20:09 +0100630 unsigned long pgsize_bitmap;
Will Deacon48ec83b2015-05-27 17:25:59 +0100631
632#define ARM_SMMU_MAX_ASIDS (1 << 16)
633 unsigned int asid_bits;
634 DECLARE_BITMAP(asid_map, ARM_SMMU_MAX_ASIDS);
635
636#define ARM_SMMU_MAX_VMIDS (1 << 16)
637 unsigned int vmid_bits;
638 DECLARE_BITMAP(vmid_map, ARM_SMMU_MAX_VMIDS);
639
640 unsigned int ssid_bits;
641 unsigned int sid_bits;
642
643 struct arm_smmu_strtab_cfg strtab_cfg;
Joerg Roedel9648cbc2017-02-01 18:11:36 +0100644
Robin Murphy37de98f2017-10-18 15:04:26 +0100645 u32 sync_count;
646
Joerg Roedel9648cbc2017-02-01 18:11:36 +0100647 /* IOMMU core code handle */
648 struct iommu_device iommu;
Will Deacon48ec83b2015-05-27 17:25:59 +0100649};
650
Robin Murphy8f785152016-09-12 17:13:45 +0100651/* SMMU private data for each master */
652struct arm_smmu_master_data {
Will Deacon48ec83b2015-05-27 17:25:59 +0100653 struct arm_smmu_device *smmu;
Will Deacon48ec83b2015-05-27 17:25:59 +0100654 struct arm_smmu_strtab_ent ste;
655};
656
657/* SMMU private data for an IOMMU domain */
658enum arm_smmu_domain_stage {
659 ARM_SMMU_DOMAIN_S1 = 0,
660 ARM_SMMU_DOMAIN_S2,
661 ARM_SMMU_DOMAIN_NESTED,
Will Deaconbeb3c6a2017-01-06 16:27:30 +0000662 ARM_SMMU_DOMAIN_BYPASS,
Will Deacon48ec83b2015-05-27 17:25:59 +0100663};
664
665struct arm_smmu_domain {
666 struct arm_smmu_device *smmu;
667 struct mutex init_mutex; /* Protects smmu pointer */
668
669 struct io_pgtable_ops *pgtbl_ops;
Will Deacon48ec83b2015-05-27 17:25:59 +0100670
671 enum arm_smmu_domain_stage stage;
672 union {
673 struct arm_smmu_s1_cfg s1_cfg;
674 struct arm_smmu_s2_cfg s2_cfg;
675 };
676
677 struct iommu_domain domain;
678};
679
Zhen Lei5e929462015-07-07 04:30:18 +0100680struct arm_smmu_option_prop {
681 u32 opt;
682 const char *prop;
683};
684
685static struct arm_smmu_option_prop arm_smmu_options[] = {
686 { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
Linu Cheriane5b829d2017-06-22 17:35:37 +0530687 { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"},
Zhen Lei5e929462015-07-07 04:30:18 +0100688 { 0, NULL},
689};
690
Linu Cheriane5b829d2017-06-22 17:35:37 +0530691static inline void __iomem *arm_smmu_page1_fixup(unsigned long offset,
692 struct arm_smmu_device *smmu)
693{
694 if ((offset > SZ_64K) &&
695 (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY))
696 offset -= SZ_64K;
697
698 return smmu->base + offset;
699}
700
Will Deacon48ec83b2015-05-27 17:25:59 +0100701static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
702{
703 return container_of(dom, struct arm_smmu_domain, domain);
704}
705
Zhen Lei5e929462015-07-07 04:30:18 +0100706static void parse_driver_options(struct arm_smmu_device *smmu)
707{
708 int i = 0;
709
710 do {
711 if (of_property_read_bool(smmu->dev->of_node,
712 arm_smmu_options[i].prop)) {
713 smmu->options |= arm_smmu_options[i].opt;
714 dev_notice(smmu->dev, "option %s\n",
715 arm_smmu_options[i].prop);
716 }
717 } while (arm_smmu_options[++i].opt);
718}
719
Will Deacon48ec83b2015-05-27 17:25:59 +0100720/* Low-level queue manipulation functions */
721static bool queue_full(struct arm_smmu_queue *q)
722{
723 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
724 Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
725}
726
727static bool queue_empty(struct arm_smmu_queue *q)
728{
729 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
730 Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
731}
732
733static void queue_sync_cons(struct arm_smmu_queue *q)
734{
735 q->cons = readl_relaxed(q->cons_reg);
736}
737
738static void queue_inc_cons(struct arm_smmu_queue *q)
739{
740 u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
741
742 q->cons = Q_OVF(q, q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
743 writel(q->cons, q->cons_reg);
744}
745
746static int queue_sync_prod(struct arm_smmu_queue *q)
747{
748 int ret = 0;
749 u32 prod = readl_relaxed(q->prod_reg);
750
751 if (Q_OVF(q, prod) != Q_OVF(q, q->prod))
752 ret = -EOVERFLOW;
753
754 q->prod = prod;
755 return ret;
756}
757
758static void queue_inc_prod(struct arm_smmu_queue *q)
759{
760 u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + 1;
761
762 q->prod = Q_OVF(q, q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
763 writel(q->prod, q->prod_reg);
764}
765
Jean-Philippe Bruckerbcfced12016-09-05 14:09:53 +0100766/*
767 * Wait for the SMMU to consume items. If drain is true, wait until the queue
768 * is empty. Otherwise, wait until there is at least one free slot.
769 */
770static int queue_poll_cons(struct arm_smmu_queue *q, bool drain, bool wfe)
Will Deacon48ec83b2015-05-27 17:25:59 +0100771{
Sunil Gouthamb847de42017-05-05 16:47:46 +0530772 ktime_t timeout;
773 unsigned int delay = 1;
774
775 /* Wait longer if it's queue drain */
776 timeout = ktime_add_us(ktime_get(), drain ?
777 ARM_SMMU_CMDQ_DRAIN_TIMEOUT_US :
778 ARM_SMMU_POLL_TIMEOUT_US);
Will Deacon48ec83b2015-05-27 17:25:59 +0100779
Jean-Philippe Bruckerbcfced12016-09-05 14:09:53 +0100780 while (queue_sync_cons(q), (drain ? !queue_empty(q) : queue_full(q))) {
Will Deacon48ec83b2015-05-27 17:25:59 +0100781 if (ktime_compare(ktime_get(), timeout) > 0)
782 return -ETIMEDOUT;
783
784 if (wfe) {
785 wfe();
786 } else {
787 cpu_relax();
Sunil Gouthamb847de42017-05-05 16:47:46 +0530788 udelay(delay);
789 delay *= 2;
Will Deacon48ec83b2015-05-27 17:25:59 +0100790 }
791 }
792
793 return 0;
794}
795
796static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
797{
798 int i;
799
800 for (i = 0; i < n_dwords; ++i)
801 *dst++ = cpu_to_le64(*src++);
802}
803
804static int queue_insert_raw(struct arm_smmu_queue *q, u64 *ent)
805{
806 if (queue_full(q))
807 return -ENOSPC;
808
809 queue_write(Q_ENT(q, q->prod), ent, q->ent_dwords);
810 queue_inc_prod(q);
811 return 0;
812}
813
814static void queue_read(__le64 *dst, u64 *src, size_t n_dwords)
815{
816 int i;
817
818 for (i = 0; i < n_dwords; ++i)
819 *dst++ = le64_to_cpu(*src++);
820}
821
822static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent)
823{
824 if (queue_empty(q))
825 return -EAGAIN;
826
827 queue_read(ent, Q_ENT(q, q->cons), q->ent_dwords);
828 queue_inc_cons(q);
829 return 0;
830}
831
832/* High-level queue accessors */
833static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
834{
835 memset(cmd, 0, CMDQ_ENT_DWORDS << 3);
836 cmd[0] |= (ent->opcode & CMDQ_0_OP_MASK) << CMDQ_0_OP_SHIFT;
837
838 switch (ent->opcode) {
839 case CMDQ_OP_TLBI_EL2_ALL:
840 case CMDQ_OP_TLBI_NSNH_ALL:
841 break;
842 case CMDQ_OP_PREFETCH_CFG:
843 cmd[0] |= (u64)ent->prefetch.sid << CMDQ_PREFETCH_0_SID_SHIFT;
844 cmd[1] |= ent->prefetch.size << CMDQ_PREFETCH_1_SIZE_SHIFT;
845 cmd[1] |= ent->prefetch.addr & CMDQ_PREFETCH_1_ADDR_MASK;
846 break;
847 case CMDQ_OP_CFGI_STE:
848 cmd[0] |= (u64)ent->cfgi.sid << CMDQ_CFGI_0_SID_SHIFT;
849 cmd[1] |= ent->cfgi.leaf ? CMDQ_CFGI_1_LEAF : 0;
850 break;
851 case CMDQ_OP_CFGI_ALL:
852 /* Cover the entire SID range */
853 cmd[1] |= CMDQ_CFGI_1_RANGE_MASK << CMDQ_CFGI_1_RANGE_SHIFT;
854 break;
855 case CMDQ_OP_TLBI_NH_VA:
856 cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
Will Deacon1c27df12015-09-18 16:12:56 +0100857 cmd[1] |= ent->tlbi.leaf ? CMDQ_TLBI_1_LEAF : 0;
858 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_VA_MASK;
859 break;
Will Deacon48ec83b2015-05-27 17:25:59 +0100860 case CMDQ_OP_TLBI_S2_IPA:
861 cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
862 cmd[1] |= ent->tlbi.leaf ? CMDQ_TLBI_1_LEAF : 0;
Will Deacon1c27df12015-09-18 16:12:56 +0100863 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_IPA_MASK;
Will Deacon48ec83b2015-05-27 17:25:59 +0100864 break;
865 case CMDQ_OP_TLBI_NH_ASID:
866 cmd[0] |= (u64)ent->tlbi.asid << CMDQ_TLBI_0_ASID_SHIFT;
867 /* Fallthrough */
868 case CMDQ_OP_TLBI_S12_VMALL:
869 cmd[0] |= (u64)ent->tlbi.vmid << CMDQ_TLBI_0_VMID_SHIFT;
870 break;
871 case CMDQ_OP_PRI_RESP:
872 cmd[0] |= ent->substream_valid ? CMDQ_0_SSV : 0;
873 cmd[0] |= ent->pri.ssid << CMDQ_PRI_0_SSID_SHIFT;
874 cmd[0] |= (u64)ent->pri.sid << CMDQ_PRI_0_SID_SHIFT;
875 cmd[1] |= ent->pri.grpid << CMDQ_PRI_1_GRPID_SHIFT;
876 switch (ent->pri.resp) {
877 case PRI_RESP_DENY:
878 cmd[1] |= CMDQ_PRI_1_RESP_DENY;
879 break;
880 case PRI_RESP_FAIL:
881 cmd[1] |= CMDQ_PRI_1_RESP_FAIL;
882 break;
883 case PRI_RESP_SUCC:
884 cmd[1] |= CMDQ_PRI_1_RESP_SUCC;
885 break;
886 default:
887 return -EINVAL;
888 }
889 break;
890 case CMDQ_OP_CMD_SYNC:
Robin Murphy37de98f2017-10-18 15:04:26 +0100891 if (ent->sync.msiaddr)
892 cmd[0] |= CMDQ_SYNC_0_CS_IRQ;
893 else
894 cmd[0] |= CMDQ_SYNC_0_CS_SEV;
895 cmd[0] |= CMDQ_SYNC_0_MSH_ISH | CMDQ_SYNC_0_MSIATTR_OIWB;
896 cmd[0] |= (u64)ent->sync.msidata << CMDQ_SYNC_0_MSIDATA_SHIFT;
897 cmd[1] |= ent->sync.msiaddr & CMDQ_SYNC_1_MSIADDR_MASK;
Will Deacon48ec83b2015-05-27 17:25:59 +0100898 break;
899 default:
900 return -ENOENT;
901 }
902
903 return 0;
904}
905
906static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu)
907{
908 static const char *cerror_str[] = {
909 [CMDQ_ERR_CERROR_NONE_IDX] = "No error",
910 [CMDQ_ERR_CERROR_ILL_IDX] = "Illegal command",
911 [CMDQ_ERR_CERROR_ABT_IDX] = "Abort on command fetch",
912 };
913
914 int i;
915 u64 cmd[CMDQ_ENT_DWORDS];
916 struct arm_smmu_queue *q = &smmu->cmdq.q;
917 u32 cons = readl_relaxed(q->cons_reg);
918 u32 idx = cons >> CMDQ_ERR_SHIFT & CMDQ_ERR_MASK;
919 struct arm_smmu_cmdq_ent cmd_sync = {
920 .opcode = CMDQ_OP_CMD_SYNC,
921 };
922
923 dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons,
Will Deacona0d5c042015-12-04 12:00:29 +0000924 idx < ARRAY_SIZE(cerror_str) ? cerror_str[idx] : "Unknown");
Will Deacon48ec83b2015-05-27 17:25:59 +0100925
926 switch (idx) {
Will Deacon48ec83b2015-05-27 17:25:59 +0100927 case CMDQ_ERR_CERROR_ABT_IDX:
928 dev_err(smmu->dev, "retrying command fetch\n");
929 case CMDQ_ERR_CERROR_NONE_IDX:
930 return;
Will Deacona0d5c042015-12-04 12:00:29 +0000931 case CMDQ_ERR_CERROR_ILL_IDX:
932 /* Fallthrough */
933 default:
934 break;
Will Deacon48ec83b2015-05-27 17:25:59 +0100935 }
936
937 /*
938 * We may have concurrent producers, so we need to be careful
939 * not to touch any of the shadow cmdq state.
940 */
Will Deaconaea20372016-07-29 11:15:37 +0100941 queue_read(cmd, Q_ENT(q, cons), q->ent_dwords);
Will Deacon48ec83b2015-05-27 17:25:59 +0100942 dev_err(smmu->dev, "skipping command in error state:\n");
943 for (i = 0; i < ARRAY_SIZE(cmd); ++i)
944 dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
945
946 /* Convert the erroneous command into a CMD_SYNC */
947 if (arm_smmu_cmdq_build_cmd(cmd, &cmd_sync)) {
948 dev_err(smmu->dev, "failed to convert to CMD_SYNC\n");
949 return;
950 }
951
Will Deaconaea20372016-07-29 11:15:37 +0100952 queue_write(Q_ENT(q, cons), cmd, q->ent_dwords);
Will Deacon48ec83b2015-05-27 17:25:59 +0100953}
954
Robin Murphy2f657ad2017-08-31 14:44:25 +0100955static void arm_smmu_cmdq_insert_cmd(struct arm_smmu_device *smmu, u64 *cmd)
956{
957 struct arm_smmu_queue *q = &smmu->cmdq.q;
958 bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
959
960 while (queue_insert_raw(q, cmd) == -ENOSPC) {
961 if (queue_poll_cons(q, false, wfe))
962 dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
963 }
964}
965
Will Deacon48ec83b2015-05-27 17:25:59 +0100966static void arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
967 struct arm_smmu_cmdq_ent *ent)
968{
Will Deacon48ec83b2015-05-27 17:25:59 +0100969 u64 cmd[CMDQ_ENT_DWORDS];
Will Deacon8ded2902016-09-09 14:33:59 +0100970 unsigned long flags;
Will Deacon48ec83b2015-05-27 17:25:59 +0100971
972 if (arm_smmu_cmdq_build_cmd(cmd, ent)) {
973 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
974 ent->opcode);
975 return;
976 }
977
Will Deacon8ded2902016-09-09 14:33:59 +0100978 spin_lock_irqsave(&smmu->cmdq.lock, flags);
Robin Murphy2f657ad2017-08-31 14:44:25 +0100979 arm_smmu_cmdq_insert_cmd(smmu, cmd);
Will Deacon8ded2902016-09-09 14:33:59 +0100980 spin_unlock_irqrestore(&smmu->cmdq.lock, flags);
Will Deacon48ec83b2015-05-27 17:25:59 +0100981}
982
Robin Murphy37de98f2017-10-18 15:04:26 +0100983/*
984 * The difference between val and sync_idx is bounded by the maximum size of
985 * a queue at 2^20 entries, so 32 bits is plenty for wrap-safe arithmetic.
986 */
987static int arm_smmu_sync_poll_msi(struct arm_smmu_device *smmu, u32 sync_idx)
988{
989 ktime_t timeout = ktime_add_us(ktime_get(), ARM_SMMU_SYNC_TIMEOUT_US);
990 u32 val = smp_cond_load_acquire(&smmu->sync_count,
991 (int)(VAL - sync_idx) >= 0 ||
992 !ktime_before(ktime_get(), timeout));
993
994 return (int)(val - sync_idx) < 0 ? -ETIMEDOUT : 0;
995}
996
Robin Murphy2f657ad2017-08-31 14:44:25 +0100997static void arm_smmu_cmdq_issue_sync(struct arm_smmu_device *smmu)
998{
999 u64 cmd[CMDQ_ENT_DWORDS];
1000 unsigned long flags;
1001 bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
Robin Murphy37de98f2017-10-18 15:04:26 +01001002 bool msi = (smmu->features & ARM_SMMU_FEAT_MSI) &&
1003 (smmu->features & ARM_SMMU_FEAT_COHERENCY);
Robin Murphy2f657ad2017-08-31 14:44:25 +01001004 struct arm_smmu_cmdq_ent ent = { .opcode = CMDQ_OP_CMD_SYNC };
1005 int ret;
1006
Robin Murphy37de98f2017-10-18 15:04:26 +01001007 if (msi) {
1008 ent.sync.msidata = atomic_inc_return_relaxed(&smmu->sync_nr);
1009 ent.sync.msiaddr = virt_to_phys(&smmu->sync_count);
1010 }
Robin Murphy2f657ad2017-08-31 14:44:25 +01001011 arm_smmu_cmdq_build_cmd(cmd, &ent);
1012
1013 spin_lock_irqsave(&smmu->cmdq.lock, flags);
1014 arm_smmu_cmdq_insert_cmd(smmu, cmd);
Robin Murphy37de98f2017-10-18 15:04:26 +01001015 if (!msi)
1016 ret = queue_poll_cons(&smmu->cmdq.q, true, wfe);
Robin Murphy2f657ad2017-08-31 14:44:25 +01001017 spin_unlock_irqrestore(&smmu->cmdq.lock, flags);
1018
Robin Murphy37de98f2017-10-18 15:04:26 +01001019 if (msi)
1020 ret = arm_smmu_sync_poll_msi(smmu, ent.sync.msidata);
Robin Murphy2f657ad2017-08-31 14:44:25 +01001021 if (ret)
1022 dev_err_ratelimited(smmu->dev, "CMD_SYNC timeout\n");
1023}
1024
Will Deacon48ec83b2015-05-27 17:25:59 +01001025/* Context descriptor manipulation functions */
1026static u64 arm_smmu_cpu_tcr_to_cd(u64 tcr)
1027{
1028 u64 val = 0;
1029
1030 /* Repack the TCR. Just care about TTBR0 for now */
1031 val |= ARM_SMMU_TCR2CD(tcr, T0SZ);
1032 val |= ARM_SMMU_TCR2CD(tcr, TG0);
1033 val |= ARM_SMMU_TCR2CD(tcr, IRGN0);
1034 val |= ARM_SMMU_TCR2CD(tcr, ORGN0);
1035 val |= ARM_SMMU_TCR2CD(tcr, SH0);
1036 val |= ARM_SMMU_TCR2CD(tcr, EPD0);
1037 val |= ARM_SMMU_TCR2CD(tcr, EPD1);
1038 val |= ARM_SMMU_TCR2CD(tcr, IPS);
1039 val |= ARM_SMMU_TCR2CD(tcr, TBI0);
1040
1041 return val;
1042}
1043
1044static void arm_smmu_write_ctx_desc(struct arm_smmu_device *smmu,
1045 struct arm_smmu_s1_cfg *cfg)
1046{
1047 u64 val;
1048
1049 /*
1050 * We don't need to issue any invalidation here, as we'll invalidate
1051 * the STE when installing the new entry anyway.
1052 */
1053 val = arm_smmu_cpu_tcr_to_cd(cfg->cd.tcr) |
1054#ifdef __BIG_ENDIAN
1055 CTXDESC_CD_0_ENDI |
1056#endif
1057 CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET_PRIVATE |
1058 CTXDESC_CD_0_AA64 | (u64)cfg->cd.asid << CTXDESC_CD_0_ASID_SHIFT |
1059 CTXDESC_CD_0_V;
Yisheng Xie9cff86fd22017-09-21 20:36:07 +08001060
1061 /* STALL_MODEL==0b10 && CD.S==0 is ILLEGAL */
1062 if (smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
1063 val |= CTXDESC_CD_0_S;
1064
Will Deacon48ec83b2015-05-27 17:25:59 +01001065 cfg->cdptr[0] = cpu_to_le64(val);
1066
1067 val = cfg->cd.ttbr & CTXDESC_CD_1_TTB0_MASK << CTXDESC_CD_1_TTB0_SHIFT;
1068 cfg->cdptr[1] = cpu_to_le64(val);
1069
1070 cfg->cdptr[3] = cpu_to_le64(cfg->cd.mair << CTXDESC_CD_3_MAIR_SHIFT);
1071}
1072
1073/* Stream table manipulation functions */
1074static void
1075arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc)
1076{
1077 u64 val = 0;
1078
1079 val |= (desc->span & STRTAB_L1_DESC_SPAN_MASK)
1080 << STRTAB_L1_DESC_SPAN_SHIFT;
1081 val |= desc->l2ptr_dma &
1082 STRTAB_L1_DESC_L2PTR_MASK << STRTAB_L1_DESC_L2PTR_SHIFT;
1083
1084 *dst = cpu_to_le64(val);
1085}
1086
1087static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid)
1088{
1089 struct arm_smmu_cmdq_ent cmd = {
1090 .opcode = CMDQ_OP_CFGI_STE,
1091 .cfgi = {
1092 .sid = sid,
1093 .leaf = true,
1094 },
1095 };
1096
1097 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
Robin Murphy2f657ad2017-08-31 14:44:25 +01001098 arm_smmu_cmdq_issue_sync(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01001099}
1100
1101static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
1102 __le64 *dst, struct arm_smmu_strtab_ent *ste)
1103{
1104 /*
1105 * This is hideously complicated, but we only really care about
1106 * three cases at the moment:
1107 *
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001108 * 1. Invalid (all zero) -> bypass/fault (init)
1109 * 2. Bypass/fault -> translation/bypass (attach)
1110 * 3. Translation/bypass -> bypass/fault (detach)
Will Deacon48ec83b2015-05-27 17:25:59 +01001111 *
1112 * Given that we can't update the STE atomically and the SMMU
1113 * doesn't read the thing in a defined order, that leaves us
1114 * with the following maintenance requirements:
1115 *
1116 * 1. Update Config, return (init time STEs aren't live)
1117 * 2. Write everything apart from dword 0, sync, write dword 0, sync
1118 * 3. Update Config, sync
1119 */
1120 u64 val = le64_to_cpu(dst[0]);
1121 bool ste_live = false;
1122 struct arm_smmu_cmdq_ent prefetch_cmd = {
1123 .opcode = CMDQ_OP_PREFETCH_CFG,
1124 .prefetch = {
1125 .sid = sid,
1126 },
1127 };
1128
1129 if (val & STRTAB_STE_0_V) {
1130 u64 cfg;
1131
1132 cfg = val & STRTAB_STE_0_CFG_MASK << STRTAB_STE_0_CFG_SHIFT;
1133 switch (cfg) {
1134 case STRTAB_STE_0_CFG_BYPASS:
1135 break;
1136 case STRTAB_STE_0_CFG_S1_TRANS:
1137 case STRTAB_STE_0_CFG_S2_TRANS:
1138 ste_live = true;
1139 break;
Will Deacon5bc0a112016-08-16 14:29:16 +01001140 case STRTAB_STE_0_CFG_ABORT:
1141 if (disable_bypass)
1142 break;
Will Deacon48ec83b2015-05-27 17:25:59 +01001143 default:
1144 BUG(); /* STE corruption */
1145 }
1146 }
1147
Nate Watterson810871c2016-12-20 23:11:48 -05001148 /* Nuke the existing STE_0 value, as we're going to rewrite it */
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001149 val = STRTAB_STE_0_V;
Will Deacon48ec83b2015-05-27 17:25:59 +01001150
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001151 /* Bypass/fault */
1152 if (!ste->assigned || !(ste->s1_cfg || ste->s2_cfg)) {
1153 if (!ste->assigned && disable_bypass)
1154 val |= STRTAB_STE_0_CFG_ABORT;
1155 else
1156 val |= STRTAB_STE_0_CFG_BYPASS;
1157
Will Deacon48ec83b2015-05-27 17:25:59 +01001158 dst[0] = cpu_to_le64(val);
Will Deacona0eacd82015-11-18 18:15:51 +00001159 dst[1] = cpu_to_le64(STRTAB_STE_1_SHCFG_INCOMING
1160 << STRTAB_STE_1_SHCFG_SHIFT);
Will Deacon48ec83b2015-05-27 17:25:59 +01001161 dst[2] = 0; /* Nuke the VMID */
Will Deacon704c0382017-10-05 16:49:37 +01001162 /*
1163 * The SMMU can perform negative caching, so we must sync
1164 * the STE regardless of whether the old value was live.
1165 */
1166 if (smmu)
Will Deacon48ec83b2015-05-27 17:25:59 +01001167 arm_smmu_sync_ste_for_sid(smmu, sid);
1168 return;
1169 }
1170
1171 if (ste->s1_cfg) {
1172 BUG_ON(ste_live);
1173 dst[1] = cpu_to_le64(
1174 STRTAB_STE_1_S1C_CACHE_WBRA
1175 << STRTAB_STE_1_S1CIR_SHIFT |
1176 STRTAB_STE_1_S1C_CACHE_WBRA
1177 << STRTAB_STE_1_S1COR_SHIFT |
1178 STRTAB_STE_1_S1C_SH_ISH << STRTAB_STE_1_S1CSH_SHIFT |
Will Deacon48ec83b2015-05-27 17:25:59 +01001179#ifdef CONFIG_PCI_ATS
1180 STRTAB_STE_1_EATS_TRANS << STRTAB_STE_1_EATS_SHIFT |
1181#endif
Robin Murphy14b4dba2017-01-06 18:58:16 +05301182 STRTAB_STE_1_STRW_NSEL1 << STRTAB_STE_1_STRW_SHIFT);
Will Deacon48ec83b2015-05-27 17:25:59 +01001183
Yisheng Xie9cff86fd22017-09-21 20:36:07 +08001184 if (smmu->features & ARM_SMMU_FEAT_STALLS &&
1185 !(smmu->features & ARM_SMMU_FEAT_STALL_FORCE))
Prem Mallappa6380be02015-12-14 22:01:23 +05301186 dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD);
1187
Will Deacon48ec83b2015-05-27 17:25:59 +01001188 val |= (ste->s1_cfg->cdptr_dma & STRTAB_STE_0_S1CTXPTR_MASK
1189 << STRTAB_STE_0_S1CTXPTR_SHIFT) |
1190 STRTAB_STE_0_CFG_S1_TRANS;
Will Deacon48ec83b2015-05-27 17:25:59 +01001191 }
1192
1193 if (ste->s2_cfg) {
1194 BUG_ON(ste_live);
1195 dst[2] = cpu_to_le64(
1196 ste->s2_cfg->vmid << STRTAB_STE_2_S2VMID_SHIFT |
1197 (ste->s2_cfg->vtcr & STRTAB_STE_2_VTCR_MASK)
1198 << STRTAB_STE_2_VTCR_SHIFT |
1199#ifdef __BIG_ENDIAN
1200 STRTAB_STE_2_S2ENDI |
1201#endif
1202 STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 |
1203 STRTAB_STE_2_S2R);
1204
1205 dst[3] = cpu_to_le64(ste->s2_cfg->vttbr &
1206 STRTAB_STE_3_S2TTB_MASK << STRTAB_STE_3_S2TTB_SHIFT);
1207
1208 val |= STRTAB_STE_0_CFG_S2_TRANS;
1209 }
1210
1211 arm_smmu_sync_ste_for_sid(smmu, sid);
1212 dst[0] = cpu_to_le64(val);
1213 arm_smmu_sync_ste_for_sid(smmu, sid);
1214
1215 /* It's likely that we'll want to use the new STE soon */
Zhen Lei5e929462015-07-07 04:30:18 +01001216 if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
1217 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
Will Deacon48ec83b2015-05-27 17:25:59 +01001218}
1219
1220static void arm_smmu_init_bypass_stes(u64 *strtab, unsigned int nent)
1221{
1222 unsigned int i;
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001223 struct arm_smmu_strtab_ent ste = { .assigned = false };
Will Deacon48ec83b2015-05-27 17:25:59 +01001224
1225 for (i = 0; i < nent; ++i) {
1226 arm_smmu_write_strtab_ent(NULL, -1, strtab, &ste);
1227 strtab += STRTAB_STE_DWORDS;
1228 }
1229}
1230
1231static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1232{
1233 size_t size;
1234 void *strtab;
1235 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1236 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[sid >> STRTAB_SPLIT];
1237
1238 if (desc->l2ptr)
1239 return 0;
1240
1241 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
Zhen Lei69146e72015-06-26 09:32:58 +01001242 strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
Will Deacon48ec83b2015-05-27 17:25:59 +01001243
1244 desc->span = STRTAB_SPLIT + 1;
Will Deacon04fa26c2015-10-30 18:12:41 +00001245 desc->l2ptr = dmam_alloc_coherent(smmu->dev, size, &desc->l2ptr_dma,
1246 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01001247 if (!desc->l2ptr) {
1248 dev_err(smmu->dev,
1249 "failed to allocate l2 stream table for SID %u\n",
1250 sid);
1251 return -ENOMEM;
1252 }
1253
1254 arm_smmu_init_bypass_stes(desc->l2ptr, 1 << STRTAB_SPLIT);
1255 arm_smmu_write_strtab_l1_desc(strtab, desc);
1256 return 0;
1257}
1258
1259/* IRQ and event handlers */
1260static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
1261{
1262 int i;
1263 struct arm_smmu_device *smmu = dev;
1264 struct arm_smmu_queue *q = &smmu->evtq.q;
1265 u64 evt[EVTQ_ENT_DWORDS];
1266
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001267 do {
1268 while (!queue_remove_raw(q, evt)) {
1269 u8 id = evt[0] >> EVTQ_0_ID_SHIFT & EVTQ_0_ID_MASK;
Will Deacon48ec83b2015-05-27 17:25:59 +01001270
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001271 dev_info(smmu->dev, "event 0x%02x received:\n", id);
1272 for (i = 0; i < ARRAY_SIZE(evt); ++i)
1273 dev_info(smmu->dev, "\t0x%016llx\n",
1274 (unsigned long long)evt[i]);
1275
1276 }
1277
1278 /*
1279 * Not much we can do on overflow, so scream and pretend we're
1280 * trying harder.
1281 */
1282 if (queue_sync_prod(q) == -EOVERFLOW)
1283 dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
1284 } while (!queue_empty(q));
Will Deacon48ec83b2015-05-27 17:25:59 +01001285
1286 /* Sync our overflow flag, as we believe we're up to speed */
1287 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1288 return IRQ_HANDLED;
1289}
1290
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001291static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt)
Will Deacon48ec83b2015-05-27 17:25:59 +01001292{
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001293 u32 sid, ssid;
1294 u16 grpid;
1295 bool ssv, last;
Will Deacon48ec83b2015-05-27 17:25:59 +01001296
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001297 sid = evt[0] >> PRIQ_0_SID_SHIFT & PRIQ_0_SID_MASK;
1298 ssv = evt[0] & PRIQ_0_SSID_V;
1299 ssid = ssv ? evt[0] >> PRIQ_0_SSID_SHIFT & PRIQ_0_SSID_MASK : 0;
1300 last = evt[0] & PRIQ_0_PRG_LAST;
1301 grpid = evt[1] >> PRIQ_1_PRG_IDX_SHIFT & PRIQ_1_PRG_IDX_MASK;
Will Deacon48ec83b2015-05-27 17:25:59 +01001302
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001303 dev_info(smmu->dev, "unexpected PRI request received:\n");
1304 dev_info(smmu->dev,
1305 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
1306 sid, ssid, grpid, last ? "L" : "",
1307 evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
1308 evt[0] & PRIQ_0_PERM_READ ? "R" : "",
1309 evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
1310 evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
1311 evt[1] & PRIQ_1_ADDR_MASK << PRIQ_1_ADDR_SHIFT);
1312
1313 if (last) {
1314 struct arm_smmu_cmdq_ent cmd = {
1315 .opcode = CMDQ_OP_PRI_RESP,
1316 .substream_valid = ssv,
1317 .pri = {
1318 .sid = sid,
1319 .ssid = ssid,
1320 .grpid = grpid,
1321 .resp = PRI_RESP_DENY,
1322 },
1323 };
1324
1325 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1326 }
Will Deacon48ec83b2015-05-27 17:25:59 +01001327}
1328
1329static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
1330{
1331 struct arm_smmu_device *smmu = dev;
1332 struct arm_smmu_queue *q = &smmu->priq.q;
1333 u64 evt[PRIQ_ENT_DWORDS];
1334
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001335 do {
1336 while (!queue_remove_raw(q, evt))
1337 arm_smmu_handle_ppr(smmu, evt);
Will Deacon48ec83b2015-05-27 17:25:59 +01001338
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001339 if (queue_sync_prod(q) == -EOVERFLOW)
1340 dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
1341 } while (!queue_empty(q));
Will Deacon48ec83b2015-05-27 17:25:59 +01001342
1343 /* Sync our overflow flag, as we believe we're up to speed */
1344 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1345 return IRQ_HANDLED;
1346}
1347
Will Deacon48ec83b2015-05-27 17:25:59 +01001348static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
1349
1350static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
1351{
Prem Mallappa324ba102015-12-14 22:01:14 +05301352 u32 gerror, gerrorn, active;
Will Deacon48ec83b2015-05-27 17:25:59 +01001353 struct arm_smmu_device *smmu = dev;
1354
1355 gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
1356 gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
1357
Prem Mallappa324ba102015-12-14 22:01:14 +05301358 active = gerror ^ gerrorn;
1359 if (!(active & GERROR_ERR_MASK))
Will Deacon48ec83b2015-05-27 17:25:59 +01001360 return IRQ_NONE; /* No errors pending */
1361
1362 dev_warn(smmu->dev,
1363 "unexpected global error reported (0x%08x), this could be serious\n",
Prem Mallappa324ba102015-12-14 22:01:14 +05301364 active);
Will Deacon48ec83b2015-05-27 17:25:59 +01001365
Prem Mallappa324ba102015-12-14 22:01:14 +05301366 if (active & GERROR_SFM_ERR) {
Will Deacon48ec83b2015-05-27 17:25:59 +01001367 dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
1368 arm_smmu_device_disable(smmu);
1369 }
1370
Prem Mallappa324ba102015-12-14 22:01:14 +05301371 if (active & GERROR_MSI_GERROR_ABT_ERR)
Will Deacon48ec83b2015-05-27 17:25:59 +01001372 dev_warn(smmu->dev, "GERROR MSI write aborted\n");
1373
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001374 if (active & GERROR_MSI_PRIQ_ABT_ERR)
Will Deacon48ec83b2015-05-27 17:25:59 +01001375 dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
Will Deacon48ec83b2015-05-27 17:25:59 +01001376
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01001377 if (active & GERROR_MSI_EVTQ_ABT_ERR)
Will Deacon48ec83b2015-05-27 17:25:59 +01001378 dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
Will Deacon48ec83b2015-05-27 17:25:59 +01001379
Robin Murphydce032a2017-08-31 14:44:26 +01001380 if (active & GERROR_MSI_CMDQ_ABT_ERR)
Will Deacon48ec83b2015-05-27 17:25:59 +01001381 dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
Will Deacon48ec83b2015-05-27 17:25:59 +01001382
Prem Mallappa324ba102015-12-14 22:01:14 +05301383 if (active & GERROR_PRIQ_ABT_ERR)
Will Deacon48ec83b2015-05-27 17:25:59 +01001384 dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
1385
Prem Mallappa324ba102015-12-14 22:01:14 +05301386 if (active & GERROR_EVTQ_ABT_ERR)
Will Deacon48ec83b2015-05-27 17:25:59 +01001387 dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
1388
Prem Mallappa324ba102015-12-14 22:01:14 +05301389 if (active & GERROR_CMDQ_ERR)
Will Deacon48ec83b2015-05-27 17:25:59 +01001390 arm_smmu_cmdq_skip_err(smmu);
1391
1392 writel(gerror, smmu->base + ARM_SMMU_GERRORN);
1393 return IRQ_HANDLED;
1394}
1395
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301396static irqreturn_t arm_smmu_combined_irq_thread(int irq, void *dev)
1397{
1398 struct arm_smmu_device *smmu = dev;
1399
1400 arm_smmu_evtq_thread(irq, dev);
1401 if (smmu->features & ARM_SMMU_FEAT_PRI)
1402 arm_smmu_priq_thread(irq, dev);
1403
1404 return IRQ_HANDLED;
1405}
1406
1407static irqreturn_t arm_smmu_combined_irq_handler(int irq, void *dev)
1408{
1409 arm_smmu_gerror_handler(irq, dev);
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05301410 return IRQ_WAKE_THREAD;
1411}
1412
Will Deacon48ec83b2015-05-27 17:25:59 +01001413/* IO_PGTABLE API */
1414static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
1415{
Robin Murphy2f657ad2017-08-31 14:44:25 +01001416 arm_smmu_cmdq_issue_sync(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01001417}
1418
1419static void arm_smmu_tlb_sync(void *cookie)
1420{
1421 struct arm_smmu_domain *smmu_domain = cookie;
1422 __arm_smmu_tlb_sync(smmu_domain->smmu);
1423}
1424
1425static void arm_smmu_tlb_inv_context(void *cookie)
1426{
1427 struct arm_smmu_domain *smmu_domain = cookie;
1428 struct arm_smmu_device *smmu = smmu_domain->smmu;
1429 struct arm_smmu_cmdq_ent cmd;
1430
1431 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1432 cmd.opcode = CMDQ_OP_TLBI_NH_ASID;
1433 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1434 cmd.tlbi.vmid = 0;
1435 } else {
1436 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL;
1437 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1438 }
1439
1440 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1441 __arm_smmu_tlb_sync(smmu);
1442}
1443
1444static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
Robin Murphy06c610e2015-12-07 18:18:53 +00001445 size_t granule, bool leaf, void *cookie)
Will Deacon48ec83b2015-05-27 17:25:59 +01001446{
1447 struct arm_smmu_domain *smmu_domain = cookie;
1448 struct arm_smmu_device *smmu = smmu_domain->smmu;
1449 struct arm_smmu_cmdq_ent cmd = {
1450 .tlbi = {
1451 .leaf = leaf,
1452 .addr = iova,
1453 },
1454 };
1455
1456 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1457 cmd.opcode = CMDQ_OP_TLBI_NH_VA;
1458 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1459 } else {
1460 cmd.opcode = CMDQ_OP_TLBI_S2_IPA;
1461 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1462 }
1463
Robin Murphy75df1382015-12-07 18:18:52 +00001464 do {
1465 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1466 cmd.tlbi.addr += granule;
1467 } while (size -= granule);
Will Deacon48ec83b2015-05-27 17:25:59 +01001468}
1469
Bhumika Goyalca297aa2016-10-25 23:36:11 +05301470static const struct iommu_gather_ops arm_smmu_gather_ops = {
Will Deacon48ec83b2015-05-27 17:25:59 +01001471 .tlb_flush_all = arm_smmu_tlb_inv_context,
1472 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
1473 .tlb_sync = arm_smmu_tlb_sync,
Will Deacon48ec83b2015-05-27 17:25:59 +01001474};
1475
1476/* IOMMU API */
1477static bool arm_smmu_capable(enum iommu_cap cap)
1478{
1479 switch (cap) {
1480 case IOMMU_CAP_CACHE_COHERENCY:
1481 return true;
Will Deacon48ec83b2015-05-27 17:25:59 +01001482 case IOMMU_CAP_NOEXEC:
1483 return true;
1484 default:
1485 return false;
1486 }
1487}
1488
1489static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
1490{
1491 struct arm_smmu_domain *smmu_domain;
1492
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001493 if (type != IOMMU_DOMAIN_UNMANAGED &&
1494 type != IOMMU_DOMAIN_DMA &&
1495 type != IOMMU_DOMAIN_IDENTITY)
Will Deacon48ec83b2015-05-27 17:25:59 +01001496 return NULL;
1497
1498 /*
1499 * Allocate the domain and initialise some of its data structures.
1500 * We can't really do anything meaningful until we've added a
1501 * master.
1502 */
1503 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1504 if (!smmu_domain)
1505 return NULL;
1506
Robin Murphy9adb9592016-01-26 18:06:36 +00001507 if (type == IOMMU_DOMAIN_DMA &&
1508 iommu_get_dma_cookie(&smmu_domain->domain)) {
1509 kfree(smmu_domain);
1510 return NULL;
1511 }
1512
Will Deacon48ec83b2015-05-27 17:25:59 +01001513 mutex_init(&smmu_domain->init_mutex);
Will Deacon48ec83b2015-05-27 17:25:59 +01001514 return &smmu_domain->domain;
1515}
1516
1517static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
1518{
1519 int idx, size = 1 << span;
1520
1521 do {
1522 idx = find_first_zero_bit(map, size);
1523 if (idx == size)
1524 return -ENOSPC;
1525 } while (test_and_set_bit(idx, map));
1526
1527 return idx;
1528}
1529
1530static void arm_smmu_bitmap_free(unsigned long *map, int idx)
1531{
1532 clear_bit(idx, map);
1533}
1534
1535static void arm_smmu_domain_free(struct iommu_domain *domain)
1536{
1537 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1538 struct arm_smmu_device *smmu = smmu_domain->smmu;
1539
Robin Murphy9adb9592016-01-26 18:06:36 +00001540 iommu_put_dma_cookie(domain);
Markus Elfringa6e08fb2015-06-29 17:47:43 +01001541 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
Will Deacon48ec83b2015-05-27 17:25:59 +01001542
1543 /* Free the CD and ASID, if we allocated them */
1544 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1545 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1546
1547 if (cfg->cdptr) {
Will Deacon04fa26c2015-10-30 18:12:41 +00001548 dmam_free_coherent(smmu_domain->smmu->dev,
1549 CTXDESC_CD_DWORDS << 3,
1550 cfg->cdptr,
1551 cfg->cdptr_dma);
Will Deacon48ec83b2015-05-27 17:25:59 +01001552
1553 arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
1554 }
1555 } else {
1556 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1557 if (cfg->vmid)
1558 arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
1559 }
1560
1561 kfree(smmu_domain);
1562}
1563
1564static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
1565 struct io_pgtable_cfg *pgtbl_cfg)
1566{
1567 int ret;
Will Deaconc0733a22015-10-13 17:51:14 +01001568 int asid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001569 struct arm_smmu_device *smmu = smmu_domain->smmu;
1570 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1571
1572 asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001573 if (asid < 0)
Will Deacon48ec83b2015-05-27 17:25:59 +01001574 return asid;
1575
Will Deacon04fa26c2015-10-30 18:12:41 +00001576 cfg->cdptr = dmam_alloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
1577 &cfg->cdptr_dma,
1578 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01001579 if (!cfg->cdptr) {
1580 dev_warn(smmu->dev, "failed to allocate context descriptor\n");
Will Deaconc0733a22015-10-13 17:51:14 +01001581 ret = -ENOMEM;
Will Deacon48ec83b2015-05-27 17:25:59 +01001582 goto out_free_asid;
1583 }
1584
Will Deaconc0733a22015-10-13 17:51:14 +01001585 cfg->cd.asid = (u16)asid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001586 cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
1587 cfg->cd.tcr = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
1588 cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
1589 return 0;
1590
1591out_free_asid:
1592 arm_smmu_bitmap_free(smmu->asid_map, asid);
1593 return ret;
1594}
1595
1596static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
1597 struct io_pgtable_cfg *pgtbl_cfg)
1598{
Will Deaconc0733a22015-10-13 17:51:14 +01001599 int vmid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001600 struct arm_smmu_device *smmu = smmu_domain->smmu;
1601 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1602
1603 vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001604 if (vmid < 0)
Will Deacon48ec83b2015-05-27 17:25:59 +01001605 return vmid;
1606
Will Deaconc0733a22015-10-13 17:51:14 +01001607 cfg->vmid = (u16)vmid;
Will Deacon48ec83b2015-05-27 17:25:59 +01001608 cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
1609 cfg->vtcr = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
1610 return 0;
1611}
1612
Will Deacon48ec83b2015-05-27 17:25:59 +01001613static int arm_smmu_domain_finalise(struct iommu_domain *domain)
1614{
1615 int ret;
1616 unsigned long ias, oas;
1617 enum io_pgtable_fmt fmt;
1618 struct io_pgtable_cfg pgtbl_cfg;
1619 struct io_pgtable_ops *pgtbl_ops;
1620 int (*finalise_stage_fn)(struct arm_smmu_domain *,
1621 struct io_pgtable_cfg *);
1622 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1623 struct arm_smmu_device *smmu = smmu_domain->smmu;
1624
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001625 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
1626 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
1627 return 0;
1628 }
1629
Will Deacon48ec83b2015-05-27 17:25:59 +01001630 /* Restrict the stage to what we can actually support */
1631 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
1632 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
1633 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
1634 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1635
1636 switch (smmu_domain->stage) {
1637 case ARM_SMMU_DOMAIN_S1:
1638 ias = VA_BITS;
1639 oas = smmu->ias;
1640 fmt = ARM_64_LPAE_S1;
1641 finalise_stage_fn = arm_smmu_domain_finalise_s1;
1642 break;
1643 case ARM_SMMU_DOMAIN_NESTED:
1644 case ARM_SMMU_DOMAIN_S2:
1645 ias = smmu->ias;
1646 oas = smmu->oas;
1647 fmt = ARM_64_LPAE_S2;
1648 finalise_stage_fn = arm_smmu_domain_finalise_s2;
1649 break;
1650 default:
1651 return -EINVAL;
1652 }
1653
1654 pgtbl_cfg = (struct io_pgtable_cfg) {
Robin Murphyd5466352016-05-09 17:20:09 +01001655 .pgsize_bitmap = smmu->pgsize_bitmap,
Will Deacon48ec83b2015-05-27 17:25:59 +01001656 .ias = ias,
1657 .oas = oas,
1658 .tlb = &arm_smmu_gather_ops,
Robin Murphybdc6d972015-07-29 19:46:07 +01001659 .iommu_dev = smmu->dev,
Will Deacon48ec83b2015-05-27 17:25:59 +01001660 };
1661
Robin Murphy81b3c252017-06-22 16:53:53 +01001662 if (smmu->features & ARM_SMMU_FEAT_COHERENCY)
1663 pgtbl_cfg.quirks = IO_PGTABLE_QUIRK_NO_DMA;
1664
Will Deacon48ec83b2015-05-27 17:25:59 +01001665 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
1666 if (!pgtbl_ops)
1667 return -ENOMEM;
1668
Robin Murphyd5466352016-05-09 17:20:09 +01001669 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
Robin Murphy455eb7d2016-09-12 17:13:58 +01001670 domain->geometry.aperture_end = (1UL << ias) - 1;
1671 domain->geometry.force_aperture = true;
Will Deacon48ec83b2015-05-27 17:25:59 +01001672 smmu_domain->pgtbl_ops = pgtbl_ops;
1673
1674 ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
Arnd Bergmann287980e2016-05-27 23:23:25 +02001675 if (ret < 0)
Will Deacon48ec83b2015-05-27 17:25:59 +01001676 free_io_pgtable_ops(pgtbl_ops);
1677
1678 return ret;
1679}
1680
Will Deacon48ec83b2015-05-27 17:25:59 +01001681static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
1682{
1683 __le64 *step;
1684 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1685
1686 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1687 struct arm_smmu_strtab_l1_desc *l1_desc;
1688 int idx;
1689
1690 /* Two-level walk */
1691 idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
1692 l1_desc = &cfg->l1_desc[idx];
1693 idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
1694 step = &l1_desc->l2ptr[idx];
1695 } else {
1696 /* Simple linear lookup */
1697 step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
1698 }
1699
1700 return step;
1701}
1702
Will Deacon67560ed2017-03-01 21:11:29 +00001703static void arm_smmu_install_ste_for_dev(struct iommu_fwspec *fwspec)
Will Deacon48ec83b2015-05-27 17:25:59 +01001704{
1705 int i;
Robin Murphy8f785152016-09-12 17:13:45 +01001706 struct arm_smmu_master_data *master = fwspec->iommu_priv;
1707 struct arm_smmu_device *smmu = master->smmu;
Will Deacon48ec83b2015-05-27 17:25:59 +01001708
Robin Murphy8f785152016-09-12 17:13:45 +01001709 for (i = 0; i < fwspec->num_ids; ++i) {
1710 u32 sid = fwspec->ids[i];
Will Deacon48ec83b2015-05-27 17:25:59 +01001711 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
1712
Robin Murphy8f785152016-09-12 17:13:45 +01001713 arm_smmu_write_strtab_ent(smmu, sid, step, &master->ste);
Will Deacon48ec83b2015-05-27 17:25:59 +01001714 }
Will Deacon48ec83b2015-05-27 17:25:59 +01001715}
1716
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001717static void arm_smmu_detach_dev(struct device *dev)
1718{
Robin Murphy8f785152016-09-12 17:13:45 +01001719 struct arm_smmu_master_data *master = dev->iommu_fwspec->iommu_priv;
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001720
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001721 master->ste.assigned = false;
Will Deacon67560ed2017-03-01 21:11:29 +00001722 arm_smmu_install_ste_for_dev(dev->iommu_fwspec);
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001723}
1724
Will Deacon48ec83b2015-05-27 17:25:59 +01001725static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1726{
1727 int ret = 0;
1728 struct arm_smmu_device *smmu;
1729 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
Robin Murphy8f785152016-09-12 17:13:45 +01001730 struct arm_smmu_master_data *master;
1731 struct arm_smmu_strtab_ent *ste;
Will Deacon48ec83b2015-05-27 17:25:59 +01001732
Robin Murphy8f785152016-09-12 17:13:45 +01001733 if (!dev->iommu_fwspec)
Will Deacon48ec83b2015-05-27 17:25:59 +01001734 return -ENOENT;
1735
Robin Murphy8f785152016-09-12 17:13:45 +01001736 master = dev->iommu_fwspec->iommu_priv;
1737 smmu = master->smmu;
1738 ste = &master->ste;
1739
Will Deacon48ec83b2015-05-27 17:25:59 +01001740 /* Already attached to a different domain? */
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001741 if (ste->assigned)
Will Deaconbc7f2ce2016-02-17 17:41:57 +00001742 arm_smmu_detach_dev(dev);
Will Deacon48ec83b2015-05-27 17:25:59 +01001743
Will Deacon48ec83b2015-05-27 17:25:59 +01001744 mutex_lock(&smmu_domain->init_mutex);
1745
1746 if (!smmu_domain->smmu) {
1747 smmu_domain->smmu = smmu;
1748 ret = arm_smmu_domain_finalise(domain);
1749 if (ret) {
1750 smmu_domain->smmu = NULL;
1751 goto out_unlock;
1752 }
1753 } else if (smmu_domain->smmu != smmu) {
1754 dev_err(dev,
1755 "cannot attach to SMMU %s (upstream of %s)\n",
1756 dev_name(smmu_domain->smmu->dev),
1757 dev_name(smmu->dev));
1758 ret = -ENXIO;
1759 goto out_unlock;
1760 }
1761
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001762 ste->assigned = true;
Will Deacon48ec83b2015-05-27 17:25:59 +01001763
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001764 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS) {
1765 ste->s1_cfg = NULL;
1766 ste->s2_cfg = NULL;
1767 } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
Robin Murphy8f785152016-09-12 17:13:45 +01001768 ste->s1_cfg = &smmu_domain->s1_cfg;
1769 ste->s2_cfg = NULL;
1770 arm_smmu_write_ctx_desc(smmu, ste->s1_cfg);
1771 } else {
1772 ste->s1_cfg = NULL;
1773 ste->s2_cfg = &smmu_domain->s2_cfg;
1774 }
Will Deaconcbf82772016-02-18 12:05:57 +00001775
Will Deacon67560ed2017-03-01 21:11:29 +00001776 arm_smmu_install_ste_for_dev(dev->iommu_fwspec);
Will Deacon48ec83b2015-05-27 17:25:59 +01001777out_unlock:
1778 mutex_unlock(&smmu_domain->init_mutex);
1779 return ret;
1780}
1781
Will Deacon48ec83b2015-05-27 17:25:59 +01001782static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1783 phys_addr_t paddr, size_t size, int prot)
1784{
Robin Murphy58188af2017-06-22 16:53:57 +01001785 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
Will Deacon48ec83b2015-05-27 17:25:59 +01001786
1787 if (!ops)
1788 return -ENODEV;
1789
Robin Murphy58188af2017-06-22 16:53:57 +01001790 return ops->map(ops, iova, paddr, size, prot);
Will Deacon48ec83b2015-05-27 17:25:59 +01001791}
1792
1793static size_t
1794arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1795{
Robin Murphy58188af2017-06-22 16:53:57 +01001796 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
Will Deacon48ec83b2015-05-27 17:25:59 +01001797
1798 if (!ops)
1799 return 0;
1800
Robin Murphy58188af2017-06-22 16:53:57 +01001801 return ops->unmap(ops, iova, size);
Will Deacon48ec83b2015-05-27 17:25:59 +01001802}
1803
1804static phys_addr_t
1805arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1806{
Robin Murphy58188af2017-06-22 16:53:57 +01001807 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
Will Deacon48ec83b2015-05-27 17:25:59 +01001808
Sunil Gouthambdf95922017-04-25 15:27:52 +05301809 if (domain->type == IOMMU_DOMAIN_IDENTITY)
1810 return iova;
1811
Will Deacon48ec83b2015-05-27 17:25:59 +01001812 if (!ops)
1813 return 0;
1814
Robin Murphy58188af2017-06-22 16:53:57 +01001815 return ops->iova_to_phys(ops, iova);
Will Deacon48ec83b2015-05-27 17:25:59 +01001816}
1817
Robin Murphy8f785152016-09-12 17:13:45 +01001818static struct platform_driver arm_smmu_driver;
1819
1820static int arm_smmu_match_node(struct device *dev, void *data)
Will Deacon48ec83b2015-05-27 17:25:59 +01001821{
Lorenzo Pieralisi778de072016-11-21 10:01:38 +00001822 return dev->fwnode == data;
Will Deacon48ec83b2015-05-27 17:25:59 +01001823}
1824
Lorenzo Pieralisi778de072016-11-21 10:01:38 +00001825static
1826struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
Will Deacon48ec83b2015-05-27 17:25:59 +01001827{
Robin Murphy8f785152016-09-12 17:13:45 +01001828 struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
Lorenzo Pieralisi778de072016-11-21 10:01:38 +00001829 fwnode, arm_smmu_match_node);
Robin Murphy8f785152016-09-12 17:13:45 +01001830 put_device(dev);
1831 return dev ? dev_get_drvdata(dev) : NULL;
Will Deacon48ec83b2015-05-27 17:25:59 +01001832}
1833
1834static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
1835{
1836 unsigned long limit = smmu->strtab_cfg.num_l1_ents;
1837
1838 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
1839 limit *= 1UL << STRTAB_SPLIT;
1840
1841 return sid < limit;
1842}
1843
Robin Murphy8f785152016-09-12 17:13:45 +01001844static struct iommu_ops arm_smmu_ops;
1845
Will Deacon48ec83b2015-05-27 17:25:59 +01001846static int arm_smmu_add_device(struct device *dev)
1847{
1848 int i, ret;
Will Deacon48ec83b2015-05-27 17:25:59 +01001849 struct arm_smmu_device *smmu;
Robin Murphy8f785152016-09-12 17:13:45 +01001850 struct arm_smmu_master_data *master;
1851 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1852 struct iommu_group *group;
Will Deacon48ec83b2015-05-27 17:25:59 +01001853
Robin Murphy8f785152016-09-12 17:13:45 +01001854 if (!fwspec || fwspec->ops != &arm_smmu_ops)
Will Deacon48ec83b2015-05-27 17:25:59 +01001855 return -ENODEV;
Robin Murphy8f785152016-09-12 17:13:45 +01001856 /*
1857 * We _can_ actually withstand dodgy bus code re-calling add_device()
1858 * without an intervening remove_device()/of_xlate() sequence, but
1859 * we're not going to do so quietly...
1860 */
1861 if (WARN_ON_ONCE(fwspec->iommu_priv)) {
1862 master = fwspec->iommu_priv;
1863 smmu = master->smmu;
Will Deacon48ec83b2015-05-27 17:25:59 +01001864 } else {
Lorenzo Pieralisi778de072016-11-21 10:01:38 +00001865 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
Robin Murphy8f785152016-09-12 17:13:45 +01001866 if (!smmu)
1867 return -ENODEV;
1868 master = kzalloc(sizeof(*master), GFP_KERNEL);
1869 if (!master)
1870 return -ENOMEM;
1871
1872 master->smmu = smmu;
1873 fwspec->iommu_priv = master;
Will Deacon48ec83b2015-05-27 17:25:59 +01001874 }
1875
Robin Murphy8f785152016-09-12 17:13:45 +01001876 /* Check the SIDs are in range of the SMMU and our stream table */
1877 for (i = 0; i < fwspec->num_ids; i++) {
1878 u32 sid = fwspec->ids[i];
1879
1880 if (!arm_smmu_sid_in_range(smmu, sid))
1881 return -ERANGE;
1882
1883 /* Ensure l2 strtab is initialised */
1884 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1885 ret = arm_smmu_init_l2_strtab(smmu, sid);
1886 if (ret)
1887 return ret;
1888 }
Will Deacon48ec83b2015-05-27 17:25:59 +01001889 }
1890
Robin Murphy8f785152016-09-12 17:13:45 +01001891 group = iommu_group_get_for_dev(dev);
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001892 if (!IS_ERR(group)) {
Robin Murphy8f785152016-09-12 17:13:45 +01001893 iommu_group_put(group);
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001894 iommu_device_link(&smmu->iommu, dev);
1895 }
Will Deacon48ec83b2015-05-27 17:25:59 +01001896
Robin Murphy8f785152016-09-12 17:13:45 +01001897 return PTR_ERR_OR_ZERO(group);
Will Deacon48ec83b2015-05-27 17:25:59 +01001898}
1899
1900static void arm_smmu_remove_device(struct device *dev)
1901{
Robin Murphy8f785152016-09-12 17:13:45 +01001902 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1903 struct arm_smmu_master_data *master;
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001904 struct arm_smmu_device *smmu;
Robin Murphy8f785152016-09-12 17:13:45 +01001905
1906 if (!fwspec || fwspec->ops != &arm_smmu_ops)
1907 return;
1908
1909 master = fwspec->iommu_priv;
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001910 smmu = master->smmu;
Will Deaconbeb3c6a2017-01-06 16:27:30 +00001911 if (master && master->ste.assigned)
Robin Murphy8f785152016-09-12 17:13:45 +01001912 arm_smmu_detach_dev(dev);
Will Deacon48ec83b2015-05-27 17:25:59 +01001913 iommu_group_remove_device(dev);
Joerg Roedel9648cbc2017-02-01 18:11:36 +01001914 iommu_device_unlink(&smmu->iommu, dev);
Robin Murphy8f785152016-09-12 17:13:45 +01001915 kfree(master);
1916 iommu_fwspec_free(dev);
Will Deacon48ec83b2015-05-27 17:25:59 +01001917}
1918
Robin Murphy08d4ca22016-09-12 17:13:46 +01001919static struct iommu_group *arm_smmu_device_group(struct device *dev)
1920{
1921 struct iommu_group *group;
1922
1923 /*
1924 * We don't support devices sharing stream IDs other than PCI RID
1925 * aliases, since the necessary ID-to-device lookup becomes rather
1926 * impractical given a potential sparse 32-bit stream ID space.
1927 */
1928 if (dev_is_pci(dev))
1929 group = pci_device_group(dev);
1930 else
1931 group = generic_device_group(dev);
1932
1933 return group;
1934}
1935
Will Deacon48ec83b2015-05-27 17:25:59 +01001936static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1937 enum iommu_attr attr, void *data)
1938{
1939 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1940
Will Deacon0834cc22017-01-06 16:28:17 +00001941 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
1942 return -EINVAL;
1943
Will Deacon48ec83b2015-05-27 17:25:59 +01001944 switch (attr) {
1945 case DOMAIN_ATTR_NESTING:
1946 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1947 return 0;
1948 default:
1949 return -ENODEV;
1950 }
1951}
1952
1953static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1954 enum iommu_attr attr, void *data)
1955{
1956 int ret = 0;
1957 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1958
Will Deacon0834cc22017-01-06 16:28:17 +00001959 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
1960 return -EINVAL;
1961
Will Deacon48ec83b2015-05-27 17:25:59 +01001962 mutex_lock(&smmu_domain->init_mutex);
1963
1964 switch (attr) {
1965 case DOMAIN_ATTR_NESTING:
1966 if (smmu_domain->smmu) {
1967 ret = -EPERM;
1968 goto out_unlock;
1969 }
1970
1971 if (*(int *)data)
1972 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1973 else
1974 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1975
1976 break;
1977 default:
1978 ret = -ENODEV;
1979 }
1980
1981out_unlock:
1982 mutex_unlock(&smmu_domain->init_mutex);
1983 return ret;
1984}
1985
Robin Murphy8f785152016-09-12 17:13:45 +01001986static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1987{
Robin Murphy8f785152016-09-12 17:13:45 +01001988 return iommu_fwspec_add_ids(dev, args->args, 1);
1989}
1990
Eric Auger50019f02017-01-19 20:57:56 +00001991static void arm_smmu_get_resv_regions(struct device *dev,
1992 struct list_head *head)
1993{
1994 struct iommu_resv_region *region;
1995 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1996
1997 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
Robin Murphy9d3a4de2017-03-16 17:00:16 +00001998 prot, IOMMU_RESV_SW_MSI);
Eric Auger50019f02017-01-19 20:57:56 +00001999 if (!region)
2000 return;
2001
2002 list_add_tail(&region->list, head);
Robin Murphy273df962017-03-16 17:00:19 +00002003
2004 iommu_dma_get_resv_regions(dev, head);
Eric Auger50019f02017-01-19 20:57:56 +00002005}
2006
2007static void arm_smmu_put_resv_regions(struct device *dev,
2008 struct list_head *head)
2009{
2010 struct iommu_resv_region *entry, *next;
2011
2012 list_for_each_entry_safe(entry, next, head, list)
2013 kfree(entry);
2014}
2015
Will Deacon48ec83b2015-05-27 17:25:59 +01002016static struct iommu_ops arm_smmu_ops = {
2017 .capable = arm_smmu_capable,
2018 .domain_alloc = arm_smmu_domain_alloc,
2019 .domain_free = arm_smmu_domain_free,
2020 .attach_dev = arm_smmu_attach_dev,
Will Deacon48ec83b2015-05-27 17:25:59 +01002021 .map = arm_smmu_map,
2022 .unmap = arm_smmu_unmap,
Jean-Philippe Brucker9aeb26c2016-06-03 11:50:30 +01002023 .map_sg = default_iommu_map_sg,
Will Deacon48ec83b2015-05-27 17:25:59 +01002024 .iova_to_phys = arm_smmu_iova_to_phys,
2025 .add_device = arm_smmu_add_device,
2026 .remove_device = arm_smmu_remove_device,
Robin Murphy08d4ca22016-09-12 17:13:46 +01002027 .device_group = arm_smmu_device_group,
Will Deacon48ec83b2015-05-27 17:25:59 +01002028 .domain_get_attr = arm_smmu_domain_get_attr,
2029 .domain_set_attr = arm_smmu_domain_set_attr,
Robin Murphy8f785152016-09-12 17:13:45 +01002030 .of_xlate = arm_smmu_of_xlate,
Eric Auger50019f02017-01-19 20:57:56 +00002031 .get_resv_regions = arm_smmu_get_resv_regions,
2032 .put_resv_regions = arm_smmu_put_resv_regions,
Will Deacon48ec83b2015-05-27 17:25:59 +01002033 .pgsize_bitmap = -1UL, /* Restricted during device attach */
2034};
2035
2036/* Probing and initialisation functions */
2037static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
2038 struct arm_smmu_queue *q,
2039 unsigned long prod_off,
2040 unsigned long cons_off,
2041 size_t dwords)
2042{
2043 size_t qsz = ((1 << q->max_n_shift) * dwords) << 3;
2044
Will Deacon04fa26c2015-10-30 18:12:41 +00002045 q->base = dmam_alloc_coherent(smmu->dev, qsz, &q->base_dma, GFP_KERNEL);
Will Deacon48ec83b2015-05-27 17:25:59 +01002046 if (!q->base) {
2047 dev_err(smmu->dev, "failed to allocate queue (0x%zx bytes)\n",
2048 qsz);
2049 return -ENOMEM;
2050 }
2051
Linu Cheriane5b829d2017-06-22 17:35:37 +05302052 q->prod_reg = arm_smmu_page1_fixup(prod_off, smmu);
2053 q->cons_reg = arm_smmu_page1_fixup(cons_off, smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002054 q->ent_dwords = dwords;
2055
2056 q->q_base = Q_BASE_RWA;
2057 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK << Q_BASE_ADDR_SHIFT;
2058 q->q_base |= (q->max_n_shift & Q_BASE_LOG2SIZE_MASK)
2059 << Q_BASE_LOG2SIZE_SHIFT;
2060
2061 q->prod = q->cons = 0;
2062 return 0;
2063}
2064
Will Deacon48ec83b2015-05-27 17:25:59 +01002065static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
2066{
2067 int ret;
2068
2069 /* cmdq */
2070 spin_lock_init(&smmu->cmdq.lock);
2071 ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
2072 ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS);
2073 if (ret)
Will Deacon04fa26c2015-10-30 18:12:41 +00002074 return ret;
Will Deacon48ec83b2015-05-27 17:25:59 +01002075
2076 /* evtq */
2077 ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
2078 ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS);
2079 if (ret)
Will Deacon04fa26c2015-10-30 18:12:41 +00002080 return ret;
Will Deacon48ec83b2015-05-27 17:25:59 +01002081
2082 /* priq */
2083 if (!(smmu->features & ARM_SMMU_FEAT_PRI))
2084 return 0;
2085
Will Deacon04fa26c2015-10-30 18:12:41 +00002086 return arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
2087 ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS);
Will Deacon48ec83b2015-05-27 17:25:59 +01002088}
2089
2090static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
2091{
2092 unsigned int i;
2093 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2094 size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
2095 void *strtab = smmu->strtab_cfg.strtab;
2096
2097 cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
2098 if (!cfg->l1_desc) {
2099 dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
2100 return -ENOMEM;
2101 }
2102
2103 for (i = 0; i < cfg->num_l1_ents; ++i) {
2104 arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
2105 strtab += STRTAB_L1_DESC_DWORDS << 3;
2106 }
2107
2108 return 0;
2109}
2110
2111static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
2112{
2113 void *strtab;
2114 u64 reg;
Will Deacond2e88e72015-06-30 10:02:28 +01002115 u32 size, l1size;
Will Deacon48ec83b2015-05-27 17:25:59 +01002116 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2117
Nate Watterson692c4e42017-01-10 14:47:13 -05002118 /* Calculate the L1 size, capped to the SIDSIZE. */
2119 size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
2120 size = min(size, smmu->sid_bits - STRTAB_SPLIT);
Will Deacond2e88e72015-06-30 10:02:28 +01002121 cfg->num_l1_ents = 1 << size;
2122
2123 size += STRTAB_SPLIT;
2124 if (size < smmu->sid_bits)
Will Deacon48ec83b2015-05-27 17:25:59 +01002125 dev_warn(smmu->dev,
2126 "2-level strtab only covers %u/%u bits of SID\n",
Will Deacond2e88e72015-06-30 10:02:28 +01002127 size, smmu->sid_bits);
Will Deacon48ec83b2015-05-27 17:25:59 +01002128
Will Deacond2e88e72015-06-30 10:02:28 +01002129 l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
Will Deacon04fa26c2015-10-30 18:12:41 +00002130 strtab = dmam_alloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
2131 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01002132 if (!strtab) {
2133 dev_err(smmu->dev,
2134 "failed to allocate l1 stream table (%u bytes)\n",
2135 size);
2136 return -ENOMEM;
2137 }
2138 cfg->strtab = strtab;
2139
2140 /* Configure strtab_base_cfg for 2 levels */
2141 reg = STRTAB_BASE_CFG_FMT_2LVL;
2142 reg |= (size & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2143 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2144 reg |= (STRTAB_SPLIT & STRTAB_BASE_CFG_SPLIT_MASK)
2145 << STRTAB_BASE_CFG_SPLIT_SHIFT;
2146 cfg->strtab_base_cfg = reg;
2147
Will Deacon04fa26c2015-10-30 18:12:41 +00002148 return arm_smmu_init_l1_strtab(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002149}
2150
2151static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
2152{
2153 void *strtab;
2154 u64 reg;
2155 u32 size;
2156 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2157
2158 size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
Will Deacon04fa26c2015-10-30 18:12:41 +00002159 strtab = dmam_alloc_coherent(smmu->dev, size, &cfg->strtab_dma,
2160 GFP_KERNEL | __GFP_ZERO);
Will Deacon48ec83b2015-05-27 17:25:59 +01002161 if (!strtab) {
2162 dev_err(smmu->dev,
2163 "failed to allocate linear stream table (%u bytes)\n",
2164 size);
2165 return -ENOMEM;
2166 }
2167 cfg->strtab = strtab;
2168 cfg->num_l1_ents = 1 << smmu->sid_bits;
2169
2170 /* Configure strtab_base_cfg for a linear table covering all SIDs */
2171 reg = STRTAB_BASE_CFG_FMT_LINEAR;
2172 reg |= (smmu->sid_bits & STRTAB_BASE_CFG_LOG2SIZE_MASK)
2173 << STRTAB_BASE_CFG_LOG2SIZE_SHIFT;
2174 cfg->strtab_base_cfg = reg;
2175
2176 arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
2177 return 0;
2178}
2179
2180static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
2181{
2182 u64 reg;
2183 int ret;
2184
2185 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
2186 ret = arm_smmu_init_strtab_2lvl(smmu);
2187 else
2188 ret = arm_smmu_init_strtab_linear(smmu);
2189
2190 if (ret)
2191 return ret;
2192
2193 /* Set the strtab base address */
2194 reg = smmu->strtab_cfg.strtab_dma &
2195 STRTAB_BASE_ADDR_MASK << STRTAB_BASE_ADDR_SHIFT;
2196 reg |= STRTAB_BASE_RA;
2197 smmu->strtab_cfg.strtab_base = reg;
2198
2199 /* Allocate the first VMID for stage-2 bypass STEs */
2200 set_bit(0, smmu->vmid_map);
2201 return 0;
2202}
2203
Will Deacon48ec83b2015-05-27 17:25:59 +01002204static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
2205{
2206 int ret;
2207
Robin Murphy37de98f2017-10-18 15:04:26 +01002208 atomic_set(&smmu->sync_nr, 0);
Will Deacon48ec83b2015-05-27 17:25:59 +01002209 ret = arm_smmu_init_queues(smmu);
2210 if (ret)
2211 return ret;
2212
Will Deacon04fa26c2015-10-30 18:12:41 +00002213 return arm_smmu_init_strtab(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002214}
2215
2216static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
2217 unsigned int reg_off, unsigned int ack_off)
2218{
2219 u32 reg;
2220
2221 writel_relaxed(val, smmu->base + reg_off);
2222 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
2223 1, ARM_SMMU_POLL_TIMEOUT_US);
2224}
2225
Robin Murphydc87a982016-09-12 17:13:44 +01002226/* GBPA is "special" */
2227static int arm_smmu_update_gbpa(struct arm_smmu_device *smmu, u32 set, u32 clr)
2228{
2229 int ret;
2230 u32 reg, __iomem *gbpa = smmu->base + ARM_SMMU_GBPA;
2231
2232 ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
2233 1, ARM_SMMU_POLL_TIMEOUT_US);
2234 if (ret)
2235 return ret;
2236
2237 reg &= ~clr;
2238 reg |= set;
2239 writel_relaxed(reg | GBPA_UPDATE, gbpa);
2240 return readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
2241 1, ARM_SMMU_POLL_TIMEOUT_US);
2242}
2243
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002244static void arm_smmu_free_msis(void *data)
2245{
2246 struct device *dev = data;
2247 platform_msi_domain_free_irqs(dev);
2248}
2249
2250static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
2251{
2252 phys_addr_t doorbell;
2253 struct device *dev = msi_desc_to_dev(desc);
2254 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2255 phys_addr_t *cfg = arm_smmu_msi_cfg[desc->platform.msi_index];
2256
2257 doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
2258 doorbell &= MSI_CFG0_ADDR_MASK << MSI_CFG0_ADDR_SHIFT;
2259
2260 writeq_relaxed(doorbell, smmu->base + cfg[0]);
2261 writel_relaxed(msg->data, smmu->base + cfg[1]);
2262 writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]);
2263}
2264
2265static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
2266{
2267 struct msi_desc *desc;
2268 int ret, nvec = ARM_SMMU_MAX_MSIS;
2269 struct device *dev = smmu->dev;
2270
2271 /* Clear the MSI address regs */
2272 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
2273 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
2274
2275 if (smmu->features & ARM_SMMU_FEAT_PRI)
2276 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
2277 else
2278 nvec--;
2279
2280 if (!(smmu->features & ARM_SMMU_FEAT_MSI))
2281 return;
2282
2283 /* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */
2284 ret = platform_msi_domain_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg);
2285 if (ret) {
2286 dev_warn(dev, "failed to allocate MSIs\n");
2287 return;
2288 }
2289
2290 for_each_msi_entry(desc, dev) {
2291 switch (desc->platform.msi_index) {
2292 case EVTQ_MSI_INDEX:
2293 smmu->evtq.q.irq = desc->irq;
2294 break;
2295 case GERROR_MSI_INDEX:
2296 smmu->gerr_irq = desc->irq;
2297 break;
2298 case PRIQ_MSI_INDEX:
2299 smmu->priq.q.irq = desc->irq;
2300 break;
2301 default: /* Unknown */
2302 continue;
2303 }
2304 }
2305
2306 /* Add callback to free MSIs on teardown */
2307 devm_add_action(dev, arm_smmu_free_msis, dev);
2308}
2309
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05302310static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
Will Deacon48ec83b2015-05-27 17:25:59 +01002311{
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05302312 int irq, ret;
Will Deacon48ec83b2015-05-27 17:25:59 +01002313
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002314 arm_smmu_setup_msis(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002315
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002316 /* Request interrupt lines */
Will Deacon48ec83b2015-05-27 17:25:59 +01002317 irq = smmu->evtq.q.irq;
2318 if (irq) {
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01002319 ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
Will Deacon48ec83b2015-05-27 17:25:59 +01002320 arm_smmu_evtq_thread,
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01002321 IRQF_ONESHOT,
2322 "arm-smmu-v3-evtq", smmu);
Arnd Bergmann287980e2016-05-27 23:23:25 +02002323 if (ret < 0)
Will Deacon48ec83b2015-05-27 17:25:59 +01002324 dev_warn(smmu->dev, "failed to enable evtq irq\n");
2325 }
2326
Will Deacon48ec83b2015-05-27 17:25:59 +01002327 irq = smmu->gerr_irq;
2328 if (irq) {
2329 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
2330 0, "arm-smmu-v3-gerror", smmu);
Arnd Bergmann287980e2016-05-27 23:23:25 +02002331 if (ret < 0)
Will Deacon48ec83b2015-05-27 17:25:59 +01002332 dev_warn(smmu->dev, "failed to enable gerror irq\n");
2333 }
2334
2335 if (smmu->features & ARM_SMMU_FEAT_PRI) {
Will Deacon48ec83b2015-05-27 17:25:59 +01002336 irq = smmu->priq.q.irq;
2337 if (irq) {
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01002338 ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
Will Deacon48ec83b2015-05-27 17:25:59 +01002339 arm_smmu_priq_thread,
Jean-Philippe Bruckerb4163fb2016-08-22 14:42:24 +01002340 IRQF_ONESHOT,
2341 "arm-smmu-v3-priq",
Will Deacon48ec83b2015-05-27 17:25:59 +01002342 smmu);
Arnd Bergmann287980e2016-05-27 23:23:25 +02002343 if (ret < 0)
Will Deacon48ec83b2015-05-27 17:25:59 +01002344 dev_warn(smmu->dev,
2345 "failed to enable priq irq\n");
2346 }
2347 }
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05302348}
2349
2350static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
2351{
2352 int ret, irq;
2353 u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
2354
2355 /* Disable IRQs first */
2356 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
2357 ARM_SMMU_IRQ_CTRLACK);
2358 if (ret) {
2359 dev_err(smmu->dev, "failed to disable irqs\n");
2360 return ret;
2361 }
2362
2363 irq = smmu->combined_irq;
2364 if (irq) {
2365 /*
2366 * Cavium ThunderX2 implementation doesn't not support unique
2367 * irq lines. Use single irq line for all the SMMUv3 interrupts.
2368 */
2369 ret = devm_request_threaded_irq(smmu->dev, irq,
2370 arm_smmu_combined_irq_handler,
2371 arm_smmu_combined_irq_thread,
2372 IRQF_ONESHOT,
2373 "arm-smmu-v3-combined-irq", smmu);
2374 if (ret < 0)
2375 dev_warn(smmu->dev, "failed to enable combined irq\n");
2376 } else
2377 arm_smmu_setup_unique_irqs(smmu);
2378
2379 if (smmu->features & ARM_SMMU_FEAT_PRI)
2380 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
Will Deacon48ec83b2015-05-27 17:25:59 +01002381
2382 /* Enable interrupt generation on the SMMU */
Marc Zyngierccd63852015-07-15 11:55:18 +01002383 ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
Will Deacon48ec83b2015-05-27 17:25:59 +01002384 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
2385 if (ret)
2386 dev_warn(smmu->dev, "failed to enable irqs\n");
2387
2388 return 0;
2389}
2390
2391static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
2392{
2393 int ret;
2394
2395 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
2396 if (ret)
2397 dev_err(smmu->dev, "failed to clear cr0\n");
2398
2399 return ret;
2400}
2401
Robin Murphydc87a982016-09-12 17:13:44 +01002402static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)
Will Deacon48ec83b2015-05-27 17:25:59 +01002403{
2404 int ret;
2405 u32 reg, enables;
2406 struct arm_smmu_cmdq_ent cmd;
2407
2408 /* Clear CR0 and sync (disables SMMU and queue processing) */
2409 reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
2410 if (reg & CR0_SMMUEN)
2411 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
2412
2413 ret = arm_smmu_device_disable(smmu);
2414 if (ret)
2415 return ret;
2416
2417 /* CR1 (table and queue memory attributes) */
2418 reg = (CR1_SH_ISH << CR1_TABLE_SH_SHIFT) |
2419 (CR1_CACHE_WB << CR1_TABLE_OC_SHIFT) |
2420 (CR1_CACHE_WB << CR1_TABLE_IC_SHIFT) |
2421 (CR1_SH_ISH << CR1_QUEUE_SH_SHIFT) |
2422 (CR1_CACHE_WB << CR1_QUEUE_OC_SHIFT) |
2423 (CR1_CACHE_WB << CR1_QUEUE_IC_SHIFT);
2424 writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
2425
2426 /* CR2 (random crap) */
2427 reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
2428 writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
2429
2430 /* Stream table */
2431 writeq_relaxed(smmu->strtab_cfg.strtab_base,
2432 smmu->base + ARM_SMMU_STRTAB_BASE);
2433 writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
2434 smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
2435
2436 /* Command queue */
2437 writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
2438 writel_relaxed(smmu->cmdq.q.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
2439 writel_relaxed(smmu->cmdq.q.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
2440
2441 enables = CR0_CMDQEN;
2442 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2443 ARM_SMMU_CR0ACK);
2444 if (ret) {
2445 dev_err(smmu->dev, "failed to enable command queue\n");
2446 return ret;
2447 }
2448
2449 /* Invalidate any cached configuration */
2450 cmd.opcode = CMDQ_OP_CFGI_ALL;
2451 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
Robin Murphy2f657ad2017-08-31 14:44:25 +01002452 arm_smmu_cmdq_issue_sync(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002453
2454 /* Invalidate any stale TLB entries */
2455 if (smmu->features & ARM_SMMU_FEAT_HYP) {
2456 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
2457 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2458 }
2459
2460 cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
2461 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
Robin Murphy2f657ad2017-08-31 14:44:25 +01002462 arm_smmu_cmdq_issue_sync(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002463
2464 /* Event queue */
2465 writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
Linu Cheriane5b829d2017-06-22 17:35:37 +05302466 writel_relaxed(smmu->evtq.q.prod,
2467 arm_smmu_page1_fixup(ARM_SMMU_EVTQ_PROD, smmu));
2468 writel_relaxed(smmu->evtq.q.cons,
2469 arm_smmu_page1_fixup(ARM_SMMU_EVTQ_CONS, smmu));
Will Deacon48ec83b2015-05-27 17:25:59 +01002470
2471 enables |= CR0_EVTQEN;
2472 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2473 ARM_SMMU_CR0ACK);
2474 if (ret) {
2475 dev_err(smmu->dev, "failed to enable event queue\n");
2476 return ret;
2477 }
2478
2479 /* PRI queue */
2480 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2481 writeq_relaxed(smmu->priq.q.q_base,
2482 smmu->base + ARM_SMMU_PRIQ_BASE);
2483 writel_relaxed(smmu->priq.q.prod,
Linu Cheriane5b829d2017-06-22 17:35:37 +05302484 arm_smmu_page1_fixup(ARM_SMMU_PRIQ_PROD, smmu));
Will Deacon48ec83b2015-05-27 17:25:59 +01002485 writel_relaxed(smmu->priq.q.cons,
Linu Cheriane5b829d2017-06-22 17:35:37 +05302486 arm_smmu_page1_fixup(ARM_SMMU_PRIQ_CONS, smmu));
Will Deacon48ec83b2015-05-27 17:25:59 +01002487
2488 enables |= CR0_PRIQEN;
2489 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2490 ARM_SMMU_CR0ACK);
2491 if (ret) {
2492 dev_err(smmu->dev, "failed to enable PRI queue\n");
2493 return ret;
2494 }
2495 }
2496
2497 ret = arm_smmu_setup_irqs(smmu);
2498 if (ret) {
2499 dev_err(smmu->dev, "failed to setup irqs\n");
2500 return ret;
2501 }
2502
Robin Murphydc87a982016-09-12 17:13:44 +01002503
2504 /* Enable the SMMU interface, or ensure bypass */
2505 if (!bypass || disable_bypass) {
2506 enables |= CR0_SMMUEN;
2507 } else {
2508 ret = arm_smmu_update_gbpa(smmu, 0, GBPA_ABORT);
2509 if (ret) {
2510 dev_err(smmu->dev, "GBPA not responding to update\n");
2511 return ret;
2512 }
2513 }
Will Deacon48ec83b2015-05-27 17:25:59 +01002514 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2515 ARM_SMMU_CR0ACK);
2516 if (ret) {
2517 dev_err(smmu->dev, "failed to enable SMMU interface\n");
2518 return ret;
2519 }
2520
2521 return 0;
2522}
2523
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002524static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
Will Deacon48ec83b2015-05-27 17:25:59 +01002525{
2526 u32 reg;
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002527 bool coherent = smmu->features & ARM_SMMU_FEAT_COHERENCY;
Will Deacon48ec83b2015-05-27 17:25:59 +01002528
2529 /* IDR0 */
2530 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
2531
2532 /* 2-level structures */
2533 if ((reg & IDR0_ST_LVL_MASK << IDR0_ST_LVL_SHIFT) == IDR0_ST_LVL_2LVL)
2534 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
2535
2536 if (reg & IDR0_CD2L)
2537 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
2538
2539 /*
2540 * Translation table endianness.
2541 * We currently require the same endianness as the CPU, but this
2542 * could be changed later by adding a new IO_PGTABLE_QUIRK.
2543 */
2544 switch (reg & IDR0_TTENDIAN_MASK << IDR0_TTENDIAN_SHIFT) {
2545 case IDR0_TTENDIAN_MIXED:
2546 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
2547 break;
2548#ifdef __BIG_ENDIAN
2549 case IDR0_TTENDIAN_BE:
2550 smmu->features |= ARM_SMMU_FEAT_TT_BE;
2551 break;
2552#else
2553 case IDR0_TTENDIAN_LE:
2554 smmu->features |= ARM_SMMU_FEAT_TT_LE;
2555 break;
2556#endif
2557 default:
2558 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
2559 return -ENXIO;
2560 }
2561
2562 /* Boolean feature flags */
2563 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
2564 smmu->features |= ARM_SMMU_FEAT_PRI;
2565
2566 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
2567 smmu->features |= ARM_SMMU_FEAT_ATS;
2568
2569 if (reg & IDR0_SEV)
2570 smmu->features |= ARM_SMMU_FEAT_SEV;
2571
2572 if (reg & IDR0_MSI)
2573 smmu->features |= ARM_SMMU_FEAT_MSI;
2574
2575 if (reg & IDR0_HYP)
2576 smmu->features |= ARM_SMMU_FEAT_HYP;
2577
2578 /*
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002579 * The coherency feature as set by FW is used in preference to the ID
Will Deacon48ec83b2015-05-27 17:25:59 +01002580 * register, but warn on mismatch.
2581 */
Will Deacon48ec83b2015-05-27 17:25:59 +01002582 if (!!(reg & IDR0_COHACC) != coherent)
Robin Murphy2a22baa2017-09-25 14:55:40 +01002583 dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n",
Will Deacon48ec83b2015-05-27 17:25:59 +01002584 coherent ? "true" : "false");
2585
Prem Mallappa6380be02015-12-14 22:01:23 +05302586 switch (reg & IDR0_STALL_MODEL_MASK << IDR0_STALL_MODEL_SHIFT) {
Prem Mallappa6380be02015-12-14 22:01:23 +05302587 case IDR0_STALL_MODEL_FORCE:
Yisheng Xie9cff86fd22017-09-21 20:36:07 +08002588 smmu->features |= ARM_SMMU_FEAT_STALL_FORCE;
2589 /* Fallthrough */
2590 case IDR0_STALL_MODEL_STALL:
Will Deacon48ec83b2015-05-27 17:25:59 +01002591 smmu->features |= ARM_SMMU_FEAT_STALLS;
Prem Mallappa6380be02015-12-14 22:01:23 +05302592 }
Will Deacon48ec83b2015-05-27 17:25:59 +01002593
2594 if (reg & IDR0_S1P)
2595 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
2596
2597 if (reg & IDR0_S2P)
2598 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
2599
2600 if (!(reg & (IDR0_S1P | IDR0_S2P))) {
2601 dev_err(smmu->dev, "no translation support!\n");
2602 return -ENXIO;
2603 }
2604
2605 /* We only support the AArch64 table format at present */
Will Deaconf0c453d2015-08-20 12:12:32 +01002606 switch (reg & IDR0_TTF_MASK << IDR0_TTF_SHIFT) {
2607 case IDR0_TTF_AARCH32_64:
2608 smmu->ias = 40;
2609 /* Fallthrough */
2610 case IDR0_TTF_AARCH64:
2611 break;
2612 default:
Will Deacon48ec83b2015-05-27 17:25:59 +01002613 dev_err(smmu->dev, "AArch64 table format not supported!\n");
2614 return -ENXIO;
2615 }
2616
2617 /* ASID/VMID sizes */
2618 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
2619 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
2620
2621 /* IDR1 */
2622 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
2623 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
2624 dev_err(smmu->dev, "embedded implementation not supported\n");
2625 return -ENXIO;
2626 }
2627
2628 /* Queue sizes, capped at 4k */
2629 smmu->cmdq.q.max_n_shift = min((u32)CMDQ_MAX_SZ_SHIFT,
2630 reg >> IDR1_CMDQ_SHIFT & IDR1_CMDQ_MASK);
2631 if (!smmu->cmdq.q.max_n_shift) {
2632 /* Odd alignment restrictions on the base, so ignore for now */
2633 dev_err(smmu->dev, "unit-length command queue not supported\n");
2634 return -ENXIO;
2635 }
2636
2637 smmu->evtq.q.max_n_shift = min((u32)EVTQ_MAX_SZ_SHIFT,
2638 reg >> IDR1_EVTQ_SHIFT & IDR1_EVTQ_MASK);
2639 smmu->priq.q.max_n_shift = min((u32)PRIQ_MAX_SZ_SHIFT,
2640 reg >> IDR1_PRIQ_SHIFT & IDR1_PRIQ_MASK);
2641
2642 /* SID/SSID sizes */
2643 smmu->ssid_bits = reg >> IDR1_SSID_SHIFT & IDR1_SSID_MASK;
2644 smmu->sid_bits = reg >> IDR1_SID_SHIFT & IDR1_SID_MASK;
2645
Nate Watterson692c4e42017-01-10 14:47:13 -05002646 /*
2647 * If the SMMU supports fewer bits than would fill a single L2 stream
2648 * table, use a linear table instead.
2649 */
2650 if (smmu->sid_bits <= STRTAB_SPLIT)
2651 smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB;
2652
Will Deacon48ec83b2015-05-27 17:25:59 +01002653 /* IDR5 */
2654 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
2655
2656 /* Maximum number of outstanding stalls */
2657 smmu->evtq.max_stalls = reg >> IDR5_STALL_MAX_SHIFT
2658 & IDR5_STALL_MAX_MASK;
2659
2660 /* Page sizes */
2661 if (reg & IDR5_GRAN64K)
Robin Murphyd5466352016-05-09 17:20:09 +01002662 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
Will Deacon48ec83b2015-05-27 17:25:59 +01002663 if (reg & IDR5_GRAN16K)
Robin Murphyd5466352016-05-09 17:20:09 +01002664 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
Will Deacon48ec83b2015-05-27 17:25:59 +01002665 if (reg & IDR5_GRAN4K)
Robin Murphyd5466352016-05-09 17:20:09 +01002666 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
Will Deacon48ec83b2015-05-27 17:25:59 +01002667
Robin Murphyd5466352016-05-09 17:20:09 +01002668 if (arm_smmu_ops.pgsize_bitmap == -1UL)
2669 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
2670 else
2671 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
Will Deacon48ec83b2015-05-27 17:25:59 +01002672
2673 /* Output address size */
2674 switch (reg & IDR5_OAS_MASK << IDR5_OAS_SHIFT) {
2675 case IDR5_OAS_32_BIT:
2676 smmu->oas = 32;
2677 break;
2678 case IDR5_OAS_36_BIT:
2679 smmu->oas = 36;
2680 break;
2681 case IDR5_OAS_40_BIT:
2682 smmu->oas = 40;
2683 break;
2684 case IDR5_OAS_42_BIT:
2685 smmu->oas = 42;
2686 break;
2687 case IDR5_OAS_44_BIT:
2688 smmu->oas = 44;
2689 break;
Will Deacon85430962015-08-03 10:35:40 +01002690 default:
2691 dev_info(smmu->dev,
2692 "unknown output address size. Truncating to 48-bit\n");
2693 /* Fallthrough */
Will Deacon48ec83b2015-05-27 17:25:59 +01002694 case IDR5_OAS_48_BIT:
2695 smmu->oas = 48;
Will Deacon48ec83b2015-05-27 17:25:59 +01002696 }
2697
2698 /* Set the DMA mask for our table walker */
2699 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
2700 dev_warn(smmu->dev,
2701 "failed to set DMA mask for table walker\n");
2702
Will Deaconf0c453d2015-08-20 12:12:32 +01002703 smmu->ias = max(smmu->ias, smmu->oas);
Will Deacon48ec83b2015-05-27 17:25:59 +01002704
2705 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
2706 smmu->ias, smmu->oas, smmu->features);
2707 return 0;
2708}
2709
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00002710#ifdef CONFIG_ACPI
Linu Cheriane5b829d2017-06-22 17:35:37 +05302711static void acpi_smmu_get_options(u32 model, struct arm_smmu_device *smmu)
2712{
shameer99caf172017-05-17 10:12:05 +01002713 switch (model) {
2714 case ACPI_IORT_SMMU_V3_CAVIUM_CN99XX:
Linu Cheriane5b829d2017-06-22 17:35:37 +05302715 smmu->options |= ARM_SMMU_OPT_PAGE0_REGS_ONLY;
shameer99caf172017-05-17 10:12:05 +01002716 break;
Robin Murphy6948d4a2017-09-22 15:04:00 +01002717 case ACPI_IORT_SMMU_V3_HISILICON_HI161X:
shameer99caf172017-05-17 10:12:05 +01002718 smmu->options |= ARM_SMMU_OPT_SKIP_PREFETCH;
2719 break;
2720 }
Linu Cheriane5b829d2017-06-22 17:35:37 +05302721
2722 dev_notice(smmu->dev, "option mask 0x%x\n", smmu->options);
2723}
2724
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00002725static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2726 struct arm_smmu_device *smmu)
2727{
2728 struct acpi_iort_smmu_v3 *iort_smmu;
2729 struct device *dev = smmu->dev;
2730 struct acpi_iort_node *node;
2731
2732 node = *(struct acpi_iort_node **)dev_get_platdata(dev);
2733
2734 /* Retrieve SMMUv3 specific data */
2735 iort_smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
2736
Linu Cheriane5b829d2017-06-22 17:35:37 +05302737 acpi_smmu_get_options(iort_smmu->model, smmu);
2738
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00002739 if (iort_smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE)
2740 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2741
2742 return 0;
2743}
2744#else
2745static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2746 struct arm_smmu_device *smmu)
2747{
2748 return -ENODEV;
2749}
2750#endif
2751
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002752static int arm_smmu_device_dt_probe(struct platform_device *pdev,
2753 struct arm_smmu_device *smmu)
Will Deacon48ec83b2015-05-27 17:25:59 +01002754{
Will Deacon48ec83b2015-05-27 17:25:59 +01002755 struct device *dev = &pdev->dev;
Robin Murphydc87a982016-09-12 17:13:44 +01002756 u32 cells;
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002757 int ret = -EINVAL;
Robin Murphydc87a982016-09-12 17:13:44 +01002758
2759 if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells))
2760 dev_err(dev, "missing #iommu-cells property\n");
2761 else if (cells != 1)
2762 dev_err(dev, "invalid #iommu-cells value (%d)\n", cells);
2763 else
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002764 ret = 0;
2765
2766 parse_driver_options(smmu);
2767
2768 if (of_dma_is_coherent(dev->of_node))
2769 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2770
2771 return ret;
2772}
2773
Linu Cheriane5b829d2017-06-22 17:35:37 +05302774static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)
2775{
2776 if (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY)
2777 return SZ_64K;
2778 else
2779 return SZ_128K;
2780}
2781
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002782static int arm_smmu_device_probe(struct platform_device *pdev)
2783{
2784 int irq, ret;
2785 struct resource *res;
Joerg Roedel9648cbc2017-02-01 18:11:36 +01002786 resource_size_t ioaddr;
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002787 struct arm_smmu_device *smmu;
2788 struct device *dev = &pdev->dev;
2789 bool bypass;
Will Deacon48ec83b2015-05-27 17:25:59 +01002790
2791 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2792 if (!smmu) {
2793 dev_err(dev, "failed to allocate arm_smmu_device\n");
2794 return -ENOMEM;
2795 }
2796 smmu->dev = dev;
2797
Linu Cheriane5b829d2017-06-22 17:35:37 +05302798 if (dev->of_node) {
2799 ret = arm_smmu_device_dt_probe(pdev, smmu);
2800 } else {
2801 ret = arm_smmu_device_acpi_probe(pdev, smmu);
2802 if (ret == -ENODEV)
2803 return ret;
2804 }
2805
2806 /* Set bypass mode according to firmware probing result */
2807 bypass = !!ret;
2808
Will Deacon48ec83b2015-05-27 17:25:59 +01002809 /* Base address */
2810 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Linu Cheriane5b829d2017-06-22 17:35:37 +05302811 if (resource_size(res) + 1 < arm_smmu_resource_size(smmu)) {
Will Deacon48ec83b2015-05-27 17:25:59 +01002812 dev_err(dev, "MMIO region too small (%pr)\n", res);
2813 return -EINVAL;
2814 }
Joerg Roedel9648cbc2017-02-01 18:11:36 +01002815 ioaddr = res->start;
Will Deacon48ec83b2015-05-27 17:25:59 +01002816
2817 smmu->base = devm_ioremap_resource(dev, res);
2818 if (IS_ERR(smmu->base))
2819 return PTR_ERR(smmu->base);
2820
2821 /* Interrupt lines */
Will Deacon48ec83b2015-05-27 17:25:59 +01002822
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05302823 irq = platform_get_irq_byname(pdev, "combined");
Will Deacon48ec83b2015-05-27 17:25:59 +01002824 if (irq > 0)
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05302825 smmu->combined_irq = irq;
2826 else {
2827 irq = platform_get_irq_byname(pdev, "eventq");
2828 if (irq > 0)
2829 smmu->evtq.q.irq = irq;
Will Deacon48ec83b2015-05-27 17:25:59 +01002830
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05302831 irq = platform_get_irq_byname(pdev, "priq");
2832 if (irq > 0)
2833 smmu->priq.q.irq = irq;
Will Deacon48ec83b2015-05-27 17:25:59 +01002834
Geetha Sowjanyaf9354482017-06-23 19:04:36 +05302835 irq = platform_get_irq_byname(pdev, "gerror");
2836 if (irq > 0)
2837 smmu->gerr_irq = irq;
2838 }
Will Deacon48ec83b2015-05-27 17:25:59 +01002839 /* Probe the h/w */
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002840 ret = arm_smmu_device_hw_probe(smmu);
Will Deacon48ec83b2015-05-27 17:25:59 +01002841 if (ret)
2842 return ret;
2843
2844 /* Initialise in-memory data structures */
2845 ret = arm_smmu_init_structures(smmu);
2846 if (ret)
2847 return ret;
2848
Marc Zyngier166bdbd2015-10-13 18:32:30 +01002849 /* Record our private device structure */
2850 platform_set_drvdata(pdev, smmu);
2851
Will Deacon48ec83b2015-05-27 17:25:59 +01002852 /* Reset the device */
Robin Murphy8f785152016-09-12 17:13:45 +01002853 ret = arm_smmu_device_reset(smmu, bypass);
2854 if (ret)
2855 return ret;
2856
2857 /* And we're up. Go go go! */
Joerg Roedel9648cbc2017-02-01 18:11:36 +01002858 ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL,
2859 "smmu3.%pa", &ioaddr);
Robin Murphy08d4ca22016-09-12 17:13:46 +01002860 if (ret)
2861 return ret;
Joerg Roedel9648cbc2017-02-01 18:11:36 +01002862
2863 iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2864 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2865
2866 ret = iommu_device_register(&smmu->iommu);
Arvind Yadav5c2d0212017-06-22 12:57:42 +05302867 if (ret) {
2868 dev_err(dev, "Failed to register iommu\n");
2869 return ret;
2870 }
Lorenzo Pieralisi778de072016-11-21 10:01:38 +00002871
Robin Murphy8f785152016-09-12 17:13:45 +01002872#ifdef CONFIG_PCI
Robin Murphyec615f42016-11-03 17:39:07 +00002873 if (pci_bus_type.iommu_ops != &arm_smmu_ops) {
2874 pci_request_acs();
2875 ret = bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2876 if (ret)
2877 return ret;
2878 }
Robin Murphy08d4ca22016-09-12 17:13:46 +01002879#endif
2880#ifdef CONFIG_ARM_AMBA
Robin Murphyec615f42016-11-03 17:39:07 +00002881 if (amba_bustype.iommu_ops != &arm_smmu_ops) {
2882 ret = bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2883 if (ret)
2884 return ret;
2885 }
Robin Murphy08d4ca22016-09-12 17:13:46 +01002886#endif
Robin Murphyec615f42016-11-03 17:39:07 +00002887 if (platform_bus_type.iommu_ops != &arm_smmu_ops) {
2888 ret = bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2889 if (ret)
2890 return ret;
2891 }
2892 return 0;
Will Deacon48ec83b2015-05-27 17:25:59 +01002893}
2894
2895static int arm_smmu_device_remove(struct platform_device *pdev)
2896{
Will Deacon941a8022015-08-11 16:25:10 +01002897 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
Will Deacon48ec83b2015-05-27 17:25:59 +01002898
2899 arm_smmu_device_disable(smmu);
Nate Watterson7aa86192017-06-29 18:18:15 -04002900
Will Deacon48ec83b2015-05-27 17:25:59 +01002901 return 0;
2902}
2903
Nate Watterson7aa86192017-06-29 18:18:15 -04002904static void arm_smmu_device_shutdown(struct platform_device *pdev)
2905{
2906 arm_smmu_device_remove(pdev);
2907}
2908
Arvind Yadavebdd13c2017-06-22 12:51:00 +05302909static const struct of_device_id arm_smmu_of_match[] = {
Will Deacon48ec83b2015-05-27 17:25:59 +01002910 { .compatible = "arm,smmu-v3", },
2911 { },
2912};
2913MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2914
2915static struct platform_driver arm_smmu_driver = {
2916 .driver = {
2917 .name = "arm-smmu-v3",
2918 .of_match_table = of_match_ptr(arm_smmu_of_match),
2919 },
Lorenzo Pieralisi2985b522016-11-21 10:01:42 +00002920 .probe = arm_smmu_device_probe,
Will Deacon48ec83b2015-05-27 17:25:59 +01002921 .remove = arm_smmu_device_remove,
Nate Watterson7aa86192017-06-29 18:18:15 -04002922 .shutdown = arm_smmu_device_shutdown,
Will Deacon48ec83b2015-05-27 17:25:59 +01002923};
Robin Murphyf6810c12017-04-10 16:51:05 +05302924module_platform_driver(arm_smmu_driver);
Will Deacon48ec83b2015-05-27 17:25:59 +01002925
Robin Murphyf6810c12017-04-10 16:51:05 +05302926IOMMU_OF_DECLARE(arm_smmuv3, "arm,smmu-v3", NULL);
Lorenzo Pieralisie4dadfa2016-11-21 10:01:43 +00002927
Will Deacon48ec83b2015-05-27 17:25:59 +01002928MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
2929MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2930MODULE_LICENSE("GPL v2");