From patchwork Tue Jun 6 13:25:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ying-Chun Liu \(PaulLiu\)" X-Patchwork-Id: 689742 Delivered-To: patch@linaro.org Received: by 2002:a5d:4d8a:0:0:0:0:0 with SMTP id b10csp2633933wru; Tue, 6 Jun 2023 06:26:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ58A6EQirKXDnzt14QJovKqMkMfgW5x455HByrauVjeZsyecB1fZNJlS3OZxqUpsbxDaJ4U X-Received: by 2002:a05:6a00:230b:b0:64b:205:dbf3 with SMTP id h11-20020a056a00230b00b0064b0205dbf3mr2606348pfh.34.1686057974026; Tue, 06 Jun 2023 06:26:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686057974; cv=none; d=google.com; s=arc-20160816; b=nveS5YjGPFK+7akTEU0huV6DgBy0JU0mLtcGUwWrsqB+9UAajOyxeSfCRD539xEuzh zQF1eEk+apsu2ohLeLitwgPg3K3CHcYZupNvEy7mNdLP8PRlBCj8/9l25x+0NqlfYfFt myAhU3rCSSxCnPwbyqO09B7O9P7QtxNzs8QAAwEpkc6Ok/NoDlBeTt7daHeAu9w9wmgy yTwF0Uuesj59yag6ChDt6v4JwUxdHsZXJoDy7bzH57KKevYr/f3hMmKnyXsVLoT/G0W3 YTfxFjLgHFdTZm1L7u8ZyAtjTPAfdYdAiyKBoiTYzz2xWRW9KGV/gs1uHuuX9pvWHE1/ L3aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kKHc9I/zD6GXqv3Zg3+d9sxvaW5GSCX86tE6BtpALxI=; b=grdk8/hJTEdgexjY2apcMPyMr5an6GPgrPSvQgF88dKJrvODJl0A7H/X48bxNzJcUF v73fxOAjdRK+kz3XBKzMb/VVwfsSxEpJIuec8ojbBeirljQbTdVxsEFhkuAyrGoBjcvo 1SmPfbGx2gLGJdwkXPaFUXvMZSwYy33ETjg3JoVV9jypASNYCmVDt71TjBSrIHArgVnF JSe1s37puusIlmvxStaQTv3dfZGBL2eV/EKw3Lp7YwLPFftL04E4qsfT91GsbMA6UktY ToA9MlKwmwsOAKsVgKKw5dG9Opp2Mt52wEvZFoEsX8KUp10ypTd32ywbi88AmbGBYbyh mW3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20221208 header.b=Ruo1zOff; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id z7-20020aa79487000000b0065d19b74191si2698700pfk.220.2023.06.06.06.26.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:26:13 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20221208 header.b=Ruo1zOff; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3DF7185D8B; Tue, 6 Jun 2023 15:25:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="Ruo1zOff"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A96C485C5E; Tue, 6 Jun 2023 15:25:52 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BE19185C5E for ; Tue, 6 Jun 2023 15:25:48 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=grandpaul@gmail.com Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1b04706c85fso57019485ad.0 for ; Tue, 06 Jun 2023 06:25:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686057947; x=1688649947; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=kKHc9I/zD6GXqv3Zg3+d9sxvaW5GSCX86tE6BtpALxI=; b=Ruo1zOff4hfUthrmujg5iYMElBjzhLTpuMpM8imBCgwgUBExrn9yUBlJmclqVFR2mq vrfGS+y4C4LLTYo5e+bO/VwGKTXpw0IwfoZ1DEI+gMcrt3+u/OVLaSCdlBBTbh4xH8JK ToiowtVjFUO9GYjE7uRqNaYGnlX7aYLuC3NX4aH+XBLm1VzFdYupUkyIxAo42UyzS/ah 51zwNLzGh6bFXff7O00GsHoTJq+gf813Lf11YRaksU+nULTtBdQk4YwjEafjW1+8hH1M gd63rEj2p12rsZxiX5oHi+abbfEjVhZ6/GFvw6zSCsP0JhbQeoSK4zJtTP2wur9uelFF HSjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686057947; x=1688649947; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=kKHc9I/zD6GXqv3Zg3+d9sxvaW5GSCX86tE6BtpALxI=; b=RCBSUAby8ZGc3r1qNVv9AMeCfYwuGfaC09Iu3zxvzonzNQZ6xMxdGsWG5Vo4iFpfa1 1WG2E5/8ry08ml2tN/rDXWDB/AmjpP664iJ+s5hSh84zo7xBlfK+yULqYanc8O/coxQQ liOpr782p38qmItT1WH+xL+M6qaM3HDEnJ2H91yDBayZyyRtRPS824ad1BQ5NBio8wM9 qcmEHd/tG1bFfWn6xH2KU6Nm2z3/lqJni+tava4nD2bjyxrPgTMwYAJlxOv5HY5imahJ 77kFL3GXPupxCvxdF+pd7avTw/U8IHAElzUzUkayXXshfjr3HiskbizxDNQNIBppWGsk 21xw== X-Gm-Message-State: AC+VfDwagd2F6L1aeVtJ3MWLiomxUlvfJTO6VUYAzKHLx+sbYjQWLqAA 62tsyEv7VZwk7pUC+sdl7bYpSEe7s/fzDw== X-Received: by 2002:a17:902:d506:b0:1b1:9233:bbf5 with SMTP id b6-20020a170902d50600b001b19233bbf5mr2801864plg.57.1686057946758; Tue, 06 Jun 2023 06:25:46 -0700 (PDT) Received: from localhost ([111.184.129.17]) by smtp.gmail.com with ESMTPSA id q9-20020a170902788900b001ae0a4b1d3fsm8523089pll.153.2023.06.06.06.25.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 06:25:46 -0700 (PDT) From: "Ying-Chun Liu (PaulLiu)" X-Google-Original-From: "Ying-Chun Liu (PaulLiu)" To: u-boot@lists.denx.de Cc: "A. Cody Schuffelen" , Ying-Chun Liu , Bin Meng Subject: [PATCH 1/1] virtio: add driver for virtio_console devices Date: Tue, 6 Jun 2023 21:25:40 +0800 Message-Id: <20230606132540.3362711-2-paul.liu@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230606132540.3362711-1-paul.liu@linaro.org> References: <20230606132540.3362711-1-paul.liu@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean From: "A. Cody Schuffelen" This is an implementation of single-character virtio-console. Part of the patch is based on barebox implementations. Signed-off-by: A. Cody Schuffelen [ Paul: pick from the Android tree. Rebase to the upstream. Small fixes. ] Signed-off-by: Ying-Chun Liu (PaulLiu) Link: https://android.googlesource.com/platform/external/u-boot/+/bc68da98ebf835a50d869cf01c535ac936bfcb11 Link: https://git.pengutronix.de/cgit/barebox/tree/drivers/serial/virtio_console.c?id=180a551d542844b70012d7b94a415aacdcf31d45 Link: https://android.googlesource.com/platform/external/u-boot/+/4581f7c6e96d7332b534ae20de356a4554594d08 Cc: Bin Meng --- 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 + * Copyright (C) 2018, Bin Meng + * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation + * Copyright (C) 2009, 2010, 2011 Red Hat, Inc. + * Copyright (C) 2009, 2010, 2011 Amit Shah + * Copyright (C) 2021 Ahmad Fatoum + * Copyright (C) 2021, Google LLC, schuffelen@google.com (A. Cody Schuffelen) + * Copyright (C) 2023, Linaro + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#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 #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 */