1 // SPDX-License-Identifier: GPL-2.0
3 * Implementation of the IOMMU SVA API for the ARM SMMUv3
7 #include <linux/mmu_context.h>
8 #include <linux/slab.h>
10 #include "arm-smmu-v3.h"
11 #include "../../io-pgtable-arm.h"
14 * Check if the CPU ASID is available on the SMMU side. If a private context
15 * descriptor is using it, try to replace it.
17 static struct arm_smmu_ctx_desc *
18 arm_smmu_share_asid(struct mm_struct *mm, u16 asid)
22 struct arm_smmu_ctx_desc *cd;
23 struct arm_smmu_device *smmu;
24 struct arm_smmu_domain *smmu_domain;
26 cd = xa_load(&arm_smmu_asid_xa, asid);
31 if (WARN_ON(cd->mm != mm))
32 return ERR_PTR(-EINVAL);
33 /* All devices bound to this mm use the same cd struct. */
34 refcount_inc(&cd->refs);
38 smmu_domain = container_of(cd, struct arm_smmu_domain, s1_cfg.cd);
39 smmu = smmu_domain->smmu;
41 ret = xa_alloc(&arm_smmu_asid_xa, &new_asid, cd,
42 XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL);
44 return ERR_PTR(-ENOSPC);
46 * Race with unmap: TLB invalidations will start targeting the new ASID,
47 * which isn't assigned yet. We'll do an invalidate-all on the old ASID
48 * later, so it doesn't matter.
52 * Update ASID and invalidate CD in all associated masters. There will
53 * be some overlap between use of both ASIDs, until we invalidate the
56 arm_smmu_write_ctx_desc(smmu_domain, 0, cd);
58 /* Invalidate TLB entries previously associated with that context */
59 arm_smmu_tlb_inv_asid(smmu, asid);
61 xa_erase(&arm_smmu_asid_xa, asid);
66 static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
71 struct arm_smmu_ctx_desc *cd;
72 struct arm_smmu_ctx_desc *ret = NULL;
74 asid = arm64_mm_context_get(mm);
76 return ERR_PTR(-ESRCH);
78 cd = kzalloc(sizeof(*cd), GFP_KERNEL);
84 refcount_set(&cd->refs, 1);
86 mutex_lock(&arm_smmu_asid_lock);
87 ret = arm_smmu_share_asid(mm, asid);
89 mutex_unlock(&arm_smmu_asid_lock);
93 err = xa_insert(&arm_smmu_asid_xa, asid, cd, GFP_KERNEL);
94 mutex_unlock(&arm_smmu_asid_lock);
99 tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, 64ULL - vabits_actual) |
100 FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, ARM_LPAE_TCR_RGN_WBWA) |
101 FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, ARM_LPAE_TCR_RGN_WBWA) |
102 FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS) |
103 CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64;
107 tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_4K);
110 tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_16K);
113 tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_64K);
121 reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
122 par = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_PARANGE_SHIFT);
123 tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par);
125 cd->ttbr = virt_to_phys(mm->pgd);
128 * MAIR value is pretty much constant and global, so we can just get it
129 * from the current CPU register
131 cd->mair = read_sysreg(mair_el1);
138 arm_smmu_free_asid(cd);
142 arm64_mm_context_put(mm);
143 return err < 0 ? ERR_PTR(err) : ret;
147 static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd)
149 if (arm_smmu_free_asid(cd)) {
151 arm64_mm_context_put(cd->mm);
156 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
158 unsigned long reg, fld;
160 unsigned long asid_bits;
161 u32 feat_mask = ARM_SMMU_FEAT_BTM | ARM_SMMU_FEAT_COHERENCY;
163 if (vabits_actual == 52)
164 feat_mask |= ARM_SMMU_FEAT_VAX;
166 if ((smmu->features & feat_mask) != feat_mask)
169 if (!(smmu->pgsize_bitmap & PAGE_SIZE))
173 * Get the smallest PA size of all CPUs (sanitized by cpufeature). We're
174 * not even pretending to support AArch32 here. Abort if the MMU outputs
175 * addresses larger than what we support.
177 reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
178 fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_PARANGE_SHIFT);
179 oas = id_aa64mmfr0_parange_to_phys_shift(fld);
183 /* We can support bigger ASIDs than the CPU, but not smaller */
184 fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_ASID_SHIFT);
185 asid_bits = fld ? 16 : 8;
186 if (smmu->asid_bits < asid_bits)
190 * See max_pinned_asids in arch/arm64/mm/context.c. The following is
191 * generally the maximum number of bindable processes.
193 if (arm64_kernel_unmapped_at_el0())
195 dev_dbg(smmu->dev, "%d shared contexts\n", (1 << asid_bits) -
196 num_possible_cpus() - 2);