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/init.h>
18 #include <linux/console.h>
19 #include <linux/cpu.h>
20 #include <linux/resume-trace.h>
21 #include <linux/freezer.h>
22 #include <linux/vmstat.h>
23 #include <linux/syscalls.h>
27 BLOCKING_NOTIFIER_HEAD(pm_chain_head);
29 DEFINE_MUTEX(pm_mutex);
31 unsigned int pm_flags;
32 EXPORT_SYMBOL(pm_flags);
34 #ifdef CONFIG_PM_DEBUG
35 int pm_test_level = TEST_NONE;
37 static int suspend_test(int level)
39 if (pm_test_level == level) {
40 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
47 static const char * const pm_tests[__TEST_AFTER_LAST] = {
50 [TEST_CPUS] = "processors",
51 [TEST_PLATFORM] = "platform",
52 [TEST_DEVICES] = "devices",
53 [TEST_FREEZER] = "freezer",
56 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
62 for (level = TEST_FIRST; level <= TEST_MAX; level++)
63 if (pm_tests[level]) {
64 if (level == pm_test_level)
65 s += sprintf(s, "[%s] ", pm_tests[level]);
67 s += sprintf(s, "%s ", pm_tests[level]);
71 /* convert the last space to a newline */
77 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
78 const char *buf, size_t n)
80 const char * const *s;
86 p = memchr(buf, '\n', n);
87 len = p ? p - buf : n;
89 mutex_lock(&pm_mutex);
92 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
93 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
94 pm_test_level = level;
99 mutex_unlock(&pm_mutex);
101 return error ? error : n;
105 #else /* !CONFIG_PM_DEBUG */
106 static inline int suspend_test(int level) { return 0; }
107 #endif /* !CONFIG_PM_DEBUG */
110 #ifdef CONFIG_SUSPEND
112 /* This is just an arbitrary number */
113 #define FREE_PAGE_NUMBER (100)
115 static struct platform_suspend_ops *suspend_ops;
118 * suspend_set_ops - Set the global suspend method table.
119 * @ops: Pointer to ops structure.
122 void suspend_set_ops(struct platform_suspend_ops *ops)
124 mutex_lock(&pm_mutex);
126 mutex_unlock(&pm_mutex);
130 * suspend_valid_only_mem - generic memory-only valid callback
132 * Platform drivers that implement mem suspend only and only need
133 * to check for that in their .valid callback can use this instead
134 * of rolling their own .valid callback.
136 int suspend_valid_only_mem(suspend_state_t state)
138 return state == PM_SUSPEND_MEM;
142 * suspend_prepare - Do prep work before entering low-power state.
144 * This is common code that is called for each state that we're entering.
145 * Run suspend notifiers, allocate a console and stop all processes.
147 static int suspend_prepare(void)
150 unsigned int free_pages;
152 if (!suspend_ops || !suspend_ops->enter)
155 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
159 pm_prepare_console();
161 if (freeze_processes()) {
166 free_pages = global_page_state(NR_FREE_PAGES);
167 if (free_pages < FREE_PAGE_NUMBER) {
168 pr_debug("PM: free some memory\n");
169 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
170 if (nr_free_pages() < FREE_PAGE_NUMBER) {
172 printk(KERN_ERR "PM: No enough memory\n");
180 pm_restore_console();
182 pm_notifier_call_chain(PM_POST_SUSPEND);
186 /* default implementation */
187 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
192 /* default implementation */
193 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
199 * suspend_enter - enter the desired system sleep state.
200 * @state: state to enter
202 * This function should be called after devices have been suspended.
204 static int suspend_enter(suspend_state_t state)
208 arch_suspend_disable_irqs();
209 BUG_ON(!irqs_disabled());
211 if ((error = device_power_down(PMSG_SUSPEND))) {
212 printk(KERN_ERR "Some devices failed to power down\n");
216 if (!suspend_test(TEST_CORE))
217 error = suspend_ops->enter(state);
221 arch_suspend_enable_irqs();
222 BUG_ON(irqs_disabled());
227 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
229 * @state: state to enter
231 int suspend_devices_and_enter(suspend_state_t state)
238 if (suspend_ops->set_target) {
239 error = suspend_ops->set_target(state);
244 error = device_suspend(PMSG_SUSPEND);
246 printk(KERN_ERR "Some devices failed to suspend\n");
250 if (suspend_test(TEST_DEVICES))
253 if (suspend_ops->prepare) {
254 error = suspend_ops->prepare();
259 if (suspend_test(TEST_PLATFORM))
262 error = disable_nonboot_cpus();
263 if (!error && !suspend_test(TEST_CPUS))
264 suspend_enter(state);
266 enable_nonboot_cpus();
268 if (suspend_ops->finish)
269 suspend_ops->finish();
278 * suspend_finish - Do final work before exiting suspend sequence.
280 * Call platform code to clean up, restart processes, and free the
281 * console that we've allocated. This is not called for suspend-to-disk.
283 static void suspend_finish(void)
286 pm_restore_console();
287 pm_notifier_call_chain(PM_POST_SUSPEND);
293 static const char * const pm_states[PM_SUSPEND_MAX] = {
294 [PM_SUSPEND_STANDBY] = "standby",
295 [PM_SUSPEND_MEM] = "mem",
298 static inline int valid_state(suspend_state_t state)
300 /* All states need lowlevel support and need to be valid
301 * to the lowlevel implementation, no valid callback
302 * implies that none are valid. */
303 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
310 * enter_state - Do common work of entering low-power state.
311 * @state: pm_state structure for state we're entering.
313 * Make sure we're the only ones trying to enter a sleep state. Fail
314 * if someone has beat us to it, since we don't want anything weird to
315 * happen when we wake up.
316 * Then, do the setup for suspend, enter the state, and cleaup (after
319 static int enter_state(suspend_state_t state)
323 if (!valid_state(state))
326 if (!mutex_trylock(&pm_mutex))
329 printk("Syncing filesystems ... ");
333 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
334 error = suspend_prepare();
338 if (suspend_test(TEST_FREEZER))
341 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
342 error = suspend_devices_and_enter(state);
345 pr_debug("PM: Finishing wakeup.\n");
348 mutex_unlock(&pm_mutex);
354 * pm_suspend - Externally visible function for suspending system.
355 * @state: Enumerated value of state to enter.
357 * Determine whether or not value is within range, get state
358 * structure, and enter (above).
361 int pm_suspend(suspend_state_t state)
363 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
364 return enter_state(state);
368 EXPORT_SYMBOL(pm_suspend);
370 #endif /* CONFIG_SUSPEND */
372 struct kobject *power_kobj;
375 * state - control system power state.
377 * show() returns what states are supported, which is hard-coded to
378 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
379 * 'disk' (Suspend-to-Disk).
381 * store() accepts one of those strings, translates it into the
382 * proper enumerated value, and initiates a suspend transition.
385 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
389 #ifdef CONFIG_SUSPEND
392 for (i = 0; i < PM_SUSPEND_MAX; i++) {
393 if (pm_states[i] && valid_state(i))
394 s += sprintf(s,"%s ", pm_states[i]);
397 #ifdef CONFIG_HIBERNATION
398 s += sprintf(s, "%s\n", "disk");
401 /* convert the last space to a newline */
407 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
408 const char *buf, size_t n)
410 #ifdef CONFIG_SUSPEND
411 suspend_state_t state = PM_SUSPEND_STANDBY;
412 const char * const *s;
418 p = memchr(buf, '\n', n);
419 len = p ? p - buf : n;
421 /* First, check if we are requested to hibernate */
422 if (len == 4 && !strncmp(buf, "disk", len)) {
427 #ifdef CONFIG_SUSPEND
428 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
429 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
432 if (state < PM_SUSPEND_MAX && *s)
433 error = enter_state(state);
437 return error ? error : n;
442 #ifdef CONFIG_PM_TRACE
443 int pm_trace_enabled;
445 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
448 return sprintf(buf, "%d\n", pm_trace_enabled);
452 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
453 const char *buf, size_t n)
457 if (sscanf(buf, "%d", &val) == 1) {
458 pm_trace_enabled = !!val;
464 power_attr(pm_trace);
465 #endif /* CONFIG_PM_TRACE */
467 static struct attribute * g[] = {
469 #ifdef CONFIG_PM_TRACE
472 #ifdef CONFIG_PM_DEBUG
478 static struct attribute_group attr_group = {
483 static int __init pm_init(void)
485 power_kobj = kobject_create_and_add("power", NULL);
488 return sysfs_create_group(power_kobj, &attr_group);
491 core_initcall(pm_init);