suspend.c 7.4 KB

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