]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/vfio/vfio_iommu_type1.c
vfio iommu type1: Add support for mediated devices
[mirror_ubuntu-eoan-kernel.git] / drivers / vfio / vfio_iommu_type1.c
1 /*
2 * VFIO: IOMMU DMA mapping support for Type1 IOMMU
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
14 *
15 * We arbitrarily define a Type1 IOMMU as one matching the below code.
16 * It could be called the x86 IOMMU as it's designed for AMD-Vi & Intel
17 * VT-d, but that makes it harder to re-use as theoretically anyone
18 * implementing a similar IOMMU could make use of this. We expect the
19 * IOMMU to support the IOMMU API and have few to no restrictions around
20 * the IOVA range that can be mapped. The Type1 IOMMU is currently
21 * optimized for relatively static mappings of a userspace process with
22 * userpsace pages pinned into memory. We also assume devices and IOMMU
23 * domains are PCI based as the IOMMU API is still centered around a
24 * device/bus interface rather than a group interface.
25 */
26
27 #include <linux/compat.h>
28 #include <linux/device.h>
29 #include <linux/fs.h>
30 #include <linux/iommu.h>
31 #include <linux/module.h>
32 #include <linux/mm.h>
33 #include <linux/rbtree.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/uaccess.h>
37 #include <linux/vfio.h>
38 #include <linux/workqueue.h>
39 #include <linux/pid_namespace.h>
40 #include <linux/mdev.h>
41
42 #define DRIVER_VERSION "0.2"
43 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
44 #define DRIVER_DESC "Type1 IOMMU driver for VFIO"
45
46 static bool allow_unsafe_interrupts;
47 module_param_named(allow_unsafe_interrupts,
48 allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR);
49 MODULE_PARM_DESC(allow_unsafe_interrupts,
50 "Enable VFIO IOMMU support for on platforms without interrupt remapping support.");
51
52 static bool disable_hugepages;
53 module_param_named(disable_hugepages,
54 disable_hugepages, bool, S_IRUGO | S_IWUSR);
55 MODULE_PARM_DESC(disable_hugepages,
56 "Disable VFIO IOMMU support for IOMMU hugepages.");
57
58 struct vfio_iommu {
59 struct list_head domain_list;
60 struct vfio_domain *external_domain; /* domain for external user */
61 struct mutex lock;
62 struct rb_root dma_list;
63 bool v2;
64 bool nesting;
65 };
66
67 struct vfio_domain {
68 struct iommu_domain *domain;
69 struct list_head next;
70 struct list_head group_list;
71 int prot; /* IOMMU_CACHE */
72 bool fgsp; /* Fine-grained super pages */
73 };
74
75 struct vfio_dma {
76 struct rb_node node;
77 dma_addr_t iova; /* Device address */
78 unsigned long vaddr; /* Process virtual addr */
79 size_t size; /* Map size (bytes) */
80 int prot; /* IOMMU_READ/WRITE */
81 bool iommu_mapped;
82 struct task_struct *task;
83 struct rb_root pfn_list; /* Ex-user pinned pfn list */
84 };
85
86 struct vfio_group {
87 struct iommu_group *iommu_group;
88 struct list_head next;
89 };
90
91 /*
92 * Guest RAM pinning working set or DMA target
93 */
94 struct vfio_pfn {
95 struct rb_node node;
96 dma_addr_t iova; /* Device address */
97 unsigned long pfn; /* Host pfn */
98 atomic_t ref_count;
99 };
100
101 #define IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu) \
102 (!list_empty(&iommu->domain_list))
103
104 static int put_pfn(unsigned long pfn, int prot);
105
106 /*
107 * This code handles mapping and unmapping of user data buffers
108 * into DMA'ble space using the IOMMU
109 */
110
111 static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
112 dma_addr_t start, size_t size)
113 {
114 struct rb_node *node = iommu->dma_list.rb_node;
115
116 while (node) {
117 struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
118
119 if (start + size <= dma->iova)
120 node = node->rb_left;
121 else if (start >= dma->iova + dma->size)
122 node = node->rb_right;
123 else
124 return dma;
125 }
126
127 return NULL;
128 }
129
130 static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
131 {
132 struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
133 struct vfio_dma *dma;
134
135 while (*link) {
136 parent = *link;
137 dma = rb_entry(parent, struct vfio_dma, node);
138
139 if (new->iova + new->size <= dma->iova)
140 link = &(*link)->rb_left;
141 else
142 link = &(*link)->rb_right;
143 }
144
145 rb_link_node(&new->node, parent, link);
146 rb_insert_color(&new->node, &iommu->dma_list);
147 }
148
149 static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)
150 {
151 rb_erase(&old->node, &iommu->dma_list);
152 }
153
154 /*
155 * Helper Functions for host iova-pfn list
156 */
157 static struct vfio_pfn *vfio_find_vpfn(struct vfio_dma *dma, dma_addr_t iova)
158 {
159 struct vfio_pfn *vpfn;
160 struct rb_node *node = dma->pfn_list.rb_node;
161
162 while (node) {
163 vpfn = rb_entry(node, struct vfio_pfn, node);
164
165 if (iova < vpfn->iova)
166 node = node->rb_left;
167 else if (iova > vpfn->iova)
168 node = node->rb_right;
169 else
170 return vpfn;
171 }
172 return NULL;
173 }
174
175 static void vfio_link_pfn(struct vfio_dma *dma,
176 struct vfio_pfn *new)
177 {
178 struct rb_node **link, *parent = NULL;
179 struct vfio_pfn *vpfn;
180
181 link = &dma->pfn_list.rb_node;
182 while (*link) {
183 parent = *link;
184 vpfn = rb_entry(parent, struct vfio_pfn, node);
185
186 if (new->iova < vpfn->iova)
187 link = &(*link)->rb_left;
188 else
189 link = &(*link)->rb_right;
190 }
191
192 rb_link_node(&new->node, parent, link);
193 rb_insert_color(&new->node, &dma->pfn_list);
194 }
195
196 static void vfio_unlink_pfn(struct vfio_dma *dma, struct vfio_pfn *old)
197 {
198 rb_erase(&old->node, &dma->pfn_list);
199 }
200
201 static int vfio_add_to_pfn_list(struct vfio_dma *dma, dma_addr_t iova,
202 unsigned long pfn)
203 {
204 struct vfio_pfn *vpfn;
205
206 vpfn = kzalloc(sizeof(*vpfn), GFP_KERNEL);
207 if (!vpfn)
208 return -ENOMEM;
209
210 vpfn->iova = iova;
211 vpfn->pfn = pfn;
212 atomic_set(&vpfn->ref_count, 1);
213 vfio_link_pfn(dma, vpfn);
214 return 0;
215 }
216
217 static void vfio_remove_from_pfn_list(struct vfio_dma *dma,
218 struct vfio_pfn *vpfn)
219 {
220 vfio_unlink_pfn(dma, vpfn);
221 kfree(vpfn);
222 }
223
224 static struct vfio_pfn *vfio_iova_get_vfio_pfn(struct vfio_dma *dma,
225 unsigned long iova)
226 {
227 struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
228
229 if (vpfn)
230 atomic_inc(&vpfn->ref_count);
231 return vpfn;
232 }
233
234 static int vfio_iova_put_vfio_pfn(struct vfio_dma *dma, struct vfio_pfn *vpfn)
235 {
236 int ret = 0;
237
238 if (atomic_dec_and_test(&vpfn->ref_count)) {
239 ret = put_pfn(vpfn->pfn, dma->prot);
240 vfio_remove_from_pfn_list(dma, vpfn);
241 }
242 return ret;
243 }
244
245 struct vwork {
246 struct mm_struct *mm;
247 long npage;
248 struct work_struct work;
249 };
250
251 /* delayed decrement/increment for locked_vm */
252 static void vfio_lock_acct_bg(struct work_struct *work)
253 {
254 struct vwork *vwork = container_of(work, struct vwork, work);
255 struct mm_struct *mm;
256
257 mm = vwork->mm;
258 down_write(&mm->mmap_sem);
259 mm->locked_vm += vwork->npage;
260 up_write(&mm->mmap_sem);
261 mmput(mm);
262 kfree(vwork);
263 }
264
265 static void vfio_lock_acct(struct task_struct *task, long npage)
266 {
267 struct vwork *vwork;
268 struct mm_struct *mm;
269
270 if (!npage)
271 return;
272
273 mm = get_task_mm(task);
274 if (!mm)
275 return; /* process exited or nothing to do */
276
277 if (down_write_trylock(&mm->mmap_sem)) {
278 mm->locked_vm += npage;
279 up_write(&mm->mmap_sem);
280 mmput(mm);
281 return;
282 }
283
284 /*
285 * Couldn't get mmap_sem lock, so must setup to update
286 * mm->locked_vm later. If locked_vm were atomic, we
287 * wouldn't need this silliness
288 */
289 vwork = kmalloc(sizeof(struct vwork), GFP_KERNEL);
290 if (!vwork) {
291 mmput(mm);
292 return;
293 }
294 INIT_WORK(&vwork->work, vfio_lock_acct_bg);
295 vwork->mm = mm;
296 vwork->npage = npage;
297 schedule_work(&vwork->work);
298 }
299
300 /*
301 * Some mappings aren't backed by a struct page, for example an mmap'd
302 * MMIO range for our own or another device. These use a different
303 * pfn conversion and shouldn't be tracked as locked pages.
304 */
305 static bool is_invalid_reserved_pfn(unsigned long pfn)
306 {
307 if (pfn_valid(pfn)) {
308 bool reserved;
309 struct page *tail = pfn_to_page(pfn);
310 struct page *head = compound_head(tail);
311 reserved = !!(PageReserved(head));
312 if (head != tail) {
313 /*
314 * "head" is not a dangling pointer
315 * (compound_head takes care of that)
316 * but the hugepage may have been split
317 * from under us (and we may not hold a
318 * reference count on the head page so it can
319 * be reused before we run PageReferenced), so
320 * we've to check PageTail before returning
321 * what we just read.
322 */
323 smp_rmb();
324 if (PageTail(tail))
325 return reserved;
326 }
327 return PageReserved(tail);
328 }
329
330 return true;
331 }
332
333 static int put_pfn(unsigned long pfn, int prot)
334 {
335 if (!is_invalid_reserved_pfn(pfn)) {
336 struct page *page = pfn_to_page(pfn);
337 if (prot & IOMMU_WRITE)
338 SetPageDirty(page);
339 put_page(page);
340 return 1;
341 }
342 return 0;
343 }
344
345 static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr,
346 int prot, unsigned long *pfn)
347 {
348 struct page *page[1];
349 struct vm_area_struct *vma;
350 int ret;
351
352 if (mm == current->mm) {
353 ret = get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE),
354 page);
355 } else {
356 unsigned int flags = 0;
357
358 if (prot & IOMMU_WRITE)
359 flags |= FOLL_WRITE;
360
361 down_read(&mm->mmap_sem);
362 ret = get_user_pages_remote(NULL, mm, vaddr, 1, flags, page,
363 NULL);
364 up_read(&mm->mmap_sem);
365 }
366
367 if (ret == 1) {
368 *pfn = page_to_pfn(page[0]);
369 return 0;
370 }
371
372 down_read(&mm->mmap_sem);
373
374 vma = find_vma_intersection(mm, vaddr, vaddr + 1);
375
376 if (vma && vma->vm_flags & VM_PFNMAP) {
377 *pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
378 if (is_invalid_reserved_pfn(*pfn))
379 ret = 0;
380 }
381
382 up_read(&mm->mmap_sem);
383 return ret;
384 }
385
386 /*
387 * Attempt to pin pages. We really don't want to track all the pfns and
388 * the iommu can only map chunks of consecutive pfns anyway, so get the
389 * first page and all consecutive pages with the same locking.
390 */
391 static long vfio_pin_pages_remote(struct vfio_dma *dma, unsigned long vaddr,
392 long npage, unsigned long *pfn_base)
393 {
394 unsigned long limit;
395 bool lock_cap = ns_capable(task_active_pid_ns(dma->task)->user_ns,
396 CAP_IPC_LOCK);
397 struct mm_struct *mm;
398 long ret, i = 0, lock_acct = 0;
399 bool rsvd;
400 dma_addr_t iova = vaddr - dma->vaddr + dma->iova;
401
402 mm = get_task_mm(dma->task);
403 if (!mm)
404 return -ENODEV;
405
406 ret = vaddr_get_pfn(mm, vaddr, dma->prot, pfn_base);
407 if (ret)
408 goto pin_pg_remote_exit;
409
410 rsvd = is_invalid_reserved_pfn(*pfn_base);
411 limit = task_rlimit(dma->task, RLIMIT_MEMLOCK) >> PAGE_SHIFT;
412
413 /*
414 * Reserved pages aren't counted against the user, externally pinned
415 * pages are already counted against the user.
416 */
417 if (!rsvd && !vfio_find_vpfn(dma, iova)) {
418 if (!lock_cap && mm->locked_vm + 1 > limit) {
419 put_pfn(*pfn_base, dma->prot);
420 pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__,
421 limit << PAGE_SHIFT);
422 ret = -ENOMEM;
423 goto pin_pg_remote_exit;
424 }
425 lock_acct++;
426 }
427
428 i++;
429 if (likely(!disable_hugepages)) {
430 /* Lock all the consecutive pages from pfn_base */
431 for (vaddr += PAGE_SIZE, iova += PAGE_SIZE; i < npage;
432 i++, vaddr += PAGE_SIZE, iova += PAGE_SIZE) {
433 unsigned long pfn = 0;
434
435 ret = vaddr_get_pfn(mm, vaddr, dma->prot, &pfn);
436 if (ret)
437 break;
438
439 if (pfn != *pfn_base + i ||
440 rsvd != is_invalid_reserved_pfn(pfn)) {
441 put_pfn(pfn, dma->prot);
442 break;
443 }
444
445 if (!rsvd && !vfio_find_vpfn(dma, iova)) {
446 if (!lock_cap &&
447 mm->locked_vm + lock_acct + 1 > limit) {
448 put_pfn(pfn, dma->prot);
449 pr_warn("%s: RLIMIT_MEMLOCK (%ld) "
450 "exceeded\n", __func__,
451 limit << PAGE_SHIFT);
452 break;
453 }
454 lock_acct++;
455 }
456 }
457 }
458
459 vfio_lock_acct(dma->task, lock_acct);
460 ret = i;
461
462 pin_pg_remote_exit:
463 mmput(mm);
464 return ret;
465 }
466
467 static long vfio_unpin_pages_remote(struct vfio_dma *dma, dma_addr_t iova,
468 unsigned long pfn, long npage,
469 bool do_accounting)
470 {
471 long unlocked = 0, locked = 0;
472 long i;
473
474 for (i = 0; i < npage; i++) {
475 if (put_pfn(pfn++, dma->prot)) {
476 unlocked++;
477 if (vfio_find_vpfn(dma, iova + (i << PAGE_SHIFT)))
478 locked++;
479 }
480 }
481
482 if (do_accounting)
483 vfio_lock_acct(dma->task, locked - unlocked);
484
485 return unlocked;
486 }
487
488 static int vfio_pin_page_external(struct vfio_dma *dma, unsigned long vaddr,
489 unsigned long *pfn_base, bool do_accounting)
490 {
491 unsigned long limit;
492 bool lock_cap = ns_capable(task_active_pid_ns(dma->task)->user_ns,
493 CAP_IPC_LOCK);
494 struct mm_struct *mm;
495 int ret;
496 bool rsvd;
497
498 mm = get_task_mm(dma->task);
499 if (!mm)
500 return -ENODEV;
501
502 ret = vaddr_get_pfn(mm, vaddr, dma->prot, pfn_base);
503 if (ret)
504 goto pin_page_exit;
505
506 rsvd = is_invalid_reserved_pfn(*pfn_base);
507 limit = task_rlimit(dma->task, RLIMIT_MEMLOCK) >> PAGE_SHIFT;
508
509 if (!rsvd && !lock_cap && mm->locked_vm + 1 > limit) {
510 put_pfn(*pfn_base, dma->prot);
511 pr_warn("%s: Task %s (%d) RLIMIT_MEMLOCK (%ld) exceeded\n",
512 __func__, dma->task->comm, task_pid_nr(dma->task),
513 limit << PAGE_SHIFT);
514 ret = -ENOMEM;
515 goto pin_page_exit;
516 }
517
518 if (!rsvd && do_accounting)
519 vfio_lock_acct(dma->task, 1);
520 ret = 1;
521
522 pin_page_exit:
523 mmput(mm);
524 return ret;
525 }
526
527 static int vfio_unpin_page_external(struct vfio_dma *dma, dma_addr_t iova,
528 bool do_accounting)
529 {
530 int unlocked;
531 struct vfio_pfn *vpfn = vfio_find_vpfn(dma, iova);
532
533 if (!vpfn)
534 return 0;
535
536 unlocked = vfio_iova_put_vfio_pfn(dma, vpfn);
537
538 if (do_accounting)
539 vfio_lock_acct(dma->task, -unlocked);
540
541 return unlocked;
542 }
543
544 static int vfio_iommu_type1_pin_pages(void *iommu_data,
545 unsigned long *user_pfn,
546 int npage, int prot,
547 unsigned long *phys_pfn)
548 {
549 struct vfio_iommu *iommu = iommu_data;
550 int i, j, ret;
551 unsigned long remote_vaddr;
552 struct vfio_dma *dma;
553 bool do_accounting;
554
555 if (!iommu || !user_pfn || !phys_pfn)
556 return -EINVAL;
557
558 /* Supported for v2 version only */
559 if (!iommu->v2)
560 return -EACCES;
561
562 mutex_lock(&iommu->lock);
563
564 if (!iommu->external_domain) {
565 ret = -EINVAL;
566 goto pin_done;
567 }
568
569 /*
570 * If iommu capable domain exist in the container then all pages are
571 * already pinned and accounted. Accouting should be done if there is no
572 * iommu capable domain in the container.
573 */
574 do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu);
575
576 for (i = 0; i < npage; i++) {
577 dma_addr_t iova;
578 struct vfio_pfn *vpfn;
579
580 iova = user_pfn[i] << PAGE_SHIFT;
581 dma = vfio_find_dma(iommu, iova, 0);
582 if (!dma) {
583 ret = -EINVAL;
584 goto pin_unwind;
585 }
586
587 if ((dma->prot & prot) != prot) {
588 ret = -EPERM;
589 goto pin_unwind;
590 }
591
592 vpfn = vfio_iova_get_vfio_pfn(dma, iova);
593 if (vpfn) {
594 phys_pfn[i] = vpfn->pfn;
595 continue;
596 }
597
598 remote_vaddr = dma->vaddr + iova - dma->iova;
599 ret = vfio_pin_page_external(dma, remote_vaddr, &phys_pfn[i],
600 do_accounting);
601 if (ret <= 0) {
602 WARN_ON(!ret);
603 goto pin_unwind;
604 }
605
606 ret = vfio_add_to_pfn_list(dma, iova, phys_pfn[i]);
607 if (ret) {
608 vfio_unpin_page_external(dma, iova, do_accounting);
609 goto pin_unwind;
610 }
611 }
612
613 ret = i;
614 goto pin_done;
615
616 pin_unwind:
617 phys_pfn[i] = 0;
618 for (j = 0; j < i; j++) {
619 dma_addr_t iova;
620
621 iova = user_pfn[j] << PAGE_SHIFT;
622 dma = vfio_find_dma(iommu, iova, 0);
623 vfio_unpin_page_external(dma, iova, do_accounting);
624 phys_pfn[j] = 0;
625 }
626 pin_done:
627 mutex_unlock(&iommu->lock);
628 return ret;
629 }
630
631 static int vfio_iommu_type1_unpin_pages(void *iommu_data,
632 unsigned long *user_pfn,
633 int npage)
634 {
635 struct vfio_iommu *iommu = iommu_data;
636 bool do_accounting;
637 int i;
638
639 if (!iommu || !user_pfn)
640 return -EINVAL;
641
642 /* Supported for v2 version only */
643 if (!iommu->v2)
644 return -EACCES;
645
646 mutex_lock(&iommu->lock);
647
648 if (!iommu->external_domain) {
649 mutex_unlock(&iommu->lock);
650 return -EINVAL;
651 }
652
653 do_accounting = !IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu);
654 for (i = 0; i < npage; i++) {
655 struct vfio_dma *dma;
656 dma_addr_t iova;
657
658 iova = user_pfn[i] << PAGE_SHIFT;
659 dma = vfio_find_dma(iommu, iova, 0);
660 if (!dma)
661 goto unpin_exit;
662 vfio_unpin_page_external(dma, iova, do_accounting);
663 }
664
665 unpin_exit:
666 mutex_unlock(&iommu->lock);
667 return i > npage ? npage : (i > 0 ? i : -EINVAL);
668 }
669
670 static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma,
671 bool do_accounting)
672 {
673 dma_addr_t iova = dma->iova, end = dma->iova + dma->size;
674 struct vfio_domain *domain, *d;
675 long unlocked = 0;
676
677 if (!dma->size)
678 return 0;
679
680 if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
681 return 0;
682
683 /*
684 * We use the IOMMU to track the physical addresses, otherwise we'd
685 * need a much more complicated tracking system. Unfortunately that
686 * means we need to use one of the iommu domains to figure out the
687 * pfns to unpin. The rest need to be unmapped in advance so we have
688 * no iommu translations remaining when the pages are unpinned.
689 */
690 domain = d = list_first_entry(&iommu->domain_list,
691 struct vfio_domain, next);
692
693 list_for_each_entry_continue(d, &iommu->domain_list, next) {
694 iommu_unmap(d->domain, dma->iova, dma->size);
695 cond_resched();
696 }
697
698 while (iova < end) {
699 size_t unmapped, len;
700 phys_addr_t phys, next;
701
702 phys = iommu_iova_to_phys(domain->domain, iova);
703 if (WARN_ON(!phys)) {
704 iova += PAGE_SIZE;
705 continue;
706 }
707
708 /*
709 * To optimize for fewer iommu_unmap() calls, each of which
710 * may require hardware cache flushing, try to find the
711 * largest contiguous physical memory chunk to unmap.
712 */
713 for (len = PAGE_SIZE;
714 !domain->fgsp && iova + len < end; len += PAGE_SIZE) {
715 next = iommu_iova_to_phys(domain->domain, iova + len);
716 if (next != phys + len)
717 break;
718 }
719
720 unmapped = iommu_unmap(domain->domain, iova, len);
721 if (WARN_ON(!unmapped))
722 break;
723
724 unlocked += vfio_unpin_pages_remote(dma, iova,
725 phys >> PAGE_SHIFT,
726 unmapped >> PAGE_SHIFT,
727 false);
728 iova += unmapped;
729
730 cond_resched();
731 }
732
733 dma->iommu_mapped = false;
734 if (do_accounting) {
735 vfio_lock_acct(dma->task, -unlocked);
736 return 0;
737 }
738 return unlocked;
739 }
740
741 static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma)
742 {
743 vfio_unmap_unpin(iommu, dma, true);
744 vfio_unlink_dma(iommu, dma);
745 put_task_struct(dma->task);
746 kfree(dma);
747 }
748
749 static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)
750 {
751 struct vfio_domain *domain;
752 unsigned long bitmap = ULONG_MAX;
753
754 mutex_lock(&iommu->lock);
755 list_for_each_entry(domain, &iommu->domain_list, next)
756 bitmap &= domain->domain->pgsize_bitmap;
757 mutex_unlock(&iommu->lock);
758
759 /*
760 * In case the IOMMU supports page sizes smaller than PAGE_SIZE
761 * we pretend PAGE_SIZE is supported and hide sub-PAGE_SIZE sizes.
762 * That way the user will be able to map/unmap buffers whose size/
763 * start address is aligned with PAGE_SIZE. Pinning code uses that
764 * granularity while iommu driver can use the sub-PAGE_SIZE size
765 * to map the buffer.
766 */
767 if (bitmap & ~PAGE_MASK) {
768 bitmap &= PAGE_MASK;
769 bitmap |= PAGE_SIZE;
770 }
771
772 return bitmap;
773 }
774
775 static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
776 struct vfio_iommu_type1_dma_unmap *unmap)
777 {
778 uint64_t mask;
779 struct vfio_dma *dma;
780 size_t unmapped = 0;
781 int ret = 0;
782
783 mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1;
784
785 if (unmap->iova & mask)
786 return -EINVAL;
787 if (!unmap->size || unmap->size & mask)
788 return -EINVAL;
789
790 WARN_ON(mask & PAGE_MASK);
791
792 mutex_lock(&iommu->lock);
793
794 /*
795 * vfio-iommu-type1 (v1) - User mappings were coalesced together to
796 * avoid tracking individual mappings. This means that the granularity
797 * of the original mapping was lost and the user was allowed to attempt
798 * to unmap any range. Depending on the contiguousness of physical
799 * memory and page sizes supported by the IOMMU, arbitrary unmaps may
800 * or may not have worked. We only guaranteed unmap granularity
801 * matching the original mapping; even though it was untracked here,
802 * the original mappings are reflected in IOMMU mappings. This
803 * resulted in a couple unusual behaviors. First, if a range is not
804 * able to be unmapped, ex. a set of 4k pages that was mapped as a
805 * 2M hugepage into the IOMMU, the unmap ioctl returns success but with
806 * a zero sized unmap. Also, if an unmap request overlaps the first
807 * address of a hugepage, the IOMMU will unmap the entire hugepage.
808 * This also returns success and the returned unmap size reflects the
809 * actual size unmapped.
810 *
811 * We attempt to maintain compatibility with this "v1" interface, but
812 * we take control out of the hands of the IOMMU. Therefore, an unmap
813 * request offset from the beginning of the original mapping will
814 * return success with zero sized unmap. And an unmap request covering
815 * the first iova of mapping will unmap the entire range.
816 *
817 * The v2 version of this interface intends to be more deterministic.
818 * Unmap requests must fully cover previous mappings. Multiple
819 * mappings may still be unmaped by specifying large ranges, but there
820 * must not be any previous mappings bisected by the range. An error
821 * will be returned if these conditions are not met. The v2 interface
822 * will only return success and a size of zero if there were no
823 * mappings within the range.
824 */
825 if (iommu->v2) {
826 dma = vfio_find_dma(iommu, unmap->iova, 0);
827 if (dma && dma->iova != unmap->iova) {
828 ret = -EINVAL;
829 goto unlock;
830 }
831 dma = vfio_find_dma(iommu, unmap->iova + unmap->size - 1, 0);
832 if (dma && dma->iova + dma->size != unmap->iova + unmap->size) {
833 ret = -EINVAL;
834 goto unlock;
835 }
836 }
837
838 while ((dma = vfio_find_dma(iommu, unmap->iova, unmap->size))) {
839 if (!iommu->v2 && unmap->iova > dma->iova)
840 break;
841 /*
842 * Task with same address space who mapped this iova range is
843 * allowed to unmap the iova range.
844 */
845 if (dma->task->mm != current->mm)
846 break;
847 unmapped += dma->size;
848 vfio_remove_dma(iommu, dma);
849 }
850
851 unlock:
852 mutex_unlock(&iommu->lock);
853
854 /* Report how much was unmapped */
855 unmap->size = unmapped;
856
857 return ret;
858 }
859
860 /*
861 * Turns out AMD IOMMU has a page table bug where it won't map large pages
862 * to a region that previously mapped smaller pages. This should be fixed
863 * soon, so this is just a temporary workaround to break mappings down into
864 * PAGE_SIZE. Better to map smaller pages than nothing.
865 */
866 static int map_try_harder(struct vfio_domain *domain, dma_addr_t iova,
867 unsigned long pfn, long npage, int prot)
868 {
869 long i;
870 int ret = 0;
871
872 for (i = 0; i < npage; i++, pfn++, iova += PAGE_SIZE) {
873 ret = iommu_map(domain->domain, iova,
874 (phys_addr_t)pfn << PAGE_SHIFT,
875 PAGE_SIZE, prot | domain->prot);
876 if (ret)
877 break;
878 }
879
880 for (; i < npage && i > 0; i--, iova -= PAGE_SIZE)
881 iommu_unmap(domain->domain, iova, PAGE_SIZE);
882
883 return ret;
884 }
885
886 static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova,
887 unsigned long pfn, long npage, int prot)
888 {
889 struct vfio_domain *d;
890 int ret;
891
892 list_for_each_entry(d, &iommu->domain_list, next) {
893 ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT,
894 npage << PAGE_SHIFT, prot | d->prot);
895 if (ret) {
896 if (ret != -EBUSY ||
897 map_try_harder(d, iova, pfn, npage, prot))
898 goto unwind;
899 }
900
901 cond_resched();
902 }
903
904 return 0;
905
906 unwind:
907 list_for_each_entry_continue_reverse(d, &iommu->domain_list, next)
908 iommu_unmap(d->domain, iova, npage << PAGE_SHIFT);
909
910 return ret;
911 }
912
913 static int vfio_pin_map_dma(struct vfio_iommu *iommu, struct vfio_dma *dma,
914 size_t map_size)
915 {
916 dma_addr_t iova = dma->iova;
917 unsigned long vaddr = dma->vaddr;
918 size_t size = map_size;
919 long npage;
920 unsigned long pfn;
921 int ret = 0;
922
923 while (size) {
924 /* Pin a contiguous chunk of memory */
925 npage = vfio_pin_pages_remote(dma, vaddr + dma->size,
926 size >> PAGE_SHIFT, &pfn);
927 if (npage <= 0) {
928 WARN_ON(!npage);
929 ret = (int)npage;
930 break;
931 }
932
933 /* Map it! */
934 ret = vfio_iommu_map(iommu, iova + dma->size, pfn, npage,
935 dma->prot);
936 if (ret) {
937 vfio_unpin_pages_remote(dma, iova + dma->size, pfn,
938 npage, true);
939 break;
940 }
941
942 size -= npage << PAGE_SHIFT;
943 dma->size += npage << PAGE_SHIFT;
944 }
945
946 dma->iommu_mapped = true;
947
948 if (ret)
949 vfio_remove_dma(iommu, dma);
950
951 return ret;
952 }
953
954 static int vfio_dma_do_map(struct vfio_iommu *iommu,
955 struct vfio_iommu_type1_dma_map *map)
956 {
957 dma_addr_t iova = map->iova;
958 unsigned long vaddr = map->vaddr;
959 size_t size = map->size;
960 int ret = 0, prot = 0;
961 uint64_t mask;
962 struct vfio_dma *dma;
963
964 /* Verify that none of our __u64 fields overflow */
965 if (map->size != size || map->vaddr != vaddr || map->iova != iova)
966 return -EINVAL;
967
968 mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1;
969
970 WARN_ON(mask & PAGE_MASK);
971
972 /* READ/WRITE from device perspective */
973 if (map->flags & VFIO_DMA_MAP_FLAG_WRITE)
974 prot |= IOMMU_WRITE;
975 if (map->flags & VFIO_DMA_MAP_FLAG_READ)
976 prot |= IOMMU_READ;
977
978 if (!prot || !size || (size | iova | vaddr) & mask)
979 return -EINVAL;
980
981 /* Don't allow IOVA or virtual address wrap */
982 if (iova + size - 1 < iova || vaddr + size - 1 < vaddr)
983 return -EINVAL;
984
985 mutex_lock(&iommu->lock);
986
987 if (vfio_find_dma(iommu, iova, size)) {
988 ret = -EEXIST;
989 goto out_unlock;
990 }
991
992 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
993 if (!dma) {
994 ret = -ENOMEM;
995 goto out_unlock;
996 }
997
998 dma->iova = iova;
999 dma->vaddr = vaddr;
1000 dma->prot = prot;
1001 get_task_struct(current);
1002 dma->task = current;
1003 dma->pfn_list = RB_ROOT;
1004
1005 /* Insert zero-sized and grow as we map chunks of it */
1006 vfio_link_dma(iommu, dma);
1007
1008 /* Don't pin and map if container doesn't contain IOMMU capable domain*/
1009 if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
1010 dma->size = size;
1011 else
1012 ret = vfio_pin_map_dma(iommu, dma, size);
1013
1014 out_unlock:
1015 mutex_unlock(&iommu->lock);
1016 return ret;
1017 }
1018
1019 static int vfio_bus_type(struct device *dev, void *data)
1020 {
1021 struct bus_type **bus = data;
1022
1023 if (*bus && *bus != dev->bus)
1024 return -EINVAL;
1025
1026 *bus = dev->bus;
1027
1028 return 0;
1029 }
1030
1031 static int vfio_iommu_replay(struct vfio_iommu *iommu,
1032 struct vfio_domain *domain)
1033 {
1034 struct vfio_domain *d;
1035 struct rb_node *n;
1036 int ret;
1037
1038 /* Arbitrarily pick the first domain in the list for lookups */
1039 d = list_first_entry(&iommu->domain_list, struct vfio_domain, next);
1040 n = rb_first(&iommu->dma_list);
1041
1042 for (; n; n = rb_next(n)) {
1043 struct vfio_dma *dma;
1044 dma_addr_t iova;
1045
1046 dma = rb_entry(n, struct vfio_dma, node);
1047 iova = dma->iova;
1048
1049 while (iova < dma->iova + dma->size) {
1050 phys_addr_t phys;
1051 size_t size;
1052
1053 if (dma->iommu_mapped) {
1054 phys_addr_t p;
1055 dma_addr_t i;
1056
1057 phys = iommu_iova_to_phys(d->domain, iova);
1058
1059 if (WARN_ON(!phys)) {
1060 iova += PAGE_SIZE;
1061 continue;
1062 }
1063
1064 size = PAGE_SIZE;
1065 p = phys + size;
1066 i = iova + size;
1067 while (i < dma->iova + dma->size &&
1068 p == iommu_iova_to_phys(d->domain, i)) {
1069 size += PAGE_SIZE;
1070 p += PAGE_SIZE;
1071 i += PAGE_SIZE;
1072 }
1073 } else {
1074 unsigned long pfn;
1075 unsigned long vaddr = dma->vaddr +
1076 (iova - dma->iova);
1077 size_t n = dma->iova + dma->size - iova;
1078 long npage;
1079
1080 npage = vfio_pin_pages_remote(dma, vaddr,
1081 n >> PAGE_SHIFT,
1082 &pfn);
1083 if (npage <= 0) {
1084 WARN_ON(!npage);
1085 ret = (int)npage;
1086 return ret;
1087 }
1088
1089 phys = pfn << PAGE_SHIFT;
1090 size = npage << PAGE_SHIFT;
1091 }
1092
1093 ret = iommu_map(domain->domain, iova, phys,
1094 size, dma->prot | domain->prot);
1095 if (ret)
1096 return ret;
1097
1098 iova += size;
1099 }
1100 dma->iommu_mapped = true;
1101 }
1102 return 0;
1103 }
1104
1105 /*
1106 * We change our unmap behavior slightly depending on whether the IOMMU
1107 * supports fine-grained superpages. IOMMUs like AMD-Vi will use a superpage
1108 * for practically any contiguous power-of-two mapping we give it. This means
1109 * we don't need to look for contiguous chunks ourselves to make unmapping
1110 * more efficient. On IOMMUs with coarse-grained super pages, like Intel VT-d
1111 * with discrete 2M/1G/512G/1T superpages, identifying contiguous chunks
1112 * significantly boosts non-hugetlbfs mappings and doesn't seem to hurt when
1113 * hugetlbfs is in use.
1114 */
1115 static void vfio_test_domain_fgsp(struct vfio_domain *domain)
1116 {
1117 struct page *pages;
1118 int ret, order = get_order(PAGE_SIZE * 2);
1119
1120 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
1121 if (!pages)
1122 return;
1123
1124 ret = iommu_map(domain->domain, 0, page_to_phys(pages), PAGE_SIZE * 2,
1125 IOMMU_READ | IOMMU_WRITE | domain->prot);
1126 if (!ret) {
1127 size_t unmapped = iommu_unmap(domain->domain, 0, PAGE_SIZE);
1128
1129 if (unmapped == PAGE_SIZE)
1130 iommu_unmap(domain->domain, PAGE_SIZE, PAGE_SIZE);
1131 else
1132 domain->fgsp = true;
1133 }
1134
1135 __free_pages(pages, order);
1136 }
1137
1138 static struct vfio_group *find_iommu_group(struct vfio_domain *domain,
1139 struct iommu_group *iommu_group)
1140 {
1141 struct vfio_group *g;
1142
1143 list_for_each_entry(g, &domain->group_list, next) {
1144 if (g->iommu_group == iommu_group)
1145 return g;
1146 }
1147
1148 return NULL;
1149 }
1150
1151 static int vfio_iommu_type1_attach_group(void *iommu_data,
1152 struct iommu_group *iommu_group)
1153 {
1154 struct vfio_iommu *iommu = iommu_data;
1155 struct vfio_group *group;
1156 struct vfio_domain *domain, *d;
1157 struct bus_type *bus = NULL, *mdev_bus;
1158 int ret;
1159
1160 mutex_lock(&iommu->lock);
1161
1162 list_for_each_entry(d, &iommu->domain_list, next) {
1163 if (find_iommu_group(d, iommu_group)) {
1164 mutex_unlock(&iommu->lock);
1165 return -EINVAL;
1166 }
1167 }
1168
1169 if (iommu->external_domain) {
1170 if (find_iommu_group(iommu->external_domain, iommu_group)) {
1171 mutex_unlock(&iommu->lock);
1172 return -EINVAL;
1173 }
1174 }
1175
1176 group = kzalloc(sizeof(*group), GFP_KERNEL);
1177 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
1178 if (!group || !domain) {
1179 ret = -ENOMEM;
1180 goto out_free;
1181 }
1182
1183 group->iommu_group = iommu_group;
1184
1185 /* Determine bus_type in order to allocate a domain */
1186 ret = iommu_group_for_each_dev(iommu_group, &bus, vfio_bus_type);
1187 if (ret)
1188 goto out_free;
1189
1190 mdev_bus = symbol_get(mdev_bus_type);
1191
1192 if (mdev_bus) {
1193 if ((bus == mdev_bus) && !iommu_present(bus)) {
1194 symbol_put(mdev_bus_type);
1195 if (!iommu->external_domain) {
1196 INIT_LIST_HEAD(&domain->group_list);
1197 iommu->external_domain = domain;
1198 } else
1199 kfree(domain);
1200
1201 list_add(&group->next,
1202 &iommu->external_domain->group_list);
1203 mutex_unlock(&iommu->lock);
1204 return 0;
1205 }
1206 symbol_put(mdev_bus_type);
1207 }
1208
1209 domain->domain = iommu_domain_alloc(bus);
1210 if (!domain->domain) {
1211 ret = -EIO;
1212 goto out_free;
1213 }
1214
1215 if (iommu->nesting) {
1216 int attr = 1;
1217
1218 ret = iommu_domain_set_attr(domain->domain, DOMAIN_ATTR_NESTING,
1219 &attr);
1220 if (ret)
1221 goto out_domain;
1222 }
1223
1224 ret = iommu_attach_group(domain->domain, iommu_group);
1225 if (ret)
1226 goto out_domain;
1227
1228 INIT_LIST_HEAD(&domain->group_list);
1229 list_add(&group->next, &domain->group_list);
1230
1231 if (!allow_unsafe_interrupts &&
1232 !iommu_capable(bus, IOMMU_CAP_INTR_REMAP)) {
1233 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
1234 __func__);
1235 ret = -EPERM;
1236 goto out_detach;
1237 }
1238
1239 if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
1240 domain->prot |= IOMMU_CACHE;
1241
1242 /*
1243 * Try to match an existing compatible domain. We don't want to
1244 * preclude an IOMMU driver supporting multiple bus_types and being
1245 * able to include different bus_types in the same IOMMU domain, so
1246 * we test whether the domains use the same iommu_ops rather than
1247 * testing if they're on the same bus_type.
1248 */
1249 list_for_each_entry(d, &iommu->domain_list, next) {
1250 if (d->domain->ops == domain->domain->ops &&
1251 d->prot == domain->prot) {
1252 iommu_detach_group(domain->domain, iommu_group);
1253 if (!iommu_attach_group(d->domain, iommu_group)) {
1254 list_add(&group->next, &d->group_list);
1255 iommu_domain_free(domain->domain);
1256 kfree(domain);
1257 mutex_unlock(&iommu->lock);
1258 return 0;
1259 }
1260
1261 ret = iommu_attach_group(domain->domain, iommu_group);
1262 if (ret)
1263 goto out_domain;
1264 }
1265 }
1266
1267 vfio_test_domain_fgsp(domain);
1268
1269 /* replay mappings on new domains */
1270 ret = vfio_iommu_replay(iommu, domain);
1271 if (ret)
1272 goto out_detach;
1273
1274 list_add(&domain->next, &iommu->domain_list);
1275
1276 mutex_unlock(&iommu->lock);
1277
1278 return 0;
1279
1280 out_detach:
1281 iommu_detach_group(domain->domain, iommu_group);
1282 out_domain:
1283 iommu_domain_free(domain->domain);
1284 out_free:
1285 kfree(domain);
1286 kfree(group);
1287 mutex_unlock(&iommu->lock);
1288 return ret;
1289 }
1290
1291 static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu)
1292 {
1293 struct rb_node *node;
1294
1295 while ((node = rb_first(&iommu->dma_list)))
1296 vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node));
1297 }
1298
1299 static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu)
1300 {
1301 struct rb_node *n, *p;
1302
1303 n = rb_first(&iommu->dma_list);
1304 for (; n; n = rb_next(n)) {
1305 struct vfio_dma *dma;
1306 long locked = 0, unlocked = 0;
1307
1308 dma = rb_entry(n, struct vfio_dma, node);
1309 unlocked += vfio_unmap_unpin(iommu, dma, false);
1310 p = rb_first(&dma->pfn_list);
1311 for (; p; p = rb_next(p)) {
1312 struct vfio_pfn *vpfn = rb_entry(p, struct vfio_pfn,
1313 node);
1314
1315 if (!is_invalid_reserved_pfn(vpfn->pfn))
1316 locked++;
1317 }
1318 vfio_lock_acct(dma->task, locked - unlocked);
1319 }
1320 }
1321
1322 static void vfio_sanity_check_pfn_list(struct vfio_iommu *iommu)
1323 {
1324 struct rb_node *n;
1325
1326 n = rb_first(&iommu->dma_list);
1327 for (; n; n = rb_next(n)) {
1328 struct vfio_dma *dma;
1329
1330 dma = rb_entry(n, struct vfio_dma, node);
1331
1332 if (WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list)))
1333 break;
1334 }
1335 }
1336
1337 static void vfio_iommu_type1_detach_group(void *iommu_data,
1338 struct iommu_group *iommu_group)
1339 {
1340 struct vfio_iommu *iommu = iommu_data;
1341 struct vfio_domain *domain;
1342 struct vfio_group *group;
1343
1344 mutex_lock(&iommu->lock);
1345
1346 if (iommu->external_domain) {
1347 group = find_iommu_group(iommu->external_domain, iommu_group);
1348 if (group) {
1349 list_del(&group->next);
1350 kfree(group);
1351
1352 if (list_empty(&iommu->external_domain->group_list)) {
1353 vfio_sanity_check_pfn_list(iommu);
1354
1355 if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu))
1356 vfio_iommu_unmap_unpin_all(iommu);
1357
1358 kfree(iommu->external_domain);
1359 iommu->external_domain = NULL;
1360 }
1361 goto detach_group_done;
1362 }
1363 }
1364
1365 list_for_each_entry(domain, &iommu->domain_list, next) {
1366 group = find_iommu_group(domain, iommu_group);
1367 if (!group)
1368 continue;
1369
1370 iommu_detach_group(domain->domain, iommu_group);
1371 list_del(&group->next);
1372 kfree(group);
1373 /*
1374 * Group ownership provides privilege, if the group list is
1375 * empty, the domain goes away. If it's the last domain with
1376 * iommu and external domain doesn't exist, then all the
1377 * mappings go away too. If it's the last domain with iommu and
1378 * external domain exist, update accounting
1379 */
1380 if (list_empty(&domain->group_list)) {
1381 if (list_is_singular(&iommu->domain_list)) {
1382 if (!iommu->external_domain)
1383 vfio_iommu_unmap_unpin_all(iommu);
1384 else
1385 vfio_iommu_unmap_unpin_reaccount(iommu);
1386 }
1387 iommu_domain_free(domain->domain);
1388 list_del(&domain->next);
1389 kfree(domain);
1390 }
1391 break;
1392 }
1393
1394 detach_group_done:
1395 mutex_unlock(&iommu->lock);
1396 }
1397
1398 static void *vfio_iommu_type1_open(unsigned long arg)
1399 {
1400 struct vfio_iommu *iommu;
1401
1402 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
1403 if (!iommu)
1404 return ERR_PTR(-ENOMEM);
1405
1406 switch (arg) {
1407 case VFIO_TYPE1_IOMMU:
1408 break;
1409 case VFIO_TYPE1_NESTING_IOMMU:
1410 iommu->nesting = true;
1411 case VFIO_TYPE1v2_IOMMU:
1412 iommu->v2 = true;
1413 break;
1414 default:
1415 kfree(iommu);
1416 return ERR_PTR(-EINVAL);
1417 }
1418
1419 INIT_LIST_HEAD(&iommu->domain_list);
1420 iommu->dma_list = RB_ROOT;
1421 mutex_init(&iommu->lock);
1422
1423 return iommu;
1424 }
1425
1426 static void vfio_release_domain(struct vfio_domain *domain, bool external)
1427 {
1428 struct vfio_group *group, *group_tmp;
1429
1430 list_for_each_entry_safe(group, group_tmp,
1431 &domain->group_list, next) {
1432 if (!external)
1433 iommu_detach_group(domain->domain, group->iommu_group);
1434 list_del(&group->next);
1435 kfree(group);
1436 }
1437
1438 if (!external)
1439 iommu_domain_free(domain->domain);
1440 }
1441
1442 static void vfio_iommu_type1_release(void *iommu_data)
1443 {
1444 struct vfio_iommu *iommu = iommu_data;
1445 struct vfio_domain *domain, *domain_tmp;
1446
1447 if (iommu->external_domain) {
1448 vfio_release_domain(iommu->external_domain, true);
1449 vfio_sanity_check_pfn_list(iommu);
1450 kfree(iommu->external_domain);
1451 }
1452
1453 vfio_iommu_unmap_unpin_all(iommu);
1454
1455 list_for_each_entry_safe(domain, domain_tmp,
1456 &iommu->domain_list, next) {
1457 vfio_release_domain(domain, false);
1458 list_del(&domain->next);
1459 kfree(domain);
1460 }
1461 kfree(iommu);
1462 }
1463
1464 static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)
1465 {
1466 struct vfio_domain *domain;
1467 int ret = 1;
1468
1469 mutex_lock(&iommu->lock);
1470 list_for_each_entry(domain, &iommu->domain_list, next) {
1471 if (!(domain->prot & IOMMU_CACHE)) {
1472 ret = 0;
1473 break;
1474 }
1475 }
1476 mutex_unlock(&iommu->lock);
1477
1478 return ret;
1479 }
1480
1481 static long vfio_iommu_type1_ioctl(void *iommu_data,
1482 unsigned int cmd, unsigned long arg)
1483 {
1484 struct vfio_iommu *iommu = iommu_data;
1485 unsigned long minsz;
1486
1487 if (cmd == VFIO_CHECK_EXTENSION) {
1488 switch (arg) {
1489 case VFIO_TYPE1_IOMMU:
1490 case VFIO_TYPE1v2_IOMMU:
1491 case VFIO_TYPE1_NESTING_IOMMU:
1492 return 1;
1493 case VFIO_DMA_CC_IOMMU:
1494 if (!iommu)
1495 return 0;
1496 return vfio_domains_have_iommu_cache(iommu);
1497 default:
1498 return 0;
1499 }
1500 } else if (cmd == VFIO_IOMMU_GET_INFO) {
1501 struct vfio_iommu_type1_info info;
1502
1503 minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes);
1504
1505 if (copy_from_user(&info, (void __user *)arg, minsz))
1506 return -EFAULT;
1507
1508 if (info.argsz < minsz)
1509 return -EINVAL;
1510
1511 info.flags = VFIO_IOMMU_INFO_PGSIZES;
1512
1513 info.iova_pgsizes = vfio_pgsize_bitmap(iommu);
1514
1515 return copy_to_user((void __user *)arg, &info, minsz) ?
1516 -EFAULT : 0;
1517
1518 } else if (cmd == VFIO_IOMMU_MAP_DMA) {
1519 struct vfio_iommu_type1_dma_map map;
1520 uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
1521 VFIO_DMA_MAP_FLAG_WRITE;
1522
1523 minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
1524
1525 if (copy_from_user(&map, (void __user *)arg, minsz))
1526 return -EFAULT;
1527
1528 if (map.argsz < minsz || map.flags & ~mask)
1529 return -EINVAL;
1530
1531 return vfio_dma_do_map(iommu, &map);
1532
1533 } else if (cmd == VFIO_IOMMU_UNMAP_DMA) {
1534 struct vfio_iommu_type1_dma_unmap unmap;
1535 long ret;
1536
1537 minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size);
1538
1539 if (copy_from_user(&unmap, (void __user *)arg, minsz))
1540 return -EFAULT;
1541
1542 if (unmap.argsz < minsz || unmap.flags)
1543 return -EINVAL;
1544
1545 ret = vfio_dma_do_unmap(iommu, &unmap);
1546 if (ret)
1547 return ret;
1548
1549 return copy_to_user((void __user *)arg, &unmap, minsz) ?
1550 -EFAULT : 0;
1551 }
1552
1553 return -ENOTTY;
1554 }
1555
1556 static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
1557 .name = "vfio-iommu-type1",
1558 .owner = THIS_MODULE,
1559 .open = vfio_iommu_type1_open,
1560 .release = vfio_iommu_type1_release,
1561 .ioctl = vfio_iommu_type1_ioctl,
1562 .attach_group = vfio_iommu_type1_attach_group,
1563 .detach_group = vfio_iommu_type1_detach_group,
1564 .pin_pages = vfio_iommu_type1_pin_pages,
1565 .unpin_pages = vfio_iommu_type1_unpin_pages,
1566 };
1567
1568 static int __init vfio_iommu_type1_init(void)
1569 {
1570 return vfio_register_iommu_driver(&vfio_iommu_driver_ops_type1);
1571 }
1572
1573 static void __exit vfio_iommu_type1_cleanup(void)
1574 {
1575 vfio_unregister_iommu_driver(&vfio_iommu_driver_ops_type1);
1576 }
1577
1578 module_init(vfio_iommu_type1_init);
1579 module_exit(vfio_iommu_type1_cleanup);
1580
1581 MODULE_VERSION(DRIVER_VERSION);
1582 MODULE_LICENSE("GPL v2");
1583 MODULE_AUTHOR(DRIVER_AUTHOR);
1584 MODULE_DESCRIPTION(DRIVER_DESC);