iommu/arm-smmu-v3: Add command queue batching helpers
[linux-2.6-microblaze.git] / drivers / iommu / arm-smmu-v3.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * IOMMU API for ARM architected SMMUv3 implementations.
4  *
5  * Copyright (C) 2015 ARM Limited
6  *
7  * Author: Will Deacon <will.deacon@arm.com>
8  *
9  * This driver is powered by bad coffee and bombay mix.
10  */
11
12 #include <linux/acpi.h>
13 #include <linux/acpi_iort.h>
14 #include <linux/bitfield.h>
15 #include <linux/bitops.h>
16 #include <linux/crash_dump.h>
17 #include <linux/delay.h>
18 #include <linux/dma-iommu.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/io-pgtable.h>
22 #include <linux/iommu.h>
23 #include <linux/iopoll.h>
24 #include <linux/module.h>
25 #include <linux/msi.h>
26 #include <linux/of.h>
27 #include <linux/of_address.h>
28 #include <linux/of_iommu.h>
29 #include <linux/of_platform.h>
30 #include <linux/pci.h>
31 #include <linux/pci-ats.h>
32 #include <linux/platform_device.h>
33
34 #include <linux/amba/bus.h>
35
36 /* MMIO registers */
37 #define ARM_SMMU_IDR0                   0x0
38 #define IDR0_ST_LVL                     GENMASK(28, 27)
39 #define IDR0_ST_LVL_2LVL                1
40 #define IDR0_STALL_MODEL                GENMASK(25, 24)
41 #define IDR0_STALL_MODEL_STALL          0
42 #define IDR0_STALL_MODEL_FORCE          2
43 #define IDR0_TTENDIAN                   GENMASK(22, 21)
44 #define IDR0_TTENDIAN_MIXED             0
45 #define IDR0_TTENDIAN_LE                2
46 #define IDR0_TTENDIAN_BE                3
47 #define IDR0_CD2L                       (1 << 19)
48 #define IDR0_VMID16                     (1 << 18)
49 #define IDR0_PRI                        (1 << 16)
50 #define IDR0_SEV                        (1 << 14)
51 #define IDR0_MSI                        (1 << 13)
52 #define IDR0_ASID16                     (1 << 12)
53 #define IDR0_ATS                        (1 << 10)
54 #define IDR0_HYP                        (1 << 9)
55 #define IDR0_COHACC                     (1 << 4)
56 #define IDR0_TTF                        GENMASK(3, 2)
57 #define IDR0_TTF_AARCH64                2
58 #define IDR0_TTF_AARCH32_64             3
59 #define IDR0_S1P                        (1 << 1)
60 #define IDR0_S2P                        (1 << 0)
61
62 #define ARM_SMMU_IDR1                   0x4
63 #define IDR1_TABLES_PRESET              (1 << 30)
64 #define IDR1_QUEUES_PRESET              (1 << 29)
65 #define IDR1_REL                        (1 << 28)
66 #define IDR1_CMDQS                      GENMASK(25, 21)
67 #define IDR1_EVTQS                      GENMASK(20, 16)
68 #define IDR1_PRIQS                      GENMASK(15, 11)
69 #define IDR1_SSIDSIZE                   GENMASK(10, 6)
70 #define IDR1_SIDSIZE                    GENMASK(5, 0)
71
72 #define ARM_SMMU_IDR5                   0x14
73 #define IDR5_STALL_MAX                  GENMASK(31, 16)
74 #define IDR5_GRAN64K                    (1 << 6)
75 #define IDR5_GRAN16K                    (1 << 5)
76 #define IDR5_GRAN4K                     (1 << 4)
77 #define IDR5_OAS                        GENMASK(2, 0)
78 #define IDR5_OAS_32_BIT                 0
79 #define IDR5_OAS_36_BIT                 1
80 #define IDR5_OAS_40_BIT                 2
81 #define IDR5_OAS_42_BIT                 3
82 #define IDR5_OAS_44_BIT                 4
83 #define IDR5_OAS_48_BIT                 5
84 #define IDR5_OAS_52_BIT                 6
85 #define IDR5_VAX                        GENMASK(11, 10)
86 #define IDR5_VAX_52_BIT                 1
87
88 #define ARM_SMMU_CR0                    0x20
89 #define CR0_ATSCHK                      (1 << 4)
90 #define CR0_CMDQEN                      (1 << 3)
91 #define CR0_EVTQEN                      (1 << 2)
92 #define CR0_PRIQEN                      (1 << 1)
93 #define CR0_SMMUEN                      (1 << 0)
94
95 #define ARM_SMMU_CR0ACK                 0x24
96
97 #define ARM_SMMU_CR1                    0x28
98 #define CR1_TABLE_SH                    GENMASK(11, 10)
99 #define CR1_TABLE_OC                    GENMASK(9, 8)
100 #define CR1_TABLE_IC                    GENMASK(7, 6)
101 #define CR1_QUEUE_SH                    GENMASK(5, 4)
102 #define CR1_QUEUE_OC                    GENMASK(3, 2)
103 #define CR1_QUEUE_IC                    GENMASK(1, 0)
104 /* CR1 cacheability fields don't quite follow the usual TCR-style encoding */
105 #define CR1_CACHE_NC                    0
106 #define CR1_CACHE_WB                    1
107 #define CR1_CACHE_WT                    2
108
109 #define ARM_SMMU_CR2                    0x2c
110 #define CR2_PTM                         (1 << 2)
111 #define CR2_RECINVSID                   (1 << 1)
112 #define CR2_E2H                         (1 << 0)
113
114 #define ARM_SMMU_GBPA                   0x44
115 #define GBPA_UPDATE                     (1 << 31)
116 #define GBPA_ABORT                      (1 << 20)
117
118 #define ARM_SMMU_IRQ_CTRL               0x50
119 #define IRQ_CTRL_EVTQ_IRQEN             (1 << 2)
120 #define IRQ_CTRL_PRIQ_IRQEN             (1 << 1)
121 #define IRQ_CTRL_GERROR_IRQEN           (1 << 0)
122
123 #define ARM_SMMU_IRQ_CTRLACK            0x54
124
125 #define ARM_SMMU_GERROR                 0x60
126 #define GERROR_SFM_ERR                  (1 << 8)
127 #define GERROR_MSI_GERROR_ABT_ERR       (1 << 7)
128 #define GERROR_MSI_PRIQ_ABT_ERR         (1 << 6)
129 #define GERROR_MSI_EVTQ_ABT_ERR         (1 << 5)
130 #define GERROR_MSI_CMDQ_ABT_ERR         (1 << 4)
131 #define GERROR_PRIQ_ABT_ERR             (1 << 3)
132 #define GERROR_EVTQ_ABT_ERR             (1 << 2)
133 #define GERROR_CMDQ_ERR                 (1 << 0)
134 #define GERROR_ERR_MASK                 0xfd
135
136 #define ARM_SMMU_GERRORN                0x64
137
138 #define ARM_SMMU_GERROR_IRQ_CFG0        0x68
139 #define ARM_SMMU_GERROR_IRQ_CFG1        0x70
140 #define ARM_SMMU_GERROR_IRQ_CFG2        0x74
141
142 #define ARM_SMMU_STRTAB_BASE            0x80
143 #define STRTAB_BASE_RA                  (1UL << 62)
144 #define STRTAB_BASE_ADDR_MASK           GENMASK_ULL(51, 6)
145
146 #define ARM_SMMU_STRTAB_BASE_CFG        0x88
147 #define STRTAB_BASE_CFG_FMT             GENMASK(17, 16)
148 #define STRTAB_BASE_CFG_FMT_LINEAR      0
149 #define STRTAB_BASE_CFG_FMT_2LVL        1
150 #define STRTAB_BASE_CFG_SPLIT           GENMASK(10, 6)
151 #define STRTAB_BASE_CFG_LOG2SIZE        GENMASK(5, 0)
152
153 #define ARM_SMMU_CMDQ_BASE              0x90
154 #define ARM_SMMU_CMDQ_PROD              0x98
155 #define ARM_SMMU_CMDQ_CONS              0x9c
156
157 #define ARM_SMMU_EVTQ_BASE              0xa0
158 #define ARM_SMMU_EVTQ_PROD              0x100a8
159 #define ARM_SMMU_EVTQ_CONS              0x100ac
160 #define ARM_SMMU_EVTQ_IRQ_CFG0          0xb0
161 #define ARM_SMMU_EVTQ_IRQ_CFG1          0xb8
162 #define ARM_SMMU_EVTQ_IRQ_CFG2          0xbc
163
164 #define ARM_SMMU_PRIQ_BASE              0xc0
165 #define ARM_SMMU_PRIQ_PROD              0x100c8
166 #define ARM_SMMU_PRIQ_CONS              0x100cc
167 #define ARM_SMMU_PRIQ_IRQ_CFG0          0xd0
168 #define ARM_SMMU_PRIQ_IRQ_CFG1          0xd8
169 #define ARM_SMMU_PRIQ_IRQ_CFG2          0xdc
170
171 /* Common MSI config fields */
172 #define MSI_CFG0_ADDR_MASK              GENMASK_ULL(51, 2)
173 #define MSI_CFG2_SH                     GENMASK(5, 4)
174 #define MSI_CFG2_MEMATTR                GENMASK(3, 0)
175
176 /* Common memory attribute values */
177 #define ARM_SMMU_SH_NSH                 0
178 #define ARM_SMMU_SH_OSH                 2
179 #define ARM_SMMU_SH_ISH                 3
180 #define ARM_SMMU_MEMATTR_DEVICE_nGnRE   0x1
181 #define ARM_SMMU_MEMATTR_OIWB           0xf
182
183 #define Q_IDX(llq, p)                   ((p) & ((1 << (llq)->max_n_shift) - 1))
184 #define Q_WRP(llq, p)                   ((p) & (1 << (llq)->max_n_shift))
185 #define Q_OVERFLOW_FLAG                 (1U << 31)
186 #define Q_OVF(p)                        ((p) & Q_OVERFLOW_FLAG)
187 #define Q_ENT(q, p)                     ((q)->base +                    \
188                                          Q_IDX(&((q)->llq), p) *        \
189                                          (q)->ent_dwords)
190
191 #define Q_BASE_RWA                      (1UL << 62)
192 #define Q_BASE_ADDR_MASK                GENMASK_ULL(51, 5)
193 #define Q_BASE_LOG2SIZE                 GENMASK(4, 0)
194
195 /* Ensure DMA allocations are naturally aligned */
196 #ifdef CONFIG_CMA_ALIGNMENT
197 #define Q_MAX_SZ_SHIFT                  (PAGE_SHIFT + CONFIG_CMA_ALIGNMENT)
198 #else
199 #define Q_MAX_SZ_SHIFT                  (PAGE_SHIFT + MAX_ORDER - 1)
200 #endif
201
202 /*
203  * Stream table.
204  *
205  * Linear: Enough to cover 1 << IDR1.SIDSIZE entries
206  * 2lvl: 128k L1 entries,
207  *       256 lazy entries per table (each table covers a PCI bus)
208  */
209 #define STRTAB_L1_SZ_SHIFT              20
210 #define STRTAB_SPLIT                    8
211
212 #define STRTAB_L1_DESC_DWORDS           1
213 #define STRTAB_L1_DESC_SPAN             GENMASK_ULL(4, 0)
214 #define STRTAB_L1_DESC_L2PTR_MASK       GENMASK_ULL(51, 6)
215
216 #define STRTAB_STE_DWORDS               8
217 #define STRTAB_STE_0_V                  (1UL << 0)
218 #define STRTAB_STE_0_CFG                GENMASK_ULL(3, 1)
219 #define STRTAB_STE_0_CFG_ABORT          0
220 #define STRTAB_STE_0_CFG_BYPASS         4
221 #define STRTAB_STE_0_CFG_S1_TRANS       5
222 #define STRTAB_STE_0_CFG_S2_TRANS       6
223
224 #define STRTAB_STE_0_S1FMT              GENMASK_ULL(5, 4)
225 #define STRTAB_STE_0_S1FMT_LINEAR       0
226 #define STRTAB_STE_0_S1FMT_64K_L2       2
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_S1DSS              GENMASK_ULL(1, 0)
231 #define STRTAB_STE_1_S1DSS_TERMINATE    0x0
232 #define STRTAB_STE_1_S1DSS_BYPASS       0x1
233 #define STRTAB_STE_1_S1DSS_SSID0        0x2
234
235 #define STRTAB_STE_1_S1C_CACHE_NC       0UL
236 #define STRTAB_STE_1_S1C_CACHE_WBRA     1UL
237 #define STRTAB_STE_1_S1C_CACHE_WT       2UL
238 #define STRTAB_STE_1_S1C_CACHE_WB       3UL
239 #define STRTAB_STE_1_S1CIR              GENMASK_ULL(3, 2)
240 #define STRTAB_STE_1_S1COR              GENMASK_ULL(5, 4)
241 #define STRTAB_STE_1_S1CSH              GENMASK_ULL(7, 6)
242
243 #define STRTAB_STE_1_S1STALLD           (1UL << 27)
244
245 #define STRTAB_STE_1_EATS               GENMASK_ULL(29, 28)
246 #define STRTAB_STE_1_EATS_ABT           0UL
247 #define STRTAB_STE_1_EATS_TRANS         1UL
248 #define STRTAB_STE_1_EATS_S1CHK         2UL
249
250 #define STRTAB_STE_1_STRW               GENMASK_ULL(31, 30)
251 #define STRTAB_STE_1_STRW_NSEL1         0UL
252 #define STRTAB_STE_1_STRW_EL2           2UL
253
254 #define STRTAB_STE_1_SHCFG              GENMASK_ULL(45, 44)
255 #define STRTAB_STE_1_SHCFG_INCOMING     1UL
256
257 #define STRTAB_STE_2_S2VMID             GENMASK_ULL(15, 0)
258 #define STRTAB_STE_2_VTCR               GENMASK_ULL(50, 32)
259 #define STRTAB_STE_2_VTCR_S2T0SZ        GENMASK_ULL(5, 0)
260 #define STRTAB_STE_2_VTCR_S2SL0         GENMASK_ULL(7, 6)
261 #define STRTAB_STE_2_VTCR_S2IR0         GENMASK_ULL(9, 8)
262 #define STRTAB_STE_2_VTCR_S2OR0         GENMASK_ULL(11, 10)
263 #define STRTAB_STE_2_VTCR_S2SH0         GENMASK_ULL(13, 12)
264 #define STRTAB_STE_2_VTCR_S2TG          GENMASK_ULL(15, 14)
265 #define STRTAB_STE_2_VTCR_S2PS          GENMASK_ULL(18, 16)
266 #define STRTAB_STE_2_S2AA64             (1UL << 51)
267 #define STRTAB_STE_2_S2ENDI             (1UL << 52)
268 #define STRTAB_STE_2_S2PTW              (1UL << 54)
269 #define STRTAB_STE_2_S2R                (1UL << 58)
270
271 #define STRTAB_STE_3_S2TTB_MASK         GENMASK_ULL(51, 4)
272
273 /*
274  * Context descriptors.
275  *
276  * Linear: when less than 1024 SSIDs are supported
277  * 2lvl: at most 1024 L1 entries,
278  *       1024 lazy entries per table.
279  */
280 #define CTXDESC_SPLIT                   10
281 #define CTXDESC_L2_ENTRIES              (1 << CTXDESC_SPLIT)
282
283 #define CTXDESC_L1_DESC_DWORDS          1
284 #define CTXDESC_L1_DESC_V               (1UL << 0)
285 #define CTXDESC_L1_DESC_L2PTR_MASK      GENMASK_ULL(51, 12)
286
287 #define CTXDESC_CD_DWORDS               8
288 #define CTXDESC_CD_0_TCR_T0SZ           GENMASK_ULL(5, 0)
289 #define CTXDESC_CD_0_TCR_TG0            GENMASK_ULL(7, 6)
290 #define CTXDESC_CD_0_TCR_IRGN0          GENMASK_ULL(9, 8)
291 #define CTXDESC_CD_0_TCR_ORGN0          GENMASK_ULL(11, 10)
292 #define CTXDESC_CD_0_TCR_SH0            GENMASK_ULL(13, 12)
293 #define CTXDESC_CD_0_TCR_EPD0           (1ULL << 14)
294 #define CTXDESC_CD_0_TCR_EPD1           (1ULL << 30)
295
296 #define CTXDESC_CD_0_ENDI               (1UL << 15)
297 #define CTXDESC_CD_0_V                  (1UL << 31)
298
299 #define CTXDESC_CD_0_TCR_IPS            GENMASK_ULL(34, 32)
300 #define CTXDESC_CD_0_TCR_TBI0           (1ULL << 38)
301
302 #define CTXDESC_CD_0_AA64               (1UL << 41)
303 #define CTXDESC_CD_0_S                  (1UL << 44)
304 #define CTXDESC_CD_0_R                  (1UL << 45)
305 #define CTXDESC_CD_0_A                  (1UL << 46)
306 #define CTXDESC_CD_0_ASET               (1UL << 47)
307 #define CTXDESC_CD_0_ASID               GENMASK_ULL(63, 48)
308
309 #define CTXDESC_CD_1_TTB0_MASK          GENMASK_ULL(51, 4)
310
311 /*
312  * When the SMMU only supports linear context descriptor tables, pick a
313  * reasonable size limit (64kB).
314  */
315 #define CTXDESC_LINEAR_CDMAX            ilog2(SZ_64K / (CTXDESC_CD_DWORDS << 3))
316
317 /* Command queue */
318 #define CMDQ_ENT_SZ_SHIFT               4
319 #define CMDQ_ENT_DWORDS                 ((1 << CMDQ_ENT_SZ_SHIFT) >> 3)
320 #define CMDQ_MAX_SZ_SHIFT               (Q_MAX_SZ_SHIFT - CMDQ_ENT_SZ_SHIFT)
321
322 #define CMDQ_CONS_ERR                   GENMASK(30, 24)
323 #define CMDQ_ERR_CERROR_NONE_IDX        0
324 #define CMDQ_ERR_CERROR_ILL_IDX         1
325 #define CMDQ_ERR_CERROR_ABT_IDX         2
326 #define CMDQ_ERR_CERROR_ATC_INV_IDX     3
327
328 #define CMDQ_PROD_OWNED_FLAG            Q_OVERFLOW_FLAG
329
330 /*
331  * This is used to size the command queue and therefore must be at least
332  * BITS_PER_LONG so that the valid_map works correctly (it relies on the
333  * total number of queue entries being a multiple of BITS_PER_LONG).
334  */
335 #define CMDQ_BATCH_ENTRIES              BITS_PER_LONG
336
337 #define CMDQ_0_OP                       GENMASK_ULL(7, 0)
338 #define CMDQ_0_SSV                      (1UL << 11)
339
340 #define CMDQ_PREFETCH_0_SID             GENMASK_ULL(63, 32)
341 #define CMDQ_PREFETCH_1_SIZE            GENMASK_ULL(4, 0)
342 #define CMDQ_PREFETCH_1_ADDR_MASK       GENMASK_ULL(63, 12)
343
344 #define CMDQ_CFGI_0_SSID                GENMASK_ULL(31, 12)
345 #define CMDQ_CFGI_0_SID                 GENMASK_ULL(63, 32)
346 #define CMDQ_CFGI_1_LEAF                (1UL << 0)
347 #define CMDQ_CFGI_1_RANGE               GENMASK_ULL(4, 0)
348
349 #define CMDQ_TLBI_0_VMID                GENMASK_ULL(47, 32)
350 #define CMDQ_TLBI_0_ASID                GENMASK_ULL(63, 48)
351 #define CMDQ_TLBI_1_LEAF                (1UL << 0)
352 #define CMDQ_TLBI_1_VA_MASK             GENMASK_ULL(63, 12)
353 #define CMDQ_TLBI_1_IPA_MASK            GENMASK_ULL(51, 12)
354
355 #define CMDQ_ATC_0_SSID                 GENMASK_ULL(31, 12)
356 #define CMDQ_ATC_0_SID                  GENMASK_ULL(63, 32)
357 #define CMDQ_ATC_0_GLOBAL               (1UL << 9)
358 #define CMDQ_ATC_1_SIZE                 GENMASK_ULL(5, 0)
359 #define CMDQ_ATC_1_ADDR_MASK            GENMASK_ULL(63, 12)
360
361 #define CMDQ_PRI_0_SSID                 GENMASK_ULL(31, 12)
362 #define CMDQ_PRI_0_SID                  GENMASK_ULL(63, 32)
363 #define CMDQ_PRI_1_GRPID                GENMASK_ULL(8, 0)
364 #define CMDQ_PRI_1_RESP                 GENMASK_ULL(13, 12)
365
366 #define CMDQ_SYNC_0_CS                  GENMASK_ULL(13, 12)
367 #define CMDQ_SYNC_0_CS_NONE             0
368 #define CMDQ_SYNC_0_CS_IRQ              1
369 #define CMDQ_SYNC_0_CS_SEV              2
370 #define CMDQ_SYNC_0_MSH                 GENMASK_ULL(23, 22)
371 #define CMDQ_SYNC_0_MSIATTR             GENMASK_ULL(27, 24)
372 #define CMDQ_SYNC_0_MSIDATA             GENMASK_ULL(63, 32)
373 #define CMDQ_SYNC_1_MSIADDR_MASK        GENMASK_ULL(51, 2)
374
375 /* Event queue */
376 #define EVTQ_ENT_SZ_SHIFT               5
377 #define EVTQ_ENT_DWORDS                 ((1 << EVTQ_ENT_SZ_SHIFT) >> 3)
378 #define EVTQ_MAX_SZ_SHIFT               (Q_MAX_SZ_SHIFT - EVTQ_ENT_SZ_SHIFT)
379
380 #define EVTQ_0_ID                       GENMASK_ULL(7, 0)
381
382 /* PRI queue */
383 #define PRIQ_ENT_SZ_SHIFT               4
384 #define PRIQ_ENT_DWORDS                 ((1 << PRIQ_ENT_SZ_SHIFT) >> 3)
385 #define PRIQ_MAX_SZ_SHIFT               (Q_MAX_SZ_SHIFT - PRIQ_ENT_SZ_SHIFT)
386
387 #define PRIQ_0_SID                      GENMASK_ULL(31, 0)
388 #define PRIQ_0_SSID                     GENMASK_ULL(51, 32)
389 #define PRIQ_0_PERM_PRIV                (1UL << 58)
390 #define PRIQ_0_PERM_EXEC                (1UL << 59)
391 #define PRIQ_0_PERM_READ                (1UL << 60)
392 #define PRIQ_0_PERM_WRITE               (1UL << 61)
393 #define PRIQ_0_PRG_LAST                 (1UL << 62)
394 #define PRIQ_0_SSID_V                   (1UL << 63)
395
396 #define PRIQ_1_PRG_IDX                  GENMASK_ULL(8, 0)
397 #define PRIQ_1_ADDR_MASK                GENMASK_ULL(63, 12)
398
399 /* High-level queue structures */
400 #define ARM_SMMU_POLL_TIMEOUT_US        1000000 /* 1s! */
401 #define ARM_SMMU_POLL_SPIN_COUNT        10
402
403 #define MSI_IOVA_BASE                   0x8000000
404 #define MSI_IOVA_LENGTH                 0x100000
405
406 static bool disable_bypass = 1;
407 module_param_named(disable_bypass, disable_bypass, bool, S_IRUGO);
408 MODULE_PARM_DESC(disable_bypass,
409         "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.");
410
411 enum pri_resp {
412         PRI_RESP_DENY = 0,
413         PRI_RESP_FAIL = 1,
414         PRI_RESP_SUCC = 2,
415 };
416
417 enum arm_smmu_msi_index {
418         EVTQ_MSI_INDEX,
419         GERROR_MSI_INDEX,
420         PRIQ_MSI_INDEX,
421         ARM_SMMU_MAX_MSIS,
422 };
423
424 static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = {
425         [EVTQ_MSI_INDEX] = {
426                 ARM_SMMU_EVTQ_IRQ_CFG0,
427                 ARM_SMMU_EVTQ_IRQ_CFG1,
428                 ARM_SMMU_EVTQ_IRQ_CFG2,
429         },
430         [GERROR_MSI_INDEX] = {
431                 ARM_SMMU_GERROR_IRQ_CFG0,
432                 ARM_SMMU_GERROR_IRQ_CFG1,
433                 ARM_SMMU_GERROR_IRQ_CFG2,
434         },
435         [PRIQ_MSI_INDEX] = {
436                 ARM_SMMU_PRIQ_IRQ_CFG0,
437                 ARM_SMMU_PRIQ_IRQ_CFG1,
438                 ARM_SMMU_PRIQ_IRQ_CFG2,
439         },
440 };
441
442 struct arm_smmu_cmdq_ent {
443         /* Common fields */
444         u8                              opcode;
445         bool                            substream_valid;
446
447         /* Command-specific fields */
448         union {
449                 #define CMDQ_OP_PREFETCH_CFG    0x1
450                 struct {
451                         u32                     sid;
452                         u8                      size;
453                         u64                     addr;
454                 } prefetch;
455
456                 #define CMDQ_OP_CFGI_STE        0x3
457                 #define CMDQ_OP_CFGI_ALL        0x4
458                 #define CMDQ_OP_CFGI_CD         0x5
459                 #define CMDQ_OP_CFGI_CD_ALL     0x6
460                 struct {
461                         u32                     sid;
462                         u32                     ssid;
463                         union {
464                                 bool            leaf;
465                                 u8              span;
466                         };
467                 } cfgi;
468
469                 #define CMDQ_OP_TLBI_NH_ASID    0x11
470                 #define CMDQ_OP_TLBI_NH_VA      0x12
471                 #define CMDQ_OP_TLBI_EL2_ALL    0x20
472                 #define CMDQ_OP_TLBI_S12_VMALL  0x28
473                 #define CMDQ_OP_TLBI_S2_IPA     0x2a
474                 #define CMDQ_OP_TLBI_NSNH_ALL   0x30
475                 struct {
476                         u16                     asid;
477                         u16                     vmid;
478                         bool                    leaf;
479                         u64                     addr;
480                 } tlbi;
481
482                 #define CMDQ_OP_ATC_INV         0x40
483                 #define ATC_INV_SIZE_ALL        52
484                 struct {
485                         u32                     sid;
486                         u32                     ssid;
487                         u64                     addr;
488                         u8                      size;
489                         bool                    global;
490                 } atc;
491
492                 #define CMDQ_OP_PRI_RESP        0x41
493                 struct {
494                         u32                     sid;
495                         u32                     ssid;
496                         u16                     grpid;
497                         enum pri_resp           resp;
498                 } pri;
499
500                 #define CMDQ_OP_CMD_SYNC        0x46
501                 struct {
502                         u64                     msiaddr;
503                 } sync;
504         };
505 };
506
507 struct arm_smmu_ll_queue {
508         union {
509                 u64                     val;
510                 struct {
511                         u32             prod;
512                         u32             cons;
513                 };
514                 struct {
515                         atomic_t        prod;
516                         atomic_t        cons;
517                 } atomic;
518                 u8                      __pad[SMP_CACHE_BYTES];
519         } ____cacheline_aligned_in_smp;
520         u32                             max_n_shift;
521 };
522
523 struct arm_smmu_queue {
524         struct arm_smmu_ll_queue        llq;
525         int                             irq; /* Wired interrupt */
526
527         __le64                          *base;
528         dma_addr_t                      base_dma;
529         u64                             q_base;
530
531         size_t                          ent_dwords;
532
533         u32 __iomem                     *prod_reg;
534         u32 __iomem                     *cons_reg;
535 };
536
537 struct arm_smmu_queue_poll {
538         ktime_t                         timeout;
539         unsigned int                    delay;
540         unsigned int                    spin_cnt;
541         bool                            wfe;
542 };
543
544 struct arm_smmu_cmdq {
545         struct arm_smmu_queue           q;
546         atomic_long_t                   *valid_map;
547         atomic_t                        owner_prod;
548         atomic_t                        lock;
549 };
550
551 struct arm_smmu_cmdq_batch {
552         u64                             cmds[CMDQ_BATCH_ENTRIES * CMDQ_ENT_DWORDS];
553         int                             num;
554 };
555
556 struct arm_smmu_evtq {
557         struct arm_smmu_queue           q;
558         u32                             max_stalls;
559 };
560
561 struct arm_smmu_priq {
562         struct arm_smmu_queue           q;
563 };
564
565 /* High-level stream table and context descriptor structures */
566 struct arm_smmu_strtab_l1_desc {
567         u8                              span;
568
569         __le64                          *l2ptr;
570         dma_addr_t                      l2ptr_dma;
571 };
572
573 struct arm_smmu_ctx_desc {
574         u16                             asid;
575         u64                             ttbr;
576         u64                             tcr;
577         u64                             mair;
578 };
579
580 struct arm_smmu_l1_ctx_desc {
581         __le64                          *l2ptr;
582         dma_addr_t                      l2ptr_dma;
583 };
584
585 struct arm_smmu_ctx_desc_cfg {
586         __le64                          *cdtab;
587         dma_addr_t                      cdtab_dma;
588         struct arm_smmu_l1_ctx_desc     *l1_desc;
589         unsigned int                    num_l1_ents;
590 };
591
592 struct arm_smmu_s1_cfg {
593         struct arm_smmu_ctx_desc_cfg    cdcfg;
594         struct arm_smmu_ctx_desc        cd;
595         u8                              s1fmt;
596         u8                              s1cdmax;
597 };
598
599 struct arm_smmu_s2_cfg {
600         u16                             vmid;
601         u64                             vttbr;
602         u64                             vtcr;
603 };
604
605 struct arm_smmu_strtab_cfg {
606         __le64                          *strtab;
607         dma_addr_t                      strtab_dma;
608         struct arm_smmu_strtab_l1_desc  *l1_desc;
609         unsigned int                    num_l1_ents;
610
611         u64                             strtab_base;
612         u32                             strtab_base_cfg;
613 };
614
615 /* An SMMUv3 instance */
616 struct arm_smmu_device {
617         struct device                   *dev;
618         void __iomem                    *base;
619
620 #define ARM_SMMU_FEAT_2_LVL_STRTAB      (1 << 0)
621 #define ARM_SMMU_FEAT_2_LVL_CDTAB       (1 << 1)
622 #define ARM_SMMU_FEAT_TT_LE             (1 << 2)
623 #define ARM_SMMU_FEAT_TT_BE             (1 << 3)
624 #define ARM_SMMU_FEAT_PRI               (1 << 4)
625 #define ARM_SMMU_FEAT_ATS               (1 << 5)
626 #define ARM_SMMU_FEAT_SEV               (1 << 6)
627 #define ARM_SMMU_FEAT_MSI               (1 << 7)
628 #define ARM_SMMU_FEAT_COHERENCY         (1 << 8)
629 #define ARM_SMMU_FEAT_TRANS_S1          (1 << 9)
630 #define ARM_SMMU_FEAT_TRANS_S2          (1 << 10)
631 #define ARM_SMMU_FEAT_STALLS            (1 << 11)
632 #define ARM_SMMU_FEAT_HYP               (1 << 12)
633 #define ARM_SMMU_FEAT_STALL_FORCE       (1 << 13)
634 #define ARM_SMMU_FEAT_VAX               (1 << 14)
635         u32                             features;
636
637 #define ARM_SMMU_OPT_SKIP_PREFETCH      (1 << 0)
638 #define ARM_SMMU_OPT_PAGE0_REGS_ONLY    (1 << 1)
639         u32                             options;
640
641         struct arm_smmu_cmdq            cmdq;
642         struct arm_smmu_evtq            evtq;
643         struct arm_smmu_priq            priq;
644
645         int                             gerr_irq;
646         int                             combined_irq;
647
648         unsigned long                   ias; /* IPA */
649         unsigned long                   oas; /* PA */
650         unsigned long                   pgsize_bitmap;
651
652 #define ARM_SMMU_MAX_ASIDS              (1 << 16)
653         unsigned int                    asid_bits;
654         DECLARE_BITMAP(asid_map, ARM_SMMU_MAX_ASIDS);
655
656 #define ARM_SMMU_MAX_VMIDS              (1 << 16)
657         unsigned int                    vmid_bits;
658         DECLARE_BITMAP(vmid_map, ARM_SMMU_MAX_VMIDS);
659
660         unsigned int                    ssid_bits;
661         unsigned int                    sid_bits;
662
663         struct arm_smmu_strtab_cfg      strtab_cfg;
664
665         /* IOMMU core code handle */
666         struct iommu_device             iommu;
667 };
668
669 /* SMMU private data for each master */
670 struct arm_smmu_master {
671         struct arm_smmu_device          *smmu;
672         struct device                   *dev;
673         struct arm_smmu_domain          *domain;
674         struct list_head                domain_head;
675         u32                             *sids;
676         unsigned int                    num_sids;
677         bool                            ats_enabled;
678         unsigned int                    ssid_bits;
679 };
680
681 /* SMMU private data for an IOMMU domain */
682 enum arm_smmu_domain_stage {
683         ARM_SMMU_DOMAIN_S1 = 0,
684         ARM_SMMU_DOMAIN_S2,
685         ARM_SMMU_DOMAIN_NESTED,
686         ARM_SMMU_DOMAIN_BYPASS,
687 };
688
689 struct arm_smmu_domain {
690         struct arm_smmu_device          *smmu;
691         struct mutex                    init_mutex; /* Protects smmu pointer */
692
693         struct io_pgtable_ops           *pgtbl_ops;
694         bool                            non_strict;
695         atomic_t                        nr_ats_masters;
696
697         enum arm_smmu_domain_stage      stage;
698         union {
699                 struct arm_smmu_s1_cfg  s1_cfg;
700                 struct arm_smmu_s2_cfg  s2_cfg;
701         };
702
703         struct iommu_domain             domain;
704
705         struct list_head                devices;
706         spinlock_t                      devices_lock;
707 };
708
709 struct arm_smmu_option_prop {
710         u32 opt;
711         const char *prop;
712 };
713
714 static struct arm_smmu_option_prop arm_smmu_options[] = {
715         { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" },
716         { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"},
717         { 0, NULL},
718 };
719
720 static inline void __iomem *arm_smmu_page1_fixup(unsigned long offset,
721                                                  struct arm_smmu_device *smmu)
722 {
723         if ((offset > SZ_64K) &&
724             (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY))
725                 offset -= SZ_64K;
726
727         return smmu->base + offset;
728 }
729
730 static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
731 {
732         return container_of(dom, struct arm_smmu_domain, domain);
733 }
734
735 static void parse_driver_options(struct arm_smmu_device *smmu)
736 {
737         int i = 0;
738
739         do {
740                 if (of_property_read_bool(smmu->dev->of_node,
741                                                 arm_smmu_options[i].prop)) {
742                         smmu->options |= arm_smmu_options[i].opt;
743                         dev_notice(smmu->dev, "option %s\n",
744                                 arm_smmu_options[i].prop);
745                 }
746         } while (arm_smmu_options[++i].opt);
747 }
748
749 /* Low-level queue manipulation functions */
750 static bool queue_has_space(struct arm_smmu_ll_queue *q, u32 n)
751 {
752         u32 space, prod, cons;
753
754         prod = Q_IDX(q, q->prod);
755         cons = Q_IDX(q, q->cons);
756
757         if (Q_WRP(q, q->prod) == Q_WRP(q, q->cons))
758                 space = (1 << q->max_n_shift) - (prod - cons);
759         else
760                 space = cons - prod;
761
762         return space >= n;
763 }
764
765 static bool queue_full(struct arm_smmu_ll_queue *q)
766 {
767         return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
768                Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
769 }
770
771 static bool queue_empty(struct arm_smmu_ll_queue *q)
772 {
773         return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
774                Q_WRP(q, q->prod) == Q_WRP(q, q->cons);
775 }
776
777 static bool queue_consumed(struct arm_smmu_ll_queue *q, u32 prod)
778 {
779         return ((Q_WRP(q, q->cons) == Q_WRP(q, prod)) &&
780                 (Q_IDX(q, q->cons) > Q_IDX(q, prod))) ||
781                ((Q_WRP(q, q->cons) != Q_WRP(q, prod)) &&
782                 (Q_IDX(q, q->cons) <= Q_IDX(q, prod)));
783 }
784
785 static void queue_sync_cons_out(struct arm_smmu_queue *q)
786 {
787         /*
788          * Ensure that all CPU accesses (reads and writes) to the queue
789          * are complete before we update the cons pointer.
790          */
791         mb();
792         writel_relaxed(q->llq.cons, q->cons_reg);
793 }
794
795 static void queue_inc_cons(struct arm_smmu_ll_queue *q)
796 {
797         u32 cons = (Q_WRP(q, q->cons) | Q_IDX(q, q->cons)) + 1;
798         q->cons = Q_OVF(q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
799 }
800
801 static int queue_sync_prod_in(struct arm_smmu_queue *q)
802 {
803         int ret = 0;
804         u32 prod = readl_relaxed(q->prod_reg);
805
806         if (Q_OVF(prod) != Q_OVF(q->llq.prod))
807                 ret = -EOVERFLOW;
808
809         q->llq.prod = prod;
810         return ret;
811 }
812
813 static u32 queue_inc_prod_n(struct arm_smmu_ll_queue *q, int n)
814 {
815         u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + n;
816         return Q_OVF(q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
817 }
818
819 static void queue_poll_init(struct arm_smmu_device *smmu,
820                             struct arm_smmu_queue_poll *qp)
821 {
822         qp->delay = 1;
823         qp->spin_cnt = 0;
824         qp->wfe = !!(smmu->features & ARM_SMMU_FEAT_SEV);
825         qp->timeout = ktime_add_us(ktime_get(), ARM_SMMU_POLL_TIMEOUT_US);
826 }
827
828 static int queue_poll(struct arm_smmu_queue_poll *qp)
829 {
830         if (ktime_compare(ktime_get(), qp->timeout) > 0)
831                 return -ETIMEDOUT;
832
833         if (qp->wfe) {
834                 wfe();
835         } else if (++qp->spin_cnt < ARM_SMMU_POLL_SPIN_COUNT) {
836                 cpu_relax();
837         } else {
838                 udelay(qp->delay);
839                 qp->delay *= 2;
840                 qp->spin_cnt = 0;
841         }
842
843         return 0;
844 }
845
846 static void queue_write(__le64 *dst, u64 *src, size_t n_dwords)
847 {
848         int i;
849
850         for (i = 0; i < n_dwords; ++i)
851                 *dst++ = cpu_to_le64(*src++);
852 }
853
854 static void queue_read(__le64 *dst, u64 *src, size_t n_dwords)
855 {
856         int i;
857
858         for (i = 0; i < n_dwords; ++i)
859                 *dst++ = le64_to_cpu(*src++);
860 }
861
862 static int queue_remove_raw(struct arm_smmu_queue *q, u64 *ent)
863 {
864         if (queue_empty(&q->llq))
865                 return -EAGAIN;
866
867         queue_read(ent, Q_ENT(q, q->llq.cons), q->ent_dwords);
868         queue_inc_cons(&q->llq);
869         queue_sync_cons_out(q);
870         return 0;
871 }
872
873 /* High-level queue accessors */
874 static int arm_smmu_cmdq_build_cmd(u64 *cmd, struct arm_smmu_cmdq_ent *ent)
875 {
876         memset(cmd, 0, 1 << CMDQ_ENT_SZ_SHIFT);
877         cmd[0] |= FIELD_PREP(CMDQ_0_OP, ent->opcode);
878
879         switch (ent->opcode) {
880         case CMDQ_OP_TLBI_EL2_ALL:
881         case CMDQ_OP_TLBI_NSNH_ALL:
882                 break;
883         case CMDQ_OP_PREFETCH_CFG:
884                 cmd[0] |= FIELD_PREP(CMDQ_PREFETCH_0_SID, ent->prefetch.sid);
885                 cmd[1] |= FIELD_PREP(CMDQ_PREFETCH_1_SIZE, ent->prefetch.size);
886                 cmd[1] |= ent->prefetch.addr & CMDQ_PREFETCH_1_ADDR_MASK;
887                 break;
888         case CMDQ_OP_CFGI_CD:
889                 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SSID, ent->cfgi.ssid);
890                 /* Fallthrough */
891         case CMDQ_OP_CFGI_STE:
892                 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, ent->cfgi.sid);
893                 cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_LEAF, ent->cfgi.leaf);
894                 break;
895         case CMDQ_OP_CFGI_CD_ALL:
896                 cmd[0] |= FIELD_PREP(CMDQ_CFGI_0_SID, ent->cfgi.sid);
897                 break;
898         case CMDQ_OP_CFGI_ALL:
899                 /* Cover the entire SID range */
900                 cmd[1] |= FIELD_PREP(CMDQ_CFGI_1_RANGE, 31);
901                 break;
902         case CMDQ_OP_TLBI_NH_VA:
903                 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
904                 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid);
905                 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf);
906                 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_VA_MASK;
907                 break;
908         case CMDQ_OP_TLBI_S2_IPA:
909                 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
910                 cmd[1] |= FIELD_PREP(CMDQ_TLBI_1_LEAF, ent->tlbi.leaf);
911                 cmd[1] |= ent->tlbi.addr & CMDQ_TLBI_1_IPA_MASK;
912                 break;
913         case CMDQ_OP_TLBI_NH_ASID:
914                 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_ASID, ent->tlbi.asid);
915                 /* Fallthrough */
916         case CMDQ_OP_TLBI_S12_VMALL:
917                 cmd[0] |= FIELD_PREP(CMDQ_TLBI_0_VMID, ent->tlbi.vmid);
918                 break;
919         case CMDQ_OP_ATC_INV:
920                 cmd[0] |= FIELD_PREP(CMDQ_0_SSV, ent->substream_valid);
921                 cmd[0] |= FIELD_PREP(CMDQ_ATC_0_GLOBAL, ent->atc.global);
922                 cmd[0] |= FIELD_PREP(CMDQ_ATC_0_SSID, ent->atc.ssid);
923                 cmd[0] |= FIELD_PREP(CMDQ_ATC_0_SID, ent->atc.sid);
924                 cmd[1] |= FIELD_PREP(CMDQ_ATC_1_SIZE, ent->atc.size);
925                 cmd[1] |= ent->atc.addr & CMDQ_ATC_1_ADDR_MASK;
926                 break;
927         case CMDQ_OP_PRI_RESP:
928                 cmd[0] |= FIELD_PREP(CMDQ_0_SSV, ent->substream_valid);
929                 cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SSID, ent->pri.ssid);
930                 cmd[0] |= FIELD_PREP(CMDQ_PRI_0_SID, ent->pri.sid);
931                 cmd[1] |= FIELD_PREP(CMDQ_PRI_1_GRPID, ent->pri.grpid);
932                 switch (ent->pri.resp) {
933                 case PRI_RESP_DENY:
934                 case PRI_RESP_FAIL:
935                 case PRI_RESP_SUCC:
936                         break;
937                 default:
938                         return -EINVAL;
939                 }
940                 cmd[1] |= FIELD_PREP(CMDQ_PRI_1_RESP, ent->pri.resp);
941                 break;
942         case CMDQ_OP_CMD_SYNC:
943                 if (ent->sync.msiaddr) {
944                         cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_IRQ);
945                         cmd[1] |= ent->sync.msiaddr & CMDQ_SYNC_1_MSIADDR_MASK;
946                 } else {
947                         cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_CS, CMDQ_SYNC_0_CS_SEV);
948                 }
949                 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSH, ARM_SMMU_SH_ISH);
950                 cmd[0] |= FIELD_PREP(CMDQ_SYNC_0_MSIATTR, ARM_SMMU_MEMATTR_OIWB);
951                 break;
952         default:
953                 return -ENOENT;
954         }
955
956         return 0;
957 }
958
959 static void arm_smmu_cmdq_build_sync_cmd(u64 *cmd, struct arm_smmu_device *smmu,
960                                          u32 prod)
961 {
962         struct arm_smmu_queue *q = &smmu->cmdq.q;
963         struct arm_smmu_cmdq_ent ent = {
964                 .opcode = CMDQ_OP_CMD_SYNC,
965         };
966
967         /*
968          * Beware that Hi16xx adds an extra 32 bits of goodness to its MSI
969          * payload, so the write will zero the entire command on that platform.
970          */
971         if (smmu->features & ARM_SMMU_FEAT_MSI &&
972             smmu->features & ARM_SMMU_FEAT_COHERENCY) {
973                 ent.sync.msiaddr = q->base_dma + Q_IDX(&q->llq, prod) *
974                                    q->ent_dwords * 8;
975         }
976
977         arm_smmu_cmdq_build_cmd(cmd, &ent);
978 }
979
980 static void arm_smmu_cmdq_skip_err(struct arm_smmu_device *smmu)
981 {
982         static const char *cerror_str[] = {
983                 [CMDQ_ERR_CERROR_NONE_IDX]      = "No error",
984                 [CMDQ_ERR_CERROR_ILL_IDX]       = "Illegal command",
985                 [CMDQ_ERR_CERROR_ABT_IDX]       = "Abort on command fetch",
986                 [CMDQ_ERR_CERROR_ATC_INV_IDX]   = "ATC invalidate timeout",
987         };
988
989         int i;
990         u64 cmd[CMDQ_ENT_DWORDS];
991         struct arm_smmu_queue *q = &smmu->cmdq.q;
992         u32 cons = readl_relaxed(q->cons_reg);
993         u32 idx = FIELD_GET(CMDQ_CONS_ERR, cons);
994         struct arm_smmu_cmdq_ent cmd_sync = {
995                 .opcode = CMDQ_OP_CMD_SYNC,
996         };
997
998         dev_err(smmu->dev, "CMDQ error (cons 0x%08x): %s\n", cons,
999                 idx < ARRAY_SIZE(cerror_str) ?  cerror_str[idx] : "Unknown");
1000
1001         switch (idx) {
1002         case CMDQ_ERR_CERROR_ABT_IDX:
1003                 dev_err(smmu->dev, "retrying command fetch\n");
1004         case CMDQ_ERR_CERROR_NONE_IDX:
1005                 return;
1006         case CMDQ_ERR_CERROR_ATC_INV_IDX:
1007                 /*
1008                  * ATC Invalidation Completion timeout. CONS is still pointing
1009                  * at the CMD_SYNC. Attempt to complete other pending commands
1010                  * by repeating the CMD_SYNC, though we might well end up back
1011                  * here since the ATC invalidation may still be pending.
1012                  */
1013                 return;
1014         case CMDQ_ERR_CERROR_ILL_IDX:
1015                 /* Fallthrough */
1016         default:
1017                 break;
1018         }
1019
1020         /*
1021          * We may have concurrent producers, so we need to be careful
1022          * not to touch any of the shadow cmdq state.
1023          */
1024         queue_read(cmd, Q_ENT(q, cons), q->ent_dwords);
1025         dev_err(smmu->dev, "skipping command in error state:\n");
1026         for (i = 0; i < ARRAY_SIZE(cmd); ++i)
1027                 dev_err(smmu->dev, "\t0x%016llx\n", (unsigned long long)cmd[i]);
1028
1029         /* Convert the erroneous command into a CMD_SYNC */
1030         if (arm_smmu_cmdq_build_cmd(cmd, &cmd_sync)) {
1031                 dev_err(smmu->dev, "failed to convert to CMD_SYNC\n");
1032                 return;
1033         }
1034
1035         queue_write(Q_ENT(q, cons), cmd, q->ent_dwords);
1036 }
1037
1038 /*
1039  * Command queue locking.
1040  * This is a form of bastardised rwlock with the following major changes:
1041  *
1042  * - The only LOCK routines are exclusive_trylock() and shared_lock().
1043  *   Neither have barrier semantics, and instead provide only a control
1044  *   dependency.
1045  *
1046  * - The UNLOCK routines are supplemented with shared_tryunlock(), which
1047  *   fails if the caller appears to be the last lock holder (yes, this is
1048  *   racy). All successful UNLOCK routines have RELEASE semantics.
1049  */
1050 static void arm_smmu_cmdq_shared_lock(struct arm_smmu_cmdq *cmdq)
1051 {
1052         int val;
1053
1054         /*
1055          * We can try to avoid the cmpxchg() loop by simply incrementing the
1056          * lock counter. When held in exclusive state, the lock counter is set
1057          * to INT_MIN so these increments won't hurt as the value will remain
1058          * negative.
1059          */
1060         if (atomic_fetch_inc_relaxed(&cmdq->lock) >= 0)
1061                 return;
1062
1063         do {
1064                 val = atomic_cond_read_relaxed(&cmdq->lock, VAL >= 0);
1065         } while (atomic_cmpxchg_relaxed(&cmdq->lock, val, val + 1) != val);
1066 }
1067
1068 static void arm_smmu_cmdq_shared_unlock(struct arm_smmu_cmdq *cmdq)
1069 {
1070         (void)atomic_dec_return_release(&cmdq->lock);
1071 }
1072
1073 static bool arm_smmu_cmdq_shared_tryunlock(struct arm_smmu_cmdq *cmdq)
1074 {
1075         if (atomic_read(&cmdq->lock) == 1)
1076                 return false;
1077
1078         arm_smmu_cmdq_shared_unlock(cmdq);
1079         return true;
1080 }
1081
1082 #define arm_smmu_cmdq_exclusive_trylock_irqsave(cmdq, flags)            \
1083 ({                                                                      \
1084         bool __ret;                                                     \
1085         local_irq_save(flags);                                          \
1086         __ret = !atomic_cmpxchg_relaxed(&cmdq->lock, 0, INT_MIN);       \
1087         if (!__ret)                                                     \
1088                 local_irq_restore(flags);                               \
1089         __ret;                                                          \
1090 })
1091
1092 #define arm_smmu_cmdq_exclusive_unlock_irqrestore(cmdq, flags)          \
1093 ({                                                                      \
1094         atomic_set_release(&cmdq->lock, 0);                             \
1095         local_irq_restore(flags);                                       \
1096 })
1097
1098
1099 /*
1100  * Command queue insertion.
1101  * This is made fiddly by our attempts to achieve some sort of scalability
1102  * since there is one queue shared amongst all of the CPUs in the system.  If
1103  * you like mixed-size concurrency, dependency ordering and relaxed atomics,
1104  * then you'll *love* this monstrosity.
1105  *
1106  * The basic idea is to split the queue up into ranges of commands that are
1107  * owned by a given CPU; the owner may not have written all of the commands
1108  * itself, but is responsible for advancing the hardware prod pointer when
1109  * the time comes. The algorithm is roughly:
1110  *
1111  *      1. Allocate some space in the queue. At this point we also discover
1112  *         whether the head of the queue is currently owned by another CPU,
1113  *         or whether we are the owner.
1114  *
1115  *      2. Write our commands into our allocated slots in the queue.
1116  *
1117  *      3. Mark our slots as valid in arm_smmu_cmdq.valid_map.
1118  *
1119  *      4. If we are an owner:
1120  *              a. Wait for the previous owner to finish.
1121  *              b. Mark the queue head as unowned, which tells us the range
1122  *                 that we are responsible for publishing.
1123  *              c. Wait for all commands in our owned range to become valid.
1124  *              d. Advance the hardware prod pointer.
1125  *              e. Tell the next owner we've finished.
1126  *
1127  *      5. If we are inserting a CMD_SYNC (we may or may not have been an
1128  *         owner), then we need to stick around until it has completed:
1129  *              a. If we have MSIs, the SMMU can write back into the CMD_SYNC
1130  *                 to clear the first 4 bytes.
1131  *              b. Otherwise, we spin waiting for the hardware cons pointer to
1132  *                 advance past our command.
1133  *
1134  * The devil is in the details, particularly the use of locking for handling
1135  * SYNC completion and freeing up space in the queue before we think that it is
1136  * full.
1137  */
1138 static void __arm_smmu_cmdq_poll_set_valid_map(struct arm_smmu_cmdq *cmdq,
1139                                                u32 sprod, u32 eprod, bool set)
1140 {
1141         u32 swidx, sbidx, ewidx, ebidx;
1142         struct arm_smmu_ll_queue llq = {
1143                 .max_n_shift    = cmdq->q.llq.max_n_shift,
1144                 .prod           = sprod,
1145         };
1146
1147         ewidx = BIT_WORD(Q_IDX(&llq, eprod));
1148         ebidx = Q_IDX(&llq, eprod) % BITS_PER_LONG;
1149
1150         while (llq.prod != eprod) {
1151                 unsigned long mask;
1152                 atomic_long_t *ptr;
1153                 u32 limit = BITS_PER_LONG;
1154
1155                 swidx = BIT_WORD(Q_IDX(&llq, llq.prod));
1156                 sbidx = Q_IDX(&llq, llq.prod) % BITS_PER_LONG;
1157
1158                 ptr = &cmdq->valid_map[swidx];
1159
1160                 if ((swidx == ewidx) && (sbidx < ebidx))
1161                         limit = ebidx;
1162
1163                 mask = GENMASK(limit - 1, sbidx);
1164
1165                 /*
1166                  * The valid bit is the inverse of the wrap bit. This means
1167                  * that a zero-initialised queue is invalid and, after marking
1168                  * all entries as valid, they become invalid again when we
1169                  * wrap.
1170                  */
1171                 if (set) {
1172                         atomic_long_xor(mask, ptr);
1173                 } else { /* Poll */
1174                         unsigned long valid;
1175
1176                         valid = (ULONG_MAX + !!Q_WRP(&llq, llq.prod)) & mask;
1177                         atomic_long_cond_read_relaxed(ptr, (VAL & mask) == valid);
1178                 }
1179
1180                 llq.prod = queue_inc_prod_n(&llq, limit - sbidx);
1181         }
1182 }
1183
1184 /* Mark all entries in the range [sprod, eprod) as valid */
1185 static void arm_smmu_cmdq_set_valid_map(struct arm_smmu_cmdq *cmdq,
1186                                         u32 sprod, u32 eprod)
1187 {
1188         __arm_smmu_cmdq_poll_set_valid_map(cmdq, sprod, eprod, true);
1189 }
1190
1191 /* Wait for all entries in the range [sprod, eprod) to become valid */
1192 static void arm_smmu_cmdq_poll_valid_map(struct arm_smmu_cmdq *cmdq,
1193                                          u32 sprod, u32 eprod)
1194 {
1195         __arm_smmu_cmdq_poll_set_valid_map(cmdq, sprod, eprod, false);
1196 }
1197
1198 /* Wait for the command queue to become non-full */
1199 static int arm_smmu_cmdq_poll_until_not_full(struct arm_smmu_device *smmu,
1200                                              struct arm_smmu_ll_queue *llq)
1201 {
1202         unsigned long flags;
1203         struct arm_smmu_queue_poll qp;
1204         struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
1205         int ret = 0;
1206
1207         /*
1208          * Try to update our copy of cons by grabbing exclusive cmdq access. If
1209          * that fails, spin until somebody else updates it for us.
1210          */
1211         if (arm_smmu_cmdq_exclusive_trylock_irqsave(cmdq, flags)) {
1212                 WRITE_ONCE(cmdq->q.llq.cons, readl_relaxed(cmdq->q.cons_reg));
1213                 arm_smmu_cmdq_exclusive_unlock_irqrestore(cmdq, flags);
1214                 llq->val = READ_ONCE(cmdq->q.llq.val);
1215                 return 0;
1216         }
1217
1218         queue_poll_init(smmu, &qp);
1219         do {
1220                 llq->val = READ_ONCE(smmu->cmdq.q.llq.val);
1221                 if (!queue_full(llq))
1222                         break;
1223
1224                 ret = queue_poll(&qp);
1225         } while (!ret);
1226
1227         return ret;
1228 }
1229
1230 /*
1231  * Wait until the SMMU signals a CMD_SYNC completion MSI.
1232  * Must be called with the cmdq lock held in some capacity.
1233  */
1234 static int __arm_smmu_cmdq_poll_until_msi(struct arm_smmu_device *smmu,
1235                                           struct arm_smmu_ll_queue *llq)
1236 {
1237         int ret = 0;
1238         struct arm_smmu_queue_poll qp;
1239         struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
1240         u32 *cmd = (u32 *)(Q_ENT(&cmdq->q, llq->prod));
1241
1242         queue_poll_init(smmu, &qp);
1243
1244         /*
1245          * The MSI won't generate an event, since it's being written back
1246          * into the command queue.
1247          */
1248         qp.wfe = false;
1249         smp_cond_load_relaxed(cmd, !VAL || (ret = queue_poll(&qp)));
1250         llq->cons = ret ? llq->prod : queue_inc_prod_n(llq, 1);
1251         return ret;
1252 }
1253
1254 /*
1255  * Wait until the SMMU cons index passes llq->prod.
1256  * Must be called with the cmdq lock held in some capacity.
1257  */
1258 static int __arm_smmu_cmdq_poll_until_consumed(struct arm_smmu_device *smmu,
1259                                                struct arm_smmu_ll_queue *llq)
1260 {
1261         struct arm_smmu_queue_poll qp;
1262         struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
1263         u32 prod = llq->prod;
1264         int ret = 0;
1265
1266         queue_poll_init(smmu, &qp);
1267         llq->val = READ_ONCE(smmu->cmdq.q.llq.val);
1268         do {
1269                 if (queue_consumed(llq, prod))
1270                         break;
1271
1272                 ret = queue_poll(&qp);
1273
1274                 /*
1275                  * This needs to be a readl() so that our subsequent call
1276                  * to arm_smmu_cmdq_shared_tryunlock() can fail accurately.
1277                  *
1278                  * Specifically, we need to ensure that we observe all
1279                  * shared_lock()s by other CMD_SYNCs that share our owner,
1280                  * so that a failing call to tryunlock() means that we're
1281                  * the last one out and therefore we can safely advance
1282                  * cmdq->q.llq.cons. Roughly speaking:
1283                  *
1284                  * CPU 0                CPU1                    CPU2 (us)
1285                  *
1286                  * if (sync)
1287                  *      shared_lock();
1288                  *
1289                  * dma_wmb();
1290                  * set_valid_map();
1291                  *
1292                  *                      if (owner) {
1293                  *                              poll_valid_map();
1294                  *                              <control dependency>
1295                  *                              writel(prod_reg);
1296                  *
1297                  *                                              readl(cons_reg);
1298                  *                                              tryunlock();
1299                  *
1300                  * Requires us to see CPU 0's shared_lock() acquisition.
1301                  */
1302                 llq->cons = readl(cmdq->q.cons_reg);
1303         } while (!ret);
1304
1305         return ret;
1306 }
1307
1308 static int arm_smmu_cmdq_poll_until_sync(struct arm_smmu_device *smmu,
1309                                          struct arm_smmu_ll_queue *llq)
1310 {
1311         if (smmu->features & ARM_SMMU_FEAT_MSI &&
1312             smmu->features & ARM_SMMU_FEAT_COHERENCY)
1313                 return __arm_smmu_cmdq_poll_until_msi(smmu, llq);
1314
1315         return __arm_smmu_cmdq_poll_until_consumed(smmu, llq);
1316 }
1317
1318 static void arm_smmu_cmdq_write_entries(struct arm_smmu_cmdq *cmdq, u64 *cmds,
1319                                         u32 prod, int n)
1320 {
1321         int i;
1322         struct arm_smmu_ll_queue llq = {
1323                 .max_n_shift    = cmdq->q.llq.max_n_shift,
1324                 .prod           = prod,
1325         };
1326
1327         for (i = 0; i < n; ++i) {
1328                 u64 *cmd = &cmds[i * CMDQ_ENT_DWORDS];
1329
1330                 prod = queue_inc_prod_n(&llq, i);
1331                 queue_write(Q_ENT(&cmdq->q, prod), cmd, CMDQ_ENT_DWORDS);
1332         }
1333 }
1334
1335 /*
1336  * This is the actual insertion function, and provides the following
1337  * ordering guarantees to callers:
1338  *
1339  * - There is a dma_wmb() before publishing any commands to the queue.
1340  *   This can be relied upon to order prior writes to data structures
1341  *   in memory (such as a CD or an STE) before the command.
1342  *
1343  * - On completion of a CMD_SYNC, there is a control dependency.
1344  *   This can be relied upon to order subsequent writes to memory (e.g.
1345  *   freeing an IOVA) after completion of the CMD_SYNC.
1346  *
1347  * - Command insertion is totally ordered, so if two CPUs each race to
1348  *   insert their own list of commands then all of the commands from one
1349  *   CPU will appear before any of the commands from the other CPU.
1350  */
1351 static int arm_smmu_cmdq_issue_cmdlist(struct arm_smmu_device *smmu,
1352                                        u64 *cmds, int n, bool sync)
1353 {
1354         u64 cmd_sync[CMDQ_ENT_DWORDS];
1355         u32 prod;
1356         unsigned long flags;
1357         bool owner;
1358         struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
1359         struct arm_smmu_ll_queue llq = {
1360                 .max_n_shift = cmdq->q.llq.max_n_shift,
1361         }, head = llq;
1362         int ret = 0;
1363
1364         /* 1. Allocate some space in the queue */
1365         local_irq_save(flags);
1366         llq.val = READ_ONCE(cmdq->q.llq.val);
1367         do {
1368                 u64 old;
1369
1370                 while (!queue_has_space(&llq, n + sync)) {
1371                         local_irq_restore(flags);
1372                         if (arm_smmu_cmdq_poll_until_not_full(smmu, &llq))
1373                                 dev_err_ratelimited(smmu->dev, "CMDQ timeout\n");
1374                         local_irq_save(flags);
1375                 }
1376
1377                 head.cons = llq.cons;
1378                 head.prod = queue_inc_prod_n(&llq, n + sync) |
1379                                              CMDQ_PROD_OWNED_FLAG;
1380
1381                 old = cmpxchg_relaxed(&cmdq->q.llq.val, llq.val, head.val);
1382                 if (old == llq.val)
1383                         break;
1384
1385                 llq.val = old;
1386         } while (1);
1387         owner = !(llq.prod & CMDQ_PROD_OWNED_FLAG);
1388         head.prod &= ~CMDQ_PROD_OWNED_FLAG;
1389         llq.prod &= ~CMDQ_PROD_OWNED_FLAG;
1390
1391         /*
1392          * 2. Write our commands into the queue
1393          * Dependency ordering from the cmpxchg() loop above.
1394          */
1395         arm_smmu_cmdq_write_entries(cmdq, cmds, llq.prod, n);
1396         if (sync) {
1397                 prod = queue_inc_prod_n(&llq, n);
1398                 arm_smmu_cmdq_build_sync_cmd(cmd_sync, smmu, prod);
1399                 queue_write(Q_ENT(&cmdq->q, prod), cmd_sync, CMDQ_ENT_DWORDS);
1400
1401                 /*
1402                  * In order to determine completion of our CMD_SYNC, we must
1403                  * ensure that the queue can't wrap twice without us noticing.
1404                  * We achieve that by taking the cmdq lock as shared before
1405                  * marking our slot as valid.
1406                  */
1407                 arm_smmu_cmdq_shared_lock(cmdq);
1408         }
1409
1410         /* 3. Mark our slots as valid, ensuring commands are visible first */
1411         dma_wmb();
1412         arm_smmu_cmdq_set_valid_map(cmdq, llq.prod, head.prod);
1413
1414         /* 4. If we are the owner, take control of the SMMU hardware */
1415         if (owner) {
1416                 /* a. Wait for previous owner to finish */
1417                 atomic_cond_read_relaxed(&cmdq->owner_prod, VAL == llq.prod);
1418
1419                 /* b. Stop gathering work by clearing the owned flag */
1420                 prod = atomic_fetch_andnot_relaxed(CMDQ_PROD_OWNED_FLAG,
1421                                                    &cmdq->q.llq.atomic.prod);
1422                 prod &= ~CMDQ_PROD_OWNED_FLAG;
1423
1424                 /*
1425                  * c. Wait for any gathered work to be written to the queue.
1426                  * Note that we read our own entries so that we have the control
1427                  * dependency required by (d).
1428                  */
1429                 arm_smmu_cmdq_poll_valid_map(cmdq, llq.prod, prod);
1430
1431                 /*
1432                  * d. Advance the hardware prod pointer
1433                  * Control dependency ordering from the entries becoming valid.
1434                  */
1435                 writel_relaxed(prod, cmdq->q.prod_reg);
1436
1437                 /*
1438                  * e. Tell the next owner we're done
1439                  * Make sure we've updated the hardware first, so that we don't
1440                  * race to update prod and potentially move it backwards.
1441                  */
1442                 atomic_set_release(&cmdq->owner_prod, prod);
1443         }
1444
1445         /* 5. If we are inserting a CMD_SYNC, we must wait for it to complete */
1446         if (sync) {
1447                 llq.prod = queue_inc_prod_n(&llq, n);
1448                 ret = arm_smmu_cmdq_poll_until_sync(smmu, &llq);
1449                 if (ret) {
1450                         dev_err_ratelimited(smmu->dev,
1451                                             "CMD_SYNC timeout at 0x%08x [hwprod 0x%08x, hwcons 0x%08x]\n",
1452                                             llq.prod,
1453                                             readl_relaxed(cmdq->q.prod_reg),
1454                                             readl_relaxed(cmdq->q.cons_reg));
1455                 }
1456
1457                 /*
1458                  * Try to unlock the cmq lock. This will fail if we're the last
1459                  * reader, in which case we can safely update cmdq->q.llq.cons
1460                  */
1461                 if (!arm_smmu_cmdq_shared_tryunlock(cmdq)) {
1462                         WRITE_ONCE(cmdq->q.llq.cons, llq.cons);
1463                         arm_smmu_cmdq_shared_unlock(cmdq);
1464                 }
1465         }
1466
1467         local_irq_restore(flags);
1468         return ret;
1469 }
1470
1471 static int arm_smmu_cmdq_issue_cmd(struct arm_smmu_device *smmu,
1472                                    struct arm_smmu_cmdq_ent *ent)
1473 {
1474         u64 cmd[CMDQ_ENT_DWORDS];
1475
1476         if (arm_smmu_cmdq_build_cmd(cmd, ent)) {
1477                 dev_warn(smmu->dev, "ignoring unknown CMDQ opcode 0x%x\n",
1478                          ent->opcode);
1479                 return -EINVAL;
1480         }
1481
1482         return arm_smmu_cmdq_issue_cmdlist(smmu, cmd, 1, false);
1483 }
1484
1485 static int arm_smmu_cmdq_issue_sync(struct arm_smmu_device *smmu)
1486 {
1487         return arm_smmu_cmdq_issue_cmdlist(smmu, NULL, 0, true);
1488 }
1489
1490 static void arm_smmu_cmdq_batch_add(struct arm_smmu_device *smmu,
1491                                     struct arm_smmu_cmdq_batch *cmds,
1492                                     struct arm_smmu_cmdq_ent *cmd)
1493 {
1494         if (cmds->num == CMDQ_BATCH_ENTRIES) {
1495                 arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmds, cmds->num, false);
1496                 cmds->num = 0;
1497         }
1498         arm_smmu_cmdq_build_cmd(&cmds->cmds[cmds->num * CMDQ_ENT_DWORDS], cmd);
1499         cmds->num++;
1500 }
1501
1502 static int arm_smmu_cmdq_batch_submit(struct arm_smmu_device *smmu,
1503                                       struct arm_smmu_cmdq_batch *cmds)
1504 {
1505         return arm_smmu_cmdq_issue_cmdlist(smmu, cmds->cmds, cmds->num, true);
1506 }
1507
1508 /* Context descriptor manipulation functions */
1509 static void arm_smmu_sync_cd(struct arm_smmu_domain *smmu_domain,
1510                              int ssid, bool leaf)
1511 {
1512         size_t i;
1513         unsigned long flags;
1514         struct arm_smmu_master *master;
1515         struct arm_smmu_device *smmu = smmu_domain->smmu;
1516         struct arm_smmu_cmdq_ent cmd = {
1517                 .opcode = CMDQ_OP_CFGI_CD,
1518                 .cfgi   = {
1519                         .ssid   = ssid,
1520                         .leaf   = leaf,
1521                 },
1522         };
1523
1524         spin_lock_irqsave(&smmu_domain->devices_lock, flags);
1525         list_for_each_entry(master, &smmu_domain->devices, domain_head) {
1526                 for (i = 0; i < master->num_sids; i++) {
1527                         cmd.cfgi.sid = master->sids[i];
1528                         arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1529                 }
1530         }
1531         spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
1532
1533         arm_smmu_cmdq_issue_sync(smmu);
1534 }
1535
1536 static int arm_smmu_alloc_cd_leaf_table(struct arm_smmu_device *smmu,
1537                                         struct arm_smmu_l1_ctx_desc *l1_desc)
1538 {
1539         size_t size = CTXDESC_L2_ENTRIES * (CTXDESC_CD_DWORDS << 3);
1540
1541         l1_desc->l2ptr = dmam_alloc_coherent(smmu->dev, size,
1542                                              &l1_desc->l2ptr_dma, GFP_KERNEL);
1543         if (!l1_desc->l2ptr) {
1544                 dev_warn(smmu->dev,
1545                          "failed to allocate context descriptor table\n");
1546                 return -ENOMEM;
1547         }
1548         return 0;
1549 }
1550
1551 static void arm_smmu_write_cd_l1_desc(__le64 *dst,
1552                                       struct arm_smmu_l1_ctx_desc *l1_desc)
1553 {
1554         u64 val = (l1_desc->l2ptr_dma & CTXDESC_L1_DESC_L2PTR_MASK) |
1555                   CTXDESC_L1_DESC_V;
1556
1557         /* See comment in arm_smmu_write_ctx_desc() */
1558         WRITE_ONCE(*dst, cpu_to_le64(val));
1559 }
1560
1561 static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_domain *smmu_domain,
1562                                    u32 ssid)
1563 {
1564         __le64 *l1ptr;
1565         unsigned int idx;
1566         struct arm_smmu_l1_ctx_desc *l1_desc;
1567         struct arm_smmu_device *smmu = smmu_domain->smmu;
1568         struct arm_smmu_ctx_desc_cfg *cdcfg = &smmu_domain->s1_cfg.cdcfg;
1569
1570         if (smmu_domain->s1_cfg.s1fmt == STRTAB_STE_0_S1FMT_LINEAR)
1571                 return cdcfg->cdtab + ssid * CTXDESC_CD_DWORDS;
1572
1573         idx = ssid >> CTXDESC_SPLIT;
1574         l1_desc = &cdcfg->l1_desc[idx];
1575         if (!l1_desc->l2ptr) {
1576                 if (arm_smmu_alloc_cd_leaf_table(smmu, l1_desc))
1577                         return NULL;
1578
1579                 l1ptr = cdcfg->cdtab + idx * CTXDESC_L1_DESC_DWORDS;
1580                 arm_smmu_write_cd_l1_desc(l1ptr, l1_desc);
1581                 /* An invalid L1CD can be cached */
1582                 arm_smmu_sync_cd(smmu_domain, ssid, false);
1583         }
1584         idx = ssid & (CTXDESC_L2_ENTRIES - 1);
1585         return l1_desc->l2ptr + idx * CTXDESC_CD_DWORDS;
1586 }
1587
1588 static int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain,
1589                                    int ssid, struct arm_smmu_ctx_desc *cd)
1590 {
1591         /*
1592          * This function handles the following cases:
1593          *
1594          * (1) Install primary CD, for normal DMA traffic (SSID = 0).
1595          * (2) Install a secondary CD, for SID+SSID traffic.
1596          * (3) Update ASID of a CD. Atomically write the first 64 bits of the
1597          *     CD, then invalidate the old entry and mappings.
1598          * (4) Remove a secondary CD.
1599          */
1600         u64 val;
1601         bool cd_live;
1602         __le64 *cdptr;
1603         struct arm_smmu_device *smmu = smmu_domain->smmu;
1604
1605         if (WARN_ON(ssid >= (1 << smmu_domain->s1_cfg.s1cdmax)))
1606                 return -E2BIG;
1607
1608         cdptr = arm_smmu_get_cd_ptr(smmu_domain, ssid);
1609         if (!cdptr)
1610                 return -ENOMEM;
1611
1612         val = le64_to_cpu(cdptr[0]);
1613         cd_live = !!(val & CTXDESC_CD_0_V);
1614
1615         if (!cd) { /* (4) */
1616                 val = 0;
1617         } else if (cd_live) { /* (3) */
1618                 val &= ~CTXDESC_CD_0_ASID;
1619                 val |= FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid);
1620                 /*
1621                  * Until CD+TLB invalidation, both ASIDs may be used for tagging
1622                  * this substream's traffic
1623                  */
1624         } else { /* (1) and (2) */
1625                 cdptr[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK);
1626                 cdptr[2] = 0;
1627                 cdptr[3] = cpu_to_le64(cd->mair);
1628
1629                 /*
1630                  * STE is live, and the SMMU might read dwords of this CD in any
1631                  * order. Ensure that it observes valid values before reading
1632                  * V=1.
1633                  */
1634                 arm_smmu_sync_cd(smmu_domain, ssid, true);
1635
1636                 val = cd->tcr |
1637 #ifdef __BIG_ENDIAN
1638                         CTXDESC_CD_0_ENDI |
1639 #endif
1640                         CTXDESC_CD_0_R | CTXDESC_CD_0_A | CTXDESC_CD_0_ASET |
1641                         CTXDESC_CD_0_AA64 |
1642                         FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) |
1643                         CTXDESC_CD_0_V;
1644
1645                 /* STALL_MODEL==0b10 && CD.S==0 is ILLEGAL */
1646                 if (smmu->features & ARM_SMMU_FEAT_STALL_FORCE)
1647                         val |= CTXDESC_CD_0_S;
1648         }
1649
1650         /*
1651          * The SMMU accesses 64-bit values atomically. See IHI0070Ca 3.21.3
1652          * "Configuration structures and configuration invalidation completion"
1653          *
1654          *   The size of single-copy atomic reads made by the SMMU is
1655          *   IMPLEMENTATION DEFINED but must be at least 64 bits. Any single
1656          *   field within an aligned 64-bit span of a structure can be altered
1657          *   without first making the structure invalid.
1658          */
1659         WRITE_ONCE(cdptr[0], cpu_to_le64(val));
1660         arm_smmu_sync_cd(smmu_domain, ssid, true);
1661         return 0;
1662 }
1663
1664 static int arm_smmu_alloc_cd_tables(struct arm_smmu_domain *smmu_domain)
1665 {
1666         int ret;
1667         size_t l1size;
1668         size_t max_contexts;
1669         struct arm_smmu_device *smmu = smmu_domain->smmu;
1670         struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
1671         struct arm_smmu_ctx_desc_cfg *cdcfg = &cfg->cdcfg;
1672
1673         max_contexts = 1 << cfg->s1cdmax;
1674
1675         if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB) ||
1676             max_contexts <= CTXDESC_L2_ENTRIES) {
1677                 cfg->s1fmt = STRTAB_STE_0_S1FMT_LINEAR;
1678                 cdcfg->num_l1_ents = max_contexts;
1679
1680                 l1size = max_contexts * (CTXDESC_CD_DWORDS << 3);
1681         } else {
1682                 cfg->s1fmt = STRTAB_STE_0_S1FMT_64K_L2;
1683                 cdcfg->num_l1_ents = DIV_ROUND_UP(max_contexts,
1684                                                   CTXDESC_L2_ENTRIES);
1685
1686                 cdcfg->l1_desc = devm_kcalloc(smmu->dev, cdcfg->num_l1_ents,
1687                                               sizeof(*cdcfg->l1_desc),
1688                                               GFP_KERNEL);
1689                 if (!cdcfg->l1_desc)
1690                         return -ENOMEM;
1691
1692                 l1size = cdcfg->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3);
1693         }
1694
1695         cdcfg->cdtab = dmam_alloc_coherent(smmu->dev, l1size, &cdcfg->cdtab_dma,
1696                                            GFP_KERNEL);
1697         if (!cdcfg->cdtab) {
1698                 dev_warn(smmu->dev, "failed to allocate context descriptor\n");
1699                 ret = -ENOMEM;
1700                 goto err_free_l1;
1701         }
1702
1703         return 0;
1704
1705 err_free_l1:
1706         if (cdcfg->l1_desc) {
1707                 devm_kfree(smmu->dev, cdcfg->l1_desc);
1708                 cdcfg->l1_desc = NULL;
1709         }
1710         return ret;
1711 }
1712
1713 static void arm_smmu_free_cd_tables(struct arm_smmu_domain *smmu_domain)
1714 {
1715         int i;
1716         size_t size, l1size;
1717         struct arm_smmu_device *smmu = smmu_domain->smmu;
1718         struct arm_smmu_ctx_desc_cfg *cdcfg = &smmu_domain->s1_cfg.cdcfg;
1719
1720         if (cdcfg->l1_desc) {
1721                 size = CTXDESC_L2_ENTRIES * (CTXDESC_CD_DWORDS << 3);
1722
1723                 for (i = 0; i < cdcfg->num_l1_ents; i++) {
1724                         if (!cdcfg->l1_desc[i].l2ptr)
1725                                 continue;
1726
1727                         dmam_free_coherent(smmu->dev, size,
1728                                            cdcfg->l1_desc[i].l2ptr,
1729                                            cdcfg->l1_desc[i].l2ptr_dma);
1730                 }
1731                 devm_kfree(smmu->dev, cdcfg->l1_desc);
1732                 cdcfg->l1_desc = NULL;
1733
1734                 l1size = cdcfg->num_l1_ents * (CTXDESC_L1_DESC_DWORDS << 3);
1735         } else {
1736                 l1size = cdcfg->num_l1_ents * (CTXDESC_CD_DWORDS << 3);
1737         }
1738
1739         dmam_free_coherent(smmu->dev, l1size, cdcfg->cdtab, cdcfg->cdtab_dma);
1740         cdcfg->cdtab_dma = 0;
1741         cdcfg->cdtab = NULL;
1742 }
1743
1744 /* Stream table manipulation functions */
1745 static void
1746 arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc)
1747 {
1748         u64 val = 0;
1749
1750         val |= FIELD_PREP(STRTAB_L1_DESC_SPAN, desc->span);
1751         val |= desc->l2ptr_dma & STRTAB_L1_DESC_L2PTR_MASK;
1752
1753         /* See comment in arm_smmu_write_ctx_desc() */
1754         WRITE_ONCE(*dst, cpu_to_le64(val));
1755 }
1756
1757 static void arm_smmu_sync_ste_for_sid(struct arm_smmu_device *smmu, u32 sid)
1758 {
1759         struct arm_smmu_cmdq_ent cmd = {
1760                 .opcode = CMDQ_OP_CFGI_STE,
1761                 .cfgi   = {
1762                         .sid    = sid,
1763                         .leaf   = true,
1764                 },
1765         };
1766
1767         arm_smmu_cmdq_issue_cmd(smmu, &cmd);
1768         arm_smmu_cmdq_issue_sync(smmu);
1769 }
1770
1771 static void arm_smmu_write_strtab_ent(struct arm_smmu_master *master, u32 sid,
1772                                       __le64 *dst)
1773 {
1774         /*
1775          * This is hideously complicated, but we only really care about
1776          * three cases at the moment:
1777          *
1778          * 1. Invalid (all zero) -> bypass/fault (init)
1779          * 2. Bypass/fault -> translation/bypass (attach)
1780          * 3. Translation/bypass -> bypass/fault (detach)
1781          *
1782          * Given that we can't update the STE atomically and the SMMU
1783          * doesn't read the thing in a defined order, that leaves us
1784          * with the following maintenance requirements:
1785          *
1786          * 1. Update Config, return (init time STEs aren't live)
1787          * 2. Write everything apart from dword 0, sync, write dword 0, sync
1788          * 3. Update Config, sync
1789          */
1790         u64 val = le64_to_cpu(dst[0]);
1791         bool ste_live = false;
1792         struct arm_smmu_device *smmu = NULL;
1793         struct arm_smmu_s1_cfg *s1_cfg = NULL;
1794         struct arm_smmu_s2_cfg *s2_cfg = NULL;
1795         struct arm_smmu_domain *smmu_domain = NULL;
1796         struct arm_smmu_cmdq_ent prefetch_cmd = {
1797                 .opcode         = CMDQ_OP_PREFETCH_CFG,
1798                 .prefetch       = {
1799                         .sid    = sid,
1800                 },
1801         };
1802
1803         if (master) {
1804                 smmu_domain = master->domain;
1805                 smmu = master->smmu;
1806         }
1807
1808         if (smmu_domain) {
1809                 switch (smmu_domain->stage) {
1810                 case ARM_SMMU_DOMAIN_S1:
1811                         s1_cfg = &smmu_domain->s1_cfg;
1812                         break;
1813                 case ARM_SMMU_DOMAIN_S2:
1814                 case ARM_SMMU_DOMAIN_NESTED:
1815                         s2_cfg = &smmu_domain->s2_cfg;
1816                         break;
1817                 default:
1818                         break;
1819                 }
1820         }
1821
1822         if (val & STRTAB_STE_0_V) {
1823                 switch (FIELD_GET(STRTAB_STE_0_CFG, val)) {
1824                 case STRTAB_STE_0_CFG_BYPASS:
1825                         break;
1826                 case STRTAB_STE_0_CFG_S1_TRANS:
1827                 case STRTAB_STE_0_CFG_S2_TRANS:
1828                         ste_live = true;
1829                         break;
1830                 case STRTAB_STE_0_CFG_ABORT:
1831                         BUG_ON(!disable_bypass);
1832                         break;
1833                 default:
1834                         BUG(); /* STE corruption */
1835                 }
1836         }
1837
1838         /* Nuke the existing STE_0 value, as we're going to rewrite it */
1839         val = STRTAB_STE_0_V;
1840
1841         /* Bypass/fault */
1842         if (!smmu_domain || !(s1_cfg || s2_cfg)) {
1843                 if (!smmu_domain && disable_bypass)
1844                         val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_ABORT);
1845                 else
1846                         val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_BYPASS);
1847
1848                 dst[0] = cpu_to_le64(val);
1849                 dst[1] = cpu_to_le64(FIELD_PREP(STRTAB_STE_1_SHCFG,
1850                                                 STRTAB_STE_1_SHCFG_INCOMING));
1851                 dst[2] = 0; /* Nuke the VMID */
1852                 /*
1853                  * The SMMU can perform negative caching, so we must sync
1854                  * the STE regardless of whether the old value was live.
1855                  */
1856                 if (smmu)
1857                         arm_smmu_sync_ste_for_sid(smmu, sid);
1858                 return;
1859         }
1860
1861         if (s1_cfg) {
1862                 BUG_ON(ste_live);
1863                 dst[1] = cpu_to_le64(
1864                          FIELD_PREP(STRTAB_STE_1_S1DSS, STRTAB_STE_1_S1DSS_SSID0) |
1865                          FIELD_PREP(STRTAB_STE_1_S1CIR, STRTAB_STE_1_S1C_CACHE_WBRA) |
1866                          FIELD_PREP(STRTAB_STE_1_S1COR, STRTAB_STE_1_S1C_CACHE_WBRA) |
1867                          FIELD_PREP(STRTAB_STE_1_S1CSH, ARM_SMMU_SH_ISH) |
1868                          FIELD_PREP(STRTAB_STE_1_STRW, STRTAB_STE_1_STRW_NSEL1));
1869
1870                 if (smmu->features & ARM_SMMU_FEAT_STALLS &&
1871                    !(smmu->features & ARM_SMMU_FEAT_STALL_FORCE))
1872                         dst[1] |= cpu_to_le64(STRTAB_STE_1_S1STALLD);
1873
1874                 val |= (s1_cfg->cdcfg.cdtab_dma & STRTAB_STE_0_S1CTXPTR_MASK) |
1875                         FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S1_TRANS) |
1876                         FIELD_PREP(STRTAB_STE_0_S1CDMAX, s1_cfg->s1cdmax) |
1877                         FIELD_PREP(STRTAB_STE_0_S1FMT, s1_cfg->s1fmt);
1878         }
1879
1880         if (s2_cfg) {
1881                 BUG_ON(ste_live);
1882                 dst[2] = cpu_to_le64(
1883                          FIELD_PREP(STRTAB_STE_2_S2VMID, s2_cfg->vmid) |
1884                          FIELD_PREP(STRTAB_STE_2_VTCR, s2_cfg->vtcr) |
1885 #ifdef __BIG_ENDIAN
1886                          STRTAB_STE_2_S2ENDI |
1887 #endif
1888                          STRTAB_STE_2_S2PTW | STRTAB_STE_2_S2AA64 |
1889                          STRTAB_STE_2_S2R);
1890
1891                 dst[3] = cpu_to_le64(s2_cfg->vttbr & STRTAB_STE_3_S2TTB_MASK);
1892
1893                 val |= FIELD_PREP(STRTAB_STE_0_CFG, STRTAB_STE_0_CFG_S2_TRANS);
1894         }
1895
1896         if (master->ats_enabled)
1897                 dst[1] |= cpu_to_le64(FIELD_PREP(STRTAB_STE_1_EATS,
1898                                                  STRTAB_STE_1_EATS_TRANS));
1899
1900         arm_smmu_sync_ste_for_sid(smmu, sid);
1901         /* See comment in arm_smmu_write_ctx_desc() */
1902         WRITE_ONCE(dst[0], cpu_to_le64(val));
1903         arm_smmu_sync_ste_for_sid(smmu, sid);
1904
1905         /* It's likely that we'll want to use the new STE soon */
1906         if (!(smmu->options & ARM_SMMU_OPT_SKIP_PREFETCH))
1907                 arm_smmu_cmdq_issue_cmd(smmu, &prefetch_cmd);
1908 }
1909
1910 static void arm_smmu_init_bypass_stes(u64 *strtab, unsigned int nent)
1911 {
1912         unsigned int i;
1913
1914         for (i = 0; i < nent; ++i) {
1915                 arm_smmu_write_strtab_ent(NULL, -1, strtab);
1916                 strtab += STRTAB_STE_DWORDS;
1917         }
1918 }
1919
1920 static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
1921 {
1922         size_t size;
1923         void *strtab;
1924         struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
1925         struct arm_smmu_strtab_l1_desc *desc = &cfg->l1_desc[sid >> STRTAB_SPLIT];
1926
1927         if (desc->l2ptr)
1928                 return 0;
1929
1930         size = 1 << (STRTAB_SPLIT + ilog2(STRTAB_STE_DWORDS) + 3);
1931         strtab = &cfg->strtab[(sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS];
1932
1933         desc->span = STRTAB_SPLIT + 1;
1934         desc->l2ptr = dmam_alloc_coherent(smmu->dev, size, &desc->l2ptr_dma,
1935                                           GFP_KERNEL);
1936         if (!desc->l2ptr) {
1937                 dev_err(smmu->dev,
1938                         "failed to allocate l2 stream table for SID %u\n",
1939                         sid);
1940                 return -ENOMEM;
1941         }
1942
1943         arm_smmu_init_bypass_stes(desc->l2ptr, 1 << STRTAB_SPLIT);
1944         arm_smmu_write_strtab_l1_desc(strtab, desc);
1945         return 0;
1946 }
1947
1948 /* IRQ and event handlers */
1949 static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)
1950 {
1951         int i;
1952         struct arm_smmu_device *smmu = dev;
1953         struct arm_smmu_queue *q = &smmu->evtq.q;
1954         struct arm_smmu_ll_queue *llq = &q->llq;
1955         u64 evt[EVTQ_ENT_DWORDS];
1956
1957         do {
1958                 while (!queue_remove_raw(q, evt)) {
1959                         u8 id = FIELD_GET(EVTQ_0_ID, evt[0]);
1960
1961                         dev_info(smmu->dev, "event 0x%02x received:\n", id);
1962                         for (i = 0; i < ARRAY_SIZE(evt); ++i)
1963                                 dev_info(smmu->dev, "\t0x%016llx\n",
1964                                          (unsigned long long)evt[i]);
1965
1966                 }
1967
1968                 /*
1969                  * Not much we can do on overflow, so scream and pretend we're
1970                  * trying harder.
1971                  */
1972                 if (queue_sync_prod_in(q) == -EOVERFLOW)
1973                         dev_err(smmu->dev, "EVTQ overflow detected -- events lost\n");
1974         } while (!queue_empty(llq));
1975
1976         /* Sync our overflow flag, as we believe we're up to speed */
1977         llq->cons = Q_OVF(llq->prod) | Q_WRP(llq, llq->cons) |
1978                     Q_IDX(llq, llq->cons);
1979         return IRQ_HANDLED;
1980 }
1981
1982 static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt)
1983 {
1984         u32 sid, ssid;
1985         u16 grpid;
1986         bool ssv, last;
1987
1988         sid = FIELD_GET(PRIQ_0_SID, evt[0]);
1989         ssv = FIELD_GET(PRIQ_0_SSID_V, evt[0]);
1990         ssid = ssv ? FIELD_GET(PRIQ_0_SSID, evt[0]) : 0;
1991         last = FIELD_GET(PRIQ_0_PRG_LAST, evt[0]);
1992         grpid = FIELD_GET(PRIQ_1_PRG_IDX, evt[1]);
1993
1994         dev_info(smmu->dev, "unexpected PRI request received:\n");
1995         dev_info(smmu->dev,
1996                  "\tsid 0x%08x.0x%05x: [%u%s] %sprivileged %s%s%s access at iova 0x%016llx\n",
1997                  sid, ssid, grpid, last ? "L" : "",
1998                  evt[0] & PRIQ_0_PERM_PRIV ? "" : "un",
1999                  evt[0] & PRIQ_0_PERM_READ ? "R" : "",
2000                  evt[0] & PRIQ_0_PERM_WRITE ? "W" : "",
2001                  evt[0] & PRIQ_0_PERM_EXEC ? "X" : "",
2002                  evt[1] & PRIQ_1_ADDR_MASK);
2003
2004         if (last) {
2005                 struct arm_smmu_cmdq_ent cmd = {
2006                         .opcode                 = CMDQ_OP_PRI_RESP,
2007                         .substream_valid        = ssv,
2008                         .pri                    = {
2009                                 .sid    = sid,
2010                                 .ssid   = ssid,
2011                                 .grpid  = grpid,
2012                                 .resp   = PRI_RESP_DENY,
2013                         },
2014                 };
2015
2016                 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2017         }
2018 }
2019
2020 static irqreturn_t arm_smmu_priq_thread(int irq, void *dev)
2021 {
2022         struct arm_smmu_device *smmu = dev;
2023         struct arm_smmu_queue *q = &smmu->priq.q;
2024         struct arm_smmu_ll_queue *llq = &q->llq;
2025         u64 evt[PRIQ_ENT_DWORDS];
2026
2027         do {
2028                 while (!queue_remove_raw(q, evt))
2029                         arm_smmu_handle_ppr(smmu, evt);
2030
2031                 if (queue_sync_prod_in(q) == -EOVERFLOW)
2032                         dev_err(smmu->dev, "PRIQ overflow detected -- requests lost\n");
2033         } while (!queue_empty(llq));
2034
2035         /* Sync our overflow flag, as we believe we're up to speed */
2036         llq->cons = Q_OVF(llq->prod) | Q_WRP(llq, llq->cons) |
2037                       Q_IDX(llq, llq->cons);
2038         queue_sync_cons_out(q);
2039         return IRQ_HANDLED;
2040 }
2041
2042 static int arm_smmu_device_disable(struct arm_smmu_device *smmu);
2043
2044 static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)
2045 {
2046         u32 gerror, gerrorn, active;
2047         struct arm_smmu_device *smmu = dev;
2048
2049         gerror = readl_relaxed(smmu->base + ARM_SMMU_GERROR);
2050         gerrorn = readl_relaxed(smmu->base + ARM_SMMU_GERRORN);
2051
2052         active = gerror ^ gerrorn;
2053         if (!(active & GERROR_ERR_MASK))
2054                 return IRQ_NONE; /* No errors pending */
2055
2056         dev_warn(smmu->dev,
2057                  "unexpected global error reported (0x%08x), this could be serious\n",
2058                  active);
2059
2060         if (active & GERROR_SFM_ERR) {
2061                 dev_err(smmu->dev, "device has entered Service Failure Mode!\n");
2062                 arm_smmu_device_disable(smmu);
2063         }
2064
2065         if (active & GERROR_MSI_GERROR_ABT_ERR)
2066                 dev_warn(smmu->dev, "GERROR MSI write aborted\n");
2067
2068         if (active & GERROR_MSI_PRIQ_ABT_ERR)
2069                 dev_warn(smmu->dev, "PRIQ MSI write aborted\n");
2070
2071         if (active & GERROR_MSI_EVTQ_ABT_ERR)
2072                 dev_warn(smmu->dev, "EVTQ MSI write aborted\n");
2073
2074         if (active & GERROR_MSI_CMDQ_ABT_ERR)
2075                 dev_warn(smmu->dev, "CMDQ MSI write aborted\n");
2076
2077         if (active & GERROR_PRIQ_ABT_ERR)
2078                 dev_err(smmu->dev, "PRIQ write aborted -- events may have been lost\n");
2079
2080         if (active & GERROR_EVTQ_ABT_ERR)
2081                 dev_err(smmu->dev, "EVTQ write aborted -- events may have been lost\n");
2082
2083         if (active & GERROR_CMDQ_ERR)
2084                 arm_smmu_cmdq_skip_err(smmu);
2085
2086         writel(gerror, smmu->base + ARM_SMMU_GERRORN);
2087         return IRQ_HANDLED;
2088 }
2089
2090 static irqreturn_t arm_smmu_combined_irq_thread(int irq, void *dev)
2091 {
2092         struct arm_smmu_device *smmu = dev;
2093
2094         arm_smmu_evtq_thread(irq, dev);
2095         if (smmu->features & ARM_SMMU_FEAT_PRI)
2096                 arm_smmu_priq_thread(irq, dev);
2097
2098         return IRQ_HANDLED;
2099 }
2100
2101 static irqreturn_t arm_smmu_combined_irq_handler(int irq, void *dev)
2102 {
2103         arm_smmu_gerror_handler(irq, dev);
2104         return IRQ_WAKE_THREAD;
2105 }
2106
2107 static void
2108 arm_smmu_atc_inv_to_cmd(int ssid, unsigned long iova, size_t size,
2109                         struct arm_smmu_cmdq_ent *cmd)
2110 {
2111         size_t log2_span;
2112         size_t span_mask;
2113         /* ATC invalidates are always on 4096-bytes pages */
2114         size_t inval_grain_shift = 12;
2115         unsigned long page_start, page_end;
2116
2117         *cmd = (struct arm_smmu_cmdq_ent) {
2118                 .opcode                 = CMDQ_OP_ATC_INV,
2119                 .substream_valid        = !!ssid,
2120                 .atc.ssid               = ssid,
2121         };
2122
2123         if (!size) {
2124                 cmd->atc.size = ATC_INV_SIZE_ALL;
2125                 return;
2126         }
2127
2128         page_start      = iova >> inval_grain_shift;
2129         page_end        = (iova + size - 1) >> inval_grain_shift;
2130
2131         /*
2132          * In an ATS Invalidate Request, the address must be aligned on the
2133          * range size, which must be a power of two number of page sizes. We
2134          * thus have to choose between grossly over-invalidating the region, or
2135          * splitting the invalidation into multiple commands. For simplicity
2136          * we'll go with the first solution, but should refine it in the future
2137          * if multiple commands are shown to be more efficient.
2138          *
2139          * Find the smallest power of two that covers the range. The most
2140          * significant differing bit between the start and end addresses,
2141          * fls(start ^ end), indicates the required span. For example:
2142          *
2143          * We want to invalidate pages [8; 11]. This is already the ideal range:
2144          *              x = 0b1000 ^ 0b1011 = 0b11
2145          *              span = 1 << fls(x) = 4
2146          *
2147          * To invalidate pages [7; 10], we need to invalidate [0; 15]:
2148          *              x = 0b0111 ^ 0b1010 = 0b1101
2149          *              span = 1 << fls(x) = 16
2150          */
2151         log2_span       = fls_long(page_start ^ page_end);
2152         span_mask       = (1ULL << log2_span) - 1;
2153
2154         page_start      &= ~span_mask;
2155
2156         cmd->atc.addr   = page_start << inval_grain_shift;
2157         cmd->atc.size   = log2_span;
2158 }
2159
2160 static int arm_smmu_atc_inv_master(struct arm_smmu_master *master,
2161                                    struct arm_smmu_cmdq_ent *cmd)
2162 {
2163         int i;
2164
2165         if (!master->ats_enabled)
2166                 return 0;
2167
2168         for (i = 0; i < master->num_sids; i++) {
2169                 cmd->atc.sid = master->sids[i];
2170                 arm_smmu_cmdq_issue_cmd(master->smmu, cmd);
2171         }
2172
2173         return arm_smmu_cmdq_issue_sync(master->smmu);
2174 }
2175
2176 static int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain,
2177                                    int ssid, unsigned long iova, size_t size)
2178 {
2179         int ret = 0;
2180         unsigned long flags;
2181         struct arm_smmu_cmdq_ent cmd;
2182         struct arm_smmu_master *master;
2183
2184         if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_ATS))
2185                 return 0;
2186
2187         /*
2188          * Ensure that we've completed prior invalidation of the main TLBs
2189          * before we read 'nr_ats_masters' in case of a concurrent call to
2190          * arm_smmu_enable_ats():
2191          *
2192          *      // unmap()                      // arm_smmu_enable_ats()
2193          *      TLBI+SYNC                       atomic_inc(&nr_ats_masters);
2194          *      smp_mb();                       [...]
2195          *      atomic_read(&nr_ats_masters);   pci_enable_ats() // writel()
2196          *
2197          * Ensures that we always see the incremented 'nr_ats_masters' count if
2198          * ATS was enabled at the PCI device before completion of the TLBI.
2199          */
2200         smp_mb();
2201         if (!atomic_read(&smmu_domain->nr_ats_masters))
2202                 return 0;
2203
2204         arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd);
2205
2206         spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2207         list_for_each_entry(master, &smmu_domain->devices, domain_head)
2208                 ret |= arm_smmu_atc_inv_master(master, &cmd);
2209         spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2210
2211         return ret ? -ETIMEDOUT : 0;
2212 }
2213
2214 /* IO_PGTABLE API */
2215 static void arm_smmu_tlb_inv_context(void *cookie)
2216 {
2217         struct arm_smmu_domain *smmu_domain = cookie;
2218         struct arm_smmu_device *smmu = smmu_domain->smmu;
2219         struct arm_smmu_cmdq_ent cmd;
2220
2221         if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
2222                 cmd.opcode      = CMDQ_OP_TLBI_NH_ASID;
2223                 cmd.tlbi.asid   = smmu_domain->s1_cfg.cd.asid;
2224                 cmd.tlbi.vmid   = 0;
2225         } else {
2226                 cmd.opcode      = CMDQ_OP_TLBI_S12_VMALL;
2227                 cmd.tlbi.vmid   = smmu_domain->s2_cfg.vmid;
2228         }
2229
2230         /*
2231          * NOTE: when io-pgtable is in non-strict mode, we may get here with
2232          * PTEs previously cleared by unmaps on the current CPU not yet visible
2233          * to the SMMU. We are relying on the dma_wmb() implicit during cmd
2234          * insertion to guarantee those are observed before the TLBI. Do be
2235          * careful, 007.
2236          */
2237         arm_smmu_cmdq_issue_cmd(smmu, &cmd);
2238         arm_smmu_cmdq_issue_sync(smmu);
2239         arm_smmu_atc_inv_domain(smmu_domain, 0, 0, 0);
2240 }
2241
2242 static void arm_smmu_tlb_inv_range(unsigned long iova, size_t size,
2243                                    size_t granule, bool leaf,
2244                                    struct arm_smmu_domain *smmu_domain)
2245 {
2246         struct arm_smmu_device *smmu = smmu_domain->smmu;
2247         unsigned long start = iova, end = iova + size;
2248         struct arm_smmu_cmdq_batch cmds = {};
2249         struct arm_smmu_cmdq_ent cmd = {
2250                 .tlbi = {
2251                         .leaf   = leaf,
2252                 },
2253         };
2254
2255         if (!size)
2256                 return;
2257
2258         if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
2259                 cmd.opcode      = CMDQ_OP_TLBI_NH_VA;
2260                 cmd.tlbi.asid   = smmu_domain->s1_cfg.cd.asid;
2261         } else {
2262                 cmd.opcode      = CMDQ_OP_TLBI_S2_IPA;
2263                 cmd.tlbi.vmid   = smmu_domain->s2_cfg.vmid;
2264         }
2265
2266         while (iova < end) {
2267                 cmd.tlbi.addr = iova;
2268                 arm_smmu_cmdq_batch_add(smmu, &cmds, &cmd);
2269                 iova += granule;
2270         }
2271         arm_smmu_cmdq_batch_submit(smmu, &cmds);
2272
2273         /*
2274          * Unfortunately, this can't be leaf-only since we may have
2275          * zapped an entire table.
2276          */
2277         arm_smmu_atc_inv_domain(smmu_domain, 0, start, size);
2278 }
2279
2280 static void arm_smmu_tlb_inv_page_nosync(struct iommu_iotlb_gather *gather,
2281                                          unsigned long iova, size_t granule,
2282                                          void *cookie)
2283 {
2284         struct arm_smmu_domain *smmu_domain = cookie;
2285         struct iommu_domain *domain = &smmu_domain->domain;
2286
2287         iommu_iotlb_gather_add_page(domain, gather, iova, granule);
2288 }
2289
2290 static void arm_smmu_tlb_inv_walk(unsigned long iova, size_t size,
2291                                   size_t granule, void *cookie)
2292 {
2293         arm_smmu_tlb_inv_range(iova, size, granule, false, cookie);
2294 }
2295
2296 static void arm_smmu_tlb_inv_leaf(unsigned long iova, size_t size,
2297                                   size_t granule, void *cookie)
2298 {
2299         arm_smmu_tlb_inv_range(iova, size, granule, true, cookie);
2300 }
2301
2302 static const struct iommu_flush_ops arm_smmu_flush_ops = {
2303         .tlb_flush_all  = arm_smmu_tlb_inv_context,
2304         .tlb_flush_walk = arm_smmu_tlb_inv_walk,
2305         .tlb_flush_leaf = arm_smmu_tlb_inv_leaf,
2306         .tlb_add_page   = arm_smmu_tlb_inv_page_nosync,
2307 };
2308
2309 /* IOMMU API */
2310 static bool arm_smmu_capable(enum iommu_cap cap)
2311 {
2312         switch (cap) {
2313         case IOMMU_CAP_CACHE_COHERENCY:
2314                 return true;
2315         case IOMMU_CAP_NOEXEC:
2316                 return true;
2317         default:
2318                 return false;
2319         }
2320 }
2321
2322 static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
2323 {
2324         struct arm_smmu_domain *smmu_domain;
2325
2326         if (type != IOMMU_DOMAIN_UNMANAGED &&
2327             type != IOMMU_DOMAIN_DMA &&
2328             type != IOMMU_DOMAIN_IDENTITY)
2329                 return NULL;
2330
2331         /*
2332          * Allocate the domain and initialise some of its data structures.
2333          * We can't really do anything meaningful until we've added a
2334          * master.
2335          */
2336         smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
2337         if (!smmu_domain)
2338                 return NULL;
2339
2340         if (type == IOMMU_DOMAIN_DMA &&
2341             iommu_get_dma_cookie(&smmu_domain->domain)) {
2342                 kfree(smmu_domain);
2343                 return NULL;
2344         }
2345
2346         mutex_init(&smmu_domain->init_mutex);
2347         INIT_LIST_HEAD(&smmu_domain->devices);
2348         spin_lock_init(&smmu_domain->devices_lock);
2349
2350         return &smmu_domain->domain;
2351 }
2352
2353 static int arm_smmu_bitmap_alloc(unsigned long *map, int span)
2354 {
2355         int idx, size = 1 << span;
2356
2357         do {
2358                 idx = find_first_zero_bit(map, size);
2359                 if (idx == size)
2360                         return -ENOSPC;
2361         } while (test_and_set_bit(idx, map));
2362
2363         return idx;
2364 }
2365
2366 static void arm_smmu_bitmap_free(unsigned long *map, int idx)
2367 {
2368         clear_bit(idx, map);
2369 }
2370
2371 static void arm_smmu_domain_free(struct iommu_domain *domain)
2372 {
2373         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2374         struct arm_smmu_device *smmu = smmu_domain->smmu;
2375
2376         iommu_put_dma_cookie(domain);
2377         free_io_pgtable_ops(smmu_domain->pgtbl_ops);
2378
2379         /* Free the CD and ASID, if we allocated them */
2380         if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
2381                 struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
2382
2383                 if (cfg->cdcfg.cdtab) {
2384                         arm_smmu_free_cd_tables(smmu_domain);
2385                         arm_smmu_bitmap_free(smmu->asid_map, cfg->cd.asid);
2386                 }
2387         } else {
2388                 struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
2389                 if (cfg->vmid)
2390                         arm_smmu_bitmap_free(smmu->vmid_map, cfg->vmid);
2391         }
2392
2393         kfree(smmu_domain);
2394 }
2395
2396 static int arm_smmu_domain_finalise_s1(struct arm_smmu_domain *smmu_domain,
2397                                        struct arm_smmu_master *master,
2398                                        struct io_pgtable_cfg *pgtbl_cfg)
2399 {
2400         int ret;
2401         int asid;
2402         struct arm_smmu_device *smmu = smmu_domain->smmu;
2403         struct arm_smmu_s1_cfg *cfg = &smmu_domain->s1_cfg;
2404         typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr;
2405
2406         asid = arm_smmu_bitmap_alloc(smmu->asid_map, smmu->asid_bits);
2407         if (asid < 0)
2408                 return asid;
2409
2410         cfg->s1cdmax = master->ssid_bits;
2411
2412         ret = arm_smmu_alloc_cd_tables(smmu_domain);
2413         if (ret)
2414                 goto out_free_asid;
2415
2416         cfg->cd.asid    = (u16)asid;
2417         cfg->cd.ttbr    = pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
2418         cfg->cd.tcr     = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) |
2419                           FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) |
2420                           FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) |
2421                           FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) |
2422                           FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) |
2423                           FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) |
2424                           CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64;
2425         cfg->cd.mair    = pgtbl_cfg->arm_lpae_s1_cfg.mair;
2426
2427         /*
2428          * Note that this will end up calling arm_smmu_sync_cd() before
2429          * the master has been added to the devices list for this domain.
2430          * This isn't an issue because the STE hasn't been installed yet.
2431          */
2432         ret = arm_smmu_write_ctx_desc(smmu_domain, 0, &cfg->cd);
2433         if (ret)
2434                 goto out_free_cd_tables;
2435
2436         return 0;
2437
2438 out_free_cd_tables:
2439         arm_smmu_free_cd_tables(smmu_domain);
2440 out_free_asid:
2441         arm_smmu_bitmap_free(smmu->asid_map, asid);
2442         return ret;
2443 }
2444
2445 static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,
2446                                        struct arm_smmu_master *master,
2447                                        struct io_pgtable_cfg *pgtbl_cfg)
2448 {
2449         int vmid;
2450         struct arm_smmu_device *smmu = smmu_domain->smmu;
2451         struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;
2452         typeof(&pgtbl_cfg->arm_lpae_s2_cfg.vtcr) vtcr;
2453
2454         vmid = arm_smmu_bitmap_alloc(smmu->vmid_map, smmu->vmid_bits);
2455         if (vmid < 0)
2456                 return vmid;
2457
2458         vtcr = &pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
2459         cfg->vmid       = (u16)vmid;
2460         cfg->vttbr      = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
2461         cfg->vtcr       = FIELD_PREP(STRTAB_STE_2_VTCR_S2T0SZ, vtcr->tsz) |
2462                           FIELD_PREP(STRTAB_STE_2_VTCR_S2SL0, vtcr->sl) |
2463                           FIELD_PREP(STRTAB_STE_2_VTCR_S2IR0, vtcr->irgn) |
2464                           FIELD_PREP(STRTAB_STE_2_VTCR_S2OR0, vtcr->orgn) |
2465                           FIELD_PREP(STRTAB_STE_2_VTCR_S2SH0, vtcr->sh) |
2466                           FIELD_PREP(STRTAB_STE_2_VTCR_S2TG, vtcr->tg) |
2467                           FIELD_PREP(STRTAB_STE_2_VTCR_S2PS, vtcr->ps);
2468         return 0;
2469 }
2470
2471 static int arm_smmu_domain_finalise(struct iommu_domain *domain,
2472                                     struct arm_smmu_master *master)
2473 {
2474         int ret;
2475         unsigned long ias, oas;
2476         enum io_pgtable_fmt fmt;
2477         struct io_pgtable_cfg pgtbl_cfg;
2478         struct io_pgtable_ops *pgtbl_ops;
2479         int (*finalise_stage_fn)(struct arm_smmu_domain *,
2480                                  struct arm_smmu_master *,
2481                                  struct io_pgtable_cfg *);
2482         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2483         struct arm_smmu_device *smmu = smmu_domain->smmu;
2484
2485         if (domain->type == IOMMU_DOMAIN_IDENTITY) {
2486                 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
2487                 return 0;
2488         }
2489
2490         /* Restrict the stage to what we can actually support */
2491         if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
2492                 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
2493         if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
2494                 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
2495
2496         switch (smmu_domain->stage) {
2497         case ARM_SMMU_DOMAIN_S1:
2498                 ias = (smmu->features & ARM_SMMU_FEAT_VAX) ? 52 : 48;
2499                 ias = min_t(unsigned long, ias, VA_BITS);
2500                 oas = smmu->ias;
2501                 fmt = ARM_64_LPAE_S1;
2502                 finalise_stage_fn = arm_smmu_domain_finalise_s1;
2503                 break;
2504         case ARM_SMMU_DOMAIN_NESTED:
2505         case ARM_SMMU_DOMAIN_S2:
2506                 ias = smmu->ias;
2507                 oas = smmu->oas;
2508                 fmt = ARM_64_LPAE_S2;
2509                 finalise_stage_fn = arm_smmu_domain_finalise_s2;
2510                 break;
2511         default:
2512                 return -EINVAL;
2513         }
2514
2515         pgtbl_cfg = (struct io_pgtable_cfg) {
2516                 .pgsize_bitmap  = smmu->pgsize_bitmap,
2517                 .ias            = ias,
2518                 .oas            = oas,
2519                 .coherent_walk  = smmu->features & ARM_SMMU_FEAT_COHERENCY,
2520                 .tlb            = &arm_smmu_flush_ops,
2521                 .iommu_dev      = smmu->dev,
2522         };
2523
2524         if (smmu_domain->non_strict)
2525                 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT;
2526
2527         pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
2528         if (!pgtbl_ops)
2529                 return -ENOMEM;
2530
2531         domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
2532         domain->geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1;
2533         domain->geometry.force_aperture = true;
2534
2535         ret = finalise_stage_fn(smmu_domain, master, &pgtbl_cfg);
2536         if (ret < 0) {
2537                 free_io_pgtable_ops(pgtbl_ops);
2538                 return ret;
2539         }
2540
2541         smmu_domain->pgtbl_ops = pgtbl_ops;
2542         return 0;
2543 }
2544
2545 static __le64 *arm_smmu_get_step_for_sid(struct arm_smmu_device *smmu, u32 sid)
2546 {
2547         __le64 *step;
2548         struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
2549
2550         if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
2551                 struct arm_smmu_strtab_l1_desc *l1_desc;
2552                 int idx;
2553
2554                 /* Two-level walk */
2555                 idx = (sid >> STRTAB_SPLIT) * STRTAB_L1_DESC_DWORDS;
2556                 l1_desc = &cfg->l1_desc[idx];
2557                 idx = (sid & ((1 << STRTAB_SPLIT) - 1)) * STRTAB_STE_DWORDS;
2558                 step = &l1_desc->l2ptr[idx];
2559         } else {
2560                 /* Simple linear lookup */
2561                 step = &cfg->strtab[sid * STRTAB_STE_DWORDS];
2562         }
2563
2564         return step;
2565 }
2566
2567 static void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master)
2568 {
2569         int i, j;
2570         struct arm_smmu_device *smmu = master->smmu;
2571
2572         for (i = 0; i < master->num_sids; ++i) {
2573                 u32 sid = master->sids[i];
2574                 __le64 *step = arm_smmu_get_step_for_sid(smmu, sid);
2575
2576                 /* Bridged PCI devices may end up with duplicated IDs */
2577                 for (j = 0; j < i; j++)
2578                         if (master->sids[j] == sid)
2579                                 break;
2580                 if (j < i)
2581                         continue;
2582
2583                 arm_smmu_write_strtab_ent(master, sid, step);
2584         }
2585 }
2586
2587 #ifdef CONFIG_PCI_ATS
2588 static bool arm_smmu_ats_supported(struct arm_smmu_master *master)
2589 {
2590         struct pci_dev *pdev;
2591         struct arm_smmu_device *smmu = master->smmu;
2592         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(master->dev);
2593
2594         if (!(smmu->features & ARM_SMMU_FEAT_ATS) || !dev_is_pci(master->dev) ||
2595             !(fwspec->flags & IOMMU_FWSPEC_PCI_RC_ATS) || pci_ats_disabled())
2596                 return false;
2597
2598         pdev = to_pci_dev(master->dev);
2599         return !pdev->untrusted && pdev->ats_cap;
2600 }
2601 #else
2602 static bool arm_smmu_ats_supported(struct arm_smmu_master *master)
2603 {
2604         return false;
2605 }
2606 #endif
2607
2608 static void arm_smmu_enable_ats(struct arm_smmu_master *master)
2609 {
2610         size_t stu;
2611         struct pci_dev *pdev;
2612         struct arm_smmu_device *smmu = master->smmu;
2613         struct arm_smmu_domain *smmu_domain = master->domain;
2614
2615         /* Don't enable ATS at the endpoint if it's not enabled in the STE */
2616         if (!master->ats_enabled)
2617                 return;
2618
2619         /* Smallest Translation Unit: log2 of the smallest supported granule */
2620         stu = __ffs(smmu->pgsize_bitmap);
2621         pdev = to_pci_dev(master->dev);
2622
2623         atomic_inc(&smmu_domain->nr_ats_masters);
2624         arm_smmu_atc_inv_domain(smmu_domain, 0, 0, 0);
2625         if (pci_enable_ats(pdev, stu))
2626                 dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu);
2627 }
2628
2629 static void arm_smmu_disable_ats(struct arm_smmu_master *master)
2630 {
2631         struct arm_smmu_cmdq_ent cmd;
2632         struct arm_smmu_domain *smmu_domain = master->domain;
2633
2634         if (!master->ats_enabled)
2635                 return;
2636
2637         pci_disable_ats(to_pci_dev(master->dev));
2638         /*
2639          * Ensure ATS is disabled at the endpoint before we issue the
2640          * ATC invalidation via the SMMU.
2641          */
2642         wmb();
2643         arm_smmu_atc_inv_to_cmd(0, 0, 0, &cmd);
2644         arm_smmu_atc_inv_master(master, &cmd);
2645         atomic_dec(&smmu_domain->nr_ats_masters);
2646 }
2647
2648 static int arm_smmu_enable_pasid(struct arm_smmu_master *master)
2649 {
2650         int ret;
2651         int features;
2652         int num_pasids;
2653         struct pci_dev *pdev;
2654
2655         if (!dev_is_pci(master->dev))
2656                 return -ENODEV;
2657
2658         pdev = to_pci_dev(master->dev);
2659
2660         features = pci_pasid_features(pdev);
2661         if (features < 0)
2662                 return features;
2663
2664         num_pasids = pci_max_pasids(pdev);
2665         if (num_pasids <= 0)
2666                 return num_pasids;
2667
2668         ret = pci_enable_pasid(pdev, features);
2669         if (ret) {
2670                 dev_err(&pdev->dev, "Failed to enable PASID\n");
2671                 return ret;
2672         }
2673
2674         master->ssid_bits = min_t(u8, ilog2(num_pasids),
2675                                   master->smmu->ssid_bits);
2676         return 0;
2677 }
2678
2679 static void arm_smmu_disable_pasid(struct arm_smmu_master *master)
2680 {
2681         struct pci_dev *pdev;
2682
2683         if (!dev_is_pci(master->dev))
2684                 return;
2685
2686         pdev = to_pci_dev(master->dev);
2687
2688         if (!pdev->pasid_enabled)
2689                 return;
2690
2691         master->ssid_bits = 0;
2692         pci_disable_pasid(pdev);
2693 }
2694
2695 static void arm_smmu_detach_dev(struct arm_smmu_master *master)
2696 {
2697         unsigned long flags;
2698         struct arm_smmu_domain *smmu_domain = master->domain;
2699
2700         if (!smmu_domain)
2701                 return;
2702
2703         arm_smmu_disable_ats(master);
2704
2705         spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2706         list_del(&master->domain_head);
2707         spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2708
2709         master->domain = NULL;
2710         master->ats_enabled = false;
2711         arm_smmu_install_ste_for_dev(master);
2712 }
2713
2714 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
2715 {
2716         int ret = 0;
2717         unsigned long flags;
2718         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2719         struct arm_smmu_device *smmu;
2720         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2721         struct arm_smmu_master *master;
2722
2723         if (!fwspec)
2724                 return -ENOENT;
2725
2726         master = fwspec->iommu_priv;
2727         smmu = master->smmu;
2728
2729         arm_smmu_detach_dev(master);
2730
2731         mutex_lock(&smmu_domain->init_mutex);
2732
2733         if (!smmu_domain->smmu) {
2734                 smmu_domain->smmu = smmu;
2735                 ret = arm_smmu_domain_finalise(domain, master);
2736                 if (ret) {
2737                         smmu_domain->smmu = NULL;
2738                         goto out_unlock;
2739                 }
2740         } else if (smmu_domain->smmu != smmu) {
2741                 dev_err(dev,
2742                         "cannot attach to SMMU %s (upstream of %s)\n",
2743                         dev_name(smmu_domain->smmu->dev),
2744                         dev_name(smmu->dev));
2745                 ret = -ENXIO;
2746                 goto out_unlock;
2747         } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 &&
2748                    master->ssid_bits != smmu_domain->s1_cfg.s1cdmax) {
2749                 dev_err(dev,
2750                         "cannot attach to incompatible domain (%u SSID bits != %u)\n",
2751                         smmu_domain->s1_cfg.s1cdmax, master->ssid_bits);
2752                 ret = -EINVAL;
2753                 goto out_unlock;
2754         }
2755
2756         master->domain = smmu_domain;
2757
2758         if (smmu_domain->stage != ARM_SMMU_DOMAIN_BYPASS)
2759                 master->ats_enabled = arm_smmu_ats_supported(master);
2760
2761         arm_smmu_install_ste_for_dev(master);
2762
2763         spin_lock_irqsave(&smmu_domain->devices_lock, flags);
2764         list_add(&master->domain_head, &smmu_domain->devices);
2765         spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
2766
2767         arm_smmu_enable_ats(master);
2768
2769 out_unlock:
2770         mutex_unlock(&smmu_domain->init_mutex);
2771         return ret;
2772 }
2773
2774 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
2775                         phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2776 {
2777         struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
2778
2779         if (!ops)
2780                 return -ENODEV;
2781
2782         return ops->map(ops, iova, paddr, size, prot);
2783 }
2784
2785 static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
2786                              size_t size, struct iommu_iotlb_gather *gather)
2787 {
2788         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2789         struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
2790
2791         if (!ops)
2792                 return 0;
2793
2794         return ops->unmap(ops, iova, size, gather);
2795 }
2796
2797 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
2798 {
2799         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2800
2801         if (smmu_domain->smmu)
2802                 arm_smmu_tlb_inv_context(smmu_domain);
2803 }
2804
2805 static void arm_smmu_iotlb_sync(struct iommu_domain *domain,
2806                                 struct iommu_iotlb_gather *gather)
2807 {
2808         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2809
2810         arm_smmu_tlb_inv_range(gather->start, gather->end - gather->start,
2811                                gather->pgsize, true, smmu_domain);
2812 }
2813
2814 static phys_addr_t
2815 arm_smmu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2816 {
2817         struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
2818
2819         if (domain->type == IOMMU_DOMAIN_IDENTITY)
2820                 return iova;
2821
2822         if (!ops)
2823                 return 0;
2824
2825         return ops->iova_to_phys(ops, iova);
2826 }
2827
2828 static struct platform_driver arm_smmu_driver;
2829
2830 static
2831 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
2832 {
2833         struct device *dev = driver_find_device_by_fwnode(&arm_smmu_driver.driver,
2834                                                           fwnode);
2835         put_device(dev);
2836         return dev ? dev_get_drvdata(dev) : NULL;
2837 }
2838
2839 static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
2840 {
2841         unsigned long limit = smmu->strtab_cfg.num_l1_ents;
2842
2843         if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
2844                 limit *= 1UL << STRTAB_SPLIT;
2845
2846         return sid < limit;
2847 }
2848
2849 static struct iommu_ops arm_smmu_ops;
2850
2851 static int arm_smmu_add_device(struct device *dev)
2852 {
2853         int i, ret;
2854         struct arm_smmu_device *smmu;
2855         struct arm_smmu_master *master;
2856         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2857         struct iommu_group *group;
2858
2859         if (!fwspec || fwspec->ops != &arm_smmu_ops)
2860                 return -ENODEV;
2861
2862         if (WARN_ON_ONCE(fwspec->iommu_priv))
2863                 return -EBUSY;
2864
2865         smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
2866         if (!smmu)
2867                 return -ENODEV;
2868
2869         master = kzalloc(sizeof(*master), GFP_KERNEL);
2870         if (!master)
2871                 return -ENOMEM;
2872
2873         master->dev = dev;
2874         master->smmu = smmu;
2875         master->sids = fwspec->ids;
2876         master->num_sids = fwspec->num_ids;
2877         fwspec->iommu_priv = master;
2878
2879         /* Check the SIDs are in range of the SMMU and our stream table */
2880         for (i = 0; i < master->num_sids; i++) {
2881                 u32 sid = master->sids[i];
2882
2883                 if (!arm_smmu_sid_in_range(smmu, sid)) {
2884                         ret = -ERANGE;
2885                         goto err_free_master;
2886                 }
2887
2888                 /* Ensure l2 strtab is initialised */
2889                 if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB) {
2890                         ret = arm_smmu_init_l2_strtab(smmu, sid);
2891                         if (ret)
2892                                 goto err_free_master;
2893                 }
2894         }
2895
2896         master->ssid_bits = min(smmu->ssid_bits, fwspec->num_pasid_bits);
2897
2898         /*
2899          * Note that PASID must be enabled before, and disabled after ATS:
2900          * PCI Express Base 4.0r1.0 - 10.5.1.3 ATS Control Register
2901          *
2902          *   Behavior is undefined if this bit is Set and the value of the PASID
2903          *   Enable, Execute Requested Enable, or Privileged Mode Requested bits
2904          *   are changed.
2905          */
2906         arm_smmu_enable_pasid(master);
2907
2908         if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB))
2909                 master->ssid_bits = min_t(u8, master->ssid_bits,
2910                                           CTXDESC_LINEAR_CDMAX);
2911
2912         ret = iommu_device_link(&smmu->iommu, dev);
2913         if (ret)
2914                 goto err_disable_pasid;
2915
2916         group = iommu_group_get_for_dev(dev);
2917         if (IS_ERR(group)) {
2918                 ret = PTR_ERR(group);
2919                 goto err_unlink;
2920         }
2921
2922         iommu_group_put(group);
2923         return 0;
2924
2925 err_unlink:
2926         iommu_device_unlink(&smmu->iommu, dev);
2927 err_disable_pasid:
2928         arm_smmu_disable_pasid(master);
2929 err_free_master:
2930         kfree(master);
2931         fwspec->iommu_priv = NULL;
2932         return ret;
2933 }
2934
2935 static void arm_smmu_remove_device(struct device *dev)
2936 {
2937         struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2938         struct arm_smmu_master *master;
2939         struct arm_smmu_device *smmu;
2940
2941         if (!fwspec || fwspec->ops != &arm_smmu_ops)
2942                 return;
2943
2944         master = fwspec->iommu_priv;
2945         smmu = master->smmu;
2946         arm_smmu_detach_dev(master);
2947         iommu_group_remove_device(dev);
2948         iommu_device_unlink(&smmu->iommu, dev);
2949         arm_smmu_disable_pasid(master);
2950         kfree(master);
2951         iommu_fwspec_free(dev);
2952 }
2953
2954 static struct iommu_group *arm_smmu_device_group(struct device *dev)
2955 {
2956         struct iommu_group *group;
2957
2958         /*
2959          * We don't support devices sharing stream IDs other than PCI RID
2960          * aliases, since the necessary ID-to-device lookup becomes rather
2961          * impractical given a potential sparse 32-bit stream ID space.
2962          */
2963         if (dev_is_pci(dev))
2964                 group = pci_device_group(dev);
2965         else
2966                 group = generic_device_group(dev);
2967
2968         return group;
2969 }
2970
2971 static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
2972                                     enum iommu_attr attr, void *data)
2973 {
2974         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
2975
2976         switch (domain->type) {
2977         case IOMMU_DOMAIN_UNMANAGED:
2978                 switch (attr) {
2979                 case DOMAIN_ATTR_NESTING:
2980                         *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
2981                         return 0;
2982                 default:
2983                         return -ENODEV;
2984                 }
2985                 break;
2986         case IOMMU_DOMAIN_DMA:
2987                 switch (attr) {
2988                 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
2989                         *(int *)data = smmu_domain->non_strict;
2990                         return 0;
2991                 default:
2992                         return -ENODEV;
2993                 }
2994                 break;
2995         default:
2996                 return -EINVAL;
2997         }
2998 }
2999
3000 static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
3001                                     enum iommu_attr attr, void *data)
3002 {
3003         int ret = 0;
3004         struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
3005
3006         mutex_lock(&smmu_domain->init_mutex);
3007
3008         switch (domain->type) {
3009         case IOMMU_DOMAIN_UNMANAGED:
3010                 switch (attr) {
3011                 case DOMAIN_ATTR_NESTING:
3012                         if (smmu_domain->smmu) {
3013                                 ret = -EPERM;
3014                                 goto out_unlock;
3015                         }
3016
3017                         if (*(int *)data)
3018                                 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
3019                         else
3020                                 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
3021                         break;
3022                 default:
3023                         ret = -ENODEV;
3024                 }
3025                 break;
3026         case IOMMU_DOMAIN_DMA:
3027                 switch(attr) {
3028                 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
3029                         smmu_domain->non_strict = *(int *)data;
3030                         break;
3031                 default:
3032                         ret = -ENODEV;
3033                 }
3034                 break;
3035         default:
3036                 ret = -EINVAL;
3037         }
3038
3039 out_unlock:
3040         mutex_unlock(&smmu_domain->init_mutex);
3041         return ret;
3042 }
3043
3044 static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
3045 {
3046         return iommu_fwspec_add_ids(dev, args->args, 1);
3047 }
3048
3049 static void arm_smmu_get_resv_regions(struct device *dev,
3050                                       struct list_head *head)
3051 {
3052         struct iommu_resv_region *region;
3053         int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
3054
3055         region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
3056                                          prot, IOMMU_RESV_SW_MSI);
3057         if (!region)
3058                 return;
3059
3060         list_add_tail(&region->list, head);
3061
3062         iommu_dma_get_resv_regions(dev, head);
3063 }
3064
3065 static struct iommu_ops arm_smmu_ops = {
3066         .capable                = arm_smmu_capable,
3067         .domain_alloc           = arm_smmu_domain_alloc,
3068         .domain_free            = arm_smmu_domain_free,
3069         .attach_dev             = arm_smmu_attach_dev,
3070         .map                    = arm_smmu_map,
3071         .unmap                  = arm_smmu_unmap,
3072         .flush_iotlb_all        = arm_smmu_flush_iotlb_all,
3073         .iotlb_sync             = arm_smmu_iotlb_sync,
3074         .iova_to_phys           = arm_smmu_iova_to_phys,
3075         .add_device             = arm_smmu_add_device,
3076         .remove_device          = arm_smmu_remove_device,
3077         .device_group           = arm_smmu_device_group,
3078         .domain_get_attr        = arm_smmu_domain_get_attr,
3079         .domain_set_attr        = arm_smmu_domain_set_attr,
3080         .of_xlate               = arm_smmu_of_xlate,
3081         .get_resv_regions       = arm_smmu_get_resv_regions,
3082         .put_resv_regions       = generic_iommu_put_resv_regions,
3083         .pgsize_bitmap          = -1UL, /* Restricted during device attach */
3084 };
3085
3086 /* Probing and initialisation functions */
3087 static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
3088                                    struct arm_smmu_queue *q,
3089                                    unsigned long prod_off,
3090                                    unsigned long cons_off,
3091                                    size_t dwords, const char *name)
3092 {
3093         size_t qsz;
3094
3095         do {
3096                 qsz = ((1 << q->llq.max_n_shift) * dwords) << 3;
3097                 q->base = dmam_alloc_coherent(smmu->dev, qsz, &q->base_dma,
3098                                               GFP_KERNEL);
3099                 if (q->base || qsz < PAGE_SIZE)
3100                         break;
3101
3102                 q->llq.max_n_shift--;
3103         } while (1);
3104
3105         if (!q->base) {
3106                 dev_err(smmu->dev,
3107                         "failed to allocate queue (0x%zx bytes) for %s\n",
3108                         qsz, name);
3109                 return -ENOMEM;
3110         }
3111
3112         if (!WARN_ON(q->base_dma & (qsz - 1))) {
3113                 dev_info(smmu->dev, "allocated %u entries for %s\n",
3114                          1 << q->llq.max_n_shift, name);
3115         }
3116
3117         q->prod_reg     = arm_smmu_page1_fixup(prod_off, smmu);
3118         q->cons_reg     = arm_smmu_page1_fixup(cons_off, smmu);
3119         q->ent_dwords   = dwords;
3120
3121         q->q_base  = Q_BASE_RWA;
3122         q->q_base |= q->base_dma & Q_BASE_ADDR_MASK;
3123         q->q_base |= FIELD_PREP(Q_BASE_LOG2SIZE, q->llq.max_n_shift);
3124
3125         q->llq.prod = q->llq.cons = 0;
3126         return 0;
3127 }
3128
3129 static void arm_smmu_cmdq_free_bitmap(void *data)
3130 {
3131         unsigned long *bitmap = data;
3132         bitmap_free(bitmap);
3133 }
3134
3135 static int arm_smmu_cmdq_init(struct arm_smmu_device *smmu)
3136 {
3137         int ret = 0;
3138         struct arm_smmu_cmdq *cmdq = &smmu->cmdq;
3139         unsigned int nents = 1 << cmdq->q.llq.max_n_shift;
3140         atomic_long_t *bitmap;
3141
3142         atomic_set(&cmdq->owner_prod, 0);
3143         atomic_set(&cmdq->lock, 0);
3144
3145         bitmap = (atomic_long_t *)bitmap_zalloc(nents, GFP_KERNEL);
3146         if (!bitmap) {
3147                 dev_err(smmu->dev, "failed to allocate cmdq bitmap\n");
3148                 ret = -ENOMEM;
3149         } else {
3150                 cmdq->valid_map = bitmap;
3151                 devm_add_action(smmu->dev, arm_smmu_cmdq_free_bitmap, bitmap);
3152         }
3153
3154         return ret;
3155 }
3156
3157 static int arm_smmu_init_queues(struct arm_smmu_device *smmu)
3158 {
3159         int ret;
3160
3161         /* cmdq */
3162         ret = arm_smmu_init_one_queue(smmu, &smmu->cmdq.q, ARM_SMMU_CMDQ_PROD,
3163                                       ARM_SMMU_CMDQ_CONS, CMDQ_ENT_DWORDS,
3164                                       "cmdq");
3165         if (ret)
3166                 return ret;
3167
3168         ret = arm_smmu_cmdq_init(smmu);
3169         if (ret)
3170                 return ret;
3171
3172         /* evtq */
3173         ret = arm_smmu_init_one_queue(smmu, &smmu->evtq.q, ARM_SMMU_EVTQ_PROD,
3174                                       ARM_SMMU_EVTQ_CONS, EVTQ_ENT_DWORDS,
3175                                       "evtq");
3176         if (ret)
3177                 return ret;
3178
3179         /* priq */
3180         if (!(smmu->features & ARM_SMMU_FEAT_PRI))
3181                 return 0;
3182
3183         return arm_smmu_init_one_queue(smmu, &smmu->priq.q, ARM_SMMU_PRIQ_PROD,
3184                                        ARM_SMMU_PRIQ_CONS, PRIQ_ENT_DWORDS,
3185                                        "priq");
3186 }
3187
3188 static int arm_smmu_init_l1_strtab(struct arm_smmu_device *smmu)
3189 {
3190         unsigned int i;
3191         struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
3192         size_t size = sizeof(*cfg->l1_desc) * cfg->num_l1_ents;
3193         void *strtab = smmu->strtab_cfg.strtab;
3194
3195         cfg->l1_desc = devm_kzalloc(smmu->dev, size, GFP_KERNEL);
3196         if (!cfg->l1_desc) {
3197                 dev_err(smmu->dev, "failed to allocate l1 stream table desc\n");
3198                 return -ENOMEM;
3199         }
3200
3201         for (i = 0; i < cfg->num_l1_ents; ++i) {
3202                 arm_smmu_write_strtab_l1_desc(strtab, &cfg->l1_desc[i]);
3203                 strtab += STRTAB_L1_DESC_DWORDS << 3;
3204         }
3205
3206         return 0;
3207 }
3208
3209 static int arm_smmu_init_strtab_2lvl(struct arm_smmu_device *smmu)
3210 {
3211         void *strtab;
3212         u64 reg;
3213         u32 size, l1size;
3214         struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
3215
3216         /* Calculate the L1 size, capped to the SIDSIZE. */
3217         size = STRTAB_L1_SZ_SHIFT - (ilog2(STRTAB_L1_DESC_DWORDS) + 3);
3218         size = min(size, smmu->sid_bits - STRTAB_SPLIT);
3219         cfg->num_l1_ents = 1 << size;
3220
3221         size += STRTAB_SPLIT;
3222         if (size < smmu->sid_bits)
3223                 dev_warn(smmu->dev,
3224                          "2-level strtab only covers %u/%u bits of SID\n",
3225                          size, smmu->sid_bits);
3226
3227         l1size = cfg->num_l1_ents * (STRTAB_L1_DESC_DWORDS << 3);
3228         strtab = dmam_alloc_coherent(smmu->dev, l1size, &cfg->strtab_dma,
3229                                      GFP_KERNEL);
3230         if (!strtab) {
3231                 dev_err(smmu->dev,
3232                         "failed to allocate l1 stream table (%u bytes)\n",
3233                         size);
3234                 return -ENOMEM;
3235         }
3236         cfg->strtab = strtab;
3237
3238         /* Configure strtab_base_cfg for 2 levels */
3239         reg  = FIELD_PREP(STRTAB_BASE_CFG_FMT, STRTAB_BASE_CFG_FMT_2LVL);
3240         reg |= FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, size);
3241         reg |= FIELD_PREP(STRTAB_BASE_CFG_SPLIT, STRTAB_SPLIT);
3242         cfg->strtab_base_cfg = reg;
3243
3244         return arm_smmu_init_l1_strtab(smmu);
3245 }
3246
3247 static int arm_smmu_init_strtab_linear(struct arm_smmu_device *smmu)
3248 {
3249         void *strtab;
3250         u64 reg;
3251         u32 size;
3252         struct arm_smmu_strtab_cfg *cfg = &smmu->strtab_cfg;
3253
3254         size = (1 << smmu->sid_bits) * (STRTAB_STE_DWORDS << 3);
3255         strtab = dmam_alloc_coherent(smmu->dev, size, &cfg->strtab_dma,
3256                                      GFP_KERNEL);
3257         if (!strtab) {
3258                 dev_err(smmu->dev,
3259                         "failed to allocate linear stream table (%u bytes)\n",
3260                         size);
3261                 return -ENOMEM;
3262         }
3263         cfg->strtab = strtab;
3264         cfg->num_l1_ents = 1 << smmu->sid_bits;
3265
3266         /* Configure strtab_base_cfg for a linear table covering all SIDs */
3267         reg  = FIELD_PREP(STRTAB_BASE_CFG_FMT, STRTAB_BASE_CFG_FMT_LINEAR);
3268         reg |= FIELD_PREP(STRTAB_BASE_CFG_LOG2SIZE, smmu->sid_bits);
3269         cfg->strtab_base_cfg = reg;
3270
3271         arm_smmu_init_bypass_stes(strtab, cfg->num_l1_ents);
3272         return 0;
3273 }
3274
3275 static int arm_smmu_init_strtab(struct arm_smmu_device *smmu)
3276 {
3277         u64 reg;
3278         int ret;
3279
3280         if (smmu->features & ARM_SMMU_FEAT_2_LVL_STRTAB)
3281                 ret = arm_smmu_init_strtab_2lvl(smmu);
3282         else
3283                 ret = arm_smmu_init_strtab_linear(smmu);
3284
3285         if (ret)
3286                 return ret;
3287
3288         /* Set the strtab base address */
3289         reg  = smmu->strtab_cfg.strtab_dma & STRTAB_BASE_ADDR_MASK;
3290         reg |= STRTAB_BASE_RA;
3291         smmu->strtab_cfg.strtab_base = reg;
3292
3293         /* Allocate the first VMID for stage-2 bypass STEs */
3294         set_bit(0, smmu->vmid_map);
3295         return 0;
3296 }
3297
3298 static int arm_smmu_init_structures(struct arm_smmu_device *smmu)
3299 {
3300         int ret;
3301
3302         ret = arm_smmu_init_queues(smmu);
3303         if (ret)
3304                 return ret;
3305
3306         return arm_smmu_init_strtab(smmu);
3307 }
3308
3309 static int arm_smmu_write_reg_sync(struct arm_smmu_device *smmu, u32 val,
3310                                    unsigned int reg_off, unsigned int ack_off)
3311 {
3312         u32 reg;
3313
3314         writel_relaxed(val, smmu->base + reg_off);
3315         return readl_relaxed_poll_timeout(smmu->base + ack_off, reg, reg == val,
3316                                           1, ARM_SMMU_POLL_TIMEOUT_US);
3317 }
3318
3319 /* GBPA is "special" */
3320 static int arm_smmu_update_gbpa(struct arm_smmu_device *smmu, u32 set, u32 clr)
3321 {
3322         int ret;
3323         u32 reg, __iomem *gbpa = smmu->base + ARM_SMMU_GBPA;
3324
3325         ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
3326                                          1, ARM_SMMU_POLL_TIMEOUT_US);
3327         if (ret)
3328                 return ret;
3329
3330         reg &= ~clr;
3331         reg |= set;
3332         writel_relaxed(reg | GBPA_UPDATE, gbpa);
3333         ret = readl_relaxed_poll_timeout(gbpa, reg, !(reg & GBPA_UPDATE),
3334                                          1, ARM_SMMU_POLL_TIMEOUT_US);
3335
3336         if (ret)
3337                 dev_err(smmu->dev, "GBPA not responding to update\n");
3338         return ret;
3339 }
3340
3341 static void arm_smmu_free_msis(void *data)
3342 {
3343         struct device *dev = data;
3344         platform_msi_domain_free_irqs(dev);
3345 }
3346
3347 static void arm_smmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
3348 {
3349         phys_addr_t doorbell;
3350         struct device *dev = msi_desc_to_dev(desc);
3351         struct arm_smmu_device *smmu = dev_get_drvdata(dev);
3352         phys_addr_t *cfg = arm_smmu_msi_cfg[desc->platform.msi_index];
3353
3354         doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
3355         doorbell &= MSI_CFG0_ADDR_MASK;
3356
3357         writeq_relaxed(doorbell, smmu->base + cfg[0]);
3358         writel_relaxed(msg->data, smmu->base + cfg[1]);
3359         writel_relaxed(ARM_SMMU_MEMATTR_DEVICE_nGnRE, smmu->base + cfg[2]);
3360 }
3361
3362 static void arm_smmu_setup_msis(struct arm_smmu_device *smmu)
3363 {
3364         struct msi_desc *desc;
3365         int ret, nvec = ARM_SMMU_MAX_MSIS;
3366         struct device *dev = smmu->dev;
3367
3368         /* Clear the MSI address regs */
3369         writeq_relaxed(0, smmu->base + ARM_SMMU_GERROR_IRQ_CFG0);
3370         writeq_relaxed(0, smmu->base + ARM_SMMU_EVTQ_IRQ_CFG0);
3371
3372         if (smmu->features & ARM_SMMU_FEAT_PRI)
3373                 writeq_relaxed(0, smmu->base + ARM_SMMU_PRIQ_IRQ_CFG0);
3374         else
3375                 nvec--;
3376
3377         if (!(smmu->features & ARM_SMMU_FEAT_MSI))
3378                 return;
3379
3380         if (!dev->msi_domain) {
3381                 dev_info(smmu->dev, "msi_domain absent - falling back to wired irqs\n");
3382                 return;
3383         }
3384
3385         /* Allocate MSIs for evtq, gerror and priq. Ignore cmdq */
3386         ret = platform_msi_domain_alloc_irqs(dev, nvec, arm_smmu_write_msi_msg);
3387         if (ret) {
3388                 dev_warn(dev, "failed to allocate MSIs - falling back to wired irqs\n");
3389                 return;
3390         }
3391
3392         for_each_msi_entry(desc, dev) {
3393                 switch (desc->platform.msi_index) {
3394                 case EVTQ_MSI_INDEX:
3395                         smmu->evtq.q.irq = desc->irq;
3396                         break;
3397                 case GERROR_MSI_INDEX:
3398                         smmu->gerr_irq = desc->irq;
3399                         break;
3400                 case PRIQ_MSI_INDEX:
3401                         smmu->priq.q.irq = desc->irq;
3402                         break;
3403                 default:        /* Unknown */
3404                         continue;
3405                 }
3406         }
3407
3408         /* Add callback to free MSIs on teardown */
3409         devm_add_action(dev, arm_smmu_free_msis, dev);
3410 }
3411
3412 static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)
3413 {
3414         int irq, ret;
3415
3416         arm_smmu_setup_msis(smmu);
3417
3418         /* Request interrupt lines */
3419         irq = smmu->evtq.q.irq;
3420         if (irq) {
3421                 ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
3422                                                 arm_smmu_evtq_thread,
3423                                                 IRQF_ONESHOT,
3424                                                 "arm-smmu-v3-evtq", smmu);
3425                 if (ret < 0)
3426                         dev_warn(smmu->dev, "failed to enable evtq irq\n");
3427         } else {
3428                 dev_warn(smmu->dev, "no evtq irq - events will not be reported!\n");
3429         }
3430
3431         irq = smmu->gerr_irq;
3432         if (irq) {
3433                 ret = devm_request_irq(smmu->dev, irq, arm_smmu_gerror_handler,
3434                                        0, "arm-smmu-v3-gerror", smmu);
3435                 if (ret < 0)
3436                         dev_warn(smmu->dev, "failed to enable gerror irq\n");
3437         } else {
3438                 dev_warn(smmu->dev, "no gerr irq - errors will not be reported!\n");
3439         }
3440
3441         if (smmu->features & ARM_SMMU_FEAT_PRI) {
3442                 irq = smmu->priq.q.irq;
3443                 if (irq) {
3444                         ret = devm_request_threaded_irq(smmu->dev, irq, NULL,
3445                                                         arm_smmu_priq_thread,
3446                                                         IRQF_ONESHOT,
3447                                                         "arm-smmu-v3-priq",
3448                                                         smmu);
3449                         if (ret < 0)
3450                                 dev_warn(smmu->dev,
3451                                          "failed to enable priq irq\n");
3452                 } else {
3453                         dev_warn(smmu->dev, "no priq irq - PRI will be broken\n");
3454                 }
3455         }
3456 }
3457
3458 static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)
3459 {
3460         int ret, irq;
3461         u32 irqen_flags = IRQ_CTRL_EVTQ_IRQEN | IRQ_CTRL_GERROR_IRQEN;
3462
3463         /* Disable IRQs first */
3464         ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_IRQ_CTRL,
3465                                       ARM_SMMU_IRQ_CTRLACK);
3466         if (ret) {
3467                 dev_err(smmu->dev, "failed to disable irqs\n");
3468                 return ret;
3469         }
3470
3471         irq = smmu->combined_irq;
3472         if (irq) {
3473                 /*
3474                  * Cavium ThunderX2 implementation doesn't support unique irq
3475                  * lines. Use a single irq line for all the SMMUv3 interrupts.
3476                  */
3477                 ret = devm_request_threaded_irq(smmu->dev, irq,
3478                                         arm_smmu_combined_irq_handler,
3479                                         arm_smmu_combined_irq_thread,
3480                                         IRQF_ONESHOT,
3481                                         "arm-smmu-v3-combined-irq", smmu);
3482                 if (ret < 0)
3483                         dev_warn(smmu->dev, "failed to enable combined irq\n");
3484         } else
3485                 arm_smmu_setup_unique_irqs(smmu);
3486
3487         if (smmu->features & ARM_SMMU_FEAT_PRI)
3488                 irqen_flags |= IRQ_CTRL_PRIQ_IRQEN;
3489
3490         /* Enable interrupt generation on the SMMU */
3491         ret = arm_smmu_write_reg_sync(smmu, irqen_flags,
3492                                       ARM_SMMU_IRQ_CTRL, ARM_SMMU_IRQ_CTRLACK);
3493         if (ret)
3494                 dev_warn(smmu->dev, "failed to enable irqs\n");
3495
3496         return 0;
3497 }
3498
3499 static int arm_smmu_device_disable(struct arm_smmu_device *smmu)
3500 {
3501         int ret;
3502
3503         ret = arm_smmu_write_reg_sync(smmu, 0, ARM_SMMU_CR0, ARM_SMMU_CR0ACK);
3504         if (ret)
3505                 dev_err(smmu->dev, "failed to clear cr0\n");
3506
3507         return ret;
3508 }
3509
3510 static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)
3511 {
3512         int ret;
3513         u32 reg, enables;
3514         struct arm_smmu_cmdq_ent cmd;
3515
3516         /* Clear CR0 and sync (disables SMMU and queue processing) */
3517         reg = readl_relaxed(smmu->base + ARM_SMMU_CR0);
3518         if (reg & CR0_SMMUEN) {
3519                 dev_warn(smmu->dev, "SMMU currently enabled! Resetting...\n");
3520                 WARN_ON(is_kdump_kernel() && !disable_bypass);
3521                 arm_smmu_update_gbpa(smmu, GBPA_ABORT, 0);
3522         }
3523
3524         ret = arm_smmu_device_disable(smmu);
3525         if (ret)
3526                 return ret;
3527
3528         /* CR1 (table and queue memory attributes) */
3529         reg = FIELD_PREP(CR1_TABLE_SH, ARM_SMMU_SH_ISH) |
3530               FIELD_PREP(CR1_TABLE_OC, CR1_CACHE_WB) |
3531               FIELD_PREP(CR1_TABLE_IC, CR1_CACHE_WB) |
3532               FIELD_PREP(CR1_QUEUE_SH, ARM_SMMU_SH_ISH) |
3533               FIELD_PREP(CR1_QUEUE_OC, CR1_CACHE_WB) |
3534               FIELD_PREP(CR1_QUEUE_IC, CR1_CACHE_WB);
3535         writel_relaxed(reg, smmu->base + ARM_SMMU_CR1);
3536
3537         /* CR2 (random crap) */
3538         reg = CR2_PTM | CR2_RECINVSID | CR2_E2H;
3539         writel_relaxed(reg, smmu->base + ARM_SMMU_CR2);
3540
3541         /* Stream table */
3542         writeq_relaxed(smmu->strtab_cfg.strtab_base,
3543                        smmu->base + ARM_SMMU_STRTAB_BASE);
3544         writel_relaxed(smmu->strtab_cfg.strtab_base_cfg,
3545                        smmu->base + ARM_SMMU_STRTAB_BASE_CFG);
3546
3547         /* Command queue */
3548         writeq_relaxed(smmu->cmdq.q.q_base, smmu->base + ARM_SMMU_CMDQ_BASE);
3549         writel_relaxed(smmu->cmdq.q.llq.prod, smmu->base + ARM_SMMU_CMDQ_PROD);
3550         writel_relaxed(smmu->cmdq.q.llq.cons, smmu->base + ARM_SMMU_CMDQ_CONS);
3551
3552         enables = CR0_CMDQEN;
3553         ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
3554                                       ARM_SMMU_CR0ACK);
3555         if (ret) {
3556                 dev_err(smmu->dev, "failed to enable command queue\n");
3557                 return ret;
3558         }
3559
3560         /* Invalidate any cached configuration */
3561         cmd.opcode = CMDQ_OP_CFGI_ALL;
3562         arm_smmu_cmdq_issue_cmd(smmu, &cmd);
3563         arm_smmu_cmdq_issue_sync(smmu);
3564
3565         /* Invalidate any stale TLB entries */
3566         if (smmu->features & ARM_SMMU_FEAT_HYP) {
3567                 cmd.opcode = CMDQ_OP_TLBI_EL2_ALL;
3568                 arm_smmu_cmdq_issue_cmd(smmu, &cmd);
3569         }
3570
3571         cmd.opcode = CMDQ_OP_TLBI_NSNH_ALL;
3572         arm_smmu_cmdq_issue_cmd(smmu, &cmd);
3573         arm_smmu_cmdq_issue_sync(smmu);
3574
3575         /* Event queue */
3576         writeq_relaxed(smmu->evtq.q.q_base, smmu->base + ARM_SMMU_EVTQ_BASE);
3577         writel_relaxed(smmu->evtq.q.llq.prod,
3578                        arm_smmu_page1_fixup(ARM_SMMU_EVTQ_PROD, smmu));
3579         writel_relaxed(smmu->evtq.q.llq.cons,
3580                        arm_smmu_page1_fixup(ARM_SMMU_EVTQ_CONS, smmu));
3581
3582         enables |= CR0_EVTQEN;
3583         ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
3584                                       ARM_SMMU_CR0ACK);
3585         if (ret) {
3586                 dev_err(smmu->dev, "failed to enable event queue\n");
3587                 return ret;
3588         }
3589
3590         /* PRI queue */
3591         if (smmu->features & ARM_SMMU_FEAT_PRI) {
3592                 writeq_relaxed(smmu->priq.q.q_base,
3593                                smmu->base + ARM_SMMU_PRIQ_BASE);
3594                 writel_relaxed(smmu->priq.q.llq.prod,
3595                                arm_smmu_page1_fixup(ARM_SMMU_PRIQ_PROD, smmu));
3596                 writel_relaxed(smmu->priq.q.llq.cons,
3597                                arm_smmu_page1_fixup(ARM_SMMU_PRIQ_CONS, smmu));
3598
3599                 enables |= CR0_PRIQEN;
3600                 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
3601                                               ARM_SMMU_CR0ACK);
3602                 if (ret) {
3603                         dev_err(smmu->dev, "failed to enable PRI queue\n");
3604                         return ret;
3605                 }
3606         }
3607
3608         if (smmu->features & ARM_SMMU_FEAT_ATS) {
3609                 enables |= CR0_ATSCHK;
3610                 ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
3611                                               ARM_SMMU_CR0ACK);
3612                 if (ret) {
3613                         dev_err(smmu->dev, "failed to enable ATS check\n");
3614                         return ret;
3615                 }
3616         }
3617
3618         ret = arm_smmu_setup_irqs(smmu);
3619         if (ret) {
3620                 dev_err(smmu->dev, "failed to setup irqs\n");
3621                 return ret;
3622         }
3623
3624         if (is_kdump_kernel())
3625                 enables &= ~(CR0_EVTQEN | CR0_PRIQEN);
3626
3627         /* Enable the SMMU interface, or ensure bypass */
3628         if (!bypass || disable_bypass) {
3629                 enables |= CR0_SMMUEN;
3630         } else {
3631                 ret = arm_smmu_update_gbpa(smmu, 0, GBPA_ABORT);
3632                 if (ret)
3633                         return ret;
3634         }
3635         ret = arm_smmu_write_reg_sync(smmu, enables, ARM_SMMU_CR0,
3636                                       ARM_SMMU_CR0ACK);
3637         if (ret) {
3638                 dev_err(smmu->dev, "failed to enable SMMU interface\n");
3639                 return ret;
3640         }
3641
3642         return 0;
3643 }
3644
3645 static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)
3646 {
3647         u32 reg;
3648         bool coherent = smmu->features & ARM_SMMU_FEAT_COHERENCY;
3649
3650         /* IDR0 */
3651         reg = readl_relaxed(smmu->base + ARM_SMMU_IDR0);
3652
3653         /* 2-level structures */
3654         if (FIELD_GET(IDR0_ST_LVL, reg) == IDR0_ST_LVL_2LVL)
3655                 smmu->features |= ARM_SMMU_FEAT_2_LVL_STRTAB;
3656
3657         if (reg & IDR0_CD2L)
3658                 smmu->features |= ARM_SMMU_FEAT_2_LVL_CDTAB;
3659
3660         /*
3661          * Translation table endianness.
3662          * We currently require the same endianness as the CPU, but this
3663          * could be changed later by adding a new IO_PGTABLE_QUIRK.
3664          */
3665         switch (FIELD_GET(IDR0_TTENDIAN, reg)) {
3666         case IDR0_TTENDIAN_MIXED:
3667                 smmu->features |= ARM_SMMU_FEAT_TT_LE | ARM_SMMU_FEAT_TT_BE;
3668                 break;
3669 #ifdef __BIG_ENDIAN
3670         case IDR0_TTENDIAN_BE:
3671                 smmu->features |= ARM_SMMU_FEAT_TT_BE;
3672                 break;
3673 #else
3674         case IDR0_TTENDIAN_LE:
3675                 smmu->features |= ARM_SMMU_FEAT_TT_LE;
3676                 break;
3677 #endif
3678         default:
3679                 dev_err(smmu->dev, "unknown/unsupported TT endianness!\n");
3680                 return -ENXIO;
3681         }
3682
3683         /* Boolean feature flags */
3684         if (IS_ENABLED(CONFIG_PCI_PRI) && reg & IDR0_PRI)
3685                 smmu->features |= ARM_SMMU_FEAT_PRI;
3686
3687         if (IS_ENABLED(CONFIG_PCI_ATS) && reg & IDR0_ATS)
3688                 smmu->features |= ARM_SMMU_FEAT_ATS;
3689
3690         if (reg & IDR0_SEV)
3691                 smmu->features |= ARM_SMMU_FEAT_SEV;
3692
3693         if (reg & IDR0_MSI)
3694                 smmu->features |= ARM_SMMU_FEAT_MSI;
3695
3696         if (reg & IDR0_HYP)
3697                 smmu->features |= ARM_SMMU_FEAT_HYP;
3698
3699         /*
3700          * The coherency feature as set by FW is used in preference to the ID
3701          * register, but warn on mismatch.
3702          */
3703         if (!!(reg & IDR0_COHACC) != coherent)
3704                 dev_warn(smmu->dev, "IDR0.COHACC overridden by FW configuration (%s)\n",
3705                          coherent ? "true" : "false");
3706
3707         switch (FIELD_GET(IDR0_STALL_MODEL, reg)) {
3708         case IDR0_STALL_MODEL_FORCE:
3709                 smmu->features |= ARM_SMMU_FEAT_STALL_FORCE;
3710                 /* Fallthrough */
3711         case IDR0_STALL_MODEL_STALL:
3712                 smmu->features |= ARM_SMMU_FEAT_STALLS;
3713         }
3714
3715         if (reg & IDR0_S1P)
3716                 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
3717
3718         if (reg & IDR0_S2P)
3719                 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
3720
3721         if (!(reg & (IDR0_S1P | IDR0_S2P))) {
3722                 dev_err(smmu->dev, "no translation support!\n");
3723                 return -ENXIO;
3724         }
3725
3726         /* We only support the AArch64 table format at present */
3727         switch (FIELD_GET(IDR0_TTF, reg)) {
3728         case IDR0_TTF_AARCH32_64:
3729                 smmu->ias = 40;
3730                 /* Fallthrough */
3731         case IDR0_TTF_AARCH64:
3732                 break;
3733         default:
3734                 dev_err(smmu->dev, "AArch64 table format not supported!\n");
3735                 return -ENXIO;
3736         }
3737
3738         /* ASID/VMID sizes */
3739         smmu->asid_bits = reg & IDR0_ASID16 ? 16 : 8;
3740         smmu->vmid_bits = reg & IDR0_VMID16 ? 16 : 8;
3741
3742         /* IDR1 */
3743         reg = readl_relaxed(smmu->base + ARM_SMMU_IDR1);
3744         if (reg & (IDR1_TABLES_PRESET | IDR1_QUEUES_PRESET | IDR1_REL)) {
3745                 dev_err(smmu->dev, "embedded implementation not supported\n");
3746                 return -ENXIO;
3747         }
3748
3749         /* Queue sizes, capped to ensure natural alignment */
3750         smmu->cmdq.q.llq.max_n_shift = min_t(u32, CMDQ_MAX_SZ_SHIFT,
3751                                              FIELD_GET(IDR1_CMDQS, reg));
3752         if (smmu->cmdq.q.llq.max_n_shift <= ilog2(CMDQ_BATCH_ENTRIES)) {
3753                 /*
3754                  * We don't support splitting up batches, so one batch of
3755                  * commands plus an extra sync needs to fit inside the command
3756                  * queue. There's also no way we can handle the weird alignment
3757                  * restrictions on the base pointer for a unit-length queue.
3758                  */
3759                 dev_err(smmu->dev, "command queue size <= %d entries not supported\n",
3760                         CMDQ_BATCH_ENTRIES);
3761                 return -ENXIO;
3762         }
3763
3764         smmu->evtq.q.llq.max_n_shift = min_t(u32, EVTQ_MAX_SZ_SHIFT,
3765                                              FIELD_GET(IDR1_EVTQS, reg));
3766         smmu->priq.q.llq.max_n_shift = min_t(u32, PRIQ_MAX_SZ_SHIFT,
3767                                              FIELD_GET(IDR1_PRIQS, reg));
3768
3769         /* SID/SSID sizes */
3770         smmu->ssid_bits = FIELD_GET(IDR1_SSIDSIZE, reg);
3771         smmu->sid_bits = FIELD_GET(IDR1_SIDSIZE, reg);
3772
3773         /*
3774          * If the SMMU supports fewer bits than would fill a single L2 stream
3775          * table, use a linear table instead.
3776          */
3777         if (smmu->sid_bits <= STRTAB_SPLIT)
3778                 smmu->features &= ~ARM_SMMU_FEAT_2_LVL_STRTAB;
3779
3780         /* IDR5 */
3781         reg = readl_relaxed(smmu->base + ARM_SMMU_IDR5);
3782
3783         /* Maximum number of outstanding stalls */
3784         smmu->evtq.max_stalls = FIELD_GET(IDR5_STALL_MAX, reg);
3785
3786         /* Page sizes */
3787         if (reg & IDR5_GRAN64K)
3788                 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
3789         if (reg & IDR5_GRAN16K)
3790                 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
3791         if (reg & IDR5_GRAN4K)
3792                 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
3793
3794         /* Input address size */
3795         if (FIELD_GET(IDR5_VAX, reg) == IDR5_VAX_52_BIT)
3796                 smmu->features |= ARM_SMMU_FEAT_VAX;
3797
3798         /* Output address size */
3799         switch (FIELD_GET(IDR5_OAS, reg)) {
3800         case IDR5_OAS_32_BIT:
3801                 smmu->oas = 32;
3802                 break;
3803         case IDR5_OAS_36_BIT:
3804                 smmu->oas = 36;
3805                 break;
3806         case IDR5_OAS_40_BIT:
3807                 smmu->oas = 40;
3808                 break;
3809         case IDR5_OAS_42_BIT:
3810                 smmu->oas = 42;
3811                 break;
3812         case IDR5_OAS_44_BIT:
3813                 smmu->oas = 44;
3814                 break;
3815         case IDR5_OAS_52_BIT:
3816                 smmu->oas = 52;
3817                 smmu->pgsize_bitmap |= 1ULL << 42; /* 4TB */
3818                 break;
3819         default:
3820                 dev_info(smmu->dev,
3821                         "unknown output address size. Truncating to 48-bit\n");
3822                 /* Fallthrough */
3823         case IDR5_OAS_48_BIT:
3824                 smmu->oas = 48;
3825         }
3826
3827         if (arm_smmu_ops.pgsize_bitmap == -1UL)
3828                 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
3829         else
3830                 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
3831
3832         /* Set the DMA mask for our table walker */
3833         if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(smmu->oas)))
3834                 dev_warn(smmu->dev,
3835                          "failed to set DMA mask for table walker\n");
3836
3837         smmu->ias = max(smmu->ias, smmu->oas);
3838
3839         dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",
3840                  smmu->ias, smmu->oas, smmu->features);
3841         return 0;
3842 }
3843
3844 #ifdef CONFIG_ACPI
3845 static void acpi_smmu_get_options(u32 model, struct arm_smmu_device *smmu)
3846 {
3847         switch (model) {
3848         case ACPI_IORT_SMMU_V3_CAVIUM_CN99XX:
3849                 smmu->options |= ARM_SMMU_OPT_PAGE0_REGS_ONLY;
3850                 break;
3851         case ACPI_IORT_SMMU_V3_HISILICON_HI161X:
3852                 smmu->options |= ARM_SMMU_OPT_SKIP_PREFETCH;
3853                 break;
3854         }
3855
3856         dev_notice(smmu->dev, "option mask 0x%x\n", smmu->options);
3857 }
3858
3859 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
3860                                       struct arm_smmu_device *smmu)
3861 {
3862         struct acpi_iort_smmu_v3 *iort_smmu;
3863         struct device *dev = smmu->dev;
3864         struct acpi_iort_node *node;
3865
3866         node = *(struct acpi_iort_node **)dev_get_platdata(dev);
3867
3868         /* Retrieve SMMUv3 specific data */
3869         iort_smmu = (struct acpi_iort_smmu_v3 *)node->node_data;
3870
3871         acpi_smmu_get_options(iort_smmu->model, smmu);
3872
3873         if (iort_smmu->flags & ACPI_IORT_SMMU_V3_COHACC_OVERRIDE)
3874                 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
3875
3876         return 0;
3877 }
3878 #else
3879 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
3880                                              struct arm_smmu_device *smmu)
3881 {
3882         return -ENODEV;
3883 }
3884 #endif
3885
3886 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
3887                                     struct arm_smmu_device *smmu)
3888 {
3889         struct device *dev = &pdev->dev;
3890         u32 cells;
3891         int ret = -EINVAL;
3892
3893         if (of_property_read_u32(dev->of_node, "#iommu-cells", &cells))
3894                 dev_err(dev, "missing #iommu-cells property\n");
3895         else if (cells != 1)
3896                 dev_err(dev, "invalid #iommu-cells value (%d)\n", cells);
3897         else
3898                 ret = 0;
3899
3900         parse_driver_options(smmu);
3901
3902         if (of_dma_is_coherent(dev->of_node))
3903                 smmu->features |= ARM_SMMU_FEAT_COHERENCY;
3904
3905         return ret;
3906 }
3907
3908 static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)
3909 {
3910         if (smmu->options & ARM_SMMU_OPT_PAGE0_REGS_ONLY)
3911                 return SZ_64K;
3912         else
3913                 return SZ_128K;
3914 }
3915
3916 static int arm_smmu_set_bus_ops(struct iommu_ops *ops)
3917 {
3918         int err;
3919
3920 #ifdef CONFIG_PCI
3921         if (pci_bus_type.iommu_ops != ops) {
3922                 err = bus_set_iommu(&pci_bus_type, ops);
3923                 if (err)
3924                         return err;
3925         }
3926 #endif
3927 #ifdef CONFIG_ARM_AMBA
3928         if (amba_bustype.iommu_ops != ops) {
3929                 err = bus_set_iommu(&amba_bustype, ops);
3930                 if (err)
3931                         goto err_reset_pci_ops;
3932         }
3933 #endif
3934         if (platform_bus_type.iommu_ops != ops) {
3935                 err = bus_set_iommu(&platform_bus_type, ops);
3936                 if (err)
3937                         goto err_reset_amba_ops;
3938         }
3939
3940         return 0;
3941
3942 err_reset_amba_ops:
3943 #ifdef CONFIG_ARM_AMBA
3944         bus_set_iommu(&amba_bustype, NULL);
3945 #endif
3946 err_reset_pci_ops: __maybe_unused;
3947 #ifdef CONFIG_PCI
3948         bus_set_iommu(&pci_bus_type, NULL);
3949 #endif
3950         return err;
3951 }
3952
3953 static int arm_smmu_device_probe(struct platform_device *pdev)
3954 {
3955         int irq, ret;
3956         struct resource *res;
3957         resource_size_t ioaddr;
3958         struct arm_smmu_device *smmu;
3959         struct device *dev = &pdev->dev;
3960         bool bypass;
3961
3962         smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
3963         if (!smmu) {
3964                 dev_err(dev, "failed to allocate arm_smmu_device\n");
3965                 return -ENOMEM;
3966         }
3967         smmu->dev = dev;
3968
3969         if (dev->of_node) {
3970                 ret = arm_smmu_device_dt_probe(pdev, smmu);
3971         } else {
3972                 ret = arm_smmu_device_acpi_probe(pdev, smmu);
3973                 if (ret == -ENODEV)
3974                         return ret;
3975         }
3976
3977         /* Set bypass mode according to firmware probing result */
3978         bypass = !!ret;
3979
3980         /* Base address */
3981         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3982         if (resource_size(res) < arm_smmu_resource_size(smmu)) {
3983                 dev_err(dev, "MMIO region too small (%pr)\n", res);
3984                 return -EINVAL;
3985         }
3986         ioaddr = res->start;
3987
3988         smmu->base = devm_ioremap_resource(dev, res);
3989         if (IS_ERR(smmu->base))
3990                 return PTR_ERR(smmu->base);
3991
3992         /* Interrupt lines */
3993
3994         irq = platform_get_irq_byname_optional(pdev, "combined");
3995         if (irq > 0)
3996                 smmu->combined_irq = irq;
3997         else {
3998                 irq = platform_get_irq_byname_optional(pdev, "eventq");
3999                 if (irq > 0)
4000                         smmu->evtq.q.irq = irq;
4001
4002                 irq = platform_get_irq_byname_optional(pdev, "priq");
4003                 if (irq > 0)
4004                         smmu->priq.q.irq = irq;
4005
4006                 irq = platform_get_irq_byname_optional(pdev, "gerror");
4007                 if (irq > 0)
4008                         smmu->gerr_irq = irq;
4009         }
4010         /* Probe the h/w */
4011         ret = arm_smmu_device_hw_probe(smmu);
4012         if (ret)
4013                 return ret;
4014
4015         /* Initialise in-memory data structures */
4016         ret = arm_smmu_init_structures(smmu);
4017         if (ret)
4018                 return ret;
4019
4020         /* Record our private device structure */
4021         platform_set_drvdata(pdev, smmu);
4022
4023         /* Reset the device */
4024         ret = arm_smmu_device_reset(smmu, bypass);
4025         if (ret)
4026                 return ret;
4027
4028         /* And we're up. Go go go! */
4029         ret = iommu_device_sysfs_add(&smmu->iommu, dev, NULL,
4030                                      "smmu3.%pa", &ioaddr);
4031         if (ret)
4032                 return ret;
4033
4034         iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
4035         iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
4036
4037         ret = iommu_device_register(&smmu->iommu);
4038         if (ret) {
4039                 dev_err(dev, "Failed to register iommu\n");
4040                 return ret;
4041         }
4042
4043         return arm_smmu_set_bus_ops(&arm_smmu_ops);
4044 }
4045
4046 static int arm_smmu_device_remove(struct platform_device *pdev)
4047 {
4048         struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
4049
4050         arm_smmu_set_bus_ops(NULL);
4051         iommu_device_unregister(&smmu->iommu);
4052         iommu_device_sysfs_remove(&smmu->iommu);
4053         arm_smmu_device_disable(smmu);
4054
4055         return 0;
4056 }
4057
4058 static void arm_smmu_device_shutdown(struct platform_device *pdev)
4059 {
4060         arm_smmu_device_remove(pdev);
4061 }
4062
4063 static const struct of_device_id arm_smmu_of_match[] = {
4064         { .compatible = "arm,smmu-v3", },
4065         { },
4066 };
4067 MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
4068
4069 static struct platform_driver arm_smmu_driver = {
4070         .driver = {
4071                 .name                   = "arm-smmu-v3",
4072                 .of_match_table         = arm_smmu_of_match,
4073                 .suppress_bind_attrs    = true,
4074         },
4075         .probe  = arm_smmu_device_probe,
4076         .remove = arm_smmu_device_remove,
4077         .shutdown = arm_smmu_device_shutdown,
4078 };
4079 module_platform_driver(arm_smmu_driver);
4080
4081 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMUv3 implementations");
4082 MODULE_AUTHOR("Will Deacon <will@kernel.org>");
4083 MODULE_ALIAS("platform:arm-smmu-v3");
4084 MODULE_LICENSE("GPL v2");