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