xref: /qemu/hw/virtio/vhost-vdpa.c (revision d83b4945805d68f47742f70bba2ea4d5c9880dc8)
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"
20dff4426fSEugenio Pérez #include "hw/virtio/vhost-shadow-virtqueue.h"
21108a6481SCindy Lu #include "hw/virtio/vhost-vdpa.h"
22df77d45aSXie Yongji #include "exec/address-spaces.h"
23c156d5bfSEugenio Pérez #include "migration/blocker.h"
24415b7327SMarc-André Lureau #include "qemu/cutils.h"
25108a6481SCindy Lu #include "qemu/main-loop.h"
264dc5acc0SCindy Lu #include "cpu.h"
27778e67deSLaurent Vivier #include "trace.h"
28dff4426fSEugenio Pérez #include "qapi/error.h"
29108a6481SCindy Lu 
30032e4d68SEugenio Pérez /*
31032e4d68SEugenio Pérez  * Return one past the end of the end of section. Be careful with uint64_t
32032e4d68SEugenio Pérez  * conversions!
33032e4d68SEugenio Pérez  */
34032e4d68SEugenio Pérez static Int128 vhost_vdpa_section_end(const MemoryRegionSection *section)
35032e4d68SEugenio Pérez {
36032e4d68SEugenio Pérez     Int128 llend = int128_make64(section->offset_within_address_space);
37032e4d68SEugenio Pérez     llend = int128_add(llend, section->size);
38032e4d68SEugenio Pérez     llend = int128_and(llend, int128_exts64(TARGET_PAGE_MASK));
39032e4d68SEugenio Pérez 
40032e4d68SEugenio Pérez     return llend;
41032e4d68SEugenio Pérez }
42032e4d68SEugenio Pérez 
43013108b6SEugenio Pérez static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection *section,
44013108b6SEugenio Pérez                                                 uint64_t iova_min,
45013108b6SEugenio Pérez                                                 uint64_t iova_max)
46108a6481SCindy Lu {
47013108b6SEugenio Pérez     Int128 llend;
48013108b6SEugenio Pérez 
49013108b6SEugenio Pérez     if ((!memory_region_is_ram(section->mr) &&
50108a6481SCindy Lu          !memory_region_is_iommu(section->mr)) ||
51c64038c9SEugenio Pérez         memory_region_is_protected(section->mr) ||
52d60c75d2SJason Wang         /* vhost-vDPA doesn't allow MMIO to be mapped  */
53013108b6SEugenio Pérez         memory_region_is_ram_device(section->mr)) {
54013108b6SEugenio Pérez         return true;
55013108b6SEugenio Pérez     }
56013108b6SEugenio Pérez 
57013108b6SEugenio Pérez     if (section->offset_within_address_space < iova_min) {
58013108b6SEugenio Pérez         error_report("RAM section out of device range (min=0x%" PRIx64
59013108b6SEugenio Pérez                      ", addr=0x%" HWADDR_PRIx ")",
60013108b6SEugenio Pérez                      iova_min, section->offset_within_address_space);
61013108b6SEugenio Pérez         return true;
62013108b6SEugenio Pérez     }
63013108b6SEugenio Pérez 
64013108b6SEugenio Pérez     llend = vhost_vdpa_section_end(section);
65013108b6SEugenio Pérez     if (int128_gt(llend, int128_make64(iova_max))) {
66013108b6SEugenio Pérez         error_report("RAM section out of device range (max=0x%" PRIx64
67013108b6SEugenio Pérez                      ", end addr=0x%" PRIx64 ")",
68013108b6SEugenio Pérez                      iova_max, int128_get64(llend));
69013108b6SEugenio Pérez         return true;
70013108b6SEugenio Pérez     }
71013108b6SEugenio Pérez 
72013108b6SEugenio Pérez     return false;
73108a6481SCindy Lu }
74108a6481SCindy Lu 
75cd831ed5SEugenio Pérez /*
76cd831ed5SEugenio Pérez  * The caller must set asid = 0 if the device does not support asid.
77cd831ed5SEugenio Pérez  * This is not an ABI break since it is set to 0 by the initializer anyway.
78cd831ed5SEugenio Pérez  */
79cd831ed5SEugenio Pérez int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
80cd831ed5SEugenio Pérez                        hwaddr size, void *vaddr, bool readonly)
81108a6481SCindy Lu {
82386494f2SCindy Lu     struct vhost_msg_v2 msg = {};
83108a6481SCindy Lu     int fd = v->device_fd;
84108a6481SCindy Lu     int ret = 0;
85108a6481SCindy Lu 
86108a6481SCindy Lu     msg.type = v->msg_type;
87cd831ed5SEugenio Pérez     msg.asid = asid;
88108a6481SCindy Lu     msg.iotlb.iova = iova;
89108a6481SCindy Lu     msg.iotlb.size = size;
90108a6481SCindy Lu     msg.iotlb.uaddr = (uint64_t)(uintptr_t)vaddr;
91108a6481SCindy Lu     msg.iotlb.perm = readonly ? VHOST_ACCESS_RO : VHOST_ACCESS_RW;
92108a6481SCindy Lu     msg.iotlb.type = VHOST_IOTLB_UPDATE;
93108a6481SCindy Lu 
94cd831ed5SEugenio Pérez     trace_vhost_vdpa_dma_map(v, fd, msg.type, msg.asid, msg.iotlb.iova,
95cd831ed5SEugenio Pérez                              msg.iotlb.size, msg.iotlb.uaddr, msg.iotlb.perm,
96cd831ed5SEugenio Pérez                              msg.iotlb.type);
97778e67deSLaurent Vivier 
98108a6481SCindy Lu     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
99108a6481SCindy Lu         error_report("failed to write, fd=%d, errno=%d (%s)",
100108a6481SCindy Lu             fd, errno, strerror(errno));
101108a6481SCindy Lu         return -EIO ;
102108a6481SCindy Lu     }
103108a6481SCindy Lu 
104108a6481SCindy Lu     return ret;
105108a6481SCindy Lu }
106108a6481SCindy Lu 
107cd831ed5SEugenio Pérez /*
108cd831ed5SEugenio Pérez  * The caller must set asid = 0 if the device does not support asid.
109cd831ed5SEugenio Pérez  * This is not an ABI break since it is set to 0 by the initializer anyway.
110cd831ed5SEugenio Pérez  */
111cd831ed5SEugenio Pérez int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
112cd831ed5SEugenio Pérez                          hwaddr size)
113108a6481SCindy Lu {
114386494f2SCindy Lu     struct vhost_msg_v2 msg = {};
115108a6481SCindy Lu     int fd = v->device_fd;
116108a6481SCindy Lu     int ret = 0;
117108a6481SCindy Lu 
118108a6481SCindy Lu     msg.type = v->msg_type;
119cd831ed5SEugenio Pérez     msg.asid = asid;
120108a6481SCindy Lu     msg.iotlb.iova = iova;
121108a6481SCindy Lu     msg.iotlb.size = size;
122108a6481SCindy Lu     msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
123108a6481SCindy Lu 
124cd831ed5SEugenio Pérez     trace_vhost_vdpa_dma_unmap(v, fd, msg.type, msg.asid, msg.iotlb.iova,
125778e67deSLaurent Vivier                                msg.iotlb.size, msg.iotlb.type);
126778e67deSLaurent Vivier 
127108a6481SCindy Lu     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
128108a6481SCindy Lu         error_report("failed to write, fd=%d, errno=%d (%s)",
129108a6481SCindy Lu             fd, errno, strerror(errno));
130108a6481SCindy Lu         return -EIO ;
131108a6481SCindy Lu     }
132108a6481SCindy Lu 
133108a6481SCindy Lu     return ret;
134108a6481SCindy Lu }
135108a6481SCindy Lu 
136e6db5df7SEugenio Pérez static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa *v)
137a5bd0580SJason Wang {
138a5bd0580SJason Wang     int fd = v->device_fd;
139e6db5df7SEugenio Pérez     struct vhost_msg_v2 msg = {
140e6db5df7SEugenio Pérez         .type = v->msg_type,
141e6db5df7SEugenio Pérez         .iotlb.type = VHOST_IOTLB_BATCH_BEGIN,
142e6db5df7SEugenio Pérez     };
143a5bd0580SJason Wang 
1445580b9f0SEugenio Pérez     trace_vhost_vdpa_listener_begin_batch(v, fd, msg.type, msg.iotlb.type);
145a5bd0580SJason Wang     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
146a5bd0580SJason Wang         error_report("failed to write, fd=%d, errno=%d (%s)",
147a5bd0580SJason Wang                      fd, errno, strerror(errno));
148a5bd0580SJason Wang     }
149a5bd0580SJason Wang }
150a5bd0580SJason Wang 
151e6db5df7SEugenio Pérez static void vhost_vdpa_iotlb_batch_begin_once(struct vhost_vdpa *v)
152e6db5df7SEugenio Pérez {
153e6db5df7SEugenio Pérez     if (v->dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH) &&
154e6db5df7SEugenio Pérez         !v->iotlb_batch_begin_sent) {
155e6db5df7SEugenio Pérez         vhost_vdpa_listener_begin_batch(v);
156e6db5df7SEugenio Pérez     }
157e6db5df7SEugenio Pérez 
158e6db5df7SEugenio Pérez     v->iotlb_batch_begin_sent = true;
159e6db5df7SEugenio Pérez }
160e6db5df7SEugenio Pérez 
161a5bd0580SJason Wang static void vhost_vdpa_listener_commit(MemoryListener *listener)
162a5bd0580SJason Wang {
163a5bd0580SJason Wang     struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener);
164a5bd0580SJason Wang     struct vhost_dev *dev = v->dev;
1658acb3218SPhilippe Mathieu-Daudé     struct vhost_msg_v2 msg = {};
166a5bd0580SJason Wang     int fd = v->device_fd;
167a5bd0580SJason Wang 
168a5bd0580SJason Wang     if (!(dev->backend_cap & (0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH))) {
169a5bd0580SJason Wang         return;
170a5bd0580SJason Wang     }
171a5bd0580SJason Wang 
172e6db5df7SEugenio Pérez     if (!v->iotlb_batch_begin_sent) {
173e6db5df7SEugenio Pérez         return;
174e6db5df7SEugenio Pérez     }
175e6db5df7SEugenio Pérez 
176a5bd0580SJason Wang     msg.type = v->msg_type;
177a5bd0580SJason Wang     msg.iotlb.type = VHOST_IOTLB_BATCH_END;
178a5bd0580SJason Wang 
1795580b9f0SEugenio Pérez     trace_vhost_vdpa_listener_commit(v, fd, msg.type, msg.iotlb.type);
180a5bd0580SJason Wang     if (write(fd, &msg, sizeof(msg)) != sizeof(msg)) {
181a5bd0580SJason Wang         error_report("failed to write, fd=%d, errno=%d (%s)",
182a5bd0580SJason Wang                      fd, errno, strerror(errno));
183a5bd0580SJason Wang     }
184e6db5df7SEugenio Pérez 
185e6db5df7SEugenio Pérez     v->iotlb_batch_begin_sent = false;
186a5bd0580SJason Wang }
187a5bd0580SJason Wang 
188108a6481SCindy Lu static void vhost_vdpa_listener_region_add(MemoryListener *listener,
189108a6481SCindy Lu                                            MemoryRegionSection *section)
190108a6481SCindy Lu {
1917dab70beSEugenio Pérez     DMAMap mem_region = {};
192108a6481SCindy Lu     struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener);
193108a6481SCindy Lu     hwaddr iova;
194108a6481SCindy Lu     Int128 llend, llsize;
195108a6481SCindy Lu     void *vaddr;
196108a6481SCindy Lu     int ret;
197108a6481SCindy Lu 
198013108b6SEugenio Pérez     if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first,
199013108b6SEugenio Pérez                                             v->iova_range.last)) {
200108a6481SCindy Lu         return;
201108a6481SCindy Lu     }
202108a6481SCindy Lu 
203108a6481SCindy Lu     if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
204108a6481SCindy Lu                  (section->offset_within_region & ~TARGET_PAGE_MASK))) {
205108a6481SCindy Lu         error_report("%s received unaligned region", __func__);
206108a6481SCindy Lu         return;
207108a6481SCindy Lu     }
208108a6481SCindy Lu 
209108a6481SCindy Lu     iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
210032e4d68SEugenio Pérez     llend = vhost_vdpa_section_end(section);
211108a6481SCindy Lu     if (int128_ge(int128_make64(iova), llend)) {
212108a6481SCindy Lu         return;
213108a6481SCindy Lu     }
214108a6481SCindy Lu 
215108a6481SCindy Lu     memory_region_ref(section->mr);
216108a6481SCindy Lu 
217108a6481SCindy Lu     /* Here we assume that memory_region_is_ram(section->mr)==true */
218108a6481SCindy Lu 
219108a6481SCindy Lu     vaddr = memory_region_get_ram_ptr(section->mr) +
220108a6481SCindy Lu             section->offset_within_region +
221108a6481SCindy Lu             (iova - section->offset_within_address_space);
222108a6481SCindy Lu 
223778e67deSLaurent Vivier     trace_vhost_vdpa_listener_region_add(v, iova, int128_get64(llend),
224778e67deSLaurent Vivier                                          vaddr, section->readonly);
225778e67deSLaurent Vivier 
226108a6481SCindy Lu     llsize = int128_sub(llend, int128_make64(iova));
2276188d78aSEugenio Pérez     if (v->shadow_data) {
2287dab70beSEugenio Pérez         int r;
22934e3c94eSEugenio Pérez 
2307dab70beSEugenio Pérez         mem_region.translated_addr = (hwaddr)(uintptr_t)vaddr,
2317dab70beSEugenio Pérez         mem_region.size = int128_get64(llsize) - 1,
2327dab70beSEugenio Pérez         mem_region.perm = IOMMU_ACCESS_FLAG(true, section->readonly),
2337dab70beSEugenio Pérez 
2347dab70beSEugenio Pérez         r = vhost_iova_tree_map_alloc(v->iova_tree, &mem_region);
23534e3c94eSEugenio Pérez         if (unlikely(r != IOVA_OK)) {
23634e3c94eSEugenio Pérez             error_report("Can't allocate a mapping (%d)", r);
23734e3c94eSEugenio Pérez             goto fail;
23834e3c94eSEugenio Pérez         }
23934e3c94eSEugenio Pérez 
24034e3c94eSEugenio Pérez         iova = mem_region.iova;
24134e3c94eSEugenio Pérez     }
242108a6481SCindy Lu 
243e6db5df7SEugenio Pérez     vhost_vdpa_iotlb_batch_begin_once(v);
244cd831ed5SEugenio Pérez     ret = vhost_vdpa_dma_map(v, VHOST_VDPA_GUEST_PA_ASID, iova,
245cd831ed5SEugenio Pérez                              int128_get64(llsize), vaddr, section->readonly);
246108a6481SCindy Lu     if (ret) {
247108a6481SCindy Lu         error_report("vhost vdpa map fail!");
2487dab70beSEugenio Pérez         goto fail_map;
249108a6481SCindy Lu     }
250108a6481SCindy Lu 
251108a6481SCindy Lu     return;
252108a6481SCindy Lu 
2537dab70beSEugenio Pérez fail_map:
2546188d78aSEugenio Pérez     if (v->shadow_data) {
25569292a8eSEugenio Pérez         vhost_iova_tree_remove(v->iova_tree, mem_region);
2567dab70beSEugenio Pérez     }
2577dab70beSEugenio Pérez 
258108a6481SCindy Lu fail:
259108a6481SCindy Lu     /*
260108a6481SCindy Lu      * On the initfn path, store the first error in the container so we
261108a6481SCindy Lu      * can gracefully fail.  Runtime, there's not much we can do other
262108a6481SCindy Lu      * than throw a hardware error.
263108a6481SCindy Lu      */
264108a6481SCindy Lu     error_report("vhost-vdpa: DMA mapping failed, unable to continue");
265108a6481SCindy Lu     return;
266108a6481SCindy Lu 
267108a6481SCindy Lu }
268108a6481SCindy Lu 
269108a6481SCindy Lu static void vhost_vdpa_listener_region_del(MemoryListener *listener,
270108a6481SCindy Lu                                            MemoryRegionSection *section)
271108a6481SCindy Lu {
272108a6481SCindy Lu     struct vhost_vdpa *v = container_of(listener, struct vhost_vdpa, listener);
273108a6481SCindy Lu     hwaddr iova;
274108a6481SCindy Lu     Int128 llend, llsize;
275108a6481SCindy Lu     int ret;
276108a6481SCindy Lu 
277013108b6SEugenio Pérez     if (vhost_vdpa_listener_skipped_section(section, v->iova_range.first,
278013108b6SEugenio Pérez                                             v->iova_range.last)) {
279108a6481SCindy Lu         return;
280108a6481SCindy Lu     }
281108a6481SCindy Lu 
282108a6481SCindy Lu     if (unlikely((section->offset_within_address_space & ~TARGET_PAGE_MASK) !=
283108a6481SCindy Lu                  (section->offset_within_region & ~TARGET_PAGE_MASK))) {
284108a6481SCindy Lu         error_report("%s received unaligned region", __func__);
285108a6481SCindy Lu         return;
286108a6481SCindy Lu     }
287108a6481SCindy Lu 
288108a6481SCindy Lu     iova = TARGET_PAGE_ALIGN(section->offset_within_address_space);
289032e4d68SEugenio Pérez     llend = vhost_vdpa_section_end(section);
290108a6481SCindy Lu 
291778e67deSLaurent Vivier     trace_vhost_vdpa_listener_region_del(v, iova, int128_get64(llend));
292778e67deSLaurent Vivier 
293108a6481SCindy Lu     if (int128_ge(int128_make64(iova), llend)) {
294108a6481SCindy Lu         return;
295108a6481SCindy Lu     }
296108a6481SCindy Lu 
297108a6481SCindy Lu     llsize = int128_sub(llend, int128_make64(iova));
298108a6481SCindy Lu 
2996188d78aSEugenio Pérez     if (v->shadow_data) {
30034e3c94eSEugenio Pérez         const DMAMap *result;
30134e3c94eSEugenio Pérez         const void *vaddr = memory_region_get_ram_ptr(section->mr) +
30234e3c94eSEugenio Pérez             section->offset_within_region +
30334e3c94eSEugenio Pérez             (iova - section->offset_within_address_space);
30434e3c94eSEugenio Pérez         DMAMap mem_region = {
30534e3c94eSEugenio Pérez             .translated_addr = (hwaddr)(uintptr_t)vaddr,
30634e3c94eSEugenio Pérez             .size = int128_get64(llsize) - 1,
30734e3c94eSEugenio Pérez         };
30834e3c94eSEugenio Pérez 
30934e3c94eSEugenio Pérez         result = vhost_iova_tree_find_iova(v->iova_tree, &mem_region);
31010dab9f2SEugenio Pérez         if (!result) {
31110dab9f2SEugenio Pérez             /* The memory listener map wasn't mapped */
31210dab9f2SEugenio Pérez             return;
31310dab9f2SEugenio Pérez         }
31434e3c94eSEugenio Pérez         iova = result->iova;
31569292a8eSEugenio Pérez         vhost_iova_tree_remove(v->iova_tree, *result);
31634e3c94eSEugenio Pérez     }
317e6db5df7SEugenio Pérez     vhost_vdpa_iotlb_batch_begin_once(v);
318cd831ed5SEugenio Pérez     ret = vhost_vdpa_dma_unmap(v, VHOST_VDPA_GUEST_PA_ASID, iova,
319cd831ed5SEugenio Pérez                                int128_get64(llsize));
320108a6481SCindy Lu     if (ret) {
321108a6481SCindy Lu         error_report("vhost_vdpa dma unmap error!");
322108a6481SCindy Lu     }
323108a6481SCindy Lu 
324108a6481SCindy Lu     memory_region_unref(section->mr);
325108a6481SCindy Lu }
326108a6481SCindy Lu /*
327ef4ff56cSStefano Garzarella  * IOTLB API is used by vhost-vdpa which requires incremental updating
328108a6481SCindy Lu  * of the mapping. So we can not use generic vhost memory listener which
329108a6481SCindy Lu  * depends on the addnop().
330108a6481SCindy Lu  */
331108a6481SCindy Lu static const MemoryListener vhost_vdpa_memory_listener = {
332142518bdSPeter Xu     .name = "vhost-vdpa",
333a5bd0580SJason Wang     .commit = vhost_vdpa_listener_commit,
334108a6481SCindy Lu     .region_add = vhost_vdpa_listener_region_add,
335108a6481SCindy Lu     .region_del = vhost_vdpa_listener_region_del,
336108a6481SCindy Lu };
337108a6481SCindy Lu 
338108a6481SCindy Lu static int vhost_vdpa_call(struct vhost_dev *dev, unsigned long int request,
339108a6481SCindy Lu                              void *arg)
340108a6481SCindy Lu {
341108a6481SCindy Lu     struct vhost_vdpa *v = dev->opaque;
342108a6481SCindy Lu     int fd = v->device_fd;
343f2a6e6c4SKevin Wolf     int ret;
344108a6481SCindy Lu 
345108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
346108a6481SCindy Lu 
347f2a6e6c4SKevin Wolf     ret = ioctl(fd, request, arg);
348f2a6e6c4SKevin Wolf     return ret < 0 ? -errno : ret;
349108a6481SCindy Lu }
350108a6481SCindy Lu 
3513631151bSRoman Kagan static int vhost_vdpa_add_status(struct vhost_dev *dev, uint8_t status)
352108a6481SCindy Lu {
353108a6481SCindy Lu     uint8_t s;
3543631151bSRoman Kagan     int ret;
355108a6481SCindy Lu 
356778e67deSLaurent Vivier     trace_vhost_vdpa_add_status(dev, status);
3573631151bSRoman Kagan     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s);
3583631151bSRoman Kagan     if (ret < 0) {
3593631151bSRoman Kagan         return ret;
360108a6481SCindy Lu     }
361108a6481SCindy Lu 
362108a6481SCindy Lu     s |= status;
363108a6481SCindy Lu 
3643631151bSRoman Kagan     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &s);
3653631151bSRoman Kagan     if (ret < 0) {
3663631151bSRoman Kagan         return ret;
3673631151bSRoman Kagan     }
3683631151bSRoman Kagan 
3693631151bSRoman Kagan     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_STATUS, &s);
3703631151bSRoman Kagan     if (ret < 0) {
3713631151bSRoman Kagan         return ret;
3723631151bSRoman Kagan     }
3733631151bSRoman Kagan 
3743631151bSRoman Kagan     if (!(s & status)) {
3753631151bSRoman Kagan         return -EIO;
3763631151bSRoman Kagan     }
3773631151bSRoman Kagan 
3783631151bSRoman Kagan     return 0;
379108a6481SCindy Lu }
380108a6481SCindy Lu 
381c672f348SLongpeng int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_range)
382c672f348SLongpeng {
383c672f348SLongpeng     int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range);
384c672f348SLongpeng 
385c672f348SLongpeng     return ret < 0 ? -errno : 0;
386c672f348SLongpeng }
387c672f348SLongpeng 
388d71b0609SSi-Wei Liu /*
389d71b0609SSi-Wei Liu  * The use of this function is for requests that only need to be
390d71b0609SSi-Wei Liu  * applied once. Typically such request occurs at the beginning
391d71b0609SSi-Wei Liu  * of operation, and before setting up queues. It should not be
392d71b0609SSi-Wei Liu  * used for request that performs operation until all queues are
393d71b0609SSi-Wei Liu  * set, which would need to check dev->vq_index_end instead.
394d71b0609SSi-Wei Liu  */
395d71b0609SSi-Wei Liu static bool vhost_vdpa_first_dev(struct vhost_dev *dev)
3964d191cfdSJason Wang {
3974d191cfdSJason Wang     struct vhost_vdpa *v = dev->opaque;
3984d191cfdSJason Wang 
399d71b0609SSi-Wei Liu     return v->index == 0;
4004d191cfdSJason Wang }
4014d191cfdSJason Wang 
40212a195faSEugenio Pérez static int vhost_vdpa_get_dev_features(struct vhost_dev *dev,
40312a195faSEugenio Pérez                                        uint64_t *features)
40412a195faSEugenio Pérez {
40512a195faSEugenio Pérez     int ret;
40612a195faSEugenio Pérez 
40712a195faSEugenio Pérez     ret = vhost_vdpa_call(dev, VHOST_GET_FEATURES, features);
40812a195faSEugenio Pérez     trace_vhost_vdpa_get_features(dev, *features);
40912a195faSEugenio Pérez     return ret;
41012a195faSEugenio Pérez }
41112a195faSEugenio Pérez 
412258a0394SEugenio Pérez static void vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v)
413dff4426fSEugenio Pérez {
414dff4426fSEugenio Pérez     g_autoptr(GPtrArray) shadow_vqs = NULL;
4154725a418SEugenio Pérez 
416dff4426fSEugenio Pérez     shadow_vqs = g_ptr_array_new_full(hdev->nvqs, vhost_svq_free);
417dff4426fSEugenio Pérez     for (unsigned n = 0; n < hdev->nvqs; ++n) {
4183cfb4d06SEugenio Pérez         VhostShadowVirtqueue *svq;
419dff4426fSEugenio Pérez 
4205fde952bSEugenio Pérez         svq = vhost_svq_new(v->shadow_vq_ops, v->shadow_vq_ops_opaque);
4213cfb4d06SEugenio Pérez         g_ptr_array_add(shadow_vqs, svq);
422dff4426fSEugenio Pérez     }
423dff4426fSEugenio Pérez 
424dff4426fSEugenio Pérez     v->shadow_vqs = g_steal_pointer(&shadow_vqs);
425dff4426fSEugenio Pérez }
426dff4426fSEugenio Pérez 
42728770ff9SKevin Wolf static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)
428108a6481SCindy Lu {
429108a6481SCindy Lu     struct vhost_vdpa *v;
430108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
431778e67deSLaurent Vivier     trace_vhost_vdpa_init(dev, opaque);
432e1c1915bSDavid Hildenbrand     int ret;
433e1c1915bSDavid Hildenbrand 
434e1c1915bSDavid Hildenbrand     /*
435e1c1915bSDavid Hildenbrand      * Similar to VFIO, we end up pinning all guest memory and have to
436e1c1915bSDavid Hildenbrand      * disable discarding of RAM.
437e1c1915bSDavid Hildenbrand      */
438e1c1915bSDavid Hildenbrand     ret = ram_block_discard_disable(true);
439e1c1915bSDavid Hildenbrand     if (ret) {
440e1c1915bSDavid Hildenbrand         error_report("Cannot set discarding of RAM broken");
441e1c1915bSDavid Hildenbrand         return ret;
442e1c1915bSDavid Hildenbrand     }
443108a6481SCindy Lu 
444108a6481SCindy Lu     v = opaque;
445a5bd0580SJason Wang     v->dev = dev;
446108a6481SCindy Lu     dev->opaque =  opaque ;
447108a6481SCindy Lu     v->listener = vhost_vdpa_memory_listener;
448108a6481SCindy Lu     v->msg_type = VHOST_IOTLB_MSG_V2;
449258a0394SEugenio Pérez     vhost_vdpa_init_svq(dev, v);
450108a6481SCindy Lu 
451d71b0609SSi-Wei Liu     if (!vhost_vdpa_first_dev(dev)) {
4524d191cfdSJason Wang         return 0;
4534d191cfdSJason Wang     }
4544d191cfdSJason Wang 
455108a6481SCindy Lu     vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
456108a6481SCindy Lu                                VIRTIO_CONFIG_S_DRIVER);
457108a6481SCindy Lu 
458108a6481SCindy Lu     return 0;
459108a6481SCindy Lu }
460108a6481SCindy Lu 
461d0416d48SJason Wang static void vhost_vdpa_host_notifier_uninit(struct vhost_dev *dev,
462d0416d48SJason Wang                                             int queue_index)
463d0416d48SJason Wang {
4648e3b0cbbSMarc-André Lureau     size_t page_size = qemu_real_host_page_size();
465d0416d48SJason Wang     struct vhost_vdpa *v = dev->opaque;
466d0416d48SJason Wang     VirtIODevice *vdev = dev->vdev;
467d0416d48SJason Wang     VhostVDPAHostNotifier *n;
468d0416d48SJason Wang 
469d0416d48SJason Wang     n = &v->notifier[queue_index];
470d0416d48SJason Wang 
471d0416d48SJason Wang     if (n->addr) {
472d0416d48SJason Wang         virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, false);
473d0416d48SJason Wang         object_unparent(OBJECT(&n->mr));
474d0416d48SJason Wang         munmap(n->addr, page_size);
475d0416d48SJason Wang         n->addr = NULL;
476d0416d48SJason Wang     }
477d0416d48SJason Wang }
478d0416d48SJason Wang 
479d0416d48SJason Wang static int vhost_vdpa_host_notifier_init(struct vhost_dev *dev, int queue_index)
480d0416d48SJason Wang {
4818e3b0cbbSMarc-André Lureau     size_t page_size = qemu_real_host_page_size();
482d0416d48SJason Wang     struct vhost_vdpa *v = dev->opaque;
483d0416d48SJason Wang     VirtIODevice *vdev = dev->vdev;
484d0416d48SJason Wang     VhostVDPAHostNotifier *n;
485d0416d48SJason Wang     int fd = v->device_fd;
486d0416d48SJason Wang     void *addr;
487d0416d48SJason Wang     char *name;
488d0416d48SJason Wang 
489d0416d48SJason Wang     vhost_vdpa_host_notifier_uninit(dev, queue_index);
490d0416d48SJason Wang 
491d0416d48SJason Wang     n = &v->notifier[queue_index];
492d0416d48SJason Wang 
493d0416d48SJason Wang     addr = mmap(NULL, page_size, PROT_WRITE, MAP_SHARED, fd,
494d0416d48SJason Wang                 queue_index * page_size);
495d0416d48SJason Wang     if (addr == MAP_FAILED) {
496d0416d48SJason Wang         goto err;
497d0416d48SJason Wang     }
498d0416d48SJason Wang 
499d0416d48SJason Wang     name = g_strdup_printf("vhost-vdpa/host-notifier@%p mmaps[%d]",
500d0416d48SJason Wang                            v, queue_index);
501d0416d48SJason Wang     memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name,
502d0416d48SJason Wang                                       page_size, addr);
503d0416d48SJason Wang     g_free(name);
504d0416d48SJason Wang 
505d0416d48SJason Wang     if (virtio_queue_set_host_notifier_mr(vdev, queue_index, &n->mr, true)) {
50698f7607eSLaurent Vivier         object_unparent(OBJECT(&n->mr));
507d0416d48SJason Wang         munmap(addr, page_size);
508d0416d48SJason Wang         goto err;
509d0416d48SJason Wang     }
510d0416d48SJason Wang     n->addr = addr;
511d0416d48SJason Wang 
512d0416d48SJason Wang     return 0;
513d0416d48SJason Wang 
514d0416d48SJason Wang err:
515d0416d48SJason Wang     return -1;
516d0416d48SJason Wang }
517d0416d48SJason Wang 
518b1f030a0SLaurent Vivier static void vhost_vdpa_host_notifiers_uninit(struct vhost_dev *dev, int n)
519b1f030a0SLaurent Vivier {
520b1f030a0SLaurent Vivier     int i;
521b1f030a0SLaurent Vivier 
522e66f2311SLongpeng     /*
523e66f2311SLongpeng      * Pack all the changes to the memory regions in a single
524e66f2311SLongpeng      * transaction to avoid a few updating of the address space
525e66f2311SLongpeng      * topology.
526e66f2311SLongpeng      */
527e66f2311SLongpeng     memory_region_transaction_begin();
528e66f2311SLongpeng 
529b1f030a0SLaurent Vivier     for (i = dev->vq_index; i < dev->vq_index + n; i++) {
530b1f030a0SLaurent Vivier         vhost_vdpa_host_notifier_uninit(dev, i);
531b1f030a0SLaurent Vivier     }
532e66f2311SLongpeng 
533e66f2311SLongpeng     memory_region_transaction_commit();
534b1f030a0SLaurent Vivier }
535b1f030a0SLaurent Vivier 
536d0416d48SJason Wang static void vhost_vdpa_host_notifiers_init(struct vhost_dev *dev)
537d0416d48SJason Wang {
538dff4426fSEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
539d0416d48SJason Wang     int i;
540d0416d48SJason Wang 
541dff4426fSEugenio Pérez     if (v->shadow_vqs_enabled) {
542dff4426fSEugenio Pérez         /* FIXME SVQ is not compatible with host notifiers mr */
543dff4426fSEugenio Pérez         return;
544dff4426fSEugenio Pérez     }
545dff4426fSEugenio Pérez 
546e66f2311SLongpeng     /*
547e66f2311SLongpeng      * Pack all the changes to the memory regions in a single
548e66f2311SLongpeng      * transaction to avoid a few updating of the address space
549e66f2311SLongpeng      * topology.
550e66f2311SLongpeng      */
551e66f2311SLongpeng     memory_region_transaction_begin();
552e66f2311SLongpeng 
553d0416d48SJason Wang     for (i = dev->vq_index; i < dev->vq_index + dev->nvqs; i++) {
554d0416d48SJason Wang         if (vhost_vdpa_host_notifier_init(dev, i)) {
555b1f030a0SLaurent Vivier             vhost_vdpa_host_notifiers_uninit(dev, i - dev->vq_index);
556e66f2311SLongpeng             break;
557e66f2311SLongpeng         }
558e66f2311SLongpeng     }
559e66f2311SLongpeng 
560e66f2311SLongpeng     memory_region_transaction_commit();
561d0416d48SJason Wang }
562d0416d48SJason Wang 
563dff4426fSEugenio Pérez static void vhost_vdpa_svq_cleanup(struct vhost_dev *dev)
564dff4426fSEugenio Pérez {
565dff4426fSEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
566dff4426fSEugenio Pérez     size_t idx;
567dff4426fSEugenio Pérez 
568dff4426fSEugenio Pérez     for (idx = 0; idx < v->shadow_vqs->len; ++idx) {
569dff4426fSEugenio Pérez         vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, idx));
570dff4426fSEugenio Pérez     }
571dff4426fSEugenio Pérez     g_ptr_array_free(v->shadow_vqs, true);
572dff4426fSEugenio Pérez }
573dff4426fSEugenio Pérez 
574108a6481SCindy Lu static int vhost_vdpa_cleanup(struct vhost_dev *dev)
575108a6481SCindy Lu {
576108a6481SCindy Lu     struct vhost_vdpa *v;
577108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
578108a6481SCindy Lu     v = dev->opaque;
579778e67deSLaurent Vivier     trace_vhost_vdpa_cleanup(dev, v);
580d0416d48SJason Wang     vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
581108a6481SCindy Lu     memory_listener_unregister(&v->listener);
582dff4426fSEugenio Pérez     vhost_vdpa_svq_cleanup(dev);
583108a6481SCindy Lu 
584108a6481SCindy Lu     dev->opaque = NULL;
585e1c1915bSDavid Hildenbrand     ram_block_discard_disable(false);
586e1c1915bSDavid Hildenbrand 
587108a6481SCindy Lu     return 0;
588108a6481SCindy Lu }
589108a6481SCindy Lu 
590108a6481SCindy Lu static int vhost_vdpa_memslots_limit(struct vhost_dev *dev)
591108a6481SCindy Lu {
592778e67deSLaurent Vivier     trace_vhost_vdpa_memslots_limit(dev, INT_MAX);
593108a6481SCindy Lu     return INT_MAX;
594108a6481SCindy Lu }
595108a6481SCindy Lu 
596108a6481SCindy Lu static int vhost_vdpa_set_mem_table(struct vhost_dev *dev,
597108a6481SCindy Lu                                     struct vhost_memory *mem)
598108a6481SCindy Lu {
599d71b0609SSi-Wei Liu     if (!vhost_vdpa_first_dev(dev)) {
6004d191cfdSJason Wang         return 0;
6014d191cfdSJason Wang     }
6024d191cfdSJason Wang 
603778e67deSLaurent Vivier     trace_vhost_vdpa_set_mem_table(dev, mem->nregions, mem->padding);
604778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_MEM_TABLE) &&
605778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_REGIONS)) {
606778e67deSLaurent Vivier         int i;
607778e67deSLaurent Vivier         for (i = 0; i < mem->nregions; i++) {
608778e67deSLaurent Vivier             trace_vhost_vdpa_dump_regions(dev, i,
609778e67deSLaurent Vivier                                           mem->regions[i].guest_phys_addr,
610778e67deSLaurent Vivier                                           mem->regions[i].memory_size,
611778e67deSLaurent Vivier                                           mem->regions[i].userspace_addr,
612778e67deSLaurent Vivier                                           mem->regions[i].flags_padding);
613778e67deSLaurent Vivier         }
614778e67deSLaurent Vivier     }
615108a6481SCindy Lu     if (mem->padding) {
6163631151bSRoman Kagan         return -EINVAL;
617108a6481SCindy Lu     }
618108a6481SCindy Lu 
619108a6481SCindy Lu     return 0;
620108a6481SCindy Lu }
621108a6481SCindy Lu 
622108a6481SCindy Lu static int vhost_vdpa_set_features(struct vhost_dev *dev,
623108a6481SCindy Lu                                    uint64_t features)
624108a6481SCindy Lu {
62512a195faSEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
626108a6481SCindy Lu     int ret;
6274d191cfdSJason Wang 
628d71b0609SSi-Wei Liu     if (!vhost_vdpa_first_dev(dev)) {
6294d191cfdSJason Wang         return 0;
6304d191cfdSJason Wang     }
6314d191cfdSJason Wang 
63212a195faSEugenio Pérez     if (v->shadow_vqs_enabled) {
63312a195faSEugenio Pérez         if ((v->acked_features ^ features) == BIT_ULL(VHOST_F_LOG_ALL)) {
63412a195faSEugenio Pérez             /*
63512a195faSEugenio Pérez              * QEMU is just trying to enable or disable logging. SVQ handles
63612a195faSEugenio Pérez              * this sepparately, so no need to forward this.
63712a195faSEugenio Pérez              */
63812a195faSEugenio Pérez             v->acked_features = features;
63912a195faSEugenio Pérez             return 0;
64012a195faSEugenio Pérez         }
64112a195faSEugenio Pérez 
64212a195faSEugenio Pérez         v->acked_features = features;
64312a195faSEugenio Pérez 
64412a195faSEugenio Pérez         /* We must not ack _F_LOG if SVQ is enabled */
64512a195faSEugenio Pérez         features &= ~BIT_ULL(VHOST_F_LOG_ALL);
64612a195faSEugenio Pérez     }
64712a195faSEugenio Pérez 
648778e67deSLaurent Vivier     trace_vhost_vdpa_set_features(dev, features);
649108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_SET_FEATURES, &features);
650108a6481SCindy Lu     if (ret) {
651108a6481SCindy Lu         return ret;
652108a6481SCindy Lu     }
653108a6481SCindy Lu 
6543631151bSRoman Kagan     return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
655108a6481SCindy Lu }
656108a6481SCindy Lu 
657a5bd0580SJason Wang static int vhost_vdpa_set_backend_cap(struct vhost_dev *dev)
658a5bd0580SJason Wang {
659a5bd0580SJason Wang     uint64_t features;
660a5bd0580SJason Wang     uint64_t f = 0x1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 |
661c1a10086SEugenio Pérez         0x1ULL << VHOST_BACKEND_F_IOTLB_BATCH |
662*d83b4945SEugenio Pérez         0x1ULL << VHOST_BACKEND_F_IOTLB_ASID |
663*d83b4945SEugenio Pérez         0x1ULL << VHOST_BACKEND_F_SUSPEND;
664a5bd0580SJason Wang     int r;
665a5bd0580SJason Wang 
666a5bd0580SJason Wang     if (vhost_vdpa_call(dev, VHOST_GET_BACKEND_FEATURES, &features)) {
6672a83e97eSJason Wang         return -EFAULT;
668a5bd0580SJason Wang     }
669a5bd0580SJason Wang 
670a5bd0580SJason Wang     features &= f;
6714d191cfdSJason Wang 
672d71b0609SSi-Wei Liu     if (vhost_vdpa_first_dev(dev)) {
673a5bd0580SJason Wang         r = vhost_vdpa_call(dev, VHOST_SET_BACKEND_FEATURES, &features);
674a5bd0580SJason Wang         if (r) {
6752a83e97eSJason Wang             return -EFAULT;
676a5bd0580SJason Wang         }
6774d191cfdSJason Wang     }
678a5bd0580SJason Wang 
679a5bd0580SJason Wang     dev->backend_cap = features;
680a5bd0580SJason Wang 
681a5bd0580SJason Wang     return 0;
682a5bd0580SJason Wang }
683a5bd0580SJason Wang 
684c232b8f4SZenghui Yu static int vhost_vdpa_get_device_id(struct vhost_dev *dev,
685108a6481SCindy Lu                                     uint32_t *device_id)
686108a6481SCindy Lu {
687778e67deSLaurent Vivier     int ret;
688778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_DEVICE_ID, device_id);
689778e67deSLaurent Vivier     trace_vhost_vdpa_get_device_id(dev, *device_id);
690778e67deSLaurent Vivier     return ret;
691108a6481SCindy Lu }
692108a6481SCindy Lu 
693108a6481SCindy Lu static int vhost_vdpa_reset_device(struct vhost_dev *dev)
694108a6481SCindy Lu {
695778e67deSLaurent Vivier     int ret;
696108a6481SCindy Lu     uint8_t status = 0;
697108a6481SCindy Lu 
698778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_STATUS, &status);
699778e67deSLaurent Vivier     trace_vhost_vdpa_reset_device(dev, status);
700778e67deSLaurent Vivier     return ret;
701108a6481SCindy Lu }
702108a6481SCindy Lu 
703108a6481SCindy Lu static int vhost_vdpa_get_vq_index(struct vhost_dev *dev, int idx)
704108a6481SCindy Lu {
705108a6481SCindy Lu     assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
706108a6481SCindy Lu 
707353244d8SJason Wang     trace_vhost_vdpa_get_vq_index(dev, idx, idx);
708353244d8SJason Wang     return idx;
709108a6481SCindy Lu }
710108a6481SCindy Lu 
711108a6481SCindy Lu static int vhost_vdpa_set_vring_ready(struct vhost_dev *dev)
712108a6481SCindy Lu {
713108a6481SCindy Lu     int i;
714778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_ready(dev);
715108a6481SCindy Lu     for (i = 0; i < dev->nvqs; ++i) {
716108a6481SCindy Lu         struct vhost_vring_state state = {
717108a6481SCindy Lu             .index = dev->vq_index + i,
718108a6481SCindy Lu             .num = 1,
719108a6481SCindy Lu         };
720108a6481SCindy Lu         vhost_vdpa_call(dev, VHOST_VDPA_SET_VRING_ENABLE, &state);
721108a6481SCindy Lu     }
722108a6481SCindy Lu     return 0;
723108a6481SCindy Lu }
724108a6481SCindy Lu 
725259f3accSCindy Lu static int vhost_vdpa_set_config_call(struct vhost_dev *dev,
726259f3accSCindy Lu                                        int fd)
727259f3accSCindy Lu {
728259f3accSCindy Lu     trace_vhost_vdpa_set_config_call(dev, fd);
729259f3accSCindy Lu     return vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG_CALL, &fd);
730259f3accSCindy Lu }
731259f3accSCindy Lu 
732778e67deSLaurent Vivier static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config,
733778e67deSLaurent Vivier                                    uint32_t config_len)
734778e67deSLaurent Vivier {
735778e67deSLaurent Vivier     int b, len;
736778e67deSLaurent Vivier     char line[QEMU_HEXDUMP_LINE_LEN];
737778e67deSLaurent Vivier 
738778e67deSLaurent Vivier     for (b = 0; b < config_len; b += 16) {
739778e67deSLaurent Vivier         len = config_len - b;
740778e67deSLaurent Vivier         qemu_hexdump_line(line, b, config, len, false);
741778e67deSLaurent Vivier         trace_vhost_vdpa_dump_config(dev, line);
742778e67deSLaurent Vivier     }
743778e67deSLaurent Vivier }
744778e67deSLaurent Vivier 
745108a6481SCindy Lu static int vhost_vdpa_set_config(struct vhost_dev *dev, const uint8_t *data,
746108a6481SCindy Lu                                    uint32_t offset, uint32_t size,
747108a6481SCindy Lu                                    uint32_t flags)
748108a6481SCindy Lu {
749108a6481SCindy Lu     struct vhost_vdpa_config *config;
750108a6481SCindy Lu     int ret;
751108a6481SCindy Lu     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
752986d4f78SLi Qiang 
753778e67deSLaurent Vivier     trace_vhost_vdpa_set_config(dev, offset, size, flags);
754108a6481SCindy Lu     config = g_malloc(size + config_size);
755108a6481SCindy Lu     config->off = offset;
756108a6481SCindy Lu     config->len = size;
757108a6481SCindy Lu     memcpy(config->buf, data, size);
758778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_SET_CONFIG) &&
759778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) {
760778e67deSLaurent Vivier         vhost_vdpa_dump_config(dev, data, size);
761778e67deSLaurent Vivier     }
762108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_VDPA_SET_CONFIG, config);
763108a6481SCindy Lu     g_free(config);
764108a6481SCindy Lu     return ret;
765108a6481SCindy Lu }
766108a6481SCindy Lu 
767108a6481SCindy Lu static int vhost_vdpa_get_config(struct vhost_dev *dev, uint8_t *config,
76850de5138SKevin Wolf                                    uint32_t config_len, Error **errp)
769108a6481SCindy Lu {
770108a6481SCindy Lu     struct vhost_vdpa_config *v_config;
771108a6481SCindy Lu     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
772108a6481SCindy Lu     int ret;
773108a6481SCindy Lu 
774778e67deSLaurent Vivier     trace_vhost_vdpa_get_config(dev, config, config_len);
775108a6481SCindy Lu     v_config = g_malloc(config_len + config_size);
776108a6481SCindy Lu     v_config->len = config_len;
777108a6481SCindy Lu     v_config->off = 0;
778108a6481SCindy Lu     ret = vhost_vdpa_call(dev, VHOST_VDPA_GET_CONFIG, v_config);
779108a6481SCindy Lu     memcpy(config, v_config->buf, config_len);
780108a6481SCindy Lu     g_free(v_config);
781778e67deSLaurent Vivier     if (trace_event_get_state_backends(TRACE_VHOST_VDPA_GET_CONFIG) &&
782778e67deSLaurent Vivier         trace_event_get_state_backends(TRACE_VHOST_VDPA_DUMP_CONFIG)) {
783778e67deSLaurent Vivier         vhost_vdpa_dump_config(dev, config, config_len);
784778e67deSLaurent Vivier     }
785108a6481SCindy Lu     return ret;
786108a6481SCindy Lu  }
787108a6481SCindy Lu 
788d96be4c8SEugenio Pérez static int vhost_vdpa_set_dev_vring_base(struct vhost_dev *dev,
789d96be4c8SEugenio Pérez                                          struct vhost_vring_state *ring)
790d96be4c8SEugenio Pérez {
791d96be4c8SEugenio Pérez     trace_vhost_vdpa_set_vring_base(dev, ring->index, ring->num);
792d96be4c8SEugenio Pérez     return vhost_vdpa_call(dev, VHOST_SET_VRING_BASE, ring);
793d96be4c8SEugenio Pérez }
794d96be4c8SEugenio Pérez 
795dff4426fSEugenio Pérez static int vhost_vdpa_set_vring_dev_kick(struct vhost_dev *dev,
796dff4426fSEugenio Pérez                                          struct vhost_vring_file *file)
797dff4426fSEugenio Pérez {
798dff4426fSEugenio Pérez     trace_vhost_vdpa_set_vring_kick(dev, file->index, file->fd);
799dff4426fSEugenio Pérez     return vhost_vdpa_call(dev, VHOST_SET_VRING_KICK, file);
800dff4426fSEugenio Pérez }
801dff4426fSEugenio Pérez 
802a8ac8858SEugenio Pérez static int vhost_vdpa_set_vring_dev_call(struct vhost_dev *dev,
803a8ac8858SEugenio Pérez                                          struct vhost_vring_file *file)
804a8ac8858SEugenio Pérez {
805a8ac8858SEugenio Pérez     trace_vhost_vdpa_set_vring_call(dev, file->index, file->fd);
806a8ac8858SEugenio Pérez     return vhost_vdpa_call(dev, VHOST_SET_VRING_CALL, file);
807a8ac8858SEugenio Pérez }
808a8ac8858SEugenio Pérez 
809d96be4c8SEugenio Pérez static int vhost_vdpa_set_vring_dev_addr(struct vhost_dev *dev,
810d96be4c8SEugenio Pérez                                          struct vhost_vring_addr *addr)
811d96be4c8SEugenio Pérez {
812d96be4c8SEugenio Pérez     trace_vhost_vdpa_set_vring_addr(dev, addr->index, addr->flags,
813d96be4c8SEugenio Pérez                                 addr->desc_user_addr, addr->used_user_addr,
814d96be4c8SEugenio Pérez                                 addr->avail_user_addr,
815d96be4c8SEugenio Pérez                                 addr->log_guest_addr);
816d96be4c8SEugenio Pérez 
817d96be4c8SEugenio Pérez     return vhost_vdpa_call(dev, VHOST_SET_VRING_ADDR, addr);
818d96be4c8SEugenio Pérez 
819d96be4c8SEugenio Pérez }
820d96be4c8SEugenio Pérez 
821dff4426fSEugenio Pérez /**
822dff4426fSEugenio Pérez  * Set the shadow virtqueue descriptors to the device
823dff4426fSEugenio Pérez  *
824dff4426fSEugenio Pérez  * @dev: The vhost device model
825dff4426fSEugenio Pérez  * @svq: The shadow virtqueue
826dff4426fSEugenio Pérez  * @idx: The index of the virtqueue in the vhost device
827dff4426fSEugenio Pérez  * @errp: Error
828a8ac8858SEugenio Pérez  *
829a8ac8858SEugenio Pérez  * Note that this function does not rewind kick file descriptor if cannot set
830a8ac8858SEugenio Pérez  * call one.
831dff4426fSEugenio Pérez  */
832100890f7SEugenio Pérez static int vhost_vdpa_svq_set_fds(struct vhost_dev *dev,
833dff4426fSEugenio Pérez                                   VhostShadowVirtqueue *svq, unsigned idx,
834dff4426fSEugenio Pérez                                   Error **errp)
835dff4426fSEugenio Pérez {
836dff4426fSEugenio Pérez     struct vhost_vring_file file = {
837dff4426fSEugenio Pérez         .index = dev->vq_index + idx,
838dff4426fSEugenio Pérez     };
839dff4426fSEugenio Pérez     const EventNotifier *event_notifier = &svq->hdev_kick;
840dff4426fSEugenio Pérez     int r;
841dff4426fSEugenio Pérez 
8423cfb4d06SEugenio Pérez     r = event_notifier_init(&svq->hdev_kick, 0);
8433cfb4d06SEugenio Pérez     if (r != 0) {
8443cfb4d06SEugenio Pérez         error_setg_errno(errp, -r, "Couldn't create kick event notifier");
8453cfb4d06SEugenio Pérez         goto err_init_hdev_kick;
8463cfb4d06SEugenio Pérez     }
8473cfb4d06SEugenio Pérez 
8483cfb4d06SEugenio Pérez     r = event_notifier_init(&svq->hdev_call, 0);
8493cfb4d06SEugenio Pérez     if (r != 0) {
8503cfb4d06SEugenio Pérez         error_setg_errno(errp, -r, "Couldn't create call event notifier");
8513cfb4d06SEugenio Pérez         goto err_init_hdev_call;
8523cfb4d06SEugenio Pérez     }
8533cfb4d06SEugenio Pérez 
854dff4426fSEugenio Pérez     file.fd = event_notifier_get_fd(event_notifier);
855dff4426fSEugenio Pérez     r = vhost_vdpa_set_vring_dev_kick(dev, &file);
856dff4426fSEugenio Pérez     if (unlikely(r != 0)) {
857dff4426fSEugenio Pérez         error_setg_errno(errp, -r, "Can't set device kick fd");
8583cfb4d06SEugenio Pérez         goto err_init_set_dev_fd;
859a8ac8858SEugenio Pérez     }
860a8ac8858SEugenio Pérez 
861a8ac8858SEugenio Pérez     event_notifier = &svq->hdev_call;
862a8ac8858SEugenio Pérez     file.fd = event_notifier_get_fd(event_notifier);
863a8ac8858SEugenio Pérez     r = vhost_vdpa_set_vring_dev_call(dev, &file);
864a8ac8858SEugenio Pérez     if (unlikely(r != 0)) {
865a8ac8858SEugenio Pérez         error_setg_errno(errp, -r, "Can't set device call fd");
8663cfb4d06SEugenio Pérez         goto err_init_set_dev_fd;
867dff4426fSEugenio Pérez     }
868dff4426fSEugenio Pérez 
8693cfb4d06SEugenio Pérez     return 0;
8703cfb4d06SEugenio Pérez 
8713cfb4d06SEugenio Pérez err_init_set_dev_fd:
8723cfb4d06SEugenio Pérez     event_notifier_set_handler(&svq->hdev_call, NULL);
8733cfb4d06SEugenio Pérez 
8743cfb4d06SEugenio Pérez err_init_hdev_call:
8753cfb4d06SEugenio Pérez     event_notifier_cleanup(&svq->hdev_kick);
8763cfb4d06SEugenio Pérez 
8773cfb4d06SEugenio Pérez err_init_hdev_kick:
878100890f7SEugenio Pérez     return r;
879100890f7SEugenio Pérez }
880100890f7SEugenio Pérez 
881100890f7SEugenio Pérez /**
882100890f7SEugenio Pérez  * Unmap a SVQ area in the device
883100890f7SEugenio Pérez  */
8848b6d6119SEugenio Pérez static void vhost_vdpa_svq_unmap_ring(struct vhost_vdpa *v, hwaddr addr)
885100890f7SEugenio Pérez {
8868b6d6119SEugenio Pérez     const DMAMap needle = {
8878b6d6119SEugenio Pérez         .translated_addr = addr,
8888b6d6119SEugenio Pérez     };
8898b6d6119SEugenio Pérez     const DMAMap *result = vhost_iova_tree_find_iova(v->iova_tree, &needle);
89034e3c94eSEugenio Pérez     hwaddr size;
891100890f7SEugenio Pérez     int r;
892100890f7SEugenio Pérez 
89334e3c94eSEugenio Pérez     if (unlikely(!result)) {
89434e3c94eSEugenio Pérez         error_report("Unable to find SVQ address to unmap");
8955b590f51SEugenio Pérez         return;
89634e3c94eSEugenio Pérez     }
89734e3c94eSEugenio Pérez 
8988e3b0cbbSMarc-André Lureau     size = ROUND_UP(result->size, qemu_real_host_page_size());
899cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_unmap(v, v->address_space_id, result->iova, size);
900b37c12beSEugenio Pérez     if (unlikely(r < 0)) {
901b37c12beSEugenio Pérez         error_report("Unable to unmap SVQ vring: %s (%d)", g_strerror(-r), -r);
9025b590f51SEugenio Pérez         return;
903b37c12beSEugenio Pérez     }
904b37c12beSEugenio Pérez 
905b37c12beSEugenio Pérez     vhost_iova_tree_remove(v->iova_tree, *result);
906100890f7SEugenio Pérez }
907100890f7SEugenio Pérez 
9085b590f51SEugenio Pérez static void vhost_vdpa_svq_unmap_rings(struct vhost_dev *dev,
909100890f7SEugenio Pérez                                        const VhostShadowVirtqueue *svq)
910100890f7SEugenio Pérez {
911100890f7SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
912100890f7SEugenio Pérez     struct vhost_vring_addr svq_addr;
913100890f7SEugenio Pérez 
914100890f7SEugenio Pérez     vhost_svq_get_vring_addr(svq, &svq_addr);
915100890f7SEugenio Pérez 
9168b6d6119SEugenio Pérez     vhost_vdpa_svq_unmap_ring(v, svq_addr.desc_user_addr);
917100890f7SEugenio Pérez 
9188b6d6119SEugenio Pérez     vhost_vdpa_svq_unmap_ring(v, svq_addr.used_user_addr);
91934e3c94eSEugenio Pérez }
92034e3c94eSEugenio Pérez 
92134e3c94eSEugenio Pérez /**
92234e3c94eSEugenio Pérez  * Map the SVQ area in the device
92334e3c94eSEugenio Pérez  *
92434e3c94eSEugenio Pérez  * @v: Vhost-vdpa device
92534e3c94eSEugenio Pérez  * @needle: The area to search iova
92634e3c94eSEugenio Pérez  * @errorp: Error pointer
92734e3c94eSEugenio Pérez  */
92834e3c94eSEugenio Pérez static bool vhost_vdpa_svq_map_ring(struct vhost_vdpa *v, DMAMap *needle,
92934e3c94eSEugenio Pérez                                     Error **errp)
93034e3c94eSEugenio Pérez {
93134e3c94eSEugenio Pérez     int r;
93234e3c94eSEugenio Pérez 
93334e3c94eSEugenio Pérez     r = vhost_iova_tree_map_alloc(v->iova_tree, needle);
93434e3c94eSEugenio Pérez     if (unlikely(r != IOVA_OK)) {
93534e3c94eSEugenio Pérez         error_setg(errp, "Cannot allocate iova (%d)", r);
93634e3c94eSEugenio Pérez         return false;
93734e3c94eSEugenio Pérez     }
93834e3c94eSEugenio Pérez 
939cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_map(v, v->address_space_id, needle->iova,
940cd831ed5SEugenio Pérez                            needle->size + 1,
94134e3c94eSEugenio Pérez                            (void *)(uintptr_t)needle->translated_addr,
94234e3c94eSEugenio Pérez                            needle->perm == IOMMU_RO);
94334e3c94eSEugenio Pérez     if (unlikely(r != 0)) {
94434e3c94eSEugenio Pérez         error_setg_errno(errp, -r, "Cannot map region to device");
94569292a8eSEugenio Pérez         vhost_iova_tree_remove(v->iova_tree, *needle);
94634e3c94eSEugenio Pérez     }
94734e3c94eSEugenio Pérez 
94834e3c94eSEugenio Pérez     return r == 0;
949100890f7SEugenio Pérez }
950100890f7SEugenio Pérez 
951100890f7SEugenio Pérez /**
952100890f7SEugenio Pérez  * Map the shadow virtqueue rings in the device
953100890f7SEugenio Pérez  *
954100890f7SEugenio Pérez  * @dev: The vhost device
955100890f7SEugenio Pérez  * @svq: The shadow virtqueue
956100890f7SEugenio Pérez  * @addr: Assigned IOVA addresses
957100890f7SEugenio Pérez  * @errp: Error pointer
958100890f7SEugenio Pérez  */
959100890f7SEugenio Pérez static bool vhost_vdpa_svq_map_rings(struct vhost_dev *dev,
960100890f7SEugenio Pérez                                      const VhostShadowVirtqueue *svq,
961100890f7SEugenio Pérez                                      struct vhost_vring_addr *addr,
962100890f7SEugenio Pérez                                      Error **errp)
963100890f7SEugenio Pérez {
96405e385d2SMarkus Armbruster     ERRP_GUARD();
96534e3c94eSEugenio Pérez     DMAMap device_region, driver_region;
96634e3c94eSEugenio Pérez     struct vhost_vring_addr svq_addr;
967100890f7SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
968100890f7SEugenio Pérez     size_t device_size = vhost_svq_device_area_size(svq);
969100890f7SEugenio Pérez     size_t driver_size = vhost_svq_driver_area_size(svq);
97034e3c94eSEugenio Pérez     size_t avail_offset;
97134e3c94eSEugenio Pérez     bool ok;
972100890f7SEugenio Pérez 
97334e3c94eSEugenio Pérez     vhost_svq_get_vring_addr(svq, &svq_addr);
974100890f7SEugenio Pérez 
97534e3c94eSEugenio Pérez     driver_region = (DMAMap) {
97634e3c94eSEugenio Pérez         .translated_addr = svq_addr.desc_user_addr,
97734e3c94eSEugenio Pérez         .size = driver_size - 1,
97834e3c94eSEugenio Pérez         .perm = IOMMU_RO,
97934e3c94eSEugenio Pérez     };
98034e3c94eSEugenio Pérez     ok = vhost_vdpa_svq_map_ring(v, &driver_region, errp);
98134e3c94eSEugenio Pérez     if (unlikely(!ok)) {
98234e3c94eSEugenio Pérez         error_prepend(errp, "Cannot create vq driver region: ");
983100890f7SEugenio Pérez         return false;
984100890f7SEugenio Pérez     }
98534e3c94eSEugenio Pérez     addr->desc_user_addr = driver_region.iova;
98634e3c94eSEugenio Pérez     avail_offset = svq_addr.avail_user_addr - svq_addr.desc_user_addr;
98734e3c94eSEugenio Pérez     addr->avail_user_addr = driver_region.iova + avail_offset;
988100890f7SEugenio Pérez 
98934e3c94eSEugenio Pérez     device_region = (DMAMap) {
99034e3c94eSEugenio Pérez         .translated_addr = svq_addr.used_user_addr,
99134e3c94eSEugenio Pérez         .size = device_size - 1,
99234e3c94eSEugenio Pérez         .perm = IOMMU_RW,
99334e3c94eSEugenio Pérez     };
99434e3c94eSEugenio Pérez     ok = vhost_vdpa_svq_map_ring(v, &device_region, errp);
99534e3c94eSEugenio Pérez     if (unlikely(!ok)) {
99634e3c94eSEugenio Pérez         error_prepend(errp, "Cannot create vq device region: ");
9978b6d6119SEugenio Pérez         vhost_vdpa_svq_unmap_ring(v, driver_region.translated_addr);
998100890f7SEugenio Pérez     }
99934e3c94eSEugenio Pérez     addr->used_user_addr = device_region.iova;
1000100890f7SEugenio Pérez 
100134e3c94eSEugenio Pérez     return ok;
1002100890f7SEugenio Pérez }
1003100890f7SEugenio Pérez 
1004100890f7SEugenio Pérez static bool vhost_vdpa_svq_setup(struct vhost_dev *dev,
1005100890f7SEugenio Pérez                                  VhostShadowVirtqueue *svq, unsigned idx,
1006100890f7SEugenio Pérez                                  Error **errp)
1007100890f7SEugenio Pérez {
1008100890f7SEugenio Pérez     uint16_t vq_index = dev->vq_index + idx;
1009100890f7SEugenio Pérez     struct vhost_vring_state s = {
1010100890f7SEugenio Pérez         .index = vq_index,
1011100890f7SEugenio Pérez     };
1012100890f7SEugenio Pérez     int r;
1013100890f7SEugenio Pérez 
1014100890f7SEugenio Pérez     r = vhost_vdpa_set_dev_vring_base(dev, &s);
1015100890f7SEugenio Pérez     if (unlikely(r)) {
1016100890f7SEugenio Pérez         error_setg_errno(errp, -r, "Cannot set vring base");
1017100890f7SEugenio Pérez         return false;
1018100890f7SEugenio Pérez     }
1019100890f7SEugenio Pérez 
1020100890f7SEugenio Pérez     r = vhost_vdpa_svq_set_fds(dev, svq, idx, errp);
1021dff4426fSEugenio Pérez     return r == 0;
1022dff4426fSEugenio Pérez }
1023dff4426fSEugenio Pérez 
1024dff4426fSEugenio Pérez static bool vhost_vdpa_svqs_start(struct vhost_dev *dev)
1025dff4426fSEugenio Pérez {
1026dff4426fSEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
1027dff4426fSEugenio Pérez     Error *err = NULL;
1028dff4426fSEugenio Pérez     unsigned i;
1029dff4426fSEugenio Pérez 
1030712c1a31SEugenio Pérez     if (!v->shadow_vqs_enabled) {
1031dff4426fSEugenio Pérez         return true;
1032dff4426fSEugenio Pérez     }
1033dff4426fSEugenio Pérez 
1034dff4426fSEugenio Pérez     for (i = 0; i < v->shadow_vqs->len; ++i) {
1035100890f7SEugenio Pérez         VirtQueue *vq = virtio_get_queue(dev->vdev, dev->vq_index + i);
1036dff4426fSEugenio Pérez         VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i);
1037100890f7SEugenio Pérez         struct vhost_vring_addr addr = {
10381c82fdfeSEugenio Pérez             .index = dev->vq_index + i,
1039100890f7SEugenio Pérez         };
1040100890f7SEugenio Pérez         int r;
1041dff4426fSEugenio Pérez         bool ok = vhost_vdpa_svq_setup(dev, svq, i, &err);
1042dff4426fSEugenio Pérez         if (unlikely(!ok)) {
1043100890f7SEugenio Pérez             goto err;
1044100890f7SEugenio Pérez         }
1045100890f7SEugenio Pérez 
10465fde952bSEugenio Pérez         vhost_svq_start(svq, dev->vdev, vq, v->iova_tree);
1047100890f7SEugenio Pérez         ok = vhost_vdpa_svq_map_rings(dev, svq, &addr, &err);
1048100890f7SEugenio Pérez         if (unlikely(!ok)) {
1049100890f7SEugenio Pérez             goto err_map;
1050100890f7SEugenio Pérez         }
1051100890f7SEugenio Pérez 
1052100890f7SEugenio Pérez         /* Override vring GPA set by vhost subsystem */
1053100890f7SEugenio Pérez         r = vhost_vdpa_set_vring_dev_addr(dev, &addr);
1054100890f7SEugenio Pérez         if (unlikely(r != 0)) {
1055100890f7SEugenio Pérez             error_setg_errno(&err, -r, "Cannot set device address");
1056100890f7SEugenio Pérez             goto err_set_addr;
1057100890f7SEugenio Pérez         }
1058100890f7SEugenio Pérez     }
1059100890f7SEugenio Pérez 
1060100890f7SEugenio Pérez     return true;
1061100890f7SEugenio Pérez 
1062100890f7SEugenio Pérez err_set_addr:
1063100890f7SEugenio Pérez     vhost_vdpa_svq_unmap_rings(dev, g_ptr_array_index(v->shadow_vqs, i));
1064100890f7SEugenio Pérez 
1065100890f7SEugenio Pérez err_map:
1066100890f7SEugenio Pérez     vhost_svq_stop(g_ptr_array_index(v->shadow_vqs, i));
1067100890f7SEugenio Pérez 
1068100890f7SEugenio Pérez err:
1069dff4426fSEugenio Pérez     error_reportf_err(err, "Cannot setup SVQ %u: ", i);
1070100890f7SEugenio Pérez     for (unsigned j = 0; j < i; ++j) {
1071100890f7SEugenio Pérez         VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, j);
1072100890f7SEugenio Pérez         vhost_vdpa_svq_unmap_rings(dev, svq);
1073100890f7SEugenio Pérez         vhost_svq_stop(svq);
1074100890f7SEugenio Pérez     }
1075100890f7SEugenio Pérez 
1076100890f7SEugenio Pérez     return false;
1077100890f7SEugenio Pérez }
1078100890f7SEugenio Pérez 
10795b590f51SEugenio Pérez static void vhost_vdpa_svqs_stop(struct vhost_dev *dev)
1080100890f7SEugenio Pérez {
1081100890f7SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
1082100890f7SEugenio Pérez 
1083712c1a31SEugenio Pérez     if (!v->shadow_vqs_enabled) {
10845b590f51SEugenio Pérez         return;
1085100890f7SEugenio Pérez     }
1086100890f7SEugenio Pérez 
1087100890f7SEugenio Pérez     for (unsigned i = 0; i < v->shadow_vqs->len; ++i) {
1088100890f7SEugenio Pérez         VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, i);
10892e1a9de9SEugenio Pérez 
10902e1a9de9SEugenio Pérez         vhost_svq_stop(svq);
10915b590f51SEugenio Pérez         vhost_vdpa_svq_unmap_rings(dev, svq);
10923cfb4d06SEugenio Pérez 
10933cfb4d06SEugenio Pérez         event_notifier_cleanup(&svq->hdev_kick);
10943cfb4d06SEugenio Pérez         event_notifier_cleanup(&svq->hdev_call);
1095dff4426fSEugenio Pérez     }
1096dff4426fSEugenio Pérez }
1097dff4426fSEugenio Pérez 
1098108a6481SCindy Lu static int vhost_vdpa_dev_start(struct vhost_dev *dev, bool started)
1099108a6481SCindy Lu {
1100108a6481SCindy Lu     struct vhost_vdpa *v = dev->opaque;
1101dff4426fSEugenio Pérez     bool ok;
1102778e67deSLaurent Vivier     trace_vhost_vdpa_dev_start(dev, started);
11034d191cfdSJason Wang 
11044d191cfdSJason Wang     if (started) {
11054d191cfdSJason Wang         vhost_vdpa_host_notifiers_init(dev);
1106dff4426fSEugenio Pérez         ok = vhost_vdpa_svqs_start(dev);
1107dff4426fSEugenio Pérez         if (unlikely(!ok)) {
1108dff4426fSEugenio Pérez             return -1;
1109dff4426fSEugenio Pérez         }
11104d191cfdSJason Wang         vhost_vdpa_set_vring_ready(dev);
11114d191cfdSJason Wang     } else {
11125b590f51SEugenio Pérez         vhost_vdpa_svqs_stop(dev);
11134d191cfdSJason Wang         vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
11144d191cfdSJason Wang     }
11154d191cfdSJason Wang 
1116245cf2c2SEugenio Pérez     if (dev->vq_index + dev->nvqs != dev->vq_index_end) {
11174d191cfdSJason Wang         return 0;
11184d191cfdSJason Wang     }
11194d191cfdSJason Wang 
1120108a6481SCindy Lu     if (started) {
1121108a6481SCindy Lu         memory_listener_register(&v->listener, &address_space_memory);
11223631151bSRoman Kagan         return vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
1123108a6481SCindy Lu     } else {
1124108a6481SCindy Lu         vhost_vdpa_reset_device(dev);
1125108a6481SCindy Lu         vhost_vdpa_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
1126108a6481SCindy Lu                                    VIRTIO_CONFIG_S_DRIVER);
1127108a6481SCindy Lu         memory_listener_unregister(&v->listener);
1128108a6481SCindy Lu 
1129108a6481SCindy Lu         return 0;
1130108a6481SCindy Lu     }
1131108a6481SCindy Lu }
1132108a6481SCindy Lu 
1133108a6481SCindy Lu static int vhost_vdpa_set_log_base(struct vhost_dev *dev, uint64_t base,
1134108a6481SCindy Lu                                      struct vhost_log *log)
1135108a6481SCindy Lu {
1136773ebc95SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
1137d71b0609SSi-Wei Liu     if (v->shadow_vqs_enabled || !vhost_vdpa_first_dev(dev)) {
11384d191cfdSJason Wang         return 0;
11394d191cfdSJason Wang     }
11404d191cfdSJason Wang 
1141778e67deSLaurent Vivier     trace_vhost_vdpa_set_log_base(dev, base, log->size, log->refcnt, log->fd,
1142778e67deSLaurent Vivier                                   log->log);
1143108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_LOG_BASE, &base);
1144108a6481SCindy Lu }
1145108a6481SCindy Lu 
1146108a6481SCindy Lu static int vhost_vdpa_set_vring_addr(struct vhost_dev *dev,
1147108a6481SCindy Lu                                        struct vhost_vring_addr *addr)
1148108a6481SCindy Lu {
1149d96be4c8SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
1150d96be4c8SEugenio Pérez 
1151d96be4c8SEugenio Pérez     if (v->shadow_vqs_enabled) {
1152d96be4c8SEugenio Pérez         /*
1153d96be4c8SEugenio Pérez          * Device vring addr was set at device start. SVQ base is handled by
1154d96be4c8SEugenio Pérez          * VirtQueue code.
1155d96be4c8SEugenio Pérez          */
1156d96be4c8SEugenio Pérez         return 0;
1157d96be4c8SEugenio Pérez     }
1158d96be4c8SEugenio Pérez 
1159d96be4c8SEugenio Pérez     return vhost_vdpa_set_vring_dev_addr(dev, addr);
1160108a6481SCindy Lu }
1161108a6481SCindy Lu 
1162108a6481SCindy Lu static int vhost_vdpa_set_vring_num(struct vhost_dev *dev,
1163108a6481SCindy Lu                                       struct vhost_vring_state *ring)
1164108a6481SCindy Lu {
1165778e67deSLaurent Vivier     trace_vhost_vdpa_set_vring_num(dev, ring->index, ring->num);
1166108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_VRING_NUM, ring);
1167108a6481SCindy Lu }
1168108a6481SCindy Lu 
1169108a6481SCindy Lu static int vhost_vdpa_set_vring_base(struct vhost_dev *dev,
1170108a6481SCindy Lu                                        struct vhost_vring_state *ring)
1171108a6481SCindy Lu {
1172d96be4c8SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
11732fdac348SEugenio Pérez     VirtQueue *vq = virtio_get_queue(dev->vdev, ring->index);
1174d96be4c8SEugenio Pérez 
11752fdac348SEugenio Pérez     /*
11762fdac348SEugenio Pérez      * vhost-vdpa devices does not support in-flight requests. Set all of them
11772fdac348SEugenio Pérez      * as available.
11782fdac348SEugenio Pérez      *
11792fdac348SEugenio Pérez      * TODO: This is ok for networking, but other kinds of devices might
11802fdac348SEugenio Pérez      * have problems with these retransmissions.
11812fdac348SEugenio Pérez      */
11822fdac348SEugenio Pérez     while (virtqueue_rewind(vq, 1)) {
11832fdac348SEugenio Pérez         continue;
11842fdac348SEugenio Pérez     }
1185d96be4c8SEugenio Pérez     if (v->shadow_vqs_enabled) {
1186d96be4c8SEugenio Pérez         /*
1187d96be4c8SEugenio Pérez          * Device vring base was set at device start. SVQ base is handled by
1188d96be4c8SEugenio Pérez          * VirtQueue code.
1189d96be4c8SEugenio Pérez          */
1190d96be4c8SEugenio Pérez         return 0;
1191d96be4c8SEugenio Pérez     }
1192d96be4c8SEugenio Pérez 
1193d96be4c8SEugenio Pérez     return vhost_vdpa_set_dev_vring_base(dev, ring);
1194108a6481SCindy Lu }
1195108a6481SCindy Lu 
1196108a6481SCindy Lu static int vhost_vdpa_get_vring_base(struct vhost_dev *dev,
1197108a6481SCindy Lu                                        struct vhost_vring_state *ring)
1198108a6481SCindy Lu {
11996d0b2226SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
1200778e67deSLaurent Vivier     int ret;
1201778e67deSLaurent Vivier 
12026d0b2226SEugenio Pérez     if (v->shadow_vqs_enabled) {
12032fdac348SEugenio Pérez         ring->num = virtio_queue_get_last_avail_idx(dev->vdev, ring->index);
12046d0b2226SEugenio Pérez         return 0;
12056d0b2226SEugenio Pérez     }
12066d0b2226SEugenio Pérez 
1207778e67deSLaurent Vivier     ret = vhost_vdpa_call(dev, VHOST_GET_VRING_BASE, ring);
1208778e67deSLaurent Vivier     trace_vhost_vdpa_get_vring_base(dev, ring->index, ring->num);
1209778e67deSLaurent Vivier     return ret;
1210108a6481SCindy Lu }
1211108a6481SCindy Lu 
1212108a6481SCindy Lu static int vhost_vdpa_set_vring_kick(struct vhost_dev *dev,
1213108a6481SCindy Lu                                        struct vhost_vring_file *file)
1214108a6481SCindy Lu {
1215dff4426fSEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
1216dff4426fSEugenio Pérez     int vdpa_idx = file->index - dev->vq_index;
1217dff4426fSEugenio Pérez 
1218dff4426fSEugenio Pérez     if (v->shadow_vqs_enabled) {
1219dff4426fSEugenio Pérez         VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx);
1220dff4426fSEugenio Pérez         vhost_svq_set_svq_kick_fd(svq, file->fd);
1221dff4426fSEugenio Pérez         return 0;
1222dff4426fSEugenio Pérez     } else {
1223dff4426fSEugenio Pérez         return vhost_vdpa_set_vring_dev_kick(dev, file);
1224dff4426fSEugenio Pérez     }
1225108a6481SCindy Lu }
1226108a6481SCindy Lu 
1227108a6481SCindy Lu static int vhost_vdpa_set_vring_call(struct vhost_dev *dev,
1228108a6481SCindy Lu                                        struct vhost_vring_file *file)
1229108a6481SCindy Lu {
1230a8ac8858SEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
1231a8ac8858SEugenio Pérez     int vdpa_idx = file->index - dev->vq_index;
1232a8ac8858SEugenio Pérez     VhostShadowVirtqueue *svq = g_ptr_array_index(v->shadow_vqs, vdpa_idx);
1233a8ac8858SEugenio Pérez 
1234b2765243SEugenio Pérez     /* Remember last call fd because we can switch to SVQ anytime. */
1235a8ac8858SEugenio Pérez     vhost_svq_set_svq_call_fd(svq, file->fd);
1236b2765243SEugenio Pérez     if (v->shadow_vqs_enabled) {
1237a8ac8858SEugenio Pérez         return 0;
1238a8ac8858SEugenio Pérez     }
1239b2765243SEugenio Pérez 
1240b2765243SEugenio Pérez     return vhost_vdpa_set_vring_dev_call(dev, file);
1241108a6481SCindy Lu }
1242108a6481SCindy Lu 
1243108a6481SCindy Lu static int vhost_vdpa_get_features(struct vhost_dev *dev,
1244108a6481SCindy Lu                                      uint64_t *features)
1245108a6481SCindy Lu {
124612a195faSEugenio Pérez     struct vhost_vdpa *v = dev->opaque;
124712a195faSEugenio Pérez     int ret = vhost_vdpa_get_dev_features(dev, features);
1248778e67deSLaurent Vivier 
124912a195faSEugenio Pérez     if (ret == 0 && v->shadow_vqs_enabled) {
125012a195faSEugenio Pérez         /* Add SVQ logging capabilities */
125112a195faSEugenio Pérez         *features |= BIT_ULL(VHOST_F_LOG_ALL);
125212a195faSEugenio Pérez     }
125312a195faSEugenio Pérez 
1254778e67deSLaurent Vivier     return ret;
1255108a6481SCindy Lu }
1256108a6481SCindy Lu 
1257108a6481SCindy Lu static int vhost_vdpa_set_owner(struct vhost_dev *dev)
1258108a6481SCindy Lu {
1259d71b0609SSi-Wei Liu     if (!vhost_vdpa_first_dev(dev)) {
12604d191cfdSJason Wang         return 0;
12614d191cfdSJason Wang     }
12624d191cfdSJason Wang 
1263778e67deSLaurent Vivier     trace_vhost_vdpa_set_owner(dev);
1264108a6481SCindy Lu     return vhost_vdpa_call(dev, VHOST_SET_OWNER, NULL);
1265108a6481SCindy Lu }
1266108a6481SCindy Lu 
1267108a6481SCindy Lu static int vhost_vdpa_vq_get_addr(struct vhost_dev *dev,
1268108a6481SCindy Lu                     struct vhost_vring_addr *addr, struct vhost_virtqueue *vq)
1269108a6481SCindy Lu {
1270108a6481SCindy Lu     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
1271108a6481SCindy Lu     addr->desc_user_addr = (uint64_t)(unsigned long)vq->desc_phys;
1272108a6481SCindy Lu     addr->avail_user_addr = (uint64_t)(unsigned long)vq->avail_phys;
1273108a6481SCindy Lu     addr->used_user_addr = (uint64_t)(unsigned long)vq->used_phys;
1274778e67deSLaurent Vivier     trace_vhost_vdpa_vq_get_addr(dev, vq, addr->desc_user_addr,
1275778e67deSLaurent Vivier                                  addr->avail_user_addr, addr->used_user_addr);
1276108a6481SCindy Lu     return 0;
1277108a6481SCindy Lu }
1278108a6481SCindy Lu 
1279108a6481SCindy Lu static bool  vhost_vdpa_force_iommu(struct vhost_dev *dev)
1280108a6481SCindy Lu {
1281108a6481SCindy Lu     return true;
1282108a6481SCindy Lu }
1283108a6481SCindy Lu 
1284108a6481SCindy Lu const VhostOps vdpa_ops = {
1285108a6481SCindy Lu         .backend_type = VHOST_BACKEND_TYPE_VDPA,
1286108a6481SCindy Lu         .vhost_backend_init = vhost_vdpa_init,
1287108a6481SCindy Lu         .vhost_backend_cleanup = vhost_vdpa_cleanup,
1288108a6481SCindy Lu         .vhost_set_log_base = vhost_vdpa_set_log_base,
1289108a6481SCindy Lu         .vhost_set_vring_addr = vhost_vdpa_set_vring_addr,
1290108a6481SCindy Lu         .vhost_set_vring_num = vhost_vdpa_set_vring_num,
1291108a6481SCindy Lu         .vhost_set_vring_base = vhost_vdpa_set_vring_base,
1292108a6481SCindy Lu         .vhost_get_vring_base = vhost_vdpa_get_vring_base,
1293108a6481SCindy Lu         .vhost_set_vring_kick = vhost_vdpa_set_vring_kick,
1294108a6481SCindy Lu         .vhost_set_vring_call = vhost_vdpa_set_vring_call,
1295108a6481SCindy Lu         .vhost_get_features = vhost_vdpa_get_features,
1296a5bd0580SJason Wang         .vhost_set_backend_cap = vhost_vdpa_set_backend_cap,
1297108a6481SCindy Lu         .vhost_set_owner = vhost_vdpa_set_owner,
1298108a6481SCindy Lu         .vhost_set_vring_endian = NULL,
1299108a6481SCindy Lu         .vhost_backend_memslots_limit = vhost_vdpa_memslots_limit,
1300108a6481SCindy Lu         .vhost_set_mem_table = vhost_vdpa_set_mem_table,
1301108a6481SCindy Lu         .vhost_set_features = vhost_vdpa_set_features,
1302108a6481SCindy Lu         .vhost_reset_device = vhost_vdpa_reset_device,
1303108a6481SCindy Lu         .vhost_get_vq_index = vhost_vdpa_get_vq_index,
1304108a6481SCindy Lu         .vhost_get_config  = vhost_vdpa_get_config,
1305108a6481SCindy Lu         .vhost_set_config = vhost_vdpa_set_config,
1306108a6481SCindy Lu         .vhost_requires_shm_log = NULL,
1307108a6481SCindy Lu         .vhost_migration_done = NULL,
1308108a6481SCindy Lu         .vhost_backend_can_merge = NULL,
1309108a6481SCindy Lu         .vhost_net_set_mtu = NULL,
1310108a6481SCindy Lu         .vhost_set_iotlb_callback = NULL,
1311108a6481SCindy Lu         .vhost_send_device_iotlb_msg = NULL,
1312108a6481SCindy Lu         .vhost_dev_start = vhost_vdpa_dev_start,
1313108a6481SCindy Lu         .vhost_get_device_id = vhost_vdpa_get_device_id,
1314108a6481SCindy Lu         .vhost_vq_get_addr = vhost_vdpa_vq_get_addr,
1315108a6481SCindy Lu         .vhost_force_iommu = vhost_vdpa_force_iommu,
1316259f3accSCindy Lu         .vhost_set_config_call = vhost_vdpa_set_config_call,
1317108a6481SCindy Lu };
1318