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