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