Skip to contentSkip to conversation

riscv: Introduce system suspend support

RFC PATCH Superseded Andrew Jones 4 participants 10 messages Jan 6, 2023 – Jan 18, 2023 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

RFC PATCH 1/1 riscv: sbi: Introduce system suspend support

In reply to Andrew Jones

When the SUSP SBI extension is present it implies that the standard "suspend to RAM" type is available. Wire it up to the generic platform suspend support, also applying the already present support for non-retentive CPU suspend. When the kernel is built with CONFIG_SUSPEND, one can do 'echo mem > /sys/power/state' to suspend. Resumption will occur when a platform-specific wake-up event arrives.

---
 arch/riscv/Kconfig           |  5 ++++-
 arch/riscv/include/asm/sbi.h |  9 ++++++++
 arch/riscv/kernel/suspend.c  | 41 ++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +4 -1
index e2b656043abf..a53d94c1953e 100644
@@ -52,7 +52,7 @@ config RISCV
 	select CLONE_BACKWARDS
 	select CLINT_TIMER if !MMU
 	select COMMON_CLK
-	select CPU_PM if CPU_IDLE
+	select CPU_PM if (SUSPEND || CPU_IDLE)
 	select EDAC_SUPPORT
 	select GENERIC_ARCH_TOPOLOGY
 	select GENERIC_ATOMIC64 if !64BIT
@@ -686,6 +686,9 @@ config PORTABLE
 	select OF
 	select MMU
 
+config ARCH_SUSPEND_POSSIBLE
+	def_bool y
+
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/sbi.h +9 -0
index 4ca7fbacff42..9834ba4ce3e4 100644
@@ -29,6 +29,7 @@ enum sbi_ext_id {
 	SBI_EXT_RFENCE = 0x52464E43,
 	SBI_EXT_HSM = 0x48534D,
 	SBI_EXT_SRST = 0x53525354,
+	SBI_EXT_SUSP = 0x53555350,
 	SBI_EXT_PMU = 0x504D55,
 
 	/* Experimentals extensions must lie within this range */
@@ -113,6 +114,14 @@ enum sbi_srst_reset_reason {
 	SBI_SRST_RESET_REASON_SYS_FAILURE,
 };
 
+enum sbi_ext_susp_fid {
+	SBI_EXT_SUSP_SUSPEND = 0,
+};
+
+enum sbi_ext_susp_sleep_type {
+	SBI_SUSP_SLEEP_TYPE_SUSPEND = 0,
+};
+
 enum sbi_ext_pmu_fid {
 	SBI_EXT_PMU_NUM_COUNTERS = 0,
 	SBI_EXT_PMU_COUNTER_GET_INFO,
arch/riscv/kernel/suspend.c +41 -0

52 lines not on this page · raw message

Signature · 7 lines
2.39.0


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

cover riscv: Introduce system suspend support

Booting with an OpenSBI including the RFC series[1] implementing the draft proposal for SBI system suspend[2] we can add system support to Linux. This support implements "suspend-to-RAM", which means when a kernel is built with CONFIG_SUSPEND 'echo mem > /sys/power/state' will initiate a suspension.

This has only been tested on QEMU using the OpenSBI system suspend test. The test just waits 5 seconds and then resumes. To truly use system suspend a platform must have a low-level firmware implementation and provide at least one wake-up event, such as from a wakeup-capable RTC alarm, to resume.

[1] https://github.com/jones-drew/opensbi/commits/susp-rfc

    Posting: http://lists.infradead.org/pipermail/opensbi/2023-January/004091.html

[2] https://github.com/jones-drew/riscv-sbi-doc/commit/d9e43e9a938fc3eb510e023c3f352462876f7785
Posting: https://lists.riscv.org/g/tech-prs/message/75

This patch is also available at
https://github.com/jones-drew/linux/commits/riscv/sbi-susp-rfc

Andrew Jones (1):
riscv: sbi: Introduce system suspend support

 arch/riscv/Kconfig           |  5 ++++-
 arch/riscv/include/asm/sbi.h |  9 ++++++++
 arch/riscv/kernel/suspend.c  | 41 ++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
Signature · 7 lines
2.39.0


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

RE: [RFC PATCH 1/1] riscv: sbi: Introduce system suspend support

In reply to Andrew Jones
Quoted 11 lines

source "kernel/power/Kconfig"

arch/riscv/include/asm/sbi.h index renamed from arch/riscv/include/asm/sbi.h +0 -0

4ca7fbacff42..9834ba4ce3e4 100644

arch/riscv/include/asm/sbi.h +0 -0
@@ -29,6 +29,7 @@ enum sbi_ext_id {
 	SBI_EXT_RFENCE = 0x52464E43,
 	SBI_EXT_HSM = 0x48534D,
 	SBI_EXT_SRST = 0x53525354,

arch/riscv/include/asm/sbi.h enum sbi_ext_id {

Quoted 10 lines
arch/riscv/include/asm/sbi.h +4 -0
+	SBI_EXT_SUSP = 0x53555350,
 	SBI_EXT_PMU = 0x504D55,
 
 	/* Experimentals extensions must lie within this range */ @@ -113,6
+114,14 @@ enum sbi_srst_reset_reason {
 	SBI_SRST_RESET_REASON_SYS_FAILURE,
 };
 
+enum sbi_ext_susp_fid {
+	SBI_EXT_SUSP_SUSPEND = 0,

Macro name with "*_SYSTEM_SUSPEND" is better? Avoid confusing with CPU suspend.

arch/riscv/include/asm/sbi.h enum sbi_srst_reset_reason { · line 122

Quoted 4 lines
arch/riscv/include/asm/sbi.h +4 -0
+};
+
+enum sbi_ext_susp_sleep_type {
+	SBI_SUSP_SLEEP_TYPE_SUSPEND = 0,

This should be SBI_SUSP_SLEEP_TYPE_SUSPEND_TO_RAM?

Regards
Ley Foon

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

#3 lore raw

Re: [RFC PATCH 1/1] riscv: sbi: Introduce system suspend support

Quoted 19 lines · Leyfoon Tan
Quoted 19 lines

source "kernel/power/Kconfig"

arch/riscv/include/asm/sbi.h index renamed from arch/riscv/include/asm/sbi.h +0 -0

4ca7fbacff42..9834ba4ce3e4 100644

arch/riscv/include/asm/sbi.h +2 -0
@@ -29,6 +29,7 @@ enum sbi_ext_id {
 	SBI_EXT_RFENCE = 0x52464E43,
 	SBI_EXT_HSM = 0x48534D,
 	SBI_EXT_SRST = 0x53525354,
+	SBI_EXT_SUSP = 0x53555350,
 	SBI_EXT_PMU = 0x504D55,
 
 	/* Experimentals extensions must lie within this range */ @@ -113,6
+114,14 @@ enum sbi_srst_reset_reason {
 	SBI_SRST_RESET_REASON_SYS_FAILURE,
 };
Quoted 4 lines

arch/riscv/include/asm/sbi.h enum sbi_srst_reset_reason { · line 118

Quoted 2 lines
arch/riscv/include/asm/sbi.h +2 -0
+enum sbi_ext_susp_fid {
+	SBI_EXT_SUSP_SUSPEND = 0,

Macro name with "*_SYSTEM_SUSPEND" is better? Avoid confusing with CPU suspend.

The _SUSP_ part should cover that, but I'm OK with adding SYSTEM as well.

Quoted 6 lines

arch/riscv/include/asm/sbi.h enum sbi_srst_reset_reason { · line 122

Quoted 5 lines
arch/riscv/include/asm/sbi.h +4 -0
+};
+
+enum sbi_ext_susp_sleep_type {
+	SBI_SUSP_SLEEP_TYPE_SUSPEND = 0,

This should be SBI_SUSP_SLEEP_TYPE_SUSPEND_TO_RAM?

Yes, that would be better. I'll change that for the next version.

Thanks,
drew

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

#4 lore raw

Re: [RFC PATCH 1/1] riscv: sbi: Introduce system suspend support

In reply to Andrew Jones

Hey Drew!

Quoted 8 lines · Andrew Jones

When the SUSP SBI extension is present it implies that the standard "suspend to RAM" type is available. Wire it up to the generic platform suspend support, also applying the already present support for non-retentive CPU suspend. When the kernel is built with CONFIG_SUSPEND, one can do 'echo mem > /sys/power/state' to suspend. Resumption will occur when a platform-specific wake-up event arrives.

First things first, anything SBI depends on !m-mode, so you've gotta add some sort of gating unfortunately around those ECALLs. But I figure you may have already seen the build failures on patchwork for nommu?

Also, when there's an actual spec would you mind doing a Link: spec.pdf?

Quoted 20 lines
---
 arch/riscv/Kconfig           |  5 ++++-
 arch/riscv/include/asm/sbi.h |  9 ++++++++
 arch/riscv/kernel/suspend.c  | 41 ++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +1 -1
index e2b656043abf..a53d94c1953e 100644
@@ -52,7 +52,7 @@ config RISCV
 	select CLONE_BACKWARDS
 	select CLINT_TIMER if !MMU
 	select COMMON_CLK
-	select CPU_PM if CPU_IDLE
+	select CPU_PM if (SUSPEND || CPU_IDLE)
 	select EDAC_SUPPORT
 	select GENERIC_ARCH_TOPOLOGY
 	select GENERIC_ATOMIC64 if !64BIT

arch/riscv/Kconfig config PORTABLE · line 690

Quoted 5 lines
arch/riscv/Kconfig +2 -0
@@ -686,6 +686,9 @@ config PORTABLE
 	select OF
 	select MMU
 
+config ARCH_SUSPEND_POSSIBLE
+	def_bool y

Since the content you're adding depends on having an SBI extention, does this need to be s/y/RISCV_SBI/?

Quoted 48 lines
arch/riscv/Kconfig +0 -0
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/sbi.h +9 -0
index 4ca7fbacff42..9834ba4ce3e4 100644
@@ -29,6 +29,7 @@ enum sbi_ext_id {
 	SBI_EXT_RFENCE = 0x52464E43,
 	SBI_EXT_HSM = 0x48534D,
 	SBI_EXT_SRST = 0x53525354,
+	SBI_EXT_SUSP = 0x53555350,
 	SBI_EXT_PMU = 0x504D55,
 
 	/* Experimentals extensions must lie within this range */
@@ -113,6 +114,14 @@ enum sbi_srst_reset_reason {
 	SBI_SRST_RESET_REASON_SYS_FAILURE,
 };
 
+enum sbi_ext_susp_fid {
+	SBI_EXT_SUSP_SUSPEND = 0,
+};
+
+enum sbi_ext_susp_sleep_type {
+	SBI_SUSP_SLEEP_TYPE_SUSPEND = 0,
+};
+
 enum sbi_ext_pmu_fid {
 	SBI_EXT_PMU_NUM_COUNTERS = 0,
 	SBI_EXT_PMU_COUNTER_GET_INFO,
arch/riscv/kernel/suspend.c +4 -0
index 9ba24fb8cc93..bc26e9ae4782 100644
@@ -4,8 +4,12 @@
  * Copyright (c) 2022 Ventana Micro Systems Inc.
  */
 
+#define pr_fmt(fmt) "suspend: " fmt
+
 #include <linux/ftrace.h>
+#include <linux/suspend.h>
 #include <asm/csr.h>
+#include <asm/sbi.h>
 #include <asm/suspend.h>
 
 static void suspend_save_csrs(struct suspend_context *context)

arch/riscv/kernel/suspend.c int cpu_suspend(unsigned long arg, · line 91

Quoted 3 lines
arch/riscv/kernel/suspend.c +0 -0
@@ -85,3 +89,40 @@ int cpu_suspend(unsigned long arg,
 
 	return rc;
 }

And then from here down needs to be #ifdef RISCV_SBI?

Anyways, probably stating the obvious on an RFC, but ¯\_(ツ)_/¯

Thanks,
Conor.

Quoted 46 lines
arch/riscv/kernel/suspend.c +37 -0
+
+static int sbi_system_suspend(unsigned long sleep_type,
+			      unsigned long resume_addr,
+			      unsigned long opaque)
+{
+	struct sbiret ret;
+
+	ret = sbi_ecall(SBI_EXT_SUSP, SBI_EXT_SUSP_SUSPEND,
+			sleep_type, resume_addr, opaque, 0, 0, 0);
+	if (ret.error)
+		return sbi_err_map_linux_errno(ret.error);
+
+	return ret.value;
+}
+
+static int sbi_system_suspend_enter(suspend_state_t state)
+{
+	return cpu_suspend(SBI_SUSP_SLEEP_TYPE_SUSPEND, sbi_system_suspend);
+}
+
+static const struct platform_suspend_ops sbi_system_suspend_ops = {
+	.valid = suspend_valid_only_mem,
+	.enter = sbi_system_suspend_enter,
+};
+
+static int __init sbi_system_suspend_init(void)
+{
+	if (!sbi_spec_is_0_1() && sbi_probe_extension(SBI_EXT_SUSP) > 0) {
+		pr_info("SBI SUSP extension detected\n");
+		if (IS_ENABLED(CONFIG_SUSPEND))
+			suspend_set_ops(&sbi_system_suspend_ops);
+	}
+
+	return 0;
+}
+
+arch_initcall(sbi_system_suspend_init);
Signature · 7 lines
2.39.0


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

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

#5 lore raw

Re: [RFC PATCH 1/1] riscv: sbi: Introduce system suspend support

Quoted 10 lines · Conor Dooley

Hey Drew!

Quoted 7 lines · Andrew Jones

When the SUSP SBI extension is present it implies that the standard "suspend to RAM" type is available. Wire it up to the generic platform suspend support, also applying the already present support for non-retentive CPU suspend. When the kernel is built with CONFIG_SUSPEND, one can do 'echo mem > /sys/power/state' to suspend. Resumption will occur when a platform-specific wake-up event arrives.

Quoted 5 lines
Quoted 1 line

First things first, anything SBI depends on !m-mode, so you've gotta add some sort of gating unfortunately around those ECALLs. But I figure you may have already seen the build failures on patchwork for nommu?

Actually, no, and I think I missed other emails like that in the past. I assume I'm on the To: of these messages, so it's odd they're not showing up. Can you forward me this message? I'll inspect the headers and try to figure what's going on.

Quoted 2 lines

Also, when there's an actual spec would you mind doing a Link: spec.pdf?

Will do

Quoted 24 lines
Quoted 24 lines
---
 arch/riscv/Kconfig           |  5 ++++-
 arch/riscv/include/asm/sbi.h |  9 ++++++++
 arch/riscv/kernel/suspend.c  | 41 ++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 1 deletion(-)
arch/riscv/Kconfig +1 -1
index e2b656043abf..a53d94c1953e 100644
@@ -52,7 +52,7 @@ config RISCV
 	select CLONE_BACKWARDS
 	select CLINT_TIMER if !MMU
 	select COMMON_CLK
-	select CPU_PM if CPU_IDLE
+	select CPU_PM if (SUSPEND || CPU_IDLE)
 	select EDAC_SUPPORT
 	select GENERIC_ARCH_TOPOLOGY
 	select GENERIC_ATOMIC64 if !64BIT
@@ -686,6 +686,9 @@ config PORTABLE
 	select OF
 	select MMU
 
Quoted 5 lines

arch/riscv/Kconfig config PORTABLE · line 690

Quoted 2 lines
arch/riscv/Kconfig +2 -0
+config ARCH_SUSPEND_POSSIBLE
+	def_bool y

Since the content you're adding depends on having an SBI extention, does this need to be s/y/RISCV_SBI/?

Indeed. Will do.

Quoted 50 lines
Quoted 50 lines
arch/riscv/Kconfig +0 -0
 menu "Power management options"
 
 source "kernel/power/Kconfig"
arch/riscv/include/asm/sbi.h +9 -0
index 4ca7fbacff42..9834ba4ce3e4 100644
@@ -29,6 +29,7 @@ enum sbi_ext_id {
 	SBI_EXT_RFENCE = 0x52464E43,
 	SBI_EXT_HSM = 0x48534D,
 	SBI_EXT_SRST = 0x53525354,
+	SBI_EXT_SUSP = 0x53555350,
 	SBI_EXT_PMU = 0x504D55,
 
 	/* Experimentals extensions must lie within this range */
@@ -113,6 +114,14 @@ enum sbi_srst_reset_reason {
 	SBI_SRST_RESET_REASON_SYS_FAILURE,
 };
 
+enum sbi_ext_susp_fid {
+	SBI_EXT_SUSP_SUSPEND = 0,
+};
+
+enum sbi_ext_susp_sleep_type {
+	SBI_SUSP_SLEEP_TYPE_SUSPEND = 0,
+};
+
 enum sbi_ext_pmu_fid {
 	SBI_EXT_PMU_NUM_COUNTERS = 0,
 	SBI_EXT_PMU_COUNTER_GET_INFO,
arch/riscv/kernel/suspend.c +4 -0
index 9ba24fb8cc93..bc26e9ae4782 100644
@@ -4,8 +4,12 @@
  * Copyright (c) 2022 Ventana Micro Systems Inc.
  */
 
+#define pr_fmt(fmt) "suspend: " fmt
+
 #include <linux/ftrace.h>
+#include <linux/suspend.h>
 #include <asm/csr.h>
+#include <asm/sbi.h>
 #include <asm/suspend.h>
 
 static void suspend_save_csrs(struct suspend_context *context)
@@ -85,3 +89,40 @@ int cpu_suspend(unsigned long arg,
 
Quoted 6 lines

arch/riscv/kernel/suspend.c int cpu_suspend(unsigned long arg, · line 91

Quoted 2 lines
arch/riscv/kernel/suspend.c +0 -0
 	return rc;
 }

And then from here down needs to be #ifdef RISCV_SBI?

Anyways, probably stating the obvious on an RFC, but ¯\_(ツ)_/¯

Thanks for pointing this out. I made a last minute change to move the content from arch/riscv/kernel/sbi.c to arch/riscv/kernel/suspend.c and forgot I needed to worry about RISCV_SBI.

Thanks,
drew

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

#6 lore raw

Re: [RFC PATCH 1/1] riscv: sbi: Introduce system suspend support

Quoted 11 lines · Andrew Jones
Quoted 10 lines · Conor Dooley
Quoted 8 lines · Andrew Jones

When the SUSP SBI extension is present it implies that the standard "suspend to RAM" type is available. Wire it up to the generic platform suspend support, also applying the already present support for non-retentive CPU suspend. When the kernel is built with CONFIG_SUSPEND, one can do 'echo mem > /sys/power/state' to suspend. Resumption will occur when a platform-specific wake-up event arrives.

Quoted 8 lines
Quoted 3 lines

First things first, anything SBI depends on !m-mode, so you've gotta add some sort of gating unfortunately around those ECALLs. But I figure you may have already seen the build failures on patchwork for nommu?

Actually, no, and I think I missed other emails like that in the past. I assume I'm on the To: of these messages, so it's odd they're not showing up. Can you forward me this message? I'll inspect the headers and try to figure what's going on.

Oh my bad, I assumed that you'd look on patchwork. I haven't made it send emails as I don't trust it not to spam people with build issues that are not their fault.

Sorry!
Conor.

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

#7 lore raw

Re: [RFC PATCH 1/1] riscv: sbi: Introduce system suspend support

Quoted 16 lines · Conor Dooley
Quoted 15 lines · Andrew Jones
Quoted 13 lines · Conor Dooley
Quoted 8 lines · Andrew Jones

When the SUSP SBI extension is present it implies that the standard "suspend to RAM" type is available. Wire it up to the generic platform suspend support, also applying the already present support for non-retentive CPU suspend. When the kernel is built with CONFIG_SUSPEND, one can do 'echo mem > /sys/power/state' to suspend. Resumption will occur when a platform-specific wake-up event arrives.

First things first, anything SBI depends on !m-mode, so you've gotta add some sort of gating unfortunately around those ECALLs. But I figure you may have already seen the build failures on patchwork for nommu?

Quoted 8 lines
Quoted 4 lines

Actually, no, and I think I missed other emails like that in the past. I assume I'm on the To: of these messages, so it's odd they're not showing up. Can you forward me this message? I'll inspect the headers and try to figure what's going on.

Oh my bad, I assumed that you'd look on patchwork. I haven't made it send emails as I don't trust it not to spam people with build issues that are not their fault.

Ah, OK. I'll add a patchwork check to my patch submission workflow!

Thanks,
drew

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

#8 lore raw
In reply to Andrew Jones

Hey Drew!

Quoted 12 lines · Andrew Jones

Booting with an OpenSBI including the RFC series[1] implementing the draft proposal for SBI system suspend[2] we can add system support to Linux. This support implements "suspend-to-RAM", which means when a kernel is built with CONFIG_SUSPEND 'echo mem > /sys/power/state' will initiate a suspension.

This has only been tested on QEMU using the OpenSBI system suspend test. The test just waits 5 seconds and then resumes. To truly use system suspend a platform must have a low-level firmware implementation and provide at least one wake-up event, such as from a wakeup-capable RTC alarm, to resume.

Quoted 3 lines

[1] https://github.com/jones-drew/opensbi/commits/susp-rfc

    Posting: http://lists.infradead.org/pipermail/opensbi/2023-January/004091.html

[2] https://github.com/jones-drew/riscv-sbi-doc/commit/d9e43e9a938fc3eb510e023c3f352462876f7785

In case you don't get alerts from GH, I left one *tiny* comment on this. And I really mean tiny!

Thanks,
Conor.

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

#9 lore raw
Quoted 15 lines · Conor Dooley

Hey Drew!

Quoted 12 lines · Andrew Jones

Booting with an OpenSBI including the RFC series[1] implementing the draft proposal for SBI system suspend[2] we can add system support to Linux. This support implements "suspend-to-RAM", which means when a kernel is built with CONFIG_SUSPEND 'echo mem > /sys/power/state' will initiate a suspension.

This has only been tested on QEMU using the OpenSBI system suspend test. The test just waits 5 seconds and then resumes. To truly use system suspend a platform must have a low-level firmware implementation and provide at least one wake-up event, such as from a wakeup-capable RTC alarm, to resume.

Quoted 6 lines
Quoted 3 lines

[1] https://github.com/jones-drew/opensbi/commits/susp-rfc

    Posting: http://lists.infradead.org/pipermail/opensbi/2023-January/004091.html

[2] https://github.com/jones-drew/riscv-sbi-doc/commit/d9e43e9a938fc3eb510e023c3f352462876f7785

In case you don't get alerts from GH, I left one *tiny* comment on this. And I really mean tiny!

Hi Conor,

Yup, I saw the comma suggestion and agree with it. As there are two other instances of that error description in the spec I'll also send a PR to add commas to them as well.

Thanks,
drew

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