hw/virtio: Add virtio_vdev_is_legacy()
This simplifies code compared to having virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) or !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1). Acked-by: Stefan Hajnoczi <stefanha@redhat.com> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Link: https://lore.kernel.org/qemu-devel/20260225041948.52929-2-philmd@linaro.org Signed-off-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Pierrick Bouvier committed
Feb 25, 2026 at 05:19 UTC
64495d7cfe2d9ce01e4424a5895486783171d935
5 files changed
+19
-16
hw/virtio/vhost.c
+5
-7
@@ -1168,14 +1168,12 @@ static void vhost_log_stop(MemoryListener *listener,
1168
*/
1169
static inline bool vhost_needs_vring_endian(VirtIODevice *vdev)
1170
{
1171
- if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1172
- return false;
1171
+ if (virtio_vdev_is_legacy(vdev)) {
1172
+ return vdev->device_endian == (HOST_BIG_ENDIAN
1173
+ ? VIRTIO_DEVICE_ENDIAN_LITTLE
1174
+ : VIRTIO_DEVICE_ENDIAN_BIG);
1175
}
1174
-#if HOST_BIG_ENDIAN
1175
- return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_LITTLE;
1176
-#else
1177
- return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
1178
-#endif
1176
+ return false;
1177
}
1178
1179
static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
hw/virtio/virtio-pci.c
+3
-3
@@ -1449,11 +1449,11 @@ static bool virtio_pci_queue_enabled(DeviceState *d, int n)
1449
VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
1450
VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1451
1452
- if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
1453
- return proxy->vqs[n].enabled;
1452
+ if (virtio_vdev_is_legacy(vdev)) {
1453
+ return virtio_queue_enabled_legacy(vdev, n);
1454
}
1455
1456
- return virtio_queue_enabled_legacy(vdev, n);
1456
+ return proxy->vqs[n].enabled;
1457
}
1458
1459
static int virtio_pci_add_mem_cap(VirtIOPCIProxy *proxy,
hw/virtio/virtio.c
+4
-4
@@ -2753,7 +2753,7 @@ static bool virtio_device_endian_needed(void *opaque)
2753
VirtIODevice *vdev = opaque;
2754
2755
assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
2756
- if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2756
+ if (virtio_vdev_is_legacy(vdev)) {
2757
return vdev->device_endian != virtio_default_endian();
2758
}
2759
/* Devices conforming to VIRTIO 1.0 or later are always LE. */
@@ -3460,10 +3460,10 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
3460
* to calculate used and avail ring addresses based on the desc
3461
* address.
3462
*/
3463
- if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3464
- virtio_init_region_cache(vdev, i);
3465
- } else {
3463
+ if (virtio_vdev_is_legacy(vdev)) {
3464
virtio_queue_update_rings(vdev, i);
3465
+ } else {
3466
+ virtio_init_region_cache(vdev, i);
3467
}
3468
3469
if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) {
include/hw/virtio/virtio-access.h
+1
-1
@@ -30,7 +30,7 @@ static inline bool virtio_access_is_big_endian(VirtIODevice *vdev)
30
#if defined(LEGACY_VIRTIO_IS_BIENDIAN)
31
return virtio_vdev_is_big_endian(vdev);
32
#elif TARGET_BIG_ENDIAN
33
- if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
33
+ if (!virtio_vdev_is_legacy(vdev)) {
34
/* Devices conforming to VIRTIO 1.0 or later are always LE. */
35
return false;
36
}
include/hw/virtio/virtio.h
+6
-1
@@ -468,9 +468,14 @@ static inline bool virtio_host_has_feature(VirtIODevice *vdev,
468
return virtio_has_feature(vdev->host_features, fbit);
469
}
470
471
+static inline bool virtio_vdev_is_legacy(const VirtIODevice *vdev)
472
+{
473
+ return !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1);
474
+}
475
+
476
static inline bool virtio_vdev_is_big_endian(const VirtIODevice *vdev)
477
{
473
- if (!virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
478
+ if (virtio_vdev_is_legacy(vdev)) {
479
assert(vdev->device_endian != VIRTIO_DEVICE_ENDIAN_UNKNOWN);
480
return vdev->device_endian == VIRTIO_DEVICE_ENDIAN_BIG;
481
}