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