blob: fb2412e7a0ce2ca7cedd858b3aa47f94eb7d285c [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
Stephen Hemmingera6d70982006-12-06 20:34:35 -080028DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Rafael J. Wysocki296699d2007-07-29 23:27:18 +020030#ifdef CONFIG_SUSPEND
31
32/* This is just an arbitrary number */
33#define FREE_PAGE_NUMBER (100)
34
Rafael J. Wysockif242d912007-10-18 03:04:41 -070035static struct platform_suspend_ops *suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070038 * suspend_set_ops - Set the global suspend method table.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * @ops: Pointer to ops structure.
40 */
41
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070042void suspend_set_ops(struct platform_suspend_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Stephen Hemmingera6d70982006-12-06 20:34:35 -080044 mutex_lock(&pm_mutex);
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070045 suspend_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -080046 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Johannes Berge8c9c502007-04-30 15:09:54 -070049/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070050 * suspend_valid_only_mem - generic memory-only valid callback
Johannes Berge8c9c502007-04-30 15:09:54 -070051 *
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070052 * Platform drivers that implement mem suspend only and only need
Johannes Berge8c9c502007-04-30 15:09:54 -070053 * to check for that in their .valid callback can use this instead
54 * of rolling their own .valid callback.
55 */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070056int suspend_valid_only_mem(suspend_state_t state)
Johannes Berge8c9c502007-04-30 15:09:54 -070057{
58 return state == PM_SUSPEND_MEM;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/**
62 * suspend_prepare - Do prep work before entering low-power state.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070064 * This is common code that is called for each state that we're entering.
65 * Run suspend notifiers, allocate a console and stop all processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070067static int suspend_prepare(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070069 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050070 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Rafael J. Wysocki26398a72007-10-18 03:04:40 -070072 if (!suspend_ops || !suspend_ops->enter)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EPERM;
74
Rafael J. Wysockib10d9112007-07-19 01:47:36 -070075 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
76 if (error)
77 goto Finish;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 pm_prepare_console();
80
81 if (freeze_processes()) {
82 error = -EAGAIN;
83 goto Thaw;
84 }
85
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -070086 free_pages = global_page_state(NR_FREE_PAGES);
87 if (free_pages < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050088 pr_debug("PM: free some memory\n");
89 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
90 if (nr_free_pages() < FREE_PAGE_NUMBER) {
91 error = -ENOMEM;
92 printk(KERN_ERR "PM: No enough memory\n");
David Shaohua Li5ae947e2005-03-18 16:27:13 -050093 }
94 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080095 if (!error)
96 return 0;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 Thaw:
99 thaw_processes();
100 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700101 Finish:
102 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return error;
104}
105
Johannes Berga53c46d2007-04-26 11:43:58 +0200106/* default implementation */
107void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
108{
109 local_irq_disable();
110}
111
112/* default implementation */
113void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
114{
115 local_irq_enable();
116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700118/**
119 * suspend_enter - enter the desired system sleep state.
120 * @state: state to enter
121 *
122 * This function should be called after devices have been suspended.
123 */
Adrian Bunka065c862007-10-18 03:04:37 -0700124static int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Johannes Berga53c46d2007-04-26 11:43:58 +0200128 arch_suspend_disable_irqs();
129 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if ((error = device_power_down(PMSG_SUSPEND))) {
132 printk(KERN_ERR "Some devices failed to power down\n");
133 goto Done;
134 }
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700135 error = suspend_ops->enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 device_power_up();
137 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200138 arch_suspend_enable_irqs();
139 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return error;
141}
142
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700143/**
144 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
145 * state.
146 * @state: state to enter
147 */
148int suspend_devices_and_enter(suspend_state_t state)
149{
150 int error;
151
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700152 if (!suspend_ops)
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700153 return -ENOSYS;
154
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700155 if (suspend_ops->set_target) {
156 error = suspend_ops->set_target(state);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700157 if (error)
158 return error;
159 }
160 suspend_console();
161 error = device_suspend(PMSG_SUSPEND);
162 if (error) {
163 printk(KERN_ERR "Some devices failed to suspend\n");
164 goto Resume_console;
165 }
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700166 if (suspend_ops->prepare) {
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700167 error = suspend_ops->prepare();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700168 if (error)
169 goto Resume_devices;
170 }
171 error = disable_nonboot_cpus();
172 if (!error)
173 suspend_enter(state);
174
175 enable_nonboot_cpus();
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700176 if (suspend_ops->finish)
177 suspend_ops->finish();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700178 Resume_devices:
179 device_resume();
180 Resume_console:
181 resume_console();
182 return error;
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185/**
186 * suspend_finish - Do final work before exiting suspend sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 *
188 * Call platform code to clean up, restart processes, and free the
189 * console that we've allocated. This is not called for suspend-to-disk.
190 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700191static void suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 thaw_processes();
194 pm_restore_console();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700195 pm_notifier_call_chain(PM_POST_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198
199
200
Andreas Mohr3b364b82006-06-25 05:47:56 -0700201static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 [PM_SUSPEND_STANDBY] = "standby",
203 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Pavel Machek123d3c12005-11-29 19:34:37 -0800206static inline int valid_state(suspend_state_t state)
207{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700208 /* All states need lowlevel support and need to be valid
209 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700210 * implies that none are valid. */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700211 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800212 return 0;
213 return 1;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/**
218 * enter_state - Do common work of entering low-power state.
219 * @state: pm_state structure for state we're entering.
220 *
221 * Make sure we're the only ones trying to enter a sleep state. Fail
222 * if someone has beat us to it, since we don't want anything weird to
223 * happen when we wake up.
224 * Then, do the setup for suspend, enter the state, and cleaup (after
225 * we've woken up).
226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static int enter_state(suspend_state_t state)
228{
229 int error;
230
Pavel Machek123d3c12005-11-29 19:34:37 -0800231 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800232 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800233 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return -EBUSY;
235
David Brownell82428b62005-05-09 08:07:00 -0700236 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700237 if ((error = suspend_prepare()))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto Unlock;
239
David Brownell82428b62005-05-09 08:07:00 -0700240 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700241 error = suspend_devices_and_enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
David Brownell82428b62005-05-09 08:07:00 -0700243 pr_debug("PM: Finishing wakeup.\n");
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700244 suspend_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800246 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return error;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/**
252 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700253 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *
255 * Determine whether or not value is within range, get state
256 * structure, and enter (above).
257 */
258
259int pm_suspend(suspend_state_t state)
260{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500261 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return enter_state(state);
263 return -EINVAL;
264}
265
Ralf Baechle4cf30342006-12-06 20:36:06 -0800266EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200268#endif /* CONFIG_SUSPEND */
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270decl_subsys(power,NULL,NULL);
271
272
273/**
274 * state - control system power state.
275 *
276 * show() returns what states are supported, which is hard-coded to
277 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
278 * 'disk' (Suspend-to-Disk).
279 *
280 * store() accepts one of those strings, translates it into the
281 * proper enumerated value, and initiates a suspend transition.
282 */
283
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700284static ssize_t state_show(struct kset *kset, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200286 char *s = buf;
287#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800291 if (pm_states[i] && valid_state(i))
292 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200294#endif
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{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200307#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700309 const char * const *s;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 int len;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200313 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 p = memchr(buf, '\n', n);
316 len = p ? p - buf : n;
317
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700318 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700319 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700320 error = hibernate();
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200321 goto Exit;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700322 }
323
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200324#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700326 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
328 }
dean gaudet47bb7892006-04-27 18:39:17 -0700329 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 error = enter_state(state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200331#endif
332
333 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return error ? error : n;
335}
336
337power_attr(state);
338
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700339#ifdef CONFIG_PM_TRACE
340int pm_trace_enabled;
341
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700342static ssize_t pm_trace_show(struct kset *kset, char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700343{
344 return sprintf(buf, "%d\n", pm_trace_enabled);
345}
346
347static ssize_t
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700348pm_trace_store(struct kset *kset, const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700349{
350 int val;
351
352 if (sscanf(buf, "%d", &val) == 1) {
353 pm_trace_enabled = !!val;
354 return n;
355 }
356 return -EINVAL;
357}
358
359power_attr(pm_trace);
360
361static struct attribute * g[] = {
362 &state_attr.attr,
363 &pm_trace_attr.attr,
364 NULL,
365};
366#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static struct attribute * g[] = {
368 &state_attr.attr,
369 NULL,
370};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700371#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373static struct attribute_group attr_group = {
374 .attrs = g,
375};
376
377
378static int __init pm_init(void)
379{
380 int error = subsystem_register(&power_subsys);
381 if (!error)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700382 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return error;
384}
385
386core_initcall(pm_init);