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