2 * IOMMU API for ARM architected SMMU implementations.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * Copyright (C) 2013 ARM Limited
19 * Author: Will Deacon <will.deacon@arm.com>
21 * This driver currently supports:
22 * - SMMUv1 and v2 implementations
23 * - Stream-matching and stream-indexing
24 * - v7/v8 long-descriptor format
25 * - Non-secure access to the SMMU
26 * - Context fault reporting
27 * - Extended Stream ID (16 bit)
30 #define pr_fmt(fmt) "arm-smmu: " fmt
32 #include <linux/acpi.h>
33 #include <linux/acpi_iort.h>
34 #include <linux/atomic.h>
35 #include <linux/delay.h>
36 #include <linux/dma-iommu.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/err.h>
39 #include <linux/interrupt.h>
41 #include <linux/io-64-nonatomic-hi-lo.h>
42 #include <linux/io-pgtable.h>
43 #include <linux/iommu.h>
44 #include <linux/iopoll.h>
45 #include <linux/init.h>
46 #include <linux/moduleparam.h>
48 #include <linux/of_address.h>
49 #include <linux/of_device.h>
50 #include <linux/of_iommu.h>
51 #include <linux/pci.h>
52 #include <linux/platform_device.h>
53 #include <linux/pm_runtime.h>
54 #include <linux/slab.h>
55 #include <linux/spinlock.h>
57 #include <linux/amba/bus.h>
58 #include <linux/fsl/mc.h>
60 #include "arm-smmu-regs.h"
63 * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU
64 * global register space are still, in fact, using a hypervisor to mediate it
65 * by trapping and emulating register accesses. Sadly, some deployed versions
66 * of said trapping code have bugs wherein they go horribly wrong for stores
67 * using r31 (i.e. XZR/WZR) as the source register.
69 #define QCOM_DUMMY_VAL -1
71 #define ARM_MMU500_ACTLR_CPRE (1 << 1)
73 #define ARM_MMU500_ACR_CACHE_LOCK (1 << 26)
74 #define ARM_MMU500_ACR_S2CRB_TLBEN (1 << 10)
75 #define ARM_MMU500_ACR_SMTNMB_TLBEN (1 << 8)
77 #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
78 #define TLB_SPIN_COUNT 10
80 /* Maximum number of context banks per SMMU */
81 #define ARM_SMMU_MAX_CBS 128
83 /* SMMU global address space */
84 #define ARM_SMMU_GR0(smmu) ((smmu)->base)
85 #define ARM_SMMU_GR1(smmu) ((smmu)->base + (1 << (smmu)->pgshift))
88 * SMMU global address space with conditional offset to access secure
89 * aliases of non-secure registers (e.g. nsCR0: 0x400, nsGFSR: 0x448,
92 #define ARM_SMMU_GR0_NS(smmu) \
94 ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \
98 * Some 64-bit registers only make sense to write atomically, but in such
99 * cases all the data relevant to AArch32 formats lies within the lower word,
100 * therefore this actually makes more sense than it might first appear.
103 #define smmu_write_atomic_lq writeq_relaxed
105 #define smmu_write_atomic_lq writel_relaxed
108 /* Translation context bank */
109 #define ARM_SMMU_CB(smmu, n) ((smmu)->cb_base + ((n) << (smmu)->pgshift))
111 #define MSI_IOVA_BASE 0x8000000
112 #define MSI_IOVA_LENGTH 0x100000
114 static int force_stage
;
116 * not really modular, but the easiest way to keep compat with existing
117 * bootargs behaviour is to continue using module_param() here.
119 module_param(force_stage
, int, S_IRUGO
);
120 MODULE_PARM_DESC(force_stage
,
121 "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.");
122 static bool disable_bypass
;
123 module_param(disable_bypass
, bool, S_IRUGO
);
124 MODULE_PARM_DESC(disable_bypass
,
125 "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.");
127 enum arm_smmu_arch_version
{
133 enum arm_smmu_implementation
{
140 struct arm_smmu_s2cr
{
141 struct iommu_group
*group
;
143 enum arm_smmu_s2cr_type type
;
144 enum arm_smmu_s2cr_privcfg privcfg
;
148 #define s2cr_init_val (struct arm_smmu_s2cr){ \
149 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
152 struct arm_smmu_smr
{
162 struct arm_smmu_cfg
*cfg
;
165 struct arm_smmu_master_cfg
{
166 struct arm_smmu_device
*smmu
;
169 #define INVALID_SMENDX -1
170 #define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
171 #define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
172 #define fwspec_smendx(fw, i) \
173 (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
174 #define for_each_cfg_sme(fw, i, idx) \
175 for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
177 struct arm_smmu_device
{
181 void __iomem
*cb_base
;
182 unsigned long pgshift
;
184 #define ARM_SMMU_FEAT_COHERENT_WALK (1 << 0)
185 #define ARM_SMMU_FEAT_STREAM_MATCH (1 << 1)
186 #define ARM_SMMU_FEAT_TRANS_S1 (1 << 2)
187 #define ARM_SMMU_FEAT_TRANS_S2 (1 << 3)
188 #define ARM_SMMU_FEAT_TRANS_NESTED (1 << 4)
189 #define ARM_SMMU_FEAT_TRANS_OPS (1 << 5)
190 #define ARM_SMMU_FEAT_VMID16 (1 << 6)
191 #define ARM_SMMU_FEAT_FMT_AARCH64_4K (1 << 7)
192 #define ARM_SMMU_FEAT_FMT_AARCH64_16K (1 << 8)
193 #define ARM_SMMU_FEAT_FMT_AARCH64_64K (1 << 9)
194 #define ARM_SMMU_FEAT_FMT_AARCH32_L (1 << 10)
195 #define ARM_SMMU_FEAT_FMT_AARCH32_S (1 << 11)
196 #define ARM_SMMU_FEAT_EXIDS (1 << 12)
199 #define ARM_SMMU_OPT_SECURE_CFG_ACCESS (1 << 0)
201 enum arm_smmu_arch_version version
;
202 enum arm_smmu_implementation model
;
204 u32 num_context_banks
;
205 u32 num_s2_context_banks
;
206 DECLARE_BITMAP(context_map
, ARM_SMMU_MAX_CBS
);
207 struct arm_smmu_cb
*cbs
;
210 u32 num_mapping_groups
;
213 struct arm_smmu_smr
*smrs
;
214 struct arm_smmu_s2cr
*s2crs
;
215 struct mutex stream_map_mutex
;
217 unsigned long va_size
;
218 unsigned long ipa_size
;
219 unsigned long pa_size
;
220 unsigned long pgsize_bitmap
;
223 u32 num_context_irqs
;
225 struct clk_bulk_data
*clks
;
228 u32 cavium_id_base
; /* Specific to Cavium */
230 spinlock_t global_sync_lock
;
232 /* IOMMU core code handle */
233 struct iommu_device iommu
;
236 enum arm_smmu_context_fmt
{
237 ARM_SMMU_CTX_FMT_NONE
,
238 ARM_SMMU_CTX_FMT_AARCH64
,
239 ARM_SMMU_CTX_FMT_AARCH32_L
,
240 ARM_SMMU_CTX_FMT_AARCH32_S
,
243 struct arm_smmu_cfg
{
251 enum arm_smmu_context_fmt fmt
;
253 #define INVALID_IRPTNDX 0xff
255 enum arm_smmu_domain_stage
{
256 ARM_SMMU_DOMAIN_S1
= 0,
258 ARM_SMMU_DOMAIN_NESTED
,
259 ARM_SMMU_DOMAIN_BYPASS
,
262 struct arm_smmu_domain
{
263 struct arm_smmu_device
*smmu
;
264 struct io_pgtable_ops
*pgtbl_ops
;
265 const struct iommu_gather_ops
*tlb_ops
;
266 struct arm_smmu_cfg cfg
;
267 enum arm_smmu_domain_stage stage
;
269 struct mutex init_mutex
; /* Protects smmu pointer */
270 spinlock_t cb_lock
; /* Serialises ATS1* ops and TLB syncs */
271 struct iommu_domain domain
;
274 struct arm_smmu_option_prop
{
279 static atomic_t cavium_smmu_context_count
= ATOMIC_INIT(0);
281 static bool using_legacy_binding
, using_generic_binding
;
283 static struct arm_smmu_option_prop arm_smmu_options
[] = {
284 { ARM_SMMU_OPT_SECURE_CFG_ACCESS
, "calxeda,smmu-secure-config-access" },
288 static inline int arm_smmu_rpm_get(struct arm_smmu_device
*smmu
)
290 if (pm_runtime_enabled(smmu
->dev
))
291 return pm_runtime_get_sync(smmu
->dev
);
296 static inline void arm_smmu_rpm_put(struct arm_smmu_device
*smmu
)
298 if (pm_runtime_enabled(smmu
->dev
))
299 pm_runtime_put(smmu
->dev
);
302 static struct arm_smmu_domain
*to_smmu_domain(struct iommu_domain
*dom
)
304 return container_of(dom
, struct arm_smmu_domain
, domain
);
307 static void parse_driver_options(struct arm_smmu_device
*smmu
)
312 if (of_property_read_bool(smmu
->dev
->of_node
,
313 arm_smmu_options
[i
].prop
)) {
314 smmu
->options
|= arm_smmu_options
[i
].opt
;
315 dev_notice(smmu
->dev
, "option %s\n",
316 arm_smmu_options
[i
].prop
);
318 } while (arm_smmu_options
[++i
].opt
);
321 static struct device_node
*dev_get_dev_node(struct device
*dev
)
323 if (dev_is_pci(dev
)) {
324 struct pci_bus
*bus
= to_pci_dev(dev
)->bus
;
326 while (!pci_is_root_bus(bus
))
328 return of_node_get(bus
->bridge
->parent
->of_node
);
331 return of_node_get(dev
->of_node
);
334 static int __arm_smmu_get_pci_sid(struct pci_dev
*pdev
, u16 alias
, void *data
)
336 *((__be32
*)data
) = cpu_to_be32(alias
);
337 return 0; /* Continue walking */
340 static int __find_legacy_master_phandle(struct device
*dev
, void *data
)
342 struct of_phandle_iterator
*it
= *(void **)data
;
343 struct device_node
*np
= it
->node
;
346 of_for_each_phandle(it
, err
, dev
->of_node
, "mmu-masters",
347 "#stream-id-cells", 0)
348 if (it
->node
== np
) {
349 *(void **)data
= dev
;
353 return err
== -ENOENT
? 0 : err
;
356 static struct platform_driver arm_smmu_driver
;
357 static struct iommu_ops arm_smmu_ops
;
359 static int arm_smmu_register_legacy_master(struct device
*dev
,
360 struct arm_smmu_device
**smmu
)
362 struct device
*smmu_dev
;
363 struct device_node
*np
;
364 struct of_phandle_iterator it
;
370 np
= dev_get_dev_node(dev
);
371 if (!np
|| !of_find_property(np
, "#stream-id-cells", NULL
)) {
377 err
= driver_for_each_device(&arm_smmu_driver
.driver
, NULL
, &data
,
378 __find_legacy_master_phandle
);
386 if (dev_is_pci(dev
)) {
387 /* "mmu-masters" assumes Stream ID == Requester ID */
388 pci_for_each_dma_alias(to_pci_dev(dev
), __arm_smmu_get_pci_sid
,
394 err
= iommu_fwspec_init(dev
, &smmu_dev
->of_node
->fwnode
,
399 sids
= kcalloc(it
.cur_count
, sizeof(*sids
), GFP_KERNEL
);
403 *smmu
= dev_get_drvdata(smmu_dev
);
404 of_phandle_iterator_args(&it
, sids
, it
.cur_count
);
405 err
= iommu_fwspec_add_ids(dev
, sids
, it
.cur_count
);
410 static int __arm_smmu_alloc_bitmap(unsigned long *map
, int start
, int end
)
415 idx
= find_next_zero_bit(map
, end
, start
);
418 } while (test_and_set_bit(idx
, map
));
423 static void __arm_smmu_free_bitmap(unsigned long *map
, int idx
)
428 /* Wait for any pending TLB invalidations to complete */
429 static void __arm_smmu_tlb_sync(struct arm_smmu_device
*smmu
,
430 void __iomem
*sync
, void __iomem
*status
)
432 unsigned int spin_cnt
, delay
;
434 writel_relaxed(QCOM_DUMMY_VAL
, sync
);
435 for (delay
= 1; delay
< TLB_LOOP_TIMEOUT
; delay
*= 2) {
436 for (spin_cnt
= TLB_SPIN_COUNT
; spin_cnt
> 0; spin_cnt
--) {
437 if (!(readl_relaxed(status
) & sTLBGSTATUS_GSACTIVE
))
443 dev_err_ratelimited(smmu
->dev
,
444 "TLB sync timed out -- SMMU may be deadlocked\n");
447 static void arm_smmu_tlb_sync_global(struct arm_smmu_device
*smmu
)
449 void __iomem
*base
= ARM_SMMU_GR0(smmu
);
452 spin_lock_irqsave(&smmu
->global_sync_lock
, flags
);
453 __arm_smmu_tlb_sync(smmu
, base
+ ARM_SMMU_GR0_sTLBGSYNC
,
454 base
+ ARM_SMMU_GR0_sTLBGSTATUS
);
455 spin_unlock_irqrestore(&smmu
->global_sync_lock
, flags
);
458 static void arm_smmu_tlb_sync_context(void *cookie
)
460 struct arm_smmu_domain
*smmu_domain
= cookie
;
461 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
462 void __iomem
*base
= ARM_SMMU_CB(smmu
, smmu_domain
->cfg
.cbndx
);
465 spin_lock_irqsave(&smmu_domain
->cb_lock
, flags
);
466 __arm_smmu_tlb_sync(smmu
, base
+ ARM_SMMU_CB_TLBSYNC
,
467 base
+ ARM_SMMU_CB_TLBSTATUS
);
468 spin_unlock_irqrestore(&smmu_domain
->cb_lock
, flags
);
471 static void arm_smmu_tlb_sync_vmid(void *cookie
)
473 struct arm_smmu_domain
*smmu_domain
= cookie
;
475 arm_smmu_tlb_sync_global(smmu_domain
->smmu
);
478 static void arm_smmu_tlb_inv_context_s1(void *cookie
)
480 struct arm_smmu_domain
*smmu_domain
= cookie
;
481 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
482 void __iomem
*base
= ARM_SMMU_CB(smmu_domain
->smmu
, cfg
->cbndx
);
485 * NOTE: this is not a relaxed write; it needs to guarantee that PTEs
486 * cleared by the current CPU are visible to the SMMU before the TLBI.
488 writel(cfg
->asid
, base
+ ARM_SMMU_CB_S1_TLBIASID
);
489 arm_smmu_tlb_sync_context(cookie
);
492 static void arm_smmu_tlb_inv_context_s2(void *cookie
)
494 struct arm_smmu_domain
*smmu_domain
= cookie
;
495 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
496 void __iomem
*base
= ARM_SMMU_GR0(smmu
);
498 /* NOTE: see above */
499 writel(smmu_domain
->cfg
.vmid
, base
+ ARM_SMMU_GR0_TLBIVMID
);
500 arm_smmu_tlb_sync_global(smmu
);
503 static void arm_smmu_tlb_inv_range_nosync(unsigned long iova
, size_t size
,
504 size_t granule
, bool leaf
, void *cookie
)
506 struct arm_smmu_domain
*smmu_domain
= cookie
;
507 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
508 bool stage1
= cfg
->cbar
!= CBAR_TYPE_S2_TRANS
;
509 void __iomem
*reg
= ARM_SMMU_CB(smmu_domain
->smmu
, cfg
->cbndx
);
511 if (smmu_domain
->smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
)
515 reg
+= leaf
? ARM_SMMU_CB_S1_TLBIVAL
: ARM_SMMU_CB_S1_TLBIVA
;
517 if (cfg
->fmt
!= ARM_SMMU_CTX_FMT_AARCH64
) {
521 writel_relaxed(iova
, reg
);
523 } while (size
-= granule
);
526 iova
|= (u64
)cfg
->asid
<< 48;
528 writeq_relaxed(iova
, reg
);
529 iova
+= granule
>> 12;
530 } while (size
-= granule
);
533 reg
+= leaf
? ARM_SMMU_CB_S2_TLBIIPAS2L
:
534 ARM_SMMU_CB_S2_TLBIIPAS2
;
537 smmu_write_atomic_lq(iova
, reg
);
538 iova
+= granule
>> 12;
539 } while (size
-= granule
);
544 * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
545 * almost negligible, but the benefit of getting the first one in as far ahead
546 * of the sync as possible is significant, hence we don't just make this a
547 * no-op and set .tlb_sync to arm_smmu_inv_context_s2() as you might think.
549 static void arm_smmu_tlb_inv_vmid_nosync(unsigned long iova
, size_t size
,
550 size_t granule
, bool leaf
, void *cookie
)
552 struct arm_smmu_domain
*smmu_domain
= cookie
;
553 void __iomem
*base
= ARM_SMMU_GR0(smmu_domain
->smmu
);
555 if (smmu_domain
->smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
)
558 writel_relaxed(smmu_domain
->cfg
.vmid
, base
+ ARM_SMMU_GR0_TLBIVMID
);
561 static const struct iommu_gather_ops arm_smmu_s1_tlb_ops
= {
562 .tlb_flush_all
= arm_smmu_tlb_inv_context_s1
,
563 .tlb_add_flush
= arm_smmu_tlb_inv_range_nosync
,
564 .tlb_sync
= arm_smmu_tlb_sync_context
,
567 static const struct iommu_gather_ops arm_smmu_s2_tlb_ops_v2
= {
568 .tlb_flush_all
= arm_smmu_tlb_inv_context_s2
,
569 .tlb_add_flush
= arm_smmu_tlb_inv_range_nosync
,
570 .tlb_sync
= arm_smmu_tlb_sync_context
,
573 static const struct iommu_gather_ops arm_smmu_s2_tlb_ops_v1
= {
574 .tlb_flush_all
= arm_smmu_tlb_inv_context_s2
,
575 .tlb_add_flush
= arm_smmu_tlb_inv_vmid_nosync
,
576 .tlb_sync
= arm_smmu_tlb_sync_vmid
,
579 static irqreturn_t
arm_smmu_context_fault(int irq
, void *dev
)
583 struct iommu_domain
*domain
= dev
;
584 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
585 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
586 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
587 void __iomem
*cb_base
;
589 cb_base
= ARM_SMMU_CB(smmu
, cfg
->cbndx
);
590 fsr
= readl_relaxed(cb_base
+ ARM_SMMU_CB_FSR
);
592 if (!(fsr
& FSR_FAULT
))
595 fsynr
= readl_relaxed(cb_base
+ ARM_SMMU_CB_FSYNR0
);
596 iova
= readq_relaxed(cb_base
+ ARM_SMMU_CB_FAR
);
598 dev_err_ratelimited(smmu
->dev
,
599 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cb=%d\n",
600 fsr
, iova
, fsynr
, cfg
->cbndx
);
602 writel(fsr
, cb_base
+ ARM_SMMU_CB_FSR
);
606 static irqreturn_t
arm_smmu_global_fault(int irq
, void *dev
)
608 u32 gfsr
, gfsynr0
, gfsynr1
, gfsynr2
;
609 struct arm_smmu_device
*smmu
= dev
;
610 void __iomem
*gr0_base
= ARM_SMMU_GR0_NS(smmu
);
612 gfsr
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_sGFSR
);
613 gfsynr0
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_sGFSYNR0
);
614 gfsynr1
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_sGFSYNR1
);
615 gfsynr2
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_sGFSYNR2
);
620 dev_err_ratelimited(smmu
->dev
,
621 "Unexpected global fault, this could be serious\n");
622 dev_err_ratelimited(smmu
->dev
,
623 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
624 gfsr
, gfsynr0
, gfsynr1
, gfsynr2
);
626 writel(gfsr
, gr0_base
+ ARM_SMMU_GR0_sGFSR
);
630 static void arm_smmu_init_context_bank(struct arm_smmu_domain
*smmu_domain
,
631 struct io_pgtable_cfg
*pgtbl_cfg
)
633 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
634 struct arm_smmu_cb
*cb
= &smmu_domain
->smmu
->cbs
[cfg
->cbndx
];
635 bool stage1
= cfg
->cbar
!= CBAR_TYPE_S2_TRANS
;
641 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
642 cb
->tcr
[0] = pgtbl_cfg
->arm_v7s_cfg
.tcr
;
644 cb
->tcr
[0] = pgtbl_cfg
->arm_lpae_s1_cfg
.tcr
;
645 cb
->tcr
[1] = pgtbl_cfg
->arm_lpae_s1_cfg
.tcr
>> 32;
646 cb
->tcr
[1] |= TTBCR2_SEP_UPSTREAM
;
647 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
)
648 cb
->tcr
[1] |= TTBCR2_AS
;
651 cb
->tcr
[0] = pgtbl_cfg
->arm_lpae_s2_cfg
.vtcr
;
656 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
657 cb
->ttbr
[0] = pgtbl_cfg
->arm_v7s_cfg
.ttbr
[0];
658 cb
->ttbr
[1] = pgtbl_cfg
->arm_v7s_cfg
.ttbr
[1];
660 cb
->ttbr
[0] = pgtbl_cfg
->arm_lpae_s1_cfg
.ttbr
[0];
661 cb
->ttbr
[0] |= (u64
)cfg
->asid
<< TTBRn_ASID_SHIFT
;
662 cb
->ttbr
[1] = pgtbl_cfg
->arm_lpae_s1_cfg
.ttbr
[1];
663 cb
->ttbr
[1] |= (u64
)cfg
->asid
<< TTBRn_ASID_SHIFT
;
666 cb
->ttbr
[0] = pgtbl_cfg
->arm_lpae_s2_cfg
.vttbr
;
669 /* MAIRs (stage-1 only) */
671 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
672 cb
->mair
[0] = pgtbl_cfg
->arm_v7s_cfg
.prrr
;
673 cb
->mair
[1] = pgtbl_cfg
->arm_v7s_cfg
.nmrr
;
675 cb
->mair
[0] = pgtbl_cfg
->arm_lpae_s1_cfg
.mair
[0];
676 cb
->mair
[1] = pgtbl_cfg
->arm_lpae_s1_cfg
.mair
[1];
681 static void arm_smmu_write_context_bank(struct arm_smmu_device
*smmu
, int idx
)
685 struct arm_smmu_cb
*cb
= &smmu
->cbs
[idx
];
686 struct arm_smmu_cfg
*cfg
= cb
->cfg
;
687 void __iomem
*cb_base
, *gr1_base
;
689 cb_base
= ARM_SMMU_CB(smmu
, idx
);
691 /* Unassigned context banks only need disabling */
693 writel_relaxed(0, cb_base
+ ARM_SMMU_CB_SCTLR
);
697 gr1_base
= ARM_SMMU_GR1(smmu
);
698 stage1
= cfg
->cbar
!= CBAR_TYPE_S2_TRANS
;
701 if (smmu
->version
> ARM_SMMU_V1
) {
702 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
)
703 reg
= CBA2R_RW64_64BIT
;
705 reg
= CBA2R_RW64_32BIT
;
706 /* 16-bit VMIDs live in CBA2R */
707 if (smmu
->features
& ARM_SMMU_FEAT_VMID16
)
708 reg
|= cfg
->vmid
<< CBA2R_VMID_SHIFT
;
710 writel_relaxed(reg
, gr1_base
+ ARM_SMMU_GR1_CBA2R(idx
));
715 if (smmu
->version
< ARM_SMMU_V2
)
716 reg
|= cfg
->irptndx
<< CBAR_IRPTNDX_SHIFT
;
719 * Use the weakest shareability/memory types, so they are
720 * overridden by the ttbcr/pte.
723 reg
|= (CBAR_S1_BPSHCFG_NSH
<< CBAR_S1_BPSHCFG_SHIFT
) |
724 (CBAR_S1_MEMATTR_WB
<< CBAR_S1_MEMATTR_SHIFT
);
725 } else if (!(smmu
->features
& ARM_SMMU_FEAT_VMID16
)) {
726 /* 8-bit VMIDs live in CBAR */
727 reg
|= cfg
->vmid
<< CBAR_VMID_SHIFT
;
729 writel_relaxed(reg
, gr1_base
+ ARM_SMMU_GR1_CBAR(idx
));
733 * We must write this before the TTBRs, since it determines the
734 * access behaviour of some fields (in particular, ASID[15:8]).
736 if (stage1
&& smmu
->version
> ARM_SMMU_V1
)
737 writel_relaxed(cb
->tcr
[1], cb_base
+ ARM_SMMU_CB_TTBCR2
);
738 writel_relaxed(cb
->tcr
[0], cb_base
+ ARM_SMMU_CB_TTBCR
);
741 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
742 writel_relaxed(cfg
->asid
, cb_base
+ ARM_SMMU_CB_CONTEXTIDR
);
743 writel_relaxed(cb
->ttbr
[0], cb_base
+ ARM_SMMU_CB_TTBR0
);
744 writel_relaxed(cb
->ttbr
[1], cb_base
+ ARM_SMMU_CB_TTBR1
);
746 writeq_relaxed(cb
->ttbr
[0], cb_base
+ ARM_SMMU_CB_TTBR0
);
748 writeq_relaxed(cb
->ttbr
[1], cb_base
+ ARM_SMMU_CB_TTBR1
);
751 /* MAIRs (stage-1 only) */
753 writel_relaxed(cb
->mair
[0], cb_base
+ ARM_SMMU_CB_S1_MAIR0
);
754 writel_relaxed(cb
->mair
[1], cb_base
+ ARM_SMMU_CB_S1_MAIR1
);
758 reg
= SCTLR_CFIE
| SCTLR_CFRE
| SCTLR_AFE
| SCTLR_TRE
| SCTLR_M
;
760 reg
|= SCTLR_S1_ASIDPNE
;
761 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
764 writel_relaxed(reg
, cb_base
+ ARM_SMMU_CB_SCTLR
);
767 static int arm_smmu_init_domain_context(struct iommu_domain
*domain
,
768 struct arm_smmu_device
*smmu
)
770 int irq
, start
, ret
= 0;
771 unsigned long ias
, oas
;
772 struct io_pgtable_ops
*pgtbl_ops
;
773 struct io_pgtable_cfg pgtbl_cfg
;
774 enum io_pgtable_fmt fmt
;
775 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
776 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
778 mutex_lock(&smmu_domain
->init_mutex
);
779 if (smmu_domain
->smmu
)
782 if (domain
->type
== IOMMU_DOMAIN_IDENTITY
) {
783 smmu_domain
->stage
= ARM_SMMU_DOMAIN_BYPASS
;
784 smmu_domain
->smmu
= smmu
;
789 * Mapping the requested stage onto what we support is surprisingly
790 * complicated, mainly because the spec allows S1+S2 SMMUs without
791 * support for nested translation. That means we end up with the
794 * Requested Supported Actual
804 * Note that you can't actually request stage-2 mappings.
806 if (!(smmu
->features
& ARM_SMMU_FEAT_TRANS_S1
))
807 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S2
;
808 if (!(smmu
->features
& ARM_SMMU_FEAT_TRANS_S2
))
809 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S1
;
812 * Choosing a suitable context format is even more fiddly. Until we
813 * grow some way for the caller to express a preference, and/or move
814 * the decision into the io-pgtable code where it arguably belongs,
815 * just aim for the closest thing to the rest of the system, and hope
816 * that the hardware isn't esoteric enough that we can't assume AArch64
817 * support to be a superset of AArch32 support...
819 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH32_L
)
820 cfg
->fmt
= ARM_SMMU_CTX_FMT_AARCH32_L
;
821 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S
) &&
822 !IS_ENABLED(CONFIG_64BIT
) && !IS_ENABLED(CONFIG_ARM_LPAE
) &&
823 (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH32_S
) &&
824 (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
))
825 cfg
->fmt
= ARM_SMMU_CTX_FMT_AARCH32_S
;
826 if ((IS_ENABLED(CONFIG_64BIT
) || cfg
->fmt
== ARM_SMMU_CTX_FMT_NONE
) &&
827 (smmu
->features
& (ARM_SMMU_FEAT_FMT_AARCH64_64K
|
828 ARM_SMMU_FEAT_FMT_AARCH64_16K
|
829 ARM_SMMU_FEAT_FMT_AARCH64_4K
)))
830 cfg
->fmt
= ARM_SMMU_CTX_FMT_AARCH64
;
832 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_NONE
) {
837 switch (smmu_domain
->stage
) {
838 case ARM_SMMU_DOMAIN_S1
:
839 cfg
->cbar
= CBAR_TYPE_S1_TRANS_S2_BYPASS
;
840 start
= smmu
->num_s2_context_banks
;
842 oas
= smmu
->ipa_size
;
843 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
) {
844 fmt
= ARM_64_LPAE_S1
;
845 } else if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_L
) {
846 fmt
= ARM_32_LPAE_S1
;
847 ias
= min(ias
, 32UL);
848 oas
= min(oas
, 40UL);
851 ias
= min(ias
, 32UL);
852 oas
= min(oas
, 32UL);
854 smmu_domain
->tlb_ops
= &arm_smmu_s1_tlb_ops
;
856 case ARM_SMMU_DOMAIN_NESTED
:
858 * We will likely want to change this if/when KVM gets
861 case ARM_SMMU_DOMAIN_S2
:
862 cfg
->cbar
= CBAR_TYPE_S2_TRANS
;
864 ias
= smmu
->ipa_size
;
866 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
) {
867 fmt
= ARM_64_LPAE_S2
;
869 fmt
= ARM_32_LPAE_S2
;
870 ias
= min(ias
, 40UL);
871 oas
= min(oas
, 40UL);
873 if (smmu
->version
== ARM_SMMU_V2
)
874 smmu_domain
->tlb_ops
= &arm_smmu_s2_tlb_ops_v2
;
876 smmu_domain
->tlb_ops
= &arm_smmu_s2_tlb_ops_v1
;
882 ret
= __arm_smmu_alloc_bitmap(smmu
->context_map
, start
,
883 smmu
->num_context_banks
);
888 if (smmu
->version
< ARM_SMMU_V2
) {
889 cfg
->irptndx
= atomic_inc_return(&smmu
->irptndx
);
890 cfg
->irptndx
%= smmu
->num_context_irqs
;
892 cfg
->irptndx
= cfg
->cbndx
;
895 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S2
)
896 cfg
->vmid
= cfg
->cbndx
+ 1 + smmu
->cavium_id_base
;
898 cfg
->asid
= cfg
->cbndx
+ smmu
->cavium_id_base
;
900 pgtbl_cfg
= (struct io_pgtable_cfg
) {
901 .pgsize_bitmap
= smmu
->pgsize_bitmap
,
904 .tlb
= smmu_domain
->tlb_ops
,
905 .iommu_dev
= smmu
->dev
,
908 if (smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
)
909 pgtbl_cfg
.quirks
= IO_PGTABLE_QUIRK_NO_DMA
;
911 if (smmu_domain
->non_strict
)
912 pgtbl_cfg
.quirks
|= IO_PGTABLE_QUIRK_NON_STRICT
;
914 smmu_domain
->smmu
= smmu
;
915 pgtbl_ops
= alloc_io_pgtable_ops(fmt
, &pgtbl_cfg
, smmu_domain
);
921 /* Update the domain's page sizes to reflect the page table format */
922 domain
->pgsize_bitmap
= pgtbl_cfg
.pgsize_bitmap
;
923 domain
->geometry
.aperture_end
= (1UL << ias
) - 1;
924 domain
->geometry
.force_aperture
= true;
926 /* Initialise the context bank with our page table cfg */
927 arm_smmu_init_context_bank(smmu_domain
, &pgtbl_cfg
);
928 arm_smmu_write_context_bank(smmu
, cfg
->cbndx
);
931 * Request context fault interrupt. Do this last to avoid the
932 * handler seeing a half-initialised domain state.
934 irq
= smmu
->irqs
[smmu
->num_global_irqs
+ cfg
->irptndx
];
935 ret
= devm_request_irq(smmu
->dev
, irq
, arm_smmu_context_fault
,
936 IRQF_SHARED
, "arm-smmu-context-fault", domain
);
938 dev_err(smmu
->dev
, "failed to request context IRQ %d (%u)\n",
940 cfg
->irptndx
= INVALID_IRPTNDX
;
943 mutex_unlock(&smmu_domain
->init_mutex
);
945 /* Publish page table ops for map/unmap */
946 smmu_domain
->pgtbl_ops
= pgtbl_ops
;
950 smmu_domain
->smmu
= NULL
;
952 mutex_unlock(&smmu_domain
->init_mutex
);
956 static void arm_smmu_destroy_domain_context(struct iommu_domain
*domain
)
958 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
959 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
960 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
963 if (!smmu
|| domain
->type
== IOMMU_DOMAIN_IDENTITY
)
966 ret
= arm_smmu_rpm_get(smmu
);
971 * Disable the context bank and free the page tables before freeing
974 smmu
->cbs
[cfg
->cbndx
].cfg
= NULL
;
975 arm_smmu_write_context_bank(smmu
, cfg
->cbndx
);
977 if (cfg
->irptndx
!= INVALID_IRPTNDX
) {
978 irq
= smmu
->irqs
[smmu
->num_global_irqs
+ cfg
->irptndx
];
979 devm_free_irq(smmu
->dev
, irq
, domain
);
982 free_io_pgtable_ops(smmu_domain
->pgtbl_ops
);
983 __arm_smmu_free_bitmap(smmu
->context_map
, cfg
->cbndx
);
985 arm_smmu_rpm_put(smmu
);
988 static struct iommu_domain
*arm_smmu_domain_alloc(unsigned type
)
990 struct arm_smmu_domain
*smmu_domain
;
992 if (type
!= IOMMU_DOMAIN_UNMANAGED
&&
993 type
!= IOMMU_DOMAIN_DMA
&&
994 type
!= IOMMU_DOMAIN_IDENTITY
)
997 * Allocate the domain and initialise some of its data structures.
998 * We can't really do anything meaningful until we've added a
1001 smmu_domain
= kzalloc(sizeof(*smmu_domain
), GFP_KERNEL
);
1005 if (type
== IOMMU_DOMAIN_DMA
&& (using_legacy_binding
||
1006 iommu_get_dma_cookie(&smmu_domain
->domain
))) {
1011 mutex_init(&smmu_domain
->init_mutex
);
1012 spin_lock_init(&smmu_domain
->cb_lock
);
1014 return &smmu_domain
->domain
;
1017 static void arm_smmu_domain_free(struct iommu_domain
*domain
)
1019 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1022 * Free the domain resources. We assume that all devices have
1023 * already been detached.
1025 iommu_put_dma_cookie(domain
);
1026 arm_smmu_destroy_domain_context(domain
);
1030 static void arm_smmu_write_smr(struct arm_smmu_device
*smmu
, int idx
)
1032 struct arm_smmu_smr
*smr
= smmu
->smrs
+ idx
;
1033 u32 reg
= smr
->id
<< SMR_ID_SHIFT
| smr
->mask
<< SMR_MASK_SHIFT
;
1035 if (!(smmu
->features
& ARM_SMMU_FEAT_EXIDS
) && smr
->valid
)
1037 writel_relaxed(reg
, ARM_SMMU_GR0(smmu
) + ARM_SMMU_GR0_SMR(idx
));
1040 static void arm_smmu_write_s2cr(struct arm_smmu_device
*smmu
, int idx
)
1042 struct arm_smmu_s2cr
*s2cr
= smmu
->s2crs
+ idx
;
1043 u32 reg
= (s2cr
->type
& S2CR_TYPE_MASK
) << S2CR_TYPE_SHIFT
|
1044 (s2cr
->cbndx
& S2CR_CBNDX_MASK
) << S2CR_CBNDX_SHIFT
|
1045 (s2cr
->privcfg
& S2CR_PRIVCFG_MASK
) << S2CR_PRIVCFG_SHIFT
;
1047 if (smmu
->features
& ARM_SMMU_FEAT_EXIDS
&& smmu
->smrs
&&
1048 smmu
->smrs
[idx
].valid
)
1049 reg
|= S2CR_EXIDVALID
;
1050 writel_relaxed(reg
, ARM_SMMU_GR0(smmu
) + ARM_SMMU_GR0_S2CR(idx
));
1053 static void arm_smmu_write_sme(struct arm_smmu_device
*smmu
, int idx
)
1055 arm_smmu_write_s2cr(smmu
, idx
);
1057 arm_smmu_write_smr(smmu
, idx
);
1061 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
1062 * should be called after sCR0 is written.
1064 static void arm_smmu_test_smr_masks(struct arm_smmu_device
*smmu
)
1066 void __iomem
*gr0_base
= ARM_SMMU_GR0(smmu
);
1073 * SMR.ID bits may not be preserved if the corresponding MASK
1074 * bits are set, so check each one separately. We can reject
1075 * masters later if they try to claim IDs outside these masks.
1077 smr
= smmu
->streamid_mask
<< SMR_ID_SHIFT
;
1078 writel_relaxed(smr
, gr0_base
+ ARM_SMMU_GR0_SMR(0));
1079 smr
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_SMR(0));
1080 smmu
->streamid_mask
= smr
>> SMR_ID_SHIFT
;
1082 smr
= smmu
->streamid_mask
<< SMR_MASK_SHIFT
;
1083 writel_relaxed(smr
, gr0_base
+ ARM_SMMU_GR0_SMR(0));
1084 smr
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_SMR(0));
1085 smmu
->smr_mask_mask
= smr
>> SMR_MASK_SHIFT
;
1088 static int arm_smmu_find_sme(struct arm_smmu_device
*smmu
, u16 id
, u16 mask
)
1090 struct arm_smmu_smr
*smrs
= smmu
->smrs
;
1091 int i
, free_idx
= -ENOSPC
;
1093 /* Stream indexing is blissfully easy */
1097 /* Validating SMRs is... less so */
1098 for (i
= 0; i
< smmu
->num_mapping_groups
; ++i
) {
1099 if (!smrs
[i
].valid
) {
1101 * Note the first free entry we come across, which
1102 * we'll claim in the end if nothing else matches.
1109 * If the new entry is _entirely_ matched by an existing entry,
1110 * then reuse that, with the guarantee that there also cannot
1111 * be any subsequent conflicting entries. In normal use we'd
1112 * expect simply identical entries for this case, but there's
1113 * no harm in accommodating the generalisation.
1115 if ((mask
& smrs
[i
].mask
) == mask
&&
1116 !((id
^ smrs
[i
].id
) & ~smrs
[i
].mask
))
1119 * If the new entry has any other overlap with an existing one,
1120 * though, then there always exists at least one stream ID
1121 * which would cause a conflict, and we can't allow that risk.
1123 if (!((id
^ smrs
[i
].id
) & ~(smrs
[i
].mask
| mask
)))
1130 static bool arm_smmu_free_sme(struct arm_smmu_device
*smmu
, int idx
)
1132 if (--smmu
->s2crs
[idx
].count
)
1135 smmu
->s2crs
[idx
] = s2cr_init_val
;
1137 smmu
->smrs
[idx
].valid
= false;
1142 static int arm_smmu_master_alloc_smes(struct device
*dev
)
1144 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1145 struct arm_smmu_master_cfg
*cfg
= fwspec
->iommu_priv
;
1146 struct arm_smmu_device
*smmu
= cfg
->smmu
;
1147 struct arm_smmu_smr
*smrs
= smmu
->smrs
;
1148 struct iommu_group
*group
;
1151 mutex_lock(&smmu
->stream_map_mutex
);
1152 /* Figure out a viable stream map entry allocation */
1153 for_each_cfg_sme(fwspec
, i
, idx
) {
1154 u16 sid
= fwspec
->ids
[i
];
1155 u16 mask
= fwspec
->ids
[i
] >> SMR_MASK_SHIFT
;
1157 if (idx
!= INVALID_SMENDX
) {
1162 ret
= arm_smmu_find_sme(smmu
, sid
, mask
);
1167 if (smrs
&& smmu
->s2crs
[idx
].count
== 0) {
1169 smrs
[idx
].mask
= mask
;
1170 smrs
[idx
].valid
= true;
1172 smmu
->s2crs
[idx
].count
++;
1173 cfg
->smendx
[i
] = (s16
)idx
;
1176 group
= iommu_group_get_for_dev(dev
);
1178 group
= ERR_PTR(-ENOMEM
);
1179 if (IS_ERR(group
)) {
1180 ret
= PTR_ERR(group
);
1183 iommu_group_put(group
);
1185 /* It worked! Now, poke the actual hardware */
1186 for_each_cfg_sme(fwspec
, i
, idx
) {
1187 arm_smmu_write_sme(smmu
, idx
);
1188 smmu
->s2crs
[idx
].group
= group
;
1191 mutex_unlock(&smmu
->stream_map_mutex
);
1196 arm_smmu_free_sme(smmu
, cfg
->smendx
[i
]);
1197 cfg
->smendx
[i
] = INVALID_SMENDX
;
1199 mutex_unlock(&smmu
->stream_map_mutex
);
1203 static void arm_smmu_master_free_smes(struct iommu_fwspec
*fwspec
)
1205 struct arm_smmu_device
*smmu
= fwspec_smmu(fwspec
);
1206 struct arm_smmu_master_cfg
*cfg
= fwspec
->iommu_priv
;
1209 mutex_lock(&smmu
->stream_map_mutex
);
1210 for_each_cfg_sme(fwspec
, i
, idx
) {
1211 if (arm_smmu_free_sme(smmu
, idx
))
1212 arm_smmu_write_sme(smmu
, idx
);
1213 cfg
->smendx
[i
] = INVALID_SMENDX
;
1215 mutex_unlock(&smmu
->stream_map_mutex
);
1218 static int arm_smmu_domain_add_master(struct arm_smmu_domain
*smmu_domain
,
1219 struct iommu_fwspec
*fwspec
)
1221 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1222 struct arm_smmu_s2cr
*s2cr
= smmu
->s2crs
;
1223 u8 cbndx
= smmu_domain
->cfg
.cbndx
;
1224 enum arm_smmu_s2cr_type type
;
1227 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_BYPASS
)
1228 type
= S2CR_TYPE_BYPASS
;
1230 type
= S2CR_TYPE_TRANS
;
1232 for_each_cfg_sme(fwspec
, i
, idx
) {
1233 if (type
== s2cr
[idx
].type
&& cbndx
== s2cr
[idx
].cbndx
)
1236 s2cr
[idx
].type
= type
;
1237 s2cr
[idx
].privcfg
= S2CR_PRIVCFG_DEFAULT
;
1238 s2cr
[idx
].cbndx
= cbndx
;
1239 arm_smmu_write_s2cr(smmu
, idx
);
1244 static int arm_smmu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1247 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1248 struct arm_smmu_device
*smmu
;
1249 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1251 if (!fwspec
|| fwspec
->ops
!= &arm_smmu_ops
) {
1252 dev_err(dev
, "cannot attach to SMMU, is it on the same bus?\n");
1257 * FIXME: The arch/arm DMA API code tries to attach devices to its own
1258 * domains between of_xlate() and add_device() - we have no way to cope
1259 * with that, so until ARM gets converted to rely on groups and default
1260 * domains, just say no (but more politely than by dereferencing NULL).
1261 * This should be at least a WARN_ON once that's sorted.
1263 if (!fwspec
->iommu_priv
)
1266 smmu
= fwspec_smmu(fwspec
);
1268 ret
= arm_smmu_rpm_get(smmu
);
1272 /* Ensure that the domain is finalised */
1273 ret
= arm_smmu_init_domain_context(domain
, smmu
);
1278 * Sanity check the domain. We don't support domains across
1281 if (smmu_domain
->smmu
!= smmu
) {
1283 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1284 dev_name(smmu_domain
->smmu
->dev
), dev_name(smmu
->dev
));
1289 /* Looks ok, so add the device to the domain */
1290 ret
= arm_smmu_domain_add_master(smmu_domain
, fwspec
);
1293 arm_smmu_rpm_put(smmu
);
1297 static int arm_smmu_map(struct iommu_domain
*domain
, unsigned long iova
,
1298 phys_addr_t paddr
, size_t size
, int prot
)
1300 struct io_pgtable_ops
*ops
= to_smmu_domain(domain
)->pgtbl_ops
;
1301 struct arm_smmu_device
*smmu
= to_smmu_domain(domain
)->smmu
;
1307 arm_smmu_rpm_get(smmu
);
1308 ret
= ops
->map(ops
, iova
, paddr
, size
, prot
);
1309 arm_smmu_rpm_put(smmu
);
1314 static size_t arm_smmu_unmap(struct iommu_domain
*domain
, unsigned long iova
,
1317 struct io_pgtable_ops
*ops
= to_smmu_domain(domain
)->pgtbl_ops
;
1318 struct arm_smmu_device
*smmu
= to_smmu_domain(domain
)->smmu
;
1324 arm_smmu_rpm_get(smmu
);
1325 ret
= ops
->unmap(ops
, iova
, size
);
1326 arm_smmu_rpm_put(smmu
);
1331 static void arm_smmu_flush_iotlb_all(struct iommu_domain
*domain
)
1333 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1334 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1336 if (smmu_domain
->tlb_ops
) {
1337 arm_smmu_rpm_get(smmu
);
1338 smmu_domain
->tlb_ops
->tlb_flush_all(smmu_domain
);
1339 arm_smmu_rpm_put(smmu
);
1343 static void arm_smmu_iotlb_sync(struct iommu_domain
*domain
)
1345 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1346 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1348 if (smmu_domain
->tlb_ops
) {
1349 arm_smmu_rpm_get(smmu
);
1350 smmu_domain
->tlb_ops
->tlb_sync(smmu_domain
);
1351 arm_smmu_rpm_put(smmu
);
1355 static phys_addr_t
arm_smmu_iova_to_phys_hard(struct iommu_domain
*domain
,
1358 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1359 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1360 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
1361 struct io_pgtable_ops
*ops
= smmu_domain
->pgtbl_ops
;
1362 struct device
*dev
= smmu
->dev
;
1363 void __iomem
*cb_base
;
1366 unsigned long va
, flags
;
1369 ret
= arm_smmu_rpm_get(smmu
);
1373 cb_base
= ARM_SMMU_CB(smmu
, cfg
->cbndx
);
1375 spin_lock_irqsave(&smmu_domain
->cb_lock
, flags
);
1376 /* ATS1 registers can only be written atomically */
1377 va
= iova
& ~0xfffUL
;
1378 if (smmu
->version
== ARM_SMMU_V2
)
1379 smmu_write_atomic_lq(va
, cb_base
+ ARM_SMMU_CB_ATS1PR
);
1380 else /* Register is only 32-bit in v1 */
1381 writel_relaxed(va
, cb_base
+ ARM_SMMU_CB_ATS1PR
);
1383 if (readl_poll_timeout_atomic(cb_base
+ ARM_SMMU_CB_ATSR
, tmp
,
1384 !(tmp
& ATSR_ACTIVE
), 5, 50)) {
1385 spin_unlock_irqrestore(&smmu_domain
->cb_lock
, flags
);
1387 "iova to phys timed out on %pad. Falling back to software table walk.\n",
1389 return ops
->iova_to_phys(ops
, iova
);
1392 phys
= readq_relaxed(cb_base
+ ARM_SMMU_CB_PAR
);
1393 spin_unlock_irqrestore(&smmu_domain
->cb_lock
, flags
);
1394 if (phys
& CB_PAR_F
) {
1395 dev_err(dev
, "translation fault!\n");
1396 dev_err(dev
, "PAR = 0x%llx\n", phys
);
1400 arm_smmu_rpm_put(smmu
);
1402 return (phys
& GENMASK_ULL(39, 12)) | (iova
& 0xfff);
1405 static phys_addr_t
arm_smmu_iova_to_phys(struct iommu_domain
*domain
,
1408 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1409 struct io_pgtable_ops
*ops
= smmu_domain
->pgtbl_ops
;
1411 if (domain
->type
== IOMMU_DOMAIN_IDENTITY
)
1417 if (smmu_domain
->smmu
->features
& ARM_SMMU_FEAT_TRANS_OPS
&&
1418 smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
)
1419 return arm_smmu_iova_to_phys_hard(domain
, iova
);
1421 return ops
->iova_to_phys(ops
, iova
);
1424 static bool arm_smmu_capable(enum iommu_cap cap
)
1427 case IOMMU_CAP_CACHE_COHERENCY
:
1429 * Return true here as the SMMU can always send out coherent
1433 case IOMMU_CAP_NOEXEC
:
1440 static int arm_smmu_match_node(struct device
*dev
, void *data
)
1442 return dev
->fwnode
== data
;
1446 struct arm_smmu_device
*arm_smmu_get_by_fwnode(struct fwnode_handle
*fwnode
)
1448 struct device
*dev
= driver_find_device(&arm_smmu_driver
.driver
, NULL
,
1449 fwnode
, arm_smmu_match_node
);
1451 return dev
? dev_get_drvdata(dev
) : NULL
;
1454 static int arm_smmu_add_device(struct device
*dev
)
1456 struct arm_smmu_device
*smmu
;
1457 struct arm_smmu_master_cfg
*cfg
;
1458 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1461 if (using_legacy_binding
) {
1462 ret
= arm_smmu_register_legacy_master(dev
, &smmu
);
1465 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1466 * will allocate/initialise a new one. Thus we need to update fwspec for
1469 fwspec
= dev_iommu_fwspec_get(dev
);
1472 } else if (fwspec
&& fwspec
->ops
== &arm_smmu_ops
) {
1473 smmu
= arm_smmu_get_by_fwnode(fwspec
->iommu_fwnode
);
1479 for (i
= 0; i
< fwspec
->num_ids
; i
++) {
1480 u16 sid
= fwspec
->ids
[i
];
1481 u16 mask
= fwspec
->ids
[i
] >> SMR_MASK_SHIFT
;
1483 if (sid
& ~smmu
->streamid_mask
) {
1484 dev_err(dev
, "stream ID 0x%x out of range for SMMU (0x%x)\n",
1485 sid
, smmu
->streamid_mask
);
1488 if (mask
& ~smmu
->smr_mask_mask
) {
1489 dev_err(dev
, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1490 mask
, smmu
->smr_mask_mask
);
1496 cfg
= kzalloc(offsetof(struct arm_smmu_master_cfg
, smendx
[i
]),
1502 fwspec
->iommu_priv
= cfg
;
1504 cfg
->smendx
[i
] = INVALID_SMENDX
;
1506 ret
= arm_smmu_rpm_get(smmu
);
1510 ret
= arm_smmu_master_alloc_smes(dev
);
1511 arm_smmu_rpm_put(smmu
);
1516 iommu_device_link(&smmu
->iommu
, dev
);
1518 device_link_add(dev
, smmu
->dev
,
1519 DL_FLAG_PM_RUNTIME
| DL_FLAG_AUTOREMOVE_SUPPLIER
);
1526 iommu_fwspec_free(dev
);
1530 static void arm_smmu_remove_device(struct device
*dev
)
1532 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1533 struct arm_smmu_master_cfg
*cfg
;
1534 struct arm_smmu_device
*smmu
;
1537 if (!fwspec
|| fwspec
->ops
!= &arm_smmu_ops
)
1540 cfg
= fwspec
->iommu_priv
;
1543 ret
= arm_smmu_rpm_get(smmu
);
1547 iommu_device_unlink(&smmu
->iommu
, dev
);
1548 arm_smmu_master_free_smes(fwspec
);
1550 arm_smmu_rpm_put(smmu
);
1552 iommu_group_remove_device(dev
);
1553 kfree(fwspec
->iommu_priv
);
1554 iommu_fwspec_free(dev
);
1557 static struct iommu_group
*arm_smmu_device_group(struct device
*dev
)
1559 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1560 struct arm_smmu_device
*smmu
= fwspec_smmu(fwspec
);
1561 struct iommu_group
*group
= NULL
;
1564 for_each_cfg_sme(fwspec
, i
, idx
) {
1565 if (group
&& smmu
->s2crs
[idx
].group
&&
1566 group
!= smmu
->s2crs
[idx
].group
)
1567 return ERR_PTR(-EINVAL
);
1569 group
= smmu
->s2crs
[idx
].group
;
1573 return iommu_group_ref_get(group
);
1575 if (dev_is_pci(dev
))
1576 group
= pci_device_group(dev
);
1577 else if (dev_is_fsl_mc(dev
))
1578 group
= fsl_mc_device_group(dev
);
1580 group
= generic_device_group(dev
);
1585 static int arm_smmu_domain_get_attr(struct iommu_domain
*domain
,
1586 enum iommu_attr attr
, void *data
)
1588 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1590 switch(domain
->type
) {
1591 case IOMMU_DOMAIN_UNMANAGED
:
1593 case DOMAIN_ATTR_NESTING
:
1594 *(int *)data
= (smmu_domain
->stage
== ARM_SMMU_DOMAIN_NESTED
);
1600 case IOMMU_DOMAIN_DMA
:
1602 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE
:
1603 *(int *)data
= smmu_domain
->non_strict
;
1614 static int arm_smmu_domain_set_attr(struct iommu_domain
*domain
,
1615 enum iommu_attr attr
, void *data
)
1618 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1620 mutex_lock(&smmu_domain
->init_mutex
);
1622 switch(domain
->type
) {
1623 case IOMMU_DOMAIN_UNMANAGED
:
1625 case DOMAIN_ATTR_NESTING
:
1626 if (smmu_domain
->smmu
) {
1632 smmu_domain
->stage
= ARM_SMMU_DOMAIN_NESTED
;
1634 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S1
;
1640 case IOMMU_DOMAIN_DMA
:
1642 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE
:
1643 smmu_domain
->non_strict
= *(int *)data
;
1653 mutex_unlock(&smmu_domain
->init_mutex
);
1657 static int arm_smmu_of_xlate(struct device
*dev
, struct of_phandle_args
*args
)
1661 if (args
->args_count
> 0)
1662 fwid
|= (u16
)args
->args
[0];
1664 if (args
->args_count
> 1)
1665 fwid
|= (u16
)args
->args
[1] << SMR_MASK_SHIFT
;
1666 else if (!of_property_read_u32(args
->np
, "stream-match-mask", &mask
))
1667 fwid
|= (u16
)mask
<< SMR_MASK_SHIFT
;
1669 return iommu_fwspec_add_ids(dev
, &fwid
, 1);
1672 static void arm_smmu_get_resv_regions(struct device
*dev
,
1673 struct list_head
*head
)
1675 struct iommu_resv_region
*region
;
1676 int prot
= IOMMU_WRITE
| IOMMU_NOEXEC
| IOMMU_MMIO
;
1678 region
= iommu_alloc_resv_region(MSI_IOVA_BASE
, MSI_IOVA_LENGTH
,
1679 prot
, IOMMU_RESV_SW_MSI
);
1683 list_add_tail(®ion
->list
, head
);
1685 iommu_dma_get_resv_regions(dev
, head
);
1688 static void arm_smmu_put_resv_regions(struct device
*dev
,
1689 struct list_head
*head
)
1691 struct iommu_resv_region
*entry
, *next
;
1693 list_for_each_entry_safe(entry
, next
, head
, list
)
1697 static struct iommu_ops arm_smmu_ops
= {
1698 .capable
= arm_smmu_capable
,
1699 .domain_alloc
= arm_smmu_domain_alloc
,
1700 .domain_free
= arm_smmu_domain_free
,
1701 .attach_dev
= arm_smmu_attach_dev
,
1702 .map
= arm_smmu_map
,
1703 .unmap
= arm_smmu_unmap
,
1704 .flush_iotlb_all
= arm_smmu_flush_iotlb_all
,
1705 .iotlb_sync
= arm_smmu_iotlb_sync
,
1706 .iova_to_phys
= arm_smmu_iova_to_phys
,
1707 .add_device
= arm_smmu_add_device
,
1708 .remove_device
= arm_smmu_remove_device
,
1709 .device_group
= arm_smmu_device_group
,
1710 .domain_get_attr
= arm_smmu_domain_get_attr
,
1711 .domain_set_attr
= arm_smmu_domain_set_attr
,
1712 .of_xlate
= arm_smmu_of_xlate
,
1713 .get_resv_regions
= arm_smmu_get_resv_regions
,
1714 .put_resv_regions
= arm_smmu_put_resv_regions
,
1715 .pgsize_bitmap
= -1UL, /* Restricted during device attach */
1718 static void arm_smmu_device_reset(struct arm_smmu_device
*smmu
)
1720 void __iomem
*gr0_base
= ARM_SMMU_GR0(smmu
);
1724 /* clear global FSR */
1725 reg
= readl_relaxed(ARM_SMMU_GR0_NS(smmu
) + ARM_SMMU_GR0_sGFSR
);
1726 writel(reg
, ARM_SMMU_GR0_NS(smmu
) + ARM_SMMU_GR0_sGFSR
);
1729 * Reset stream mapping groups: Initial values mark all SMRn as
1730 * invalid and all S2CRn as bypass unless overridden.
1732 for (i
= 0; i
< smmu
->num_mapping_groups
; ++i
)
1733 arm_smmu_write_sme(smmu
, i
);
1735 if (smmu
->model
== ARM_MMU500
) {
1737 * Before clearing ARM_MMU500_ACTLR_CPRE, need to
1738 * clear CACHE_LOCK bit of ACR first. And, CACHE_LOCK
1739 * bit is only present in MMU-500r2 onwards.
1741 reg
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_ID7
);
1742 major
= (reg
>> ID7_MAJOR_SHIFT
) & ID7_MAJOR_MASK
;
1743 reg
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_sACR
);
1745 reg
&= ~ARM_MMU500_ACR_CACHE_LOCK
;
1747 * Allow unmatched Stream IDs to allocate bypass
1748 * TLB entries for reduced latency.
1750 reg
|= ARM_MMU500_ACR_SMTNMB_TLBEN
| ARM_MMU500_ACR_S2CRB_TLBEN
;
1751 writel_relaxed(reg
, gr0_base
+ ARM_SMMU_GR0_sACR
);
1754 /* Make sure all context banks are disabled and clear CB_FSR */
1755 for (i
= 0; i
< smmu
->num_context_banks
; ++i
) {
1756 void __iomem
*cb_base
= ARM_SMMU_CB(smmu
, i
);
1758 arm_smmu_write_context_bank(smmu
, i
);
1759 writel_relaxed(FSR_FAULT
, cb_base
+ ARM_SMMU_CB_FSR
);
1761 * Disable MMU-500's not-particularly-beneficial next-page
1762 * prefetcher for the sake of errata #841119 and #826419.
1764 if (smmu
->model
== ARM_MMU500
) {
1765 reg
= readl_relaxed(cb_base
+ ARM_SMMU_CB_ACTLR
);
1766 reg
&= ~ARM_MMU500_ACTLR_CPRE
;
1767 writel_relaxed(reg
, cb_base
+ ARM_SMMU_CB_ACTLR
);
1771 /* Invalidate the TLB, just in case */
1772 writel_relaxed(QCOM_DUMMY_VAL
, gr0_base
+ ARM_SMMU_GR0_TLBIALLH
);
1773 writel_relaxed(QCOM_DUMMY_VAL
, gr0_base
+ ARM_SMMU_GR0_TLBIALLNSNH
);
1775 reg
= readl_relaxed(ARM_SMMU_GR0_NS(smmu
) + ARM_SMMU_GR0_sCR0
);
1777 /* Enable fault reporting */
1778 reg
|= (sCR0_GFRE
| sCR0_GFIE
| sCR0_GCFGFRE
| sCR0_GCFGFIE
);
1780 /* Disable TLB broadcasting. */
1781 reg
|= (sCR0_VMIDPNE
| sCR0_PTM
);
1783 /* Enable client access, handling unmatched streams as appropriate */
1784 reg
&= ~sCR0_CLIENTPD
;
1788 reg
&= ~sCR0_USFCFG
;
1790 /* Disable forced broadcasting */
1793 /* Don't upgrade barriers */
1794 reg
&= ~(sCR0_BSU_MASK
<< sCR0_BSU_SHIFT
);
1796 if (smmu
->features
& ARM_SMMU_FEAT_VMID16
)
1797 reg
|= sCR0_VMID16EN
;
1799 if (smmu
->features
& ARM_SMMU_FEAT_EXIDS
)
1800 reg
|= sCR0_EXIDENABLE
;
1802 /* Push the button */
1803 arm_smmu_tlb_sync_global(smmu
);
1804 writel(reg
, ARM_SMMU_GR0_NS(smmu
) + ARM_SMMU_GR0_sCR0
);
1807 static int arm_smmu_id_size_to_bits(int size
)
1826 static int arm_smmu_device_cfg_probe(struct arm_smmu_device
*smmu
)
1829 void __iomem
*gr0_base
= ARM_SMMU_GR0(smmu
);
1831 bool cttw_reg
, cttw_fw
= smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
;
1834 dev_notice(smmu
->dev
, "probing hardware configuration...\n");
1835 dev_notice(smmu
->dev
, "SMMUv%d with:\n",
1836 smmu
->version
== ARM_SMMU_V2
? 2 : 1);
1839 id
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_ID0
);
1841 /* Restrict available stages based on module parameter */
1842 if (force_stage
== 1)
1843 id
&= ~(ID0_S2TS
| ID0_NTS
);
1844 else if (force_stage
== 2)
1845 id
&= ~(ID0_S1TS
| ID0_NTS
);
1847 if (id
& ID0_S1TS
) {
1848 smmu
->features
|= ARM_SMMU_FEAT_TRANS_S1
;
1849 dev_notice(smmu
->dev
, "\tstage 1 translation\n");
1852 if (id
& ID0_S2TS
) {
1853 smmu
->features
|= ARM_SMMU_FEAT_TRANS_S2
;
1854 dev_notice(smmu
->dev
, "\tstage 2 translation\n");
1858 smmu
->features
|= ARM_SMMU_FEAT_TRANS_NESTED
;
1859 dev_notice(smmu
->dev
, "\tnested translation\n");
1862 if (!(smmu
->features
&
1863 (ARM_SMMU_FEAT_TRANS_S1
| ARM_SMMU_FEAT_TRANS_S2
))) {
1864 dev_err(smmu
->dev
, "\tno translation support!\n");
1868 if ((id
& ID0_S1TS
) &&
1869 ((smmu
->version
< ARM_SMMU_V2
) || !(id
& ID0_ATOSNS
))) {
1870 smmu
->features
|= ARM_SMMU_FEAT_TRANS_OPS
;
1871 dev_notice(smmu
->dev
, "\taddress translation ops\n");
1875 * In order for DMA API calls to work properly, we must defer to what
1876 * the FW says about coherency, regardless of what the hardware claims.
1877 * Fortunately, this also opens up a workaround for systems where the
1878 * ID register value has ended up configured incorrectly.
1880 cttw_reg
= !!(id
& ID0_CTTW
);
1881 if (cttw_fw
|| cttw_reg
)
1882 dev_notice(smmu
->dev
, "\t%scoherent table walk\n",
1883 cttw_fw
? "" : "non-");
1884 if (cttw_fw
!= cttw_reg
)
1885 dev_notice(smmu
->dev
,
1886 "\t(IDR0.CTTW overridden by FW configuration)\n");
1888 /* Max. number of entries we have for stream matching/indexing */
1889 if (smmu
->version
== ARM_SMMU_V2
&& id
& ID0_EXIDS
) {
1890 smmu
->features
|= ARM_SMMU_FEAT_EXIDS
;
1893 size
= 1 << ((id
>> ID0_NUMSIDB_SHIFT
) & ID0_NUMSIDB_MASK
);
1895 smmu
->streamid_mask
= size
- 1;
1897 smmu
->features
|= ARM_SMMU_FEAT_STREAM_MATCH
;
1898 size
= (id
>> ID0_NUMSMRG_SHIFT
) & ID0_NUMSMRG_MASK
;
1901 "stream-matching supported, but no SMRs present!\n");
1905 /* Zero-initialised to mark as invalid */
1906 smmu
->smrs
= devm_kcalloc(smmu
->dev
, size
, sizeof(*smmu
->smrs
),
1911 dev_notice(smmu
->dev
,
1912 "\tstream matching with %lu register groups", size
);
1914 /* s2cr->type == 0 means translation, so initialise explicitly */
1915 smmu
->s2crs
= devm_kmalloc_array(smmu
->dev
, size
, sizeof(*smmu
->s2crs
),
1919 for (i
= 0; i
< size
; i
++)
1920 smmu
->s2crs
[i
] = s2cr_init_val
;
1922 smmu
->num_mapping_groups
= size
;
1923 mutex_init(&smmu
->stream_map_mutex
);
1924 spin_lock_init(&smmu
->global_sync_lock
);
1926 if (smmu
->version
< ARM_SMMU_V2
|| !(id
& ID0_PTFS_NO_AARCH32
)) {
1927 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH32_L
;
1928 if (!(id
& ID0_PTFS_NO_AARCH32S
))
1929 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH32_S
;
1933 id
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_ID1
);
1934 smmu
->pgshift
= (id
& ID1_PAGESIZE
) ? 16 : 12;
1936 /* Check for size mismatch of SMMU address space from mapped region */
1937 size
= 1 << (((id
>> ID1_NUMPAGENDXB_SHIFT
) & ID1_NUMPAGENDXB_MASK
) + 1);
1938 size
<<= smmu
->pgshift
;
1939 if (smmu
->cb_base
!= gr0_base
+ size
)
1941 "SMMU address space size (0x%lx) differs from mapped region size (0x%tx)!\n",
1942 size
* 2, (smmu
->cb_base
- gr0_base
) * 2);
1944 smmu
->num_s2_context_banks
= (id
>> ID1_NUMS2CB_SHIFT
) & ID1_NUMS2CB_MASK
;
1945 smmu
->num_context_banks
= (id
>> ID1_NUMCB_SHIFT
) & ID1_NUMCB_MASK
;
1946 if (smmu
->num_s2_context_banks
> smmu
->num_context_banks
) {
1947 dev_err(smmu
->dev
, "impossible number of S2 context banks!\n");
1950 dev_notice(smmu
->dev
, "\t%u context banks (%u stage-2 only)\n",
1951 smmu
->num_context_banks
, smmu
->num_s2_context_banks
);
1953 * Cavium CN88xx erratum #27704.
1954 * Ensure ASID and VMID allocation is unique across all SMMUs in
1957 if (smmu
->model
== CAVIUM_SMMUV2
) {
1958 smmu
->cavium_id_base
=
1959 atomic_add_return(smmu
->num_context_banks
,
1960 &cavium_smmu_context_count
);
1961 smmu
->cavium_id_base
-= smmu
->num_context_banks
;
1962 dev_notice(smmu
->dev
, "\tenabling workaround for Cavium erratum 27704\n");
1964 smmu
->cbs
= devm_kcalloc(smmu
->dev
, smmu
->num_context_banks
,
1965 sizeof(*smmu
->cbs
), GFP_KERNEL
);
1970 id
= readl_relaxed(gr0_base
+ ARM_SMMU_GR0_ID2
);
1971 size
= arm_smmu_id_size_to_bits((id
>> ID2_IAS_SHIFT
) & ID2_IAS_MASK
);
1972 smmu
->ipa_size
= size
;
1974 /* The output mask is also applied for bypass */
1975 size
= arm_smmu_id_size_to_bits((id
>> ID2_OAS_SHIFT
) & ID2_OAS_MASK
);
1976 smmu
->pa_size
= size
;
1978 if (id
& ID2_VMID16
)
1979 smmu
->features
|= ARM_SMMU_FEAT_VMID16
;
1982 * What the page table walker can address actually depends on which
1983 * descriptor format is in use, but since a) we don't know that yet,
1984 * and b) it can vary per context bank, this will have to do...
1986 if (dma_set_mask_and_coherent(smmu
->dev
, DMA_BIT_MASK(size
)))
1988 "failed to set DMA mask for table walker\n");
1990 if (smmu
->version
< ARM_SMMU_V2
) {
1991 smmu
->va_size
= smmu
->ipa_size
;
1992 if (smmu
->version
== ARM_SMMU_V1_64K
)
1993 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_64K
;
1995 size
= (id
>> ID2_UBS_SHIFT
) & ID2_UBS_MASK
;
1996 smmu
->va_size
= arm_smmu_id_size_to_bits(size
);
1997 if (id
& ID2_PTFS_4K
)
1998 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_4K
;
1999 if (id
& ID2_PTFS_16K
)
2000 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_16K
;
2001 if (id
& ID2_PTFS_64K
)
2002 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_64K
;
2005 /* Now we've corralled the various formats, what'll it do? */
2006 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH32_S
)
2007 smmu
->pgsize_bitmap
|= SZ_4K
| SZ_64K
| SZ_1M
| SZ_16M
;
2008 if (smmu
->features
&
2009 (ARM_SMMU_FEAT_FMT_AARCH32_L
| ARM_SMMU_FEAT_FMT_AARCH64_4K
))
2010 smmu
->pgsize_bitmap
|= SZ_4K
| SZ_2M
| SZ_1G
;
2011 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH64_16K
)
2012 smmu
->pgsize_bitmap
|= SZ_16K
| SZ_32M
;
2013 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH64_64K
)
2014 smmu
->pgsize_bitmap
|= SZ_64K
| SZ_512M
;
2016 if (arm_smmu_ops
.pgsize_bitmap
== -1UL)
2017 arm_smmu_ops
.pgsize_bitmap
= smmu
->pgsize_bitmap
;
2019 arm_smmu_ops
.pgsize_bitmap
|= smmu
->pgsize_bitmap
;
2020 dev_notice(smmu
->dev
, "\tSupported page sizes: 0x%08lx\n",
2021 smmu
->pgsize_bitmap
);
2024 if (smmu
->features
& ARM_SMMU_FEAT_TRANS_S1
)
2025 dev_notice(smmu
->dev
, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
2026 smmu
->va_size
, smmu
->ipa_size
);
2028 if (smmu
->features
& ARM_SMMU_FEAT_TRANS_S2
)
2029 dev_notice(smmu
->dev
, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
2030 smmu
->ipa_size
, smmu
->pa_size
);
2035 struct arm_smmu_match_data
{
2036 enum arm_smmu_arch_version version
;
2037 enum arm_smmu_implementation model
;
2040 #define ARM_SMMU_MATCH_DATA(name, ver, imp) \
2041 static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
2043 ARM_SMMU_MATCH_DATA(smmu_generic_v1
, ARM_SMMU_V1
, GENERIC_SMMU
);
2044 ARM_SMMU_MATCH_DATA(smmu_generic_v2
, ARM_SMMU_V2
, GENERIC_SMMU
);
2045 ARM_SMMU_MATCH_DATA(arm_mmu401
, ARM_SMMU_V1_64K
, GENERIC_SMMU
);
2046 ARM_SMMU_MATCH_DATA(arm_mmu500
, ARM_SMMU_V2
, ARM_MMU500
);
2047 ARM_SMMU_MATCH_DATA(cavium_smmuv2
, ARM_SMMU_V2
, CAVIUM_SMMUV2
);
2048 ARM_SMMU_MATCH_DATA(qcom_smmuv2
, ARM_SMMU_V2
, QCOM_SMMUV2
);
2050 static const struct of_device_id arm_smmu_of_match
[] = {
2051 { .compatible
= "arm,smmu-v1", .data
= &smmu_generic_v1
},
2052 { .compatible
= "arm,smmu-v2", .data
= &smmu_generic_v2
},
2053 { .compatible
= "arm,mmu-400", .data
= &smmu_generic_v1
},
2054 { .compatible
= "arm,mmu-401", .data
= &arm_mmu401
},
2055 { .compatible
= "arm,mmu-500", .data
= &arm_mmu500
},
2056 { .compatible
= "cavium,smmu-v2", .data
= &cavium_smmuv2
},
2057 { .compatible
= "qcom,smmu-v2", .data
= &qcom_smmuv2
},
2062 static int acpi_smmu_get_data(u32 model
, struct arm_smmu_device
*smmu
)
2067 case ACPI_IORT_SMMU_V1
:
2068 case ACPI_IORT_SMMU_CORELINK_MMU400
:
2069 smmu
->version
= ARM_SMMU_V1
;
2070 smmu
->model
= GENERIC_SMMU
;
2072 case ACPI_IORT_SMMU_CORELINK_MMU401
:
2073 smmu
->version
= ARM_SMMU_V1_64K
;
2074 smmu
->model
= GENERIC_SMMU
;
2076 case ACPI_IORT_SMMU_V2
:
2077 smmu
->version
= ARM_SMMU_V2
;
2078 smmu
->model
= GENERIC_SMMU
;
2080 case ACPI_IORT_SMMU_CORELINK_MMU500
:
2081 smmu
->version
= ARM_SMMU_V2
;
2082 smmu
->model
= ARM_MMU500
;
2084 case ACPI_IORT_SMMU_CAVIUM_THUNDERX
:
2085 smmu
->version
= ARM_SMMU_V2
;
2086 smmu
->model
= CAVIUM_SMMUV2
;
2095 static int arm_smmu_device_acpi_probe(struct platform_device
*pdev
,
2096 struct arm_smmu_device
*smmu
)
2098 struct device
*dev
= smmu
->dev
;
2099 struct acpi_iort_node
*node
=
2100 *(struct acpi_iort_node
**)dev_get_platdata(dev
);
2101 struct acpi_iort_smmu
*iort_smmu
;
2104 /* Retrieve SMMU1/2 specific data */
2105 iort_smmu
= (struct acpi_iort_smmu
*)node
->node_data
;
2107 ret
= acpi_smmu_get_data(iort_smmu
->model
, smmu
);
2111 /* Ignore the configuration access interrupt */
2112 smmu
->num_global_irqs
= 1;
2114 if (iort_smmu
->flags
& ACPI_IORT_SMMU_COHERENT_WALK
)
2115 smmu
->features
|= ARM_SMMU_FEAT_COHERENT_WALK
;
2120 static inline int arm_smmu_device_acpi_probe(struct platform_device
*pdev
,
2121 struct arm_smmu_device
*smmu
)
2127 static int arm_smmu_device_dt_probe(struct platform_device
*pdev
,
2128 struct arm_smmu_device
*smmu
)
2130 const struct arm_smmu_match_data
*data
;
2131 struct device
*dev
= &pdev
->dev
;
2132 bool legacy_binding
;
2134 if (of_property_read_u32(dev
->of_node
, "#global-interrupts",
2135 &smmu
->num_global_irqs
)) {
2136 dev_err(dev
, "missing #global-interrupts property\n");
2140 data
= of_device_get_match_data(dev
);
2141 smmu
->version
= data
->version
;
2142 smmu
->model
= data
->model
;
2144 parse_driver_options(smmu
);
2146 legacy_binding
= of_find_property(dev
->of_node
, "mmu-masters", NULL
);
2147 if (legacy_binding
&& !using_generic_binding
) {
2148 if (!using_legacy_binding
)
2149 pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
2150 using_legacy_binding
= true;
2151 } else if (!legacy_binding
&& !using_legacy_binding
) {
2152 using_generic_binding
= true;
2154 dev_err(dev
, "not probing due to mismatched DT properties\n");
2158 if (of_dma_is_coherent(dev
->of_node
))
2159 smmu
->features
|= ARM_SMMU_FEAT_COHERENT_WALK
;
2164 static void arm_smmu_bus_init(void)
2166 /* Oh, for a proper bus abstraction */
2167 if (!iommu_present(&platform_bus_type
))
2168 bus_set_iommu(&platform_bus_type
, &arm_smmu_ops
);
2169 #ifdef CONFIG_ARM_AMBA
2170 if (!iommu_present(&amba_bustype
))
2171 bus_set_iommu(&amba_bustype
, &arm_smmu_ops
);
2174 if (!iommu_present(&pci_bus_type
)) {
2176 bus_set_iommu(&pci_bus_type
, &arm_smmu_ops
);
2179 #ifdef CONFIG_FSL_MC_BUS
2180 if (!iommu_present(&fsl_mc_bus_type
))
2181 bus_set_iommu(&fsl_mc_bus_type
, &arm_smmu_ops
);
2185 static int arm_smmu_device_probe(struct platform_device
*pdev
)
2187 struct resource
*res
;
2188 resource_size_t ioaddr
;
2189 struct arm_smmu_device
*smmu
;
2190 struct device
*dev
= &pdev
->dev
;
2191 int num_irqs
, i
, err
;
2193 smmu
= devm_kzalloc(dev
, sizeof(*smmu
), GFP_KERNEL
);
2195 dev_err(dev
, "failed to allocate arm_smmu_device\n");
2201 err
= arm_smmu_device_dt_probe(pdev
, smmu
);
2203 err
= arm_smmu_device_acpi_probe(pdev
, smmu
);
2208 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2209 ioaddr
= res
->start
;
2210 smmu
->base
= devm_ioremap_resource(dev
, res
);
2211 if (IS_ERR(smmu
->base
))
2212 return PTR_ERR(smmu
->base
);
2213 smmu
->cb_base
= smmu
->base
+ resource_size(res
) / 2;
2216 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, num_irqs
))) {
2218 if (num_irqs
> smmu
->num_global_irqs
)
2219 smmu
->num_context_irqs
++;
2222 if (!smmu
->num_context_irqs
) {
2223 dev_err(dev
, "found %d interrupts but expected at least %d\n",
2224 num_irqs
, smmu
->num_global_irqs
+ 1);
2228 smmu
->irqs
= devm_kcalloc(dev
, num_irqs
, sizeof(*smmu
->irqs
),
2231 dev_err(dev
, "failed to allocate %d irqs\n", num_irqs
);
2235 for (i
= 0; i
< num_irqs
; ++i
) {
2236 int irq
= platform_get_irq(pdev
, i
);
2239 dev_err(dev
, "failed to get irq index %d\n", i
);
2242 smmu
->irqs
[i
] = irq
;
2245 err
= devm_clk_bulk_get_all(dev
, &smmu
->clks
);
2247 dev_err(dev
, "failed to get clocks %d\n", err
);
2250 smmu
->num_clks
= err
;
2252 err
= clk_bulk_prepare_enable(smmu
->num_clks
, smmu
->clks
);
2256 err
= arm_smmu_device_cfg_probe(smmu
);
2260 if (smmu
->version
== ARM_SMMU_V2
) {
2261 if (smmu
->num_context_banks
> smmu
->num_context_irqs
) {
2263 "found only %d context irq(s) but %d required\n",
2264 smmu
->num_context_irqs
, smmu
->num_context_banks
);
2268 /* Ignore superfluous interrupts */
2269 smmu
->num_context_irqs
= smmu
->num_context_banks
;
2272 for (i
= 0; i
< smmu
->num_global_irqs
; ++i
) {
2273 err
= devm_request_irq(smmu
->dev
, smmu
->irqs
[i
],
2274 arm_smmu_global_fault
,
2276 "arm-smmu global fault",
2279 dev_err(dev
, "failed to request global IRQ %d (%u)\n",
2285 err
= iommu_device_sysfs_add(&smmu
->iommu
, smmu
->dev
, NULL
,
2286 "smmu.%pa", &ioaddr
);
2288 dev_err(dev
, "Failed to register iommu in sysfs\n");
2292 iommu_device_set_ops(&smmu
->iommu
, &arm_smmu_ops
);
2293 iommu_device_set_fwnode(&smmu
->iommu
, dev
->fwnode
);
2295 err
= iommu_device_register(&smmu
->iommu
);
2297 dev_err(dev
, "Failed to register iommu\n");
2301 platform_set_drvdata(pdev
, smmu
);
2302 arm_smmu_device_reset(smmu
);
2303 arm_smmu_test_smr_masks(smmu
);
2306 * We want to avoid touching dev->power.lock in fastpaths unless
2307 * it's really going to do something useful - pm_runtime_enabled()
2308 * can serve as an ideal proxy for that decision. So, conditionally
2309 * enable pm_runtime.
2311 if (dev
->pm_domain
) {
2312 pm_runtime_set_active(dev
);
2313 pm_runtime_enable(dev
);
2317 * For ACPI and generic DT bindings, an SMMU will be probed before
2318 * any device which might need it, so we want the bus ops in place
2319 * ready to handle default domain setup as soon as any SMMU exists.
2321 if (!using_legacy_binding
)
2322 arm_smmu_bus_init();
2328 * With the legacy DT binding in play, though, we have no guarantees about
2329 * probe order, but then we're also not doing default domains, so we can
2330 * delay setting bus ops until we're sure every possible SMMU is ready,
2331 * and that way ensure that no add_device() calls get missed.
2333 static int arm_smmu_legacy_bus_init(void)
2335 if (using_legacy_binding
)
2336 arm_smmu_bus_init();
2339 device_initcall_sync(arm_smmu_legacy_bus_init
);
2341 static void arm_smmu_device_shutdown(struct platform_device
*pdev
)
2343 struct arm_smmu_device
*smmu
= platform_get_drvdata(pdev
);
2348 if (!bitmap_empty(smmu
->context_map
, ARM_SMMU_MAX_CBS
))
2349 dev_err(&pdev
->dev
, "removing device with active domains!\n");
2351 arm_smmu_rpm_get(smmu
);
2352 /* Turn the thing off */
2353 writel(sCR0_CLIENTPD
, ARM_SMMU_GR0_NS(smmu
) + ARM_SMMU_GR0_sCR0
);
2354 arm_smmu_rpm_put(smmu
);
2356 if (pm_runtime_enabled(smmu
->dev
))
2357 pm_runtime_force_suspend(smmu
->dev
);
2359 clk_bulk_disable(smmu
->num_clks
, smmu
->clks
);
2361 clk_bulk_unprepare(smmu
->num_clks
, smmu
->clks
);
2364 static int __maybe_unused
arm_smmu_runtime_resume(struct device
*dev
)
2366 struct arm_smmu_device
*smmu
= dev_get_drvdata(dev
);
2369 ret
= clk_bulk_enable(smmu
->num_clks
, smmu
->clks
);
2373 arm_smmu_device_reset(smmu
);
2378 static int __maybe_unused
arm_smmu_runtime_suspend(struct device
*dev
)
2380 struct arm_smmu_device
*smmu
= dev_get_drvdata(dev
);
2382 clk_bulk_disable(smmu
->num_clks
, smmu
->clks
);
2387 static int __maybe_unused
arm_smmu_pm_resume(struct device
*dev
)
2389 if (pm_runtime_suspended(dev
))
2392 return arm_smmu_runtime_resume(dev
);
2395 static int __maybe_unused
arm_smmu_pm_suspend(struct device
*dev
)
2397 if (pm_runtime_suspended(dev
))
2400 return arm_smmu_runtime_suspend(dev
);
2403 static const struct dev_pm_ops arm_smmu_pm_ops
= {
2404 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend
, arm_smmu_pm_resume
)
2405 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend
,
2406 arm_smmu_runtime_resume
, NULL
)
2409 static struct platform_driver arm_smmu_driver
= {
2412 .of_match_table
= of_match_ptr(arm_smmu_of_match
),
2413 .pm
= &arm_smmu_pm_ops
,
2414 .suppress_bind_attrs
= true,
2416 .probe
= arm_smmu_device_probe
,
2417 .shutdown
= arm_smmu_device_shutdown
,
2419 builtin_platform_driver(arm_smmu_driver
);