1 /* 2 * vhost-vdpa 3 * 4 * Copyright(c) 2017-2018 Intel Corporation. 5 * Copyright(c) 2020 Red Hat, Inc. 6 * 7 * This work is licensed under the terms of the GNU GPL, version 2 or later. 8 * See the COPYING file in the top-level directory. 9 * 10 */ 11 12 #include "qemu/osdep.h" 13 #include <linux/vhost.h> 14 #include <linux/vfio.h> 15 #include <sys/eventfd.h> 16 #include <sys/ioctl.h> 17 #include "exec/target_page.h" 18 #include "hw/virtio/vhost.h" 19 #include "hw/virtio/vhost-backend.h" 20 #include "hw/virtio/virtio-net.h" 21 #include "hw/virtio/vhost-shadow-virtqueue.h" 22 #include "hw/virtio/vhost-vdpa.h" 23 #include "exec/address-spaces.h" 24 #include "migration/blocker.h" 25 #include "qemu/cutils.h" 26 #include "qemu/main-loop.h" 27 #include "trace.h" 28 #include "qapi/error.h" 29 30 /* 31 * Return one past the end of the end of section. Be careful with uint64_t 32 * conversions! 33 */ 34 static Int128 vhost_vdpa_section_end(const MemoryRegionSection *section, 35 int page_mask) 36 { 37 Int128 llend = int128_make64(section->offset_within_address_space); 38 llend = int128_add(llend, section->size); 39 llend = int128_and(llend, int128_exts64(page_mask)); 40 41 return llend; 42 } 43 44 static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection *section, 45 uint64_t iova_min, 46 uint64_t iova_max, 47 int page_mask) 48 { 49 Int128 llend; 50 51 if ((!memory_region_is_ram(section->mr) && 52 !memory_region_is_iommu(section->mr)) || 53 memory_region_is_protected(section->mr) || 54 /* vhost-vDPA doesn't allow MMIO to be mapped */ 55 memory_region_is_ram_device(section->mr)) { 56 return true; 57 } 58 59 if (section->offset_within_address_space < iova_min) { 60 error_report("RAM section out of device range (min=0x%" PRIx64 61 ", addr=0x%" HWADDR_PRIx ")", 62 iova_min, section->offset_within_address_space); 63 return true; 64 } 65 /* 66 * While using vIOMMU, sometimes the section will be larger than iova_max, 67 * but the memory that actually maps is smaller, so move the check to 68 * function vhost_vdpa_iommu_map_notify(). That function will use the actual 69 * size that maps to the kernel 70 */ 71 72 if (!memory_region_is_iommu(section->mr)) { 73 llend = vhost_vdpa_section_end(section, page_mask); 74 if (int128_gt(llend, int128_make64(iova_max))) { 75 error_report("RAM section out of device range (max=0x%" PRIx64 76 ", end addr=0x%" PRIx64 ")", 77 iova_max, int128_get64(llend)); 78 return true; 79 } 80 } 81 82 return false; 83 } 84 85 /* 86 * The caller must set asid = 0 if the device does not support asid. 87 * This is not an ABI break since it is set to 0 by the initializer anyway. 88 */ 89 int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, 90 hwaddr size, void *vaddr, bool readonly) 91 { 92 struct vhost_msg_v2 msg = {}; 93 int fd = v->device_fd; 94 int ret = 0; 95 96 msg.type = v->msg_type; 97 msg.asid = asid; 98 msg.iotlb.iova = iova; 99 msg.iotlb.size = size; 100 msg.iotlb.uaddr = (uint64_t)(uintptr_t)vaddr; 101 msg.iotlb.perm = readonly ? VHOST_ACCESS_RO : VHOST_ACCESS_RW; 102 msg.iotlb.type = VHOST_IOTLB_UPDATE; 103 104 trace_vhost_vdpa_dma_map(v, fd, msg.type, msg.asid, msg.iotlb.iova, 105 msg.iotlb.size, msg.iotlb.uaddr, msg.iotlb.perm, 106 msg.iotlb.type); 107 108 if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 109 error_report("failed to write, fd=%d, errno=%d (%s)", 110 fd, errno, strerror(errno)); 111 return -EIO ; 112 } 113 114 return ret; 115 } 116 117 /* 118 * The caller must set asid = 0 if the device does not support asid. 119 * This is not an ABI break since it is set to 0 by the initializer anyway. 120 */ 121 int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova, 122 hwaddr size) 123 { 124 struct vhost_msg_v2 msg = {}; 125 int fd = v->device_fd; 126 int ret = 0; 127 128 msg.type = v->msg_type; 129 msg.asid = asid; 130 msg.iotlb.iova = iova; 131 msg.iotlb.size = size; 132 msg.iotlb.type = VHOST_IOTLB_INVALIDATE; 133 134 trace_vhost_vdpa_dma_unmap(v, fd, msg.type, msg.asid, msg.iotlb.iova, 135 msg.iotlb.size, msg.iotlb.type); 136 137 if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 138 error_report("failed to write, fd=%d, errno=%d (%s)", 139 fd, errno, strerror(errno)); 140 return -EIO ; 141 } 142 143 return ret; 144 } 145 146 static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa *v) 147 { 148 int fd = v->device_fd; 149 struct vhost_msg_v2 msg = { 150 .type = v->msg_type, 151 .iotlb.type = VHOST_IOTLB_BATCH_BEGIN, 152 }; 153 154 trace_vhost_vdpa_listener_begin_batch(v, fd, msg.type, msg.iotlb.type); 155 if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 156 error_report("failed to write, fd=%d, errno=%d (%s)", 157 fd, errno, strerror(errno)); 158 } 159 } 160 161 static void vhost_vdpa_iotlb_batch_begin_once(struct vhost_vdpa *v) 162 { 163 if (v->dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH) && 164 !v->iotlb_batch_begin_sent) { 165 vhost_vdpa_listener_begin_batch(v); 166 } 167 168 v->iotlb_batch_begin_sent = true; 169 } 170 171 static void vhost_vdpa_listener_commit(MemoryListener *listener) 172 { 173 struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 174 struct vhost_dev *dev = v->dev; 175 struct vhost_msg_v2 msg = {}; 176 int fd = v->device_fd; 177 178 if (!(dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH))) { 179 return; 180 } 181 182 if (!v->iotlb_batch_begin_sent) { 183 return; 184 } 185 186 msg.type = v->msg_type; 187 msg.iotlb.type = VHOST_IOTLB_BATCH_END; 188 189 trace_vhost_vdpa_listener_commit(v, fd, msg.type, msg.iotlb.type); 190 if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) { 191 error_report("failed to write, fd=%d, errno=%d (%s)", 192 fd, errno, strerror(errno)); 193 } 194 195 v->iotlb_batch_begin_sent = false; 196 } 197 198 static void vhost_vdpa_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) 199 { 200 struct vdpa_iommu *iommu = container_of(n, struct vdpa_iommu, n); 201 202 hwaddr iova = iotlb->iova + iommu->iommu_offset; 203 struct vhost_vdpa *v = iommu->dev; 204 void *vaddr; 205 int ret; 206 Int128 llend; 207 208 if (iotlb->target_as != &address_space_memory) { 209 error_report("Wrong target AS \"%s\", only system memory is allowed", 210 iotlb->target_as->name ? iotlb->target_as->name : "none"); 211 return; 212 } 213 RCU_READ_LOCK_GUARD(); 214 /* check if RAM section out of device range */ 215 llend = int128_add(int128_makes64(iotlb->addr_mask), int128_makes64(iova)); 216 if (int128_gt(llend, int128_make64(v->iova_range.last))) { 217 error_report("RAM section out of device range (max=0x%" PRIx64 218 ", end addr=0x%" PRIx64 ")", 219 v->iova_range.last, int128_get64(llend)); 220 return; 221 } 222 223 if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) { 224 bool read_only; 225 226 if (!memory_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, NULL)) { 227 return; 228 } 229 ret = vhost_vdpa_dma_map(v, VHOST_VDPA_GUEST_PA_ASID, iova, 230 iotlb->addr_mask + 1, vaddr, read_only); 231 if (ret) { 232 error_report("vhost_vdpa_dma_map(%p, 0x%" HWADDR_PRIx ", " 233 "0x%" HWADDR_PRIx ", %p) = %d (%m)", 234 v, iova, iotlb->addr_mask + 1, vaddr, ret); 235 } 236 } else { 237 ret = vhost_vdpa_dma_unmap(v, VHOST_VDPA_GUEST_PA_ASID, iova, 238 iotlb->addr_mask + 1); 239 if (ret) { 240 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx ", " 241 "0x%" HWADDR_PRIx ") = %d (%m)", 242 v, iova, iotlb->addr_mask + 1, ret); 243 } 244 } 245 } 246 247 static void vhost_vdpa_iommu_region_add(MemoryListener *listener, 248 MemoryRegionSection *section) 249 { 250 struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 251 252 struct vdpa_iommu *iommu; 253 Int128 end; 254 int iommu_idx; 255 IOMMUMemoryRegion *iommu_mr; 256 int ret; 257 258 iommu_mr = IOMMU_MEMORY_REGION(section->mr); 259 260 iommu = g_malloc0(sizeof(*iommu)); 261 end = int128_add(int128_make64(section->offset_within_region), 262 section->size); 263 end = int128_sub(end, int128_one()); 264 iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, 265 MEMTXATTRS_UNSPECIFIED); 266 iommu->iommu_mr = iommu_mr; 267 iommu_notifier_init(&iommu->n, vhost_vdpa_iommu_map_notify, 268 IOMMU_NOTIFIER_IOTLB_EVENTS, 269 section->offset_within_region, 270 int128_get64(end), 271 iommu_idx); 272 iommu->iommu_offset = section->offset_within_address_space - 273 section->offset_within_region; 274 iommu->dev = v; 275 276 ret = memory_region_register_iommu_notifier(section->mr, &iommu->n, NULL); 277 if (ret) { 278 g_free(iommu); 279 return; 280 } 281 282 QLIST_INSERT_HEAD(&v->iommu_list, iommu, iommu_next); 283 memory_region_iommu_replay(iommu->iommu_mr, &iommu->n); 284 285 return; 286 } 287 288 static void vhost_vdpa_iommu_region_del(MemoryListener *listener, 289 MemoryRegionSection *section) 290 { 291 struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 292 293 struct vdpa_iommu *iommu; 294 295 QLIST_FOREACH(iommu, &v->iommu_list, iommu_next) 296 { 297 if (MEMORY_REGION(iommu->iommu_mr) == section->mr && 298 iommu->n.start == section->offset_within_region) { 299 memory_region_unregister_iommu_notifier(section->mr, &iommu->n); 300 QLIST_REMOVE(iommu, iommu_next); 301 g_free(iommu); 302 break; 303 } 304 } 305 } 306 307 static void vhost_vdpa_listener_region_add(MemoryListener *listener, 308 MemoryRegionSection *section) 309 { 310 DMAMap mem_region = {}; 311 struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 312 hwaddr iova; 313 Int128 llend, llsize; 314 void *vaddr; 315 int ret; 316 int page_size = qemu_target_page_size(); 317 int page_mask = -page_size; 318 319 if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first, 320 v->iova_range.last, page_mask)) { 321 return; 322 } 323 if (memory_region_is_iommu(section->mr)) { 324 vhost_vdpa_iommu_region_add(listener, section); 325 return; 326 } 327 328 if (unlikely((section->offset_within_address_space & ~page_mask) != 329 (section->offset_within_region & ~page_mask))) { 330 trace_vhost_vdpa_listener_region_add_unaligned(v, section->mr->name, 331 section->offset_within_address_space & ~page_mask, 332 section->offset_within_region & ~page_mask); 333 return; 334 } 335 336 iova = ROUND_UP(section->offset_within_address_space, page_size); 337 llend = vhost_vdpa_section_end(section, page_mask); 338 if (int128_ge(int128_make64(iova), llend)) { 339 return; 340 } 341 342 memory_region_ref(section->mr); 343 344 /* Here we assume that memory_region_is_ram(section->mr)==true */ 345 346 vaddr = memory_region_get_ram_ptr(section->mr) + 347 section->offset_within_region + 348 (iova - section->offset_within_address_space); 349 350 trace_vhost_vdpa_listener_region_add(v, iova, int128_get64(llend), 351 vaddr, section->readonly); 352 353 llsize = int128_sub(llend, int128_make64(iova)); 354 if (v->shadow_data) { 355 int r; 356 357 mem_region.translated_addr = (hwaddr)(uintptr_t)vaddr, 358 mem_region.size = int128_get64(llsize) - 1, 359 mem_region.perm = IOMMU_ACCESS_FLAG(true, section->readonly), 360 361 r = vhost_iova_tree_map_alloc(v->iova_tree, &mem_region); 362 if (unlikely(r != IOVA_OK)) { 363 error_report("Can't allocate a mapping (%d)", r); 364 goto fail; 365 } 366 367 iova = mem_region.iova; 368 } 369 370 vhost_vdpa_iotlb_batch_begin_once(v); 371 ret = vhost_vdpa_dma_map(v, VHOST_VDPA_GUEST_PA_ASID, iova, 372 int128_get64(llsize), vaddr, section->readonly); 373 if (ret) { 374 error_report("vhost vdpa map fail!"); 375 goto fail_map; 376 } 377 378 return; 379 380 fail_map: 381 if (v->shadow_data) { 382 vhost_iova_tree_remove(v->iova_tree, mem_region); 383 } 384 385 fail: 386 /* 387 * On the initfn path, store the first error in the container so we 388 * can gracefully fail. Runtime, there's not much we can do other 389 * than throw a hardware error. 390 */ 391 error_report("vhost-vdpa: DMA mapping failed, unable to continue"); 392 return; 393 394 } 395 396 static void vhost_vdpa_listener_region_del(MemoryListener *listener, 397 MemoryRegionSection *section) 398 { 399 struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener); 400 hwaddr iova; 401 Int128 llend, llsize; 402 int ret; 403 int page_size = qemu_target_page_size(); 404 int page_mask = -page_size; 405 406 if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first, 407 v->iova_range.last, page_mask)) { 408 return; 409 } 410 if (memory_region_is_iommu(section->mr)) { 411 vhost_vdpa_iommu_region_del(listener, section); 412 } 413 414 if (unlikely((section->offset_within_address_space & ~page_mask) != 415 (section->offset_within_region & ~page_mask))) { 416 trace_vhost_vdpa_listener_region_del_unaligned(v, section->mr->name, 417 section->offset_within_address_space & ~page_mask, 418 section->offset_within_region & ~page_mask); 419 return; 420 } 421 422 iova = ROUND_UP(section->offset_within_address_space, page_size); 423 llend = vhost_vdpa_section_end(section, page_mask); 424 425 trace_vhost_vdpa_listener_region_del(v, iova, 426 int128_get64(int128_sub(llend, int128_one()))); 427 428 if (int128_ge(int128_make64(iova), llend)) { 429 return; 430 } 431 432 llsize = int128_sub(llend, int128_make64(iova)); 433 434 if (v->shadow_data) { 435 const DMAMap *result; 436 const void *vaddr = memory_region_get_ram_ptr(section->mr) + 437 section->offset_within_region + 438 (iova - section->offset_within_address_space); 439 DMAMap mem_region = { 440 .translated_addr = (hwaddr)(uintptr_t)vaddr, 441 .size = int128_get64(llsize) - 1, 442 }; 443 444 result = vhost_iova_tree_find_iova(v->iova_tree, &mem_region); 445 if (!result) { 446 /* The memory listener map wasn't mapped */ 447 return; 448 } 449 iova = result->iova; 450 vhost_iova_tree_remove(v->iova_tree, *result); 451 } 452 vhost_vdpa_iotlb_batch_begin_once(v); 453 /* 454 * The unmap ioctl doesn't accept a full 64-bit. need to check it 455 */ 456 if (int128_eq(llsize, int128_2_64())) { 457 llsize = int128_rshift(llsize, 1); 458 ret = vhost_vdpa_dma_unmap(v, VHOST_VDPA_GUEST_PA_ASID, iova, 459 int128_get64(llsize)); 460 461 if (ret) { 462 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx ", " 463 "0x%" HWADDR_PRIx ") = %d (%m)", 464 v, iova, int128_get64(llsize), ret); 465 } 466 iova += int128_get64(llsize); 467 } 468 ret = vhost_vdpa_dma_unmap(v, VHOST_VDPA_GUEST_PA_ASID, iova, 469 int128_get64(llsize)); 470 471 if (ret) { 472 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx ", " 473 "0x%" HWADDR_PRIx ") = %d (%m)", 474 v, iova, int128_get64(llsize), ret); 475 } 476 477 memory_region_unref(section->mr); 478 } 479 /* 480 * IOTLB API is used by vhost-vdpa which requires incremental updating 481 * of the mapping. So we can not use generic vhost memory listener which 482 * depends on the addnop(). 483 */ 484 static const MemoryListener vhost_vdpa_memory_listener = { 485 .name = "vhost-vdpa", 486 .commit = vhost_vdpa_listener_commit, 487 .region_add = vhost_vdpa_listener_region_add, 488 .region_del = vhost_vdpa_listener_region_del, 489 }; 490 491 static int vhost_vdpa_call(struct vhost_dev *dev, unsigned long int request, 492 void *arg) 493 { 494 struct vhost_vdpa *v = dev->opaque; 495 int fd = v->device_fd; 496 int ret; 497 498 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 499 500 ret = ioctl(fd, request, arg); 501 return ret < 0 ? -errno : ret; 502 } 503 504 static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status) 505 { 506 uint8_t s; 507 int ret; 508 509 trace_vhost_vdpa_add_status(dev, status); 510 ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s); 511 if (ret < 0) { 512 return ret; 513 } 514 515 s |= status; 516 517 ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &s); 518 if (ret < 0) { 519 return ret; 520 } 521 522 ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s); 523 if (ret < 0) { 524 return ret; 525 } 526 527 if (!(s & status)) { 528 return -EIO; 529 } 530 531 return 0; 532 } 533 534 int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_range) 535 { 536 int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range); 537 538 return ret < 0 ? -errno : 0; 539 } 540 541 /* 542 * The use of this function is for requests that only need to be 543 * applied once. Typically such request occurs at the beginning 544 * of operation, and before setting up queues. It should not be 545 * used for request that performs operation until all queues are 546 * set, which would need to check dev->vq_index_end instead. 547 */ 548 static bool vhost_vdpa_first_dev(struct vhost_dev *dev) 549 { 550 struct vhost_vdpa *v = dev->opaque; 551 552 return v->index == 0; 553 } 554 555 static int vhost_vdpa_get_dev_features(struct vhost_dev *dev, 556 uint64_t *features) 557 { 558 int ret; 559 560 ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features); 561 trace_vhost_vdpa_get_features(dev, *features); 562 return ret; 563 } 564 565 static void vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v) 566 { 567 g_autoptr(GPtrArray) shadow_vqs = NULL; 568 569 shadow_vqs = g_ptr_array_new_full(hdev->nvqs, vhost_svq_free); 570 for (unsigned n = 0; n < hdev->nvqs; ++n) { 571 VhostShadowVirtqueue *svq; 572 573 svq = vhost_svq_new(v->shadow_vq_ops, v->shadow_vq_ops_opaque); 574 g_ptr_array_add(shadow_vqs, svq); 575 } 576 577 v->shadow_vqs = g_steal_pointer(&shadow_vqs); 578 } 579 580 static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp) 581 { 582 struct vhost_vdpa *v; 583 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 584 trace_vhost_vdpa_init(dev, opaque); 585 int ret; 586 587 v = opaque; 588 v->dev = dev; 589 dev->opaque = opaque ; 590 v->listener = vhost_vdpa_memory_listener; 591 v->msg_type = VHOST_IOTLB_MSG_V2; 592 vhost_vdpa_init_svq(dev, v); 593 594 error_propagate(&dev->migration_blocker, v->migration_blocker); 595 if (!vhost_vdpa_first_dev(dev)) { 596 return 0; 597 } 598 599 /* 600 * If dev->shadow_vqs_enabled at initialization that means the device has 601 * been started with x-svq=on, so don't block migration 602 */ 603 if (dev->migration_blocker == NULL && !v->shadow_vqs_enabled) { 604 /* We don't have dev->features yet */ 605 uint64_t features; 606 ret = vhost_vdpa_get_dev_features(dev, &features); 607 if (unlikely(ret)) { 608 error_setg_errno(errp, -ret, "Could not get device features"); 609 return ret; 610 } 611 vhost_svq_valid_features(features, &dev->migration_blocker); 612 } 613 614 /* 615 * Similar to VFIO, we end up pinning all guest memory and have to 616 * disable discarding of RAM. 617 */ 618 ret = ram_block_discard_disable(true); 619 if (ret) { 620 error_report("Cannot set discarding of RAM broken"); 621 return ret; 622 } 623 624 vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | 625 VIRTIO_CONFIG_S_DRIVER); 626 627 return 0; 628 } 629 630 static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev, 631 int queue_index) 632 { 633 size_t page_size = qemu_real_host_page_size(); 634 struct vhost_vdpa *v = dev->opaque; 635 VirtIODevice *vdev = dev->vdev; 636 VhostVDPAHostNotifier *n; 637 638 n = &v->notifier[queue_index]; 639 640 if (n->addr) { 641 virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, false); 642 object_unparent(OBJECT(&n->mr)); 643 munmap(n->addr, page_size); 644 n->addr = NULL; 645 } 646 } 647 648 static int vhost_vdpa_host_notifier_init(struct vhost_dev *dev, int queue_index) 649 { 650 size_t page_size = qemu_real_host_page_size(); 651 struct vhost_vdpa *v = dev->opaque; 652 VirtIODevice *vdev = dev->vdev; 653 VhostVDPAHostNotifier *n; 654 int fd = v->device_fd; 655 void *addr; 656 char *name; 657 658 vhost_vdpa_host_notifier_uninit(dev, queue_index); 659 660 n = &v->notifier[queue_index]; 661 662 addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd, 663 queue_index * page_size); 664 if (addr == MAP_FAILED) { 665 goto err; 666 } 667 668 name = g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]", 669 v, queue_index); 670 memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name, 671 page_size, addr); 672 g_free(name); 673 674 if (virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, true)) { 675 object_unparent(OBJECT(&n->mr)); 676 munmap(addr, page_size); 677 goto err; 678 } 679 n->addr = addr; 680 681 return 0; 682 683 err: 684 return -1; 685 } 686 687 static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n) 688 { 689 int i; 690 691 /* 692 * Pack all the changes to the memory regions in a single 693 * transaction to avoid a few updating of the address space 694 * topology. 695 */ 696 memory_region_transaction_begin(); 697 698 for (i = dev->vq_index; i < dev->vq_index + n; i++) { 699 vhost_vdpa_host_notifier_uninit(dev, i); 700 } 701 702 memory_region_transaction_commit(); 703 } 704 705 static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev) 706 { 707 struct vhost_vdpa *v = dev->opaque; 708 int i; 709 710 if (v->shadow_vqs_enabled) { 711 /* FIXME SVQ is not compatible with host notifiers mr */ 712 return; 713 } 714 715 /* 716 * Pack all the changes to the memory regions in a single 717 * transaction to avoid a few updating of the address space 718 * topology. 719 */ 720 memory_region_transaction_begin(); 721 722 for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) { 723 if (vhost_vdpa_host_notifier_init(dev, i)) { 724 vhost_vdpa_host_notifiers_uninit(dev, i - dev->vq_index); 725 break; 726 } 727 } 728 729 memory_region_transaction_commit(); 730 } 731 732 static void vhost_vdpa_svq_cleanup(struct vhost_dev *dev) 733 { 734 struct vhost_vdpa *v = dev->opaque; 735 size_t idx; 736 737 for (idx = 0; idx < v->shadow_vqs->len; ++idx) { 738 vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, idx)); 739 } 740 g_ptr_array_free(v->shadow_vqs, true); 741 } 742 743 static int vhost_vdpa_cleanup(struct vhost_dev *dev) 744 { 745 struct vhost_vdpa *v; 746 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 747 v = dev->opaque; 748 trace_vhost_vdpa_cleanup(dev, v); 749 if (vhost_vdpa_first_dev(dev)) { 750 ram_block_discard_disable(false); 751 } 752 753 vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); 754 memory_listener_unregister(&v->listener); 755 vhost_vdpa_svq_cleanup(dev); 756 757 dev->opaque = NULL; 758 759 return 0; 760 } 761 762 static int vhost_vdpa_memslots_limit(struct vhost_dev *dev) 763 { 764 trace_vhost_vdpa_memslots_limit(dev, INT_MAX); 765 return INT_MAX; 766 } 767 768 static int vhost_vdpa_set_mem_table(struct vhost_dev *dev, 769 struct vhost_memory *mem) 770 { 771 if (!vhost_vdpa_first_dev(dev)) { 772 return 0; 773 } 774 775 trace_vhost_vdpa_set_mem_table(dev, mem->nregions, mem->padding); 776 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE) && 777 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS)) { 778 int i; 779 for (i = 0; i < mem->nregions; i++) { 780 trace_vhost_vdpa_dump_regions(dev, i, 781 mem->regions[i].guest_phys_addr, 782 mem->regions[i].memory_size, 783 mem->regions[i].userspace_addr, 784 mem->regions[i].flags_padding); 785 } 786 } 787 if (mem->padding) { 788 return -EINVAL; 789 } 790 791 return 0; 792 } 793 794 static int vhost_vdpa_set_features(struct vhost_dev *dev, 795 uint64_t features) 796 { 797 struct vhost_vdpa *v = dev->opaque; 798 int ret; 799 800 if (!vhost_vdpa_first_dev(dev)) { 801 return 0; 802 } 803 804 if (v->shadow_vqs_enabled) { 805 if ((v->acked_features ^ features) == BIT_ULL(VHOST_F_LOG_ALL)) { 806 /* 807 * QEMU is just trying to enable or disable logging. SVQ handles 808 * this sepparately, so no need to forward this. 809 */ 810 v->acked_features = features; 811 return 0; 812 } 813 814 v->acked_features = features; 815 816 /* We must not ack _F_LOG if SVQ is enabled */ 817 features &= ~BIT_ULL(VHOST_F_LOG_ALL); 818 } 819 820 trace_vhost_vdpa_set_features(dev, features); 821 ret = vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features); 822 if (ret) { 823 return ret; 824 } 825 826 return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK); 827 } 828 829 static int vhost_vdpa_set_backend_cap(struct vhost_dev *dev) 830 { 831 uint64_t features; 832 uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | 833 0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH | 834 0x1ULL << VHOST_BACKEND_F_IOTLB_ASID | 835 0x1ULL << VHOST_BACKEND_F_SUSPEND; 836 int r; 837 838 if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) { 839 return -EFAULT; 840 } 841 842 features &= f; 843 844 if (vhost_vdpa_first_dev(dev)) { 845 r = vhost_vdpa_call(dev, VHOST_SET_BACKEND_FEATURES, &features); 846 if (r) { 847 return -EFAULT; 848 } 849 } 850 851 dev->backend_cap = features; 852 853 return 0; 854 } 855 856 static int vhost_vdpa_get_device_id(struct vhost_dev *dev, 857 uint32_t *device_id) 858 { 859 int ret; 860 ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_DEVICE_ID, device_id); 861 trace_vhost_vdpa_get_device_id(dev, *device_id); 862 return ret; 863 } 864 865 static int vhost_vdpa_reset_device(struct vhost_dev *dev) 866 { 867 struct vhost_vdpa *v = dev->opaque; 868 int ret; 869 uint8_t status = 0; 870 871 ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status); 872 trace_vhost_vdpa_reset_device(dev); 873 v->suspended = false; 874 return ret; 875 } 876 877 static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx) 878 { 879 assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs); 880 881 trace_vhost_vdpa_get_vq_index(dev, idx, idx); 882 return idx; 883 } 884 885 static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev) 886 { 887 int i; 888 trace_vhost_vdpa_set_vring_ready(dev); 889 for (i = 0; i < dev->nvqs; ++i) { 890 struct vhost_vring_state state = { 891 .index = dev->vq_index + i, 892 .num = 1, 893 }; 894 vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state); 895 } 896 return 0; 897 } 898 899 static int vhost_vdpa_set_config_call(struct vhost_dev *dev, 900 int fd) 901 { 902 trace_vhost_vdpa_set_config_call(dev, fd); 903 return vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG_CALL, &fd); 904 } 905 906 static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config, 907 uint32_t config_len) 908 { 909 int b, len; 910 char line[QEMU_HEXDUMP_LINE_LEN]; 911 912 for (b = 0; b < config_len; b += 16) { 913 len = config_len - b; 914 qemu_hexdump_line(line, b, config, len, false); 915 trace_vhost_vdpa_dump_config(dev, line); 916 } 917 } 918 919 static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data, 920 uint32_t offset, uint32_t size, 921 uint32_t flags) 922 { 923 struct vhost_vdpa_config *config; 924 int ret; 925 unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 926 927 trace_vhost_vdpa_set_config(dev, offset, size, flags); 928 config = g_malloc(size + config_size); 929 config->off = offset; 930 config->len = size; 931 memcpy(config->buf, data, size); 932 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG) && 933 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) { 934 vhost_vdpa_dump_config(dev, data, size); 935 } 936 ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG, config); 937 g_free(config); 938 return ret; 939 } 940 941 static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config, 942 uint32_t config_len, Error **errp) 943 { 944 struct vhost_vdpa_config *v_config; 945 unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 946 int ret; 947 948 trace_vhost_vdpa_get_config(dev, config, config_len); 949 v_config = g_malloc(config_len + config_size); 950 v_config->len = config_len; 951 v_config->off = 0; 952 ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config); 953 memcpy(config, v_config->buf, config_len); 954 g_free(v_config); 955 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG) && 956 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) { 957 vhost_vdpa_dump_config(dev, config, config_len); 958 } 959 return ret; 960 } 961 962 static int vhost_vdpa_set_dev_vring_base(struct vhost_dev *dev, 963 struct vhost_vring_state *ring) 964 { 965 trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num); 966 return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring); 967 } 968 969 static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev, 970 struct vhost_vring_file *file) 971 { 972 trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd); 973 return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file); 974 } 975 976 static int vhost_vdpa_set_vring_dev_call(struct vhost_dev *dev, 977 struct vhost_vring_file *file) 978 { 979 trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd); 980 return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file); 981 } 982 983 static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev *dev, 984 struct vhost_vring_addr *addr) 985 { 986 trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags, 987 addr->desc_user_addr, addr->used_user_addr, 988 addr->avail_user_addr, 989 addr->log_guest_addr); 990 991 return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr); 992 993 } 994 995 /** 996 * Set the shadow virtqueue descriptors to the device 997 * 998 * @dev: The vhost device model 999 * @svq: The shadow virtqueue 1000 * @idx: The index of the virtqueue in the vhost device 1001 * @errp: Error 1002 * 1003 * Note that this function does not rewind kick file descriptor if cannot set 1004 * call one. 1005 */ 1006 static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev, 1007 VhostShadowVirtqueue *svq, unsigned idx, 1008 Error **errp) 1009 { 1010 struct vhost_vring_file file = { 1011 .index = dev->vq_index + idx, 1012 }; 1013 const EventNotifier *event_notifier = &svq->hdev_kick; 1014 int r; 1015 1016 r = event_notifier_init(&svq->hdev_kick, 0); 1017 if (r != 0) { 1018 error_setg_errno(errp, -r, "Couldn't create kick event notifier"); 1019 goto err_init_hdev_kick; 1020 } 1021 1022 r = event_notifier_init(&svq->hdev_call, 0); 1023 if (r != 0) { 1024 error_setg_errno(errp, -r, "Couldn't create call event notifier"); 1025 goto err_init_hdev_call; 1026 } 1027 1028 file.fd = event_notifier_get_fd(event_notifier); 1029 r = vhost_vdpa_set_vring_dev_kick(dev, &file); 1030 if (unlikely(r != 0)) { 1031 error_setg_errno(errp, -r, "Can't set device kick fd"); 1032 goto err_init_set_dev_fd; 1033 } 1034 1035 event_notifier = &svq->hdev_call; 1036 file.fd = event_notifier_get_fd(event_notifier); 1037 r = vhost_vdpa_set_vring_dev_call(dev, &file); 1038 if (unlikely(r != 0)) { 1039 error_setg_errno(errp, -r, "Can't set device call fd"); 1040 goto err_init_set_dev_fd; 1041 } 1042 1043 return 0; 1044 1045 err_init_set_dev_fd: 1046 event_notifier_set_handler(&svq->hdev_call, NULL); 1047 1048 err_init_hdev_call: 1049 event_notifier_cleanup(&svq->hdev_kick); 1050 1051 err_init_hdev_kick: 1052 return r; 1053 } 1054 1055 /** 1056 * Unmap a SVQ area in the device 1057 */ 1058 static void vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr addr) 1059 { 1060 const DMAMap needle = { 1061 .translated_addr = addr, 1062 }; 1063 const DMAMap *result = vhost_iova_tree_find_iova(v->iova_tree, &needle); 1064 hwaddr size; 1065 int r; 1066 1067 if (unlikely(!result)) { 1068 error_report("Unable to find SVQ address to unmap"); 1069 return; 1070 } 1071 1072 size = ROUND_UP(result->size, qemu_real_host_page_size()); 1073 r = vhost_vdpa_dma_unmap(v, v->address_space_id, result->iova, size); 1074 if (unlikely(r < 0)) { 1075 error_report("Unable to unmap SVQ vring: %s (%d)", g_strerror(-r), -r); 1076 return; 1077 } 1078 1079 vhost_iova_tree_remove(v->iova_tree, *result); 1080 } 1081 1082 static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev, 1083 const VhostShadowVirtqueue *svq) 1084 { 1085 struct vhost_vdpa *v = dev->opaque; 1086 struct vhost_vring_addr svq_addr; 1087 1088 vhost_svq_get_vring_addr(svq, &svq_addr); 1089 1090 vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr); 1091 1092 vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr); 1093 } 1094 1095 /** 1096 * Map the SVQ area in the device 1097 * 1098 * @v: Vhost-vdpa device 1099 * @needle: The area to search iova 1100 * @errorp: Error pointer 1101 */ 1102 static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle, 1103 Error **errp) 1104 { 1105 int r; 1106 1107 r = vhost_iova_tree_map_alloc(v->iova_tree, needle); 1108 if (unlikely(r != IOVA_OK)) { 1109 error_setg(errp, "Cannot allocate iova (%d)", r); 1110 return false; 1111 } 1112 1113 r = vhost_vdpa_dma_map(v, v->address_space_id, needle->iova, 1114 needle->size + 1, 1115 (void *)(uintptr_t)needle->translated_addr, 1116 needle->perm == IOMMU_RO); 1117 if (unlikely(r != 0)) { 1118 error_setg_errno(errp, -r, "Cannot map region to device"); 1119 vhost_iova_tree_remove(v->iova_tree, *needle); 1120 } 1121 1122 return r == 0; 1123 } 1124 1125 /** 1126 * Map the shadow virtqueue rings in the device 1127 * 1128 * @dev: The vhost device 1129 * @svq: The shadow virtqueue 1130 * @addr: Assigned IOVA addresses 1131 * @errp: Error pointer 1132 */ 1133 static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev, 1134 const VhostShadowVirtqueue *svq, 1135 struct vhost_vring_addr *addr, 1136 Error **errp) 1137 { 1138 ERRP_GUARD(); 1139 DMAMap device_region, driver_region; 1140 struct vhost_vring_addr svq_addr; 1141 struct vhost_vdpa *v = dev->opaque; 1142 size_t device_size = vhost_svq_device_area_size(svq); 1143 size_t driver_size = vhost_svq_driver_area_size(svq); 1144 size_t avail_offset; 1145 bool ok; 1146 1147 vhost_svq_get_vring_addr(svq, &svq_addr); 1148 1149 driver_region = (DMAMap) { 1150 .translated_addr = svq_addr.desc_user_addr, 1151 .size = driver_size - 1, 1152 .perm = IOMMU_RO, 1153 }; 1154 ok = vhost_vdpa_svq_map_ring(v, &driver_region, errp); 1155 if (unlikely(!ok)) { 1156 error_prepend(errp, "Cannot create vq driver region: "); 1157 return false; 1158 } 1159 addr->desc_user_addr = driver_region.iova; 1160 avail_offset = svq_addr.avail_user_addr - svq_addr.desc_user_addr; 1161 addr->avail_user_addr = driver_region.iova + avail_offset; 1162 1163 device_region = (DMAMap) { 1164 .translated_addr = svq_addr.used_user_addr, 1165 .size = device_size - 1, 1166 .perm = IOMMU_RW, 1167 }; 1168 ok = vhost_vdpa_svq_map_ring(v, &device_region, errp); 1169 if (unlikely(!ok)) { 1170 error_prepend(errp, "Cannot create vq device region: "); 1171 vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr); 1172 } 1173 addr->used_user_addr = device_region.iova; 1174 1175 return ok; 1176 } 1177 1178 static bool vhost_vdpa_svq_setup(struct vhost_dev *dev, 1179 VhostShadowVirtqueue *svq, unsigned idx, 1180 Error **errp) 1181 { 1182 uint16_t vq_index = dev->vq_index + idx; 1183 struct vhost_vring_state s = { 1184 .index = vq_index, 1185 }; 1186 int r; 1187 1188 r = vhost_vdpa_set_dev_vring_base(dev, &s); 1189 if (unlikely(r)) { 1190 error_setg_errno(errp, -r, "Cannot set vring base"); 1191 return false; 1192 } 1193 1194 r = vhost_vdpa_svq_set_fds(dev, svq, idx, errp); 1195 return r == 0; 1196 } 1197 1198 static bool vhost_vdpa_svqs_start(struct vhost_dev *dev) 1199 { 1200 struct vhost_vdpa *v = dev->opaque; 1201 Error *err = NULL; 1202 unsigned i; 1203 1204 if (!v->shadow_vqs_enabled) { 1205 return true; 1206 } 1207 1208 for (i = 0; i < v->shadow_vqs->len; ++i) { 1209 VirtQueue *vq = virtio_get_queue(dev->vdev, dev->vq_index + i); 1210 VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i); 1211 struct vhost_vring_addr addr = { 1212 .index = dev->vq_index + i, 1213 }; 1214 int r; 1215 bool ok = vhost_vdpa_svq_setup(dev, svq, i, &err); 1216 if (unlikely(!ok)) { 1217 goto err; 1218 } 1219 1220 vhost_svq_start(svq, dev->vdev, vq, v->iova_tree); 1221 ok = vhost_vdpa_svq_map_rings(dev, svq, &addr, &err); 1222 if (unlikely(!ok)) { 1223 goto err_map; 1224 } 1225 1226 /* Override vring GPA set by vhost subsystem */ 1227 r = vhost_vdpa_set_vring_dev_addr(dev, &addr); 1228 if (unlikely(r != 0)) { 1229 error_setg_errno(&err, -r, "Cannot set device address"); 1230 goto err_set_addr; 1231 } 1232 } 1233 1234 return true; 1235 1236 err_set_addr: 1237 vhost_vdpa_svq_unmap_rings(dev, g_ptr_array_index(v->shadow_vqs, i)); 1238 1239 err_map: 1240 vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, i)); 1241 1242 err: 1243 error_reportf_err(err, "Cannot setup SVQ %u: ", i); 1244 for (unsigned j = 0; j < i; ++j) { 1245 VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, j); 1246 vhost_vdpa_svq_unmap_rings(dev, svq); 1247 vhost_svq_stop(svq); 1248 } 1249 1250 return false; 1251 } 1252 1253 static void vhost_vdpa_svqs_stop(struct vhost_dev *dev) 1254 { 1255 struct vhost_vdpa *v = dev->opaque; 1256 1257 if (!v->shadow_vqs_enabled) { 1258 return; 1259 } 1260 1261 for (unsigned i = 0; i < v->shadow_vqs->len; ++i) { 1262 VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i); 1263 1264 vhost_svq_stop(svq); 1265 vhost_vdpa_svq_unmap_rings(dev, svq); 1266 1267 event_notifier_cleanup(&svq->hdev_kick); 1268 event_notifier_cleanup(&svq->hdev_call); 1269 } 1270 } 1271 1272 static void vhost_vdpa_suspend(struct vhost_dev *dev) 1273 { 1274 struct vhost_vdpa *v = dev->opaque; 1275 int r; 1276 1277 if (!vhost_vdpa_first_dev(dev)) { 1278 return; 1279 } 1280 1281 if (dev->backend_cap & BIT_ULL(VHOST_BACKEND_F_SUSPEND)) { 1282 trace_vhost_vdpa_suspend(dev); 1283 r = ioctl(v->device_fd, VHOST_VDPA_SUSPEND); 1284 if (unlikely(r)) { 1285 error_report("Cannot suspend: %s(%d)", g_strerror(errno), errno); 1286 } else { 1287 v->suspended = true; 1288 return; 1289 } 1290 } 1291 1292 vhost_vdpa_reset_device(dev); 1293 } 1294 1295 static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started) 1296 { 1297 struct vhost_vdpa *v = dev->opaque; 1298 bool ok; 1299 trace_vhost_vdpa_dev_start(dev, started); 1300 1301 if (started) { 1302 vhost_vdpa_host_notifiers_init(dev); 1303 ok = vhost_vdpa_svqs_start(dev); 1304 if (unlikely(!ok)) { 1305 return -1; 1306 } 1307 vhost_vdpa_set_vring_ready(dev); 1308 } else { 1309 vhost_vdpa_suspend(dev); 1310 vhost_vdpa_svqs_stop(dev); 1311 vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs); 1312 } 1313 1314 if (dev->vq_index + dev->nvqs != dev->vq_index_end) { 1315 return 0; 1316 } 1317 1318 if (started) { 1319 if (vhost_dev_has_iommu(dev) && (v->shadow_vqs_enabled)) { 1320 error_report("SVQ can not work while IOMMU enable, please disable" 1321 "IOMMU and try again"); 1322 return -1; 1323 } 1324 memory_listener_register(&v->listener, dev->vdev->dma_as); 1325 1326 return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK); 1327 } 1328 1329 return 0; 1330 } 1331 1332 static void vhost_vdpa_reset_status(struct vhost_dev *dev) 1333 { 1334 struct vhost_vdpa *v = dev->opaque; 1335 1336 if (dev->vq_index + dev->nvqs != dev->vq_index_end) { 1337 return; 1338 } 1339 1340 vhost_vdpa_reset_device(dev); 1341 vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE | 1342 VIRTIO_CONFIG_S_DRIVER); 1343 memory_listener_unregister(&v->listener); 1344 } 1345 1346 static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base, 1347 struct vhost_log *log) 1348 { 1349 struct vhost_vdpa *v = dev->opaque; 1350 if (v->shadow_vqs_enabled || !vhost_vdpa_first_dev(dev)) { 1351 return 0; 1352 } 1353 1354 trace_vhost_vdpa_set_log_base(dev, base, log->size, log->refcnt, log->fd, 1355 log->log); 1356 return vhost_vdpa_call(dev, VHOST_SET_LOG_BASE, &base); 1357 } 1358 1359 static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev, 1360 struct vhost_vring_addr *addr) 1361 { 1362 struct vhost_vdpa *v = dev->opaque; 1363 1364 if (v->shadow_vqs_enabled) { 1365 /* 1366 * Device vring addr was set at device start. SVQ base is handled by 1367 * VirtQueue code. 1368 */ 1369 return 0; 1370 } 1371 1372 return vhost_vdpa_set_vring_dev_addr(dev, addr); 1373 } 1374 1375 static int vhost_vdpa_set_vring_num(struct vhost_dev *dev, 1376 struct vhost_vring_state *ring) 1377 { 1378 trace_vhost_vdpa_set_vring_num(dev, ring->index, ring->num); 1379 return vhost_vdpa_call(dev, VHOST_SET_VRING_NUM, ring); 1380 } 1381 1382 static int vhost_vdpa_set_vring_base(struct vhost_dev *dev, 1383 struct vhost_vring_state *ring) 1384 { 1385 struct vhost_vdpa *v = dev->opaque; 1386 1387 if (v->shadow_vqs_enabled) { 1388 /* 1389 * Device vring base was set at device start. SVQ base is handled by 1390 * VirtQueue code. 1391 */ 1392 return 0; 1393 } 1394 1395 return vhost_vdpa_set_dev_vring_base(dev, ring); 1396 } 1397 1398 static int vhost_vdpa_get_vring_base(struct vhost_dev *dev, 1399 struct vhost_vring_state *ring) 1400 { 1401 struct vhost_vdpa *v = dev->opaque; 1402 int ret; 1403 1404 if (v->shadow_vqs_enabled) { 1405 ring->num = virtio_queue_get_last_avail_idx(dev->vdev, ring->index); 1406 return 0; 1407 } 1408 1409 if (!v->suspended) { 1410 /* 1411 * Cannot trust in value returned by device, let vhost recover used 1412 * idx from guest. 1413 */ 1414 return -1; 1415 } 1416 1417 ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring); 1418 trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num); 1419 return ret; 1420 } 1421 1422 static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev, 1423 struct vhost_vring_file *file) 1424 { 1425 struct vhost_vdpa *v = dev->opaque; 1426 int vdpa_idx = file->index - dev->vq_index; 1427 1428 if (v->shadow_vqs_enabled) { 1429 VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx); 1430 vhost_svq_set_svq_kick_fd(svq, file->fd); 1431 return 0; 1432 } else { 1433 return vhost_vdpa_set_vring_dev_kick(dev, file); 1434 } 1435 } 1436 1437 static int vhost_vdpa_set_vring_call(struct vhost_dev *dev, 1438 struct vhost_vring_file *file) 1439 { 1440 struct vhost_vdpa *v = dev->opaque; 1441 int vdpa_idx = file->index - dev->vq_index; 1442 VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx); 1443 1444 /* Remember last call fd because we can switch to SVQ anytime. */ 1445 vhost_svq_set_svq_call_fd(svq, file->fd); 1446 if (v->shadow_vqs_enabled) { 1447 return 0; 1448 } 1449 1450 return vhost_vdpa_set_vring_dev_call(dev, file); 1451 } 1452 1453 static int vhost_vdpa_get_features(struct vhost_dev *dev, 1454 uint64_t *features) 1455 { 1456 int ret = vhost_vdpa_get_dev_features(dev, features); 1457 1458 if (ret == 0) { 1459 /* Add SVQ logging capabilities */ 1460 *features |= BIT_ULL(VHOST_F_LOG_ALL); 1461 } 1462 1463 return ret; 1464 } 1465 1466 static int vhost_vdpa_set_owner(struct vhost_dev *dev) 1467 { 1468 if (!vhost_vdpa_first_dev(dev)) { 1469 return 0; 1470 } 1471 1472 trace_vhost_vdpa_set_owner(dev); 1473 return vhost_vdpa_call(dev, VHOST_SET_OWNER, NULL); 1474 } 1475 1476 static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev, 1477 struct vhost_vring_addr *addr, struct vhost_virtqueue *vq) 1478 { 1479 assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA); 1480 addr->desc_user_addr = (uint64_t)(unsigned long)vq->desc_phys; 1481 addr->avail_user_addr = (uint64_t)(unsigned long)vq->avail_phys; 1482 addr->used_user_addr = (uint64_t)(unsigned long)vq->used_phys; 1483 trace_vhost_vdpa_vq_get_addr(dev, vq, addr->desc_user_addr, 1484 addr->avail_user_addr, addr->used_user_addr); 1485 return 0; 1486 } 1487 1488 static bool vhost_vdpa_force_iommu(struct vhost_dev *dev) 1489 { 1490 return true; 1491 } 1492 1493 const VhostOps vdpa_ops = { 1494 .backend_type = VHOST_BACKEND_TYPE_VDPA, 1495 .vhost_backend_init = vhost_vdpa_init, 1496 .vhost_backend_cleanup = vhost_vdpa_cleanup, 1497 .vhost_set_log_base = vhost_vdpa_set_log_base, 1498 .vhost_set_vring_addr = vhost_vdpa_set_vring_addr, 1499 .vhost_set_vring_num = vhost_vdpa_set_vring_num, 1500 .vhost_set_vring_base = vhost_vdpa_set_vring_base, 1501 .vhost_get_vring_base = vhost_vdpa_get_vring_base, 1502 .vhost_set_vring_kick = vhost_vdpa_set_vring_kick, 1503 .vhost_set_vring_call = vhost_vdpa_set_vring_call, 1504 .vhost_get_features = vhost_vdpa_get_features, 1505 .vhost_set_backend_cap = vhost_vdpa_set_backend_cap, 1506 .vhost_set_owner = vhost_vdpa_set_owner, 1507 .vhost_set_vring_endian = NULL, 1508 .vhost_backend_memslots_limit = vhost_vdpa_memslots_limit, 1509 .vhost_set_mem_table = vhost_vdpa_set_mem_table, 1510 .vhost_set_features = vhost_vdpa_set_features, 1511 .vhost_reset_device = vhost_vdpa_reset_device, 1512 .vhost_get_vq_index = vhost_vdpa_get_vq_index, 1513 .vhost_get_config = vhost_vdpa_get_config, 1514 .vhost_set_config = vhost_vdpa_set_config, 1515 .vhost_requires_shm_log = NULL, 1516 .vhost_migration_done = NULL, 1517 .vhost_backend_can_merge = NULL, 1518 .vhost_net_set_mtu = NULL, 1519 .vhost_set_iotlb_callback = NULL, 1520 .vhost_send_device_iotlb_msg = NULL, 1521 .vhost_dev_start = vhost_vdpa_dev_start, 1522 .vhost_get_device_id = vhost_vdpa_get_device_id, 1523 .vhost_vq_get_addr = vhost_vdpa_vq_get_addr, 1524 .vhost_force_iommu = vhost_vdpa_force_iommu, 1525 .vhost_set_config_call = vhost_vdpa_set_config_call, 1526 .vhost_reset_status = vhost_vdpa_reset_status, 1527 }; 1528