suspend.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * kernel/power/suspend.c - Suspend to RAM and standby functionality.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/string.h>
  11. #include <linux/delay.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/console.h>
  15. #include <linux/cpu.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/gfp.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/list.h>
  21. #include <linux/mm.h>
  22. #include <linux/slab.h>
  23. #include <linux/suspend.h>
  24. #include <linux/syscore_ops.h>
  25. #include <trace/events/power.h>
  26. #include "power.h"
  27. const char *const pm_states[PM_SUSPEND_MAX] = {
  28. #ifdef CONFIG_EARLYSUSPEND
  29. [PM_SUSPEND_ON] = "on",
  30. #endif
  31. [PM_SUSPEND_STANDBY] = "standby",
  32. [PM_SUSPEND_MEM] = "mem",
  33. };
  34. static const struct platform_suspend_ops *suspend_ops;
  35. /**
  36. * suspend_set_ops - Set the global suspend method table.
  37. * @ops: Pointer to ops structure.
  38. */
  39. void suspend_set_ops(const struct platform_suspend_ops *ops)
  40. {
  41. mutex_lock(&pm_mutex);
  42. suspend_ops = ops;
  43. mutex_unlock(&pm_mutex);
  44. }
  45. bool valid_state(suspend_state_t state)
  46. {
  47. /*
  48. * All states need lowlevel support and need to be valid to the lowlevel
  49. * implementation, no valid callback implies that none are valid.
  50. */
  51. return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
  52. }
  53. /**
  54. * suspend_valid_only_mem - generic memory-only valid callback
  55. *
  56. * Platform drivers that implement mem suspend only and only need
  57. * to check for that in their .valid callback can use this instead
  58. * of rolling their own .valid callback.
  59. */
  60. int suspend_valid_only_mem(suspend_state_t state)
  61. {
  62. return state == PM_SUSPEND_MEM;
  63. }
  64. static int suspend_test(int level)
  65. {
  66. #ifdef CONFIG_PM_DEBUG
  67. if (pm_test_level == level) {
  68. printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
  69. mdelay(5000);
  70. return 1;
  71. }
  72. #endif /* !CONFIG_PM_DEBUG */
  73. return 0;
  74. }
  75. /**
  76. * suspend_prepare - Do prep work before entering low-power state.
  77. *
  78. * This is common code that is called for each state that we're entering.
  79. * Run suspend notifiers, allocate a console and stop all processes.
  80. */
  81. static int suspend_prepare(void)
  82. {
  83. int error;
  84. if (!suspend_ops || !suspend_ops->enter)
  85. return -EPERM;
  86. pm_prepare_console();
  87. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  88. if (error)
  89. goto Finish;
  90. error = usermodehelper_disable();
  91. if (error)
  92. goto Finish;
  93. error = suspend_freeze_processes();
  94. if (!error)
  95. return 0;
  96. suspend_thaw_processes();
  97. usermodehelper_enable();
  98. Finish:
  99. pm_notifier_call_chain(PM_POST_SUSPEND);
  100. pm_restore_console();
  101. return error;
  102. }
  103. /* default implementation */
  104. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  105. {
  106. local_irq_disable();
  107. }
  108. /* default implementation */
  109. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  110. {
  111. local_irq_enable();
  112. }
  113. /**
  114. * suspend_enter - enter the desired system sleep state.
  115. * @state: state to enter
  116. *
  117. * This function should be called after devices have been suspended.
  118. */
  119. static int suspend_enter(suspend_state_t state)
  120. {
  121. int error;
  122. if (suspend_ops->prepare) {
  123. error = suspend_ops->prepare();
  124. if (error)
  125. goto Platform_finish;
  126. }
  127. error = dpm_suspend_noirq(PMSG_SUSPEND);
  128. if (error) {
  129. printk(KERN_ERR "PM: Some devices failed to power down\n");
  130. goto Platform_finish;
  131. }
  132. if (suspend_ops->prepare_late) {
  133. error = suspend_ops->prepare_late();
  134. if (error)
  135. goto Platform_wake;
  136. }
  137. if (suspend_test(TEST_PLATFORM))
  138. goto Platform_wake;
  139. error = disable_nonboot_cpus();
  140. if (error || suspend_test(TEST_CPUS))
  141. goto Enable_cpus;
  142. arch_suspend_disable_irqs();
  143. BUG_ON(!irqs_disabled());
  144. error = syscore_suspend();
  145. if (!error) {
  146. if (!(suspend_test(TEST_CORE) || pm_wakeup_pending())) {
  147. error = suspend_ops->enter(state);
  148. events_check_enabled = false;
  149. }
  150. syscore_resume();
  151. }
  152. arch_suspend_enable_irqs();
  153. BUG_ON(irqs_disabled());
  154. Enable_cpus:
  155. enable_nonboot_cpus();
  156. Platform_wake:
  157. if (suspend_ops->wake)
  158. suspend_ops->wake();
  159. dpm_resume_noirq(PMSG_RESUME);
  160. Platform_finish:
  161. if (suspend_ops->finish)
  162. suspend_ops->finish();
  163. return error;
  164. }
  165. /**
  166. * suspend_devices_and_enter - suspend devices and enter the desired system
  167. * sleep state.
  168. * @state: state to enter
  169. */
  170. int suspend_devices_and_enter(suspend_state_t state)
  171. {
  172. int error;
  173. if (!suspend_ops)
  174. return -ENOSYS;
  175. trace_machine_suspend(state);
  176. if (suspend_ops->begin) {
  177. error = suspend_ops->begin(state);
  178. if (error)
  179. goto Close;
  180. }
  181. suspend_console();
  182. suspend_test_start();
  183. error = dpm_suspend_start(PMSG_SUSPEND);
  184. if (error) {
  185. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  186. goto Recover_platform;
  187. }
  188. suspend_test_finish("suspend devices");
  189. if (suspend_test(TEST_DEVICES))
  190. goto Recover_platform;
  191. error = suspend_enter(state);
  192. Resume_devices:
  193. suspend_test_start();
  194. dpm_resume_end(PMSG_RESUME);
  195. suspend_test_finish("resume devices");
  196. resume_console();
  197. Close:
  198. if (suspend_ops->end)
  199. suspend_ops->end();
  200. trace_machine_suspend(PWR_EVENT_EXIT);
  201. return error;
  202. Recover_platform:
  203. if (suspend_ops->recover)
  204. suspend_ops->recover();
  205. goto Resume_devices;
  206. }
  207. /**
  208. * suspend_finish - Do final work before exiting suspend sequence.
  209. *
  210. * Call platform code to clean up, restart processes, and free the
  211. * console that we've allocated. This is not called for suspend-to-disk.
  212. */
  213. static void suspend_finish(void)
  214. {
  215. suspend_thaw_processes();
  216. usermodehelper_enable();
  217. pm_notifier_call_chain(PM_POST_SUSPEND);
  218. pm_restore_console();
  219. }
  220. /**
  221. * enter_state - Do common work of entering low-power state.
  222. * @state: pm_state structure for state we're entering.
  223. *
  224. * Make sure we're the only ones trying to enter a sleep state. Fail
  225. * if someone has beat us to it, since we don't want anything weird to
  226. * happen when we wake up.
  227. * Then, do the setup for suspend, enter the state, and cleaup (after
  228. * we've woken up).
  229. */
  230. int enter_state(suspend_state_t state)
  231. {
  232. int error;
  233. if (!valid_state(state))
  234. return -ENODEV;
  235. if (!mutex_trylock(&pm_mutex))
  236. return -EBUSY;
  237. printk(KERN_INFO "PM: Syncing filesystems ... ");
  238. sys_sync();
  239. printk("done.\n");
  240. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  241. error = suspend_prepare();
  242. if (error)
  243. goto Unlock;
  244. if (suspend_test(TEST_FREEZER))
  245. goto Finish;
  246. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  247. pm_restrict_gfp_mask();
  248. error = suspend_devices_and_enter(state);
  249. pm_restore_gfp_mask();
  250. Finish:
  251. pr_debug("PM: Finishing wakeup.\n");
  252. suspend_finish();
  253. Unlock:
  254. mutex_unlock(&pm_mutex);
  255. return error;
  256. }
  257. /**
  258. * pm_suspend - Externally visible function for suspending system.
  259. * @state: Enumerated value of state to enter.
  260. *
  261. * Determine whether or not value is within range, get state
  262. * structure, and enter (above).
  263. */
  264. int pm_suspend(suspend_state_t state)
  265. {
  266. if (state > PM_SUSPEND_ON && state < PM_SUSPEND_MAX)
  267. return enter_state(state);
  268. return -EINVAL;
  269. }
  270. EXPORT_SYMBOL(pm_suspend);