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