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>
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>
27 /*This is just an arbitrary number */
28 #define FREE_PAGE_NUMBER (100)
30 DEFINE_MUTEX(pm_mutex);
32 struct pm_ops *pm_ops;
35 * pm_set_ops - Set the global power method table.
36 * @ops: Pointer to ops structure.
39 void pm_set_ops(struct pm_ops * ops)
41 mutex_lock(&pm_mutex);
43 mutex_unlock(&pm_mutex);
47 * pm_valid_only_mem - generic memory-only valid callback
49 * pm_ops drivers that implement mem suspend only and only need
50 * to check for that in their .valid callback can use this instead
51 * of rolling their own .valid callback.
53 int pm_valid_only_mem(suspend_state_t state)
55 return state == PM_SUSPEND_MEM;
59 static inline void pm_finish(suspend_state_t state)
62 pm_ops->finish(state);
66 * suspend_prepare - Do prep work before entering low-power state.
67 * @state: State we're entering.
69 * This is common code that is called for each state that we're
70 * entering. Allocate a console, stop all processes, then make sure
71 * the platform can enter the requested state.
74 static int suspend_prepare(suspend_state_t state)
77 unsigned int free_pages;
79 if (!pm_ops || !pm_ops->enter)
84 if (freeze_processes()) {
89 if ((free_pages = global_page_state(NR_FREE_PAGES))
91 pr_debug("PM: free some memory\n");
92 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
93 if (nr_free_pages() < FREE_PAGE_NUMBER) {
95 printk(KERN_ERR "PM: No enough memory\n");
100 if (pm_ops->prepare) {
101 if ((error = pm_ops->prepare(state)))
106 error = device_suspend(PMSG_SUSPEND);
108 printk(KERN_ERR "Some devices failed to suspend\n");
111 error = disable_nonboot_cpus();
115 enable_nonboot_cpus();
122 pm_restore_console();
126 /* default implementation */
127 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
132 /* default implementation */
133 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
138 int suspend_enter(suspend_state_t state)
142 arch_suspend_disable_irqs();
143 BUG_ON(!irqs_disabled());
145 if ((error = device_power_down(PMSG_SUSPEND))) {
146 printk(KERN_ERR "Some devices failed to power down\n");
149 error = pm_ops->enter(state);
152 arch_suspend_enable_irqs();
153 BUG_ON(irqs_disabled());
159 * suspend_finish - Do final work before exiting suspend sequence.
160 * @state: State we're coming out of.
162 * Call platform code to clean up, restart processes, and free the
163 * console that we've allocated. This is not called for suspend-to-disk.
166 static void suspend_finish(suspend_state_t state)
168 enable_nonboot_cpus();
173 pm_restore_console();
179 static const char * const pm_states[PM_SUSPEND_MAX] = {
180 [PM_SUSPEND_STANDBY] = "standby",
181 [PM_SUSPEND_MEM] = "mem",
184 static inline int valid_state(suspend_state_t state)
186 /* All states need lowlevel support and need to be valid
187 * to the lowlevel implementation, no valid callback
188 * implies that none are valid. */
189 if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
196 * enter_state - Do common work of entering low-power state.
197 * @state: pm_state structure for state we're entering.
199 * Make sure we're the only ones trying to enter a sleep state. Fail
200 * if someone has beat us to it, since we don't want anything weird to
201 * happen when we wake up.
202 * Then, do the setup for suspend, enter the state, and cleaup (after
206 static int enter_state(suspend_state_t state)
210 if (!valid_state(state))
212 if (!mutex_trylock(&pm_mutex))
215 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
216 if ((error = suspend_prepare(state)))
219 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
220 error = suspend_enter(state);
222 pr_debug("PM: Finishing wakeup.\n");
223 suspend_finish(state);
225 mutex_unlock(&pm_mutex);
231 * pm_suspend - Externally visible function for suspending system.
232 * @state: Enumerated value of state to enter.
234 * Determine whether or not value is within range, get state
235 * structure, and enter (above).
238 int pm_suspend(suspend_state_t state)
240 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
241 return enter_state(state);
245 EXPORT_SYMBOL(pm_suspend);
247 decl_subsys(power,NULL,NULL);
251 * state - control system power state.
253 * show() returns what states are supported, which is hard-coded to
254 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
255 * 'disk' (Suspend-to-Disk).
257 * store() accepts one of those strings, translates it into the
258 * proper enumerated value, and initiates a suspend transition.
261 static ssize_t state_show(struct kset *kset, char *buf)
266 for (i = 0; i < PM_SUSPEND_MAX; i++) {
267 if (pm_states[i] && valid_state(i))
268 s += sprintf(s,"%s ", pm_states[i]);
270 #ifdef CONFIG_SOFTWARE_SUSPEND
271 s += sprintf(s, "%s\n", "disk");
274 /* convert the last space to a newline */
280 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
282 suspend_state_t state = PM_SUSPEND_STANDBY;
283 const char * const *s;
288 p = memchr(buf, '\n', n);
289 len = p ? p - buf : n;
291 /* First, check if we are requested to hibernate */
292 if (!strncmp(buf, "disk", len)) {
294 return error ? error : n;
297 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
298 if (*s && !strncmp(buf, *s, len))
301 if (state < PM_SUSPEND_MAX && *s)
302 error = enter_state(state);
305 return error ? error : n;
310 #ifdef CONFIG_PM_TRACE
311 int pm_trace_enabled;
313 static ssize_t pm_trace_show(struct kset *kset, char *buf)
315 return sprintf(buf, "%d\n", pm_trace_enabled);
319 pm_trace_store(struct kset *kset, const char *buf, size_t n)
323 if (sscanf(buf, "%d", &val) == 1) {
324 pm_trace_enabled = !!val;
330 power_attr(pm_trace);
332 static struct attribute * g[] = {
338 static struct attribute * g[] = {
342 #endif /* CONFIG_PM_TRACE */
344 static struct attribute_group attr_group = {
349 static int __init pm_init(void)
351 int error = subsystem_register(&power_subsys);
353 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
357 core_initcall(pm_init);