Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * kernel/power/suspend.c - Suspend to RAM and standby functionality. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Lab |
| 6 | * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/string.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/console.h> |
| 16 | #include <linux/cpu.h> |
Rafael J. Wysocki | f3f1253 | 2014-04-20 23:43:01 +0200 | [diff] [blame] | 17 | #include <linux/cpuidle.h> |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 18 | #include <linux/syscalls.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> |
Srivatsa S. Bhat | 443772d4 | 2012-06-16 15:30:45 +0200 | [diff] [blame] | 28 | #include <linux/ftrace.h> |
Jean Pihet | 938cfed | 2011-01-05 19:49:01 +0100 | [diff] [blame] | 29 | #include <trace/events/power.h> |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 30 | #include <linux/compiler.h> |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 31 | |
| 32 | #include "power.h" |
| 33 | |
Rafael J. Wysocki | 27ddcc6 | 2014-05-26 13:40:47 +0200 | [diff] [blame] | 34 | struct pm_sleep_state pm_states[PM_SUSPEND_MAX] = { |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 35 | [PM_SUSPEND_FREEZE] = { .label = "freeze", .state = PM_SUSPEND_FREEZE }, |
| 36 | [PM_SUSPEND_STANDBY] = { .label = "standby", }, |
| 37 | [PM_SUSPEND_MEM] = { .label = "mem", }, |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 40 | static const struct platform_suspend_ops *suspend_ops; |
Rafael J. Wysocki | 1f0b638 | 2014-05-15 23:29:57 +0200 | [diff] [blame] | 41 | static const struct platform_freeze_ops *freeze_ops; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 42 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 43 | static bool need_suspend_ops(suspend_state_t state) |
| 44 | { |
Viresh Kumar | 7500d93 | 2014-03-10 19:31:51 +0530 | [diff] [blame] | 45 | return state > PM_SUSPEND_FREEZE; |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head); |
| 49 | static bool suspend_freeze_wake; |
| 50 | |
Rafael J. Wysocki | 1f0b638 | 2014-05-15 23:29:57 +0200 | [diff] [blame] | 51 | void freeze_set_ops(const struct platform_freeze_ops *ops) |
| 52 | { |
| 53 | lock_system_sleep(); |
| 54 | freeze_ops = ops; |
| 55 | unlock_system_sleep(); |
| 56 | } |
| 57 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 58 | static void freeze_begin(void) |
| 59 | { |
| 60 | suspend_freeze_wake = false; |
| 61 | } |
| 62 | |
| 63 | static void freeze_enter(void) |
| 64 | { |
Rafael J. Wysocki | a6220fc | 2014-05-05 00:51:54 +0200 | [diff] [blame] | 65 | cpuidle_use_deepest_state(true); |
Rafael J. Wysocki | f3f1253 | 2014-04-20 23:43:01 +0200 | [diff] [blame] | 66 | cpuidle_resume(); |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 67 | wait_event(suspend_freeze_wait_head, suspend_freeze_wake); |
Rafael J. Wysocki | f3f1253 | 2014-04-20 23:43:01 +0200 | [diff] [blame] | 68 | cpuidle_pause(); |
Rafael J. Wysocki | a6220fc | 2014-05-05 00:51:54 +0200 | [diff] [blame] | 69 | cpuidle_use_deepest_state(false); |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void freeze_wake(void) |
| 73 | { |
| 74 | suspend_freeze_wake = true; |
| 75 | wake_up(&suspend_freeze_wait_head); |
| 76 | } |
| 77 | EXPORT_SYMBOL_GPL(freeze_wake); |
| 78 | |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 79 | static bool valid_state(suspend_state_t state) |
| 80 | { |
| 81 | /* |
| 82 | * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level |
| 83 | * support and need to be valid to the low level |
| 84 | * implementation, no valid callback implies that none are valid. |
| 85 | */ |
| 86 | return suspend_ops && suspend_ops->valid && suspend_ops->valid(state); |
| 87 | } |
| 88 | |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 89 | /* |
| 90 | * If this is set, the "mem" label always corresponds to the deepest sleep state |
| 91 | * available, the "standby" label corresponds to the second deepest sleep state |
| 92 | * available (if any), and the "freeze" label corresponds to the remaining |
| 93 | * available sleep state (if there is one). |
| 94 | */ |
| 95 | static bool relative_states; |
| 96 | |
| 97 | static int __init sleep_states_setup(char *str) |
| 98 | { |
| 99 | relative_states = !strncmp(str, "1", 1); |
| 100 | if (relative_states) { |
| 101 | pm_states[PM_SUSPEND_MEM].state = PM_SUSPEND_FREEZE; |
| 102 | pm_states[PM_SUSPEND_FREEZE].state = 0; |
| 103 | } |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | __setup("relative_sleep_states=", sleep_states_setup); |
| 108 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 109 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 110 | * suspend_set_ops - Set the global suspend method table. |
| 111 | * @ops: Suspend operations to use. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 112 | */ |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 113 | void suspend_set_ops(const struct platform_suspend_ops *ops) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 114 | { |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 115 | suspend_state_t i; |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 116 | int j = PM_SUSPEND_MAX - 1; |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 117 | |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 118 | lock_system_sleep(); |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 119 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 120 | suspend_ops = ops; |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 121 | for (i = PM_SUSPEND_MEM; i >= PM_SUSPEND_STANDBY; i--) |
| 122 | if (valid_state(i)) |
| 123 | pm_states[j--].state = i; |
| 124 | else if (!relative_states) |
| 125 | pm_states[j--].state = 0; |
| 126 | |
| 127 | pm_states[j--].state = PM_SUSPEND_FREEZE; |
| 128 | while (j >= PM_SUSPEND_MIN) |
| 129 | pm_states[j--].state = 0; |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 130 | |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 131 | unlock_system_sleep(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 132 | } |
Kevin Hilman | a5e4fd8 | 2011-06-27 01:01:07 +0200 | [diff] [blame] | 133 | EXPORT_SYMBOL_GPL(suspend_set_ops); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 134 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 135 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 136 | * suspend_valid_only_mem - Generic memory-only valid callback. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 137 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 138 | * Platform drivers that implement mem suspend only and only need to check for |
| 139 | * that in their .valid() callback can use this instead of rolling their own |
| 140 | * .valid() callback. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 141 | */ |
| 142 | int suspend_valid_only_mem(suspend_state_t state) |
| 143 | { |
| 144 | return state == PM_SUSPEND_MEM; |
| 145 | } |
Kevin Hilman | a5e4fd8 | 2011-06-27 01:01:07 +0200 | [diff] [blame] | 146 | EXPORT_SYMBOL_GPL(suspend_valid_only_mem); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 147 | |
| 148 | static int suspend_test(int level) |
| 149 | { |
| 150 | #ifdef CONFIG_PM_DEBUG |
| 151 | if (pm_test_level == level) { |
| 152 | printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n"); |
| 153 | mdelay(5000); |
| 154 | return 1; |
| 155 | } |
| 156 | #endif /* !CONFIG_PM_DEBUG */ |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 161 | * suspend_prepare - Prepare for entering system sleep state. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 162 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 163 | * Common code run for every system sleep state that can be entered (except for |
| 164 | * hibernation). Run suspend notifiers, allocate the "suspend" console and |
| 165 | * freeze processes. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 166 | */ |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 167 | static int suspend_prepare(suspend_state_t state) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 168 | { |
| 169 | int error; |
| 170 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 171 | if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter)) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 172 | return -EPERM; |
| 173 | |
| 174 | pm_prepare_console(); |
| 175 | |
| 176 | error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); |
| 177 | if (error) |
| 178 | goto Finish; |
| 179 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 180 | trace_suspend_resume(TPS("freeze_processes"), 0, true); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 181 | error = suspend_freeze_processes(); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 182 | trace_suspend_resume(TPS("freeze_processes"), 0, false); |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 183 | if (!error) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 184 | return 0; |
| 185 | |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 186 | suspend_stats.failed_freeze++; |
| 187 | dpm_save_failed_step(SUSPEND_FREEZE); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 188 | Finish: |
| 189 | pm_notifier_call_chain(PM_POST_SUSPEND); |
| 190 | pm_restore_console(); |
| 191 | return error; |
| 192 | } |
| 193 | |
| 194 | /* default implementation */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 195 | void __weak arch_suspend_disable_irqs(void) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 196 | { |
| 197 | local_irq_disable(); |
| 198 | } |
| 199 | |
| 200 | /* default implementation */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 201 | void __weak arch_suspend_enable_irqs(void) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 202 | { |
| 203 | local_irq_enable(); |
| 204 | } |
| 205 | |
| 206 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 207 | * suspend_enter - Make the system enter the given sleep state. |
| 208 | * @state: System sleep state to enter. |
| 209 | * @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] | 210 | * |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 211 | * This function should be called after devices have been suspended. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 212 | */ |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 213 | static int suspend_enter(suspend_state_t state, bool *wakeup) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 214 | { |
| 215 | int error; |
| 216 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 217 | if (need_suspend_ops(state) && suspend_ops->prepare) { |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 218 | error = suspend_ops->prepare(); |
| 219 | if (error) |
Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 220 | goto Platform_finish; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 223 | error = dpm_suspend_end(PMSG_SUSPEND); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 224 | if (error) { |
| 225 | printk(KERN_ERR "PM: Some devices failed to power down\n"); |
Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 226 | goto Platform_finish; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 229 | if (need_suspend_ops(state) && suspend_ops->prepare_late) { |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 230 | error = suspend_ops->prepare_late(); |
| 231 | if (error) |
Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 232 | goto Platform_wake; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Zhang Rui | 08605ac | 2013-03-27 03:36:10 +0000 | [diff] [blame] | 235 | if (suspend_test(TEST_PLATFORM)) |
| 236 | goto Platform_wake; |
| 237 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 238 | /* |
| 239 | * PM_SUSPEND_FREEZE equals |
| 240 | * frozen processes + suspended devices + idle processors. |
| 241 | * Thus we should invoke freeze_enter() soon after |
| 242 | * all the devices are suspended. |
| 243 | */ |
| 244 | if (state == PM_SUSPEND_FREEZE) { |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 245 | trace_suspend_resume(TPS("machine_suspend"), state, true); |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 246 | freeze_enter(); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 247 | trace_suspend_resume(TPS("machine_suspend"), state, false); |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 248 | goto Platform_wake; |
| 249 | } |
| 250 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 251 | error = disable_nonboot_cpus(); |
| 252 | if (error || suspend_test(TEST_CPUS)) |
| 253 | goto Enable_cpus; |
| 254 | |
| 255 | arch_suspend_disable_irqs(); |
| 256 | BUG_ON(!irqs_disabled()); |
| 257 | |
Rafael J. Wysocki | 2e711c0 | 2011-04-26 19:15:07 +0200 | [diff] [blame] | 258 | error = syscore_suspend(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 259 | if (!error) { |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 260 | *wakeup = pm_wakeup_pending(); |
| 261 | if (!(suspend_test(TEST_CORE) || *wakeup)) { |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 262 | trace_suspend_resume(TPS("machine_suspend"), |
| 263 | state, true); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 264 | error = suspend_ops->enter(state); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 265 | trace_suspend_resume(TPS("machine_suspend"), |
| 266 | state, false); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 267 | events_check_enabled = false; |
| 268 | } |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 269 | syscore_resume(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | arch_suspend_enable_irqs(); |
| 273 | BUG_ON(irqs_disabled()); |
| 274 | |
| 275 | Enable_cpus: |
| 276 | enable_nonboot_cpus(); |
| 277 | |
| 278 | Platform_wake: |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 279 | if (need_suspend_ops(state) && suspend_ops->wake) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 280 | suspend_ops->wake(); |
| 281 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 282 | dpm_resume_start(PMSG_RESUME); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 283 | |
Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 284 | Platform_finish: |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 285 | if (need_suspend_ops(state) && suspend_ops->finish) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 286 | suspend_ops->finish(); |
| 287 | |
| 288 | return error; |
| 289 | } |
| 290 | |
| 291 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 292 | * suspend_devices_and_enter - Suspend devices and enter system sleep state. |
| 293 | * @state: System sleep state to enter. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 294 | */ |
| 295 | int suspend_devices_and_enter(suspend_state_t state) |
| 296 | { |
| 297 | int error; |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 298 | bool wakeup = false; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 299 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 300 | if (need_suspend_ops(state) && !suspend_ops) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 301 | return -ENOSYS; |
| 302 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 303 | if (need_suspend_ops(state) && suspend_ops->begin) { |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 304 | error = suspend_ops->begin(state); |
| 305 | if (error) |
| 306 | goto Close; |
Zhang Rui | 653a353 | 2014-07-15 14:26:13 +0200 | [diff] [blame] | 307 | } else if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->begin) { |
Rafael J. Wysocki | 1f0b638 | 2014-05-15 23:29:57 +0200 | [diff] [blame] | 308 | error = freeze_ops->begin(); |
| 309 | if (error) |
| 310 | goto Close; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 311 | } |
| 312 | suspend_console(); |
| 313 | suspend_test_start(); |
| 314 | error = dpm_suspend_start(PMSG_SUSPEND); |
| 315 | if (error) { |
Bernie Thompson | 9350de06 | 2013-06-01 00:47:43 +0000 | [diff] [blame] | 316 | pr_err("PM: Some devices failed to suspend, or early wake event detected\n"); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 317 | goto Recover_platform; |
| 318 | } |
| 319 | suspend_test_finish("suspend devices"); |
| 320 | if (suspend_test(TEST_DEVICES)) |
| 321 | goto Recover_platform; |
| 322 | |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 323 | do { |
| 324 | error = suspend_enter(state, &wakeup); |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 325 | } while (!error && !wakeup && need_suspend_ops(state) |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 326 | && suspend_ops->suspend_again && suspend_ops->suspend_again()); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 327 | |
| 328 | Resume_devices: |
| 329 | suspend_test_start(); |
| 330 | dpm_resume_end(PMSG_RESUME); |
| 331 | suspend_test_finish("resume devices"); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 332 | resume_console(); |
| 333 | Close: |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 334 | if (need_suspend_ops(state) && suspend_ops->end) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 335 | suspend_ops->end(); |
Zhang Rui | 653a353 | 2014-07-15 14:26:13 +0200 | [diff] [blame] | 336 | else if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->end) |
Rafael J. Wysocki | 1f0b638 | 2014-05-15 23:29:57 +0200 | [diff] [blame] | 337 | freeze_ops->end(); |
| 338 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 339 | return error; |
| 340 | |
| 341 | Recover_platform: |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 342 | if (need_suspend_ops(state) && suspend_ops->recover) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 343 | suspend_ops->recover(); |
| 344 | goto Resume_devices; |
| 345 | } |
| 346 | |
| 347 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 348 | * suspend_finish - Clean up before finishing the suspend sequence. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 349 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 350 | * Call platform code to clean up, restart processes, and free the console that |
| 351 | * we've allocated. This routine is not called for hibernation. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 352 | */ |
| 353 | static void suspend_finish(void) |
| 354 | { |
| 355 | suspend_thaw_processes(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 356 | pm_notifier_call_chain(PM_POST_SUSPEND); |
| 357 | pm_restore_console(); |
| 358 | } |
| 359 | |
| 360 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 361 | * enter_state - Do common work needed to enter system sleep state. |
| 362 | * @state: System sleep state to enter. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 363 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 364 | * Make sure that no one else is trying to put the system into a sleep state. |
| 365 | * Fail if that's not the case. Otherwise, prepare for system suspend, make the |
| 366 | * system enter the given sleep state and clean up after wakeup. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 367 | */ |
Rafael J. Wysocki | 93e1ee4 | 2012-02-13 16:29:24 +0100 | [diff] [blame] | 368 | static int enter_state(suspend_state_t state) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 369 | { |
| 370 | int error; |
| 371 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 372 | trace_suspend_resume(TPS("suspend_enter"), state, true); |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 373 | if (state == PM_SUSPEND_FREEZE) { |
| 374 | #ifdef CONFIG_PM_DEBUG |
| 375 | if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) { |
| 376 | pr_warning("PM: Unsupported test mode for freeze state," |
| 377 | "please choose none/freezer/devices/platform.\n"); |
| 378 | return -EAGAIN; |
| 379 | } |
| 380 | #endif |
| 381 | } else if (!valid_state(state)) { |
| 382 | return -EINVAL; |
| 383 | } |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 384 | if (!mutex_trylock(&pm_mutex)) |
| 385 | return -EBUSY; |
| 386 | |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 387 | if (state == PM_SUSPEND_FREEZE) |
| 388 | freeze_begin(); |
| 389 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 390 | trace_suspend_resume(TPS("sync_filesystems"), 0, true); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 391 | printk(KERN_INFO "PM: Syncing filesystems ... "); |
| 392 | sys_sync(); |
| 393 | printk("done.\n"); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 394 | trace_suspend_resume(TPS("sync_filesystems"), 0, false); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 395 | |
Rafael J. Wysocki | 27ddcc6 | 2014-05-26 13:40:47 +0200 | [diff] [blame] | 396 | pr_debug("PM: Preparing system for %s sleep\n", pm_states[state].label); |
Zhang Rui | 7e73c5a | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 397 | error = suspend_prepare(state); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 398 | if (error) |
| 399 | goto Unlock; |
| 400 | |
| 401 | if (suspend_test(TEST_FREEZER)) |
| 402 | goto Finish; |
| 403 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 404 | trace_suspend_resume(TPS("suspend_enter"), state, false); |
Rafael J. Wysocki | 27ddcc6 | 2014-05-26 13:40:47 +0200 | [diff] [blame] | 405 | pr_debug("PM: Entering %s sleep\n", pm_states[state].label); |
Rafael J. Wysocki | 8718647 | 2011-05-10 21:09:53 +0200 | [diff] [blame] | 406 | pm_restrict_gfp_mask(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 407 | error = suspend_devices_and_enter(state); |
Rafael J. Wysocki | 8718647 | 2011-05-10 21:09:53 +0200 | [diff] [blame] | 408 | pm_restore_gfp_mask(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 409 | |
| 410 | Finish: |
| 411 | pr_debug("PM: Finishing wakeup.\n"); |
| 412 | suspend_finish(); |
| 413 | Unlock: |
| 414 | mutex_unlock(&pm_mutex); |
| 415 | return error; |
| 416 | } |
| 417 | |
| 418 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 419 | * pm_suspend - Externally visible function for suspending the system. |
| 420 | * @state: System sleep state to enter. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 421 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 422 | * Check if the value of @state represents one of the supported states, |
| 423 | * execute enter_state() and update system suspend statistics. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 424 | */ |
| 425 | int pm_suspend(suspend_state_t state) |
| 426 | { |
Rafael J. Wysocki | bc25cf5 | 2012-02-13 16:29:33 +0100 | [diff] [blame] | 427 | int error; |
| 428 | |
| 429 | if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | error = enter_state(state); |
| 433 | if (error) { |
| 434 | suspend_stats.fail++; |
| 435 | dpm_save_failed_errno(error); |
| 436 | } else { |
| 437 | suspend_stats.success++; |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 438 | } |
Rafael J. Wysocki | bc25cf5 | 2012-02-13 16:29:33 +0100 | [diff] [blame] | 439 | return error; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 440 | } |
| 441 | EXPORT_SYMBOL(pm_suspend); |