suspend.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <linux/percpu.h>
  2. #include <linux/slab.h>
  3. #include <asm/cacheflush.h>
  4. #include <asm/cpu_ops.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. 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() - function to enter a low-power state
  52. * @arg: argument to pass to CPU suspend operations
  53. *
  54. * Return: 0 on success, -EOPNOTSUPP if CPU suspend hook not initialized, CPU
  55. * operations back-end error code otherwise.
  56. */
  57. int cpu_suspend(unsigned long arg)
  58. {
  59. int cpu = smp_processor_id();
  60. /*
  61. * If cpu_ops have not been registered or suspend
  62. * has not been initialized, cpu_suspend call fails early.
  63. */
  64. if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_suspend)
  65. return -EOPNOTSUPP;
  66. return cpu_ops[cpu]->cpu_suspend(arg);
  67. }
  68. /*
  69. * __cpu_suspend
  70. *
  71. * arg: argument to pass to the finisher function
  72. * fn: finisher function pointer
  73. *
  74. */
  75. int __cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
  76. {
  77. struct mm_struct *mm = current->active_mm;
  78. int ret;
  79. unsigned long flags;
  80. /*
  81. * From this point debug exceptions are disabled to prevent
  82. * updates to mdscr register (saved and restored along with
  83. * general purpose registers) from kernel debuggers.
  84. */
  85. local_dbg_save(flags);
  86. /*
  87. * mm context saved on the stack, it will be restored when
  88. * the cpu comes out of reset through the identity mapped
  89. * page tables, so that the thread address space is properly
  90. * set-up on function return.
  91. */
  92. ret = __cpu_suspend_enter(arg, fn);
  93. if (ret == 0) {
  94. /*
  95. * We are resuming from reset with TTBR0_EL1 set to the
  96. * idmap to enable the MMU; restore the active_mm mappings in
  97. * TTBR0_EL1 unless the active_mm == &init_mm, in which case
  98. * the thread entered __cpu_suspend with TTBR0_EL1 set to
  99. * reserved TTBR0 page tables and should be restored as such.
  100. */
  101. if (mm == &init_mm)
  102. cpu_set_reserved_ttbr0();
  103. else
  104. cpu_switch_mm(mm->pgd, mm);
  105. flush_tlb_all();
  106. /*
  107. * Restore per-cpu offset before any kernel
  108. * subsystem relying on it has a chance to run.
  109. */
  110. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  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. /*
  120. * Restore pstate flags. OS lock and mdscr have been already
  121. * restored, so from this point onwards, debugging is fully
  122. * renabled if it was enabled when core started shutdown.
  123. */
  124. local_dbg_restore(flags);
  125. return ret;
  126. }
  127. struct sleep_save_sp sleep_save_sp;
  128. phys_addr_t sleep_idmap_phys;
  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. sleep_idmap_phys = virt_to_phys(idmap_pg_dir);
  139. __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
  140. __flush_dcache_area(&sleep_idmap_phys, sizeof(sleep_idmap_phys));
  141. return 0;
  142. }
  143. early_initcall(cpu_suspend_init);