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