1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/ftrace.h> 3 #include <linux/percpu.h> 4 #include <linux/slab.h> 5 #include <linux/uaccess.h> 6 #include <linux/pgtable.h> 7 #include <linux/cpuidle.h> 8 #include <linux/sched.h> 9 #include <asm/alternative.h> 10 #include <asm/cacheflush.h> 11 #include <asm/cpufeature.h> 12 #include <asm/cpuidle.h> 13 #include <asm/daifflags.h> 14 #include <asm/debug-monitors.h> 15 #include <asm/exec.h> 16 #include <asm/fpsimd.h> 17 #include <asm/mte.h> 18 #include <asm/memory.h> 19 #include <asm/mmu_context.h> 20 #include <asm/smp_plat.h> 21 #include <asm/suspend.h> 22 23 /* 24 * This is allocated by cpu_suspend_init(), and used to store a pointer to 25 * the 'struct sleep_stack_data' the contains a particular CPUs state. 26 */ 27 unsigned long *sleep_save_stash; 28 29 /* 30 * This hook is provided so that cpu_suspend code can restore HW 31 * breakpoints as early as possible in the resume path, before reenabling 32 * debug exceptions. Code cannot be run from a CPU PM notifier since by the 33 * time the notifier runs debug exceptions might have been enabled already, 34 * with HW breakpoints registers content still in an unknown state. 35 */ 36 static int (*hw_breakpoint_restore)(unsigned int); 37 void __init cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int)) 38 { 39 /* Prevent multiple restore hook initializations */ 40 if (WARN_ON(hw_breakpoint_restore)) 41 return; 42 hw_breakpoint_restore = hw_bp_restore; 43 } 44 45 void notrace __cpu_suspend_exit(void) 46 { 47 unsigned int cpu = smp_processor_id(); 48 49 mte_suspend_exit(); 50 51 /* 52 * We are resuming from reset with the idmap active in TTBR0_EL1. 53 * We must uninstall the idmap and restore the expected MMU 54 * state before we can possibly return to userspace. 55 */ 56 cpu_uninstall_idmap(); 57 58 /* Restore CnP bit in TTBR1_EL1 */ 59 if (system_supports_cnp()) 60 cpu_enable_swapper_cnp(); 61 62 /* 63 * PSTATE was not saved over suspend/resume, re-enable any detected 64 * features that might not have been set correctly. 65 */ 66 if (alternative_has_cap_unlikely(ARM64_HAS_DIT)) 67 set_pstate_dit(1); 68 __uaccess_enable_hw_pan(); 69 70 /* 71 * Restore HW breakpoint registers to sane values 72 * before debug exceptions are possibly reenabled 73 * by cpu_suspend()s local_daif_restore() call. 74 */ 75 if (hw_breakpoint_restore) 76 hw_breakpoint_restore(cpu); 77 78 /* 79 * On resume, firmware implementing dynamic mitigation will 80 * have turned the mitigation on. If the user has forcefully 81 * disabled it, make sure their wishes are obeyed. 82 */ 83 spectre_v4_enable_mitigation(NULL); 84 85 sme_suspend_exit(); 86 87 /* Restore additional feature-specific configuration */ 88 ptrauth_suspend_exit(); 89 } 90 91 /* 92 * cpu_suspend 93 * 94 * arg: argument to pass to the finisher function 95 * fn: finisher function pointer 96 * 97 */ 98 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long)) 99 { 100 int ret = 0; 101 unsigned long flags; 102 struct sleep_stack_data state; 103 bool idle = is_idle_task(current); 104 105 /* 106 * Some portions of CPU state (e.g. PSTATE.{PAN,DIT}) are initialized 107 * before alternatives are patched, but are only restored by 108 * __cpu_suspend_exit() after alternatives are patched. To avoid 109 * accidentally losing these bits we must not attempt to suspend until 110 * after alternatives have been patched. 111 */ 112 WARN_ON(!system_capabilities_finalized()); 113 114 /* Report any MTE async fault before going to suspend */ 115 mte_suspend_enter(); 116 117 /* 118 * From this point debug exceptions are disabled to prevent 119 * updates to mdscr register (saved and restored along with 120 * general purpose registers) from kernel debuggers. 121 * 122 * Strictly speaking the trace_hardirqs_off() here is superfluous, 123 * hardirqs should be firmly off by now. This really ought to use 124 * something like raw_local_daif_save(). 125 * 126 * This also unmasks interrupts in PMR in order to reliably 127 * resume if we're using pseudo-NMIs. 128 */ 129 flags = local_daif_save(); 130 131 /* 132 * Function graph tracer state gets inconsistent when the kernel 133 * calls functions that never return (aka suspend finishers) hence 134 * disable graph tracing during their execution. 135 */ 136 pause_graph_tracing(); 137 138 if (idle) 139 ct_cpuidle_enter(); 140 141 if (__cpu_suspend_enter(&state)) { 142 /* Call the suspend finisher */ 143 ret = fn(arg); 144 145 /* 146 * Never gets here, unless the suspend finisher fails. 147 * Successful cpu_suspend() should return from cpu_resume(), 148 * returning through this code path is considered an error 149 * If the return value is set to 0 force ret = -EOPNOTSUPP 150 * to make sure a proper error condition is propagated 151 */ 152 if (!ret) 153 ret = -EOPNOTSUPP; 154 155 if (idle) 156 ct_cpuidle_exit(); 157 } else { 158 if (idle) 159 ct_cpuidle_exit(); 160 __cpu_suspend_exit(); 161 } 162 163 unpause_graph_tracing(); 164 165 /* 166 * Restore pstate flags. OS lock and mdscr have been already 167 * restored, so from this point onwards, debugging is fully 168 * reenabled if it was enabled when core started shutdown. 169 */ 170 local_daif_restore(flags); 171 172 return ret; 173 } 174 175 static int __init cpu_suspend_init(void) 176 { 177 /* ctx_ptr is an array of physical addresses */ 178 sleep_save_stash = kzalloc_objs(*sleep_save_stash, mpidr_hash_size()); 179 180 if (WARN_ON(!sleep_save_stash)) 181 return -ENOMEM; 182 183 return 0; 184 } 185 early_initcall(cpu_suspend_init); 186