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