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