xref: /openbmc/qemu/hw/virtio/vhost-user-scmi.c (revision bac4711b)
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 
67     /*
68      * guest_notifier_mask/pending not used yet, so just unmask
69      * everything here. virtio-pci will do the right thing by
70      * enabling/disabling irqfd.
71      */
72     for (i = 0; i < scmi->vhost_dev.nvqs; i++) {
73         vhost_virtqueue_mask(&scmi->vhost_dev, vdev, i, false);
74     }
75     return 0;
76 
77 err_guest_notifiers:
78     k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, false);
79 err_host_notifiers:
80     vhost_dev_disable_notifiers(vhost_dev, vdev);
81 
82     return ret;
83 }
84 
85 static void vu_scmi_stop(VirtIODevice *vdev)
86 {
87     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
88     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
89     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
90     struct vhost_dev *vhost_dev = &scmi->vhost_dev;
91     int ret;
92 
93     if (!k->set_guest_notifiers) {
94         return;
95     }
96 
97     vhost_dev_stop(vhost_dev, vdev, true);
98 
99     ret = k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, false);
100     if (ret < 0) {
101         error_report("vhost guest notifier cleanup failed: %d", ret);
102         return;
103     }
104     vhost_dev_disable_notifiers(vhost_dev, vdev);
105 }
106 
107 static void vu_scmi_set_status(VirtIODevice *vdev, uint8_t status)
108 {
109     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
110     bool should_start = virtio_device_should_start(vdev, status);
111 
112     if (!scmi->connected) {
113         return;
114     }
115     if (vhost_dev_is_started(&scmi->vhost_dev) == should_start) {
116         return;
117     }
118 
119     if (should_start) {
120         vu_scmi_start(vdev);
121     } else {
122         vu_scmi_stop(vdev);
123     }
124 }
125 
126 static uint64_t vu_scmi_get_features(VirtIODevice *vdev, uint64_t features,
127                                      Error **errp)
128 {
129     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
130 
131     return vhost_get_features(&scmi->vhost_dev, feature_bits, features);
132 }
133 
134 static void vu_scmi_handle_output(VirtIODevice *vdev, VirtQueue *vq)
135 {
136     /*
137      * Not normally called; it's the daemon that handles the queue;
138      * however virtio's cleanup path can call this.
139      */
140 }
141 
142 static void vu_scmi_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
143 {
144     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
145 
146     if (idx == VIRTIO_CONFIG_IRQ_IDX) {
147         return;
148     }
149 
150     vhost_virtqueue_mask(&scmi->vhost_dev, vdev, idx, mask);
151 }
152 
153 static bool vu_scmi_guest_notifier_pending(VirtIODevice *vdev, int idx)
154 {
155     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
156 
157     return vhost_virtqueue_pending(&scmi->vhost_dev, idx);
158 }
159 
160 static void vu_scmi_connect(DeviceState *dev)
161 {
162     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
163     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
164 
165     if (scmi->connected) {
166         return;
167     }
168     scmi->connected = true;
169 
170     /* restore vhost state */
171     if (virtio_device_started(vdev, vdev->status)) {
172         vu_scmi_start(vdev);
173     }
174 }
175 
176 static void vu_scmi_disconnect(DeviceState *dev)
177 {
178     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
179     VHostUserSCMI *scmi = VHOST_USER_SCMI(vdev);
180 
181     if (!scmi->connected) {
182         return;
183     }
184     scmi->connected = false;
185 
186     if (vhost_dev_is_started(&scmi->vhost_dev)) {
187         vu_scmi_stop(vdev);
188     }
189 }
190 
191 static void vu_scmi_event(void *opaque, QEMUChrEvent event)
192 {
193     DeviceState *dev = opaque;
194 
195     switch (event) {
196     case CHR_EVENT_OPENED:
197         vu_scmi_connect(dev);
198         break;
199     case CHR_EVENT_CLOSED:
200         vu_scmi_disconnect(dev);
201         break;
202     case CHR_EVENT_BREAK:
203     case CHR_EVENT_MUX_IN:
204     case CHR_EVENT_MUX_OUT:
205         /* Ignore */
206         break;
207     }
208 }
209 
210 static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserSCMI *scmi)
211 {
212     virtio_delete_queue(scmi->cmd_vq);
213     virtio_delete_queue(scmi->event_vq);
214     g_free(scmi->vhost_dev.vqs);
215     virtio_cleanup(vdev);
216     vhost_user_cleanup(&scmi->vhost_user);
217 }
218 
219 static void vu_scmi_device_realize(DeviceState *dev, Error **errp)
220 {
221     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
222     VHostUserSCMI *scmi = VHOST_USER_SCMI(dev);
223     int ret;
224 
225     if (!scmi->chardev.chr) {
226         error_setg(errp, "vhost-user-scmi: chardev is mandatory");
227         return;
228     }
229 
230     vdev->host_features |= (1ULL << VIRTIO_SCMI_F_P2A_CHANNELS);
231 
232     if (!vhost_user_init(&scmi->vhost_user, &scmi->chardev, errp)) {
233         return;
234     }
235 
236     virtio_init(vdev, VIRTIO_ID_SCMI, 0);
237 
238     scmi->cmd_vq = virtio_add_queue(vdev, 256, vu_scmi_handle_output);
239     scmi->event_vq = virtio_add_queue(vdev, 256, vu_scmi_handle_output);
240     scmi->vhost_dev.nvqs = 2;
241     scmi->vhost_dev.vqs = g_new0(struct vhost_virtqueue, scmi->vhost_dev.nvqs);
242 
243     ret = vhost_dev_init(&scmi->vhost_dev, &scmi->vhost_user,
244                          VHOST_BACKEND_TYPE_USER, 0, errp);
245     if (ret < 0) {
246         error_setg_errno(errp, -ret,
247                          "vhost-user-scmi: vhost_dev_init() failed");
248         do_vhost_user_cleanup(vdev, scmi);
249         return;
250     }
251 
252     qemu_chr_fe_set_handlers(&scmi->chardev, NULL, NULL, vu_scmi_event, NULL,
253                              dev, NULL, true);
254 
255     return;
256 }
257 
258 static void vu_scmi_device_unrealize(DeviceState *dev)
259 {
260     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
261     VHostUserSCMI *scmi = VHOST_USER_SCMI(dev);
262 
263     vu_scmi_set_status(vdev, 0);
264     vhost_dev_cleanup(&scmi->vhost_dev);
265     do_vhost_user_cleanup(vdev, scmi);
266 }
267 
268 static const VMStateDescription vu_scmi_vmstate = {
269     .name = "vhost-user-scmi",
270     .unmigratable = 1,
271 };
272 
273 static Property vu_scmi_properties[] = {
274     DEFINE_PROP_CHR("chardev", VHostUserSCMI, chardev),
275     DEFINE_PROP_END_OF_LIST(),
276 };
277 
278 static void vu_scmi_class_init(ObjectClass *klass, void *data)
279 {
280     DeviceClass *dc = DEVICE_CLASS(klass);
281     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
282 
283     device_class_set_props(dc, vu_scmi_properties);
284     dc->vmsd = &vu_scmi_vmstate;
285     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
286     vdc->realize = vu_scmi_device_realize;
287     vdc->unrealize = vu_scmi_device_unrealize;
288     vdc->get_features = vu_scmi_get_features;
289     vdc->set_status = vu_scmi_set_status;
290     vdc->guest_notifier_mask = vu_scmi_guest_notifier_mask;
291     vdc->guest_notifier_pending = vu_scmi_guest_notifier_pending;
292 }
293 
294 static const TypeInfo vu_scmi_info = {
295     .name = TYPE_VHOST_USER_SCMI,
296     .parent = TYPE_VIRTIO_DEVICE,
297     .instance_size = sizeof(VHostUserSCMI),
298     .class_init = vu_scmi_class_init,
299 };
300 
301 static void vu_scmi_register_types(void)
302 {
303     type_register_static(&vu_scmi_info);
304 }
305 
306 type_init(vu_scmi_register_types)
307