suspend.c 4.4 KB

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