Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux
[linux-2.6-block.git] / arch / arm64 / kernel / suspend.c
CommitLineData
de818bd4 1#include <linux/ftrace.h>
fb4a9602 2#include <linux/percpu.h>
95322526
LP
3#include <linux/slab.h>
4#include <asm/cacheflush.h>
95322526
LP
5#include <asm/debug-monitors.h>
6#include <asm/pgtable.h>
7#include <asm/memory.h>
f43c2718 8#include <asm/mmu_context.h>
95322526
LP
9#include <asm/smp_plat.h>
10#include <asm/suspend.h>
11#include <asm/tlbflush.h>
12
714f5992 13extern int __cpu_suspend_enter(unsigned long arg, int (*fn)(unsigned long));
95322526 14/*
714f5992 15 * This is called by __cpu_suspend_enter() to save the state, and do whatever
95322526
LP
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 *
714f5992
LP
19 * ptr: CPU context virtual address
20 * save_ptr: address of the location where the context physical address
21 * must be saved
95322526 22 */
714f5992
LP
23void notrace __cpu_suspend_save(struct cpu_suspend_ctx *ptr,
24 phys_addr_t *save_ptr)
95322526 25{
95322526
LP
26 *save_ptr = virt_to_phys(ptr);
27
28 cpu_do_suspend(ptr);
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));
95322526
LP
36}
37
65c021bb
LP
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 */
01b305a2 45static void (*hw_breakpoint_restore)(void *);
65c021bb
LP
46void __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
714f5992 54/*
af391b15 55 * cpu_suspend
714f5992
LP
56 *
57 * arg: argument to pass to the finisher function
58 * fn: finisher function pointer
59 *
60 */
af391b15 61int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
714f5992 62{
714f5992
LP
63 int ret;
64 unsigned long flags;
95322526
LP
65
66 /*
67 * From this point debug exceptions are disabled to prevent
68 * updates to mdscr register (saved and restored along with
69 * general purpose registers) from kernel debuggers.
70 */
71 local_dbg_save(flags);
72
de818bd4
LP
73 /*
74 * Function graph tracer state gets incosistent when the kernel
75 * calls functions that never return (aka suspend finishers) hence
76 * disable graph tracing during their execution.
77 */
78 pause_graph_tracing();
79
95322526
LP
80 /*
81 * mm context saved on the stack, it will be restored when
82 * the cpu comes out of reset through the identity mapped
83 * page tables, so that the thread address space is properly
84 * set-up on function return.
85 */
714f5992 86 ret = __cpu_suspend_enter(arg, fn);
95322526 87 if (ret == 0) {
f43c2718 88 /*
9e8e865b
MR
89 * We are resuming from reset with the idmap active in TTBR0_EL1.
90 * We must uninstall the idmap and restore the expected MMU
91 * state before we can possibly return to userspace.
f43c2718 92 */
9e8e865b 93 cpu_uninstall_idmap();
fb4a9602
LP
94
95 /*
96 * Restore per-cpu offset before any kernel
97 * subsystem relying on it has a chance to run.
98 */
714f5992 99 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
fb4a9602 100
65c021bb
LP
101 /*
102 * Restore HW breakpoint registers to sane values
103 * before debug exceptions are possibly reenabled
104 * through local_dbg_restore.
105 */
106 if (hw_breakpoint_restore)
107 hw_breakpoint_restore(NULL);
95322526
LP
108 }
109
de818bd4
LP
110 unpause_graph_tracing();
111
95322526
LP
112 /*
113 * Restore pstate flags. OS lock and mdscr have been already
114 * restored, so from this point onwards, debugging is fully
115 * renabled if it was enabled when core started shutdown.
116 */
117 local_dbg_restore(flags);
118
119 return ret;
120}
121
c3684fbb 122struct sleep_save_sp sleep_save_sp;
95322526 123
18ab7db6 124static int __init cpu_suspend_init(void)
95322526
LP
125{
126 void *ctx_ptr;
127
128 /* ctx_ptr is an array of physical addresses */
129 ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL);
130
131 if (WARN_ON(!ctx_ptr))
132 return -ENOMEM;
133
134 sleep_save_sp.save_ptr_stash = ctx_ptr;
135 sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
95322526 136 __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
95322526
LP
137
138 return 0;
139}
140early_initcall(cpu_suspend_init);