blob: 3cdf95b1dc92af97e13e8a6c68c4e0a6f8d42153 [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
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020031#ifdef CONFIG_SUSPEND
32
33/* This is just an arbitrary number */
34#define FREE_PAGE_NUMBER (100)
35
Rafael J. Wysockif242d912007-10-18 03:04:41 -070036static struct platform_suspend_ops *suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070039 * suspend_set_ops - Set the global suspend method table.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * @ops: Pointer to ops structure.
41 */
42
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070043void suspend_set_ops(struct platform_suspend_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Stephen Hemmingera6d70982006-12-06 20:34:35 -080045 mutex_lock(&pm_mutex);
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070046 suspend_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -080047 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Johannes Berge8c9c502007-04-30 15:09:54 -070050/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070051 * suspend_valid_only_mem - generic memory-only valid callback
Johannes Berge8c9c502007-04-30 15:09:54 -070052 *
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070053 * Platform drivers that implement mem suspend only and only need
Johannes Berge8c9c502007-04-30 15:09:54 -070054 * to check for that in their .valid callback can use this instead
55 * of rolling their own .valid callback.
56 */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070057int suspend_valid_only_mem(suspend_state_t state)
Johannes Berge8c9c502007-04-30 15:09:54 -070058{
59 return state == PM_SUSPEND_MEM;
60}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/**
63 * suspend_prepare - Do prep work before entering low-power state.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070065 * This is common code that is called for each state that we're entering.
66 * Run suspend notifiers, allocate a console and stop all processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070068static int suspend_prepare(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070070 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050071 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070073 if (!suspend_ops || !suspend_ops->enter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -EPERM;
75
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070076 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
77 if (error)
78 goto Finish;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 pm_prepare_console();
81
82 if (freeze_processes()) {
83 error = -EAGAIN;
84 goto Thaw;
85 }
86
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070087 free_pages = global_page_state(NR_FREE_PAGES);
88 if (free_pages < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050089 pr_debug("PM: free some memory\n");
90 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
91 if (nr_free_pages() < FREE_PAGE_NUMBER) {
92 error = -ENOMEM;
93 printk(KERN_ERR "PM: No enough memory\n");
David Shaohua Li5ae947e2005-03-18 16:27:13 -050094 }
95 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080096 if (!error)
97 return 0;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 Thaw:
100 thaw_processes();
101 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700102 Finish:
103 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return error;
105}
106
Johannes Berga53c46d2007-04-26 11:43:58 +0200107/* default implementation */
108void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
109{
110 local_irq_disable();
111}
112
113/* default implementation */
114void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
115{
116 local_irq_enable();
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700119/**
120 * suspend_enter - enter the desired system sleep state.
121 * @state: state to enter
122 *
123 * This function should be called after devices have been suspended.
124 */
Adrian Bunka065c862007-10-18 03:04:37 -0700125static int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Johannes Berga53c46d2007-04-26 11:43:58 +0200129 arch_suspend_disable_irqs();
130 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if ((error = device_power_down(PMSG_SUSPEND))) {
133 printk(KERN_ERR "Some devices failed to power down\n");
134 goto Done;
135 }
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700136 error = suspend_ops->enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 device_power_up();
138 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200139 arch_suspend_enable_irqs();
140 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return error;
142}
143
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700144/**
145 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
146 * state.
147 * @state: state to enter
148 */
149int suspend_devices_and_enter(suspend_state_t state)
150{
151 int error;
152
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700153 if (!suspend_ops)
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700154 return -ENOSYS;
155
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700156 if (suspend_ops->set_target) {
157 error = suspend_ops->set_target(state);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700158 if (error)
159 return error;
160 }
161 suspend_console();
162 error = device_suspend(PMSG_SUSPEND);
163 if (error) {
164 printk(KERN_ERR "Some devices failed to suspend\n");
165 goto Resume_console;
166 }
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700167 if (suspend_ops->prepare) {
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700168 error = suspend_ops->prepare();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700169 if (error)
170 goto Resume_devices;
171 }
172 error = disable_nonboot_cpus();
173 if (!error)
174 suspend_enter(state);
175
176 enable_nonboot_cpus();
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700177 if (suspend_ops->finish)
178 suspend_ops->finish();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700179 Resume_devices:
180 device_resume();
181 Resume_console:
182 resume_console();
183 return error;
184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/**
187 * suspend_finish - Do final work before exiting suspend sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
189 * Call platform code to clean up, restart processes, and free the
190 * console that we've allocated. This is not called for suspend-to-disk.
191 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700192static void suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 thaw_processes();
195 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700196 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199
200
201
Andreas Mohr3b364b82006-06-25 05:47:56 -0700202static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 [PM_SUSPEND_STANDBY] = "standby",
204 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
Pavel Machek123d3c12005-11-29 19:34:37 -0800207static inline int valid_state(suspend_state_t state)
208{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700209 /* All states need lowlevel support and need to be valid
210 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700211 * implies that none are valid. */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700212 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800213 return 0;
214 return 1;
215}
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/**
219 * enter_state - Do common work of entering low-power state.
220 * @state: pm_state structure for state we're entering.
221 *
222 * Make sure we're the only ones trying to enter a sleep state. Fail
223 * if someone has beat us to it, since we don't want anything weird to
224 * happen when we wake up.
225 * Then, do the setup for suspend, enter the state, and cleaup (after
226 * we've woken up).
227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int enter_state(suspend_state_t state)
229{
230 int error;
231
Pavel Machek123d3c12005-11-29 19:34:37 -0800232 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800233 return -ENODEV;
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700234
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800235 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -EBUSY;
237
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700238 printk("Syncing filesystems ... ");
239 sys_sync();
240 printk("done.\n");
241
David Brownell82428b62005-05-09 08:07:00 -0700242 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700243 if ((error = suspend_prepare()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 goto Unlock;
245
David Brownell82428b62005-05-09 08:07:00 -0700246 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700247 error = suspend_devices_and_enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
David Brownell82428b62005-05-09 08:07:00 -0700249 pr_debug("PM: Finishing wakeup.\n");
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700250 suspend_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800252 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return error;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257/**
258 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700259 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
261 * Determine whether or not value is within range, get state
262 * structure, and enter (above).
263 */
264
265int pm_suspend(suspend_state_t state)
266{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500267 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return enter_state(state);
269 return -EINVAL;
270}
271
Ralf Baechle4cf30342006-12-06 20:36:06 -0800272EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200274#endif /* CONFIG_SUSPEND */
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276decl_subsys(power,NULL,NULL);
277
278
279/**
280 * state - control system power state.
281 *
282 * show() returns what states are supported, which is hard-coded to
283 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
284 * 'disk' (Suspend-to-Disk).
285 *
286 * store() accepts one of those strings, translates it into the
287 * proper enumerated value, and initiates a suspend transition.
288 */
289
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700290static ssize_t state_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200292 char *s = buf;
293#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800297 if (pm_states[i] && valid_state(i))
298 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200300#endif
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200301#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700302 s += sprintf(s, "%s\n", "disk");
303#else
304 if (s != buf)
305 /* convert the last space to a newline */
306 *(s-1) = '\n';
307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return (s - buf);
309}
310
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700311static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200313#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700315 const char * const *s;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int len;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200319 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 p = memchr(buf, '\n', n);
322 len = p ? p - buf : n;
323
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700324 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700325 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700326 error = hibernate();
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200327 goto Exit;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700328 }
329
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200330#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700332 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
334 }
dean gaudet47bb7892006-04-27 18:39:17 -0700335 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 error = enter_state(state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200337#endif
338
339 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return error ? error : n;
341}
342
343power_attr(state);
344
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700345#ifdef CONFIG_PM_TRACE
346int pm_trace_enabled;
347
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700348static ssize_t pm_trace_show(struct kset *kset, char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700349{
350 return sprintf(buf, "%d\n", pm_trace_enabled);
351}
352
353static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700354pm_trace_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700355{
356 int val;
357
358 if (sscanf(buf, "%d", &val) == 1) {
359 pm_trace_enabled = !!val;
360 return n;
361 }
362 return -EINVAL;
363}
364
365power_attr(pm_trace);
366
367static struct attribute * g[] = {
368 &state_attr.attr,
369 &pm_trace_attr.attr,
370 NULL,
371};
372#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static struct attribute * g[] = {
374 &state_attr.attr,
375 NULL,
376};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700377#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379static struct attribute_group attr_group = {
380 .attrs = g,
381};
382
383
384static int __init pm_init(void)
385{
386 int error = subsystem_register(&power_subsys);
387 if (!error)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700388 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return error;
390}
391
392core_initcall(pm_init);