suspend.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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. #define pr_fmt(fmt) "PM: " fmt
  11. #include <linux/string.h>
  12. #include <linux/delay.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/console.h>
  16. #include <linux/cpu.h>
  17. #include <linux/cpuidle.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/gfp.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include <linux/export.h>
  26. #include <linux/suspend.h>
  27. #include <linux/syscore_ops.h>
  28. #include <linux/swait.h>
  29. #include <linux/ftrace.h>
  30. #include <trace/events/power.h>
  31. #include <linux/compiler.h>
  32. #include <linux/moduleparam.h>
  33. #include "power.h"
  34. const char * const pm_labels[] = {
  35. [PM_SUSPEND_TO_IDLE] = "freeze",
  36. [PM_SUSPEND_STANDBY] = "standby",
  37. [PM_SUSPEND_MEM] = "mem",
  38. };
  39. const char *pm_states[PM_SUSPEND_MAX];
  40. static const char * const mem_sleep_labels[] = {
  41. [PM_SUSPEND_TO_IDLE] = "s2idle",
  42. [PM_SUSPEND_STANDBY] = "shallow",
  43. [PM_SUSPEND_MEM] = "deep",
  44. };
  45. const char *mem_sleep_states[PM_SUSPEND_MAX];
  46. suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE;
  47. suspend_state_t mem_sleep_default = PM_SUSPEND_MAX;
  48. suspend_state_t pm_suspend_target_state;
  49. EXPORT_SYMBOL_GPL(pm_suspend_target_state);
  50. unsigned int pm_suspend_global_flags;
  51. EXPORT_SYMBOL_GPL(pm_suspend_global_flags);
  52. static const struct platform_suspend_ops *suspend_ops;
  53. static const struct platform_s2idle_ops *s2idle_ops;
  54. static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head);
  55. enum s2idle_states __read_mostly s2idle_state;
  56. static DEFINE_RAW_SPINLOCK(s2idle_lock);
  57. bool pm_suspend_via_s2idle(void)
  58. {
  59. return mem_sleep_current == PM_SUSPEND_TO_IDLE;
  60. }
  61. EXPORT_SYMBOL_GPL(pm_suspend_via_s2idle);
  62. void s2idle_set_ops(const struct platform_s2idle_ops *ops)
  63. {
  64. lock_system_sleep();
  65. s2idle_ops = ops;
  66. unlock_system_sleep();
  67. }
  68. static void s2idle_begin(void)
  69. {
  70. s2idle_state = S2IDLE_STATE_NONE;
  71. }
  72. static void s2idle_enter(void)
  73. {
  74. trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
  75. raw_spin_lock_irq(&s2idle_lock);
  76. if (pm_wakeup_pending())
  77. goto out;
  78. s2idle_state = S2IDLE_STATE_ENTER;
  79. raw_spin_unlock_irq(&s2idle_lock);
  80. get_online_cpus();
  81. cpuidle_resume();
  82. /* Push all the CPUs into the idle loop. */
  83. wake_up_all_idle_cpus();
  84. /* Make the current CPU wait so it can enter the idle loop too. */
  85. swait_event_exclusive(s2idle_wait_head,
  86. s2idle_state == S2IDLE_STATE_WAKE);
  87. cpuidle_pause();
  88. put_online_cpus();
  89. raw_spin_lock_irq(&s2idle_lock);
  90. out:
  91. s2idle_state = S2IDLE_STATE_NONE;
  92. raw_spin_unlock_irq(&s2idle_lock);
  93. trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
  94. }
  95. static void s2idle_loop(void)
  96. {
  97. pm_pr_dbg("suspend-to-idle\n");
  98. for (;;) {
  99. int error;
  100. dpm_noirq_begin();
  101. /*
  102. * Suspend-to-idle equals
  103. * frozen processes + suspended devices + idle processors.
  104. * Thus s2idle_enter() should be called right after
  105. * all devices have been suspended.
  106. *
  107. * Wakeups during the noirq suspend of devices may be spurious,
  108. * so prevent them from terminating the loop right away.
  109. */
  110. error = dpm_noirq_suspend_devices(PMSG_SUSPEND);
  111. if (!error)
  112. s2idle_enter();
  113. else if (error == -EBUSY && pm_wakeup_pending())
  114. error = 0;
  115. if (!error && s2idle_ops && s2idle_ops->wake)
  116. s2idle_ops->wake();
  117. dpm_noirq_resume_devices(PMSG_RESUME);
  118. dpm_noirq_end();
  119. if (error)
  120. break;
  121. if (s2idle_ops && s2idle_ops->sync)
  122. s2idle_ops->sync();
  123. if (pm_wakeup_pending())
  124. break;
  125. pm_wakeup_clear(false);
  126. }
  127. pm_pr_dbg("resume from suspend-to-idle\n");
  128. }
  129. void s2idle_wake(void)
  130. {
  131. unsigned long flags;
  132. raw_spin_lock_irqsave(&s2idle_lock, flags);
  133. if (s2idle_state > S2IDLE_STATE_NONE) {
  134. s2idle_state = S2IDLE_STATE_WAKE;
  135. swake_up_one(&s2idle_wait_head);
  136. }
  137. raw_spin_unlock_irqrestore(&s2idle_lock, flags);
  138. }
  139. EXPORT_SYMBOL_GPL(s2idle_wake);
  140. static bool valid_state(suspend_state_t state)
  141. {
  142. /*
  143. * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
  144. * support and need to be valid to the low level
  145. * implementation, no valid callback implies that none are valid.
  146. */
  147. return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
  148. }
  149. void __init pm_states_init(void)
  150. {
  151. /* "mem" and "freeze" are always present in /sys/power/state. */
  152. pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
  153. pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
  154. /*
  155. * Suspend-to-idle should be supported even without any suspend_ops,
  156. * initialize mem_sleep_states[] accordingly here.
  157. */
  158. mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
  159. }
  160. static int __init mem_sleep_default_setup(char *str)
  161. {
  162. suspend_state_t state;
  163. for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
  164. if (mem_sleep_labels[state] &&
  165. !strcmp(str, mem_sleep_labels[state])) {
  166. mem_sleep_default = state;
  167. break;
  168. }
  169. return 1;
  170. }
  171. __setup("mem_sleep_default=", mem_sleep_default_setup);
  172. /**
  173. * suspend_set_ops - Set the global suspend method table.
  174. * @ops: Suspend operations to use.
  175. */
  176. void suspend_set_ops(const struct platform_suspend_ops *ops)
  177. {
  178. lock_system_sleep();
  179. suspend_ops = ops;
  180. if (valid_state(PM_SUSPEND_STANDBY)) {
  181. mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
  182. pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
  183. if (mem_sleep_default == PM_SUSPEND_STANDBY)
  184. mem_sleep_current = PM_SUSPEND_STANDBY;
  185. }
  186. if (valid_state(PM_SUSPEND_MEM)) {
  187. mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
  188. if (mem_sleep_default >= PM_SUSPEND_MEM)
  189. mem_sleep_current = PM_SUSPEND_MEM;
  190. }
  191. unlock_system_sleep();
  192. }
  193. EXPORT_SYMBOL_GPL(suspend_set_ops);
  194. /**
  195. * suspend_valid_only_mem - Generic memory-only valid callback.
  196. *
  197. * Platform drivers that implement mem suspend only and only need to check for
  198. * that in their .valid() callback can use this instead of rolling their own
  199. * .valid() callback.
  200. */
  201. int suspend_valid_only_mem(suspend_state_t state)
  202. {
  203. return state == PM_SUSPEND_MEM;
  204. }
  205. EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
  206. static bool sleep_state_supported(suspend_state_t state)
  207. {
  208. return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
  209. }
  210. static int platform_suspend_prepare(suspend_state_t state)
  211. {
  212. return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
  213. suspend_ops->prepare() : 0;
  214. }
  215. static int platform_suspend_prepare_late(suspend_state_t state)
  216. {
  217. return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
  218. s2idle_ops->prepare() : 0;
  219. }
  220. static int platform_suspend_prepare_noirq(suspend_state_t state)
  221. {
  222. return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare_late ?
  223. suspend_ops->prepare_late() : 0;
  224. }
  225. static void platform_resume_noirq(suspend_state_t state)
  226. {
  227. if (state != PM_SUSPEND_TO_IDLE && suspend_ops->wake)
  228. suspend_ops->wake();
  229. }
  230. static void platform_resume_early(suspend_state_t state)
  231. {
  232. if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
  233. s2idle_ops->restore();
  234. }
  235. static void platform_resume_finish(suspend_state_t state)
  236. {
  237. if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
  238. suspend_ops->finish();
  239. }
  240. static int platform_suspend_begin(suspend_state_t state)
  241. {
  242. if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
  243. return s2idle_ops->begin();
  244. else if (suspend_ops && suspend_ops->begin)
  245. return suspend_ops->begin(state);
  246. else
  247. return 0;
  248. }
  249. static void platform_resume_end(suspend_state_t state)
  250. {
  251. if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
  252. s2idle_ops->end();
  253. else if (suspend_ops && suspend_ops->end)
  254. suspend_ops->end();
  255. }
  256. static void platform_recover(suspend_state_t state)
  257. {
  258. if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
  259. suspend_ops->recover();
  260. }
  261. static bool platform_suspend_again(suspend_state_t state)
  262. {
  263. return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
  264. suspend_ops->suspend_again() : false;
  265. }
  266. #ifdef CONFIG_PM_DEBUG
  267. static unsigned int pm_test_delay = 5;
  268. module_param(pm_test_delay, uint, 0644);
  269. MODULE_PARM_DESC(pm_test_delay,
  270. "Number of seconds to wait before resuming from suspend test");
  271. #endif
  272. static int suspend_test(int level)
  273. {
  274. #ifdef CONFIG_PM_DEBUG
  275. if (pm_test_level == level) {
  276. pr_info("suspend debug: Waiting for %d second(s).\n",
  277. pm_test_delay);
  278. mdelay(pm_test_delay * 1000);
  279. return 1;
  280. }
  281. #endif /* !CONFIG_PM_DEBUG */
  282. return 0;
  283. }
  284. /**
  285. * suspend_prepare - Prepare for entering system sleep state.
  286. *
  287. * Common code run for every system sleep state that can be entered (except for
  288. * hibernation). Run suspend notifiers, allocate the "suspend" console and
  289. * freeze processes.
  290. */
  291. static int suspend_prepare(suspend_state_t state)
  292. {
  293. int error, nr_calls = 0;
  294. if (!sleep_state_supported(state))
  295. return -EPERM;
  296. pm_prepare_console();
  297. error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
  298. if (error) {
  299. nr_calls--;
  300. goto Finish;
  301. }
  302. trace_suspend_resume(TPS("freeze_processes"), 0, true);
  303. error = suspend_freeze_processes();
  304. trace_suspend_resume(TPS("freeze_processes"), 0, false);
  305. if (!error)
  306. return 0;
  307. suspend_stats.failed_freeze++;
  308. dpm_save_failed_step(SUSPEND_FREEZE);
  309. Finish:
  310. __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
  311. pm_restore_console();
  312. return error;
  313. }
  314. /* default implementation */
  315. void __weak arch_suspend_disable_irqs(void)
  316. {
  317. local_irq_disable();
  318. }
  319. /* default implementation */
  320. void __weak arch_suspend_enable_irqs(void)
  321. {
  322. local_irq_enable();
  323. }
  324. /**
  325. * suspend_enter - Make the system enter the given sleep state.
  326. * @state: System sleep state to enter.
  327. * @wakeup: Returns information that the sleep state should not be re-entered.
  328. *
  329. * This function should be called after devices have been suspended.
  330. */
  331. static int suspend_enter(suspend_state_t state, bool *wakeup)
  332. {
  333. int error;
  334. error = platform_suspend_prepare(state);
  335. if (error)
  336. goto Platform_finish;
  337. error = dpm_suspend_late(PMSG_SUSPEND);
  338. if (error) {
  339. pr_err("late suspend of devices failed\n");
  340. goto Platform_finish;
  341. }
  342. error = platform_suspend_prepare_late(state);
  343. if (error)
  344. goto Devices_early_resume;
  345. if (state == PM_SUSPEND_TO_IDLE && pm_test_level != TEST_PLATFORM) {
  346. s2idle_loop();
  347. goto Platform_early_resume;
  348. }
  349. error = dpm_suspend_noirq(PMSG_SUSPEND);
  350. if (error) {
  351. pr_err("noirq suspend of devices failed\n");
  352. goto Platform_early_resume;
  353. }
  354. error = platform_suspend_prepare_noirq(state);
  355. if (error)
  356. goto Platform_wake;
  357. if (suspend_test(TEST_PLATFORM))
  358. goto Platform_wake;
  359. error = disable_nonboot_cpus();
  360. if (error || suspend_test(TEST_CPUS))
  361. goto Enable_cpus;
  362. arch_suspend_disable_irqs();
  363. BUG_ON(!irqs_disabled());
  364. system_state = SYSTEM_SUSPEND;
  365. error = syscore_suspend();
  366. if (!error) {
  367. *wakeup = pm_wakeup_pending();
  368. if (!(suspend_test(TEST_CORE) || *wakeup)) {
  369. trace_suspend_resume(TPS("machine_suspend"),
  370. state, true);
  371. error = suspend_ops->enter(state);
  372. trace_suspend_resume(TPS("machine_suspend"),
  373. state, false);
  374. } else if (*wakeup) {
  375. error = -EBUSY;
  376. }
  377. syscore_resume();
  378. }
  379. system_state = SYSTEM_RUNNING;
  380. arch_suspend_enable_irqs();
  381. BUG_ON(irqs_disabled());
  382. Enable_cpus:
  383. enable_nonboot_cpus();
  384. Platform_wake:
  385. platform_resume_noirq(state);
  386. dpm_resume_noirq(PMSG_RESUME);
  387. Platform_early_resume:
  388. platform_resume_early(state);
  389. Devices_early_resume:
  390. dpm_resume_early(PMSG_RESUME);
  391. Platform_finish:
  392. platform_resume_finish(state);
  393. return error;
  394. }
  395. /**
  396. * suspend_devices_and_enter - Suspend devices and enter system sleep state.
  397. * @state: System sleep state to enter.
  398. */
  399. int suspend_devices_and_enter(suspend_state_t state)
  400. {
  401. int error;
  402. bool wakeup = false;
  403. if (!sleep_state_supported(state))
  404. return -ENOSYS;
  405. pm_suspend_target_state = state;
  406. error = platform_suspend_begin(state);
  407. if (error)
  408. goto Close;
  409. suspend_console();
  410. suspend_test_start();
  411. error = dpm_suspend_start(PMSG_SUSPEND);
  412. if (error) {
  413. pr_err("Some devices failed to suspend, or early wake event detected\n");
  414. goto Recover_platform;
  415. }
  416. suspend_test_finish("suspend devices");
  417. if (suspend_test(TEST_DEVICES))
  418. goto Recover_platform;
  419. do {
  420. error = suspend_enter(state, &wakeup);
  421. } while (!error && !wakeup && platform_suspend_again(state));
  422. Resume_devices:
  423. suspend_test_start();
  424. dpm_resume_end(PMSG_RESUME);
  425. suspend_test_finish("resume devices");
  426. trace_suspend_resume(TPS("resume_console"), state, true);
  427. resume_console();
  428. trace_suspend_resume(TPS("resume_console"), state, false);
  429. Close:
  430. platform_resume_end(state);
  431. pm_suspend_target_state = PM_SUSPEND_ON;
  432. return error;
  433. Recover_platform:
  434. platform_recover(state);
  435. goto Resume_devices;
  436. }
  437. /**
  438. * suspend_finish - Clean up before finishing the suspend sequence.
  439. *
  440. * Call platform code to clean up, restart processes, and free the console that
  441. * we've allocated. This routine is not called for hibernation.
  442. */
  443. static void suspend_finish(void)
  444. {
  445. suspend_thaw_processes();
  446. pm_notifier_call_chain(PM_POST_SUSPEND);
  447. pm_restore_console();
  448. }
  449. /**
  450. * enter_state - Do common work needed to enter system sleep state.
  451. * @state: System sleep state to enter.
  452. *
  453. * Make sure that no one else is trying to put the system into a sleep state.
  454. * Fail if that's not the case. Otherwise, prepare for system suspend, make the
  455. * system enter the given sleep state and clean up after wakeup.
  456. */
  457. static int enter_state(suspend_state_t state)
  458. {
  459. int error;
  460. trace_suspend_resume(TPS("suspend_enter"), state, true);
  461. if (state == PM_SUSPEND_TO_IDLE) {
  462. #ifdef CONFIG_PM_DEBUG
  463. if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
  464. pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
  465. return -EAGAIN;
  466. }
  467. #endif
  468. } else if (!valid_state(state)) {
  469. return -EINVAL;
  470. }
  471. if (!mutex_trylock(&system_transition_mutex))
  472. return -EBUSY;
  473. if (state == PM_SUSPEND_TO_IDLE)
  474. s2idle_begin();
  475. #ifndef CONFIG_SUSPEND_SKIP_SYNC
  476. trace_suspend_resume(TPS("sync_filesystems"), 0, true);
  477. pr_info("Syncing filesystems ... ");
  478. ksys_sync();
  479. pr_cont("done.\n");
  480. trace_suspend_resume(TPS("sync_filesystems"), 0, false);
  481. #endif
  482. pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
  483. pm_suspend_clear_flags();
  484. error = suspend_prepare(state);
  485. if (error)
  486. goto Unlock;
  487. if (suspend_test(TEST_FREEZER))
  488. goto Finish;
  489. trace_suspend_resume(TPS("suspend_enter"), state, false);
  490. pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
  491. pm_restrict_gfp_mask();
  492. error = suspend_devices_and_enter(state);
  493. pm_restore_gfp_mask();
  494. Finish:
  495. events_check_enabled = false;
  496. pm_pr_dbg("Finishing wakeup.\n");
  497. suspend_finish();
  498. Unlock:
  499. mutex_unlock(&system_transition_mutex);
  500. return error;
  501. }
  502. /**
  503. * pm_suspend - Externally visible function for suspending the system.
  504. * @state: System sleep state to enter.
  505. *
  506. * Check if the value of @state represents one of the supported states,
  507. * execute enter_state() and update system suspend statistics.
  508. */
  509. int pm_suspend(suspend_state_t state)
  510. {
  511. int error;
  512. if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
  513. return -EINVAL;
  514. pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
  515. error = enter_state(state);
  516. if (error) {
  517. suspend_stats.fail++;
  518. dpm_save_failed_errno(error);
  519. } else {
  520. suspend_stats.success++;
  521. }
  522. pr_info("suspend exit\n");
  523. return error;
  524. }
  525. EXPORT_SYMBOL(pm_suspend);