[PATCH] lib: sbi: Detect support of entropy source
Xiang W
wxjstz at 126.com
Mon Sep 29 19:26:45 PDT 2025
Detect if entropy source, Zkr extension, is supported
by the HART.
Signed-off-by: Xiang W <wxjstz at 126.com>
---
include/sbi/riscv_encoding.h | 3 +++
lib/sbi/sbi_hart.c | 14 ++++++++++++++
2 files changed, 17 insertions(+)
diff --git a/include/sbi/riscv_encoding.h b/include/sbi/riscv_encoding.h
index 40a854e3..8ca607c0 100644
--- a/include/sbi/riscv_encoding.h
+++ b/include/sbi/riscv_encoding.h
@@ -261,6 +261,9 @@
#define CSR_FRM 0x002
#define CSR_FCSR 0x003
+/* User entropy source */
+#define CSR_SEED 0x015
+
/* User Counters/Timers */
#define CSR_CYCLE 0xc00
#define CSR_TIME 0xc01
diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
index 1b50f671..ee655375 100644
--- a/lib/sbi/sbi_hart.c
+++ b/lib/sbi/sbi_hart.c
@@ -983,6 +983,20 @@ __pmp_skip:
#undef __check_ext_csr
+#define __check_ext_csr_w(__base_priv, __csr, __val, __ext) \
+ if (hfeatures->priv_version >= __base_priv) { \
+ csr_write_allowed(__csr, &trap, __val); \
+ if (!trap.cause) \
+ __sbi_hart_update_extension(hfeatures, \
+ __ext, true); \
+ }
+
+ /* Detect if hart supports Zkr (entropy source) */
+ __check_ext_csr_w(SBI_HART_PRIV_VER_UNKNOWN,
+ CSR_SEED, 0, SBI_HART_EXT_ZKR);
+
+#undef __check_ext_csr_w
+
#define __check_csr_existence(__csr, __csr_id) \
csr_read_allowed(__csr, &trap); \
if (!trap.cause) \
--
2.47.3
More information about the opensbi
mailing list