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>
18 #include <linux/suspend.h>
19 #include <linux/syscalls.h>
23 #ifdef CONFIG_PM_SLEEP
25 void lock_system_sleep(void)
27 current->flags |= PF_FREEZER_SKIP;
28 mutex_lock(&system_transition_mutex);
30 EXPORT_SYMBOL_GPL(lock_system_sleep);
32 void unlock_system_sleep(void)
35 * Don't use freezer_count() because we don't want the call to
36 * try_to_freeze() here.
39 * Fundamentally, we just don't need it, because freezing condition
40 * doesn't come into effect until we release the
41 * system_transition_mutex lock, since the freezer always works with
42 * system_transition_mutex held.
44 * More importantly, in the case of hibernation,
45 * unlock_system_sleep() gets called in snapshot_read() and
46 * snapshot_write() when the freezing condition is still in effect.
47 * Which means, if we use try_to_freeze() here, it would make them
48 * enter the refrigerator, thus causing hibernation to lockup.
50 current->flags &= ~PF_FREEZER_SKIP;
51 mutex_unlock(&system_transition_mutex);
53 EXPORT_SYMBOL_GPL(unlock_system_sleep);
55 void ksys_sync_helper(void)
62 elapsed_msecs = ktime_to_ms(ktime_sub(ktime_get(), start));
63 pr_info("Filesystems sync: %ld.%03ld seconds\n",
64 elapsed_msecs / MSEC_PER_SEC, elapsed_msecs % MSEC_PER_SEC);
66 EXPORT_SYMBOL_GPL(ksys_sync_helper);
68 /* Routines for PM-transition notifications */
70 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
72 int register_pm_notifier(struct notifier_block *nb)
74 return blocking_notifier_chain_register(&pm_chain_head, nb);
76 EXPORT_SYMBOL_GPL(register_pm_notifier);
78 int unregister_pm_notifier(struct notifier_block *nb)
80 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
82 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
84 int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls)
88 ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL,
89 nr_to_call, nr_calls);
91 return notifier_to_errno(ret);
93 int pm_notifier_call_chain(unsigned long val)
95 return __pm_notifier_call_chain(val, -1, NULL);
98 /* If set, devices may be suspended and resumed asynchronously. */
99 int pm_async_enabled = 1;
101 static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
104 return sprintf(buf, "%d\n", pm_async_enabled);
107 static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
108 const char *buf, size_t n)
112 if (kstrtoul(buf, 10, &val))
118 pm_async_enabled = val;
122 power_attr(pm_async);
124 #ifdef CONFIG_SUSPEND
125 static ssize_t mem_sleep_show(struct kobject *kobj, struct kobj_attribute *attr,
131 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
132 if (mem_sleep_states[i]) {
133 const char *label = mem_sleep_states[i];
135 if (mem_sleep_current == i)
136 s += sprintf(s, "[%s] ", label);
138 s += sprintf(s, "%s ", label);
141 /* Convert the last space to a newline if needed. */
148 static suspend_state_t decode_suspend_state(const char *buf, size_t n)
150 suspend_state_t state;
154 p = memchr(buf, '\n', n);
155 len = p ? p - buf : n;
157 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
158 const char *label = mem_sleep_states[state];
160 if (label && len == strlen(label) && !strncmp(buf, label, len))
164 return PM_SUSPEND_ON;
167 static ssize_t mem_sleep_store(struct kobject *kobj, struct kobj_attribute *attr,
168 const char *buf, size_t n)
170 suspend_state_t state;
173 error = pm_autosleep_lock();
177 if (pm_autosleep_state() > PM_SUSPEND_ON) {
182 state = decode_suspend_state(buf, n);
183 if (state < PM_SUSPEND_MAX && state > PM_SUSPEND_ON)
184 mem_sleep_current = state;
189 pm_autosleep_unlock();
190 return error ? error : n;
193 power_attr(mem_sleep);
194 #endif /* CONFIG_SUSPEND */
196 #ifdef CONFIG_PM_SLEEP_DEBUG
197 int pm_test_level = TEST_NONE;
199 static const char * const pm_tests[__TEST_AFTER_LAST] = {
200 [TEST_NONE] = "none",
201 [TEST_CORE] = "core",
202 [TEST_CPUS] = "processors",
203 [TEST_PLATFORM] = "platform",
204 [TEST_DEVICES] = "devices",
205 [TEST_FREEZER] = "freezer",
208 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
214 for (level = TEST_FIRST; level <= TEST_MAX; level++)
215 if (pm_tests[level]) {
216 if (level == pm_test_level)
217 s += sprintf(s, "[%s] ", pm_tests[level]);
219 s += sprintf(s, "%s ", pm_tests[level]);
223 /* convert the last space to a newline */
229 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
230 const char *buf, size_t n)
232 const char * const *s;
238 p = memchr(buf, '\n', n);
239 len = p ? p - buf : n;
244 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
245 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
246 pm_test_level = level;
251 unlock_system_sleep();
253 return error ? error : n;
257 #endif /* CONFIG_PM_SLEEP_DEBUG */
259 #ifdef CONFIG_DEBUG_FS
260 static char *suspend_step_name(enum suspend_stat_step step)
265 case SUSPEND_PREPARE:
267 case SUSPEND_SUSPEND:
269 case SUSPEND_SUSPEND_NOIRQ:
270 return "suspend_noirq";
271 case SUSPEND_RESUME_NOIRQ:
272 return "resume_noirq";
280 static int suspend_stats_show(struct seq_file *s, void *unused)
282 int i, index, last_dev, last_errno, last_step;
284 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
285 last_dev %= REC_FAILED_NUM;
286 last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
287 last_errno %= REC_FAILED_NUM;
288 last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
289 last_step %= REC_FAILED_NUM;
290 seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
291 "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
292 "success", suspend_stats.success,
293 "fail", suspend_stats.fail,
294 "failed_freeze", suspend_stats.failed_freeze,
295 "failed_prepare", suspend_stats.failed_prepare,
296 "failed_suspend", suspend_stats.failed_suspend,
297 "failed_suspend_late",
298 suspend_stats.failed_suspend_late,
299 "failed_suspend_noirq",
300 suspend_stats.failed_suspend_noirq,
301 "failed_resume", suspend_stats.failed_resume,
302 "failed_resume_early",
303 suspend_stats.failed_resume_early,
304 "failed_resume_noirq",
305 suspend_stats.failed_resume_noirq);
306 seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
307 suspend_stats.failed_devs[last_dev]);
308 for (i = 1; i < REC_FAILED_NUM; i++) {
309 index = last_dev + REC_FAILED_NUM - i;
310 index %= REC_FAILED_NUM;
311 seq_printf(s, "\t\t\t%-s\n",
312 suspend_stats.failed_devs[index]);
314 seq_printf(s, " last_failed_errno:\t%-d\n",
315 suspend_stats.errno[last_errno]);
316 for (i = 1; i < REC_FAILED_NUM; i++) {
317 index = last_errno + REC_FAILED_NUM - i;
318 index %= REC_FAILED_NUM;
319 seq_printf(s, "\t\t\t%-d\n",
320 suspend_stats.errno[index]);
322 seq_printf(s, " last_failed_step:\t%-s\n",
324 suspend_stats.failed_steps[last_step]));
325 for (i = 1; i < REC_FAILED_NUM; i++) {
326 index = last_step + REC_FAILED_NUM - i;
327 index %= REC_FAILED_NUM;
328 seq_printf(s, "\t\t\t%-s\n",
330 suspend_stats.failed_steps[index]));
335 DEFINE_SHOW_ATTRIBUTE(suspend_stats);
337 static int __init pm_debugfs_init(void)
339 debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
340 NULL, NULL, &suspend_stats_fops);
344 late_initcall(pm_debugfs_init);
345 #endif /* CONFIG_DEBUG_FS */
347 #endif /* CONFIG_PM_SLEEP */
349 #ifdef CONFIG_PM_SLEEP_DEBUG
351 * pm_print_times: print time taken by devices to suspend and resume.
353 * show() returns whether printing of suspend and resume times is enabled.
354 * store() accepts 0 or 1. 0 disables printing and 1 enables it.
356 bool pm_print_times_enabled;
358 static ssize_t pm_print_times_show(struct kobject *kobj,
359 struct kobj_attribute *attr, char *buf)
361 return sprintf(buf, "%d\n", pm_print_times_enabled);
364 static ssize_t pm_print_times_store(struct kobject *kobj,
365 struct kobj_attribute *attr,
366 const char *buf, size_t n)
370 if (kstrtoul(buf, 10, &val))
376 pm_print_times_enabled = !!val;
380 power_attr(pm_print_times);
382 static inline void pm_print_times_init(void)
384 pm_print_times_enabled = !!initcall_debug;
387 static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
388 struct kobj_attribute *attr,
391 return pm_wakeup_irq ? sprintf(buf, "%u\n", pm_wakeup_irq) : -ENODATA;
394 power_attr_ro(pm_wakeup_irq);
396 bool pm_debug_messages_on __read_mostly;
398 static ssize_t pm_debug_messages_show(struct kobject *kobj,
399 struct kobj_attribute *attr, char *buf)
401 return sprintf(buf, "%d\n", pm_debug_messages_on);
404 static ssize_t pm_debug_messages_store(struct kobject *kobj,
405 struct kobj_attribute *attr,
406 const char *buf, size_t n)
410 if (kstrtoul(buf, 10, &val))
416 pm_debug_messages_on = !!val;
420 power_attr(pm_debug_messages);
423 * __pm_pr_dbg - Print a suspend debug message to the kernel log.
424 * @defer: Whether or not to use printk_deferred() to print the message.
425 * @fmt: Message format.
427 * The message will be emitted if enabled through the pm_debug_messages
430 void __pm_pr_dbg(bool defer, const char *fmt, ...)
432 struct va_format vaf;
435 if (!pm_debug_messages_on)
444 printk_deferred(KERN_DEBUG "PM: %pV", &vaf);
446 printk(KERN_DEBUG "PM: %pV", &vaf);
451 #else /* !CONFIG_PM_SLEEP_DEBUG */
452 static inline void pm_print_times_init(void) {}
453 #endif /* CONFIG_PM_SLEEP_DEBUG */
455 struct kobject *power_kobj;
458 * state - control system sleep states.
460 * show() returns available sleep state labels, which may be "mem", "standby",
461 * "freeze" and "disk" (hibernation).
462 * See Documentation/admin-guide/pm/sleep-states.rst for a description of
465 * store() accepts one of those strings, translates it into the proper
466 * enumerated value, and initiates a suspend transition.
468 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
472 #ifdef CONFIG_SUSPEND
475 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
477 s += sprintf(s,"%s ", pm_states[i]);
480 if (hibernation_available())
481 s += sprintf(s, "disk ");
483 /* convert the last space to a newline */
488 static suspend_state_t decode_state(const char *buf, size_t n)
490 #ifdef CONFIG_SUSPEND
491 suspend_state_t state;
496 p = memchr(buf, '\n', n);
497 len = p ? p - buf : n;
499 /* Check hibernation first. */
500 if (len == 4 && !strncmp(buf, "disk", len))
501 return PM_SUSPEND_MAX;
503 #ifdef CONFIG_SUSPEND
504 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
505 const char *label = pm_states[state];
507 if (label && len == strlen(label) && !strncmp(buf, label, len))
512 return PM_SUSPEND_ON;
515 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
516 const char *buf, size_t n)
518 suspend_state_t state;
521 error = pm_autosleep_lock();
525 if (pm_autosleep_state() > PM_SUSPEND_ON) {
530 state = decode_state(buf, n);
531 if (state < PM_SUSPEND_MAX) {
532 if (state == PM_SUSPEND_MEM)
533 state = mem_sleep_current;
535 error = pm_suspend(state);
536 } else if (state == PM_SUSPEND_MAX) {
543 pm_autosleep_unlock();
544 return error ? error : n;
549 #ifdef CONFIG_PM_SLEEP
551 * The 'wakeup_count' attribute, along with the functions defined in
552 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
553 * handled in a non-racy way.
555 * If a wakeup event occurs when the system is in a sleep state, it simply is
556 * woken up. In turn, if an event that would wake the system up from a sleep
557 * state occurs when it is undergoing a transition to that sleep state, the
558 * transition should be aborted. Moreover, if such an event occurs when the
559 * system is in the working state, an attempt to start a transition to the
560 * given sleep state should fail during certain period after the detection of
561 * the event. Using the 'state' attribute alone is not sufficient to satisfy
562 * these requirements, because a wakeup event may occur exactly when 'state'
563 * is being written to and may be delivered to user space right before it is
564 * frozen, so the event will remain only partially processed until the system is
565 * woken up by another event. In particular, it won't cause the transition to
566 * a sleep state to be aborted.
568 * This difficulty may be overcome if user space uses 'wakeup_count' before
569 * writing to 'state'. It first should read from 'wakeup_count' and store
570 * the read value. Then, after carrying out its own preparations for the system
571 * transition to a sleep state, it should write the stored value to
572 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
573 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
574 * is allowed to write to 'state', but the transition will be aborted if there
575 * are any wakeup events detected after 'wakeup_count' was written to.
578 static ssize_t wakeup_count_show(struct kobject *kobj,
579 struct kobj_attribute *attr,
584 return pm_get_wakeup_count(&val, true) ?
585 sprintf(buf, "%u\n", val) : -EINTR;
588 static ssize_t wakeup_count_store(struct kobject *kobj,
589 struct kobj_attribute *attr,
590 const char *buf, size_t n)
595 error = pm_autosleep_lock();
599 if (pm_autosleep_state() > PM_SUSPEND_ON) {
605 if (sscanf(buf, "%u", &val) == 1) {
606 if (pm_save_wakeup_count(val))
609 pm_print_active_wakeup_sources();
613 pm_autosleep_unlock();
617 power_attr(wakeup_count);
619 #ifdef CONFIG_PM_AUTOSLEEP
620 static ssize_t autosleep_show(struct kobject *kobj,
621 struct kobj_attribute *attr,
624 suspend_state_t state = pm_autosleep_state();
626 if (state == PM_SUSPEND_ON)
627 return sprintf(buf, "off\n");
629 #ifdef CONFIG_SUSPEND
630 if (state < PM_SUSPEND_MAX)
631 return sprintf(buf, "%s\n", pm_states[state] ?
632 pm_states[state] : "error");
634 #ifdef CONFIG_HIBERNATION
635 return sprintf(buf, "disk\n");
637 return sprintf(buf, "error");
641 static ssize_t autosleep_store(struct kobject *kobj,
642 struct kobj_attribute *attr,
643 const char *buf, size_t n)
645 suspend_state_t state = decode_state(buf, n);
648 if (state == PM_SUSPEND_ON
649 && strcmp(buf, "off") && strcmp(buf, "off\n"))
652 if (state == PM_SUSPEND_MEM)
653 state = mem_sleep_current;
655 error = pm_autosleep_set_state(state);
656 return error ? error : n;
659 power_attr(autosleep);
660 #endif /* CONFIG_PM_AUTOSLEEP */
662 #ifdef CONFIG_PM_WAKELOCKS
663 static ssize_t wake_lock_show(struct kobject *kobj,
664 struct kobj_attribute *attr,
667 return pm_show_wakelocks(buf, true);
670 static ssize_t wake_lock_store(struct kobject *kobj,
671 struct kobj_attribute *attr,
672 const char *buf, size_t n)
674 int error = pm_wake_lock(buf);
675 return error ? error : n;
678 power_attr(wake_lock);
680 static ssize_t wake_unlock_show(struct kobject *kobj,
681 struct kobj_attribute *attr,
684 return pm_show_wakelocks(buf, false);
687 static ssize_t wake_unlock_store(struct kobject *kobj,
688 struct kobj_attribute *attr,
689 const char *buf, size_t n)
691 int error = pm_wake_unlock(buf);
692 return error ? error : n;
695 power_attr(wake_unlock);
697 #endif /* CONFIG_PM_WAKELOCKS */
698 #endif /* CONFIG_PM_SLEEP */
700 #ifdef CONFIG_PM_TRACE
701 int pm_trace_enabled;
703 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
706 return sprintf(buf, "%d\n", pm_trace_enabled);
710 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
711 const char *buf, size_t n)
715 if (sscanf(buf, "%d", &val) == 1) {
716 pm_trace_enabled = !!val;
717 if (pm_trace_enabled) {
718 pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
719 "PM: Correct system time has to be restored manually after resume.\n");
726 power_attr(pm_trace);
728 static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
729 struct kobj_attribute *attr,
732 return show_trace_dev_match(buf, PAGE_SIZE);
735 power_attr_ro(pm_trace_dev_match);
737 #endif /* CONFIG_PM_TRACE */
739 #ifdef CONFIG_FREEZER
740 static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
741 struct kobj_attribute *attr, char *buf)
743 return sprintf(buf, "%u\n", freeze_timeout_msecs);
746 static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
747 struct kobj_attribute *attr,
748 const char *buf, size_t n)
752 if (kstrtoul(buf, 10, &val))
755 freeze_timeout_msecs = val;
759 power_attr(pm_freeze_timeout);
761 #endif /* CONFIG_FREEZER*/
763 static struct attribute * g[] = {
765 #ifdef CONFIG_PM_TRACE
767 &pm_trace_dev_match_attr.attr,
769 #ifdef CONFIG_PM_SLEEP
771 &wakeup_count_attr.attr,
772 #ifdef CONFIG_SUSPEND
773 &mem_sleep_attr.attr,
775 #ifdef CONFIG_PM_AUTOSLEEP
776 &autosleep_attr.attr,
778 #ifdef CONFIG_PM_WAKELOCKS
779 &wake_lock_attr.attr,
780 &wake_unlock_attr.attr,
782 #ifdef CONFIG_PM_SLEEP_DEBUG
784 &pm_print_times_attr.attr,
785 &pm_wakeup_irq_attr.attr,
786 &pm_debug_messages_attr.attr,
789 #ifdef CONFIG_FREEZER
790 &pm_freeze_timeout_attr.attr,
795 static const struct attribute_group attr_group = {
799 struct workqueue_struct *pm_wq;
800 EXPORT_SYMBOL_GPL(pm_wq);
802 static int __init pm_start_workqueue(void)
804 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
806 return pm_wq ? 0 : -ENOMEM;
809 static int __init pm_init(void)
811 int error = pm_start_workqueue();
814 hibernate_image_size_init();
815 hibernate_reserved_size_init();
817 power_kobj = kobject_create_and_add("power", NULL);
820 error = sysfs_create_group(power_kobj, &attr_group);
823 pm_print_times_init();
824 return pm_autosleep_init();
827 core_initcall(pm_init);