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