xref: /linux/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
13f1ce8e8SJean-Philippe Brucker // SPDX-License-Identifier: GPL-2.0
23f1ce8e8SJean-Philippe Brucker /*
33f1ce8e8SJean-Philippe Brucker  * Implementation of the IOMMU SVA API for the ARM SMMUv3
43f1ce8e8SJean-Philippe Brucker  */
53f1ce8e8SJean-Philippe Brucker 
63f1ce8e8SJean-Philippe Brucker #include <linux/mm.h>
73f1ce8e8SJean-Philippe Brucker #include <linux/mmu_context.h>
832784a95SJean-Philippe Brucker #include <linux/mmu_notifier.h>
9cbd23144SJean-Philippe Brucker #include <linux/sched/mm.h>
103f1ce8e8SJean-Philippe Brucker #include <linux/slab.h>
1156e1a4ccSJason Gunthorpe #include <kunit/visibility.h>
123f1ce8e8SJean-Philippe Brucker 
133f1ce8e8SJean-Philippe Brucker #include "arm-smmu-v3.h"
143f1ce8e8SJean-Philippe Brucker #include "../../io-pgtable-arm.h"
153f1ce8e8SJean-Philippe Brucker 
16d38c28dbSJason Gunthorpe static void __maybe_unused
arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain * smmu_domain)17e9d1e4ffSJason Gunthorpe arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain)
18e9d1e4ffSJason Gunthorpe {
19ad10dce6SJason Gunthorpe 	struct arm_smmu_master_domain *master_domain;
20e9d1e4ffSJason Gunthorpe 	struct arm_smmu_cd target_cd;
21e9d1e4ffSJason Gunthorpe 	unsigned long flags;
22e9d1e4ffSJason Gunthorpe 
23e9d1e4ffSJason Gunthorpe 	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
24ad10dce6SJason Gunthorpe 	list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) {
25ad10dce6SJason Gunthorpe 		struct arm_smmu_master *master = master_domain->master;
26e9d1e4ffSJason Gunthorpe 		struct arm_smmu_cd *cdptr;
27e9d1e4ffSJason Gunthorpe 
2864efb3deSJason Gunthorpe 		cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid);
29e9d1e4ffSJason Gunthorpe 		if (WARN_ON(!cdptr))
30e9d1e4ffSJason Gunthorpe 			continue;
31e9d1e4ffSJason Gunthorpe 
32e9d1e4ffSJason Gunthorpe 		arm_smmu_make_s1_cd(&target_cd, master, smmu_domain);
3364efb3deSJason Gunthorpe 		arm_smmu_write_cd_entry(master, master_domain->ssid, cdptr,
34e9d1e4ffSJason Gunthorpe 					&target_cd);
35e9d1e4ffSJason Gunthorpe 	}
36e9d1e4ffSJason Gunthorpe 	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
37e9d1e4ffSJason Gunthorpe }
38e9d1e4ffSJason Gunthorpe 
page_size_to_cd(void)397b87c93cSJason Gunthorpe static u64 page_size_to_cd(void)
407b87c93cSJason Gunthorpe {
417b87c93cSJason Gunthorpe 	static_assert(PAGE_SIZE == SZ_4K || PAGE_SIZE == SZ_16K ||
427b87c93cSJason Gunthorpe 		      PAGE_SIZE == SZ_64K);
437b87c93cSJason Gunthorpe 	if (PAGE_SIZE == SZ_64K)
447b87c93cSJason Gunthorpe 		return ARM_LPAE_TCR_TG0_64K;
457b87c93cSJason Gunthorpe 	if (PAGE_SIZE == SZ_16K)
467b87c93cSJason Gunthorpe 		return ARM_LPAE_TCR_TG0_16K;
477b87c93cSJason Gunthorpe 	return ARM_LPAE_TCR_TG0_4K;
487b87c93cSJason Gunthorpe }
497b87c93cSJason Gunthorpe 
5056e1a4ccSJason Gunthorpe VISIBLE_IF_KUNIT
arm_smmu_make_sva_cd(struct arm_smmu_cd * target,struct arm_smmu_master * master,struct mm_struct * mm,u16 asid)5156e1a4ccSJason Gunthorpe void arm_smmu_make_sva_cd(struct arm_smmu_cd *target,
5256e1a4ccSJason Gunthorpe 			  struct arm_smmu_master *master, struct mm_struct *mm,
5356e1a4ccSJason Gunthorpe 			  u16 asid)
547b87c93cSJason Gunthorpe {
557b87c93cSJason Gunthorpe 	u64 par;
567b87c93cSJason Gunthorpe 
577b87c93cSJason Gunthorpe 	memset(target, 0, sizeof(*target));
587b87c93cSJason Gunthorpe 
597b87c93cSJason Gunthorpe 	par = cpuid_feature_extract_unsigned_field(
607b87c93cSJason Gunthorpe 		read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1),
617b87c93cSJason Gunthorpe 		ID_AA64MMFR0_EL1_PARANGE_SHIFT);
627b87c93cSJason Gunthorpe 
637b87c93cSJason Gunthorpe 	target->data[0] = cpu_to_le64(
647b87c93cSJason Gunthorpe 		CTXDESC_CD_0_TCR_EPD1 |
657b87c93cSJason Gunthorpe #ifdef __BIG_ENDIAN
667b87c93cSJason Gunthorpe 		CTXDESC_CD_0_ENDI |
677b87c93cSJason Gunthorpe #endif
687b87c93cSJason Gunthorpe 		CTXDESC_CD_0_V |
697b87c93cSJason Gunthorpe 		FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par) |
707b87c93cSJason Gunthorpe 		CTXDESC_CD_0_AA64 |
717b87c93cSJason Gunthorpe 		(master->stall_enabled ? CTXDESC_CD_0_S : 0) |
727b87c93cSJason Gunthorpe 		CTXDESC_CD_0_R |
737b87c93cSJason Gunthorpe 		CTXDESC_CD_0_A |
747b87c93cSJason Gunthorpe 		CTXDESC_CD_0_ASET |
757b87c93cSJason Gunthorpe 		FIELD_PREP(CTXDESC_CD_0_ASID, asid));
767b87c93cSJason Gunthorpe 
777b87c93cSJason Gunthorpe 	/*
787b87c93cSJason Gunthorpe 	 * If no MM is passed then this creates a SVA entry that faults
797b87c93cSJason Gunthorpe 	 * everything. arm_smmu_write_cd_entry() can hitlessly go between these
807b87c93cSJason Gunthorpe 	 * two entries types since TTB0 is ignored by HW when EPD0 is set.
817b87c93cSJason Gunthorpe 	 */
827b87c93cSJason Gunthorpe 	if (mm) {
837b87c93cSJason Gunthorpe 		target->data[0] |= cpu_to_le64(
847b87c93cSJason Gunthorpe 			FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ,
857b87c93cSJason Gunthorpe 				   64ULL - vabits_actual) |
867b87c93cSJason Gunthorpe 			FIELD_PREP(CTXDESC_CD_0_TCR_TG0, page_size_to_cd()) |
877b87c93cSJason Gunthorpe 			FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0,
887b87c93cSJason Gunthorpe 				   ARM_LPAE_TCR_RGN_WBWA) |
897b87c93cSJason Gunthorpe 			FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0,
907b87c93cSJason Gunthorpe 				   ARM_LPAE_TCR_RGN_WBWA) |
917b87c93cSJason Gunthorpe 			FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS));
927b87c93cSJason Gunthorpe 
937b87c93cSJason Gunthorpe 		target->data[1] = cpu_to_le64(virt_to_phys(mm->pgd) &
947b87c93cSJason Gunthorpe 					      CTXDESC_CD_1_TTB0_MASK);
957b87c93cSJason Gunthorpe 	} else {
967b87c93cSJason Gunthorpe 		target->data[0] |= cpu_to_le64(CTXDESC_CD_0_TCR_EPD0);
977b87c93cSJason Gunthorpe 
987b87c93cSJason Gunthorpe 		/*
997b87c93cSJason Gunthorpe 		 * Disable stall and immediately generate an abort if stall
1007b87c93cSJason Gunthorpe 		 * disable is permitted. This speeds up cleanup for an unclean
1017b87c93cSJason Gunthorpe 		 * exit if the device is still doing a lot of DMA.
1027b87c93cSJason Gunthorpe 		 */
1037b87c93cSJason Gunthorpe 		if (!(master->smmu->features & ARM_SMMU_FEAT_STALL_FORCE))
1047b87c93cSJason Gunthorpe 			target->data[0] &=
1057b87c93cSJason Gunthorpe 				cpu_to_le64(~(CTXDESC_CD_0_S | CTXDESC_CD_0_R));
1067b87c93cSJason Gunthorpe 	}
1077b87c93cSJason Gunthorpe 
1087b87c93cSJason Gunthorpe 	/*
1097b87c93cSJason Gunthorpe 	 * MAIR value is pretty much constant and global, so we can just get it
1107b87c93cSJason Gunthorpe 	 * from the current CPU register
1117b87c93cSJason Gunthorpe 	 */
1127b87c93cSJason Gunthorpe 	target->data[3] = cpu_to_le64(read_sysreg(mair_el1));
1136e192214SRobin Murphy 
1146e192214SRobin Murphy 	/*
1156e192214SRobin Murphy 	 * Note that we don't bother with S1PIE on the SMMU, we just rely on
1166e192214SRobin Murphy 	 * our default encoding scheme matching direct permissions anyway.
1176e192214SRobin Murphy 	 * SMMU has no notion of S1POE nor GCS, so make sure that is clear if
1186e192214SRobin Murphy 	 * either is enabled for CPUs, just in case anyone imagines otherwise.
1196e192214SRobin Murphy 	 */
1206e192214SRobin Murphy 	if (system_supports_poe() || system_supports_gcs())
1216e192214SRobin Murphy 		dev_warn_once(master->smmu->dev, "SVA devices ignore permission overlays and GCS\n");
1227b87c93cSJason Gunthorpe }
123da55da5aSJason Gunthorpe EXPORT_SYMBOL_IF_KUNIT(arm_smmu_make_sva_cd);
1247b87c93cSJason Gunthorpe 
125d5afb4b4SNicolin Chen /*
126d5afb4b4SNicolin Chen  * Cloned from the MAX_TLBI_OPS in arch/arm64/include/asm/tlbflush.h, this
127d5afb4b4SNicolin Chen  * is used as a threshold to replace per-page TLBI commands to issue in the
128d5afb4b4SNicolin Chen  * command queue with an address-space TLBI command, when SMMU w/o a range
129d5afb4b4SNicolin Chen  * invalidation feature handles too many per-page TLBI commands, which will
130d5afb4b4SNicolin Chen  * otherwise result in a soft lockup.
131d5afb4b4SNicolin Chen  */
132d5afb4b4SNicolin Chen #define CMDQ_MAX_TLBI_OPS		(1 << (PAGE_SHIFT - 3))
133d5afb4b4SNicolin Chen 
arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier * mn,struct mm_struct * mm,unsigned long start,unsigned long end)1341af5a810SAlistair Popple static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn,
1352f7e8c55SJean-Philippe Brucker 						struct mm_struct *mm,
1361af5a810SAlistair Popple 						unsigned long start,
1371af5a810SAlistair Popple 						unsigned long end)
1382f7e8c55SJean-Philippe Brucker {
139d38c28dbSJason Gunthorpe 	struct arm_smmu_domain *smmu_domain =
140d38c28dbSJason Gunthorpe 		container_of(mn, struct arm_smmu_domain, mmu_notifier);
14195d4782cSNicolin Chen 	size_t size;
14295d4782cSNicolin Chen 
14395d4782cSNicolin Chen 	/*
14495d4782cSNicolin Chen 	 * The mm_types defines vm_end as the first byte after the end address,
14595d4782cSNicolin Chen 	 * different from IOMMU subsystem using the last address of an address
14695d4782cSNicolin Chen 	 * range. So do a simple translation here by calculating size correctly.
14795d4782cSNicolin Chen 	 */
14895d4782cSNicolin Chen 	size = end - start;
149d5afb4b4SNicolin Chen 	if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_RANGE_INV)) {
150d5afb4b4SNicolin Chen 		if (size >= CMDQ_MAX_TLBI_OPS * PAGE_SIZE)
151d5afb4b4SNicolin Chen 			size = 0;
152d5afb4b4SNicolin Chen 	} else {
15338b14e2eSAlistair Popple 		if (size == ULONG_MAX)
15438b14e2eSAlistair Popple 			size = 0;
155d5afb4b4SNicolin Chen 	}
1562f7e8c55SJean-Philippe Brucker 
15738b14e2eSAlistair Popple 	if (!size)
158d38c28dbSJason Gunthorpe 		arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid);
15938b14e2eSAlistair Popple 	else
160d38c28dbSJason Gunthorpe 		arm_smmu_tlb_inv_range_asid(start, size, smmu_domain->cd.asid,
161d38c28dbSJason Gunthorpe 					    PAGE_SIZE, false, smmu_domain);
16238b14e2eSAlistair Popple 
163d38c28dbSJason Gunthorpe 	arm_smmu_atc_inv_domain(smmu_domain, start, size);
1642f7e8c55SJean-Philippe Brucker }
1652f7e8c55SJean-Philippe Brucker 
arm_smmu_mm_release(struct mmu_notifier * mn,struct mm_struct * mm)16632784a95SJean-Philippe Brucker static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
16732784a95SJean-Philippe Brucker {
168d38c28dbSJason Gunthorpe 	struct arm_smmu_domain *smmu_domain =
169d38c28dbSJason Gunthorpe 		container_of(mn, struct arm_smmu_domain, mmu_notifier);
170ad10dce6SJason Gunthorpe 	struct arm_smmu_master_domain *master_domain;
1717b87c93cSJason Gunthorpe 	unsigned long flags;
17232784a95SJean-Philippe Brucker 
17332784a95SJean-Philippe Brucker 	/*
17432784a95SJean-Philippe Brucker 	 * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events,
17532784a95SJean-Philippe Brucker 	 * but disable translation.
17632784a95SJean-Philippe Brucker 	 */
1777b87c93cSJason Gunthorpe 	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
178ad10dce6SJason Gunthorpe 	list_for_each_entry(master_domain, &smmu_domain->devices,
179ad10dce6SJason Gunthorpe 			    devices_elm) {
180ad10dce6SJason Gunthorpe 		struct arm_smmu_master *master = master_domain->master;
1817b87c93cSJason Gunthorpe 		struct arm_smmu_cd target;
1827b87c93cSJason Gunthorpe 		struct arm_smmu_cd *cdptr;
1837b87c93cSJason Gunthorpe 
184d38c28dbSJason Gunthorpe 		cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid);
1857b87c93cSJason Gunthorpe 		if (WARN_ON(!cdptr))
1867b87c93cSJason Gunthorpe 			continue;
187d38c28dbSJason Gunthorpe 		arm_smmu_make_sva_cd(&target, master, NULL,
188d38c28dbSJason Gunthorpe 				     smmu_domain->cd.asid);
189d38c28dbSJason Gunthorpe 		arm_smmu_write_cd_entry(master, master_domain->ssid, cdptr,
1907b87c93cSJason Gunthorpe 					&target);
1917b87c93cSJason Gunthorpe 	}
1927b87c93cSJason Gunthorpe 	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
19332784a95SJean-Philippe Brucker 
194d38c28dbSJason Gunthorpe 	arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid);
195d38c28dbSJason Gunthorpe 	arm_smmu_atc_inv_domain(smmu_domain, 0, 0);
19632784a95SJean-Philippe Brucker }
19732784a95SJean-Philippe Brucker 
arm_smmu_mmu_notifier_free(struct mmu_notifier * mn)19832784a95SJean-Philippe Brucker static void arm_smmu_mmu_notifier_free(struct mmu_notifier *mn)
19932784a95SJean-Philippe Brucker {
200d38c28dbSJason Gunthorpe 	kfree(container_of(mn, struct arm_smmu_domain, mmu_notifier));
20132784a95SJean-Philippe Brucker }
20232784a95SJean-Philippe Brucker 
20317d9a4b4SRikard Falkeborn static const struct mmu_notifier_ops arm_smmu_mmu_notifier_ops = {
2041af5a810SAlistair Popple 	.arch_invalidate_secondary_tlbs	= arm_smmu_mm_arch_invalidate_secondary_tlbs,
20532784a95SJean-Philippe Brucker 	.release			= arm_smmu_mm_release,
20632784a95SJean-Philippe Brucker 	.free_notifier			= arm_smmu_mmu_notifier_free,
20732784a95SJean-Philippe Brucker };
20832784a95SJean-Philippe Brucker 
arm_smmu_sva_supported(struct arm_smmu_device * smmu)209d744f9e6SJean-Philippe Brucker bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
210d744f9e6SJean-Philippe Brucker {
211d744f9e6SJean-Philippe Brucker 	unsigned long reg, fld;
212d744f9e6SJean-Philippe Brucker 	unsigned long oas;
213d744f9e6SJean-Philippe Brucker 	unsigned long asid_bits;
21451d113c3SJean-Philippe Brucker 	u32 feat_mask = ARM_SMMU_FEAT_COHERENCY;
215d744f9e6SJean-Philippe Brucker 
2166e192214SRobin Murphy 	if (vabits_actual == 52) {
2176e192214SRobin Murphy 		/* We don't support LPA2 */
2186e192214SRobin Murphy 		if (PAGE_SIZE != SZ_64K)
2196e192214SRobin Murphy 			return false;
220d744f9e6SJean-Philippe Brucker 		feat_mask |= ARM_SMMU_FEAT_VAX;
2216e192214SRobin Murphy 	}
222d744f9e6SJean-Philippe Brucker 
223d744f9e6SJean-Philippe Brucker 	if (system_supports_bbml2_noabort())
224d744f9e6SJean-Philippe Brucker 		feat_mask |= ARM_SMMU_FEAT_BBML2;
225d744f9e6SJean-Philippe Brucker 
226d744f9e6SJean-Philippe Brucker 	if ((smmu->features & feat_mask) != feat_mask)
227d744f9e6SJean-Philippe Brucker 		return false;
228d744f9e6SJean-Philippe Brucker 
229d744f9e6SJean-Philippe Brucker 	if (!(smmu->pgsize_bitmap & PAGE_SIZE))
230d744f9e6SJean-Philippe Brucker 		return false;
231d744f9e6SJean-Philippe Brucker 
232d744f9e6SJean-Philippe Brucker 	/*
233d744f9e6SJean-Philippe Brucker 	 * Get the smallest PA size of all CPUs (sanitized by cpufeature). We're
234d744f9e6SJean-Philippe Brucker 	 * not even pretending to support AArch32 here. Abort if the MMU outputs
2352d987e64SMark Brown 	 * addresses larger than what we support.
236d744f9e6SJean-Philippe Brucker 	 */
237d744f9e6SJean-Philippe Brucker 	reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
238d744f9e6SJean-Philippe Brucker 	fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_EL1_PARANGE_SHIFT);
239d744f9e6SJean-Philippe Brucker 	oas = id_aa64mmfr0_parange_to_phys_shift(fld);
240d744f9e6SJean-Philippe Brucker 	if (smmu->oas < oas)
24107d7d848SMark Brown 		return false;
242d744f9e6SJean-Philippe Brucker 
243d744f9e6SJean-Philippe Brucker 	/* We can support bigger ASIDs than the CPU, but not smaller */
244d744f9e6SJean-Philippe Brucker 	fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_EL1_ASIDBITS_SHIFT);
245d744f9e6SJean-Philippe Brucker 	asid_bits = fld ? 16 : 8;
246d744f9e6SJean-Philippe Brucker 	if (smmu->asid_bits < asid_bits)
247d744f9e6SJean-Philippe Brucker 		return false;
248d744f9e6SJean-Philippe Brucker 
249d744f9e6SJean-Philippe Brucker 	/*
250d744f9e6SJean-Philippe Brucker 	 * See max_pinned_asids in arch/arm64/mm/context.c. The following is
251d744f9e6SJean-Philippe Brucker 	 * generally the maximum number of bindable processes.
252d744f9e6SJean-Philippe Brucker 	 */
253d744f9e6SJean-Philippe Brucker 	if (arm64_kernel_unmapped_at_el0())
254d744f9e6SJean-Philippe Brucker 		asid_bits--;
255d744f9e6SJean-Philippe Brucker 	dev_dbg(smmu->dev, "%d shared contexts\n", (1 << asid_bits) -
256d744f9e6SJean-Philippe Brucker 		num_possible_cpus() - 2);
257f534d98bSJean-Philippe Brucker 
25832784a95SJean-Philippe Brucker 	return true;
25932784a95SJean-Philippe Brucker }
26032784a95SJean-Philippe Brucker 
arm_smmu_sva_notifier_synchronize(void)26132784a95SJean-Philippe Brucker void arm_smmu_sva_notifier_synchronize(void)
26232784a95SJean-Philippe Brucker {
26332784a95SJean-Philippe Brucker 	/*
26432784a95SJean-Philippe Brucker 	 * Some MMU notifiers may still be waiting to be freed, using
26532784a95SJean-Philippe Brucker 	 * arm_smmu_mmu_notifier_free(). Wait for them.
266386fa64fSLu Baolu 	 */
267386fa64fSLu Baolu 	mmu_notifier_synchronize();
268b45a3777SYi Liu }
269b45a3777SYi Liu 
arm_smmu_sva_set_dev_pasid(struct iommu_domain * domain,struct device * dev,ioasid_t id,struct iommu_domain * old)270386fa64fSLu Baolu static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain,
271d38c28dbSJason Gunthorpe 				      struct device *dev, ioasid_t id,
27285f2fb6eSJason Gunthorpe 				      struct iommu_domain *old)
27385f2fb6eSJason Gunthorpe {
27485f2fb6eSJason Gunthorpe 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
275386fa64fSLu Baolu 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
276*cfea71aeSJason Gunthorpe 	struct arm_smmu_cd target;
277*cfea71aeSJason Gunthorpe 	int ret;
278*cfea71aeSJason Gunthorpe 
279d38c28dbSJason Gunthorpe 	if (!(master->smmu->features & ARM_SMMU_FEAT_SVA))
280d38c28dbSJason Gunthorpe 		return -EOPNOTSUPP;
281fdc69d39SJason Gunthorpe 
282fdc69d39SJason Gunthorpe 	/* Prevent arm_smmu_mm_release from being called while we are attaching */
283d38c28dbSJason Gunthorpe 	if (!mmget_not_zero(domain->mm))
284d38c28dbSJason Gunthorpe 		return -EINVAL;
285d38c28dbSJason Gunthorpe 
286d38c28dbSJason Gunthorpe 	/*
287d38c28dbSJason Gunthorpe 	 * This does not need the arm_smmu_asid_lock because SVA domains never
288e9f1f727SJason Gunthorpe 	 * get reassigned
289386fa64fSLu Baolu 	 */
290d38c28dbSJason Gunthorpe 	arm_smmu_make_sva_cd(&target, master, domain->mm, smmu_domain->cd.asid);
291386fa64fSLu Baolu 	ret = arm_smmu_set_pasid(master, smmu_domain, id, &target, old);
292386fa64fSLu Baolu 
293386fa64fSLu Baolu 	mmput(domain->mm);
294386fa64fSLu Baolu 	return ret;
295386fa64fSLu Baolu }
296d38c28dbSJason Gunthorpe 
arm_smmu_sva_domain_free(struct iommu_domain * domain)297d38c28dbSJason Gunthorpe static void arm_smmu_sva_domain_free(struct iommu_domain *domain)
298d38c28dbSJason Gunthorpe {
299d38c28dbSJason Gunthorpe 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
300d38c28dbSJason Gunthorpe 
301d38c28dbSJason Gunthorpe 	/*
302d38c28dbSJason Gunthorpe 	 * Ensure the ASID is empty in the iommu cache before allowing reuse.
303d38c28dbSJason Gunthorpe 	 */
304d38c28dbSJason Gunthorpe 	arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid);
305d38c28dbSJason Gunthorpe 
306d38c28dbSJason Gunthorpe 	/*
307d38c28dbSJason Gunthorpe 	 * Notice that the arm_smmu_mm_arch_invalidate_secondary_tlbs op can
308d38c28dbSJason Gunthorpe 	 * still be called/running at this point. We allow the ASID to be
309d38c28dbSJason Gunthorpe 	 * reused, and if there is a race then it just suffers harmless
310d38c28dbSJason Gunthorpe 	 * unnecessary invalidation.
311d38c28dbSJason Gunthorpe 	 */
312d38c28dbSJason Gunthorpe 	xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid);
313d38c28dbSJason Gunthorpe 
314d38c28dbSJason Gunthorpe 	/*
315d38c28dbSJason Gunthorpe 	 * Actual free is defered to the SRCU callback
316386fa64fSLu Baolu 	 * arm_smmu_mmu_notifier_free()
317386fa64fSLu Baolu 	 */
318386fa64fSLu Baolu 	mmu_notifier_put(&smmu_domain->mmu_notifier);
319386fa64fSLu Baolu }
320386fa64fSLu Baolu 
321386fa64fSLu Baolu static const struct iommu_domain_ops arm_smmu_sva_domain_ops = {
322386fa64fSLu Baolu 	.set_dev_pasid		= arm_smmu_sva_set_dev_pasid,
323678d79b9SJason Gunthorpe 	.free			= arm_smmu_sva_domain_free
324678d79b9SJason Gunthorpe };
325386fa64fSLu Baolu 
arm_smmu_sva_domain_alloc(struct device * dev,struct mm_struct * mm)326d7b2d2baSJason Gunthorpe struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev,
327d7b2d2baSJason Gunthorpe 					       struct mm_struct *mm)
328d7b2d2baSJason Gunthorpe {
329d38c28dbSJason Gunthorpe 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
330d38c28dbSJason Gunthorpe 	struct arm_smmu_device *smmu = master->smmu;
331386fa64fSLu Baolu 	struct arm_smmu_domain *smmu_domain;
332*cfea71aeSJason Gunthorpe 	u32 asid;
333*cfea71aeSJason Gunthorpe 	int ret;
334*cfea71aeSJason Gunthorpe 
335d7b2d2baSJason Gunthorpe 	if (!(master->smmu->features & ARM_SMMU_FEAT_SVA))
336d7b2d2baSJason Gunthorpe 		return ERR_PTR(-EOPNOTSUPP);
337d7b2d2baSJason Gunthorpe 
338d7b2d2baSJason Gunthorpe 	smmu_domain = arm_smmu_domain_alloc();
339d7b2d2baSJason Gunthorpe 	if (IS_ERR(smmu_domain))
34012f78021SBalbir Singh 		return ERR_CAST(smmu_domain);
34112f78021SBalbir Singh 	smmu_domain->domain.type = IOMMU_DOMAIN_SVA;
34212f78021SBalbir Singh 	smmu_domain->domain.ops = &arm_smmu_sva_domain_ops;
34312f78021SBalbir Singh 
34412f78021SBalbir Singh 	/*
34512f78021SBalbir Singh 	 * Choose page_size as the leaf page size for invalidation when
346d7b2d2baSJason Gunthorpe 	 * ARM_SMMU_FEAT_RANGE_INV is present
347386fa64fSLu Baolu 	 */
348d38c28dbSJason Gunthorpe 	smmu_domain->domain.pgsize_bitmap = PAGE_SIZE;
349d38c28dbSJason Gunthorpe 	smmu_domain->smmu = smmu;
350d38c28dbSJason Gunthorpe 
351d38c28dbSJason Gunthorpe 	ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain,
352d38c28dbSJason Gunthorpe 		       XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL);
353d38c28dbSJason Gunthorpe 	if (ret)
354d38c28dbSJason Gunthorpe 		goto err_free;
355d38c28dbSJason Gunthorpe 
356d38c28dbSJason Gunthorpe 	smmu_domain->cd.asid = asid;
357d38c28dbSJason Gunthorpe 	smmu_domain->mmu_notifier.ops = &arm_smmu_mmu_notifier_ops;
358d38c28dbSJason Gunthorpe 	ret = mmu_notifier_register(&smmu_domain->mmu_notifier, mm);
359d7b2d2baSJason Gunthorpe 	if (ret)
360d38c28dbSJason Gunthorpe 		goto err_asid;
361d38c28dbSJason Gunthorpe 
362d38c28dbSJason Gunthorpe 	return &smmu_domain->domain;
363d38c28dbSJason Gunthorpe 
364d38c28dbSJason Gunthorpe err_asid:
365d38c28dbSJason Gunthorpe 	xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid);
366386fa64fSLu Baolu err_free:
367 	kfree(smmu_domain);
368 	return ERR_PTR(ret);
369 }
370