| 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/core/qdev.h" |
| 19 | #include "hw/virtio/virtio-features.h" |
| 20 | #include "net/net.h" |
| 21 | #include "migration/vmstate.h" |
| 22 | #include "qemu/event_notifier.h" |
| 23 | #include "standard-headers/linux/virtio_config.h" |
| 24 | #include "standard-headers/linux/virtio_ring.h" |
| 25 | #include "qom/object.h" |
| 26 | #include "qemu/aio.h" |
| 27 | |
| 28 | /* |
| 29 | * A guest should never accept this. It implies negotiation is broken |
| 30 | * between the driver frontend and the device. This bit is re-used for |
| 31 | * vhost-user to advertise VHOST_USER_F_PROTOCOL_FEATURES between QEMU |
| 32 | * and a vhost-user backend. |
| 33 | */ |
| 34 | #define VIRTIO_F_BAD_FEATURE 30 |
| 35 | |
| 36 | #define VIRTIO_LEGACY_FEATURES ((0x1ULL << VIRTIO_F_BAD_FEATURE) | \ |
| 37 | (0x1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \ |
| 38 | (0x1ULL << VIRTIO_F_ANY_LAYOUT)) |
| 39 | |
| 40 | struct VirtQueue; |
| 41 | |
| 42 | static inline hwaddr vring_align(hwaddr addr, |
| 43 | unsigned long align) |
| 44 | { |
| 45 | return QEMU_ALIGN_UP(addr, align); |
| 46 | } |
| 47 | |
| 48 | typedef struct VirtIOFeature { |
| 49 | uint64_t flags; |
| 50 | size_t end; |
| 51 | } VirtIOFeature; |
| 52 | |
| 53 | typedef struct VirtIOConfigSizeParams { |
| 54 | size_t min_size; |
| 55 | size_t max_size; |
| 56 | const VirtIOFeature *feature_sizes; |
| 57 | } VirtIOConfigSizeParams; |
| 58 | |
| 59 | size_t virtio_get_config_size(const VirtIOConfigSizeParams *params, |
| 60 | uint64_t host_features); |
| 61 | |
| 62 | typedef struct VirtQueue VirtQueue; |
| 63 | |
| 64 | #define VIRTQUEUE_MAX_SIZE 1024 |
| 65 | |
| 66 | typedef struct VirtQueueElement |
| 67 | { |
| 68 | unsigned int index; |
| 69 | unsigned int len; |
| 70 | unsigned int ndescs; |
| 71 | unsigned int out_num; |
| 72 | unsigned int in_num; |
| 73 | /* Element has been processed (VIRTIO_F_IN_ORDER) */ |
| 74 | bool in_order_filled; |
| 75 | hwaddr *in_addr; |
| 76 | hwaddr *out_addr; |
| 77 | struct iovec *in_sg; |
| 78 | struct iovec *out_sg; |
| 79 | } VirtQueueElement; |
| 80 | |
| 81 | #define VIRTIO_QUEUE_MAX 1024 |
| 82 | |
| 83 | #define VIRTIO_NO_VECTOR 0xffff |
| 84 | |
| 85 | #define VIRTIO_MAX_SHMEM_REGIONS 256 |
| 86 | |
| 87 | /* special index value used internally for config irqs */ |
| 88 | #define VIRTIO_CONFIG_IRQ_IDX -1 |
| 89 | |
| 90 | #define TYPE_VIRTIO_DEVICE "virtio-device" |
| 91 | OBJECT_DECLARE_TYPE(VirtIODevice, VirtioDeviceClass, VIRTIO_DEVICE) |
| 92 | |
| 93 | typedef struct { |
| 94 | int virtio_bit; |
| 95 | const char *feature_desc; |
| 96 | } qmp_virtio_feature_map_t; |
| 97 | |
| 98 | enum virtio_device_endian { |
| 99 | VIRTIO_DEVICE_ENDIAN_UNKNOWN, |
| 100 | VIRTIO_DEVICE_ENDIAN_LITTLE, |
| 101 | VIRTIO_DEVICE_ENDIAN_BIG, |
| 102 | }; |
| 103 | |
| 104 | #define TYPE_VIRTIO_SHARED_MEMORY_MAPPING "virtio-shared-memory-mapping" |
| 105 | OBJECT_DECLARE_SIMPLE_TYPE(VirtioSharedMemoryMapping, VIRTIO_SHARED_MEMORY_MAPPING) |
| 106 | |
| 107 | /** |
| 108 | * VirtioSharedMemoryMapping: |
| 109 | * @parent: Parent QOM object |
| 110 | * @shmid: VIRTIO Shared Memory Region ID |
| 111 | * @offset: Offset within the VIRTIO Shared Memory Region |
| 112 | * @len: Size of the mapping |
| 113 | * @mr: MemoryRegion associated with this shared memory mapping |
| 114 | * @link: List entry for the shared memory region's mapping list |
| 115 | * |
| 116 | * A QOM object that represents an individual file descriptor-based shared |
| 117 | * memory mapping within a VIRTIO Shared Memory Region. It manages the |
| 118 | * MemoryRegion lifecycle and file descriptor cleanup through QOM reference |
| 119 | * counting. When the object is unreferenced and its reference count drops |
| 120 | * to zero, it automatically cleans up the MemoryRegion and closes the file |
| 121 | * descriptor. |
| 122 | */ |
| 123 | struct VirtioSharedMemoryMapping { |
| 124 | Object parent; |
| 125 | |
| 126 | uint8_t shmid; |
| 127 | hwaddr offset; |
| 128 | uint64_t len; |
| 129 | MemoryRegion *mr; |
| 130 | QTAILQ_ENTRY(VirtioSharedMemoryMapping) link; |
| 131 | }; |
| 132 | |
| 133 | struct VirtioSharedMemory { |
| 134 | uint8_t shmid; |
| 135 | MemoryRegion mr; |
| 136 | QTAILQ_HEAD(, VirtioSharedMemoryMapping) mmaps; |
| 137 | QSIMPLEQ_ENTRY(VirtioSharedMemory) entry; |
| 138 | }; |
| 139 | |
| 140 | typedef struct VirtioSharedMemory VirtioSharedMemory; |
| 141 | |
| 142 | /** |
| 143 | * struct VirtIODevice - common VirtIO structure |
| 144 | * @name: name of the device |
| 145 | * @status: VirtIO Device Status field |
| 146 | * |
| 147 | */ |
| 148 | struct VirtIODevice |
| 149 | { |
| 150 | DeviceState parent_obj; |
| 151 | const char *name; |
| 152 | uint8_t status; |
| 153 | uint8_t isr; |
| 154 | uint16_t queue_sel; |
| 155 | /** |
| 156 | * These fields represent a set of VirtIO features at various |
| 157 | * levels of the stack. @host_features indicates the complete |
| 158 | * feature set the VirtIO device can offer to the driver. |
| 159 | * @guest_features indicates which features the VirtIO driver has |
| 160 | * selected by writing to the feature register. Finally |
| 161 | * @backend_features represents everything supported by the |
| 162 | * backend (e.g. vhost) and could potentially be a subset of the |
| 163 | * total feature set offered by QEMU. |
| 164 | */ |
| 165 | VIRTIO_DECLARE_FEATURES(host_features); |
| 166 | VIRTIO_DECLARE_FEATURES(guest_features); |
| 167 | VIRTIO_DECLARE_FEATURES(backend_features); |
| 168 | |
| 169 | size_t config_len; |
| 170 | void *config; |
| 171 | uint16_t config_vector; |
| 172 | uint32_t generation; |
| 173 | int nvectors; |
| 174 | VirtQueue *vq; |
| 175 | MemoryListener listener; |
| 176 | uint16_t device_id; |
| 177 | /* @vm_running: current VM running state via virtio_vmstate_change() */ |
| 178 | bool vm_running; |
| 179 | bool broken; /* device in invalid state, needs reset */ |
| 180 | bool use_disabled_flag; /* allow use of 'disable' flag when needed */ |
| 181 | bool disabled; /* device in temporarily disabled state */ |
| 182 | /** |
| 183 | * @use_started: true if the @started flag should be used to check the |
| 184 | * current state of the VirtIO device. Otherwise status bits |
| 185 | * should be checked for a current status of the device. |
| 186 | * @use_started is only set via QMP and defaults to true for all |
| 187 | * modern machines (since 4.1). |
| 188 | */ |
| 189 | bool use_started; |
| 190 | bool started; |
| 191 | bool start_on_kick; /* when virtio 1.0 feature has not been negotiated */ |
| 192 | bool disable_legacy_check; |
| 193 | bool vhost_started; |
| 194 | VMChangeStateEntry *vmstate; |
| 195 | char *bus_name; |
| 196 | uint8_t device_endian; |
| 197 | /** |
| 198 | * @user_guest_notifier_mask: gate usage of ->guest_notifier_mask() callback. |
| 199 | * This is used to suppress the masking of guest updates for |
| 200 | * vhost-user devices which are asynchronous by design. |
| 201 | */ |
| 202 | bool use_guest_notifier_mask; |
| 203 | AddressSpace *dma_as; |
| 204 | QLIST_HEAD(, VirtQueue) *vector_queues; |
| 205 | QTAILQ_ENTRY(VirtIODevice) next; |
| 206 | /** |
| 207 | * @config_notifier: the event notifier that handles config events |
| 208 | */ |
| 209 | EventNotifier config_notifier; |
| 210 | bool device_iotlb_enabled; |
| 211 | /* Shared memory region for mappings. */ |
| 212 | QSIMPLEQ_HEAD(, VirtioSharedMemory) shmem_list; |
| 213 | }; |
| 214 | |
| 215 | struct VirtioDeviceClass { |
| 216 | /*< private >*/ |
| 217 | DeviceClass parent; |
| 218 | /*< public >*/ |
| 219 | |
| 220 | /* This is what a VirtioDevice must implement */ |
| 221 | DeviceRealize realize; |
| 222 | DeviceUnrealize unrealize; |
| 223 | void (*get_features_ex)(VirtIODevice *vdev, uint64_t *requested_features, |
| 224 | Error **errp); |
| 225 | void (*set_features_ex)(VirtIODevice *vdev, const uint64_t *val); |
| 226 | uint64_t (*get_features)(VirtIODevice *vdev, |
| 227 | uint64_t requested_features, |
| 228 | Error **errp); |
| 229 | uint64_t (*bad_features)(VirtIODevice *vdev); |
| 230 | void (*set_features)(VirtIODevice *vdev, uint64_t val); |
| 231 | int (*validate_features)(VirtIODevice *vdev); |
| 232 | void (*get_config)(VirtIODevice *vdev, uint8_t *config); |
| 233 | void (*set_config)(VirtIODevice *vdev, const uint8_t *config); |
| 234 | void (*reset)(VirtIODevice *vdev); |
| 235 | int (*set_status)(VirtIODevice *vdev, uint8_t val); |
| 236 | /* Device must validate queue_index. */ |
| 237 | void (*queue_reset)(VirtIODevice *vdev, uint32_t queue_index); |
| 238 | /* Device must validate queue_index. */ |
| 239 | void (*queue_enable)(VirtIODevice *vdev, uint32_t queue_index); |
| 240 | /* For transitional devices, this is a bitmap of features |
| 241 | * that are only exposed on the legacy interface but not |
| 242 | * the modern one. |
| 243 | */ |
| 244 | uint64_t legacy_features; |
| 245 | /* Test and clear event pending status. |
| 246 | * Should be called after unmask to avoid losing events. |
| 247 | * If backend does not support masking, |
| 248 | * must check in frontend instead. |
| 249 | */ |
| 250 | bool (*guest_notifier_pending)(VirtIODevice *vdev, int n); |
| 251 | /* Mask/unmask events from this vq. Any events reported |
| 252 | * while masked will become pending. |
| 253 | * If backend does not support masking, |
| 254 | * must mask in frontend instead. |
| 255 | */ |
| 256 | void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask); |
| 257 | int (*start_ioeventfd)(VirtIODevice *vdev); |
| 258 | void (*stop_ioeventfd)(VirtIODevice *vdev); |
| 259 | /* |
| 260 | * Called before loading queues. |
| 261 | * If the number of queues change at runtime, use @n to know the |
| 262 | * number and add or remove queues accordingly. |
| 263 | * Note that this function is called in the middle of loading vmsd; |
| 264 | * no assumption should be made on states being loaded from vmsd. |
| 265 | */ |
| 266 | int (*pre_load_queues)(VirtIODevice *vdev, uint32_t n); |
| 267 | /* Saving and loading of a device; trying to deprecate save/load |
| 268 | * use vmsd for new devices. |
| 269 | */ |
| 270 | void (*save)(VirtIODevice *vdev, QEMUFile *f); |
| 271 | int (*load)(VirtIODevice *vdev, QEMUFile *f, int version_id); |
| 272 | /* Post load hook in vmsd is called early while device is processed, and |
| 273 | * when VirtIODevice isn't fully initialized. Devices should use this instead, |
| 274 | * unless they specifically want to verify the migration stream as it's |
| 275 | * processed, e.g. for bounds checking. |
| 276 | */ |
| 277 | int (*post_load)(VirtIODevice *vdev); |
| 278 | const VMStateDescription *vmsd; |
| 279 | bool (*primary_unplug_pending)(void *opaque); |
| 280 | /* May be called even when vdev->vhost_started is false */ |
| 281 | struct vhost_dev *(*get_vhost)(VirtIODevice *vdev); |
| 282 | void (*toggle_device_iotlb)(VirtIODevice *vdev); |
| 283 | }; |
| 284 | |
| 285 | void virtio_instance_init_common(Object *proxy_obj, void *data, |
| 286 | size_t vdev_size, const char *vdev_name); |
| 287 | |
| 288 | /** |
| 289 | * virtio_init() - initialise the common VirtIODevice structure |
| 290 | * @vdev: pointer to VirtIODevice |
| 291 | * @device_id: the VirtIO device ID (see virtio_ids.h) |
| 292 | * @config_size: size of the config space |
| 293 | */ |
| 294 | void virtio_init(VirtIODevice *vdev, uint16_t device_id, size_t config_size); |
| 295 | |
| 296 | void virtio_cleanup(VirtIODevice *vdev); |
| 297 | |
| 298 | void virtio_error(VirtIODevice *vdev, const char *fmt, ...) G_GNUC_PRINTF(2, 3); |
| 299 | |
| 300 | /* Set the child bus name. */ |
| 301 | void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name); |
| 302 | |
| 303 | typedef void (*VirtIOHandleOutput)(VirtIODevice *, VirtQueue *); |
| 304 | |
| 305 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
| 306 | VirtIOHandleOutput handle_output); |
| 307 | |
| 308 | void virtio_del_queue(VirtIODevice *vdev, int n); |
| 309 | |
| 310 | void virtio_delete_queue(VirtQueue *vq); |
| 311 | |
| 312 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, |
| 313 | unsigned int len); |
| 314 | void virtqueue_flush(VirtQueue *vq, unsigned int count); |
| 315 | void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem, |
| 316 | unsigned int len); |
| 317 | void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem, |
| 318 | unsigned int len); |
| 319 | bool virtqueue_rewind(VirtQueue *vq, unsigned int num); |
| 320 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
| 321 | unsigned int len, unsigned int idx); |
| 322 | |
| 323 | void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem); |
| 324 | void *virtqueue_pop(VirtQueue *vq, size_t sz); |
| 325 | unsigned int virtqueue_drop_all(VirtQueue *vq); |
| 326 | void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz); |
| 327 | void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, |
| 328 | VirtQueueElement *elem); |
| 329 | int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, |
| 330 | unsigned int out_bytes); |
| 331 | /** |
| 332 | * Return <0 on error or an opaque >=0 to pass to |
| 333 | * virtio_queue_enable_notification_and_check on success. |
| 334 | */ |
| 335 | int virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, |
| 336 | unsigned int *out_bytes, unsigned max_in_bytes, |
| 337 | unsigned max_out_bytes); |
| 338 | |
| 339 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq); |
| 340 | |
| 341 | int virtio_save(VirtIODevice *vdev, QEMUFile *f); |
| 342 | |
| 343 | /** |
| 344 | * virtio_new_shmem_region() - Create a new shared memory region |
| 345 | * @vdev: VirtIODevice |
| 346 | * @shmid: Shared memory ID |
| 347 | * @size: Size of the shared memory region |
| 348 | * |
| 349 | * Creates a new VirtioSharedMemory region for the given device and ID. |
| 350 | * The returned VirtioSharedMemory is owned by the VirtIODevice and will |
| 351 | * be automatically freed when the device is destroyed. The caller |
| 352 | * should not free the returned pointer. |
| 353 | * |
| 354 | * Returns: Pointer to the new VirtioSharedMemory region, or NULL on failure |
| 355 | */ |
| 356 | VirtioSharedMemory *virtio_new_shmem_region(VirtIODevice *vdev, uint8_t shmid, uint64_t size); |
| 357 | |
| 358 | /** |
| 359 | * virtio_find_shmem_region() - Find an existing shared memory region |
| 360 | * @vdev: VirtIODevice |
| 361 | * @shmid: Shared memory ID to find |
| 362 | * |
| 363 | * Finds an existing VirtioSharedMemory region by ID. The returned pointer |
| 364 | * is owned by the VirtIODevice and should not be freed by the caller. |
| 365 | * |
| 366 | * Returns: Pointer to the VirtioSharedMemory region, or NULL if not found |
| 367 | */ |
| 368 | VirtioSharedMemory *virtio_find_shmem_region(VirtIODevice *vdev, uint8_t shmid); |
| 369 | |
| 370 | /** |
| 371 | * virtio_shared_memory_mapping_new() - Create a new VirtioSharedMemoryMapping |
| 372 | * @shmid: VIRTIO Shared Memory Region ID |
| 373 | * @fd: File descriptor for the shared memory |
| 374 | * @fd_offset: Offset within the file descriptor |
| 375 | * @shm_offset: Offset within the VIRTIO Shared Memory Region |
| 376 | * @len: Size of the mapping |
| 377 | * @allow_write: Whether to allow write access to the mapping |
| 378 | * |
| 379 | * Creates a new VirtioSharedMemoryMapping that manages a shared memory mapping. |
| 380 | * The object will create a MemoryRegion using memory_region_init_ram_from_fd() |
| 381 | * as a child object. When the object is finalized, it will automatically |
| 382 | * clean up the MemoryRegion and close the file descriptor. |
| 383 | * |
| 384 | * Return: A new VirtioSharedMemoryMapping on success, NULL on error. |
| 385 | */ |
| 386 | VirtioSharedMemoryMapping *virtio_shared_memory_mapping_new(uint8_t shmid, |
| 387 | int fd, |
| 388 | uint64_t fd_offset, |
| 389 | uint64_t shm_offset, |
| 390 | uint64_t len, |
| 391 | bool allow_write); |
| 392 | |
| 393 | /** |
| 394 | * virtio_add_shmem_map() - Add a memory mapping to a shared region |
| 395 | * @shmem: VirtioSharedMemory region |
| 396 | * @mapping: VirtioSharedMemoryMapping to add (transfers ownership) |
| 397 | * |
| 398 | * Adds a memory mapping to the shared memory region. The VirtioSharedMemoryMapping |
| 399 | * ownership is transferred to the shared memory region and will be automatically |
| 400 | * cleaned up through QOM reference counting when virtio_del_shmem_map() is |
| 401 | * called or when the shared memory region is destroyed. |
| 402 | * |
| 403 | * Returns: 0 on success, negative errno on failure |
| 404 | */ |
| 405 | int virtio_add_shmem_map(VirtioSharedMemory *shmem, |
| 406 | VirtioSharedMemoryMapping *mapping); |
| 407 | |
| 408 | /** |
| 409 | * virtio_find_shmem_map() - Find a memory mapping in a shared region |
| 410 | * @shmem: VirtioSharedMemory region |
| 411 | * @offset: Offset within the shared memory region |
| 412 | * @size: Size of the mapping to find |
| 413 | * |
| 414 | * Finds an existing memory mapping that covers the specified range. |
| 415 | * The returned VirtioSharedMemoryMapping is owned by the VirtioSharedMemory |
| 416 | * region and should not be freed by the caller. |
| 417 | * |
| 418 | * Returns: Pointer to the VirtioSharedMemoryMapping, or NULL if not found |
| 419 | */ |
| 420 | VirtioSharedMemoryMapping *virtio_find_shmem_map(VirtioSharedMemory *shmem, |
| 421 | hwaddr offset, uint64_t size); |
| 422 | |
| 423 | /** |
| 424 | * virtio_del_shmem_map() - Remove a memory mapping from a shared region |
| 425 | * @shmem: VirtioSharedMemory region |
| 426 | * @offset: Offset of the mapping to remove |
| 427 | * @size: Size of the mapping to remove |
| 428 | * |
| 429 | * Removes a memory mapping from the shared memory region. This will |
| 430 | * automatically unref the associated VhostUserShmemObject, which may |
| 431 | * trigger its finalization and cleanup if no other references exist. |
| 432 | * The mapping's MemoryRegion will be properly unmapped and cleaned up. |
| 433 | */ |
| 434 | void virtio_del_shmem_map(VirtioSharedMemory *shmem, hwaddr offset, |
| 435 | uint64_t size); |
| 436 | |
| 437 | extern const VMStateInfo virtio_vmstate_info; |
| 438 | |
| 439 | #define VMSTATE_VIRTIO_DEVICE \ |
| 440 | { \ |
| 441 | .name = "virtio", \ |
| 442 | .info = &virtio_vmstate_info, \ |
| 443 | .flags = VMS_SINGLE, \ |
| 444 | } |
| 445 | |
| 446 | int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id); |
| 447 | |
| 448 | /** |
| 449 | * virtio_notify_config() - signal a change to device config |
| 450 | * @vdev: the virtio device |
| 451 | * |
| 452 | * Assuming the virtio device is up (VIRTIO_CONFIG_S_DRIVER_OK) this |
| 453 | * will trigger a guest interrupt and update the config version. |
| 454 | */ |
| 455 | void virtio_notify_config(VirtIODevice *vdev); |
| 456 | |
| 457 | bool virtio_queue_get_notification(VirtQueue *vq); |
| 458 | void virtio_queue_set_notification(VirtQueue *vq, int enable); |
| 459 | |
| 460 | int virtio_queue_ready(VirtQueue *vq); |
| 461 | |
| 462 | int virtio_queue_empty(VirtQueue *vq); |
| 463 | |
| 464 | /** |
| 465 | * Enable notification and check whether guest has added some |
| 466 | * buffers since last call to virtqueue_get_avail_bytes. |
| 467 | * |
| 468 | * @opaque: value returned from virtqueue_get_avail_bytes |
| 469 | */ |
| 470 | bool virtio_queue_enable_notification_and_check(VirtQueue *vq, |
| 471 | int opaque); |
| 472 | |
| 473 | void virtio_queue_set_shadow_avail_idx(VirtQueue *vq, uint16_t idx); |
| 474 | |
| 475 | /* Host binding interface. */ |
| 476 | |
| 477 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr); |
| 478 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr); |
| 479 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr); |
| 480 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data); |
| 481 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data); |
| 482 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data); |
| 483 | uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr); |
| 484 | uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr); |
| 485 | uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr); |
| 486 | void virtio_config_modern_writeb(VirtIODevice *vdev, |
| 487 | uint32_t addr, uint32_t data); |
| 488 | void virtio_config_modern_writew(VirtIODevice *vdev, |
| 489 | uint32_t addr, uint32_t data); |
| 490 | void virtio_config_modern_writel(VirtIODevice *vdev, |
| 491 | uint32_t addr, uint32_t data); |
| 492 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr); |
| 493 | hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n); |
| 494 | void virtio_queue_set_num(VirtIODevice *vdev, int n, int num); |
| 495 | int virtio_queue_get_num(VirtIODevice *vdev, int n); |
| 496 | int virtio_queue_get_max_num(VirtIODevice *vdev, int n); |
| 497 | int virtio_get_num_queues(VirtIODevice *vdev); |
| 498 | void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc, |
| 499 | hwaddr avail, hwaddr used); |
| 500 | void virtio_queue_update_rings(VirtIODevice *vdev, int n); |
| 501 | void virtio_init_region_cache(VirtIODevice *vdev, int n); |
| 502 | void virtio_queue_set_align(VirtIODevice *vdev, int n, int align); |
| 503 | void virtio_queue_notify(VirtIODevice *vdev, int n); |
| 504 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n); |
| 505 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector); |
| 506 | int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n, |
| 507 | MemoryRegion *mr, bool assign); |
| 508 | int virtio_set_status(VirtIODevice *vdev, uint8_t val); |
| 509 | void virtio_reset(VirtIODevice *vdev); |
| 510 | void virtio_queue_reset(VirtIODevice *vdev, uint32_t queue_index); |
| 511 | void virtio_queue_enable(VirtIODevice *vdev, uint32_t queue_index); |
| 512 | void virtio_update_irq(VirtIODevice *vdev); |
| 513 | int virtio_set_features(VirtIODevice *vdev, uint64_t val); |
| 514 | int virtio_set_features_ex(VirtIODevice *vdev, const uint64_t *val); |
| 515 | |
| 516 | /* Base devices. */ |
| 517 | typedef struct VirtIOBlkConf VirtIOBlkConf; |
| 518 | struct virtio_net_conf; |
| 519 | typedef struct virtio_serial_conf virtio_serial_conf; |
| 520 | typedef struct virtio_input_conf virtio_input_conf; |
| 521 | typedef struct VirtIOSCSIConf VirtIOSCSIConf; |
| 522 | typedef struct VirtIORNGConf VirtIORNGConf; |
| 523 | |
| 524 | #define DEFINE_VIRTIO_COMMON_FEATURES(_state, _field) \ |
| 525 | DEFINE_PROP_BIT64("indirect_desc", _state, _field, \ |
| 526 | VIRTIO_RING_F_INDIRECT_DESC, true), \ |
| 527 | DEFINE_PROP_BIT64("event_idx", _state, _field, \ |
| 528 | VIRTIO_RING_F_EVENT_IDX, true), \ |
| 529 | DEFINE_PROP_BIT64("notify_on_empty", _state, _field, \ |
| 530 | VIRTIO_F_NOTIFY_ON_EMPTY, true), \ |
| 531 | DEFINE_PROP_BIT64("any_layout", _state, _field, \ |
| 532 | VIRTIO_F_ANY_LAYOUT, true), \ |
| 533 | DEFINE_PROP_BIT64("iommu_platform", _state, _field, \ |
| 534 | VIRTIO_F_IOMMU_PLATFORM, false), \ |
| 535 | DEFINE_PROP_BIT64("packed", _state, _field, \ |
| 536 | VIRTIO_F_RING_PACKED, false), \ |
| 537 | DEFINE_PROP_BIT64("queue_reset", _state, _field, \ |
| 538 | VIRTIO_F_RING_RESET, true), \ |
| 539 | DEFINE_PROP_BIT64("in_order", _state, _field, \ |
| 540 | VIRTIO_F_IN_ORDER, false) |
| 541 | |
| 542 | hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n); |
| 543 | bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n); |
| 544 | bool virtio_queue_enabled(VirtIODevice *vdev, int n); |
| 545 | hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n); |
| 546 | hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n); |
| 547 | hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n); |
| 548 | hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n); |
| 549 | hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n); |
| 550 | unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n); |
| 551 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, |
| 552 | unsigned int idx); |
| 553 | void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n); |
| 554 | void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n); |
| 555 | void virtio_queue_update_used_idx(VirtIODevice *vdev, int n); |
| 556 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n); |
| 557 | uint16_t virtio_get_queue_index(VirtQueue *vq); |
| 558 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq); |
| 559 | int virtio_device_start_ioeventfd(VirtIODevice *vdev); |
| 560 | int virtio_device_grab_ioeventfd(VirtIODevice *vdev); |
| 561 | void virtio_device_release_ioeventfd(VirtIODevice *vdev); |
| 562 | bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev); |
| 563 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq); |
| 564 | void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled); |
| 565 | void virtio_queue_host_notifier_read(EventNotifier *n); |
| 566 | void virtio_queue_aio_attach_host_notifier(VirtQueue *vq, AioContext *ctx); |
| 567 | void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ctx); |
| 568 | void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx); |
| 569 | VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector); |
| 570 | VirtQueue *virtio_vector_next_queue(VirtQueue *vq); |
| 571 | EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev); |
| 572 | |
| 573 | /** |
| 574 | * virtio_set_guest_notifier - set/unset queue or config guest notifier |
| 575 | * |
| 576 | * @vdev: the VirtIO device |
| 577 | * @n: queue number, or VIRTIO_CONFIG_IRQ_IDX to set config notifer |
| 578 | * @assign: true to set notifier, false to unset |
| 579 | * @with_irqfd: irqfd enabled |
| 580 | */ |
| 581 | int virtio_set_guest_notifier(VirtIODevice *vdev, int n, bool assign, |
| 582 | bool with_irqfd); |
| 583 | |
| 584 | static inline void virtio_add_feature(uint64_t *features, unsigned int fbit) |
| 585 | { |
| 586 | assert(fbit < 64); |
| 587 | *features |= (1ULL << fbit); |
| 588 | } |
| 589 | |
| 590 | static inline void virtio_clear_feature(uint64_t *features, unsigned int fbit) |
| 591 | { |
| 592 | assert(fbit < 64); |
| 593 | *features &= ~(1ULL << fbit); |
| 594 | } |
| 595 | |
| 596 | static inline bool virtio_has_feature(uint64_t features, unsigned int fbit) |
| 597 | { |
| 598 | assert(fbit < 64); |
| 599 | return !!(features & (1ULL << fbit)); |
| 600 | } |
| 601 | |
| 602 | static inline bool virtio_vdev_has_feature(const VirtIODevice *vdev, |
| 603 | unsigned int fbit) |
| 604 | { |
| 605 | return virtio_has_feature(vdev->guest_features, fbit); |
| 606 | } |
| 607 | |
| 608 | static inline bool virtio_host_has_feature(VirtIODevice *vdev, |
| 609 | unsigned int fbit) |
| 610 | { |
| 611 | return virtio_has_feature(vdev->host_features, fbit); |
| 612 | } |
| 613 | |
| 614 | static inline bool virtio_vdev_is_legacy(const VirtIODevice *vdev) |
| 615 | { |
| 616 | return !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1); |
| 617 | } |
| 618 | |
| 619 | static inline bool virtio_vdev_is_big_endian(const VirtIODevice *vdev) |
| 620 | { |
| 621 | if (virtio_vdev_is_legacy(vdev)) { |
| 622 | assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN); |
| 623 | return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG; |
| 624 | } |
| 625 | /* Devices conforming to VIRTIO 1.0 or later are always LE. */ |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * virtio_device_started() - check if device started |
| 631 | * @vdev - the VirtIO device |
| 632 | * @status - the devices status bits |
| 633 | * |
| 634 | * Check if the device is started. For most modern machines this is |
| 635 | * tracked via the @vdev->started field (to support migration), |
| 636 | * otherwise we check for the final negotiated status bit that |
| 637 | * indicates everything is ready. |
| 638 | */ |
| 639 | static inline bool virtio_device_started(VirtIODevice *vdev, uint8_t status) |
| 640 | { |
| 641 | if (vdev->use_started) { |
| 642 | return vdev->started; |
| 643 | } |
| 644 | |
| 645 | return status & VIRTIO_CONFIG_S_DRIVER_OK; |
| 646 | } |
| 647 | |
| 648 | /** |
| 649 | * virtio_device_should_start() - check if device startable |
| 650 | * @vdev - the VirtIO device |
| 651 | * @status - the devices status bits |
| 652 | * |
| 653 | * This is similar to virtio_device_started() but ignores vdev->started |
| 654 | * and also encapsulates a check on the VM status which would prevent a |
| 655 | * device from starting anyway. |
| 656 | */ |
| 657 | static inline bool virtio_device_should_start(VirtIODevice *vdev, uint8_t status) |
| 658 | { |
| 659 | if (!vdev->vm_running) { |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | return status & VIRTIO_CONFIG_S_DRIVER_OK; |
| 664 | } |
| 665 | |
| 666 | static inline void virtio_set_started(VirtIODevice *vdev, bool started) |
| 667 | { |
| 668 | if (started) { |
| 669 | vdev->start_on_kick = false; |
| 670 | } |
| 671 | |
| 672 | if (vdev->use_started) { |
| 673 | vdev->started = started; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static inline void virtio_set_disabled(VirtIODevice *vdev, bool disable) |
| 678 | { |
| 679 | if (vdev->use_disabled_flag) { |
| 680 | vdev->disabled = disable; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | static inline bool virtio_device_disabled(VirtIODevice *vdev) |
| 685 | { |
| 686 | return unlikely(vdev->disabled || vdev->broken); |
| 687 | } |
| 688 | |
| 689 | bool virtio_legacy_allowed(VirtIODevice *vdev); |
| 690 | bool virtio_legacy_check_disabled(VirtIODevice *vdev); |
| 691 | |
| 692 | QEMUBH *virtio_bh_new_guarded_full(DeviceState *dev, |
| 693 | QEMUBHFunc *cb, void *opaque, |
| 694 | const char *name); |
| 695 | #define virtio_bh_new_guarded(dev, cb, opaque) \ |
| 696 | virtio_bh_new_guarded_full((dev), (cb), (opaque), (stringify(cb))) |
| 697 | |
| 698 | /* |
| 699 | * The "_io" variant runs BH only on a main-loop thread, while generic BH |
| 700 | * may run on a vCPU thread. |
| 701 | */ |
| 702 | QEMUBH *virtio_bh_io_new_guarded_full(DeviceState *dev, |
| 703 | QEMUBHFunc *cb, void *opaque, |
| 704 | const char *name); |
| 705 | #define virtio_bh_io_new_guarded(dev, cb, opaque) \ |
| 706 | virtio_bh_io_new_guarded_full((dev), (cb), (opaque), (stringify(cb))) |
| 707 | |
| 708 | #endif |