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