1 // SPDX-License-Identifier: GPL-2.0-only
3 * IOMMU API for ARM architected SMMU implementations.
5 * Copyright (C) 2013 ARM Limited
7 * Author: Will Deacon <will.deacon@arm.com>
9 * This driver currently supports:
10 * - SMMUv1 and v2 implementations
11 * - Stream-matching and stream-indexing
12 * - v7/v8 long-descriptor format
13 * - Non-secure access to the SMMU
14 * - Context fault reporting
15 * - Extended Stream ID (16 bit)
18 #define pr_fmt(fmt) "arm-smmu: " fmt
20 #include <linux/acpi.h>
21 #include <linux/acpi_iort.h>
22 #include <linux/bitfield.h>
23 #include <linux/delay.h>
24 #include <linux/dma-iommu.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
29 #include <linux/iopoll.h>
30 #include <linux/init.h>
31 #include <linux/moduleparam.h>
33 #include <linux/of_address.h>
34 #include <linux/of_device.h>
35 #include <linux/of_iommu.h>
36 #include <linux/pci.h>
37 #include <linux/platform_device.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/ratelimit.h>
40 #include <linux/slab.h>
42 #include <linux/amba/bus.h>
43 #include <linux/fsl/mc.h>
48 * Apparently, some Qualcomm arm64 platforms which appear to expose their SMMU
49 * global register space are still, in fact, using a hypervisor to mediate it
50 * by trapping and emulating register accesses. Sadly, some deployed versions
51 * of said trapping code have bugs wherein they go horribly wrong for stores
52 * using r31 (i.e. XZR/WZR) as the source register.
54 #define QCOM_DUMMY_VAL -1
56 #define TLB_LOOP_TIMEOUT 1000000 /* 1s! */
57 #define TLB_SPIN_COUNT 10
59 #define MSI_IOVA_BASE 0x8000000
60 #define MSI_IOVA_LENGTH 0x100000
62 static int force_stage
;
64 * not really modular, but the easiest way to keep compat with existing
65 * bootargs behaviour is to continue using module_param() here.
67 module_param(force_stage
, int, S_IRUGO
);
68 MODULE_PARM_DESC(force_stage
,
69 "Force SMMU mappings to be installed at a particular stage of translation. A value of '1' or '2' forces the corresponding stage. All other values are ignored (i.e. no stage is forced). Note that selecting a specific stage will disable support for nested translation.");
70 static bool disable_bypass
=
71 IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT
);
72 module_param(disable_bypass
, bool, S_IRUGO
);
73 MODULE_PARM_DESC(disable_bypass
,
74 "Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU.");
76 struct arm_smmu_s2cr
{
77 struct iommu_group
*group
;
79 enum arm_smmu_s2cr_type type
;
80 enum arm_smmu_s2cr_privcfg privcfg
;
84 #define s2cr_init_val (struct arm_smmu_s2cr){ \
85 .type = disable_bypass ? S2CR_TYPE_FAULT : S2CR_TYPE_BYPASS, \
98 struct arm_smmu_cfg
*cfg
;
101 struct arm_smmu_master_cfg
{
102 struct arm_smmu_device
*smmu
;
105 #define INVALID_SMENDX -1
106 #define __fwspec_cfg(fw) ((struct arm_smmu_master_cfg *)fw->iommu_priv)
107 #define fwspec_smmu(fw) (__fwspec_cfg(fw)->smmu)
108 #define fwspec_smendx(fw, i) \
109 (i >= fw->num_ids ? INVALID_SMENDX : __fwspec_cfg(fw)->smendx[i])
110 #define for_each_cfg_sme(fw, i, idx) \
111 for (i = 0; idx = fwspec_smendx(fw, i), i < fw->num_ids; ++i)
113 static bool using_legacy_binding
, using_generic_binding
;
115 static inline int arm_smmu_rpm_get(struct arm_smmu_device
*smmu
)
117 if (pm_runtime_enabled(smmu
->dev
))
118 return pm_runtime_get_sync(smmu
->dev
);
123 static inline void arm_smmu_rpm_put(struct arm_smmu_device
*smmu
)
125 if (pm_runtime_enabled(smmu
->dev
))
126 pm_runtime_put_autosuspend(smmu
->dev
);
129 static struct arm_smmu_domain
*to_smmu_domain(struct iommu_domain
*dom
)
131 return container_of(dom
, struct arm_smmu_domain
, domain
);
134 static struct device_node
*dev_get_dev_node(struct device
*dev
)
136 if (dev_is_pci(dev
)) {
137 struct pci_bus
*bus
= to_pci_dev(dev
)->bus
;
139 while (!pci_is_root_bus(bus
))
141 return of_node_get(bus
->bridge
->parent
->of_node
);
144 return of_node_get(dev
->of_node
);
147 static int __arm_smmu_get_pci_sid(struct pci_dev
*pdev
, u16 alias
, void *data
)
149 *((__be32
*)data
) = cpu_to_be32(alias
);
150 return 0; /* Continue walking */
153 static int __find_legacy_master_phandle(struct device
*dev
, void *data
)
155 struct of_phandle_iterator
*it
= *(void **)data
;
156 struct device_node
*np
= it
->node
;
159 of_for_each_phandle(it
, err
, dev
->of_node
, "mmu-masters",
160 "#stream-id-cells", -1)
161 if (it
->node
== np
) {
162 *(void **)data
= dev
;
166 return err
== -ENOENT
? 0 : err
;
169 static struct platform_driver arm_smmu_driver
;
170 static struct iommu_ops arm_smmu_ops
;
172 static int arm_smmu_register_legacy_master(struct device
*dev
,
173 struct arm_smmu_device
**smmu
)
175 struct device
*smmu_dev
;
176 struct device_node
*np
;
177 struct of_phandle_iterator it
;
183 np
= dev_get_dev_node(dev
);
184 if (!np
|| !of_find_property(np
, "#stream-id-cells", NULL
)) {
190 err
= driver_for_each_device(&arm_smmu_driver
.driver
, NULL
, &data
,
191 __find_legacy_master_phandle
);
199 if (dev_is_pci(dev
)) {
200 /* "mmu-masters" assumes Stream ID == Requester ID */
201 pci_for_each_dma_alias(to_pci_dev(dev
), __arm_smmu_get_pci_sid
,
207 err
= iommu_fwspec_init(dev
, &smmu_dev
->of_node
->fwnode
,
212 sids
= kcalloc(it
.cur_count
, sizeof(*sids
), GFP_KERNEL
);
216 *smmu
= dev_get_drvdata(smmu_dev
);
217 of_phandle_iterator_args(&it
, sids
, it
.cur_count
);
218 err
= iommu_fwspec_add_ids(dev
, sids
, it
.cur_count
);
223 static int __arm_smmu_alloc_bitmap(unsigned long *map
, int start
, int end
)
228 idx
= find_next_zero_bit(map
, end
, start
);
231 } while (test_and_set_bit(idx
, map
));
236 static void __arm_smmu_free_bitmap(unsigned long *map
, int idx
)
241 /* Wait for any pending TLB invalidations to complete */
242 static void __arm_smmu_tlb_sync(struct arm_smmu_device
*smmu
, int page
,
243 int sync
, int status
)
245 unsigned int spin_cnt
, delay
;
248 if (smmu
->impl
&& unlikely(smmu
->impl
->tlb_sync
))
249 return smmu
->impl
->tlb_sync(smmu
, page
, sync
, status
);
251 arm_smmu_writel(smmu
, page
, sync
, QCOM_DUMMY_VAL
);
252 for (delay
= 1; delay
< TLB_LOOP_TIMEOUT
; delay
*= 2) {
253 for (spin_cnt
= TLB_SPIN_COUNT
; spin_cnt
> 0; spin_cnt
--) {
254 reg
= arm_smmu_readl(smmu
, page
, status
);
255 if (!(reg
& sTLBGSTATUS_GSACTIVE
))
261 dev_err_ratelimited(smmu
->dev
,
262 "TLB sync timed out -- SMMU may be deadlocked\n");
265 static void arm_smmu_tlb_sync_global(struct arm_smmu_device
*smmu
)
269 spin_lock_irqsave(&smmu
->global_sync_lock
, flags
);
270 __arm_smmu_tlb_sync(smmu
, ARM_SMMU_GR0
, ARM_SMMU_GR0_sTLBGSYNC
,
271 ARM_SMMU_GR0_sTLBGSTATUS
);
272 spin_unlock_irqrestore(&smmu
->global_sync_lock
, flags
);
275 static void arm_smmu_tlb_sync_context(struct arm_smmu_domain
*smmu_domain
)
277 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
280 spin_lock_irqsave(&smmu_domain
->cb_lock
, flags
);
281 __arm_smmu_tlb_sync(smmu
, ARM_SMMU_CB(smmu
, smmu_domain
->cfg
.cbndx
),
282 ARM_SMMU_CB_TLBSYNC
, ARM_SMMU_CB_TLBSTATUS
);
283 spin_unlock_irqrestore(&smmu_domain
->cb_lock
, flags
);
286 static void arm_smmu_tlb_inv_context_s1(void *cookie
)
288 struct arm_smmu_domain
*smmu_domain
= cookie
;
290 * The TLBI write may be relaxed, so ensure that PTEs cleared by the
291 * current CPU are visible beforehand.
294 arm_smmu_cb_write(smmu_domain
->smmu
, smmu_domain
->cfg
.cbndx
,
295 ARM_SMMU_CB_S1_TLBIASID
, smmu_domain
->cfg
.asid
);
296 arm_smmu_tlb_sync_context(smmu_domain
);
299 static void arm_smmu_tlb_inv_context_s2(void *cookie
)
301 struct arm_smmu_domain
*smmu_domain
= cookie
;
302 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
306 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_TLBIVMID
, smmu_domain
->cfg
.vmid
);
307 arm_smmu_tlb_sync_global(smmu
);
310 static void arm_smmu_tlb_inv_range_s1(unsigned long iova
, size_t size
,
311 size_t granule
, void *cookie
, int reg
)
313 struct arm_smmu_domain
*smmu_domain
= cookie
;
314 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
315 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
316 int idx
= cfg
->cbndx
;
318 if (smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
)
321 if (cfg
->fmt
!= ARM_SMMU_CTX_FMT_AARCH64
) {
322 iova
= (iova
>> 12) << 12;
325 arm_smmu_cb_write(smmu
, idx
, reg
, iova
);
327 } while (size
-= granule
);
330 iova
|= (u64
)cfg
->asid
<< 48;
332 arm_smmu_cb_writeq(smmu
, idx
, reg
, iova
);
333 iova
+= granule
>> 12;
334 } while (size
-= granule
);
338 static void arm_smmu_tlb_inv_range_s2(unsigned long iova
, size_t size
,
339 size_t granule
, void *cookie
, int reg
)
341 struct arm_smmu_domain
*smmu_domain
= cookie
;
342 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
343 int idx
= smmu_domain
->cfg
.cbndx
;
345 if (smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
)
350 if (smmu_domain
->cfg
.fmt
== ARM_SMMU_CTX_FMT_AARCH64
)
351 arm_smmu_cb_writeq(smmu
, idx
, reg
, iova
);
353 arm_smmu_cb_write(smmu
, idx
, reg
, iova
);
354 iova
+= granule
>> 12;
355 } while (size
-= granule
);
358 static void arm_smmu_tlb_inv_walk_s1(unsigned long iova
, size_t size
,
359 size_t granule
, void *cookie
)
361 arm_smmu_tlb_inv_range_s1(iova
, size
, granule
, cookie
,
362 ARM_SMMU_CB_S1_TLBIVA
);
363 arm_smmu_tlb_sync_context(cookie
);
366 static void arm_smmu_tlb_inv_leaf_s1(unsigned long iova
, size_t size
,
367 size_t granule
, void *cookie
)
369 arm_smmu_tlb_inv_range_s1(iova
, size
, granule
, cookie
,
370 ARM_SMMU_CB_S1_TLBIVAL
);
371 arm_smmu_tlb_sync_context(cookie
);
374 static void arm_smmu_tlb_add_page_s1(struct iommu_iotlb_gather
*gather
,
375 unsigned long iova
, size_t granule
,
378 arm_smmu_tlb_inv_range_s1(iova
, granule
, granule
, cookie
,
379 ARM_SMMU_CB_S1_TLBIVAL
);
382 static void arm_smmu_tlb_inv_walk_s2(unsigned long iova
, size_t size
,
383 size_t granule
, void *cookie
)
385 arm_smmu_tlb_inv_range_s2(iova
, size
, granule
, cookie
,
386 ARM_SMMU_CB_S2_TLBIIPAS2
);
387 arm_smmu_tlb_sync_context(cookie
);
390 static void arm_smmu_tlb_inv_leaf_s2(unsigned long iova
, size_t size
,
391 size_t granule
, void *cookie
)
393 arm_smmu_tlb_inv_range_s2(iova
, size
, granule
, cookie
,
394 ARM_SMMU_CB_S2_TLBIIPAS2L
);
395 arm_smmu_tlb_sync_context(cookie
);
398 static void arm_smmu_tlb_add_page_s2(struct iommu_iotlb_gather
*gather
,
399 unsigned long iova
, size_t granule
,
402 arm_smmu_tlb_inv_range_s2(iova
, granule
, granule
, cookie
,
403 ARM_SMMU_CB_S2_TLBIIPAS2L
);
406 static void arm_smmu_tlb_inv_any_s2_v1(unsigned long iova
, size_t size
,
407 size_t granule
, void *cookie
)
409 arm_smmu_tlb_inv_context_s2(cookie
);
412 * On MMU-401 at least, the cost of firing off multiple TLBIVMIDs appears
413 * almost negligible, but the benefit of getting the first one in as far ahead
414 * of the sync as possible is significant, hence we don't just make this a
415 * no-op and call arm_smmu_tlb_inv_context_s2() from .iotlb_sync as you might
418 static void arm_smmu_tlb_add_page_s2_v1(struct iommu_iotlb_gather
*gather
,
419 unsigned long iova
, size_t granule
,
422 struct arm_smmu_domain
*smmu_domain
= cookie
;
423 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
425 if (smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
)
428 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_TLBIVMID
, smmu_domain
->cfg
.vmid
);
431 static const struct iommu_flush_ops arm_smmu_s1_tlb_ops
= {
432 .tlb_flush_all
= arm_smmu_tlb_inv_context_s1
,
433 .tlb_flush_walk
= arm_smmu_tlb_inv_walk_s1
,
434 .tlb_flush_leaf
= arm_smmu_tlb_inv_leaf_s1
,
435 .tlb_add_page
= arm_smmu_tlb_add_page_s1
,
438 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v2
= {
439 .tlb_flush_all
= arm_smmu_tlb_inv_context_s2
,
440 .tlb_flush_walk
= arm_smmu_tlb_inv_walk_s2
,
441 .tlb_flush_leaf
= arm_smmu_tlb_inv_leaf_s2
,
442 .tlb_add_page
= arm_smmu_tlb_add_page_s2
,
445 static const struct iommu_flush_ops arm_smmu_s2_tlb_ops_v1
= {
446 .tlb_flush_all
= arm_smmu_tlb_inv_context_s2
,
447 .tlb_flush_walk
= arm_smmu_tlb_inv_any_s2_v1
,
448 .tlb_flush_leaf
= arm_smmu_tlb_inv_any_s2_v1
,
449 .tlb_add_page
= arm_smmu_tlb_add_page_s2_v1
,
452 static irqreturn_t
arm_smmu_context_fault(int irq
, void *dev
)
454 u32 fsr
, fsynr
, cbfrsynra
;
456 struct iommu_domain
*domain
= dev
;
457 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
458 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
459 int idx
= smmu_domain
->cfg
.cbndx
;
461 fsr
= arm_smmu_cb_read(smmu
, idx
, ARM_SMMU_CB_FSR
);
462 if (!(fsr
& FSR_FAULT
))
465 fsynr
= arm_smmu_cb_read(smmu
, idx
, ARM_SMMU_CB_FSYNR0
);
466 iova
= arm_smmu_cb_readq(smmu
, idx
, ARM_SMMU_CB_FAR
);
467 cbfrsynra
= arm_smmu_gr1_read(smmu
, ARM_SMMU_GR1_CBFRSYNRA(idx
));
469 dev_err_ratelimited(smmu
->dev
,
470 "Unhandled context fault: fsr=0x%x, iova=0x%08lx, fsynr=0x%x, cbfrsynra=0x%x, cb=%d\n",
471 fsr
, iova
, fsynr
, cbfrsynra
, idx
);
473 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_FSR
, fsr
);
477 static irqreturn_t
arm_smmu_global_fault(int irq
, void *dev
)
479 u32 gfsr
, gfsynr0
, gfsynr1
, gfsynr2
;
480 struct arm_smmu_device
*smmu
= dev
;
481 static DEFINE_RATELIMIT_STATE(rs
, DEFAULT_RATELIMIT_INTERVAL
,
482 DEFAULT_RATELIMIT_BURST
);
484 gfsr
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_sGFSR
);
485 gfsynr0
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_sGFSYNR0
);
486 gfsynr1
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_sGFSYNR1
);
487 gfsynr2
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_sGFSYNR2
);
492 if (__ratelimit(&rs
)) {
493 if (IS_ENABLED(CONFIG_ARM_SMMU_DISABLE_BYPASS_BY_DEFAULT
) &&
496 "Blocked unknown Stream ID 0x%hx; boot with \"arm-smmu.disable_bypass=0\" to allow, but this may have security implications\n",
500 "Unexpected global fault, this could be serious\n");
502 "\tGFSR 0x%08x, GFSYNR0 0x%08x, GFSYNR1 0x%08x, GFSYNR2 0x%08x\n",
503 gfsr
, gfsynr0
, gfsynr1
, gfsynr2
);
506 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_sGFSR
, gfsr
);
510 static void arm_smmu_init_context_bank(struct arm_smmu_domain
*smmu_domain
,
511 struct io_pgtable_cfg
*pgtbl_cfg
)
513 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
514 struct arm_smmu_cb
*cb
= &smmu_domain
->smmu
->cbs
[cfg
->cbndx
];
515 bool stage1
= cfg
->cbar
!= CBAR_TYPE_S2_TRANS
;
521 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
522 cb
->tcr
[0] = pgtbl_cfg
->arm_v7s_cfg
.tcr
;
524 cb
->tcr
[0] = pgtbl_cfg
->arm_lpae_s1_cfg
.tcr
;
525 cb
->tcr
[1] = pgtbl_cfg
->arm_lpae_s1_cfg
.tcr
>> 32;
526 cb
->tcr
[1] |= FIELD_PREP(TCR2_SEP
, TCR2_SEP_UPSTREAM
);
527 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
)
528 cb
->tcr
[1] |= TCR2_AS
;
531 cb
->tcr
[0] = pgtbl_cfg
->arm_lpae_s2_cfg
.vtcr
;
536 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
537 cb
->ttbr
[0] = pgtbl_cfg
->arm_v7s_cfg
.ttbr
[0];
538 cb
->ttbr
[1] = pgtbl_cfg
->arm_v7s_cfg
.ttbr
[1];
540 cb
->ttbr
[0] = pgtbl_cfg
->arm_lpae_s1_cfg
.ttbr
[0];
541 cb
->ttbr
[0] |= FIELD_PREP(TTBRn_ASID
, cfg
->asid
);
542 cb
->ttbr
[1] = pgtbl_cfg
->arm_lpae_s1_cfg
.ttbr
[1];
543 cb
->ttbr
[1] |= FIELD_PREP(TTBRn_ASID
, cfg
->asid
);
546 cb
->ttbr
[0] = pgtbl_cfg
->arm_lpae_s2_cfg
.vttbr
;
549 /* MAIRs (stage-1 only) */
551 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
552 cb
->mair
[0] = pgtbl_cfg
->arm_v7s_cfg
.prrr
;
553 cb
->mair
[1] = pgtbl_cfg
->arm_v7s_cfg
.nmrr
;
555 cb
->mair
[0] = pgtbl_cfg
->arm_lpae_s1_cfg
.mair
;
556 cb
->mair
[1] = pgtbl_cfg
->arm_lpae_s1_cfg
.mair
>> 32;
561 static void arm_smmu_write_context_bank(struct arm_smmu_device
*smmu
, int idx
)
565 struct arm_smmu_cb
*cb
= &smmu
->cbs
[idx
];
566 struct arm_smmu_cfg
*cfg
= cb
->cfg
;
568 /* Unassigned context banks only need disabling */
570 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_SCTLR
, 0);
574 stage1
= cfg
->cbar
!= CBAR_TYPE_S2_TRANS
;
577 if (smmu
->version
> ARM_SMMU_V1
) {
578 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
)
582 /* 16-bit VMIDs live in CBA2R */
583 if (smmu
->features
& ARM_SMMU_FEAT_VMID16
)
584 reg
|= FIELD_PREP(CBA2R_VMID16
, cfg
->vmid
);
586 arm_smmu_gr1_write(smmu
, ARM_SMMU_GR1_CBA2R(idx
), reg
);
590 reg
= FIELD_PREP(CBAR_TYPE
, cfg
->cbar
);
591 if (smmu
->version
< ARM_SMMU_V2
)
592 reg
|= FIELD_PREP(CBAR_IRPTNDX
, cfg
->irptndx
);
595 * Use the weakest shareability/memory types, so they are
596 * overridden by the ttbcr/pte.
599 reg
|= FIELD_PREP(CBAR_S1_BPSHCFG
, CBAR_S1_BPSHCFG_NSH
) |
600 FIELD_PREP(CBAR_S1_MEMATTR
, CBAR_S1_MEMATTR_WB
);
601 } else if (!(smmu
->features
& ARM_SMMU_FEAT_VMID16
)) {
602 /* 8-bit VMIDs live in CBAR */
603 reg
|= FIELD_PREP(CBAR_VMID
, cfg
->vmid
);
605 arm_smmu_gr1_write(smmu
, ARM_SMMU_GR1_CBAR(idx
), reg
);
609 * We must write this before the TTBRs, since it determines the
610 * access behaviour of some fields (in particular, ASID[15:8]).
612 if (stage1
&& smmu
->version
> ARM_SMMU_V1
)
613 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_TCR2
, cb
->tcr
[1]);
614 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_TCR
, cb
->tcr
[0]);
617 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_S
) {
618 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_CONTEXTIDR
, cfg
->asid
);
619 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_TTBR0
, cb
->ttbr
[0]);
620 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_TTBR1
, cb
->ttbr
[1]);
622 arm_smmu_cb_writeq(smmu
, idx
, ARM_SMMU_CB_TTBR0
, cb
->ttbr
[0]);
624 arm_smmu_cb_writeq(smmu
, idx
, ARM_SMMU_CB_TTBR1
,
628 /* MAIRs (stage-1 only) */
630 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_S1_MAIR0
, cb
->mair
[0]);
631 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_S1_MAIR1
, cb
->mair
[1]);
635 reg
= SCTLR_CFIE
| SCTLR_CFRE
| SCTLR_AFE
| SCTLR_TRE
| SCTLR_M
;
637 reg
|= SCTLR_S1_ASIDPNE
;
638 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
641 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_SCTLR
, reg
);
644 static int arm_smmu_init_domain_context(struct iommu_domain
*domain
,
645 struct arm_smmu_device
*smmu
)
647 int irq
, start
, ret
= 0;
648 unsigned long ias
, oas
;
649 struct io_pgtable_ops
*pgtbl_ops
;
650 struct io_pgtable_cfg pgtbl_cfg
;
651 enum io_pgtable_fmt fmt
;
652 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
653 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
655 mutex_lock(&smmu_domain
->init_mutex
);
656 if (smmu_domain
->smmu
)
659 if (domain
->type
== IOMMU_DOMAIN_IDENTITY
) {
660 smmu_domain
->stage
= ARM_SMMU_DOMAIN_BYPASS
;
661 smmu_domain
->smmu
= smmu
;
666 * Mapping the requested stage onto what we support is surprisingly
667 * complicated, mainly because the spec allows S1+S2 SMMUs without
668 * support for nested translation. That means we end up with the
671 * Requested Supported Actual
681 * Note that you can't actually request stage-2 mappings.
683 if (!(smmu
->features
& ARM_SMMU_FEAT_TRANS_S1
))
684 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S2
;
685 if (!(smmu
->features
& ARM_SMMU_FEAT_TRANS_S2
))
686 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S1
;
689 * Choosing a suitable context format is even more fiddly. Until we
690 * grow some way for the caller to express a preference, and/or move
691 * the decision into the io-pgtable code where it arguably belongs,
692 * just aim for the closest thing to the rest of the system, and hope
693 * that the hardware isn't esoteric enough that we can't assume AArch64
694 * support to be a superset of AArch32 support...
696 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH32_L
)
697 cfg
->fmt
= ARM_SMMU_CTX_FMT_AARCH32_L
;
698 if (IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_ARMV7S
) &&
699 !IS_ENABLED(CONFIG_64BIT
) && !IS_ENABLED(CONFIG_ARM_LPAE
) &&
700 (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH32_S
) &&
701 (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
))
702 cfg
->fmt
= ARM_SMMU_CTX_FMT_AARCH32_S
;
703 if ((IS_ENABLED(CONFIG_64BIT
) || cfg
->fmt
== ARM_SMMU_CTX_FMT_NONE
) &&
704 (smmu
->features
& (ARM_SMMU_FEAT_FMT_AARCH64_64K
|
705 ARM_SMMU_FEAT_FMT_AARCH64_16K
|
706 ARM_SMMU_FEAT_FMT_AARCH64_4K
)))
707 cfg
->fmt
= ARM_SMMU_CTX_FMT_AARCH64
;
709 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_NONE
) {
714 switch (smmu_domain
->stage
) {
715 case ARM_SMMU_DOMAIN_S1
:
716 cfg
->cbar
= CBAR_TYPE_S1_TRANS_S2_BYPASS
;
717 start
= smmu
->num_s2_context_banks
;
719 oas
= smmu
->ipa_size
;
720 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
) {
721 fmt
= ARM_64_LPAE_S1
;
722 } else if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH32_L
) {
723 fmt
= ARM_32_LPAE_S1
;
724 ias
= min(ias
, 32UL);
725 oas
= min(oas
, 40UL);
728 ias
= min(ias
, 32UL);
729 oas
= min(oas
, 32UL);
731 smmu_domain
->flush_ops
= &arm_smmu_s1_tlb_ops
;
733 case ARM_SMMU_DOMAIN_NESTED
:
735 * We will likely want to change this if/when KVM gets
738 case ARM_SMMU_DOMAIN_S2
:
739 cfg
->cbar
= CBAR_TYPE_S2_TRANS
;
741 ias
= smmu
->ipa_size
;
743 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
) {
744 fmt
= ARM_64_LPAE_S2
;
746 fmt
= ARM_32_LPAE_S2
;
747 ias
= min(ias
, 40UL);
748 oas
= min(oas
, 40UL);
750 if (smmu
->version
== ARM_SMMU_V2
)
751 smmu_domain
->flush_ops
= &arm_smmu_s2_tlb_ops_v2
;
753 smmu_domain
->flush_ops
= &arm_smmu_s2_tlb_ops_v1
;
759 ret
= __arm_smmu_alloc_bitmap(smmu
->context_map
, start
,
760 smmu
->num_context_banks
);
765 if (smmu
->version
< ARM_SMMU_V2
) {
766 cfg
->irptndx
= atomic_inc_return(&smmu
->irptndx
);
767 cfg
->irptndx
%= smmu
->num_context_irqs
;
769 cfg
->irptndx
= cfg
->cbndx
;
772 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_S2
)
773 cfg
->vmid
= cfg
->cbndx
+ 1;
775 cfg
->asid
= cfg
->cbndx
;
777 smmu_domain
->smmu
= smmu
;
778 if (smmu
->impl
&& smmu
->impl
->init_context
) {
779 ret
= smmu
->impl
->init_context(smmu_domain
);
784 pgtbl_cfg
= (struct io_pgtable_cfg
) {
785 .pgsize_bitmap
= smmu
->pgsize_bitmap
,
788 .coherent_walk
= smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
,
789 .tlb
= smmu_domain
->flush_ops
,
790 .iommu_dev
= smmu
->dev
,
793 if (smmu_domain
->non_strict
)
794 pgtbl_cfg
.quirks
|= IO_PGTABLE_QUIRK_NON_STRICT
;
796 pgtbl_ops
= alloc_io_pgtable_ops(fmt
, &pgtbl_cfg
, smmu_domain
);
802 /* Update the domain's page sizes to reflect the page table format */
803 domain
->pgsize_bitmap
= pgtbl_cfg
.pgsize_bitmap
;
804 domain
->geometry
.aperture_end
= (1UL << ias
) - 1;
805 domain
->geometry
.force_aperture
= true;
807 /* Initialise the context bank with our page table cfg */
808 arm_smmu_init_context_bank(smmu_domain
, &pgtbl_cfg
);
809 arm_smmu_write_context_bank(smmu
, cfg
->cbndx
);
812 * Request context fault interrupt. Do this last to avoid the
813 * handler seeing a half-initialised domain state.
815 irq
= smmu
->irqs
[smmu
->num_global_irqs
+ cfg
->irptndx
];
816 ret
= devm_request_irq(smmu
->dev
, irq
, arm_smmu_context_fault
,
817 IRQF_SHARED
, "arm-smmu-context-fault", domain
);
819 dev_err(smmu
->dev
, "failed to request context IRQ %d (%u)\n",
821 cfg
->irptndx
= INVALID_IRPTNDX
;
824 mutex_unlock(&smmu_domain
->init_mutex
);
826 /* Publish page table ops for map/unmap */
827 smmu_domain
->pgtbl_ops
= pgtbl_ops
;
831 __arm_smmu_free_bitmap(smmu
->context_map
, cfg
->cbndx
);
832 smmu_domain
->smmu
= NULL
;
834 mutex_unlock(&smmu_domain
->init_mutex
);
838 static void arm_smmu_destroy_domain_context(struct iommu_domain
*domain
)
840 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
841 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
842 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
845 if (!smmu
|| domain
->type
== IOMMU_DOMAIN_IDENTITY
)
848 ret
= arm_smmu_rpm_get(smmu
);
853 * Disable the context bank and free the page tables before freeing
856 smmu
->cbs
[cfg
->cbndx
].cfg
= NULL
;
857 arm_smmu_write_context_bank(smmu
, cfg
->cbndx
);
859 if (cfg
->irptndx
!= INVALID_IRPTNDX
) {
860 irq
= smmu
->irqs
[smmu
->num_global_irqs
+ cfg
->irptndx
];
861 devm_free_irq(smmu
->dev
, irq
, domain
);
864 free_io_pgtable_ops(smmu_domain
->pgtbl_ops
);
865 __arm_smmu_free_bitmap(smmu
->context_map
, cfg
->cbndx
);
867 arm_smmu_rpm_put(smmu
);
870 static struct iommu_domain
*arm_smmu_domain_alloc(unsigned type
)
872 struct arm_smmu_domain
*smmu_domain
;
874 if (type
!= IOMMU_DOMAIN_UNMANAGED
&&
875 type
!= IOMMU_DOMAIN_DMA
&&
876 type
!= IOMMU_DOMAIN_IDENTITY
)
879 * Allocate the domain and initialise some of its data structures.
880 * We can't really do anything meaningful until we've added a
883 smmu_domain
= kzalloc(sizeof(*smmu_domain
), GFP_KERNEL
);
887 if (type
== IOMMU_DOMAIN_DMA
&& (using_legacy_binding
||
888 iommu_get_dma_cookie(&smmu_domain
->domain
))) {
893 mutex_init(&smmu_domain
->init_mutex
);
894 spin_lock_init(&smmu_domain
->cb_lock
);
896 return &smmu_domain
->domain
;
899 static void arm_smmu_domain_free(struct iommu_domain
*domain
)
901 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
904 * Free the domain resources. We assume that all devices have
905 * already been detached.
907 iommu_put_dma_cookie(domain
);
908 arm_smmu_destroy_domain_context(domain
);
912 static void arm_smmu_write_smr(struct arm_smmu_device
*smmu
, int idx
)
914 struct arm_smmu_smr
*smr
= smmu
->smrs
+ idx
;
915 u32 reg
= FIELD_PREP(SMR_ID
, smr
->id
) | FIELD_PREP(SMR_MASK
, smr
->mask
);
917 if (!(smmu
->features
& ARM_SMMU_FEAT_EXIDS
) && smr
->valid
)
919 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_SMR(idx
), reg
);
922 static void arm_smmu_write_s2cr(struct arm_smmu_device
*smmu
, int idx
)
924 struct arm_smmu_s2cr
*s2cr
= smmu
->s2crs
+ idx
;
925 u32 reg
= FIELD_PREP(S2CR_TYPE
, s2cr
->type
) |
926 FIELD_PREP(S2CR_CBNDX
, s2cr
->cbndx
) |
927 FIELD_PREP(S2CR_PRIVCFG
, s2cr
->privcfg
);
929 if (smmu
->features
& ARM_SMMU_FEAT_EXIDS
&& smmu
->smrs
&&
930 smmu
->smrs
[idx
].valid
)
931 reg
|= S2CR_EXIDVALID
;
932 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_S2CR(idx
), reg
);
935 static void arm_smmu_write_sme(struct arm_smmu_device
*smmu
, int idx
)
937 arm_smmu_write_s2cr(smmu
, idx
);
939 arm_smmu_write_smr(smmu
, idx
);
943 * The width of SMR's mask field depends on sCR0_EXIDENABLE, so this function
944 * should be called after sCR0 is written.
946 static void arm_smmu_test_smr_masks(struct arm_smmu_device
*smmu
)
954 * SMR.ID bits may not be preserved if the corresponding MASK
955 * bits are set, so check each one separately. We can reject
956 * masters later if they try to claim IDs outside these masks.
958 smr
= FIELD_PREP(SMR_ID
, smmu
->streamid_mask
);
959 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_SMR(0), smr
);
960 smr
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_SMR(0));
961 smmu
->streamid_mask
= FIELD_GET(SMR_ID
, smr
);
963 smr
= FIELD_PREP(SMR_MASK
, smmu
->streamid_mask
);
964 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_SMR(0), smr
);
965 smr
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_SMR(0));
966 smmu
->smr_mask_mask
= FIELD_GET(SMR_MASK
, smr
);
969 static int arm_smmu_find_sme(struct arm_smmu_device
*smmu
, u16 id
, u16 mask
)
971 struct arm_smmu_smr
*smrs
= smmu
->smrs
;
972 int i
, free_idx
= -ENOSPC
;
974 /* Stream indexing is blissfully easy */
978 /* Validating SMRs is... less so */
979 for (i
= 0; i
< smmu
->num_mapping_groups
; ++i
) {
980 if (!smrs
[i
].valid
) {
982 * Note the first free entry we come across, which
983 * we'll claim in the end if nothing else matches.
990 * If the new entry is _entirely_ matched by an existing entry,
991 * then reuse that, with the guarantee that there also cannot
992 * be any subsequent conflicting entries. In normal use we'd
993 * expect simply identical entries for this case, but there's
994 * no harm in accommodating the generalisation.
996 if ((mask
& smrs
[i
].mask
) == mask
&&
997 !((id
^ smrs
[i
].id
) & ~smrs
[i
].mask
))
1000 * If the new entry has any other overlap with an existing one,
1001 * though, then there always exists at least one stream ID
1002 * which would cause a conflict, and we can't allow that risk.
1004 if (!((id
^ smrs
[i
].id
) & ~(smrs
[i
].mask
| mask
)))
1011 static bool arm_smmu_free_sme(struct arm_smmu_device
*smmu
, int idx
)
1013 if (--smmu
->s2crs
[idx
].count
)
1016 smmu
->s2crs
[idx
] = s2cr_init_val
;
1018 smmu
->smrs
[idx
].valid
= false;
1023 static int arm_smmu_master_alloc_smes(struct device
*dev
)
1025 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1026 struct arm_smmu_master_cfg
*cfg
= fwspec
->iommu_priv
;
1027 struct arm_smmu_device
*smmu
= cfg
->smmu
;
1028 struct arm_smmu_smr
*smrs
= smmu
->smrs
;
1029 struct iommu_group
*group
;
1032 mutex_lock(&smmu
->stream_map_mutex
);
1033 /* Figure out a viable stream map entry allocation */
1034 for_each_cfg_sme(fwspec
, i
, idx
) {
1035 u16 sid
= FIELD_GET(SMR_ID
, fwspec
->ids
[i
]);
1036 u16 mask
= FIELD_GET(SMR_MASK
, fwspec
->ids
[i
]);
1038 if (idx
!= INVALID_SMENDX
) {
1043 ret
= arm_smmu_find_sme(smmu
, sid
, mask
);
1048 if (smrs
&& smmu
->s2crs
[idx
].count
== 0) {
1050 smrs
[idx
].mask
= mask
;
1051 smrs
[idx
].valid
= true;
1053 smmu
->s2crs
[idx
].count
++;
1054 cfg
->smendx
[i
] = (s16
)idx
;
1057 group
= iommu_group_get_for_dev(dev
);
1058 if (IS_ERR(group
)) {
1059 ret
= PTR_ERR(group
);
1062 iommu_group_put(group
);
1064 /* It worked! Now, poke the actual hardware */
1065 for_each_cfg_sme(fwspec
, i
, idx
) {
1066 arm_smmu_write_sme(smmu
, idx
);
1067 smmu
->s2crs
[idx
].group
= group
;
1070 mutex_unlock(&smmu
->stream_map_mutex
);
1075 arm_smmu_free_sme(smmu
, cfg
->smendx
[i
]);
1076 cfg
->smendx
[i
] = INVALID_SMENDX
;
1078 mutex_unlock(&smmu
->stream_map_mutex
);
1082 static void arm_smmu_master_free_smes(struct iommu_fwspec
*fwspec
)
1084 struct arm_smmu_device
*smmu
= fwspec_smmu(fwspec
);
1085 struct arm_smmu_master_cfg
*cfg
= fwspec
->iommu_priv
;
1088 mutex_lock(&smmu
->stream_map_mutex
);
1089 for_each_cfg_sme(fwspec
, i
, idx
) {
1090 if (arm_smmu_free_sme(smmu
, idx
))
1091 arm_smmu_write_sme(smmu
, idx
);
1092 cfg
->smendx
[i
] = INVALID_SMENDX
;
1094 mutex_unlock(&smmu
->stream_map_mutex
);
1097 static int arm_smmu_domain_add_master(struct arm_smmu_domain
*smmu_domain
,
1098 struct iommu_fwspec
*fwspec
)
1100 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1101 struct arm_smmu_s2cr
*s2cr
= smmu
->s2crs
;
1102 u8 cbndx
= smmu_domain
->cfg
.cbndx
;
1103 enum arm_smmu_s2cr_type type
;
1106 if (smmu_domain
->stage
== ARM_SMMU_DOMAIN_BYPASS
)
1107 type
= S2CR_TYPE_BYPASS
;
1109 type
= S2CR_TYPE_TRANS
;
1111 for_each_cfg_sme(fwspec
, i
, idx
) {
1112 if (type
== s2cr
[idx
].type
&& cbndx
== s2cr
[idx
].cbndx
)
1115 s2cr
[idx
].type
= type
;
1116 s2cr
[idx
].privcfg
= S2CR_PRIVCFG_DEFAULT
;
1117 s2cr
[idx
].cbndx
= cbndx
;
1118 arm_smmu_write_s2cr(smmu
, idx
);
1123 static int arm_smmu_attach_dev(struct iommu_domain
*domain
, struct device
*dev
)
1126 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1127 struct arm_smmu_device
*smmu
;
1128 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
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 add_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 if (!fwspec
->iommu_priv
)
1145 smmu
= fwspec_smmu(fwspec
);
1147 ret
= arm_smmu_rpm_get(smmu
);
1151 /* Ensure that the domain is finalised */
1152 ret
= arm_smmu_init_domain_context(domain
, smmu
);
1157 * Sanity check the domain. We don't support domains across
1160 if (smmu_domain
->smmu
!= smmu
) {
1162 "cannot attach to SMMU %s whilst already attached to domain on SMMU %s\n",
1163 dev_name(smmu_domain
->smmu
->dev
), dev_name(smmu
->dev
));
1168 /* Looks ok, so add the device to the domain */
1169 ret
= arm_smmu_domain_add_master(smmu_domain
, fwspec
);
1172 * Setup an autosuspend delay to avoid bouncing runpm state.
1173 * Otherwise, if a driver for a suspended consumer device
1174 * unmaps buffers, it will runpm resume/suspend for each one.
1176 * For example, when used by a GPU device, when an application
1177 * or game exits, it can trigger unmapping 100s or 1000s of
1178 * buffers. With a runpm cycle for each buffer, that adds up
1179 * to 5-10sec worth of reprogramming the context bank, while
1180 * the system appears to be locked up to the user.
1182 pm_runtime_set_autosuspend_delay(smmu
->dev
, 20);
1183 pm_runtime_use_autosuspend(smmu
->dev
);
1186 arm_smmu_rpm_put(smmu
);
1190 static int arm_smmu_map(struct iommu_domain
*domain
, unsigned long iova
,
1191 phys_addr_t paddr
, size_t size
, int prot
, gfp_t gfp
)
1193 struct io_pgtable_ops
*ops
= to_smmu_domain(domain
)->pgtbl_ops
;
1194 struct arm_smmu_device
*smmu
= to_smmu_domain(domain
)->smmu
;
1200 arm_smmu_rpm_get(smmu
);
1201 ret
= ops
->map(ops
, iova
, paddr
, size
, prot
);
1202 arm_smmu_rpm_put(smmu
);
1207 static size_t arm_smmu_unmap(struct iommu_domain
*domain
, unsigned long iova
,
1208 size_t size
, struct iommu_iotlb_gather
*gather
)
1210 struct io_pgtable_ops
*ops
= to_smmu_domain(domain
)->pgtbl_ops
;
1211 struct arm_smmu_device
*smmu
= to_smmu_domain(domain
)->smmu
;
1217 arm_smmu_rpm_get(smmu
);
1218 ret
= ops
->unmap(ops
, iova
, size
, gather
);
1219 arm_smmu_rpm_put(smmu
);
1224 static void arm_smmu_flush_iotlb_all(struct iommu_domain
*domain
)
1226 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1227 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1229 if (smmu_domain
->flush_ops
) {
1230 arm_smmu_rpm_get(smmu
);
1231 smmu_domain
->flush_ops
->tlb_flush_all(smmu_domain
);
1232 arm_smmu_rpm_put(smmu
);
1236 static void arm_smmu_iotlb_sync(struct iommu_domain
*domain
,
1237 struct iommu_iotlb_gather
*gather
)
1239 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1240 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1245 arm_smmu_rpm_get(smmu
);
1246 if (smmu
->version
== ARM_SMMU_V2
||
1247 smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
)
1248 arm_smmu_tlb_sync_context(smmu_domain
);
1250 arm_smmu_tlb_sync_global(smmu
);
1251 arm_smmu_rpm_put(smmu
);
1254 static phys_addr_t
arm_smmu_iova_to_phys_hard(struct iommu_domain
*domain
,
1257 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1258 struct arm_smmu_device
*smmu
= smmu_domain
->smmu
;
1259 struct arm_smmu_cfg
*cfg
= &smmu_domain
->cfg
;
1260 struct io_pgtable_ops
*ops
= smmu_domain
->pgtbl_ops
;
1261 struct device
*dev
= smmu
->dev
;
1265 unsigned long va
, flags
;
1266 int ret
, idx
= cfg
->cbndx
;
1268 ret
= arm_smmu_rpm_get(smmu
);
1272 spin_lock_irqsave(&smmu_domain
->cb_lock
, flags
);
1273 va
= iova
& ~0xfffUL
;
1274 if (cfg
->fmt
== ARM_SMMU_CTX_FMT_AARCH64
)
1275 arm_smmu_cb_writeq(smmu
, idx
, ARM_SMMU_CB_ATS1PR
, va
);
1277 arm_smmu_cb_write(smmu
, idx
, ARM_SMMU_CB_ATS1PR
, va
);
1279 reg
= arm_smmu_page(smmu
, ARM_SMMU_CB(smmu
, idx
)) + ARM_SMMU_CB_ATSR
;
1280 if (readl_poll_timeout_atomic(reg
, tmp
, !(tmp
& ATSR_ACTIVE
), 5, 50)) {
1281 spin_unlock_irqrestore(&smmu_domain
->cb_lock
, flags
);
1283 "iova to phys timed out on %pad. Falling back to software table walk.\n",
1285 return ops
->iova_to_phys(ops
, iova
);
1288 phys
= arm_smmu_cb_readq(smmu
, idx
, ARM_SMMU_CB_PAR
);
1289 spin_unlock_irqrestore(&smmu_domain
->cb_lock
, flags
);
1290 if (phys
& CB_PAR_F
) {
1291 dev_err(dev
, "translation fault!\n");
1292 dev_err(dev
, "PAR = 0x%llx\n", phys
);
1296 arm_smmu_rpm_put(smmu
);
1298 return (phys
& GENMASK_ULL(39, 12)) | (iova
& 0xfff);
1301 static phys_addr_t
arm_smmu_iova_to_phys(struct iommu_domain
*domain
,
1304 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1305 struct io_pgtable_ops
*ops
= smmu_domain
->pgtbl_ops
;
1307 if (domain
->type
== IOMMU_DOMAIN_IDENTITY
)
1313 if (smmu_domain
->smmu
->features
& ARM_SMMU_FEAT_TRANS_OPS
&&
1314 smmu_domain
->stage
== ARM_SMMU_DOMAIN_S1
)
1315 return arm_smmu_iova_to_phys_hard(domain
, iova
);
1317 return ops
->iova_to_phys(ops
, iova
);
1320 static bool arm_smmu_capable(enum iommu_cap cap
)
1323 case IOMMU_CAP_CACHE_COHERENCY
:
1325 * Return true here as the SMMU can always send out coherent
1329 case IOMMU_CAP_NOEXEC
:
1337 struct arm_smmu_device
*arm_smmu_get_by_fwnode(struct fwnode_handle
*fwnode
)
1339 struct device
*dev
= driver_find_device_by_fwnode(&arm_smmu_driver
.driver
,
1342 return dev
? dev_get_drvdata(dev
) : NULL
;
1345 static int arm_smmu_add_device(struct device
*dev
)
1347 struct arm_smmu_device
*smmu
;
1348 struct arm_smmu_master_cfg
*cfg
;
1349 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1352 if (using_legacy_binding
) {
1353 ret
= arm_smmu_register_legacy_master(dev
, &smmu
);
1356 * If dev->iommu_fwspec is initally NULL, arm_smmu_register_legacy_master()
1357 * will allocate/initialise a new one. Thus we need to update fwspec for
1360 fwspec
= dev_iommu_fwspec_get(dev
);
1363 } else if (fwspec
&& fwspec
->ops
== &arm_smmu_ops
) {
1364 smmu
= arm_smmu_get_by_fwnode(fwspec
->iommu_fwnode
);
1370 for (i
= 0; i
< fwspec
->num_ids
; i
++) {
1371 u16 sid
= FIELD_GET(SMR_ID
, fwspec
->ids
[i
]);
1372 u16 mask
= FIELD_GET(SMR_MASK
, fwspec
->ids
[i
]);
1374 if (sid
& ~smmu
->streamid_mask
) {
1375 dev_err(dev
, "stream ID 0x%x out of range for SMMU (0x%x)\n",
1376 sid
, smmu
->streamid_mask
);
1379 if (mask
& ~smmu
->smr_mask_mask
) {
1380 dev_err(dev
, "SMR mask 0x%x out of range for SMMU (0x%x)\n",
1381 mask
, smmu
->smr_mask_mask
);
1387 cfg
= kzalloc(offsetof(struct arm_smmu_master_cfg
, smendx
[i
]),
1393 fwspec
->iommu_priv
= cfg
;
1395 cfg
->smendx
[i
] = INVALID_SMENDX
;
1397 ret
= arm_smmu_rpm_get(smmu
);
1401 ret
= arm_smmu_master_alloc_smes(dev
);
1402 arm_smmu_rpm_put(smmu
);
1407 iommu_device_link(&smmu
->iommu
, dev
);
1409 device_link_add(dev
, smmu
->dev
,
1410 DL_FLAG_PM_RUNTIME
| DL_FLAG_AUTOREMOVE_SUPPLIER
);
1417 iommu_fwspec_free(dev
);
1421 static void arm_smmu_remove_device(struct device
*dev
)
1423 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1424 struct arm_smmu_master_cfg
*cfg
;
1425 struct arm_smmu_device
*smmu
;
1428 if (!fwspec
|| fwspec
->ops
!= &arm_smmu_ops
)
1431 cfg
= fwspec
->iommu_priv
;
1434 ret
= arm_smmu_rpm_get(smmu
);
1438 iommu_device_unlink(&smmu
->iommu
, dev
);
1439 arm_smmu_master_free_smes(fwspec
);
1441 arm_smmu_rpm_put(smmu
);
1443 iommu_group_remove_device(dev
);
1444 kfree(fwspec
->iommu_priv
);
1445 iommu_fwspec_free(dev
);
1448 static struct iommu_group
*arm_smmu_device_group(struct device
*dev
)
1450 struct iommu_fwspec
*fwspec
= dev_iommu_fwspec_get(dev
);
1451 struct arm_smmu_device
*smmu
= fwspec_smmu(fwspec
);
1452 struct iommu_group
*group
= NULL
;
1455 for_each_cfg_sme(fwspec
, i
, idx
) {
1456 if (group
&& smmu
->s2crs
[idx
].group
&&
1457 group
!= smmu
->s2crs
[idx
].group
)
1458 return ERR_PTR(-EINVAL
);
1460 group
= smmu
->s2crs
[idx
].group
;
1464 return iommu_group_ref_get(group
);
1466 if (dev_is_pci(dev
))
1467 group
= pci_device_group(dev
);
1468 else if (dev_is_fsl_mc(dev
))
1469 group
= fsl_mc_device_group(dev
);
1471 group
= generic_device_group(dev
);
1476 static int arm_smmu_domain_get_attr(struct iommu_domain
*domain
,
1477 enum iommu_attr attr
, void *data
)
1479 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1481 switch(domain
->type
) {
1482 case IOMMU_DOMAIN_UNMANAGED
:
1484 case DOMAIN_ATTR_NESTING
:
1485 *(int *)data
= (smmu_domain
->stage
== ARM_SMMU_DOMAIN_NESTED
);
1491 case IOMMU_DOMAIN_DMA
:
1493 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE
:
1494 *(int *)data
= smmu_domain
->non_strict
;
1505 static int arm_smmu_domain_set_attr(struct iommu_domain
*domain
,
1506 enum iommu_attr attr
, void *data
)
1509 struct arm_smmu_domain
*smmu_domain
= to_smmu_domain(domain
);
1511 mutex_lock(&smmu_domain
->init_mutex
);
1513 switch(domain
->type
) {
1514 case IOMMU_DOMAIN_UNMANAGED
:
1516 case DOMAIN_ATTR_NESTING
:
1517 if (smmu_domain
->smmu
) {
1523 smmu_domain
->stage
= ARM_SMMU_DOMAIN_NESTED
;
1525 smmu_domain
->stage
= ARM_SMMU_DOMAIN_S1
;
1531 case IOMMU_DOMAIN_DMA
:
1533 case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE
:
1534 smmu_domain
->non_strict
= *(int *)data
;
1544 mutex_unlock(&smmu_domain
->init_mutex
);
1548 static int arm_smmu_of_xlate(struct device
*dev
, struct of_phandle_args
*args
)
1552 if (args
->args_count
> 0)
1553 fwid
|= FIELD_PREP(SMR_ID
, args
->args
[0]);
1555 if (args
->args_count
> 1)
1556 fwid
|= FIELD_PREP(SMR_MASK
, args
->args
[1]);
1557 else if (!of_property_read_u32(args
->np
, "stream-match-mask", &mask
))
1558 fwid
|= FIELD_PREP(SMR_MASK
, mask
);
1560 return iommu_fwspec_add_ids(dev
, &fwid
, 1);
1563 static void arm_smmu_get_resv_regions(struct device
*dev
,
1564 struct list_head
*head
)
1566 struct iommu_resv_region
*region
;
1567 int prot
= IOMMU_WRITE
| IOMMU_NOEXEC
| IOMMU_MMIO
;
1569 region
= iommu_alloc_resv_region(MSI_IOVA_BASE
, MSI_IOVA_LENGTH
,
1570 prot
, IOMMU_RESV_SW_MSI
);
1574 list_add_tail(®ion
->list
, head
);
1576 iommu_dma_get_resv_regions(dev
, head
);
1579 static void arm_smmu_put_resv_regions(struct device
*dev
,
1580 struct list_head
*head
)
1582 struct iommu_resv_region
*entry
, *next
;
1584 list_for_each_entry_safe(entry
, next
, head
, list
)
1588 static struct iommu_ops arm_smmu_ops
= {
1589 .capable
= arm_smmu_capable
,
1590 .domain_alloc
= arm_smmu_domain_alloc
,
1591 .domain_free
= arm_smmu_domain_free
,
1592 .attach_dev
= arm_smmu_attach_dev
,
1593 .map
= arm_smmu_map
,
1594 .unmap
= arm_smmu_unmap
,
1595 .flush_iotlb_all
= arm_smmu_flush_iotlb_all
,
1596 .iotlb_sync
= arm_smmu_iotlb_sync
,
1597 .iova_to_phys
= arm_smmu_iova_to_phys
,
1598 .add_device
= arm_smmu_add_device
,
1599 .remove_device
= arm_smmu_remove_device
,
1600 .device_group
= arm_smmu_device_group
,
1601 .domain_get_attr
= arm_smmu_domain_get_attr
,
1602 .domain_set_attr
= arm_smmu_domain_set_attr
,
1603 .of_xlate
= arm_smmu_of_xlate
,
1604 .get_resv_regions
= arm_smmu_get_resv_regions
,
1605 .put_resv_regions
= arm_smmu_put_resv_regions
,
1606 .pgsize_bitmap
= -1UL, /* Restricted during device attach */
1609 static void arm_smmu_device_reset(struct arm_smmu_device
*smmu
)
1614 /* clear global FSR */
1615 reg
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_sGFSR
);
1616 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_sGFSR
, reg
);
1619 * Reset stream mapping groups: Initial values mark all SMRn as
1620 * invalid and all S2CRn as bypass unless overridden.
1622 for (i
= 0; i
< smmu
->num_mapping_groups
; ++i
)
1623 arm_smmu_write_sme(smmu
, i
);
1625 /* Make sure all context banks are disabled and clear CB_FSR */
1626 for (i
= 0; i
< smmu
->num_context_banks
; ++i
) {
1627 arm_smmu_write_context_bank(smmu
, i
);
1628 arm_smmu_cb_write(smmu
, i
, ARM_SMMU_CB_FSR
, FSR_FAULT
);
1631 /* Invalidate the TLB, just in case */
1632 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_TLBIALLH
, QCOM_DUMMY_VAL
);
1633 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_TLBIALLNSNH
, QCOM_DUMMY_VAL
);
1635 reg
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_sCR0
);
1637 /* Enable fault reporting */
1638 reg
|= (sCR0_GFRE
| sCR0_GFIE
| sCR0_GCFGFRE
| sCR0_GCFGFIE
);
1640 /* Disable TLB broadcasting. */
1641 reg
|= (sCR0_VMIDPNE
| sCR0_PTM
);
1643 /* Enable client access, handling unmatched streams as appropriate */
1644 reg
&= ~sCR0_CLIENTPD
;
1648 reg
&= ~sCR0_USFCFG
;
1650 /* Disable forced broadcasting */
1653 /* Don't upgrade barriers */
1656 if (smmu
->features
& ARM_SMMU_FEAT_VMID16
)
1657 reg
|= sCR0_VMID16EN
;
1659 if (smmu
->features
& ARM_SMMU_FEAT_EXIDS
)
1660 reg
|= sCR0_EXIDENABLE
;
1662 if (smmu
->impl
&& smmu
->impl
->reset
)
1663 smmu
->impl
->reset(smmu
);
1665 /* Push the button */
1666 arm_smmu_tlb_sync_global(smmu
);
1667 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_sCR0
, reg
);
1670 static int arm_smmu_id_size_to_bits(int size
)
1689 static int arm_smmu_device_cfg_probe(struct arm_smmu_device
*smmu
)
1693 bool cttw_reg
, cttw_fw
= smmu
->features
& ARM_SMMU_FEAT_COHERENT_WALK
;
1696 dev_notice(smmu
->dev
, "probing hardware configuration...\n");
1697 dev_notice(smmu
->dev
, "SMMUv%d with:\n",
1698 smmu
->version
== ARM_SMMU_V2
? 2 : 1);
1701 id
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_ID0
);
1703 /* Restrict available stages based on module parameter */
1704 if (force_stage
== 1)
1705 id
&= ~(ID0_S2TS
| ID0_NTS
);
1706 else if (force_stage
== 2)
1707 id
&= ~(ID0_S1TS
| ID0_NTS
);
1709 if (id
& ID0_S1TS
) {
1710 smmu
->features
|= ARM_SMMU_FEAT_TRANS_S1
;
1711 dev_notice(smmu
->dev
, "\tstage 1 translation\n");
1714 if (id
& ID0_S2TS
) {
1715 smmu
->features
|= ARM_SMMU_FEAT_TRANS_S2
;
1716 dev_notice(smmu
->dev
, "\tstage 2 translation\n");
1720 smmu
->features
|= ARM_SMMU_FEAT_TRANS_NESTED
;
1721 dev_notice(smmu
->dev
, "\tnested translation\n");
1724 if (!(smmu
->features
&
1725 (ARM_SMMU_FEAT_TRANS_S1
| ARM_SMMU_FEAT_TRANS_S2
))) {
1726 dev_err(smmu
->dev
, "\tno translation support!\n");
1730 if ((id
& ID0_S1TS
) &&
1731 ((smmu
->version
< ARM_SMMU_V2
) || !(id
& ID0_ATOSNS
))) {
1732 smmu
->features
|= ARM_SMMU_FEAT_TRANS_OPS
;
1733 dev_notice(smmu
->dev
, "\taddress translation ops\n");
1737 * In order for DMA API calls to work properly, we must defer to what
1738 * the FW says about coherency, regardless of what the hardware claims.
1739 * Fortunately, this also opens up a workaround for systems where the
1740 * ID register value has ended up configured incorrectly.
1742 cttw_reg
= !!(id
& ID0_CTTW
);
1743 if (cttw_fw
|| cttw_reg
)
1744 dev_notice(smmu
->dev
, "\t%scoherent table walk\n",
1745 cttw_fw
? "" : "non-");
1746 if (cttw_fw
!= cttw_reg
)
1747 dev_notice(smmu
->dev
,
1748 "\t(IDR0.CTTW overridden by FW configuration)\n");
1750 /* Max. number of entries we have for stream matching/indexing */
1751 if (smmu
->version
== ARM_SMMU_V2
&& id
& ID0_EXIDS
) {
1752 smmu
->features
|= ARM_SMMU_FEAT_EXIDS
;
1755 size
= 1 << FIELD_GET(ID0_NUMSIDB
, id
);
1757 smmu
->streamid_mask
= size
- 1;
1759 smmu
->features
|= ARM_SMMU_FEAT_STREAM_MATCH
;
1760 size
= FIELD_GET(ID0_NUMSMRG
, id
);
1763 "stream-matching supported, but no SMRs present!\n");
1767 /* Zero-initialised to mark as invalid */
1768 smmu
->smrs
= devm_kcalloc(smmu
->dev
, size
, sizeof(*smmu
->smrs
),
1773 dev_notice(smmu
->dev
,
1774 "\tstream matching with %u register groups", size
);
1776 /* s2cr->type == 0 means translation, so initialise explicitly */
1777 smmu
->s2crs
= devm_kmalloc_array(smmu
->dev
, size
, sizeof(*smmu
->s2crs
),
1781 for (i
= 0; i
< size
; i
++)
1782 smmu
->s2crs
[i
] = s2cr_init_val
;
1784 smmu
->num_mapping_groups
= size
;
1785 mutex_init(&smmu
->stream_map_mutex
);
1786 spin_lock_init(&smmu
->global_sync_lock
);
1788 if (smmu
->version
< ARM_SMMU_V2
|| !(id
& ID0_PTFS_NO_AARCH32
)) {
1789 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH32_L
;
1790 if (!(id
& ID0_PTFS_NO_AARCH32S
))
1791 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH32_S
;
1795 id
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_ID1
);
1796 smmu
->pgshift
= (id
& ID1_PAGESIZE
) ? 16 : 12;
1798 /* Check for size mismatch of SMMU address space from mapped region */
1799 size
= 1 << (FIELD_GET(ID1_NUMPAGENDXB
, id
) + 1);
1800 if (smmu
->numpage
!= 2 * size
<< smmu
->pgshift
)
1802 "SMMU address space size (0x%x) differs from mapped region size (0x%x)!\n",
1803 2 * size
<< smmu
->pgshift
, smmu
->numpage
);
1804 /* Now properly encode NUMPAGE to subsequently derive SMMU_CB_BASE */
1805 smmu
->numpage
= size
;
1807 smmu
->num_s2_context_banks
= FIELD_GET(ID1_NUMS2CB
, id
);
1808 smmu
->num_context_banks
= FIELD_GET(ID1_NUMCB
, id
);
1809 if (smmu
->num_s2_context_banks
> smmu
->num_context_banks
) {
1810 dev_err(smmu
->dev
, "impossible number of S2 context banks!\n");
1813 dev_notice(smmu
->dev
, "\t%u context banks (%u stage-2 only)\n",
1814 smmu
->num_context_banks
, smmu
->num_s2_context_banks
);
1815 smmu
->cbs
= devm_kcalloc(smmu
->dev
, smmu
->num_context_banks
,
1816 sizeof(*smmu
->cbs
), GFP_KERNEL
);
1821 id
= arm_smmu_gr0_read(smmu
, ARM_SMMU_GR0_ID2
);
1822 size
= arm_smmu_id_size_to_bits(FIELD_GET(ID2_IAS
, id
));
1823 smmu
->ipa_size
= size
;
1825 /* The output mask is also applied for bypass */
1826 size
= arm_smmu_id_size_to_bits(FIELD_GET(ID2_OAS
, id
));
1827 smmu
->pa_size
= size
;
1829 if (id
& ID2_VMID16
)
1830 smmu
->features
|= ARM_SMMU_FEAT_VMID16
;
1833 * What the page table walker can address actually depends on which
1834 * descriptor format is in use, but since a) we don't know that yet,
1835 * and b) it can vary per context bank, this will have to do...
1837 if (dma_set_mask_and_coherent(smmu
->dev
, DMA_BIT_MASK(size
)))
1839 "failed to set DMA mask for table walker\n");
1841 if (smmu
->version
< ARM_SMMU_V2
) {
1842 smmu
->va_size
= smmu
->ipa_size
;
1843 if (smmu
->version
== ARM_SMMU_V1_64K
)
1844 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_64K
;
1846 size
= FIELD_GET(ID2_UBS
, id
);
1847 smmu
->va_size
= arm_smmu_id_size_to_bits(size
);
1848 if (id
& ID2_PTFS_4K
)
1849 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_4K
;
1850 if (id
& ID2_PTFS_16K
)
1851 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_16K
;
1852 if (id
& ID2_PTFS_64K
)
1853 smmu
->features
|= ARM_SMMU_FEAT_FMT_AARCH64_64K
;
1856 /* Now we've corralled the various formats, what'll it do? */
1857 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH32_S
)
1858 smmu
->pgsize_bitmap
|= SZ_4K
| SZ_64K
| SZ_1M
| SZ_16M
;
1859 if (smmu
->features
&
1860 (ARM_SMMU_FEAT_FMT_AARCH32_L
| ARM_SMMU_FEAT_FMT_AARCH64_4K
))
1861 smmu
->pgsize_bitmap
|= SZ_4K
| SZ_2M
| SZ_1G
;
1862 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH64_16K
)
1863 smmu
->pgsize_bitmap
|= SZ_16K
| SZ_32M
;
1864 if (smmu
->features
& ARM_SMMU_FEAT_FMT_AARCH64_64K
)
1865 smmu
->pgsize_bitmap
|= SZ_64K
| SZ_512M
;
1867 if (arm_smmu_ops
.pgsize_bitmap
== -1UL)
1868 arm_smmu_ops
.pgsize_bitmap
= smmu
->pgsize_bitmap
;
1870 arm_smmu_ops
.pgsize_bitmap
|= smmu
->pgsize_bitmap
;
1871 dev_notice(smmu
->dev
, "\tSupported page sizes: 0x%08lx\n",
1872 smmu
->pgsize_bitmap
);
1875 if (smmu
->features
& ARM_SMMU_FEAT_TRANS_S1
)
1876 dev_notice(smmu
->dev
, "\tStage-1: %lu-bit VA -> %lu-bit IPA\n",
1877 smmu
->va_size
, smmu
->ipa_size
);
1879 if (smmu
->features
& ARM_SMMU_FEAT_TRANS_S2
)
1880 dev_notice(smmu
->dev
, "\tStage-2: %lu-bit IPA -> %lu-bit PA\n",
1881 smmu
->ipa_size
, smmu
->pa_size
);
1883 if (smmu
->impl
&& smmu
->impl
->cfg_probe
)
1884 return smmu
->impl
->cfg_probe(smmu
);
1889 struct arm_smmu_match_data
{
1890 enum arm_smmu_arch_version version
;
1891 enum arm_smmu_implementation model
;
1894 #define ARM_SMMU_MATCH_DATA(name, ver, imp) \
1895 static const struct arm_smmu_match_data name = { .version = ver, .model = imp }
1897 ARM_SMMU_MATCH_DATA(smmu_generic_v1
, ARM_SMMU_V1
, GENERIC_SMMU
);
1898 ARM_SMMU_MATCH_DATA(smmu_generic_v2
, ARM_SMMU_V2
, GENERIC_SMMU
);
1899 ARM_SMMU_MATCH_DATA(arm_mmu401
, ARM_SMMU_V1_64K
, GENERIC_SMMU
);
1900 ARM_SMMU_MATCH_DATA(arm_mmu500
, ARM_SMMU_V2
, ARM_MMU500
);
1901 ARM_SMMU_MATCH_DATA(cavium_smmuv2
, ARM_SMMU_V2
, CAVIUM_SMMUV2
);
1902 ARM_SMMU_MATCH_DATA(qcom_smmuv2
, ARM_SMMU_V2
, QCOM_SMMUV2
);
1904 static const struct of_device_id arm_smmu_of_match
[] = {
1905 { .compatible
= "arm,smmu-v1", .data
= &smmu_generic_v1
},
1906 { .compatible
= "arm,smmu-v2", .data
= &smmu_generic_v2
},
1907 { .compatible
= "arm,mmu-400", .data
= &smmu_generic_v1
},
1908 { .compatible
= "arm,mmu-401", .data
= &arm_mmu401
},
1909 { .compatible
= "arm,mmu-500", .data
= &arm_mmu500
},
1910 { .compatible
= "cavium,smmu-v2", .data
= &cavium_smmuv2
},
1911 { .compatible
= "qcom,smmu-v2", .data
= &qcom_smmuv2
},
1916 static int acpi_smmu_get_data(u32 model
, struct arm_smmu_device
*smmu
)
1921 case ACPI_IORT_SMMU_V1
:
1922 case ACPI_IORT_SMMU_CORELINK_MMU400
:
1923 smmu
->version
= ARM_SMMU_V1
;
1924 smmu
->model
= GENERIC_SMMU
;
1926 case ACPI_IORT_SMMU_CORELINK_MMU401
:
1927 smmu
->version
= ARM_SMMU_V1_64K
;
1928 smmu
->model
= GENERIC_SMMU
;
1930 case ACPI_IORT_SMMU_V2
:
1931 smmu
->version
= ARM_SMMU_V2
;
1932 smmu
->model
= GENERIC_SMMU
;
1934 case ACPI_IORT_SMMU_CORELINK_MMU500
:
1935 smmu
->version
= ARM_SMMU_V2
;
1936 smmu
->model
= ARM_MMU500
;
1938 case ACPI_IORT_SMMU_CAVIUM_THUNDERX
:
1939 smmu
->version
= ARM_SMMU_V2
;
1940 smmu
->model
= CAVIUM_SMMUV2
;
1949 static int arm_smmu_device_acpi_probe(struct platform_device
*pdev
,
1950 struct arm_smmu_device
*smmu
)
1952 struct device
*dev
= smmu
->dev
;
1953 struct acpi_iort_node
*node
=
1954 *(struct acpi_iort_node
**)dev_get_platdata(dev
);
1955 struct acpi_iort_smmu
*iort_smmu
;
1958 /* Retrieve SMMU1/2 specific data */
1959 iort_smmu
= (struct acpi_iort_smmu
*)node
->node_data
;
1961 ret
= acpi_smmu_get_data(iort_smmu
->model
, smmu
);
1965 /* Ignore the configuration access interrupt */
1966 smmu
->num_global_irqs
= 1;
1968 if (iort_smmu
->flags
& ACPI_IORT_SMMU_COHERENT_WALK
)
1969 smmu
->features
|= ARM_SMMU_FEAT_COHERENT_WALK
;
1974 static inline int arm_smmu_device_acpi_probe(struct platform_device
*pdev
,
1975 struct arm_smmu_device
*smmu
)
1981 static int arm_smmu_device_dt_probe(struct platform_device
*pdev
,
1982 struct arm_smmu_device
*smmu
)
1984 const struct arm_smmu_match_data
*data
;
1985 struct device
*dev
= &pdev
->dev
;
1986 bool legacy_binding
;
1988 if (of_property_read_u32(dev
->of_node
, "#global-interrupts",
1989 &smmu
->num_global_irqs
)) {
1990 dev_err(dev
, "missing #global-interrupts property\n");
1994 data
= of_device_get_match_data(dev
);
1995 smmu
->version
= data
->version
;
1996 smmu
->model
= data
->model
;
1998 legacy_binding
= of_find_property(dev
->of_node
, "mmu-masters", NULL
);
1999 if (legacy_binding
&& !using_generic_binding
) {
2000 if (!using_legacy_binding
)
2001 pr_notice("deprecated \"mmu-masters\" DT property in use; DMA API support unavailable\n");
2002 using_legacy_binding
= true;
2003 } else if (!legacy_binding
&& !using_legacy_binding
) {
2004 using_generic_binding
= true;
2006 dev_err(dev
, "not probing due to mismatched DT properties\n");
2010 if (of_dma_is_coherent(dev
->of_node
))
2011 smmu
->features
|= ARM_SMMU_FEAT_COHERENT_WALK
;
2016 static void arm_smmu_bus_init(void)
2018 /* Oh, for a proper bus abstraction */
2019 if (!iommu_present(&platform_bus_type
))
2020 bus_set_iommu(&platform_bus_type
, &arm_smmu_ops
);
2021 #ifdef CONFIG_ARM_AMBA
2022 if (!iommu_present(&amba_bustype
))
2023 bus_set_iommu(&amba_bustype
, &arm_smmu_ops
);
2026 if (!iommu_present(&pci_bus_type
)) {
2028 bus_set_iommu(&pci_bus_type
, &arm_smmu_ops
);
2031 #ifdef CONFIG_FSL_MC_BUS
2032 if (!iommu_present(&fsl_mc_bus_type
))
2033 bus_set_iommu(&fsl_mc_bus_type
, &arm_smmu_ops
);
2037 static int arm_smmu_device_probe(struct platform_device
*pdev
)
2039 struct resource
*res
;
2040 resource_size_t ioaddr
;
2041 struct arm_smmu_device
*smmu
;
2042 struct device
*dev
= &pdev
->dev
;
2043 int num_irqs
, i
, err
;
2045 smmu
= devm_kzalloc(dev
, sizeof(*smmu
), GFP_KERNEL
);
2047 dev_err(dev
, "failed to allocate arm_smmu_device\n");
2053 err
= arm_smmu_device_dt_probe(pdev
, smmu
);
2055 err
= arm_smmu_device_acpi_probe(pdev
, smmu
);
2060 smmu
= arm_smmu_impl_init(smmu
);
2062 return PTR_ERR(smmu
);
2064 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2065 ioaddr
= res
->start
;
2066 smmu
->base
= devm_ioremap_resource(dev
, res
);
2067 if (IS_ERR(smmu
->base
))
2068 return PTR_ERR(smmu
->base
);
2070 * The resource size should effectively match the value of SMMU_TOP;
2071 * stash that temporarily until we know PAGESIZE to validate it with.
2073 smmu
->numpage
= resource_size(res
);
2076 while ((res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, num_irqs
))) {
2078 if (num_irqs
> smmu
->num_global_irqs
)
2079 smmu
->num_context_irqs
++;
2082 if (!smmu
->num_context_irqs
) {
2083 dev_err(dev
, "found %d interrupts but expected at least %d\n",
2084 num_irqs
, smmu
->num_global_irqs
+ 1);
2088 smmu
->irqs
= devm_kcalloc(dev
, num_irqs
, sizeof(*smmu
->irqs
),
2091 dev_err(dev
, "failed to allocate %d irqs\n", num_irqs
);
2095 for (i
= 0; i
< num_irqs
; ++i
) {
2096 int irq
= platform_get_irq(pdev
, i
);
2100 smmu
->irqs
[i
] = irq
;
2103 err
= devm_clk_bulk_get_all(dev
, &smmu
->clks
);
2105 dev_err(dev
, "failed to get clocks %d\n", err
);
2108 smmu
->num_clks
= err
;
2110 err
= clk_bulk_prepare_enable(smmu
->num_clks
, smmu
->clks
);
2114 err
= arm_smmu_device_cfg_probe(smmu
);
2118 if (smmu
->version
== ARM_SMMU_V2
) {
2119 if (smmu
->num_context_banks
> smmu
->num_context_irqs
) {
2121 "found only %d context irq(s) but %d required\n",
2122 smmu
->num_context_irqs
, smmu
->num_context_banks
);
2126 /* Ignore superfluous interrupts */
2127 smmu
->num_context_irqs
= smmu
->num_context_banks
;
2130 for (i
= 0; i
< smmu
->num_global_irqs
; ++i
) {
2131 err
= devm_request_irq(smmu
->dev
, smmu
->irqs
[i
],
2132 arm_smmu_global_fault
,
2134 "arm-smmu global fault",
2137 dev_err(dev
, "failed to request global IRQ %d (%u)\n",
2143 err
= iommu_device_sysfs_add(&smmu
->iommu
, smmu
->dev
, NULL
,
2144 "smmu.%pa", &ioaddr
);
2146 dev_err(dev
, "Failed to register iommu in sysfs\n");
2150 iommu_device_set_ops(&smmu
->iommu
, &arm_smmu_ops
);
2151 iommu_device_set_fwnode(&smmu
->iommu
, dev
->fwnode
);
2153 err
= iommu_device_register(&smmu
->iommu
);
2155 dev_err(dev
, "Failed to register iommu\n");
2159 platform_set_drvdata(pdev
, smmu
);
2160 arm_smmu_device_reset(smmu
);
2161 arm_smmu_test_smr_masks(smmu
);
2164 * We want to avoid touching dev->power.lock in fastpaths unless
2165 * it's really going to do something useful - pm_runtime_enabled()
2166 * can serve as an ideal proxy for that decision. So, conditionally
2167 * enable pm_runtime.
2169 if (dev
->pm_domain
) {
2170 pm_runtime_set_active(dev
);
2171 pm_runtime_enable(dev
);
2175 * For ACPI and generic DT bindings, an SMMU will be probed before
2176 * any device which might need it, so we want the bus ops in place
2177 * ready to handle default domain setup as soon as any SMMU exists.
2179 if (!using_legacy_binding
)
2180 arm_smmu_bus_init();
2186 * With the legacy DT binding in play, though, we have no guarantees about
2187 * probe order, but then we're also not doing default domains, so we can
2188 * delay setting bus ops until we're sure every possible SMMU is ready,
2189 * and that way ensure that no add_device() calls get missed.
2191 static int arm_smmu_legacy_bus_init(void)
2193 if (using_legacy_binding
)
2194 arm_smmu_bus_init();
2197 device_initcall_sync(arm_smmu_legacy_bus_init
);
2199 static void arm_smmu_device_shutdown(struct platform_device
*pdev
)
2201 struct arm_smmu_device
*smmu
= platform_get_drvdata(pdev
);
2206 if (!bitmap_empty(smmu
->context_map
, ARM_SMMU_MAX_CBS
))
2207 dev_err(&pdev
->dev
, "removing device with active domains!\n");
2209 arm_smmu_rpm_get(smmu
);
2210 /* Turn the thing off */
2211 arm_smmu_gr0_write(smmu
, ARM_SMMU_GR0_sCR0
, sCR0_CLIENTPD
);
2212 arm_smmu_rpm_put(smmu
);
2214 if (pm_runtime_enabled(smmu
->dev
))
2215 pm_runtime_force_suspend(smmu
->dev
);
2217 clk_bulk_disable(smmu
->num_clks
, smmu
->clks
);
2219 clk_bulk_unprepare(smmu
->num_clks
, smmu
->clks
);
2222 static int __maybe_unused
arm_smmu_runtime_resume(struct device
*dev
)
2224 struct arm_smmu_device
*smmu
= dev_get_drvdata(dev
);
2227 ret
= clk_bulk_enable(smmu
->num_clks
, smmu
->clks
);
2231 arm_smmu_device_reset(smmu
);
2236 static int __maybe_unused
arm_smmu_runtime_suspend(struct device
*dev
)
2238 struct arm_smmu_device
*smmu
= dev_get_drvdata(dev
);
2240 clk_bulk_disable(smmu
->num_clks
, smmu
->clks
);
2245 static int __maybe_unused
arm_smmu_pm_resume(struct device
*dev
)
2247 if (pm_runtime_suspended(dev
))
2250 return arm_smmu_runtime_resume(dev
);
2253 static int __maybe_unused
arm_smmu_pm_suspend(struct device
*dev
)
2255 if (pm_runtime_suspended(dev
))
2258 return arm_smmu_runtime_suspend(dev
);
2261 static const struct dev_pm_ops arm_smmu_pm_ops
= {
2262 SET_SYSTEM_SLEEP_PM_OPS(arm_smmu_pm_suspend
, arm_smmu_pm_resume
)
2263 SET_RUNTIME_PM_OPS(arm_smmu_runtime_suspend
,
2264 arm_smmu_runtime_resume
, NULL
)
2267 static struct platform_driver arm_smmu_driver
= {
2270 .of_match_table
= of_match_ptr(arm_smmu_of_match
),
2271 .pm
= &arm_smmu_pm_ops
,
2272 .suppress_bind_attrs
= true,
2274 .probe
= arm_smmu_device_probe
,
2275 .shutdown
= arm_smmu_device_shutdown
,
2277 builtin_platform_driver(arm_smmu_driver
);