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/module.h>
32 #include <linux/of_address.h>
33 #include <linux/of_device.h>
34 #include <linux/pci.h>
35 #include <linux/platform_device.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/ratelimit.h>
38 #include <linux/slab.h>
40 #include <linux/amba/bus.h>
41 #include <linux/fsl/mc.h>
46 * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU
47 * global register space are still, in fact, using a hypervisor to mediate it
48 * by trapping and emulating register accesses. Sadly, some deployed versions
49 * of said trapping code have bugs wherein they go horribly wrong for stores
50 * using r31 (i.e. XZR/WZR) as the source register.
52 #define QCOM_DUMMY_VAL -1
54 #define MSI_IOVA_BASE 0x8000000
55 #define MSI_IOVA_LENGTH 0x100000
57 static int force_stage;
58 module_param(force_stage, int, S_IRUGO);
59 MODULE_PARM_DESC(force_stage,
60 "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.");
61 static bool disable_bypass =
62 IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT);
63 module_param(disable_bypass, bool, S_IRUGO);
64 MODULE_PARM_DESC(disable_bypass,
65 "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.");
67 #define s2cr_init_val (struct arm_smmu_s2cr){ \
68 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
71 static bool using_legacy_binding, using_generic_binding;
73 static inline int arm_smmu_rpm_get(struct arm_smmu_device *smmu)
75 if (pm_runtime_enabled(smmu->dev))
76 return pm_runtime_resume_and_get(smmu->dev);
81 static inline void arm_smmu_rpm_put(struct arm_smmu_device *smmu)
83 if (pm_runtime_enabled(smmu->dev))
84 pm_runtime_put_autosuspend(smmu->dev);
87 static struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
89 return container_of(dom, struct arm_smmu_domain, domain);
92 static struct platform_driver arm_smmu_driver;
93 static struct iommu_ops arm_smmu_ops;
95 #ifdef CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS
96 static int arm_smmu_bus_init(struct iommu_ops *ops);
98 static struct device_node *dev_get_dev_node(struct device *dev)
100 if (dev_is_pci(dev)) {
101 struct pci_bus *bus = to_pci_dev(dev)->bus;
103 while (!pci_is_root_bus(bus))
105 return of_node_get(bus->bridge->parent->of_node);
108 return of_node_get(dev->of_node);
111 static int __arm_smmu_get_pci_sid(struct pci_dev *pdev, u16 alias, void *data)
113 *((__be32 *)data) = cpu_to_be32(alias);
114 return 0; /* Continue walking */
117 static int __find_legacy_master_phandle(struct device *dev, void *data)
119 struct of_phandle_iterator *it = *(void **)data;
120 struct device_node *np = it->node;
123 of_for_each_phandle(it, err, dev->of_node, "mmu-masters",
124 "#stream-id-cells", -1)
125 if (it->node == np) {
126 *(void **)data = dev;
130 return err == -ENOENT ? 0 : err;
133 static int arm_smmu_register_legacy_master(struct device *dev,
134 struct arm_smmu_device **smmu)
136 struct device *smmu_dev;
137 struct device_node *np;
138 struct of_phandle_iterator it;
144 np = dev_get_dev_node(dev);
145 if (!np || !of_find_property(np, "#stream-id-cells", NULL)) {
151 err = driver_for_each_device(&arm_smmu_driver.driver, NULL, &data,
152 __find_legacy_master_phandle);
160 if (dev_is_pci(dev)) {
161 /* "mmu-masters" assumes Stream ID == Requester ID */
162 pci_for_each_dma_alias(to_pci_dev(dev), __arm_smmu_get_pci_sid,
168 err = iommu_fwspec_init(dev, &smmu_dev->of_node->fwnode,
173 sids = kcalloc(it.cur_count, sizeof(*sids), GFP_KERNEL);
177 *smmu = dev_get_drvdata(smmu_dev);
178 of_phandle_iterator_args(&it, sids, it.cur_count);
179 err = iommu_fwspec_add_ids(dev, sids, it.cur_count);
185 * With the legacy DT binding in play, we have no guarantees about
186 * probe order, but then we're also not doing default domains, so we can
187 * delay setting bus ops until we're sure every possible SMMU is ready,
188 * and that way ensure that no probe_device() calls get missed.
190 static int arm_smmu_legacy_bus_init(void)
192 if (using_legacy_binding)
193 return arm_smmu_bus_init(&arm_smmu_ops);
196 device_initcall_sync(arm_smmu_legacy_bus_init);
198 static int arm_smmu_register_legacy_master(struct device *dev,
199 struct arm_smmu_device **smmu)
203 #endif /* CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS */
205 static void __arm_smmu_free_bitmap(unsigned long *map, int idx)
210 /* Wait for any pending TLB invalidations to complete */
211 static void __arm_smmu_tlb_sync(struct arm_smmu_device *smmu, int page,
212 int sync, int status)
214 unsigned int spin_cnt, delay;
217 if (smmu->impl && unlikely(smmu->impl->tlb_sync))
218 return smmu->impl->tlb_sync(smmu, page, sync, status);
220 arm_smmu_writel(smmu, page, sync, QCOM_DUMMY_VAL);
221 for (delay = 1; delay < TLB_LOOP_TIMEOUT; delay *= 2) {
222 for (spin_cnt = TLB_SPIN_COUNT; spin_cnt > 0; spin_cnt--) {
223 reg = arm_smmu_readl(smmu, page, status);
224 if (!(reg & ARM_SMMU_sTLBGSTATUS_GSACTIVE))
230 dev_err_ratelimited(smmu->dev,
231 "TLB sync timed out -- SMMU may be deadlocked\n");
234 static void arm_smmu_tlb_sync_global(struct arm_smmu_device *smmu)
238 spin_lock_irqsave(&smmu->global_sync_lock, flags);
239 __arm_smmu_tlb_sync(smmu, ARM_SMMU_GR0, ARM_SMMU_GR0_sTLBGSYNC,
240 ARM_SMMU_GR0_sTLBGSTATUS);
241 spin_unlock_irqrestore(&smmu->global_sync_lock, flags);
244 static void arm_smmu_tlb_sync_context(struct arm_smmu_domain *smmu_domain)
246 struct arm_smmu_device *smmu = smmu_domain->smmu;
249 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
250 __arm_smmu_tlb_sync(smmu, ARM_SMMU_CB(smmu, smmu_domain->cfg.cbndx),
251 ARM_SMMU_CB_TLBSYNC, ARM_SMMU_CB_TLBSTATUS);
252 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
255 static void arm_smmu_tlb_inv_context_s1(void *cookie)
257 struct arm_smmu_domain *smmu_domain = cookie;
259 * The TLBI write may be relaxed, so ensure that PTEs cleared by the
260 * current CPU are visible beforehand.
263 arm_smmu_cb_write(smmu_domain->smmu, smmu_domain->cfg.cbndx,
264 ARM_SMMU_CB_S1_TLBIASID, smmu_domain->cfg.asid);
265 arm_smmu_tlb_sync_context(smmu_domain);
268 static void arm_smmu_tlb_inv_context_s2(void *cookie)
270 struct arm_smmu_domain *smmu_domain = cookie;
271 struct arm_smmu_device *smmu = smmu_domain->smmu;
275 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
276 arm_smmu_tlb_sync_global(smmu);
279 static void arm_smmu_tlb_inv_range_s1(unsigned long iova, size_t size,
280 size_t granule, void *cookie, int reg)
282 struct arm_smmu_domain *smmu_domain = cookie;
283 struct arm_smmu_device *smmu = smmu_domain->smmu;
284 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
285 int idx = cfg->cbndx;
287 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
290 if (cfg->fmt != ARM_SMMU_CTX_FMT_AARCH64) {
291 iova = (iova >> 12) << 12;
294 arm_smmu_cb_write(smmu, idx, reg, iova);
296 } while (size -= granule);
299 iova |= (u64)cfg->asid << 48;
301 arm_smmu_cb_writeq(smmu, idx, reg, iova);
302 iova += granule >> 12;
303 } while (size -= granule);
307 static void arm_smmu_tlb_inv_range_s2(unsigned long iova, size_t size,
308 size_t granule, void *cookie, int reg)
310 struct arm_smmu_domain *smmu_domain = cookie;
311 struct arm_smmu_device *smmu = smmu_domain->smmu;
312 int idx = smmu_domain->cfg.cbndx;
314 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
319 if (smmu_domain->cfg.fmt == ARM_SMMU_CTX_FMT_AARCH64)
320 arm_smmu_cb_writeq(smmu, idx, reg, iova);
322 arm_smmu_cb_write(smmu, idx, reg, iova);
323 iova += granule >> 12;
324 } while (size -= granule);
327 static void arm_smmu_tlb_inv_walk_s1(unsigned long iova, size_t size,
328 size_t granule, void *cookie)
330 arm_smmu_tlb_inv_range_s1(iova, size, granule, cookie,
331 ARM_SMMU_CB_S1_TLBIVA);
332 arm_smmu_tlb_sync_context(cookie);
335 static void arm_smmu_tlb_add_page_s1(struct iommu_iotlb_gather *gather,
336 unsigned long iova, size_t granule,
339 arm_smmu_tlb_inv_range_s1(iova, granule, granule, cookie,
340 ARM_SMMU_CB_S1_TLBIVAL);
343 static void arm_smmu_tlb_inv_walk_s2(unsigned long iova, size_t size,
344 size_t granule, void *cookie)
346 arm_smmu_tlb_inv_range_s2(iova, size, granule, cookie,
347 ARM_SMMU_CB_S2_TLBIIPAS2);
348 arm_smmu_tlb_sync_context(cookie);
351 static void arm_smmu_tlb_add_page_s2(struct iommu_iotlb_gather *gather,
352 unsigned long iova, size_t granule,
355 arm_smmu_tlb_inv_range_s2(iova, granule, granule, cookie,
356 ARM_SMMU_CB_S2_TLBIIPAS2L);
359 static void arm_smmu_tlb_inv_walk_s2_v1(unsigned long iova, size_t size,
360 size_t granule, void *cookie)
362 arm_smmu_tlb_inv_context_s2(cookie);
365 * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
366 * almost negligible, but the benefit of getting the first one in as far ahead
367 * of the sync as possible is significant, hence we don't just make this a
368 * no-op and call arm_smmu_tlb_inv_context_s2() from .iotlb_sync as you might
371 static void arm_smmu_tlb_add_page_s2_v1(struct iommu_iotlb_gather *gather,
372 unsigned long iova, size_t granule,
375 struct arm_smmu_domain *smmu_domain = cookie;
376 struct arm_smmu_device *smmu = smmu_domain->smmu;
378 if (smmu->features & ARM_SMMU_FEAT_COHERENT_WALK)
381 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIVMID, smmu_domain->cfg.vmid);
384 static const struct iommu_flush_ops arm_smmu_s1_tlb_ops = {
385 .tlb_flush_all = arm_smmu_tlb_inv_context_s1,
386 .tlb_flush_walk = arm_smmu_tlb_inv_walk_s1,
387 .tlb_add_page = arm_smmu_tlb_add_page_s1,
390 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v2 = {
391 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
392 .tlb_flush_walk = arm_smmu_tlb_inv_walk_s2,
393 .tlb_add_page = arm_smmu_tlb_add_page_s2,
396 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v1 = {
397 .tlb_flush_all = arm_smmu_tlb_inv_context_s2,
398 .tlb_flush_walk = arm_smmu_tlb_inv_walk_s2_v1,
399 .tlb_add_page = arm_smmu_tlb_add_page_s2_v1,
402 static irqreturn_t arm_smmu_context_fault(int irq, void *dev)
404 u32 fsr, fsynr, cbfrsynra;
406 struct iommu_domain *domain = dev;
407 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
408 struct arm_smmu_device *smmu = smmu_domain->smmu;
409 int idx = smmu_domain->cfg.cbndx;
412 fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
413 if (!(fsr & ARM_SMMU_FSR_FAULT))
416 fsynr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSYNR0);
417 iova = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_FAR);
418 cbfrsynra = arm_smmu_gr1_read(smmu, ARM_SMMU_GR1_CBFRSYNRA(idx));
420 ret = report_iommu_fault(domain, NULL, iova,
421 fsynr & ARM_SMMU_FSYNR0_WNR ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ);
424 dev_err_ratelimited(smmu->dev,
425 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
426 fsr, iova, fsynr, cbfrsynra, idx);
428 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
432 static irqreturn_t arm_smmu_global_fault(int irq, void *dev)
434 u32 gfsr, gfsynr0, gfsynr1, gfsynr2;
435 struct arm_smmu_device *smmu = dev;
436 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
437 DEFAULT_RATELIMIT_BURST);
439 gfsr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
440 gfsynr0 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR0);
441 gfsynr1 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR1);
442 gfsynr2 = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSYNR2);
447 if (__ratelimit(&rs)) {
448 if (IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT) &&
449 (gfsr & ARM_SMMU_sGFSR_USF))
451 "Blocked unknown Stream ID 0x%hx; boot with \"arm-smmu.disable_bypass=0\" to allow, but this may have security implications\n",
455 "Unexpected global fault, this could be serious\n");
457 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
458 gfsr, gfsynr0, gfsynr1, gfsynr2);
461 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, gfsr);
465 static void arm_smmu_init_context_bank(struct arm_smmu_domain *smmu_domain,
466 struct io_pgtable_cfg *pgtbl_cfg)
468 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
469 struct arm_smmu_cb *cb = &smmu_domain->smmu->cbs[cfg->cbndx];
470 bool stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
476 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
477 cb->tcr[0] = pgtbl_cfg->arm_v7s_cfg.tcr;
479 cb->tcr[0] = arm_smmu_lpae_tcr(pgtbl_cfg);
480 cb->tcr[1] = arm_smmu_lpae_tcr2(pgtbl_cfg);
481 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
482 cb->tcr[1] |= ARM_SMMU_TCR2_AS;
484 cb->tcr[0] |= ARM_SMMU_TCR_EAE;
487 cb->tcr[0] = arm_smmu_lpae_vtcr(pgtbl_cfg);
492 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
493 cb->ttbr[0] = pgtbl_cfg->arm_v7s_cfg.ttbr;
496 cb->ttbr[0] = FIELD_PREP(ARM_SMMU_TTBRn_ASID,
498 cb->ttbr[1] = FIELD_PREP(ARM_SMMU_TTBRn_ASID,
501 if (pgtbl_cfg->quirks & IO_PGTABLE_QUIRK_ARM_TTBR1)
502 cb->ttbr[1] |= pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
504 cb->ttbr[0] |= pgtbl_cfg->arm_lpae_s1_cfg.ttbr;
507 cb->ttbr[0] = pgtbl_cfg->arm_lpae_s2_cfg.vttbr;
510 /* MAIRs (stage-1 only) */
512 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
513 cb->mair[0] = pgtbl_cfg->arm_v7s_cfg.prrr;
514 cb->mair[1] = pgtbl_cfg->arm_v7s_cfg.nmrr;
516 cb->mair[0] = pgtbl_cfg->arm_lpae_s1_cfg.mair;
517 cb->mair[1] = pgtbl_cfg->arm_lpae_s1_cfg.mair >> 32;
522 void arm_smmu_write_context_bank(struct arm_smmu_device *smmu, int idx)
526 struct arm_smmu_cb *cb = &smmu->cbs[idx];
527 struct arm_smmu_cfg *cfg = cb->cfg;
529 /* Unassigned context banks only need disabling */
531 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, 0);
535 stage1 = cfg->cbar != CBAR_TYPE_S2_TRANS;
538 if (smmu->version > ARM_SMMU_V1) {
539 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
540 reg = ARM_SMMU_CBA2R_VA64;
543 /* 16-bit VMIDs live in CBA2R */
544 if (smmu->features & ARM_SMMU_FEAT_VMID16)
545 reg |= FIELD_PREP(ARM_SMMU_CBA2R_VMID16, cfg->vmid);
547 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBA2R(idx), reg);
551 reg = FIELD_PREP(ARM_SMMU_CBAR_TYPE, cfg->cbar);
552 if (smmu->version < ARM_SMMU_V2)
553 reg |= FIELD_PREP(ARM_SMMU_CBAR_IRPTNDX, cfg->irptndx);
556 * Use the weakest shareability/memory types, so they are
557 * overridden by the ttbcr/pte.
560 reg |= FIELD_PREP(ARM_SMMU_CBAR_S1_BPSHCFG,
561 ARM_SMMU_CBAR_S1_BPSHCFG_NSH) |
562 FIELD_PREP(ARM_SMMU_CBAR_S1_MEMATTR,
563 ARM_SMMU_CBAR_S1_MEMATTR_WB);
564 } else if (!(smmu->features & ARM_SMMU_FEAT_VMID16)) {
565 /* 8-bit VMIDs live in CBAR */
566 reg |= FIELD_PREP(ARM_SMMU_CBAR_VMID, cfg->vmid);
568 arm_smmu_gr1_write(smmu, ARM_SMMU_GR1_CBAR(idx), reg);
572 * We must write this before the TTBRs, since it determines the
573 * access behaviour of some fields (in particular, ASID[15:8]).
575 if (stage1 && smmu->version > ARM_SMMU_V1)
576 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR2, cb->tcr[1]);
577 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TCR, cb->tcr[0]);
580 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_S) {
581 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_CONTEXTIDR, cfg->asid);
582 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
583 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_TTBR1, cb->ttbr[1]);
585 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR0, cb->ttbr[0]);
587 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_TTBR1,
591 /* MAIRs (stage-1 only) */
593 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR0, cb->mair[0]);
594 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_S1_MAIR1, cb->mair[1]);
598 reg = ARM_SMMU_SCTLR_CFIE | ARM_SMMU_SCTLR_CFRE | ARM_SMMU_SCTLR_AFE |
599 ARM_SMMU_SCTLR_TRE | ARM_SMMU_SCTLR_M;
601 reg |= ARM_SMMU_SCTLR_S1_ASIDPNE;
602 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
603 reg |= ARM_SMMU_SCTLR_E;
605 if (smmu->impl && smmu->impl->write_sctlr)
606 smmu->impl->write_sctlr(smmu, idx, reg);
608 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, reg);
611 static int arm_smmu_alloc_context_bank(struct arm_smmu_domain *smmu_domain,
612 struct arm_smmu_device *smmu,
613 struct device *dev, unsigned int start)
615 if (smmu->impl && smmu->impl->alloc_context_bank)
616 return smmu->impl->alloc_context_bank(smmu_domain, smmu, dev, start);
618 return __arm_smmu_alloc_bitmap(smmu->context_map, start, smmu->num_context_banks);
621 static int arm_smmu_init_domain_context(struct iommu_domain *domain,
622 struct arm_smmu_device *smmu,
625 int irq, start, ret = 0;
626 unsigned long ias, oas;
627 struct io_pgtable_ops *pgtbl_ops;
628 struct io_pgtable_cfg pgtbl_cfg;
629 enum io_pgtable_fmt fmt;
630 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
631 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
632 irqreturn_t (*context_fault)(int irq, void *dev);
634 mutex_lock(&smmu_domain->init_mutex);
635 if (smmu_domain->smmu)
638 if (domain->type == IOMMU_DOMAIN_IDENTITY) {
639 smmu_domain->stage = ARM_SMMU_DOMAIN_BYPASS;
640 smmu_domain->smmu = smmu;
645 * Mapping the requested stage onto what we support is surprisingly
646 * complicated, mainly because the spec allows S1+S2 SMMUs without
647 * support for nested translation. That means we end up with the
650 * Requested Supported Actual
660 * Note that you can't actually request stage-2 mappings.
662 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1))
663 smmu_domain->stage = ARM_SMMU_DOMAIN_S2;
664 if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S2))
665 smmu_domain->stage = ARM_SMMU_DOMAIN_S1;
668 * Choosing a suitable context format is even more fiddly. Until we
669 * grow some way for the caller to express a preference, and/or move
670 * the decision into the io-pgtable code where it arguably belongs,
671 * just aim for the closest thing to the rest of the system, and hope
672 * that the hardware isn't esoteric enough that we can't assume AArch64
673 * support to be a superset of AArch32 support...
675 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_L)
676 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_L;
677 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S) &&
678 !IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_ARM_LPAE) &&
679 (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S) &&
680 (smmu_domain->stage == ARM_SMMU_DOMAIN_S1))
681 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH32_S;
682 if ((IS_ENABLED(CONFIG_64BIT) || cfg->fmt == ARM_SMMU_CTX_FMT_NONE) &&
683 (smmu->features & (ARM_SMMU_FEAT_FMT_AARCH64_64K |
684 ARM_SMMU_FEAT_FMT_AARCH64_16K |
685 ARM_SMMU_FEAT_FMT_AARCH64_4K)))
686 cfg->fmt = ARM_SMMU_CTX_FMT_AARCH64;
688 if (cfg->fmt == ARM_SMMU_CTX_FMT_NONE) {
693 switch (smmu_domain->stage) {
694 case ARM_SMMU_DOMAIN_S1:
695 cfg->cbar = CBAR_TYPE_S1_TRANS_S2_BYPASS;
696 start = smmu->num_s2_context_banks;
698 oas = smmu->ipa_size;
699 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
700 fmt = ARM_64_LPAE_S1;
701 } else if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH32_L) {
702 fmt = ARM_32_LPAE_S1;
703 ias = min(ias, 32UL);
704 oas = min(oas, 40UL);
707 ias = min(ias, 32UL);
708 oas = min(oas, 32UL);
710 smmu_domain->flush_ops = &arm_smmu_s1_tlb_ops;
712 case ARM_SMMU_DOMAIN_NESTED:
714 * We will likely want to change this if/when KVM gets
717 case ARM_SMMU_DOMAIN_S2:
718 cfg->cbar = CBAR_TYPE_S2_TRANS;
720 ias = smmu->ipa_size;
722 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64) {
723 fmt = ARM_64_LPAE_S2;
725 fmt = ARM_32_LPAE_S2;
726 ias = min(ias, 40UL);
727 oas = min(oas, 40UL);
729 if (smmu->version == ARM_SMMU_V2)
730 smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v2;
732 smmu_domain->flush_ops = &arm_smmu_s2_tlb_ops_v1;
739 ret = arm_smmu_alloc_context_bank(smmu_domain, smmu, dev, start);
744 smmu_domain->smmu = smmu;
747 if (smmu->version < ARM_SMMU_V2) {
748 cfg->irptndx = atomic_inc_return(&smmu->irptndx);
749 cfg->irptndx %= smmu->num_context_irqs;
751 cfg->irptndx = cfg->cbndx;
754 if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2)
755 cfg->vmid = cfg->cbndx + 1;
757 cfg->asid = cfg->cbndx;
759 pgtbl_cfg = (struct io_pgtable_cfg) {
760 .pgsize_bitmap = smmu->pgsize_bitmap,
763 .coherent_walk = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK,
764 .tlb = smmu_domain->flush_ops,
765 .iommu_dev = smmu->dev,
768 if (!iommu_get_dma_strict(domain))
769 pgtbl_cfg.quirks |= IO_PGTABLE_QUIRK_NON_STRICT;
771 if (smmu->impl && smmu->impl->init_context) {
772 ret = smmu->impl->init_context(smmu_domain, &pgtbl_cfg, dev);
777 if (smmu_domain->pgtbl_quirks)
778 pgtbl_cfg.quirks |= smmu_domain->pgtbl_quirks;
780 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
786 /* Update the domain's page sizes to reflect the page table format */
787 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
789 if (pgtbl_cfg.quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) {
790 domain->geometry.aperture_start = ~0UL << ias;
791 domain->geometry.aperture_end = ~0UL;
793 domain->geometry.aperture_end = (1UL << ias) - 1;
796 domain->geometry.force_aperture = true;
798 /* Initialise the context bank with our page table cfg */
799 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
800 arm_smmu_write_context_bank(smmu, cfg->cbndx);
803 * Request context fault interrupt. Do this last to avoid the
804 * handler seeing a half-initialised domain state.
806 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
808 if (smmu->impl && smmu->impl->context_fault)
809 context_fault = smmu->impl->context_fault;
811 context_fault = arm_smmu_context_fault;
813 ret = devm_request_irq(smmu->dev, irq, context_fault,
814 IRQF_SHARED, "arm-smmu-context-fault", domain);
816 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
818 cfg->irptndx = ARM_SMMU_INVALID_IRPTNDX;
821 mutex_unlock(&smmu_domain->init_mutex);
823 /* Publish page table ops for map/unmap */
824 smmu_domain->pgtbl_ops = pgtbl_ops;
828 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
829 smmu_domain->smmu = NULL;
831 mutex_unlock(&smmu_domain->init_mutex);
835 static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
837 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
838 struct arm_smmu_device *smmu = smmu_domain->smmu;
839 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
842 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
845 ret = arm_smmu_rpm_get(smmu);
850 * Disable the context bank and free the page tables before freeing
853 smmu->cbs[cfg->cbndx].cfg = NULL;
854 arm_smmu_write_context_bank(smmu, cfg->cbndx);
856 if (cfg->irptndx != ARM_SMMU_INVALID_IRPTNDX) {
857 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
858 devm_free_irq(smmu->dev, irq, domain);
861 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
862 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
864 arm_smmu_rpm_put(smmu);
867 static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
869 struct arm_smmu_domain *smmu_domain;
871 if (type != IOMMU_DOMAIN_UNMANAGED &&
872 type != IOMMU_DOMAIN_DMA &&
873 type != IOMMU_DOMAIN_IDENTITY)
876 * Allocate the domain and initialise some of its data structures.
877 * We can't really do anything meaningful until we've added a
880 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
884 if (type == IOMMU_DOMAIN_DMA && (using_legacy_binding ||
885 iommu_get_dma_cookie(&smmu_domain->domain))) {
890 mutex_init(&smmu_domain->init_mutex);
891 spin_lock_init(&smmu_domain->cb_lock);
893 return &smmu_domain->domain;
896 static void arm_smmu_domain_free(struct iommu_domain *domain)
898 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
901 * Free the domain resources. We assume that all devices have
902 * already been detached.
904 iommu_put_dma_cookie(domain);
905 arm_smmu_destroy_domain_context(domain);
909 static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
911 struct arm_smmu_smr *smr = smmu->smrs + idx;
912 u32 reg = FIELD_PREP(ARM_SMMU_SMR_ID, smr->id) |
913 FIELD_PREP(ARM_SMMU_SMR_MASK, smr->mask);
915 if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
916 reg |= ARM_SMMU_SMR_VALID;
917 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(idx), reg);
920 static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
922 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
925 if (smmu->impl && smmu->impl->write_s2cr) {
926 smmu->impl->write_s2cr(smmu, idx);
930 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, s2cr->type) |
931 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, s2cr->cbndx) |
932 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, s2cr->privcfg);
934 if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
935 smmu->smrs[idx].valid)
936 reg |= ARM_SMMU_S2CR_EXIDVALID;
937 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
940 static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
942 arm_smmu_write_s2cr(smmu, idx);
944 arm_smmu_write_smr(smmu, idx);
948 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
949 * should be called after sCR0 is written.
951 static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
959 * If we've had to accommodate firmware memory regions, we may
960 * have live SMRs by now; tread carefully...
962 * Somewhat perversely, not having a free SMR for this test implies we
963 * can get away without it anyway, as we'll only be able to 'allocate'
964 * these SMRs for the ID/mask values we're already trusting to be OK.
966 for (i = 0; i < smmu->num_mapping_groups; i++)
967 if (!smmu->smrs[i].valid)
972 * SMR.ID bits may not be preserved if the corresponding MASK
973 * bits are set, so check each one separately. We can reject
974 * masters later if they try to claim IDs outside these masks.
976 smr = FIELD_PREP(ARM_SMMU_SMR_ID, smmu->streamid_mask);
977 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
978 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
979 smmu->streamid_mask = FIELD_GET(ARM_SMMU_SMR_ID, smr);
981 smr = FIELD_PREP(ARM_SMMU_SMR_MASK, smmu->streamid_mask);
982 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
983 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
984 smmu->smr_mask_mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
987 static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
989 struct arm_smmu_smr *smrs = smmu->smrs;
990 int i, free_idx = -ENOSPC;
992 /* Stream indexing is blissfully easy */
996 /* Validating SMRs is... less so */
997 for (i = 0; i < smmu->num_mapping_groups; ++i) {
998 if (!smrs[i].valid) {
1000 * Note the first free entry we come across, which
1001 * we'll claim in the end if nothing else matches.
1008 * If the new entry is _entirely_ matched by an existing entry,
1009 * then reuse that, with the guarantee that there also cannot
1010 * be any subsequent conflicting entries. In normal use we'd
1011 * expect simply identical entries for this case, but there's
1012 * no harm in accommodating the generalisation.
1014 if ((mask & smrs[i].mask) == mask &&
1015 !((id ^ smrs[i].id) & ~smrs[i].mask))
1018 * If the new entry has any other overlap with an existing one,
1019 * though, then there always exists at least one stream ID
1020 * which would cause a conflict, and we can't allow that risk.
1022 if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1029 static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1031 if (--smmu->s2crs[idx].count)
1034 smmu->s2crs[idx] = s2cr_init_val;
1036 smmu->smrs[idx].valid = false;
1041 static int arm_smmu_master_alloc_smes(struct device *dev)
1043 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1044 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1045 struct arm_smmu_device *smmu = cfg->smmu;
1046 struct arm_smmu_smr *smrs = smmu->smrs;
1049 mutex_lock(&smmu->stream_map_mutex);
1050 /* Figure out a viable stream map entry allocation */
1051 for_each_cfg_sme(cfg, fwspec, i, idx) {
1052 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1053 u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
1055 if (idx != INVALID_SMENDX) {
1060 ret = arm_smmu_find_sme(smmu, sid, mask);
1065 if (smrs && smmu->s2crs[idx].count == 0) {
1067 smrs[idx].mask = mask;
1068 smrs[idx].valid = true;
1070 smmu->s2crs[idx].count++;
1071 cfg->smendx[i] = (s16)idx;
1074 /* It worked! Now, poke the actual hardware */
1075 for_each_cfg_sme(cfg, fwspec, i, idx)
1076 arm_smmu_write_sme(smmu, idx);
1078 mutex_unlock(&smmu->stream_map_mutex);
1083 arm_smmu_free_sme(smmu, cfg->smendx[i]);
1084 cfg->smendx[i] = INVALID_SMENDX;
1086 mutex_unlock(&smmu->stream_map_mutex);
1090 static void arm_smmu_master_free_smes(struct arm_smmu_master_cfg *cfg,
1091 struct iommu_fwspec *fwspec)
1093 struct arm_smmu_device *smmu = cfg->smmu;
1096 mutex_lock(&smmu->stream_map_mutex);
1097 for_each_cfg_sme(cfg, fwspec, i, idx) {
1098 if (arm_smmu_free_sme(smmu, idx))
1099 arm_smmu_write_sme(smmu, idx);
1100 cfg->smendx[i] = INVALID_SMENDX;
1102 mutex_unlock(&smmu->stream_map_mutex);
1105 static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1106 struct arm_smmu_master_cfg *cfg,
1107 struct iommu_fwspec *fwspec)
1109 struct arm_smmu_device *smmu = smmu_domain->smmu;
1110 struct arm_smmu_s2cr *s2cr = smmu->s2crs;
1111 u8 cbndx = smmu_domain->cfg.cbndx;
1112 enum arm_smmu_s2cr_type type;
1115 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1116 type = S2CR_TYPE_BYPASS;
1118 type = S2CR_TYPE_TRANS;
1120 for_each_cfg_sme(cfg, fwspec, i, idx) {
1121 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
1124 s2cr[idx].type = type;
1125 s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
1126 s2cr[idx].cbndx = cbndx;
1127 arm_smmu_write_s2cr(smmu, idx);
1132 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1134 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1135 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1136 struct arm_smmu_master_cfg *cfg;
1137 struct arm_smmu_device *smmu;
1140 if (!fwspec || fwspec->ops != &arm_smmu_ops) {
1141 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1146 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1147 * domains between of_xlate() and probe_device() - we have no way to cope
1148 * with that, so until ARM gets converted to rely on groups and default
1149 * domains, just say no (but more politely than by dereferencing NULL).
1150 * This should be at least a WARN_ON once that's sorted.
1152 cfg = dev_iommu_priv_get(dev);
1158 ret = arm_smmu_rpm_get(smmu);
1162 /* Ensure that the domain is finalised */
1163 ret = arm_smmu_init_domain_context(domain, smmu, dev);
1168 * Sanity check the domain. We don't support domains across
1171 if (smmu_domain->smmu != smmu) {
1173 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1174 dev_name(smmu_domain->smmu->dev), dev_name(smmu->dev));
1179 /* Looks ok, so add the device to the domain */
1180 ret = arm_smmu_domain_add_master(smmu_domain, cfg, fwspec);
1183 * Setup an autosuspend delay to avoid bouncing runpm state.
1184 * Otherwise, if a driver for a suspended consumer device
1185 * unmaps buffers, it will runpm resume/suspend for each one.
1187 * For example, when used by a GPU device, when an application
1188 * or game exits, it can trigger unmapping 100s or 1000s of
1189 * buffers. With a runpm cycle for each buffer, that adds up
1190 * to 5-10sec worth of reprogramming the context bank, while
1191 * the system appears to be locked up to the user.
1193 pm_runtime_set_autosuspend_delay(smmu->dev, 20);
1194 pm_runtime_use_autosuspend(smmu->dev);
1197 arm_smmu_rpm_put(smmu);
1201 static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova,
1202 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
1204 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1205 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1211 arm_smmu_rpm_get(smmu);
1212 ret = ops->map(ops, iova, paddr, size, prot, gfp);
1213 arm_smmu_rpm_put(smmu);
1218 static size_t arm_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
1219 size_t size, struct iommu_iotlb_gather *gather)
1221 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1222 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1228 arm_smmu_rpm_get(smmu);
1229 ret = ops->unmap(ops, iova, size, gather);
1230 arm_smmu_rpm_put(smmu);
1235 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
1237 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1238 struct arm_smmu_device *smmu = smmu_domain->smmu;
1240 if (smmu_domain->flush_ops) {
1241 arm_smmu_rpm_get(smmu);
1242 smmu_domain->flush_ops->tlb_flush_all(smmu_domain);
1243 arm_smmu_rpm_put(smmu);
1247 static void arm_smmu_iotlb_sync(struct iommu_domain *domain,
1248 struct iommu_iotlb_gather *gather)
1250 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1251 struct arm_smmu_device *smmu = smmu_domain->smmu;
1256 arm_smmu_rpm_get(smmu);
1257 if (smmu->version == ARM_SMMU_V2 ||
1258 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1259 arm_smmu_tlb_sync_context(smmu_domain);
1261 arm_smmu_tlb_sync_global(smmu);
1262 arm_smmu_rpm_put(smmu);
1265 static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1268 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1269 struct arm_smmu_device *smmu = smmu_domain->smmu;
1270 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1271 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1272 struct device *dev = smmu->dev;
1276 unsigned long va, flags;
1277 int ret, idx = cfg->cbndx;
1278 phys_addr_t addr = 0;
1280 ret = arm_smmu_rpm_get(smmu);
1284 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
1285 va = iova & ~0xfffUL;
1286 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
1287 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1289 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1291 reg = arm_smmu_page(smmu, ARM_SMMU_CB(smmu, idx)) + ARM_SMMU_CB_ATSR;
1292 if (readl_poll_timeout_atomic(reg, tmp, !(tmp & ARM_SMMU_ATSR_ACTIVE),
1294 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1296 "iova to phys timed out on %pad. Falling back to software table walk.\n",
1298 arm_smmu_rpm_put(smmu);
1299 return ops->iova_to_phys(ops, iova);
1302 phys = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_PAR);
1303 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1304 if (phys & ARM_SMMU_CB_PAR_F) {
1305 dev_err(dev, "translation fault!\n");
1306 dev_err(dev, "PAR = 0x%llx\n", phys);
1310 addr = (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1312 arm_smmu_rpm_put(smmu);
1317 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1320 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1321 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1323 if (domain->type == IOMMU_DOMAIN_IDENTITY)
1329 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1330 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1331 return arm_smmu_iova_to_phys_hard(domain, iova);
1333 return ops->iova_to_phys(ops, iova);
1336 static bool arm_smmu_capable(enum iommu_cap cap)
1339 case IOMMU_CAP_CACHE_COHERENCY:
1341 * Return true here as the SMMU can always send out coherent
1345 case IOMMU_CAP_NOEXEC:
1353 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
1355 struct device *dev = driver_find_device_by_fwnode(&arm_smmu_driver.driver,
1358 return dev ? dev_get_drvdata(dev) : NULL;
1361 static struct iommu_device *arm_smmu_probe_device(struct device *dev)
1363 struct arm_smmu_device *smmu = NULL;
1364 struct arm_smmu_master_cfg *cfg;
1365 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1368 if (using_legacy_binding) {
1369 ret = arm_smmu_register_legacy_master(dev, &smmu);
1372 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1373 * will allocate/initialise a new one. Thus we need to update fwspec for
1376 fwspec = dev_iommu_fwspec_get(dev);
1379 } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
1380 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
1382 return ERR_PTR(-ENODEV);
1386 for (i = 0; i < fwspec->num_ids; i++) {
1387 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1388 u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
1390 if (sid & ~smmu->streamid_mask) {
1391 dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
1392 sid, smmu->streamid_mask);
1395 if (mask & ~smmu->smr_mask_mask) {
1396 dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1397 mask, smmu->smr_mask_mask);
1403 cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1409 dev_iommu_priv_set(dev, cfg);
1411 cfg->smendx[i] = INVALID_SMENDX;
1413 ret = arm_smmu_rpm_get(smmu);
1417 ret = arm_smmu_master_alloc_smes(dev);
1418 arm_smmu_rpm_put(smmu);
1423 device_link_add(dev, smmu->dev,
1424 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
1426 return &smmu->iommu;
1431 iommu_fwspec_free(dev);
1432 return ERR_PTR(ret);
1435 static void arm_smmu_release_device(struct device *dev)
1437 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1438 struct arm_smmu_master_cfg *cfg;
1439 struct arm_smmu_device *smmu;
1442 if (!fwspec || fwspec->ops != &arm_smmu_ops)
1445 cfg = dev_iommu_priv_get(dev);
1448 ret = arm_smmu_rpm_get(smmu);
1452 arm_smmu_master_free_smes(cfg, fwspec);
1454 arm_smmu_rpm_put(smmu);
1456 dev_iommu_priv_set(dev, NULL);
1458 iommu_fwspec_free(dev);
1461 static void arm_smmu_probe_finalize(struct device *dev)
1463 struct arm_smmu_master_cfg *cfg;
1464 struct arm_smmu_device *smmu;
1466 cfg = dev_iommu_priv_get(dev);
1469 if (smmu->impl && smmu->impl->probe_finalize)
1470 smmu->impl->probe_finalize(smmu, dev);
1473 static struct iommu_group *arm_smmu_device_group(struct device *dev)
1475 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1476 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1477 struct arm_smmu_device *smmu = cfg->smmu;
1478 struct iommu_group *group = NULL;
1481 for_each_cfg_sme(cfg, fwspec, i, idx) {
1482 if (group && smmu->s2crs[idx].group &&
1483 group != smmu->s2crs[idx].group)
1484 return ERR_PTR(-EINVAL);
1486 group = smmu->s2crs[idx].group;
1490 return iommu_group_ref_get(group);
1492 if (dev_is_pci(dev))
1493 group = pci_device_group(dev);
1494 else if (dev_is_fsl_mc(dev))
1495 group = fsl_mc_device_group(dev);
1497 group = generic_device_group(dev);
1499 /* Remember group for faster lookups */
1501 for_each_cfg_sme(cfg, fwspec, i, idx)
1502 smmu->s2crs[idx].group = group;
1507 static int arm_smmu_enable_nesting(struct iommu_domain *domain)
1509 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1512 mutex_lock(&smmu_domain->init_mutex);
1513 if (smmu_domain->smmu)
1516 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1517 mutex_unlock(&smmu_domain->init_mutex);
1522 static int arm_smmu_set_pgtable_quirks(struct iommu_domain *domain,
1523 unsigned long quirks)
1525 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1528 mutex_lock(&smmu_domain->init_mutex);
1529 if (smmu_domain->smmu)
1532 smmu_domain->pgtbl_quirks = quirks;
1533 mutex_unlock(&smmu_domain->init_mutex);
1538 static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1542 if (args->args_count > 0)
1543 fwid |= FIELD_PREP(ARM_SMMU_SMR_ID, args->args[0]);
1545 if (args->args_count > 1)
1546 fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, args->args[1]);
1547 else if (!of_property_read_u32(args->np, "stream-match-mask", &mask))
1548 fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, mask);
1550 return iommu_fwspec_add_ids(dev, &fwid, 1);
1553 static void arm_smmu_get_resv_regions(struct device *dev,
1554 struct list_head *head)
1556 struct iommu_resv_region *region;
1557 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1559 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
1560 prot, IOMMU_RESV_SW_MSI);
1564 list_add_tail(®ion->list, head);
1566 iommu_dma_get_resv_regions(dev, head);
1569 static int arm_smmu_def_domain_type(struct device *dev)
1571 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1572 const struct arm_smmu_impl *impl = cfg->smmu->impl;
1574 if (impl && impl->def_domain_type)
1575 return impl->def_domain_type(dev);
1580 static struct iommu_ops arm_smmu_ops = {
1581 .capable = arm_smmu_capable,
1582 .domain_alloc = arm_smmu_domain_alloc,
1583 .domain_free = arm_smmu_domain_free,
1584 .attach_dev = arm_smmu_attach_dev,
1585 .map = arm_smmu_map,
1586 .unmap = arm_smmu_unmap,
1587 .flush_iotlb_all = arm_smmu_flush_iotlb_all,
1588 .iotlb_sync = arm_smmu_iotlb_sync,
1589 .iova_to_phys = arm_smmu_iova_to_phys,
1590 .probe_device = arm_smmu_probe_device,
1591 .release_device = arm_smmu_release_device,
1592 .probe_finalize = arm_smmu_probe_finalize,
1593 .device_group = arm_smmu_device_group,
1594 .enable_nesting = arm_smmu_enable_nesting,
1595 .set_pgtable_quirks = arm_smmu_set_pgtable_quirks,
1596 .of_xlate = arm_smmu_of_xlate,
1597 .get_resv_regions = arm_smmu_get_resv_regions,
1598 .put_resv_regions = generic_iommu_put_resv_regions,
1599 .def_domain_type = arm_smmu_def_domain_type,
1600 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1601 .owner = THIS_MODULE,
1604 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1609 /* clear global FSR */
1610 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
1611 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, reg);
1614 * Reset stream mapping groups: Initial values mark all SMRn as
1615 * invalid and all S2CRn as bypass unless overridden.
1617 for (i = 0; i < smmu->num_mapping_groups; ++i)
1618 arm_smmu_write_sme(smmu, i);
1620 /* Make sure all context banks are disabled and clear CB_FSR */
1621 for (i = 0; i < smmu->num_context_banks; ++i) {
1622 arm_smmu_write_context_bank(smmu, i);
1623 arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_FSR, ARM_SMMU_FSR_FAULT);
1626 /* Invalidate the TLB, just in case */
1627 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLH, QCOM_DUMMY_VAL);
1628 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLNSNH, QCOM_DUMMY_VAL);
1630 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sCR0);
1632 /* Enable fault reporting */
1633 reg |= (ARM_SMMU_sCR0_GFRE | ARM_SMMU_sCR0_GFIE |
1634 ARM_SMMU_sCR0_GCFGFRE | ARM_SMMU_sCR0_GCFGFIE);
1636 /* Disable TLB broadcasting. */
1637 reg |= (ARM_SMMU_sCR0_VMIDPNE | ARM_SMMU_sCR0_PTM);
1639 /* Enable client access, handling unmatched streams as appropriate */
1640 reg &= ~ARM_SMMU_sCR0_CLIENTPD;
1642 reg |= ARM_SMMU_sCR0_USFCFG;
1644 reg &= ~ARM_SMMU_sCR0_USFCFG;
1646 /* Disable forced broadcasting */
1647 reg &= ~ARM_SMMU_sCR0_FB;
1649 /* Don't upgrade barriers */
1650 reg &= ~(ARM_SMMU_sCR0_BSU);
1652 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1653 reg |= ARM_SMMU_sCR0_VMID16EN;
1655 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
1656 reg |= ARM_SMMU_sCR0_EXIDENABLE;
1658 if (smmu->impl && smmu->impl->reset)
1659 smmu->impl->reset(smmu);
1661 /* Push the button */
1662 arm_smmu_tlb_sync_global(smmu);
1663 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, reg);
1666 static int arm_smmu_id_size_to_bits(int size)
1685 static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1689 bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
1692 dev_notice(smmu->dev, "probing hardware configuration...\n");
1693 dev_notice(smmu->dev, "SMMUv%d with:\n",
1694 smmu->version == ARM_SMMU_V2 ? 2 : 1);
1697 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID0);
1699 /* Restrict available stages based on module parameter */
1700 if (force_stage == 1)
1701 id &= ~(ARM_SMMU_ID0_S2TS | ARM_SMMU_ID0_NTS);
1702 else if (force_stage == 2)
1703 id &= ~(ARM_SMMU_ID0_S1TS | ARM_SMMU_ID0_NTS);
1705 if (id & ARM_SMMU_ID0_S1TS) {
1706 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1707 dev_notice(smmu->dev, "\tstage 1 translation\n");
1710 if (id & ARM_SMMU_ID0_S2TS) {
1711 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1712 dev_notice(smmu->dev, "\tstage 2 translation\n");
1715 if (id & ARM_SMMU_ID0_NTS) {
1716 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1717 dev_notice(smmu->dev, "\tnested translation\n");
1720 if (!(smmu->features &
1721 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
1722 dev_err(smmu->dev, "\tno translation support!\n");
1726 if ((id & ARM_SMMU_ID0_S1TS) &&
1727 ((smmu->version < ARM_SMMU_V2) || !(id & ARM_SMMU_ID0_ATOSNS))) {
1728 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1729 dev_notice(smmu->dev, "\taddress translation ops\n");
1733 * In order for DMA API calls to work properly, we must defer to what
1734 * the FW says about coherency, regardless of what the hardware claims.
1735 * Fortunately, this also opens up a workaround for systems where the
1736 * ID register value has ended up configured incorrectly.
1738 cttw_reg = !!(id & ARM_SMMU_ID0_CTTW);
1739 if (cttw_fw || cttw_reg)
1740 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1741 cttw_fw ? "" : "non-");
1742 if (cttw_fw != cttw_reg)
1743 dev_notice(smmu->dev,
1744 "\t(IDR0.CTTW overridden by FW configuration)\n");
1746 /* Max. number of entries we have for stream matching/indexing */
1747 if (smmu->version == ARM_SMMU_V2 && id & ARM_SMMU_ID0_EXIDS) {
1748 smmu->features |= ARM_SMMU_FEAT_EXIDS;
1751 size = 1 << FIELD_GET(ARM_SMMU_ID0_NUMSIDB, id);
1753 smmu->streamid_mask = size - 1;
1754 if (id & ARM_SMMU_ID0_SMS) {
1755 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1756 size = FIELD_GET(ARM_SMMU_ID0_NUMSMRG, id);
1759 "stream-matching supported, but no SMRs present!\n");
1763 /* Zero-initialised to mark as invalid */
1764 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1769 dev_notice(smmu->dev,
1770 "\tstream matching with %u register groups", size);
1772 /* s2cr->type == 0 means translation, so initialise explicitly */
1773 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1777 for (i = 0; i < size; i++)
1778 smmu->s2crs[i] = s2cr_init_val;
1780 smmu->num_mapping_groups = size;
1781 mutex_init(&smmu->stream_map_mutex);
1782 spin_lock_init(&smmu->global_sync_lock);
1784 if (smmu->version < ARM_SMMU_V2 ||
1785 !(id & ARM_SMMU_ID0_PTFS_NO_AARCH32)) {
1786 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1787 if (!(id & ARM_SMMU_ID0_PTFS_NO_AARCH32S))
1788 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1792 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID1);
1793 smmu->pgshift = (id & ARM_SMMU_ID1_PAGESIZE) ? 16 : 12;
1795 /* Check for size mismatch of SMMU address space from mapped region */
1796 size = 1 << (FIELD_GET(ARM_SMMU_ID1_NUMPAGENDXB, id) + 1);
1797 if (smmu->numpage != 2 * size << smmu->pgshift)
1799 "SMMU address space size (0x%x) differs from mapped region size (0x%x)!\n",
1800 2 * size << smmu->pgshift, smmu->numpage);
1801 /* Now properly encode NUMPAGE to subsequently derive SMMU_CB_BASE */
1802 smmu->numpage = size;
1804 smmu->num_s2_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMS2CB, id);
1805 smmu->num_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMCB, id);
1806 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1807 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1810 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1811 smmu->num_context_banks, smmu->num_s2_context_banks);
1812 smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
1813 sizeof(*smmu->cbs), GFP_KERNEL);
1818 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID2);
1819 size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_IAS, id));
1820 smmu->ipa_size = size;
1822 /* The output mask is also applied for bypass */
1823 size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_OAS, id));
1824 smmu->pa_size = size;
1826 if (id & ARM_SMMU_ID2_VMID16)
1827 smmu->features |= ARM_SMMU_FEAT_VMID16;
1830 * What the page table walker can address actually depends on which
1831 * descriptor format is in use, but since a) we don't know that yet,
1832 * and b) it can vary per context bank, this will have to do...
1834 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1836 "failed to set DMA mask for table walker\n");
1838 if (smmu->version < ARM_SMMU_V2) {
1839 smmu->va_size = smmu->ipa_size;
1840 if (smmu->version == ARM_SMMU_V1_64K)
1841 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1843 size = FIELD_GET(ARM_SMMU_ID2_UBS, id);
1844 smmu->va_size = arm_smmu_id_size_to_bits(size);
1845 if (id & ARM_SMMU_ID2_PTFS_4K)
1846 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
1847 if (id & ARM_SMMU_ID2_PTFS_16K)
1848 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
1849 if (id & ARM_SMMU_ID2_PTFS_64K)
1850 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1853 if (smmu->impl && smmu->impl->cfg_probe) {
1854 ret = smmu->impl->cfg_probe(smmu);
1859 /* Now we've corralled the various formats, what'll it do? */
1860 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
1861 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
1862 if (smmu->features &
1863 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
1864 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
1865 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
1866 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
1867 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
1868 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
1870 if (arm_smmu_ops.pgsize_bitmap == -1UL)
1871 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1873 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1874 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1875 smmu->pgsize_bitmap);
1878 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1879 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1880 smmu->va_size, smmu->ipa_size);
1882 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1883 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1884 smmu->ipa_size, smmu->pa_size);
1889 struct arm_smmu_match_data {
1890 enum arm_smmu_arch_version version;
1891 enum arm_smmu_implementation model;
1894 #define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1895 static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
1897 ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1898 ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
1899 ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
1900 ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
1901 ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
1902 ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2);
1904 static const struct of_device_id arm_smmu_of_match[] = {
1905 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
1906 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
1907 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
1908 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
1909 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
1910 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
1911 { .compatible = "nvidia,smmu-500", .data = &arm_mmu500 },
1912 { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
1915 MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1918 static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
1923 case ACPI_IORT_SMMU_V1:
1924 case ACPI_IORT_SMMU_CORELINK_MMU400:
1925 smmu->version = ARM_SMMU_V1;
1926 smmu->model = GENERIC_SMMU;
1928 case ACPI_IORT_SMMU_CORELINK_MMU401:
1929 smmu->version = ARM_SMMU_V1_64K;
1930 smmu->model = GENERIC_SMMU;
1932 case ACPI_IORT_SMMU_V2:
1933 smmu->version = ARM_SMMU_V2;
1934 smmu->model = GENERIC_SMMU;
1936 case ACPI_IORT_SMMU_CORELINK_MMU500:
1937 smmu->version = ARM_SMMU_V2;
1938 smmu->model = ARM_MMU500;
1940 case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
1941 smmu->version = ARM_SMMU_V2;
1942 smmu->model = CAVIUM_SMMUV2;
1951 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1952 struct arm_smmu_device *smmu)
1954 struct device *dev = smmu->dev;
1955 struct acpi_iort_node *node =
1956 *(struct acpi_iort_node **)dev_get_platdata(dev);
1957 struct acpi_iort_smmu *iort_smmu;
1960 /* Retrieve SMMU1/2 specific data */
1961 iort_smmu = (struct acpi_iort_smmu *)node->node_data;
1963 ret = acpi_smmu_get_data(iort_smmu->model, smmu);
1967 /* Ignore the configuration access interrupt */
1968 smmu->num_global_irqs = 1;
1970 if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
1971 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1976 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1977 struct arm_smmu_device *smmu)
1983 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
1984 struct arm_smmu_device *smmu)
1986 const struct arm_smmu_match_data *data;
1987 struct device *dev = &pdev->dev;
1988 bool legacy_binding;
1990 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1991 &smmu->num_global_irqs)) {
1992 dev_err(dev, "missing #global-interrupts property\n");
1996 data = of_device_get_match_data(dev);
1997 smmu->version = data->version;
1998 smmu->model = data->model;
2000 legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
2001 if (legacy_binding && !using_generic_binding) {
2002 if (!using_legacy_binding) {
2003 pr_notice("deprecated \"mmu-masters\" DT property in use; %s support unavailable\n",
2004 IS_ENABLED(CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS) ? "DMA API" : "SMMU");
2006 using_legacy_binding = true;
2007 } else if (!legacy_binding && !using_legacy_binding) {
2008 using_generic_binding = true;
2010 dev_err(dev, "not probing due to mismatched DT properties\n");
2014 if (of_dma_is_coherent(dev->of_node))
2015 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2020 static int arm_smmu_bus_init(struct iommu_ops *ops)
2024 /* Oh, for a proper bus abstraction */
2025 if (!iommu_present(&platform_bus_type)) {
2026 err = bus_set_iommu(&platform_bus_type, ops);
2030 #ifdef CONFIG_ARM_AMBA
2031 if (!iommu_present(&amba_bustype)) {
2032 err = bus_set_iommu(&amba_bustype, ops);
2034 goto err_reset_platform_ops;
2038 if (!iommu_present(&pci_bus_type)) {
2039 err = bus_set_iommu(&pci_bus_type, ops);
2041 goto err_reset_amba_ops;
2044 #ifdef CONFIG_FSL_MC_BUS
2045 if (!iommu_present(&fsl_mc_bus_type)) {
2046 err = bus_set_iommu(&fsl_mc_bus_type, ops);
2048 goto err_reset_pci_ops;
2053 err_reset_pci_ops: __maybe_unused;
2055 bus_set_iommu(&pci_bus_type, NULL);
2057 err_reset_amba_ops: __maybe_unused;
2058 #ifdef CONFIG_ARM_AMBA
2059 bus_set_iommu(&amba_bustype, NULL);
2061 err_reset_platform_ops: __maybe_unused;
2062 bus_set_iommu(&platform_bus_type, NULL);
2066 static int arm_smmu_device_probe(struct platform_device *pdev)
2068 struct resource *res;
2069 resource_size_t ioaddr;
2070 struct arm_smmu_device *smmu;
2071 struct device *dev = &pdev->dev;
2072 int num_irqs, i, err;
2073 irqreturn_t (*global_fault)(int irq, void *dev);
2075 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2077 dev_err(dev, "failed to allocate arm_smmu_device\n");
2083 err = arm_smmu_device_dt_probe(pdev, smmu);
2085 err = arm_smmu_device_acpi_probe(pdev, smmu);
2090 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2091 ioaddr = res->start;
2092 smmu->base = devm_ioremap_resource(dev, res);
2093 if (IS_ERR(smmu->base))
2094 return PTR_ERR(smmu->base);
2096 * The resource size should effectively match the value of SMMU_TOP;
2097 * stash that temporarily until we know PAGESIZE to validate it with.
2099 smmu->numpage = resource_size(res);
2101 smmu = arm_smmu_impl_init(smmu);
2103 return PTR_ERR(smmu);
2106 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2108 if (num_irqs > smmu->num_global_irqs)
2109 smmu->num_context_irqs++;
2112 if (!smmu->num_context_irqs) {
2113 dev_err(dev, "found %d interrupts but expected at least %d\n",
2114 num_irqs, smmu->num_global_irqs + 1);
2118 smmu->irqs = devm_kcalloc(dev, num_irqs, sizeof(*smmu->irqs),
2121 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2125 for (i = 0; i < num_irqs; ++i) {
2126 int irq = platform_get_irq(pdev, i);
2130 smmu->irqs[i] = irq;
2133 err = devm_clk_bulk_get_all(dev, &smmu->clks);
2135 dev_err(dev, "failed to get clocks %d\n", err);
2138 smmu->num_clks = err;
2140 err = clk_bulk_prepare_enable(smmu->num_clks, smmu->clks);
2144 err = arm_smmu_device_cfg_probe(smmu);
2148 if (smmu->version == ARM_SMMU_V2) {
2149 if (smmu->num_context_banks > smmu->num_context_irqs) {
2151 "found only %d context irq(s) but %d required\n",
2152 smmu->num_context_irqs, smmu->num_context_banks);
2156 /* Ignore superfluous interrupts */
2157 smmu->num_context_irqs = smmu->num_context_banks;
2160 if (smmu->impl && smmu->impl->global_fault)
2161 global_fault = smmu->impl->global_fault;
2163 global_fault = arm_smmu_global_fault;
2165 for (i = 0; i < smmu->num_global_irqs; ++i) {
2166 err = devm_request_irq(smmu->dev, smmu->irqs[i],
2169 "arm-smmu global fault",
2172 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2178 err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2179 "smmu.%pa", &ioaddr);
2181 dev_err(dev, "Failed to register iommu in sysfs\n");
2185 err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
2187 dev_err(dev, "Failed to register iommu\n");
2188 goto err_sysfs_remove;
2191 platform_set_drvdata(pdev, smmu);
2192 arm_smmu_device_reset(smmu);
2193 arm_smmu_test_smr_masks(smmu);
2196 * We want to avoid touching dev->power.lock in fastpaths unless
2197 * it's really going to do something useful - pm_runtime_enabled()
2198 * can serve as an ideal proxy for that decision. So, conditionally
2199 * enable pm_runtime.
2201 if (dev->pm_domain) {
2202 pm_runtime_set_active(dev);
2203 pm_runtime_enable(dev);
2207 * For ACPI and generic DT bindings, an SMMU will be probed before
2208 * any device which might need it, so we want the bus ops in place
2209 * ready to handle default domain setup as soon as any SMMU exists.
2211 if (!using_legacy_binding) {
2212 err = arm_smmu_bus_init(&arm_smmu_ops);
2214 goto err_unregister_device;
2219 err_unregister_device:
2220 iommu_device_unregister(&smmu->iommu);
2222 iommu_device_sysfs_remove(&smmu->iommu);
2226 static int arm_smmu_device_remove(struct platform_device *pdev)
2228 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
2233 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
2234 dev_notice(&pdev->dev, "disabling translation\n");
2236 arm_smmu_bus_init(NULL);
2237 iommu_device_unregister(&smmu->iommu);
2238 iommu_device_sysfs_remove(&smmu->iommu);
2240 arm_smmu_rpm_get(smmu);
2241 /* Turn the thing off */
2242 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, ARM_SMMU_sCR0_CLIENTPD);
2243 arm_smmu_rpm_put(smmu);
2245 if (pm_runtime_enabled(smmu->dev))
2246 pm_runtime_force_suspend(smmu->dev);
2248 clk_bulk_disable(smmu->num_clks, smmu->clks);
2250 clk_bulk_unprepare(smmu->num_clks, smmu->clks);
2254 static void arm_smmu_device_shutdown(struct platform_device *pdev)
2256 arm_smmu_device_remove(pdev);
2259 static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
2261 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2264 ret = clk_bulk_enable(smmu->num_clks, smmu->clks);
2268 arm_smmu_device_reset(smmu);
2273 static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
2275 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2277 clk_bulk_disable(smmu->num_clks, smmu->clks);
2282 static int __maybe_unused arm_smmu_pm_resume(struct device *dev)
2284 if (pm_runtime_suspended(dev))
2287 return arm_smmu_runtime_resume(dev);
2290 static int __maybe_unused arm_smmu_pm_suspend(struct device *dev)
2292 if (pm_runtime_suspended(dev))
2295 return arm_smmu_runtime_suspend(dev);
2298 static const struct dev_pm_ops arm_smmu_pm_ops = {
2299 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume)
2300 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend,
2301 arm_smmu_runtime_resume, NULL)
2304 static struct platform_driver arm_smmu_driver = {
2307 .of_match_table = arm_smmu_of_match,
2308 .pm = &arm_smmu_pm_ops,
2309 .suppress_bind_attrs = true,
2311 .probe = arm_smmu_device_probe,
2312 .remove = arm_smmu_device_remove,
2313 .shutdown = arm_smmu_device_shutdown,
2315 module_platform_driver(arm_smmu_driver);
2317 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2318 MODULE_AUTHOR("Will Deacon <will@kernel.org>");
2319 MODULE_ALIAS("platform:arm-smmu");
2320 MODULE_LICENSE("GPL v2");