blob: 0d5b667c0e652de526fdcdef866a1cb878ee3fe1 [file] [log] [blame]
Alex Williamson73fa0d12012-07-31 08:16:23 -06001/*
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>
Alex Williamsoncd9b2262013-06-21 09:37:50 -060033#include <linux/rbtree.h>
Alex Williamson73fa0d12012-07-31 08:16:23 -060034#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
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"
43
44static bool allow_unsafe_interrupts;
45module_param_named(allow_unsafe_interrupts,
46 allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR);
47MODULE_PARM_DESC(allow_unsafe_interrupts,
48 "Enable VFIO IOMMU support for on platforms without interrupt remapping support.");
49
Alex Williamson5c6c2b22013-06-21 09:38:11 -060050static bool disable_hugepages;
51module_param_named(disable_hugepages,
52 disable_hugepages, bool, S_IRUGO | S_IWUSR);
53MODULE_PARM_DESC(disable_hugepages,
54 "Disable VFIO IOMMU support for IOMMU hugepages.");
55
Alex Williamson4f97abd2019-04-03 12:36:21 -060056static unsigned int dma_entry_limit __read_mostly = U16_MAX;
57module_param_named(dma_entry_limit, dma_entry_limit, uint, 0644);
58MODULE_PARM_DESC(dma_entry_limit,
59 "Maximum number of user DMA mappings per container (65535).");
60
Alex Williamson73fa0d12012-07-31 08:16:23 -060061struct vfio_iommu {
Alex Williamson1ef3e2b2014-02-26 11:38:36 -070062 struct list_head domain_list;
Alex Williamson73fa0d12012-07-31 08:16:23 -060063 struct mutex lock;
Alex Williamsoncd9b2262013-06-21 09:37:50 -060064 struct rb_root dma_list;
Alex Williamson4f97abd2019-04-03 12:36:21 -060065 unsigned int dma_avail;
Will Deaconf5c9ece2014-09-29 10:06:19 -060066 bool v2;
67 bool nesting;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -070068};
69
70struct vfio_domain {
71 struct iommu_domain *domain;
72 struct list_head next;
Alex Williamson73fa0d12012-07-31 08:16:23 -060073 struct list_head group_list;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -070074 int prot; /* IOMMU_CACHE */
Alex Williamson6fe10102015-02-06 10:58:56 -070075 bool fgsp; /* Fine-grained super pages */
Alex Williamson73fa0d12012-07-31 08:16:23 -060076};
77
78struct vfio_dma {
Alex Williamsoncd9b2262013-06-21 09:37:50 -060079 struct rb_node node;
Alex Williamson73fa0d12012-07-31 08:16:23 -060080 dma_addr_t iova; /* Device address */
81 unsigned long vaddr; /* Process virtual addr */
Alex Williamson166fd7d2013-06-21 09:38:02 -060082 size_t size; /* Map size (bytes) */
Alex Williamson73fa0d12012-07-31 08:16:23 -060083 int prot; /* IOMMU_READ/WRITE */
84};
85
86struct vfio_group {
87 struct iommu_group *iommu_group;
88 struct list_head next;
89};
90
91/*
92 * This code handles mapping and unmapping of user data buffers
93 * into DMA'ble space using the IOMMU
94 */
95
Alex Williamsoncd9b2262013-06-21 09:37:50 -060096static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu,
97 dma_addr_t start, size_t size)
98{
99 struct rb_node *node = iommu->dma_list.rb_node;
100
101 while (node) {
102 struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node);
103
104 if (start + size <= dma->iova)
105 node = node->rb_left;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600106 else if (start >= dma->iova + dma->size)
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600107 node = node->rb_right;
108 else
109 return dma;
110 }
111
112 return NULL;
113}
114
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700115static void vfio_link_dma(struct vfio_iommu *iommu, struct vfio_dma *new)
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600116{
117 struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL;
118 struct vfio_dma *dma;
119
120 while (*link) {
121 parent = *link;
122 dma = rb_entry(parent, struct vfio_dma, node);
123
Alex Williamson166fd7d2013-06-21 09:38:02 -0600124 if (new->iova + new->size <= dma->iova)
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600125 link = &(*link)->rb_left;
126 else
127 link = &(*link)->rb_right;
128 }
129
130 rb_link_node(&new->node, parent, link);
131 rb_insert_color(&new->node, &iommu->dma_list);
132}
133
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700134static void vfio_unlink_dma(struct vfio_iommu *iommu, struct vfio_dma *old)
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600135{
136 rb_erase(&old->node, &iommu->dma_list);
137}
138
Alex Williamson9f43f702017-04-13 14:10:15 -0600139static int vfio_lock_acct(long npage, bool *lock_cap)
Alex Williamson73fa0d12012-07-31 08:16:23 -0600140{
Alex Williamson9f43f702017-04-13 14:10:15 -0600141 int ret;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600142
Alex Williamson9f43f702017-04-13 14:10:15 -0600143 if (!npage)
144 return 0;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600145
Alex Williamson9f43f702017-04-13 14:10:15 -0600146 if (!current->mm)
147 return -ESRCH; /* process exited */
Alex Williamson73fa0d12012-07-31 08:16:23 -0600148
Alex Williamson9f43f702017-04-13 14:10:15 -0600149 ret = down_write_killable(&current->mm->mmap_sem);
150 if (!ret) {
151 if (npage > 0) {
152 if (lock_cap ? !*lock_cap : !capable(CAP_IPC_LOCK)) {
153 unsigned long limit;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600154
Alex Williamson9f43f702017-04-13 14:10:15 -0600155 limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
156
157 if (current->mm->locked_vm + npage > limit)
158 ret = -ENOMEM;
159 }
160 }
161
162 if (!ret)
163 current->mm->locked_vm += npage;
164
Alex Williamson73fa0d12012-07-31 08:16:23 -0600165 up_write(&current->mm->mmap_sem);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600166 }
167
Alex Williamson9f43f702017-04-13 14:10:15 -0600168 return ret;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600169}
170
171/*
172 * Some mappings aren't backed by a struct page, for example an mmap'd
173 * MMIO range for our own or another device. These use a different
174 * pfn conversion and shouldn't be tracked as locked pages.
175 */
176static bool is_invalid_reserved_pfn(unsigned long pfn)
177{
178 if (pfn_valid(pfn)) {
179 bool reserved;
180 struct page *tail = pfn_to_page(pfn);
David Rientjes668f9abb2014-03-03 15:38:18 -0800181 struct page *head = compound_head(tail);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600182 reserved = !!(PageReserved(head));
183 if (head != tail) {
184 /*
185 * "head" is not a dangling pointer
David Rientjes668f9abb2014-03-03 15:38:18 -0800186 * (compound_head takes care of that)
Alex Williamson73fa0d12012-07-31 08:16:23 -0600187 * but the hugepage may have been split
188 * from under us (and we may not hold a
189 * reference count on the head page so it can
190 * be reused before we run PageReferenced), so
191 * we've to check PageTail before returning
192 * what we just read.
193 */
194 smp_rmb();
195 if (PageTail(tail))
196 return reserved;
197 }
198 return PageReserved(tail);
199 }
200
201 return true;
202}
203
204static int put_pfn(unsigned long pfn, int prot)
205{
206 if (!is_invalid_reserved_pfn(pfn)) {
207 struct page *page = pfn_to_page(pfn);
208 if (prot & IOMMU_WRITE)
209 SetPageDirty(page);
210 put_page(page);
211 return 1;
212 }
213 return 0;
214}
215
Alex Williamson73fa0d12012-07-31 08:16:23 -0600216static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn)
217{
218 struct page *page[1];
219 struct vm_area_struct *vma;
220 int ret = -EFAULT;
221
222 if (get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE), page) == 1) {
223 *pfn = page_to_pfn(page[0]);
224 return 0;
225 }
226
227 down_read(&current->mm->mmap_sem);
228
229 vma = find_vma_intersection(current->mm, vaddr, vaddr + 1);
230
231 if (vma && vma->vm_flags & VM_PFNMAP) {
232 *pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
233 if (is_invalid_reserved_pfn(*pfn))
234 ret = 0;
235 }
236
237 up_read(&current->mm->mmap_sem);
238
239 return ret;
240}
241
Alex Williamson166fd7d2013-06-21 09:38:02 -0600242/*
243 * Attempt to pin pages. We really don't want to track all the pfns and
244 * the iommu can only map chunks of consecutive pfns anyway, so get the
245 * first page and all consecutive pages with the same locking.
246 */
247static long vfio_pin_pages(unsigned long vaddr, long npage,
248 int prot, unsigned long *pfn_base)
Alex Williamson73fa0d12012-07-31 08:16:23 -0600249{
Alex Williamson9f43f702017-04-13 14:10:15 -0600250 unsigned long pfn = 0, limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600251 bool lock_cap = capable(CAP_IPC_LOCK);
Alex Williamson9f43f702017-04-13 14:10:15 -0600252 long ret, i = 1;
Alex Williamsonbabbf1762015-02-06 10:59:16 -0700253 bool rsvd;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600254
Alex Williamson166fd7d2013-06-21 09:38:02 -0600255 if (!current->mm)
256 return -ENODEV;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600257
Alex Williamson166fd7d2013-06-21 09:38:02 -0600258 ret = vaddr_get_pfn(vaddr, prot, pfn_base);
259 if (ret)
260 return ret;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600261
Alex Williamsonbabbf1762015-02-06 10:59:16 -0700262 rsvd = is_invalid_reserved_pfn(*pfn_base);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600263
Alex Williamsonbabbf1762015-02-06 10:59:16 -0700264 if (!rsvd && !lock_cap && current->mm->locked_vm + 1 > limit) {
Alex Williamson166fd7d2013-06-21 09:38:02 -0600265 put_pfn(*pfn_base, prot);
266 pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__,
267 limit << PAGE_SHIFT);
268 return -ENOMEM;
269 }
270
Alex Williamson9f43f702017-04-13 14:10:15 -0600271 if (unlikely(disable_hugepages))
272 goto out;
Alex Williamson5c6c2b22013-06-21 09:38:11 -0600273
Alex Williamson166fd7d2013-06-21 09:38:02 -0600274 /* Lock all the consecutive pages from pfn_base */
Alex Williamson9f43f702017-04-13 14:10:15 -0600275 for (vaddr += PAGE_SIZE; i < npage; i++, vaddr += PAGE_SIZE) {
Alex Williamson73fa0d12012-07-31 08:16:23 -0600276 ret = vaddr_get_pfn(vaddr, prot, &pfn);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600277 if (ret)
278 break;
279
Alex Williamsonbabbf1762015-02-06 10:59:16 -0700280 if (pfn != *pfn_base + i ||
281 rsvd != is_invalid_reserved_pfn(pfn)) {
Alex Williamson166fd7d2013-06-21 09:38:02 -0600282 put_pfn(pfn, prot);
283 break;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600284 }
285
Alex Williamsonbabbf1762015-02-06 10:59:16 -0700286 if (!rsvd && !lock_cap &&
287 current->mm->locked_vm + i + 1 > limit) {
Alex Williamson73fa0d12012-07-31 08:16:23 -0600288 put_pfn(pfn, prot);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600289 pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n",
290 __func__, limit << PAGE_SHIFT);
Alex Williamson9f43f702017-04-13 14:10:15 -0600291 ret = -ENOMEM;
292 goto unpin_out;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600293 }
294 }
Alex Williamson166fd7d2013-06-21 09:38:02 -0600295
Alex Williamson9f43f702017-04-13 14:10:15 -0600296out:
Alex Williamsonbabbf1762015-02-06 10:59:16 -0700297 if (!rsvd)
Alex Williamson9f43f702017-04-13 14:10:15 -0600298 ret = vfio_lock_acct(i, &lock_cap);
299
300unpin_out:
301 if (ret) {
302 if (!rsvd) {
303 for (pfn = *pfn_base ; i ; pfn++, i--)
304 put_pfn(pfn, prot);
305 }
306
307 return ret;
308 }
Alex Williamson166fd7d2013-06-21 09:38:02 -0600309
310 return i;
311}
312
313static long vfio_unpin_pages(unsigned long pfn, long npage,
314 int prot, bool do_accounting)
315{
316 unsigned long unlocked = 0;
317 long i;
318
319 for (i = 0; i < npage; i++)
320 unlocked += put_pfn(pfn++, prot);
321
322 if (do_accounting)
Alex Williamson9f43f702017-04-13 14:10:15 -0600323 vfio_lock_acct(-unlocked, NULL);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600324
325 return unlocked;
326}
327
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700328static void vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma)
Alex Williamson166fd7d2013-06-21 09:38:02 -0600329{
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700330 dma_addr_t iova = dma->iova, end = dma->iova + dma->size;
331 struct vfio_domain *domain, *d;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600332 long unlocked = 0;
333
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700334 if (!dma->size)
335 return;
336 /*
337 * We use the IOMMU to track the physical addresses, otherwise we'd
338 * need a much more complicated tracking system. Unfortunately that
339 * means we need to use one of the iommu domains to figure out the
340 * pfns to unpin. The rest need to be unmapped in advance so we have
341 * no iommu translations remaining when the pages are unpinned.
342 */
343 domain = d = list_first_entry(&iommu->domain_list,
344 struct vfio_domain, next);
345
Alex Williamsonc5e66882015-02-06 14:19:12 -0700346 list_for_each_entry_continue(d, &iommu->domain_list, next) {
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700347 iommu_unmap(d->domain, dma->iova, dma->size);
Alex Williamsonc5e66882015-02-06 14:19:12 -0700348 cond_resched();
349 }
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700350
Alex Williamson166fd7d2013-06-21 09:38:02 -0600351 while (iova < end) {
Alex Williamson6fe10102015-02-06 10:58:56 -0700352 size_t unmapped, len;
353 phys_addr_t phys, next;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600354
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700355 phys = iommu_iova_to_phys(domain->domain, iova);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600356 if (WARN_ON(!phys)) {
357 iova += PAGE_SIZE;
358 continue;
359 }
360
Alex Williamson6fe10102015-02-06 10:58:56 -0700361 /*
362 * To optimize for fewer iommu_unmap() calls, each of which
363 * may require hardware cache flushing, try to find the
364 * largest contiguous physical memory chunk to unmap.
365 */
366 for (len = PAGE_SIZE;
367 !domain->fgsp && iova + len < end; len += PAGE_SIZE) {
368 next = iommu_iova_to_phys(domain->domain, iova + len);
369 if (next != phys + len)
370 break;
371 }
372
373 unmapped = iommu_unmap(domain->domain, iova, len);
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700374 if (WARN_ON(!unmapped))
Alex Williamson166fd7d2013-06-21 09:38:02 -0600375 break;
376
377 unlocked += vfio_unpin_pages(phys >> PAGE_SHIFT,
378 unmapped >> PAGE_SHIFT,
379 dma->prot, false);
380 iova += unmapped;
Alex Williamsonc5e66882015-02-06 14:19:12 -0700381
382 cond_resched();
Alex Williamson166fd7d2013-06-21 09:38:02 -0600383 }
384
Alex Williamson9f43f702017-04-13 14:10:15 -0600385 vfio_lock_acct(-unlocked, NULL);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600386}
387
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700388static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma)
Alex Williamson73fa0d12012-07-31 08:16:23 -0600389{
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700390 vfio_unmap_unpin(iommu, dma);
391 vfio_unlink_dma(iommu, dma);
392 kfree(dma);
Alex Williamson4f97abd2019-04-03 12:36:21 -0600393 iommu->dma_avail++;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700394}
Alex Williamson73fa0d12012-07-31 08:16:23 -0600395
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700396static unsigned long vfio_pgsize_bitmap(struct vfio_iommu *iommu)
397{
398 struct vfio_domain *domain;
Eric Auger46443212015-10-29 17:49:42 +0000399 unsigned long bitmap = ULONG_MAX;
Alex Williamsonf5bfdbf2013-06-25 16:01:44 -0600400
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700401 mutex_lock(&iommu->lock);
402 list_for_each_entry(domain, &iommu->domain_list, next)
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100403 bitmap &= domain->domain->pgsize_bitmap;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700404 mutex_unlock(&iommu->lock);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600405
Eric Auger46443212015-10-29 17:49:42 +0000406 /*
407 * In case the IOMMU supports page sizes smaller than PAGE_SIZE
408 * we pretend PAGE_SIZE is supported and hide sub-PAGE_SIZE sizes.
409 * That way the user will be able to map/unmap buffers whose size/
410 * start address is aligned with PAGE_SIZE. Pinning code uses that
411 * granularity while iommu driver can use the sub-PAGE_SIZE size
412 * to map the buffer.
413 */
414 if (bitmap & ~PAGE_MASK) {
415 bitmap &= PAGE_MASK;
416 bitmap |= PAGE_SIZE;
417 }
418
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700419 return bitmap;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600420}
421
422static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
423 struct vfio_iommu_type1_dma_unmap *unmap)
424{
Alex Williamson73fa0d12012-07-31 08:16:23 -0600425 uint64_t mask;
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600426 struct vfio_dma *dma;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700427 size_t unmapped = 0;
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600428 int ret = 0;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600429
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700430 mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600431
432 if (unmap->iova & mask)
433 return -EINVAL;
Alex Williamsonf5bfdbf2013-06-25 16:01:44 -0600434 if (!unmap->size || unmap->size & mask)
Alex Williamson73fa0d12012-07-31 08:16:23 -0600435 return -EINVAL;
436
Alex Williamson73fa0d12012-07-31 08:16:23 -0600437 WARN_ON(mask & PAGE_MASK);
438
439 mutex_lock(&iommu->lock);
440
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700441 /*
442 * vfio-iommu-type1 (v1) - User mappings were coalesced together to
443 * avoid tracking individual mappings. This means that the granularity
444 * of the original mapping was lost and the user was allowed to attempt
445 * to unmap any range. Depending on the contiguousness of physical
446 * memory and page sizes supported by the IOMMU, arbitrary unmaps may
447 * or may not have worked. We only guaranteed unmap granularity
448 * matching the original mapping; even though it was untracked here,
449 * the original mappings are reflected in IOMMU mappings. This
450 * resulted in a couple unusual behaviors. First, if a range is not
451 * able to be unmapped, ex. a set of 4k pages that was mapped as a
452 * 2M hugepage into the IOMMU, the unmap ioctl returns success but with
453 * a zero sized unmap. Also, if an unmap request overlaps the first
454 * address of a hugepage, the IOMMU will unmap the entire hugepage.
455 * This also returns success and the returned unmap size reflects the
456 * actual size unmapped.
457 *
458 * We attempt to maintain compatibility with this "v1" interface, but
459 * we take control out of the hands of the IOMMU. Therefore, an unmap
460 * request offset from the beginning of the original mapping will
461 * return success with zero sized unmap. And an unmap request covering
462 * the first iova of mapping will unmap the entire range.
463 *
464 * The v2 version of this interface intends to be more deterministic.
465 * Unmap requests must fully cover previous mappings. Multiple
466 * mappings may still be unmaped by specifying large ranges, but there
467 * must not be any previous mappings bisected by the range. An error
468 * will be returned if these conditions are not met. The v2 interface
469 * will only return success and a size of zero if there were no
470 * mappings within the range.
471 */
472 if (iommu->v2) {
473 dma = vfio_find_dma(iommu, unmap->iova, 0);
474 if (dma && dma->iova != unmap->iova) {
475 ret = -EINVAL;
476 goto unlock;
477 }
478 dma = vfio_find_dma(iommu, unmap->iova + unmap->size - 1, 0);
479 if (dma && dma->iova + dma->size != unmap->iova + unmap->size) {
480 ret = -EINVAL;
481 goto unlock;
482 }
Alex Williamson166fd7d2013-06-21 09:38:02 -0600483 }
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600484
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700485 while ((dma = vfio_find_dma(iommu, unmap->iova, unmap->size))) {
486 if (!iommu->v2 && unmap->iova > dma->iova)
487 break;
488 unmapped += dma->size;
489 vfio_remove_dma(iommu, dma);
490 }
491
492unlock:
Alex Williamson73fa0d12012-07-31 08:16:23 -0600493 mutex_unlock(&iommu->lock);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600494
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700495 /* Report how much was unmapped */
Alex Williamson166fd7d2013-06-21 09:38:02 -0600496 unmap->size = unmapped;
497
498 return ret;
499}
500
501/*
502 * Turns out AMD IOMMU has a page table bug where it won't map large pages
503 * to a region that previously mapped smaller pages. This should be fixed
504 * soon, so this is just a temporary workaround to break mappings down into
505 * PAGE_SIZE. Better to map smaller pages than nothing.
506 */
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700507static int map_try_harder(struct vfio_domain *domain, dma_addr_t iova,
Alex Williamson166fd7d2013-06-21 09:38:02 -0600508 unsigned long pfn, long npage, int prot)
509{
510 long i;
Alex Williamson089f1c62016-05-30 07:58:10 -0600511 int ret = 0;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600512
513 for (i = 0; i < npage; i++, pfn++, iova += PAGE_SIZE) {
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700514 ret = iommu_map(domain->domain, iova,
Alex Williamson166fd7d2013-06-21 09:38:02 -0600515 (phys_addr_t)pfn << PAGE_SHIFT,
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700516 PAGE_SIZE, prot | domain->prot);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600517 if (ret)
518 break;
519 }
520
521 for (; i < npage && i > 0; i--, iova -= PAGE_SIZE)
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700522 iommu_unmap(domain->domain, iova, PAGE_SIZE);
523
524 return ret;
525}
526
527static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova,
528 unsigned long pfn, long npage, int prot)
529{
530 struct vfio_domain *d;
531 int ret;
532
533 list_for_each_entry(d, &iommu->domain_list, next) {
534 ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT,
535 npage << PAGE_SHIFT, prot | d->prot);
536 if (ret) {
537 if (ret != -EBUSY ||
538 map_try_harder(d, iova, pfn, npage, prot))
539 goto unwind;
540 }
Alex Williamsonc5e66882015-02-06 14:19:12 -0700541
542 cond_resched();
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700543 }
544
545 return 0;
546
547unwind:
548 list_for_each_entry_continue_reverse(d, &iommu->domain_list, next)
549 iommu_unmap(d->domain, iova, npage << PAGE_SHIFT);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600550
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600551 return ret;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600552}
553
554static int vfio_dma_do_map(struct vfio_iommu *iommu,
555 struct vfio_iommu_type1_dma_map *map)
556{
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600557 dma_addr_t iova = map->iova;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600558 unsigned long vaddr = map->vaddr;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600559 size_t size = map->size;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600560 long npage;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600561 int ret = 0, prot = 0;
562 uint64_t mask;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700563 struct vfio_dma *dma;
Antonios Motakisd93b3ac2013-10-11 10:40:46 -0600564 unsigned long pfn;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600565
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600566 /* Verify that none of our __u64 fields overflow */
567 if (map->size != size || map->vaddr != vaddr || map->iova != iova)
568 return -EINVAL;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600569
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700570 mask = ((uint64_t)1 << __ffs(vfio_pgsize_bitmap(iommu))) - 1;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600571
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600572 WARN_ON(mask & PAGE_MASK);
573
Alex Williamson73fa0d12012-07-31 08:16:23 -0600574 /* READ/WRITE from device perspective */
575 if (map->flags & VFIO_DMA_MAP_FLAG_WRITE)
576 prot |= IOMMU_WRITE;
577 if (map->flags & VFIO_DMA_MAP_FLAG_READ)
578 prot |= IOMMU_READ;
579
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600580 if (!prot || !size || (size | iova | vaddr) & mask)
Alex Williamson73fa0d12012-07-31 08:16:23 -0600581 return -EINVAL;
582
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600583 /* Don't allow IOVA or virtual address wrap */
584 if (iova + size - 1 < iova || vaddr + size - 1 < vaddr)
Alex Williamson73fa0d12012-07-31 08:16:23 -0600585 return -EINVAL;
586
Alex Williamson73fa0d12012-07-31 08:16:23 -0600587 mutex_lock(&iommu->lock);
588
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600589 if (vfio_find_dma(iommu, iova, size)) {
Alex Williamson166fd7d2013-06-21 09:38:02 -0600590 mutex_unlock(&iommu->lock);
591 return -EEXIST;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600592 }
593
Alex Williamson4f97abd2019-04-03 12:36:21 -0600594 if (!iommu->dma_avail) {
595 mutex_unlock(&iommu->lock);
596 return -ENOSPC;
597 }
598
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700599 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
600 if (!dma) {
601 mutex_unlock(&iommu->lock);
602 return -ENOMEM;
603 }
Alex Williamson73fa0d12012-07-31 08:16:23 -0600604
Alex Williamson4f97abd2019-04-03 12:36:21 -0600605 iommu->dma_avail--;
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600606 dma->iova = iova;
607 dma->vaddr = vaddr;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700608 dma->prot = prot;
609
610 /* Insert zero-sized and grow as we map chunks of it */
611 vfio_link_dma(iommu, dma);
612
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600613 while (size) {
Alex Williamson166fd7d2013-06-21 09:38:02 -0600614 /* Pin a contiguous chunk of memory */
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600615 npage = vfio_pin_pages(vaddr + dma->size,
616 size >> PAGE_SHIFT, prot, &pfn);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600617 if (npage <= 0) {
618 WARN_ON(!npage);
619 ret = (int)npage;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700620 break;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600621 }
Alex Williamson73fa0d12012-07-31 08:16:23 -0600622
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700623 /* Map it! */
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600624 ret = vfio_iommu_map(iommu, iova + dma->size, pfn, npage, prot);
Alex Williamson166fd7d2013-06-21 09:38:02 -0600625 if (ret) {
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700626 vfio_unpin_pages(pfn, npage, prot, true);
627 break;
Alex Williamson166fd7d2013-06-21 09:38:02 -0600628 }
629
Alex Williamsonc8dbca12014-05-30 11:35:54 -0600630 size -= npage << PAGE_SHIFT;
631 dma->size += npage << PAGE_SHIFT;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600632 }
633
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700634 if (ret)
635 vfio_remove_dma(iommu, dma);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600636
Alex Williamson73fa0d12012-07-31 08:16:23 -0600637 mutex_unlock(&iommu->lock);
638 return ret;
639}
640
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700641static int vfio_bus_type(struct device *dev, void *data)
642{
643 struct bus_type **bus = data;
644
645 if (*bus && *bus != dev->bus)
646 return -EINVAL;
647
648 *bus = dev->bus;
649
650 return 0;
651}
652
653static int vfio_iommu_replay(struct vfio_iommu *iommu,
654 struct vfio_domain *domain)
655{
656 struct vfio_domain *d;
657 struct rb_node *n;
658 int ret;
659
660 /* Arbitrarily pick the first domain in the list for lookups */
661 d = list_first_entry(&iommu->domain_list, struct vfio_domain, next);
662 n = rb_first(&iommu->dma_list);
663
664 /* If there's not a domain, there better not be any mappings */
665 if (WARN_ON(n && !d))
666 return -EINVAL;
667
668 for (; n; n = rb_next(n)) {
669 struct vfio_dma *dma;
670 dma_addr_t iova;
671
672 dma = rb_entry(n, struct vfio_dma, node);
673 iova = dma->iova;
674
675 while (iova < dma->iova + dma->size) {
676 phys_addr_t phys = iommu_iova_to_phys(d->domain, iova);
677 size_t size;
678
679 if (WARN_ON(!phys)) {
680 iova += PAGE_SIZE;
681 continue;
682 }
683
684 size = PAGE_SIZE;
685
686 while (iova + size < dma->iova + dma->size &&
687 phys + size == iommu_iova_to_phys(d->domain,
688 iova + size))
689 size += PAGE_SIZE;
690
691 ret = iommu_map(domain->domain, iova, phys,
692 size, dma->prot | domain->prot);
693 if (ret)
694 return ret;
695
696 iova += size;
697 }
698 }
699
700 return 0;
701}
702
Alex Williamson6fe10102015-02-06 10:58:56 -0700703/*
704 * We change our unmap behavior slightly depending on whether the IOMMU
705 * supports fine-grained superpages. IOMMUs like AMD-Vi will use a superpage
706 * for practically any contiguous power-of-two mapping we give it. This means
707 * we don't need to look for contiguous chunks ourselves to make unmapping
708 * more efficient. On IOMMUs with coarse-grained super pages, like Intel VT-d
709 * with discrete 2M/1G/512G/1T superpages, identifying contiguous chunks
710 * significantly boosts non-hugetlbfs mappings and doesn't seem to hurt when
711 * hugetlbfs is in use.
712 */
713static void vfio_test_domain_fgsp(struct vfio_domain *domain)
714{
715 struct page *pages;
716 int ret, order = get_order(PAGE_SIZE * 2);
717
718 pages = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
719 if (!pages)
720 return;
721
722 ret = iommu_map(domain->domain, 0, page_to_phys(pages), PAGE_SIZE * 2,
723 IOMMU_READ | IOMMU_WRITE | domain->prot);
724 if (!ret) {
725 size_t unmapped = iommu_unmap(domain->domain, 0, PAGE_SIZE);
726
727 if (unmapped == PAGE_SIZE)
728 iommu_unmap(domain->domain, PAGE_SIZE, PAGE_SIZE);
729 else
730 domain->fgsp = true;
731 }
732
733 __free_pages(pages, order);
734}
735
Alex Williamson73fa0d12012-07-31 08:16:23 -0600736static int vfio_iommu_type1_attach_group(void *iommu_data,
737 struct iommu_group *iommu_group)
738{
739 struct vfio_iommu *iommu = iommu_data;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700740 struct vfio_group *group, *g;
741 struct vfio_domain *domain, *d;
742 struct bus_type *bus = NULL;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600743 int ret;
744
Alex Williamson73fa0d12012-07-31 08:16:23 -0600745 mutex_lock(&iommu->lock);
746
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700747 list_for_each_entry(d, &iommu->domain_list, next) {
748 list_for_each_entry(g, &d->group_list, next) {
749 if (g->iommu_group != iommu_group)
750 continue;
751
Alex Williamson73fa0d12012-07-31 08:16:23 -0600752 mutex_unlock(&iommu->lock);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600753 return -EINVAL;
754 }
755 }
756
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700757 group = kzalloc(sizeof(*group), GFP_KERNEL);
758 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
759 if (!group || !domain) {
760 ret = -ENOMEM;
761 goto out_free;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600762 }
763
764 group->iommu_group = iommu_group;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700765
766 /* Determine bus_type in order to allocate a domain */
767 ret = iommu_group_for_each_dev(iommu_group, &bus, vfio_bus_type);
768 if (ret)
769 goto out_free;
770
771 domain->domain = iommu_domain_alloc(bus);
772 if (!domain->domain) {
773 ret = -EIO;
774 goto out_free;
775 }
776
Will Deaconf5c9ece2014-09-29 10:06:19 -0600777 if (iommu->nesting) {
778 int attr = 1;
779
780 ret = iommu_domain_set_attr(domain->domain, DOMAIN_ATTR_NESTING,
781 &attr);
782 if (ret)
783 goto out_domain;
784 }
785
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700786 ret = iommu_attach_group(domain->domain, iommu_group);
787 if (ret)
788 goto out_domain;
789
790 INIT_LIST_HEAD(&domain->group_list);
791 list_add(&group->next, &domain->group_list);
792
793 if (!allow_unsafe_interrupts &&
Joerg Roedeleb165f02014-09-05 10:56:05 +0200794 !iommu_capable(bus, IOMMU_CAP_INTR_REMAP)) {
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700795 pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n",
796 __func__);
797 ret = -EPERM;
798 goto out_detach;
799 }
800
Joerg Roedeleb165f02014-09-05 10:56:05 +0200801 if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700802 domain->prot |= IOMMU_CACHE;
803
804 /*
805 * Try to match an existing compatible domain. We don't want to
806 * preclude an IOMMU driver supporting multiple bus_types and being
807 * able to include different bus_types in the same IOMMU domain, so
808 * we test whether the domains use the same iommu_ops rather than
809 * testing if they're on the same bus_type.
810 */
811 list_for_each_entry(d, &iommu->domain_list, next) {
812 if (d->domain->ops == domain->domain->ops &&
813 d->prot == domain->prot) {
814 iommu_detach_group(domain->domain, iommu_group);
815 if (!iommu_attach_group(d->domain, iommu_group)) {
816 list_add(&group->next, &d->group_list);
817 iommu_domain_free(domain->domain);
818 kfree(domain);
819 mutex_unlock(&iommu->lock);
820 return 0;
821 }
822
823 ret = iommu_attach_group(domain->domain, iommu_group);
824 if (ret)
825 goto out_domain;
826 }
827 }
828
Alex Williamson6fe10102015-02-06 10:58:56 -0700829 vfio_test_domain_fgsp(domain);
830
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700831 /* replay mappings on new domains */
832 ret = vfio_iommu_replay(iommu, domain);
833 if (ret)
834 goto out_detach;
835
836 list_add(&domain->next, &iommu->domain_list);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600837
838 mutex_unlock(&iommu->lock);
839
840 return 0;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700841
842out_detach:
843 iommu_detach_group(domain->domain, iommu_group);
844out_domain:
845 iommu_domain_free(domain->domain);
846out_free:
847 kfree(domain);
848 kfree(group);
849 mutex_unlock(&iommu->lock);
850 return ret;
851}
852
853static void vfio_iommu_unmap_unpin_all(struct vfio_iommu *iommu)
854{
855 struct rb_node *node;
856
857 while ((node = rb_first(&iommu->dma_list)))
858 vfio_remove_dma(iommu, rb_entry(node, struct vfio_dma, node));
Alex Williamson73fa0d12012-07-31 08:16:23 -0600859}
860
861static void vfio_iommu_type1_detach_group(void *iommu_data,
862 struct iommu_group *iommu_group)
863{
864 struct vfio_iommu *iommu = iommu_data;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700865 struct vfio_domain *domain;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600866 struct vfio_group *group;
867
868 mutex_lock(&iommu->lock);
869
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700870 list_for_each_entry(domain, &iommu->domain_list, next) {
871 list_for_each_entry(group, &domain->group_list, next) {
872 if (group->iommu_group != iommu_group)
873 continue;
874
875 iommu_detach_group(domain->domain, iommu_group);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600876 list_del(&group->next);
877 kfree(group);
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700878 /*
879 * Group ownership provides privilege, if the group
880 * list is empty, the domain goes away. If it's the
881 * last domain, then all the mappings go away too.
882 */
883 if (list_empty(&domain->group_list)) {
884 if (list_is_singular(&iommu->domain_list))
885 vfio_iommu_unmap_unpin_all(iommu);
886 iommu_domain_free(domain->domain);
887 list_del(&domain->next);
888 kfree(domain);
889 }
890 goto done;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600891 }
892 }
893
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700894done:
Alex Williamson73fa0d12012-07-31 08:16:23 -0600895 mutex_unlock(&iommu->lock);
896}
897
898static void *vfio_iommu_type1_open(unsigned long arg)
899{
900 struct vfio_iommu *iommu;
901
Alex Williamson73fa0d12012-07-31 08:16:23 -0600902 iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
903 if (!iommu)
904 return ERR_PTR(-ENOMEM);
905
Will Deaconf5c9ece2014-09-29 10:06:19 -0600906 switch (arg) {
907 case VFIO_TYPE1_IOMMU:
908 break;
909 case VFIO_TYPE1_NESTING_IOMMU:
910 iommu->nesting = true;
911 case VFIO_TYPE1v2_IOMMU:
912 iommu->v2 = true;
913 break;
914 default:
915 kfree(iommu);
916 return ERR_PTR(-EINVAL);
917 }
918
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700919 INIT_LIST_HEAD(&iommu->domain_list);
Alex Williamsoncd9b2262013-06-21 09:37:50 -0600920 iommu->dma_list = RB_ROOT;
Alex Williamson4f97abd2019-04-03 12:36:21 -0600921 iommu->dma_avail = dma_entry_limit;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600922 mutex_init(&iommu->lock);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600923
924 return iommu;
925}
926
927static void vfio_iommu_type1_release(void *iommu_data)
928{
929 struct vfio_iommu *iommu = iommu_data;
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700930 struct vfio_domain *domain, *domain_tmp;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600931 struct vfio_group *group, *group_tmp;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600932
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700933 vfio_iommu_unmap_unpin_all(iommu);
934
935 list_for_each_entry_safe(domain, domain_tmp,
936 &iommu->domain_list, next) {
937 list_for_each_entry_safe(group, group_tmp,
938 &domain->group_list, next) {
939 iommu_detach_group(domain->domain, group->iommu_group);
940 list_del(&group->next);
941 kfree(group);
942 }
943 iommu_domain_free(domain->domain);
944 list_del(&domain->next);
945 kfree(domain);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600946 }
947
Alex Williamson73fa0d12012-07-31 08:16:23 -0600948 kfree(iommu);
949}
950
Alex Williamsonaa429312014-02-26 11:38:37 -0700951static int vfio_domains_have_iommu_cache(struct vfio_iommu *iommu)
952{
953 struct vfio_domain *domain;
954 int ret = 1;
955
956 mutex_lock(&iommu->lock);
957 list_for_each_entry(domain, &iommu->domain_list, next) {
958 if (!(domain->prot & IOMMU_CACHE)) {
959 ret = 0;
960 break;
961 }
962 }
963 mutex_unlock(&iommu->lock);
964
965 return ret;
966}
967
Alex Williamson73fa0d12012-07-31 08:16:23 -0600968static long vfio_iommu_type1_ioctl(void *iommu_data,
969 unsigned int cmd, unsigned long arg)
970{
971 struct vfio_iommu *iommu = iommu_data;
972 unsigned long minsz;
973
974 if (cmd == VFIO_CHECK_EXTENSION) {
975 switch (arg) {
976 case VFIO_TYPE1_IOMMU:
Alex Williamson1ef3e2b2014-02-26 11:38:36 -0700977 case VFIO_TYPE1v2_IOMMU:
Will Deaconf5c9ece2014-09-29 10:06:19 -0600978 case VFIO_TYPE1_NESTING_IOMMU:
Alex Williamson73fa0d12012-07-31 08:16:23 -0600979 return 1;
Alex Williamsonaa429312014-02-26 11:38:37 -0700980 case VFIO_DMA_CC_IOMMU:
981 if (!iommu)
982 return 0;
983 return vfio_domains_have_iommu_cache(iommu);
Alex Williamson73fa0d12012-07-31 08:16:23 -0600984 default:
985 return 0;
986 }
987 } else if (cmd == VFIO_IOMMU_GET_INFO) {
988 struct vfio_iommu_type1_info info;
989
990 minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes);
991
992 if (copy_from_user(&info, (void __user *)arg, minsz))
993 return -EFAULT;
994
995 if (info.argsz < minsz)
996 return -EINVAL;
997
Pierre Moreld4f50ee2015-12-23 13:08:05 +0100998 info.flags = VFIO_IOMMU_INFO_PGSIZES;
Alex Williamson73fa0d12012-07-31 08:16:23 -0600999
Alex Williamson1ef3e2b2014-02-26 11:38:36 -07001000 info.iova_pgsizes = vfio_pgsize_bitmap(iommu);
Alex Williamson73fa0d12012-07-31 08:16:23 -06001001
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +02001002 return copy_to_user((void __user *)arg, &info, minsz) ?
1003 -EFAULT : 0;
Alex Williamson73fa0d12012-07-31 08:16:23 -06001004
1005 } else if (cmd == VFIO_IOMMU_MAP_DMA) {
1006 struct vfio_iommu_type1_dma_map map;
1007 uint32_t mask = VFIO_DMA_MAP_FLAG_READ |
1008 VFIO_DMA_MAP_FLAG_WRITE;
1009
1010 minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);
1011
1012 if (copy_from_user(&map, (void __user *)arg, minsz))
1013 return -EFAULT;
1014
1015 if (map.argsz < minsz || map.flags & ~mask)
1016 return -EINVAL;
1017
1018 return vfio_dma_do_map(iommu, &map);
1019
1020 } else if (cmd == VFIO_IOMMU_UNMAP_DMA) {
1021 struct vfio_iommu_type1_dma_unmap unmap;
Alex Williamson166fd7d2013-06-21 09:38:02 -06001022 long ret;
Alex Williamson73fa0d12012-07-31 08:16:23 -06001023
1024 minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size);
1025
1026 if (copy_from_user(&unmap, (void __user *)arg, minsz))
1027 return -EFAULT;
1028
1029 if (unmap.argsz < minsz || unmap.flags)
1030 return -EINVAL;
1031
Alex Williamson166fd7d2013-06-21 09:38:02 -06001032 ret = vfio_dma_do_unmap(iommu, &unmap);
1033 if (ret)
1034 return ret;
1035
Michael S. Tsirkin8160c4e2016-02-28 16:31:39 +02001036 return copy_to_user((void __user *)arg, &unmap, minsz) ?
1037 -EFAULT : 0;
Alex Williamson73fa0d12012-07-31 08:16:23 -06001038 }
1039
1040 return -ENOTTY;
1041}
1042
1043static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
1044 .name = "vfio-iommu-type1",
1045 .owner = THIS_MODULE,
1046 .open = vfio_iommu_type1_open,
1047 .release = vfio_iommu_type1_release,
1048 .ioctl = vfio_iommu_type1_ioctl,
1049 .attach_group = vfio_iommu_type1_attach_group,
1050 .detach_group = vfio_iommu_type1_detach_group,
1051};
1052
1053static int __init vfio_iommu_type1_init(void)
1054{
Alex Williamson73fa0d12012-07-31 08:16:23 -06001055 return vfio_register_iommu_driver(&vfio_iommu_driver_ops_type1);
1056}
1057
1058static void __exit vfio_iommu_type1_cleanup(void)
1059{
1060 vfio_unregister_iommu_driver(&vfio_iommu_driver_ops_type1);
1061}
1062
1063module_init(vfio_iommu_type1_init);
1064module_exit(vfio_iommu_type1_cleanup);
1065
1066MODULE_VERSION(DRIVER_VERSION);
1067MODULE_LICENSE("GPL v2");
1068MODULE_AUTHOR(DRIVER_AUTHOR);
1069MODULE_DESCRIPTION(DRIVER_DESC);