Marc Marí | 311e666 | 2014-09-01 12:07:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * libqos virtio definitions |
| 3 | * |
| 4 | * Copyright (c) 2014 Marc Marí |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | */ |
| 9 | |
| 10 | #ifndef LIBQOS_VIRTIO_H |
| 11 | #define LIBQOS_VIRTIO_H |
| 12 | |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 13 | #include "libqos/malloc.h" |
| 14 | |
Marc Marí | 311e666 | 2014-09-01 12:07:54 +0200 | [diff] [blame] | 15 | #define QVIRTIO_VENDOR_ID 0x1AF4 |
| 16 | |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 17 | #define QVIRTIO_RESET 0x0 |
| 18 | #define QVIRTIO_ACKNOWLEDGE 0x1 |
| 19 | #define QVIRTIO_DRIVER 0x2 |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 20 | #define QVIRTIO_DRIVER_OK 0x4 |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 21 | |
Fam Zheng | bea2f09 | 2015-04-24 19:35:18 +0800 | [diff] [blame] | 22 | #define QVIRTIO_NET_DEVICE_ID 0x1 |
| 23 | #define QVIRTIO_BLK_DEVICE_ID 0x2 |
| 24 | #define QVIRTIO_CONSOLE_DEVICE_ID 0x3 |
| 25 | #define QVIRTIO_RNG_DEVICE_ID 0x4 |
| 26 | #define QVIRTIO_BALLOON_DEVICE_ID 0x5 |
| 27 | #define QVIRTIO_RPMSG_DEVICE_ID 0x7 |
| 28 | #define QVIRTIO_SCSI_DEVICE_ID 0x8 |
| 29 | #define QVIRTIO_9P_DEVICE_ID 0x9 |
Marc Marí | 311e666 | 2014-09-01 12:07:54 +0200 | [diff] [blame] | 30 | |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 31 | #define QVIRTIO_F_NOTIFY_ON_EMPTY 0x01000000 |
| 32 | #define QVIRTIO_F_ANY_LAYOUT 0x08000000 |
| 33 | #define QVIRTIO_F_RING_INDIRECT_DESC 0x10000000 |
| 34 | #define QVIRTIO_F_RING_EVENT_IDX 0x20000000 |
Marc Marí | 1053587 | 2014-09-01 12:08:00 +0200 | [diff] [blame] | 35 | #define QVIRTIO_F_BAD_FEATURE 0x40000000 |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 36 | |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 37 | #define QVRING_DESC_F_NEXT 0x1 |
| 38 | #define QVRING_DESC_F_WRITE 0x2 |
| 39 | #define QVRING_DESC_F_INDIRECT 0x4 |
| 40 | |
| 41 | #define QVIRTIO_F_NOTIFY_ON_EMPTY 0x01000000 |
| 42 | #define QVIRTIO_F_ANY_LAYOUT 0x08000000 |
| 43 | #define QVIRTIO_F_RING_INDIRECT_DESC 0x10000000 |
| 44 | #define QVIRTIO_F_RING_EVENT_IDX 0x20000000 |
| 45 | #define QVIRTIO_F_BAD_FEATURE 0x40000000 |
| 46 | |
| 47 | #define QVRING_AVAIL_F_NO_INTERRUPT 1 |
| 48 | |
| 49 | #define QVRING_USED_F_NO_NOTIFY 1 |
| 50 | |
Marc Marí | 311e666 | 2014-09-01 12:07:54 +0200 | [diff] [blame] | 51 | typedef struct QVirtioDevice { |
| 52 | /* Device type */ |
| 53 | uint16_t device_type; |
| 54 | } QVirtioDevice; |
| 55 | |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 56 | typedef struct QVRingDesc { |
| 57 | uint64_t addr; |
| 58 | uint32_t len; |
| 59 | uint16_t flags; |
| 60 | uint16_t next; |
| 61 | } QVRingDesc; |
| 62 | |
| 63 | typedef struct QVRingAvail { |
| 64 | uint16_t flags; |
| 65 | uint16_t idx; |
| 66 | uint16_t ring[0]; /* This is an array of uint16_t */ |
Marc Marí | 1053587 | 2014-09-01 12:08:00 +0200 | [diff] [blame] | 67 | uint16_t used_event; |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 68 | } QVRingAvail; |
| 69 | |
| 70 | typedef struct QVRingUsedElem { |
| 71 | uint32_t id; |
| 72 | uint32_t len; |
| 73 | } QVRingUsedElem; |
| 74 | |
| 75 | typedef struct QVRingUsed { |
| 76 | uint16_t flags; |
| 77 | uint16_t idx; |
| 78 | QVRingUsedElem ring[0]; /* This is an array of QVRingUsedElem structs */ |
Marc Marí | 1053587 | 2014-09-01 12:08:00 +0200 | [diff] [blame] | 79 | uint16_t avail_event; |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 80 | } QVRingUsed; |
| 81 | |
| 82 | typedef struct QVirtQueue { |
| 83 | uint64_t desc; /* This points to an array of QVRingDesc */ |
| 84 | uint64_t avail; /* This points to a QVRingAvail */ |
| 85 | uint64_t used; /* This points to a QVRingDesc */ |
| 86 | uint16_t index; |
| 87 | uint32_t size; |
| 88 | uint32_t free_head; |
| 89 | uint32_t num_free; |
| 90 | uint32_t align; |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 91 | bool indirect; |
Marc Marí | 1053587 | 2014-09-01 12:08:00 +0200 | [diff] [blame] | 92 | bool event; |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 93 | } QVirtQueue; |
| 94 | |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 95 | typedef struct QVRingIndirectDesc { |
| 96 | uint64_t desc; /* This points to an array fo QVRingDesc */ |
| 97 | uint16_t index; |
| 98 | uint16_t elem; |
| 99 | } QVRingIndirectDesc; |
| 100 | |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 101 | typedef struct QVirtioBus { |
Marc Marí | 728312b | 2015-02-24 22:21:51 +0100 | [diff] [blame] | 102 | uint8_t (*config_readb)(QVirtioDevice *d, uint64_t addr); |
| 103 | uint16_t (*config_readw)(QVirtioDevice *d, uint64_t addr); |
| 104 | uint32_t (*config_readl)(QVirtioDevice *d, uint64_t addr); |
| 105 | uint64_t (*config_readq)(QVirtioDevice *d, uint64_t addr); |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 106 | |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 107 | /* Get features of the device */ |
| 108 | uint32_t (*get_features)(QVirtioDevice *d); |
| 109 | |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 110 | /* Set features of the device */ |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 111 | void (*set_features)(QVirtioDevice *d, uint32_t features); |
| 112 | |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 113 | /* Get features of the guest */ |
| 114 | uint32_t (*get_guest_features)(QVirtioDevice *d); |
| 115 | |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 116 | /* Get status of the device */ |
| 117 | uint8_t (*get_status)(QVirtioDevice *d); |
| 118 | |
| 119 | /* Set status of the device */ |
| 120 | void (*set_status)(QVirtioDevice *d, uint8_t status); |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 121 | |
Marc Marí | 5836811 | 2014-09-01 12:07:59 +0200 | [diff] [blame] | 122 | /* Get the queue ISR status of the device */ |
| 123 | bool (*get_queue_isr_status)(QVirtioDevice *d, QVirtQueue *vq); |
| 124 | |
| 125 | /* Get the configuration ISR status of the device */ |
| 126 | bool (*get_config_isr_status)(QVirtioDevice *d); |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 127 | |
| 128 | /* Select a queue to work on */ |
| 129 | void (*queue_select)(QVirtioDevice *d, uint16_t index); |
| 130 | |
| 131 | /* Get the size of the selected queue */ |
| 132 | uint16_t (*get_queue_size)(QVirtioDevice *d); |
| 133 | |
| 134 | /* Set the address of the selected queue */ |
| 135 | void (*set_queue_address)(QVirtioDevice *d, uint32_t pfn); |
| 136 | |
| 137 | /* Setup the virtqueue specified by index */ |
| 138 | QVirtQueue *(*virtqueue_setup)(QVirtioDevice *d, QGuestAllocator *alloc, |
| 139 | uint16_t index); |
| 140 | |
| 141 | /* Notify changes in virtqueue */ |
| 142 | void (*virtqueue_kick)(QVirtioDevice *d, QVirtQueue *vq); |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 143 | } QVirtioBus; |
| 144 | |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 145 | static inline uint32_t qvring_size(uint32_t num, uint32_t align) |
| 146 | { |
| 147 | return ((sizeof(struct QVRingDesc) * num + sizeof(uint16_t) * (3 + num) |
| 148 | + align - 1) & ~(align - 1)) |
| 149 | + sizeof(uint16_t) * 3 + sizeof(struct QVRingUsedElem) * num; |
| 150 | } |
| 151 | |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 152 | uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d, |
Marc Marí | 728312b | 2015-02-24 22:21:51 +0100 | [diff] [blame] | 153 | uint64_t addr); |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 154 | uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d, |
Marc Marí | 728312b | 2015-02-24 22:21:51 +0100 | [diff] [blame] | 155 | uint64_t addr); |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 156 | uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d, |
Marc Marí | 728312b | 2015-02-24 22:21:51 +0100 | [diff] [blame] | 157 | uint64_t addr); |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 158 | uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d, |
Marc Marí | 728312b | 2015-02-24 22:21:51 +0100 | [diff] [blame] | 159 | uint64_t addr); |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 160 | uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d); |
| 161 | void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d, |
| 162 | uint32_t features); |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 163 | |
| 164 | void qvirtio_reset(const QVirtioBus *bus, QVirtioDevice *d); |
| 165 | void qvirtio_set_acknowledge(const QVirtioBus *bus, QVirtioDevice *d); |
| 166 | void qvirtio_set_driver(const QVirtioBus *bus, QVirtioDevice *d); |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 167 | void qvirtio_set_driver_ok(const QVirtioBus *bus, QVirtioDevice *d); |
| 168 | |
Stefan Hajnoczi | 7055626 | 2014-09-29 16:40:12 +0100 | [diff] [blame] | 169 | void qvirtio_wait_queue_isr(const QVirtioBus *bus, QVirtioDevice *d, |
| 170 | QVirtQueue *vq, gint64 timeout_us); |
Stefan Hajnoczi | e8c81b4 | 2014-09-29 16:40:11 +0100 | [diff] [blame] | 171 | uint8_t qvirtio_wait_status_byte_no_isr(const QVirtioBus *bus, |
| 172 | QVirtioDevice *d, |
| 173 | QVirtQueue *vq, |
| 174 | uint64_t addr, |
| 175 | gint64 timeout_us); |
Stefan Hajnoczi | 7055626 | 2014-09-29 16:40:12 +0100 | [diff] [blame] | 176 | void qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d, |
| 177 | gint64 timeout_us); |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 178 | QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d, |
| 179 | QGuestAllocator *alloc, uint16_t index); |
| 180 | |
| 181 | void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr); |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 182 | QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d, |
| 183 | QGuestAllocator *alloc, uint16_t elem); |
| 184 | void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data, |
| 185 | uint32_t len, bool write); |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 186 | uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write, |
| 187 | bool next); |
Marc Marí | f294b02 | 2014-09-01 12:07:57 +0200 | [diff] [blame] | 188 | uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect); |
Marc Marí | bf3c63d | 2014-09-01 12:07:56 +0200 | [diff] [blame] | 189 | void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq, |
| 190 | uint32_t free_head); |
Marc Marí | 46e0cf7 | 2014-09-01 12:07:55 +0200 | [diff] [blame] | 191 | |
Marc Marí | 1053587 | 2014-09-01 12:08:00 +0200 | [diff] [blame] | 192 | void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx); |
Marc Marí | 311e666 | 2014-09-01 12:07:54 +0200 | [diff] [blame] | 193 | #endif |