suspend.c 3.3 KB

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