xref: /openbmc/qemu/hw/virtio/vhost-user-scmi.c (revision 243975c0)
1 /*
2  * Vhost-user SCMI virtio device
3  *
4  * SPDX-FileCopyrightText: Red Hat, Inc.
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  *
7  * Implementation based on other vhost-user devices in QEMU.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "qemu/error-report.h"
13 #include "hw/virtio/virtio-bus.h"
14 #include "hw/virtio/vhost-user-scmi.h"
15 #include "standard-headers/linux/virtio_ids.h"
16 #include "standard-headers/linux/virtio_scmi.h"
17 #include "trace.h"
18 
19 /*
20  * In this version, we don't support VIRTIO_SCMI_F_SHARED_MEMORY.
21  * Note that VIRTIO_SCMI_F_SHARED_MEMORY is currently not supported in
22  * Linux VirtIO SCMI guest driver.
23  */
24 static const int feature_bits[] = {
25     VIRTIO_F_VERSION_1,
26     VIRTIO_F_NOTIFY_ON_EMPTY,
27     VIRTIO_RING_F_INDIRECT_DESC,
28     VIRTIO_RING_F_EVENT_IDX,
29     VIRTIO_F_RING_RESET,
30     VIRTIO_SCMI_F_P2A_CHANNELS,
31     VHOST_INVALID_FEATURE_BIT
32 };
33 
34 static int vu_scmi_start(VirtIODevice *vdev)
35 {
36     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
37     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
38     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
39     struct vhost_dev *vhost_dev = &scmi->vhost_dev;
40     int ret, i;
41 
42     if (!k->set_guest_notifiers) {
43         error_report("binding does not support guest notifiers");
44         return -ENOSYS;
45     }
46 
47     ret = vhost_dev_enable_notifiers(vhost_dev, vdev);
48     if (ret < 0) {
49         error_report("Error enabling host notifiers: %d", ret);
50         return ret;
51     }
52 
53     ret = k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, true);
54     if (ret < 0) {
55         error_report("Error binding guest notifier: %d", ret);
56         goto err_host_notifiers;
57     }
58 
59     vhost_ack_features(&scmi->vhost_dev, feature_bits, vdev->guest_features);
60 
61     ret = vhost_dev_start(&scmi->vhost_dev, vdev, true);
62     if (ret < 0) {
63         error_report("Error starting vhost-user-scmi: %d", ret);
64         goto err_guest_notifiers;
65     }
66     scmi->started_vu = true;
67 
68     /*
69      * guest_notifier_mask/pending not used yet, so just unmask
70      * everything here. virtio-pci will do the right thing by
71      * enabling/disabling irqfd.
72      */
73     for (i = 0; i < scmi->vhost_dev.nvqs; i++) {
74         vhost_virtqueue_mask(&scmi->vhost_dev, vdev, i, false);
75     }
76     return 0;
77 
78 err_guest_notifiers:
79     k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, false);
80 err_host_notifiers:
81     vhost_dev_disable_notifiers(vhost_dev, vdev);
82 
83     return ret;
84 }
85 
86 static void vu_scmi_stop(VirtIODevice *vdev)
87 {
88     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
89     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
90     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
91     struct vhost_dev *vhost_dev = &scmi->vhost_dev;
92     int ret;
93 
94     /* vhost_dev_is_started() check in the callers is not fully reliable. */
95     if (!scmi->started_vu) {
96         return;
97     }
98     scmi->started_vu = false;
99 
100     if (!k->set_guest_notifiers) {
101         return;
102     }
103 
104     vhost_dev_stop(vhost_dev, vdev, true);
105 
106     ret = k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, false);
107     if (ret < 0) {
108         error_report("vhost guest notifier cleanup failed: %d", ret);
109         return;
110     }
111     vhost_dev_disable_notifiers(vhost_dev, vdev);
112 }
113 
114 static void vu_scmi_set_status(VirtIODevice *vdev, uint8_t status)
115 {
116     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
117     bool should_start = virtio_device_should_start(vdev, status);
118 
119     if (!scmi->connected) {
120         return;
121     }
122     if (vhost_dev_is_started(&scmi->vhost_dev) == should_start) {
123         return;
124     }
125 
126     if (should_start) {
127         vu_scmi_start(vdev);
128     } else {
129         vu_scmi_stop(vdev);
130     }
131 }
132 
133 static uint64_t vu_scmi_get_features(VirtIODevice *vdev, uint64_t features,
134                                      Error **errp)
135 {
136     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
137 
138     return vhost_get_features(&scmi->vhost_dev, feature_bits, features);
139 }
140 
141 static void vu_scmi_handle_output(VirtIODevice *vdev, VirtQueue *vq)
142 {
143     /*
144      * Not normally called; it's the daemon that handles the queue;
145      * however virtio's cleanup path can call this.
146      */
147 }
148 
149 static void vu_scmi_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
150 {
151     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
152 
153     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
154         return;
155     }
156 
157     vhost_virtqueue_mask(&scmi->vhost_dev, vdev, idx, mask);
158 }
159 
160 static bool vu_scmi_guest_notifier_pending(VirtIODevice *vdev, int idx)
161 {
162     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
163 
164     return vhost_virtqueue_pending(&scmi->vhost_dev, idx);
165 }
166 
167 static void vu_scmi_connect(DeviceState *dev)
168 {
169     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
170     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
171 
172     if (scmi->connected) {
173         return;
174     }
175     scmi->connected = true;
176 
177     /* restore vhost state */
178     if (virtio_device_started(vdev, vdev->status)) {
179         vu_scmi_start(vdev);
180     }
181 }
182 
183 static void vu_scmi_disconnect(DeviceState *dev)
184 {
185     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
186     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
187 
188     if (!scmi->connected) {
189         return;
190     }
191     scmi->connected = false;
192 
193     if (vhost_dev_is_started(&scmi->vhost_dev)) {
194         vu_scmi_stop(vdev);
195     }
196 }
197 
198 static void vu_scmi_event(void *opaque, QEMUChrEvent event)
199 {
200     DeviceState *dev = opaque;
201 
202     switch (event) {
203     case CHR_EVENT_OPENED:
204         vu_scmi_connect(dev);
205         break;
206     case CHR_EVENT_CLOSED:
207         vu_scmi_disconnect(dev);
208         break;
209     case CHR_EVENT_BREAK:
210     case CHR_EVENT_MUX_IN:
211     case CHR_EVENT_MUX_OUT:
212         /* Ignore */
213         break;
214     }
215 }
216 
217 static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserSCMI *scmi)
218 {
219     virtio_delete_queue(scmi->cmd_vq);
220     virtio_delete_queue(scmi->event_vq);
221     g_free(scmi->vhost_dev.vqs);
222     virtio_cleanup(vdev);
223     vhost_user_cleanup(&scmi->vhost_user);
224 }
225 
226 static void vu_scmi_device_realize(DeviceState *dev, Error **errp)
227 {
228     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
229     VHostUserSCMI *scmi = VHOST_USER_SCMI(dev);
230     int ret;
231 
232     if (!scmi->chardev.chr) {
233         error_setg(errp, "vhost-user-scmi: chardev is mandatory");
234         return;
235     }
236 
237     vdev->host_features |= (1ULL << VIRTIO_SCMI_F_P2A_CHANNELS);
238 
239     if (!vhost_user_init(&scmi->vhost_user, &scmi->chardev, errp)) {
240         return;
241     }
242 
243     virtio_init(vdev, VIRTIO_ID_SCMI, 0);
244 
245     scmi->cmd_vq = virtio_add_queue(vdev, 256, vu_scmi_handle_output);
246     scmi->event_vq = virtio_add_queue(vdev, 256, vu_scmi_handle_output);
247     scmi->vhost_dev.nvqs = 2;
248     scmi->vhost_dev.vqs = g_new0(struct vhost_virtqueue, scmi->vhost_dev.nvqs);
249 
250     ret = vhost_dev_init(&scmi->vhost_dev, &scmi->vhost_user,
251                          VHOST_BACKEND_TYPE_USER, 0, errp);
252     if (ret < 0) {
253         error_setg_errno(errp, -ret,
254                          "vhost-user-scmi: vhost_dev_init() failed");
255         do_vhost_user_cleanup(vdev, scmi);
256         return;
257     }
258 
259     qemu_chr_fe_set_handlers(&scmi->chardev, NULL, NULL, vu_scmi_event, NULL,
260                              dev, NULL, true);
261 
262     return;
263 }
264 
265 static void vu_scmi_device_unrealize(DeviceState *dev)
266 {
267     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
268     VHostUserSCMI *scmi = VHOST_USER_SCMI(dev);
269 
270     vu_scmi_set_status(vdev, 0);
271     vhost_dev_cleanup(&scmi->vhost_dev);
272     do_vhost_user_cleanup(vdev, scmi);
273 }
274 
275 static const VMStateDescription vu_scmi_vmstate = {
276     .name = "vhost-user-scmi",
277     .unmigratable = 1,
278 };
279 
280 static Property vu_scmi_properties[] = {
281     DEFINE_PROP_CHR("chardev", VHostUserSCMI, chardev),
282     DEFINE_PROP_END_OF_LIST(),
283 };
284 
285 static void vu_scmi_class_init(ObjectClass *klass, void *data)
286 {
287     DeviceClass *dc = DEVICE_CLASS(klass);
288     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
289 
290     device_class_set_props(dc, vu_scmi_properties);
291     dc->vmsd = &vu_scmi_vmstate;
292     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
293     vdc->realize = vu_scmi_device_realize;
294     vdc->unrealize = vu_scmi_device_unrealize;
295     vdc->get_features = vu_scmi_get_features;
296     vdc->set_status = vu_scmi_set_status;
297     vdc->guest_notifier_mask = vu_scmi_guest_notifier_mask;
298     vdc->guest_notifier_pending = vu_scmi_guest_notifier_pending;
299 }
300 
301 static const TypeInfo vu_scmi_info = {
302     .name = TYPE_VHOST_USER_SCMI,
303     .parent = TYPE_VIRTIO_DEVICE,
304     .instance_size = sizeof(VHostUserSCMI),
305     .class_init = vu_scmi_class_init,
306 };
307 
308 static void vu_scmi_register_types(void)
309 {
310     type_register_static(&vu_scmi_info);
311 }
312 
313 type_init(vu_scmi_register_types)
314