4 * Copyright IBM, Corp. 2007
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"
18 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/module.h"
22 #include "hw/virtio/virtio.h"
23 #include "migration/qemu-file-types.h"
24 #include "qemu/atomic.h"
25 #include "hw/virtio/virtio-bus.h"
26 #include "hw/qdev-properties.h"
27 #include "hw/virtio/virtio-access.h"
28 #include "sysemu/dma.h"
29 #include "sysemu/runstate.h"
30 #include "standard-headers/linux/virtio_ids.h"
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.
37 #define VIRTIO_PCI_VRING_ALIGN 4096
39 typedef struct VRingDesc
47 typedef struct VRingPackedDesc
{
54 typedef struct VRingAvail
61 typedef struct VRingUsedElem
67 typedef struct VRingUsed
74 typedef struct VRingMemoryRegionCaches
{
76 MemoryRegionCache desc
;
77 MemoryRegionCache avail
;
78 MemoryRegionCache used
;
79 } VRingMemoryRegionCaches
;
84 unsigned int num_default
;
89 VRingMemoryRegionCaches
*caches
;
92 typedef struct VRingPackedDescEvent
{
95 } VRingPackedDescEvent
;
100 VirtQueueElement
*used_elems
;
102 /* Next head to pop */
103 uint16_t last_avail_idx
;
104 bool last_avail_wrap_counter
;
106 /* Last avail_idx read from VQ. */
107 uint16_t shadow_avail_idx
;
108 bool shadow_avail_wrap_counter
;
111 bool used_wrap_counter
;
113 /* Last used index value we have signalled on */
114 uint16_t signalled_used
;
116 /* Last used index value we have signalled on */
117 bool signalled_used_valid
;
119 /* Notification enabled? */
122 uint16_t queue_index
;
127 VirtIOHandleOutput handle_output
;
128 VirtIOHandleAIOOutput handle_aio_output
;
130 EventNotifier guest_notifier
;
131 EventNotifier host_notifier
;
132 bool host_notifier_enabled
;
133 QLIST_ENTRY(VirtQueue
) node
;
136 static void virtio_free_region_cache(VRingMemoryRegionCaches
*caches
)
142 address_space_cache_destroy(&caches
->desc
);
143 address_space_cache_destroy(&caches
->avail
);
144 address_space_cache_destroy(&caches
->used
);
148 static void virtio_virtqueue_reset_region_cache(struct VirtQueue
*vq
)
150 VRingMemoryRegionCaches
*caches
;
152 caches
= qatomic_read(&vq
->vring
.caches
);
153 qatomic_rcu_set(&vq
->vring
.caches
, NULL
);
155 call_rcu(caches
, virtio_free_region_cache
, rcu
);
159 static void virtio_init_region_cache(VirtIODevice
*vdev
, int n
)
161 VirtQueue
*vq
= &vdev
->vq
[n
];
162 VRingMemoryRegionCaches
*old
= vq
->vring
.caches
;
163 VRingMemoryRegionCaches
*new = NULL
;
169 addr
= vq
->vring
.desc
;
173 new = g_new0(VRingMemoryRegionCaches
, 1);
174 size
= virtio_queue_get_desc_size(vdev
, n
);
175 packed
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
177 len
= address_space_cache_init(&new->desc
, vdev
->dma_as
,
180 virtio_error(vdev
, "Cannot map desc");
184 size
= virtio_queue_get_used_size(vdev
, n
);
185 len
= address_space_cache_init(&new->used
, vdev
->dma_as
,
186 vq
->vring
.used
, size
, true);
188 virtio_error(vdev
, "Cannot map used");
192 size
= virtio_queue_get_avail_size(vdev
, n
);
193 len
= address_space_cache_init(&new->avail
, vdev
->dma_as
,
194 vq
->vring
.avail
, size
, false);
196 virtio_error(vdev
, "Cannot map avail");
200 qatomic_rcu_set(&vq
->vring
.caches
, new);
202 call_rcu(old
, virtio_free_region_cache
, rcu
);
207 address_space_cache_destroy(&new->avail
);
209 address_space_cache_destroy(&new->used
);
211 address_space_cache_destroy(&new->desc
);
214 virtio_virtqueue_reset_region_cache(vq
);
217 /* virt queue functions */
218 void virtio_queue_update_rings(VirtIODevice
*vdev
, int n
)
220 VRing
*vring
= &vdev
->vq
[n
].vring
;
222 if (!vring
->num
|| !vring
->desc
|| !vring
->align
) {
223 /* not yet setup -> nothing to do */
226 vring
->avail
= vring
->desc
+ vring
->num
* sizeof(VRingDesc
);
227 vring
->used
= vring_align(vring
->avail
+
228 offsetof(VRingAvail
, ring
[vring
->num
]),
230 virtio_init_region_cache(vdev
, n
);
233 /* Called within rcu_read_lock(). */
234 static void vring_split_desc_read(VirtIODevice
*vdev
, VRingDesc
*desc
,
235 MemoryRegionCache
*cache
, int i
)
237 address_space_read_cached(cache
, i
* sizeof(VRingDesc
),
238 desc
, sizeof(VRingDesc
));
239 virtio_tswap64s(vdev
, &desc
->addr
);
240 virtio_tswap32s(vdev
, &desc
->len
);
241 virtio_tswap16s(vdev
, &desc
->flags
);
242 virtio_tswap16s(vdev
, &desc
->next
);
245 static void vring_packed_event_read(VirtIODevice
*vdev
,
246 MemoryRegionCache
*cache
,
247 VRingPackedDescEvent
*e
)
249 hwaddr off_off
= offsetof(VRingPackedDescEvent
, off_wrap
);
250 hwaddr off_flags
= offsetof(VRingPackedDescEvent
, flags
);
252 address_space_read_cached(cache
, off_flags
, &e
->flags
,
254 /* Make sure flags is seen before off_wrap */
256 address_space_read_cached(cache
, off_off
, &e
->off_wrap
,
257 sizeof(e
->off_wrap
));
258 virtio_tswap16s(vdev
, &e
->off_wrap
);
259 virtio_tswap16s(vdev
, &e
->flags
);
262 static void vring_packed_off_wrap_write(VirtIODevice
*vdev
,
263 MemoryRegionCache
*cache
,
266 hwaddr off
= offsetof(VRingPackedDescEvent
, off_wrap
);
268 virtio_tswap16s(vdev
, &off_wrap
);
269 address_space_write_cached(cache
, off
, &off_wrap
, sizeof(off_wrap
));
270 address_space_cache_invalidate(cache
, off
, sizeof(off_wrap
));
273 static void vring_packed_flags_write(VirtIODevice
*vdev
,
274 MemoryRegionCache
*cache
, uint16_t flags
)
276 hwaddr off
= offsetof(VRingPackedDescEvent
, flags
);
278 virtio_tswap16s(vdev
, &flags
);
279 address_space_write_cached(cache
, off
, &flags
, sizeof(flags
));
280 address_space_cache_invalidate(cache
, off
, sizeof(flags
));
283 /* Called within rcu_read_lock(). */
284 static VRingMemoryRegionCaches
*vring_get_region_caches(struct VirtQueue
*vq
)
286 return qatomic_rcu_read(&vq
->vring
.caches
);
289 /* Called within rcu_read_lock(). */
290 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
292 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
293 hwaddr pa
= offsetof(VRingAvail
, flags
);
299 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
302 /* Called within rcu_read_lock(). */
303 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
305 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
306 hwaddr pa
= offsetof(VRingAvail
, idx
);
312 vq
->shadow_avail_idx
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
313 return vq
->shadow_avail_idx
;
316 /* Called within rcu_read_lock(). */
317 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
319 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
320 hwaddr pa
= offsetof(VRingAvail
, ring
[i
]);
326 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
329 /* Called within rcu_read_lock(). */
330 static inline uint16_t vring_get_used_event(VirtQueue
*vq
)
332 return vring_avail_ring(vq
, vq
->vring
.num
);
335 /* Called within rcu_read_lock(). */
336 static inline void vring_used_write(VirtQueue
*vq
, VRingUsedElem
*uelem
,
339 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
340 hwaddr pa
= offsetof(VRingUsed
, ring
[i
]);
346 virtio_tswap32s(vq
->vdev
, &uelem
->id
);
347 virtio_tswap32s(vq
->vdev
, &uelem
->len
);
348 address_space_write_cached(&caches
->used
, pa
, uelem
, sizeof(VRingUsedElem
));
349 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(VRingUsedElem
));
352 /* Called within rcu_read_lock(). */
353 static uint16_t vring_used_idx(VirtQueue
*vq
)
355 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
356 hwaddr pa
= offsetof(VRingUsed
, idx
);
362 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
365 /* Called within rcu_read_lock(). */
366 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
368 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
369 hwaddr pa
= offsetof(VRingUsed
, idx
);
372 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
373 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
379 /* Called within rcu_read_lock(). */
380 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
382 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
383 VirtIODevice
*vdev
= vq
->vdev
;
384 hwaddr pa
= offsetof(VRingUsed
, flags
);
391 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
392 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
| mask
);
393 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
396 /* Called within rcu_read_lock(). */
397 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
399 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
400 VirtIODevice
*vdev
= vq
->vdev
;
401 hwaddr pa
= offsetof(VRingUsed
, flags
);
408 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
409 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
& ~mask
);
410 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
413 /* Called within rcu_read_lock(). */
414 static inline void vring_set_avail_event(VirtQueue
*vq
, uint16_t val
)
416 VRingMemoryRegionCaches
*caches
;
418 if (!vq
->notification
) {
422 caches
= vring_get_region_caches(vq
);
427 pa
= offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
428 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
429 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
432 static void virtio_queue_split_set_notification(VirtQueue
*vq
, int enable
)
434 RCU_READ_LOCK_GUARD();
436 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
437 vring_set_avail_event(vq
, vring_avail_idx(vq
));
439 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
441 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
444 /* Expose avail event/used flags before caller checks the avail idx. */
449 static void virtio_queue_packed_set_notification(VirtQueue
*vq
, int enable
)
452 VRingPackedDescEvent e
;
453 VRingMemoryRegionCaches
*caches
;
455 RCU_READ_LOCK_GUARD();
456 caches
= vring_get_region_caches(vq
);
461 vring_packed_event_read(vq
->vdev
, &caches
->used
, &e
);
464 e
.flags
= VRING_PACKED_EVENT_FLAG_DISABLE
;
465 } else if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
466 off_wrap
= vq
->shadow_avail_idx
| vq
->shadow_avail_wrap_counter
<< 15;
467 vring_packed_off_wrap_write(vq
->vdev
, &caches
->used
, off_wrap
);
468 /* Make sure off_wrap is wrote before flags */
470 e
.flags
= VRING_PACKED_EVENT_FLAG_DESC
;
472 e
.flags
= VRING_PACKED_EVENT_FLAG_ENABLE
;
475 vring_packed_flags_write(vq
->vdev
, &caches
->used
, e
.flags
);
477 /* Expose avail event/used flags before caller checks the avail idx. */
482 bool virtio_queue_get_notification(VirtQueue
*vq
)
484 return vq
->notification
;
487 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
489 vq
->notification
= enable
;
491 if (!vq
->vring
.desc
) {
495 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
496 virtio_queue_packed_set_notification(vq
, enable
);
498 virtio_queue_split_set_notification(vq
, enable
);
502 int virtio_queue_ready(VirtQueue
*vq
)
504 return vq
->vring
.avail
!= 0;
507 static void vring_packed_desc_read_flags(VirtIODevice
*vdev
,
509 MemoryRegionCache
*cache
,
512 address_space_read_cached(cache
,
513 i
* sizeof(VRingPackedDesc
) +
514 offsetof(VRingPackedDesc
, flags
),
515 flags
, sizeof(*flags
));
516 virtio_tswap16s(vdev
, flags
);
519 static void vring_packed_desc_read(VirtIODevice
*vdev
,
520 VRingPackedDesc
*desc
,
521 MemoryRegionCache
*cache
,
522 int i
, bool strict_order
)
524 hwaddr off
= i
* sizeof(VRingPackedDesc
);
526 vring_packed_desc_read_flags(vdev
, &desc
->flags
, cache
, i
);
529 /* Make sure flags is read before the rest fields. */
533 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, addr
),
534 &desc
->addr
, sizeof(desc
->addr
));
535 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, id
),
536 &desc
->id
, sizeof(desc
->id
));
537 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, len
),
538 &desc
->len
, sizeof(desc
->len
));
539 virtio_tswap64s(vdev
, &desc
->addr
);
540 virtio_tswap16s(vdev
, &desc
->id
);
541 virtio_tswap32s(vdev
, &desc
->len
);
544 static void vring_packed_desc_write_data(VirtIODevice
*vdev
,
545 VRingPackedDesc
*desc
,
546 MemoryRegionCache
*cache
,
549 hwaddr off_id
= i
* sizeof(VRingPackedDesc
) +
550 offsetof(VRingPackedDesc
, id
);
551 hwaddr off_len
= i
* sizeof(VRingPackedDesc
) +
552 offsetof(VRingPackedDesc
, len
);
554 virtio_tswap32s(vdev
, &desc
->len
);
555 virtio_tswap16s(vdev
, &desc
->id
);
556 address_space_write_cached(cache
, off_id
, &desc
->id
, sizeof(desc
->id
));
557 address_space_cache_invalidate(cache
, off_id
, sizeof(desc
->id
));
558 address_space_write_cached(cache
, off_len
, &desc
->len
, sizeof(desc
->len
));
559 address_space_cache_invalidate(cache
, off_len
, sizeof(desc
->len
));
562 static void vring_packed_desc_write_flags(VirtIODevice
*vdev
,
563 VRingPackedDesc
*desc
,
564 MemoryRegionCache
*cache
,
567 hwaddr off
= i
* sizeof(VRingPackedDesc
) + offsetof(VRingPackedDesc
, flags
);
569 virtio_tswap16s(vdev
, &desc
->flags
);
570 address_space_write_cached(cache
, off
, &desc
->flags
, sizeof(desc
->flags
));
571 address_space_cache_invalidate(cache
, off
, sizeof(desc
->flags
));
574 static void vring_packed_desc_write(VirtIODevice
*vdev
,
575 VRingPackedDesc
*desc
,
576 MemoryRegionCache
*cache
,
577 int i
, bool strict_order
)
579 vring_packed_desc_write_data(vdev
, desc
, cache
, i
);
581 /* Make sure data is wrote before flags. */
584 vring_packed_desc_write_flags(vdev
, desc
, cache
, i
);
587 static inline bool is_desc_avail(uint16_t flags
, bool wrap_counter
)
591 avail
= !!(flags
& (1 << VRING_PACKED_DESC_F_AVAIL
));
592 used
= !!(flags
& (1 << VRING_PACKED_DESC_F_USED
));
593 return (avail
!= used
) && (avail
== wrap_counter
);
596 /* Fetch avail_idx from VQ memory only when we really need to know if
597 * guest has added some buffers.
598 * Called within rcu_read_lock(). */
599 static int virtio_queue_empty_rcu(VirtQueue
*vq
)
601 if (virtio_device_disabled(vq
->vdev
)) {
605 if (unlikely(!vq
->vring
.avail
)) {
609 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
613 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
616 static int virtio_queue_split_empty(VirtQueue
*vq
)
620 if (virtio_device_disabled(vq
->vdev
)) {
624 if (unlikely(!vq
->vring
.avail
)) {
628 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
632 RCU_READ_LOCK_GUARD();
633 empty
= vring_avail_idx(vq
) == vq
->last_avail_idx
;
637 static int virtio_queue_packed_empty_rcu(VirtQueue
*vq
)
639 struct VRingPackedDesc desc
;
640 VRingMemoryRegionCaches
*cache
;
642 if (unlikely(!vq
->vring
.desc
)) {
646 cache
= vring_get_region_caches(vq
);
651 vring_packed_desc_read_flags(vq
->vdev
, &desc
.flags
, &cache
->desc
,
654 return !is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
);
657 static int virtio_queue_packed_empty(VirtQueue
*vq
)
659 RCU_READ_LOCK_GUARD();
660 return virtio_queue_packed_empty_rcu(vq
);
663 int virtio_queue_empty(VirtQueue
*vq
)
665 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
666 return virtio_queue_packed_empty(vq
);
668 return virtio_queue_split_empty(vq
);
672 static void virtqueue_unmap_sg(VirtQueue
*vq
, const VirtQueueElement
*elem
,
675 AddressSpace
*dma_as
= vq
->vdev
->dma_as
;
680 for (i
= 0; i
< elem
->in_num
; i
++) {
681 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
683 dma_memory_unmap(dma_as
, elem
->in_sg
[i
].iov_base
,
684 elem
->in_sg
[i
].iov_len
,
685 DMA_DIRECTION_FROM_DEVICE
, size
);
690 for (i
= 0; i
< elem
->out_num
; i
++)
691 dma_memory_unmap(dma_as
, elem
->out_sg
[i
].iov_base
,
692 elem
->out_sg
[i
].iov_len
,
693 DMA_DIRECTION_TO_DEVICE
,
694 elem
->out_sg
[i
].iov_len
);
697 /* virtqueue_detach_element:
698 * @vq: The #VirtQueue
699 * @elem: The #VirtQueueElement
700 * @len: number of bytes written
702 * Detach the element from the virtqueue. This function is suitable for device
703 * reset or other situations where a #VirtQueueElement is simply freed and will
704 * not be pushed or discarded.
706 void virtqueue_detach_element(VirtQueue
*vq
, const VirtQueueElement
*elem
,
709 vq
->inuse
-= elem
->ndescs
;
710 virtqueue_unmap_sg(vq
, elem
, len
);
713 static void virtqueue_split_rewind(VirtQueue
*vq
, unsigned int num
)
715 vq
->last_avail_idx
-= num
;
718 static void virtqueue_packed_rewind(VirtQueue
*vq
, unsigned int num
)
720 if (vq
->last_avail_idx
< num
) {
721 vq
->last_avail_idx
= vq
->vring
.num
+ vq
->last_avail_idx
- num
;
722 vq
->last_avail_wrap_counter
^= 1;
724 vq
->last_avail_idx
-= num
;
729 * @vq: The #VirtQueue
730 * @elem: The #VirtQueueElement
731 * @len: number of bytes written
733 * Pretend the most recent element wasn't popped from the virtqueue. The next
734 * call to virtqueue_pop() will refetch the element.
736 void virtqueue_unpop(VirtQueue
*vq
, const VirtQueueElement
*elem
,
740 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
741 virtqueue_packed_rewind(vq
, 1);
743 virtqueue_split_rewind(vq
, 1);
746 virtqueue_detach_element(vq
, elem
, len
);
750 * @vq: The #VirtQueue
751 * @num: Number of elements to push back
753 * Pretend that elements weren't popped from the virtqueue. The next
754 * virtqueue_pop() will refetch the oldest element.
756 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
758 * Returns: true on success, false if @num is greater than the number of in use
761 bool virtqueue_rewind(VirtQueue
*vq
, unsigned int num
)
763 if (num
> vq
->inuse
) {
768 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
769 virtqueue_packed_rewind(vq
, num
);
771 virtqueue_split_rewind(vq
, num
);
776 static void virtqueue_split_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
777 unsigned int len
, unsigned int idx
)
781 if (unlikely(!vq
->vring
.used
)) {
785 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
787 uelem
.id
= elem
->index
;
789 vring_used_write(vq
, &uelem
, idx
);
792 static void virtqueue_packed_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
793 unsigned int len
, unsigned int idx
)
795 vq
->used_elems
[idx
].index
= elem
->index
;
796 vq
->used_elems
[idx
].len
= len
;
797 vq
->used_elems
[idx
].ndescs
= elem
->ndescs
;
800 static void virtqueue_packed_fill_desc(VirtQueue
*vq
,
801 const VirtQueueElement
*elem
,
806 VRingMemoryRegionCaches
*caches
;
807 VRingPackedDesc desc
= {
811 bool wrap_counter
= vq
->used_wrap_counter
;
813 if (unlikely(!vq
->vring
.desc
)) {
817 head
= vq
->used_idx
+ idx
;
818 if (head
>= vq
->vring
.num
) {
819 head
-= vq
->vring
.num
;
823 desc
.flags
|= (1 << VRING_PACKED_DESC_F_AVAIL
);
824 desc
.flags
|= (1 << VRING_PACKED_DESC_F_USED
);
826 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_AVAIL
);
827 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_USED
);
830 caches
= vring_get_region_caches(vq
);
835 vring_packed_desc_write(vq
->vdev
, &desc
, &caches
->desc
, head
, strict_order
);
838 /* Called within rcu_read_lock(). */
839 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
840 unsigned int len
, unsigned int idx
)
842 trace_virtqueue_fill(vq
, elem
, len
, idx
);
844 virtqueue_unmap_sg(vq
, elem
, len
);
846 if (virtio_device_disabled(vq
->vdev
)) {
850 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
851 virtqueue_packed_fill(vq
, elem
, len
, idx
);
853 virtqueue_split_fill(vq
, elem
, len
, idx
);
857 /* Called within rcu_read_lock(). */
858 static void virtqueue_split_flush(VirtQueue
*vq
, unsigned int count
)
862 if (unlikely(!vq
->vring
.used
)) {
866 /* Make sure buffer is written before we update index. */
868 trace_virtqueue_flush(vq
, count
);
871 vring_used_idx_set(vq
, new);
873 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
874 vq
->signalled_used_valid
= false;
877 static void virtqueue_packed_flush(VirtQueue
*vq
, unsigned int count
)
879 unsigned int i
, ndescs
= 0;
881 if (unlikely(!vq
->vring
.desc
)) {
885 for (i
= 1; i
< count
; i
++) {
886 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[i
], i
, false);
887 ndescs
+= vq
->used_elems
[i
].ndescs
;
889 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[0], 0, true);
890 ndescs
+= vq
->used_elems
[0].ndescs
;
893 vq
->used_idx
+= ndescs
;
894 if (vq
->used_idx
>= vq
->vring
.num
) {
895 vq
->used_idx
-= vq
->vring
.num
;
896 vq
->used_wrap_counter
^= 1;
900 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
902 if (virtio_device_disabled(vq
->vdev
)) {
907 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
908 virtqueue_packed_flush(vq
, count
);
910 virtqueue_split_flush(vq
, count
);
914 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
917 RCU_READ_LOCK_GUARD();
918 virtqueue_fill(vq
, elem
, len
, 0);
919 virtqueue_flush(vq
, 1);
922 /* Called within rcu_read_lock(). */
923 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
925 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
927 /* Check it isn't doing very strange things with descriptor numbers. */
928 if (num_heads
> vq
->vring
.num
) {
929 virtio_error(vq
->vdev
, "Guest moved used index from %u to %u",
930 idx
, vq
->shadow_avail_idx
);
933 /* On success, callers read a descriptor at vq->last_avail_idx.
934 * Make sure descriptor read does not bypass avail index read. */
942 /* Called within rcu_read_lock(). */
943 static bool virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
,
946 /* Grab the next descriptor number they're advertising, and increment
947 * the index we've seen. */
948 *head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
950 /* If their number is silly, that's a fatal mistake. */
951 if (*head
>= vq
->vring
.num
) {
952 virtio_error(vq
->vdev
, "Guest says index %u is available", *head
);
960 VIRTQUEUE_READ_DESC_ERROR
= -1,
961 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
962 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
965 static int virtqueue_split_read_next_desc(VirtIODevice
*vdev
, VRingDesc
*desc
,
966 MemoryRegionCache
*desc_cache
,
967 unsigned int max
, unsigned int *next
)
969 /* If this descriptor says it doesn't chain, we're done. */
970 if (!(desc
->flags
& VRING_DESC_F_NEXT
)) {
971 return VIRTQUEUE_READ_DESC_DONE
;
974 /* Check they're not leading us off end of descriptors. */
976 /* Make sure compiler knows to grab that: we don't want it changing! */
980 virtio_error(vdev
, "Desc next is %u", *next
);
981 return VIRTQUEUE_READ_DESC_ERROR
;
984 vring_split_desc_read(vdev
, desc
, desc_cache
, *next
);
985 return VIRTQUEUE_READ_DESC_MORE
;
988 static void virtqueue_split_get_avail_bytes(VirtQueue
*vq
,
989 unsigned int *in_bytes
, unsigned int *out_bytes
,
990 unsigned max_in_bytes
, unsigned max_out_bytes
)
992 VirtIODevice
*vdev
= vq
->vdev
;
993 unsigned int max
, idx
;
994 unsigned int total_bufs
, in_total
, out_total
;
995 VRingMemoryRegionCaches
*caches
;
996 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1000 RCU_READ_LOCK_GUARD();
1002 idx
= vq
->last_avail_idx
;
1003 total_bufs
= in_total
= out_total
= 0;
1005 max
= vq
->vring
.num
;
1006 caches
= vring_get_region_caches(vq
);
1011 while ((rc
= virtqueue_num_heads(vq
, idx
)) > 0) {
1012 MemoryRegionCache
*desc_cache
= &caches
->desc
;
1013 unsigned int num_bufs
;
1017 num_bufs
= total_bufs
;
1019 if (!virtqueue_get_head(vq
, idx
++, &i
)) {
1023 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1025 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1026 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1027 virtio_error(vdev
, "Invalid size for indirect buffer table");
1031 /* If we've got too many, that implies a descriptor loop. */
1032 if (num_bufs
>= max
) {
1033 virtio_error(vdev
, "Looped descriptor");
1037 /* loop over the indirect descriptor table */
1038 len
= address_space_cache_init(&indirect_desc_cache
,
1040 desc
.addr
, desc
.len
, false);
1041 desc_cache
= &indirect_desc_cache
;
1042 if (len
< desc
.len
) {
1043 virtio_error(vdev
, "Cannot map indirect buffer");
1047 max
= desc
.len
/ sizeof(VRingDesc
);
1049 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1053 /* If we've got too many, that implies a descriptor loop. */
1054 if (++num_bufs
> max
) {
1055 virtio_error(vdev
, "Looped descriptor");
1059 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1060 in_total
+= desc
.len
;
1062 out_total
+= desc
.len
;
1064 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1068 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1069 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1071 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1075 if (desc_cache
== &indirect_desc_cache
) {
1076 address_space_cache_destroy(&indirect_desc_cache
);
1079 total_bufs
= num_bufs
;
1088 address_space_cache_destroy(&indirect_desc_cache
);
1090 *in_bytes
= in_total
;
1093 *out_bytes
= out_total
;
1098 in_total
= out_total
= 0;
1102 static int virtqueue_packed_read_next_desc(VirtQueue
*vq
,
1103 VRingPackedDesc
*desc
,
1110 /* If this descriptor says it doesn't chain, we're done. */
1111 if (!indirect
&& !(desc
->flags
& VRING_DESC_F_NEXT
)) {
1112 return VIRTQUEUE_READ_DESC_DONE
;
1118 return VIRTQUEUE_READ_DESC_DONE
;
1120 (*next
) -= vq
->vring
.num
;
1124 vring_packed_desc_read(vq
->vdev
, desc
, desc_cache
, *next
, false);
1125 return VIRTQUEUE_READ_DESC_MORE
;
1128 static void virtqueue_packed_get_avail_bytes(VirtQueue
*vq
,
1129 unsigned int *in_bytes
,
1130 unsigned int *out_bytes
,
1131 unsigned max_in_bytes
,
1132 unsigned max_out_bytes
)
1134 VirtIODevice
*vdev
= vq
->vdev
;
1135 unsigned int max
, idx
;
1136 unsigned int total_bufs
, in_total
, out_total
;
1137 MemoryRegionCache
*desc_cache
;
1138 VRingMemoryRegionCaches
*caches
;
1139 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1141 VRingPackedDesc desc
;
1144 RCU_READ_LOCK_GUARD();
1145 idx
= vq
->last_avail_idx
;
1146 wrap_counter
= vq
->last_avail_wrap_counter
;
1147 total_bufs
= in_total
= out_total
= 0;
1149 max
= vq
->vring
.num
;
1150 caches
= vring_get_region_caches(vq
);
1156 unsigned int num_bufs
= total_bufs
;
1157 unsigned int i
= idx
;
1160 desc_cache
= &caches
->desc
;
1161 vring_packed_desc_read(vdev
, &desc
, desc_cache
, idx
, true);
1162 if (!is_desc_avail(desc
.flags
, wrap_counter
)) {
1166 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1167 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1168 virtio_error(vdev
, "Invalid size for indirect buffer table");
1172 /* If we've got too many, that implies a descriptor loop. */
1173 if (num_bufs
>= max
) {
1174 virtio_error(vdev
, "Looped descriptor");
1178 /* loop over the indirect descriptor table */
1179 len
= address_space_cache_init(&indirect_desc_cache
,
1181 desc
.addr
, desc
.len
, false);
1182 desc_cache
= &indirect_desc_cache
;
1183 if (len
< desc
.len
) {
1184 virtio_error(vdev
, "Cannot map indirect buffer");
1188 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1190 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1194 /* If we've got too many, that implies a descriptor loop. */
1195 if (++num_bufs
> max
) {
1196 virtio_error(vdev
, "Looped descriptor");
1200 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1201 in_total
+= desc
.len
;
1203 out_total
+= desc
.len
;
1205 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1209 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
,
1211 &indirect_desc_cache
);
1212 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1214 if (desc_cache
== &indirect_desc_cache
) {
1215 address_space_cache_destroy(&indirect_desc_cache
);
1219 idx
+= num_bufs
- total_bufs
;
1220 total_bufs
= num_bufs
;
1223 if (idx
>= vq
->vring
.num
) {
1224 idx
-= vq
->vring
.num
;
1229 /* Record the index and wrap counter for a kick we want */
1230 vq
->shadow_avail_idx
= idx
;
1231 vq
->shadow_avail_wrap_counter
= wrap_counter
;
1233 address_space_cache_destroy(&indirect_desc_cache
);
1235 *in_bytes
= in_total
;
1238 *out_bytes
= out_total
;
1243 in_total
= out_total
= 0;
1247 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
1248 unsigned int *out_bytes
,
1249 unsigned max_in_bytes
, unsigned max_out_bytes
)
1252 VRingMemoryRegionCaches
*caches
;
1254 if (unlikely(!vq
->vring
.desc
)) {
1258 caches
= vring_get_region_caches(vq
);
1263 desc_size
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
1264 sizeof(VRingPackedDesc
) : sizeof(VRingDesc
);
1265 if (caches
->desc
.len
< vq
->vring
.num
* desc_size
) {
1266 virtio_error(vq
->vdev
, "Cannot map descriptor ring");
1270 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1271 virtqueue_packed_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1272 max_in_bytes
, max_out_bytes
);
1274 virtqueue_split_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1275 max_in_bytes
, max_out_bytes
);
1288 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
1289 unsigned int out_bytes
)
1291 unsigned int in_total
, out_total
;
1293 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
1294 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
1297 static bool virtqueue_map_desc(VirtIODevice
*vdev
, unsigned int *p_num_sg
,
1298 hwaddr
*addr
, struct iovec
*iov
,
1299 unsigned int max_num_sg
, bool is_write
,
1300 hwaddr pa
, size_t sz
)
1303 unsigned num_sg
= *p_num_sg
;
1304 assert(num_sg
<= max_num_sg
);
1307 virtio_error(vdev
, "virtio: zero sized buffers are not allowed");
1314 if (num_sg
== max_num_sg
) {
1315 virtio_error(vdev
, "virtio: too many write descriptors in "
1320 iov
[num_sg
].iov_base
= dma_memory_map(vdev
->dma_as
, pa
, &len
,
1322 DMA_DIRECTION_FROM_DEVICE
:
1323 DMA_DIRECTION_TO_DEVICE
);
1324 if (!iov
[num_sg
].iov_base
) {
1325 virtio_error(vdev
, "virtio: bogus descriptor or out of resources");
1329 iov
[num_sg
].iov_len
= len
;
1343 /* Only used by error code paths before we have a VirtQueueElement (therefore
1344 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
1347 static void virtqueue_undo_map_desc(unsigned int out_num
, unsigned int in_num
,
1352 for (i
= 0; i
< out_num
+ in_num
; i
++) {
1353 int is_write
= i
>= out_num
;
1355 cpu_physical_memory_unmap(iov
->iov_base
, iov
->iov_len
, is_write
, 0);
1360 static void virtqueue_map_iovec(VirtIODevice
*vdev
, struct iovec
*sg
,
1361 hwaddr
*addr
, unsigned int num_sg
,
1367 for (i
= 0; i
< num_sg
; i
++) {
1368 len
= sg
[i
].iov_len
;
1369 sg
[i
].iov_base
= dma_memory_map(vdev
->dma_as
,
1370 addr
[i
], &len
, is_write
?
1371 DMA_DIRECTION_FROM_DEVICE
:
1372 DMA_DIRECTION_TO_DEVICE
);
1373 if (!sg
[i
].iov_base
) {
1374 error_report("virtio: error trying to map MMIO memory");
1377 if (len
!= sg
[i
].iov_len
) {
1378 error_report("virtio: unexpected memory split");
1384 void virtqueue_map(VirtIODevice
*vdev
, VirtQueueElement
*elem
)
1386 virtqueue_map_iovec(vdev
, elem
->in_sg
, elem
->in_addr
, elem
->in_num
, true);
1387 virtqueue_map_iovec(vdev
, elem
->out_sg
, elem
->out_addr
, elem
->out_num
,
1391 static void *virtqueue_alloc_element(size_t sz
, unsigned out_num
, unsigned in_num
)
1393 VirtQueueElement
*elem
;
1394 size_t in_addr_ofs
= QEMU_ALIGN_UP(sz
, __alignof__(elem
->in_addr
[0]));
1395 size_t out_addr_ofs
= in_addr_ofs
+ in_num
* sizeof(elem
->in_addr
[0]);
1396 size_t out_addr_end
= out_addr_ofs
+ out_num
* sizeof(elem
->out_addr
[0]);
1397 size_t in_sg_ofs
= QEMU_ALIGN_UP(out_addr_end
, __alignof__(elem
->in_sg
[0]));
1398 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
1399 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
1401 assert(sz
>= sizeof(VirtQueueElement
));
1402 elem
= g_malloc(out_sg_end
);
1403 trace_virtqueue_alloc_element(elem
, sz
, in_num
, out_num
);
1404 elem
->out_num
= out_num
;
1405 elem
->in_num
= in_num
;
1406 elem
->in_addr
= (void *)elem
+ in_addr_ofs
;
1407 elem
->out_addr
= (void *)elem
+ out_addr_ofs
;
1408 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
1409 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
1413 static void *virtqueue_split_pop(VirtQueue
*vq
, size_t sz
)
1415 unsigned int i
, head
, max
;
1416 VRingMemoryRegionCaches
*caches
;
1417 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1418 MemoryRegionCache
*desc_cache
;
1420 VirtIODevice
*vdev
= vq
->vdev
;
1421 VirtQueueElement
*elem
= NULL
;
1422 unsigned out_num
, in_num
, elem_entries
;
1423 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1424 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1428 RCU_READ_LOCK_GUARD();
1429 if (virtio_queue_empty_rcu(vq
)) {
1432 /* Needed after virtio_queue_empty(), see comment in
1433 * virtqueue_num_heads(). */
1436 /* When we start there are none of either input nor output. */
1437 out_num
= in_num
= elem_entries
= 0;
1439 max
= vq
->vring
.num
;
1441 if (vq
->inuse
>= vq
->vring
.num
) {
1442 virtio_error(vdev
, "Virtqueue size exceeded");
1446 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
++, &head
)) {
1450 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
1451 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1456 caches
= vring_get_region_caches(vq
);
1458 virtio_error(vdev
, "Region caches not initialized");
1462 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1463 virtio_error(vdev
, "Cannot map descriptor ring");
1467 desc_cache
= &caches
->desc
;
1468 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1469 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1470 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1471 virtio_error(vdev
, "Invalid size for indirect buffer table");
1475 /* loop over the indirect descriptor table */
1476 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1477 desc
.addr
, desc
.len
, false);
1478 desc_cache
= &indirect_desc_cache
;
1479 if (len
< desc
.len
) {
1480 virtio_error(vdev
, "Cannot map indirect buffer");
1484 max
= desc
.len
/ sizeof(VRingDesc
);
1486 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1489 /* Collect all the descriptors */
1493 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1494 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1496 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1497 desc
.addr
, desc
.len
);
1500 virtio_error(vdev
, "Incorrect order for descriptors");
1503 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1504 VIRTQUEUE_MAX_SIZE
, false,
1505 desc
.addr
, desc
.len
);
1511 /* If we've got too many, that implies a descriptor loop. */
1512 if (++elem_entries
> max
) {
1513 virtio_error(vdev
, "Looped descriptor");
1517 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1518 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1520 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1524 /* Now copy what we have collected and mapped */
1525 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1528 for (i
= 0; i
< out_num
; i
++) {
1529 elem
->out_addr
[i
] = addr
[i
];
1530 elem
->out_sg
[i
] = iov
[i
];
1532 for (i
= 0; i
< in_num
; i
++) {
1533 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1534 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1539 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1541 address_space_cache_destroy(&indirect_desc_cache
);
1546 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1550 static void *virtqueue_packed_pop(VirtQueue
*vq
, size_t sz
)
1552 unsigned int i
, max
;
1553 VRingMemoryRegionCaches
*caches
;
1554 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1555 MemoryRegionCache
*desc_cache
;
1557 VirtIODevice
*vdev
= vq
->vdev
;
1558 VirtQueueElement
*elem
= NULL
;
1559 unsigned out_num
, in_num
, elem_entries
;
1560 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1561 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1562 VRingPackedDesc desc
;
1566 RCU_READ_LOCK_GUARD();
1567 if (virtio_queue_packed_empty_rcu(vq
)) {
1571 /* When we start there are none of either input nor output. */
1572 out_num
= in_num
= elem_entries
= 0;
1574 max
= vq
->vring
.num
;
1576 if (vq
->inuse
>= vq
->vring
.num
) {
1577 virtio_error(vdev
, "Virtqueue size exceeded");
1581 i
= vq
->last_avail_idx
;
1583 caches
= vring_get_region_caches(vq
);
1585 virtio_error(vdev
, "Region caches not initialized");
1589 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1590 virtio_error(vdev
, "Cannot map descriptor ring");
1594 desc_cache
= &caches
->desc
;
1595 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, true);
1597 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1598 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1599 virtio_error(vdev
, "Invalid size for indirect buffer table");
1603 /* loop over the indirect descriptor table */
1604 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1605 desc
.addr
, desc
.len
, false);
1606 desc_cache
= &indirect_desc_cache
;
1607 if (len
< desc
.len
) {
1608 virtio_error(vdev
, "Cannot map indirect buffer");
1612 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1614 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1617 /* Collect all the descriptors */
1621 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1622 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1624 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1625 desc
.addr
, desc
.len
);
1628 virtio_error(vdev
, "Incorrect order for descriptors");
1631 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1632 VIRTQUEUE_MAX_SIZE
, false,
1633 desc
.addr
, desc
.len
);
1639 /* If we've got too many, that implies a descriptor loop. */
1640 if (++elem_entries
> max
) {
1641 virtio_error(vdev
, "Looped descriptor");
1645 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
, &i
,
1647 &indirect_desc_cache
);
1648 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1650 /* Now copy what we have collected and mapped */
1651 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1652 for (i
= 0; i
< out_num
; i
++) {
1653 elem
->out_addr
[i
] = addr
[i
];
1654 elem
->out_sg
[i
] = iov
[i
];
1656 for (i
= 0; i
< in_num
; i
++) {
1657 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1658 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1662 elem
->ndescs
= (desc_cache
== &indirect_desc_cache
) ? 1 : elem_entries
;
1663 vq
->last_avail_idx
+= elem
->ndescs
;
1664 vq
->inuse
+= elem
->ndescs
;
1666 if (vq
->last_avail_idx
>= vq
->vring
.num
) {
1667 vq
->last_avail_idx
-= vq
->vring
.num
;
1668 vq
->last_avail_wrap_counter
^= 1;
1671 vq
->shadow_avail_idx
= vq
->last_avail_idx
;
1672 vq
->shadow_avail_wrap_counter
= vq
->last_avail_wrap_counter
;
1674 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1676 address_space_cache_destroy(&indirect_desc_cache
);
1681 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1685 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
)
1687 if (virtio_device_disabled(vq
->vdev
)) {
1691 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1692 return virtqueue_packed_pop(vq
, sz
);
1694 return virtqueue_split_pop(vq
, sz
);
1698 static unsigned int virtqueue_packed_drop_all(VirtQueue
*vq
)
1700 VRingMemoryRegionCaches
*caches
;
1701 MemoryRegionCache
*desc_cache
;
1702 unsigned int dropped
= 0;
1703 VirtQueueElement elem
= {};
1704 VirtIODevice
*vdev
= vq
->vdev
;
1705 VRingPackedDesc desc
;
1707 caches
= vring_get_region_caches(vq
);
1712 desc_cache
= &caches
->desc
;
1714 virtio_queue_set_notification(vq
, 0);
1716 while (vq
->inuse
< vq
->vring
.num
) {
1717 unsigned int idx
= vq
->last_avail_idx
;
1719 * works similar to virtqueue_pop but does not map buffers
1720 * and does not allocate any memory.
1722 vring_packed_desc_read(vdev
, &desc
, desc_cache
,
1723 vq
->last_avail_idx
, true);
1724 if (!is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
)) {
1727 elem
.index
= desc
.id
;
1729 while (virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
,
1730 vq
->vring
.num
, &idx
, false)) {
1734 * immediately push the element, nothing to unmap
1735 * as both in_num and out_num are set to 0.
1737 virtqueue_push(vq
, &elem
, 0);
1739 vq
->last_avail_idx
+= elem
.ndescs
;
1740 if (vq
->last_avail_idx
>= vq
->vring
.num
) {
1741 vq
->last_avail_idx
-= vq
->vring
.num
;
1742 vq
->last_avail_wrap_counter
^= 1;
1749 static unsigned int virtqueue_split_drop_all(VirtQueue
*vq
)
1751 unsigned int dropped
= 0;
1752 VirtQueueElement elem
= {};
1753 VirtIODevice
*vdev
= vq
->vdev
;
1754 bool fEventIdx
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
);
1756 while (!virtio_queue_empty(vq
) && vq
->inuse
< vq
->vring
.num
) {
1757 /* works similar to virtqueue_pop but does not map buffers
1758 * and does not allocate any memory */
1760 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
, &elem
.index
)) {
1764 vq
->last_avail_idx
++;
1766 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1768 /* immediately push the element, nothing to unmap
1769 * as both in_num and out_num are set to 0 */
1770 virtqueue_push(vq
, &elem
, 0);
1777 /* virtqueue_drop_all:
1778 * @vq: The #VirtQueue
1779 * Drops all queued buffers and indicates them to the guest
1780 * as if they are done. Useful when buffers can not be
1781 * processed but must be returned to the guest.
1783 unsigned int virtqueue_drop_all(VirtQueue
*vq
)
1785 struct VirtIODevice
*vdev
= vq
->vdev
;
1787 if (virtio_device_disabled(vq
->vdev
)) {
1791 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1792 return virtqueue_packed_drop_all(vq
);
1794 return virtqueue_split_drop_all(vq
);
1798 /* Reading and writing a structure directly to QEMUFile is *awful*, but
1799 * it is what QEMU has always done by mistake. We can change it sooner
1800 * or later by bumping the version number of the affected vm states.
1801 * In the meanwhile, since the in-memory layout of VirtQueueElement
1802 * has changed, we need to marshal to and from the layout that was
1803 * used before the change.
1805 typedef struct VirtQueueElementOld
{
1807 unsigned int out_num
;
1808 unsigned int in_num
;
1809 hwaddr in_addr
[VIRTQUEUE_MAX_SIZE
];
1810 hwaddr out_addr
[VIRTQUEUE_MAX_SIZE
];
1811 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
1812 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
1813 } VirtQueueElementOld
;
1815 void *qemu_get_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
, size_t sz
)
1817 VirtQueueElement
*elem
;
1818 VirtQueueElementOld data
;
1821 qemu_get_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1823 /* TODO: teach all callers that this can fail, and return failure instead
1824 * of asserting here.
1825 * This is just one thing (there are probably more) that must be
1826 * fixed before we can allow NDEBUG compilation.
1828 assert(ARRAY_SIZE(data
.in_addr
) >= data
.in_num
);
1829 assert(ARRAY_SIZE(data
.out_addr
) >= data
.out_num
);
1831 elem
= virtqueue_alloc_element(sz
, data
.out_num
, data
.in_num
);
1832 elem
->index
= data
.index
;
1834 for (i
= 0; i
< elem
->in_num
; i
++) {
1835 elem
->in_addr
[i
] = data
.in_addr
[i
];
1838 for (i
= 0; i
< elem
->out_num
; i
++) {
1839 elem
->out_addr
[i
] = data
.out_addr
[i
];
1842 for (i
= 0; i
< elem
->in_num
; i
++) {
1843 /* Base is overwritten by virtqueue_map. */
1844 elem
->in_sg
[i
].iov_base
= 0;
1845 elem
->in_sg
[i
].iov_len
= data
.in_sg
[i
].iov_len
;
1848 for (i
= 0; i
< elem
->out_num
; i
++) {
1849 /* Base is overwritten by virtqueue_map. */
1850 elem
->out_sg
[i
].iov_base
= 0;
1851 elem
->out_sg
[i
].iov_len
= data
.out_sg
[i
].iov_len
;
1854 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1855 qemu_get_be32s(f
, &elem
->ndescs
);
1858 virtqueue_map(vdev
, elem
);
1862 void qemu_put_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
,
1863 VirtQueueElement
*elem
)
1865 VirtQueueElementOld data
;
1868 memset(&data
, 0, sizeof(data
));
1869 data
.index
= elem
->index
;
1870 data
.in_num
= elem
->in_num
;
1871 data
.out_num
= elem
->out_num
;
1873 for (i
= 0; i
< elem
->in_num
; i
++) {
1874 data
.in_addr
[i
] = elem
->in_addr
[i
];
1877 for (i
= 0; i
< elem
->out_num
; i
++) {
1878 data
.out_addr
[i
] = elem
->out_addr
[i
];
1881 for (i
= 0; i
< elem
->in_num
; i
++) {
1882 /* Base is overwritten by virtqueue_map when loading. Do not
1883 * save it, as it would leak the QEMU address space layout. */
1884 data
.in_sg
[i
].iov_len
= elem
->in_sg
[i
].iov_len
;
1887 for (i
= 0; i
< elem
->out_num
; i
++) {
1888 /* Do not save iov_base as above. */
1889 data
.out_sg
[i
].iov_len
= elem
->out_sg
[i
].iov_len
;
1892 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1893 qemu_put_be32s(f
, &elem
->ndescs
);
1896 qemu_put_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1900 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
1902 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1903 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1905 if (virtio_device_disabled(vdev
)) {
1910 k
->notify(qbus
->parent
, vector
);
1914 void virtio_update_irq(VirtIODevice
*vdev
)
1916 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
1919 static int virtio_validate_features(VirtIODevice
*vdev
)
1921 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1923 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
) &&
1924 !virtio_vdev_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1928 if (k
->validate_features
) {
1929 return k
->validate_features(vdev
);
1935 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
1937 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1938 trace_virtio_set_status(vdev
, val
);
1940 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1941 if (!(vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) &&
1942 val
& VIRTIO_CONFIG_S_FEATURES_OK
) {
1943 int ret
= virtio_validate_features(vdev
);
1951 if ((vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) !=
1952 (val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1953 virtio_set_started(vdev
, val
& VIRTIO_CONFIG_S_DRIVER_OK
);
1956 if (k
->set_status
) {
1957 k
->set_status(vdev
, val
);
1964 static enum virtio_device_endian
virtio_default_endian(void)
1966 if (target_words_bigendian()) {
1967 return VIRTIO_DEVICE_ENDIAN_BIG
;
1969 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
1973 static enum virtio_device_endian
virtio_current_cpu_endian(void)
1975 if (cpu_virtio_is_big_endian(current_cpu
)) {
1976 return VIRTIO_DEVICE_ENDIAN_BIG
;
1978 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
1982 void virtio_reset(void *opaque
)
1984 VirtIODevice
*vdev
= opaque
;
1985 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1988 virtio_set_status(vdev
, 0);
1990 /* Guest initiated reset */
1991 vdev
->device_endian
= virtio_current_cpu_endian();
1994 vdev
->device_endian
= virtio_default_endian();
2001 vdev
->start_on_kick
= false;
2002 vdev
->started
= false;
2003 vdev
->broken
= false;
2004 vdev
->guest_features
= 0;
2005 vdev
->queue_sel
= 0;
2007 vdev
->disabled
= false;
2008 qatomic_set(&vdev
->isr
, 0);
2009 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
2010 virtio_notify_vector(vdev
, vdev
->config_vector
);
2012 for(i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2013 vdev
->vq
[i
].vring
.desc
= 0;
2014 vdev
->vq
[i
].vring
.avail
= 0;
2015 vdev
->vq
[i
].vring
.used
= 0;
2016 vdev
->vq
[i
].last_avail_idx
= 0;
2017 vdev
->vq
[i
].shadow_avail_idx
= 0;
2018 vdev
->vq
[i
].used_idx
= 0;
2019 vdev
->vq
[i
].last_avail_wrap_counter
= true;
2020 vdev
->vq
[i
].shadow_avail_wrap_counter
= true;
2021 vdev
->vq
[i
].used_wrap_counter
= true;
2022 virtio_queue_set_vector(vdev
, i
, VIRTIO_NO_VECTOR
);
2023 vdev
->vq
[i
].signalled_used
= 0;
2024 vdev
->vq
[i
].signalled_used_valid
= false;
2025 vdev
->vq
[i
].notification
= true;
2026 vdev
->vq
[i
].vring
.num
= vdev
->vq
[i
].vring
.num_default
;
2027 vdev
->vq
[i
].inuse
= 0;
2028 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
2032 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
2034 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2037 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2038 return (uint32_t)-1;
2041 k
->get_config(vdev
, vdev
->config
);
2043 val
= ldub_p(vdev
->config
+ addr
);
2047 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
2049 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2052 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2053 return (uint32_t)-1;
2056 k
->get_config(vdev
, vdev
->config
);
2058 val
= lduw_p(vdev
->config
+ addr
);
2062 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
2064 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2067 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2068 return (uint32_t)-1;
2071 k
->get_config(vdev
, vdev
->config
);
2073 val
= ldl_p(vdev
->config
+ addr
);
2077 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2079 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2082 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2086 stb_p(vdev
->config
+ addr
, val
);
2088 if (k
->set_config
) {
2089 k
->set_config(vdev
, vdev
->config
);
2093 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2095 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2096 uint16_t val
= data
;
2098 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2102 stw_p(vdev
->config
+ addr
, val
);
2104 if (k
->set_config
) {
2105 k
->set_config(vdev
, vdev
->config
);
2109 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2111 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2112 uint32_t val
= data
;
2114 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2118 stl_p(vdev
->config
+ addr
, val
);
2120 if (k
->set_config
) {
2121 k
->set_config(vdev
, vdev
->config
);
2125 uint32_t virtio_config_modern_readb(VirtIODevice
*vdev
, uint32_t addr
)
2127 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2130 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2131 return (uint32_t)-1;
2134 k
->get_config(vdev
, vdev
->config
);
2136 val
= ldub_p(vdev
->config
+ addr
);
2140 uint32_t virtio_config_modern_readw(VirtIODevice
*vdev
, uint32_t addr
)
2142 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2145 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2146 return (uint32_t)-1;
2149 k
->get_config(vdev
, vdev
->config
);
2151 val
= lduw_le_p(vdev
->config
+ addr
);
2155 uint32_t virtio_config_modern_readl(VirtIODevice
*vdev
, uint32_t addr
)
2157 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2160 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2161 return (uint32_t)-1;
2164 k
->get_config(vdev
, vdev
->config
);
2166 val
= ldl_le_p(vdev
->config
+ addr
);
2170 void virtio_config_modern_writeb(VirtIODevice
*vdev
,
2171 uint32_t addr
, uint32_t data
)
2173 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2176 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2180 stb_p(vdev
->config
+ addr
, val
);
2182 if (k
->set_config
) {
2183 k
->set_config(vdev
, vdev
->config
);
2187 void virtio_config_modern_writew(VirtIODevice
*vdev
,
2188 uint32_t addr
, uint32_t data
)
2190 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2191 uint16_t val
= data
;
2193 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2197 stw_le_p(vdev
->config
+ addr
, val
);
2199 if (k
->set_config
) {
2200 k
->set_config(vdev
, vdev
->config
);
2204 void virtio_config_modern_writel(VirtIODevice
*vdev
,
2205 uint32_t addr
, uint32_t data
)
2207 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2208 uint32_t val
= data
;
2210 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2214 stl_le_p(vdev
->config
+ addr
, val
);
2216 if (k
->set_config
) {
2217 k
->set_config(vdev
, vdev
->config
);
2221 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
2223 if (!vdev
->vq
[n
].vring
.num
) {
2226 vdev
->vq
[n
].vring
.desc
= addr
;
2227 virtio_queue_update_rings(vdev
, n
);
2230 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
2232 return vdev
->vq
[n
].vring
.desc
;
2235 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
2236 hwaddr avail
, hwaddr used
)
2238 if (!vdev
->vq
[n
].vring
.num
) {
2241 vdev
->vq
[n
].vring
.desc
= desc
;
2242 vdev
->vq
[n
].vring
.avail
= avail
;
2243 vdev
->vq
[n
].vring
.used
= used
;
2244 virtio_init_region_cache(vdev
, n
);
2247 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
2249 /* Don't allow guest to flip queue between existent and
2250 * nonexistent states, or to set it to an invalid size.
2252 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
2253 num
> VIRTQUEUE_MAX_SIZE
||
2257 vdev
->vq
[n
].vring
.num
= num
;
2260 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
)
2262 return QLIST_FIRST(&vdev
->vector_queues
[vector
]);
2265 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
)
2267 return QLIST_NEXT(vq
, node
);
2270 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
2272 return vdev
->vq
[n
].vring
.num
;
2275 int virtio_queue_get_max_num(VirtIODevice
*vdev
, int n
)
2277 return vdev
->vq
[n
].vring
.num_default
;
2280 int virtio_get_num_queues(VirtIODevice
*vdev
)
2284 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2285 if (!virtio_queue_get_num(vdev
, i
)) {
2293 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
2295 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2296 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2298 /* virtio-1 compliant devices cannot change the alignment */
2299 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2300 error_report("tried to modify queue alignment for virtio-1 device");
2303 /* Check that the transport told us it was going to do this
2304 * (so a buggy transport will immediately assert rather than
2305 * silently failing to migrate this state)
2307 assert(k
->has_variable_vring_alignment
);
2310 vdev
->vq
[n
].vring
.align
= align
;
2311 virtio_queue_update_rings(vdev
, n
);
2315 static bool virtio_queue_notify_aio_vq(VirtQueue
*vq
)
2319 if (vq
->vring
.desc
&& vq
->handle_aio_output
) {
2320 VirtIODevice
*vdev
= vq
->vdev
;
2322 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2323 ret
= vq
->handle_aio_output(vdev
, vq
);
2325 if (unlikely(vdev
->start_on_kick
)) {
2326 virtio_set_started(vdev
, true);
2333 static void virtio_queue_notify_vq(VirtQueue
*vq
)
2335 if (vq
->vring
.desc
&& vq
->handle_output
) {
2336 VirtIODevice
*vdev
= vq
->vdev
;
2338 if (unlikely(vdev
->broken
)) {
2342 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2343 vq
->handle_output(vdev
, vq
);
2345 if (unlikely(vdev
->start_on_kick
)) {
2346 virtio_set_started(vdev
, true);
2351 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
2353 VirtQueue
*vq
= &vdev
->vq
[n
];
2355 if (unlikely(!vq
->vring
.desc
|| vdev
->broken
)) {
2359 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2360 if (vq
->host_notifier_enabled
) {
2361 event_notifier_set(&vq
->host_notifier
);
2362 } else if (vq
->handle_output
) {
2363 vq
->handle_output(vdev
, vq
);
2365 if (unlikely(vdev
->start_on_kick
)) {
2366 virtio_set_started(vdev
, true);
2371 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
2373 return n
< VIRTIO_QUEUE_MAX
? vdev
->vq
[n
].vector
:
2377 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
2379 VirtQueue
*vq
= &vdev
->vq
[n
];
2381 if (n
< VIRTIO_QUEUE_MAX
) {
2382 if (vdev
->vector_queues
&&
2383 vdev
->vq
[n
].vector
!= VIRTIO_NO_VECTOR
) {
2384 QLIST_REMOVE(vq
, node
);
2386 vdev
->vq
[n
].vector
= vector
;
2387 if (vdev
->vector_queues
&&
2388 vector
!= VIRTIO_NO_VECTOR
) {
2389 QLIST_INSERT_HEAD(&vdev
->vector_queues
[vector
], vq
, node
);
2394 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
2395 VirtIOHandleOutput handle_output
)
2399 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2400 if (vdev
->vq
[i
].vring
.num
== 0)
2404 if (i
== VIRTIO_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
2407 vdev
->vq
[i
].vring
.num
= queue_size
;
2408 vdev
->vq
[i
].vring
.num_default
= queue_size
;
2409 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
2410 vdev
->vq
[i
].handle_output
= handle_output
;
2411 vdev
->vq
[i
].handle_aio_output
= NULL
;
2412 vdev
->vq
[i
].used_elems
= g_malloc0(sizeof(VirtQueueElement
) *
2415 return &vdev
->vq
[i
];
2418 void virtio_delete_queue(VirtQueue
*vq
)
2421 vq
->vring
.num_default
= 0;
2422 vq
->handle_output
= NULL
;
2423 vq
->handle_aio_output
= NULL
;
2424 g_free(vq
->used_elems
);
2425 vq
->used_elems
= NULL
;
2426 virtio_virtqueue_reset_region_cache(vq
);
2429 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
2431 if (n
< 0 || n
>= VIRTIO_QUEUE_MAX
) {
2435 virtio_delete_queue(&vdev
->vq
[n
]);
2438 static void virtio_set_isr(VirtIODevice
*vdev
, int value
)
2440 uint8_t old
= qatomic_read(&vdev
->isr
);
2442 /* Do not write ISR if it does not change, so that its cacheline remains
2443 * shared in the common case where the guest does not read it.
2445 if ((old
& value
) != value
) {
2446 qatomic_or(&vdev
->isr
, value
);
2450 static bool virtio_split_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2454 /* We need to expose used array entries before checking used event. */
2456 /* Always notify when queue is empty (when feature acknowledge) */
2457 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
2458 !vq
->inuse
&& virtio_queue_empty(vq
)) {
2462 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2463 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
2466 v
= vq
->signalled_used_valid
;
2467 vq
->signalled_used_valid
= true;
2468 old
= vq
->signalled_used
;
2469 new = vq
->signalled_used
= vq
->used_idx
;
2470 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
2473 static bool vring_packed_need_event(VirtQueue
*vq
, bool wrap
,
2474 uint16_t off_wrap
, uint16_t new,
2477 int off
= off_wrap
& ~(1 << 15);
2479 if (wrap
!= off_wrap
>> 15) {
2480 off
-= vq
->vring
.num
;
2483 return vring_need_event(off
, new, old
);
2486 static bool virtio_packed_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2488 VRingPackedDescEvent e
;
2491 VRingMemoryRegionCaches
*caches
;
2493 caches
= vring_get_region_caches(vq
);
2498 vring_packed_event_read(vdev
, &caches
->avail
, &e
);
2500 old
= vq
->signalled_used
;
2501 new = vq
->signalled_used
= vq
->used_idx
;
2502 v
= vq
->signalled_used_valid
;
2503 vq
->signalled_used_valid
= true;
2505 if (e
.flags
== VRING_PACKED_EVENT_FLAG_DISABLE
) {
2507 } else if (e
.flags
== VRING_PACKED_EVENT_FLAG_ENABLE
) {
2511 return !v
|| vring_packed_need_event(vq
, vq
->used_wrap_counter
,
2512 e
.off_wrap
, new, old
);
2515 /* Called within rcu_read_lock(). */
2516 static bool virtio_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2518 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
2519 return virtio_packed_should_notify(vdev
, vq
);
2521 return virtio_split_should_notify(vdev
, vq
);
2525 void virtio_notify_irqfd(VirtIODevice
*vdev
, VirtQueue
*vq
)
2527 WITH_RCU_READ_LOCK_GUARD() {
2528 if (!virtio_should_notify(vdev
, vq
)) {
2533 trace_virtio_notify_irqfd(vdev
, vq
);
2536 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
2537 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
2538 * incorrectly polling this bit during crashdump and hibernation
2539 * in MSI mode, causing a hang if this bit is never updated.
2540 * Recent releases of Windows do not really shut down, but rather
2541 * log out and hibernate to make the next startup faster. Hence,
2542 * this manifested as a more serious hang during shutdown with
2544 * Next driver release from 2016 fixed this problem, so working around it
2545 * is not a must, but it's easy to do so let's do it here.
2547 * Note: it's safe to update ISR from any thread as it was switched
2548 * to an atomic operation.
2550 virtio_set_isr(vq
->vdev
, 0x1);
2551 event_notifier_set(&vq
->guest_notifier
);
2554 static void virtio_irq(VirtQueue
*vq
)
2556 virtio_set_isr(vq
->vdev
, 0x1);
2557 virtio_notify_vector(vq
->vdev
, vq
->vector
);
2560 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2562 WITH_RCU_READ_LOCK_GUARD() {
2563 if (!virtio_should_notify(vdev
, vq
)) {
2568 trace_virtio_notify(vdev
, vq
);
2572 void virtio_notify_config(VirtIODevice
*vdev
)
2574 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
2577 virtio_set_isr(vdev
, 0x3);
2579 virtio_notify_vector(vdev
, vdev
->config_vector
);
2582 static bool virtio_device_endian_needed(void *opaque
)
2584 VirtIODevice
*vdev
= opaque
;
2586 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
2587 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2588 return vdev
->device_endian
!= virtio_default_endian();
2590 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
2591 return vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_LITTLE
;
2594 static bool virtio_64bit_features_needed(void *opaque
)
2596 VirtIODevice
*vdev
= opaque
;
2598 return (vdev
->host_features
>> 32) != 0;
2601 static bool virtio_virtqueue_needed(void *opaque
)
2603 VirtIODevice
*vdev
= opaque
;
2605 return virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
);
2608 static bool virtio_packed_virtqueue_needed(void *opaque
)
2610 VirtIODevice
*vdev
= opaque
;
2612 return virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
);
2615 static bool virtio_ringsize_needed(void *opaque
)
2617 VirtIODevice
*vdev
= opaque
;
2620 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2621 if (vdev
->vq
[i
].vring
.num
!= vdev
->vq
[i
].vring
.num_default
) {
2628 static bool virtio_extra_state_needed(void *opaque
)
2630 VirtIODevice
*vdev
= opaque
;
2631 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2632 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2634 return k
->has_extra_state
&&
2635 k
->has_extra_state(qbus
->parent
);
2638 static bool virtio_broken_needed(void *opaque
)
2640 VirtIODevice
*vdev
= opaque
;
2642 return vdev
->broken
;
2645 static bool virtio_started_needed(void *opaque
)
2647 VirtIODevice
*vdev
= opaque
;
2649 return vdev
->started
;
2652 static bool virtio_disabled_needed(void *opaque
)
2654 VirtIODevice
*vdev
= opaque
;
2656 return vdev
->disabled
;
2659 static const VMStateDescription vmstate_virtqueue
= {
2660 .name
= "virtqueue_state",
2662 .minimum_version_id
= 1,
2663 .fields
= (VMStateField
[]) {
2664 VMSTATE_UINT64(vring
.avail
, struct VirtQueue
),
2665 VMSTATE_UINT64(vring
.used
, struct VirtQueue
),
2666 VMSTATE_END_OF_LIST()
2670 static const VMStateDescription vmstate_packed_virtqueue
= {
2671 .name
= "packed_virtqueue_state",
2673 .minimum_version_id
= 1,
2674 .fields
= (VMStateField
[]) {
2675 VMSTATE_UINT16(last_avail_idx
, struct VirtQueue
),
2676 VMSTATE_BOOL(last_avail_wrap_counter
, struct VirtQueue
),
2677 VMSTATE_UINT16(used_idx
, struct VirtQueue
),
2678 VMSTATE_BOOL(used_wrap_counter
, struct VirtQueue
),
2679 VMSTATE_UINT32(inuse
, struct VirtQueue
),
2680 VMSTATE_END_OF_LIST()
2684 static const VMStateDescription vmstate_virtio_virtqueues
= {
2685 .name
= "virtio/virtqueues",
2687 .minimum_version_id
= 1,
2688 .needed
= &virtio_virtqueue_needed
,
2689 .fields
= (VMStateField
[]) {
2690 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2691 VIRTIO_QUEUE_MAX
, 0, vmstate_virtqueue
, VirtQueue
),
2692 VMSTATE_END_OF_LIST()
2696 static const VMStateDescription vmstate_virtio_packed_virtqueues
= {
2697 .name
= "virtio/packed_virtqueues",
2699 .minimum_version_id
= 1,
2700 .needed
= &virtio_packed_virtqueue_needed
,
2701 .fields
= (VMStateField
[]) {
2702 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2703 VIRTIO_QUEUE_MAX
, 0, vmstate_packed_virtqueue
, VirtQueue
),
2704 VMSTATE_END_OF_LIST()
2708 static const VMStateDescription vmstate_ringsize
= {
2709 .name
= "ringsize_state",
2711 .minimum_version_id
= 1,
2712 .fields
= (VMStateField
[]) {
2713 VMSTATE_UINT32(vring
.num_default
, struct VirtQueue
),
2714 VMSTATE_END_OF_LIST()
2718 static const VMStateDescription vmstate_virtio_ringsize
= {
2719 .name
= "virtio/ringsize",
2721 .minimum_version_id
= 1,
2722 .needed
= &virtio_ringsize_needed
,
2723 .fields
= (VMStateField
[]) {
2724 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2725 VIRTIO_QUEUE_MAX
, 0, vmstate_ringsize
, VirtQueue
),
2726 VMSTATE_END_OF_LIST()
2730 static int get_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2731 const VMStateField
*field
)
2733 VirtIODevice
*vdev
= pv
;
2734 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2735 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2737 if (!k
->load_extra_state
) {
2740 return k
->load_extra_state(qbus
->parent
, f
);
2744 static int put_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2745 const VMStateField
*field
, JSONWriter
*vmdesc
)
2747 VirtIODevice
*vdev
= pv
;
2748 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2749 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2751 k
->save_extra_state(qbus
->parent
, f
);
2755 static const VMStateInfo vmstate_info_extra_state
= {
2756 .name
= "virtqueue_extra_state",
2757 .get
= get_extra_state
,
2758 .put
= put_extra_state
,
2761 static const VMStateDescription vmstate_virtio_extra_state
= {
2762 .name
= "virtio/extra_state",
2764 .minimum_version_id
= 1,
2765 .needed
= &virtio_extra_state_needed
,
2766 .fields
= (VMStateField
[]) {
2768 .name
= "extra_state",
2770 .field_exists
= NULL
,
2772 .info
= &vmstate_info_extra_state
,
2773 .flags
= VMS_SINGLE
,
2776 VMSTATE_END_OF_LIST()
2780 static const VMStateDescription vmstate_virtio_device_endian
= {
2781 .name
= "virtio/device_endian",
2783 .minimum_version_id
= 1,
2784 .needed
= &virtio_device_endian_needed
,
2785 .fields
= (VMStateField
[]) {
2786 VMSTATE_UINT8(device_endian
, VirtIODevice
),
2787 VMSTATE_END_OF_LIST()
2791 static const VMStateDescription vmstate_virtio_64bit_features
= {
2792 .name
= "virtio/64bit_features",
2794 .minimum_version_id
= 1,
2795 .needed
= &virtio_64bit_features_needed
,
2796 .fields
= (VMStateField
[]) {
2797 VMSTATE_UINT64(guest_features
, VirtIODevice
),
2798 VMSTATE_END_OF_LIST()
2802 static const VMStateDescription vmstate_virtio_broken
= {
2803 .name
= "virtio/broken",
2805 .minimum_version_id
= 1,
2806 .needed
= &virtio_broken_needed
,
2807 .fields
= (VMStateField
[]) {
2808 VMSTATE_BOOL(broken
, VirtIODevice
),
2809 VMSTATE_END_OF_LIST()
2813 static const VMStateDescription vmstate_virtio_started
= {
2814 .name
= "virtio/started",
2816 .minimum_version_id
= 1,
2817 .needed
= &virtio_started_needed
,
2818 .fields
= (VMStateField
[]) {
2819 VMSTATE_BOOL(started
, VirtIODevice
),
2820 VMSTATE_END_OF_LIST()
2824 static const VMStateDescription vmstate_virtio_disabled
= {
2825 .name
= "virtio/disabled",
2827 .minimum_version_id
= 1,
2828 .needed
= &virtio_disabled_needed
,
2829 .fields
= (VMStateField
[]) {
2830 VMSTATE_BOOL(disabled
, VirtIODevice
),
2831 VMSTATE_END_OF_LIST()
2835 static const VMStateDescription vmstate_virtio
= {
2838 .minimum_version_id
= 1,
2839 .minimum_version_id_old
= 1,
2840 .fields
= (VMStateField
[]) {
2841 VMSTATE_END_OF_LIST()
2843 .subsections
= (const VMStateDescription
*[]) {
2844 &vmstate_virtio_device_endian
,
2845 &vmstate_virtio_64bit_features
,
2846 &vmstate_virtio_virtqueues
,
2847 &vmstate_virtio_ringsize
,
2848 &vmstate_virtio_broken
,
2849 &vmstate_virtio_extra_state
,
2850 &vmstate_virtio_started
,
2851 &vmstate_virtio_packed_virtqueues
,
2852 &vmstate_virtio_disabled
,
2857 int virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
2859 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2860 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2861 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2862 uint32_t guest_features_lo
= (vdev
->guest_features
& 0xffffffff);
2865 if (k
->save_config
) {
2866 k
->save_config(qbus
->parent
, f
);
2869 qemu_put_8s(f
, &vdev
->status
);
2870 qemu_put_8s(f
, &vdev
->isr
);
2871 qemu_put_be16s(f
, &vdev
->queue_sel
);
2872 qemu_put_be32s(f
, &guest_features_lo
);
2873 qemu_put_be32(f
, vdev
->config_len
);
2874 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
2876 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2877 if (vdev
->vq
[i
].vring
.num
== 0)
2881 qemu_put_be32(f
, i
);
2883 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2884 if (vdev
->vq
[i
].vring
.num
== 0)
2887 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
2888 if (k
->has_variable_vring_alignment
) {
2889 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
2892 * Save desc now, the rest of the ring addresses are saved in
2893 * subsections for VIRTIO-1 devices.
2895 qemu_put_be64(f
, vdev
->vq
[i
].vring
.desc
);
2896 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
2897 if (k
->save_queue
) {
2898 k
->save_queue(qbus
->parent
, i
, f
);
2902 if (vdc
->save
!= NULL
) {
2907 int ret
= vmstate_save_state(f
, vdc
->vmsd
, vdev
, NULL
);
2914 return vmstate_save_state(f
, &vmstate_virtio
, vdev
, NULL
);
2917 /* A wrapper for use as a VMState .put function */
2918 static int virtio_device_put(QEMUFile
*f
, void *opaque
, size_t size
,
2919 const VMStateField
*field
, JSONWriter
*vmdesc
)
2921 return virtio_save(VIRTIO_DEVICE(opaque
), f
);
2924 /* A wrapper for use as a VMState .get function */
2925 static int virtio_device_get(QEMUFile
*f
, void *opaque
, size_t size
,
2926 const VMStateField
*field
)
2928 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
2929 DeviceClass
*dc
= DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev
));
2931 return virtio_load(vdev
, f
, dc
->vmsd
->version_id
);
2934 const VMStateInfo virtio_vmstate_info
= {
2936 .get
= virtio_device_get
,
2937 .put
= virtio_device_put
,
2940 static int virtio_set_features_nocheck(VirtIODevice
*vdev
, uint64_t val
)
2942 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2943 bool bad
= (val
& ~(vdev
->host_features
)) != 0;
2945 val
&= vdev
->host_features
;
2946 if (k
->set_features
) {
2947 k
->set_features(vdev
, val
);
2949 vdev
->guest_features
= val
;
2950 return bad
? -1 : 0;
2953 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
)
2957 * The driver must not attempt to set features after feature negotiation
2960 if (vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
2963 ret
= virtio_set_features_nocheck(vdev
, val
);
2964 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2965 /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */
2967 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2968 if (vdev
->vq
[i
].vring
.num
!= 0) {
2969 virtio_init_region_cache(vdev
, i
);
2974 if (!virtio_device_started(vdev
, vdev
->status
) &&
2975 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2976 vdev
->start_on_kick
= true;
2982 size_t virtio_feature_get_config_size(const VirtIOFeature
*feature_sizes
,
2983 uint64_t host_features
)
2985 size_t config_size
= 0;
2988 for (i
= 0; feature_sizes
[i
].flags
!= 0; i
++) {
2989 if (host_features
& feature_sizes
[i
].flags
) {
2990 config_size
= MAX(feature_sizes
[i
].end
, config_size
);
2997 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
)
3003 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3004 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3005 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
3008 * We poison the endianness to ensure it does not get used before
3009 * subsections have been loaded.
3011 vdev
->device_endian
= VIRTIO_DEVICE_ENDIAN_UNKNOWN
;
3013 if (k
->load_config
) {
3014 ret
= k
->load_config(qbus
->parent
, f
);
3019 qemu_get_8s(f
, &vdev
->status
);
3020 qemu_get_8s(f
, &vdev
->isr
);
3021 qemu_get_be16s(f
, &vdev
->queue_sel
);
3022 if (vdev
->queue_sel
>= VIRTIO_QUEUE_MAX
) {
3025 qemu_get_be32s(f
, &features
);
3028 * Temporarily set guest_features low bits - needed by
3029 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
3030 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
3032 * Note: devices should always test host features in future - don't create
3033 * new dependencies like this.
3035 vdev
->guest_features
= features
;
3037 config_len
= qemu_get_be32(f
);
3040 * There are cases where the incoming config can be bigger or smaller
3041 * than what we have; so load what we have space for, and skip
3042 * any excess that's in the stream.
3044 qemu_get_buffer(f
, vdev
->config
, MIN(config_len
, vdev
->config_len
));
3046 while (config_len
> vdev
->config_len
) {
3051 num
= qemu_get_be32(f
);
3053 if (num
> VIRTIO_QUEUE_MAX
) {
3054 error_report("Invalid number of virtqueues: 0x%x", num
);
3058 for (i
= 0; i
< num
; i
++) {
3059 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
3060 if (k
->has_variable_vring_alignment
) {
3061 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
3063 vdev
->vq
[i
].vring
.desc
= qemu_get_be64(f
);
3064 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
3065 vdev
->vq
[i
].signalled_used_valid
= false;
3066 vdev
->vq
[i
].notification
= true;
3068 if (!vdev
->vq
[i
].vring
.desc
&& vdev
->vq
[i
].last_avail_idx
) {
3069 error_report("VQ %d address 0x0 "
3070 "inconsistent with Host index 0x%x",
3071 i
, vdev
->vq
[i
].last_avail_idx
);
3074 if (k
->load_queue
) {
3075 ret
= k
->load_queue(qbus
->parent
, i
, f
);
3081 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
3083 if (vdc
->load
!= NULL
) {
3084 ret
= vdc
->load(vdev
, f
, version_id
);
3091 ret
= vmstate_load_state(f
, vdc
->vmsd
, vdev
, version_id
);
3098 ret
= vmstate_load_state(f
, &vmstate_virtio
, vdev
, 1);
3103 if (vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_UNKNOWN
) {
3104 vdev
->device_endian
= virtio_default_endian();
3107 if (virtio_64bit_features_needed(vdev
)) {
3109 * Subsection load filled vdev->guest_features. Run them
3110 * through virtio_set_features to sanity-check them against
3113 uint64_t features64
= vdev
->guest_features
;
3114 if (virtio_set_features_nocheck(vdev
, features64
) < 0) {
3115 error_report("Features 0x%" PRIx64
" unsupported. "
3116 "Allowed features: 0x%" PRIx64
,
3117 features64
, vdev
->host_features
);
3121 if (virtio_set_features_nocheck(vdev
, features
) < 0) {
3122 error_report("Features 0x%x unsupported. "
3123 "Allowed features: 0x%" PRIx64
,
3124 features
, vdev
->host_features
);
3129 if (!virtio_device_started(vdev
, vdev
->status
) &&
3130 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3131 vdev
->start_on_kick
= true;
3134 RCU_READ_LOCK_GUARD();
3135 for (i
= 0; i
< num
; i
++) {
3136 if (vdev
->vq
[i
].vring
.desc
) {
3140 * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
3141 * only the region cache needs to be set up. Legacy devices need
3142 * to calculate used and avail ring addresses based on the desc
3145 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3146 virtio_init_region_cache(vdev
, i
);
3148 virtio_queue_update_rings(vdev
, i
);
3151 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3152 vdev
->vq
[i
].shadow_avail_idx
= vdev
->vq
[i
].last_avail_idx
;
3153 vdev
->vq
[i
].shadow_avail_wrap_counter
=
3154 vdev
->vq
[i
].last_avail_wrap_counter
;
3158 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
3159 /* Check it isn't doing strange things with descriptor numbers. */
3160 if (nheads
> vdev
->vq
[i
].vring
.num
) {
3161 virtio_error(vdev
, "VQ %d size 0x%x Guest index 0x%x "
3162 "inconsistent with Host index 0x%x: delta 0x%x",
3163 i
, vdev
->vq
[i
].vring
.num
,
3164 vring_avail_idx(&vdev
->vq
[i
]),
3165 vdev
->vq
[i
].last_avail_idx
, nheads
);
3166 vdev
->vq
[i
].used_idx
= 0;
3167 vdev
->vq
[i
].shadow_avail_idx
= 0;
3168 vdev
->vq
[i
].inuse
= 0;
3171 vdev
->vq
[i
].used_idx
= vring_used_idx(&vdev
->vq
[i
]);
3172 vdev
->vq
[i
].shadow_avail_idx
= vring_avail_idx(&vdev
->vq
[i
]);
3175 * Some devices migrate VirtQueueElements that have been popped
3176 * from the avail ring but not yet returned to the used ring.
3177 * Since max ring size < UINT16_MAX it's safe to use modulo
3178 * UINT16_MAX + 1 subtraction.
3180 vdev
->vq
[i
].inuse
= (uint16_t)(vdev
->vq
[i
].last_avail_idx
-
3181 vdev
->vq
[i
].used_idx
);
3182 if (vdev
->vq
[i
].inuse
> vdev
->vq
[i
].vring
.num
) {
3183 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
3185 i
, vdev
->vq
[i
].vring
.num
,
3186 vdev
->vq
[i
].last_avail_idx
,
3187 vdev
->vq
[i
].used_idx
);
3193 if (vdc
->post_load
) {
3194 ret
= vdc
->post_load(vdev
);
3203 void virtio_cleanup(VirtIODevice
*vdev
)
3205 qemu_del_vm_change_state_handler(vdev
->vmstate
);
3208 static void virtio_vmstate_change(void *opaque
, bool running
, RunState state
)
3210 VirtIODevice
*vdev
= opaque
;
3211 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3212 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3213 bool backend_run
= running
&& virtio_device_started(vdev
, vdev
->status
);
3214 vdev
->vm_running
= running
;
3217 virtio_set_status(vdev
, vdev
->status
);
3220 if (k
->vmstate_change
) {
3221 k
->vmstate_change(qbus
->parent
, backend_run
);
3225 virtio_set_status(vdev
, vdev
->status
);
3229 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
3230 size_t vdev_size
, const char *vdev_name
)
3232 DeviceState
*vdev
= data
;
3234 object_initialize_child_with_props(proxy_obj
, "virtio-backend", vdev
,
3235 vdev_size
, vdev_name
, &error_abort
,
3237 qdev_alias_all_properties(vdev
, proxy_obj
);
3240 void virtio_init(VirtIODevice
*vdev
, const char *name
,
3241 uint16_t device_id
, size_t config_size
)
3243 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3244 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3246 int nvectors
= k
->query_nvectors
? k
->query_nvectors(qbus
->parent
) : 0;
3249 vdev
->vector_queues
=
3250 g_malloc0(sizeof(*vdev
->vector_queues
) * nvectors
);
3253 vdev
->start_on_kick
= false;
3254 vdev
->started
= false;
3255 vdev
->device_id
= device_id
;
3257 qatomic_set(&vdev
->isr
, 0);
3258 vdev
->queue_sel
= 0;
3259 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
3260 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_QUEUE_MAX
);
3261 vdev
->vm_running
= runstate_is_running();
3262 vdev
->broken
= false;
3263 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3264 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
3265 vdev
->vq
[i
].vdev
= vdev
;
3266 vdev
->vq
[i
].queue_index
= i
;
3267 vdev
->vq
[i
].host_notifier_enabled
= false;
3271 vdev
->config_len
= config_size
;
3272 if (vdev
->config_len
) {
3273 vdev
->config
= g_malloc0(config_size
);
3275 vdev
->config
= NULL
;
3277 vdev
->vmstate
= qdev_add_vm_change_state_handler(DEVICE(vdev
),
3278 virtio_vmstate_change
, vdev
);
3279 vdev
->device_endian
= virtio_default_endian();
3280 vdev
->use_guest_notifier_mask
= true;
3284 * Only devices that have already been around prior to defining the virtio
3285 * standard support legacy mode; this includes devices not specified in the
3286 * standard. All newer devices conform to the virtio standard only.
3288 bool virtio_legacy_allowed(VirtIODevice
*vdev
)
3290 switch (vdev
->device_id
) {
3292 case VIRTIO_ID_BLOCK
:
3293 case VIRTIO_ID_CONSOLE
:
3295 case VIRTIO_ID_BALLOON
:
3296 case VIRTIO_ID_RPMSG
:
3297 case VIRTIO_ID_SCSI
:
3299 case VIRTIO_ID_RPROC_SERIAL
:
3300 case VIRTIO_ID_CAIF
:
3307 bool virtio_legacy_check_disabled(VirtIODevice
*vdev
)
3309 return vdev
->disable_legacy_check
;
3312 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
3314 return vdev
->vq
[n
].vring
.desc
;
3317 bool virtio_queue_enabled_legacy(VirtIODevice
*vdev
, int n
)
3319 return virtio_queue_get_desc_addr(vdev
, n
) != 0;
3322 bool virtio_queue_enabled(VirtIODevice
*vdev
, int n
)
3324 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3325 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3327 if (k
->queue_enabled
) {
3328 return k
->queue_enabled(qbus
->parent
, n
);
3330 return virtio_queue_enabled_legacy(vdev
, n
);
3333 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
3335 return vdev
->vq
[n
].vring
.avail
;
3338 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
3340 return vdev
->vq
[n
].vring
.used
;
3343 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
3345 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
3348 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
3352 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3353 return sizeof(struct VRingPackedDescEvent
);
3356 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3357 return offsetof(VRingAvail
, ring
) +
3358 sizeof(uint16_t) * vdev
->vq
[n
].vring
.num
+ s
;
3361 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
3365 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3366 return sizeof(struct VRingPackedDescEvent
);
3369 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3370 return offsetof(VRingUsed
, ring
) +
3371 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
+ s
;
3374 static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice
*vdev
,
3377 unsigned int avail
, used
;
3379 avail
= vdev
->vq
[n
].last_avail_idx
;
3380 avail
|= ((uint16_t)vdev
->vq
[n
].last_avail_wrap_counter
) << 15;
3382 used
= vdev
->vq
[n
].used_idx
;
3383 used
|= ((uint16_t)vdev
->vq
[n
].used_wrap_counter
) << 15;
3385 return avail
| used
<< 16;
3388 static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice
*vdev
,
3391 return vdev
->vq
[n
].last_avail_idx
;
3394 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
3396 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3397 return virtio_queue_packed_get_last_avail_idx(vdev
, n
);
3399 return virtio_queue_split_get_last_avail_idx(vdev
, n
);
3403 static void virtio_queue_packed_set_last_avail_idx(VirtIODevice
*vdev
,
3404 int n
, unsigned int idx
)
3406 struct VirtQueue
*vq
= &vdev
->vq
[n
];
3408 vq
->last_avail_idx
= vq
->shadow_avail_idx
= idx
& 0x7fff;
3409 vq
->last_avail_wrap_counter
=
3410 vq
->shadow_avail_wrap_counter
= !!(idx
& 0x8000);
3412 vq
->used_idx
= idx
& 0x7ffff;
3413 vq
->used_wrap_counter
= !!(idx
& 0x8000);
3416 static void virtio_queue_split_set_last_avail_idx(VirtIODevice
*vdev
,
3417 int n
, unsigned int idx
)
3419 vdev
->vq
[n
].last_avail_idx
= idx
;
3420 vdev
->vq
[n
].shadow_avail_idx
= idx
;
3423 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
,
3426 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3427 virtio_queue_packed_set_last_avail_idx(vdev
, n
, idx
);
3429 virtio_queue_split_set_last_avail_idx(vdev
, n
, idx
);
3433 static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice
*vdev
,
3436 /* We don't have a reference like avail idx in shared memory */
3440 static void virtio_queue_split_restore_last_avail_idx(VirtIODevice
*vdev
,
3443 RCU_READ_LOCK_GUARD();
3444 if (vdev
->vq
[n
].vring
.desc
) {
3445 vdev
->vq
[n
].last_avail_idx
= vring_used_idx(&vdev
->vq
[n
]);
3446 vdev
->vq
[n
].shadow_avail_idx
= vdev
->vq
[n
].last_avail_idx
;
3450 void virtio_queue_restore_last_avail_idx(VirtIODevice
*vdev
, int n
)
3452 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3453 virtio_queue_packed_restore_last_avail_idx(vdev
, n
);
3455 virtio_queue_split_restore_last_avail_idx(vdev
, n
);
3459 static void virtio_queue_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3461 /* used idx was updated through set_last_avail_idx() */
3465 static void virtio_split_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3467 RCU_READ_LOCK_GUARD();
3468 if (vdev
->vq
[n
].vring
.desc
) {
3469 vdev
->vq
[n
].used_idx
= vring_used_idx(&vdev
->vq
[n
]);
3473 void virtio_queue_update_used_idx(VirtIODevice
*vdev
, int n
)
3475 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3476 return virtio_queue_packed_update_used_idx(vdev
, n
);
3478 return virtio_split_packed_update_used_idx(vdev
, n
);
3482 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
3484 vdev
->vq
[n
].signalled_used_valid
= false;
3487 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
3489 return vdev
->vq
+ n
;
3492 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
3494 return vq
->queue_index
;
3497 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
3499 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
3500 if (event_notifier_test_and_clear(n
)) {
3505 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
3508 if (assign
&& !with_irqfd
) {
3509 event_notifier_set_handler(&vq
->guest_notifier
,
3510 virtio_queue_guest_notifier_read
);
3512 event_notifier_set_handler(&vq
->guest_notifier
, NULL
);
3515 /* Test and clear notifier before closing it,
3516 * in case poll callback didn't have time to run. */
3517 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
3521 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
3523 return &vq
->guest_notifier
;
3526 static void virtio_queue_host_notifier_aio_read(EventNotifier
*n
)
3528 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3529 if (event_notifier_test_and_clear(n
)) {
3530 virtio_queue_notify_aio_vq(vq
);
3534 static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier
*n
)
3536 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3538 virtio_queue_set_notification(vq
, 0);
3541 static bool virtio_queue_host_notifier_aio_poll(void *opaque
)
3543 EventNotifier
*n
= opaque
;
3544 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3546 if (!vq
->vring
.desc
|| virtio_queue_empty(vq
)) {
3550 return virtio_queue_notify_aio_vq(vq
);
3553 static void virtio_queue_host_notifier_aio_poll_end(EventNotifier
*n
)
3555 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3557 /* Caller polls once more after this to catch requests that race with us */
3558 virtio_queue_set_notification(vq
, 1);
3561 void virtio_queue_aio_set_host_notifier_handler(VirtQueue
*vq
, AioContext
*ctx
,
3562 VirtIOHandleAIOOutput handle_output
)
3564 if (handle_output
) {
3565 vq
->handle_aio_output
= handle_output
;
3566 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true,
3567 virtio_queue_host_notifier_aio_read
,
3568 virtio_queue_host_notifier_aio_poll
);
3569 aio_set_event_notifier_poll(ctx
, &vq
->host_notifier
,
3570 virtio_queue_host_notifier_aio_poll_begin
,
3571 virtio_queue_host_notifier_aio_poll_end
);
3573 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true, NULL
, NULL
);
3574 /* Test and clear notifier before after disabling event,
3575 * in case poll callback didn't have time to run. */
3576 virtio_queue_host_notifier_aio_read(&vq
->host_notifier
);
3577 vq
->handle_aio_output
= NULL
;
3581 void virtio_queue_host_notifier_read(EventNotifier
*n
)
3583 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3584 if (event_notifier_test_and_clear(n
)) {
3585 virtio_queue_notify_vq(vq
);
3589 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
3591 return &vq
->host_notifier
;
3594 void virtio_queue_set_host_notifier_enabled(VirtQueue
*vq
, bool enabled
)
3596 vq
->host_notifier_enabled
= enabled
;
3599 int virtio_queue_set_host_notifier_mr(VirtIODevice
*vdev
, int n
,
3600 MemoryRegion
*mr
, bool assign
)
3602 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3603 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3605 if (k
->set_host_notifier_mr
) {
3606 return k
->set_host_notifier_mr(qbus
->parent
, n
, mr
, assign
);
3612 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
3614 g_free(vdev
->bus_name
);
3615 vdev
->bus_name
= g_strdup(bus_name
);
3618 void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice
*vdev
, const char *fmt
, ...)
3623 error_vreport(fmt
, ap
);
3626 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3627 vdev
->status
= vdev
->status
| VIRTIO_CONFIG_S_NEEDS_RESET
;
3628 virtio_notify_config(vdev
);
3631 vdev
->broken
= true;
3634 static void virtio_memory_listener_commit(MemoryListener
*listener
)
3636 VirtIODevice
*vdev
= container_of(listener
, VirtIODevice
, listener
);
3639 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3640 if (vdev
->vq
[i
].vring
.num
== 0) {
3643 virtio_init_region_cache(vdev
, i
);
3647 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
3649 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3650 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3653 /* Devices should either use vmsd or the load/save methods */
3654 assert(!vdc
->vmsd
|| !vdc
->load
);
3656 if (vdc
->realize
!= NULL
) {
3657 vdc
->realize(dev
, &err
);
3659 error_propagate(errp
, err
);
3664 virtio_bus_device_plugged(vdev
, &err
);
3666 error_propagate(errp
, err
);
3667 vdc
->unrealize(dev
);
3671 vdev
->listener
.commit
= virtio_memory_listener_commit
;
3672 memory_listener_register(&vdev
->listener
, vdev
->dma_as
);
3675 static void virtio_device_unrealize(DeviceState
*dev
)
3677 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3678 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3680 memory_listener_unregister(&vdev
->listener
);
3681 virtio_bus_device_unplugged(vdev
);
3683 if (vdc
->unrealize
!= NULL
) {
3684 vdc
->unrealize(dev
);
3687 g_free(vdev
->bus_name
);
3688 vdev
->bus_name
= NULL
;
3691 static void virtio_device_free_virtqueues(VirtIODevice
*vdev
)
3698 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3699 if (vdev
->vq
[i
].vring
.num
== 0) {
3702 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
3707 static void virtio_device_instance_finalize(Object
*obj
)
3709 VirtIODevice
*vdev
= VIRTIO_DEVICE(obj
);
3711 virtio_device_free_virtqueues(vdev
);
3713 g_free(vdev
->config
);
3714 g_free(vdev
->vector_queues
);
3717 static Property virtio_properties
[] = {
3718 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice
, host_features
),
3719 DEFINE_PROP_BOOL("use-started", VirtIODevice
, use_started
, true),
3720 DEFINE_PROP_BOOL("use-disabled-flag", VirtIODevice
, use_disabled_flag
, true),
3721 DEFINE_PROP_BOOL("x-disable-legacy-check", VirtIODevice
,
3722 disable_legacy_check
, false),
3723 DEFINE_PROP_END_OF_LIST(),
3726 static int virtio_device_start_ioeventfd_impl(VirtIODevice
*vdev
)
3728 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3731 memory_region_transaction_begin();
3732 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3733 VirtQueue
*vq
= &vdev
->vq
[n
];
3734 if (!virtio_queue_get_num(vdev
, n
)) {
3737 r
= virtio_bus_set_host_notifier(qbus
, n
, true);
3742 event_notifier_set_handler(&vq
->host_notifier
,
3743 virtio_queue_host_notifier_read
);
3746 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3747 /* Kick right away to begin processing requests already in vring */
3748 VirtQueue
*vq
= &vdev
->vq
[n
];
3749 if (!vq
->vring
.num
) {
3752 event_notifier_set(&vq
->host_notifier
);
3754 memory_region_transaction_commit();
3758 i
= n
; /* save n for a second iteration after transaction is committed. */
3760 VirtQueue
*vq
= &vdev
->vq
[n
];
3761 if (!virtio_queue_get_num(vdev
, n
)) {
3765 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3766 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3769 memory_region_transaction_commit();
3772 if (!virtio_queue_get_num(vdev
, i
)) {
3775 virtio_bus_cleanup_host_notifier(qbus
, i
);
3780 int virtio_device_start_ioeventfd(VirtIODevice
*vdev
)
3782 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3783 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3785 return virtio_bus_start_ioeventfd(vbus
);
3788 static void virtio_device_stop_ioeventfd_impl(VirtIODevice
*vdev
)
3790 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3793 memory_region_transaction_begin();
3794 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3795 VirtQueue
*vq
= &vdev
->vq
[n
];
3797 if (!virtio_queue_get_num(vdev
, n
)) {
3800 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3801 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3804 memory_region_transaction_commit();
3806 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3807 if (!virtio_queue_get_num(vdev
, n
)) {
3810 virtio_bus_cleanup_host_notifier(qbus
, n
);
3814 int virtio_device_grab_ioeventfd(VirtIODevice
*vdev
)
3816 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3817 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3819 return virtio_bus_grab_ioeventfd(vbus
);
3822 void virtio_device_release_ioeventfd(VirtIODevice
*vdev
)
3824 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3825 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3827 virtio_bus_release_ioeventfd(vbus
);
3830 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
3832 /* Set the default value here. */
3833 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
3834 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3836 dc
->realize
= virtio_device_realize
;
3837 dc
->unrealize
= virtio_device_unrealize
;
3838 dc
->bus_type
= TYPE_VIRTIO_BUS
;
3839 device_class_set_props(dc
, virtio_properties
);
3840 vdc
->start_ioeventfd
= virtio_device_start_ioeventfd_impl
;
3841 vdc
->stop_ioeventfd
= virtio_device_stop_ioeventfd_impl
;
3843 vdc
->legacy_features
|= VIRTIO_LEGACY_FEATURES
;
3846 bool virtio_device_ioeventfd_enabled(VirtIODevice
*vdev
)
3848 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3849 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3851 return virtio_bus_ioeventfd_enabled(vbus
);
3854 static const TypeInfo virtio_device_info
= {
3855 .name
= TYPE_VIRTIO_DEVICE
,
3856 .parent
= TYPE_DEVICE
,
3857 .instance_size
= sizeof(VirtIODevice
),
3858 .class_init
= virtio_device_class_init
,
3859 .instance_finalize
= virtio_device_instance_finalize
,
3861 .class_size
= sizeof(VirtioDeviceClass
),
3864 static void virtio_register_types(void)
3866 type_register_static(&virtio_device_info
);
3869 type_init(virtio_register_types
)