2 * kernel/power/main.c - PM subsystem core functionality.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
11 #include <linux/export.h>
12 #include <linux/kobject.h>
13 #include <linux/string.h>
14 #include <linux/pm-trace.h>
15 #include <linux/workqueue.h>
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
21 DEFINE_MUTEX(pm_mutex);
23 #ifdef CONFIG_PM_SLEEP
25 /* Routines for PM-transition notifications */
27 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
29 int register_pm_notifier(struct notifier_block *nb)
31 return blocking_notifier_chain_register(&pm_chain_head, nb);
33 EXPORT_SYMBOL_GPL(register_pm_notifier);
35 int unregister_pm_notifier(struct notifier_block *nb)
37 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
39 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
41 int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls)
45 ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL,
46 nr_to_call, nr_calls);
48 return notifier_to_errno(ret);
50 int pm_notifier_call_chain(unsigned long val)
52 return __pm_notifier_call_chain(val, -1, NULL);
55 /* If set, devices may be suspended and resumed asynchronously. */
56 int pm_async_enabled = 1;
58 static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
61 return sprintf(buf, "%d\n", pm_async_enabled);
64 static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
65 const char *buf, size_t n)
69 if (kstrtoul(buf, 10, &val))
75 pm_async_enabled = val;
81 #ifdef CONFIG_PM_DEBUG
82 int pm_test_level = TEST_NONE;
84 static const char * const pm_tests[__TEST_AFTER_LAST] = {
87 [TEST_CPUS] = "processors",
88 [TEST_PLATFORM] = "platform",
89 [TEST_DEVICES] = "devices",
90 [TEST_FREEZER] = "freezer",
93 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
99 for (level = TEST_FIRST; level <= TEST_MAX; level++)
100 if (pm_tests[level]) {
101 if (level == pm_test_level)
102 s += sprintf(s, "[%s] ", pm_tests[level]);
104 s += sprintf(s, "%s ", pm_tests[level]);
108 /* convert the last space to a newline */
114 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
115 const char *buf, size_t n)
117 const char * const *s;
123 p = memchr(buf, '\n', n);
124 len = p ? p - buf : n;
129 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
130 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
131 pm_test_level = level;
136 unlock_system_sleep();
138 return error ? error : n;
142 #endif /* CONFIG_PM_DEBUG */
144 #ifdef CONFIG_DEBUG_FS
145 static char *suspend_step_name(enum suspend_stat_step step)
150 case SUSPEND_PREPARE:
152 case SUSPEND_SUSPEND:
154 case SUSPEND_SUSPEND_NOIRQ:
155 return "suspend_noirq";
156 case SUSPEND_RESUME_NOIRQ:
157 return "resume_noirq";
165 static int suspend_stats_show(struct seq_file *s, void *unused)
167 int i, index, last_dev, last_errno, last_step;
169 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
170 last_dev %= REC_FAILED_NUM;
171 last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
172 last_errno %= REC_FAILED_NUM;
173 last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
174 last_step %= REC_FAILED_NUM;
175 seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
176 "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
177 "success", suspend_stats.success,
178 "fail", suspend_stats.fail,
179 "failed_freeze", suspend_stats.failed_freeze,
180 "failed_prepare", suspend_stats.failed_prepare,
181 "failed_suspend", suspend_stats.failed_suspend,
182 "failed_suspend_late",
183 suspend_stats.failed_suspend_late,
184 "failed_suspend_noirq",
185 suspend_stats.failed_suspend_noirq,
186 "failed_resume", suspend_stats.failed_resume,
187 "failed_resume_early",
188 suspend_stats.failed_resume_early,
189 "failed_resume_noirq",
190 suspend_stats.failed_resume_noirq);
191 seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
192 suspend_stats.failed_devs[last_dev]);
193 for (i = 1; i < REC_FAILED_NUM; i++) {
194 index = last_dev + REC_FAILED_NUM - i;
195 index %= REC_FAILED_NUM;
196 seq_printf(s, "\t\t\t%-s\n",
197 suspend_stats.failed_devs[index]);
199 seq_printf(s, " last_failed_errno:\t%-d\n",
200 suspend_stats.errno[last_errno]);
201 for (i = 1; i < REC_FAILED_NUM; i++) {
202 index = last_errno + REC_FAILED_NUM - i;
203 index %= REC_FAILED_NUM;
204 seq_printf(s, "\t\t\t%-d\n",
205 suspend_stats.errno[index]);
207 seq_printf(s, " last_failed_step:\t%-s\n",
209 suspend_stats.failed_steps[last_step]));
210 for (i = 1; i < REC_FAILED_NUM; i++) {
211 index = last_step + REC_FAILED_NUM - i;
212 index %= REC_FAILED_NUM;
213 seq_printf(s, "\t\t\t%-s\n",
215 suspend_stats.failed_steps[index]));
221 static int suspend_stats_open(struct inode *inode, struct file *file)
223 return single_open(file, suspend_stats_show, NULL);
226 static const struct file_operations suspend_stats_operations = {
227 .open = suspend_stats_open,
230 .release = single_release,
233 static int __init pm_debugfs_init(void)
235 debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
236 NULL, NULL, &suspend_stats_operations);
240 late_initcall(pm_debugfs_init);
241 #endif /* CONFIG_DEBUG_FS */
243 #endif /* CONFIG_PM_SLEEP */
245 #ifdef CONFIG_PM_SLEEP_DEBUG
247 * pm_print_times: print time taken by devices to suspend and resume.
249 * show() returns whether printing of suspend and resume times is enabled.
250 * store() accepts 0 or 1. 0 disables printing and 1 enables it.
252 bool pm_print_times_enabled;
254 static ssize_t pm_print_times_show(struct kobject *kobj,
255 struct kobj_attribute *attr, char *buf)
257 return sprintf(buf, "%d\n", pm_print_times_enabled);
260 static ssize_t pm_print_times_store(struct kobject *kobj,
261 struct kobj_attribute *attr,
262 const char *buf, size_t n)
266 if (kstrtoul(buf, 10, &val))
272 pm_print_times_enabled = !!val;
276 power_attr(pm_print_times);
278 static inline void pm_print_times_init(void)
280 pm_print_times_enabled = !!initcall_debug;
283 static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
284 struct kobj_attribute *attr,
287 return pm_wakeup_irq ? sprintf(buf, "%u\n", pm_wakeup_irq) : -ENODATA;
290 static ssize_t pm_wakeup_irq_store(struct kobject *kobj,
291 struct kobj_attribute *attr,
292 const char *buf, size_t n)
296 power_attr(pm_wakeup_irq);
298 #else /* !CONFIG_PM_SLEEP_DEBUG */
299 static inline void pm_print_times_init(void) {}
300 #endif /* CONFIG_PM_SLEEP_DEBUG */
302 struct kobject *power_kobj;
305 * state - control system sleep states.
307 * show() returns available sleep state labels, which may be "mem", "standby",
308 * "freeze" and "disk" (hibernation). See Documentation/power/states.txt for a
309 * description of what they mean.
311 * store() accepts one of those strings, translates it into the proper
312 * enumerated value, and initiates a suspend transition.
314 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
318 #ifdef CONFIG_SUSPEND
321 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
323 s += sprintf(s,"%s ", pm_states[i]);
326 if (hibernation_available())
327 s += sprintf(s, "disk ");
329 /* convert the last space to a newline */
334 static suspend_state_t decode_state(const char *buf, size_t n)
336 #ifdef CONFIG_SUSPEND
337 suspend_state_t state;
342 p = memchr(buf, '\n', n);
343 len = p ? p - buf : n;
345 /* Check hibernation first. */
346 if (len == 4 && !strncmp(buf, "disk", len))
347 return PM_SUSPEND_MAX;
349 #ifdef CONFIG_SUSPEND
350 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
351 const char *label = pm_states[state];
353 if (label && len == strlen(label) && !strncmp(buf, label, len))
358 return PM_SUSPEND_ON;
361 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
362 const char *buf, size_t n)
364 suspend_state_t state;
367 error = pm_autosleep_lock();
371 if (pm_autosleep_state() > PM_SUSPEND_ON) {
376 state = decode_state(buf, n);
377 if (state < PM_SUSPEND_MAX)
378 error = pm_suspend(state);
379 else if (state == PM_SUSPEND_MAX)
385 pm_autosleep_unlock();
386 return error ? error : n;
391 #ifdef CONFIG_PM_SLEEP
393 * The 'wakeup_count' attribute, along with the functions defined in
394 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
395 * handled in a non-racy way.
397 * If a wakeup event occurs when the system is in a sleep state, it simply is
398 * woken up. In turn, if an event that would wake the system up from a sleep
399 * state occurs when it is undergoing a transition to that sleep state, the
400 * transition should be aborted. Moreover, if such an event occurs when the
401 * system is in the working state, an attempt to start a transition to the
402 * given sleep state should fail during certain period after the detection of
403 * the event. Using the 'state' attribute alone is not sufficient to satisfy
404 * these requirements, because a wakeup event may occur exactly when 'state'
405 * is being written to and may be delivered to user space right before it is
406 * frozen, so the event will remain only partially processed until the system is
407 * woken up by another event. In particular, it won't cause the transition to
408 * a sleep state to be aborted.
410 * This difficulty may be overcome if user space uses 'wakeup_count' before
411 * writing to 'state'. It first should read from 'wakeup_count' and store
412 * the read value. Then, after carrying out its own preparations for the system
413 * transition to a sleep state, it should write the stored value to
414 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
415 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
416 * is allowed to write to 'state', but the transition will be aborted if there
417 * are any wakeup events detected after 'wakeup_count' was written to.
420 static ssize_t wakeup_count_show(struct kobject *kobj,
421 struct kobj_attribute *attr,
426 return pm_get_wakeup_count(&val, true) ?
427 sprintf(buf, "%u\n", val) : -EINTR;
430 static ssize_t wakeup_count_store(struct kobject *kobj,
431 struct kobj_attribute *attr,
432 const char *buf, size_t n)
437 error = pm_autosleep_lock();
441 if (pm_autosleep_state() > PM_SUSPEND_ON) {
447 if (sscanf(buf, "%u", &val) == 1) {
448 if (pm_save_wakeup_count(val))
451 pm_print_active_wakeup_sources();
455 pm_autosleep_unlock();
459 power_attr(wakeup_count);
461 #ifdef CONFIG_PM_AUTOSLEEP
462 static ssize_t autosleep_show(struct kobject *kobj,
463 struct kobj_attribute *attr,
466 suspend_state_t state = pm_autosleep_state();
468 if (state == PM_SUSPEND_ON)
469 return sprintf(buf, "off\n");
471 #ifdef CONFIG_SUSPEND
472 if (state < PM_SUSPEND_MAX)
473 return sprintf(buf, "%s\n", pm_states[state] ?
474 pm_states[state] : "error");
476 #ifdef CONFIG_HIBERNATION
477 return sprintf(buf, "disk\n");
479 return sprintf(buf, "error");
483 static ssize_t autosleep_store(struct kobject *kobj,
484 struct kobj_attribute *attr,
485 const char *buf, size_t n)
487 suspend_state_t state = decode_state(buf, n);
490 if (state == PM_SUSPEND_ON
491 && strcmp(buf, "off") && strcmp(buf, "off\n"))
494 error = pm_autosleep_set_state(state);
495 return error ? error : n;
498 power_attr(autosleep);
499 #endif /* CONFIG_PM_AUTOSLEEP */
501 #ifdef CONFIG_PM_WAKELOCKS
502 static ssize_t wake_lock_show(struct kobject *kobj,
503 struct kobj_attribute *attr,
506 return pm_show_wakelocks(buf, true);
509 static ssize_t wake_lock_store(struct kobject *kobj,
510 struct kobj_attribute *attr,
511 const char *buf, size_t n)
513 int error = pm_wake_lock(buf);
514 return error ? error : n;
517 power_attr(wake_lock);
519 static ssize_t wake_unlock_show(struct kobject *kobj,
520 struct kobj_attribute *attr,
523 return pm_show_wakelocks(buf, false);
526 static ssize_t wake_unlock_store(struct kobject *kobj,
527 struct kobj_attribute *attr,
528 const char *buf, size_t n)
530 int error = pm_wake_unlock(buf);
531 return error ? error : n;
534 power_attr(wake_unlock);
536 #endif /* CONFIG_PM_WAKELOCKS */
537 #endif /* CONFIG_PM_SLEEP */
539 #ifdef CONFIG_PM_TRACE
540 int pm_trace_enabled;
542 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
545 return sprintf(buf, "%d\n", pm_trace_enabled);
549 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
550 const char *buf, size_t n)
554 if (sscanf(buf, "%d", &val) == 1) {
555 pm_trace_enabled = !!val;
556 if (pm_trace_enabled) {
557 pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
558 "PM: Correct system time has to be restored manually after resume.\n");
565 power_attr(pm_trace);
567 static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
568 struct kobj_attribute *attr,
571 return show_trace_dev_match(buf, PAGE_SIZE);
575 pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr,
576 const char *buf, size_t n)
581 power_attr(pm_trace_dev_match);
583 #endif /* CONFIG_PM_TRACE */
585 #ifdef CONFIG_FREEZER
586 static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
587 struct kobj_attribute *attr, char *buf)
589 return sprintf(buf, "%u\n", freeze_timeout_msecs);
592 static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
593 struct kobj_attribute *attr,
594 const char *buf, size_t n)
598 if (kstrtoul(buf, 10, &val))
601 freeze_timeout_msecs = val;
605 power_attr(pm_freeze_timeout);
607 #endif /* CONFIG_FREEZER*/
609 static struct attribute * g[] = {
611 #ifdef CONFIG_PM_TRACE
613 &pm_trace_dev_match_attr.attr,
615 #ifdef CONFIG_PM_SLEEP
617 &wakeup_count_attr.attr,
618 #ifdef CONFIG_PM_AUTOSLEEP
619 &autosleep_attr.attr,
621 #ifdef CONFIG_PM_WAKELOCKS
622 &wake_lock_attr.attr,
623 &wake_unlock_attr.attr,
625 #ifdef CONFIG_PM_DEBUG
628 #ifdef CONFIG_PM_SLEEP_DEBUG
629 &pm_print_times_attr.attr,
630 &pm_wakeup_irq_attr.attr,
633 #ifdef CONFIG_FREEZER
634 &pm_freeze_timeout_attr.attr,
639 static struct attribute_group attr_group = {
643 struct workqueue_struct *pm_wq;
644 EXPORT_SYMBOL_GPL(pm_wq);
646 static int __init pm_start_workqueue(void)
648 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
650 return pm_wq ? 0 : -ENOMEM;
653 static int __init pm_init(void)
655 int error = pm_start_workqueue();
658 hibernate_image_size_init();
659 hibernate_reserved_size_init();
660 power_kobj = kobject_create_and_add("power", NULL);
663 error = sysfs_create_group(power_kobj, &attr_group);
666 pm_print_times_init();
667 return pm_autosleep_init();
670 core_initcall(pm_init);