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"
20 #include "qemu/main-loop.h"
21 #include "qemu/module.h"
22 #include "hw/virtio/virtio.h"
23 #include "migration/qemu-file-types.h"
24 #include "qemu/atomic.h"
25 #include "hw/virtio/virtio-bus.h"
26 #include "hw/qdev-properties.h"
27 #include "hw/virtio/virtio-access.h"
28 #include "sysemu/dma.h"
29 #include "sysemu/runstate.h"
32 * The alignment to use between consumer and producer parts of vring.
33 * x86 pagesize again. This is the default, used by transports like PCI
34 * which don't provide a means for the guest to tell the host the alignment.
36 #define VIRTIO_PCI_VRING_ALIGN 4096
38 typedef struct VRingDesc
46 typedef struct VRingPackedDesc
{
53 typedef struct VRingAvail
60 typedef struct VRingUsedElem
66 typedef struct VRingUsed
73 typedef struct VRingMemoryRegionCaches
{
75 MemoryRegionCache desc
;
76 MemoryRegionCache avail
;
77 MemoryRegionCache used
;
78 } VRingMemoryRegionCaches
;
83 unsigned int num_default
;
88 VRingMemoryRegionCaches
*caches
;
91 typedef struct VRingPackedDescEvent
{
94 } VRingPackedDescEvent
;
99 VirtQueueElement
*used_elems
;
101 /* Next head to pop */
102 uint16_t last_avail_idx
;
103 bool last_avail_wrap_counter
;
105 /* Last avail_idx read from VQ. */
106 uint16_t shadow_avail_idx
;
107 bool shadow_avail_wrap_counter
;
110 bool used_wrap_counter
;
112 /* Last used index value we have signalled on */
113 uint16_t signalled_used
;
115 /* Last used index value we have signalled on */
116 bool signalled_used_valid
;
118 /* Notification enabled? */
121 uint16_t queue_index
;
126 VirtIOHandleOutput handle_output
;
127 VirtIOHandleAIOOutput handle_aio_output
;
129 EventNotifier guest_notifier
;
130 EventNotifier host_notifier
;
131 bool host_notifier_enabled
;
132 QLIST_ENTRY(VirtQueue
) node
;
135 static void virtio_free_region_cache(VRingMemoryRegionCaches
*caches
)
141 address_space_cache_destroy(&caches
->desc
);
142 address_space_cache_destroy(&caches
->avail
);
143 address_space_cache_destroy(&caches
->used
);
147 static void virtio_virtqueue_reset_region_cache(struct VirtQueue
*vq
)
149 VRingMemoryRegionCaches
*caches
;
151 caches
= atomic_read(&vq
->vring
.caches
);
152 atomic_rcu_set(&vq
->vring
.caches
, NULL
);
154 call_rcu(caches
, virtio_free_region_cache
, rcu
);
158 static void virtio_init_region_cache(VirtIODevice
*vdev
, int n
)
160 VirtQueue
*vq
= &vdev
->vq
[n
];
161 VRingMemoryRegionCaches
*old
= vq
->vring
.caches
;
162 VRingMemoryRegionCaches
*new = NULL
;
168 addr
= vq
->vring
.desc
;
172 new = g_new0(VRingMemoryRegionCaches
, 1);
173 size
= virtio_queue_get_desc_size(vdev
, n
);
174 packed
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
176 len
= address_space_cache_init(&new->desc
, vdev
->dma_as
,
179 virtio_error(vdev
, "Cannot map desc");
183 size
= virtio_queue_get_used_size(vdev
, n
);
184 len
= address_space_cache_init(&new->used
, vdev
->dma_as
,
185 vq
->vring
.used
, size
, true);
187 virtio_error(vdev
, "Cannot map used");
191 size
= virtio_queue_get_avail_size(vdev
, n
);
192 len
= address_space_cache_init(&new->avail
, vdev
->dma_as
,
193 vq
->vring
.avail
, size
, false);
195 virtio_error(vdev
, "Cannot map avail");
199 atomic_rcu_set(&vq
->vring
.caches
, new);
201 call_rcu(old
, virtio_free_region_cache
, rcu
);
206 address_space_cache_destroy(&new->avail
);
208 address_space_cache_destroy(&new->used
);
210 address_space_cache_destroy(&new->desc
);
213 virtio_virtqueue_reset_region_cache(vq
);
216 /* virt queue functions */
217 void virtio_queue_update_rings(VirtIODevice
*vdev
, int n
)
219 VRing
*vring
= &vdev
->vq
[n
].vring
;
221 if (!vring
->num
|| !vring
->desc
|| !vring
->align
) {
222 /* not yet setup -> nothing to do */
225 vring
->avail
= vring
->desc
+ vring
->num
* sizeof(VRingDesc
);
226 vring
->used
= vring_align(vring
->avail
+
227 offsetof(VRingAvail
, ring
[vring
->num
]),
229 virtio_init_region_cache(vdev
, n
);
232 /* Called within rcu_read_lock(). */
233 static void vring_split_desc_read(VirtIODevice
*vdev
, VRingDesc
*desc
,
234 MemoryRegionCache
*cache
, int i
)
236 address_space_read_cached(cache
, i
* sizeof(VRingDesc
),
237 desc
, sizeof(VRingDesc
));
238 virtio_tswap64s(vdev
, &desc
->addr
);
239 virtio_tswap32s(vdev
, &desc
->len
);
240 virtio_tswap16s(vdev
, &desc
->flags
);
241 virtio_tswap16s(vdev
, &desc
->next
);
244 static void vring_packed_event_read(VirtIODevice
*vdev
,
245 MemoryRegionCache
*cache
,
246 VRingPackedDescEvent
*e
)
248 hwaddr off_off
= offsetof(VRingPackedDescEvent
, off_wrap
);
249 hwaddr off_flags
= offsetof(VRingPackedDescEvent
, flags
);
251 address_space_read_cached(cache
, off_flags
, &e
->flags
,
253 /* Make sure flags is seen before off_wrap */
255 address_space_read_cached(cache
, off_off
, &e
->off_wrap
,
256 sizeof(e
->off_wrap
));
257 virtio_tswap16s(vdev
, &e
->off_wrap
);
258 virtio_tswap16s(vdev
, &e
->flags
);
261 static void vring_packed_off_wrap_write(VirtIODevice
*vdev
,
262 MemoryRegionCache
*cache
,
265 hwaddr off
= offsetof(VRingPackedDescEvent
, off_wrap
);
267 virtio_tswap16s(vdev
, &off_wrap
);
268 address_space_write_cached(cache
, off
, &off_wrap
, sizeof(off_wrap
));
269 address_space_cache_invalidate(cache
, off
, sizeof(off_wrap
));
272 static void vring_packed_flags_write(VirtIODevice
*vdev
,
273 MemoryRegionCache
*cache
, uint16_t flags
)
275 hwaddr off
= offsetof(VRingPackedDescEvent
, flags
);
277 virtio_tswap16s(vdev
, &flags
);
278 address_space_write_cached(cache
, off
, &flags
, sizeof(flags
));
279 address_space_cache_invalidate(cache
, off
, sizeof(flags
));
282 /* Called within rcu_read_lock(). */
283 static VRingMemoryRegionCaches
*vring_get_region_caches(struct VirtQueue
*vq
)
285 return atomic_rcu_read(&vq
->vring
.caches
);
288 /* Called within rcu_read_lock(). */
289 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
291 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
292 hwaddr pa
= offsetof(VRingAvail
, flags
);
298 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
301 /* Called within rcu_read_lock(). */
302 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
304 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
305 hwaddr pa
= offsetof(VRingAvail
, idx
);
311 vq
->shadow_avail_idx
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
312 return vq
->shadow_avail_idx
;
315 /* Called within rcu_read_lock(). */
316 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
318 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
319 hwaddr pa
= offsetof(VRingAvail
, ring
[i
]);
325 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
328 /* Called within rcu_read_lock(). */
329 static inline uint16_t vring_get_used_event(VirtQueue
*vq
)
331 return vring_avail_ring(vq
, vq
->vring
.num
);
334 /* Called within rcu_read_lock(). */
335 static inline void vring_used_write(VirtQueue
*vq
, VRingUsedElem
*uelem
,
338 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
339 hwaddr pa
= offsetof(VRingUsed
, ring
[i
]);
345 virtio_tswap32s(vq
->vdev
, &uelem
->id
);
346 virtio_tswap32s(vq
->vdev
, &uelem
->len
);
347 address_space_write_cached(&caches
->used
, pa
, uelem
, sizeof(VRingUsedElem
));
348 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(VRingUsedElem
));
351 /* Called within rcu_read_lock(). */
352 static uint16_t vring_used_idx(VirtQueue
*vq
)
354 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
355 hwaddr pa
= offsetof(VRingUsed
, idx
);
361 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
364 /* Called within rcu_read_lock(). */
365 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
367 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
368 hwaddr pa
= offsetof(VRingUsed
, idx
);
371 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
372 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
378 /* Called within rcu_read_lock(). */
379 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
381 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
382 VirtIODevice
*vdev
= vq
->vdev
;
383 hwaddr pa
= offsetof(VRingUsed
, flags
);
390 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
391 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
| mask
);
392 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
395 /* Called within rcu_read_lock(). */
396 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
398 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
399 VirtIODevice
*vdev
= vq
->vdev
;
400 hwaddr pa
= offsetof(VRingUsed
, flags
);
407 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
408 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
& ~mask
);
409 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
412 /* Called within rcu_read_lock(). */
413 static inline void vring_set_avail_event(VirtQueue
*vq
, uint16_t val
)
415 VRingMemoryRegionCaches
*caches
;
417 if (!vq
->notification
) {
421 caches
= vring_get_region_caches(vq
);
426 pa
= offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
427 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
428 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
431 static void virtio_queue_split_set_notification(VirtQueue
*vq
, int enable
)
433 RCU_READ_LOCK_GUARD();
435 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
436 vring_set_avail_event(vq
, vring_avail_idx(vq
));
438 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
440 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
443 /* Expose avail event/used flags before caller checks the avail idx. */
448 static void virtio_queue_packed_set_notification(VirtQueue
*vq
, int enable
)
451 VRingPackedDescEvent e
;
452 VRingMemoryRegionCaches
*caches
;
454 RCU_READ_LOCK_GUARD();
455 caches
= vring_get_region_caches(vq
);
460 vring_packed_event_read(vq
->vdev
, &caches
->used
, &e
);
463 e
.flags
= VRING_PACKED_EVENT_FLAG_DISABLE
;
464 } else if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
465 off_wrap
= vq
->shadow_avail_idx
| vq
->shadow_avail_wrap_counter
<< 15;
466 vring_packed_off_wrap_write(vq
->vdev
, &caches
->used
, off_wrap
);
467 /* Make sure off_wrap is wrote before flags */
469 e
.flags
= VRING_PACKED_EVENT_FLAG_DESC
;
471 e
.flags
= VRING_PACKED_EVENT_FLAG_ENABLE
;
474 vring_packed_flags_write(vq
->vdev
, &caches
->used
, e
.flags
);
476 /* Expose avail event/used flags before caller checks the avail idx. */
481 bool virtio_queue_get_notification(VirtQueue
*vq
)
483 return vq
->notification
;
486 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
488 vq
->notification
= enable
;
490 if (!vq
->vring
.desc
) {
494 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
495 virtio_queue_packed_set_notification(vq
, enable
);
497 virtio_queue_split_set_notification(vq
, enable
);
501 int virtio_queue_ready(VirtQueue
*vq
)
503 return vq
->vring
.avail
!= 0;
506 static void vring_packed_desc_read_flags(VirtIODevice
*vdev
,
508 MemoryRegionCache
*cache
,
511 address_space_read_cached(cache
,
512 i
* sizeof(VRingPackedDesc
) +
513 offsetof(VRingPackedDesc
, flags
),
514 flags
, sizeof(*flags
));
515 virtio_tswap16s(vdev
, flags
);
518 static void vring_packed_desc_read(VirtIODevice
*vdev
,
519 VRingPackedDesc
*desc
,
520 MemoryRegionCache
*cache
,
521 int i
, bool strict_order
)
523 hwaddr off
= i
* sizeof(VRingPackedDesc
);
525 vring_packed_desc_read_flags(vdev
, &desc
->flags
, cache
, i
);
528 /* Make sure flags is read before the rest fields. */
532 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, addr
),
533 &desc
->addr
, sizeof(desc
->addr
));
534 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, id
),
535 &desc
->id
, sizeof(desc
->id
));
536 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, len
),
537 &desc
->len
, sizeof(desc
->len
));
538 virtio_tswap64s(vdev
, &desc
->addr
);
539 virtio_tswap16s(vdev
, &desc
->id
);
540 virtio_tswap32s(vdev
, &desc
->len
);
543 static void vring_packed_desc_write_data(VirtIODevice
*vdev
,
544 VRingPackedDesc
*desc
,
545 MemoryRegionCache
*cache
,
548 hwaddr off_id
= i
* sizeof(VRingPackedDesc
) +
549 offsetof(VRingPackedDesc
, id
);
550 hwaddr off_len
= i
* sizeof(VRingPackedDesc
) +
551 offsetof(VRingPackedDesc
, len
);
553 virtio_tswap32s(vdev
, &desc
->len
);
554 virtio_tswap16s(vdev
, &desc
->id
);
555 address_space_write_cached(cache
, off_id
, &desc
->id
, sizeof(desc
->id
));
556 address_space_cache_invalidate(cache
, off_id
, sizeof(desc
->id
));
557 address_space_write_cached(cache
, off_len
, &desc
->len
, sizeof(desc
->len
));
558 address_space_cache_invalidate(cache
, off_len
, sizeof(desc
->len
));
561 static void vring_packed_desc_write_flags(VirtIODevice
*vdev
,
562 VRingPackedDesc
*desc
,
563 MemoryRegionCache
*cache
,
566 hwaddr off
= i
* sizeof(VRingPackedDesc
) + offsetof(VRingPackedDesc
, flags
);
568 virtio_tswap16s(vdev
, &desc
->flags
);
569 address_space_write_cached(cache
, off
, &desc
->flags
, sizeof(desc
->flags
));
570 address_space_cache_invalidate(cache
, off
, sizeof(desc
->flags
));
573 static void vring_packed_desc_write(VirtIODevice
*vdev
,
574 VRingPackedDesc
*desc
,
575 MemoryRegionCache
*cache
,
576 int i
, bool strict_order
)
578 vring_packed_desc_write_data(vdev
, desc
, cache
, i
);
580 /* Make sure data is wrote before flags. */
583 vring_packed_desc_write_flags(vdev
, desc
, cache
, i
);
586 static inline bool is_desc_avail(uint16_t flags
, bool wrap_counter
)
590 avail
= !!(flags
& (1 << VRING_PACKED_DESC_F_AVAIL
));
591 used
= !!(flags
& (1 << VRING_PACKED_DESC_F_USED
));
592 return (avail
!= used
) && (avail
== wrap_counter
);
595 /* Fetch avail_idx from VQ memory only when we really need to know if
596 * guest has added some buffers.
597 * Called within rcu_read_lock(). */
598 static int virtio_queue_empty_rcu(VirtQueue
*vq
)
600 if (virtio_device_disabled(vq
->vdev
)) {
604 if (unlikely(!vq
->vring
.avail
)) {
608 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
612 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
615 static int virtio_queue_split_empty(VirtQueue
*vq
)
619 if (virtio_device_disabled(vq
->vdev
)) {
623 if (unlikely(!vq
->vring
.avail
)) {
627 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
631 RCU_READ_LOCK_GUARD();
632 empty
= vring_avail_idx(vq
) == vq
->last_avail_idx
;
636 static int virtio_queue_packed_empty_rcu(VirtQueue
*vq
)
638 struct VRingPackedDesc desc
;
639 VRingMemoryRegionCaches
*cache
;
641 if (unlikely(!vq
->vring
.desc
)) {
645 cache
= vring_get_region_caches(vq
);
650 vring_packed_desc_read_flags(vq
->vdev
, &desc
.flags
, &cache
->desc
,
653 return !is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
);
656 static int virtio_queue_packed_empty(VirtQueue
*vq
)
658 RCU_READ_LOCK_GUARD();
659 return virtio_queue_packed_empty_rcu(vq
);
662 int virtio_queue_empty(VirtQueue
*vq
)
664 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
665 return virtio_queue_packed_empty(vq
);
667 return virtio_queue_split_empty(vq
);
671 static void virtqueue_unmap_sg(VirtQueue
*vq
, const VirtQueueElement
*elem
,
674 AddressSpace
*dma_as
= vq
->vdev
->dma_as
;
679 for (i
= 0; i
< elem
->in_num
; i
++) {
680 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
682 dma_memory_unmap(dma_as
, elem
->in_sg
[i
].iov_base
,
683 elem
->in_sg
[i
].iov_len
,
684 DMA_DIRECTION_FROM_DEVICE
, size
);
689 for (i
= 0; i
< elem
->out_num
; i
++)
690 dma_memory_unmap(dma_as
, elem
->out_sg
[i
].iov_base
,
691 elem
->out_sg
[i
].iov_len
,
692 DMA_DIRECTION_TO_DEVICE
,
693 elem
->out_sg
[i
].iov_len
);
696 /* virtqueue_detach_element:
697 * @vq: The #VirtQueue
698 * @elem: The #VirtQueueElement
699 * @len: number of bytes written
701 * Detach the element from the virtqueue. This function is suitable for device
702 * reset or other situations where a #VirtQueueElement is simply freed and will
703 * not be pushed or discarded.
705 void virtqueue_detach_element(VirtQueue
*vq
, const VirtQueueElement
*elem
,
708 vq
->inuse
-= elem
->ndescs
;
709 virtqueue_unmap_sg(vq
, elem
, len
);
712 static void virtqueue_split_rewind(VirtQueue
*vq
, unsigned int num
)
714 vq
->last_avail_idx
-= num
;
717 static void virtqueue_packed_rewind(VirtQueue
*vq
, unsigned int num
)
719 if (vq
->last_avail_idx
< num
) {
720 vq
->last_avail_idx
= vq
->vring
.num
+ vq
->last_avail_idx
- num
;
721 vq
->last_avail_wrap_counter
^= 1;
723 vq
->last_avail_idx
-= num
;
728 * @vq: The #VirtQueue
729 * @elem: The #VirtQueueElement
730 * @len: number of bytes written
732 * Pretend the most recent element wasn't popped from the virtqueue. The next
733 * call to virtqueue_pop() will refetch the element.
735 void virtqueue_unpop(VirtQueue
*vq
, const VirtQueueElement
*elem
,
739 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
740 virtqueue_packed_rewind(vq
, 1);
742 virtqueue_split_rewind(vq
, 1);
745 virtqueue_detach_element(vq
, elem
, len
);
749 * @vq: The #VirtQueue
750 * @num: Number of elements to push back
752 * Pretend that elements weren't popped from the virtqueue. The next
753 * virtqueue_pop() will refetch the oldest element.
755 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
757 * Returns: true on success, false if @num is greater than the number of in use
760 bool virtqueue_rewind(VirtQueue
*vq
, unsigned int num
)
762 if (num
> vq
->inuse
) {
767 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
768 virtqueue_packed_rewind(vq
, num
);
770 virtqueue_split_rewind(vq
, num
);
775 static void virtqueue_split_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
776 unsigned int len
, unsigned int idx
)
780 if (unlikely(!vq
->vring
.used
)) {
784 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
786 uelem
.id
= elem
->index
;
788 vring_used_write(vq
, &uelem
, idx
);
791 static void virtqueue_packed_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
792 unsigned int len
, unsigned int idx
)
794 vq
->used_elems
[idx
].index
= elem
->index
;
795 vq
->used_elems
[idx
].len
= len
;
796 vq
->used_elems
[idx
].ndescs
= elem
->ndescs
;
799 static void virtqueue_packed_fill_desc(VirtQueue
*vq
,
800 const VirtQueueElement
*elem
,
805 VRingMemoryRegionCaches
*caches
;
806 VRingPackedDesc desc
= {
810 bool wrap_counter
= vq
->used_wrap_counter
;
812 if (unlikely(!vq
->vring
.desc
)) {
816 head
= vq
->used_idx
+ idx
;
817 if (head
>= vq
->vring
.num
) {
818 head
-= vq
->vring
.num
;
822 desc
.flags
|= (1 << VRING_PACKED_DESC_F_AVAIL
);
823 desc
.flags
|= (1 << VRING_PACKED_DESC_F_USED
);
825 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_AVAIL
);
826 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_USED
);
829 caches
= vring_get_region_caches(vq
);
834 vring_packed_desc_write(vq
->vdev
, &desc
, &caches
->desc
, head
, strict_order
);
837 /* Called within rcu_read_lock(). */
838 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
839 unsigned int len
, unsigned int idx
)
841 trace_virtqueue_fill(vq
, elem
, len
, idx
);
843 virtqueue_unmap_sg(vq
, elem
, len
);
845 if (virtio_device_disabled(vq
->vdev
)) {
849 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
850 virtqueue_packed_fill(vq
, elem
, len
, idx
);
852 virtqueue_split_fill(vq
, elem
, len
, idx
);
856 /* Called within rcu_read_lock(). */
857 static void virtqueue_split_flush(VirtQueue
*vq
, unsigned int count
)
861 if (unlikely(!vq
->vring
.used
)) {
865 /* Make sure buffer is written before we update index. */
867 trace_virtqueue_flush(vq
, count
);
870 vring_used_idx_set(vq
, new);
872 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
873 vq
->signalled_used_valid
= false;
876 static void virtqueue_packed_flush(VirtQueue
*vq
, unsigned int count
)
878 unsigned int i
, ndescs
= 0;
880 if (unlikely(!vq
->vring
.desc
)) {
884 for (i
= 1; i
< count
; i
++) {
885 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[i
], i
, false);
886 ndescs
+= vq
->used_elems
[i
].ndescs
;
888 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[0], 0, true);
889 ndescs
+= vq
->used_elems
[0].ndescs
;
892 vq
->used_idx
+= ndescs
;
893 if (vq
->used_idx
>= vq
->vring
.num
) {
894 vq
->used_idx
-= vq
->vring
.num
;
895 vq
->used_wrap_counter
^= 1;
899 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
901 if (virtio_device_disabled(vq
->vdev
)) {
906 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
907 virtqueue_packed_flush(vq
, count
);
909 virtqueue_split_flush(vq
, count
);
913 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
916 RCU_READ_LOCK_GUARD();
917 virtqueue_fill(vq
, elem
, len
, 0);
918 virtqueue_flush(vq
, 1);
921 /* Called within rcu_read_lock(). */
922 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
924 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
926 /* Check it isn't doing very strange things with descriptor numbers. */
927 if (num_heads
> vq
->vring
.num
) {
928 virtio_error(vq
->vdev
, "Guest moved used index from %u to %u",
929 idx
, vq
->shadow_avail_idx
);
932 /* On success, callers read a descriptor at vq->last_avail_idx.
933 * Make sure descriptor read does not bypass avail index read. */
941 /* Called within rcu_read_lock(). */
942 static bool virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
,
945 /* Grab the next descriptor number they're advertising, and increment
946 * the index we've seen. */
947 *head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
949 /* If their number is silly, that's a fatal mistake. */
950 if (*head
>= vq
->vring
.num
) {
951 virtio_error(vq
->vdev
, "Guest says index %u is available", *head
);
959 VIRTQUEUE_READ_DESC_ERROR
= -1,
960 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
961 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
964 static int virtqueue_split_read_next_desc(VirtIODevice
*vdev
, VRingDesc
*desc
,
965 MemoryRegionCache
*desc_cache
,
966 unsigned int max
, unsigned int *next
)
968 /* If this descriptor says it doesn't chain, we're done. */
969 if (!(desc
->flags
& VRING_DESC_F_NEXT
)) {
970 return VIRTQUEUE_READ_DESC_DONE
;
973 /* Check they're not leading us off end of descriptors. */
975 /* Make sure compiler knows to grab that: we don't want it changing! */
979 virtio_error(vdev
, "Desc next is %u", *next
);
980 return VIRTQUEUE_READ_DESC_ERROR
;
983 vring_split_desc_read(vdev
, desc
, desc_cache
, *next
);
984 return VIRTQUEUE_READ_DESC_MORE
;
987 static void virtqueue_split_get_avail_bytes(VirtQueue
*vq
,
988 unsigned int *in_bytes
, unsigned int *out_bytes
,
989 unsigned max_in_bytes
, unsigned max_out_bytes
)
991 VirtIODevice
*vdev
= vq
->vdev
;
992 unsigned int max
, idx
;
993 unsigned int total_bufs
, in_total
, out_total
;
994 VRingMemoryRegionCaches
*caches
;
995 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
999 RCU_READ_LOCK_GUARD();
1001 idx
= vq
->last_avail_idx
;
1002 total_bufs
= in_total
= out_total
= 0;
1004 max
= vq
->vring
.num
;
1005 caches
= vring_get_region_caches(vq
);
1010 while ((rc
= virtqueue_num_heads(vq
, idx
)) > 0) {
1011 MemoryRegionCache
*desc_cache
= &caches
->desc
;
1012 unsigned int num_bufs
;
1016 num_bufs
= total_bufs
;
1018 if (!virtqueue_get_head(vq
, idx
++, &i
)) {
1022 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1024 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1025 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1026 virtio_error(vdev
, "Invalid size for indirect buffer table");
1030 /* If we've got too many, that implies a descriptor loop. */
1031 if (num_bufs
>= max
) {
1032 virtio_error(vdev
, "Looped descriptor");
1036 /* loop over the indirect descriptor table */
1037 len
= address_space_cache_init(&indirect_desc_cache
,
1039 desc
.addr
, desc
.len
, false);
1040 desc_cache
= &indirect_desc_cache
;
1041 if (len
< desc
.len
) {
1042 virtio_error(vdev
, "Cannot map indirect buffer");
1046 max
= desc
.len
/ sizeof(VRingDesc
);
1048 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1052 /* If we've got too many, that implies a descriptor loop. */
1053 if (++num_bufs
> max
) {
1054 virtio_error(vdev
, "Looped descriptor");
1058 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1059 in_total
+= desc
.len
;
1061 out_total
+= desc
.len
;
1063 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1067 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1068 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1070 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1074 if (desc_cache
== &indirect_desc_cache
) {
1075 address_space_cache_destroy(&indirect_desc_cache
);
1078 total_bufs
= num_bufs
;
1087 address_space_cache_destroy(&indirect_desc_cache
);
1089 *in_bytes
= in_total
;
1092 *out_bytes
= out_total
;
1097 in_total
= out_total
= 0;
1101 static int virtqueue_packed_read_next_desc(VirtQueue
*vq
,
1102 VRingPackedDesc
*desc
,
1109 /* If this descriptor says it doesn't chain, we're done. */
1110 if (!indirect
&& !(desc
->flags
& VRING_DESC_F_NEXT
)) {
1111 return VIRTQUEUE_READ_DESC_DONE
;
1117 return VIRTQUEUE_READ_DESC_DONE
;
1119 (*next
) -= vq
->vring
.num
;
1123 vring_packed_desc_read(vq
->vdev
, desc
, desc_cache
, *next
, false);
1124 return VIRTQUEUE_READ_DESC_MORE
;
1127 static void virtqueue_packed_get_avail_bytes(VirtQueue
*vq
,
1128 unsigned int *in_bytes
,
1129 unsigned int *out_bytes
,
1130 unsigned max_in_bytes
,
1131 unsigned max_out_bytes
)
1133 VirtIODevice
*vdev
= vq
->vdev
;
1134 unsigned int max
, idx
;
1135 unsigned int total_bufs
, in_total
, out_total
;
1136 MemoryRegionCache
*desc_cache
;
1137 VRingMemoryRegionCaches
*caches
;
1138 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1140 VRingPackedDesc desc
;
1143 RCU_READ_LOCK_GUARD();
1144 idx
= vq
->last_avail_idx
;
1145 wrap_counter
= vq
->last_avail_wrap_counter
;
1146 total_bufs
= in_total
= out_total
= 0;
1148 max
= vq
->vring
.num
;
1149 caches
= vring_get_region_caches(vq
);
1155 unsigned int num_bufs
= total_bufs
;
1156 unsigned int i
= idx
;
1159 desc_cache
= &caches
->desc
;
1160 vring_packed_desc_read(vdev
, &desc
, desc_cache
, idx
, true);
1161 if (!is_desc_avail(desc
.flags
, wrap_counter
)) {
1165 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1166 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1167 virtio_error(vdev
, "Invalid size for indirect buffer table");
1171 /* If we've got too many, that implies a descriptor loop. */
1172 if (num_bufs
>= max
) {
1173 virtio_error(vdev
, "Looped descriptor");
1177 /* loop over the indirect descriptor table */
1178 len
= address_space_cache_init(&indirect_desc_cache
,
1180 desc
.addr
, desc
.len
, false);
1181 desc_cache
= &indirect_desc_cache
;
1182 if (len
< desc
.len
) {
1183 virtio_error(vdev
, "Cannot map indirect buffer");
1187 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1189 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1193 /* If we've got too many, that implies a descriptor loop. */
1194 if (++num_bufs
> max
) {
1195 virtio_error(vdev
, "Looped descriptor");
1199 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1200 in_total
+= desc
.len
;
1202 out_total
+= desc
.len
;
1204 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1208 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
,
1210 &indirect_desc_cache
);
1211 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1213 if (desc_cache
== &indirect_desc_cache
) {
1214 address_space_cache_destroy(&indirect_desc_cache
);
1218 idx
+= num_bufs
- total_bufs
;
1219 total_bufs
= num_bufs
;
1222 if (idx
>= vq
->vring
.num
) {
1223 idx
-= vq
->vring
.num
;
1228 /* Record the index and wrap counter for a kick we want */
1229 vq
->shadow_avail_idx
= idx
;
1230 vq
->shadow_avail_wrap_counter
= wrap_counter
;
1232 address_space_cache_destroy(&indirect_desc_cache
);
1234 *in_bytes
= in_total
;
1237 *out_bytes
= out_total
;
1242 in_total
= out_total
= 0;
1246 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
1247 unsigned int *out_bytes
,
1248 unsigned max_in_bytes
, unsigned max_out_bytes
)
1251 VRingMemoryRegionCaches
*caches
;
1253 if (unlikely(!vq
->vring
.desc
)) {
1257 caches
= vring_get_region_caches(vq
);
1262 desc_size
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
1263 sizeof(VRingPackedDesc
) : sizeof(VRingDesc
);
1264 if (caches
->desc
.len
< vq
->vring
.num
* desc_size
) {
1265 virtio_error(vq
->vdev
, "Cannot map descriptor ring");
1269 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1270 virtqueue_packed_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1271 max_in_bytes
, max_out_bytes
);
1273 virtqueue_split_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1274 max_in_bytes
, max_out_bytes
);
1287 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
1288 unsigned int out_bytes
)
1290 unsigned int in_total
, out_total
;
1292 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
1293 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
1296 static bool virtqueue_map_desc(VirtIODevice
*vdev
, unsigned int *p_num_sg
,
1297 hwaddr
*addr
, struct iovec
*iov
,
1298 unsigned int max_num_sg
, bool is_write
,
1299 hwaddr pa
, size_t sz
)
1302 unsigned num_sg
= *p_num_sg
;
1303 assert(num_sg
<= max_num_sg
);
1306 virtio_error(vdev
, "virtio: zero sized buffers are not allowed");
1313 if (num_sg
== max_num_sg
) {
1314 virtio_error(vdev
, "virtio: too many write descriptors in "
1319 iov
[num_sg
].iov_base
= dma_memory_map(vdev
->dma_as
, pa
, &len
,
1321 DMA_DIRECTION_FROM_DEVICE
:
1322 DMA_DIRECTION_TO_DEVICE
);
1323 if (!iov
[num_sg
].iov_base
) {
1324 virtio_error(vdev
, "virtio: bogus descriptor or out of resources");
1328 iov
[num_sg
].iov_len
= len
;
1342 /* Only used by error code paths before we have a VirtQueueElement (therefore
1343 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
1346 static void virtqueue_undo_map_desc(unsigned int out_num
, unsigned int in_num
,
1351 for (i
= 0; i
< out_num
+ in_num
; i
++) {
1352 int is_write
= i
>= out_num
;
1354 cpu_physical_memory_unmap(iov
->iov_base
, iov
->iov_len
, is_write
, 0);
1359 static void virtqueue_map_iovec(VirtIODevice
*vdev
, struct iovec
*sg
,
1360 hwaddr
*addr
, unsigned int num_sg
,
1366 for (i
= 0; i
< num_sg
; i
++) {
1367 len
= sg
[i
].iov_len
;
1368 sg
[i
].iov_base
= dma_memory_map(vdev
->dma_as
,
1369 addr
[i
], &len
, is_write
?
1370 DMA_DIRECTION_FROM_DEVICE
:
1371 DMA_DIRECTION_TO_DEVICE
);
1372 if (!sg
[i
].iov_base
) {
1373 error_report("virtio: error trying to map MMIO memory");
1376 if (len
!= sg
[i
].iov_len
) {
1377 error_report("virtio: unexpected memory split");
1383 void virtqueue_map(VirtIODevice
*vdev
, VirtQueueElement
*elem
)
1385 virtqueue_map_iovec(vdev
, elem
->in_sg
, elem
->in_addr
, elem
->in_num
, true);
1386 virtqueue_map_iovec(vdev
, elem
->out_sg
, elem
->out_addr
, elem
->out_num
,
1390 static void *virtqueue_alloc_element(size_t sz
, unsigned out_num
, unsigned in_num
)
1392 VirtQueueElement
*elem
;
1393 size_t in_addr_ofs
= QEMU_ALIGN_UP(sz
, __alignof__(elem
->in_addr
[0]));
1394 size_t out_addr_ofs
= in_addr_ofs
+ in_num
* sizeof(elem
->in_addr
[0]);
1395 size_t out_addr_end
= out_addr_ofs
+ out_num
* sizeof(elem
->out_addr
[0]);
1396 size_t in_sg_ofs
= QEMU_ALIGN_UP(out_addr_end
, __alignof__(elem
->in_sg
[0]));
1397 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
1398 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
1400 assert(sz
>= sizeof(VirtQueueElement
));
1401 elem
= g_malloc(out_sg_end
);
1402 trace_virtqueue_alloc_element(elem
, sz
, in_num
, out_num
);
1403 elem
->out_num
= out_num
;
1404 elem
->in_num
= in_num
;
1405 elem
->in_addr
= (void *)elem
+ in_addr_ofs
;
1406 elem
->out_addr
= (void *)elem
+ out_addr_ofs
;
1407 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
1408 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
1412 static void *virtqueue_split_pop(VirtQueue
*vq
, size_t sz
)
1414 unsigned int i
, head
, max
;
1415 VRingMemoryRegionCaches
*caches
;
1416 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1417 MemoryRegionCache
*desc_cache
;
1419 VirtIODevice
*vdev
= vq
->vdev
;
1420 VirtQueueElement
*elem
= NULL
;
1421 unsigned out_num
, in_num
, elem_entries
;
1422 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1423 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1427 RCU_READ_LOCK_GUARD();
1428 if (virtio_queue_empty_rcu(vq
)) {
1431 /* Needed after virtio_queue_empty(), see comment in
1432 * virtqueue_num_heads(). */
1435 /* When we start there are none of either input nor output. */
1436 out_num
= in_num
= elem_entries
= 0;
1438 max
= vq
->vring
.num
;
1440 if (vq
->inuse
>= vq
->vring
.num
) {
1441 virtio_error(vdev
, "Virtqueue size exceeded");
1445 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
++, &head
)) {
1449 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
1450 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1455 caches
= vring_get_region_caches(vq
);
1457 virtio_error(vdev
, "Region caches not initialized");
1461 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1462 virtio_error(vdev
, "Cannot map descriptor ring");
1466 desc_cache
= &caches
->desc
;
1467 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1468 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1469 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1470 virtio_error(vdev
, "Invalid size for indirect buffer table");
1474 /* loop over the indirect descriptor table */
1475 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1476 desc
.addr
, desc
.len
, false);
1477 desc_cache
= &indirect_desc_cache
;
1478 if (len
< desc
.len
) {
1479 virtio_error(vdev
, "Cannot map indirect buffer");
1483 max
= desc
.len
/ sizeof(VRingDesc
);
1485 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1488 /* Collect all the descriptors */
1492 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1493 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1495 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1496 desc
.addr
, desc
.len
);
1499 virtio_error(vdev
, "Incorrect order for descriptors");
1502 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1503 VIRTQUEUE_MAX_SIZE
, false,
1504 desc
.addr
, desc
.len
);
1510 /* If we've got too many, that implies a descriptor loop. */
1511 if (++elem_entries
> max
) {
1512 virtio_error(vdev
, "Looped descriptor");
1516 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1517 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1519 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1523 /* Now copy what we have collected and mapped */
1524 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1527 for (i
= 0; i
< out_num
; i
++) {
1528 elem
->out_addr
[i
] = addr
[i
];
1529 elem
->out_sg
[i
] = iov
[i
];
1531 for (i
= 0; i
< in_num
; i
++) {
1532 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1533 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1538 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1540 address_space_cache_destroy(&indirect_desc_cache
);
1545 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1549 static void *virtqueue_packed_pop(VirtQueue
*vq
, size_t sz
)
1551 unsigned int i
, max
;
1552 VRingMemoryRegionCaches
*caches
;
1553 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1554 MemoryRegionCache
*desc_cache
;
1556 VirtIODevice
*vdev
= vq
->vdev
;
1557 VirtQueueElement
*elem
= NULL
;
1558 unsigned out_num
, in_num
, elem_entries
;
1559 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1560 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1561 VRingPackedDesc desc
;
1565 RCU_READ_LOCK_GUARD();
1566 if (virtio_queue_packed_empty_rcu(vq
)) {
1570 /* When we start there are none of either input nor output. */
1571 out_num
= in_num
= elem_entries
= 0;
1573 max
= vq
->vring
.num
;
1575 if (vq
->inuse
>= vq
->vring
.num
) {
1576 virtio_error(vdev
, "Virtqueue size exceeded");
1580 i
= vq
->last_avail_idx
;
1582 caches
= vring_get_region_caches(vq
);
1584 virtio_error(vdev
, "Region caches not initialized");
1588 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1589 virtio_error(vdev
, "Cannot map descriptor ring");
1593 desc_cache
= &caches
->desc
;
1594 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, true);
1596 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1597 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1598 virtio_error(vdev
, "Invalid size for indirect buffer table");
1602 /* loop over the indirect descriptor table */
1603 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1604 desc
.addr
, desc
.len
, false);
1605 desc_cache
= &indirect_desc_cache
;
1606 if (len
< desc
.len
) {
1607 virtio_error(vdev
, "Cannot map indirect buffer");
1611 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1613 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1616 /* Collect all the descriptors */
1620 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1621 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1623 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1624 desc
.addr
, desc
.len
);
1627 virtio_error(vdev
, "Incorrect order for descriptors");
1630 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1631 VIRTQUEUE_MAX_SIZE
, false,
1632 desc
.addr
, desc
.len
);
1638 /* If we've got too many, that implies a descriptor loop. */
1639 if (++elem_entries
> max
) {
1640 virtio_error(vdev
, "Looped descriptor");
1644 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
, &i
,
1646 &indirect_desc_cache
);
1647 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1649 /* Now copy what we have collected and mapped */
1650 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1651 for (i
= 0; i
< out_num
; i
++) {
1652 elem
->out_addr
[i
] = addr
[i
];
1653 elem
->out_sg
[i
] = iov
[i
];
1655 for (i
= 0; i
< in_num
; i
++) {
1656 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1657 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1661 elem
->ndescs
= (desc_cache
== &indirect_desc_cache
) ? 1 : elem_entries
;
1662 vq
->last_avail_idx
+= elem
->ndescs
;
1663 vq
->inuse
+= elem
->ndescs
;
1665 if (vq
->last_avail_idx
>= vq
->vring
.num
) {
1666 vq
->last_avail_idx
-= vq
->vring
.num
;
1667 vq
->last_avail_wrap_counter
^= 1;
1670 vq
->shadow_avail_idx
= vq
->last_avail_idx
;
1671 vq
->shadow_avail_wrap_counter
= vq
->last_avail_wrap_counter
;
1673 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1675 address_space_cache_destroy(&indirect_desc_cache
);
1680 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1684 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
)
1686 if (virtio_device_disabled(vq
->vdev
)) {
1690 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1691 return virtqueue_packed_pop(vq
, sz
);
1693 return virtqueue_split_pop(vq
, sz
);
1697 static unsigned int virtqueue_packed_drop_all(VirtQueue
*vq
)
1699 VRingMemoryRegionCaches
*caches
;
1700 MemoryRegionCache
*desc_cache
;
1701 unsigned int dropped
= 0;
1702 VirtQueueElement elem
= {};
1703 VirtIODevice
*vdev
= vq
->vdev
;
1704 VRingPackedDesc desc
;
1706 caches
= vring_get_region_caches(vq
);
1711 desc_cache
= &caches
->desc
;
1713 virtio_queue_set_notification(vq
, 0);
1715 while (vq
->inuse
< vq
->vring
.num
) {
1716 unsigned int idx
= vq
->last_avail_idx
;
1718 * works similar to virtqueue_pop but does not map buffers
1719 * and does not allocate any memory.
1721 vring_packed_desc_read(vdev
, &desc
, desc_cache
,
1722 vq
->last_avail_idx
, true);
1723 if (!is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
)) {
1726 elem
.index
= desc
.id
;
1728 while (virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
,
1729 vq
->vring
.num
, &idx
, false)) {
1733 * immediately push the element, nothing to unmap
1734 * as both in_num and out_num are set to 0.
1736 virtqueue_push(vq
, &elem
, 0);
1738 vq
->last_avail_idx
+= elem
.ndescs
;
1739 if (vq
->last_avail_idx
>= vq
->vring
.num
) {
1740 vq
->last_avail_idx
-= vq
->vring
.num
;
1741 vq
->last_avail_wrap_counter
^= 1;
1748 static unsigned int virtqueue_split_drop_all(VirtQueue
*vq
)
1750 unsigned int dropped
= 0;
1751 VirtQueueElement elem
= {};
1752 VirtIODevice
*vdev
= vq
->vdev
;
1753 bool fEventIdx
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
);
1755 while (!virtio_queue_empty(vq
) && vq
->inuse
< vq
->vring
.num
) {
1756 /* works similar to virtqueue_pop but does not map buffers
1757 * and does not allocate any memory */
1759 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
, &elem
.index
)) {
1763 vq
->last_avail_idx
++;
1765 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1767 /* immediately push the element, nothing to unmap
1768 * as both in_num and out_num are set to 0 */
1769 virtqueue_push(vq
, &elem
, 0);
1776 /* virtqueue_drop_all:
1777 * @vq: The #VirtQueue
1778 * Drops all queued buffers and indicates them to the guest
1779 * as if they are done. Useful when buffers can not be
1780 * processed but must be returned to the guest.
1782 unsigned int virtqueue_drop_all(VirtQueue
*vq
)
1784 struct VirtIODevice
*vdev
= vq
->vdev
;
1786 if (virtio_device_disabled(vq
->vdev
)) {
1790 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1791 return virtqueue_packed_drop_all(vq
);
1793 return virtqueue_split_drop_all(vq
);
1797 /* Reading and writing a structure directly to QEMUFile is *awful*, but
1798 * it is what QEMU has always done by mistake. We can change it sooner
1799 * or later by bumping the version number of the affected vm states.
1800 * In the meanwhile, since the in-memory layout of VirtQueueElement
1801 * has changed, we need to marshal to and from the layout that was
1802 * used before the change.
1804 typedef struct VirtQueueElementOld
{
1806 unsigned int out_num
;
1807 unsigned int in_num
;
1808 hwaddr in_addr
[VIRTQUEUE_MAX_SIZE
];
1809 hwaddr out_addr
[VIRTQUEUE_MAX_SIZE
];
1810 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
1811 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
1812 } VirtQueueElementOld
;
1814 void *qemu_get_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
, size_t sz
)
1816 VirtQueueElement
*elem
;
1817 VirtQueueElementOld data
;
1820 qemu_get_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1822 /* TODO: teach all callers that this can fail, and return failure instead
1823 * of asserting here.
1824 * This is just one thing (there are probably more) that must be
1825 * fixed before we can allow NDEBUG compilation.
1827 assert(ARRAY_SIZE(data
.in_addr
) >= data
.in_num
);
1828 assert(ARRAY_SIZE(data
.out_addr
) >= data
.out_num
);
1830 elem
= virtqueue_alloc_element(sz
, data
.out_num
, data
.in_num
);
1831 elem
->index
= data
.index
;
1833 for (i
= 0; i
< elem
->in_num
; i
++) {
1834 elem
->in_addr
[i
] = data
.in_addr
[i
];
1837 for (i
= 0; i
< elem
->out_num
; i
++) {
1838 elem
->out_addr
[i
] = data
.out_addr
[i
];
1841 for (i
= 0; i
< elem
->in_num
; i
++) {
1842 /* Base is overwritten by virtqueue_map. */
1843 elem
->in_sg
[i
].iov_base
= 0;
1844 elem
->in_sg
[i
].iov_len
= data
.in_sg
[i
].iov_len
;
1847 for (i
= 0; i
< elem
->out_num
; i
++) {
1848 /* Base is overwritten by virtqueue_map. */
1849 elem
->out_sg
[i
].iov_base
= 0;
1850 elem
->out_sg
[i
].iov_len
= data
.out_sg
[i
].iov_len
;
1853 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1854 qemu_get_be32s(f
, &elem
->ndescs
);
1857 virtqueue_map(vdev
, elem
);
1861 void qemu_put_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
,
1862 VirtQueueElement
*elem
)
1864 VirtQueueElementOld data
;
1867 memset(&data
, 0, sizeof(data
));
1868 data
.index
= elem
->index
;
1869 data
.in_num
= elem
->in_num
;
1870 data
.out_num
= elem
->out_num
;
1872 for (i
= 0; i
< elem
->in_num
; i
++) {
1873 data
.in_addr
[i
] = elem
->in_addr
[i
];
1876 for (i
= 0; i
< elem
->out_num
; i
++) {
1877 data
.out_addr
[i
] = elem
->out_addr
[i
];
1880 for (i
= 0; i
< elem
->in_num
; i
++) {
1881 /* Base is overwritten by virtqueue_map when loading. Do not
1882 * save it, as it would leak the QEMU address space layout. */
1883 data
.in_sg
[i
].iov_len
= elem
->in_sg
[i
].iov_len
;
1886 for (i
= 0; i
< elem
->out_num
; i
++) {
1887 /* Do not save iov_base as above. */
1888 data
.out_sg
[i
].iov_len
= elem
->out_sg
[i
].iov_len
;
1891 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1892 qemu_put_be32s(f
, &elem
->ndescs
);
1895 qemu_put_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1899 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
1901 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1902 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1904 if (virtio_device_disabled(vdev
)) {
1909 k
->notify(qbus
->parent
, vector
);
1913 void virtio_update_irq(VirtIODevice
*vdev
)
1915 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
1918 static int virtio_validate_features(VirtIODevice
*vdev
)
1920 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1922 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
) &&
1923 !virtio_vdev_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
1927 if (k
->validate_features
) {
1928 return k
->validate_features(vdev
);
1934 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
1936 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1937 trace_virtio_set_status(vdev
, val
);
1939 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
1940 if (!(vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) &&
1941 val
& VIRTIO_CONFIG_S_FEATURES_OK
) {
1942 int ret
= virtio_validate_features(vdev
);
1950 if ((vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) !=
1951 (val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
1952 virtio_set_started(vdev
, val
& VIRTIO_CONFIG_S_DRIVER_OK
);
1955 if (k
->set_status
) {
1956 k
->set_status(vdev
, val
);
1963 static enum virtio_device_endian
virtio_default_endian(void)
1965 if (target_words_bigendian()) {
1966 return VIRTIO_DEVICE_ENDIAN_BIG
;
1968 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
1972 static enum virtio_device_endian
virtio_current_cpu_endian(void)
1974 CPUClass
*cc
= CPU_GET_CLASS(current_cpu
);
1976 if (cc
->virtio_is_big_endian(current_cpu
)) {
1977 return VIRTIO_DEVICE_ENDIAN_BIG
;
1979 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
1983 void virtio_reset(void *opaque
)
1985 VirtIODevice
*vdev
= opaque
;
1986 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1989 virtio_set_status(vdev
, 0);
1991 /* Guest initiated reset */
1992 vdev
->device_endian
= virtio_current_cpu_endian();
1995 vdev
->device_endian
= virtio_default_endian();
2002 vdev
->start_on_kick
= false;
2003 vdev
->started
= false;
2004 vdev
->broken
= false;
2005 vdev
->guest_features
= 0;
2006 vdev
->queue_sel
= 0;
2008 vdev
->disabled
= false;
2009 atomic_set(&vdev
->isr
, 0);
2010 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
2011 virtio_notify_vector(vdev
, vdev
->config_vector
);
2013 for(i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2014 vdev
->vq
[i
].vring
.desc
= 0;
2015 vdev
->vq
[i
].vring
.avail
= 0;
2016 vdev
->vq
[i
].vring
.used
= 0;
2017 vdev
->vq
[i
].last_avail_idx
= 0;
2018 vdev
->vq
[i
].shadow_avail_idx
= 0;
2019 vdev
->vq
[i
].used_idx
= 0;
2020 vdev
->vq
[i
].last_avail_wrap_counter
= true;
2021 vdev
->vq
[i
].shadow_avail_wrap_counter
= true;
2022 vdev
->vq
[i
].used_wrap_counter
= true;
2023 virtio_queue_set_vector(vdev
, i
, VIRTIO_NO_VECTOR
);
2024 vdev
->vq
[i
].signalled_used
= 0;
2025 vdev
->vq
[i
].signalled_used_valid
= false;
2026 vdev
->vq
[i
].notification
= true;
2027 vdev
->vq
[i
].vring
.num
= vdev
->vq
[i
].vring
.num_default
;
2028 vdev
->vq
[i
].inuse
= 0;
2029 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
2033 uint32_t virtio_config_readb(VirtIODevice
*vdev
, uint32_t addr
)
2035 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2038 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2039 return (uint32_t)-1;
2042 k
->get_config(vdev
, vdev
->config
);
2044 val
= ldub_p(vdev
->config
+ addr
);
2048 uint32_t virtio_config_readw(VirtIODevice
*vdev
, uint32_t addr
)
2050 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2053 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2054 return (uint32_t)-1;
2057 k
->get_config(vdev
, vdev
->config
);
2059 val
= lduw_p(vdev
->config
+ addr
);
2063 uint32_t virtio_config_readl(VirtIODevice
*vdev
, uint32_t addr
)
2065 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2068 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2069 return (uint32_t)-1;
2072 k
->get_config(vdev
, vdev
->config
);
2074 val
= ldl_p(vdev
->config
+ addr
);
2078 void virtio_config_writeb(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2080 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2083 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2087 stb_p(vdev
->config
+ addr
, val
);
2089 if (k
->set_config
) {
2090 k
->set_config(vdev
, vdev
->config
);
2094 void virtio_config_writew(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2096 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2097 uint16_t val
= data
;
2099 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2103 stw_p(vdev
->config
+ addr
, val
);
2105 if (k
->set_config
) {
2106 k
->set_config(vdev
, vdev
->config
);
2110 void virtio_config_writel(VirtIODevice
*vdev
, uint32_t addr
, uint32_t data
)
2112 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2113 uint32_t val
= data
;
2115 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2119 stl_p(vdev
->config
+ addr
, val
);
2121 if (k
->set_config
) {
2122 k
->set_config(vdev
, vdev
->config
);
2126 uint32_t virtio_config_modern_readb(VirtIODevice
*vdev
, uint32_t addr
)
2128 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2131 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2132 return (uint32_t)-1;
2135 k
->get_config(vdev
, vdev
->config
);
2137 val
= ldub_p(vdev
->config
+ addr
);
2141 uint32_t virtio_config_modern_readw(VirtIODevice
*vdev
, uint32_t addr
)
2143 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2146 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2147 return (uint32_t)-1;
2150 k
->get_config(vdev
, vdev
->config
);
2152 val
= lduw_le_p(vdev
->config
+ addr
);
2156 uint32_t virtio_config_modern_readl(VirtIODevice
*vdev
, uint32_t addr
)
2158 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2161 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2162 return (uint32_t)-1;
2165 k
->get_config(vdev
, vdev
->config
);
2167 val
= ldl_le_p(vdev
->config
+ addr
);
2171 void virtio_config_modern_writeb(VirtIODevice
*vdev
,
2172 uint32_t addr
, uint32_t data
)
2174 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2177 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2181 stb_p(vdev
->config
+ addr
, val
);
2183 if (k
->set_config
) {
2184 k
->set_config(vdev
, vdev
->config
);
2188 void virtio_config_modern_writew(VirtIODevice
*vdev
,
2189 uint32_t addr
, uint32_t data
)
2191 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2192 uint16_t val
= data
;
2194 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2198 stw_le_p(vdev
->config
+ addr
, val
);
2200 if (k
->set_config
) {
2201 k
->set_config(vdev
, vdev
->config
);
2205 void virtio_config_modern_writel(VirtIODevice
*vdev
,
2206 uint32_t addr
, uint32_t data
)
2208 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2209 uint32_t val
= data
;
2211 if (addr
+ sizeof(val
) > vdev
->config_len
) {
2215 stl_le_p(vdev
->config
+ addr
, val
);
2217 if (k
->set_config
) {
2218 k
->set_config(vdev
, vdev
->config
);
2222 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
2224 if (!vdev
->vq
[n
].vring
.num
) {
2227 vdev
->vq
[n
].vring
.desc
= addr
;
2228 virtio_queue_update_rings(vdev
, n
);
2231 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
2233 return vdev
->vq
[n
].vring
.desc
;
2236 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
2237 hwaddr avail
, hwaddr used
)
2239 if (!vdev
->vq
[n
].vring
.num
) {
2242 vdev
->vq
[n
].vring
.desc
= desc
;
2243 vdev
->vq
[n
].vring
.avail
= avail
;
2244 vdev
->vq
[n
].vring
.used
= used
;
2245 virtio_init_region_cache(vdev
, n
);
2248 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
2250 /* Don't allow guest to flip queue between existent and
2251 * nonexistent states, or to set it to an invalid size.
2253 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
2254 num
> VIRTQUEUE_MAX_SIZE
||
2258 vdev
->vq
[n
].vring
.num
= num
;
2261 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
)
2263 return QLIST_FIRST(&vdev
->vector_queues
[vector
]);
2266 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
)
2268 return QLIST_NEXT(vq
, node
);
2271 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
2273 return vdev
->vq
[n
].vring
.num
;
2276 int virtio_queue_get_max_num(VirtIODevice
*vdev
, int n
)
2278 return vdev
->vq
[n
].vring
.num_default
;
2281 int virtio_get_num_queues(VirtIODevice
*vdev
)
2285 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2286 if (!virtio_queue_get_num(vdev
, i
)) {
2294 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
2296 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2297 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2299 /* virtio-1 compliant devices cannot change the alignment */
2300 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2301 error_report("tried to modify queue alignment for virtio-1 device");
2304 /* Check that the transport told us it was going to do this
2305 * (so a buggy transport will immediately assert rather than
2306 * silently failing to migrate this state)
2308 assert(k
->has_variable_vring_alignment
);
2311 vdev
->vq
[n
].vring
.align
= align
;
2312 virtio_queue_update_rings(vdev
, n
);
2316 static bool virtio_queue_notify_aio_vq(VirtQueue
*vq
)
2320 if (vq
->vring
.desc
&& vq
->handle_aio_output
) {
2321 VirtIODevice
*vdev
= vq
->vdev
;
2323 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2324 ret
= vq
->handle_aio_output(vdev
, vq
);
2326 if (unlikely(vdev
->start_on_kick
)) {
2327 virtio_set_started(vdev
, true);
2334 static void virtio_queue_notify_vq(VirtQueue
*vq
)
2336 if (vq
->vring
.desc
&& vq
->handle_output
) {
2337 VirtIODevice
*vdev
= vq
->vdev
;
2339 if (unlikely(vdev
->broken
)) {
2343 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2344 vq
->handle_output(vdev
, vq
);
2346 if (unlikely(vdev
->start_on_kick
)) {
2347 virtio_set_started(vdev
, true);
2352 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
2354 VirtQueue
*vq
= &vdev
->vq
[n
];
2356 if (unlikely(!vq
->vring
.desc
|| vdev
->broken
)) {
2360 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2361 if (vq
->host_notifier_enabled
) {
2362 event_notifier_set(&vq
->host_notifier
);
2363 } else if (vq
->handle_output
) {
2364 vq
->handle_output(vdev
, vq
);
2366 if (unlikely(vdev
->start_on_kick
)) {
2367 virtio_set_started(vdev
, true);
2372 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
2374 return n
< VIRTIO_QUEUE_MAX
? vdev
->vq
[n
].vector
:
2378 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
2380 VirtQueue
*vq
= &vdev
->vq
[n
];
2382 if (n
< VIRTIO_QUEUE_MAX
) {
2383 if (vdev
->vector_queues
&&
2384 vdev
->vq
[n
].vector
!= VIRTIO_NO_VECTOR
) {
2385 QLIST_REMOVE(vq
, node
);
2387 vdev
->vq
[n
].vector
= vector
;
2388 if (vdev
->vector_queues
&&
2389 vector
!= VIRTIO_NO_VECTOR
) {
2390 QLIST_INSERT_HEAD(&vdev
->vector_queues
[vector
], vq
, node
);
2395 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
2396 VirtIOHandleOutput handle_output
)
2400 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2401 if (vdev
->vq
[i
].vring
.num
== 0)
2405 if (i
== VIRTIO_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
2408 vdev
->vq
[i
].vring
.num
= queue_size
;
2409 vdev
->vq
[i
].vring
.num_default
= queue_size
;
2410 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
2411 vdev
->vq
[i
].handle_output
= handle_output
;
2412 vdev
->vq
[i
].handle_aio_output
= NULL
;
2413 vdev
->vq
[i
].used_elems
= g_malloc0(sizeof(VirtQueueElement
) *
2416 return &vdev
->vq
[i
];
2419 void virtio_delete_queue(VirtQueue
*vq
)
2422 vq
->vring
.num_default
= 0;
2423 vq
->handle_output
= NULL
;
2424 vq
->handle_aio_output
= NULL
;
2425 g_free(vq
->used_elems
);
2426 vq
->used_elems
= NULL
;
2427 virtio_virtqueue_reset_region_cache(vq
);
2430 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
2432 if (n
< 0 || n
>= VIRTIO_QUEUE_MAX
) {
2436 virtio_delete_queue(&vdev
->vq
[n
]);
2439 static void virtio_set_isr(VirtIODevice
*vdev
, int value
)
2441 uint8_t old
= atomic_read(&vdev
->isr
);
2443 /* Do not write ISR if it does not change, so that its cacheline remains
2444 * shared in the common case where the guest does not read it.
2446 if ((old
& value
) != value
) {
2447 atomic_or(&vdev
->isr
, value
);
2451 static bool virtio_split_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2455 /* We need to expose used array entries before checking used event. */
2457 /* Always notify when queue is empty (when feature acknowledge) */
2458 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
2459 !vq
->inuse
&& virtio_queue_empty(vq
)) {
2463 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2464 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
2467 v
= vq
->signalled_used_valid
;
2468 vq
->signalled_used_valid
= true;
2469 old
= vq
->signalled_used
;
2470 new = vq
->signalled_used
= vq
->used_idx
;
2471 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
2474 static bool vring_packed_need_event(VirtQueue
*vq
, bool wrap
,
2475 uint16_t off_wrap
, uint16_t new,
2478 int off
= off_wrap
& ~(1 << 15);
2480 if (wrap
!= off_wrap
>> 15) {
2481 off
-= vq
->vring
.num
;
2484 return vring_need_event(off
, new, old
);
2487 static bool virtio_packed_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2489 VRingPackedDescEvent e
;
2492 VRingMemoryRegionCaches
*caches
;
2494 caches
= vring_get_region_caches(vq
);
2499 vring_packed_event_read(vdev
, &caches
->avail
, &e
);
2501 old
= vq
->signalled_used
;
2502 new = vq
->signalled_used
= vq
->used_idx
;
2503 v
= vq
->signalled_used_valid
;
2504 vq
->signalled_used_valid
= true;
2506 if (e
.flags
== VRING_PACKED_EVENT_FLAG_DISABLE
) {
2508 } else if (e
.flags
== VRING_PACKED_EVENT_FLAG_ENABLE
) {
2512 return !v
|| vring_packed_need_event(vq
, vq
->used_wrap_counter
,
2513 e
.off_wrap
, new, old
);
2516 /* Called within rcu_read_lock(). */
2517 static bool virtio_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2519 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
2520 return virtio_packed_should_notify(vdev
, vq
);
2522 return virtio_split_should_notify(vdev
, vq
);
2526 void virtio_notify_irqfd(VirtIODevice
*vdev
, VirtQueue
*vq
)
2528 WITH_RCU_READ_LOCK_GUARD() {
2529 if (!virtio_should_notify(vdev
, vq
)) {
2534 trace_virtio_notify_irqfd(vdev
, vq
);
2537 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
2538 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
2539 * incorrectly polling this bit during crashdump and hibernation
2540 * in MSI mode, causing a hang if this bit is never updated.
2541 * Recent releases of Windows do not really shut down, but rather
2542 * log out and hibernate to make the next startup faster. Hence,
2543 * this manifested as a more serious hang during shutdown with
2545 * Next driver release from 2016 fixed this problem, so working around it
2546 * is not a must, but it's easy to do so let's do it here.
2548 * Note: it's safe to update ISR from any thread as it was switched
2549 * to an atomic operation.
2551 virtio_set_isr(vq
->vdev
, 0x1);
2552 event_notifier_set(&vq
->guest_notifier
);
2555 static void virtio_irq(VirtQueue
*vq
)
2557 virtio_set_isr(vq
->vdev
, 0x1);
2558 virtio_notify_vector(vq
->vdev
, vq
->vector
);
2561 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2563 WITH_RCU_READ_LOCK_GUARD() {
2564 if (!virtio_should_notify(vdev
, vq
)) {
2569 trace_virtio_notify(vdev
, vq
);
2573 void virtio_notify_config(VirtIODevice
*vdev
)
2575 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
2578 virtio_set_isr(vdev
, 0x3);
2580 virtio_notify_vector(vdev
, vdev
->config_vector
);
2583 static bool virtio_device_endian_needed(void *opaque
)
2585 VirtIODevice
*vdev
= opaque
;
2587 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
2588 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2589 return vdev
->device_endian
!= virtio_default_endian();
2591 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
2592 return vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_LITTLE
;
2595 static bool virtio_64bit_features_needed(void *opaque
)
2597 VirtIODevice
*vdev
= opaque
;
2599 return (vdev
->host_features
>> 32) != 0;
2602 static bool virtio_virtqueue_needed(void *opaque
)
2604 VirtIODevice
*vdev
= opaque
;
2606 return virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
);
2609 static bool virtio_packed_virtqueue_needed(void *opaque
)
2611 VirtIODevice
*vdev
= opaque
;
2613 return virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
);
2616 static bool virtio_ringsize_needed(void *opaque
)
2618 VirtIODevice
*vdev
= opaque
;
2621 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2622 if (vdev
->vq
[i
].vring
.num
!= vdev
->vq
[i
].vring
.num_default
) {
2629 static bool virtio_extra_state_needed(void *opaque
)
2631 VirtIODevice
*vdev
= opaque
;
2632 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2633 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2635 return k
->has_extra_state
&&
2636 k
->has_extra_state(qbus
->parent
);
2639 static bool virtio_broken_needed(void *opaque
)
2641 VirtIODevice
*vdev
= opaque
;
2643 return vdev
->broken
;
2646 static bool virtio_started_needed(void *opaque
)
2648 VirtIODevice
*vdev
= opaque
;
2650 return vdev
->started
;
2653 static bool virtio_disabled_needed(void *opaque
)
2655 VirtIODevice
*vdev
= opaque
;
2657 return vdev
->disabled
;
2660 static const VMStateDescription vmstate_virtqueue
= {
2661 .name
= "virtqueue_state",
2663 .minimum_version_id
= 1,
2664 .fields
= (VMStateField
[]) {
2665 VMSTATE_UINT64(vring
.avail
, struct VirtQueue
),
2666 VMSTATE_UINT64(vring
.used
, struct VirtQueue
),
2667 VMSTATE_END_OF_LIST()
2671 static const VMStateDescription vmstate_packed_virtqueue
= {
2672 .name
= "packed_virtqueue_state",
2674 .minimum_version_id
= 1,
2675 .fields
= (VMStateField
[]) {
2676 VMSTATE_UINT16(last_avail_idx
, struct VirtQueue
),
2677 VMSTATE_BOOL(last_avail_wrap_counter
, struct VirtQueue
),
2678 VMSTATE_UINT16(used_idx
, struct VirtQueue
),
2679 VMSTATE_BOOL(used_wrap_counter
, struct VirtQueue
),
2680 VMSTATE_UINT32(inuse
, struct VirtQueue
),
2681 VMSTATE_END_OF_LIST()
2685 static const VMStateDescription vmstate_virtio_virtqueues
= {
2686 .name
= "virtio/virtqueues",
2688 .minimum_version_id
= 1,
2689 .needed
= &virtio_virtqueue_needed
,
2690 .fields
= (VMStateField
[]) {
2691 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2692 VIRTIO_QUEUE_MAX
, 0, vmstate_virtqueue
, VirtQueue
),
2693 VMSTATE_END_OF_LIST()
2697 static const VMStateDescription vmstate_virtio_packed_virtqueues
= {
2698 .name
= "virtio/packed_virtqueues",
2700 .minimum_version_id
= 1,
2701 .needed
= &virtio_packed_virtqueue_needed
,
2702 .fields
= (VMStateField
[]) {
2703 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2704 VIRTIO_QUEUE_MAX
, 0, vmstate_packed_virtqueue
, VirtQueue
),
2705 VMSTATE_END_OF_LIST()
2709 static const VMStateDescription vmstate_ringsize
= {
2710 .name
= "ringsize_state",
2712 .minimum_version_id
= 1,
2713 .fields
= (VMStateField
[]) {
2714 VMSTATE_UINT32(vring
.num_default
, struct VirtQueue
),
2715 VMSTATE_END_OF_LIST()
2719 static const VMStateDescription vmstate_virtio_ringsize
= {
2720 .name
= "virtio/ringsize",
2722 .minimum_version_id
= 1,
2723 .needed
= &virtio_ringsize_needed
,
2724 .fields
= (VMStateField
[]) {
2725 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2726 VIRTIO_QUEUE_MAX
, 0, vmstate_ringsize
, VirtQueue
),
2727 VMSTATE_END_OF_LIST()
2731 static int get_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2732 const VMStateField
*field
)
2734 VirtIODevice
*vdev
= pv
;
2735 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2736 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2738 if (!k
->load_extra_state
) {
2741 return k
->load_extra_state(qbus
->parent
, f
);
2745 static int put_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2746 const VMStateField
*field
, QJSON
*vmdesc
)
2748 VirtIODevice
*vdev
= pv
;
2749 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2750 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2752 k
->save_extra_state(qbus
->parent
, f
);
2756 static const VMStateInfo vmstate_info_extra_state
= {
2757 .name
= "virtqueue_extra_state",
2758 .get
= get_extra_state
,
2759 .put
= put_extra_state
,
2762 static const VMStateDescription vmstate_virtio_extra_state
= {
2763 .name
= "virtio/extra_state",
2765 .minimum_version_id
= 1,
2766 .needed
= &virtio_extra_state_needed
,
2767 .fields
= (VMStateField
[]) {
2769 .name
= "extra_state",
2771 .field_exists
= NULL
,
2773 .info
= &vmstate_info_extra_state
,
2774 .flags
= VMS_SINGLE
,
2777 VMSTATE_END_OF_LIST()
2781 static const VMStateDescription vmstate_virtio_device_endian
= {
2782 .name
= "virtio/device_endian",
2784 .minimum_version_id
= 1,
2785 .needed
= &virtio_device_endian_needed
,
2786 .fields
= (VMStateField
[]) {
2787 VMSTATE_UINT8(device_endian
, VirtIODevice
),
2788 VMSTATE_END_OF_LIST()
2792 static const VMStateDescription vmstate_virtio_64bit_features
= {
2793 .name
= "virtio/64bit_features",
2795 .minimum_version_id
= 1,
2796 .needed
= &virtio_64bit_features_needed
,
2797 .fields
= (VMStateField
[]) {
2798 VMSTATE_UINT64(guest_features
, VirtIODevice
),
2799 VMSTATE_END_OF_LIST()
2803 static const VMStateDescription vmstate_virtio_broken
= {
2804 .name
= "virtio/broken",
2806 .minimum_version_id
= 1,
2807 .needed
= &virtio_broken_needed
,
2808 .fields
= (VMStateField
[]) {
2809 VMSTATE_BOOL(broken
, VirtIODevice
),
2810 VMSTATE_END_OF_LIST()
2814 static const VMStateDescription vmstate_virtio_started
= {
2815 .name
= "virtio/started",
2817 .minimum_version_id
= 1,
2818 .needed
= &virtio_started_needed
,
2819 .fields
= (VMStateField
[]) {
2820 VMSTATE_BOOL(started
, VirtIODevice
),
2821 VMSTATE_END_OF_LIST()
2825 static const VMStateDescription vmstate_virtio_disabled
= {
2826 .name
= "virtio/disabled",
2828 .minimum_version_id
= 1,
2829 .needed
= &virtio_disabled_needed
,
2830 .fields
= (VMStateField
[]) {
2831 VMSTATE_BOOL(disabled
, VirtIODevice
),
2832 VMSTATE_END_OF_LIST()
2836 static const VMStateDescription vmstate_virtio
= {
2839 .minimum_version_id
= 1,
2840 .minimum_version_id_old
= 1,
2841 .fields
= (VMStateField
[]) {
2842 VMSTATE_END_OF_LIST()
2844 .subsections
= (const VMStateDescription
*[]) {
2845 &vmstate_virtio_device_endian
,
2846 &vmstate_virtio_64bit_features
,
2847 &vmstate_virtio_virtqueues
,
2848 &vmstate_virtio_ringsize
,
2849 &vmstate_virtio_broken
,
2850 &vmstate_virtio_extra_state
,
2851 &vmstate_virtio_started
,
2852 &vmstate_virtio_packed_virtqueues
,
2853 &vmstate_virtio_disabled
,
2858 int virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
2860 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2861 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2862 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2863 uint32_t guest_features_lo
= (vdev
->guest_features
& 0xffffffff);
2866 if (k
->save_config
) {
2867 k
->save_config(qbus
->parent
, f
);
2870 qemu_put_8s(f
, &vdev
->status
);
2871 qemu_put_8s(f
, &vdev
->isr
);
2872 qemu_put_be16s(f
, &vdev
->queue_sel
);
2873 qemu_put_be32s(f
, &guest_features_lo
);
2874 qemu_put_be32(f
, vdev
->config_len
);
2875 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
2877 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2878 if (vdev
->vq
[i
].vring
.num
== 0)
2882 qemu_put_be32(f
, i
);
2884 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2885 if (vdev
->vq
[i
].vring
.num
== 0)
2888 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
2889 if (k
->has_variable_vring_alignment
) {
2890 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
2893 * Save desc now, the rest of the ring addresses are saved in
2894 * subsections for VIRTIO-1 devices.
2896 qemu_put_be64(f
, vdev
->vq
[i
].vring
.desc
);
2897 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
2898 if (k
->save_queue
) {
2899 k
->save_queue(qbus
->parent
, i
, f
);
2903 if (vdc
->save
!= NULL
) {
2908 int ret
= vmstate_save_state(f
, vdc
->vmsd
, vdev
, NULL
);
2915 return vmstate_save_state(f
, &vmstate_virtio
, vdev
, NULL
);
2918 /* A wrapper for use as a VMState .put function */
2919 static int virtio_device_put(QEMUFile
*f
, void *opaque
, size_t size
,
2920 const VMStateField
*field
, QJSON
*vmdesc
)
2922 return virtio_save(VIRTIO_DEVICE(opaque
), f
);
2925 /* A wrapper for use as a VMState .get function */
2926 static int virtio_device_get(QEMUFile
*f
, void *opaque
, size_t size
,
2927 const VMStateField
*field
)
2929 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
2930 DeviceClass
*dc
= DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev
));
2932 return virtio_load(vdev
, f
, dc
->vmsd
->version_id
);
2935 const VMStateInfo virtio_vmstate_info
= {
2937 .get
= virtio_device_get
,
2938 .put
= virtio_device_put
,
2941 static int virtio_set_features_nocheck(VirtIODevice
*vdev
, uint64_t val
)
2943 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2944 bool bad
= (val
& ~(vdev
->host_features
)) != 0;
2946 val
&= vdev
->host_features
;
2947 if (k
->set_features
) {
2948 k
->set_features(vdev
, val
);
2950 vdev
->guest_features
= val
;
2951 return bad
? -1 : 0;
2954 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
)
2958 * The driver must not attempt to set features after feature negotiation
2961 if (vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
2964 ret
= virtio_set_features_nocheck(vdev
, val
);
2966 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2967 /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */
2969 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2970 if (vdev
->vq
[i
].vring
.num
!= 0) {
2971 virtio_init_region_cache(vdev
, i
);
2976 if (!virtio_device_started(vdev
, vdev
->status
) &&
2977 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2978 vdev
->start_on_kick
= true;
2984 size_t virtio_feature_get_config_size(VirtIOFeature
*feature_sizes
,
2985 uint64_t host_features
)
2987 size_t config_size
= 0;
2990 for (i
= 0; feature_sizes
[i
].flags
!= 0; i
++) {
2991 if (host_features
& feature_sizes
[i
].flags
) {
2992 config_size
= MAX(feature_sizes
[i
].end
, config_size
);
2999 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
)
3005 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3006 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3007 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
3010 * We poison the endianness to ensure it does not get used before
3011 * subsections have been loaded.
3013 vdev
->device_endian
= VIRTIO_DEVICE_ENDIAN_UNKNOWN
;
3015 if (k
->load_config
) {
3016 ret
= k
->load_config(qbus
->parent
, f
);
3021 qemu_get_8s(f
, &vdev
->status
);
3022 qemu_get_8s(f
, &vdev
->isr
);
3023 qemu_get_be16s(f
, &vdev
->queue_sel
);
3024 if (vdev
->queue_sel
>= VIRTIO_QUEUE_MAX
) {
3027 qemu_get_be32s(f
, &features
);
3030 * Temporarily set guest_features low bits - needed by
3031 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
3032 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
3034 * Note: devices should always test host features in future - don't create
3035 * new dependencies like this.
3037 vdev
->guest_features
= features
;
3039 config_len
= qemu_get_be32(f
);
3042 * There are cases where the incoming config can be bigger or smaller
3043 * than what we have; so load what we have space for, and skip
3044 * any excess that's in the stream.
3046 qemu_get_buffer(f
, vdev
->config
, MIN(config_len
, vdev
->config_len
));
3048 while (config_len
> vdev
->config_len
) {
3053 num
= qemu_get_be32(f
);
3055 if (num
> VIRTIO_QUEUE_MAX
) {
3056 error_report("Invalid number of virtqueues: 0x%x", num
);
3060 for (i
= 0; i
< num
; i
++) {
3061 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
3062 if (k
->has_variable_vring_alignment
) {
3063 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
3065 vdev
->vq
[i
].vring
.desc
= qemu_get_be64(f
);
3066 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
3067 vdev
->vq
[i
].signalled_used_valid
= false;
3068 vdev
->vq
[i
].notification
= true;
3070 if (!vdev
->vq
[i
].vring
.desc
&& vdev
->vq
[i
].last_avail_idx
) {
3071 error_report("VQ %d address 0x0 "
3072 "inconsistent with Host index 0x%x",
3073 i
, vdev
->vq
[i
].last_avail_idx
);
3076 if (k
->load_queue
) {
3077 ret
= k
->load_queue(qbus
->parent
, i
, f
);
3083 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
3085 if (vdc
->load
!= NULL
) {
3086 ret
= vdc
->load(vdev
, f
, version_id
);
3093 ret
= vmstate_load_state(f
, vdc
->vmsd
, vdev
, version_id
);
3100 ret
= vmstate_load_state(f
, &vmstate_virtio
, vdev
, 1);
3105 if (vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_UNKNOWN
) {
3106 vdev
->device_endian
= virtio_default_endian();
3109 if (virtio_64bit_features_needed(vdev
)) {
3111 * Subsection load filled vdev->guest_features. Run them
3112 * through virtio_set_features to sanity-check them against
3115 uint64_t features64
= vdev
->guest_features
;
3116 if (virtio_set_features_nocheck(vdev
, features64
) < 0) {
3117 error_report("Features 0x%" PRIx64
" unsupported. "
3118 "Allowed features: 0x%" PRIx64
,
3119 features64
, vdev
->host_features
);
3123 if (virtio_set_features_nocheck(vdev
, features
) < 0) {
3124 error_report("Features 0x%x unsupported. "
3125 "Allowed features: 0x%" PRIx64
,
3126 features
, vdev
->host_features
);
3131 if (!virtio_device_started(vdev
, vdev
->status
) &&
3132 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3133 vdev
->start_on_kick
= true;
3136 RCU_READ_LOCK_GUARD();
3137 for (i
= 0; i
< num
; i
++) {
3138 if (vdev
->vq
[i
].vring
.desc
) {
3142 * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
3143 * only the region cache needs to be set up. Legacy devices need
3144 * to calculate used and avail ring addresses based on the desc
3147 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3148 virtio_init_region_cache(vdev
, i
);
3150 virtio_queue_update_rings(vdev
, i
);
3153 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3154 vdev
->vq
[i
].shadow_avail_idx
= vdev
->vq
[i
].last_avail_idx
;
3155 vdev
->vq
[i
].shadow_avail_wrap_counter
=
3156 vdev
->vq
[i
].last_avail_wrap_counter
;
3160 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
3161 /* Check it isn't doing strange things with descriptor numbers. */
3162 if (nheads
> vdev
->vq
[i
].vring
.num
) {
3163 error_report("VQ %d size 0x%x Guest index 0x%x "
3164 "inconsistent with Host index 0x%x: delta 0x%x",
3165 i
, vdev
->vq
[i
].vring
.num
,
3166 vring_avail_idx(&vdev
->vq
[i
]),
3167 vdev
->vq
[i
].last_avail_idx
, nheads
);
3170 vdev
->vq
[i
].used_idx
= vring_used_idx(&vdev
->vq
[i
]);
3171 vdev
->vq
[i
].shadow_avail_idx
= vring_avail_idx(&vdev
->vq
[i
]);
3174 * Some devices migrate VirtQueueElements that have been popped
3175 * from the avail ring but not yet returned to the used ring.
3176 * Since max ring size < UINT16_MAX it's safe to use modulo
3177 * UINT16_MAX + 1 subtraction.
3179 vdev
->vq
[i
].inuse
= (uint16_t)(vdev
->vq
[i
].last_avail_idx
-
3180 vdev
->vq
[i
].used_idx
);
3181 if (vdev
->vq
[i
].inuse
> vdev
->vq
[i
].vring
.num
) {
3182 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
3184 i
, vdev
->vq
[i
].vring
.num
,
3185 vdev
->vq
[i
].last_avail_idx
,
3186 vdev
->vq
[i
].used_idx
);
3192 if (vdc
->post_load
) {
3193 ret
= vdc
->post_load(vdev
);
3202 void virtio_cleanup(VirtIODevice
*vdev
)
3204 qemu_del_vm_change_state_handler(vdev
->vmstate
);
3207 static void virtio_vmstate_change(void *opaque
, int running
, RunState state
)
3209 VirtIODevice
*vdev
= opaque
;
3210 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3211 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3212 bool backend_run
= running
&& virtio_device_started(vdev
, vdev
->status
);
3213 vdev
->vm_running
= running
;
3216 virtio_set_status(vdev
, vdev
->status
);
3219 if (k
->vmstate_change
) {
3220 k
->vmstate_change(qbus
->parent
, backend_run
);
3224 virtio_set_status(vdev
, vdev
->status
);
3228 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
3229 size_t vdev_size
, const char *vdev_name
)
3231 DeviceState
*vdev
= data
;
3233 object_initialize_child_with_props(proxy_obj
, "virtio-backend", vdev
,
3234 vdev_size
, vdev_name
, &error_abort
,
3236 qdev_alias_all_properties(vdev
, proxy_obj
);
3239 void virtio_init(VirtIODevice
*vdev
, const char *name
,
3240 uint16_t device_id
, size_t config_size
)
3242 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3243 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3245 int nvectors
= k
->query_nvectors
? k
->query_nvectors(qbus
->parent
) : 0;
3248 vdev
->vector_queues
=
3249 g_malloc0(sizeof(*vdev
->vector_queues
) * nvectors
);
3252 vdev
->start_on_kick
= false;
3253 vdev
->started
= false;
3254 vdev
->device_id
= device_id
;
3256 atomic_set(&vdev
->isr
, 0);
3257 vdev
->queue_sel
= 0;
3258 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
3259 vdev
->vq
= g_malloc0(sizeof(VirtQueue
) * VIRTIO_QUEUE_MAX
);
3260 vdev
->vm_running
= runstate_is_running();
3261 vdev
->broken
= false;
3262 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3263 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
3264 vdev
->vq
[i
].vdev
= vdev
;
3265 vdev
->vq
[i
].queue_index
= i
;
3266 vdev
->vq
[i
].host_notifier_enabled
= false;
3270 vdev
->config_len
= config_size
;
3271 if (vdev
->config_len
) {
3272 vdev
->config
= g_malloc0(config_size
);
3274 vdev
->config
= NULL
;
3276 vdev
->vmstate
= qdev_add_vm_change_state_handler(DEVICE(vdev
),
3277 virtio_vmstate_change
, vdev
);
3278 vdev
->device_endian
= virtio_default_endian();
3279 vdev
->use_guest_notifier_mask
= true;
3282 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
3284 return vdev
->vq
[n
].vring
.desc
;
3287 bool virtio_queue_enabled(VirtIODevice
*vdev
, int n
)
3289 return virtio_queue_get_desc_addr(vdev
, n
) != 0;
3292 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
3294 return vdev
->vq
[n
].vring
.avail
;
3297 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
3299 return vdev
->vq
[n
].vring
.used
;
3302 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
3304 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
3307 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
3311 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3312 return sizeof(struct VRingPackedDescEvent
);
3315 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3316 return offsetof(VRingAvail
, ring
) +
3317 sizeof(uint16_t) * vdev
->vq
[n
].vring
.num
+ s
;
3320 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
3324 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3325 return sizeof(struct VRingPackedDescEvent
);
3328 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3329 return offsetof(VRingUsed
, ring
) +
3330 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
+ s
;
3333 static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice
*vdev
,
3336 unsigned int avail
, used
;
3338 avail
= vdev
->vq
[n
].last_avail_idx
;
3339 avail
|= ((uint16_t)vdev
->vq
[n
].last_avail_wrap_counter
) << 15;
3341 used
= vdev
->vq
[n
].used_idx
;
3342 used
|= ((uint16_t)vdev
->vq
[n
].used_wrap_counter
) << 15;
3344 return avail
| used
<< 16;
3347 static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice
*vdev
,
3350 return vdev
->vq
[n
].last_avail_idx
;
3353 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
3355 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3356 return virtio_queue_packed_get_last_avail_idx(vdev
, n
);
3358 return virtio_queue_split_get_last_avail_idx(vdev
, n
);
3362 static void virtio_queue_packed_set_last_avail_idx(VirtIODevice
*vdev
,
3363 int n
, unsigned int idx
)
3365 struct VirtQueue
*vq
= &vdev
->vq
[n
];
3367 vq
->last_avail_idx
= vq
->shadow_avail_idx
= idx
& 0x7fff;
3368 vq
->last_avail_wrap_counter
=
3369 vq
->shadow_avail_wrap_counter
= !!(idx
& 0x8000);
3371 vq
->used_idx
= idx
& 0x7ffff;
3372 vq
->used_wrap_counter
= !!(idx
& 0x8000);
3375 static void virtio_queue_split_set_last_avail_idx(VirtIODevice
*vdev
,
3376 int n
, unsigned int idx
)
3378 vdev
->vq
[n
].last_avail_idx
= idx
;
3379 vdev
->vq
[n
].shadow_avail_idx
= idx
;
3382 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
,
3385 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3386 virtio_queue_packed_set_last_avail_idx(vdev
, n
, idx
);
3388 virtio_queue_split_set_last_avail_idx(vdev
, n
, idx
);
3392 static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice
*vdev
,
3395 /* We don't have a reference like avail idx in shared memory */
3399 static void virtio_queue_split_restore_last_avail_idx(VirtIODevice
*vdev
,
3402 RCU_READ_LOCK_GUARD();
3403 if (vdev
->vq
[n
].vring
.desc
) {
3404 vdev
->vq
[n
].last_avail_idx
= vring_used_idx(&vdev
->vq
[n
]);
3405 vdev
->vq
[n
].shadow_avail_idx
= vdev
->vq
[n
].last_avail_idx
;
3409 void virtio_queue_restore_last_avail_idx(VirtIODevice
*vdev
, int n
)
3411 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3412 virtio_queue_packed_restore_last_avail_idx(vdev
, n
);
3414 virtio_queue_split_restore_last_avail_idx(vdev
, n
);
3418 static void virtio_queue_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3420 /* used idx was updated through set_last_avail_idx() */
3424 static void virtio_split_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3426 RCU_READ_LOCK_GUARD();
3427 if (vdev
->vq
[n
].vring
.desc
) {
3428 vdev
->vq
[n
].used_idx
= vring_used_idx(&vdev
->vq
[n
]);
3432 void virtio_queue_update_used_idx(VirtIODevice
*vdev
, int n
)
3434 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3435 return virtio_queue_packed_update_used_idx(vdev
, n
);
3437 return virtio_split_packed_update_used_idx(vdev
, n
);
3441 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
3443 vdev
->vq
[n
].signalled_used_valid
= false;
3446 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
3448 return vdev
->vq
+ n
;
3451 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
3453 return vq
->queue_index
;
3456 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
3458 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
3459 if (event_notifier_test_and_clear(n
)) {
3464 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
3467 if (assign
&& !with_irqfd
) {
3468 event_notifier_set_handler(&vq
->guest_notifier
,
3469 virtio_queue_guest_notifier_read
);
3471 event_notifier_set_handler(&vq
->guest_notifier
, NULL
);
3474 /* Test and clear notifier before closing it,
3475 * in case poll callback didn't have time to run. */
3476 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
3480 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
3482 return &vq
->guest_notifier
;
3485 static void virtio_queue_host_notifier_aio_read(EventNotifier
*n
)
3487 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3488 if (event_notifier_test_and_clear(n
)) {
3489 virtio_queue_notify_aio_vq(vq
);
3493 static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier
*n
)
3495 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3497 virtio_queue_set_notification(vq
, 0);
3500 static bool virtio_queue_host_notifier_aio_poll(void *opaque
)
3502 EventNotifier
*n
= opaque
;
3503 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3505 if (!vq
->vring
.desc
|| virtio_queue_empty(vq
)) {
3509 return virtio_queue_notify_aio_vq(vq
);
3512 static void virtio_queue_host_notifier_aio_poll_end(EventNotifier
*n
)
3514 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3516 /* Caller polls once more after this to catch requests that race with us */
3517 virtio_queue_set_notification(vq
, 1);
3520 void virtio_queue_aio_set_host_notifier_handler(VirtQueue
*vq
, AioContext
*ctx
,
3521 VirtIOHandleAIOOutput handle_output
)
3523 if (handle_output
) {
3524 vq
->handle_aio_output
= handle_output
;
3525 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true,
3526 virtio_queue_host_notifier_aio_read
,
3527 virtio_queue_host_notifier_aio_poll
);
3528 aio_set_event_notifier_poll(ctx
, &vq
->host_notifier
,
3529 virtio_queue_host_notifier_aio_poll_begin
,
3530 virtio_queue_host_notifier_aio_poll_end
);
3532 aio_set_event_notifier(ctx
, &vq
->host_notifier
, true, NULL
, NULL
);
3533 /* Test and clear notifier before after disabling event,
3534 * in case poll callback didn't have time to run. */
3535 virtio_queue_host_notifier_aio_read(&vq
->host_notifier
);
3536 vq
->handle_aio_output
= NULL
;
3540 void virtio_queue_host_notifier_read(EventNotifier
*n
)
3542 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3543 if (event_notifier_test_and_clear(n
)) {
3544 virtio_queue_notify_vq(vq
);
3548 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
3550 return &vq
->host_notifier
;
3553 void virtio_queue_set_host_notifier_enabled(VirtQueue
*vq
, bool enabled
)
3555 vq
->host_notifier_enabled
= enabled
;
3558 int virtio_queue_set_host_notifier_mr(VirtIODevice
*vdev
, int n
,
3559 MemoryRegion
*mr
, bool assign
)
3561 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3562 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3564 if (k
->set_host_notifier_mr
) {
3565 return k
->set_host_notifier_mr(qbus
->parent
, n
, mr
, assign
);
3571 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
3573 g_free(vdev
->bus_name
);
3574 vdev
->bus_name
= g_strdup(bus_name
);
3577 void GCC_FMT_ATTR(2, 3) virtio_error(VirtIODevice
*vdev
, const char *fmt
, ...)
3582 error_vreport(fmt
, ap
);
3585 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3586 vdev
->status
= vdev
->status
| VIRTIO_CONFIG_S_NEEDS_RESET
;
3587 virtio_notify_config(vdev
);
3590 vdev
->broken
= true;
3593 static void virtio_memory_listener_commit(MemoryListener
*listener
)
3595 VirtIODevice
*vdev
= container_of(listener
, VirtIODevice
, listener
);
3598 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3599 if (vdev
->vq
[i
].vring
.num
== 0) {
3602 virtio_init_region_cache(vdev
, i
);
3606 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
3608 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3609 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3612 /* Devices should either use vmsd or the load/save methods */
3613 assert(!vdc
->vmsd
|| !vdc
->load
);
3615 if (vdc
->realize
!= NULL
) {
3616 vdc
->realize(dev
, &err
);
3618 error_propagate(errp
, err
);
3623 virtio_bus_device_plugged(vdev
, &err
);
3625 error_propagate(errp
, err
);
3626 vdc
->unrealize(dev
);
3630 vdev
->listener
.commit
= virtio_memory_listener_commit
;
3631 memory_listener_register(&vdev
->listener
, vdev
->dma_as
);
3634 static void virtio_device_unrealize(DeviceState
*dev
)
3636 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3637 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3639 virtio_bus_device_unplugged(vdev
);
3641 if (vdc
->unrealize
!= NULL
) {
3642 vdc
->unrealize(dev
);
3645 g_free(vdev
->bus_name
);
3646 vdev
->bus_name
= NULL
;
3649 static void virtio_device_free_virtqueues(VirtIODevice
*vdev
)
3656 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3657 if (vdev
->vq
[i
].vring
.num
== 0) {
3660 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
3665 static void virtio_device_instance_finalize(Object
*obj
)
3667 VirtIODevice
*vdev
= VIRTIO_DEVICE(obj
);
3669 memory_listener_unregister(&vdev
->listener
);
3670 virtio_device_free_virtqueues(vdev
);
3672 g_free(vdev
->config
);
3673 g_free(vdev
->vector_queues
);
3676 static Property virtio_properties
[] = {
3677 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice
, host_features
),
3678 DEFINE_PROP_BOOL("use-started", VirtIODevice
, use_started
, true),
3679 DEFINE_PROP_BOOL("use-disabled-flag", VirtIODevice
, use_disabled_flag
, true),
3680 DEFINE_PROP_END_OF_LIST(),
3683 static int virtio_device_start_ioeventfd_impl(VirtIODevice
*vdev
)
3685 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3688 memory_region_transaction_begin();
3689 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3690 VirtQueue
*vq
= &vdev
->vq
[n
];
3691 if (!virtio_queue_get_num(vdev
, n
)) {
3694 r
= virtio_bus_set_host_notifier(qbus
, n
, true);
3699 event_notifier_set_handler(&vq
->host_notifier
,
3700 virtio_queue_host_notifier_read
);
3703 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3704 /* Kick right away to begin processing requests already in vring */
3705 VirtQueue
*vq
= &vdev
->vq
[n
];
3706 if (!vq
->vring
.num
) {
3709 event_notifier_set(&vq
->host_notifier
);
3711 memory_region_transaction_commit();
3715 i
= n
; /* save n for a second iteration after transaction is committed. */
3717 VirtQueue
*vq
= &vdev
->vq
[n
];
3718 if (!virtio_queue_get_num(vdev
, n
)) {
3722 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3723 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3726 memory_region_transaction_commit();
3729 if (!virtio_queue_get_num(vdev
, i
)) {
3732 virtio_bus_cleanup_host_notifier(qbus
, i
);
3737 int virtio_device_start_ioeventfd(VirtIODevice
*vdev
)
3739 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3740 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3742 return virtio_bus_start_ioeventfd(vbus
);
3745 static void virtio_device_stop_ioeventfd_impl(VirtIODevice
*vdev
)
3747 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3750 memory_region_transaction_begin();
3751 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3752 VirtQueue
*vq
= &vdev
->vq
[n
];
3754 if (!virtio_queue_get_num(vdev
, n
)) {
3757 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3758 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3761 memory_region_transaction_commit();
3763 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3764 if (!virtio_queue_get_num(vdev
, n
)) {
3767 virtio_bus_cleanup_host_notifier(qbus
, n
);
3771 int virtio_device_grab_ioeventfd(VirtIODevice
*vdev
)
3773 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3774 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3776 return virtio_bus_grab_ioeventfd(vbus
);
3779 void virtio_device_release_ioeventfd(VirtIODevice
*vdev
)
3781 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3782 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3784 virtio_bus_release_ioeventfd(vbus
);
3787 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
3789 /* Set the default value here. */
3790 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
3791 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3793 dc
->realize
= virtio_device_realize
;
3794 dc
->unrealize
= virtio_device_unrealize
;
3795 dc
->bus_type
= TYPE_VIRTIO_BUS
;
3796 device_class_set_props(dc
, virtio_properties
);
3797 vdc
->start_ioeventfd
= virtio_device_start_ioeventfd_impl
;
3798 vdc
->stop_ioeventfd
= virtio_device_stop_ioeventfd_impl
;
3800 vdc
->legacy_features
|= VIRTIO_LEGACY_FEATURES
;
3803 bool virtio_device_ioeventfd_enabled(VirtIODevice
*vdev
)
3805 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3806 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3808 return virtio_bus_ioeventfd_enabled(vbus
);
3811 static const TypeInfo virtio_device_info
= {
3812 .name
= TYPE_VIRTIO_DEVICE
,
3813 .parent
= TYPE_DEVICE
,
3814 .instance_size
= sizeof(VirtIODevice
),
3815 .class_init
= virtio_device_class_init
,
3816 .instance_finalize
= virtio_device_instance_finalize
,
3818 .class_size
= sizeof(VirtioDeviceClass
),
3821 static void virtio_register_types(void)
3823 type_register_static(&virtio_device_info
);
3826 type_init(virtio_register_types
)