xref: /qemu/hw/virtio/vhost-vdpa.c (revision 245cf2c24eb0d5a4fe75151e1794aa5cd53b130d)
1108a6481SCindy Lu /*
2108a6481SCindy Lu  * vhost-vdpa
3108a6481SCindy Lu  *
4108a6481SCindy Lu  *  Copyright(c) 2017-2018 Intel Corporation.
5108a6481SCindy Lu  *  Copyright(c) 2020 Red Hat, Inc.
6108a6481SCindy Lu  *
7108a6481SCindy Lu  * This work is licensed under the terms of the GNU GPL, version 2 or later.
8108a6481SCindy Lu  * See the COPYING file in the top-level directory.
9108a6481SCindy Lu  *
10108a6481SCindy Lu  */
11108a6481SCindy Lu 
12108a6481SCindy Lu #include "qemu/osdep.h"
13108a6481SCindy Lu #include <linux/vhost.h>
14108a6481SCindy Lu #include <linux/vfio.h>
15108a6481SCindy Lu #include <sys/eventfd.h>
16108a6481SCindy Lu #include <sys/ioctl.h>
17108a6481SCindy Lu #include "hw/virtio/vhost.h"
18108a6481SCindy Lu #include "hw/virtio/vhost-backend.h"
19108a6481SCindy Lu #include "hw/virtio/virtio-net.h"
20108a6481SCindy Lu #include "hw/virtio/vhost-vdpa.h"
21df77d45aSXie Yongji #include "exec/address-spaces.h"
22108a6481SCindy Lu #include "qemu/main-loop.h"
234dc5acc0SCindy Lu #include "cpu.h"
24778e67deSLaurent Vivier #include "trace.h"
25778e67deSLaurent Vivier #include "qemu-common.h"
26108a6481SCindy Lu 
27032e4d68SEugenio Pérez /*
28032e4d68SEugenio Pérez  * Return one past the end of the end of section. Be careful with uint64_t
29032e4d68SEugenio Pérez  * conversions!
30032e4d68SEugenio Pérez  */
31032e4d68SEugenio Pérez static Int128 vhost_vdpa_section_end(const MemoryRegionSection *section)
32032e4d68SEugenio Pérez {
33032e4d68SEugenio Pérez     Int128 llend = int128_make64(section->offset_within_address_space);
34032e4d68SEugenio Pérez     llend = int128_add(llend, section->size);
35032e4d68SEugenio Pérez     llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
36032e4d68SEugenio Pérez 
37032e4d68SEugenio Pérez     return llend;
38032e4d68SEugenio Pérez }
39032e4d68SEugenio Pérez 
40013108b6SEugenio Pérez static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection *section,
41013108b6SEugenio Pérez                                                 uint64_t iova_min,
42013108b6SEugenio Pérez                                                 uint64_t iova_max)
43108a6481SCindy Lu {
44013108b6SEugenio Pérez     Int128 llend;
45013108b6SEugenio Pérez 
46013108b6SEugenio Pérez     if ((!memory_region_is_ram(section->mr) &&
47108a6481SCindy Lu          !memory_region_is_iommu(section->mr)) ||
48c64038c9SEugenio Pérez         memory_region_is_protected(section->mr) ||
49d60c75d2SJason Wang         /* vhost-vDPA doesn't allow MMIO to be mapped  */
50013108b6SEugenio Pérez         memory_region_is_ram_device(section->mr)) {
51013108b6SEugenio Pérez         return true;
52013108b6SEugenio Pérez     }
53013108b6SEugenio Pérez 
54013108b6SEugenio Pérez     if (section->offset_within_address_space < iova_min) {
55013108b6SEugenio Pérez         error_report("RAM section out of device range (min=0x%" PRIx64
56013108b6SEugenio Pérez                      ", addr=0x%" HWADDR_PRIx ")",
57013108b6SEugenio Pérez                      iova_min, section->offset_within_address_space);
58013108b6SEugenio Pérez         return true;
59013108b6SEugenio Pérez     }
60013108b6SEugenio Pérez 
61013108b6SEugenio Pérez     llend = vhost_vdpa_section_end(section);
62013108b6SEugenio Pérez     if (int128_gt(llend, int128_make64(iova_max))) {
63013108b6SEugenio Pérez         error_report("RAM section out of device range (max=0x%" PRIx64
64013108b6SEugenio Pérez                      ", end addr=0x%" PRIx64 ")",
65013108b6SEugenio Pérez                      iova_max, int128_get64(llend));
66013108b6SEugenio Pérez         return true;
67013108b6SEugenio Pérez     }
68013108b6SEugenio Pérez 
69013108b6SEugenio Pérez     return false;
70108a6481SCindy Lu }
71108a6481SCindy Lu 
72108a6481SCindy Lu static int vhost_vdpa_dma_map(struct vhost_vdpa *v, hwaddr iova, hwaddr size,
73108a6481SCindy Lu                               void *vaddr, bool readonly)
74108a6481SCindy Lu {
75386494f2SCindy Lu     struct vhost_msg_v2 msg = {};
76108a6481SCindy Lu     int fd = v->device_fd;
77108a6481SCindy Lu     int ret = 0;
78108a6481SCindy Lu 
79108a6481SCindy Lu     msg.type = v->msg_type;
80108a6481SCindy Lu     msg.iotlb.iova = iova;
81108a6481SCindy Lu     msg.iotlb.size = size;
82108a6481SCindy Lu     msg.iotlb.uaddr = (uint64_t)(uintptr_t)vaddr;
83108a6481SCindy Lu     msg.iotlb.perm = readonly ? VHOST_ACCESS_RO : VHOST_ACCESS_RW;
84108a6481SCindy Lu     msg.iotlb.type = VHOST_IOTLB_UPDATE;
85108a6481SCindy Lu 
86778e67deSLaurent Vivier    trace_vhost_vdpa_dma_map(v, fd, msg.type, msg.iotlb.iova, msg.iotlb.size,
87778e67deSLaurent Vivier                             msg.iotlb.uaddr, msg.iotlb.perm, msg.iotlb.type);
88778e67deSLaurent Vivier 
89108a6481SCindy Lu     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
90108a6481SCindy Lu         error_report("failed to write, fd=%d, errno=%d (%s)",
91108a6481SCindy Lu             fd, errno, strerror(errno));
92108a6481SCindy Lu         return -EIO ;
93108a6481SCindy Lu     }
94108a6481SCindy Lu 
95108a6481SCindy Lu     return ret;
96108a6481SCindy Lu }
97108a6481SCindy Lu 
98108a6481SCindy Lu static int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, hwaddr iova,
99108a6481SCindy Lu                                 hwaddr size)
100108a6481SCindy Lu {
101386494f2SCindy Lu     struct vhost_msg_v2 msg = {};
102108a6481SCindy Lu     int fd = v->device_fd;
103108a6481SCindy Lu     int ret = 0;
104108a6481SCindy Lu 
105108a6481SCindy Lu     msg.type = v->msg_type;
106108a6481SCindy Lu     msg.iotlb.iova = iova;
107108a6481SCindy Lu     msg.iotlb.size = size;
108108a6481SCindy Lu     msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
109108a6481SCindy Lu 
110778e67deSLaurent Vivier     trace_vhost_vdpa_dma_unmap(v, fd, msg.type, msg.iotlb.iova,
111778e67deSLaurent Vivier                                msg.iotlb.size, msg.iotlb.type);
112778e67deSLaurent Vivier 
113108a6481SCindy Lu     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
114108a6481SCindy Lu         error_report("failed to write, fd=%d, errno=%d (%s)",
115108a6481SCindy Lu             fd, errno, strerror(errno));
116108a6481SCindy Lu         return -EIO ;
117108a6481SCindy Lu     }
118108a6481SCindy Lu 
119108a6481SCindy Lu     return ret;
120108a6481SCindy Lu }
121108a6481SCindy Lu 
122e6db5df7SEugenio Pérez static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa *v)
123a5bd0580SJason Wang {
124a5bd0580SJason Wang     int fd = v->device_fd;
125e6db5df7SEugenio Pérez     struct vhost_msg_v2 msg = {
126e6db5df7SEugenio Pérez         .type = v->msg_type,
127e6db5df7SEugenio Pérez         .iotlb.type = VHOST_IOTLB_BATCH_BEGIN,
128e6db5df7SEugenio Pérez     };
129a5bd0580SJason Wang 
130a5bd0580SJason Wang     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
131a5bd0580SJason Wang         error_report("failed to write, fd=%d, errno=%d (%s)",
132a5bd0580SJason Wang                      fd, errno, strerror(errno));
133a5bd0580SJason Wang     }
134a5bd0580SJason Wang }
135a5bd0580SJason Wang 
136e6db5df7SEugenio Pérez static void vhost_vdpa_iotlb_batch_begin_once(struct vhost_vdpa *v)
137e6db5df7SEugenio Pérez {
138e6db5df7SEugenio Pérez     if (v->dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH) &&
139e6db5df7SEugenio Pérez         !v->iotlb_batch_begin_sent) {
140e6db5df7SEugenio Pérez         vhost_vdpa_listener_begin_batch(v);
141e6db5df7SEugenio Pérez     }
142e6db5df7SEugenio Pérez 
143e6db5df7SEugenio Pérez     v->iotlb_batch_begin_sent = true;
144e6db5df7SEugenio Pérez }
145e6db5df7SEugenio Pérez 
146a5bd0580SJason Wang static void vhost_vdpa_listener_commit(MemoryListener *listener)
147a5bd0580SJason Wang {
148a5bd0580SJason Wang     struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener);
149a5bd0580SJason Wang     struct vhost_dev *dev = v->dev;
1508acb3218SPhilippe Mathieu-Daudé     struct vhost_msg_v2 msg = {};
151a5bd0580SJason Wang     int fd = v->device_fd;
152a5bd0580SJason Wang 
153a5bd0580SJason Wang     if (!(dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH))) {
154a5bd0580SJason Wang         return;
155a5bd0580SJason Wang     }
156a5bd0580SJason Wang 
157e6db5df7SEugenio Pérez     if (!v->iotlb_batch_begin_sent) {
158e6db5df7SEugenio Pérez         return;
159e6db5df7SEugenio Pérez     }
160e6db5df7SEugenio Pérez 
161a5bd0580SJason Wang     msg.type = v->msg_type;
162a5bd0580SJason Wang     msg.iotlb.type = VHOST_IOTLB_BATCH_END;
163a5bd0580SJason Wang 
164a5bd0580SJason Wang     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
165a5bd0580SJason Wang         error_report("failed to write, fd=%d, errno=%d (%s)",
166a5bd0580SJason Wang                      fd, errno, strerror(errno));
167a5bd0580SJason Wang     }
168e6db5df7SEugenio Pérez 
169e6db5df7SEugenio Pérez     v->iotlb_batch_begin_sent = false;
170a5bd0580SJason Wang }
171a5bd0580SJason Wang 
172108a6481SCindy Lu static void vhost_vdpa_listener_region_add(MemoryListener *listener,
173108a6481SCindy Lu                                            MemoryRegionSection *section)
174108a6481SCindy Lu {
175108a6481SCindy Lu     struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener);
176108a6481SCindy Lu     hwaddr iova;
177108a6481SCindy Lu     Int128 llend, llsize;
178108a6481SCindy Lu     void *vaddr;
179108a6481SCindy Lu     int ret;
180108a6481SCindy Lu 
181013108b6SEugenio Pérez     if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first,
182013108b6SEugenio Pérez                                             v->iova_range.last)) {
183108a6481SCindy Lu         return;
184108a6481SCindy Lu     }
185108a6481SCindy Lu 
186108a6481SCindy Lu     if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
187108a6481SCindy Lu                  (section->offset_within_region & ~TARGET_PAGE_MASK))) {
188108a6481SCindy Lu         error_report("%s received unaligned region", __func__);
189108a6481SCindy Lu         return;
190108a6481SCindy Lu     }
191108a6481SCindy Lu 
192108a6481SCindy Lu     iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
193032e4d68SEugenio Pérez     llend = vhost_vdpa_section_end(section);
194108a6481SCindy Lu     if (int128_ge(int128_make64(iova), llend)) {
195108a6481SCindy Lu         return;
196108a6481SCindy Lu     }
197108a6481SCindy Lu 
198108a6481SCindy Lu     memory_region_ref(section->mr);
199108a6481SCindy Lu 
200108a6481SCindy Lu     /* Here we assume that memory_region_is_ram(section->mr)==true */
201108a6481SCindy Lu 
202108a6481SCindy Lu     vaddr = memory_region_get_ram_ptr(section->mr) +
203108a6481SCindy Lu             section->offset_within_region +
204108a6481SCindy Lu             (iova - section->offset_within_address_space);
205108a6481SCindy Lu 
206778e67deSLaurent Vivier     trace_vhost_vdpa_listener_region_add(v, iova, int128_get64(llend),
207778e67deSLaurent Vivier                                          vaddr, section->readonly);
208778e67deSLaurent Vivier 
209108a6481SCindy Lu     llsize = int128_sub(llend, int128_make64(iova));
210108a6481SCindy Lu 
211e6db5df7SEugenio Pérez     vhost_vdpa_iotlb_batch_begin_once(v);
212108a6481SCindy Lu     ret = vhost_vdpa_dma_map(v, iova, int128_get64(llsize),
213108a6481SCindy Lu                              vaddr, section->readonly);
214108a6481SCindy Lu     if (ret) {
215108a6481SCindy Lu         error_report("vhost vdpa map fail!");
216108a6481SCindy Lu         goto fail;
217108a6481SCindy Lu     }
218108a6481SCindy Lu 
219108a6481SCindy Lu     return;
220108a6481SCindy Lu 
221108a6481SCindy Lu fail:
222108a6481SCindy Lu     /*
223108a6481SCindy Lu      * On the initfn path, store the first error in the container so we
224108a6481SCindy Lu      * can gracefully fail.  Runtime, there's not much we can do other
225108a6481SCindy Lu      * than throw a hardware error.
226108a6481SCindy Lu      */
227108a6481SCindy Lu     error_report("vhost-vdpa: DMA mapping failed, unable to continue");
228108a6481SCindy Lu     return;
229108a6481SCindy Lu 
230108a6481SCindy Lu }
231108a6481SCindy Lu 
232108a6481SCindy Lu static void vhost_vdpa_listener_region_del(MemoryListener *listener,
233108a6481SCindy Lu                                            MemoryRegionSection *section)
234108a6481SCindy Lu {
235108a6481SCindy Lu     struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener);
236108a6481SCindy Lu     hwaddr iova;
237108a6481SCindy Lu     Int128 llend, llsize;
238108a6481SCindy Lu     int ret;
239108a6481SCindy Lu 
240013108b6SEugenio Pérez     if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first,
241013108b6SEugenio Pérez                                             v->iova_range.last)) {
242108a6481SCindy Lu         return;
243108a6481SCindy Lu     }
244108a6481SCindy Lu 
245108a6481SCindy Lu     if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
246108a6481SCindy Lu                  (section->offset_within_region & ~TARGET_PAGE_MASK))) {
247108a6481SCindy Lu         error_report("%s received unaligned region", __func__);
248108a6481SCindy Lu         return;
249108a6481SCindy Lu     }
250108a6481SCindy Lu 
251108a6481SCindy Lu     iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
252032e4d68SEugenio Pérez     llend = vhost_vdpa_section_end(section);
253108a6481SCindy Lu 
254778e67deSLaurent Vivier     trace_vhost_vdpa_listener_region_del(v, iova, int128_get64(llend));
255778e67deSLaurent Vivier 
256108a6481SCindy Lu     if (int128_ge(int128_make64(iova), llend)) {
257108a6481SCindy Lu         return;
258108a6481SCindy Lu     }
259108a6481SCindy Lu 
260108a6481SCindy Lu     llsize = int128_sub(llend, int128_make64(iova));
261108a6481SCindy Lu 
262e6db5df7SEugenio Pérez     vhost_vdpa_iotlb_batch_begin_once(v);
263108a6481SCindy Lu     ret = vhost_vdpa_dma_unmap(v, iova, int128_get64(llsize));
264108a6481SCindy Lu     if (ret) {
265108a6481SCindy Lu         error_report("vhost_vdpa dma unmap error!");
266108a6481SCindy Lu     }
267108a6481SCindy Lu 
268108a6481SCindy Lu     memory_region_unref(section->mr);
269108a6481SCindy Lu }
270108a6481SCindy Lu /*
271108a6481SCindy Lu  * IOTLB API is used by vhost-vpda which requires incremental updating
272108a6481SCindy Lu  * of the mapping. So we can not use generic vhost memory listener which
273108a6481SCindy Lu  * depends on the addnop().
274108a6481SCindy Lu  */
275108a6481SCindy Lu static const MemoryListener vhost_vdpa_memory_listener = {
276142518bdSPeter Xu     .name = "vhost-vdpa",
277a5bd0580SJason Wang     .commit = vhost_vdpa_listener_commit,
278108a6481SCindy Lu     .region_add = vhost_vdpa_listener_region_add,
279108a6481SCindy Lu     .region_del = vhost_vdpa_listener_region_del,
280108a6481SCindy Lu };
281108a6481SCindy Lu 
282108a6481SCindy Lu static int vhost_vdpa_call(struct vhost_dev *dev, unsigned long int request,
283108a6481SCindy Lu                              void *arg)
284108a6481SCindy Lu {
285108a6481SCindy Lu     struct vhost_vdpa *v = dev->opaque;
286108a6481SCindy Lu     int fd = v->device_fd;
287f2a6e6c4SKevin Wolf     int ret;
288108a6481SCindy Lu 
289108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
290108a6481SCindy Lu 
291f2a6e6c4SKevin Wolf     ret = ioctl(fd, request, arg);
292f2a6e6c4SKevin Wolf     return ret < 0 ? -errno : ret;
293108a6481SCindy Lu }
294108a6481SCindy Lu 
295108a6481SCindy Lu static void vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status)
296108a6481SCindy Lu {
297108a6481SCindy Lu     uint8_t s;
298108a6481SCindy Lu 
299778e67deSLaurent Vivier     trace_vhost_vdpa_add_status(dev, status);
300108a6481SCindy Lu     if (vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s)) {
301108a6481SCindy Lu         return;
302108a6481SCindy Lu     }
303108a6481SCindy Lu 
304108a6481SCindy Lu     s |= status;
305108a6481SCindy Lu 
306108a6481SCindy Lu     vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &s);
307108a6481SCindy Lu }
308108a6481SCindy Lu 
309013108b6SEugenio Pérez static void vhost_vdpa_get_iova_range(struct vhost_vdpa *v)
310013108b6SEugenio Pérez {
311013108b6SEugenio Pérez     int ret = vhost_vdpa_call(v->dev, VHOST_VDPA_GET_IOVA_RANGE,
312013108b6SEugenio Pérez                               &v->iova_range);
313013108b6SEugenio Pérez     if (ret != 0) {
314013108b6SEugenio Pérez         v->iova_range.first = 0;
315013108b6SEugenio Pérez         v->iova_range.last = UINT64_MAX;
316013108b6SEugenio Pérez     }
317013108b6SEugenio Pérez 
318013108b6SEugenio Pérez     trace_vhost_vdpa_get_iova_range(v->dev, v->iova_range.first,
319013108b6SEugenio Pérez                                     v->iova_range.last);
320013108b6SEugenio Pérez }
321013108b6SEugenio Pérez 
3224d191cfdSJason Wang static bool vhost_vdpa_one_time_request(struct vhost_dev *dev)
3234d191cfdSJason Wang {
3244d191cfdSJason Wang     struct vhost_vdpa *v = dev->opaque;
3254d191cfdSJason Wang 
3264d191cfdSJason Wang     return v->index != 0;
3274d191cfdSJason Wang }
3284d191cfdSJason Wang 
32928770ff9SKevin Wolf static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)
330108a6481SCindy Lu {
331108a6481SCindy Lu     struct vhost_vdpa *v;
332108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
333778e67deSLaurent Vivier     trace_vhost_vdpa_init(dev, opaque);
334e1c1915bSDavid Hildenbrand     int ret;
335e1c1915bSDavid Hildenbrand 
336e1c1915bSDavid Hildenbrand     /*
337e1c1915bSDavid Hildenbrand      * Similar to VFIO, we end up pinning all guest memory and have to
338e1c1915bSDavid Hildenbrand      * disable discarding of RAM.
339e1c1915bSDavid Hildenbrand      */
340e1c1915bSDavid Hildenbrand     ret = ram_block_discard_disable(true);
341e1c1915bSDavid Hildenbrand     if (ret) {
342e1c1915bSDavid Hildenbrand         error_report("Cannot set discarding of RAM broken");
343e1c1915bSDavid Hildenbrand         return ret;
344e1c1915bSDavid Hildenbrand     }
345108a6481SCindy Lu 
346108a6481SCindy Lu     v = opaque;
347a5bd0580SJason Wang     v->dev = dev;
348108a6481SCindy Lu     dev->opaque =  opaque ;
349108a6481SCindy Lu     v->listener = vhost_vdpa_memory_listener;
350108a6481SCindy Lu     v->msg_type = VHOST_IOTLB_MSG_V2;
351108a6481SCindy Lu 
352013108b6SEugenio Pérez     vhost_vdpa_get_iova_range(v);
3534d191cfdSJason Wang 
3544d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
3554d191cfdSJason Wang         return 0;
3564d191cfdSJason Wang     }
3574d191cfdSJason Wang 
358108a6481SCindy Lu     vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
359108a6481SCindy Lu                                VIRTIO_CONFIG_S_DRIVER);
360108a6481SCindy Lu 
361108a6481SCindy Lu     return 0;
362108a6481SCindy Lu }
363108a6481SCindy Lu 
364d0416d48SJason Wang static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev,
365d0416d48SJason Wang                                             int queue_index)
366d0416d48SJason Wang {
367d0416d48SJason Wang     size_t page_size = qemu_real_host_page_size;
368d0416d48SJason Wang     struct vhost_vdpa *v = dev->opaque;
369d0416d48SJason Wang     VirtIODevice *vdev = dev->vdev;
370d0416d48SJason Wang     VhostVDPAHostNotifier *n;
371d0416d48SJason Wang 
372d0416d48SJason Wang     n = &v->notifier[queue_index];
373d0416d48SJason Wang 
374d0416d48SJason Wang     if (n->addr) {
375d0416d48SJason Wang         virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, false);
376d0416d48SJason Wang         object_unparent(OBJECT(&n->mr));
377d0416d48SJason Wang         munmap(n->addr, page_size);
378d0416d48SJason Wang         n->addr = NULL;
379d0416d48SJason Wang     }
380d0416d48SJason Wang }
381d0416d48SJason Wang 
382d0416d48SJason Wang static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
383d0416d48SJason Wang {
384d0416d48SJason Wang     int i;
385d0416d48SJason Wang 
386d0416d48SJason Wang     for (i = 0; i < n; i++) {
387d0416d48SJason Wang         vhost_vdpa_host_notifier_uninit(dev, i);
388d0416d48SJason Wang     }
389d0416d48SJason Wang }
390d0416d48SJason Wang 
391d0416d48SJason Wang static int vhost_vdpa_host_notifier_init(struct vhost_dev *dev, int queue_index)
392d0416d48SJason Wang {
393d0416d48SJason Wang     size_t page_size = qemu_real_host_page_size;
394d0416d48SJason Wang     struct vhost_vdpa *v = dev->opaque;
395d0416d48SJason Wang     VirtIODevice *vdev = dev->vdev;
396d0416d48SJason Wang     VhostVDPAHostNotifier *n;
397d0416d48SJason Wang     int fd = v->device_fd;
398d0416d48SJason Wang     void *addr;
399d0416d48SJason Wang     char *name;
400d0416d48SJason Wang 
401d0416d48SJason Wang     vhost_vdpa_host_notifier_uninit(dev, queue_index);
402d0416d48SJason Wang 
403d0416d48SJason Wang     n = &v->notifier[queue_index];
404d0416d48SJason Wang 
405d0416d48SJason Wang     addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
406d0416d48SJason Wang                 queue_index * page_size);
407d0416d48SJason Wang     if (addr == MAP_FAILED) {
408d0416d48SJason Wang         goto err;
409d0416d48SJason Wang     }
410d0416d48SJason Wang 
411d0416d48SJason Wang     name = g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]",
412d0416d48SJason Wang                            v, queue_index);
413d0416d48SJason Wang     memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name,
414d0416d48SJason Wang                                       page_size, addr);
415d0416d48SJason Wang     g_free(name);
416d0416d48SJason Wang 
417d0416d48SJason Wang     if (virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, true)) {
418d0416d48SJason Wang         munmap(addr, page_size);
419d0416d48SJason Wang         goto err;
420d0416d48SJason Wang     }
421d0416d48SJason Wang     n->addr = addr;
422d0416d48SJason Wang 
423d0416d48SJason Wang     return 0;
424d0416d48SJason Wang 
425d0416d48SJason Wang err:
426d0416d48SJason Wang     return -1;
427d0416d48SJason Wang }
428d0416d48SJason Wang 
429d0416d48SJason Wang static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev)
430d0416d48SJason Wang {
431d0416d48SJason Wang     int i;
432d0416d48SJason Wang 
433d0416d48SJason Wang     for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {
434d0416d48SJason Wang         if (vhost_vdpa_host_notifier_init(dev, i)) {
435d0416d48SJason Wang             goto err;
436d0416d48SJason Wang         }
437d0416d48SJason Wang     }
438d0416d48SJason Wang 
439d0416d48SJason Wang     return;
440d0416d48SJason Wang 
441d0416d48SJason Wang err:
442d0416d48SJason Wang     vhost_vdpa_host_notifiers_uninit(dev, i);
443d0416d48SJason Wang     return;
444d0416d48SJason Wang }
445d0416d48SJason Wang 
446108a6481SCindy Lu static int vhost_vdpa_cleanup(struct vhost_dev *dev)
447108a6481SCindy Lu {
448108a6481SCindy Lu     struct vhost_vdpa *v;
449108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
450108a6481SCindy Lu     v = dev->opaque;
451778e67deSLaurent Vivier     trace_vhost_vdpa_cleanup(dev, v);
452d0416d48SJason Wang     vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
453108a6481SCindy Lu     memory_listener_unregister(&v->listener);
454108a6481SCindy Lu 
455108a6481SCindy Lu     dev->opaque = NULL;
456e1c1915bSDavid Hildenbrand     ram_block_discard_disable(false);
457e1c1915bSDavid Hildenbrand 
458108a6481SCindy Lu     return 0;
459108a6481SCindy Lu }
460108a6481SCindy Lu 
461108a6481SCindy Lu static int vhost_vdpa_memslots_limit(struct vhost_dev *dev)
462108a6481SCindy Lu {
463778e67deSLaurent Vivier     trace_vhost_vdpa_memslots_limit(dev, INT_MAX);
464108a6481SCindy Lu     return INT_MAX;
465108a6481SCindy Lu }
466108a6481SCindy Lu 
467108a6481SCindy Lu static int vhost_vdpa_set_mem_table(struct vhost_dev *dev,
468108a6481SCindy Lu                                     struct vhost_memory *mem)
469108a6481SCindy Lu {
4704d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
4714d191cfdSJason Wang         return 0;
4724d191cfdSJason Wang     }
4734d191cfdSJason Wang 
474778e67deSLaurent Vivier     trace_vhost_vdpa_set_mem_table(dev, mem->nregions, mem->padding);
475778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE) &&
476778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS)) {
477778e67deSLaurent Vivier         int i;
478778e67deSLaurent Vivier         for (i = 0; i < mem->nregions; i++) {
479778e67deSLaurent Vivier             trace_vhost_vdpa_dump_regions(dev, i,
480778e67deSLaurent Vivier                                           mem->regions[i].guest_phys_addr,
481778e67deSLaurent Vivier                                           mem->regions[i].memory_size,
482778e67deSLaurent Vivier                                           mem->regions[i].userspace_addr,
483778e67deSLaurent Vivier                                           mem->regions[i].flags_padding);
484778e67deSLaurent Vivier         }
485778e67deSLaurent Vivier     }
486108a6481SCindy Lu     if (mem->padding) {
487108a6481SCindy Lu         return -1;
488108a6481SCindy Lu     }
489108a6481SCindy Lu 
490108a6481SCindy Lu     return 0;
491108a6481SCindy Lu }
492108a6481SCindy Lu 
493108a6481SCindy Lu static int vhost_vdpa_set_features(struct vhost_dev *dev,
494108a6481SCindy Lu                                    uint64_t features)
495108a6481SCindy Lu {
496108a6481SCindy Lu     int ret;
4974d191cfdSJason Wang 
4984d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
4994d191cfdSJason Wang         return 0;
5004d191cfdSJason Wang     }
5014d191cfdSJason Wang 
502778e67deSLaurent Vivier     trace_vhost_vdpa_set_features(dev, features);
503108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features);
504108a6481SCindy Lu     uint8_t status = 0;
505108a6481SCindy Lu     if (ret) {
506108a6481SCindy Lu         return ret;
507108a6481SCindy Lu     }
508108a6481SCindy Lu     vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
509108a6481SCindy Lu     vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &status);
510108a6481SCindy Lu 
511108a6481SCindy Lu     return !(status & VIRTIO_CONFIG_S_FEATURES_OK);
512108a6481SCindy Lu }
513108a6481SCindy Lu 
514a5bd0580SJason Wang static int vhost_vdpa_set_backend_cap(struct vhost_dev *dev)
515a5bd0580SJason Wang {
516a5bd0580SJason Wang     uint64_t features;
517a5bd0580SJason Wang     uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 |
518a5bd0580SJason Wang         0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH;
519a5bd0580SJason Wang     int r;
520a5bd0580SJason Wang 
521a5bd0580SJason Wang     if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) {
5222a83e97eSJason Wang         return -EFAULT;
523a5bd0580SJason Wang     }
524a5bd0580SJason Wang 
525a5bd0580SJason Wang     features &= f;
5264d191cfdSJason Wang 
5274d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
528a5bd0580SJason Wang         r = vhost_vdpa_call(dev, VHOST_SET_BACKEND_FEATURES, &features);
529a5bd0580SJason Wang         if (r) {
5302a83e97eSJason Wang             return -EFAULT;
531a5bd0580SJason Wang         }
5324d191cfdSJason Wang     }
533a5bd0580SJason Wang 
534a5bd0580SJason Wang     dev->backend_cap = features;
535a5bd0580SJason Wang 
536a5bd0580SJason Wang     return 0;
537a5bd0580SJason Wang }
538a5bd0580SJason Wang 
539c232b8f4SZenghui Yu static int vhost_vdpa_get_device_id(struct vhost_dev *dev,
540108a6481SCindy Lu                                     uint32_t *device_id)
541108a6481SCindy Lu {
542778e67deSLaurent Vivier     int ret;
543778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_DEVICE_ID, device_id);
544778e67deSLaurent Vivier     trace_vhost_vdpa_get_device_id(dev, *device_id);
545778e67deSLaurent Vivier     return ret;
546108a6481SCindy Lu }
547108a6481SCindy Lu 
548108a6481SCindy Lu static int vhost_vdpa_reset_device(struct vhost_dev *dev)
549108a6481SCindy Lu {
550778e67deSLaurent Vivier     int ret;
551108a6481SCindy Lu     uint8_t status = 0;
552108a6481SCindy Lu 
553778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
554778e67deSLaurent Vivier     trace_vhost_vdpa_reset_device(dev, status);
555778e67deSLaurent Vivier     return ret;
556108a6481SCindy Lu }
557108a6481SCindy Lu 
558108a6481SCindy Lu static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
559108a6481SCindy Lu {
560108a6481SCindy Lu     assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
561108a6481SCindy Lu 
562353244d8SJason Wang     trace_vhost_vdpa_get_vq_index(dev, idx, idx);
563353244d8SJason Wang     return idx;
564108a6481SCindy Lu }
565108a6481SCindy Lu 
566108a6481SCindy Lu static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
567108a6481SCindy Lu {
568108a6481SCindy Lu     int i;
569778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_ready(dev);
570108a6481SCindy Lu     for (i = 0; i < dev->nvqs; ++i) {
571108a6481SCindy Lu         struct vhost_vring_state state = {
572108a6481SCindy Lu             .index = dev->vq_index + i,
573108a6481SCindy Lu             .num = 1,
574108a6481SCindy Lu         };
575108a6481SCindy Lu         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
576108a6481SCindy Lu     }
577108a6481SCindy Lu     return 0;
578108a6481SCindy Lu }
579108a6481SCindy Lu 
580778e67deSLaurent Vivier static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config,
581778e67deSLaurent Vivier                                    uint32_t config_len)
582778e67deSLaurent Vivier {
583778e67deSLaurent Vivier     int b, len;
584778e67deSLaurent Vivier     char line[QEMU_HEXDUMP_LINE_LEN];
585778e67deSLaurent Vivier 
586778e67deSLaurent Vivier     for (b = 0; b < config_len; b += 16) {
587778e67deSLaurent Vivier         len = config_len - b;
588778e67deSLaurent Vivier         qemu_hexdump_line(line, b, config, len, false);
589778e67deSLaurent Vivier         trace_vhost_vdpa_dump_config(dev, line);
590778e67deSLaurent Vivier     }
591778e67deSLaurent Vivier }
592778e67deSLaurent Vivier 
593108a6481SCindy Lu static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
594108a6481SCindy Lu                                    uint32_t offset, uint32_t size,
595108a6481SCindy Lu                                    uint32_t flags)
596108a6481SCindy Lu {
597108a6481SCindy Lu     struct vhost_vdpa_config *config;
598108a6481SCindy Lu     int ret;
599108a6481SCindy Lu     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
600986d4f78SLi Qiang 
601778e67deSLaurent Vivier     trace_vhost_vdpa_set_config(dev, offset, size, flags);
602108a6481SCindy Lu     config = g_malloc(size + config_size);
603108a6481SCindy Lu     config->off = offset;
604108a6481SCindy Lu     config->len = size;
605108a6481SCindy Lu     memcpy(config->buf, data, size);
606778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG) &&
607778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) {
608778e67deSLaurent Vivier         vhost_vdpa_dump_config(dev, data, size);
609778e67deSLaurent Vivier     }
610108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG, config);
611108a6481SCindy Lu     g_free(config);
612108a6481SCindy Lu     return ret;
613108a6481SCindy Lu }
614108a6481SCindy Lu 
615108a6481SCindy Lu static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
61650de5138SKevin Wolf                                    uint32_t config_len, Error **errp)
617108a6481SCindy Lu {
618108a6481SCindy Lu     struct vhost_vdpa_config *v_config;
619108a6481SCindy Lu     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
620108a6481SCindy Lu     int ret;
621108a6481SCindy Lu 
622778e67deSLaurent Vivier     trace_vhost_vdpa_get_config(dev, config, config_len);
623108a6481SCindy Lu     v_config = g_malloc(config_len + config_size);
624108a6481SCindy Lu     v_config->len = config_len;
625108a6481SCindy Lu     v_config->off = 0;
626108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
627108a6481SCindy Lu     memcpy(config, v_config->buf, config_len);
628108a6481SCindy Lu     g_free(v_config);
629778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG) &&
630778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) {
631778e67deSLaurent Vivier         vhost_vdpa_dump_config(dev, config, config_len);
632778e67deSLaurent Vivier     }
633108a6481SCindy Lu     return ret;
634108a6481SCindy Lu  }
635108a6481SCindy Lu 
636108a6481SCindy Lu static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
637108a6481SCindy Lu {
638108a6481SCindy Lu     struct vhost_vdpa *v = dev->opaque;
639778e67deSLaurent Vivier     trace_vhost_vdpa_dev_start(dev, started);
6404d191cfdSJason Wang 
6414d191cfdSJason Wang     if (started) {
6424d191cfdSJason Wang         vhost_vdpa_host_notifiers_init(dev);
6434d191cfdSJason Wang         vhost_vdpa_set_vring_ready(dev);
6444d191cfdSJason Wang     } else {
6454d191cfdSJason Wang         vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
6464d191cfdSJason Wang     }
6474d191cfdSJason Wang 
648*245cf2c2SEugenio Pérez     if (dev->vq_index + dev->nvqs != dev->vq_index_end) {
6494d191cfdSJason Wang         return 0;
6504d191cfdSJason Wang     }
6514d191cfdSJason Wang 
652108a6481SCindy Lu     if (started) {
653108a6481SCindy Lu         uint8_t status = 0;
654108a6481SCindy Lu         memory_listener_register(&v->listener, &address_space_memory);
655108a6481SCindy Lu         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
656108a6481SCindy Lu         vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &status);
657108a6481SCindy Lu 
658108a6481SCindy Lu         return !(status & VIRTIO_CONFIG_S_DRIVER_OK);
659108a6481SCindy Lu     } else {
660108a6481SCindy Lu         vhost_vdpa_reset_device(dev);
661108a6481SCindy Lu         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
662108a6481SCindy Lu                                    VIRTIO_CONFIG_S_DRIVER);
663108a6481SCindy Lu         memory_listener_unregister(&v->listener);
664108a6481SCindy Lu 
665108a6481SCindy Lu         return 0;
666108a6481SCindy Lu     }
667108a6481SCindy Lu }
668108a6481SCindy Lu 
669108a6481SCindy Lu static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,
670108a6481SCindy Lu                                      struct vhost_log *log)
671108a6481SCindy Lu {
6724d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
6734d191cfdSJason Wang         return 0;
6744d191cfdSJason Wang     }
6754d191cfdSJason Wang 
676778e67deSLaurent Vivier     trace_vhost_vdpa_set_log_base(dev, base, log->size, log->refcnt, log->fd,
677778e67deSLaurent Vivier                                   log->log);
678108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_LOG_BASE, &base);
679108a6481SCindy Lu }
680108a6481SCindy Lu 
681108a6481SCindy Lu static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev,
682108a6481SCindy Lu                                        struct vhost_vring_addr *addr)
683108a6481SCindy Lu {
684778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags,
685778e67deSLaurent Vivier                                     addr->desc_user_addr, addr->used_user_addr,
686778e67deSLaurent Vivier                                     addr->avail_user_addr,
687778e67deSLaurent Vivier                                     addr->log_guest_addr);
688108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr);
689108a6481SCindy Lu }
690108a6481SCindy Lu 
691108a6481SCindy Lu static int vhost_vdpa_set_vring_num(struct vhost_dev *dev,
692108a6481SCindy Lu                                       struct vhost_vring_state *ring)
693108a6481SCindy Lu {
694778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_num(dev, ring->index, ring->num);
695108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_NUM, ring);
696108a6481SCindy Lu }
697108a6481SCindy Lu 
698108a6481SCindy Lu static int vhost_vdpa_set_vring_base(struct vhost_dev *dev,
699108a6481SCindy Lu                                        struct vhost_vring_state *ring)
700108a6481SCindy Lu {
701778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num);
702108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring);
703108a6481SCindy Lu }
704108a6481SCindy Lu 
705108a6481SCindy Lu static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,
706108a6481SCindy Lu                                        struct vhost_vring_state *ring)
707108a6481SCindy Lu {
708778e67deSLaurent Vivier     int ret;
709778e67deSLaurent Vivier 
710778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
711778e67deSLaurent Vivier     trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);
712778e67deSLaurent Vivier     return ret;
713108a6481SCindy Lu }
714108a6481SCindy Lu 
715108a6481SCindy Lu static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev,
716108a6481SCindy Lu                                        struct vhost_vring_file *file)
717108a6481SCindy Lu {
718778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd);
719108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file);
720108a6481SCindy Lu }
721108a6481SCindy Lu 
722108a6481SCindy Lu static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
723108a6481SCindy Lu                                        struct vhost_vring_file *file)
724108a6481SCindy Lu {
725778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
726108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
727108a6481SCindy Lu }
728108a6481SCindy Lu 
729108a6481SCindy Lu static int vhost_vdpa_get_features(struct vhost_dev *dev,
730108a6481SCindy Lu                                      uint64_t *features)
731108a6481SCindy Lu {
732778e67deSLaurent Vivier     int ret;
733778e67deSLaurent Vivier 
734778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features);
735778e67deSLaurent Vivier     trace_vhost_vdpa_get_features(dev, *features);
736778e67deSLaurent Vivier     return ret;
737108a6481SCindy Lu }
738108a6481SCindy Lu 
739108a6481SCindy Lu static int vhost_vdpa_set_owner(struct vhost_dev *dev)
740108a6481SCindy Lu {
7414d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
7424d191cfdSJason Wang         return 0;
7434d191cfdSJason Wang     }
7444d191cfdSJason Wang 
745778e67deSLaurent Vivier     trace_vhost_vdpa_set_owner(dev);
746108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_OWNER, NULL);
747108a6481SCindy Lu }
748108a6481SCindy Lu 
749108a6481SCindy Lu static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev,
750108a6481SCindy Lu                     struct vhost_vring_addr *addr, struct vhost_virtqueue *vq)
751108a6481SCindy Lu {
752108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
753108a6481SCindy Lu     addr->desc_user_addr = (uint64_t)(unsigned long)vq->desc_phys;
754108a6481SCindy Lu     addr->avail_user_addr = (uint64_t)(unsigned long)vq->avail_phys;
755108a6481SCindy Lu     addr->used_user_addr = (uint64_t)(unsigned long)vq->used_phys;
756778e67deSLaurent Vivier     trace_vhost_vdpa_vq_get_addr(dev, vq, addr->desc_user_addr,
757778e67deSLaurent Vivier                                  addr->avail_user_addr, addr->used_user_addr);
758108a6481SCindy Lu     return 0;
759108a6481SCindy Lu }
760108a6481SCindy Lu 
761108a6481SCindy Lu static bool  vhost_vdpa_force_iommu(struct vhost_dev *dev)
762108a6481SCindy Lu {
763108a6481SCindy Lu     return true;
764108a6481SCindy Lu }
765108a6481SCindy Lu 
766108a6481SCindy Lu const VhostOps vdpa_ops = {
767108a6481SCindy Lu         .backend_type = VHOST_BACKEND_TYPE_VDPA,
768108a6481SCindy Lu         .vhost_backend_init = vhost_vdpa_init,
769108a6481SCindy Lu         .vhost_backend_cleanup = vhost_vdpa_cleanup,
770108a6481SCindy Lu         .vhost_set_log_base = vhost_vdpa_set_log_base,
771108a6481SCindy Lu         .vhost_set_vring_addr = vhost_vdpa_set_vring_addr,
772108a6481SCindy Lu         .vhost_set_vring_num = vhost_vdpa_set_vring_num,
773108a6481SCindy Lu         .vhost_set_vring_base = vhost_vdpa_set_vring_base,
774108a6481SCindy Lu         .vhost_get_vring_base = vhost_vdpa_get_vring_base,
775108a6481SCindy Lu         .vhost_set_vring_kick = vhost_vdpa_set_vring_kick,
776108a6481SCindy Lu         .vhost_set_vring_call = vhost_vdpa_set_vring_call,
777108a6481SCindy Lu         .vhost_get_features = vhost_vdpa_get_features,
778a5bd0580SJason Wang         .vhost_set_backend_cap = vhost_vdpa_set_backend_cap,
779108a6481SCindy Lu         .vhost_set_owner = vhost_vdpa_set_owner,
780108a6481SCindy Lu         .vhost_set_vring_endian = NULL,
781108a6481SCindy Lu         .vhost_backend_memslots_limit = vhost_vdpa_memslots_limit,
782108a6481SCindy Lu         .vhost_set_mem_table = vhost_vdpa_set_mem_table,
783108a6481SCindy Lu         .vhost_set_features = vhost_vdpa_set_features,
784108a6481SCindy Lu         .vhost_reset_device = vhost_vdpa_reset_device,
785108a6481SCindy Lu         .vhost_get_vq_index = vhost_vdpa_get_vq_index,
786108a6481SCindy Lu         .vhost_get_config  = vhost_vdpa_get_config,
787108a6481SCindy Lu         .vhost_set_config = vhost_vdpa_set_config,
788108a6481SCindy Lu         .vhost_requires_shm_log = NULL,
789108a6481SCindy Lu         .vhost_migration_done = NULL,
790108a6481SCindy Lu         .vhost_backend_can_merge = NULL,
791108a6481SCindy Lu         .vhost_net_set_mtu = NULL,
792108a6481SCindy Lu         .vhost_set_iotlb_callback = NULL,
793108a6481SCindy Lu         .vhost_send_device_iotlb_msg = NULL,
794108a6481SCindy Lu         .vhost_dev_start = vhost_vdpa_dev_start,
795108a6481SCindy Lu         .vhost_get_device_id = vhost_vdpa_get_device_id,
796108a6481SCindy Lu         .vhost_vq_get_addr = vhost_vdpa_vq_get_addr,
797108a6481SCindy Lu         .vhost_force_iommu = vhost_vdpa_force_iommu,
798108a6481SCindy Lu };
799