[kvm-unit-tests PATCH v2 5/8] riscv: Prefer SBI DBCN console with UART fallback

Xiang W wangxiang at iscas.ac.cn
Tue Nov 24 04:52:40 PST 2026


在 2026-11-20五的 11:08 +0100,Andrew Jones写道:
> 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.

Thanks for the diff.

I'll use it as the base for v3, with one policy change: keep UART
when initialization succeeds and only fall back to DBCN if there is
no usable UART. Each DBCN write is an SBI ecall, which is slower
than an MMIO UART store, so the fast path should stay on UART when
one is present. Guests without an MMIO UART still print via DBCN.

I'll send v3 shortly.

Thanks,
Xiang
> 
> 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);
>  }
>  
>  /*




More information about the kvm-riscv mailing list