4 * Copyright(c) 2017-2018 Intel Corporation.
5 * Copyright(c) 2020 Red Hat, Inc.
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.
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"
28 #include "qapi/error.h"
31 * Return one past the end of the end of section. Be careful with uint64_t
34 static Int128
vhost_vdpa_section_end(const MemoryRegionSection
*section
,
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
));
44 static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection
*section
,
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
)) {
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
);
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
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
));
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.
89 int vhost_vdpa_dma_map(struct vhost_vdpa
*v
, uint32_t asid
, hwaddr iova
,
90 hwaddr size
, void *vaddr
, bool readonly
)
92 struct vhost_msg_v2 msg
= {};
93 int fd
= v
->device_fd
;
96 msg
.type
= v
->msg_type
;
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
;
104 trace_vhost_vdpa_dma_map(v
, fd
, msg
.type
, msg
.asid
, msg
.iotlb
.iova
,
105 msg
.iotlb
.size
, msg
.iotlb
.uaddr
, msg
.iotlb
.perm
,
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
));
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.
121 int vhost_vdpa_dma_unmap(struct vhost_vdpa
*v
, uint32_t asid
, hwaddr iova
,
124 struct vhost_msg_v2 msg
= {};
125 int fd
= v
->device_fd
;
128 msg
.type
= v
->msg_type
;
130 msg
.iotlb
.iova
= iova
;
131 msg
.iotlb
.size
= size
;
132 msg
.iotlb
.type
= VHOST_IOTLB_INVALIDATE
;
134 trace_vhost_vdpa_dma_unmap(v
, fd
, msg
.type
, msg
.asid
, msg
.iotlb
.iova
,
135 msg
.iotlb
.size
, msg
.iotlb
.type
);
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
));
146 static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa
*v
)
148 int fd
= v
->device_fd
;
149 struct vhost_msg_v2 msg
= {
151 .iotlb
.type
= VHOST_IOTLB_BATCH_BEGIN
,
154 trace_vhost_vdpa_listener_begin_batch(v
, fd
, msg
.type
, msg
.iotlb
.type
);
155 if (write(fd
, &msg
, sizeof(msg
)) != sizeof(msg
)) {
156 error_report("failed to write, fd=%d, errno=%d (%s)",
157 fd
, errno
, strerror(errno
));
161 static void vhost_vdpa_iotlb_batch_begin_once(struct vhost_vdpa
*v
)
163 if (v
->dev
->backend_cap
& (0x1ULL
<< VHOST_BACKEND_F_IOTLB_BATCH
) &&
164 !v
->iotlb_batch_begin_sent
) {
165 vhost_vdpa_listener_begin_batch(v
);
168 v
->iotlb_batch_begin_sent
= true;
171 static void vhost_vdpa_listener_commit(MemoryListener
*listener
)
173 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
174 struct vhost_dev
*dev
= v
->dev
;
175 struct vhost_msg_v2 msg
= {};
176 int fd
= v
->device_fd
;
178 if (!(dev
->backend_cap
& (0x1ULL
<< VHOST_BACKEND_F_IOTLB_BATCH
))) {
182 if (!v
->iotlb_batch_begin_sent
) {
186 msg
.type
= v
->msg_type
;
187 msg
.iotlb
.type
= VHOST_IOTLB_BATCH_END
;
189 trace_vhost_vdpa_listener_commit(v
, fd
, msg
.type
, msg
.iotlb
.type
);
190 if (write(fd
, &msg
, sizeof(msg
)) != sizeof(msg
)) {
191 error_report("failed to write, fd=%d, errno=%d (%s)",
192 fd
, errno
, strerror(errno
));
195 v
->iotlb_batch_begin_sent
= false;
198 static void vhost_vdpa_iommu_map_notify(IOMMUNotifier
*n
, IOMMUTLBEntry
*iotlb
)
200 struct vdpa_iommu
*iommu
= container_of(n
, struct vdpa_iommu
, n
);
202 hwaddr iova
= iotlb
->iova
+ iommu
->iommu_offset
;
203 struct vhost_vdpa
*v
= iommu
->dev
;
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");
213 RCU_READ_LOCK_GUARD();
214 /* check if RAM section out of device range */
215 llend
= int128_add(int128_makes64(iotlb
->addr_mask
), int128_makes64(iova
));
216 if (int128_gt(llend
, int128_make64(v
->iova_range
.last
))) {
217 error_report("RAM section out of device range (max=0x%" PRIx64
218 ", end addr=0x%" PRIx64
")",
219 v
->iova_range
.last
, int128_get64(llend
));
223 if ((iotlb
->perm
& IOMMU_RW
) != IOMMU_NONE
) {
226 if (!memory_get_xlat_addr(iotlb
, &vaddr
, NULL
, &read_only
, NULL
)) {
229 ret
= vhost_vdpa_dma_map(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
230 iotlb
->addr_mask
+ 1, vaddr
, read_only
);
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
);
237 ret
= vhost_vdpa_dma_unmap(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
238 iotlb
->addr_mask
+ 1);
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
);
247 static void vhost_vdpa_iommu_region_add(MemoryListener
*listener
,
248 MemoryRegionSection
*section
)
250 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
252 struct vdpa_iommu
*iommu
;
255 IOMMUMemoryRegion
*iommu_mr
;
258 iommu_mr
= IOMMU_MEMORY_REGION(section
->mr
);
260 iommu
= g_malloc0(sizeof(*iommu
));
261 end
= int128_add(int128_make64(section
->offset_within_region
),
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
,
272 iommu
->iommu_offset
= section
->offset_within_address_space
-
273 section
->offset_within_region
;
276 ret
= memory_region_register_iommu_notifier(section
->mr
, &iommu
->n
, NULL
);
282 QLIST_INSERT_HEAD(&v
->iommu_list
, iommu
, iommu_next
);
283 memory_region_iommu_replay(iommu
->iommu_mr
, &iommu
->n
);
288 static void vhost_vdpa_iommu_region_del(MemoryListener
*listener
,
289 MemoryRegionSection
*section
)
291 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
293 struct vdpa_iommu
*iommu
;
295 QLIST_FOREACH(iommu
, &v
->iommu_list
, iommu_next
)
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
);
307 static void vhost_vdpa_listener_region_add(MemoryListener
*listener
,
308 MemoryRegionSection
*section
)
310 DMAMap mem_region
= {};
311 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
313 Int128 llend
, llsize
;
316 int page_size
= qemu_target_page_size();
317 int page_mask
= -page_size
;
319 if (vhost_vdpa_listener_skipped_section(section
, v
->iova_range
.first
,
320 v
->iova_range
.last
, page_mask
)) {
323 if (memory_region_is_iommu(section
->mr
)) {
324 vhost_vdpa_iommu_region_add(listener
, section
);
328 if (unlikely((section
->offset_within_address_space
& ~page_mask
) !=
329 (section
->offset_within_region
& ~page_mask
))) {
330 trace_vhost_vdpa_listener_region_add_unaligned(v
, section
->mr
->name
,
331 section
->offset_within_address_space
& ~page_mask
,
332 section
->offset_within_region
& ~page_mask
);
336 iova
= ROUND_UP(section
->offset_within_address_space
, page_size
);
337 llend
= vhost_vdpa_section_end(section
, page_mask
);
338 if (int128_ge(int128_make64(iova
), llend
)) {
342 memory_region_ref(section
->mr
);
344 /* Here we assume that memory_region_is_ram(section->mr)==true */
346 vaddr
= memory_region_get_ram_ptr(section
->mr
) +
347 section
->offset_within_region
+
348 (iova
- section
->offset_within_address_space
);
350 trace_vhost_vdpa_listener_region_add(v
, iova
, int128_get64(llend
),
351 vaddr
, section
->readonly
);
353 llsize
= int128_sub(llend
, int128_make64(iova
));
354 if (v
->shadow_data
) {
357 mem_region
.translated_addr
= (hwaddr
)(uintptr_t)vaddr
,
358 mem_region
.size
= int128_get64(llsize
) - 1,
359 mem_region
.perm
= IOMMU_ACCESS_FLAG(true, section
->readonly
),
361 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &mem_region
);
362 if (unlikely(r
!= IOVA_OK
)) {
363 error_report("Can't allocate a mapping (%d)", r
);
367 iova
= mem_region
.iova
;
370 vhost_vdpa_iotlb_batch_begin_once(v
);
371 ret
= vhost_vdpa_dma_map(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
372 int128_get64(llsize
), vaddr
, section
->readonly
);
374 error_report("vhost vdpa map fail!");
381 if (v
->shadow_data
) {
382 vhost_iova_tree_remove(v
->iova_tree
, mem_region
);
387 * On the initfn path, store the first error in the container so we
388 * can gracefully fail. Runtime, there's not much we can do other
389 * than throw a hardware error.
391 error_report("vhost-vdpa: DMA mapping failed, unable to continue");
396 static void vhost_vdpa_listener_region_del(MemoryListener
*listener
,
397 MemoryRegionSection
*section
)
399 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
401 Int128 llend
, llsize
;
403 int page_size
= qemu_target_page_size();
404 int page_mask
= -page_size
;
406 if (vhost_vdpa_listener_skipped_section(section
, v
->iova_range
.first
,
407 v
->iova_range
.last
, page_mask
)) {
410 if (memory_region_is_iommu(section
->mr
)) {
411 vhost_vdpa_iommu_region_del(listener
, section
);
414 if (unlikely((section
->offset_within_address_space
& ~page_mask
) !=
415 (section
->offset_within_region
& ~page_mask
))) {
416 trace_vhost_vdpa_listener_region_del_unaligned(v
, section
->mr
->name
,
417 section
->offset_within_address_space
& ~page_mask
,
418 section
->offset_within_region
& ~page_mask
);
422 iova
= ROUND_UP(section
->offset_within_address_space
, page_size
);
423 llend
= vhost_vdpa_section_end(section
, page_mask
);
425 trace_vhost_vdpa_listener_region_del(v
, iova
,
426 int128_get64(int128_sub(llend
, int128_one())));
428 if (int128_ge(int128_make64(iova
), llend
)) {
432 llsize
= int128_sub(llend
, int128_make64(iova
));
434 if (v
->shadow_data
) {
435 const DMAMap
*result
;
436 const void *vaddr
= memory_region_get_ram_ptr(section
->mr
) +
437 section
->offset_within_region
+
438 (iova
- section
->offset_within_address_space
);
439 DMAMap mem_region
= {
440 .translated_addr
= (hwaddr
)(uintptr_t)vaddr
,
441 .size
= int128_get64(llsize
) - 1,
444 result
= vhost_iova_tree_find_iova(v
->iova_tree
, &mem_region
);
446 /* The memory listener map wasn't mapped */
450 vhost_iova_tree_remove(v
->iova_tree
, *result
);
452 vhost_vdpa_iotlb_batch_begin_once(v
);
454 * The unmap ioctl doesn't accept a full 64-bit. need to check it
456 if (int128_eq(llsize
, int128_2_64())) {
457 llsize
= int128_rshift(llsize
, 1);
458 ret
= vhost_vdpa_dma_unmap(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
459 int128_get64(llsize
));
462 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx
", "
463 "0x%" HWADDR_PRIx
") = %d (%m)",
464 v
, iova
, int128_get64(llsize
), ret
);
466 iova
+= int128_get64(llsize
);
468 ret
= vhost_vdpa_dma_unmap(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
469 int128_get64(llsize
));
472 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx
", "
473 "0x%" HWADDR_PRIx
") = %d (%m)",
474 v
, iova
, int128_get64(llsize
), ret
);
477 memory_region_unref(section
->mr
);
480 * IOTLB API is used by vhost-vdpa which requires incremental updating
481 * of the mapping. So we can not use generic vhost memory listener which
482 * depends on the addnop().
484 static const MemoryListener vhost_vdpa_memory_listener
= {
485 .name
= "vhost-vdpa",
486 .commit
= vhost_vdpa_listener_commit
,
487 .region_add
= vhost_vdpa_listener_region_add
,
488 .region_del
= vhost_vdpa_listener_region_del
,
491 static int vhost_vdpa_call(struct vhost_dev
*dev
, unsigned long int request
,
494 struct vhost_vdpa
*v
= dev
->opaque
;
495 int fd
= v
->device_fd
;
498 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
500 ret
= ioctl(fd
, request
, arg
);
501 return ret
< 0 ? -errno
: ret
;
504 static int vhost_vdpa_add_status(struct vhost_dev
*dev
, uint8_t status
)
509 trace_vhost_vdpa_add_status(dev
, status
);
510 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_STATUS
, &s
);
514 if ((s
& status
) == status
) {
515 /* Don't set bits already set */
521 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_SET_STATUS
, &s
);
526 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_STATUS
, &s
);
538 int vhost_vdpa_get_iova_range(int fd
, struct vhost_vdpa_iova_range
*iova_range
)
540 int ret
= ioctl(fd
, VHOST_VDPA_GET_IOVA_RANGE
, iova_range
);
542 return ret
< 0 ? -errno
: 0;
546 * The use of this function is for requests that only need to be
547 * applied once. Typically such request occurs at the beginning
548 * of operation, and before setting up queues. It should not be
549 * used for request that performs operation until all queues are
550 * set, which would need to check dev->vq_index_end instead.
552 static bool vhost_vdpa_first_dev(struct vhost_dev
*dev
)
554 struct vhost_vdpa
*v
= dev
->opaque
;
556 return v
->index
== 0;
559 static int vhost_vdpa_get_dev_features(struct vhost_dev
*dev
,
564 ret
= vhost_vdpa_call(dev
, VHOST_GET_FEATURES
, features
);
565 trace_vhost_vdpa_get_features(dev
, *features
);
569 static void vhost_vdpa_init_svq(struct vhost_dev
*hdev
, struct vhost_vdpa
*v
)
571 g_autoptr(GPtrArray
) shadow_vqs
= NULL
;
573 shadow_vqs
= g_ptr_array_new_full(hdev
->nvqs
, vhost_svq_free
);
574 for (unsigned n
= 0; n
< hdev
->nvqs
; ++n
) {
575 VhostShadowVirtqueue
*svq
;
577 svq
= vhost_svq_new(v
->shadow_vq_ops
, v
->shadow_vq_ops_opaque
);
578 g_ptr_array_add(shadow_vqs
, svq
);
581 v
->shadow_vqs
= g_steal_pointer(&shadow_vqs
);
584 static int vhost_vdpa_init(struct vhost_dev
*dev
, void *opaque
, Error
**errp
)
586 struct vhost_vdpa
*v
;
587 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
588 trace_vhost_vdpa_init(dev
, opaque
);
593 dev
->opaque
= opaque
;
594 v
->listener
= vhost_vdpa_memory_listener
;
595 v
->msg_type
= VHOST_IOTLB_MSG_V2
;
596 vhost_vdpa_init_svq(dev
, v
);
598 error_propagate(&dev
->migration_blocker
, v
->migration_blocker
);
599 if (!vhost_vdpa_first_dev(dev
)) {
604 * If dev->shadow_vqs_enabled at initialization that means the device has
605 * been started with x-svq=on, so don't block migration
607 if (dev
->migration_blocker
== NULL
&& !v
->shadow_vqs_enabled
) {
608 /* We don't have dev->features yet */
610 ret
= vhost_vdpa_get_dev_features(dev
, &features
);
612 error_setg_errno(errp
, -ret
, "Could not get device features");
615 vhost_svq_valid_features(features
, &dev
->migration_blocker
);
619 * Similar to VFIO, we end up pinning all guest memory and have to
620 * disable discarding of RAM.
622 ret
= ram_block_discard_disable(true);
624 error_report("Cannot set discarding of RAM broken");
628 vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_ACKNOWLEDGE
|
629 VIRTIO_CONFIG_S_DRIVER
);
634 static void vhost_vdpa_host_notifier_uninit(struct vhost_dev
*dev
,
637 size_t page_size
= qemu_real_host_page_size();
638 struct vhost_vdpa
*v
= dev
->opaque
;
639 VirtIODevice
*vdev
= dev
->vdev
;
640 VhostVDPAHostNotifier
*n
;
642 n
= &v
->notifier
[queue_index
];
645 virtio_queue_set_host_notifier_mr(vdev
, queue_index
, &n
->mr
, false);
646 object_unparent(OBJECT(&n
->mr
));
647 munmap(n
->addr
, page_size
);
652 static int vhost_vdpa_host_notifier_init(struct vhost_dev
*dev
, int queue_index
)
654 size_t page_size
= qemu_real_host_page_size();
655 struct vhost_vdpa
*v
= dev
->opaque
;
656 VirtIODevice
*vdev
= dev
->vdev
;
657 VhostVDPAHostNotifier
*n
;
658 int fd
= v
->device_fd
;
662 vhost_vdpa_host_notifier_uninit(dev
, queue_index
);
664 n
= &v
->notifier
[queue_index
];
666 addr
= mmap(NULL
, page_size
, PROT_WRITE
, MAP_SHARED
, fd
,
667 queue_index
* page_size
);
668 if (addr
== MAP_FAILED
) {
672 name
= g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]",
674 memory_region_init_ram_device_ptr(&n
->mr
, OBJECT(vdev
), name
,
678 if (virtio_queue_set_host_notifier_mr(vdev
, queue_index
, &n
->mr
, true)) {
679 object_unparent(OBJECT(&n
->mr
));
680 munmap(addr
, page_size
);
691 static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev
*dev
, int n
)
696 * Pack all the changes to the memory regions in a single
697 * transaction to avoid a few updating of the address space
700 memory_region_transaction_begin();
702 for (i
= dev
->vq_index
; i
< dev
->vq_index
+ n
; i
++) {
703 vhost_vdpa_host_notifier_uninit(dev
, i
);
706 memory_region_transaction_commit();
709 static void vhost_vdpa_host_notifiers_init(struct vhost_dev
*dev
)
711 struct vhost_vdpa
*v
= dev
->opaque
;
714 if (v
->shadow_vqs_enabled
) {
715 /* FIXME SVQ is not compatible with host notifiers mr */
720 * Pack all the changes to the memory regions in a single
721 * transaction to avoid a few updating of the address space
724 memory_region_transaction_begin();
726 for (i
= dev
->vq_index
; i
< dev
->vq_index
+ dev
->nvqs
; i
++) {
727 if (vhost_vdpa_host_notifier_init(dev
, i
)) {
728 vhost_vdpa_host_notifiers_uninit(dev
, i
- dev
->vq_index
);
733 memory_region_transaction_commit();
736 static void vhost_vdpa_svq_cleanup(struct vhost_dev
*dev
)
738 struct vhost_vdpa
*v
= dev
->opaque
;
741 for (idx
= 0; idx
< v
->shadow_vqs
->len
; ++idx
) {
742 vhost_svq_stop(g_ptr_array_index(v
->shadow_vqs
, idx
));
744 g_ptr_array_free(v
->shadow_vqs
, true);
747 static int vhost_vdpa_cleanup(struct vhost_dev
*dev
)
749 struct vhost_vdpa
*v
;
750 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
752 trace_vhost_vdpa_cleanup(dev
, v
);
753 if (vhost_vdpa_first_dev(dev
)) {
754 ram_block_discard_disable(false);
757 vhost_vdpa_host_notifiers_uninit(dev
, dev
->nvqs
);
758 memory_listener_unregister(&v
->listener
);
759 vhost_vdpa_svq_cleanup(dev
);
766 static int vhost_vdpa_memslots_limit(struct vhost_dev
*dev
)
768 trace_vhost_vdpa_memslots_limit(dev
, INT_MAX
);
772 static int vhost_vdpa_set_mem_table(struct vhost_dev
*dev
,
773 struct vhost_memory
*mem
)
775 if (!vhost_vdpa_first_dev(dev
)) {
779 trace_vhost_vdpa_set_mem_table(dev
, mem
->nregions
, mem
->padding
);
780 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE
) &&
781 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS
)) {
783 for (i
= 0; i
< mem
->nregions
; i
++) {
784 trace_vhost_vdpa_dump_regions(dev
, i
,
785 mem
->regions
[i
].guest_phys_addr
,
786 mem
->regions
[i
].memory_size
,
787 mem
->regions
[i
].userspace_addr
,
788 mem
->regions
[i
].flags_padding
);
798 static int vhost_vdpa_set_features(struct vhost_dev
*dev
,
801 struct vhost_vdpa
*v
= dev
->opaque
;
804 if (!vhost_vdpa_first_dev(dev
)) {
808 if (v
->shadow_vqs_enabled
) {
809 if ((v
->acked_features
^ features
) == BIT_ULL(VHOST_F_LOG_ALL
)) {
811 * QEMU is just trying to enable or disable logging. SVQ handles
812 * this sepparately, so no need to forward this.
814 v
->acked_features
= features
;
818 v
->acked_features
= features
;
820 /* We must not ack _F_LOG if SVQ is enabled */
821 features
&= ~BIT_ULL(VHOST_F_LOG_ALL
);
824 trace_vhost_vdpa_set_features(dev
, features
);
825 ret
= vhost_vdpa_call(dev
, VHOST_SET_FEATURES
, &features
);
830 return vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_FEATURES_OK
);
833 static int vhost_vdpa_set_backend_cap(struct vhost_dev
*dev
)
836 uint64_t f
= 0x1ULL
<< VHOST_BACKEND_F_IOTLB_MSG_V2
|
837 0x1ULL
<< VHOST_BACKEND_F_IOTLB_BATCH
|
838 0x1ULL
<< VHOST_BACKEND_F_IOTLB_ASID
|
839 0x1ULL
<< VHOST_BACKEND_F_SUSPEND
;
842 if (vhost_vdpa_call(dev
, VHOST_GET_BACKEND_FEATURES
, &features
)) {
848 if (vhost_vdpa_first_dev(dev
)) {
849 r
= vhost_vdpa_call(dev
, VHOST_SET_BACKEND_FEATURES
, &features
);
855 dev
->backend_cap
= features
;
860 static int vhost_vdpa_get_device_id(struct vhost_dev
*dev
,
864 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_DEVICE_ID
, device_id
);
865 trace_vhost_vdpa_get_device_id(dev
, *device_id
);
869 static int vhost_vdpa_reset_device(struct vhost_dev
*dev
)
871 struct vhost_vdpa
*v
= dev
->opaque
;
875 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_SET_STATUS
, &status
);
876 trace_vhost_vdpa_reset_device(dev
);
877 v
->suspended
= false;
881 static int vhost_vdpa_get_vq_index(struct vhost_dev
*dev
, int idx
)
883 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
885 trace_vhost_vdpa_get_vq_index(dev
, idx
, idx
);
889 int vhost_vdpa_set_vring_ready(struct vhost_vdpa
*v
, unsigned idx
)
891 struct vhost_dev
*dev
= v
->dev
;
892 struct vhost_vring_state state
= {
896 int r
= vhost_vdpa_call(dev
, VHOST_VDPA_SET_VRING_ENABLE
, &state
);
898 trace_vhost_vdpa_set_vring_ready(dev
, idx
, r
);
902 static int vhost_vdpa_set_config_call(struct vhost_dev
*dev
,
905 trace_vhost_vdpa_set_config_call(dev
, fd
);
906 return vhost_vdpa_call(dev
, VHOST_VDPA_SET_CONFIG_CALL
, &fd
);
909 static void vhost_vdpa_dump_config(struct vhost_dev
*dev
, const uint8_t *config
,
913 char line
[QEMU_HEXDUMP_LINE_LEN
];
915 for (b
= 0; b
< config_len
; b
+= 16) {
916 len
= config_len
- b
;
917 qemu_hexdump_line(line
, b
, config
, len
, false);
918 trace_vhost_vdpa_dump_config(dev
, line
);
922 static int vhost_vdpa_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
923 uint32_t offset
, uint32_t size
,
926 struct vhost_vdpa_config
*config
;
928 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
930 trace_vhost_vdpa_set_config(dev
, offset
, size
, flags
);
931 config
= g_malloc(size
+ config_size
);
932 config
->off
= offset
;
934 memcpy(config
->buf
, data
, size
);
935 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG
) &&
936 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG
)) {
937 vhost_vdpa_dump_config(dev
, data
, size
);
939 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_SET_CONFIG
, config
);
944 static int vhost_vdpa_get_config(struct vhost_dev
*dev
, uint8_t *config
,
945 uint32_t config_len
, Error
**errp
)
947 struct vhost_vdpa_config
*v_config
;
948 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
951 trace_vhost_vdpa_get_config(dev
, config
, config_len
);
952 v_config
= g_malloc(config_len
+ config_size
);
953 v_config
->len
= config_len
;
955 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_CONFIG
, v_config
);
956 memcpy(config
, v_config
->buf
, config_len
);
958 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG
) &&
959 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG
)) {
960 vhost_vdpa_dump_config(dev
, config
, config_len
);
965 static int vhost_vdpa_set_dev_vring_base(struct vhost_dev
*dev
,
966 struct vhost_vring_state
*ring
)
968 trace_vhost_vdpa_set_vring_base(dev
, ring
->index
, ring
->num
);
969 return vhost_vdpa_call(dev
, VHOST_SET_VRING_BASE
, ring
);
972 static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev
*dev
,
973 struct vhost_vring_file
*file
)
975 trace_vhost_vdpa_set_vring_kick(dev
, file
->index
, file
->fd
);
976 return vhost_vdpa_call(dev
, VHOST_SET_VRING_KICK
, file
);
979 static int vhost_vdpa_set_vring_dev_call(struct vhost_dev
*dev
,
980 struct vhost_vring_file
*file
)
982 trace_vhost_vdpa_set_vring_call(dev
, file
->index
, file
->fd
);
983 return vhost_vdpa_call(dev
, VHOST_SET_VRING_CALL
, file
);
986 static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev
*dev
,
987 struct vhost_vring_addr
*addr
)
989 trace_vhost_vdpa_set_vring_addr(dev
, addr
->index
, addr
->flags
,
990 addr
->desc_user_addr
, addr
->used_user_addr
,
991 addr
->avail_user_addr
,
992 addr
->log_guest_addr
);
994 return vhost_vdpa_call(dev
, VHOST_SET_VRING_ADDR
, addr
);
999 * Set the shadow virtqueue descriptors to the device
1001 * @dev: The vhost device model
1002 * @svq: The shadow virtqueue
1003 * @idx: The index of the virtqueue in the vhost device
1006 * Note that this function does not rewind kick file descriptor if cannot set
1009 static int vhost_vdpa_svq_set_fds(struct vhost_dev
*dev
,
1010 VhostShadowVirtqueue
*svq
, unsigned idx
,
1013 struct vhost_vring_file file
= {
1014 .index
= dev
->vq_index
+ idx
,
1016 const EventNotifier
*event_notifier
= &svq
->hdev_kick
;
1019 r
= event_notifier_init(&svq
->hdev_kick
, 0);
1021 error_setg_errno(errp
, -r
, "Couldn't create kick event notifier");
1022 goto err_init_hdev_kick
;
1025 r
= event_notifier_init(&svq
->hdev_call
, 0);
1027 error_setg_errno(errp
, -r
, "Couldn't create call event notifier");
1028 goto err_init_hdev_call
;
1031 file
.fd
= event_notifier_get_fd(event_notifier
);
1032 r
= vhost_vdpa_set_vring_dev_kick(dev
, &file
);
1033 if (unlikely(r
!= 0)) {
1034 error_setg_errno(errp
, -r
, "Can't set device kick fd");
1035 goto err_init_set_dev_fd
;
1038 event_notifier
= &svq
->hdev_call
;
1039 file
.fd
= event_notifier_get_fd(event_notifier
);
1040 r
= vhost_vdpa_set_vring_dev_call(dev
, &file
);
1041 if (unlikely(r
!= 0)) {
1042 error_setg_errno(errp
, -r
, "Can't set device call fd");
1043 goto err_init_set_dev_fd
;
1048 err_init_set_dev_fd
:
1049 event_notifier_set_handler(&svq
->hdev_call
, NULL
);
1052 event_notifier_cleanup(&svq
->hdev_kick
);
1059 * Unmap a SVQ area in the device
1061 static void vhost_vdpa_svq_unmap_ring(struct vhost_vdpa
*v
, hwaddr addr
)
1063 const DMAMap needle
= {
1064 .translated_addr
= addr
,
1066 const DMAMap
*result
= vhost_iova_tree_find_iova(v
->iova_tree
, &needle
);
1070 if (unlikely(!result
)) {
1071 error_report("Unable to find SVQ address to unmap");
1075 size
= ROUND_UP(result
->size
, qemu_real_host_page_size());
1076 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, result
->iova
, size
);
1077 if (unlikely(r
< 0)) {
1078 error_report("Unable to unmap SVQ vring: %s (%d)", g_strerror(-r
), -r
);
1082 vhost_iova_tree_remove(v
->iova_tree
, *result
);
1085 static void vhost_vdpa_svq_unmap_rings(struct vhost_dev
*dev
,
1086 const VhostShadowVirtqueue
*svq
)
1088 struct vhost_vdpa
*v
= dev
->opaque
;
1089 struct vhost_vring_addr svq_addr
;
1091 vhost_svq_get_vring_addr(svq
, &svq_addr
);
1093 vhost_vdpa_svq_unmap_ring(v
, svq_addr
.desc_user_addr
);
1095 vhost_vdpa_svq_unmap_ring(v
, svq_addr
.used_user_addr
);
1099 * Map the SVQ area in the device
1101 * @v: Vhost-vdpa device
1102 * @needle: The area to search iova
1103 * @errorp: Error pointer
1105 static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa
*v
, DMAMap
*needle
,
1110 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, needle
);
1111 if (unlikely(r
!= IOVA_OK
)) {
1112 error_setg(errp
, "Cannot allocate iova (%d)", r
);
1116 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, needle
->iova
,
1118 (void *)(uintptr_t)needle
->translated_addr
,
1119 needle
->perm
== IOMMU_RO
);
1120 if (unlikely(r
!= 0)) {
1121 error_setg_errno(errp
, -r
, "Cannot map region to device");
1122 vhost_iova_tree_remove(v
->iova_tree
, *needle
);
1129 * Map the shadow virtqueue rings in the device
1131 * @dev: The vhost device
1132 * @svq: The shadow virtqueue
1133 * @addr: Assigned IOVA addresses
1134 * @errp: Error pointer
1136 static bool vhost_vdpa_svq_map_rings(struct vhost_dev
*dev
,
1137 const VhostShadowVirtqueue
*svq
,
1138 struct vhost_vring_addr
*addr
,
1142 DMAMap device_region
, driver_region
;
1143 struct vhost_vring_addr svq_addr
;
1144 struct vhost_vdpa
*v
= dev
->opaque
;
1145 size_t device_size
= vhost_svq_device_area_size(svq
);
1146 size_t driver_size
= vhost_svq_driver_area_size(svq
);
1147 size_t avail_offset
;
1150 vhost_svq_get_vring_addr(svq
, &svq_addr
);
1152 driver_region
= (DMAMap
) {
1153 .translated_addr
= svq_addr
.desc_user_addr
,
1154 .size
= driver_size
- 1,
1157 ok
= vhost_vdpa_svq_map_ring(v
, &driver_region
, errp
);
1158 if (unlikely(!ok
)) {
1159 error_prepend(errp
, "Cannot create vq driver region: ");
1162 addr
->desc_user_addr
= driver_region
.iova
;
1163 avail_offset
= svq_addr
.avail_user_addr
- svq_addr
.desc_user_addr
;
1164 addr
->avail_user_addr
= driver_region
.iova
+ avail_offset
;
1166 device_region
= (DMAMap
) {
1167 .translated_addr
= svq_addr
.used_user_addr
,
1168 .size
= device_size
- 1,
1171 ok
= vhost_vdpa_svq_map_ring(v
, &device_region
, errp
);
1172 if (unlikely(!ok
)) {
1173 error_prepend(errp
, "Cannot create vq device region: ");
1174 vhost_vdpa_svq_unmap_ring(v
, driver_region
.translated_addr
);
1176 addr
->used_user_addr
= device_region
.iova
;
1181 static bool vhost_vdpa_svq_setup(struct vhost_dev
*dev
,
1182 VhostShadowVirtqueue
*svq
, unsigned idx
,
1185 uint16_t vq_index
= dev
->vq_index
+ idx
;
1186 struct vhost_vring_state s
= {
1191 r
= vhost_vdpa_set_dev_vring_base(dev
, &s
);
1193 error_setg_errno(errp
, -r
, "Cannot set vring base");
1197 r
= vhost_vdpa_svq_set_fds(dev
, svq
, idx
, errp
);
1201 static bool vhost_vdpa_svqs_start(struct vhost_dev
*dev
)
1203 struct vhost_vdpa
*v
= dev
->opaque
;
1207 if (!v
->shadow_vqs_enabled
) {
1211 for (i
= 0; i
< v
->shadow_vqs
->len
; ++i
) {
1212 VirtQueue
*vq
= virtio_get_queue(dev
->vdev
, dev
->vq_index
+ i
);
1213 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, i
);
1214 struct vhost_vring_addr addr
= {
1215 .index
= dev
->vq_index
+ i
,
1218 bool ok
= vhost_vdpa_svq_setup(dev
, svq
, i
, &err
);
1219 if (unlikely(!ok
)) {
1223 vhost_svq_start(svq
, dev
->vdev
, vq
, v
->iova_tree
);
1224 ok
= vhost_vdpa_svq_map_rings(dev
, svq
, &addr
, &err
);
1225 if (unlikely(!ok
)) {
1229 /* Override vring GPA set by vhost subsystem */
1230 r
= vhost_vdpa_set_vring_dev_addr(dev
, &addr
);
1231 if (unlikely(r
!= 0)) {
1232 error_setg_errno(&err
, -r
, "Cannot set device address");
1240 vhost_vdpa_svq_unmap_rings(dev
, g_ptr_array_index(v
->shadow_vqs
, i
));
1243 vhost_svq_stop(g_ptr_array_index(v
->shadow_vqs
, i
));
1246 error_reportf_err(err
, "Cannot setup SVQ %u: ", i
);
1247 for (unsigned j
= 0; j
< i
; ++j
) {
1248 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, j
);
1249 vhost_vdpa_svq_unmap_rings(dev
, svq
);
1250 vhost_svq_stop(svq
);
1256 static void vhost_vdpa_svqs_stop(struct vhost_dev
*dev
)
1258 struct vhost_vdpa
*v
= dev
->opaque
;
1260 if (!v
->shadow_vqs_enabled
) {
1264 for (unsigned i
= 0; i
< v
->shadow_vqs
->len
; ++i
) {
1265 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, i
);
1267 vhost_svq_stop(svq
);
1268 vhost_vdpa_svq_unmap_rings(dev
, svq
);
1270 event_notifier_cleanup(&svq
->hdev_kick
);
1271 event_notifier_cleanup(&svq
->hdev_call
);
1275 static void vhost_vdpa_suspend(struct vhost_dev
*dev
)
1277 struct vhost_vdpa
*v
= dev
->opaque
;
1280 if (!vhost_vdpa_first_dev(dev
)) {
1284 if (dev
->backend_cap
& BIT_ULL(VHOST_BACKEND_F_SUSPEND
)) {
1285 trace_vhost_vdpa_suspend(dev
);
1286 r
= ioctl(v
->device_fd
, VHOST_VDPA_SUSPEND
);
1288 error_report("Cannot suspend: %s(%d)", g_strerror(errno
), errno
);
1290 v
->suspended
= true;
1295 vhost_vdpa_reset_device(dev
);
1298 static int vhost_vdpa_dev_start(struct vhost_dev
*dev
, bool started
)
1300 struct vhost_vdpa
*v
= dev
->opaque
;
1302 trace_vhost_vdpa_dev_start(dev
, started
);
1305 vhost_vdpa_host_notifiers_init(dev
);
1306 ok
= vhost_vdpa_svqs_start(dev
);
1307 if (unlikely(!ok
)) {
1311 vhost_vdpa_suspend(dev
);
1312 vhost_vdpa_svqs_stop(dev
);
1313 vhost_vdpa_host_notifiers_uninit(dev
, dev
->nvqs
);
1316 if (dev
->vq_index
+ dev
->nvqs
!= dev
->vq_index_end
) {
1321 if (vhost_dev_has_iommu(dev
) && (v
->shadow_vqs_enabled
)) {
1322 error_report("SVQ can not work while IOMMU enable, please disable"
1323 "IOMMU and try again");
1326 memory_listener_register(&v
->listener
, dev
->vdev
->dma_as
);
1328 return vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_DRIVER_OK
);
1334 static void vhost_vdpa_reset_status(struct vhost_dev
*dev
)
1336 struct vhost_vdpa
*v
= dev
->opaque
;
1338 if (dev
->vq_index
+ dev
->nvqs
!= dev
->vq_index_end
) {
1342 vhost_vdpa_reset_device(dev
);
1343 vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_ACKNOWLEDGE
|
1344 VIRTIO_CONFIG_S_DRIVER
);
1345 memory_listener_unregister(&v
->listener
);
1348 static int vhost_vdpa_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
1349 struct vhost_log
*log
)
1351 struct vhost_vdpa
*v
= dev
->opaque
;
1352 if (v
->shadow_vqs_enabled
|| !vhost_vdpa_first_dev(dev
)) {
1356 trace_vhost_vdpa_set_log_base(dev
, base
, log
->size
, log
->refcnt
, log
->fd
,
1358 return vhost_vdpa_call(dev
, VHOST_SET_LOG_BASE
, &base
);
1361 static int vhost_vdpa_set_vring_addr(struct vhost_dev
*dev
,
1362 struct vhost_vring_addr
*addr
)
1364 struct vhost_vdpa
*v
= dev
->opaque
;
1366 if (v
->shadow_vqs_enabled
) {
1368 * Device vring addr was set at device start. SVQ base is handled by
1374 return vhost_vdpa_set_vring_dev_addr(dev
, addr
);
1377 static int vhost_vdpa_set_vring_num(struct vhost_dev
*dev
,
1378 struct vhost_vring_state
*ring
)
1380 trace_vhost_vdpa_set_vring_num(dev
, ring
->index
, ring
->num
);
1381 return vhost_vdpa_call(dev
, VHOST_SET_VRING_NUM
, ring
);
1384 static int vhost_vdpa_set_vring_base(struct vhost_dev
*dev
,
1385 struct vhost_vring_state
*ring
)
1387 struct vhost_vdpa
*v
= dev
->opaque
;
1389 if (v
->shadow_vqs_enabled
) {
1391 * Device vring base was set at device start. SVQ base is handled by
1397 return vhost_vdpa_set_dev_vring_base(dev
, ring
);
1400 static int vhost_vdpa_get_vring_base(struct vhost_dev
*dev
,
1401 struct vhost_vring_state
*ring
)
1403 struct vhost_vdpa
*v
= dev
->opaque
;
1406 if (v
->shadow_vqs_enabled
) {
1407 ring
->num
= virtio_queue_get_last_avail_idx(dev
->vdev
, ring
->index
);
1411 if (!v
->suspended
) {
1413 * Cannot trust in value returned by device, let vhost recover used
1419 ret
= vhost_vdpa_call(dev
, VHOST_GET_VRING_BASE
, ring
);
1420 trace_vhost_vdpa_get_vring_base(dev
, ring
->index
, ring
->num
);
1424 static int vhost_vdpa_set_vring_kick(struct vhost_dev
*dev
,
1425 struct vhost_vring_file
*file
)
1427 struct vhost_vdpa
*v
= dev
->opaque
;
1428 int vdpa_idx
= file
->index
- dev
->vq_index
;
1430 if (v
->shadow_vqs_enabled
) {
1431 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, vdpa_idx
);
1432 vhost_svq_set_svq_kick_fd(svq
, file
->fd
);
1435 return vhost_vdpa_set_vring_dev_kick(dev
, file
);
1439 static int vhost_vdpa_set_vring_call(struct vhost_dev
*dev
,
1440 struct vhost_vring_file
*file
)
1442 struct vhost_vdpa
*v
= dev
->opaque
;
1443 int vdpa_idx
= file
->index
- dev
->vq_index
;
1444 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, vdpa_idx
);
1446 /* Remember last call fd because we can switch to SVQ anytime. */
1447 vhost_svq_set_svq_call_fd(svq
, file
->fd
);
1448 if (v
->shadow_vqs_enabled
) {
1452 return vhost_vdpa_set_vring_dev_call(dev
, file
);
1455 static int vhost_vdpa_get_features(struct vhost_dev
*dev
,
1458 int ret
= vhost_vdpa_get_dev_features(dev
, features
);
1461 /* Add SVQ logging capabilities */
1462 *features
|= BIT_ULL(VHOST_F_LOG_ALL
);
1468 static int vhost_vdpa_set_owner(struct vhost_dev
*dev
)
1470 if (!vhost_vdpa_first_dev(dev
)) {
1474 trace_vhost_vdpa_set_owner(dev
);
1475 return vhost_vdpa_call(dev
, VHOST_SET_OWNER
, NULL
);
1478 static int vhost_vdpa_vq_get_addr(struct vhost_dev
*dev
,
1479 struct vhost_vring_addr
*addr
, struct vhost_virtqueue
*vq
)
1481 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
1482 addr
->desc_user_addr
= (uint64_t)(unsigned long)vq
->desc_phys
;
1483 addr
->avail_user_addr
= (uint64_t)(unsigned long)vq
->avail_phys
;
1484 addr
->used_user_addr
= (uint64_t)(unsigned long)vq
->used_phys
;
1485 trace_vhost_vdpa_vq_get_addr(dev
, vq
, addr
->desc_user_addr
,
1486 addr
->avail_user_addr
, addr
->used_user_addr
);
1490 static bool vhost_vdpa_force_iommu(struct vhost_dev
*dev
)
1495 const VhostOps vdpa_ops
= {
1496 .backend_type
= VHOST_BACKEND_TYPE_VDPA
,
1497 .vhost_backend_init
= vhost_vdpa_init
,
1498 .vhost_backend_cleanup
= vhost_vdpa_cleanup
,
1499 .vhost_set_log_base
= vhost_vdpa_set_log_base
,
1500 .vhost_set_vring_addr
= vhost_vdpa_set_vring_addr
,
1501 .vhost_set_vring_num
= vhost_vdpa_set_vring_num
,
1502 .vhost_set_vring_base
= vhost_vdpa_set_vring_base
,
1503 .vhost_get_vring_base
= vhost_vdpa_get_vring_base
,
1504 .vhost_set_vring_kick
= vhost_vdpa_set_vring_kick
,
1505 .vhost_set_vring_call
= vhost_vdpa_set_vring_call
,
1506 .vhost_get_features
= vhost_vdpa_get_features
,
1507 .vhost_set_backend_cap
= vhost_vdpa_set_backend_cap
,
1508 .vhost_set_owner
= vhost_vdpa_set_owner
,
1509 .vhost_set_vring_endian
= NULL
,
1510 .vhost_backend_memslots_limit
= vhost_vdpa_memslots_limit
,
1511 .vhost_set_mem_table
= vhost_vdpa_set_mem_table
,
1512 .vhost_set_features
= vhost_vdpa_set_features
,
1513 .vhost_reset_device
= vhost_vdpa_reset_device
,
1514 .vhost_get_vq_index
= vhost_vdpa_get_vq_index
,
1515 .vhost_get_config
= vhost_vdpa_get_config
,
1516 .vhost_set_config
= vhost_vdpa_set_config
,
1517 .vhost_requires_shm_log
= NULL
,
1518 .vhost_migration_done
= NULL
,
1519 .vhost_net_set_mtu
= NULL
,
1520 .vhost_set_iotlb_callback
= NULL
,
1521 .vhost_send_device_iotlb_msg
= NULL
,
1522 .vhost_dev_start
= vhost_vdpa_dev_start
,
1523 .vhost_get_device_id
= vhost_vdpa_get_device_id
,
1524 .vhost_vq_get_addr
= vhost_vdpa_vq_get_addr
,
1525 .vhost_force_iommu
= vhost_vdpa_force_iommu
,
1526 .vhost_set_config_call
= vhost_vdpa_set_config_call
,
1527 .vhost_reset_status
= vhost_vdpa_reset_status
,