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