Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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