blob: efc08360e627199374837ed95ea31ebcd0f94a80 [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>
Rafael J. Wysocki232b1432007-10-18 03:04:44 -070023#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "power.h"
26
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070027BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28
Stephen Hemmingera6d70982006-12-06 20:34:35 -080029DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Len Brown9f9adec2007-12-13 17:38:03 -050031unsigned int pm_flags;
32EXPORT_SYMBOL(pm_flags);
33
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020034#ifdef CONFIG_SUSPEND
35
36/* This is just an arbitrary number */
37#define FREE_PAGE_NUMBER (100)
38
Rafael J. Wysockif242d912007-10-18 03:04:41 -070039static struct platform_suspend_ops *suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070042 * suspend_set_ops - Set the global suspend method table.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * @ops: Pointer to ops structure.
44 */
45
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070046void suspend_set_ops(struct platform_suspend_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Stephen Hemmingera6d70982006-12-06 20:34:35 -080048 mutex_lock(&pm_mutex);
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070049 suspend_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -080050 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Johannes Berge8c9c502007-04-30 15:09:54 -070053/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070054 * suspend_valid_only_mem - generic memory-only valid callback
Johannes Berge8c9c502007-04-30 15:09:54 -070055 *
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070056 * Platform drivers that implement mem suspend only and only need
Johannes Berge8c9c502007-04-30 15:09:54 -070057 * to check for that in their .valid callback can use this instead
58 * of rolling their own .valid callback.
59 */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070060int suspend_valid_only_mem(suspend_state_t state)
Johannes Berge8c9c502007-04-30 15:09:54 -070061{
62 return state == PM_SUSPEND_MEM;
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/**
66 * suspend_prepare - Do prep work before entering low-power state.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070068 * This is common code that is called for each state that we're entering.
69 * Run suspend notifiers, allocate a console and stop all processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070071static int suspend_prepare(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070073 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050074 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070076 if (!suspend_ops || !suspend_ops->enter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -EPERM;
78
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070079 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
80 if (error)
81 goto Finish;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 pm_prepare_console();
84
85 if (freeze_processes()) {
86 error = -EAGAIN;
87 goto Thaw;
88 }
89
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070090 free_pages = global_page_state(NR_FREE_PAGES);
91 if (free_pages < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050092 pr_debug("PM: free some memory\n");
93 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
94 if (nr_free_pages() < FREE_PAGE_NUMBER) {
95 error = -ENOMEM;
96 printk(KERN_ERR "PM: No enough memory\n");
David Shaohua Li5ae947e2005-03-18 16:27:13 -050097 }
98 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080099 if (!error)
100 return 0;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 Thaw:
103 thaw_processes();
104 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700105 Finish:
106 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return error;
108}
109
Johannes Berga53c46d2007-04-26 11:43:58 +0200110/* default implementation */
111void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
112{
113 local_irq_disable();
114}
115
116/* default implementation */
117void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
118{
119 local_irq_enable();
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700122/**
123 * suspend_enter - enter the desired system sleep state.
124 * @state: state to enter
125 *
126 * This function should be called after devices have been suspended.
127 */
Adrian Bunka065c862007-10-18 03:04:37 -0700128static int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Johannes Berga53c46d2007-04-26 11:43:58 +0200132 arch_suspend_disable_irqs();
133 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if ((error = device_power_down(PMSG_SUSPEND))) {
136 printk(KERN_ERR "Some devices failed to power down\n");
137 goto Done;
138 }
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700139 error = suspend_ops->enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 device_power_up();
141 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200142 arch_suspend_enable_irqs();
143 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return error;
145}
146
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700147/**
148 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
149 * state.
150 * @state: state to enter
151 */
152int suspend_devices_and_enter(suspend_state_t state)
153{
154 int error;
155
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700156 if (!suspend_ops)
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700157 return -ENOSYS;
158
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700159 if (suspend_ops->set_target) {
160 error = suspend_ops->set_target(state);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700161 if (error)
162 return error;
163 }
164 suspend_console();
165 error = device_suspend(PMSG_SUSPEND);
166 if (error) {
167 printk(KERN_ERR "Some devices failed to suspend\n");
168 goto Resume_console;
169 }
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700170 if (suspend_ops->prepare) {
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700171 error = suspend_ops->prepare();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700172 if (error)
173 goto Resume_devices;
174 }
175 error = disable_nonboot_cpus();
176 if (!error)
177 suspend_enter(state);
178
179 enable_nonboot_cpus();
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700180 if (suspend_ops->finish)
181 suspend_ops->finish();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700182 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. */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700215 if (!suspend_ops || !suspend_ops->valid || !suspend_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;
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700237
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800238 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EBUSY;
240
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700241 printk("Syncing filesystems ... ");
242 sys_sync();
243 printk("done.\n");
244
David Brownell82428b62005-05-09 08:07:00 -0700245 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700246 if ((error = suspend_prepare()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 goto Unlock;
248
David Brownell82428b62005-05-09 08:07:00 -0700249 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700250 error = suspend_devices_and_enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Brownell82428b62005-05-09 08:07:00 -0700252 pr_debug("PM: Finishing wakeup.\n");
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700253 suspend_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800255 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return error;
257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/**
261 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700262 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
264 * Determine whether or not value is within range, get state
265 * structure, and enter (above).
266 */
267
268int pm_suspend(suspend_state_t state)
269{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500270 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return enter_state(state);
272 return -EINVAL;
273}
274
Ralf Baechle4cf30342006-12-06 20:36:06 -0800275EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200277#endif /* CONFIG_SUSPEND */
278
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800279struct kobject *power_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281/**
282 * state - control system power state.
283 *
284 * show() returns what states are supported, which is hard-coded to
285 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
286 * 'disk' (Suspend-to-Disk).
287 *
288 * store() accepts one of those strings, translates it into the
289 * proper enumerated value, and initiates a suspend transition.
290 */
291
Kay Sievers386f2752007-11-02 13:47:53 +0100292static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
293 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200295 char *s = buf;
296#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800300 if (pm_states[i] && valid_state(i))
301 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200303#endif
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200304#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700305 s += sprintf(s, "%s\n", "disk");
306#else
307 if (s != buf)
308 /* convert the last space to a newline */
309 *(s-1) = '\n';
310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return (s - buf);
312}
313
Kay Sievers386f2752007-11-02 13:47:53 +0100314static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
315 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200317#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700319 const char * const *s;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200320#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 int len;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200323 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 p = memchr(buf, '\n', n);
326 len = p ? p - buf : n;
327
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700328 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700329 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700330 error = hibernate();
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200331 goto Exit;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700332 }
333
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200334#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700336 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
338 }
dean gaudet47bb7892006-04-27 18:39:17 -0700339 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 error = enter_state(state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200341#endif
342
343 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return error ? error : n;
345}
346
347power_attr(state);
348
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700349#ifdef CONFIG_PM_TRACE
350int pm_trace_enabled;
351
Kay Sievers386f2752007-11-02 13:47:53 +0100352static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
353 char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700354{
355 return sprintf(buf, "%d\n", pm_trace_enabled);
356}
357
358static ssize_t
Kay Sievers386f2752007-11-02 13:47:53 +0100359pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
360 const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700361{
362 int val;
363
364 if (sscanf(buf, "%d", &val) == 1) {
365 pm_trace_enabled = !!val;
366 return n;
367 }
368 return -EINVAL;
369}
370
371power_attr(pm_trace);
372
373static struct attribute * g[] = {
374 &state_attr.attr,
375 &pm_trace_attr.attr,
376 NULL,
377};
378#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static struct attribute * g[] = {
380 &state_attr.attr,
381 NULL,
382};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700383#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385static struct attribute_group attr_group = {
386 .attrs = g,
387};
388
389
390static int __init pm_init(void)
391{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800392 power_kobj = kobject_create_and_add("power", NULL);
393 if (!power_kobj)
Greg Kroah-Hartman039a5dc2007-11-01 10:39:50 -0700394 return -ENOMEM;
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800395 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398core_initcall(pm_init);