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