[kvm-unit-tests PATCH v3 9/9] riscv: Add BRS SBI requirement tests

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


Add a riscv-brs unit test for the SBI rules in the RISC-V Boot and
Runtime Services specification:

  SBI_010  spec version must be v2.0 or later
  SBI_020  HSM must be implemented
  SBI_030  TIME is required unless Sstc is present
  SBI_040  IPI is required unless IMSIC is present
  SBI_050  RFENCE is required unless IMSIC is present
  SBI_060  PMU is required unless Sscsrind and Ssccfg are all present
  SBI_070  DBCN is required when ACPI SPCR Interface Type is 0x15

Parse the extra ISA names used as skip conditions, and add the RFNC
and PMU extension IDs needed to probe those extensions.

Signed-off-by: Xiang W <wangxiang at iscas.ac.cn>
---
 lib/riscv/asm/isa.h |   3 +
 lib/riscv/asm/sbi.h |   8 +-
 lib/riscv/isa.c     |  12 ++-
 riscv/Makefile      |   1 +
 riscv/riscv-brs.c   | 192 ++++++++++++++++++++++++++++++++++++++++++++
 riscv/unittests.cfg |   4 +
 6 files changed, 215 insertions(+), 5 deletions(-)
 create mode 100644 riscv/riscv-brs.c

diff --git a/lib/riscv/asm/isa.h b/lib/riscv/asm/isa.h
index fb3af67d..6516c869 100644
--- a/lib/riscv/asm/isa.h
+++ b/lib/riscv/asm/isa.h
@@ -13,6 +13,9 @@
  * by name not tracked here with cpu_has_extension_name()
  */
 enum {
+	ISA_SSAIA,
+	ISA_SSCCFG,
+	ISA_SSCSRIND,
 	ISA_SSTC,
 	ISA_MAX,
 };
diff --git a/lib/riscv/asm/sbi.h b/lib/riscv/asm/sbi.h
index 289a6a24..987ea1de 100644
--- a/lib/riscv/asm/sbi.h
+++ b/lib/riscv/asm/sbi.h
@@ -44,13 +44,15 @@
 enum sbi_ext_id {
 	SBI_EXT_BASE = 0x10,
 	SBI_EXT_TIME = 0x54494d45,
-	SBI_EXT_IPI = 0x735049,
-	SBI_EXT_HSM = 0x48534d,
+	SBI_EXT_IPI  = 0x735049,
+	SBI_EXT_RFNC = 0x52464E43,
+	SBI_EXT_HSM  = 0x48534d,
 	SBI_EXT_SRST = 0x53525354,
+	SBI_EXT_PMU  = 0x504D55,
 	SBI_EXT_DBCN = 0x4442434E,
 	SBI_EXT_SUSP = 0x53555350,
 	SBI_EXT_FWFT = 0x46574654,
-	SBI_EXT_SSE = 0x535345,
+	SBI_EXT_SSE  = 0x535345,
 	SBI_EXT_DBTR = 0x44425452,
 };
 
diff --git a/lib/riscv/isa.c b/lib/riscv/isa.c
index c03bb19b..38418e3e 100644
--- a/lib/riscv/isa.c
+++ b/lib/riscv/isa.c
@@ -39,8 +39,16 @@ static void isa_bit(const char *name, int len, void *data)
 {
 	struct thread_info *info = (struct thread_info *)data;
 
-	if (isa_match("sstc", name, len))
-		set_bit(ISA_SSTC, info->isa);
+#define ISA_MATCH_AND_SET(ext) \
+	if (isa_match(#ext, name, len)) \
+		set_bit(ISA_##ext, info->isa)
+
+	ISA_MATCH_AND_SET(SSAIA);
+	ISA_MATCH_AND_SET(SSCCFG);
+	ISA_MATCH_AND_SET(SSCSRIND);
+	ISA_MATCH_AND_SET(SSTC);
+
+#undef ISA_MATCH_AND_SET
 }
 
 static void isa_parse(const char *isa_string, int len, struct isa_info *info)
diff --git a/riscv/Makefile b/riscv/Makefile
index f90992ab..8b3bb99a 100644
--- a/riscv/Makefile
+++ b/riscv/Makefile
@@ -15,6 +15,7 @@ tests += $(TEST_DIR)/sbi.$(exe)
 tests += $(TEST_DIR)/selftest.$(exe)
 tests += $(TEST_DIR)/sieve.$(exe)
 tests += $(TEST_DIR)/isa-dbltrp.$(exe)
+tests += $(TEST_DIR)/riscv-brs.$(exe)
 
 all: $(tests)
 
diff --git a/riscv/riscv-brs.c b/riscv/riscv-brs.c
new file mode 100644
index 00000000..0591d3d0
--- /dev/null
+++ b/riscv/riscv-brs.c
@@ -0,0 +1,192 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * RISC-V BRS SBI requirement tests
+ *
+ * Copyright (C) 2026, Institute of Software, CAS., Xiang W <wangxiang at iscas.ac.cn>
+ */
+
+#include <libcflat.h>
+#include <devicetree.h>
+#include <asm/sbi.h>
+#include <asm/isa.h>
+#include <asm/smp.h>
+#include <acpi.h>
+
+static void check_sbi_010(void)
+{
+	struct sbiret ret;
+	unsigned long major, minor;
+
+	report_prefix_push("SBI_010");
+
+	ret = sbi_get_spec_version();
+	if (ret.error) {
+		report_fail("sbi_get_spec_version failed with %ld", ret.error);
+	} else {
+		major = ret.value >> SBI_SPEC_VERSION_MAJOR_SHIFT;
+		major = major & SBI_SPEC_VERSION_MAJOR_MASK;
+		minor = ret.value & SBI_SPEC_VERSION_MINOR_MASK;
+
+		report(sbi_mk_version(major, minor) >= sbi_mk_version(2, 0),
+			"SBI spec version is v2.0 or later (current %lu.%lu)", major, minor);
+	}
+
+	report_prefix_pop();
+}
+
+static void check_sbi_020(void)
+{
+	report_prefix_push("SBI_020");
+	report(sbi_probe(SBI_EXT_HSM), "HSM extension is implemented");
+	report_prefix_pop();
+}
+
+static void check_sbi_030(void)
+{
+	int cpu;
+	bool has = true;
+
+	report_prefix_push("SBI_030");
+
+	for_each_present_cpu(cpu) {
+		if (!cpu_has_extension(cpu, ISA_SSTC)) {
+			has = false;
+			break;
+		}
+	}
+
+	if (has)
+		report_skip("Sstc is present, TIME is not mandated");
+	else
+		report(sbi_probe(SBI_EXT_TIME), "TIME extension is implemented");
+
+	report_prefix_pop();
+}
+
+static void check_sbi_040(void)
+{
+	int cpu;
+	bool has = true;
+
+	report_prefix_push("SBI_040");
+
+	for_each_present_cpu(cpu) {
+		if (!cpu_has_extension(cpu, ISA_SSAIA)) {
+			has = false;
+			break;
+		}
+		if (cpus[cpu].imsic_pa == INVALID_PHYS_ADDR) {
+			has = false;
+			break;
+		}
+	}
+
+	if (has)
+		report_skip("IMSIC is present, IPI is not mandated");
+	else
+		report(sbi_probe(SBI_EXT_IPI), "IPI extension is implemented");
+
+	report_prefix_pop();
+}
+
+static void check_sbi_050(void)
+{
+	int cpu;
+	bool has = true;
+
+	report_prefix_push("SBI_050");
+
+	for_each_present_cpu(cpu) {
+		if (!cpu_has_extension(cpu, ISA_SSAIA)) {
+			has = false;
+			break;
+		}
+		if (cpus[cpu].imsic_pa == INVALID_PHYS_ADDR) {
+			has = false;
+			break;
+		}
+	}
+
+	if (has)
+		report_skip("IMSIC is present, RFENCE is not mandated");
+	else
+		report(sbi_probe(SBI_EXT_RFNC), "RFENCE extension is implemented");
+
+	report_prefix_pop();
+}
+
+static void check_sbi_060(void)
+{
+	int cpu;
+	bool has = true;
+
+	report_prefix_push("SBI_060");
+
+	for_each_present_cpu(cpu) {
+		if (!cpu_has_extension(cpu, ISA_SSCSRIND)) {
+			has = false;
+			break;
+		}
+		if (!cpu_has_extension(cpu, ISA_SSCCFG)) {
+			has = false;
+			break;
+		}
+	}
+
+	if (has) {
+		/*
+		 * TODO: OE_SBI_060_010 also requires Smcsrind and Smcdeleg,
+		 * but they are M-mode extensions and not present in the S-mode
+		 * ISA string (DT or RHCT). Revisit if BRS clarifies how to detect them.
+		 */
+		report_skip("Sscsrind and Ssccfg are present, PMU is not mandated");
+	} else {
+		report(sbi_probe(SBI_EXT_PMU), "PMU extension is implemented");
+	}
+
+	report_prefix_pop();
+}
+
+static void check_sbi_070(void)
+{
+	report_prefix_push("SBI_070");
+
+	if (dt_available()) {
+		report_skip("ACPI not available, DBCN is not mandated");
+	} else {
+#ifdef CONFIG_EFI
+		struct spcr_descriptor *spcr;
+
+		spcr = find_acpi_table_addr(SPCR_SIGNATURE);
+		if (spcr == NULL) {
+			report_skip("SPCR is not present, DBCN is not mandated");
+		} else {
+			if (spcr->interface_type == 0x15) {
+				report(sbi_probe(SBI_EXT_DBCN), "DBCN extension is implemented");
+			} else {
+				report_skip("SPCR interface type is %#x (not 0x15), DBCN is not mandated",
+					    spcr->interface_type);
+			}
+		}
+#else
+		report_skip("ACPI not available, DBCN is not mandated");
+#endif
+	}
+
+	report_prefix_pop();
+}
+
+int main(int argc, char **argv)
+{
+	report_prefix_push("riscv-brs");
+
+	check_sbi_010();
+	check_sbi_020();
+	check_sbi_030();
+	check_sbi_040();
+	check_sbi_050();
+	check_sbi_060();
+	check_sbi_070();
+
+	return report_summary();
+}
diff --git a/riscv/unittests.cfg b/riscv/unittests.cfg
index 9e9f58d9..990f03db 100644
--- a/riscv/unittests.cfg
+++ b/riscv/unittests.cfg
@@ -22,3 +22,7 @@ groups = sbi
 [dbltrp]
 file = isa-dbltrp.flat
 groups = isa sbi
+
+[riscv-brs]
+file = riscv-brs.flat
+groups = riscv-brs nodefault
-- 
2.47.3




More information about the kvm-riscv mailing list