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 "exec/address-spaces.h"
19 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
22 #include "qemu/module.h"
23 #include "hw/virtio/virtio.h"
24 #include "migration/qemu-file-types.h"
25 #include "qemu/atomic.h"
26 #include "hw/virtio/virtio-bus.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/virtio/virtio-access.h"
29 #include "sysemu/dma.h"
30 #include "sysemu/runstate.h"
31 #include "standard-headers/linux/virtio_ids.h"
34 * The alignment to use between consumer and producer parts of vring.
35 * x86 pagesize again. This is the default, used by transports like PCI
36 * which don't provide a means for the guest to tell the host the alignment.
38 #define VIRTIO_PCI_VRING_ALIGN 4096
40 typedef struct VRingDesc
48 typedef struct VRingPackedDesc
{
55 typedef struct VRingAvail
62 typedef struct VRingUsedElem
68 typedef struct VRingUsed
75 typedef struct VRingMemoryRegionCaches
{
77 MemoryRegionCache desc
;
78 MemoryRegionCache avail
;
79 MemoryRegionCache used
;
80 } VRingMemoryRegionCaches
;
85 unsigned int num_default
;
90 VRingMemoryRegionCaches
*caches
;
93 typedef struct VRingPackedDescEvent
{
96 } VRingPackedDescEvent
;
101 VirtQueueElement
*used_elems
;
103 /* Next head to pop */
104 uint16_t last_avail_idx
;
105 bool last_avail_wrap_counter
;
107 /* Last avail_idx read from VQ. */
108 uint16_t shadow_avail_idx
;
109 bool shadow_avail_wrap_counter
;
112 bool used_wrap_counter
;
114 /* Last used index value we have signalled on */
115 uint16_t signalled_used
;
117 /* Last used index value we have signalled on */
118 bool signalled_used_valid
;
120 /* Notification enabled? */
123 uint16_t queue_index
;
128 VirtIOHandleOutput handle_output
;
129 VirtIOHandleAIOOutput handle_aio_output
;
131 EventNotifier guest_notifier
;
132 EventNotifier host_notifier
;
133 bool host_notifier_enabled
;
134 QLIST_ENTRY(VirtQueue
) node
;
137 static void virtio_free_region_cache(VRingMemoryRegionCaches
*caches
)
143 address_space_cache_destroy(&caches
->desc
);
144 address_space_cache_destroy(&caches
->avail
);
145 address_space_cache_destroy(&caches
->used
);
149 static void virtio_virtqueue_reset_region_cache(struct VirtQueue
*vq
)
151 VRingMemoryRegionCaches
*caches
;
153 caches
= qatomic_read(&vq
->vring
.caches
);
154 qatomic_rcu_set(&vq
->vring
.caches
, NULL
);
156 call_rcu(caches
, virtio_free_region_cache
, rcu
);
160 static void virtio_init_region_cache(VirtIODevice
*vdev
, int n
)
162 VirtQueue
*vq
= &vdev
->vq
[n
];
163 VRingMemoryRegionCaches
*old
= vq
->vring
.caches
;
164 VRingMemoryRegionCaches
*new = NULL
;
170 addr
= vq
->vring
.desc
;
174 new = g_new0(VRingMemoryRegionCaches
, 1);
175 size
= virtio_queue_get_desc_size(vdev
, n
);
176 packed
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
178 len
= address_space_cache_init(&new->desc
, vdev
->dma_as
,
181 virtio_error(vdev
, "Cannot map desc");
185 size
= virtio_queue_get_used_size(vdev
, n
);
186 len
= address_space_cache_init(&new->used
, vdev
->dma_as
,
187 vq
->vring
.used
, size
, true);
189 virtio_error(vdev
, "Cannot map used");
193 size
= virtio_queue_get_avail_size(vdev
, n
);
194 len
= address_space_cache_init(&new->avail
, vdev
->dma_as
,
195 vq
->vring
.avail
, size
, false);
197 virtio_error(vdev
, "Cannot map avail");
201 qatomic_rcu_set(&vq
->vring
.caches
, new);
203 call_rcu(old
, virtio_free_region_cache
, rcu
);
208 address_space_cache_destroy(&new->avail
);
210 address_space_cache_destroy(&new->used
);
212 address_space_cache_destroy(&new->desc
);
215 virtio_virtqueue_reset_region_cache(vq
);
218 /* virt queue functions */
219 void virtio_queue_update_rings(VirtIODevice
*vdev
, int n
)
221 VRing
*vring
= &vdev
->vq
[n
].vring
;
223 if (!vring
->num
|| !vring
->desc
|| !vring
->align
) {
224 /* not yet setup -> nothing to do */
227 vring
->avail
= vring
->desc
+ vring
->num
* sizeof(VRingDesc
);
228 vring
->used
= vring_align(vring
->avail
+
229 offsetof(VRingAvail
, ring
[vring
->num
]),
231 virtio_init_region_cache(vdev
, n
);
234 /* Called within rcu_read_lock(). */
235 static void vring_split_desc_read(VirtIODevice
*vdev
, VRingDesc
*desc
,
236 MemoryRegionCache
*cache
, int i
)
238 address_space_read_cached(cache
, i
* sizeof(VRingDesc
),
239 desc
, sizeof(VRingDesc
));
240 virtio_tswap64s(vdev
, &desc
->addr
);
241 virtio_tswap32s(vdev
, &desc
->len
);
242 virtio_tswap16s(vdev
, &desc
->flags
);
243 virtio_tswap16s(vdev
, &desc
->next
);
246 static void vring_packed_event_read(VirtIODevice
*vdev
,
247 MemoryRegionCache
*cache
,
248 VRingPackedDescEvent
*e
)
250 hwaddr off_off
= offsetof(VRingPackedDescEvent
, off_wrap
);
251 hwaddr off_flags
= offsetof(VRingPackedDescEvent
, flags
);
253 address_space_read_cached(cache
, off_flags
, &e
->flags
,
255 /* Make sure flags is seen before off_wrap */
257 address_space_read_cached(cache
, off_off
, &e
->off_wrap
,
258 sizeof(e
->off_wrap
));
259 virtio_tswap16s(vdev
, &e
->off_wrap
);
260 virtio_tswap16s(vdev
, &e
->flags
);
263 static void vring_packed_off_wrap_write(VirtIODevice
*vdev
,
264 MemoryRegionCache
*cache
,
267 hwaddr off
= offsetof(VRingPackedDescEvent
, off_wrap
);
269 virtio_tswap16s(vdev
, &off_wrap
);
270 address_space_write_cached(cache
, off
, &off_wrap
, sizeof(off_wrap
));
271 address_space_cache_invalidate(cache
, off
, sizeof(off_wrap
));
274 static void vring_packed_flags_write(VirtIODevice
*vdev
,
275 MemoryRegionCache
*cache
, uint16_t flags
)
277 hwaddr off
= offsetof(VRingPackedDescEvent
, flags
);
279 virtio_tswap16s(vdev
, &flags
);
280 address_space_write_cached(cache
, off
, &flags
, sizeof(flags
));
281 address_space_cache_invalidate(cache
, off
, sizeof(flags
));
284 /* Called within rcu_read_lock(). */
285 static VRingMemoryRegionCaches
*vring_get_region_caches(struct VirtQueue
*vq
)
287 return qatomic_rcu_read(&vq
->vring
.caches
);
290 /* Called within rcu_read_lock(). */
291 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
293 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
294 hwaddr pa
= offsetof(VRingAvail
, flags
);
300 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
303 /* Called within rcu_read_lock(). */
304 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
306 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
307 hwaddr pa
= offsetof(VRingAvail
, idx
);
313 vq
->shadow_avail_idx
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
314 return vq
->shadow_avail_idx
;
317 /* Called within rcu_read_lock(). */
318 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
320 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
321 hwaddr pa
= offsetof(VRingAvail
, ring
[i
]);
327 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
330 /* Called within rcu_read_lock(). */
331 static inline uint16_t vring_get_used_event(VirtQueue
*vq
)
333 return vring_avail_ring(vq
, vq
->vring
.num
);
336 /* Called within rcu_read_lock(). */
337 static inline void vring_used_write(VirtQueue
*vq
, VRingUsedElem
*uelem
,
340 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
341 hwaddr pa
= offsetof(VRingUsed
, ring
[i
]);
347 virtio_tswap32s(vq
->vdev
, &uelem
->id
);
348 virtio_tswap32s(vq
->vdev
, &uelem
->len
);
349 address_space_write_cached(&caches
->used
, pa
, uelem
, sizeof(VRingUsedElem
));
350 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(VRingUsedElem
));
353 /* Called within rcu_read_lock(). */
354 static uint16_t vring_used_idx(VirtQueue
*vq
)
356 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
357 hwaddr pa
= offsetof(VRingUsed
, idx
);
363 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
366 /* Called within rcu_read_lock(). */
367 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
369 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
370 hwaddr pa
= offsetof(VRingUsed
, idx
);
373 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
374 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
380 /* Called within rcu_read_lock(). */
381 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
383 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
384 VirtIODevice
*vdev
= vq
->vdev
;
385 hwaddr pa
= offsetof(VRingUsed
, flags
);
392 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
393 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
| mask
);
394 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
397 /* Called within rcu_read_lock(). */
398 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
400 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
401 VirtIODevice
*vdev
= vq
->vdev
;
402 hwaddr pa
= offsetof(VRingUsed
, flags
);
409 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
410 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
& ~mask
);
411 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
414 /* Called within rcu_read_lock(). */
415 static inline void vring_set_avail_event(VirtQueue
*vq
, uint16_t val
)
417 VRingMemoryRegionCaches
*caches
;
419 if (!vq
->notification
) {
423 caches
= vring_get_region_caches(vq
);
428 pa
= offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
429 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
430 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
433 static void virtio_queue_split_set_notification(VirtQueue
*vq
, int enable
)
435 RCU_READ_LOCK_GUARD();
437 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
438 vring_set_avail_event(vq
, vring_avail_idx(vq
));
440 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
442 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
445 /* Expose avail event/used flags before caller checks the avail idx. */
450 static void virtio_queue_packed_set_notification(VirtQueue
*vq
, int enable
)
453 VRingPackedDescEvent e
;
454 VRingMemoryRegionCaches
*caches
;
456 RCU_READ_LOCK_GUARD();
457 caches
= vring_get_region_caches(vq
);
462 vring_packed_event_read(vq
->vdev
, &caches
->used
, &e
);
465 e
.flags
= VRING_PACKED_EVENT_FLAG_DISABLE
;
466 } else if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
467 off_wrap
= vq
->shadow_avail_idx
| vq
->shadow_avail_wrap_counter
<< 15;
468 vring_packed_off_wrap_write(vq
->vdev
, &caches
->used
, off_wrap
);
469 /* Make sure off_wrap is wrote before flags */
471 e
.flags
= VRING_PACKED_EVENT_FLAG_DESC
;
473 e
.flags
= VRING_PACKED_EVENT_FLAG_ENABLE
;
476 vring_packed_flags_write(vq
->vdev
, &caches
->used
, e
.flags
);
478 /* Expose avail event/used flags before caller checks the avail idx. */
483 bool virtio_queue_get_notification(VirtQueue
*vq
)
485 return vq
->notification
;
488 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
490 vq
->notification
= enable
;
492 if (!vq
->vring
.desc
) {
496 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
497 virtio_queue_packed_set_notification(vq
, enable
);
499 virtio_queue_split_set_notification(vq
, enable
);
503 int virtio_queue_ready(VirtQueue
*vq
)
505 return vq
->vring
.avail
!= 0;
508 static void vring_packed_desc_read_flags(VirtIODevice
*vdev
,
510 MemoryRegionCache
*cache
,
513 address_space_read_cached(cache
,
514 i
* sizeof(VRingPackedDesc
) +
515 offsetof(VRingPackedDesc
, flags
),
516 flags
, sizeof(*flags
));
517 virtio_tswap16s(vdev
, flags
);
520 static void vring_packed_desc_read(VirtIODevice
*vdev
,
521 VRingPackedDesc
*desc
,
522 MemoryRegionCache
*cache
,
523 int i
, bool strict_order
)
525 hwaddr off
= i
* sizeof(VRingPackedDesc
);
527 vring_packed_desc_read_flags(vdev
, &desc
->flags
, cache
, i
);
530 /* Make sure flags is read before the rest fields. */
534 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, addr
),
535 &desc
->addr
, sizeof(desc
->addr
));
536 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, id
),
537 &desc
->id
, sizeof(desc
->id
));
538 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, len
),
539 &desc
->len
, sizeof(desc
->len
));
540 virtio_tswap64s(vdev
, &desc
->addr
);
541 virtio_tswap16s(vdev
, &desc
->id
);
542 virtio_tswap32s(vdev
, &desc
->len
);
545 static void vring_packed_desc_write_data(VirtIODevice
*vdev
,
546 VRingPackedDesc
*desc
,
547 MemoryRegionCache
*cache
,
550 hwaddr off_id
= i
* sizeof(VRingPackedDesc
) +
551 offsetof(VRingPackedDesc
, id
);
552 hwaddr off_len
= i
* sizeof(VRingPackedDesc
) +
553 offsetof(VRingPackedDesc
, len
);
555 virtio_tswap32s(vdev
, &desc
->len
);
556 virtio_tswap16s(vdev
, &desc
->id
);
557 address_space_write_cached(cache
, off_id
, &desc
->id
, sizeof(desc
->id
));
558 address_space_cache_invalidate(cache
, off_id
, sizeof(desc
->id
));
559 address_space_write_cached(cache
, off_len
, &desc
->len
, sizeof(desc
->len
));
560 address_space_cache_invalidate(cache
, off_len
, sizeof(desc
->len
));
563 static void vring_packed_desc_write_flags(VirtIODevice
*vdev
,
564 VRingPackedDesc
*desc
,
565 MemoryRegionCache
*cache
,
568 hwaddr off
= i
* sizeof(VRingPackedDesc
) + offsetof(VRingPackedDesc
, flags
);
570 virtio_tswap16s(vdev
, &desc
->flags
);
571 address_space_write_cached(cache
, off
, &desc
->flags
, sizeof(desc
->flags
));
572 address_space_cache_invalidate(cache
, off
, sizeof(desc
->flags
));
575 static void vring_packed_desc_write(VirtIODevice
*vdev
,
576 VRingPackedDesc
*desc
,
577 MemoryRegionCache
*cache
,
578 int i
, bool strict_order
)
580 vring_packed_desc_write_data(vdev
, desc
, cache
, i
);
582 /* Make sure data is wrote before flags. */
585 vring_packed_desc_write_flags(vdev
, desc
, cache
, i
);
588 static inline bool is_desc_avail(uint16_t flags
, bool wrap_counter
)
592 avail
= !!(flags
& (1 << VRING_PACKED_DESC_F_AVAIL
));
593 used
= !!(flags
& (1 << VRING_PACKED_DESC_F_USED
));
594 return (avail
!= used
) && (avail
== wrap_counter
);
597 /* Fetch avail_idx from VQ memory only when we really need to know if
598 * guest has added some buffers.
599 * Called within rcu_read_lock(). */
600 static int virtio_queue_empty_rcu(VirtQueue
*vq
)
602 if (virtio_device_disabled(vq
->vdev
)) {
606 if (unlikely(!vq
->vring
.avail
)) {
610 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
614 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
617 static int virtio_queue_split_empty(VirtQueue
*vq
)
621 if (virtio_device_disabled(vq
->vdev
)) {
625 if (unlikely(!vq
->vring
.avail
)) {
629 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
633 RCU_READ_LOCK_GUARD();
634 empty
= vring_avail_idx(vq
) == vq
->last_avail_idx
;
638 static int virtio_queue_packed_empty_rcu(VirtQueue
*vq
)
640 struct VRingPackedDesc desc
;
641 VRingMemoryRegionCaches
*cache
;
643 if (unlikely(!vq
->vring
.desc
)) {
647 cache
= vring_get_region_caches(vq
);
652 vring_packed_desc_read_flags(vq
->vdev
, &desc
.flags
, &cache
->desc
,
655 return !is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
);
658 static int virtio_queue_packed_empty(VirtQueue
*vq
)
660 RCU_READ_LOCK_GUARD();
661 return virtio_queue_packed_empty_rcu(vq
);
664 int virtio_queue_empty(VirtQueue
*vq
)
666 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
667 return virtio_queue_packed_empty(vq
);
669 return virtio_queue_split_empty(vq
);
673 static void virtqueue_unmap_sg(VirtQueue
*vq
, const VirtQueueElement
*elem
,
676 AddressSpace
*dma_as
= vq
->vdev
->dma_as
;
681 for (i
= 0; i
< elem
->in_num
; i
++) {
682 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
684 dma_memory_unmap(dma_as
, elem
->in_sg
[i
].iov_base
,
685 elem
->in_sg
[i
].iov_len
,
686 DMA_DIRECTION_FROM_DEVICE
, size
);
691 for (i
= 0; i
< elem
->out_num
; i
++)
692 dma_memory_unmap(dma_as
, elem
->out_sg
[i
].iov_base
,
693 elem
->out_sg
[i
].iov_len
,
694 DMA_DIRECTION_TO_DEVICE
,
695 elem
->out_sg
[i
].iov_len
);
698 /* virtqueue_detach_element:
699 * @vq: The #VirtQueue
700 * @elem: The #VirtQueueElement
701 * @len: number of bytes written
703 * Detach the element from the virtqueue. This function is suitable for device
704 * reset or other situations where a #VirtQueueElement is simply freed and will
705 * not be pushed or discarded.
707 void virtqueue_detach_element(VirtQueue
*vq
, const VirtQueueElement
*elem
,
710 vq
->inuse
-= elem
->ndescs
;
711 virtqueue_unmap_sg(vq
, elem
, len
);
714 static void virtqueue_split_rewind(VirtQueue
*vq
, unsigned int num
)
716 vq
->last_avail_idx
-= num
;
719 static void virtqueue_packed_rewind(VirtQueue
*vq
, unsigned int num
)
721 if (vq
->last_avail_idx
< num
) {
722 vq
->last_avail_idx
= vq
->vring
.num
+ vq
->last_avail_idx
- num
;
723 vq
->last_avail_wrap_counter
^= 1;
725 vq
->last_avail_idx
-= num
;
730 * @vq: The #VirtQueue
731 * @elem: The #VirtQueueElement
732 * @len: number of bytes written
734 * Pretend the most recent element wasn't popped from the virtqueue. The next
735 * call to virtqueue_pop() will refetch the element.
737 void virtqueue_unpop(VirtQueue
*vq
, const VirtQueueElement
*elem
,
741 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
742 virtqueue_packed_rewind(vq
, 1);
744 virtqueue_split_rewind(vq
, 1);
747 virtqueue_detach_element(vq
, elem
, len
);
751 * @vq: The #VirtQueue
752 * @num: Number of elements to push back
754 * Pretend that elements weren't popped from the virtqueue. The next
755 * virtqueue_pop() will refetch the oldest element.
757 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
759 * Returns: true on success, false if @num is greater than the number of in use
762 bool virtqueue_rewind(VirtQueue
*vq
, unsigned int num
)
764 if (num
> vq
->inuse
) {
769 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
770 virtqueue_packed_rewind(vq
, num
);
772 virtqueue_split_rewind(vq
, num
);
777 static void virtqueue_split_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
778 unsigned int len
, unsigned int idx
)
782 if (unlikely(!vq
->vring
.used
)) {
786 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
788 uelem
.id
= elem
->index
;
790 vring_used_write(vq
, &uelem
, idx
);
793 static void virtqueue_packed_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
794 unsigned int len
, unsigned int idx
)
796 vq
->used_elems
[idx
].index
= elem
->index
;
797 vq
->used_elems
[idx
].len
= len
;
798 vq
->used_elems
[idx
].ndescs
= elem
->ndescs
;
801 static void virtqueue_packed_fill_desc(VirtQueue
*vq
,
802 const VirtQueueElement
*elem
,
807 VRingMemoryRegionCaches
*caches
;
808 VRingPackedDesc desc
= {
812 bool wrap_counter
= vq
->used_wrap_counter
;
814 if (unlikely(!vq
->vring
.desc
)) {
818 head
= vq
->used_idx
+ idx
;
819 if (head
>= vq
->vring
.num
) {
820 head
-= vq
->vring
.num
;
824 desc
.flags
|= (1 << VRING_PACKED_DESC_F_AVAIL
);
825 desc
.flags
|= (1 << VRING_PACKED_DESC_F_USED
);
827 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_AVAIL
);
828 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_USED
);
831 caches
= vring_get_region_caches(vq
);
836 vring_packed_desc_write(vq
->vdev
, &desc
, &caches
->desc
, head
, strict_order
);
839 /* Called within rcu_read_lock(). */
840 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
841 unsigned int len
, unsigned int idx
)
843 trace_virtqueue_fill(vq
, elem
, len
, idx
);
845 virtqueue_unmap_sg(vq
, elem
, len
);
847 if (virtio_device_disabled(vq
->vdev
)) {
851 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
852 virtqueue_packed_fill(vq
, elem
, len
, idx
);
854 virtqueue_split_fill(vq
, elem
, len
, idx
);
858 /* Called within rcu_read_lock(). */
859 static void virtqueue_split_flush(VirtQueue
*vq
, unsigned int count
)
863 if (unlikely(!vq
->vring
.used
)) {
867 /* Make sure buffer is written before we update index. */
869 trace_virtqueue_flush(vq
, count
);
872 vring_used_idx_set(vq
, new);
874 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
875 vq
->signalled_used_valid
= false;
878 static void virtqueue_packed_flush(VirtQueue
*vq
, unsigned int count
)
880 unsigned int i
, ndescs
= 0;
882 if (unlikely(!vq
->vring
.desc
)) {
886 for (i
= 1; i
< count
; i
++) {
887 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[i
], i
, false);
888 ndescs
+= vq
->used_elems
[i
].ndescs
;
890 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[0], 0, true);
891 ndescs
+= vq
->used_elems
[0].ndescs
;
894 vq
->used_idx
+= ndescs
;
895 if (vq
->used_idx
>= vq
->vring
.num
) {
896 vq
->used_idx
-= vq
->vring
.num
;
897 vq
->used_wrap_counter
^= 1;
901 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
903 if (virtio_device_disabled(vq
->vdev
)) {
908 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
909 virtqueue_packed_flush(vq
, count
);
911 virtqueue_split_flush(vq
, count
);
915 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
918 RCU_READ_LOCK_GUARD();
919 virtqueue_fill(vq
, elem
, len
, 0);
920 virtqueue_flush(vq
, 1);
923 /* Called within rcu_read_lock(). */
924 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
926 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
928 /* Check it isn't doing very strange things with descriptor numbers. */
929 if (num_heads
> vq
->vring
.num
) {
930 virtio_error(vq
->vdev
, "Guest moved used index from %u to %u",
931 idx
, vq
->shadow_avail_idx
);
934 /* On success, callers read a descriptor at vq->last_avail_idx.
935 * Make sure descriptor read does not bypass avail index read. */
943 /* Called within rcu_read_lock(). */
944 static bool virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
,
947 /* Grab the next descriptor number they're advertising, and increment
948 * the index we've seen. */
949 *head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
951 /* If their number is silly, that's a fatal mistake. */
952 if (*head
>= vq
->vring
.num
) {
953 virtio_error(vq
->vdev
, "Guest says index %u is available", *head
);
961 VIRTQUEUE_READ_DESC_ERROR
= -1,
962 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
963 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
966 static int virtqueue_split_read_next_desc(VirtIODevice
*vdev
, VRingDesc
*desc
,
967 MemoryRegionCache
*desc_cache
,
968 unsigned int max
, unsigned int *next
)
970 /* If this descriptor says it doesn't chain, we're done. */
971 if (!(desc
->flags
& VRING_DESC_F_NEXT
)) {
972 return VIRTQUEUE_READ_DESC_DONE
;
975 /* Check they're not leading us off end of descriptors. */
977 /* Make sure compiler knows to grab that: we don't want it changing! */
981 virtio_error(vdev
, "Desc next is %u", *next
);
982 return VIRTQUEUE_READ_DESC_ERROR
;
985 vring_split_desc_read(vdev
, desc
, desc_cache
, *next
);
986 return VIRTQUEUE_READ_DESC_MORE
;
989 static void virtqueue_split_get_avail_bytes(VirtQueue
*vq
,
990 unsigned int *in_bytes
, unsigned int *out_bytes
,
991 unsigned max_in_bytes
, unsigned max_out_bytes
)
993 VirtIODevice
*vdev
= vq
->vdev
;
994 unsigned int max
, idx
;
995 unsigned int total_bufs
, in_total
, out_total
;
996 VRingMemoryRegionCaches
*caches
;
997 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1001 RCU_READ_LOCK_GUARD();
1003 idx
= vq
->last_avail_idx
;
1004 total_bufs
= in_total
= out_total
= 0;
1006 max
= vq
->vring
.num
;
1007 caches
= vring_get_region_caches(vq
);
1012 while ((rc
= virtqueue_num_heads(vq
, idx
)) > 0) {
1013 MemoryRegionCache
*desc_cache
= &caches
->desc
;
1014 unsigned int num_bufs
;
1018 num_bufs
= total_bufs
;
1020 if (!virtqueue_get_head(vq
, idx
++, &i
)) {
1024 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1026 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1027 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1028 virtio_error(vdev
, "Invalid size for indirect buffer table");
1032 /* If we've got too many, that implies a descriptor loop. */
1033 if (num_bufs
>= max
) {
1034 virtio_error(vdev
, "Looped descriptor");
1038 /* loop over the indirect descriptor table */
1039 len
= address_space_cache_init(&indirect_desc_cache
,
1041 desc
.addr
, desc
.len
, false);
1042 desc_cache
= &indirect_desc_cache
;
1043 if (len
< desc
.len
) {
1044 virtio_error(vdev
, "Cannot map indirect buffer");
1048 max
= desc
.len
/ sizeof(VRingDesc
);
1050 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1054 /* If we've got too many, that implies a descriptor loop. */
1055 if (++num_bufs
> max
) {
1056 virtio_error(vdev
, "Looped descriptor");
1060 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1061 in_total
+= desc
.len
;
1063 out_total
+= desc
.len
;
1065 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1069 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1070 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1072 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1076 if (desc_cache
== &indirect_desc_cache
) {
1077 address_space_cache_destroy(&indirect_desc_cache
);
1080 total_bufs
= num_bufs
;
1089 address_space_cache_destroy(&indirect_desc_cache
);
1091 *in_bytes
= in_total
;
1094 *out_bytes
= out_total
;
1099 in_total
= out_total
= 0;
1103 static int virtqueue_packed_read_next_desc(VirtQueue
*vq
,
1104 VRingPackedDesc
*desc
,
1111 /* If this descriptor says it doesn't chain, we're done. */
1112 if (!indirect
&& !(desc
->flags
& VRING_DESC_F_NEXT
)) {
1113 return VIRTQUEUE_READ_DESC_DONE
;
1119 return VIRTQUEUE_READ_DESC_DONE
;
1121 (*next
) -= vq
->vring
.num
;
1125 vring_packed_desc_read(vq
->vdev
, desc
, desc_cache
, *next
, false);
1126 return VIRTQUEUE_READ_DESC_MORE
;
1129 static void virtqueue_packed_get_avail_bytes(VirtQueue
*vq
,
1130 unsigned int *in_bytes
,
1131 unsigned int *out_bytes
,
1132 unsigned max_in_bytes
,
1133 unsigned max_out_bytes
)
1135 VirtIODevice
*vdev
= vq
->vdev
;
1136 unsigned int max
, idx
;
1137 unsigned int total_bufs
, in_total
, out_total
;
1138 MemoryRegionCache
*desc_cache
;
1139 VRingMemoryRegionCaches
*caches
;
1140 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1142 VRingPackedDesc desc
;
1145 RCU_READ_LOCK_GUARD();
1146 idx
= vq
->last_avail_idx
;
1147 wrap_counter
= vq
->last_avail_wrap_counter
;
1148 total_bufs
= in_total
= out_total
= 0;
1150 max
= vq
->vring
.num
;
1151 caches
= vring_get_region_caches(vq
);
1157 unsigned int num_bufs
= total_bufs
;
1158 unsigned int i
= idx
;
1161 desc_cache
= &caches
->desc
;
1162 vring_packed_desc_read(vdev
, &desc
, desc_cache
, idx
, true);
1163 if (!is_desc_avail(desc
.flags
, wrap_counter
)) {
1167 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1168 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1169 virtio_error(vdev
, "Invalid size for indirect buffer table");
1173 /* If we've got too many, that implies a descriptor loop. */
1174 if (num_bufs
>= max
) {
1175 virtio_error(vdev
, "Looped descriptor");
1179 /* loop over the indirect descriptor table */
1180 len
= address_space_cache_init(&indirect_desc_cache
,
1182 desc
.addr
, desc
.len
, false);
1183 desc_cache
= &indirect_desc_cache
;
1184 if (len
< desc
.len
) {
1185 virtio_error(vdev
, "Cannot map indirect buffer");
1189 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1191 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1195 /* If we've got too many, that implies a descriptor loop. */
1196 if (++num_bufs
> max
) {
1197 virtio_error(vdev
, "Looped descriptor");
1201 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1202 in_total
+= desc
.len
;
1204 out_total
+= desc
.len
;
1206 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1210 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
,
1212 &indirect_desc_cache
);
1213 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1215 if (desc_cache
== &indirect_desc_cache
) {
1216 address_space_cache_destroy(&indirect_desc_cache
);
1220 idx
+= num_bufs
- total_bufs
;
1221 total_bufs
= num_bufs
;
1224 if (idx
>= vq
->vring
.num
) {
1225 idx
-= vq
->vring
.num
;
1230 /* Record the index and wrap counter for a kick we want */
1231 vq
->shadow_avail_idx
= idx
;
1232 vq
->shadow_avail_wrap_counter
= wrap_counter
;
1234 address_space_cache_destroy(&indirect_desc_cache
);
1236 *in_bytes
= in_total
;
1239 *out_bytes
= out_total
;
1244 in_total
= out_total
= 0;
1248 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
1249 unsigned int *out_bytes
,
1250 unsigned max_in_bytes
, unsigned max_out_bytes
)
1253 VRingMemoryRegionCaches
*caches
;
1255 if (unlikely(!vq
->vring
.desc
)) {
1259 caches
= vring_get_region_caches(vq
);
1264 desc_size
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
1265 sizeof(VRingPackedDesc
) : sizeof(VRingDesc
);
1266 if (caches
->desc
.len
< vq
->vring
.num
* desc_size
) {
1267 virtio_error(vq
->vdev
, "Cannot map descriptor ring");
1271 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1272 virtqueue_packed_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1273 max_in_bytes
, max_out_bytes
);
1275 virtqueue_split_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1276 max_in_bytes
, max_out_bytes
);
1289 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
1290 unsigned int out_bytes
)
1292 unsigned int in_total
, out_total
;
1294 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
1295 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
1298 static bool virtqueue_map_desc(VirtIODevice
*vdev
, unsigned int *p_num_sg
,
1299 hwaddr
*addr
, struct iovec
*iov
,
1300 unsigned int max_num_sg
, bool is_write
,
1301 hwaddr pa
, size_t sz
)
1304 unsigned num_sg
= *p_num_sg
;
1305 assert(num_sg
<= max_num_sg
);
1308 virtio_error(vdev
, "virtio: zero sized buffers are not allowed");
1315 if (num_sg
== max_num_sg
) {
1316 virtio_error(vdev
, "virtio: too many write descriptors in "
1321 iov
[num_sg
].iov_base
= dma_memory_map(vdev
->dma_as
, pa
, &len
,
1323 DMA_DIRECTION_FROM_DEVICE
:
1324 DMA_DIRECTION_TO_DEVICE
);
1325 if (!iov
[num_sg
].iov_base
) {
1326 virtio_error(vdev
, "virtio: bogus descriptor or out of resources");
1330 iov
[num_sg
].iov_len
= len
;
1344 /* Only used by error code paths before we have a VirtQueueElement (therefore
1345 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
1348 static void virtqueue_undo_map_desc(unsigned int out_num
, unsigned int in_num
,
1353 for (i
= 0; i
< out_num
+ in_num
; i
++) {
1354 int is_write
= i
>= out_num
;
1356 cpu_physical_memory_unmap(iov
->iov_base
, iov
->iov_len
, is_write
, 0);
1361 static void virtqueue_map_iovec(VirtIODevice
*vdev
, struct iovec
*sg
,
1362 hwaddr
*addr
, unsigned int num_sg
,
1368 for (i
= 0; i
< num_sg
; i
++) {
1369 len
= sg
[i
].iov_len
;
1370 sg
[i
].iov_base
= dma_memory_map(vdev
->dma_as
,
1371 addr
[i
], &len
, is_write
?
1372 DMA_DIRECTION_FROM_DEVICE
:
1373 DMA_DIRECTION_TO_DEVICE
);
1374 if (!sg
[i
].iov_base
) {
1375 error_report("virtio: error trying to map MMIO memory");
1378 if (len
!= sg
[i
].iov_len
) {
1379 error_report("virtio: unexpected memory split");
1385 void virtqueue_map(VirtIODevice
*vdev
, VirtQueueElement
*elem
)
1387 virtqueue_map_iovec(vdev
, elem
->in_sg
, elem
->in_addr
, elem
->in_num
, true);
1388 virtqueue_map_iovec(vdev
, elem
->out_sg
, elem
->out_addr
, elem
->out_num
,
1392 static void *virtqueue_alloc_element(size_t sz
, unsigned out_num
, unsigned in_num
)
1394 VirtQueueElement
*elem
;
1395 size_t in_addr_ofs
= QEMU_ALIGN_UP(sz
, __alignof__(elem
->in_addr
[0]));
1396 size_t out_addr_ofs
= in_addr_ofs
+ in_num
* sizeof(elem
->in_addr
[0]);
1397 size_t out_addr_end
= out_addr_ofs
+ out_num
* sizeof(elem
->out_addr
[0]);
1398 size_t in_sg_ofs
= QEMU_ALIGN_UP(out_addr_end
, __alignof__(elem
->in_sg
[0]));
1399 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
1400 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
1402 assert(sz
>= sizeof(VirtQueueElement
));
1403 elem
= g_malloc(out_sg_end
);
1404 trace_virtqueue_alloc_element(elem
, sz
, in_num
, out_num
);
1405 elem
->out_num
= out_num
;
1406 elem
->in_num
= in_num
;
1407 elem
->in_addr
= (void *)elem
+ in_addr_ofs
;
1408 elem
->out_addr
= (void *)elem
+ out_addr_ofs
;
1409 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
1410 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
1414 static void *virtqueue_split_pop(VirtQueue
*vq
, size_t sz
)
1416 unsigned int i
, head
, max
;
1417 VRingMemoryRegionCaches
*caches
;
1418 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1419 MemoryRegionCache
*desc_cache
;
1421 VirtIODevice
*vdev
= vq
->vdev
;
1422 VirtQueueElement
*elem
= NULL
;
1423 unsigned out_num
, in_num
, elem_entries
;
1424 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1425 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1429 RCU_READ_LOCK_GUARD();
1430 if (virtio_queue_empty_rcu(vq
)) {
1433 /* Needed after virtio_queue_empty(), see comment in
1434 * virtqueue_num_heads(). */
1437 /* When we start there are none of either input nor output. */
1438 out_num
= in_num
= elem_entries
= 0;
1440 max
= vq
->vring
.num
;
1442 if (vq
->inuse
>= vq
->vring
.num
) {
1443 virtio_error(vdev
, "Virtqueue size exceeded");
1447 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
++, &head
)) {
1451 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
1452 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1457 caches
= vring_get_region_caches(vq
);
1459 virtio_error(vdev
, "Region caches not initialized");
1463 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1464 virtio_error(vdev
, "Cannot map descriptor ring");
1468 desc_cache
= &caches
->desc
;
1469 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1470 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1471 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1472 virtio_error(vdev
, "Invalid size for indirect buffer table");
1476 /* loop over the indirect descriptor table */
1477 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1478 desc
.addr
, desc
.len
, false);
1479 desc_cache
= &indirect_desc_cache
;
1480 if (len
< desc
.len
) {
1481 virtio_error(vdev
, "Cannot map indirect buffer");
1485 max
= desc
.len
/ sizeof(VRingDesc
);
1487 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1490 /* Collect all the descriptors */
1494 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1495 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1497 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1498 desc
.addr
, desc
.len
);
1501 virtio_error(vdev
, "Incorrect order for descriptors");
1504 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1505 VIRTQUEUE_MAX_SIZE
, false,
1506 desc
.addr
, desc
.len
);
1512 /* If we've got too many, that implies a descriptor loop. */
1513 if (++elem_entries
> max
) {
1514 virtio_error(vdev
, "Looped descriptor");
1518 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1519 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1521 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1525 /* Now copy what we have collected and mapped */
1526 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1529 for (i
= 0; i
< out_num
; i
++) {
1530 elem
->out_addr
[i
] = addr
[i
];
1531 elem
->out_sg
[i
] = iov
[i
];
1533 for (i
= 0; i
< in_num
; i
++) {
1534 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1535 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1540 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1542 address_space_cache_destroy(&indirect_desc_cache
);
1547 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1551 static void *virtqueue_packed_pop(VirtQueue
*vq
, size_t sz
)
1553 unsigned int i
, max
;
1554 VRingMemoryRegionCaches
*caches
;
1555 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1556 MemoryRegionCache
*desc_cache
;
1558 VirtIODevice
*vdev
= vq
->vdev
;
1559 VirtQueueElement
*elem
= NULL
;
1560 unsigned out_num
, in_num
, elem_entries
;
1561 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1562 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1563 VRingPackedDesc desc
;
1567 RCU_READ_LOCK_GUARD();
1568 if (virtio_queue_packed_empty_rcu(vq
)) {
1572 /* When we start there are none of either input nor output. */
1573 out_num
= in_num
= elem_entries
= 0;
1575 max
= vq
->vring
.num
;
1577 if (vq
->inuse
>= vq
->vring
.num
) {
1578 virtio_error(vdev
, "Virtqueue size exceeded");
1582 i
= vq
->last_avail_idx
;
1584 caches
= vring_get_region_caches(vq
);
1586 virtio_error(vdev
, "Region caches not initialized");
1590 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1591 virtio_error(vdev
, "Cannot map descriptor ring");
1595 desc_cache
= &caches
->desc
;
1596 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, true);
1598 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1599 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1600 virtio_error(vdev
, "Invalid size for indirect buffer table");
1604 /* loop over the indirect descriptor table */
1605 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1606 desc
.addr
, desc
.len
, false);
1607 desc_cache
= &indirect_desc_cache
;
1608 if (len
< desc
.len
) {
1609 virtio_error(vdev
, "Cannot map indirect buffer");
1613 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1615 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1618 /* Collect all the descriptors */
1622 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1623 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1625 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1626 desc
.addr
, desc
.len
);
1629 virtio_error(vdev
, "Incorrect order for descriptors");
1632 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1633 VIRTQUEUE_MAX_SIZE
, false,
1634 desc
.addr
, desc
.len
);
1640 /* If we've got too many, that implies a descriptor loop. */
1641 if (++elem_entries
> max
) {
1642 virtio_error(vdev
, "Looped descriptor");
1646 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
, &i
,
1648 &indirect_desc_cache
);
1649 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1651 /* Now copy what we have collected and mapped */
1652 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1653 for (i
= 0; i
< out_num
; i
++) {
1654 elem
->out_addr
[i
] = addr
[i
];
1655 elem
->out_sg
[i
] = iov
[i
];
1657 for (i
= 0; i
< in_num
; i
++) {
1658 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1659 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1663 elem
->ndescs
= (desc_cache
== &indirect_desc_cache
) ? 1 : elem_entries
;
1664 vq
->last_avail_idx
+= elem
->ndescs
;
1665 vq
->inuse
+= elem
->ndescs
;
1667 if (vq
->last_avail_idx
>= vq
->vring
.num
) {
1668 vq
->last_avail_idx
-= vq
->vring
.num
;
1669 vq
->last_avail_wrap_counter
^= 1;
1672 vq
->shadow_avail_idx
= vq
->last_avail_idx
;
1673 vq
->shadow_avail_wrap_counter
= vq
->last_avail_wrap_counter
;
1675 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1677 address_space_cache_destroy(&indirect_desc_cache
);
1682 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1686 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
)
1688 if (virtio_device_disabled(vq
->vdev
)) {
1692 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1693 return virtqueue_packed_pop(vq
, sz
);
1695 return virtqueue_split_pop(vq
, sz
);
1699 static unsigned int virtqueue_packed_drop_all(VirtQueue
*vq
)
1701 VRingMemoryRegionCaches
*caches
;
1702 MemoryRegionCache
*desc_cache
;
1703 unsigned int dropped
= 0;
1704 VirtQueueElement elem
= {};
1705 VirtIODevice
*vdev
= vq
->vdev
;
1706 VRingPackedDesc desc
;
1708 caches
= vring_get_region_caches(vq
);
1713 desc_cache
= &caches
->desc
;
1715 virtio_queue_set_notification(vq
, 0);
1717 while (vq
->inuse
< vq
->vring
.num
) {
1718 unsigned int idx
= vq
->last_avail_idx
;
1720 * works similar to virtqueue_pop but does not map buffers
1721 * and does not allocate any memory.
1723 vring_packed_desc_read(vdev
, &desc
, desc_cache
,
1724 vq
->last_avail_idx
, true);
1725 if (!is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
)) {
1728 elem
.index
= desc
.id
;
1730 while (virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
,
1731 vq
->vring
.num
, &idx
, false)) {
1735 * immediately push the element, nothing to unmap
1736 * as both in_num and out_num are set to 0.
1738 virtqueue_push(vq
, &elem
, 0);
1740 vq
->last_avail_idx
+= elem
.ndescs
;
1741 if (vq
->last_avail_idx
>= vq
->vring
.num
) {
1742 vq
->last_avail_idx
-= vq
->vring
.num
;
1743 vq
->last_avail_wrap_counter
^= 1;
1750 static unsigned int virtqueue_split_drop_all(VirtQueue
*vq
)
1752 unsigned int dropped
= 0;
1753 VirtQueueElement elem
= {};
1754 VirtIODevice
*vdev
= vq
->vdev
;
1755 bool fEventIdx
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
);
1757 while (!virtio_queue_empty(vq
) && vq
->inuse
< vq
->vring
.num
) {
1758 /* works similar to virtqueue_pop but does not map buffers
1759 * and does not allocate any memory */
1761 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
, &elem
.index
)) {
1765 vq
->last_avail_idx
++;
1767 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1769 /* immediately push the element, nothing to unmap
1770 * as both in_num and out_num are set to 0 */
1771 virtqueue_push(vq
, &elem
, 0);
1778 /* virtqueue_drop_all:
1779 * @vq: The #VirtQueue
1780 * Drops all queued buffers and indicates them to the guest
1781 * as if they are done. Useful when buffers can not be
1782 * processed but must be returned to the guest.
1784 unsigned int virtqueue_drop_all(VirtQueue
*vq
)
1786 struct VirtIODevice
*vdev
= vq
->vdev
;
1788 if (virtio_device_disabled(vq
->vdev
)) {
1792 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1793 return virtqueue_packed_drop_all(vq
);
1795 return virtqueue_split_drop_all(vq
);
1799 /* Reading and writing a structure directly to QEMUFile is *awful*, but
1800 * it is what QEMU has always done by mistake. We can change it sooner
1801 * or later by bumping the version number of the affected vm states.
1802 * In the meanwhile, since the in-memory layout of VirtQueueElement
1803 * has changed, we need to marshal to and from the layout that was
1804 * used before the change.
1806 typedef struct VirtQueueElementOld
{
1808 unsigned int out_num
;
1809 unsigned int in_num
;
1810 hwaddr in_addr
[VIRTQUEUE_MAX_SIZE
];
1811 hwaddr out_addr
[VIRTQUEUE_MAX_SIZE
];
1812 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
1813 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
1814 } VirtQueueElementOld
;
1816 void *qemu_get_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
, size_t sz
)
1818 VirtQueueElement
*elem
;
1819 VirtQueueElementOld data
;
1822 qemu_get_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1824 /* TODO: teach all callers that this can fail, and return failure instead
1825 * of asserting here.
1826 * This is just one thing (there are probably more) that must be
1827 * fixed before we can allow NDEBUG compilation.
1829 assert(ARRAY_SIZE(data
.in_addr
) >= data
.in_num
);
1830 assert(ARRAY_SIZE(data
.out_addr
) >= data
.out_num
);
1832 elem
= virtqueue_alloc_element(sz
, data
.out_num
, data
.in_num
);
1833 elem
->index
= data
.index
;
1835 for (i
= 0; i
< elem
->in_num
; i
++) {
1836 elem
->in_addr
[i
] = data
.in_addr
[i
];
1839 for (i
= 0; i
< elem
->out_num
; i
++) {
1840 elem
->out_addr
[i
] = data
.out_addr
[i
];
1843 for (i
= 0; i
< elem
->in_num
; i
++) {
1844 /* Base is overwritten by virtqueue_map. */
1845 elem
->in_sg
[i
].iov_base
= 0;
1846 elem
->in_sg
[i
].iov_len
= data
.in_sg
[i
].iov_len
;
1849 for (i
= 0; i
< elem
->out_num
; i
++) {
1850 /* Base is overwritten by virtqueue_map. */
1851 elem
->out_sg
[i
].iov_base
= 0;
1852 elem
->out_sg
[i
].iov_len
= data
.out_sg
[i
].iov_len
;
1855 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1856 qemu_get_be32s(f
, &elem
->ndescs
);
1859 virtqueue_map(vdev
, elem
);
1863 void qemu_put_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
,
1864 VirtQueueElement
*elem
)
1866 VirtQueueElementOld data
;
1869 memset(&data
, 0, sizeof(data
));
1870 data
.index
= elem
->index
;
1871 data
.in_num
= elem
->in_num
;
1872 data
.out_num
= elem
->out_num
;
1874 for (i
= 0; i
< elem
->in_num
; i
++) {
1875 data
.in_addr
[i
] = elem
->in_addr
[i
];
1878 for (i
= 0; i
< elem
->out_num
; i
++) {
1879 data
.out_addr
[i
] = elem
->out_addr
[i
];
1882 for (i
= 0; i
< elem
->in_num
; i
++) {
1883 /* Base is overwritten by virtqueue_map when loading. Do not
1884 * save it, as it would leak the QEMU address space layout. */
1885 data
.in_sg
[i
].iov_len
= elem
->in_sg
[i
].iov_len
;
1888 for (i
= 0; i
< elem
->out_num
; i
++) {
1889 /* Do not save iov_base as above. */
1890 data
.out_sg
[i
].iov_len
= elem
->out_sg
[i
].iov_len
;
1893 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1894 qemu_put_be32s(f
, &elem
->ndescs
);
1897 qemu_put_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1901 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
1903 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1904 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1906 if (virtio_device_disabled(vdev
)) {
1911 k
->notify(qbus
->parent
, vector
);
1915 void virtio_update_irq(VirtIODevice
*vdev
)
1917 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
1920 static int virtio_validate_features(VirtIODevice
*vdev
)
1922 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1924 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
) &&
1925 !virtio_vdev_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1929 if (k
->validate_features
) {
1930 return k
->validate_features(vdev
);
1936 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
1938 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1939 trace_virtio_set_status(vdev
, val
);
1941 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1942 if (!(vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) &&
1943 val
& VIRTIO_CONFIG_S_FEATURES_OK
) {
1944 int ret
= virtio_validate_features(vdev
);
1952 if ((vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) !=
1953 (val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1954 virtio_set_started(vdev
, val
& VIRTIO_CONFIG_S_DRIVER_OK
);
1957 if (k
->set_status
) {
1958 k
->set_status(vdev
, val
);
1965 static enum virtio_device_endian
virtio_default_endian(void)
1967 if (target_words_bigendian()) {
1968 return VIRTIO_DEVICE_ENDIAN_BIG
;
1970 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
1974 static enum virtio_device_endian
virtio_current_cpu_endian(void)
1976 CPUClass
*cc
= CPU_GET_CLASS(current_cpu
);
1978 if (cc
->virtio_is_big_endian(current_cpu
)) {
1979 return VIRTIO_DEVICE_ENDIAN_BIG
;
1981 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
1985 void virtio_reset(void *opaque
)
1987 VirtIODevice
*vdev
= opaque
;
1988 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1991 virtio_set_status(vdev
, 0);
1993 /* Guest initiated reset */
1994 vdev
->device_endian
= virtio_current_cpu_endian();
1997 vdev
->device_endian
= virtio_default_endian();
2004 vdev
->start_on_kick
= false;
2005 vdev
->started
= false;
2006 vdev
->broken
= false;
2007 vdev
->guest_features
= 0;
2008 vdev
->queue_sel
= 0;
2010 vdev
->disabled
= false;
2011 qatomic_set(&vdev
->isr
, 0);
2012 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
2013 virtio_notify_vector(vdev
, vdev
->config_vector
);
2015 for(i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2016 vdev
->vq
[i
].vring
.desc
= 0;
2017 vdev
->vq
[i
].vring
.avail
= 0;
2018 vdev
->vq
[i
].vring
.used
= 0;
2019 vdev
->vq
[i
].last_avail_idx
= 0;
2020 vdev
->vq
[i
].shadow_avail_idx
= 0;
2021 vdev
->vq
[i
].used_idx
= 0;
2022 vdev
->vq
[i
].last_avail_wrap_counter
= true;
2023 vdev
->vq
[i
].shadow_avail_wrap_counter
= true;
2024 vdev
->vq
[i
].used_wrap_counter
= true;
2025 virtio_queue_set_vector(vdev
, i
, VIRTIO_NO_VECTOR
);
2026 vdev
->vq
[i
].signalled_used
= 0;
2027 vdev
->vq
[i
].signalled_used_valid
= false;
2028 vdev
->vq
[i
].notification
= true;
2029 vdev
->vq
[i
].vring
.num
= vdev
->vq
[i
].vring
.num_default
;
2030 vdev
->vq
[i
].inuse
= 0;
2031 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
2035 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
2037 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2040 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2041 return (uint32_t)-1;
2044 k
->get_config(vdev
, vdev
->config
);
2046 val
= ldub_p(vdev
->config
+ addr
);
2050 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
2052 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2055 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2056 return (uint32_t)-1;
2059 k
->get_config(vdev
, vdev
->config
);
2061 val
= lduw_p(vdev
->config
+ addr
);
2065 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
2067 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2070 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2071 return (uint32_t)-1;
2074 k
->get_config(vdev
, vdev
->config
);
2076 val
= ldl_p(vdev
->config
+ addr
);
2080 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2082 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2085 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2089 stb_p(vdev
->config
+ addr
, val
);
2091 if (k
->set_config
) {
2092 k
->set_config(vdev
, vdev
->config
);
2096 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2098 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2099 uint16_t val
= data
;
2101 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2105 stw_p(vdev
->config
+ addr
, val
);
2107 if (k
->set_config
) {
2108 k
->set_config(vdev
, vdev
->config
);
2112 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2114 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2115 uint32_t val
= data
;
2117 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2121 stl_p(vdev
->config
+ addr
, val
);
2123 if (k
->set_config
) {
2124 k
->set_config(vdev
, vdev
->config
);
2128 uint32_t virtio_config_modern_readb(VirtIODevice
*vdev
, uint32_t addr
)
2130 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2133 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2134 return (uint32_t)-1;
2137 k
->get_config(vdev
, vdev
->config
);
2139 val
= ldub_p(vdev
->config
+ addr
);
2143 uint32_t virtio_config_modern_readw(VirtIODevice
*vdev
, uint32_t addr
)
2145 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2148 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2149 return (uint32_t)-1;
2152 k
->get_config(vdev
, vdev
->config
);
2154 val
= lduw_le_p(vdev
->config
+ addr
);
2158 uint32_t virtio_config_modern_readl(VirtIODevice
*vdev
, uint32_t addr
)
2160 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2163 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2164 return (uint32_t)-1;
2167 k
->get_config(vdev
, vdev
->config
);
2169 val
= ldl_le_p(vdev
->config
+ addr
);
2173 void virtio_config_modern_writeb(VirtIODevice
*vdev
,
2174 uint32_t addr
, uint32_t data
)
2176 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2179 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2183 stb_p(vdev
->config
+ addr
, val
);
2185 if (k
->set_config
) {
2186 k
->set_config(vdev
, vdev
->config
);
2190 void virtio_config_modern_writew(VirtIODevice
*vdev
,
2191 uint32_t addr
, uint32_t data
)
2193 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2194 uint16_t val
= data
;
2196 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2200 stw_le_p(vdev
->config
+ addr
, val
);
2202 if (k
->set_config
) {
2203 k
->set_config(vdev
, vdev
->config
);
2207 void virtio_config_modern_writel(VirtIODevice
*vdev
,
2208 uint32_t addr
, uint32_t data
)
2210 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2211 uint32_t val
= data
;
2213 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2217 stl_le_p(vdev
->config
+ addr
, val
);
2219 if (k
->set_config
) {
2220 k
->set_config(vdev
, vdev
->config
);
2224 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
2226 if (!vdev
->vq
[n
].vring
.num
) {
2229 vdev
->vq
[n
].vring
.desc
= addr
;
2230 virtio_queue_update_rings(vdev
, n
);
2233 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
2235 return vdev
->vq
[n
].vring
.desc
;
2238 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
2239 hwaddr avail
, hwaddr used
)
2241 if (!vdev
->vq
[n
].vring
.num
) {
2244 vdev
->vq
[n
].vring
.desc
= desc
;
2245 vdev
->vq
[n
].vring
.avail
= avail
;
2246 vdev
->vq
[n
].vring
.used
= used
;
2247 virtio_init_region_cache(vdev
, n
);
2250 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
2252 /* Don't allow guest to flip queue between existent and
2253 * nonexistent states, or to set it to an invalid size.
2255 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
2256 num
> VIRTQUEUE_MAX_SIZE
||
2260 vdev
->vq
[n
].vring
.num
= num
;
2263 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
)
2265 return QLIST_FIRST(&vdev
->vector_queues
[vector
]);
2268 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
)
2270 return QLIST_NEXT(vq
, node
);
2273 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
2275 return vdev
->vq
[n
].vring
.num
;
2278 int virtio_queue_get_max_num(VirtIODevice
*vdev
, int n
)
2280 return vdev
->vq
[n
].vring
.num_default
;
2283 int virtio_get_num_queues(VirtIODevice
*vdev
)
2287 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2288 if (!virtio_queue_get_num(vdev
, i
)) {
2296 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
2298 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2299 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2301 /* virtio-1 compliant devices cannot change the alignment */
2302 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2303 error_report("tried to modify queue alignment for virtio-1 device");
2306 /* Check that the transport told us it was going to do this
2307 * (so a buggy transport will immediately assert rather than
2308 * silently failing to migrate this state)
2310 assert(k
->has_variable_vring_alignment
);
2313 vdev
->vq
[n
].vring
.align
= align
;
2314 virtio_queue_update_rings(vdev
, n
);
2318 static bool virtio_queue_notify_aio_vq(VirtQueue
*vq
)
2322 if (vq
->vring
.desc
&& vq
->handle_aio_output
) {
2323 VirtIODevice
*vdev
= vq
->vdev
;
2325 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2326 ret
= vq
->handle_aio_output(vdev
, vq
);
2328 if (unlikely(vdev
->start_on_kick
)) {
2329 virtio_set_started(vdev
, true);
2336 static void virtio_queue_notify_vq(VirtQueue
*vq
)
2338 if (vq
->vring
.desc
&& vq
->handle_output
) {
2339 VirtIODevice
*vdev
= vq
->vdev
;
2341 if (unlikely(vdev
->broken
)) {
2345 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2346 vq
->handle_output(vdev
, vq
);
2348 if (unlikely(vdev
->start_on_kick
)) {
2349 virtio_set_started(vdev
, true);
2354 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
2356 VirtQueue
*vq
= &vdev
->vq
[n
];
2358 if (unlikely(!vq
->vring
.desc
|| vdev
->broken
)) {
2362 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2363 if (vq
->host_notifier_enabled
) {
2364 event_notifier_set(&vq
->host_notifier
);
2365 } else if (vq
->handle_output
) {
2366 vq
->handle_output(vdev
, vq
);
2368 if (unlikely(vdev
->start_on_kick
)) {
2369 virtio_set_started(vdev
, true);
2374 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
2376 return n
< VIRTIO_QUEUE_MAX
? vdev
->vq
[n
].vector
:
2380 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
2382 VirtQueue
*vq
= &vdev
->vq
[n
];
2384 if (n
< VIRTIO_QUEUE_MAX
) {
2385 if (vdev
->vector_queues
&&
2386 vdev
->vq
[n
].vector
!= VIRTIO_NO_VECTOR
) {
2387 QLIST_REMOVE(vq
, node
);
2389 vdev
->vq
[n
].vector
= vector
;
2390 if (vdev
->vector_queues
&&
2391 vector
!= VIRTIO_NO_VECTOR
) {
2392 QLIST_INSERT_HEAD(&vdev
->vector_queues
[vector
], vq
, node
);
2397 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
2398 VirtIOHandleOutput handle_output
)
2402 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2403 if (vdev
->vq
[i
].vring
.num
== 0)
2407 if (i
== VIRTIO_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
2410 vdev
->vq
[i
].vring
.num
= queue_size
;
2411 vdev
->vq
[i
].vring
.num_default
= queue_size
;
2412 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
2413 vdev
->vq
[i
].handle_output
= handle_output
;
2414 vdev
->vq
[i
].handle_aio_output
= NULL
;
2415 vdev
->vq
[i
].used_elems
= g_malloc0(sizeof(VirtQueueElement
) *
2418 return &vdev
->vq
[i
];
2421 void virtio_delete_queue(VirtQueue
*vq
)
2424 vq
->vring
.num_default
= 0;
2425 vq
->handle_output
= NULL
;
2426 vq
->handle_aio_output
= NULL
;
2427 g_free(vq
->used_elems
);
2428 vq
->used_elems
= NULL
;
2429 virtio_virtqueue_reset_region_cache(vq
);
2432 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
2434 if (n
< 0 || n
>= VIRTIO_QUEUE_MAX
) {
2438 virtio_delete_queue(&vdev
->vq
[n
]);
2441 static void virtio_set_isr(VirtIODevice
*vdev
, int value
)
2443 uint8_t old
= qatomic_read(&vdev
->isr
);
2445 /* Do not write ISR if it does not change, so that its cacheline remains
2446 * shared in the common case where the guest does not read it.
2448 if ((old
& value
) != value
) {
2449 qatomic_or(&vdev
->isr
, value
);
2453 static bool virtio_split_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2457 /* We need to expose used array entries before checking used event. */
2459 /* Always notify when queue is empty (when feature acknowledge) */
2460 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
2461 !vq
->inuse
&& virtio_queue_empty(vq
)) {
2465 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2466 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
2469 v
= vq
->signalled_used_valid
;
2470 vq
->signalled_used_valid
= true;
2471 old
= vq
->signalled_used
;
2472 new = vq
->signalled_used
= vq
->used_idx
;
2473 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
2476 static bool vring_packed_need_event(VirtQueue
*vq
, bool wrap
,
2477 uint16_t off_wrap
, uint16_t new,
2480 int off
= off_wrap
& ~(1 << 15);
2482 if (wrap
!= off_wrap
>> 15) {
2483 off
-= vq
->vring
.num
;
2486 return vring_need_event(off
, new, old
);
2489 static bool virtio_packed_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2491 VRingPackedDescEvent e
;
2494 VRingMemoryRegionCaches
*caches
;
2496 caches
= vring_get_region_caches(vq
);
2501 vring_packed_event_read(vdev
, &caches
->avail
, &e
);
2503 old
= vq
->signalled_used
;
2504 new = vq
->signalled_used
= vq
->used_idx
;
2505 v
= vq
->signalled_used_valid
;
2506 vq
->signalled_used_valid
= true;
2508 if (e
.flags
== VRING_PACKED_EVENT_FLAG_DISABLE
) {
2510 } else if (e
.flags
== VRING_PACKED_EVENT_FLAG_ENABLE
) {
2514 return !v
|| vring_packed_need_event(vq
, vq
->used_wrap_counter
,
2515 e
.off_wrap
, new, old
);
2518 /* Called within rcu_read_lock(). */
2519 static bool virtio_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2521 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
2522 return virtio_packed_should_notify(vdev
, vq
);
2524 return virtio_split_should_notify(vdev
, vq
);
2528 void virtio_notify_irqfd(VirtIODevice
*vdev
, VirtQueue
*vq
)
2530 WITH_RCU_READ_LOCK_GUARD() {
2531 if (!virtio_should_notify(vdev
, vq
)) {
2536 trace_virtio_notify_irqfd(vdev
, vq
);
2539 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
2540 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
2541 * incorrectly polling this bit during crashdump and hibernation
2542 * in MSI mode, causing a hang if this bit is never updated.
2543 * Recent releases of Windows do not really shut down, but rather
2544 * log out and hibernate to make the next startup faster. Hence,
2545 * this manifested as a more serious hang during shutdown with
2547 * Next driver release from 2016 fixed this problem, so working around it
2548 * is not a must, but it's easy to do so let's do it here.
2550 * Note: it's safe to update ISR from any thread as it was switched
2551 * to an atomic operation.
2553 virtio_set_isr(vq
->vdev
, 0x1);
2554 event_notifier_set(&vq
->guest_notifier
);
2557 static void virtio_irq(VirtQueue
*vq
)
2559 virtio_set_isr(vq
->vdev
, 0x1);
2560 virtio_notify_vector(vq
->vdev
, vq
->vector
);
2563 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2565 WITH_RCU_READ_LOCK_GUARD() {
2566 if (!virtio_should_notify(vdev
, vq
)) {
2571 trace_virtio_notify(vdev
, vq
);
2575 void virtio_notify_config(VirtIODevice
*vdev
)
2577 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
2580 virtio_set_isr(vdev
, 0x3);
2582 virtio_notify_vector(vdev
, vdev
->config_vector
);
2585 static bool virtio_device_endian_needed(void *opaque
)
2587 VirtIODevice
*vdev
= opaque
;
2589 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
2590 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2591 return vdev
->device_endian
!= virtio_default_endian();
2593 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
2594 return vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_LITTLE
;
2597 static bool virtio_64bit_features_needed(void *opaque
)
2599 VirtIODevice
*vdev
= opaque
;
2601 return (vdev
->host_features
>> 32) != 0;
2604 static bool virtio_virtqueue_needed(void *opaque
)
2606 VirtIODevice
*vdev
= opaque
;
2608 return virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
);
2611 static bool virtio_packed_virtqueue_needed(void *opaque
)
2613 VirtIODevice
*vdev
= opaque
;
2615 return virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
);
2618 static bool virtio_ringsize_needed(void *opaque
)
2620 VirtIODevice
*vdev
= opaque
;
2623 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2624 if (vdev
->vq
[i
].vring
.num
!= vdev
->vq
[i
].vring
.num_default
) {
2631 static bool virtio_extra_state_needed(void *opaque
)
2633 VirtIODevice
*vdev
= opaque
;
2634 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2635 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2637 return k
->has_extra_state
&&
2638 k
->has_extra_state(qbus
->parent
);
2641 static bool virtio_broken_needed(void *opaque
)
2643 VirtIODevice
*vdev
= opaque
;
2645 return vdev
->broken
;
2648 static bool virtio_started_needed(void *opaque
)
2650 VirtIODevice
*vdev
= opaque
;
2652 return vdev
->started
;
2655 static bool virtio_disabled_needed(void *opaque
)
2657 VirtIODevice
*vdev
= opaque
;
2659 return vdev
->disabled
;
2662 static const VMStateDescription vmstate_virtqueue
= {
2663 .name
= "virtqueue_state",
2665 .minimum_version_id
= 1,
2666 .fields
= (VMStateField
[]) {
2667 VMSTATE_UINT64(vring
.avail
, struct VirtQueue
),
2668 VMSTATE_UINT64(vring
.used
, struct VirtQueue
),
2669 VMSTATE_END_OF_LIST()
2673 static const VMStateDescription vmstate_packed_virtqueue
= {
2674 .name
= "packed_virtqueue_state",
2676 .minimum_version_id
= 1,
2677 .fields
= (VMStateField
[]) {
2678 VMSTATE_UINT16(last_avail_idx
, struct VirtQueue
),
2679 VMSTATE_BOOL(last_avail_wrap_counter
, struct VirtQueue
),
2680 VMSTATE_UINT16(used_idx
, struct VirtQueue
),
2681 VMSTATE_BOOL(used_wrap_counter
, struct VirtQueue
),
2682 VMSTATE_UINT32(inuse
, struct VirtQueue
),
2683 VMSTATE_END_OF_LIST()
2687 static const VMStateDescription vmstate_virtio_virtqueues
= {
2688 .name
= "virtio/virtqueues",
2690 .minimum_version_id
= 1,
2691 .needed
= &virtio_virtqueue_needed
,
2692 .fields
= (VMStateField
[]) {
2693 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2694 VIRTIO_QUEUE_MAX
, 0, vmstate_virtqueue
, VirtQueue
),
2695 VMSTATE_END_OF_LIST()
2699 static const VMStateDescription vmstate_virtio_packed_virtqueues
= {
2700 .name
= "virtio/packed_virtqueues",
2702 .minimum_version_id
= 1,
2703 .needed
= &virtio_packed_virtqueue_needed
,
2704 .fields
= (VMStateField
[]) {
2705 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2706 VIRTIO_QUEUE_MAX
, 0, vmstate_packed_virtqueue
, VirtQueue
),
2707 VMSTATE_END_OF_LIST()
2711 static const VMStateDescription vmstate_ringsize
= {
2712 .name
= "ringsize_state",
2714 .minimum_version_id
= 1,
2715 .fields
= (VMStateField
[]) {
2716 VMSTATE_UINT32(vring
.num_default
, struct VirtQueue
),
2717 VMSTATE_END_OF_LIST()
2721 static const VMStateDescription vmstate_virtio_ringsize
= {
2722 .name
= "virtio/ringsize",
2724 .minimum_version_id
= 1,
2725 .needed
= &virtio_ringsize_needed
,
2726 .fields
= (VMStateField
[]) {
2727 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2728 VIRTIO_QUEUE_MAX
, 0, vmstate_ringsize
, VirtQueue
),
2729 VMSTATE_END_OF_LIST()
2733 static int get_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2734 const VMStateField
*field
)
2736 VirtIODevice
*vdev
= pv
;
2737 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2738 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2740 if (!k
->load_extra_state
) {
2743 return k
->load_extra_state(qbus
->parent
, f
);
2747 static int put_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2748 const VMStateField
*field
, JSONWriter
*vmdesc
)
2750 VirtIODevice
*vdev
= pv
;
2751 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2752 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2754 k
->save_extra_state(qbus
->parent
, f
);
2758 static const VMStateInfo vmstate_info_extra_state
= {
2759 .name
= "virtqueue_extra_state",
2760 .get
= get_extra_state
,
2761 .put
= put_extra_state
,
2764 static const VMStateDescription vmstate_virtio_extra_state
= {
2765 .name
= "virtio/extra_state",
2767 .minimum_version_id
= 1,
2768 .needed
= &virtio_extra_state_needed
,
2769 .fields
= (VMStateField
[]) {
2771 .name
= "extra_state",
2773 .field_exists
= NULL
,
2775 .info
= &vmstate_info_extra_state
,
2776 .flags
= VMS_SINGLE
,
2779 VMSTATE_END_OF_LIST()
2783 static const VMStateDescription vmstate_virtio_device_endian
= {
2784 .name
= "virtio/device_endian",
2786 .minimum_version_id
= 1,
2787 .needed
= &virtio_device_endian_needed
,
2788 .fields
= (VMStateField
[]) {
2789 VMSTATE_UINT8(device_endian
, VirtIODevice
),
2790 VMSTATE_END_OF_LIST()
2794 static const VMStateDescription vmstate_virtio_64bit_features
= {
2795 .name
= "virtio/64bit_features",
2797 .minimum_version_id
= 1,
2798 .needed
= &virtio_64bit_features_needed
,
2799 .fields
= (VMStateField
[]) {
2800 VMSTATE_UINT64(guest_features
, VirtIODevice
),
2801 VMSTATE_END_OF_LIST()
2805 static const VMStateDescription vmstate_virtio_broken
= {
2806 .name
= "virtio/broken",
2808 .minimum_version_id
= 1,
2809 .needed
= &virtio_broken_needed
,
2810 .fields
= (VMStateField
[]) {
2811 VMSTATE_BOOL(broken
, VirtIODevice
),
2812 VMSTATE_END_OF_LIST()
2816 static const VMStateDescription vmstate_virtio_started
= {
2817 .name
= "virtio/started",
2819 .minimum_version_id
= 1,
2820 .needed
= &virtio_started_needed
,
2821 .fields
= (VMStateField
[]) {
2822 VMSTATE_BOOL(started
, VirtIODevice
),
2823 VMSTATE_END_OF_LIST()
2827 static const VMStateDescription vmstate_virtio_disabled
= {
2828 .name
= "virtio/disabled",
2830 .minimum_version_id
= 1,
2831 .needed
= &virtio_disabled_needed
,
2832 .fields
= (VMStateField
[]) {
2833 VMSTATE_BOOL(disabled
, VirtIODevice
),
2834 VMSTATE_END_OF_LIST()
2838 static const VMStateDescription vmstate_virtio
= {
2841 .minimum_version_id
= 1,
2842 .minimum_version_id_old
= 1,
2843 .fields
= (VMStateField
[]) {
2844 VMSTATE_END_OF_LIST()
2846 .subsections
= (const VMStateDescription
*[]) {
2847 &vmstate_virtio_device_endian
,
2848 &vmstate_virtio_64bit_features
,
2849 &vmstate_virtio_virtqueues
,
2850 &vmstate_virtio_ringsize
,
2851 &vmstate_virtio_broken
,
2852 &vmstate_virtio_extra_state
,
2853 &vmstate_virtio_started
,
2854 &vmstate_virtio_packed_virtqueues
,
2855 &vmstate_virtio_disabled
,
2860 int virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
2862 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2863 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2864 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2865 uint32_t guest_features_lo
= (vdev
->guest_features
& 0xffffffff);
2868 if (k
->save_config
) {
2869 k
->save_config(qbus
->parent
, f
);
2872 qemu_put_8s(f
, &vdev
->status
);
2873 qemu_put_8s(f
, &vdev
->isr
);
2874 qemu_put_be16s(f
, &vdev
->queue_sel
);
2875 qemu_put_be32s(f
, &guest_features_lo
);
2876 qemu_put_be32(f
, vdev
->config_len
);
2877 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
2879 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2880 if (vdev
->vq
[i
].vring
.num
== 0)
2884 qemu_put_be32(f
, i
);
2886 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2887 if (vdev
->vq
[i
].vring
.num
== 0)
2890 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
2891 if (k
->has_variable_vring_alignment
) {
2892 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
2895 * Save desc now, the rest of the ring addresses are saved in
2896 * subsections for VIRTIO-1 devices.
2898 qemu_put_be64(f
, vdev
->vq
[i
].vring
.desc
);
2899 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
2900 if (k
->save_queue
) {
2901 k
->save_queue(qbus
->parent
, i
, f
);
2905 if (vdc
->save
!= NULL
) {
2910 int ret
= vmstate_save_state(f
, vdc
->vmsd
, vdev
, NULL
);
2917 return vmstate_save_state(f
, &vmstate_virtio
, vdev
, NULL
);
2920 /* A wrapper for use as a VMState .put function */
2921 static int virtio_device_put(QEMUFile
*f
, void *opaque
, size_t size
,
2922 const VMStateField
*field
, JSONWriter
*vmdesc
)
2924 return virtio_save(VIRTIO_DEVICE(opaque
), f
);
2927 /* A wrapper for use as a VMState .get function */
2928 static int virtio_device_get(QEMUFile
*f
, void *opaque
, size_t size
,
2929 const VMStateField
*field
)
2931 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
2932 DeviceClass
*dc
= DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev
));
2934 return virtio_load(vdev
, f
, dc
->vmsd
->version_id
);
2937 const VMStateInfo virtio_vmstate_info
= {
2939 .get
= virtio_device_get
,
2940 .put
= virtio_device_put
,
2943 static int virtio_set_features_nocheck(VirtIODevice
*vdev
, uint64_t val
)
2945 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2946 bool bad
= (val
& ~(vdev
->host_features
)) != 0;
2948 val
&= vdev
->host_features
;
2949 if (k
->set_features
) {
2950 k
->set_features(vdev
, val
);
2952 vdev
->guest_features
= val
;
2953 return bad
? -1 : 0;
2956 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
)
2960 * The driver must not attempt to set features after feature negotiation
2963 if (vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
2966 ret
= virtio_set_features_nocheck(vdev
, val
);
2967 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2968 /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */
2970 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2971 if (vdev
->vq
[i
].vring
.num
!= 0) {
2972 virtio_init_region_cache(vdev
, i
);
2977 if (!virtio_device_started(vdev
, vdev
->status
) &&
2978 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2979 vdev
->start_on_kick
= true;
2985 size_t virtio_feature_get_config_size(VirtIOFeature
*feature_sizes
,
2986 uint64_t host_features
)
2988 size_t config_size
= 0;
2991 for (i
= 0; feature_sizes
[i
].flags
!= 0; i
++) {
2992 if (host_features
& feature_sizes
[i
].flags
) {
2993 config_size
= MAX(feature_sizes
[i
].end
, config_size
);
3000 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
)
3006 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3007 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3008 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
3011 * We poison the endianness to ensure it does not get used before
3012 * subsections have been loaded.
3014 vdev
->device_endian
= VIRTIO_DEVICE_ENDIAN_UNKNOWN
;
3016 if (k
->load_config
) {
3017 ret
= k
->load_config(qbus
->parent
, f
);
3022 qemu_get_8s(f
, &vdev
->status
);
3023 qemu_get_8s(f
, &vdev
->isr
);
3024 qemu_get_be16s(f
, &vdev
->queue_sel
);
3025 if (vdev
->queue_sel
>= VIRTIO_QUEUE_MAX
) {
3028 qemu_get_be32s(f
, &features
);
3031 * Temporarily set guest_features low bits - needed by
3032 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
3033 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
3035 * Note: devices should always test host features in future - don't create
3036 * new dependencies like this.
3038 vdev
->guest_features
= features
;
3040 config_len
= qemu_get_be32(f
);
3043 * There are cases where the incoming config can be bigger or smaller
3044 * than what we have; so load what we have space for, and skip
3045 * any excess that's in the stream.
3047 qemu_get_buffer(f
, vdev
->config
, MIN(config_len
, vdev
->config_len
));
3049 while (config_len
> vdev
->config_len
) {
3054 num
= qemu_get_be32(f
);
3056 if (num
> VIRTIO_QUEUE_MAX
) {
3057 error_report("Invalid number of virtqueues: 0x%x", num
);
3061 for (i
= 0; i
< num
; i
++) {
3062 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
3063 if (k
->has_variable_vring_alignment
) {
3064 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
3066 vdev
->vq
[i
].vring
.desc
= qemu_get_be64(f
);
3067 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
3068 vdev
->vq
[i
].signalled_used_valid
= false;
3069 vdev
->vq
[i
].notification
= true;
3071 if (!vdev
->vq
[i
].vring
.desc
&& vdev
->vq
[i
].last_avail_idx
) {
3072 error_report("VQ %d address 0x0 "
3073 "inconsistent with Host index 0x%x",
3074 i
, vdev
->vq
[i
].last_avail_idx
);
3077 if (k
->load_queue
) {
3078 ret
= k
->load_queue(qbus
->parent
, i
, f
);
3084 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
3086 if (vdc
->load
!= NULL
) {
3087 ret
= vdc
->load(vdev
, f
, version_id
);
3094 ret
= vmstate_load_state(f
, vdc
->vmsd
, vdev
, version_id
);
3101 ret
= vmstate_load_state(f
, &vmstate_virtio
, vdev
, 1);
3106 if (vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_UNKNOWN
) {
3107 vdev
->device_endian
= virtio_default_endian();
3110 if (virtio_64bit_features_needed(vdev
)) {
3112 * Subsection load filled vdev->guest_features. Run them
3113 * through virtio_set_features to sanity-check them against
3116 uint64_t features64
= vdev
->guest_features
;
3117 if (virtio_set_features_nocheck(vdev
, features64
) < 0) {
3118 error_report("Features 0x%" PRIx64
" unsupported. "
3119 "Allowed features: 0x%" PRIx64
,
3120 features64
, vdev
->host_features
);
3124 if (virtio_set_features_nocheck(vdev
, features
) < 0) {
3125 error_report("Features 0x%x unsupported. "
3126 "Allowed features: 0x%" PRIx64
,
3127 features
, vdev
->host_features
);
3132 if (!virtio_device_started(vdev
, vdev
->status
) &&
3133 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3134 vdev
->start_on_kick
= true;
3137 RCU_READ_LOCK_GUARD();
3138 for (i
= 0; i
< num
; i
++) {
3139 if (vdev
->vq
[i
].vring
.desc
) {
3143 * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
3144 * only the region cache needs to be set up. Legacy devices need
3145 * to calculate used and avail ring addresses based on the desc
3148 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3149 virtio_init_region_cache(vdev
, i
);
3151 virtio_queue_update_rings(vdev
, i
);
3154 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3155 vdev
->vq
[i
].shadow_avail_idx
= vdev
->vq
[i
].last_avail_idx
;
3156 vdev
->vq
[i
].shadow_avail_wrap_counter
=
3157 vdev
->vq
[i
].last_avail_wrap_counter
;
3161 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
3162 /* Check it isn't doing strange things with descriptor numbers. */
3163 if (nheads
> vdev
->vq
[i
].vring
.num
) {
3164 virtio_error(vdev
, "VQ %d size 0x%x Guest index 0x%x "
3165 "inconsistent with Host index 0x%x: delta 0x%x",
3166 i
, vdev
->vq
[i
].vring
.num
,
3167 vring_avail_idx(&vdev
->vq
[i
]),
3168 vdev
->vq
[i
].last_avail_idx
, nheads
);
3169 vdev
->vq
[i
].used_idx
= 0;
3170 vdev
->vq
[i
].shadow_avail_idx
= 0;
3171 vdev
->vq
[i
].inuse
= 0;
3174 vdev
->vq
[i
].used_idx
= vring_used_idx(&vdev
->vq
[i
]);
3175 vdev
->vq
[i
].shadow_avail_idx
= vring_avail_idx(&vdev
->vq
[i
]);
3178 * Some devices migrate VirtQueueElements that have been popped
3179 * from the avail ring but not yet returned to the used ring.
3180 * Since max ring size < UINT16_MAX it's safe to use modulo
3181 * UINT16_MAX + 1 subtraction.
3183 vdev
->vq
[i
].inuse
= (uint16_t)(vdev
->vq
[i
].last_avail_idx
-
3184 vdev
->vq
[i
].used_idx
);
3185 if (vdev
->vq
[i
].inuse
> vdev
->vq
[i
].vring
.num
) {
3186 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
3188 i
, vdev
->vq
[i
].vring
.num
,
3189 vdev
->vq
[i
].last_avail_idx
,
3190 vdev
->vq
[i
].used_idx
);
3196 if (vdc
->post_load
) {
3197 ret
= vdc
->post_load(vdev
);
3206 void virtio_cleanup(VirtIODevice
*vdev
)
3208 qemu_del_vm_change_state_handler(vdev
->vmstate
);
3211 static void virtio_vmstate_change(void *opaque
, int running
, RunState state
)
3213 VirtIODevice
*vdev
= opaque
;
3214 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3215 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3216 bool backend_run
= running
&& virtio_device_started(vdev
, vdev
->status
);
3217 vdev
->vm_running
= running
;
3220 virtio_set_status(vdev
, vdev
->status
);
3223 if (k
->vmstate_change
) {
3224 k
->vmstate_change(qbus
->parent
, backend_run
);
3228 virtio_set_status(vdev
, vdev
->status
);
3232 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
3233 size_t vdev_size
, const char *vdev_name
)
3235 DeviceState
*vdev
= data
;
3237 object_initialize_child_with_props(proxy_obj
, "virtio-backend", vdev
,
3238 vdev_size
, vdev_name
, &error_abort
,
3240 qdev_alias_all_properties(vdev
, proxy_obj
);
3243 void virtio_init(VirtIODevice
*vdev
, const char *name
,
3244 uint16_t device_id
, size_t config_size
)
3246 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3247 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3249 int nvectors
= k
->query_nvectors
? k
->query_nvectors(qbus
->parent
) : 0;
3252 vdev
->vector_queues
=
3253 g_malloc0(sizeof(*vdev
->vector_queues
) * nvectors
);
3256 vdev
->start_on_kick
= false;
3257 vdev
->started
= false;
3258 vdev
->device_id
= device_id
;
3260 qatomic_set(&vdev
->isr
, 0);
3261 vdev
->queue_sel
= 0;
3262 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
3263 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_QUEUE_MAX
);
3264 vdev
->vm_running
= runstate_is_running();
3265 vdev
->broken
= false;
3266 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3267 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
3268 vdev
->vq
[i
].vdev
= vdev
;
3269 vdev
->vq
[i
].queue_index
= i
;
3270 vdev
->vq
[i
].host_notifier_enabled
= false;
3274 vdev
->config_len
= config_size
;
3275 if (vdev
->config_len
) {
3276 vdev
->config
= g_malloc0(config_size
);
3278 vdev
->config
= NULL
;
3280 vdev
->vmstate
= qdev_add_vm_change_state_handler(DEVICE(vdev
),
3281 virtio_vmstate_change
, vdev
);
3282 vdev
->device_endian
= virtio_default_endian();
3283 vdev
->use_guest_notifier_mask
= true;
3287 * Only devices that have already been around prior to defining the virtio
3288 * standard support legacy mode; this includes devices not specified in the
3289 * standard. All newer devices conform to the virtio standard only.
3291 bool virtio_legacy_allowed(VirtIODevice
*vdev
)
3293 switch (vdev
->device_id
) {
3295 case VIRTIO_ID_BLOCK
:
3296 case VIRTIO_ID_CONSOLE
:
3298 case VIRTIO_ID_BALLOON
:
3299 case VIRTIO_ID_RPMSG
:
3300 case VIRTIO_ID_SCSI
:
3302 case VIRTIO_ID_RPROC_SERIAL
:
3303 case VIRTIO_ID_CAIF
:
3310 bool virtio_legacy_check_disabled(VirtIODevice
*vdev
)
3312 return vdev
->disable_legacy_check
;
3315 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
3317 return vdev
->vq
[n
].vring
.desc
;
3320 bool virtio_queue_enabled_legacy(VirtIODevice
*vdev
, int n
)
3322 return virtio_queue_get_desc_addr(vdev
, n
) != 0;
3325 bool virtio_queue_enabled(VirtIODevice
*vdev
, int n
)
3327 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3328 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3330 if (k
->queue_enabled
) {
3331 return k
->queue_enabled(qbus
->parent
, n
);
3333 return virtio_queue_enabled_legacy(vdev
, n
);
3336 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
3338 return vdev
->vq
[n
].vring
.avail
;
3341 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
3343 return vdev
->vq
[n
].vring
.used
;
3346 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
3348 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
3351 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
3355 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3356 return sizeof(struct VRingPackedDescEvent
);
3359 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3360 return offsetof(VRingAvail
, ring
) +
3361 sizeof(uint16_t) * vdev
->vq
[n
].vring
.num
+ s
;
3364 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
3368 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3369 return sizeof(struct VRingPackedDescEvent
);
3372 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3373 return offsetof(VRingUsed
, ring
) +
3374 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
+ s
;
3377 static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice
*vdev
,
3380 unsigned int avail
, used
;
3382 avail
= vdev
->vq
[n
].last_avail_idx
;
3383 avail
|= ((uint16_t)vdev
->vq
[n
].last_avail_wrap_counter
) << 15;
3385 used
= vdev
->vq
[n
].used_idx
;
3386 used
|= ((uint16_t)vdev
->vq
[n
].used_wrap_counter
) << 15;
3388 return avail
| used
<< 16;
3391 static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice
*vdev
,
3394 return vdev
->vq
[n
].last_avail_idx
;
3397 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
3399 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3400 return virtio_queue_packed_get_last_avail_idx(vdev
, n
);
3402 return virtio_queue_split_get_last_avail_idx(vdev
, n
);
3406 static void virtio_queue_packed_set_last_avail_idx(VirtIODevice
*vdev
,
3407 int n
, unsigned int idx
)
3409 struct VirtQueue
*vq
= &vdev
->vq
[n
];
3411 vq
->last_avail_idx
= vq
->shadow_avail_idx
= idx
& 0x7fff;
3412 vq
->last_avail_wrap_counter
=
3413 vq
->shadow_avail_wrap_counter
= !!(idx
& 0x8000);
3415 vq
->used_idx
= idx
& 0x7ffff;
3416 vq
->used_wrap_counter
= !!(idx
& 0x8000);
3419 static void virtio_queue_split_set_last_avail_idx(VirtIODevice
*vdev
,
3420 int n
, unsigned int idx
)
3422 vdev
->vq
[n
].last_avail_idx
= idx
;
3423 vdev
->vq
[n
].shadow_avail_idx
= idx
;
3426 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
,
3429 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3430 virtio_queue_packed_set_last_avail_idx(vdev
, n
, idx
);
3432 virtio_queue_split_set_last_avail_idx(vdev
, n
, idx
);
3436 static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice
*vdev
,
3439 /* We don't have a reference like avail idx in shared memory */
3443 static void virtio_queue_split_restore_last_avail_idx(VirtIODevice
*vdev
,
3446 RCU_READ_LOCK_GUARD();
3447 if (vdev
->vq
[n
].vring
.desc
) {
3448 vdev
->vq
[n
].last_avail_idx
= vring_used_idx(&vdev
->vq
[n
]);
3449 vdev
->vq
[n
].shadow_avail_idx
= vdev
->vq
[n
].last_avail_idx
;
3453 void virtio_queue_restore_last_avail_idx(VirtIODevice
*vdev
, int n
)
3455 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3456 virtio_queue_packed_restore_last_avail_idx(vdev
, n
);
3458 virtio_queue_split_restore_last_avail_idx(vdev
, n
);
3462 static void virtio_queue_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3464 /* used idx was updated through set_last_avail_idx() */
3468 static void virtio_split_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3470 RCU_READ_LOCK_GUARD();
3471 if (vdev
->vq
[n
].vring
.desc
) {
3472 vdev
->vq
[n
].used_idx
= vring_used_idx(&vdev
->vq
[n
]);
3476 void virtio_queue_update_used_idx(VirtIODevice
*vdev
, int n
)
3478 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3479 return virtio_queue_packed_update_used_idx(vdev
, n
);
3481 return virtio_split_packed_update_used_idx(vdev
, n
);
3485 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
3487 vdev
->vq
[n
].signalled_used_valid
= false;
3490 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
3492 return vdev
->vq
+ n
;
3495 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
3497 return vq
->queue_index
;
3500 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
3502 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
3503 if (event_notifier_test_and_clear(n
)) {
3508 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
3511 if (assign
&& !with_irqfd
) {
3512 event_notifier_set_handler(&vq
->guest_notifier
,
3513 virtio_queue_guest_notifier_read
);
3515 event_notifier_set_handler(&vq
->guest_notifier
, NULL
);
3518 /* Test and clear notifier before closing it,
3519 * in case poll callback didn't have time to run. */
3520 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
3524 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
3526 return &vq
->guest_notifier
;
3529 static void virtio_queue_host_notifier_aio_read(EventNotifier
*n
)
3531 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3532 if (event_notifier_test_and_clear(n
)) {
3533 virtio_queue_notify_aio_vq(vq
);
3537 static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier
*n
)
3539 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3541 virtio_queue_set_notification(vq
, 0);
3544 static bool virtio_queue_host_notifier_aio_poll(void *opaque
)
3546 EventNotifier
*n
= opaque
;
3547 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3549 if (!vq
->vring
.desc
|| virtio_queue_empty(vq
)) {
3553 return virtio_queue_notify_aio_vq(vq
);
3556 static void virtio_queue_host_notifier_aio_poll_end(EventNotifier
*n
)
3558 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3560 /* Caller polls once more after this to catch requests that race with us */
3561 virtio_queue_set_notification(vq
, 1);
3564 void virtio_queue_aio_set_host_notifier_handler(VirtQueue
*vq
, AioContext
*ctx
,
3565 VirtIOHandleAIOOutput handle_output
)
3567 if (handle_output
) {
3568 vq
->handle_aio_output
= handle_output
;
3569 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true,
3570 virtio_queue_host_notifier_aio_read
,
3571 virtio_queue_host_notifier_aio_poll
);
3572 aio_set_event_notifier_poll(ctx
, &vq
->host_notifier
,
3573 virtio_queue_host_notifier_aio_poll_begin
,
3574 virtio_queue_host_notifier_aio_poll_end
);
3576 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true, NULL
, NULL
);
3577 /* Test and clear notifier before after disabling event,
3578 * in case poll callback didn't have time to run. */
3579 virtio_queue_host_notifier_aio_read(&vq
->host_notifier
);
3580 vq
->handle_aio_output
= NULL
;
3584 void virtio_queue_host_notifier_read(EventNotifier
*n
)
3586 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3587 if (event_notifier_test_and_clear(n
)) {
3588 virtio_queue_notify_vq(vq
);
3592 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
3594 return &vq
->host_notifier
;
3597 void virtio_queue_set_host_notifier_enabled(VirtQueue
*vq
, bool enabled
)
3599 vq
->host_notifier_enabled
= enabled
;
3602 int virtio_queue_set_host_notifier_mr(VirtIODevice
*vdev
, int n
,
3603 MemoryRegion
*mr
, bool assign
)
3605 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3606 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3608 if (k
->set_host_notifier_mr
) {
3609 return k
->set_host_notifier_mr(qbus
->parent
, n
, mr
, assign
);
3615 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
3617 g_free(vdev
->bus_name
);
3618 vdev
->bus_name
= g_strdup(bus_name
);
3621 void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice
*vdev
, const char *fmt
, ...)
3626 error_vreport(fmt
, ap
);
3629 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3630 vdev
->status
= vdev
->status
| VIRTIO_CONFIG_S_NEEDS_RESET
;
3631 virtio_notify_config(vdev
);
3634 vdev
->broken
= true;
3637 static void virtio_memory_listener_commit(MemoryListener
*listener
)
3639 VirtIODevice
*vdev
= container_of(listener
, VirtIODevice
, listener
);
3642 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3643 if (vdev
->vq
[i
].vring
.num
== 0) {
3646 virtio_init_region_cache(vdev
, i
);
3650 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
3652 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3653 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3656 /* Devices should either use vmsd or the load/save methods */
3657 assert(!vdc
->vmsd
|| !vdc
->load
);
3659 if (vdc
->realize
!= NULL
) {
3660 vdc
->realize(dev
, &err
);
3662 error_propagate(errp
, err
);
3667 virtio_bus_device_plugged(vdev
, &err
);
3669 error_propagate(errp
, err
);
3670 vdc
->unrealize(dev
);
3674 vdev
->listener
.commit
= virtio_memory_listener_commit
;
3675 memory_listener_register(&vdev
->listener
, vdev
->dma_as
);
3678 static void virtio_device_unrealize(DeviceState
*dev
)
3680 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3681 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3683 virtio_bus_device_unplugged(vdev
);
3685 if (vdc
->unrealize
!= NULL
) {
3686 vdc
->unrealize(dev
);
3689 g_free(vdev
->bus_name
);
3690 vdev
->bus_name
= NULL
;
3693 static void virtio_device_free_virtqueues(VirtIODevice
*vdev
)
3700 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3701 if (vdev
->vq
[i
].vring
.num
== 0) {
3704 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
3709 static void virtio_device_instance_finalize(Object
*obj
)
3711 VirtIODevice
*vdev
= VIRTIO_DEVICE(obj
);
3713 memory_listener_unregister(&vdev
->listener
);
3714 virtio_device_free_virtqueues(vdev
);
3716 g_free(vdev
->config
);
3717 g_free(vdev
->vector_queues
);
3720 static Property virtio_properties
[] = {
3721 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice
, host_features
),
3722 DEFINE_PROP_BOOL("use-started", VirtIODevice
, use_started
, true),
3723 DEFINE_PROP_BOOL("use-disabled-flag", VirtIODevice
, use_disabled_flag
, true),
3724 DEFINE_PROP_BOOL("x-disable-legacy-check", VirtIODevice
,
3725 disable_legacy_check
, false),
3726 DEFINE_PROP_END_OF_LIST(),
3729 static int virtio_device_start_ioeventfd_impl(VirtIODevice
*vdev
)
3731 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3734 memory_region_transaction_begin();
3735 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3736 VirtQueue
*vq
= &vdev
->vq
[n
];
3737 if (!virtio_queue_get_num(vdev
, n
)) {
3740 r
= virtio_bus_set_host_notifier(qbus
, n
, true);
3745 event_notifier_set_handler(&vq
->host_notifier
,
3746 virtio_queue_host_notifier_read
);
3749 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3750 /* Kick right away to begin processing requests already in vring */
3751 VirtQueue
*vq
= &vdev
->vq
[n
];
3752 if (!vq
->vring
.num
) {
3755 event_notifier_set(&vq
->host_notifier
);
3757 memory_region_transaction_commit();
3761 i
= n
; /* save n for a second iteration after transaction is committed. */
3763 VirtQueue
*vq
= &vdev
->vq
[n
];
3764 if (!virtio_queue_get_num(vdev
, n
)) {
3768 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3769 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3772 memory_region_transaction_commit();
3775 if (!virtio_queue_get_num(vdev
, i
)) {
3778 virtio_bus_cleanup_host_notifier(qbus
, i
);
3783 int virtio_device_start_ioeventfd(VirtIODevice
*vdev
)
3785 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3786 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3788 return virtio_bus_start_ioeventfd(vbus
);
3791 static void virtio_device_stop_ioeventfd_impl(VirtIODevice
*vdev
)
3793 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3796 memory_region_transaction_begin();
3797 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3798 VirtQueue
*vq
= &vdev
->vq
[n
];
3800 if (!virtio_queue_get_num(vdev
, n
)) {
3803 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3804 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3807 memory_region_transaction_commit();
3809 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3810 if (!virtio_queue_get_num(vdev
, n
)) {
3813 virtio_bus_cleanup_host_notifier(qbus
, n
);
3817 int virtio_device_grab_ioeventfd(VirtIODevice
*vdev
)
3819 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3820 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3822 return virtio_bus_grab_ioeventfd(vbus
);
3825 void virtio_device_release_ioeventfd(VirtIODevice
*vdev
)
3827 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3828 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3830 virtio_bus_release_ioeventfd(vbus
);
3833 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
3835 /* Set the default value here. */
3836 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
3837 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3839 dc
->realize
= virtio_device_realize
;
3840 dc
->unrealize
= virtio_device_unrealize
;
3841 dc
->bus_type
= TYPE_VIRTIO_BUS
;
3842 device_class_set_props(dc
, virtio_properties
);
3843 vdc
->start_ioeventfd
= virtio_device_start_ioeventfd_impl
;
3844 vdc
->stop_ioeventfd
= virtio_device_stop_ioeventfd_impl
;
3846 vdc
->legacy_features
|= VIRTIO_LEGACY_FEATURES
;
3849 bool virtio_device_ioeventfd_enabled(VirtIODevice
*vdev
)
3851 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3852 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3854 return virtio_bus_ioeventfd_enabled(vbus
);
3857 static const TypeInfo virtio_device_info
= {
3858 .name
= TYPE_VIRTIO_DEVICE
,
3859 .parent
= TYPE_DEVICE
,
3860 .instance_size
= sizeof(VirtIODevice
),
3861 .class_init
= virtio_device_class_init
,
3862 .instance_finalize
= virtio_device_instance_finalize
,
3864 .class_size
= sizeof(VirtioDeviceClass
),
3867 static void virtio_register_types(void)
3869 type_register_static(&virtio_device_info
);
3872 type_init(virtio_register_types
)