import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / arm64 / kernel / suspend.c
1 #include <linux/percpu.h>
2 #include <linux/slab.h>
3 #include <asm/cacheflush.h>
4 #include <asm/cpu_ops.h>
5 #include <asm/debug-monitors.h>
6 #include <asm/pgtable.h>
7 #include <asm/memory.h>
8 #include <asm/smp_plat.h>
9 #include <asm/suspend.h>
10 #include <asm/tlbflush.h>
11
12 extern phys_addr_t sleep_aee_rec_cpu_dormant_va;
13
14 #define pclog() \
15 do { \
16 __asm__ __volatile__ ( \
17 "mrs x7, mpidr_el1 \n\t" \
18 "ubfx x8, x7, #0, #8 \n\t" \
19 "ubfx x9, x7, #8, #8 \n\t" \
20 "add x8, x8, x9, lsl #2 \n\t" \
21 "ldr x9, =sleep_aee_rec_cpu_dormant_va \n\t" \
22 "ldr x9, [x9] \n\t" \
23 "cbz x9, 1f \n\t" \
24 "adr x10, 1f \n\t" \
25 "str x10, [ x9, x8, lsl #3 ] \n\t" \
26 "1: \n\t" \
27 ::: "x7", "x8", "x9", "x10"); \
28 } while(0)
29
30 extern int __cpu_suspend(unsigned long);
31 /*
32 * This is called by __cpu_suspend() to save the state, and do whatever
33 * flushing is required to ensure that when the CPU goes to sleep we have
34 * the necessary data available when the caches are not searched.
35 *
36 * @arg: Argument to pass to suspend operations
37 * @ptr: CPU context virtual address
38 * @save_ptr: address of the location where the context physical address
39 * must be saved
40 */
41 int __cpu_suspend_finisher(unsigned long arg, struct cpu_suspend_ctx *ptr,
42 phys_addr_t *save_ptr)
43 {
44 int cpu = smp_processor_id();
45
46 *save_ptr = virt_to_phys(ptr);
47
48 cpu_do_suspend(ptr);
49 /*
50 * Only flush the context that must be retrieved with the MMU
51 * off. VA primitives ensure the flush is applied to all
52 * cache levels so context is pushed to DRAM.
53 */
54 __flush_dcache_area(ptr, sizeof(*ptr));
55 __flush_dcache_area(save_ptr, sizeof(*save_ptr));
56
57 return cpu_ops[cpu]->cpu_suspend(arg);
58 }
59
60 /*
61 * This hook is provided so that cpu_suspend code can restore HW
62 * breakpoints as early as possible in the resume path, before reenabling
63 * debug exceptions. Code cannot be run from a CPU PM notifier since by the
64 * time the notifier runs debug exceptions might have been enabled already,
65 * with HW breakpoints registers content still in an unknown state.
66 */
67 void (*hw_breakpoint_restore)(void *);
68 void __init cpu_suspend_set_dbg_restorer(void (*hw_bp_restore)(void *))
69 {
70 /* Prevent multiple restore hook initializations */
71 if (WARN_ON(hw_breakpoint_restore))
72 return;
73 hw_breakpoint_restore = hw_bp_restore;
74 }
75
76 /**
77 * cpu_suspend
78 *
79 * @arg: argument to pass to the finisher function
80 */
81 int cpu_suspend(unsigned long arg)
82 {
83 struct mm_struct *mm = current->active_mm;
84 int ret, cpu = smp_processor_id();
85 unsigned long flags;
86
87 /*
88 * If cpu_ops have not been registered or suspend
89 * has not been initialized, cpu_suspend call fails early.
90 */
91 if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_suspend)
92 return -EOPNOTSUPP;
93
94 /*
95 * From this point debug exceptions are disabled to prevent
96 * updates to mdscr register (saved and restored along with
97 * general purpose registers) from kernel debuggers.
98 */
99 local_dbg_save(flags);
100
101 /*
102 * mm context saved on the stack, it will be restored when
103 * the cpu comes out of reset through the identity mapped
104 * page tables, so that the thread address space is properly
105 * set-up on function return.
106 */
107 ret = __cpu_suspend(arg);
108 pclog();
109
110 if (ret == 0) {
111 cpu_switch_mm(mm->pgd, mm);
112 flush_tlb_all();
113
114 /*
115 * Restore per-cpu offset before any kernel
116 * subsystem relying on it has a chance to run.
117 */
118 set_my_cpu_offset(per_cpu_offset(cpu));
119
120 /*
121 * Restore HW breakpoint registers to sane values
122 * before debug exceptions are possibly reenabled
123 * through local_dbg_restore.
124 */
125 if (hw_breakpoint_restore)
126 hw_breakpoint_restore(NULL);
127 }
128
129 /*
130 * Restore pstate flags. OS lock and mdscr have been already
131 * restored, so from this point onwards, debugging is fully
132 * renabled if it was enabled when core started shutdown.
133 */
134 local_dbg_restore(flags);
135
136 return ret;
137 }
138
139 extern struct sleep_save_sp sleep_save_sp;
140 extern phys_addr_t sleep_idmap_phys;
141
142 static int cpu_suspend_init(void)
143 {
144 void *ctx_ptr;
145
146 /* ctx_ptr is an array of physical addresses */
147 ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL);
148
149 if (WARN_ON(!ctx_ptr))
150 return -ENOMEM;
151
152 sleep_save_sp.save_ptr_stash = ctx_ptr;
153 sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
154 sleep_idmap_phys = virt_to_phys(idmap_pg_dir);
155 __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
156 __flush_dcache_area(&sleep_idmap_phys, sizeof(sleep_idmap_phys));
157
158 return 0;
159 }
160 early_initcall(cpu_suspend_init);