blob: cfba6987ae7d4178ede27b1079d302506bf25404 [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>
Andrew Morton6cc07192006-06-22 14:47:18 -070018#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070019#include <linux/cpu.h>
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -070020#include <linux/resume-trace.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080021#include <linux/freezer.h>
Christoph Lameter96177292007-02-10 01:43:03 -080022#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "power.h"
25
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070026BLOCKING_NOTIFIER_HEAD(pm_chain_head);
27
David Shaohua Li5ae947e2005-03-18 16:27:13 -050028/*This is just an arbitrary number */
29#define FREE_PAGE_NUMBER (100)
30
Stephen Hemmingera6d70982006-12-06 20:34:35 -080031DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Pavel Machek123d3c12005-11-29 19:34:37 -080033struct pm_ops *pm_ops;
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;
Stephen Hemmingera6d70982006-12-06 20:34:35 -080044 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Johannes Berge8c9c502007-04-30 15:09:54 -070047/**
48 * pm_valid_only_mem - generic memory-only valid callback
49 *
50 * pm_ops drivers that implement mem suspend only and only need
51 * to check for that in their .valid callback can use this instead
52 * of rolling their own .valid callback.
53 */
54int pm_valid_only_mem(suspend_state_t state)
55{
56 return state == PM_SUSPEND_MEM;
57}
58
59
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080060static inline void pm_finish(suspend_state_t state)
61{
62 if (pm_ops->finish)
63 pm_ops->finish(state);
64}
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/**
67 * suspend_prepare - Do prep work before entering low-power state.
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 *
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070069 * This is common code that is called for each state that we're entering.
70 * Run suspend notifiers, allocate a console and stop all processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070072static int suspend_prepare(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070074 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050075 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (!pm_ops || !pm_ops->enter)
78 return -EPERM;
79
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070080 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
81 if (error)
82 goto Finish;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 pm_prepare_console();
85
86 if (freeze_processes()) {
87 error = -EAGAIN;
88 goto Thaw;
89 }
90
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070091 free_pages = global_page_state(NR_FREE_PAGES);
92 if (free_pages < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050093 pr_debug("PM: free some memory\n");
94 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
95 if (nr_free_pages() < FREE_PAGE_NUMBER) {
96 error = -ENOMEM;
97 printk(KERN_ERR "PM: No enough memory\n");
David Shaohua Li5ae947e2005-03-18 16:27:13 -050098 }
99 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800100 if (!error)
101 return 0;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 Thaw:
104 thaw_processes();
105 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700106 Finish:
107 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return error;
109}
110
Johannes Berga53c46d2007-04-26 11:43:58 +0200111/* default implementation */
112void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
113{
114 local_irq_disable();
115}
116
117/* default implementation */
118void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
119{
120 local_irq_enable();
121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700123/**
124 * suspend_enter - enter the desired system sleep state.
125 * @state: state to enter
126 *
127 * This function should be called after devices have been suspended.
128 */
Luca Tettamanti9b238202006-03-23 03:00:09 -0800129int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Johannes Berga53c46d2007-04-26 11:43:58 +0200133 arch_suspend_disable_irqs();
134 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if ((error = device_power_down(PMSG_SUSPEND))) {
137 printk(KERN_ERR "Some devices failed to power down\n");
138 goto Done;
139 }
140 error = pm_ops->enter(state);
141 device_power_up();
142 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200143 arch_suspend_enable_irqs();
144 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return error;
146}
147
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700148/**
149 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
150 * state.
151 * @state: state to enter
152 */
153int suspend_devices_and_enter(suspend_state_t state)
154{
155 int error;
156
157 if (!pm_ops)
158 return -ENOSYS;
159
160 if (pm_ops->set_target) {
161 error = pm_ops->set_target(state);
162 if (error)
163 return error;
164 }
165 suspend_console();
166 error = device_suspend(PMSG_SUSPEND);
167 if (error) {
168 printk(KERN_ERR "Some devices failed to suspend\n");
169 goto Resume_console;
170 }
171 if (pm_ops->prepare) {
172 error = pm_ops->prepare(state);
173 if (error)
174 goto Resume_devices;
175 }
176 error = disable_nonboot_cpus();
177 if (!error)
178 suspend_enter(state);
179
180 enable_nonboot_cpus();
181 pm_finish(state);
182 Resume_devices:
183 device_resume();
184 Resume_console:
185 resume_console();
186 return error;
187}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/**
190 * suspend_finish - Do final work before exiting suspend sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *
192 * Call platform code to clean up, restart processes, and free the
193 * console that we've allocated. This is not called for suspend-to-disk.
194 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700195static void suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 thaw_processes();
198 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700199 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202
203
204
Andreas Mohr3b364b82006-06-25 05:47:56 -0700205static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 [PM_SUSPEND_STANDBY] = "standby",
207 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Pavel Machek123d3c12005-11-29 19:34:37 -0800210static inline int valid_state(suspend_state_t state)
211{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700212 /* All states need lowlevel support and need to be valid
213 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700214 * implies that none are valid. */
215 if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800216 return 0;
217 return 1;
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/**
222 * enter_state - Do common work of entering low-power state.
223 * @state: pm_state structure for state we're entering.
224 *
225 * Make sure we're the only ones trying to enter a sleep state. Fail
226 * if someone has beat us to it, since we don't want anything weird to
227 * happen when we wake up.
228 * Then, do the setup for suspend, enter the state, and cleaup (after
229 * we've woken up).
230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static int enter_state(suspend_state_t state)
232{
233 int error;
234
Pavel Machek123d3c12005-11-29 19:34:37 -0800235 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800236 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800237 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EBUSY;
239
David Brownell82428b62005-05-09 08:07:00 -0700240 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700241 if ((error = suspend_prepare()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 goto Unlock;
243
David Brownell82428b62005-05-09 08:07:00 -0700244 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700245 error = suspend_devices_and_enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Brownell82428b62005-05-09 08:07:00 -0700247 pr_debug("PM: Finishing wakeup.\n");
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700248 suspend_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800250 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return error;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255/**
256 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700257 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
259 * Determine whether or not value is within range, get state
260 * structure, and enter (above).
261 */
262
263int pm_suspend(suspend_state_t state)
264{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500265 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return enter_state(state);
267 return -EINVAL;
268}
269
Ralf Baechle4cf30342006-12-06 20:36:06 -0800270EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272decl_subsys(power,NULL,NULL);
273
274
275/**
276 * state - control system power state.
277 *
278 * show() returns what states are supported, which is hard-coded to
279 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
280 * 'disk' (Suspend-to-Disk).
281 *
282 * store() accepts one of those strings, translates it into the
283 * proper enumerated value, and initiates a suspend transition.
284 */
285
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700286static ssize_t state_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int i;
289 char * s = buf;
290
291 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800292 if (pm_states[i] && valid_state(i))
293 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200295#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700296 s += sprintf(s, "%s\n", "disk");
297#else
298 if (s != buf)
299 /* convert the last space to a newline */
300 *(s-1) = '\n';
301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return (s - buf);
303}
304
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700305static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700308 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 char *p;
310 int error;
311 int len;
312
313 p = memchr(buf, '\n', n);
314 len = p ? p - buf : n;
315
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700316 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700317 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700318 error = hibernate();
319 return error ? error : n;
320 }
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700323 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 }
dean gaudet47bb7892006-04-27 18:39:17 -0700326 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 error = enter_state(state);
328 else
329 error = -EINVAL;
330 return error ? error : n;
331}
332
333power_attr(state);
334
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700335#ifdef CONFIG_PM_TRACE
336int pm_trace_enabled;
337
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700338static ssize_t pm_trace_show(struct kset *kset, char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700339{
340 return sprintf(buf, "%d\n", pm_trace_enabled);
341}
342
343static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700344pm_trace_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700345{
346 int val;
347
348 if (sscanf(buf, "%d", &val) == 1) {
349 pm_trace_enabled = !!val;
350 return n;
351 }
352 return -EINVAL;
353}
354
355power_attr(pm_trace);
356
357static struct attribute * g[] = {
358 &state_attr.attr,
359 &pm_trace_attr.attr,
360 NULL,
361};
362#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static struct attribute * g[] = {
364 &state_attr.attr,
365 NULL,
366};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700367#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369static struct attribute_group attr_group = {
370 .attrs = g,
371};
372
373
374static int __init pm_init(void)
375{
376 int error = subsystem_register(&power_subsys);
377 if (!error)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700378 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return error;
380}
381
382core_initcall(pm_init);