]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio/virtio.c
dma: Let dma_memory_read/write() take MemTxAttrs argument
[mirror_qemu.git] / hw / virtio / virtio.c
CommitLineData
967f97fa
AL
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
9b8bfe21 14#include "qemu/osdep.h"
da34e65c 15#include "qapi/error.h"
4771d756 16#include "cpu.h"
64979a4d 17#include "trace.h"
1de7afc9 18#include "qemu/error-report.h"
d68cdae3 19#include "qemu/log.h"
db725815 20#include "qemu/main-loop.h"
0b8fa32f 21#include "qemu/module.h"
0d09e41a 22#include "hw/virtio/virtio.h"
ca77ee28 23#include "migration/qemu-file-types.h"
1de7afc9 24#include "qemu/atomic.h"
0d09e41a 25#include "hw/virtio/virtio-bus.h"
a27bd6c7 26#include "hw/qdev-properties.h"
cee3ca00 27#include "hw/virtio/virtio-access.h"
8607f5c3 28#include "sysemu/dma.h"
54d31236 29#include "sysemu/runstate.h"
7c78bdd7 30#include "standard-headers/linux/virtio_ids.h"
967f97fa 31
6ce69d1c
PM
32/*
33 * The alignment to use between consumer and producer parts of vring.
34 * x86 pagesize again. This is the default, used by transports like PCI
35 * which don't provide a means for the guest to tell the host the alignment.
36 */
f46f15bc
AL
37#define VIRTIO_PCI_VRING_ALIGN 4096
38
967f97fa
AL
39typedef struct VRingDesc
40{
41 uint64_t addr;
42 uint32_t len;
43 uint16_t flags;
44 uint16_t next;
45} VRingDesc;
46
a40dcec9
WX
47typedef struct VRingPackedDesc {
48 uint64_t addr;
49 uint32_t len;
50 uint16_t id;
51 uint16_t flags;
52} VRingPackedDesc;
53
967f97fa
AL
54typedef struct VRingAvail
55{
56 uint16_t flags;
57 uint16_t idx;
f7795e40 58 uint16_t ring[];
967f97fa
AL
59} VRingAvail;
60
61typedef struct VRingUsedElem
62{
63 uint32_t id;
64 uint32_t len;
65} VRingUsedElem;
66
67typedef struct VRingUsed
68{
69 uint16_t flags;
70 uint16_t idx;
f7795e40 71 VRingUsedElem ring[];
967f97fa
AL
72} VRingUsed;
73
c611c764
PB
74typedef struct VRingMemoryRegionCaches {
75 struct rcu_head rcu;
76 MemoryRegionCache desc;
77 MemoryRegionCache avail;
78 MemoryRegionCache used;
79} VRingMemoryRegionCaches;
80
967f97fa
AL
81typedef struct VRing
82{
83 unsigned int num;
46c5d082 84 unsigned int num_default;
6ce69d1c 85 unsigned int align;
a8170e5e
AK
86 hwaddr desc;
87 hwaddr avail;
88 hwaddr used;
c611c764 89 VRingMemoryRegionCaches *caches;
967f97fa
AL
90} VRing;
91
a40dcec9
WX
92typedef struct VRingPackedDescEvent {
93 uint16_t off_wrap;
94 uint16_t flags;
95} VRingPackedDescEvent ;
96
967f97fa
AL
97struct VirtQueue
98{
99 VRing vring;
86044b24 100 VirtQueueElement *used_elems;
be1fea9b
VM
101
102 /* Next head to pop */
967f97fa 103 uint16_t last_avail_idx;
a40dcec9 104 bool last_avail_wrap_counter;
b796fcd1 105
be1fea9b
VM
106 /* Last avail_idx read from VQ. */
107 uint16_t shadow_avail_idx;
a40dcec9 108 bool shadow_avail_wrap_counter;
be1fea9b 109
b796fcd1 110 uint16_t used_idx;
a40dcec9 111 bool used_wrap_counter;
b796fcd1 112
bcbabae8
MT
113 /* Last used index value we have signalled on */
114 uint16_t signalled_used;
115
116 /* Last used index value we have signalled on */
117 bool signalled_used_valid;
118
332fa82d
SH
119 /* Notification enabled? */
120 bool notification;
bcbabae8 121
e78a2b42
JW
122 uint16_t queue_index;
123
e66bcc40 124 unsigned int inuse;
bcbabae8 125
7055e687 126 uint16_t vector;
bf1780b0 127 VirtIOHandleOutput handle_output;
07931698 128 VirtIOHandleAIOOutput handle_aio_output;
1cbdabe2
MT
129 VirtIODevice *vdev;
130 EventNotifier guest_notifier;
131 EventNotifier host_notifier;
fcccb271 132 bool host_notifier_enabled;
e0d686bf 133 QLIST_ENTRY(VirtQueue) node;
967f97fa
AL
134};
135
b116d6c3 136/* Called within call_rcu(). */
c611c764
PB
137static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
138{
b116d6c3 139 assert(caches != NULL);
c611c764
PB
140 address_space_cache_destroy(&caches->desc);
141 address_space_cache_destroy(&caches->avail);
142 address_space_cache_destroy(&caches->used);
143 g_free(caches);
144}
145
45641dba
PB
146static void virtio_virtqueue_reset_region_cache(struct VirtQueue *vq)
147{
148 VRingMemoryRegionCaches *caches;
149
d73415a3
SH
150 caches = qatomic_read(&vq->vring.caches);
151 qatomic_rcu_set(&vq->vring.caches, NULL);
45641dba
PB
152 if (caches) {
153 call_rcu(caches, virtio_free_region_cache, rcu);
154 }
155}
156
c611c764
PB
157static void virtio_init_region_cache(VirtIODevice *vdev, int n)
158{
159 VirtQueue *vq = &vdev->vq[n];
160 VRingMemoryRegionCaches *old = vq->vring.caches;
45641dba 161 VRingMemoryRegionCaches *new = NULL;
c611c764 162 hwaddr addr, size;
e45da653 163 int64_t len;
86044b24 164 bool packed;
c611c764 165
c611c764
PB
166
167 addr = vq->vring.desc;
168 if (!addr) {
45641dba 169 goto out_no_cache;
c611c764
PB
170 }
171 new = g_new0(VRingMemoryRegionCaches, 1);
172 size = virtio_queue_get_desc_size(vdev, n);
86044b24
JW
173 packed = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ?
174 true : false;
e45da653 175 len = address_space_cache_init(&new->desc, vdev->dma_as,
86044b24 176 addr, size, packed);
e45da653
JW
177 if (len < size) {
178 virtio_error(vdev, "Cannot map desc");
179 goto err_desc;
180 }
c611c764 181
f90cda63 182 size = virtio_queue_get_used_size(vdev, n);
e45da653
JW
183 len = address_space_cache_init(&new->used, vdev->dma_as,
184 vq->vring.used, size, true);
185 if (len < size) {
186 virtio_error(vdev, "Cannot map used");
187 goto err_used;
188 }
c611c764 189
f90cda63 190 size = virtio_queue_get_avail_size(vdev, n);
e45da653
JW
191 len = address_space_cache_init(&new->avail, vdev->dma_as,
192 vq->vring.avail, size, false);
193 if (len < size) {
194 virtio_error(vdev, "Cannot map avail");
195 goto err_avail;
196 }
c611c764 197
d73415a3 198 qatomic_rcu_set(&vq->vring.caches, new);
c611c764
PB
199 if (old) {
200 call_rcu(old, virtio_free_region_cache, rcu);
201 }
e45da653
JW
202 return;
203
204err_avail:
45641dba 205 address_space_cache_destroy(&new->avail);
e45da653 206err_used:
45641dba 207 address_space_cache_destroy(&new->used);
e45da653 208err_desc:
45641dba
PB
209 address_space_cache_destroy(&new->desc);
210out_no_cache:
e45da653 211 g_free(new);
45641dba 212 virtio_virtqueue_reset_region_cache(vq);
c611c764
PB
213}
214
967f97fa 215/* virt queue functions */
ab223c95 216void virtio_queue_update_rings(VirtIODevice *vdev, int n)
967f97fa 217{
ab223c95 218 VRing *vring = &vdev->vq[n].vring;
53c25cea 219
758ead31 220 if (!vring->num || !vring->desc || !vring->align) {
ab223c95
CH
221 /* not yet setup -> nothing to do */
222 return;
223 }
224 vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
225 vring->used = vring_align(vring->avail +
226 offsetof(VRingAvail, ring[vring->num]),
227 vring->align);
c611c764 228 virtio_init_region_cache(vdev, n);
967f97fa
AL
229}
230
97cd965c 231/* Called within rcu_read_lock(). */
86044b24
JW
232static void vring_split_desc_read(VirtIODevice *vdev, VRingDesc *desc,
233 MemoryRegionCache *cache, int i)
967f97fa 234{
5eba0404
PB
235 address_space_read_cached(cache, i * sizeof(VRingDesc),
236 desc, sizeof(VRingDesc));
aa570d6f
PB
237 virtio_tswap64s(vdev, &desc->addr);
238 virtio_tswap32s(vdev, &desc->len);
239 virtio_tswap16s(vdev, &desc->flags);
240 virtio_tswap16s(vdev, &desc->next);
967f97fa
AL
241}
242
683f7665
JW
243static void vring_packed_event_read(VirtIODevice *vdev,
244 MemoryRegionCache *cache,
245 VRingPackedDescEvent *e)
246{
247 hwaddr off_off = offsetof(VRingPackedDescEvent, off_wrap);
248 hwaddr off_flags = offsetof(VRingPackedDescEvent, flags);
249
d152cdd6 250 e->flags = virtio_lduw_phys_cached(vdev, cache, off_flags);
683f7665
JW
251 /* Make sure flags is seen before off_wrap */
252 smp_rmb();
d152cdd6 253 e->off_wrap = virtio_lduw_phys_cached(vdev, cache, off_off);
683f7665
JW
254 virtio_tswap16s(vdev, &e->flags);
255}
256
257static void vring_packed_off_wrap_write(VirtIODevice *vdev,
258 MemoryRegionCache *cache,
259 uint16_t off_wrap)
260{
261 hwaddr off = offsetof(VRingPackedDescEvent, off_wrap);
262
d152cdd6 263 virtio_stw_phys_cached(vdev, cache, off, off_wrap);
683f7665
JW
264 address_space_cache_invalidate(cache, off, sizeof(off_wrap));
265}
266
267static void vring_packed_flags_write(VirtIODevice *vdev,
268 MemoryRegionCache *cache, uint16_t flags)
269{
270 hwaddr off = offsetof(VRingPackedDescEvent, flags);
271
d152cdd6 272 virtio_stw_phys_cached(vdev, cache, off, flags);
683f7665
JW
273 address_space_cache_invalidate(cache, off, sizeof(flags));
274}
275
86044b24 276/* Called within rcu_read_lock(). */
e0e2d644
JW
277static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *vq)
278{
d73415a3 279 return qatomic_rcu_read(&vq->vring.caches);
e0e2d644 280}
abdd16f4 281
97cd965c 282/* Called within rcu_read_lock(). */
967f97fa
AL
283static inline uint16_t vring_avail_flags(VirtQueue *vq)
284{
e0e2d644 285 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 286 hwaddr pa = offsetof(VRingAvail, flags);
abdd16f4
SH
287
288 if (!caches) {
289 return 0;
290 }
291
97cd965c 292 return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
967f97fa
AL
293}
294
97cd965c 295/* Called within rcu_read_lock(). */
967f97fa
AL
296static inline uint16_t vring_avail_idx(VirtQueue *vq)
297{
e0e2d644 298 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 299 hwaddr pa = offsetof(VRingAvail, idx);
abdd16f4
SH
300
301 if (!caches) {
302 return 0;
303 }
304
97cd965c 305 vq->shadow_avail_idx = virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
be1fea9b 306 return vq->shadow_avail_idx;
967f97fa
AL
307}
308
97cd965c 309/* Called within rcu_read_lock(). */
967f97fa
AL
310static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
311{
e0e2d644 312 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 313 hwaddr pa = offsetof(VRingAvail, ring[i]);
abdd16f4
SH
314
315 if (!caches) {
316 return 0;
317 }
318
97cd965c 319 return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
967f97fa
AL
320}
321
97cd965c 322/* Called within rcu_read_lock(). */
e9600c6c 323static inline uint16_t vring_get_used_event(VirtQueue *vq)
bcbabae8
MT
324{
325 return vring_avail_ring(vq, vq->vring.num);
326}
327
97cd965c 328/* Called within rcu_read_lock(). */
1cdd2ee5
VM
329static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
330 int i)
967f97fa 331{
e0e2d644 332 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 333 hwaddr pa = offsetof(VRingUsed, ring[i]);
abdd16f4
SH
334
335 if (!caches) {
336 return;
337 }
338
1cdd2ee5
VM
339 virtio_tswap32s(vq->vdev, &uelem->id);
340 virtio_tswap32s(vq->vdev, &uelem->len);
97cd965c
PB
341 address_space_write_cached(&caches->used, pa, uelem, sizeof(VRingUsedElem));
342 address_space_cache_invalidate(&caches->used, pa, sizeof(VRingUsedElem));
967f97fa
AL
343}
344
97cd965c 345/* Called within rcu_read_lock(). */
967f97fa
AL
346static uint16_t vring_used_idx(VirtQueue *vq)
347{
e0e2d644 348 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 349 hwaddr pa = offsetof(VRingUsed, idx);
abdd16f4
SH
350
351 if (!caches) {
352 return 0;
353 }
354
97cd965c 355 return virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
967f97fa
AL
356}
357
97cd965c 358/* Called within rcu_read_lock(). */
bcbabae8 359static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
967f97fa 360{
e0e2d644 361 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
97cd965c 362 hwaddr pa = offsetof(VRingUsed, idx);
abdd16f4
SH
363
364 if (caches) {
365 virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
366 address_space_cache_invalidate(&caches->used, pa, sizeof(val));
367 }
368
b796fcd1 369 vq->used_idx = val;
967f97fa
AL
370}
371
97cd965c 372/* Called within rcu_read_lock(). */
967f97fa
AL
373static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
374{
e0e2d644 375 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
cee3ca00 376 VirtIODevice *vdev = vq->vdev;
97cd965c 377 hwaddr pa = offsetof(VRingUsed, flags);
abdd16f4 378 uint16_t flags;
97cd965c 379
abdd16f4
SH
380 if (!caches) {
381 return;
382 }
383
384 flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
97cd965c
PB
385 virtio_stw_phys_cached(vdev, &caches->used, pa, flags | mask);
386 address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
967f97fa
AL
387}
388
97cd965c 389/* Called within rcu_read_lock(). */
967f97fa
AL
390static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
391{
e0e2d644 392 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
cee3ca00 393 VirtIODevice *vdev = vq->vdev;
97cd965c 394 hwaddr pa = offsetof(VRingUsed, flags);
abdd16f4 395 uint16_t flags;
97cd965c 396
abdd16f4
SH
397 if (!caches) {
398 return;
399 }
400
401 flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
97cd965c
PB
402 virtio_stw_phys_cached(vdev, &caches->used, pa, flags & ~mask);
403 address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
967f97fa
AL
404}
405
97cd965c 406/* Called within rcu_read_lock(). */
e9600c6c 407static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
bcbabae8 408{
97cd965c 409 VRingMemoryRegionCaches *caches;
a8170e5e 410 hwaddr pa;
332fa82d 411 if (!vq->notification) {
bcbabae8
MT
412 return;
413 }
97cd965c 414
e0e2d644 415 caches = vring_get_region_caches(vq);
abdd16f4
SH
416 if (!caches) {
417 return;
418 }
419
97cd965c
PB
420 pa = offsetof(VRingUsed, ring[vq->vring.num]);
421 virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
3cdf8473 422 address_space_cache_invalidate(&caches->used, pa, sizeof(val));
bcbabae8
MT
423}
424
683f7665 425static void virtio_queue_split_set_notification(VirtQueue *vq, int enable)
967f97fa 426{
b5f53d04
DDAG
427 RCU_READ_LOCK_GUARD();
428
95129d6f 429 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 430 vring_set_avail_event(vq, vring_avail_idx(vq));
bcbabae8 431 } else if (enable) {
967f97fa 432 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 433 } else {
967f97fa 434 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
bcbabae8 435 }
92045d80
MT
436 if (enable) {
437 /* Expose avail event/used flags before caller checks the avail idx. */
438 smp_mb();
439 }
967f97fa
AL
440}
441
683f7665
JW
442static void virtio_queue_packed_set_notification(VirtQueue *vq, int enable)
443{
444 uint16_t off_wrap;
445 VRingPackedDescEvent e;
446 VRingMemoryRegionCaches *caches;
447
b5f53d04 448 RCU_READ_LOCK_GUARD();
abdd16f4
SH
449 caches = vring_get_region_caches(vq);
450 if (!caches) {
451 return;
452 }
453
683f7665
JW
454 vring_packed_event_read(vq->vdev, &caches->used, &e);
455
456 if (!enable) {
457 e.flags = VRING_PACKED_EVENT_FLAG_DISABLE;
458 } else if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
459 off_wrap = vq->shadow_avail_idx | vq->shadow_avail_wrap_counter << 15;
460 vring_packed_off_wrap_write(vq->vdev, &caches->used, off_wrap);
461 /* Make sure off_wrap is wrote before flags */
462 smp_wmb();
463 e.flags = VRING_PACKED_EVENT_FLAG_DESC;
464 } else {
465 e.flags = VRING_PACKED_EVENT_FLAG_ENABLE;
466 }
467
468 vring_packed_flags_write(vq->vdev, &caches->used, e.flags);
469 if (enable) {
470 /* Expose avail event/used flags before caller checks the avail idx. */
471 smp_mb();
472 }
683f7665
JW
473}
474
d0435bc5
SH
475bool virtio_queue_get_notification(VirtQueue *vq)
476{
477 return vq->notification;
478}
479
683f7665
JW
480void virtio_queue_set_notification(VirtQueue *vq, int enable)
481{
482 vq->notification = enable;
483
484 if (!vq->vring.desc) {
485 return;
486 }
487
488 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
489 virtio_queue_packed_set_notification(vq, enable);
490 } else {
491 virtio_queue_split_set_notification(vq, enable);
492 }
493}
494
967f97fa
AL
495int virtio_queue_ready(VirtQueue *vq)
496{
497 return vq->vring.avail != 0;
498}
499
86044b24
JW
500static void vring_packed_desc_read_flags(VirtIODevice *vdev,
501 uint16_t *flags,
502 MemoryRegionCache *cache,
503 int i)
504{
f463e761
JW
505 hwaddr off = i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flags);
506
507 *flags = virtio_lduw_phys_cached(vdev, cache, off);
86044b24
JW
508}
509
510static void vring_packed_desc_read(VirtIODevice *vdev,
511 VRingPackedDesc *desc,
512 MemoryRegionCache *cache,
513 int i, bool strict_order)
514{
515 hwaddr off = i * sizeof(VRingPackedDesc);
516
517 vring_packed_desc_read_flags(vdev, &desc->flags, cache, i);
518
519 if (strict_order) {
520 /* Make sure flags is read before the rest fields. */
521 smp_rmb();
522 }
523
524 address_space_read_cached(cache, off + offsetof(VRingPackedDesc, addr),
525 &desc->addr, sizeof(desc->addr));
526 address_space_read_cached(cache, off + offsetof(VRingPackedDesc, id),
527 &desc->id, sizeof(desc->id));
528 address_space_read_cached(cache, off + offsetof(VRingPackedDesc, len),
529 &desc->len, sizeof(desc->len));
530 virtio_tswap64s(vdev, &desc->addr);
531 virtio_tswap16s(vdev, &desc->id);
532 virtio_tswap32s(vdev, &desc->len);
533}
534
535static void vring_packed_desc_write_data(VirtIODevice *vdev,
536 VRingPackedDesc *desc,
537 MemoryRegionCache *cache,
538 int i)
539{
540 hwaddr off_id = i * sizeof(VRingPackedDesc) +
541 offsetof(VRingPackedDesc, id);
542 hwaddr off_len = i * sizeof(VRingPackedDesc) +
543 offsetof(VRingPackedDesc, len);
544
545 virtio_tswap32s(vdev, &desc->len);
546 virtio_tswap16s(vdev, &desc->id);
547 address_space_write_cached(cache, off_id, &desc->id, sizeof(desc->id));
548 address_space_cache_invalidate(cache, off_id, sizeof(desc->id));
549 address_space_write_cached(cache, off_len, &desc->len, sizeof(desc->len));
550 address_space_cache_invalidate(cache, off_len, sizeof(desc->len));
551}
552
553static void vring_packed_desc_write_flags(VirtIODevice *vdev,
554 VRingPackedDesc *desc,
555 MemoryRegionCache *cache,
556 int i)
557{
558 hwaddr off = i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flags);
559
f463e761 560 virtio_stw_phys_cached(vdev, cache, off, desc->flags);
86044b24
JW
561 address_space_cache_invalidate(cache, off, sizeof(desc->flags));
562}
563
564static void vring_packed_desc_write(VirtIODevice *vdev,
565 VRingPackedDesc *desc,
566 MemoryRegionCache *cache,
567 int i, bool strict_order)
568{
569 vring_packed_desc_write_data(vdev, desc, cache, i);
570 if (strict_order) {
571 /* Make sure data is wrote before flags. */
572 smp_wmb();
573 }
574 vring_packed_desc_write_flags(vdev, desc, cache, i);
575}
576
577static inline bool is_desc_avail(uint16_t flags, bool wrap_counter)
578{
579 bool avail, used;
580
581 avail = !!(flags & (1 << VRING_PACKED_DESC_F_AVAIL));
582 used = !!(flags & (1 << VRING_PACKED_DESC_F_USED));
583 return (avail != used) && (avail == wrap_counter);
584}
585
be1fea9b 586/* Fetch avail_idx from VQ memory only when we really need to know if
97cd965c
PB
587 * guest has added some buffers.
588 * Called within rcu_read_lock(). */
589static int virtio_queue_empty_rcu(VirtQueue *vq)
967f97fa 590{
9d7bd082 591 if (virtio_device_disabled(vq->vdev)) {
2d1df859
FZ
592 return 1;
593 }
594
168e4af3
JW
595 if (unlikely(!vq->vring.avail)) {
596 return 1;
597 }
598
be1fea9b
VM
599 if (vq->shadow_avail_idx != vq->last_avail_idx) {
600 return 0;
601 }
602
967f97fa
AL
603 return vring_avail_idx(vq) == vq->last_avail_idx;
604}
605
86044b24 606static int virtio_queue_split_empty(VirtQueue *vq)
97cd965c
PB
607{
608 bool empty;
609
9d7bd082 610 if (virtio_device_disabled(vq->vdev)) {
2d1df859
FZ
611 return 1;
612 }
613
168e4af3
JW
614 if (unlikely(!vq->vring.avail)) {
615 return 1;
616 }
617
97cd965c
PB
618 if (vq->shadow_avail_idx != vq->last_avail_idx) {
619 return 0;
620 }
621
b5f53d04 622 RCU_READ_LOCK_GUARD();
97cd965c 623 empty = vring_avail_idx(vq) == vq->last_avail_idx;
97cd965c
PB
624 return empty;
625}
626
7f51bedd 627/* Called within rcu_read_lock(). */
86044b24
JW
628static int virtio_queue_packed_empty_rcu(VirtQueue *vq)
629{
630 struct VRingPackedDesc desc;
631 VRingMemoryRegionCaches *cache;
632
633 if (unlikely(!vq->vring.desc)) {
634 return 1;
635 }
636
637 cache = vring_get_region_caches(vq);
abdd16f4
SH
638 if (!cache) {
639 return 1;
640 }
641
86044b24
JW
642 vring_packed_desc_read_flags(vq->vdev, &desc.flags, &cache->desc,
643 vq->last_avail_idx);
644
645 return !is_desc_avail(desc.flags, vq->last_avail_wrap_counter);
646}
647
648static int virtio_queue_packed_empty(VirtQueue *vq)
649{
b5f53d04
DDAG
650 RCU_READ_LOCK_GUARD();
651 return virtio_queue_packed_empty_rcu(vq);
86044b24
JW
652}
653
654int virtio_queue_empty(VirtQueue *vq)
655{
656 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
657 return virtio_queue_packed_empty(vq);
658 } else {
659 return virtio_queue_split_empty(vq);
660 }
661}
662
ce317461
JW
663static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
664 unsigned int len)
967f97fa 665{
8607f5c3 666 AddressSpace *dma_as = vq->vdev->dma_as;
967f97fa
AL
667 unsigned int offset;
668 int i;
669
967f97fa
AL
670 offset = 0;
671 for (i = 0; i < elem->in_num; i++) {
672 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
673
8607f5c3
JW
674 dma_memory_unmap(dma_as, elem->in_sg[i].iov_base,
675 elem->in_sg[i].iov_len,
676 DMA_DIRECTION_FROM_DEVICE, size);
967f97fa 677
0cea71a2 678 offset += size;
967f97fa
AL
679 }
680
26b258e1 681 for (i = 0; i < elem->out_num; i++)
8607f5c3
JW
682 dma_memory_unmap(dma_as, elem->out_sg[i].iov_base,
683 elem->out_sg[i].iov_len,
684 DMA_DIRECTION_TO_DEVICE,
685 elem->out_sg[i].iov_len);
ce317461
JW
686}
687
2640d2a5
SH
688/* virtqueue_detach_element:
689 * @vq: The #VirtQueue
690 * @elem: The #VirtQueueElement
691 * @len: number of bytes written
692 *
693 * Detach the element from the virtqueue. This function is suitable for device
694 * reset or other situations where a #VirtQueueElement is simply freed and will
695 * not be pushed or discarded.
696 */
697void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
698 unsigned int len)
699{
86044b24 700 vq->inuse -= elem->ndescs;
2640d2a5
SH
701 virtqueue_unmap_sg(vq, elem, len);
702}
703
86044b24
JW
704static void virtqueue_split_rewind(VirtQueue *vq, unsigned int num)
705{
706 vq->last_avail_idx -= num;
707}
708
709static void virtqueue_packed_rewind(VirtQueue *vq, unsigned int num)
710{
711 if (vq->last_avail_idx < num) {
712 vq->last_avail_idx = vq->vring.num + vq->last_avail_idx - num;
713 vq->last_avail_wrap_counter ^= 1;
714 } else {
715 vq->last_avail_idx -= num;
716 }
717}
718
27e57efe 719/* virtqueue_unpop:
2640d2a5
SH
720 * @vq: The #VirtQueue
721 * @elem: The #VirtQueueElement
722 * @len: number of bytes written
723 *
724 * Pretend the most recent element wasn't popped from the virtqueue. The next
725 * call to virtqueue_pop() will refetch the element.
726 */
27e57efe
LP
727void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
728 unsigned int len)
29b9f5ef 729{
86044b24
JW
730
731 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
732 virtqueue_packed_rewind(vq, 1);
733 } else {
734 virtqueue_split_rewind(vq, 1);
735 }
736
2640d2a5 737 virtqueue_detach_element(vq, elem, len);
29b9f5ef
JW
738}
739
297a75e6
SH
740/* virtqueue_rewind:
741 * @vq: The #VirtQueue
742 * @num: Number of elements to push back
743 *
744 * Pretend that elements weren't popped from the virtqueue. The next
745 * virtqueue_pop() will refetch the oldest element.
746 *
27e57efe 747 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
297a75e6
SH
748 *
749 * Returns: true on success, false if @num is greater than the number of in use
750 * elements.
751 */
752bool virtqueue_rewind(VirtQueue *vq, unsigned int num)
753{
754 if (num > vq->inuse) {
755 return false;
756 }
86044b24 757
297a75e6 758 vq->inuse -= num;
86044b24
JW
759 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
760 virtqueue_packed_rewind(vq, num);
761 } else {
762 virtqueue_split_rewind(vq, num);
763 }
297a75e6
SH
764 return true;
765}
766
86044b24 767static void virtqueue_split_fill(VirtQueue *vq, const VirtQueueElement *elem,
ce317461
JW
768 unsigned int len, unsigned int idx)
769{
1cdd2ee5
VM
770 VRingUsedElem uelem;
771
168e4af3
JW
772 if (unlikely(!vq->vring.used)) {
773 return;
774 }
775
b796fcd1 776 idx = (idx + vq->used_idx) % vq->vring.num;
967f97fa 777
1cdd2ee5
VM
778 uelem.id = elem->index;
779 uelem.len = len;
780 vring_used_write(vq, &uelem, idx);
967f97fa
AL
781}
782
86044b24
JW
783static void virtqueue_packed_fill(VirtQueue *vq, const VirtQueueElement *elem,
784 unsigned int len, unsigned int idx)
785{
786 vq->used_elems[idx].index = elem->index;
787 vq->used_elems[idx].len = len;
788 vq->used_elems[idx].ndescs = elem->ndescs;
789}
790
791static void virtqueue_packed_fill_desc(VirtQueue *vq,
792 const VirtQueueElement *elem,
793 unsigned int idx,
794 bool strict_order)
795{
796 uint16_t head;
797 VRingMemoryRegionCaches *caches;
798 VRingPackedDesc desc = {
799 .id = elem->index,
800 .len = elem->len,
801 };
802 bool wrap_counter = vq->used_wrap_counter;
803
804 if (unlikely(!vq->vring.desc)) {
805 return;
806 }
807
808 head = vq->used_idx + idx;
809 if (head >= vq->vring.num) {
810 head -= vq->vring.num;
811 wrap_counter ^= 1;
812 }
813 if (wrap_counter) {
814 desc.flags |= (1 << VRING_PACKED_DESC_F_AVAIL);
815 desc.flags |= (1 << VRING_PACKED_DESC_F_USED);
816 } else {
817 desc.flags &= ~(1 << VRING_PACKED_DESC_F_AVAIL);
818 desc.flags &= ~(1 << VRING_PACKED_DESC_F_USED);
819 }
820
821 caches = vring_get_region_caches(vq);
abdd16f4
SH
822 if (!caches) {
823 return;
824 }
825
86044b24
JW
826 vring_packed_desc_write(vq->vdev, &desc, &caches->desc, head, strict_order);
827}
828
97cd965c 829/* Called within rcu_read_lock(). */
86044b24
JW
830void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
831 unsigned int len, unsigned int idx)
967f97fa 832{
86044b24
JW
833 trace_virtqueue_fill(vq, elem, len, idx);
834
835 virtqueue_unmap_sg(vq, elem, len);
f5ed3663 836
9d7bd082 837 if (virtio_device_disabled(vq->vdev)) {
f5ed3663
SH
838 return;
839 }
840
86044b24
JW
841 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
842 virtqueue_packed_fill(vq, elem, len, idx);
843 } else {
844 virtqueue_split_fill(vq, elem, len, idx);
845 }
846}
847
848/* Called within rcu_read_lock(). */
849static void virtqueue_split_flush(VirtQueue *vq, unsigned int count)
850{
851 uint16_t old, new;
852
168e4af3
JW
853 if (unlikely(!vq->vring.used)) {
854 return;
855 }
856
967f97fa 857 /* Make sure buffer is written before we update index. */
b90d2f35 858 smp_wmb();
64979a4d 859 trace_virtqueue_flush(vq, count);
b796fcd1 860 old = vq->used_idx;
bcbabae8
MT
861 new = old + count;
862 vring_used_idx_set(vq, new);
967f97fa 863 vq->inuse -= count;
bcbabae8
MT
864 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
865 vq->signalled_used_valid = false;
967f97fa
AL
866}
867
86044b24
JW
868static void virtqueue_packed_flush(VirtQueue *vq, unsigned int count)
869{
870 unsigned int i, ndescs = 0;
871
872 if (unlikely(!vq->vring.desc)) {
873 return;
874 }
875
876 for (i = 1; i < count; i++) {
877 virtqueue_packed_fill_desc(vq, &vq->used_elems[i], i, false);
878 ndescs += vq->used_elems[i].ndescs;
879 }
880 virtqueue_packed_fill_desc(vq, &vq->used_elems[0], 0, true);
881 ndescs += vq->used_elems[0].ndescs;
882
883 vq->inuse -= ndescs;
884 vq->used_idx += ndescs;
885 if (vq->used_idx >= vq->vring.num) {
886 vq->used_idx -= vq->vring.num;
887 vq->used_wrap_counter ^= 1;
888 }
889}
890
891void virtqueue_flush(VirtQueue *vq, unsigned int count)
892{
9d7bd082 893 if (virtio_device_disabled(vq->vdev)) {
86044b24
JW
894 vq->inuse -= count;
895 return;
896 }
897
898 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
899 virtqueue_packed_flush(vq, count);
900 } else {
901 virtqueue_split_flush(vq, count);
902 }
903}
904
967f97fa
AL
905void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
906 unsigned int len)
907{
b5f53d04 908 RCU_READ_LOCK_GUARD();
967f97fa
AL
909 virtqueue_fill(vq, elem, len, 0);
910 virtqueue_flush(vq, 1);
911}
912
97cd965c 913/* Called within rcu_read_lock(). */
967f97fa
AL
914static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
915{
916 uint16_t num_heads = vring_avail_idx(vq) - idx;
917
918 /* Check it isn't doing very strange things with descriptor numbers. */
bb6834cf 919 if (num_heads > vq->vring.num) {
4355c1ab 920 virtio_error(vq->vdev, "Guest moved used index from %u to %u",
be1fea9b 921 idx, vq->shadow_avail_idx);
4355c1ab 922 return -EINVAL;
bb6834cf 923 }
a821ce59
MT
924 /* On success, callers read a descriptor at vq->last_avail_idx.
925 * Make sure descriptor read does not bypass avail index read. */
926 if (num_heads) {
927 smp_rmb();
928 }
967f97fa
AL
929
930 return num_heads;
931}
932
97cd965c 933/* Called within rcu_read_lock(). */
fb1131b6
SH
934static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
935 unsigned int *head)
967f97fa 936{
967f97fa
AL
937 /* Grab the next descriptor number they're advertising, and increment
938 * the index we've seen. */
fb1131b6 939 *head = vring_avail_ring(vq, idx % vq->vring.num);
967f97fa
AL
940
941 /* If their number is silly, that's a fatal mistake. */
fb1131b6
SH
942 if (*head >= vq->vring.num) {
943 virtio_error(vq->vdev, "Guest says index %u is available", *head);
944 return false;
bb6834cf 945 }
967f97fa 946
fb1131b6 947 return true;
967f97fa
AL
948}
949
412e0e81
SH
950enum {
951 VIRTQUEUE_READ_DESC_ERROR = -1,
952 VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
953 VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
954};
967f97fa 955
86044b24
JW
956static int virtqueue_split_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
957 MemoryRegionCache *desc_cache,
958 unsigned int max, unsigned int *next)
412e0e81 959{
967f97fa 960 /* If this descriptor says it doesn't chain, we're done. */
aa570d6f 961 if (!(desc->flags & VRING_DESC_F_NEXT)) {
412e0e81 962 return VIRTQUEUE_READ_DESC_DONE;
cee3ca00 963 }
967f97fa
AL
964
965 /* Check they're not leading us off end of descriptors. */
412e0e81 966 *next = desc->next;
967f97fa 967 /* Make sure compiler knows to grab that: we don't want it changing! */
b90d2f35 968 smp_wmb();
967f97fa 969
412e0e81
SH
970 if (*next >= max) {
971 virtio_error(vdev, "Desc next is %u", *next);
972 return VIRTQUEUE_READ_DESC_ERROR;
bb6834cf 973 }
967f97fa 974
86044b24 975 vring_split_desc_read(vdev, desc, desc_cache, *next);
412e0e81 976 return VIRTQUEUE_READ_DESC_MORE;
967f97fa
AL
977}
978
d6ed27ba 979/* Called within rcu_read_lock(). */
86044b24
JW
980static void virtqueue_split_get_avail_bytes(VirtQueue *vq,
981 unsigned int *in_bytes, unsigned int *out_bytes,
d6ed27ba
PMD
982 unsigned max_in_bytes, unsigned max_out_bytes,
983 VRingMemoryRegionCaches *caches)
967f97fa 984{
9796d0ac
PB
985 VirtIODevice *vdev = vq->vdev;
986 unsigned int max, idx;
385ce95d 987 unsigned int total_bufs, in_total, out_total;
5eba0404
PB
988 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
989 int64_t len = 0;
412e0e81 990 int rc;
967f97fa
AL
991
992 idx = vq->last_avail_idx;
efeea6d0 993 total_bufs = in_total = out_total = 0;
9796d0ac
PB
994
995 max = vq->vring.num;
abdd16f4 996
4355c1ab 997 while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
991976f7 998 MemoryRegionCache *desc_cache = &caches->desc;
9796d0ac 999 unsigned int num_bufs;
aa570d6f 1000 VRingDesc desc;
b1c7c07f 1001 unsigned int i;
967f97fa 1002
efeea6d0 1003 num_bufs = total_bufs;
fb1131b6
SH
1004
1005 if (!virtqueue_get_head(vq, idx++, &i)) {
1006 goto err;
1007 }
1008
86044b24 1009 vring_split_desc_read(vdev, &desc, desc_cache, i);
efeea6d0 1010
aa570d6f 1011 if (desc.flags & VRING_DESC_F_INDIRECT) {
74231929 1012 if (!desc.len || (desc.len % sizeof(VRingDesc))) {
d65abf85
SH
1013 virtio_error(vdev, "Invalid size for indirect buffer table");
1014 goto err;
efeea6d0
MM
1015 }
1016
1017 /* If we've got too many, that implies a descriptor loop. */
1018 if (num_bufs >= max) {
d65abf85
SH
1019 virtio_error(vdev, "Looped descriptor");
1020 goto err;
efeea6d0
MM
1021 }
1022
1023 /* loop over the indirect descriptor table */
5eba0404
PB
1024 len = address_space_cache_init(&indirect_desc_cache,
1025 vdev->dma_as,
1026 desc.addr, desc.len, false);
1027 desc_cache = &indirect_desc_cache;
9796d0ac
PB
1028 if (len < desc.len) {
1029 virtio_error(vdev, "Cannot map indirect buffer");
1030 goto err;
1031 }
1032
aa570d6f 1033 max = desc.len / sizeof(VRingDesc);
1ae2757c 1034 num_bufs = i = 0;
86044b24 1035 vring_split_desc_read(vdev, &desc, desc_cache, i);
efeea6d0
MM
1036 }
1037
967f97fa
AL
1038 do {
1039 /* If we've got too many, that implies a descriptor loop. */
5774cf98 1040 if (++num_bufs > max) {
d65abf85
SH
1041 virtio_error(vdev, "Looped descriptor");
1042 goto err;
bb6834cf 1043 }
967f97fa 1044
aa570d6f
PB
1045 if (desc.flags & VRING_DESC_F_WRITE) {
1046 in_total += desc.len;
967f97fa 1047 } else {
aa570d6f 1048 out_total += desc.len;
967f97fa 1049 }
e1f7b481
MT
1050 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
1051 goto done;
1052 }
412e0e81 1053
86044b24 1054 rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i);
412e0e81
SH
1055 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1056
1057 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
1058 goto err;
1059 }
efeea6d0 1060
5eba0404
PB
1061 if (desc_cache == &indirect_desc_cache) {
1062 address_space_cache_destroy(&indirect_desc_cache);
efeea6d0 1063 total_bufs++;
9796d0ac
PB
1064 } else {
1065 total_bufs = num_bufs;
1066 }
967f97fa 1067 }
4355c1ab
SH
1068
1069 if (rc < 0) {
1070 goto err;
1071 }
1072
e1f7b481 1073done:
5eba0404 1074 address_space_cache_destroy(&indirect_desc_cache);
0d8d7690 1075 if (in_bytes) {
86044b24
JW
1076 *in_bytes = in_total;
1077 }
1078 if (out_bytes) {
1079 *out_bytes = out_total;
1080 }
86044b24
JW
1081 return;
1082
1083err:
1084 in_total = out_total = 0;
1085 goto done;
1086}
1087
1088static int virtqueue_packed_read_next_desc(VirtQueue *vq,
1089 VRingPackedDesc *desc,
1090 MemoryRegionCache
1091 *desc_cache,
1092 unsigned int max,
1093 unsigned int *next,
1094 bool indirect)
1095{
1096 /* If this descriptor says it doesn't chain, we're done. */
1097 if (!indirect && !(desc->flags & VRING_DESC_F_NEXT)) {
1098 return VIRTQUEUE_READ_DESC_DONE;
1099 }
1100
1101 ++*next;
1102 if (*next == max) {
1103 if (indirect) {
1104 return VIRTQUEUE_READ_DESC_DONE;
1105 } else {
1106 (*next) -= vq->vring.num;
1107 }
1108 }
1109
1110 vring_packed_desc_read(vq->vdev, desc, desc_cache, *next, false);
1111 return VIRTQUEUE_READ_DESC_MORE;
1112}
1113
d6ed27ba 1114/* Called within rcu_read_lock(). */
86044b24
JW
1115static void virtqueue_packed_get_avail_bytes(VirtQueue *vq,
1116 unsigned int *in_bytes,
1117 unsigned int *out_bytes,
1118 unsigned max_in_bytes,
d6ed27ba
PMD
1119 unsigned max_out_bytes,
1120 VRingMemoryRegionCaches *caches)
86044b24
JW
1121{
1122 VirtIODevice *vdev = vq->vdev;
1123 unsigned int max, idx;
1124 unsigned int total_bufs, in_total, out_total;
1125 MemoryRegionCache *desc_cache;
86044b24
JW
1126 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
1127 int64_t len = 0;
1128 VRingPackedDesc desc;
1129 bool wrap_counter;
1130
86044b24
JW
1131 idx = vq->last_avail_idx;
1132 wrap_counter = vq->last_avail_wrap_counter;
1133 total_bufs = in_total = out_total = 0;
1134
1135 max = vq->vring.num;
86044b24
JW
1136
1137 for (;;) {
1138 unsigned int num_bufs = total_bufs;
1139 unsigned int i = idx;
1140 int rc;
1141
1142 desc_cache = &caches->desc;
1143 vring_packed_desc_read(vdev, &desc, desc_cache, idx, true);
1144 if (!is_desc_avail(desc.flags, wrap_counter)) {
1145 break;
1146 }
1147
1148 if (desc.flags & VRING_DESC_F_INDIRECT) {
1149 if (desc.len % sizeof(VRingPackedDesc)) {
1150 virtio_error(vdev, "Invalid size for indirect buffer table");
1151 goto err;
1152 }
1153
1154 /* If we've got too many, that implies a descriptor loop. */
1155 if (num_bufs >= max) {
1156 virtio_error(vdev, "Looped descriptor");
1157 goto err;
1158 }
1159
1160 /* loop over the indirect descriptor table */
1161 len = address_space_cache_init(&indirect_desc_cache,
1162 vdev->dma_as,
1163 desc.addr, desc.len, false);
1164 desc_cache = &indirect_desc_cache;
1165 if (len < desc.len) {
1166 virtio_error(vdev, "Cannot map indirect buffer");
1167 goto err;
1168 }
1169
1170 max = desc.len / sizeof(VRingPackedDesc);
1171 num_bufs = i = 0;
1172 vring_packed_desc_read(vdev, &desc, desc_cache, i, false);
1173 }
1174
1175 do {
1176 /* If we've got too many, that implies a descriptor loop. */
1177 if (++num_bufs > max) {
1178 virtio_error(vdev, "Looped descriptor");
1179 goto err;
1180 }
1181
1182 if (desc.flags & VRING_DESC_F_WRITE) {
1183 in_total += desc.len;
1184 } else {
1185 out_total += desc.len;
1186 }
1187 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
1188 goto done;
1189 }
1190
1191 rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max,
1192 &i, desc_cache ==
1193 &indirect_desc_cache);
1194 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1195
1196 if (desc_cache == &indirect_desc_cache) {
1197 address_space_cache_destroy(&indirect_desc_cache);
1198 total_bufs++;
1199 idx++;
1200 } else {
1201 idx += num_bufs - total_bufs;
1202 total_bufs = num_bufs;
1203 }
1204
1205 if (idx >= vq->vring.num) {
1206 idx -= vq->vring.num;
1207 wrap_counter ^= 1;
1208 }
1209 }
1210
1211 /* Record the index and wrap counter for a kick we want */
1212 vq->shadow_avail_idx = idx;
1213 vq->shadow_avail_wrap_counter = wrap_counter;
1214done:
1215 address_space_cache_destroy(&indirect_desc_cache);
1216 if (in_bytes) {
1217 *in_bytes = in_total;
1218 }
1219 if (out_bytes) {
1220 *out_bytes = out_total;
1221 }
86044b24
JW
1222 return;
1223
1224err:
1225 in_total = out_total = 0;
1226 goto done;
1227}
1228
1229void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
1230 unsigned int *out_bytes,
1231 unsigned max_in_bytes, unsigned max_out_bytes)
1232{
1233 uint16_t desc_size;
1234 VRingMemoryRegionCaches *caches;
1235
d6ed27ba
PMD
1236 RCU_READ_LOCK_GUARD();
1237
86044b24
JW
1238 if (unlikely(!vq->vring.desc)) {
1239 goto err;
1240 }
1241
1242 caches = vring_get_region_caches(vq);
abdd16f4
SH
1243 if (!caches) {
1244 goto err;
1245 }
1246
86044b24
JW
1247 desc_size = virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ?
1248 sizeof(VRingPackedDesc) : sizeof(VRingDesc);
1249 if (caches->desc.len < vq->vring.num * desc_size) {
1250 virtio_error(vq->vdev, "Cannot map descriptor ring");
1251 goto err;
1252 }
1253
1254 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
1255 virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes,
d6ed27ba
PMD
1256 max_in_bytes, max_out_bytes,
1257 caches);
86044b24
JW
1258 } else {
1259 virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes,
d6ed27ba
PMD
1260 max_in_bytes, max_out_bytes,
1261 caches);
86044b24
JW
1262 }
1263
1264 return;
1265err:
1266 if (in_bytes) {
1267 *in_bytes = 0;
0d8d7690
AS
1268 }
1269 if (out_bytes) {
86044b24 1270 *out_bytes = 0;
0d8d7690
AS
1271 }
1272}
967f97fa 1273
0d8d7690
AS
1274int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
1275 unsigned int out_bytes)
1276{
1277 unsigned int in_total, out_total;
1278
e1f7b481
MT
1279 virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
1280 return in_bytes <= in_total && out_bytes <= out_total;
967f97fa
AL
1281}
1282
ec55da19
SH
1283static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg,
1284 hwaddr *addr, struct iovec *iov,
3b3b0628
PB
1285 unsigned int max_num_sg, bool is_write,
1286 hwaddr pa, size_t sz)
1287{
ec55da19 1288 bool ok = false;
3b3b0628
PB
1289 unsigned num_sg = *p_num_sg;
1290 assert(num_sg <= max_num_sg);
1291
1e7aed70 1292 if (!sz) {
ec55da19
SH
1293 virtio_error(vdev, "virtio: zero sized buffers are not allowed");
1294 goto out;
1e7aed70
PP
1295 }
1296
3b3b0628
PB
1297 while (sz) {
1298 hwaddr len = sz;
1299
1300 if (num_sg == max_num_sg) {
ec55da19
SH
1301 virtio_error(vdev, "virtio: too many write descriptors in "
1302 "indirect table");
1303 goto out;
3b3b0628
PB
1304 }
1305
8607f5c3
JW
1306 iov[num_sg].iov_base = dma_memory_map(vdev->dma_as, pa, &len,
1307 is_write ?
1308 DMA_DIRECTION_FROM_DEVICE :
1309 DMA_DIRECTION_TO_DEVICE);
973e7170 1310 if (!iov[num_sg].iov_base) {
ec55da19
SH
1311 virtio_error(vdev, "virtio: bogus descriptor or out of resources");
1312 goto out;
973e7170
PP
1313 }
1314
3b3b0628
PB
1315 iov[num_sg].iov_len = len;
1316 addr[num_sg] = pa;
1317
1318 sz -= len;
1319 pa += len;
1320 num_sg++;
1321 }
ec55da19
SH
1322 ok = true;
1323
1324out:
3b3b0628 1325 *p_num_sg = num_sg;
ec55da19
SH
1326 return ok;
1327}
1328
1329/* Only used by error code paths before we have a VirtQueueElement (therefore
1330 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
1331 * yet.
1332 */
1333static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,
1334 struct iovec *iov)
1335{
1336 unsigned int i;
1337
1338 for (i = 0; i < out_num + in_num; i++) {
1339 int is_write = i >= out_num;
1340
1341 cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0);
1342 iov++;
1343 }
3b3b0628
PB
1344}
1345
8607f5c3 1346static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
e4fbf5b2 1347 hwaddr *addr, unsigned int num_sg,
22953364 1348 bool is_write)
42fb2e07
KW
1349{
1350 unsigned int i;
a8170e5e 1351 hwaddr len;
42fb2e07 1352
e4fbf5b2 1353 for (i = 0; i < num_sg; i++) {
42fb2e07 1354 len = sg[i].iov_len;
8607f5c3
JW
1355 sg[i].iov_base = dma_memory_map(vdev->dma_as,
1356 addr[i], &len, is_write ?
1357 DMA_DIRECTION_FROM_DEVICE :
1358 DMA_DIRECTION_TO_DEVICE);
8059feee 1359 if (!sg[i].iov_base) {
1a285899 1360 error_report("virtio: error trying to map MMIO memory");
42fb2e07
KW
1361 exit(1);
1362 }
3b3b0628
PB
1363 if (len != sg[i].iov_len) {
1364 error_report("virtio: unexpected memory split");
8059feee
MT
1365 exit(1);
1366 }
42fb2e07
KW
1367 }
1368}
1369
8607f5c3 1370void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem)
8059feee 1371{
22953364
PMD
1372 virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, elem->in_num, true);
1373 virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, elem->out_num,
1374 false);
3724650d
PB
1375}
1376
bf91bd27 1377static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
3724650d
PB
1378{
1379 VirtQueueElement *elem;
1380 size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
1381 size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
1382 size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
1383 size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
1384 size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
1385 size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
1386
1387 assert(sz >= sizeof(VirtQueueElement));
1388 elem = g_malloc(out_sg_end);
b0ac429f 1389 trace_virtqueue_alloc_element(elem, sz, in_num, out_num);
3724650d
PB
1390 elem->out_num = out_num;
1391 elem->in_num = in_num;
1392 elem->in_addr = (void *)elem + in_addr_ofs;
1393 elem->out_addr = (void *)elem + out_addr_ofs;
1394 elem->in_sg = (void *)elem + in_sg_ofs;
1395 elem->out_sg = (void *)elem + out_sg_ofs;
1396 return elem;
8059feee
MT
1397}
1398
86044b24 1399static void *virtqueue_split_pop(VirtQueue *vq, size_t sz)
967f97fa 1400{
5774cf98 1401 unsigned int i, head, max;
991976f7 1402 VRingMemoryRegionCaches *caches;
5eba0404
PB
1403 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
1404 MemoryRegionCache *desc_cache;
1405 int64_t len;
cee3ca00 1406 VirtIODevice *vdev = vq->vdev;
9796d0ac 1407 VirtQueueElement *elem = NULL;
37ef70be 1408 unsigned out_num, in_num, elem_entries;
3b3b0628
PB
1409 hwaddr addr[VIRTQUEUE_MAX_SIZE];
1410 struct iovec iov[VIRTQUEUE_MAX_SIZE];
aa570d6f 1411 VRingDesc desc;
412e0e81 1412 int rc;
967f97fa 1413
b5f53d04 1414 RCU_READ_LOCK_GUARD();
97cd965c
PB
1415 if (virtio_queue_empty_rcu(vq)) {
1416 goto done;
51b19ebe 1417 }
be1fea9b
VM
1418 /* Needed after virtio_queue_empty(), see comment in
1419 * virtqueue_num_heads(). */
1420 smp_rmb();
967f97fa
AL
1421
1422 /* When we start there are none of either input nor output. */
37ef70be 1423 out_num = in_num = elem_entries = 0;
967f97fa 1424
5774cf98
MM
1425 max = vq->vring.num;
1426
afd9096e 1427 if (vq->inuse >= vq->vring.num) {
ec55da19 1428 virtio_error(vdev, "Virtqueue size exceeded");
97cd965c 1429 goto done;
afd9096e
SH
1430 }
1431
fb1131b6 1432 if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
97cd965c 1433 goto done;
fb1131b6
SH
1434 }
1435
95129d6f 1436 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
e9600c6c 1437 vring_set_avail_event(vq, vq->last_avail_idx);
bcbabae8 1438 }
efeea6d0 1439
fb1131b6 1440 i = head;
9796d0ac 1441
e0e2d644 1442 caches = vring_get_region_caches(vq);
abdd16f4
SH
1443 if (!caches) {
1444 virtio_error(vdev, "Region caches not initialized");
1445 goto done;
1446 }
1447
991976f7 1448 if (caches->desc.len < max * sizeof(VRingDesc)) {
9796d0ac
PB
1449 virtio_error(vdev, "Cannot map descriptor ring");
1450 goto done;
1451 }
1452
991976f7 1453 desc_cache = &caches->desc;
86044b24 1454 vring_split_desc_read(vdev, &desc, desc_cache, i);
aa570d6f 1455 if (desc.flags & VRING_DESC_F_INDIRECT) {
74231929 1456 if (!desc.len || (desc.len % sizeof(VRingDesc))) {
ec55da19 1457 virtio_error(vdev, "Invalid size for indirect buffer table");
9796d0ac 1458 goto done;
efeea6d0
MM
1459 }
1460
1461 /* loop over the indirect descriptor table */
5eba0404
PB
1462 len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
1463 desc.addr, desc.len, false);
1464 desc_cache = &indirect_desc_cache;
9796d0ac
PB
1465 if (len < desc.len) {
1466 virtio_error(vdev, "Cannot map indirect buffer");
1467 goto done;
1468 }
1469
aa570d6f 1470 max = desc.len / sizeof(VRingDesc);
efeea6d0 1471 i = 0;
86044b24 1472 vring_split_desc_read(vdev, &desc, desc_cache, i);
efeea6d0
MM
1473 }
1474
42fb2e07 1475 /* Collect all the descriptors */
967f97fa 1476 do {
ec55da19
SH
1477 bool map_ok;
1478
aa570d6f 1479 if (desc.flags & VRING_DESC_F_WRITE) {
ec55da19
SH
1480 map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
1481 iov + out_num,
1482 VIRTQUEUE_MAX_SIZE - out_num, true,
1483 desc.addr, desc.len);
42fb2e07 1484 } else {
3b3b0628 1485 if (in_num) {
ec55da19
SH
1486 virtio_error(vdev, "Incorrect order for descriptors");
1487 goto err_undo_map;
c8eac1cf 1488 }
ec55da19
SH
1489 map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
1490 VIRTQUEUE_MAX_SIZE, false,
1491 desc.addr, desc.len);
1492 }
1493 if (!map_ok) {
1494 goto err_undo_map;
42fb2e07 1495 }
967f97fa 1496
967f97fa 1497 /* If we've got too many, that implies a descriptor loop. */
37ef70be 1498 if (++elem_entries > max) {
ec55da19
SH
1499 virtio_error(vdev, "Looped descriptor");
1500 goto err_undo_map;
bb6834cf 1501 }
412e0e81 1502
86044b24 1503 rc = virtqueue_split_read_next_desc(vdev, &desc, desc_cache, max, &i);
412e0e81
SH
1504 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1505
1506 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
1507 goto err_undo_map;
1508 }
967f97fa 1509
3b3b0628
PB
1510 /* Now copy what we have collected and mapped */
1511 elem = virtqueue_alloc_element(sz, out_num, in_num);
967f97fa 1512 elem->index = head;
86044b24 1513 elem->ndescs = 1;
3b3b0628
PB
1514 for (i = 0; i < out_num; i++) {
1515 elem->out_addr[i] = addr[i];
1516 elem->out_sg[i] = iov[i];
1517 }
1518 for (i = 0; i < in_num; i++) {
1519 elem->in_addr[i] = addr[out_num + i];
1520 elem->in_sg[i] = iov[out_num + i];
1521 }
967f97fa
AL
1522
1523 vq->inuse++;
1524
64979a4d 1525 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
9796d0ac 1526done:
5eba0404 1527 address_space_cache_destroy(&indirect_desc_cache);
9796d0ac 1528
51b19ebe 1529 return elem;
ec55da19
SH
1530
1531err_undo_map:
1532 virtqueue_undo_map_desc(out_num, in_num, iov);
9796d0ac 1533 goto done;
967f97fa
AL
1534}
1535
86044b24
JW
1536static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz)
1537{
1538 unsigned int i, max;
1539 VRingMemoryRegionCaches *caches;
1540 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
1541 MemoryRegionCache *desc_cache;
1542 int64_t len;
1543 VirtIODevice *vdev = vq->vdev;
1544 VirtQueueElement *elem = NULL;
1545 unsigned out_num, in_num, elem_entries;
1546 hwaddr addr[VIRTQUEUE_MAX_SIZE];
1547 struct iovec iov[VIRTQUEUE_MAX_SIZE];
1548 VRingPackedDesc desc;
1549 uint16_t id;
1550 int rc;
1551
b5f53d04 1552 RCU_READ_LOCK_GUARD();
86044b24
JW
1553 if (virtio_queue_packed_empty_rcu(vq)) {
1554 goto done;
1555 }
1556
1557 /* When we start there are none of either input nor output. */
1558 out_num = in_num = elem_entries = 0;
1559
1560 max = vq->vring.num;
1561
1562 if (vq->inuse >= vq->vring.num) {
1563 virtio_error(vdev, "Virtqueue size exceeded");
1564 goto done;
1565 }
1566
1567 i = vq->last_avail_idx;
1568
1569 caches = vring_get_region_caches(vq);
abdd16f4
SH
1570 if (!caches) {
1571 virtio_error(vdev, "Region caches not initialized");
1572 goto done;
1573 }
1574
86044b24
JW
1575 if (caches->desc.len < max * sizeof(VRingDesc)) {
1576 virtio_error(vdev, "Cannot map descriptor ring");
1577 goto done;
1578 }
1579
1580 desc_cache = &caches->desc;
1581 vring_packed_desc_read(vdev, &desc, desc_cache, i, true);
1582 id = desc.id;
1583 if (desc.flags & VRING_DESC_F_INDIRECT) {
1584 if (desc.len % sizeof(VRingPackedDesc)) {
1585 virtio_error(vdev, "Invalid size for indirect buffer table");
1586 goto done;
1587 }
1588
1589 /* loop over the indirect descriptor table */
1590 len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
1591 desc.addr, desc.len, false);
1592 desc_cache = &indirect_desc_cache;
1593 if (len < desc.len) {
1594 virtio_error(vdev, "Cannot map indirect buffer");
1595 goto done;
1596 }
1597
1598 max = desc.len / sizeof(VRingPackedDesc);
1599 i = 0;
1600 vring_packed_desc_read(vdev, &desc, desc_cache, i, false);
1601 }
1602
1603 /* Collect all the descriptors */
1604 do {
1605 bool map_ok;
1606
1607 if (desc.flags & VRING_DESC_F_WRITE) {
1608 map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
1609 iov + out_num,
1610 VIRTQUEUE_MAX_SIZE - out_num, true,
1611 desc.addr, desc.len);
1612 } else {
1613 if (in_num) {
1614 virtio_error(vdev, "Incorrect order for descriptors");
1615 goto err_undo_map;
1616 }
1617 map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
1618 VIRTQUEUE_MAX_SIZE, false,
1619 desc.addr, desc.len);
1620 }
1621 if (!map_ok) {
1622 goto err_undo_map;
1623 }
1624
1625 /* If we've got too many, that implies a descriptor loop. */
1626 if (++elem_entries > max) {
1627 virtio_error(vdev, "Looped descriptor");
1628 goto err_undo_map;
1629 }
1630
1631 rc = virtqueue_packed_read_next_desc(vq, &desc, desc_cache, max, &i,
1632 desc_cache ==
1633 &indirect_desc_cache);
1634 } while (rc == VIRTQUEUE_READ_DESC_MORE);
1635
1636 /* Now copy what we have collected and mapped */
1637 elem = virtqueue_alloc_element(sz, out_num, in_num);
1638 for (i = 0; i < out_num; i++) {
1639 elem->out_addr[i] = addr[i];
1640 elem->out_sg[i] = iov[i];
1641 }
1642 for (i = 0; i < in_num; i++) {
1643 elem->in_addr[i] = addr[out_num + i];
1644 elem->in_sg[i] = iov[out_num + i];
1645 }
1646
1647 elem->index = id;
1648 elem->ndescs = (desc_cache == &indirect_desc_cache) ? 1 : elem_entries;
1649 vq->last_avail_idx += elem->ndescs;
1650 vq->inuse += elem->ndescs;
1651
1652 if (vq->last_avail_idx >= vq->vring.num) {
1653 vq->last_avail_idx -= vq->vring.num;
1654 vq->last_avail_wrap_counter ^= 1;
1655 }
1656
1657 vq->shadow_avail_idx = vq->last_avail_idx;
1658 vq->shadow_avail_wrap_counter = vq->last_avail_wrap_counter;
1659
1660 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
1661done:
1662 address_space_cache_destroy(&indirect_desc_cache);
86044b24
JW
1663
1664 return elem;
1665
1666err_undo_map:
1667 virtqueue_undo_map_desc(out_num, in_num, iov);
1668 goto done;
1669}
1670
1671void *virtqueue_pop(VirtQueue *vq, size_t sz)
1672{
9d7bd082 1673 if (virtio_device_disabled(vq->vdev)) {
86044b24
JW
1674 return NULL;
1675 }
1676
1677 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) {
1678 return virtqueue_packed_pop(vq, sz);
1679 } else {
1680 return virtqueue_split_pop(vq, sz);
1681 }
1682}
1683
1684static unsigned int virtqueue_packed_drop_all(VirtQueue *vq)
54e17709 1685{
86044b24
JW
1686 VRingMemoryRegionCaches *caches;
1687 MemoryRegionCache *desc_cache;
54e17709
YB
1688 unsigned int dropped = 0;
1689 VirtQueueElement elem = {};
1690 VirtIODevice *vdev = vq->vdev;
86044b24 1691 VRingPackedDesc desc;
54e17709 1692
ab4dd274
PMD
1693 RCU_READ_LOCK_GUARD();
1694
86044b24 1695 caches = vring_get_region_caches(vq);
abdd16f4
SH
1696 if (!caches) {
1697 return 0;
1698 }
1699
86044b24
JW
1700 desc_cache = &caches->desc;
1701
1702 virtio_queue_set_notification(vq, 0);
1703
1704 while (vq->inuse < vq->vring.num) {
1705 unsigned int idx = vq->last_avail_idx;
1706 /*
1707 * works similar to virtqueue_pop but does not map buffers
1708 * and does not allocate any memory.
1709 */
1710 vring_packed_desc_read(vdev, &desc, desc_cache,
1711 vq->last_avail_idx , true);
1712 if (!is_desc_avail(desc.flags, vq->last_avail_wrap_counter)) {
1713 break;
1714 }
1715 elem.index = desc.id;
1716 elem.ndescs = 1;
1717 while (virtqueue_packed_read_next_desc(vq, &desc, desc_cache,
1718 vq->vring.num, &idx, false)) {
1719 ++elem.ndescs;
1720 }
1721 /*
1722 * immediately push the element, nothing to unmap
1723 * as both in_num and out_num are set to 0.
1724 */
1725 virtqueue_push(vq, &elem, 0);
1726 dropped++;
1727 vq->last_avail_idx += elem.ndescs;
1728 if (vq->last_avail_idx >= vq->vring.num) {
1729 vq->last_avail_idx -= vq->vring.num;
1730 vq->last_avail_wrap_counter ^= 1;
1731 }
54e17709
YB
1732 }
1733
86044b24
JW
1734 return dropped;
1735}
1736
1737static unsigned int virtqueue_split_drop_all(VirtQueue *vq)
1738{
1739 unsigned int dropped = 0;
1740 VirtQueueElement elem = {};
1741 VirtIODevice *vdev = vq->vdev;
1742 bool fEventIdx = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
1743
54e17709
YB
1744 while (!virtio_queue_empty(vq) && vq->inuse < vq->vring.num) {
1745 /* works similar to virtqueue_pop but does not map buffers
1746 * and does not allocate any memory */
1747 smp_rmb();
1748 if (!virtqueue_get_head(vq, vq->last_avail_idx, &elem.index)) {
1749 break;
1750 }
1751 vq->inuse++;
1752 vq->last_avail_idx++;
1753 if (fEventIdx) {
1754 vring_set_avail_event(vq, vq->last_avail_idx);
1755 }
1756 /* immediately push the element, nothing to unmap
1757 * as both in_num and out_num are set to 0 */
1758 virtqueue_push(vq, &elem, 0);
1759 dropped++;
1760 }
1761
1762 return dropped;
1763}
1764
86044b24
JW
1765/* virtqueue_drop_all:
1766 * @vq: The #VirtQueue
1767 * Drops all queued buffers and indicates them to the guest
1768 * as if they are done. Useful when buffers can not be
1769 * processed but must be returned to the guest.
1770 */
1771unsigned int virtqueue_drop_all(VirtQueue *vq)
1772{
1773 struct VirtIODevice *vdev = vq->vdev;
1774
9d7bd082 1775 if (virtio_device_disabled(vq->vdev)) {
86044b24
JW
1776 return 0;
1777 }
1778
1779 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
1780 return virtqueue_packed_drop_all(vq);
1781 } else {
1782 return virtqueue_split_drop_all(vq);
1783 }
1784}
1785
3724650d
PB
1786/* Reading and writing a structure directly to QEMUFile is *awful*, but
1787 * it is what QEMU has always done by mistake. We can change it sooner
1788 * or later by bumping the version number of the affected vm states.
1789 * In the meanwhile, since the in-memory layout of VirtQueueElement
1790 * has changed, we need to marshal to and from the layout that was
1791 * used before the change.
1792 */
1793typedef struct VirtQueueElementOld {
1794 unsigned int index;
1795 unsigned int out_num;
1796 unsigned int in_num;
1797 hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
1798 hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
1799 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
1800 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
1801} VirtQueueElementOld;
1802
8607f5c3 1803void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
ab281c17 1804{
3724650d
PB
1805 VirtQueueElement *elem;
1806 VirtQueueElementOld data;
1807 int i;
1808
1809 qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
1810
6bdc21c0
MT
1811 /* TODO: teach all callers that this can fail, and return failure instead
1812 * of asserting here.
262a69f4
EB
1813 * This is just one thing (there are probably more) that must be
1814 * fixed before we can allow NDEBUG compilation.
6bdc21c0 1815 */
6bdc21c0
MT
1816 assert(ARRAY_SIZE(data.in_addr) >= data.in_num);
1817 assert(ARRAY_SIZE(data.out_addr) >= data.out_num);
1818
3724650d
PB
1819 elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
1820 elem->index = data.index;
1821
1822 for (i = 0; i < elem->in_num; i++) {
1823 elem->in_addr[i] = data.in_addr[i];
1824 }
1825
1826 for (i = 0; i < elem->out_num; i++) {
1827 elem->out_addr[i] = data.out_addr[i];
1828 }
1829
1830 for (i = 0; i < elem->in_num; i++) {
1831 /* Base is overwritten by virtqueue_map. */
1832 elem->in_sg[i].iov_base = 0;
1833 elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
1834 }
1835
1836 for (i = 0; i < elem->out_num; i++) {
1837 /* Base is overwritten by virtqueue_map. */
1838 elem->out_sg[i].iov_base = 0;
1839 elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
1840 }
1841
86044b24
JW
1842 if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
1843 qemu_get_be32s(f, &elem->ndescs);
1844 }
1845
8607f5c3 1846 virtqueue_map(vdev, elem);
ab281c17
PB
1847 return elem;
1848}
1849
86044b24
JW
1850void qemu_put_virtqueue_element(VirtIODevice *vdev, QEMUFile *f,
1851 VirtQueueElement *elem)
ab281c17 1852{
3724650d
PB
1853 VirtQueueElementOld data;
1854 int i;
1855
1856 memset(&data, 0, sizeof(data));
1857 data.index = elem->index;
1858 data.in_num = elem->in_num;
1859 data.out_num = elem->out_num;
1860
1861 for (i = 0; i < elem->in_num; i++) {
1862 data.in_addr[i] = elem->in_addr[i];
1863 }
1864
1865 for (i = 0; i < elem->out_num; i++) {
1866 data.out_addr[i] = elem->out_addr[i];
1867 }
1868
1869 for (i = 0; i < elem->in_num; i++) {
1870 /* Base is overwritten by virtqueue_map when loading. Do not
1871 * save it, as it would leak the QEMU address space layout. */
1872 data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
1873 }
1874
1875 for (i = 0; i < elem->out_num; i++) {
1876 /* Do not save iov_base as above. */
1877 data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
1878 }
86044b24
JW
1879
1880 if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
1881 qemu_put_be32s(f, &elem->ndescs);
1882 }
1883
3724650d 1884 qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
ab281c17
PB
1885}
1886
967f97fa 1887/* virtio device */
7055e687
MT
1888static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
1889{
1c819449
FK
1890 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1891 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1892
9d7bd082 1893 if (virtio_device_disabled(vdev)) {
f5ed3663
SH
1894 return;
1895 }
1896
1c819449
FK
1897 if (k->notify) {
1898 k->notify(qbus->parent, vector);
7055e687
MT
1899 }
1900}
967f97fa 1901
53c25cea 1902void virtio_update_irq(VirtIODevice *vdev)
967f97fa 1903{
7055e687 1904 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
967f97fa
AL
1905}
1906
0b352fd6
CH
1907static int virtio_validate_features(VirtIODevice *vdev)
1908{
1909 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1910
8607f5c3
JW
1911 if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM) &&
1912 !virtio_vdev_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
1913 return -EFAULT;
1914 }
1915
0b352fd6
CH
1916 if (k->validate_features) {
1917 return k->validate_features(vdev);
1918 } else {
1919 return 0;
1920 }
1921}
1922
1923int virtio_set_status(VirtIODevice *vdev, uint8_t val)
4e1837f8 1924{
181103cd 1925 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
4e1837f8
SH
1926 trace_virtio_set_status(vdev, val);
1927
95129d6f 1928 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
0b352fd6
CH
1929 if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
1930 val & VIRTIO_CONFIG_S_FEATURES_OK) {
1931 int ret = virtio_validate_features(vdev);
1932
1933 if (ret) {
1934 return ret;
1935 }
1936 }
1937 }
e57f2c31 1938
4c5cf37b
XY
1939 if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) !=
1940 (val & VIRTIO_CONFIG_S_DRIVER_OK)) {
1941 virtio_set_started(vdev, val & VIRTIO_CONFIG_S_DRIVER_OK);
1942 }
badaf79c 1943
181103cd
FK
1944 if (k->set_status) {
1945 k->set_status(vdev, val);
4e1837f8
SH
1946 }
1947 vdev->status = val;
badaf79c 1948
0b352fd6 1949 return 0;
4e1837f8
SH
1950}
1951
616a6552
GK
1952static enum virtio_device_endian virtio_default_endian(void)
1953{
1954 if (target_words_bigendian()) {
1955 return VIRTIO_DEVICE_ENDIAN_BIG;
1956 } else {
1957 return VIRTIO_DEVICE_ENDIAN_LITTLE;
1958 }
1959}
1960
1961static enum virtio_device_endian virtio_current_cpu_endian(void)
1962{
cdba7e2f 1963 if (cpu_virtio_is_big_endian(current_cpu)) {
616a6552
GK
1964 return VIRTIO_DEVICE_ENDIAN_BIG;
1965 } else {
1966 return VIRTIO_DEVICE_ENDIAN_LITTLE;
1967 }
1968}
1969
53c25cea 1970void virtio_reset(void *opaque)
967f97fa
AL
1971{
1972 VirtIODevice *vdev = opaque;
181103cd 1973 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
1974 int i;
1975
e0c472d8 1976 virtio_set_status(vdev, 0);
616a6552
GK
1977 if (current_cpu) {
1978 /* Guest initiated reset */
1979 vdev->device_endian = virtio_current_cpu_endian();
1980 } else {
1981 /* System reset */
1982 vdev->device_endian = virtio_default_endian();
1983 }
e0c472d8 1984
181103cd
FK
1985 if (k->reset) {
1986 k->reset(vdev);
1987 }
967f97fa 1988
868a8f44 1989 vdev->start_on_kick = false;
badaf79c 1990 vdev->started = false;
f5ed3663 1991 vdev->broken = false;
704a76fc 1992 vdev->guest_features = 0;
967f97fa
AL
1993 vdev->queue_sel = 0;
1994 vdev->status = 0;
9d7bd082 1995 vdev->disabled = false;
d73415a3 1996 qatomic_set(&vdev->isr, 0);
7055e687
MT
1997 vdev->config_vector = VIRTIO_NO_VECTOR;
1998 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa 1999
87b3bd1c 2000 for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2001 vdev->vq[i].vring.desc = 0;
2002 vdev->vq[i].vring.avail = 0;
2003 vdev->vq[i].vring.used = 0;
2004 vdev->vq[i].last_avail_idx = 0;
be1fea9b 2005 vdev->vq[i].shadow_avail_idx = 0;
b796fcd1 2006 vdev->vq[i].used_idx = 0;
86044b24
JW
2007 vdev->vq[i].last_avail_wrap_counter = true;
2008 vdev->vq[i].shadow_avail_wrap_counter = true;
2009 vdev->vq[i].used_wrap_counter = true;
e0d686bf 2010 virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
bcbabae8
MT
2011 vdev->vq[i].signalled_used = 0;
2012 vdev->vq[i].signalled_used_valid = false;
332fa82d 2013 vdev->vq[i].notification = true;
46c5d082 2014 vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
4b7f91ed 2015 vdev->vq[i].inuse = 0;
e0e2d644 2016 virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
967f97fa
AL
2017 }
2018}
2019
53c25cea 2020uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
967f97fa 2021{
181103cd 2022 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2023 uint8_t val;
2024
5f5a1318 2025 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2026 return (uint32_t)-1;
5f5a1318
JW
2027 }
2028
2029 k->get_config(vdev, vdev->config);
967f97fa 2030
06dbfc6f 2031 val = ldub_p(vdev->config + addr);
967f97fa
AL
2032 return val;
2033}
2034
53c25cea 2035uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
967f97fa 2036{
181103cd 2037 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2038 uint16_t val;
2039
5f5a1318 2040 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2041 return (uint32_t)-1;
5f5a1318
JW
2042 }
2043
2044 k->get_config(vdev, vdev->config);
967f97fa 2045
06dbfc6f 2046 val = lduw_p(vdev->config + addr);
967f97fa
AL
2047 return val;
2048}
2049
53c25cea 2050uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
967f97fa 2051{
181103cd 2052 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2053 uint32_t val;
2054
5f5a1318 2055 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2056 return (uint32_t)-1;
5f5a1318
JW
2057 }
2058
2059 k->get_config(vdev, vdev->config);
967f97fa 2060
06dbfc6f 2061 val = ldl_p(vdev->config + addr);
967f97fa
AL
2062 return val;
2063}
2064
53c25cea 2065void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 2066{
181103cd 2067 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2068 uint8_t val = data;
2069
5f5a1318 2070 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2071 return;
5f5a1318 2072 }
967f97fa 2073
06dbfc6f 2074 stb_p(vdev->config + addr, val);
967f97fa 2075
181103cd
FK
2076 if (k->set_config) {
2077 k->set_config(vdev, vdev->config);
2078 }
967f97fa
AL
2079}
2080
53c25cea 2081void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 2082{
181103cd 2083 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2084 uint16_t val = data;
2085
5f5a1318 2086 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2087 return;
5f5a1318 2088 }
967f97fa 2089
06dbfc6f 2090 stw_p(vdev->config + addr, val);
967f97fa 2091
181103cd
FK
2092 if (k->set_config) {
2093 k->set_config(vdev, vdev->config);
2094 }
967f97fa
AL
2095}
2096
53c25cea 2097void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
967f97fa 2098{
181103cd 2099 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa
AL
2100 uint32_t val = data;
2101
5f5a1318 2102 if (addr + sizeof(val) > vdev->config_len) {
967f97fa 2103 return;
5f5a1318 2104 }
967f97fa 2105
06dbfc6f 2106 stl_p(vdev->config + addr, val);
967f97fa 2107
181103cd
FK
2108 if (k->set_config) {
2109 k->set_config(vdev, vdev->config);
2110 }
967f97fa
AL
2111}
2112
adfb743c
MT
2113uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
2114{
2115 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2116 uint8_t val;
2117
2118 if (addr + sizeof(val) > vdev->config_len) {
2119 return (uint32_t)-1;
2120 }
2121
2122 k->get_config(vdev, vdev->config);
2123
2124 val = ldub_p(vdev->config + addr);
2125 return val;
2126}
2127
2128uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
2129{
2130 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2131 uint16_t val;
2132
2133 if (addr + sizeof(val) > vdev->config_len) {
2134 return (uint32_t)-1;
2135 }
2136
2137 k->get_config(vdev, vdev->config);
2138
2139 val = lduw_le_p(vdev->config + addr);
2140 return val;
2141}
2142
2143uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
2144{
2145 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2146 uint32_t val;
2147
2148 if (addr + sizeof(val) > vdev->config_len) {
2149 return (uint32_t)-1;
2150 }
2151
2152 k->get_config(vdev, vdev->config);
2153
2154 val = ldl_le_p(vdev->config + addr);
2155 return val;
2156}
2157
2158void virtio_config_modern_writeb(VirtIODevice *vdev,
2159 uint32_t addr, uint32_t data)
2160{
2161 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2162 uint8_t val = data;
2163
2164 if (addr + sizeof(val) > vdev->config_len) {
2165 return;
2166 }
2167
2168 stb_p(vdev->config + addr, val);
2169
2170 if (k->set_config) {
2171 k->set_config(vdev, vdev->config);
2172 }
2173}
2174
2175void virtio_config_modern_writew(VirtIODevice *vdev,
2176 uint32_t addr, uint32_t data)
2177{
2178 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2179 uint16_t val = data;
2180
2181 if (addr + sizeof(val) > vdev->config_len) {
2182 return;
2183 }
2184
2185 stw_le_p(vdev->config + addr, val);
2186
2187 if (k->set_config) {
2188 k->set_config(vdev, vdev->config);
2189 }
2190}
2191
2192void virtio_config_modern_writel(VirtIODevice *vdev,
2193 uint32_t addr, uint32_t data)
2194{
2195 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
2196 uint32_t val = data;
2197
2198 if (addr + sizeof(val) > vdev->config_len) {
2199 return;
2200 }
2201
2202 stl_le_p(vdev->config + addr, val);
2203
2204 if (k->set_config) {
2205 k->set_config(vdev, vdev->config);
2206 }
2207}
2208
a8170e5e 2209void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
967f97fa 2210{
758ead31
PP
2211 if (!vdev->vq[n].vring.num) {
2212 return;
2213 }
ab223c95
CH
2214 vdev->vq[n].vring.desc = addr;
2215 virtio_queue_update_rings(vdev, n);
53c25cea
PB
2216}
2217
a8170e5e 2218hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
53c25cea 2219{
ab223c95
CH
2220 return vdev->vq[n].vring.desc;
2221}
2222
2223void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
2224 hwaddr avail, hwaddr used)
2225{
758ead31
PP
2226 if (!vdev->vq[n].vring.num) {
2227 return;
2228 }
ab223c95
CH
2229 vdev->vq[n].vring.desc = desc;
2230 vdev->vq[n].vring.avail = avail;
2231 vdev->vq[n].vring.used = used;
c611c764 2232 virtio_init_region_cache(vdev, n);
53c25cea
PB
2233}
2234
e63c0ba1
PM
2235void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
2236{
f6049f44
PM
2237 /* Don't allow guest to flip queue between existent and
2238 * nonexistent states, or to set it to an invalid size.
2239 */
2240 if (!!num != !!vdev->vq[n].vring.num ||
2241 num > VIRTQUEUE_MAX_SIZE ||
2242 num < 0) {
2243 return;
e63c0ba1 2244 }
f6049f44 2245 vdev->vq[n].vring.num = num;
e63c0ba1
PM
2246}
2247
e0d686bf
JW
2248VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
2249{
2250 return QLIST_FIRST(&vdev->vector_queues[vector]);
2251}
2252
2253VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
2254{
2255 return QLIST_NEXT(vq, node);
2256}
2257
53c25cea
PB
2258int virtio_queue_get_num(VirtIODevice *vdev, int n)
2259{
2260 return vdev->vq[n].vring.num;
2261}
967f97fa 2262
8c797e75
MT
2263int virtio_queue_get_max_num(VirtIODevice *vdev, int n)
2264{
2265 return vdev->vq[n].vring.num_default;
2266}
2267
8ad176aa
JW
2268int virtio_get_num_queues(VirtIODevice *vdev)
2269{
2270 int i;
2271
87b3bd1c 2272 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
8ad176aa
JW
2273 if (!virtio_queue_get_num(vdev, i)) {
2274 break;
2275 }
2276 }
2277
2278 return i;
2279}
2280
6ce69d1c
PM
2281void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
2282{
2283 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2284 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2285
ab223c95 2286 /* virtio-1 compliant devices cannot change the alignment */
95129d6f 2287 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
ab223c95
CH
2288 error_report("tried to modify queue alignment for virtio-1 device");
2289 return;
2290 }
6ce69d1c
PM
2291 /* Check that the transport told us it was going to do this
2292 * (so a buggy transport will immediately assert rather than
2293 * silently failing to migrate this state)
2294 */
2295 assert(k->has_variable_vring_alignment);
2296
758ead31
PP
2297 if (align) {
2298 vdev->vq[n].vring.align = align;
2299 virtio_queue_update_rings(vdev, n);
2300 }
6ce69d1c
PM
2301}
2302
07931698 2303static bool virtio_queue_notify_aio_vq(VirtQueue *vq)
344dc16f 2304{
badaf79c
XY
2305 bool ret = false;
2306
344dc16f
MT
2307 if (vq->vring.desc && vq->handle_aio_output) {
2308 VirtIODevice *vdev = vq->vdev;
2309
2310 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
badaf79c
XY
2311 ret = vq->handle_aio_output(vdev, vq);
2312
2313 if (unlikely(vdev->start_on_kick)) {
e57f2c31 2314 virtio_set_started(vdev, true);
badaf79c 2315 }
344dc16f 2316 }
07931698 2317
badaf79c 2318 return ret;
344dc16f
MT
2319}
2320
2b2cbcad 2321static void virtio_queue_notify_vq(VirtQueue *vq)
25db9ebe 2322{
9e0f5b81 2323 if (vq->vring.desc && vq->handle_output) {
25db9ebe 2324 VirtIODevice *vdev = vq->vdev;
9e0f5b81 2325
f5ed3663
SH
2326 if (unlikely(vdev->broken)) {
2327 return;
2328 }
2329
25db9ebe
SH
2330 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
2331 vq->handle_output(vdev, vq);
badaf79c
XY
2332
2333 if (unlikely(vdev->start_on_kick)) {
e57f2c31 2334 virtio_set_started(vdev, true);
badaf79c 2335 }
25db9ebe
SH
2336 }
2337}
2338
53c25cea
PB
2339void virtio_queue_notify(VirtIODevice *vdev, int n)
2340{
e49a6618
PB
2341 VirtQueue *vq = &vdev->vq[n];
2342
2343 if (unlikely(!vq->vring.desc || vdev->broken)) {
2344 return;
2345 }
2346
2347 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
fcccb271 2348 if (vq->host_notifier_enabled) {
e49a6618
PB
2349 event_notifier_set(&vq->host_notifier);
2350 } else if (vq->handle_output) {
2351 vq->handle_output(vdev, vq);
badaf79c 2352
8b04e2c7
XY
2353 if (unlikely(vdev->start_on_kick)) {
2354 virtio_set_started(vdev, true);
2355 }
badaf79c 2356 }
967f97fa
AL
2357}
2358
7055e687
MT
2359uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
2360{
87b3bd1c 2361 return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
7055e687
MT
2362 VIRTIO_NO_VECTOR;
2363}
2364
2365void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
2366{
e0d686bf
JW
2367 VirtQueue *vq = &vdev->vq[n];
2368
87b3bd1c 2369 if (n < VIRTIO_QUEUE_MAX) {
e0d686bf
JW
2370 if (vdev->vector_queues &&
2371 vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
2372 QLIST_REMOVE(vq, node);
2373 }
7055e687 2374 vdev->vq[n].vector = vector;
e0d686bf
JW
2375 if (vdev->vector_queues &&
2376 vector != VIRTIO_NO_VECTOR) {
2377 QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
2378 }
2379 }
7055e687
MT
2380}
2381
f1ac6a55
PB
2382VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
2383 VirtIOHandleOutput handle_output)
967f97fa
AL
2384{
2385 int i;
2386
87b3bd1c 2387 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2388 if (vdev->vq[i].vring.num == 0)
2389 break;
2390 }
2391
87b3bd1c 2392 if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
967f97fa
AL
2393 abort();
2394
2395 vdev->vq[i].vring.num = queue_size;
46c5d082 2396 vdev->vq[i].vring.num_default = queue_size;
6ce69d1c 2397 vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
967f97fa 2398 vdev->vq[i].handle_output = handle_output;
344dc16f 2399 vdev->vq[i].handle_aio_output = NULL;
86044b24
JW
2400 vdev->vq[i].used_elems = g_malloc0(sizeof(VirtQueueElement) *
2401 queue_size);
967f97fa
AL
2402
2403 return &vdev->vq[i];
2404}
2405
722f8c51
MT
2406void virtio_delete_queue(VirtQueue *vq)
2407{
2408 vq->vring.num = 0;
2409 vq->vring.num_default = 0;
2410 vq->handle_output = NULL;
2411 vq->handle_aio_output = NULL;
2412 g_free(vq->used_elems);
8cd353ea 2413 vq->used_elems = NULL;
421afd2f 2414 virtio_virtqueue_reset_region_cache(vq);
722f8c51
MT
2415}
2416
f23fd811
JW
2417void virtio_del_queue(VirtIODevice *vdev, int n)
2418{
87b3bd1c 2419 if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
f23fd811
JW
2420 abort();
2421 }
2422
722f8c51 2423 virtio_delete_queue(&vdev->vq[n]);
f23fd811
JW
2424}
2425
0687c37c
PB
2426static void virtio_set_isr(VirtIODevice *vdev, int value)
2427{
d73415a3 2428 uint8_t old = qatomic_read(&vdev->isr);
0687c37c
PB
2429
2430 /* Do not write ISR if it does not change, so that its cacheline remains
2431 * shared in the common case where the guest does not read it.
2432 */
2433 if ((old & value) != value) {
d73415a3 2434 qatomic_or(&vdev->isr, value);
0687c37c
PB
2435 }
2436}
2437
4c6dd9a0 2438/* Called within rcu_read_lock(). */
683f7665 2439static bool virtio_split_should_notify(VirtIODevice *vdev, VirtQueue *vq)
bcbabae8
MT
2440{
2441 uint16_t old, new;
2442 bool v;
a281ebc1
MT
2443 /* We need to expose used array entries before checking used event. */
2444 smp_mb();
97b83deb 2445 /* Always notify when queue is empty (when feature acknowledge) */
95129d6f 2446 if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
be1fea9b 2447 !vq->inuse && virtio_queue_empty(vq)) {
bcbabae8
MT
2448 return true;
2449 }
2450
95129d6f 2451 if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
bcbabae8
MT
2452 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
2453 }
2454
2455 v = vq->signalled_used_valid;
2456 vq->signalled_used_valid = true;
2457 old = vq->signalled_used;
b796fcd1 2458 new = vq->signalled_used = vq->used_idx;
e9600c6c 2459 return !v || vring_need_event(vring_get_used_event(vq), new, old);
bcbabae8
MT
2460}
2461
683f7665
JW
2462static bool vring_packed_need_event(VirtQueue *vq, bool wrap,
2463 uint16_t off_wrap, uint16_t new,
2464 uint16_t old)
2465{
2466 int off = off_wrap & ~(1 << 15);
2467
2468 if (wrap != off_wrap >> 15) {
2469 off -= vq->vring.num;
2470 }
2471
2472 return vring_need_event(off, new, old);
2473}
2474
4c6dd9a0 2475/* Called within rcu_read_lock(). */
683f7665
JW
2476static bool virtio_packed_should_notify(VirtIODevice *vdev, VirtQueue *vq)
2477{
2478 VRingPackedDescEvent e;
2479 uint16_t old, new;
2480 bool v;
2481 VRingMemoryRegionCaches *caches;
2482
2483 caches = vring_get_region_caches(vq);
abdd16f4
SH
2484 if (!caches) {
2485 return false;
2486 }
2487
683f7665
JW
2488 vring_packed_event_read(vdev, &caches->avail, &e);
2489
2490 old = vq->signalled_used;
2491 new = vq->signalled_used = vq->used_idx;
2492 v = vq->signalled_used_valid;
2493 vq->signalled_used_valid = true;
2494
2495 if (e.flags == VRING_PACKED_EVENT_FLAG_DISABLE) {
2496 return false;
2497 } else if (e.flags == VRING_PACKED_EVENT_FLAG_ENABLE) {
2498 return true;
2499 }
2500
2501 return !v || vring_packed_need_event(vq, vq->used_wrap_counter,
2502 e.off_wrap, new, old);
2503}
2504
2505/* Called within rcu_read_lock(). */
2506static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
2507{
2508 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
2509 return virtio_packed_should_notify(vdev, vq);
2510 } else {
2511 return virtio_split_should_notify(vdev, vq);
2512 }
2513}
2514
83d768b5
PB
2515void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
2516{
b5f53d04
DDAG
2517 WITH_RCU_READ_LOCK_GUARD() {
2518 if (!virtio_should_notify(vdev, vq)) {
2519 return;
2520 }
83d768b5
PB
2521 }
2522
2523 trace_virtio_notify_irqfd(vdev, vq);
2524
2525 /*
2526 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
2527 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
2528 * incorrectly polling this bit during crashdump and hibernation
2529 * in MSI mode, causing a hang if this bit is never updated.
2530 * Recent releases of Windows do not really shut down, but rather
2531 * log out and hibernate to make the next startup faster. Hence,
2532 * this manifested as a more serious hang during shutdown with
2533 *
2534 * Next driver release from 2016 fixed this problem, so working around it
2535 * is not a must, but it's easy to do so let's do it here.
2536 *
2537 * Note: it's safe to update ISR from any thread as it was switched
2538 * to an atomic operation.
2539 */
2540 virtio_set_isr(vq->vdev, 0x1);
2541 event_notifier_set(&vq->guest_notifier);
2542}
2543
b4b9862b
MT
2544static void virtio_irq(VirtQueue *vq)
2545{
2546 virtio_set_isr(vq->vdev, 0x1);
2547 virtio_notify_vector(vq->vdev, vq->vector);
2548}
2549
bcbabae8
MT
2550void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
2551{
b5f53d04
DDAG
2552 WITH_RCU_READ_LOCK_GUARD() {
2553 if (!virtio_should_notify(vdev, vq)) {
2554 return;
2555 }
bcbabae8 2556 }
967f97fa 2557
64979a4d 2558 trace_virtio_notify(vdev, vq);
b4b9862b 2559 virtio_irq(vq);
967f97fa
AL
2560}
2561
2562void virtio_notify_config(VirtIODevice *vdev)
2563{
7625162c
AL
2564 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
2565 return;
2566
0687c37c 2567 virtio_set_isr(vdev, 0x3);
b8f05908 2568 vdev->generation++;
7055e687 2569 virtio_notify_vector(vdev, vdev->config_vector);
967f97fa
AL
2570}
2571
616a6552
GK
2572static bool virtio_device_endian_needed(void *opaque)
2573{
2574 VirtIODevice *vdev = opaque;
2575
2576 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
95129d6f 2577 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3c185597
CH
2578 return vdev->device_endian != virtio_default_endian();
2579 }
2580 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
2581 return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
616a6552
GK
2582}
2583
019a3edb
GH
2584static bool virtio_64bit_features_needed(void *opaque)
2585{
2586 VirtIODevice *vdev = opaque;
2587
2588 return (vdev->host_features >> 32) != 0;
2589}
2590
74aae7b2
JW
2591static bool virtio_virtqueue_needed(void *opaque)
2592{
2593 VirtIODevice *vdev = opaque;
2594
2595 return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
2596}
2597
86044b24
JW
2598static bool virtio_packed_virtqueue_needed(void *opaque)
2599{
2600 VirtIODevice *vdev = opaque;
2601
2602 return virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED);
2603}
2604
46c5d082
CH
2605static bool virtio_ringsize_needed(void *opaque)
2606{
2607 VirtIODevice *vdev = opaque;
2608 int i;
2609
2610 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2611 if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
2612 return true;
2613 }
2614 }
2615 return false;
2616}
2617
a6df8adf
JW
2618static bool virtio_extra_state_needed(void *opaque)
2619{
2620 VirtIODevice *vdev = opaque;
2621 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2622 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2623
2624 return k->has_extra_state &&
2625 k->has_extra_state(qbus->parent);
2626}
2627
791b1daf
SH
2628static bool virtio_broken_needed(void *opaque)
2629{
2630 VirtIODevice *vdev = opaque;
2631
2632 return vdev->broken;
2633}
2634
badaf79c
XY
2635static bool virtio_started_needed(void *opaque)
2636{
2637 VirtIODevice *vdev = opaque;
2638
2639 return vdev->started;
2640}
2641
9d7bd082
MR
2642static bool virtio_disabled_needed(void *opaque)
2643{
2644 VirtIODevice *vdev = opaque;
2645
2646 return vdev->disabled;
2647}
2648
50e5ae4d 2649static const VMStateDescription vmstate_virtqueue = {
74aae7b2 2650 .name = "virtqueue_state",
50e5ae4d
DDAG
2651 .version_id = 1,
2652 .minimum_version_id = 1,
2653 .fields = (VMStateField[]) {
2654 VMSTATE_UINT64(vring.avail, struct VirtQueue),
2655 VMSTATE_UINT64(vring.used, struct VirtQueue),
2656 VMSTATE_END_OF_LIST()
2657 }
74aae7b2
JW
2658};
2659
86044b24
JW
2660static const VMStateDescription vmstate_packed_virtqueue = {
2661 .name = "packed_virtqueue_state",
2662 .version_id = 1,
2663 .minimum_version_id = 1,
2664 .fields = (VMStateField[]) {
2665 VMSTATE_UINT16(last_avail_idx, struct VirtQueue),
2666 VMSTATE_BOOL(last_avail_wrap_counter, struct VirtQueue),
2667 VMSTATE_UINT16(used_idx, struct VirtQueue),
2668 VMSTATE_BOOL(used_wrap_counter, struct VirtQueue),
2669 VMSTATE_UINT32(inuse, struct VirtQueue),
2670 VMSTATE_END_OF_LIST()
2671 }
2672};
2673
74aae7b2
JW
2674static const VMStateDescription vmstate_virtio_virtqueues = {
2675 .name = "virtio/virtqueues",
2676 .version_id = 1,
2677 .minimum_version_id = 1,
2678 .needed = &virtio_virtqueue_needed,
2679 .fields = (VMStateField[]) {
3e996cc5
DDAG
2680 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
2681 VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
74aae7b2
JW
2682 VMSTATE_END_OF_LIST()
2683 }
2684};
2685
86044b24
JW
2686static const VMStateDescription vmstate_virtio_packed_virtqueues = {
2687 .name = "virtio/packed_virtqueues",
2688 .version_id = 1,
2689 .minimum_version_id = 1,
2690 .needed = &virtio_packed_virtqueue_needed,
2691 .fields = (VMStateField[]) {
2692 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
2693 VIRTIO_QUEUE_MAX, 0, vmstate_packed_virtqueue, VirtQueue),
2694 VMSTATE_END_OF_LIST()
2695 }
2696};
2697
50e5ae4d 2698static const VMStateDescription vmstate_ringsize = {
46c5d082 2699 .name = "ringsize_state",
50e5ae4d
DDAG
2700 .version_id = 1,
2701 .minimum_version_id = 1,
2702 .fields = (VMStateField[]) {
2703 VMSTATE_UINT32(vring.num_default, struct VirtQueue),
2704 VMSTATE_END_OF_LIST()
2705 }
46c5d082
CH
2706};
2707
2708static const VMStateDescription vmstate_virtio_ringsize = {
2709 .name = "virtio/ringsize",
2710 .version_id = 1,
2711 .minimum_version_id = 1,
2712 .needed = &virtio_ringsize_needed,
2713 .fields = (VMStateField[]) {
3e996cc5
DDAG
2714 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
2715 VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
46c5d082
CH
2716 VMSTATE_END_OF_LIST()
2717 }
2718};
2719
2c21ee76 2720static int get_extra_state(QEMUFile *f, void *pv, size_t size,
03fee66f 2721 const VMStateField *field)
a6df8adf
JW
2722{
2723 VirtIODevice *vdev = pv;
2724 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2725 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2726
2727 if (!k->load_extra_state) {
2728 return -1;
2729 } else {
2730 return k->load_extra_state(qbus->parent, f);
2731 }
2732}
2733
2c21ee76 2734static int put_extra_state(QEMUFile *f, void *pv, size_t size,
3ddba9a9 2735 const VMStateField *field, JSONWriter *vmdesc)
a6df8adf
JW
2736{
2737 VirtIODevice *vdev = pv;
2738 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2739 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2740
2741 k->save_extra_state(qbus->parent, f);
2c21ee76 2742 return 0;
a6df8adf
JW
2743}
2744
2745static const VMStateInfo vmstate_info_extra_state = {
2746 .name = "virtqueue_extra_state",
2747 .get = get_extra_state,
2748 .put = put_extra_state,
2749};
2750
2751static const VMStateDescription vmstate_virtio_extra_state = {
2752 .name = "virtio/extra_state",
2753 .version_id = 1,
2754 .minimum_version_id = 1,
2755 .needed = &virtio_extra_state_needed,
2756 .fields = (VMStateField[]) {
2757 {
2758 .name = "extra_state",
2759 .version_id = 0,
2760 .field_exists = NULL,
2761 .size = 0,
2762 .info = &vmstate_info_extra_state,
2763 .flags = VMS_SINGLE,
2764 .offset = 0,
2765 },
2766 VMSTATE_END_OF_LIST()
2767 }
2768};
2769
616a6552
GK
2770static const VMStateDescription vmstate_virtio_device_endian = {
2771 .name = "virtio/device_endian",
2772 .version_id = 1,
2773 .minimum_version_id = 1,
5cd8cada 2774 .needed = &virtio_device_endian_needed,
616a6552
GK
2775 .fields = (VMStateField[]) {
2776 VMSTATE_UINT8(device_endian, VirtIODevice),
2777 VMSTATE_END_OF_LIST()
2778 }
2779};
2780
019a3edb
GH
2781static const VMStateDescription vmstate_virtio_64bit_features = {
2782 .name = "virtio/64bit_features",
2783 .version_id = 1,
2784 .minimum_version_id = 1,
5cd8cada 2785 .needed = &virtio_64bit_features_needed,
019a3edb
GH
2786 .fields = (VMStateField[]) {
2787 VMSTATE_UINT64(guest_features, VirtIODevice),
2788 VMSTATE_END_OF_LIST()
2789 }
2790};
2791
791b1daf
SH
2792static const VMStateDescription vmstate_virtio_broken = {
2793 .name = "virtio/broken",
2794 .version_id = 1,
2795 .minimum_version_id = 1,
2796 .needed = &virtio_broken_needed,
2797 .fields = (VMStateField[]) {
2798 VMSTATE_BOOL(broken, VirtIODevice),
2799 VMSTATE_END_OF_LIST()
2800 }
2801};
2802
badaf79c
XY
2803static const VMStateDescription vmstate_virtio_started = {
2804 .name = "virtio/started",
2805 .version_id = 1,
2806 .minimum_version_id = 1,
2807 .needed = &virtio_started_needed,
2808 .fields = (VMStateField[]) {
2809 VMSTATE_BOOL(started, VirtIODevice),
2810 VMSTATE_END_OF_LIST()
2811 }
2812};
2813
9d7bd082
MR
2814static const VMStateDescription vmstate_virtio_disabled = {
2815 .name = "virtio/disabled",
2816 .version_id = 1,
2817 .minimum_version_id = 1,
2818 .needed = &virtio_disabled_needed,
2819 .fields = (VMStateField[]) {
2820 VMSTATE_BOOL(disabled, VirtIODevice),
2821 VMSTATE_END_OF_LIST()
2822 }
2823};
2824
6b321a3d
GK
2825static const VMStateDescription vmstate_virtio = {
2826 .name = "virtio",
2827 .version_id = 1,
2828 .minimum_version_id = 1,
2829 .minimum_version_id_old = 1,
2830 .fields = (VMStateField[]) {
2831 VMSTATE_END_OF_LIST()
616a6552 2832 },
5cd8cada
JQ
2833 .subsections = (const VMStateDescription*[]) {
2834 &vmstate_virtio_device_endian,
2835 &vmstate_virtio_64bit_features,
74aae7b2 2836 &vmstate_virtio_virtqueues,
46c5d082 2837 &vmstate_virtio_ringsize,
791b1daf 2838 &vmstate_virtio_broken,
a6df8adf 2839 &vmstate_virtio_extra_state,
badaf79c 2840 &vmstate_virtio_started,
86044b24 2841 &vmstate_virtio_packed_virtqueues,
9d7bd082 2842 &vmstate_virtio_disabled,
5cd8cada 2843 NULL
6b321a3d
GK
2844 }
2845};
2846
2f168d07 2847int virtio_save(VirtIODevice *vdev, QEMUFile *f)
967f97fa 2848{
1c819449
FK
2849 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2850 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 2851 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
019a3edb 2852 uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
967f97fa
AL
2853 int i;
2854
1c819449
FK
2855 if (k->save_config) {
2856 k->save_config(qbus->parent, f);
2857 }
967f97fa 2858
967f97fa
AL
2859 qemu_put_8s(f, &vdev->status);
2860 qemu_put_8s(f, &vdev->isr);
2861 qemu_put_be16s(f, &vdev->queue_sel);
019a3edb 2862 qemu_put_be32s(f, &guest_features_lo);
967f97fa
AL
2863 qemu_put_be32(f, vdev->config_len);
2864 qemu_put_buffer(f, vdev->config, vdev->config_len);
2865
87b3bd1c 2866 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2867 if (vdev->vq[i].vring.num == 0)
2868 break;
2869 }
2870
2871 qemu_put_be32(f, i);
2872
87b3bd1c 2873 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
967f97fa
AL
2874 if (vdev->vq[i].vring.num == 0)
2875 break;
2876
2877 qemu_put_be32(f, vdev->vq[i].vring.num);
6ce69d1c
PM
2878 if (k->has_variable_vring_alignment) {
2879 qemu_put_be32(f, vdev->vq[i].vring.align);
2880 }
874adf45
SH
2881 /*
2882 * Save desc now, the rest of the ring addresses are saved in
2883 * subsections for VIRTIO-1 devices.
2884 */
ab223c95 2885 qemu_put_be64(f, vdev->vq[i].vring.desc);
967f97fa 2886 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1c819449
FK
2887 if (k->save_queue) {
2888 k->save_queue(qbus->parent, i, f);
2889 }
967f97fa 2890 }
1b5fc0de
GK
2891
2892 if (vdc->save != NULL) {
2893 vdc->save(vdev, f);
2894 }
6b321a3d 2895
ea43e259 2896 if (vdc->vmsd) {
2f168d07
DDAG
2897 int ret = vmstate_save_state(f, vdc->vmsd, vdev, NULL);
2898 if (ret) {
2899 return ret;
2900 }
ea43e259
DDAG
2901 }
2902
6b321a3d 2903 /* Subsections */
2f168d07 2904 return vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
967f97fa
AL
2905}
2906
1a665855 2907/* A wrapper for use as a VMState .put function */
2c21ee76 2908static int virtio_device_put(QEMUFile *f, void *opaque, size_t size,
3ddba9a9 2909 const VMStateField *field, JSONWriter *vmdesc)
1a665855 2910{
2f168d07 2911 return virtio_save(VIRTIO_DEVICE(opaque), f);
1a665855
HP
2912}
2913
2914/* A wrapper for use as a VMState .get function */
2c21ee76 2915static int virtio_device_get(QEMUFile *f, void *opaque, size_t size,
03fee66f 2916 const VMStateField *field)
1a665855
HP
2917{
2918 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
2919 DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
2920
2921 return virtio_load(vdev, f, dc->vmsd->version_id);
2922}
2923
2924const VMStateInfo virtio_vmstate_info = {
2925 .name = "virtio",
2926 .get = virtio_device_get,
2927 .put = virtio_device_put,
2928};
2929
6c0196d7 2930static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
ad0c9332 2931{
181103cd 2932 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
6b8f1020 2933 bool bad = (val & ~(vdev->host_features)) != 0;
ad0c9332 2934
6b8f1020 2935 val &= vdev->host_features;
181103cd
FK
2936 if (k->set_features) {
2937 k->set_features(vdev, val);
ad0c9332
PB
2938 }
2939 vdev->guest_features = val;
2940 return bad ? -1 : 0;
2941}
2942
6c0196d7
CH
2943int virtio_set_features(VirtIODevice *vdev, uint64_t val)
2944{
db812c40
PB
2945 int ret;
2946 /*
6c0196d7
CH
2947 * The driver must not attempt to set features after feature negotiation
2948 * has finished.
2949 */
2950 if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
2951 return -EINVAL;
2952 }
db812c40 2953 ret = virtio_set_features_nocheck(vdev, val);
2d69eba5
LQ
2954 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
2955 /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */
2956 int i;
2957 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2958 if (vdev->vq[i].vring.num != 0) {
2959 virtio_init_region_cache(vdev, i);
db812c40
PB
2960 }
2961 }
2d69eba5
LQ
2962 }
2963 if (!ret) {
868a8f44
XY
2964 if (!virtio_device_started(vdev, vdev->status) &&
2965 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2966 vdev->start_on_kick = true;
2967 }
db812c40
PB
2968 }
2969 return ret;
6c0196d7
CH
2970}
2971
4c21e353 2972size_t virtio_feature_get_config_size(const VirtIOFeature *feature_sizes,
ba550851
SG
2973 uint64_t host_features)
2974{
2975 size_t config_size = 0;
2976 int i;
2977
2978 for (i = 0; feature_sizes[i].flags != 0; i++) {
2979 if (host_features & feature_sizes[i].flags) {
2980 config_size = MAX(feature_sizes[i].end, config_size);
2981 }
2982 }
2983
2984 return config_size;
2985}
2986
1b5fc0de 2987int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
967f97fa 2988{
cc459952 2989 int i, ret;
a890a2f9 2990 int32_t config_len;
cc459952 2991 uint32_t num;
6d74ca5a 2992 uint32_t features;
1c819449
FK
2993 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2994 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1b5fc0de 2995 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
967f97fa 2996
616a6552
GK
2997 /*
2998 * We poison the endianness to ensure it does not get used before
2999 * subsections have been loaded.
3000 */
3001 vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
3002
1c819449
FK
3003 if (k->load_config) {
3004 ret = k->load_config(qbus->parent, f);
ff24bd58
MT
3005 if (ret)
3006 return ret;
3007 }
967f97fa 3008
967f97fa
AL
3009 qemu_get_8s(f, &vdev->status);
3010 qemu_get_8s(f, &vdev->isr);
3011 qemu_get_be16s(f, &vdev->queue_sel);
87b3bd1c 3012 if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
4b53c2c7
MR
3013 return -1;
3014 }
6d74ca5a 3015 qemu_get_be32s(f, &features);
ad0c9332 3016
62cee1a2
MT
3017 /*
3018 * Temporarily set guest_features low bits - needed by
3019 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
3020 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
3021 *
3022 * Note: devices should always test host features in future - don't create
3023 * new dependencies like this.
3024 */
3025 vdev->guest_features = features;
3026
a890a2f9 3027 config_len = qemu_get_be32(f);
2f5732e9
DDAG
3028
3029 /*
3030 * There are cases where the incoming config can be bigger or smaller
3031 * than what we have; so load what we have space for, and skip
3032 * any excess that's in the stream.
3033 */
3034 qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
3035
3036 while (config_len > vdev->config_len) {
3037 qemu_get_byte(f);
3038 config_len--;
a890a2f9 3039 }
967f97fa
AL
3040
3041 num = qemu_get_be32(f);
3042
87b3bd1c 3043 if (num > VIRTIO_QUEUE_MAX) {
8a1be662 3044 error_report("Invalid number of virtqueues: 0x%x", num);
cc459952
MT
3045 return -1;
3046 }
3047
967f97fa
AL
3048 for (i = 0; i < num; i++) {
3049 vdev->vq[i].vring.num = qemu_get_be32(f);
6ce69d1c
PM
3050 if (k->has_variable_vring_alignment) {
3051 vdev->vq[i].vring.align = qemu_get_be32(f);
3052 }
ab223c95 3053 vdev->vq[i].vring.desc = qemu_get_be64(f);
967f97fa 3054 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
bcbabae8 3055 vdev->vq[i].signalled_used_valid = false;
332fa82d 3056 vdev->vq[i].notification = true;
967f97fa 3057
874adf45 3058 if (!vdev->vq[i].vring.desc && vdev->vq[i].last_avail_idx) {
1abeb5a6 3059 error_report("VQ %d address 0x0 "
6daf194d 3060 "inconsistent with Host index 0x%x",
1abeb5a6 3061 i, vdev->vq[i].last_avail_idx);
874adf45 3062 return -1;
8275e2f6 3063 }
1c819449
FK
3064 if (k->load_queue) {
3065 ret = k->load_queue(qbus->parent, i, f);
ff24bd58
MT
3066 if (ret)
3067 return ret;
7055e687 3068 }
967f97fa
AL
3069 }
3070
7055e687 3071 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
1b5fc0de
GK
3072
3073 if (vdc->load != NULL) {
6b321a3d
GK
3074 ret = vdc->load(vdev, f, version_id);
3075 if (ret) {
3076 return ret;
3077 }
1b5fc0de
GK
3078 }
3079
ea43e259
DDAG
3080 if (vdc->vmsd) {
3081 ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id);
3082 if (ret) {
3083 return ret;
3084 }
3085 }
3086
616a6552
GK
3087 /* Subsections */
3088 ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
3089 if (ret) {
3090 return ret;
3091 }
3092
3093 if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
3094 vdev->device_endian = virtio_default_endian();
3095 }
3096
019a3edb
GH
3097 if (virtio_64bit_features_needed(vdev)) {
3098 /*
3099 * Subsection load filled vdev->guest_features. Run them
3100 * through virtio_set_features to sanity-check them against
3101 * host_features.
3102 */
3103 uint64_t features64 = vdev->guest_features;
6c0196d7 3104 if (virtio_set_features_nocheck(vdev, features64) < 0) {
019a3edb
GH
3105 error_report("Features 0x%" PRIx64 " unsupported. "
3106 "Allowed features: 0x%" PRIx64,
3107 features64, vdev->host_features);
3108 return -1;
3109 }
3110 } else {
6c0196d7 3111 if (virtio_set_features_nocheck(vdev, features) < 0) {
019a3edb
GH
3112 error_report("Features 0x%x unsupported. "
3113 "Allowed features: 0x%" PRIx64,
3114 features, vdev->host_features);
3115 return -1;
3116 }
3117 }
3118
868a8f44
XY
3119 if (!virtio_device_started(vdev, vdev->status) &&
3120 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3121 vdev->start_on_kick = true;
3122 }
3123
b5f53d04 3124 RCU_READ_LOCK_GUARD();
616a6552 3125 for (i = 0; i < num; i++) {
ab223c95 3126 if (vdev->vq[i].vring.desc) {
616a6552 3127 uint16_t nheads;
874adf45
SH
3128
3129 /*
3130 * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
3131 * only the region cache needs to be set up. Legacy devices need
3132 * to calculate used and avail ring addresses based on the desc
3133 * address.
3134 */
3135 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3136 virtio_init_region_cache(vdev, i);
3137 } else {
3138 virtio_queue_update_rings(vdev, i);
3139 }
3140
86044b24
JW
3141 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3142 vdev->vq[i].shadow_avail_idx = vdev->vq[i].last_avail_idx;
3143 vdev->vq[i].shadow_avail_wrap_counter =
3144 vdev->vq[i].last_avail_wrap_counter;
3145 continue;
3146 }
3147
616a6552
GK
3148 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
3149 /* Check it isn't doing strange things with descriptor numbers. */
3150 if (nheads > vdev->vq[i].vring.num) {
4aedda25
JL
3151 virtio_error(vdev, "VQ %d size 0x%x Guest index 0x%x "
3152 "inconsistent with Host index 0x%x: delta 0x%x",
3153 i, vdev->vq[i].vring.num,
3154 vring_avail_idx(&vdev->vq[i]),
3155 vdev->vq[i].last_avail_idx, nheads);
3156 vdev->vq[i].used_idx = 0;
3157 vdev->vq[i].shadow_avail_idx = 0;
3158 vdev->vq[i].inuse = 0;
3159 continue;
616a6552 3160 }
b796fcd1 3161 vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
be1fea9b 3162 vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
bccdef6b
SH
3163
3164 /*
3165 * Some devices migrate VirtQueueElements that have been popped
3166 * from the avail ring but not yet returned to the used ring.
e66bcc40
HP
3167 * Since max ring size < UINT16_MAX it's safe to use modulo
3168 * UINT16_MAX + 1 subtraction.
bccdef6b 3169 */
e66bcc40
HP
3170 vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
3171 vdev->vq[i].used_idx);
bccdef6b
SH
3172 if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
3173 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
3174 "used_idx 0x%x",
3175 i, vdev->vq[i].vring.num,
3176 vdev->vq[i].last_avail_idx,
3177 vdev->vq[i].used_idx);
3178 return -1;
3179 }
616a6552
GK
3180 }
3181 }
3182
1dd71383
MT
3183 if (vdc->post_load) {
3184 ret = vdc->post_load(vdev);
3185 if (ret) {
3186 return ret;
3187 }
3188 }
3189
616a6552 3190 return 0;
967f97fa
AL
3191}
3192
6a1a8cc7 3193void virtio_cleanup(VirtIODevice *vdev)
b946a153 3194{
85cf2a8d 3195 qemu_del_vm_change_state_handler(vdev->vmstate);
8e05db92
FK
3196}
3197
538f0497 3198static void virtio_vmstate_change(void *opaque, bool running, RunState state)
85cf2a8d
MT
3199{
3200 VirtIODevice *vdev = opaque;
1c819449
FK
3201 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3202 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
e57f2c31 3203 bool backend_run = running && virtio_device_started(vdev, vdev->status);
9e8e8c48 3204 vdev->vm_running = running;
85cf2a8d
MT
3205
3206 if (backend_run) {
3207 virtio_set_status(vdev, vdev->status);
3208 }
3209
1c819449
FK
3210 if (k->vmstate_change) {
3211 k->vmstate_change(qbus->parent, backend_run);
85cf2a8d
MT
3212 }
3213
3214 if (!backend_run) {
3215 virtio_set_status(vdev, vdev->status);
3216 }
3217}
3218
c8075caf
GA
3219void virtio_instance_init_common(Object *proxy_obj, void *data,
3220 size_t vdev_size, const char *vdev_name)
3221{
3222 DeviceState *vdev = data;
3223
9fc7fc4d
MA
3224 object_initialize_child_with_props(proxy_obj, "virtio-backend", vdev,
3225 vdev_size, vdev_name, &error_abort,
3226 NULL);
c8075caf
GA
3227 qdev_alias_all_properties(vdev, proxy_obj);
3228}
3229
8e05db92
FK
3230void virtio_init(VirtIODevice *vdev, const char *name,
3231 uint16_t device_id, size_t config_size)
967f97fa 3232{
e0d686bf
JW
3233 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3234 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
b8193adb 3235 int i;
e0d686bf
JW
3236 int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
3237
3238 if (nvectors) {
3239 vdev->vector_queues =
3240 g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
3241 }
3242
868a8f44 3243 vdev->start_on_kick = false;
badaf79c 3244 vdev->started = false;
53c25cea 3245 vdev->device_id = device_id;
967f97fa 3246 vdev->status = 0;
d73415a3 3247 qatomic_set(&vdev->isr, 0);
967f97fa 3248 vdev->queue_sel = 0;
7055e687 3249 vdev->config_vector = VIRTIO_NO_VECTOR;
87b3bd1c 3250 vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
1354869c 3251 vdev->vm_running = runstate_is_running();
f5ed3663 3252 vdev->broken = false;
87b3bd1c 3253 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
b8193adb 3254 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
1cbdabe2 3255 vdev->vq[i].vdev = vdev;
e78a2b42 3256 vdev->vq[i].queue_index = i;
fcccb271 3257 vdev->vq[i].host_notifier_enabled = false;
1cbdabe2 3258 }
967f97fa 3259
967f97fa
AL
3260 vdev->name = name;
3261 vdev->config_len = config_size;
8e05db92 3262 if (vdev->config_len) {
7267c094 3263 vdev->config = g_malloc0(config_size);
8e05db92 3264 } else {
967f97fa 3265 vdev->config = NULL;
8e05db92 3266 }
1a8c091c
SH
3267 vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev),
3268 virtio_vmstate_change, vdev);
616a6552 3269 vdev->device_endian = virtio_default_endian();
5669655a 3270 vdev->use_guest_notifier_mask = true;
8e05db92 3271}
967f97fa 3272
7c78bdd7
CH
3273/*
3274 * Only devices that have already been around prior to defining the virtio
3275 * standard support legacy mode; this includes devices not specified in the
3276 * standard. All newer devices conform to the virtio standard only.
3277 */
3278bool virtio_legacy_allowed(VirtIODevice *vdev)
3279{
3280 switch (vdev->device_id) {
3281 case VIRTIO_ID_NET:
3282 case VIRTIO_ID_BLOCK:
3283 case VIRTIO_ID_CONSOLE:
3284 case VIRTIO_ID_RNG:
3285 case VIRTIO_ID_BALLOON:
3286 case VIRTIO_ID_RPMSG:
3287 case VIRTIO_ID_SCSI:
3288 case VIRTIO_ID_9P:
3289 case VIRTIO_ID_RPROC_SERIAL:
3290 case VIRTIO_ID_CAIF:
3291 return true;
3292 default:
3293 return false;
3294 }
3295}
3296
d55f5182
SG
3297bool virtio_legacy_check_disabled(VirtIODevice *vdev)
3298{
3299 return vdev->disable_legacy_check;
3300}
3301
a8170e5e 3302hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
3303{
3304 return vdev->vq[n].vring.desc;
3305}
3306
0c9753eb
LV
3307bool virtio_queue_enabled_legacy(VirtIODevice *vdev, int n)
3308{
3309 return virtio_queue_get_desc_addr(vdev, n) != 0;
3310}
3311
23bfaf77
JW
3312bool virtio_queue_enabled(VirtIODevice *vdev, int n)
3313{
b2a5f62a
JW
3314 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3315 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
3316
3317 if (k->queue_enabled) {
3318 return k->queue_enabled(qbus->parent, n);
3319 }
0c9753eb 3320 return virtio_queue_enabled_legacy(vdev, n);
23bfaf77
JW
3321}
3322
a8170e5e 3323hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
3324{
3325 return vdev->vq[n].vring.avail;
3326}
3327
a8170e5e 3328hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
1cbdabe2
MT
3329{
3330 return vdev->vq[n].vring.used;
3331}
3332
a8170e5e 3333hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
1cbdabe2
MT
3334{
3335 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
3336}
3337
a8170e5e 3338hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
1cbdabe2 3339{
f90cda63
WX
3340 int s;
3341
86044b24
JW
3342 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3343 return sizeof(struct VRingPackedDescEvent);
3344 }
3345
f90cda63 3346 s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1cbdabe2 3347 return offsetof(VRingAvail, ring) +
f90cda63 3348 sizeof(uint16_t) * vdev->vq[n].vring.num + s;
1cbdabe2
MT
3349}
3350
a8170e5e 3351hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
1cbdabe2 3352{
f90cda63
WX
3353 int s;
3354
86044b24
JW
3355 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3356 return sizeof(struct VRingPackedDescEvent);
3357 }
3358
f90cda63 3359 s = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
1cbdabe2 3360 return offsetof(VRingUsed, ring) +
f90cda63 3361 sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s;
1cbdabe2
MT
3362}
3363
86044b24
JW
3364static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice *vdev,
3365 int n)
3366{
3367 unsigned int avail, used;
3368
3369 avail = vdev->vq[n].last_avail_idx;
3370 avail |= ((uint16_t)vdev->vq[n].last_avail_wrap_counter) << 15;
3371
3372 used = vdev->vq[n].used_idx;
3373 used |= ((uint16_t)vdev->vq[n].used_wrap_counter) << 15;
3374
3375 return avail | used << 16;
3376}
3377
3378static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice *vdev,
3379 int n)
1cbdabe2
MT
3380{
3381 return vdev->vq[n].last_avail_idx;
3382}
3383
86044b24 3384unsigned int virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
1cbdabe2 3385{
86044b24
JW
3386 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3387 return virtio_queue_packed_get_last_avail_idx(vdev, n);
3388 } else {
3389 return virtio_queue_split_get_last_avail_idx(vdev, n);
3390 }
1cbdabe2
MT
3391}
3392
86044b24
JW
3393static void virtio_queue_packed_set_last_avail_idx(VirtIODevice *vdev,
3394 int n, unsigned int idx)
3395{
3396 struct VirtQueue *vq = &vdev->vq[n];
3397
3398 vq->last_avail_idx = vq->shadow_avail_idx = idx & 0x7fff;
3399 vq->last_avail_wrap_counter =
3400 vq->shadow_avail_wrap_counter = !!(idx & 0x8000);
3401 idx >>= 16;
3402 vq->used_idx = idx & 0x7ffff;
3403 vq->used_wrap_counter = !!(idx & 0x8000);
3404}
3405
3406static void virtio_queue_split_set_last_avail_idx(VirtIODevice *vdev,
3407 int n, unsigned int idx)
3408{
3409 vdev->vq[n].last_avail_idx = idx;
3410 vdev->vq[n].shadow_avail_idx = idx;
3411}
3412
3413void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n,
3414 unsigned int idx)
3415{
3416 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3417 virtio_queue_packed_set_last_avail_idx(vdev, n, idx);
3418 } else {
3419 virtio_queue_split_set_last_avail_idx(vdev, n, idx);
3420 }
3421}
3422
3423static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice *vdev,
3424 int n)
3425{
3426 /* We don't have a reference like avail idx in shared memory */
3427 return;
3428}
3429
3430static void virtio_queue_split_restore_last_avail_idx(VirtIODevice *vdev,
3431 int n)
2d4ba6cc 3432{
b5f53d04 3433 RCU_READ_LOCK_GUARD();
2d4ba6cc
MC
3434 if (vdev->vq[n].vring.desc) {
3435 vdev->vq[n].last_avail_idx = vring_used_idx(&vdev->vq[n]);
3436 vdev->vq[n].shadow_avail_idx = vdev->vq[n].last_avail_idx;
3437 }
2d4ba6cc
MC
3438}
3439
86044b24
JW
3440void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n)
3441{
3442 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3443 virtio_queue_packed_restore_last_avail_idx(vdev, n);
3444 } else {
3445 virtio_queue_split_restore_last_avail_idx(vdev, n);
3446 }
3447}
3448
3449static void virtio_queue_packed_update_used_idx(VirtIODevice *vdev, int n)
3450{
3451 /* used idx was updated through set_last_avail_idx() */
3452 return;
3453}
3454
3455static void virtio_split_packed_update_used_idx(VirtIODevice *vdev, int n)
312d3b35 3456{
b5f53d04 3457 RCU_READ_LOCK_GUARD();
ca0176ad
PB
3458 if (vdev->vq[n].vring.desc) {
3459 vdev->vq[n].used_idx = vring_used_idx(&vdev->vq[n]);
3460 }
312d3b35
YB
3461}
3462
86044b24
JW
3463void virtio_queue_update_used_idx(VirtIODevice *vdev, int n)
3464{
3465 if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
3466 return virtio_queue_packed_update_used_idx(vdev, n);
3467 } else {
3468 return virtio_split_packed_update_used_idx(vdev, n);
3469 }
3470}
3471
6793dfd1
SH
3472void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
3473{
3474 vdev->vq[n].signalled_used_valid = false;
3475}
3476
1cbdabe2
MT
3477VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
3478{
3479 return vdev->vq + n;
3480}
3481
e78a2b42
JW
3482uint16_t virtio_get_queue_index(VirtQueue *vq)
3483{
3484 return vq->queue_index;
3485}
3486
15b2bd18
PB
3487static void virtio_queue_guest_notifier_read(EventNotifier *n)
3488{
3489 VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
3490 if (event_notifier_test_and_clear(n)) {
b4b9862b 3491 virtio_irq(vq);
15b2bd18
PB
3492 }
3493}
3494
3495void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
3496 bool with_irqfd)
3497{
3498 if (assign && !with_irqfd) {
d6da1e9e 3499 event_notifier_set_handler(&vq->guest_notifier,
15b2bd18
PB
3500 virtio_queue_guest_notifier_read);
3501 } else {
d6da1e9e 3502 event_notifier_set_handler(&vq->guest_notifier, NULL);
15b2bd18
PB
3503 }
3504 if (!assign) {
3505 /* Test and clear notifier before closing it,
3506 * in case poll callback didn't have time to run. */
3507 virtio_queue_guest_notifier_read(&vq->guest_notifier);
3508 }
3509}
3510
1cbdabe2
MT
3511EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
3512{
3513 return &vq->guest_notifier;
3514}
b1f416aa 3515
344dc16f 3516static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
b1f416aa
PB
3517{
3518 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3519 if (event_notifier_test_and_clear(n)) {
344dc16f 3520 virtio_queue_notify_aio_vq(vq);
b1f416aa
PB
3521 }
3522}
3523
a7c8215e
SH
3524static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier *n)
3525{
3526 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3527
3528 virtio_queue_set_notification(vq, 0);
3529}
3530
0062ea0f
SH
3531static bool virtio_queue_host_notifier_aio_poll(void *opaque)
3532{
3533 EventNotifier *n = opaque;
3534 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3535
dd3dd4ba 3536 if (!vq->vring.desc || virtio_queue_empty(vq)) {
0062ea0f
SH
3537 return false;
3538 }
3539
d0435bc5 3540 return virtio_queue_notify_aio_vq(vq);
0062ea0f
SH
3541}
3542
a7c8215e
SH
3543static void virtio_queue_host_notifier_aio_poll_end(EventNotifier *n)
3544{
3545 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3546
3547 /* Caller polls once more after this to catch requests that race with us */
3548 virtio_queue_set_notification(vq, 1);
3549}
3550
a1afb606 3551void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
07931698 3552 VirtIOHandleAIOOutput handle_output)
a1afb606 3553{
a378b49a
PB
3554 if (handle_output) {
3555 vq->handle_aio_output = handle_output;
a1afb606 3556 aio_set_event_notifier(ctx, &vq->host_notifier, true,
0062ea0f
SH
3557 virtio_queue_host_notifier_aio_read,
3558 virtio_queue_host_notifier_aio_poll);
a7c8215e
SH
3559 aio_set_event_notifier_poll(ctx, &vq->host_notifier,
3560 virtio_queue_host_notifier_aio_poll_begin,
3561 virtio_queue_host_notifier_aio_poll_end);
a1afb606 3562 } else {
f6a51c84 3563 aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL);
a1afb606
PB
3564 /* Test and clear notifier before after disabling event,
3565 * in case poll callback didn't have time to run. */
344dc16f 3566 virtio_queue_host_notifier_aio_read(&vq->host_notifier);
a378b49a 3567 vq->handle_aio_output = NULL;
344dc16f
MT
3568 }
3569}
3570
fa283a4a 3571void virtio_queue_host_notifier_read(EventNotifier *n)
344dc16f
MT
3572{
3573 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
3574 if (event_notifier_test_and_clear(n)) {
3575 virtio_queue_notify_vq(vq);
a1afb606
PB
3576 }
3577}
3578
1cbdabe2
MT
3579EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
3580{
3581 return &vq->host_notifier;
3582}
8e05db92 3583
fcccb271
SH
3584void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
3585{
3586 vq->host_notifier_enabled = enabled;
3587}
3588
6f80e617
TB
3589int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
3590 MemoryRegion *mr, bool assign)
3591{
3592 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3593 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
3594
3595 if (k->set_host_notifier_mr) {
3596 return k->set_host_notifier_mr(qbus->parent, n, mr, assign);
3597 }
3598
3599 return -1;
3600}
3601
1034e9cf
FK
3602void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
3603{
9e288406 3604 g_free(vdev->bus_name);
80e0090a 3605 vdev->bus_name = g_strdup(bus_name);
1034e9cf
FK
3606}
3607
f5ed3663
SH
3608void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...)
3609{
3610 va_list ap;
3611
3612 va_start(ap, fmt);
3613 error_vreport(fmt, ap);
3614 va_end(ap);
3615
f5ed3663 3616 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
8fc47c87 3617 vdev->status = vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET;
f5ed3663
SH
3618 virtio_notify_config(vdev);
3619 }
66453cff
GK
3620
3621 vdev->broken = true;
f5ed3663
SH
3622}
3623
c611c764
PB
3624static void virtio_memory_listener_commit(MemoryListener *listener)
3625{
3626 VirtIODevice *vdev = container_of(listener, VirtIODevice, listener);
3627 int i;
3628
3629 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
3630 if (vdev->vq[i].vring.num == 0) {
3631 break;
3632 }
3633 virtio_init_region_cache(vdev, i);
3634 }
3635}
3636
1d244b42
AF
3637static void virtio_device_realize(DeviceState *dev, Error **errp)
3638{
3639 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
3640 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
3641 Error *err = NULL;
3642
ea43e259
DDAG
3643 /* Devices should either use vmsd or the load/save methods */
3644 assert(!vdc->vmsd || !vdc->load);
3645
1d244b42
AF
3646 if (vdc->realize != NULL) {
3647 vdc->realize(dev, &err);
3648 if (err != NULL) {
3649 error_propagate(errp, err);
3650 return;
3651 }
8e05db92 3652 }
e8398045
JW
3653
3654 virtio_bus_device_plugged(vdev, &err);
3655 if (err != NULL) {
3656 error_propagate(errp, err);
b69c3c21 3657 vdc->unrealize(dev);
e8398045
JW
3658 return;
3659 }
c611c764
PB
3660
3661 vdev->listener.commit = virtio_memory_listener_commit;
142518bd 3662 vdev->listener.name = "virtio";
c611c764 3663 memory_listener_register(&vdev->listener, vdev->dma_as);
8e05db92
FK
3664}
3665
b69c3c21 3666static void virtio_device_unrealize(DeviceState *dev)
1034e9cf 3667{
1d244b42 3668 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
306ec6c3 3669 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
1d244b42 3670
f6ab64c0 3671 memory_listener_unregister(&vdev->listener);
83d07047
PB
3672 virtio_bus_device_unplugged(vdev);
3673
306ec6c3 3674 if (vdc->unrealize != NULL) {
b69c3c21 3675 vdc->unrealize(dev);
5e96f5d2 3676 }
1d244b42 3677
9e288406
MA
3678 g_free(vdev->bus_name);
3679 vdev->bus_name = NULL;
1034e9cf
FK
3680}
3681
c611c764
PB
3682static void virtio_device_free_virtqueues(VirtIODevice *vdev)
3683{
3684 int i;
3685 if (!vdev->vq) {
3686 return;
3687 }
3688
3689 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
c611c764
PB
3690 if (vdev->vq[i].vring.num == 0) {
3691 break;
3692 }
e0e2d644 3693 virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
c611c764
PB
3694 }
3695 g_free(vdev->vq);
3696}
3697
3698static void virtio_device_instance_finalize(Object *obj)
3699{
3700 VirtIODevice *vdev = VIRTIO_DEVICE(obj);
3701
c611c764
PB
3702 virtio_device_free_virtqueues(vdev);
3703
3704 g_free(vdev->config);
3705 g_free(vdev->vector_queues);
3706}
3707
6b8f1020
CH
3708static Property virtio_properties[] = {
3709 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
e57f2c31 3710 DEFINE_PROP_BOOL("use-started", VirtIODevice, use_started, true),
9d7bd082 3711 DEFINE_PROP_BOOL("use-disabled-flag", VirtIODevice, use_disabled_flag, true),
d55f5182
SG
3712 DEFINE_PROP_BOOL("x-disable-legacy-check", VirtIODevice,
3713 disable_legacy_check, false),
6b8f1020
CH
3714 DEFINE_PROP_END_OF_LIST(),
3715};
3716
ff4c07df
PB
3717static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev)
3718{
3719 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
710fccf8 3720 int i, n, r, err;
ff4c07df 3721
9cf4fd87
GK
3722 /*
3723 * Batch all the host notifiers in a single transaction to avoid
3724 * quadratic time complexity in address_space_update_ioeventfds().
3725 */
710fccf8 3726 memory_region_transaction_begin();
ff4c07df 3727 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
fa283a4a 3728 VirtQueue *vq = &vdev->vq[n];
ff4c07df
PB
3729 if (!virtio_queue_get_num(vdev, n)) {
3730 continue;
3731 }
ed08a2a0 3732 r = virtio_bus_set_host_notifier(qbus, n, true);
ff4c07df
PB
3733 if (r < 0) {
3734 err = r;
3735 goto assign_error;
3736 }
d6da1e9e 3737 event_notifier_set_handler(&vq->host_notifier,
fa283a4a 3738 virtio_queue_host_notifier_read);
6019f3b9
PB
3739 }
3740
3741 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
3742 /* Kick right away to begin processing requests already in vring */
3743 VirtQueue *vq = &vdev->vq[n];
3744 if (!vq->vring.num) {
3745 continue;
3746 }
3747 event_notifier_set(&vq->host_notifier);
ff4c07df 3748 }
710fccf8 3749 memory_region_transaction_commit();
ff4c07df
PB
3750 return 0;
3751
3752assign_error:
710fccf8 3753 i = n; /* save n for a second iteration after transaction is committed. */
ff4c07df 3754 while (--n >= 0) {
fa283a4a 3755 VirtQueue *vq = &vdev->vq[n];
ff4c07df
PB
3756 if (!virtio_queue_get_num(vdev, n)) {
3757 continue;
3758 }
3759
d6da1e9e 3760 event_notifier_set_handler(&vq->host_notifier, NULL);
ed08a2a0 3761 r = virtio_bus_set_host_notifier(qbus, n, false);
ff4c07df 3762 assert(r >= 0);
710fccf8 3763 }
9cf4fd87
GK
3764 /*
3765 * The transaction expects the ioeventfds to be open when it
3766 * commits. Do it now, before the cleanup loop.
3767 */
710fccf8
GH
3768 memory_region_transaction_commit();
3769
3770 while (--i >= 0) {
3771 if (!virtio_queue_get_num(vdev, i)) {
3772 continue;
3773 }
3774 virtio_bus_cleanup_host_notifier(qbus, i);
ff4c07df
PB
3775 }
3776 return err;
3777}
3778
3779int virtio_device_start_ioeventfd(VirtIODevice *vdev)
3780{
3781 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3782 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3783
3784 return virtio_bus_start_ioeventfd(vbus);
3785}
3786
3787static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev)
3788{
3789 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
ff4c07df
PB
3790 int n, r;
3791
9cf4fd87
GK
3792 /*
3793 * Batch all the host notifiers in a single transaction to avoid
3794 * quadratic time complexity in address_space_update_ioeventfds().
3795 */
710fccf8 3796 memory_region_transaction_begin();
ff4c07df 3797 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
fa283a4a
PB
3798 VirtQueue *vq = &vdev->vq[n];
3799
ff4c07df
PB
3800 if (!virtio_queue_get_num(vdev, n)) {
3801 continue;
3802 }
d6da1e9e 3803 event_notifier_set_handler(&vq->host_notifier, NULL);
ed08a2a0 3804 r = virtio_bus_set_host_notifier(qbus, n, false);
ff4c07df 3805 assert(r >= 0);
710fccf8 3806 }
9cf4fd87
GK
3807 /*
3808 * The transaction expects the ioeventfds to be open when it
3809 * commits. Do it now, before the cleanup loop.
3810 */
710fccf8
GH
3811 memory_region_transaction_commit();
3812
3813 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
3814 if (!virtio_queue_get_num(vdev, n)) {
3815 continue;
3816 }
76143618 3817 virtio_bus_cleanup_host_notifier(qbus, n);
ff4c07df
PB
3818 }
3819}
3820
310837de
PB
3821int virtio_device_grab_ioeventfd(VirtIODevice *vdev)
3822{
3823 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3824 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3825
3826 return virtio_bus_grab_ioeventfd(vbus);
3827}
3828
3829void virtio_device_release_ioeventfd(VirtIODevice *vdev)
3830{
3831 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3832 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3833
3834 virtio_bus_release_ioeventfd(vbus);
3835}
3836
8e05db92
FK
3837static void virtio_device_class_init(ObjectClass *klass, void *data)
3838{
3839 /* Set the default value here. */
ff4c07df 3840 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
8e05db92 3841 DeviceClass *dc = DEVICE_CLASS(klass);
1d244b42
AF
3842
3843 dc->realize = virtio_device_realize;
3844 dc->unrealize = virtio_device_unrealize;
8e05db92 3845 dc->bus_type = TYPE_VIRTIO_BUS;
4f67d30b 3846 device_class_set_props(dc, virtio_properties);
ff4c07df
PB
3847 vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl;
3848 vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
9b706dbb
MT
3849
3850 vdc->legacy_features |= VIRTIO_LEGACY_FEATURES;
8e05db92
FK
3851}
3852
8e93cef1
PB
3853bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
3854{
3855 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
3856 VirtioBusState *vbus = VIRTIO_BUS(qbus);
3857
3858 return virtio_bus_ioeventfd_enabled(vbus);
3859}
3860
8e05db92
FK
3861static const TypeInfo virtio_device_info = {
3862 .name = TYPE_VIRTIO_DEVICE,
3863 .parent = TYPE_DEVICE,
3864 .instance_size = sizeof(VirtIODevice),
3865 .class_init = virtio_device_class_init,
c611c764 3866 .instance_finalize = virtio_device_instance_finalize,
8e05db92
FK
3867 .abstract = true,
3868 .class_size = sizeof(VirtioDeviceClass),
3869};
3870
3871static void virtio_register_types(void)
3872{
3873 type_register_static(&virtio_device_info);
3874}
3875
3876type_init(virtio_register_types)