suspend.c 13 KB

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