blob: 8812985f30296cd01aab981337fcf90f95b14ff4 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
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
Johannes Berge8c9c502007-04-30 15:09:54 -070046/**
47 * pm_valid_only_mem - generic memory-only valid callback
48 *
49 * pm_ops drivers that implement mem suspend only and only need
50 * to check for that in their .valid callback can use this instead
51 * of rolling their own .valid callback.
52 */
53int pm_valid_only_mem(suspend_state_t state)
54{
55 return state == PM_SUSPEND_MEM;
56}
57
58
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080059static inline void pm_finish(suspend_state_t state)
60{
61 if (pm_ops->finish)
62 pm_ops->finish(state);
63}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/**
66 * suspend_prepare - Do prep work before entering low-power state.
67 * @state: State we're entering.
68 *
69 * This is common code that is called for each state that we're
70 * entering. Allocate a console, stop all processes, then make sure
71 * the platform can enter the requested state.
72 */
73
74static int suspend_prepare(suspend_state_t state)
75{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070076 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050077 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (!pm_ops || !pm_ops->enter)
80 return -EPERM;
81
82 pm_prepare_console();
83
84 if (freeze_processes()) {
85 error = -EAGAIN;
86 goto Thaw;
87 }
88
Christoph Lameter96177292007-02-10 01:43:03 -080089 if ((free_pages = global_page_state(NR_FREE_PAGES))
90 < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050091 pr_debug("PM: free some memory\n");
92 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
93 if (nr_free_pages() < FREE_PAGE_NUMBER) {
94 error = -ENOMEM;
95 printk(KERN_ERR "PM: No enough memory\n");
96 goto Thaw;
97 }
98 }
99
Linus Torvalds557240b2006-06-19 18:16:01 -0700100 suspend_console();
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800101 error = device_suspend(PMSG_SUSPEND);
102 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 printk(KERN_ERR "Some devices failed to suspend\n");
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700104 goto Resume_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700106 if (pm_ops->prepare) {
107 if ((error = pm_ops->prepare(state)))
108 goto Resume_devices;
109 }
110
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800111 error = disable_nonboot_cpus();
112 if (!error)
113 return 0;
114
115 enable_nonboot_cpus();
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800116 pm_finish(state);
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700117 Resume_devices:
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800118 device_resume();
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700119 Resume_console:
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800120 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 Thaw:
122 thaw_processes();
123 pm_restore_console();
124 return error;
125}
126
Johannes Berga53c46d2007-04-26 11:43:58 +0200127/* default implementation */
128void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
129{
130 local_irq_disable();
131}
132
133/* default implementation */
134void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
135{
136 local_irq_enable();
137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Luca Tettamanti9b238202006-03-23 03:00:09 -0800139int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Johannes Berga53c46d2007-04-26 11:43:58 +0200143 arch_suspend_disable_irqs();
144 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if ((error = device_power_down(PMSG_SUSPEND))) {
147 printk(KERN_ERR "Some devices failed to power down\n");
148 goto Done;
149 }
150 error = pm_ops->enter(state);
151 device_power_up();
152 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200153 arch_suspend_enable_irqs();
154 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return error;
156}
157
158
159/**
160 * suspend_finish - Do final work before exiting suspend sequence.
161 * @state: State we're coming out of.
162 *
163 * Call platform code to clean up, restart processes, and free the
164 * console that we've allocated. This is not called for suspend-to-disk.
165 */
166
167static void suspend_finish(suspend_state_t state)
168{
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800169 enable_nonboot_cpus();
170 pm_finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700172 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 thaw_processes();
174 pm_restore_console();
175}
176
177
178
179
Andreas Mohr3b364b82006-06-25 05:47:56 -0700180static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 [PM_SUSPEND_STANDBY] = "standby",
182 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Pavel Machek123d3c12005-11-29 19:34:37 -0800185static inline int valid_state(suspend_state_t state)
186{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700187 /* All states need lowlevel support and need to be valid
188 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700189 * implies that none are valid. */
190 if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800191 return 0;
192 return 1;
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/**
197 * enter_state - Do common work of entering low-power state.
198 * @state: pm_state structure for state we're entering.
199 *
200 * Make sure we're the only ones trying to enter a sleep state. Fail
201 * if someone has beat us to it, since we don't want anything weird to
202 * happen when we wake up.
203 * Then, do the setup for suspend, enter the state, and cleaup (after
204 * we've woken up).
205 */
206
207static int enter_state(suspend_state_t state)
208{
209 int error;
210
Pavel Machek123d3c12005-11-29 19:34:37 -0800211 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800212 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800213 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EBUSY;
215
David Brownell82428b62005-05-09 08:07:00 -0700216 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if ((error = suspend_prepare(state)))
218 goto Unlock;
219
David Brownell82428b62005-05-09 08:07:00 -0700220 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 error = suspend_enter(state);
222
David Brownell82428b62005-05-09 08:07:00 -0700223 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 suspend_finish(state);
225 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800226 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return error;
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/**
232 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700233 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
235 * Determine whether or not value is within range, get state
236 * structure, and enter (above).
237 */
238
239int pm_suspend(suspend_state_t state)
240{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500241 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return enter_state(state);
243 return -EINVAL;
244}
245
Ralf Baechle4cf30342006-12-06 20:36:06 -0800246EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248decl_subsys(power,NULL,NULL);
249
250
251/**
252 * state - control system power state.
253 *
254 * show() returns what states are supported, which is hard-coded to
255 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
256 * 'disk' (Suspend-to-Disk).
257 *
258 * store() accepts one of those strings, translates it into the
259 * proper enumerated value, and initiates a suspend transition.
260 */
261
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700262static ssize_t state_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int i;
265 char * s = buf;
266
267 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800268 if (pm_states[i] && valid_state(i))
269 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700271#ifdef CONFIG_SOFTWARE_SUSPEND
272 s += sprintf(s, "%s\n", "disk");
273#else
274 if (s != buf)
275 /* convert the last space to a newline */
276 *(s-1) = '\n';
277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return (s - buf);
279}
280
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700281static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700284 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 char *p;
286 int error;
287 int len;
288
289 p = memchr(buf, '\n', n);
290 len = p ? p - buf : n;
291
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700292 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700293 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700294 error = hibernate();
295 return error ? error : n;
296 }
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700299 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 }
dean gaudet47bb7892006-04-27 18:39:17 -0700302 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 error = enter_state(state);
304 else
305 error = -EINVAL;
306 return error ? error : n;
307}
308
309power_attr(state);
310
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700311#ifdef CONFIG_PM_TRACE
312int pm_trace_enabled;
313
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700314static ssize_t pm_trace_show(struct kset *kset, char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700315{
316 return sprintf(buf, "%d\n", pm_trace_enabled);
317}
318
319static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700320pm_trace_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700321{
322 int val;
323
324 if (sscanf(buf, "%d", &val) == 1) {
325 pm_trace_enabled = !!val;
326 return n;
327 }
328 return -EINVAL;
329}
330
331power_attr(pm_trace);
332
333static struct attribute * g[] = {
334 &state_attr.attr,
335 &pm_trace_attr.attr,
336 NULL,
337};
338#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static struct attribute * g[] = {
340 &state_attr.attr,
341 NULL,
342};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700343#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345static struct attribute_group attr_group = {
346 .attrs = g,
347};
348
349
350static int __init pm_init(void)
351{
352 int error = subsystem_register(&power_subsys);
353 if (!error)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700354 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return error;
356}
357
358core_initcall(pm_init);