Skip to contentSkip to conversation

lib: sbi: Add Smrnmi extension detection

PATCH In review Nylon Chen 2 participants 3 messages Apr 28, 2025 – May 20, 2025 RISC-V

lore
Share & tools
lore

Nylon Chen

author
#0 lore raw

PATCH 1/1 lib: sbi: Add Smrnmi extension detection

The commit adds detection and initialization of the Smrnmi extension in the SBI hart initialization code.

If the extension is detected,
it sets the NMIE bit in the mnstatus CSR to enable RNMIs out of reset.

---
 include/sbi/riscv_encoding.h | 10 ++++++++++
 include/sbi/sbi_hart.h       |  2 ++
 lib/sbi/sbi_hart.c           |  6 ++++++
 3 files changed, 18 insertions(+)
include/sbi/riscv_encoding.h +10 -0
index 2082783..93228ce 100644
@@ -215,6 +215,10 @@
 
 #endif
 
+#define MNSTATUS_NMIE			(_UL(0x8))
+#define MNSTATUS_MNPV			(_UL(0x80))
+#define MNSTATUS_MNPP			(_UL(0x1800))
+
 #define MHPMEVENT_SSCOF_MASK		_ULL(0xFF00000000000000)
 
 #define ENVCFG_STCE			(_ULL(1) << 63)
@@ -770,6 +774,12 @@
 #define CSR_MSTATEEN3			0x30F
 #define CSR_MSTATEEN3H			0x31F
 
+/* Smrnmi extension registers */
+#define CSR_MNSCRATCH			0x740
+#define CSR_MNEPC			0x741
+#define CSR_MNCAUSE			0x742
+#define CSR_MNSTATUS			0x744
+
 /* Machine-Level High-Half CSRs (AIA) */
 #define CSR_MIDELEGH			0x313
 #define CSR_MIEH			0x314
include/sbi/sbi_hart.h +2 -0
index c3a7feb..07cbead 100644
@@ -79,6 +79,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SMCTR,
 	/** HART has CTR S-mode CSRs */
 	SBI_HART_EXT_SSCTR,
+	/** HART has Smrnmi extension */
+	SBI_HART_EXT_SMRNMI,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +6 -0
index eefb03f..66d5d02 100644
@@ -65,6 +65,11 @@ static void mstatus_init(struct sbi_scratch *scratch)
 	/* All programmable counters will start running at runtime after S-mode request */
 	if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11)
 		csr_write(CSR_MCOUNTINHIBIT, 0xFFFFFFF8);
+	/* Detect Resumable Non-Maskable Interrupts
+	 * If it exists we must enable it before all traps.
+	 */
+	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMRNMI))
+		csr_set(CSR_MNSTATUS, MNSTATUS_NMIE);
 
 	/**
 	 * The mhpmeventn[h] CSR should be initialized with interrupt disabled
@@ -695,6 +700,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
 	__SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
 	__SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
+	__SBI_HART_EXT_DATA(smrnmi, SBI_HART_EXT_SMRNMI),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
Signature · 7 lines
2.39.3


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#1 lore raw

Hi all, just a gentle ping on this PR that was submitted ago. I'm wondering if there's any feedback? Thanks!

Nylon Chen <[email protected]> 於 2025年4月28日 週一 下午5:25寫道:

Quoted 85 lines

The commit adds detection and initialization of the Smrnmi extension in the SBI hart initialization code.

If the extension is detected,
it sets the NMIE bit in the mnstatus CSR to enable RNMIs out of reset.

---
 include/sbi/riscv_encoding.h | 10 ++++++++++
 include/sbi/sbi_hart.h       |  2 ++
 lib/sbi/sbi_hart.c           |  6 ++++++
 3 files changed, 18 insertions(+)
include/sbi/riscv_encoding.h +10 -0
index 2082783..93228ce 100644
@@ -215,6 +215,10 @@
 
 #endif
 
+#define MNSTATUS_NMIE                  (_UL(0x8))
+#define MNSTATUS_MNPV                  (_UL(0x80))
+#define MNSTATUS_MNPP                  (_UL(0x1800))
+
 #define MHPMEVENT_SSCOF_MASK           _ULL(0xFF00000000000000)
 
 #define ENVCFG_STCE                    (_ULL(1) << 63)
@@ -770,6 +774,12 @@
 #define CSR_MSTATEEN3                  0x30F
 #define CSR_MSTATEEN3H                 0x31F
 
+/* Smrnmi extension registers */
+#define CSR_MNSCRATCH                  0x740
+#define CSR_MNEPC                      0x741
+#define CSR_MNCAUSE                    0x742
+#define CSR_MNSTATUS                   0x744
+
 /* Machine-Level High-Half CSRs (AIA) */
 #define CSR_MIDELEGH                   0x313
 #define CSR_MIEH                       0x314
include/sbi/sbi_hart.h +2 -0
index c3a7feb..07cbead 100644
@@ -79,6 +79,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SMCTR,
        /** HART has CTR S-mode CSRs */
        SBI_HART_EXT_SSCTR,
+       /** HART has Smrnmi extension */
+       SBI_HART_EXT_SMRNMI,
 
        /** Maximum index of Hart extension */
        SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +6 -1
index eefb03f..66d5d02 100644
@@ -65,6 +65,11 @@ static void mstatus_init(struct sbi_scratch *scratch)
        /* All programmable counters will start running at runtime after S-mode request */
        if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11)
                csr_write(CSR_MCOUNTINHIBIT, 0xFFFFFFF8);
+       /* Detect Resumable Non-Maskable Interrupts
+        * If it exists we must enable it before all traps.
+        */
+       if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMRNMI))
+               csr_set(CSR_MNSTATUS, MNSTATUS_NMIE);
 
        /**
         * The mhpmeventn[h] CSR should be initialized with interrupt disabled
@@ -695,6 +700,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
        __SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
        __SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
        __SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
+       __SBI_HART_EXT_DATA(smrnmi, SBI_HART_EXT_SMRNMI),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
--

2.39.3

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw
In reply to Nylon Chen
Quoted 63 lines · Nylon Chen

The commit adds detection and initialization of the Smrnmi extension in the SBI hart initialization code.

If the extension is detected,
it sets the NMIE bit in the mnstatus CSR to enable RNMIs out of reset.

---
 include/sbi/riscv_encoding.h | 10 ++++++++++
 include/sbi/sbi_hart.h       |  2 ++
 lib/sbi/sbi_hart.c           |  6 ++++++
 3 files changed, 18 insertions(+)
include/sbi/riscv_encoding.h +10 -0
index 2082783..93228ce 100644
@@ -215,6 +215,10 @@
 
 #endif
 
+#define MNSTATUS_NMIE                  (_UL(0x8))
+#define MNSTATUS_MNPV                  (_UL(0x80))
+#define MNSTATUS_MNPP                  (_UL(0x1800))
+
 #define MHPMEVENT_SSCOF_MASK           _ULL(0xFF00000000000000)
 
 #define ENVCFG_STCE                    (_ULL(1) << 63)
@@ -770,6 +774,12 @@
 #define CSR_MSTATEEN3                  0x30F
 #define CSR_MSTATEEN3H                 0x31F
 
+/* Smrnmi extension registers */
+#define CSR_MNSCRATCH                  0x740
+#define CSR_MNEPC                      0x741
+#define CSR_MNCAUSE                    0x742
+#define CSR_MNSTATUS                   0x744
+
 /* Machine-Level High-Half CSRs (AIA) */
 #define CSR_MIDELEGH                   0x313
 #define CSR_MIEH                       0x314
include/sbi/sbi_hart.h +2 -0
index c3a7feb..07cbead 100644
@@ -79,6 +79,8 @@ enum sbi_hart_extensions {
        SBI_HART_EXT_SMCTR,
        /** HART has CTR S-mode CSRs */
        SBI_HART_EXT_SSCTR,
+       /** HART has Smrnmi extension */
+       SBI_HART_EXT_SMRNMI,
 
        /** Maximum index of Hart extension */
        SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +0 -0
index eefb03f..66d5d02 100644

lib/sbi/sbi_hart.c static void mstatus_init(struct sbi_scratch *scratch) · line 72

Quoted 8 lines
lib/sbi/sbi_hart.c +5 -0
@@ -65,6 +65,11 @@ static void mstatus_init(struct sbi_scratch *scratch)
        /* All programmable counters will start running at runtime after S-mode request */
        if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11)
                csr_write(CSR_MCOUNTINHIBIT, 0xFFFFFFF8);
+       /* Detect Resumable Non-Maskable Interrupts
+        * If it exists we must enable it before all traps.
+        */
+       if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMRNMI))
+               csr_set(CSR_MNSTATUS, MNSTATUS_NMIE);

We can't blindy enable non-maskable interrupts without any NMI handler in OpenSBI.

This patch needs to be converted into a series with
at least following patches:
PATCH1: Add Smrnmi extension detection (this patch without the mstatus_init() change)
PATCH2: Add common NMI trap handler in fw_base.S
PATCH3: Enable NMI handling (the mstatus_init() change from this patch plus set the common NMI trap handler in platform specific way)

Regards,
Anup

Quoted 19 lines
lib/sbi/sbi_hart.c +1 -1
        /**
         * The mhpmeventn[h] CSR should be initialized with interrupt disabled
@@ -695,6 +700,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
        __SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
        __SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
        __SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
+       __SBI_HART_EXT_DATA(smrnmi, SBI_HART_EXT_SMRNMI),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
--

2.39.3

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi