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