2 * kernel/power/main.c - PM subsystem core functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
11 #include <linux/module.h>
12 #include <linux/suspend.h>
13 #include <linux/kobject.h>
14 #include <linux/string.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/kmod.h>
18 #include <linux/init.h>
19 #include <linux/console.h>
20 #include <linux/cpu.h>
21 #include <linux/resume-trace.h>
22 #include <linux/freezer.h>
23 #include <linux/vmstat.h>
24 #include <linux/syscalls.h>
25 #include <linux/ftrace.h>
29 DEFINE_MUTEX(pm_mutex);
31 unsigned int pm_flags;
32 EXPORT_SYMBOL(pm_flags);
34 #ifdef CONFIG_PM_SLEEP
36 /* Routines for PM-transition notifications */
38 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
40 int register_pm_notifier(struct notifier_block *nb)
42 return blocking_notifier_chain_register(&pm_chain_head, nb);
44 EXPORT_SYMBOL_GPL(register_pm_notifier);
46 int unregister_pm_notifier(struct notifier_block *nb)
48 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
50 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
52 int pm_notifier_call_chain(unsigned long val)
54 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
55 == NOTIFY_BAD) ? -EINVAL : 0;
58 #ifdef CONFIG_PM_DEBUG
59 int pm_test_level = TEST_NONE;
61 static int suspend_test(int level)
63 if (pm_test_level == level) {
64 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
71 static const char * const pm_tests[__TEST_AFTER_LAST] = {
74 [TEST_CPUS] = "processors",
75 [TEST_PLATFORM] = "platform",
76 [TEST_DEVICES] = "devices",
77 [TEST_FREEZER] = "freezer",
80 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
86 for (level = TEST_FIRST; level <= TEST_MAX; level++)
87 if (pm_tests[level]) {
88 if (level == pm_test_level)
89 s += sprintf(s, "[%s] ", pm_tests[level]);
91 s += sprintf(s, "%s ", pm_tests[level]);
95 /* convert the last space to a newline */
101 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
102 const char *buf, size_t n)
104 const char * const *s;
110 p = memchr(buf, '\n', n);
111 len = p ? p - buf : n;
113 mutex_lock(&pm_mutex);
116 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
117 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
118 pm_test_level = level;
123 mutex_unlock(&pm_mutex);
125 return error ? error : n;
129 #else /* !CONFIG_PM_DEBUG */
130 static inline int suspend_test(int level) { return 0; }
131 #endif /* !CONFIG_PM_DEBUG */
133 #endif /* CONFIG_PM_SLEEP */
135 #ifdef CONFIG_SUSPEND
137 #ifdef CONFIG_PM_TEST_SUSPEND
140 * We test the system suspend code by setting an RTC wakealarm a short
141 * time in the future, then suspending. Suspending the devices won't
142 * normally take long ... some systems only need a few milliseconds.
144 * The time it takes is system-specific though, so when we test this
145 * during system bootup we allow a LOT of time.
147 #define TEST_SUSPEND_SECONDS 5
149 static unsigned long suspend_test_start_time;
151 static void suspend_test_start(void)
153 /* FIXME Use better timebase than "jiffies", ideally a clocksource.
154 * What we want is a hardware counter that will work correctly even
155 * during the irqs-are-off stages of the suspend/resume cycle...
157 suspend_test_start_time = jiffies;
160 static void suspend_test_finish(const char *label)
162 long nj = jiffies - suspend_test_start_time;
165 msec = jiffies_to_msecs(abs(nj));
166 pr_info("PM: %s took %d.%03d seconds\n", label,
167 msec / 1000, msec % 1000);
169 /* Warning on suspend means the RTC alarm period needs to be
170 * larger -- the system was sooo slooowwww to suspend that the
171 * alarm (should have) fired before the system went to sleep!
173 * Warning on either suspend or resume also means the system
174 * has some performance issues. The stack dump of a WARN_ON
175 * is more likely to get the right attention than a printk...
177 WARN(msec > (TEST_SUSPEND_SECONDS * 1000), "Component: %s\n", label);
182 static void suspend_test_start(void)
186 static void suspend_test_finish(const char *label)
192 /* This is just an arbitrary number */
193 #define FREE_PAGE_NUMBER (100)
195 static struct platform_suspend_ops *suspend_ops;
198 * suspend_set_ops - Set the global suspend method table.
199 * @ops: Pointer to ops structure.
202 void suspend_set_ops(struct platform_suspend_ops *ops)
204 mutex_lock(&pm_mutex);
206 mutex_unlock(&pm_mutex);
210 * suspend_valid_only_mem - generic memory-only valid callback
212 * Platform drivers that implement mem suspend only and only need
213 * to check for that in their .valid callback can use this instead
214 * of rolling their own .valid callback.
216 int suspend_valid_only_mem(suspend_state_t state)
218 return state == PM_SUSPEND_MEM;
222 * suspend_prepare - Do prep work before entering low-power state.
224 * This is common code that is called for each state that we're entering.
225 * Run suspend notifiers, allocate a console and stop all processes.
227 static int suspend_prepare(void)
230 unsigned int free_pages;
232 if (!suspend_ops || !suspend_ops->enter)
235 pm_prepare_console();
237 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
241 error = usermodehelper_disable();
245 if (suspend_freeze_processes()) {
250 free_pages = global_page_state(NR_FREE_PAGES);
251 if (free_pages < FREE_PAGE_NUMBER) {
252 pr_debug("PM: free some memory\n");
253 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
254 if (nr_free_pages() < FREE_PAGE_NUMBER) {
256 printk(KERN_ERR "PM: No enough memory\n");
263 suspend_thaw_processes();
264 usermodehelper_enable();
266 pm_notifier_call_chain(PM_POST_SUSPEND);
267 pm_restore_console();
271 /* default implementation */
272 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
277 /* default implementation */
278 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
284 * suspend_enter - enter the desired system sleep state.
285 * @state: state to enter
287 * This function should be called after devices have been suspended.
289 static int suspend_enter(suspend_state_t state)
294 arch_suspend_disable_irqs();
295 BUG_ON(!irqs_disabled());
297 if ((error = device_power_down(PMSG_SUSPEND))) {
298 printk(KERN_ERR "PM: Some devices failed to power down\n");
302 if (!suspend_test(TEST_CORE))
303 error = suspend_ops->enter(state);
305 device_power_up(PMSG_RESUME);
307 arch_suspend_enable_irqs();
308 BUG_ON(irqs_disabled());
314 * suspend_devices_and_enter - suspend devices and enter the desired system
316 * @state: state to enter
318 int suspend_devices_and_enter(suspend_state_t state)
320 int error, ftrace_save;
325 if (suspend_ops->begin) {
326 error = suspend_ops->begin(state);
331 ftrace_save = __ftrace_enabled_save();
332 suspend_test_start();
333 error = device_suspend(PMSG_SUSPEND);
335 printk(KERN_ERR "PM: Some devices failed to suspend\n");
336 goto Recover_platform;
338 suspend_test_finish("suspend devices");
339 if (suspend_test(TEST_DEVICES))
340 goto Recover_platform;
342 if (suspend_ops->prepare) {
343 error = suspend_ops->prepare();
348 if (suspend_test(TEST_PLATFORM))
351 error = disable_nonboot_cpus();
352 if (!error && !suspend_test(TEST_CPUS))
353 suspend_enter(state);
355 enable_nonboot_cpus();
357 if (suspend_ops->finish)
358 suspend_ops->finish();
360 suspend_test_start();
361 device_resume(PMSG_RESUME);
362 suspend_test_finish("resume devices");
363 __ftrace_enabled_restore(ftrace_save);
366 if (suspend_ops->end)
371 if (suspend_ops->recover)
372 suspend_ops->recover();
377 * suspend_finish - Do final work before exiting suspend sequence.
379 * Call platform code to clean up, restart processes, and free the
380 * console that we've allocated. This is not called for suspend-to-disk.
382 static void suspend_finish(void)
384 suspend_thaw_processes();
385 usermodehelper_enable();
386 pm_notifier_call_chain(PM_POST_SUSPEND);
387 pm_restore_console();
393 static const char * const pm_states[PM_SUSPEND_MAX] = {
394 [PM_SUSPEND_STANDBY] = "standby",
395 [PM_SUSPEND_MEM] = "mem",
398 static inline int valid_state(suspend_state_t state)
400 /* All states need lowlevel support and need to be valid
401 * to the lowlevel implementation, no valid callback
402 * implies that none are valid. */
403 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
410 * enter_state - Do common work of entering low-power state.
411 * @state: pm_state structure for state we're entering.
413 * Make sure we're the only ones trying to enter a sleep state. Fail
414 * if someone has beat us to it, since we don't want anything weird to
415 * happen when we wake up.
416 * Then, do the setup for suspend, enter the state, and cleaup (after
419 static int enter_state(suspend_state_t state)
423 if (!valid_state(state))
426 if (!mutex_trylock(&pm_mutex))
429 printk(KERN_INFO "PM: Syncing filesystems ... ");
433 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
434 error = suspend_prepare();
438 if (suspend_test(TEST_FREEZER))
441 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
442 error = suspend_devices_and_enter(state);
445 pr_debug("PM: Finishing wakeup.\n");
448 mutex_unlock(&pm_mutex);
454 * pm_suspend - Externally visible function for suspending system.
455 * @state: Enumerated value of state to enter.
457 * Determine whether or not value is within range, get state
458 * structure, and enter (above).
461 int pm_suspend(suspend_state_t state)
463 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
464 return enter_state(state);
468 EXPORT_SYMBOL(pm_suspend);
470 #endif /* CONFIG_SUSPEND */
472 struct kobject *power_kobj;
475 * state - control system power state.
477 * show() returns what states are supported, which is hard-coded to
478 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
479 * 'disk' (Suspend-to-Disk).
481 * store() accepts one of those strings, translates it into the
482 * proper enumerated value, and initiates a suspend transition.
485 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
489 #ifdef CONFIG_SUSPEND
492 for (i = 0; i < PM_SUSPEND_MAX; i++) {
493 if (pm_states[i] && valid_state(i))
494 s += sprintf(s,"%s ", pm_states[i]);
497 #ifdef CONFIG_HIBERNATION
498 s += sprintf(s, "%s\n", "disk");
501 /* convert the last space to a newline */
507 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
508 const char *buf, size_t n)
510 #ifdef CONFIG_SUSPEND
511 suspend_state_t state = PM_SUSPEND_STANDBY;
512 const char * const *s;
518 p = memchr(buf, '\n', n);
519 len = p ? p - buf : n;
521 /* First, check if we are requested to hibernate */
522 if (len == 4 && !strncmp(buf, "disk", len)) {
527 #ifdef CONFIG_SUSPEND
528 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
529 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
532 if (state < PM_SUSPEND_MAX && *s)
533 error = enter_state(state);
537 return error ? error : n;
542 #ifdef CONFIG_PM_TRACE
543 int pm_trace_enabled;
545 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
548 return sprintf(buf, "%d\n", pm_trace_enabled);
552 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
553 const char *buf, size_t n)
557 if (sscanf(buf, "%d", &val) == 1) {
558 pm_trace_enabled = !!val;
564 power_attr(pm_trace);
565 #endif /* CONFIG_PM_TRACE */
567 static struct attribute * g[] = {
569 #ifdef CONFIG_PM_TRACE
572 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
578 static struct attribute_group attr_group = {
583 static int __init pm_init(void)
585 power_kobj = kobject_create_and_add("power", NULL);
588 return sysfs_create_group(power_kobj, &attr_group);
591 core_initcall(pm_init);
594 #ifdef CONFIG_PM_TEST_SUSPEND
596 #include <linux/rtc.h>
599 * To test system suspend, we need a hands-off mechanism to resume the
600 * system. RTCs wake alarms are a common self-contained mechanism.
603 static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
605 static char err_readtime[] __initdata =
606 KERN_ERR "PM: can't read %s time, err %d\n";
607 static char err_wakealarm [] __initdata =
608 KERN_ERR "PM: can't set %s wakealarm, err %d\n";
609 static char err_suspend[] __initdata =
610 KERN_ERR "PM: suspend test failed, error %d\n";
611 static char info_test[] __initdata =
612 KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
615 struct rtc_wkalrm alm;
618 /* this may fail if the RTC hasn't been initialized */
619 status = rtc_read_time(rtc, &alm.time);
621 printk(err_readtime, rtc->dev.bus_id, status);
624 rtc_tm_to_time(&alm.time, &now);
626 memset(&alm, 0, sizeof alm);
627 rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
630 status = rtc_set_alarm(rtc, &alm);
632 printk(err_wakealarm, rtc->dev.bus_id, status);
636 if (state == PM_SUSPEND_MEM) {
637 printk(info_test, pm_states[state]);
638 status = pm_suspend(state);
639 if (status == -ENODEV)
640 state = PM_SUSPEND_STANDBY;
642 if (state == PM_SUSPEND_STANDBY) {
643 printk(info_test, pm_states[state]);
644 status = pm_suspend(state);
647 printk(err_suspend, status);
649 /* Some platforms can't detect that the alarm triggered the
650 * wakeup, or (accordingly) disable it after it afterwards.
651 * It's supposed to give oneshot behavior; cope.
654 rtc_set_alarm(rtc, &alm);
657 static int __init has_wakealarm(struct device *dev, void *name_ptr)
659 struct rtc_device *candidate = to_rtc_device(dev);
661 if (!candidate->ops->set_alarm)
663 if (!device_may_wakeup(candidate->dev.parent))
666 *(char **)name_ptr = dev->bus_id;
671 * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
672 * at startup time. They're normally disabled, for faster boot and because
673 * we can't know which states really work on this particular system.
675 static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
677 static char warn_bad_state[] __initdata =
678 KERN_WARNING "PM: can't test '%s' suspend state\n";
680 static int __init setup_test_suspend(char *value)
684 /* "=mem" ==> "mem" */
686 for (i = 0; i < PM_SUSPEND_MAX; i++) {
689 if (strcmp(pm_states[i], value) != 0)
691 test_state = (__force suspend_state_t) i;
694 printk(warn_bad_state, value);
697 __setup("test_suspend", setup_test_suspend);
699 static int __init test_suspend(void)
701 static char warn_no_rtc[] __initdata =
702 KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
705 struct rtc_device *rtc = NULL;
707 /* PM is initialized by now; is that state testable? */
708 if (test_state == PM_SUSPEND_ON)
710 if (!valid_state(test_state)) {
711 printk(warn_bad_state, pm_states[test_state]);
715 /* RTCs have initialized by now too ... can we use one? */
716 class_find_device(rtc_class, NULL, &pony, has_wakealarm);
718 rtc = rtc_class_open(pony);
725 test_wakealarm(rtc, test_state);
726 rtc_class_close(rtc);
730 late_initcall(test_suspend);
732 #endif /* CONFIG_PM_TEST_SUSPEND */