suspend.c 6.6 KB

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