suspend.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <asm/alternative.h>
  8. #include <asm/cacheflush.h>
  9. #include <asm/cpufeature.h>
  10. #include <asm/cpuidle.h>
  11. #include <asm/daifflags.h>
  12. #include <asm/debug-monitors.h>
  13. #include <asm/exec.h>
  14. #include <asm/mte.h>
  15. #include <asm/memory.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/smp_plat.h>
  18. #include <asm/suspend.h>
  19. /*
  20. * This is allocated by cpu_suspend_init(), and used to store a pointer to
  21. * the 'struct sleep_stack_data' the contains a particular CPUs state.
  22. */
  23. unsigned long *sleep_save_stash;
  24. /*
  25. * This hook is provided so that cpu_suspend code can restore HW
  26. * breakpoints as early as possible in the resume path, before reenabling
  27. * debug exceptions. Code cannot be run from a CPU PM notifier since by the
  28. * time the notifier runs debug exceptions might have been enabled already,
  29. * with HW breakpoints registers content still in an unknown state.
  30. */
  31. static int (*hw_breakpoint_restore)(unsigned int);
  32. void __init cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int))
  33. {
  34. /* Prevent multiple restore hook initializations */
  35. if (WARN_ON(hw_breakpoint_restore))
  36. return;
  37. hw_breakpoint_restore = hw_bp_restore;
  38. }
  39. void notrace __cpu_suspend_exit(void)
  40. {
  41. unsigned int cpu = smp_processor_id();
  42. mte_suspend_exit();
  43. /*
  44. * We are resuming from reset with the idmap active in TTBR0_EL1.
  45. * We must uninstall the idmap and restore the expected MMU
  46. * state before we can possibly return to userspace.
  47. */
  48. cpu_uninstall_idmap();
  49. /* Restore CnP bit in TTBR1_EL1 */
  50. if (system_supports_cnp())
  51. cpu_replace_ttbr1(lm_alias(swapper_pg_dir), idmap_pg_dir);
  52. /*
  53. * PSTATE was not saved over suspend/resume, re-enable any detected
  54. * features that might not have been set correctly.
  55. */
  56. __uaccess_enable_hw_pan();
  57. /*
  58. * Restore HW breakpoint registers to sane values
  59. * before debug exceptions are possibly reenabled
  60. * by cpu_suspend()s local_daif_restore() call.
  61. */
  62. if (hw_breakpoint_restore)
  63. hw_breakpoint_restore(cpu);
  64. /*
  65. * On resume, firmware implementing dynamic mitigation will
  66. * have turned the mitigation on. If the user has forcefully
  67. * disabled it, make sure their wishes are obeyed.
  68. */
  69. spectre_v4_enable_mitigation(NULL);
  70. /* Restore additional feature-specific configuration */
  71. ptrauth_suspend_exit();
  72. }
  73. /*
  74. * cpu_suspend
  75. *
  76. * arg: argument to pass to the finisher function
  77. * fn: finisher function pointer
  78. *
  79. */
  80. int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
  81. {
  82. int ret = 0;
  83. unsigned long flags;
  84. struct sleep_stack_data state;
  85. struct arm_cpuidle_irq_context context;
  86. /* Report any MTE async fault before going to suspend */
  87. mte_suspend_enter();
  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. flags = local_daif_save();
  94. /*
  95. * Function graph tracer state gets inconsistent when the kernel
  96. * calls functions that never return (aka suspend finishers) hence
  97. * disable graph tracing during their execution.
  98. */
  99. pause_graph_tracing();
  100. /*
  101. * Switch to using DAIF.IF instead of PMR in order to reliably
  102. * resume if we're using pseudo-NMIs.
  103. */
  104. arm_cpuidle_save_irq_context(&context);
  105. if (__cpu_suspend_enter(&state)) {
  106. /* Call the suspend finisher */
  107. ret = fn(arg);
  108. /*
  109. * Never gets here, unless the suspend finisher fails.
  110. * Successful cpu_suspend() should return from cpu_resume(),
  111. * returning through this code path is considered an error
  112. * If the return value is set to 0 force ret = -EOPNOTSUPP
  113. * to make sure a proper error condition is propagated
  114. */
  115. if (!ret)
  116. ret = -EOPNOTSUPP;
  117. } else {
  118. RCU_NONIDLE(__cpu_suspend_exit());
  119. }
  120. arm_cpuidle_restore_irq_context(&context);
  121. unpause_graph_tracing();
  122. /*
  123. * Restore pstate flags. OS lock and mdscr have been already
  124. * restored, so from this point onwards, debugging is fully
  125. * reenabled if it was enabled when core started shutdown.
  126. */
  127. local_daif_restore(flags);
  128. return ret;
  129. }
  130. static int __init cpu_suspend_init(void)
  131. {
  132. /* ctx_ptr is an array of physical addresses */
  133. sleep_save_stash = kcalloc(mpidr_hash_size(), sizeof(*sleep_save_stash),
  134. GFP_KERNEL);
  135. if (WARN_ON(!sleep_save_stash))
  136. return -ENOMEM;
  137. return 0;
  138. }
  139. early_initcall(cpu_suspend_init);