suspend.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/smp_plat.h>
  9. #include <asm/suspend.h>
  10. #include <asm/tlbflush.h>
  11. extern int __cpu_suspend(unsigned long);
  12. /*
  13. * This is called by __cpu_suspend() 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. * @arg: Argument to pass to suspend operations
  18. * @ptr: CPU context virtual address
  19. * @save_ptr: address of the location where the context physical address
  20. * must be saved
  21. */
  22. int __cpu_suspend_finisher(unsigned long arg, struct cpu_suspend_ctx *ptr,
  23. phys_addr_t *save_ptr)
  24. {
  25. int cpu = smp_processor_id();
  26. *save_ptr = virt_to_phys(ptr);
  27. cpu_do_suspend(ptr);
  28. /*
  29. * Only flush the context that must be retrieved with the MMU
  30. * off. VA primitives ensure the flush is applied to all
  31. * cache levels so context is pushed to DRAM.
  32. */
  33. __flush_dcache_area(ptr, sizeof(*ptr));
  34. __flush_dcache_area(save_ptr, sizeof(*save_ptr));
  35. return cpu_ops[cpu]->cpu_suspend(arg);
  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. 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. */
  57. int cpu_suspend(unsigned long arg)
  58. {
  59. struct mm_struct *mm = current->active_mm;
  60. int ret, cpu = smp_processor_id();
  61. unsigned long flags;
  62. /*
  63. * If cpu_ops have not been registered or suspend
  64. * has not been initialized, cpu_suspend call fails early.
  65. */
  66. if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_suspend)
  67. return -EOPNOTSUPP;
  68. /*
  69. * From this point debug exceptions are disabled to prevent
  70. * updates to mdscr register (saved and restored along with
  71. * general purpose registers) from kernel debuggers.
  72. */
  73. local_dbg_save(flags);
  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(arg);
  81. if (ret == 0) {
  82. cpu_switch_mm(mm->pgd, mm);
  83. flush_tlb_all();
  84. /*
  85. * Restore per-cpu offset before any kernel
  86. * subsystem relying on it has a chance to run.
  87. */
  88. set_my_cpu_offset(per_cpu_offset(cpu));
  89. /*
  90. * Restore HW breakpoint registers to sane values
  91. * before debug exceptions are possibly reenabled
  92. * through local_dbg_restore.
  93. */
  94. if (hw_breakpoint_restore)
  95. hw_breakpoint_restore(NULL);
  96. }
  97. /*
  98. * Restore pstate flags. OS lock and mdscr have been already
  99. * restored, so from this point onwards, debugging is fully
  100. * renabled if it was enabled when core started shutdown.
  101. */
  102. local_dbg_restore(flags);
  103. return ret;
  104. }
  105. extern struct sleep_save_sp sleep_save_sp;
  106. extern phys_addr_t sleep_idmap_phys;
  107. static int cpu_suspend_init(void)
  108. {
  109. void *ctx_ptr;
  110. /* ctx_ptr is an array of physical addresses */
  111. ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL);
  112. if (WARN_ON(!ctx_ptr))
  113. return -ENOMEM;
  114. sleep_save_sp.save_ptr_stash = ctx_ptr;
  115. sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
  116. sleep_idmap_phys = virt_to_phys(idmap_pg_dir);
  117. __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp));
  118. __flush_dcache_area(&sleep_idmap_phys, sizeof(sleep_idmap_phys));
  119. return 0;
  120. }
  121. early_initcall(cpu_suspend_init);