blob: 4d403323a7bb709c6384f7a5085f3cdb0872c360 [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
11#include <linux/suspend.h>
12#include <linux/kobject.h>
13#include <linux/string.h>
14#include <linux/delay.h>
15#include <linux/errno.h>
16#include <linux/init.h>
17#include <linux/pm.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "power.h"
22
David Shaohua Li5ae947e2005-03-18 16:27:13 -050023/*This is just an arbitrary number */
24#define FREE_PAGE_NUMBER (100)
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026DECLARE_MUTEX(pm_sem);
27
Pavel Machek123d3c12005-11-29 19:34:37 -080028struct pm_ops *pm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
30
31/**
32 * pm_set_ops - Set the global power method table.
33 * @ops: Pointer to ops structure.
34 */
35
36void pm_set_ops(struct pm_ops * ops)
37{
38 down(&pm_sem);
39 pm_ops = ops;
40 up(&pm_sem);
41}
42
43
44/**
45 * suspend_prepare - Do prep work before entering low-power state.
46 * @state: State we're entering.
47 *
48 * This is common code that is called for each state that we're
49 * entering. Allocate a console, stop all processes, then make sure
50 * the platform can enter the requested state.
51 */
52
53static int suspend_prepare(suspend_state_t state)
54{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070055 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050056 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if (!pm_ops || !pm_ops->enter)
59 return -EPERM;
60
61 pm_prepare_console();
62
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070063 error = disable_nonboot_cpus();
64 if (error)
Li Shaohua5a72e042005-06-25 14:55:06 -070065 goto Enable_cpu;
Li Shaohua5a72e042005-06-25 14:55:06 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (freeze_processes()) {
68 error = -EAGAIN;
69 goto Thaw;
70 }
71
David Shaohua Li5ae947e2005-03-18 16:27:13 -050072 if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) {
73 pr_debug("PM: free some memory\n");
74 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
75 if (nr_free_pages() < FREE_PAGE_NUMBER) {
76 error = -ENOMEM;
77 printk(KERN_ERR "PM: No enough memory\n");
78 goto Thaw;
79 }
80 }
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (pm_ops->prepare) {
83 if ((error = pm_ops->prepare(state)))
84 goto Thaw;
85 }
86
Linus Torvalds557240b2006-06-19 18:16:01 -070087 suspend_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if ((error = device_suspend(PMSG_SUSPEND))) {
89 printk(KERN_ERR "Some devices failed to suspend\n");
90 goto Finish;
91 }
92 return 0;
93 Finish:
94 if (pm_ops->finish)
95 pm_ops->finish(state);
96 Thaw:
97 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -070098 Enable_cpu:
99 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 pm_restore_console();
101 return error;
102}
103
104
Luca Tettamanti9b238202006-03-23 03:00:09 -0800105int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int error = 0;
108 unsigned long flags;
109
110 local_irq_save(flags);
111
112 if ((error = device_power_down(PMSG_SUSPEND))) {
113 printk(KERN_ERR "Some devices failed to power down\n");
114 goto Done;
115 }
116 error = pm_ops->enter(state);
117 device_power_up();
118 Done:
119 local_irq_restore(flags);
120 return error;
121}
122
123
124/**
125 * suspend_finish - Do final work before exiting suspend sequence.
126 * @state: State we're coming out of.
127 *
128 * Call platform code to clean up, restart processes, and free the
129 * console that we've allocated. This is not called for suspend-to-disk.
130 */
131
132static void suspend_finish(suspend_state_t state)
133{
134 device_resume();
Linus Torvalds557240b2006-06-19 18:16:01 -0700135 resume_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700137 enable_nonboot_cpus();
David Shaohua Li1a384162005-11-23 12:36:00 -0500138 if (pm_ops && pm_ops->finish)
139 pm_ops->finish(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 pm_restore_console();
141}
142
143
144
145
Andreas Mohr3b364b82006-06-25 05:47:56 -0700146static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 [PM_SUSPEND_STANDBY] = "standby",
148 [PM_SUSPEND_MEM] = "mem",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700149#ifdef CONFIG_SOFTWARE_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 [PM_SUSPEND_DISK] = "disk",
Pavel Machek57c4ce32005-09-03 15:57:06 -0700151#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
Pavel Machek123d3c12005-11-29 19:34:37 -0800154static inline int valid_state(suspend_state_t state)
155{
156 /* Suspend-to-disk does not really need low-level support.
157 * It can work with reboot if needed. */
158 if (state == PM_SUSPEND_DISK)
159 return 1;
160
161 if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
162 return 0;
163 return 1;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/**
168 * enter_state - Do common work of entering low-power state.
169 * @state: pm_state structure for state we're entering.
170 *
171 * Make sure we're the only ones trying to enter a sleep state. Fail
172 * if someone has beat us to it, since we don't want anything weird to
173 * happen when we wake up.
174 * Then, do the setup for suspend, enter the state, and cleaup (after
175 * we've woken up).
176 */
177
178static int enter_state(suspend_state_t state)
179{
180 int error;
181
Pavel Machek123d3c12005-11-29 19:34:37 -0800182 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800183 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (down_trylock(&pm_sem))
185 return -EBUSY;
186
187 if (state == PM_SUSPEND_DISK) {
188 error = pm_suspend_disk();
189 goto Unlock;
190 }
191
David Brownell82428b62005-05-09 08:07:00 -0700192 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if ((error = suspend_prepare(state)))
194 goto Unlock;
195
David Brownell82428b62005-05-09 08:07:00 -0700196 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 error = suspend_enter(state);
198
David Brownell82428b62005-05-09 08:07:00 -0700199 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 suspend_finish(state);
201 Unlock:
202 up(&pm_sem);
203 return error;
204}
205
206/*
207 * This is main interface to the outside world. It needs to be
208 * called from process context.
209 */
210int software_suspend(void)
211{
212 return enter_state(PM_SUSPEND_DISK);
213}
214
215
216/**
217 * pm_suspend - Externally visible function for suspending system.
218 * @state: Enumarted value of state to enter.
219 *
220 * Determine whether or not value is within range, get state
221 * structure, and enter (above).
222 */
223
224int pm_suspend(suspend_state_t state)
225{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500226 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return enter_state(state);
228 return -EINVAL;
229}
230
231
232
233decl_subsys(power,NULL,NULL);
234
235
236/**
237 * state - control system power state.
238 *
239 * show() returns what states are supported, which is hard-coded to
240 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
241 * 'disk' (Suspend-to-Disk).
242 *
243 * store() accepts one of those strings, translates it into the
244 * proper enumerated value, and initiates a suspend transition.
245 */
246
247static ssize_t state_show(struct subsystem * subsys, char * buf)
248{
249 int i;
250 char * s = buf;
251
252 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800253 if (pm_states[i] && valid_state(i))
254 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 s += sprintf(s,"\n");
257 return (s - buf);
258}
259
260static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
261{
262 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700263 const char * const *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 char *p;
265 int error;
266 int len;
267
268 p = memchr(buf, '\n', n);
269 len = p ? p - buf : n;
270
271 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
272 if (*s && !strncmp(buf, *s, len))
273 break;
274 }
dean gaudet47bb7892006-04-27 18:39:17 -0700275 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 error = enter_state(state);
277 else
278 error = -EINVAL;
279 return error ? error : n;
280}
281
282power_attr(state);
283
284static struct attribute * g[] = {
285 &state_attr.attr,
286 NULL,
287};
288
289static struct attribute_group attr_group = {
290 .attrs = g,
291};
292
293
294static int __init pm_init(void)
295{
296 int error = subsystem_register(&power_subsys);
297 if (!error)
298 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
299 return error;
300}
301
302core_initcall(pm_init);