suspend.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/export.h>
  24. #include <linux/suspend.h>
  25. #include <linux/syscore_ops.h>
  26. #include <linux/rtc.h>
  27. #include <linux/ftrace.h>
  28. #include <trace/events/power.h>
  29. #include <linux/pm_qos.h>
  30. #include "power.h"
  31. const char *const pm_states[PM_SUSPEND_MAX] = {
  32. #ifdef CONFIG_EARLYSUSPEND
  33. [PM_SUSPEND_ON] = "on",
  34. #endif
  35. [PM_SUSPEND_STANDBY] = "standby",
  36. [PM_SUSPEND_MEM] = "mem",
  37. };
  38. static const struct platform_suspend_ops *suspend_ops;
  39. static struct pm_qos_request suspend_pm_qos;
  40. /**
  41. * suspend_set_ops - Set the global suspend method table.
  42. * @ops: Suspend operations to use.
  43. */
  44. void suspend_set_ops(const struct platform_suspend_ops *ops)
  45. {
  46. lock_system_sleep();
  47. suspend_ops = ops;
  48. unlock_system_sleep();
  49. }
  50. EXPORT_SYMBOL_GPL(suspend_set_ops);
  51. bool valid_state(suspend_state_t state)
  52. {
  53. /*
  54. * All states need lowlevel support and need to be valid to the lowlevel
  55. * implementation, no valid callback implies that none are valid.
  56. */
  57. return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
  58. }
  59. /**
  60. * suspend_valid_only_mem - Generic memory-only valid callback.
  61. *
  62. * Platform drivers that implement mem suspend only and only need to check for
  63. * that in their .valid() callback can use this instead of rolling their own
  64. * .valid() callback.
  65. */
  66. int suspend_valid_only_mem(suspend_state_t state)
  67. {
  68. return state == PM_SUSPEND_MEM;
  69. }
  70. EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
  71. static int suspend_test(int level)
  72. {
  73. #ifdef CONFIG_PM_DEBUG
  74. if (pm_test_level == level) {
  75. printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
  76. mdelay(5000);
  77. return 1;
  78. }
  79. #endif /* !CONFIG_PM_DEBUG */
  80. return 0;
  81. }
  82. /**
  83. * suspend_prepare - Prepare for entering system sleep state.
  84. *
  85. * Common code run for every system sleep state that can be entered (except for
  86. * hibernation). Run suspend notifiers, allocate the "suspend" console and
  87. * freeze processes.
  88. */
  89. static int suspend_prepare(void)
  90. {
  91. int error;
  92. if (!suspend_ops || !suspend_ops->enter)
  93. return -EPERM;
  94. pm_prepare_console();
  95. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  96. if (error)
  97. goto Finish;
  98. error = suspend_freeze_processes();
  99. if (!error)
  100. return 0;
  101. suspend_stats.failed_freeze++;
  102. dpm_save_failed_step(SUSPEND_FREEZE);
  103. Finish:
  104. pm_notifier_call_chain(PM_POST_SUSPEND);
  105. pm_restore_console();
  106. return error;
  107. }
  108. /* default implementation */
  109. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  110. {
  111. local_irq_disable();
  112. }
  113. /* default implementation */
  114. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  115. {
  116. local_irq_enable();
  117. }
  118. /**
  119. * suspend_enter - Make the system enter the given sleep state.
  120. * @state: System sleep state to enter.
  121. * @wakeup: Returns information that the sleep state should not be re-entered.
  122. *
  123. * This function should be called after devices have been suspended.
  124. */
  125. static int suspend_enter(suspend_state_t state, bool *wakeup)
  126. {
  127. int error;
  128. if (suspend_ops->prepare) {
  129. error = suspend_ops->prepare();
  130. if (error)
  131. goto Platform_finish;
  132. }
  133. error = dpm_suspend_end(PMSG_SUSPEND);
  134. if (error) {
  135. printk(KERN_ERR "PM: Some devices failed to power down\n");
  136. goto Platform_finish;
  137. }
  138. if (suspend_ops->prepare_late) {
  139. error = suspend_ops->prepare_late();
  140. if (error)
  141. goto Platform_wake;
  142. }
  143. if (suspend_test(TEST_PLATFORM))
  144. goto Platform_wake;
  145. error = disable_nonboot_cpus();
  146. if (error || suspend_test(TEST_CPUS))
  147. goto Enable_cpus;
  148. arch_suspend_disable_irqs();
  149. BUG_ON(!irqs_disabled());
  150. error = syscore_suspend();
  151. if (!error) {
  152. *wakeup = pm_wakeup_pending();
  153. if (!(suspend_test(TEST_CORE) || *wakeup)) {
  154. error = suspend_ops->enter(state);
  155. events_check_enabled = false;
  156. } else if (*wakeup) {
  157. error = -EBUSY;
  158. }
  159. syscore_resume();
  160. }
  161. arch_suspend_enable_irqs();
  162. BUG_ON(irqs_disabled());
  163. Enable_cpus:
  164. enable_nonboot_cpus();
  165. Platform_wake:
  166. if (suspend_ops->wake)
  167. suspend_ops->wake();
  168. dpm_resume_start(PMSG_RESUME);
  169. Platform_finish:
  170. if (suspend_ops->finish)
  171. suspend_ops->finish();
  172. return error;
  173. }
  174. /**
  175. * suspend_devices_and_enter - Suspend devices and enter system sleep state.
  176. * @state: System sleep state to enter.
  177. */
  178. int suspend_devices_and_enter(suspend_state_t state)
  179. {
  180. int error;
  181. bool wakeup = false;
  182. if (!suspend_ops)
  183. return -ENOSYS;
  184. trace_machine_suspend(state);
  185. if (suspend_ops->begin) {
  186. error = suspend_ops->begin(state);
  187. if (error)
  188. goto Close;
  189. }
  190. suspend_console();
  191. ftrace_stop();
  192. suspend_test_start();
  193. error = dpm_suspend_start(PMSG_SUSPEND);
  194. if (error) {
  195. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  196. goto Recover_platform;
  197. }
  198. suspend_test_finish("suspend devices");
  199. if (suspend_test(TEST_DEVICES))
  200. goto Recover_platform;
  201. do {
  202. error = suspend_enter(state, &wakeup);
  203. } while (!error && !wakeup
  204. && suspend_ops->suspend_again && suspend_ops->suspend_again());
  205. Resume_devices:
  206. suspend_test_start();
  207. dpm_resume_end(PMSG_RESUME);
  208. suspend_test_finish("resume devices");
  209. ftrace_start();
  210. resume_console();
  211. Close:
  212. if (suspend_ops->end)
  213. suspend_ops->end();
  214. trace_machine_suspend(PWR_EVENT_EXIT);
  215. return error;
  216. Recover_platform:
  217. if (suspend_ops->recover)
  218. suspend_ops->recover();
  219. goto Resume_devices;
  220. }
  221. /**
  222. * suspend_finish - Clean up before finishing the suspend sequence.
  223. *
  224. * Call platform code to clean up, restart processes, and free the console that
  225. * we've allocated. This routine is not called for hibernation.
  226. */
  227. static void suspend_finish(void)
  228. {
  229. suspend_thaw_processes();
  230. pm_notifier_call_chain(PM_POST_SUSPEND);
  231. pm_restore_console();
  232. }
  233. /**
  234. * enter_state - Do common work needed to enter system sleep state.
  235. * @state: System sleep state to enter.
  236. *
  237. * Make sure that no one else is trying to put the system into a sleep state.
  238. * Fail if that's not the case. Otherwise, prepare for system suspend, make the
  239. * system enter the given sleep state and clean up after wakeup.
  240. */
  241. static int enter_state(suspend_state_t state)
  242. {
  243. int error;
  244. if (!valid_state(state))
  245. return -ENODEV;
  246. if (!mutex_trylock(&pm_mutex))
  247. return -EBUSY;
  248. printk(KERN_INFO "PM: Syncing filesystems ... ");
  249. sys_sync();
  250. printk("done.\n");
  251. pm_qos_add_request(&suspend_pm_qos, PM_QOS_CPU_DMA_LATENCY,
  252. PM_QOS_DEFAULT_VALUE);
  253. pm_qos_update_request(&suspend_pm_qos, 0);
  254. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  255. error = suspend_prepare();
  256. if (error)
  257. goto Unlock;
  258. if (suspend_test(TEST_FREEZER))
  259. goto Finish;
  260. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  261. pm_restrict_gfp_mask();
  262. error = suspend_devices_and_enter(state);
  263. pm_restore_gfp_mask();
  264. Finish:
  265. pr_debug("PM: Finishing wakeup.\n");
  266. suspend_finish();
  267. Unlock:
  268. pm_qos_update_request(&suspend_pm_qos, PM_QOS_CPU_DMA_LATENCY);
  269. pm_qos_remove_request(&suspend_pm_qos);
  270. mutex_unlock(&pm_mutex);
  271. return error;
  272. }
  273. static void pm_suspend_marker(char *annotation)
  274. {
  275. struct timespec ts;
  276. struct rtc_time tm;
  277. getnstimeofday(&ts);
  278. rtc_time_to_tm(ts.tv_sec, &tm);
  279. pr_info("PM: suspend %s %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n",
  280. annotation, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  281. tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
  282. }
  283. /**
  284. * pm_suspend - Externally visible function for suspending the system.
  285. * @state: System sleep state to enter.
  286. *
  287. * Check if the value of @state represents one of the supported states,
  288. * execute enter_state() and update system suspend statistics.
  289. */
  290. int pm_suspend(suspend_state_t state)
  291. {
  292. int error;
  293. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  294. return -EINVAL;
  295. pm_suspend_marker("entry");
  296. error = enter_state(state);
  297. if (error) {
  298. suspend_stats.fail++;
  299. dpm_save_failed_errno(error);
  300. } else {
  301. suspend_stats.success++;
  302. }
  303. pm_suspend_marker("exit");
  304. return error;
  305. }
  306. EXPORT_SYMBOL(pm_suspend);