Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[sfrench/cifs-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 #ifdef CONFIG_SUSPEND
32
33 /* This is just an arbitrary number */
34 #define FREE_PAGE_NUMBER (100)
35
36 static struct platform_suspend_ops *suspend_ops;
37
38 /**
39  *      suspend_set_ops - Set the global suspend method table.
40  *      @ops:   Pointer to ops structure.
41  */
42
43 void suspend_set_ops(struct platform_suspend_ops *ops)
44 {
45         mutex_lock(&pm_mutex);
46         suspend_ops = ops;
47         mutex_unlock(&pm_mutex);
48 }
49
50 /**
51  * suspend_valid_only_mem - generic memory-only valid callback
52  *
53  * Platform drivers that implement mem suspend only and only need
54  * to check for that in their .valid callback can use this instead
55  * of rolling their own .valid callback.
56  */
57 int suspend_valid_only_mem(suspend_state_t state)
58 {
59         return state == PM_SUSPEND_MEM;
60 }
61
62 /**
63  *      suspend_prepare - Do prep work before entering low-power state.
64  *
65  *      This is common code that is called for each state that we're entering.
66  *      Run suspend notifiers, allocate a console and stop all processes.
67  */
68 static int suspend_prepare(void)
69 {
70         int error;
71         unsigned int free_pages;
72
73         if (!suspend_ops || !suspend_ops->enter)
74                 return -EPERM;
75
76         error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
77         if (error)
78                 goto Finish;
79
80         pm_prepare_console();
81
82         if (freeze_processes()) {
83                 error = -EAGAIN;
84                 goto Thaw;
85         }
86
87         free_pages = global_page_state(NR_FREE_PAGES);
88         if (free_pages < FREE_PAGE_NUMBER) {
89                 pr_debug("PM: free some memory\n");
90                 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
91                 if (nr_free_pages() < FREE_PAGE_NUMBER) {
92                         error = -ENOMEM;
93                         printk(KERN_ERR "PM: No enough memory\n");
94                 }
95         }
96         if (!error)
97                 return 0;
98
99  Thaw:
100         thaw_processes();
101         pm_restore_console();
102  Finish:
103         pm_notifier_call_chain(PM_POST_SUSPEND);
104         return error;
105 }
106
107 /* default implementation */
108 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
109 {
110         local_irq_disable();
111 }
112
113 /* default implementation */
114 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
115 {
116         local_irq_enable();
117 }
118
119 /**
120  *      suspend_enter - enter the desired system sleep state.
121  *      @state:         state to enter
122  *
123  *      This function should be called after devices have been suspended.
124  */
125 static int suspend_enter(suspend_state_t state)
126 {
127         int error = 0;
128
129         arch_suspend_disable_irqs();
130         BUG_ON(!irqs_disabled());
131
132         if ((error = device_power_down(PMSG_SUSPEND))) {
133                 printk(KERN_ERR "Some devices failed to power down\n");
134                 goto Done;
135         }
136         error = suspend_ops->enter(state);
137         device_power_up();
138  Done:
139         arch_suspend_enable_irqs();
140         BUG_ON(irqs_disabled());
141         return error;
142 }
143
144 /**
145  *      suspend_devices_and_enter - suspend devices and enter the desired system sleep
146  *                        state.
147  *      @state:           state to enter
148  */
149 int suspend_devices_and_enter(suspend_state_t state)
150 {
151         int error;
152
153         if (!suspend_ops)
154                 return -ENOSYS;
155
156         if (suspend_ops->set_target) {
157                 error = suspend_ops->set_target(state);
158                 if (error)
159                         return error;
160         }
161         suspend_console();
162         error = device_suspend(PMSG_SUSPEND);
163         if (error) {
164                 printk(KERN_ERR "Some devices failed to suspend\n");
165                 goto Resume_console;
166         }
167         if (suspend_ops->prepare) {
168                 error = suspend_ops->prepare();
169                 if (error)
170                         goto Resume_devices;
171         }
172         error = disable_nonboot_cpus();
173         if (!error)
174                 suspend_enter(state);
175
176         enable_nonboot_cpus();
177         if (suspend_ops->finish)
178                 suspend_ops->finish();
179  Resume_devices:
180         device_resume();
181  Resume_console:
182         resume_console();
183         return error;
184 }
185
186 /**
187  *      suspend_finish - Do final work before exiting suspend sequence.
188  *
189  *      Call platform code to clean up, restart processes, and free the 
190  *      console that we've allocated. This is not called for suspend-to-disk.
191  */
192 static void suspend_finish(void)
193 {
194         thaw_processes();
195         pm_restore_console();
196         pm_notifier_call_chain(PM_POST_SUSPEND);
197 }
198
199
200
201
202 static const char * const pm_states[PM_SUSPEND_MAX] = {
203         [PM_SUSPEND_STANDBY]    = "standby",
204         [PM_SUSPEND_MEM]        = "mem",
205 };
206
207 static inline int valid_state(suspend_state_t state)
208 {
209         /* All states need lowlevel support and need to be valid
210          * to the lowlevel implementation, no valid callback
211          * implies that none are valid. */
212         if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
213                 return 0;
214         return 1;
215 }
216
217
218 /**
219  *      enter_state - Do common work of entering low-power state.
220  *      @state:         pm_state structure for state we're entering.
221  *
222  *      Make sure we're the only ones trying to enter a sleep state. Fail
223  *      if someone has beat us to it, since we don't want anything weird to
224  *      happen when we wake up.
225  *      Then, do the setup for suspend, enter the state, and cleaup (after
226  *      we've woken up).
227  */
228 static int enter_state(suspend_state_t state)
229 {
230         int error;
231
232         if (!valid_state(state))
233                 return -ENODEV;
234
235         if (!mutex_trylock(&pm_mutex))
236                 return -EBUSY;
237
238         printk("Syncing filesystems ... ");
239         sys_sync();
240         printk("done.\n");
241
242         pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
243         if ((error = suspend_prepare()))
244                 goto Unlock;
245
246         pr_debug("PM: Entering %s sleep\n", pm_states[state]);
247         error = suspend_devices_and_enter(state);
248
249         pr_debug("PM: Finishing wakeup.\n");
250         suspend_finish();
251  Unlock:
252         mutex_unlock(&pm_mutex);
253         return error;
254 }
255
256
257 /**
258  *      pm_suspend - Externally visible function for suspending system.
259  *      @state:         Enumerated value of state to enter.
260  *
261  *      Determine whether or not value is within range, get state 
262  *      structure, and enter (above).
263  */
264
265 int pm_suspend(suspend_state_t state)
266 {
267         if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
268                 return enter_state(state);
269         return -EINVAL;
270 }
271
272 EXPORT_SYMBOL(pm_suspend);
273
274 #endif /* CONFIG_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         char *s = buf;
293 #ifdef CONFIG_SUSPEND
294         int i;
295
296         for (i = 0; i < PM_SUSPEND_MAX; i++) {
297                 if (pm_states[i] && valid_state(i))
298                         s += sprintf(s,"%s ", pm_states[i]);
299         }
300 #endif
301 #ifdef CONFIG_HIBERNATION
302         s += sprintf(s, "%s\n", "disk");
303 #else
304         if (s != buf)
305                 /* convert the last space to a newline */
306                 *(s-1) = '\n';
307 #endif
308         return (s - buf);
309 }
310
311 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
312 {
313 #ifdef CONFIG_SUSPEND
314         suspend_state_t state = PM_SUSPEND_STANDBY;
315         const char * const *s;
316 #endif
317         char *p;
318         int len;
319         int error = -EINVAL;
320
321         p = memchr(buf, '\n', n);
322         len = p ? p - buf : n;
323
324         /* First, check if we are requested to hibernate */
325         if (len == 4 && !strncmp(buf, "disk", len)) {
326                 error = hibernate();
327   goto Exit;
328         }
329
330 #ifdef CONFIG_SUSPEND
331         for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
332                 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
333                         break;
334         }
335         if (state < PM_SUSPEND_MAX && *s)
336                 error = enter_state(state);
337 #endif
338
339  Exit:
340         return error ? error : n;
341 }
342
343 power_attr(state);
344
345 #ifdef CONFIG_PM_TRACE
346 int pm_trace_enabled;
347
348 static ssize_t pm_trace_show(struct kset *kset, char *buf)
349 {
350         return sprintf(buf, "%d\n", pm_trace_enabled);
351 }
352
353 static ssize_t
354 pm_trace_store(struct kset *kset, const char *buf, size_t n)
355 {
356         int val;
357
358         if (sscanf(buf, "%d", &val) == 1) {
359                 pm_trace_enabled = !!val;
360                 return n;
361         }
362         return -EINVAL;
363 }
364
365 power_attr(pm_trace);
366
367 static struct attribute * g[] = {
368         &state_attr.attr,
369         &pm_trace_attr.attr,
370         NULL,
371 };
372 #else
373 static struct attribute * g[] = {
374         &state_attr.attr,
375         NULL,
376 };
377 #endif /* CONFIG_PM_TRACE */
378
379 static struct attribute_group attr_group = {
380         .attrs = g,
381 };
382
383
384 static int __init pm_init(void)
385 {
386         int error = subsystem_register(&power_subsys);
387         if (!error)
388                 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
389         return error;
390 }
391
392 core_initcall(pm_init);