hibernate.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Hibernation support specific for ARM
  3. *
  4. * Derived from work on ARM hibernation support by:
  5. *
  6. * Ubuntu project, hibernation support for mach-dove
  7. * Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu)
  8. * Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.)
  9. * https://lkml.org/lkml/2010/6/18/4
  10. * https://lists.linux-foundation.org/pipermail/linux-pm/2010-June/027422.html
  11. * https://patchwork.kernel.org/patch/96442/
  12. *
  13. * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
  14. *
  15. * License terms: GNU General Public License (GPL) version 2
  16. */
  17. #include <linux/mm.h>
  18. #include <linux/suspend.h>
  19. #include <asm/system_misc.h>
  20. #include <asm/idmap.h>
  21. #include <asm/suspend.h>
  22. #include <asm/memory.h>
  23. #include <asm/sections.h>
  24. #include <mtk_hibernate_core.h>
  25. #include "reboot.h"
  26. #ifdef CONFIG_MTK_HIBERNATION
  27. static int swsusp_saved;
  28. #endif
  29. int pfn_is_nosave(unsigned long pfn)
  30. {
  31. unsigned long nosave_begin_pfn = virt_to_pfn(&__nosave_begin);
  32. unsigned long nosave_end_pfn = virt_to_pfn(&__nosave_end - 1);
  33. return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);
  34. }
  35. void notrace save_processor_state(void)
  36. {
  37. WARN_ON(num_online_cpus() != 1);
  38. local_fiq_disable();
  39. #ifdef CONFIG_MTK_HIBERNATION
  40. mtk_save_processor_state();
  41. #endif
  42. }
  43. void notrace restore_processor_state(void)
  44. {
  45. local_fiq_enable();
  46. #ifdef CONFIG_MTK_HIBERNATION
  47. mtk_restore_processor_state();
  48. #endif
  49. }
  50. /*
  51. * Snapshot kernel memory and reset the system.
  52. *
  53. * swsusp_save() is executed in the suspend finisher so that the CPU
  54. * context pointer and memory are part of the saved image, which is
  55. * required by the resume kernel image to restart execution from
  56. * swsusp_arch_suspend().
  57. *
  58. * soft_restart is not technically needed, but is used to get success
  59. * returned from cpu_suspend.
  60. *
  61. * When soft reboot completes, the hibernation snapshot is written out.
  62. */
  63. static int notrace arch_save_image(unsigned long unused)
  64. {
  65. int ret;
  66. ret = swsusp_save();
  67. #ifdef CONFIG_MTK_HIBERNATION
  68. swsusp_saved = (ret == 0) ? 1 : 0;
  69. #else
  70. if (ret == 0)
  71. _soft_restart(virt_to_phys(cpu_resume), false);
  72. #endif
  73. return ret;
  74. }
  75. /*
  76. * Save the current CPU state before suspend / poweroff.
  77. */
  78. int notrace swsusp_arch_suspend(void)
  79. {
  80. #ifdef CONFIG_MTK_HIBERNATION
  81. int retval = 0;
  82. retval = cpu_suspend(0, arch_save_image);
  83. if (swsusp_saved)
  84. retval = 0;
  85. return retval;
  86. #else
  87. return cpu_suspend(0, arch_save_image);
  88. #endif
  89. }
  90. /*
  91. * Restore page contents for physical pages that were in use during loading
  92. * hibernation image. Switch to idmap_pgd so the physical page tables
  93. * are overwritten with the same contents.
  94. */
  95. static void notrace arch_restore_image(void *unused)
  96. {
  97. #ifdef CONFIG_MTK_HIBERNATION
  98. mtk_arch_restore_image();
  99. #else
  100. struct pbe *pbe;
  101. cpu_switch_mm(idmap_pgd, &init_mm);
  102. for (pbe = restore_pblist; pbe; pbe = pbe->next)
  103. copy_page(pbe->orig_address, pbe->address);
  104. _soft_restart(virt_to_phys(cpu_resume), false);
  105. #endif
  106. }
  107. static u64 resume_stack[PAGE_SIZE/2/sizeof(u64)] __nosavedata;
  108. /*
  109. * Resume from the hibernation image.
  110. * Due to the kernel heap / data restore, stack contents change underneath
  111. * and that would make function calls impossible; switch to a temporary
  112. * stack within the nosave region to avoid that problem.
  113. */
  114. int swsusp_arch_resume(void)
  115. {
  116. #ifdef CONFIG_MTK_HIBERNATION
  117. cpu_init(); /* get a clean PSR */
  118. #endif
  119. call_with_stack(arch_restore_image, 0,
  120. resume_stack + ARRAY_SIZE(resume_stack));
  121. return 0;
  122. }