1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021 Western Digital Corporation or its affiliates.
4 * Copyright (c) 2022 Ventana Micro Systems Inc.
7 #define pr_fmt(fmt) "suspend: " fmt
9 #include <linux/ftrace.h>
10 #include <linux/suspend.h>
13 #include <asm/suspend.h>
15 void suspend_save_csrs(struct suspend_context *context)
17 context->scratch = csr_read(CSR_SCRATCH);
18 if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
19 context->envcfg = csr_read(CSR_ENVCFG);
20 context->tvec = csr_read(CSR_TVEC);
21 context->ie = csr_read(CSR_IE);
24 * No need to save/restore IP CSR (i.e. MIP or SIP) because:
26 * 1. For no-MMU (M-mode) kernel, the bits in MIP are set by
27 * external devices (such as interrupt controller, timer, etc).
28 * 2. For MMU (S-mode) kernel, the bits in SIP are set by
29 * M-mode firmware and external devices (such as interrupt
34 context->satp = csr_read(CSR_SATP);
38 void suspend_restore_csrs(struct suspend_context *context)
40 csr_write(CSR_SCRATCH, context->scratch);
41 if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
42 csr_write(CSR_ENVCFG, context->envcfg);
43 csr_write(CSR_TVEC, context->tvec);
44 csr_write(CSR_IE, context->ie);
47 csr_write(CSR_SATP, context->satp);
51 int cpu_suspend(unsigned long arg,
52 int (*finish)(unsigned long arg,
54 unsigned long context))
57 struct suspend_context context = { 0 };
59 /* Finisher should be non-NULL */
63 /* Save additional CSRs*/
64 suspend_save_csrs(&context);
67 * Function graph tracer state gets incosistent when the kernel
68 * calls functions that never return (aka finishers) hence disable
69 * graph tracing during their execution.
71 pause_graph_tracing();
73 /* Save context on stack */
74 if (__cpu_suspend_enter(&context)) {
75 /* Call the finisher */
76 rc = finish(arg, __pa_symbol(__cpu_resume_enter),
80 * Should never reach here, unless the suspend finisher
81 * fails. Successful cpu_suspend() should return from
82 * __cpu_resume_entry()
88 /* Enable function graph tracer */
89 unpause_graph_tracing();
91 /* Restore additional CSRs */
92 suspend_restore_csrs(&context);
97 #ifdef CONFIG_RISCV_SBI
98 static int sbi_system_suspend(unsigned long sleep_type,
99 unsigned long resume_addr,
100 unsigned long opaque)
104 ret = sbi_ecall(SBI_EXT_SUSP, SBI_EXT_SUSP_SYSTEM_SUSPEND,
105 sleep_type, resume_addr, opaque, 0, 0, 0);
107 return sbi_err_map_linux_errno(ret.error);
112 static int sbi_system_suspend_enter(suspend_state_t state)
114 return cpu_suspend(SBI_SUSP_SLEEP_TYPE_SUSPEND_TO_RAM, sbi_system_suspend);
117 static const struct platform_suspend_ops sbi_system_suspend_ops = {
118 .valid = suspend_valid_only_mem,
119 .enter = sbi_system_suspend_enter,
122 static int __init sbi_system_suspend_init(void)
124 if (sbi_spec_version >= sbi_mk_version(2, 0) &&
125 sbi_probe_extension(SBI_EXT_SUSP) > 0) {
126 pr_info("SBI SUSP extension detected\n");
127 if (IS_ENABLED(CONFIG_SUSPEND))
128 suspend_set_ops(&sbi_system_suspend_ops);
134 arch_initcall(sbi_system_suspend_init);
135 #endif /* CONFIG_RISCV_SBI */