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