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 "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"
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
)
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
));
43 static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection
*section
,
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
)) {
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
);
64 * While using vIOMMU, sometimes the section will be larger than iova_max,
65 * but the memory that actually maps is smaller, so move the check to
66 * function vhost_vdpa_iommu_map_notify(). That function will use the actual
67 * size that maps to the kernel
70 if (!memory_region_is_iommu(section
->mr
)) {
71 llend
= vhost_vdpa_section_end(section
);
72 if (int128_gt(llend
, int128_make64(iova_max
))) {
73 error_report("RAM section out of device range (max=0x%" PRIx64
74 ", end addr=0x%" PRIx64
")",
75 iova_max
, int128_get64(llend
));
84 * The caller must set asid = 0 if the device does not support asid.
85 * This is not an ABI break since it is set to 0 by the initializer anyway.
87 int vhost_vdpa_dma_map(struct vhost_vdpa
*v
, uint32_t asid
, hwaddr iova
,
88 hwaddr size
, void *vaddr
, bool readonly
)
90 struct vhost_msg_v2 msg
= {};
91 int fd
= v
->device_fd
;
94 msg
.type
= v
->msg_type
;
96 msg
.iotlb
.iova
= iova
;
97 msg
.iotlb
.size
= size
;
98 msg
.iotlb
.uaddr
= (uint64_t)(uintptr_t)vaddr
;
99 msg
.iotlb
.perm
= readonly
? VHOST_ACCESS_RO
: VHOST_ACCESS_RW
;
100 msg
.iotlb
.type
= VHOST_IOTLB_UPDATE
;
102 trace_vhost_vdpa_dma_map(v
, fd
, msg
.type
, msg
.asid
, msg
.iotlb
.iova
,
103 msg
.iotlb
.size
, msg
.iotlb
.uaddr
, msg
.iotlb
.perm
,
106 if (write(fd
, &msg
, sizeof(msg
)) != sizeof(msg
)) {
107 error_report("failed to write, fd=%d, errno=%d (%s)",
108 fd
, errno
, strerror(errno
));
116 * The caller must set asid = 0 if the device does not support asid.
117 * This is not an ABI break since it is set to 0 by the initializer anyway.
119 int vhost_vdpa_dma_unmap(struct vhost_vdpa
*v
, uint32_t asid
, hwaddr iova
,
122 struct vhost_msg_v2 msg
= {};
123 int fd
= v
->device_fd
;
126 msg
.type
= v
->msg_type
;
128 msg
.iotlb
.iova
= iova
;
129 msg
.iotlb
.size
= size
;
130 msg
.iotlb
.type
= VHOST_IOTLB_INVALIDATE
;
132 trace_vhost_vdpa_dma_unmap(v
, fd
, msg
.type
, msg
.asid
, msg
.iotlb
.iova
,
133 msg
.iotlb
.size
, msg
.iotlb
.type
);
135 if (write(fd
, &msg
, sizeof(msg
)) != sizeof(msg
)) {
136 error_report("failed to write, fd=%d, errno=%d (%s)",
137 fd
, errno
, strerror(errno
));
144 static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa
*v
)
146 int fd
= v
->device_fd
;
147 struct vhost_msg_v2 msg
= {
149 .iotlb
.type
= VHOST_IOTLB_BATCH_BEGIN
,
152 trace_vhost_vdpa_listener_begin_batch(v
, fd
, msg
.type
, msg
.iotlb
.type
);
153 if (write(fd
, &msg
, sizeof(msg
)) != sizeof(msg
)) {
154 error_report("failed to write, fd=%d, errno=%d (%s)",
155 fd
, errno
, strerror(errno
));
159 static void vhost_vdpa_iotlb_batch_begin_once(struct vhost_vdpa
*v
)
161 if (v
->dev
->backend_cap
& (0x1ULL
<< VHOST_BACKEND_F_IOTLB_BATCH
) &&
162 !v
->iotlb_batch_begin_sent
) {
163 vhost_vdpa_listener_begin_batch(v
);
166 v
->iotlb_batch_begin_sent
= true;
169 static void vhost_vdpa_listener_commit(MemoryListener
*listener
)
171 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
172 struct vhost_dev
*dev
= v
->dev
;
173 struct vhost_msg_v2 msg
= {};
174 int fd
= v
->device_fd
;
176 if (!(dev
->backend_cap
& (0x1ULL
<< VHOST_BACKEND_F_IOTLB_BATCH
))) {
180 if (!v
->iotlb_batch_begin_sent
) {
184 msg
.type
= v
->msg_type
;
185 msg
.iotlb
.type
= VHOST_IOTLB_BATCH_END
;
187 trace_vhost_vdpa_listener_commit(v
, fd
, msg
.type
, msg
.iotlb
.type
);
188 if (write(fd
, &msg
, sizeof(msg
)) != sizeof(msg
)) {
189 error_report("failed to write, fd=%d, errno=%d (%s)",
190 fd
, errno
, strerror(errno
));
193 v
->iotlb_batch_begin_sent
= false;
196 static void vhost_vdpa_iommu_map_notify(IOMMUNotifier
*n
, IOMMUTLBEntry
*iotlb
)
198 struct vdpa_iommu
*iommu
= container_of(n
, struct vdpa_iommu
, n
);
200 hwaddr iova
= iotlb
->iova
+ iommu
->iommu_offset
;
201 struct vhost_vdpa
*v
= iommu
->dev
;
206 if (iotlb
->target_as
!= &address_space_memory
) {
207 error_report("Wrong target AS \"%s\", only system memory is allowed",
208 iotlb
->target_as
->name
? iotlb
->target_as
->name
: "none");
211 RCU_READ_LOCK_GUARD();
212 /* check if RAM section out of device range */
213 llend
= int128_add(int128_makes64(iotlb
->addr_mask
), int128_makes64(iova
));
214 if (int128_gt(llend
, int128_make64(v
->iova_range
.last
))) {
215 error_report("RAM section out of device range (max=0x%" PRIx64
216 ", end addr=0x%" PRIx64
")",
217 v
->iova_range
.last
, int128_get64(llend
));
221 if ((iotlb
->perm
& IOMMU_RW
) != IOMMU_NONE
) {
224 if (!memory_get_xlat_addr(iotlb
, &vaddr
, NULL
, &read_only
, NULL
)) {
227 ret
= vhost_vdpa_dma_map(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
228 iotlb
->addr_mask
+ 1, vaddr
, read_only
);
230 error_report("vhost_vdpa_dma_map(%p, 0x%" HWADDR_PRIx
", "
231 "0x%" HWADDR_PRIx
", %p) = %d (%m)",
232 v
, iova
, iotlb
->addr_mask
+ 1, vaddr
, ret
);
235 ret
= vhost_vdpa_dma_unmap(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
236 iotlb
->addr_mask
+ 1);
238 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx
", "
239 "0x%" HWADDR_PRIx
") = %d (%m)",
240 v
, iova
, iotlb
->addr_mask
+ 1, ret
);
245 static void vhost_vdpa_iommu_region_add(MemoryListener
*listener
,
246 MemoryRegionSection
*section
)
248 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
250 struct vdpa_iommu
*iommu
;
253 IOMMUMemoryRegion
*iommu_mr
;
256 iommu_mr
= IOMMU_MEMORY_REGION(section
->mr
);
258 iommu
= g_malloc0(sizeof(*iommu
));
259 end
= int128_add(int128_make64(section
->offset_within_region
),
261 end
= int128_sub(end
, int128_one());
262 iommu_idx
= memory_region_iommu_attrs_to_index(iommu_mr
,
263 MEMTXATTRS_UNSPECIFIED
);
264 iommu
->iommu_mr
= iommu_mr
;
265 iommu_notifier_init(&iommu
->n
, vhost_vdpa_iommu_map_notify
,
266 IOMMU_NOTIFIER_IOTLB_EVENTS
,
267 section
->offset_within_region
,
270 iommu
->iommu_offset
= section
->offset_within_address_space
-
271 section
->offset_within_region
;
274 ret
= memory_region_register_iommu_notifier(section
->mr
, &iommu
->n
, NULL
);
280 QLIST_INSERT_HEAD(&v
->iommu_list
, iommu
, iommu_next
);
281 memory_region_iommu_replay(iommu
->iommu_mr
, &iommu
->n
);
286 static void vhost_vdpa_iommu_region_del(MemoryListener
*listener
,
287 MemoryRegionSection
*section
)
289 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
291 struct vdpa_iommu
*iommu
;
293 QLIST_FOREACH(iommu
, &v
->iommu_list
, iommu_next
)
295 if (MEMORY_REGION(iommu
->iommu_mr
) == section
->mr
&&
296 iommu
->n
.start
== section
->offset_within_region
) {
297 memory_region_unregister_iommu_notifier(section
->mr
, &iommu
->n
);
298 QLIST_REMOVE(iommu
, iommu_next
);
305 static void vhost_vdpa_listener_region_add(MemoryListener
*listener
,
306 MemoryRegionSection
*section
)
308 DMAMap mem_region
= {};
309 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
311 Int128 llend
, llsize
;
315 if (vhost_vdpa_listener_skipped_section(section
, v
->iova_range
.first
,
316 v
->iova_range
.last
)) {
319 if (memory_region_is_iommu(section
->mr
)) {
320 vhost_vdpa_iommu_region_add(listener
, section
);
324 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
325 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
326 trace_vhost_vdpa_listener_region_add_unaligned(v
, section
->mr
->name
,
327 section
->offset_within_address_space
& ~TARGET_PAGE_MASK
,
328 section
->offset_within_region
& ~TARGET_PAGE_MASK
);
332 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
333 llend
= vhost_vdpa_section_end(section
);
334 if (int128_ge(int128_make64(iova
), llend
)) {
338 memory_region_ref(section
->mr
);
340 /* Here we assume that memory_region_is_ram(section->mr)==true */
342 vaddr
= memory_region_get_ram_ptr(section
->mr
) +
343 section
->offset_within_region
+
344 (iova
- section
->offset_within_address_space
);
346 trace_vhost_vdpa_listener_region_add(v
, iova
, int128_get64(llend
),
347 vaddr
, section
->readonly
);
349 llsize
= int128_sub(llend
, int128_make64(iova
));
350 if (v
->shadow_data
) {
353 mem_region
.translated_addr
= (hwaddr
)(uintptr_t)vaddr
,
354 mem_region
.size
= int128_get64(llsize
) - 1,
355 mem_region
.perm
= IOMMU_ACCESS_FLAG(true, section
->readonly
),
357 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &mem_region
);
358 if (unlikely(r
!= IOVA_OK
)) {
359 error_report("Can't allocate a mapping (%d)", r
);
363 iova
= mem_region
.iova
;
366 vhost_vdpa_iotlb_batch_begin_once(v
);
367 ret
= vhost_vdpa_dma_map(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
368 int128_get64(llsize
), vaddr
, section
->readonly
);
370 error_report("vhost vdpa map fail!");
377 if (v
->shadow_data
) {
378 vhost_iova_tree_remove(v
->iova_tree
, mem_region
);
383 * On the initfn path, store the first error in the container so we
384 * can gracefully fail. Runtime, there's not much we can do other
385 * than throw a hardware error.
387 error_report("vhost-vdpa: DMA mapping failed, unable to continue");
392 static void vhost_vdpa_listener_region_del(MemoryListener
*listener
,
393 MemoryRegionSection
*section
)
395 struct vhost_vdpa
*v
= container_of(listener
, struct vhost_vdpa
, listener
);
397 Int128 llend
, llsize
;
400 if (vhost_vdpa_listener_skipped_section(section
, v
->iova_range
.first
,
401 v
->iova_range
.last
)) {
404 if (memory_region_is_iommu(section
->mr
)) {
405 vhost_vdpa_iommu_region_del(listener
, section
);
408 if (unlikely((section
->offset_within_address_space
& ~TARGET_PAGE_MASK
) !=
409 (section
->offset_within_region
& ~TARGET_PAGE_MASK
))) {
410 trace_vhost_vdpa_listener_region_del_unaligned(v
, section
->mr
->name
,
411 section
->offset_within_address_space
& ~TARGET_PAGE_MASK
,
412 section
->offset_within_region
& ~TARGET_PAGE_MASK
);
416 iova
= TARGET_PAGE_ALIGN(section
->offset_within_address_space
);
417 llend
= vhost_vdpa_section_end(section
);
419 trace_vhost_vdpa_listener_region_del(v
, iova
,
420 int128_get64(int128_sub(llend
, int128_one())));
422 if (int128_ge(int128_make64(iova
), llend
)) {
426 llsize
= int128_sub(llend
, int128_make64(iova
));
428 if (v
->shadow_data
) {
429 const DMAMap
*result
;
430 const void *vaddr
= memory_region_get_ram_ptr(section
->mr
) +
431 section
->offset_within_region
+
432 (iova
- section
->offset_within_address_space
);
433 DMAMap mem_region
= {
434 .translated_addr
= (hwaddr
)(uintptr_t)vaddr
,
435 .size
= int128_get64(llsize
) - 1,
438 result
= vhost_iova_tree_find_iova(v
->iova_tree
, &mem_region
);
440 /* The memory listener map wasn't mapped */
444 vhost_iova_tree_remove(v
->iova_tree
, *result
);
446 vhost_vdpa_iotlb_batch_begin_once(v
);
448 * The unmap ioctl doesn't accept a full 64-bit. need to check it
450 if (int128_eq(llsize
, int128_2_64())) {
451 llsize
= int128_rshift(llsize
, 1);
452 ret
= vhost_vdpa_dma_unmap(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
453 int128_get64(llsize
));
456 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx
", "
457 "0x%" HWADDR_PRIx
") = %d (%m)",
458 v
, iova
, int128_get64(llsize
), ret
);
460 iova
+= int128_get64(llsize
);
462 ret
= vhost_vdpa_dma_unmap(v
, VHOST_VDPA_GUEST_PA_ASID
, iova
,
463 int128_get64(llsize
));
466 error_report("vhost_vdpa_dma_unmap(%p, 0x%" HWADDR_PRIx
", "
467 "0x%" HWADDR_PRIx
") = %d (%m)",
468 v
, iova
, int128_get64(llsize
), ret
);
471 memory_region_unref(section
->mr
);
474 * IOTLB API is used by vhost-vdpa which requires incremental updating
475 * of the mapping. So we can not use generic vhost memory listener which
476 * depends on the addnop().
478 static const MemoryListener vhost_vdpa_memory_listener
= {
479 .name
= "vhost-vdpa",
480 .commit
= vhost_vdpa_listener_commit
,
481 .region_add
= vhost_vdpa_listener_region_add
,
482 .region_del
= vhost_vdpa_listener_region_del
,
485 static int vhost_vdpa_call(struct vhost_dev
*dev
, unsigned long int request
,
488 struct vhost_vdpa
*v
= dev
->opaque
;
489 int fd
= v
->device_fd
;
492 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
494 ret
= ioctl(fd
, request
, arg
);
495 return ret
< 0 ? -errno
: ret
;
498 static int vhost_vdpa_add_status(struct vhost_dev
*dev
, uint8_t status
)
503 trace_vhost_vdpa_add_status(dev
, status
);
504 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_STATUS
, &s
);
511 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_SET_STATUS
, &s
);
516 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_STATUS
, &s
);
528 int vhost_vdpa_get_iova_range(int fd
, struct vhost_vdpa_iova_range
*iova_range
)
530 int ret
= ioctl(fd
, VHOST_VDPA_GET_IOVA_RANGE
, iova_range
);
532 return ret
< 0 ? -errno
: 0;
536 * The use of this function is for requests that only need to be
537 * applied once. Typically such request occurs at the beginning
538 * of operation, and before setting up queues. It should not be
539 * used for request that performs operation until all queues are
540 * set, which would need to check dev->vq_index_end instead.
542 static bool vhost_vdpa_first_dev(struct vhost_dev
*dev
)
544 struct vhost_vdpa
*v
= dev
->opaque
;
546 return v
->index
== 0;
549 static int vhost_vdpa_get_dev_features(struct vhost_dev
*dev
,
554 ret
= vhost_vdpa_call(dev
, VHOST_GET_FEATURES
, features
);
555 trace_vhost_vdpa_get_features(dev
, *features
);
559 static void vhost_vdpa_init_svq(struct vhost_dev
*hdev
, struct vhost_vdpa
*v
)
561 g_autoptr(GPtrArray
) shadow_vqs
= NULL
;
563 shadow_vqs
= g_ptr_array_new_full(hdev
->nvqs
, vhost_svq_free
);
564 for (unsigned n
= 0; n
< hdev
->nvqs
; ++n
) {
565 VhostShadowVirtqueue
*svq
;
567 svq
= vhost_svq_new(v
->shadow_vq_ops
, v
->shadow_vq_ops_opaque
);
568 g_ptr_array_add(shadow_vqs
, svq
);
571 v
->shadow_vqs
= g_steal_pointer(&shadow_vqs
);
574 static int vhost_vdpa_init(struct vhost_dev
*dev
, void *opaque
, Error
**errp
)
576 struct vhost_vdpa
*v
;
577 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
578 trace_vhost_vdpa_init(dev
, opaque
);
583 dev
->opaque
= opaque
;
584 v
->listener
= vhost_vdpa_memory_listener
;
585 v
->msg_type
= VHOST_IOTLB_MSG_V2
;
586 vhost_vdpa_init_svq(dev
, v
);
588 error_propagate(&dev
->migration_blocker
, v
->migration_blocker
);
589 if (!vhost_vdpa_first_dev(dev
)) {
594 * If dev->shadow_vqs_enabled at initialization that means the device has
595 * been started with x-svq=on, so don't block migration
597 if (dev
->migration_blocker
== NULL
&& !v
->shadow_vqs_enabled
) {
598 /* We don't have dev->features yet */
600 ret
= vhost_vdpa_get_dev_features(dev
, &features
);
602 error_setg_errno(errp
, -ret
, "Could not get device features");
605 vhost_svq_valid_features(features
, &dev
->migration_blocker
);
609 * Similar to VFIO, we end up pinning all guest memory and have to
610 * disable discarding of RAM.
612 ret
= ram_block_discard_disable(true);
614 error_report("Cannot set discarding of RAM broken");
618 vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_ACKNOWLEDGE
|
619 VIRTIO_CONFIG_S_DRIVER
);
624 static void vhost_vdpa_host_notifier_uninit(struct vhost_dev
*dev
,
627 size_t page_size
= qemu_real_host_page_size();
628 struct vhost_vdpa
*v
= dev
->opaque
;
629 VirtIODevice
*vdev
= dev
->vdev
;
630 VhostVDPAHostNotifier
*n
;
632 n
= &v
->notifier
[queue_index
];
635 virtio_queue_set_host_notifier_mr(vdev
, queue_index
, &n
->mr
, false);
636 object_unparent(OBJECT(&n
->mr
));
637 munmap(n
->addr
, page_size
);
642 static int vhost_vdpa_host_notifier_init(struct vhost_dev
*dev
, int queue_index
)
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 int fd
= v
->device_fd
;
652 vhost_vdpa_host_notifier_uninit(dev
, queue_index
);
654 n
= &v
->notifier
[queue_index
];
656 addr
= mmap(NULL
, page_size
, PROT_WRITE
, MAP_SHARED
, fd
,
657 queue_index
* page_size
);
658 if (addr
== MAP_FAILED
) {
662 name
= g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]",
664 memory_region_init_ram_device_ptr(&n
->mr
, OBJECT(vdev
), name
,
668 if (virtio_queue_set_host_notifier_mr(vdev
, queue_index
, &n
->mr
, true)) {
669 object_unparent(OBJECT(&n
->mr
));
670 munmap(addr
, page_size
);
681 static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev
*dev
, int n
)
686 * Pack all the changes to the memory regions in a single
687 * transaction to avoid a few updating of the address space
690 memory_region_transaction_begin();
692 for (i
= dev
->vq_index
; i
< dev
->vq_index
+ n
; i
++) {
693 vhost_vdpa_host_notifier_uninit(dev
, i
);
696 memory_region_transaction_commit();
699 static void vhost_vdpa_host_notifiers_init(struct vhost_dev
*dev
)
701 struct vhost_vdpa
*v
= dev
->opaque
;
704 if (v
->shadow_vqs_enabled
) {
705 /* FIXME SVQ is not compatible with host notifiers mr */
710 * Pack all the changes to the memory regions in a single
711 * transaction to avoid a few updating of the address space
714 memory_region_transaction_begin();
716 for (i
= dev
->vq_index
; i
< dev
->vq_index
+ dev
->nvqs
; i
++) {
717 if (vhost_vdpa_host_notifier_init(dev
, i
)) {
718 vhost_vdpa_host_notifiers_uninit(dev
, i
- dev
->vq_index
);
723 memory_region_transaction_commit();
726 static void vhost_vdpa_svq_cleanup(struct vhost_dev
*dev
)
728 struct vhost_vdpa
*v
= dev
->opaque
;
731 for (idx
= 0; idx
< v
->shadow_vqs
->len
; ++idx
) {
732 vhost_svq_stop(g_ptr_array_index(v
->shadow_vqs
, idx
));
734 g_ptr_array_free(v
->shadow_vqs
, true);
737 static int vhost_vdpa_cleanup(struct vhost_dev
*dev
)
739 struct vhost_vdpa
*v
;
740 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
742 trace_vhost_vdpa_cleanup(dev
, v
);
743 if (vhost_vdpa_first_dev(dev
)) {
744 ram_block_discard_disable(false);
747 vhost_vdpa_host_notifiers_uninit(dev
, dev
->nvqs
);
748 memory_listener_unregister(&v
->listener
);
749 vhost_vdpa_svq_cleanup(dev
);
756 static int vhost_vdpa_memslots_limit(struct vhost_dev
*dev
)
758 trace_vhost_vdpa_memslots_limit(dev
, INT_MAX
);
762 static int vhost_vdpa_set_mem_table(struct vhost_dev
*dev
,
763 struct vhost_memory
*mem
)
765 if (!vhost_vdpa_first_dev(dev
)) {
769 trace_vhost_vdpa_set_mem_table(dev
, mem
->nregions
, mem
->padding
);
770 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE
) &&
771 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS
)) {
773 for (i
= 0; i
< mem
->nregions
; i
++) {
774 trace_vhost_vdpa_dump_regions(dev
, i
,
775 mem
->regions
[i
].guest_phys_addr
,
776 mem
->regions
[i
].memory_size
,
777 mem
->regions
[i
].userspace_addr
,
778 mem
->regions
[i
].flags_padding
);
788 static int vhost_vdpa_set_features(struct vhost_dev
*dev
,
791 struct vhost_vdpa
*v
= dev
->opaque
;
794 if (!vhost_vdpa_first_dev(dev
)) {
798 if (v
->shadow_vqs_enabled
) {
799 if ((v
->acked_features
^ features
) == BIT_ULL(VHOST_F_LOG_ALL
)) {
801 * QEMU is just trying to enable or disable logging. SVQ handles
802 * this sepparately, so no need to forward this.
804 v
->acked_features
= features
;
808 v
->acked_features
= features
;
810 /* We must not ack _F_LOG if SVQ is enabled */
811 features
&= ~BIT_ULL(VHOST_F_LOG_ALL
);
814 trace_vhost_vdpa_set_features(dev
, features
);
815 ret
= vhost_vdpa_call(dev
, VHOST_SET_FEATURES
, &features
);
820 return vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_FEATURES_OK
);
823 static int vhost_vdpa_set_backend_cap(struct vhost_dev
*dev
)
826 uint64_t f
= 0x1ULL
<< VHOST_BACKEND_F_IOTLB_MSG_V2
|
827 0x1ULL
<< VHOST_BACKEND_F_IOTLB_BATCH
|
828 0x1ULL
<< VHOST_BACKEND_F_IOTLB_ASID
|
829 0x1ULL
<< VHOST_BACKEND_F_SUSPEND
;
832 if (vhost_vdpa_call(dev
, VHOST_GET_BACKEND_FEATURES
, &features
)) {
838 if (vhost_vdpa_first_dev(dev
)) {
839 r
= vhost_vdpa_call(dev
, VHOST_SET_BACKEND_FEATURES
, &features
);
845 dev
->backend_cap
= features
;
850 static int vhost_vdpa_get_device_id(struct vhost_dev
*dev
,
854 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_DEVICE_ID
, device_id
);
855 trace_vhost_vdpa_get_device_id(dev
, *device_id
);
859 static int vhost_vdpa_reset_device(struct vhost_dev
*dev
)
861 struct vhost_vdpa
*v
= dev
->opaque
;
865 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_SET_STATUS
, &status
);
866 trace_vhost_vdpa_reset_device(dev
);
867 v
->suspended
= false;
871 static int vhost_vdpa_get_vq_index(struct vhost_dev
*dev
, int idx
)
873 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
875 trace_vhost_vdpa_get_vq_index(dev
, idx
, idx
);
879 static int vhost_vdpa_set_vring_ready(struct vhost_dev
*dev
)
882 trace_vhost_vdpa_set_vring_ready(dev
);
883 for (i
= 0; i
< dev
->nvqs
; ++i
) {
884 struct vhost_vring_state state
= {
885 .index
= dev
->vq_index
+ i
,
888 vhost_vdpa_call(dev
, VHOST_VDPA_SET_VRING_ENABLE
, &state
);
893 static int vhost_vdpa_set_config_call(struct vhost_dev
*dev
,
896 trace_vhost_vdpa_set_config_call(dev
, fd
);
897 return vhost_vdpa_call(dev
, VHOST_VDPA_SET_CONFIG_CALL
, &fd
);
900 static void vhost_vdpa_dump_config(struct vhost_dev
*dev
, const uint8_t *config
,
904 char line
[QEMU_HEXDUMP_LINE_LEN
];
906 for (b
= 0; b
< config_len
; b
+= 16) {
907 len
= config_len
- b
;
908 qemu_hexdump_line(line
, b
, config
, len
, false);
909 trace_vhost_vdpa_dump_config(dev
, line
);
913 static int vhost_vdpa_set_config(struct vhost_dev
*dev
, const uint8_t *data
,
914 uint32_t offset
, uint32_t size
,
917 struct vhost_vdpa_config
*config
;
919 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
921 trace_vhost_vdpa_set_config(dev
, offset
, size
, flags
);
922 config
= g_malloc(size
+ config_size
);
923 config
->off
= offset
;
925 memcpy(config
->buf
, data
, size
);
926 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG
) &&
927 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG
)) {
928 vhost_vdpa_dump_config(dev
, data
, size
);
930 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_SET_CONFIG
, config
);
935 static int vhost_vdpa_get_config(struct vhost_dev
*dev
, uint8_t *config
,
936 uint32_t config_len
, Error
**errp
)
938 struct vhost_vdpa_config
*v_config
;
939 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
942 trace_vhost_vdpa_get_config(dev
, config
, config_len
);
943 v_config
= g_malloc(config_len
+ config_size
);
944 v_config
->len
= config_len
;
946 ret
= vhost_vdpa_call(dev
, VHOST_VDPA_GET_CONFIG
, v_config
);
947 memcpy(config
, v_config
->buf
, config_len
);
949 if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG
) &&
950 trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG
)) {
951 vhost_vdpa_dump_config(dev
, config
, config_len
);
956 static int vhost_vdpa_set_dev_vring_base(struct vhost_dev
*dev
,
957 struct vhost_vring_state
*ring
)
959 trace_vhost_vdpa_set_vring_base(dev
, ring
->index
, ring
->num
);
960 return vhost_vdpa_call(dev
, VHOST_SET_VRING_BASE
, ring
);
963 static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev
*dev
,
964 struct vhost_vring_file
*file
)
966 trace_vhost_vdpa_set_vring_kick(dev
, file
->index
, file
->fd
);
967 return vhost_vdpa_call(dev
, VHOST_SET_VRING_KICK
, file
);
970 static int vhost_vdpa_set_vring_dev_call(struct vhost_dev
*dev
,
971 struct vhost_vring_file
*file
)
973 trace_vhost_vdpa_set_vring_call(dev
, file
->index
, file
->fd
);
974 return vhost_vdpa_call(dev
, VHOST_SET_VRING_CALL
, file
);
977 static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev
*dev
,
978 struct vhost_vring_addr
*addr
)
980 trace_vhost_vdpa_set_vring_addr(dev
, addr
->index
, addr
->flags
,
981 addr
->desc_user_addr
, addr
->used_user_addr
,
982 addr
->avail_user_addr
,
983 addr
->log_guest_addr
);
985 return vhost_vdpa_call(dev
, VHOST_SET_VRING_ADDR
, addr
);
990 * Set the shadow virtqueue descriptors to the device
992 * @dev: The vhost device model
993 * @svq: The shadow virtqueue
994 * @idx: The index of the virtqueue in the vhost device
997 * Note that this function does not rewind kick file descriptor if cannot set
1000 static int vhost_vdpa_svq_set_fds(struct vhost_dev
*dev
,
1001 VhostShadowVirtqueue
*svq
, unsigned idx
,
1004 struct vhost_vring_file file
= {
1005 .index
= dev
->vq_index
+ idx
,
1007 const EventNotifier
*event_notifier
= &svq
->hdev_kick
;
1010 r
= event_notifier_init(&svq
->hdev_kick
, 0);
1012 error_setg_errno(errp
, -r
, "Couldn't create kick event notifier");
1013 goto err_init_hdev_kick
;
1016 r
= event_notifier_init(&svq
->hdev_call
, 0);
1018 error_setg_errno(errp
, -r
, "Couldn't create call event notifier");
1019 goto err_init_hdev_call
;
1022 file
.fd
= event_notifier_get_fd(event_notifier
);
1023 r
= vhost_vdpa_set_vring_dev_kick(dev
, &file
);
1024 if (unlikely(r
!= 0)) {
1025 error_setg_errno(errp
, -r
, "Can't set device kick fd");
1026 goto err_init_set_dev_fd
;
1029 event_notifier
= &svq
->hdev_call
;
1030 file
.fd
= event_notifier_get_fd(event_notifier
);
1031 r
= vhost_vdpa_set_vring_dev_call(dev
, &file
);
1032 if (unlikely(r
!= 0)) {
1033 error_setg_errno(errp
, -r
, "Can't set device call fd");
1034 goto err_init_set_dev_fd
;
1039 err_init_set_dev_fd
:
1040 event_notifier_set_handler(&svq
->hdev_call
, NULL
);
1043 event_notifier_cleanup(&svq
->hdev_kick
);
1050 * Unmap a SVQ area in the device
1052 static void vhost_vdpa_svq_unmap_ring(struct vhost_vdpa
*v
, hwaddr addr
)
1054 const DMAMap needle
= {
1055 .translated_addr
= addr
,
1057 const DMAMap
*result
= vhost_iova_tree_find_iova(v
->iova_tree
, &needle
);
1061 if (unlikely(!result
)) {
1062 error_report("Unable to find SVQ address to unmap");
1066 size
= ROUND_UP(result
->size
, qemu_real_host_page_size());
1067 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, result
->iova
, size
);
1068 if (unlikely(r
< 0)) {
1069 error_report("Unable to unmap SVQ vring: %s (%d)", g_strerror(-r
), -r
);
1073 vhost_iova_tree_remove(v
->iova_tree
, *result
);
1076 static void vhost_vdpa_svq_unmap_rings(struct vhost_dev
*dev
,
1077 const VhostShadowVirtqueue
*svq
)
1079 struct vhost_vdpa
*v
= dev
->opaque
;
1080 struct vhost_vring_addr svq_addr
;
1082 vhost_svq_get_vring_addr(svq
, &svq_addr
);
1084 vhost_vdpa_svq_unmap_ring(v
, svq_addr
.desc_user_addr
);
1086 vhost_vdpa_svq_unmap_ring(v
, svq_addr
.used_user_addr
);
1090 * Map the SVQ area in the device
1092 * @v: Vhost-vdpa device
1093 * @needle: The area to search iova
1094 * @errorp: Error pointer
1096 static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa
*v
, DMAMap
*needle
,
1101 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, needle
);
1102 if (unlikely(r
!= IOVA_OK
)) {
1103 error_setg(errp
, "Cannot allocate iova (%d)", r
);
1107 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, needle
->iova
,
1109 (void *)(uintptr_t)needle
->translated_addr
,
1110 needle
->perm
== IOMMU_RO
);
1111 if (unlikely(r
!= 0)) {
1112 error_setg_errno(errp
, -r
, "Cannot map region to device");
1113 vhost_iova_tree_remove(v
->iova_tree
, *needle
);
1120 * Map the shadow virtqueue rings in the device
1122 * @dev: The vhost device
1123 * @svq: The shadow virtqueue
1124 * @addr: Assigned IOVA addresses
1125 * @errp: Error pointer
1127 static bool vhost_vdpa_svq_map_rings(struct vhost_dev
*dev
,
1128 const VhostShadowVirtqueue
*svq
,
1129 struct vhost_vring_addr
*addr
,
1133 DMAMap device_region
, driver_region
;
1134 struct vhost_vring_addr svq_addr
;
1135 struct vhost_vdpa
*v
= dev
->opaque
;
1136 size_t device_size
= vhost_svq_device_area_size(svq
);
1137 size_t driver_size
= vhost_svq_driver_area_size(svq
);
1138 size_t avail_offset
;
1141 vhost_svq_get_vring_addr(svq
, &svq_addr
);
1143 driver_region
= (DMAMap
) {
1144 .translated_addr
= svq_addr
.desc_user_addr
,
1145 .size
= driver_size
- 1,
1148 ok
= vhost_vdpa_svq_map_ring(v
, &driver_region
, errp
);
1149 if (unlikely(!ok
)) {
1150 error_prepend(errp
, "Cannot create vq driver region: ");
1153 addr
->desc_user_addr
= driver_region
.iova
;
1154 avail_offset
= svq_addr
.avail_user_addr
- svq_addr
.desc_user_addr
;
1155 addr
->avail_user_addr
= driver_region
.iova
+ avail_offset
;
1157 device_region
= (DMAMap
) {
1158 .translated_addr
= svq_addr
.used_user_addr
,
1159 .size
= device_size
- 1,
1162 ok
= vhost_vdpa_svq_map_ring(v
, &device_region
, errp
);
1163 if (unlikely(!ok
)) {
1164 error_prepend(errp
, "Cannot create vq device region: ");
1165 vhost_vdpa_svq_unmap_ring(v
, driver_region
.translated_addr
);
1167 addr
->used_user_addr
= device_region
.iova
;
1172 static bool vhost_vdpa_svq_setup(struct vhost_dev
*dev
,
1173 VhostShadowVirtqueue
*svq
, unsigned idx
,
1176 uint16_t vq_index
= dev
->vq_index
+ idx
;
1177 struct vhost_vring_state s
= {
1182 r
= vhost_vdpa_set_dev_vring_base(dev
, &s
);
1184 error_setg_errno(errp
, -r
, "Cannot set vring base");
1188 r
= vhost_vdpa_svq_set_fds(dev
, svq
, idx
, errp
);
1192 static bool vhost_vdpa_svqs_start(struct vhost_dev
*dev
)
1194 struct vhost_vdpa
*v
= dev
->opaque
;
1198 if (!v
->shadow_vqs_enabled
) {
1202 for (i
= 0; i
< v
->shadow_vqs
->len
; ++i
) {
1203 VirtQueue
*vq
= virtio_get_queue(dev
->vdev
, dev
->vq_index
+ i
);
1204 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, i
);
1205 struct vhost_vring_addr addr
= {
1206 .index
= dev
->vq_index
+ i
,
1209 bool ok
= vhost_vdpa_svq_setup(dev
, svq
, i
, &err
);
1210 if (unlikely(!ok
)) {
1214 vhost_svq_start(svq
, dev
->vdev
, vq
, v
->iova_tree
);
1215 ok
= vhost_vdpa_svq_map_rings(dev
, svq
, &addr
, &err
);
1216 if (unlikely(!ok
)) {
1220 /* Override vring GPA set by vhost subsystem */
1221 r
= vhost_vdpa_set_vring_dev_addr(dev
, &addr
);
1222 if (unlikely(r
!= 0)) {
1223 error_setg_errno(&err
, -r
, "Cannot set device address");
1231 vhost_vdpa_svq_unmap_rings(dev
, g_ptr_array_index(v
->shadow_vqs
, i
));
1234 vhost_svq_stop(g_ptr_array_index(v
->shadow_vqs
, i
));
1237 error_reportf_err(err
, "Cannot setup SVQ %u: ", i
);
1238 for (unsigned j
= 0; j
< i
; ++j
) {
1239 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, j
);
1240 vhost_vdpa_svq_unmap_rings(dev
, svq
);
1241 vhost_svq_stop(svq
);
1247 static void vhost_vdpa_svqs_stop(struct vhost_dev
*dev
)
1249 struct vhost_vdpa
*v
= dev
->opaque
;
1251 if (!v
->shadow_vqs_enabled
) {
1255 for (unsigned i
= 0; i
< v
->shadow_vqs
->len
; ++i
) {
1256 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, i
);
1258 vhost_svq_stop(svq
);
1259 vhost_vdpa_svq_unmap_rings(dev
, svq
);
1261 event_notifier_cleanup(&svq
->hdev_kick
);
1262 event_notifier_cleanup(&svq
->hdev_call
);
1266 static void vhost_vdpa_suspend(struct vhost_dev
*dev
)
1268 struct vhost_vdpa
*v
= dev
->opaque
;
1271 if (!vhost_vdpa_first_dev(dev
)) {
1275 if (dev
->backend_cap
& BIT_ULL(VHOST_BACKEND_F_SUSPEND
)) {
1276 trace_vhost_vdpa_suspend(dev
);
1277 r
= ioctl(v
->device_fd
, VHOST_VDPA_SUSPEND
);
1279 error_report("Cannot suspend: %s(%d)", g_strerror(errno
), errno
);
1281 v
->suspended
= true;
1286 vhost_vdpa_reset_device(dev
);
1289 static int vhost_vdpa_dev_start(struct vhost_dev
*dev
, bool started
)
1291 struct vhost_vdpa
*v
= dev
->opaque
;
1293 trace_vhost_vdpa_dev_start(dev
, started
);
1296 vhost_vdpa_host_notifiers_init(dev
);
1297 ok
= vhost_vdpa_svqs_start(dev
);
1298 if (unlikely(!ok
)) {
1301 vhost_vdpa_set_vring_ready(dev
);
1303 vhost_vdpa_suspend(dev
);
1304 vhost_vdpa_svqs_stop(dev
);
1305 vhost_vdpa_host_notifiers_uninit(dev
, dev
->nvqs
);
1308 if (dev
->vq_index
+ dev
->nvqs
!= dev
->vq_index_end
) {
1313 if (vhost_dev_has_iommu(dev
) && (v
->shadow_vqs_enabled
)) {
1314 error_report("SVQ can not work while IOMMU enable, please disable"
1315 "IOMMU and try again");
1318 memory_listener_register(&v
->listener
, dev
->vdev
->dma_as
);
1320 return vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_DRIVER_OK
);
1326 static void vhost_vdpa_reset_status(struct vhost_dev
*dev
)
1328 struct vhost_vdpa
*v
= dev
->opaque
;
1330 if (dev
->vq_index
+ dev
->nvqs
!= dev
->vq_index_end
) {
1334 vhost_vdpa_reset_device(dev
);
1335 vhost_vdpa_add_status(dev
, VIRTIO_CONFIG_S_ACKNOWLEDGE
|
1336 VIRTIO_CONFIG_S_DRIVER
);
1337 memory_listener_unregister(&v
->listener
);
1340 static int vhost_vdpa_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
1341 struct vhost_log
*log
)
1343 struct vhost_vdpa
*v
= dev
->opaque
;
1344 if (v
->shadow_vqs_enabled
|| !vhost_vdpa_first_dev(dev
)) {
1348 trace_vhost_vdpa_set_log_base(dev
, base
, log
->size
, log
->refcnt
, log
->fd
,
1350 return vhost_vdpa_call(dev
, VHOST_SET_LOG_BASE
, &base
);
1353 static int vhost_vdpa_set_vring_addr(struct vhost_dev
*dev
,
1354 struct vhost_vring_addr
*addr
)
1356 struct vhost_vdpa
*v
= dev
->opaque
;
1358 if (v
->shadow_vqs_enabled
) {
1360 * Device vring addr was set at device start. SVQ base is handled by
1366 return vhost_vdpa_set_vring_dev_addr(dev
, addr
);
1369 static int vhost_vdpa_set_vring_num(struct vhost_dev
*dev
,
1370 struct vhost_vring_state
*ring
)
1372 trace_vhost_vdpa_set_vring_num(dev
, ring
->index
, ring
->num
);
1373 return vhost_vdpa_call(dev
, VHOST_SET_VRING_NUM
, ring
);
1376 static int vhost_vdpa_set_vring_base(struct vhost_dev
*dev
,
1377 struct vhost_vring_state
*ring
)
1379 struct vhost_vdpa
*v
= dev
->opaque
;
1381 if (v
->shadow_vqs_enabled
) {
1383 * Device vring base was set at device start. SVQ base is handled by
1389 return vhost_vdpa_set_dev_vring_base(dev
, ring
);
1392 static int vhost_vdpa_get_vring_base(struct vhost_dev
*dev
,
1393 struct vhost_vring_state
*ring
)
1395 struct vhost_vdpa
*v
= dev
->opaque
;
1398 if (v
->shadow_vqs_enabled
) {
1399 ring
->num
= virtio_queue_get_last_avail_idx(dev
->vdev
, ring
->index
);
1403 if (!v
->suspended
) {
1405 * Cannot trust in value returned by device, let vhost recover used
1411 ret
= vhost_vdpa_call(dev
, VHOST_GET_VRING_BASE
, ring
);
1412 trace_vhost_vdpa_get_vring_base(dev
, ring
->index
, ring
->num
);
1416 static int vhost_vdpa_set_vring_kick(struct vhost_dev
*dev
,
1417 struct vhost_vring_file
*file
)
1419 struct vhost_vdpa
*v
= dev
->opaque
;
1420 int vdpa_idx
= file
->index
- dev
->vq_index
;
1422 if (v
->shadow_vqs_enabled
) {
1423 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, vdpa_idx
);
1424 vhost_svq_set_svq_kick_fd(svq
, file
->fd
);
1427 return vhost_vdpa_set_vring_dev_kick(dev
, file
);
1431 static int vhost_vdpa_set_vring_call(struct vhost_dev
*dev
,
1432 struct vhost_vring_file
*file
)
1434 struct vhost_vdpa
*v
= dev
->opaque
;
1435 int vdpa_idx
= file
->index
- dev
->vq_index
;
1436 VhostShadowVirtqueue
*svq
= g_ptr_array_index(v
->shadow_vqs
, vdpa_idx
);
1438 /* Remember last call fd because we can switch to SVQ anytime. */
1439 vhost_svq_set_svq_call_fd(svq
, file
->fd
);
1440 if (v
->shadow_vqs_enabled
) {
1444 return vhost_vdpa_set_vring_dev_call(dev
, file
);
1447 static int vhost_vdpa_get_features(struct vhost_dev
*dev
,
1450 int ret
= vhost_vdpa_get_dev_features(dev
, features
);
1453 /* Add SVQ logging capabilities */
1454 *features
|= BIT_ULL(VHOST_F_LOG_ALL
);
1460 static int vhost_vdpa_set_owner(struct vhost_dev
*dev
)
1462 if (!vhost_vdpa_first_dev(dev
)) {
1466 trace_vhost_vdpa_set_owner(dev
);
1467 return vhost_vdpa_call(dev
, VHOST_SET_OWNER
, NULL
);
1470 static int vhost_vdpa_vq_get_addr(struct vhost_dev
*dev
,
1471 struct vhost_vring_addr
*addr
, struct vhost_virtqueue
*vq
)
1473 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_VDPA
);
1474 addr
->desc_user_addr
= (uint64_t)(unsigned long)vq
->desc_phys
;
1475 addr
->avail_user_addr
= (uint64_t)(unsigned long)vq
->avail_phys
;
1476 addr
->used_user_addr
= (uint64_t)(unsigned long)vq
->used_phys
;
1477 trace_vhost_vdpa_vq_get_addr(dev
, vq
, addr
->desc_user_addr
,
1478 addr
->avail_user_addr
, addr
->used_user_addr
);
1482 static bool vhost_vdpa_force_iommu(struct vhost_dev
*dev
)
1487 const VhostOps vdpa_ops
= {
1488 .backend_type
= VHOST_BACKEND_TYPE_VDPA
,
1489 .vhost_backend_init
= vhost_vdpa_init
,
1490 .vhost_backend_cleanup
= vhost_vdpa_cleanup
,
1491 .vhost_set_log_base
= vhost_vdpa_set_log_base
,
1492 .vhost_set_vring_addr
= vhost_vdpa_set_vring_addr
,
1493 .vhost_set_vring_num
= vhost_vdpa_set_vring_num
,
1494 .vhost_set_vring_base
= vhost_vdpa_set_vring_base
,
1495 .vhost_get_vring_base
= vhost_vdpa_get_vring_base
,
1496 .vhost_set_vring_kick
= vhost_vdpa_set_vring_kick
,
1497 .vhost_set_vring_call
= vhost_vdpa_set_vring_call
,
1498 .vhost_get_features
= vhost_vdpa_get_features
,
1499 .vhost_set_backend_cap
= vhost_vdpa_set_backend_cap
,
1500 .vhost_set_owner
= vhost_vdpa_set_owner
,
1501 .vhost_set_vring_endian
= NULL
,
1502 .vhost_backend_memslots_limit
= vhost_vdpa_memslots_limit
,
1503 .vhost_set_mem_table
= vhost_vdpa_set_mem_table
,
1504 .vhost_set_features
= vhost_vdpa_set_features
,
1505 .vhost_reset_device
= vhost_vdpa_reset_device
,
1506 .vhost_get_vq_index
= vhost_vdpa_get_vq_index
,
1507 .vhost_get_config
= vhost_vdpa_get_config
,
1508 .vhost_set_config
= vhost_vdpa_set_config
,
1509 .vhost_requires_shm_log
= NULL
,
1510 .vhost_migration_done
= NULL
,
1511 .vhost_backend_can_merge
= NULL
,
1512 .vhost_net_set_mtu
= NULL
,
1513 .vhost_set_iotlb_callback
= NULL
,
1514 .vhost_send_device_iotlb_msg
= NULL
,
1515 .vhost_dev_start
= vhost_vdpa_dev_start
,
1516 .vhost_get_device_id
= vhost_vdpa_get_device_id
,
1517 .vhost_vq_get_addr
= vhost_vdpa_vq_get_addr
,
1518 .vhost_force_iommu
= vhost_vdpa_force_iommu
,
1519 .vhost_set_config_call
= vhost_vdpa_set_config_call
,
1520 .vhost_reset_status
= vhost_vdpa_reset_status
,