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>
23 #include <linux/syscalls.h>
27 BLOCKING_NOTIFIER_HEAD(pm_chain_head
);
29 DEFINE_MUTEX(pm_mutex
);
31 unsigned int pm_flags
;
32 EXPORT_SYMBOL(pm_flags
);
34 #ifdef CONFIG_PM_DEBUG
35 int pm_test_level
= TEST_NONE
;
37 static int suspend_test(int level
)
39 if (pm_test_level
== level
) {
40 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
47 static const char * const pm_tests
[__TEST_AFTER_LAST
] = {
50 [TEST_CPUS
] = "processors",
51 [TEST_PLATFORM
] = "platform",
52 [TEST_DEVICES
] = "devices",
53 [TEST_FREEZER
] = "freezer",
56 static ssize_t
pm_test_show(struct kset
*kset
, char *buf
)
61 for (level
= TEST_FIRST
; level
<= TEST_MAX
; level
++)
62 if (pm_tests
[level
]) {
63 if (level
== pm_test_level
)
64 s
+= sprintf(s
, "[%s] ", pm_tests
[level
]);
66 s
+= sprintf(s
, "%s ", pm_tests
[level
]);
70 /* convert the last space to a newline */
76 static ssize_t
pm_test_store(struct kset
*kset
, const char *buf
, size_t n
)
78 const char * const *s
;
84 p
= memchr(buf
, '\n', n
);
85 len
= p
? p
- buf
: n
;
87 mutex_lock(&pm_mutex
);
90 for (s
= &pm_tests
[level
]; level
<= TEST_MAX
; s
++, level
++)
91 if (*s
&& len
== strlen(*s
) && !strncmp(buf
, *s
, len
)) {
92 pm_test_level
= level
;
97 mutex_unlock(&pm_mutex
);
99 return error
? error
: n
;
103 #else /* !CONFIG_PM_DEBUG */
104 static inline int suspend_test(int level
) { return 0; }
105 #endif /* !CONFIG_PM_DEBUG */
107 #ifdef CONFIG_SUSPEND
109 /* This is just an arbitrary number */
110 #define FREE_PAGE_NUMBER (100)
112 static struct platform_suspend_ops
*suspend_ops
;
115 * suspend_set_ops - Set the global suspend method table.
116 * @ops: Pointer to ops structure.
119 void suspend_set_ops(struct platform_suspend_ops
*ops
)
121 mutex_lock(&pm_mutex
);
123 mutex_unlock(&pm_mutex
);
127 * suspend_valid_only_mem - generic memory-only valid callback
129 * Platform drivers that implement mem suspend only and only need
130 * to check for that in their .valid callback can use this instead
131 * of rolling their own .valid callback.
133 int suspend_valid_only_mem(suspend_state_t state
)
135 return state
== PM_SUSPEND_MEM
;
139 * suspend_prepare - Do prep work before entering low-power state.
141 * This is common code that is called for each state that we're entering.
142 * Run suspend notifiers, allocate a console and stop all processes.
144 static int suspend_prepare(void)
147 unsigned int free_pages
;
149 if (!suspend_ops
|| !suspend_ops
->enter
)
152 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
156 pm_prepare_console();
158 if (freeze_processes()) {
163 free_pages
= global_page_state(NR_FREE_PAGES
);
164 if (free_pages
< FREE_PAGE_NUMBER
) {
165 pr_debug("PM: free some memory\n");
166 shrink_all_memory(FREE_PAGE_NUMBER
- free_pages
);
167 if (nr_free_pages() < FREE_PAGE_NUMBER
) {
169 printk(KERN_ERR
"PM: No enough memory\n");
177 pm_restore_console();
179 pm_notifier_call_chain(PM_POST_SUSPEND
);
183 /* default implementation */
184 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
189 /* default implementation */
190 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
196 * suspend_enter - enter the desired system sleep state.
197 * @state: state to enter
199 * This function should be called after devices have been suspended.
201 static int suspend_enter(suspend_state_t state
)
205 arch_suspend_disable_irqs();
206 BUG_ON(!irqs_disabled());
208 if ((error
= device_power_down(PMSG_SUSPEND
))) {
209 printk(KERN_ERR
"Some devices failed to power down\n");
213 if (!suspend_test(TEST_CORE
))
214 error
= suspend_ops
->enter(state
);
218 arch_suspend_enable_irqs();
219 BUG_ON(irqs_disabled());
224 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
226 * @state: state to enter
228 int suspend_devices_and_enter(suspend_state_t state
)
235 if (suspend_ops
->set_target
) {
236 error
= suspend_ops
->set_target(state
);
241 error
= device_suspend(PMSG_SUSPEND
);
243 printk(KERN_ERR
"Some devices failed to suspend\n");
247 if (suspend_test(TEST_DEVICES
))
250 if (suspend_ops
->prepare
) {
251 error
= suspend_ops
->prepare();
256 if (suspend_test(TEST_PLATFORM
))
259 error
= disable_nonboot_cpus();
260 if (!error
&& !suspend_test(TEST_CPUS
))
261 suspend_enter(state
);
263 enable_nonboot_cpus();
265 if (suspend_ops
->finish
)
266 suspend_ops
->finish();
275 * suspend_finish - Do final work before exiting suspend sequence.
277 * Call platform code to clean up, restart processes, and free the
278 * console that we've allocated. This is not called for suspend-to-disk.
280 static void suspend_finish(void)
283 pm_restore_console();
284 pm_notifier_call_chain(PM_POST_SUSPEND
);
290 static const char * const pm_states
[PM_SUSPEND_MAX
] = {
291 [PM_SUSPEND_STANDBY
] = "standby",
292 [PM_SUSPEND_MEM
] = "mem",
295 static inline int valid_state(suspend_state_t state
)
297 /* All states need lowlevel support and need to be valid
298 * to the lowlevel implementation, no valid callback
299 * implies that none are valid. */
300 if (!suspend_ops
|| !suspend_ops
->valid
|| !suspend_ops
->valid(state
))
307 * enter_state - Do common work of entering low-power state.
308 * @state: pm_state structure for state we're entering.
310 * Make sure we're the only ones trying to enter a sleep state. Fail
311 * if someone has beat us to it, since we don't want anything weird to
312 * happen when we wake up.
313 * Then, do the setup for suspend, enter the state, and cleaup (after
316 static int enter_state(suspend_state_t state
)
320 if (!valid_state(state
))
323 if (!mutex_trylock(&pm_mutex
))
326 printk("Syncing filesystems ... ");
330 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
331 error
= suspend_prepare();
335 if (suspend_test(TEST_FREEZER
))
338 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
339 error
= suspend_devices_and_enter(state
);
342 pr_debug("PM: Finishing wakeup.\n");
345 mutex_unlock(&pm_mutex
);
351 * pm_suspend - Externally visible function for suspending system.
352 * @state: Enumerated value of state to enter.
354 * Determine whether or not value is within range, get state
355 * structure, and enter (above).
358 int pm_suspend(suspend_state_t state
)
360 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
361 return enter_state(state
);
365 EXPORT_SYMBOL(pm_suspend
);
367 #endif /* CONFIG_SUSPEND */
369 struct kobject
*power_kobj
;
372 * state - control system power state.
374 * show() returns what states are supported, which is hard-coded to
375 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
376 * 'disk' (Suspend-to-Disk).
378 * store() accepts one of those strings, translates it into the
379 * proper enumerated value, and initiates a suspend transition.
382 static ssize_t
state_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
386 #ifdef CONFIG_SUSPEND
389 for (i
= 0; i
< PM_SUSPEND_MAX
; i
++) {
390 if (pm_states
[i
] && valid_state(i
))
391 s
+= sprintf(s
,"%s ", pm_states
[i
]);
394 #ifdef CONFIG_HIBERNATION
395 s
+= sprintf(s
, "%s\n", "disk");
398 /* convert the last space to a newline */
404 static ssize_t
state_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
405 const char *buf
, size_t n
)
407 #ifdef CONFIG_SUSPEND
408 suspend_state_t state
= PM_SUSPEND_STANDBY
;
409 const char * const *s
;
415 p
= memchr(buf
, '\n', n
);
416 len
= p
? p
- buf
: n
;
418 /* First, check if we are requested to hibernate */
419 if (len
== 4 && !strncmp(buf
, "disk", len
)) {
424 #ifdef CONFIG_SUSPEND
425 for (s
= &pm_states
[state
]; state
< PM_SUSPEND_MAX
; s
++, state
++) {
426 if (*s
&& len
== strlen(*s
) && !strncmp(buf
, *s
, len
))
429 if (state
< PM_SUSPEND_MAX
&& *s
)
430 error
= enter_state(state
);
434 return error
? error
: n
;
439 #ifdef CONFIG_PM_TRACE
440 int pm_trace_enabled
;
442 static ssize_t
pm_trace_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
445 return sprintf(buf
, "%d\n", pm_trace_enabled
);
449 pm_trace_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
450 const char *buf
, size_t n
)
454 if (sscanf(buf
, "%d", &val
) == 1) {
455 pm_trace_enabled
= !!val
;
461 power_attr(pm_trace
);
462 #endif /* CONFIG_PM_TRACE */
464 static struct attribute
* g
[] = {
466 #ifdef CONFIG_PM_TRACE
469 #ifdef CONFIG_PM_DEBUG
475 static struct attribute_group attr_group
= {
480 static int __init
pm_init(void)
482 power_kobj
= kobject_create_and_add("power", NULL
);
485 return sysfs_create_group(power_kobj
, &attr_group
);
488 core_initcall(pm_init
);