suspend.c 4.1 KB

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