xref: /qemu/hw/virtio/vhost-user-i2c.c (revision 370ed600)
1 /*
2  * Vhost-user i2c virtio device
3  *
4  * Copyright (c) 2021 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-i2c.h"
14 #include "qemu/error-report.h"
15 #include "standard-headers/linux/virtio_ids.h"
16 
17 static const int feature_bits[] = {
18     VIRTIO_I2C_F_ZERO_LENGTH_REQUEST,
19     VIRTIO_F_RING_RESET,
20     VHOST_INVALID_FEATURE_BIT
21 };
22 
23 static void vu_i2c_start(VirtIODevice *vdev)
24 {
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);
28     int ret, i;
29 
30     if (!k->set_guest_notifiers) {
31         error_report("binding does not support guest notifiers");
32         return;
33     }
34 
35     ret = vhost_dev_enable_notifiers(&i2c->vhost_dev, vdev);
36     if (ret < 0) {
37         error_report("Error enabling host notifiers: %d", -ret);
38         return;
39     }
40 
41     ret = k->set_guest_notifiers(qbus->parent, i2c->vhost_dev.nvqs, true);
42     if (ret < 0) {
43         error_report("Error binding guest notifier: %d", -ret);
44         goto err_host_notifiers;
45     }
46 
47     i2c->vhost_dev.acked_features = vdev->guest_features;
48 
49     ret = vhost_dev_start(&i2c->vhost_dev, vdev, true);
50     if (ret < 0) {
51         error_report("Error starting vhost-user-i2c: %d", -ret);
52         goto err_guest_notifiers;
53     }
54 
55     /*
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.
59      */
60     for (i = 0; i < i2c->vhost_dev.nvqs; i++) {
61         vhost_virtqueue_mask(&i2c->vhost_dev, vdev, i, false);
62     }
63 
64     return;
65 
66 err_guest_notifiers:
67     k->set_guest_notifiers(qbus->parent, i2c->vhost_dev.nvqs, false);
68 err_host_notifiers:
69     vhost_dev_disable_notifiers(&i2c->vhost_dev, vdev);
70 }
71 
72 static void vu_i2c_stop(VirtIODevice *vdev)
73 {
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);
77     int ret;
78 
79     if (!k->set_guest_notifiers) {
80         return;
81     }
82 
83     vhost_dev_stop(&i2c->vhost_dev, vdev, true);
84 
85     ret = k->set_guest_notifiers(qbus->parent, i2c->vhost_dev.nvqs, false);
86     if (ret < 0) {
87         error_report("vhost guest notifier cleanup failed: %d", ret);
88         return;
89     }
90 
91     vhost_dev_disable_notifiers(&i2c->vhost_dev, vdev);
92 }
93 
94 static void vu_i2c_set_status(VirtIODevice *vdev, uint8_t status)
95 {
96     VHostUserI2C *i2c = VHOST_USER_I2C(vdev);
97     bool should_start = virtio_device_should_start(vdev, status);
98 
99     if (vhost_dev_is_started(&i2c->vhost_dev) == should_start) {
100         return;
101     }
102 
103     if (should_start) {
104         vu_i2c_start(vdev);
105     } else {
106         vu_i2c_stop(vdev);
107     }
108 }
109 
110 static uint64_t vu_i2c_get_features(VirtIODevice *vdev,
111                                     uint64_t requested_features, Error **errp)
112 {
113     VHostUserI2C *i2c = VHOST_USER_I2C(vdev);
114 
115     virtio_add_feature(&requested_features, VIRTIO_I2C_F_ZERO_LENGTH_REQUEST);
116     return vhost_get_features(&i2c->vhost_dev, feature_bits, requested_features);
117 }
118 
119 static void vu_i2c_handle_output(VirtIODevice *vdev, VirtQueue *vq)
120 {
121     /*
122      * Not normally called; it's the daemon that handles the queue;
123      * however virtio's cleanup path can call this.
124      */
125 }
126 
127 static void vu_i2c_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
128 {
129     VHostUserI2C *i2c = VHOST_USER_I2C(vdev);
130 
131     /*
132      * We don't support interrupts, return early if index is set to
133      * VIRTIO_CONFIG_IRQ_IDX.
134      */
135     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
136         return;
137     }
138 
139     vhost_virtqueue_mask(&i2c->vhost_dev, vdev, idx, mask);
140 }
141 
142 static bool vu_i2c_guest_notifier_pending(VirtIODevice *vdev, int idx)
143 {
144     VHostUserI2C *i2c = VHOST_USER_I2C(vdev);
145 
146     /*
147      * We don't support interrupts, return early if index is set to
148      * VIRTIO_CONFIG_IRQ_IDX.
149      */
150     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
151         return false;
152     }
153 
154     return vhost_virtqueue_pending(&i2c->vhost_dev, idx);
155 }
156 
157 static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserI2C *i2c)
158 {
159     vhost_user_cleanup(&i2c->vhost_user);
160     virtio_delete_queue(i2c->vq);
161     virtio_cleanup(vdev);
162 }
163 
164 static int vu_i2c_connect(DeviceState *dev)
165 {
166     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
167     VHostUserI2C *i2c = VHOST_USER_I2C(vdev);
168 
169     if (i2c->connected) {
170         return 0;
171     }
172     i2c->connected = true;
173 
174     /* restore vhost state */
175     if (virtio_device_started(vdev, vdev->status)) {
176         vu_i2c_start(vdev);
177     }
178 
179     return 0;
180 }
181 
182 static void vu_i2c_disconnect(DeviceState *dev)
183 {
184     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
185     VHostUserI2C *i2c = VHOST_USER_I2C(vdev);
186 
187     if (!i2c->connected) {
188         return;
189     }
190     i2c->connected = false;
191 
192     if (vhost_dev_is_started(&i2c->vhost_dev)) {
193         vu_i2c_stop(vdev);
194     }
195 }
196 
197 static void vu_i2c_event(void *opaque, QEMUChrEvent event)
198 {
199     DeviceState *dev = opaque;
200     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
201     VHostUserI2C *i2c = VHOST_USER_I2C(vdev);
202 
203     switch (event) {
204     case CHR_EVENT_OPENED:
205         if (vu_i2c_connect(dev) < 0) {
206             qemu_chr_fe_disconnect(&i2c->chardev);
207             return;
208         }
209         break;
210     case CHR_EVENT_CLOSED:
211         vu_i2c_disconnect(dev);
212         break;
213     case CHR_EVENT_BREAK:
214     case CHR_EVENT_MUX_IN:
215     case CHR_EVENT_MUX_OUT:
216         /* Ignore */
217         break;
218     }
219 }
220 
221 static void vu_i2c_device_realize(DeviceState *dev, Error **errp)
222 {
223     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
224     VHostUserI2C *i2c = VHOST_USER_I2C(dev);
225     int ret;
226 
227     if (!i2c->chardev.chr) {
228         error_setg(errp, "vhost-user-i2c: missing chardev");
229         return;
230     }
231 
232     if (!vhost_user_init(&i2c->vhost_user, &i2c->chardev, errp)) {
233         return;
234     }
235 
236     virtio_init(vdev, VIRTIO_ID_I2C_ADAPTER, 0);
237 
238     i2c->vhost_dev.nvqs = 1;
239     i2c->vq = virtio_add_queue(vdev, 4, vu_i2c_handle_output);
240     i2c->vhost_dev.vqs = g_new0(struct vhost_virtqueue, i2c->vhost_dev.nvqs);
241 
242     ret = vhost_dev_init(&i2c->vhost_dev, &i2c->vhost_user,
243                          VHOST_BACKEND_TYPE_USER, 0, errp);
244     if (ret < 0) {
245         g_free(i2c->vhost_dev.vqs);
246         do_vhost_user_cleanup(vdev, i2c);
247     }
248 
249     qemu_chr_fe_set_handlers(&i2c->chardev, NULL, NULL, vu_i2c_event, NULL,
250                              dev, NULL, true);
251 }
252 
253 static void vu_i2c_device_unrealize(DeviceState *dev)
254 {
255     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
256     VHostUserI2C *i2c = VHOST_USER_I2C(dev);
257     struct vhost_virtqueue *vhost_vqs = i2c->vhost_dev.vqs;
258 
259     /* This will stop vhost backend if appropriate. */
260     vu_i2c_set_status(vdev, 0);
261     vhost_dev_cleanup(&i2c->vhost_dev);
262     g_free(vhost_vqs);
263     do_vhost_user_cleanup(vdev, i2c);
264 }
265 
266 static const VMStateDescription vu_i2c_vmstate = {
267     .name = "vhost-user-i2c",
268     .unmigratable = 1,
269 };
270 
271 static Property vu_i2c_properties[] = {
272     DEFINE_PROP_CHR("chardev", VHostUserI2C, chardev),
273     DEFINE_PROP_END_OF_LIST(),
274 };
275 
276 static void vu_i2c_class_init(ObjectClass *klass, void *data)
277 {
278     DeviceClass *dc = DEVICE_CLASS(klass);
279     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
280 
281     device_class_set_props(dc, vu_i2c_properties);
282     dc->vmsd = &vu_i2c_vmstate;
283     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
284     vdc->realize = vu_i2c_device_realize;
285     vdc->unrealize = vu_i2c_device_unrealize;
286     vdc->get_features = vu_i2c_get_features;
287     vdc->set_status = vu_i2c_set_status;
288     vdc->guest_notifier_mask = vu_i2c_guest_notifier_mask;
289     vdc->guest_notifier_pending = vu_i2c_guest_notifier_pending;
290 }
291 
292 static const TypeInfo vu_i2c_info = {
293     .name = TYPE_VHOST_USER_I2C,
294     .parent = TYPE_VIRTIO_DEVICE,
295     .instance_size = sizeof(VHostUserI2C),
296     .class_init = vu_i2c_class_init,
297 };
298 
299 static void vu_i2c_register_types(void)
300 {
301     type_register_static(&vu_i2c_info);
302 }
303 
304 type_init(vu_i2c_register_types)
305