Skip to contentSkip to conversation

SBI SRST extension support

PATCH Superseded Anup Patel 4 participants 6 messages Mar 15, 2021 – Mar 17, 2021 RISC-V

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

v6 cover SBI SRST extension support

This series adds SBI SRST extension support to Linux RISC-V.

These patches can be found in riscv_sbi_srst_v6 branch at:
https://github.com/avpatel/linux

Changes since v5:
- Factored-out pr_info() related change into separate patch
- Added cover letter

Changes since v4:
- We should compare both major and minor number to ensure that SBI spec version is 0.3 (or above) for detecting SRST extension.

Changes since v3:
- Rebased on Linux-5.12-rc1
- Check SBI spec version when probing for SRST extension

Changes since v2:
- Rebased on Linux-5.10-rc5
- Updated patch as-per SBI SRST extension available in the latest SBI v0.3-draft specification

Changes since v1:
- Updated patch as-per latest SBI SRST extension draft spec where we have only one SBI call with "reset_type" parameter

Anup Patel (2):
RISC-V: Don't print SBI version for all detected extensions
RISC-V: Use SBI SRST extension when available

 arch/riscv/include/asm/sbi.h | 24 +++++++++++++++++++++
 arch/riscv/kernel/sbi.c      | 41 +++++++++++++++++++++++++++++++++---
 2 files changed, 62 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v6 1/2 RISC-V: Don't print SBI version for all detected extensions

The sbi_init() already prints SBI version before detecting various SBI extensions so we don't need to print SBI version for all detected SBI extensions.

---
 arch/riscv/kernel/sbi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/kernel/sbi.c +3 -3
index f4a7db3d309e..c0dcebdd30ec 100644
@@ -577,19 +577,19 @@ void __init sbi_init(void)
 			sbi_get_firmware_id(), sbi_get_firmware_version());
 		if (sbi_probe_extension(SBI_EXT_TIME) > 0) {
 			__sbi_set_timer = __sbi_set_timer_v02;
-			pr_info("SBI v0.2 TIME extension detected\n");
+			pr_info("SBI TIME extension detected\n");
 		} else {
 			__sbi_set_timer = __sbi_set_timer_v01;
 		}
 		if (sbi_probe_extension(SBI_EXT_IPI) > 0) {
 			__sbi_send_ipi	= __sbi_send_ipi_v02;
-			pr_info("SBI v0.2 IPI extension detected\n");
+			pr_info("SBI IPI extension detected\n");
 		} else {
 			__sbi_send_ipi	= __sbi_send_ipi_v01;
 		}
 		if (sbi_probe_extension(SBI_EXT_RFENCE) > 0) {
 			__sbi_rfence	= __sbi_rfence_v02;
-			pr_info("SBI v0.2 RFENCE extension detected\n");
+			pr_info("SBI RFENCE extension detected\n");
 		} else {
 			__sbi_rfence	= __sbi_rfence_v01;
 		}
Signature · 1 line
2.25.1
Reviewed-by on v6 1/2
#2 lore raw

Re: [PATCH v6 1/2] RISC-V: Don't print SBI version for all detected extensions

Quoted 41 lines · Anup Patel

The sbi_init() already prints SBI version before detecting various SBI extensions so we don't need to print SBI version for all detected SBI extensions.

---
 arch/riscv/kernel/sbi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/kernel/sbi.c +3 -4
index f4a7db3d309e..c0dcebdd30ec 100644
@@ -577,19 +577,19 @@ void __init sbi_init(void)
                        sbi_get_firmware_id(), sbi_get_firmware_version());
                if (sbi_probe_extension(SBI_EXT_TIME) > 0) {
                        __sbi_set_timer = __sbi_set_timer_v02;
-                       pr_info("SBI v0.2 TIME extension detected\n");
+                       pr_info("SBI TIME extension detected\n");
                } else {
                        __sbi_set_timer = __sbi_set_timer_v01;
                }
                if (sbi_probe_extension(SBI_EXT_IPI) > 0) {
                        __sbi_send_ipi  = __sbi_send_ipi_v02;
-                       pr_info("SBI v0.2 IPI extension detected\n");
+                       pr_info("SBI IPI extension detected\n");
                } else {
                        __sbi_send_ipi  = __sbi_send_ipi_v01;
                }
                if (sbi_probe_extension(SBI_EXT_RFENCE) > 0) {
                        __sbi_rfence    = __sbi_rfence_v02;
-                       pr_info("SBI v0.2 RFENCE extension detected\n");
+                       pr_info("SBI RFENCE extension detected\n");
                } else {
                        __sbi_rfence    = __sbi_rfence_v01;
                }
--

2.25.1

Quoted 4 lines

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

Signature · 7 lines
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer
#3 lore raw

Re: [PATCH v6 1/2] RISC-V: Don't print SBI version for all detected extensions

In reply to Anup Patel
Quoted 26 lines · Anup Patel

The sbi_init() already prints SBI version before detecting various SBI extensions so we don't need to print SBI version for all detected SBI extensions.

---
 arch/riscv/kernel/sbi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/kernel/sbi.c +2 -2
index f4a7db3d309e..c0dcebdd30ec 100644
@@ -577,19 +577,19 @@ void __init sbi_init(void)
 			sbi_get_firmware_id(), sbi_get_firmware_version());
 		if (sbi_probe_extension(SBI_EXT_TIME) > 0) {
 			__sbi_set_timer = __sbi_set_timer_v02;
-			pr_info("SBI v0.2 TIME extension detected\n");
+			pr_info("SBI TIME extension detected\n");
 		} else {
 			__sbi_set_timer = __sbi_set_timer_v01;
 		}
 		if (sbi_probe_extension(SBI_EXT_IPI) > 0) {
 			__sbi_send_ipi	= __sbi_send_ipi_v02;
-			pr_info("SBI v0.2 IPI extension detected\n");
+			pr_info("SBI IPI extension detected\n");

arch/riscv/kernel/sbi.c void __init sbi_init(void) · line 595

Quoted 10 lines
arch/riscv/kernel/sbi.c +1 -1
 		} else {
 			__sbi_send_ipi	= __sbi_send_ipi_v01;
 		}
 		if (sbi_probe_extension(SBI_EXT_RFENCE) > 0) {
 			__sbi_rfence	= __sbi_rfence_v02;
-			pr_info("SBI v0.2 RFENCE extension detected\n");
+			pr_info("SBI RFENCE extension detected\n");
 		} else {
 			__sbi_rfence	= __sbi_rfence_v01;
 		}

Thanks. I'm just putting this one on for-next so you don't have to carry around the diff.

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

#4 lore raw

Re: [PATCH v6 1/2] RISC-V: Don't print SBI version for all detected extensions

Quoted 33 lines · Palmer Dabbelt
Quoted 31 lines · Anup Patel

The sbi_init() already prints SBI version before detecting various SBI extensions so we don't need to print SBI version for all detected SBI extensions.

---
 arch/riscv/kernel/sbi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/kernel/sbi.c +2 -2
index f4a7db3d309e..c0dcebdd30ec 100644
@@ -577,19 +577,19 @@ void __init sbi_init(void)
                      sbi_get_firmware_id(), sbi_get_firmware_version());
              if (sbi_probe_extension(SBI_EXT_TIME) > 0) {
                      __sbi_set_timer = __sbi_set_timer_v02;
-                     pr_info("SBI v0.2 TIME extension detected\n");
+                     pr_info("SBI TIME extension detected\n");
              } else {
                      __sbi_set_timer = __sbi_set_timer_v01;
              }
              if (sbi_probe_extension(SBI_EXT_IPI) > 0) {
                      __sbi_send_ipi  = __sbi_send_ipi_v02;
-                     pr_info("SBI v0.2 IPI extension detected\n");
+                     pr_info("SBI IPI extension detected\n");
              } else {
                      __sbi_send_ipi  = __sbi_send_ipi_v01;
              }
              if (sbi_probe_extension(SBI_EXT_RFENCE) > 0) {
                      __sbi_rfence    = __sbi_rfence_v02;
Quoted 8 lines

arch/riscv/kernel/sbi.c void __init sbi_init(void) · line 595

Quoted 5 lines
arch/riscv/kernel/sbi.c +1 -1
-                     pr_info("SBI v0.2 RFENCE extension detected\n");
+                     pr_info("SBI RFENCE extension detected\n");
              } else {
                      __sbi_rfence    = __sbi_rfence_v01;
              }

Thanks. I'm just putting this one on for-next so you don't have to carry around the diff.

Thanks Palmer.

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

author
#5 lore raw

PATCH v6 2/2 RISC-V: Use SBI SRST extension when available

In reply to Anup Patel

The SBI SRST extension provides a standard way to poweroff and reboot the system irrespective to whether Linux RISC-V S-mode is running natively (HS-mode) or inside Guest/VM (VS-mode).

The SBI SRST extension is available in latest SBI v0.3-draft specification at: https://github.com/riscv/riscv-sbi-doc.

This patch extends Linux RISC-V SBI implementation to detect and use SBI SRST extension.

---
 arch/riscv/include/asm/sbi.h | 24 ++++++++++++++++++++++++
 arch/riscv/kernel/sbi.c      | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)
arch/riscv/include/asm/sbi.h +24 -0
index 99895d9c3bdd..79fa9f28b786 100644
@@ -27,6 +27,7 @@ enum sbi_ext_id {
 	SBI_EXT_IPI = 0x735049,
 	SBI_EXT_RFENCE = 0x52464E43,
 	SBI_EXT_HSM = 0x48534D,
+	SBI_EXT_SRST = 0x53525354,
 };
 
 enum sbi_ext_base_fid {
@@ -70,6 +71,21 @@ enum sbi_hsm_hart_status {
 	SBI_HSM_HART_STATUS_STOP_PENDING,
 };
 
+enum sbi_ext_srst_fid {
+	SBI_EXT_SRST_RESET = 0,
+};
+
+enum sbi_srst_reset_type {
+	SBI_SRST_RESET_TYPE_SHUTDOWN = 0,
+	SBI_SRST_RESET_TYPE_COLD_REBOOT,
+	SBI_SRST_RESET_TYPE_WARM_REBOOT,
+};
+
+enum sbi_srst_reset_reason {
+	SBI_SRST_RESET_REASON_NONE = 0,
+	SBI_SRST_RESET_REASON_SYS_FAILURE,
+};
+
 #define SBI_SPEC_VERSION_DEFAULT	0x1
 #define SBI_SPEC_VERSION_MAJOR_SHIFT	24
 #define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
@@ -145,6 +161,14 @@ static inline unsigned long sbi_minor_version(void)
 	return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
 }
 
+/* Make SBI version */
+static inline unsigned long sbi_mk_version(unsigned long major,
+					    unsigned long minor)
+{
+	return ((major & SBI_SPEC_VERSION_MAJOR_MASK) <<
+		SBI_SPEC_VERSION_MAJOR_SHIFT) | minor;
+}
+
 int sbi_err_map_linux_errno(int err);
 #else /* CONFIG_RISCV_SBI */
 static inline int sbi_remote_fence_i(const unsigned long *hart_mask) { return -1; }
arch/riscv/kernel/sbi.c +35 -0
index c0dcebdd30ec..e94ea8053984 100644
@@ -7,6 +7,7 @@
 
 #include <linux/init.h>
 #include <linux/pm.h>
+#include <linux/reboot.h>
 #include <asm/sbi.h>
 #include <asm/smp.h>
 
@@ -501,6 +502,32 @@ int sbi_remote_hfence_vvma_asid(const unsigned long *hart_mask,
 }
 EXPORT_SYMBOL(sbi_remote_hfence_vvma_asid);
 
+static void sbi_srst_reset(unsigned long type, unsigned long reason)
+{
+	sbi_ecall(SBI_EXT_SRST, SBI_EXT_SRST_RESET, type, reason,
+		  0, 0, 0, 0);
+	pr_warn("%s: type=0x%lx reason=0x%lx failed\n",
+		__func__, type, reason);
+}
+
+static int sbi_srst_reboot(struct notifier_block *this,
+			   unsigned long mode, void *cmd)
+{
+	sbi_srst_reset((mode == REBOOT_WARM || mode == REBOOT_SOFT) ?
+		       SBI_SRST_RESET_TYPE_WARM_REBOOT :
+		       SBI_SRST_RESET_TYPE_COLD_REBOOT,
+		       SBI_SRST_RESET_REASON_NONE);
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block sbi_srst_reboot_nb;
+
+static void sbi_srst_power_off(void)
+{
+	sbi_srst_reset(SBI_SRST_RESET_TYPE_SHUTDOWN,
+		       SBI_SRST_RESET_REASON_NONE);
+}
+
 /**
  * sbi_probe_extension() - Check if an SBI extension ID is supported or not.
  * @extid: The extension ID to be probed.
@@ -593,6 +620,14 @@ void __init sbi_init(void)
 		} else {
 			__sbi_rfence	= __sbi_rfence_v01;
 		}
+		if ((sbi_spec_version >= sbi_mk_version(0, 3)) &&
+		    (sbi_probe_extension(SBI_EXT_SRST) > 0)) {
+			pr_info("SBI SRST extension detected\n");
+			pm_power_off = sbi_srst_power_off;
+			sbi_srst_reboot_nb.notifier_call = sbi_srst_reboot;
+			sbi_srst_reboot_nb.priority = 192;
+			register_restart_handler(&sbi_srst_reboot_nb);
+		}
 	} else {
 		__sbi_set_timer = __sbi_set_timer_v01;
 		__sbi_send_ipi	= __sbi_send_ipi_v01;
Signature · 1 line
2.25.1