Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Andrew Jones <andrew.jones@linux.dev>
To: Xiang W <wangxiang@iscas.ac.cn>
Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	 pbonzini@redhat.com, thuth@redhat.com
Subject: Re: [kvm-unit-tests PATCH v2 5/8] riscv: Prefer SBI DBCN console with UART fallback
Date: Fri, 20 Nov 2026 11:08:56 +0100	[thread overview]
Message-ID: <awAQYxcqIrJwxRl2@orel> (raw)
In-Reply-To: <20261105082035.133680-6-wangxiang@iscas.ac.cn>

On Thu, Nov 05, 2026 at 04:20:32PM +0800, Xiang W wrote:
> CONFIG_SBI_CONSOLE already routes output through SBI DBCN, but console
> setup still always went through the UART init path. uart0_init_fdt()
> could skip a missing UART while uart0_init_acpi() aborted, so EFI/ACPI
> guests without a described MMIO UART could not print.
> 
> Probe SBI_EXT_DBCN first. When it is available, use
> SBI_EXT_DBCN_CONSOLE_WRITE and skip UART setup. Otherwise fall back to
> the existing FDT or ACPI UART path. This keeps a UART fallback (as
> required by the server platform spec on ACPI systems) while allowing

Your patch actually didn't support the fallback for early prints.
Let's go with the following approach instead.

diff --git a/lib/riscv/io.c b/lib/riscv/io.c
index b11634046b8f..e92c9288b3ad 100644
--- a/lib/riscv/io.c
+++ b/lib/riscv/io.c
@@ -28,7 +28,12 @@
 static volatile u8 *uart0_base = UART_EARLY_BASE;
 static u32 uart0_reg_width = 1;
 static u32 uart0_reg_shift;
-static struct spinlock uart_lock;
+static struct spinlock console_lock;
+#ifdef CONFIG_SBI_CONSOLE
+static bool prefer_dbcn = true;
+#else
+static bool prefer_dbcn = false;
+#endif
 
 static u32 uart0_read(u32 num)
 {
@@ -71,16 +76,15 @@ static void uart0_init_fdt(void)
 				break;
 		}
 
-#ifdef CONFIG_SBI_CONSOLE
-		uart0_base = NULL;
-		return;
-#else
 		if (ret) {
+			if (prefer_dbcn) {
+				uart0_base = NULL;
+				return;
+			}
 			printf("%s: Compatible uart not found in the device tree, aborting...\n",
 			       __func__);
 			abort();
 		}
-#endif
 	} else {
 		const fdt32_t *val;
 		int len;
@@ -102,17 +106,23 @@ static void uart0_init_fdt(void)
 
 static void uart0_init_acpi(void)
 {
-	assert_msg(false, "ACPI not available");
+	if (prefer_dbcn)
+		uart0_base = NULL;
+	else
+		assert_msg(false, "ACPI UART not supported");
 }
 
 void io_init(void)
 {
+	if (prefer_dbcn && !sbi_probe(SBI_EXT_DBCN))
+		prefer_dbcn = false;
+
 	if (dt_available())
 		uart0_init_fdt();
 	else
 		uart0_init_acpi();
 
-	if (uart0_base != UART_EARLY_BASE) {
+	if (!prefer_dbcn && uart0_base != UART_EARLY_BASE) {
 		printf("WARNING: early print support may not work. "
 		       "Found uart at %p, but early base is %p.\n",
 		       uart0_base, UART_EARLY_BASE);
@@ -126,31 +136,30 @@ void sbi_puts(const char *s)
 	unsigned long hi = upper_32_bits(addr);
 	unsigned long lo = lower_32_bits(addr);
 
-	spin_lock(&uart_lock);
+	spin_lock(&console_lock);
 	sbi_ecall(SBI_EXT_DBCN, SBI_EXT_DBCN_CONSOLE_WRITE, strlen(s), lo, hi, 0, 0, 0);
-	spin_unlock(&uart_lock);
+	spin_unlock(&console_lock);
 }
 
 void uart0_puts(const char *s);
 void uart0_puts(const char *s)
 {
 	assert(uart0_base);
-	spin_lock(&uart_lock);
+	spin_lock(&console_lock);
 	while (*s) {
 		while (!(uart0_read(UART_LSR_OFFSET) & UART_LSR_THRE))
 			;
 		uart0_write(0, *s++);
 	}
-	spin_unlock(&uart_lock);
+	spin_unlock(&console_lock);
 }
 
 void puts(const char *s)
 {
-#ifdef CONFIG_SBI_CONSOLE
-	sbi_puts(s);
-#else
-	uart0_puts(s);
-#endif
+	if (prefer_dbcn)
+		sbi_puts(s);
+	else
+		uart0_puts(s);
 }
 
 /*

  reply	other threads:[~2026-11-20 10:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05  8:20 [kvm-unit-tests PATCH v2 0/8] riscv: ACPI boot and BRS-I SBI tests Xiang W
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 1/8] riscv: Initialize the ACPI RSDP from the EFI system table Xiang W
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 2/8] riscv: Add ACPI support for CPU initialization Xiang W
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 3/8] riscv: Add ACPI support for timer frequency discovery Xiang W
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 4/8] riscv: Add ACPI support for ISA string discovery Xiang W
2026-11-20  8:54   ` Andrew Jones
2026-11-20  9:14   ` Andrew Jones
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 5/8] riscv: Prefer SBI DBCN console with UART fallback Xiang W
2026-11-20 10:08   ` Andrew Jones [this message]
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 6/8] riscv: efi: Allow ACPI boots Xiang W
2026-11-20 10:12   ` Andrew Jones
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 7/8] riscv: initialize ISA extensions for all harts in cpu_init Xiang W
2026-11-05  8:20 ` [kvm-unit-tests PATCH v2 8/8] riscv: Add BRS SBI requirement tests Xiang W

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awAQYxcqIrJwxRl2@orel \
    --to=andrew.jones@linux.dev \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=thuth@redhat.com \
    --cc=wangxiang@iscas.ac.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox