usb: gadget: udc: core: Document the relation between usb_ep_queue() and completion...
[linux/fpc-iii.git] / kernel / power / suspend.c
blob4c10be0f4843a416d95a28f0cbfe81f053c7c68a
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 * Wakeups during the noirq suspend of devices may be spurious,
125 * so prevent them from terminating the loop right away.
127 error = dpm_noirq_suspend_devices(PMSG_SUSPEND);
128 if (!error)
129 s2idle_enter();
130 else if (error == -EBUSY && pm_wakeup_pending())
131 error = 0;
133 if (!error && s2idle_ops && s2idle_ops->wake)
134 s2idle_ops->wake();
136 dpm_noirq_resume_devices(PMSG_RESUME);
138 dpm_noirq_end();
140 if (error)
141 break;
143 if (s2idle_ops && s2idle_ops->sync)
144 s2idle_ops->sync();
146 if (pm_wakeup_pending())
147 break;
149 pm_wakeup_clear(false);
152 pm_pr_dbg("resume from suspend-to-idle\n");
155 void s2idle_wake(void)
157 unsigned long flags;
159 spin_lock_irqsave(&s2idle_lock, flags);
160 if (s2idle_state > S2IDLE_STATE_NONE) {
161 s2idle_state = S2IDLE_STATE_WAKE;
162 wake_up(&s2idle_wait_head);
164 spin_unlock_irqrestore(&s2idle_lock, flags);
166 EXPORT_SYMBOL_GPL(s2idle_wake);
168 static bool valid_state(suspend_state_t state)
171 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
172 * support and need to be valid to the low level
173 * implementation, no valid callback implies that none are valid.
175 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
178 void __init pm_states_init(void)
180 /* "mem" and "freeze" are always present in /sys/power/state. */
181 pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
182 pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
184 * Suspend-to-idle should be supported even without any suspend_ops,
185 * initialize mem_sleep_states[] accordingly here.
187 mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
190 static int __init mem_sleep_default_setup(char *str)
192 suspend_state_t state;
194 for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
195 if (mem_sleep_labels[state] &&
196 !strcmp(str, mem_sleep_labels[state])) {
197 mem_sleep_default = state;
198 break;
201 return 1;
203 __setup("mem_sleep_default=", mem_sleep_default_setup);
206 * suspend_set_ops - Set the global suspend method table.
207 * @ops: Suspend operations to use.
209 void suspend_set_ops(const struct platform_suspend_ops *ops)
211 lock_system_sleep();
213 suspend_ops = ops;
215 if (valid_state(PM_SUSPEND_STANDBY)) {
216 mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
217 pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
218 if (mem_sleep_default == PM_SUSPEND_STANDBY)
219 mem_sleep_current = PM_SUSPEND_STANDBY;
221 if (valid_state(PM_SUSPEND_MEM)) {
222 mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
223 if (mem_sleep_default >= PM_SUSPEND_MEM)
224 mem_sleep_current = PM_SUSPEND_MEM;
227 unlock_system_sleep();
229 EXPORT_SYMBOL_GPL(suspend_set_ops);
232 * suspend_valid_only_mem - Generic memory-only valid callback.
234 * Platform drivers that implement mem suspend only and only need to check for
235 * that in their .valid() callback can use this instead of rolling their own
236 * .valid() callback.
238 int suspend_valid_only_mem(suspend_state_t state)
240 return state == PM_SUSPEND_MEM;
242 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
244 static bool sleep_state_supported(suspend_state_t state)
246 return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
249 static int platform_suspend_prepare(suspend_state_t state)
251 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
252 suspend_ops->prepare() : 0;
255 static int platform_suspend_prepare_late(suspend_state_t state)
257 return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
258 s2idle_ops->prepare() : 0;
261 static int platform_suspend_prepare_noirq(suspend_state_t state)
263 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare_late ?
264 suspend_ops->prepare_late() : 0;
267 static void platform_resume_noirq(suspend_state_t state)
269 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->wake)
270 suspend_ops->wake();
273 static void platform_resume_early(suspend_state_t state)
275 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
276 s2idle_ops->restore();
279 static void platform_resume_finish(suspend_state_t state)
281 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
282 suspend_ops->finish();
285 static int platform_suspend_begin(suspend_state_t state)
287 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
288 return s2idle_ops->begin();
289 else if (suspend_ops && suspend_ops->begin)
290 return suspend_ops->begin(state);
291 else
292 return 0;
295 static void platform_resume_end(suspend_state_t state)
297 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
298 s2idle_ops->end();
299 else if (suspend_ops && suspend_ops->end)
300 suspend_ops->end();
303 static void platform_recover(suspend_state_t state)
305 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
306 suspend_ops->recover();
309 static bool platform_suspend_again(suspend_state_t state)
311 return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
312 suspend_ops->suspend_again() : false;
315 #ifdef CONFIG_PM_DEBUG
316 static unsigned int pm_test_delay = 5;
317 module_param(pm_test_delay, uint, 0644);
318 MODULE_PARM_DESC(pm_test_delay,
319 "Number of seconds to wait before resuming from suspend test");
320 #endif
322 static int suspend_test(int level)
324 #ifdef CONFIG_PM_DEBUG
325 if (pm_test_level == level) {
326 pr_info("suspend debug: Waiting for %d second(s).\n",
327 pm_test_delay);
328 mdelay(pm_test_delay * 1000);
329 return 1;
331 #endif /* !CONFIG_PM_DEBUG */
332 return 0;
336 * suspend_prepare - Prepare for entering system sleep state.
338 * Common code run for every system sleep state that can be entered (except for
339 * hibernation). Run suspend notifiers, allocate the "suspend" console and
340 * freeze processes.
342 static int suspend_prepare(suspend_state_t state)
344 int error, nr_calls = 0;
346 if (!sleep_state_supported(state))
347 return -EPERM;
349 pm_prepare_console();
351 error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
352 if (error) {
353 nr_calls--;
354 goto Finish;
357 trace_suspend_resume(TPS("freeze_processes"), 0, true);
358 error = suspend_freeze_processes();
359 trace_suspend_resume(TPS("freeze_processes"), 0, false);
360 if (!error)
361 return 0;
363 suspend_stats.failed_freeze++;
364 dpm_save_failed_step(SUSPEND_FREEZE);
365 Finish:
366 __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
367 pm_restore_console();
368 return error;
371 /* default implementation */
372 void __weak arch_suspend_disable_irqs(void)
374 local_irq_disable();
377 /* default implementation */
378 void __weak arch_suspend_enable_irqs(void)
380 local_irq_enable();
384 * suspend_enter - Make the system enter the given sleep state.
385 * @state: System sleep state to enter.
386 * @wakeup: Returns information that the sleep state should not be re-entered.
388 * This function should be called after devices have been suspended.
390 static int suspend_enter(suspend_state_t state, bool *wakeup)
392 int error;
394 error = platform_suspend_prepare(state);
395 if (error)
396 goto Platform_finish;
398 error = dpm_suspend_late(PMSG_SUSPEND);
399 if (error) {
400 pr_err("late suspend of devices failed\n");
401 goto Platform_finish;
403 error = platform_suspend_prepare_late(state);
404 if (error)
405 goto Devices_early_resume;
407 if (state == PM_SUSPEND_TO_IDLE && pm_test_level != TEST_PLATFORM) {
408 s2idle_loop();
409 goto Platform_early_resume;
412 error = dpm_suspend_noirq(PMSG_SUSPEND);
413 if (error) {
414 pr_err("noirq suspend of devices failed\n");
415 goto Platform_early_resume;
417 error = platform_suspend_prepare_noirq(state);
418 if (error)
419 goto Platform_wake;
421 if (suspend_test(TEST_PLATFORM))
422 goto Platform_wake;
424 error = disable_nonboot_cpus();
425 if (error || suspend_test(TEST_CPUS))
426 goto Enable_cpus;
428 arch_suspend_disable_irqs();
429 BUG_ON(!irqs_disabled());
431 error = syscore_suspend();
432 if (!error) {
433 *wakeup = pm_wakeup_pending();
434 if (!(suspend_test(TEST_CORE) || *wakeup)) {
435 trace_suspend_resume(TPS("machine_suspend"),
436 state, true);
437 error = suspend_ops->enter(state);
438 trace_suspend_resume(TPS("machine_suspend"),
439 state, false);
440 } else if (*wakeup) {
441 error = -EBUSY;
443 syscore_resume();
446 arch_suspend_enable_irqs();
447 BUG_ON(irqs_disabled());
449 Enable_cpus:
450 enable_nonboot_cpus();
452 Platform_wake:
453 platform_resume_noirq(state);
454 dpm_resume_noirq(PMSG_RESUME);
456 Platform_early_resume:
457 platform_resume_early(state);
459 Devices_early_resume:
460 dpm_resume_early(PMSG_RESUME);
462 Platform_finish:
463 platform_resume_finish(state);
464 return error;
468 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
469 * @state: System sleep state to enter.
471 int suspend_devices_and_enter(suspend_state_t state)
473 int error;
474 bool wakeup = false;
476 if (!sleep_state_supported(state))
477 return -ENOSYS;
479 pm_suspend_target_state = state;
481 error = platform_suspend_begin(state);
482 if (error)
483 goto Close;
485 suspend_console();
486 suspend_test_start();
487 error = dpm_suspend_start(PMSG_SUSPEND);
488 if (error) {
489 pr_err("Some devices failed to suspend, or early wake event detected\n");
490 goto Recover_platform;
492 suspend_test_finish("suspend devices");
493 if (suspend_test(TEST_DEVICES))
494 goto Recover_platform;
496 do {
497 error = suspend_enter(state, &wakeup);
498 } while (!error && !wakeup && platform_suspend_again(state));
500 Resume_devices:
501 suspend_test_start();
502 dpm_resume_end(PMSG_RESUME);
503 suspend_test_finish("resume devices");
504 trace_suspend_resume(TPS("resume_console"), state, true);
505 resume_console();
506 trace_suspend_resume(TPS("resume_console"), state, false);
508 Close:
509 platform_resume_end(state);
510 pm_suspend_target_state = PM_SUSPEND_ON;
511 return error;
513 Recover_platform:
514 platform_recover(state);
515 goto Resume_devices;
519 * suspend_finish - Clean up before finishing the suspend sequence.
521 * Call platform code to clean up, restart processes, and free the console that
522 * we've allocated. This routine is not called for hibernation.
524 static void suspend_finish(void)
526 suspend_thaw_processes();
527 pm_notifier_call_chain(PM_POST_SUSPEND);
528 pm_restore_console();
532 * enter_state - Do common work needed to enter system sleep state.
533 * @state: System sleep state to enter.
535 * Make sure that no one else is trying to put the system into a sleep state.
536 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
537 * system enter the given sleep state and clean up after wakeup.
539 static int enter_state(suspend_state_t state)
541 int error;
543 trace_suspend_resume(TPS("suspend_enter"), state, true);
544 if (state == PM_SUSPEND_TO_IDLE) {
545 #ifdef CONFIG_PM_DEBUG
546 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
547 pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
548 return -EAGAIN;
550 #endif
551 } else if (!valid_state(state)) {
552 return -EINVAL;
554 if (!mutex_trylock(&pm_mutex))
555 return -EBUSY;
557 if (state == PM_SUSPEND_TO_IDLE)
558 s2idle_begin();
560 #ifndef CONFIG_SUSPEND_SKIP_SYNC
561 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
562 pr_info("Syncing filesystems ... ");
563 ksys_sync();
564 pr_cont("done.\n");
565 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
566 #endif
568 pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
569 pm_suspend_clear_flags();
570 error = suspend_prepare(state);
571 if (error)
572 goto Unlock;
574 if (suspend_test(TEST_FREEZER))
575 goto Finish;
577 trace_suspend_resume(TPS("suspend_enter"), state, false);
578 pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
579 pm_restrict_gfp_mask();
580 error = suspend_devices_and_enter(state);
581 pm_restore_gfp_mask();
583 Finish:
584 events_check_enabled = false;
585 pm_pr_dbg("Finishing wakeup.\n");
586 suspend_finish();
587 Unlock:
588 mutex_unlock(&pm_mutex);
589 return error;
593 * pm_suspend - Externally visible function for suspending the system.
594 * @state: System sleep state to enter.
596 * Check if the value of @state represents one of the supported states,
597 * execute enter_state() and update system suspend statistics.
599 int pm_suspend(suspend_state_t state)
601 int error;
603 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
604 return -EINVAL;
606 pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
607 error = enter_state(state);
608 if (error) {
609 suspend_stats.fail++;
610 dpm_save_failed_errno(error);
611 } else {
612 suspend_stats.success++;
614 pr_info("suspend exit\n");
615 return error;
617 EXPORT_SYMBOL(pm_suspend);