virtio: Add driver for virtio gpu
RFC PATCH 1/1 virtio: Add driver for virtio gpu
Add a basic driver for virtio gpu. This allows for a video on qemu virt machines.
---
Since the m68k virtio stuff is now in next I thought I'd send this. This patch is based on next.
This is a very simple driver for virtio gpu that works just enough to create a framebuffer that u-boot can use.
I've been playing with this on m68k and its shown that some of the framebuffer code has some endian issues.
I think this is useful for testing the framebuffer code on other arches like arm, riscv and so on.
Open to comments, ideas etc.
drivers/virtio/Kconfig | 19 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_gpu.c | 284 ++++++++++++++++++++ drivers/virtio/virtio_gpu.h | 461 +++++++++++++++++++++++++++++++++ include/virtio.h | 10 +- 6 files changed, 772 insertions(+), 4 deletions(-) create mode 100644 drivers/virtio/virtio_gpu.c create mode 100644 drivers/virtio/virtio_gpu.h
drivers/virtio/Kconfig +19 -0
@@ -77,4 +77,23 @@ config VIRTIO_RNG help This is the virtual random number generator driver. It can be used with QEMU based targets. + +config VIRTIO_GPU + bool "virtio gpu driver" + depends on VIDEO + depends on VIRTIO + help + This is the virtual GPU driver. It can be used + with QEMU based targets. + +if VIRTIO_GPU +config VIRTIO_DEFAULT_WIDTH + int "default width" + default 1024 + +config VIRTIO_DEFAULT_HEIGHT + int "default height" + default 768 +endif + endmenu
drivers/virtio/Makefile +1 -0
@@ -11,3 +11,4 @@ obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.o +obj-$(CONFIG_VIRTIO_GPU) += virtio_gpu.o
drivers/virtio/virtio-uclass.c +1 -0
@@ -30,6 +30,7 @@ static const char *const virtio_drv_name[VIRTIO_ID_MAX_NUM] = { [VIRTIO_ID_NET] = VIRTIO_NET_DRV_NAME, [VIRTIO_ID_BLOCK] = VIRTIO_BLK_DRV_NAME, [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME, + [VIRTIO_ID_GPU] = VIRTIO_GPU_DRV_NAME, }; int virtio_get_config(struct udevice *vdev, unsigned int offset,
drivers/virtio/virtio_gpu.c new file +284 -0
284 lines not on this page · raw message
drivers/virtio/virtio_gpu.h new file +461 -0
461 lines not on this page · raw message
include/virtio.h +6 -4
@@ -28,14 +28,16 @@#define VIRTIO_VENDOR_QEMU 0x554d4551 -#define VIRTIO_ID_NET 1/* virtio net */ -#define VIRTIO_ID_BLOCK 2/* virtio block */ -#define VIRTIO_ID_RNG 4/* virtio rng */ -#define VIRTIO_ID_MAX_NUM 5 +#define VIRTIO_ID_NET 1/* virtio net */ +#define VIRTIO_ID_BLOCK 2/* virtio block */ +#define VIRTIO_ID_RNG 4/* virtio rng */ +#define VIRTIO_ID_GPU 16/* virtio gpu */ +#define VIRTIO_ID_MAX_NUM 17#define VIRTIO_NET_DRV_NAME "virtio-net"#define VIRTIO_BLK_DRV_NAME "virtio-blk"#define VIRTIO_RNG_DRV_NAME "virtio-rng" +#define VIRTIO_GPU_DRV_NAME "virtio-gpu"/* Status byte for guest to report progress, and synchronize features */
Signature · 1 line
2.53.0
Hi Daniel,
Quoted 7 lines · Daniel Palmer
virtio: Add driver for virtio gpu
Add a basic driver for virtio gpu. This allows for a video on qemu virt machines.
Signed-off-by Daniel Palmer <[email protected]>
Quoted 7 lines
drivers/virtio/Kconfig| 19 ++drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_gpu.c | 284 +++++++++++++++++++++++++ drivers/virtio/virtio_gpu.h | 461 +++++++++++++++++++++++++++++++++++++++++ include/virtio.h | 10 +- 6 files changed, 772 insertions(+), 4 deletions(-)
Thanks for doing this!
drivers/virtio/virtio_gpu.c line 8
Quoted 10 lines
drivers/virtio/virtio_gpu.c +8
-0@@ -0,0 +1,284 @@ +U_BOOT_DRIVER (virtio_gpu) = { + .name =VIRTIO_GPU_DRV_NAME , + .id =UCLASS_VIDEO , + .ops = &virtio_gpu_ops, + .probe = virtio_gpu_probe, + .flags =DM_FLAG_ACTIVE_DMA , + .priv_auto =sizeof (struct virtio_gpu_priv ), +};
Two things are missing here per doc/develop/driver-model/virtio.rst
- a bind() that calls virtio_driver_features_init() for feature negotiation
- a '.remove = virtio_reset' so the device is reset before jumping to the OS; without it the host may keep scanning out of a buffer U-Boot has freed.
drivers/virtio/virtio_gpu.c line 10
Quoted 12 lines
drivers/virtio/virtio_gpu.c +10
-0@@ -0,0 +1,284 @@ +/* virtio GPU apparently doesn't care about alignment */ + plat->size = (width * height) *4 ; + plat->base = (ulong )malloc (plat->size); +if (!plat->base) +return -ENOMEM ; + + ret =virtio_gpu_resource_attach_backing (vq, +FRAMEBUFFER_RESOURCE_ID , + plat->base, +round_up (plat->size,4096 ));
The length advertised to the host is round_up(plat->size, 4096) but only plat->size bytes are allocated, so the host may read past the end. Allocate the rounded-up size or pass plat->size
More fundamentally, video drivers declare plat->size (and plat->align if needed) in bind(), and video_reserve() places the framebuffer in the reserved region below the U-Boot relocation address - see drivers/video/bochs.c for the standard pattern. So you should not need the malloc() in probe() ?
drivers/virtio/Kconfig config VIRTIO_RNG · line 85
Quoted 11 lines
drivers/virtio/Kconfig +9
-0@@ -77,4 +77,23 @@ config VIRTIO_RNG +if VIRTIO_GPU +config VIRTIO_DEFAULT_WIDTH + int "default width" + default 1024 + +config VIRTIO_DEFAULT_HEIGHT + int "default height" + default 768 +endif
These sit in the VIRTIO namespace but are GPU-specific - please rename to VIRTIO_GPU_DEFAULT_WIDTH / VIRTIO_GPU_DEFAULT_HEIGHT
The prompts also do not say what they configure; something like 'Default framebuffer width' would be clearer. BTW, are these ever used?
Quoted 4 lines
drivers/virtio/virtio_gpu.c +2
-0@@ -0,0 +1,284 @@ +// SPDX-License-Identifier: GPL-2.0+ +/*
drivers/virtio/virtio_gpu.c line 12
Quoted 10 lines
drivers/virtio/virtio_gpu.c +10
-0+* virtio GPU driver +* 2025 Daniel Palmer <[email protected]> +*/ + +#include <dm.h> +#include <malloc.h> +#include <video.h> +#include <virtio_types.h> +#include <virtio.h> +#include <virtio_ring.h>
Please add #define LOG_CATEGORY UCLASS_VIRTIO at the top - also good to use log_msg_ret() / log_debug() on the error paths. Several goto-err paths currently swallow the error silently.
Quoted 8 lines
drivers/virtio/virtio_gpu.c +6
-0@@ -0,0 +1,284 @@ +while (!virtqueue_get_buf (vq,NULL )) + ; + +/* Some basic error handling */ +switch (le32_to_cpu (resp_hdr->type)) { +case VIRTIO_GPU_RESP_ERR_UNSPEC :
drivers/virtio/virtio_gpu.c line 16
Quoted 10 lines
drivers/virtio/virtio_gpu.c +10
-0+case VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID : +case VIRTIO_GPU_RESP_ERR_INVALID_RESOURCE_ID : +case VIRTIO_GPU_RESP_ERR_INVALID_CONTEXT_ID : +case VIRTIO_GPU_RESP_ERR_INVALID_PARAMETER : +return -EINVAL ; +case VIRTIO_GPU_RESP_ERR_OUT_OF_MEMORY : +return -ENOMEM ; + } + +return 0 ;
This returns success for any response not in the listed error codes, so a future spec error (or garbage) is silently treated as OK. Please invert the check — require resp_hdr->type to be one of VIRTIO_GPU_RESP_OK_* and return -EIO otherwise.
drivers/virtio/virtio_gpu.c line 6
Quoted 8 lines
drivers/virtio/virtio_gpu.c +6
-0@@ -0,0 +1,284 @@ + virtio: Add driver for virtio gpu + + Add a basic driver for virtio gpu. This allows for a video on + qemu virt machines. + + Signed-off-by: Daniel Palmer <[email protected]>
The commit message is thin. Please mention the motivation (no current display driver works on QEMU virt for arm64/riscv?), what is and isn't supported (2D only, single scanout, no cursor, no EDID, no feature negotiation) and how the user enables it. Also 'allows for a video' reads oddly - perhaps 'enables video output on QEMU virt machines'.
Regards,
Simon
Hi Daniel,
Quoted 2 lines · Daniel Palmer
Add a basic driver for virtio gpu. This allows for a video on qemu virt machines.
Cool! Thanks for doing this.
I was also planning to look into adding support for some other virtio drivers, but I've been traveling recently and haven't had the time to finish them up.
Video drivers aren't really my area of expertise, so my feedback is a bit limited. I just have one minor comment below:
Quoted 24 lines
Signed-off-by Daniel Palmer <[email protected]>---
Since the m68k virtio stuff is now in next I thought I'd send this. This patch is based on next.
This is a very simple driver for virtio gpu that works just enough to create a framebuffer that u-boot can use.
I've been playing with this on m68k and its shown that some of the framebuffer code has some endian issues.
I think this is useful for testing the framebuffer code on other arches like arm, riscv and so on.
Open to comments, ideas etc.
drivers/virtio/virtio_gpu.c new file +6
-0+static int virtio_gpu_resource_create_2d (struct virtqueue *vq, +u32 resource_id, +u32 format, +u32 width, +u32 height) +{
drivers/virtio/virtio_gpu.c line 84
Quoted 10 lines
drivers/virtio/virtio_gpu.c new file +10
-0+struct virtio_gpu_resource_create_2d req = { + .hdr.type =cpu_to_le32 (VIRTIO_GPU_CMD_RESOURCE_CREATE_2D ), + .resource_id =cpu_to_le32 (resource_id), + .format =cpu_to_le32 (format), + .width =cpu_to_le32 (width), + .height =cpu_to_le32 (height), + }; +struct virtio_gpu_ctrl_hdr resp; + +return virtio_gpu_cmd (vq, &req,sizeof (req), &resp,sizeof (resp));
IIUC, req and resp here are used as dma buffers. I'm not sure if it's safe to allocate them on the stack. Are there alignment requirements for them?
Regards,
Kuan-Wei
Hi Kuan-Wei,
Quoted 4 lines · Kuan-Wei Chiu
Hi Daniel,
On Sat, May 23, 2026 at 02:50:00PM +0900, Daniel Palmer wrote:
Quoted 8 lines
Quoted 2 lines
Add a basic driver for virtio gpu. This allows for a video on qemu virt machines.
Cool! Thanks for doing this.
I was also planning to look into adding support for some other virtio drivers, but I've been traveling recently and haven't had the time to finish them up.
That's great! Let's coordinate a bit - I have virtio-scsi and virtiofs locally - I just send a precursor series with a sandbox emulator too.
[..]
Regards,
Simon