blob: f94f4e20115a665ae8cb20c3f6a7f0c4c0c9cd0e [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>
Christoph Lameter96177292007-02-10 01:43:03 -080023#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "power.h"
26
David Shaohua Li5ae947e2005-03-18 16:27:13 -050027/*This is just an arbitrary number */
28#define FREE_PAGE_NUMBER (100)
29
Stephen Hemmingera6d70982006-12-06 20:34:35 -080030DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Pavel Machek123d3c12005-11-29 19:34:37 -080032struct pm_ops *pm_ops;
Johannes Bergfe0c9352007-04-30 15:09:51 -070033suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/**
36 * pm_set_ops - Set the global power method table.
37 * @ops: Pointer to ops structure.
38 */
39
40void pm_set_ops(struct pm_ops * ops)
41{
Stephen Hemmingera6d70982006-12-06 20:34:35 -080042 mutex_lock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 pm_ops = ops;
Johannes Bergfe0c9352007-04-30 15:09:51 -070044 if (ops && ops->pm_disk_mode != PM_DISK_INVALID) {
45 pm_disk_mode = ops->pm_disk_mode;
46 } else
47 pm_disk_mode = PM_DISK_SHUTDOWN;
Stephen Hemmingera6d70982006-12-06 20:34:35 -080048 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Johannes Berge8c9c502007-04-30 15:09:54 -070051/**
52 * pm_valid_only_mem - generic memory-only valid callback
53 *
54 * pm_ops drivers that implement mem suspend only and only need
55 * to check for that in their .valid callback can use this instead
56 * of rolling their own .valid callback.
57 */
58int pm_valid_only_mem(suspend_state_t state)
59{
60 return state == PM_SUSPEND_MEM;
61}
62
63
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080064static inline void pm_finish(suspend_state_t state)
65{
66 if (pm_ops->finish)
67 pm_ops->finish(state);
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/**
71 * suspend_prepare - Do prep work before entering low-power state.
72 * @state: State we're entering.
73 *
74 * This is common code that is called for each state that we're
75 * entering. Allocate a console, stop all processes, then make sure
76 * the platform can enter the requested state.
77 */
78
79static int suspend_prepare(suspend_state_t state)
80{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070081 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050082 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 if (!pm_ops || !pm_ops->enter)
85 return -EPERM;
86
87 pm_prepare_console();
88
89 if (freeze_processes()) {
90 error = -EAGAIN;
91 goto Thaw;
92 }
93
Christoph Lameter96177292007-02-10 01:43:03 -080094 if ((free_pages = global_page_state(NR_FREE_PAGES))
95 < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050096 pr_debug("PM: free some memory\n");
97 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
98 if (nr_free_pages() < FREE_PAGE_NUMBER) {
99 error = -ENOMEM;
100 printk(KERN_ERR "PM: No enough memory\n");
101 goto Thaw;
102 }
103 }
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 if (pm_ops->prepare) {
106 if ((error = pm_ops->prepare(state)))
107 goto Thaw;
108 }
109
Linus Torvalds557240b2006-06-19 18:16:01 -0700110 suspend_console();
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800111 error = device_suspend(PMSG_SUSPEND);
112 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 printk(KERN_ERR "Some devices failed to suspend\n");
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800114 goto Resume_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800116 error = disable_nonboot_cpus();
117 if (!error)
118 return 0;
119
120 enable_nonboot_cpus();
121 Resume_devices:
122 pm_finish(state);
123 device_resume();
124 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 Thaw:
126 thaw_processes();
127 pm_restore_console();
128 return error;
129}
130
Johannes Berga53c46d2007-04-26 11:43:58 +0200131/* default implementation */
132void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
133{
134 local_irq_disable();
135}
136
137/* default implementation */
138void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
139{
140 local_irq_enable();
141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Luca Tettamanti9b238202006-03-23 03:00:09 -0800143int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Johannes Berga53c46d2007-04-26 11:43:58 +0200147 arch_suspend_disable_irqs();
148 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if ((error = device_power_down(PMSG_SUSPEND))) {
151 printk(KERN_ERR "Some devices failed to power down\n");
152 goto Done;
153 }
154 error = pm_ops->enter(state);
155 device_power_up();
156 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200157 arch_suspend_enable_irqs();
158 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return error;
160}
161
162
163/**
164 * suspend_finish - Do final work before exiting suspend sequence.
165 * @state: State we're coming out of.
166 *
167 * Call platform code to clean up, restart processes, and free the
168 * console that we've allocated. This is not called for suspend-to-disk.
169 */
170
171static void suspend_finish(suspend_state_t state)
172{
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800173 enable_nonboot_cpus();
174 pm_finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700176 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 thaw_processes();
178 pm_restore_console();
179}
180
181
182
183
Andreas Mohr3b364b82006-06-25 05:47:56 -0700184static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 [PM_SUSPEND_STANDBY] = "standby",
186 [PM_SUSPEND_MEM] = "mem",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700187#ifdef CONFIG_SOFTWARE_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 [PM_SUSPEND_DISK] = "disk",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700189#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
191
Pavel Machek123d3c12005-11-29 19:34:37 -0800192static inline int valid_state(suspend_state_t state)
193{
194 /* Suspend-to-disk does not really need low-level support.
195 * It can work with reboot if needed. */
196 if (state == PM_SUSPEND_DISK)
197 return 1;
198
Johannes Berg9c372d02007-02-16 01:38:29 -0800199 /* all other states need lowlevel support and need to be
200 * valid to the lowlevel implementation, no valid callback
201 * implies that all are valid. */
202 if (!pm_ops || (pm_ops->valid && !pm_ops->valid(state)))
Pavel Machek123d3c12005-11-29 19:34:37 -0800203 return 0;
204 return 1;
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208/**
209 * enter_state - Do common work of entering low-power state.
210 * @state: pm_state structure for state we're entering.
211 *
212 * Make sure we're the only ones trying to enter a sleep state. Fail
213 * if someone has beat us to it, since we don't want anything weird to
214 * happen when we wake up.
215 * Then, do the setup for suspend, enter the state, and cleaup (after
216 * we've woken up).
217 */
218
219static int enter_state(suspend_state_t state)
220{
221 int error;
222
Pavel Machek123d3c12005-11-29 19:34:37 -0800223 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800224 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800225 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -EBUSY;
227
228 if (state == PM_SUSPEND_DISK) {
229 error = pm_suspend_disk();
230 goto Unlock;
231 }
232
David Brownell82428b62005-05-09 08:07:00 -0700233 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if ((error = suspend_prepare(state)))
235 goto Unlock;
236
David Brownell82428b62005-05-09 08:07:00 -0700237 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 error = suspend_enter(state);
239
David Brownell82428b62005-05-09 08:07:00 -0700240 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 suspend_finish(state);
242 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800243 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return error;
245}
246
247/*
248 * This is main interface to the outside world. It needs to be
249 * called from process context.
250 */
251int software_suspend(void)
252{
253 return enter_state(PM_SUSPEND_DISK);
254}
255
256
257/**
258 * pm_suspend - Externally visible function for suspending system.
259 * @state: Enumarted value of state to enter.
260 *
261 * Determine whether or not value is within range, get state
262 * structure, and enter (above).
263 */
264
265int pm_suspend(suspend_state_t state)
266{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500267 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return enter_state(state);
269 return -EINVAL;
270}
271
Ralf Baechle4cf30342006-12-06 20:36:06 -0800272EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274decl_subsys(power,NULL,NULL);
275
276
277/**
278 * state - control system power state.
279 *
280 * show() returns what states are supported, which is hard-coded to
281 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
282 * 'disk' (Suspend-to-Disk).
283 *
284 * store() accepts one of those strings, translates it into the
285 * proper enumerated value, and initiates a suspend transition.
286 */
287
288static ssize_t state_show(struct subsystem * subsys, char * buf)
289{
290 int i;
291 char * s = buf;
292
293 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800294 if (pm_states[i] && valid_state(i))
295 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 s += sprintf(s,"\n");
298 return (s - buf);
299}
300
301static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
302{
303 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700304 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 char *p;
306 int error;
307 int len;
308
309 p = memchr(buf, '\n', n);
310 len = p ? p - buf : n;
311
312 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
313 if (*s && !strncmp(buf, *s, len))
314 break;
315 }
dean gaudet47bb7892006-04-27 18:39:17 -0700316 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 error = enter_state(state);
318 else
319 error = -EINVAL;
320 return error ? error : n;
321}
322
323power_attr(state);
324
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700325#ifdef CONFIG_PM_TRACE
326int pm_trace_enabled;
327
328static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
329{
330 return sprintf(buf, "%d\n", pm_trace_enabled);
331}
332
333static ssize_t
334pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
335{
336 int val;
337
338 if (sscanf(buf, "%d", &val) == 1) {
339 pm_trace_enabled = !!val;
340 return n;
341 }
342 return -EINVAL;
343}
344
345power_attr(pm_trace);
346
347static struct attribute * g[] = {
348 &state_attr.attr,
349 &pm_trace_attr.attr,
350 NULL,
351};
352#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static struct attribute * g[] = {
354 &state_attr.attr,
355 NULL,
356};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700357#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359static struct attribute_group attr_group = {
360 .attrs = g,
361};
362
363
364static int __init pm_init(void)
365{
366 int error = subsystem_register(&power_subsys);
367 if (!error)
368 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
369 return error;
370}
371
372core_initcall(pm_init);