blob: 04d3ceeeaa37a9c1b765f11a5c85a8bbef423b7d [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * IOMMU API for ARM architected SMMUv3 implementations.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Copyright (C) 2015 ARM Limited
17 *
18 * Author: Will Deacon <will.deacon@arm.com>
19 *
20 * This driver is powered by bad coffee and bombay mix.
21 */
22
23#include <linux/acpi.h>
24#include <linux/acpi_iort.h>
25#include <linux/bitfield.h>
26#include <linux/bitops.h>
27#include <linux/crash_dump.h>
28#include <linux/delay.h>
29#include <linux/dma-iommu.h>
30#include <linux/err.h>
31#include <linux/interrupt.h>
32#include <linux/io-pgtable.h>
33#include <linux/iommu.h>
34#include <linux/iopoll.h>
35#include <linux/module.h>
36#include <linux/msi.h>
37#include <linux/of.h>
38#include <linux/of_address.h>
39#include <linux/of_iommu.h>
40#include <linux/of_platform.h>
41#include <linux/pci.h>
42#include <linux/platform_device.h>
43
44#include <linux/amba/bus.h>
45
46/* MMIO registers */
47#define ARM_SMMU_IDR0 0x0
48#define IDR0_ST_LVL GENMASK(28, 27)
49#define IDR0_ST_LVL_2LVL 1
50#define IDR0_STALL_MODEL GENMASK(25, 24)
51#define IDR0_STALL_MODEL_STALL 0
52#define IDR0_STALL_MODEL_FORCE 2
53#define IDR0_TTENDIAN GENMASK(22, 21)
54#define IDR0_TTENDIAN_MIXED 0
55#define IDR0_TTENDIAN_LE 2
56#define IDR0_TTENDIAN_BE 3
57#define IDR0_CD2L (1 << 19)
58#define IDR0_VMID16 (1 << 18)
59#define IDR0_PRI (1 << 16)
60#define IDR0_SEV (1 << 14)
61#define IDR0_MSI (1 << 13)
62#define IDR0_ASID16 (1 << 12)
63#define IDR0_ATS (1 << 10)
64#define IDR0_HYP (1 << 9)
65#define IDR0_COHACC (1 << 4)
66#define IDR0_TTF GENMASK(3, 2)
67#define IDR0_TTF_AARCH64 2
68#define IDR0_TTF_AARCH32_64 3
69#define IDR0_S1P (1 << 1)
70#define IDR0_S2P (1 << 0)
71
72#define ARM_SMMU_IDR1 0x4
73#define IDR1_TABLES_PRESET (1 << 30)
74#define IDR1_QUEUES_PRESET (1 << 29)
75#define IDR1_REL (1 << 28)
76#define IDR1_CMDQS GENMASK(25, 21)
77#define IDR1_EVTQS GENMASK(20, 16)
78#define IDR1_PRIQS GENMASK(15, 11)
79#define IDR1_SSIDSIZE GENMASK(10, 6)
80#define IDR1_SIDSIZE GENMASK(5, 0)
81
82#define ARM_SMMU_IDR5 0x14
83#define IDR5_STALL_MAX GENMASK(31, 16)
84#define IDR5_GRAN64K (1 << 6)
85#define IDR5_GRAN16K (1 << 5)
86#define IDR5_GRAN4K (1 << 4)
87#define IDR5_OAS GENMASK(2, 0)
88#define IDR5_OAS_32_BIT 0
89#define IDR5_OAS_36_BIT 1
90#define IDR5_OAS_40_BIT 2
91#define IDR5_OAS_42_BIT 3
92#define IDR5_OAS_44_BIT 4
93#define IDR5_OAS_48_BIT 5
94#define IDR5_OAS_52_BIT 6
95#define IDR5_VAX GENMASK(11, 10)
96#define IDR5_VAX_52_BIT 1
97
98#define ARM_SMMU_CR0 0x20
99#define CR0_CMDQEN (1 << 3)
100#define CR0_EVTQEN (1 << 2)
101#define CR0_PRIQEN (1 << 1)
102#define CR0_SMMUEN (1 << 0)
103
104#define ARM_SMMU_CR0ACK 0x24
105
106#define ARM_SMMU_CR1 0x28
107#define CR1_TABLE_SH GENMASK(11, 10)
108#define CR1_TABLE_OC GENMASK(9, 8)
109#define CR1_TABLE_IC GENMASK(7, 6)
110#define CR1_QUEUE_SH GENMASK(5, 4)
111#define CR1_QUEUE_OC GENMASK(3, 2)
112#define CR1_QUEUE_IC GENMASK(1, 0)
113/* CR1 cacheability fields don't quite follow the usual TCR-style encoding */
114#define CR1_CACHE_NC 0
115#define CR1_CACHE_WB 1
116#define CR1_CACHE_WT 2
117
118#define ARM_SMMU_CR2 0x2c
119#define CR2_PTM (1 << 2)
120#define CR2_RECINVSID (1 << 1)
121#define CR2_E2H (1 << 0)
122
123#define ARM_SMMU_GBPA 0x44
124#define GBPA_UPDATE (1 << 31)
125#define GBPA_ABORT (1 << 20)
126
127#define ARM_SMMU_IRQ_CTRL 0x50
128#define IRQ_CTRL_EVTQ_IRQEN (1 << 2)
129#define IRQ_CTRL_PRIQ_IRQEN (1 << 1)
130#define IRQ_CTRL_GERROR_IRQEN (1 << 0)
131
132#define ARM_SMMU_IRQ_CTRLACK 0x54
133
134#define ARM_SMMU_GERROR 0x60
135#define GERROR_SFM_ERR (1 << 8)
136#define GERROR_MSI_GERROR_ABT_ERR (1 << 7)
137#define GERROR_MSI_PRIQ_ABT_ERR (1 << 6)
138#define GERROR_MSI_EVTQ_ABT_ERR (1 << 5)
139#define GERROR_MSI_CMDQ_ABT_ERR (1 << 4)
140#define GERROR_PRIQ_ABT_ERR (1 << 3)
141#define GERROR_EVTQ_ABT_ERR (1 << 2)
142#define GERROR_CMDQ_ERR (1 << 0)
143#define GERROR_ERR_MASK 0xfd
144
145#define ARM_SMMU_GERRORN 0x64
146
147#define ARM_SMMU_GERROR_IRQ_CFG0 0x68
148#define ARM_SMMU_GERROR_IRQ_CFG1 0x70
149#define ARM_SMMU_GERROR_IRQ_CFG2 0x74
150
151#define ARM_SMMU_STRTAB_BASE 0x80
152#define STRTAB_BASE_RA (1UL << 62)
153#define STRTAB_BASE_ADDR_MASK GENMASK_ULL(51, 6)
154
155#define ARM_SMMU_STRTAB_BASE_CFG 0x88
156#define STRTAB_BASE_CFG_FMT GENMASK(17, 16)
157#define STRTAB_BASE_CFG_FMT_LINEAR 0
158#define STRTAB_BASE_CFG_FMT_2LVL 1
159#define STRTAB_BASE_CFG_SPLIT GENMASK(10, 6)
160#define STRTAB_BASE_CFG_LOG2SIZE GENMASK(5, 0)
161
162#define ARM_SMMU_CMDQ_BASE 0x90
163#define ARM_SMMU_CMDQ_PROD 0x98
164#define ARM_SMMU_CMDQ_CONS 0x9c
165
166#define ARM_SMMU_EVTQ_BASE 0xa0
167#define ARM_SMMU_EVTQ_PROD 0x100a8
168#define ARM_SMMU_EVTQ_CONS 0x100ac
169#define ARM_SMMU_EVTQ_IRQ_CFG0 0xb0
170#define ARM_SMMU_EVTQ_IRQ_CFG1 0xb8
171#define ARM_SMMU_EVTQ_IRQ_CFG2 0xbc
172
173#define ARM_SMMU_PRIQ_BASE 0xc0
174#define ARM_SMMU_PRIQ_PROD 0x100c8
175#define ARM_SMMU_PRIQ_CONS 0x100cc
176#define ARM_SMMU_PRIQ_IRQ_CFG0 0xd0
177#define ARM_SMMU_PRIQ_IRQ_CFG1 0xd8
178#define ARM_SMMU_PRIQ_IRQ_CFG2 0xdc
179
180/* Common MSI config fields */
181#define MSI_CFG0_ADDR_MASK GENMASK_ULL(51, 2)
182#define MSI_CFG2_SH GENMASK(5, 4)
183#define MSI_CFG2_MEMATTR GENMASK(3, 0)
184
185/* Common memory attribute values */
186#define ARM_SMMU_SH_NSH 0
187#define ARM_SMMU_SH_OSH 2
188#define ARM_SMMU_SH_ISH 3
189#define ARM_SMMU_MEMATTR_DEVICE_nGnRE 0x1
190#define ARM_SMMU_MEMATTR_OIWB 0xf
191
192#define Q_IDX(q, p) ((p) & ((1 << (q)->max_n_shift) - 1))
193#define Q_WRP(q, p) ((p) & (1 << (q)->max_n_shift))
194#define Q_OVERFLOW_FLAG (1 << 31)
195#define Q_OVF(q, p) ((p) & Q_OVERFLOW_FLAG)
196#define Q_ENT(q, p) ((q)->base + \
197 Q_IDX(q, p) * (q)->ent_dwords)
198
199#define Q_BASE_RWA (1UL << 62)
200#define Q_BASE_ADDR_MASK GENMASK_ULL(51, 5)
201#define Q_BASE_LOG2SIZE GENMASK(4, 0)
202
203/*
204 * Stream table.
205 *
206 * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
207 * 2lvl: 128k L1 entries,
208 * 256 lazy entries per table (each table covers a PCI bus)
209 */
210#define STRTAB_L1_SZ_SHIFT 20
211#define STRTAB_SPLIT 8
212
213#define STRTAB_L1_DESC_DWORDS 1
214#define STRTAB_L1_DESC_SPAN GENMASK_ULL(4, 0)
215#define STRTAB_L1_DESC_L2PTR_MASK GENMASK_ULL(51, 6)
216
217#define STRTAB_STE_DWORDS 8
218#define STRTAB_STE_0_V (1UL << 0)
219#define STRTAB_STE_0_CFG GENMASK_ULL(3, 1)
220#define STRTAB_STE_0_CFG_ABORT 0
221#define STRTAB_STE_0_CFG_BYPASS 4
222#define STRTAB_STE_0_CFG_S1_TRANS 5
223#define STRTAB_STE_0_CFG_S2_TRANS 6
224
225#define STRTAB_STE_0_S1FMT GENMASK_ULL(5, 4)
226#define STRTAB_STE_0_S1FMT_LINEAR 0
227#define STRTAB_STE_0_S1CTXPTR_MASK GENMASK_ULL(51, 6)
228#define STRTAB_STE_0_S1CDMAX GENMASK_ULL(63, 59)
229
230#define STRTAB_STE_1_S1C_CACHE_NC 0UL
231#define STRTAB_STE_1_S1C_CACHE_WBRA 1UL
232#define STRTAB_STE_1_S1C_CACHE_WT 2UL
233#define STRTAB_STE_1_S1C_CACHE_WB 3UL
234#define STRTAB_STE_1_S1CIR GENMASK_ULL(3, 2)
235#define STRTAB_STE_1_S1COR GENMASK_ULL(5, 4)
236#define STRTAB_STE_1_S1CSH GENMASK_ULL(7, 6)
237
238#define STRTAB_STE_1_S1STALLD (1UL << 27)
239
240#define STRTAB_STE_1_EATS GENMASK_ULL(29, 28)
241#define STRTAB_STE_1_EATS_ABT 0UL
242#define STRTAB_STE_1_EATS_TRANS 1UL
243#define STRTAB_STE_1_EATS_S1CHK 2UL
244
245#define STRTAB_STE_1_STRW GENMASK_ULL(31, 30)
246#define STRTAB_STE_1_STRW_NSEL1 0UL
247#define STRTAB_STE_1_STRW_EL2 2UL
248
249#define STRTAB_STE_1_SHCFG GENMASK_ULL(45, 44)
250#define STRTAB_STE_1_SHCFG_INCOMING 1UL
251
252#define STRTAB_STE_2_S2VMID GENMASK_ULL(15, 0)
253#define STRTAB_STE_2_VTCR GENMASK_ULL(50, 32)
254#define STRTAB_STE_2_S2AA64 (1UL << 51)
255#define STRTAB_STE_2_S2ENDI (1UL << 52)
256#define STRTAB_STE_2_S2PTW (1UL << 54)
257#define STRTAB_STE_2_S2R (1UL << 58)
258
259#define STRTAB_STE_3_S2TTB_MASK GENMASK_ULL(51, 4)
260
261/* Context descriptor (stage-1 only) */
262#define CTXDESC_CD_DWORDS 8
263#define CTXDESC_CD_0_TCR_T0SZ GENMASK_ULL(5, 0)
264#define ARM64_TCR_T0SZ GENMASK_ULL(5, 0)
265#define CTXDESC_CD_0_TCR_TG0 GENMASK_ULL(7, 6)
266#define ARM64_TCR_TG0 GENMASK_ULL(15, 14)
267#define CTXDESC_CD_0_TCR_IRGN0 GENMASK_ULL(9, 8)
268#define ARM64_TCR_IRGN0 GENMASK_ULL(9, 8)
269#define CTXDESC_CD_0_TCR_ORGN0 GENMASK_ULL(11, 10)
270#define ARM64_TCR_ORGN0 GENMASK_ULL(11, 10)
271#define CTXDESC_CD_0_TCR_SH0 GENMASK_ULL(13, 12)
272#define ARM64_TCR_SH0 GENMASK_ULL(13, 12)
273#define CTXDESC_CD_0_TCR_EPD0 (1ULL << 14)
274#define ARM64_TCR_EPD0 (1ULL << 7)
275#define CTXDESC_CD_0_TCR_EPD1 (1ULL << 30)
276#define ARM64_TCR_EPD1 (1ULL << 23)
277
278#define CTXDESC_CD_0_ENDI (1UL << 15)
279#define CTXDESC_CD_0_V (1UL << 31)
280
281#define CTXDESC_CD_0_TCR_IPS GENMASK_ULL(34, 32)
282#define ARM64_TCR_IPS GENMASK_ULL(34, 32)
283#define CTXDESC_CD_0_TCR_TBI0 (1ULL << 38)
284#define ARM64_TCR_TBI0 (1ULL << 37)
285
286#define CTXDESC_CD_0_AA64 (1UL << 41)
287#define CTXDESC_CD_0_S (1UL << 44)
288#define CTXDESC_CD_0_R (1UL << 45)
289#define CTXDESC_CD_0_A (1UL << 46)
290#define CTXDESC_CD_0_ASET (1UL << 47)
291#define CTXDESC_CD_0_ASID GENMASK_ULL(63, 48)
292
293#define CTXDESC_CD_1_TTB0_MASK GENMASK_ULL(51, 4)
294
295/* Convert between AArch64 (CPU) TCR format and SMMU CD format */
296#define ARM_SMMU_TCR2CD(tcr, fld) FIELD_PREP(CTXDESC_CD_0_TCR_##fld, \
297 FIELD_GET(ARM64_TCR_##fld, tcr))
298
299/* Command queue */
300#define CMDQ_ENT_DWORDS 2
301#define CMDQ_MAX_SZ_SHIFT 8
302
303#define CMDQ_CONS_ERR GENMASK(30, 24)
304#define CMDQ_ERR_CERROR_NONE_IDX 0
305#define CMDQ_ERR_CERROR_ILL_IDX 1
306#define CMDQ_ERR_CERROR_ABT_IDX 2
307
308#define CMDQ_0_OP GENMASK_ULL(7, 0)
309#define CMDQ_0_SSV (1UL << 11)
310
311#define CMDQ_PREFETCH_0_SID GENMASK_ULL(63, 32)
312#define CMDQ_PREFETCH_1_SIZE GENMASK_ULL(4, 0)
313#define CMDQ_PREFETCH_1_ADDR_MASK GENMASK_ULL(63, 12)
314
315#define CMDQ_CFGI_0_SID GENMASK_ULL(63, 32)
316#define CMDQ_CFGI_1_LEAF (1UL << 0)
317#define CMDQ_CFGI_1_RANGE GENMASK_ULL(4, 0)
318
319#define CMDQ_TLBI_0_VMID GENMASK_ULL(47, 32)
320#define CMDQ_TLBI_0_ASID GENMASK_ULL(63, 48)
321#define CMDQ_TLBI_1_LEAF (1UL << 0)
322#define CMDQ_TLBI_1_VA_MASK GENMASK_ULL(63, 12)
323#define CMDQ_TLBI_1_IPA_MASK GENMASK_ULL(51, 12)
324
325#define CMDQ_PRI_0_SSID GENMASK_ULL(31, 12)
326#define CMDQ_PRI_0_SID GENMASK_ULL(63, 32)
327#define CMDQ_PRI_1_GRPID GENMASK_ULL(8, 0)
328#define CMDQ_PRI_1_RESP GENMASK_ULL(13, 12)
329
330#define CMDQ_SYNC_0_CS GENMASK_ULL(13, 12)
331#define CMDQ_SYNC_0_CS_NONE 0
332#define CMDQ_SYNC_0_CS_IRQ 1
333#define CMDQ_SYNC_0_CS_SEV 2
334#define CMDQ_SYNC_0_MSH GENMASK_ULL(23, 22)
335#define CMDQ_SYNC_0_MSIATTR GENMASK_ULL(27, 24)
336#define CMDQ_SYNC_0_MSIDATA GENMASK_ULL(63, 32)
337#define CMDQ_SYNC_1_MSIADDR_MASK GENMASK_ULL(51, 2)
338
339/* Event queue */
340#define EVTQ_ENT_DWORDS 4
341#define EVTQ_MAX_SZ_SHIFT 7
342
343#define EVTQ_0_ID GENMASK_ULL(7, 0)
344
345/* PRI queue */
346#define PRIQ_ENT_DWORDS 2
347#define PRIQ_MAX_SZ_SHIFT 8
348
349#define PRIQ_0_SID GENMASK_ULL(31, 0)
350#define PRIQ_0_SSID GENMASK_ULL(51, 32)
351#define PRIQ_0_PERM_PRIV (1UL << 58)
352#define PRIQ_0_PERM_EXEC (1UL << 59)
353#define PRIQ_0_PERM_READ (1UL << 60)
354#define PRIQ_0_PERM_WRITE (1UL << 61)
355#define PRIQ_0_PRG_LAST (1UL << 62)
356#define PRIQ_0_SSID_V (1UL << 63)
357
358#define PRIQ_1_PRG_IDX GENMASK_ULL(8, 0)
359#define PRIQ_1_ADDR_MASK GENMASK_ULL(63, 12)
360
361/* High-level queue structures */
362#define ARM_SMMU_POLL_TIMEOUT_US 100
363#define ARM_SMMU_CMDQ_SYNC_TIMEOUT_US 1000000 /* 1s! */
364#define ARM_SMMU_CMDQ_SYNC_SPIN_COUNT 10
365
366#define MSI_IOVA_BASE 0x8000000
367#define MSI_IOVA_LENGTH 0x100000
368
369static bool disable_bypass = 1;
370module_param_named(disable_bypass, disable_bypass, bool, S_IRUGO);
371MODULE_PARM_DESC(disable_bypass,
372 "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.");
373
374enum pri_resp {
375 PRI_RESP_DENY = 0,
376 PRI_RESP_FAIL = 1,
377 PRI_RESP_SUCC = 2,
378};
379
380enum arm_smmu_msi_index {
381 EVTQ_MSI_INDEX,
382 GERROR_MSI_INDEX,
383 PRIQ_MSI_INDEX,
384 ARM_SMMU_MAX_MSIS,
385};
386
387static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {
388 [EVTQ_MSI_INDEX] = {
389 ARM_SMMU_EVTQ_IRQ_CFG0,
390 ARM_SMMU_EVTQ_IRQ_CFG1,
391 ARM_SMMU_EVTQ_IRQ_CFG2,
392 },
393 [GERROR_MSI_INDEX] = {
394 ARM_SMMU_GERROR_IRQ_CFG0,
395 ARM_SMMU_GERROR_IRQ_CFG1,
396 ARM_SMMU_GERROR_IRQ_CFG2,
397 },
398 [PRIQ_MSI_INDEX] = {
399 ARM_SMMU_PRIQ_IRQ_CFG0,
400 ARM_SMMU_PRIQ_IRQ_CFG1,
401 ARM_SMMU_PRIQ_IRQ_CFG2,
402 },
403};
404
405struct arm_smmu_cmdq_ent {
406 /* Common fields */
407 u8 opcode;
408 bool substream_valid;
409
410 /* Command-specific fields */
411 union {
412 #define CMDQ_OP_PREFETCH_CFG 0x1
413 struct {
414 u32 sid;
415 u8 size;
416 u64 addr;
417 } prefetch;
418
419 #define CMDQ_OP_CFGI_STE 0x3
420 #define CMDQ_OP_CFGI_ALL 0x4
421 struct {
422 u32 sid;
423 union {
424 bool leaf;
425 u8 span;
426 };
427 } cfgi;
428
429 #define CMDQ_OP_TLBI_NH_ASID 0x11
430 #define CMDQ_OP_TLBI_NH_VA 0x12
431 #define CMDQ_OP_TLBI_EL2_ALL 0x20
432 #define CMDQ_OP_TLBI_S12_VMALL 0x28
433 #define CMDQ_OP_TLBI_S2_IPA 0x2a
434 #define CMDQ_OP_TLBI_NSNH_ALL 0x30
435 struct {
436 u16 asid;
437 u16 vmid;
438 bool leaf;
439 u64 addr;
440 } tlbi;
441
442 #define CMDQ_OP_PRI_RESP 0x41
443 struct {
444 u32 sid;
445 u32 ssid;
446 u16 grpid;
447 enum pri_resp resp;
448 } pri;
449
450 #define CMDQ_OP_CMD_SYNC 0x46
451 struct {
452 u32 msidata;
453 u64 msiaddr;
454 } sync;
455 };
456};
457
458struct arm_smmu_queue {
459 int irq; /* Wired interrupt */
460
461 __le64 *base;
462 dma_addr_t base_dma;
463 u64 q_base;
464
465 size_t ent_dwords;
466 u32 max_n_shift;
467 u32 prod;
468 u32 cons;
469
470 u32 __iomem *prod_reg;
471 u32 __iomem *cons_reg;
472};
473
474struct arm_smmu_cmdq {
475 struct arm_smmu_queue q;
476 spinlock_t lock;
477};
478
479struct arm_smmu_evtq {
480 struct arm_smmu_queue q;
481 u32 max_stalls;
482};
483
484struct arm_smmu_priq {
485 struct arm_smmu_queue q;
486};
487
488/* High-level stream table and context descriptor structures */
489struct arm_smmu_strtab_l1_desc {
490 u8 span;
491
492 __le64 *l2ptr;
493 dma_addr_t l2ptr_dma;
494};
495
496struct arm_smmu_s1_cfg {
497 __le64 *cdptr;
498 dma_addr_t cdptr_dma;
499
500 struct arm_smmu_ctx_desc {
501 u16 asid;
502 u64 ttbr;
503 u64 tcr;
504 u64 mair;
505 } cd;
506};
507
508struct arm_smmu_s2_cfg {
509 u16 vmid;
510 u64 vttbr;
511 u64 vtcr;
512};
513
514struct arm_smmu_strtab_ent {
515 /*
516 * An STE is "assigned" if the master emitting the corresponding SID
517 * is attached to a domain. The behaviour of an unassigned STE is
518 * determined by the disable_bypass parameter, whereas an assigned
519 * STE behaves according to s1_cfg/s2_cfg, which themselves are
520 * configured according to the domain type.
521 */
522 bool assigned;
523 struct arm_smmu_s1_cfg *s1_cfg;
524 struct arm_smmu_s2_cfg *s2_cfg;
525};
526
527struct arm_smmu_strtab_cfg {
528 __le64 *strtab;
529 dma_addr_t strtab_dma;
530 struct arm_smmu_strtab_l1_desc *l1_desc;
531 unsigned int num_l1_ents;
532
533 u64 strtab_base;
534 u32 strtab_base_cfg;
535};
536
537/* An SMMUv3 instance */
538struct arm_smmu_device {
539 struct device *dev;
540 void __iomem *base;
541
542#define ARM_SMMU_FEAT_2_LVL_STRTAB (1 << 0)
543#define ARM_SMMU_FEAT_2_LVL_CDTAB (1 << 1)
544#define ARM_SMMU_FEAT_TT_LE (1 << 2)
545#define ARM_SMMU_FEAT_TT_BE (1 << 3)
546#define ARM_SMMU_FEAT_PRI (1 << 4)
547#define ARM_SMMU_FEAT_ATS (1 << 5)
548#define ARM_SMMU_FEAT_SEV (1 << 6)
549#define ARM_SMMU_FEAT_MSI (1 << 7)
550#define ARM_SMMU_FEAT_COHERENCY (1 << 8)
551#define ARM_SMMU_FEAT_TRANS_S1 (1 << 9)
552#define ARM_SMMU_FEAT_TRANS_S2 (1 << 10)
553#define ARM_SMMU_FEAT_STALLS (1 << 11)
554#define ARM_SMMU_FEAT_HYP (1 << 12)
555#define ARM_SMMU_FEAT_STALL_FORCE (1 << 13)
556#define ARM_SMMU_FEAT_VAX (1 << 14)
557 u32 features;
558
559#define ARM_SMMU_OPT_SKIP_PREFETCH (1 << 0)
560#define ARM_SMMU_OPT_PAGE0_REGS_ONLY (1 << 1)
561 u32 options;
562
563 struct arm_smmu_cmdq cmdq;
564 struct arm_smmu_evtq evtq;
565 struct arm_smmu_priq priq;
566
567 int gerr_irq;
568 int combined_irq;
569 u32 sync_nr;
570
571 unsigned long ias; /* IPA */
572 unsigned long oas; /* PA */
573 unsigned long pgsize_bitmap;
574
575#define ARM_SMMU_MAX_ASIDS (1 << 16)
576 unsigned int asid_bits;
577 DECLARE_BITMAP(asid_map, ARM_SMMU_MAX_ASIDS);
578
579#define ARM_SMMU_MAX_VMIDS (1 << 16)
580 unsigned int vmid_bits;
581 DECLARE_BITMAP(vmid_map, ARM_SMMU_MAX_VMIDS);
582
583 unsigned int ssid_bits;
584 unsigned int sid_bits;
585
586 struct arm_smmu_strtab_cfg strtab_cfg;
587
588 /* Hi16xx adds an extra 32 bits of goodness to its MSI payload */
589 union {
590 u32 sync_count;
591 u64 padding;
592 };
593
594 /* IOMMU core code handle */
595 struct iommu_device iommu;
596};
597
598/* SMMU private data for each master */
599struct arm_smmu_master_data {
600 struct arm_smmu_device *smmu;
601 struct arm_smmu_strtab_ent ste;
602};
603
604/* SMMU private data for an IOMMU domain */
605enum arm_smmu_domain_stage {
606 ARM_SMMU_DOMAIN_S1 = 0,
607 ARM_SMMU_DOMAIN_S2,
608 ARM_SMMU_DOMAIN_NESTED,
609 ARM_SMMU_DOMAIN_BYPASS,
610};
611
612struct arm_smmu_domain {
613 struct arm_smmu_device *smmu;
614 struct mutex init_mutex; /* Protects smmu pointer */
615
616 struct io_pgtable_ops *pgtbl_ops;
617
618 enum arm_smmu_domain_stage stage;
619 union {
620 struct arm_smmu_s1_cfg s1_cfg;
621 struct arm_smmu_s2_cfg s2_cfg;
622 };
623
624 struct iommu_domain domain;
625};
626
627struct arm_smmu_option_prop {
628 u32 opt;
629 const char *prop;
630};
631
632static struct arm_smmu_option_prop arm_smmu_options[] = {
633 { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
634 { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"},
635 { 0, NULL},
636};
637
638static inline void __iomem *arm_smmu_page1_fixup(unsigned long offset,
639 struct arm_smmu_device *smmu)
640{
641 if ((offset > SZ_64K) &&
642 (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY))
643 offset -= SZ_64K;
644
645 return smmu->base + offset;
646}
647
648static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
649{
650 return container_of(dom, struct arm_smmu_domain, domain);
651}
652
653static void parse_driver_options(struct arm_smmu_device *smmu)
654{
655 int i = 0;
656
657 do {
658 if (of_property_read_bool(smmu->dev->of_node,
659 arm_smmu_options[i].prop)) {
660 smmu->options |= arm_smmu_options[i].opt;
661 dev_notice(smmu->dev, "option %s\n",
662 arm_smmu_options[i].prop);
663 }
664 } while (arm_smmu_options[++i].opt);
665}
666
667/* Low-level queue manipulation functions */
668static bool queue_full(struct arm_smmu_queue *q)
669{
670 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
671 Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
672}
673
674static bool queue_empty(struct arm_smmu_queue *q)
675{
676 return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
677 Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
678}
679
680static void queue_sync_cons(struct arm_smmu_queue *q)
681{
682 q->cons = readl_relaxed(q->cons_reg);
683}
684
685static void queue_inc_cons(struct arm_smmu_queue *q)
686{
687 u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
688
689 q->cons = Q_OVF(q, q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
690
691 /*
692 * Ensure that all CPU accesses (reads and writes) to the queue
693 * are complete before we update the cons pointer.
694 */
695 mb();
696 writel_relaxed(q->cons, q->cons_reg);
697}
698
699static int queue_sync_prod(struct arm_smmu_queue *q)
700{
701 int ret = 0;
702 u32 prod = readl_relaxed(q->prod_reg);
703
704 if (Q_OVF(q, prod) != Q_OVF(q, q->prod))
705 ret = -EOVERFLOW;
706
707 q->prod = prod;
708 return ret;
709}
710
711static void queue_inc_prod(struct arm_smmu_queue *q)
712{
713 u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + 1;
714
715 q->prod = Q_OVF(q, q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
716 writel(q->prod, q->prod_reg);
717}
718
719/*
720 * Wait for the SMMU to consume items. If drain is true, wait until the queue
721 * is empty. Otherwise, wait until there is at least one free slot.
722 */
723static int queue_poll_cons(struct arm_smmu_queue *q, bool sync, bool wfe)
724{
725 ktime_t timeout;
726 unsigned int delay = 1, spin_cnt = 0;
727
728 /* Wait longer if it's a CMD_SYNC */
729 timeout = ktime_add_us(ktime_get(), sync ?
730 ARM_SMMU_CMDQ_SYNC_TIMEOUT_US :
731 ARM_SMMU_POLL_TIMEOUT_US);
732
733 while (queue_sync_cons(q), (sync ? !queue_empty(q) : queue_full(q))) {
734 if (ktime_compare(ktime_get(), timeout) > 0)
735 return -ETIMEDOUT;
736
737 if (wfe) {
738 wfe();
739 } else if (++spin_cnt < ARM_SMMU_CMDQ_SYNC_SPIN_COUNT) {
740 cpu_relax();
741 continue;
742 } else {
743 udelay(delay);
744 delay *= 2;
745 spin_cnt = 0;
746 }
747 }
748
749 return 0;
750}
751
752static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
753{
754 int i;
755
756 for (i = 0; i < n_dwords; ++i)
757 *dst++ = cpu_to_le64(*src++);
758}
759
760static int queue_insert_raw(struct arm_smmu_queue *q, u64 *ent)
761{
762 if (queue_full(q))
763 return -ENOSPC;
764
765 queue_write(Q_ENT(q, q->prod), ent, q->ent_dwords);
766 queue_inc_prod(q);
767 return 0;
768}
769
770static void queue_read(__le64 *dst, u64 *src, size_t n_dwords)
771{
772 int i;
773
774 for (i = 0; i < n_dwords; ++i)
775 *dst++ = le64_to_cpu(*src++);
776}
777
778static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent)
779{
780 if (queue_empty(q))
781 return -EAGAIN;
782
783 queue_read(ent, Q_ENT(q, q->cons), q->ent_dwords);
784 queue_inc_cons(q);
785 return 0;
786}
787
788/* High-level queue accessors */
789static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
790{
791 memset(cmd, 0, CMDQ_ENT_DWORDS << 3);
792 cmd[0] |= FIELD_PREP(CMDQ_0_OP, ent->opcode);
793
794 switch (ent->opcode) {
795 case CMDQ_OP_TLBI_EL2_ALL:
796 case CMDQ_OP_TLBI_NSNH_ALL:
797 break;
798 case CMDQ_OP_PREFETCH_CFG:
799 cmd[0] |= FIELD_PREP(CMDQ_PREFETCH_0_SID, ent->prefetch.sid);
800 cmd[1] |= FIELD_PREP(CMDQ_PREFETCH_1_SIZE, ent->prefetch.size);
801 cmd[1] |= ent->prefetch.addr & CMDQ_PREFETCH_1_ADDR_MASK;
802 break;
803 case CMDQ_OP_CFGI_STE:
804 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, ent->cfgi.sid);
805 cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_LEAF, ent->cfgi.leaf);
806 break;
807 case CMDQ_OP_CFGI_ALL:
808 /* Cover the entire SID range */
809 cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_RANGE, 31);
810 break;
811 case CMDQ_OP_TLBI_NH_VA:
812 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid);
813 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf);
814 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_VA_MASK;
815 break;
816 case CMDQ_OP_TLBI_S2_IPA:
817 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
818 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf);
819 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_IPA_MASK;
820 break;
821 case CMDQ_OP_TLBI_NH_ASID:
822 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid);
823 /* Fallthrough */
824 case CMDQ_OP_TLBI_S12_VMALL:
825 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
826 break;
827 case CMDQ_OP_PRI_RESP:
828 cmd[0] |= FIELD_PREP(CMDQ_0_SSV, ent->substream_valid);
829 cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SSID, ent->pri.ssid);
830 cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SID, ent->pri.sid);
831 cmd[1] |= FIELD_PREP(CMDQ_PRI_1_GRPID, ent->pri.grpid);
832 switch (ent->pri.resp) {
833 case PRI_RESP_DENY:
834 case PRI_RESP_FAIL:
835 case PRI_RESP_SUCC:
836 break;
837 default:
838 return -EINVAL;
839 }
840 cmd[1] |= FIELD_PREP(CMDQ_PRI_1_RESP, ent->pri.resp);
841 break;
842 case CMDQ_OP_CMD_SYNC:
843 if (ent->sync.msiaddr)
844 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_IRQ);
845 else
846 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV);
847 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSH, ARM_SMMU_SH_ISH);
848 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSIATTR, ARM_SMMU_MEMATTR_OIWB);
849 /*
850 * Commands are written little-endian, but we want the SMMU to
851 * receive MSIData, and thus write it back to memory, in CPU
852 * byte order, so big-endian needs an extra byteswap here.
853 */
854 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSIDATA,
855 cpu_to_le32(ent->sync.msidata));
856 cmd[1] |= ent->sync.msiaddr & CMDQ_SYNC_1_MSIADDR_MASK;
857 break;
858 default:
859 return -ENOENT;
860 }
861
862 return 0;
863}
864
865static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu)
866{
867 static const char *cerror_str[] = {
868 [CMDQ_ERR_CERROR_NONE_IDX] = "No error",
869 [CMDQ_ERR_CERROR_ILL_IDX] = "Illegal command",
870 [CMDQ_ERR_CERROR_ABT_IDX] = "Abort on command fetch",
871 };
872
873 int i;
874 u64 cmd[CMDQ_ENT_DWORDS];
875 struct arm_smmu_queue *q = &smmu->cmdq.q;
876 u32 cons = readl_relaxed(q->cons_reg);
877 u32 idx = FIELD_GET(CMDQ_CONS_ERR, cons);
878 struct arm_smmu_cmdq_ent cmd_sync = {
879 .opcode = CMDQ_OP_CMD_SYNC,
880 };
881
882 dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons,
883 idx < ARRAY_SIZE(cerror_str) ? cerror_str[idx] : "Unknown");
884
885 switch (idx) {
886 case CMDQ_ERR_CERROR_ABT_IDX:
887 dev_err(smmu->dev, "retrying command fetch\n");
888 case CMDQ_ERR_CERROR_NONE_IDX:
889 return;
890 case CMDQ_ERR_CERROR_ILL_IDX:
891 /* Fallthrough */
892 default:
893 break;
894 }
895
896 /*
897 * We may have concurrent producers, so we need to be careful
898 * not to touch any of the shadow cmdq state.
899 */
900 queue_read(cmd, Q_ENT(q, cons), q->ent_dwords);
901 dev_err(smmu->dev, "skipping command in error state:\n");
902 for (i = 0; i < ARRAY_SIZE(cmd); ++i)
903 dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
904
905 /* Convert the erroneous command into a CMD_SYNC */
906 if (arm_smmu_cmdq_build_cmd(cmd, &cmd_sync)) {
907 dev_err(smmu->dev, "failed to convert to CMD_SYNC\n");
908 return;
909 }
910
911 queue_write(Q_ENT(q, cons), cmd, q->ent_dwords);
912}
913
914static void arm_smmu_cmdq_insert_cmd(struct arm_smmu_device *smmu, u64 *cmd)
915{
916 struct arm_smmu_queue *q = &smmu->cmdq.q;
917 bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
918
919 while (queue_insert_raw(q, cmd) == -ENOSPC) {
920 if (queue_poll_cons(q, false, wfe))
921 dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
922 }
923}
924
925static void arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
926 struct arm_smmu_cmdq_ent *ent)
927{
928 u64 cmd[CMDQ_ENT_DWORDS];
929 unsigned long flags;
930
931 if (arm_smmu_cmdq_build_cmd(cmd, ent)) {
932 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
933 ent->opcode);
934 return;
935 }
936
937 spin_lock_irqsave(&smmu->cmdq.lock, flags);
938 arm_smmu_cmdq_insert_cmd(smmu, cmd);
939 spin_unlock_irqrestore(&smmu->cmdq.lock, flags);
940}
941
942/*
943 * The difference between val and sync_idx is bounded by the maximum size of
944 * a queue at 2^20 entries, so 32 bits is plenty for wrap-safe arithmetic.
945 */
946static int __arm_smmu_sync_poll_msi(struct arm_smmu_device *smmu, u32 sync_idx)
947{
948 ktime_t timeout;
949 u32 val;
950
951 timeout = ktime_add_us(ktime_get(), ARM_SMMU_CMDQ_SYNC_TIMEOUT_US);
952 val = smp_cond_load_acquire(&smmu->sync_count,
953 (int)(VAL - sync_idx) >= 0 ||
954 !ktime_before(ktime_get(), timeout));
955
956 return (int)(val - sync_idx) < 0 ? -ETIMEDOUT : 0;
957}
958
959static int __arm_smmu_cmdq_issue_sync_msi(struct arm_smmu_device *smmu)
960{
961 u64 cmd[CMDQ_ENT_DWORDS];
962 unsigned long flags;
963 struct arm_smmu_cmdq_ent ent = {
964 .opcode = CMDQ_OP_CMD_SYNC,
965 .sync = {
966 .msiaddr = virt_to_phys(&smmu->sync_count),
967 },
968 };
969
970 spin_lock_irqsave(&smmu->cmdq.lock, flags);
971 ent.sync.msidata = ++smmu->sync_nr;
972 arm_smmu_cmdq_build_cmd(cmd, &ent);
973 arm_smmu_cmdq_insert_cmd(smmu, cmd);
974 spin_unlock_irqrestore(&smmu->cmdq.lock, flags);
975
976 return __arm_smmu_sync_poll_msi(smmu, ent.sync.msidata);
977}
978
979static int __arm_smmu_cmdq_issue_sync(struct arm_smmu_device *smmu)
980{
981 u64 cmd[CMDQ_ENT_DWORDS];
982 unsigned long flags;
983 bool wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
984 struct arm_smmu_cmdq_ent ent = { .opcode = CMDQ_OP_CMD_SYNC };
985 int ret;
986
987 arm_smmu_cmdq_build_cmd(cmd, &ent);
988
989 spin_lock_irqsave(&smmu->cmdq.lock, flags);
990 arm_smmu_cmdq_insert_cmd(smmu, cmd);
991 ret = queue_poll_cons(&smmu->cmdq.q, true, wfe);
992 spin_unlock_irqrestore(&smmu->cmdq.lock, flags);
993
994 return ret;
995}
996
997static void arm_smmu_cmdq_issue_sync(struct arm_smmu_device *smmu)
998{
999 int ret;
1000 bool msi = (smmu->features & ARM_SMMU_FEAT_MSI) &&
1001 (smmu->features & ARM_SMMU_FEAT_COHERENCY);
1002
1003 ret = msi ? __arm_smmu_cmdq_issue_sync_msi(smmu)
1004 : __arm_smmu_cmdq_issue_sync(smmu);
1005 if (ret)
1006 dev_err_ratelimited(smmu->dev, "CMD_SYNC timeout\n");
1007}
1008
1009/* Context descriptor manipulation functions */
1010static u64 arm_smmu_cpu_tcr_to_cd(u64 tcr)
1011{
1012 u64 val = 0;
1013
1014 /* Repack the TCR. Just care about TTBR0 for now */
1015 val |= ARM_SMMU_TCR2CD(tcr, T0SZ);
1016 val |= ARM_SMMU_TCR2CD(tcr, TG0);
1017 val |= ARM_SMMU_TCR2CD(tcr, IRGN0);
1018 val |= ARM_SMMU_TCR2CD(tcr, ORGN0);
1019 val |= ARM_SMMU_TCR2CD(tcr, SH0);
1020 val |= ARM_SMMU_TCR2CD(tcr, EPD0);
1021 val |= ARM_SMMU_TCR2CD(tcr, EPD1);
1022 val |= ARM_SMMU_TCR2CD(tcr, IPS);
1023 val |= ARM_SMMU_TCR2CD(tcr, TBI0);
1024
1025 return val;
1026}
1027
1028static void arm_smmu_write_ctx_desc(struct arm_smmu_device *smmu,
1029 struct arm_smmu_s1_cfg *cfg)
1030{
1031 u64 val;
1032
1033 /*
1034 * We don't need to issue any invalidation here, as we'll invalidate
1035 * the STE when installing the new entry anyway.
1036 */
1037 val = arm_smmu_cpu_tcr_to_cd(cfg->cd.tcr) |
1038#ifdef __BIG_ENDIAN
1039 CTXDESC_CD_0_ENDI |
1040#endif
1041 CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET |
1042 CTXDESC_CD_0_AA64 | FIELD_PREP(CTXDESC_CD_0_ASID, cfg->cd.asid) |
1043 CTXDESC_CD_0_V;
1044
1045 /* STALL_MODEL==0b10 && CD.S==0 is ILLEGAL */
1046 if (smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
1047 val |= CTXDESC_CD_0_S;
1048
1049 cfg->cdptr[0] = cpu_to_le64(val);
1050
1051 val = cfg->cd.ttbr & CTXDESC_CD_1_TTB0_MASK;
1052 cfg->cdptr[1] = cpu_to_le64(val);
1053
1054 cfg->cdptr[3] = cpu_to_le64(cfg->cd.mair);
1055}
1056
1057/* Stream table manipulation functions */
1058static void
1059arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc)
1060{
1061 u64 val = 0;
1062
1063 val |= FIELD_PREP(STRTAB_L1_DESC_SPAN, desc->span);
1064 val |= desc->l2ptr_dma & STRTAB_L1_DESC_L2PTR_MASK;
1065
1066 *dst = cpu_to_le64(val);
1067}
1068
1069static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid)
1070{
1071 struct arm_smmu_cmdq_ent cmd = {
1072 .opcode = CMDQ_OP_CFGI_STE,
1073 .cfgi = {
1074 .sid = sid,
1075 .leaf = true,
1076 },
1077 };
1078
1079 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1080 arm_smmu_cmdq_issue_sync(smmu);
1081}
1082
1083static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
1084 __le64 *dst, struct arm_smmu_strtab_ent *ste)
1085{
1086 /*
1087 * This is hideously complicated, but we only really care about
1088 * three cases at the moment:
1089 *
1090 * 1. Invalid (all zero) -> bypass/fault (init)
1091 * 2. Bypass/fault -> translation/bypass (attach)
1092 * 3. Translation/bypass -> bypass/fault (detach)
1093 *
1094 * Given that we can't update the STE atomically and the SMMU
1095 * doesn't read the thing in a defined order, that leaves us
1096 * with the following maintenance requirements:
1097 *
1098 * 1. Update Config, return (init time STEs aren't live)
1099 * 2. Write everything apart from dword 0, sync, write dword 0, sync
1100 * 3. Update Config, sync
1101 */
1102 u64 val = le64_to_cpu(dst[0]);
1103 bool ste_live = false;
1104 struct arm_smmu_cmdq_ent prefetch_cmd = {
1105 .opcode = CMDQ_OP_PREFETCH_CFG,
1106 .prefetch = {
1107 .sid = sid,
1108 },
1109 };
1110
1111 if (val & STRTAB_STE_0_V) {
1112 switch (FIELD_GET(STRTAB_STE_0_CFG, val)) {
1113 case STRTAB_STE_0_CFG_BYPASS:
1114 break;
1115 case STRTAB_STE_0_CFG_S1_TRANS:
1116 case STRTAB_STE_0_CFG_S2_TRANS:
1117 ste_live = true;
1118 break;
1119 case STRTAB_STE_0_CFG_ABORT:
1120 if (disable_bypass)
1121 break;
1122 default:
1123 BUG(); /* STE corruption */
1124 }
1125 }
1126
1127 /* Nuke the existing STE_0 value, as we're going to rewrite it */
1128 val = STRTAB_STE_0_V;
1129
1130 /* Bypass/fault */
1131 if (!ste->assigned || !(ste->s1_cfg || ste->s2_cfg)) {
1132 if (!ste->assigned && disable_bypass)
1133 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT);
1134 else
1135 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_BYPASS);
1136
1137 dst[0] = cpu_to_le64(val);
1138 dst[1] = cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG,
1139 STRTAB_STE_1_SHCFG_INCOMING));
1140 dst[2] = 0; /* Nuke the VMID */
1141 /*
1142 * The SMMU can perform negative caching, so we must sync
1143 * the STE regardless of whether the old value was live.
1144 */
1145 if (smmu)
1146 arm_smmu_sync_ste_for_sid(smmu, sid);
1147 return;
1148 }
1149
1150 if (ste->s1_cfg) {
1151 BUG_ON(ste_live);
1152 dst[1] = cpu_to_le64(
1153 FIELD_PREP(STRTAB_STE_1_S1CIR, STRTAB_STE_1_S1C_CACHE_WBRA) |
1154 FIELD_PREP(STRTAB_STE_1_S1COR, STRTAB_STE_1_S1C_CACHE_WBRA) |
1155 FIELD_PREP(STRTAB_STE_1_S1CSH, ARM_SMMU_SH_ISH) |
1156#ifdef CONFIG_PCI_ATS
1157 FIELD_PREP(STRTAB_STE_1_EATS, STRTAB_STE_1_EATS_TRANS) |
1158#endif
1159 FIELD_PREP(STRTAB_STE_1_STRW, STRTAB_STE_1_STRW_NSEL1));
1160
1161 if (smmu->features & ARM_SMMU_FEAT_STALLS &&
1162 !(smmu->features & ARM_SMMU_FEAT_STALL_FORCE))
1163 dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD);
1164
1165 val |= (ste->s1_cfg->cdptr_dma & STRTAB_STE_0_S1CTXPTR_MASK) |
1166 FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S1_TRANS);
1167 }
1168
1169 if (ste->s2_cfg) {
1170 BUG_ON(ste_live);
1171 dst[2] = cpu_to_le64(
1172 FIELD_PREP(STRTAB_STE_2_S2VMID, ste->s2_cfg->vmid) |
1173 FIELD_PREP(STRTAB_STE_2_VTCR, ste->s2_cfg->vtcr) |
1174#ifdef __BIG_ENDIAN
1175 STRTAB_STE_2_S2ENDI |
1176#endif
1177 STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 |
1178 STRTAB_STE_2_S2R);
1179
1180 dst[3] = cpu_to_le64(ste->s2_cfg->vttbr & STRTAB_STE_3_S2TTB_MASK);
1181
1182 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S2_TRANS);
1183 }
1184
1185 arm_smmu_sync_ste_for_sid(smmu, sid);
1186 dst[0] = cpu_to_le64(val);
1187 arm_smmu_sync_ste_for_sid(smmu, sid);
1188
1189 /* It's likely that we'll want to use the new STE soon */
1190 if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
1191 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
1192}
1193
1194static void arm_smmu_init_bypass_stes(u64 *strtab, unsigned int nent)
1195{
1196 unsigned int i;
1197 struct arm_smmu_strtab_ent ste = { .assigned = false };
1198
1199 for (i = 0; i < nent; ++i) {
1200 arm_smmu_write_strtab_ent(NULL, -1, strtab, &ste);
1201 strtab += STRTAB_STE_DWORDS;
1202 }
1203}
1204
1205static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1206{
1207 size_t size;
1208 void *strtab;
1209 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1210 struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[sid >> STRTAB_SPLIT];
1211
1212 if (desc->l2ptr)
1213 return 0;
1214
1215 size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
1216 strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
1217
1218 desc->span = STRTAB_SPLIT + 1;
1219 desc->l2ptr = dmam_alloc_coherent(smmu->dev, size, &desc->l2ptr_dma,
1220 GFP_KERNEL | __GFP_ZERO);
1221 if (!desc->l2ptr) {
1222 dev_err(smmu->dev,
1223 "failed to allocate l2 stream table for SID %u\n",
1224 sid);
1225 return -ENOMEM;
1226 }
1227
1228 arm_smmu_init_bypass_stes(desc->l2ptr, 1 << STRTAB_SPLIT);
1229 arm_smmu_write_strtab_l1_desc(strtab, desc);
1230 return 0;
1231}
1232
1233/* IRQ and event handlers */
1234static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
1235{
1236 int i;
1237 struct arm_smmu_device *smmu = dev;
1238 struct arm_smmu_queue *q = &smmu->evtq.q;
1239 u64 evt[EVTQ_ENT_DWORDS];
1240
1241 do {
1242 while (!queue_remove_raw(q, evt)) {
1243 u8 id = FIELD_GET(EVTQ_0_ID, evt[0]);
1244
1245 dev_info(smmu->dev, "event 0x%02x received:\n", id);
1246 for (i = 0; i < ARRAY_SIZE(evt); ++i)
1247 dev_info(smmu->dev, "\t0x%016llx\n",
1248 (unsigned long long)evt[i]);
1249
1250 }
1251
1252 /*
1253 * Not much we can do on overflow, so scream and pretend we're
1254 * trying harder.
1255 */
1256 if (queue_sync_prod(q) == -EOVERFLOW)
1257 dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
1258 } while (!queue_empty(q));
1259
1260 /* Sync our overflow flag, as we believe we're up to speed */
1261 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1262 return IRQ_HANDLED;
1263}
1264
1265static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt)
1266{
1267 u32 sid, ssid;
1268 u16 grpid;
1269 bool ssv, last;
1270
1271 sid = FIELD_GET(PRIQ_0_SID, evt[0]);
1272 ssv = FIELD_GET(PRIQ_0_SSID_V, evt[0]);
1273 ssid = ssv ? FIELD_GET(PRIQ_0_SSID, evt[0]) : 0;
1274 last = FIELD_GET(PRIQ_0_PRG_LAST, evt[0]);
1275 grpid = FIELD_GET(PRIQ_1_PRG_IDX, evt[1]);
1276
1277 dev_info(smmu->dev, "unexpected PRI request received:\n");
1278 dev_info(smmu->dev,
1279 "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
1280 sid, ssid, grpid, last ? "L" : "",
1281 evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
1282 evt[0] & PRIQ_0_PERM_READ ? "R" : "",
1283 evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
1284 evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
1285 evt[1] & PRIQ_1_ADDR_MASK);
1286
1287 if (last) {
1288 struct arm_smmu_cmdq_ent cmd = {
1289 .opcode = CMDQ_OP_PRI_RESP,
1290 .substream_valid = ssv,
1291 .pri = {
1292 .sid = sid,
1293 .ssid = ssid,
1294 .grpid = grpid,
1295 .resp = PRI_RESP_DENY,
1296 },
1297 };
1298
1299 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1300 }
1301}
1302
1303static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
1304{
1305 struct arm_smmu_device *smmu = dev;
1306 struct arm_smmu_queue *q = &smmu->priq.q;
1307 u64 evt[PRIQ_ENT_DWORDS];
1308
1309 do {
1310 while (!queue_remove_raw(q, evt))
1311 arm_smmu_handle_ppr(smmu, evt);
1312
1313 if (queue_sync_prod(q) == -EOVERFLOW)
1314 dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
1315 } while (!queue_empty(q));
1316
1317 /* Sync our overflow flag, as we believe we're up to speed */
1318 q->cons = Q_OVF(q, q->prod) | Q_WRP(q, q->cons) | Q_IDX(q, q->cons);
1319 writel(q->cons, q->cons_reg);
1320 return IRQ_HANDLED;
1321}
1322
1323static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
1324
1325static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
1326{
1327 u32 gerror, gerrorn, active;
1328 struct arm_smmu_device *smmu = dev;
1329
1330 gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
1331 gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
1332
1333 active = gerror ^ gerrorn;
1334 if (!(active & GERROR_ERR_MASK))
1335 return IRQ_NONE; /* No errors pending */
1336
1337 dev_warn(smmu->dev,
1338 "unexpected global error reported (0x%08x), this could be serious\n",
1339 active);
1340
1341 if (active & GERROR_SFM_ERR) {
1342 dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
1343 arm_smmu_device_disable(smmu);
1344 }
1345
1346 if (active & GERROR_MSI_GERROR_ABT_ERR)
1347 dev_warn(smmu->dev, "GERROR MSI write aborted\n");
1348
1349 if (active & GERROR_MSI_PRIQ_ABT_ERR)
1350 dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
1351
1352 if (active & GERROR_MSI_EVTQ_ABT_ERR)
1353 dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
1354
1355 if (active & GERROR_MSI_CMDQ_ABT_ERR)
1356 dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
1357
1358 if (active & GERROR_PRIQ_ABT_ERR)
1359 dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
1360
1361 if (active & GERROR_EVTQ_ABT_ERR)
1362 dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
1363
1364 if (active & GERROR_CMDQ_ERR)
1365 arm_smmu_cmdq_skip_err(smmu);
1366
1367 writel(gerror, smmu->base + ARM_SMMU_GERRORN);
1368 return IRQ_HANDLED;
1369}
1370
1371static irqreturn_t arm_smmu_combined_irq_thread(int irq, void *dev)
1372{
1373 struct arm_smmu_device *smmu = dev;
1374
1375 arm_smmu_evtq_thread(irq, dev);
1376 if (smmu->features & ARM_SMMU_FEAT_PRI)
1377 arm_smmu_priq_thread(irq, dev);
1378
1379 return IRQ_HANDLED;
1380}
1381
1382static irqreturn_t arm_smmu_combined_irq_handler(int irq, void *dev)
1383{
1384 arm_smmu_gerror_handler(irq, dev);
1385 return IRQ_WAKE_THREAD;
1386}
1387
1388/* IO_PGTABLE API */
1389static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu)
1390{
1391 arm_smmu_cmdq_issue_sync(smmu);
1392}
1393
1394static void arm_smmu_tlb_sync(void *cookie)
1395{
1396 struct arm_smmu_domain *smmu_domain = cookie;
1397 __arm_smmu_tlb_sync(smmu_domain->smmu);
1398}
1399
1400static void arm_smmu_tlb_inv_context(void *cookie)
1401{
1402 struct arm_smmu_domain *smmu_domain = cookie;
1403 struct arm_smmu_device *smmu = smmu_domain->smmu;
1404 struct arm_smmu_cmdq_ent cmd;
1405
1406 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1407 cmd.opcode = CMDQ_OP_TLBI_NH_ASID;
1408 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1409 cmd.tlbi.vmid = 0;
1410 } else {
1411 cmd.opcode = CMDQ_OP_TLBI_S12_VMALL;
1412 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1413 }
1414
1415 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1416 __arm_smmu_tlb_sync(smmu);
1417}
1418
1419static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
1420 size_t granule, bool leaf, void *cookie)
1421{
1422 struct arm_smmu_domain *smmu_domain = cookie;
1423 struct arm_smmu_device *smmu = smmu_domain->smmu;
1424 struct arm_smmu_cmdq_ent cmd = {
1425 .tlbi = {
1426 .leaf = leaf,
1427 .addr = iova,
1428 },
1429 };
1430
1431 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1432 cmd.opcode = CMDQ_OP_TLBI_NH_VA;
1433 cmd.tlbi.asid = smmu_domain->s1_cfg.cd.asid;
1434 } else {
1435 cmd.opcode = CMDQ_OP_TLBI_S2_IPA;
1436 cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid;
1437 }
1438
1439 do {
1440 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1441 cmd.tlbi.addr += granule;
1442 } while (size -= granule);
1443}
1444
1445static const struct iommu_gather_ops arm_smmu_gather_ops = {
1446 .tlb_flush_all = arm_smmu_tlb_inv_context,
1447 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
1448 .tlb_sync = arm_smmu_tlb_sync,
1449};
1450
1451/* IOMMU API */
1452static bool arm_smmu_capable(enum iommu_cap cap)
1453{
1454 switch (cap) {
1455 case IOMMU_CAP_CACHE_COHERENCY:
1456 return true;
1457 case IOMMU_CAP_NOEXEC:
1458 return true;
1459 default:
1460 return false;
1461 }
1462}
1463
1464static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
1465{
1466 struct arm_smmu_domain *smmu_domain;
1467
1468 if (type != IOMMU_DOMAIN_UNMANAGED &&
1469 type != IOMMU_DOMAIN_DMA &&
1470 type != IOMMU_DOMAIN_IDENTITY)
1471 return NULL;
1472
1473 /*
1474 * Allocate the domain and initialise some of its data structures.
1475 * We can't really do anything meaningful until we've added a
1476 * master.
1477 */
1478 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1479 if (!smmu_domain)
1480 return NULL;
1481
1482 if (type == IOMMU_DOMAIN_DMA &&
1483 iommu_get_dma_cookie(&smmu_domain->domain)) {
1484 kfree(smmu_domain);
1485 return NULL;
1486 }
1487
1488 mutex_init(&smmu_domain->init_mutex);
1489 return &smmu_domain->domain;
1490}
1491
1492static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
1493{
1494 int idx, size = 1 << span;
1495
1496 do {
1497 idx = find_first_zero_bit(map, size);
1498 if (idx == size)
1499 return -ENOSPC;
1500 } while (test_and_set_bit(idx, map));
1501
1502 return idx;
1503}
1504
1505static void arm_smmu_bitmap_free(unsigned long *map, int idx)
1506{
1507 clear_bit(idx, map);
1508}
1509
1510static void arm_smmu_domain_free(struct iommu_domain *domain)
1511{
1512 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1513 struct arm_smmu_device *smmu = smmu_domain->smmu;
1514
1515 iommu_put_dma_cookie(domain);
1516 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
1517
1518 /* Free the CD and ASID, if we allocated them */
1519 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1520 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1521
1522 if (cfg->cdptr) {
1523 dmam_free_coherent(smmu_domain->smmu->dev,
1524 CTXDESC_CD_DWORDS << 3,
1525 cfg->cdptr,
1526 cfg->cdptr_dma);
1527
1528 arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
1529 }
1530 } else {
1531 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1532 if (cfg->vmid)
1533 arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
1534 }
1535
1536 kfree(smmu_domain);
1537}
1538
1539static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
1540 struct io_pgtable_cfg *pgtbl_cfg)
1541{
1542 int ret;
1543 int asid;
1544 struct arm_smmu_device *smmu = smmu_domain->smmu;
1545 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1546
1547 asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
1548 if (asid < 0)
1549 return asid;
1550
1551 cfg->cdptr = dmam_alloc_coherent(smmu->dev, CTXDESC_CD_DWORDS << 3,
1552 &cfg->cdptr_dma,
1553 GFP_KERNEL | __GFP_ZERO);
1554 if (!cfg->cdptr) {
1555 dev_warn(smmu->dev, "failed to allocate context descriptor\n");
1556 ret = -ENOMEM;
1557 goto out_free_asid;
1558 }
1559
1560 cfg->cd.asid = (u16)asid;
1561 cfg->cd.ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
1562 cfg->cd.tcr = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
1563 cfg->cd.mair = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
1564 return 0;
1565
1566out_free_asid:
1567 arm_smmu_bitmap_free(smmu->asid_map, asid);
1568 return ret;
1569}
1570
1571static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
1572 struct io_pgtable_cfg *pgtbl_cfg)
1573{
1574 int vmid;
1575 struct arm_smmu_device *smmu = smmu_domain->smmu;
1576 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
1577
1578 vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
1579 if (vmid < 0)
1580 return vmid;
1581
1582 cfg->vmid = (u16)vmid;
1583 cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
1584 cfg->vtcr = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
1585 return 0;
1586}
1587
1588static int arm_smmu_domain_finalise(struct iommu_domain *domain)
1589{
1590 int ret;
1591 unsigned long ias, oas;
1592 enum io_pgtable_fmt fmt;
1593 struct io_pgtable_cfg pgtbl_cfg;
1594 struct io_pgtable_ops *pgtbl_ops;
1595 int (*finalise_stage_fn)(struct arm_smmu_domain *,
1596 struct io_pgtable_cfg *);
1597 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1598 struct arm_smmu_device *smmu = smmu_domain->smmu;
1599
1600 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
1601 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
1602 return 0;
1603 }
1604
1605 /* Restrict the stage to what we can actually support */
1606 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
1607 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
1608 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
1609 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1610
1611 switch (smmu_domain->stage) {
1612 case ARM_SMMU_DOMAIN_S1:
1613 ias = (smmu->features & ARM_SMMU_FEAT_VAX) ? 52 : 48;
1614 ias = min_t(unsigned long, ias, VA_BITS);
1615 oas = smmu->ias;
1616 fmt = ARM_64_LPAE_S1;
1617 finalise_stage_fn = arm_smmu_domain_finalise_s1;
1618 break;
1619 case ARM_SMMU_DOMAIN_NESTED:
1620 case ARM_SMMU_DOMAIN_S2:
1621 ias = smmu->ias;
1622 oas = smmu->oas;
1623 fmt = ARM_64_LPAE_S2;
1624 finalise_stage_fn = arm_smmu_domain_finalise_s2;
1625 break;
1626 default:
1627 return -EINVAL;
1628 }
1629
1630 pgtbl_cfg = (struct io_pgtable_cfg) {
1631 .pgsize_bitmap = smmu->pgsize_bitmap,
1632 .ias = ias,
1633 .oas = oas,
1634 .tlb = &arm_smmu_gather_ops,
1635 .iommu_dev = smmu->dev,
1636 };
1637
1638 if (smmu->features & ARM_SMMU_FEAT_COHERENCY)
1639 pgtbl_cfg.quirks = IO_PGTABLE_QUIRK_NO_DMA;
1640
1641 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
1642 if (!pgtbl_ops)
1643 return -ENOMEM;
1644
1645 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
1646 domain->geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1;
1647 domain->geometry.force_aperture = true;
1648
1649 ret = finalise_stage_fn(smmu_domain, &pgtbl_cfg);
1650 if (ret < 0) {
1651 free_io_pgtable_ops(pgtbl_ops);
1652 return ret;
1653 }
1654
1655 smmu_domain->pgtbl_ops = pgtbl_ops;
1656 return 0;
1657}
1658
1659static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
1660{
1661 __le64 *step;
1662 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1663
1664 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1665 struct arm_smmu_strtab_l1_desc *l1_desc;
1666 int idx;
1667
1668 /* Two-level walk */
1669 idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
1670 l1_desc = &cfg->l1_desc[idx];
1671 idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
1672 step = &l1_desc->l2ptr[idx];
1673 } else {
1674 /* Simple linear lookup */
1675 step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
1676 }
1677
1678 return step;
1679}
1680
1681static void arm_smmu_install_ste_for_dev(struct iommu_fwspec *fwspec)
1682{
1683 int i, j;
1684 struct arm_smmu_master_data *master = fwspec->iommu_priv;
1685 struct arm_smmu_device *smmu = master->smmu;
1686
1687 for (i = 0; i < fwspec->num_ids; ++i) {
1688 u32 sid = fwspec->ids[i];
1689 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
1690
1691 /* Bridged PCI devices may end up with duplicated IDs */
1692 for (j = 0; j < i; j++)
1693 if (fwspec->ids[j] == sid)
1694 break;
1695 if (j < i)
1696 continue;
1697
1698 arm_smmu_write_strtab_ent(smmu, sid, step, &master->ste);
1699 }
1700}
1701
1702static void arm_smmu_detach_dev(struct device *dev)
1703{
1704 struct arm_smmu_master_data *master = dev->iommu_fwspec->iommu_priv;
1705
1706 master->ste.assigned = false;
1707 arm_smmu_install_ste_for_dev(dev->iommu_fwspec);
1708}
1709
1710static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1711{
1712 int ret = 0;
1713 struct arm_smmu_device *smmu;
1714 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1715 struct arm_smmu_master_data *master;
1716 struct arm_smmu_strtab_ent *ste;
1717
1718 if (!dev->iommu_fwspec)
1719 return -ENOENT;
1720
1721 master = dev->iommu_fwspec->iommu_priv;
1722 smmu = master->smmu;
1723 ste = &master->ste;
1724
1725 /* Already attached to a different domain? */
1726 if (ste->assigned)
1727 arm_smmu_detach_dev(dev);
1728
1729 mutex_lock(&smmu_domain->init_mutex);
1730
1731 if (!smmu_domain->smmu) {
1732 smmu_domain->smmu = smmu;
1733 ret = arm_smmu_domain_finalise(domain);
1734 if (ret) {
1735 smmu_domain->smmu = NULL;
1736 goto out_unlock;
1737 }
1738 } else if (smmu_domain->smmu != smmu) {
1739 dev_err(dev,
1740 "cannot attach to SMMU %s (upstream of %s)\n",
1741 dev_name(smmu_domain->smmu->dev),
1742 dev_name(smmu->dev));
1743 ret = -ENXIO;
1744 goto out_unlock;
1745 }
1746
1747 ste->assigned = true;
1748
1749 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS) {
1750 ste->s1_cfg = NULL;
1751 ste->s2_cfg = NULL;
1752 } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
1753 ste->s1_cfg = &smmu_domain->s1_cfg;
1754 ste->s2_cfg = NULL;
1755 arm_smmu_write_ctx_desc(smmu, ste->s1_cfg);
1756 } else {
1757 ste->s1_cfg = NULL;
1758 ste->s2_cfg = &smmu_domain->s2_cfg;
1759 }
1760
1761 arm_smmu_install_ste_for_dev(dev->iommu_fwspec);
1762out_unlock:
1763 mutex_unlock(&smmu_domain->init_mutex);
1764 return ret;
1765}
1766
1767static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1768 phys_addr_t paddr, size_t size, int prot)
1769{
1770 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1771
1772 if (!ops)
1773 return -ENODEV;
1774
1775 return ops->map(ops, iova, paddr, size, prot);
1776}
1777
1778static size_t
1779arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1780{
1781 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1782
1783 if (!ops)
1784 return 0;
1785
1786 return ops->unmap(ops, iova, size);
1787}
1788
1789static void arm_smmu_iotlb_sync(struct iommu_domain *domain)
1790{
1791 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1792
1793 if (smmu)
1794 __arm_smmu_tlb_sync(smmu);
1795}
1796
1797static phys_addr_t
1798arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1799{
1800 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1801
1802 if (domain->type == IOMMU_DOMAIN_IDENTITY)
1803 return iova;
1804
1805 if (!ops)
1806 return 0;
1807
1808 return ops->iova_to_phys(ops, iova);
1809}
1810
1811static struct platform_driver arm_smmu_driver;
1812
1813static int arm_smmu_match_node(struct device *dev, void *data)
1814{
1815 return dev->fwnode == data;
1816}
1817
1818static
1819struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
1820{
1821 struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
1822 fwnode, arm_smmu_match_node);
1823 put_device(dev);
1824 return dev ? dev_get_drvdata(dev) : NULL;
1825}
1826
1827static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
1828{
1829 unsigned long limit = smmu->strtab_cfg.num_l1_ents;
1830
1831 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
1832 limit *= 1UL << STRTAB_SPLIT;
1833
1834 return sid < limit;
1835}
1836
1837static struct iommu_ops arm_smmu_ops;
1838
1839static int arm_smmu_add_device(struct device *dev)
1840{
1841 int i, ret;
1842 struct arm_smmu_device *smmu;
1843 struct arm_smmu_master_data *master;
1844 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1845 struct iommu_group *group;
1846
1847 if (!fwspec || fwspec->ops != &arm_smmu_ops)
1848 return -ENODEV;
1849 /*
1850 * We _can_ actually withstand dodgy bus code re-calling add_device()
1851 * without an intervening remove_device()/of_xlate() sequence, but
1852 * we're not going to do so quietly...
1853 */
1854 if (WARN_ON_ONCE(fwspec->iommu_priv)) {
1855 master = fwspec->iommu_priv;
1856 smmu = master->smmu;
1857 } else {
1858 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
1859 if (!smmu)
1860 return -ENODEV;
1861 master = kzalloc(sizeof(*master), GFP_KERNEL);
1862 if (!master)
1863 return -ENOMEM;
1864
1865 master->smmu = smmu;
1866 fwspec->iommu_priv = master;
1867 }
1868
1869 /* Check the SIDs are in range of the SMMU and our stream table */
1870 for (i = 0; i < fwspec->num_ids; i++) {
1871 u32 sid = fwspec->ids[i];
1872
1873 if (!arm_smmu_sid_in_range(smmu, sid))
1874 return -ERANGE;
1875
1876 /* Ensure l2 strtab is initialised */
1877 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
1878 ret = arm_smmu_init_l2_strtab(smmu, sid);
1879 if (ret)
1880 return ret;
1881 }
1882 }
1883
1884 group = iommu_group_get_for_dev(dev);
1885 if (!IS_ERR(group)) {
1886 iommu_group_put(group);
1887 iommu_device_link(&smmu->iommu, dev);
1888 }
1889
1890 return PTR_ERR_OR_ZERO(group);
1891}
1892
1893static void arm_smmu_remove_device(struct device *dev)
1894{
1895 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1896 struct arm_smmu_master_data *master;
1897 struct arm_smmu_device *smmu;
1898
1899 if (!fwspec || fwspec->ops != &arm_smmu_ops)
1900 return;
1901
1902 master = fwspec->iommu_priv;
1903 smmu = master->smmu;
1904 if (master && master->ste.assigned)
1905 arm_smmu_detach_dev(dev);
1906 iommu_group_remove_device(dev);
1907 iommu_device_unlink(&smmu->iommu, dev);
1908 kfree(master);
1909 iommu_fwspec_free(dev);
1910}
1911
1912static struct iommu_group *arm_smmu_device_group(struct device *dev)
1913{
1914 struct iommu_group *group;
1915
1916 /*
1917 * We don't support devices sharing stream IDs other than PCI RID
1918 * aliases, since the necessary ID-to-device lookup becomes rather
1919 * impractical given a potential sparse 32-bit stream ID space.
1920 */
1921 if (dev_is_pci(dev))
1922 group = pci_device_group(dev);
1923 else
1924 group = generic_device_group(dev);
1925
1926 return group;
1927}
1928
1929static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1930 enum iommu_attr attr, void *data)
1931{
1932 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1933
1934 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
1935 return -EINVAL;
1936
1937 switch (attr) {
1938 case DOMAIN_ATTR_NESTING:
1939 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1940 return 0;
1941 default:
1942 return -ENODEV;
1943 }
1944}
1945
1946static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1947 enum iommu_attr attr, void *data)
1948{
1949 int ret = 0;
1950 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1951
1952 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
1953 return -EINVAL;
1954
1955 mutex_lock(&smmu_domain->init_mutex);
1956
1957 switch (attr) {
1958 case DOMAIN_ATTR_NESTING:
1959 if (smmu_domain->smmu) {
1960 ret = -EPERM;
1961 goto out_unlock;
1962 }
1963
1964 if (*(int *)data)
1965 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1966 else
1967 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1968
1969 break;
1970 default:
1971 ret = -ENODEV;
1972 }
1973
1974out_unlock:
1975 mutex_unlock(&smmu_domain->init_mutex);
1976 return ret;
1977}
1978
1979static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1980{
1981 return iommu_fwspec_add_ids(dev, args->args, 1);
1982}
1983
1984static void arm_smmu_get_resv_regions(struct device *dev,
1985 struct list_head *head)
1986{
1987 struct iommu_resv_region *region;
1988 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1989
1990 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
1991 prot, IOMMU_RESV_SW_MSI);
1992 if (!region)
1993 return;
1994
1995 list_add_tail(&region->list, head);
1996
1997 iommu_dma_get_resv_regions(dev, head);
1998}
1999
2000static void arm_smmu_put_resv_regions(struct device *dev,
2001 struct list_head *head)
2002{
2003 struct iommu_resv_region *entry, *next;
2004
2005 list_for_each_entry_safe(entry, next, head, list)
2006 kfree(entry);
2007}
2008
2009static struct iommu_ops arm_smmu_ops = {
2010 .capable = arm_smmu_capable,
2011 .domain_alloc = arm_smmu_domain_alloc,
2012 .domain_free = arm_smmu_domain_free,
2013 .attach_dev = arm_smmu_attach_dev,
2014 .map = arm_smmu_map,
2015 .unmap = arm_smmu_unmap,
2016 .flush_iotlb_all = arm_smmu_iotlb_sync,
2017 .iotlb_sync = arm_smmu_iotlb_sync,
2018 .iova_to_phys = arm_smmu_iova_to_phys,
2019 .add_device = arm_smmu_add_device,
2020 .remove_device = arm_smmu_remove_device,
2021 .device_group = arm_smmu_device_group,
2022 .domain_get_attr = arm_smmu_domain_get_attr,
2023 .domain_set_attr = arm_smmu_domain_set_attr,
2024 .of_xlate = arm_smmu_of_xlate,
2025 .get_resv_regions = arm_smmu_get_resv_regions,
2026 .put_resv_regions = arm_smmu_put_resv_regions,
2027 .pgsize_bitmap = -1UL, /* Restricted during device attach */
2028};
2029
2030/* Probing and initialisation functions */
2031static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
2032 struct arm_smmu_queue *q,
2033 unsigned long prod_off,
2034 unsigned long cons_off,
2035 size_t dwords)
2036{
2037 size_t qsz = ((1 << q->max_n_shift) * dwords) << 3;
2038
2039 q->base = dmam_alloc_coherent(smmu->dev, qsz, &q->base_dma, GFP_KERNEL);
2040 if (!q->base) {
2041 dev_err(smmu->dev, "failed to allocate queue (0x%zx bytes)\n",
2042 qsz);
2043 return -ENOMEM;
2044 }
2045
2046 q->prod_reg = arm_smmu_page1_fixup(prod_off, smmu);
2047 q->cons_reg = arm_smmu_page1_fixup(cons_off, smmu);
2048 q->ent_dwords = dwords;
2049
2050 q->q_base = Q_BASE_RWA;
2051 q->q_base |= q->base_dma & Q_BASE_ADDR_MASK;
2052 q->q_base |= FIELD_PREP(Q_BASE_LOG2SIZE, q->max_n_shift);
2053
2054 q->prod = q->cons = 0;
2055 return 0;
2056}
2057
2058static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
2059{
2060 int ret;
2061
2062 /* cmdq */
2063 spin_lock_init(&smmu->cmdq.lock);
2064 ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
2065 ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS);
2066 if (ret)
2067 return ret;
2068
2069 /* evtq */
2070 ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
2071 ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS);
2072 if (ret)
2073 return ret;
2074
2075 /* priq */
2076 if (!(smmu->features & ARM_SMMU_FEAT_PRI))
2077 return 0;
2078
2079 return arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
2080 ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS);
2081}
2082
2083static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
2084{
2085 unsigned int i;
2086 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2087 size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
2088 void *strtab = smmu->strtab_cfg.strtab;
2089
2090 cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
2091 if (!cfg->l1_desc) {
2092 dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
2093 return -ENOMEM;
2094 }
2095
2096 for (i = 0; i < cfg->num_l1_ents; ++i) {
2097 arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
2098 strtab += STRTAB_L1_DESC_DWORDS << 3;
2099 }
2100
2101 return 0;
2102}
2103
2104static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
2105{
2106 void *strtab;
2107 u64 reg;
2108 u32 size, l1size;
2109 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2110
2111 /* Calculate the L1 size, capped to the SIDSIZE. */
2112 size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
2113 size = min(size, smmu->sid_bits - STRTAB_SPLIT);
2114 cfg->num_l1_ents = 1 << size;
2115
2116 size += STRTAB_SPLIT;
2117 if (size < smmu->sid_bits)
2118 dev_warn(smmu->dev,
2119 "2-level strtab only covers %u/%u bits of SID\n",
2120 size, smmu->sid_bits);
2121
2122 l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
2123 strtab = dmam_alloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
2124 GFP_KERNEL | __GFP_ZERO);
2125 if (!strtab) {
2126 dev_err(smmu->dev,
2127 "failed to allocate l1 stream table (%u bytes)\n",
2128 size);
2129 return -ENOMEM;
2130 }
2131 cfg->strtab = strtab;
2132
2133 /* Configure strtab_base_cfg for 2 levels */
2134 reg = FIELD_PREP(STRTAB_BASE_CFG_FMT, STRTAB_BASE_CFG_FMT_2LVL);
2135 reg |= FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, size);
2136 reg |= FIELD_PREP(STRTAB_BASE_CFG_SPLIT, STRTAB_SPLIT);
2137 cfg->strtab_base_cfg = reg;
2138
2139 return arm_smmu_init_l1_strtab(smmu);
2140}
2141
2142static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
2143{
2144 void *strtab;
2145 u64 reg;
2146 u32 size;
2147 struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2148
2149 size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
2150 strtab = dmam_alloc_coherent(smmu->dev, size, &cfg->strtab_dma,
2151 GFP_KERNEL | __GFP_ZERO);
2152 if (!strtab) {
2153 dev_err(smmu->dev,
2154 "failed to allocate linear stream table (%u bytes)\n",
2155 size);
2156 return -ENOMEM;
2157 }
2158 cfg->strtab = strtab;
2159 cfg->num_l1_ents = 1 << smmu->sid_bits;
2160
2161 /* Configure strtab_base_cfg for a linear table covering all SIDs */
2162 reg = FIELD_PREP(STRTAB_BASE_CFG_FMT, STRTAB_BASE_CFG_FMT_LINEAR);
2163 reg |= FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, smmu->sid_bits);
2164 cfg->strtab_base_cfg = reg;
2165
2166 arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
2167 return 0;
2168}
2169
2170static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
2171{
2172 u64 reg;
2173 int ret;
2174
2175 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
2176 ret = arm_smmu_init_strtab_2lvl(smmu);
2177 else
2178 ret = arm_smmu_init_strtab_linear(smmu);
2179
2180 if (ret)
2181 return ret;
2182
2183 /* Set the strtab base address */
2184 reg = smmu->strtab_cfg.strtab_dma & STRTAB_BASE_ADDR_MASK;
2185 reg |= STRTAB_BASE_RA;
2186 smmu->strtab_cfg.strtab_base = reg;
2187
2188 /* Allocate the first VMID for stage-2 bypass STEs */
2189 set_bit(0, smmu->vmid_map);
2190 return 0;
2191}
2192
2193static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
2194{
2195 int ret;
2196
2197 ret = arm_smmu_init_queues(smmu);
2198 if (ret)
2199 return ret;
2200
2201 return arm_smmu_init_strtab(smmu);
2202}
2203
2204static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
2205 unsigned int reg_off, unsigned int ack_off)
2206{
2207 u32 reg;
2208
2209 writel_relaxed(val, smmu->base + reg_off);
2210 return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
2211 1, ARM_SMMU_POLL_TIMEOUT_US);
2212}
2213
2214/* GBPA is "special" */
2215static int arm_smmu_update_gbpa(struct arm_smmu_device *smmu, u32 set, u32 clr)
2216{
2217 int ret;
2218 u32 reg, __iomem *gbpa = smmu->base + ARM_SMMU_GBPA;
2219
2220 ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
2221 1, ARM_SMMU_POLL_TIMEOUT_US);
2222 if (ret)
2223 return ret;
2224
2225 reg &= ~clr;
2226 reg |= set;
2227 writel_relaxed(reg | GBPA_UPDATE, gbpa);
2228 ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
2229 1, ARM_SMMU_POLL_TIMEOUT_US);
2230
2231 if (ret)
2232 dev_err(smmu->dev, "GBPA not responding to update\n");
2233 return ret;
2234}
2235
2236static void arm_smmu_free_msis(void *data)
2237{
2238 struct device *dev = data;
2239 platform_msi_domain_free_irqs(dev);
2240}
2241
2242static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
2243{
2244 phys_addr_t doorbell;
2245 struct device *dev = msi_desc_to_dev(desc);
2246 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2247 phys_addr_t *cfg = arm_smmu_msi_cfg[desc->platform.msi_index];
2248
2249 doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
2250 doorbell &= MSI_CFG0_ADDR_MASK;
2251
2252 writeq_relaxed(doorbell, smmu->base + cfg[0]);
2253 writel_relaxed(msg->data, smmu->base + cfg[1]);
2254 writel_relaxed(ARM_SMMU_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]);
2255}
2256
2257static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
2258{
2259 struct msi_desc *desc;
2260 int ret, nvec = ARM_SMMU_MAX_MSIS;
2261 struct device *dev = smmu->dev;
2262
2263 /* Clear the MSI address regs */
2264 writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
2265 writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
2266
2267 if (smmu->features & ARM_SMMU_FEAT_PRI)
2268 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
2269 else
2270 nvec--;
2271
2272 if (!(smmu->features & ARM_SMMU_FEAT_MSI))
2273 return;
2274
2275 if (!dev->msi_domain) {
2276 dev_info(smmu->dev, "msi_domain absent - falling back to wired irqs\n");
2277 return;
2278 }
2279
2280 /* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */
2281 ret = platform_msi_domain_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg);
2282 if (ret) {
2283 dev_warn(dev, "failed to allocate MSIs - falling back to wired irqs\n");
2284 return;
2285 }
2286
2287 for_each_msi_entry(desc, dev) {
2288 switch (desc->platform.msi_index) {
2289 case EVTQ_MSI_INDEX:
2290 smmu->evtq.q.irq = desc->irq;
2291 break;
2292 case GERROR_MSI_INDEX:
2293 smmu->gerr_irq = desc->irq;
2294 break;
2295 case PRIQ_MSI_INDEX:
2296 smmu->priq.q.irq = desc->irq;
2297 break;
2298 default: /* Unknown */
2299 continue;
2300 }
2301 }
2302
2303 /* Add callback to free MSIs on teardown */
2304 devm_add_action(dev, arm_smmu_free_msis, dev);
2305}
2306
2307static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
2308{
2309 int irq, ret;
2310
2311 arm_smmu_setup_msis(smmu);
2312
2313 /* Request interrupt lines */
2314 irq = smmu->evtq.q.irq;
2315 if (irq) {
2316 ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
2317 arm_smmu_evtq_thread,
2318 IRQF_ONESHOT,
2319 "arm-smmu-v3-evtq", smmu);
2320 if (ret < 0)
2321 dev_warn(smmu->dev, "failed to enable evtq irq\n");
2322 } else {
2323 dev_warn(smmu->dev, "no evtq irq - events will not be reported!\n");
2324 }
2325
2326 irq = smmu->gerr_irq;
2327 if (irq) {
2328 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
2329 0, "arm-smmu-v3-gerror", smmu);
2330 if (ret < 0)
2331 dev_warn(smmu->dev, "failed to enable gerror irq\n");
2332 } else {
2333 dev_warn(smmu->dev, "no gerr irq - errors will not be reported!\n");
2334 }
2335
2336 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2337 irq = smmu->priq.q.irq;
2338 if (irq) {
2339 ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
2340 arm_smmu_priq_thread,
2341 IRQF_ONESHOT,
2342 "arm-smmu-v3-priq",
2343 smmu);
2344 if (ret < 0)
2345 dev_warn(smmu->dev,
2346 "failed to enable priq irq\n");
2347 } else {
2348 dev_warn(smmu->dev, "no priq irq - PRI will be broken\n");
2349 }
2350 }
2351}
2352
2353static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
2354{
2355 int ret, irq;
2356 u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
2357
2358 /* Disable IRQs first */
2359 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
2360 ARM_SMMU_IRQ_CTRLACK);
2361 if (ret) {
2362 dev_err(smmu->dev, "failed to disable irqs\n");
2363 return ret;
2364 }
2365
2366 irq = smmu->combined_irq;
2367 if (irq) {
2368 /*
2369 * Cavium ThunderX2 implementation doesn't not support unique
2370 * irq lines. Use single irq line for all the SMMUv3 interrupts.
2371 */
2372 ret = devm_request_threaded_irq(smmu->dev, irq,
2373 arm_smmu_combined_irq_handler,
2374 arm_smmu_combined_irq_thread,
2375 IRQF_ONESHOT,
2376 "arm-smmu-v3-combined-irq", smmu);
2377 if (ret < 0)
2378 dev_warn(smmu->dev, "failed to enable combined irq\n");
2379 } else
2380 arm_smmu_setup_unique_irqs(smmu);
2381
2382 if (smmu->features & ARM_SMMU_FEAT_PRI)
2383 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
2384
2385 /* Enable interrupt generation on the SMMU */
2386 ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
2387 ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
2388 if (ret)
2389 dev_warn(smmu->dev, "failed to enable irqs\n");
2390
2391 return 0;
2392}
2393
2394static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
2395{
2396 int ret;
2397
2398 ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
2399 if (ret)
2400 dev_err(smmu->dev, "failed to clear cr0\n");
2401
2402 return ret;
2403}
2404
2405static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)
2406{
2407 int ret;
2408 u32 reg, enables;
2409 struct arm_smmu_cmdq_ent cmd;
2410
2411 /* Clear CR0 and sync (disables SMMU and queue processing) */
2412 reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
2413 if (reg & CR0_SMMUEN) {
2414 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
2415 WARN_ON(is_kdump_kernel() && !disable_bypass);
2416 arm_smmu_update_gbpa(smmu, GBPA_ABORT, 0);
2417 }
2418
2419 ret = arm_smmu_device_disable(smmu);
2420 if (ret)
2421 return ret;
2422
2423 /* CR1 (table and queue memory attributes) */
2424 reg = FIELD_PREP(CR1_TABLE_SH, ARM_SMMU_SH_ISH) |
2425 FIELD_PREP(CR1_TABLE_OC, CR1_CACHE_WB) |
2426 FIELD_PREP(CR1_TABLE_IC, CR1_CACHE_WB) |
2427 FIELD_PREP(CR1_QUEUE_SH, ARM_SMMU_SH_ISH) |
2428 FIELD_PREP(CR1_QUEUE_OC, CR1_CACHE_WB) |
2429 FIELD_PREP(CR1_QUEUE_IC, CR1_CACHE_WB);
2430 writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
2431
2432 /* CR2 (random crap) */
2433 reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
2434 writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
2435
2436 /* Stream table */
2437 writeq_relaxed(smmu->strtab_cfg.strtab_base,
2438 smmu->base + ARM_SMMU_STRTAB_BASE);
2439 writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
2440 smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
2441
2442 /* Command queue */
2443 writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
2444 writel_relaxed(smmu->cmdq.q.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
2445 writel_relaxed(smmu->cmdq.q.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
2446
2447 enables = CR0_CMDQEN;
2448 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2449 ARM_SMMU_CR0ACK);
2450 if (ret) {
2451 dev_err(smmu->dev, "failed to enable command queue\n");
2452 return ret;
2453 }
2454
2455 /* Invalidate any cached configuration */
2456 cmd.opcode = CMDQ_OP_CFGI_ALL;
2457 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2458 arm_smmu_cmdq_issue_sync(smmu);
2459
2460 /* Invalidate any stale TLB entries */
2461 if (smmu->features & ARM_SMMU_FEAT_HYP) {
2462 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
2463 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2464 }
2465
2466 cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
2467 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2468 arm_smmu_cmdq_issue_sync(smmu);
2469
2470 /* Event queue */
2471 writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
2472 writel_relaxed(smmu->evtq.q.prod,
2473 arm_smmu_page1_fixup(ARM_SMMU_EVTQ_PROD, smmu));
2474 writel_relaxed(smmu->evtq.q.cons,
2475 arm_smmu_page1_fixup(ARM_SMMU_EVTQ_CONS, smmu));
2476
2477 enables |= CR0_EVTQEN;
2478 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2479 ARM_SMMU_CR0ACK);
2480 if (ret) {
2481 dev_err(smmu->dev, "failed to enable event queue\n");
2482 return ret;
2483 }
2484
2485 /* PRI queue */
2486 if (smmu->features & ARM_SMMU_FEAT_PRI) {
2487 writeq_relaxed(smmu->priq.q.q_base,
2488 smmu->base + ARM_SMMU_PRIQ_BASE);
2489 writel_relaxed(smmu->priq.q.prod,
2490 arm_smmu_page1_fixup(ARM_SMMU_PRIQ_PROD, smmu));
2491 writel_relaxed(smmu->priq.q.cons,
2492 arm_smmu_page1_fixup(ARM_SMMU_PRIQ_CONS, smmu));
2493
2494 enables |= CR0_PRIQEN;
2495 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2496 ARM_SMMU_CR0ACK);
2497 if (ret) {
2498 dev_err(smmu->dev, "failed to enable PRI queue\n");
2499 return ret;
2500 }
2501 }
2502
2503 ret = arm_smmu_setup_irqs(smmu);
2504 if (ret) {
2505 dev_err(smmu->dev, "failed to setup irqs\n");
2506 return ret;
2507 }
2508
2509 if (is_kdump_kernel())
2510 enables &= ~(CR0_EVTQEN | CR0_PRIQEN);
2511
2512 /* Enable the SMMU interface, or ensure bypass */
2513 if (!bypass || disable_bypass) {
2514 enables |= CR0_SMMUEN;
2515 } else {
2516 ret = arm_smmu_update_gbpa(smmu, 0, GBPA_ABORT);
2517 if (ret)
2518 return ret;
2519 }
2520 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
2521 ARM_SMMU_CR0ACK);
2522 if (ret) {
2523 dev_err(smmu->dev, "failed to enable SMMU interface\n");
2524 return ret;
2525 }
2526
2527 return 0;
2528}
2529
2530static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
2531{
2532 u32 reg;
2533 bool coherent = smmu->features & ARM_SMMU_FEAT_COHERENCY;
2534
2535 /* IDR0 */
2536 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
2537
2538 /* 2-level structures */
2539 if (FIELD_GET(IDR0_ST_LVL, reg) == IDR0_ST_LVL_2LVL)
2540 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
2541
2542 if (reg & IDR0_CD2L)
2543 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
2544
2545 /*
2546 * Translation table endianness.
2547 * We currently require the same endianness as the CPU, but this
2548 * could be changed later by adding a new IO_PGTABLE_QUIRK.
2549 */
2550 switch (FIELD_GET(IDR0_TTENDIAN, reg)) {
2551 case IDR0_TTENDIAN_MIXED:
2552 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
2553 break;
2554#ifdef __BIG_ENDIAN
2555 case IDR0_TTENDIAN_BE:
2556 smmu->features |= ARM_SMMU_FEAT_TT_BE;
2557 break;
2558#else
2559 case IDR0_TTENDIAN_LE:
2560 smmu->features |= ARM_SMMU_FEAT_TT_LE;
2561 break;
2562#endif
2563 default:
2564 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
2565 return -ENXIO;
2566 }
2567
2568 /* Boolean feature flags */
2569 if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
2570 smmu->features |= ARM_SMMU_FEAT_PRI;
2571
2572 if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
2573 smmu->features |= ARM_SMMU_FEAT_ATS;
2574
2575 if (reg & IDR0_SEV)
2576 smmu->features |= ARM_SMMU_FEAT_SEV;
2577
2578 if (reg & IDR0_MSI)
2579 smmu->features |= ARM_SMMU_FEAT_MSI;
2580
2581 if (reg & IDR0_HYP)
2582 smmu->features |= ARM_SMMU_FEAT_HYP;
2583
2584 /*
2585 * The coherency feature as set by FW is used in preference to the ID
2586 * register, but warn on mismatch.
2587 */
2588 if (!!(reg & IDR0_COHACC) != coherent)
2589 dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n",
2590 coherent ? "true" : "false");
2591
2592 switch (FIELD_GET(IDR0_STALL_MODEL, reg)) {
2593 case IDR0_STALL_MODEL_FORCE:
2594 smmu->features |= ARM_SMMU_FEAT_STALL_FORCE;
2595 /* Fallthrough */
2596 case IDR0_STALL_MODEL_STALL:
2597 smmu->features |= ARM_SMMU_FEAT_STALLS;
2598 }
2599
2600 if (reg & IDR0_S1P)
2601 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
2602
2603 if (reg & IDR0_S2P)
2604 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
2605
2606 if (!(reg & (IDR0_S1P | IDR0_S2P))) {
2607 dev_err(smmu->dev, "no translation support!\n");
2608 return -ENXIO;
2609 }
2610
2611 /* We only support the AArch64 table format at present */
2612 switch (FIELD_GET(IDR0_TTF, reg)) {
2613 case IDR0_TTF_AARCH32_64:
2614 smmu->ias = 40;
2615 /* Fallthrough */
2616 case IDR0_TTF_AARCH64:
2617 break;
2618 default:
2619 dev_err(smmu->dev, "AArch64 table format not supported!\n");
2620 return -ENXIO;
2621 }
2622
2623 /* ASID/VMID sizes */
2624 smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
2625 smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
2626
2627 /* IDR1 */
2628 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
2629 if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
2630 dev_err(smmu->dev, "embedded implementation not supported\n");
2631 return -ENXIO;
2632 }
2633
2634 /* Queue sizes, capped at 4k */
2635 smmu->cmdq.q.max_n_shift = min_t(u32, CMDQ_MAX_SZ_SHIFT,
2636 FIELD_GET(IDR1_CMDQS, reg));
2637 if (!smmu->cmdq.q.max_n_shift) {
2638 /* Odd alignment restrictions on the base, so ignore for now */
2639 dev_err(smmu->dev, "unit-length command queue not supported\n");
2640 return -ENXIO;
2641 }
2642
2643 smmu->evtq.q.max_n_shift = min_t(u32, EVTQ_MAX_SZ_SHIFT,
2644 FIELD_GET(IDR1_EVTQS, reg));
2645 smmu->priq.q.max_n_shift = min_t(u32, PRIQ_MAX_SZ_SHIFT,
2646 FIELD_GET(IDR1_PRIQS, reg));
2647
2648 /* SID/SSID sizes */
2649 smmu->ssid_bits = FIELD_GET(IDR1_SSIDSIZE, reg);
2650 smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg);
2651
2652 /*
2653 * If the SMMU supports fewer bits than would fill a single L2 stream
2654 * table, use a linear table instead.
2655 */
2656 if (smmu->sid_bits <= STRTAB_SPLIT)
2657 smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB;
2658
2659 /* IDR5 */
2660 reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
2661
2662 /* Maximum number of outstanding stalls */
2663 smmu->evtq.max_stalls = FIELD_GET(IDR5_STALL_MAX, reg);
2664
2665 /* Page sizes */
2666 if (reg & IDR5_GRAN64K)
2667 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
2668 if (reg & IDR5_GRAN16K)
2669 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
2670 if (reg & IDR5_GRAN4K)
2671 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
2672
2673 /* Input address size */
2674 if (FIELD_GET(IDR5_VAX, reg) == IDR5_VAX_52_BIT)
2675 smmu->features |= ARM_SMMU_FEAT_VAX;
2676
2677 /* Output address size */
2678 switch (FIELD_GET(IDR5_OAS, reg)) {
2679 case IDR5_OAS_32_BIT:
2680 smmu->oas = 32;
2681 break;
2682 case IDR5_OAS_36_BIT:
2683 smmu->oas = 36;
2684 break;
2685 case IDR5_OAS_40_BIT:
2686 smmu->oas = 40;
2687 break;
2688 case IDR5_OAS_42_BIT:
2689 smmu->oas = 42;
2690 break;
2691 case IDR5_OAS_44_BIT:
2692 smmu->oas = 44;
2693 break;
2694 case IDR5_OAS_52_BIT:
2695 smmu->oas = 52;
2696 smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
2697 break;
2698 default:
2699 dev_info(smmu->dev,
2700 "unknown output address size. Truncating to 48-bit\n");
2701 /* Fallthrough */
2702 case IDR5_OAS_48_BIT:
2703 smmu->oas = 48;
2704 }
2705
2706 if (arm_smmu_ops.pgsize_bitmap == -1UL)
2707 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
2708 else
2709 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
2710
2711 /* Set the DMA mask for our table walker */
2712 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
2713 dev_warn(smmu->dev,
2714 "failed to set DMA mask for table walker\n");
2715
2716 smmu->ias = max(smmu->ias, smmu->oas);
2717
2718 dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
2719 smmu->ias, smmu->oas, smmu->features);
2720 return 0;
2721}
2722
2723#ifdef CONFIG_ACPI
2724static void acpi_smmu_get_options(u32 model, struct arm_smmu_device *smmu)
2725{
2726 switch (model) {
2727 case ACPI_IORT_SMMU_V3_CAVIUM_CN99XX:
2728 smmu->options |= ARM_SMMU_OPT_PAGE0_REGS_ONLY;
2729 break;
2730 case ACPI_IORT_SMMU_V3_HISILICON_HI161X:
2731 smmu->options |= ARM_SMMU_OPT_SKIP_PREFETCH;
2732 break;
2733 }
2734
2735 dev_notice(smmu->dev, "option mask 0x%x\n", smmu->options);
2736}
2737
2738static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2739 struct arm_smmu_device *smmu)
2740{
2741 struct acpi_iort_smmu_v3 *iort_smmu;
2742 struct device *dev = smmu->dev;
2743 struct acpi_iort_node *node;
2744
2745 node = *(struct acpi_iort_node **)dev_get_platdata(dev);
2746
2747 /* Retrieve SMMUv3 specific data */
2748 iort_smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
2749
2750 acpi_smmu_get_options(iort_smmu->model, smmu);
2751
2752 if (iort_smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE)
2753 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2754
2755 return 0;
2756}
2757#else
2758static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2759 struct arm_smmu_device *smmu)
2760{
2761 return -ENODEV;
2762}
2763#endif
2764
2765static int arm_smmu_device_dt_probe(struct platform_device *pdev,
2766 struct arm_smmu_device *smmu)
2767{
2768 struct device *dev = &pdev->dev;
2769 u32 cells;
2770 int ret = -EINVAL;
2771
2772 if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells))
2773 dev_err(dev, "missing #iommu-cells property\n");
2774 else if (cells != 1)
2775 dev_err(dev, "invalid #iommu-cells value (%d)\n", cells);
2776 else
2777 ret = 0;
2778
2779 parse_driver_options(smmu);
2780
2781 if (of_dma_is_coherent(dev->of_node))
2782 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
2783
2784 return ret;
2785}
2786
2787static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)
2788{
2789 if (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY)
2790 return SZ_64K;
2791 else
2792 return SZ_128K;
2793}
2794
2795static int arm_smmu_device_probe(struct platform_device *pdev)
2796{
2797 int irq, ret;
2798 struct resource *res;
2799 resource_size_t ioaddr;
2800 struct arm_smmu_device *smmu;
2801 struct device *dev = &pdev->dev;
2802 bool bypass;
2803
2804 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2805 if (!smmu) {
2806 dev_err(dev, "failed to allocate arm_smmu_device\n");
2807 return -ENOMEM;
2808 }
2809 smmu->dev = dev;
2810
2811 if (dev->of_node) {
2812 ret = arm_smmu_device_dt_probe(pdev, smmu);
2813 } else {
2814 ret = arm_smmu_device_acpi_probe(pdev, smmu);
2815 if (ret == -ENODEV)
2816 return ret;
2817 }
2818
2819 /* Set bypass mode according to firmware probing result */
2820 bypass = !!ret;
2821
2822 /* Base address */
2823 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2824 if (resource_size(res) + 1 < arm_smmu_resource_size(smmu)) {
2825 dev_err(dev, "MMIO region too small (%pr)\n", res);
2826 return -EINVAL;
2827 }
2828 ioaddr = res->start;
2829
2830 smmu->base = devm_ioremap_resource(dev, res);
2831 if (IS_ERR(smmu->base))
2832 return PTR_ERR(smmu->base);
2833
2834 /* Interrupt lines */
2835
2836 irq = platform_get_irq_byname(pdev, "combined");
2837 if (irq > 0)
2838 smmu->combined_irq = irq;
2839 else {
2840 irq = platform_get_irq_byname(pdev, "eventq");
2841 if (irq > 0)
2842 smmu->evtq.q.irq = irq;
2843
2844 irq = platform_get_irq_byname(pdev, "priq");
2845 if (irq > 0)
2846 smmu->priq.q.irq = irq;
2847
2848 irq = platform_get_irq_byname(pdev, "gerror");
2849 if (irq > 0)
2850 smmu->gerr_irq = irq;
2851 }
2852 /* Probe the h/w */
2853 ret = arm_smmu_device_hw_probe(smmu);
2854 if (ret)
2855 return ret;
2856
2857 /* Initialise in-memory data structures */
2858 ret = arm_smmu_init_structures(smmu);
2859 if (ret)
2860 return ret;
2861
2862 /* Record our private device structure */
2863 platform_set_drvdata(pdev, smmu);
2864
2865 /* Reset the device */
2866 ret = arm_smmu_device_reset(smmu, bypass);
2867 if (ret)
2868 return ret;
2869
2870 /* And we're up. Go go go! */
2871 ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL,
2872 "smmu3.%pa", &ioaddr);
2873 if (ret)
2874 return ret;
2875
2876 iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2877 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2878
2879 ret = iommu_device_register(&smmu->iommu);
2880 if (ret) {
2881 dev_err(dev, "Failed to register iommu\n");
2882 return ret;
2883 }
2884
2885#ifdef CONFIG_PCI
2886 if (pci_bus_type.iommu_ops != &arm_smmu_ops) {
2887 pci_request_acs();
2888 ret = bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2889 if (ret)
2890 return ret;
2891 }
2892#endif
2893#ifdef CONFIG_ARM_AMBA
2894 if (amba_bustype.iommu_ops != &arm_smmu_ops) {
2895 ret = bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2896 if (ret)
2897 return ret;
2898 }
2899#endif
2900 if (platform_bus_type.iommu_ops != &arm_smmu_ops) {
2901 ret = bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2902 if (ret)
2903 return ret;
2904 }
2905 return 0;
2906}
2907
2908static int arm_smmu_device_remove(struct platform_device *pdev)
2909{
2910 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
2911
2912 arm_smmu_device_disable(smmu);
2913
2914 return 0;
2915}
2916
2917static void arm_smmu_device_shutdown(struct platform_device *pdev)
2918{
2919 arm_smmu_device_remove(pdev);
2920}
2921
2922static const struct of_device_id arm_smmu_of_match[] = {
2923 { .compatible = "arm,smmu-v3", },
2924 { },
2925};
2926MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
2927
2928static struct platform_driver arm_smmu_driver = {
2929 .driver = {
2930 .name = "arm-smmu-v3",
2931 .of_match_table = of_match_ptr(arm_smmu_of_match),
2932 },
2933 .probe = arm_smmu_device_probe,
2934 .remove = arm_smmu_device_remove,
2935 .shutdown = arm_smmu_device_shutdown,
2936};
2937module_platform_driver(arm_smmu_driver);
2938
2939MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
2940MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
2941MODULE_LICENSE("GPL v2");