xref: /openbmc/linux/arch/arm64/kernel/suspend.c (revision 4949009e)
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/mmu_context.h>
9 #include <asm/smp_plat.h>
10 #include <asm/suspend.h>
11 #include <asm/tlbflush.h>
12 
13 extern int __cpu_suspend_enter(unsigned long arg, int (*fn)(unsigned long));
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: CPU context 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 cpu_suspend_ctx *ptr,
24 				phys_addr_t *save_ptr)
25 {
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));
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 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 /**
55  * cpu_suspend() - function to enter a low-power state
56  * @arg: argument to pass to CPU suspend operations
57  *
58  * Return: 0 on success, -EOPNOTSUPP if CPU suspend hook not initialized, CPU
59  * operations back-end error code otherwise.
60  */
61 int cpu_suspend(unsigned long arg)
62 {
63 	int cpu = smp_processor_id();
64 
65 	/*
66 	 * If cpu_ops have not been registered or suspend
67 	 * has not been initialized, cpu_suspend call fails early.
68 	 */
69 	if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_suspend)
70 		return -EOPNOTSUPP;
71 	return cpu_ops[cpu]->cpu_suspend(arg);
72 }
73 
74 /*
75  * __cpu_suspend
76  *
77  * arg: argument to pass to the finisher function
78  * fn: finisher function pointer
79  *
80  */
81 int __cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
82 {
83 	struct mm_struct *mm = current->active_mm;
84 	int ret;
85 	unsigned long flags;
86 
87 	/*
88 	 * From this point debug exceptions are disabled to prevent
89 	 * updates to mdscr register (saved and restored along with
90 	 * general purpose registers) from kernel debuggers.
91 	 */
92 	local_dbg_save(flags);
93 
94 	/*
95 	 * mm context saved on the stack, it will be restored when
96 	 * the cpu comes out of reset through the identity mapped
97 	 * page tables, so that the thread address space is properly
98 	 * set-up on function return.
99 	 */
100 	ret = __cpu_suspend_enter(arg, fn);
101 	if (ret == 0) {
102 		/*
103 		 * We are resuming from reset with TTBR0_EL1 set to the
104 		 * idmap to enable the MMU; restore the active_mm mappings in
105 		 * TTBR0_EL1 unless the active_mm == &init_mm, in which case
106 		 * the thread entered __cpu_suspend with TTBR0_EL1 set to
107 		 * reserved TTBR0 page tables and should be restored as such.
108 		 */
109 		if (mm == &init_mm)
110 			cpu_set_reserved_ttbr0();
111 		else
112 			cpu_switch_mm(mm->pgd, mm);
113 
114 		flush_tlb_all();
115 
116 		/*
117 		 * Restore per-cpu offset before any kernel
118 		 * subsystem relying on it has a chance to run.
119 		 */
120 		set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
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 	/*
132 	 * Restore pstate flags. OS lock and mdscr have been already
133 	 * restored, so from this point onwards, debugging is fully
134 	 * renabled if it was enabled when core started shutdown.
135 	 */
136 	local_dbg_restore(flags);
137 
138 	return ret;
139 }
140 
141 struct sleep_save_sp sleep_save_sp;
142 phys_addr_t sleep_idmap_phys;
143 
144 static int __init cpu_suspend_init(void)
145 {
146 	void *ctx_ptr;
147 
148 	/* ctx_ptr is an array of physical addresses */
149 	ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL);
150 
151 	if (WARN_ON(!ctx_ptr))
152 		return -ENOMEM;
153 
154 	sleep_save_sp.save_ptr_stash = ctx_ptr;
155 	sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
156 	sleep_idmap_phys = virt_to_phys(idmap_pg_dir);
157 	__flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
158 	__flush_dcache_area(&sleep_idmap_phys, sizeof(sleep_idmap_phys));
159 
160 	return 0;
161 }
162 early_initcall(cpu_suspend_init);
163