xref: /openbmc/qemu/hw/virtio/vdpa-dev.c (revision 6edfca9e)
1 /*
2  * Vhost Vdpa Device
3  *
4  * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
5  *
6  * Authors:
7  *   Longpeng <longpeng2@huawei.com>
8  *
9  * Largely based on the "vhost-user-blk-pci.c" and "vhost-user-blk.c"
10  * implemented by:
11  *   Changpeng Liu <changpeng.liu@intel.com>
12  *
13  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
14  * See the COPYING.LIB file in the top-level directory.
15  */
16 #include "qemu/osdep.h"
17 #include <sys/ioctl.h>
18 #include <linux/vhost.h>
19 #include "qapi/error.h"
20 #include "qemu/error-report.h"
21 #include "qemu/cutils.h"
22 #include "hw/qdev-core.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/qdev-properties-system.h"
25 #include "hw/virtio/vhost.h"
26 #include "hw/virtio/virtio.h"
27 #include "hw/virtio/virtio-bus.h"
28 #include "hw/virtio/vdpa-dev.h"
29 #include "sysemu/sysemu.h"
30 #include "sysemu/runstate.h"
31 
32 static void
33 vhost_vdpa_device_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
34 {
35     /* Nothing to do */
36 }
37 
38 static uint32_t
39 vhost_vdpa_device_get_u32(int fd, unsigned long int cmd, Error **errp)
40 {
41     uint32_t val = (uint32_t)-1;
42 
43     if (ioctl(fd, cmd, &val) < 0) {
44         error_setg(errp, "vhost-vdpa-device: cmd 0x%lx failed: %s",
45                    cmd, strerror(errno));
46     }
47 
48     return val;
49 }
50 
51 static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
52 {
53     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
54     VhostVdpaDevice *v = VHOST_VDPA_DEVICE(vdev);
55     struct vhost_vdpa_iova_range iova_range;
56     uint16_t max_queue_size;
57     struct vhost_virtqueue *vqs;
58     int i, ret;
59 
60     if (!v->vhostdev) {
61         error_setg(errp, "vhost-vdpa-device: vhostdev are missing");
62         return;
63     }
64 
65     v->vhostfd = qemu_open(v->vhostdev, O_RDWR, errp);
66     if (*errp) {
67         return;
68     }
69     v->vdpa.device_fd = v->vhostfd;
70 
71     v->vdev_id = vhost_vdpa_device_get_u32(v->vhostfd,
72                                            VHOST_VDPA_GET_DEVICE_ID, errp);
73     if (*errp) {
74         goto out;
75     }
76 
77     max_queue_size = vhost_vdpa_device_get_u32(v->vhostfd,
78                                                VHOST_VDPA_GET_VRING_NUM, errp);
79     if (*errp) {
80         goto out;
81     }
82 
83     if (v->queue_size > max_queue_size) {
84         error_setg(errp, "vhost-vdpa-device: invalid queue_size: %u (max:%u)",
85                    v->queue_size, max_queue_size);
86         goto out;
87     } else if (!v->queue_size) {
88         v->queue_size = max_queue_size;
89     }
90 
91     v->num_queues = vhost_vdpa_device_get_u32(v->vhostfd,
92                                               VHOST_VDPA_GET_VQS_COUNT, errp);
93     if (*errp) {
94         goto out;
95     }
96 
97     if (!v->num_queues || v->num_queues > VIRTIO_QUEUE_MAX) {
98         error_setg(errp, "invalid number of virtqueues: %u (max:%u)",
99                    v->num_queues, VIRTIO_QUEUE_MAX);
100         goto out;
101     }
102 
103     v->dev.nvqs = v->num_queues;
104     vqs = g_new0(struct vhost_virtqueue, v->dev.nvqs);
105     v->dev.vqs = vqs;
106     v->dev.vq_index = 0;
107     v->dev.vq_index_end = v->dev.nvqs;
108     v->dev.backend_features = 0;
109     v->started = false;
110 
111     ret = vhost_vdpa_get_iova_range(v->vhostfd, &iova_range);
112     if (ret < 0) {
113         error_setg(errp, "vhost-vdpa-device: get iova range failed: %s",
114                    strerror(-ret));
115         goto free_vqs;
116     }
117     v->vdpa.iova_range = iova_range;
118 
119     ret = vhost_dev_init(&v->dev, &v->vdpa, VHOST_BACKEND_TYPE_VDPA, 0, NULL);
120     if (ret < 0) {
121         error_setg(errp, "vhost-vdpa-device: vhost initialization failed: %s",
122                    strerror(-ret));
123         goto free_vqs;
124     }
125 
126     v->config_size = vhost_vdpa_device_get_u32(v->vhostfd,
127                                                VHOST_VDPA_GET_CONFIG_SIZE,
128                                                errp);
129     if (*errp) {
130         goto vhost_cleanup;
131     }
132 
133     /*
134      * Invoke .post_init() to initialize the transport-specific fields
135      * before calling virtio_init().
136      */
137     if (v->post_init && v->post_init(v, errp) < 0) {
138         goto vhost_cleanup;
139     }
140 
141     v->config = g_malloc0(v->config_size);
142 
143     ret = vhost_dev_get_config(&v->dev, v->config, v->config_size, NULL);
144     if (ret < 0) {
145         error_setg(errp, "vhost-vdpa-device: get config failed");
146         goto free_config;
147     }
148 
149     virtio_init(vdev, v->vdev_id, v->config_size);
150 
151     v->virtqs = g_new0(VirtQueue *, v->dev.nvqs);
152     for (i = 0; i < v->dev.nvqs; i++) {
153         v->virtqs[i] = virtio_add_queue(vdev, v->queue_size,
154                                         vhost_vdpa_device_dummy_handle_output);
155     }
156 
157     return;
158 
159 free_config:
160     g_free(v->config);
161 vhost_cleanup:
162     vhost_dev_cleanup(&v->dev);
163 free_vqs:
164     g_free(vqs);
165 out:
166     qemu_close(v->vhostfd);
167     v->vhostfd = -1;
168 }
169 
170 static void vhost_vdpa_device_unrealize(DeviceState *dev)
171 {
172     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
173     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
174     int i;
175 
176     virtio_set_status(vdev, 0);
177 
178     for (i = 0; i < s->num_queues; i++) {
179         virtio_delete_queue(s->virtqs[i]);
180     }
181     g_free(s->virtqs);
182     virtio_cleanup(vdev);
183 
184     g_free(s->config);
185     g_free(s->dev.vqs);
186     vhost_dev_cleanup(&s->dev);
187     qemu_close(s->vhostfd);
188     s->vhostfd = -1;
189 }
190 
191 static void
192 vhost_vdpa_device_get_config(VirtIODevice *vdev, uint8_t *config)
193 {
194     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
195 
196     memcpy(config, s->config, s->config_size);
197 }
198 
199 static void
200 vhost_vdpa_device_set_config(VirtIODevice *vdev, const uint8_t *config)
201 {
202     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
203     int ret;
204 
205     ret = vhost_dev_set_config(&s->dev, s->config, 0, s->config_size,
206                                VHOST_SET_CONFIG_TYPE_FRONTEND);
207     if (ret) {
208         error_report("set device config space failed");
209         return;
210     }
211 }
212 
213 static uint64_t vhost_vdpa_device_get_features(VirtIODevice *vdev,
214                                                uint64_t features,
215                                                Error **errp)
216 {
217     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
218     uint64_t backend_features = s->dev.features;
219 
220     if (!virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM)) {
221         virtio_clear_feature(&backend_features, VIRTIO_F_IOMMU_PLATFORM);
222     }
223 
224     return backend_features;
225 }
226 
227 static int vhost_vdpa_device_start(VirtIODevice *vdev, Error **errp)
228 {
229     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
230     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
231     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
232     int i, ret;
233 
234     if (!k->set_guest_notifiers) {
235         error_setg(errp, "binding does not support guest notifiers");
236         return -ENOSYS;
237     }
238 
239     ret = vhost_dev_enable_notifiers(&s->dev, vdev);
240     if (ret < 0) {
241         error_setg_errno(errp, -ret, "Error enabling host notifiers");
242         return ret;
243     }
244 
245     ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
246     if (ret < 0) {
247         error_setg_errno(errp, -ret, "Error binding guest notifier");
248         goto err_host_notifiers;
249     }
250 
251     s->dev.acked_features = vdev->guest_features;
252 
253     ret = vhost_dev_start(&s->dev, vdev, false);
254     if (ret < 0) {
255         error_setg_errno(errp, -ret, "Error starting vhost");
256         goto err_guest_notifiers;
257     }
258     s->started = true;
259 
260     /*
261      * guest_notifier_mask/pending not used yet, so just unmask
262      * everything here. virtio-pci will do the right thing by
263      * enabling/disabling irqfd.
264      */
265     for (i = 0; i < s->dev.nvqs; i++) {
266         vhost_virtqueue_mask(&s->dev, vdev, i, false);
267     }
268 
269     return ret;
270 
271 err_guest_notifiers:
272     k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
273 err_host_notifiers:
274     vhost_dev_disable_notifiers(&s->dev, vdev);
275     return ret;
276 }
277 
278 static void vhost_vdpa_device_stop(VirtIODevice *vdev)
279 {
280     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
281     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
282     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
283     int ret;
284 
285     if (!s->started) {
286         return;
287     }
288     s->started = false;
289 
290     if (!k->set_guest_notifiers) {
291         return;
292     }
293 
294     vhost_dev_stop(&s->dev, vdev, false);
295 
296     ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
297     if (ret < 0) {
298         error_report("vhost guest notifier cleanup failed: %d", ret);
299         return;
300     }
301 
302     vhost_dev_disable_notifiers(&s->dev, vdev);
303 }
304 
305 static void vhost_vdpa_device_set_status(VirtIODevice *vdev, uint8_t status)
306 {
307     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev);
308     bool should_start = virtio_device_started(vdev, status);
309     Error *local_err = NULL;
310     int ret;
311 
312     if (!vdev->vm_running) {
313         should_start = false;
314     }
315 
316     if (s->started == should_start) {
317         return;
318     }
319 
320     if (should_start) {
321         ret = vhost_vdpa_device_start(vdev, &local_err);
322         if (ret < 0) {
323             error_reportf_err(local_err, "vhost-vdpa-device: start failed: ");
324         }
325     } else {
326         vhost_vdpa_device_stop(vdev);
327     }
328 }
329 
330 static Property vhost_vdpa_device_properties[] = {
331     DEFINE_PROP_STRING("vhostdev", VhostVdpaDevice, vhostdev),
332     DEFINE_PROP_UINT16("queue-size", VhostVdpaDevice, queue_size, 0),
333     DEFINE_PROP_END_OF_LIST(),
334 };
335 
336 static const VMStateDescription vmstate_vhost_vdpa_device = {
337     .name = "vhost-vdpa-device",
338     .unmigratable = 1,
339     .minimum_version_id = 1,
340     .version_id = 1,
341     .fields = (VMStateField[]) {
342         VMSTATE_VIRTIO_DEVICE,
343         VMSTATE_END_OF_LIST()
344     },
345 };
346 
347 static void vhost_vdpa_device_class_init(ObjectClass *klass, void *data)
348 {
349     DeviceClass *dc = DEVICE_CLASS(klass);
350     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
351 
352     device_class_set_props(dc, vhost_vdpa_device_properties);
353     dc->desc = "VDPA-based generic device assignment";
354     dc->vmsd = &vmstate_vhost_vdpa_device;
355     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
356     vdc->realize = vhost_vdpa_device_realize;
357     vdc->unrealize = vhost_vdpa_device_unrealize;
358     vdc->get_config = vhost_vdpa_device_get_config;
359     vdc->set_config = vhost_vdpa_device_set_config;
360     vdc->get_features = vhost_vdpa_device_get_features;
361     vdc->set_status = vhost_vdpa_device_set_status;
362 }
363 
364 static void vhost_vdpa_device_instance_init(Object *obj)
365 {
366     VhostVdpaDevice *s = VHOST_VDPA_DEVICE(obj);
367 
368     device_add_bootindex_property(obj, &s->bootindex, "bootindex",
369                                   NULL, DEVICE(obj));
370 }
371 
372 static const TypeInfo vhost_vdpa_device_info = {
373     .name = TYPE_VHOST_VDPA_DEVICE,
374     .parent = TYPE_VIRTIO_DEVICE,
375     .instance_size = sizeof(VhostVdpaDevice),
376     .class_init = vhost_vdpa_device_class_init,
377     .instance_init = vhost_vdpa_device_instance_init,
378 };
379 
380 static void register_vhost_vdpa_device_type(void)
381 {
382     type_register_static(&vhost_vdpa_device_info);
383 }
384 
385 type_init(register_vhost_vdpa_device_type);
386