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