suspend.c 10 KB

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