• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Implementation of the IOMMU SVA API for the ARM SMMUv3
4  */
5 
6 #include <linux/mm.h>
7 #include <linux/mmu_context.h>
8 #include <linux/mmu_notifier.h>
9 #include <linux/sched/mm.h>
10 #include <linux/slab.h>
11 
12 #include "arm-smmu-v3.h"
13 #include "../../iommu-sva-lib.h"
14 #include "../../io-pgtable-arm.h"
15 
16 struct arm_smmu_mmu_notifier {
17 	struct mmu_notifier		mn;
18 	struct arm_smmu_ctx_desc	*cd;
19 	bool				cleared;
20 	refcount_t			refs;
21 	struct list_head		list;
22 	struct arm_smmu_domain		*domain;
23 };
24 
25 #define mn_to_smmu(mn) container_of(mn, struct arm_smmu_mmu_notifier, mn)
26 
27 struct arm_smmu_bond {
28 	struct iommu_sva		sva;
29 	struct mm_struct		*mm;
30 	struct arm_smmu_mmu_notifier	*smmu_mn;
31 	struct list_head		list;
32 	refcount_t			refs;
33 };
34 
35 #define sva_to_bond(handle) \
36 	container_of(handle, struct arm_smmu_bond, sva)
37 
38 static DEFINE_MUTEX(sva_lock);
39 
40 /*
41  * Check if the CPU ASID is available on the SMMU side. If a private context
42  * descriptor is using it, try to replace it.
43  */
44 static struct arm_smmu_ctx_desc *
arm_smmu_share_asid(struct mm_struct * mm,u16 asid)45 arm_smmu_share_asid(struct mm_struct *mm, u16 asid)
46 {
47 	int ret;
48 	u32 new_asid;
49 	struct arm_smmu_ctx_desc *cd;
50 	struct arm_smmu_device *smmu;
51 	struct arm_smmu_domain *smmu_domain;
52 
53 	cd = xa_load(&arm_smmu_asid_xa, asid);
54 	if (!cd)
55 		return NULL;
56 
57 	if (cd->mm) {
58 		if (WARN_ON(cd->mm != mm))
59 			return ERR_PTR(-EINVAL);
60 		/* All devices bound to this mm use the same cd struct. */
61 		refcount_inc(&cd->refs);
62 		return cd;
63 	}
64 
65 	smmu_domain = container_of(cd, struct arm_smmu_domain, s1_cfg.cd);
66 	smmu = smmu_domain->smmu;
67 
68 	ret = xa_alloc(&arm_smmu_asid_xa, &new_asid, cd,
69 		       XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL);
70 	if (ret)
71 		return ERR_PTR(-ENOSPC);
72 	/*
73 	 * Race with unmap: TLB invalidations will start targeting the new ASID,
74 	 * which isn't assigned yet. We'll do an invalidate-all on the old ASID
75 	 * later, so it doesn't matter.
76 	 */
77 	cd->asid = new_asid;
78 	/*
79 	 * Update ASID and invalidate CD in all associated masters. There will
80 	 * be some overlap between use of both ASIDs, until we invalidate the
81 	 * TLB.
82 	 */
83 	arm_smmu_write_ctx_desc(smmu_domain, 0, cd);
84 
85 	/* Invalidate TLB entries previously associated with that context */
86 	arm_smmu_tlb_inv_asid(smmu, asid);
87 
88 	xa_erase(&arm_smmu_asid_xa, asid);
89 	return NULL;
90 }
91 
arm_smmu_alloc_shared_cd(struct mm_struct * mm)92 static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm)
93 {
94 	u16 asid;
95 	int err = 0;
96 	u64 tcr, par, reg;
97 	struct arm_smmu_ctx_desc *cd;
98 	struct arm_smmu_ctx_desc *ret = NULL;
99 
100 	/* Don't free the mm until we release the ASID */
101 	mmgrab(mm);
102 
103 	asid = arm64_mm_context_get(mm);
104 	if (!asid) {
105 		err = -ESRCH;
106 		goto out_drop_mm;
107 	}
108 
109 	cd = kzalloc(sizeof(*cd), GFP_KERNEL);
110 	if (!cd) {
111 		err = -ENOMEM;
112 		goto out_put_context;
113 	}
114 
115 	refcount_set(&cd->refs, 1);
116 
117 	mutex_lock(&arm_smmu_asid_lock);
118 	ret = arm_smmu_share_asid(mm, asid);
119 	if (ret) {
120 		mutex_unlock(&arm_smmu_asid_lock);
121 		goto out_free_cd;
122 	}
123 
124 	err = xa_insert(&arm_smmu_asid_xa, asid, cd, GFP_KERNEL);
125 	mutex_unlock(&arm_smmu_asid_lock);
126 
127 	if (err)
128 		goto out_free_asid;
129 
130 	tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, 64ULL - vabits_actual) |
131 	      FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, ARM_LPAE_TCR_RGN_WBWA) |
132 	      FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, ARM_LPAE_TCR_RGN_WBWA) |
133 	      FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS) |
134 	      CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64;
135 
136 	switch (PAGE_SIZE) {
137 	case SZ_4K:
138 		tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_4K);
139 		break;
140 	case SZ_16K:
141 		tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_16K);
142 		break;
143 	case SZ_64K:
144 		tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_64K);
145 		break;
146 	default:
147 		WARN_ON(1);
148 		err = -EINVAL;
149 		goto out_free_asid;
150 	}
151 
152 	reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
153 	par = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_EL1_PARANGE_SHIFT);
154 	tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par);
155 
156 	cd->ttbr = virt_to_phys(mm->pgd);
157 	cd->tcr = tcr;
158 	/*
159 	 * MAIR value is pretty much constant and global, so we can just get it
160 	 * from the current CPU register
161 	 */
162 	cd->mair = read_sysreg(mair_el1);
163 	cd->asid = asid;
164 	cd->mm = mm;
165 
166 	return cd;
167 
168 out_free_asid:
169 	arm_smmu_free_asid(cd);
170 out_free_cd:
171 	kfree(cd);
172 out_put_context:
173 	arm64_mm_context_put(mm);
174 out_drop_mm:
175 	mmdrop(mm);
176 	return err < 0 ? ERR_PTR(err) : ret;
177 }
178 
arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc * cd)179 static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd)
180 {
181 	if (arm_smmu_free_asid(cd)) {
182 		/* Unpin ASID */
183 		arm64_mm_context_put(cd->mm);
184 		mmdrop(cd->mm);
185 		kfree(cd);
186 	}
187 }
188 
189 /*
190  * Cloned from the MAX_TLBI_OPS in arch/arm64/include/asm/tlbflush.h, this
191  * is used as a threshold to replace per-page TLBI commands to issue in the
192  * command queue with an address-space TLBI command, when SMMU w/o a range
193  * invalidation feature handles too many per-page TLBI commands, which will
194  * otherwise result in a soft lockup.
195  */
196 #define CMDQ_MAX_TLBI_OPS		(1 << (PAGE_SHIFT - 3))
197 
arm_smmu_mm_invalidate_range(struct mmu_notifier * mn,struct mm_struct * mm,unsigned long start,unsigned long end)198 static void arm_smmu_mm_invalidate_range(struct mmu_notifier *mn,
199 					 struct mm_struct *mm,
200 					 unsigned long start, unsigned long end)
201 {
202 	struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn);
203 	struct arm_smmu_domain *smmu_domain = smmu_mn->domain;
204 	size_t size;
205 
206 	/*
207 	 * The mm_types defines vm_end as the first byte after the end address,
208 	 * different from IOMMU subsystem using the last address of an address
209 	 * range. So do a simple translation here by calculating size correctly.
210 	 */
211 	size = end - start;
212 	if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_RANGE_INV)) {
213 		if (size >= CMDQ_MAX_TLBI_OPS * PAGE_SIZE)
214 			size = 0;
215 	}
216 
217 	if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_BTM)) {
218 		if (!size)
219 			arm_smmu_tlb_inv_asid(smmu_domain->smmu,
220 					      smmu_mn->cd->asid);
221 		else
222 			arm_smmu_tlb_inv_range_asid(start, size,
223 						    smmu_mn->cd->asid,
224 						    PAGE_SIZE, false,
225 						    smmu_domain);
226 	}
227 
228 	arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, start, size);
229 }
230 
arm_smmu_mm_release(struct mmu_notifier * mn,struct mm_struct * mm)231 static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
232 {
233 	struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn);
234 	struct arm_smmu_domain *smmu_domain = smmu_mn->domain;
235 
236 	mutex_lock(&sva_lock);
237 	if (smmu_mn->cleared) {
238 		mutex_unlock(&sva_lock);
239 		return;
240 	}
241 
242 	/*
243 	 * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events,
244 	 * but disable translation.
245 	 */
246 	arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, &quiet_cd);
247 
248 	arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid);
249 	arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0);
250 
251 	smmu_mn->cleared = true;
252 	mutex_unlock(&sva_lock);
253 }
254 
arm_smmu_mmu_notifier_free(struct mmu_notifier * mn)255 static void arm_smmu_mmu_notifier_free(struct mmu_notifier *mn)
256 {
257 	kfree(mn_to_smmu(mn));
258 }
259 
260 static struct mmu_notifier_ops arm_smmu_mmu_notifier_ops = {
261 	.invalidate_range	= arm_smmu_mm_invalidate_range,
262 	.release		= arm_smmu_mm_release,
263 	.free_notifier		= arm_smmu_mmu_notifier_free,
264 };
265 
266 /* Allocate or get existing MMU notifier for this {domain, mm} pair */
267 static struct arm_smmu_mmu_notifier *
arm_smmu_mmu_notifier_get(struct arm_smmu_domain * smmu_domain,struct mm_struct * mm)268 arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain,
269 			  struct mm_struct *mm)
270 {
271 	int ret;
272 	struct arm_smmu_ctx_desc *cd;
273 	struct arm_smmu_mmu_notifier *smmu_mn;
274 
275 	list_for_each_entry(smmu_mn, &smmu_domain->mmu_notifiers, list) {
276 		if (smmu_mn->mn.mm == mm) {
277 			refcount_inc(&smmu_mn->refs);
278 			return smmu_mn;
279 		}
280 	}
281 
282 	cd = arm_smmu_alloc_shared_cd(mm);
283 	if (IS_ERR(cd))
284 		return ERR_CAST(cd);
285 
286 	smmu_mn = kzalloc(sizeof(*smmu_mn), GFP_KERNEL);
287 	if (!smmu_mn) {
288 		ret = -ENOMEM;
289 		goto err_free_cd;
290 	}
291 
292 	refcount_set(&smmu_mn->refs, 1);
293 	smmu_mn->cd = cd;
294 	smmu_mn->domain = smmu_domain;
295 	smmu_mn->mn.ops = &arm_smmu_mmu_notifier_ops;
296 
297 	ret = mmu_notifier_register(&smmu_mn->mn, mm);
298 	if (ret) {
299 		kfree(smmu_mn);
300 		goto err_free_cd;
301 	}
302 
303 	ret = arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, cd);
304 	if (ret)
305 		goto err_put_notifier;
306 
307 	list_add(&smmu_mn->list, &smmu_domain->mmu_notifiers);
308 	return smmu_mn;
309 
310 err_put_notifier:
311 	/* Frees smmu_mn */
312 	mmu_notifier_put(&smmu_mn->mn);
313 err_free_cd:
314 	arm_smmu_free_shared_cd(cd);
315 	return ERR_PTR(ret);
316 }
317 
arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier * smmu_mn)318 static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn)
319 {
320 	struct mm_struct *mm = smmu_mn->mn.mm;
321 	struct arm_smmu_ctx_desc *cd = smmu_mn->cd;
322 	struct arm_smmu_domain *smmu_domain = smmu_mn->domain;
323 
324 	if (!refcount_dec_and_test(&smmu_mn->refs))
325 		return;
326 
327 	list_del(&smmu_mn->list);
328 	arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, NULL);
329 
330 	/*
331 	 * If we went through clear(), we've already invalidated, and no
332 	 * new TLB entry can have been formed.
333 	 */
334 	if (!smmu_mn->cleared) {
335 		arm_smmu_tlb_inv_asid(smmu_domain->smmu, cd->asid);
336 		arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0);
337 	}
338 
339 	/* Frees smmu_mn */
340 	mmu_notifier_put(&smmu_mn->mn);
341 	arm_smmu_free_shared_cd(cd);
342 }
343 
344 static struct iommu_sva *
__arm_smmu_sva_bind(struct device * dev,struct mm_struct * mm)345 __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
346 {
347 	int ret;
348 	struct arm_smmu_bond *bond;
349 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
350 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
351 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
352 
353 	if (!master || !master->sva_enabled)
354 		return ERR_PTR(-ENODEV);
355 
356 	/* If bind() was already called for this {dev, mm} pair, reuse it. */
357 	list_for_each_entry(bond, &master->bonds, list) {
358 		if (bond->mm == mm) {
359 			refcount_inc(&bond->refs);
360 			return &bond->sva;
361 		}
362 	}
363 
364 	bond = kzalloc(sizeof(*bond), GFP_KERNEL);
365 	if (!bond)
366 		return ERR_PTR(-ENOMEM);
367 
368 	/* Allocate a PASID for this mm if necessary */
369 	ret = iommu_sva_alloc_pasid(mm, 1, (1U << master->ssid_bits) - 1);
370 	if (ret)
371 		goto err_free_bond;
372 
373 	bond->mm = mm;
374 	bond->sva.dev = dev;
375 	refcount_set(&bond->refs, 1);
376 
377 	bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm);
378 	if (IS_ERR(bond->smmu_mn)) {
379 		ret = PTR_ERR(bond->smmu_mn);
380 		goto err_free_pasid;
381 	}
382 
383 	list_add(&bond->list, &master->bonds);
384 	return &bond->sva;
385 
386 err_free_pasid:
387 	iommu_sva_free_pasid(mm);
388 err_free_bond:
389 	kfree(bond);
390 	return ERR_PTR(ret);
391 }
392 
393 struct iommu_sva *
arm_smmu_sva_bind(struct device * dev,struct mm_struct * mm,void * drvdata)394 arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, void *drvdata)
395 {
396 	struct iommu_sva *handle;
397 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
398 	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
399 
400 	if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1)
401 		return ERR_PTR(-EINVAL);
402 
403 	mutex_lock(&sva_lock);
404 	handle = __arm_smmu_sva_bind(dev, mm);
405 	mutex_unlock(&sva_lock);
406 	return handle;
407 }
408 
arm_smmu_sva_unbind(struct iommu_sva * handle)409 void arm_smmu_sva_unbind(struct iommu_sva *handle)
410 {
411 	struct arm_smmu_bond *bond = sva_to_bond(handle);
412 
413 	mutex_lock(&sva_lock);
414 	if (refcount_dec_and_test(&bond->refs)) {
415 		list_del(&bond->list);
416 		arm_smmu_mmu_notifier_put(bond->smmu_mn);
417 		iommu_sva_free_pasid(bond->mm);
418 		kfree(bond);
419 	}
420 	mutex_unlock(&sva_lock);
421 }
422 
arm_smmu_sva_get_pasid(struct iommu_sva * handle)423 u32 arm_smmu_sva_get_pasid(struct iommu_sva *handle)
424 {
425 	struct arm_smmu_bond *bond = sva_to_bond(handle);
426 
427 	return bond->mm->pasid;
428 }
429 
arm_smmu_sva_supported(struct arm_smmu_device * smmu)430 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu)
431 {
432 	unsigned long reg, fld;
433 	unsigned long oas;
434 	unsigned long asid_bits;
435 	u32 feat_mask = ARM_SMMU_FEAT_COHERENCY;
436 
437 	if (vabits_actual == 52)
438 		feat_mask |= ARM_SMMU_FEAT_VAX;
439 
440 	if ((smmu->features & feat_mask) != feat_mask)
441 		return false;
442 
443 	if (!(smmu->pgsize_bitmap & PAGE_SIZE))
444 		return false;
445 
446 	/*
447 	 * Get the smallest PA size of all CPUs (sanitized by cpufeature). We're
448 	 * not even pretending to support AArch32 here. Abort if the MMU outputs
449 	 * addresses larger than what we support.
450 	 */
451 	reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1);
452 	fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_EL1_PARANGE_SHIFT);
453 	oas = id_aa64mmfr0_parange_to_phys_shift(fld);
454 	if (smmu->oas < oas)
455 		return false;
456 
457 	/* We can support bigger ASIDs than the CPU, but not smaller */
458 	fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_EL1_ASIDBITS_SHIFT);
459 	asid_bits = fld ? 16 : 8;
460 	if (smmu->asid_bits < asid_bits)
461 		return false;
462 
463 	/*
464 	 * See max_pinned_asids in arch/arm64/mm/context.c. The following is
465 	 * generally the maximum number of bindable processes.
466 	 */
467 	if (arm64_kernel_unmapped_at_el0())
468 		asid_bits--;
469 	dev_dbg(smmu->dev, "%d shared contexts\n", (1 << asid_bits) -
470 		num_possible_cpus() - 2);
471 
472 	return true;
473 }
474 
arm_smmu_master_iopf_supported(struct arm_smmu_master * master)475 bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master)
476 {
477 	/* We're not keeping track of SIDs in fault events */
478 	if (master->num_streams != 1)
479 		return false;
480 
481 	return master->stall_enabled;
482 }
483 
arm_smmu_master_sva_supported(struct arm_smmu_master * master)484 bool arm_smmu_master_sva_supported(struct arm_smmu_master *master)
485 {
486 	if (!(master->smmu->features & ARM_SMMU_FEAT_SVA))
487 		return false;
488 
489 	/* SSID support is mandatory for the moment */
490 	return master->ssid_bits;
491 }
492 
arm_smmu_master_sva_enabled(struct arm_smmu_master * master)493 bool arm_smmu_master_sva_enabled(struct arm_smmu_master *master)
494 {
495 	bool enabled;
496 
497 	mutex_lock(&sva_lock);
498 	enabled = master->sva_enabled;
499 	mutex_unlock(&sva_lock);
500 	return enabled;
501 }
502 
arm_smmu_master_sva_enable_iopf(struct arm_smmu_master * master)503 static int arm_smmu_master_sva_enable_iopf(struct arm_smmu_master *master)
504 {
505 	int ret;
506 	struct device *dev = master->dev;
507 
508 	/*
509 	 * Drivers for devices supporting PRI or stall should enable IOPF first.
510 	 * Others have device-specific fault handlers and don't need IOPF.
511 	 */
512 	if (!arm_smmu_master_iopf_supported(master))
513 		return 0;
514 
515 	if (!master->iopf_enabled)
516 		return -EINVAL;
517 
518 	ret = iopf_queue_add_device(master->smmu->evtq.iopf, dev);
519 	if (ret)
520 		return ret;
521 
522 	ret = iommu_register_device_fault_handler(dev, iommu_queue_iopf, dev);
523 	if (ret) {
524 		iopf_queue_remove_device(master->smmu->evtq.iopf, dev);
525 		return ret;
526 	}
527 	return 0;
528 }
529 
arm_smmu_master_sva_disable_iopf(struct arm_smmu_master * master)530 static void arm_smmu_master_sva_disable_iopf(struct arm_smmu_master *master)
531 {
532 	struct device *dev = master->dev;
533 
534 	if (!master->iopf_enabled)
535 		return;
536 
537 	iommu_unregister_device_fault_handler(dev);
538 	iopf_queue_remove_device(master->smmu->evtq.iopf, dev);
539 }
540 
arm_smmu_master_enable_sva(struct arm_smmu_master * master)541 int arm_smmu_master_enable_sva(struct arm_smmu_master *master)
542 {
543 	int ret;
544 
545 	mutex_lock(&sva_lock);
546 	ret = arm_smmu_master_sva_enable_iopf(master);
547 	if (!ret)
548 		master->sva_enabled = true;
549 	mutex_unlock(&sva_lock);
550 
551 	return ret;
552 }
553 
arm_smmu_master_disable_sva(struct arm_smmu_master * master)554 int arm_smmu_master_disable_sva(struct arm_smmu_master *master)
555 {
556 	mutex_lock(&sva_lock);
557 	if (!list_empty(&master->bonds)) {
558 		dev_err(master->dev, "cannot disable SVA, device is bound\n");
559 		mutex_unlock(&sva_lock);
560 		return -EBUSY;
561 	}
562 	arm_smmu_master_sva_disable_iopf(master);
563 	master->sva_enabled = false;
564 	mutex_unlock(&sva_lock);
565 
566 	return 0;
567 }
568 
arm_smmu_sva_notifier_synchronize(void)569 void arm_smmu_sva_notifier_synchronize(void)
570 {
571 	/*
572 	 * Some MMU notifiers may still be waiting to be freed, using
573 	 * arm_smmu_mmu_notifier_free(). Wait for them.
574 	 */
575 	mmu_notifier_synchronize();
576 }
577