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