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