suspend: build fix responding to 2.6.25 kset change
[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/console.h>
19 #include <linux/cpu.h>
20 #include <linux/resume-trace.h>
21 #include <linux/freezer.h>
22 #include <linux/vmstat.h>
23 #include <linux/syscalls.h>
24
25 #include "power.h"
26
27 BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28
29 DEFINE_MUTEX(pm_mutex);
30
31 unsigned int pm_flags;
32 EXPORT_SYMBOL(pm_flags);
33
34 #ifdef CONFIG_PM_DEBUG
35 int pm_test_level = TEST_NONE;
36
37 static int suspend_test(int level)
38 {
39         if (pm_test_level == level) {
40                 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
41                 mdelay(5000);
42                 return 1;
43         }
44         return 0;
45 }
46
47 static const char * const pm_tests[__TEST_AFTER_LAST] = {
48         [TEST_NONE] = "none",
49         [TEST_CORE] = "core",
50         [TEST_CPUS] = "processors",
51         [TEST_PLATFORM] = "platform",
52         [TEST_DEVICES] = "devices",
53         [TEST_FREEZER] = "freezer",
54 };
55
56 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
57                                 char *buf)
58 {
59         char *s = buf;
60         int level;
61
62         for (level = TEST_FIRST; level <= TEST_MAX; level++)
63                 if (pm_tests[level]) {
64                         if (level == pm_test_level)
65                                 s += sprintf(s, "[%s] ", pm_tests[level]);
66                         else
67                                 s += sprintf(s, "%s ", pm_tests[level]);
68                 }
69
70         if (s != buf)
71                 /* convert the last space to a newline */
72                 *(s-1) = '\n';
73
74         return (s - buf);
75 }
76
77 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
78                                 const char *buf, size_t n)
79 {
80         const char * const *s;
81         int level;
82         char *p;
83         int len;
84         int error = -EINVAL;
85
86         p = memchr(buf, '\n', n);
87         len = p ? p - buf : n;
88
89         mutex_lock(&pm_mutex);
90
91         level = TEST_FIRST;
92         for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
93                 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
94                         pm_test_level = level;
95                         error = 0;
96                         break;
97                 }
98
99         mutex_unlock(&pm_mutex);
100
101         return error ? error : n;
102 }
103
104 power_attr(pm_test);
105 #else /* !CONFIG_PM_DEBUG */
106 static inline int suspend_test(int level) { return 0; }
107 #endif /* !CONFIG_PM_DEBUG */
108
109
110 #ifdef CONFIG_SUSPEND
111
112 /* This is just an arbitrary number */
113 #define FREE_PAGE_NUMBER (100)
114
115 static struct platform_suspend_ops *suspend_ops;
116
117 /**
118  *      suspend_set_ops - Set the global suspend method table.
119  *      @ops:   Pointer to ops structure.
120  */
121
122 void suspend_set_ops(struct platform_suspend_ops *ops)
123 {
124         mutex_lock(&pm_mutex);
125         suspend_ops = ops;
126         mutex_unlock(&pm_mutex);
127 }
128
129 /**
130  * suspend_valid_only_mem - generic memory-only valid callback
131  *
132  * Platform drivers that implement mem suspend only and only need
133  * to check for that in their .valid callback can use this instead
134  * of rolling their own .valid callback.
135  */
136 int suspend_valid_only_mem(suspend_state_t state)
137 {
138         return state == PM_SUSPEND_MEM;
139 }
140
141 /**
142  *      suspend_prepare - Do prep work before entering low-power state.
143  *
144  *      This is common code that is called for each state that we're entering.
145  *      Run suspend notifiers, allocate a console and stop all processes.
146  */
147 static int suspend_prepare(void)
148 {
149         int error;
150         unsigned int free_pages;
151
152         if (!suspend_ops || !suspend_ops->enter)
153                 return -EPERM;
154
155         error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
156         if (error)
157                 goto Finish;
158
159         pm_prepare_console();
160
161         if (freeze_processes()) {
162                 error = -EAGAIN;
163                 goto Thaw;
164         }
165
166         free_pages = global_page_state(NR_FREE_PAGES);
167         if (free_pages < FREE_PAGE_NUMBER) {
168                 pr_debug("PM: free some memory\n");
169                 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
170                 if (nr_free_pages() < FREE_PAGE_NUMBER) {
171                         error = -ENOMEM;
172                         printk(KERN_ERR "PM: No enough memory\n");
173                 }
174         }
175         if (!error)
176                 return 0;
177
178  Thaw:
179         thaw_processes();
180         pm_restore_console();
181  Finish:
182         pm_notifier_call_chain(PM_POST_SUSPEND);
183         return error;
184 }
185
186 /* default implementation */
187 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
188 {
189         local_irq_disable();
190 }
191
192 /* default implementation */
193 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
194 {
195         local_irq_enable();
196 }
197
198 /**
199  *      suspend_enter - enter the desired system sleep state.
200  *      @state:         state to enter
201  *
202  *      This function should be called after devices have been suspended.
203  */
204 static int suspend_enter(suspend_state_t state)
205 {
206         int error = 0;
207
208         arch_suspend_disable_irqs();
209         BUG_ON(!irqs_disabled());
210
211         if ((error = device_power_down(PMSG_SUSPEND))) {
212                 printk(KERN_ERR "Some devices failed to power down\n");
213                 goto Done;
214         }
215
216         if (!suspend_test(TEST_CORE))
217                 error = suspend_ops->enter(state);
218
219         device_power_up();
220  Done:
221         arch_suspend_enable_irqs();
222         BUG_ON(irqs_disabled());
223         return error;
224 }
225
226 /**
227  *      suspend_devices_and_enter - suspend devices and enter the desired system sleep
228  *                        state.
229  *      @state:           state to enter
230  */
231 int suspend_devices_and_enter(suspend_state_t state)
232 {
233         int error;
234
235         if (!suspend_ops)
236                 return -ENOSYS;
237
238         if (suspend_ops->set_target) {
239                 error = suspend_ops->set_target(state);
240                 if (error)
241                         return error;
242         }
243         suspend_console();
244         error = device_suspend(PMSG_SUSPEND);
245         if (error) {
246                 printk(KERN_ERR "Some devices failed to suspend\n");
247                 goto Resume_console;
248         }
249
250         if (suspend_test(TEST_DEVICES))
251                 goto Resume_devices;
252
253         if (suspend_ops->prepare) {
254                 error = suspend_ops->prepare();
255                 if (error)
256                         goto Resume_devices;
257         }
258
259         if (suspend_test(TEST_PLATFORM))
260                 goto Finish;
261
262         error = disable_nonboot_cpus();
263         if (!error && !suspend_test(TEST_CPUS))
264                 suspend_enter(state);
265
266         enable_nonboot_cpus();
267  Finish:
268         if (suspend_ops->finish)
269                 suspend_ops->finish();
270  Resume_devices:
271         device_resume();
272  Resume_console:
273         resume_console();
274         return error;
275 }
276
277 /**
278  *      suspend_finish - Do final work before exiting suspend sequence.
279  *
280  *      Call platform code to clean up, restart processes, and free the 
281  *      console that we've allocated. This is not called for suspend-to-disk.
282  */
283 static void suspend_finish(void)
284 {
285         thaw_processes();
286         pm_restore_console();
287         pm_notifier_call_chain(PM_POST_SUSPEND);
288 }
289
290
291
292
293 static const char * const pm_states[PM_SUSPEND_MAX] = {
294         [PM_SUSPEND_STANDBY]    = "standby",
295         [PM_SUSPEND_MEM]        = "mem",
296 };
297
298 static inline int valid_state(suspend_state_t state)
299 {
300         /* All states need lowlevel support and need to be valid
301          * to the lowlevel implementation, no valid callback
302          * implies that none are valid. */
303         if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
304                 return 0;
305         return 1;
306 }
307
308
309 /**
310  *      enter_state - Do common work of entering low-power state.
311  *      @state:         pm_state structure for state we're entering.
312  *
313  *      Make sure we're the only ones trying to enter a sleep state. Fail
314  *      if someone has beat us to it, since we don't want anything weird to
315  *      happen when we wake up.
316  *      Then, do the setup for suspend, enter the state, and cleaup (after
317  *      we've woken up).
318  */
319 static int enter_state(suspend_state_t state)
320 {
321         int error;
322
323         if (!valid_state(state))
324                 return -ENODEV;
325
326         if (!mutex_trylock(&pm_mutex))
327                 return -EBUSY;
328
329         printk("Syncing filesystems ... ");
330         sys_sync();
331         printk("done.\n");
332
333         pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
334         error = suspend_prepare();
335         if (error)
336                 goto Unlock;
337
338         if (suspend_test(TEST_FREEZER))
339                 goto Finish;
340
341         pr_debug("PM: Entering %s sleep\n", pm_states[state]);
342         error = suspend_devices_and_enter(state);
343
344  Finish:
345         pr_debug("PM: Finishing wakeup.\n");
346         suspend_finish();
347  Unlock:
348         mutex_unlock(&pm_mutex);
349         return error;
350 }
351
352
353 /**
354  *      pm_suspend - Externally visible function for suspending system.
355  *      @state:         Enumerated value of state to enter.
356  *
357  *      Determine whether or not value is within range, get state 
358  *      structure, and enter (above).
359  */
360
361 int pm_suspend(suspend_state_t state)
362 {
363         if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
364                 return enter_state(state);
365         return -EINVAL;
366 }
367
368 EXPORT_SYMBOL(pm_suspend);
369
370 #endif /* CONFIG_SUSPEND */
371
372 struct kobject *power_kobj;
373
374 /**
375  *      state - control system power state.
376  *
377  *      show() returns what states are supported, which is hard-coded to
378  *      'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
379  *      'disk' (Suspend-to-Disk).
380  *
381  *      store() accepts one of those strings, translates it into the 
382  *      proper enumerated value, and initiates a suspend transition.
383  */
384
385 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
386                           char *buf)
387 {
388         char *s = buf;
389 #ifdef CONFIG_SUSPEND
390         int i;
391
392         for (i = 0; i < PM_SUSPEND_MAX; i++) {
393                 if (pm_states[i] && valid_state(i))
394                         s += sprintf(s,"%s ", pm_states[i]);
395         }
396 #endif
397 #ifdef CONFIG_HIBERNATION
398         s += sprintf(s, "%s\n", "disk");
399 #else
400         if (s != buf)
401                 /* convert the last space to a newline */
402                 *(s-1) = '\n';
403 #endif
404         return (s - buf);
405 }
406
407 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
408                            const char *buf, size_t n)
409 {
410 #ifdef CONFIG_SUSPEND
411         suspend_state_t state = PM_SUSPEND_STANDBY;
412         const char * const *s;
413 #endif
414         char *p;
415         int len;
416         int error = -EINVAL;
417
418         p = memchr(buf, '\n', n);
419         len = p ? p - buf : n;
420
421         /* First, check if we are requested to hibernate */
422         if (len == 4 && !strncmp(buf, "disk", len)) {
423                 error = hibernate();
424   goto Exit;
425         }
426
427 #ifdef CONFIG_SUSPEND
428         for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
429                 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
430                         break;
431         }
432         if (state < PM_SUSPEND_MAX && *s)
433                 error = enter_state(state);
434 #endif
435
436  Exit:
437         return error ? error : n;
438 }
439
440 power_attr(state);
441
442 #ifdef CONFIG_PM_TRACE
443 int pm_trace_enabled;
444
445 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
446                              char *buf)
447 {
448         return sprintf(buf, "%d\n", pm_trace_enabled);
449 }
450
451 static ssize_t
452 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
453                const char *buf, size_t n)
454 {
455         int val;
456
457         if (sscanf(buf, "%d", &val) == 1) {
458                 pm_trace_enabled = !!val;
459                 return n;
460         }
461         return -EINVAL;
462 }
463
464 power_attr(pm_trace);
465 #endif /* CONFIG_PM_TRACE */
466
467 static struct attribute * g[] = {
468         &state_attr.attr,
469 #ifdef CONFIG_PM_TRACE
470         &pm_trace_attr.attr,
471 #endif
472 #ifdef CONFIG_PM_DEBUG
473         &pm_test_attr.attr,
474 #endif
475         NULL,
476 };
477
478 static struct attribute_group attr_group = {
479         .attrs = g,
480 };
481
482
483 static int __init pm_init(void)
484 {
485         power_kobj = kobject_create_and_add("power", NULL);
486         if (!power_kobj)
487                 return -ENOMEM;
488         return sysfs_create_group(power_kobj, &attr_group);
489 }
490
491 core_initcall(pm_init);