blob: 4d26ad394fb3bbeab4787013f173fa5aaa153ee7 [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.
68 * @state: State we're entering.
69 *
70 * This is common code that is called for each state that we're
71 * entering. Allocate a console, stop all processes, then make sure
72 * the platform can enter the requested state.
73 */
74
75static int suspend_prepare(suspend_state_t state)
76{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070077 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050078 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 if (!pm_ops || !pm_ops->enter)
81 return -EPERM;
82
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070083 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
84 if (error)
85 goto Finish;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 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
Rafael J. Wysocki2391dae32007-07-01 12:07:33 -0700105 if (pm_ops->set_target) {
106 error = pm_ops->set_target(state);
107 if (error)
108 goto Thaw;
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");
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700114 goto Resume_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700116 if (pm_ops->prepare) {
117 if ((error = pm_ops->prepare(state)))
118 goto Resume_devices;
119 }
120
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800121 error = disable_nonboot_cpus();
122 if (!error)
123 return 0;
124
125 enable_nonboot_cpus();
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800126 pm_finish(state);
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700127 Resume_devices:
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800128 device_resume();
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700129 Resume_console:
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800130 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 Thaw:
132 thaw_processes();
133 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700134 Finish:
135 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return error;
137}
138
Johannes Berga53c46d2007-04-26 11:43:58 +0200139/* default implementation */
140void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
141{
142 local_irq_disable();
143}
144
145/* default implementation */
146void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
147{
148 local_irq_enable();
149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Luca Tettamanti9b238202006-03-23 03:00:09 -0800151int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Johannes Berga53c46d2007-04-26 11:43:58 +0200155 arch_suspend_disable_irqs();
156 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if ((error = device_power_down(PMSG_SUSPEND))) {
159 printk(KERN_ERR "Some devices failed to power down\n");
160 goto Done;
161 }
162 error = pm_ops->enter(state);
163 device_power_up();
164 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200165 arch_suspend_enable_irqs();
166 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return error;
168}
169
170
171/**
172 * suspend_finish - Do final work before exiting suspend sequence.
173 * @state: State we're coming out of.
174 *
175 * Call platform code to clean up, restart processes, and free the
176 * console that we've allocated. This is not called for suspend-to-disk.
177 */
178
179static void suspend_finish(suspend_state_t state)
180{
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800181 enable_nonboot_cpus();
182 pm_finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700184 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 thaw_processes();
186 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700187 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190
191
192
Andreas Mohr3b364b82006-06-25 05:47:56 -0700193static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 [PM_SUSPEND_STANDBY] = "standby",
195 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Pavel Machek123d3c12005-11-29 19:34:37 -0800198static inline int valid_state(suspend_state_t state)
199{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700200 /* All states need lowlevel support and need to be valid
201 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700202 * implies that none are valid. */
203 if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800204 return 0;
205 return 1;
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/**
210 * enter_state - Do common work of entering low-power state.
211 * @state: pm_state structure for state we're entering.
212 *
213 * Make sure we're the only ones trying to enter a sleep state. Fail
214 * if someone has beat us to it, since we don't want anything weird to
215 * happen when we wake up.
216 * Then, do the setup for suspend, enter the state, and cleaup (after
217 * we've woken up).
218 */
219
220static int enter_state(suspend_state_t state)
221{
222 int error;
223
Pavel Machek123d3c12005-11-29 19:34:37 -0800224 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800225 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800226 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EBUSY;
228
David Brownell82428b62005-05-09 08:07:00 -0700229 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if ((error = suspend_prepare(state)))
231 goto Unlock;
232
David Brownell82428b62005-05-09 08:07:00 -0700233 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 error = suspend_enter(state);
235
David Brownell82428b62005-05-09 08:07:00 -0700236 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 suspend_finish(state);
238 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800239 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return error;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244/**
245 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700246 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 *
248 * Determine whether or not value is within range, get state
249 * structure, and enter (above).
250 */
251
252int pm_suspend(suspend_state_t state)
253{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500254 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return enter_state(state);
256 return -EINVAL;
257}
258
Ralf Baechle4cf30342006-12-06 20:36:06 -0800259EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261decl_subsys(power,NULL,NULL);
262
263
264/**
265 * state - control system power state.
266 *
267 * show() returns what states are supported, which is hard-coded to
268 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
269 * 'disk' (Suspend-to-Disk).
270 *
271 * store() accepts one of those strings, translates it into the
272 * proper enumerated value, and initiates a suspend transition.
273 */
274
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700275static ssize_t state_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 int i;
278 char * s = buf;
279
280 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800281 if (pm_states[i] && valid_state(i))
282 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700284#ifdef CONFIG_SOFTWARE_SUSPEND
285 s += sprintf(s, "%s\n", "disk");
286#else
287 if (s != buf)
288 /* convert the last space to a newline */
289 *(s-1) = '\n';
290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return (s - buf);
292}
293
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700294static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700297 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 char *p;
299 int error;
300 int len;
301
302 p = memchr(buf, '\n', n);
303 len = p ? p - buf : n;
304
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700305 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700306 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700307 error = hibernate();
308 return error ? error : n;
309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700312 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
314 }
dean gaudet47bb7892006-04-27 18:39:17 -0700315 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 error = enter_state(state);
317 else
318 error = -EINVAL;
319 return error ? error : n;
320}
321
322power_attr(state);
323
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700324#ifdef CONFIG_PM_TRACE
325int pm_trace_enabled;
326
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700327static ssize_t pm_trace_show(struct kset *kset, char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700328{
329 return sprintf(buf, "%d\n", pm_trace_enabled);
330}
331
332static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700333pm_trace_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700334{
335 int val;
336
337 if (sscanf(buf, "%d", &val) == 1) {
338 pm_trace_enabled = !!val;
339 return n;
340 }
341 return -EINVAL;
342}
343
344power_attr(pm_trace);
345
346static struct attribute * g[] = {
347 &state_attr.attr,
348 &pm_trace_attr.attr,
349 NULL,
350};
351#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static struct attribute * g[] = {
353 &state_attr.attr,
354 NULL,
355};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700356#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358static struct attribute_group attr_group = {
359 .attrs = g,
360};
361
362
363static int __init pm_init(void)
364{
365 int error = subsystem_register(&power_subsys);
366 if (!error)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700367 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return error;
369}
370
371core_initcall(pm_init);