suspend.c 8.9 KB

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