rework pm_ops pm_disk_mode, kill misuse
[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 static inline void pm_finish(suspend_state_t state)
52 {
53         if (pm_ops->finish)
54                 pm_ops->finish(state);
55 }
56
57 /**
58  *      suspend_prepare - Do prep work before entering low-power state.
59  *      @state:         State we're entering.
60  *
61  *      This is common code that is called for each state that we're 
62  *      entering. Allocate a console, stop all processes, then make sure
63  *      the platform can enter the requested state.
64  */
65
66 static int suspend_prepare(suspend_state_t state)
67 {
68         int error;
69         unsigned int free_pages;
70
71         if (!pm_ops || !pm_ops->enter)
72                 return -EPERM;
73
74         pm_prepare_console();
75
76         if (freeze_processes()) {
77                 error = -EAGAIN;
78                 goto Thaw;
79         }
80
81         if ((free_pages = global_page_state(NR_FREE_PAGES))
82                         < FREE_PAGE_NUMBER) {
83                 pr_debug("PM: free some memory\n");
84                 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
85                 if (nr_free_pages() < FREE_PAGE_NUMBER) {
86                         error = -ENOMEM;
87                         printk(KERN_ERR "PM: No enough memory\n");
88                         goto Thaw;
89                 }
90         }
91
92         if (pm_ops->prepare) {
93                 if ((error = pm_ops->prepare(state)))
94                         goto Thaw;
95         }
96
97         suspend_console();
98         error = device_suspend(PMSG_SUSPEND);
99         if (error) {
100                 printk(KERN_ERR "Some devices failed to suspend\n");
101                 goto Resume_devices;
102         }
103         error = disable_nonboot_cpus();
104         if (!error)
105                 return 0;
106
107         enable_nonboot_cpus();
108  Resume_devices:
109         pm_finish(state);
110         device_resume();
111         resume_console();
112  Thaw:
113         thaw_processes();
114         pm_restore_console();
115         return error;
116 }
117
118 /* default implementation */
119 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
120 {
121         local_irq_disable();
122 }
123
124 /* default implementation */
125 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
126 {
127         local_irq_enable();
128 }
129
130 int suspend_enter(suspend_state_t state)
131 {
132         int error = 0;
133
134         arch_suspend_disable_irqs();
135         BUG_ON(!irqs_disabled());
136
137         if ((error = device_power_down(PMSG_SUSPEND))) {
138                 printk(KERN_ERR "Some devices failed to power down\n");
139                 goto Done;
140         }
141         error = pm_ops->enter(state);
142         device_power_up();
143  Done:
144         arch_suspend_enable_irqs();
145         BUG_ON(irqs_disabled());
146         return error;
147 }
148
149
150 /**
151  *      suspend_finish - Do final work before exiting suspend sequence.
152  *      @state:         State we're coming out of.
153  *
154  *      Call platform code to clean up, restart processes, and free the 
155  *      console that we've allocated. This is not called for suspend-to-disk.
156  */
157
158 static void suspend_finish(suspend_state_t state)
159 {
160         enable_nonboot_cpus();
161         pm_finish(state);
162         device_resume();
163         resume_console();
164         thaw_processes();
165         pm_restore_console();
166 }
167
168
169
170
171 static const char * const pm_states[PM_SUSPEND_MAX] = {
172         [PM_SUSPEND_STANDBY]    = "standby",
173         [PM_SUSPEND_MEM]        = "mem",
174 #ifdef CONFIG_SOFTWARE_SUSPEND
175         [PM_SUSPEND_DISK]       = "disk",
176 #endif
177 };
178
179 static inline int valid_state(suspend_state_t state)
180 {
181         /* Suspend-to-disk does not really need low-level support.
182          * It can work with reboot if needed. */
183         if (state == PM_SUSPEND_DISK)
184                 return 1;
185
186         /* all other states need lowlevel support and need to be
187          * valid to the lowlevel implementation, no valid callback
188          * implies that all are valid. */
189         if (!pm_ops || (pm_ops->valid && !pm_ops->valid(state)))
190                 return 0;
191         return 1;
192 }
193
194
195 /**
196  *      enter_state - Do common work of entering low-power state.
197  *      @state:         pm_state structure for state we're entering.
198  *
199  *      Make sure we're the only ones trying to enter a sleep state. Fail
200  *      if someone has beat us to it, since we don't want anything weird to
201  *      happen when we wake up.
202  *      Then, do the setup for suspend, enter the state, and cleaup (after
203  *      we've woken up).
204  */
205
206 static int enter_state(suspend_state_t state)
207 {
208         int error;
209
210         if (!valid_state(state))
211                 return -ENODEV;
212         if (!mutex_trylock(&pm_mutex))
213                 return -EBUSY;
214
215         if (state == PM_SUSPEND_DISK) {
216                 error = pm_suspend_disk();
217                 goto Unlock;
218         }
219
220         pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
221         if ((error = suspend_prepare(state)))
222                 goto Unlock;
223
224         pr_debug("PM: Entering %s sleep\n", pm_states[state]);
225         error = suspend_enter(state);
226
227         pr_debug("PM: Finishing wakeup.\n");
228         suspend_finish(state);
229  Unlock:
230         mutex_unlock(&pm_mutex);
231         return error;
232 }
233
234 /*
235  * This is main interface to the outside world. It needs to be
236  * called from process context.
237  */
238 int software_suspend(void)
239 {
240         return enter_state(PM_SUSPEND_DISK);
241 }
242
243
244 /**
245  *      pm_suspend - Externally visible function for suspending system.
246  *      @state:         Enumarted 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 subsystem * subsys, 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         s += sprintf(s,"\n");
285         return (s - buf);
286 }
287
288 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
289 {
290         suspend_state_t state = PM_SUSPEND_STANDBY;
291         const char * const *s;
292         char *p;
293         int error;
294         int len;
295
296         p = memchr(buf, '\n', n);
297         len = p ? p - buf : n;
298
299         for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
300                 if (*s && !strncmp(buf, *s, len))
301                         break;
302         }
303         if (state < PM_SUSPEND_MAX && *s)
304                 error = enter_state(state);
305         else
306                 error = -EINVAL;
307         return error ? error : n;
308 }
309
310 power_attr(state);
311
312 #ifdef CONFIG_PM_TRACE
313 int pm_trace_enabled;
314
315 static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
316 {
317         return sprintf(buf, "%d\n", pm_trace_enabled);
318 }
319
320 static ssize_t
321 pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
322 {
323         int val;
324
325         if (sscanf(buf, "%d", &val) == 1) {
326                 pm_trace_enabled = !!val;
327                 return n;
328         }
329         return -EINVAL;
330 }
331
332 power_attr(pm_trace);
333
334 static struct attribute * g[] = {
335         &state_attr.attr,
336         &pm_trace_attr.attr,
337         NULL,
338 };
339 #else
340 static struct attribute * g[] = {
341         &state_attr.attr,
342         NULL,
343 };
344 #endif /* CONFIG_PM_TRACE */
345
346 static struct attribute_group attr_group = {
347         .attrs = g,
348 };
349
350
351 static int __init pm_init(void)
352 {
353         int error = subsystem_register(&power_subsys);
354         if (!error)
355                 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
356         return error;
357 }
358
359 core_initcall(pm_init);