blob: 47ca5a2b653bdf26ac2aaa65e5b5df7a261182e3 [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
47
48/**
49 * suspend_prepare - Do prep work before entering low-power state.
50 * @state: State we're entering.
51 *
52 * This is common code that is called for each state that we're
53 * entering. Allocate a console, stop all processes, then make sure
54 * the platform can enter the requested state.
55 */
56
57static int suspend_prepare(suspend_state_t state)
58{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070059 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050060 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (!pm_ops || !pm_ops->enter)
63 return -EPERM;
64
65 pm_prepare_console();
66
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070067 error = disable_nonboot_cpus();
68 if (error)
Li Shaohua5a72e042005-06-25 14:55:06 -070069 goto Enable_cpu;
Li Shaohua5a72e042005-06-25 14:55:06 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (freeze_processes()) {
72 error = -EAGAIN;
73 goto Thaw;
74 }
75
Christoph Lameter96177292007-02-10 01:43:03 -080076 if ((free_pages = global_page_state(NR_FREE_PAGES))
77 < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -050078 pr_debug("PM: free some memory\n");
79 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
80 if (nr_free_pages() < FREE_PAGE_NUMBER) {
81 error = -ENOMEM;
82 printk(KERN_ERR "PM: No enough memory\n");
83 goto Thaw;
84 }
85 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (pm_ops->prepare) {
88 if ((error = pm_ops->prepare(state)))
89 goto Thaw;
90 }
91
Linus Torvalds557240b2006-06-19 18:16:01 -070092 suspend_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if ((error = device_suspend(PMSG_SUSPEND))) {
94 printk(KERN_ERR "Some devices failed to suspend\n");
95 goto Finish;
96 }
97 return 0;
98 Finish:
99 if (pm_ops->finish)
100 pm_ops->finish(state);
101 Thaw:
102 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700103 Enable_cpu:
104 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 pm_restore_console();
106 return error;
107}
108
109
Luca Tettamanti9b238202006-03-23 03:00:09 -0800110int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 int error = 0;
113 unsigned long flags;
114
115 local_irq_save(flags);
116
117 if ((error = device_power_down(PMSG_SUSPEND))) {
118 printk(KERN_ERR "Some devices failed to power down\n");
119 goto Done;
120 }
121 error = pm_ops->enter(state);
122 device_power_up();
123 Done:
124 local_irq_restore(flags);
125 return error;
126}
127
128
129/**
130 * suspend_finish - Do final work before exiting suspend sequence.
131 * @state: State we're coming out of.
132 *
133 * Call platform code to clean up, restart processes, and free the
134 * console that we've allocated. This is not called for suspend-to-disk.
135 */
136
137static void suspend_finish(suspend_state_t state)
138{
139 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700140 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700142 enable_nonboot_cpus();
David Shaohua Li1a384162005-11-23 12:36:00 -0500143 if (pm_ops && pm_ops->finish)
144 pm_ops->finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 pm_restore_console();
146}
147
148
149
150
Andreas Mohr3b364b82006-06-25 05:47:56 -0700151static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 [PM_SUSPEND_STANDBY] = "standby",
153 [PM_SUSPEND_MEM] = "mem",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700154#ifdef CONFIG_SOFTWARE_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 [PM_SUSPEND_DISK] = "disk",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
Pavel Machek123d3c12005-11-29 19:34:37 -0800159static inline int valid_state(suspend_state_t state)
160{
161 /* Suspend-to-disk does not really need low-level support.
162 * It can work with reboot if needed. */
163 if (state == PM_SUSPEND_DISK)
164 return 1;
165
166 if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
167 return 0;
168 return 1;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/**
173 * enter_state - Do common work of entering low-power state.
174 * @state: pm_state structure for state we're entering.
175 *
176 * Make sure we're the only ones trying to enter a sleep state. Fail
177 * if someone has beat us to it, since we don't want anything weird to
178 * happen when we wake up.
179 * Then, do the setup for suspend, enter the state, and cleaup (after
180 * we've woken up).
181 */
182
183static int enter_state(suspend_state_t state)
184{
185 int error;
186
Pavel Machek123d3c12005-11-29 19:34:37 -0800187 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800188 return -ENODEV;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800189 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return -EBUSY;
191
192 if (state == PM_SUSPEND_DISK) {
193 error = pm_suspend_disk();
194 goto Unlock;
195 }
196
David Brownell82428b62005-05-09 08:07:00 -0700197 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if ((error = suspend_prepare(state)))
199 goto Unlock;
200
David Brownell82428b62005-05-09 08:07:00 -0700201 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 error = suspend_enter(state);
203
David Brownell82428b62005-05-09 08:07:00 -0700204 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 suspend_finish(state);
206 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800207 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return error;
209}
210
211/*
212 * This is main interface to the outside world. It needs to be
213 * called from process context.
214 */
215int software_suspend(void)
216{
217 return enter_state(PM_SUSPEND_DISK);
218}
219
220
221/**
222 * pm_suspend - Externally visible function for suspending system.
223 * @state: Enumarted value of state to enter.
224 *
225 * Determine whether or not value is within range, get state
226 * structure, and enter (above).
227 */
228
229int pm_suspend(suspend_state_t state)
230{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500231 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return enter_state(state);
233 return -EINVAL;
234}
235
Ralf Baechle4cf30342006-12-06 20:36:06 -0800236EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238decl_subsys(power,NULL,NULL);
239
240
241/**
242 * state - control system power state.
243 *
244 * show() returns what states are supported, which is hard-coded to
245 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
246 * 'disk' (Suspend-to-Disk).
247 *
248 * store() accepts one of those strings, translates it into the
249 * proper enumerated value, and initiates a suspend transition.
250 */
251
252static ssize_t state_show(struct subsystem * subsys, char * buf)
253{
254 int i;
255 char * s = buf;
256
257 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800258 if (pm_states[i] && valid_state(i))
259 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261 s += sprintf(s,"\n");
262 return (s - buf);
263}
264
265static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
266{
267 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700268 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 char *p;
270 int error;
271 int len;
272
273 p = memchr(buf, '\n', n);
274 len = p ? p - buf : n;
275
276 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
277 if (*s && !strncmp(buf, *s, len))
278 break;
279 }
dean gaudet47bb7892006-04-27 18:39:17 -0700280 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 error = enter_state(state);
282 else
283 error = -EINVAL;
284 return error ? error : n;
285}
286
287power_attr(state);
288
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700289#ifdef CONFIG_PM_TRACE
290int pm_trace_enabled;
291
292static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
293{
294 return sprintf(buf, "%d\n", pm_trace_enabled);
295}
296
297static ssize_t
298pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
299{
300 int val;
301
302 if (sscanf(buf, "%d", &val) == 1) {
303 pm_trace_enabled = !!val;
304 return n;
305 }
306 return -EINVAL;
307}
308
309power_attr(pm_trace);
310
311static struct attribute * g[] = {
312 &state_attr.attr,
313 &pm_trace_attr.attr,
314 NULL,
315};
316#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317static struct attribute * g[] = {
318 &state_attr.attr,
319 NULL,
320};
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700321#endif /* CONFIG_PM_TRACE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323static struct attribute_group attr_group = {
324 .attrs = g,
325};
326
327
328static int __init pm_init(void)
329{
330 int error = subsystem_register(&power_subsys);
331 if (!error)
332 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
333 return error;
334}
335
336core_initcall(pm_init);