suspend.c 8.5 KB

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