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"
16 #include "qapi/qapi-commands-virtio.h"
18 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/module.h"
22 #include "qom/object_interfaces.h"
23 #include "hw/core/cpu.h"
24 #include "hw/virtio/virtio.h"
25 #include "hw/virtio/vhost.h"
26 #include "migration/qemu-file-types.h"
27 #include "qemu/atomic.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/qdev-properties.h"
30 #include "hw/virtio/virtio-access.h"
31 #include "sysemu/dma.h"
32 #include "sysemu/runstate.h"
33 #include "virtio-qmp.h"
35 #include "standard-headers/linux/virtio_ids.h"
36 #include "standard-headers/linux/vhost_types.h"
37 #include "standard-headers/linux/virtio_blk.h"
38 #include "standard-headers/linux/virtio_console.h"
39 #include "standard-headers/linux/virtio_gpu.h"
40 #include "standard-headers/linux/virtio_net.h"
41 #include "standard-headers/linux/virtio_scsi.h"
42 #include "standard-headers/linux/virtio_i2c.h"
43 #include "standard-headers/linux/virtio_balloon.h"
44 #include "standard-headers/linux/virtio_iommu.h"
45 #include "standard-headers/linux/virtio_mem.h"
46 #include "standard-headers/linux/virtio_vsock.h"
48 QmpVirtIODeviceList virtio_list
;
51 * Maximum size of virtio device config space
53 #define VHOST_USER_MAX_CONFIG_SIZE 256
56 * The alignment to use between consumer and producer parts of vring.
57 * x86 pagesize again. This is the default, used by transports like PCI
58 * which don't provide a means for the guest to tell the host the alignment.
60 #define VIRTIO_PCI_VRING_ALIGN 4096
62 typedef struct VRingDesc
70 typedef struct VRingPackedDesc
{
77 typedef struct VRingAvail
84 typedef struct VRingUsedElem
90 typedef struct VRingUsed
97 typedef struct VRingMemoryRegionCaches
{
99 MemoryRegionCache desc
;
100 MemoryRegionCache avail
;
101 MemoryRegionCache used
;
102 } VRingMemoryRegionCaches
;
107 unsigned int num_default
;
112 VRingMemoryRegionCaches
*caches
;
115 typedef struct VRingPackedDescEvent
{
118 } VRingPackedDescEvent
;
123 VirtQueueElement
*used_elems
;
125 /* Next head to pop */
126 uint16_t last_avail_idx
;
127 bool last_avail_wrap_counter
;
129 /* Last avail_idx read from VQ. */
130 uint16_t shadow_avail_idx
;
131 bool shadow_avail_wrap_counter
;
134 bool used_wrap_counter
;
136 /* Last used index value we have signalled on */
137 uint16_t signalled_used
;
139 /* Last used index value we have signalled on */
140 bool signalled_used_valid
;
142 /* Notification enabled? */
145 uint16_t queue_index
;
150 VirtIOHandleOutput handle_output
;
152 EventNotifier guest_notifier
;
153 EventNotifier host_notifier
;
154 bool host_notifier_enabled
;
155 QLIST_ENTRY(VirtQueue
) node
;
158 const char *virtio_device_names
[] = {
159 [VIRTIO_ID_NET
] = "virtio-net",
160 [VIRTIO_ID_BLOCK
] = "virtio-blk",
161 [VIRTIO_ID_CONSOLE
] = "virtio-serial",
162 [VIRTIO_ID_RNG
] = "virtio-rng",
163 [VIRTIO_ID_BALLOON
] = "virtio-balloon",
164 [VIRTIO_ID_IOMEM
] = "virtio-iomem",
165 [VIRTIO_ID_RPMSG
] = "virtio-rpmsg",
166 [VIRTIO_ID_SCSI
] = "virtio-scsi",
167 [VIRTIO_ID_9P
] = "virtio-9p",
168 [VIRTIO_ID_MAC80211_WLAN
] = "virtio-mac-wlan",
169 [VIRTIO_ID_RPROC_SERIAL
] = "virtio-rproc-serial",
170 [VIRTIO_ID_CAIF
] = "virtio-caif",
171 [VIRTIO_ID_MEMORY_BALLOON
] = "virtio-mem-balloon",
172 [VIRTIO_ID_GPU
] = "virtio-gpu",
173 [VIRTIO_ID_CLOCK
] = "virtio-clk",
174 [VIRTIO_ID_INPUT
] = "virtio-input",
175 [VIRTIO_ID_VSOCK
] = "vhost-vsock",
176 [VIRTIO_ID_CRYPTO
] = "virtio-crypto",
177 [VIRTIO_ID_SIGNAL_DIST
] = "virtio-signal",
178 [VIRTIO_ID_PSTORE
] = "virtio-pstore",
179 [VIRTIO_ID_IOMMU
] = "virtio-iommu",
180 [VIRTIO_ID_MEM
] = "virtio-mem",
181 [VIRTIO_ID_SOUND
] = "virtio-sound",
182 [VIRTIO_ID_FS
] = "virtio-user-fs",
183 [VIRTIO_ID_PMEM
] = "virtio-pmem",
184 [VIRTIO_ID_RPMB
] = "virtio-rpmb",
185 [VIRTIO_ID_MAC80211_HWSIM
] = "virtio-mac-hwsim",
186 [VIRTIO_ID_VIDEO_ENCODER
] = "virtio-vid-encoder",
187 [VIRTIO_ID_VIDEO_DECODER
] = "virtio-vid-decoder",
188 [VIRTIO_ID_SCMI
] = "virtio-scmi",
189 [VIRTIO_ID_NITRO_SEC_MOD
] = "virtio-nitro-sec-mod",
190 [VIRTIO_ID_I2C_ADAPTER
] = "vhost-user-i2c",
191 [VIRTIO_ID_WATCHDOG
] = "virtio-watchdog",
192 [VIRTIO_ID_CAN
] = "virtio-can",
193 [VIRTIO_ID_DMABUF
] = "virtio-dmabuf",
194 [VIRTIO_ID_PARAM_SERV
] = "virtio-param-serv",
195 [VIRTIO_ID_AUDIO_POLICY
] = "virtio-audio-pol",
196 [VIRTIO_ID_BT
] = "virtio-bluetooth",
197 [VIRTIO_ID_GPIO
] = "virtio-gpio"
200 static const char *virtio_id_to_name(uint16_t device_id
)
202 assert(device_id
< G_N_ELEMENTS(virtio_device_names
));
203 const char *name
= virtio_device_names
[device_id
];
204 assert(name
!= NULL
);
208 /* Called within call_rcu(). */
209 static void virtio_free_region_cache(VRingMemoryRegionCaches
*caches
)
211 assert(caches
!= NULL
);
212 address_space_cache_destroy(&caches
->desc
);
213 address_space_cache_destroy(&caches
->avail
);
214 address_space_cache_destroy(&caches
->used
);
218 static void virtio_virtqueue_reset_region_cache(struct VirtQueue
*vq
)
220 VRingMemoryRegionCaches
*caches
;
222 caches
= qatomic_read(&vq
->vring
.caches
);
223 qatomic_rcu_set(&vq
->vring
.caches
, NULL
);
225 call_rcu(caches
, virtio_free_region_cache
, rcu
);
229 void virtio_init_region_cache(VirtIODevice
*vdev
, int n
)
231 VirtQueue
*vq
= &vdev
->vq
[n
];
232 VRingMemoryRegionCaches
*old
= vq
->vring
.caches
;
233 VRingMemoryRegionCaches
*new = NULL
;
239 addr
= vq
->vring
.desc
;
243 new = g_new0(VRingMemoryRegionCaches
, 1);
244 size
= virtio_queue_get_desc_size(vdev
, n
);
245 packed
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
247 len
= address_space_cache_init(&new->desc
, vdev
->dma_as
,
250 virtio_error(vdev
, "Cannot map desc");
254 size
= virtio_queue_get_used_size(vdev
, n
);
255 len
= address_space_cache_init(&new->used
, vdev
->dma_as
,
256 vq
->vring
.used
, size
, true);
258 virtio_error(vdev
, "Cannot map used");
262 size
= virtio_queue_get_avail_size(vdev
, n
);
263 len
= address_space_cache_init(&new->avail
, vdev
->dma_as
,
264 vq
->vring
.avail
, size
, false);
266 virtio_error(vdev
, "Cannot map avail");
270 qatomic_rcu_set(&vq
->vring
.caches
, new);
272 call_rcu(old
, virtio_free_region_cache
, rcu
);
277 address_space_cache_destroy(&new->avail
);
279 address_space_cache_destroy(&new->used
);
281 address_space_cache_destroy(&new->desc
);
284 virtio_virtqueue_reset_region_cache(vq
);
287 /* virt queue functions */
288 void virtio_queue_update_rings(VirtIODevice
*vdev
, int n
)
290 VRing
*vring
= &vdev
->vq
[n
].vring
;
292 if (!vring
->num
|| !vring
->desc
|| !vring
->align
) {
293 /* not yet setup -> nothing to do */
296 vring
->avail
= vring
->desc
+ vring
->num
* sizeof(VRingDesc
);
297 vring
->used
= vring_align(vring
->avail
+
298 offsetof(VRingAvail
, ring
[vring
->num
]),
300 virtio_init_region_cache(vdev
, n
);
303 /* Called within rcu_read_lock(). */
304 static void vring_split_desc_read(VirtIODevice
*vdev
, VRingDesc
*desc
,
305 MemoryRegionCache
*cache
, int i
)
307 address_space_read_cached(cache
, i
* sizeof(VRingDesc
),
308 desc
, sizeof(VRingDesc
));
309 virtio_tswap64s(vdev
, &desc
->addr
);
310 virtio_tswap32s(vdev
, &desc
->len
);
311 virtio_tswap16s(vdev
, &desc
->flags
);
312 virtio_tswap16s(vdev
, &desc
->next
);
315 static void vring_packed_event_read(VirtIODevice
*vdev
,
316 MemoryRegionCache
*cache
,
317 VRingPackedDescEvent
*e
)
319 hwaddr off_off
= offsetof(VRingPackedDescEvent
, off_wrap
);
320 hwaddr off_flags
= offsetof(VRingPackedDescEvent
, flags
);
322 e
->flags
= virtio_lduw_phys_cached(vdev
, cache
, off_flags
);
323 /* Make sure flags is seen before off_wrap */
325 e
->off_wrap
= virtio_lduw_phys_cached(vdev
, cache
, off_off
);
326 virtio_tswap16s(vdev
, &e
->flags
);
329 static void vring_packed_off_wrap_write(VirtIODevice
*vdev
,
330 MemoryRegionCache
*cache
,
333 hwaddr off
= offsetof(VRingPackedDescEvent
, off_wrap
);
335 virtio_stw_phys_cached(vdev
, cache
, off
, off_wrap
);
336 address_space_cache_invalidate(cache
, off
, sizeof(off_wrap
));
339 static void vring_packed_flags_write(VirtIODevice
*vdev
,
340 MemoryRegionCache
*cache
, uint16_t flags
)
342 hwaddr off
= offsetof(VRingPackedDescEvent
, flags
);
344 virtio_stw_phys_cached(vdev
, cache
, off
, flags
);
345 address_space_cache_invalidate(cache
, off
, sizeof(flags
));
348 /* Called within rcu_read_lock(). */
349 static VRingMemoryRegionCaches
*vring_get_region_caches(struct VirtQueue
*vq
)
351 return qatomic_rcu_read(&vq
->vring
.caches
);
354 /* Called within rcu_read_lock(). */
355 static inline uint16_t vring_avail_flags(VirtQueue
*vq
)
357 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
358 hwaddr pa
= offsetof(VRingAvail
, flags
);
364 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
367 /* Called within rcu_read_lock(). */
368 static inline uint16_t vring_avail_idx(VirtQueue
*vq
)
370 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
371 hwaddr pa
= offsetof(VRingAvail
, idx
);
377 vq
->shadow_avail_idx
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
378 return vq
->shadow_avail_idx
;
381 /* Called within rcu_read_lock(). */
382 static inline uint16_t vring_avail_ring(VirtQueue
*vq
, int i
)
384 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
385 hwaddr pa
= offsetof(VRingAvail
, ring
[i
]);
391 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->avail
, pa
);
394 /* Called within rcu_read_lock(). */
395 static inline uint16_t vring_get_used_event(VirtQueue
*vq
)
397 return vring_avail_ring(vq
, vq
->vring
.num
);
400 /* Called within rcu_read_lock(). */
401 static inline void vring_used_write(VirtQueue
*vq
, VRingUsedElem
*uelem
,
404 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
405 hwaddr pa
= offsetof(VRingUsed
, ring
[i
]);
411 virtio_tswap32s(vq
->vdev
, &uelem
->id
);
412 virtio_tswap32s(vq
->vdev
, &uelem
->len
);
413 address_space_write_cached(&caches
->used
, pa
, uelem
, sizeof(VRingUsedElem
));
414 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(VRingUsedElem
));
417 /* Called within rcu_read_lock(). */
418 static inline uint16_t vring_used_flags(VirtQueue
*vq
)
420 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
421 hwaddr pa
= offsetof(VRingUsed
, flags
);
427 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
430 /* Called within rcu_read_lock(). */
431 static uint16_t vring_used_idx(VirtQueue
*vq
)
433 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
434 hwaddr pa
= offsetof(VRingUsed
, idx
);
440 return virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
443 /* Called within rcu_read_lock(). */
444 static inline void vring_used_idx_set(VirtQueue
*vq
, uint16_t val
)
446 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
447 hwaddr pa
= offsetof(VRingUsed
, idx
);
450 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
451 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
457 /* Called within rcu_read_lock(). */
458 static inline void vring_used_flags_set_bit(VirtQueue
*vq
, int mask
)
460 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
461 VirtIODevice
*vdev
= vq
->vdev
;
462 hwaddr pa
= offsetof(VRingUsed
, flags
);
469 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
470 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
| mask
);
471 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
474 /* Called within rcu_read_lock(). */
475 static inline void vring_used_flags_unset_bit(VirtQueue
*vq
, int mask
)
477 VRingMemoryRegionCaches
*caches
= vring_get_region_caches(vq
);
478 VirtIODevice
*vdev
= vq
->vdev
;
479 hwaddr pa
= offsetof(VRingUsed
, flags
);
486 flags
= virtio_lduw_phys_cached(vq
->vdev
, &caches
->used
, pa
);
487 virtio_stw_phys_cached(vdev
, &caches
->used
, pa
, flags
& ~mask
);
488 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(flags
));
491 /* Called within rcu_read_lock(). */
492 static inline void vring_set_avail_event(VirtQueue
*vq
, uint16_t val
)
494 VRingMemoryRegionCaches
*caches
;
496 if (!vq
->notification
) {
500 caches
= vring_get_region_caches(vq
);
505 pa
= offsetof(VRingUsed
, ring
[vq
->vring
.num
]);
506 virtio_stw_phys_cached(vq
->vdev
, &caches
->used
, pa
, val
);
507 address_space_cache_invalidate(&caches
->used
, pa
, sizeof(val
));
510 static void virtio_queue_split_set_notification(VirtQueue
*vq
, int enable
)
512 RCU_READ_LOCK_GUARD();
514 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
515 vring_set_avail_event(vq
, vring_avail_idx(vq
));
517 vring_used_flags_unset_bit(vq
, VRING_USED_F_NO_NOTIFY
);
519 vring_used_flags_set_bit(vq
, VRING_USED_F_NO_NOTIFY
);
522 /* Expose avail event/used flags before caller checks the avail idx. */
527 static void virtio_queue_packed_set_notification(VirtQueue
*vq
, int enable
)
530 VRingPackedDescEvent e
;
531 VRingMemoryRegionCaches
*caches
;
533 RCU_READ_LOCK_GUARD();
534 caches
= vring_get_region_caches(vq
);
539 vring_packed_event_read(vq
->vdev
, &caches
->used
, &e
);
542 e
.flags
= VRING_PACKED_EVENT_FLAG_DISABLE
;
543 } else if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
544 off_wrap
= vq
->shadow_avail_idx
| vq
->shadow_avail_wrap_counter
<< 15;
545 vring_packed_off_wrap_write(vq
->vdev
, &caches
->used
, off_wrap
);
546 /* Make sure off_wrap is wrote before flags */
548 e
.flags
= VRING_PACKED_EVENT_FLAG_DESC
;
550 e
.flags
= VRING_PACKED_EVENT_FLAG_ENABLE
;
553 vring_packed_flags_write(vq
->vdev
, &caches
->used
, e
.flags
);
555 /* Expose avail event/used flags before caller checks the avail idx. */
560 bool virtio_queue_get_notification(VirtQueue
*vq
)
562 return vq
->notification
;
565 void virtio_queue_set_notification(VirtQueue
*vq
, int enable
)
567 vq
->notification
= enable
;
569 if (!vq
->vring
.desc
) {
573 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
574 virtio_queue_packed_set_notification(vq
, enable
);
576 virtio_queue_split_set_notification(vq
, enable
);
580 int virtio_queue_ready(VirtQueue
*vq
)
582 return vq
->vring
.avail
!= 0;
585 static void vring_packed_desc_read_flags(VirtIODevice
*vdev
,
587 MemoryRegionCache
*cache
,
590 hwaddr off
= i
* sizeof(VRingPackedDesc
) + offsetof(VRingPackedDesc
, flags
);
592 *flags
= virtio_lduw_phys_cached(vdev
, cache
, off
);
595 static void vring_packed_desc_read(VirtIODevice
*vdev
,
596 VRingPackedDesc
*desc
,
597 MemoryRegionCache
*cache
,
598 int i
, bool strict_order
)
600 hwaddr off
= i
* sizeof(VRingPackedDesc
);
602 vring_packed_desc_read_flags(vdev
, &desc
->flags
, cache
, i
);
605 /* Make sure flags is read before the rest fields. */
609 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, addr
),
610 &desc
->addr
, sizeof(desc
->addr
));
611 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, id
),
612 &desc
->id
, sizeof(desc
->id
));
613 address_space_read_cached(cache
, off
+ offsetof(VRingPackedDesc
, len
),
614 &desc
->len
, sizeof(desc
->len
));
615 virtio_tswap64s(vdev
, &desc
->addr
);
616 virtio_tswap16s(vdev
, &desc
->id
);
617 virtio_tswap32s(vdev
, &desc
->len
);
620 static void vring_packed_desc_write_data(VirtIODevice
*vdev
,
621 VRingPackedDesc
*desc
,
622 MemoryRegionCache
*cache
,
625 hwaddr off_id
= i
* sizeof(VRingPackedDesc
) +
626 offsetof(VRingPackedDesc
, id
);
627 hwaddr off_len
= i
* sizeof(VRingPackedDesc
) +
628 offsetof(VRingPackedDesc
, len
);
630 virtio_tswap32s(vdev
, &desc
->len
);
631 virtio_tswap16s(vdev
, &desc
->id
);
632 address_space_write_cached(cache
, off_id
, &desc
->id
, sizeof(desc
->id
));
633 address_space_cache_invalidate(cache
, off_id
, sizeof(desc
->id
));
634 address_space_write_cached(cache
, off_len
, &desc
->len
, sizeof(desc
->len
));
635 address_space_cache_invalidate(cache
, off_len
, sizeof(desc
->len
));
638 static void vring_packed_desc_write_flags(VirtIODevice
*vdev
,
639 VRingPackedDesc
*desc
,
640 MemoryRegionCache
*cache
,
643 hwaddr off
= i
* sizeof(VRingPackedDesc
) + offsetof(VRingPackedDesc
, flags
);
645 virtio_stw_phys_cached(vdev
, cache
, off
, desc
->flags
);
646 address_space_cache_invalidate(cache
, off
, sizeof(desc
->flags
));
649 static void vring_packed_desc_write(VirtIODevice
*vdev
,
650 VRingPackedDesc
*desc
,
651 MemoryRegionCache
*cache
,
652 int i
, bool strict_order
)
654 vring_packed_desc_write_data(vdev
, desc
, cache
, i
);
656 /* Make sure data is wrote before flags. */
659 vring_packed_desc_write_flags(vdev
, desc
, cache
, i
);
662 static inline bool is_desc_avail(uint16_t flags
, bool wrap_counter
)
666 avail
= !!(flags
& (1 << VRING_PACKED_DESC_F_AVAIL
));
667 used
= !!(flags
& (1 << VRING_PACKED_DESC_F_USED
));
668 return (avail
!= used
) && (avail
== wrap_counter
);
671 /* Fetch avail_idx from VQ memory only when we really need to know if
672 * guest has added some buffers.
673 * Called within rcu_read_lock(). */
674 static int virtio_queue_empty_rcu(VirtQueue
*vq
)
676 if (virtio_device_disabled(vq
->vdev
)) {
680 if (unlikely(!vq
->vring
.avail
)) {
684 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
688 return vring_avail_idx(vq
) == vq
->last_avail_idx
;
691 static int virtio_queue_split_empty(VirtQueue
*vq
)
695 if (virtio_device_disabled(vq
->vdev
)) {
699 if (unlikely(!vq
->vring
.avail
)) {
703 if (vq
->shadow_avail_idx
!= vq
->last_avail_idx
) {
707 RCU_READ_LOCK_GUARD();
708 empty
= vring_avail_idx(vq
) == vq
->last_avail_idx
;
712 /* Called within rcu_read_lock(). */
713 static int virtio_queue_packed_empty_rcu(VirtQueue
*vq
)
715 struct VRingPackedDesc desc
;
716 VRingMemoryRegionCaches
*cache
;
718 if (unlikely(!vq
->vring
.desc
)) {
722 cache
= vring_get_region_caches(vq
);
727 vring_packed_desc_read_flags(vq
->vdev
, &desc
.flags
, &cache
->desc
,
730 return !is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
);
733 static int virtio_queue_packed_empty(VirtQueue
*vq
)
735 RCU_READ_LOCK_GUARD();
736 return virtio_queue_packed_empty_rcu(vq
);
739 int virtio_queue_empty(VirtQueue
*vq
)
741 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
742 return virtio_queue_packed_empty(vq
);
744 return virtio_queue_split_empty(vq
);
748 static void virtqueue_unmap_sg(VirtQueue
*vq
, const VirtQueueElement
*elem
,
751 AddressSpace
*dma_as
= vq
->vdev
->dma_as
;
756 for (i
= 0; i
< elem
->in_num
; i
++) {
757 size_t size
= MIN(len
- offset
, elem
->in_sg
[i
].iov_len
);
759 dma_memory_unmap(dma_as
, elem
->in_sg
[i
].iov_base
,
760 elem
->in_sg
[i
].iov_len
,
761 DMA_DIRECTION_FROM_DEVICE
, size
);
766 for (i
= 0; i
< elem
->out_num
; i
++)
767 dma_memory_unmap(dma_as
, elem
->out_sg
[i
].iov_base
,
768 elem
->out_sg
[i
].iov_len
,
769 DMA_DIRECTION_TO_DEVICE
,
770 elem
->out_sg
[i
].iov_len
);
773 /* virtqueue_detach_element:
774 * @vq: The #VirtQueue
775 * @elem: The #VirtQueueElement
776 * @len: number of bytes written
778 * Detach the element from the virtqueue. This function is suitable for device
779 * reset or other situations where a #VirtQueueElement is simply freed and will
780 * not be pushed or discarded.
782 void virtqueue_detach_element(VirtQueue
*vq
, const VirtQueueElement
*elem
,
785 vq
->inuse
-= elem
->ndescs
;
786 virtqueue_unmap_sg(vq
, elem
, len
);
789 static void virtqueue_split_rewind(VirtQueue
*vq
, unsigned int num
)
791 vq
->last_avail_idx
-= num
;
794 static void virtqueue_packed_rewind(VirtQueue
*vq
, unsigned int num
)
796 if (vq
->last_avail_idx
< num
) {
797 vq
->last_avail_idx
= vq
->vring
.num
+ vq
->last_avail_idx
- num
;
798 vq
->last_avail_wrap_counter
^= 1;
800 vq
->last_avail_idx
-= num
;
805 * @vq: The #VirtQueue
806 * @elem: The #VirtQueueElement
807 * @len: number of bytes written
809 * Pretend the most recent element wasn't popped from the virtqueue. The next
810 * call to virtqueue_pop() will refetch the element.
812 void virtqueue_unpop(VirtQueue
*vq
, const VirtQueueElement
*elem
,
816 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
817 virtqueue_packed_rewind(vq
, 1);
819 virtqueue_split_rewind(vq
, 1);
822 virtqueue_detach_element(vq
, elem
, len
);
826 * @vq: The #VirtQueue
827 * @num: Number of elements to push back
829 * Pretend that elements weren't popped from the virtqueue. The next
830 * virtqueue_pop() will refetch the oldest element.
832 * Use virtqueue_unpop() instead if you have a VirtQueueElement.
834 * Returns: true on success, false if @num is greater than the number of in use
837 bool virtqueue_rewind(VirtQueue
*vq
, unsigned int num
)
839 if (num
> vq
->inuse
) {
844 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
845 virtqueue_packed_rewind(vq
, num
);
847 virtqueue_split_rewind(vq
, num
);
852 static void virtqueue_split_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
853 unsigned int len
, unsigned int idx
)
857 if (unlikely(!vq
->vring
.used
)) {
861 idx
= (idx
+ vq
->used_idx
) % vq
->vring
.num
;
863 uelem
.id
= elem
->index
;
865 vring_used_write(vq
, &uelem
, idx
);
868 static void virtqueue_packed_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
869 unsigned int len
, unsigned int idx
)
871 vq
->used_elems
[idx
].index
= elem
->index
;
872 vq
->used_elems
[idx
].len
= len
;
873 vq
->used_elems
[idx
].ndescs
= elem
->ndescs
;
876 static void virtqueue_packed_fill_desc(VirtQueue
*vq
,
877 const VirtQueueElement
*elem
,
882 VRingMemoryRegionCaches
*caches
;
883 VRingPackedDesc desc
= {
887 bool wrap_counter
= vq
->used_wrap_counter
;
889 if (unlikely(!vq
->vring
.desc
)) {
893 head
= vq
->used_idx
+ idx
;
894 if (head
>= vq
->vring
.num
) {
895 head
-= vq
->vring
.num
;
899 desc
.flags
|= (1 << VRING_PACKED_DESC_F_AVAIL
);
900 desc
.flags
|= (1 << VRING_PACKED_DESC_F_USED
);
902 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_AVAIL
);
903 desc
.flags
&= ~(1 << VRING_PACKED_DESC_F_USED
);
906 caches
= vring_get_region_caches(vq
);
911 vring_packed_desc_write(vq
->vdev
, &desc
, &caches
->desc
, head
, strict_order
);
914 /* Called within rcu_read_lock(). */
915 void virtqueue_fill(VirtQueue
*vq
, const VirtQueueElement
*elem
,
916 unsigned int len
, unsigned int idx
)
918 trace_virtqueue_fill(vq
, elem
, len
, idx
);
920 virtqueue_unmap_sg(vq
, elem
, len
);
922 if (virtio_device_disabled(vq
->vdev
)) {
926 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
927 virtqueue_packed_fill(vq
, elem
, len
, idx
);
929 virtqueue_split_fill(vq
, elem
, len
, idx
);
933 /* Called within rcu_read_lock(). */
934 static void virtqueue_split_flush(VirtQueue
*vq
, unsigned int count
)
938 if (unlikely(!vq
->vring
.used
)) {
942 /* Make sure buffer is written before we update index. */
944 trace_virtqueue_flush(vq
, count
);
947 vring_used_idx_set(vq
, new);
949 if (unlikely((int16_t)(new - vq
->signalled_used
) < (uint16_t)(new - old
)))
950 vq
->signalled_used_valid
= false;
953 static void virtqueue_packed_flush(VirtQueue
*vq
, unsigned int count
)
955 unsigned int i
, ndescs
= 0;
957 if (unlikely(!vq
->vring
.desc
)) {
961 for (i
= 1; i
< count
; i
++) {
962 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[i
], i
, false);
963 ndescs
+= vq
->used_elems
[i
].ndescs
;
965 virtqueue_packed_fill_desc(vq
, &vq
->used_elems
[0], 0, true);
966 ndescs
+= vq
->used_elems
[0].ndescs
;
969 vq
->used_idx
+= ndescs
;
970 if (vq
->used_idx
>= vq
->vring
.num
) {
971 vq
->used_idx
-= vq
->vring
.num
;
972 vq
->used_wrap_counter
^= 1;
973 vq
->signalled_used_valid
= false;
977 void virtqueue_flush(VirtQueue
*vq
, unsigned int count
)
979 if (virtio_device_disabled(vq
->vdev
)) {
984 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
985 virtqueue_packed_flush(vq
, count
);
987 virtqueue_split_flush(vq
, count
);
991 void virtqueue_push(VirtQueue
*vq
, const VirtQueueElement
*elem
,
994 RCU_READ_LOCK_GUARD();
995 virtqueue_fill(vq
, elem
, len
, 0);
996 virtqueue_flush(vq
, 1);
999 /* Called within rcu_read_lock(). */
1000 static int virtqueue_num_heads(VirtQueue
*vq
, unsigned int idx
)
1002 uint16_t num_heads
= vring_avail_idx(vq
) - idx
;
1004 /* Check it isn't doing very strange things with descriptor numbers. */
1005 if (num_heads
> vq
->vring
.num
) {
1006 virtio_error(vq
->vdev
, "Guest moved used index from %u to %u",
1007 idx
, vq
->shadow_avail_idx
);
1010 /* On success, callers read a descriptor at vq->last_avail_idx.
1011 * Make sure descriptor read does not bypass avail index read. */
1019 /* Called within rcu_read_lock(). */
1020 static bool virtqueue_get_head(VirtQueue
*vq
, unsigned int idx
,
1023 /* Grab the next descriptor number they're advertising, and increment
1024 * the index we've seen. */
1025 *head
= vring_avail_ring(vq
, idx
% vq
->vring
.num
);
1027 /* If their number is silly, that's a fatal mistake. */
1028 if (*head
>= vq
->vring
.num
) {
1029 virtio_error(vq
->vdev
, "Guest says index %u is available", *head
);
1037 VIRTQUEUE_READ_DESC_ERROR
= -1,
1038 VIRTQUEUE_READ_DESC_DONE
= 0, /* end of chain */
1039 VIRTQUEUE_READ_DESC_MORE
= 1, /* more buffers in chain */
1042 static int virtqueue_split_read_next_desc(VirtIODevice
*vdev
, VRingDesc
*desc
,
1043 MemoryRegionCache
*desc_cache
,
1044 unsigned int max
, unsigned int *next
)
1046 /* If this descriptor says it doesn't chain, we're done. */
1047 if (!(desc
->flags
& VRING_DESC_F_NEXT
)) {
1048 return VIRTQUEUE_READ_DESC_DONE
;
1051 /* Check they're not leading us off end of descriptors. */
1053 /* Make sure compiler knows to grab that: we don't want it changing! */
1057 virtio_error(vdev
, "Desc next is %u", *next
);
1058 return VIRTQUEUE_READ_DESC_ERROR
;
1061 vring_split_desc_read(vdev
, desc
, desc_cache
, *next
);
1062 return VIRTQUEUE_READ_DESC_MORE
;
1065 /* Called within rcu_read_lock(). */
1066 static void virtqueue_split_get_avail_bytes(VirtQueue
*vq
,
1067 unsigned int *in_bytes
, unsigned int *out_bytes
,
1068 unsigned max_in_bytes
, unsigned max_out_bytes
,
1069 VRingMemoryRegionCaches
*caches
)
1071 VirtIODevice
*vdev
= vq
->vdev
;
1073 unsigned int total_bufs
, in_total
, out_total
;
1074 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1078 idx
= vq
->last_avail_idx
;
1079 total_bufs
= in_total
= out_total
= 0;
1081 while ((rc
= virtqueue_num_heads(vq
, idx
)) > 0) {
1082 MemoryRegionCache
*desc_cache
= &caches
->desc
;
1083 unsigned int num_bufs
;
1086 unsigned int max
= vq
->vring
.num
;
1088 num_bufs
= total_bufs
;
1090 if (!virtqueue_get_head(vq
, idx
++, &i
)) {
1094 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1096 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1097 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1098 virtio_error(vdev
, "Invalid size for indirect buffer table");
1102 /* If we've got too many, that implies a descriptor loop. */
1103 if (num_bufs
>= max
) {
1104 virtio_error(vdev
, "Looped descriptor");
1108 /* loop over the indirect descriptor table */
1109 len
= address_space_cache_init(&indirect_desc_cache
,
1111 desc
.addr
, desc
.len
, false);
1112 desc_cache
= &indirect_desc_cache
;
1113 if (len
< desc
.len
) {
1114 virtio_error(vdev
, "Cannot map indirect buffer");
1118 max
= desc
.len
/ sizeof(VRingDesc
);
1120 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1124 /* If we've got too many, that implies a descriptor loop. */
1125 if (++num_bufs
> max
) {
1126 virtio_error(vdev
, "Looped descriptor");
1130 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1131 in_total
+= desc
.len
;
1133 out_total
+= desc
.len
;
1135 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1139 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1140 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1142 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1146 if (desc_cache
== &indirect_desc_cache
) {
1147 address_space_cache_destroy(&indirect_desc_cache
);
1150 total_bufs
= num_bufs
;
1159 address_space_cache_destroy(&indirect_desc_cache
);
1161 *in_bytes
= in_total
;
1164 *out_bytes
= out_total
;
1169 in_total
= out_total
= 0;
1173 static int virtqueue_packed_read_next_desc(VirtQueue
*vq
,
1174 VRingPackedDesc
*desc
,
1181 /* If this descriptor says it doesn't chain, we're done. */
1182 if (!indirect
&& !(desc
->flags
& VRING_DESC_F_NEXT
)) {
1183 return VIRTQUEUE_READ_DESC_DONE
;
1189 return VIRTQUEUE_READ_DESC_DONE
;
1191 (*next
) -= vq
->vring
.num
;
1195 vring_packed_desc_read(vq
->vdev
, desc
, desc_cache
, *next
, false);
1196 return VIRTQUEUE_READ_DESC_MORE
;
1199 /* Called within rcu_read_lock(). */
1200 static void virtqueue_packed_get_avail_bytes(VirtQueue
*vq
,
1201 unsigned int *in_bytes
,
1202 unsigned int *out_bytes
,
1203 unsigned max_in_bytes
,
1204 unsigned max_out_bytes
,
1205 VRingMemoryRegionCaches
*caches
)
1207 VirtIODevice
*vdev
= vq
->vdev
;
1209 unsigned int total_bufs
, in_total
, out_total
;
1210 MemoryRegionCache
*desc_cache
;
1211 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1213 VRingPackedDesc desc
;
1216 idx
= vq
->last_avail_idx
;
1217 wrap_counter
= vq
->last_avail_wrap_counter
;
1218 total_bufs
= in_total
= out_total
= 0;
1221 unsigned int num_bufs
= total_bufs
;
1222 unsigned int i
= idx
;
1224 unsigned int max
= vq
->vring
.num
;
1226 desc_cache
= &caches
->desc
;
1228 vring_packed_desc_read(vdev
, &desc
, desc_cache
, idx
, true);
1229 if (!is_desc_avail(desc
.flags
, wrap_counter
)) {
1233 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1234 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1235 virtio_error(vdev
, "Invalid size for indirect buffer table");
1239 /* If we've got too many, that implies a descriptor loop. */
1240 if (num_bufs
>= max
) {
1241 virtio_error(vdev
, "Looped descriptor");
1245 /* loop over the indirect descriptor table */
1246 len
= address_space_cache_init(&indirect_desc_cache
,
1248 desc
.addr
, desc
.len
, false);
1249 desc_cache
= &indirect_desc_cache
;
1250 if (len
< desc
.len
) {
1251 virtio_error(vdev
, "Cannot map indirect buffer");
1255 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1257 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1261 /* If we've got too many, that implies a descriptor loop. */
1262 if (++num_bufs
> max
) {
1263 virtio_error(vdev
, "Looped descriptor");
1267 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1268 in_total
+= desc
.len
;
1270 out_total
+= desc
.len
;
1272 if (in_total
>= max_in_bytes
&& out_total
>= max_out_bytes
) {
1276 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
,
1278 &indirect_desc_cache
);
1279 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1281 if (desc_cache
== &indirect_desc_cache
) {
1282 address_space_cache_destroy(&indirect_desc_cache
);
1286 idx
+= num_bufs
- total_bufs
;
1287 total_bufs
= num_bufs
;
1290 if (idx
>= vq
->vring
.num
) {
1291 idx
-= vq
->vring
.num
;
1296 /* Record the index and wrap counter for a kick we want */
1297 vq
->shadow_avail_idx
= idx
;
1298 vq
->shadow_avail_wrap_counter
= wrap_counter
;
1300 address_space_cache_destroy(&indirect_desc_cache
);
1302 *in_bytes
= in_total
;
1305 *out_bytes
= out_total
;
1310 in_total
= out_total
= 0;
1314 void virtqueue_get_avail_bytes(VirtQueue
*vq
, unsigned int *in_bytes
,
1315 unsigned int *out_bytes
,
1316 unsigned max_in_bytes
, unsigned max_out_bytes
)
1319 VRingMemoryRegionCaches
*caches
;
1321 RCU_READ_LOCK_GUARD();
1323 if (unlikely(!vq
->vring
.desc
)) {
1327 caches
= vring_get_region_caches(vq
);
1332 desc_size
= virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
) ?
1333 sizeof(VRingPackedDesc
) : sizeof(VRingDesc
);
1334 if (caches
->desc
.len
< vq
->vring
.num
* desc_size
) {
1335 virtio_error(vq
->vdev
, "Cannot map descriptor ring");
1339 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1340 virtqueue_packed_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1341 max_in_bytes
, max_out_bytes
,
1344 virtqueue_split_get_avail_bytes(vq
, in_bytes
, out_bytes
,
1345 max_in_bytes
, max_out_bytes
,
1359 int virtqueue_avail_bytes(VirtQueue
*vq
, unsigned int in_bytes
,
1360 unsigned int out_bytes
)
1362 unsigned int in_total
, out_total
;
1364 virtqueue_get_avail_bytes(vq
, &in_total
, &out_total
, in_bytes
, out_bytes
);
1365 return in_bytes
<= in_total
&& out_bytes
<= out_total
;
1368 static bool virtqueue_map_desc(VirtIODevice
*vdev
, unsigned int *p_num_sg
,
1369 hwaddr
*addr
, struct iovec
*iov
,
1370 unsigned int max_num_sg
, bool is_write
,
1371 hwaddr pa
, size_t sz
)
1374 unsigned num_sg
= *p_num_sg
;
1375 assert(num_sg
<= max_num_sg
);
1378 virtio_error(vdev
, "virtio: zero sized buffers are not allowed");
1385 if (num_sg
== max_num_sg
) {
1386 virtio_error(vdev
, "virtio: too many write descriptors in "
1391 iov
[num_sg
].iov_base
= dma_memory_map(vdev
->dma_as
, pa
, &len
,
1393 DMA_DIRECTION_FROM_DEVICE
:
1394 DMA_DIRECTION_TO_DEVICE
,
1395 MEMTXATTRS_UNSPECIFIED
);
1396 if (!iov
[num_sg
].iov_base
) {
1397 virtio_error(vdev
, "virtio: bogus descriptor or out of resources");
1401 iov
[num_sg
].iov_len
= len
;
1415 /* Only used by error code paths before we have a VirtQueueElement (therefore
1416 * virtqueue_unmap_sg() can't be used). Assumes buffers weren't written to
1419 static void virtqueue_undo_map_desc(unsigned int out_num
, unsigned int in_num
,
1424 for (i
= 0; i
< out_num
+ in_num
; i
++) {
1425 int is_write
= i
>= out_num
;
1427 cpu_physical_memory_unmap(iov
->iov_base
, iov
->iov_len
, is_write
, 0);
1432 static void virtqueue_map_iovec(VirtIODevice
*vdev
, struct iovec
*sg
,
1433 hwaddr
*addr
, unsigned int num_sg
,
1439 for (i
= 0; i
< num_sg
; i
++) {
1440 len
= sg
[i
].iov_len
;
1441 sg
[i
].iov_base
= dma_memory_map(vdev
->dma_as
,
1442 addr
[i
], &len
, is_write
?
1443 DMA_DIRECTION_FROM_DEVICE
:
1444 DMA_DIRECTION_TO_DEVICE
,
1445 MEMTXATTRS_UNSPECIFIED
);
1446 if (!sg
[i
].iov_base
) {
1447 error_report("virtio: error trying to map MMIO memory");
1450 if (len
!= sg
[i
].iov_len
) {
1451 error_report("virtio: unexpected memory split");
1457 void virtqueue_map(VirtIODevice
*vdev
, VirtQueueElement
*elem
)
1459 virtqueue_map_iovec(vdev
, elem
->in_sg
, elem
->in_addr
, elem
->in_num
, true);
1460 virtqueue_map_iovec(vdev
, elem
->out_sg
, elem
->out_addr
, elem
->out_num
,
1464 static void *virtqueue_alloc_element(size_t sz
, unsigned out_num
, unsigned in_num
)
1466 VirtQueueElement
*elem
;
1467 size_t in_addr_ofs
= QEMU_ALIGN_UP(sz
, __alignof__(elem
->in_addr
[0]));
1468 size_t out_addr_ofs
= in_addr_ofs
+ in_num
* sizeof(elem
->in_addr
[0]);
1469 size_t out_addr_end
= out_addr_ofs
+ out_num
* sizeof(elem
->out_addr
[0]);
1470 size_t in_sg_ofs
= QEMU_ALIGN_UP(out_addr_end
, __alignof__(elem
->in_sg
[0]));
1471 size_t out_sg_ofs
= in_sg_ofs
+ in_num
* sizeof(elem
->in_sg
[0]);
1472 size_t out_sg_end
= out_sg_ofs
+ out_num
* sizeof(elem
->out_sg
[0]);
1474 assert(sz
>= sizeof(VirtQueueElement
));
1475 elem
= g_malloc(out_sg_end
);
1476 trace_virtqueue_alloc_element(elem
, sz
, in_num
, out_num
);
1477 elem
->out_num
= out_num
;
1478 elem
->in_num
= in_num
;
1479 elem
->in_addr
= (void *)elem
+ in_addr_ofs
;
1480 elem
->out_addr
= (void *)elem
+ out_addr_ofs
;
1481 elem
->in_sg
= (void *)elem
+ in_sg_ofs
;
1482 elem
->out_sg
= (void *)elem
+ out_sg_ofs
;
1486 static void *virtqueue_split_pop(VirtQueue
*vq
, size_t sz
)
1488 unsigned int i
, head
, max
;
1489 VRingMemoryRegionCaches
*caches
;
1490 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1491 MemoryRegionCache
*desc_cache
;
1493 VirtIODevice
*vdev
= vq
->vdev
;
1494 VirtQueueElement
*elem
= NULL
;
1495 unsigned out_num
, in_num
, elem_entries
;
1496 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1497 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1501 RCU_READ_LOCK_GUARD();
1502 if (virtio_queue_empty_rcu(vq
)) {
1505 /* Needed after virtio_queue_empty(), see comment in
1506 * virtqueue_num_heads(). */
1509 /* When we start there are none of either input nor output. */
1510 out_num
= in_num
= elem_entries
= 0;
1512 max
= vq
->vring
.num
;
1514 if (vq
->inuse
>= vq
->vring
.num
) {
1515 virtio_error(vdev
, "Virtqueue size exceeded");
1519 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
++, &head
)) {
1523 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
1524 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1529 caches
= vring_get_region_caches(vq
);
1531 virtio_error(vdev
, "Region caches not initialized");
1535 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1536 virtio_error(vdev
, "Cannot map descriptor ring");
1540 desc_cache
= &caches
->desc
;
1541 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1542 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1543 if (!desc
.len
|| (desc
.len
% sizeof(VRingDesc
))) {
1544 virtio_error(vdev
, "Invalid size for indirect buffer table");
1548 /* loop over the indirect descriptor table */
1549 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1550 desc
.addr
, desc
.len
, false);
1551 desc_cache
= &indirect_desc_cache
;
1552 if (len
< desc
.len
) {
1553 virtio_error(vdev
, "Cannot map indirect buffer");
1557 max
= desc
.len
/ sizeof(VRingDesc
);
1559 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
1562 /* Collect all the descriptors */
1566 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1567 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1569 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1570 desc
.addr
, desc
.len
);
1573 virtio_error(vdev
, "Incorrect order for descriptors");
1576 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1577 VIRTQUEUE_MAX_SIZE
, false,
1578 desc
.addr
, desc
.len
);
1584 /* If we've got too many, that implies a descriptor loop. */
1585 if (++elem_entries
> max
) {
1586 virtio_error(vdev
, "Looped descriptor");
1590 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
, max
, &i
);
1591 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1593 if (rc
== VIRTQUEUE_READ_DESC_ERROR
) {
1597 /* Now copy what we have collected and mapped */
1598 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1601 for (i
= 0; i
< out_num
; i
++) {
1602 elem
->out_addr
[i
] = addr
[i
];
1603 elem
->out_sg
[i
] = iov
[i
];
1605 for (i
= 0; i
< in_num
; i
++) {
1606 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1607 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1612 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1614 address_space_cache_destroy(&indirect_desc_cache
);
1619 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1623 static void *virtqueue_packed_pop(VirtQueue
*vq
, size_t sz
)
1625 unsigned int i
, max
;
1626 VRingMemoryRegionCaches
*caches
;
1627 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
1628 MemoryRegionCache
*desc_cache
;
1630 VirtIODevice
*vdev
= vq
->vdev
;
1631 VirtQueueElement
*elem
= NULL
;
1632 unsigned out_num
, in_num
, elem_entries
;
1633 hwaddr addr
[VIRTQUEUE_MAX_SIZE
];
1634 struct iovec iov
[VIRTQUEUE_MAX_SIZE
];
1635 VRingPackedDesc desc
;
1639 RCU_READ_LOCK_GUARD();
1640 if (virtio_queue_packed_empty_rcu(vq
)) {
1644 /* When we start there are none of either input nor output. */
1645 out_num
= in_num
= elem_entries
= 0;
1647 max
= vq
->vring
.num
;
1649 if (vq
->inuse
>= vq
->vring
.num
) {
1650 virtio_error(vdev
, "Virtqueue size exceeded");
1654 i
= vq
->last_avail_idx
;
1656 caches
= vring_get_region_caches(vq
);
1658 virtio_error(vdev
, "Region caches not initialized");
1662 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
1663 virtio_error(vdev
, "Cannot map descriptor ring");
1667 desc_cache
= &caches
->desc
;
1668 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, true);
1670 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
1671 if (desc
.len
% sizeof(VRingPackedDesc
)) {
1672 virtio_error(vdev
, "Invalid size for indirect buffer table");
1676 /* loop over the indirect descriptor table */
1677 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
1678 desc
.addr
, desc
.len
, false);
1679 desc_cache
= &indirect_desc_cache
;
1680 if (len
< desc
.len
) {
1681 virtio_error(vdev
, "Cannot map indirect buffer");
1685 max
= desc
.len
/ sizeof(VRingPackedDesc
);
1687 vring_packed_desc_read(vdev
, &desc
, desc_cache
, i
, false);
1690 /* Collect all the descriptors */
1694 if (desc
.flags
& VRING_DESC_F_WRITE
) {
1695 map_ok
= virtqueue_map_desc(vdev
, &in_num
, addr
+ out_num
,
1697 VIRTQUEUE_MAX_SIZE
- out_num
, true,
1698 desc
.addr
, desc
.len
);
1701 virtio_error(vdev
, "Incorrect order for descriptors");
1704 map_ok
= virtqueue_map_desc(vdev
, &out_num
, addr
, iov
,
1705 VIRTQUEUE_MAX_SIZE
, false,
1706 desc
.addr
, desc
.len
);
1712 /* If we've got too many, that implies a descriptor loop. */
1713 if (++elem_entries
> max
) {
1714 virtio_error(vdev
, "Looped descriptor");
1718 rc
= virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
, max
, &i
,
1720 &indirect_desc_cache
);
1721 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
1723 /* Now copy what we have collected and mapped */
1724 elem
= virtqueue_alloc_element(sz
, out_num
, in_num
);
1725 for (i
= 0; i
< out_num
; i
++) {
1726 elem
->out_addr
[i
] = addr
[i
];
1727 elem
->out_sg
[i
] = iov
[i
];
1729 for (i
= 0; i
< in_num
; i
++) {
1730 elem
->in_addr
[i
] = addr
[out_num
+ i
];
1731 elem
->in_sg
[i
] = iov
[out_num
+ i
];
1735 elem
->ndescs
= (desc_cache
== &indirect_desc_cache
) ? 1 : elem_entries
;
1736 vq
->last_avail_idx
+= elem
->ndescs
;
1737 vq
->inuse
+= 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;
1744 vq
->shadow_avail_idx
= vq
->last_avail_idx
;
1745 vq
->shadow_avail_wrap_counter
= vq
->last_avail_wrap_counter
;
1747 trace_virtqueue_pop(vq
, elem
, elem
->in_num
, elem
->out_num
);
1749 address_space_cache_destroy(&indirect_desc_cache
);
1754 virtqueue_undo_map_desc(out_num
, in_num
, iov
);
1758 void *virtqueue_pop(VirtQueue
*vq
, size_t sz
)
1760 if (virtio_device_disabled(vq
->vdev
)) {
1764 if (virtio_vdev_has_feature(vq
->vdev
, VIRTIO_F_RING_PACKED
)) {
1765 return virtqueue_packed_pop(vq
, sz
);
1767 return virtqueue_split_pop(vq
, sz
);
1771 static unsigned int virtqueue_packed_drop_all(VirtQueue
*vq
)
1773 VRingMemoryRegionCaches
*caches
;
1774 MemoryRegionCache
*desc_cache
;
1775 unsigned int dropped
= 0;
1776 VirtQueueElement elem
= {};
1777 VirtIODevice
*vdev
= vq
->vdev
;
1778 VRingPackedDesc desc
;
1780 RCU_READ_LOCK_GUARD();
1782 caches
= vring_get_region_caches(vq
);
1787 desc_cache
= &caches
->desc
;
1789 virtio_queue_set_notification(vq
, 0);
1791 while (vq
->inuse
< vq
->vring
.num
) {
1792 unsigned int idx
= vq
->last_avail_idx
;
1794 * works similar to virtqueue_pop but does not map buffers
1795 * and does not allocate any memory.
1797 vring_packed_desc_read(vdev
, &desc
, desc_cache
,
1798 vq
->last_avail_idx
, true);
1799 if (!is_desc_avail(desc
.flags
, vq
->last_avail_wrap_counter
)) {
1802 elem
.index
= desc
.id
;
1804 while (virtqueue_packed_read_next_desc(vq
, &desc
, desc_cache
,
1805 vq
->vring
.num
, &idx
, false)) {
1809 * immediately push the element, nothing to unmap
1810 * as both in_num and out_num are set to 0.
1812 virtqueue_push(vq
, &elem
, 0);
1814 vq
->last_avail_idx
+= elem
.ndescs
;
1815 if (vq
->last_avail_idx
>= vq
->vring
.num
) {
1816 vq
->last_avail_idx
-= vq
->vring
.num
;
1817 vq
->last_avail_wrap_counter
^= 1;
1824 static unsigned int virtqueue_split_drop_all(VirtQueue
*vq
)
1826 unsigned int dropped
= 0;
1827 VirtQueueElement elem
= {};
1828 VirtIODevice
*vdev
= vq
->vdev
;
1829 bool fEventIdx
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
);
1831 while (!virtio_queue_empty(vq
) && vq
->inuse
< vq
->vring
.num
) {
1832 /* works similar to virtqueue_pop but does not map buffers
1833 * and does not allocate any memory */
1835 if (!virtqueue_get_head(vq
, vq
->last_avail_idx
, &elem
.index
)) {
1839 vq
->last_avail_idx
++;
1841 vring_set_avail_event(vq
, vq
->last_avail_idx
);
1843 /* immediately push the element, nothing to unmap
1844 * as both in_num and out_num are set to 0 */
1845 virtqueue_push(vq
, &elem
, 0);
1852 /* virtqueue_drop_all:
1853 * @vq: The #VirtQueue
1854 * Drops all queued buffers and indicates them to the guest
1855 * as if they are done. Useful when buffers can not be
1856 * processed but must be returned to the guest.
1858 unsigned int virtqueue_drop_all(VirtQueue
*vq
)
1860 struct VirtIODevice
*vdev
= vq
->vdev
;
1862 if (virtio_device_disabled(vq
->vdev
)) {
1866 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1867 return virtqueue_packed_drop_all(vq
);
1869 return virtqueue_split_drop_all(vq
);
1873 /* Reading and writing a structure directly to QEMUFile is *awful*, but
1874 * it is what QEMU has always done by mistake. We can change it sooner
1875 * or later by bumping the version number of the affected vm states.
1876 * In the meanwhile, since the in-memory layout of VirtQueueElement
1877 * has changed, we need to marshal to and from the layout that was
1878 * used before the change.
1880 typedef struct VirtQueueElementOld
{
1882 unsigned int out_num
;
1883 unsigned int in_num
;
1884 hwaddr in_addr
[VIRTQUEUE_MAX_SIZE
];
1885 hwaddr out_addr
[VIRTQUEUE_MAX_SIZE
];
1886 struct iovec in_sg
[VIRTQUEUE_MAX_SIZE
];
1887 struct iovec out_sg
[VIRTQUEUE_MAX_SIZE
];
1888 } VirtQueueElementOld
;
1890 void *qemu_get_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
, size_t sz
)
1892 VirtQueueElement
*elem
;
1893 VirtQueueElementOld data
;
1896 qemu_get_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1898 /* TODO: teach all callers that this can fail, and return failure instead
1899 * of asserting here.
1900 * This is just one thing (there are probably more) that must be
1901 * fixed before we can allow NDEBUG compilation.
1903 assert(ARRAY_SIZE(data
.in_addr
) >= data
.in_num
);
1904 assert(ARRAY_SIZE(data
.out_addr
) >= data
.out_num
);
1906 elem
= virtqueue_alloc_element(sz
, data
.out_num
, data
.in_num
);
1907 elem
->index
= data
.index
;
1909 for (i
= 0; i
< elem
->in_num
; i
++) {
1910 elem
->in_addr
[i
] = data
.in_addr
[i
];
1913 for (i
= 0; i
< elem
->out_num
; i
++) {
1914 elem
->out_addr
[i
] = data
.out_addr
[i
];
1917 for (i
= 0; i
< elem
->in_num
; i
++) {
1918 /* Base is overwritten by virtqueue_map. */
1919 elem
->in_sg
[i
].iov_base
= 0;
1920 elem
->in_sg
[i
].iov_len
= data
.in_sg
[i
].iov_len
;
1923 for (i
= 0; i
< elem
->out_num
; i
++) {
1924 /* Base is overwritten by virtqueue_map. */
1925 elem
->out_sg
[i
].iov_base
= 0;
1926 elem
->out_sg
[i
].iov_len
= data
.out_sg
[i
].iov_len
;
1929 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1930 qemu_get_be32s(f
, &elem
->ndescs
);
1933 virtqueue_map(vdev
, elem
);
1937 void qemu_put_virtqueue_element(VirtIODevice
*vdev
, QEMUFile
*f
,
1938 VirtQueueElement
*elem
)
1940 VirtQueueElementOld data
;
1943 memset(&data
, 0, sizeof(data
));
1944 data
.index
= elem
->index
;
1945 data
.in_num
= elem
->in_num
;
1946 data
.out_num
= elem
->out_num
;
1948 for (i
= 0; i
< elem
->in_num
; i
++) {
1949 data
.in_addr
[i
] = elem
->in_addr
[i
];
1952 for (i
= 0; i
< elem
->out_num
; i
++) {
1953 data
.out_addr
[i
] = elem
->out_addr
[i
];
1956 for (i
= 0; i
< elem
->in_num
; i
++) {
1957 /* Base is overwritten by virtqueue_map when loading. Do not
1958 * save it, as it would leak the QEMU address space layout. */
1959 data
.in_sg
[i
].iov_len
= elem
->in_sg
[i
].iov_len
;
1962 for (i
= 0; i
< elem
->out_num
; i
++) {
1963 /* Do not save iov_base as above. */
1964 data
.out_sg
[i
].iov_len
= elem
->out_sg
[i
].iov_len
;
1967 if (virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
1968 qemu_put_be32s(f
, &elem
->ndescs
);
1971 qemu_put_buffer(f
, (uint8_t *)&data
, sizeof(VirtQueueElementOld
));
1975 static void virtio_notify_vector(VirtIODevice
*vdev
, uint16_t vector
)
1977 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
1978 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
1980 if (virtio_device_disabled(vdev
)) {
1985 k
->notify(qbus
->parent
, vector
);
1989 void virtio_update_irq(VirtIODevice
*vdev
)
1991 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
1994 static int virtio_validate_features(VirtIODevice
*vdev
)
1996 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
1998 if (virtio_host_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
) &&
1999 !virtio_vdev_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
)) {
2003 if (k
->validate_features
) {
2004 return k
->validate_features(vdev
);
2010 int virtio_set_status(VirtIODevice
*vdev
, uint8_t val
)
2012 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2013 trace_virtio_set_status(vdev
, val
);
2015 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2016 if (!(vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) &&
2017 val
& VIRTIO_CONFIG_S_FEATURES_OK
) {
2018 int ret
= virtio_validate_features(vdev
);
2026 if ((vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) !=
2027 (val
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
2028 virtio_set_started(vdev
, val
& VIRTIO_CONFIG_S_DRIVER_OK
);
2031 if (k
->set_status
) {
2032 k
->set_status(vdev
, val
);
2039 static enum virtio_device_endian
virtio_default_endian(void)
2041 if (target_words_bigendian()) {
2042 return VIRTIO_DEVICE_ENDIAN_BIG
;
2044 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
2048 static enum virtio_device_endian
virtio_current_cpu_endian(void)
2050 if (cpu_virtio_is_big_endian(current_cpu
)) {
2051 return VIRTIO_DEVICE_ENDIAN_BIG
;
2053 return VIRTIO_DEVICE_ENDIAN_LITTLE
;
2057 static void __virtio_queue_reset(VirtIODevice
*vdev
, uint32_t i
)
2059 vdev
->vq
[i
].vring
.desc
= 0;
2060 vdev
->vq
[i
].vring
.avail
= 0;
2061 vdev
->vq
[i
].vring
.used
= 0;
2062 vdev
->vq
[i
].last_avail_idx
= 0;
2063 vdev
->vq
[i
].shadow_avail_idx
= 0;
2064 vdev
->vq
[i
].used_idx
= 0;
2065 vdev
->vq
[i
].last_avail_wrap_counter
= true;
2066 vdev
->vq
[i
].shadow_avail_wrap_counter
= true;
2067 vdev
->vq
[i
].used_wrap_counter
= true;
2068 virtio_queue_set_vector(vdev
, i
, VIRTIO_NO_VECTOR
);
2069 vdev
->vq
[i
].signalled_used
= 0;
2070 vdev
->vq
[i
].signalled_used_valid
= false;
2071 vdev
->vq
[i
].notification
= true;
2072 vdev
->vq
[i
].vring
.num
= vdev
->vq
[i
].vring
.num_default
;
2073 vdev
->vq
[i
].inuse
= 0;
2074 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
2077 void virtio_queue_reset(VirtIODevice
*vdev
, uint32_t queue_index
)
2079 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2081 if (k
->queue_reset
) {
2082 k
->queue_reset(vdev
, queue_index
);
2085 __virtio_queue_reset(vdev
, queue_index
);
2088 void virtio_queue_enable(VirtIODevice
*vdev
, uint32_t queue_index
)
2090 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2093 * TODO: Seabios is currently out of spec and triggering this error.
2094 * So this needs to be fixed in Seabios, then this can
2095 * be re-enabled for new machine types only, and also after
2096 * being converted to LOG_GUEST_ERROR.
2098 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2099 error_report("queue_enable is only suppported in devices of virtio "
2104 if (k
->queue_enable
) {
2105 k
->queue_enable(vdev
, queue_index
);
2109 void virtio_reset(void *opaque
)
2111 VirtIODevice
*vdev
= opaque
;
2112 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2115 virtio_set_status(vdev
, 0);
2117 /* Guest initiated reset */
2118 vdev
->device_endian
= virtio_current_cpu_endian();
2121 vdev
->device_endian
= virtio_default_endian();
2128 vdev
->start_on_kick
= false;
2129 vdev
->started
= false;
2130 vdev
->broken
= false;
2131 vdev
->guest_features
= 0;
2132 vdev
->queue_sel
= 0;
2134 vdev
->disabled
= false;
2135 qatomic_set(&vdev
->isr
, 0);
2136 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
2137 virtio_notify_vector(vdev
, vdev
->config_vector
);
2139 for(i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2140 __virtio_queue_reset(vdev
, i
);
2144 void virtio_queue_set_addr(VirtIODevice
*vdev
, int n
, hwaddr addr
)
2146 if (!vdev
->vq
[n
].vring
.num
) {
2149 vdev
->vq
[n
].vring
.desc
= addr
;
2150 virtio_queue_update_rings(vdev
, n
);
2153 hwaddr
virtio_queue_get_addr(VirtIODevice
*vdev
, int n
)
2155 return vdev
->vq
[n
].vring
.desc
;
2158 void virtio_queue_set_rings(VirtIODevice
*vdev
, int n
, hwaddr desc
,
2159 hwaddr avail
, hwaddr used
)
2161 if (!vdev
->vq
[n
].vring
.num
) {
2164 vdev
->vq
[n
].vring
.desc
= desc
;
2165 vdev
->vq
[n
].vring
.avail
= avail
;
2166 vdev
->vq
[n
].vring
.used
= used
;
2167 virtio_init_region_cache(vdev
, n
);
2170 void virtio_queue_set_num(VirtIODevice
*vdev
, int n
, int num
)
2172 /* Don't allow guest to flip queue between existent and
2173 * nonexistent states, or to set it to an invalid size.
2175 if (!!num
!= !!vdev
->vq
[n
].vring
.num
||
2176 num
> VIRTQUEUE_MAX_SIZE
||
2180 vdev
->vq
[n
].vring
.num
= num
;
2183 VirtQueue
*virtio_vector_first_queue(VirtIODevice
*vdev
, uint16_t vector
)
2185 return QLIST_FIRST(&vdev
->vector_queues
[vector
]);
2188 VirtQueue
*virtio_vector_next_queue(VirtQueue
*vq
)
2190 return QLIST_NEXT(vq
, node
);
2193 int virtio_queue_get_num(VirtIODevice
*vdev
, int n
)
2195 return vdev
->vq
[n
].vring
.num
;
2198 int virtio_queue_get_max_num(VirtIODevice
*vdev
, int n
)
2200 return vdev
->vq
[n
].vring
.num_default
;
2203 int virtio_get_num_queues(VirtIODevice
*vdev
)
2207 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2208 if (!virtio_queue_get_num(vdev
, i
)) {
2216 void virtio_queue_set_align(VirtIODevice
*vdev
, int n
, int align
)
2218 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2219 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2221 /* virtio-1 compliant devices cannot change the alignment */
2222 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2223 error_report("tried to modify queue alignment for virtio-1 device");
2226 /* Check that the transport told us it was going to do this
2227 * (so a buggy transport will immediately assert rather than
2228 * silently failing to migrate this state)
2230 assert(k
->has_variable_vring_alignment
);
2233 vdev
->vq
[n
].vring
.align
= align
;
2234 virtio_queue_update_rings(vdev
, n
);
2238 static void virtio_queue_notify_vq(VirtQueue
*vq
)
2240 if (vq
->vring
.desc
&& vq
->handle_output
) {
2241 VirtIODevice
*vdev
= vq
->vdev
;
2243 if (unlikely(vdev
->broken
)) {
2247 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2248 vq
->handle_output(vdev
, vq
);
2250 if (unlikely(vdev
->start_on_kick
)) {
2251 virtio_set_started(vdev
, true);
2256 void virtio_queue_notify(VirtIODevice
*vdev
, int n
)
2258 VirtQueue
*vq
= &vdev
->vq
[n
];
2260 if (unlikely(!vq
->vring
.desc
|| vdev
->broken
)) {
2264 trace_virtio_queue_notify(vdev
, vq
- vdev
->vq
, vq
);
2265 if (vq
->host_notifier_enabled
) {
2266 event_notifier_set(&vq
->host_notifier
);
2267 } else if (vq
->handle_output
) {
2268 vq
->handle_output(vdev
, vq
);
2270 if (unlikely(vdev
->start_on_kick
)) {
2271 virtio_set_started(vdev
, true);
2276 uint16_t virtio_queue_vector(VirtIODevice
*vdev
, int n
)
2278 return n
< VIRTIO_QUEUE_MAX
? vdev
->vq
[n
].vector
:
2282 void virtio_queue_set_vector(VirtIODevice
*vdev
, int n
, uint16_t vector
)
2284 VirtQueue
*vq
= &vdev
->vq
[n
];
2286 if (n
< VIRTIO_QUEUE_MAX
) {
2287 if (vdev
->vector_queues
&&
2288 vdev
->vq
[n
].vector
!= VIRTIO_NO_VECTOR
) {
2289 QLIST_REMOVE(vq
, node
);
2291 vdev
->vq
[n
].vector
= vector
;
2292 if (vdev
->vector_queues
&&
2293 vector
!= VIRTIO_NO_VECTOR
) {
2294 QLIST_INSERT_HEAD(&vdev
->vector_queues
[vector
], vq
, node
);
2299 VirtQueue
*virtio_add_queue(VirtIODevice
*vdev
, int queue_size
,
2300 VirtIOHandleOutput handle_output
)
2304 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2305 if (vdev
->vq
[i
].vring
.num
== 0)
2309 if (i
== VIRTIO_QUEUE_MAX
|| queue_size
> VIRTQUEUE_MAX_SIZE
)
2312 vdev
->vq
[i
].vring
.num
= queue_size
;
2313 vdev
->vq
[i
].vring
.num_default
= queue_size
;
2314 vdev
->vq
[i
].vring
.align
= VIRTIO_PCI_VRING_ALIGN
;
2315 vdev
->vq
[i
].handle_output
= handle_output
;
2316 vdev
->vq
[i
].used_elems
= g_new0(VirtQueueElement
, queue_size
);
2318 return &vdev
->vq
[i
];
2321 void virtio_delete_queue(VirtQueue
*vq
)
2324 vq
->vring
.num_default
= 0;
2325 vq
->handle_output
= NULL
;
2326 g_free(vq
->used_elems
);
2327 vq
->used_elems
= NULL
;
2328 virtio_virtqueue_reset_region_cache(vq
);
2331 void virtio_del_queue(VirtIODevice
*vdev
, int n
)
2333 if (n
< 0 || n
>= VIRTIO_QUEUE_MAX
) {
2337 virtio_delete_queue(&vdev
->vq
[n
]);
2340 static void virtio_set_isr(VirtIODevice
*vdev
, int value
)
2342 uint8_t old
= qatomic_read(&vdev
->isr
);
2344 /* Do not write ISR if it does not change, so that its cacheline remains
2345 * shared in the common case where the guest does not read it.
2347 if ((old
& value
) != value
) {
2348 qatomic_or(&vdev
->isr
, value
);
2352 /* Called within rcu_read_lock(). */
2353 static bool virtio_split_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2357 /* We need to expose used array entries before checking used event. */
2359 /* Always notify when queue is empty (when feature acknowledge) */
2360 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_NOTIFY_ON_EMPTY
) &&
2361 !vq
->inuse
&& virtio_queue_empty(vq
)) {
2365 if (!virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2366 return !(vring_avail_flags(vq
) & VRING_AVAIL_F_NO_INTERRUPT
);
2369 v
= vq
->signalled_used_valid
;
2370 vq
->signalled_used_valid
= true;
2371 old
= vq
->signalled_used
;
2372 new = vq
->signalled_used
= vq
->used_idx
;
2373 return !v
|| vring_need_event(vring_get_used_event(vq
), new, old
);
2376 static bool vring_packed_need_event(VirtQueue
*vq
, bool wrap
,
2377 uint16_t off_wrap
, uint16_t new,
2380 int off
= off_wrap
& ~(1 << 15);
2382 if (wrap
!= off_wrap
>> 15) {
2383 off
-= vq
->vring
.num
;
2386 return vring_need_event(off
, new, old
);
2389 /* Called within rcu_read_lock(). */
2390 static bool virtio_packed_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2392 VRingPackedDescEvent e
;
2395 VRingMemoryRegionCaches
*caches
;
2397 caches
= vring_get_region_caches(vq
);
2402 vring_packed_event_read(vdev
, &caches
->avail
, &e
);
2404 old
= vq
->signalled_used
;
2405 new = vq
->signalled_used
= vq
->used_idx
;
2406 v
= vq
->signalled_used_valid
;
2407 vq
->signalled_used_valid
= true;
2409 if (e
.flags
== VRING_PACKED_EVENT_FLAG_DISABLE
) {
2411 } else if (e
.flags
== VRING_PACKED_EVENT_FLAG_ENABLE
) {
2415 return !v
|| vring_packed_need_event(vq
, vq
->used_wrap_counter
,
2416 e
.off_wrap
, new, old
);
2419 /* Called within rcu_read_lock(). */
2420 static bool virtio_should_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2422 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
2423 return virtio_packed_should_notify(vdev
, vq
);
2425 return virtio_split_should_notify(vdev
, vq
);
2429 void virtio_notify_irqfd(VirtIODevice
*vdev
, VirtQueue
*vq
)
2431 WITH_RCU_READ_LOCK_GUARD() {
2432 if (!virtio_should_notify(vdev
, vq
)) {
2437 trace_virtio_notify_irqfd(vdev
, vq
);
2440 * virtio spec 1.0 says ISR bit 0 should be ignored with MSI, but
2441 * windows drivers included in virtio-win 1.8.0 (circa 2015) are
2442 * incorrectly polling this bit during crashdump and hibernation
2443 * in MSI mode, causing a hang if this bit is never updated.
2444 * Recent releases of Windows do not really shut down, but rather
2445 * log out and hibernate to make the next startup faster. Hence,
2446 * this manifested as a more serious hang during shutdown with
2448 * Next driver release from 2016 fixed this problem, so working around it
2449 * is not a must, but it's easy to do so let's do it here.
2451 * Note: it's safe to update ISR from any thread as it was switched
2452 * to an atomic operation.
2454 virtio_set_isr(vq
->vdev
, 0x1);
2455 event_notifier_set(&vq
->guest_notifier
);
2458 static void virtio_irq(VirtQueue
*vq
)
2460 virtio_set_isr(vq
->vdev
, 0x1);
2461 virtio_notify_vector(vq
->vdev
, vq
->vector
);
2464 void virtio_notify(VirtIODevice
*vdev
, VirtQueue
*vq
)
2466 WITH_RCU_READ_LOCK_GUARD() {
2467 if (!virtio_should_notify(vdev
, vq
)) {
2472 trace_virtio_notify(vdev
, vq
);
2476 void virtio_notify_config(VirtIODevice
*vdev
)
2478 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
))
2481 virtio_set_isr(vdev
, 0x3);
2483 virtio_notify_vector(vdev
, vdev
->config_vector
);
2486 static bool virtio_device_endian_needed(void *opaque
)
2488 VirtIODevice
*vdev
= opaque
;
2490 assert(vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_UNKNOWN
);
2491 if (!virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2492 return vdev
->device_endian
!= virtio_default_endian();
2494 /* Devices conforming to VIRTIO 1.0 or later are always LE. */
2495 return vdev
->device_endian
!= VIRTIO_DEVICE_ENDIAN_LITTLE
;
2498 static bool virtio_64bit_features_needed(void *opaque
)
2500 VirtIODevice
*vdev
= opaque
;
2502 return (vdev
->host_features
>> 32) != 0;
2505 static bool virtio_virtqueue_needed(void *opaque
)
2507 VirtIODevice
*vdev
= opaque
;
2509 return virtio_host_has_feature(vdev
, VIRTIO_F_VERSION_1
);
2512 static bool virtio_packed_virtqueue_needed(void *opaque
)
2514 VirtIODevice
*vdev
= opaque
;
2516 return virtio_host_has_feature(vdev
, VIRTIO_F_RING_PACKED
);
2519 static bool virtio_ringsize_needed(void *opaque
)
2521 VirtIODevice
*vdev
= opaque
;
2524 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2525 if (vdev
->vq
[i
].vring
.num
!= vdev
->vq
[i
].vring
.num_default
) {
2532 static bool virtio_extra_state_needed(void *opaque
)
2534 VirtIODevice
*vdev
= opaque
;
2535 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2536 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2538 return k
->has_extra_state
&&
2539 k
->has_extra_state(qbus
->parent
);
2542 static bool virtio_broken_needed(void *opaque
)
2544 VirtIODevice
*vdev
= opaque
;
2546 return vdev
->broken
;
2549 static bool virtio_started_needed(void *opaque
)
2551 VirtIODevice
*vdev
= opaque
;
2553 return vdev
->started
;
2556 static bool virtio_disabled_needed(void *opaque
)
2558 VirtIODevice
*vdev
= opaque
;
2560 return vdev
->disabled
;
2563 static const VMStateDescription vmstate_virtqueue
= {
2564 .name
= "virtqueue_state",
2566 .minimum_version_id
= 1,
2567 .fields
= (VMStateField
[]) {
2568 VMSTATE_UINT64(vring
.avail
, struct VirtQueue
),
2569 VMSTATE_UINT64(vring
.used
, struct VirtQueue
),
2570 VMSTATE_END_OF_LIST()
2574 static const VMStateDescription vmstate_packed_virtqueue
= {
2575 .name
= "packed_virtqueue_state",
2577 .minimum_version_id
= 1,
2578 .fields
= (VMStateField
[]) {
2579 VMSTATE_UINT16(last_avail_idx
, struct VirtQueue
),
2580 VMSTATE_BOOL(last_avail_wrap_counter
, struct VirtQueue
),
2581 VMSTATE_UINT16(used_idx
, struct VirtQueue
),
2582 VMSTATE_BOOL(used_wrap_counter
, struct VirtQueue
),
2583 VMSTATE_UINT32(inuse
, struct VirtQueue
),
2584 VMSTATE_END_OF_LIST()
2588 static const VMStateDescription vmstate_virtio_virtqueues
= {
2589 .name
= "virtio/virtqueues",
2591 .minimum_version_id
= 1,
2592 .needed
= &virtio_virtqueue_needed
,
2593 .fields
= (VMStateField
[]) {
2594 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2595 VIRTIO_QUEUE_MAX
, 0, vmstate_virtqueue
, VirtQueue
),
2596 VMSTATE_END_OF_LIST()
2600 static const VMStateDescription vmstate_virtio_packed_virtqueues
= {
2601 .name
= "virtio/packed_virtqueues",
2603 .minimum_version_id
= 1,
2604 .needed
= &virtio_packed_virtqueue_needed
,
2605 .fields
= (VMStateField
[]) {
2606 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2607 VIRTIO_QUEUE_MAX
, 0, vmstate_packed_virtqueue
, VirtQueue
),
2608 VMSTATE_END_OF_LIST()
2612 static const VMStateDescription vmstate_ringsize
= {
2613 .name
= "ringsize_state",
2615 .minimum_version_id
= 1,
2616 .fields
= (VMStateField
[]) {
2617 VMSTATE_UINT32(vring
.num_default
, struct VirtQueue
),
2618 VMSTATE_END_OF_LIST()
2622 static const VMStateDescription vmstate_virtio_ringsize
= {
2623 .name
= "virtio/ringsize",
2625 .minimum_version_id
= 1,
2626 .needed
= &virtio_ringsize_needed
,
2627 .fields
= (VMStateField
[]) {
2628 VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq
, struct VirtIODevice
,
2629 VIRTIO_QUEUE_MAX
, 0, vmstate_ringsize
, VirtQueue
),
2630 VMSTATE_END_OF_LIST()
2634 static int get_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2635 const VMStateField
*field
)
2637 VirtIODevice
*vdev
= pv
;
2638 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2639 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2641 if (!k
->load_extra_state
) {
2644 return k
->load_extra_state(qbus
->parent
, f
);
2648 static int put_extra_state(QEMUFile
*f
, void *pv
, size_t size
,
2649 const VMStateField
*field
, JSONWriter
*vmdesc
)
2651 VirtIODevice
*vdev
= pv
;
2652 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2653 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2655 k
->save_extra_state(qbus
->parent
, f
);
2659 static const VMStateInfo vmstate_info_extra_state
= {
2660 .name
= "virtqueue_extra_state",
2661 .get
= get_extra_state
,
2662 .put
= put_extra_state
,
2665 static const VMStateDescription vmstate_virtio_extra_state
= {
2666 .name
= "virtio/extra_state",
2668 .minimum_version_id
= 1,
2669 .needed
= &virtio_extra_state_needed
,
2670 .fields
= (VMStateField
[]) {
2672 .name
= "extra_state",
2674 .field_exists
= NULL
,
2676 .info
= &vmstate_info_extra_state
,
2677 .flags
= VMS_SINGLE
,
2680 VMSTATE_END_OF_LIST()
2684 static const VMStateDescription vmstate_virtio_device_endian
= {
2685 .name
= "virtio/device_endian",
2687 .minimum_version_id
= 1,
2688 .needed
= &virtio_device_endian_needed
,
2689 .fields
= (VMStateField
[]) {
2690 VMSTATE_UINT8(device_endian
, VirtIODevice
),
2691 VMSTATE_END_OF_LIST()
2695 static const VMStateDescription vmstate_virtio_64bit_features
= {
2696 .name
= "virtio/64bit_features",
2698 .minimum_version_id
= 1,
2699 .needed
= &virtio_64bit_features_needed
,
2700 .fields
= (VMStateField
[]) {
2701 VMSTATE_UINT64(guest_features
, VirtIODevice
),
2702 VMSTATE_END_OF_LIST()
2706 static const VMStateDescription vmstate_virtio_broken
= {
2707 .name
= "virtio/broken",
2709 .minimum_version_id
= 1,
2710 .needed
= &virtio_broken_needed
,
2711 .fields
= (VMStateField
[]) {
2712 VMSTATE_BOOL(broken
, VirtIODevice
),
2713 VMSTATE_END_OF_LIST()
2717 static const VMStateDescription vmstate_virtio_started
= {
2718 .name
= "virtio/started",
2720 .minimum_version_id
= 1,
2721 .needed
= &virtio_started_needed
,
2722 .fields
= (VMStateField
[]) {
2723 VMSTATE_BOOL(started
, VirtIODevice
),
2724 VMSTATE_END_OF_LIST()
2728 static const VMStateDescription vmstate_virtio_disabled
= {
2729 .name
= "virtio/disabled",
2731 .minimum_version_id
= 1,
2732 .needed
= &virtio_disabled_needed
,
2733 .fields
= (VMStateField
[]) {
2734 VMSTATE_BOOL(disabled
, VirtIODevice
),
2735 VMSTATE_END_OF_LIST()
2739 static const VMStateDescription vmstate_virtio
= {
2742 .minimum_version_id
= 1,
2743 .fields
= (VMStateField
[]) {
2744 VMSTATE_END_OF_LIST()
2746 .subsections
= (const VMStateDescription
*[]) {
2747 &vmstate_virtio_device_endian
,
2748 &vmstate_virtio_64bit_features
,
2749 &vmstate_virtio_virtqueues
,
2750 &vmstate_virtio_ringsize
,
2751 &vmstate_virtio_broken
,
2752 &vmstate_virtio_extra_state
,
2753 &vmstate_virtio_started
,
2754 &vmstate_virtio_packed_virtqueues
,
2755 &vmstate_virtio_disabled
,
2760 int virtio_save(VirtIODevice
*vdev
, QEMUFile
*f
)
2762 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2763 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2764 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2765 uint32_t guest_features_lo
= (vdev
->guest_features
& 0xffffffff);
2768 if (k
->save_config
) {
2769 k
->save_config(qbus
->parent
, f
);
2772 qemu_put_8s(f
, &vdev
->status
);
2773 qemu_put_8s(f
, &vdev
->isr
);
2774 qemu_put_be16s(f
, &vdev
->queue_sel
);
2775 qemu_put_be32s(f
, &guest_features_lo
);
2776 qemu_put_be32(f
, vdev
->config_len
);
2777 qemu_put_buffer(f
, vdev
->config
, vdev
->config_len
);
2779 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2780 if (vdev
->vq
[i
].vring
.num
== 0)
2784 qemu_put_be32(f
, i
);
2786 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2787 if (vdev
->vq
[i
].vring
.num
== 0)
2790 qemu_put_be32(f
, vdev
->vq
[i
].vring
.num
);
2791 if (k
->has_variable_vring_alignment
) {
2792 qemu_put_be32(f
, vdev
->vq
[i
].vring
.align
);
2795 * Save desc now, the rest of the ring addresses are saved in
2796 * subsections for VIRTIO-1 devices.
2798 qemu_put_be64(f
, vdev
->vq
[i
].vring
.desc
);
2799 qemu_put_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
2800 if (k
->save_queue
) {
2801 k
->save_queue(qbus
->parent
, i
, f
);
2805 if (vdc
->save
!= NULL
) {
2810 int ret
= vmstate_save_state(f
, vdc
->vmsd
, vdev
, NULL
);
2817 return vmstate_save_state(f
, &vmstate_virtio
, vdev
, NULL
);
2820 /* A wrapper for use as a VMState .put function */
2821 static int virtio_device_put(QEMUFile
*f
, void *opaque
, size_t size
,
2822 const VMStateField
*field
, JSONWriter
*vmdesc
)
2824 return virtio_save(VIRTIO_DEVICE(opaque
), f
);
2827 /* A wrapper for use as a VMState .get function */
2828 static int virtio_device_get(QEMUFile
*f
, void *opaque
, size_t size
,
2829 const VMStateField
*field
)
2831 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
2832 DeviceClass
*dc
= DEVICE_CLASS(VIRTIO_DEVICE_GET_CLASS(vdev
));
2834 return virtio_load(vdev
, f
, dc
->vmsd
->version_id
);
2837 const VMStateInfo virtio_vmstate_info
= {
2839 .get
= virtio_device_get
,
2840 .put
= virtio_device_put
,
2843 static int virtio_set_features_nocheck(VirtIODevice
*vdev
, uint64_t val
)
2845 VirtioDeviceClass
*k
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2846 bool bad
= (val
& ~(vdev
->host_features
)) != 0;
2848 val
&= vdev
->host_features
;
2849 if (k
->set_features
) {
2850 k
->set_features(vdev
, val
);
2852 vdev
->guest_features
= val
;
2853 return bad
? -1 : 0;
2856 int virtio_set_features(VirtIODevice
*vdev
, uint64_t val
)
2860 * The driver must not attempt to set features after feature negotiation
2863 if (vdev
->status
& VIRTIO_CONFIG_S_FEATURES_OK
) {
2867 if (val
& (1ull << VIRTIO_F_BAD_FEATURE
)) {
2868 qemu_log_mask(LOG_GUEST_ERROR
,
2869 "%s: guest driver for %s has enabled UNUSED(30) feature bit!\n",
2870 __func__
, vdev
->name
);
2873 ret
= virtio_set_features_nocheck(vdev
, val
);
2874 if (virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
)) {
2875 /* VIRTIO_RING_F_EVENT_IDX changes the size of the caches. */
2877 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
2878 if (vdev
->vq
[i
].vring
.num
!= 0) {
2879 virtio_init_region_cache(vdev
, i
);
2884 if (!virtio_device_started(vdev
, vdev
->status
) &&
2885 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
2886 vdev
->start_on_kick
= true;
2892 size_t virtio_get_config_size(const VirtIOConfigSizeParams
*params
,
2893 uint64_t host_features
)
2895 size_t config_size
= params
->min_size
;
2896 const VirtIOFeature
*feature_sizes
= params
->feature_sizes
;
2899 for (i
= 0; feature_sizes
[i
].flags
!= 0; i
++) {
2900 if (host_features
& feature_sizes
[i
].flags
) {
2901 config_size
= MAX(feature_sizes
[i
].end
, config_size
);
2905 assert(config_size
<= params
->max_size
);
2909 int virtio_load(VirtIODevice
*vdev
, QEMUFile
*f
, int version_id
)
2915 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
2916 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
2917 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
2920 * We poison the endianness to ensure it does not get used before
2921 * subsections have been loaded.
2923 vdev
->device_endian
= VIRTIO_DEVICE_ENDIAN_UNKNOWN
;
2925 if (k
->load_config
) {
2926 ret
= k
->load_config(qbus
->parent
, f
);
2931 qemu_get_8s(f
, &vdev
->status
);
2932 qemu_get_8s(f
, &vdev
->isr
);
2933 qemu_get_be16s(f
, &vdev
->queue_sel
);
2934 if (vdev
->queue_sel
>= VIRTIO_QUEUE_MAX
) {
2937 qemu_get_be32s(f
, &features
);
2940 * Temporarily set guest_features low bits - needed by
2941 * virtio net load code testing for VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
2942 * VIRTIO_NET_F_GUEST_ANNOUNCE and VIRTIO_NET_F_CTRL_VQ.
2944 * Note: devices should always test host features in future - don't create
2945 * new dependencies like this.
2947 vdev
->guest_features
= features
;
2949 config_len
= qemu_get_be32(f
);
2952 * There are cases where the incoming config can be bigger or smaller
2953 * than what we have; so load what we have space for, and skip
2954 * any excess that's in the stream.
2956 qemu_get_buffer(f
, vdev
->config
, MIN(config_len
, vdev
->config_len
));
2958 while (config_len
> vdev
->config_len
) {
2963 num
= qemu_get_be32(f
);
2965 if (num
> VIRTIO_QUEUE_MAX
) {
2966 error_report("Invalid number of virtqueues: 0x%x", num
);
2970 for (i
= 0; i
< num
; i
++) {
2971 vdev
->vq
[i
].vring
.num
= qemu_get_be32(f
);
2972 if (k
->has_variable_vring_alignment
) {
2973 vdev
->vq
[i
].vring
.align
= qemu_get_be32(f
);
2975 vdev
->vq
[i
].vring
.desc
= qemu_get_be64(f
);
2976 qemu_get_be16s(f
, &vdev
->vq
[i
].last_avail_idx
);
2977 vdev
->vq
[i
].signalled_used_valid
= false;
2978 vdev
->vq
[i
].notification
= true;
2980 if (!vdev
->vq
[i
].vring
.desc
&& vdev
->vq
[i
].last_avail_idx
) {
2981 error_report("VQ %d address 0x0 "
2982 "inconsistent with Host index 0x%x",
2983 i
, vdev
->vq
[i
].last_avail_idx
);
2986 if (k
->load_queue
) {
2987 ret
= k
->load_queue(qbus
->parent
, i
, f
);
2993 virtio_notify_vector(vdev
, VIRTIO_NO_VECTOR
);
2995 if (vdc
->load
!= NULL
) {
2996 ret
= vdc
->load(vdev
, f
, version_id
);
3003 ret
= vmstate_load_state(f
, vdc
->vmsd
, vdev
, version_id
);
3010 ret
= vmstate_load_state(f
, &vmstate_virtio
, vdev
, 1);
3015 if (vdev
->device_endian
== VIRTIO_DEVICE_ENDIAN_UNKNOWN
) {
3016 vdev
->device_endian
= virtio_default_endian();
3019 if (virtio_64bit_features_needed(vdev
)) {
3021 * Subsection load filled vdev->guest_features. Run them
3022 * through virtio_set_features to sanity-check them against
3025 uint64_t features64
= vdev
->guest_features
;
3026 if (virtio_set_features_nocheck(vdev
, features64
) < 0) {
3027 error_report("Features 0x%" PRIx64
" unsupported. "
3028 "Allowed features: 0x%" PRIx64
,
3029 features64
, vdev
->host_features
);
3033 if (virtio_set_features_nocheck(vdev
, features
) < 0) {
3034 error_report("Features 0x%x unsupported. "
3035 "Allowed features: 0x%" PRIx64
,
3036 features
, vdev
->host_features
);
3041 if (!virtio_device_started(vdev
, vdev
->status
) &&
3042 !virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3043 vdev
->start_on_kick
= true;
3046 RCU_READ_LOCK_GUARD();
3047 for (i
= 0; i
< num
; i
++) {
3048 if (vdev
->vq
[i
].vring
.desc
) {
3052 * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
3053 * only the region cache needs to be set up. Legacy devices need
3054 * to calculate used and avail ring addresses based on the desc
3057 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3058 virtio_init_region_cache(vdev
, i
);
3060 virtio_queue_update_rings(vdev
, i
);
3063 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3064 vdev
->vq
[i
].shadow_avail_idx
= vdev
->vq
[i
].last_avail_idx
;
3065 vdev
->vq
[i
].shadow_avail_wrap_counter
=
3066 vdev
->vq
[i
].last_avail_wrap_counter
;
3070 nheads
= vring_avail_idx(&vdev
->vq
[i
]) - vdev
->vq
[i
].last_avail_idx
;
3071 /* Check it isn't doing strange things with descriptor numbers. */
3072 if (nheads
> vdev
->vq
[i
].vring
.num
) {
3073 virtio_error(vdev
, "VQ %d size 0x%x Guest index 0x%x "
3074 "inconsistent with Host index 0x%x: delta 0x%x",
3075 i
, vdev
->vq
[i
].vring
.num
,
3076 vring_avail_idx(&vdev
->vq
[i
]),
3077 vdev
->vq
[i
].last_avail_idx
, nheads
);
3078 vdev
->vq
[i
].used_idx
= 0;
3079 vdev
->vq
[i
].shadow_avail_idx
= 0;
3080 vdev
->vq
[i
].inuse
= 0;
3083 vdev
->vq
[i
].used_idx
= vring_used_idx(&vdev
->vq
[i
]);
3084 vdev
->vq
[i
].shadow_avail_idx
= vring_avail_idx(&vdev
->vq
[i
]);
3087 * Some devices migrate VirtQueueElements that have been popped
3088 * from the avail ring but not yet returned to the used ring.
3089 * Since max ring size < UINT16_MAX it's safe to use modulo
3090 * UINT16_MAX + 1 subtraction.
3092 vdev
->vq
[i
].inuse
= (uint16_t)(vdev
->vq
[i
].last_avail_idx
-
3093 vdev
->vq
[i
].used_idx
);
3094 if (vdev
->vq
[i
].inuse
> vdev
->vq
[i
].vring
.num
) {
3095 error_report("VQ %d size 0x%x < last_avail_idx 0x%x - "
3097 i
, vdev
->vq
[i
].vring
.num
,
3098 vdev
->vq
[i
].last_avail_idx
,
3099 vdev
->vq
[i
].used_idx
);
3105 if (vdc
->post_load
) {
3106 ret
= vdc
->post_load(vdev
);
3115 void virtio_cleanup(VirtIODevice
*vdev
)
3117 qemu_del_vm_change_state_handler(vdev
->vmstate
);
3120 static void virtio_vmstate_change(void *opaque
, bool running
, RunState state
)
3122 VirtIODevice
*vdev
= opaque
;
3123 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3124 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3125 bool backend_run
= running
&& virtio_device_started(vdev
, vdev
->status
);
3126 vdev
->vm_running
= running
;
3129 virtio_set_status(vdev
, vdev
->status
);
3132 if (k
->vmstate_change
) {
3133 k
->vmstate_change(qbus
->parent
, backend_run
);
3137 virtio_set_status(vdev
, vdev
->status
);
3141 void virtio_instance_init_common(Object
*proxy_obj
, void *data
,
3142 size_t vdev_size
, const char *vdev_name
)
3144 DeviceState
*vdev
= data
;
3146 object_initialize_child_with_props(proxy_obj
, "virtio-backend", vdev
,
3147 vdev_size
, vdev_name
, &error_abort
,
3149 qdev_alias_all_properties(vdev
, proxy_obj
);
3152 void virtio_init(VirtIODevice
*vdev
, uint16_t device_id
, size_t config_size
)
3154 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3155 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3157 int nvectors
= k
->query_nvectors
? k
->query_nvectors(qbus
->parent
) : 0;
3160 vdev
->vector_queues
=
3161 g_malloc0(sizeof(*vdev
->vector_queues
) * nvectors
);
3164 vdev
->start_on_kick
= false;
3165 vdev
->started
= false;
3166 vdev
->vhost_started
= false;
3167 vdev
->device_id
= device_id
;
3169 qatomic_set(&vdev
->isr
, 0);
3170 vdev
->queue_sel
= 0;
3171 vdev
->config_vector
= VIRTIO_NO_VECTOR
;
3172 vdev
->vq
= g_new0(VirtQueue
, VIRTIO_QUEUE_MAX
);
3173 vdev
->vm_running
= runstate_is_running();
3174 vdev
->broken
= false;
3175 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3176 vdev
->vq
[i
].vector
= VIRTIO_NO_VECTOR
;
3177 vdev
->vq
[i
].vdev
= vdev
;
3178 vdev
->vq
[i
].queue_index
= i
;
3179 vdev
->vq
[i
].host_notifier_enabled
= false;
3182 vdev
->name
= virtio_id_to_name(device_id
);
3183 vdev
->config_len
= config_size
;
3184 if (vdev
->config_len
) {
3185 vdev
->config
= g_malloc0(config_size
);
3187 vdev
->config
= NULL
;
3189 vdev
->vmstate
= qdev_add_vm_change_state_handler(DEVICE(vdev
),
3190 virtio_vmstate_change
, vdev
);
3191 vdev
->device_endian
= virtio_default_endian();
3192 vdev
->use_guest_notifier_mask
= true;
3196 * Only devices that have already been around prior to defining the virtio
3197 * standard support legacy mode; this includes devices not specified in the
3198 * standard. All newer devices conform to the virtio standard only.
3200 bool virtio_legacy_allowed(VirtIODevice
*vdev
)
3202 switch (vdev
->device_id
) {
3204 case VIRTIO_ID_BLOCK
:
3205 case VIRTIO_ID_CONSOLE
:
3207 case VIRTIO_ID_BALLOON
:
3208 case VIRTIO_ID_RPMSG
:
3209 case VIRTIO_ID_SCSI
:
3211 case VIRTIO_ID_RPROC_SERIAL
:
3212 case VIRTIO_ID_CAIF
:
3219 bool virtio_legacy_check_disabled(VirtIODevice
*vdev
)
3221 return vdev
->disable_legacy_check
;
3224 hwaddr
virtio_queue_get_desc_addr(VirtIODevice
*vdev
, int n
)
3226 return vdev
->vq
[n
].vring
.desc
;
3229 bool virtio_queue_enabled_legacy(VirtIODevice
*vdev
, int n
)
3231 return virtio_queue_get_desc_addr(vdev
, n
) != 0;
3234 bool virtio_queue_enabled(VirtIODevice
*vdev
, int n
)
3236 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3237 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3239 if (k
->queue_enabled
) {
3240 return k
->queue_enabled(qbus
->parent
, n
);
3242 return virtio_queue_enabled_legacy(vdev
, n
);
3245 hwaddr
virtio_queue_get_avail_addr(VirtIODevice
*vdev
, int n
)
3247 return vdev
->vq
[n
].vring
.avail
;
3250 hwaddr
virtio_queue_get_used_addr(VirtIODevice
*vdev
, int n
)
3252 return vdev
->vq
[n
].vring
.used
;
3255 hwaddr
virtio_queue_get_desc_size(VirtIODevice
*vdev
, int n
)
3257 return sizeof(VRingDesc
) * vdev
->vq
[n
].vring
.num
;
3260 hwaddr
virtio_queue_get_avail_size(VirtIODevice
*vdev
, int n
)
3264 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3265 return sizeof(struct VRingPackedDescEvent
);
3268 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3269 return offsetof(VRingAvail
, ring
) +
3270 sizeof(uint16_t) * vdev
->vq
[n
].vring
.num
+ s
;
3273 hwaddr
virtio_queue_get_used_size(VirtIODevice
*vdev
, int n
)
3277 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3278 return sizeof(struct VRingPackedDescEvent
);
3281 s
= virtio_vdev_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
) ? 2 : 0;
3282 return offsetof(VRingUsed
, ring
) +
3283 sizeof(VRingUsedElem
) * vdev
->vq
[n
].vring
.num
+ s
;
3286 static unsigned int virtio_queue_packed_get_last_avail_idx(VirtIODevice
*vdev
,
3289 unsigned int avail
, used
;
3291 avail
= vdev
->vq
[n
].last_avail_idx
;
3292 avail
|= ((uint16_t)vdev
->vq
[n
].last_avail_wrap_counter
) << 15;
3294 used
= vdev
->vq
[n
].used_idx
;
3295 used
|= ((uint16_t)vdev
->vq
[n
].used_wrap_counter
) << 15;
3297 return avail
| used
<< 16;
3300 static uint16_t virtio_queue_split_get_last_avail_idx(VirtIODevice
*vdev
,
3303 return vdev
->vq
[n
].last_avail_idx
;
3306 unsigned int virtio_queue_get_last_avail_idx(VirtIODevice
*vdev
, int n
)
3308 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3309 return virtio_queue_packed_get_last_avail_idx(vdev
, n
);
3311 return virtio_queue_split_get_last_avail_idx(vdev
, n
);
3315 static void virtio_queue_packed_set_last_avail_idx(VirtIODevice
*vdev
,
3316 int n
, unsigned int idx
)
3318 struct VirtQueue
*vq
= &vdev
->vq
[n
];
3320 vq
->last_avail_idx
= vq
->shadow_avail_idx
= idx
& 0x7fff;
3321 vq
->last_avail_wrap_counter
=
3322 vq
->shadow_avail_wrap_counter
= !!(idx
& 0x8000);
3324 vq
->used_idx
= idx
& 0x7ffff;
3325 vq
->used_wrap_counter
= !!(idx
& 0x8000);
3328 static void virtio_queue_split_set_last_avail_idx(VirtIODevice
*vdev
,
3329 int n
, unsigned int idx
)
3331 vdev
->vq
[n
].last_avail_idx
= idx
;
3332 vdev
->vq
[n
].shadow_avail_idx
= idx
;
3335 void virtio_queue_set_last_avail_idx(VirtIODevice
*vdev
, int n
,
3338 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3339 virtio_queue_packed_set_last_avail_idx(vdev
, n
, idx
);
3341 virtio_queue_split_set_last_avail_idx(vdev
, n
, idx
);
3345 static void virtio_queue_packed_restore_last_avail_idx(VirtIODevice
*vdev
,
3348 /* We don't have a reference like avail idx in shared memory */
3352 static void virtio_queue_split_restore_last_avail_idx(VirtIODevice
*vdev
,
3355 RCU_READ_LOCK_GUARD();
3356 if (vdev
->vq
[n
].vring
.desc
) {
3357 vdev
->vq
[n
].last_avail_idx
= vring_used_idx(&vdev
->vq
[n
]);
3358 vdev
->vq
[n
].shadow_avail_idx
= vdev
->vq
[n
].last_avail_idx
;
3362 void virtio_queue_restore_last_avail_idx(VirtIODevice
*vdev
, int n
)
3364 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3365 virtio_queue_packed_restore_last_avail_idx(vdev
, n
);
3367 virtio_queue_split_restore_last_avail_idx(vdev
, n
);
3371 static void virtio_queue_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3373 /* used idx was updated through set_last_avail_idx() */
3377 static void virtio_split_packed_update_used_idx(VirtIODevice
*vdev
, int n
)
3379 RCU_READ_LOCK_GUARD();
3380 if (vdev
->vq
[n
].vring
.desc
) {
3381 vdev
->vq
[n
].used_idx
= vring_used_idx(&vdev
->vq
[n
]);
3385 void virtio_queue_update_used_idx(VirtIODevice
*vdev
, int n
)
3387 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3388 return virtio_queue_packed_update_used_idx(vdev
, n
);
3390 return virtio_split_packed_update_used_idx(vdev
, n
);
3394 void virtio_queue_invalidate_signalled_used(VirtIODevice
*vdev
, int n
)
3396 vdev
->vq
[n
].signalled_used_valid
= false;
3399 VirtQueue
*virtio_get_queue(VirtIODevice
*vdev
, int n
)
3401 return vdev
->vq
+ n
;
3404 uint16_t virtio_get_queue_index(VirtQueue
*vq
)
3406 return vq
->queue_index
;
3409 static void virtio_queue_guest_notifier_read(EventNotifier
*n
)
3411 VirtQueue
*vq
= container_of(n
, VirtQueue
, guest_notifier
);
3412 if (event_notifier_test_and_clear(n
)) {
3416 static void virtio_config_guest_notifier_read(EventNotifier
*n
)
3418 VirtIODevice
*vdev
= container_of(n
, VirtIODevice
, config_notifier
);
3420 if (event_notifier_test_and_clear(n
)) {
3421 virtio_notify_config(vdev
);
3424 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue
*vq
, bool assign
,
3427 if (assign
&& !with_irqfd
) {
3428 event_notifier_set_handler(&vq
->guest_notifier
,
3429 virtio_queue_guest_notifier_read
);
3431 event_notifier_set_handler(&vq
->guest_notifier
, NULL
);
3434 /* Test and clear notifier before closing it,
3435 * in case poll callback didn't have time to run. */
3436 virtio_queue_guest_notifier_read(&vq
->guest_notifier
);
3440 void virtio_config_set_guest_notifier_fd_handler(VirtIODevice
*vdev
,
3441 bool assign
, bool with_irqfd
)
3444 n
= &vdev
->config_notifier
;
3445 if (assign
&& !with_irqfd
) {
3446 event_notifier_set_handler(n
, virtio_config_guest_notifier_read
);
3448 event_notifier_set_handler(n
, NULL
);
3451 /* Test and clear notifier before closing it,*/
3452 /* in case poll callback didn't have time to run. */
3453 virtio_config_guest_notifier_read(n
);
3457 EventNotifier
*virtio_queue_get_guest_notifier(VirtQueue
*vq
)
3459 return &vq
->guest_notifier
;
3462 static void virtio_queue_host_notifier_aio_poll_begin(EventNotifier
*n
)
3464 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3466 virtio_queue_set_notification(vq
, 0);
3469 static bool virtio_queue_host_notifier_aio_poll(void *opaque
)
3471 EventNotifier
*n
= opaque
;
3472 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3474 return vq
->vring
.desc
&& !virtio_queue_empty(vq
);
3477 static void virtio_queue_host_notifier_aio_poll_ready(EventNotifier
*n
)
3479 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3481 virtio_queue_notify_vq(vq
);
3484 static void virtio_queue_host_notifier_aio_poll_end(EventNotifier
*n
)
3486 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3488 /* Caller polls once more after this to catch requests that race with us */
3489 virtio_queue_set_notification(vq
, 1);
3492 void virtio_queue_aio_attach_host_notifier(VirtQueue
*vq
, AioContext
*ctx
)
3494 aio_set_event_notifier(ctx
, &vq
->host_notifier
, false,
3495 virtio_queue_host_notifier_read
,
3496 virtio_queue_host_notifier_aio_poll
,
3497 virtio_queue_host_notifier_aio_poll_ready
);
3498 aio_set_event_notifier_poll(ctx
, &vq
->host_notifier
,
3499 virtio_queue_host_notifier_aio_poll_begin
,
3500 virtio_queue_host_notifier_aio_poll_end
);
3504 * Same as virtio_queue_aio_attach_host_notifier() but without polling. Use
3505 * this for rx virtqueues and similar cases where the virtqueue handler
3506 * function does not pop all elements. When the virtqueue is left non-empty
3507 * polling consumes CPU cycles and should not be used.
3509 void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue
*vq
, AioContext
*ctx
)
3511 aio_set_event_notifier(ctx
, &vq
->host_notifier
, false,
3512 virtio_queue_host_notifier_read
,
3516 void virtio_queue_aio_detach_host_notifier(VirtQueue
*vq
, AioContext
*ctx
)
3518 aio_set_event_notifier(ctx
, &vq
->host_notifier
, false, NULL
, NULL
, NULL
);
3521 void virtio_queue_host_notifier_read(EventNotifier
*n
)
3523 VirtQueue
*vq
= container_of(n
, VirtQueue
, host_notifier
);
3524 if (event_notifier_test_and_clear(n
)) {
3525 virtio_queue_notify_vq(vq
);
3529 EventNotifier
*virtio_queue_get_host_notifier(VirtQueue
*vq
)
3531 return &vq
->host_notifier
;
3534 EventNotifier
*virtio_config_get_guest_notifier(VirtIODevice
*vdev
)
3536 return &vdev
->config_notifier
;
3539 void virtio_queue_set_host_notifier_enabled(VirtQueue
*vq
, bool enabled
)
3541 vq
->host_notifier_enabled
= enabled
;
3544 int virtio_queue_set_host_notifier_mr(VirtIODevice
*vdev
, int n
,
3545 MemoryRegion
*mr
, bool assign
)
3547 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3548 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
3550 if (k
->set_host_notifier_mr
) {
3551 return k
->set_host_notifier_mr(qbus
->parent
, n
, mr
, assign
);
3557 void virtio_device_set_child_bus_name(VirtIODevice
*vdev
, char *bus_name
)
3559 g_free(vdev
->bus_name
);
3560 vdev
->bus_name
= g_strdup(bus_name
);
3563 void G_GNUC_PRINTF(2, 3) virtio_error(VirtIODevice
*vdev
, const char *fmt
, ...)
3568 error_vreport(fmt
, ap
);
3571 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_VERSION_1
)) {
3572 vdev
->status
= vdev
->status
| VIRTIO_CONFIG_S_NEEDS_RESET
;
3573 virtio_notify_config(vdev
);
3576 vdev
->broken
= true;
3579 static void virtio_memory_listener_commit(MemoryListener
*listener
)
3581 VirtIODevice
*vdev
= container_of(listener
, VirtIODevice
, listener
);
3584 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3585 if (vdev
->vq
[i
].vring
.num
== 0) {
3588 virtio_init_region_cache(vdev
, i
);
3592 static void virtio_device_realize(DeviceState
*dev
, Error
**errp
)
3594 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3595 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3598 /* Devices should either use vmsd or the load/save methods */
3599 assert(!vdc
->vmsd
|| !vdc
->load
);
3601 if (vdc
->realize
!= NULL
) {
3602 vdc
->realize(dev
, &err
);
3604 error_propagate(errp
, err
);
3609 virtio_bus_device_plugged(vdev
, &err
);
3611 error_propagate(errp
, err
);
3612 vdc
->unrealize(dev
);
3616 vdev
->listener
.commit
= virtio_memory_listener_commit
;
3617 vdev
->listener
.name
= "virtio";
3618 memory_listener_register(&vdev
->listener
, vdev
->dma_as
);
3619 QTAILQ_INSERT_TAIL(&virtio_list
, vdev
, next
);
3622 static void virtio_device_unrealize(DeviceState
*dev
)
3624 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
3625 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(dev
);
3627 memory_listener_unregister(&vdev
->listener
);
3628 virtio_bus_device_unplugged(vdev
);
3630 if (vdc
->unrealize
!= NULL
) {
3631 vdc
->unrealize(dev
);
3634 QTAILQ_REMOVE(&virtio_list
, vdev
, next
);
3635 g_free(vdev
->bus_name
);
3636 vdev
->bus_name
= NULL
;
3639 static void virtio_device_free_virtqueues(VirtIODevice
*vdev
)
3646 for (i
= 0; i
< VIRTIO_QUEUE_MAX
; i
++) {
3647 if (vdev
->vq
[i
].vring
.num
== 0) {
3650 virtio_virtqueue_reset_region_cache(&vdev
->vq
[i
]);
3655 static void virtio_device_instance_finalize(Object
*obj
)
3657 VirtIODevice
*vdev
= VIRTIO_DEVICE(obj
);
3659 virtio_device_free_virtqueues(vdev
);
3661 g_free(vdev
->config
);
3662 g_free(vdev
->vector_queues
);
3665 static Property virtio_properties
[] = {
3666 DEFINE_VIRTIO_COMMON_FEATURES(VirtIODevice
, host_features
),
3667 DEFINE_PROP_BOOL("use-started", VirtIODevice
, use_started
, true),
3668 DEFINE_PROP_BOOL("use-disabled-flag", VirtIODevice
, use_disabled_flag
, true),
3669 DEFINE_PROP_BOOL("x-disable-legacy-check", VirtIODevice
,
3670 disable_legacy_check
, false),
3671 DEFINE_PROP_END_OF_LIST(),
3674 static int virtio_device_start_ioeventfd_impl(VirtIODevice
*vdev
)
3676 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3680 * Batch all the host notifiers in a single transaction to avoid
3681 * quadratic time complexity in address_space_update_ioeventfds().
3683 memory_region_transaction_begin();
3684 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3685 VirtQueue
*vq
= &vdev
->vq
[n
];
3686 if (!virtio_queue_get_num(vdev
, n
)) {
3689 r
= virtio_bus_set_host_notifier(qbus
, n
, true);
3694 event_notifier_set_handler(&vq
->host_notifier
,
3695 virtio_queue_host_notifier_read
);
3698 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3699 /* Kick right away to begin processing requests already in vring */
3700 VirtQueue
*vq
= &vdev
->vq
[n
];
3701 if (!vq
->vring
.num
) {
3704 event_notifier_set(&vq
->host_notifier
);
3706 memory_region_transaction_commit();
3710 i
= n
; /* save n for a second iteration after transaction is committed. */
3712 VirtQueue
*vq
= &vdev
->vq
[n
];
3713 if (!virtio_queue_get_num(vdev
, n
)) {
3717 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3718 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3722 * The transaction expects the ioeventfds to be open when it
3723 * commits. Do it now, before the cleanup loop.
3725 memory_region_transaction_commit();
3728 if (!virtio_queue_get_num(vdev
, i
)) {
3731 virtio_bus_cleanup_host_notifier(qbus
, i
);
3736 int virtio_device_start_ioeventfd(VirtIODevice
*vdev
)
3738 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3739 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3741 return virtio_bus_start_ioeventfd(vbus
);
3744 static void virtio_device_stop_ioeventfd_impl(VirtIODevice
*vdev
)
3746 VirtioBusState
*qbus
= VIRTIO_BUS(qdev_get_parent_bus(DEVICE(vdev
)));
3750 * Batch all the host notifiers in a single transaction to avoid
3751 * quadratic time complexity in address_space_update_ioeventfds().
3753 memory_region_transaction_begin();
3754 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3755 VirtQueue
*vq
= &vdev
->vq
[n
];
3757 if (!virtio_queue_get_num(vdev
, n
)) {
3760 event_notifier_set_handler(&vq
->host_notifier
, NULL
);
3761 r
= virtio_bus_set_host_notifier(qbus
, n
, false);
3765 * The transaction expects the ioeventfds to be open when it
3766 * commits. Do it now, before the cleanup loop.
3768 memory_region_transaction_commit();
3770 for (n
= 0; n
< VIRTIO_QUEUE_MAX
; n
++) {
3771 if (!virtio_queue_get_num(vdev
, n
)) {
3774 virtio_bus_cleanup_host_notifier(qbus
, n
);
3778 int virtio_device_grab_ioeventfd(VirtIODevice
*vdev
)
3780 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3781 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3783 return virtio_bus_grab_ioeventfd(vbus
);
3786 void virtio_device_release_ioeventfd(VirtIODevice
*vdev
)
3788 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3789 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3791 virtio_bus_release_ioeventfd(vbus
);
3794 static void virtio_device_class_init(ObjectClass
*klass
, void *data
)
3796 /* Set the default value here. */
3797 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
3798 DeviceClass
*dc
= DEVICE_CLASS(klass
);
3800 dc
->realize
= virtio_device_realize
;
3801 dc
->unrealize
= virtio_device_unrealize
;
3802 dc
->bus_type
= TYPE_VIRTIO_BUS
;
3803 device_class_set_props(dc
, virtio_properties
);
3804 vdc
->start_ioeventfd
= virtio_device_start_ioeventfd_impl
;
3805 vdc
->stop_ioeventfd
= virtio_device_stop_ioeventfd_impl
;
3807 vdc
->legacy_features
|= VIRTIO_LEGACY_FEATURES
;
3809 QTAILQ_INIT(&virtio_list
);
3812 bool virtio_device_ioeventfd_enabled(VirtIODevice
*vdev
)
3814 BusState
*qbus
= qdev_get_parent_bus(DEVICE(vdev
));
3815 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
3817 return virtio_bus_ioeventfd_enabled(vbus
);
3820 VirtQueueStatus
*qmp_x_query_virtio_queue_status(const char *path
,
3825 VirtQueueStatus
*status
;
3827 vdev
= qmp_find_virtio_device(path
);
3829 error_setg(errp
, "Path %s is not a VirtIODevice", path
);
3833 if (queue
>= VIRTIO_QUEUE_MAX
|| !virtio_queue_get_num(vdev
, queue
)) {
3834 error_setg(errp
, "Invalid virtqueue number %d", queue
);
3838 status
= g_new0(VirtQueueStatus
, 1);
3839 status
->name
= g_strdup(vdev
->name
);
3840 status
->queue_index
= vdev
->vq
[queue
].queue_index
;
3841 status
->inuse
= vdev
->vq
[queue
].inuse
;
3842 status
->vring_num
= vdev
->vq
[queue
].vring
.num
;
3843 status
->vring_num_default
= vdev
->vq
[queue
].vring
.num_default
;
3844 status
->vring_align
= vdev
->vq
[queue
].vring
.align
;
3845 status
->vring_desc
= vdev
->vq
[queue
].vring
.desc
;
3846 status
->vring_avail
= vdev
->vq
[queue
].vring
.avail
;
3847 status
->vring_used
= vdev
->vq
[queue
].vring
.used
;
3848 status
->used_idx
= vdev
->vq
[queue
].used_idx
;
3849 status
->signalled_used
= vdev
->vq
[queue
].signalled_used
;
3850 status
->signalled_used_valid
= vdev
->vq
[queue
].signalled_used_valid
;
3852 if (vdev
->vhost_started
) {
3853 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_GET_CLASS(vdev
);
3854 struct vhost_dev
*hdev
= vdc
->get_vhost(vdev
);
3856 /* check if vq index exists for vhost as well */
3857 if (queue
>= hdev
->vq_index
&& queue
< hdev
->vq_index
+ hdev
->nvqs
) {
3858 status
->has_last_avail_idx
= true;
3860 int vhost_vq_index
=
3861 hdev
->vhost_ops
->vhost_get_vq_index(hdev
, queue
);
3862 struct vhost_vring_state state
= {
3863 .index
= vhost_vq_index
,
3866 status
->last_avail_idx
=
3867 hdev
->vhost_ops
->vhost_get_vring_base(hdev
, &state
);
3870 status
->has_shadow_avail_idx
= true;
3871 status
->has_last_avail_idx
= true;
3872 status
->last_avail_idx
= vdev
->vq
[queue
].last_avail_idx
;
3873 status
->shadow_avail_idx
= vdev
->vq
[queue
].shadow_avail_idx
;
3879 static strList
*qmp_decode_vring_desc_flags(uint16_t flags
)
3881 strList
*list
= NULL
;
3889 { VRING_DESC_F_NEXT
, "next" },
3890 { VRING_DESC_F_WRITE
, "write" },
3891 { VRING_DESC_F_INDIRECT
, "indirect" },
3892 { 1 << VRING_PACKED_DESC_F_AVAIL
, "avail" },
3893 { 1 << VRING_PACKED_DESC_F_USED
, "used" },
3897 for (i
= 0; map
[i
].flag
; i
++) {
3898 if ((map
[i
].flag
& flags
) == 0) {
3901 node
= g_malloc0(sizeof(strList
));
3902 node
->value
= g_strdup(map
[i
].value
);
3910 VirtioQueueElement
*qmp_x_query_virtio_queue_element(const char *path
,
3918 VirtioQueueElement
*element
= NULL
;
3920 vdev
= qmp_find_virtio_device(path
);
3922 error_setg(errp
, "Path %s is not a VirtIO device", path
);
3926 if (queue
>= VIRTIO_QUEUE_MAX
|| !virtio_queue_get_num(vdev
, queue
)) {
3927 error_setg(errp
, "Invalid virtqueue number %d", queue
);
3930 vq
= &vdev
->vq
[queue
];
3932 if (virtio_vdev_has_feature(vdev
, VIRTIO_F_RING_PACKED
)) {
3933 error_setg(errp
, "Packed ring not supported");
3936 unsigned int head
, i
, max
;
3937 VRingMemoryRegionCaches
*caches
;
3938 MemoryRegionCache indirect_desc_cache
= MEMORY_REGION_CACHE_INVALID
;
3939 MemoryRegionCache
*desc_cache
;
3941 VirtioRingDescList
*list
= NULL
;
3942 VirtioRingDescList
*node
;
3945 RCU_READ_LOCK_GUARD();
3947 max
= vq
->vring
.num
;
3950 head
= vring_avail_ring(vq
, vq
->last_avail_idx
% vq
->vring
.num
);
3952 head
= vring_avail_ring(vq
, index
% vq
->vring
.num
);
3956 caches
= vring_get_region_caches(vq
);
3958 error_setg(errp
, "Region caches not initialized");
3961 if (caches
->desc
.len
< max
* sizeof(VRingDesc
)) {
3962 error_setg(errp
, "Cannot map descriptor ring");
3966 desc_cache
= &caches
->desc
;
3967 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
3968 if (desc
.flags
& VRING_DESC_F_INDIRECT
) {
3970 len
= address_space_cache_init(&indirect_desc_cache
, vdev
->dma_as
,
3971 desc
.addr
, desc
.len
, false);
3972 desc_cache
= &indirect_desc_cache
;
3973 if (len
< desc
.len
) {
3974 error_setg(errp
, "Cannot map indirect buffer");
3978 max
= desc
.len
/ sizeof(VRingDesc
);
3980 vring_split_desc_read(vdev
, &desc
, desc_cache
, i
);
3983 element
= g_new0(VirtioQueueElement
, 1);
3984 element
->avail
= g_new0(VirtioRingAvail
, 1);
3985 element
->used
= g_new0(VirtioRingUsed
, 1);
3986 element
->name
= g_strdup(vdev
->name
);
3987 element
->index
= head
;
3988 element
->avail
->flags
= vring_avail_flags(vq
);
3989 element
->avail
->idx
= vring_avail_idx(vq
);
3990 element
->avail
->ring
= head
;
3991 element
->used
->flags
= vring_used_flags(vq
);
3992 element
->used
->idx
= vring_used_idx(vq
);
3996 /* A buggy driver may produce an infinite loop */
3997 if (ndescs
>= max
) {
4000 node
= g_new0(VirtioRingDescList
, 1);
4001 node
->value
= g_new0(VirtioRingDesc
, 1);
4002 node
->value
->addr
= desc
.addr
;
4003 node
->value
->len
= desc
.len
;
4004 node
->value
->flags
= qmp_decode_vring_desc_flags(desc
.flags
);
4009 rc
= virtqueue_split_read_next_desc(vdev
, &desc
, desc_cache
,
4011 } while (rc
== VIRTQUEUE_READ_DESC_MORE
);
4012 element
->descs
= list
;
4014 address_space_cache_destroy(&indirect_desc_cache
);
4020 static const TypeInfo virtio_device_info
= {
4021 .name
= TYPE_VIRTIO_DEVICE
,
4022 .parent
= TYPE_DEVICE
,
4023 .instance_size
= sizeof(VirtIODevice
),
4024 .class_init
= virtio_device_class_init
,
4025 .instance_finalize
= virtio_device_instance_finalize
,
4027 .class_size
= sizeof(VirtioDeviceClass
),
4030 static void virtio_register_types(void)
4032 type_register_static(&virtio_device_info
);
4035 type_init(virtio_register_types
)