aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 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 | #include <inttypes.h> |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 15 | |
Stefan Hajnoczi | 64979a4 | 2010-05-24 13:19:21 +0100 | [diff] [blame] | 16 | #include "trace.h" |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 17 | #include "qemu-error.h" |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 18 | #include "virtio.h" |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 19 | |
aliguori | f46f15b | 2008-12-04 19:58:45 +0000 | [diff] [blame] | 20 | /* The alignment to use between consumer and producer parts of vring. |
| 21 | * x86 pagesize again. */ |
| 22 | #define VIRTIO_PCI_VRING_ALIGN 4096 |
| 23 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 24 | /* QEMU doesn't strictly need write barriers since everything runs in |
| 25 | * lock-step. We'll leave the calls to wmb() in though to make it obvious for |
| 26 | * KVM or if kqemu gets SMP support. |
Michael S. Tsirkin | 79758e9 | 2009-10-26 15:17:15 +0200 | [diff] [blame] | 27 | * In any case, we must prevent the compiler from reordering the code. |
| 28 | * TODO: we likely need some rmb()/mb() as well. |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 29 | */ |
Michael S. Tsirkin | 79758e9 | 2009-10-26 15:17:15 +0200 | [diff] [blame] | 30 | |
| 31 | #define wmb() __asm__ __volatile__("": : :"memory") |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 32 | |
| 33 | typedef struct VRingDesc |
| 34 | { |
| 35 | uint64_t addr; |
| 36 | uint32_t len; |
| 37 | uint16_t flags; |
| 38 | uint16_t next; |
| 39 | } VRingDesc; |
| 40 | |
| 41 | typedef struct VRingAvail |
| 42 | { |
| 43 | uint16_t flags; |
| 44 | uint16_t idx; |
| 45 | uint16_t ring[0]; |
| 46 | } VRingAvail; |
| 47 | |
| 48 | typedef struct VRingUsedElem |
| 49 | { |
| 50 | uint32_t id; |
| 51 | uint32_t len; |
| 52 | } VRingUsedElem; |
| 53 | |
| 54 | typedef struct VRingUsed |
| 55 | { |
| 56 | uint16_t flags; |
| 57 | uint16_t idx; |
| 58 | VRingUsedElem ring[0]; |
| 59 | } VRingUsed; |
| 60 | |
| 61 | typedef struct VRing |
| 62 | { |
| 63 | unsigned int num; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 64 | target_phys_addr_t desc; |
| 65 | target_phys_addr_t avail; |
| 66 | target_phys_addr_t used; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 67 | } VRing; |
| 68 | |
| 69 | struct VirtQueue |
| 70 | { |
| 71 | VRing vring; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 72 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 73 | uint16_t last_avail_idx; |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 74 | /* Last used index value we have signalled on */ |
| 75 | uint16_t signalled_used; |
| 76 | |
| 77 | /* Last used index value we have signalled on */ |
| 78 | bool signalled_used_valid; |
| 79 | |
| 80 | /* Notification enabled? */ |
| 81 | bool notification; |
| 82 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 83 | int inuse; |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 84 | |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 85 | uint16_t vector; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 86 | void (*handle_output)(VirtIODevice *vdev, VirtQueue *vq); |
Michael S. Tsirkin | 1cbdabe | 2010-03-17 13:08:02 +0200 | [diff] [blame] | 87 | VirtIODevice *vdev; |
| 88 | EventNotifier guest_notifier; |
| 89 | EventNotifier host_notifier; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 92 | /* virt queue functions */ |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 93 | static void virtqueue_init(VirtQueue *vq) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 94 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 95 | target_phys_addr_t pa = vq->pa; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 96 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 97 | vq->vring.desc = pa; |
| 98 | vq->vring.avail = pa + vq->vring.num * sizeof(VRingDesc); |
aliguori | f46f15b | 2008-12-04 19:58:45 +0000 | [diff] [blame] | 99 | vq->vring.used = vring_align(vq->vring.avail + |
| 100 | offsetof(VRingAvail, ring[vq->vring.num]), |
| 101 | VIRTIO_PCI_VRING_ALIGN); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 104 | static inline uint64_t vring_desc_addr(target_phys_addr_t desc_pa, int i) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 105 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 106 | target_phys_addr_t pa; |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 107 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, addr); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 108 | return ldq_phys(pa); |
| 109 | } |
| 110 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 111 | static inline uint32_t vring_desc_len(target_phys_addr_t desc_pa, int i) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 112 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 113 | target_phys_addr_t pa; |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 114 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, len); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 115 | return ldl_phys(pa); |
| 116 | } |
| 117 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 118 | static inline uint16_t vring_desc_flags(target_phys_addr_t desc_pa, int i) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 119 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 120 | target_phys_addr_t pa; |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 121 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, flags); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 122 | return lduw_phys(pa); |
| 123 | } |
| 124 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 125 | static inline uint16_t vring_desc_next(target_phys_addr_t desc_pa, int i) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 126 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 127 | target_phys_addr_t pa; |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 128 | pa = desc_pa + sizeof(VRingDesc) * i + offsetof(VRingDesc, next); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 129 | return lduw_phys(pa); |
| 130 | } |
| 131 | |
| 132 | static inline uint16_t vring_avail_flags(VirtQueue *vq) |
| 133 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 134 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 135 | pa = vq->vring.avail + offsetof(VRingAvail, flags); |
| 136 | return lduw_phys(pa); |
| 137 | } |
| 138 | |
| 139 | static inline uint16_t vring_avail_idx(VirtQueue *vq) |
| 140 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 141 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 142 | pa = vq->vring.avail + offsetof(VRingAvail, idx); |
| 143 | return lduw_phys(pa); |
| 144 | } |
| 145 | |
| 146 | static inline uint16_t vring_avail_ring(VirtQueue *vq, int i) |
| 147 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 148 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 149 | pa = vq->vring.avail + offsetof(VRingAvail, ring[i]); |
| 150 | return lduw_phys(pa); |
| 151 | } |
| 152 | |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 153 | static inline uint16_t vring_used_event(VirtQueue *vq) |
| 154 | { |
| 155 | return vring_avail_ring(vq, vq->vring.num); |
| 156 | } |
| 157 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 158 | static inline void vring_used_ring_id(VirtQueue *vq, int i, uint32_t val) |
| 159 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 160 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 161 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].id); |
| 162 | stl_phys(pa, val); |
| 163 | } |
| 164 | |
| 165 | static inline void vring_used_ring_len(VirtQueue *vq, int i, uint32_t val) |
| 166 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 167 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 168 | pa = vq->vring.used + offsetof(VRingUsed, ring[i].len); |
| 169 | stl_phys(pa, val); |
| 170 | } |
| 171 | |
| 172 | static uint16_t vring_used_idx(VirtQueue *vq) |
| 173 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 174 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 175 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
| 176 | return lduw_phys(pa); |
| 177 | } |
| 178 | |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 179 | static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 180 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 181 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 182 | pa = vq->vring.used + offsetof(VRingUsed, idx); |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 183 | stw_phys(pa, val); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask) |
| 187 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 188 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 189 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
| 190 | stw_phys(pa, lduw_phys(pa) | mask); |
| 191 | } |
| 192 | |
| 193 | static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask) |
| 194 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 195 | target_phys_addr_t pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 196 | pa = vq->vring.used + offsetof(VRingUsed, flags); |
| 197 | stw_phys(pa, lduw_phys(pa) & ~mask); |
| 198 | } |
| 199 | |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 200 | static inline void vring_avail_event(VirtQueue *vq, uint16_t val) |
| 201 | { |
| 202 | target_phys_addr_t pa; |
| 203 | if (!vq->notification) { |
| 204 | return; |
| 205 | } |
| 206 | pa = vq->vring.used + offsetof(VRingUsed, ring[vq->vring.num]); |
| 207 | stw_phys(pa, val); |
| 208 | } |
| 209 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 210 | void virtio_queue_set_notification(VirtQueue *vq, int enable) |
| 211 | { |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 212 | vq->notification = enable; |
| 213 | if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) { |
| 214 | vring_avail_event(vq, vring_avail_idx(vq)); |
| 215 | } else if (enable) { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 216 | vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY); |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 217 | } else { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 218 | vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY); |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 219 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int virtio_queue_ready(VirtQueue *vq) |
| 223 | { |
| 224 | return vq->vring.avail != 0; |
| 225 | } |
| 226 | |
| 227 | int virtio_queue_empty(VirtQueue *vq) |
| 228 | { |
| 229 | return vring_avail_idx(vq) == vq->last_avail_idx; |
| 230 | } |
| 231 | |
| 232 | void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, |
| 233 | unsigned int len, unsigned int idx) |
| 234 | { |
| 235 | unsigned int offset; |
| 236 | int i; |
| 237 | |
Stefan Hajnoczi | 64979a4 | 2010-05-24 13:19:21 +0100 | [diff] [blame] | 238 | trace_virtqueue_fill(vq, elem, len, idx); |
| 239 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 240 | offset = 0; |
| 241 | for (i = 0; i < elem->in_num; i++) { |
| 242 | size_t size = MIN(len - offset, elem->in_sg[i].iov_len); |
| 243 | |
aliguori | 26b258e | 2009-03-28 17:46:18 +0000 | [diff] [blame] | 244 | cpu_physical_memory_unmap(elem->in_sg[i].iov_base, |
| 245 | elem->in_sg[i].iov_len, |
| 246 | 1, size); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 247 | |
aliguori | 26b258e | 2009-03-28 17:46:18 +0000 | [diff] [blame] | 248 | offset += elem->in_sg[i].iov_len; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 249 | } |
| 250 | |
aliguori | 26b258e | 2009-03-28 17:46:18 +0000 | [diff] [blame] | 251 | for (i = 0; i < elem->out_num; i++) |
| 252 | cpu_physical_memory_unmap(elem->out_sg[i].iov_base, |
| 253 | elem->out_sg[i].iov_len, |
| 254 | 0, elem->out_sg[i].iov_len); |
| 255 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 256 | idx = (idx + vring_used_idx(vq)) % vq->vring.num; |
| 257 | |
| 258 | /* Get a pointer to the next entry in the used ring. */ |
| 259 | vring_used_ring_id(vq, idx, elem->index); |
| 260 | vring_used_ring_len(vq, idx, len); |
| 261 | } |
| 262 | |
| 263 | void virtqueue_flush(VirtQueue *vq, unsigned int count) |
| 264 | { |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 265 | uint16_t old, new; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 266 | /* Make sure buffer is written before we update index. */ |
| 267 | wmb(); |
Stefan Hajnoczi | 64979a4 | 2010-05-24 13:19:21 +0100 | [diff] [blame] | 268 | trace_virtqueue_flush(vq, count); |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 269 | old = vring_used_idx(vq); |
| 270 | new = old + count; |
| 271 | vring_used_idx_set(vq, new); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 272 | vq->inuse -= count; |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 273 | if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old))) |
| 274 | vq->signalled_used_valid = false; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, |
| 278 | unsigned int len) |
| 279 | { |
| 280 | virtqueue_fill(vq, elem, len, 0); |
| 281 | virtqueue_flush(vq, 1); |
| 282 | } |
| 283 | |
| 284 | static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx) |
| 285 | { |
| 286 | uint16_t num_heads = vring_avail_idx(vq) - idx; |
| 287 | |
| 288 | /* Check it isn't doing very strange things with descriptor numbers. */ |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 289 | if (num_heads > vq->vring.num) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 290 | error_report("Guest moved used index from %u to %u", |
| 291 | idx, vring_avail_idx(vq)); |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 292 | exit(1); |
| 293 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 294 | |
| 295 | return num_heads; |
| 296 | } |
| 297 | |
| 298 | static unsigned int virtqueue_get_head(VirtQueue *vq, unsigned int idx) |
| 299 | { |
| 300 | unsigned int head; |
| 301 | |
| 302 | /* Grab the next descriptor number they're advertising, and increment |
| 303 | * the index we've seen. */ |
| 304 | head = vring_avail_ring(vq, idx % vq->vring.num); |
| 305 | |
| 306 | /* If their number is silly, that's a fatal mistake. */ |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 307 | if (head >= vq->vring.num) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 308 | error_report("Guest says index %u is available", head); |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 309 | exit(1); |
| 310 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 311 | |
| 312 | return head; |
| 313 | } |
| 314 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 315 | static unsigned virtqueue_next_desc(target_phys_addr_t desc_pa, |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 316 | unsigned int i, unsigned int max) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 317 | { |
| 318 | unsigned int next; |
| 319 | |
| 320 | /* If this descriptor says it doesn't chain, we're done. */ |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 321 | if (!(vring_desc_flags(desc_pa, i) & VRING_DESC_F_NEXT)) |
| 322 | return max; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 323 | |
| 324 | /* Check they're not leading us off end of descriptors. */ |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 325 | next = vring_desc_next(desc_pa, i); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 326 | /* Make sure compiler knows to grab that: we don't want it changing! */ |
| 327 | wmb(); |
| 328 | |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 329 | if (next >= max) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 330 | error_report("Desc next is %u", next); |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 331 | exit(1); |
| 332 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 333 | |
| 334 | return next; |
| 335 | } |
| 336 | |
| 337 | int virtqueue_avail_bytes(VirtQueue *vq, int in_bytes, int out_bytes) |
| 338 | { |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 339 | unsigned int idx; |
| 340 | int total_bufs, in_total, out_total; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 341 | |
| 342 | idx = vq->last_avail_idx; |
| 343 | |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 344 | total_bufs = in_total = out_total = 0; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 345 | while (virtqueue_num_heads(vq, idx)) { |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 346 | unsigned int max, num_bufs, indirect = 0; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 347 | target_phys_addr_t desc_pa; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 348 | int i; |
| 349 | |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 350 | max = vq->vring.num; |
| 351 | num_bufs = total_bufs; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 352 | i = virtqueue_get_head(vq, idx++); |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 353 | desc_pa = vq->vring.desc; |
| 354 | |
| 355 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) { |
| 356 | if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 357 | error_report("Invalid size for indirect buffer table"); |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 358 | exit(1); |
| 359 | } |
| 360 | |
| 361 | /* If we've got too many, that implies a descriptor loop. */ |
| 362 | if (num_bufs >= max) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 363 | error_report("Looped descriptor"); |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 364 | exit(1); |
| 365 | } |
| 366 | |
| 367 | /* loop over the indirect descriptor table */ |
| 368 | indirect = 1; |
| 369 | max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc); |
| 370 | num_bufs = i = 0; |
| 371 | desc_pa = vring_desc_addr(desc_pa, i); |
| 372 | } |
| 373 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 374 | do { |
| 375 | /* If we've got too many, that implies a descriptor loop. */ |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 376 | if (++num_bufs > max) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 377 | error_report("Looped descriptor"); |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 378 | exit(1); |
| 379 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 380 | |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 381 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 382 | if (in_bytes > 0 && |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 383 | (in_total += vring_desc_len(desc_pa, i)) >= in_bytes) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 384 | return 1; |
| 385 | } else { |
| 386 | if (out_bytes > 0 && |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 387 | (out_total += vring_desc_len(desc_pa, i)) >= out_bytes) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 388 | return 1; |
| 389 | } |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 390 | } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max); |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 391 | |
| 392 | if (!indirect) |
| 393 | total_bufs = num_bufs; |
| 394 | else |
| 395 | total_bufs++; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Kevin Wolf | 42fb2e0 | 2010-08-03 16:54:38 +0200 | [diff] [blame] | 401 | void virtqueue_map_sg(struct iovec *sg, target_phys_addr_t *addr, |
| 402 | size_t num_sg, int is_write) |
| 403 | { |
| 404 | unsigned int i; |
| 405 | target_phys_addr_t len; |
| 406 | |
| 407 | for (i = 0; i < num_sg; i++) { |
| 408 | len = sg[i].iov_len; |
| 409 | sg[i].iov_base = cpu_physical_memory_map(addr[i], &len, is_write); |
| 410 | if (sg[i].iov_base == NULL || len != sg[i].iov_len) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 411 | error_report("virtio: trying to map MMIO memory"); |
Kevin Wolf | 42fb2e0 | 2010-08-03 16:54:38 +0200 | [diff] [blame] | 412 | exit(1); |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 417 | int virtqueue_pop(VirtQueue *vq, VirtQueueElement *elem) |
| 418 | { |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 419 | unsigned int i, head, max; |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 420 | target_phys_addr_t desc_pa = vq->vring.desc; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 421 | |
| 422 | if (!virtqueue_num_heads(vq, vq->last_avail_idx)) |
| 423 | return 0; |
| 424 | |
| 425 | /* When we start there are none of either input nor output. */ |
| 426 | elem->out_num = elem->in_num = 0; |
| 427 | |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 428 | max = vq->vring.num; |
| 429 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 430 | i = head = virtqueue_get_head(vq, vq->last_avail_idx++); |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 431 | if (vq->vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX)) { |
| 432 | vring_avail_event(vq, vring_avail_idx(vq)); |
| 433 | } |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 434 | |
| 435 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_INDIRECT) { |
| 436 | if (vring_desc_len(desc_pa, i) % sizeof(VRingDesc)) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 437 | error_report("Invalid size for indirect buffer table"); |
Mark McLoughlin | efeea6d | 2009-06-17 11:38:28 +0100 | [diff] [blame] | 438 | exit(1); |
| 439 | } |
| 440 | |
| 441 | /* loop over the indirect descriptor table */ |
| 442 | max = vring_desc_len(desc_pa, i) / sizeof(VRingDesc); |
| 443 | desc_pa = vring_desc_addr(desc_pa, i); |
| 444 | i = 0; |
| 445 | } |
| 446 | |
Kevin Wolf | 42fb2e0 | 2010-08-03 16:54:38 +0200 | [diff] [blame] | 447 | /* Collect all the descriptors */ |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 448 | do { |
| 449 | struct iovec *sg; |
| 450 | |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 451 | if (vring_desc_flags(desc_pa, i) & VRING_DESC_F_WRITE) { |
Michael S. Tsirkin | c8eac1c | 2011-06-20 13:42:27 +0300 | [diff] [blame] | 452 | if (elem->in_num >= ARRAY_SIZE(elem->in_sg)) { |
| 453 | error_report("Too many write descriptors in indirect table"); |
| 454 | exit(1); |
| 455 | } |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 456 | elem->in_addr[elem->in_num] = vring_desc_addr(desc_pa, i); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 457 | sg = &elem->in_sg[elem->in_num++]; |
Kevin Wolf | 42fb2e0 | 2010-08-03 16:54:38 +0200 | [diff] [blame] | 458 | } else { |
Michael S. Tsirkin | c8eac1c | 2011-06-20 13:42:27 +0300 | [diff] [blame] | 459 | if (elem->out_num >= ARRAY_SIZE(elem->out_sg)) { |
| 460 | error_report("Too many read descriptors in indirect table"); |
| 461 | exit(1); |
| 462 | } |
Kevin Wolf | 42fb2e0 | 2010-08-03 16:54:38 +0200 | [diff] [blame] | 463 | elem->out_addr[elem->out_num] = vring_desc_addr(desc_pa, i); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 464 | sg = &elem->out_sg[elem->out_num++]; |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 465 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 466 | |
Kevin Wolf | 42fb2e0 | 2010-08-03 16:54:38 +0200 | [diff] [blame] | 467 | sg->iov_len = vring_desc_len(desc_pa, i); |
| 468 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 469 | /* If we've got too many, that implies a descriptor loop. */ |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 470 | if ((elem->in_num + elem->out_num) > max) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 471 | error_report("Looped descriptor"); |
aliguori | bb6834c | 2008-12-04 21:28:28 +0000 | [diff] [blame] | 472 | exit(1); |
| 473 | } |
Mark McLoughlin | 5774cf9 | 2009-06-17 11:37:32 +0100 | [diff] [blame] | 474 | } while ((i = virtqueue_next_desc(desc_pa, i, max)) != max); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 475 | |
Kevin Wolf | 42fb2e0 | 2010-08-03 16:54:38 +0200 | [diff] [blame] | 476 | /* Now map what we have collected */ |
| 477 | virtqueue_map_sg(elem->in_sg, elem->in_addr, elem->in_num, 1); |
| 478 | virtqueue_map_sg(elem->out_sg, elem->out_addr, elem->out_num, 0); |
| 479 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 480 | elem->index = head; |
| 481 | |
| 482 | vq->inuse++; |
| 483 | |
Stefan Hajnoczi | 64979a4 | 2010-05-24 13:19:21 +0100 | [diff] [blame] | 484 | trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 485 | return elem->in_num + elem->out_num; |
| 486 | } |
| 487 | |
| 488 | /* virtio device */ |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 489 | static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector) |
| 490 | { |
| 491 | if (vdev->binding->notify) { |
| 492 | vdev->binding->notify(vdev->binding_opaque, vector); |
| 493 | } |
| 494 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 495 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 496 | void virtio_update_irq(VirtIODevice *vdev) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 497 | { |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 498 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 501 | void virtio_reset(void *opaque) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 502 | { |
| 503 | VirtIODevice *vdev = opaque; |
| 504 | int i; |
| 505 | |
Michael S. Tsirkin | e0c472d | 2010-09-27 18:32:52 +0200 | [diff] [blame] | 506 | virtio_set_status(vdev, 0); |
| 507 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 508 | if (vdev->reset) |
| 509 | vdev->reset(vdev); |
| 510 | |
Michael S. Tsirkin | 704a76f | 2010-01-10 13:52:47 +0200 | [diff] [blame] | 511 | vdev->guest_features = 0; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 512 | vdev->queue_sel = 0; |
| 513 | vdev->status = 0; |
| 514 | vdev->isr = 0; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 515 | vdev->config_vector = VIRTIO_NO_VECTOR; |
| 516 | virtio_notify_vector(vdev, vdev->config_vector); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 517 | |
| 518 | for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { |
| 519 | vdev->vq[i].vring.desc = 0; |
| 520 | vdev->vq[i].vring.avail = 0; |
| 521 | vdev->vq[i].vring.used = 0; |
| 522 | vdev->vq[i].last_avail_idx = 0; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 523 | vdev->vq[i].pa = 0; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 524 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 525 | vdev->vq[i].signalled_used = 0; |
| 526 | vdev->vq[i].signalled_used_valid = false; |
| 527 | vdev->vq[i].notification = true; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 531 | uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 532 | { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 533 | uint8_t val; |
| 534 | |
| 535 | vdev->get_config(vdev, vdev->config); |
| 536 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 537 | if (addr > (vdev->config_len - sizeof(val))) |
| 538 | return (uint32_t)-1; |
| 539 | |
| 540 | memcpy(&val, vdev->config + addr, sizeof(val)); |
| 541 | return val; |
| 542 | } |
| 543 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 544 | uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 545 | { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 546 | uint16_t val; |
| 547 | |
| 548 | vdev->get_config(vdev, vdev->config); |
| 549 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 550 | if (addr > (vdev->config_len - sizeof(val))) |
| 551 | return (uint32_t)-1; |
| 552 | |
| 553 | memcpy(&val, vdev->config + addr, sizeof(val)); |
| 554 | return val; |
| 555 | } |
| 556 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 557 | uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 558 | { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 559 | uint32_t val; |
| 560 | |
| 561 | vdev->get_config(vdev, vdev->config); |
| 562 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 563 | if (addr > (vdev->config_len - sizeof(val))) |
| 564 | return (uint32_t)-1; |
| 565 | |
| 566 | memcpy(&val, vdev->config + addr, sizeof(val)); |
| 567 | return val; |
| 568 | } |
| 569 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 570 | void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 571 | { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 572 | uint8_t val = data; |
| 573 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 574 | if (addr > (vdev->config_len - sizeof(val))) |
| 575 | return; |
| 576 | |
| 577 | memcpy(vdev->config + addr, &val, sizeof(val)); |
| 578 | |
| 579 | if (vdev->set_config) |
| 580 | vdev->set_config(vdev, vdev->config); |
| 581 | } |
| 582 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 583 | void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 584 | { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 585 | uint16_t val = data; |
| 586 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 587 | if (addr > (vdev->config_len - sizeof(val))) |
| 588 | return; |
| 589 | |
| 590 | memcpy(vdev->config + addr, &val, sizeof(val)); |
| 591 | |
| 592 | if (vdev->set_config) |
| 593 | vdev->set_config(vdev, vdev->config); |
| 594 | } |
| 595 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 596 | void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 597 | { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 598 | uint32_t val = data; |
| 599 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 600 | if (addr > (vdev->config_len - sizeof(val))) |
| 601 | return; |
| 602 | |
| 603 | memcpy(vdev->config + addr, &val, sizeof(val)); |
| 604 | |
| 605 | if (vdev->set_config) |
| 606 | vdev->set_config(vdev, vdev->config); |
| 607 | } |
| 608 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 609 | void virtio_queue_set_addr(VirtIODevice *vdev, int n, target_phys_addr_t addr) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 610 | { |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 611 | vdev->vq[n].pa = addr; |
| 612 | virtqueue_init(&vdev->vq[n]); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 613 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 614 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 615 | target_phys_addr_t virtio_queue_get_addr(VirtIODevice *vdev, int n) |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 616 | { |
| 617 | return vdev->vq[n].pa; |
| 618 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 619 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 620 | int virtio_queue_get_num(VirtIODevice *vdev, int n) |
| 621 | { |
| 622 | return vdev->vq[n].vring.num; |
| 623 | } |
| 624 | |
Stefan Hajnoczi | 25db9eb | 2010-12-17 12:01:50 +0000 | [diff] [blame] | 625 | void virtio_queue_notify_vq(VirtQueue *vq) |
| 626 | { |
| 627 | if (vq->vring.desc) { |
| 628 | VirtIODevice *vdev = vq->vdev; |
| 629 | trace_virtio_queue_notify(vdev, vq - vdev->vq, vq); |
| 630 | vq->handle_output(vdev, vq); |
| 631 | } |
| 632 | } |
| 633 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 634 | void virtio_queue_notify(VirtIODevice *vdev, int n) |
| 635 | { |
Stefan Hajnoczi | 7157e2e | 2011-05-08 22:29:07 +0100 | [diff] [blame] | 636 | virtio_queue_notify_vq(&vdev->vq[n]); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 639 | uint16_t virtio_queue_vector(VirtIODevice *vdev, int n) |
| 640 | { |
| 641 | return n < VIRTIO_PCI_QUEUE_MAX ? vdev->vq[n].vector : |
| 642 | VIRTIO_NO_VECTOR; |
| 643 | } |
| 644 | |
| 645 | void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector) |
| 646 | { |
| 647 | if (n < VIRTIO_PCI_QUEUE_MAX) |
| 648 | vdev->vq[n].vector = vector; |
| 649 | } |
| 650 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 651 | VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size, |
| 652 | void (*handle_output)(VirtIODevice *, VirtQueue *)) |
| 653 | { |
| 654 | int i; |
| 655 | |
| 656 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { |
| 657 | if (vdev->vq[i].vring.num == 0) |
| 658 | break; |
| 659 | } |
| 660 | |
| 661 | if (i == VIRTIO_PCI_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE) |
| 662 | abort(); |
| 663 | |
| 664 | vdev->vq[i].vring.num = queue_size; |
| 665 | vdev->vq[i].handle_output = handle_output; |
| 666 | |
| 667 | return &vdev->vq[i]; |
| 668 | } |
| 669 | |
Michael S. Tsirkin | 1cbdabe | 2010-03-17 13:08:02 +0200 | [diff] [blame] | 670 | void virtio_irq(VirtQueue *vq) |
| 671 | { |
Stefan Hajnoczi | 64979a4 | 2010-05-24 13:19:21 +0100 | [diff] [blame] | 672 | trace_virtio_irq(vq); |
Michael S. Tsirkin | 1cbdabe | 2010-03-17 13:08:02 +0200 | [diff] [blame] | 673 | vq->vdev->isr |= 0x01; |
| 674 | virtio_notify_vector(vq->vdev, vq->vector); |
| 675 | } |
| 676 | |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 677 | /* Assuming a given event_idx value from the other size, if |
| 678 | * we have just incremented index from old to new_idx, |
| 679 | * should we trigger an event? */ |
| 680 | static inline int vring_need_event(uint16_t event, uint16_t new, uint16_t old) |
| 681 | { |
| 682 | /* Note: Xen has similar logic for notification hold-off |
| 683 | * in include/xen/interface/io/ring.h with req_event and req_prod |
| 684 | * corresponding to event_idx + 1 and new respectively. |
| 685 | * Note also that req_event and req_prod in Xen start at 1, |
| 686 | * event indexes in virtio start at 0. */ |
| 687 | return (uint16_t)(new - event - 1) < (uint16_t)(new - old); |
| 688 | } |
| 689 | |
| 690 | static bool vring_notify(VirtIODevice *vdev, VirtQueue *vq) |
| 691 | { |
| 692 | uint16_t old, new; |
| 693 | bool v; |
| 694 | /* Always notify when queue is empty (when feature acknowledge) */ |
| 695 | if (((vdev->guest_features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) && |
| 696 | !vq->inuse && vring_avail_idx(vq) == vq->last_avail_idx)) { |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | if (!(vdev->guest_features & (1 << VIRTIO_RING_F_EVENT_IDX))) { |
| 701 | return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT); |
| 702 | } |
| 703 | |
| 704 | v = vq->signalled_used_valid; |
| 705 | vq->signalled_used_valid = true; |
| 706 | old = vq->signalled_used; |
| 707 | new = vq->signalled_used = vring_used_idx(vq); |
| 708 | return !v || vring_need_event(vring_used_event(vq), new, old); |
| 709 | } |
| 710 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 711 | void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) |
| 712 | { |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 713 | if (!vring_notify(vdev, vq)) { |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 714 | return; |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 715 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 716 | |
Stefan Hajnoczi | 64979a4 | 2010-05-24 13:19:21 +0100 | [diff] [blame] | 717 | trace_virtio_notify(vdev, vq); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 718 | vdev->isr |= 0x01; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 719 | virtio_notify_vector(vdev, vq->vector); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | void virtio_notify_config(VirtIODevice *vdev) |
| 723 | { |
aliguori | 7625162 | 2009-01-29 17:02:13 +0000 | [diff] [blame] | 724 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) |
| 725 | return; |
| 726 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 727 | vdev->isr |= 0x03; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 728 | virtio_notify_vector(vdev, vdev->config_vector); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | void virtio_save(VirtIODevice *vdev, QEMUFile *f) |
| 732 | { |
| 733 | int i; |
| 734 | |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 735 | if (vdev->binding->save_config) |
| 736 | vdev->binding->save_config(vdev->binding_opaque, f); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 737 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 738 | qemu_put_8s(f, &vdev->status); |
| 739 | qemu_put_8s(f, &vdev->isr); |
| 740 | qemu_put_be16s(f, &vdev->queue_sel); |
Michael S. Tsirkin | 704a76f | 2010-01-10 13:52:47 +0200 | [diff] [blame] | 741 | qemu_put_be32s(f, &vdev->guest_features); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 742 | qemu_put_be32(f, vdev->config_len); |
| 743 | qemu_put_buffer(f, vdev->config, vdev->config_len); |
| 744 | |
| 745 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { |
| 746 | if (vdev->vq[i].vring.num == 0) |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | qemu_put_be32(f, i); |
| 751 | |
| 752 | for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { |
| 753 | if (vdev->vq[i].vring.num == 0) |
| 754 | break; |
| 755 | |
| 756 | qemu_put_be32(f, vdev->vq[i].vring.num); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 757 | qemu_put_be64(f, vdev->vq[i].pa); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 758 | qemu_put_be16s(f, &vdev->vq[i].last_avail_idx); |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 759 | if (vdev->binding->save_queue) |
| 760 | vdev->binding->save_queue(vdev->binding_opaque, i, f); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 764 | int virtio_load(VirtIODevice *vdev, QEMUFile *f) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 765 | { |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 766 | int num, i, ret; |
Michael S. Tsirkin | 6d74ca5 | 2009-12-08 20:07:48 +0200 | [diff] [blame] | 767 | uint32_t features; |
Michael S. Tsirkin | 8172539 | 2010-01-10 13:52:53 +0200 | [diff] [blame] | 768 | uint32_t supported_features = |
Michael S. Tsirkin | 6d74ca5 | 2009-12-08 20:07:48 +0200 | [diff] [blame] | 769 | vdev->binding->get_features(vdev->binding_opaque); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 770 | |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 771 | if (vdev->binding->load_config) { |
| 772 | ret = vdev->binding->load_config(vdev->binding_opaque, f); |
| 773 | if (ret) |
| 774 | return ret; |
| 775 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 776 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 777 | qemu_get_8s(f, &vdev->status); |
| 778 | qemu_get_8s(f, &vdev->isr); |
| 779 | qemu_get_be16s(f, &vdev->queue_sel); |
Michael S. Tsirkin | 6d74ca5 | 2009-12-08 20:07:48 +0200 | [diff] [blame] | 780 | qemu_get_be32s(f, &features); |
| 781 | if (features & ~supported_features) { |
Stefan Hajnoczi | ce67ed6 | 2010-11-15 20:44:36 +0000 | [diff] [blame] | 782 | error_report("Features 0x%x unsupported. Allowed features: 0x%x", |
| 783 | features, supported_features); |
Michael S. Tsirkin | 6d74ca5 | 2009-12-08 20:07:48 +0200 | [diff] [blame] | 784 | return -1; |
| 785 | } |
Michael S. Tsirkin | fae054b | 2010-05-09 19:42:09 +0300 | [diff] [blame] | 786 | if (vdev->set_features) |
| 787 | vdev->set_features(vdev, features); |
Michael S. Tsirkin | 704a76f | 2010-01-10 13:52:47 +0200 | [diff] [blame] | 788 | vdev->guest_features = features; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 789 | vdev->config_len = qemu_get_be32(f); |
| 790 | qemu_get_buffer(f, vdev->config, vdev->config_len); |
| 791 | |
| 792 | num = qemu_get_be32(f); |
| 793 | |
| 794 | for (i = 0; i < num; i++) { |
| 795 | vdev->vq[i].vring.num = qemu_get_be32(f); |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 796 | vdev->vq[i].pa = qemu_get_be64(f); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 797 | qemu_get_be16s(f, &vdev->vq[i].last_avail_idx); |
Michael S. Tsirkin | bcbabae | 2011-06-12 16:21:57 +0300 | [diff] [blame] | 798 | vdev->vq[i].signalled_used_valid = false; |
| 799 | vdev->vq[i].notification = true; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 800 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 801 | if (vdev->vq[i].pa) { |
Michael S. Tsirkin | 1abeb5a | 2010-11-23 21:55:39 +0200 | [diff] [blame] | 802 | uint16_t nheads; |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 803 | virtqueue_init(&vdev->vq[i]); |
Michael S. Tsirkin | 1abeb5a | 2010-11-23 21:55:39 +0200 | [diff] [blame] | 804 | nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx; |
| 805 | /* Check it isn't doing very strange things with descriptor numbers. */ |
| 806 | if (nheads > vdev->vq[i].vring.num) { |
| 807 | error_report("VQ %d size 0x%x Guest index 0x%x " |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 808 | "inconsistent with Host index 0x%x: delta 0x%x", |
Michael S. Tsirkin | 1abeb5a | 2010-11-23 21:55:39 +0200 | [diff] [blame] | 809 | i, vdev->vq[i].vring.num, |
| 810 | vring_avail_idx(&vdev->vq[i]), |
| 811 | vdev->vq[i].last_avail_idx, nheads); |
| 812 | return -1; |
| 813 | } |
| 814 | } else if (vdev->vq[i].last_avail_idx) { |
| 815 | error_report("VQ %d address 0x0 " |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 816 | "inconsistent with Host index 0x%x", |
Michael S. Tsirkin | 1abeb5a | 2010-11-23 21:55:39 +0200 | [diff] [blame] | 817 | i, vdev->vq[i].last_avail_idx); |
| 818 | return -1; |
Michael S. Tsirkin | 258dc7c | 2010-10-17 20:23:48 +0200 | [diff] [blame] | 819 | } |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 820 | if (vdev->binding->load_queue) { |
| 821 | ret = vdev->binding->load_queue(vdev->binding_opaque, i, f); |
| 822 | if (ret) |
| 823 | return ret; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 824 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 827 | virtio_notify_vector(vdev, VIRTIO_NO_VECTOR); |
Michael S. Tsirkin | ff24bd5 | 2009-06-21 19:50:40 +0300 | [diff] [blame] | 828 | return 0; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 829 | } |
| 830 | |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 831 | void virtio_cleanup(VirtIODevice *vdev) |
| 832 | { |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 833 | qemu_del_vm_change_state_handler(vdev->vmstate); |
aliguori | b946a15 | 2009-04-17 17:11:08 +0000 | [diff] [blame] | 834 | if (vdev->config) |
| 835 | qemu_free(vdev->config); |
| 836 | qemu_free(vdev->vq); |
| 837 | } |
| 838 | |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 839 | static void virtio_vmstate_change(void *opaque, int running, int reason) |
| 840 | { |
| 841 | VirtIODevice *vdev = opaque; |
| 842 | bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK); |
| 843 | vdev->vm_running = running; |
| 844 | |
| 845 | if (backend_run) { |
| 846 | virtio_set_status(vdev, vdev->status); |
| 847 | } |
| 848 | |
| 849 | if (vdev->binding->vmstate_change) { |
| 850 | vdev->binding->vmstate_change(vdev->binding_opaque, backend_run); |
| 851 | } |
| 852 | |
| 853 | if (!backend_run) { |
| 854 | virtio_set_status(vdev, vdev->status); |
| 855 | } |
| 856 | } |
| 857 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 858 | VirtIODevice *virtio_common_init(const char *name, uint16_t device_id, |
| 859 | size_t config_size, size_t struct_size) |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 860 | { |
| 861 | VirtIODevice *vdev; |
Michael S. Tsirkin | b8193ad | 2009-09-07 21:20:15 +0300 | [diff] [blame] | 862 | int i; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 863 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 864 | vdev = qemu_mallocz(struct_size); |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 865 | |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 866 | vdev->device_id = device_id; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 867 | vdev->status = 0; |
| 868 | vdev->isr = 0; |
| 869 | vdev->queue_sel = 0; |
Michael S. Tsirkin | 7055e68 | 2009-06-21 19:50:13 +0300 | [diff] [blame] | 870 | vdev->config_vector = VIRTIO_NO_VECTOR; |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 871 | vdev->vq = qemu_mallocz(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX); |
Jason Wang | d3674c5 | 2011-05-18 13:57:37 +0800 | [diff] [blame] | 872 | vdev->vm_running = vm_running; |
Michael S. Tsirkin | 1cbdabe | 2010-03-17 13:08:02 +0200 | [diff] [blame] | 873 | for(i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) { |
Michael S. Tsirkin | b8193ad | 2009-09-07 21:20:15 +0300 | [diff] [blame] | 874 | vdev->vq[i].vector = VIRTIO_NO_VECTOR; |
Michael S. Tsirkin | 1cbdabe | 2010-03-17 13:08:02 +0200 | [diff] [blame] | 875 | vdev->vq[i].vdev = vdev; |
| 876 | } |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 877 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 878 | vdev->name = name; |
| 879 | vdev->config_len = config_size; |
| 880 | if (vdev->config_len) |
| 881 | vdev->config = qemu_mallocz(config_size); |
| 882 | else |
| 883 | vdev->config = NULL; |
| 884 | |
Michael S. Tsirkin | 85cf2a8 | 2011-01-10 14:28:40 +0200 | [diff] [blame] | 885 | vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change, vdev); |
| 886 | |
aliguori | 967f97f | 2008-12-04 19:38:57 +0000 | [diff] [blame] | 887 | return vdev; |
| 888 | } |
Paul Brook | 53c25ce | 2009-05-18 14:51:59 +0100 | [diff] [blame] | 889 | |
| 890 | void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding, |
| 891 | void *opaque) |
| 892 | { |
| 893 | vdev->binding = binding; |
| 894 | vdev->binding_opaque = opaque; |
| 895 | } |
Michael S. Tsirkin | 1cbdabe | 2010-03-17 13:08:02 +0200 | [diff] [blame] | 896 | |
| 897 | target_phys_addr_t virtio_queue_get_desc_addr(VirtIODevice *vdev, int n) |
| 898 | { |
| 899 | return vdev->vq[n].vring.desc; |
| 900 | } |
| 901 | |
| 902 | target_phys_addr_t virtio_queue_get_avail_addr(VirtIODevice *vdev, int n) |
| 903 | { |
| 904 | return vdev->vq[n].vring.avail; |
| 905 | } |
| 906 | |
| 907 | target_phys_addr_t virtio_queue_get_used_addr(VirtIODevice *vdev, int n) |
| 908 | { |
| 909 | return vdev->vq[n].vring.used; |
| 910 | } |
| 911 | |
| 912 | target_phys_addr_t virtio_queue_get_ring_addr(VirtIODevice *vdev, int n) |
| 913 | { |
| 914 | return vdev->vq[n].vring.desc; |
| 915 | } |
| 916 | |
| 917 | target_phys_addr_t virtio_queue_get_desc_size(VirtIODevice *vdev, int n) |
| 918 | { |
| 919 | return sizeof(VRingDesc) * vdev->vq[n].vring.num; |
| 920 | } |
| 921 | |
| 922 | target_phys_addr_t virtio_queue_get_avail_size(VirtIODevice *vdev, int n) |
| 923 | { |
| 924 | return offsetof(VRingAvail, ring) + |
Stefan Weil | 2b3af99 | 2010-04-01 16:59:51 -0500 | [diff] [blame] | 925 | sizeof(uint64_t) * vdev->vq[n].vring.num; |
Michael S. Tsirkin | 1cbdabe | 2010-03-17 13:08:02 +0200 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | target_phys_addr_t virtio_queue_get_used_size(VirtIODevice *vdev, int n) |
| 929 | { |
| 930 | return offsetof(VRingUsed, ring) + |
| 931 | sizeof(VRingUsedElem) * vdev->vq[n].vring.num; |
| 932 | } |
| 933 | |
| 934 | target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n) |
| 935 | { |
| 936 | return vdev->vq[n].vring.used - vdev->vq[n].vring.desc + |
| 937 | virtio_queue_get_used_size(vdev, n); |
| 938 | } |
| 939 | |
| 940 | uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) |
| 941 | { |
| 942 | return vdev->vq[n].last_avail_idx; |
| 943 | } |
| 944 | |
| 945 | void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx) |
| 946 | { |
| 947 | vdev->vq[n].last_avail_idx = idx; |
| 948 | } |
| 949 | |
| 950 | VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n) |
| 951 | { |
| 952 | return vdev->vq + n; |
| 953 | } |
| 954 | |
| 955 | EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq) |
| 956 | { |
| 957 | return &vq->guest_notifier; |
| 958 | } |
| 959 | EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq) |
| 960 | { |
| 961 | return &vq->host_notifier; |
| 962 | } |