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