suspend.c 8.9 KB

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