blob: 500eb87f643dd9ff56d08079a29c506e7140b777 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/power/main.c - PM subsystem core functionality.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
8 *
9 */
10
Ralf Baechle4cf30342006-12-06 20:36:06 -080011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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/pm.h>
Andrew Morton6cc07192006-06-22 14:47:18 -070019#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070020#include <linux/cpu.h>
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -070021#include <linux/resume-trace.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080022#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "power.h"
25
David Shaohua Li5ae947e2005-03-18 16:27:13 -050026/*This is just an arbitrary number */
27#define FREE_PAGE_NUMBER (100)
28
Stephen Hemmingera6d70982006-12-06 20:34:35 -080029DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Pavel Machek123d3c12005-11-29 19:34:37 -080031struct pm_ops *pm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
33
34/**
35 * pm_set_ops - Set the global power method table.
36 * @ops: Pointer to ops structure.
37 */
38
39void pm_set_ops(struct pm_ops * ops)
40{
Stephen Hemmingera6d70982006-12-06 20:34:35 -080041 mutex_lock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 pm_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -080043 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46
47/**
48 * suspend_prepare - Do prep work before entering low-power state.
49 * @state: State we're entering.
50 *
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.
54 */
55
56static int suspend_prepare(suspend_state_t state)
57{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070058 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050059 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 if (!pm_ops || !pm_ops->enter)
62 return -EPERM;
63
64 pm_prepare_console();
65
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070066 error = disable_nonboot_cpus();
67 if (error)
Li Shaohua5a72e042005-06-25 14:55:06 -070068 goto Enable_cpu;
Li Shaohua5a72e042005-06-25 14:55:06 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 if (freeze_processes()) {
71 error = -EAGAIN;
72 goto Thaw;
73 }
74
David Shaohua Li5ae947e2005-03-18 16:27:13 -050075 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) {
79 error = -ENOMEM;
80 printk(KERN_ERR "PM: No enough memory\n");
81 goto Thaw;
82 }
83 }
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (pm_ops->prepare) {
86 if ((error = pm_ops->prepare(state)))
87 goto Thaw;
88 }
89
Linus Torvalds557240b2006-06-19 18:16:01 -070090 suspend_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if ((error = device_suspend(PMSG_SUSPEND))) {
92 printk(KERN_ERR "Some devices failed to suspend\n");
93 goto Finish;
94 }
95 return 0;
96 Finish:
97 if (pm_ops->finish)
98 pm_ops->finish(state);
99 Thaw:
100 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700101 Enable_cpu:
102 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 pm_restore_console();
104 return error;
105}
106
107
Luca Tettamanti9b238202006-03-23 03:00:09 -0800108int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 int error = 0;
111 unsigned long flags;
112
113 local_irq_save(flags);
114
115 if ((error = device_power_down(PMSG_SUSPEND))) {
116 printk(KERN_ERR "Some devices failed to power down\n");
117 goto Done;
118 }
119 error = pm_ops->enter(state);
120 device_power_up();
121 Done:
122 local_irq_restore(flags);
123 return error;
124}
125
126
127/**
128 * suspend_finish - Do final work before exiting suspend sequence.
129 * @state: State we're coming out of.
130 *
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.
133 */
134
135static void suspend_finish(suspend_state_t state)
136{
137 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700138 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700140 enable_nonboot_cpus();
David Shaohua Li1a384162005-11-23 12:36:00 -0500141 if (pm_ops && pm_ops->finish)
142 pm_ops->finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 pm_restore_console();
144}
145
146
147
148
Andreas Mohr3b364b82006-06-25 05:47:56 -0700149static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 [PM_SUSPEND_STANDBY] = "standby",
151 [PM_SUSPEND_MEM] = "mem",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700152#ifdef CONFIG_SOFTWARE_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 [PM_SUSPEND_DISK] = "disk",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Pavel Machek123d3c12005-11-29 19:34:37 -0800157static inline int valid_state(suspend_state_t state)
158{
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)
162 return 1;
163
164 if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
165 return 0;
166 return 1;
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/**
171 * enter_state - Do common work of entering low-power state.
172 * @state: pm_state structure for state we're entering.
173 *
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
178 * we've woken up).
179 */
180
181static int enter_state(suspend_state_t state)
182{
183 int error;
184
Pavel Machek123d3c12005-11-29 19:34:37 -0800185 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800186 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800187 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -EBUSY;
189
190 if (state == PM_SUSPEND_DISK) {
191 error = pm_suspend_disk();
192 goto Unlock;
193 }
194
David Brownell82428b62005-05-09 08:07:00 -0700195 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if ((error = suspend_prepare(state)))
197 goto Unlock;
198
David Brownell82428b62005-05-09 08:07:00 -0700199 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 error = suspend_enter(state);
201
David Brownell82428b62005-05-09 08:07:00 -0700202 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 suspend_finish(state);
204 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800205 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return error;
207}
208
209/*
210 * This is main interface to the outside world. It needs to be
211 * called from process context.
212 */
213int software_suspend(void)
214{
215 return enter_state(PM_SUSPEND_DISK);
216}
217
218
219/**
220 * pm_suspend - Externally visible function for suspending system.
221 * @state: Enumarted value of state to enter.
222 *
223 * Determine whether or not value is within range, get state
224 * structure, and enter (above).
225 */
226
227int pm_suspend(suspend_state_t state)
228{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500229 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return enter_state(state);
231 return -EINVAL;
232}
233
Ralf Baechle4cf30342006-12-06 20:36:06 -0800234EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236decl_subsys(power,NULL,NULL);
237
238
239/**
240 * state - control system power state.
241 *
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).
245 *
246 * store() accepts one of those strings, translates it into the
247 * proper enumerated value, and initiates a suspend transition.
248 */
249
250static ssize_t state_show(struct subsystem * subsys, char * buf)
251{
252 int i;
253 char * s = buf;
254
255 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800256 if (pm_states[i] && valid_state(i))
257 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 s += sprintf(s,"\n");
260 return (s - buf);
261}
262
263static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
264{
265 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700266 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 char *p;
268 int error;
269 int len;
270
271 p = memchr(buf, '\n', n);
272 len = p ? p - buf : n;
273
274 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
275 if (*s && !strncmp(buf, *s, len))
276 break;
277 }
dean gaudet47bb7892006-04-27 18:39:17 -0700278 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 error = enter_state(state);
280 else
281 error = -EINVAL;
282 return error ? error : n;
283}
284
285power_attr(state);
286
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700287#ifdef CONFIG_PM_TRACE
288int pm_trace_enabled;
289
290static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
291{
292 return sprintf(buf, "%d\n", pm_trace_enabled);
293}
294
295static ssize_t
296pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
297{
298 int val;
299
300 if (sscanf(buf, "%d", &val) == 1) {
301 pm_trace_enabled = !!val;
302 return n;
303 }
304 return -EINVAL;
305}
306
307power_attr(pm_trace);
308
309static struct attribute * g[] = {
310 &state_attr.attr,
311 &pm_trace_attr.attr,
312 NULL,
313};
314#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315static struct attribute * g[] = {
316 &state_attr.attr,
317 NULL,
318};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700319#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321static struct attribute_group attr_group = {
322 .attrs = g,
323};
324
325
326static int __init pm_init(void)
327{
328 int error = subsystem_register(&power_subsys);
329 if (!error)
330 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
331 return error;
332}
333
334core_initcall(pm_init);