blob: 3062e940d1fa6e77c01778e1a037cfaf6abde397 [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>
18#include <linux/pm.h>
Andrew Morton6cc07192006-06-22 14:47:18 -070019#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070020#include <linux/cpu.h>
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -070021#include <linux/resume-trace.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080022#include <linux/freezer.h>
Christoph Lameter96177292007-02-10 01:43:03 -080023#include <linux/vmstat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "power.h"
26
David Shaohua Li5ae947e2005-03-18 16:27:13 -050027/*This is just an arbitrary number */
28#define FREE_PAGE_NUMBER (100)
29
Stephen Hemmingera6d70982006-12-06 20:34:35 -080030DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Pavel Machek123d3c12005-11-29 19:34:37 -080032struct pm_ops *pm_ops;
Rafael J. Wysocki9185cfa2006-11-01 13:23:14 +010033suspend_disk_method_t pm_disk_mode = PM_DISK_PLATFORM;
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
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080047static inline void pm_finish(suspend_state_t state)
48{
49 if (pm_ops->finish)
50 pm_ops->finish(state);
51}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/**
54 * suspend_prepare - Do prep work before entering low-power state.
55 * @state: State we're entering.
56 *
57 * This is common code that is called for each state that we're
58 * entering. Allocate a console, stop all processes, then make sure
59 * the platform can enter the requested state.
60 */
61
62static int suspend_prepare(suspend_state_t state)
63{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070064 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050065 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (!pm_ops || !pm_ops->enter)
68 return -EPERM;
69
70 pm_prepare_console();
71
72 if (freeze_processes()) {
73 error = -EAGAIN;
74 goto Thaw;
75 }
76
Christoph Lameter96177292007-02-10 01:43:03 -080077 if ((free_pages = global_page_state(NR_FREE_PAGES))
78 < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050079 pr_debug("PM: free some memory\n");
80 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
81 if (nr_free_pages() < FREE_PAGE_NUMBER) {
82 error = -ENOMEM;
83 printk(KERN_ERR "PM: No enough memory\n");
84 goto Thaw;
85 }
86 }
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (pm_ops->prepare) {
89 if ((error = pm_ops->prepare(state)))
90 goto Thaw;
91 }
92
Linus Torvalds557240b2006-06-19 18:16:01 -070093 suspend_console();
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080094 error = device_suspend(PMSG_SUSPEND);
95 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 printk(KERN_ERR "Some devices failed to suspend\n");
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080097 goto Resume_devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -080099 error = disable_nonboot_cpus();
100 if (!error)
101 return 0;
102
103 enable_nonboot_cpus();
104 Resume_devices:
105 pm_finish(state);
106 device_resume();
107 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 Thaw:
109 thaw_processes();
110 pm_restore_console();
111 return error;
112}
113
Johannes Berga53c46d2007-04-26 11:43:58 +0200114/* default implementation */
115void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
116{
117 local_irq_disable();
118}
119
120/* default implementation */
121void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
122{
123 local_irq_enable();
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Luca Tettamanti9b238202006-03-23 03:00:09 -0800126int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Johannes Berga53c46d2007-04-26 11:43:58 +0200130 arch_suspend_disable_irqs();
131 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if ((error = device_power_down(PMSG_SUSPEND))) {
134 printk(KERN_ERR "Some devices failed to power down\n");
135 goto Done;
136 }
137 error = pm_ops->enter(state);
138 device_power_up();
139 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200140 arch_suspend_enable_irqs();
141 BUG_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return error;
143}
144
145
146/**
147 * suspend_finish - Do final work before exiting suspend sequence.
148 * @state: State we're coming out of.
149 *
150 * Call platform code to clean up, restart processes, and free the
151 * console that we've allocated. This is not called for suspend-to-disk.
152 */
153
154static void suspend_finish(suspend_state_t state)
155{
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800156 enable_nonboot_cpus();
157 pm_finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700159 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 thaw_processes();
161 pm_restore_console();
162}
163
164
165
166
Andreas Mohr3b364b82006-06-25 05:47:56 -0700167static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 [PM_SUSPEND_STANDBY] = "standby",
169 [PM_SUSPEND_MEM] = "mem",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700170#ifdef CONFIG_SOFTWARE_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 [PM_SUSPEND_DISK] = "disk",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
Pavel Machek123d3c12005-11-29 19:34:37 -0800175static inline int valid_state(suspend_state_t state)
176{
177 /* Suspend-to-disk does not really need low-level support.
178 * It can work with reboot if needed. */
179 if (state == PM_SUSPEND_DISK)
180 return 1;
181
Johannes Berg9c372d02007-02-16 01:38:29 -0800182 /* all other states need lowlevel support and need to be
183 * valid to the lowlevel implementation, no valid callback
184 * implies that all are valid. */
185 if (!pm_ops || (pm_ops->valid && !pm_ops->valid(state)))
Pavel Machek123d3c12005-11-29 19:34:37 -0800186 return 0;
187 return 1;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/**
192 * enter_state - Do common work of entering low-power state.
193 * @state: pm_state structure for state we're entering.
194 *
195 * Make sure we're the only ones trying to enter a sleep state. Fail
196 * if someone has beat us to it, since we don't want anything weird to
197 * happen when we wake up.
198 * Then, do the setup for suspend, enter the state, and cleaup (after
199 * we've woken up).
200 */
201
202static int enter_state(suspend_state_t state)
203{
204 int error;
205
Pavel Machek123d3c12005-11-29 19:34:37 -0800206 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800207 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800208 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return -EBUSY;
210
211 if (state == PM_SUSPEND_DISK) {
212 error = pm_suspend_disk();
213 goto Unlock;
214 }
215
David Brownell82428b62005-05-09 08:07:00 -0700216 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if ((error = suspend_prepare(state)))
218 goto Unlock;
219
David Brownell82428b62005-05-09 08:07:00 -0700220 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 error = suspend_enter(state);
222
David Brownell82428b62005-05-09 08:07:00 -0700223 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 suspend_finish(state);
225 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800226 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return error;
228}
229
230/*
231 * This is main interface to the outside world. It needs to be
232 * called from process context.
233 */
234int software_suspend(void)
235{
236 return enter_state(PM_SUSPEND_DISK);
237}
238
239
240/**
241 * pm_suspend - Externally visible function for suspending system.
242 * @state: Enumarted value of state to enter.
243 *
244 * Determine whether or not value is within range, get state
245 * structure, and enter (above).
246 */
247
248int pm_suspend(suspend_state_t state)
249{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500250 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return enter_state(state);
252 return -EINVAL;
253}
254
Ralf Baechle4cf30342006-12-06 20:36:06 -0800255EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257decl_subsys(power,NULL,NULL);
258
259
260/**
261 * state - control system power state.
262 *
263 * show() returns what states are supported, which is hard-coded to
264 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
265 * 'disk' (Suspend-to-Disk).
266 *
267 * store() accepts one of those strings, translates it into the
268 * proper enumerated value, and initiates a suspend transition.
269 */
270
271static ssize_t state_show(struct subsystem * subsys, char * buf)
272{
273 int i;
274 char * s = buf;
275
276 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800277 if (pm_states[i] && valid_state(i))
278 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 s += sprintf(s,"\n");
281 return (s - buf);
282}
283
284static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
285{
286 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700287 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 char *p;
289 int error;
290 int len;
291
292 p = memchr(buf, '\n', n);
293 len = p ? p - buf : n;
294
295 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
296 if (*s && !strncmp(buf, *s, len))
297 break;
298 }
dean gaudet47bb7892006-04-27 18:39:17 -0700299 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 error = enter_state(state);
301 else
302 error = -EINVAL;
303 return error ? error : n;
304}
305
306power_attr(state);
307
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700308#ifdef CONFIG_PM_TRACE
309int pm_trace_enabled;
310
311static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
312{
313 return sprintf(buf, "%d\n", pm_trace_enabled);
314}
315
316static ssize_t
317pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
318{
319 int val;
320
321 if (sscanf(buf, "%d", &val) == 1) {
322 pm_trace_enabled = !!val;
323 return n;
324 }
325 return -EINVAL;
326}
327
328power_attr(pm_trace);
329
330static struct attribute * g[] = {
331 &state_attr.attr,
332 &pm_trace_attr.attr,
333 NULL,
334};
335#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static struct attribute * g[] = {
337 &state_attr.attr,
338 NULL,
339};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700340#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342static struct attribute_group attr_group = {
343 .attrs = g,
344};
345
346
347static int __init pm_init(void)
348{
349 int error = subsystem_register(&power_subsys);
350 if (!error)
351 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
352 return error;
353}
354
355core_initcall(pm_init);