suspend.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <linux/ftrace.h>
  2. #include <linux/percpu.h>
  3. #include <linux/slab.h>
  4. #include <asm/alternative.h>
  5. #include <asm/cacheflush.h>
  6. #include <asm/cpufeature.h>
  7. #include <asm/debug-monitors.h>
  8. #include <asm/pgtable.h>
  9. #include <asm/memory.h>
  10. #include <asm/mmu_context.h>
  11. #include <asm/smp_plat.h>
  12. #include <asm/suspend.h>
  13. #include <asm/tlbflush.h>
  14. extern int __cpu_suspend_enter(unsigned long arg, int (*fn)(unsigned long));
  15. /*
  16. * This is called by __cpu_suspend_enter() to save the state, and do whatever
  17. * flushing is required to ensure that when the CPU goes to sleep we have
  18. * the necessary data available when the caches are not searched.
  19. *
  20. * ptr: CPU context virtual address
  21. * save_ptr: address of the location where the context physical address
  22. * must be saved
  23. */
  24. void notrace __cpu_suspend_save(struct cpu_suspend_ctx *ptr,
  25. phys_addr_t *save_ptr)
  26. {
  27. *save_ptr = virt_to_phys(ptr);
  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. * This hook is provided so that cpu_suspend code can restore HW
  39. * breakpoints as early as possible in the resume path, before reenabling
  40. * debug exceptions. Code cannot be run from a CPU PM notifier since by the
  41. * time the notifier runs debug exceptions might have been enabled already,
  42. * with HW breakpoints registers content still in an unknown state.
  43. */
  44. static void (*hw_breakpoint_restore)(void *);
  45. void __init cpu_suspend_set_dbg_restorer(void (*hw_bp_restore)(void *))
  46. {
  47. /* Prevent multiple restore hook initializations */
  48. if (WARN_ON(hw_breakpoint_restore))
  49. return;
  50. hw_breakpoint_restore = hw_bp_restore;
  51. }
  52. /*
  53. * cpu_suspend
  54. *
  55. * arg: argument to pass to the finisher function
  56. * fn: finisher function pointer
  57. *
  58. */
  59. int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
  60. {
  61. struct mm_struct *mm = current->active_mm;
  62. int ret;
  63. unsigned long flags;
  64. /*
  65. * From this point debug exceptions are disabled to prevent
  66. * updates to mdscr register (saved and restored along with
  67. * general purpose registers) from kernel debuggers.
  68. */
  69. local_dbg_save(flags);
  70. /*
  71. * Function graph tracer state gets incosistent when the kernel
  72. * calls functions that never return (aka suspend finishers) hence
  73. * disable graph tracing during their execution.
  74. */
  75. pause_graph_tracing();
  76. /*
  77. * mm context saved on the stack, it will be restored when
  78. * the cpu comes out of reset through the identity mapped
  79. * page tables, so that the thread address space is properly
  80. * set-up on function return.
  81. */
  82. ret = __cpu_suspend_enter(arg, fn);
  83. if (ret == 0) {
  84. /*
  85. * We are resuming from reset with TTBR0_EL1 set to the
  86. * idmap to enable the MMU; set the TTBR0 to the reserved
  87. * page tables to prevent speculative TLB allocations, flush
  88. * the local tlb and set the default tcr_el1.t0sz so that
  89. * the TTBR0 address space set-up is properly restored.
  90. * If the current active_mm != &init_mm we entered cpu_suspend
  91. * with mappings in TTBR0 that must be restored, so we switch
  92. * them back to complete the address space configuration
  93. * restoration before returning.
  94. */
  95. cpu_set_reserved_ttbr0();
  96. local_flush_tlb_all();
  97. cpu_set_default_tcr_t0sz();
  98. if (mm != &init_mm)
  99. cpu_switch_mm(mm->pgd, mm);
  100. /*
  101. * Restore per-cpu offset before any kernel
  102. * subsystem relying on it has a chance to run.
  103. */
  104. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  105. /*
  106. * PSTATE was not saved over suspend/resume, re-enable any
  107. * detected features that might not have been set correctly.
  108. */
  109. asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
  110. CONFIG_ARM64_PAN));
  111. /*
  112. * Restore HW breakpoint registers to sane values
  113. * before debug exceptions are possibly reenabled
  114. * through local_dbg_restore.
  115. */
  116. if (hw_breakpoint_restore)
  117. hw_breakpoint_restore(NULL);
  118. }
  119. unpause_graph_tracing();
  120. /*
  121. * Restore pstate flags. OS lock and mdscr have been already
  122. * restored, so from this point onwards, debugging is fully
  123. * renabled if it was enabled when core started shutdown.
  124. */
  125. local_dbg_restore(flags);
  126. return ret;
  127. }
  128. struct sleep_save_sp sleep_save_sp;
  129. static int __init cpu_suspend_init(void)
  130. {
  131. void *ctx_ptr;
  132. /* ctx_ptr is an array of physical addresses */
  133. ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL);
  134. if (WARN_ON(!ctx_ptr))
  135. return -ENOMEM;
  136. sleep_save_sp.save_ptr_stash = ctx_ptr;
  137. sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
  138. __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
  139. return 0;
  140. }
  141. early_initcall(cpu_suspend_init);