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