Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / arch / arm64 / kernel / suspend.c
1 #include <linux/ftrace.h>
2 #include <linux/percpu.h>
3 #include <linux/slab.h>
4 #include <asm/alternative.h>
5 #include <asm/cacheflush.h>
6 #include <asm/cpufeature.h>
7 #include <asm/debug-monitors.h>
8 #include <asm/pgtable.h>
9 #include <asm/memory.h>
10 #include <asm/mmu_context.h>
11 #include <asm/smp_plat.h>
12 #include <asm/suspend.h>
13 #include <asm/tlbflush.h>
14
15 extern int __cpu_suspend_enter(unsigned long arg, int (*fn)(unsigned long));
16 /*
17  * This is called by __cpu_suspend_enter() to save the state, and do whatever
18  * flushing is required to ensure that when the CPU goes to sleep we have
19  * the necessary data available when the caches are not searched.
20  *
21  * ptr: CPU context virtual address
22  * save_ptr: address of the location where the context physical address
23  *           must be saved
24  */
25 void notrace __cpu_suspend_save(struct cpu_suspend_ctx *ptr,
26                                 phys_addr_t *save_ptr)
27 {
28         *save_ptr = virt_to_phys(ptr);
29
30         cpu_do_suspend(ptr);
31         /*
32          * Only flush the context that must be retrieved with the MMU
33          * off. VA primitives ensure the flush is applied to all
34          * cache levels so context is pushed to DRAM.
35          */
36         __flush_dcache_area(ptr, sizeof(*ptr));
37         __flush_dcache_area(save_ptr, sizeof(*save_ptr));
38 }
39
40 /*
41  * This hook is provided so that cpu_suspend code can restore HW
42  * breakpoints as early as possible in the resume path, before reenabling
43  * debug exceptions. Code cannot be run from a CPU PM notifier since by the
44  * time the notifier runs debug exceptions might have been enabled already,
45  * with HW breakpoints registers content still in an unknown state.
46  */
47 static void (*hw_breakpoint_restore)(void *);
48 void __init cpu_suspend_set_dbg_restorer(void (*hw_bp_restore)(void *))
49 {
50         /* Prevent multiple restore hook initializations */
51         if (WARN_ON(hw_breakpoint_restore))
52                 return;
53         hw_breakpoint_restore = hw_bp_restore;
54 }
55
56 /*
57  * cpu_suspend
58  *
59  * arg: argument to pass to the finisher function
60  * fn: finisher function pointer
61  *
62  */
63 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
64 {
65         struct mm_struct *mm = current->active_mm;
66         int ret;
67         unsigned long flags;
68
69         /*
70          * From this point debug exceptions are disabled to prevent
71          * updates to mdscr register (saved and restored along with
72          * general purpose registers) from kernel debuggers.
73          */
74         local_dbg_save(flags);
75
76         /*
77          * Function graph tracer state gets incosistent when the kernel
78          * calls functions that never return (aka suspend finishers) hence
79          * disable graph tracing during their execution.
80          */
81         pause_graph_tracing();
82
83         /*
84          * mm context saved on the stack, it will be restored when
85          * the cpu comes out of reset through the identity mapped
86          * page tables, so that the thread address space is properly
87          * set-up on function return.
88          */
89         ret = __cpu_suspend_enter(arg, fn);
90         if (ret == 0) {
91                 /*
92                  * We are resuming from reset with TTBR0_EL1 set to the
93                  * idmap to enable the MMU; set the TTBR0 to the reserved
94                  * page tables to prevent speculative TLB allocations, flush
95                  * the local tlb and set the default tcr_el1.t0sz so that
96                  * the TTBR0 address space set-up is properly restored.
97                  * If the current active_mm != &init_mm we entered cpu_suspend
98                  * with mappings in TTBR0 that must be restored, so we switch
99                  * them back to complete the address space configuration
100                  * restoration before returning.
101                  */
102                 cpu_set_reserved_ttbr0();
103                 local_flush_tlb_all();
104                 cpu_set_default_tcr_t0sz();
105
106                 if (mm != &init_mm)
107                         cpu_switch_mm(mm->pgd, mm);
108
109                 /*
110                  * Restore per-cpu offset before any kernel
111                  * subsystem relying on it has a chance to run.
112                  */
113                 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
114
115                 /*
116                  * PSTATE was not saved over suspend/resume, re-enable any
117                  * detected features that might not have been set correctly.
118                  */
119                 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
120                                 CONFIG_ARM64_PAN));
121
122                 /*
123                  * Restore HW breakpoint registers to sane values
124                  * before debug exceptions are possibly reenabled
125                  * through local_dbg_restore.
126                  */
127                 if (hw_breakpoint_restore)
128                         hw_breakpoint_restore(NULL);
129         }
130
131         unpause_graph_tracing();
132
133         /*
134          * Restore pstate flags. OS lock and mdscr have been already
135          * restored, so from this point onwards, debugging is fully
136          * renabled if it was enabled when core started shutdown.
137          */
138         local_dbg_restore(flags);
139
140         return ret;
141 }
142
143 struct sleep_save_sp sleep_save_sp;
144
145 static int __init cpu_suspend_init(void)
146 {
147         void *ctx_ptr;
148
149         /* ctx_ptr is an array of physical addresses */
150         ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL);
151
152         if (WARN_ON(!ctx_ptr))
153                 return -ENOMEM;
154
155         sleep_save_sp.save_ptr_stash = ctx_ptr;
156         sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
157         __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
158
159         return 0;
160 }
161 early_initcall(cpu_suspend_init);