Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial
[deliverable/linux.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
34 /**
35 * pm_set_ops - Set the global power method table.
36 * @ops: Pointer to ops structure.
37 */
38
39 void pm_set_ops(struct pm_ops * ops)
40 {
41 mutex_lock(&pm_mutex);
42 pm_ops = ops;
43 mutex_unlock(&pm_mutex);
44 }
45
46 /**
47 * pm_valid_only_mem - generic memory-only valid callback
48 *
49 * pm_ops drivers that implement mem suspend only and only need
50 * to check for that in their .valid callback can use this instead
51 * of rolling their own .valid callback.
52 */
53 int pm_valid_only_mem(suspend_state_t state)
54 {
55 return state == PM_SUSPEND_MEM;
56 }
57
58
59 static inline void pm_finish(suspend_state_t state)
60 {
61 if (pm_ops->finish)
62 pm_ops->finish(state);
63 }
64
65 /**
66 * suspend_prepare - Do prep work before entering low-power state.
67 * @state: State we're entering.
68 *
69 * This is common code that is called for each state that we're
70 * entering. Allocate a console, stop all processes, then make sure
71 * the platform can enter the requested state.
72 */
73
74 static int suspend_prepare(suspend_state_t state)
75 {
76 int error;
77 unsigned int free_pages;
78
79 if (!pm_ops || !pm_ops->enter)
80 return -EPERM;
81
82 pm_prepare_console();
83
84 if (freeze_processes()) {
85 error = -EAGAIN;
86 goto Thaw;
87 }
88
89 if ((free_pages = global_page_state(NR_FREE_PAGES))
90 < FREE_PAGE_NUMBER) {
91 pr_debug("PM: free some memory\n");
92 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
93 if (nr_free_pages() < FREE_PAGE_NUMBER) {
94 error = -ENOMEM;
95 printk(KERN_ERR "PM: No enough memory\n");
96 goto Thaw;
97 }
98 }
99
100 if (pm_ops->prepare) {
101 if ((error = pm_ops->prepare(state)))
102 goto Thaw;
103 }
104
105 suspend_console();
106 error = device_suspend(PMSG_SUSPEND);
107 if (error) {
108 printk(KERN_ERR "Some devices failed to suspend\n");
109 goto Resume_devices;
110 }
111 error = disable_nonboot_cpus();
112 if (!error)
113 return 0;
114
115 enable_nonboot_cpus();
116 Resume_devices:
117 pm_finish(state);
118 device_resume();
119 resume_console();
120 Thaw:
121 thaw_processes();
122 pm_restore_console();
123 return error;
124 }
125
126 /* default implementation */
127 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
128 {
129 local_irq_disable();
130 }
131
132 /* default implementation */
133 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
134 {
135 local_irq_enable();
136 }
137
138 int suspend_enter(suspend_state_t state)
139 {
140 int error = 0;
141
142 arch_suspend_disable_irqs();
143 BUG_ON(!irqs_disabled());
144
145 if ((error = device_power_down(PMSG_SUSPEND))) {
146 printk(KERN_ERR "Some devices failed to power down\n");
147 goto Done;
148 }
149 error = pm_ops->enter(state);
150 device_power_up();
151 Done:
152 arch_suspend_enable_irqs();
153 BUG_ON(irqs_disabled());
154 return error;
155 }
156
157
158 /**
159 * suspend_finish - Do final work before exiting suspend sequence.
160 * @state: State we're coming out of.
161 *
162 * Call platform code to clean up, restart processes, and free the
163 * console that we've allocated. This is not called for suspend-to-disk.
164 */
165
166 static void suspend_finish(suspend_state_t state)
167 {
168 enable_nonboot_cpus();
169 pm_finish(state);
170 device_resume();
171 resume_console();
172 thaw_processes();
173 pm_restore_console();
174 }
175
176
177
178
179 static const char * const pm_states[PM_SUSPEND_MAX] = {
180 [PM_SUSPEND_STANDBY] = "standby",
181 [PM_SUSPEND_MEM] = "mem",
182 };
183
184 static inline int valid_state(suspend_state_t state)
185 {
186 /* All states need lowlevel support and need to be valid
187 * to the lowlevel implementation, no valid callback
188 * implies that none 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 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
216 if ((error = suspend_prepare(state)))
217 goto Unlock;
218
219 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
220 error = suspend_enter(state);
221
222 pr_debug("PM: Finishing wakeup.\n");
223 suspend_finish(state);
224 Unlock:
225 mutex_unlock(&pm_mutex);
226 return error;
227 }
228
229
230 /**
231 * pm_suspend - Externally visible function for suspending system.
232 * @state: Enumerated value of state to enter.
233 *
234 * Determine whether or not value is within range, get state
235 * structure, and enter (above).
236 */
237
238 int pm_suspend(suspend_state_t state)
239 {
240 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
241 return enter_state(state);
242 return -EINVAL;
243 }
244
245 EXPORT_SYMBOL(pm_suspend);
246
247 decl_subsys(power,NULL,NULL);
248
249
250 /**
251 * state - control system power state.
252 *
253 * show() returns what states are supported, which is hard-coded to
254 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
255 * 'disk' (Suspend-to-Disk).
256 *
257 * store() accepts one of those strings, translates it into the
258 * proper enumerated value, and initiates a suspend transition.
259 */
260
261 static ssize_t state_show(struct kset *kset, char *buf)
262 {
263 int i;
264 char * s = buf;
265
266 for (i = 0; i < PM_SUSPEND_MAX; i++) {
267 if (pm_states[i] && valid_state(i))
268 s += sprintf(s,"%s ", pm_states[i]);
269 }
270 #ifdef CONFIG_SOFTWARE_SUSPEND
271 s += sprintf(s, "%s\n", "disk");
272 #else
273 if (s != buf)
274 /* convert the last space to a newline */
275 *(s-1) = '\n';
276 #endif
277 return (s - buf);
278 }
279
280 static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
281 {
282 suspend_state_t state = PM_SUSPEND_STANDBY;
283 const char * const *s;
284 char *p;
285 int error;
286 int len;
287
288 p = memchr(buf, '\n', n);
289 len = p ? p - buf : n;
290
291 /* First, check if we are requested to hibernate */
292 if (!strncmp(buf, "disk", len)) {
293 error = hibernate();
294 return error ? error : n;
295 }
296
297 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
298 if (*s && !strncmp(buf, *s, len))
299 break;
300 }
301 if (state < PM_SUSPEND_MAX && *s)
302 error = enter_state(state);
303 else
304 error = -EINVAL;
305 return error ? error : n;
306 }
307
308 power_attr(state);
309
310 #ifdef CONFIG_PM_TRACE
311 int pm_trace_enabled;
312
313 static ssize_t pm_trace_show(struct kset *kset, char *buf)
314 {
315 return sprintf(buf, "%d\n", pm_trace_enabled);
316 }
317
318 static ssize_t
319 pm_trace_store(struct kset *kset, const char *buf, size_t n)
320 {
321 int val;
322
323 if (sscanf(buf, "%d", &val) == 1) {
324 pm_trace_enabled = !!val;
325 return n;
326 }
327 return -EINVAL;
328 }
329
330 power_attr(pm_trace);
331
332 static struct attribute * g[] = {
333 &state_attr.attr,
334 &pm_trace_attr.attr,
335 NULL,
336 };
337 #else
338 static struct attribute * g[] = {
339 &state_attr.attr,
340 NULL,
341 };
342 #endif /* CONFIG_PM_TRACE */
343
344 static struct attribute_group attr_group = {
345 .attrs = g,
346 };
347
348
349 static int __init pm_init(void)
350 {
351 int error = subsystem_register(&power_subsys);
352 if (!error)
353 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
354 return error;
355 }
356
357 core_initcall(pm_init);
This page took 0.040439 seconds and 6 git commands to generate.