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: Pointer to ops structure.
  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
  57. * to check for that in their .valid callback can use this instead
  58. * of rolling their own .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 - Do prep work before entering low-power state.
  78. *
  79. * This is common code that is called for each state that we're entering.
  80. * Run suspend notifiers, allocate a console and stop all processes.
  81. */
  82. static int suspend_prepare(void)
  83. {
  84. int error;
  85. if (!suspend_ops || !suspend_ops->enter)
  86. return -EPERM;
  87. pm_prepare_console();
  88. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  89. if (error)
  90. goto Finish;
  91. error = usermodehelper_disable();
  92. if (error)
  93. goto Finish;
  94. error = suspend_freeze_processes();
  95. if (!error)
  96. return 0;
  97. suspend_stats.failed_freeze++;
  98. dpm_save_failed_step(SUSPEND_FREEZE);
  99. usermodehelper_enable();
  100. Finish:
  101. pm_notifier_call_chain(PM_POST_SUSPEND);
  102. pm_restore_console();
  103. return error;
  104. }
  105. /* default implementation */
  106. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  107. {
  108. local_irq_disable();
  109. }
  110. /* default implementation */
  111. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  112. {
  113. local_irq_enable();
  114. }
  115. /**
  116. * suspend_enter - enter the desired system sleep state.
  117. * @state: State to enter
  118. * @wakeup: Returns information that suspend should not be entered again.
  119. *
  120. * This function should be called after devices have been suspended.
  121. */
  122. static int suspend_enter(suspend_state_t state, bool *wakeup)
  123. {
  124. int error;
  125. if (suspend_ops->prepare) {
  126. error = suspend_ops->prepare();
  127. if (error)
  128. goto Platform_finish;
  129. }
  130. error = dpm_suspend_noirq(PMSG_SUSPEND);
  131. if (error) {
  132. printk(KERN_ERR "PM: Some devices failed to power down\n");
  133. goto Platform_finish;
  134. }
  135. if (suspend_ops->prepare_late) {
  136. error = suspend_ops->prepare_late();
  137. if (error)
  138. goto Platform_wake;
  139. }
  140. if (suspend_test(TEST_PLATFORM))
  141. goto Platform_wake;
  142. error = disable_nonboot_cpus();
  143. if (error || suspend_test(TEST_CPUS))
  144. goto Enable_cpus;
  145. arch_suspend_disable_irqs();
  146. BUG_ON(!irqs_disabled());
  147. error = syscore_suspend();
  148. if (!error) {
  149. *wakeup = pm_wakeup_pending();
  150. if (!(suspend_test(TEST_CORE) || *wakeup)) {
  151. error = suspend_ops->enter(state);
  152. events_check_enabled = false;
  153. }
  154. syscore_resume();
  155. }
  156. arch_suspend_enable_irqs();
  157. BUG_ON(irqs_disabled());
  158. Enable_cpus:
  159. enable_nonboot_cpus();
  160. Platform_wake:
  161. if (suspend_ops->wake)
  162. suspend_ops->wake();
  163. dpm_resume_noirq(PMSG_RESUME);
  164. Platform_finish:
  165. if (suspend_ops->finish)
  166. suspend_ops->finish();
  167. return error;
  168. }
  169. /**
  170. * suspend_devices_and_enter - suspend devices and enter the desired system
  171. * sleep state.
  172. * @state: 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 - Do final work before exiting suspend sequence.
  217. *
  218. * Call platform code to clean up, restart processes, and free the
  219. * console that we've allocated. This is not called for suspend-to-disk.
  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 of entering low-power state.
  230. * @state: pm_state structure for state we're entering.
  231. *
  232. * Make sure we're the only ones trying to enter a sleep state. Fail
  233. * if someone has beat us to it, since we don't want anything weird to
  234. * happen when we wake up.
  235. * Then, do the setup for suspend, enter the state, and cleaup (after
  236. * we've woken up).
  237. */
  238. int enter_state(suspend_state_t state)
  239. {
  240. int error;
  241. if (!valid_state(state))
  242. return -ENODEV;
  243. if (!mutex_trylock(&pm_mutex))
  244. return -EBUSY;
  245. printk(KERN_INFO "PM: Syncing filesystems ... ");
  246. sys_sync();
  247. printk("done.\n");
  248. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  249. error = suspend_prepare();
  250. if (error)
  251. goto Unlock;
  252. if (suspend_test(TEST_FREEZER))
  253. goto Finish;
  254. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  255. pm_restrict_gfp_mask();
  256. error = suspend_devices_and_enter(state);
  257. pm_restore_gfp_mask();
  258. Finish:
  259. pr_debug("PM: Finishing wakeup.\n");
  260. suspend_finish();
  261. Unlock:
  262. mutex_unlock(&pm_mutex);
  263. return error;
  264. }
  265. /**
  266. * pm_suspend - Externally visible function for suspending system.
  267. * @state: Enumerated value of state to enter.
  268. *
  269. * Determine whether or not value is within range, get state
  270. * structure, and enter (above).
  271. */
  272. int pm_suspend(suspend_state_t state)
  273. {
  274. int ret;
  275. if (state > PM_SUSPEND_ON && state < PM_SUSPEND_MAX) {
  276. ret = enter_state(state);
  277. if (ret) {
  278. suspend_stats.fail++;
  279. dpm_save_failed_errno(ret);
  280. } else
  281. suspend_stats.success++;
  282. return ret;
  283. }
  284. return -EINVAL;
  285. }
  286. EXPORT_SYMBOL(pm_suspend);