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;
33 suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
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 if (ops && ops->pm_disk_mode != PM_DISK_INVALID) {
45 pm_disk_mode = ops->pm_disk_mode;
47 pm_disk_mode = PM_DISK_SHUTDOWN;
48 mutex_unlock(&pm_mutex);
51 static inline void pm_finish(suspend_state_t state)
54 pm_ops->finish(state);
58 * suspend_prepare - Do prep work before entering low-power state.
59 * @state: State we're entering.
61 * This is common code that is called for each state that we're
62 * entering. Allocate a console, stop all processes, then make sure
63 * the platform can enter the requested state.
66 static int suspend_prepare(suspend_state_t state)
69 unsigned int free_pages;
71 if (!pm_ops || !pm_ops->enter)
76 if (freeze_processes()) {
81 if ((free_pages = global_page_state(NR_FREE_PAGES))
83 pr_debug("PM: free some memory\n");
84 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
85 if (nr_free_pages() < FREE_PAGE_NUMBER) {
87 printk(KERN_ERR "PM: No enough memory\n");
92 if (pm_ops->prepare) {
93 if ((error = pm_ops->prepare(state)))
98 error = device_suspend(PMSG_SUSPEND);
100 printk(KERN_ERR "Some devices failed to suspend\n");
103 error = disable_nonboot_cpus();
107 enable_nonboot_cpus();
114 pm_restore_console();
118 /* default implementation */
119 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
124 /* default implementation */
125 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
130 int suspend_enter(suspend_state_t state)
134 arch_suspend_disable_irqs();
135 BUG_ON(!irqs_disabled());
137 if ((error = device_power_down(PMSG_SUSPEND))) {
138 printk(KERN_ERR "Some devices failed to power down\n");
141 error = pm_ops->enter(state);
144 arch_suspend_enable_irqs();
145 BUG_ON(irqs_disabled());
151 * suspend_finish - Do final work before exiting suspend sequence.
152 * @state: State we're coming out of.
154 * Call platform code to clean up, restart processes, and free the
155 * console that we've allocated. This is not called for suspend-to-disk.
158 static void suspend_finish(suspend_state_t state)
160 enable_nonboot_cpus();
165 pm_restore_console();
171 static const char * const pm_states[PM_SUSPEND_MAX] = {
172 [PM_SUSPEND_STANDBY] = "standby",
173 [PM_SUSPEND_MEM] = "mem",
174 #ifdef CONFIG_SOFTWARE_SUSPEND
175 [PM_SUSPEND_DISK] = "disk",
179 static inline int valid_state(suspend_state_t state)
181 /* Suspend-to-disk does not really need low-level support.
182 * It can work with reboot if needed. */
183 if (state == PM_SUSPEND_DISK)
186 /* all other states need lowlevel support and need to be
187 * valid to the lowlevel implementation, no valid callback
188 * implies that all 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 if (state == PM_SUSPEND_DISK) {
216 error = pm_suspend_disk();
220 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
221 if ((error = suspend_prepare(state)))
224 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
225 error = suspend_enter(state);
227 pr_debug("PM: Finishing wakeup.\n");
228 suspend_finish(state);
230 mutex_unlock(&pm_mutex);
235 * This is main interface to the outside world. It needs to be
236 * called from process context.
238 int software_suspend(void)
240 return enter_state(PM_SUSPEND_DISK);
245 * pm_suspend - Externally visible function for suspending system.
246 * @state: Enumarted 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 subsystem * subsys, 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 s += sprintf(s,"\n");
288 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
290 suspend_state_t state = PM_SUSPEND_STANDBY;
291 const char * const *s;
296 p = memchr(buf, '\n', n);
297 len = p ? p - buf : n;
299 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
300 if (*s && !strncmp(buf, *s, len))
303 if (state < PM_SUSPEND_MAX && *s)
304 error = enter_state(state);
307 return error ? error : n;
312 #ifdef CONFIG_PM_TRACE
313 int pm_trace_enabled;
315 static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
317 return sprintf(buf, "%d\n", pm_trace_enabled);
321 pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
325 if (sscanf(buf, "%d", &val) == 1) {
326 pm_trace_enabled = !!val;
332 power_attr(pm_trace);
334 static struct attribute * g[] = {
340 static struct attribute * g[] = {
344 #endif /* CONFIG_PM_TRACE */
346 static struct attribute_group attr_group = {
351 static int __init pm_init(void)
353 int error = subsystem_register(&power_subsys);
355 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
359 core_initcall(pm_init);