suspend.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /* Kick the lockup detector */
  156. lockup_detector_bootcpu_resume();
  157. Enable_cpus:
  158. enable_nonboot_cpus();
  159. Platform_wake:
  160. if (suspend_ops->wake)
  161. suspend_ops->wake();
  162. dpm_resume_start(PMSG_RESUME);
  163. Platform_finish:
  164. if (suspend_ops->finish)
  165. suspend_ops->finish();
  166. return error;
  167. }
  168. /**
  169. * suspend_devices_and_enter - Suspend devices and enter system sleep state.
  170. * @state: System sleep state to enter.
  171. */
  172. int suspend_devices_and_enter(suspend_state_t state)
  173. {
  174. int error;
  175. bool wakeup = false;
  176. if (!suspend_ops)
  177. return -ENOSYS;
  178. trace_machine_suspend(state);
  179. if (suspend_ops->begin) {
  180. error = suspend_ops->begin(state);
  181. if (error)
  182. goto Close;
  183. }
  184. suspend_console();
  185. ftrace_stop();
  186. suspend_test_start();
  187. error = dpm_suspend_start(PMSG_SUSPEND);
  188. if (error) {
  189. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  190. goto Recover_platform;
  191. }
  192. suspend_test_finish("suspend devices");
  193. if (suspend_test(TEST_DEVICES))
  194. goto Recover_platform;
  195. do {
  196. error = suspend_enter(state, &wakeup);
  197. } while (!error && !wakeup
  198. && suspend_ops->suspend_again && suspend_ops->suspend_again());
  199. Resume_devices:
  200. suspend_test_start();
  201. dpm_resume_end(PMSG_RESUME);
  202. suspend_test_finish("resume devices");
  203. ftrace_start();
  204. resume_console();
  205. Close:
  206. if (suspend_ops->end)
  207. suspend_ops->end();
  208. trace_machine_suspend(PWR_EVENT_EXIT);
  209. return error;
  210. Recover_platform:
  211. if (suspend_ops->recover)
  212. suspend_ops->recover();
  213. goto Resume_devices;
  214. }
  215. /**
  216. * suspend_finish - Clean up before finishing the suspend sequence.
  217. *
  218. * Call platform code to clean up, restart processes, and free the console that
  219. * we've allocated. This routine is not called for hibernation.
  220. */
  221. static void suspend_finish(void)
  222. {
  223. suspend_thaw_processes();
  224. pm_notifier_call_chain(PM_POST_SUSPEND);
  225. pm_restore_console();
  226. }
  227. /**
  228. * enter_state - Do common work needed to enter system sleep state.
  229. * @state: System sleep state to enter.
  230. *
  231. * Make sure that no one else is trying to put the system into a sleep state.
  232. * Fail if that's not the case. Otherwise, prepare for system suspend, make the
  233. * system enter the given sleep state and clean up after wakeup.
  234. */
  235. static int enter_state(suspend_state_t state)
  236. {
  237. int error;
  238. if (!valid_state(state))
  239. return -ENODEV;
  240. if (!mutex_trylock(&pm_mutex))
  241. return -EBUSY;
  242. printk(KERN_INFO "PM: Syncing filesystems ... ");
  243. sys_sync();
  244. printk("done.\n");
  245. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  246. error = suspend_prepare();
  247. if (error)
  248. goto Unlock;
  249. if (suspend_test(TEST_FREEZER))
  250. goto Finish;
  251. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  252. pm_restrict_gfp_mask();
  253. error = suspend_devices_and_enter(state);
  254. pm_restore_gfp_mask();
  255. Finish:
  256. pr_debug("PM: Finishing wakeup.\n");
  257. suspend_finish();
  258. Unlock:
  259. mutex_unlock(&pm_mutex);
  260. return error;
  261. }
  262. /**
  263. * pm_suspend - Externally visible function for suspending the system.
  264. * @state: System sleep state to enter.
  265. *
  266. * Check if the value of @state represents one of the supported states,
  267. * execute enter_state() and update system suspend statistics.
  268. */
  269. int pm_suspend(suspend_state_t state)
  270. {
  271. int error;
  272. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  273. return -EINVAL;
  274. error = enter_state(state);
  275. if (error) {
  276. suspend_stats.fail++;
  277. dpm_save_failed_errno(error);
  278. } else {
  279. suspend_stats.success++;
  280. }
  281. return error;
  282. }
  283. EXPORT_SYMBOL(pm_suspend);