Suspend infrastructure cleanup and extension
[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/suspend.h>
12 #include <linux/kobject.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/pm.h>
18 #include <linux/console.h>
19
20 #include "power.h"
21
22 /*This is just an arbitrary number */
23 #define FREE_PAGE_NUMBER (100)
24
25 DECLARE_MUTEX(pm_sem);
26
27 struct pm_ops *pm_ops;
28 suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
29
30 /**
31  *      pm_set_ops - Set the global power method table. 
32  *      @ops:   Pointer to ops structure.
33  */
34
35 void pm_set_ops(struct pm_ops * ops)
36 {
37         down(&pm_sem);
38         pm_ops = ops;
39         up(&pm_sem);
40 }
41
42
43 /**
44  *      suspend_prepare - Do prep work before entering low-power state.
45  *      @state:         State we're entering.
46  *
47  *      This is common code that is called for each state that we're 
48  *      entering. Allocate a console, stop all processes, then make sure
49  *      the platform can enter the requested state.
50  */
51
52 static int suspend_prepare(suspend_state_t state)
53 {
54         int error = 0;
55         unsigned int free_pages;
56
57         if (!pm_ops || !pm_ops->enter)
58                 return -EPERM;
59
60         error = device_prepare_suspend(PMSG_SUSPEND);
61         if (error)
62                 return error;
63
64         pm_prepare_console();
65
66         disable_nonboot_cpus();
67
68         if (num_online_cpus() != 1) {
69                 error = -EPERM;
70                 goto Enable_cpu;
71         }
72
73         if (freeze_processes()) {
74                 error = -EAGAIN;
75                 goto Thaw;
76         }
77
78         if ((free_pages = nr_free_pages()) < FREE_PAGE_NUMBER) {
79                 pr_debug("PM: free some memory\n");
80                 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
81                 if (nr_free_pages() < FREE_PAGE_NUMBER) {
82                         error = -ENOMEM;
83                         printk(KERN_ERR "PM: No enough memory\n");
84                         goto Thaw;
85                 }
86         }
87
88         if (pm_ops->prepare) {
89                 if ((error = pm_ops->prepare(state)))
90                         goto Thaw;
91         }
92
93         suspend_console();
94         if ((error = device_suspend(PMSG_SUSPEND))) {
95                 printk(KERN_ERR "Some devices failed to suspend\n");
96                 goto Finish;
97         }
98         return 0;
99  Finish:
100         if (pm_ops->finish)
101                 pm_ops->finish(state);
102  Thaw:
103         thaw_processes();
104  Enable_cpu:
105         enable_nonboot_cpus();
106         pm_restore_console();
107         return error;
108 }
109
110
111 int suspend_enter(suspend_state_t state)
112 {
113         int error = 0;
114         unsigned long flags;
115
116         local_irq_save(flags);
117
118         if ((error = device_power_down(PMSG_SUSPEND))) {
119                 printk(KERN_ERR "Some devices failed to power down\n");
120                 goto Done;
121         }
122         error = pm_ops->enter(state);
123         device_power_up();
124  Done:
125         local_irq_restore(flags);
126         return error;
127 }
128
129
130 /**
131  *      suspend_finish - Do final work before exiting suspend sequence.
132  *      @state:         State we're coming out of.
133  *
134  *      Call platform code to clean up, restart processes, and free the 
135  *      console that we've allocated. This is not called for suspend-to-disk.
136  */
137
138 static void suspend_finish(suspend_state_t state)
139 {
140         device_resume();
141         resume_console();
142         thaw_processes();
143         enable_nonboot_cpus();
144         if (pm_ops && pm_ops->finish)
145                 pm_ops->finish(state);
146         pm_restore_console();
147 }
148
149
150
151
152 static const char * const pm_states[PM_SUSPEND_MAX] = {
153         [PM_SUSPEND_STANDBY]    = "standby",
154         [PM_SUSPEND_MEM]        = "mem",
155 #ifdef CONFIG_SOFTWARE_SUSPEND
156         [PM_SUSPEND_DISK]       = "disk",
157 #endif
158 };
159
160 static inline int valid_state(suspend_state_t state)
161 {
162         /* Suspend-to-disk does not really need low-level support.
163          * It can work with reboot if needed. */
164         if (state == PM_SUSPEND_DISK)
165                 return 1;
166
167         if (pm_ops && pm_ops->valid && !pm_ops->valid(state))
168                 return 0;
169         return 1;
170 }
171
172
173 /**
174  *      enter_state - Do common work of entering low-power state.
175  *      @state:         pm_state structure for state we're entering.
176  *
177  *      Make sure we're the only ones trying to enter a sleep state. Fail
178  *      if someone has beat us to it, since we don't want anything weird to
179  *      happen when we wake up.
180  *      Then, do the setup for suspend, enter the state, and cleaup (after
181  *      we've woken up).
182  */
183
184 static int enter_state(suspend_state_t state)
185 {
186         int error;
187
188         if (!valid_state(state))
189                 return -ENODEV;
190         if (down_trylock(&pm_sem))
191                 return -EBUSY;
192
193         if (state == PM_SUSPEND_DISK) {
194                 error = pm_suspend_disk();
195                 goto Unlock;
196         }
197
198         pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
199         if ((error = suspend_prepare(state)))
200                 goto Unlock;
201
202         pr_debug("PM: Entering %s sleep\n", pm_states[state]);
203         error = suspend_enter(state);
204
205         pr_debug("PM: Finishing wakeup.\n");
206         suspend_finish(state);
207  Unlock:
208         up(&pm_sem);
209         return error;
210 }
211
212 /*
213  * This is main interface to the outside world. It needs to be
214  * called from process context.
215  */
216 int software_suspend(void)
217 {
218         return enter_state(PM_SUSPEND_DISK);
219 }
220
221
222 /**
223  *      pm_suspend - Externally visible function for suspending system.
224  *      @state:         Enumarted value of state to enter.
225  *
226  *      Determine whether or not value is within range, get state 
227  *      structure, and enter (above).
228  */
229
230 int pm_suspend(suspend_state_t state)
231 {
232         if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
233                 return enter_state(state);
234         return -EINVAL;
235 }
236
237
238
239 decl_subsys(power,NULL,NULL);
240
241
242 /**
243  *      state - control system power state.
244  *
245  *      show() returns what states are supported, which is hard-coded to
246  *      'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
247  *      'disk' (Suspend-to-Disk).
248  *
249  *      store() accepts one of those strings, translates it into the 
250  *      proper enumerated value, and initiates a suspend transition.
251  */
252
253 static ssize_t state_show(struct subsystem * subsys, char * buf)
254 {
255         int i;
256         char * s = buf;
257
258         for (i = 0; i < PM_SUSPEND_MAX; i++) {
259                 if (pm_states[i] && valid_state(i))
260                         s += sprintf(s,"%s ", pm_states[i]);
261         }
262         s += sprintf(s,"\n");
263         return (s - buf);
264 }
265
266 static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
267 {
268         suspend_state_t state = PM_SUSPEND_STANDBY;
269         const char * const *s;
270         char *p;
271         int error;
272         int len;
273
274         p = memchr(buf, '\n', n);
275         len = p ? p - buf : n;
276
277         for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
278                 if (*s && !strncmp(buf, *s, len))
279                         break;
280         }
281         if (state < PM_SUSPEND_MAX && *s)
282                 error = enter_state(state);
283         else
284                 error = -EINVAL;
285         return error ? error : n;
286 }
287
288 power_attr(state);
289
290 static struct attribute * g[] = {
291         &state_attr.attr,
292         NULL,
293 };
294
295 static struct attribute_group attr_group = {
296         .attrs = g,
297 };
298
299
300 static int __init pm_init(void)
301 {
302         int error = subsystem_register(&power_subsys);
303         if (!error)
304                 error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
305         return error;
306 }
307
308 core_initcall(pm_init);