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);
52 * pm_valid_only_mem - generic memory-only valid callback
54 * pm_ops drivers that implement mem suspend only and only need
55 * to check for that in their .valid callback can use this instead
56 * of rolling their own .valid callback.
58 int pm_valid_only_mem(suspend_state_t state)
60 return state == PM_SUSPEND_MEM;
64 static inline void pm_finish(suspend_state_t state)
67 pm_ops->finish(state);
71 * suspend_prepare - Do prep work before entering low-power state.
72 * @state: State we're entering.
74 * This is common code that is called for each state that we're
75 * entering. Allocate a console, stop all processes, then make sure
76 * the platform can enter the requested state.
79 static int suspend_prepare(suspend_state_t state)
82 unsigned int free_pages;
84 if (!pm_ops || !pm_ops->enter)
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->prepare) {
106 if ((error = pm_ops->prepare(state)))
111 error = device_suspend(PMSG_SUSPEND);
113 printk(KERN_ERR "Some devices failed to suspend\n");
116 error = disable_nonboot_cpus();
120 enable_nonboot_cpus();
127 pm_restore_console();
131 /* default implementation */
132 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
137 /* default implementation */
138 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
143 int suspend_enter(suspend_state_t state)
147 arch_suspend_disable_irqs();
148 BUG_ON(!irqs_disabled());
150 if ((error = device_power_down(PMSG_SUSPEND))) {
151 printk(KERN_ERR "Some devices failed to power down\n");
154 error = pm_ops->enter(state);
157 arch_suspend_enable_irqs();
158 BUG_ON(irqs_disabled());
164 * suspend_finish - Do final work before exiting suspend sequence.
165 * @state: State we're coming out of.
167 * Call platform code to clean up, restart processes, and free the
168 * console that we've allocated. This is not called for suspend-to-disk.
171 static void suspend_finish(suspend_state_t state)
173 enable_nonboot_cpus();
178 pm_restore_console();
184 static const char * const pm_states[PM_SUSPEND_MAX] = {
185 [PM_SUSPEND_STANDBY] = "standby",
186 [PM_SUSPEND_MEM] = "mem",
187 [PM_SUSPEND_DISK] = "disk",
190 static inline int valid_state(suspend_state_t state)
192 /* Suspend-to-disk does not really need low-level support.
193 * It can work with shutdown/reboot if needed. If it isn't
194 * configured, then it cannot be supported.
196 if (state == PM_SUSPEND_DISK)
197 #ifdef CONFIG_SOFTWARE_SUSPEND
203 /* all other states need lowlevel support and need to be
204 * valid to the lowlevel implementation, no valid callback
205 * implies that none are valid. */
206 if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
213 * enter_state - Do common work of entering low-power state.
214 * @state: pm_state structure for state we're entering.
216 * Make sure we're the only ones trying to enter a sleep state. Fail
217 * if someone has beat us to it, since we don't want anything weird to
218 * happen when we wake up.
219 * Then, do the setup for suspend, enter the state, and cleaup (after
223 static int enter_state(suspend_state_t state)
227 if (!valid_state(state))
229 if (!mutex_trylock(&pm_mutex))
232 if (state == PM_SUSPEND_DISK) {
233 error = pm_suspend_disk();
237 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
238 if ((error = suspend_prepare(state)))
241 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
242 error = suspend_enter(state);
244 pr_debug("PM: Finishing wakeup.\n");
245 suspend_finish(state);
247 mutex_unlock(&pm_mutex);
253 * pm_suspend - Externally visible function for suspending system.
254 * @state: Enumarted value of state to enter.
256 * Determine whether or not value is within range, get state
257 * structure, and enter (above).
260 int pm_suspend(suspend_state_t state)
262 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
263 return enter_state(state);
267 EXPORT_SYMBOL(pm_suspend);
269 decl_subsys(power,NULL,NULL);
273 * state - control system power state.
275 * show() returns what states are supported, which is hard-coded to
276 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
277 * 'disk' (Suspend-to-Disk).
279 * store() accepts one of those strings, translates it into the
280 * proper enumerated value, and initiates a suspend transition.
283 static ssize_t state_show(struct kset *kset, char *buf)
288 for (i = 0; i < PM_SUSPEND_MAX; i++) {
289 if (pm_states[i] && valid_state(i))
290 s += sprintf(s,"%s ", pm_states[i]);
292 s += sprintf(s,"\n");
296 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
298 suspend_state_t state = PM_SUSPEND_STANDBY;
299 const char * const *s;
304 p = memchr(buf, '\n', n);
305 len = p ? p - buf : n;
307 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
308 if (*s && !strncmp(buf, *s, len))
311 if (state < PM_SUSPEND_MAX && *s)
312 error = enter_state(state);
315 return error ? error : n;
320 #ifdef CONFIG_PM_TRACE
321 int pm_trace_enabled;
323 static ssize_t pm_trace_show(struct kset *kset, char *buf)
325 return sprintf(buf, "%d\n", pm_trace_enabled);
329 pm_trace_store(struct kset *kset, const char *buf, size_t n)
333 if (sscanf(buf, "%d", &val) == 1) {
334 pm_trace_enabled = !!val;
340 power_attr(pm_trace);
342 static struct attribute * g[] = {
348 static struct attribute * g[] = {
352 #endif /* CONFIG_PM_TRACE */
354 static struct attribute_group attr_group = {
359 static int __init pm_init(void)
361 int error = subsystem_register(&power_subsys);
363 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
367 core_initcall(pm_init);