From: Nick Hu <nick.hu@sifive.com>
To: paul.walmsley@sifive.com, palmer@dabbelt.com,
aou@eecs.berkeley.edu, leyfoon.tan@starfivetech.com,
mason.huo@starfivetech.com, conor.dooley@microchip.com,
jeeheng.sia@starfivetech.com, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org, nick.hu@sifive.com,
zong.li@sifive.com
Cc: Andy Chiu <andy.chiu@sifive.com>
Subject: [PATCH v2] riscv: suspend: Add syscore ops for suspend
Date: Tue, 22 Aug 2023 13:47:39 +0800 [thread overview]
Message-ID: <20230822054739.33552-1-nick.hu@sifive.com> (raw)
If a task is the one who performs the suspend flow and it also do some
floating or vector operations before the suspend, we might lose the FPU
and vector states when it goes to the non-retention system suspend state.
Add syscore ops to save and restore the FPU and vector states of the
current task to fix the issue above.
Co-developed-by: Andy Chiu <andy.chiu@sifive.com>
Signed-off-by: Andy Chiu <andy.chiu@sifive.com>
Signed-off-by: Nick Hu <nick.hu@sifive.com>
---
Changes in v2:
a) Add Co-developed-by and adjust the order of signed-off
b) Rephrase the commit message
arch/riscv/kernel/suspend.c | 45 +++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c
index 3c89b8ec69c4..ff69ff8a1974 100644
--- a/arch/riscv/kernel/suspend.c
+++ b/arch/riscv/kernel/suspend.c
@@ -4,9 +4,14 @@
* Copyright (c) 2022 Ventana Micro Systems Inc.
*/
+#include <linux/cpu_pm.h>
#include <linux/ftrace.h>
+#include <linux/thread_info.h>
+#include <linux/syscore_ops.h>
#include <asm/csr.h>
#include <asm/suspend.h>
+#include <asm/switch_to.h>
+#include <asm/vector.h>
void suspend_save_csrs(struct suspend_context *context)
{
@@ -85,3 +90,43 @@ int cpu_suspend(unsigned long arg,
return rc;
}
+
+static int riscv_cpu_suspend(void)
+{
+ struct task_struct *cur_task = get_current();
+ struct pt_regs *regs = task_pt_regs(cur_task);
+
+ if (has_fpu()) {
+ if (unlikely(regs->status & SR_SD))
+ fstate_save(cur_task, regs);
+ }
+ if (has_vector()) {
+ if (unlikely(regs->status & SR_SD))
+ riscv_v_vstate_save(cur_task, regs);
+ }
+
+ return 0;
+}
+
+static void riscv_cpu_resume(void)
+{
+ struct task_struct *cur_task = get_current();
+ struct pt_regs *regs = task_pt_regs(cur_task);
+
+ if (has_fpu())
+ fstate_restore(cur_task, regs);
+ if (has_vector())
+ riscv_v_vstate_restore(cur_task, regs);
+}
+
+static struct syscore_ops riscv_cpu_syscore_ops = {
+ .suspend = riscv_cpu_suspend,
+ .resume = riscv_cpu_resume,
+};
+
+static int __init riscv_cpu_suspend_init(void)
+{
+ register_syscore_ops(&riscv_cpu_syscore_ops);
+ return 0;
+}
+arch_initcall(riscv_cpu_suspend_init);
--
2.34.1
next reply other threads:[~2023-08-22 5:47 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-22 5:47 Nick Hu [this message]
2023-08-22 6:16 ` Anup Patel
2023-08-22 7:58 ` Nick Hu
2023-08-22 9:17 ` Anup Patel
2023-08-23 3:08 ` Nick Hu
2023-08-23 13:44 ` Anup Patel
2023-08-28 3:33 ` Nick Hu
2023-08-22 18:06 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230822054739.33552-1-nick.hu@sifive.com \
--to=nick.hu@sifive.com \
--cc=andy.chiu@sifive.com \
--cc=aou@eecs.berkeley.edu \
--cc=conor.dooley@microchip.com \
--cc=jeeheng.sia@starfivetech.com \
--cc=leyfoon.tan@starfivetech.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mason.huo@starfivetech.com \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=zong.li@sifive.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®