xref: /openbmc/linux/arch/arm64/kernel/suspend.c (revision 39d08e8318c49c5fd2bda9cadfd2bc54d2d3dfd8)
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>
7d0854412SJames Morse #include <asm/alternative.h>
895322526SLorenzo Pieralisi #include <asm/cacheflush.h>
9d0854412SJames Morse #include <asm/cpufeature.h>
100fbeb318SJames Morse #include <asm/daifflags.h>
1195322526SLorenzo Pieralisi #include <asm/debug-monitors.h>
12d0854412SJames Morse #include <asm/exec.h>
13*39d08e83SCatalin Marinas #include <asm/mte.h>
1495322526SLorenzo Pieralisi #include <asm/memory.h>
15f43c2718SLorenzo Pieralisi #include <asm/mmu_context.h>
1695322526SLorenzo Pieralisi #include <asm/smp_plat.h>
1795322526SLorenzo Pieralisi #include <asm/suspend.h>
1895322526SLorenzo Pieralisi 
1995322526SLorenzo Pieralisi /*
20cabe1c81SJames Morse  * This is allocated by cpu_suspend_init(), and used to store a pointer to
21cabe1c81SJames Morse  * the 'struct sleep_stack_data' the contains a particular CPUs state.
2295322526SLorenzo Pieralisi  */
23cabe1c81SJames Morse unsigned long *sleep_save_stash;
2495322526SLorenzo Pieralisi 
2565c021bbSLorenzo Pieralisi /*
2665c021bbSLorenzo Pieralisi  * This hook is provided so that cpu_suspend code can restore HW
2765c021bbSLorenzo Pieralisi  * breakpoints as early as possible in the resume path, before reenabling
2865c021bbSLorenzo Pieralisi  * debug exceptions. Code cannot be run from a CPU PM notifier since by the
2965c021bbSLorenzo Pieralisi  * time the notifier runs debug exceptions might have been enabled already,
3065c021bbSLorenzo Pieralisi  * with HW breakpoints registers content still in an unknown state.
3165c021bbSLorenzo Pieralisi  */
32d7a83d12SWill Deacon static int (*hw_breakpoint_restore)(unsigned int);
33d7a83d12SWill Deacon void __init cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int))
3465c021bbSLorenzo Pieralisi {
3565c021bbSLorenzo Pieralisi 	/* Prevent multiple restore hook initializations */
3665c021bbSLorenzo Pieralisi 	if (WARN_ON(hw_breakpoint_restore))
3765c021bbSLorenzo Pieralisi 		return;
3865c021bbSLorenzo Pieralisi 	hw_breakpoint_restore = hw_bp_restore;
3965c021bbSLorenzo Pieralisi }
4065c021bbSLorenzo Pieralisi 
41adc9b2dfSJames Morse void notrace __cpu_suspend_exit(void)
42714f5992SLorenzo Pieralisi {
43d7a83d12SWill Deacon 	unsigned int cpu = smp_processor_id();
44d7a83d12SWill Deacon 
45f43c2718SLorenzo Pieralisi 	/*
469e8e865bSMark Rutland 	 * We are resuming from reset with the idmap active in TTBR0_EL1.
479e8e865bSMark Rutland 	 * We must uninstall the idmap and restore the expected MMU
489e8e865bSMark Rutland 	 * state before we can possibly return to userspace.
49f43c2718SLorenzo Pieralisi 	 */
509e8e865bSMark Rutland 	cpu_uninstall_idmap();
51fb4a9602SLorenzo Pieralisi 
525ffdfaedSVladimir Murzin 	/* Restore CnP bit in TTBR1_EL1 */
535ffdfaedSVladimir Murzin 	if (system_supports_cnp())
545ffdfaedSVladimir Murzin 		cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
555ffdfaedSVladimir Murzin 
56fb4a9602SLorenzo Pieralisi 	/*
57d0854412SJames Morse 	 * PSTATE was not saved over suspend/resume, re-enable any detected
58d0854412SJames Morse 	 * features that might not have been set correctly.
59d0854412SJames Morse 	 */
60e1281f56SJames Morse 	__uaccess_enable_hw_pan();
61d0854412SJames Morse 	uao_thread_switch(current);
62d0854412SJames Morse 
63d0854412SJames Morse 	/*
6465c021bbSLorenzo Pieralisi 	 * Restore HW breakpoint registers to sane values
6565c021bbSLorenzo Pieralisi 	 * before debug exceptions are possibly reenabled
660fbeb318SJames Morse 	 * by cpu_suspend()s local_daif_restore() call.
6765c021bbSLorenzo Pieralisi 	 */
6865c021bbSLorenzo Pieralisi 	if (hw_breakpoint_restore)
69d7a83d12SWill Deacon 		hw_breakpoint_restore(cpu);
70647d0519SMarc Zyngier 
71647d0519SMarc Zyngier 	/*
72647d0519SMarc Zyngier 	 * On resume, firmware implementing dynamic mitigation will
73647d0519SMarc Zyngier 	 * have turned the mitigation on. If the user has forcefully
74647d0519SMarc Zyngier 	 * disabled it, make sure their wishes are obeyed.
75647d0519SMarc Zyngier 	 */
76647d0519SMarc Zyngier 	if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE)
77647d0519SMarc Zyngier 		arm64_set_ssbd_mitigation(false);
78*39d08e83SCatalin Marinas 
79*39d08e83SCatalin Marinas 	/* Restore additional MTE-specific configuration */
80*39d08e83SCatalin Marinas 	mte_suspend_exit();
8195322526SLorenzo Pieralisi }
8295322526SLorenzo Pieralisi 
83adc9b2dfSJames Morse /*
84adc9b2dfSJames Morse  * cpu_suspend
85adc9b2dfSJames Morse  *
86adc9b2dfSJames Morse  * arg: argument to pass to the finisher function
87adc9b2dfSJames Morse  * fn: finisher function pointer
88adc9b2dfSJames Morse  *
89adc9b2dfSJames Morse  */
90adc9b2dfSJames Morse int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
91adc9b2dfSJames Morse {
92adc9b2dfSJames Morse 	int ret = 0;
93adc9b2dfSJames Morse 	unsigned long flags;
94adc9b2dfSJames Morse 	struct sleep_stack_data state;
95adc9b2dfSJames Morse 
96adc9b2dfSJames Morse 	/*
97adc9b2dfSJames Morse 	 * From this point debug exceptions are disabled to prevent
98adc9b2dfSJames Morse 	 * updates to mdscr register (saved and restored along with
99adc9b2dfSJames Morse 	 * general purpose registers) from kernel debuggers.
100adc9b2dfSJames Morse 	 */
1010fbeb318SJames Morse 	flags = local_daif_save();
102adc9b2dfSJames Morse 
103adc9b2dfSJames Morse 	/*
104adc9b2dfSJames Morse 	 * Function graph tracer state gets incosistent when the kernel
105adc9b2dfSJames Morse 	 * calls functions that never return (aka suspend finishers) hence
106adc9b2dfSJames Morse 	 * disable graph tracing during their execution.
107adc9b2dfSJames Morse 	 */
108adc9b2dfSJames Morse 	pause_graph_tracing();
109adc9b2dfSJames Morse 
110adc9b2dfSJames Morse 	if (__cpu_suspend_enter(&state)) {
111adc9b2dfSJames Morse 		/* Call the suspend finisher */
112adc9b2dfSJames Morse 		ret = fn(arg);
113adc9b2dfSJames Morse 
114adc9b2dfSJames Morse 		/*
115adc9b2dfSJames Morse 		 * Never gets here, unless the suspend finisher fails.
116adc9b2dfSJames Morse 		 * Successful cpu_suspend() should return from cpu_resume(),
117adc9b2dfSJames Morse 		 * returning through this code path is considered an error
118adc9b2dfSJames Morse 		 * If the return value is set to 0 force ret = -EOPNOTSUPP
119adc9b2dfSJames Morse 		 * to make sure a proper error condition is propagated
120adc9b2dfSJames Morse 		 */
121adc9b2dfSJames Morse 		if (!ret)
122adc9b2dfSJames Morse 			ret = -EOPNOTSUPP;
123adc9b2dfSJames Morse 	} else {
124adc9b2dfSJames Morse 		__cpu_suspend_exit();
125adc9b2dfSJames Morse 	}
126adc9b2dfSJames Morse 
127de818bd4SLorenzo Pieralisi 	unpause_graph_tracing();
128de818bd4SLorenzo Pieralisi 
12995322526SLorenzo Pieralisi 	/*
13095322526SLorenzo Pieralisi 	 * Restore pstate flags. OS lock and mdscr have been already
13195322526SLorenzo Pieralisi 	 * restored, so from this point onwards, debugging is fully
13295322526SLorenzo Pieralisi 	 * renabled if it was enabled when core started shutdown.
13395322526SLorenzo Pieralisi 	 */
1340fbeb318SJames Morse 	local_daif_restore(flags);
13595322526SLorenzo Pieralisi 
13695322526SLorenzo Pieralisi 	return ret;
13795322526SLorenzo Pieralisi }
13895322526SLorenzo Pieralisi 
13918ab7db6SLorenzo Pieralisi static int __init cpu_suspend_init(void)
14095322526SLorenzo Pieralisi {
14195322526SLorenzo Pieralisi 	/* ctx_ptr is an array of physical addresses */
142cabe1c81SJames Morse 	sleep_save_stash = kcalloc(mpidr_hash_size(), sizeof(*sleep_save_stash),
143cabe1c81SJames Morse 				   GFP_KERNEL);
14495322526SLorenzo Pieralisi 
145cabe1c81SJames Morse 	if (WARN_ON(!sleep_save_stash))
14695322526SLorenzo Pieralisi 		return -ENOMEM;
14795322526SLorenzo Pieralisi 
14895322526SLorenzo Pieralisi 	return 0;
14995322526SLorenzo Pieralisi }
15095322526SLorenzo Pieralisi early_initcall(cpu_suspend_init);
151