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 (smmu->impl && smmu->impl->init_context) {
769 ret = smmu->impl->init_context(smmu_domain, &pgtbl_cfg, dev);
774 if (smmu_domain->pgtbl_quirks)
775 pgtbl_cfg.quirks |= smmu_domain->pgtbl_quirks;
777 pgtbl_ops = alloc_io_pgtable_ops(fmt, &pgtbl_cfg, smmu_domain);
783 /* Update the domain's page sizes to reflect the page table format */
784 domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap;
786 if (pgtbl_cfg.quirks & IO_PGTABLE_QUIRK_ARM_TTBR1) {
787 domain->geometry.aperture_start = ~0UL << ias;
788 domain->geometry.aperture_end = ~0UL;
790 domain->geometry.aperture_end = (1UL << ias) - 1;
793 domain->geometry.force_aperture = true;
795 /* Initialise the context bank with our page table cfg */
796 arm_smmu_init_context_bank(smmu_domain, &pgtbl_cfg);
797 arm_smmu_write_context_bank(smmu, cfg->cbndx);
800 * Request context fault interrupt. Do this last to avoid the
801 * handler seeing a half-initialised domain state.
803 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
805 if (smmu->impl && smmu->impl->context_fault)
806 context_fault = smmu->impl->context_fault;
808 context_fault = arm_smmu_context_fault;
810 ret = devm_request_irq(smmu->dev, irq, context_fault,
811 IRQF_SHARED, "arm-smmu-context-fault", domain);
813 dev_err(smmu->dev, "failed to request context IRQ %d (%u)\n",
815 cfg->irptndx = ARM_SMMU_INVALID_IRPTNDX;
818 mutex_unlock(&smmu_domain->init_mutex);
820 /* Publish page table ops for map/unmap */
821 smmu_domain->pgtbl_ops = pgtbl_ops;
825 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
826 smmu_domain->smmu = NULL;
828 mutex_unlock(&smmu_domain->init_mutex);
832 static void arm_smmu_destroy_domain_context(struct iommu_domain *domain)
834 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
835 struct arm_smmu_device *smmu = smmu_domain->smmu;
836 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
839 if (!smmu || domain->type == IOMMU_DOMAIN_IDENTITY)
842 ret = arm_smmu_rpm_get(smmu);
847 * Disable the context bank and free the page tables before freeing
850 smmu->cbs[cfg->cbndx].cfg = NULL;
851 arm_smmu_write_context_bank(smmu, cfg->cbndx);
853 if (cfg->irptndx != ARM_SMMU_INVALID_IRPTNDX) {
854 irq = smmu->irqs[smmu->num_global_irqs + cfg->irptndx];
855 devm_free_irq(smmu->dev, irq, domain);
858 free_io_pgtable_ops(smmu_domain->pgtbl_ops);
859 __arm_smmu_free_bitmap(smmu->context_map, cfg->cbndx);
861 arm_smmu_rpm_put(smmu);
864 static struct iommu_domain *arm_smmu_domain_alloc(unsigned type)
866 struct arm_smmu_domain *smmu_domain;
868 if (type != IOMMU_DOMAIN_UNMANAGED && type != IOMMU_DOMAIN_IDENTITY) {
869 if (using_legacy_binding || type != IOMMU_DOMAIN_DMA)
873 * Allocate the domain and initialise some of its data structures.
874 * We can't really do anything meaningful until we've added a
877 smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL);
881 mutex_init(&smmu_domain->init_mutex);
882 spin_lock_init(&smmu_domain->cb_lock);
884 return &smmu_domain->domain;
887 static void arm_smmu_domain_free(struct iommu_domain *domain)
889 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
892 * Free the domain resources. We assume that all devices have
893 * already been detached.
895 arm_smmu_destroy_domain_context(domain);
899 static void arm_smmu_write_smr(struct arm_smmu_device *smmu, int idx)
901 struct arm_smmu_smr *smr = smmu->smrs + idx;
902 u32 reg = FIELD_PREP(ARM_SMMU_SMR_ID, smr->id) |
903 FIELD_PREP(ARM_SMMU_SMR_MASK, smr->mask);
905 if (!(smmu->features & ARM_SMMU_FEAT_EXIDS) && smr->valid)
906 reg |= ARM_SMMU_SMR_VALID;
907 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(idx), reg);
910 static void arm_smmu_write_s2cr(struct arm_smmu_device *smmu, int idx)
912 struct arm_smmu_s2cr *s2cr = smmu->s2crs + idx;
915 if (smmu->impl && smmu->impl->write_s2cr) {
916 smmu->impl->write_s2cr(smmu, idx);
920 reg = FIELD_PREP(ARM_SMMU_S2CR_TYPE, s2cr->type) |
921 FIELD_PREP(ARM_SMMU_S2CR_CBNDX, s2cr->cbndx) |
922 FIELD_PREP(ARM_SMMU_S2CR_PRIVCFG, s2cr->privcfg);
924 if (smmu->features & ARM_SMMU_FEAT_EXIDS && smmu->smrs &&
925 smmu->smrs[idx].valid)
926 reg |= ARM_SMMU_S2CR_EXIDVALID;
927 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_S2CR(idx), reg);
930 static void arm_smmu_write_sme(struct arm_smmu_device *smmu, int idx)
932 arm_smmu_write_s2cr(smmu, idx);
934 arm_smmu_write_smr(smmu, idx);
938 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
939 * should be called after sCR0 is written.
941 static void arm_smmu_test_smr_masks(struct arm_smmu_device *smmu)
949 * If we've had to accommodate firmware memory regions, we may
950 * have live SMRs by now; tread carefully...
952 * Somewhat perversely, not having a free SMR for this test implies we
953 * can get away without it anyway, as we'll only be able to 'allocate'
954 * these SMRs for the ID/mask values we're already trusting to be OK.
956 for (i = 0; i < smmu->num_mapping_groups; i++)
957 if (!smmu->smrs[i].valid)
962 * SMR.ID bits may not be preserved if the corresponding MASK
963 * bits are set, so check each one separately. We can reject
964 * masters later if they try to claim IDs outside these masks.
966 smr = FIELD_PREP(ARM_SMMU_SMR_ID, smmu->streamid_mask);
967 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
968 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
969 smmu->streamid_mask = FIELD_GET(ARM_SMMU_SMR_ID, smr);
971 smr = FIELD_PREP(ARM_SMMU_SMR_MASK, smmu->streamid_mask);
972 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_SMR(i), smr);
973 smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
974 smmu->smr_mask_mask = FIELD_GET(ARM_SMMU_SMR_MASK, smr);
977 static int arm_smmu_find_sme(struct arm_smmu_device *smmu, u16 id, u16 mask)
979 struct arm_smmu_smr *smrs = smmu->smrs;
980 int i, free_idx = -ENOSPC;
982 /* Stream indexing is blissfully easy */
986 /* Validating SMRs is... less so */
987 for (i = 0; i < smmu->num_mapping_groups; ++i) {
988 if (!smrs[i].valid) {
990 * Note the first free entry we come across, which
991 * we'll claim in the end if nothing else matches.
998 * If the new entry is _entirely_ matched by an existing entry,
999 * then reuse that, with the guarantee that there also cannot
1000 * be any subsequent conflicting entries. In normal use we'd
1001 * expect simply identical entries for this case, but there's
1002 * no harm in accommodating the generalisation.
1004 if ((mask & smrs[i].mask) == mask &&
1005 !((id ^ smrs[i].id) & ~smrs[i].mask))
1008 * If the new entry has any other overlap with an existing one,
1009 * though, then there always exists at least one stream ID
1010 * which would cause a conflict, and we can't allow that risk.
1012 if (!((id ^ smrs[i].id) & ~(smrs[i].mask | mask)))
1019 static bool arm_smmu_free_sme(struct arm_smmu_device *smmu, int idx)
1021 if (--smmu->s2crs[idx].count)
1024 smmu->s2crs[idx] = s2cr_init_val;
1026 smmu->smrs[idx].valid = false;
1031 static int arm_smmu_master_alloc_smes(struct device *dev)
1033 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1034 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1035 struct arm_smmu_device *smmu = cfg->smmu;
1036 struct arm_smmu_smr *smrs = smmu->smrs;
1039 mutex_lock(&smmu->stream_map_mutex);
1040 /* Figure out a viable stream map entry allocation */
1041 for_each_cfg_sme(cfg, fwspec, i, idx) {
1042 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1043 u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
1045 if (idx != INVALID_SMENDX) {
1050 ret = arm_smmu_find_sme(smmu, sid, mask);
1055 if (smrs && smmu->s2crs[idx].count == 0) {
1057 smrs[idx].mask = mask;
1058 smrs[idx].valid = true;
1060 smmu->s2crs[idx].count++;
1061 cfg->smendx[i] = (s16)idx;
1064 /* It worked! Now, poke the actual hardware */
1065 for_each_cfg_sme(cfg, fwspec, i, idx)
1066 arm_smmu_write_sme(smmu, idx);
1068 mutex_unlock(&smmu->stream_map_mutex);
1073 arm_smmu_free_sme(smmu, cfg->smendx[i]);
1074 cfg->smendx[i] = INVALID_SMENDX;
1076 mutex_unlock(&smmu->stream_map_mutex);
1080 static void arm_smmu_master_free_smes(struct arm_smmu_master_cfg *cfg,
1081 struct iommu_fwspec *fwspec)
1083 struct arm_smmu_device *smmu = cfg->smmu;
1086 mutex_lock(&smmu->stream_map_mutex);
1087 for_each_cfg_sme(cfg, fwspec, i, idx) {
1088 if (arm_smmu_free_sme(smmu, idx))
1089 arm_smmu_write_sme(smmu, idx);
1090 cfg->smendx[i] = INVALID_SMENDX;
1092 mutex_unlock(&smmu->stream_map_mutex);
1095 static int arm_smmu_domain_add_master(struct arm_smmu_domain *smmu_domain,
1096 struct arm_smmu_master_cfg *cfg,
1097 struct iommu_fwspec *fwspec)
1099 struct arm_smmu_device *smmu = smmu_domain->smmu;
1100 struct arm_smmu_s2cr *s2cr = smmu->s2crs;
1101 u8 cbndx = smmu_domain->cfg.cbndx;
1102 enum arm_smmu_s2cr_type type;
1105 if (smmu_domain->stage == ARM_SMMU_DOMAIN_BYPASS)
1106 type = S2CR_TYPE_BYPASS;
1108 type = S2CR_TYPE_TRANS;
1110 for_each_cfg_sme(cfg, fwspec, i, idx) {
1111 if (type == s2cr[idx].type && cbndx == s2cr[idx].cbndx)
1114 s2cr[idx].type = type;
1115 s2cr[idx].privcfg = S2CR_PRIVCFG_DEFAULT;
1116 s2cr[idx].cbndx = cbndx;
1117 arm_smmu_write_s2cr(smmu, idx);
1122 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
1124 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1125 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1126 struct arm_smmu_master_cfg *cfg;
1127 struct arm_smmu_device *smmu;
1130 if (!fwspec || fwspec->ops != &arm_smmu_ops) {
1131 dev_err(dev, "cannot attach to SMMU, is it on the same bus?\n");
1136 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1137 * domains between of_xlate() and probe_device() - we have no way to cope
1138 * with that, so until ARM gets converted to rely on groups and default
1139 * domains, just say no (but more politely than by dereferencing NULL).
1140 * This should be at least a WARN_ON once that's sorted.
1142 cfg = dev_iommu_priv_get(dev);
1148 ret = arm_smmu_rpm_get(smmu);
1152 /* Ensure that the domain is finalised */
1153 ret = arm_smmu_init_domain_context(domain, smmu, dev);
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, cfg, fwspec);
1173 * Setup an autosuspend delay to avoid bouncing runpm state.
1174 * Otherwise, if a driver for a suspended consumer device
1175 * unmaps buffers, it will runpm resume/suspend for each one.
1177 * For example, when used by a GPU device, when an application
1178 * or game exits, it can trigger unmapping 100s or 1000s of
1179 * buffers. With a runpm cycle for each buffer, that adds up
1180 * to 5-10sec worth of reprogramming the context bank, while
1181 * the system appears to be locked up to the user.
1183 pm_runtime_set_autosuspend_delay(smmu->dev, 20);
1184 pm_runtime_use_autosuspend(smmu->dev);
1187 arm_smmu_rpm_put(smmu);
1191 static int arm_smmu_map_pages(struct iommu_domain *domain, unsigned long iova,
1192 phys_addr_t paddr, size_t pgsize, size_t pgcount,
1193 int prot, gfp_t gfp, size_t *mapped)
1195 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1196 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1202 arm_smmu_rpm_get(smmu);
1203 ret = ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot, gfp, mapped);
1204 arm_smmu_rpm_put(smmu);
1209 static size_t arm_smmu_unmap_pages(struct iommu_domain *domain, unsigned long iova,
1210 size_t pgsize, size_t pgcount,
1211 struct iommu_iotlb_gather *iotlb_gather)
1213 struct io_pgtable_ops *ops = to_smmu_domain(domain)->pgtbl_ops;
1214 struct arm_smmu_device *smmu = to_smmu_domain(domain)->smmu;
1220 arm_smmu_rpm_get(smmu);
1221 ret = ops->unmap_pages(ops, iova, pgsize, pgcount, iotlb_gather);
1222 arm_smmu_rpm_put(smmu);
1227 static void arm_smmu_flush_iotlb_all(struct iommu_domain *domain)
1229 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1230 struct arm_smmu_device *smmu = smmu_domain->smmu;
1232 if (smmu_domain->flush_ops) {
1233 arm_smmu_rpm_get(smmu);
1234 smmu_domain->flush_ops->tlb_flush_all(smmu_domain);
1235 arm_smmu_rpm_put(smmu);
1239 static void arm_smmu_iotlb_sync(struct iommu_domain *domain,
1240 struct iommu_iotlb_gather *gather)
1242 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1243 struct arm_smmu_device *smmu = smmu_domain->smmu;
1248 arm_smmu_rpm_get(smmu);
1249 if (smmu->version == ARM_SMMU_V2 ||
1250 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1251 arm_smmu_tlb_sync_context(smmu_domain);
1253 arm_smmu_tlb_sync_global(smmu);
1254 arm_smmu_rpm_put(smmu);
1257 static phys_addr_t arm_smmu_iova_to_phys_hard(struct iommu_domain *domain,
1260 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1261 struct arm_smmu_device *smmu = smmu_domain->smmu;
1262 struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
1263 struct io_pgtable_ops *ops= smmu_domain->pgtbl_ops;
1264 struct device *dev = smmu->dev;
1268 unsigned long va, flags;
1269 int ret, idx = cfg->cbndx;
1270 phys_addr_t addr = 0;
1272 ret = arm_smmu_rpm_get(smmu);
1276 spin_lock_irqsave(&smmu_domain->cb_lock, flags);
1277 va = iova & ~0xfffUL;
1278 if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
1279 arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1281 arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
1283 reg = arm_smmu_page(smmu, ARM_SMMU_CB(smmu, idx)) + ARM_SMMU_CB_ATSR;
1284 if (readl_poll_timeout_atomic(reg, tmp, !(tmp & ARM_SMMU_ATSR_ACTIVE),
1286 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1288 "iova to phys timed out on %pad. Falling back to software table walk.\n",
1290 arm_smmu_rpm_put(smmu);
1291 return ops->iova_to_phys(ops, iova);
1294 phys = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_PAR);
1295 spin_unlock_irqrestore(&smmu_domain->cb_lock, flags);
1296 if (phys & ARM_SMMU_CB_PAR_F) {
1297 dev_err(dev, "translation fault!\n");
1298 dev_err(dev, "PAR = 0x%llx\n", phys);
1302 addr = (phys & GENMASK_ULL(39, 12)) | (iova & 0xfff);
1304 arm_smmu_rpm_put(smmu);
1309 static phys_addr_t arm_smmu_iova_to_phys(struct iommu_domain *domain,
1312 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1313 struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops;
1318 if (smmu_domain->smmu->features & ARM_SMMU_FEAT_TRANS_OPS &&
1319 smmu_domain->stage == ARM_SMMU_DOMAIN_S1)
1320 return arm_smmu_iova_to_phys_hard(domain, iova);
1322 return ops->iova_to_phys(ops, iova);
1325 static bool arm_smmu_capable(enum iommu_cap cap)
1328 case IOMMU_CAP_CACHE_COHERENCY:
1330 * Return true here as the SMMU can always send out coherent
1334 case IOMMU_CAP_NOEXEC:
1342 struct arm_smmu_device *arm_smmu_get_by_fwnode(struct fwnode_handle *fwnode)
1344 struct device *dev = driver_find_device_by_fwnode(&arm_smmu_driver.driver,
1347 return dev ? dev_get_drvdata(dev) : NULL;
1350 static struct iommu_device *arm_smmu_probe_device(struct device *dev)
1352 struct arm_smmu_device *smmu = NULL;
1353 struct arm_smmu_master_cfg *cfg;
1354 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1357 if (using_legacy_binding) {
1358 ret = arm_smmu_register_legacy_master(dev, &smmu);
1361 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1362 * will allocate/initialise a new one. Thus we need to update fwspec for
1365 fwspec = dev_iommu_fwspec_get(dev);
1368 } else if (fwspec && fwspec->ops == &arm_smmu_ops) {
1369 smmu = arm_smmu_get_by_fwnode(fwspec->iommu_fwnode);
1371 return ERR_PTR(-ENODEV);
1375 for (i = 0; i < fwspec->num_ids; i++) {
1376 u16 sid = FIELD_GET(ARM_SMMU_SMR_ID, fwspec->ids[i]);
1377 u16 mask = FIELD_GET(ARM_SMMU_SMR_MASK, fwspec->ids[i]);
1379 if (sid & ~smmu->streamid_mask) {
1380 dev_err(dev, "stream ID 0x%x out of range for SMMU (0x%x)\n",
1381 sid, smmu->streamid_mask);
1384 if (mask & ~smmu->smr_mask_mask) {
1385 dev_err(dev, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1386 mask, smmu->smr_mask_mask);
1392 cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
1398 dev_iommu_priv_set(dev, cfg);
1400 cfg->smendx[i] = INVALID_SMENDX;
1402 ret = arm_smmu_rpm_get(smmu);
1406 ret = arm_smmu_master_alloc_smes(dev);
1407 arm_smmu_rpm_put(smmu);
1412 device_link_add(dev, smmu->dev,
1413 DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_SUPPLIER);
1415 return &smmu->iommu;
1420 iommu_fwspec_free(dev);
1421 return ERR_PTR(ret);
1424 static void arm_smmu_release_device(struct device *dev)
1426 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1427 struct arm_smmu_master_cfg *cfg;
1428 struct arm_smmu_device *smmu;
1431 if (!fwspec || fwspec->ops != &arm_smmu_ops)
1434 cfg = dev_iommu_priv_get(dev);
1437 ret = arm_smmu_rpm_get(smmu);
1441 arm_smmu_master_free_smes(cfg, fwspec);
1443 arm_smmu_rpm_put(smmu);
1445 dev_iommu_priv_set(dev, NULL);
1447 iommu_fwspec_free(dev);
1450 static void arm_smmu_probe_finalize(struct device *dev)
1452 struct arm_smmu_master_cfg *cfg;
1453 struct arm_smmu_device *smmu;
1455 cfg = dev_iommu_priv_get(dev);
1458 if (smmu->impl && smmu->impl->probe_finalize)
1459 smmu->impl->probe_finalize(smmu, dev);
1462 static struct iommu_group *arm_smmu_device_group(struct device *dev)
1464 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1465 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1466 struct arm_smmu_device *smmu = cfg->smmu;
1467 struct iommu_group *group = NULL;
1470 for_each_cfg_sme(cfg, fwspec, i, idx) {
1471 if (group && smmu->s2crs[idx].group &&
1472 group != smmu->s2crs[idx].group)
1473 return ERR_PTR(-EINVAL);
1475 group = smmu->s2crs[idx].group;
1479 return iommu_group_ref_get(group);
1481 if (dev_is_pci(dev))
1482 group = pci_device_group(dev);
1483 else if (dev_is_fsl_mc(dev))
1484 group = fsl_mc_device_group(dev);
1486 group = generic_device_group(dev);
1488 /* Remember group for faster lookups */
1490 for_each_cfg_sme(cfg, fwspec, i, idx)
1491 smmu->s2crs[idx].group = group;
1496 static int arm_smmu_enable_nesting(struct iommu_domain *domain)
1498 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1501 mutex_lock(&smmu_domain->init_mutex);
1502 if (smmu_domain->smmu)
1505 smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
1506 mutex_unlock(&smmu_domain->init_mutex);
1511 static int arm_smmu_set_pgtable_quirks(struct iommu_domain *domain,
1512 unsigned long quirks)
1514 struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
1517 mutex_lock(&smmu_domain->init_mutex);
1518 if (smmu_domain->smmu)
1521 smmu_domain->pgtbl_quirks = quirks;
1522 mutex_unlock(&smmu_domain->init_mutex);
1527 static int arm_smmu_of_xlate(struct device *dev, struct of_phandle_args *args)
1531 if (args->args_count > 0)
1532 fwid |= FIELD_PREP(ARM_SMMU_SMR_ID, args->args[0]);
1534 if (args->args_count > 1)
1535 fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, args->args[1]);
1536 else if (!of_property_read_u32(args->np, "stream-match-mask", &mask))
1537 fwid |= FIELD_PREP(ARM_SMMU_SMR_MASK, mask);
1539 return iommu_fwspec_add_ids(dev, &fwid, 1);
1542 static void arm_smmu_get_resv_regions(struct device *dev,
1543 struct list_head *head)
1545 struct iommu_resv_region *region;
1546 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1548 region = iommu_alloc_resv_region(MSI_IOVA_BASE, MSI_IOVA_LENGTH,
1549 prot, IOMMU_RESV_SW_MSI);
1553 list_add_tail(®ion->list, head);
1555 iommu_dma_get_resv_regions(dev, head);
1558 static int arm_smmu_def_domain_type(struct device *dev)
1560 struct arm_smmu_master_cfg *cfg = dev_iommu_priv_get(dev);
1561 const struct arm_smmu_impl *impl = cfg->smmu->impl;
1563 if (impl && impl->def_domain_type)
1564 return impl->def_domain_type(dev);
1569 static struct iommu_ops arm_smmu_ops = {
1570 .capable = arm_smmu_capable,
1571 .domain_alloc = arm_smmu_domain_alloc,
1572 .domain_free = arm_smmu_domain_free,
1573 .attach_dev = arm_smmu_attach_dev,
1574 .map_pages = arm_smmu_map_pages,
1575 .unmap_pages = arm_smmu_unmap_pages,
1576 .flush_iotlb_all = arm_smmu_flush_iotlb_all,
1577 .iotlb_sync = arm_smmu_iotlb_sync,
1578 .iova_to_phys = arm_smmu_iova_to_phys,
1579 .probe_device = arm_smmu_probe_device,
1580 .release_device = arm_smmu_release_device,
1581 .probe_finalize = arm_smmu_probe_finalize,
1582 .device_group = arm_smmu_device_group,
1583 .enable_nesting = arm_smmu_enable_nesting,
1584 .set_pgtable_quirks = arm_smmu_set_pgtable_quirks,
1585 .of_xlate = arm_smmu_of_xlate,
1586 .get_resv_regions = arm_smmu_get_resv_regions,
1587 .put_resv_regions = generic_iommu_put_resv_regions,
1588 .def_domain_type = arm_smmu_def_domain_type,
1589 .pgsize_bitmap = -1UL, /* Restricted during device attach */
1590 .owner = THIS_MODULE,
1593 static void arm_smmu_device_reset(struct arm_smmu_device *smmu)
1598 /* clear global FSR */
1599 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sGFSR);
1600 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sGFSR, reg);
1603 * Reset stream mapping groups: Initial values mark all SMRn as
1604 * invalid and all S2CRn as bypass unless overridden.
1606 for (i = 0; i < smmu->num_mapping_groups; ++i)
1607 arm_smmu_write_sme(smmu, i);
1609 /* Make sure all context banks are disabled and clear CB_FSR */
1610 for (i = 0; i < smmu->num_context_banks; ++i) {
1611 arm_smmu_write_context_bank(smmu, i);
1612 arm_smmu_cb_write(smmu, i, ARM_SMMU_CB_FSR, ARM_SMMU_FSR_FAULT);
1615 /* Invalidate the TLB, just in case */
1616 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLH, QCOM_DUMMY_VAL);
1617 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_TLBIALLNSNH, QCOM_DUMMY_VAL);
1619 reg = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sCR0);
1621 /* Enable fault reporting */
1622 reg |= (ARM_SMMU_sCR0_GFRE | ARM_SMMU_sCR0_GFIE |
1623 ARM_SMMU_sCR0_GCFGFRE | ARM_SMMU_sCR0_GCFGFIE);
1625 /* Disable TLB broadcasting. */
1626 reg |= (ARM_SMMU_sCR0_VMIDPNE | ARM_SMMU_sCR0_PTM);
1628 /* Enable client access, handling unmatched streams as appropriate */
1629 reg &= ~ARM_SMMU_sCR0_CLIENTPD;
1631 reg |= ARM_SMMU_sCR0_USFCFG;
1633 reg &= ~ARM_SMMU_sCR0_USFCFG;
1635 /* Disable forced broadcasting */
1636 reg &= ~ARM_SMMU_sCR0_FB;
1638 /* Don't upgrade barriers */
1639 reg &= ~(ARM_SMMU_sCR0_BSU);
1641 if (smmu->features & ARM_SMMU_FEAT_VMID16)
1642 reg |= ARM_SMMU_sCR0_VMID16EN;
1644 if (smmu->features & ARM_SMMU_FEAT_EXIDS)
1645 reg |= ARM_SMMU_sCR0_EXIDENABLE;
1647 if (smmu->impl && smmu->impl->reset)
1648 smmu->impl->reset(smmu);
1650 /* Push the button */
1651 arm_smmu_tlb_sync_global(smmu);
1652 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, reg);
1655 static int arm_smmu_id_size_to_bits(int size)
1674 static int arm_smmu_device_cfg_probe(struct arm_smmu_device *smmu)
1678 bool cttw_reg, cttw_fw = smmu->features & ARM_SMMU_FEAT_COHERENT_WALK;
1681 dev_notice(smmu->dev, "probing hardware configuration...\n");
1682 dev_notice(smmu->dev, "SMMUv%d with:\n",
1683 smmu->version == ARM_SMMU_V2 ? 2 : 1);
1686 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID0);
1688 /* Restrict available stages based on module parameter */
1689 if (force_stage == 1)
1690 id &= ~(ARM_SMMU_ID0_S2TS | ARM_SMMU_ID0_NTS);
1691 else if (force_stage == 2)
1692 id &= ~(ARM_SMMU_ID0_S1TS | ARM_SMMU_ID0_NTS);
1694 if (id & ARM_SMMU_ID0_S1TS) {
1695 smmu->features |= ARM_SMMU_FEAT_TRANS_S1;
1696 dev_notice(smmu->dev, "\tstage 1 translation\n");
1699 if (id & ARM_SMMU_ID0_S2TS) {
1700 smmu->features |= ARM_SMMU_FEAT_TRANS_S2;
1701 dev_notice(smmu->dev, "\tstage 2 translation\n");
1704 if (id & ARM_SMMU_ID0_NTS) {
1705 smmu->features |= ARM_SMMU_FEAT_TRANS_NESTED;
1706 dev_notice(smmu->dev, "\tnested translation\n");
1709 if (!(smmu->features &
1710 (ARM_SMMU_FEAT_TRANS_S1 | ARM_SMMU_FEAT_TRANS_S2))) {
1711 dev_err(smmu->dev, "\tno translation support!\n");
1715 if ((id & ARM_SMMU_ID0_S1TS) &&
1716 ((smmu->version < ARM_SMMU_V2) || !(id & ARM_SMMU_ID0_ATOSNS))) {
1717 smmu->features |= ARM_SMMU_FEAT_TRANS_OPS;
1718 dev_notice(smmu->dev, "\taddress translation ops\n");
1722 * In order for DMA API calls to work properly, we must defer to what
1723 * the FW says about coherency, regardless of what the hardware claims.
1724 * Fortunately, this also opens up a workaround for systems where the
1725 * ID register value has ended up configured incorrectly.
1727 cttw_reg = !!(id & ARM_SMMU_ID0_CTTW);
1728 if (cttw_fw || cttw_reg)
1729 dev_notice(smmu->dev, "\t%scoherent table walk\n",
1730 cttw_fw ? "" : "non-");
1731 if (cttw_fw != cttw_reg)
1732 dev_notice(smmu->dev,
1733 "\t(IDR0.CTTW overridden by FW configuration)\n");
1735 /* Max. number of entries we have for stream matching/indexing */
1736 if (smmu->version == ARM_SMMU_V2 && id & ARM_SMMU_ID0_EXIDS) {
1737 smmu->features |= ARM_SMMU_FEAT_EXIDS;
1740 size = 1 << FIELD_GET(ARM_SMMU_ID0_NUMSIDB, id);
1742 smmu->streamid_mask = size - 1;
1743 if (id & ARM_SMMU_ID0_SMS) {
1744 smmu->features |= ARM_SMMU_FEAT_STREAM_MATCH;
1745 size = FIELD_GET(ARM_SMMU_ID0_NUMSMRG, id);
1748 "stream-matching supported, but no SMRs present!\n");
1752 /* Zero-initialised to mark as invalid */
1753 smmu->smrs = devm_kcalloc(smmu->dev, size, sizeof(*smmu->smrs),
1758 dev_notice(smmu->dev,
1759 "\tstream matching with %u register groups", size);
1761 /* s2cr->type == 0 means translation, so initialise explicitly */
1762 smmu->s2crs = devm_kmalloc_array(smmu->dev, size, sizeof(*smmu->s2crs),
1766 for (i = 0; i < size; i++)
1767 smmu->s2crs[i] = s2cr_init_val;
1769 smmu->num_mapping_groups = size;
1770 mutex_init(&smmu->stream_map_mutex);
1771 spin_lock_init(&smmu->global_sync_lock);
1773 if (smmu->version < ARM_SMMU_V2 ||
1774 !(id & ARM_SMMU_ID0_PTFS_NO_AARCH32)) {
1775 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_L;
1776 if (!(id & ARM_SMMU_ID0_PTFS_NO_AARCH32S))
1777 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH32_S;
1781 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID1);
1782 smmu->pgshift = (id & ARM_SMMU_ID1_PAGESIZE) ? 16 : 12;
1784 /* Check for size mismatch of SMMU address space from mapped region */
1785 size = 1 << (FIELD_GET(ARM_SMMU_ID1_NUMPAGENDXB, id) + 1);
1786 if (smmu->numpage != 2 * size << smmu->pgshift)
1788 "SMMU address space size (0x%x) differs from mapped region size (0x%x)!\n",
1789 2 * size << smmu->pgshift, smmu->numpage);
1790 /* Now properly encode NUMPAGE to subsequently derive SMMU_CB_BASE */
1791 smmu->numpage = size;
1793 smmu->num_s2_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMS2CB, id);
1794 smmu->num_context_banks = FIELD_GET(ARM_SMMU_ID1_NUMCB, id);
1795 if (smmu->num_s2_context_banks > smmu->num_context_banks) {
1796 dev_err(smmu->dev, "impossible number of S2 context banks!\n");
1799 dev_notice(smmu->dev, "\t%u context banks (%u stage-2 only)\n",
1800 smmu->num_context_banks, smmu->num_s2_context_banks);
1801 smmu->cbs = devm_kcalloc(smmu->dev, smmu->num_context_banks,
1802 sizeof(*smmu->cbs), GFP_KERNEL);
1807 id = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_ID2);
1808 size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_IAS, id));
1809 smmu->ipa_size = size;
1811 /* The output mask is also applied for bypass */
1812 size = arm_smmu_id_size_to_bits(FIELD_GET(ARM_SMMU_ID2_OAS, id));
1813 smmu->pa_size = size;
1815 if (id & ARM_SMMU_ID2_VMID16)
1816 smmu->features |= ARM_SMMU_FEAT_VMID16;
1819 * What the page table walker can address actually depends on which
1820 * descriptor format is in use, but since a) we don't know that yet,
1821 * and b) it can vary per context bank, this will have to do...
1823 if (dma_set_mask_and_coherent(smmu->dev, DMA_BIT_MASK(size)))
1825 "failed to set DMA mask for table walker\n");
1827 if (smmu->version < ARM_SMMU_V2) {
1828 smmu->va_size = smmu->ipa_size;
1829 if (smmu->version == ARM_SMMU_V1_64K)
1830 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1832 size = FIELD_GET(ARM_SMMU_ID2_UBS, id);
1833 smmu->va_size = arm_smmu_id_size_to_bits(size);
1834 if (id & ARM_SMMU_ID2_PTFS_4K)
1835 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_4K;
1836 if (id & ARM_SMMU_ID2_PTFS_16K)
1837 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_16K;
1838 if (id & ARM_SMMU_ID2_PTFS_64K)
1839 smmu->features |= ARM_SMMU_FEAT_FMT_AARCH64_64K;
1842 if (smmu->impl && smmu->impl->cfg_probe) {
1843 ret = smmu->impl->cfg_probe(smmu);
1848 /* Now we've corralled the various formats, what'll it do? */
1849 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH32_S)
1850 smmu->pgsize_bitmap |= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
1851 if (smmu->features &
1852 (ARM_SMMU_FEAT_FMT_AARCH32_L | ARM_SMMU_FEAT_FMT_AARCH64_4K))
1853 smmu->pgsize_bitmap |= SZ_4K | SZ_2M | SZ_1G;
1854 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_16K)
1855 smmu->pgsize_bitmap |= SZ_16K | SZ_32M;
1856 if (smmu->features & ARM_SMMU_FEAT_FMT_AARCH64_64K)
1857 smmu->pgsize_bitmap |= SZ_64K | SZ_512M;
1859 if (arm_smmu_ops.pgsize_bitmap == -1UL)
1860 arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap;
1862 arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap;
1863 dev_notice(smmu->dev, "\tSupported page sizes: 0x%08lx\n",
1864 smmu->pgsize_bitmap);
1867 if (smmu->features & ARM_SMMU_FEAT_TRANS_S1)
1868 dev_notice(smmu->dev, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1869 smmu->va_size, smmu->ipa_size);
1871 if (smmu->features & ARM_SMMU_FEAT_TRANS_S2)
1872 dev_notice(smmu->dev, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1873 smmu->ipa_size, smmu->pa_size);
1878 struct arm_smmu_match_data {
1879 enum arm_smmu_arch_version version;
1880 enum arm_smmu_implementation model;
1883 #define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1884 static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
1886 ARM_SMMU_MATCH_DATA(smmu_generic_v1, ARM_SMMU_V1, GENERIC_SMMU);
1887 ARM_SMMU_MATCH_DATA(smmu_generic_v2, ARM_SMMU_V2, GENERIC_SMMU);
1888 ARM_SMMU_MATCH_DATA(arm_mmu401, ARM_SMMU_V1_64K, GENERIC_SMMU);
1889 ARM_SMMU_MATCH_DATA(arm_mmu500, ARM_SMMU_V2, ARM_MMU500);
1890 ARM_SMMU_MATCH_DATA(cavium_smmuv2, ARM_SMMU_V2, CAVIUM_SMMUV2);
1891 ARM_SMMU_MATCH_DATA(qcom_smmuv2, ARM_SMMU_V2, QCOM_SMMUV2);
1893 static const struct of_device_id arm_smmu_of_match[] = {
1894 { .compatible = "arm,smmu-v1", .data = &smmu_generic_v1 },
1895 { .compatible = "arm,smmu-v2", .data = &smmu_generic_v2 },
1896 { .compatible = "arm,mmu-400", .data = &smmu_generic_v1 },
1897 { .compatible = "arm,mmu-401", .data = &arm_mmu401 },
1898 { .compatible = "arm,mmu-500", .data = &arm_mmu500 },
1899 { .compatible = "cavium,smmu-v2", .data = &cavium_smmuv2 },
1900 { .compatible = "nvidia,smmu-500", .data = &arm_mmu500 },
1901 { .compatible = "qcom,smmu-v2", .data = &qcom_smmuv2 },
1904 MODULE_DEVICE_TABLE(of, arm_smmu_of_match);
1907 static int acpi_smmu_get_data(u32 model, struct arm_smmu_device *smmu)
1912 case ACPI_IORT_SMMU_V1:
1913 case ACPI_IORT_SMMU_CORELINK_MMU400:
1914 smmu->version = ARM_SMMU_V1;
1915 smmu->model = GENERIC_SMMU;
1917 case ACPI_IORT_SMMU_CORELINK_MMU401:
1918 smmu->version = ARM_SMMU_V1_64K;
1919 smmu->model = GENERIC_SMMU;
1921 case ACPI_IORT_SMMU_V2:
1922 smmu->version = ARM_SMMU_V2;
1923 smmu->model = GENERIC_SMMU;
1925 case ACPI_IORT_SMMU_CORELINK_MMU500:
1926 smmu->version = ARM_SMMU_V2;
1927 smmu->model = ARM_MMU500;
1929 case ACPI_IORT_SMMU_CAVIUM_THUNDERX:
1930 smmu->version = ARM_SMMU_V2;
1931 smmu->model = CAVIUM_SMMUV2;
1940 static int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1941 struct arm_smmu_device *smmu)
1943 struct device *dev = smmu->dev;
1944 struct acpi_iort_node *node =
1945 *(struct acpi_iort_node **)dev_get_platdata(dev);
1946 struct acpi_iort_smmu *iort_smmu;
1949 /* Retrieve SMMU1/2 specific data */
1950 iort_smmu = (struct acpi_iort_smmu *)node->node_data;
1952 ret = acpi_smmu_get_data(iort_smmu->model, smmu);
1956 /* Ignore the configuration access interrupt */
1957 smmu->num_global_irqs = 1;
1959 if (iort_smmu->flags & ACPI_IORT_SMMU_COHERENT_WALK)
1960 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
1965 static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,
1966 struct arm_smmu_device *smmu)
1972 static int arm_smmu_device_dt_probe(struct platform_device *pdev,
1973 struct arm_smmu_device *smmu)
1975 const struct arm_smmu_match_data *data;
1976 struct device *dev = &pdev->dev;
1977 bool legacy_binding;
1979 if (of_property_read_u32(dev->of_node, "#global-interrupts",
1980 &smmu->num_global_irqs)) {
1981 dev_err(dev, "missing #global-interrupts property\n");
1985 data = of_device_get_match_data(dev);
1986 smmu->version = data->version;
1987 smmu->model = data->model;
1989 legacy_binding = of_find_property(dev->of_node, "mmu-masters", NULL);
1990 if (legacy_binding && !using_generic_binding) {
1991 if (!using_legacy_binding) {
1992 pr_notice("deprecated \"mmu-masters\" DT property in use; %s support unavailable\n",
1993 IS_ENABLED(CONFIG_ARM_SMMU_LEGACY_DT_BINDINGS) ? "DMA API" : "SMMU");
1995 using_legacy_binding = true;
1996 } else if (!legacy_binding && !using_legacy_binding) {
1997 using_generic_binding = true;
1999 dev_err(dev, "not probing due to mismatched DT properties\n");
2003 if (of_dma_is_coherent(dev->of_node))
2004 smmu->features |= ARM_SMMU_FEAT_COHERENT_WALK;
2009 static int arm_smmu_bus_init(struct iommu_ops *ops)
2013 /* Oh, for a proper bus abstraction */
2014 if (!iommu_present(&platform_bus_type)) {
2015 err = bus_set_iommu(&platform_bus_type, ops);
2019 #ifdef CONFIG_ARM_AMBA
2020 if (!iommu_present(&amba_bustype)) {
2021 err = bus_set_iommu(&amba_bustype, ops);
2023 goto err_reset_platform_ops;
2027 if (!iommu_present(&pci_bus_type)) {
2028 err = bus_set_iommu(&pci_bus_type, ops);
2030 goto err_reset_amba_ops;
2033 #ifdef CONFIG_FSL_MC_BUS
2034 if (!iommu_present(&fsl_mc_bus_type)) {
2035 err = bus_set_iommu(&fsl_mc_bus_type, ops);
2037 goto err_reset_pci_ops;
2042 err_reset_pci_ops: __maybe_unused;
2044 bus_set_iommu(&pci_bus_type, NULL);
2046 err_reset_amba_ops: __maybe_unused;
2047 #ifdef CONFIG_ARM_AMBA
2048 bus_set_iommu(&amba_bustype, NULL);
2050 err_reset_platform_ops: __maybe_unused;
2051 bus_set_iommu(&platform_bus_type, NULL);
2055 static int arm_smmu_device_probe(struct platform_device *pdev)
2057 struct resource *res;
2058 resource_size_t ioaddr;
2059 struct arm_smmu_device *smmu;
2060 struct device *dev = &pdev->dev;
2061 int num_irqs, i, err;
2062 irqreturn_t (*global_fault)(int irq, void *dev);
2064 smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
2066 dev_err(dev, "failed to allocate arm_smmu_device\n");
2072 err = arm_smmu_device_dt_probe(pdev, smmu);
2074 err = arm_smmu_device_acpi_probe(pdev, smmu);
2079 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2080 ioaddr = res->start;
2081 smmu->base = devm_ioremap_resource(dev, res);
2082 if (IS_ERR(smmu->base))
2083 return PTR_ERR(smmu->base);
2085 * The resource size should effectively match the value of SMMU_TOP;
2086 * stash that temporarily until we know PAGESIZE to validate it with.
2088 smmu->numpage = resource_size(res);
2090 smmu = arm_smmu_impl_init(smmu);
2092 return PTR_ERR(smmu);
2095 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs))) {
2097 if (num_irqs > smmu->num_global_irqs)
2098 smmu->num_context_irqs++;
2101 if (!smmu->num_context_irqs) {
2102 dev_err(dev, "found %d interrupts but expected at least %d\n",
2103 num_irqs, smmu->num_global_irqs + 1);
2107 smmu->irqs = devm_kcalloc(dev, num_irqs, sizeof(*smmu->irqs),
2110 dev_err(dev, "failed to allocate %d irqs\n", num_irqs);
2114 for (i = 0; i < num_irqs; ++i) {
2115 int irq = platform_get_irq(pdev, i);
2119 smmu->irqs[i] = irq;
2122 err = devm_clk_bulk_get_all(dev, &smmu->clks);
2124 dev_err(dev, "failed to get clocks %d\n", err);
2127 smmu->num_clks = err;
2129 err = clk_bulk_prepare_enable(smmu->num_clks, smmu->clks);
2133 err = arm_smmu_device_cfg_probe(smmu);
2137 if (smmu->version == ARM_SMMU_V2) {
2138 if (smmu->num_context_banks > smmu->num_context_irqs) {
2140 "found only %d context irq(s) but %d required\n",
2141 smmu->num_context_irqs, smmu->num_context_banks);
2145 /* Ignore superfluous interrupts */
2146 smmu->num_context_irqs = smmu->num_context_banks;
2149 if (smmu->impl && smmu->impl->global_fault)
2150 global_fault = smmu->impl->global_fault;
2152 global_fault = arm_smmu_global_fault;
2154 for (i = 0; i < smmu->num_global_irqs; ++i) {
2155 err = devm_request_irq(smmu->dev, smmu->irqs[i],
2158 "arm-smmu global fault",
2161 dev_err(dev, "failed to request global IRQ %d (%u)\n",
2167 err = iommu_device_sysfs_add(&smmu->iommu, smmu->dev, NULL,
2168 "smmu.%pa", &ioaddr);
2170 dev_err(dev, "Failed to register iommu in sysfs\n");
2174 err = iommu_device_register(&smmu->iommu, &arm_smmu_ops, dev);
2176 dev_err(dev, "Failed to register iommu\n");
2177 goto err_sysfs_remove;
2180 platform_set_drvdata(pdev, smmu);
2181 arm_smmu_device_reset(smmu);
2182 arm_smmu_test_smr_masks(smmu);
2185 * We want to avoid touching dev->power.lock in fastpaths unless
2186 * it's really going to do something useful - pm_runtime_enabled()
2187 * can serve as an ideal proxy for that decision. So, conditionally
2188 * enable pm_runtime.
2190 if (dev->pm_domain) {
2191 pm_runtime_set_active(dev);
2192 pm_runtime_enable(dev);
2196 * For ACPI and generic DT bindings, an SMMU will be probed before
2197 * any device which might need it, so we want the bus ops in place
2198 * ready to handle default domain setup as soon as any SMMU exists.
2200 if (!using_legacy_binding) {
2201 err = arm_smmu_bus_init(&arm_smmu_ops);
2203 goto err_unregister_device;
2208 err_unregister_device:
2209 iommu_device_unregister(&smmu->iommu);
2211 iommu_device_sysfs_remove(&smmu->iommu);
2215 static int arm_smmu_device_remove(struct platform_device *pdev)
2217 struct arm_smmu_device *smmu = platform_get_drvdata(pdev);
2222 if (!bitmap_empty(smmu->context_map, ARM_SMMU_MAX_CBS))
2223 dev_notice(&pdev->dev, "disabling translation\n");
2225 arm_smmu_bus_init(NULL);
2226 iommu_device_unregister(&smmu->iommu);
2227 iommu_device_sysfs_remove(&smmu->iommu);
2229 arm_smmu_rpm_get(smmu);
2230 /* Turn the thing off */
2231 arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sCR0, ARM_SMMU_sCR0_CLIENTPD);
2232 arm_smmu_rpm_put(smmu);
2234 if (pm_runtime_enabled(smmu->dev))
2235 pm_runtime_force_suspend(smmu->dev);
2237 clk_bulk_disable(smmu->num_clks, smmu->clks);
2239 clk_bulk_unprepare(smmu->num_clks, smmu->clks);
2243 static void arm_smmu_device_shutdown(struct platform_device *pdev)
2245 arm_smmu_device_remove(pdev);
2248 static int __maybe_unused arm_smmu_runtime_resume(struct device *dev)
2250 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2253 ret = clk_bulk_enable(smmu->num_clks, smmu->clks);
2257 arm_smmu_device_reset(smmu);
2262 static int __maybe_unused arm_smmu_runtime_suspend(struct device *dev)
2264 struct arm_smmu_device *smmu = dev_get_drvdata(dev);
2266 clk_bulk_disable(smmu->num_clks, smmu->clks);
2271 static int __maybe_unused arm_smmu_pm_resume(struct device *dev)
2273 if (pm_runtime_suspended(dev))
2276 return arm_smmu_runtime_resume(dev);
2279 static int __maybe_unused arm_smmu_pm_suspend(struct device *dev)
2281 if (pm_runtime_suspended(dev))
2284 return arm_smmu_runtime_suspend(dev);
2287 static const struct dev_pm_ops arm_smmu_pm_ops = {
2288 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend, arm_smmu_pm_resume)
2289 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend,
2290 arm_smmu_runtime_resume, NULL)
2293 static struct platform_driver arm_smmu_driver = {
2296 .of_match_table = arm_smmu_of_match,
2297 .pm = &arm_smmu_pm_ops,
2298 .suppress_bind_attrs = true,
2300 .probe = arm_smmu_device_probe,
2301 .remove = arm_smmu_device_remove,
2302 .shutdown = arm_smmu_device_shutdown,
2304 module_platform_driver(arm_smmu_driver);
2306 MODULE_DESCRIPTION("IOMMU API for ARM architected SMMU implementations");
2307 MODULE_AUTHOR("Will Deacon <will@kernel.org>");
2308 MODULE_ALIAS("platform:arm-smmu");
2309 MODULE_LICENSE("GPL v2");