1 /* 2 * generic functions used by VFIO devices 3 * 4 * Copyright Red Hat, Inc. 2012 5 * 6 * Authors: 7 * Alex Williamson <alex.williamson@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Based on qemu-kvm device-assignment: 13 * Adapted for KVM by Qumranet. 14 * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com) 15 * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com) 16 * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com) 17 * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com) 18 * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com) 19 */ 20 21 #include "qemu/osdep.h" 22 #include <sys/ioctl.h> 23 #include <sys/mman.h> 24 #include <linux/vfio.h> 25 26 #include "hw/vfio/vfio-common.h" 27 #include "hw/vfio/vfio.h" 28 #include "exec/address-spaces.h" 29 #include "exec/memory.h" 30 #include "hw/hw.h" 31 #include "qemu/error-report.h" 32 #include "sysemu/kvm.h" 33 #include "trace.h" 34 35 struct vfio_group_head vfio_group_list = 36 QLIST_HEAD_INITIALIZER(vfio_group_list); 37 struct vfio_as_head vfio_address_spaces = 38 QLIST_HEAD_INITIALIZER(vfio_address_spaces); 39 40 #ifdef CONFIG_KVM 41 /* 42 * We have a single VFIO pseudo device per KVM VM. Once created it lives 43 * for the life of the VM. Closing the file descriptor only drops our 44 * reference to it and the device's reference to kvm. Therefore once 45 * initialized, this file descriptor is only released on QEMU exit and 46 * we'll re-use it should another vfio device be attached before then. 47 */ 48 static int vfio_kvm_device_fd = -1; 49 #endif 50 51 /* 52 * Common VFIO interrupt disable 53 */ 54 void vfio_disable_irqindex(VFIODevice *vbasedev, int index) 55 { 56 struct vfio_irq_set irq_set = { 57 .argsz = sizeof(irq_set), 58 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER, 59 .index = index, 60 .start = 0, 61 .count = 0, 62 }; 63 64 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); 65 } 66 67 void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index) 68 { 69 struct vfio_irq_set irq_set = { 70 .argsz = sizeof(irq_set), 71 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK, 72 .index = index, 73 .start = 0, 74 .count = 1, 75 }; 76 77 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); 78 } 79 80 void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index) 81 { 82 struct vfio_irq_set irq_set = { 83 .argsz = sizeof(irq_set), 84 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK, 85 .index = index, 86 .start = 0, 87 .count = 1, 88 }; 89 90 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); 91 } 92 93 /* 94 * IO Port/MMIO - Beware of the endians, VFIO is always little endian 95 */ 96 void vfio_region_write(void *opaque, hwaddr addr, 97 uint64_t data, unsigned size) 98 { 99 VFIORegion *region = opaque; 100 VFIODevice *vbasedev = region->vbasedev; 101 union { 102 uint8_t byte; 103 uint16_t word; 104 uint32_t dword; 105 uint64_t qword; 106 } buf; 107 108 switch (size) { 109 case 1: 110 buf.byte = data; 111 break; 112 case 2: 113 buf.word = cpu_to_le16(data); 114 break; 115 case 4: 116 buf.dword = cpu_to_le32(data); 117 break; 118 default: 119 hw_error("vfio: unsupported write size, %d bytes", size); 120 break; 121 } 122 123 if (pwrite(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) { 124 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64 125 ",%d) failed: %m", 126 __func__, vbasedev->name, region->nr, 127 addr, data, size); 128 } 129 130 trace_vfio_region_write(vbasedev->name, region->nr, addr, data, size); 131 132 /* 133 * A read or write to a BAR always signals an INTx EOI. This will 134 * do nothing if not pending (including not in INTx mode). We assume 135 * that a BAR access is in response to an interrupt and that BAR 136 * accesses will service the interrupt. Unfortunately, we don't know 137 * which access will service the interrupt, so we're potentially 138 * getting quite a few host interrupts per guest interrupt. 139 */ 140 vbasedev->ops->vfio_eoi(vbasedev); 141 } 142 143 uint64_t vfio_region_read(void *opaque, 144 hwaddr addr, unsigned size) 145 { 146 VFIORegion *region = opaque; 147 VFIODevice *vbasedev = region->vbasedev; 148 union { 149 uint8_t byte; 150 uint16_t word; 151 uint32_t dword; 152 uint64_t qword; 153 } buf; 154 uint64_t data = 0; 155 156 if (pread(vbasedev->fd, &buf, size, region->fd_offset + addr) != size) { 157 error_report("%s(%s:region%d+0x%"HWADDR_PRIx", %d) failed: %m", 158 __func__, vbasedev->name, region->nr, 159 addr, size); 160 return (uint64_t)-1; 161 } 162 switch (size) { 163 case 1: 164 data = buf.byte; 165 break; 166 case 2: 167 data = le16_to_cpu(buf.word); 168 break; 169 case 4: 170 data = le32_to_cpu(buf.dword); 171 break; 172 default: 173 hw_error("vfio: unsupported read size, %d bytes", size); 174 break; 175 } 176 177 trace_vfio_region_read(vbasedev->name, region->nr, addr, size, data); 178 179 /* Same as write above */ 180 vbasedev->ops->vfio_eoi(vbasedev); 181 182 return data; 183 } 184 185 const MemoryRegionOps vfio_region_ops = { 186 .read = vfio_region_read, 187 .write = vfio_region_write, 188 .endianness = DEVICE_LITTLE_ENDIAN, 189 }; 190 191 /* 192 * DMA - Mapping and unmapping for the "type1" IOMMU interface used on x86 193 */ 194 static int vfio_dma_unmap(VFIOContainer *container, 195 hwaddr iova, ram_addr_t size) 196 { 197 struct vfio_iommu_type1_dma_unmap unmap = { 198 .argsz = sizeof(unmap), 199 .flags = 0, 200 .iova = iova, 201 .size = size, 202 }; 203 204 if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) { 205 error_report("VFIO_UNMAP_DMA: %d", -errno); 206 return -errno; 207 } 208 209 return 0; 210 } 211 212 static int vfio_dma_map(VFIOContainer *container, hwaddr iova, 213 ram_addr_t size, void *vaddr, bool readonly) 214 { 215 struct vfio_iommu_type1_dma_map map = { 216 .argsz = sizeof(map), 217 .flags = VFIO_DMA_MAP_FLAG_READ, 218 .vaddr = (__u64)(uintptr_t)vaddr, 219 .iova = iova, 220 .size = size, 221 }; 222 223 if (!readonly) { 224 map.flags |= VFIO_DMA_MAP_FLAG_WRITE; 225 } 226 227 /* 228 * Try the mapping, if it fails with EBUSY, unmap the region and try 229 * again. This shouldn't be necessary, but we sometimes see it in 230 * the VGA ROM space. 231 */ 232 if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0 || 233 (errno == EBUSY && vfio_dma_unmap(container, iova, size) == 0 && 234 ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map) == 0)) { 235 return 0; 236 } 237 238 error_report("VFIO_MAP_DMA: %d", -errno); 239 return -errno; 240 } 241 242 static bool vfio_listener_skipped_section(MemoryRegionSection *section) 243 { 244 return (!memory_region_is_ram(section->mr) && 245 !memory_region_is_iommu(section->mr)) || 246 /* 247 * Sizing an enabled 64-bit BAR can cause spurious mappings to 248 * addresses in the upper part of the 64-bit address space. These 249 * are never accessed by the CPU and beyond the address width of 250 * some IOMMU hardware. TODO: VFIO should tell us the IOMMU width. 251 */ 252 section->offset_within_address_space & (1ULL << 63); 253 } 254 255 static void vfio_iommu_map_notify(Notifier *n, void *data) 256 { 257 VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); 258 VFIOContainer *container = giommu->container; 259 IOMMUTLBEntry *iotlb = data; 260 MemoryRegion *mr; 261 hwaddr xlat; 262 hwaddr len = iotlb->addr_mask + 1; 263 void *vaddr; 264 int ret; 265 266 trace_vfio_iommu_map_notify(iotlb->iova, 267 iotlb->iova + iotlb->addr_mask); 268 269 /* 270 * The IOMMU TLB entry we have just covers translation through 271 * this IOMMU to its immediate target. We need to translate 272 * it the rest of the way through to memory. 273 */ 274 rcu_read_lock(); 275 mr = address_space_translate(&address_space_memory, 276 iotlb->translated_addr, 277 &xlat, &len, iotlb->perm & IOMMU_WO); 278 if (!memory_region_is_ram(mr)) { 279 error_report("iommu map to non memory area %"HWADDR_PRIx"", 280 xlat); 281 goto out; 282 } 283 /* 284 * Translation truncates length to the IOMMU page size, 285 * check that it did not truncate too much. 286 */ 287 if (len & iotlb->addr_mask) { 288 error_report("iommu has granularity incompatible with target AS"); 289 goto out; 290 } 291 292 if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) { 293 vaddr = memory_region_get_ram_ptr(mr) + xlat; 294 ret = vfio_dma_map(container, iotlb->iova, 295 iotlb->addr_mask + 1, vaddr, 296 !(iotlb->perm & IOMMU_WO) || mr->readonly); 297 if (ret) { 298 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", " 299 "0x%"HWADDR_PRIx", %p) = %d (%m)", 300 container, iotlb->iova, 301 iotlb->addr_mask + 1, vaddr, ret); 302 } 303 } else { 304 ret = vfio_dma_unmap(container, iotlb->iova, iotlb->addr_mask + 1); 305 if (ret) { 306 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " 307 "0x%"HWADDR_PRIx") = %d (%m)", 308 container, iotlb->iova, 309 iotlb->addr_mask + 1, ret); 310 } 311 } 312 out: 313 rcu_read_unlock(); 314 } 315 316 static hwaddr vfio_container_granularity(VFIOContainer *container) 317 { 318 return (hwaddr)1 << ctz64(container->iova_pgsizes); 319 } 320 321 static void vfio_listener_region_add(MemoryListener *listener, 322 MemoryRegionSection *section) 323 { 324 VFIOContainer *container = container_of(listener, VFIOContainer, listener); 325 hwaddr iova, end; 326 Int128 llend; 327 void *vaddr; 328 int ret; 329 330 if (vfio_listener_skipped_section(section)) { 331 trace_vfio_listener_region_add_skip( 332 section->offset_within_address_space, 333 section->offset_within_address_space + 334 int128_get64(int128_sub(section->size, int128_one()))); 335 return; 336 } 337 338 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) != 339 (section->offset_within_region & ~TARGET_PAGE_MASK))) { 340 error_report("%s received unaligned region", __func__); 341 return; 342 } 343 344 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space); 345 llend = int128_make64(section->offset_within_address_space); 346 llend = int128_add(llend, section->size); 347 llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK)); 348 349 if (int128_ge(int128_make64(iova), llend)) { 350 return; 351 } 352 end = int128_get64(llend); 353 354 if ((iova < container->min_iova) || ((end - 1) > container->max_iova)) { 355 error_report("vfio: IOMMU container %p can't map guest IOVA region" 356 " 0x%"HWADDR_PRIx"..0x%"HWADDR_PRIx, 357 container, iova, end - 1); 358 ret = -EFAULT; 359 goto fail; 360 } 361 362 memory_region_ref(section->mr); 363 364 if (memory_region_is_iommu(section->mr)) { 365 VFIOGuestIOMMU *giommu; 366 367 trace_vfio_listener_region_add_iommu(iova, end - 1); 368 /* 369 * FIXME: We should do some checking to see if the 370 * capabilities of the host VFIO IOMMU are adequate to model 371 * the guest IOMMU 372 * 373 * FIXME: For VFIO iommu types which have KVM acceleration to 374 * avoid bouncing all map/unmaps through qemu this way, this 375 * would be the right place to wire that up (tell the KVM 376 * device emulation the VFIO iommu handles to use). 377 */ 378 giommu = g_malloc0(sizeof(*giommu)); 379 giommu->iommu = section->mr; 380 giommu->container = container; 381 giommu->n.notify = vfio_iommu_map_notify; 382 QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); 383 384 memory_region_register_iommu_notifier(giommu->iommu, &giommu->n); 385 memory_region_iommu_replay(giommu->iommu, &giommu->n, 386 vfio_container_granularity(container), 387 false); 388 389 return; 390 } 391 392 /* Here we assume that memory_region_is_ram(section->mr)==true */ 393 394 vaddr = memory_region_get_ram_ptr(section->mr) + 395 section->offset_within_region + 396 (iova - section->offset_within_address_space); 397 398 trace_vfio_listener_region_add_ram(iova, end - 1, vaddr); 399 400 ret = vfio_dma_map(container, iova, end - iova, vaddr, section->readonly); 401 if (ret) { 402 error_report("vfio_dma_map(%p, 0x%"HWADDR_PRIx", " 403 "0x%"HWADDR_PRIx", %p) = %d (%m)", 404 container, iova, end - iova, vaddr, ret); 405 goto fail; 406 } 407 408 return; 409 410 fail: 411 /* 412 * On the initfn path, store the first error in the container so we 413 * can gracefully fail. Runtime, there's not much we can do other 414 * than throw a hardware error. 415 */ 416 if (!container->initialized) { 417 if (!container->error) { 418 container->error = ret; 419 } 420 } else { 421 hw_error("vfio: DMA mapping failed, unable to continue"); 422 } 423 } 424 425 static void vfio_listener_region_del(MemoryListener *listener, 426 MemoryRegionSection *section) 427 { 428 VFIOContainer *container = container_of(listener, VFIOContainer, listener); 429 hwaddr iova, end; 430 int ret; 431 432 if (vfio_listener_skipped_section(section)) { 433 trace_vfio_listener_region_del_skip( 434 section->offset_within_address_space, 435 section->offset_within_address_space + 436 int128_get64(int128_sub(section->size, int128_one()))); 437 return; 438 } 439 440 if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) != 441 (section->offset_within_region & ~TARGET_PAGE_MASK))) { 442 error_report("%s received unaligned region", __func__); 443 return; 444 } 445 446 if (memory_region_is_iommu(section->mr)) { 447 VFIOGuestIOMMU *giommu; 448 449 QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) { 450 if (giommu->iommu == section->mr) { 451 memory_region_unregister_iommu_notifier(&giommu->n); 452 QLIST_REMOVE(giommu, giommu_next); 453 g_free(giommu); 454 break; 455 } 456 } 457 458 /* 459 * FIXME: We assume the one big unmap below is adequate to 460 * remove any individual page mappings in the IOMMU which 461 * might have been copied into VFIO. This works for a page table 462 * based IOMMU where a big unmap flattens a large range of IO-PTEs. 463 * That may not be true for all IOMMU types. 464 */ 465 } 466 467 iova = TARGET_PAGE_ALIGN(section->offset_within_address_space); 468 end = (section->offset_within_address_space + int128_get64(section->size)) & 469 TARGET_PAGE_MASK; 470 471 if (iova >= end) { 472 return; 473 } 474 475 trace_vfio_listener_region_del(iova, end - 1); 476 477 ret = vfio_dma_unmap(container, iova, end - iova); 478 memory_region_unref(section->mr); 479 if (ret) { 480 error_report("vfio_dma_unmap(%p, 0x%"HWADDR_PRIx", " 481 "0x%"HWADDR_PRIx") = %d (%m)", 482 container, iova, end - iova, ret); 483 } 484 } 485 486 static const MemoryListener vfio_memory_listener = { 487 .region_add = vfio_listener_region_add, 488 .region_del = vfio_listener_region_del, 489 }; 490 491 static void vfio_listener_release(VFIOContainer *container) 492 { 493 memory_listener_unregister(&container->listener); 494 } 495 496 int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, 497 int index, const char *name) 498 { 499 struct vfio_region_info *info; 500 int ret; 501 502 ret = vfio_get_region_info(vbasedev, index, &info); 503 if (ret) { 504 return ret; 505 } 506 507 region->vbasedev = vbasedev; 508 region->flags = info->flags; 509 region->size = info->size; 510 region->fd_offset = info->offset; 511 region->nr = index; 512 513 if (region->size) { 514 region->mem = g_new0(MemoryRegion, 1); 515 memory_region_init_io(region->mem, obj, &vfio_region_ops, 516 region, name, region->size); 517 518 if (!vbasedev->no_mmap && 519 region->flags & VFIO_REGION_INFO_FLAG_MMAP && 520 !(region->size & ~qemu_real_host_page_mask)) { 521 522 region->nr_mmaps = 1; 523 region->mmaps = g_new0(VFIOMmap, region->nr_mmaps); 524 525 region->mmaps[0].offset = 0; 526 region->mmaps[0].size = region->size; 527 } 528 } 529 530 g_free(info); 531 532 trace_vfio_region_setup(vbasedev->name, index, name, 533 region->flags, region->fd_offset, region->size); 534 return 0; 535 } 536 537 int vfio_region_mmap(VFIORegion *region) 538 { 539 int i, prot = 0; 540 char *name; 541 542 if (!region->mem) { 543 return 0; 544 } 545 546 prot |= region->flags & VFIO_REGION_INFO_FLAG_READ ? PROT_READ : 0; 547 prot |= region->flags & VFIO_REGION_INFO_FLAG_WRITE ? PROT_WRITE : 0; 548 549 for (i = 0; i < region->nr_mmaps; i++) { 550 region->mmaps[i].mmap = mmap(NULL, region->mmaps[i].size, prot, 551 MAP_SHARED, region->vbasedev->fd, 552 region->fd_offset + 553 region->mmaps[i].offset); 554 if (region->mmaps[i].mmap == MAP_FAILED) { 555 int ret = -errno; 556 557 trace_vfio_region_mmap_fault(memory_region_name(region->mem), i, 558 region->fd_offset + 559 region->mmaps[i].offset, 560 region->fd_offset + 561 region->mmaps[i].offset + 562 region->mmaps[i].size - 1, ret); 563 564 region->mmaps[i].mmap = NULL; 565 566 for (i--; i >= 0; i--) { 567 memory_region_del_subregion(region->mem, ®ion->mmaps[i].mem); 568 munmap(region->mmaps[i].mmap, region->mmaps[i].size); 569 object_unparent(OBJECT(®ion->mmaps[i].mem)); 570 region->mmaps[i].mmap = NULL; 571 } 572 573 return ret; 574 } 575 576 name = g_strdup_printf("%s mmaps[%d]", 577 memory_region_name(region->mem), i); 578 memory_region_init_ram_ptr(®ion->mmaps[i].mem, 579 memory_region_owner(region->mem), 580 name, region->mmaps[i].size, 581 region->mmaps[i].mmap); 582 g_free(name); 583 memory_region_set_skip_dump(®ion->mmaps[i].mem); 584 memory_region_add_subregion(region->mem, region->mmaps[i].offset, 585 ®ion->mmaps[i].mem); 586 587 trace_vfio_region_mmap(memory_region_name(®ion->mmaps[i].mem), 588 region->mmaps[i].offset, 589 region->mmaps[i].offset + 590 region->mmaps[i].size - 1); 591 } 592 593 return 0; 594 } 595 596 void vfio_region_exit(VFIORegion *region) 597 { 598 int i; 599 600 if (!region->mem) { 601 return; 602 } 603 604 for (i = 0; i < region->nr_mmaps; i++) { 605 if (region->mmaps[i].mmap) { 606 memory_region_del_subregion(region->mem, ®ion->mmaps[i].mem); 607 } 608 } 609 610 trace_vfio_region_exit(region->vbasedev->name, region->nr); 611 } 612 613 void vfio_region_finalize(VFIORegion *region) 614 { 615 int i; 616 617 if (!region->mem) { 618 return; 619 } 620 621 for (i = 0; i < region->nr_mmaps; i++) { 622 if (region->mmaps[i].mmap) { 623 munmap(region->mmaps[i].mmap, region->mmaps[i].size); 624 object_unparent(OBJECT(®ion->mmaps[i].mem)); 625 } 626 } 627 628 object_unparent(OBJECT(region->mem)); 629 630 g_free(region->mem); 631 g_free(region->mmaps); 632 633 trace_vfio_region_finalize(region->vbasedev->name, region->nr); 634 } 635 636 void vfio_region_mmaps_set_enabled(VFIORegion *region, bool enabled) 637 { 638 int i; 639 640 if (!region->mem) { 641 return; 642 } 643 644 for (i = 0; i < region->nr_mmaps; i++) { 645 if (region->mmaps[i].mmap) { 646 memory_region_set_enabled(®ion->mmaps[i].mem, enabled); 647 } 648 } 649 650 trace_vfio_region_mmaps_set_enabled(memory_region_name(region->mem), 651 enabled); 652 } 653 654 void vfio_reset_handler(void *opaque) 655 { 656 VFIOGroup *group; 657 VFIODevice *vbasedev; 658 659 QLIST_FOREACH(group, &vfio_group_list, next) { 660 QLIST_FOREACH(vbasedev, &group->device_list, next) { 661 vbasedev->ops->vfio_compute_needs_reset(vbasedev); 662 } 663 } 664 665 QLIST_FOREACH(group, &vfio_group_list, next) { 666 QLIST_FOREACH(vbasedev, &group->device_list, next) { 667 if (vbasedev->needs_reset) { 668 vbasedev->ops->vfio_hot_reset_multi(vbasedev); 669 } 670 } 671 } 672 } 673 674 static void vfio_kvm_device_add_group(VFIOGroup *group) 675 { 676 #ifdef CONFIG_KVM 677 struct kvm_device_attr attr = { 678 .group = KVM_DEV_VFIO_GROUP, 679 .attr = KVM_DEV_VFIO_GROUP_ADD, 680 .addr = (uint64_t)(unsigned long)&group->fd, 681 }; 682 683 if (!kvm_enabled()) { 684 return; 685 } 686 687 if (vfio_kvm_device_fd < 0) { 688 struct kvm_create_device cd = { 689 .type = KVM_DEV_TYPE_VFIO, 690 }; 691 692 if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) { 693 error_report("Failed to create KVM VFIO device: %m"); 694 return; 695 } 696 697 vfio_kvm_device_fd = cd.fd; 698 } 699 700 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { 701 error_report("Failed to add group %d to KVM VFIO device: %m", 702 group->groupid); 703 } 704 #endif 705 } 706 707 static void vfio_kvm_device_del_group(VFIOGroup *group) 708 { 709 #ifdef CONFIG_KVM 710 struct kvm_device_attr attr = { 711 .group = KVM_DEV_VFIO_GROUP, 712 .attr = KVM_DEV_VFIO_GROUP_DEL, 713 .addr = (uint64_t)(unsigned long)&group->fd, 714 }; 715 716 if (vfio_kvm_device_fd < 0) { 717 return; 718 } 719 720 if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) { 721 error_report("Failed to remove group %d from KVM VFIO device: %m", 722 group->groupid); 723 } 724 #endif 725 } 726 727 static VFIOAddressSpace *vfio_get_address_space(AddressSpace *as) 728 { 729 VFIOAddressSpace *space; 730 731 QLIST_FOREACH(space, &vfio_address_spaces, list) { 732 if (space->as == as) { 733 return space; 734 } 735 } 736 737 /* No suitable VFIOAddressSpace, create a new one */ 738 space = g_malloc0(sizeof(*space)); 739 space->as = as; 740 QLIST_INIT(&space->containers); 741 742 QLIST_INSERT_HEAD(&vfio_address_spaces, space, list); 743 744 return space; 745 } 746 747 static void vfio_put_address_space(VFIOAddressSpace *space) 748 { 749 if (QLIST_EMPTY(&space->containers)) { 750 QLIST_REMOVE(space, list); 751 g_free(space); 752 } 753 } 754 755 static int vfio_connect_container(VFIOGroup *group, AddressSpace *as) 756 { 757 VFIOContainer *container; 758 int ret, fd; 759 VFIOAddressSpace *space; 760 761 space = vfio_get_address_space(as); 762 763 QLIST_FOREACH(container, &space->containers, next) { 764 if (!ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &container->fd)) { 765 group->container = container; 766 QLIST_INSERT_HEAD(&container->group_list, group, container_next); 767 return 0; 768 } 769 } 770 771 fd = qemu_open("/dev/vfio/vfio", O_RDWR); 772 if (fd < 0) { 773 error_report("vfio: failed to open /dev/vfio/vfio: %m"); 774 ret = -errno; 775 goto put_space_exit; 776 } 777 778 ret = ioctl(fd, VFIO_GET_API_VERSION); 779 if (ret != VFIO_API_VERSION) { 780 error_report("vfio: supported vfio version: %d, " 781 "reported version: %d", VFIO_API_VERSION, ret); 782 ret = -EINVAL; 783 goto close_fd_exit; 784 } 785 786 container = g_malloc0(sizeof(*container)); 787 container->space = space; 788 container->fd = fd; 789 if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) || 790 ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU)) { 791 bool v2 = !!ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1v2_IOMMU); 792 struct vfio_iommu_type1_info info; 793 794 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd); 795 if (ret) { 796 error_report("vfio: failed to set group container: %m"); 797 ret = -errno; 798 goto free_container_exit; 799 } 800 801 ret = ioctl(fd, VFIO_SET_IOMMU, 802 v2 ? VFIO_TYPE1v2_IOMMU : VFIO_TYPE1_IOMMU); 803 if (ret) { 804 error_report("vfio: failed to set iommu for container: %m"); 805 ret = -errno; 806 goto free_container_exit; 807 } 808 809 /* 810 * FIXME: This assumes that a Type1 IOMMU can map any 64-bit 811 * IOVA whatsoever. That's not actually true, but the current 812 * kernel interface doesn't tell us what it can map, and the 813 * existing Type1 IOMMUs generally support any IOVA we're 814 * going to actually try in practice. 815 */ 816 container->min_iova = 0; 817 container->max_iova = (hwaddr)-1; 818 819 /* Assume just 4K IOVA page size */ 820 container->iova_pgsizes = 0x1000; 821 info.argsz = sizeof(info); 822 ret = ioctl(fd, VFIO_IOMMU_GET_INFO, &info); 823 /* Ignore errors */ 824 if ((ret == 0) && (info.flags & VFIO_IOMMU_INFO_PGSIZES)) { 825 container->iova_pgsizes = info.iova_pgsizes; 826 } 827 } else if (ioctl(fd, VFIO_CHECK_EXTENSION, VFIO_SPAPR_TCE_IOMMU)) { 828 struct vfio_iommu_spapr_tce_info info; 829 830 ret = ioctl(group->fd, VFIO_GROUP_SET_CONTAINER, &fd); 831 if (ret) { 832 error_report("vfio: failed to set group container: %m"); 833 ret = -errno; 834 goto free_container_exit; 835 } 836 ret = ioctl(fd, VFIO_SET_IOMMU, VFIO_SPAPR_TCE_IOMMU); 837 if (ret) { 838 error_report("vfio: failed to set iommu for container: %m"); 839 ret = -errno; 840 goto free_container_exit; 841 } 842 843 /* 844 * The host kernel code implementing VFIO_IOMMU_DISABLE is called 845 * when container fd is closed so we do not call it explicitly 846 * in this file. 847 */ 848 ret = ioctl(fd, VFIO_IOMMU_ENABLE); 849 if (ret) { 850 error_report("vfio: failed to enable container: %m"); 851 ret = -errno; 852 goto free_container_exit; 853 } 854 855 /* 856 * This only considers the host IOMMU's 32-bit window. At 857 * some point we need to add support for the optional 64-bit 858 * window and dynamic windows 859 */ 860 info.argsz = sizeof(info); 861 ret = ioctl(fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info); 862 if (ret) { 863 error_report("vfio: VFIO_IOMMU_SPAPR_TCE_GET_INFO failed: %m"); 864 ret = -errno; 865 goto free_container_exit; 866 } 867 container->min_iova = info.dma32_window_start; 868 container->max_iova = container->min_iova + info.dma32_window_size - 1; 869 870 /* Assume just 4K IOVA pages for now */ 871 container->iova_pgsizes = 0x1000; 872 } else { 873 error_report("vfio: No available IOMMU models"); 874 ret = -EINVAL; 875 goto free_container_exit; 876 } 877 878 container->listener = vfio_memory_listener; 879 880 memory_listener_register(&container->listener, container->space->as); 881 882 if (container->error) { 883 ret = container->error; 884 error_report("vfio: memory listener initialization failed for container"); 885 goto listener_release_exit; 886 } 887 888 container->initialized = true; 889 890 QLIST_INIT(&container->group_list); 891 QLIST_INSERT_HEAD(&space->containers, container, next); 892 893 group->container = container; 894 QLIST_INSERT_HEAD(&container->group_list, group, container_next); 895 896 return 0; 897 listener_release_exit: 898 vfio_listener_release(container); 899 900 free_container_exit: 901 g_free(container); 902 903 close_fd_exit: 904 close(fd); 905 906 put_space_exit: 907 vfio_put_address_space(space); 908 909 return ret; 910 } 911 912 static void vfio_disconnect_container(VFIOGroup *group) 913 { 914 VFIOContainer *container = group->container; 915 916 if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) { 917 error_report("vfio: error disconnecting group %d from container", 918 group->groupid); 919 } 920 921 QLIST_REMOVE(group, container_next); 922 group->container = NULL; 923 924 if (QLIST_EMPTY(&container->group_list)) { 925 VFIOAddressSpace *space = container->space; 926 VFIOGuestIOMMU *giommu, *tmp; 927 928 vfio_listener_release(container); 929 QLIST_REMOVE(container, next); 930 931 QLIST_FOREACH_SAFE(giommu, &container->giommu_list, giommu_next, tmp) { 932 memory_region_unregister_iommu_notifier(&giommu->n); 933 QLIST_REMOVE(giommu, giommu_next); 934 g_free(giommu); 935 } 936 937 trace_vfio_disconnect_container(container->fd); 938 close(container->fd); 939 g_free(container); 940 941 vfio_put_address_space(space); 942 } 943 } 944 945 VFIOGroup *vfio_get_group(int groupid, AddressSpace *as) 946 { 947 VFIOGroup *group; 948 char path[32]; 949 struct vfio_group_status status = { .argsz = sizeof(status) }; 950 951 QLIST_FOREACH(group, &vfio_group_list, next) { 952 if (group->groupid == groupid) { 953 /* Found it. Now is it already in the right context? */ 954 if (group->container->space->as == as) { 955 return group; 956 } else { 957 error_report("vfio: group %d used in multiple address spaces", 958 group->groupid); 959 return NULL; 960 } 961 } 962 } 963 964 group = g_malloc0(sizeof(*group)); 965 966 snprintf(path, sizeof(path), "/dev/vfio/%d", groupid); 967 group->fd = qemu_open(path, O_RDWR); 968 if (group->fd < 0) { 969 error_report("vfio: error opening %s: %m", path); 970 goto free_group_exit; 971 } 972 973 if (ioctl(group->fd, VFIO_GROUP_GET_STATUS, &status)) { 974 error_report("vfio: error getting group status: %m"); 975 goto close_fd_exit; 976 } 977 978 if (!(status.flags & VFIO_GROUP_FLAGS_VIABLE)) { 979 error_report("vfio: error, group %d is not viable, please ensure " 980 "all devices within the iommu_group are bound to their " 981 "vfio bus driver.", groupid); 982 goto close_fd_exit; 983 } 984 985 group->groupid = groupid; 986 QLIST_INIT(&group->device_list); 987 988 if (vfio_connect_container(group, as)) { 989 error_report("vfio: failed to setup container for group %d", groupid); 990 goto close_fd_exit; 991 } 992 993 if (QLIST_EMPTY(&vfio_group_list)) { 994 qemu_register_reset(vfio_reset_handler, NULL); 995 } 996 997 QLIST_INSERT_HEAD(&vfio_group_list, group, next); 998 999 vfio_kvm_device_add_group(group); 1000 1001 return group; 1002 1003 close_fd_exit: 1004 close(group->fd); 1005 1006 free_group_exit: 1007 g_free(group); 1008 1009 return NULL; 1010 } 1011 1012 void vfio_put_group(VFIOGroup *group) 1013 { 1014 if (!group || !QLIST_EMPTY(&group->device_list)) { 1015 return; 1016 } 1017 1018 vfio_kvm_device_del_group(group); 1019 vfio_disconnect_container(group); 1020 QLIST_REMOVE(group, next); 1021 trace_vfio_put_group(group->fd); 1022 close(group->fd); 1023 g_free(group); 1024 1025 if (QLIST_EMPTY(&vfio_group_list)) { 1026 qemu_unregister_reset(vfio_reset_handler, NULL); 1027 } 1028 } 1029 1030 int vfio_get_device(VFIOGroup *group, const char *name, 1031 VFIODevice *vbasedev) 1032 { 1033 struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) }; 1034 int ret, fd; 1035 1036 fd = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name); 1037 if (fd < 0) { 1038 error_report("vfio: error getting device %s from group %d: %m", 1039 name, group->groupid); 1040 error_printf("Verify all devices in group %d are bound to vfio-<bus> " 1041 "or pci-stub and not already in use\n", group->groupid); 1042 return fd; 1043 } 1044 1045 ret = ioctl(fd, VFIO_DEVICE_GET_INFO, &dev_info); 1046 if (ret) { 1047 error_report("vfio: error getting device info: %m"); 1048 close(fd); 1049 return ret; 1050 } 1051 1052 vbasedev->fd = fd; 1053 vbasedev->group = group; 1054 QLIST_INSERT_HEAD(&group->device_list, vbasedev, next); 1055 1056 vbasedev->num_irqs = dev_info.num_irqs; 1057 vbasedev->num_regions = dev_info.num_regions; 1058 vbasedev->flags = dev_info.flags; 1059 1060 trace_vfio_get_device(name, dev_info.flags, dev_info.num_regions, 1061 dev_info.num_irqs); 1062 1063 vbasedev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET); 1064 return 0; 1065 } 1066 1067 void vfio_put_base_device(VFIODevice *vbasedev) 1068 { 1069 if (!vbasedev->group) { 1070 return; 1071 } 1072 QLIST_REMOVE(vbasedev, next); 1073 vbasedev->group = NULL; 1074 trace_vfio_put_base_device(vbasedev->fd); 1075 close(vbasedev->fd); 1076 } 1077 1078 int vfio_get_region_info(VFIODevice *vbasedev, int index, 1079 struct vfio_region_info **info) 1080 { 1081 size_t argsz = sizeof(struct vfio_region_info); 1082 1083 *info = g_malloc0(argsz); 1084 1085 (*info)->index = index; 1086 (*info)->argsz = argsz; 1087 1088 if (ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, *info)) { 1089 g_free(*info); 1090 return -errno; 1091 } 1092 1093 return 0; 1094 } 1095 1096 /* 1097 * Interfaces for IBM EEH (Enhanced Error Handling) 1098 */ 1099 static bool vfio_eeh_container_ok(VFIOContainer *container) 1100 { 1101 /* 1102 * As of 2016-03-04 (linux-4.5) the host kernel EEH/VFIO 1103 * implementation is broken if there are multiple groups in a 1104 * container. The hardware works in units of Partitionable 1105 * Endpoints (== IOMMU groups) and the EEH operations naively 1106 * iterate across all groups in the container, without any logic 1107 * to make sure the groups have their state synchronized. For 1108 * certain operations (ENABLE) that might be ok, until an error 1109 * occurs, but for others (GET_STATE) it's clearly broken. 1110 */ 1111 1112 /* 1113 * XXX Once fixed kernels exist, test for them here 1114 */ 1115 1116 if (QLIST_EMPTY(&container->group_list)) { 1117 return false; 1118 } 1119 1120 if (QLIST_NEXT(QLIST_FIRST(&container->group_list), container_next)) { 1121 return false; 1122 } 1123 1124 return true; 1125 } 1126 1127 static int vfio_eeh_container_op(VFIOContainer *container, uint32_t op) 1128 { 1129 struct vfio_eeh_pe_op pe_op = { 1130 .argsz = sizeof(pe_op), 1131 .op = op, 1132 }; 1133 int ret; 1134 1135 if (!vfio_eeh_container_ok(container)) { 1136 error_report("vfio/eeh: EEH_PE_OP 0x%x: " 1137 "kernel requires a container with exactly one group", op); 1138 return -EPERM; 1139 } 1140 1141 ret = ioctl(container->fd, VFIO_EEH_PE_OP, &pe_op); 1142 if (ret < 0) { 1143 error_report("vfio/eeh: EEH_PE_OP 0x%x failed: %m", op); 1144 return -errno; 1145 } 1146 1147 return 0; 1148 } 1149 1150 static VFIOContainer *vfio_eeh_as_container(AddressSpace *as) 1151 { 1152 VFIOAddressSpace *space = vfio_get_address_space(as); 1153 VFIOContainer *container = NULL; 1154 1155 if (QLIST_EMPTY(&space->containers)) { 1156 /* No containers to act on */ 1157 goto out; 1158 } 1159 1160 container = QLIST_FIRST(&space->containers); 1161 1162 if (QLIST_NEXT(container, next)) { 1163 /* We don't yet have logic to synchronize EEH state across 1164 * multiple containers */ 1165 container = NULL; 1166 goto out; 1167 } 1168 1169 out: 1170 vfio_put_address_space(space); 1171 return container; 1172 } 1173 1174 bool vfio_eeh_as_ok(AddressSpace *as) 1175 { 1176 VFIOContainer *container = vfio_eeh_as_container(as); 1177 1178 return (container != NULL) && vfio_eeh_container_ok(container); 1179 } 1180 1181 int vfio_eeh_as_op(AddressSpace *as, uint32_t op) 1182 { 1183 VFIOContainer *container = vfio_eeh_as_container(as); 1184 1185 if (!container) { 1186 return -ENODEV; 1187 } 1188 return vfio_eeh_container_op(container, op); 1189 } 1190