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
20 /* Features required from VirtIO */
21 static const int feature_bits
[] = {
23 VIRTIO_F_NOTIFY_ON_EMPTY
,
24 VIRTIO_RING_F_INDIRECT_DESC
,
25 VIRTIO_RING_F_EVENT_IDX
,
28 VHOST_INVALID_FEATURE_BIT
31 static void vu_gpio_get_config(VirtIODevice
*vdev
, uint8_t *config
)
33 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
35 memcpy(config
, &gpio
->config
, sizeof(gpio
->config
));
38 static int vu_gpio_config_notifier(struct vhost_dev
*dev
)
40 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(dev
->vdev
);
42 memcpy(dev
->vdev
->config
, &gpio
->config
, sizeof(gpio
->config
));
43 virtio_notify_config(dev
->vdev
);
48 const VhostDevConfigOps gpio_ops
= {
49 .vhost_dev_config_notifier
= vu_gpio_config_notifier
,
52 static int vu_gpio_start(VirtIODevice
*vdev
)
54 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
55 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
56 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
57 struct vhost_dev
*vhost_dev
= &gpio
->vhost_dev
;
60 if (!k
->set_guest_notifiers
) {
61 error_report("binding does not support guest notifiers");
65 ret
= vhost_dev_enable_notifiers(vhost_dev
, vdev
);
67 error_report("Error enabling host notifiers: %d", ret
);
71 ret
= k
->set_guest_notifiers(qbus
->parent
, vhost_dev
->nvqs
, true);
73 error_report("Error binding guest notifier: %d", ret
);
74 goto err_host_notifiers
;
78 * Before we start up we need to ensure we have the final feature
79 * set needed for the vhost configuration. The backend may also
80 * apply backend_features when the feature set is sent.
82 vhost_ack_features(&gpio
->vhost_dev
, feature_bits
, vdev
->guest_features
);
84 ret
= vhost_dev_start(&gpio
->vhost_dev
, vdev
, false);
86 error_report("Error starting vhost-user-gpio: %d", ret
);
87 goto err_guest_notifiers
;
91 * guest_notifier_mask/pending not used yet, so just unmask
92 * everything here. virtio-pci will do the right thing by
93 * enabling/disabling irqfd.
95 for (i
= 0; i
< gpio
->vhost_dev
.nvqs
; i
++) {
96 vhost_virtqueue_mask(&gpio
->vhost_dev
, vdev
, i
, false);
100 * As we must have VHOST_USER_F_PROTOCOL_FEATURES (because
101 * VHOST_USER_GET_CONFIG requires it) we need to explicitly enable
104 g_assert(vhost_dev
->vhost_ops
&&
105 vhost_dev
->vhost_ops
->vhost_set_vring_enable
);
106 ret
= vhost_dev
->vhost_ops
->vhost_set_vring_enable(vhost_dev
, true);
111 error_report("Failed to start vrings for vhost-user-gpio: %d", ret
);
114 k
->set_guest_notifiers(qbus
->parent
, gpio
->vhost_dev
.nvqs
, false);
116 vhost_dev_disable_notifiers(&gpio
->vhost_dev
, vdev
);
121 static void vu_gpio_stop(VirtIODevice
*vdev
)
123 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
124 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
125 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
126 struct vhost_dev
*vhost_dev
= &gpio
->vhost_dev
;
129 if (!k
->set_guest_notifiers
) {
134 * We can call vu_gpio_stop multiple times, for example from
135 * vm_state_notify and the final object finalisation. Check we
136 * aren't already stopped before doing so.
138 if (!vhost_dev_is_started(vhost_dev
)) {
142 vhost_dev_stop(vhost_dev
, vdev
, false);
144 ret
= k
->set_guest_notifiers(qbus
->parent
, vhost_dev
->nvqs
, false);
146 error_report("vhost guest notifier cleanup failed: %d", ret
);
150 vhost_dev_disable_notifiers(vhost_dev
, vdev
);
153 static void vu_gpio_set_status(VirtIODevice
*vdev
, uint8_t status
)
155 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
156 bool should_start
= virtio_device_should_start(vdev
, status
);
158 trace_virtio_gpio_set_status(status
);
160 if (!gpio
->connected
) {
164 if (vhost_dev_is_started(&gpio
->vhost_dev
) == should_start
) {
169 if (vu_gpio_start(vdev
)) {
170 qemu_chr_fe_disconnect(&gpio
->chardev
);
177 static uint64_t vu_gpio_get_features(VirtIODevice
*vdev
, uint64_t features
,
180 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
182 return vhost_get_features(&gpio
->vhost_dev
, feature_bits
, features
);
185 static void vu_gpio_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
188 * Not normally called; it's the daemon that handles the queue;
189 * however virtio's cleanup path can call this.
193 static void vu_gpio_guest_notifier_mask(VirtIODevice
*vdev
, int idx
, bool mask
)
195 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
197 vhost_virtqueue_mask(&gpio
->vhost_dev
, vdev
, idx
, mask
);
200 static void do_vhost_user_cleanup(VirtIODevice
*vdev
, VHostUserGPIO
*gpio
)
202 virtio_delete_queue(gpio
->command_vq
);
203 virtio_delete_queue(gpio
->interrupt_vq
);
204 g_free(gpio
->vhost_dev
.vqs
);
205 gpio
->vhost_dev
.vqs
= NULL
;
206 virtio_cleanup(vdev
);
207 vhost_user_cleanup(&gpio
->vhost_user
);
210 static int vu_gpio_connect(DeviceState
*dev
, Error
**errp
)
212 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
213 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
214 struct vhost_dev
*vhost_dev
= &gpio
->vhost_dev
;
217 if (gpio
->connected
) {
220 gpio
->connected
= true;
222 vhost_dev_set_config_notifier(vhost_dev
, &gpio_ops
);
223 gpio
->vhost_user
.supports_config
= true;
225 ret
= vhost_dev_init(vhost_dev
, &gpio
->vhost_user
,
226 VHOST_BACKEND_TYPE_USER
, 0, errp
);
231 /* restore vhost state */
232 if (virtio_device_started(vdev
, vdev
->status
)) {
239 static void vu_gpio_disconnect(DeviceState
*dev
)
241 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
242 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
244 if (!gpio
->connected
) {
247 gpio
->connected
= false;
250 vhost_dev_cleanup(&gpio
->vhost_dev
);
253 static void vu_gpio_event(void *opaque
, QEMUChrEvent event
)
255 DeviceState
*dev
= opaque
;
256 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
257 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(vdev
);
258 Error
*local_err
= NULL
;
261 case CHR_EVENT_OPENED
:
262 if (vu_gpio_connect(dev
, &local_err
) < 0) {
263 qemu_chr_fe_disconnect(&gpio
->chardev
);
267 case CHR_EVENT_CLOSED
:
268 vu_gpio_disconnect(dev
);
270 case CHR_EVENT_BREAK
:
271 case CHR_EVENT_MUX_IN
:
272 case CHR_EVENT_MUX_OUT
:
278 static int vu_gpio_realize_connect(VHostUserGPIO
*gpio
, Error
**errp
)
280 VirtIODevice
*vdev
= &gpio
->parent_obj
;
281 DeviceState
*dev
= &vdev
->parent_obj
;
282 struct vhost_dev
*vhost_dev
= &gpio
->vhost_dev
;
285 ret
= qemu_chr_fe_wait_connected(&gpio
->chardev
, errp
);
291 * vu_gpio_connect() may have already connected (via the event
292 * callback) in which case it will just report success.
294 ret
= vu_gpio_connect(dev
, errp
);
296 qemu_chr_fe_disconnect(&gpio
->chardev
);
299 g_assert(gpio
->connected
);
301 ret
= vhost_dev_get_config(vhost_dev
, (uint8_t *)&gpio
->config
,
302 sizeof(gpio
->config
), errp
);
305 error_report("vhost-user-gpio: get config failed");
307 qemu_chr_fe_disconnect(&gpio
->chardev
);
308 vhost_dev_cleanup(vhost_dev
);
315 static void vu_gpio_device_realize(DeviceState
*dev
, Error
**errp
)
319 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
320 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(dev
);
323 if (!gpio
->chardev
.chr
) {
324 error_setg(errp
, "vhost-user-gpio: chardev is mandatory");
328 if (!vhost_user_init(&gpio
->vhost_user
, &gpio
->chardev
, errp
)) {
332 virtio_init(vdev
, VIRTIO_ID_GPIO
, sizeof(gpio
->config
));
334 gpio
->vhost_dev
.nvqs
= 2;
335 gpio
->command_vq
= virtio_add_queue(vdev
, 256, vu_gpio_handle_output
);
336 gpio
->interrupt_vq
= virtio_add_queue(vdev
, 256, vu_gpio_handle_output
);
337 gpio
->vhost_dev
.vqs
= g_new0(struct vhost_virtqueue
, gpio
->vhost_dev
.nvqs
);
339 gpio
->connected
= false;
341 qemu_chr_fe_set_handlers(&gpio
->chardev
, NULL
, NULL
, vu_gpio_event
, NULL
,
344 retries
= REALIZE_CONNECTION_RETRIES
;
348 error_prepend(errp
, "Reconnecting after error: ");
349 error_report_err(*errp
);
352 ret
= vu_gpio_realize_connect(gpio
, errp
);
353 } while (ret
< 0 && retries
--);
356 do_vhost_user_cleanup(vdev
, gpio
);
362 static void vu_gpio_device_unrealize(DeviceState
*dev
)
364 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
365 VHostUserGPIO
*gpio
= VHOST_USER_GPIO(dev
);
367 vu_gpio_set_status(vdev
, 0);
368 qemu_chr_fe_set_handlers(&gpio
->chardev
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
370 vhost_dev_cleanup(&gpio
->vhost_dev
);
371 do_vhost_user_cleanup(vdev
, gpio
);
374 static const VMStateDescription vu_gpio_vmstate
= {
375 .name
= "vhost-user-gpio",
379 static Property vu_gpio_properties
[] = {
380 DEFINE_PROP_CHR("chardev", VHostUserGPIO
, chardev
),
381 DEFINE_PROP_END_OF_LIST(),
384 static void vu_gpio_class_init(ObjectClass
*klass
, void *data
)
386 DeviceClass
*dc
= DEVICE_CLASS(klass
);
387 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
389 device_class_set_props(dc
, vu_gpio_properties
);
390 dc
->vmsd
= &vu_gpio_vmstate
;
391 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
392 vdc
->realize
= vu_gpio_device_realize
;
393 vdc
->unrealize
= vu_gpio_device_unrealize
;
394 vdc
->get_features
= vu_gpio_get_features
;
395 vdc
->get_config
= vu_gpio_get_config
;
396 vdc
->set_status
= vu_gpio_set_status
;
397 vdc
->guest_notifier_mask
= vu_gpio_guest_notifier_mask
;
400 static const TypeInfo vu_gpio_info
= {
401 .name
= TYPE_VHOST_USER_GPIO
,
402 .parent
= TYPE_VIRTIO_DEVICE
,
403 .instance_size
= sizeof(VHostUserGPIO
),
404 .class_init
= vu_gpio_class_init
,
407 static void vu_gpio_register_types(void)
409 type_register_static(&vu_gpio_info
);
412 type_init(vu_gpio_register_types
)