xref: /qemu/hw/virtio/vhost-vdpa.c (revision 98f7607ecda00dea3cbb2ed7b4427c96846efb83)
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 
2953631151bSRoman Kagan static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status)
296108a6481SCindy Lu {
297108a6481SCindy Lu     uint8_t s;
2983631151bSRoman Kagan     int ret;
299108a6481SCindy Lu 
300778e67deSLaurent Vivier     trace_vhost_vdpa_add_status(dev, status);
3013631151bSRoman Kagan     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s);
3023631151bSRoman Kagan     if (ret < 0) {
3033631151bSRoman Kagan         return ret;
304108a6481SCindy Lu     }
305108a6481SCindy Lu 
306108a6481SCindy Lu     s |= status;
307108a6481SCindy Lu 
3083631151bSRoman Kagan     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &s);
3093631151bSRoman Kagan     if (ret < 0) {
3103631151bSRoman Kagan         return ret;
3113631151bSRoman Kagan     }
3123631151bSRoman Kagan 
3133631151bSRoman Kagan     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s);
3143631151bSRoman Kagan     if (ret < 0) {
3153631151bSRoman Kagan         return ret;
3163631151bSRoman Kagan     }
3173631151bSRoman Kagan 
3183631151bSRoman Kagan     if (!(s & status)) {
3193631151bSRoman Kagan         return -EIO;
3203631151bSRoman Kagan     }
3213631151bSRoman Kagan 
3223631151bSRoman Kagan     return 0;
323108a6481SCindy Lu }
324108a6481SCindy Lu 
325013108b6SEugenio Pérez static void vhost_vdpa_get_iova_range(struct vhost_vdpa *v)
326013108b6SEugenio Pérez {
327013108b6SEugenio Pérez     int ret = vhost_vdpa_call(v->dev, VHOST_VDPA_GET_IOVA_RANGE,
328013108b6SEugenio Pérez                               &v->iova_range);
329013108b6SEugenio Pérez     if (ret != 0) {
330013108b6SEugenio Pérez         v->iova_range.first = 0;
331013108b6SEugenio Pérez         v->iova_range.last = UINT64_MAX;
332013108b6SEugenio Pérez     }
333013108b6SEugenio Pérez 
334013108b6SEugenio Pérez     trace_vhost_vdpa_get_iova_range(v->dev, v->iova_range.first,
335013108b6SEugenio Pérez                                     v->iova_range.last);
336013108b6SEugenio Pérez }
337013108b6SEugenio Pérez 
3384d191cfdSJason Wang static bool vhost_vdpa_one_time_request(struct vhost_dev *dev)
3394d191cfdSJason Wang {
3404d191cfdSJason Wang     struct vhost_vdpa *v = dev->opaque;
3414d191cfdSJason Wang 
3424d191cfdSJason Wang     return v->index != 0;
3434d191cfdSJason Wang }
3444d191cfdSJason Wang 
34528770ff9SKevin Wolf static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)
346108a6481SCindy Lu {
347108a6481SCindy Lu     struct vhost_vdpa *v;
348108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
349778e67deSLaurent Vivier     trace_vhost_vdpa_init(dev, opaque);
350e1c1915bSDavid Hildenbrand     int ret;
351e1c1915bSDavid Hildenbrand 
352e1c1915bSDavid Hildenbrand     /*
353e1c1915bSDavid Hildenbrand      * Similar to VFIO, we end up pinning all guest memory and have to
354e1c1915bSDavid Hildenbrand      * disable discarding of RAM.
355e1c1915bSDavid Hildenbrand      */
356e1c1915bSDavid Hildenbrand     ret = ram_block_discard_disable(true);
357e1c1915bSDavid Hildenbrand     if (ret) {
358e1c1915bSDavid Hildenbrand         error_report("Cannot set discarding of RAM broken");
359e1c1915bSDavid Hildenbrand         return ret;
360e1c1915bSDavid Hildenbrand     }
361108a6481SCindy Lu 
362108a6481SCindy Lu     v = opaque;
363a5bd0580SJason Wang     v->dev = dev;
364108a6481SCindy Lu     dev->opaque =  opaque ;
365108a6481SCindy Lu     v->listener = vhost_vdpa_memory_listener;
366108a6481SCindy Lu     v->msg_type = VHOST_IOTLB_MSG_V2;
367108a6481SCindy Lu 
368013108b6SEugenio Pérez     vhost_vdpa_get_iova_range(v);
3694d191cfdSJason Wang 
3704d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
3714d191cfdSJason Wang         return 0;
3724d191cfdSJason Wang     }
3734d191cfdSJason Wang 
374108a6481SCindy Lu     vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
375108a6481SCindy Lu                                VIRTIO_CONFIG_S_DRIVER);
376108a6481SCindy Lu 
377108a6481SCindy Lu     return 0;
378108a6481SCindy Lu }
379108a6481SCindy Lu 
380d0416d48SJason Wang static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev,
381d0416d48SJason Wang                                             int queue_index)
382d0416d48SJason Wang {
383d0416d48SJason Wang     size_t page_size = qemu_real_host_page_size;
384d0416d48SJason Wang     struct vhost_vdpa *v = dev->opaque;
385d0416d48SJason Wang     VirtIODevice *vdev = dev->vdev;
386d0416d48SJason Wang     VhostVDPAHostNotifier *n;
387d0416d48SJason Wang 
388d0416d48SJason Wang     n = &v->notifier[queue_index];
389d0416d48SJason Wang 
390d0416d48SJason Wang     if (n->addr) {
391d0416d48SJason Wang         virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, false);
392d0416d48SJason Wang         object_unparent(OBJECT(&n->mr));
393d0416d48SJason Wang         munmap(n->addr, page_size);
394d0416d48SJason Wang         n->addr = NULL;
395d0416d48SJason Wang     }
396d0416d48SJason Wang }
397d0416d48SJason Wang 
398d0416d48SJason Wang static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
399d0416d48SJason Wang {
400d0416d48SJason Wang     int i;
401d0416d48SJason Wang 
402d0416d48SJason Wang     for (i = 0; i < n; i++) {
403d0416d48SJason Wang         vhost_vdpa_host_notifier_uninit(dev, i);
404d0416d48SJason Wang     }
405d0416d48SJason Wang }
406d0416d48SJason Wang 
407d0416d48SJason Wang static int vhost_vdpa_host_notifier_init(struct vhost_dev *dev, int queue_index)
408d0416d48SJason Wang {
409d0416d48SJason Wang     size_t page_size = qemu_real_host_page_size;
410d0416d48SJason Wang     struct vhost_vdpa *v = dev->opaque;
411d0416d48SJason Wang     VirtIODevice *vdev = dev->vdev;
412d0416d48SJason Wang     VhostVDPAHostNotifier *n;
413d0416d48SJason Wang     int fd = v->device_fd;
414d0416d48SJason Wang     void *addr;
415d0416d48SJason Wang     char *name;
416d0416d48SJason Wang 
417d0416d48SJason Wang     vhost_vdpa_host_notifier_uninit(dev, queue_index);
418d0416d48SJason Wang 
419d0416d48SJason Wang     n = &v->notifier[queue_index];
420d0416d48SJason Wang 
421d0416d48SJason Wang     addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
422d0416d48SJason Wang                 queue_index * page_size);
423d0416d48SJason Wang     if (addr == MAP_FAILED) {
424d0416d48SJason Wang         goto err;
425d0416d48SJason Wang     }
426d0416d48SJason Wang 
427d0416d48SJason Wang     name = g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]",
428d0416d48SJason Wang                            v, queue_index);
429d0416d48SJason Wang     memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name,
430d0416d48SJason Wang                                       page_size, addr);
431d0416d48SJason Wang     g_free(name);
432d0416d48SJason Wang 
433d0416d48SJason Wang     if (virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, true)) {
434*98f7607eSLaurent Vivier         object_unparent(OBJECT(&n->mr));
435d0416d48SJason Wang         munmap(addr, page_size);
436d0416d48SJason Wang         goto err;
437d0416d48SJason Wang     }
438d0416d48SJason Wang     n->addr = addr;
439d0416d48SJason Wang 
440d0416d48SJason Wang     return 0;
441d0416d48SJason Wang 
442d0416d48SJason Wang err:
443d0416d48SJason Wang     return -1;
444d0416d48SJason Wang }
445d0416d48SJason Wang 
446d0416d48SJason Wang static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev)
447d0416d48SJason Wang {
448d0416d48SJason Wang     int i;
449d0416d48SJason Wang 
450d0416d48SJason Wang     for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {
451d0416d48SJason Wang         if (vhost_vdpa_host_notifier_init(dev, i)) {
452d0416d48SJason Wang             goto err;
453d0416d48SJason Wang         }
454d0416d48SJason Wang     }
455d0416d48SJason Wang 
456d0416d48SJason Wang     return;
457d0416d48SJason Wang 
458d0416d48SJason Wang err:
459d0416d48SJason Wang     vhost_vdpa_host_notifiers_uninit(dev, i);
460d0416d48SJason Wang     return;
461d0416d48SJason Wang }
462d0416d48SJason Wang 
463108a6481SCindy Lu static int vhost_vdpa_cleanup(struct vhost_dev *dev)
464108a6481SCindy Lu {
465108a6481SCindy Lu     struct vhost_vdpa *v;
466108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
467108a6481SCindy Lu     v = dev->opaque;
468778e67deSLaurent Vivier     trace_vhost_vdpa_cleanup(dev, v);
469d0416d48SJason Wang     vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
470108a6481SCindy Lu     memory_listener_unregister(&v->listener);
471108a6481SCindy Lu 
472108a6481SCindy Lu     dev->opaque = NULL;
473e1c1915bSDavid Hildenbrand     ram_block_discard_disable(false);
474e1c1915bSDavid Hildenbrand 
475108a6481SCindy Lu     return 0;
476108a6481SCindy Lu }
477108a6481SCindy Lu 
478108a6481SCindy Lu static int vhost_vdpa_memslots_limit(struct vhost_dev *dev)
479108a6481SCindy Lu {
480778e67deSLaurent Vivier     trace_vhost_vdpa_memslots_limit(dev, INT_MAX);
481108a6481SCindy Lu     return INT_MAX;
482108a6481SCindy Lu }
483108a6481SCindy Lu 
484108a6481SCindy Lu static int vhost_vdpa_set_mem_table(struct vhost_dev *dev,
485108a6481SCindy Lu                                     struct vhost_memory *mem)
486108a6481SCindy Lu {
4874d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
4884d191cfdSJason Wang         return 0;
4894d191cfdSJason Wang     }
4904d191cfdSJason Wang 
491778e67deSLaurent Vivier     trace_vhost_vdpa_set_mem_table(dev, mem->nregions, mem->padding);
492778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE) &&
493778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS)) {
494778e67deSLaurent Vivier         int i;
495778e67deSLaurent Vivier         for (i = 0; i < mem->nregions; i++) {
496778e67deSLaurent Vivier             trace_vhost_vdpa_dump_regions(dev, i,
497778e67deSLaurent Vivier                                           mem->regions[i].guest_phys_addr,
498778e67deSLaurent Vivier                                           mem->regions[i].memory_size,
499778e67deSLaurent Vivier                                           mem->regions[i].userspace_addr,
500778e67deSLaurent Vivier                                           mem->regions[i].flags_padding);
501778e67deSLaurent Vivier         }
502778e67deSLaurent Vivier     }
503108a6481SCindy Lu     if (mem->padding) {
5043631151bSRoman Kagan         return -EINVAL;
505108a6481SCindy Lu     }
506108a6481SCindy Lu 
507108a6481SCindy Lu     return 0;
508108a6481SCindy Lu }
509108a6481SCindy Lu 
510108a6481SCindy Lu static int vhost_vdpa_set_features(struct vhost_dev *dev,
511108a6481SCindy Lu                                    uint64_t features)
512108a6481SCindy Lu {
513108a6481SCindy Lu     int ret;
5144d191cfdSJason Wang 
5154d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
5164d191cfdSJason Wang         return 0;
5174d191cfdSJason Wang     }
5184d191cfdSJason Wang 
519778e67deSLaurent Vivier     trace_vhost_vdpa_set_features(dev, features);
520108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features);
521108a6481SCindy Lu     if (ret) {
522108a6481SCindy Lu         return ret;
523108a6481SCindy Lu     }
524108a6481SCindy Lu 
5253631151bSRoman Kagan     return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
526108a6481SCindy Lu }
527108a6481SCindy Lu 
528a5bd0580SJason Wang static int vhost_vdpa_set_backend_cap(struct vhost_dev *dev)
529a5bd0580SJason Wang {
530a5bd0580SJason Wang     uint64_t features;
531a5bd0580SJason Wang     uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 |
532a5bd0580SJason Wang         0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH;
533a5bd0580SJason Wang     int r;
534a5bd0580SJason Wang 
535a5bd0580SJason Wang     if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) {
5362a83e97eSJason Wang         return -EFAULT;
537a5bd0580SJason Wang     }
538a5bd0580SJason Wang 
539a5bd0580SJason Wang     features &= f;
5404d191cfdSJason Wang 
5414d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
542a5bd0580SJason Wang         r = vhost_vdpa_call(dev, VHOST_SET_BACKEND_FEATURES, &features);
543a5bd0580SJason Wang         if (r) {
5442a83e97eSJason Wang             return -EFAULT;
545a5bd0580SJason Wang         }
5464d191cfdSJason Wang     }
547a5bd0580SJason Wang 
548a5bd0580SJason Wang     dev->backend_cap = features;
549a5bd0580SJason Wang 
550a5bd0580SJason Wang     return 0;
551a5bd0580SJason Wang }
552a5bd0580SJason Wang 
553c232b8f4SZenghui Yu static int vhost_vdpa_get_device_id(struct vhost_dev *dev,
554108a6481SCindy Lu                                     uint32_t *device_id)
555108a6481SCindy Lu {
556778e67deSLaurent Vivier     int ret;
557778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_DEVICE_ID, device_id);
558778e67deSLaurent Vivier     trace_vhost_vdpa_get_device_id(dev, *device_id);
559778e67deSLaurent Vivier     return ret;
560108a6481SCindy Lu }
561108a6481SCindy Lu 
562108a6481SCindy Lu static int vhost_vdpa_reset_device(struct vhost_dev *dev)
563108a6481SCindy Lu {
564778e67deSLaurent Vivier     int ret;
565108a6481SCindy Lu     uint8_t status = 0;
566108a6481SCindy Lu 
567778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
568778e67deSLaurent Vivier     trace_vhost_vdpa_reset_device(dev, status);
569778e67deSLaurent Vivier     return ret;
570108a6481SCindy Lu }
571108a6481SCindy Lu 
572108a6481SCindy Lu static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
573108a6481SCindy Lu {
574108a6481SCindy Lu     assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
575108a6481SCindy Lu 
576353244d8SJason Wang     trace_vhost_vdpa_get_vq_index(dev, idx, idx);
577353244d8SJason Wang     return idx;
578108a6481SCindy Lu }
579108a6481SCindy Lu 
580108a6481SCindy Lu static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
581108a6481SCindy Lu {
582108a6481SCindy Lu     int i;
583778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_ready(dev);
584108a6481SCindy Lu     for (i = 0; i < dev->nvqs; ++i) {
585108a6481SCindy Lu         struct vhost_vring_state state = {
586108a6481SCindy Lu             .index = dev->vq_index + i,
587108a6481SCindy Lu             .num = 1,
588108a6481SCindy Lu         };
589108a6481SCindy Lu         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
590108a6481SCindy Lu     }
591108a6481SCindy Lu     return 0;
592108a6481SCindy Lu }
593108a6481SCindy Lu 
594778e67deSLaurent Vivier static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config,
595778e67deSLaurent Vivier                                    uint32_t config_len)
596778e67deSLaurent Vivier {
597778e67deSLaurent Vivier     int b, len;
598778e67deSLaurent Vivier     char line[QEMU_HEXDUMP_LINE_LEN];
599778e67deSLaurent Vivier 
600778e67deSLaurent Vivier     for (b = 0; b < config_len; b += 16) {
601778e67deSLaurent Vivier         len = config_len - b;
602778e67deSLaurent Vivier         qemu_hexdump_line(line, b, config, len, false);
603778e67deSLaurent Vivier         trace_vhost_vdpa_dump_config(dev, line);
604778e67deSLaurent Vivier     }
605778e67deSLaurent Vivier }
606778e67deSLaurent Vivier 
607108a6481SCindy Lu static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
608108a6481SCindy Lu                                    uint32_t offset, uint32_t size,
609108a6481SCindy Lu                                    uint32_t flags)
610108a6481SCindy Lu {
611108a6481SCindy Lu     struct vhost_vdpa_config *config;
612108a6481SCindy Lu     int ret;
613108a6481SCindy Lu     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
614986d4f78SLi Qiang 
615778e67deSLaurent Vivier     trace_vhost_vdpa_set_config(dev, offset, size, flags);
616108a6481SCindy Lu     config = g_malloc(size + config_size);
617108a6481SCindy Lu     config->off = offset;
618108a6481SCindy Lu     config->len = size;
619108a6481SCindy Lu     memcpy(config->buf, data, size);
620778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG) &&
621778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) {
622778e67deSLaurent Vivier         vhost_vdpa_dump_config(dev, data, size);
623778e67deSLaurent Vivier     }
624108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG, config);
625108a6481SCindy Lu     g_free(config);
626108a6481SCindy Lu     return ret;
627108a6481SCindy Lu }
628108a6481SCindy Lu 
629108a6481SCindy Lu static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
63050de5138SKevin Wolf                                    uint32_t config_len, Error **errp)
631108a6481SCindy Lu {
632108a6481SCindy Lu     struct vhost_vdpa_config *v_config;
633108a6481SCindy Lu     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
634108a6481SCindy Lu     int ret;
635108a6481SCindy Lu 
636778e67deSLaurent Vivier     trace_vhost_vdpa_get_config(dev, config, config_len);
637108a6481SCindy Lu     v_config = g_malloc(config_len + config_size);
638108a6481SCindy Lu     v_config->len = config_len;
639108a6481SCindy Lu     v_config->off = 0;
640108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
641108a6481SCindy Lu     memcpy(config, v_config->buf, config_len);
642108a6481SCindy Lu     g_free(v_config);
643778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG) &&
644778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) {
645778e67deSLaurent Vivier         vhost_vdpa_dump_config(dev, config, config_len);
646778e67deSLaurent Vivier     }
647108a6481SCindy Lu     return ret;
648108a6481SCindy Lu  }
649108a6481SCindy Lu 
650108a6481SCindy Lu static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
651108a6481SCindy Lu {
652108a6481SCindy Lu     struct vhost_vdpa *v = dev->opaque;
653778e67deSLaurent Vivier     trace_vhost_vdpa_dev_start(dev, started);
6544d191cfdSJason Wang 
6554d191cfdSJason Wang     if (started) {
6564d191cfdSJason Wang         vhost_vdpa_host_notifiers_init(dev);
6574d191cfdSJason Wang         vhost_vdpa_set_vring_ready(dev);
6584d191cfdSJason Wang     } else {
6594d191cfdSJason Wang         vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
6604d191cfdSJason Wang     }
6614d191cfdSJason Wang 
662245cf2c2SEugenio Pérez     if (dev->vq_index + dev->nvqs != dev->vq_index_end) {
6634d191cfdSJason Wang         return 0;
6644d191cfdSJason Wang     }
6654d191cfdSJason Wang 
666108a6481SCindy Lu     if (started) {
667108a6481SCindy Lu         memory_listener_register(&v->listener, &address_space_memory);
6683631151bSRoman Kagan         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
669108a6481SCindy Lu     } else {
670108a6481SCindy Lu         vhost_vdpa_reset_device(dev);
671108a6481SCindy Lu         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
672108a6481SCindy Lu                                    VIRTIO_CONFIG_S_DRIVER);
673108a6481SCindy Lu         memory_listener_unregister(&v->listener);
674108a6481SCindy Lu 
675108a6481SCindy Lu         return 0;
676108a6481SCindy Lu     }
677108a6481SCindy Lu }
678108a6481SCindy Lu 
679108a6481SCindy Lu static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,
680108a6481SCindy Lu                                      struct vhost_log *log)
681108a6481SCindy Lu {
6824d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
6834d191cfdSJason Wang         return 0;
6844d191cfdSJason Wang     }
6854d191cfdSJason Wang 
686778e67deSLaurent Vivier     trace_vhost_vdpa_set_log_base(dev, base, log->size, log->refcnt, log->fd,
687778e67deSLaurent Vivier                                   log->log);
688108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_LOG_BASE, &base);
689108a6481SCindy Lu }
690108a6481SCindy Lu 
691108a6481SCindy Lu static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev,
692108a6481SCindy Lu                                        struct vhost_vring_addr *addr)
693108a6481SCindy Lu {
694778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags,
695778e67deSLaurent Vivier                                     addr->desc_user_addr, addr->used_user_addr,
696778e67deSLaurent Vivier                                     addr->avail_user_addr,
697778e67deSLaurent Vivier                                     addr->log_guest_addr);
698108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr);
699108a6481SCindy Lu }
700108a6481SCindy Lu 
701108a6481SCindy Lu static int vhost_vdpa_set_vring_num(struct vhost_dev *dev,
702108a6481SCindy Lu                                       struct vhost_vring_state *ring)
703108a6481SCindy Lu {
704778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_num(dev, ring->index, ring->num);
705108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_NUM, ring);
706108a6481SCindy Lu }
707108a6481SCindy Lu 
708108a6481SCindy Lu static int vhost_vdpa_set_vring_base(struct vhost_dev *dev,
709108a6481SCindy Lu                                        struct vhost_vring_state *ring)
710108a6481SCindy Lu {
711778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num);
712108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring);
713108a6481SCindy Lu }
714108a6481SCindy Lu 
715108a6481SCindy Lu static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,
716108a6481SCindy Lu                                        struct vhost_vring_state *ring)
717108a6481SCindy Lu {
718778e67deSLaurent Vivier     int ret;
719778e67deSLaurent Vivier 
720778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
721778e67deSLaurent Vivier     trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);
722778e67deSLaurent Vivier     return ret;
723108a6481SCindy Lu }
724108a6481SCindy Lu 
725108a6481SCindy Lu static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev,
726108a6481SCindy Lu                                        struct vhost_vring_file *file)
727108a6481SCindy Lu {
728778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd);
729108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file);
730108a6481SCindy Lu }
731108a6481SCindy Lu 
732108a6481SCindy Lu static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
733108a6481SCindy Lu                                        struct vhost_vring_file *file)
734108a6481SCindy Lu {
735778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
736108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
737108a6481SCindy Lu }
738108a6481SCindy Lu 
739108a6481SCindy Lu static int vhost_vdpa_get_features(struct vhost_dev *dev,
740108a6481SCindy Lu                                      uint64_t *features)
741108a6481SCindy Lu {
742778e67deSLaurent Vivier     int ret;
743778e67deSLaurent Vivier 
744778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features);
745778e67deSLaurent Vivier     trace_vhost_vdpa_get_features(dev, *features);
746778e67deSLaurent Vivier     return ret;
747108a6481SCindy Lu }
748108a6481SCindy Lu 
749108a6481SCindy Lu static int vhost_vdpa_set_owner(struct vhost_dev *dev)
750108a6481SCindy Lu {
7514d191cfdSJason Wang     if (vhost_vdpa_one_time_request(dev)) {
7524d191cfdSJason Wang         return 0;
7534d191cfdSJason Wang     }
7544d191cfdSJason Wang 
755778e67deSLaurent Vivier     trace_vhost_vdpa_set_owner(dev);
756108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_OWNER, NULL);
757108a6481SCindy Lu }
758108a6481SCindy Lu 
759108a6481SCindy Lu static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev,
760108a6481SCindy Lu                     struct vhost_vring_addr *addr, struct vhost_virtqueue *vq)
761108a6481SCindy Lu {
762108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
763108a6481SCindy Lu     addr->desc_user_addr = (uint64_t)(unsigned long)vq->desc_phys;
764108a6481SCindy Lu     addr->avail_user_addr = (uint64_t)(unsigned long)vq->avail_phys;
765108a6481SCindy Lu     addr->used_user_addr = (uint64_t)(unsigned long)vq->used_phys;
766778e67deSLaurent Vivier     trace_vhost_vdpa_vq_get_addr(dev, vq, addr->desc_user_addr,
767778e67deSLaurent Vivier                                  addr->avail_user_addr, addr->used_user_addr);
768108a6481SCindy Lu     return 0;
769108a6481SCindy Lu }
770108a6481SCindy Lu 
771108a6481SCindy Lu static bool  vhost_vdpa_force_iommu(struct vhost_dev *dev)
772108a6481SCindy Lu {
773108a6481SCindy Lu     return true;
774108a6481SCindy Lu }
775108a6481SCindy Lu 
776108a6481SCindy Lu const VhostOps vdpa_ops = {
777108a6481SCindy Lu         .backend_type = VHOST_BACKEND_TYPE_VDPA,
778108a6481SCindy Lu         .vhost_backend_init = vhost_vdpa_init,
779108a6481SCindy Lu         .vhost_backend_cleanup = vhost_vdpa_cleanup,
780108a6481SCindy Lu         .vhost_set_log_base = vhost_vdpa_set_log_base,
781108a6481SCindy Lu         .vhost_set_vring_addr = vhost_vdpa_set_vring_addr,
782108a6481SCindy Lu         .vhost_set_vring_num = vhost_vdpa_set_vring_num,
783108a6481SCindy Lu         .vhost_set_vring_base = vhost_vdpa_set_vring_base,
784108a6481SCindy Lu         .vhost_get_vring_base = vhost_vdpa_get_vring_base,
785108a6481SCindy Lu         .vhost_set_vring_kick = vhost_vdpa_set_vring_kick,
786108a6481SCindy Lu         .vhost_set_vring_call = vhost_vdpa_set_vring_call,
787108a6481SCindy Lu         .vhost_get_features = vhost_vdpa_get_features,
788a5bd0580SJason Wang         .vhost_set_backend_cap = vhost_vdpa_set_backend_cap,
789108a6481SCindy Lu         .vhost_set_owner = vhost_vdpa_set_owner,
790108a6481SCindy Lu         .vhost_set_vring_endian = NULL,
791108a6481SCindy Lu         .vhost_backend_memslots_limit = vhost_vdpa_memslots_limit,
792108a6481SCindy Lu         .vhost_set_mem_table = vhost_vdpa_set_mem_table,
793108a6481SCindy Lu         .vhost_set_features = vhost_vdpa_set_features,
794108a6481SCindy Lu         .vhost_reset_device = vhost_vdpa_reset_device,
795108a6481SCindy Lu         .vhost_get_vq_index = vhost_vdpa_get_vq_index,
796108a6481SCindy Lu         .vhost_get_config  = vhost_vdpa_get_config,
797108a6481SCindy Lu         .vhost_set_config = vhost_vdpa_set_config,
798108a6481SCindy Lu         .vhost_requires_shm_log = NULL,
799108a6481SCindy Lu         .vhost_migration_done = NULL,
800108a6481SCindy Lu         .vhost_backend_can_merge = NULL,
801108a6481SCindy Lu         .vhost_net_set_mtu = NULL,
802108a6481SCindy Lu         .vhost_set_iotlb_callback = NULL,
803108a6481SCindy Lu         .vhost_send_device_iotlb_msg = NULL,
804108a6481SCindy Lu         .vhost_dev_start = vhost_vdpa_dev_start,
805108a6481SCindy Lu         .vhost_get_device_id = vhost_vdpa_get_device_id,
806108a6481SCindy Lu         .vhost_vq_get_addr = vhost_vdpa_vq_get_addr,
807108a6481SCindy Lu         .vhost_force_iommu = vhost_vdpa_force_iommu,
808108a6481SCindy Lu };
809