blob: 18d7d693fbba852c48acc40de4ad8809baf5d36e [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>
18
19
20#include "power.h"
21
David Shaohua Li5ae947e2005-03-18 16:27:13 -050022/*This is just an arbitrary number */
23#define FREE_PAGE_NUMBER (100)
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025DECLARE_MUTEX(pm_sem);
26
27struct pm_ops * pm_ops = NULL;
28suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
29
30/**
31 * pm_set_ops - Set the global power method table.
32 * @ops: Pointer to ops structure.
33 */
34
35void pm_set_ops(struct pm_ops * ops)
36{
37 down(&pm_sem);
38 pm_ops = ops;
39 up(&pm_sem);
40}
41
42
43/**
44 * suspend_prepare - Do prep work before entering low-power state.
45 * @state: State we're entering.
46 *
47 * This is common code that is called for each state that we're
48 * entering. Allocate a console, stop all processes, then make sure
49 * the platform can enter the requested state.
50 */
51
52static int suspend_prepare(suspend_state_t state)
53{
54 int error = 0;
David Shaohua Li5ae947e2005-03-18 16:27:13 -050055 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 if (!pm_ops || !pm_ops->enter)
58 return -EPERM;
59
60 pm_prepare_console();
61
Li Shaohua5a72e042005-06-25 14:55:06 -070062 disable_nonboot_cpus();
63
64 if (num_online_cpus() != 1) {
65 error = -EPERM;
66 goto Enable_cpu;
67 }
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (freeze_processes()) {
70 error = -EAGAIN;
71 goto Thaw;
72 }
73
David Shaohua Li5ae947e2005-03-18 16:27:13 -050074 if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) {
75 pr_debug("PM: free some memory\n");
76 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
77 if (nr_free_pages() < FREE_PAGE_NUMBER) {
78 error = -ENOMEM;
79 printk(KERN_ERR "PM: No enough memory\n");
80 goto Thaw;
81 }
82 }
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (pm_ops->prepare) {
85 if ((error = pm_ops->prepare(state)))
86 goto Thaw;
87 }
88
89 if ((error = device_suspend(PMSG_SUSPEND))) {
90 printk(KERN_ERR "Some devices failed to suspend\n");
91 goto Finish;
92 }
93 return 0;
94 Finish:
95 if (pm_ops->finish)
96 pm_ops->finish(state);
97 Thaw:
98 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -070099 Enable_cpu:
100 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 pm_restore_console();
102 return error;
103}
104
105
106static int suspend_enter(suspend_state_t state)
107{
108 int error = 0;
109 unsigned long flags;
110
111 local_irq_save(flags);
112
113 if ((error = device_power_down(PMSG_SUSPEND))) {
114 printk(KERN_ERR "Some devices failed to power down\n");
115 goto Done;
116 }
117 error = pm_ops->enter(state);
118 device_power_up();
119 Done:
120 local_irq_restore(flags);
121 return error;
122}
123
124
125/**
126 * suspend_finish - Do final work before exiting suspend sequence.
127 * @state: State we're coming out of.
128 *
129 * Call platform code to clean up, restart processes, and free the
130 * console that we've allocated. This is not called for suspend-to-disk.
131 */
132
133static void suspend_finish(suspend_state_t state)
134{
135 device_resume();
136 if (pm_ops && pm_ops->finish)
137 pm_ops->finish(state);
138 thaw_processes();
Li Shaohua5a72e042005-06-25 14:55:06 -0700139 enable_nonboot_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 pm_restore_console();
141}
142
143
144
145
Pavel Machek57c4ce32005-09-03 15:57:06 -0700146static char *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
154
155/**
156 * enter_state - Do common work of entering low-power state.
157 * @state: pm_state structure for state we're entering.
158 *
159 * Make sure we're the only ones trying to enter a sleep state. Fail
160 * if someone has beat us to it, since we don't want anything weird to
161 * happen when we wake up.
162 * Then, do the setup for suspend, enter the state, and cleaup (after
163 * we've woken up).
164 */
165
166static int enter_state(suspend_state_t state)
167{
168 int error;
169
Shaohua Lieb9289e2005-10-30 15:00:01 -0800170 if (pm_ops->valid && !pm_ops->valid(state))
171 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (down_trylock(&pm_sem))
173 return -EBUSY;
174
175 if (state == PM_SUSPEND_DISK) {
176 error = pm_suspend_disk();
177 goto Unlock;
178 }
179
David Brownell82428b62005-05-09 08:07:00 -0700180 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if ((error = suspend_prepare(state)))
182 goto Unlock;
183
David Brownell82428b62005-05-09 08:07:00 -0700184 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 error = suspend_enter(state);
186
David Brownell82428b62005-05-09 08:07:00 -0700187 pr_debug("PM: Finishing wakeup.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 suspend_finish(state);
189 Unlock:
190 up(&pm_sem);
191 return error;
192}
193
194/*
195 * This is main interface to the outside world. It needs to be
196 * called from process context.
197 */
198int software_suspend(void)
199{
200 return enter_state(PM_SUSPEND_DISK);
201}
202
203
204/**
205 * pm_suspend - Externally visible function for suspending system.
206 * @state: Enumarted value of state to enter.
207 *
208 * Determine whether or not value is within range, get state
209 * structure, and enter (above).
210 */
211
212int pm_suspend(suspend_state_t state)
213{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500214 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return enter_state(state);
216 return -EINVAL;
217}
218
219
220
221decl_subsys(power,NULL,NULL);
222
223
224/**
225 * state - control system power state.
226 *
227 * show() returns what states are supported, which is hard-coded to
228 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
229 * 'disk' (Suspend-to-Disk).
230 *
231 * store() accepts one of those strings, translates it into the
232 * proper enumerated value, and initiates a suspend transition.
233 */
234
235static ssize_t state_show(struct subsystem * subsys, char * buf)
236{
237 int i;
238 char * s = buf;
239
240 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Shaohua Lieb9289e2005-10-30 15:00:01 -0800241 if (pm_states[i] && pm_ops && (!pm_ops->valid
242 ||(pm_ops->valid && pm_ops->valid(i))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 s += sprintf(s,"%s ",pm_states[i]);
244 }
245 s += sprintf(s,"\n");
246 return (s - buf);
247}
248
249static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
250{
251 suspend_state_t state = PM_SUSPEND_STANDBY;
252 char ** s;
253 char *p;
254 int error;
255 int len;
256
257 p = memchr(buf, '\n', n);
258 len = p ? p - buf : n;
259
260 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
261 if (*s && !strncmp(buf, *s, len))
262 break;
263 }
264 if (*s)
265 error = enter_state(state);
266 else
267 error = -EINVAL;
268 return error ? error : n;
269}
270
271power_attr(state);
272
273static struct attribute * g[] = {
274 &state_attr.attr,
275 NULL,
276};
277
278static struct attribute_group attr_group = {
279 .attrs = g,
280};
281
282
283static int __init pm_init(void)
284{
285 int error = subsystem_register(&power_subsys);
286 if (!error)
287 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
288 return error;
289}
290
291core_initcall(pm_init);