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>
26 /*This is just an arbitrary number */
27 #define FREE_PAGE_NUMBER (100)
29 DEFINE_MUTEX(pm_mutex
);
31 struct pm_ops
*pm_ops
;
32 suspend_disk_method_t pm_disk_mode
= PM_DISK_PLATFORM
;
35 * pm_set_ops - Set the global power method table.
36 * @ops: Pointer to ops structure.
39 void pm_set_ops(struct pm_ops
* ops
)
41 mutex_lock(&pm_mutex
);
43 mutex_unlock(&pm_mutex
);
48 * suspend_prepare - Do prep work before entering low-power state.
49 * @state: State we're entering.
51 * This is common code that is called for each state that we're
52 * entering. Allocate a console, stop all processes, then make sure
53 * the platform can enter the requested state.
56 static int suspend_prepare(suspend_state_t state
)
59 unsigned int free_pages
;
61 if (!pm_ops
|| !pm_ops
->enter
)
66 error
= disable_nonboot_cpus();
70 if (freeze_processes()) {
75 if ((free_pages
= nr_free_pages()) < FREE_PAGE_NUMBER
) {
76 pr_debug("PM: free some memory\n");
77 shrink_all_memory(FREE_PAGE_NUMBER
- free_pages
);
78 if (nr_free_pages() < FREE_PAGE_NUMBER
) {
80 printk(KERN_ERR
"PM: No enough memory\n");
85 if (pm_ops
->prepare
) {
86 if ((error
= pm_ops
->prepare(state
)))
91 if ((error
= device_suspend(PMSG_SUSPEND
))) {
92 printk(KERN_ERR
"Some devices failed to suspend\n");
98 pm_ops
->finish(state
);
102 enable_nonboot_cpus();
103 pm_restore_console();
108 int suspend_enter(suspend_state_t state
)
113 local_irq_save(flags
);
115 if ((error
= device_power_down(PMSG_SUSPEND
))) {
116 printk(KERN_ERR
"Some devices failed to power down\n");
119 error
= pm_ops
->enter(state
);
122 local_irq_restore(flags
);
128 * suspend_finish - Do final work before exiting suspend sequence.
129 * @state: State we're coming out of.
131 * Call platform code to clean up, restart processes, and free the
132 * console that we've allocated. This is not called for suspend-to-disk.
135 static void suspend_finish(suspend_state_t state
)
140 enable_nonboot_cpus();
141 if (pm_ops
&& pm_ops
->finish
)
142 pm_ops
->finish(state
);
143 pm_restore_console();
149 static const char * const pm_states
[PM_SUSPEND_MAX
] = {
150 [PM_SUSPEND_STANDBY
] = "standby",
151 [PM_SUSPEND_MEM
] = "mem",
152 #ifdef CONFIG_SOFTWARE_SUSPEND
153 [PM_SUSPEND_DISK
] = "disk",
157 static inline int valid_state(suspend_state_t state
)
159 /* Suspend-to-disk does not really need low-level support.
160 * It can work with reboot if needed. */
161 if (state
== PM_SUSPEND_DISK
)
164 if (pm_ops
&& pm_ops
->valid
&& !pm_ops
->valid(state
))
171 * enter_state - Do common work of entering low-power state.
172 * @state: pm_state structure for state we're entering.
174 * Make sure we're the only ones trying to enter a sleep state. Fail
175 * if someone has beat us to it, since we don't want anything weird to
176 * happen when we wake up.
177 * Then, do the setup for suspend, enter the state, and cleaup (after
181 static int enter_state(suspend_state_t state
)
185 if (!valid_state(state
))
187 if (!mutex_trylock(&pm_mutex
))
190 if (state
== PM_SUSPEND_DISK
) {
191 error
= pm_suspend_disk();
195 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
196 if ((error
= suspend_prepare(state
)))
199 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
200 error
= suspend_enter(state
);
202 pr_debug("PM: Finishing wakeup.\n");
203 suspend_finish(state
);
205 mutex_unlock(&pm_mutex
);
210 * This is main interface to the outside world. It needs to be
211 * called from process context.
213 int software_suspend(void)
215 return enter_state(PM_SUSPEND_DISK
);
220 * pm_suspend - Externally visible function for suspending system.
221 * @state: Enumarted value of state to enter.
223 * Determine whether or not value is within range, get state
224 * structure, and enter (above).
227 int pm_suspend(suspend_state_t state
)
229 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
230 return enter_state(state
);
234 EXPORT_SYMBOL(pm_suspend
);
236 decl_subsys(power
,NULL
,NULL
);
240 * state - control system power state.
242 * show() returns what states are supported, which is hard-coded to
243 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
244 * 'disk' (Suspend-to-Disk).
246 * store() accepts one of those strings, translates it into the
247 * proper enumerated value, and initiates a suspend transition.
250 static ssize_t
state_show(struct subsystem
* subsys
, char * buf
)
255 for (i
= 0; i
< PM_SUSPEND_MAX
; i
++) {
256 if (pm_states
[i
] && valid_state(i
))
257 s
+= sprintf(s
,"%s ", pm_states
[i
]);
259 s
+= sprintf(s
,"\n");
263 static ssize_t
state_store(struct subsystem
* subsys
, const char * buf
, size_t n
)
265 suspend_state_t state
= PM_SUSPEND_STANDBY
;
266 const char * const *s
;
271 p
= memchr(buf
, '\n', n
);
272 len
= p
? p
- buf
: n
;
274 for (s
= &pm_states
[state
]; state
< PM_SUSPEND_MAX
; s
++, state
++) {
275 if (*s
&& !strncmp(buf
, *s
, len
))
278 if (state
< PM_SUSPEND_MAX
&& *s
)
279 error
= enter_state(state
);
282 return error
? error
: n
;
287 #ifdef CONFIG_PM_TRACE
288 int pm_trace_enabled
;
290 static ssize_t
pm_trace_show(struct subsystem
* subsys
, char * buf
)
292 return sprintf(buf
, "%d\n", pm_trace_enabled
);
296 pm_trace_store(struct subsystem
* subsys
, const char * buf
, size_t n
)
300 if (sscanf(buf
, "%d", &val
) == 1) {
301 pm_trace_enabled
= !!val
;
307 power_attr(pm_trace
);
309 static struct attribute
* g
[] = {
315 static struct attribute
* g
[] = {
319 #endif /* CONFIG_PM_TRACE */
321 static struct attribute_group attr_group
= {
326 static int __init
pm_init(void)
328 int error
= subsystem_register(&power_subsys
);
330 error
= sysfs_create_group(&power_subsys
.kset
.kobj
,&attr_group
);
334 core_initcall(pm_init
);