blob: fc45ed22620f46c8a84341e51069892cab3e93fa [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
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 -070032
33/**
34 * pm_set_ops - Set the global power method table.
35 * @ops: Pointer to ops structure.
36 */
37
38void pm_set_ops(struct pm_ops * ops)
39{
Stephen Hemmingera6d70982006-12-06 20:34:35 -080040 mutex_lock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 pm_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -080042 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Johannes Berge8c9c502007-04-30 15:09:54 -070045/**
46 * pm_valid_only_mem - generic memory-only valid callback
47 *
48 * pm_ops drivers that implement mem suspend only and only need
49 * to check for that in their .valid callback can use this instead
50 * of rolling their own .valid callback.
51 */
52int pm_valid_only_mem(suspend_state_t state)
53{
54 return state == PM_SUSPEND_MEM;
55}
56
57
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080058static inline void pm_finish(suspend_state_t state)
59{
60 if (pm_ops->finish)
61 pm_ops->finish(state);
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/**
65 * suspend_prepare - Do prep work before entering low-power state.
66 * @state: State we're entering.
67 *
68 * This is common code that is called for each state that we're
69 * entering. Allocate a console, stop all processes, then make sure
70 * the platform can enter the requested state.
71 */
72
73static int suspend_prepare(suspend_state_t state)
74{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070075 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050076 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (!pm_ops || !pm_ops->enter)
79 return -EPERM;
80
81 pm_prepare_console();
82
83 if (freeze_processes()) {
84 error = -EAGAIN;
85 goto Thaw;
86 }
87
Christoph Lameter96177292007-02-10 01:43:03 -080088 if ((free_pages = global_page_state(NR_FREE_PAGES))
89 < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050090 pr_debug("PM: free some memory\n");
91 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
92 if (nr_free_pages() < FREE_PAGE_NUMBER) {
93 error = -ENOMEM;
94 printk(KERN_ERR "PM: No enough memory\n");
95 goto Thaw;
96 }
97 }
98
Rafael J. Wysocki2391dae32007-07-01 12:07:33 -070099 if (pm_ops->set_target) {
100 error = pm_ops->set_target(state);
101 if (error)
102 goto Thaw;
103 }
Linus Torvalds557240b2006-06-19 18:16:01 -0700104 suspend_console();
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800105 error = device_suspend(PMSG_SUSPEND);
106 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 printk(KERN_ERR "Some devices failed to suspend\n");
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700108 goto Resume_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700110 if (pm_ops->prepare) {
111 if ((error = pm_ops->prepare(state)))
112 goto Resume_devices;
113 }
114
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800115 error = disable_nonboot_cpus();
116 if (!error)
117 return 0;
118
119 enable_nonboot_cpus();
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800120 pm_finish(state);
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700121 Resume_devices:
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800122 device_resume();
Linus Torvalds52ade9b2007-05-16 15:28:14 -0700123 Resume_console:
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800124 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",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Pavel Machek123d3c12005-11-29 19:34:37 -0800189static inline int valid_state(suspend_state_t state)
190{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700191 /* All states need lowlevel support and need to be valid
192 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700193 * implies that none are valid. */
194 if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800195 return 0;
196 return 1;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/**
201 * enter_state - Do common work of entering low-power state.
202 * @state: pm_state structure for state we're entering.
203 *
204 * Make sure we're the only ones trying to enter a sleep state. Fail
205 * if someone has beat us to it, since we don't want anything weird to
206 * happen when we wake up.
207 * Then, do the setup for suspend, enter the state, and cleaup (after
208 * we've woken up).
209 */
210
211static int enter_state(suspend_state_t state)
212{
213 int error;
214
Pavel Machek123d3c12005-11-29 19:34:37 -0800215 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800216 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800217 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -EBUSY;
219
David Brownell82428b62005-05-09 08:07:00 -0700220 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if ((error = suspend_prepare(state)))
222 goto Unlock;
223
David Brownell82428b62005-05-09 08:07:00 -0700224 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 error = suspend_enter(state);
226
David Brownell82428b62005-05-09 08:07:00 -0700227 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 suspend_finish(state);
229 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800230 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return error;
232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235/**
236 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700237 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 *
239 * Determine whether or not value is within range, get state
240 * structure, and enter (above).
241 */
242
243int pm_suspend(suspend_state_t state)
244{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500245 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return enter_state(state);
247 return -EINVAL;
248}
249
Ralf Baechle4cf30342006-12-06 20:36:06 -0800250EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252decl_subsys(power,NULL,NULL);
253
254
255/**
256 * state - control system power state.
257 *
258 * show() returns what states are supported, which is hard-coded to
259 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
260 * 'disk' (Suspend-to-Disk).
261 *
262 * store() accepts one of those strings, translates it into the
263 * proper enumerated value, and initiates a suspend transition.
264 */
265
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700266static ssize_t state_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 int i;
269 char * s = buf;
270
271 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800272 if (pm_states[i] && valid_state(i))
273 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700275#ifdef CONFIG_SOFTWARE_SUSPEND
276 s += sprintf(s, "%s\n", "disk");
277#else
278 if (s != buf)
279 /* convert the last space to a newline */
280 *(s-1) = '\n';
281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return (s - buf);
283}
284
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700285static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700288 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 char *p;
290 int error;
291 int len;
292
293 p = memchr(buf, '\n', n);
294 len = p ? p - buf : n;
295
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700296 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700297 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700298 error = hibernate();
299 return error ? error : n;
300 }
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700303 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 }
dean gaudet47bb7892006-04-27 18:39:17 -0700306 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 error = enter_state(state);
308 else
309 error = -EINVAL;
310 return error ? error : n;
311}
312
313power_attr(state);
314
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700315#ifdef CONFIG_PM_TRACE
316int pm_trace_enabled;
317
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700318static ssize_t pm_trace_show(struct kset *kset, char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700319{
320 return sprintf(buf, "%d\n", pm_trace_enabled);
321}
322
323static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700324pm_trace_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700325{
326 int val;
327
328 if (sscanf(buf, "%d", &val) == 1) {
329 pm_trace_enabled = !!val;
330 return n;
331 }
332 return -EINVAL;
333}
334
335power_attr(pm_trace);
336
337static struct attribute * g[] = {
338 &state_attr.attr,
339 &pm_trace_attr.attr,
340 NULL,
341};
342#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static struct attribute * g[] = {
344 &state_attr.attr,
345 NULL,
346};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700347#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349static struct attribute_group attr_group = {
350 .attrs = g,
351};
352
353
354static int __init pm_init(void)
355{
356 int error = subsystem_register(&power_subsys);
357 if (!error)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700358 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return error;
360}
361
362core_initcall(pm_init);