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