git: 7cf02164a517 - main - virtio_gpu: use an X8R8G8B8 resource on big-endian guests

From: Piotr Kubaj <pkubaj_at_FreeBSD.org>
Date: Thu, 08 Oct 2026 08:44:43 UTC
The branch main has been updated by pkubaj:

URL: https://cgit.FreeBSD.org/src/commit/?id=7cf02164a517824c07faed91c009be90f2f17ec0

commit 7cf02164a517824c07faed91c009be90f2f17ec0
Author:     Piotr Kubaj <pkubaj@FreeBSD.org>
AuthorDate: 2026-09-28 08:43:35 +0000
Commit:     Piotr Kubaj <pkubaj@FreeBSD.org>
CommitDate: 2026-10-08 08:44:06 +0000

    virtio_gpu: use an X8R8G8B8 resource on big-endian guests
    
    The virtio-gpu resource formats are defined by byte order in memory,
    while vt(4) and the X server (through vt_fb and the fb mmap) write
    native 32-bit 0x00RRGGBB pixels into the shadow framebuffer.  On a
    big-endian guest such as powerpc64 those pixels land in memory as
    00 RR GG BB, which the host interprets under B8G8R8X8 with red and
    blue swapped and the padding byte taken as blue: white renders yellow
    and blue renders black.
    
    Request X8R8G8B8 on big-endian guests instead, which is also what the
    Linux driver does (DRM_FORMAT_HOST_XRGB8888).  Reproduced with a colour
    test pattern on a powerpc64 QEMU pseries guest using the identically
    coded out-of-tree fork of this driver (graphics/virtio-gpu-qemu-kmod);
    little-endian guests are unchanged.
    
    Differential Revision:  https://reviews.freebsd.org/D60085
    Approved by:    jhibbits
    MFC after:      1 week
---
 sys/dev/virtio/gpu/virtio_gpu.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/sys/dev/virtio/gpu/virtio_gpu.c b/sys/dev/virtio/gpu/virtio_gpu.c
index b95bae9fe286..2cbfc472909c 100644
--- a/sys/dev/virtio/gpu/virtio_gpu.c
+++ b/sys/dev/virtio/gpu/virtio_gpu.c
@@ -61,6 +61,12 @@
 /* The guest can allocate resource IDs, we only need one */
 #define	VTGPU_RESOURCE_ID	1
 
+#if _BYTE_ORDER == _BIG_ENDIAN
+#define	VTGPU_FORMAT		VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM
+#else
+#define	VTGPU_FORMAT		VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM
+#endif
+
 struct vtgpu_softc {
 	/* Must be first so we can cast from info -> softc */
 	struct fb_info 		 vtgpu_fb_info;
@@ -556,7 +562,7 @@ vtgpu_create_2d(struct vtgpu_softc *sc)
 	    atomic_fetchadd_64(&sc->vtgpu_next_fence, 1));
 
 	s.req.resource_id = htole32(VTGPU_RESOURCE_ID);
-	s.req.format = htole32(VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM);
+	s.req.format = htole32(VTGPU_FORMAT);
 	s.req.width = htole32(sc->vtgpu_fb_info.fb_width);
 	s.req.height = htole32(sc->vtgpu_fb_info.fb_height);