2 * VFIO: IOMMU DMA mapping support for Type1 IOMMU
4 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
5 * Author: Alex Williamson <alex.williamson@redhat.com>
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.
11 * Derived from original vfio:
12 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
13 * Author: Tom Lyon, pugs@cisco.com
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.
27 #include <linux/compat.h>
28 #include <linux/device.h>
30 #include <linux/iommu.h>
31 #include <linux/module.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>
40 #define DRIVER_VERSION "0.2"
41 #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>"
42 #define DRIVER_DESC "Type1 IOMMU driver for VFIO"
44 static bool allow_unsafe_interrupts
;
45 module_param_named(allow_unsafe_interrupts
,
46 allow_unsafe_interrupts
, bool, S_IRUGO
| S_IWUSR
);
47 MODULE_PARM_DESC(allow_unsafe_interrupts
,
48 "Enable VFIO IOMMU support for on platforms without interrupt remapping support.");
50 static bool disable_hugepages
;
51 module_param_named(disable_hugepages
,
52 disable_hugepages
, bool, S_IRUGO
| S_IWUSR
);
53 MODULE_PARM_DESC(disable_hugepages
,
54 "Disable VFIO IOMMU support for IOMMU hugepages.");
57 struct list_head domain_list
;
59 struct rb_root dma_list
;
64 struct iommu_domain
*domain
;
65 struct list_head next
;
66 struct list_head group_list
;
67 int prot
; /* IOMMU_CACHE */
72 dma_addr_t iova
; /* Device address */
73 unsigned long vaddr
; /* Process virtual addr */
74 size_t size
; /* Map size (bytes) */
75 int prot
; /* IOMMU_READ/WRITE */
79 struct iommu_group
*iommu_group
;
80 struct list_head next
;
84 * This code handles mapping and unmapping of user data buffers
85 * into DMA'ble space using the IOMMU
88 static struct vfio_dma
*vfio_find_dma(struct vfio_iommu
*iommu
,
89 dma_addr_t start
, size_t size
)
91 struct rb_node
*node
= iommu
->dma_list
.rb_node
;
94 struct vfio_dma
*dma
= rb_entry(node
, struct vfio_dma
, node
);
96 if (start
+ size
<= dma
->iova
)
98 else if (start
>= dma
->iova
+ dma
->size
)
99 node
= node
->rb_right
;
107 static void vfio_link_dma(struct vfio_iommu
*iommu
, struct vfio_dma
*new)
109 struct rb_node
**link
= &iommu
->dma_list
.rb_node
, *parent
= NULL
;
110 struct vfio_dma
*dma
;
114 dma
= rb_entry(parent
, struct vfio_dma
, node
);
116 if (new->iova
+ new->size
<= dma
->iova
)
117 link
= &(*link
)->rb_left
;
119 link
= &(*link
)->rb_right
;
122 rb_link_node(&new->node
, parent
, link
);
123 rb_insert_color(&new->node
, &iommu
->dma_list
);
126 static void vfio_unlink_dma(struct vfio_iommu
*iommu
, struct vfio_dma
*old
)
128 rb_erase(&old
->node
, &iommu
->dma_list
);
132 struct mm_struct
*mm
;
134 struct work_struct work
;
137 /* delayed decrement/increment for locked_vm */
138 static void vfio_lock_acct_bg(struct work_struct
*work
)
140 struct vwork
*vwork
= container_of(work
, struct vwork
, work
);
141 struct mm_struct
*mm
;
144 down_write(&mm
->mmap_sem
);
145 mm
->locked_vm
+= vwork
->npage
;
146 up_write(&mm
->mmap_sem
);
151 static void vfio_lock_acct(long npage
)
154 struct mm_struct
*mm
;
156 if (!current
->mm
|| !npage
)
157 return; /* process exited or nothing to do */
159 if (down_write_trylock(¤t
->mm
->mmap_sem
)) {
160 current
->mm
->locked_vm
+= npage
;
161 up_write(¤t
->mm
->mmap_sem
);
166 * Couldn't get mmap_sem lock, so must setup to update
167 * mm->locked_vm later. If locked_vm were atomic, we
168 * wouldn't need this silliness
170 vwork
= kmalloc(sizeof(struct vwork
), GFP_KERNEL
);
173 mm
= get_task_mm(current
);
178 INIT_WORK(&vwork
->work
, vfio_lock_acct_bg
);
180 vwork
->npage
= npage
;
181 schedule_work(&vwork
->work
);
185 * Some mappings aren't backed by a struct page, for example an mmap'd
186 * MMIO range for our own or another device. These use a different
187 * pfn conversion and shouldn't be tracked as locked pages.
189 static bool is_invalid_reserved_pfn(unsigned long pfn
)
191 if (pfn_valid(pfn
)) {
193 struct page
*tail
= pfn_to_page(pfn
);
194 struct page
*head
= compound_head(tail
);
195 reserved
= !!(PageReserved(head
));
198 * "head" is not a dangling pointer
199 * (compound_head takes care of that)
200 * but the hugepage may have been split
201 * from under us (and we may not hold a
202 * reference count on the head page so it can
203 * be reused before we run PageReferenced), so
204 * we've to check PageTail before returning
211 return PageReserved(tail
);
217 static int put_pfn(unsigned long pfn
, int prot
)
219 if (!is_invalid_reserved_pfn(pfn
)) {
220 struct page
*page
= pfn_to_page(pfn
);
221 if (prot
& IOMMU_WRITE
)
229 static int vaddr_get_pfn(unsigned long vaddr
, int prot
, unsigned long *pfn
)
231 struct page
*page
[1];
232 struct vm_area_struct
*vma
;
235 if (get_user_pages_fast(vaddr
, 1, !!(prot
& IOMMU_WRITE
), page
) == 1) {
236 *pfn
= page_to_pfn(page
[0]);
240 down_read(¤t
->mm
->mmap_sem
);
242 vma
= find_vma_intersection(current
->mm
, vaddr
, vaddr
+ 1);
244 if (vma
&& vma
->vm_flags
& VM_PFNMAP
) {
245 *pfn
= ((vaddr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
246 if (is_invalid_reserved_pfn(*pfn
))
250 up_read(¤t
->mm
->mmap_sem
);
256 * Attempt to pin pages. We really don't want to track all the pfns and
257 * the iommu can only map chunks of consecutive pfns anyway, so get the
258 * first page and all consecutive pages with the same locking.
260 static long vfio_pin_pages(unsigned long vaddr
, long npage
,
261 int prot
, unsigned long *pfn_base
)
263 unsigned long limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
264 bool lock_cap
= capable(CAP_IPC_LOCK
);
270 ret
= vaddr_get_pfn(vaddr
, prot
, pfn_base
);
274 if (is_invalid_reserved_pfn(*pfn_base
))
277 if (!lock_cap
&& current
->mm
->locked_vm
+ 1 > limit
) {
278 put_pfn(*pfn_base
, prot
);
279 pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__
,
280 limit
<< PAGE_SHIFT
);
284 if (unlikely(disable_hugepages
)) {
289 /* Lock all the consecutive pages from pfn_base */
290 for (i
= 1, vaddr
+= PAGE_SIZE
; i
< npage
; i
++, vaddr
+= PAGE_SIZE
) {
291 unsigned long pfn
= 0;
293 ret
= vaddr_get_pfn(vaddr
, prot
, &pfn
);
297 if (pfn
!= *pfn_base
+ i
|| is_invalid_reserved_pfn(pfn
)) {
302 if (!lock_cap
&& current
->mm
->locked_vm
+ i
+ 1 > limit
) {
304 pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
305 __func__
, limit
<< PAGE_SHIFT
);
315 static long vfio_unpin_pages(unsigned long pfn
, long npage
,
316 int prot
, bool do_accounting
)
318 unsigned long unlocked
= 0;
321 for (i
= 0; i
< npage
; i
++)
322 unlocked
+= put_pfn(pfn
++, prot
);
325 vfio_lock_acct(-unlocked
);
330 static void vfio_unmap_unpin(struct vfio_iommu
*iommu
, struct vfio_dma
*dma
)
332 dma_addr_t iova
= dma
->iova
, end
= dma
->iova
+ dma
->size
;
333 struct vfio_domain
*domain
, *d
;
339 * We use the IOMMU to track the physical addresses, otherwise we'd
340 * need a much more complicated tracking system. Unfortunately that
341 * means we need to use one of the iommu domains to figure out the
342 * pfns to unpin. The rest need to be unmapped in advance so we have
343 * no iommu translations remaining when the pages are unpinned.
345 domain
= d
= list_first_entry(&iommu
->domain_list
,
346 struct vfio_domain
, next
);
348 list_for_each_entry_continue(d
, &iommu
->domain_list
, next
)
349 iommu_unmap(d
->domain
, dma
->iova
, dma
->size
);
355 phys
= iommu_iova_to_phys(domain
->domain
, iova
);
356 if (WARN_ON(!phys
)) {
361 unmapped
= iommu_unmap(domain
->domain
, iova
, PAGE_SIZE
);
362 if (WARN_ON(!unmapped
))
365 unlocked
+= vfio_unpin_pages(phys
>> PAGE_SHIFT
,
366 unmapped
>> PAGE_SHIFT
,
371 vfio_lock_acct(-unlocked
);
374 static void vfio_remove_dma(struct vfio_iommu
*iommu
, struct vfio_dma
*dma
)
376 vfio_unmap_unpin(iommu
, dma
);
377 vfio_unlink_dma(iommu
, dma
);
381 static unsigned long vfio_pgsize_bitmap(struct vfio_iommu
*iommu
)
383 struct vfio_domain
*domain
;
384 unsigned long bitmap
= PAGE_MASK
;
386 mutex_lock(&iommu
->lock
);
387 list_for_each_entry(domain
, &iommu
->domain_list
, next
)
388 bitmap
&= domain
->domain
->ops
->pgsize_bitmap
;
389 mutex_unlock(&iommu
->lock
);
394 static int vfio_dma_do_unmap(struct vfio_iommu
*iommu
,
395 struct vfio_iommu_type1_dma_unmap
*unmap
)
398 struct vfio_dma
*dma
;
402 mask
= ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu
))) - 1;
404 if (unmap
->iova
& mask
)
406 if (!unmap
->size
|| unmap
->size
& mask
)
409 WARN_ON(mask
& PAGE_MASK
);
411 mutex_lock(&iommu
->lock
);
414 * vfio-iommu-type1 (v1) - User mappings were coalesced together to
415 * avoid tracking individual mappings. This means that the granularity
416 * of the original mapping was lost and the user was allowed to attempt
417 * to unmap any range. Depending on the contiguousness of physical
418 * memory and page sizes supported by the IOMMU, arbitrary unmaps may
419 * or may not have worked. We only guaranteed unmap granularity
420 * matching the original mapping; even though it was untracked here,
421 * the original mappings are reflected in IOMMU mappings. This
422 * resulted in a couple unusual behaviors. First, if a range is not
423 * able to be unmapped, ex. a set of 4k pages that was mapped as a
424 * 2M hugepage into the IOMMU, the unmap ioctl returns success but with
425 * a zero sized unmap. Also, if an unmap request overlaps the first
426 * address of a hugepage, the IOMMU will unmap the entire hugepage.
427 * This also returns success and the returned unmap size reflects the
428 * actual size unmapped.
430 * We attempt to maintain compatibility with this "v1" interface, but
431 * we take control out of the hands of the IOMMU. Therefore, an unmap
432 * request offset from the beginning of the original mapping will
433 * return success with zero sized unmap. And an unmap request covering
434 * the first iova of mapping will unmap the entire range.
436 * The v2 version of this interface intends to be more deterministic.
437 * Unmap requests must fully cover previous mappings. Multiple
438 * mappings may still be unmaped by specifying large ranges, but there
439 * must not be any previous mappings bisected by the range. An error
440 * will be returned if these conditions are not met. The v2 interface
441 * will only return success and a size of zero if there were no
442 * mappings within the range.
445 dma
= vfio_find_dma(iommu
, unmap
->iova
, 0);
446 if (dma
&& dma
->iova
!= unmap
->iova
) {
450 dma
= vfio_find_dma(iommu
, unmap
->iova
+ unmap
->size
- 1, 0);
451 if (dma
&& dma
->iova
+ dma
->size
!= unmap
->iova
+ unmap
->size
) {
457 while ((dma
= vfio_find_dma(iommu
, unmap
->iova
, unmap
->size
))) {
458 if (!iommu
->v2
&& unmap
->iova
> dma
->iova
)
460 unmapped
+= dma
->size
;
461 vfio_remove_dma(iommu
, dma
);
465 mutex_unlock(&iommu
->lock
);
467 /* Report how much was unmapped */
468 unmap
->size
= unmapped
;
474 * Turns out AMD IOMMU has a page table bug where it won't map large pages
475 * to a region that previously mapped smaller pages. This should be fixed
476 * soon, so this is just a temporary workaround to break mappings down into
477 * PAGE_SIZE. Better to map smaller pages than nothing.
479 static int map_try_harder(struct vfio_domain
*domain
, dma_addr_t iova
,
480 unsigned long pfn
, long npage
, int prot
)
485 for (i
= 0; i
< npage
; i
++, pfn
++, iova
+= PAGE_SIZE
) {
486 ret
= iommu_map(domain
->domain
, iova
,
487 (phys_addr_t
)pfn
<< PAGE_SHIFT
,
488 PAGE_SIZE
, prot
| domain
->prot
);
493 for (; i
< npage
&& i
> 0; i
--, iova
-= PAGE_SIZE
)
494 iommu_unmap(domain
->domain
, iova
, PAGE_SIZE
);
499 static int vfio_iommu_map(struct vfio_iommu
*iommu
, dma_addr_t iova
,
500 unsigned long pfn
, long npage
, int prot
)
502 struct vfio_domain
*d
;
505 list_for_each_entry(d
, &iommu
->domain_list
, next
) {
506 ret
= iommu_map(d
->domain
, iova
, (phys_addr_t
)pfn
<< PAGE_SHIFT
,
507 npage
<< PAGE_SHIFT
, prot
| d
->prot
);
510 map_try_harder(d
, iova
, pfn
, npage
, prot
))
518 list_for_each_entry_continue_reverse(d
, &iommu
->domain_list
, next
)
519 iommu_unmap(d
->domain
, iova
, npage
<< PAGE_SHIFT
);
524 static int vfio_dma_do_map(struct vfio_iommu
*iommu
,
525 struct vfio_iommu_type1_dma_map
*map
)
527 dma_addr_t end
, iova
;
528 unsigned long vaddr
= map
->vaddr
;
529 size_t size
= map
->size
;
531 int ret
= 0, prot
= 0;
533 struct vfio_dma
*dma
;
536 end
= map
->iova
+ map
->size
;
538 mask
= ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu
))) - 1;
540 /* READ/WRITE from device perspective */
541 if (map
->flags
& VFIO_DMA_MAP_FLAG_WRITE
)
543 if (map
->flags
& VFIO_DMA_MAP_FLAG_READ
)
547 return -EINVAL
; /* No READ/WRITE? */
551 if (map
->iova
& mask
)
553 if (!map
->size
|| map
->size
& mask
)
556 WARN_ON(mask
& PAGE_MASK
);
558 /* Don't allow IOVA wrap */
559 if (end
&& end
< map
->iova
)
562 /* Don't allow virtual address wrap */
563 if (vaddr
+ map
->size
&& vaddr
+ map
->size
< vaddr
)
566 mutex_lock(&iommu
->lock
);
568 if (vfio_find_dma(iommu
, map
->iova
, map
->size
)) {
569 mutex_unlock(&iommu
->lock
);
573 dma
= kzalloc(sizeof(*dma
), GFP_KERNEL
);
575 mutex_unlock(&iommu
->lock
);
579 dma
->iova
= map
->iova
;
580 dma
->vaddr
= map
->vaddr
;
583 /* Insert zero-sized and grow as we map chunks of it */
584 vfio_link_dma(iommu
, dma
);
586 for (iova
= map
->iova
; iova
< end
; iova
+= size
, vaddr
+= size
) {
587 /* Pin a contiguous chunk of memory */
588 npage
= vfio_pin_pages(vaddr
, (end
- iova
) >> PAGE_SHIFT
,
597 ret
= vfio_iommu_map(iommu
, iova
, pfn
, npage
, prot
);
599 vfio_unpin_pages(pfn
, npage
, prot
, true);
603 size
= npage
<< PAGE_SHIFT
;
608 vfio_remove_dma(iommu
, dma
);
610 mutex_unlock(&iommu
->lock
);
614 static int vfio_bus_type(struct device
*dev
, void *data
)
616 struct bus_type
**bus
= data
;
618 if (*bus
&& *bus
!= dev
->bus
)
626 static int vfio_iommu_replay(struct vfio_iommu
*iommu
,
627 struct vfio_domain
*domain
)
629 struct vfio_domain
*d
;
633 /* Arbitrarily pick the first domain in the list for lookups */
634 d
= list_first_entry(&iommu
->domain_list
, struct vfio_domain
, next
);
635 n
= rb_first(&iommu
->dma_list
);
637 /* If there's not a domain, there better not be any mappings */
638 if (WARN_ON(n
&& !d
))
641 for (; n
; n
= rb_next(n
)) {
642 struct vfio_dma
*dma
;
645 dma
= rb_entry(n
, struct vfio_dma
, node
);
648 while (iova
< dma
->iova
+ dma
->size
) {
649 phys_addr_t phys
= iommu_iova_to_phys(d
->domain
, iova
);
652 if (WARN_ON(!phys
)) {
659 while (iova
+ size
< dma
->iova
+ dma
->size
&&
660 phys
+ size
== iommu_iova_to_phys(d
->domain
,
664 ret
= iommu_map(domain
->domain
, iova
, phys
,
665 size
, dma
->prot
| domain
->prot
);
676 static int vfio_iommu_type1_attach_group(void *iommu_data
,
677 struct iommu_group
*iommu_group
)
679 struct vfio_iommu
*iommu
= iommu_data
;
680 struct vfio_group
*group
, *g
;
681 struct vfio_domain
*domain
, *d
;
682 struct bus_type
*bus
= NULL
;
685 mutex_lock(&iommu
->lock
);
687 list_for_each_entry(d
, &iommu
->domain_list
, next
) {
688 list_for_each_entry(g
, &d
->group_list
, next
) {
689 if (g
->iommu_group
!= iommu_group
)
692 mutex_unlock(&iommu
->lock
);
697 group
= kzalloc(sizeof(*group
), GFP_KERNEL
);
698 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
699 if (!group
|| !domain
) {
704 group
->iommu_group
= iommu_group
;
706 /* Determine bus_type in order to allocate a domain */
707 ret
= iommu_group_for_each_dev(iommu_group
, &bus
, vfio_bus_type
);
711 domain
->domain
= iommu_domain_alloc(bus
);
712 if (!domain
->domain
) {
717 ret
= iommu_attach_group(domain
->domain
, iommu_group
);
721 INIT_LIST_HEAD(&domain
->group_list
);
722 list_add(&group
->next
, &domain
->group_list
);
724 if (!allow_unsafe_interrupts
&&
725 !iommu_domain_has_cap(domain
->domain
, IOMMU_CAP_INTR_REMAP
)) {
726 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
732 if (iommu_domain_has_cap(domain
->domain
, IOMMU_CAP_CACHE_COHERENCY
))
733 domain
->prot
|= IOMMU_CACHE
;
736 * Try to match an existing compatible domain. We don't want to
737 * preclude an IOMMU driver supporting multiple bus_types and being
738 * able to include different bus_types in the same IOMMU domain, so
739 * we test whether the domains use the same iommu_ops rather than
740 * testing if they're on the same bus_type.
742 list_for_each_entry(d
, &iommu
->domain_list
, next
) {
743 if (d
->domain
->ops
== domain
->domain
->ops
&&
744 d
->prot
== domain
->prot
) {
745 iommu_detach_group(domain
->domain
, iommu_group
);
746 if (!iommu_attach_group(d
->domain
, iommu_group
)) {
747 list_add(&group
->next
, &d
->group_list
);
748 iommu_domain_free(domain
->domain
);
750 mutex_unlock(&iommu
->lock
);
754 ret
= iommu_attach_group(domain
->domain
, iommu_group
);
760 /* replay mappings on new domains */
761 ret
= vfio_iommu_replay(iommu
, domain
);
765 list_add(&domain
->next
, &iommu
->domain_list
);
767 mutex_unlock(&iommu
->lock
);
772 iommu_detach_group(domain
->domain
, iommu_group
);
774 iommu_domain_free(domain
->domain
);
778 mutex_unlock(&iommu
->lock
);
782 static void vfio_iommu_unmap_unpin_all(struct vfio_iommu
*iommu
)
784 struct rb_node
*node
;
786 while ((node
= rb_first(&iommu
->dma_list
)))
787 vfio_remove_dma(iommu
, rb_entry(node
, struct vfio_dma
, node
));
790 static void vfio_iommu_type1_detach_group(void *iommu_data
,
791 struct iommu_group
*iommu_group
)
793 struct vfio_iommu
*iommu
= iommu_data
;
794 struct vfio_domain
*domain
;
795 struct vfio_group
*group
;
797 mutex_lock(&iommu
->lock
);
799 list_for_each_entry(domain
, &iommu
->domain_list
, next
) {
800 list_for_each_entry(group
, &domain
->group_list
, next
) {
801 if (group
->iommu_group
!= iommu_group
)
804 iommu_detach_group(domain
->domain
, iommu_group
);
805 list_del(&group
->next
);
808 * Group ownership provides privilege, if the group
809 * list is empty, the domain goes away. If it's the
810 * last domain, then all the mappings go away too.
812 if (list_empty(&domain
->group_list
)) {
813 if (list_is_singular(&iommu
->domain_list
))
814 vfio_iommu_unmap_unpin_all(iommu
);
815 iommu_domain_free(domain
->domain
);
816 list_del(&domain
->next
);
824 mutex_unlock(&iommu
->lock
);
827 static void *vfio_iommu_type1_open(unsigned long arg
)
829 struct vfio_iommu
*iommu
;
831 if (arg
!= VFIO_TYPE1_IOMMU
&& arg
!= VFIO_TYPE1v2_IOMMU
)
832 return ERR_PTR(-EINVAL
);
834 iommu
= kzalloc(sizeof(*iommu
), GFP_KERNEL
);
836 return ERR_PTR(-ENOMEM
);
838 INIT_LIST_HEAD(&iommu
->domain_list
);
839 iommu
->dma_list
= RB_ROOT
;
840 mutex_init(&iommu
->lock
);
841 iommu
->v2
= (arg
== VFIO_TYPE1v2_IOMMU
);
846 static void vfio_iommu_type1_release(void *iommu_data
)
848 struct vfio_iommu
*iommu
= iommu_data
;
849 struct vfio_domain
*domain
, *domain_tmp
;
850 struct vfio_group
*group
, *group_tmp
;
852 vfio_iommu_unmap_unpin_all(iommu
);
854 list_for_each_entry_safe(domain
, domain_tmp
,
855 &iommu
->domain_list
, next
) {
856 list_for_each_entry_safe(group
, group_tmp
,
857 &domain
->group_list
, next
) {
858 iommu_detach_group(domain
->domain
, group
->iommu_group
);
859 list_del(&group
->next
);
862 iommu_domain_free(domain
->domain
);
863 list_del(&domain
->next
);
870 static int vfio_domains_have_iommu_cache(struct vfio_iommu
*iommu
)
872 struct vfio_domain
*domain
;
875 mutex_lock(&iommu
->lock
);
876 list_for_each_entry(domain
, &iommu
->domain_list
, next
) {
877 if (!(domain
->prot
& IOMMU_CACHE
)) {
882 mutex_unlock(&iommu
->lock
);
887 static long vfio_iommu_type1_ioctl(void *iommu_data
,
888 unsigned int cmd
, unsigned long arg
)
890 struct vfio_iommu
*iommu
= iommu_data
;
893 if (cmd
== VFIO_CHECK_EXTENSION
) {
895 case VFIO_TYPE1_IOMMU
:
896 case VFIO_TYPE1v2_IOMMU
:
898 case VFIO_DMA_CC_IOMMU
:
901 return vfio_domains_have_iommu_cache(iommu
);
905 } else if (cmd
== VFIO_IOMMU_GET_INFO
) {
906 struct vfio_iommu_type1_info info
;
908 minsz
= offsetofend(struct vfio_iommu_type1_info
, iova_pgsizes
);
910 if (copy_from_user(&info
, (void __user
*)arg
, minsz
))
913 if (info
.argsz
< minsz
)
918 info
.iova_pgsizes
= vfio_pgsize_bitmap(iommu
);
920 return copy_to_user((void __user
*)arg
, &info
, minsz
);
922 } else if (cmd
== VFIO_IOMMU_MAP_DMA
) {
923 struct vfio_iommu_type1_dma_map map
;
924 uint32_t mask
= VFIO_DMA_MAP_FLAG_READ
|
925 VFIO_DMA_MAP_FLAG_WRITE
;
927 minsz
= offsetofend(struct vfio_iommu_type1_dma_map
, size
);
929 if (copy_from_user(&map
, (void __user
*)arg
, minsz
))
932 if (map
.argsz
< minsz
|| map
.flags
& ~mask
)
935 return vfio_dma_do_map(iommu
, &map
);
937 } else if (cmd
== VFIO_IOMMU_UNMAP_DMA
) {
938 struct vfio_iommu_type1_dma_unmap unmap
;
941 minsz
= offsetofend(struct vfio_iommu_type1_dma_unmap
, size
);
943 if (copy_from_user(&unmap
, (void __user
*)arg
, minsz
))
946 if (unmap
.argsz
< minsz
|| unmap
.flags
)
949 ret
= vfio_dma_do_unmap(iommu
, &unmap
);
953 return copy_to_user((void __user
*)arg
, &unmap
, minsz
);
959 static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1
= {
960 .name
= "vfio-iommu-type1",
961 .owner
= THIS_MODULE
,
962 .open
= vfio_iommu_type1_open
,
963 .release
= vfio_iommu_type1_release
,
964 .ioctl
= vfio_iommu_type1_ioctl
,
965 .attach_group
= vfio_iommu_type1_attach_group
,
966 .detach_group
= vfio_iommu_type1_detach_group
,
969 static int __init
vfio_iommu_type1_init(void)
971 return vfio_register_iommu_driver(&vfio_iommu_driver_ops_type1
);
974 static void __exit
vfio_iommu_type1_cleanup(void)
976 vfio_unregister_iommu_driver(&vfio_iommu_driver_ops_type1
);
979 module_init(vfio_iommu_type1_init
);
980 module_exit(vfio_iommu_type1_cleanup
);
982 MODULE_VERSION(DRIVER_VERSION
);
983 MODULE_LICENSE("GPL v2");
984 MODULE_AUTHOR(DRIVER_AUTHOR
);
985 MODULE_DESCRIPTION(DRIVER_DESC
);