suspend.c 7.2 KB

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