suspend.c 5.1 KB

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