suspend.c 6.5 KB

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