1 // SPDX-License-Identifier: GPL-2.0-only
3 * IOMMU API for ARM architected SMMU implementations.
5 * Copyright (C) 2013 ARM Limited
7 * Author: Will Deacon <will.deacon@arm.com>
9 * This driver currently supports:
10 * - SMMUv1 and v2 implementations
11 * - Stream-matching and stream-indexing
12 * - v7/v8 long-descriptor format
13 * - Non-secure access to the SMMU
14 * - Context fault reporting
15 * - Extended Stream ID (16 bit)
18 #define pr_fmt(fmt) "arm-smmu: " fmt
20 #include <linux/acpi.h>
21 #include <linux/acpi_iort.h>
22 #include <linux/bitfield.h>
23 #include <linux/delay.h>
24 #include <linux/dma-iommu.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
29 #include <linux/io-64-nonatomic-hi-lo.h>
30 #include <linux/io-pgtable.h>
31 #include <linux/iopoll.h>
32 #include <linux/init.h>
33 #include <linux/moduleparam.h>
35 #include <linux/of_address.h>
36 #include <linux/of_device.h>
37 #include <linux/of_iommu.h>
38 #include <linux/pci.h>
39 #include <linux/platform_device.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/slab.h>
43 #include <linux/amba/bus.h>
44 #include <linux/fsl/mc.h>
49 * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU
50 * global register space are still, in fact, using a hypervisor to mediate it
51 * by trapping and emulating register accesses. Sadly, some deployed versions
52 * of said trapping code have bugs wherein they go horribly wrong for stores
53 * using r31 (i.e. XZR/WZR) as the source register.
55 #define QCOM_DUMMY_VAL -1
57 #define ARM_MMU500_ACTLR_CPRE (1 << 1)
59 #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
60 #define ARM_MMU500_ACR_S2CRB_TLBEN (1 << 10)
61 #define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8)
63 #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
64 #define TLB_SPIN_COUNT 10
66 #define MSI_IOVA_BASE 0x8000000
67 #define MSI_IOVA_LENGTH 0x100000
69 static int force_stage;
71 * not really modular, but the easiest way to keep compat with existing
72 * bootargs behaviour is to continue using module_param() here.
74 module_param(force_stage, int, S_IRUGO);
75 MODULE_PARM_DESC(force_stage,
76 "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.");
77 static bool disable_bypass =
78 IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT);
79 module_param(disable_bypass, bool, S_IRUGO);
80 MODULE_PARM_DESC(disable_bypass,
81 "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.");
83 struct arm_smmu_s2cr {
84 struct iommu_group *group;
86 enum arm_smmu_s2cr_type type;
87 enum arm_smmu_s2cr_privcfg privcfg;
91 #define s2cr_init_val (struct arm_smmu_s2cr){ \
92 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
105 struct arm_smmu_cfg *cfg;
108 struct arm_smmu_master_cfg {
109 struct arm_smmu_device *smmu;
112 #define INVALID_SMENDX -1
113 #define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
114 #define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
115 #define fwspec_smendx(fw, i) \
116 (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
117 #define for_each_cfg_sme(fw, i, idx) \
118 for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
120 enum arm_smmu_context_fmt {
121 ARM_SMMU_CTX_FMT_NONE,
122 ARM_SMMU_CTX_FMT_AARCH64,
123 ARM_SMMU_CTX_FMT_AARCH32_L,
124 ARM_SMMU_CTX_FMT_AARCH32_S,
127 struct arm_smmu_cfg {
134 enum arm_smmu_cbar_type cbar;
135 enum arm_smmu_context_fmt fmt;
137 #define INVALID_IRPTNDX 0xff
139 enum arm_smmu_domain_stage {
140 ARM_SMMU_DOMAIN_S1 = 0,
142 ARM_SMMU_DOMAIN_NESTED,
143 ARM_SMMU_DOMAIN_BYPASS,
146 struct arm_smmu_domain {
147 struct arm_smmu_device *smmu;
148 struct io_pgtable_ops *pgtbl_ops;
149 const struct iommu_gather_ops *tlb_ops;
150 struct arm_smmu_cfg cfg;
151 enum arm_smmu_domain_stage stage;
153 struct mutex init_mutex; /* Protects smmu pointer */
154 spinlock_t cb_lock; /* Serialises ATS1* ops and TLB syncs */
155 struct iommu_domain domain;
158 static int arm_smmu_gr0_ns(int offset)
161 case ARM_SMMU_GR0_sCR0:
162 case ARM_SMMU_GR0_sACR:
163 case ARM_SMMU_GR0_sGFSR:
164 case ARM_SMMU_GR0_sGFSYNR0:
165 case ARM_SMMU_GR0_sGFSYNR1:
166 case ARM_SMMU_GR0_sGFSYNR2:
167 return offset + 0x400;
173 static void __iomem *arm_smmu_page(struct arm_smmu_device *smmu, int n)
175 return smmu->base + (n << smmu->pgshift);
178 static u32 arm_smmu_readl(struct arm_smmu_device *smmu, int page, int offset)
180 if ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) && page == 0)
181 offset = arm_smmu_gr0_ns(offset);
183 return readl_relaxed(arm_smmu_page(smmu, page) + offset);
186 static void arm_smmu_writel(struct arm_smmu_device *smmu, int page, int offset,
189 if ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) && page == 0)
190 offset = arm_smmu_gr0_ns(offset);
192 writel_relaxed(val, arm_smmu_page(smmu, page) + offset);
195 static u64 arm_smmu_readq(struct arm_smmu_device *smmu, int page, int offset)
197 return readq_relaxed(arm_smmu_page(smmu, page) + offset);
200 static void arm_smmu_writeq(struct arm_smmu_device *smmu, int page, int offset,
203 writeq_relaxed(val, arm_smmu_page(smmu, page) + offset);
206 #define ARM_SMMU_GR0 0
207 #define ARM_SMMU_GR1 1
208 #define ARM_SMMU_CB(s, n) ((s)->numpage + (n))
210 #define arm_smmu_gr0_read(s, o) \
211 arm_smmu_readl((s), ARM_SMMU_GR0, (o))
212 #define arm_smmu_gr0_write(s, o, v) \
213 arm_smmu_writel((s), ARM_SMMU_GR0, (o), (v))
215 #define arm_smmu_gr1_read(s, o) \
216 arm_smmu_readl((s), ARM_SMMU_GR1, (o))
217 #define arm_smmu_gr1_write(s, o, v) \
218 arm_smmu_writel((s), ARM_SMMU_GR1, (o), (v))
220 #define arm_smmu_cb_read(s, n, o) \
221 arm_smmu_readl((s), ARM_SMMU_CB((s), (n)), (o))
222 #define arm_smmu_cb_write(s, n, o, v) \
223 arm_smmu_writel((s), ARM_SMMU_CB((s), (n)), (o), (v))
224 #define arm_smmu_cb_readq(s, n, o) \
225 arm_smmu_readq((s), ARM_SMMU_CB((s), (n)), (o))
226 #define arm_smmu_cb_writeq(s, n, o, v) \
227 arm_smmu_writeq((s), ARM_SMMU_CB((s), (n)), (o), (v))
229 struct arm_smmu_option_prop {
234 static atomic_t cavium_smmu_context_count = ATOMIC_INIT(0);
236 static bool using_legacy_binding, using_generic_binding;
238 static struct arm_smmu_option_prop arm_smmu_options[] = {
239 { ARM_SMMU_OPT_SECURE_CFG_ACCESS, "calxeda,smmu-secure-config-access" },
243 static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)
245 if (pm_runtime_enabled(smmu->dev))
246 return pm_runtime_get_sync(smmu->dev);
251 static inline void arm_smmu_rpm_put(struct arm_smmu_device *smmu)
253 if (pm_runtime_enabled(smmu->dev))
254 pm_runtime_put(smmu->dev);
257 static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
259 return container_of(dom, struct arm_smmu_domain, domain);
262 static void parse_driver_options(struct arm_smmu_device *smmu)
267 if (of_property_read_bool(smmu->dev->of_node,
268 arm_smmu_options[i].prop)) {
269 smmu->options |= arm_smmu_options[i].opt;
270 dev_notice(smmu->dev, "option %s\n",
271 arm_smmu_options[i].prop);
273 } while (arm_smmu_options[++i].opt);
276 static struct device_node *dev_get_dev_node(struct device *dev)
278 if (dev_is_pci(dev)) {
279 struct pci_bus *bus = to_pci_dev(dev)->bus;
281 while (!pci_is_root_bus(bus))
283 return of_node_get(bus->bridge->parent->of_node);
286 return of_node_get(dev->of_node);
289 static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
291 *((__be32 *)data) = cpu_to_be32(alias);
292 return 0; /* Continue walking */
295 static int __find_legacy_master_phandle(struct device *dev, void *data)
297 struct of_phandle_iterator *it = *(void **)data;
298 struct device_node *np = it->node;
301 of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
302 "#stream-id-cells", 0)
303 if (it->node == np) {
304 *(void **)data = dev;
308 return err == -ENOENT ? 0 : err;
311 static struct platform_driver arm_smmu_driver;
312 static struct iommu_ops arm_smmu_ops;
314 static int arm_smmu_register_legacy_master(struct device *dev,
315 struct arm_smmu_device **smmu)
317 struct device *smmu_dev;
318 struct device_node *np;
319 struct of_phandle_iterator it;
325 np = dev_get_dev_node(dev);
326 if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
332 err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
333 __find_legacy_master_phandle);
341 if (dev_is_pci(dev)) {
342 /* "mmu-masters" assumes Stream ID == Requester ID */
343 pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
349 err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
354 sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
358 *smmu = dev_get_drvdata(smmu_dev);
359 of_phandle_iterator_args(&it, sids, it.cur_count);
360 err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
365 static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
370 idx = find_next_zero_bit(map, end, start);
373 } while (test_and_set_bit(idx, map));
378 static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
383 /* Wait for any pending TLB invalidations to complete */
384 static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu, int page,
385 int sync, int status)
387 unsigned int spin_cnt, delay;
390 arm_smmu_writel(smmu, page, sync, QCOM_DUMMY_VAL);
391 for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
392 for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
393 reg = arm_smmu_readl(smmu, page, status);
394 if (!(reg & sTLBGSTATUS_GSACTIVE))
400 dev_err_ratelimited(smmu->dev,
401 "TLB sync timed out -- SMMU may be deadlocked\n");
404 static void arm_smmu_tlb_sync_global(struct arm_smmu_device *smmu)
408 spin_lock_irqsave(&smmu->global_sync_lock, flags);
409 __arm_smmu_tlb_sync(smmu, ARM_SMMU_GR0, ARM_SMMU_GR0_sTLBGSYNC,
410 ARM_SMMU_GR0_sTLBGSTATUS);
411 spin_unlock_irqrestore(&smmu->global_sync_lock, flags);
414 static void arm_smmu_tlb_sync_context(void *cookie)
416 struct arm_smmu_domain *smmu_domain = cookie;
417 struct arm_smmu_device *smmu = smmu_domain->smmu;
420 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
421 __arm_smmu_tlb_sync(smmu, ARM_SMMU_CB(smmu, smmu_domain->cfg.cbndx),
422 ARM_SMMU_CB_TLBSYNC, ARM_SMMU_CB_TLBSTATUS);
423 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
426 static void arm_smmu_tlb_sync_vmid(void *cookie)
428 struct arm_smmu_domain *smmu_domain = cookie;
430 arm_smmu_tlb_sync_global(smmu_domain->smmu);
433 static void arm_smmu_tlb_inv_context_s1(void *cookie)
435 struct arm_smmu_domain *smmu_domain = cookie;
437 * The TLBI write may be relaxed, so ensure that PTEs cleared by the
438 * current CPU are visible beforehand.
441 arm_smmu_cb_write(smmu_domain->smmu, smmu_domain->cfg.cbndx,
442 ARM_SMMU_CB_S1_TLBIASID, smmu_domain->cfg.asid);
443 arm_smmu_tlb_sync_context(cookie);
446 static void arm_smmu_tlb_inv_context_s2(void *cookie)
448 struct arm_smmu_domain *smmu_domain = cookie;
449 struct arm_smmu_device *smmu = smmu_domain->smmu;
453 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
454 arm_smmu_tlb_sync_global(smmu);
457 static void arm_smmu_tlb_inv_range_s1(unsigned long iova, size_t size,
458 size_t granule, bool leaf, void *cookie)
460 struct arm_smmu_domain *smmu_domain = cookie;
461 struct arm_smmu_device *smmu = smmu_domain->smmu;
462 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
463 int reg, idx = cfg->cbndx;
465 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
468 reg = leaf ? ARM_SMMU_CB_S1_TLBIVAL : ARM_SMMU_CB_S1_TLBIVA;
470 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
471 iova = (iova >> 12) << 12;
474 arm_smmu_cb_write(smmu, idx, reg, iova);
476 } while (size -= granule);
479 iova |= (u64)cfg->asid << 48;
481 arm_smmu_cb_writeq(smmu, idx, reg, iova);
482 iova += granule >> 12;
483 } while (size -= granule);
487 static void arm_smmu_tlb_inv_range_s2(unsigned long iova, size_t size,
488 size_t granule, bool leaf, void *cookie)
490 struct arm_smmu_domain *smmu_domain = cookie;
491 struct arm_smmu_device *smmu = smmu_domain->smmu;
492 int reg, idx = smmu_domain->cfg.cbndx;
494 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
497 reg = leaf ? ARM_SMMU_CB_S2_TLBIIPAS2L : ARM_SMMU_CB_S2_TLBIIPAS2;
500 if (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64)
501 arm_smmu_cb_writeq(smmu, idx, reg, iova);
503 arm_smmu_cb_write(smmu, idx, reg, iova);
504 iova += granule >> 12;
505 } while (size -= granule);
509 * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
510 * almost negligible, but the benefit of getting the first one in as far ahead
511 * of the sync as possible is significant, hence we don't just make this a
512 * no-op and set .tlb_sync to arm_smmu_inv_context_s2() as you might think.
514 static void arm_smmu_tlb_inv_vmid_nosync(unsigned long iova, size_t size,
515 size_t granule, bool leaf, void *cookie)
517 struct arm_smmu_domain *smmu_domain = cookie;
518 struct arm_smmu_device *smmu = smmu_domain->smmu;
520 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
523 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
526 static const struct iommu_gather_ops arm_smmu_s1_tlb_ops = {
527 .tlb_flush_all = arm_smmu_tlb_inv_context_s1,
528 .tlb_add_flush = arm_smmu_tlb_inv_range_s1,
529 .tlb_sync = arm_smmu_tlb_sync_context,
532 static const struct iommu_gather_ops arm_smmu_s2_tlb_ops_v2 = {
533 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
534 .tlb_add_flush = arm_smmu_tlb_inv_range_s2,
535 .tlb_sync = arm_smmu_tlb_sync_context,
538 static const struct iommu_gather_ops arm_smmu_s2_tlb_ops_v1 = {
539 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
540 .tlb_add_flush = arm_smmu_tlb_inv_vmid_nosync,
541 .tlb_sync = arm_smmu_tlb_sync_vmid,
544 static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
546 u32 fsr, fsynr, cbfrsynra;
548 struct iommu_domain *domain = dev;
549 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
550 struct arm_smmu_device *smmu = smmu_domain->smmu;
551 int idx = smmu_domain->cfg.cbndx;
553 fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
554 if (!(fsr & FSR_FAULT))
557 fsynr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSYNR0);
558 iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
559 cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
561 dev_err_ratelimited(smmu->dev,
562 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
563 fsr, iova, fsynr, cbfrsynra, idx);
565 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
569 static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
571 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
572 struct arm_smmu_device *smmu = dev;
574 gfsr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
575 gfsynr0 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR0);
576 gfsynr1 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR1);
577 gfsynr2 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR2);
582 dev_err_ratelimited(smmu->dev,
583 "Unexpected global fault, this could be serious\n");
584 dev_err_ratelimited(smmu->dev,
585 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
586 gfsr, gfsynr0, gfsynr1, gfsynr2);
588 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, gfsr);
592 static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
593 struct io_pgtable_cfg *pgtbl_cfg)
595 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
596 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
597 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
603 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
604 cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr;
606 cb->tcr[0] = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
607 cb->tcr[1] = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
608 cb->tcr[1] |= FIELD_PREP(TCR2_SEP, TCR2_SEP_UPSTREAM);
609 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
610 cb->tcr[1] |= TCR2_AS;
613 cb->tcr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
618 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
619 cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr[0];
620 cb->ttbr[1] = pgtbl_cfg->arm_v7s_cfg.ttbr[1];
622 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
623 cb->ttbr[0] |= FIELD_PREP(TTBRn_ASID, cfg->asid);
624 cb->ttbr[1] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
625 cb->ttbr[1] |= FIELD_PREP(TTBRn_ASID, cfg->asid);
628 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
631 /* MAIRs (stage-1 only) */
633 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
634 cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr;
635 cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr;
637 cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
638 cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
643 static void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
647 struct arm_smmu_cb *cb = &smmu->cbs[idx];
648 struct arm_smmu_cfg *cfg = cb->cfg;
650 /* Unassigned context banks only need disabling */
652 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, 0);
656 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
659 if (smmu->version > ARM_SMMU_V1) {
660 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
664 /* 16-bit VMIDs live in CBA2R */
665 if (smmu->features & ARM_SMMU_FEAT_VMID16)
666 reg |= FIELD_PREP(CBA2R_VMID16, cfg->vmid);
668 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBA2R(idx), reg);
672 reg = FIELD_PREP(CBAR_TYPE, cfg->cbar);
673 if (smmu->version < ARM_SMMU_V2)
674 reg |= FIELD_PREP(CBAR_IRPTNDX, cfg->irptndx);
677 * Use the weakest shareability/memory types, so they are
678 * overridden by the ttbcr/pte.
681 reg |= FIELD_PREP(CBAR_S1_BPSHCFG, CBAR_S1_BPSHCFG_NSH) |
682 FIELD_PREP(CBAR_S1_MEMATTR, CBAR_S1_MEMATTR_WB);
683 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
684 /* 8-bit VMIDs live in CBAR */
685 reg |= FIELD_PREP(CBAR_VMID, cfg->vmid);
687 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(idx), reg);
691 * We must write this before the TTBRs, since it determines the
692 * access behaviour of some fields (in particular, ASID[15:8]).
694 if (stage1 && smmu->version > ARM_SMMU_V1)
695 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR2, cb->tcr[1]);
696 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR, cb->tcr[0]);
699 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
700 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_CONTEXTIDR, cfg->asid);
701 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
702 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR1, cb->ttbr[1]);
704 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
706 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR1,
710 /* MAIRs (stage-1 only) */
712 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR0, cb->mair[0]);
713 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR1, cb->mair[1]);
717 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE | SCTLR_M;
719 reg |= SCTLR_S1_ASIDPNE;
720 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
723 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
726 static int arm_smmu_init_domain_context(struct iommu_domain *domain,
727 struct arm_smmu_device *smmu)
729 int irq, start, ret = 0;
730 unsigned long ias, oas;
731 struct io_pgtable_ops *pgtbl_ops;
732 struct io_pgtable_cfg pgtbl_cfg;
733 enum io_pgtable_fmt fmt;
734 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
735 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
737 mutex_lock(&smmu_domain->init_mutex);
738 if (smmu_domain->smmu)
741 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
742 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
743 smmu_domain->smmu = smmu;
748 * Mapping the requested stage onto what we support is surprisingly
749 * complicated, mainly because the spec allows S1+S2 SMMUs without
750 * support for nested translation. That means we end up with the
753 * Requested Supported Actual
763 * Note that you can't actually request stage-2 mappings.
765 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
766 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
767 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
768 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
771 * Choosing a suitable context format is even more fiddly. Until we
772 * grow some way for the caller to express a preference, and/or move
773 * the decision into the io-pgtable code where it arguably belongs,
774 * just aim for the closest thing to the rest of the system, and hope
775 * that the hardware isn't esoteric enough that we can't assume AArch64
776 * support to be a superset of AArch32 support...
778 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
779 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
780 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
781 !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
782 (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
783 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
784 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
785 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
786 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
787 ARM_SMMU_FEAT_FMT_AARCH64_16K |
788 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
789 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
791 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
796 switch (smmu_domain->stage) {
797 case ARM_SMMU_DOMAIN_S1:
798 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
799 start = smmu->num_s2_context_banks;
801 oas = smmu->ipa_size;
802 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
803 fmt = ARM_64_LPAE_S1;
804 } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
805 fmt = ARM_32_LPAE_S1;
806 ias = min(ias, 32UL);
807 oas = min(oas, 40UL);
810 ias = min(ias, 32UL);
811 oas = min(oas, 32UL);
813 smmu_domain->tlb_ops = &arm_smmu_s1_tlb_ops;
815 case ARM_SMMU_DOMAIN_NESTED:
817 * We will likely want to change this if/when KVM gets
820 case ARM_SMMU_DOMAIN_S2:
821 cfg->cbar = CBAR_TYPE_S2_TRANS;
823 ias = smmu->ipa_size;
825 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
826 fmt = ARM_64_LPAE_S2;
828 fmt = ARM_32_LPAE_S2;
829 ias = min(ias, 40UL);
830 oas = min(oas, 40UL);
832 if (smmu->version == ARM_SMMU_V2)
833 smmu_domain->tlb_ops = &arm_smmu_s2_tlb_ops_v2;
835 smmu_domain->tlb_ops = &arm_smmu_s2_tlb_ops_v1;
841 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
842 smmu->num_context_banks);
847 if (smmu->version < ARM_SMMU_V2) {
848 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
849 cfg->irptndx %= smmu->num_context_irqs;
851 cfg->irptndx = cfg->cbndx;
854 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
855 cfg->vmid = cfg->cbndx + 1 + smmu->cavium_id_base;
857 cfg->asid = cfg->cbndx + smmu->cavium_id_base;
859 pgtbl_cfg = (struct io_pgtable_cfg) {
860 .pgsize_bitmap = smmu->pgsize_bitmap,
863 .coherent_walk = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK,
864 .tlb = smmu_domain->tlb_ops,
865 .iommu_dev = smmu->dev,
868 if (smmu_domain->non_strict)
869 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT;
871 smmu_domain->smmu = smmu;
872 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
878 /* Update the domain's page sizes to reflect the page table format */
879 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
880 domain->geometry.aperture_end = (1UL << ias) - 1;
881 domain->geometry.force_aperture = true;
883 /* Initialise the context bank with our page table cfg */
884 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
885 arm_smmu_write_context_bank(smmu, cfg->cbndx);
888 * Request context fault interrupt. Do this last to avoid the
889 * handler seeing a half-initialised domain state.
891 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
892 ret = devm_request_irq(smmu->dev, irq, arm_smmu_context_fault,
893 IRQF_SHARED, "arm-smmu-context-fault", domain);
895 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
897 cfg->irptndx = INVALID_IRPTNDX;
900 mutex_unlock(&smmu_domain->init_mutex);
902 /* Publish page table ops for map/unmap */
903 smmu_domain->pgtbl_ops = pgtbl_ops;
907 smmu_domain->smmu = NULL;
909 mutex_unlock(&smmu_domain->init_mutex);
913 static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
915 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
916 struct arm_smmu_device *smmu = smmu_domain->smmu;
917 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
920 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
923 ret = arm_smmu_rpm_get(smmu);
928 * Disable the context bank and free the page tables before freeing
931 smmu->cbs[cfg->cbndx].cfg = NULL;
932 arm_smmu_write_context_bank(smmu, cfg->cbndx);
934 if (cfg->irptndx != INVALID_IRPTNDX) {
935 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
936 devm_free_irq(smmu->dev, irq, domain);
939 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
940 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
942 arm_smmu_rpm_put(smmu);
945 static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
947 struct arm_smmu_domain *smmu_domain;
949 if (type != IOMMU_DOMAIN_UNMANAGED &&
950 type != IOMMU_DOMAIN_DMA &&
951 type != IOMMU_DOMAIN_IDENTITY)
954 * Allocate the domain and initialise some of its data structures.
955 * We can't really do anything meaningful until we've added a
958 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
962 if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
963 iommu_get_dma_cookie(&smmu_domain->domain))) {
968 mutex_init(&smmu_domain->init_mutex);
969 spin_lock_init(&smmu_domain->cb_lock);
971 return &smmu_domain->domain;
974 static void arm_smmu_domain_free(struct iommu_domain *domain)
976 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
979 * Free the domain resources. We assume that all devices have
980 * already been detached.
982 iommu_put_dma_cookie(domain);
983 arm_smmu_destroy_domain_context(domain);
987 static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
989 struct arm_smmu_smr *smr = smmu->smrs + idx;
990 u32 reg = FIELD_PREP(SMR_ID, smr->id) | FIELD_PREP(SMR_MASK, smr->mask);
992 if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
994 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(idx), reg);
997 static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
999 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
1000 u32 reg = FIELD_PREP(S2CR_TYPE, s2cr->type) |
1001 FIELD_PREP(S2CR_CBNDX, s2cr->cbndx) |
1002 FIELD_PREP(S2CR_PRIVCFG, s2cr->privcfg);
1004 if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
1005 smmu->smrs[idx].valid)
1006 reg |= S2CR_EXIDVALID;
1007 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
1010 static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
1012 arm_smmu_write_s2cr(smmu, idx);
1014 arm_smmu_write_smr(smmu, idx);
1018 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
1019 * should be called after sCR0 is written.
1021 static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
1029 * SMR.ID bits may not be preserved if the corresponding MASK
1030 * bits are set, so check each one separately. We can reject
1031 * masters later if they try to claim IDs outside these masks.
1033 smr = FIELD_PREP(SMR_ID, smmu->streamid_mask);
1034 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(0), smr);
1035 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(0));
1036 smmu->streamid_mask = FIELD_GET(SMR_ID, smr);
1038 smr = FIELD_PREP(SMR_MASK, smmu->streamid_mask);
1039 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(0), smr);
1040 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(0));
1041 smmu->smr_mask_mask = FIELD_GET(SMR_MASK, smr);
1044 static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
1046 struct arm_smmu_smr *smrs = smmu->smrs;
1047 int i, free_idx = -ENOSPC;
1049 /* Stream indexing is blissfully easy */
1053 /* Validating SMRs is... less so */
1054 for (i = 0; i < smmu->num_mapping_groups; ++i) {
1055 if (!smrs[i].valid) {
1057 * Note the first free entry we come across, which
1058 * we'll claim in the end if nothing else matches.
1065 * If the new entry is _entirely_ matched by an existing entry,
1066 * then reuse that, with the guarantee that there also cannot
1067 * be any subsequent conflicting entries. In normal use we'd
1068 * expect simply identical entries for this case, but there's
1069 * no harm in accommodating the generalisation.
1071 if ((mask & smrs[i].mask) == mask &&
1072 !((id ^ smrs[i].id) & ~smrs[i].mask))
1075 * If the new entry has any other overlap with an existing one,
1076 * though, then there always exists at least one stream ID
1077 * which would cause a conflict, and we can't allow that risk.
1079 if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1086 static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1088 if (--smmu->s2crs[idx].count)
1091 smmu->s2crs[idx] = s2cr_init_val;
1093 smmu->smrs[idx].valid = false;
1098 static int arm_smmu_master_alloc_smes(struct device *dev)
1100 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1101 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
1102 struct arm_smmu_device *smmu = cfg->smmu;
1103 struct arm_smmu_smr *smrs = smmu->smrs;
1104 struct iommu_group *group;
1107 mutex_lock(&smmu->stream_map_mutex);
1108 /* Figure out a viable stream map entry allocation */
1109 for_each_cfg_sme(fwspec, i, idx) {
1110 u16 sid = FIELD_GET(SMR_ID, fwspec->ids[i]);
1111 u16 mask = FIELD_GET(SMR_MASK, fwspec->ids[i]);
1113 if (idx != INVALID_SMENDX) {
1118 ret = arm_smmu_find_sme(smmu, sid, mask);
1123 if (smrs && smmu->s2crs[idx].count == 0) {
1125 smrs[idx].mask = mask;
1126 smrs[idx].valid = true;
1128 smmu->s2crs[idx].count++;
1129 cfg->smendx[i] = (s16)idx;
1132 group = iommu_group_get_for_dev(dev);
1134 group = ERR_PTR(-ENOMEM);
1135 if (IS_ERR(group)) {
1136 ret = PTR_ERR(group);
1139 iommu_group_put(group);
1141 /* It worked! Now, poke the actual hardware */
1142 for_each_cfg_sme(fwspec, i, idx) {
1143 arm_smmu_write_sme(smmu, idx);
1144 smmu->s2crs[idx].group = group;
1147 mutex_unlock(&smmu->stream_map_mutex);
1152 arm_smmu_free_sme(smmu, cfg->smendx[i]);
1153 cfg->smendx[i] = INVALID_SMENDX;
1155 mutex_unlock(&smmu->stream_map_mutex);
1159 static void arm_smmu_master_free_smes(struct iommu_fwspec *fwspec)
1161 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
1162 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
1165 mutex_lock(&smmu->stream_map_mutex);
1166 for_each_cfg_sme(fwspec, i, idx) {
1167 if (arm_smmu_free_sme(smmu, idx))
1168 arm_smmu_write_sme(smmu, idx);
1169 cfg->smendx[i] = INVALID_SMENDX;
1171 mutex_unlock(&smmu->stream_map_mutex);
1174 static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1175 struct iommu_fwspec *fwspec)
1177 struct arm_smmu_device *smmu = smmu_domain->smmu;
1178 struct arm_smmu_s2cr *s2cr = smmu->s2crs;
1179 u8 cbndx = smmu_domain->cfg.cbndx;
1180 enum arm_smmu_s2cr_type type;
1183 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1184 type = S2CR_TYPE_BYPASS;
1186 type = S2CR_TYPE_TRANS;
1188 for_each_cfg_sme(fwspec, i, idx) {
1189 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
1192 s2cr[idx].type = type;
1193 s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
1194 s2cr[idx].cbndx = cbndx;
1195 arm_smmu_write_s2cr(smmu, idx);
1200 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1203 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1204 struct arm_smmu_device *smmu;
1205 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1207 if (!fwspec || fwspec->ops != &arm_smmu_ops) {
1208 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1213 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1214 * domains between of_xlate() and add_device() - we have no way to cope
1215 * with that, so until ARM gets converted to rely on groups and default
1216 * domains, just say no (but more politely than by dereferencing NULL).
1217 * This should be at least a WARN_ON once that's sorted.
1219 if (!fwspec->iommu_priv)
1222 smmu = fwspec_smmu(fwspec);
1224 ret = arm_smmu_rpm_get(smmu);
1228 /* Ensure that the domain is finalised */
1229 ret = arm_smmu_init_domain_context(domain, smmu);
1234 * Sanity check the domain. We don't support domains across
1237 if (smmu_domain->smmu != smmu) {
1239 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1240 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1245 /* Looks ok, so add the device to the domain */
1246 ret = arm_smmu_domain_add_master(smmu_domain, fwspec);
1249 arm_smmu_rpm_put(smmu);
1253 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1254 phys_addr_t paddr, size_t size, int prot)
1256 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1257 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1263 arm_smmu_rpm_get(smmu);
1264 ret = ops->map(ops, iova, paddr, size, prot);
1265 arm_smmu_rpm_put(smmu);
1270 static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1273 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1274 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1280 arm_smmu_rpm_get(smmu);
1281 ret = ops->unmap(ops, iova, size);
1282 arm_smmu_rpm_put(smmu);
1287 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
1289 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1290 struct arm_smmu_device *smmu = smmu_domain->smmu;
1292 if (smmu_domain->tlb_ops) {
1293 arm_smmu_rpm_get(smmu);
1294 smmu_domain->tlb_ops->tlb_flush_all(smmu_domain);
1295 arm_smmu_rpm_put(smmu);
1299 static void arm_smmu_iotlb_sync(struct iommu_domain *domain)
1301 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1302 struct arm_smmu_device *smmu = smmu_domain->smmu;
1304 if (smmu_domain->tlb_ops) {
1305 arm_smmu_rpm_get(smmu);
1306 smmu_domain->tlb_ops->tlb_sync(smmu_domain);
1307 arm_smmu_rpm_put(smmu);
1311 static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1314 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1315 struct arm_smmu_device *smmu = smmu_domain->smmu;
1316 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1317 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1318 struct device *dev = smmu->dev;
1322 unsigned long va, flags;
1323 int ret, idx = cfg->cbndx;
1325 ret = arm_smmu_rpm_get(smmu);
1329 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
1330 va = iova & ~0xfffUL;
1331 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
1332 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1334 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1336 reg = arm_smmu_page(smmu, ARM_SMMU_CB(smmu, idx)) + ARM_SMMU_CB_ATSR;
1337 if (readl_poll_timeout_atomic(reg, tmp, !(tmp & ATSR_ACTIVE), 5, 50)) {
1338 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1340 "iova to phys timed out on %pad. Falling back to software table walk.\n",
1342 return ops->iova_to_phys(ops, iova);
1345 phys = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_PAR);
1346 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1347 if (phys & CB_PAR_F) {
1348 dev_err(dev, "translation fault!\n");
1349 dev_err(dev, "PAR = 0x%llx\n", phys);
1353 arm_smmu_rpm_put(smmu);
1355 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1358 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1361 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1362 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1364 if (domain->type == IOMMU_DOMAIN_IDENTITY)
1370 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1371 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1372 return arm_smmu_iova_to_phys_hard(domain, iova);
1374 return ops->iova_to_phys(ops, iova);
1377 static bool arm_smmu_capable(enum iommu_cap cap)
1380 case IOMMU_CAP_CACHE_COHERENCY:
1382 * Return true here as the SMMU can always send out coherent
1386 case IOMMU_CAP_NOEXEC:
1393 static int arm_smmu_match_node(struct device *dev, const void *data)
1395 return dev->fwnode == data;
1399 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
1401 struct device *dev = driver_find_device(&arm_smmu_driver.driver, NULL,
1402 fwnode, arm_smmu_match_node);
1404 return dev ? dev_get_drvdata(dev) : NULL;
1407 static int arm_smmu_add_device(struct device *dev)
1409 struct arm_smmu_device *smmu;
1410 struct arm_smmu_master_cfg *cfg;
1411 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1414 if (using_legacy_binding) {
1415 ret = arm_smmu_register_legacy_master(dev, &smmu);
1418 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1419 * will allocate/initialise a new one. Thus we need to update fwspec for
1422 fwspec = dev_iommu_fwspec_get(dev);
1425 } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
1426 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
1432 for (i = 0; i < fwspec->num_ids; i++) {
1433 u16 sid = FIELD_GET(SMR_ID, fwspec->ids[i]);
1434 u16 mask = FIELD_GET(SMR_MASK, fwspec->ids[i]);
1436 if (sid & ~smmu->streamid_mask) {
1437 dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
1438 sid, smmu->streamid_mask);
1441 if (mask & ~smmu->smr_mask_mask) {
1442 dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1443 mask, smmu->smr_mask_mask);
1449 cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1455 fwspec->iommu_priv = cfg;
1457 cfg->smendx[i] = INVALID_SMENDX;
1459 ret = arm_smmu_rpm_get(smmu);
1463 ret = arm_smmu_master_alloc_smes(dev);
1464 arm_smmu_rpm_put(smmu);
1469 iommu_device_link(&smmu->iommu, dev);
1471 device_link_add(dev, smmu->dev,
1472 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
1479 iommu_fwspec_free(dev);
1483 static void arm_smmu_remove_device(struct device *dev)
1485 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1486 struct arm_smmu_master_cfg *cfg;
1487 struct arm_smmu_device *smmu;
1490 if (!fwspec || fwspec->ops != &arm_smmu_ops)
1493 cfg = fwspec->iommu_priv;
1496 ret = arm_smmu_rpm_get(smmu);
1500 iommu_device_unlink(&smmu->iommu, dev);
1501 arm_smmu_master_free_smes(fwspec);
1503 arm_smmu_rpm_put(smmu);
1505 iommu_group_remove_device(dev);
1506 kfree(fwspec->iommu_priv);
1507 iommu_fwspec_free(dev);
1510 static struct iommu_group *arm_smmu_device_group(struct device *dev)
1512 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1513 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
1514 struct iommu_group *group = NULL;
1517 for_each_cfg_sme(fwspec, i, idx) {
1518 if (group && smmu->s2crs[idx].group &&
1519 group != smmu->s2crs[idx].group)
1520 return ERR_PTR(-EINVAL);
1522 group = smmu->s2crs[idx].group;
1526 return iommu_group_ref_get(group);
1528 if (dev_is_pci(dev))
1529 group = pci_device_group(dev);
1530 else if (dev_is_fsl_mc(dev))
1531 group = fsl_mc_device_group(dev);
1533 group = generic_device_group(dev);
1538 static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1539 enum iommu_attr attr, void *data)
1541 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1543 switch(domain->type) {
1544 case IOMMU_DOMAIN_UNMANAGED:
1546 case DOMAIN_ATTR_NESTING:
1547 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1553 case IOMMU_DOMAIN_DMA:
1555 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1556 *(int *)data = smmu_domain->non_strict;
1567 static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1568 enum iommu_attr attr, void *data)
1571 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1573 mutex_lock(&smmu_domain->init_mutex);
1575 switch(domain->type) {
1576 case IOMMU_DOMAIN_UNMANAGED:
1578 case DOMAIN_ATTR_NESTING:
1579 if (smmu_domain->smmu) {
1585 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1587 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1593 case IOMMU_DOMAIN_DMA:
1595 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1596 smmu_domain->non_strict = *(int *)data;
1606 mutex_unlock(&smmu_domain->init_mutex);
1610 static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1614 if (args->args_count > 0)
1615 fwid |= FIELD_PREP(SMR_ID, args->args[0]);
1617 if (args->args_count > 1)
1618 fwid |= FIELD_PREP(SMR_MASK, args->args[1]);
1619 else if (!of_property_read_u32(args->np, "stream-match-mask", &mask))
1620 fwid |= FIELD_PREP(SMR_MASK, mask);
1622 return iommu_fwspec_add_ids(dev, &fwid, 1);
1625 static void arm_smmu_get_resv_regions(struct device *dev,
1626 struct list_head *head)
1628 struct iommu_resv_region *region;
1629 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1631 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
1632 prot, IOMMU_RESV_SW_MSI);
1636 list_add_tail(®ion->list, head);
1638 iommu_dma_get_resv_regions(dev, head);
1641 static void arm_smmu_put_resv_regions(struct device *dev,
1642 struct list_head *head)
1644 struct iommu_resv_region *entry, *next;
1646 list_for_each_entry_safe(entry, next, head, list)
1650 static struct iommu_ops arm_smmu_ops = {
1651 .capable = arm_smmu_capable,
1652 .domain_alloc = arm_smmu_domain_alloc,
1653 .domain_free = arm_smmu_domain_free,
1654 .attach_dev = arm_smmu_attach_dev,
1655 .map = arm_smmu_map,
1656 .unmap = arm_smmu_unmap,
1657 .flush_iotlb_all = arm_smmu_flush_iotlb_all,
1658 .iotlb_sync = arm_smmu_iotlb_sync,
1659 .iova_to_phys = arm_smmu_iova_to_phys,
1660 .add_device = arm_smmu_add_device,
1661 .remove_device = arm_smmu_remove_device,
1662 .device_group = arm_smmu_device_group,
1663 .domain_get_attr = arm_smmu_domain_get_attr,
1664 .domain_set_attr = arm_smmu_domain_set_attr,
1665 .of_xlate = arm_smmu_of_xlate,
1666 .get_resv_regions = arm_smmu_get_resv_regions,
1667 .put_resv_regions = arm_smmu_put_resv_regions,
1668 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1671 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1676 /* clear global FSR */
1677 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
1678 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, reg);
1681 * Reset stream mapping groups: Initial values mark all SMRn as
1682 * invalid and all S2CRn as bypass unless overridden.
1684 for (i = 0; i < smmu->num_mapping_groups; ++i)
1685 arm_smmu_write_sme(smmu, i);
1687 if (smmu->model == ARM_MMU500) {
1689 * Before clearing ARM_MMU500_ACTLR_CPRE, need to
1690 * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
1691 * bit is only present in MMU-500r2 onwards.
1693 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID7);
1694 major = FIELD_GET(ID7_MAJOR, reg);
1695 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sACR);
1697 reg &= ~ARM_MMU500_ACR_CACHE_LOCK;
1699 * Allow unmatched Stream IDs to allocate bypass
1700 * TLB entries for reduced latency.
1702 reg |= ARM_MMU500_ACR_SMTNMB_TLBEN | ARM_MMU500_ACR_S2CRB_TLBEN;
1703 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sACR, reg);
1706 /* Make sure all context banks are disabled and clear CB_FSR */
1707 for (i = 0; i < smmu->num_context_banks; ++i) {
1708 arm_smmu_write_context_bank(smmu, i);
1709 arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_FSR, FSR_FAULT);
1711 * Disable MMU-500's not-particularly-beneficial next-page
1712 * prefetcher for the sake of errata #841119 and #826419.
1714 if (smmu->model == ARM_MMU500) {
1715 reg = arm_smmu_cb_read(smmu, i, ARM_SMMU_CB_ACTLR);
1716 reg &= ~ARM_MMU500_ACTLR_CPRE;
1717 arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_ACTLR, reg);
1721 /* Invalidate the TLB, just in case */
1722 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLH, QCOM_DUMMY_VAL);
1723 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLNSNH, QCOM_DUMMY_VAL);
1725 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sCR0);
1727 /* Enable fault reporting */
1728 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
1730 /* Disable TLB broadcasting. */
1731 reg |= (sCR0_VMIDPNE | sCR0_PTM);
1733 /* Enable client access, handling unmatched streams as appropriate */
1734 reg &= ~sCR0_CLIENTPD;
1738 reg &= ~sCR0_USFCFG;
1740 /* Disable forced broadcasting */
1743 /* Don't upgrade barriers */
1746 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1747 reg |= sCR0_VMID16EN;
1749 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
1750 reg |= sCR0_EXIDENABLE;
1752 /* Push the button */
1753 arm_smmu_tlb_sync_global(smmu);
1754 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, reg);
1757 static int arm_smmu_id_size_to_bits(int size)
1776 static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1780 bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
1783 dev_notice(smmu->dev, "probing hardware configuration...\n");
1784 dev_notice(smmu->dev, "SMMUv%d with:\n",
1785 smmu->version == ARM_SMMU_V2 ? 2 : 1);
1788 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID0);
1790 /* Restrict available stages based on module parameter */
1791 if (force_stage == 1)
1792 id &= ~(ID0_S2TS | ID0_NTS);
1793 else if (force_stage == 2)
1794 id &= ~(ID0_S1TS | ID0_NTS);
1796 if (id & ID0_S1TS) {
1797 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1798 dev_notice(smmu->dev, "\tstage 1 translation\n");
1801 if (id & ID0_S2TS) {
1802 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1803 dev_notice(smmu->dev, "\tstage 2 translation\n");
1807 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1808 dev_notice(smmu->dev, "\tnested translation\n");
1811 if (!(smmu->features &
1812 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
1813 dev_err(smmu->dev, "\tno translation support!\n");
1817 if ((id & ID0_S1TS) &&
1818 ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
1819 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1820 dev_notice(smmu->dev, "\taddress translation ops\n");
1824 * In order for DMA API calls to work properly, we must defer to what
1825 * the FW says about coherency, regardless of what the hardware claims.
1826 * Fortunately, this also opens up a workaround for systems where the
1827 * ID register value has ended up configured incorrectly.
1829 cttw_reg = !!(id & ID0_CTTW);
1830 if (cttw_fw || cttw_reg)
1831 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1832 cttw_fw ? "" : "non-");
1833 if (cttw_fw != cttw_reg)
1834 dev_notice(smmu->dev,
1835 "\t(IDR0.CTTW overridden by FW configuration)\n");
1837 /* Max. number of entries we have for stream matching/indexing */
1838 if (smmu->version == ARM_SMMU_V2 && id & ID0_EXIDS) {
1839 smmu->features |= ARM_SMMU_FEAT_EXIDS;
1842 size = 1 << FIELD_GET(ID0_NUMSIDB, id);
1844 smmu->streamid_mask = size - 1;
1846 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1847 size = FIELD_GET(ID0_NUMSMRG, id);
1850 "stream-matching supported, but no SMRs present!\n");
1854 /* Zero-initialised to mark as invalid */
1855 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1860 dev_notice(smmu->dev,
1861 "\tstream matching with %u register groups", size);
1863 /* s2cr->type == 0 means translation, so initialise explicitly */
1864 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1868 for (i = 0; i < size; i++)
1869 smmu->s2crs[i] = s2cr_init_val;
1871 smmu->num_mapping_groups = size;
1872 mutex_init(&smmu->stream_map_mutex);
1873 spin_lock_init(&smmu->global_sync_lock);
1875 if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
1876 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1877 if (!(id & ID0_PTFS_NO_AARCH32S))
1878 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1882 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID1);
1883 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
1885 /* Check for size mismatch of SMMU address space from mapped region */
1886 size = 1 << (FIELD_GET(ID1_NUMPAGENDXB, id) + 1);
1887 if (smmu->numpage != 2 * size << smmu->pgshift)
1889 "SMMU address space size (0x%x) differs from mapped region size (0x%x)!\n",
1890 2 * size << smmu->pgshift, smmu->numpage);
1891 /* Now properly encode NUMPAGE to subsequently derive SMMU_CB_BASE */
1892 smmu->numpage = size;
1894 smmu->num_s2_context_banks = FIELD_GET(ID1_NUMS2CB, id);
1895 smmu->num_context_banks = FIELD_GET(ID1_NUMCB, id);
1896 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1897 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1900 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1901 smmu->num_context_banks, smmu->num_s2_context_banks);
1903 * Cavium CN88xx erratum #27704.
1904 * Ensure ASID and VMID allocation is unique across all SMMUs in
1907 if (smmu->model == CAVIUM_SMMUV2) {
1908 smmu->cavium_id_base =
1909 atomic_add_return(smmu->num_context_banks,
1910 &cavium_smmu_context_count);
1911 smmu->cavium_id_base -= smmu->num_context_banks;
1912 dev_notice(smmu->dev, "\tenabling workaround for Cavium erratum 27704\n");
1914 smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
1915 sizeof(*smmu->cbs), GFP_KERNEL);
1920 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID2);
1921 size = arm_smmu_id_size_to_bits(FIELD_GET(ID2_IAS, id));
1922 smmu->ipa_size = size;
1924 /* The output mask is also applied for bypass */
1925 size = arm_smmu_id_size_to_bits(FIELD_GET(ID2_OAS, id));
1926 smmu->pa_size = size;
1928 if (id & ID2_VMID16)
1929 smmu->features |= ARM_SMMU_FEAT_VMID16;
1932 * What the page table walker can address actually depends on which
1933 * descriptor format is in use, but since a) we don't know that yet,
1934 * and b) it can vary per context bank, this will have to do...
1936 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1938 "failed to set DMA mask for table walker\n");
1940 if (smmu->version < ARM_SMMU_V2) {
1941 smmu->va_size = smmu->ipa_size;
1942 if (smmu->version == ARM_SMMU_V1_64K)
1943 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1945 size = FIELD_GET(ID2_UBS, id);
1946 smmu->va_size = arm_smmu_id_size_to_bits(size);
1947 if (id & ID2_PTFS_4K)
1948 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
1949 if (id & ID2_PTFS_16K)
1950 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
1951 if (id & ID2_PTFS_64K)
1952 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1955 /* Now we've corralled the various formats, what'll it do? */
1956 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
1957 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
1958 if (smmu->features &
1959 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
1960 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
1961 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
1962 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
1963 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
1964 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
1966 if (arm_smmu_ops.pgsize_bitmap == -1UL)
1967 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1969 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1970 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1971 smmu->pgsize_bitmap);
1974 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1975 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1976 smmu->va_size, smmu->ipa_size);
1978 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1979 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1980 smmu->ipa_size, smmu->pa_size);
1985 struct arm_smmu_match_data {
1986 enum arm_smmu_arch_version version;
1987 enum arm_smmu_implementation model;
1990 #define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1991 static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
1993 ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1994 ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
1995 ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
1996 ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
1997 ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
1998 ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2);
2000 static const struct of_device_id arm_smmu_of_match[] = {
2001 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
2002 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
2003 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
2004 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
2005 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
2006 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
2007 { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
2012 static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
2017 case ACPI_IORT_SMMU_V1:
2018 case ACPI_IORT_SMMU_CORELINK_MMU400:
2019 smmu->version = ARM_SMMU_V1;
2020 smmu->model = GENERIC_SMMU;
2022 case ACPI_IORT_SMMU_CORELINK_MMU401:
2023 smmu->version = ARM_SMMU_V1_64K;
2024 smmu->model = GENERIC_SMMU;
2026 case ACPI_IORT_SMMU_V2:
2027 smmu->version = ARM_SMMU_V2;
2028 smmu->model = GENERIC_SMMU;
2030 case ACPI_IORT_SMMU_CORELINK_MMU500:
2031 smmu->version = ARM_SMMU_V2;
2032 smmu->model = ARM_MMU500;
2034 case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
2035 smmu->version = ARM_SMMU_V2;
2036 smmu->model = CAVIUM_SMMUV2;
2045 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2046 struct arm_smmu_device *smmu)
2048 struct device *dev = smmu->dev;
2049 struct acpi_iort_node *node =
2050 *(struct acpi_iort_node **)dev_get_platdata(dev);
2051 struct acpi_iort_smmu *iort_smmu;
2054 /* Retrieve SMMU1/2 specific data */
2055 iort_smmu = (struct acpi_iort_smmu *)node->node_data;
2057 ret = acpi_smmu_get_data(iort_smmu->model, smmu);
2061 /* Ignore the configuration access interrupt */
2062 smmu->num_global_irqs = 1;
2064 if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
2065 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2070 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
2071 struct arm_smmu_device *smmu)
2077 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
2078 struct arm_smmu_device *smmu)
2080 const struct arm_smmu_match_data *data;
2081 struct device *dev = &pdev->dev;
2082 bool legacy_binding;
2084 if (of_property_read_u32(dev->of_node, "#global-interrupts",
2085 &smmu->num_global_irqs)) {
2086 dev_err(dev, "missing #global-interrupts property\n");
2090 data = of_device_get_match_data(dev);
2091 smmu->version = data->version;
2092 smmu->model = data->model;
2094 parse_driver_options(smmu);
2096 legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
2097 if (legacy_binding && !using_generic_binding) {
2098 if (!using_legacy_binding)
2099 pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
2100 using_legacy_binding = true;
2101 } else if (!legacy_binding && !using_legacy_binding) {
2102 using_generic_binding = true;
2104 dev_err(dev, "not probing due to mismatched DT properties\n");
2108 if (of_dma_is_coherent(dev->of_node))
2109 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2114 static void arm_smmu_bus_init(void)
2116 /* Oh, for a proper bus abstraction */
2117 if (!iommu_present(&platform_bus_type))
2118 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2119 #ifdef CONFIG_ARM_AMBA
2120 if (!iommu_present(&amba_bustype))
2121 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2124 if (!iommu_present(&pci_bus_type)) {
2126 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2129 #ifdef CONFIG_FSL_MC_BUS
2130 if (!iommu_present(&fsl_mc_bus_type))
2131 bus_set_iommu(&fsl_mc_bus_type, &arm_smmu_ops);
2135 static int arm_smmu_device_probe(struct platform_device *pdev)
2137 struct resource *res;
2138 resource_size_t ioaddr;
2139 struct arm_smmu_device *smmu;
2140 struct device *dev = &pdev->dev;
2141 int num_irqs, i, err;
2143 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2145 dev_err(dev, "failed to allocate arm_smmu_device\n");
2151 err = arm_smmu_device_dt_probe(pdev, smmu);
2153 err = arm_smmu_device_acpi_probe(pdev, smmu);
2158 smmu = arm_smmu_impl_init(smmu);
2160 return PTR_ERR(smmu);
2162 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2163 ioaddr = res->start;
2164 smmu->base = devm_ioremap_resource(dev, res);
2165 if (IS_ERR(smmu->base))
2166 return PTR_ERR(smmu->base);
2168 * The resource size should effectively match the value of SMMU_TOP;
2169 * stash that temporarily until we know PAGESIZE to validate it with.
2171 smmu->numpage = resource_size(res);
2174 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2176 if (num_irqs > smmu->num_global_irqs)
2177 smmu->num_context_irqs++;
2180 if (!smmu->num_context_irqs) {
2181 dev_err(dev, "found %d interrupts but expected at least %d\n",
2182 num_irqs, smmu->num_global_irqs + 1);
2186 smmu->irqs = devm_kcalloc(dev, num_irqs, sizeof(*smmu->irqs),
2189 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2193 for (i = 0; i < num_irqs; ++i) {
2194 int irq = platform_get_irq(pdev, i);
2197 dev_err(dev, "failed to get irq index %d\n", i);
2200 smmu->irqs[i] = irq;
2203 err = devm_clk_bulk_get_all(dev, &smmu->clks);
2205 dev_err(dev, "failed to get clocks %d\n", err);
2208 smmu->num_clks = err;
2210 err = clk_bulk_prepare_enable(smmu->num_clks, smmu->clks);
2214 err = arm_smmu_device_cfg_probe(smmu);
2218 if (smmu->version == ARM_SMMU_V2) {
2219 if (smmu->num_context_banks > smmu->num_context_irqs) {
2221 "found only %d context irq(s) but %d required\n",
2222 smmu->num_context_irqs, smmu->num_context_banks);
2226 /* Ignore superfluous interrupts */
2227 smmu->num_context_irqs = smmu->num_context_banks;
2230 for (i = 0; i < smmu->num_global_irqs; ++i) {
2231 err = devm_request_irq(smmu->dev, smmu->irqs[i],
2232 arm_smmu_global_fault,
2234 "arm-smmu global fault",
2237 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2243 err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2244 "smmu.%pa", &ioaddr);
2246 dev_err(dev, "Failed to register iommu in sysfs\n");
2250 iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2251 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2253 err = iommu_device_register(&smmu->iommu);
2255 dev_err(dev, "Failed to register iommu\n");
2259 platform_set_drvdata(pdev, smmu);
2260 arm_smmu_device_reset(smmu);
2261 arm_smmu_test_smr_masks(smmu);
2264 * We want to avoid touching dev->power.lock in fastpaths unless
2265 * it's really going to do something useful - pm_runtime_enabled()
2266 * can serve as an ideal proxy for that decision. So, conditionally
2267 * enable pm_runtime.
2269 if (dev->pm_domain) {
2270 pm_runtime_set_active(dev);
2271 pm_runtime_enable(dev);
2275 * For ACPI and generic DT bindings, an SMMU will be probed before
2276 * any device which might need it, so we want the bus ops in place
2277 * ready to handle default domain setup as soon as any SMMU exists.
2279 if (!using_legacy_binding)
2280 arm_smmu_bus_init();
2286 * With the legacy DT binding in play, though, we have no guarantees about
2287 * probe order, but then we're also not doing default domains, so we can
2288 * delay setting bus ops until we're sure every possible SMMU is ready,
2289 * and that way ensure that no add_device() calls get missed.
2291 static int arm_smmu_legacy_bus_init(void)
2293 if (using_legacy_binding)
2294 arm_smmu_bus_init();
2297 device_initcall_sync(arm_smmu_legacy_bus_init);
2299 static void arm_smmu_device_shutdown(struct platform_device *pdev)
2301 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
2306 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
2307 dev_err(&pdev->dev, "removing device with active domains!\n");
2309 arm_smmu_rpm_get(smmu);
2310 /* Turn the thing off */
2311 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, sCR0_CLIENTPD);
2312 arm_smmu_rpm_put(smmu);
2314 if (pm_runtime_enabled(smmu->dev))
2315 pm_runtime_force_suspend(smmu->dev);
2317 clk_bulk_disable(smmu->num_clks, smmu->clks);
2319 clk_bulk_unprepare(smmu->num_clks, smmu->clks);
2322 static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
2324 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2327 ret = clk_bulk_enable(smmu->num_clks, smmu->clks);
2331 arm_smmu_device_reset(smmu);
2336 static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
2338 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2340 clk_bulk_disable(smmu->num_clks, smmu->clks);
2345 static int __maybe_unused arm_smmu_pm_resume(struct device *dev)
2347 if (pm_runtime_suspended(dev))
2350 return arm_smmu_runtime_resume(dev);
2353 static int __maybe_unused arm_smmu_pm_suspend(struct device *dev)
2355 if (pm_runtime_suspended(dev))
2358 return arm_smmu_runtime_suspend(dev);
2361 static const struct dev_pm_ops arm_smmu_pm_ops = {
2362 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume)
2363 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend,
2364 arm_smmu_runtime_resume, NULL)
2367 static struct platform_driver arm_smmu_driver = {
2370 .of_match_table = of_match_ptr(arm_smmu_of_match),
2371 .pm = &arm_smmu_pm_ops,
2372 .suppress_bind_attrs = true,
2374 .probe = arm_smmu_device_probe,
2375 .shutdown = arm_smmu_device_shutdown,
2377 builtin_platform_driver(arm_smmu_driver);