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