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