Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 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> |
| 33 | #include <linux/pci.h> /* pci_bus_type */ |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 34 | #include <linux/rbtree.h> |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 35 | #include <linux/sched.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/uaccess.h> |
| 38 | #include <linux/vfio.h> |
| 39 | #include <linux/workqueue.h> |
| 40 | |
| 41 | #define DRIVER_VERSION "0.2" |
| 42 | #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" |
| 43 | #define DRIVER_DESC "Type1 IOMMU driver for VFIO" |
| 44 | |
| 45 | static bool allow_unsafe_interrupts; |
| 46 | module_param_named(allow_unsafe_interrupts, |
| 47 | allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR); |
| 48 | MODULE_PARM_DESC(allow_unsafe_interrupts, |
| 49 | "Enable VFIO IOMMU support for on platforms without interrupt remapping support."); |
| 50 | |
Alex Williamson | 5c6c2b2 | 2013-06-21 09:38:11 -0600 | [diff] [blame] | 51 | static bool disable_hugepages; |
| 52 | module_param_named(disable_hugepages, |
| 53 | disable_hugepages, bool, S_IRUGO | S_IWUSR); |
| 54 | MODULE_PARM_DESC(disable_hugepages, |
| 55 | "Disable VFIO IOMMU support for IOMMU hugepages."); |
| 56 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 57 | struct vfio_iommu { |
| 58 | struct iommu_domain *domain; |
| 59 | struct mutex lock; |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 60 | struct rb_root dma_list; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 61 | struct list_head group_list; |
| 62 | bool cache; |
| 63 | }; |
| 64 | |
| 65 | struct vfio_dma { |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 66 | struct rb_node node; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 67 | dma_addr_t iova; /* Device address */ |
| 68 | unsigned long vaddr; /* Process virtual addr */ |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 69 | size_t size; /* Map size (bytes) */ |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 70 | int prot; /* IOMMU_READ/WRITE */ |
| 71 | }; |
| 72 | |
| 73 | struct vfio_group { |
| 74 | struct iommu_group *iommu_group; |
| 75 | struct list_head next; |
| 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * This code handles mapping and unmapping of user data buffers |
| 80 | * into DMA'ble space using the IOMMU |
| 81 | */ |
| 82 | |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 83 | static struct vfio_dma *vfio_find_dma(struct vfio_iommu *iommu, |
| 84 | dma_addr_t start, size_t size) |
| 85 | { |
| 86 | struct rb_node *node = iommu->dma_list.rb_node; |
| 87 | |
| 88 | while (node) { |
| 89 | struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node); |
| 90 | |
| 91 | if (start + size <= dma->iova) |
| 92 | node = node->rb_left; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 93 | else if (start >= dma->iova + dma->size) |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 94 | node = node->rb_right; |
| 95 | else |
| 96 | return dma; |
| 97 | } |
| 98 | |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | static void vfio_insert_dma(struct vfio_iommu *iommu, struct vfio_dma *new) |
| 103 | { |
| 104 | struct rb_node **link = &iommu->dma_list.rb_node, *parent = NULL; |
| 105 | struct vfio_dma *dma; |
| 106 | |
| 107 | while (*link) { |
| 108 | parent = *link; |
| 109 | dma = rb_entry(parent, struct vfio_dma, node); |
| 110 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 111 | if (new->iova + new->size <= dma->iova) |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 112 | link = &(*link)->rb_left; |
| 113 | else |
| 114 | link = &(*link)->rb_right; |
| 115 | } |
| 116 | |
| 117 | rb_link_node(&new->node, parent, link); |
| 118 | rb_insert_color(&new->node, &iommu->dma_list); |
| 119 | } |
| 120 | |
| 121 | static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *old) |
| 122 | { |
| 123 | rb_erase(&old->node, &iommu->dma_list); |
| 124 | } |
| 125 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 126 | struct vwork { |
| 127 | struct mm_struct *mm; |
| 128 | long npage; |
| 129 | struct work_struct work; |
| 130 | }; |
| 131 | |
| 132 | /* delayed decrement/increment for locked_vm */ |
| 133 | static void vfio_lock_acct_bg(struct work_struct *work) |
| 134 | { |
| 135 | struct vwork *vwork = container_of(work, struct vwork, work); |
| 136 | struct mm_struct *mm; |
| 137 | |
| 138 | mm = vwork->mm; |
| 139 | down_write(&mm->mmap_sem); |
| 140 | mm->locked_vm += vwork->npage; |
| 141 | up_write(&mm->mmap_sem); |
| 142 | mmput(mm); |
| 143 | kfree(vwork); |
| 144 | } |
| 145 | |
| 146 | static void vfio_lock_acct(long npage) |
| 147 | { |
| 148 | struct vwork *vwork; |
| 149 | struct mm_struct *mm; |
| 150 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 151 | if (!current->mm || !npage) |
| 152 | return; /* process exited or nothing to do */ |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 153 | |
| 154 | if (down_write_trylock(¤t->mm->mmap_sem)) { |
| 155 | current->mm->locked_vm += npage; |
| 156 | up_write(¤t->mm->mmap_sem); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Couldn't get mmap_sem lock, so must setup to update |
| 162 | * mm->locked_vm later. If locked_vm were atomic, we |
| 163 | * wouldn't need this silliness |
| 164 | */ |
| 165 | vwork = kmalloc(sizeof(struct vwork), GFP_KERNEL); |
| 166 | if (!vwork) |
| 167 | return; |
| 168 | mm = get_task_mm(current); |
| 169 | if (!mm) { |
| 170 | kfree(vwork); |
| 171 | return; |
| 172 | } |
| 173 | INIT_WORK(&vwork->work, vfio_lock_acct_bg); |
| 174 | vwork->mm = mm; |
| 175 | vwork->npage = npage; |
| 176 | schedule_work(&vwork->work); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Some mappings aren't backed by a struct page, for example an mmap'd |
| 181 | * MMIO range for our own or another device. These use a different |
| 182 | * pfn conversion and shouldn't be tracked as locked pages. |
| 183 | */ |
| 184 | static bool is_invalid_reserved_pfn(unsigned long pfn) |
| 185 | { |
| 186 | if (pfn_valid(pfn)) { |
| 187 | bool reserved; |
| 188 | struct page *tail = pfn_to_page(pfn); |
| 189 | struct page *head = compound_trans_head(tail); |
| 190 | reserved = !!(PageReserved(head)); |
| 191 | if (head != tail) { |
| 192 | /* |
| 193 | * "head" is not a dangling pointer |
| 194 | * (compound_trans_head takes care of that) |
| 195 | * but the hugepage may have been split |
| 196 | * from under us (and we may not hold a |
| 197 | * reference count on the head page so it can |
| 198 | * be reused before we run PageReferenced), so |
| 199 | * we've to check PageTail before returning |
| 200 | * what we just read. |
| 201 | */ |
| 202 | smp_rmb(); |
| 203 | if (PageTail(tail)) |
| 204 | return reserved; |
| 205 | } |
| 206 | return PageReserved(tail); |
| 207 | } |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | static int put_pfn(unsigned long pfn, int prot) |
| 213 | { |
| 214 | if (!is_invalid_reserved_pfn(pfn)) { |
| 215 | struct page *page = pfn_to_page(pfn); |
| 216 | if (prot & IOMMU_WRITE) |
| 217 | SetPageDirty(page); |
| 218 | put_page(page); |
| 219 | return 1; |
| 220 | } |
| 221 | return 0; |
| 222 | } |
| 223 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 224 | static int vaddr_get_pfn(unsigned long vaddr, int prot, unsigned long *pfn) |
| 225 | { |
| 226 | struct page *page[1]; |
| 227 | struct vm_area_struct *vma; |
| 228 | int ret = -EFAULT; |
| 229 | |
| 230 | if (get_user_pages_fast(vaddr, 1, !!(prot & IOMMU_WRITE), page) == 1) { |
| 231 | *pfn = page_to_pfn(page[0]); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | down_read(¤t->mm->mmap_sem); |
| 236 | |
| 237 | vma = find_vma_intersection(current->mm, vaddr, vaddr + 1); |
| 238 | |
| 239 | if (vma && vma->vm_flags & VM_PFNMAP) { |
| 240 | *pfn = ((vaddr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 241 | if (is_invalid_reserved_pfn(*pfn)) |
| 242 | ret = 0; |
| 243 | } |
| 244 | |
| 245 | up_read(¤t->mm->mmap_sem); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 250 | /* |
| 251 | * Attempt to pin pages. We really don't want to track all the pfns and |
| 252 | * the iommu can only map chunks of consecutive pfns anyway, so get the |
| 253 | * first page and all consecutive pages with the same locking. |
| 254 | */ |
| 255 | static long vfio_pin_pages(unsigned long vaddr, long npage, |
| 256 | int prot, unsigned long *pfn_base) |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 257 | { |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 258 | unsigned long limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 259 | bool lock_cap = capable(CAP_IPC_LOCK); |
| 260 | long ret, i; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 261 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 262 | if (!current->mm) |
| 263 | return -ENODEV; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 264 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 265 | ret = vaddr_get_pfn(vaddr, prot, pfn_base); |
| 266 | if (ret) |
| 267 | return ret; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 268 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 269 | if (is_invalid_reserved_pfn(*pfn_base)) |
| 270 | return 1; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 271 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 272 | if (!lock_cap && current->mm->locked_vm + 1 > limit) { |
| 273 | put_pfn(*pfn_base, prot); |
| 274 | pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", __func__, |
| 275 | limit << PAGE_SHIFT); |
| 276 | return -ENOMEM; |
| 277 | } |
| 278 | |
Alex Williamson | 5c6c2b2 | 2013-06-21 09:38:11 -0600 | [diff] [blame] | 279 | if (unlikely(disable_hugepages)) { |
| 280 | vfio_lock_acct(1); |
| 281 | return 1; |
| 282 | } |
| 283 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 284 | /* Lock all the consecutive pages from pfn_base */ |
| 285 | for (i = 1, vaddr += PAGE_SIZE; i < npage; i++, vaddr += PAGE_SIZE) { |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 286 | unsigned long pfn = 0; |
| 287 | |
| 288 | ret = vaddr_get_pfn(vaddr, prot, &pfn); |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 289 | if (ret) |
| 290 | break; |
| 291 | |
| 292 | if (pfn != *pfn_base + i || is_invalid_reserved_pfn(pfn)) { |
| 293 | put_pfn(pfn, prot); |
| 294 | break; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 295 | } |
| 296 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 297 | if (!lock_cap && current->mm->locked_vm + i + 1 > limit) { |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 298 | put_pfn(pfn, prot); |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 299 | pr_warn("%s: RLIMIT_MEMLOCK (%ld) exceeded\n", |
| 300 | __func__, limit << PAGE_SHIFT); |
| 301 | break; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 302 | } |
| 303 | } |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 304 | |
| 305 | vfio_lock_acct(i); |
| 306 | |
| 307 | return i; |
| 308 | } |
| 309 | |
| 310 | static long vfio_unpin_pages(unsigned long pfn, long npage, |
| 311 | int prot, bool do_accounting) |
| 312 | { |
| 313 | unsigned long unlocked = 0; |
| 314 | long i; |
| 315 | |
| 316 | for (i = 0; i < npage; i++) |
| 317 | unlocked += put_pfn(pfn++, prot); |
| 318 | |
| 319 | if (do_accounting) |
| 320 | vfio_lock_acct(-unlocked); |
| 321 | |
| 322 | return unlocked; |
| 323 | } |
| 324 | |
| 325 | static int vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma, |
| 326 | dma_addr_t iova, size_t *size) |
| 327 | { |
| 328 | dma_addr_t start = iova, end = iova + *size; |
| 329 | long unlocked = 0; |
| 330 | |
| 331 | while (iova < end) { |
| 332 | size_t unmapped; |
| 333 | phys_addr_t phys; |
| 334 | |
| 335 | /* |
| 336 | * We use the IOMMU to track the physical address. This |
| 337 | * saves us from having a lot more entries in our mapping |
| 338 | * tree. The downside is that we don't track the size |
| 339 | * used to do the mapping. We request unmap of a single |
| 340 | * page, but expect IOMMUs that support large pages to |
| 341 | * unmap a larger chunk. |
| 342 | */ |
| 343 | phys = iommu_iova_to_phys(iommu->domain, iova); |
| 344 | if (WARN_ON(!phys)) { |
| 345 | iova += PAGE_SIZE; |
| 346 | continue; |
| 347 | } |
| 348 | |
| 349 | unmapped = iommu_unmap(iommu->domain, iova, PAGE_SIZE); |
| 350 | if (!unmapped) |
| 351 | break; |
| 352 | |
| 353 | unlocked += vfio_unpin_pages(phys >> PAGE_SHIFT, |
| 354 | unmapped >> PAGE_SHIFT, |
| 355 | dma->prot, false); |
| 356 | iova += unmapped; |
| 357 | } |
| 358 | |
| 359 | vfio_lock_acct(-unlocked); |
| 360 | |
| 361 | *size = iova - start; |
| 362 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 366 | static int vfio_remove_dma_overlap(struct vfio_iommu *iommu, dma_addr_t start, |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 367 | size_t *size, struct vfio_dma *dma) |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 368 | { |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 369 | size_t offset, overlap, tmp; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 370 | struct vfio_dma *split; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 371 | int ret; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 372 | |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 373 | if (!*size) |
| 374 | return 0; |
| 375 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 376 | /* |
| 377 | * Existing dma region is completely covered, unmap all. This is |
| 378 | * the likely case since userspace tends to map and unmap buffers |
| 379 | * in one shot rather than multiple mappings within a buffer. |
| 380 | */ |
| 381 | if (likely(start <= dma->iova && |
| 382 | start + *size >= dma->iova + dma->size)) { |
| 383 | *size = dma->size; |
| 384 | ret = vfio_unmap_unpin(iommu, dma, dma->iova, size); |
| 385 | if (ret) |
| 386 | return ret; |
| 387 | |
| 388 | /* |
| 389 | * Did we remove more than we have? Should never happen |
| 390 | * since a vfio_dma is contiguous in iova and vaddr. |
| 391 | */ |
| 392 | WARN_ON(*size != dma->size); |
| 393 | |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 394 | vfio_remove_dma(iommu, dma); |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 395 | kfree(dma); |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 396 | return 0; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /* Overlap low address of existing range */ |
| 400 | if (start <= dma->iova) { |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 401 | overlap = start + *size - dma->iova; |
| 402 | ret = vfio_unmap_unpin(iommu, dma, dma->iova, &overlap); |
| 403 | if (ret) |
| 404 | return ret; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 405 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 406 | vfio_remove_dma(iommu, dma); |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 407 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 408 | /* |
| 409 | * Check, we may have removed to whole vfio_dma. If not |
| 410 | * fixup and re-insert. |
| 411 | */ |
| 412 | if (overlap < dma->size) { |
| 413 | dma->iova += overlap; |
| 414 | dma->vaddr += overlap; |
| 415 | dma->size -= overlap; |
| 416 | vfio_insert_dma(iommu, dma); |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 417 | } else |
| 418 | kfree(dma); |
| 419 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 420 | *size = overlap; |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 421 | return 0; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* Overlap high address of existing range */ |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 425 | if (start + *size >= dma->iova + dma->size) { |
| 426 | offset = start - dma->iova; |
| 427 | overlap = dma->size - offset; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 428 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 429 | ret = vfio_unmap_unpin(iommu, dma, start, &overlap); |
| 430 | if (ret) |
| 431 | return ret; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 432 | |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 433 | dma->size -= overlap; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 434 | *size = overlap; |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 435 | return 0; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* Split existing */ |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 439 | split = kzalloc(sizeof(*split), GFP_KERNEL); |
| 440 | if (!split) |
| 441 | return -ENOMEM; |
| 442 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 443 | offset = start - dma->iova; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 444 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 445 | ret = vfio_unmap_unpin(iommu, dma, start, size); |
| 446 | if (ret) |
| 447 | return ret; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 448 | |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 449 | if (!*size) { |
| 450 | kfree(split); |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 454 | tmp = dma->size; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 455 | |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 456 | /* Resize the lower vfio_dma in place, before the below insert */ |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 457 | dma->size = offset; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 458 | |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 459 | /* Insert new for remainder, assuming it didn't all get unmapped */ |
| 460 | if (likely(offset + *size < tmp)) { |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 461 | split->size = tmp - offset - *size; |
| 462 | split->iova = dma->iova + offset + *size; |
| 463 | split->vaddr = dma->vaddr + offset + *size; |
| 464 | split->prot = dma->prot; |
| 465 | vfio_insert_dma(iommu, split); |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 466 | } else |
| 467 | kfree(split); |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 468 | |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 469 | return 0; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static int vfio_dma_do_unmap(struct vfio_iommu *iommu, |
| 473 | struct vfio_iommu_type1_dma_unmap *unmap) |
| 474 | { |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 475 | uint64_t mask; |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 476 | struct vfio_dma *dma; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 477 | size_t unmapped = 0, size; |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 478 | int ret = 0; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 479 | |
| 480 | mask = ((uint64_t)1 << __ffs(iommu->domain->ops->pgsize_bitmap)) - 1; |
| 481 | |
| 482 | if (unmap->iova & mask) |
| 483 | return -EINVAL; |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 484 | if (!unmap->size || unmap->size & mask) |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 485 | return -EINVAL; |
| 486 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 487 | WARN_ON(mask & PAGE_MASK); |
| 488 | |
| 489 | mutex_lock(&iommu->lock); |
| 490 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 491 | while ((dma = vfio_find_dma(iommu, unmap->iova, unmap->size))) { |
| 492 | size = unmap->size; |
| 493 | ret = vfio_remove_dma_overlap(iommu, unmap->iova, &size, dma); |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 494 | if (ret || !size) |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 495 | break; |
| 496 | unmapped += size; |
| 497 | } |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 498 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 499 | mutex_unlock(&iommu->lock); |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * We may unmap more than requested, update the unmap struct so |
| 503 | * userspace can know. |
| 504 | */ |
| 505 | unmap->size = unmapped; |
| 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Turns out AMD IOMMU has a page table bug where it won't map large pages |
| 512 | * to a region that previously mapped smaller pages. This should be fixed |
| 513 | * soon, so this is just a temporary workaround to break mappings down into |
| 514 | * PAGE_SIZE. Better to map smaller pages than nothing. |
| 515 | */ |
| 516 | static int map_try_harder(struct vfio_iommu *iommu, dma_addr_t iova, |
| 517 | unsigned long pfn, long npage, int prot) |
| 518 | { |
| 519 | long i; |
| 520 | int ret; |
| 521 | |
| 522 | for (i = 0; i < npage; i++, pfn++, iova += PAGE_SIZE) { |
| 523 | ret = iommu_map(iommu->domain, iova, |
| 524 | (phys_addr_t)pfn << PAGE_SHIFT, |
| 525 | PAGE_SIZE, prot); |
| 526 | if (ret) |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | for (; i < npage && i > 0; i--, iova -= PAGE_SIZE) |
| 531 | iommu_unmap(iommu->domain, iova, PAGE_SIZE); |
| 532 | |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 533 | return ret; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static int vfio_dma_do_map(struct vfio_iommu *iommu, |
| 537 | struct vfio_iommu_type1_dma_map *map) |
| 538 | { |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 539 | dma_addr_t end, iova; |
| 540 | unsigned long vaddr = map->vaddr; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 541 | size_t size = map->size; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 542 | long npage; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 543 | int ret = 0, prot = 0; |
| 544 | uint64_t mask; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 545 | |
| 546 | end = map->iova + map->size; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 547 | |
| 548 | mask = ((uint64_t)1 << __ffs(iommu->domain->ops->pgsize_bitmap)) - 1; |
| 549 | |
| 550 | /* READ/WRITE from device perspective */ |
| 551 | if (map->flags & VFIO_DMA_MAP_FLAG_WRITE) |
| 552 | prot |= IOMMU_WRITE; |
| 553 | if (map->flags & VFIO_DMA_MAP_FLAG_READ) |
| 554 | prot |= IOMMU_READ; |
| 555 | |
| 556 | if (!prot) |
| 557 | return -EINVAL; /* No READ/WRITE? */ |
| 558 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 559 | if (iommu->cache) |
| 560 | prot |= IOMMU_CACHE; |
| 561 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 562 | if (vaddr & mask) |
| 563 | return -EINVAL; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 564 | if (map->iova & mask) |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 565 | return -EINVAL; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 566 | if (!map->size || map->size & mask) |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 567 | return -EINVAL; |
| 568 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 569 | WARN_ON(mask & PAGE_MASK); |
| 570 | |
| 571 | /* Don't allow IOVA wrap */ |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 572 | if (end && end < map->iova) |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 573 | return -EINVAL; |
| 574 | |
| 575 | /* Don't allow virtual address wrap */ |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 576 | if (vaddr + map->size && vaddr + map->size < vaddr) |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 577 | return -EINVAL; |
| 578 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 579 | mutex_lock(&iommu->lock); |
| 580 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 581 | if (vfio_find_dma(iommu, map->iova, map->size)) { |
| 582 | mutex_unlock(&iommu->lock); |
| 583 | return -EEXIST; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 584 | } |
| 585 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 586 | for (iova = map->iova; iova < end; iova += size, vaddr += size) { |
| 587 | struct vfio_dma *dma = NULL; |
| 588 | unsigned long pfn; |
| 589 | long i; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 590 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 591 | /* Pin a contiguous chunk of memory */ |
| 592 | npage = vfio_pin_pages(vaddr, (end - iova) >> PAGE_SHIFT, |
| 593 | prot, &pfn); |
| 594 | if (npage <= 0) { |
| 595 | WARN_ON(!npage); |
| 596 | ret = (int)npage; |
| 597 | break; |
| 598 | } |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 599 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 600 | /* Verify pages are not already mapped */ |
| 601 | for (i = 0; i < npage; i++) { |
| 602 | if (iommu_iova_to_phys(iommu->domain, |
| 603 | iova + (i << PAGE_SHIFT))) { |
| 604 | vfio_unpin_pages(pfn, npage, prot, true); |
| 605 | ret = -EBUSY; |
| 606 | break; |
| 607 | } |
| 608 | } |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 609 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 610 | ret = iommu_map(iommu->domain, iova, |
| 611 | (phys_addr_t)pfn << PAGE_SHIFT, |
| 612 | npage << PAGE_SHIFT, prot); |
| 613 | if (ret) { |
| 614 | if (ret != -EBUSY || |
| 615 | map_try_harder(iommu, iova, pfn, npage, prot)) { |
| 616 | vfio_unpin_pages(pfn, npage, prot, true); |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | size = npage << PAGE_SHIFT; |
| 622 | |
| 623 | /* |
| 624 | * Check if we abut a region below - nothing below 0. |
| 625 | * This is the most likely case when mapping chunks of |
| 626 | * physically contiguous regions within a virtual address |
| 627 | * range. Update the abutting entry in place since iova |
| 628 | * doesn't change. |
| 629 | */ |
| 630 | if (likely(iova)) { |
| 631 | struct vfio_dma *tmp; |
| 632 | tmp = vfio_find_dma(iommu, iova - 1, 1); |
| 633 | if (tmp && tmp->prot == prot && |
| 634 | tmp->vaddr + tmp->size == vaddr) { |
| 635 | tmp->size += size; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 636 | iova = tmp->iova; |
| 637 | size = tmp->size; |
| 638 | vaddr = tmp->vaddr; |
| 639 | dma = tmp; |
| 640 | } |
| 641 | } |
| 642 | |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 643 | /* |
| 644 | * Check if we abut a region above - nothing above ~0 + 1. |
| 645 | * If we abut above and below, remove and free. If only |
| 646 | * abut above, remove, modify, reinsert. |
| 647 | */ |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 648 | if (likely(iova + size)) { |
| 649 | struct vfio_dma *tmp; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 650 | tmp = vfio_find_dma(iommu, iova + size, 1); |
| 651 | if (tmp && tmp->prot == prot && |
| 652 | tmp->vaddr == vaddr + size) { |
| 653 | vfio_remove_dma(iommu, tmp); |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 654 | if (dma) { |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 655 | dma->size += tmp->size; |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 656 | kfree(tmp); |
| 657 | } else { |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 658 | size += tmp->size; |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 659 | tmp->size = size; |
| 660 | tmp->iova = iova; |
| 661 | tmp->vaddr = vaddr; |
| 662 | vfio_insert_dma(iommu, tmp); |
| 663 | dma = tmp; |
| 664 | } |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
| 668 | if (!dma) { |
| 669 | dma = kzalloc(sizeof(*dma), GFP_KERNEL); |
| 670 | if (!dma) { |
| 671 | iommu_unmap(iommu->domain, iova, size); |
| 672 | vfio_unpin_pages(pfn, npage, prot, true); |
| 673 | ret = -ENOMEM; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | dma->size = size; |
| 678 | dma->iova = iova; |
| 679 | dma->vaddr = vaddr; |
| 680 | dma->prot = prot; |
| 681 | vfio_insert_dma(iommu, dma); |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 685 | if (ret) { |
| 686 | struct vfio_dma *tmp; |
| 687 | iova = map->iova; |
| 688 | size = map->size; |
| 689 | while ((tmp = vfio_find_dma(iommu, iova, size))) { |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 690 | int r = vfio_remove_dma_overlap(iommu, iova, |
| 691 | &size, tmp); |
| 692 | if (WARN_ON(r || !size)) |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 693 | break; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 697 | mutex_unlock(&iommu->lock); |
| 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | static int vfio_iommu_type1_attach_group(void *iommu_data, |
| 702 | struct iommu_group *iommu_group) |
| 703 | { |
| 704 | struct vfio_iommu *iommu = iommu_data; |
| 705 | struct vfio_group *group, *tmp; |
| 706 | int ret; |
| 707 | |
| 708 | group = kzalloc(sizeof(*group), GFP_KERNEL); |
| 709 | if (!group) |
| 710 | return -ENOMEM; |
| 711 | |
| 712 | mutex_lock(&iommu->lock); |
| 713 | |
| 714 | list_for_each_entry(tmp, &iommu->group_list, next) { |
| 715 | if (tmp->iommu_group == iommu_group) { |
| 716 | mutex_unlock(&iommu->lock); |
| 717 | kfree(group); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * TODO: Domain have capabilities that might change as we add |
| 724 | * groups (see iommu->cache, currently never set). Check for |
| 725 | * them and potentially disallow groups to be attached when it |
| 726 | * would change capabilities (ugh). |
| 727 | */ |
| 728 | ret = iommu_attach_group(iommu->domain, iommu_group); |
| 729 | if (ret) { |
| 730 | mutex_unlock(&iommu->lock); |
| 731 | kfree(group); |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | group->iommu_group = iommu_group; |
| 736 | list_add(&group->next, &iommu->group_list); |
| 737 | |
| 738 | mutex_unlock(&iommu->lock); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static void vfio_iommu_type1_detach_group(void *iommu_data, |
| 744 | struct iommu_group *iommu_group) |
| 745 | { |
| 746 | struct vfio_iommu *iommu = iommu_data; |
| 747 | struct vfio_group *group; |
| 748 | |
| 749 | mutex_lock(&iommu->lock); |
| 750 | |
| 751 | list_for_each_entry(group, &iommu->group_list, next) { |
| 752 | if (group->iommu_group == iommu_group) { |
| 753 | iommu_detach_group(iommu->domain, iommu_group); |
| 754 | list_del(&group->next); |
| 755 | kfree(group); |
| 756 | break; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | mutex_unlock(&iommu->lock); |
| 761 | } |
| 762 | |
| 763 | static void *vfio_iommu_type1_open(unsigned long arg) |
| 764 | { |
| 765 | struct vfio_iommu *iommu; |
| 766 | |
| 767 | if (arg != VFIO_TYPE1_IOMMU) |
| 768 | return ERR_PTR(-EINVAL); |
| 769 | |
| 770 | iommu = kzalloc(sizeof(*iommu), GFP_KERNEL); |
| 771 | if (!iommu) |
| 772 | return ERR_PTR(-ENOMEM); |
| 773 | |
| 774 | INIT_LIST_HEAD(&iommu->group_list); |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 775 | iommu->dma_list = RB_ROOT; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 776 | mutex_init(&iommu->lock); |
| 777 | |
| 778 | /* |
| 779 | * Wish we didn't have to know about bus_type here. |
| 780 | */ |
| 781 | iommu->domain = iommu_domain_alloc(&pci_bus_type); |
| 782 | if (!iommu->domain) { |
| 783 | kfree(iommu); |
| 784 | return ERR_PTR(-EIO); |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Wish we could specify required capabilities rather than create |
| 789 | * a domain, see what comes out and hope it doesn't change along |
| 790 | * the way. Fortunately we know interrupt remapping is global for |
| 791 | * our iommus. |
| 792 | */ |
| 793 | if (!allow_unsafe_interrupts && |
| 794 | !iommu_domain_has_cap(iommu->domain, IOMMU_CAP_INTR_REMAP)) { |
| 795 | pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n", |
| 796 | __func__); |
| 797 | iommu_domain_free(iommu->domain); |
| 798 | kfree(iommu); |
| 799 | return ERR_PTR(-EPERM); |
| 800 | } |
| 801 | |
| 802 | return iommu; |
| 803 | } |
| 804 | |
| 805 | static void vfio_iommu_type1_release(void *iommu_data) |
| 806 | { |
| 807 | struct vfio_iommu *iommu = iommu_data; |
| 808 | struct vfio_group *group, *group_tmp; |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 809 | struct rb_node *node; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 810 | |
| 811 | list_for_each_entry_safe(group, group_tmp, &iommu->group_list, next) { |
| 812 | iommu_detach_group(iommu->domain, group->iommu_group); |
| 813 | list_del(&group->next); |
| 814 | kfree(group); |
| 815 | } |
| 816 | |
Alex Williamson | cd9b226 | 2013-06-21 09:37:50 -0600 | [diff] [blame] | 817 | while ((node = rb_first(&iommu->dma_list))) { |
| 818 | struct vfio_dma *dma = rb_entry(node, struct vfio_dma, node); |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 819 | size_t size = dma->size; |
| 820 | vfio_remove_dma_overlap(iommu, dma->iova, &size, dma); |
Alex Williamson | f5bfdbf | 2013-06-25 16:01:44 -0600 | [diff] [blame^] | 821 | if (WARN_ON(!size)) |
| 822 | break; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | iommu_domain_free(iommu->domain); |
| 826 | iommu->domain = NULL; |
| 827 | kfree(iommu); |
| 828 | } |
| 829 | |
| 830 | static long vfio_iommu_type1_ioctl(void *iommu_data, |
| 831 | unsigned int cmd, unsigned long arg) |
| 832 | { |
| 833 | struct vfio_iommu *iommu = iommu_data; |
| 834 | unsigned long minsz; |
| 835 | |
| 836 | if (cmd == VFIO_CHECK_EXTENSION) { |
| 837 | switch (arg) { |
| 838 | case VFIO_TYPE1_IOMMU: |
| 839 | return 1; |
| 840 | default: |
| 841 | return 0; |
| 842 | } |
| 843 | } else if (cmd == VFIO_IOMMU_GET_INFO) { |
| 844 | struct vfio_iommu_type1_info info; |
| 845 | |
| 846 | minsz = offsetofend(struct vfio_iommu_type1_info, iova_pgsizes); |
| 847 | |
| 848 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
| 849 | return -EFAULT; |
| 850 | |
| 851 | if (info.argsz < minsz) |
| 852 | return -EINVAL; |
| 853 | |
| 854 | info.flags = 0; |
| 855 | |
| 856 | info.iova_pgsizes = iommu->domain->ops->pgsize_bitmap; |
| 857 | |
| 858 | return copy_to_user((void __user *)arg, &info, minsz); |
| 859 | |
| 860 | } else if (cmd == VFIO_IOMMU_MAP_DMA) { |
| 861 | struct vfio_iommu_type1_dma_map map; |
| 862 | uint32_t mask = VFIO_DMA_MAP_FLAG_READ | |
| 863 | VFIO_DMA_MAP_FLAG_WRITE; |
| 864 | |
| 865 | minsz = offsetofend(struct vfio_iommu_type1_dma_map, size); |
| 866 | |
| 867 | if (copy_from_user(&map, (void __user *)arg, minsz)) |
| 868 | return -EFAULT; |
| 869 | |
| 870 | if (map.argsz < minsz || map.flags & ~mask) |
| 871 | return -EINVAL; |
| 872 | |
| 873 | return vfio_dma_do_map(iommu, &map); |
| 874 | |
| 875 | } else if (cmd == VFIO_IOMMU_UNMAP_DMA) { |
| 876 | struct vfio_iommu_type1_dma_unmap unmap; |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 877 | long ret; |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 878 | |
| 879 | minsz = offsetofend(struct vfio_iommu_type1_dma_unmap, size); |
| 880 | |
| 881 | if (copy_from_user(&unmap, (void __user *)arg, minsz)) |
| 882 | return -EFAULT; |
| 883 | |
| 884 | if (unmap.argsz < minsz || unmap.flags) |
| 885 | return -EINVAL; |
| 886 | |
Alex Williamson | 166fd7d | 2013-06-21 09:38:02 -0600 | [diff] [blame] | 887 | ret = vfio_dma_do_unmap(iommu, &unmap); |
| 888 | if (ret) |
| 889 | return ret; |
| 890 | |
| 891 | return copy_to_user((void __user *)arg, &unmap, minsz); |
Alex Williamson | 73fa0d1 | 2012-07-31 08:16:23 -0600 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | return -ENOTTY; |
| 895 | } |
| 896 | |
| 897 | static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = { |
| 898 | .name = "vfio-iommu-type1", |
| 899 | .owner = THIS_MODULE, |
| 900 | .open = vfio_iommu_type1_open, |
| 901 | .release = vfio_iommu_type1_release, |
| 902 | .ioctl = vfio_iommu_type1_ioctl, |
| 903 | .attach_group = vfio_iommu_type1_attach_group, |
| 904 | .detach_group = vfio_iommu_type1_detach_group, |
| 905 | }; |
| 906 | |
| 907 | static int __init vfio_iommu_type1_init(void) |
| 908 | { |
| 909 | if (!iommu_present(&pci_bus_type)) |
| 910 | return -ENODEV; |
| 911 | |
| 912 | return vfio_register_iommu_driver(&vfio_iommu_driver_ops_type1); |
| 913 | } |
| 914 | |
| 915 | static void __exit vfio_iommu_type1_cleanup(void) |
| 916 | { |
| 917 | vfio_unregister_iommu_driver(&vfio_iommu_driver_ops_type1); |
| 918 | } |
| 919 | |
| 920 | module_init(vfio_iommu_type1_init); |
| 921 | module_exit(vfio_iommu_type1_cleanup); |
| 922 | |
| 923 | MODULE_VERSION(DRIVER_VERSION); |
| 924 | MODULE_LICENSE("GPL v2"); |
| 925 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 926 | MODULE_DESCRIPTION(DRIVER_DESC); |