IB/mlx4: fix sprintf format warning
[linux/fpc-iii.git] / kernel / power / suspend.c
blob3e2b4f519009709c61b8de20e7f159efd159df04
1 /*
2 * kernel/power/suspend.c - Suspend to RAM and standby functionality.
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.
8 * This file is released under the GPLv2.
9 */
11 #define pr_fmt(fmt) "PM: " fmt
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/console.h>
18 #include <linux/cpu.h>
19 #include <linux/cpuidle.h>
20 #include <linux/syscalls.h>
21 #include <linux/gfp.h>
22 #include <linux/io.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/mm.h>
26 #include <linux/slab.h>
27 #include <linux/export.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/ftrace.h>
31 #include <trace/events/power.h>
32 #include <linux/compiler.h>
33 #include <linux/moduleparam.h>
35 #include "power.h"
37 const char * const pm_labels[] = {
38 [PM_SUSPEND_TO_IDLE] = "freeze",
39 [PM_SUSPEND_STANDBY] = "standby",
40 [PM_SUSPEND_MEM] = "mem",
42 const char *pm_states[PM_SUSPEND_MAX];
43 static const char * const mem_sleep_labels[] = {
44 [PM_SUSPEND_TO_IDLE] = "s2idle",
45 [PM_SUSPEND_STANDBY] = "shallow",
46 [PM_SUSPEND_MEM] = "deep",
48 const char *mem_sleep_states[PM_SUSPEND_MAX];
50 suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE;
51 suspend_state_t mem_sleep_default = PM_SUSPEND_MAX;
52 suspend_state_t pm_suspend_target_state;
53 EXPORT_SYMBOL_GPL(pm_suspend_target_state);
55 unsigned int pm_suspend_global_flags;
56 EXPORT_SYMBOL_GPL(pm_suspend_global_flags);
58 static const struct platform_suspend_ops *suspend_ops;
59 static const struct platform_s2idle_ops *s2idle_ops;
60 static DECLARE_WAIT_QUEUE_HEAD(s2idle_wait_head);
62 enum s2idle_states __read_mostly s2idle_state;
63 static DEFINE_SPINLOCK(s2idle_lock);
65 void s2idle_set_ops(const struct platform_s2idle_ops *ops)
67 lock_system_sleep();
68 s2idle_ops = ops;
69 unlock_system_sleep();
72 static void s2idle_begin(void)
74 s2idle_state = S2IDLE_STATE_NONE;
77 static void s2idle_enter(void)
79 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
81 spin_lock_irq(&s2idle_lock);
82 if (pm_wakeup_pending())
83 goto out;
85 s2idle_state = S2IDLE_STATE_ENTER;
86 spin_unlock_irq(&s2idle_lock);
88 get_online_cpus();
89 cpuidle_resume();
91 /* Push all the CPUs into the idle loop. */
92 wake_up_all_idle_cpus();
93 /* Make the current CPU wait so it can enter the idle loop too. */
94 wait_event(s2idle_wait_head,
95 s2idle_state == S2IDLE_STATE_WAKE);
97 cpuidle_pause();
98 put_online_cpus();
100 spin_lock_irq(&s2idle_lock);
102 out:
103 s2idle_state = S2IDLE_STATE_NONE;
104 spin_unlock_irq(&s2idle_lock);
106 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
109 static void s2idle_loop(void)
111 pm_pr_dbg("suspend-to-idle\n");
113 for (;;) {
114 int error;
116 dpm_noirq_begin();
119 * Suspend-to-idle equals
120 * frozen processes + suspended devices + idle processors.
121 * Thus s2idle_enter() should be called right after
122 * all devices have been suspended.
124 error = dpm_noirq_suspend_devices(PMSG_SUSPEND);
125 if (!error)
126 s2idle_enter();
128 dpm_noirq_resume_devices(PMSG_RESUME);
129 if (error && (error != -EBUSY || !pm_wakeup_pending())) {
130 dpm_noirq_end();
131 break;
134 if (s2idle_ops && s2idle_ops->wake)
135 s2idle_ops->wake();
137 dpm_noirq_end();
139 if (s2idle_ops && s2idle_ops->sync)
140 s2idle_ops->sync();
142 if (pm_wakeup_pending())
143 break;
145 pm_wakeup_clear(false);
148 pm_pr_dbg("resume from suspend-to-idle\n");
151 void s2idle_wake(void)
153 unsigned long flags;
155 spin_lock_irqsave(&s2idle_lock, flags);
156 if (s2idle_state > S2IDLE_STATE_NONE) {
157 s2idle_state = S2IDLE_STATE_WAKE;
158 wake_up(&s2idle_wait_head);
160 spin_unlock_irqrestore(&s2idle_lock, flags);
162 EXPORT_SYMBOL_GPL(s2idle_wake);
164 static bool valid_state(suspend_state_t state)
167 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
168 * support and need to be valid to the low level
169 * implementation, no valid callback implies that none are valid.
171 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
174 void __init pm_states_init(void)
176 /* "mem" and "freeze" are always present in /sys/power/state. */
177 pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
178 pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
180 * Suspend-to-idle should be supported even without any suspend_ops,
181 * initialize mem_sleep_states[] accordingly here.
183 mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
186 static int __init mem_sleep_default_setup(char *str)
188 suspend_state_t state;
190 for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
191 if (mem_sleep_labels[state] &&
192 !strcmp(str, mem_sleep_labels[state])) {
193 mem_sleep_default = state;
194 break;
197 return 1;
199 __setup("mem_sleep_default=", mem_sleep_default_setup);
202 * suspend_set_ops - Set the global suspend method table.
203 * @ops: Suspend operations to use.
205 void suspend_set_ops(const struct platform_suspend_ops *ops)
207 lock_system_sleep();
209 suspend_ops = ops;
211 if (valid_state(PM_SUSPEND_STANDBY)) {
212 mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
213 pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
214 if (mem_sleep_default == PM_SUSPEND_STANDBY)
215 mem_sleep_current = PM_SUSPEND_STANDBY;
217 if (valid_state(PM_SUSPEND_MEM)) {
218 mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
219 if (mem_sleep_default >= PM_SUSPEND_MEM)
220 mem_sleep_current = PM_SUSPEND_MEM;
223 unlock_system_sleep();
225 EXPORT_SYMBOL_GPL(suspend_set_ops);
228 * suspend_valid_only_mem - Generic memory-only valid callback.
230 * Platform drivers that implement mem suspend only and only need to check for
231 * that in their .valid() callback can use this instead of rolling their own
232 * .valid() callback.
234 int suspend_valid_only_mem(suspend_state_t state)
236 return state == PM_SUSPEND_MEM;
238 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
240 static bool sleep_state_supported(suspend_state_t state)
242 return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
245 static int platform_suspend_prepare(suspend_state_t state)
247 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
248 suspend_ops->prepare() : 0;
251 static int platform_suspend_prepare_late(suspend_state_t state)
253 return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
254 s2idle_ops->prepare() : 0;
257 static int platform_suspend_prepare_noirq(suspend_state_t state)
259 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare_late ?
260 suspend_ops->prepare_late() : 0;
263 static void platform_resume_noirq(suspend_state_t state)
265 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->wake)
266 suspend_ops->wake();
269 static void platform_resume_early(suspend_state_t state)
271 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
272 s2idle_ops->restore();
275 static void platform_resume_finish(suspend_state_t state)
277 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
278 suspend_ops->finish();
281 static int platform_suspend_begin(suspend_state_t state)
283 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
284 return s2idle_ops->begin();
285 else if (suspend_ops && suspend_ops->begin)
286 return suspend_ops->begin(state);
287 else
288 return 0;
291 static void platform_resume_end(suspend_state_t state)
293 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
294 s2idle_ops->end();
295 else if (suspend_ops && suspend_ops->end)
296 suspend_ops->end();
299 static void platform_recover(suspend_state_t state)
301 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
302 suspend_ops->recover();
305 static bool platform_suspend_again(suspend_state_t state)
307 return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
308 suspend_ops->suspend_again() : false;
311 #ifdef CONFIG_PM_DEBUG
312 static unsigned int pm_test_delay = 5;
313 module_param(pm_test_delay, uint, 0644);
314 MODULE_PARM_DESC(pm_test_delay,
315 "Number of seconds to wait before resuming from suspend test");
316 #endif
318 static int suspend_test(int level)
320 #ifdef CONFIG_PM_DEBUG
321 if (pm_test_level == level) {
322 pr_info("suspend debug: Waiting for %d second(s).\n",
323 pm_test_delay);
324 mdelay(pm_test_delay * 1000);
325 return 1;
327 #endif /* !CONFIG_PM_DEBUG */
328 return 0;
332 * suspend_prepare - Prepare for entering system sleep state.
334 * Common code run for every system sleep state that can be entered (except for
335 * hibernation). Run suspend notifiers, allocate the "suspend" console and
336 * freeze processes.
338 static int suspend_prepare(suspend_state_t state)
340 int error, nr_calls = 0;
342 if (!sleep_state_supported(state))
343 return -EPERM;
345 pm_prepare_console();
347 error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
348 if (error) {
349 nr_calls--;
350 goto Finish;
353 trace_suspend_resume(TPS("freeze_processes"), 0, true);
354 error = suspend_freeze_processes();
355 trace_suspend_resume(TPS("freeze_processes"), 0, false);
356 if (!error)
357 return 0;
359 suspend_stats.failed_freeze++;
360 dpm_save_failed_step(SUSPEND_FREEZE);
361 Finish:
362 __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
363 pm_restore_console();
364 return error;
367 /* default implementation */
368 void __weak arch_suspend_disable_irqs(void)
370 local_irq_disable();
373 /* default implementation */
374 void __weak arch_suspend_enable_irqs(void)
376 local_irq_enable();
380 * suspend_enter - Make the system enter the given sleep state.
381 * @state: System sleep state to enter.
382 * @wakeup: Returns information that the sleep state should not be re-entered.
384 * This function should be called after devices have been suspended.
386 static int suspend_enter(suspend_state_t state, bool *wakeup)
388 int error;
390 error = platform_suspend_prepare(state);
391 if (error)
392 goto Platform_finish;
394 error = dpm_suspend_late(PMSG_SUSPEND);
395 if (error) {
396 pr_err("late suspend of devices failed\n");
397 goto Platform_finish;
399 error = platform_suspend_prepare_late(state);
400 if (error)
401 goto Devices_early_resume;
403 if (state == PM_SUSPEND_TO_IDLE && pm_test_level != TEST_PLATFORM) {
404 s2idle_loop();
405 goto Platform_early_resume;
408 error = dpm_suspend_noirq(PMSG_SUSPEND);
409 if (error) {
410 pr_err("noirq suspend of devices failed\n");
411 goto Platform_early_resume;
413 error = platform_suspend_prepare_noirq(state);
414 if (error)
415 goto Platform_wake;
417 if (suspend_test(TEST_PLATFORM))
418 goto Platform_wake;
420 error = disable_nonboot_cpus();
421 if (error || suspend_test(TEST_CPUS))
422 goto Enable_cpus;
424 arch_suspend_disable_irqs();
425 BUG_ON(!irqs_disabled());
427 error = syscore_suspend();
428 if (!error) {
429 *wakeup = pm_wakeup_pending();
430 if (!(suspend_test(TEST_CORE) || *wakeup)) {
431 trace_suspend_resume(TPS("machine_suspend"),
432 state, true);
433 error = suspend_ops->enter(state);
434 trace_suspend_resume(TPS("machine_suspend"),
435 state, false);
436 events_check_enabled = false;
437 } else if (*wakeup) {
438 error = -EBUSY;
440 syscore_resume();
443 arch_suspend_enable_irqs();
444 BUG_ON(irqs_disabled());
446 Enable_cpus:
447 enable_nonboot_cpus();
449 Platform_wake:
450 platform_resume_noirq(state);
451 dpm_resume_noirq(PMSG_RESUME);
453 Platform_early_resume:
454 platform_resume_early(state);
456 Devices_early_resume:
457 dpm_resume_early(PMSG_RESUME);
459 Platform_finish:
460 platform_resume_finish(state);
461 return error;
465 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
466 * @state: System sleep state to enter.
468 int suspend_devices_and_enter(suspend_state_t state)
470 int error;
471 bool wakeup = false;
473 if (!sleep_state_supported(state))
474 return -ENOSYS;
476 pm_suspend_target_state = state;
478 error = platform_suspend_begin(state);
479 if (error)
480 goto Close;
482 suspend_console();
483 suspend_test_start();
484 error = dpm_suspend_start(PMSG_SUSPEND);
485 if (error) {
486 pr_err("Some devices failed to suspend, or early wake event detected\n");
487 goto Recover_platform;
489 suspend_test_finish("suspend devices");
490 if (suspend_test(TEST_DEVICES))
491 goto Recover_platform;
493 do {
494 error = suspend_enter(state, &wakeup);
495 } while (!error && !wakeup && platform_suspend_again(state));
497 Resume_devices:
498 suspend_test_start();
499 dpm_resume_end(PMSG_RESUME);
500 suspend_test_finish("resume devices");
501 trace_suspend_resume(TPS("resume_console"), state, true);
502 resume_console();
503 trace_suspend_resume(TPS("resume_console"), state, false);
505 Close:
506 platform_resume_end(state);
507 pm_suspend_target_state = PM_SUSPEND_ON;
508 return error;
510 Recover_platform:
511 platform_recover(state);
512 goto Resume_devices;
516 * suspend_finish - Clean up before finishing the suspend sequence.
518 * Call platform code to clean up, restart processes, and free the console that
519 * we've allocated. This routine is not called for hibernation.
521 static void suspend_finish(void)
523 suspend_thaw_processes();
524 pm_notifier_call_chain(PM_POST_SUSPEND);
525 pm_restore_console();
529 * enter_state - Do common work needed to enter system sleep state.
530 * @state: System sleep state to enter.
532 * Make sure that no one else is trying to put the system into a sleep state.
533 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
534 * system enter the given sleep state and clean up after wakeup.
536 static int enter_state(suspend_state_t state)
538 int error;
540 trace_suspend_resume(TPS("suspend_enter"), state, true);
541 if (state == PM_SUSPEND_TO_IDLE) {
542 #ifdef CONFIG_PM_DEBUG
543 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
544 pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
545 return -EAGAIN;
547 #endif
548 } else if (!valid_state(state)) {
549 return -EINVAL;
551 if (!mutex_trylock(&pm_mutex))
552 return -EBUSY;
554 if (state == PM_SUSPEND_TO_IDLE)
555 s2idle_begin();
557 #ifndef CONFIG_SUSPEND_SKIP_SYNC
558 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
559 pr_info("Syncing filesystems ... ");
560 sys_sync();
561 pr_cont("done.\n");
562 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
563 #endif
565 pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
566 pm_suspend_clear_flags();
567 error = suspend_prepare(state);
568 if (error)
569 goto Unlock;
571 if (suspend_test(TEST_FREEZER))
572 goto Finish;
574 trace_suspend_resume(TPS("suspend_enter"), state, false);
575 pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
576 pm_restrict_gfp_mask();
577 error = suspend_devices_and_enter(state);
578 pm_restore_gfp_mask();
580 Finish:
581 pm_pr_dbg("Finishing wakeup.\n");
582 suspend_finish();
583 Unlock:
584 mutex_unlock(&pm_mutex);
585 return error;
589 * pm_suspend - Externally visible function for suspending the system.
590 * @state: System sleep state to enter.
592 * Check if the value of @state represents one of the supported states,
593 * execute enter_state() and update system suspend statistics.
595 int pm_suspend(suspend_state_t state)
597 int error;
599 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
600 return -EINVAL;
602 pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
603 error = enter_state(state);
604 if (error) {
605 suspend_stats.fail++;
606 dpm_save_failed_errno(error);
607 } else {
608 suspend_stats.success++;
610 pr_info("suspend exit\n");
611 return error;
613 EXPORT_SYMBOL(pm_suspend);