suspend.c 13 KB

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