Commit | Line | Data |
---|---|---|
a9d70523 RW |
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> | |
f3f12532 | 17 | #include <linux/cpuidle.h> |
a9d70523 | 18 | #include <linux/syscalls.h> |
5a0e3ad6 | 19 | #include <linux/gfp.h> |
dd4c4f17 MG |
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> | |
6e5fdeed | 25 | #include <linux/export.h> |
dd4c4f17 | 26 | #include <linux/suspend.h> |
40dc166c | 27 | #include <linux/syscore_ops.h> |
443772d4 | 28 | #include <linux/ftrace.h> |
938cfed1 | 29 | #include <trace/events/power.h> |
52f5684c | 30 | #include <linux/compiler.h> |
a9d70523 RW |
31 | |
32 | #include "power.h" | |
33 | ||
d431cbc5 RW |
34 | static const char *pm_labels[] = { "mem", "standby", "freeze", }; |
35 | const char *pm_states[PM_SUSPEND_MAX]; | |
a9d70523 | 36 | |
2f55ac07 | 37 | static const struct platform_suspend_ops *suspend_ops; |
1f0b6386 | 38 | static const struct platform_freeze_ops *freeze_ops; |
a9d70523 | 39 | |
7e73c5ae ZR |
40 | static bool need_suspend_ops(suspend_state_t state) |
41 | { | |
7500d936 | 42 | return state > PM_SUSPEND_FREEZE; |
7e73c5ae ZR |
43 | } |
44 | ||
45 | static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head); | |
46 | static bool suspend_freeze_wake; | |
47 | ||
1f0b6386 RW |
48 | void freeze_set_ops(const struct platform_freeze_ops *ops) |
49 | { | |
50 | lock_system_sleep(); | |
51 | freeze_ops = ops; | |
52 | unlock_system_sleep(); | |
53 | } | |
54 | ||
7e73c5ae ZR |
55 | static void freeze_begin(void) |
56 | { | |
57 | suspend_freeze_wake = false; | |
58 | } | |
59 | ||
60 | static void freeze_enter(void) | |
61 | { | |
a6220fc1 | 62 | cpuidle_use_deepest_state(true); |
f3f12532 | 63 | cpuidle_resume(); |
7e73c5ae | 64 | wait_event(suspend_freeze_wait_head, suspend_freeze_wake); |
f3f12532 | 65 | cpuidle_pause(); |
a6220fc1 | 66 | cpuidle_use_deepest_state(false); |
7e73c5ae ZR |
67 | } |
68 | ||
69 | void freeze_wake(void) | |
70 | { | |
71 | suspend_freeze_wake = true; | |
72 | wake_up(&suspend_freeze_wait_head); | |
73 | } | |
74 | EXPORT_SYMBOL_GPL(freeze_wake); | |
75 | ||
43e8317b RW |
76 | static bool valid_state(suspend_state_t state) |
77 | { | |
78 | /* | |
79 | * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level | |
80 | * support and need to be valid to the low level | |
81 | * implementation, no valid callback implies that none are valid. | |
82 | */ | |
83 | return suspend_ops && suspend_ops->valid && suspend_ops->valid(state); | |
84 | } | |
85 | ||
0399d4db RW |
86 | /* |
87 | * If this is set, the "mem" label always corresponds to the deepest sleep state | |
88 | * available, the "standby" label corresponds to the second deepest sleep state | |
89 | * available (if any), and the "freeze" label corresponds to the remaining | |
90 | * available sleep state (if there is one). | |
91 | */ | |
92 | static bool relative_states; | |
93 | ||
94 | static int __init sleep_states_setup(char *str) | |
95 | { | |
96 | relative_states = !strncmp(str, "1", 1); | |
d431cbc5 | 97 | pm_states[PM_SUSPEND_FREEZE] = pm_labels[relative_states ? 0 : 2]; |
0399d4db RW |
98 | return 1; |
99 | } | |
100 | ||
101 | __setup("relative_sleep_states=", sleep_states_setup); | |
102 | ||
a9d70523 | 103 | /** |
55ae4519 RW |
104 | * suspend_set_ops - Set the global suspend method table. |
105 | * @ops: Suspend operations to use. | |
a9d70523 | 106 | */ |
2f55ac07 | 107 | void suspend_set_ops(const struct platform_suspend_ops *ops) |
a9d70523 | 108 | { |
43e8317b | 109 | suspend_state_t i; |
d431cbc5 | 110 | int j = 0; |
43e8317b | 111 | |
bcda53fa | 112 | lock_system_sleep(); |
43e8317b | 113 | |
a9d70523 | 114 | suspend_ops = ops; |
0399d4db | 115 | for (i = PM_SUSPEND_MEM; i >= PM_SUSPEND_STANDBY; i--) |
d431cbc5 RW |
116 | if (valid_state(i)) { |
117 | pm_states[i] = pm_labels[j++]; | |
118 | } else if (!relative_states) { | |
119 | pm_states[i] = NULL; | |
120 | j++; | |
121 | } | |
0399d4db | 122 | |
d431cbc5 | 123 | pm_states[PM_SUSPEND_FREEZE] = pm_labels[j]; |
43e8317b | 124 | |
bcda53fa | 125 | unlock_system_sleep(); |
a9d70523 | 126 | } |
a5e4fd87 | 127 | EXPORT_SYMBOL_GPL(suspend_set_ops); |
a9d70523 | 128 | |
a9d70523 | 129 | /** |
55ae4519 | 130 | * suspend_valid_only_mem - Generic memory-only valid callback. |
a9d70523 | 131 | * |
55ae4519 RW |
132 | * Platform drivers that implement mem suspend only and only need to check for |
133 | * that in their .valid() callback can use this instead of rolling their own | |
134 | * .valid() callback. | |
a9d70523 RW |
135 | */ |
136 | int suspend_valid_only_mem(suspend_state_t state) | |
137 | { | |
138 | return state == PM_SUSPEND_MEM; | |
139 | } | |
a5e4fd87 | 140 | EXPORT_SYMBOL_GPL(suspend_valid_only_mem); |
a9d70523 RW |
141 | |
142 | static int suspend_test(int level) | |
143 | { | |
144 | #ifdef CONFIG_PM_DEBUG | |
145 | if (pm_test_level == level) { | |
146 | printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n"); | |
147 | mdelay(5000); | |
148 | return 1; | |
149 | } | |
150 | #endif /* !CONFIG_PM_DEBUG */ | |
151 | return 0; | |
152 | } | |
153 | ||
154 | /** | |
55ae4519 | 155 | * suspend_prepare - Prepare for entering system sleep state. |
a9d70523 | 156 | * |
55ae4519 RW |
157 | * Common code run for every system sleep state that can be entered (except for |
158 | * hibernation). Run suspend notifiers, allocate the "suspend" console and | |
159 | * freeze processes. | |
a9d70523 | 160 | */ |
7e73c5ae | 161 | static int suspend_prepare(suspend_state_t state) |
a9d70523 RW |
162 | { |
163 | int error; | |
164 | ||
7e73c5ae | 165 | if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter)) |
a9d70523 RW |
166 | return -EPERM; |
167 | ||
168 | pm_prepare_console(); | |
169 | ||
170 | error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); | |
171 | if (error) | |
172 | goto Finish; | |
173 | ||
bb3632c6 | 174 | trace_suspend_resume(TPS("freeze_processes"), 0, true); |
a9d70523 | 175 | error = suspend_freeze_processes(); |
bb3632c6 | 176 | trace_suspend_resume(TPS("freeze_processes"), 0, false); |
03afed8b | 177 | if (!error) |
a9d70523 RW |
178 | return 0; |
179 | ||
03afed8b TH |
180 | suspend_stats.failed_freeze++; |
181 | dpm_save_failed_step(SUSPEND_FREEZE); | |
a9d70523 RW |
182 | Finish: |
183 | pm_notifier_call_chain(PM_POST_SUSPEND); | |
184 | pm_restore_console(); | |
185 | return error; | |
186 | } | |
187 | ||
188 | /* default implementation */ | |
52f5684c | 189 | void __weak arch_suspend_disable_irqs(void) |
a9d70523 RW |
190 | { |
191 | local_irq_disable(); | |
192 | } | |
193 | ||
194 | /* default implementation */ | |
52f5684c | 195 | void __weak arch_suspend_enable_irqs(void) |
a9d70523 RW |
196 | { |
197 | local_irq_enable(); | |
198 | } | |
199 | ||
200 | /** | |
55ae4519 RW |
201 | * suspend_enter - Make the system enter the given sleep state. |
202 | * @state: System sleep state to enter. | |
203 | * @wakeup: Returns information that the sleep state should not be re-entered. | |
a9d70523 | 204 | * |
3b5fe852 | 205 | * This function should be called after devices have been suspended. |
a9d70523 | 206 | */ |
3b5fe852 | 207 | static int suspend_enter(suspend_state_t state, bool *wakeup) |
a9d70523 RW |
208 | { |
209 | int error; | |
210 | ||
7e73c5ae | 211 | if (need_suspend_ops(state) && suspend_ops->prepare) { |
a9d70523 RW |
212 | error = suspend_ops->prepare(); |
213 | if (error) | |
ce441011 | 214 | goto Platform_finish; |
a9d70523 RW |
215 | } |
216 | ||
cf579dfb | 217 | error = dpm_suspend_end(PMSG_SUSPEND); |
a9d70523 RW |
218 | if (error) { |
219 | printk(KERN_ERR "PM: Some devices failed to power down\n"); | |
ce441011 | 220 | goto Platform_finish; |
a9d70523 RW |
221 | } |
222 | ||
7e73c5ae | 223 | if (need_suspend_ops(state) && suspend_ops->prepare_late) { |
a9d70523 RW |
224 | error = suspend_ops->prepare_late(); |
225 | if (error) | |
ce441011 | 226 | goto Platform_wake; |
a9d70523 RW |
227 | } |
228 | ||
08605acc ZR |
229 | if (suspend_test(TEST_PLATFORM)) |
230 | goto Platform_wake; | |
231 | ||
7e73c5ae ZR |
232 | /* |
233 | * PM_SUSPEND_FREEZE equals | |
234 | * frozen processes + suspended devices + idle processors. | |
235 | * Thus we should invoke freeze_enter() soon after | |
236 | * all the devices are suspended. | |
237 | */ | |
238 | if (state == PM_SUSPEND_FREEZE) { | |
bb3632c6 | 239 | trace_suspend_resume(TPS("machine_suspend"), state, true); |
7e73c5ae | 240 | freeze_enter(); |
bb3632c6 | 241 | trace_suspend_resume(TPS("machine_suspend"), state, false); |
7e73c5ae ZR |
242 | goto Platform_wake; |
243 | } | |
244 | ||
3831261e | 245 | ftrace_stop(); |
a9d70523 RW |
246 | error = disable_nonboot_cpus(); |
247 | if (error || suspend_test(TEST_CPUS)) | |
248 | goto Enable_cpus; | |
249 | ||
250 | arch_suspend_disable_irqs(); | |
251 | BUG_ON(!irqs_disabled()); | |
252 | ||
2e711c04 | 253 | error = syscore_suspend(); |
a9d70523 | 254 | if (!error) { |
3b5fe852 MH |
255 | *wakeup = pm_wakeup_pending(); |
256 | if (!(suspend_test(TEST_CORE) || *wakeup)) { | |
bb3632c6 TB |
257 | trace_suspend_resume(TPS("machine_suspend"), |
258 | state, true); | |
a9d70523 | 259 | error = suspend_ops->enter(state); |
bb3632c6 TB |
260 | trace_suspend_resume(TPS("machine_suspend"), |
261 | state, false); | |
c125e96f RW |
262 | events_check_enabled = false; |
263 | } | |
40dc166c | 264 | syscore_resume(); |
a9d70523 RW |
265 | } |
266 | ||
267 | arch_suspend_enable_irqs(); | |
268 | BUG_ON(irqs_disabled()); | |
269 | ||
270 | Enable_cpus: | |
271 | enable_nonboot_cpus(); | |
3831261e | 272 | ftrace_start(); |
a9d70523 RW |
273 | |
274 | Platform_wake: | |
7e73c5ae | 275 | if (need_suspend_ops(state) && suspend_ops->wake) |
a9d70523 RW |
276 | suspend_ops->wake(); |
277 | ||
cf579dfb | 278 | dpm_resume_start(PMSG_RESUME); |
a9d70523 | 279 | |
ce441011 | 280 | Platform_finish: |
7e73c5ae | 281 | if (need_suspend_ops(state) && suspend_ops->finish) |
a9d70523 RW |
282 | suspend_ops->finish(); |
283 | ||
284 | return error; | |
285 | } | |
286 | ||
287 | /** | |
55ae4519 RW |
288 | * suspend_devices_and_enter - Suspend devices and enter system sleep state. |
289 | * @state: System sleep state to enter. | |
a9d70523 RW |
290 | */ |
291 | int suspend_devices_and_enter(suspend_state_t state) | |
292 | { | |
293 | int error; | |
3b5fe852 | 294 | bool wakeup = false; |
a9d70523 | 295 | |
7e73c5ae | 296 | if (need_suspend_ops(state) && !suspend_ops) |
a9d70523 RW |
297 | return -ENOSYS; |
298 | ||
7e73c5ae | 299 | if (need_suspend_ops(state) && suspend_ops->begin) { |
a9d70523 RW |
300 | error = suspend_ops->begin(state); |
301 | if (error) | |
302 | goto Close; | |
653a3538 | 303 | } else if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->begin) { |
1f0b6386 RW |
304 | error = freeze_ops->begin(); |
305 | if (error) | |
306 | goto Close; | |
a9d70523 RW |
307 | } |
308 | suspend_console(); | |
309 | suspend_test_start(); | |
310 | error = dpm_suspend_start(PMSG_SUSPEND); | |
311 | if (error) { | |
9350de06 | 312 | pr_err("PM: Some devices failed to suspend, or early wake event detected\n"); |
a9d70523 RW |
313 | goto Recover_platform; |
314 | } | |
315 | suspend_test_finish("suspend devices"); | |
316 | if (suspend_test(TEST_DEVICES)) | |
317 | goto Recover_platform; | |
318 | ||
3b5fe852 MH |
319 | do { |
320 | error = suspend_enter(state, &wakeup); | |
7e73c5ae | 321 | } while (!error && !wakeup && need_suspend_ops(state) |
3b5fe852 | 322 | && suspend_ops->suspend_again && suspend_ops->suspend_again()); |
a9d70523 RW |
323 | |
324 | Resume_devices: | |
325 | suspend_test_start(); | |
326 | dpm_resume_end(PMSG_RESUME); | |
327 | suspend_test_finish("resume devices"); | |
328 | resume_console(); | |
329 | Close: | |
7e73c5ae | 330 | if (need_suspend_ops(state) && suspend_ops->end) |
a9d70523 | 331 | suspend_ops->end(); |
653a3538 | 332 | else if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->end) |
1f0b6386 RW |
333 | freeze_ops->end(); |
334 | ||
a9d70523 RW |
335 | return error; |
336 | ||
337 | Recover_platform: | |
7e73c5ae | 338 | if (need_suspend_ops(state) && suspend_ops->recover) |
a9d70523 RW |
339 | suspend_ops->recover(); |
340 | goto Resume_devices; | |
341 | } | |
342 | ||
343 | /** | |
55ae4519 | 344 | * suspend_finish - Clean up before finishing the suspend sequence. |
a9d70523 | 345 | * |
55ae4519 RW |
346 | * Call platform code to clean up, restart processes, and free the console that |
347 | * we've allocated. This routine is not called for hibernation. | |
a9d70523 RW |
348 | */ |
349 | static void suspend_finish(void) | |
350 | { | |
351 | suspend_thaw_processes(); | |
a9d70523 RW |
352 | pm_notifier_call_chain(PM_POST_SUSPEND); |
353 | pm_restore_console(); | |
354 | } | |
355 | ||
356 | /** | |
55ae4519 RW |
357 | * enter_state - Do common work needed to enter system sleep state. |
358 | * @state: System sleep state to enter. | |
a9d70523 | 359 | * |
55ae4519 RW |
360 | * Make sure that no one else is trying to put the system into a sleep state. |
361 | * Fail if that's not the case. Otherwise, prepare for system suspend, make the | |
362 | * system enter the given sleep state and clean up after wakeup. | |
a9d70523 | 363 | */ |
93e1ee43 | 364 | static int enter_state(suspend_state_t state) |
a9d70523 RW |
365 | { |
366 | int error; | |
367 | ||
bb3632c6 | 368 | trace_suspend_resume(TPS("suspend_enter"), state, true); |
43e8317b RW |
369 | if (state == PM_SUSPEND_FREEZE) { |
370 | #ifdef CONFIG_PM_DEBUG | |
371 | if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) { | |
372 | pr_warning("PM: Unsupported test mode for freeze state," | |
373 | "please choose none/freezer/devices/platform.\n"); | |
374 | return -EAGAIN; | |
375 | } | |
376 | #endif | |
377 | } else if (!valid_state(state)) { | |
378 | return -EINVAL; | |
379 | } | |
a9d70523 RW |
380 | if (!mutex_trylock(&pm_mutex)) |
381 | return -EBUSY; | |
382 | ||
7e73c5ae ZR |
383 | if (state == PM_SUSPEND_FREEZE) |
384 | freeze_begin(); | |
385 | ||
bb3632c6 | 386 | trace_suspend_resume(TPS("sync_filesystems"), 0, true); |
a9d70523 RW |
387 | printk(KERN_INFO "PM: Syncing filesystems ... "); |
388 | sys_sync(); | |
389 | printk("done.\n"); | |
bb3632c6 | 390 | trace_suspend_resume(TPS("sync_filesystems"), 0, false); |
a9d70523 | 391 | |
d431cbc5 | 392 | pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); |
7e73c5ae | 393 | error = suspend_prepare(state); |
a9d70523 RW |
394 | if (error) |
395 | goto Unlock; | |
396 | ||
397 | if (suspend_test(TEST_FREEZER)) | |
398 | goto Finish; | |
399 | ||
bb3632c6 | 400 | trace_suspend_resume(TPS("suspend_enter"), state, false); |
d431cbc5 | 401 | pr_debug("PM: Entering %s sleep\n", pm_states[state]); |
87186475 | 402 | pm_restrict_gfp_mask(); |
a9d70523 | 403 | error = suspend_devices_and_enter(state); |
87186475 | 404 | pm_restore_gfp_mask(); |
a9d70523 RW |
405 | |
406 | Finish: | |
407 | pr_debug("PM: Finishing wakeup.\n"); | |
408 | suspend_finish(); | |
409 | Unlock: | |
410 | mutex_unlock(&pm_mutex); | |
411 | return error; | |
412 | } | |
413 | ||
414 | /** | |
55ae4519 RW |
415 | * pm_suspend - Externally visible function for suspending the system. |
416 | * @state: System sleep state to enter. | |
a9d70523 | 417 | * |
55ae4519 RW |
418 | * Check if the value of @state represents one of the supported states, |
419 | * execute enter_state() and update system suspend statistics. | |
a9d70523 RW |
420 | */ |
421 | int pm_suspend(suspend_state_t state) | |
422 | { | |
bc25cf50 RW |
423 | int error; |
424 | ||
425 | if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX) | |
426 | return -EINVAL; | |
427 | ||
428 | error = enter_state(state); | |
429 | if (error) { | |
430 | suspend_stats.fail++; | |
431 | dpm_save_failed_errno(error); | |
432 | } else { | |
433 | suspend_stats.success++; | |
2a77c46d | 434 | } |
bc25cf50 | 435 | return error; |
a9d70523 RW |
436 | } |
437 | EXPORT_SYMBOL(pm_suspend); |