pm_trace displays the wrong time from the RTC
[deliverable/linux.git] / kernel / power / main.c
CommitLineData
1da177e4
LT
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
4cf30348 11#include <linux/module.h>
1da177e4
LT
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>
6cc07191 18#include <linux/console.h>
e3920fb4 19#include <linux/cpu.h>
c5c6ba4e 20#include <linux/resume-trace.h>
7dfb7103 21#include <linux/freezer.h>
96177299 22#include <linux/vmstat.h>
232b1432 23#include <linux/syscalls.h>
1da177e4
LT
24
25#include "power.h"
26
b10d9117
RW
27BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28
a6d70980 29DEFINE_MUTEX(pm_mutex);
1da177e4 30
296699de
RW
31#ifdef CONFIG_SUSPEND
32
33/* This is just an arbitrary number */
34#define FREE_PAGE_NUMBER (100)
35
f242d919 36static struct platform_suspend_ops *suspend_ops;
1da177e4
LT
37
38/**
26398a70 39 * suspend_set_ops - Set the global suspend method table.
1da177e4
LT
40 * @ops: Pointer to ops structure.
41 */
42
26398a70 43void suspend_set_ops(struct platform_suspend_ops *ops)
1da177e4 44{
a6d70980 45 mutex_lock(&pm_mutex);
26398a70 46 suspend_ops = ops;
a6d70980 47 mutex_unlock(&pm_mutex);
1da177e4
LT
48}
49
e8c9c502 50/**
26398a70 51 * suspend_valid_only_mem - generic memory-only valid callback
e8c9c502 52 *
26398a70 53 * Platform drivers that implement mem suspend only and only need
e8c9c502
JB
54 * to check for that in their .valid callback can use this instead
55 * of rolling their own .valid callback.
56 */
26398a70 57int suspend_valid_only_mem(suspend_state_t state)
e8c9c502
JB
58{
59 return state == PM_SUSPEND_MEM;
60}
61
1da177e4
LT
62/**
63 * suspend_prepare - Do prep work before entering low-power state.
1da177e4 64 *
6c961dfb
RW
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.
1da177e4 67 */
6c961dfb 68static int suspend_prepare(void)
1da177e4 69{
e3920fb4 70 int error;
5ae947ec 71 unsigned int free_pages;
1da177e4 72
26398a70 73 if (!suspend_ops || !suspend_ops->enter)
1da177e4
LT
74 return -EPERM;
75
b10d9117
RW
76 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
77 if (error)
78 goto Finish;
79
1da177e4
LT
80 pm_prepare_console();
81
82 if (freeze_processes()) {
83 error = -EAGAIN;
84 goto Thaw;
85 }
86
6c961dfb
RW
87 free_pages = global_page_state(NR_FREE_PAGES);
88 if (free_pages < FREE_PAGE_NUMBER) {
5ae947ec
DSL
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");
5ae947ec
DSL
94 }
95 }
e3c7db62
RW
96 if (!error)
97 return 0;
98
1da177e4
LT
99 Thaw:
100 thaw_processes();
101 pm_restore_console();
b10d9117
RW
102 Finish:
103 pm_notifier_call_chain(PM_POST_SUSPEND);
1da177e4
LT
104 return error;
105}
106
a53c46dc
JB
107/* default implementation */
108void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
109{
110 local_irq_disable();
111}
112
113/* default implementation */
114void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
115{
116 local_irq_enable();
117}
1da177e4 118
6c961dfb
RW
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 */
a065c86e 125static int suspend_enter(suspend_state_t state)
1da177e4
LT
126{
127 int error = 0;
1da177e4 128
a53c46dc
JB
129 arch_suspend_disable_irqs();
130 BUG_ON(!irqs_disabled());
1da177e4
LT
131
132 if ((error = device_power_down(PMSG_SUSPEND))) {
133 printk(KERN_ERR "Some devices failed to power down\n");
134 goto Done;
135 }
26398a70 136 error = suspend_ops->enter(state);
1da177e4
LT
137 device_power_up();
138 Done:
a53c46dc
JB
139 arch_suspend_enable_irqs();
140 BUG_ON(irqs_disabled());
1da177e4
LT
141 return error;
142}
143
6c961dfb
RW
144/**
145 * suspend_devices_and_enter - suspend devices and enter the desired system sleep
146 * state.
147 * @state: state to enter
148 */
149int suspend_devices_and_enter(suspend_state_t state)
150{
151 int error;
152
26398a70 153 if (!suspend_ops)
6c961dfb
RW
154 return -ENOSYS;
155
26398a70
RW
156 if (suspend_ops->set_target) {
157 error = suspend_ops->set_target(state);
6c961dfb
RW
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 }
26398a70 167 if (suspend_ops->prepare) {
e6c5eb95 168 error = suspend_ops->prepare();
6c961dfb
RW
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();
e6c5eb95
RW
177 if (suspend_ops->finish)
178 suspend_ops->finish();
6c961dfb
RW
179 Resume_devices:
180 device_resume();
181 Resume_console:
182 resume_console();
183 return error;
184}
1da177e4
LT
185
186/**
187 * suspend_finish - Do final work before exiting suspend sequence.
1da177e4
LT
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 */
6c961dfb 192static void suspend_finish(void)
1da177e4 193{
1da177e4
LT
194 thaw_processes();
195 pm_restore_console();
b10d9117 196 pm_notifier_call_chain(PM_POST_SUSPEND);
1da177e4
LT
197}
198
199
200
201
3b364b8d 202static const char * const pm_states[PM_SUSPEND_MAX] = {
1da177e4
LT
203 [PM_SUSPEND_STANDBY] = "standby",
204 [PM_SUSPEND_MEM] = "mem",
1da177e4
LT
205};
206
123d3c13
PM
207static inline int valid_state(suspend_state_t state)
208{
a3d25c27
RW
209 /* All states need lowlevel support and need to be valid
210 * to the lowlevel implementation, no valid callback
9684e51c 211 * implies that none are valid. */
26398a70 212 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
123d3c13
PM
213 return 0;
214 return 1;
215}
216
1da177e4
LT
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 */
1da177e4
LT
228static int enter_state(suspend_state_t state)
229{
230 int error;
231
123d3c13 232 if (!valid_state(state))
eb9289eb 233 return -ENODEV;
232b1432 234
a6d70980 235 if (!mutex_trylock(&pm_mutex))
1da177e4
LT
236 return -EBUSY;
237
232b1432
RW
238 printk("Syncing filesystems ... ");
239 sys_sync();
240 printk("done.\n");
241
82428b62 242 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
6c961dfb 243 if ((error = suspend_prepare()))
1da177e4
LT
244 goto Unlock;
245
82428b62 246 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
6c961dfb 247 error = suspend_devices_and_enter(state);
1da177e4 248
82428b62 249 pr_debug("PM: Finishing wakeup.\n");
6c961dfb 250 suspend_finish();
1da177e4 251 Unlock:
a6d70980 252 mutex_unlock(&pm_mutex);
1da177e4
LT
253 return error;
254}
255
1da177e4
LT
256
257/**
258 * pm_suspend - Externally visible function for suspending system.
a3d25c27 259 * @state: Enumerated value of state to enter.
1da177e4
LT
260 *
261 * Determine whether or not value is within range, get state
262 * structure, and enter (above).
263 */
264
265int pm_suspend(suspend_state_t state)
266{
e2a5b420 267 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
1da177e4
LT
268 return enter_state(state);
269 return -EINVAL;
270}
271
4cf30348 272EXPORT_SYMBOL(pm_suspend);
1da177e4 273
296699de
RW
274#endif /* CONFIG_SUSPEND */
275
1da177e4
LT
276decl_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
823bccfc 290static ssize_t state_show(struct kset *kset, char *buf)
1da177e4 291{
296699de
RW
292 char *s = buf;
293#ifdef CONFIG_SUSPEND
1da177e4 294 int i;
1da177e4
LT
295
296 for (i = 0; i < PM_SUSPEND_MAX; i++) {
123d3c13
PM
297 if (pm_states[i] && valid_state(i))
298 s += sprintf(s,"%s ", pm_states[i]);
1da177e4 299 }
296699de 300#endif
b0cb1a19 301#ifdef CONFIG_HIBERNATION
a3d25c27
RW
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
1da177e4
LT
308 return (s - buf);
309}
310
823bccfc 311static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
1da177e4 312{
296699de 313#ifdef CONFIG_SUSPEND
1da177e4 314 suspend_state_t state = PM_SUSPEND_STANDBY;
3b364b8d 315 const char * const *s;
296699de 316#endif
1da177e4 317 char *p;
1da177e4 318 int len;
296699de 319 int error = -EINVAL;
1da177e4
LT
320
321 p = memchr(buf, '\n', n);
322 len = p ? p - buf : n;
323
a3d25c27 324 /* First, check if we are requested to hibernate */
8d98a690 325 if (len == 4 && !strncmp(buf, "disk", len)) {
a3d25c27 326 error = hibernate();
296699de 327 goto Exit;
a3d25c27
RW
328 }
329
296699de 330#ifdef CONFIG_SUSPEND
1da177e4 331 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
8d98a690 332 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
1da177e4
LT
333 break;
334 }
47bb7899 335 if (state < PM_SUSPEND_MAX && *s)
1da177e4 336 error = enter_state(state);
296699de
RW
337#endif
338
339 Exit:
1da177e4
LT
340 return error ? error : n;
341}
342
343power_attr(state);
344
c5c6ba4e
RW
345#ifdef CONFIG_PM_TRACE
346int pm_trace_enabled;
347
823bccfc 348static ssize_t pm_trace_show(struct kset *kset, char *buf)
c5c6ba4e
RW
349{
350 return sprintf(buf, "%d\n", pm_trace_enabled);
351}
352
353static ssize_t
823bccfc 354pm_trace_store(struct kset *kset, const char *buf, size_t n)
c5c6ba4e
RW
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
365power_attr(pm_trace);
366
367static struct attribute * g[] = {
368 &state_attr.attr,
369 &pm_trace_attr.attr,
370 NULL,
371};
372#else
1da177e4
LT
373static struct attribute * g[] = {
374 &state_attr.attr,
375 NULL,
376};
c5c6ba4e 377#endif /* CONFIG_PM_TRACE */
1da177e4
LT
378
379static struct attribute_group attr_group = {
380 .attrs = g,
381};
382
383
384static int __init pm_init(void)
385{
386 int error = subsystem_register(&power_subsys);
387 if (!error)
823bccfc 388 error = sysfs_create_group(&power_subsys.kobj,&attr_group);
1da177e4
LT
389 return error;
390}
391
392core_initcall(pm_init);
This page took 0.348181 seconds and 5 git commands to generate.