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