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