2 * Vhost-user i2c virtio device
4 * Copyright (c) 2021 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-i2c.h"
14 #include "qemu/error-report.h"
15 #include "standard-headers/linux/virtio_ids.h"
17 static const int feature_bits
[] = {
18 VIRTIO_I2C_F_ZERO_LENGTH_REQUEST
,
20 VHOST_INVALID_FEATURE_BIT
23 static void vu_i2c_start(VirtIODevice
*vdev
)
25 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
26 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
27 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
30 if (!k
->set_guest_notifiers
) {
31 error_report("binding does not support guest notifiers");
35 ret
= vhost_dev_enable_notifiers(&i2c
->vhost_dev
, vdev
);
37 error_report("Error enabling host notifiers: %d", -ret
);
41 ret
= k
->set_guest_notifiers(qbus
->parent
, i2c
->vhost_dev
.nvqs
, true);
43 error_report("Error binding guest notifier: %d", -ret
);
44 goto err_host_notifiers
;
47 i2c
->vhost_dev
.acked_features
= vdev
->guest_features
;
49 ret
= vhost_dev_start(&i2c
->vhost_dev
, vdev
, true);
51 error_report("Error starting vhost-user-i2c: %d", -ret
);
52 goto err_guest_notifiers
;
56 * guest_notifier_mask/pending not used yet, so just unmask
57 * everything here. virtio-pci will do the right thing by
58 * enabling/disabling irqfd.
60 for (i
= 0; i
< i2c
->vhost_dev
.nvqs
; i
++) {
61 vhost_virtqueue_mask(&i2c
->vhost_dev
, vdev
, i
, false);
67 k
->set_guest_notifiers(qbus
->parent
, i2c
->vhost_dev
.nvqs
, false);
69 vhost_dev_disable_notifiers(&i2c
->vhost_dev
, vdev
);
72 static void vu_i2c_stop(VirtIODevice
*vdev
)
74 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
75 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
76 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
79 if (!k
->set_guest_notifiers
) {
83 vhost_dev_stop(&i2c
->vhost_dev
, vdev
, true);
85 ret
= k
->set_guest_notifiers(qbus
->parent
, i2c
->vhost_dev
.nvqs
, false);
87 error_report("vhost guest notifier cleanup failed: %d", ret
);
91 vhost_dev_disable_notifiers(&i2c
->vhost_dev
, vdev
);
94 static void vu_i2c_set_status(VirtIODevice
*vdev
, uint8_t status
)
96 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
97 bool should_start
= virtio_device_should_start(vdev
, status
);
99 if (vhost_dev_is_started(&i2c
->vhost_dev
) == should_start
) {
110 static uint64_t vu_i2c_get_features(VirtIODevice
*vdev
,
111 uint64_t requested_features
, Error
**errp
)
113 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
115 virtio_add_feature(&requested_features
, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST
);
116 return vhost_get_features(&i2c
->vhost_dev
, feature_bits
, requested_features
);
119 static void vu_i2c_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
122 * Not normally called; it's the daemon that handles the queue;
123 * however virtio's cleanup path can call this.
127 static void vu_i2c_guest_notifier_mask(VirtIODevice
*vdev
, int idx
, bool mask
)
129 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
131 vhost_virtqueue_mask(&i2c
->vhost_dev
, vdev
, idx
, mask
);
134 static bool vu_i2c_guest_notifier_pending(VirtIODevice
*vdev
, int idx
)
136 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
138 return vhost_virtqueue_pending(&i2c
->vhost_dev
, idx
);
141 static void do_vhost_user_cleanup(VirtIODevice
*vdev
, VHostUserI2C
*i2c
)
143 vhost_user_cleanup(&i2c
->vhost_user
);
144 virtio_delete_queue(i2c
->vq
);
145 virtio_cleanup(vdev
);
148 static int vu_i2c_connect(DeviceState
*dev
)
150 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
151 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
153 if (i2c
->connected
) {
156 i2c
->connected
= true;
158 /* restore vhost state */
159 if (virtio_device_started(vdev
, vdev
->status
)) {
166 static void vu_i2c_disconnect(DeviceState
*dev
)
168 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
169 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
171 if (!i2c
->connected
) {
174 i2c
->connected
= false;
176 if (vhost_dev_is_started(&i2c
->vhost_dev
)) {
181 static void vu_i2c_event(void *opaque
, QEMUChrEvent event
)
183 DeviceState
*dev
= opaque
;
184 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
185 VHostUserI2C
*i2c
= VHOST_USER_I2C(vdev
);
188 case CHR_EVENT_OPENED
:
189 if (vu_i2c_connect(dev
) < 0) {
190 qemu_chr_fe_disconnect(&i2c
->chardev
);
194 case CHR_EVENT_CLOSED
:
195 vu_i2c_disconnect(dev
);
197 case CHR_EVENT_BREAK
:
198 case CHR_EVENT_MUX_IN
:
199 case CHR_EVENT_MUX_OUT
:
205 static void vu_i2c_device_realize(DeviceState
*dev
, Error
**errp
)
207 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
208 VHostUserI2C
*i2c
= VHOST_USER_I2C(dev
);
211 if (!i2c
->chardev
.chr
) {
212 error_setg(errp
, "vhost-user-i2c: missing chardev");
216 if (!vhost_user_init(&i2c
->vhost_user
, &i2c
->chardev
, errp
)) {
220 virtio_init(vdev
, VIRTIO_ID_I2C_ADAPTER
, 0);
222 i2c
->vhost_dev
.nvqs
= 1;
223 i2c
->vq
= virtio_add_queue(vdev
, 4, vu_i2c_handle_output
);
224 i2c
->vhost_dev
.vqs
= g_new0(struct vhost_virtqueue
, i2c
->vhost_dev
.nvqs
);
226 ret
= vhost_dev_init(&i2c
->vhost_dev
, &i2c
->vhost_user
,
227 VHOST_BACKEND_TYPE_USER
, 0, errp
);
229 g_free(i2c
->vhost_dev
.vqs
);
230 do_vhost_user_cleanup(vdev
, i2c
);
233 qemu_chr_fe_set_handlers(&i2c
->chardev
, NULL
, NULL
, vu_i2c_event
, NULL
,
237 static void vu_i2c_device_unrealize(DeviceState
*dev
)
239 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
240 VHostUserI2C
*i2c
= VHOST_USER_I2C(dev
);
241 struct vhost_virtqueue
*vhost_vqs
= i2c
->vhost_dev
.vqs
;
243 /* This will stop vhost backend if appropriate. */
244 vu_i2c_set_status(vdev
, 0);
245 vhost_dev_cleanup(&i2c
->vhost_dev
);
247 do_vhost_user_cleanup(vdev
, i2c
);
250 static const VMStateDescription vu_i2c_vmstate
= {
251 .name
= "vhost-user-i2c",
255 static Property vu_i2c_properties
[] = {
256 DEFINE_PROP_CHR("chardev", VHostUserI2C
, chardev
),
257 DEFINE_PROP_END_OF_LIST(),
260 static void vu_i2c_class_init(ObjectClass
*klass
, void *data
)
262 DeviceClass
*dc
= DEVICE_CLASS(klass
);
263 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
265 device_class_set_props(dc
, vu_i2c_properties
);
266 dc
->vmsd
= &vu_i2c_vmstate
;
267 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
268 vdc
->realize
= vu_i2c_device_realize
;
269 vdc
->unrealize
= vu_i2c_device_unrealize
;
270 vdc
->get_features
= vu_i2c_get_features
;
271 vdc
->set_status
= vu_i2c_set_status
;
272 vdc
->guest_notifier_mask
= vu_i2c_guest_notifier_mask
;
273 vdc
->guest_notifier_pending
= vu_i2c_guest_notifier_pending
;
276 static const TypeInfo vu_i2c_info
= {
277 .name
= TYPE_VHOST_USER_I2C
,
278 .parent
= TYPE_VIRTIO_DEVICE
,
279 .instance_size
= sizeof(VHostUserI2C
),
280 .class_init
= vu_i2c_class_init
,
283 static void vu_i2c_register_types(void)
285 type_register_static(&vu_i2c_info
);
288 type_init(vu_i2c_register_types
)