[SCSI] sd: remove command-size switching code
[linux-2.6/mini2440.git] / kernel / power / main.c
blob19122cf6d827d13d03e8a7520d0120d5adf92bbc
1 /*
2 * kernel/power/main.c - PM subsystem core functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 *
7 * This file is released under the GPLv2
9 */
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/kmod.h>
18 #include <linux/init.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 #include <linux/syscalls.h>
25 #include <linux/ftrace.h>
27 #include "power.h"
29 DEFINE_MUTEX(pm_mutex);
31 unsigned int pm_flags;
32 EXPORT_SYMBOL(pm_flags);
34 #ifdef CONFIG_PM_SLEEP
36 /* Routines for PM-transition notifications */
38 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
40 int register_pm_notifier(struct notifier_block *nb)
42 return blocking_notifier_chain_register(&pm_chain_head, nb);
44 EXPORT_SYMBOL_GPL(register_pm_notifier);
46 int unregister_pm_notifier(struct notifier_block *nb)
48 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
50 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
52 int pm_notifier_call_chain(unsigned long val)
54 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL)
55 == NOTIFY_BAD) ? -EINVAL : 0;
58 #ifdef CONFIG_PM_DEBUG
59 int pm_test_level = TEST_NONE;
61 static int suspend_test(int level)
63 if (pm_test_level == level) {
64 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
65 mdelay(5000);
66 return 1;
68 return 0;
71 static 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",
80 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
81 char *buf)
83 char *s = buf;
84 int level;
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]);
94 if (s != buf)
95 /* convert the last space to a newline */
96 *(s-1) = '\n';
98 return (s - buf);
101 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
102 const char *buf, size_t n)
104 const char * const *s;
105 int level;
106 char *p;
107 int len;
108 int error = -EINVAL;
110 p = memchr(buf, '\n', n);
111 len = p ? p - buf : n;
113 mutex_lock(&pm_mutex);
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;
123 mutex_unlock(&pm_mutex);
125 return error ? error : n;
128 power_attr(pm_test);
129 #else /* !CONFIG_PM_DEBUG */
130 static inline int suspend_test(int level) { return 0; }
131 #endif /* !CONFIG_PM_DEBUG */
133 #endif /* CONFIG_PM_SLEEP */
135 #ifdef CONFIG_SUSPEND
137 #ifdef CONFIG_PM_TEST_SUSPEND
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.
144 * The time it takes is system-specific though, so when we test this
145 * during system bootup we allow a LOT of time.
147 #define TEST_SUSPEND_SECONDS 5
149 static unsigned long suspend_test_start_time;
151 static void suspend_test_start(void)
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...
157 suspend_test_start_time = jiffies;
160 static void suspend_test_finish(const char *label)
162 long nj = jiffies - suspend_test_start_time;
163 unsigned msec;
165 msec = jiffies_to_msecs(abs(nj));
166 pr_info("PM: %s took %d.%03d seconds\n", label,
167 msec / 1000, msec % 1000);
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!
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...
177 WARN_ON(msec > (TEST_SUSPEND_SECONDS * 1000));
180 #else
182 static void suspend_test_start(void)
186 static void suspend_test_finish(const char *label)
190 #endif
192 /* This is just an arbitrary number */
193 #define FREE_PAGE_NUMBER (100)
195 static struct platform_suspend_ops *suspend_ops;
198 * suspend_set_ops - Set the global suspend method table.
199 * @ops: Pointer to ops structure.
202 void suspend_set_ops(struct platform_suspend_ops *ops)
204 mutex_lock(&pm_mutex);
205 suspend_ops = ops;
206 mutex_unlock(&pm_mutex);
210 * suspend_valid_only_mem - generic memory-only valid callback
212 * Platform drivers that implement mem suspend only and only need
213 * to check for that in their .valid callback can use this instead
214 * of rolling their own .valid callback.
216 int suspend_valid_only_mem(suspend_state_t state)
218 return state == PM_SUSPEND_MEM;
222 * suspend_prepare - Do prep work before entering low-power state.
224 * 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.
227 static int suspend_prepare(void)
229 int error;
230 unsigned int free_pages;
232 if (!suspend_ops || !suspend_ops->enter)
233 return -EPERM;
235 pm_prepare_console();
237 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
238 if (error)
239 goto Finish;
241 error = usermodehelper_disable();
242 if (error)
243 goto Finish;
245 if (suspend_freeze_processes()) {
246 error = -EAGAIN;
247 goto Thaw;
250 free_pages = global_page_state(NR_FREE_PAGES);
251 if (free_pages < FREE_PAGE_NUMBER) {
252 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");
259 if (!error)
260 return 0;
262 Thaw:
263 suspend_thaw_processes();
264 usermodehelper_enable();
265 Finish:
266 pm_notifier_call_chain(PM_POST_SUSPEND);
267 pm_restore_console();
268 return error;
271 /* default implementation */
272 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
274 local_irq_disable();
277 /* default implementation */
278 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
280 local_irq_enable();
284 * suspend_enter - enter the desired system sleep state.
285 * @state: state to enter
287 * This function should be called after devices have been suspended.
289 static int suspend_enter(suspend_state_t state)
291 int error = 0;
293 device_pm_lock();
294 arch_suspend_disable_irqs();
295 BUG_ON(!irqs_disabled());
297 if ((error = device_power_down(PMSG_SUSPEND))) {
298 printk(KERN_ERR "PM: Some devices failed to power down\n");
299 goto Done;
302 if (!suspend_test(TEST_CORE))
303 error = suspend_ops->enter(state);
305 device_power_up(PMSG_RESUME);
306 Done:
307 arch_suspend_enable_irqs();
308 BUG_ON(irqs_disabled());
309 device_pm_unlock();
310 return error;
314 * suspend_devices_and_enter - suspend devices and enter the desired system
315 * sleep state.
316 * @state: state to enter
318 int suspend_devices_and_enter(suspend_state_t state)
320 int error, ftrace_save;
322 if (!suspend_ops)
323 return -ENOSYS;
325 if (suspend_ops->begin) {
326 error = suspend_ops->begin(state);
327 if (error)
328 goto Close;
330 suspend_console();
331 ftrace_save = __ftrace_enabled_save();
332 suspend_test_start();
333 error = device_suspend(PMSG_SUSPEND);
334 if (error) {
335 printk(KERN_ERR "PM: Some devices failed to suspend\n");
336 goto Recover_platform;
338 suspend_test_finish("suspend devices");
339 if (suspend_test(TEST_DEVICES))
340 goto Recover_platform;
342 if (suspend_ops->prepare) {
343 error = suspend_ops->prepare();
344 if (error)
345 goto Resume_devices;
348 if (suspend_test(TEST_PLATFORM))
349 goto Finish;
351 error = disable_nonboot_cpus();
352 if (!error && !suspend_test(TEST_CPUS))
353 suspend_enter(state);
355 enable_nonboot_cpus();
356 Finish:
357 if (suspend_ops->finish)
358 suspend_ops->finish();
359 Resume_devices:
360 suspend_test_start();
361 device_resume(PMSG_RESUME);
362 suspend_test_finish("resume devices");
363 __ftrace_enabled_restore(ftrace_save);
364 resume_console();
365 Close:
366 if (suspend_ops->end)
367 suspend_ops->end();
368 return error;
370 Recover_platform:
371 if (suspend_ops->recover)
372 suspend_ops->recover();
373 goto Resume_devices;
377 * suspend_finish - Do final work before exiting suspend sequence.
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.
382 static void suspend_finish(void)
384 suspend_thaw_processes();
385 usermodehelper_enable();
386 pm_notifier_call_chain(PM_POST_SUSPEND);
387 pm_restore_console();
393 static const char * const pm_states[PM_SUSPEND_MAX] = {
394 [PM_SUSPEND_STANDBY] = "standby",
395 [PM_SUSPEND_MEM] = "mem",
398 static inline int valid_state(suspend_state_t state)
400 /* All states need lowlevel support and need to be valid
401 * to the lowlevel implementation, no valid callback
402 * implies that none are valid. */
403 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
404 return 0;
405 return 1;
410 * enter_state - Do common work of entering low-power state.
411 * @state: pm_state structure for state we're entering.
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).
419 static int enter_state(suspend_state_t state)
421 int error;
423 if (!valid_state(state))
424 return -ENODEV;
426 if (!mutex_trylock(&pm_mutex))
427 return -EBUSY;
429 printk(KERN_INFO "PM: Syncing filesystems ... ");
430 sys_sync();
431 printk("done.\n");
433 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
434 error = suspend_prepare();
435 if (error)
436 goto Unlock;
438 if (suspend_test(TEST_FREEZER))
439 goto Finish;
441 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
442 error = suspend_devices_and_enter(state);
444 Finish:
445 pr_debug("PM: Finishing wakeup.\n");
446 suspend_finish();
447 Unlock:
448 mutex_unlock(&pm_mutex);
449 return error;
454 * pm_suspend - Externally visible function for suspending system.
455 * @state: Enumerated value of state to enter.
457 * Determine whether or not value is within range, get state
458 * structure, and enter (above).
461 int pm_suspend(suspend_state_t state)
463 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
464 return enter_state(state);
465 return -EINVAL;
468 EXPORT_SYMBOL(pm_suspend);
470 #endif /* CONFIG_SUSPEND */
472 struct kobject *power_kobj;
475 * state - control system power state.
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).
481 * store() accepts one of those strings, translates it into the
482 * proper enumerated value, and initiates a suspend transition.
485 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
486 char *buf)
488 char *s = buf;
489 #ifdef CONFIG_SUSPEND
490 int i;
492 for (i = 0; i < PM_SUSPEND_MAX; i++) {
493 if (pm_states[i] && valid_state(i))
494 s += sprintf(s,"%s ", pm_states[i]);
496 #endif
497 #ifdef CONFIG_HIBERNATION
498 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
504 return (s - buf);
507 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
508 const char *buf, size_t n)
510 #ifdef CONFIG_SUSPEND
511 suspend_state_t state = PM_SUSPEND_STANDBY;
512 const char * const *s;
513 #endif
514 char *p;
515 int len;
516 int error = -EINVAL;
518 p = memchr(buf, '\n', n);
519 len = p ? p - buf : n;
521 /* First, check if we are requested to hibernate */
522 if (len == 4 && !strncmp(buf, "disk", len)) {
523 error = hibernate();
524 goto Exit;
527 #ifdef CONFIG_SUSPEND
528 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
529 if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
530 break;
532 if (state < PM_SUSPEND_MAX && *s)
533 error = enter_state(state);
534 #endif
536 Exit:
537 return error ? error : n;
540 power_attr(state);
542 #ifdef CONFIG_PM_TRACE
543 int pm_trace_enabled;
545 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
546 char *buf)
548 return sprintf(buf, "%d\n", pm_trace_enabled);
551 static ssize_t
552 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
553 const char *buf, size_t n)
555 int val;
557 if (sscanf(buf, "%d", &val) == 1) {
558 pm_trace_enabled = !!val;
559 return n;
561 return -EINVAL;
564 power_attr(pm_trace);
565 #endif /* CONFIG_PM_TRACE */
567 static struct attribute * g[] = {
568 &state_attr.attr,
569 #ifdef CONFIG_PM_TRACE
570 &pm_trace_attr.attr,
571 #endif
572 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
573 &pm_test_attr.attr,
574 #endif
575 NULL,
578 static struct attribute_group attr_group = {
579 .attrs = g,
583 static int __init pm_init(void)
585 power_kobj = kobject_create_and_add("power", NULL);
586 if (!power_kobj)
587 return -ENOMEM;
588 return sysfs_create_group(power_kobj, &attr_group);
591 core_initcall(pm_init);
594 #ifdef CONFIG_PM_TEST_SUSPEND
596 #include <linux/rtc.h>
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.
603 static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
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";
614 unsigned long now;
615 struct rtc_wkalrm alm;
616 int status;
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;
624 rtc_tm_to_time(&alm.time, &now);
626 memset(&alm, 0, sizeof alm);
627 rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
628 alm.enabled = true;
630 status = rtc_set_alarm(rtc, &alm);
631 if (status < 0) {
632 printk(err_wakealarm, rtc->dev.bus_id, status);
633 return;
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;
642 if (state == PM_SUSPEND_STANDBY) {
643 printk(info_test, pm_states[state]);
644 status = pm_suspend(state);
646 if (status < 0)
647 printk(err_suspend, status);
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.
653 alm.enabled = false;
654 rtc_set_alarm(rtc, &alm);
657 static int __init has_wakealarm(struct device *dev, void *name_ptr)
659 struct rtc_device *candidate = to_rtc_device(dev);
661 if (!candidate->ops->set_alarm)
662 return 0;
663 if (!device_may_wakeup(candidate->dev.parent))
664 return 0;
666 *(char **)name_ptr = dev->bus_id;
667 return 1;
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.
675 static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
677 static char warn_bad_state[] __initdata =
678 KERN_WARNING "PM: can't test '%s' suspend state\n";
680 static int __init setup_test_suspend(char *value)
682 unsigned i;
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;
694 printk(warn_bad_state, value);
695 return 0;
697 __setup("test_suspend", setup_test_suspend);
699 static int __init test_suspend(void)
701 static char warn_no_rtc[] __initdata =
702 KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
704 char *pony = NULL;
705 struct rtc_device *rtc = NULL;
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;
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;
724 /* go for it */
725 test_wakealarm(rtc, test_state);
726 rtc_class_close(rtc);
727 done:
728 return 0;
730 late_initcall(test_suspend);
732 #endif /* CONFIG_PM_TEST_SUSPEND */