2 * Vhost-user GPIO virtio device
4 * Copyright (c) 2022 Viresh Kumar <viresh.kumar@linaro.org>
6 * SPDX-License-Identifier: GPL-2.0-or-later
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"
18 #define REALIZE_CONNECTION_RETRIES 3
21 /* Features required from VirtIO */
22 static const int feature_bits
[] = {
24 VIRTIO_F_NOTIFY_ON_EMPTY
,
25 VIRTIO_RING_F_INDIRECT_DESC
,
26 VIRTIO_RING_F_EVENT_IDX
,
29 VHOST_INVALID_FEATURE_BIT
32 static void vu_gpio_get_config(VirtIODevice
*vdev
, uint8_t *config
)
34 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
36 memcpy(config
, &gpio
->config
, sizeof(gpio
->config
));
39 static int vu_gpio_config_notifier(struct vhost_dev
*dev
)
41 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(dev
->vdev
);
43 memcpy(dev
->vdev
->config
, &gpio
->config
, sizeof(gpio
->config
));
44 virtio_notify_config(dev
->vdev
);
49 const VhostDevConfigOps gpio_ops
= {
50 .vhost_dev_config_notifier
= vu_gpio_config_notifier
,
53 static int vu_gpio_start(VirtIODevice
*vdev
)
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
;
61 if (!k
->set_guest_notifiers
) {
62 error_report("binding does not support guest notifiers");
66 ret
= vhost_dev_enable_notifiers(vhost_dev
, vdev
);
68 error_report("Error enabling host notifiers: %d", ret
);
72 ret
= k
->set_guest_notifiers(qbus
->parent
, vhost_dev
->nvqs
, true);
74 error_report("Error binding guest notifier: %d", ret
);
75 goto err_host_notifiers
;
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.
83 vhost_ack_features(&gpio
->vhost_dev
, feature_bits
, vdev
->guest_features
);
85 ret
= vhost_dev_start(&gpio
->vhost_dev
, vdev
, false);
87 error_report("Error starting vhost-user-gpio: %d", ret
);
88 goto err_guest_notifiers
;
90 gpio
->started_vu
= true;
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.
97 for (i
= 0; i
< gpio
->vhost_dev
.nvqs
; i
++) {
98 vhost_virtqueue_mask(&gpio
->vhost_dev
, vdev
, i
, false);
102 * As we must have VHOST_USER_F_PROTOCOL_FEATURES (because
103 * VHOST_USER_GET_CONFIG requires it) we need to explicitly enable
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);
113 error_report("Failed to start vrings for vhost-user-gpio: %d", ret
);
116 k
->set_guest_notifiers(qbus
->parent
, gpio
->vhost_dev
.nvqs
, false);
118 vhost_dev_disable_notifiers(&gpio
->vhost_dev
, vdev
);
123 static void vu_gpio_stop(VirtIODevice
*vdev
)
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
;
131 if (!gpio
->started_vu
) {
134 gpio
->started_vu
= false;
136 if (!k
->set_guest_notifiers
) {
140 vhost_dev_stop(vhost_dev
, vdev
, false);
142 ret
= k
->set_guest_notifiers(qbus
->parent
, vhost_dev
->nvqs
, false);
144 error_report("vhost guest notifier cleanup failed: %d", ret
);
148 vhost_dev_disable_notifiers(vhost_dev
, vdev
);
151 static void vu_gpio_set_status(VirtIODevice
*vdev
, uint8_t status
)
153 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
154 bool should_start
= virtio_device_should_start(vdev
, status
);
156 trace_virtio_gpio_set_status(status
);
158 if (!gpio
->connected
) {
162 if (vhost_dev_is_started(&gpio
->vhost_dev
) == should_start
) {
167 if (vu_gpio_start(vdev
)) {
168 qemu_chr_fe_disconnect(&gpio
->chardev
);
175 static uint64_t vu_gpio_get_features(VirtIODevice
*vdev
, uint64_t features
,
178 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
180 return vhost_get_features(&gpio
->vhost_dev
, feature_bits
, features
);
183 static void vu_gpio_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
186 * Not normally called; it's the daemon that handles the queue;
187 * however virtio's cleanup path can call this.
191 static void vu_gpio_guest_notifier_mask(VirtIODevice
*vdev
, int idx
, bool mask
)
193 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
196 * Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
197 * as the Marco of configure interrupt's IDX, If this driver does not
198 * support, the function will return
201 if (idx
== VIRTIO_CONFIG_IRQ_IDX
) {
205 vhost_virtqueue_mask(&gpio
->vhost_dev
, vdev
, idx
, mask
);
208 static void do_vhost_user_cleanup(VirtIODevice
*vdev
, VHostUserGPIO
*gpio
)
210 virtio_delete_queue(gpio
->command_vq
);
211 virtio_delete_queue(gpio
->interrupt_vq
);
212 g_free(gpio
->vhost_vqs
);
213 virtio_cleanup(vdev
);
214 vhost_user_cleanup(&gpio
->vhost_user
);
217 static int vu_gpio_connect(DeviceState
*dev
, Error
**errp
)
219 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
220 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
221 struct vhost_dev
*vhost_dev
= &gpio
->vhost_dev
;
224 if (gpio
->connected
) {
227 gpio
->connected
= true;
229 vhost_dev_set_config_notifier(vhost_dev
, &gpio_ops
);
230 gpio
->vhost_user
.supports_config
= true;
232 gpio
->vhost_dev
.nvqs
= VHOST_NVQS
;
233 gpio
->vhost_dev
.vqs
= gpio
->vhost_vqs
;
235 ret
= vhost_dev_init(vhost_dev
, &gpio
->vhost_user
,
236 VHOST_BACKEND_TYPE_USER
, 0, errp
);
241 /* restore vhost state */
242 if (virtio_device_started(vdev
, vdev
->status
)) {
249 static void vu_gpio_event(void *opaque
, QEMUChrEvent event
);
251 static void vu_gpio_disconnect(DeviceState
*dev
)
253 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
254 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
256 if (!gpio
->connected
) {
259 gpio
->connected
= false;
262 vhost_dev_cleanup(&gpio
->vhost_dev
);
264 /* Re-instate the event handler for new connections */
265 qemu_chr_fe_set_handlers(&gpio
->chardev
,
266 NULL
, NULL
, vu_gpio_event
,
267 NULL
, dev
, NULL
, true);
270 static void vu_gpio_event(void *opaque
, QEMUChrEvent event
)
272 DeviceState
*dev
= opaque
;
273 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
274 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
275 Error
*local_err
= NULL
;
278 case CHR_EVENT_OPENED
:
279 if (vu_gpio_connect(dev
, &local_err
) < 0) {
280 qemu_chr_fe_disconnect(&gpio
->chardev
);
284 case CHR_EVENT_CLOSED
:
285 /* defer close until later to avoid circular close */
286 vhost_user_async_close(dev
, &gpio
->chardev
, &gpio
->vhost_dev
,
289 case CHR_EVENT_BREAK
:
290 case CHR_EVENT_MUX_IN
:
291 case CHR_EVENT_MUX_OUT
:
297 static int vu_gpio_realize_connect(VHostUserGPIO
*gpio
, Error
**errp
)
299 VirtIODevice
*vdev
= &gpio
->parent_obj
;
300 DeviceState
*dev
= &vdev
->parent_obj
;
301 struct vhost_dev
*vhost_dev
= &gpio
->vhost_dev
;
304 ret
= qemu_chr_fe_wait_connected(&gpio
->chardev
, errp
);
310 * vu_gpio_connect() may have already connected (via the event
311 * callback) in which case it will just report success.
313 ret
= vu_gpio_connect(dev
, errp
);
315 qemu_chr_fe_disconnect(&gpio
->chardev
);
318 g_assert(gpio
->connected
);
320 ret
= vhost_dev_get_config(vhost_dev
, (uint8_t *)&gpio
->config
,
321 sizeof(gpio
->config
), errp
);
324 error_report("vhost-user-gpio: get config failed");
326 qemu_chr_fe_disconnect(&gpio
->chardev
);
327 vhost_dev_cleanup(vhost_dev
);
334 static void vu_gpio_device_realize(DeviceState
*dev
, Error
**errp
)
338 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
339 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(dev
);
342 if (!gpio
->chardev
.chr
) {
343 error_setg(errp
, "vhost-user-gpio: chardev is mandatory");
347 if (!vhost_user_init(&gpio
->vhost_user
, &gpio
->chardev
, errp
)) {
351 virtio_init(vdev
, VIRTIO_ID_GPIO
, sizeof(gpio
->config
));
353 gpio
->command_vq
= virtio_add_queue(vdev
, 256, vu_gpio_handle_output
);
354 gpio
->interrupt_vq
= virtio_add_queue(vdev
, 256, vu_gpio_handle_output
);
355 gpio
->vhost_vqs
= g_new0(struct vhost_virtqueue
, VHOST_NVQS
);
357 gpio
->connected
= false;
359 qemu_chr_fe_set_handlers(&gpio
->chardev
, NULL
, NULL
, vu_gpio_event
, NULL
,
362 retries
= REALIZE_CONNECTION_RETRIES
;
366 error_prepend(errp
, "Reconnecting after error: ");
367 error_report_err(*errp
);
370 ret
= vu_gpio_realize_connect(gpio
, errp
);
371 } while (ret
< 0 && retries
--);
374 do_vhost_user_cleanup(vdev
, gpio
);
380 static void vu_gpio_device_unrealize(DeviceState
*dev
)
382 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
383 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(dev
);
385 vu_gpio_set_status(vdev
, 0);
386 qemu_chr_fe_set_handlers(&gpio
->chardev
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
388 vhost_dev_cleanup(&gpio
->vhost_dev
);
389 do_vhost_user_cleanup(vdev
, gpio
);
392 static const VMStateDescription vu_gpio_vmstate
= {
393 .name
= "vhost-user-gpio",
397 static Property vu_gpio_properties
[] = {
398 DEFINE_PROP_CHR("chardev", VHostUserGPIO
, chardev
),
399 DEFINE_PROP_END_OF_LIST(),
402 static void vu_gpio_class_init(ObjectClass
*klass
, void *data
)
404 DeviceClass
*dc
= DEVICE_CLASS(klass
);
405 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
407 device_class_set_props(dc
, vu_gpio_properties
);
408 dc
->vmsd
= &vu_gpio_vmstate
;
409 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
410 vdc
->realize
= vu_gpio_device_realize
;
411 vdc
->unrealize
= vu_gpio_device_unrealize
;
412 vdc
->get_features
= vu_gpio_get_features
;
413 vdc
->get_config
= vu_gpio_get_config
;
414 vdc
->set_status
= vu_gpio_set_status
;
415 vdc
->guest_notifier_mask
= vu_gpio_guest_notifier_mask
;
418 static const TypeInfo vu_gpio_info
= {
419 .name
= TYPE_VHOST_USER_GPIO
,
420 .parent
= TYPE_VIRTIO_DEVICE
,
421 .instance_size
= sizeof(VHostUserGPIO
),
422 .class_init
= vu_gpio_class_init
,
425 static void vu_gpio_register_types(void)
427 type_register_static(&vu_gpio_info
);
430 type_init(vu_gpio_register_types
)