[kvm-unit-tests PATCH v3 5/9] riscv: Fall back to SBI DBCN when no UART is available

Xiang W wangxiang at iscas.ac.cn
Tue Nov 24 17:44:05 PST 2026


Console setup currently relies on UART initialization. On EFI/ACPI
systems without a supported MMIO UART, initialization fails even when
the SBI DBCN console is available.

Probe SBI_EXT_DBCN during console initialization. Use the UART when it
is successfully initialized; if no UART is available, fall back to
SBI_EXT_DBCN_CONSOLE_WRITE when DBCN is supported. Report when early
printing may not work because the initialized UART differs from the
early UART base, or because DBCN is used instead.

Rename uart_lock to console_lock, since it protects both console
backends.

Signed-off-by: Xiang W <wangxiang at iscas.ac.cn>
---
 lib/riscv/io.c | 52 ++++++++++++++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 19 deletions(-)

diff --git a/lib/riscv/io.c b/lib/riscv/io.c
index b1163404..45fe93f5 100644
--- a/lib/riscv/io.c
+++ b/lib/riscv/io.c
@@ -28,7 +28,8 @@
 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;
+static bool prefer_dbcn;
 
 static u32 uart0_read(u32 num)
 {
@@ -71,16 +72,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,20 +102,33 @@ 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)
 {
+	prefer_dbcn = sbi_probe(SBI_EXT_DBCN);
+
 	if (dt_available())
 		uart0_init_fdt();
 	else
 		uart0_init_acpi();
 
+	if (!uart0_base && !prefer_dbcn)
+		assert_msg(false, "No console available");
+
 	if (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);
+		if (uart0_base)
+			printf("WARNING: early print support may not work. "
+			       "Found uart at %p, but early base is %p.\n",
+			       uart0_base, UART_EARLY_BASE);
+		else
+			printf("WARNING: early print support may not work. "
+			       "Found DBCN console, but early base is %p.\n",
+			       UART_EARLY_BASE);
 	}
 }
 
@@ -126,31 +139,32 @@ 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 (uart0_base)
+		uart0_puts(s);
+	else if (prefer_dbcn)
+		sbi_puts(s);
+	else
+		assert_msg(false, "No console available");
 }
 
 /*
-- 
2.47.3




More information about the kvm-riscv mailing list