1 /* 2 * Virtio Support 3 * 4 * Copyright IBM, Corp. 2007 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 */ 13 14 #ifndef QEMU_VIRTIO_H 15 #define QEMU_VIRTIO_H 16 17 #include "system/memory.h" 18 #include "hw/qdev-core.h" 19 #include "net/net.h" 20 #include "migration/vmstate.h" 21 #include "qemu/event_notifier.h" 22 #include "standard-headers/linux/virtio_config.h" 23 #include "standard-headers/linux/virtio_ring.h" 24 #include "qom/object.h" 25 #include "block/aio.h" 26 27 /* 28 * A guest should never accept this. It implies negotiation is broken 29 * between the driver frontend and the device. This bit is re-used for 30 * vhost-user to advertise VHOST_USER_F_PROTOCOL_FEATURES between QEMU 31 * and a vhost-user backend. 32 */ 33 #define VIRTIO_F_BAD_FEATURE 30 34 35 #define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \ 36 (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \ 37 (0x1ULL << VIRTIO_F_ANY_LAYOUT)) 38 39 struct VirtQueue; 40 41 static inline hwaddr vring_align(hwaddr addr, 42 unsigned long align) 43 { 44 return QEMU_ALIGN_UP(addr, align); 45 } 46 47 typedef struct VirtIOFeature { 48 uint64_t flags; 49 size_t end; 50 } VirtIOFeature; 51 52 typedef struct VirtIOConfigSizeParams { 53 size_t min_size; 54 size_t max_size; 55 const VirtIOFeature *feature_sizes; 56 } VirtIOConfigSizeParams; 57 58 size_t virtio_get_config_size(const VirtIOConfigSizeParams *params, 59 uint64_t host_features); 60 61 typedef struct VirtQueue VirtQueue; 62 63 #define VIRTQUEUE_MAX_SIZE 1024 64 65 typedef struct VirtQueueElement 66 { 67 unsigned int index; 68 unsigned int len; 69 unsigned int ndescs; 70 unsigned int out_num; 71 unsigned int in_num; 72 /* Element has been processed (VIRTIO_F_IN_ORDER) */ 73 bool in_order_filled; 74 hwaddr *in_addr; 75 hwaddr *out_addr; 76 struct iovec *in_sg; 77 struct iovec *out_sg; 78 } VirtQueueElement; 79 80 #define VIRTIO_QUEUE_MAX 1024 81 82 #define VIRTIO_NO_VECTOR 0xffff 83 84 /* special index value used internally for config irqs */ 85 #define VIRTIO_CONFIG_IRQ_IDX -1 86 87 #define TYPE_VIRTIO_DEVICE "virtio-device" 88 OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE) 89 90 typedef struct { 91 int virtio_bit; 92 const char *feature_desc; 93 } qmp_virtio_feature_map_t; 94 95 enum virtio_device_endian { 96 VIRTIO_DEVICE_ENDIAN_UNKNOWN, 97 VIRTIO_DEVICE_ENDIAN_LITTLE, 98 VIRTIO_DEVICE_ENDIAN_BIG, 99 }; 100 101 /** 102 * struct VirtIODevice - common VirtIO structure 103 * @name: name of the device 104 * @status: VirtIO Device Status field 105 * 106 */ 107 struct VirtIODevice 108 { 109 DeviceState parent_obj; 110 const char *name; 111 uint8_t status; 112 uint8_t isr; 113 uint16_t queue_sel; 114 /** 115 * These fields represent a set of VirtIO features at various 116 * levels of the stack. @host_features indicates the complete 117 * feature set the VirtIO device can offer to the driver. 118 * @guest_features indicates which features the VirtIO driver has 119 * selected by writing to the feature register. Finally 120 * @backend_features represents everything supported by the 121 * backend (e.g. vhost) and could potentially be a subset of the 122 * total feature set offered by QEMU. 123 */ 124 uint64_t host_features; 125 uint64_t guest_features; 126 uint64_t backend_features; 127 128 size_t config_len; 129 void *config; 130 uint16_t config_vector; 131 uint32_t generation; 132 int nvectors; 133 VirtQueue *vq; 134 MemoryListener listener; 135 uint16_t device_id; 136 /* @vm_running: current VM running state via virtio_vmstate_change() */ 137 bool vm_running; 138 bool broken; /* device in invalid state, needs reset */ 139 bool use_disabled_flag; /* allow use of 'disable' flag when needed */ 140 bool disabled; /* device in temporarily disabled state */ 141 /** 142 * @use_started: true if the @started flag should be used to check the 143 * current state of the VirtIO device. Otherwise status bits 144 * should be checked for a current status of the device. 145 * @use_started is only set via QMP and defaults to true for all 146 * modern machines (since 4.1). 147 */ 148 bool use_started; 149 bool started; 150 bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */ 151 bool disable_legacy_check; 152 bool vhost_started; 153 VMChangeStateEntry *vmstate; 154 char *bus_name; 155 uint8_t device_endian; 156 /** 157 * @user_guest_notifier_mask: gate usage of ->guest_notifier_mask() callback. 158 * This is used to suppress the masking of guest updates for 159 * vhost-user devices which are asynchronous by design. 160 */ 161 bool use_guest_notifier_mask; 162 AddressSpace *dma_as; 163 QLIST_HEAD(, VirtQueue) *vector_queues; 164 QTAILQ_ENTRY(VirtIODevice) next; 165 /** 166 * @config_notifier: the event notifier that handles config events 167 */ 168 EventNotifier config_notifier; 169 bool device_iotlb_enabled; 170 }; 171 172 struct VirtioDeviceClass { 173 /*< private >*/ 174 DeviceClass parent; 175 /*< public >*/ 176 177 /* This is what a VirtioDevice must implement */ 178 DeviceRealize realize; 179 DeviceUnrealize unrealize; 180 uint64_t (*get_features)(VirtIODevice *vdev, 181 uint64_t requested_features, 182 Error **errp); 183 uint64_t (*bad_features)(VirtIODevice *vdev); 184 void (*set_features)(VirtIODevice *vdev, uint64_t val); 185 int (*validate_features)(VirtIODevice *vdev); 186 void (*get_config)(VirtIODevice *vdev, uint8_t *config); 187 void (*set_config)(VirtIODevice *vdev, const uint8_t *config); 188 void (*reset)(VirtIODevice *vdev); 189 int (*set_status)(VirtIODevice *vdev, uint8_t val); 190 /* Device must validate queue_index. */ 191 void (*queue_reset)(VirtIODevice *vdev, uint32_t queue_index); 192 /* Device must validate queue_index. */ 193 void (*queue_enable)(VirtIODevice *vdev, uint32_t queue_index); 194 /* For transitional devices, this is a bitmap of features 195 * that are only exposed on the legacy interface but not 196 * the modern one. 197 */ 198 uint64_t legacy_features; 199 /* Test and clear event pending status. 200 * Should be called after unmask to avoid losing events. 201 * If backend does not support masking, 202 * must check in frontend instead. 203 */ 204 bool (*guest_notifier_pending)(VirtIODevice *vdev, int n); 205 /* Mask/unmask events from this vq. Any events reported 206 * while masked will become pending. 207 * If backend does not support masking, 208 * must mask in frontend instead. 209 */ 210 void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask); 211 int (*start_ioeventfd)(VirtIODevice *vdev); 212 void (*stop_ioeventfd)(VirtIODevice *vdev); 213 /* 214 * Called before loading queues. 215 * If the number of queues change at runtime, use @n to know the 216 * number and add or remove queues accordingly. 217 * Note that this function is called in the middle of loading vmsd; 218 * no assumption should be made on states being loaded from vmsd. 219 */ 220 int (*pre_load_queues)(VirtIODevice *vdev, uint32_t n); 221 /* Saving and loading of a device; trying to deprecate save/load 222 * use vmsd for new devices. 223 */ 224 void (*save)(VirtIODevice *vdev, QEMUFile *f); 225 int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id); 226 /* Post load hook in vmsd is called early while device is processed, and 227 * when VirtIODevice isn't fully initialized. Devices should use this instead, 228 * unless they specifically want to verify the migration stream as it's 229 * processed, e.g. for bounds checking. 230 */ 231 int (*post_load)(VirtIODevice *vdev); 232 const VMStateDescription *vmsd; 233 bool (*primary_unplug_pending)(void *opaque); 234 /* May be called even when vdev->vhost_started is false */ 235 struct vhost_dev *(*get_vhost)(VirtIODevice *vdev); 236 void (*toggle_device_iotlb)(VirtIODevice *vdev); 237 }; 238 239 void virtio_instance_init_common(Object *proxy_obj, void *data, 240 size_t vdev_size, const char *vdev_name); 241 242 /** 243 * virtio_init() - initialise the common VirtIODevice structure 244 * @vdev: pointer to VirtIODevice 245 * @device_id: the VirtIO device ID (see virtio_ids.h) 246 * @config_size: size of the config space 247 */ 248 void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size); 249 250 void virtio_cleanup(VirtIODevice *vdev); 251 252 void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3); 253 254 /* Set the child bus name. */ 255 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name); 256 257 typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *); 258 259 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, 260 VirtIOHandleOutput handle_output); 261 262 void virtio_del_queue(VirtIODevice *vdev, int n); 263 264 void virtio_delete_queue(VirtQueue *vq); 265 266 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, 267 unsigned int len); 268 void virtqueue_flush(VirtQueue *vq, unsigned int count); 269 void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem, 270 unsigned int len); 271 void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem, 272 unsigned int len); 273 bool virtqueue_rewind(VirtQueue *vq, unsigned int num); 274 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, 275 unsigned int len, unsigned int idx); 276 277 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem); 278 void *virtqueue_pop(VirtQueue *vq, size_t sz); 279 unsigned int virtqueue_drop_all(VirtQueue *vq); 280 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz); 281 void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, 282 VirtQueueElement *elem); 283 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, 284 unsigned int out_bytes); 285 /** 286 * Return <0 on error or an opaque >=0 to pass to 287 * virtio_queue_enable_notification_and_check on success. 288 */ 289 int virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, 290 unsigned int *out_bytes, unsigned max_in_bytes, 291 unsigned max_out_bytes); 292 293 void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq); 294 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq); 295 296 int virtio_save(VirtIODevice *vdev, QEMUFile *f); 297 298 extern const VMStateInfo virtio_vmstate_info; 299 300 #define VMSTATE_VIRTIO_DEVICE \ 301 { \ 302 .name = "virtio", \ 303 .info = &virtio_vmstate_info, \ 304 .flags = VMS_SINGLE, \ 305 } 306 307 int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id); 308 309 /** 310 * virtio_notify_config() - signal a change to device config 311 * @vdev: the virtio device 312 * 313 * Assuming the virtio device is up (VIRTIO_CONFIG_S_DRIVER_OK) this 314 * will trigger a guest interrupt and update the config version. 315 */ 316 void virtio_notify_config(VirtIODevice *vdev); 317 318 bool virtio_queue_get_notification(VirtQueue *vq); 319 void virtio_queue_set_notification(VirtQueue *vq, int enable); 320 321 int virtio_queue_ready(VirtQueue *vq); 322 323 int virtio_queue_empty(VirtQueue *vq); 324 325 /** 326 * Enable notification and check whether guest has added some 327 * buffers since last call to virtqueue_get_avail_bytes. 328 * 329 * @opaque: value returned from virtqueue_get_avail_bytes 330 */ 331 bool virtio_queue_enable_notification_and_check(VirtQueue *vq, 332 int opaque); 333 334 void virtio_queue_set_shadow_avail_idx(VirtQueue *vq, uint16_t idx); 335 336 /* Host binding interface. */ 337 338 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr); 339 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr); 340 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr); 341 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data); 342 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data); 343 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data); 344 uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr); 345 uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr); 346 uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr); 347 void virtio_config_modern_writeb(VirtIODevice *vdev, 348 uint32_t addr, uint32_t data); 349 void virtio_config_modern_writew(VirtIODevice *vdev, 350 uint32_t addr, uint32_t data); 351 void virtio_config_modern_writel(VirtIODevice *vdev, 352 uint32_t addr, uint32_t data); 353 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr); 354 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n); 355 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num); 356 int virtio_queue_get_num(VirtIODevice *vdev, int n); 357 int virtio_queue_get_max_num(VirtIODevice *vdev, int n); 358 int virtio_get_num_queues(VirtIODevice *vdev); 359 void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc, 360 hwaddr avail, hwaddr used); 361 void virtio_queue_update_rings(VirtIODevice *vdev, int n); 362 void virtio_init_region_cache(VirtIODevice *vdev, int n); 363 void virtio_queue_set_align(VirtIODevice *vdev, int n, int align); 364 void virtio_queue_notify(VirtIODevice *vdev, int n); 365 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n); 366 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector); 367 int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n, 368 MemoryRegion *mr, bool assign); 369 int virtio_set_status(VirtIODevice *vdev, uint8_t val); 370 void virtio_reset(void *opaque); 371 void virtio_queue_reset(VirtIODevice *vdev, uint32_t queue_index); 372 void virtio_queue_enable(VirtIODevice *vdev, uint32_t queue_index); 373 void virtio_update_irq(VirtIODevice *vdev); 374 int virtio_set_features(VirtIODevice *vdev, uint64_t val); 375 376 /* Base devices. */ 377 typedef struct VirtIOBlkConf VirtIOBlkConf; 378 struct virtio_net_conf; 379 typedef struct virtio_serial_conf virtio_serial_conf; 380 typedef struct virtio_input_conf virtio_input_conf; 381 typedef struct VirtIOSCSIConf VirtIOSCSIConf; 382 typedef struct VirtIORNGConf VirtIORNGConf; 383 384 #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \ 385 DEFINE_PROP_BIT64("indirect_desc", _state, _field, \ 386 VIRTIO_RING_F_INDIRECT_DESC, true), \ 387 DEFINE_PROP_BIT64("event_idx", _state, _field, \ 388 VIRTIO_RING_F_EVENT_IDX, true), \ 389 DEFINE_PROP_BIT64("notify_on_empty", _state, _field, \ 390 VIRTIO_F_NOTIFY_ON_EMPTY, true), \ 391 DEFINE_PROP_BIT64("any_layout", _state, _field, \ 392 VIRTIO_F_ANY_LAYOUT, true), \ 393 DEFINE_PROP_BIT64("iommu_platform", _state, _field, \ 394 VIRTIO_F_IOMMU_PLATFORM, false), \ 395 DEFINE_PROP_BIT64("packed", _state, _field, \ 396 VIRTIO_F_RING_PACKED, false), \ 397 DEFINE_PROP_BIT64("queue_reset", _state, _field, \ 398 VIRTIO_F_RING_RESET, true), \ 399 DEFINE_PROP_BIT64("in_order", _state, _field, \ 400 VIRTIO_F_IN_ORDER, false) 401 402 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n); 403 bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n); 404 bool virtio_queue_enabled(VirtIODevice *vdev, int n); 405 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n); 406 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n); 407 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n); 408 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n); 409 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n); 410 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n); 411 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, 412 unsigned int idx); 413 void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n); 414 void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n); 415 void virtio_queue_update_used_idx(VirtIODevice *vdev, int n); 416 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n); 417 uint16_t virtio_get_queue_index(VirtQueue *vq); 418 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq); 419 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, 420 bool with_irqfd); 421 int virtio_device_start_ioeventfd(VirtIODevice *vdev); 422 int virtio_device_grab_ioeventfd(VirtIODevice *vdev); 423 void virtio_device_release_ioeventfd(VirtIODevice *vdev); 424 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev); 425 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq); 426 void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled); 427 void virtio_queue_host_notifier_read(EventNotifier *n); 428 void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx); 429 void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx); 430 void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx); 431 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector); 432 VirtQueue *virtio_vector_next_queue(VirtQueue *vq); 433 EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev); 434 void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev, 435 bool assign, bool with_irqfd); 436 437 static inline void virtio_add_feature(uint64_t *features, unsigned int fbit) 438 { 439 assert(fbit < 64); 440 *features |= (1ULL << fbit); 441 } 442 443 static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit) 444 { 445 assert(fbit < 64); 446 *features &= ~(1ULL << fbit); 447 } 448 449 static inline bool virtio_has_feature(uint64_t features, unsigned int fbit) 450 { 451 assert(fbit < 64); 452 return !!(features & (1ULL << fbit)); 453 } 454 455 static inline bool virtio_vdev_has_feature(const VirtIODevice *vdev, 456 unsigned int fbit) 457 { 458 return virtio_has_feature(vdev->guest_features, fbit); 459 } 460 461 static inline bool virtio_host_has_feature(VirtIODevice *vdev, 462 unsigned int fbit) 463 { 464 return virtio_has_feature(vdev->host_features, fbit); 465 } 466 467 static inline bool virtio_is_big_endian(VirtIODevice *vdev) 468 { 469 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { 470 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); 471 return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG; 472 } 473 /* Devices conforming to VIRTIO 1.0 or later are always LE. */ 474 return false; 475 } 476 477 /** 478 * virtio_device_started() - check if device started 479 * @vdev - the VirtIO device 480 * @status - the devices status bits 481 * 482 * Check if the device is started. For most modern machines this is 483 * tracked via the @vdev->started field (to support migration), 484 * otherwise we check for the final negotiated status bit that 485 * indicates everything is ready. 486 */ 487 static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status) 488 { 489 if (vdev->use_started) { 490 return vdev->started; 491 } 492 493 return status & VIRTIO_CONFIG_S_DRIVER_OK; 494 } 495 496 /** 497 * virtio_device_should_start() - check if device startable 498 * @vdev - the VirtIO device 499 * @status - the devices status bits 500 * 501 * This is similar to virtio_device_started() but ignores vdev->started 502 * and also encapsulates a check on the VM status which would prevent a 503 * device from starting anyway. 504 */ 505 static inline bool virtio_device_should_start(VirtIODevice *vdev, uint8_t status) 506 { 507 if (!vdev->vm_running) { 508 return false; 509 } 510 511 return status & VIRTIO_CONFIG_S_DRIVER_OK; 512 } 513 514 static inline void virtio_set_started(VirtIODevice *vdev, bool started) 515 { 516 if (started) { 517 vdev->start_on_kick = false; 518 } 519 520 if (vdev->use_started) { 521 vdev->started = started; 522 } 523 } 524 525 static inline void virtio_set_disabled(VirtIODevice *vdev, bool disable) 526 { 527 if (vdev->use_disabled_flag) { 528 vdev->disabled = disable; 529 } 530 } 531 532 static inline bool virtio_device_disabled(VirtIODevice *vdev) 533 { 534 return unlikely(vdev->disabled || vdev->broken); 535 } 536 537 bool virtio_legacy_allowed(VirtIODevice *vdev); 538 bool virtio_legacy_check_disabled(VirtIODevice *vdev); 539 540 QEMUBH *virtio_bh_new_guarded_full(DeviceState *dev, 541 QEMUBHFunc *cb, void *opaque, 542 const char *name); 543 #define virtio_bh_new_guarded(dev, cb, opaque) \ 544 virtio_bh_new_guarded_full((dev), (cb), (opaque), (stringify(cb))) 545 546 #endif 547