suspend.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/suspend.h>
  24. #include "power.h"
  25. const char *const pm_states[PM_SUSPEND_MAX] = {
  26. [PM_SUSPEND_STANDBY] = "standby",
  27. [PM_SUSPEND_MEM] = "mem",
  28. };
  29. static struct platform_suspend_ops *suspend_ops;
  30. /**
  31. * suspend_set_ops - Set the global suspend method table.
  32. * @ops: Pointer to ops structure.
  33. */
  34. void suspend_set_ops(struct platform_suspend_ops *ops)
  35. {
  36. mutex_lock(&pm_mutex);
  37. suspend_ops = ops;
  38. mutex_unlock(&pm_mutex);
  39. }
  40. bool valid_state(suspend_state_t state)
  41. {
  42. /*
  43. * All states need lowlevel support and need to be valid to the lowlevel
  44. * implementation, no valid callback implies that none are valid.
  45. */
  46. return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
  47. }
  48. /**
  49. * suspend_valid_only_mem - generic memory-only valid callback
  50. *
  51. * Platform drivers that implement mem suspend only and only need
  52. * to check for that in their .valid callback can use this instead
  53. * of rolling their own .valid callback.
  54. */
  55. int suspend_valid_only_mem(suspend_state_t state)
  56. {
  57. return state == PM_SUSPEND_MEM;
  58. }
  59. static int suspend_test(int level)
  60. {
  61. #ifdef CONFIG_PM_DEBUG
  62. if (pm_test_level == level) {
  63. printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
  64. mdelay(5000);
  65. return 1;
  66. }
  67. #endif /* !CONFIG_PM_DEBUG */
  68. return 0;
  69. }
  70. /**
  71. * suspend_prepare - Do prep work before entering low-power state.
  72. *
  73. * This is common code that is called for each state that we're entering.
  74. * Run suspend notifiers, allocate a console and stop all processes.
  75. */
  76. static int suspend_prepare(void)
  77. {
  78. int error;
  79. if (!suspend_ops || !suspend_ops->enter)
  80. return -EPERM;
  81. pm_prepare_console();
  82. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  83. if (error)
  84. goto Finish;
  85. error = usermodehelper_disable();
  86. if (error)
  87. goto Finish;
  88. error = suspend_freeze_processes();
  89. if (!error)
  90. return 0;
  91. suspend_thaw_processes();
  92. usermodehelper_enable();
  93. Finish:
  94. pm_notifier_call_chain(PM_POST_SUSPEND);
  95. pm_restore_console();
  96. return error;
  97. }
  98. /* default implementation */
  99. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  100. {
  101. local_irq_disable();
  102. }
  103. /* default implementation */
  104. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  105. {
  106. local_irq_enable();
  107. }
  108. /**
  109. * suspend_enter - enter the desired system sleep state.
  110. * @state: state to enter
  111. *
  112. * This function should be called after devices have been suspended.
  113. */
  114. static int suspend_enter(suspend_state_t state)
  115. {
  116. int error;
  117. if (suspend_ops->prepare) {
  118. error = suspend_ops->prepare();
  119. if (error)
  120. goto Platform_finish;
  121. }
  122. error = dpm_suspend_noirq(PMSG_SUSPEND);
  123. if (error) {
  124. printk(KERN_ERR "PM: Some devices failed to power down\n");
  125. goto Platform_finish;
  126. }
  127. if (suspend_ops->prepare_late) {
  128. error = suspend_ops->prepare_late();
  129. if (error)
  130. goto Platform_wake;
  131. }
  132. if (suspend_test(TEST_PLATFORM))
  133. goto Platform_wake;
  134. error = disable_nonboot_cpus();
  135. if (error || suspend_test(TEST_CPUS))
  136. goto Enable_cpus;
  137. arch_suspend_disable_irqs();
  138. BUG_ON(!irqs_disabled());
  139. error = sysdev_suspend(PMSG_SUSPEND);
  140. if (!error) {
  141. if (!suspend_test(TEST_CORE) && pm_check_wakeup_events()) {
  142. error = suspend_ops->enter(state);
  143. events_check_enabled = false;
  144. }
  145. sysdev_resume();
  146. }
  147. arch_suspend_enable_irqs();
  148. BUG_ON(irqs_disabled());
  149. Enable_cpus:
  150. enable_nonboot_cpus();
  151. Platform_wake:
  152. if (suspend_ops->wake)
  153. suspend_ops->wake();
  154. dpm_resume_noirq(PMSG_RESUME);
  155. Platform_finish:
  156. if (suspend_ops->finish)
  157. suspend_ops->finish();
  158. return error;
  159. }
  160. /**
  161. * suspend_devices_and_enter - suspend devices and enter the desired system
  162. * sleep state.
  163. * @state: state to enter
  164. */
  165. int suspend_devices_and_enter(suspend_state_t state)
  166. {
  167. int error;
  168. if (!suspend_ops)
  169. return -ENOSYS;
  170. if (suspend_ops->begin) {
  171. error = suspend_ops->begin(state);
  172. if (error)
  173. goto Close;
  174. }
  175. suspend_console();
  176. pm_restrict_gfp_mask();
  177. suspend_test_start();
  178. error = dpm_suspend_start(PMSG_SUSPEND);
  179. if (error) {
  180. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  181. goto Recover_platform;
  182. }
  183. suspend_test_finish("suspend devices");
  184. if (suspend_test(TEST_DEVICES))
  185. goto Recover_platform;
  186. suspend_enter(state);
  187. Resume_devices:
  188. suspend_test_start();
  189. dpm_resume_end(PMSG_RESUME);
  190. suspend_test_finish("resume devices");
  191. pm_restore_gfp_mask();
  192. resume_console();
  193. Close:
  194. if (suspend_ops->end)
  195. suspend_ops->end();
  196. return error;
  197. Recover_platform:
  198. if (suspend_ops->recover)
  199. suspend_ops->recover();
  200. goto Resume_devices;
  201. }
  202. /**
  203. * suspend_finish - Do final work before exiting suspend sequence.
  204. *
  205. * Call platform code to clean up, restart processes, and free the
  206. * console that we've allocated. This is not called for suspend-to-disk.
  207. */
  208. static void suspend_finish(void)
  209. {
  210. suspend_thaw_processes();
  211. usermodehelper_enable();
  212. pm_notifier_call_chain(PM_POST_SUSPEND);
  213. pm_restore_console();
  214. }
  215. /**
  216. * enter_state - Do common work of entering low-power state.
  217. * @state: pm_state structure for state we're entering.
  218. *
  219. * Make sure we're the only ones trying to enter a sleep state. Fail
  220. * if someone has beat us to it, since we don't want anything weird to
  221. * happen when we wake up.
  222. * Then, do the setup for suspend, enter the state, and cleaup (after
  223. * we've woken up).
  224. */
  225. int enter_state(suspend_state_t state)
  226. {
  227. int error;
  228. if (!valid_state(state))
  229. return -ENODEV;
  230. if (!mutex_trylock(&pm_mutex))
  231. return -EBUSY;
  232. printk(KERN_INFO "PM: Syncing filesystems ... ");
  233. sys_sync();
  234. printk("done.\n");
  235. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  236. error = suspend_prepare();
  237. if (error)
  238. goto Unlock;
  239. if (suspend_test(TEST_FREEZER))
  240. goto Finish;
  241. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  242. error = suspend_devices_and_enter(state);
  243. Finish:
  244. pr_debug("PM: Finishing wakeup.\n");
  245. suspend_finish();
  246. Unlock:
  247. mutex_unlock(&pm_mutex);
  248. return error;
  249. }
  250. /**
  251. * pm_suspend - Externally visible function for suspending system.
  252. * @state: Enumerated value of state to enter.
  253. *
  254. * Determine whether or not value is within range, get state
  255. * structure, and enter (above).
  256. */
  257. int pm_suspend(suspend_state_t state)
  258. {
  259. if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
  260. return enter_state(state);
  261. return -EINVAL;
  262. }
  263. EXPORT_SYMBOL(pm_suspend);