
Hello,
On 06.06.23 15:25, Ying-Chun Liu (PaulLiu) wrote:
From: "A. Cody Schuffelen" schuffelen@google.com
This is an implementation of single-character virtio-console. Part of the patch is based on barebox implementations.
Cool to see others found this useful. I had originally written this for the demo at https://barebox.org/jsbarebox/ :-)
Back then I had trouble getting more than one console to work, whether through multiport consoles or via having one virtio device per console. The original barebox source code has a comment about what I tried. Did you manage to resolve this limitation?
If not, you should add some check into your patch to prevent registering more than one console with the driver.
Cheers, Ahmad
Signed-off-by: A. Cody Schuffelen schuffelen@google.com [ Paul: pick from the Android tree. Rebase to the upstream. Small fixes. ] Signed-off-by: Ying-Chun Liu (PaulLiu) paul.liu@linaro.org Link: https://android.googlesource.com/platform/external/u-boot/+/bc68da98ebf835a5... Link: https://git.pengutronix.de/cgit/barebox/tree/drivers/serial/virtio_console.c... Link: https://android.googlesource.com/platform/external/u-boot/+/4581f7c6e96d7332... Cc: Bin Meng bmeng.cn@gmail.com
drivers/virtio/Kconfig | 8 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio-uclass.c | 1 + drivers/virtio/virtio_console.c | 158 ++++++++++++++++++++++++++++++++ include/virtio.h | 2 + 5 files changed, 170 insertions(+) create mode 100644 drivers/virtio/virtio_console.c
diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 852f6735b6..94aaf70ab9 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -68,6 +68,14 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based targets.
+config VIRTIO_CONSOLE
- bool "virtio console driver"
- depends on VIRTIO
- default y
- help
This is the virtual console driver for virtio. It can be used
with QEMU based targets.
config VIRTIO_RNG bool "virtio rng driver" depends on DM_RNG diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 4c63a6c690..c816f66aa6 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -10,4 +10,5 @@ obj-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o obj-$(CONFIG_VIRTIO_SANDBOX) += virtio_sandbox.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VIRTIO_BLK) += virtio_blk.o +obj-$(CONFIG_VIRTIO_CONSOLE) += virtio_console.o obj-$(CONFIG_VIRTIO_RNG) += virtio_rng.o diff --git a/drivers/virtio/virtio-uclass.c b/drivers/virtio/virtio-uclass.c index 31bb21c534..9c54c67572 100644 --- a/drivers/virtio/virtio-uclass.c +++ b/drivers/virtio/virtio-uclass.c @@ -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_CONSOLE] = VIRTIO_CONSOLE_DRV_NAME, [VIRTIO_ID_RNG] = VIRTIO_RNG_DRV_NAME,
};
diff --git a/drivers/virtio/virtio_console.c b/drivers/virtio/virtio_console.c new file mode 100644 index 0000000000..1d4b319b08 --- /dev/null +++ b/drivers/virtio/virtio_console.c @@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0+ +/*
- Copyright (C) 2018, Tuomas Tynkkynen tuomas.tynkkynen@iki.fi
- Copyright (C) 2018, Bin Meng bmeng.cn@gmail.com
- Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
- Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
- Copyright (C) 2009, 2010, 2011 Amit Shah amit.shah@redhat.com
- Copyright (C) 2021 Ahmad Fatoum
- Copyright (C) 2021, Google LLC, schuffelen@google.com (A. Cody Schuffelen)
- Copyright (C) 2023, Linaro
- */
+#include <common.h> +#include <blk.h> +#include <dm.h> +#include <part.h> +#include <serial.h> +#include <virtio_types.h> +#include <virtio.h> +#include <virtio_ring.h> +#include "virtio_blk.h"
+struct virtio_console_priv {
- struct virtqueue *receiveq_port0;
- struct virtqueue *transmitq_port0;
- unsigned char inbuf[1] __aligned(4);
+};
+static int virtio_console_bind(struct udevice *dev) +{
- struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
- /* Indicate what driver features we support */
- virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0);
- return 0;
+}
+/*
- Create a scatter-gather list representing our input buffer and put
- it in the queue.
- */
+static void add_inbuf(struct virtio_console_priv *priv) +{
- struct virtio_sg sg;
- struct virtio_sg *sgs[1];
- int ret;
- sgs[0] = &sg;
- sg.addr = priv->inbuf;
- sg.length = sizeof(priv->inbuf);
- ret = virtqueue_add(priv->receiveq_port0, sgs, 0, 1);
- /* We should always be able to add one buffer to an empty queue. */
- WARN(ret < 0, "%s: virtqueue_add failed\n", __func__);
- virtqueue_kick(priv->receiveq_port0);
+}
+static int virtio_console_probe(struct udevice *dev) +{
- struct virtio_console_priv *priv = dev_get_priv(dev);
- int ret;
- struct virtqueue *virtqueues[2];
- ret = virtio_find_vqs(dev, 2, virtqueues);
- if (ret < 0) {
debug("%s: virtio_find_vqs failed\n", __func__);
return ret;
- }
- priv->receiveq_port0 = virtqueues[0];
- priv->transmitq_port0 = virtqueues[1];
- /* Register the input buffer the first time. */
- add_inbuf(priv);
- return 0;
+}
+static int virtio_console_serial_setbrg(struct udevice *dev, int baudrate) +{
- return 0;
+}
+static int virtio_console_serial_pending(struct udevice *dev, bool input) +{
- struct virtio_console_priv *priv = dev_get_priv(dev);
- return virtqueue_poll(priv->receiveq_port0,
priv->receiveq_port0->last_used_idx);
+}
+static int virtio_console_serial_getc(struct udevice *dev) +{
- struct virtio_console_priv *priv = dev_get_priv(dev);
- unsigned char *in;
- int ch;
- unsigned int len = 0;
- in = virtqueue_get_buf(priv->receiveq_port0, &len);
- if (!in)
return -EAGAIN;
- WARN(len != 1, "%s: too much data: %d\n", __func__, len);
- ch = *in;
- add_inbuf(priv);
- return ch;
+}
+static int virtio_console_serial_putc(struct udevice *dev, const char ch) +{
- struct virtio_console_priv *priv = dev_get_priv(dev);
- struct virtio_sg sg;
- struct virtio_sg *sgs[1];
- unsigned char buf[1] __aligned(4);
- int ret = 0;
- sg.addr = buf;
- sg.length = sizeof(buf);
- sgs[0] = &sg;
- buf[0] = ch;
- ret = virtqueue_add(priv->transmitq_port0, sgs, 1, 0);
- if (ret) {
debug("%s: virtqueue_add failed\n", __func__);
return ret;
- }
- virtqueue_kick(priv->transmitq_port0);
- while (!virtqueue_get_buf(priv->transmitq_port0, NULL))
;
- return 0;
+}
+static const struct dm_serial_ops virtio_console_serial_ops = {
- .putc = virtio_console_serial_putc,
- .pending = virtio_console_serial_pending,
- .getc = virtio_console_serial_getc,
- .setbrg = virtio_console_serial_setbrg,
+};
+U_BOOT_DRIVER(virtio_console) = {
- .name = VIRTIO_CONSOLE_DRV_NAME,
- .id = UCLASS_SERIAL,
- .ops = &virtio_console_serial_ops,
- .bind = virtio_console_bind,
- .probe = virtio_console_probe,
- .remove = virtio_reset,
- .priv_auto = sizeof(struct virtio_console_priv),
- .flags = DM_FLAG_ACTIVE_DMA,
+}; diff --git a/include/virtio.h b/include/virtio.h index 062a24630c..e36dd41fd1 100644 --- a/include/virtio.h +++ b/include/virtio.h @@ -25,11 +25,13 @@ #include <linux/bug.h> #define VIRTIO_ID_NET 1 /* virtio net */ #define VIRTIO_ID_BLOCK 2 /* virtio block */ +#define VIRTIO_ID_CONSOLE 3 /* virtio console */ #define VIRTIO_ID_RNG 4 /* virtio rng */ #define VIRTIO_ID_MAX_NUM 5
#define VIRTIO_NET_DRV_NAME "virtio-net" #define VIRTIO_BLK_DRV_NAME "virtio-blk" +#define VIRTIO_CONSOLE_DRV_NAME "virtio-console" #define VIRTIO_RNG_DRV_NAME "virtio-rng"
/* Status byte for guest to report progress, and synchronize features */