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