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.
69 * This is common code that is called for each state that we're entering.
70 * Run suspend notifiers, allocate a console and stop all processes.
72 static int suspend_prepare(void)
75 unsigned int free_pages
;
77 if (!pm_ops
|| !pm_ops
->enter
)
80 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
86 if (freeze_processes()) {
91 free_pages
= global_page_state(NR_FREE_PAGES
);
92 if (free_pages
< FREE_PAGE_NUMBER
) {
93 pr_debug("PM: free some memory\n");
94 shrink_all_memory(FREE_PAGE_NUMBER
- free_pages
);
95 if (nr_free_pages() < FREE_PAGE_NUMBER
) {
97 printk(KERN_ERR
"PM: No enough memory\n");
105 pm_restore_console();
107 pm_notifier_call_chain(PM_POST_SUSPEND
);
111 /* default implementation */
112 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
117 /* default implementation */
118 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
124 * suspend_enter - enter the desired system sleep state.
125 * @state: state to enter
127 * This function should be called after devices have been suspended.
129 int suspend_enter(suspend_state_t state
)
133 arch_suspend_disable_irqs();
134 BUG_ON(!irqs_disabled());
136 if ((error
= device_power_down(PMSG_SUSPEND
))) {
137 printk(KERN_ERR
"Some devices failed to power down\n");
140 error
= pm_ops
->enter(state
);
143 arch_suspend_enable_irqs();
144 BUG_ON(irqs_disabled());
149 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
151 * @state: state to enter
153 int suspend_devices_and_enter(suspend_state_t state
)
160 if (pm_ops
->set_target
) {
161 error
= pm_ops
->set_target(state
);
166 error
= device_suspend(PMSG_SUSPEND
);
168 printk(KERN_ERR
"Some devices failed to suspend\n");
171 if (pm_ops
->prepare
) {
172 error
= pm_ops
->prepare(state
);
176 error
= disable_nonboot_cpus();
178 suspend_enter(state
);
180 enable_nonboot_cpus();
190 * suspend_finish - Do final work before exiting suspend sequence.
192 * Call platform code to clean up, restart processes, and free the
193 * console that we've allocated. This is not called for suspend-to-disk.
195 static void suspend_finish(void)
198 pm_restore_console();
199 pm_notifier_call_chain(PM_POST_SUSPEND
);
205 static const char * const pm_states
[PM_SUSPEND_MAX
] = {
206 [PM_SUSPEND_STANDBY
] = "standby",
207 [PM_SUSPEND_MEM
] = "mem",
210 static inline int valid_state(suspend_state_t state
)
212 /* All states need lowlevel support and need to be valid
213 * to the lowlevel implementation, no valid callback
214 * implies that none are valid. */
215 if (!pm_ops
|| !pm_ops
->valid
|| !pm_ops
->valid(state
))
222 * enter_state - Do common work of entering low-power state.
223 * @state: pm_state structure for state we're entering.
225 * Make sure we're the only ones trying to enter a sleep state. Fail
226 * if someone has beat us to it, since we don't want anything weird to
227 * happen when we wake up.
228 * Then, do the setup for suspend, enter the state, and cleaup (after
231 static int enter_state(suspend_state_t state
)
235 if (!valid_state(state
))
237 if (!mutex_trylock(&pm_mutex
))
240 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
241 if ((error
= suspend_prepare()))
244 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
245 error
= suspend_devices_and_enter(state
);
247 pr_debug("PM: Finishing wakeup.\n");
250 mutex_unlock(&pm_mutex
);
256 * pm_suspend - Externally visible function for suspending system.
257 * @state: Enumerated value of state to enter.
259 * Determine whether or not value is within range, get state
260 * structure, and enter (above).
263 int pm_suspend(suspend_state_t state
)
265 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
266 return enter_state(state
);
270 EXPORT_SYMBOL(pm_suspend
);
272 decl_subsys(power
,NULL
,NULL
);
276 * state - control system power state.
278 * show() returns what states are supported, which is hard-coded to
279 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
280 * 'disk' (Suspend-to-Disk).
282 * store() accepts one of those strings, translates it into the
283 * proper enumerated value, and initiates a suspend transition.
286 static ssize_t
state_show(struct kset
*kset
, char *buf
)
291 for (i
= 0; i
< PM_SUSPEND_MAX
; i
++) {
292 if (pm_states
[i
] && valid_state(i
))
293 s
+= sprintf(s
,"%s ", pm_states
[i
]);
295 #ifdef CONFIG_SOFTWARE_SUSPEND
296 s
+= sprintf(s
, "%s\n", "disk");
299 /* convert the last space to a newline */
305 static ssize_t
state_store(struct kset
*kset
, const char *buf
, size_t n
)
307 suspend_state_t state
= PM_SUSPEND_STANDBY
;
308 const char * const *s
;
313 p
= memchr(buf
, '\n', n
);
314 len
= p
? p
- buf
: n
;
316 /* First, check if we are requested to hibernate */
317 if (len
== 4 && !strncmp(buf
, "disk", len
)) {
319 return error
? error
: n
;
322 for (s
= &pm_states
[state
]; state
< PM_SUSPEND_MAX
; s
++, state
++) {
323 if (*s
&& len
== strlen(*s
) && !strncmp(buf
, *s
, len
))
326 if (state
< PM_SUSPEND_MAX
&& *s
)
327 error
= enter_state(state
);
330 return error
? error
: n
;
335 #ifdef CONFIG_PM_TRACE
336 int pm_trace_enabled
;
338 static ssize_t
pm_trace_show(struct kset
*kset
, char *buf
)
340 return sprintf(buf
, "%d\n", pm_trace_enabled
);
344 pm_trace_store(struct kset
*kset
, const char *buf
, size_t n
)
348 if (sscanf(buf
, "%d", &val
) == 1) {
349 pm_trace_enabled
= !!val
;
355 power_attr(pm_trace
);
357 static struct attribute
* g
[] = {
363 static struct attribute
* g
[] = {
367 #endif /* CONFIG_PM_TRACE */
369 static struct attribute_group attr_group
= {
374 static int __init
pm_init(void)
376 int error
= subsystem_register(&power_subsys
);
378 error
= sysfs_create_group(&power_subsys
.kobj
,&attr_group
);
382 core_initcall(pm_init
);