blob: 19122cf6d827d13d03e8a7520d0120d5adf92bbc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Ralf Baechle4cf30342006-12-06 20:36:06 -080011#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/suspend.h>
13#include <linux/kobject.h>
14#include <linux/string.h>
15#include <linux/delay.h>
16#include <linux/errno.h>
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -070017#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
Andrew Morton6cc07192006-06-22 14:47:18 -070019#include <linux/console.h>
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070020#include <linux/cpu.h>
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -070021#include <linux/resume-trace.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080022#include <linux/freezer.h>
Christoph Lameter96177292007-02-10 01:43:03 -080023#include <linux/vmstat.h>
Rafael J. Wysocki232b1432007-10-18 03:04:44 -070024#include <linux/syscalls.h>
Steven Rostedtf42ac382008-08-27 09:14:40 -040025#include <linux/ftrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "power.h"
28
Stephen Hemmingera6d70982006-12-06 20:34:35 -080029DEFINE_MUTEX(pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Len Brown9f9adec2007-12-13 17:38:03 -050031unsigned int pm_flags;
32EXPORT_SYMBOL(pm_flags);
33
Alan Stern82525752007-11-19 23:49:18 +010034#ifdef CONFIG_PM_SLEEP
35
36/* Routines for PM-transition notifications */
37
38static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
39
40int register_pm_notifier(struct notifier_block *nb)
41{
42 return blocking_notifier_chain_register(&pm_chain_head, nb);
43}
44EXPORT_SYMBOL_GPL(register_pm_notifier);
45
46int unregister_pm_notifier(struct notifier_block *nb)
47{
48 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
49}
50EXPORT_SYMBOL_GPL(unregister_pm_notifier);
51
52int pm_notifier_call_chain(unsigned long val)
53{
54 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
55 == NOTIFY_BAD) ? -EINVAL : 0;
56}
57
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +010058#ifdef CONFIG_PM_DEBUG
59int pm_test_level = TEST_NONE;
60
61static int suspend_test(int level)
62{
63 if (pm_test_level == level) {
64 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
65 mdelay(5000);
66 return 1;
67 }
68 return 0;
69}
70
71static const char * const pm_tests[__TEST_AFTER_LAST] = {
72 [TEST_NONE] = "none",
73 [TEST_CORE] = "core",
74 [TEST_CPUS] = "processors",
75 [TEST_PLATFORM] = "platform",
76 [TEST_DEVICES] = "devices",
77 [TEST_FREEZER] = "freezer",
78};
79
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +010080static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
81 char *buf)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +010082{
83 char *s = buf;
84 int level;
85
86 for (level = TEST_FIRST; level <= TEST_MAX; level++)
87 if (pm_tests[level]) {
88 if (level == pm_test_level)
89 s += sprintf(s, "[%s] ", pm_tests[level]);
90 else
91 s += sprintf(s, "%s ", pm_tests[level]);
92 }
93
94 if (s != buf)
95 /* convert the last space to a newline */
96 *(s-1) = '\n';
97
98 return (s - buf);
99}
100
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100101static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
102 const char *buf, size_t n)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100103{
104 const char * const *s;
105 int level;
106 char *p;
107 int len;
108 int error = -EINVAL;
109
110 p = memchr(buf, '\n', n);
111 len = p ? p - buf : n;
112
113 mutex_lock(&pm_mutex);
114
115 level = TEST_FIRST;
116 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
117 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
118 pm_test_level = level;
119 error = 0;
120 break;
121 }
122
123 mutex_unlock(&pm_mutex);
124
125 return error ? error : n;
126}
127
128power_attr(pm_test);
129#else /* !CONFIG_PM_DEBUG */
130static inline int suspend_test(int level) { return 0; }
131#endif /* !CONFIG_PM_DEBUG */
132
Rafael J. Wysocki7671b8a2007-12-14 01:07:13 +0100133#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100134
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200135#ifdef CONFIG_SUSPEND
136
David Brownell77437fd2008-07-23 21:28:33 -0700137#ifdef CONFIG_PM_TEST_SUSPEND
138
139/*
140 * We test the system suspend code by setting an RTC wakealarm a short
141 * time in the future, then suspending. Suspending the devices won't
142 * normally take long ... some systems only need a few milliseconds.
143 *
144 * The time it takes is system-specific though, so when we test this
145 * during system bootup we allow a LOT of time.
146 */
147#define TEST_SUSPEND_SECONDS 5
148
149static unsigned long suspend_test_start_time;
150
151static void suspend_test_start(void)
152{
153 /* FIXME Use better timebase than "jiffies", ideally a clocksource.
154 * What we want is a hardware counter that will work correctly even
155 * during the irqs-are-off stages of the suspend/resume cycle...
156 */
157 suspend_test_start_time = jiffies;
158}
159
160static void suspend_test_finish(const char *label)
161{
162 long nj = jiffies - suspend_test_start_time;
163 unsigned msec;
164
165 msec = jiffies_to_msecs(abs(nj));
166 pr_info("PM: %s took %d.%03d seconds\n", label,
167 msec / 1000, msec % 1000);
168
169 /* Warning on suspend means the RTC alarm period needs to be
170 * larger -- the system was sooo slooowwww to suspend that the
171 * alarm (should have) fired before the system went to sleep!
172 *
173 * Warning on either suspend or resume also means the system
174 * has some performance issues. The stack dump of a WARN_ON
175 * is more likely to get the right attention than a printk...
176 */
177 WARN_ON(msec > (TEST_SUSPEND_SECONDS * 1000));
178}
179
180#else
181
182static void suspend_test_start(void)
183{
184}
185
186static void suspend_test_finish(const char *label)
187{
188}
189
190#endif
191
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200192/* This is just an arbitrary number */
193#define FREE_PAGE_NUMBER (100)
194
Rafael J. Wysockif242d912007-10-18 03:04:41 -0700195static struct platform_suspend_ops *suspend_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700198 * suspend_set_ops - Set the global suspend method table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * @ops: Pointer to ops structure.
200 */
201
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700202void suspend_set_ops(struct platform_suspend_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800204 mutex_lock(&pm_mutex);
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700205 suspend_ops = ops;
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800206 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Johannes Berge8c9c502007-04-30 15:09:54 -0700209/**
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700210 * suspend_valid_only_mem - generic memory-only valid callback
Johannes Berge8c9c502007-04-30 15:09:54 -0700211 *
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700212 * Platform drivers that implement mem suspend only and only need
Johannes Berge8c9c502007-04-30 15:09:54 -0700213 * to check for that in their .valid callback can use this instead
214 * of rolling their own .valid callback.
215 */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700216int suspend_valid_only_mem(suspend_state_t state)
Johannes Berge8c9c502007-04-30 15:09:54 -0700217{
218 return state == PM_SUSPEND_MEM;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/**
222 * suspend_prepare - Do prep work before entering low-power state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700224 * This is common code that is called for each state that we're entering.
225 * Run suspend notifiers, allocate a console and stop all processes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700227static int suspend_prepare(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700229 int error;
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500230 unsigned int free_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700232 if (!suspend_ops || !suspend_ops->enter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return -EPERM;
234
Johannes Bergaf258f52008-01-11 01:22:23 +0100235 pm_prepare_console();
236
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700237 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
238 if (error)
239 goto Finish;
240
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700241 error = usermodehelper_disable();
242 if (error)
243 goto Finish;
244
Johannes Bergb28f5082008-01-15 23:17:00 -0500245 if (suspend_freeze_processes()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 error = -EAGAIN;
247 goto Thaw;
248 }
249
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700250 free_pages = global_page_state(NR_FREE_PAGES);
251 if (free_pages < FREE_PAGE_NUMBER) {
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500252 pr_debug("PM: free some memory\n");
253 shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
254 if (nr_free_pages() < FREE_PAGE_NUMBER) {
255 error = -ENOMEM;
256 printk(KERN_ERR "PM: No enough memory\n");
David Shaohua Li5ae947e2005-03-18 16:27:13 -0500257 }
258 }
Rafael J. Wysockie3c7db622007-02-10 01:43:31 -0800259 if (!error)
260 return 0;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 Thaw:
Johannes Bergb28f5082008-01-15 23:17:00 -0500263 suspend_thaw_processes();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700264 usermodehelper_enable();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700265 Finish:
266 pm_notifier_call_chain(PM_POST_SUSPEND);
Johannes Bergaf258f52008-01-11 01:22:23 +0100267 pm_restore_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return error;
269}
270
Johannes Berga53c46d2007-04-26 11:43:58 +0200271/* default implementation */
272void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
273{
274 local_irq_disable();
275}
276
277/* default implementation */
278void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
279{
280 local_irq_enable();
281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700283/**
284 * suspend_enter - enter the desired system sleep state.
285 * @state: state to enter
286 *
287 * This function should be called after devices have been suspended.
288 */
Adrian Bunka065c862007-10-18 03:04:37 -0700289static int suspend_enter(suspend_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200293 device_pm_lock();
Johannes Berga53c46d2007-04-26 11:43:58 +0200294 arch_suspend_disable_irqs();
295 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if ((error = device_power_down(PMSG_SUSPEND))) {
Rafael J. Wysocki465d2b42007-12-08 02:08:38 +0100298 printk(KERN_ERR "PM: Some devices failed to power down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 goto Done;
300 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100301
302 if (!suspend_test(TEST_CORE))
303 error = suspend_ops->enter(state);
304
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200305 device_power_up(PMSG_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 Done:
Johannes Berga53c46d2007-04-26 11:43:58 +0200307 arch_suspend_enable_irqs();
308 BUG_ON(irqs_disabled());
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200309 device_pm_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return error;
311}
312
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700313/**
Rafael J. Wysocki9628c0e2007-12-08 02:06:00 +0100314 * suspend_devices_and_enter - suspend devices and enter the desired system
315 * sleep state.
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700316 * @state: state to enter
317 */
318int suspend_devices_and_enter(suspend_state_t state)
319{
Steven Rostedtf42ac382008-08-27 09:14:40 -0400320 int error, ftrace_save;
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700321
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700322 if (!suspend_ops)
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700323 return -ENOSYS;
324
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100325 if (suspend_ops->begin) {
326 error = suspend_ops->begin(state);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700327 if (error)
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100328 goto Close;
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700329 }
330 suspend_console();
Steven Rostedtf42ac382008-08-27 09:14:40 -0400331 ftrace_save = __ftrace_enabled_save();
David Brownell77437fd2008-07-23 21:28:33 -0700332 suspend_test_start();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700333 error = device_suspend(PMSG_SUSPEND);
334 if (error) {
Rafael J. Wysocki465d2b42007-12-08 02:08:38 +0100335 printk(KERN_ERR "PM: Some devices failed to suspend\n");
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200336 goto Recover_platform;
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700337 }
David Brownell77437fd2008-07-23 21:28:33 -0700338 suspend_test_finish("suspend devices");
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100339 if (suspend_test(TEST_DEVICES))
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200340 goto Recover_platform;
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100341
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700342 if (suspend_ops->prepare) {
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700343 error = suspend_ops->prepare();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700344 if (error)
345 goto Resume_devices;
346 }
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100347
348 if (suspend_test(TEST_PLATFORM))
349 goto Finish;
350
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700351 error = disable_nonboot_cpus();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100352 if (!error && !suspend_test(TEST_CPUS))
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700353 suspend_enter(state);
354
355 enable_nonboot_cpus();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100356 Finish:
Rafael J. Wysockie6c5eb92007-10-18 03:04:41 -0700357 if (suspend_ops->finish)
358 suspend_ops->finish();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700359 Resume_devices:
David Brownell77437fd2008-07-23 21:28:33 -0700360 suspend_test_start();
Rafael J. Wysocki1eede072008-05-20 23:00:01 +0200361 device_resume(PMSG_RESUME);
David Brownell77437fd2008-07-23 21:28:33 -0700362 suspend_test_finish("resume devices");
Steven Rostedtf42ac382008-08-27 09:14:40 -0400363 __ftrace_enabled_restore(ftrace_save);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700364 resume_console();
Rafael J. Wysockic697eec2008-01-08 00:04:17 +0100365 Close:
366 if (suspend_ops->end)
367 suspend_ops->end();
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700368 return error;
Rafael J. Wysockid8f3de02008-06-12 23:24:06 +0200369
370 Recover_platform:
371 if (suspend_ops->recover)
372 suspend_ops->recover();
373 goto Resume_devices;
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376/**
377 * suspend_finish - Do final work before exiting suspend sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 * Call platform code to clean up, restart processes, and free the
380 * console that we've allocated. This is not called for suspend-to-disk.
381 */
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700382static void suspend_finish(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Johannes Bergb28f5082008-01-15 23:17:00 -0500384 suspend_thaw_processes();
Rafael J. Wysocki1bfcf132008-10-15 22:01:21 -0700385 usermodehelper_enable();
Rafael J. Wysockib10d9112007-07-19 01:47:36 -0700386 pm_notifier_call_chain(PM_POST_SUSPEND);
Johannes Bergaf258f52008-01-11 01:22:23 +0100387 pm_restore_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390
391
392
Andreas Mohr3b364b82006-06-25 05:47:56 -0700393static const char * const pm_states[PM_SUSPEND_MAX] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 [PM_SUSPEND_STANDBY] = "standby",
395 [PM_SUSPEND_MEM] = "mem",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Pavel Machek123d3c12005-11-29 19:34:37 -0800398static inline int valid_state(suspend_state_t state)
399{
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700400 /* All states need lowlevel support and need to be valid
401 * to the lowlevel implementation, no valid callback
Johannes Berg9684e512007-04-30 15:09:55 -0700402 * implies that none are valid. */
Rafael J. Wysocki26398a72007-10-18 03:04:40 -0700403 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
Pavel Machek123d3c12005-11-29 19:34:37 -0800404 return 0;
405 return 1;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409/**
410 * enter_state - Do common work of entering low-power state.
411 * @state: pm_state structure for state we're entering.
412 *
413 * Make sure we're the only ones trying to enter a sleep state. Fail
414 * if someone has beat us to it, since we don't want anything weird to
415 * happen when we wake up.
416 * Then, do the setup for suspend, enter the state, and cleaup (after
417 * we've woken up).
418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419static int enter_state(suspend_state_t state)
420{
421 int error;
422
Pavel Machek123d3c12005-11-29 19:34:37 -0800423 if (!valid_state(state))
Shaohua Lieb9289e2005-10-30 15:00:01 -0800424 return -ENODEV;
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700425
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800426 if (!mutex_trylock(&pm_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EBUSY;
428
Rafael J. Wysocki465d2b42007-12-08 02:08:38 +0100429 printk(KERN_INFO "PM: Syncing filesystems ... ");
Rafael J. Wysocki232b1432007-10-18 03:04:44 -0700430 sys_sync();
431 printk("done.\n");
432
David Brownell82428b62005-05-09 08:07:00 -0700433 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100434 error = suspend_prepare();
435 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto Unlock;
437
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100438 if (suspend_test(TEST_FREEZER))
439 goto Finish;
440
David Brownell82428b62005-05-09 08:07:00 -0700441 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700442 error = suspend_devices_and_enter(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100444 Finish:
David Brownell82428b62005-05-09 08:07:00 -0700445 pr_debug("PM: Finishing wakeup.\n");
Rafael J. Wysocki6c961df2007-07-19 01:47:38 -0700446 suspend_finish();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 Unlock:
Stephen Hemmingera6d70982006-12-06 20:34:35 -0800448 mutex_unlock(&pm_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return error;
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/**
454 * pm_suspend - Externally visible function for suspending system.
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700455 * @state: Enumerated value of state to enter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 *
457 * Determine whether or not value is within range, get state
458 * structure, and enter (above).
459 */
460
461int pm_suspend(suspend_state_t state)
462{
Alexey Starikovskiye2a5b422005-03-18 16:20:46 -0500463 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return enter_state(state);
465 return -EINVAL;
466}
467
Ralf Baechle4cf30342006-12-06 20:36:06 -0800468EXPORT_SYMBOL(pm_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200470#endif /* CONFIG_SUSPEND */
471
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800472struct kobject *power_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474/**
475 * state - control system power state.
476 *
477 * show() returns what states are supported, which is hard-coded to
478 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
479 * 'disk' (Suspend-to-Disk).
480 *
481 * store() accepts one of those strings, translates it into the
482 * proper enumerated value, and initiates a suspend transition.
483 */
484
Kay Sievers386f2752007-11-02 13:47:53 +0100485static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
486 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200488 char *s = buf;
489#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 for (i = 0; i < PM_SUSPEND_MAX; i++) {
Pavel Machek123d3c12005-11-29 19:34:37 -0800493 if (pm_states[i] && valid_state(i))
494 s += sprintf(s,"%s ", pm_states[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200496#endif
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200497#ifdef CONFIG_HIBERNATION
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700498 s += sprintf(s, "%s\n", "disk");
499#else
500 if (s != buf)
501 /* convert the last space to a newline */
502 *(s-1) = '\n';
503#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return (s - buf);
505}
506
Kay Sievers386f2752007-11-02 13:47:53 +0100507static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
508 const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200510#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 suspend_state_t state = PM_SUSPEND_STANDBY;
Andreas Mohr3b364b82006-06-25 05:47:56 -0700512 const char * const *s;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200513#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 int len;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200516 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 p = memchr(buf, '\n', n);
519 len = p ? p - buf : n;
520
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700521 /* First, check if we are requested to hibernate */
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700522 if (len == 4 && !strncmp(buf, "disk", len)) {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700523 error = hibernate();
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200524 goto Exit;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700525 }
526
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200527#ifdef CONFIG_SUSPEND
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
Rafael J. Wysocki8d98a692007-05-16 22:11:19 -0700529 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531 }
dean gaudet47bb7892006-04-27 18:39:17 -0700532 if (state < PM_SUSPEND_MAX && *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 error = enter_state(state);
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200534#endif
535
536 Exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return error ? error : n;
538}
539
540power_attr(state);
541
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700542#ifdef CONFIG_PM_TRACE
543int pm_trace_enabled;
544
Kay Sievers386f2752007-11-02 13:47:53 +0100545static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
546 char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700547{
548 return sprintf(buf, "%d\n", pm_trace_enabled);
549}
550
551static ssize_t
Kay Sievers386f2752007-11-02 13:47:53 +0100552pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
553 const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700554{
555 int val;
556
557 if (sscanf(buf, "%d", &val) == 1) {
558 pm_trace_enabled = !!val;
559 return n;
560 }
561 return -EINVAL;
562}
563
564power_attr(pm_trace);
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100565#endif /* CONFIG_PM_TRACE */
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700566
567static struct attribute * g[] = {
568 &state_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100569#ifdef CONFIG_PM_TRACE
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700570 &pm_trace_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100571#endif
Rafael J. Wysocki7671b8a2007-12-14 01:07:13 +0100572#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100573 &pm_test_attr.attr,
574#endif
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700575 NULL,
576};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578static struct attribute_group attr_group = {
579 .attrs = g,
580};
581
582
583static int __init pm_init(void)
584{
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800585 power_kobj = kobject_create_and_add("power", NULL);
586 if (!power_kobj)
Greg Kroah-Hartman039a5dc2007-11-01 10:39:50 -0700587 return -ENOMEM;
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800588 return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591core_initcall(pm_init);
David Brownell77437fd2008-07-23 21:28:33 -0700592
593
594#ifdef CONFIG_PM_TEST_SUSPEND
595
596#include <linux/rtc.h>
597
598/*
599 * To test system suspend, we need a hands-off mechanism to resume the
600 * system. RTCs wake alarms are a common self-contained mechanism.
601 */
602
603static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
604{
605 static char err_readtime[] __initdata =
606 KERN_ERR "PM: can't read %s time, err %d\n";
607 static char err_wakealarm [] __initdata =
608 KERN_ERR "PM: can't set %s wakealarm, err %d\n";
609 static char err_suspend[] __initdata =
610 KERN_ERR "PM: suspend test failed, error %d\n";
611 static char info_test[] __initdata =
612 KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
613
614 unsigned long now;
615 struct rtc_wkalrm alm;
616 int status;
617
618 /* this may fail if the RTC hasn't been initialized */
619 status = rtc_read_time(rtc, &alm.time);
620 if (status < 0) {
621 printk(err_readtime, rtc->dev.bus_id, status);
622 return;
623 }
624 rtc_tm_to_time(&alm.time, &now);
625
626 memset(&alm, 0, sizeof alm);
627 rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
628 alm.enabled = true;
629
630 status = rtc_set_alarm(rtc, &alm);
631 if (status < 0) {
632 printk(err_wakealarm, rtc->dev.bus_id, status);
633 return;
634 }
635
636 if (state == PM_SUSPEND_MEM) {
637 printk(info_test, pm_states[state]);
638 status = pm_suspend(state);
639 if (status == -ENODEV)
640 state = PM_SUSPEND_STANDBY;
641 }
642 if (state == PM_SUSPEND_STANDBY) {
643 printk(info_test, pm_states[state]);
644 status = pm_suspend(state);
645 }
646 if (status < 0)
647 printk(err_suspend, status);
David Brownella2e2e352008-07-25 19:44:38 -0700648
649 /* Some platforms can't detect that the alarm triggered the
650 * wakeup, or (accordingly) disable it after it afterwards.
651 * It's supposed to give oneshot behavior; cope.
652 */
653 alm.enabled = false;
654 rtc_set_alarm(rtc, &alm);
David Brownell77437fd2008-07-23 21:28:33 -0700655}
656
657static int __init has_wakealarm(struct device *dev, void *name_ptr)
658{
659 struct rtc_device *candidate = to_rtc_device(dev);
660
661 if (!candidate->ops->set_alarm)
662 return 0;
663 if (!device_may_wakeup(candidate->dev.parent))
664 return 0;
665
666 *(char **)name_ptr = dev->bus_id;
667 return 1;
668}
669
670/*
671 * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
672 * at startup time. They're normally disabled, for faster boot and because
673 * we can't know which states really work on this particular system.
674 */
675static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
676
677static char warn_bad_state[] __initdata =
678 KERN_WARNING "PM: can't test '%s' suspend state\n";
679
680static int __init setup_test_suspend(char *value)
681{
682 unsigned i;
683
684 /* "=mem" ==> "mem" */
685 value++;
686 for (i = 0; i < PM_SUSPEND_MAX; i++) {
687 if (!pm_states[i])
688 continue;
689 if (strcmp(pm_states[i], value) != 0)
690 continue;
691 test_state = (__force suspend_state_t) i;
692 return 0;
693 }
694 printk(warn_bad_state, value);
695 return 0;
696}
697__setup("test_suspend", setup_test_suspend);
698
699static int __init test_suspend(void)
700{
701 static char warn_no_rtc[] __initdata =
702 KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
703
704 char *pony = NULL;
705 struct rtc_device *rtc = NULL;
706
707 /* PM is initialized by now; is that state testable? */
708 if (test_state == PM_SUSPEND_ON)
709 goto done;
710 if (!valid_state(test_state)) {
711 printk(warn_bad_state, pm_states[test_state]);
712 goto done;
713 }
714
715 /* RTCs have initialized by now too ... can we use one? */
716 class_find_device(rtc_class, NULL, &pony, has_wakealarm);
717 if (pony)
718 rtc = rtc_class_open(pony);
719 if (!rtc) {
720 printk(warn_no_rtc);
721 goto done;
722 }
723
724 /* go for it */
725 test_wakealarm(rtc, test_state);
726 rtc_class_close(rtc);
727done:
728 return 0;
729}
730late_initcall(test_suspend);
731
732#endif /* CONFIG_PM_TEST_SUSPEND */