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