hw/arm: make bitbanded IO optional on ARMv7-M
[qemu/ar7.git] / hw / virtio / virtio.c
blobd4e4d98b595f6f17b58dfe8a09fc8e36c56a12bd
1 /*
2 * Virtio Support
4 * Copyright IBM, Corp. 2007
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qemu-common.h"
17 #include "cpu.h"
18 #include "trace.h"
19 #include "exec/address-spaces.h"
20 #include "qemu/error-report.h"
21 #include "hw/virtio/virtio.h"
22 #include "qemu/atomic.h"
23 #include "hw/virtio/virtio-bus.h"
24 #include "hw/virtio/virtio-access.h"
25 #include "sysemu/dma.h"
28 * The alignment to use between consumer and producer parts of vring.
29 * x86 pagesize again. This is the default, used by transports like PCI
30 * which don't provide a means for the guest to tell the host the alignment.
32 #define VIRTIO_PCI_VRING_ALIGN 4096
34 typedef struct VRingDesc
36 uint64_t addr;
37 uint32_t len;
38 uint16_t flags;
39 uint16_t next;
40 } VRingDesc;
42 typedef struct VRingAvail
44 uint16_t flags;
45 uint16_t idx;
46 uint16_t ring[0];
47 } VRingAvail;
49 typedef struct VRingUsedElem
51 uint32_t id;
52 uint32_t len;
53 } VRingUsedElem;
55 typedef struct VRingUsed
57 uint16_t flags;
58 uint16_t idx;
59 VRingUsedElem ring[0];
60 } VRingUsed;
62 typedef struct VRingMemoryRegionCaches {
63 struct rcu_head rcu;
64 MemoryRegionCache desc;
65 MemoryRegionCache avail;
66 MemoryRegionCache used;
67 } VRingMemoryRegionCaches;
69 typedef struct VRing
71 unsigned int num;
72 unsigned int num_default;
73 unsigned int align;
74 hwaddr desc;
75 hwaddr avail;
76 hwaddr used;
77 VRingMemoryRegionCaches *caches;
78 } VRing;
80 struct VirtQueue
82 VRing vring;
84 /* Next head to pop */
85 uint16_t last_avail_idx;
87 /* Last avail_idx read from VQ. */
88 uint16_t shadow_avail_idx;
90 uint16_t used_idx;
92 /* Last used index value we have signalled on */
93 uint16_t signalled_used;
95 /* Last used index value we have signalled on */
96 bool signalled_used_valid;
98 /* Notification enabled? */
99 bool notification;
101 uint16_t queue_index;
103 unsigned int inuse;
105 uint16_t vector;
106 VirtIOHandleOutput handle_output;
107 VirtIOHandleAIOOutput handle_aio_output;
108 VirtIODevice *vdev;
109 EventNotifier guest_notifier;
110 EventNotifier host_notifier;
111 QLIST_ENTRY(VirtQueue) node;
114 static void virtio_free_region_cache(VRingMemoryRegionCaches *caches)
116 if (!caches) {
117 return;
120 address_space_cache_destroy(&caches->desc);
121 address_space_cache_destroy(&caches->avail);
122 address_space_cache_destroy(&caches->used);
123 g_free(caches);
126 static void virtio_virtqueue_reset_region_cache(struct VirtQueue *vq)
128 VRingMemoryRegionCaches *caches;
130 caches = atomic_read(&vq->vring.caches);
131 atomic_rcu_set(&vq->vring.caches, NULL);
132 if (caches) {
133 call_rcu(caches, virtio_free_region_cache, rcu);
137 static void virtio_init_region_cache(VirtIODevice *vdev, int n)
139 VirtQueue *vq = &vdev->vq[n];
140 VRingMemoryRegionCaches *old = vq->vring.caches;
141 VRingMemoryRegionCaches *new = NULL;
142 hwaddr addr, size;
143 int event_size;
144 int64_t len;
146 event_size = virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0;
148 addr = vq->vring.desc;
149 if (!addr) {
150 goto out_no_cache;
152 new = g_new0(VRingMemoryRegionCaches, 1);
153 size = virtio_queue_get_desc_size(vdev, n);
154 len = address_space_cache_init(&new->desc, vdev->dma_as,
155 addr, size, false);
156 if (len < size) {
157 virtio_error(vdev, "Cannot map desc");
158 goto err_desc;
161 size = virtio_queue_get_used_size(vdev, n) + event_size;
162 len = address_space_cache_init(&new->used, vdev->dma_as,
163 vq->vring.used, size, true);
164 if (len < size) {
165 virtio_error(vdev, "Cannot map used");
166 goto err_used;
169 size = virtio_queue_get_avail_size(vdev, n) + event_size;
170 len = address_space_cache_init(&new->avail, vdev->dma_as,
171 vq->vring.avail, size, false);
172 if (len < size) {
173 virtio_error(vdev, "Cannot map avail");
174 goto err_avail;
177 atomic_rcu_set(&vq->vring.caches, new);
178 if (old) {
179 call_rcu(old, virtio_free_region_cache, rcu);
181 return;
183 err_avail:
184 address_space_cache_destroy(&new->avail);
185 err_used:
186 address_space_cache_destroy(&new->used);
187 err_desc:
188 address_space_cache_destroy(&new->desc);
189 out_no_cache:
190 g_free(new);
191 virtio_virtqueue_reset_region_cache(vq);
194 /* virt queue functions */
195 void virtio_queue_update_rings(VirtIODevice *vdev, int n)
197 VRing *vring = &vdev->vq[n].vring;
199 if (!vring->num || !vring->desc || !vring->align) {
200 /* not yet setup -> nothing to do */
201 return;
203 vring->avail = vring->desc + vring->num * sizeof(VRingDesc);
204 vring->used = vring_align(vring->avail +
205 offsetof(VRingAvail, ring[vring->num]),
206 vring->align);
207 virtio_init_region_cache(vdev, n);
210 /* Called within rcu_read_lock(). */
211 static void vring_desc_read(VirtIODevice *vdev, VRingDesc *desc,
212 MemoryRegionCache *cache, int i)
214 address_space_read_cached(cache, i * sizeof(VRingDesc),
215 desc, sizeof(VRingDesc));
216 virtio_tswap64s(vdev, &desc->addr);
217 virtio_tswap32s(vdev, &desc->len);
218 virtio_tswap16s(vdev, &desc->flags);
219 virtio_tswap16s(vdev, &desc->next);
222 static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *vq)
224 VRingMemoryRegionCaches *caches = atomic_rcu_read(&vq->vring.caches);
225 assert(caches != NULL);
226 return caches;
228 /* Called within rcu_read_lock(). */
229 static inline uint16_t vring_avail_flags(VirtQueue *vq)
231 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
232 hwaddr pa = offsetof(VRingAvail, flags);
233 return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
236 /* Called within rcu_read_lock(). */
237 static inline uint16_t vring_avail_idx(VirtQueue *vq)
239 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
240 hwaddr pa = offsetof(VRingAvail, idx);
241 vq->shadow_avail_idx = virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
242 return vq->shadow_avail_idx;
245 /* Called within rcu_read_lock(). */
246 static inline uint16_t vring_avail_ring(VirtQueue *vq, int i)
248 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
249 hwaddr pa = offsetof(VRingAvail, ring[i]);
250 return virtio_lduw_phys_cached(vq->vdev, &caches->avail, pa);
253 /* Called within rcu_read_lock(). */
254 static inline uint16_t vring_get_used_event(VirtQueue *vq)
256 return vring_avail_ring(vq, vq->vring.num);
259 /* Called within rcu_read_lock(). */
260 static inline void vring_used_write(VirtQueue *vq, VRingUsedElem *uelem,
261 int i)
263 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
264 hwaddr pa = offsetof(VRingUsed, ring[i]);
265 virtio_tswap32s(vq->vdev, &uelem->id);
266 virtio_tswap32s(vq->vdev, &uelem->len);
267 address_space_write_cached(&caches->used, pa, uelem, sizeof(VRingUsedElem));
268 address_space_cache_invalidate(&caches->used, pa, sizeof(VRingUsedElem));
271 /* Called within rcu_read_lock(). */
272 static uint16_t vring_used_idx(VirtQueue *vq)
274 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
275 hwaddr pa = offsetof(VRingUsed, idx);
276 return virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
279 /* Called within rcu_read_lock(). */
280 static inline void vring_used_idx_set(VirtQueue *vq, uint16_t val)
282 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
283 hwaddr pa = offsetof(VRingUsed, idx);
284 virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
285 address_space_cache_invalidate(&caches->used, pa, sizeof(val));
286 vq->used_idx = val;
289 /* Called within rcu_read_lock(). */
290 static inline void vring_used_flags_set_bit(VirtQueue *vq, int mask)
292 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
293 VirtIODevice *vdev = vq->vdev;
294 hwaddr pa = offsetof(VRingUsed, flags);
295 uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
297 virtio_stw_phys_cached(vdev, &caches->used, pa, flags | mask);
298 address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
301 /* Called within rcu_read_lock(). */
302 static inline void vring_used_flags_unset_bit(VirtQueue *vq, int mask)
304 VRingMemoryRegionCaches *caches = vring_get_region_caches(vq);
305 VirtIODevice *vdev = vq->vdev;
306 hwaddr pa = offsetof(VRingUsed, flags);
307 uint16_t flags = virtio_lduw_phys_cached(vq->vdev, &caches->used, pa);
309 virtio_stw_phys_cached(vdev, &caches->used, pa, flags & ~mask);
310 address_space_cache_invalidate(&caches->used, pa, sizeof(flags));
313 /* Called within rcu_read_lock(). */
314 static inline void vring_set_avail_event(VirtQueue *vq, uint16_t val)
316 VRingMemoryRegionCaches *caches;
317 hwaddr pa;
318 if (!vq->notification) {
319 return;
322 caches = vring_get_region_caches(vq);
323 pa = offsetof(VRingUsed, ring[vq->vring.num]);
324 virtio_stw_phys_cached(vq->vdev, &caches->used, pa, val);
325 address_space_cache_invalidate(&caches->used, pa, sizeof(val));
328 void virtio_queue_set_notification(VirtQueue *vq, int enable)
330 vq->notification = enable;
332 if (!vq->vring.desc) {
333 return;
336 rcu_read_lock();
337 if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) {
338 vring_set_avail_event(vq, vring_avail_idx(vq));
339 } else if (enable) {
340 vring_used_flags_unset_bit(vq, VRING_USED_F_NO_NOTIFY);
341 } else {
342 vring_used_flags_set_bit(vq, VRING_USED_F_NO_NOTIFY);
344 if (enable) {
345 /* Expose avail event/used flags before caller checks the avail idx. */
346 smp_mb();
348 rcu_read_unlock();
351 int virtio_queue_ready(VirtQueue *vq)
353 return vq->vring.avail != 0;
356 /* Fetch avail_idx from VQ memory only when we really need to know if
357 * guest has added some buffers.
358 * Called within rcu_read_lock(). */
359 static int virtio_queue_empty_rcu(VirtQueue *vq)
361 if (unlikely(!vq->vring.avail)) {
362 return 1;
365 if (vq->shadow_avail_idx != vq->last_avail_idx) {
366 return 0;
369 return vring_avail_idx(vq) == vq->last_avail_idx;
372 int virtio_queue_empty(VirtQueue *vq)
374 bool empty;
376 if (unlikely(!vq->vring.avail)) {
377 return 1;
380 if (vq->shadow_avail_idx != vq->last_avail_idx) {
381 return 0;
384 rcu_read_lock();
385 empty = vring_avail_idx(vq) == vq->last_avail_idx;
386 rcu_read_unlock();
387 return empty;
390 static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem,
391 unsigned int len)
393 AddressSpace *dma_as = vq->vdev->dma_as;
394 unsigned int offset;
395 int i;
397 offset = 0;
398 for (i = 0; i < elem->in_num; i++) {
399 size_t size = MIN(len - offset, elem->in_sg[i].iov_len);
401 dma_memory_unmap(dma_as, elem->in_sg[i].iov_base,
402 elem->in_sg[i].iov_len,
403 DMA_DIRECTION_FROM_DEVICE, size);
405 offset += size;
408 for (i = 0; i < elem->out_num; i++)
409 dma_memory_unmap(dma_as, elem->out_sg[i].iov_base,
410 elem->out_sg[i].iov_len,
411 DMA_DIRECTION_TO_DEVICE,
412 elem->out_sg[i].iov_len);
415 /* virtqueue_detach_element:
416 * @vq: The #VirtQueue
417 * @elem: The #VirtQueueElement
418 * @len: number of bytes written
420 * Detach the element from the virtqueue. This function is suitable for device
421 * reset or other situations where a #VirtQueueElement is simply freed and will
422 * not be pushed or discarded.
424 void virtqueue_detach_element(VirtQueue *vq, const VirtQueueElement *elem,
425 unsigned int len)
427 vq->inuse--;
428 virtqueue_unmap_sg(vq, elem, len);
431 /* virtqueue_unpop:
432 * @vq: The #VirtQueue
433 * @elem: The #VirtQueueElement
434 * @len: number of bytes written
436 * Pretend the most recent element wasn't popped from the virtqueue. The next
437 * call to virtqueue_pop() will refetch the element.
439 void virtqueue_unpop(VirtQueue *vq, const VirtQueueElement *elem,
440 unsigned int len)
442 vq->last_avail_idx--;
443 virtqueue_detach_element(vq, elem, len);
446 /* virtqueue_rewind:
447 * @vq: The #VirtQueue
448 * @num: Number of elements to push back
450 * Pretend that elements weren't popped from the virtqueue. The next
451 * virtqueue_pop() will refetch the oldest element.
453 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
455 * Returns: true on success, false if @num is greater than the number of in use
456 * elements.
458 bool virtqueue_rewind(VirtQueue *vq, unsigned int num)
460 if (num > vq->inuse) {
461 return false;
463 vq->last_avail_idx -= num;
464 vq->inuse -= num;
465 return true;
468 /* Called within rcu_read_lock(). */
469 void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem,
470 unsigned int len, unsigned int idx)
472 VRingUsedElem uelem;
474 trace_virtqueue_fill(vq, elem, len, idx);
476 virtqueue_unmap_sg(vq, elem, len);
478 if (unlikely(vq->vdev->broken)) {
479 return;
482 if (unlikely(!vq->vring.used)) {
483 return;
486 idx = (idx + vq->used_idx) % vq->vring.num;
488 uelem.id = elem->index;
489 uelem.len = len;
490 vring_used_write(vq, &uelem, idx);
493 /* Called within rcu_read_lock(). */
494 void virtqueue_flush(VirtQueue *vq, unsigned int count)
496 uint16_t old, new;
498 if (unlikely(vq->vdev->broken)) {
499 vq->inuse -= count;
500 return;
503 if (unlikely(!vq->vring.used)) {
504 return;
507 /* Make sure buffer is written before we update index. */
508 smp_wmb();
509 trace_virtqueue_flush(vq, count);
510 old = vq->used_idx;
511 new = old + count;
512 vring_used_idx_set(vq, new);
513 vq->inuse -= count;
514 if (unlikely((int16_t)(new - vq->signalled_used) < (uint16_t)(new - old)))
515 vq->signalled_used_valid = false;
518 void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem,
519 unsigned int len)
521 rcu_read_lock();
522 virtqueue_fill(vq, elem, len, 0);
523 virtqueue_flush(vq, 1);
524 rcu_read_unlock();
527 /* Called within rcu_read_lock(). */
528 static int virtqueue_num_heads(VirtQueue *vq, unsigned int idx)
530 uint16_t num_heads = vring_avail_idx(vq) - idx;
532 /* Check it isn't doing very strange things with descriptor numbers. */
533 if (num_heads > vq->vring.num) {
534 virtio_error(vq->vdev, "Guest moved used index from %u to %u",
535 idx, vq->shadow_avail_idx);
536 return -EINVAL;
538 /* On success, callers read a descriptor at vq->last_avail_idx.
539 * Make sure descriptor read does not bypass avail index read. */
540 if (num_heads) {
541 smp_rmb();
544 return num_heads;
547 /* Called within rcu_read_lock(). */
548 static bool virtqueue_get_head(VirtQueue *vq, unsigned int idx,
549 unsigned int *head)
551 /* Grab the next descriptor number they're advertising, and increment
552 * the index we've seen. */
553 *head = vring_avail_ring(vq, idx % vq->vring.num);
555 /* If their number is silly, that's a fatal mistake. */
556 if (*head >= vq->vring.num) {
557 virtio_error(vq->vdev, "Guest says index %u is available", *head);
558 return false;
561 return true;
564 enum {
565 VIRTQUEUE_READ_DESC_ERROR = -1,
566 VIRTQUEUE_READ_DESC_DONE = 0, /* end of chain */
567 VIRTQUEUE_READ_DESC_MORE = 1, /* more buffers in chain */
570 static int virtqueue_read_next_desc(VirtIODevice *vdev, VRingDesc *desc,
571 MemoryRegionCache *desc_cache, unsigned int max,
572 unsigned int *next)
574 /* If this descriptor says it doesn't chain, we're done. */
575 if (!(desc->flags & VRING_DESC_F_NEXT)) {
576 return VIRTQUEUE_READ_DESC_DONE;
579 /* Check they're not leading us off end of descriptors. */
580 *next = desc->next;
581 /* Make sure compiler knows to grab that: we don't want it changing! */
582 smp_wmb();
584 if (*next >= max) {
585 virtio_error(vdev, "Desc next is %u", *next);
586 return VIRTQUEUE_READ_DESC_ERROR;
589 vring_desc_read(vdev, desc, desc_cache, *next);
590 return VIRTQUEUE_READ_DESC_MORE;
593 void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes,
594 unsigned int *out_bytes,
595 unsigned max_in_bytes, unsigned max_out_bytes)
597 VirtIODevice *vdev = vq->vdev;
598 unsigned int max, idx;
599 unsigned int total_bufs, in_total, out_total;
600 VRingMemoryRegionCaches *caches;
601 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
602 int64_t len = 0;
603 int rc;
605 if (unlikely(!vq->vring.desc)) {
606 if (in_bytes) {
607 *in_bytes = 0;
609 if (out_bytes) {
610 *out_bytes = 0;
612 return;
615 rcu_read_lock();
616 idx = vq->last_avail_idx;
617 total_bufs = in_total = out_total = 0;
619 max = vq->vring.num;
620 caches = vring_get_region_caches(vq);
621 if (caches->desc.len < max * sizeof(VRingDesc)) {
622 virtio_error(vdev, "Cannot map descriptor ring");
623 goto err;
626 while ((rc = virtqueue_num_heads(vq, idx)) > 0) {
627 MemoryRegionCache *desc_cache = &caches->desc;
628 unsigned int num_bufs;
629 VRingDesc desc;
630 unsigned int i;
632 num_bufs = total_bufs;
634 if (!virtqueue_get_head(vq, idx++, &i)) {
635 goto err;
638 vring_desc_read(vdev, &desc, desc_cache, i);
640 if (desc.flags & VRING_DESC_F_INDIRECT) {
641 if (desc.len % sizeof(VRingDesc)) {
642 virtio_error(vdev, "Invalid size for indirect buffer table");
643 goto err;
646 /* If we've got too many, that implies a descriptor loop. */
647 if (num_bufs >= max) {
648 virtio_error(vdev, "Looped descriptor");
649 goto err;
652 /* loop over the indirect descriptor table */
653 len = address_space_cache_init(&indirect_desc_cache,
654 vdev->dma_as,
655 desc.addr, desc.len, false);
656 desc_cache = &indirect_desc_cache;
657 if (len < desc.len) {
658 virtio_error(vdev, "Cannot map indirect buffer");
659 goto err;
662 max = desc.len / sizeof(VRingDesc);
663 num_bufs = i = 0;
664 vring_desc_read(vdev, &desc, desc_cache, i);
667 do {
668 /* If we've got too many, that implies a descriptor loop. */
669 if (++num_bufs > max) {
670 virtio_error(vdev, "Looped descriptor");
671 goto err;
674 if (desc.flags & VRING_DESC_F_WRITE) {
675 in_total += desc.len;
676 } else {
677 out_total += desc.len;
679 if (in_total >= max_in_bytes && out_total >= max_out_bytes) {
680 goto done;
683 rc = virtqueue_read_next_desc(vdev, &desc, desc_cache, max, &i);
684 } while (rc == VIRTQUEUE_READ_DESC_MORE);
686 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
687 goto err;
690 if (desc_cache == &indirect_desc_cache) {
691 address_space_cache_destroy(&indirect_desc_cache);
692 total_bufs++;
693 } else {
694 total_bufs = num_bufs;
698 if (rc < 0) {
699 goto err;
702 done:
703 address_space_cache_destroy(&indirect_desc_cache);
704 if (in_bytes) {
705 *in_bytes = in_total;
707 if (out_bytes) {
708 *out_bytes = out_total;
710 rcu_read_unlock();
711 return;
713 err:
714 in_total = out_total = 0;
715 goto done;
718 int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes,
719 unsigned int out_bytes)
721 unsigned int in_total, out_total;
723 virtqueue_get_avail_bytes(vq, &in_total, &out_total, in_bytes, out_bytes);
724 return in_bytes <= in_total && out_bytes <= out_total;
727 static bool virtqueue_map_desc(VirtIODevice *vdev, unsigned int *p_num_sg,
728 hwaddr *addr, struct iovec *iov,
729 unsigned int max_num_sg, bool is_write,
730 hwaddr pa, size_t sz)
732 bool ok = false;
733 unsigned num_sg = *p_num_sg;
734 assert(num_sg <= max_num_sg);
736 if (!sz) {
737 virtio_error(vdev, "virtio: zero sized buffers are not allowed");
738 goto out;
741 while (sz) {
742 hwaddr len = sz;
744 if (num_sg == max_num_sg) {
745 virtio_error(vdev, "virtio: too many write descriptors in "
746 "indirect table");
747 goto out;
750 iov[num_sg].iov_base = dma_memory_map(vdev->dma_as, pa, &len,
751 is_write ?
752 DMA_DIRECTION_FROM_DEVICE :
753 DMA_DIRECTION_TO_DEVICE);
754 if (!iov[num_sg].iov_base) {
755 virtio_error(vdev, "virtio: bogus descriptor or out of resources");
756 goto out;
759 iov[num_sg].iov_len = len;
760 addr[num_sg] = pa;
762 sz -= len;
763 pa += len;
764 num_sg++;
766 ok = true;
768 out:
769 *p_num_sg = num_sg;
770 return ok;
773 /* Only used by error code paths before we have a VirtQueueElement (therefore
774 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
775 * yet.
777 static void virtqueue_undo_map_desc(unsigned int out_num, unsigned int in_num,
778 struct iovec *iov)
780 unsigned int i;
782 for (i = 0; i < out_num + in_num; i++) {
783 int is_write = i >= out_num;
785 cpu_physical_memory_unmap(iov->iov_base, iov->iov_len, is_write, 0);
786 iov++;
790 static void virtqueue_map_iovec(VirtIODevice *vdev, struct iovec *sg,
791 hwaddr *addr, unsigned int *num_sg,
792 int is_write)
794 unsigned int i;
795 hwaddr len;
797 for (i = 0; i < *num_sg; i++) {
798 len = sg[i].iov_len;
799 sg[i].iov_base = dma_memory_map(vdev->dma_as,
800 addr[i], &len, is_write ?
801 DMA_DIRECTION_FROM_DEVICE :
802 DMA_DIRECTION_TO_DEVICE);
803 if (!sg[i].iov_base) {
804 error_report("virtio: error trying to map MMIO memory");
805 exit(1);
807 if (len != sg[i].iov_len) {
808 error_report("virtio: unexpected memory split");
809 exit(1);
814 void virtqueue_map(VirtIODevice *vdev, VirtQueueElement *elem)
816 virtqueue_map_iovec(vdev, elem->in_sg, elem->in_addr, &elem->in_num, 1);
817 virtqueue_map_iovec(vdev, elem->out_sg, elem->out_addr, &elem->out_num, 0);
820 static void *virtqueue_alloc_element(size_t sz, unsigned out_num, unsigned in_num)
822 VirtQueueElement *elem;
823 size_t in_addr_ofs = QEMU_ALIGN_UP(sz, __alignof__(elem->in_addr[0]));
824 size_t out_addr_ofs = in_addr_ofs + in_num * sizeof(elem->in_addr[0]);
825 size_t out_addr_end = out_addr_ofs + out_num * sizeof(elem->out_addr[0]);
826 size_t in_sg_ofs = QEMU_ALIGN_UP(out_addr_end, __alignof__(elem->in_sg[0]));
827 size_t out_sg_ofs = in_sg_ofs + in_num * sizeof(elem->in_sg[0]);
828 size_t out_sg_end = out_sg_ofs + out_num * sizeof(elem->out_sg[0]);
830 assert(sz >= sizeof(VirtQueueElement));
831 elem = g_malloc(out_sg_end);
832 trace_virtqueue_alloc_element(elem, sz, in_num, out_num);
833 elem->out_num = out_num;
834 elem->in_num = in_num;
835 elem->in_addr = (void *)elem + in_addr_ofs;
836 elem->out_addr = (void *)elem + out_addr_ofs;
837 elem->in_sg = (void *)elem + in_sg_ofs;
838 elem->out_sg = (void *)elem + out_sg_ofs;
839 return elem;
842 void *virtqueue_pop(VirtQueue *vq, size_t sz)
844 unsigned int i, head, max;
845 VRingMemoryRegionCaches *caches;
846 MemoryRegionCache indirect_desc_cache = MEMORY_REGION_CACHE_INVALID;
847 MemoryRegionCache *desc_cache;
848 int64_t len;
849 VirtIODevice *vdev = vq->vdev;
850 VirtQueueElement *elem = NULL;
851 unsigned out_num, in_num, elem_entries;
852 hwaddr addr[VIRTQUEUE_MAX_SIZE];
853 struct iovec iov[VIRTQUEUE_MAX_SIZE];
854 VRingDesc desc;
855 int rc;
857 if (unlikely(vdev->broken)) {
858 return NULL;
860 rcu_read_lock();
861 if (virtio_queue_empty_rcu(vq)) {
862 goto done;
864 /* Needed after virtio_queue_empty(), see comment in
865 * virtqueue_num_heads(). */
866 smp_rmb();
868 /* When we start there are none of either input nor output. */
869 out_num = in_num = elem_entries = 0;
871 max = vq->vring.num;
873 if (vq->inuse >= vq->vring.num) {
874 virtio_error(vdev, "Virtqueue size exceeded");
875 goto done;
878 if (!virtqueue_get_head(vq, vq->last_avail_idx++, &head)) {
879 goto done;
882 if (virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
883 vring_set_avail_event(vq, vq->last_avail_idx);
886 i = head;
888 caches = vring_get_region_caches(vq);
889 if (caches->desc.len < max * sizeof(VRingDesc)) {
890 virtio_error(vdev, "Cannot map descriptor ring");
891 goto done;
894 desc_cache = &caches->desc;
895 vring_desc_read(vdev, &desc, desc_cache, i);
896 if (desc.flags & VRING_DESC_F_INDIRECT) {
897 if (desc.len % sizeof(VRingDesc)) {
898 virtio_error(vdev, "Invalid size for indirect buffer table");
899 goto done;
902 /* loop over the indirect descriptor table */
903 len = address_space_cache_init(&indirect_desc_cache, vdev->dma_as,
904 desc.addr, desc.len, false);
905 desc_cache = &indirect_desc_cache;
906 if (len < desc.len) {
907 virtio_error(vdev, "Cannot map indirect buffer");
908 goto done;
911 max = desc.len / sizeof(VRingDesc);
912 i = 0;
913 vring_desc_read(vdev, &desc, desc_cache, i);
916 /* Collect all the descriptors */
917 do {
918 bool map_ok;
920 if (desc.flags & VRING_DESC_F_WRITE) {
921 map_ok = virtqueue_map_desc(vdev, &in_num, addr + out_num,
922 iov + out_num,
923 VIRTQUEUE_MAX_SIZE - out_num, true,
924 desc.addr, desc.len);
925 } else {
926 if (in_num) {
927 virtio_error(vdev, "Incorrect order for descriptors");
928 goto err_undo_map;
930 map_ok = virtqueue_map_desc(vdev, &out_num, addr, iov,
931 VIRTQUEUE_MAX_SIZE, false,
932 desc.addr, desc.len);
934 if (!map_ok) {
935 goto err_undo_map;
938 /* If we've got too many, that implies a descriptor loop. */
939 if (++elem_entries > max) {
940 virtio_error(vdev, "Looped descriptor");
941 goto err_undo_map;
944 rc = virtqueue_read_next_desc(vdev, &desc, desc_cache, max, &i);
945 } while (rc == VIRTQUEUE_READ_DESC_MORE);
947 if (rc == VIRTQUEUE_READ_DESC_ERROR) {
948 goto err_undo_map;
951 /* Now copy what we have collected and mapped */
952 elem = virtqueue_alloc_element(sz, out_num, in_num);
953 elem->index = head;
954 for (i = 0; i < out_num; i++) {
955 elem->out_addr[i] = addr[i];
956 elem->out_sg[i] = iov[i];
958 for (i = 0; i < in_num; i++) {
959 elem->in_addr[i] = addr[out_num + i];
960 elem->in_sg[i] = iov[out_num + i];
963 vq->inuse++;
965 trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num);
966 done:
967 address_space_cache_destroy(&indirect_desc_cache);
968 rcu_read_unlock();
970 return elem;
972 err_undo_map:
973 virtqueue_undo_map_desc(out_num, in_num, iov);
974 goto done;
977 /* virtqueue_drop_all:
978 * @vq: The #VirtQueue
979 * Drops all queued buffers and indicates them to the guest
980 * as if they are done. Useful when buffers can not be
981 * processed but must be returned to the guest.
983 unsigned int virtqueue_drop_all(VirtQueue *vq)
985 unsigned int dropped = 0;
986 VirtQueueElement elem = {};
987 VirtIODevice *vdev = vq->vdev;
988 bool fEventIdx = virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
990 if (unlikely(vdev->broken)) {
991 return 0;
994 while (!virtio_queue_empty(vq) && vq->inuse < vq->vring.num) {
995 /* works similar to virtqueue_pop but does not map buffers
996 * and does not allocate any memory */
997 smp_rmb();
998 if (!virtqueue_get_head(vq, vq->last_avail_idx, &elem.index)) {
999 break;
1001 vq->inuse++;
1002 vq->last_avail_idx++;
1003 if (fEventIdx) {
1004 vring_set_avail_event(vq, vq->last_avail_idx);
1006 /* immediately push the element, nothing to unmap
1007 * as both in_num and out_num are set to 0 */
1008 virtqueue_push(vq, &elem, 0);
1009 dropped++;
1012 return dropped;
1015 /* Reading and writing a structure directly to QEMUFile is *awful*, but
1016 * it is what QEMU has always done by mistake. We can change it sooner
1017 * or later by bumping the version number of the affected vm states.
1018 * In the meanwhile, since the in-memory layout of VirtQueueElement
1019 * has changed, we need to marshal to and from the layout that was
1020 * used before the change.
1022 typedef struct VirtQueueElementOld {
1023 unsigned int index;
1024 unsigned int out_num;
1025 unsigned int in_num;
1026 hwaddr in_addr[VIRTQUEUE_MAX_SIZE];
1027 hwaddr out_addr[VIRTQUEUE_MAX_SIZE];
1028 struct iovec in_sg[VIRTQUEUE_MAX_SIZE];
1029 struct iovec out_sg[VIRTQUEUE_MAX_SIZE];
1030 } VirtQueueElementOld;
1032 void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz)
1034 VirtQueueElement *elem;
1035 VirtQueueElementOld data;
1036 int i;
1038 qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
1040 /* TODO: teach all callers that this can fail, and return failure instead
1041 * of asserting here.
1042 * This is just one thing (there are probably more) that must be
1043 * fixed before we can allow NDEBUG compilation.
1045 assert(ARRAY_SIZE(data.in_addr) >= data.in_num);
1046 assert(ARRAY_SIZE(data.out_addr) >= data.out_num);
1048 elem = virtqueue_alloc_element(sz, data.out_num, data.in_num);
1049 elem->index = data.index;
1051 for (i = 0; i < elem->in_num; i++) {
1052 elem->in_addr[i] = data.in_addr[i];
1055 for (i = 0; i < elem->out_num; i++) {
1056 elem->out_addr[i] = data.out_addr[i];
1059 for (i = 0; i < elem->in_num; i++) {
1060 /* Base is overwritten by virtqueue_map. */
1061 elem->in_sg[i].iov_base = 0;
1062 elem->in_sg[i].iov_len = data.in_sg[i].iov_len;
1065 for (i = 0; i < elem->out_num; i++) {
1066 /* Base is overwritten by virtqueue_map. */
1067 elem->out_sg[i].iov_base = 0;
1068 elem->out_sg[i].iov_len = data.out_sg[i].iov_len;
1071 virtqueue_map(vdev, elem);
1072 return elem;
1075 void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem)
1077 VirtQueueElementOld data;
1078 int i;
1080 memset(&data, 0, sizeof(data));
1081 data.index = elem->index;
1082 data.in_num = elem->in_num;
1083 data.out_num = elem->out_num;
1085 for (i = 0; i < elem->in_num; i++) {
1086 data.in_addr[i] = elem->in_addr[i];
1089 for (i = 0; i < elem->out_num; i++) {
1090 data.out_addr[i] = elem->out_addr[i];
1093 for (i = 0; i < elem->in_num; i++) {
1094 /* Base is overwritten by virtqueue_map when loading. Do not
1095 * save it, as it would leak the QEMU address space layout. */
1096 data.in_sg[i].iov_len = elem->in_sg[i].iov_len;
1099 for (i = 0; i < elem->out_num; i++) {
1100 /* Do not save iov_base as above. */
1101 data.out_sg[i].iov_len = elem->out_sg[i].iov_len;
1103 qemu_put_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld));
1106 /* virtio device */
1107 static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector)
1109 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1110 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1112 if (unlikely(vdev->broken)) {
1113 return;
1116 if (k->notify) {
1117 k->notify(qbus->parent, vector);
1121 void virtio_update_irq(VirtIODevice *vdev)
1123 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
1126 static int virtio_validate_features(VirtIODevice *vdev)
1128 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1130 if (virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM) &&
1131 !virtio_vdev_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM)) {
1132 return -EFAULT;
1135 if (k->validate_features) {
1136 return k->validate_features(vdev);
1137 } else {
1138 return 0;
1142 int virtio_set_status(VirtIODevice *vdev, uint8_t val)
1144 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1145 trace_virtio_set_status(vdev, val);
1147 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1148 if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
1149 val & VIRTIO_CONFIG_S_FEATURES_OK) {
1150 int ret = virtio_validate_features(vdev);
1152 if (ret) {
1153 return ret;
1157 if (k->set_status) {
1158 k->set_status(vdev, val);
1160 vdev->status = val;
1161 return 0;
1164 bool target_words_bigendian(void);
1165 static enum virtio_device_endian virtio_default_endian(void)
1167 if (target_words_bigendian()) {
1168 return VIRTIO_DEVICE_ENDIAN_BIG;
1169 } else {
1170 return VIRTIO_DEVICE_ENDIAN_LITTLE;
1174 static enum virtio_device_endian virtio_current_cpu_endian(void)
1176 CPUClass *cc = CPU_GET_CLASS(current_cpu);
1178 if (cc->virtio_is_big_endian(current_cpu)) {
1179 return VIRTIO_DEVICE_ENDIAN_BIG;
1180 } else {
1181 return VIRTIO_DEVICE_ENDIAN_LITTLE;
1185 void virtio_reset(void *opaque)
1187 VirtIODevice *vdev = opaque;
1188 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1189 int i;
1191 virtio_set_status(vdev, 0);
1192 if (current_cpu) {
1193 /* Guest initiated reset */
1194 vdev->device_endian = virtio_current_cpu_endian();
1195 } else {
1196 /* System reset */
1197 vdev->device_endian = virtio_default_endian();
1200 if (k->reset) {
1201 k->reset(vdev);
1204 vdev->broken = false;
1205 vdev->guest_features = 0;
1206 vdev->queue_sel = 0;
1207 vdev->status = 0;
1208 atomic_set(&vdev->isr, 0);
1209 vdev->config_vector = VIRTIO_NO_VECTOR;
1210 virtio_notify_vector(vdev, vdev->config_vector);
1212 for(i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1213 vdev->vq[i].vring.desc = 0;
1214 vdev->vq[i].vring.avail = 0;
1215 vdev->vq[i].vring.used = 0;
1216 vdev->vq[i].last_avail_idx = 0;
1217 vdev->vq[i].shadow_avail_idx = 0;
1218 vdev->vq[i].used_idx = 0;
1219 virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR);
1220 vdev->vq[i].signalled_used = 0;
1221 vdev->vq[i].signalled_used_valid = false;
1222 vdev->vq[i].notification = true;
1223 vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
1224 vdev->vq[i].inuse = 0;
1225 virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
1229 uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
1231 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1232 uint8_t val;
1234 if (addr + sizeof(val) > vdev->config_len) {
1235 return (uint32_t)-1;
1238 k->get_config(vdev, vdev->config);
1240 val = ldub_p(vdev->config + addr);
1241 return val;
1244 uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
1246 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1247 uint16_t val;
1249 if (addr + sizeof(val) > vdev->config_len) {
1250 return (uint32_t)-1;
1253 k->get_config(vdev, vdev->config);
1255 val = lduw_p(vdev->config + addr);
1256 return val;
1259 uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
1261 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1262 uint32_t val;
1264 if (addr + sizeof(val) > vdev->config_len) {
1265 return (uint32_t)-1;
1268 k->get_config(vdev, vdev->config);
1270 val = ldl_p(vdev->config + addr);
1271 return val;
1274 void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
1276 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1277 uint8_t val = data;
1279 if (addr + sizeof(val) > vdev->config_len) {
1280 return;
1283 stb_p(vdev->config + addr, val);
1285 if (k->set_config) {
1286 k->set_config(vdev, vdev->config);
1290 void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
1292 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1293 uint16_t val = data;
1295 if (addr + sizeof(val) > vdev->config_len) {
1296 return;
1299 stw_p(vdev->config + addr, val);
1301 if (k->set_config) {
1302 k->set_config(vdev, vdev->config);
1306 void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
1308 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1309 uint32_t val = data;
1311 if (addr + sizeof(val) > vdev->config_len) {
1312 return;
1315 stl_p(vdev->config + addr, val);
1317 if (k->set_config) {
1318 k->set_config(vdev, vdev->config);
1322 uint32_t virtio_config_modern_readb(VirtIODevice *vdev, uint32_t addr)
1324 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1325 uint8_t val;
1327 if (addr + sizeof(val) > vdev->config_len) {
1328 return (uint32_t)-1;
1331 k->get_config(vdev, vdev->config);
1333 val = ldub_p(vdev->config + addr);
1334 return val;
1337 uint32_t virtio_config_modern_readw(VirtIODevice *vdev, uint32_t addr)
1339 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1340 uint16_t val;
1342 if (addr + sizeof(val) > vdev->config_len) {
1343 return (uint32_t)-1;
1346 k->get_config(vdev, vdev->config);
1348 val = lduw_le_p(vdev->config + addr);
1349 return val;
1352 uint32_t virtio_config_modern_readl(VirtIODevice *vdev, uint32_t addr)
1354 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1355 uint32_t val;
1357 if (addr + sizeof(val) > vdev->config_len) {
1358 return (uint32_t)-1;
1361 k->get_config(vdev, vdev->config);
1363 val = ldl_le_p(vdev->config + addr);
1364 return val;
1367 void virtio_config_modern_writeb(VirtIODevice *vdev,
1368 uint32_t addr, uint32_t data)
1370 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1371 uint8_t val = data;
1373 if (addr + sizeof(val) > vdev->config_len) {
1374 return;
1377 stb_p(vdev->config + addr, val);
1379 if (k->set_config) {
1380 k->set_config(vdev, vdev->config);
1384 void virtio_config_modern_writew(VirtIODevice *vdev,
1385 uint32_t addr, uint32_t data)
1387 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1388 uint16_t val = data;
1390 if (addr + sizeof(val) > vdev->config_len) {
1391 return;
1394 stw_le_p(vdev->config + addr, val);
1396 if (k->set_config) {
1397 k->set_config(vdev, vdev->config);
1401 void virtio_config_modern_writel(VirtIODevice *vdev,
1402 uint32_t addr, uint32_t data)
1404 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1405 uint32_t val = data;
1407 if (addr + sizeof(val) > vdev->config_len) {
1408 return;
1411 stl_le_p(vdev->config + addr, val);
1413 if (k->set_config) {
1414 k->set_config(vdev, vdev->config);
1418 void virtio_queue_set_addr(VirtIODevice *vdev, int n, hwaddr addr)
1420 if (!vdev->vq[n].vring.num) {
1421 return;
1423 vdev->vq[n].vring.desc = addr;
1424 virtio_queue_update_rings(vdev, n);
1427 hwaddr virtio_queue_get_addr(VirtIODevice *vdev, int n)
1429 return vdev->vq[n].vring.desc;
1432 void virtio_queue_set_rings(VirtIODevice *vdev, int n, hwaddr desc,
1433 hwaddr avail, hwaddr used)
1435 if (!vdev->vq[n].vring.num) {
1436 return;
1438 vdev->vq[n].vring.desc = desc;
1439 vdev->vq[n].vring.avail = avail;
1440 vdev->vq[n].vring.used = used;
1441 virtio_init_region_cache(vdev, n);
1444 void virtio_queue_set_num(VirtIODevice *vdev, int n, int num)
1446 /* Don't allow guest to flip queue between existent and
1447 * nonexistent states, or to set it to an invalid size.
1449 if (!!num != !!vdev->vq[n].vring.num ||
1450 num > VIRTQUEUE_MAX_SIZE ||
1451 num < 0) {
1452 return;
1454 vdev->vq[n].vring.num = num;
1457 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector)
1459 return QLIST_FIRST(&vdev->vector_queues[vector]);
1462 VirtQueue *virtio_vector_next_queue(VirtQueue *vq)
1464 return QLIST_NEXT(vq, node);
1467 int virtio_queue_get_num(VirtIODevice *vdev, int n)
1469 return vdev->vq[n].vring.num;
1472 int virtio_queue_get_max_num(VirtIODevice *vdev, int n)
1474 return vdev->vq[n].vring.num_default;
1477 int virtio_get_num_queues(VirtIODevice *vdev)
1479 int i;
1481 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1482 if (!virtio_queue_get_num(vdev, i)) {
1483 break;
1487 return i;
1490 void virtio_queue_set_align(VirtIODevice *vdev, int n, int align)
1492 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1493 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1495 /* virtio-1 compliant devices cannot change the alignment */
1496 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1497 error_report("tried to modify queue alignment for virtio-1 device");
1498 return;
1500 /* Check that the transport told us it was going to do this
1501 * (so a buggy transport will immediately assert rather than
1502 * silently failing to migrate this state)
1504 assert(k->has_variable_vring_alignment);
1506 if (align) {
1507 vdev->vq[n].vring.align = align;
1508 virtio_queue_update_rings(vdev, n);
1512 static bool virtio_queue_notify_aio_vq(VirtQueue *vq)
1514 if (vq->vring.desc && vq->handle_aio_output) {
1515 VirtIODevice *vdev = vq->vdev;
1517 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1518 return vq->handle_aio_output(vdev, vq);
1521 return false;
1524 static void virtio_queue_notify_vq(VirtQueue *vq)
1526 if (vq->vring.desc && vq->handle_output) {
1527 VirtIODevice *vdev = vq->vdev;
1529 if (unlikely(vdev->broken)) {
1530 return;
1533 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1534 vq->handle_output(vdev, vq);
1538 void virtio_queue_notify(VirtIODevice *vdev, int n)
1540 VirtQueue *vq = &vdev->vq[n];
1542 if (unlikely(!vq->vring.desc || vdev->broken)) {
1543 return;
1546 trace_virtio_queue_notify(vdev, vq - vdev->vq, vq);
1547 if (vq->handle_aio_output) {
1548 event_notifier_set(&vq->host_notifier);
1549 } else if (vq->handle_output) {
1550 vq->handle_output(vdev, vq);
1554 uint16_t virtio_queue_vector(VirtIODevice *vdev, int n)
1556 return n < VIRTIO_QUEUE_MAX ? vdev->vq[n].vector :
1557 VIRTIO_NO_VECTOR;
1560 void virtio_queue_set_vector(VirtIODevice *vdev, int n, uint16_t vector)
1562 VirtQueue *vq = &vdev->vq[n];
1564 if (n < VIRTIO_QUEUE_MAX) {
1565 if (vdev->vector_queues &&
1566 vdev->vq[n].vector != VIRTIO_NO_VECTOR) {
1567 QLIST_REMOVE(vq, node);
1569 vdev->vq[n].vector = vector;
1570 if (vdev->vector_queues &&
1571 vector != VIRTIO_NO_VECTOR) {
1572 QLIST_INSERT_HEAD(&vdev->vector_queues[vector], vq, node);
1577 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
1578 VirtIOHandleOutput handle_output)
1580 int i;
1582 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1583 if (vdev->vq[i].vring.num == 0)
1584 break;
1587 if (i == VIRTIO_QUEUE_MAX || queue_size > VIRTQUEUE_MAX_SIZE)
1588 abort();
1590 vdev->vq[i].vring.num = queue_size;
1591 vdev->vq[i].vring.num_default = queue_size;
1592 vdev->vq[i].vring.align = VIRTIO_PCI_VRING_ALIGN;
1593 vdev->vq[i].handle_output = handle_output;
1594 vdev->vq[i].handle_aio_output = NULL;
1596 return &vdev->vq[i];
1599 void virtio_del_queue(VirtIODevice *vdev, int n)
1601 if (n < 0 || n >= VIRTIO_QUEUE_MAX) {
1602 abort();
1605 vdev->vq[n].vring.num = 0;
1606 vdev->vq[n].vring.num_default = 0;
1609 static void virtio_set_isr(VirtIODevice *vdev, int value)
1611 uint8_t old = atomic_read(&vdev->isr);
1613 /* Do not write ISR if it does not change, so that its cacheline remains
1614 * shared in the common case where the guest does not read it.
1616 if ((old & value) != value) {
1617 atomic_or(&vdev->isr, value);
1621 /* Called within rcu_read_lock(). */
1622 static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq)
1624 uint16_t old, new;
1625 bool v;
1626 /* We need to expose used array entries before checking used event. */
1627 smp_mb();
1628 /* Always notify when queue is empty (when feature acknowledge) */
1629 if (virtio_vdev_has_feature(vdev, VIRTIO_F_NOTIFY_ON_EMPTY) &&
1630 !vq->inuse && virtio_queue_empty(vq)) {
1631 return true;
1634 if (!virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX)) {
1635 return !(vring_avail_flags(vq) & VRING_AVAIL_F_NO_INTERRUPT);
1638 v = vq->signalled_used_valid;
1639 vq->signalled_used_valid = true;
1640 old = vq->signalled_used;
1641 new = vq->signalled_used = vq->used_idx;
1642 return !v || vring_need_event(vring_get_used_event(vq), new, old);
1645 void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)
1647 bool should_notify;
1648 rcu_read_lock();
1649 should_notify = virtio_should_notify(vdev, vq);
1650 rcu_read_unlock();
1652 if (!should_notify) {
1653 return;
1656 trace_virtio_notify_irqfd(vdev, vq);
1659 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
1660 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
1661 * incorrectly polling this bit during crashdump and hibernation
1662 * in MSI mode, causing a hang if this bit is never updated.
1663 * Recent releases of Windows do not really shut down, but rather
1664 * log out and hibernate to make the next startup faster. Hence,
1665 * this manifested as a more serious hang during shutdown with
1667 * Next driver release from 2016 fixed this problem, so working around it
1668 * is not a must, but it's easy to do so let's do it here.
1670 * Note: it's safe to update ISR from any thread as it was switched
1671 * to an atomic operation.
1673 virtio_set_isr(vq->vdev, 0x1);
1674 event_notifier_set(&vq->guest_notifier);
1677 static void virtio_irq(VirtQueue *vq)
1679 virtio_set_isr(vq->vdev, 0x1);
1680 virtio_notify_vector(vq->vdev, vq->vector);
1683 void virtio_notify(VirtIODevice *vdev, VirtQueue *vq)
1685 bool should_notify;
1686 rcu_read_lock();
1687 should_notify = virtio_should_notify(vdev, vq);
1688 rcu_read_unlock();
1690 if (!should_notify) {
1691 return;
1694 trace_virtio_notify(vdev, vq);
1695 virtio_irq(vq);
1698 void virtio_notify_config(VirtIODevice *vdev)
1700 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
1701 return;
1703 virtio_set_isr(vdev, 0x3);
1704 vdev->generation++;
1705 virtio_notify_vector(vdev, vdev->config_vector);
1708 static bool virtio_device_endian_needed(void *opaque)
1710 VirtIODevice *vdev = opaque;
1712 assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
1713 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1714 return vdev->device_endian != virtio_default_endian();
1716 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
1717 return vdev->device_endian != VIRTIO_DEVICE_ENDIAN_LITTLE;
1720 static bool virtio_64bit_features_needed(void *opaque)
1722 VirtIODevice *vdev = opaque;
1724 return (vdev->host_features >> 32) != 0;
1727 static bool virtio_virtqueue_needed(void *opaque)
1729 VirtIODevice *vdev = opaque;
1731 return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1);
1734 static bool virtio_ringsize_needed(void *opaque)
1736 VirtIODevice *vdev = opaque;
1737 int i;
1739 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1740 if (vdev->vq[i].vring.num != vdev->vq[i].vring.num_default) {
1741 return true;
1744 return false;
1747 static bool virtio_extra_state_needed(void *opaque)
1749 VirtIODevice *vdev = opaque;
1750 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1751 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1753 return k->has_extra_state &&
1754 k->has_extra_state(qbus->parent);
1757 static bool virtio_broken_needed(void *opaque)
1759 VirtIODevice *vdev = opaque;
1761 return vdev->broken;
1764 static const VMStateDescription vmstate_virtqueue = {
1765 .name = "virtqueue_state",
1766 .version_id = 1,
1767 .minimum_version_id = 1,
1768 .fields = (VMStateField[]) {
1769 VMSTATE_UINT64(vring.avail, struct VirtQueue),
1770 VMSTATE_UINT64(vring.used, struct VirtQueue),
1771 VMSTATE_END_OF_LIST()
1775 static const VMStateDescription vmstate_virtio_virtqueues = {
1776 .name = "virtio/virtqueues",
1777 .version_id = 1,
1778 .minimum_version_id = 1,
1779 .needed = &virtio_virtqueue_needed,
1780 .fields = (VMStateField[]) {
1781 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1782 VIRTIO_QUEUE_MAX, 0, vmstate_virtqueue, VirtQueue),
1783 VMSTATE_END_OF_LIST()
1787 static const VMStateDescription vmstate_ringsize = {
1788 .name = "ringsize_state",
1789 .version_id = 1,
1790 .minimum_version_id = 1,
1791 .fields = (VMStateField[]) {
1792 VMSTATE_UINT32(vring.num_default, struct VirtQueue),
1793 VMSTATE_END_OF_LIST()
1797 static const VMStateDescription vmstate_virtio_ringsize = {
1798 .name = "virtio/ringsize",
1799 .version_id = 1,
1800 .minimum_version_id = 1,
1801 .needed = &virtio_ringsize_needed,
1802 .fields = (VMStateField[]) {
1803 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice,
1804 VIRTIO_QUEUE_MAX, 0, vmstate_ringsize, VirtQueue),
1805 VMSTATE_END_OF_LIST()
1809 static int get_extra_state(QEMUFile *f, void *pv, size_t size,
1810 VMStateField *field)
1812 VirtIODevice *vdev = pv;
1813 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1814 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1816 if (!k->load_extra_state) {
1817 return -1;
1818 } else {
1819 return k->load_extra_state(qbus->parent, f);
1823 static int put_extra_state(QEMUFile *f, void *pv, size_t size,
1824 VMStateField *field, QJSON *vmdesc)
1826 VirtIODevice *vdev = pv;
1827 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1828 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1830 k->save_extra_state(qbus->parent, f);
1831 return 0;
1834 static const VMStateInfo vmstate_info_extra_state = {
1835 .name = "virtqueue_extra_state",
1836 .get = get_extra_state,
1837 .put = put_extra_state,
1840 static const VMStateDescription vmstate_virtio_extra_state = {
1841 .name = "virtio/extra_state",
1842 .version_id = 1,
1843 .minimum_version_id = 1,
1844 .needed = &virtio_extra_state_needed,
1845 .fields = (VMStateField[]) {
1847 .name = "extra_state",
1848 .version_id = 0,
1849 .field_exists = NULL,
1850 .size = 0,
1851 .info = &vmstate_info_extra_state,
1852 .flags = VMS_SINGLE,
1853 .offset = 0,
1855 VMSTATE_END_OF_LIST()
1859 static const VMStateDescription vmstate_virtio_device_endian = {
1860 .name = "virtio/device_endian",
1861 .version_id = 1,
1862 .minimum_version_id = 1,
1863 .needed = &virtio_device_endian_needed,
1864 .fields = (VMStateField[]) {
1865 VMSTATE_UINT8(device_endian, VirtIODevice),
1866 VMSTATE_END_OF_LIST()
1870 static const VMStateDescription vmstate_virtio_64bit_features = {
1871 .name = "virtio/64bit_features",
1872 .version_id = 1,
1873 .minimum_version_id = 1,
1874 .needed = &virtio_64bit_features_needed,
1875 .fields = (VMStateField[]) {
1876 VMSTATE_UINT64(guest_features, VirtIODevice),
1877 VMSTATE_END_OF_LIST()
1881 static const VMStateDescription vmstate_virtio_broken = {
1882 .name = "virtio/broken",
1883 .version_id = 1,
1884 .minimum_version_id = 1,
1885 .needed = &virtio_broken_needed,
1886 .fields = (VMStateField[]) {
1887 VMSTATE_BOOL(broken, VirtIODevice),
1888 VMSTATE_END_OF_LIST()
1892 static const VMStateDescription vmstate_virtio = {
1893 .name = "virtio",
1894 .version_id = 1,
1895 .minimum_version_id = 1,
1896 .minimum_version_id_old = 1,
1897 .fields = (VMStateField[]) {
1898 VMSTATE_END_OF_LIST()
1900 .subsections = (const VMStateDescription*[]) {
1901 &vmstate_virtio_device_endian,
1902 &vmstate_virtio_64bit_features,
1903 &vmstate_virtio_virtqueues,
1904 &vmstate_virtio_ringsize,
1905 &vmstate_virtio_broken,
1906 &vmstate_virtio_extra_state,
1907 NULL
1911 int virtio_save(VirtIODevice *vdev, QEMUFile *f)
1913 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
1914 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
1915 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1916 uint32_t guest_features_lo = (vdev->guest_features & 0xffffffff);
1917 int i;
1919 if (k->save_config) {
1920 k->save_config(qbus->parent, f);
1923 qemu_put_8s(f, &vdev->status);
1924 qemu_put_8s(f, &vdev->isr);
1925 qemu_put_be16s(f, &vdev->queue_sel);
1926 qemu_put_be32s(f, &guest_features_lo);
1927 qemu_put_be32(f, vdev->config_len);
1928 qemu_put_buffer(f, vdev->config, vdev->config_len);
1930 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1931 if (vdev->vq[i].vring.num == 0)
1932 break;
1935 qemu_put_be32(f, i);
1937 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
1938 if (vdev->vq[i].vring.num == 0)
1939 break;
1941 qemu_put_be32(f, vdev->vq[i].vring.num);
1942 if (k->has_variable_vring_alignment) {
1943 qemu_put_be32(f, vdev->vq[i].vring.align);
1946 * Save desc now, the rest of the ring addresses are saved in
1947 * subsections for VIRTIO-1 devices.
1949 qemu_put_be64(f, vdev->vq[i].vring.desc);
1950 qemu_put_be16s(f, &vdev->vq[i].last_avail_idx);
1951 if (k->save_queue) {
1952 k->save_queue(qbus->parent, i, f);
1956 if (vdc->save != NULL) {
1957 vdc->save(vdev, f);
1960 if (vdc->vmsd) {
1961 int ret = vmstate_save_state(f, vdc->vmsd, vdev, NULL);
1962 if (ret) {
1963 return ret;
1967 /* Subsections */
1968 return vmstate_save_state(f, &vmstate_virtio, vdev, NULL);
1971 /* A wrapper for use as a VMState .put function */
1972 static int virtio_device_put(QEMUFile *f, void *opaque, size_t size,
1973 VMStateField *field, QJSON *vmdesc)
1975 return virtio_save(VIRTIO_DEVICE(opaque), f);
1978 /* A wrapper for use as a VMState .get function */
1979 static int virtio_device_get(QEMUFile *f, void *opaque, size_t size,
1980 VMStateField *field)
1982 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
1983 DeviceClass *dc = DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev));
1985 return virtio_load(vdev, f, dc->vmsd->version_id);
1988 const VMStateInfo virtio_vmstate_info = {
1989 .name = "virtio",
1990 .get = virtio_device_get,
1991 .put = virtio_device_put,
1994 static int virtio_set_features_nocheck(VirtIODevice *vdev, uint64_t val)
1996 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1997 bool bad = (val & ~(vdev->host_features)) != 0;
1999 val &= vdev->host_features;
2000 if (k->set_features) {
2001 k->set_features(vdev, val);
2003 vdev->guest_features = val;
2004 return bad ? -1 : 0;
2007 int virtio_set_features(VirtIODevice *vdev, uint64_t val)
2010 * The driver must not attempt to set features after feature negotiation
2011 * has finished.
2013 if (vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) {
2014 return -EINVAL;
2016 return virtio_set_features_nocheck(vdev, val);
2019 int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
2021 int i, ret;
2022 int32_t config_len;
2023 uint32_t num;
2024 uint32_t features;
2025 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2026 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2027 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
2030 * We poison the endianness to ensure it does not get used before
2031 * subsections have been loaded.
2033 vdev->device_endian = VIRTIO_DEVICE_ENDIAN_UNKNOWN;
2035 if (k->load_config) {
2036 ret = k->load_config(qbus->parent, f);
2037 if (ret)
2038 return ret;
2041 qemu_get_8s(f, &vdev->status);
2042 qemu_get_8s(f, &vdev->isr);
2043 qemu_get_be16s(f, &vdev->queue_sel);
2044 if (vdev->queue_sel >= VIRTIO_QUEUE_MAX) {
2045 return -1;
2047 qemu_get_be32s(f, &features);
2050 * Temporarily set guest_features low bits - needed by
2051 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
2052 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
2054 * Note: devices should always test host features in future - don't create
2055 * new dependencies like this.
2057 vdev->guest_features = features;
2059 config_len = qemu_get_be32(f);
2062 * There are cases where the incoming config can be bigger or smaller
2063 * than what we have; so load what we have space for, and skip
2064 * any excess that's in the stream.
2066 qemu_get_buffer(f, vdev->config, MIN(config_len, vdev->config_len));
2068 while (config_len > vdev->config_len) {
2069 qemu_get_byte(f);
2070 config_len--;
2073 num = qemu_get_be32(f);
2075 if (num > VIRTIO_QUEUE_MAX) {
2076 error_report("Invalid number of virtqueues: 0x%x", num);
2077 return -1;
2080 for (i = 0; i < num; i++) {
2081 vdev->vq[i].vring.num = qemu_get_be32(f);
2082 if (k->has_variable_vring_alignment) {
2083 vdev->vq[i].vring.align = qemu_get_be32(f);
2085 vdev->vq[i].vring.desc = qemu_get_be64(f);
2086 qemu_get_be16s(f, &vdev->vq[i].last_avail_idx);
2087 vdev->vq[i].signalled_used_valid = false;
2088 vdev->vq[i].notification = true;
2090 if (!vdev->vq[i].vring.desc && vdev->vq[i].last_avail_idx) {
2091 error_report("VQ %d address 0x0 "
2092 "inconsistent with Host index 0x%x",
2093 i, vdev->vq[i].last_avail_idx);
2094 return -1;
2096 if (k->load_queue) {
2097 ret = k->load_queue(qbus->parent, i, f);
2098 if (ret)
2099 return ret;
2103 virtio_notify_vector(vdev, VIRTIO_NO_VECTOR);
2105 if (vdc->load != NULL) {
2106 ret = vdc->load(vdev, f, version_id);
2107 if (ret) {
2108 return ret;
2112 if (vdc->vmsd) {
2113 ret = vmstate_load_state(f, vdc->vmsd, vdev, version_id);
2114 if (ret) {
2115 return ret;
2119 /* Subsections */
2120 ret = vmstate_load_state(f, &vmstate_virtio, vdev, 1);
2121 if (ret) {
2122 return ret;
2125 if (vdev->device_endian == VIRTIO_DEVICE_ENDIAN_UNKNOWN) {
2126 vdev->device_endian = virtio_default_endian();
2129 if (virtio_64bit_features_needed(vdev)) {
2131 * Subsection load filled vdev->guest_features. Run them
2132 * through virtio_set_features to sanity-check them against
2133 * host_features.
2135 uint64_t features64 = vdev->guest_features;
2136 if (virtio_set_features_nocheck(vdev, features64) < 0) {
2137 error_report("Features 0x%" PRIx64 " unsupported. "
2138 "Allowed features: 0x%" PRIx64,
2139 features64, vdev->host_features);
2140 return -1;
2142 } else {
2143 if (virtio_set_features_nocheck(vdev, features) < 0) {
2144 error_report("Features 0x%x unsupported. "
2145 "Allowed features: 0x%" PRIx64,
2146 features, vdev->host_features);
2147 return -1;
2151 rcu_read_lock();
2152 for (i = 0; i < num; i++) {
2153 if (vdev->vq[i].vring.desc) {
2154 uint16_t nheads;
2157 * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
2158 * only the region cache needs to be set up. Legacy devices need
2159 * to calculate used and avail ring addresses based on the desc
2160 * address.
2162 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2163 virtio_init_region_cache(vdev, i);
2164 } else {
2165 virtio_queue_update_rings(vdev, i);
2168 nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
2169 /* Check it isn't doing strange things with descriptor numbers. */
2170 if (nheads > vdev->vq[i].vring.num) {
2171 error_report("VQ %d size 0x%x Guest index 0x%x "
2172 "inconsistent with Host index 0x%x: delta 0x%x",
2173 i, vdev->vq[i].vring.num,
2174 vring_avail_idx(&vdev->vq[i]),
2175 vdev->vq[i].last_avail_idx, nheads);
2176 return -1;
2178 vdev->vq[i].used_idx = vring_used_idx(&vdev->vq[i]);
2179 vdev->vq[i].shadow_avail_idx = vring_avail_idx(&vdev->vq[i]);
2182 * Some devices migrate VirtQueueElements that have been popped
2183 * from the avail ring but not yet returned to the used ring.
2184 * Since max ring size < UINT16_MAX it's safe to use modulo
2185 * UINT16_MAX + 1 subtraction.
2187 vdev->vq[i].inuse = (uint16_t)(vdev->vq[i].last_avail_idx -
2188 vdev->vq[i].used_idx);
2189 if (vdev->vq[i].inuse > vdev->vq[i].vring.num) {
2190 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
2191 "used_idx 0x%x",
2192 i, vdev->vq[i].vring.num,
2193 vdev->vq[i].last_avail_idx,
2194 vdev->vq[i].used_idx);
2195 return -1;
2199 rcu_read_unlock();
2201 return 0;
2204 void virtio_cleanup(VirtIODevice *vdev)
2206 qemu_del_vm_change_state_handler(vdev->vmstate);
2209 static void virtio_vmstate_change(void *opaque, int running, RunState state)
2211 VirtIODevice *vdev = opaque;
2212 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2213 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2214 bool backend_run = running && (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK);
2215 vdev->vm_running = running;
2217 if (backend_run) {
2218 virtio_set_status(vdev, vdev->status);
2221 if (k->vmstate_change) {
2222 k->vmstate_change(qbus->parent, backend_run);
2225 if (!backend_run) {
2226 virtio_set_status(vdev, vdev->status);
2230 void virtio_instance_init_common(Object *proxy_obj, void *data,
2231 size_t vdev_size, const char *vdev_name)
2233 DeviceState *vdev = data;
2235 object_initialize(vdev, vdev_size, vdev_name);
2236 object_property_add_child(proxy_obj, "virtio-backend", OBJECT(vdev), NULL);
2237 object_unref(OBJECT(vdev));
2238 qdev_alias_all_properties(vdev, proxy_obj);
2241 void virtio_init(VirtIODevice *vdev, const char *name,
2242 uint16_t device_id, size_t config_size)
2244 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2245 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2246 int i;
2247 int nvectors = k->query_nvectors ? k->query_nvectors(qbus->parent) : 0;
2249 if (nvectors) {
2250 vdev->vector_queues =
2251 g_malloc0(sizeof(*vdev->vector_queues) * nvectors);
2254 vdev->device_id = device_id;
2255 vdev->status = 0;
2256 atomic_set(&vdev->isr, 0);
2257 vdev->queue_sel = 0;
2258 vdev->config_vector = VIRTIO_NO_VECTOR;
2259 vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_QUEUE_MAX);
2260 vdev->vm_running = runstate_is_running();
2261 vdev->broken = false;
2262 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2263 vdev->vq[i].vector = VIRTIO_NO_VECTOR;
2264 vdev->vq[i].vdev = vdev;
2265 vdev->vq[i].queue_index = i;
2268 vdev->name = name;
2269 vdev->config_len = config_size;
2270 if (vdev->config_len) {
2271 vdev->config = g_malloc0(config_size);
2272 } else {
2273 vdev->config = NULL;
2275 vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
2276 vdev);
2277 vdev->device_endian = virtio_default_endian();
2278 vdev->use_guest_notifier_mask = true;
2281 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n)
2283 return vdev->vq[n].vring.desc;
2286 hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n)
2288 return vdev->vq[n].vring.avail;
2291 hwaddr virtio_queue_get_used_addr(VirtIODevice *vdev, int n)
2293 return vdev->vq[n].vring.used;
2296 hwaddr virtio_queue_get_desc_size(VirtIODevice *vdev, int n)
2298 return sizeof(VRingDesc) * vdev->vq[n].vring.num;
2301 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n)
2303 return offsetof(VRingAvail, ring) +
2304 sizeof(uint16_t) * vdev->vq[n].vring.num;
2307 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n)
2309 return offsetof(VRingUsed, ring) +
2310 sizeof(VRingUsedElem) * vdev->vq[n].vring.num;
2313 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n)
2315 return vdev->vq[n].last_avail_idx;
2318 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx)
2320 vdev->vq[n].last_avail_idx = idx;
2321 vdev->vq[n].shadow_avail_idx = idx;
2324 void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n)
2326 rcu_read_lock();
2327 if (vdev->vq[n].vring.desc) {
2328 vdev->vq[n].last_avail_idx = vring_used_idx(&vdev->vq[n]);
2329 vdev->vq[n].shadow_avail_idx = vdev->vq[n].last_avail_idx;
2331 rcu_read_unlock();
2334 void virtio_queue_update_used_idx(VirtIODevice *vdev, int n)
2336 rcu_read_lock();
2337 if (vdev->vq[n].vring.desc) {
2338 vdev->vq[n].used_idx = vring_used_idx(&vdev->vq[n]);
2340 rcu_read_unlock();
2343 void virtio_queue_invalidate_signalled_used(VirtIODevice *vdev, int n)
2345 vdev->vq[n].signalled_used_valid = false;
2348 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)
2350 return vdev->vq + n;
2353 uint16_t virtio_get_queue_index(VirtQueue *vq)
2355 return vq->queue_index;
2358 static void virtio_queue_guest_notifier_read(EventNotifier *n)
2360 VirtQueue *vq = container_of(n, VirtQueue, guest_notifier);
2361 if (event_notifier_test_and_clear(n)) {
2362 virtio_irq(vq);
2366 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
2367 bool with_irqfd)
2369 if (assign && !with_irqfd) {
2370 event_notifier_set_handler(&vq->guest_notifier,
2371 virtio_queue_guest_notifier_read);
2372 } else {
2373 event_notifier_set_handler(&vq->guest_notifier, NULL);
2375 if (!assign) {
2376 /* Test and clear notifier before closing it,
2377 * in case poll callback didn't have time to run. */
2378 virtio_queue_guest_notifier_read(&vq->guest_notifier);
2382 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
2384 return &vq->guest_notifier;
2387 static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
2389 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2390 if (event_notifier_test_and_clear(n)) {
2391 virtio_queue_notify_aio_vq(vq);
2395 static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier *n)
2397 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2399 virtio_queue_set_notification(vq, 0);
2402 static bool virtio_queue_host_notifier_aio_poll(void *opaque)
2404 EventNotifier *n = opaque;
2405 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2406 bool progress;
2408 if (!vq->vring.desc || virtio_queue_empty(vq)) {
2409 return false;
2412 progress = virtio_queue_notify_aio_vq(vq);
2414 /* In case the handler function re-enabled notifications */
2415 virtio_queue_set_notification(vq, 0);
2416 return progress;
2419 static void virtio_queue_host_notifier_aio_poll_end(EventNotifier *n)
2421 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2423 /* Caller polls once more after this to catch requests that race with us */
2424 virtio_queue_set_notification(vq, 1);
2427 void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
2428 VirtIOHandleAIOOutput handle_output)
2430 if (handle_output) {
2431 vq->handle_aio_output = handle_output;
2432 aio_set_event_notifier(ctx, &vq->host_notifier, true,
2433 virtio_queue_host_notifier_aio_read,
2434 virtio_queue_host_notifier_aio_poll);
2435 aio_set_event_notifier_poll(ctx, &vq->host_notifier,
2436 virtio_queue_host_notifier_aio_poll_begin,
2437 virtio_queue_host_notifier_aio_poll_end);
2438 } else {
2439 aio_set_event_notifier(ctx, &vq->host_notifier, true, NULL, NULL);
2440 /* Test and clear notifier before after disabling event,
2441 * in case poll callback didn't have time to run. */
2442 virtio_queue_host_notifier_aio_read(&vq->host_notifier);
2443 vq->handle_aio_output = NULL;
2447 void virtio_queue_host_notifier_read(EventNotifier *n)
2449 VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
2450 if (event_notifier_test_and_clear(n)) {
2451 virtio_queue_notify_vq(vq);
2455 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
2457 return &vq->host_notifier;
2460 int virtio_queue_set_host_notifier_mr(VirtIODevice *vdev, int n,
2461 MemoryRegion *mr, bool assign)
2463 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2464 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
2466 if (k->set_host_notifier_mr) {
2467 return k->set_host_notifier_mr(qbus->parent, n, mr, assign);
2470 return -1;
2473 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
2475 g_free(vdev->bus_name);
2476 vdev->bus_name = g_strdup(bus_name);
2479 void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice *vdev, const char *fmt, ...)
2481 va_list ap;
2483 va_start(ap, fmt);
2484 error_vreport(fmt, ap);
2485 va_end(ap);
2487 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2488 vdev->status = vdev->status | VIRTIO_CONFIG_S_NEEDS_RESET;
2489 virtio_notify_config(vdev);
2492 vdev->broken = true;
2495 static void virtio_memory_listener_commit(MemoryListener *listener)
2497 VirtIODevice *vdev = container_of(listener, VirtIODevice, listener);
2498 int i;
2500 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2501 if (vdev->vq[i].vring.num == 0) {
2502 break;
2504 virtio_init_region_cache(vdev, i);
2508 static void virtio_device_realize(DeviceState *dev, Error **errp)
2510 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
2511 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
2512 Error *err = NULL;
2514 /* Devices should either use vmsd or the load/save methods */
2515 assert(!vdc->vmsd || !vdc->load);
2517 if (vdc->realize != NULL) {
2518 vdc->realize(dev, &err);
2519 if (err != NULL) {
2520 error_propagate(errp, err);
2521 return;
2525 virtio_bus_device_plugged(vdev, &err);
2526 if (err != NULL) {
2527 error_propagate(errp, err);
2528 vdc->unrealize(dev, NULL);
2529 return;
2532 vdev->listener.commit = virtio_memory_listener_commit;
2533 memory_listener_register(&vdev->listener, vdev->dma_as);
2536 static void virtio_device_unrealize(DeviceState *dev, Error **errp)
2538 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
2539 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(dev);
2540 Error *err = NULL;
2542 virtio_bus_device_unplugged(vdev);
2544 if (vdc->unrealize != NULL) {
2545 vdc->unrealize(dev, &err);
2546 if (err != NULL) {
2547 error_propagate(errp, err);
2548 return;
2552 g_free(vdev->bus_name);
2553 vdev->bus_name = NULL;
2556 static void virtio_device_free_virtqueues(VirtIODevice *vdev)
2558 int i;
2559 if (!vdev->vq) {
2560 return;
2563 for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
2564 if (vdev->vq[i].vring.num == 0) {
2565 break;
2567 virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
2569 g_free(vdev->vq);
2572 static void virtio_device_instance_finalize(Object *obj)
2574 VirtIODevice *vdev = VIRTIO_DEVICE(obj);
2576 memory_listener_unregister(&vdev->listener);
2577 virtio_device_free_virtqueues(vdev);
2579 g_free(vdev->config);
2580 g_free(vdev->vector_queues);
2583 static Property virtio_properties[] = {
2584 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice, host_features),
2585 DEFINE_PROP_END_OF_LIST(),
2588 static int virtio_device_start_ioeventfd_impl(VirtIODevice *vdev)
2590 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
2591 int i, n, r, err;
2593 memory_region_transaction_begin();
2594 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2595 VirtQueue *vq = &vdev->vq[n];
2596 if (!virtio_queue_get_num(vdev, n)) {
2597 continue;
2599 r = virtio_bus_set_host_notifier(qbus, n, true);
2600 if (r < 0) {
2601 err = r;
2602 goto assign_error;
2604 event_notifier_set_handler(&vq->host_notifier,
2605 virtio_queue_host_notifier_read);
2608 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2609 /* Kick right away to begin processing requests already in vring */
2610 VirtQueue *vq = &vdev->vq[n];
2611 if (!vq->vring.num) {
2612 continue;
2614 event_notifier_set(&vq->host_notifier);
2616 memory_region_transaction_commit();
2617 return 0;
2619 assign_error:
2620 i = n; /* save n for a second iteration after transaction is committed. */
2621 while (--n >= 0) {
2622 VirtQueue *vq = &vdev->vq[n];
2623 if (!virtio_queue_get_num(vdev, n)) {
2624 continue;
2627 event_notifier_set_handler(&vq->host_notifier, NULL);
2628 r = virtio_bus_set_host_notifier(qbus, n, false);
2629 assert(r >= 0);
2631 memory_region_transaction_commit();
2633 while (--i >= 0) {
2634 if (!virtio_queue_get_num(vdev, i)) {
2635 continue;
2637 virtio_bus_cleanup_host_notifier(qbus, i);
2639 return err;
2642 int virtio_device_start_ioeventfd(VirtIODevice *vdev)
2644 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2645 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2647 return virtio_bus_start_ioeventfd(vbus);
2650 static void virtio_device_stop_ioeventfd_impl(VirtIODevice *vdev)
2652 VirtioBusState *qbus = VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev)));
2653 int n, r;
2655 memory_region_transaction_begin();
2656 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2657 VirtQueue *vq = &vdev->vq[n];
2659 if (!virtio_queue_get_num(vdev, n)) {
2660 continue;
2662 event_notifier_set_handler(&vq->host_notifier, NULL);
2663 r = virtio_bus_set_host_notifier(qbus, n, false);
2664 assert(r >= 0);
2666 memory_region_transaction_commit();
2668 for (n = 0; n < VIRTIO_QUEUE_MAX; n++) {
2669 if (!virtio_queue_get_num(vdev, n)) {
2670 continue;
2672 virtio_bus_cleanup_host_notifier(qbus, n);
2676 void virtio_device_stop_ioeventfd(VirtIODevice *vdev)
2678 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2679 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2681 virtio_bus_stop_ioeventfd(vbus);
2684 int virtio_device_grab_ioeventfd(VirtIODevice *vdev)
2686 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2687 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2689 return virtio_bus_grab_ioeventfd(vbus);
2692 void virtio_device_release_ioeventfd(VirtIODevice *vdev)
2694 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2695 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2697 virtio_bus_release_ioeventfd(vbus);
2700 static void virtio_device_class_init(ObjectClass *klass, void *data)
2702 /* Set the default value here. */
2703 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
2704 DeviceClass *dc = DEVICE_CLASS(klass);
2706 dc->realize = virtio_device_realize;
2707 dc->unrealize = virtio_device_unrealize;
2708 dc->bus_type = TYPE_VIRTIO_BUS;
2709 dc->props = virtio_properties;
2710 vdc->start_ioeventfd = virtio_device_start_ioeventfd_impl;
2711 vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
2713 vdc->legacy_features |= VIRTIO_LEGACY_FEATURES;
2716 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
2718 BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
2719 VirtioBusState *vbus = VIRTIO_BUS(qbus);
2721 return virtio_bus_ioeventfd_enabled(vbus);
2724 static const TypeInfo virtio_device_info = {
2725 .name = TYPE_VIRTIO_DEVICE,
2726 .parent = TYPE_DEVICE,
2727 .instance_size = sizeof(VirtIODevice),
2728 .class_init = virtio_device_class_init,
2729 .instance_finalize = virtio_device_instance_finalize,
2730 .abstract = true,
2731 .class_size = sizeof(VirtioDeviceClass),
2734 static void virtio_register_types(void)
2736 type_register_static(&virtio_device_info);
2739 type_init(virtio_register_types)