Skip to contentSkip to conversation

Support SSTC while PM operations

PATCH Superseded Nick Hu 4 participants 7 messages Oct 28, 2024 – Mar 27, 2025

Subsystems (3)

v3 of 4 Superseded 1 of 2 applied Latest is v4 →

lore
Share & tools
lore

Nick Hu

author
#0 lore raw

v3 cover Support SSTC while PM operations

When the cpu is going to be hotplug, stop the stimecmp to prevent pending interrupt.
When the cpu is going to be suspended, save the stimecmp before entering the suspend state and restore it in the resume path.

changes in v3:
1. Update the commit description
2. Remove csr_read/write_hi_lo from the previous patch

changes in v2:
1. Add csr_read/write_hi_lo operations
2. Apply the suggestion from Anup.

Nick Hu (2):
riscv: Add stimecmp save and restore clocksource/drivers/timer-riscv: Stop stimecmp when cpu hotplug

 arch/riscv/include/asm/suspend.h  |  4 ++++
 arch/riscv/kernel/suspend.c       | 14 ++++++++++++++
 drivers/clocksource/timer-riscv.c |  6 ++++++
 3 files changed, 24 insertions(+)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/2 riscv: Add stimecmp save and restore

If the HW support the SSTC extension, we should save and restore the stimecmp register while cpu non retention suspend.

---
 arch/riscv/include/asm/suspend.h |  4 ++++
 arch/riscv/kernel/suspend.c      | 14 ++++++++++++++
 2 files changed, 18 insertions(+)
arch/riscv/include/asm/suspend.h +4 -0
index 4ffb022b097f..dc5782b5fbad 100644
@@ -18,6 +18,10 @@ struct suspend_context {
 	unsigned long ie;
 #ifdef CONFIG_MMU
 	unsigned long satp;
+	unsigned long stimecmp;
+#if __riscv_xlen < 64
+	unsigned long stimecmph;
+#endif
 #endif
 };
 
arch/riscv/kernel/suspend.c +14 -0
index 9a8a0dc035b2..24b3f57d467f 100644
@@ -30,6 +30,13 @@ void suspend_save_csrs(struct suspend_context *context)
 	 */
 
 #ifdef CONFIG_MMU
+	if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SSTC)) {
+		context->stimecmp = csr_read(CSR_STIMECMP);
+#if __riscv_xlen < 64
+		context->stimecmph = csr_read(CSR_STIMECMPH);
+#endif
+	}
+
 	context->satp = csr_read(CSR_SATP);
 #endif
 }
@@ -43,6 +50,13 @@ void suspend_restore_csrs(struct suspend_context *context)
 	csr_write(CSR_IE, context->ie);
 
 #ifdef CONFIG_MMU
+	if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SSTC)) {
+		csr_write(CSR_STIMECMP, context->stimecmp);
+#if __riscv_xlen < 64
+		csr_write(CSR_STIMECMPH, context->stimecmph);
+#endif
+	}
+
 	csr_write(CSR_SATP, context->satp);
 #endif
 }
Signature · 1 line
2.34.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] riscv: Add stimecmp save and restore

Quoted 5 lines · Nick Hu

If the HW support the SSTC extension, we should save and restore the stimecmp register while cpu non retention suspend.

LGTM.

Regards,
Anup

Quoted 56 lines
---
 arch/riscv/include/asm/suspend.h |  4 ++++
 arch/riscv/kernel/suspend.c      | 14 ++++++++++++++
 2 files changed, 18 insertions(+)
arch/riscv/include/asm/suspend.h +4 -0
index 4ffb022b097f..dc5782b5fbad 100644
@@ -18,6 +18,10 @@ struct suspend_context {
        unsigned long ie;
 #ifdef CONFIG_MMU
        unsigned long satp;
+       unsigned long stimecmp;
+#if __riscv_xlen < 64
+       unsigned long stimecmph;
+#endif
 #endif
 };
 
arch/riscv/kernel/suspend.c +14 -1
index 9a8a0dc035b2..24b3f57d467f 100644
@@ -30,6 +30,13 @@ void suspend_save_csrs(struct suspend_context *context)
         */
 
 #ifdef CONFIG_MMU
+       if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SSTC)) {
+               context->stimecmp = csr_read(CSR_STIMECMP);
+#if __riscv_xlen < 64
+               context->stimecmph = csr_read(CSR_STIMECMPH);
+#endif
+       }
+
        context->satp = csr_read(CSR_SATP);
 #endif
 }
@@ -43,6 +50,13 @@ void suspend_restore_csrs(struct suspend_context *context)
        csr_write(CSR_IE, context->ie);
 
 #ifdef CONFIG_MMU
+       if (riscv_has_extension_unlikely(RISCV_ISA_EXT_SSTC)) {
+               csr_write(CSR_STIMECMP, context->stimecmp);
+#if __riscv_xlen < 64
+               csr_write(CSR_STIMECMPH, context->stimecmph);
+#endif
+       }
+
        csr_write(CSR_SATP, context->satp);
 #endif
 }
--

2.34.1

author
#3 lore raw

PATCH v3 2/2 clocksource/drivers/timer-riscv: Stop stimecmp when cpu hotplug

In reply to Nick Hu

Stop the timer when the cpu is going to be offline otherwise the timer interrupt may be pending while performing power-down.

---
 drivers/clocksource/timer-riscv.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/clocksource/timer-riscv.c +6 -0
index 48ce50c5f5e6..166dee14e46b 100644
@@ -127,6 +127,12 @@ static int riscv_timer_starting_cpu(unsigned int cpu)
 static int riscv_timer_dying_cpu(unsigned int cpu)
 {
 	disable_percpu_irq(riscv_clock_event_irq);
+	/*
+	 * Stop the timer when the cpu is going to be offline otherwise
+	 * the timer interrupt may be pending while performing power-down.
+	 */
+	riscv_clock_event_stop();
+
 	return 0;
 }
 
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v3 2/2] clocksource/drivers/timer-riscv: Stop stimecmp when cpu hotplug

Quoted 17 lines · Nick Hu

Stop the timer when the cpu is going to be offline otherwise the timer interrupt may be pending while performing power-down.

---
  drivers/clocksource/timer-riscv.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/clocksource/timer-riscv.c +0 -0
index 48ce50c5f5e6..166dee14e46b 100644
@@ -127,6 +127,12 @@ static int riscv_timer_starting_cpu(unsigned int cpu)
  static int riscv_timer_dying_cpu(unsigned int cpu)

drivers/clocksource/timer-riscv.c static int riscv_timer_starting_cpu(unsigned int cpu) · line 137

Quoted 10 lines
drivers/clocksource/timer-riscv.c +6 -0
  {
  	disable_percpu_irq(riscv_clock_event_irq);
+	/*
+	 * Stop the timer when the cpu is going to be offline otherwise
+	 * the timer interrupt may be pending while performing power-down.
+	 */
+	riscv_clock_event_stop();
+
  	return 0;
  }

Should it not be the opposite?

First stop the clock which clears the interrupt and then disable the irq?

Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author
#5 lore raw

Re: [PATCH v3 2/2] clocksource/drivers/timer-riscv: Stop stimecmp when cpu hotplug

Hi Daniel,

Quoted 32 lines · Daniel Lezcano
Quoted 17 lines · Nick Hu

Stop the timer when the cpu is going to be offline otherwise the timer interrupt may be pending while performing power-down.

---
  drivers/clocksource/timer-riscv.c | 6 ++++++
  1 file changed, 6 insertions(+)
drivers/clocksource/timer-riscv.c +0 -0
index 48ce50c5f5e6..166dee14e46b 100644
@@ -127,6 +127,12 @@ static int riscv_timer_starting_cpu(unsigned int cpu)
  static int riscv_timer_dying_cpu(unsigned int cpu)

drivers/clocksource/timer-riscv.c static int riscv_timer_starting_cpu(unsigned int cpu) · line 137

Quoted 10 lines
drivers/clocksource/timer-riscv.c +6 -0
  {
      disable_percpu_irq(riscv_clock_event_irq);
+     /*
+      * Stop the timer when the cpu is going to be offline otherwise
+      * the timer interrupt may be pending while performing power-down.
+      */
+     riscv_clock_event_stop();
+
      return 0;
  }

Should it not be the opposite?

Quoted 2 lines

First stop the clock which clears the interrupt and then disable the irq?

SIE.STIE = 0 ->
Mtimer interrupt comes -> trap to m-mode -> raise STIP -> stop the clock
Is the above case you are concerned about?

Quoted 10 lines

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

#6 lore raw
In reply to Nick Hu
This series was applied to riscv/linux.git (for-next) by Alexandre Ghiti <[email protected]>:

Hello:

This series was applied to riscv/linux.git (for-next) by Alexandre Ghiti <[email protected]>:

Quoted 11 lines · On Mon, 28 Oct 2024 11:39:24 +0800 you wrote:

When the cpu is going to be hotplug, stop the stimecmp to prevent pending interrupt.
When the cpu is going to be suspended, save the stimecmp before entering the suspend state and restore it in the resume path.

changes in v3:
1. Update the commit description
2. Remove csr_read/write_hi_lo from the previous patch

[...]

Here is the summary with links:
- [v3,1/2] riscv: Add stimecmp save and restore

    https://git.kernel.org/riscv/c/ffef54ad4110

- [v3,2/2] clocksource/drivers/timer-riscv: Stop stimecmp when cpu hotplug (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html