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/suspend.h>
12 #include <linux/kobject.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
18 #include <linux/console.h>
19 #include <linux/cpu.h>
20 #include <linux/resume-trace.h>
24 /*This is just an arbitrary number */
25 #define FREE_PAGE_NUMBER (100)
27 DECLARE_MUTEX(pm_sem
);
29 struct pm_ops
*pm_ops
;
30 suspend_disk_method_t pm_disk_mode
= PM_DISK_SHUTDOWN
;
33 * pm_set_ops - Set the global power method table.
34 * @ops: Pointer to ops structure.
37 void pm_set_ops(struct pm_ops
* ops
)
46 * suspend_prepare - Do prep work before entering low-power state.
47 * @state: State we're entering.
49 * This is common code that is called for each state that we're
50 * entering. Allocate a console, stop all processes, then make sure
51 * the platform can enter the requested state.
54 static int suspend_prepare(suspend_state_t state
)
57 unsigned int free_pages
;
59 if (!pm_ops
|| !pm_ops
->enter
)
64 error
= disable_nonboot_cpus();
68 if (freeze_processes()) {
73 if ((free_pages
= nr_free_pages()) < FREE_PAGE_NUMBER
) {
74 pr_debug("PM: free some memory\n");
75 shrink_all_memory(FREE_PAGE_NUMBER
- free_pages
);
76 if (nr_free_pages() < FREE_PAGE_NUMBER
) {
78 printk(KERN_ERR
"PM: No enough memory\n");
83 if (pm_ops
->prepare
) {
84 if ((error
= pm_ops
->prepare(state
)))
89 if ((error
= device_suspend(PMSG_SUSPEND
))) {
90 printk(KERN_ERR
"Some devices failed to suspend\n");
96 pm_ops
->finish(state
);
100 enable_nonboot_cpus();
101 pm_restore_console();
106 int suspend_enter(suspend_state_t state
)
111 local_irq_save(flags
);
113 if ((error
= device_power_down(PMSG_SUSPEND
))) {
114 printk(KERN_ERR
"Some devices failed to power down\n");
117 error
= pm_ops
->enter(state
);
120 local_irq_restore(flags
);
126 * suspend_finish - Do final work before exiting suspend sequence.
127 * @state: State we're coming out of.
129 * Call platform code to clean up, restart processes, and free the
130 * console that we've allocated. This is not called for suspend-to-disk.
133 static void suspend_finish(suspend_state_t state
)
138 enable_nonboot_cpus();
139 if (pm_ops
&& pm_ops
->finish
)
140 pm_ops
->finish(state
);
141 pm_restore_console();
147 static const char * const pm_states
[PM_SUSPEND_MAX
] = {
148 [PM_SUSPEND_STANDBY
] = "standby",
149 [PM_SUSPEND_MEM
] = "mem",
150 #ifdef CONFIG_SOFTWARE_SUSPEND
151 [PM_SUSPEND_DISK
] = "disk",
155 static inline int valid_state(suspend_state_t state
)
157 /* Suspend-to-disk does not really need low-level support.
158 * It can work with reboot if needed. */
159 if (state
== PM_SUSPEND_DISK
)
162 if (pm_ops
&& pm_ops
->valid
&& !pm_ops
->valid(state
))
169 * enter_state - Do common work of entering low-power state.
170 * @state: pm_state structure for state we're entering.
172 * Make sure we're the only ones trying to enter a sleep state. Fail
173 * if someone has beat us to it, since we don't want anything weird to
174 * happen when we wake up.
175 * Then, do the setup for suspend, enter the state, and cleaup (after
179 static int enter_state(suspend_state_t state
)
183 if (!valid_state(state
))
185 if (down_trylock(&pm_sem
))
188 if (state
== PM_SUSPEND_DISK
) {
189 error
= pm_suspend_disk();
193 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
194 if ((error
= suspend_prepare(state
)))
197 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
198 error
= suspend_enter(state
);
200 pr_debug("PM: Finishing wakeup.\n");
201 suspend_finish(state
);
208 * This is main interface to the outside world. It needs to be
209 * called from process context.
211 int software_suspend(void)
213 return enter_state(PM_SUSPEND_DISK
);
218 * pm_suspend - Externally visible function for suspending system.
219 * @state: Enumarted value of state to enter.
221 * Determine whether or not value is within range, get state
222 * structure, and enter (above).
225 int pm_suspend(suspend_state_t state
)
227 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
228 return enter_state(state
);
234 decl_subsys(power
,NULL
,NULL
);
238 * state - control system power state.
240 * show() returns what states are supported, which is hard-coded to
241 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
242 * 'disk' (Suspend-to-Disk).
244 * store() accepts one of those strings, translates it into the
245 * proper enumerated value, and initiates a suspend transition.
248 static ssize_t
state_show(struct subsystem
* subsys
, char * buf
)
253 for (i
= 0; i
< PM_SUSPEND_MAX
; i
++) {
254 if (pm_states
[i
] && valid_state(i
))
255 s
+= sprintf(s
,"%s ", pm_states
[i
]);
257 s
+= sprintf(s
,"\n");
261 static ssize_t
state_store(struct subsystem
* subsys
, const char * buf
, size_t n
)
263 suspend_state_t state
= PM_SUSPEND_STANDBY
;
264 const char * const *s
;
269 p
= memchr(buf
, '\n', n
);
270 len
= p
? p
- buf
: n
;
272 for (s
= &pm_states
[state
]; state
< PM_SUSPEND_MAX
; s
++, state
++) {
273 if (*s
&& !strncmp(buf
, *s
, len
))
276 if (state
< PM_SUSPEND_MAX
&& *s
)
277 error
= enter_state(state
);
280 return error
? error
: n
;
285 #ifdef CONFIG_PM_TRACE
286 int pm_trace_enabled
;
288 static ssize_t
pm_trace_show(struct subsystem
* subsys
, char * buf
)
290 return sprintf(buf
, "%d\n", pm_trace_enabled
);
294 pm_trace_store(struct subsystem
* subsys
, const char * buf
, size_t n
)
298 if (sscanf(buf
, "%d", &val
) == 1) {
299 pm_trace_enabled
= !!val
;
305 power_attr(pm_trace
);
307 static struct attribute
* g
[] = {
313 static struct attribute
* g
[] = {
317 #endif /* CONFIG_PM_TRACE */
319 static struct attribute_group attr_group
= {
324 static int __init
pm_init(void)
326 int error
= subsystem_register(&power_subsys
);
328 error
= sysfs_create_group(&power_subsys
.kset
.kobj
,&attr_group
);
332 core_initcall(pm_init
);