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>
26 BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28 /*This is just an arbitrary number */
29 #define FREE_PAGE_NUMBER (100)
31 DEFINE_MUTEX(pm_mutex);
33 struct pm_ops *pm_ops;
36 * pm_set_ops - Set the global power method table.
37 * @ops: Pointer to ops structure.
40 void pm_set_ops(struct pm_ops * ops)
42 mutex_lock(&pm_mutex);
44 mutex_unlock(&pm_mutex);
48 * pm_valid_only_mem - generic memory-only valid callback
50 * pm_ops drivers that implement mem suspend only and only need
51 * to check for that in their .valid callback can use this instead
52 * of rolling their own .valid callback.
54 int pm_valid_only_mem(suspend_state_t state)
56 return state == PM_SUSPEND_MEM;
60 static inline void pm_finish(suspend_state_t state)
63 pm_ops->finish(state);
67 * suspend_prepare - Do prep work before entering low-power state.
68 * @state: State we're entering.
70 * This is common code that is called for each state that we're
71 * entering. Allocate a console, stop all processes, then make sure
72 * the platform can enter the requested state.
75 static int suspend_prepare(suspend_state_t state)
78 unsigned int free_pages;
80 if (!pm_ops || !pm_ops->enter)
83 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
89 if (freeze_processes()) {
94 if ((free_pages = global_page_state(NR_FREE_PAGES))
96 pr_debug("PM: free some memory\n");
97 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
98 if (nr_free_pages() < FREE_PAGE_NUMBER) {
100 printk(KERN_ERR "PM: No enough memory\n");
105 if (pm_ops->set_target) {
106 error = pm_ops->set_target(state);
111 error = device_suspend(PMSG_SUSPEND);
113 printk(KERN_ERR "Some devices failed to suspend\n");
116 if (pm_ops->prepare) {
117 if ((error = pm_ops->prepare(state)))
121 error = disable_nonboot_cpus();
125 enable_nonboot_cpus();
133 pm_restore_console();
135 pm_notifier_call_chain(PM_POST_SUSPEND);
139 /* default implementation */
140 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
145 /* default implementation */
146 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
151 int suspend_enter(suspend_state_t state)
155 arch_suspend_disable_irqs();
156 BUG_ON(!irqs_disabled());
158 if ((error = device_power_down(PMSG_SUSPEND))) {
159 printk(KERN_ERR "Some devices failed to power down\n");
162 error = pm_ops->enter(state);
165 arch_suspend_enable_irqs();
166 BUG_ON(irqs_disabled());
172 * suspend_finish - Do final work before exiting suspend sequence.
173 * @state: State we're coming out of.
175 * Call platform code to clean up, restart processes, and free the
176 * console that we've allocated. This is not called for suspend-to-disk.
179 static void suspend_finish(suspend_state_t state)
181 enable_nonboot_cpus();
186 pm_restore_console();
187 pm_notifier_call_chain(PM_POST_SUSPEND);
193 static const char * const pm_states[PM_SUSPEND_MAX] = {
194 [PM_SUSPEND_STANDBY] = "standby",
195 [PM_SUSPEND_MEM] = "mem",
198 static inline int valid_state(suspend_state_t state)
200 /* All states need lowlevel support and need to be valid
201 * to the lowlevel implementation, no valid callback
202 * implies that none are valid. */
203 if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
210 * enter_state - Do common work of entering low-power state.
211 * @state: pm_state structure for state we're entering.
213 * Make sure we're the only ones trying to enter a sleep state. Fail
214 * if someone has beat us to it, since we don't want anything weird to
215 * happen when we wake up.
216 * Then, do the setup for suspend, enter the state, and cleaup (after
220 static int enter_state(suspend_state_t state)
224 if (!valid_state(state))
226 if (!mutex_trylock(&pm_mutex))
229 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
230 if ((error = suspend_prepare(state)))
233 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
234 error = suspend_enter(state);
236 pr_debug("PM: Finishing wakeup.\n");
237 suspend_finish(state);
239 mutex_unlock(&pm_mutex);
245 * pm_suspend - Externally visible function for suspending system.
246 * @state: Enumerated value of state to enter.
248 * Determine whether or not value is within range, get state
249 * structure, and enter (above).
252 int pm_suspend(suspend_state_t state)
254 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
255 return enter_state(state);
259 EXPORT_SYMBOL(pm_suspend);
261 decl_subsys(power,NULL,NULL);
265 * state - control system power state.
267 * show() returns what states are supported, which is hard-coded to
268 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
269 * 'disk' (Suspend-to-Disk).
271 * store() accepts one of those strings, translates it into the
272 * proper enumerated value, and initiates a suspend transition.
275 static ssize_t state_show(struct kset *kset, char *buf)
280 for (i = 0; i < PM_SUSPEND_MAX; i++) {
281 if (pm_states[i] && valid_state(i))
282 s += sprintf(s,"%s ", pm_states[i]);
284 #ifdef CONFIG_SOFTWARE_SUSPEND
285 s += sprintf(s, "%s\n", "disk");
288 /* convert the last space to a newline */
294 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
296 suspend_state_t state = PM_SUSPEND_STANDBY;
297 const char * const *s;
302 p = memchr(buf, '\n', n);
303 len = p ? p - buf : n;
305 /* First, check if we are requested to hibernate */
306 if (len == 4 && !strncmp(buf, "disk", len)) {
308 return error ? error : n;
311 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
312 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
315 if (state < PM_SUSPEND_MAX && *s)
316 error = enter_state(state);
319 return error ? error : n;
324 #ifdef CONFIG_PM_TRACE
325 int pm_trace_enabled;
327 static ssize_t pm_trace_show(struct kset *kset, char *buf)
329 return sprintf(buf, "%d\n", pm_trace_enabled);
333 pm_trace_store(struct kset *kset, const char *buf, size_t n)
337 if (sscanf(buf, "%d", &val) == 1) {
338 pm_trace_enabled = !!val;
344 power_attr(pm_trace);
346 static struct attribute * g[] = {
352 static struct attribute * g[] = {
356 #endif /* CONFIG_PM_TRACE */
358 static struct attribute_group attr_group = {
363 static int __init pm_init(void)
365 int error = subsystem_register(&power_subsys);
367 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
371 core_initcall(pm_init);