suspend.c 8.9 KB

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