suspend.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/export.h>
  24. #include <linux/suspend.h>
  25. #include <linux/syscore_ops.h>
  26. #include <linux/ftrace.h>
  27. #include <trace/events/power.h>
  28. #include "power.h"
  29. const char *const pm_states[PM_SUSPEND_MAX] = {
  30. [PM_SUSPEND_STANDBY] = "standby",
  31. [PM_SUSPEND_MEM] = "mem",
  32. };
  33. static const struct platform_suspend_ops *suspend_ops;
  34. /**
  35. * suspend_set_ops - Set the global suspend method table.
  36. * @ops: Suspend operations to use.
  37. */
  38. void suspend_set_ops(const struct platform_suspend_ops *ops)
  39. {
  40. lock_system_sleep();
  41. suspend_ops = ops;
  42. unlock_system_sleep();
  43. }
  44. EXPORT_SYMBOL_GPL(suspend_set_ops);
  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 to check for
  57. * that in their .valid() callback can use this instead of rolling their own
  58. * .valid() callback.
  59. */
  60. int suspend_valid_only_mem(suspend_state_t state)
  61. {
  62. return state == PM_SUSPEND_MEM;
  63. }
  64. EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
  65. static int suspend_test(int level)
  66. {
  67. #ifdef CONFIG_PM_DEBUG
  68. if (pm_test_level == level) {
  69. printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
  70. mdelay(5000);
  71. return 1;
  72. }
  73. #endif /* !CONFIG_PM_DEBUG */
  74. return 0;
  75. }
  76. /**
  77. * suspend_prepare - Prepare for entering system sleep state.
  78. *
  79. * Common code run for every system sleep state that can be entered (except for
  80. * hibernation). Run suspend notifiers, allocate the "suspend" console and
  81. * freeze processes.
  82. */
  83. static int suspend_prepare(void)
  84. {
  85. int error;
  86. if (!suspend_ops || !suspend_ops->enter)
  87. return -EPERM;
  88. pm_prepare_console();
  89. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  90. if (error)
  91. goto Finish;
  92. error = suspend_freeze_processes();
  93. if (!error)
  94. return 0;
  95. suspend_stats.failed_freeze++;
  96. dpm_save_failed_step(SUSPEND_FREEZE);
  97. Finish:
  98. pm_notifier_call_chain(PM_POST_SUSPEND);
  99. pm_restore_console();
  100. return error;
  101. }
  102. /* default implementation */
  103. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  104. {
  105. local_irq_disable();
  106. }
  107. /* default implementation */
  108. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  109. {
  110. local_irq_enable();
  111. }
  112. /**
  113. * suspend_enter - Make the system enter the given sleep state.
  114. * @state: System sleep state to enter.
  115. * @wakeup: Returns information that the sleep state should not be re-entered.
  116. *
  117. * This function should be called after devices have been suspended.
  118. */
  119. static int suspend_enter(suspend_state_t state, bool *wakeup)
  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_end(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. *wakeup = pm_wakeup_pending();
  147. if (!(suspend_test(TEST_CORE) || *wakeup)) {
  148. error = suspend_ops->enter(state);
  149. events_check_enabled = false;
  150. }
  151. syscore_resume();
  152. }
  153. arch_suspend_enable_irqs();
  154. BUG_ON(irqs_disabled());
  155. Enable_cpus:
  156. enable_nonboot_cpus();
  157. Platform_wake:
  158. if (suspend_ops->wake)
  159. suspend_ops->wake();
  160. dpm_resume_start(PMSG_RESUME);
  161. Platform_finish:
  162. if (suspend_ops->finish)
  163. suspend_ops->finish();
  164. return error;
  165. }
  166. /**
  167. * suspend_devices_and_enter - Suspend devices and enter system sleep state.
  168. * @state: System sleep state to enter.
  169. */
  170. int suspend_devices_and_enter(suspend_state_t state)
  171. {
  172. int error;
  173. bool wakeup = false;
  174. if (!suspend_ops)
  175. return -ENOSYS;
  176. trace_machine_suspend(state);
  177. if (suspend_ops->begin) {
  178. error = suspend_ops->begin(state);
  179. if (error)
  180. goto Close;
  181. }
  182. suspend_console();
  183. ftrace_stop();
  184. suspend_test_start();
  185. error = dpm_suspend_start(PMSG_SUSPEND);
  186. if (error) {
  187. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  188. goto Recover_platform;
  189. }
  190. suspend_test_finish("suspend devices");
  191. if (suspend_test(TEST_DEVICES))
  192. goto Recover_platform;
  193. do {
  194. error = suspend_enter(state, &wakeup);
  195. } while (!error && !wakeup
  196. && suspend_ops->suspend_again && suspend_ops->suspend_again());
  197. Resume_devices:
  198. suspend_test_start();
  199. dpm_resume_end(PMSG_RESUME);
  200. suspend_test_finish("resume devices");
  201. ftrace_start();
  202. resume_console();
  203. Close:
  204. if (suspend_ops->end)
  205. suspend_ops->end();
  206. trace_machine_suspend(PWR_EVENT_EXIT);
  207. return error;
  208. Recover_platform:
  209. if (suspend_ops->recover)
  210. suspend_ops->recover();
  211. goto Resume_devices;
  212. }
  213. /**
  214. * suspend_finish - Clean up before finishing the suspend sequence.
  215. *
  216. * Call platform code to clean up, restart processes, and free the console that
  217. * we've allocated. This routine is not called for hibernation.
  218. */
  219. static void suspend_finish(void)
  220. {
  221. suspend_thaw_processes();
  222. pm_notifier_call_chain(PM_POST_SUSPEND);
  223. pm_restore_console();
  224. }
  225. /**
  226. * enter_state - Do common work needed to enter system sleep state.
  227. * @state: System sleep state to enter.
  228. *
  229. * Make sure that no one else is trying to put the system into a sleep state.
  230. * Fail if that's not the case. Otherwise, prepare for system suspend, make the
  231. * system enter the given sleep state and clean up after wakeup.
  232. */
  233. static int enter_state(suspend_state_t state)
  234. {
  235. int error;
  236. if (!valid_state(state))
  237. return -ENODEV;
  238. if (!mutex_trylock(&pm_mutex))
  239. return -EBUSY;
  240. printk(KERN_INFO "PM: Syncing filesystems ... ");
  241. sys_sync();
  242. printk("done.\n");
  243. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  244. error = suspend_prepare();
  245. if (error)
  246. goto Unlock;
  247. if (suspend_test(TEST_FREEZER))
  248. goto Finish;
  249. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  250. pm_restrict_gfp_mask();
  251. error = suspend_devices_and_enter(state);
  252. pm_restore_gfp_mask();
  253. Finish:
  254. pr_debug("PM: Finishing wakeup.\n");
  255. suspend_finish();
  256. Unlock:
  257. mutex_unlock(&pm_mutex);
  258. return error;
  259. }
  260. /**
  261. * pm_suspend - Externally visible function for suspending the system.
  262. * @state: System sleep state to enter.
  263. *
  264. * Check if the value of @state represents one of the supported states,
  265. * execute enter_state() and update system suspend statistics.
  266. */
  267. int pm_suspend(suspend_state_t state)
  268. {
  269. int error;
  270. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  271. return -EINVAL;
  272. error = enter_state(state);
  273. if (error) {
  274. suspend_stats.fail++;
  275. dpm_save_failed_errno(error);
  276. } else {
  277. suspend_stats.success++;
  278. }
  279. return error;
  280. }
  281. EXPORT_SYMBOL(pm_suspend);