suspend.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. if (!suspend_ops)
  161. return -ENOSYS;
  162. if (suspend_ops->begin) {
  163. error = suspend_ops->begin(state);
  164. if (error)
  165. goto Close;
  166. }
  167. suspend_console();
  168. suspend_test_start();
  169. error = dpm_suspend_start(PMSG_SUSPEND);
  170. if (error) {
  171. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  172. goto Recover_platform;
  173. }
  174. suspend_test_finish("suspend devices");
  175. if (suspend_test(TEST_DEVICES))
  176. goto Recover_platform;
  177. suspend_enter(state);
  178. Resume_devices:
  179. suspend_test_start();
  180. dpm_resume_end(PMSG_RESUME);
  181. suspend_test_finish("resume devices");
  182. resume_console();
  183. Close:
  184. if (suspend_ops->end)
  185. suspend_ops->end();
  186. return error;
  187. Recover_platform:
  188. if (suspend_ops->recover)
  189. suspend_ops->recover();
  190. goto Resume_devices;
  191. }
  192. /**
  193. * suspend_finish - Do final work before exiting suspend sequence.
  194. *
  195. * Call platform code to clean up, restart processes, and free the
  196. * console that we've allocated. This is not called for suspend-to-disk.
  197. */
  198. static void suspend_finish(void)
  199. {
  200. suspend_thaw_processes();
  201. usermodehelper_enable();
  202. pm_notifier_call_chain(PM_POST_SUSPEND);
  203. pm_restore_console();
  204. }
  205. /**
  206. * enter_state - Do common work of entering low-power state.
  207. * @state: pm_state structure for state we're entering.
  208. *
  209. * Make sure we're the only ones trying to enter a sleep state. Fail
  210. * if someone has beat us to it, since we don't want anything weird to
  211. * happen when we wake up.
  212. * Then, do the setup for suspend, enter the state, and cleaup (after
  213. * we've woken up).
  214. */
  215. int enter_state(suspend_state_t state)
  216. {
  217. int error;
  218. if (!valid_state(state))
  219. return -ENODEV;
  220. if (!mutex_trylock(&pm_mutex))
  221. return -EBUSY;
  222. printk(KERN_INFO "PM: Syncing filesystems ... ");
  223. sys_sync();
  224. printk("done.\n");
  225. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  226. error = suspend_prepare();
  227. if (error)
  228. goto Unlock;
  229. if (suspend_test(TEST_FREEZER))
  230. goto Finish;
  231. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  232. error = suspend_devices_and_enter(state);
  233. Finish:
  234. pr_debug("PM: Finishing wakeup.\n");
  235. suspend_finish();
  236. Unlock:
  237. mutex_unlock(&pm_mutex);
  238. return error;
  239. }
  240. /**
  241. * pm_suspend - Externally visible function for suspending system.
  242. * @state: Enumerated value of state to enter.
  243. *
  244. * Determine whether or not value is within range, get state
  245. * structure, and enter (above).
  246. */
  247. int pm_suspend(suspend_state_t state)
  248. {
  249. if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
  250. return enter_state(state);
  251. return -EINVAL;
  252. }
  253. EXPORT_SYMBOL(pm_suspend);