suspend.c 10 KB

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