xref: /openbmc/qemu/hw/virtio/vhost-user-gpio.c (revision 6c187695)
1 /*
2  * Vhost-user GPIO virtio device
3  *
4  * Copyright (c) 2022 Viresh Kumar <viresh.kumar@linaro.org>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qapi/error.h"
11 #include "hw/qdev-properties.h"
12 #include "hw/virtio/virtio-bus.h"
13 #include "hw/virtio/vhost-user-gpio.h"
14 #include "qemu/error-report.h"
15 #include "standard-headers/linux/virtio_ids.h"
16 #include "trace.h"
17 
18 #define REALIZE_CONNECTION_RETRIES 3
19 
20 /* Features required from VirtIO */
21 static const int feature_bits[] = {
22     VIRTIO_F_VERSION_1,
23     VIRTIO_F_NOTIFY_ON_EMPTY,
24     VIRTIO_RING_F_INDIRECT_DESC,
25     VIRTIO_RING_F_EVENT_IDX,
26     VIRTIO_GPIO_F_IRQ,
27     VHOST_INVALID_FEATURE_BIT
28 };
29 
30 static void vu_gpio_get_config(VirtIODevice *vdev, uint8_t *config)
31 {
32     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
33 
34     memcpy(config, &gpio->config, sizeof(gpio->config));
35 }
36 
37 static int vu_gpio_config_notifier(struct vhost_dev *dev)
38 {
39     VHostUserGPIO *gpio = VHOST_USER_GPIO(dev->vdev);
40 
41     memcpy(dev->vdev->config, &gpio->config, sizeof(gpio->config));
42     virtio_notify_config(dev->vdev);
43 
44     return 0;
45 }
46 
47 const VhostDevConfigOps gpio_ops = {
48     .vhost_dev_config_notifier = vu_gpio_config_notifier,
49 };
50 
51 static int vu_gpio_start(VirtIODevice *vdev)
52 {
53     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
54     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
55     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
56     struct vhost_dev *vhost_dev = &gpio->vhost_dev;
57     int ret, i;
58 
59     if (!k->set_guest_notifiers) {
60         error_report("binding does not support guest notifiers");
61         return -ENOSYS;
62     }
63 
64     ret = vhost_dev_enable_notifiers(vhost_dev, vdev);
65     if (ret < 0) {
66         error_report("Error enabling host notifiers: %d", ret);
67         return ret;
68     }
69 
70     ret = k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, true);
71     if (ret < 0) {
72         error_report("Error binding guest notifier: %d", ret);
73         goto err_host_notifiers;
74     }
75 
76     /*
77      * Before we start up we need to ensure we have the final feature
78      * set needed for the vhost configuration. The backend may also
79      * apply backend_features when the feature set is sent.
80      */
81     vhost_ack_features(&gpio->vhost_dev, feature_bits, vdev->guest_features);
82 
83     ret = vhost_dev_start(&gpio->vhost_dev, vdev);
84     if (ret < 0) {
85         error_report("Error starting vhost-user-gpio: %d", ret);
86         goto err_guest_notifiers;
87     }
88 
89     /*
90      * guest_notifier_mask/pending not used yet, so just unmask
91      * everything here. virtio-pci will do the right thing by
92      * enabling/disabling irqfd.
93      */
94     for (i = 0; i < gpio->vhost_dev.nvqs; i++) {
95         vhost_virtqueue_mask(&gpio->vhost_dev, vdev, i, false);
96     }
97 
98     /*
99      * As we must have VHOST_USER_F_PROTOCOL_FEATURES (because
100      * VHOST_USER_GET_CONFIG requires it) we need to explicitly enable
101      * the vrings.
102      */
103     g_assert(vhost_dev->vhost_ops &&
104              vhost_dev->vhost_ops->vhost_set_vring_enable);
105     ret = vhost_dev->vhost_ops->vhost_set_vring_enable(vhost_dev, true);
106     if (ret == 0) {
107         return 0;
108     }
109 
110     error_report("Failed to start vrings for vhost-user-gpio: %d", ret);
111 
112 err_guest_notifiers:
113     k->set_guest_notifiers(qbus->parent, gpio->vhost_dev.nvqs, false);
114 err_host_notifiers:
115     vhost_dev_disable_notifiers(&gpio->vhost_dev, vdev);
116 
117     return ret;
118 }
119 
120 static void vu_gpio_stop(VirtIODevice *vdev)
121 {
122     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
123     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
124     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
125     struct vhost_dev *vhost_dev = &gpio->vhost_dev;
126     int ret;
127 
128     if (!k->set_guest_notifiers) {
129         return;
130     }
131 
132     /*
133      * We can call vu_gpio_stop multiple times, for example from
134      * vm_state_notify and the final object finalisation. Check we
135      * aren't already stopped before doing so.
136      */
137     if (!vhost_dev_is_started(vhost_dev)) {
138         return;
139     }
140 
141     vhost_dev_stop(vhost_dev, vdev);
142 
143     ret = k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, false);
144     if (ret < 0) {
145         error_report("vhost guest notifier cleanup failed: %d", ret);
146         return;
147     }
148 
149     vhost_dev_disable_notifiers(vhost_dev, vdev);
150 }
151 
152 static void vu_gpio_set_status(VirtIODevice *vdev, uint8_t status)
153 {
154     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
155     bool should_start = virtio_device_started(vdev, status);
156 
157     trace_virtio_gpio_set_status(status);
158 
159     if (!gpio->connected) {
160         return;
161     }
162 
163     if (vhost_dev_is_started(&gpio->vhost_dev) == should_start) {
164         return;
165     }
166 
167     if (should_start) {
168         if (vu_gpio_start(vdev)) {
169             qemu_chr_fe_disconnect(&gpio->chardev);
170         }
171     } else {
172         vu_gpio_stop(vdev);
173     }
174 }
175 
176 static uint64_t vu_gpio_get_features(VirtIODevice *vdev, uint64_t features,
177                                      Error **errp)
178 {
179     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
180 
181     return vhost_get_features(&gpio->vhost_dev, feature_bits, features);
182 }
183 
184 static void vu_gpio_handle_output(VirtIODevice *vdev, VirtQueue *vq)
185 {
186     /*
187      * Not normally called; it's the daemon that handles the queue;
188      * however virtio's cleanup path can call this.
189      */
190 }
191 
192 static void vu_gpio_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
193 {
194     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
195 
196     vhost_virtqueue_mask(&gpio->vhost_dev, vdev, idx, mask);
197 }
198 
199 static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserGPIO *gpio)
200 {
201     virtio_delete_queue(gpio->command_vq);
202     virtio_delete_queue(gpio->interrupt_vq);
203     g_free(gpio->vhost_dev.vqs);
204     gpio->vhost_dev.vqs = NULL;
205     virtio_cleanup(vdev);
206     vhost_user_cleanup(&gpio->vhost_user);
207 }
208 
209 static int vu_gpio_connect(DeviceState *dev, Error **errp)
210 {
211     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
212     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
213     struct vhost_dev *vhost_dev = &gpio->vhost_dev;
214     int ret;
215 
216     if (gpio->connected) {
217         return 0;
218     }
219     gpio->connected = true;
220 
221     vhost_dev_set_config_notifier(vhost_dev, &gpio_ops);
222     gpio->vhost_user.supports_config = true;
223 
224     ret = vhost_dev_init(vhost_dev, &gpio->vhost_user,
225                          VHOST_BACKEND_TYPE_USER, 0, errp);
226     if (ret < 0) {
227         return ret;
228     }
229 
230     /* restore vhost state */
231     if (virtio_device_started(vdev, vdev->status)) {
232         vu_gpio_start(vdev);
233     }
234 
235     return 0;
236 }
237 
238 static void vu_gpio_disconnect(DeviceState *dev)
239 {
240     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
241     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
242 
243     if (!gpio->connected) {
244         return;
245     }
246     gpio->connected = false;
247 
248     vu_gpio_stop(vdev);
249     vhost_dev_cleanup(&gpio->vhost_dev);
250 }
251 
252 static void vu_gpio_event(void *opaque, QEMUChrEvent event)
253 {
254     DeviceState *dev = opaque;
255     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
256     VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
257     Error *local_err = NULL;
258 
259     switch (event) {
260     case CHR_EVENT_OPENED:
261         if (vu_gpio_connect(dev, &local_err) < 0) {
262             qemu_chr_fe_disconnect(&gpio->chardev);
263             return;
264         }
265         break;
266     case CHR_EVENT_CLOSED:
267         vu_gpio_disconnect(dev);
268         break;
269     case CHR_EVENT_BREAK:
270     case CHR_EVENT_MUX_IN:
271     case CHR_EVENT_MUX_OUT:
272         /* Ignore */
273         break;
274     }
275 }
276 
277 static int vu_gpio_realize_connect(VHostUserGPIO *gpio, Error **errp)
278 {
279     VirtIODevice *vdev = &gpio->parent_obj;
280     DeviceState *dev = &vdev->parent_obj;
281     struct vhost_dev *vhost_dev = &gpio->vhost_dev;
282     int ret;
283 
284     ret = qemu_chr_fe_wait_connected(&gpio->chardev, errp);
285     if (ret < 0) {
286         return ret;
287     }
288 
289     /*
290      * vu_gpio_connect() may have already connected (via the event
291      * callback) in which case it will just report success.
292      */
293     ret = vu_gpio_connect(dev, errp);
294     if (ret < 0) {
295         qemu_chr_fe_disconnect(&gpio->chardev);
296         return ret;
297     }
298     g_assert(gpio->connected);
299 
300     ret = vhost_dev_get_config(vhost_dev, (uint8_t *)&gpio->config,
301                                sizeof(gpio->config), errp);
302 
303     if (ret < 0) {
304         error_report("vhost-user-gpio: get config failed");
305 
306         qemu_chr_fe_disconnect(&gpio->chardev);
307         vhost_dev_cleanup(vhost_dev);
308         return ret;
309     }
310 
311     return 0;
312 }
313 
314 static void vu_gpio_device_realize(DeviceState *dev, Error **errp)
315 {
316     ERRP_GUARD();
317 
318     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
319     VHostUserGPIO *gpio = VHOST_USER_GPIO(dev);
320     int retries, ret;
321 
322     if (!gpio->chardev.chr) {
323         error_setg(errp, "vhost-user-gpio: chardev is mandatory");
324         return;
325     }
326 
327     if (!vhost_user_init(&gpio->vhost_user, &gpio->chardev, errp)) {
328         return;
329     }
330 
331     virtio_init(vdev, VIRTIO_ID_GPIO, sizeof(gpio->config));
332 
333     gpio->vhost_dev.nvqs = 2;
334     gpio->command_vq = virtio_add_queue(vdev, 256, vu_gpio_handle_output);
335     gpio->interrupt_vq = virtio_add_queue(vdev, 256, vu_gpio_handle_output);
336     gpio->vhost_dev.vqs = g_new0(struct vhost_virtqueue, gpio->vhost_dev.nvqs);
337 
338     gpio->connected = false;
339 
340     qemu_chr_fe_set_handlers(&gpio->chardev, NULL, NULL, vu_gpio_event, NULL,
341                              dev, NULL, true);
342 
343     retries = REALIZE_CONNECTION_RETRIES;
344     g_assert(!*errp);
345     do {
346         if (*errp) {
347             error_prepend(errp, "Reconnecting after error: ");
348             error_report_err(*errp);
349             *errp = NULL;
350         }
351         ret = vu_gpio_realize_connect(gpio, errp);
352     } while (ret < 0 && retries--);
353 
354     if (ret < 0) {
355         do_vhost_user_cleanup(vdev, gpio);
356     }
357 
358     return;
359 }
360 
361 static void vu_gpio_device_unrealize(DeviceState *dev)
362 {
363     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
364     VHostUserGPIO *gpio = VHOST_USER_GPIO(dev);
365 
366     vu_gpio_set_status(vdev, 0);
367     qemu_chr_fe_set_handlers(&gpio->chardev, NULL, NULL, NULL, NULL, NULL, NULL,
368                              false);
369     vhost_dev_cleanup(&gpio->vhost_dev);
370     do_vhost_user_cleanup(vdev, gpio);
371 }
372 
373 static const VMStateDescription vu_gpio_vmstate = {
374     .name = "vhost-user-gpio",
375     .unmigratable = 1,
376 };
377 
378 static Property vu_gpio_properties[] = {
379     DEFINE_PROP_CHR("chardev", VHostUserGPIO, chardev),
380     DEFINE_PROP_END_OF_LIST(),
381 };
382 
383 static void vu_gpio_class_init(ObjectClass *klass, void *data)
384 {
385     DeviceClass *dc = DEVICE_CLASS(klass);
386     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
387 
388     device_class_set_props(dc, vu_gpio_properties);
389     dc->vmsd = &vu_gpio_vmstate;
390     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
391     vdc->realize = vu_gpio_device_realize;
392     vdc->unrealize = vu_gpio_device_unrealize;
393     vdc->get_features = vu_gpio_get_features;
394     vdc->get_config = vu_gpio_get_config;
395     vdc->set_status = vu_gpio_set_status;
396     vdc->guest_notifier_mask = vu_gpio_guest_notifier_mask;
397 }
398 
399 static const TypeInfo vu_gpio_info = {
400     .name = TYPE_VHOST_USER_GPIO,
401     .parent = TYPE_VIRTIO_DEVICE,
402     .instance_size = sizeof(VHostUserGPIO),
403     .class_init = vu_gpio_class_init,
404 };
405 
406 static void vu_gpio_register_types(void)
407 {
408     type_register_static(&vu_gpio_info);
409 }
410 
411 type_init(vu_gpio_register_types)
412