1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * kernel/power/suspend.c - Suspend to RAM and standby functionality.
4 *
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
7 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
8 */
9
10 #define pr_fmt(fmt) "PM: " fmt
11
12 #include <linux/string.h>
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/console.h>
17 #include <linux/cpu.h>
18 #include <linux/cpuidle.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 <linux/wakeup_reason.h>
34 #include <trace/hooks/suspend.h>
35
36 #include "power.h"
37
38 const char * const pm_labels[] = {
39 [PM_SUSPEND_TO_IDLE] = "freeze",
40 [PM_SUSPEND_STANDBY] = "standby",
41 [PM_SUSPEND_MEM] = "mem",
42 };
43 const char *pm_states[PM_SUSPEND_MAX];
44 static const char * const mem_sleep_labels[] = {
45 [PM_SUSPEND_TO_IDLE] = "s2idle",
46 [PM_SUSPEND_STANDBY] = "shallow",
47 [PM_SUSPEND_MEM] = "deep",
48 };
49 const char *mem_sleep_states[PM_SUSPEND_MAX];
50
51 suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE;
52 suspend_state_t mem_sleep_default = PM_SUSPEND_MAX;
53 suspend_state_t pm_suspend_target_state;
54 EXPORT_SYMBOL_GPL(pm_suspend_target_state);
55
56 unsigned int pm_suspend_global_flags;
57 EXPORT_SYMBOL_GPL(pm_suspend_global_flags);
58
59 static const struct platform_suspend_ops *suspend_ops;
60 static const struct platform_s2idle_ops *s2idle_ops;
61 static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head);
62
63 enum s2idle_states __read_mostly s2idle_state;
64 static DEFINE_RAW_SPINLOCK(s2idle_lock);
65
66 /**
67 * pm_suspend_default_s2idle - Check if suspend-to-idle is the default suspend.
68 *
69 * Return 'true' if suspend-to-idle has been selected as the default system
70 * suspend method.
71 */
pm_suspend_default_s2idle(void)72 bool pm_suspend_default_s2idle(void)
73 {
74 return mem_sleep_current == PM_SUSPEND_TO_IDLE;
75 }
76 EXPORT_SYMBOL_GPL(pm_suspend_default_s2idle);
77
s2idle_set_ops(const struct platform_s2idle_ops * ops)78 void s2idle_set_ops(const struct platform_s2idle_ops *ops)
79 {
80 lock_system_sleep();
81 s2idle_ops = ops;
82 unlock_system_sleep();
83 }
84
s2idle_begin(void)85 static void s2idle_begin(void)
86 {
87 s2idle_state = S2IDLE_STATE_NONE;
88 }
89
s2idle_enter(void)90 static void s2idle_enter(void)
91 {
92 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
93
94 raw_spin_lock_irq(&s2idle_lock);
95 if (pm_wakeup_pending())
96 goto out;
97
98 s2idle_state = S2IDLE_STATE_ENTER;
99 raw_spin_unlock_irq(&s2idle_lock);
100
101 cpus_read_lock();
102 cpuidle_resume();
103
104 /* Push all the CPUs into the idle loop. */
105 wake_up_all_idle_cpus();
106 /* Make the current CPU wait so it can enter the idle loop too. */
107 swait_event_exclusive(s2idle_wait_head,
108 s2idle_state == S2IDLE_STATE_WAKE);
109
110 cpuidle_pause();
111 cpus_read_unlock();
112
113 raw_spin_lock_irq(&s2idle_lock);
114
115 out:
116 s2idle_state = S2IDLE_STATE_NONE;
117 raw_spin_unlock_irq(&s2idle_lock);
118
119 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
120 }
121
s2idle_loop(void)122 static void s2idle_loop(void)
123 {
124 pm_pr_dbg("suspend-to-idle\n");
125
126 /*
127 * Suspend-to-idle equals:
128 * frozen processes + suspended devices + idle processors.
129 * Thus s2idle_enter() should be called right after all devices have
130 * been suspended.
131 *
132 * Wakeups during the noirq suspend of devices may be spurious, so try
133 * to avoid them upfront.
134 */
135 for (;;) {
136 if (s2idle_ops && s2idle_ops->wake) {
137 if (s2idle_ops->wake())
138 break;
139 } else if (pm_wakeup_pending()) {
140 break;
141 }
142
143 clear_wakeup_reasons();
144 s2idle_enter();
145 }
146
147 pm_pr_dbg("resume from suspend-to-idle\n");
148 }
149
s2idle_wake(void)150 void s2idle_wake(void)
151 {
152 unsigned long flags;
153
154 raw_spin_lock_irqsave(&s2idle_lock, flags);
155 if (s2idle_state > S2IDLE_STATE_NONE) {
156 s2idle_state = S2IDLE_STATE_WAKE;
157 swake_up_one(&s2idle_wait_head);
158 }
159 raw_spin_unlock_irqrestore(&s2idle_lock, flags);
160 }
161 EXPORT_SYMBOL_GPL(s2idle_wake);
162
valid_state(suspend_state_t state)163 static bool valid_state(suspend_state_t state)
164 {
165 /*
166 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
167 * support and need to be valid to the low level
168 * implementation, no valid callback implies that none are valid.
169 */
170 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
171 }
172
pm_states_init(void)173 void __init pm_states_init(void)
174 {
175 /* "mem" and "freeze" are always present in /sys/power/state. */
176 pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
177 pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
178 /*
179 * Suspend-to-idle should be supported even without any suspend_ops,
180 * initialize mem_sleep_states[] accordingly here.
181 */
182 mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
183 }
184
mem_sleep_default_setup(char * str)185 static int __init mem_sleep_default_setup(char *str)
186 {
187 suspend_state_t state;
188
189 for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
190 if (mem_sleep_labels[state] &&
191 !strcmp(str, mem_sleep_labels[state])) {
192 mem_sleep_default = state;
193 break;
194 }
195
196 return 1;
197 }
198 __setup("mem_sleep_default=", mem_sleep_default_setup);
199
200 /**
201 * suspend_set_ops - Set the global suspend method table.
202 * @ops: Suspend operations to use.
203 */
suspend_set_ops(const struct platform_suspend_ops * ops)204 void suspend_set_ops(const struct platform_suspend_ops *ops)
205 {
206 lock_system_sleep();
207
208 suspend_ops = ops;
209
210 if (valid_state(PM_SUSPEND_STANDBY)) {
211 mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
212 pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
213 if (mem_sleep_default == PM_SUSPEND_STANDBY)
214 mem_sleep_current = PM_SUSPEND_STANDBY;
215 }
216 if (valid_state(PM_SUSPEND_MEM)) {
217 mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
218 if (mem_sleep_default >= PM_SUSPEND_MEM)
219 mem_sleep_current = PM_SUSPEND_MEM;
220 }
221
222 unlock_system_sleep();
223 }
224 EXPORT_SYMBOL_GPL(suspend_set_ops);
225
226 /**
227 * suspend_valid_only_mem - Generic memory-only valid callback.
228 * @state: Target system sleep state.
229 *
230 * Platform drivers that implement mem suspend only and only need to check for
231 * that in their .valid() callback can use this instead of rolling their own
232 * .valid() callback.
233 */
suspend_valid_only_mem(suspend_state_t state)234 int suspend_valid_only_mem(suspend_state_t state)
235 {
236 return state == PM_SUSPEND_MEM;
237 }
238 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
239
sleep_state_supported(suspend_state_t state)240 static bool sleep_state_supported(suspend_state_t state)
241 {
242 return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
243 }
244
platform_suspend_prepare(suspend_state_t state)245 static int platform_suspend_prepare(suspend_state_t state)
246 {
247 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
248 suspend_ops->prepare() : 0;
249 }
250
platform_suspend_prepare_late(suspend_state_t state)251 static int platform_suspend_prepare_late(suspend_state_t state)
252 {
253 return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
254 s2idle_ops->prepare() : 0;
255 }
256
platform_suspend_prepare_noirq(suspend_state_t state)257 static int platform_suspend_prepare_noirq(suspend_state_t state)
258 {
259 if (state == PM_SUSPEND_TO_IDLE)
260 return s2idle_ops && s2idle_ops->prepare_late ?
261 s2idle_ops->prepare_late() : 0;
262
263 return suspend_ops->prepare_late ? suspend_ops->prepare_late() : 0;
264 }
265
platform_resume_noirq(suspend_state_t state)266 static void platform_resume_noirq(suspend_state_t state)
267 {
268 if (state == PM_SUSPEND_TO_IDLE) {
269 if (s2idle_ops && s2idle_ops->restore_early)
270 s2idle_ops->restore_early();
271 } else if (suspend_ops->wake) {
272 suspend_ops->wake();
273 }
274 }
275
platform_resume_early(suspend_state_t state)276 static void platform_resume_early(suspend_state_t state)
277 {
278 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
279 s2idle_ops->restore();
280 }
281
platform_resume_finish(suspend_state_t state)282 static void platform_resume_finish(suspend_state_t state)
283 {
284 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
285 suspend_ops->finish();
286 }
287
platform_suspend_begin(suspend_state_t state)288 static int platform_suspend_begin(suspend_state_t state)
289 {
290 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
291 return s2idle_ops->begin();
292 else if (suspend_ops && suspend_ops->begin)
293 return suspend_ops->begin(state);
294 else
295 return 0;
296 }
297
platform_resume_end(suspend_state_t state)298 static void platform_resume_end(suspend_state_t state)
299 {
300 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
301 s2idle_ops->end();
302 else if (suspend_ops && suspend_ops->end)
303 suspend_ops->end();
304 }
305
platform_recover(suspend_state_t state)306 static void platform_recover(suspend_state_t state)
307 {
308 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
309 suspend_ops->recover();
310 }
311
platform_suspend_again(suspend_state_t state)312 static bool platform_suspend_again(suspend_state_t state)
313 {
314 return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
315 suspend_ops->suspend_again() : false;
316 }
317
318 #ifdef CONFIG_PM_DEBUG
319 static unsigned int pm_test_delay = 5;
320 module_param(pm_test_delay, uint, 0644);
321 MODULE_PARM_DESC(pm_test_delay,
322 "Number of seconds to wait before resuming from suspend test");
323 #endif
324
suspend_test(int level)325 static int suspend_test(int level)
326 {
327 #ifdef CONFIG_PM_DEBUG
328 if (pm_test_level == level) {
329 pr_info("suspend debug: Waiting for %d second(s).\n",
330 pm_test_delay);
331 mdelay(pm_test_delay * 1000);
332 return 1;
333 }
334 #endif /* !CONFIG_PM_DEBUG */
335 return 0;
336 }
337
338 /**
339 * suspend_prepare - Prepare for entering system sleep state.
340 * @state: Target system sleep state.
341 *
342 * Common code run for every system sleep state that can be entered (except for
343 * hibernation). Run suspend notifiers, allocate the "suspend" console and
344 * freeze processes.
345 */
suspend_prepare(suspend_state_t state)346 static int suspend_prepare(suspend_state_t state)
347 {
348 int error;
349
350 if (!sleep_state_supported(state))
351 return -EPERM;
352
353 pm_prepare_console();
354
355 error = pm_notifier_call_chain_robust(PM_SUSPEND_PREPARE, PM_POST_SUSPEND);
356 if (error)
357 goto Restore;
358
359 trace_suspend_resume(TPS("freeze_processes"), 0, true);
360 error = suspend_freeze_processes();
361 trace_suspend_resume(TPS("freeze_processes"), 0, false);
362 if (!error)
363 return 0;
364
365 log_suspend_abort_reason("One or more tasks refusing to freeze");
366 suspend_stats.failed_freeze++;
367 dpm_save_failed_step(SUSPEND_FREEZE);
368 pm_notifier_call_chain(PM_POST_SUSPEND);
369 Restore:
370 pm_restore_console();
371 return error;
372 }
373
374 /* default implementation */
arch_suspend_disable_irqs(void)375 void __weak arch_suspend_disable_irqs(void)
376 {
377 local_irq_disable();
378 }
379
380 /* default implementation */
arch_suspend_enable_irqs(void)381 void __weak arch_suspend_enable_irqs(void)
382 {
383 local_irq_enable();
384 }
385
386 /**
387 * suspend_enter - Make the system enter the given sleep state.
388 * @state: System sleep state to enter.
389 * @wakeup: Returns information that the sleep state should not be re-entered.
390 *
391 * This function should be called after devices have been suspended.
392 */
suspend_enter(suspend_state_t state,bool * wakeup)393 static int suspend_enter(suspend_state_t state, bool *wakeup)
394 {
395 int error, last_dev;
396
397 error = platform_suspend_prepare(state);
398 if (error)
399 goto Platform_finish;
400
401 error = dpm_suspend_late(PMSG_SUSPEND);
402 if (error) {
403 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
404 last_dev %= REC_FAILED_NUM;
405 pr_err("late suspend of devices failed\n");
406 log_suspend_abort_reason("late suspend of %s device failed",
407 suspend_stats.failed_devs[last_dev]);
408 goto Platform_finish;
409 }
410 error = platform_suspend_prepare_late(state);
411 if (error)
412 goto Devices_early_resume;
413
414 error = dpm_suspend_noirq(PMSG_SUSPEND);
415 if (error) {
416 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
417 last_dev %= REC_FAILED_NUM;
418 pr_err("noirq suspend of devices failed\n");
419 log_suspend_abort_reason("noirq suspend of %s device failed",
420 suspend_stats.failed_devs[last_dev]);
421 goto Platform_early_resume;
422 }
423 error = platform_suspend_prepare_noirq(state);
424 if (error)
425 goto Platform_wake;
426
427 if (suspend_test(TEST_PLATFORM))
428 goto Platform_wake;
429
430 if (state == PM_SUSPEND_TO_IDLE) {
431 s2idle_loop();
432 goto Platform_wake;
433 }
434
435 error = suspend_disable_secondary_cpus();
436 if (error || suspend_test(TEST_CPUS)) {
437 log_suspend_abort_reason("Disabling non-boot cpus failed");
438 goto Enable_cpus;
439 }
440
441 arch_suspend_disable_irqs();
442 BUG_ON(!irqs_disabled());
443
444 system_state = SYSTEM_SUSPEND;
445
446 error = syscore_suspend();
447 if (!error) {
448 *wakeup = pm_wakeup_pending();
449 if (!(suspend_test(TEST_CORE) || *wakeup)) {
450 trace_suspend_resume(TPS("machine_suspend"),
451 state, true);
452 error = suspend_ops->enter(state);
453 trace_suspend_resume(TPS("machine_suspend"),
454 state, false);
455 trace_android_vh_early_resume_begin(NULL);
456 } else if (*wakeup) {
457 error = -EBUSY;
458 }
459 syscore_resume();
460 }
461
462 system_state = SYSTEM_RUNNING;
463
464 arch_suspend_enable_irqs();
465 BUG_ON(irqs_disabled());
466
467 Enable_cpus:
468 suspend_enable_secondary_cpus();
469
470 Platform_wake:
471 platform_resume_noirq(state);
472 dpm_resume_noirq(PMSG_RESUME);
473
474 Platform_early_resume:
475 platform_resume_early(state);
476
477 Devices_early_resume:
478 dpm_resume_early(PMSG_RESUME);
479
480 Platform_finish:
481 platform_resume_finish(state);
482 return error;
483 }
484
485 /**
486 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
487 * @state: System sleep state to enter.
488 */
suspend_devices_and_enter(suspend_state_t state)489 int suspend_devices_and_enter(suspend_state_t state)
490 {
491 int error;
492 bool wakeup = false;
493
494 if (!sleep_state_supported(state))
495 return -ENOSYS;
496
497 pm_suspend_target_state = state;
498
499 if (state == PM_SUSPEND_TO_IDLE)
500 pm_set_suspend_no_platform();
501
502 error = platform_suspend_begin(state);
503 if (error)
504 goto Close;
505
506 suspend_console();
507 suspend_test_start();
508 error = dpm_suspend_start(PMSG_SUSPEND);
509 if (error) {
510 pr_err("Some devices failed to suspend, or early wake event detected\n");
511 log_suspend_abort_reason(
512 "Some devices failed to suspend, or early wake event detected");
513 goto Recover_platform;
514 }
515 suspend_test_finish("suspend devices");
516 if (suspend_test(TEST_DEVICES))
517 goto Recover_platform;
518
519 do {
520 error = suspend_enter(state, &wakeup);
521 } while (!error && !wakeup && platform_suspend_again(state));
522
523 Resume_devices:
524 trace_android_vh_resume_begin(NULL);
525 suspend_test_start();
526 dpm_resume_end(PMSG_RESUME);
527 suspend_test_finish("resume devices");
528 trace_suspend_resume(TPS("resume_console"), state, true);
529 resume_console();
530 trace_suspend_resume(TPS("resume_console"), state, false);
531
532 Close:
533 platform_resume_end(state);
534 pm_suspend_target_state = PM_SUSPEND_ON;
535 trace_android_vh_resume_end(NULL);
536 return error;
537
538 Recover_platform:
539 platform_recover(state);
540 goto Resume_devices;
541 }
542
543 /**
544 * suspend_finish - Clean up before finishing the suspend sequence.
545 *
546 * Call platform code to clean up, restart processes, and free the console that
547 * we've allocated. This routine is not called for hibernation.
548 */
suspend_finish(void)549 static void suspend_finish(void)
550 {
551 suspend_thaw_processes();
552 pm_notifier_call_chain(PM_POST_SUSPEND);
553 pm_restore_console();
554 }
555
556 /**
557 * enter_state - Do common work needed to enter system sleep state.
558 * @state: System sleep state to enter.
559 *
560 * Make sure that no one else is trying to put the system into a sleep state.
561 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
562 * system enter the given sleep state and clean up after wakeup.
563 */
enter_state(suspend_state_t state)564 static int enter_state(suspend_state_t state)
565 {
566 int error;
567
568 trace_suspend_resume(TPS("suspend_enter"), state, true);
569 if (state == PM_SUSPEND_TO_IDLE) {
570 #ifdef CONFIG_PM_DEBUG
571 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
572 pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
573 return -EAGAIN;
574 }
575 #endif
576 } else if (!valid_state(state)) {
577 return -EINVAL;
578 }
579 if (!mutex_trylock(&system_transition_mutex))
580 return -EBUSY;
581
582 if (state == PM_SUSPEND_TO_IDLE)
583 s2idle_begin();
584
585 if (sync_on_suspend_enabled) {
586 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
587 ksys_sync_helper();
588 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
589 }
590
591 pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
592 pm_suspend_clear_flags();
593 error = suspend_prepare(state);
594 if (error)
595 goto Unlock;
596
597 if (suspend_test(TEST_FREEZER))
598 goto Finish;
599
600 trace_suspend_resume(TPS("suspend_enter"), state, false);
601 pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
602 pm_restrict_gfp_mask();
603 error = suspend_devices_and_enter(state);
604 pm_restore_gfp_mask();
605
606 Finish:
607 events_check_enabled = false;
608 pm_pr_dbg("Finishing wakeup.\n");
609 suspend_finish();
610 Unlock:
611 mutex_unlock(&system_transition_mutex);
612 return error;
613 }
614
615 /**
616 * pm_suspend - Externally visible function for suspending the system.
617 * @state: System sleep state to enter.
618 *
619 * Check if the value of @state represents one of the supported states,
620 * execute enter_state() and update system suspend statistics.
621 */
pm_suspend(suspend_state_t state)622 int pm_suspend(suspend_state_t state)
623 {
624 int error;
625
626 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
627 return -EINVAL;
628
629 pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
630 error = enter_state(state);
631 if (error) {
632 suspend_stats.fail++;
633 dpm_save_failed_errno(error);
634 } else {
635 suspend_stats.success++;
636 }
637 pr_info("suspend exit\n");
638 return error;
639 }
640 EXPORT_SYMBOL(pm_suspend);
641