suspend.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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/cpuidle.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/gfp.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/list.h>
  22. #include <linux/mm.h>
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include <linux/suspend.h>
  26. #include <linux/syscore_ops.h>
  27. #include <linux/ftrace.h>
  28. #include <trace/events/power.h>
  29. #include <linux/compiler.h>
  30. #include "power.h"
  31. const char *const pm_states[PM_SUSPEND_MAX] = {
  32. [PM_SUSPEND_FREEZE] = "freeze",
  33. [PM_SUSPEND_STANDBY] = "standby",
  34. [PM_SUSPEND_MEM] = "mem",
  35. };
  36. static const struct platform_suspend_ops *suspend_ops;
  37. static bool need_suspend_ops(suspend_state_t state)
  38. {
  39. return state > PM_SUSPEND_FREEZE;
  40. }
  41. static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
  42. static bool suspend_freeze_wake;
  43. static void freeze_begin(void)
  44. {
  45. suspend_freeze_wake = false;
  46. }
  47. static void freeze_enter(void)
  48. {
  49. cpuidle_use_deepest_state(true);
  50. cpuidle_resume();
  51. wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
  52. cpuidle_pause();
  53. cpuidle_use_deepest_state(false);
  54. }
  55. void freeze_wake(void)
  56. {
  57. suspend_freeze_wake = true;
  58. wake_up(&suspend_freeze_wait_head);
  59. }
  60. EXPORT_SYMBOL_GPL(freeze_wake);
  61. /**
  62. * suspend_set_ops - Set the global suspend method table.
  63. * @ops: Suspend operations to use.
  64. */
  65. void suspend_set_ops(const struct platform_suspend_ops *ops)
  66. {
  67. lock_system_sleep();
  68. suspend_ops = ops;
  69. unlock_system_sleep();
  70. }
  71. EXPORT_SYMBOL_GPL(suspend_set_ops);
  72. bool valid_state(suspend_state_t state)
  73. {
  74. if (state == PM_SUSPEND_FREEZE) {
  75. #ifdef CONFIG_PM_DEBUG
  76. if (pm_test_level != TEST_NONE &&
  77. pm_test_level != TEST_FREEZER &&
  78. pm_test_level != TEST_DEVICES &&
  79. pm_test_level != TEST_PLATFORM) {
  80. printk(KERN_WARNING "Unsupported pm_test mode for "
  81. "freeze state, please choose "
  82. "none/freezer/devices/platform.\n");
  83. return false;
  84. }
  85. #endif
  86. return true;
  87. }
  88. /*
  89. * PM_SUSPEND_STANDBY and PM_SUSPEND_MEMORY states need lowlevel
  90. * support and need to be valid to the lowlevel
  91. * implementation, no valid callback implies that none are valid.
  92. */
  93. return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
  94. }
  95. /**
  96. * suspend_valid_only_mem - Generic memory-only valid callback.
  97. *
  98. * Platform drivers that implement mem suspend only and only need to check for
  99. * that in their .valid() callback can use this instead of rolling their own
  100. * .valid() callback.
  101. */
  102. int suspend_valid_only_mem(suspend_state_t state)
  103. {
  104. return state == PM_SUSPEND_MEM;
  105. }
  106. EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
  107. static int suspend_test(int level)
  108. {
  109. #ifdef CONFIG_PM_DEBUG
  110. if (pm_test_level == level) {
  111. printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
  112. mdelay(5000);
  113. return 1;
  114. }
  115. #endif /* !CONFIG_PM_DEBUG */
  116. return 0;
  117. }
  118. /**
  119. * suspend_prepare - Prepare for entering system sleep state.
  120. *
  121. * Common code run for every system sleep state that can be entered (except for
  122. * hibernation). Run suspend notifiers, allocate the "suspend" console and
  123. * freeze processes.
  124. */
  125. static int suspend_prepare(suspend_state_t state)
  126. {
  127. int error;
  128. if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter))
  129. return -EPERM;
  130. pm_prepare_console();
  131. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  132. if (error)
  133. goto Finish;
  134. error = suspend_freeze_processes();
  135. if (!error)
  136. return 0;
  137. suspend_stats.failed_freeze++;
  138. dpm_save_failed_step(SUSPEND_FREEZE);
  139. Finish:
  140. pm_notifier_call_chain(PM_POST_SUSPEND);
  141. pm_restore_console();
  142. return error;
  143. }
  144. /* default implementation */
  145. void __weak arch_suspend_disable_irqs(void)
  146. {
  147. local_irq_disable();
  148. }
  149. /* default implementation */
  150. void __weak arch_suspend_enable_irqs(void)
  151. {
  152. local_irq_enable();
  153. }
  154. /**
  155. * suspend_enter - Make the system enter the given sleep state.
  156. * @state: System sleep state to enter.
  157. * @wakeup: Returns information that the sleep state should not be re-entered.
  158. *
  159. * This function should be called after devices have been suspended.
  160. */
  161. static int suspend_enter(suspend_state_t state, bool *wakeup)
  162. {
  163. int error;
  164. if (need_suspend_ops(state) && suspend_ops->prepare) {
  165. error = suspend_ops->prepare();
  166. if (error)
  167. goto Platform_finish;
  168. }
  169. error = dpm_suspend_end(PMSG_SUSPEND);
  170. if (error) {
  171. printk(KERN_ERR "PM: Some devices failed to power down\n");
  172. goto Platform_finish;
  173. }
  174. if (need_suspend_ops(state) && suspend_ops->prepare_late) {
  175. error = suspend_ops->prepare_late();
  176. if (error)
  177. goto Platform_wake;
  178. }
  179. if (suspend_test(TEST_PLATFORM))
  180. goto Platform_wake;
  181. /*
  182. * PM_SUSPEND_FREEZE equals
  183. * frozen processes + suspended devices + idle processors.
  184. * Thus we should invoke freeze_enter() soon after
  185. * all the devices are suspended.
  186. */
  187. if (state == PM_SUSPEND_FREEZE) {
  188. freeze_enter();
  189. goto Platform_wake;
  190. }
  191. ftrace_stop();
  192. error = disable_nonboot_cpus();
  193. if (error || suspend_test(TEST_CPUS))
  194. goto Enable_cpus;
  195. arch_suspend_disable_irqs();
  196. BUG_ON(!irqs_disabled());
  197. error = syscore_suspend();
  198. if (!error) {
  199. *wakeup = pm_wakeup_pending();
  200. if (!(suspend_test(TEST_CORE) || *wakeup)) {
  201. error = suspend_ops->enter(state);
  202. events_check_enabled = false;
  203. }
  204. syscore_resume();
  205. }
  206. arch_suspend_enable_irqs();
  207. BUG_ON(irqs_disabled());
  208. Enable_cpus:
  209. enable_nonboot_cpus();
  210. ftrace_start();
  211. Platform_wake:
  212. if (need_suspend_ops(state) && suspend_ops->wake)
  213. suspend_ops->wake();
  214. dpm_resume_start(PMSG_RESUME);
  215. Platform_finish:
  216. if (need_suspend_ops(state) && suspend_ops->finish)
  217. suspend_ops->finish();
  218. return error;
  219. }
  220. /**
  221. * suspend_devices_and_enter - Suspend devices and enter system sleep state.
  222. * @state: System sleep state to enter.
  223. */
  224. int suspend_devices_and_enter(suspend_state_t state)
  225. {
  226. int error;
  227. bool wakeup = false;
  228. if (need_suspend_ops(state) && !suspend_ops)
  229. return -ENOSYS;
  230. trace_machine_suspend(state);
  231. if (need_suspend_ops(state) && suspend_ops->begin) {
  232. error = suspend_ops->begin(state);
  233. if (error)
  234. goto Close;
  235. }
  236. suspend_console();
  237. suspend_test_start();
  238. error = dpm_suspend_start(PMSG_SUSPEND);
  239. if (error) {
  240. pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
  241. goto Recover_platform;
  242. }
  243. suspend_test_finish("suspend devices");
  244. if (suspend_test(TEST_DEVICES))
  245. goto Recover_platform;
  246. do {
  247. error = suspend_enter(state, &wakeup);
  248. } while (!error && !wakeup && need_suspend_ops(state)
  249. && suspend_ops->suspend_again && suspend_ops->suspend_again());
  250. Resume_devices:
  251. suspend_test_start();
  252. dpm_resume_end(PMSG_RESUME);
  253. suspend_test_finish("resume devices");
  254. resume_console();
  255. Close:
  256. if (need_suspend_ops(state) && suspend_ops->end)
  257. suspend_ops->end();
  258. trace_machine_suspend(PWR_EVENT_EXIT);
  259. return error;
  260. Recover_platform:
  261. if (need_suspend_ops(state) && suspend_ops->recover)
  262. suspend_ops->recover();
  263. goto Resume_devices;
  264. }
  265. /**
  266. * suspend_finish - Clean up before finishing the suspend sequence.
  267. *
  268. * Call platform code to clean up, restart processes, and free the console that
  269. * we've allocated. This routine is not called for hibernation.
  270. */
  271. static void suspend_finish(void)
  272. {
  273. suspend_thaw_processes();
  274. pm_notifier_call_chain(PM_POST_SUSPEND);
  275. pm_restore_console();
  276. }
  277. /**
  278. * enter_state - Do common work needed to enter system sleep state.
  279. * @state: System sleep state to enter.
  280. *
  281. * Make sure that no one else is trying to put the system into a sleep state.
  282. * Fail if that's not the case. Otherwise, prepare for system suspend, make the
  283. * system enter the given sleep state and clean up after wakeup.
  284. */
  285. static int enter_state(suspend_state_t state)
  286. {
  287. int error;
  288. if (!valid_state(state))
  289. return -ENODEV;
  290. if (!mutex_trylock(&pm_mutex))
  291. return -EBUSY;
  292. if (state == PM_SUSPEND_FREEZE)
  293. freeze_begin();
  294. printk(KERN_INFO "PM: Syncing filesystems ... ");
  295. sys_sync();
  296. printk("done.\n");
  297. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  298. error = suspend_prepare(state);
  299. if (error)
  300. goto Unlock;
  301. if (suspend_test(TEST_FREEZER))
  302. goto Finish;
  303. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  304. pm_restrict_gfp_mask();
  305. error = suspend_devices_and_enter(state);
  306. pm_restore_gfp_mask();
  307. Finish:
  308. pr_debug("PM: Finishing wakeup.\n");
  309. suspend_finish();
  310. Unlock:
  311. mutex_unlock(&pm_mutex);
  312. return error;
  313. }
  314. /**
  315. * pm_suspend - Externally visible function for suspending the system.
  316. * @state: System sleep state to enter.
  317. *
  318. * Check if the value of @state represents one of the supported states,
  319. * execute enter_state() and update system suspend statistics.
  320. */
  321. int pm_suspend(suspend_state_t state)
  322. {
  323. int error;
  324. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  325. return -EINVAL;
  326. error = enter_state(state);
  327. if (error) {
  328. suspend_stats.fail++;
  329. dpm_save_failed_errno(error);
  330. } else {
  331. suspend_stats.success++;
  332. }
  333. return error;
  334. }
  335. EXPORT_SYMBOL(pm_suspend);