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