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