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