suspend.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ftrace.h>
  3. #include <linux/init.h>
  4. #include <linux/slab.h>
  5. #include <linux/mm_types.h>
  6. #include <asm/bugs.h>
  7. #include <asm/cacheflush.h>
  8. #include <asm/idmap.h>
  9. #include <asm/pgalloc.h>
  10. #include <asm/pgtable.h>
  11. #include <asm/memory.h>
  12. #include <asm/smp_plat.h>
  13. #include <asm/suspend.h>
  14. #include <asm/tlbflush.h>
  15. extern int __cpu_suspend(unsigned long, int (*)(unsigned long), u32 cpuid);
  16. extern void cpu_resume_mmu(void);
  17. #ifdef CONFIG_MMU
  18. int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
  19. {
  20. struct mm_struct *mm = current->active_mm;
  21. u32 __mpidr = cpu_logical_map(smp_processor_id());
  22. int ret;
  23. if (!idmap_pgd)
  24. return -EINVAL;
  25. /*
  26. * Function graph tracer state gets incosistent when the kernel
  27. * calls functions that never return (aka suspend finishers) hence
  28. * disable graph tracing during their execution.
  29. */
  30. pause_graph_tracing();
  31. /*
  32. * Provide a temporary page table with an identity mapping for
  33. * the MMU-enable code, required for resuming. On successful
  34. * resume (indicated by a zero return code), we need to switch
  35. * back to the correct page tables.
  36. */
  37. ret = __cpu_suspend(arg, fn, __mpidr);
  38. unpause_graph_tracing();
  39. if (ret == 0) {
  40. cpu_switch_mm(mm->pgd, mm);
  41. local_flush_bp_all();
  42. local_flush_tlb_all();
  43. check_other_bugs();
  44. }
  45. return ret;
  46. }
  47. #else
  48. int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
  49. {
  50. u32 __mpidr = cpu_logical_map(smp_processor_id());
  51. int ret;
  52. pause_graph_tracing();
  53. ret = __cpu_suspend(arg, fn, __mpidr);
  54. unpause_graph_tracing();
  55. return ret;
  56. }
  57. #define idmap_pgd NULL
  58. #endif
  59. /*
  60. * This is called by __cpu_suspend() to save the state, and do whatever
  61. * flushing is required to ensure that when the CPU goes to sleep we have
  62. * the necessary data available when the caches are not searched.
  63. */
  64. void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr)
  65. {
  66. u32 *ctx = ptr;
  67. *save_ptr = virt_to_phys(ptr);
  68. /* This must correspond to the LDM in cpu_resume() assembly */
  69. *ptr++ = virt_to_phys(idmap_pgd);
  70. *ptr++ = sp;
  71. *ptr++ = virt_to_phys(cpu_do_resume);
  72. cpu_do_suspend(ptr);
  73. flush_cache_louis();
  74. /*
  75. * flush_cache_louis does not guarantee that
  76. * save_ptr and ptr are cleaned to main memory,
  77. * just up to the Level of Unification Inner Shareable.
  78. * Since the context pointer and context itself
  79. * are to be retrieved with the MMU off that
  80. * data must be cleaned from all cache levels
  81. * to main memory using "area" cache primitives.
  82. */
  83. __cpuc_flush_dcache_area(ctx, ptrsz);
  84. __cpuc_flush_dcache_area(save_ptr, sizeof(*save_ptr));
  85. outer_clean_range(*save_ptr, *save_ptr + ptrsz);
  86. outer_clean_range(virt_to_phys(save_ptr),
  87. virt_to_phys(save_ptr) + sizeof(*save_ptr));
  88. }
  89. extern struct sleep_save_sp sleep_save_sp;
  90. static int cpu_suspend_alloc_sp(void)
  91. {
  92. void *ctx_ptr;
  93. /* ctx_ptr is an array of physical addresses */
  94. ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(u32), GFP_KERNEL);
  95. if (WARN_ON(!ctx_ptr))
  96. return -ENOMEM;
  97. sleep_save_sp.save_ptr_stash = ctx_ptr;
  98. sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
  99. sync_cache_w(&sleep_save_sp);
  100. return 0;
  101. }
  102. early_initcall(cpu_suspend_alloc_sp);