[PATCH RFC 06/11] riscv: suspend: add Smstateen CSRs save/restore

Max Hsu
2024-03-29T09:28:02Z

From Smstateen extension: the values of the [h/s]stateen CSRs will
be lost when entering a non-retentive idle state.
Therefore, these CSRs values need to be restored to ensure that
the corresponding functionality remains enabled.

Signed-off-by: Max Hsu <max.hsu@sifive.com>
---
arch/riscv/include/asm/suspend.h | 6 ++++++
arch/riscv/kernel/suspend.c | 18 ++++++++++++++++++
2 files changed, 24 insertions(+)

diff --git a/arch/riscv/include/asm/suspend.h b/arch/riscv/include/asm/suspend.h
index 4718096fa5e3..2ecace073869 100644
--- a/arch/riscv/include/asm/suspend.h
+++ b/arch/riscv/include/asm/suspend.h
@@ -17,6 +17,12 @@ struct suspend_context {
unsigned long envcfg;
unsigned long tvec;
unsigned long ie;
+#if __riscv_xlen < 64
+ unsigned long hstateen0h;
+#endif
+ unsigned long hstateen0;
+ unsigned long sstateen0;
+
#ifdef CONFIG_MMU
unsigned long satp;
#endif
diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c
index 8a327b485b90..a086da222872 100644
--- a/arch/riscv/kernel/suspend.c
+++ b/arch/riscv/kernel/suspend.c
@@ -19,6 +19,15 @@ void suspend_save_csrs(struct suspend_context *context)
context->envcfg = csr_read(CSR_ENVCFG);
context->tvec = csr_read(CSR_TVEC);
context->ie = csr_read(CSR_IE);
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) {
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_h)) {
+#if __riscv_xlen < 64
+ context->hstateen0h = csr_read(CSR_HSTATEEN0H);
+#endif
+ context->hstateen0 = csr_read(CSR_HSTATEEN0);
+ }
+ context->sstateen0 = csr_read(CSR_SSTATEEN0);
+ }

/*
* No need to save/restore IP CSR (i.e. MIP or SIP) because:
@@ -42,6 +51,15 @@ void suspend_restore_csrs(struct suspend_context *context)
csr_write(CSR_ENVCFG, context->envcfg);
csr_write(CSR_TVEC, context->tvec);
csr_write(CSR_IE, context->ie);
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SMSTATEEN)) {
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_h)) {
+#if __riscv_xlen < 64
+ csr_write(CSR_HSTATEEN0H, context->hstateen0h);
+#endif
+ csr_write(CSR_HSTATEEN0, context->hstateen0);
+ }
+ csr_write(CSR_SSTATEEN0, context->sstateen0);
+ }

#ifdef CONFIG_MMU
csr_write(CSR_SATP, context->satp);

--
2.43.2