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/iopoll.h>
30 #include <linux/init.h>
31 #include <linux/moduleparam.h>
33 #include <linux/of_address.h>
34 #include <linux/of_device.h>
35 #include <linux/of_iommu.h>
36 #include <linux/pci.h>
37 #include <linux/platform_device.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/ratelimit.h>
40 #include <linux/slab.h>
42 #include <linux/amba/bus.h>
43 #include <linux/fsl/mc.h>
48 * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU
49 * global register space are still, in fact, using a hypervisor to mediate it
50 * by trapping and emulating register accesses. Sadly, some deployed versions
51 * of said trapping code have bugs wherein they go horribly wrong for stores
52 * using r31 (i.e. XZR/WZR) as the source register.
54 #define QCOM_DUMMY_VAL -1
56 #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
57 #define TLB_SPIN_COUNT 10
59 #define MSI_IOVA_BASE 0x8000000
60 #define MSI_IOVA_LENGTH 0x100000
62 static int force_stage;
64 * not really modular, but the easiest way to keep compat with existing
65 * bootargs behaviour is to continue using module_param() here.
67 module_param(force_stage, int, S_IRUGO);
68 MODULE_PARM_DESC(force_stage,
69 "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.");
70 static bool disable_bypass =
71 IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT);
72 module_param(disable_bypass, bool, S_IRUGO);
73 MODULE_PARM_DESC(disable_bypass,
74 "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.");
76 struct arm_smmu_s2cr {
77 struct iommu_group *group;
79 enum arm_smmu_s2cr_type type;
80 enum arm_smmu_s2cr_privcfg privcfg;
84 #define s2cr_init_val (struct arm_smmu_s2cr){ \
85 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
98 struct arm_smmu_cfg *cfg;
101 struct arm_smmu_master_cfg {
102 struct arm_smmu_device *smmu;
105 #define INVALID_SMENDX -1
106 #define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
107 #define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
108 #define fwspec_smendx(fw, i) \
109 (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
110 #define for_each_cfg_sme(fw, i, idx) \
111 for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
113 static bool using_legacy_binding, using_generic_binding;
115 static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)
117 if (pm_runtime_enabled(smmu->dev))
118 return pm_runtime_get_sync(smmu->dev);
123 static inline void arm_smmu_rpm_put(struct arm_smmu_device *smmu)
125 if (pm_runtime_enabled(smmu->dev))
126 pm_runtime_put(smmu->dev);
129 static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
131 return container_of(dom, struct arm_smmu_domain, domain);
134 static struct device_node *dev_get_dev_node(struct device *dev)
136 if (dev_is_pci(dev)) {
137 struct pci_bus *bus = to_pci_dev(dev)->bus;
139 while (!pci_is_root_bus(bus))
141 return of_node_get(bus->bridge->parent->of_node);
144 return of_node_get(dev->of_node);
147 static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
149 *((__be32 *)data) = cpu_to_be32(alias);
150 return 0; /* Continue walking */
153 static int __find_legacy_master_phandle(struct device *dev, void *data)
155 struct of_phandle_iterator *it = *(void **)data;
156 struct device_node *np = it->node;
159 of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
160 "#stream-id-cells", -1)
161 if (it->node == np) {
162 *(void **)data = dev;
166 return err == -ENOENT ? 0 : err;
169 static struct platform_driver arm_smmu_driver;
170 static struct iommu_ops arm_smmu_ops;
172 static int arm_smmu_register_legacy_master(struct device *dev,
173 struct arm_smmu_device **smmu)
175 struct device *smmu_dev;
176 struct device_node *np;
177 struct of_phandle_iterator it;
183 np = dev_get_dev_node(dev);
184 if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
190 err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
191 __find_legacy_master_phandle);
199 if (dev_is_pci(dev)) {
200 /* "mmu-masters" assumes Stream ID == Requester ID */
201 pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
207 err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
212 sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
216 *smmu = dev_get_drvdata(smmu_dev);
217 of_phandle_iterator_args(&it, sids, it.cur_count);
218 err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
223 static int __arm_smmu_alloc_bitmap(unsigned long *map, int start, int end)
228 idx = find_next_zero_bit(map, end, start);
231 } while (test_and_set_bit(idx, map));
236 static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
241 /* Wait for any pending TLB invalidations to complete */
242 static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu, int page,
243 int sync, int status)
245 unsigned int spin_cnt, delay;
248 if (smmu->impl && unlikely(smmu->impl->tlb_sync))
249 return smmu->impl->tlb_sync(smmu, page, sync, status);
251 arm_smmu_writel(smmu, page, sync, QCOM_DUMMY_VAL);
252 for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
253 for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
254 reg = arm_smmu_readl(smmu, page, status);
255 if (!(reg & sTLBGSTATUS_GSACTIVE))
261 dev_err_ratelimited(smmu->dev,
262 "TLB sync timed out -- SMMU may be deadlocked\n");
265 static void arm_smmu_tlb_sync_global(struct arm_smmu_device *smmu)
269 spin_lock_irqsave(&smmu->global_sync_lock, flags);
270 __arm_smmu_tlb_sync(smmu, ARM_SMMU_GR0, ARM_SMMU_GR0_sTLBGSYNC,
271 ARM_SMMU_GR0_sTLBGSTATUS);
272 spin_unlock_irqrestore(&smmu->global_sync_lock, flags);
275 static void arm_smmu_tlb_sync_context(struct arm_smmu_domain *smmu_domain)
277 struct arm_smmu_device *smmu = smmu_domain->smmu;
280 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
281 __arm_smmu_tlb_sync(smmu, ARM_SMMU_CB(smmu, smmu_domain->cfg.cbndx),
282 ARM_SMMU_CB_TLBSYNC, ARM_SMMU_CB_TLBSTATUS);
283 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
286 static void arm_smmu_tlb_inv_context_s1(void *cookie)
288 struct arm_smmu_domain *smmu_domain = cookie;
290 * The TLBI write may be relaxed, so ensure that PTEs cleared by the
291 * current CPU are visible beforehand.
294 arm_smmu_cb_write(smmu_domain->smmu, smmu_domain->cfg.cbndx,
295 ARM_SMMU_CB_S1_TLBIASID, smmu_domain->cfg.asid);
296 arm_smmu_tlb_sync_context(smmu_domain);
299 static void arm_smmu_tlb_inv_context_s2(void *cookie)
301 struct arm_smmu_domain *smmu_domain = cookie;
302 struct arm_smmu_device *smmu = smmu_domain->smmu;
306 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
307 arm_smmu_tlb_sync_global(smmu);
310 static void arm_smmu_tlb_inv_range_s1(unsigned long iova, size_t size,
311 size_t granule, void *cookie, int reg)
313 struct arm_smmu_domain *smmu_domain = cookie;
314 struct arm_smmu_device *smmu = smmu_domain->smmu;
315 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
316 int idx = cfg->cbndx;
318 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
321 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
322 iova = (iova >> 12) << 12;
325 arm_smmu_cb_write(smmu, idx, reg, iova);
327 } while (size -= granule);
330 iova |= (u64)cfg->asid << 48;
332 arm_smmu_cb_writeq(smmu, idx, reg, iova);
333 iova += granule >> 12;
334 } while (size -= granule);
338 static void arm_smmu_tlb_inv_range_s2(unsigned long iova, size_t size,
339 size_t granule, void *cookie, int reg)
341 struct arm_smmu_domain *smmu_domain = cookie;
342 struct arm_smmu_device *smmu = smmu_domain->smmu;
343 int idx = smmu_domain->cfg.cbndx;
345 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
350 if (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64)
351 arm_smmu_cb_writeq(smmu, idx, reg, iova);
353 arm_smmu_cb_write(smmu, idx, reg, iova);
354 iova += granule >> 12;
355 } while (size -= granule);
358 static void arm_smmu_tlb_inv_walk_s1(unsigned long iova, size_t size,
359 size_t granule, void *cookie)
361 arm_smmu_tlb_inv_range_s1(iova, size, granule, cookie,
362 ARM_SMMU_CB_S1_TLBIVA);
363 arm_smmu_tlb_sync_context(cookie);
366 static void arm_smmu_tlb_inv_leaf_s1(unsigned long iova, size_t size,
367 size_t granule, void *cookie)
369 arm_smmu_tlb_inv_range_s1(iova, size, granule, cookie,
370 ARM_SMMU_CB_S1_TLBIVAL);
371 arm_smmu_tlb_sync_context(cookie);
374 static void arm_smmu_tlb_add_page_s1(struct iommu_iotlb_gather *gather,
375 unsigned long iova, size_t granule,
378 arm_smmu_tlb_inv_range_s1(iova, granule, granule, cookie,
379 ARM_SMMU_CB_S1_TLBIVAL);
382 static void arm_smmu_tlb_inv_walk_s2(unsigned long iova, size_t size,
383 size_t granule, void *cookie)
385 arm_smmu_tlb_inv_range_s2(iova, size, granule, cookie,
386 ARM_SMMU_CB_S2_TLBIIPAS2);
387 arm_smmu_tlb_sync_context(cookie);
390 static void arm_smmu_tlb_inv_leaf_s2(unsigned long iova, size_t size,
391 size_t granule, void *cookie)
393 arm_smmu_tlb_inv_range_s2(iova, size, granule, cookie,
394 ARM_SMMU_CB_S2_TLBIIPAS2L);
395 arm_smmu_tlb_sync_context(cookie);
398 static void arm_smmu_tlb_add_page_s2(struct iommu_iotlb_gather *gather,
399 unsigned long iova, size_t granule,
402 arm_smmu_tlb_inv_range_s2(iova, granule, granule, cookie,
403 ARM_SMMU_CB_S2_TLBIIPAS2L);
406 static void arm_smmu_tlb_inv_any_s2_v1(unsigned long iova, size_t size,
407 size_t granule, void *cookie)
409 arm_smmu_tlb_inv_context_s2(cookie);
412 * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
413 * almost negligible, but the benefit of getting the first one in as far ahead
414 * of the sync as possible is significant, hence we don't just make this a
415 * no-op and call arm_smmu_tlb_inv_context_s2() from .iotlb_sync as you might
418 static void arm_smmu_tlb_add_page_s2_v1(struct iommu_iotlb_gather *gather,
419 unsigned long iova, size_t granule,
422 struct arm_smmu_domain *smmu_domain = cookie;
423 struct arm_smmu_device *smmu = smmu_domain->smmu;
425 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
428 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
431 static const struct iommu_flush_ops arm_smmu_s1_tlb_ops = {
432 .tlb_flush_all = arm_smmu_tlb_inv_context_s1,
433 .tlb_flush_walk = arm_smmu_tlb_inv_walk_s1,
434 .tlb_flush_leaf = arm_smmu_tlb_inv_leaf_s1,
435 .tlb_add_page = arm_smmu_tlb_add_page_s1,
438 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v2 = {
439 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
440 .tlb_flush_walk = arm_smmu_tlb_inv_walk_s2,
441 .tlb_flush_leaf = arm_smmu_tlb_inv_leaf_s2,
442 .tlb_add_page = arm_smmu_tlb_add_page_s2,
445 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v1 = {
446 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
447 .tlb_flush_walk = arm_smmu_tlb_inv_any_s2_v1,
448 .tlb_flush_leaf = arm_smmu_tlb_inv_any_s2_v1,
449 .tlb_add_page = arm_smmu_tlb_add_page_s2_v1,
452 static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
454 u32 fsr, fsynr, cbfrsynra;
456 struct iommu_domain *domain = dev;
457 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
458 struct arm_smmu_device *smmu = smmu_domain->smmu;
459 int idx = smmu_domain->cfg.cbndx;
461 fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
462 if (!(fsr & FSR_FAULT))
465 fsynr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSYNR0);
466 iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
467 cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
469 dev_err_ratelimited(smmu->dev,
470 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
471 fsr, iova, fsynr, cbfrsynra, idx);
473 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
477 static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
479 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
480 struct arm_smmu_device *smmu = dev;
481 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
482 DEFAULT_RATELIMIT_BURST);
484 gfsr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
485 gfsynr0 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR0);
486 gfsynr1 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR1);
487 gfsynr2 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR2);
492 if (__ratelimit(&rs)) {
493 if (IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT) &&
496 "Blocked unknown Stream ID 0x%hx; boot with \"arm-smmu.disable_bypass=0\" to allow, but this may have security implications\n",
500 "Unexpected global fault, this could be serious\n");
502 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
503 gfsr, gfsynr0, gfsynr1, gfsynr2);
506 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, gfsr);
510 static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
511 struct io_pgtable_cfg *pgtbl_cfg)
513 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
514 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
515 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
521 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
522 cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr;
524 cb->tcr[0] = pgtbl_cfg->arm_lpae_s1_cfg.tcr;
525 cb->tcr[1] = pgtbl_cfg->arm_lpae_s1_cfg.tcr >> 32;
526 cb->tcr[1] |= FIELD_PREP(TCR2_SEP, TCR2_SEP_UPSTREAM);
527 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
528 cb->tcr[1] |= TCR2_AS;
531 cb->tcr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vtcr;
536 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
537 cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr[0];
538 cb->ttbr[1] = pgtbl_cfg->arm_v7s_cfg.ttbr[1];
540 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[0];
541 cb->ttbr[0] |= FIELD_PREP(TTBRn_ASID, cfg->asid);
542 cb->ttbr[1] = pgtbl_cfg->arm_lpae_s1_cfg.ttbr[1];
543 cb->ttbr[1] |= FIELD_PREP(TTBRn_ASID, cfg->asid);
546 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
549 /* MAIRs (stage-1 only) */
551 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
552 cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr;
553 cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr;
555 cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair[0];
556 cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair[1];
561 static void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
565 struct arm_smmu_cb *cb = &smmu->cbs[idx];
566 struct arm_smmu_cfg *cfg = cb->cfg;
568 /* Unassigned context banks only need disabling */
570 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, 0);
574 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
577 if (smmu->version > ARM_SMMU_V1) {
578 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
582 /* 16-bit VMIDs live in CBA2R */
583 if (smmu->features & ARM_SMMU_FEAT_VMID16)
584 reg |= FIELD_PREP(CBA2R_VMID16, cfg->vmid);
586 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBA2R(idx), reg);
590 reg = FIELD_PREP(CBAR_TYPE, cfg->cbar);
591 if (smmu->version < ARM_SMMU_V2)
592 reg |= FIELD_PREP(CBAR_IRPTNDX, cfg->irptndx);
595 * Use the weakest shareability/memory types, so they are
596 * overridden by the ttbcr/pte.
599 reg |= FIELD_PREP(CBAR_S1_BPSHCFG, CBAR_S1_BPSHCFG_NSH) |
600 FIELD_PREP(CBAR_S1_MEMATTR, CBAR_S1_MEMATTR_WB);
601 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
602 /* 8-bit VMIDs live in CBAR */
603 reg |= FIELD_PREP(CBAR_VMID, cfg->vmid);
605 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(idx), reg);
609 * We must write this before the TTBRs, since it determines the
610 * access behaviour of some fields (in particular, ASID[15:8]).
612 if (stage1 && smmu->version > ARM_SMMU_V1)
613 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR2, cb->tcr[1]);
614 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR, cb->tcr[0]);
617 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
618 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_CONTEXTIDR, cfg->asid);
619 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
620 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR1, cb->ttbr[1]);
622 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
624 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR1,
628 /* MAIRs (stage-1 only) */
630 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR0, cb->mair[0]);
631 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR1, cb->mair[1]);
635 reg = SCTLR_CFIE | SCTLR_CFRE | SCTLR_AFE | SCTLR_TRE | SCTLR_M;
637 reg |= SCTLR_S1_ASIDPNE;
638 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
641 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
644 static int arm_smmu_init_domain_context(struct iommu_domain *domain,
645 struct arm_smmu_device *smmu)
647 int irq, start, ret = 0;
648 unsigned long ias, oas;
649 struct io_pgtable_ops *pgtbl_ops;
650 struct io_pgtable_cfg pgtbl_cfg;
651 enum io_pgtable_fmt fmt;
652 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
653 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
655 mutex_lock(&smmu_domain->init_mutex);
656 if (smmu_domain->smmu)
659 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
660 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
661 smmu_domain->smmu = smmu;
666 * Mapping the requested stage onto what we support is surprisingly
667 * complicated, mainly because the spec allows S1+S2 SMMUs without
668 * support for nested translation. That means we end up with the
671 * Requested Supported Actual
681 * Note that you can't actually request stage-2 mappings.
683 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
684 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
685 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
686 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
689 * Choosing a suitable context format is even more fiddly. Until we
690 * grow some way for the caller to express a preference, and/or move
691 * the decision into the io-pgtable code where it arguably belongs,
692 * just aim for the closest thing to the rest of the system, and hope
693 * that the hardware isn't esoteric enough that we can't assume AArch64
694 * support to be a superset of AArch32 support...
696 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
697 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
698 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
699 !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
700 (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
701 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
702 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
703 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
704 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
705 ARM_SMMU_FEAT_FMT_AARCH64_16K |
706 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
707 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
709 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
714 switch (smmu_domain->stage) {
715 case ARM_SMMU_DOMAIN_S1:
716 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
717 start = smmu->num_s2_context_banks;
719 oas = smmu->ipa_size;
720 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
721 fmt = ARM_64_LPAE_S1;
722 } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
723 fmt = ARM_32_LPAE_S1;
724 ias = min(ias, 32UL);
725 oas = min(oas, 40UL);
728 ias = min(ias, 32UL);
729 oas = min(oas, 32UL);
731 smmu_domain->flush_ops = &arm_smmu_s1_tlb_ops;
733 case ARM_SMMU_DOMAIN_NESTED:
735 * We will likely want to change this if/when KVM gets
738 case ARM_SMMU_DOMAIN_S2:
739 cfg->cbar = CBAR_TYPE_S2_TRANS;
741 ias = smmu->ipa_size;
743 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
744 fmt = ARM_64_LPAE_S2;
746 fmt = ARM_32_LPAE_S2;
747 ias = min(ias, 40UL);
748 oas = min(oas, 40UL);
750 if (smmu->version == ARM_SMMU_V2)
751 smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v2;
753 smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v1;
759 ret = __arm_smmu_alloc_bitmap(smmu->context_map, start,
760 smmu->num_context_banks);
765 if (smmu->version < ARM_SMMU_V2) {
766 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
767 cfg->irptndx %= smmu->num_context_irqs;
769 cfg->irptndx = cfg->cbndx;
772 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
773 cfg->vmid = cfg->cbndx + 1;
775 cfg->asid = cfg->cbndx;
777 smmu_domain->smmu = smmu;
778 if (smmu->impl && smmu->impl->init_context) {
779 ret = smmu->impl->init_context(smmu_domain);
784 pgtbl_cfg = (struct io_pgtable_cfg) {
785 .pgsize_bitmap = smmu->pgsize_bitmap,
788 .coherent_walk = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK,
789 .tlb = smmu_domain->flush_ops,
790 .iommu_dev = smmu->dev,
793 if (smmu_domain->non_strict)
794 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT;
796 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
802 /* Update the domain's page sizes to reflect the page table format */
803 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
804 domain->geometry.aperture_end = (1UL << ias) - 1;
805 domain->geometry.force_aperture = true;
807 /* Initialise the context bank with our page table cfg */
808 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
809 arm_smmu_write_context_bank(smmu, cfg->cbndx);
812 * Request context fault interrupt. Do this last to avoid the
813 * handler seeing a half-initialised domain state.
815 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
816 ret = devm_request_irq(smmu->dev, irq, arm_smmu_context_fault,
817 IRQF_SHARED, "arm-smmu-context-fault", domain);
819 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
821 cfg->irptndx = INVALID_IRPTNDX;
824 mutex_unlock(&smmu_domain->init_mutex);
826 /* Publish page table ops for map/unmap */
827 smmu_domain->pgtbl_ops = pgtbl_ops;
831 smmu_domain->smmu = NULL;
833 mutex_unlock(&smmu_domain->init_mutex);
837 static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
839 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
840 struct arm_smmu_device *smmu = smmu_domain->smmu;
841 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
844 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
847 ret = arm_smmu_rpm_get(smmu);
852 * Disable the context bank and free the page tables before freeing
855 smmu->cbs[cfg->cbndx].cfg = NULL;
856 arm_smmu_write_context_bank(smmu, cfg->cbndx);
858 if (cfg->irptndx != INVALID_IRPTNDX) {
859 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
860 devm_free_irq(smmu->dev, irq, domain);
863 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
864 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
866 arm_smmu_rpm_put(smmu);
869 static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
871 struct arm_smmu_domain *smmu_domain;
873 if (type != IOMMU_DOMAIN_UNMANAGED &&
874 type != IOMMU_DOMAIN_DMA &&
875 type != IOMMU_DOMAIN_IDENTITY)
878 * Allocate the domain and initialise some of its data structures.
879 * We can't really do anything meaningful until we've added a
882 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
886 if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
887 iommu_get_dma_cookie(&smmu_domain->domain))) {
892 mutex_init(&smmu_domain->init_mutex);
893 spin_lock_init(&smmu_domain->cb_lock);
895 return &smmu_domain->domain;
898 static void arm_smmu_domain_free(struct iommu_domain *domain)
900 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
903 * Free the domain resources. We assume that all devices have
904 * already been detached.
906 iommu_put_dma_cookie(domain);
907 arm_smmu_destroy_domain_context(domain);
911 static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
913 struct arm_smmu_smr *smr = smmu->smrs + idx;
914 u32 reg = FIELD_PREP(SMR_ID, smr->id) | FIELD_PREP(SMR_MASK, smr->mask);
916 if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
918 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(idx), reg);
921 static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
923 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
924 u32 reg = FIELD_PREP(S2CR_TYPE, s2cr->type) |
925 FIELD_PREP(S2CR_CBNDX, s2cr->cbndx) |
926 FIELD_PREP(S2CR_PRIVCFG, s2cr->privcfg);
928 if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
929 smmu->smrs[idx].valid)
930 reg |= S2CR_EXIDVALID;
931 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
934 static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
936 arm_smmu_write_s2cr(smmu, idx);
938 arm_smmu_write_smr(smmu, idx);
942 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
943 * should be called after sCR0 is written.
945 static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
953 * SMR.ID bits may not be preserved if the corresponding MASK
954 * bits are set, so check each one separately. We can reject
955 * masters later if they try to claim IDs outside these masks.
957 smr = FIELD_PREP(SMR_ID, smmu->streamid_mask);
958 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(0), smr);
959 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(0));
960 smmu->streamid_mask = FIELD_GET(SMR_ID, smr);
962 smr = FIELD_PREP(SMR_MASK, smmu->streamid_mask);
963 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(0), smr);
964 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(0));
965 smmu->smr_mask_mask = FIELD_GET(SMR_MASK, smr);
968 static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
970 struct arm_smmu_smr *smrs = smmu->smrs;
971 int i, free_idx = -ENOSPC;
973 /* Stream indexing is blissfully easy */
977 /* Validating SMRs is... less so */
978 for (i = 0; i < smmu->num_mapping_groups; ++i) {
979 if (!smrs[i].valid) {
981 * Note the first free entry we come across, which
982 * we'll claim in the end if nothing else matches.
989 * If the new entry is _entirely_ matched by an existing entry,
990 * then reuse that, with the guarantee that there also cannot
991 * be any subsequent conflicting entries. In normal use we'd
992 * expect simply identical entries for this case, but there's
993 * no harm in accommodating the generalisation.
995 if ((mask & smrs[i].mask) == mask &&
996 !((id ^ smrs[i].id) & ~smrs[i].mask))
999 * If the new entry has any other overlap with an existing one,
1000 * though, then there always exists at least one stream ID
1001 * which would cause a conflict, and we can't allow that risk.
1003 if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1010 static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1012 if (--smmu->s2crs[idx].count)
1015 smmu->s2crs[idx] = s2cr_init_val;
1017 smmu->smrs[idx].valid = false;
1022 static int arm_smmu_master_alloc_smes(struct device *dev)
1024 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1025 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
1026 struct arm_smmu_device *smmu = cfg->smmu;
1027 struct arm_smmu_smr *smrs = smmu->smrs;
1028 struct iommu_group *group;
1031 mutex_lock(&smmu->stream_map_mutex);
1032 /* Figure out a viable stream map entry allocation */
1033 for_each_cfg_sme(fwspec, i, idx) {
1034 u16 sid = FIELD_GET(SMR_ID, fwspec->ids[i]);
1035 u16 mask = FIELD_GET(SMR_MASK, fwspec->ids[i]);
1037 if (idx != INVALID_SMENDX) {
1042 ret = arm_smmu_find_sme(smmu, sid, mask);
1047 if (smrs && smmu->s2crs[idx].count == 0) {
1049 smrs[idx].mask = mask;
1050 smrs[idx].valid = true;
1052 smmu->s2crs[idx].count++;
1053 cfg->smendx[i] = (s16)idx;
1056 group = iommu_group_get_for_dev(dev);
1058 group = ERR_PTR(-ENOMEM);
1059 if (IS_ERR(group)) {
1060 ret = PTR_ERR(group);
1063 iommu_group_put(group);
1065 /* It worked! Now, poke the actual hardware */
1066 for_each_cfg_sme(fwspec, i, idx) {
1067 arm_smmu_write_sme(smmu, idx);
1068 smmu->s2crs[idx].group = group;
1071 mutex_unlock(&smmu->stream_map_mutex);
1076 arm_smmu_free_sme(smmu, cfg->smendx[i]);
1077 cfg->smendx[i] = INVALID_SMENDX;
1079 mutex_unlock(&smmu->stream_map_mutex);
1083 static void arm_smmu_master_free_smes(struct iommu_fwspec *fwspec)
1085 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
1086 struct arm_smmu_master_cfg *cfg = fwspec->iommu_priv;
1089 mutex_lock(&smmu->stream_map_mutex);
1090 for_each_cfg_sme(fwspec, i, idx) {
1091 if (arm_smmu_free_sme(smmu, idx))
1092 arm_smmu_write_sme(smmu, idx);
1093 cfg->smendx[i] = INVALID_SMENDX;
1095 mutex_unlock(&smmu->stream_map_mutex);
1098 static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1099 struct iommu_fwspec *fwspec)
1101 struct arm_smmu_device *smmu = smmu_domain->smmu;
1102 struct arm_smmu_s2cr *s2cr = smmu->s2crs;
1103 u8 cbndx = smmu_domain->cfg.cbndx;
1104 enum arm_smmu_s2cr_type type;
1107 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1108 type = S2CR_TYPE_BYPASS;
1110 type = S2CR_TYPE_TRANS;
1112 for_each_cfg_sme(fwspec, i, idx) {
1113 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
1116 s2cr[idx].type = type;
1117 s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
1118 s2cr[idx].cbndx = cbndx;
1119 arm_smmu_write_s2cr(smmu, idx);
1124 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1127 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1128 struct arm_smmu_device *smmu;
1129 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1131 if (!fwspec || fwspec->ops != &arm_smmu_ops) {
1132 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1137 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1138 * domains between of_xlate() and add_device() - we have no way to cope
1139 * with that, so until ARM gets converted to rely on groups and default
1140 * domains, just say no (but more politely than by dereferencing NULL).
1141 * This should be at least a WARN_ON once that's sorted.
1143 if (!fwspec->iommu_priv)
1146 smmu = fwspec_smmu(fwspec);
1148 ret = arm_smmu_rpm_get(smmu);
1152 /* Ensure that the domain is finalised */
1153 ret = arm_smmu_init_domain_context(domain, smmu);
1158 * Sanity check the domain. We don't support domains across
1161 if (smmu_domain->smmu != smmu) {
1163 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1164 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1169 /* Looks ok, so add the device to the domain */
1170 ret = arm_smmu_domain_add_master(smmu_domain, fwspec);
1173 arm_smmu_rpm_put(smmu);
1177 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1178 phys_addr_t paddr, size_t size, int prot)
1180 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1181 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1187 arm_smmu_rpm_get(smmu);
1188 ret = ops->map(ops, iova, paddr, size, prot);
1189 arm_smmu_rpm_put(smmu);
1194 static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1195 size_t size, struct iommu_iotlb_gather *gather)
1197 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1198 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1204 arm_smmu_rpm_get(smmu);
1205 ret = ops->unmap(ops, iova, size, gather);
1206 arm_smmu_rpm_put(smmu);
1211 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
1213 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1214 struct arm_smmu_device *smmu = smmu_domain->smmu;
1216 if (smmu_domain->flush_ops) {
1217 arm_smmu_rpm_get(smmu);
1218 smmu_domain->flush_ops->tlb_flush_all(smmu_domain);
1219 arm_smmu_rpm_put(smmu);
1223 static void arm_smmu_iotlb_sync(struct iommu_domain *domain,
1224 struct iommu_iotlb_gather *gather)
1226 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1227 struct arm_smmu_device *smmu = smmu_domain->smmu;
1232 arm_smmu_rpm_get(smmu);
1233 if (smmu->version == ARM_SMMU_V2 ||
1234 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1235 arm_smmu_tlb_sync_context(smmu_domain);
1237 arm_smmu_tlb_sync_global(smmu);
1238 arm_smmu_rpm_put(smmu);
1241 static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1244 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1245 struct arm_smmu_device *smmu = smmu_domain->smmu;
1246 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1247 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1248 struct device *dev = smmu->dev;
1252 unsigned long va, flags;
1253 int ret, idx = cfg->cbndx;
1255 ret = arm_smmu_rpm_get(smmu);
1259 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
1260 va = iova & ~0xfffUL;
1261 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
1262 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1264 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1266 reg = arm_smmu_page(smmu, ARM_SMMU_CB(smmu, idx)) + ARM_SMMU_CB_ATSR;
1267 if (readl_poll_timeout_atomic(reg, tmp, !(tmp & ATSR_ACTIVE), 5, 50)) {
1268 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1270 "iova to phys timed out on %pad. Falling back to software table walk.\n",
1272 return ops->iova_to_phys(ops, iova);
1275 phys = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_PAR);
1276 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1277 if (phys & CB_PAR_F) {
1278 dev_err(dev, "translation fault!\n");
1279 dev_err(dev, "PAR = 0x%llx\n", phys);
1283 arm_smmu_rpm_put(smmu);
1285 return (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1288 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1291 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1292 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1294 if (domain->type == IOMMU_DOMAIN_IDENTITY)
1300 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1301 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1302 return arm_smmu_iova_to_phys_hard(domain, iova);
1304 return ops->iova_to_phys(ops, iova);
1307 static bool arm_smmu_capable(enum iommu_cap cap)
1310 case IOMMU_CAP_CACHE_COHERENCY:
1312 * Return true here as the SMMU can always send out coherent
1316 case IOMMU_CAP_NOEXEC:
1324 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
1326 struct device *dev = driver_find_device_by_fwnode(&arm_smmu_driver.driver,
1329 return dev ? dev_get_drvdata(dev) : NULL;
1332 static int arm_smmu_add_device(struct device *dev)
1334 struct arm_smmu_device *smmu;
1335 struct arm_smmu_master_cfg *cfg;
1336 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1339 if (using_legacy_binding) {
1340 ret = arm_smmu_register_legacy_master(dev, &smmu);
1343 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1344 * will allocate/initialise a new one. Thus we need to update fwspec for
1347 fwspec = dev_iommu_fwspec_get(dev);
1350 } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
1351 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
1357 for (i = 0; i < fwspec->num_ids; i++) {
1358 u16 sid = FIELD_GET(SMR_ID, fwspec->ids[i]);
1359 u16 mask = FIELD_GET(SMR_MASK, fwspec->ids[i]);
1361 if (sid & ~smmu->streamid_mask) {
1362 dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
1363 sid, smmu->streamid_mask);
1366 if (mask & ~smmu->smr_mask_mask) {
1367 dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1368 mask, smmu->smr_mask_mask);
1374 cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1380 fwspec->iommu_priv = cfg;
1382 cfg->smendx[i] = INVALID_SMENDX;
1384 ret = arm_smmu_rpm_get(smmu);
1388 ret = arm_smmu_master_alloc_smes(dev);
1389 arm_smmu_rpm_put(smmu);
1394 iommu_device_link(&smmu->iommu, dev);
1396 device_link_add(dev, smmu->dev,
1397 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
1404 iommu_fwspec_free(dev);
1408 static void arm_smmu_remove_device(struct device *dev)
1410 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1411 struct arm_smmu_master_cfg *cfg;
1412 struct arm_smmu_device *smmu;
1415 if (!fwspec || fwspec->ops != &arm_smmu_ops)
1418 cfg = fwspec->iommu_priv;
1421 ret = arm_smmu_rpm_get(smmu);
1425 iommu_device_unlink(&smmu->iommu, dev);
1426 arm_smmu_master_free_smes(fwspec);
1428 arm_smmu_rpm_put(smmu);
1430 iommu_group_remove_device(dev);
1431 kfree(fwspec->iommu_priv);
1432 iommu_fwspec_free(dev);
1435 static struct iommu_group *arm_smmu_device_group(struct device *dev)
1437 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1438 struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
1439 struct iommu_group *group = NULL;
1442 for_each_cfg_sme(fwspec, i, idx) {
1443 if (group && smmu->s2crs[idx].group &&
1444 group != smmu->s2crs[idx].group)
1445 return ERR_PTR(-EINVAL);
1447 group = smmu->s2crs[idx].group;
1451 return iommu_group_ref_get(group);
1453 if (dev_is_pci(dev))
1454 group = pci_device_group(dev);
1455 else if (dev_is_fsl_mc(dev))
1456 group = fsl_mc_device_group(dev);
1458 group = generic_device_group(dev);
1463 static int arm_smmu_domain_get_attr(struct iommu_domain *domain,
1464 enum iommu_attr attr, void *data)
1466 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1468 switch(domain->type) {
1469 case IOMMU_DOMAIN_UNMANAGED:
1471 case DOMAIN_ATTR_NESTING:
1472 *(int *)data = (smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED);
1478 case IOMMU_DOMAIN_DMA:
1480 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1481 *(int *)data = smmu_domain->non_strict;
1492 static int arm_smmu_domain_set_attr(struct iommu_domain *domain,
1493 enum iommu_attr attr, void *data)
1496 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1498 mutex_lock(&smmu_domain->init_mutex);
1500 switch(domain->type) {
1501 case IOMMU_DOMAIN_UNMANAGED:
1503 case DOMAIN_ATTR_NESTING:
1504 if (smmu_domain->smmu) {
1510 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1512 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
1518 case IOMMU_DOMAIN_DMA:
1520 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE:
1521 smmu_domain->non_strict = *(int *)data;
1531 mutex_unlock(&smmu_domain->init_mutex);
1535 static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1539 if (args->args_count > 0)
1540 fwid |= FIELD_PREP(SMR_ID, args->args[0]);
1542 if (args->args_count > 1)
1543 fwid |= FIELD_PREP(SMR_MASK, args->args[1]);
1544 else if (!of_property_read_u32(args->np, "stream-match-mask", &mask))
1545 fwid |= FIELD_PREP(SMR_MASK, mask);
1547 return iommu_fwspec_add_ids(dev, &fwid, 1);
1550 static void arm_smmu_get_resv_regions(struct device *dev,
1551 struct list_head *head)
1553 struct iommu_resv_region *region;
1554 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1556 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
1557 prot, IOMMU_RESV_SW_MSI);
1561 list_add_tail(®ion->list, head);
1563 iommu_dma_get_resv_regions(dev, head);
1566 static void arm_smmu_put_resv_regions(struct device *dev,
1567 struct list_head *head)
1569 struct iommu_resv_region *entry, *next;
1571 list_for_each_entry_safe(entry, next, head, list)
1575 static struct iommu_ops arm_smmu_ops = {
1576 .capable = arm_smmu_capable,
1577 .domain_alloc = arm_smmu_domain_alloc,
1578 .domain_free = arm_smmu_domain_free,
1579 .attach_dev = arm_smmu_attach_dev,
1580 .map = arm_smmu_map,
1581 .unmap = arm_smmu_unmap,
1582 .flush_iotlb_all = arm_smmu_flush_iotlb_all,
1583 .iotlb_sync = arm_smmu_iotlb_sync,
1584 .iova_to_phys = arm_smmu_iova_to_phys,
1585 .add_device = arm_smmu_add_device,
1586 .remove_device = arm_smmu_remove_device,
1587 .device_group = arm_smmu_device_group,
1588 .domain_get_attr = arm_smmu_domain_get_attr,
1589 .domain_set_attr = arm_smmu_domain_set_attr,
1590 .of_xlate = arm_smmu_of_xlate,
1591 .get_resv_regions = arm_smmu_get_resv_regions,
1592 .put_resv_regions = arm_smmu_put_resv_regions,
1593 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1596 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1601 /* clear global FSR */
1602 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
1603 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, reg);
1606 * Reset stream mapping groups: Initial values mark all SMRn as
1607 * invalid and all S2CRn as bypass unless overridden.
1609 for (i = 0; i < smmu->num_mapping_groups; ++i)
1610 arm_smmu_write_sme(smmu, i);
1612 /* Make sure all context banks are disabled and clear CB_FSR */
1613 for (i = 0; i < smmu->num_context_banks; ++i) {
1614 arm_smmu_write_context_bank(smmu, i);
1615 arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_FSR, FSR_FAULT);
1618 /* Invalidate the TLB, just in case */
1619 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLH, QCOM_DUMMY_VAL);
1620 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLNSNH, QCOM_DUMMY_VAL);
1622 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sCR0);
1624 /* Enable fault reporting */
1625 reg |= (sCR0_GFRE | sCR0_GFIE | sCR0_GCFGFRE | sCR0_GCFGFIE);
1627 /* Disable TLB broadcasting. */
1628 reg |= (sCR0_VMIDPNE | sCR0_PTM);
1630 /* Enable client access, handling unmatched streams as appropriate */
1631 reg &= ~sCR0_CLIENTPD;
1635 reg &= ~sCR0_USFCFG;
1637 /* Disable forced broadcasting */
1640 /* Don't upgrade barriers */
1643 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1644 reg |= sCR0_VMID16EN;
1646 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
1647 reg |= sCR0_EXIDENABLE;
1649 if (smmu->impl && smmu->impl->reset)
1650 smmu->impl->reset(smmu);
1652 /* Push the button */
1653 arm_smmu_tlb_sync_global(smmu);
1654 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, reg);
1657 static int arm_smmu_id_size_to_bits(int size)
1676 static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1680 bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
1683 dev_notice(smmu->dev, "probing hardware configuration...\n");
1684 dev_notice(smmu->dev, "SMMUv%d with:\n",
1685 smmu->version == ARM_SMMU_V2 ? 2 : 1);
1688 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID0);
1690 /* Restrict available stages based on module parameter */
1691 if (force_stage == 1)
1692 id &= ~(ID0_S2TS | ID0_NTS);
1693 else if (force_stage == 2)
1694 id &= ~(ID0_S1TS | ID0_NTS);
1696 if (id & ID0_S1TS) {
1697 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1698 dev_notice(smmu->dev, "\tstage 1 translation\n");
1701 if (id & ID0_S2TS) {
1702 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1703 dev_notice(smmu->dev, "\tstage 2 translation\n");
1707 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1708 dev_notice(smmu->dev, "\tnested translation\n");
1711 if (!(smmu->features &
1712 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
1713 dev_err(smmu->dev, "\tno translation support!\n");
1717 if ((id & ID0_S1TS) &&
1718 ((smmu->version < ARM_SMMU_V2) || !(id & ID0_ATOSNS))) {
1719 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1720 dev_notice(smmu->dev, "\taddress translation ops\n");
1724 * In order for DMA API calls to work properly, we must defer to what
1725 * the FW says about coherency, regardless of what the hardware claims.
1726 * Fortunately, this also opens up a workaround for systems where the
1727 * ID register value has ended up configured incorrectly.
1729 cttw_reg = !!(id & ID0_CTTW);
1730 if (cttw_fw || cttw_reg)
1731 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1732 cttw_fw ? "" : "non-");
1733 if (cttw_fw != cttw_reg)
1734 dev_notice(smmu->dev,
1735 "\t(IDR0.CTTW overridden by FW configuration)\n");
1737 /* Max. number of entries we have for stream matching/indexing */
1738 if (smmu->version == ARM_SMMU_V2 && id & ID0_EXIDS) {
1739 smmu->features |= ARM_SMMU_FEAT_EXIDS;
1742 size = 1 << FIELD_GET(ID0_NUMSIDB, id);
1744 smmu->streamid_mask = size - 1;
1746 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1747 size = FIELD_GET(ID0_NUMSMRG, id);
1750 "stream-matching supported, but no SMRs present!\n");
1754 /* Zero-initialised to mark as invalid */
1755 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1760 dev_notice(smmu->dev,
1761 "\tstream matching with %u register groups", size);
1763 /* s2cr->type == 0 means translation, so initialise explicitly */
1764 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1768 for (i = 0; i < size; i++)
1769 smmu->s2crs[i] = s2cr_init_val;
1771 smmu->num_mapping_groups = size;
1772 mutex_init(&smmu->stream_map_mutex);
1773 spin_lock_init(&smmu->global_sync_lock);
1775 if (smmu->version < ARM_SMMU_V2 || !(id & ID0_PTFS_NO_AARCH32)) {
1776 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1777 if (!(id & ID0_PTFS_NO_AARCH32S))
1778 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1782 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID1);
1783 smmu->pgshift = (id & ID1_PAGESIZE) ? 16 : 12;
1785 /* Check for size mismatch of SMMU address space from mapped region */
1786 size = 1 << (FIELD_GET(ID1_NUMPAGENDXB, id) + 1);
1787 if (smmu->numpage != 2 * size << smmu->pgshift)
1789 "SMMU address space size (0x%x) differs from mapped region size (0x%x)!\n",
1790 2 * size << smmu->pgshift, smmu->numpage);
1791 /* Now properly encode NUMPAGE to subsequently derive SMMU_CB_BASE */
1792 smmu->numpage = size;
1794 smmu->num_s2_context_banks = FIELD_GET(ID1_NUMS2CB, id);
1795 smmu->num_context_banks = FIELD_GET(ID1_NUMCB, id);
1796 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1797 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1800 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1801 smmu->num_context_banks, smmu->num_s2_context_banks);
1802 smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
1803 sizeof(*smmu->cbs), GFP_KERNEL);
1808 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID2);
1809 size = arm_smmu_id_size_to_bits(FIELD_GET(ID2_IAS, id));
1810 smmu->ipa_size = size;
1812 /* The output mask is also applied for bypass */
1813 size = arm_smmu_id_size_to_bits(FIELD_GET(ID2_OAS, id));
1814 smmu->pa_size = size;
1816 if (id & ID2_VMID16)
1817 smmu->features |= ARM_SMMU_FEAT_VMID16;
1820 * What the page table walker can address actually depends on which
1821 * descriptor format is in use, but since a) we don't know that yet,
1822 * and b) it can vary per context bank, this will have to do...
1824 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1826 "failed to set DMA mask for table walker\n");
1828 if (smmu->version < ARM_SMMU_V2) {
1829 smmu->va_size = smmu->ipa_size;
1830 if (smmu->version == ARM_SMMU_V1_64K)
1831 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1833 size = FIELD_GET(ID2_UBS, id);
1834 smmu->va_size = arm_smmu_id_size_to_bits(size);
1835 if (id & ID2_PTFS_4K)
1836 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
1837 if (id & ID2_PTFS_16K)
1838 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
1839 if (id & ID2_PTFS_64K)
1840 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1843 /* Now we've corralled the various formats, what'll it do? */
1844 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
1845 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
1846 if (smmu->features &
1847 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
1848 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
1849 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
1850 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
1851 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
1852 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
1854 if (arm_smmu_ops.pgsize_bitmap == -1UL)
1855 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1857 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1858 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1859 smmu->pgsize_bitmap);
1862 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1863 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1864 smmu->va_size, smmu->ipa_size);
1866 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1867 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1868 smmu->ipa_size, smmu->pa_size);
1870 if (smmu->impl && smmu->impl->cfg_probe)
1871 return smmu->impl->cfg_probe(smmu);
1876 struct arm_smmu_match_data {
1877 enum arm_smmu_arch_version version;
1878 enum arm_smmu_implementation model;
1881 #define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1882 static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
1884 ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1885 ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
1886 ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
1887 ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
1888 ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
1889 ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2);
1891 static const struct of_device_id arm_smmu_of_match[] = {
1892 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
1893 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
1894 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
1895 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
1896 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
1897 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
1898 { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
1903 static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
1908 case ACPI_IORT_SMMU_V1:
1909 case ACPI_IORT_SMMU_CORELINK_MMU400:
1910 smmu->version = ARM_SMMU_V1;
1911 smmu->model = GENERIC_SMMU;
1913 case ACPI_IORT_SMMU_CORELINK_MMU401:
1914 smmu->version = ARM_SMMU_V1_64K;
1915 smmu->model = GENERIC_SMMU;
1917 case ACPI_IORT_SMMU_V2:
1918 smmu->version = ARM_SMMU_V2;
1919 smmu->model = GENERIC_SMMU;
1921 case ACPI_IORT_SMMU_CORELINK_MMU500:
1922 smmu->version = ARM_SMMU_V2;
1923 smmu->model = ARM_MMU500;
1925 case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
1926 smmu->version = ARM_SMMU_V2;
1927 smmu->model = CAVIUM_SMMUV2;
1936 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1937 struct arm_smmu_device *smmu)
1939 struct device *dev = smmu->dev;
1940 struct acpi_iort_node *node =
1941 *(struct acpi_iort_node **)dev_get_platdata(dev);
1942 struct acpi_iort_smmu *iort_smmu;
1945 /* Retrieve SMMU1/2 specific data */
1946 iort_smmu = (struct acpi_iort_smmu *)node->node_data;
1948 ret = acpi_smmu_get_data(iort_smmu->model, smmu);
1952 /* Ignore the configuration access interrupt */
1953 smmu->num_global_irqs = 1;
1955 if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
1956 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1961 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1962 struct arm_smmu_device *smmu)
1968 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
1969 struct arm_smmu_device *smmu)
1971 const struct arm_smmu_match_data *data;
1972 struct device *dev = &pdev->dev;
1973 bool legacy_binding;
1975 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1976 &smmu->num_global_irqs)) {
1977 dev_err(dev, "missing #global-interrupts property\n");
1981 data = of_device_get_match_data(dev);
1982 smmu->version = data->version;
1983 smmu->model = data->model;
1985 legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
1986 if (legacy_binding && !using_generic_binding) {
1987 if (!using_legacy_binding)
1988 pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
1989 using_legacy_binding = true;
1990 } else if (!legacy_binding && !using_legacy_binding) {
1991 using_generic_binding = true;
1993 dev_err(dev, "not probing due to mismatched DT properties\n");
1997 if (of_dma_is_coherent(dev->of_node))
1998 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2003 static void arm_smmu_bus_init(void)
2005 /* Oh, for a proper bus abstraction */
2006 if (!iommu_present(&platform_bus_type))
2007 bus_set_iommu(&platform_bus_type, &arm_smmu_ops);
2008 #ifdef CONFIG_ARM_AMBA
2009 if (!iommu_present(&amba_bustype))
2010 bus_set_iommu(&amba_bustype, &arm_smmu_ops);
2013 if (!iommu_present(&pci_bus_type)) {
2015 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
2018 #ifdef CONFIG_FSL_MC_BUS
2019 if (!iommu_present(&fsl_mc_bus_type))
2020 bus_set_iommu(&fsl_mc_bus_type, &arm_smmu_ops);
2024 static int arm_smmu_device_probe(struct platform_device *pdev)
2026 struct resource *res;
2027 resource_size_t ioaddr;
2028 struct arm_smmu_device *smmu;
2029 struct device *dev = &pdev->dev;
2030 int num_irqs, i, err;
2032 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2034 dev_err(dev, "failed to allocate arm_smmu_device\n");
2040 err = arm_smmu_device_dt_probe(pdev, smmu);
2042 err = arm_smmu_device_acpi_probe(pdev, smmu);
2047 smmu = arm_smmu_impl_init(smmu);
2049 return PTR_ERR(smmu);
2051 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2052 ioaddr = res->start;
2053 smmu->base = devm_ioremap_resource(dev, res);
2054 if (IS_ERR(smmu->base))
2055 return PTR_ERR(smmu->base);
2057 * The resource size should effectively match the value of SMMU_TOP;
2058 * stash that temporarily until we know PAGESIZE to validate it with.
2060 smmu->numpage = resource_size(res);
2063 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2065 if (num_irqs > smmu->num_global_irqs)
2066 smmu->num_context_irqs++;
2069 if (!smmu->num_context_irqs) {
2070 dev_err(dev, "found %d interrupts but expected at least %d\n",
2071 num_irqs, smmu->num_global_irqs + 1);
2075 smmu->irqs = devm_kcalloc(dev, num_irqs, sizeof(*smmu->irqs),
2078 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2082 for (i = 0; i < num_irqs; ++i) {
2083 int irq = platform_get_irq(pdev, i);
2086 dev_err(dev, "failed to get irq index %d\n", i);
2089 smmu->irqs[i] = irq;
2092 err = devm_clk_bulk_get_all(dev, &smmu->clks);
2094 dev_err(dev, "failed to get clocks %d\n", err);
2097 smmu->num_clks = err;
2099 err = clk_bulk_prepare_enable(smmu->num_clks, smmu->clks);
2103 err = arm_smmu_device_cfg_probe(smmu);
2107 if (smmu->version == ARM_SMMU_V2) {
2108 if (smmu->num_context_banks > smmu->num_context_irqs) {
2110 "found only %d context irq(s) but %d required\n",
2111 smmu->num_context_irqs, smmu->num_context_banks);
2115 /* Ignore superfluous interrupts */
2116 smmu->num_context_irqs = smmu->num_context_banks;
2119 for (i = 0; i < smmu->num_global_irqs; ++i) {
2120 err = devm_request_irq(smmu->dev, smmu->irqs[i],
2121 arm_smmu_global_fault,
2123 "arm-smmu global fault",
2126 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2132 err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2133 "smmu.%pa", &ioaddr);
2135 dev_err(dev, "Failed to register iommu in sysfs\n");
2139 iommu_device_set_ops(&smmu->iommu, &arm_smmu_ops);
2140 iommu_device_set_fwnode(&smmu->iommu, dev->fwnode);
2142 err = iommu_device_register(&smmu->iommu);
2144 dev_err(dev, "Failed to register iommu\n");
2148 platform_set_drvdata(pdev, smmu);
2149 arm_smmu_device_reset(smmu);
2150 arm_smmu_test_smr_masks(smmu);
2153 * We want to avoid touching dev->power.lock in fastpaths unless
2154 * it's really going to do something useful - pm_runtime_enabled()
2155 * can serve as an ideal proxy for that decision. So, conditionally
2156 * enable pm_runtime.
2158 if (dev->pm_domain) {
2159 pm_runtime_set_active(dev);
2160 pm_runtime_enable(dev);
2164 * For ACPI and generic DT bindings, an SMMU will be probed before
2165 * any device which might need it, so we want the bus ops in place
2166 * ready to handle default domain setup as soon as any SMMU exists.
2168 if (!using_legacy_binding)
2169 arm_smmu_bus_init();
2175 * With the legacy DT binding in play, though, we have no guarantees about
2176 * probe order, but then we're also not doing default domains, so we can
2177 * delay setting bus ops until we're sure every possible SMMU is ready,
2178 * and that way ensure that no add_device() calls get missed.
2180 static int arm_smmu_legacy_bus_init(void)
2182 if (using_legacy_binding)
2183 arm_smmu_bus_init();
2186 device_initcall_sync(arm_smmu_legacy_bus_init);
2188 static void arm_smmu_device_shutdown(struct platform_device *pdev)
2190 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
2195 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
2196 dev_err(&pdev->dev, "removing device with active domains!\n");
2198 arm_smmu_rpm_get(smmu);
2199 /* Turn the thing off */
2200 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, sCR0_CLIENTPD);
2201 arm_smmu_rpm_put(smmu);
2203 if (pm_runtime_enabled(smmu->dev))
2204 pm_runtime_force_suspend(smmu->dev);
2206 clk_bulk_disable(smmu->num_clks, smmu->clks);
2208 clk_bulk_unprepare(smmu->num_clks, smmu->clks);
2211 static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
2213 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2216 ret = clk_bulk_enable(smmu->num_clks, smmu->clks);
2220 arm_smmu_device_reset(smmu);
2225 static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
2227 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2229 clk_bulk_disable(smmu->num_clks, smmu->clks);
2234 static int __maybe_unused arm_smmu_pm_resume(struct device *dev)
2236 if (pm_runtime_suspended(dev))
2239 return arm_smmu_runtime_resume(dev);
2242 static int __maybe_unused arm_smmu_pm_suspend(struct device *dev)
2244 if (pm_runtime_suspended(dev))
2247 return arm_smmu_runtime_suspend(dev);
2250 static const struct dev_pm_ops arm_smmu_pm_ops = {
2251 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume)
2252 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend,
2253 arm_smmu_runtime_resume, NULL)
2256 static struct platform_driver arm_smmu_driver = {
2259 .of_match_table = of_match_ptr(arm_smmu_of_match),
2260 .pm = &arm_smmu_pm_ops,
2261 .suppress_bind_attrs = true,
2263 .probe = arm_smmu_device_probe,
2264 .shutdown = arm_smmu_device_shutdown,
2266 builtin_platform_driver(arm_smmu_driver);