xref: /openbmc/linux/arch/arm64/kernel/suspend.c (revision 96d3c5a7d20ec546e44695983fe0508c6f904248)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2de818bd4SLorenzo Pieralisi #include <linux/ftrace.h>
3fb4a9602SLorenzo Pieralisi #include <linux/percpu.h>
495322526SLorenzo Pieralisi #include <linux/slab.h>
5e1281f56SJames Morse #include <linux/uaccess.h>
665fddcfcSMike Rapoport #include <linux/pgtable.h>
719235e47SPeter Zijlstra #include <linux/cpuidle.h>
8d0854412SJames Morse #include <asm/alternative.h>
995322526SLorenzo Pieralisi #include <asm/cacheflush.h>
10d0854412SJames Morse #include <asm/cpufeature.h>
1177345ef7SMarc Zyngier #include <asm/cpuidle.h>
120fbeb318SJames Morse #include <asm/daifflags.h>
1395322526SLorenzo Pieralisi #include <asm/debug-monitors.h>
14d0854412SJames Morse #include <asm/exec.h>
15*7c892383SMark Brown #include <asm/fpsimd.h>
1639d08e83SCatalin Marinas #include <asm/mte.h>
1795322526SLorenzo Pieralisi #include <asm/memory.h>
18f43c2718SLorenzo Pieralisi #include <asm/mmu_context.h>
1995322526SLorenzo Pieralisi #include <asm/smp_plat.h>
2095322526SLorenzo Pieralisi #include <asm/suspend.h>
2195322526SLorenzo Pieralisi 
2295322526SLorenzo Pieralisi /*
23cabe1c81SJames Morse  * This is allocated by cpu_suspend_init(), and used to store a pointer to
24cabe1c81SJames Morse  * the 'struct sleep_stack_data' the contains a particular CPUs state.
2595322526SLorenzo Pieralisi  */
26cabe1c81SJames Morse unsigned long *sleep_save_stash;
2795322526SLorenzo Pieralisi 
2865c021bbSLorenzo Pieralisi /*
2965c021bbSLorenzo Pieralisi  * This hook is provided so that cpu_suspend code can restore HW
3065c021bbSLorenzo Pieralisi  * breakpoints as early as possible in the resume path, before reenabling
3165c021bbSLorenzo Pieralisi  * debug exceptions. Code cannot be run from a CPU PM notifier since by the
3265c021bbSLorenzo Pieralisi  * time the notifier runs debug exceptions might have been enabled already,
3365c021bbSLorenzo Pieralisi  * with HW breakpoints registers content still in an unknown state.
3465c021bbSLorenzo Pieralisi  */
35d7a83d12SWill Deacon static int (*hw_breakpoint_restore)(unsigned int);
cpu_suspend_set_dbg_restorer(int (* hw_bp_restore)(unsigned int))36d7a83d12SWill Deacon void __init cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int))
3765c021bbSLorenzo Pieralisi {
3865c021bbSLorenzo Pieralisi 	/* Prevent multiple restore hook initializations */
3965c021bbSLorenzo Pieralisi 	if (WARN_ON(hw_breakpoint_restore))
4065c021bbSLorenzo Pieralisi 		return;
4165c021bbSLorenzo Pieralisi 	hw_breakpoint_restore = hw_bp_restore;
4265c021bbSLorenzo Pieralisi }
4365c021bbSLorenzo Pieralisi 
__cpu_suspend_exit(void)44adc9b2dfSJames Morse void notrace __cpu_suspend_exit(void)
45714f5992SLorenzo Pieralisi {
46d7a83d12SWill Deacon 	unsigned int cpu = smp_processor_id();
47d7a83d12SWill Deacon 
48973b9e37SPeter Collingbourne 	mte_suspend_exit();
49973b9e37SPeter Collingbourne 
50f43c2718SLorenzo Pieralisi 	/*
519e8e865bSMark Rutland 	 * We are resuming from reset with the idmap active in TTBR0_EL1.
529e8e865bSMark Rutland 	 * We must uninstall the idmap and restore the expected MMU
539e8e865bSMark Rutland 	 * state before we can possibly return to userspace.
54f43c2718SLorenzo Pieralisi 	 */
559e8e865bSMark Rutland 	cpu_uninstall_idmap();
56fb4a9602SLorenzo Pieralisi 
575ffdfaedSVladimir Murzin 	/* Restore CnP bit in TTBR1_EL1 */
585ffdfaedSVladimir Murzin 	if (system_supports_cnp())
591682c45bSArd Biesheuvel 		cpu_replace_ttbr1(lm_alias(swapper_pg_dir), idmap_pg_dir);
605ffdfaedSVladimir Murzin 
61fb4a9602SLorenzo Pieralisi 	/*
62d0854412SJames Morse 	 * PSTATE was not saved over suspend/resume, re-enable any detected
63d0854412SJames Morse 	 * features that might not have been set correctly.
64d0854412SJames Morse 	 */
6501ab991fSArd Biesheuvel 	if (cpus_have_const_cap(ARM64_HAS_DIT))
6601ab991fSArd Biesheuvel 		set_pstate_dit(1);
67e1281f56SJames Morse 	__uaccess_enable_hw_pan();
68d0854412SJames Morse 
69d0854412SJames Morse 	/*
7065c021bbSLorenzo Pieralisi 	 * Restore HW breakpoint registers to sane values
7165c021bbSLorenzo Pieralisi 	 * before debug exceptions are possibly reenabled
720fbeb318SJames Morse 	 * by cpu_suspend()s local_daif_restore() call.
7365c021bbSLorenzo Pieralisi 	 */
7465c021bbSLorenzo Pieralisi 	if (hw_breakpoint_restore)
75d7a83d12SWill Deacon 		hw_breakpoint_restore(cpu);
76647d0519SMarc Zyngier 
77647d0519SMarc Zyngier 	/*
78647d0519SMarc Zyngier 	 * On resume, firmware implementing dynamic mitigation will
79647d0519SMarc Zyngier 	 * have turned the mitigation on. If the user has forcefully
80647d0519SMarc Zyngier 	 * disabled it, make sure their wishes are obeyed.
81647d0519SMarc Zyngier 	 */
82c2876207SWill Deacon 	spectre_v4_enable_mitigation(NULL);
8339d08e83SCatalin Marinas 
84*7c892383SMark Brown 	sme_suspend_exit();
85*7c892383SMark Brown 
86b90e4839SPeter Collingbourne 	/* Restore additional feature-specific configuration */
87b90e4839SPeter Collingbourne 	ptrauth_suspend_exit();
8895322526SLorenzo Pieralisi }
8995322526SLorenzo Pieralisi 
90adc9b2dfSJames Morse /*
91adc9b2dfSJames Morse  * cpu_suspend
92adc9b2dfSJames Morse  *
93adc9b2dfSJames Morse  * arg: argument to pass to the finisher function
94adc9b2dfSJames Morse  * fn: finisher function pointer
95adc9b2dfSJames Morse  *
96adc9b2dfSJames Morse  */
cpu_suspend(unsigned long arg,int (* fn)(unsigned long))97adc9b2dfSJames Morse int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
98adc9b2dfSJames Morse {
99adc9b2dfSJames Morse 	int ret = 0;
100adc9b2dfSJames Morse 	unsigned long flags;
101adc9b2dfSJames Morse 	struct sleep_stack_data state;
10277345ef7SMarc Zyngier 	struct arm_cpuidle_irq_context context;
103adc9b2dfSJames Morse 
104eab0e6e1SVincenzo Frascino 	/* Report any MTE async fault before going to suspend */
105eab0e6e1SVincenzo Frascino 	mte_suspend_enter();
106eab0e6e1SVincenzo Frascino 
107adc9b2dfSJames Morse 	/*
108adc9b2dfSJames Morse 	 * From this point debug exceptions are disabled to prevent
109adc9b2dfSJames Morse 	 * updates to mdscr register (saved and restored along with
110adc9b2dfSJames Morse 	 * general purpose registers) from kernel debuggers.
11119235e47SPeter Zijlstra 	 *
11219235e47SPeter Zijlstra 	 * Strictly speaking the trace_hardirqs_off() here is superfluous,
11319235e47SPeter Zijlstra 	 * hardirqs should be firmly off by now. This really ought to use
11419235e47SPeter Zijlstra 	 * something like raw_local_daif_save().
115adc9b2dfSJames Morse 	 */
1160fbeb318SJames Morse 	flags = local_daif_save();
117adc9b2dfSJames Morse 
118adc9b2dfSJames Morse 	/*
11977345ef7SMarc Zyngier 	 * Function graph tracer state gets inconsistent when the kernel
120adc9b2dfSJames Morse 	 * calls functions that never return (aka suspend finishers) hence
121adc9b2dfSJames Morse 	 * disable graph tracing during their execution.
122adc9b2dfSJames Morse 	 */
123adc9b2dfSJames Morse 	pause_graph_tracing();
124adc9b2dfSJames Morse 
12577345ef7SMarc Zyngier 	/*
12677345ef7SMarc Zyngier 	 * Switch to using DAIF.IF instead of PMR in order to reliably
12777345ef7SMarc Zyngier 	 * resume if we're using pseudo-NMIs.
12877345ef7SMarc Zyngier 	 */
12977345ef7SMarc Zyngier 	arm_cpuidle_save_irq_context(&context);
13077345ef7SMarc Zyngier 
13119235e47SPeter Zijlstra 	ct_cpuidle_enter();
13219235e47SPeter Zijlstra 
133adc9b2dfSJames Morse 	if (__cpu_suspend_enter(&state)) {
134adc9b2dfSJames Morse 		/* Call the suspend finisher */
135adc9b2dfSJames Morse 		ret = fn(arg);
136adc9b2dfSJames Morse 
137adc9b2dfSJames Morse 		/*
138adc9b2dfSJames Morse 		 * Never gets here, unless the suspend finisher fails.
139adc9b2dfSJames Morse 		 * Successful cpu_suspend() should return from cpu_resume(),
140adc9b2dfSJames Morse 		 * returning through this code path is considered an error
141adc9b2dfSJames Morse 		 * If the return value is set to 0 force ret = -EOPNOTSUPP
142adc9b2dfSJames Morse 		 * to make sure a proper error condition is propagated
143adc9b2dfSJames Morse 		 */
144adc9b2dfSJames Morse 		if (!ret)
145adc9b2dfSJames Morse 			ret = -EOPNOTSUPP;
14619235e47SPeter Zijlstra 
14719235e47SPeter Zijlstra 		ct_cpuidle_exit();
148adc9b2dfSJames Morse 	} else {
14919235e47SPeter Zijlstra 		ct_cpuidle_exit();
15019235e47SPeter Zijlstra 		__cpu_suspend_exit();
151adc9b2dfSJames Morse 	}
152adc9b2dfSJames Morse 
15377345ef7SMarc Zyngier 	arm_cpuidle_restore_irq_context(&context);
15477345ef7SMarc Zyngier 
155de818bd4SLorenzo Pieralisi 	unpause_graph_tracing();
156de818bd4SLorenzo Pieralisi 
15795322526SLorenzo Pieralisi 	/*
15895322526SLorenzo Pieralisi 	 * Restore pstate flags. OS lock and mdscr have been already
15995322526SLorenzo Pieralisi 	 * restored, so from this point onwards, debugging is fully
160dd671f16SJulia Lawall 	 * reenabled if it was enabled when core started shutdown.
16195322526SLorenzo Pieralisi 	 */
1620fbeb318SJames Morse 	local_daif_restore(flags);
16395322526SLorenzo Pieralisi 
16495322526SLorenzo Pieralisi 	return ret;
16595322526SLorenzo Pieralisi }
16695322526SLorenzo Pieralisi 
cpu_suspend_init(void)16718ab7db6SLorenzo Pieralisi static int __init cpu_suspend_init(void)
16895322526SLorenzo Pieralisi {
16995322526SLorenzo Pieralisi 	/* ctx_ptr is an array of physical addresses */
170cabe1c81SJames Morse 	sleep_save_stash = kcalloc(mpidr_hash_size(), sizeof(*sleep_save_stash),
171cabe1c81SJames Morse 				   GFP_KERNEL);
17295322526SLorenzo Pieralisi 
173cabe1c81SJames Morse 	if (WARN_ON(!sleep_save_stash))
17495322526SLorenzo Pieralisi 		return -ENOMEM;
17595322526SLorenzo Pieralisi 
17695322526SLorenzo Pieralisi 	return 0;
17795322526SLorenzo Pieralisi }
17895322526SLorenzo Pieralisi early_initcall(cpu_suspend_init);
179