suspend.c 13 KB

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