]>
Commit | Line | Data |
---|---|---|
27ba7b02 VK |
1 | /* |
2 | * Vhost-user GPIO virtio device | |
3 | * | |
4 | * Copyright (c) 2022 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-gpio.h" | |
14 | #include "qemu/error-report.h" | |
15 | #include "standard-headers/linux/virtio_ids.h" | |
16 | #include "trace.h" | |
17 | ||
18 | #define REALIZE_CONNECTION_RETRIES 3 | |
19 | ||
20 | /* Features required from VirtIO */ | |
21 | static const int feature_bits[] = { | |
22 | VIRTIO_F_VERSION_1, | |
23 | VIRTIO_F_NOTIFY_ON_EMPTY, | |
24 | VIRTIO_RING_F_INDIRECT_DESC, | |
25 | VIRTIO_RING_F_EVENT_IDX, | |
26 | VIRTIO_GPIO_F_IRQ, | |
562a7d23 | 27 | VIRTIO_F_RING_RESET, |
27ba7b02 VK |
28 | VHOST_INVALID_FEATURE_BIT |
29 | }; | |
30 | ||
31 | static void vu_gpio_get_config(VirtIODevice *vdev, uint8_t *config) | |
32 | { | |
33 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
34 | ||
35 | memcpy(config, &gpio->config, sizeof(gpio->config)); | |
36 | } | |
37 | ||
38 | static int vu_gpio_config_notifier(struct vhost_dev *dev) | |
39 | { | |
40 | VHostUserGPIO *gpio = VHOST_USER_GPIO(dev->vdev); | |
41 | ||
42 | memcpy(dev->vdev->config, &gpio->config, sizeof(gpio->config)); | |
43 | virtio_notify_config(dev->vdev); | |
44 | ||
45 | return 0; | |
46 | } | |
47 | ||
48 | const VhostDevConfigOps gpio_ops = { | |
49 | .vhost_dev_config_notifier = vu_gpio_config_notifier, | |
50 | }; | |
51 | ||
52 | static int vu_gpio_start(VirtIODevice *vdev) | |
53 | { | |
54 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
55 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
56 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
57 | struct vhost_dev *vhost_dev = &gpio->vhost_dev; | |
58 | int ret, i; | |
59 | ||
60 | if (!k->set_guest_notifiers) { | |
61 | error_report("binding does not support guest notifiers"); | |
62 | return -ENOSYS; | |
63 | } | |
64 | ||
65 | ret = vhost_dev_enable_notifiers(vhost_dev, vdev); | |
66 | if (ret < 0) { | |
67 | error_report("Error enabling host notifiers: %d", ret); | |
68 | return ret; | |
69 | } | |
70 | ||
71 | ret = k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, true); | |
72 | if (ret < 0) { | |
73 | error_report("Error binding guest notifier: %d", ret); | |
74 | goto err_host_notifiers; | |
75 | } | |
76 | ||
77 | /* | |
78 | * Before we start up we need to ensure we have the final feature | |
79 | * set needed for the vhost configuration. The backend may also | |
80 | * apply backend_features when the feature set is sent. | |
81 | */ | |
82 | vhost_ack_features(&gpio->vhost_dev, feature_bits, vdev->guest_features); | |
83 | ||
4daa5054 | 84 | ret = vhost_dev_start(&gpio->vhost_dev, vdev, false); |
27ba7b02 VK |
85 | if (ret < 0) { |
86 | error_report("Error starting vhost-user-gpio: %d", ret); | |
87 | goto err_guest_notifiers; | |
88 | } | |
060f4a94 | 89 | gpio->started_vu = true; |
27ba7b02 VK |
90 | |
91 | /* | |
92 | * guest_notifier_mask/pending not used yet, so just unmask | |
93 | * everything here. virtio-pci will do the right thing by | |
94 | * enabling/disabling irqfd. | |
95 | */ | |
96 | for (i = 0; i < gpio->vhost_dev.nvqs; i++) { | |
97 | vhost_virtqueue_mask(&gpio->vhost_dev, vdev, i, false); | |
98 | } | |
99 | ||
100 | /* | |
101 | * As we must have VHOST_USER_F_PROTOCOL_FEATURES (because | |
102 | * VHOST_USER_GET_CONFIG requires it) we need to explicitly enable | |
103 | * the vrings. | |
104 | */ | |
105 | g_assert(vhost_dev->vhost_ops && | |
106 | vhost_dev->vhost_ops->vhost_set_vring_enable); | |
107 | ret = vhost_dev->vhost_ops->vhost_set_vring_enable(vhost_dev, true); | |
108 | if (ret == 0) { | |
109 | return 0; | |
110 | } | |
111 | ||
112 | error_report("Failed to start vrings for vhost-user-gpio: %d", ret); | |
113 | ||
114 | err_guest_notifiers: | |
115 | k->set_guest_notifiers(qbus->parent, gpio->vhost_dev.nvqs, false); | |
116 | err_host_notifiers: | |
117 | vhost_dev_disable_notifiers(&gpio->vhost_dev, vdev); | |
118 | ||
119 | return ret; | |
120 | } | |
121 | ||
122 | static void vu_gpio_stop(VirtIODevice *vdev) | |
123 | { | |
124 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
125 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
126 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
127 | struct vhost_dev *vhost_dev = &gpio->vhost_dev; | |
128 | int ret; | |
129 | ||
060f4a94 | 130 | if (!gpio->started_vu) { |
27ba7b02 VK |
131 | return; |
132 | } | |
060f4a94 | 133 | gpio->started_vu = false; |
27ba7b02 | 134 | |
060f4a94 | 135 | if (!k->set_guest_notifiers) { |
27ba7b02 VK |
136 | return; |
137 | } | |
138 | ||
4daa5054 | 139 | vhost_dev_stop(vhost_dev, vdev, false); |
27ba7b02 VK |
140 | |
141 | ret = k->set_guest_notifiers(qbus->parent, vhost_dev->nvqs, false); | |
142 | if (ret < 0) { | |
143 | error_report("vhost guest notifier cleanup failed: %d", ret); | |
144 | return; | |
145 | } | |
146 | ||
147 | vhost_dev_disable_notifiers(vhost_dev, vdev); | |
148 | } | |
149 | ||
150 | static void vu_gpio_set_status(VirtIODevice *vdev, uint8_t status) | |
151 | { | |
152 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
259d69c0 | 153 | bool should_start = virtio_device_should_start(vdev, status); |
27ba7b02 VK |
154 | |
155 | trace_virtio_gpio_set_status(status); | |
156 | ||
157 | if (!gpio->connected) { | |
158 | return; | |
159 | } | |
160 | ||
161 | if (vhost_dev_is_started(&gpio->vhost_dev) == should_start) { | |
162 | return; | |
163 | } | |
164 | ||
165 | if (should_start) { | |
166 | if (vu_gpio_start(vdev)) { | |
167 | qemu_chr_fe_disconnect(&gpio->chardev); | |
168 | } | |
169 | } else { | |
170 | vu_gpio_stop(vdev); | |
171 | } | |
172 | } | |
173 | ||
174 | static uint64_t vu_gpio_get_features(VirtIODevice *vdev, uint64_t features, | |
175 | Error **errp) | |
176 | { | |
177 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
178 | ||
179 | return vhost_get_features(&gpio->vhost_dev, feature_bits, features); | |
180 | } | |
181 | ||
182 | static void vu_gpio_handle_output(VirtIODevice *vdev, VirtQueue *vq) | |
183 | { | |
184 | /* | |
185 | * Not normally called; it's the daemon that handles the queue; | |
186 | * however virtio's cleanup path can call this. | |
187 | */ | |
188 | } | |
189 | ||
190 | static void vu_gpio_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask) | |
191 | { | |
192 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
193 | ||
194 | vhost_virtqueue_mask(&gpio->vhost_dev, vdev, idx, mask); | |
195 | } | |
196 | ||
197 | static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserGPIO *gpio) | |
198 | { | |
199 | virtio_delete_queue(gpio->command_vq); | |
200 | virtio_delete_queue(gpio->interrupt_vq); | |
201 | g_free(gpio->vhost_dev.vqs); | |
202 | gpio->vhost_dev.vqs = NULL; | |
203 | virtio_cleanup(vdev); | |
204 | vhost_user_cleanup(&gpio->vhost_user); | |
205 | } | |
206 | ||
207 | static int vu_gpio_connect(DeviceState *dev, Error **errp) | |
208 | { | |
209 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
210 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
211 | struct vhost_dev *vhost_dev = &gpio->vhost_dev; | |
212 | int ret; | |
213 | ||
214 | if (gpio->connected) { | |
215 | return 0; | |
216 | } | |
217 | gpio->connected = true; | |
218 | ||
219 | vhost_dev_set_config_notifier(vhost_dev, &gpio_ops); | |
220 | gpio->vhost_user.supports_config = true; | |
221 | ||
222 | ret = vhost_dev_init(vhost_dev, &gpio->vhost_user, | |
223 | VHOST_BACKEND_TYPE_USER, 0, errp); | |
224 | if (ret < 0) { | |
225 | return ret; | |
226 | } | |
227 | ||
228 | /* restore vhost state */ | |
229 | if (virtio_device_started(vdev, vdev->status)) { | |
230 | vu_gpio_start(vdev); | |
231 | } | |
232 | ||
233 | return 0; | |
234 | } | |
235 | ||
236 | static void vu_gpio_disconnect(DeviceState *dev) | |
237 | { | |
238 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
239 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
240 | ||
241 | if (!gpio->connected) { | |
242 | return; | |
243 | } | |
244 | gpio->connected = false; | |
245 | ||
246 | vu_gpio_stop(vdev); | |
247 | vhost_dev_cleanup(&gpio->vhost_dev); | |
248 | } | |
249 | ||
250 | static void vu_gpio_event(void *opaque, QEMUChrEvent event) | |
251 | { | |
252 | DeviceState *dev = opaque; | |
253 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
254 | VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); | |
255 | Error *local_err = NULL; | |
256 | ||
257 | switch (event) { | |
258 | case CHR_EVENT_OPENED: | |
259 | if (vu_gpio_connect(dev, &local_err) < 0) { | |
260 | qemu_chr_fe_disconnect(&gpio->chardev); | |
261 | return; | |
262 | } | |
263 | break; | |
264 | case CHR_EVENT_CLOSED: | |
265 | vu_gpio_disconnect(dev); | |
266 | break; | |
267 | case CHR_EVENT_BREAK: | |
268 | case CHR_EVENT_MUX_IN: | |
269 | case CHR_EVENT_MUX_OUT: | |
270 | /* Ignore */ | |
271 | break; | |
272 | } | |
273 | } | |
274 | ||
275 | static int vu_gpio_realize_connect(VHostUserGPIO *gpio, Error **errp) | |
276 | { | |
277 | VirtIODevice *vdev = &gpio->parent_obj; | |
278 | DeviceState *dev = &vdev->parent_obj; | |
279 | struct vhost_dev *vhost_dev = &gpio->vhost_dev; | |
280 | int ret; | |
281 | ||
282 | ret = qemu_chr_fe_wait_connected(&gpio->chardev, errp); | |
283 | if (ret < 0) { | |
284 | return ret; | |
285 | } | |
286 | ||
287 | /* | |
288 | * vu_gpio_connect() may have already connected (via the event | |
289 | * callback) in which case it will just report success. | |
290 | */ | |
291 | ret = vu_gpio_connect(dev, errp); | |
292 | if (ret < 0) { | |
293 | qemu_chr_fe_disconnect(&gpio->chardev); | |
294 | return ret; | |
295 | } | |
296 | g_assert(gpio->connected); | |
297 | ||
298 | ret = vhost_dev_get_config(vhost_dev, (uint8_t *)&gpio->config, | |
299 | sizeof(gpio->config), errp); | |
300 | ||
301 | if (ret < 0) { | |
302 | error_report("vhost-user-gpio: get config failed"); | |
303 | ||
304 | qemu_chr_fe_disconnect(&gpio->chardev); | |
305 | vhost_dev_cleanup(vhost_dev); | |
306 | return ret; | |
307 | } | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | static void vu_gpio_device_realize(DeviceState *dev, Error **errp) | |
313 | { | |
314 | ERRP_GUARD(); | |
315 | ||
316 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
317 | VHostUserGPIO *gpio = VHOST_USER_GPIO(dev); | |
318 | int retries, ret; | |
319 | ||
320 | if (!gpio->chardev.chr) { | |
321 | error_setg(errp, "vhost-user-gpio: chardev is mandatory"); | |
322 | return; | |
323 | } | |
324 | ||
325 | if (!vhost_user_init(&gpio->vhost_user, &gpio->chardev, errp)) { | |
326 | return; | |
327 | } | |
328 | ||
329 | virtio_init(vdev, VIRTIO_ID_GPIO, sizeof(gpio->config)); | |
330 | ||
331 | gpio->vhost_dev.nvqs = 2; | |
332 | gpio->command_vq = virtio_add_queue(vdev, 256, vu_gpio_handle_output); | |
333 | gpio->interrupt_vq = virtio_add_queue(vdev, 256, vu_gpio_handle_output); | |
334 | gpio->vhost_dev.vqs = g_new0(struct vhost_virtqueue, gpio->vhost_dev.nvqs); | |
335 | ||
336 | gpio->connected = false; | |
337 | ||
338 | qemu_chr_fe_set_handlers(&gpio->chardev, NULL, NULL, vu_gpio_event, NULL, | |
339 | dev, NULL, true); | |
340 | ||
341 | retries = REALIZE_CONNECTION_RETRIES; | |
342 | g_assert(!*errp); | |
343 | do { | |
344 | if (*errp) { | |
345 | error_prepend(errp, "Reconnecting after error: "); | |
346 | error_report_err(*errp); | |
347 | *errp = NULL; | |
348 | } | |
349 | ret = vu_gpio_realize_connect(gpio, errp); | |
350 | } while (ret < 0 && retries--); | |
351 | ||
352 | if (ret < 0) { | |
353 | do_vhost_user_cleanup(vdev, gpio); | |
354 | } | |
355 | ||
356 | return; | |
357 | } | |
358 | ||
359 | static void vu_gpio_device_unrealize(DeviceState *dev) | |
360 | { | |
361 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
362 | VHostUserGPIO *gpio = VHOST_USER_GPIO(dev); | |
363 | ||
364 | vu_gpio_set_status(vdev, 0); | |
365 | qemu_chr_fe_set_handlers(&gpio->chardev, NULL, NULL, NULL, NULL, NULL, NULL, | |
366 | false); | |
367 | vhost_dev_cleanup(&gpio->vhost_dev); | |
368 | do_vhost_user_cleanup(vdev, gpio); | |
369 | } | |
370 | ||
371 | static const VMStateDescription vu_gpio_vmstate = { | |
372 | .name = "vhost-user-gpio", | |
373 | .unmigratable = 1, | |
374 | }; | |
375 | ||
376 | static Property vu_gpio_properties[] = { | |
377 | DEFINE_PROP_CHR("chardev", VHostUserGPIO, chardev), | |
378 | DEFINE_PROP_END_OF_LIST(), | |
379 | }; | |
380 | ||
381 | static void vu_gpio_class_init(ObjectClass *klass, void *data) | |
382 | { | |
383 | DeviceClass *dc = DEVICE_CLASS(klass); | |
384 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
385 | ||
386 | device_class_set_props(dc, vu_gpio_properties); | |
387 | dc->vmsd = &vu_gpio_vmstate; | |
388 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); | |
389 | vdc->realize = vu_gpio_device_realize; | |
390 | vdc->unrealize = vu_gpio_device_unrealize; | |
391 | vdc->get_features = vu_gpio_get_features; | |
392 | vdc->get_config = vu_gpio_get_config; | |
393 | vdc->set_status = vu_gpio_set_status; | |
394 | vdc->guest_notifier_mask = vu_gpio_guest_notifier_mask; | |
395 | } | |
396 | ||
397 | static const TypeInfo vu_gpio_info = { | |
398 | .name = TYPE_VHOST_USER_GPIO, | |
399 | .parent = TYPE_VIRTIO_DEVICE, | |
400 | .instance_size = sizeof(VHostUserGPIO), | |
401 | .class_init = vu_gpio_class_init, | |
402 | }; | |
403 | ||
404 | static void vu_gpio_register_types(void) | |
405 | { | |
406 | type_register_static(&vu_gpio_info); | |
407 | } | |
408 | ||
409 | type_init(vu_gpio_register_types) |