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