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> |
1d4a9c17 | 31 | #include <linux/moduleparam.h> |
a9d70523 RW |
32 | |
33 | #include "power.h" | |
34 | ||
406e7938 RW |
35 | const char * const pm_labels[] = { |
36 | [PM_SUSPEND_FREEZE] = "freeze", | |
37 | [PM_SUSPEND_STANDBY] = "standby", | |
38 | [PM_SUSPEND_MEM] = "mem", | |
39 | }; | |
d431cbc5 | 40 | const char *pm_states[PM_SUSPEND_MAX]; |
406e7938 RW |
41 | static const char * const mem_sleep_labels[] = { |
42 | [PM_SUSPEND_FREEZE] = "s2idle", | |
43 | [PM_SUSPEND_STANDBY] = "shallow", | |
44 | [PM_SUSPEND_MEM] = "deep", | |
45 | }; | |
46 | const char *mem_sleep_states[PM_SUSPEND_MAX]; | |
47 | ||
48 | suspend_state_t mem_sleep_current = PM_SUSPEND_FREEZE; | |
e326ce01 | 49 | static suspend_state_t mem_sleep_default = PM_SUSPEND_MEM; |
a9d70523 | 50 | |
ef25ba04 RW |
51 | unsigned int pm_suspend_global_flags; |
52 | EXPORT_SYMBOL_GPL(pm_suspend_global_flags); | |
53 | ||
2f55ac07 | 54 | static const struct platform_suspend_ops *suspend_ops; |
1f0b6386 | 55 | static const struct platform_freeze_ops *freeze_ops; |
7e73c5ae | 56 | static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head); |
38106313 RW |
57 | |
58 | enum freeze_state __read_mostly suspend_freeze_state; | |
59 | static DEFINE_SPINLOCK(suspend_freeze_lock); | |
7e73c5ae | 60 | |
1f0b6386 RW |
61 | void freeze_set_ops(const struct platform_freeze_ops *ops) |
62 | { | |
63 | lock_system_sleep(); | |
64 | freeze_ops = ops; | |
65 | unlock_system_sleep(); | |
66 | } | |
67 | ||
7e73c5ae ZR |
68 | static void freeze_begin(void) |
69 | { | |
38106313 | 70 | suspend_freeze_state = FREEZE_STATE_NONE; |
7e73c5ae ZR |
71 | } |
72 | ||
73 | static void freeze_enter(void) | |
74 | { | |
eed4d47e RW |
75 | trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_FREEZE, true); |
76 | ||
38106313 RW |
77 | spin_lock_irq(&suspend_freeze_lock); |
78 | if (pm_wakeup_pending()) | |
79 | goto out; | |
80 | ||
81 | suspend_freeze_state = FREEZE_STATE_ENTER; | |
82 | spin_unlock_irq(&suspend_freeze_lock); | |
83 | ||
84 | get_online_cpus(); | |
f3f12532 | 85 | cpuidle_resume(); |
38106313 RW |
86 | |
87 | /* Push all the CPUs into the idle loop. */ | |
88 | wake_up_all_idle_cpus(); | |
89 | pr_debug("PM: suspend-to-idle\n"); | |
90 | /* Make the current CPU wait so it can enter the idle loop too. */ | |
91 | wait_event(suspend_freeze_wait_head, | |
92 | suspend_freeze_state == FREEZE_STATE_WAKE); | |
93 | pr_debug("PM: resume from suspend-to-idle\n"); | |
94 | ||
f3f12532 | 95 | cpuidle_pause(); |
38106313 RW |
96 | put_online_cpus(); |
97 | ||
98 | spin_lock_irq(&suspend_freeze_lock); | |
99 | ||
100 | out: | |
101 | suspend_freeze_state = FREEZE_STATE_NONE; | |
102 | spin_unlock_irq(&suspend_freeze_lock); | |
eed4d47e RW |
103 | |
104 | trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_FREEZE, false); | |
105 | } | |
106 | ||
107 | static void s2idle_loop(void) | |
108 | { | |
109 | do { | |
110 | freeze_enter(); | |
111 | ||
112 | if (freeze_ops && freeze_ops->wake) | |
113 | freeze_ops->wake(); | |
114 | ||
115 | dpm_resume_noirq(PMSG_RESUME); | |
116 | if (freeze_ops && freeze_ops->sync) | |
117 | freeze_ops->sync(); | |
118 | ||
119 | if (pm_wakeup_pending()) | |
120 | break; | |
121 | ||
122 | pm_wakeup_clear(false); | |
123 | } while (!dpm_suspend_noirq(PMSG_SUSPEND)); | |
7e73c5ae ZR |
124 | } |
125 | ||
126 | void freeze_wake(void) | |
127 | { | |
38106313 RW |
128 | unsigned long flags; |
129 | ||
130 | spin_lock_irqsave(&suspend_freeze_lock, flags); | |
131 | if (suspend_freeze_state > FREEZE_STATE_NONE) { | |
132 | suspend_freeze_state = FREEZE_STATE_WAKE; | |
133 | wake_up(&suspend_freeze_wait_head); | |
134 | } | |
135 | spin_unlock_irqrestore(&suspend_freeze_lock, flags); | |
7e73c5ae ZR |
136 | } |
137 | EXPORT_SYMBOL_GPL(freeze_wake); | |
138 | ||
43e8317b RW |
139 | static bool valid_state(suspend_state_t state) |
140 | { | |
141 | /* | |
142 | * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level | |
143 | * support and need to be valid to the low level | |
144 | * implementation, no valid callback implies that none are valid. | |
145 | */ | |
146 | return suspend_ops && suspend_ops->valid && suspend_ops->valid(state); | |
147 | } | |
148 | ||
fa7fd6fa SH |
149 | void __init pm_states_init(void) |
150 | { | |
406e7938 RW |
151 | /* "mem" and "freeze" are always present in /sys/power/state. */ |
152 | pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM]; | |
153 | pm_states[PM_SUSPEND_FREEZE] = pm_labels[PM_SUSPEND_FREEZE]; | |
fa7fd6fa | 154 | /* |
406e7938 RW |
155 | * Suspend-to-idle should be supported even without any suspend_ops, |
156 | * initialize mem_sleep_states[] accordingly here. | |
fa7fd6fa | 157 | */ |
406e7938 | 158 | mem_sleep_states[PM_SUSPEND_FREEZE] = mem_sleep_labels[PM_SUSPEND_FREEZE]; |
fa7fd6fa SH |
159 | } |
160 | ||
406e7938 | 161 | static int __init mem_sleep_default_setup(char *str) |
0399d4db | 162 | { |
406e7938 RW |
163 | suspend_state_t state; |
164 | ||
165 | for (state = PM_SUSPEND_FREEZE; state <= PM_SUSPEND_MEM; state++) | |
166 | if (mem_sleep_labels[state] && | |
167 | !strcmp(str, mem_sleep_labels[state])) { | |
168 | mem_sleep_default = state; | |
169 | break; | |
170 | } | |
171 | ||
0399d4db RW |
172 | return 1; |
173 | } | |
406e7938 | 174 | __setup("mem_sleep_default=", mem_sleep_default_setup); |
0399d4db | 175 | |
a9d70523 | 176 | /** |
55ae4519 RW |
177 | * suspend_set_ops - Set the global suspend method table. |
178 | * @ops: Suspend operations to use. | |
a9d70523 | 179 | */ |
2f55ac07 | 180 | void suspend_set_ops(const struct platform_suspend_ops *ops) |
a9d70523 | 181 | { |
bcda53fa | 182 | lock_system_sleep(); |
43e8317b | 183 | |
a9d70523 | 184 | suspend_ops = ops; |
0399d4db | 185 | |
406e7938 RW |
186 | if (valid_state(PM_SUSPEND_STANDBY)) { |
187 | mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY]; | |
188 | pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY]; | |
189 | if (mem_sleep_default == PM_SUSPEND_STANDBY) | |
190 | mem_sleep_current = PM_SUSPEND_STANDBY; | |
191 | } | |
192 | if (valid_state(PM_SUSPEND_MEM)) { | |
193 | mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM]; | |
e326ce01 | 194 | if (mem_sleep_default == PM_SUSPEND_MEM) |
406e7938 RW |
195 | mem_sleep_current = PM_SUSPEND_MEM; |
196 | } | |
43e8317b | 197 | |
bcda53fa | 198 | unlock_system_sleep(); |
a9d70523 | 199 | } |
a5e4fd87 | 200 | EXPORT_SYMBOL_GPL(suspend_set_ops); |
a9d70523 | 201 | |
a9d70523 | 202 | /** |
55ae4519 | 203 | * suspend_valid_only_mem - Generic memory-only valid callback. |
a9d70523 | 204 | * |
55ae4519 RW |
205 | * Platform drivers that implement mem suspend only and only need to check for |
206 | * that in their .valid() callback can use this instead of rolling their own | |
207 | * .valid() callback. | |
a9d70523 RW |
208 | */ |
209 | int suspend_valid_only_mem(suspend_state_t state) | |
210 | { | |
211 | return state == PM_SUSPEND_MEM; | |
212 | } | |
a5e4fd87 | 213 | EXPORT_SYMBOL_GPL(suspend_valid_only_mem); |
a9d70523 | 214 | |
8490fdf9 RW |
215 | static bool sleep_state_supported(suspend_state_t state) |
216 | { | |
217 | return state == PM_SUSPEND_FREEZE || (suspend_ops && suspend_ops->enter); | |
218 | } | |
219 | ||
220 | static int platform_suspend_prepare(suspend_state_t state) | |
221 | { | |
222 | return state != PM_SUSPEND_FREEZE && suspend_ops->prepare ? | |
223 | suspend_ops->prepare() : 0; | |
224 | } | |
225 | ||
a8d46b9e RW |
226 | static int platform_suspend_prepare_late(suspend_state_t state) |
227 | { | |
403b9636 | 228 | return state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->prepare ? |
a8d46b9e RW |
229 | freeze_ops->prepare() : 0; |
230 | } | |
231 | ||
ebc3e41e | 232 | static int platform_suspend_prepare_noirq(suspend_state_t state) |
8490fdf9 RW |
233 | { |
234 | return state != PM_SUSPEND_FREEZE && suspend_ops->prepare_late ? | |
235 | suspend_ops->prepare_late() : 0; | |
236 | } | |
237 | ||
ebc3e41e | 238 | static void platform_resume_noirq(suspend_state_t state) |
8490fdf9 RW |
239 | { |
240 | if (state != PM_SUSPEND_FREEZE && suspend_ops->wake) | |
241 | suspend_ops->wake(); | |
242 | } | |
243 | ||
a8d46b9e RW |
244 | static void platform_resume_early(suspend_state_t state) |
245 | { | |
403b9636 | 246 | if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->restore) |
a8d46b9e RW |
247 | freeze_ops->restore(); |
248 | } | |
249 | ||
ebc3e41e | 250 | static void platform_resume_finish(suspend_state_t state) |
8490fdf9 RW |
251 | { |
252 | if (state != PM_SUSPEND_FREEZE && suspend_ops->finish) | |
253 | suspend_ops->finish(); | |
254 | } | |
255 | ||
256 | static int platform_suspend_begin(suspend_state_t state) | |
257 | { | |
258 | if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->begin) | |
259 | return freeze_ops->begin(); | |
fa7fd6fa | 260 | else if (suspend_ops && suspend_ops->begin) |
8490fdf9 RW |
261 | return suspend_ops->begin(state); |
262 | else | |
263 | return 0; | |
264 | } | |
265 | ||
ebc3e41e | 266 | static void platform_resume_end(suspend_state_t state) |
8490fdf9 RW |
267 | { |
268 | if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->end) | |
269 | freeze_ops->end(); | |
fa7fd6fa | 270 | else if (suspend_ops && suspend_ops->end) |
8490fdf9 RW |
271 | suspend_ops->end(); |
272 | } | |
273 | ||
ebc3e41e | 274 | static void platform_recover(suspend_state_t state) |
8490fdf9 RW |
275 | { |
276 | if (state != PM_SUSPEND_FREEZE && suspend_ops->recover) | |
277 | suspend_ops->recover(); | |
278 | } | |
279 | ||
280 | static bool platform_suspend_again(suspend_state_t state) | |
281 | { | |
282 | return state != PM_SUSPEND_FREEZE && suspend_ops->suspend_again ? | |
283 | suspend_ops->suspend_again() : false; | |
284 | } | |
285 | ||
1d4a9c17 BN |
286 | #ifdef CONFIG_PM_DEBUG |
287 | static unsigned int pm_test_delay = 5; | |
288 | module_param(pm_test_delay, uint, 0644); | |
289 | MODULE_PARM_DESC(pm_test_delay, | |
290 | "Number of seconds to wait before resuming from suspend test"); | |
291 | #endif | |
292 | ||
a9d70523 RW |
293 | static int suspend_test(int level) |
294 | { | |
295 | #ifdef CONFIG_PM_DEBUG | |
296 | if (pm_test_level == level) { | |
22e09b33 | 297 | pr_info("suspend debug: Waiting for %d second(s).\n", |
1d4a9c17 BN |
298 | pm_test_delay); |
299 | mdelay(pm_test_delay * 1000); | |
a9d70523 RW |
300 | return 1; |
301 | } | |
302 | #endif /* !CONFIG_PM_DEBUG */ | |
303 | return 0; | |
304 | } | |
305 | ||
306 | /** | |
55ae4519 | 307 | * suspend_prepare - Prepare for entering system sleep state. |
a9d70523 | 308 | * |
55ae4519 RW |
309 | * Common code run for every system sleep state that can be entered (except for |
310 | * hibernation). Run suspend notifiers, allocate the "suspend" console and | |
311 | * freeze processes. | |
a9d70523 | 312 | */ |
7e73c5ae | 313 | static int suspend_prepare(suspend_state_t state) |
a9d70523 | 314 | { |
ea00f4f4 | 315 | int error, nr_calls = 0; |
a9d70523 | 316 | |
8490fdf9 | 317 | if (!sleep_state_supported(state)) |
a9d70523 RW |
318 | return -EPERM; |
319 | ||
320 | pm_prepare_console(); | |
321 | ||
ea00f4f4 LW |
322 | error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls); |
323 | if (error) { | |
324 | nr_calls--; | |
a9d70523 | 325 | goto Finish; |
ea00f4f4 | 326 | } |
a9d70523 | 327 | |
bb3632c6 | 328 | trace_suspend_resume(TPS("freeze_processes"), 0, true); |
a9d70523 | 329 | error = suspend_freeze_processes(); |
bb3632c6 | 330 | trace_suspend_resume(TPS("freeze_processes"), 0, false); |
03afed8b | 331 | if (!error) |
a9d70523 RW |
332 | return 0; |
333 | ||
03afed8b TH |
334 | suspend_stats.failed_freeze++; |
335 | dpm_save_failed_step(SUSPEND_FREEZE); | |
a9d70523 | 336 | Finish: |
ea00f4f4 | 337 | __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL); |
a9d70523 RW |
338 | pm_restore_console(); |
339 | return error; | |
340 | } | |
341 | ||
342 | /* default implementation */ | |
52f5684c | 343 | void __weak arch_suspend_disable_irqs(void) |
a9d70523 RW |
344 | { |
345 | local_irq_disable(); | |
346 | } | |
347 | ||
348 | /* default implementation */ | |
52f5684c | 349 | void __weak arch_suspend_enable_irqs(void) |
a9d70523 RW |
350 | { |
351 | local_irq_enable(); | |
352 | } | |
353 | ||
354 | /** | |
55ae4519 RW |
355 | * suspend_enter - Make the system enter the given sleep state. |
356 | * @state: System sleep state to enter. | |
357 | * @wakeup: Returns information that the sleep state should not be re-entered. | |
a9d70523 | 358 | * |
3b5fe852 | 359 | * This function should be called after devices have been suspended. |
a9d70523 | 360 | */ |
3b5fe852 | 361 | static int suspend_enter(suspend_state_t state, bool *wakeup) |
a9d70523 RW |
362 | { |
363 | int error; | |
364 | ||
8490fdf9 RW |
365 | error = platform_suspend_prepare(state); |
366 | if (error) | |
367 | goto Platform_finish; | |
a9d70523 | 368 | |
2a8a8ce6 | 369 | error = dpm_suspend_late(PMSG_SUSPEND); |
a9d70523 | 370 | if (error) { |
22e09b33 | 371 | pr_err("PM: late suspend of devices failed\n"); |
ce441011 | 372 | goto Platform_finish; |
a9d70523 | 373 | } |
a8d46b9e RW |
374 | error = platform_suspend_prepare_late(state); |
375 | if (error) | |
376 | goto Devices_early_resume; | |
377 | ||
2a8a8ce6 RW |
378 | error = dpm_suspend_noirq(PMSG_SUSPEND); |
379 | if (error) { | |
22e09b33 | 380 | pr_err("PM: noirq suspend of devices failed\n"); |
a8d46b9e | 381 | goto Platform_early_resume; |
2a8a8ce6 | 382 | } |
ebc3e41e | 383 | error = platform_suspend_prepare_noirq(state); |
8490fdf9 RW |
384 | if (error) |
385 | goto Platform_wake; | |
a9d70523 | 386 | |
08605acc ZR |
387 | if (suspend_test(TEST_PLATFORM)) |
388 | goto Platform_wake; | |
389 | ||
7e73c5ae ZR |
390 | /* |
391 | * PM_SUSPEND_FREEZE equals | |
392 | * frozen processes + suspended devices + idle processors. | |
393 | * Thus we should invoke freeze_enter() soon after | |
394 | * all the devices are suspended. | |
395 | */ | |
396 | if (state == PM_SUSPEND_FREEZE) { | |
eed4d47e RW |
397 | s2idle_loop(); |
398 | goto Platform_early_resume; | |
7e73c5ae ZR |
399 | } |
400 | ||
a9d70523 RW |
401 | error = disable_nonboot_cpus(); |
402 | if (error || suspend_test(TEST_CPUS)) | |
403 | goto Enable_cpus; | |
404 | ||
405 | arch_suspend_disable_irqs(); | |
406 | BUG_ON(!irqs_disabled()); | |
407 | ||
2e711c04 | 408 | error = syscore_suspend(); |
a9d70523 | 409 | if (!error) { |
3b5fe852 MH |
410 | *wakeup = pm_wakeup_pending(); |
411 | if (!(suspend_test(TEST_CORE) || *wakeup)) { | |
bb3632c6 TB |
412 | trace_suspend_resume(TPS("machine_suspend"), |
413 | state, true); | |
a9d70523 | 414 | error = suspend_ops->enter(state); |
bb3632c6 TB |
415 | trace_suspend_resume(TPS("machine_suspend"), |
416 | state, false); | |
c125e96f | 417 | events_check_enabled = false; |
67176736 RK |
418 | } else if (*wakeup) { |
419 | error = -EBUSY; | |
c125e96f | 420 | } |
40dc166c | 421 | syscore_resume(); |
a9d70523 RW |
422 | } |
423 | ||
424 | arch_suspend_enable_irqs(); | |
425 | BUG_ON(irqs_disabled()); | |
426 | ||
427 | Enable_cpus: | |
428 | enable_nonboot_cpus(); | |
429 | ||
430 | Platform_wake: | |
ebc3e41e | 431 | platform_resume_noirq(state); |
2a8a8ce6 RW |
432 | dpm_resume_noirq(PMSG_RESUME); |
433 | ||
a8d46b9e RW |
434 | Platform_early_resume: |
435 | platform_resume_early(state); | |
436 | ||
2a8a8ce6 RW |
437 | Devices_early_resume: |
438 | dpm_resume_early(PMSG_RESUME); | |
a9d70523 | 439 | |
ce441011 | 440 | Platform_finish: |
ebc3e41e | 441 | platform_resume_finish(state); |
a9d70523 RW |
442 | return error; |
443 | } | |
444 | ||
445 | /** | |
55ae4519 RW |
446 | * suspend_devices_and_enter - Suspend devices and enter system sleep state. |
447 | * @state: System sleep state to enter. | |
a9d70523 RW |
448 | */ |
449 | int suspend_devices_and_enter(suspend_state_t state) | |
450 | { | |
451 | int error; | |
3b5fe852 | 452 | bool wakeup = false; |
a9d70523 | 453 | |
8490fdf9 | 454 | if (!sleep_state_supported(state)) |
a9d70523 RW |
455 | return -ENOSYS; |
456 | ||
8490fdf9 RW |
457 | error = platform_suspend_begin(state); |
458 | if (error) | |
459 | goto Close; | |
460 | ||
a9d70523 RW |
461 | suspend_console(); |
462 | suspend_test_start(); | |
463 | error = dpm_suspend_start(PMSG_SUSPEND); | |
464 | if (error) { | |
9350de06 | 465 | pr_err("PM: Some devices failed to suspend, or early wake event detected\n"); |
a9d70523 RW |
466 | goto Recover_platform; |
467 | } | |
468 | suspend_test_finish("suspend devices"); | |
469 | if (suspend_test(TEST_DEVICES)) | |
470 | goto Recover_platform; | |
471 | ||
3b5fe852 MH |
472 | do { |
473 | error = suspend_enter(state, &wakeup); | |
8490fdf9 | 474 | } while (!error && !wakeup && platform_suspend_again(state)); |
a9d70523 RW |
475 | |
476 | Resume_devices: | |
477 | suspend_test_start(); | |
478 | dpm_resume_end(PMSG_RESUME); | |
479 | suspend_test_finish("resume devices"); | |
0cadc702 | 480 | trace_suspend_resume(TPS("resume_console"), state, true); |
a9d70523 | 481 | resume_console(); |
0cadc702 | 482 | trace_suspend_resume(TPS("resume_console"), state, false); |
1f0b6386 | 483 | |
8490fdf9 | 484 | Close: |
ebc3e41e | 485 | platform_resume_end(state); |
a9d70523 RW |
486 | return error; |
487 | ||
488 | Recover_platform: | |
ebc3e41e | 489 | platform_recover(state); |
a9d70523 RW |
490 | goto Resume_devices; |
491 | } | |
492 | ||
493 | /** | |
55ae4519 | 494 | * suspend_finish - Clean up before finishing the suspend sequence. |
a9d70523 | 495 | * |
55ae4519 RW |
496 | * Call platform code to clean up, restart processes, and free the console that |
497 | * we've allocated. This routine is not called for hibernation. | |
a9d70523 RW |
498 | */ |
499 | static void suspend_finish(void) | |
500 | { | |
501 | suspend_thaw_processes(); | |
a9d70523 RW |
502 | pm_notifier_call_chain(PM_POST_SUSPEND); |
503 | pm_restore_console(); | |
504 | } | |
505 | ||
506 | /** | |
55ae4519 RW |
507 | * enter_state - Do common work needed to enter system sleep state. |
508 | * @state: System sleep state to enter. | |
a9d70523 | 509 | * |
55ae4519 RW |
510 | * Make sure that no one else is trying to put the system into a sleep state. |
511 | * Fail if that's not the case. Otherwise, prepare for system suspend, make the | |
512 | * system enter the given sleep state and clean up after wakeup. | |
a9d70523 | 513 | */ |
93e1ee43 | 514 | static int enter_state(suspend_state_t state) |
a9d70523 RW |
515 | { |
516 | int error; | |
517 | ||
bb3632c6 | 518 | trace_suspend_resume(TPS("suspend_enter"), state, true); |
43e8317b RW |
519 | if (state == PM_SUSPEND_FREEZE) { |
520 | #ifdef CONFIG_PM_DEBUG | |
521 | if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) { | |
a395d6a7 | 522 | pr_warn("PM: Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n"); |
43e8317b RW |
523 | return -EAGAIN; |
524 | } | |
525 | #endif | |
526 | } else if (!valid_state(state)) { | |
527 | return -EINVAL; | |
528 | } | |
a9d70523 RW |
529 | if (!mutex_trylock(&pm_mutex)) |
530 | return -EBUSY; | |
531 | ||
7e73c5ae ZR |
532 | if (state == PM_SUSPEND_FREEZE) |
533 | freeze_begin(); | |
534 | ||
2fd77fff | 535 | #ifndef CONFIG_SUSPEND_SKIP_SYNC |
bb3632c6 | 536 | trace_suspend_resume(TPS("sync_filesystems"), 0, true); |
1adb469b | 537 | pr_info("PM: Syncing filesystems ... "); |
a9d70523 | 538 | sys_sync(); |
1adb469b | 539 | pr_cont("done.\n"); |
bb3632c6 | 540 | trace_suspend_resume(TPS("sync_filesystems"), 0, false); |
2fd77fff | 541 | #endif |
a9d70523 | 542 | |
a9215045 | 543 | pr_debug("PM: Preparing system for sleep (%s)\n", pm_states[state]); |
ef25ba04 | 544 | pm_suspend_clear_flags(); |
7e73c5ae | 545 | error = suspend_prepare(state); |
a9d70523 RW |
546 | if (error) |
547 | goto Unlock; | |
548 | ||
549 | if (suspend_test(TEST_FREEZER)) | |
550 | goto Finish; | |
551 | ||
bb3632c6 | 552 | trace_suspend_resume(TPS("suspend_enter"), state, false); |
a9215045 | 553 | pr_debug("PM: Suspending system (%s)\n", pm_states[state]); |
87186475 | 554 | pm_restrict_gfp_mask(); |
a9d70523 | 555 | error = suspend_devices_and_enter(state); |
87186475 | 556 | pm_restore_gfp_mask(); |
a9d70523 RW |
557 | |
558 | Finish: | |
559 | pr_debug("PM: Finishing wakeup.\n"); | |
560 | suspend_finish(); | |
561 | Unlock: | |
562 | mutex_unlock(&pm_mutex); | |
563 | return error; | |
564 | } | |
565 | ||
566 | /** | |
55ae4519 RW |
567 | * pm_suspend - Externally visible function for suspending the system. |
568 | * @state: System sleep state to enter. | |
a9d70523 | 569 | * |
55ae4519 RW |
570 | * Check if the value of @state represents one of the supported states, |
571 | * execute enter_state() and update system suspend statistics. | |
a9d70523 RW |
572 | */ |
573 | int pm_suspend(suspend_state_t state) | |
574 | { | |
bc25cf50 RW |
575 | int error; |
576 | ||
577 | if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX) | |
578 | return -EINVAL; | |
579 | ||
580 | error = enter_state(state); | |
581 | if (error) { | |
582 | suspend_stats.fail++; | |
583 | dpm_save_failed_errno(error); | |
584 | } else { | |
585 | suspend_stats.success++; | |
2a77c46d | 586 | } |
bc25cf50 | 587 | return error; |
a9d70523 RW |
588 | } |
589 | EXPORT_SYMBOL(pm_suspend); |