swsusp: do not use page flags
[linux-2.6.git] / kernel / power / main.c
1 /*
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/module.h>
12 #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>
19 #include <linux/console.h>
20 #include <linux/cpu.h>
21 #include <linux/resume-trace.h>
22 #include <linux/freezer.h>
23 #include <linux/vmstat.h>
24
25 #include "power.h"
26
27 /*This is just an arbitrary number */
28 #define FREE_PAGE_NUMBER (100)
29
30 DEFINE_MUTEX(pm_mutex);
31
32 struct pm_ops *pm_ops;
33 suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
34
35 /**
36  *      pm_set_ops - Set the global power method table. 
37  *      @ops:   Pointer to ops structure.
38  */
39
40 void pm_set_ops(struct pm_ops * ops)
41 {
42         mutex_lock(&pm_mutex);
43         pm_ops = ops;
44         if (ops && ops->pm_disk_mode != PM_DISK_INVALID) {
45                 pm_disk_mode = ops->pm_disk_mode;
46         } else
47                 pm_disk_mode = PM_DISK_SHUTDOWN;
48         mutex_unlock(&pm_mutex);
49 }
50
51 /**
52  * pm_valid_only_mem - generic memory-only valid callback
53  *
54  * pm_ops drivers that implement mem suspend only and only need
55  * to check for that in their .valid callback can use this instead
56  * of rolling their own .valid callback.
57  */
58 int pm_valid_only_mem(suspend_state_t state)
59 {
60         return state == PM_SUSPEND_MEM;
61 }
62
63
64 static inline void pm_finish(suspend_state_t state)
65 {
66         if (pm_ops->finish)
67                 pm_ops->finish(state);
68 }
69
70 /**
71  *      suspend_prepare - Do prep work before entering low-power state.
72  *      @state:         State we're entering.
73  *
74  *      This is common code that is called for each state that we're 
75  *      entering. Allocate a console, stop all processes, then make sure
76  *      the platform can enter the requested state.
77  */
78
79 static int suspend_prepare(suspend_state_t state)
80 {
81         int error;
82         unsigned int free_pages;
83
84         if (!pm_ops || !pm_ops->enter)
85                 return -EPERM;
86
87         pm_prepare_console();
88
89         if (freeze_processes()) {
90                 error = -EAGAIN;
91                 goto Thaw;
92         }
93
94         if ((free_pages = global_page_state(NR_FREE_PAGES))
95                         < FREE_PAGE_NUMBER) {
96                 pr_debug("PM: free some memory\n");
97                 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
98                 if (nr_free_pages() < FREE_PAGE_NUMBER) {
99                         error = -ENOMEM;
100                         printk(KERN_ERR "PM: No enough memory\n");
101                         goto Thaw;
102                 }
103         }
104
105         if (pm_ops->prepare) {
106                 if ((error = pm_ops->prepare(state)))
107                         goto Thaw;
108         }
109
110         suspend_console();
111         error = device_suspend(PMSG_SUSPEND);
112         if (error) {
113                 printk(KERN_ERR "Some devices failed to suspend\n");
114                 goto Resume_devices;
115         }
116         error = disable_nonboot_cpus();
117         if (!error)
118                 return 0;
119
120         enable_nonboot_cpus();
121  Resume_devices:
122         pm_finish(state);
123         device_resume();
124         resume_console();
125  Thaw:
126         thaw_processes();
127         pm_restore_console();
128         return error;
129 }
130
131 /* default implementation */
132 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
133 {
134         local_irq_disable();
135 }
136
137 /* default implementation */
138 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
139 {
140         local_irq_enable();
141 }
142
143 int suspend_enter(suspend_state_t state)
144 {
145         int error = 0;
146
147         arch_suspend_disable_irqs();
148         BUG_ON(!irqs_disabled());
149
150         if ((error = device_power_down(PMSG_SUSPEND))) {
151                 printk(KERN_ERR "Some devices failed to power down\n");
152                 goto Done;
153         }
154         error = pm_ops->enter(state);
155         device_power_up();
156  Done:
157         arch_suspend_enable_irqs();
158         BUG_ON(irqs_disabled());
159         return error;
160 }
161
162
163 /**
164  *      suspend_finish - Do final work before exiting suspend sequence.
165  *      @state:         State we're coming out of.
166  *
167  *      Call platform code to clean up, restart processes, and free the 
168  *      console that we've allocated. This is not called for suspend-to-disk.
169  */
170
171 static void suspend_finish(suspend_state_t state)
172 {
173         enable_nonboot_cpus();
174         pm_finish(state);
175         device_resume();
176         resume_console();
177         thaw_processes();
178         pm_restore_console();
179 }
180
181
182
183
184 static const char * const pm_states[PM_SUSPEND_MAX] = {
185         [PM_SUSPEND_STANDBY]    = "standby",
186         [PM_SUSPEND_MEM]        = "mem",
187 #ifdef CONFIG_SOFTWARE_SUSPEND
188         [PM_SUSPEND_DISK]       = "disk",
189 #endif
190 };
191
192 static inline int valid_state(suspend_state_t state)
193 {
194         /* Suspend-to-disk does not really need low-level support.
195          * It can work with reboot if needed. */
196         if (state == PM_SUSPEND_DISK)
197                 return 1;
198
199         /* all other states need lowlevel support and need to be
200          * valid to the lowlevel implementation, no valid callback
201          * implies that none are valid. */
202         if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
203                 return 0;
204         return 1;
205 }
206
207
208 /**
209  *      enter_state - Do common work of entering low-power state.
210  *      @state:         pm_state structure for state we're entering.
211  *
212  *      Make sure we're the only ones trying to enter a sleep state. Fail
213  *      if someone has beat us to it, since we don't want anything weird to
214  *      happen when we wake up.
215  *      Then, do the setup for suspend, enter the state, and cleaup (after
216  *      we've woken up).
217  */
218
219 static int enter_state(suspend_state_t state)
220 {
221         int error;
222
223         if (!valid_state(state))
224                 return -ENODEV;
225         if (!mutex_trylock(&pm_mutex))
226                 return -EBUSY;
227
228         if (state == PM_SUSPEND_DISK) {
229                 error = pm_suspend_disk();
230                 goto Unlock;
231         }
232
233         pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
234         if ((error = suspend_prepare(state)))
235                 goto Unlock;
236
237         pr_debug("PM: Entering %s sleep\n", pm_states[state]);
238         error = suspend_enter(state);
239
240         pr_debug("PM: Finishing wakeup.\n");
241         suspend_finish(state);
242  Unlock:
243         mutex_unlock(&pm_mutex);
244         return error;
245 }
246
247 #ifdef CONFIG_SOFTWARE_SUSPEND
248 /*
249  * This is main interface to the outside world. It needs to be
250  * called from process context.
251  */
252 int software_suspend(void)
253 {
254         return enter_state(PM_SUSPEND_DISK);
255 }
256 #endif
257
258
259 /**
260  *      pm_suspend - Externally visible function for suspending system.
261  *      @state:         Enumarted value of state to enter.
262  *
263  *      Determine whether or not value is within range, get state 
264  *      structure, and enter (above).
265  */
266
267 int pm_suspend(suspend_state_t state)
268 {
269         if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
270                 return enter_state(state);
271         return -EINVAL;
272 }
273
274 EXPORT_SYMBOL(pm_suspend);
275
276 decl_subsys(power,NULL,NULL);
277
278
279 /**
280  *      state - control system power state.
281  *
282  *      show() returns what states are supported, which is hard-coded to
283  *      'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
284  *      'disk' (Suspend-to-Disk).
285  *
286  *      store() accepts one of those strings, translates it into the 
287  *      proper enumerated value, and initiates a suspend transition.
288  */
289
290 static ssize_t state_show(struct kset *kset, char *buf)
291 {
292         int i;
293         char * s = buf;
294
295         for (i = 0; i < PM_SUSPEND_MAX; i++) {
296                 if (pm_states[i] && valid_state(i))
297                         s += sprintf(s,"%s ", pm_states[i]);
298         }
299         s += sprintf(s,"\n");
300         return (s - buf);
301 }
302
303 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
304 {
305         suspend_state_t state = PM_SUSPEND_STANDBY;
306         const char * const *s;
307         char *p;
308         int error;
309         int len;
310
311         p = memchr(buf, '\n', n);
312         len = p ? p - buf : n;
313
314         for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
315                 if (*s && !strncmp(buf, *s, len))
316                         break;
317         }
318         if (state < PM_SUSPEND_MAX && *s)
319                 error = enter_state(state);
320         else
321                 error = -EINVAL;
322         return error ? error : n;
323 }
324
325 power_attr(state);
326
327 #ifdef CONFIG_PM_TRACE
328 int pm_trace_enabled;
329
330 static ssize_t pm_trace_show(struct kset *kset, char *buf)
331 {
332         return sprintf(buf, "%d\n", pm_trace_enabled);
333 }
334
335 static ssize_t
336 pm_trace_store(struct kset *kset, const char *buf, size_t n)
337 {
338         int val;
339
340         if (sscanf(buf, "%d", &val) == 1) {
341                 pm_trace_enabled = !!val;
342                 return n;
343         }
344         return -EINVAL;
345 }
346
347 power_attr(pm_trace);
348
349 static struct attribute * g[] = {
350         &state_attr.attr,
351         &pm_trace_attr.attr,
352         NULL,
353 };
354 #else
355 static struct attribute * g[] = {
356         &state_attr.attr,
357         NULL,
358 };
359 #endif /* CONFIG_PM_TRACE */
360
361 static struct attribute_group attr_group = {
362         .attrs = g,
363 };
364
365
366 static int __init pm_init(void)
367 {
368         int error = subsystem_register(&power_subsys);
369         if (!error)
370                 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
371         return error;
372 }
373
374 core_initcall(pm_init);