arm64: kernel: Rework finisher callback out of __cpu_suspend_enter()
[deliverable/linux.git] / arch / arm64 / kernel / suspend.c
1 #include <linux/ftrace.h>
2 #include <linux/percpu.h>
3 #include <linux/slab.h>
4 #include <asm/cacheflush.h>
5 #include <asm/debug-monitors.h>
6 #include <asm/pgtable.h>
7 #include <asm/memory.h>
8 #include <asm/mmu_context.h>
9 #include <asm/smp_plat.h>
10 #include <asm/suspend.h>
11 #include <asm/tlbflush.h>
12
13
14 /*
15 * This is called by __cpu_suspend_enter() to save the state, and do whatever
16 * flushing is required to ensure that when the CPU goes to sleep we have
17 * the necessary data available when the caches are not searched.
18 *
19 * ptr: sleep_stack_data containing cpu state virtual address.
20 * save_ptr: address of the location where the context physical address
21 * must be saved
22 */
23 void notrace __cpu_suspend_save(struct sleep_stack_data *ptr,
24 phys_addr_t *save_ptr)
25 {
26 *save_ptr = virt_to_phys(ptr);
27
28 cpu_do_suspend(&ptr->system_regs);
29 /*
30 * Only flush the context that must be retrieved with the MMU
31 * off. VA primitives ensure the flush is applied to all
32 * cache levels so context is pushed to DRAM.
33 */
34 __flush_dcache_area(ptr, sizeof(*ptr));
35 __flush_dcache_area(save_ptr, sizeof(*save_ptr));
36 }
37
38 /*
39 * This hook is provided so that cpu_suspend code can restore HW
40 * breakpoints as early as possible in the resume path, before reenabling
41 * debug exceptions. Code cannot be run from a CPU PM notifier since by the
42 * time the notifier runs debug exceptions might have been enabled already,
43 * with HW breakpoints registers content still in an unknown state.
44 */
45 static void (*hw_breakpoint_restore)(void *);
46 void __init cpu_suspend_set_dbg_restorer(void (*hw_bp_restore)(void *))
47 {
48 /* Prevent multiple restore hook initializations */
49 if (WARN_ON(hw_breakpoint_restore))
50 return;
51 hw_breakpoint_restore = hw_bp_restore;
52 }
53
54 void notrace __cpu_suspend_exit(void)
55 {
56 /*
57 * We are resuming from reset with the idmap active in TTBR0_EL1.
58 * We must uninstall the idmap and restore the expected MMU
59 * state before we can possibly return to userspace.
60 */
61 cpu_uninstall_idmap();
62
63 /*
64 * Restore per-cpu offset before any kernel
65 * subsystem relying on it has a chance to run.
66 */
67 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
68
69 /*
70 * Restore HW breakpoint registers to sane values
71 * before debug exceptions are possibly reenabled
72 * through local_dbg_restore.
73 */
74 if (hw_breakpoint_restore)
75 hw_breakpoint_restore(NULL);
76 }
77
78 /*
79 * cpu_suspend
80 *
81 * arg: argument to pass to the finisher function
82 * fn: finisher function pointer
83 *
84 */
85 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
86 {
87 int ret = 0;
88 unsigned long flags;
89 struct sleep_stack_data state;
90
91 /*
92 * From this point debug exceptions are disabled to prevent
93 * updates to mdscr register (saved and restored along with
94 * general purpose registers) from kernel debuggers.
95 */
96 local_dbg_save(flags);
97
98 /*
99 * Function graph tracer state gets incosistent when the kernel
100 * calls functions that never return (aka suspend finishers) hence
101 * disable graph tracing during their execution.
102 */
103 pause_graph_tracing();
104
105 if (__cpu_suspend_enter(&state)) {
106 /* Call the suspend finisher */
107 ret = fn(arg);
108
109 /*
110 * Never gets here, unless the suspend finisher fails.
111 * Successful cpu_suspend() should return from cpu_resume(),
112 * returning through this code path is considered an error
113 * If the return value is set to 0 force ret = -EOPNOTSUPP
114 * to make sure a proper error condition is propagated
115 */
116 if (!ret)
117 ret = -EOPNOTSUPP;
118 } else {
119 __cpu_suspend_exit();
120 }
121
122 unpause_graph_tracing();
123
124 /*
125 * Restore pstate flags. OS lock and mdscr have been already
126 * restored, so from this point onwards, debugging is fully
127 * renabled if it was enabled when core started shutdown.
128 */
129 local_dbg_restore(flags);
130
131 return ret;
132 }
133
134 struct sleep_save_sp sleep_save_sp;
135
136 static int __init cpu_suspend_init(void)
137 {
138 void *ctx_ptr;
139
140 /* ctx_ptr is an array of physical addresses */
141 ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL);
142
143 if (WARN_ON(!ctx_ptr))
144 return -ENOMEM;
145
146 sleep_save_sp.save_ptr_stash = ctx_ptr;
147 sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
148 __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
149
150 return 0;
151 }
152 early_initcall(cpu_suspend_init);
This page took 0.041876 seconds and 5 git commands to generate.