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 DEFINE_MUTEX(pm_mutex
);
32 /* This is just an arbitrary number */
33 #define FREE_PAGE_NUMBER (100)
35 struct platform_suspend_ops
*suspend_ops
;
38 * suspend_set_ops - Set the global suspend method table.
39 * @ops: Pointer to ops structure.
42 void suspend_set_ops(struct platform_suspend_ops
*ops
)
44 mutex_lock(&pm_mutex
);
46 mutex_unlock(&pm_mutex
);
50 * suspend_valid_only_mem - generic memory-only valid callback
52 * Platform drivers that implement mem suspend only and only need
53 * to check for that in their .valid callback can use this instead
54 * of rolling their own .valid callback.
56 int suspend_valid_only_mem(suspend_state_t state
)
58 return state
== PM_SUSPEND_MEM
;
62 * suspend_prepare - Do prep work before entering low-power state.
64 * This is common code that is called for each state that we're entering.
65 * Run suspend notifiers, allocate a console and stop all processes.
67 static int suspend_prepare(void)
70 unsigned int free_pages
;
72 if (!suspend_ops
|| !suspend_ops
->enter
)
75 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
81 if (freeze_processes()) {
86 free_pages
= global_page_state(NR_FREE_PAGES
);
87 if (free_pages
< FREE_PAGE_NUMBER
) {
88 pr_debug("PM: free some memory\n");
89 shrink_all_memory(FREE_PAGE_NUMBER
- free_pages
);
90 if (nr_free_pages() < FREE_PAGE_NUMBER
) {
92 printk(KERN_ERR
"PM: No enough memory\n");
100 pm_restore_console();
102 pm_notifier_call_chain(PM_POST_SUSPEND
);
106 /* default implementation */
107 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
112 /* default implementation */
113 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
119 * suspend_enter - enter the desired system sleep state.
120 * @state: state to enter
122 * This function should be called after devices have been suspended.
124 static int suspend_enter(suspend_state_t state
)
128 arch_suspend_disable_irqs();
129 BUG_ON(!irqs_disabled());
131 if ((error
= device_power_down(PMSG_SUSPEND
))) {
132 printk(KERN_ERR
"Some devices failed to power down\n");
135 error
= suspend_ops
->enter(state
);
138 arch_suspend_enable_irqs();
139 BUG_ON(irqs_disabled());
144 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
146 * @state: state to enter
148 int suspend_devices_and_enter(suspend_state_t state
)
155 if (suspend_ops
->set_target
) {
156 error
= suspend_ops
->set_target(state
);
161 error
= device_suspend(PMSG_SUSPEND
);
163 printk(KERN_ERR
"Some devices failed to suspend\n");
166 if (suspend_ops
->prepare
) {
167 error
= suspend_ops
->prepare();
171 error
= disable_nonboot_cpus();
173 suspend_enter(state
);
175 enable_nonboot_cpus();
176 if (suspend_ops
->finish
)
177 suspend_ops
->finish();
186 * suspend_finish - Do final work before exiting suspend sequence.
188 * Call platform code to clean up, restart processes, and free the
189 * console that we've allocated. This is not called for suspend-to-disk.
191 static void suspend_finish(void)
194 pm_restore_console();
195 pm_notifier_call_chain(PM_POST_SUSPEND
);
201 static const char * const pm_states
[PM_SUSPEND_MAX
] = {
202 [PM_SUSPEND_STANDBY
] = "standby",
203 [PM_SUSPEND_MEM
] = "mem",
206 static inline int valid_state(suspend_state_t state
)
208 /* All states need lowlevel support and need to be valid
209 * to the lowlevel implementation, no valid callback
210 * implies that none are valid. */
211 if (!suspend_ops
|| !suspend_ops
->valid
|| !suspend_ops
->valid(state
))
218 * enter_state - Do common work of entering low-power state.
219 * @state: pm_state structure for state we're entering.
221 * Make sure we're the only ones trying to enter a sleep state. Fail
222 * if someone has beat us to it, since we don't want anything weird to
223 * happen when we wake up.
224 * Then, do the setup for suspend, enter the state, and cleaup (after
227 static int enter_state(suspend_state_t state
)
231 if (!valid_state(state
))
233 if (!mutex_trylock(&pm_mutex
))
236 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
237 if ((error
= suspend_prepare()))
240 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
241 error
= suspend_devices_and_enter(state
);
243 pr_debug("PM: Finishing wakeup.\n");
246 mutex_unlock(&pm_mutex
);
252 * pm_suspend - Externally visible function for suspending system.
253 * @state: Enumerated value of state to enter.
255 * Determine whether or not value is within range, get state
256 * structure, and enter (above).
259 int pm_suspend(suspend_state_t state
)
261 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
262 return enter_state(state
);
266 EXPORT_SYMBOL(pm_suspend
);
268 #endif /* CONFIG_SUSPEND */
270 decl_subsys(power
,NULL
,NULL
);
274 * state - control system power state.
276 * show() returns what states are supported, which is hard-coded to
277 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
278 * 'disk' (Suspend-to-Disk).
280 * store() accepts one of those strings, translates it into the
281 * proper enumerated value, and initiates a suspend transition.
284 static ssize_t
state_show(struct kset
*kset
, char *buf
)
287 #ifdef CONFIG_SUSPEND
290 for (i
= 0; i
< PM_SUSPEND_MAX
; i
++) {
291 if (pm_states
[i
] && valid_state(i
))
292 s
+= sprintf(s
,"%s ", pm_states
[i
]);
295 #ifdef CONFIG_HIBERNATION
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 #ifdef CONFIG_SUSPEND
308 suspend_state_t state
= PM_SUSPEND_STANDBY
;
309 const char * const *s
;
315 p
= memchr(buf
, '\n', n
);
316 len
= p
? p
- buf
: n
;
318 /* First, check if we are requested to hibernate */
319 if (len
== 4 && !strncmp(buf
, "disk", len
)) {
324 #ifdef CONFIG_SUSPEND
325 for (s
= &pm_states
[state
]; state
< PM_SUSPEND_MAX
; s
++, state
++) {
326 if (*s
&& len
== strlen(*s
) && !strncmp(buf
, *s
, len
))
329 if (state
< PM_SUSPEND_MAX
&& *s
)
330 error
= enter_state(state
);
334 return error
? error
: n
;
339 #ifdef CONFIG_PM_TRACE
340 int pm_trace_enabled
;
342 static ssize_t
pm_trace_show(struct kset
*kset
, char *buf
)
344 return sprintf(buf
, "%d\n", pm_trace_enabled
);
348 pm_trace_store(struct kset
*kset
, const char *buf
, size_t n
)
352 if (sscanf(buf
, "%d", &val
) == 1) {
353 pm_trace_enabled
= !!val
;
359 power_attr(pm_trace
);
361 static struct attribute
* g
[] = {
367 static struct attribute
* g
[] = {
371 #endif /* CONFIG_PM_TRACE */
373 static struct attribute_group attr_group
= {
378 static int __init
pm_init(void)
380 int error
= subsystem_register(&power_subsys
);
382 error
= sysfs_create_group(&power_subsys
.kobj
,&attr_group
);
386 core_initcall(pm_init
);