]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/iommu/arm-smmu.c
iommu/arm-smmu: Avoid constant zero in TLBI writes
[thirdparty/linux.git] / drivers / iommu / arm-smmu.c
CommitLineData
45ae7cff
WD
1/*
2 * IOMMU API for ARM architected SMMU implementations.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 *
17 * Copyright (C) 2013 ARM Limited
18 *
19 * Author: Will Deacon <will.deacon@arm.com>
20 *
21 * This driver currently supports:
22 * - SMMUv1 and v2 implementations
23 * - Stream-matching and stream-indexing
24 * - v7/v8 long-descriptor format
25 * - Non-secure access to the SMMU
45ae7cff 26 * - Context fault reporting
dc0eaa4e 27 * - Extended Stream ID (16 bit)
45ae7cff
WD
28 */
29
30#define pr_fmt(fmt) "arm-smmu: " fmt
31
d6fcd3b1
LP
32#include <linux/acpi.h>
33#include <linux/acpi_iort.h>
1f3d5ca4 34#include <linux/atomic.h>
45ae7cff 35#include <linux/delay.h>
9adb9594 36#include <linux/dma-iommu.h>
45ae7cff
WD
37#include <linux/dma-mapping.h>
38#include <linux/err.h>
39#include <linux/interrupt.h>
40#include <linux/io.h>
f9a05f05 41#include <linux/io-64-nonatomic-hi-lo.h>
b77cf11f 42#include <linux/io-pgtable.h>
45ae7cff 43#include <linux/iommu.h>
859a732e 44#include <linux/iopoll.h>
addb672f
PG
45#include <linux/init.h>
46#include <linux/moduleparam.h>
45ae7cff 47#include <linux/of.h>
bae2c2d4 48#include <linux/of_address.h>
d6fc5d97 49#include <linux/of_device.h>
adfec2e7 50#include <linux/of_iommu.h>
a9a1b0b5 51#include <linux/pci.h>
45ae7cff 52#include <linux/platform_device.h>
96a299d2 53#include <linux/pm_runtime.h>
45ae7cff
WD
54#include <linux/slab.h>
55#include <linux/spinlock.h>
56
57#include <linux/amba/bus.h>
eab03e2a 58#include <linux/fsl/mc.h>
45ae7cff 59
2b03774b
RC
60#include "arm-smmu-regs.h"
61
4e4abae3
RM
62/*
63 * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU
64 * global register space are still, in fact, using a hypervisor to mediate it
65 * by trapping and emulating register accesses. Sadly, some deployed versions
66 * of said trapping code have bugs wherein they go horribly wrong for stores
67 * using r31 (i.e. XZR/WZR) as the source register.
68 */
69#define QCOM_DUMMY_VAL -1
70
2b03774b
RC
71#define ARM_MMU500_ACTLR_CPRE (1 << 1)
72
73#define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
74f55d34 74#define ARM_MMU500_ACR_S2CRB_TLBEN (1 << 10)
2b03774b
RC
75#define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8)
76
77#define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
78#define TLB_SPIN_COUNT 10
45ae7cff 79
45ae7cff
WD
80/* Maximum number of context banks per SMMU */
81#define ARM_SMMU_MAX_CBS 128
82
45ae7cff
WD
83/* SMMU global address space */
84#define ARM_SMMU_GR0(smmu) ((smmu)->base)
c757e852 85#define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
45ae7cff 86
3a5df8ff
AH
87/*
88 * SMMU global address space with conditional offset to access secure
89 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
90 * nsGFSYNR0: 0x450)
91 */
92#define ARM_SMMU_GR0_NS(smmu) \
93 ((smmu)->base + \
94 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
95 ? 0x400 : 0))
96
f9a05f05
RM
97/*
98 * Some 64-bit registers only make sense to write atomically, but in such
99 * cases all the data relevant to AArch32 formats lies within the lower word,
100 * therefore this actually makes more sense than it might first appear.
101 */
668b4ada 102#ifdef CONFIG_64BIT
f9a05f05 103#define smmu_write_atomic_lq writeq_relaxed
668b4ada 104#else
f9a05f05 105#define smmu_write_atomic_lq writel_relaxed
668b4ada
TC
106#endif
107
45ae7cff 108/* Translation context bank */
452107c7 109#define ARM_SMMU_CB(smmu, n) ((smmu)->cb_base + ((n) << (smmu)->pgshift))
45ae7cff 110
f3ebee80
EA
111#define MSI_IOVA_BASE 0x8000000
112#define MSI_IOVA_LENGTH 0x100000
113
4cf740b0 114static int force_stage;
addb672f
PG
115/*
116 * not really modular, but the easiest way to keep compat with existing
117 * bootargs behaviour is to continue using module_param() here.
118 */
25a1c96c 119module_param(force_stage, int, S_IRUGO);
4cf740b0
WD
120MODULE_PARM_DESC(force_stage,
121 "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
954a03be
DA
122static bool disable_bypass =
123 IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT);
25a1c96c
RM
124module_param(disable_bypass, bool, S_IRUGO);
125MODULE_PARM_DESC(disable_bypass,
126 "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.");
4cf740b0 127
09360403 128enum arm_smmu_arch_version {
b7862e35
RM
129 ARM_SMMU_V1,
130 ARM_SMMU_V1_64K,
09360403
RM
131 ARM_SMMU_V2,
132};
133
67b65a3f
RM
134enum arm_smmu_implementation {
135 GENERIC_SMMU,
f0cfffc4 136 ARM_MMU500,
e086d912 137 CAVIUM_SMMUV2,
89cddc56 138 QCOM_SMMUV2,
67b65a3f
RM
139};
140
8e8b203e 141struct arm_smmu_s2cr {
588888a7
RM
142 struct iommu_group *group;
143 int count;
8e8b203e
RM
144 enum arm_smmu_s2cr_type type;
145 enum arm_smmu_s2cr_privcfg privcfg;
146 u8 cbndx;
147};
148
149#define s2cr_init_val (struct arm_smmu_s2cr){ \
150 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
151}
152
45ae7cff 153struct arm_smmu_smr {
45ae7cff
WD
154 u16 mask;
155 u16 id;
1f3d5ca4 156 bool valid;
45ae7cff
WD
157};
158
90df373c
RM
159struct arm_smmu_cb {
160 u64 ttbr[2];
161 u32 tcr[2];
162 u32 mair[2];
163 struct arm_smmu_cfg *cfg;
164};
165
a9a1b0b5 166struct arm_smmu_master_cfg {
f80cd885 167 struct arm_smmu_device *smmu;
adfec2e7 168 s16 smendx[];
45ae7cff 169};
1f3d5ca4 170#define INVALID_SMENDX -1
adfec2e7
RM
171#define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
172#define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
8c82d6ec
RM
173#define fwspec_smendx(fw, i) \
174 (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
adfec2e7 175#define for_each_cfg_sme(fw, i, idx) \
8c82d6ec 176 for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
45ae7cff
WD
177
178struct arm_smmu_device {
179 struct device *dev;
45ae7cff
WD
180
181 void __iomem *base;
452107c7 182 void __iomem *cb_base;
c757e852 183 unsigned long pgshift;
45ae7cff
WD
184
185#define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
186#define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
187#define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
188#define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
189#define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
859a732e 190#define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
4e3e9b69 191#define ARM_SMMU_FEAT_VMID16 (1 << 6)
7602b871
RM
192#define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
193#define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
194#define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
195#define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
196#define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
dc0eaa4e 197#define ARM_SMMU_FEAT_EXIDS (1 << 12)
45ae7cff 198 u32 features;
3a5df8ff
AH
199
200#define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
201 u32 options;
09360403 202 enum arm_smmu_arch_version version;
67b65a3f 203 enum arm_smmu_implementation model;
45ae7cff
WD
204
205 u32 num_context_banks;
206 u32 num_s2_context_banks;
207 DECLARE_BITMAP(context_map, ARM_SMMU_MAX_CBS);
90df373c 208 struct arm_smmu_cb *cbs;
45ae7cff
WD
209 atomic_t irptndx;
210
211 u32 num_mapping_groups;
21174240
RM
212 u16 streamid_mask;
213 u16 smr_mask_mask;
1f3d5ca4 214 struct arm_smmu_smr *smrs;
8e8b203e 215 struct arm_smmu_s2cr *s2crs;
588888a7 216 struct mutex stream_map_mutex;
45ae7cff 217
518f7136
WD
218 unsigned long va_size;
219 unsigned long ipa_size;
220 unsigned long pa_size;
d5466357 221 unsigned long pgsize_bitmap;
45ae7cff
WD
222
223 u32 num_global_irqs;
224 u32 num_context_irqs;
225 unsigned int *irqs;
96a299d2
SR
226 struct clk_bulk_data *clks;
227 int num_clks;
45ae7cff 228
1bd37a68 229 u32 cavium_id_base; /* Specific to Cavium */
9648cbc9 230
8e517e76
WD
231 spinlock_t global_sync_lock;
232
9648cbc9
JR
233 /* IOMMU core code handle */
234 struct iommu_device iommu;
45ae7cff
WD
235};
236
7602b871
RM
237enum arm_smmu_context_fmt {
238 ARM_SMMU_CTX_FMT_NONE,
239 ARM_SMMU_CTX_FMT_AARCH64,
240 ARM_SMMU_CTX_FMT_AARCH32_L,
241 ARM_SMMU_CTX_FMT_AARCH32_S,
45ae7cff
WD
242};
243
244struct arm_smmu_cfg {
45ae7cff
WD
245 u8 cbndx;
246 u8 irptndx;
280b683c
RM
247 union {
248 u16 asid;
249 u16 vmid;
250 };
45ae7cff 251 u32 cbar;
7602b871 252 enum arm_smmu_context_fmt fmt;
45ae7cff 253};
faea13b7 254#define INVALID_IRPTNDX 0xff
45ae7cff 255
c752ce45
WD
256enum arm_smmu_domain_stage {
257 ARM_SMMU_DOMAIN_S1 = 0,
258 ARM_SMMU_DOMAIN_S2,
259 ARM_SMMU_DOMAIN_NESTED,
61bc6711 260 ARM_SMMU_DOMAIN_BYPASS,
c752ce45
WD
261};
262
45ae7cff 263struct arm_smmu_domain {
44680eed 264 struct arm_smmu_device *smmu;
518f7136 265 struct io_pgtable_ops *pgtbl_ops;
32b12449 266 const struct iommu_gather_ops *tlb_ops;
44680eed 267 struct arm_smmu_cfg cfg;
c752ce45 268 enum arm_smmu_domain_stage stage;
44f6876a 269 bool non_strict;
518f7136 270 struct mutex init_mutex; /* Protects smmu pointer */
8e517e76 271 spinlock_t cb_lock; /* Serialises ATS1* ops and TLB syncs */
1d672638 272 struct iommu_domain domain;
45ae7cff
WD
273};
274
3a5df8ff
AH
275struct arm_smmu_option_prop {
276 u32 opt;
277 const char *prop;
278};
279
1bd37a68
TC
280static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0);
281
021bb842
RM
282static bool using_legacy_binding, using_generic_binding;
283
2907320d 284static struct arm_smmu_option_prop arm_smmu_options[] = {
3a5df8ff
AH
285 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
286 { 0, NULL},
287};
288
d4a44f07
SR
289static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)
290{
291 if (pm_runtime_enabled(smmu->dev))
292 return pm_runtime_get_sync(smmu->dev);
293
294 return 0;
295}
296
297static inline void arm_smmu_rpm_put(struct arm_smmu_device *smmu)
298{
299 if (pm_runtime_enabled(smmu->dev))
300 pm_runtime_put(smmu->dev);
301}
302
1d672638
JR
303static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
304{
305 return container_of(dom, struct arm_smmu_domain, domain);
306}
307
3a5df8ff
AH
308static void parse_driver_options(struct arm_smmu_device *smmu)
309{
310 int i = 0;
2907320d 311
3a5df8ff
AH
312 do {
313 if (of_property_read_bool(smmu->dev->of_node,
314 arm_smmu_options[i].prop)) {
315 smmu->options |= arm_smmu_options[i].opt;
316 dev_notice(smmu->dev, "option %s\n",
317 arm_smmu_options[i].prop);
318 }
319 } while (arm_smmu_options[++i].opt);
320}
321
8f68f8e2 322static struct device_node *dev_get_dev_node(struct device *dev)
a9a1b0b5
WD
323{
324 if (dev_is_pci(dev)) {
325 struct pci_bus *bus = to_pci_dev(dev)->bus;
2907320d 326
a9a1b0b5
WD
327 while (!pci_is_root_bus(bus))
328 bus = bus->parent;
f80cd885 329 return of_node_get(bus->bridge->parent->of_node);
a9a1b0b5
WD
330 }
331
f80cd885 332 return of_node_get(dev->of_node);
a9a1b0b5
WD
333}
334
f80cd885 335static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
45ae7cff 336{
f80cd885
RM
337 *((__be32 *)data) = cpu_to_be32(alias);
338 return 0; /* Continue walking */
45ae7cff
WD
339}
340
f80cd885 341static int __find_legacy_master_phandle(struct device *dev, void *data)
a9a1b0b5 342{
f80cd885
RM
343 struct of_phandle_iterator *it = *(void **)data;
344 struct device_node *np = it->node;
345 int err;
346
347 of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
348 "#stream-id-cells", 0)
349 if (it->node == np) {
350 *(void **)data = dev;
351 return 1;
352 }
353 it->node = np;
354 return err == -ENOENT ? 0 : err;
a9a1b0b5
WD
355}
356
d6fc5d97 357static struct platform_driver arm_smmu_driver;
adfec2e7 358static struct iommu_ops arm_smmu_ops;
d6fc5d97 359
adfec2e7
RM
360static int arm_smmu_register_legacy_master(struct device *dev,
361 struct arm_smmu_device **smmu)
45ae7cff 362{
adfec2e7 363 struct device *smmu_dev;
f80cd885
RM
364 struct device_node *np;
365 struct of_phandle_iterator it;
366 void *data = &it;
adfec2e7 367 u32 *sids;
f80cd885
RM
368 __be32 pci_sid;
369 int err;
45ae7cff 370
f80cd885
RM
371 np = dev_get_dev_node(dev);
372 if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
373 of_node_put(np);
374 return -ENODEV;
375 }
45ae7cff 376
f80cd885 377 it.node = np;
d6fc5d97
RM
378 err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
379 __find_legacy_master_phandle);
adfec2e7 380 smmu_dev = data;
f80cd885
RM
381 of_node_put(np);
382 if (err == 0)
383 return -ENODEV;
384 if (err < 0)
385 return err;
45ae7cff 386
f80cd885
RM
387 if (dev_is_pci(dev)) {
388 /* "mmu-masters" assumes Stream ID == Requester ID */
389 pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
390 &pci_sid);
391 it.cur = &pci_sid;
392 it.cur_count = 1;
393 }
45ae7cff 394
adfec2e7
RM
395 err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
396 &arm_smmu_ops);
397 if (err)
398 return err;
45ae7cff 399
adfec2e7
RM
400 sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
401 if (!sids)
402 return -ENOMEM;
44680eed 403
adfec2e7
RM
404 *smmu = dev_get_drvdata(smmu_dev);
405 of_phandle_iterator_args(&it, sids, it.cur_count);
406 err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
407 kfree(sids);
408 return err;
45ae7cff
WD
409}
410
411static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
412{
413 int idx;
414
415 do {
416 idx = find_next_zero_bit(map, end, start);
417 if (idx == end)
418 return -ENOSPC;
419 } while (test_and_set_bit(idx, map));
420
421 return idx;
422}
423
424static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
425{
426 clear_bit(idx, map);
427}
428
429/* Wait for any pending TLB invalidations to complete */
11febfca
RM
430static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu,
431 void __iomem *sync, void __iomem *status)
45ae7cff 432{
8513c893 433 unsigned int spin_cnt, delay;
45ae7cff 434
4e4abae3 435 writel_relaxed(QCOM_DUMMY_VAL, sync);
8513c893
RM
436 for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
437 for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
438 if (!(readl_relaxed(status) & sTLBGSTATUS_GSACTIVE))
439 return;
440 cpu_relax();
45ae7cff 441 }
8513c893 442 udelay(delay);
45ae7cff 443 }
8513c893
RM
444 dev_err_ratelimited(smmu->dev,
445 "TLB sync timed out -- SMMU may be deadlocked\n");
45ae7cff
WD
446}
447
11febfca
RM
448static void arm_smmu_tlb_sync_global(struct arm_smmu_device *smmu)
449{
450 void __iomem *base = ARM_SMMU_GR0(smmu);
8e517e76 451 unsigned long flags;
11febfca 452
8e517e76 453 spin_lock_irqsave(&smmu->global_sync_lock, flags);
11febfca
RM
454 __arm_smmu_tlb_sync(smmu, base + ARM_SMMU_GR0_sTLBGSYNC,
455 base + ARM_SMMU_GR0_sTLBGSTATUS);
8e517e76 456 spin_unlock_irqrestore(&smmu->global_sync_lock, flags);
11febfca
RM
457}
458
459static void arm_smmu_tlb_sync_context(void *cookie)
518f7136
WD
460{
461 struct arm_smmu_domain *smmu_domain = cookie;
11febfca
RM
462 struct arm_smmu_device *smmu = smmu_domain->smmu;
463 void __iomem *base = ARM_SMMU_CB(smmu, smmu_domain->cfg.cbndx);
8e517e76 464 unsigned long flags;
11febfca 465
8e517e76 466 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
11febfca
RM
467 __arm_smmu_tlb_sync(smmu, base + ARM_SMMU_CB_TLBSYNC,
468 base + ARM_SMMU_CB_TLBSTATUS);
8e517e76 469 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
518f7136
WD
470}
471
11febfca 472static void arm_smmu_tlb_sync_vmid(void *cookie)
518f7136
WD
473{
474 struct arm_smmu_domain *smmu_domain = cookie;
11febfca
RM
475
476 arm_smmu_tlb_sync_global(smmu_domain->smmu);
518f7136
WD
477}
478
11febfca 479static void arm_smmu_tlb_inv_context_s1(void *cookie)
1463fe44 480{
518f7136 481 struct arm_smmu_domain *smmu_domain = cookie;
44680eed 482 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
11febfca 483 void __iomem *base = ARM_SMMU_CB(smmu_domain->smmu, cfg->cbndx);
1463fe44 484
44f6876a
RM
485 /*
486 * NOTE: this is not a relaxed write; it needs to guarantee that PTEs
487 * cleared by the current CPU are visible to the SMMU before the TLBI.
488 */
489 writel(cfg->asid, base + ARM_SMMU_CB_S1_TLBIASID);
11febfca
RM
490 arm_smmu_tlb_sync_context(cookie);
491}
1463fe44 492
11febfca
RM
493static void arm_smmu_tlb_inv_context_s2(void *cookie)
494{
495 struct arm_smmu_domain *smmu_domain = cookie;
496 struct arm_smmu_device *smmu = smmu_domain->smmu;
497 void __iomem *base = ARM_SMMU_GR0(smmu);
1463fe44 498
44f6876a
RM
499 /* NOTE: see above */
500 writel(smmu_domain->cfg.vmid, base + ARM_SMMU_GR0_TLBIVMID);
11febfca 501 arm_smmu_tlb_sync_global(smmu);
518f7136
WD
502}
503
504static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size,
06c610e8 505 size_t granule, bool leaf, void *cookie)
518f7136
WD
506{
507 struct arm_smmu_domain *smmu_domain = cookie;
508 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
518f7136 509 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
11febfca 510 void __iomem *reg = ARM_SMMU_CB(smmu_domain->smmu, cfg->cbndx);
518f7136 511
7d321bd3
WD
512 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
513 wmb();
514
518f7136 515 if (stage1) {
518f7136
WD
516 reg += leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
517
7602b871 518 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
518f7136 519 iova &= ~12UL;
280b683c 520 iova |= cfg->asid;
75df1386
RM
521 do {
522 writel_relaxed(iova, reg);
523 iova += granule;
524 } while (size -= granule);
518f7136
WD
525 } else {
526 iova >>= 12;
280b683c 527 iova |= (u64)cfg->asid << 48;
75df1386
RM
528 do {
529 writeq_relaxed(iova, reg);
530 iova += granule >> 12;
531 } while (size -= granule);
518f7136 532 }
11febfca 533 } else {
518f7136
WD
534 reg += leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L :
535 ARM_SMMU_CB_S2_TLBIIPAS2;
75df1386
RM
536 iova >>= 12;
537 do {
f9a05f05 538 smmu_write_atomic_lq(iova, reg);
75df1386
RM
539 iova += granule >> 12;
540 } while (size -= granule);
518f7136
WD
541 }
542}
543
11febfca
RM
544/*
545 * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
546 * almost negligible, but the benefit of getting the first one in as far ahead
547 * of the sync as possible is significant, hence we don't just make this a
548 * no-op and set .tlb_sync to arm_smmu_inv_context_s2() as you might think.
549 */
550static void arm_smmu_tlb_inv_vmid_nosync(unsigned long iova, size_t size,
551 size_t granule, bool leaf, void *cookie)
552{
553 struct arm_smmu_domain *smmu_domain = cookie;
554 void __iomem *base = ARM_SMMU_GR0(smmu_domain->smmu);
555
7d321bd3
WD
556 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
557 wmb();
558
11febfca
RM
559 writel_relaxed(smmu_domain->cfg.vmid, base + ARM_SMMU_GR0_TLBIVMID);
560}
561
562static const struct iommu_gather_ops arm_smmu_s1_tlb_ops = {
563 .tlb_flush_all = arm_smmu_tlb_inv_context_s1,
518f7136 564 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
11febfca
RM
565 .tlb_sync = arm_smmu_tlb_sync_context,
566};
567
568static const struct iommu_gather_ops arm_smmu_s2_tlb_ops_v2 = {
569 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
518f7136 570 .tlb_add_flush = arm_smmu_tlb_inv_range_nosync,
11febfca
RM
571 .tlb_sync = arm_smmu_tlb_sync_context,
572};
573
574static const struct iommu_gather_ops arm_smmu_s2_tlb_ops_v1 = {
575 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
576 .tlb_add_flush = arm_smmu_tlb_inv_vmid_nosync,
577 .tlb_sync = arm_smmu_tlb_sync_vmid,
518f7136
WD
578};
579
45ae7cff
WD
580static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
581{
bc580b56 582 u32 fsr, fsynr, cbfrsynra;
45ae7cff
WD
583 unsigned long iova;
584 struct iommu_domain *domain = dev;
1d672638 585 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed
WD
586 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
587 struct arm_smmu_device *smmu = smmu_domain->smmu;
bc580b56 588 void __iomem *gr1_base = ARM_SMMU_GR1(smmu);
45ae7cff
WD
589 void __iomem *cb_base;
590
452107c7 591 cb_base = ARM_SMMU_CB(smmu, cfg->cbndx);
45ae7cff
WD
592 fsr = readl_relaxed(cb_base + ARM_SMMU_CB_FSR);
593
594 if (!(fsr & FSR_FAULT))
595 return IRQ_NONE;
596
45ae7cff 597 fsynr = readl_relaxed(cb_base + ARM_SMMU_CB_FSYNR0);
f9a05f05 598 iova = readq_relaxed(cb_base + ARM_SMMU_CB_FAR);
bc580b56 599 cbfrsynra = readl_relaxed(gr1_base + ARM_SMMU_GR1_CBFRSYNRA(cfg->cbndx));
45ae7cff 600
3714ce1d 601 dev_err_ratelimited(smmu->dev,
bc580b56
VG
602 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
603 fsr, iova, fsynr, cbfrsynra, cfg->cbndx);
45ae7cff 604
3714ce1d
WD
605 writel(fsr, cb_base + ARM_SMMU_CB_FSR);
606 return IRQ_HANDLED;
45ae7cff
WD
607}
608
609static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
610{
611 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
612 struct arm_smmu_device *smmu = dev;
3a5df8ff 613 void __iomem *gr0_base = ARM_SMMU_GR0_NS(smmu);
45ae7cff
WD
614
615 gfsr = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSR);
616 gfsynr0 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR0);
617 gfsynr1 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR1);
618 gfsynr2 = readl_relaxed(gr0_base + ARM_SMMU_GR0_sGFSYNR2);
619
3a5df8ff
AH
620 if (!gfsr)
621 return IRQ_NONE;
622
45ae7cff
WD
623 dev_err_ratelimited(smmu->dev,
624 "Unexpected global fault, this could be serious\n");
625 dev_err_ratelimited(smmu->dev,
626 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
627 gfsr, gfsynr0, gfsynr1, gfsynr2);
628
629 writel(gfsr, gr0_base + ARM_SMMU_GR0_sGFSR);
adaba320 630 return IRQ_HANDLED;
45ae7cff
WD
631}
632
518f7136
WD
633static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
634 struct io_pgtable_cfg *pgtbl_cfg)
45ae7cff 635{
44680eed 636 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
90df373c
RM
637 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
638 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
639
640 cb->cfg = cfg;
641
642 /* TTBCR */
643 if (stage1) {
644 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
645 cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr;
646 } else {
647 cb->tcr[0] = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
648 cb->tcr[1] = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
649 cb->tcr[1] |= TTBCR2_SEP_UPSTREAM;
650 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
651 cb->tcr[1] |= TTBCR2_AS;
652 }
653 } else {
654 cb->tcr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
655 }
656
657 /* TTBRs */
658 if (stage1) {
659 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
660 cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr[0];
661 cb->ttbr[1] = pgtbl_cfg->arm_v7s_cfg.ttbr[1];
662 } else {
663 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
664 cb->ttbr[0] |= (u64)cfg->asid << TTBRn_ASID_SHIFT;
665 cb->ttbr[1] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
666 cb->ttbr[1] |= (u64)cfg->asid << TTBRn_ASID_SHIFT;
667 }
668 } else {
669 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
670 }
671
672 /* MAIRs (stage-1 only) */
673 if (stage1) {
674 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
675 cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr;
676 cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr;
677 } else {
678 cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
679 cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
680 }
681 }
682}
683
684static void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
685{
686 u32 reg;
687 bool stage1;
688 struct arm_smmu_cb *cb = &smmu->cbs[idx];
689 struct arm_smmu_cfg *cfg = cb->cfg;
c88ae5de 690 void __iomem *cb_base, *gr1_base;
45ae7cff 691
90df373c
RM
692 cb_base = ARM_SMMU_CB(smmu, idx);
693
694 /* Unassigned context banks only need disabling */
695 if (!cfg) {
696 writel_relaxed(0, cb_base + ARM_SMMU_CB_SCTLR);
697 return;
698 }
699
45ae7cff 700 gr1_base = ARM_SMMU_GR1(smmu);
44680eed 701 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
45ae7cff 702
90df373c 703 /* CBA2R */
4a1c93cb 704 if (smmu->version > ARM_SMMU_V1) {
7602b871
RM
705 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
706 reg = CBA2R_RW64_64BIT;
707 else
708 reg = CBA2R_RW64_32BIT;
4e3e9b69
TC
709 /* 16-bit VMIDs live in CBA2R */
710 if (smmu->features & ARM_SMMU_FEAT_VMID16)
280b683c 711 reg |= cfg->vmid << CBA2R_VMID_SHIFT;
4e3e9b69 712
90df373c 713 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBA2R(idx));
4a1c93cb
WD
714 }
715
45ae7cff 716 /* CBAR */
44680eed 717 reg = cfg->cbar;
b7862e35 718 if (smmu->version < ARM_SMMU_V2)
2907320d 719 reg |= cfg->irptndx << CBAR_IRPTNDX_SHIFT;
45ae7cff 720
57ca90f6
WD
721 /*
722 * Use the weakest shareability/memory types, so they are
723 * overridden by the ttbcr/pte.
724 */
725 if (stage1) {
726 reg |= (CBAR_S1_BPSHCFG_NSH << CBAR_S1_BPSHCFG_SHIFT) |
727 (CBAR_S1_MEMATTR_WB << CBAR_S1_MEMATTR_SHIFT);
4e3e9b69
TC
728 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
729 /* 8-bit VMIDs live in CBAR */
280b683c 730 reg |= cfg->vmid << CBAR_VMID_SHIFT;
57ca90f6 731 }
90df373c 732 writel_relaxed(reg, gr1_base + ARM_SMMU_GR1_CBAR(idx));
45ae7cff 733
125458ab
SG
734 /*
735 * TTBCR
736 * We must write this before the TTBRs, since it determines the
737 * access behaviour of some fields (in particular, ASID[15:8]).
738 */
90df373c
RM
739 if (stage1 && smmu->version > ARM_SMMU_V1)
740 writel_relaxed(cb->tcr[1], cb_base + ARM_SMMU_CB_TTBCR2);
741 writel_relaxed(cb->tcr[0], cb_base + ARM_SMMU_CB_TTBCR);
45ae7cff 742
518f7136 743 /* TTBRs */
90df373c
RM
744 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
745 writel_relaxed(cfg->asid, cb_base + ARM_SMMU_CB_CONTEXTIDR);
746 writel_relaxed(cb->ttbr[0], cb_base + ARM_SMMU_CB_TTBR0);
747 writel_relaxed(cb->ttbr[1], cb_base + ARM_SMMU_CB_TTBR1);
518f7136 748 } else {
90df373c
RM
749 writeq_relaxed(cb->ttbr[0], cb_base + ARM_SMMU_CB_TTBR0);
750 if (stage1)
751 writeq_relaxed(cb->ttbr[1], cb_base + ARM_SMMU_CB_TTBR1);
518f7136 752 }
a65217a4 753
518f7136 754 /* MAIRs (stage-1 only) */
45ae7cff 755 if (stage1) {
90df373c
RM
756 writel_relaxed(cb->mair[0], cb_base + ARM_SMMU_CB_S1_MAIR0);
757 writel_relaxed(cb->mair[1], cb_base + ARM_SMMU_CB_S1_MAIR1);
45ae7cff
WD
758 }
759
45ae7cff 760 /* SCTLR */
6070529b 761 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE | SCTLR_M;
45ae7cff
WD
762 if (stage1)
763 reg |= SCTLR_S1_ASIDPNE;
90df373c
RM
764 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
765 reg |= SCTLR_E;
766
25724841 767 writel_relaxed(reg, cb_base + ARM_SMMU_CB_SCTLR);
45ae7cff
WD
768}
769
770static int arm_smmu_init_domain_context(struct iommu_domain *domain,
44680eed 771 struct arm_smmu_device *smmu)
45ae7cff 772{
a18037b2 773 int irq, start, ret = 0;
518f7136
WD
774 unsigned long ias, oas;
775 struct io_pgtable_ops *pgtbl_ops;
776 struct io_pgtable_cfg pgtbl_cfg;
777 enum io_pgtable_fmt fmt;
1d672638 778 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed 779 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
45ae7cff 780
518f7136 781 mutex_lock(&smmu_domain->init_mutex);
a18037b2
MH
782 if (smmu_domain->smmu)
783 goto out_unlock;
784
61bc6711
WD
785 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
786 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
787 smmu_domain->smmu = smmu;
788 goto out_unlock;
789 }
790
c752ce45
WD
791 /*
792 * Mapping the requested stage onto what we support is surprisingly
793 * complicated, mainly because the spec allows S1+S2 SMMUs without
794 * support for nested translation. That means we end up with the
795 * following table:
796 *
797 * Requested Supported Actual
798 * S1 N S1
799 * S1 S1+S2 S1
800 * S1 S2 S2
801 * S1 S1 S1
802 * N N N
803 * N S1+S2 S2
804 * N S2 S2
805 * N S1 S1
806 *
807 * Note that you can't actually request stage-2 mappings.
808 */
809 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
810 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
811 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
812 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
813
7602b871
RM
814 /*
815 * Choosing a suitable context format is even more fiddly. Until we
816 * grow some way for the caller to express a preference, and/or move
817 * the decision into the io-pgtable code where it arguably belongs,
818 * just aim for the closest thing to the rest of the system, and hope
819 * that the hardware isn't esoteric enough that we can't assume AArch64
820 * support to be a superset of AArch32 support...
821 */
822 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
823 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
6070529b
RM
824 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
825 !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
826 (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
827 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
828 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
7602b871
RM
829 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
830 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
831 ARM_SMMU_FEAT_FMT_AARCH64_16K |
832 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
833 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
834
835 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
836 ret = -EINVAL;
837 goto out_unlock;
838 }
839
c752ce45
WD
840 switch (smmu_domain->stage) {
841 case ARM_SMMU_DOMAIN_S1:
842 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
843 start = smmu->num_s2_context_banks;
518f7136
WD
844 ias = smmu->va_size;
845 oas = smmu->ipa_size;
7602b871 846 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
518f7136 847 fmt = ARM_64_LPAE_S1;
6070529b 848 } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
518f7136 849 fmt = ARM_32_LPAE_S1;
7602b871
RM
850 ias = min(ias, 32UL);
851 oas = min(oas, 40UL);
6070529b
RM
852 } else {
853 fmt = ARM_V7S;
854 ias = min(ias, 32UL);
855 oas = min(oas, 32UL);
7602b871 856 }
32b12449 857 smmu_domain->tlb_ops = &arm_smmu_s1_tlb_ops;
c752ce45
WD
858 break;
859 case ARM_SMMU_DOMAIN_NESTED:
45ae7cff
WD
860 /*
861 * We will likely want to change this if/when KVM gets
862 * involved.
863 */
c752ce45 864 case ARM_SMMU_DOMAIN_S2:
9c5c92e3
WD
865 cfg->cbar = CBAR_TYPE_S2_TRANS;
866 start = 0;
518f7136
WD
867 ias = smmu->ipa_size;
868 oas = smmu->pa_size;
7602b871 869 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
518f7136 870 fmt = ARM_64_LPAE_S2;
7602b871 871 } else {
518f7136 872 fmt = ARM_32_LPAE_S2;
7602b871
RM
873 ias = min(ias, 40UL);
874 oas = min(oas, 40UL);
875 }
11febfca 876 if (smmu->version == ARM_SMMU_V2)
32b12449 877 smmu_domain->tlb_ops = &arm_smmu_s2_tlb_ops_v2;
11febfca 878 else
32b12449 879 smmu_domain->tlb_ops = &arm_smmu_s2_tlb_ops_v1;
c752ce45
WD
880 break;
881 default:
882 ret = -EINVAL;
883 goto out_unlock;
45ae7cff 884 }
45ae7cff
WD
885 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
886 smmu->num_context_banks);
287980e4 887 if (ret < 0)
a18037b2 888 goto out_unlock;
45ae7cff 889
44680eed 890 cfg->cbndx = ret;
b7862e35 891 if (smmu->version < ARM_SMMU_V2) {
44680eed
WD
892 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
893 cfg->irptndx %= smmu->num_context_irqs;
45ae7cff 894 } else {
44680eed 895 cfg->irptndx = cfg->cbndx;
45ae7cff
WD
896 }
897
280b683c
RM
898 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
899 cfg->vmid = cfg->cbndx + 1 + smmu->cavium_id_base;
900 else
901 cfg->asid = cfg->cbndx + smmu->cavium_id_base;
902
518f7136 903 pgtbl_cfg = (struct io_pgtable_cfg) {
d5466357 904 .pgsize_bitmap = smmu->pgsize_bitmap,
518f7136
WD
905 .ias = ias,
906 .oas = oas,
32b12449 907 .tlb = smmu_domain->tlb_ops,
2df7a25c 908 .iommu_dev = smmu->dev,
518f7136
WD
909 };
910
81b3c252
RM
911 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
912 pgtbl_cfg.quirks = IO_PGTABLE_QUIRK_NO_DMA;
913
44f6876a
RM
914 if (smmu_domain->non_strict)
915 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT;
916
518f7136
WD
917 smmu_domain->smmu = smmu;
918 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
919 if (!pgtbl_ops) {
920 ret = -ENOMEM;
921 goto out_clear_smmu;
922 }
923
d5466357
RM
924 /* Update the domain's page sizes to reflect the page table format */
925 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
455eb7d3
RM
926 domain->geometry.aperture_end = (1UL << ias) - 1;
927 domain->geometry.force_aperture = true;
a18037b2 928
518f7136
WD
929 /* Initialise the context bank with our page table cfg */
930 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
90df373c 931 arm_smmu_write_context_bank(smmu, cfg->cbndx);
518f7136
WD
932
933 /*
934 * Request context fault interrupt. Do this last to avoid the
935 * handler seeing a half-initialised domain state.
936 */
44680eed 937 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
bee14004
PF
938 ret = devm_request_irq(smmu->dev, irq, arm_smmu_context_fault,
939 IRQF_SHARED, "arm-smmu-context-fault", domain);
287980e4 940 if (ret < 0) {
45ae7cff 941 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
44680eed
WD
942 cfg->irptndx, irq);
943 cfg->irptndx = INVALID_IRPTNDX;
45ae7cff
WD
944 }
945
518f7136
WD
946 mutex_unlock(&smmu_domain->init_mutex);
947
948 /* Publish page table ops for map/unmap */
949 smmu_domain->pgtbl_ops = pgtbl_ops;
a9a1b0b5 950 return 0;
45ae7cff 951
518f7136
WD
952out_clear_smmu:
953 smmu_domain->smmu = NULL;
a18037b2 954out_unlock:
518f7136 955 mutex_unlock(&smmu_domain->init_mutex);
45ae7cff
WD
956 return ret;
957}
958
959static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
960{
1d672638 961 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
44680eed
WD
962 struct arm_smmu_device *smmu = smmu_domain->smmu;
963 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
d4a44f07 964 int ret, irq;
45ae7cff 965
61bc6711 966 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
45ae7cff
WD
967 return;
968
d4a44f07
SR
969 ret = arm_smmu_rpm_get(smmu);
970 if (ret < 0)
971 return;
972
518f7136
WD
973 /*
974 * Disable the context bank and free the page tables before freeing
975 * it.
976 */
90df373c
RM
977 smmu->cbs[cfg->cbndx].cfg = NULL;
978 arm_smmu_write_context_bank(smmu, cfg->cbndx);
1463fe44 979
44680eed
WD
980 if (cfg->irptndx != INVALID_IRPTNDX) {
981 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
bee14004 982 devm_free_irq(smmu->dev, irq, domain);
45ae7cff
WD
983 }
984
44830b0c 985 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
44680eed 986 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
d4a44f07
SR
987
988 arm_smmu_rpm_put(smmu);
45ae7cff
WD
989}
990
1d672638 991static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
45ae7cff
WD
992{
993 struct arm_smmu_domain *smmu_domain;
45ae7cff 994
61bc6711
WD
995 if (type != IOMMU_DOMAIN_UNMANAGED &&
996 type != IOMMU_DOMAIN_DMA &&
997 type != IOMMU_DOMAIN_IDENTITY)
1d672638 998 return NULL;
45ae7cff
WD
999 /*
1000 * Allocate the domain and initialise some of its data structures.
1001 * We can't really do anything meaningful until we've added a
1002 * master.
1003 */
1004 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
1005 if (!smmu_domain)
1d672638 1006 return NULL;
45ae7cff 1007
021bb842
RM
1008 if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
1009 iommu_get_dma_cookie(&smmu_domain->domain))) {
9adb9594
RM
1010 kfree(smmu_domain);
1011 return NULL;
1012 }
1013
518f7136 1014 mutex_init(&smmu_domain->init_mutex);
523d7423 1015 spin_lock_init(&smmu_domain->cb_lock);
1d672638
JR
1016
1017 return &smmu_domain->domain;
45ae7cff
WD
1018}
1019
1d672638 1020static void arm_smmu_domain_free(struct iommu_domain *domain)
45ae7cff 1021{
1d672638 1022 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1463fe44
WD
1023
1024 /*
1025 * Free the domain resources. We assume that all devices have
1026 * already been detached.
1027 */
9adb9594 1028 iommu_put_dma_cookie(domain);
45ae7cff 1029 arm_smmu_destroy_domain_context(domain);
45ae7cff
WD
1030 kfree(smmu_domain);
1031}
1032
1f3d5ca4
RM
1033static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
1034{
1035 struct arm_smmu_smr *smr = smmu->smrs + idx;
f80cd885 1036 u32 reg = smr->id << SMR_ID_SHIFT | smr->mask << SMR_MASK_SHIFT;
1f3d5ca4 1037
dc0eaa4e 1038 if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
1f3d5ca4
RM
1039 reg |= SMR_VALID;
1040 writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_SMR(idx));
1041}
1042
8e8b203e
RM
1043static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
1044{
1045 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
1046 u32 reg = (s2cr->type & S2CR_TYPE_MASK) << S2CR_TYPE_SHIFT |
1047 (s2cr->cbndx & S2CR_CBNDX_MASK) << S2CR_CBNDX_SHIFT |
1048 (s2cr->privcfg & S2CR_PRIVCFG_MASK) << S2CR_PRIVCFG_SHIFT;
1049
dc0eaa4e
AM
1050 if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
1051 smmu->smrs[idx].valid)
1052 reg |= S2CR_EXIDVALID;
8e8b203e
RM
1053 writel_relaxed(reg, ARM_SMMU_GR0(smmu) + ARM_SMMU_GR0_S2CR(idx));
1054}
1055
1056static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
1057{
1058 arm_smmu_write_s2cr(smmu, idx);
1059 if (smmu->smrs)
1060 arm_smmu_write_smr(smmu, idx);
1061}
1062
dc0eaa4e
AM
1063/*
1064 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
1065 * should be called after sCR0 is written.
1066 */
1067static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
1068{
1069 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1070 u32 smr;
1071
1072 if (!smmu->smrs)
1073 return;
1074
1075 /*
1076 * SMR.ID bits may not be preserved if the corresponding MASK
1077 * bits are set, so check each one separately. We can reject
1078 * masters later if they try to claim IDs outside these masks.
1079 */
1080 smr = smmu->streamid_mask << SMR_ID_SHIFT;
1081 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1082 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1083 smmu->streamid_mask = smr >> SMR_ID_SHIFT;
1084
1085 smr = smmu->streamid_mask << SMR_MASK_SHIFT;
1086 writel_relaxed(smr, gr0_base + ARM_SMMU_GR0_SMR(0));
1087 smr = readl_relaxed(gr0_base + ARM_SMMU_GR0_SMR(0));
1088 smmu->smr_mask_mask = smr >> SMR_MASK_SHIFT;
1089}
1090
588888a7 1091static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
1f3d5ca4
RM
1092{
1093 struct arm_smmu_smr *smrs = smmu->smrs;
588888a7 1094 int i, free_idx = -ENOSPC;
1f3d5ca4 1095
588888a7
RM
1096 /* Stream indexing is blissfully easy */
1097 if (!smrs)
1098 return id;
1099
1100 /* Validating SMRs is... less so */
1101 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1102 if (!smrs[i].valid) {
1103 /*
1104 * Note the first free entry we come across, which
1105 * we'll claim in the end if nothing else matches.
1106 */
1107 if (free_idx < 0)
1108 free_idx = i;
1f3d5ca4
RM
1109 continue;
1110 }
588888a7
RM
1111 /*
1112 * If the new entry is _entirely_ matched by an existing entry,
1113 * then reuse that, with the guarantee that there also cannot
1114 * be any subsequent conflicting entries. In normal use we'd
1115 * expect simply identical entries for this case, but there's
1116 * no harm in accommodating the generalisation.
1117 */
1118 if ((mask & smrs[i].mask) == mask &&
1119 !((id ^ smrs[i].id) & ~smrs[i].mask))
1120 return i;
1121 /*
1122 * If the new entry has any other overlap with an existing one,
1123 * though, then there always exists at least one stream ID
1124 * which would cause a conflict, and we can't allow that risk.
1125 */
1126 if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1127 return -EINVAL;
1128 }
1f3d5ca4 1129
588888a7
RM
1130 return free_idx;
1131}
1132
1133static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1134{
1135 if (--smmu->s2crs[idx].count)
1136 return false;
1137
1138 smmu->s2crs[idx] = s2cr_init_val;
1139 if (smmu->smrs)
1140 smmu->smrs[idx].valid = false;
1141
1142 return true;
1143}
1144
1145static int arm_smmu_master_alloc_smes(struct device *dev)
1146{
9b468f7d 1147 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
adfec2e7 1148 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
588888a7
RM
1149 struct arm_smmu_device *smmu = cfg->smmu;
1150 struct arm_smmu_smr *smrs = smmu->smrs;
1151 struct iommu_group *group;
1152 int i, idx, ret;
1153
1154 mutex_lock(&smmu->stream_map_mutex);
1155 /* Figure out a viable stream map entry allocation */
adfec2e7 1156 for_each_cfg_sme(fwspec, i, idx) {
021bb842
RM
1157 u16 sid = fwspec->ids[i];
1158 u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
1159
588888a7
RM
1160 if (idx != INVALID_SMENDX) {
1161 ret = -EEXIST;
1162 goto out_err;
45ae7cff
WD
1163 }
1164
021bb842 1165 ret = arm_smmu_find_sme(smmu, sid, mask);
588888a7
RM
1166 if (ret < 0)
1167 goto out_err;
1168
1169 idx = ret;
1170 if (smrs && smmu->s2crs[idx].count == 0) {
021bb842
RM
1171 smrs[idx].id = sid;
1172 smrs[idx].mask = mask;
588888a7
RM
1173 smrs[idx].valid = true;
1174 }
1175 smmu->s2crs[idx].count++;
1176 cfg->smendx[i] = (s16)idx;
45ae7cff
WD
1177 }
1178
588888a7
RM
1179 group = iommu_group_get_for_dev(dev);
1180 if (!group)
1181 group = ERR_PTR(-ENOMEM);
1182 if (IS_ERR(group)) {
1183 ret = PTR_ERR(group);
1184 goto out_err;
1185 }
1186 iommu_group_put(group);
1f3d5ca4 1187
45ae7cff 1188 /* It worked! Now, poke the actual hardware */
adfec2e7 1189 for_each_cfg_sme(fwspec, i, idx) {
588888a7
RM
1190 arm_smmu_write_sme(smmu, idx);
1191 smmu->s2crs[idx].group = group;
1192 }
45ae7cff 1193
588888a7 1194 mutex_unlock(&smmu->stream_map_mutex);
45ae7cff
WD
1195 return 0;
1196
588888a7 1197out_err:
1f3d5ca4 1198 while (i--) {
588888a7 1199 arm_smmu_free_sme(smmu, cfg->smendx[i]);
1f3d5ca4
RM
1200 cfg->smendx[i] = INVALID_SMENDX;
1201 }
588888a7
RM
1202 mutex_unlock(&smmu->stream_map_mutex);
1203 return ret;
45ae7cff
WD
1204}
1205
adfec2e7 1206static void arm_smmu_master_free_smes(struct iommu_fwspec *fwspec)
45ae7cff 1207{
adfec2e7
RM
1208 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
1209 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
d3097e39 1210 int i, idx;
43b412be 1211
588888a7 1212 mutex_lock(&smmu->stream_map_mutex);
adfec2e7 1213 for_each_cfg_sme(fwspec, i, idx) {
588888a7
RM
1214 if (arm_smmu_free_sme(smmu, idx))
1215 arm_smmu_write_sme(smmu, idx);
1f3d5ca4 1216 cfg->smendx[i] = INVALID_SMENDX;
45ae7cff 1217 }
588888a7 1218 mutex_unlock(&smmu->stream_map_mutex);
45ae7cff
WD
1219}
1220
45ae7cff 1221static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
adfec2e7 1222 struct iommu_fwspec *fwspec)
45ae7cff 1223{
44680eed 1224 struct arm_smmu_device *smmu = smmu_domain->smmu;
8e8b203e 1225 struct arm_smmu_s2cr *s2cr = smmu->s2crs;
8e8b203e 1226 u8 cbndx = smmu_domain->cfg.cbndx;
61bc6711 1227 enum arm_smmu_s2cr_type type;
588888a7 1228 int i, idx;
45ae7cff 1229
61bc6711
WD
1230 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1231 type = S2CR_TYPE_BYPASS;
1232 else
1233 type = S2CR_TYPE_TRANS;
1234
adfec2e7 1235 for_each_cfg_sme(fwspec, i, idx) {
8e8b203e 1236 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
588888a7 1237 continue;
1f3d5ca4 1238
8e8b203e 1239 s2cr[idx].type = type;
e1989807 1240 s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
8e8b203e
RM
1241 s2cr[idx].cbndx = cbndx;
1242 arm_smmu_write_s2cr(smmu, idx);
43b412be 1243 }
8e8b203e 1244 return 0;
bc7f2ce0
WD
1245}
1246
45ae7cff
WD
1247static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1248{
a18037b2 1249 int ret;
9b468f7d 1250 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
adfec2e7 1251 struct arm_smmu_device *smmu;
1d672638 1252 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
45ae7cff 1253
adfec2e7 1254 if (!fwspec || fwspec->ops != &arm_smmu_ops) {
45ae7cff
WD
1255 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1256 return -ENXIO;
1257 }
1258
fba4f8e5
RM
1259 /*
1260 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1261 * domains between of_xlate() and add_device() - we have no way to cope
1262 * with that, so until ARM gets converted to rely on groups and default
1263 * domains, just say no (but more politely than by dereferencing NULL).
1264 * This should be at least a WARN_ON once that's sorted.
1265 */
1266 if (!fwspec->iommu_priv)
1267 return -ENODEV;
1268
adfec2e7 1269 smmu = fwspec_smmu(fwspec);
d4a44f07
SR
1270
1271 ret = arm_smmu_rpm_get(smmu);
1272 if (ret < 0)
1273 return ret;
1274
518f7136 1275 /* Ensure that the domain is finalised */
adfec2e7 1276 ret = arm_smmu_init_domain_context(domain, smmu);
287980e4 1277 if (ret < 0)
d4a44f07 1278 goto rpm_put;
518f7136 1279
45ae7cff 1280 /*
44680eed
WD
1281 * Sanity check the domain. We don't support domains across
1282 * different SMMUs.
45ae7cff 1283 */
adfec2e7 1284 if (smmu_domain->smmu != smmu) {
45ae7cff
WD
1285 dev_err(dev,
1286 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
adfec2e7 1287 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
d4a44f07
SR
1288 ret = -EINVAL;
1289 goto rpm_put;
45ae7cff 1290 }
45ae7cff
WD
1291
1292 /* Looks ok, so add the device to the domain */
d4a44f07
SR
1293 ret = arm_smmu_domain_add_master(smmu_domain, fwspec);
1294
1295rpm_put:
1296 arm_smmu_rpm_put(smmu);
1297 return ret;
45ae7cff
WD
1298}
1299
45ae7cff 1300static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
b410aed9 1301 phys_addr_t paddr, size_t size, int prot)
45ae7cff 1302{
523d7423 1303 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
d4a44f07
SR
1304 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1305 int ret;
45ae7cff 1306
518f7136 1307 if (!ops)
45ae7cff
WD
1308 return -ENODEV;
1309
d4a44f07
SR
1310 arm_smmu_rpm_get(smmu);
1311 ret = ops->map(ops, iova, paddr, size, prot);
1312 arm_smmu_rpm_put(smmu);
1313
1314 return ret;
45ae7cff
WD
1315}
1316
1317static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1318 size_t size)
1319{
523d7423 1320 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
d4a44f07
SR
1321 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1322 size_t ret;
45ae7cff 1323
518f7136
WD
1324 if (!ops)
1325 return 0;
1326
d4a44f07
SR
1327 arm_smmu_rpm_get(smmu);
1328 ret = ops->unmap(ops, iova, size);
1329 arm_smmu_rpm_put(smmu);
1330
1331 return ret;
45ae7cff
WD
1332}
1333
44f6876a
RM
1334static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
1335{
1336 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
d4a44f07 1337 struct arm_smmu_device *smmu = smmu_domain->smmu;
44f6876a 1338
d4a44f07
SR
1339 if (smmu_domain->tlb_ops) {
1340 arm_smmu_rpm_get(smmu);
44f6876a 1341 smmu_domain->tlb_ops->tlb_flush_all(smmu_domain);
d4a44f07
SR
1342 arm_smmu_rpm_put(smmu);
1343 }
44f6876a
RM
1344}
1345
32b12449
RM
1346static void arm_smmu_iotlb_sync(struct iommu_domain *domain)
1347{
1348 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
d4a44f07 1349 struct arm_smmu_device *smmu = smmu_domain->smmu;
32b12449 1350
d4a44f07
SR
1351 if (smmu_domain->tlb_ops) {
1352 arm_smmu_rpm_get(smmu);
32b12449 1353 smmu_domain->tlb_ops->tlb_sync(smmu_domain);
d4a44f07
SR
1354 arm_smmu_rpm_put(smmu);
1355 }
32b12449
RM
1356}
1357
859a732e
MH
1358static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1359 dma_addr_t iova)
1360{
1d672638 1361 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
859a732e
MH
1362 struct arm_smmu_device *smmu = smmu_domain->smmu;
1363 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1364 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1365 struct device *dev = smmu->dev;
1366 void __iomem *cb_base;
1367 u32 tmp;
1368 u64 phys;
523d7423 1369 unsigned long va, flags;
d4a44f07
SR
1370 int ret;
1371
1372 ret = arm_smmu_rpm_get(smmu);
1373 if (ret < 0)
1374 return 0;
859a732e 1375
452107c7 1376 cb_base = ARM_SMMU_CB(smmu, cfg->cbndx);
859a732e 1377
523d7423 1378 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
661d962f
RM
1379 /* ATS1 registers can only be written atomically */
1380 va = iova & ~0xfffUL;
661d962f 1381 if (smmu->version == ARM_SMMU_V2)
f9a05f05
RM
1382 smmu_write_atomic_lq(va, cb_base + ARM_SMMU_CB_ATS1PR);
1383 else /* Register is only 32-bit in v1 */
661d962f 1384 writel_relaxed(va, cb_base + ARM_SMMU_CB_ATS1PR);
859a732e
MH
1385
1386 if (readl_poll_timeout_atomic(cb_base + ARM_SMMU_CB_ATSR, tmp,
1387 !(tmp & ATSR_ACTIVE), 5, 50)) {
523d7423 1388 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
859a732e 1389 dev_err(dev,
077124c9 1390 "iova to phys timed out on %pad. Falling back to software table walk.\n",
859a732e
MH
1391 &iova);
1392 return ops->iova_to_phys(ops, iova);
1393 }
1394
f9a05f05 1395 phys = readq_relaxed(cb_base + ARM_SMMU_CB_PAR);
523d7423 1396 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
859a732e
MH
1397 if (phys & CB_PAR_F) {
1398 dev_err(dev, "translation fault!\n");
1399 dev_err(dev, "PAR = 0x%llx\n", phys);
1400 return 0;
1401 }
1402
d4a44f07
SR
1403 arm_smmu_rpm_put(smmu);
1404
859a732e
MH
1405 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1406}
1407
45ae7cff 1408static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
859a732e 1409 dma_addr_t iova)
45ae7cff 1410{
1d672638 1411 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
523d7423 1412 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
45ae7cff 1413
bdf95923
SG
1414 if (domain->type == IOMMU_DOMAIN_IDENTITY)
1415 return iova;
1416
518f7136 1417 if (!ops)
a44a9791 1418 return 0;
45ae7cff 1419
83a60ed8 1420 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
523d7423
RM
1421 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1422 return arm_smmu_iova_to_phys_hard(domain, iova);
859a732e 1423
523d7423 1424 return ops->iova_to_phys(ops, iova);
45ae7cff
WD
1425}
1426
1fd0c775 1427static bool arm_smmu_capable(enum iommu_cap cap)
45ae7cff 1428{
d0948945
WD
1429 switch (cap) {
1430 case IOMMU_CAP_CACHE_COHERENCY:
1fd0c775
JR
1431 /*
1432 * Return true here as the SMMU can always send out coherent
1433 * requests.
1434 */
1435 return true;
0029a8dd
AM
1436 case IOMMU_CAP_NOEXEC:
1437 return true;
d0948945 1438 default:
1fd0c775 1439 return false;
d0948945 1440 }
45ae7cff 1441}
45ae7cff 1442
021bb842
RM
1443static int arm_smmu_match_node(struct device *dev, void *data)
1444{
ce9babe5 1445 return dev->fwnode == data;
021bb842
RM
1446}
1447
ce9babe5
LP
1448static
1449struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
021bb842
RM
1450{
1451 struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
ce9babe5 1452 fwnode, arm_smmu_match_node);
021bb842
RM
1453 put_device(dev);
1454 return dev ? dev_get_drvdata(dev) : NULL;
1455}
1456
f80cd885 1457static int arm_smmu_add_device(struct device *dev)
45ae7cff 1458{
adfec2e7 1459 struct arm_smmu_device *smmu;
03edb226 1460 struct arm_smmu_master_cfg *cfg;
9b468f7d 1461 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
f80cd885 1462 int i, ret;
8f68f8e2 1463
021bb842
RM
1464 if (using_legacy_binding) {
1465 ret = arm_smmu_register_legacy_master(dev, &smmu);
a7990c64
AS
1466
1467 /*
1468 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1469 * will allocate/initialise a new one. Thus we need to update fwspec for
1470 * later use.
1471 */
9b468f7d 1472 fwspec = dev_iommu_fwspec_get(dev);
021bb842
RM
1473 if (ret)
1474 goto out_free;
3c117b54 1475 } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
ce9babe5 1476 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
021bb842
RM
1477 } else {
1478 return -ENODEV;
1479 }
a9a1b0b5 1480
f80cd885 1481 ret = -EINVAL;
adfec2e7
RM
1482 for (i = 0; i < fwspec->num_ids; i++) {
1483 u16 sid = fwspec->ids[i];
021bb842 1484 u16 mask = fwspec->ids[i] >> SMR_MASK_SHIFT;
03edb226 1485
adfec2e7 1486 if (sid & ~smmu->streamid_mask) {
f80cd885 1487 dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
021bb842
RM
1488 sid, smmu->streamid_mask);
1489 goto out_free;
1490 }
1491 if (mask & ~smmu->smr_mask_mask) {
1492 dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
6323f474 1493 mask, smmu->smr_mask_mask);
f80cd885
RM
1494 goto out_free;
1495 }
1f3d5ca4 1496 }
5fc63a7c 1497
adfec2e7
RM
1498 ret = -ENOMEM;
1499 cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1500 GFP_KERNEL);
1501 if (!cfg)
1502 goto out_free;
1503
1504 cfg->smmu = smmu;
1505 fwspec->iommu_priv = cfg;
1506 while (i--)
1507 cfg->smendx[i] = INVALID_SMENDX;
1508
d4a44f07
SR
1509 ret = arm_smmu_rpm_get(smmu);
1510 if (ret < 0)
1511 goto out_cfg_free;
1512
588888a7 1513 ret = arm_smmu_master_alloc_smes(dev);
d4a44f07
SR
1514 arm_smmu_rpm_put(smmu);
1515
adfec2e7 1516 if (ret)
c54451a5 1517 goto out_cfg_free;
adfec2e7 1518
9648cbc9
JR
1519 iommu_device_link(&smmu->iommu, dev);
1520
655e3643
SR
1521 device_link_add(dev, smmu->dev,
1522 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
1523
adfec2e7 1524 return 0;
f80cd885 1525
c54451a5
VG
1526out_cfg_free:
1527 kfree(cfg);
f80cd885 1528out_free:
adfec2e7 1529 iommu_fwspec_free(dev);
f80cd885 1530 return ret;
03edb226
WD
1531}
1532
45ae7cff
WD
1533static void arm_smmu_remove_device(struct device *dev)
1534{
9b468f7d 1535 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
9648cbc9
JR
1536 struct arm_smmu_master_cfg *cfg;
1537 struct arm_smmu_device *smmu;
d4a44f07 1538 int ret;
8e8b203e 1539
adfec2e7 1540 if (!fwspec || fwspec->ops != &arm_smmu_ops)
f80cd885 1541 return;
8e8b203e 1542
9648cbc9
JR
1543 cfg = fwspec->iommu_priv;
1544 smmu = cfg->smmu;
1545
d4a44f07
SR
1546 ret = arm_smmu_rpm_get(smmu);
1547 if (ret < 0)
1548 return;
1549
9648cbc9 1550 iommu_device_unlink(&smmu->iommu, dev);
adfec2e7 1551 arm_smmu_master_free_smes(fwspec);
d4a44f07
SR
1552
1553 arm_smmu_rpm_put(smmu);
1554
5fc63a7c 1555 iommu_group_remove_device(dev);
adfec2e7
RM
1556 kfree(fwspec->iommu_priv);
1557 iommu_fwspec_free(dev);
45ae7cff
WD
1558}
1559
af659932
JR
1560static struct iommu_group *arm_smmu_device_group(struct device *dev)
1561{
9b468f7d 1562 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
adfec2e7 1563 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
588888a7
RM
1564 struct iommu_group *group = NULL;
1565 int i, idx;
1566
adfec2e7 1567 for_each_cfg_sme(fwspec, i, idx) {
588888a7
RM
1568 if (group && smmu->s2crs[idx].group &&
1569 group != smmu->s2crs[idx].group)
1570 return ERR_PTR(-EINVAL);
1571
1572 group = smmu->s2crs[idx].group;
1573 }
1574
1575 if (group)
e1b44cbe 1576 return iommu_group_ref_get(group);
af659932
JR
1577
1578 if (dev_is_pci(dev))
1579 group = pci_device_group(dev);
eab03e2a
NG
1580 else if (dev_is_fsl_mc(dev))
1581 group = fsl_mc_device_group(dev);
af659932
JR
1582 else
1583 group = generic_device_group(dev);
1584
af659932
JR
1585 return group;
1586}
1587
c752ce45
WD
1588static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1589 enum iommu_attr attr, void *data)
1590{
1d672638 1591 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
c752ce45 1592
44f6876a
RM
1593 switch(domain->type) {
1594 case IOMMU_DOMAIN_UNMANAGED:
1595 switch (attr) {
1596 case DOMAIN_ATTR_NESTING:
1597 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1598 return 0;
1599 default:
1600 return -ENODEV;
1601 }
1602 break;
1603 case IOMMU_DOMAIN_DMA:
1604 switch (attr) {
1605 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1606 *(int *)data = smmu_domain->non_strict;
1607 return 0;
1608 default:
1609 return -ENODEV;
1610 }
1611 break;
c752ce45 1612 default:
44f6876a 1613 return -EINVAL;
c752ce45
WD
1614 }
1615}
1616
1617static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1618 enum iommu_attr attr, void *data)
1619{
518f7136 1620 int ret = 0;
1d672638 1621 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
c752ce45 1622
518f7136
WD
1623 mutex_lock(&smmu_domain->init_mutex);
1624
44f6876a
RM
1625 switch(domain->type) {
1626 case IOMMU_DOMAIN_UNMANAGED:
1627 switch (attr) {
1628 case DOMAIN_ATTR_NESTING:
1629 if (smmu_domain->smmu) {
1630 ret = -EPERM;
1631 goto out_unlock;
1632 }
1633
1634 if (*(int *)data)
1635 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1636 else
1637 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1638 break;
1639 default:
1640 ret = -ENODEV;
1641 }
1642 break;
1643 case IOMMU_DOMAIN_DMA:
1644 switch (attr) {
1645 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1646 smmu_domain->non_strict = *(int *)data;
1647 break;
1648 default:
1649 ret = -ENODEV;
518f7136 1650 }
518f7136 1651 break;
c752ce45 1652 default:
44f6876a 1653 ret = -EINVAL;
c752ce45 1654 }
518f7136
WD
1655out_unlock:
1656 mutex_unlock(&smmu_domain->init_mutex);
1657 return ret;
c752ce45
WD
1658}
1659
021bb842
RM
1660static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1661{
56fbf600 1662 u32 mask, fwid = 0;
021bb842
RM
1663
1664 if (args->args_count > 0)
1665 fwid |= (u16)args->args[0];
1666
1667 if (args->args_count > 1)
1668 fwid |= (u16)args->args[1] << SMR_MASK_SHIFT;
56fbf600
RM
1669 else if (!of_property_read_u32(args->np, "stream-match-mask", &mask))
1670 fwid |= (u16)mask << SMR_MASK_SHIFT;
021bb842
RM
1671
1672 return iommu_fwspec_add_ids(dev, &fwid, 1);
1673}
1674
f3ebee80
EA
1675static void arm_smmu_get_resv_regions(struct device *dev,
1676 struct list_head *head)
1677{
1678 struct iommu_resv_region *region;
1679 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1680
1681 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
9d3a4de4 1682 prot, IOMMU_RESV_SW_MSI);
f3ebee80
EA
1683 if (!region)
1684 return;
1685
1686 list_add_tail(&region->list, head);
273df963
RM
1687
1688 iommu_dma_get_resv_regions(dev, head);
f3ebee80
EA
1689}
1690
1691static void arm_smmu_put_resv_regions(struct device *dev,
1692 struct list_head *head)
1693{
1694 struct iommu_resv_region *entry, *next;
1695
1696 list_for_each_entry_safe(entry, next, head, list)
1697 kfree(entry);
1698}
1699
518f7136 1700static struct iommu_ops arm_smmu_ops = {
c752ce45 1701 .capable = arm_smmu_capable,
1d672638
JR
1702 .domain_alloc = arm_smmu_domain_alloc,
1703 .domain_free = arm_smmu_domain_free,
c752ce45 1704 .attach_dev = arm_smmu_attach_dev,
c752ce45
WD
1705 .map = arm_smmu_map,
1706 .unmap = arm_smmu_unmap,
44f6876a 1707 .flush_iotlb_all = arm_smmu_flush_iotlb_all,
32b12449 1708 .iotlb_sync = arm_smmu_iotlb_sync,
c752ce45
WD
1709 .iova_to_phys = arm_smmu_iova_to_phys,
1710 .add_device = arm_smmu_add_device,
1711 .remove_device = arm_smmu_remove_device,
af659932 1712 .device_group = arm_smmu_device_group,
c752ce45
WD
1713 .domain_get_attr = arm_smmu_domain_get_attr,
1714 .domain_set_attr = arm_smmu_domain_set_attr,
021bb842 1715 .of_xlate = arm_smmu_of_xlate,
f3ebee80
EA
1716 .get_resv_regions = arm_smmu_get_resv_regions,
1717 .put_resv_regions = arm_smmu_put_resv_regions,
518f7136 1718 .pgsize_bitmap = -1UL, /* Restricted during device attach */
45ae7cff
WD
1719};
1720
1721static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1722{
1723 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1f3d5ca4 1724 int i;
3ca3712a 1725 u32 reg, major;
659db6f6 1726
3a5df8ff
AH
1727 /* clear global FSR */
1728 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
1729 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sGFSR);
45ae7cff 1730
1f3d5ca4
RM
1731 /*
1732 * Reset stream mapping groups: Initial values mark all SMRn as
1733 * invalid and all S2CRn as bypass unless overridden.
1734 */
8e8b203e
RM
1735 for (i = 0; i < smmu->num_mapping_groups; ++i)
1736 arm_smmu_write_sme(smmu, i);
45ae7cff 1737
6eb18d4a
NG
1738 if (smmu->model == ARM_MMU500) {
1739 /*
1740 * Before clearing ARM_MMU500_ACTLR_CPRE, need to
1741 * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
1742 * bit is only present in MMU-500r2 onwards.
1743 */
1744 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID7);
1745 major = (reg >> ID7_MAJOR_SHIFT) & ID7_MAJOR_MASK;
3ca3712a 1746 reg = readl_relaxed(gr0_base + ARM_SMMU_GR0_sACR);
6eb18d4a
NG
1747 if (major >= 2)
1748 reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
1749 /*
1750 * Allow unmatched Stream IDs to allocate bypass
1751 * TLB entries for reduced latency.
1752 */
74f55d34 1753 reg |= ARM_MMU500_ACR_SMTNMB_TLBEN | ARM_MMU500_ACR_S2CRB_TLBEN;
3ca3712a
PF
1754 writel_relaxed(reg, gr0_base + ARM_SMMU_GR0_sACR);
1755 }
1756
659db6f6
AH
1757 /* Make sure all context banks are disabled and clear CB_FSR */
1758 for (i = 0; i < smmu->num_context_banks; ++i) {
90df373c
RM
1759 void __iomem *cb_base = ARM_SMMU_CB(smmu, i);
1760
1761 arm_smmu_write_context_bank(smmu, i);
659db6f6 1762 writel_relaxed(FSR_FAULT, cb_base + ARM_SMMU_CB_FSR);
f0cfffc4
RM
1763 /*
1764 * Disable MMU-500's not-particularly-beneficial next-page
1765 * prefetcher for the sake of errata #841119 and #826419.
1766 */
1767 if (smmu->model == ARM_MMU500) {
1768 reg = readl_relaxed(cb_base + ARM_SMMU_CB_ACTLR);
1769 reg &= ~ARM_MMU500_ACTLR_CPRE;
1770 writel_relaxed(reg, cb_base + ARM_SMMU_CB_ACTLR);
1771 }
659db6f6 1772 }
1463fe44 1773
45ae7cff 1774 /* Invalidate the TLB, just in case */
4e4abae3
RM
1775 writel_relaxed(QCOM_DUMMY_VAL, gr0_base + ARM_SMMU_GR0_TLBIALLH);
1776 writel_relaxed(QCOM_DUMMY_VAL, gr0_base + ARM_SMMU_GR0_TLBIALLNSNH);
45ae7cff 1777
3a5df8ff 1778 reg = readl_relaxed(ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
659db6f6 1779
45ae7cff 1780 /* Enable fault reporting */
659db6f6 1781 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
45ae7cff
WD
1782
1783 /* Disable TLB broadcasting. */
659db6f6 1784 reg |= (sCR0_VMIDPNE | sCR0_PTM);
45ae7cff 1785
25a1c96c
RM
1786 /* Enable client access, handling unmatched streams as appropriate */
1787 reg &= ~sCR0_CLIENTPD;
1788 if (disable_bypass)
1789 reg |= sCR0_USFCFG;
1790 else
1791 reg &= ~sCR0_USFCFG;
45ae7cff
WD
1792
1793 /* Disable forced broadcasting */
659db6f6 1794 reg &= ~sCR0_FB;
45ae7cff
WD
1795
1796 /* Don't upgrade barriers */
659db6f6 1797 reg &= ~(sCR0_BSU_MASK << sCR0_BSU_SHIFT);
45ae7cff 1798
4e3e9b69
TC
1799 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1800 reg |= sCR0_VMID16EN;
1801
dc0eaa4e
AM
1802 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
1803 reg |= sCR0_EXIDENABLE;
1804
45ae7cff 1805 /* Push the button */
11febfca 1806 arm_smmu_tlb_sync_global(smmu);
3a5df8ff 1807 writel(reg, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
45ae7cff
WD
1808}
1809
1810static int arm_smmu_id_size_to_bits(int size)
1811{
1812 switch (size) {
1813 case 0:
1814 return 32;
1815 case 1:
1816 return 36;
1817 case 2:
1818 return 40;
1819 case 3:
1820 return 42;
1821 case 4:
1822 return 44;
1823 case 5:
1824 default:
1825 return 48;
1826 }
1827}
1828
1829static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1830{
1831 unsigned long size;
1832 void __iomem *gr0_base = ARM_SMMU_GR0(smmu);
1833 u32 id;
bbb8a184 1834 bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
8e8b203e 1835 int i;
45ae7cff
WD
1836
1837 dev_notice(smmu->dev, "probing hardware configuration...\n");
b7862e35
RM
1838 dev_notice(smmu->dev, "SMMUv%d with:\n",
1839 smmu->version == ARM_SMMU_V2 ? 2 : 1);
45ae7cff
WD
1840
1841 /* ID0 */
1842 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID0);
4cf740b0
WD
1843
1844 /* Restrict available stages based on module parameter */
1845 if (force_stage == 1)
1846 id &= ~(ID0_S2TS | ID0_NTS);
1847 else if (force_stage == 2)
1848 id &= ~(ID0_S1TS | ID0_NTS);
1849
45ae7cff
WD
1850 if (id & ID0_S1TS) {
1851 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1852 dev_notice(smmu->dev, "\tstage 1 translation\n");
1853 }
1854
1855 if (id & ID0_S2TS) {
1856 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1857 dev_notice(smmu->dev, "\tstage 2 translation\n");
1858 }
1859
1860 if (id & ID0_NTS) {
1861 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1862 dev_notice(smmu->dev, "\tnested translation\n");
1863 }
1864
1865 if (!(smmu->features &
4cf740b0 1866 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
45ae7cff
WD
1867 dev_err(smmu->dev, "\tno translation support!\n");
1868 return -ENODEV;
1869 }
1870
b7862e35
RM
1871 if ((id & ID0_S1TS) &&
1872 ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
859a732e
MH
1873 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1874 dev_notice(smmu->dev, "\taddress translation ops\n");
1875 }
1876
bae2c2d4
RM
1877 /*
1878 * In order for DMA API calls to work properly, we must defer to what
bbb8a184 1879 * the FW says about coherency, regardless of what the hardware claims.
bae2c2d4
RM
1880 * Fortunately, this also opens up a workaround for systems where the
1881 * ID register value has ended up configured incorrectly.
1882 */
bae2c2d4 1883 cttw_reg = !!(id & ID0_CTTW);
bbb8a184 1884 if (cttw_fw || cttw_reg)
bae2c2d4 1885 dev_notice(smmu->dev, "\t%scoherent table walk\n",
bbb8a184
LP
1886 cttw_fw ? "" : "non-");
1887 if (cttw_fw != cttw_reg)
bae2c2d4 1888 dev_notice(smmu->dev,
bbb8a184 1889 "\t(IDR0.CTTW overridden by FW configuration)\n");
45ae7cff 1890
21174240 1891 /* Max. number of entries we have for stream matching/indexing */
dc0eaa4e
AM
1892 if (smmu->version == ARM_SMMU_V2 && id & ID0_EXIDS) {
1893 smmu->features |= ARM_SMMU_FEAT_EXIDS;
1894 size = 1 << 16;
1895 } else {
1896 size = 1 << ((id >> ID0_NUMSIDB_SHIFT) & ID0_NUMSIDB_MASK);
1897 }
21174240 1898 smmu->streamid_mask = size - 1;
45ae7cff 1899 if (id & ID0_SMS) {
45ae7cff 1900 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
21174240
RM
1901 size = (id >> ID0_NUMSMRG_SHIFT) & ID0_NUMSMRG_MASK;
1902 if (size == 0) {
45ae7cff
WD
1903 dev_err(smmu->dev,
1904 "stream-matching supported, but no SMRs present!\n");
1905 return -ENODEV;
1906 }
1907
1f3d5ca4
RM
1908 /* Zero-initialised to mark as invalid */
1909 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1910 GFP_KERNEL);
1911 if (!smmu->smrs)
1912 return -ENOMEM;
1913
45ae7cff 1914 dev_notice(smmu->dev,
dc0eaa4e 1915 "\tstream matching with %lu register groups", size);
45ae7cff 1916 }
8e8b203e
RM
1917 /* s2cr->type == 0 means translation, so initialise explicitly */
1918 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1919 GFP_KERNEL);
1920 if (!smmu->s2crs)
1921 return -ENOMEM;
1922 for (i = 0; i < size; i++)
1923 smmu->s2crs[i] = s2cr_init_val;
1924
21174240 1925 smmu->num_mapping_groups = size;
588888a7 1926 mutex_init(&smmu->stream_map_mutex);
8e517e76 1927 spin_lock_init(&smmu->global_sync_lock);
45ae7cff 1928
7602b871
RM
1929 if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
1930 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1931 if (!(id & ID0_PTFS_NO_AARCH32S))
1932 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1933 }
1934
45ae7cff
WD
1935 /* ID1 */
1936 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID1);
c757e852 1937 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
45ae7cff 1938
c55af7f7 1939 /* Check for size mismatch of SMMU address space from mapped region */
518f7136 1940 size = 1 << (((id >> ID1_NUMPAGENDXB_SHIFT) & ID1_NUMPAGENDXB_MASK) + 1);
452107c7
RM
1941 size <<= smmu->pgshift;
1942 if (smmu->cb_base != gr0_base + size)
2907320d 1943 dev_warn(smmu->dev,
452107c7
RM
1944 "SMMU address space size (0x%lx) differs from mapped region size (0x%tx)!\n",
1945 size * 2, (smmu->cb_base - gr0_base) * 2);
45ae7cff 1946
518f7136 1947 smmu->num_s2_context_banks = (id >> ID1_NUMS2CB_SHIFT) & ID1_NUMS2CB_MASK;
45ae7cff
WD
1948 smmu->num_context_banks = (id >> ID1_NUMCB_SHIFT) & ID1_NUMCB_MASK;
1949 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1950 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1951 return -ENODEV;
1952 }
1953 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1954 smmu->num_context_banks, smmu->num_s2_context_banks);
e086d912
RM
1955 /*
1956 * Cavium CN88xx erratum #27704.
1957 * Ensure ASID and VMID allocation is unique across all SMMUs in
1958 * the system.
1959 */
1960 if (smmu->model == CAVIUM_SMMUV2) {
1961 smmu->cavium_id_base =
1962 atomic_add_return(smmu->num_context_banks,
1963 &cavium_smmu_context_count);
1964 smmu->cavium_id_base -= smmu->num_context_banks;
53c35dce 1965 dev_notice(smmu->dev, "\tenabling workaround for Cavium erratum 27704\n");
e086d912 1966 }
90df373c
RM
1967 smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
1968 sizeof(*smmu->cbs), GFP_KERNEL);
1969 if (!smmu->cbs)
1970 return -ENOMEM;
45ae7cff
WD
1971
1972 /* ID2 */
1973 id = readl_relaxed(gr0_base + ARM_SMMU_GR0_ID2);
1974 size = arm_smmu_id_size_to_bits((id >> ID2_IAS_SHIFT) & ID2_IAS_MASK);
518f7136 1975 smmu->ipa_size = size;
45ae7cff 1976
518f7136 1977 /* The output mask is also applied for bypass */
45ae7cff 1978 size = arm_smmu_id_size_to_bits((id >> ID2_OAS_SHIFT) & ID2_OAS_MASK);
518f7136 1979 smmu->pa_size = size;
45ae7cff 1980
4e3e9b69
TC
1981 if (id & ID2_VMID16)
1982 smmu->features |= ARM_SMMU_FEAT_VMID16;
1983
f1d84548
RM
1984 /*
1985 * What the page table walker can address actually depends on which
1986 * descriptor format is in use, but since a) we don't know that yet,
1987 * and b) it can vary per context bank, this will have to do...
1988 */
1989 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1990 dev_warn(smmu->dev,
1991 "failed to set DMA mask for table walker\n");
1992
b7862e35 1993 if (smmu->version < ARM_SMMU_V2) {
518f7136 1994 smmu->va_size = smmu->ipa_size;
b7862e35
RM
1995 if (smmu->version == ARM_SMMU_V1_64K)
1996 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
45ae7cff 1997 } else {
45ae7cff 1998 size = (id >> ID2_UBS_SHIFT) & ID2_UBS_MASK;
518f7136 1999 smmu->va_size = arm_smmu_id_size_to_bits(size);
518f7136 2000 if (id & ID2_PTFS_4K)
7602b871 2001 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
518f7136 2002 if (id & ID2_PTFS_16K)
7602b871 2003 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
518f7136 2004 if (id & ID2_PTFS_64K)
7602b871 2005 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
45ae7cff
WD
2006 }
2007
7602b871 2008 /* Now we've corralled the various formats, what'll it do? */
7602b871 2009 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
d5466357 2010 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
7602b871
RM
2011 if (smmu->features &
2012 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
d5466357 2013 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
7602b871 2014 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
d5466357 2015 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
7602b871 2016 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
d5466357
RM
2017 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
2018
2019 if (arm_smmu_ops.pgsize_bitmap == -1UL)
2020 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
2021 else
2022 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
2023 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
2024 smmu->pgsize_bitmap);
7602b871 2025
518f7136 2026
28d6007b
WD
2027 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
2028 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
518f7136 2029 smmu->va_size, smmu->ipa_size);
28d6007b
WD
2030
2031 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
2032 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
518f7136 2033 smmu->ipa_size, smmu->pa_size);
28d6007b 2034
45ae7cff
WD
2035 return 0;
2036}
2037
67b65a3f
RM
2038struct arm_smmu_match_data {
2039 enum arm_smmu_arch_version version;
2040 enum arm_smmu_implementation model;
2041};
2042
2043#define ARM_SMMU_MATCH_DATA(name, ver, imp) \
96a299d2 2044static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
67b65a3f
RM
2045
2046ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
2047ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
b7862e35 2048ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
f0cfffc4 2049ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
e086d912 2050ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
89cddc56 2051ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2);
67b65a3f 2052
09b5269a 2053static const struct of_device_id arm_smmu_of_match[] = {
67b65a3f
RM
2054 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
2055 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
2056 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
b7862e35 2057 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
f0cfffc4 2058 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
e086d912 2059 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
89cddc56 2060 { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
09360403
RM
2061 { },
2062};
09360403 2063
d6fcd3b1
LP
2064#ifdef CONFIG_ACPI
2065static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
2066{
2067 int ret = 0;
2068
2069 switch (model) {
2070 case ACPI_IORT_SMMU_V1:
2071 case ACPI_IORT_SMMU_CORELINK_MMU400:
2072 smmu->version = ARM_SMMU_V1;
2073 smmu->model = GENERIC_SMMU;
2074 break;
84c24379
RM
2075 case ACPI_IORT_SMMU_CORELINK_MMU401:
2076 smmu->version = ARM_SMMU_V1_64K;
2077 smmu->model = GENERIC_SMMU;
2078 break;
d6fcd3b1
LP
2079 case ACPI_IORT_SMMU_V2:
2080 smmu->version = ARM_SMMU_V2;
2081 smmu->model = GENERIC_SMMU;
2082 break;
2083 case ACPI_IORT_SMMU_CORELINK_MMU500:
2084 smmu->version = ARM_SMMU_V2;
2085 smmu->model = ARM_MMU500;
2086 break;
84c24379
RM
2087 case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
2088 smmu->version = ARM_SMMU_V2;
2089 smmu->model = CAVIUM_SMMUV2;
2090 break;
d6fcd3b1
LP
2091 default:
2092 ret = -ENODEV;
2093 }
2094
2095 return ret;
2096}
2097
2098static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2099 struct arm_smmu_device *smmu)
2100{
2101 struct device *dev = smmu->dev;
2102 struct acpi_iort_node *node =
2103 *(struct acpi_iort_node **)dev_get_platdata(dev);
2104 struct acpi_iort_smmu *iort_smmu;
2105 int ret;
2106
2107 /* Retrieve SMMU1/2 specific data */
2108 iort_smmu = (struct acpi_iort_smmu *)node->node_data;
2109
2110 ret = acpi_smmu_get_data(iort_smmu->model, smmu);
2111 if (ret < 0)
2112 return ret;
2113
2114 /* Ignore the configuration access interrupt */
2115 smmu->num_global_irqs = 1;
2116
2117 if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
2118 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2119
2120 return 0;
2121}
2122#else
2123static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2124 struct arm_smmu_device *smmu)
2125{
2126 return -ENODEV;
2127}
2128#endif
2129
bbb8a184
LP
2130static int arm_smmu_device_dt_probe(struct platform_device *pdev,
2131 struct arm_smmu_device *smmu)
45ae7cff 2132{
67b65a3f 2133 const struct arm_smmu_match_data *data;
45ae7cff 2134 struct device *dev = &pdev->dev;
021bb842
RM
2135 bool legacy_binding;
2136
bbb8a184
LP
2137 if (of_property_read_u32(dev->of_node, "#global-interrupts",
2138 &smmu->num_global_irqs)) {
2139 dev_err(dev, "missing #global-interrupts property\n");
2140 return -ENODEV;
2141 }
2142
2143 data = of_device_get_match_data(dev);
2144 smmu->version = data->version;
2145 smmu->model = data->model;
2146
2147 parse_driver_options(smmu);
2148
021bb842
RM
2149 legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
2150 if (legacy_binding && !using_generic_binding) {
2151 if (!using_legacy_binding)
2152 pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
2153 using_legacy_binding = true;
2154 } else if (!legacy_binding && !using_legacy_binding) {
2155 using_generic_binding = true;
2156 } else {
2157 dev_err(dev, "not probing due to mismatched DT properties\n");
2158 return -ENODEV;
2159 }
45ae7cff 2160
bbb8a184
LP
2161 if (of_dma_is_coherent(dev->of_node))
2162 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2163
2164 return 0;
2165}
2166
f6810c15
RM
2167static void arm_smmu_bus_init(void)
2168{
2169 /* Oh, for a proper bus abstraction */
2170 if (!iommu_present(&platform_bus_type))
2171 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2172#ifdef CONFIG_ARM_AMBA
2173 if (!iommu_present(&amba_bustype))
2174 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2175#endif
2176#ifdef CONFIG_PCI
2177 if (!iommu_present(&pci_bus_type)) {
2178 pci_request_acs();
2179 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2180 }
2181#endif
eab03e2a
NG
2182#ifdef CONFIG_FSL_MC_BUS
2183 if (!iommu_present(&fsl_mc_bus_type))
2184 bus_set_iommu(&fsl_mc_bus_type, &arm_smmu_ops);
2185#endif
f6810c15
RM
2186}
2187
bbb8a184
LP
2188static int arm_smmu_device_probe(struct platform_device *pdev)
2189{
2190 struct resource *res;
9648cbc9 2191 resource_size_t ioaddr;
bbb8a184
LP
2192 struct arm_smmu_device *smmu;
2193 struct device *dev = &pdev->dev;
2194 int num_irqs, i, err;
2195
45ae7cff
WD
2196 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2197 if (!smmu) {
2198 dev_err(dev, "failed to allocate arm_smmu_device\n");
2199 return -ENOMEM;
2200 }
2201 smmu->dev = dev;
2202
d6fcd3b1
LP
2203 if (dev->of_node)
2204 err = arm_smmu_device_dt_probe(pdev, smmu);
2205 else
2206 err = arm_smmu_device_acpi_probe(pdev, smmu);
2207
bbb8a184
LP
2208 if (err)
2209 return err;
09360403 2210
45ae7cff 2211 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9648cbc9 2212 ioaddr = res->start;
8a7f4312
JL
2213 smmu->base = devm_ioremap_resource(dev, res);
2214 if (IS_ERR(smmu->base))
2215 return PTR_ERR(smmu->base);
452107c7 2216 smmu->cb_base = smmu->base + resource_size(res) / 2;
45ae7cff 2217
45ae7cff
WD
2218 num_irqs = 0;
2219 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2220 num_irqs++;
2221 if (num_irqs > smmu->num_global_irqs)
2222 smmu->num_context_irqs++;
2223 }
2224
44a08de2
AH
2225 if (!smmu->num_context_irqs) {
2226 dev_err(dev, "found %d interrupts but expected at least %d\n",
2227 num_irqs, smmu->num_global_irqs + 1);
2228 return -ENODEV;
45ae7cff 2229 }
45ae7cff 2230
a86854d0 2231 smmu->irqs = devm_kcalloc(dev, num_irqs, sizeof(*smmu->irqs),
45ae7cff
WD
2232 GFP_KERNEL);
2233 if (!smmu->irqs) {
2234 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2235 return -ENOMEM;
2236 }
2237
2238 for (i = 0; i < num_irqs; ++i) {
2239 int irq = platform_get_irq(pdev, i);
2907320d 2240
45ae7cff
WD
2241 if (irq < 0) {
2242 dev_err(dev, "failed to get irq index %d\n", i);
2243 return -ENODEV;
2244 }
2245 smmu->irqs[i] = irq;
2246 }
2247
96a299d2
SR
2248 err = devm_clk_bulk_get_all(dev, &smmu->clks);
2249 if (err < 0) {
2250 dev_err(dev, "failed to get clocks %d\n", err);
2251 return err;
2252 }
2253 smmu->num_clks = err;
2254
2255 err = clk_bulk_prepare_enable(smmu->num_clks, smmu->clks);
2256 if (err)
2257 return err;
2258
3c8766d0
OH
2259 err = arm_smmu_device_cfg_probe(smmu);
2260 if (err)
2261 return err;
2262
d1e20222
VG
2263 if (smmu->version == ARM_SMMU_V2) {
2264 if (smmu->num_context_banks > smmu->num_context_irqs) {
2265 dev_err(dev,
2266 "found only %d context irq(s) but %d required\n",
2267 smmu->num_context_irqs, smmu->num_context_banks);
2268 return -ENODEV;
2269 }
2270
2271 /* Ignore superfluous interrupts */
2272 smmu->num_context_irqs = smmu->num_context_banks;
45ae7cff
WD
2273 }
2274
45ae7cff 2275 for (i = 0; i < smmu->num_global_irqs; ++i) {
bee14004
PF
2276 err = devm_request_irq(smmu->dev, smmu->irqs[i],
2277 arm_smmu_global_fault,
2278 IRQF_SHARED,
2279 "arm-smmu global fault",
2280 smmu);
45ae7cff
WD
2281 if (err) {
2282 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2283 i, smmu->irqs[i]);
f80cd885 2284 return err;
45ae7cff
WD
2285 }
2286 }
2287
9648cbc9
JR
2288 err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2289 "smmu.%pa", &ioaddr);
2290 if (err) {
2291 dev_err(dev, "Failed to register iommu in sysfs\n");
2292 return err;
2293 }
2294
2295 iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2296 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2297
2298 err = iommu_device_register(&smmu->iommu);
2299 if (err) {
2300 dev_err(dev, "Failed to register iommu\n");
2301 return err;
2302 }
2303
d6fc5d97 2304 platform_set_drvdata(pdev, smmu);
fd90cecb 2305 arm_smmu_device_reset(smmu);
dc0eaa4e 2306 arm_smmu_test_smr_masks(smmu);
021bb842 2307
d4a44f07
SR
2308 /*
2309 * We want to avoid touching dev->power.lock in fastpaths unless
2310 * it's really going to do something useful - pm_runtime_enabled()
2311 * can serve as an ideal proxy for that decision. So, conditionally
2312 * enable pm_runtime.
2313 */
2314 if (dev->pm_domain) {
2315 pm_runtime_set_active(dev);
2316 pm_runtime_enable(dev);
2317 }
2318
f6810c15
RM
2319 /*
2320 * For ACPI and generic DT bindings, an SMMU will be probed before
2321 * any device which might need it, so we want the bus ops in place
2322 * ready to handle default domain setup as soon as any SMMU exists.
2323 */
2324 if (!using_legacy_binding)
2325 arm_smmu_bus_init();
2326
45ae7cff 2327 return 0;
45ae7cff
WD
2328}
2329
f6810c15
RM
2330/*
2331 * With the legacy DT binding in play, though, we have no guarantees about
2332 * probe order, but then we're also not doing default domains, so we can
2333 * delay setting bus ops until we're sure every possible SMMU is ready,
2334 * and that way ensure that no add_device() calls get missed.
2335 */
2336static int arm_smmu_legacy_bus_init(void)
2337{
2338 if (using_legacy_binding)
2339 arm_smmu_bus_init();
45ae7cff 2340 return 0;
45ae7cff 2341}
f6810c15 2342device_initcall_sync(arm_smmu_legacy_bus_init);
45ae7cff 2343
addb672f 2344static void arm_smmu_device_shutdown(struct platform_device *pdev)
45ae7cff 2345{
d6fc5d97 2346 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
45ae7cff
WD
2347
2348 if (!smmu)
addb672f 2349 return;
45ae7cff 2350
ecfadb6e 2351 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
d6fc5d97 2352 dev_err(&pdev->dev, "removing device with active domains!\n");
45ae7cff 2353
d4a44f07 2354 arm_smmu_rpm_get(smmu);
45ae7cff 2355 /* Turn the thing off */
2907320d 2356 writel(sCR0_CLIENTPD, ARM_SMMU_GR0_NS(smmu) + ARM_SMMU_GR0_sCR0);
d4a44f07
SR
2357 arm_smmu_rpm_put(smmu);
2358
2359 if (pm_runtime_enabled(smmu->dev))
2360 pm_runtime_force_suspend(smmu->dev);
2361 else
2362 clk_bulk_disable(smmu->num_clks, smmu->clks);
96a299d2 2363
d4a44f07 2364 clk_bulk_unprepare(smmu->num_clks, smmu->clks);
45ae7cff
WD
2365}
2366
96a299d2 2367static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
7aa8619a 2368{
a2d866f7 2369 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
96a299d2
SR
2370 int ret;
2371
2372 ret = clk_bulk_enable(smmu->num_clks, smmu->clks);
2373 if (ret)
2374 return ret;
a2d866f7
RM
2375
2376 arm_smmu_device_reset(smmu);
96a299d2 2377
a2d866f7 2378 return 0;
7aa8619a
NW
2379}
2380
96a299d2 2381static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
a2d866f7
RM
2382{
2383 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2384
96a299d2
SR
2385 clk_bulk_disable(smmu->num_clks, smmu->clks);
2386
a2d866f7
RM
2387 return 0;
2388}
2389
96a299d2
SR
2390static int __maybe_unused arm_smmu_pm_resume(struct device *dev)
2391{
2392 if (pm_runtime_suspended(dev))
2393 return 0;
2394
2395 return arm_smmu_runtime_resume(dev);
2396}
2397
2398static int __maybe_unused arm_smmu_pm_suspend(struct device *dev)
2399{
2400 if (pm_runtime_suspended(dev))
2401 return 0;
2402
2403 return arm_smmu_runtime_suspend(dev);
2404}
2405
2406static const struct dev_pm_ops arm_smmu_pm_ops = {
2407 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume)
2408 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend,
2409 arm_smmu_runtime_resume, NULL)
2410};
a2d866f7 2411
45ae7cff
WD
2412static struct platform_driver arm_smmu_driver = {
2413 .driver = {
addb672f
PG
2414 .name = "arm-smmu",
2415 .of_match_table = of_match_ptr(arm_smmu_of_match),
2416 .pm = &arm_smmu_pm_ops,
2417 .suppress_bind_attrs = true,
45ae7cff 2418 },
bbb8a184 2419 .probe = arm_smmu_device_probe,
7aa8619a 2420 .shutdown = arm_smmu_device_shutdown,
45ae7cff 2421};
addb672f 2422builtin_platform_driver(arm_smmu_driver);