1 // SPDX-License-Identifier: GPL-2.0-only
3 * kernel/power/main.c - PM subsystem core functionality.
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
9 #include <linux/export.h>
10 #include <linux/kobject.h>
11 #include <linux/string.h>
12 #include <linux/pm-trace.h>
13 #include <linux/workqueue.h>
14 #include <linux/debugfs.h>
15 #include <linux/seq_file.h>
16 #include <linux/suspend.h>
17 #include <linux/syscalls.h>
21 #ifdef CONFIG_PM_SLEEP
23 void lock_system_sleep(void)
25 current->flags |= PF_FREEZER_SKIP;
26 mutex_lock(&system_transition_mutex);
28 EXPORT_SYMBOL_GPL(lock_system_sleep);
30 void unlock_system_sleep(void)
33 * Don't use freezer_count() because we don't want the call to
34 * try_to_freeze() here.
37 * Fundamentally, we just don't need it, because freezing condition
38 * doesn't come into effect until we release the
39 * system_transition_mutex lock, since the freezer always works with
40 * system_transition_mutex held.
42 * More importantly, in the case of hibernation,
43 * unlock_system_sleep() gets called in snapshot_read() and
44 * snapshot_write() when the freezing condition is still in effect.
45 * Which means, if we use try_to_freeze() here, it would make them
46 * enter the refrigerator, thus causing hibernation to lockup.
48 current->flags &= ~PF_FREEZER_SKIP;
49 mutex_unlock(&system_transition_mutex);
51 EXPORT_SYMBOL_GPL(unlock_system_sleep);
53 void ksys_sync_helper(void)
60 elapsed_msecs = ktime_to_ms(ktime_sub(ktime_get(), start));
61 pr_info("Filesystems sync: %ld.%03ld seconds\n",
62 elapsed_msecs / MSEC_PER_SEC, elapsed_msecs % MSEC_PER_SEC);
64 EXPORT_SYMBOL_GPL(ksys_sync_helper);
66 /* Routines for PM-transition notifications */
68 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
70 int register_pm_notifier(struct notifier_block *nb)
72 return blocking_notifier_chain_register(&pm_chain_head, nb);
74 EXPORT_SYMBOL_GPL(register_pm_notifier);
76 int unregister_pm_notifier(struct notifier_block *nb)
78 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
80 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
82 int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls)
86 ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL,
87 nr_to_call, nr_calls);
89 return notifier_to_errno(ret);
91 int pm_notifier_call_chain(unsigned long val)
93 return __pm_notifier_call_chain(val, -1, NULL);
96 /* If set, devices may be suspended and resumed asynchronously. */
97 int pm_async_enabled = 1;
99 static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
102 return sprintf(buf, "%d\n", pm_async_enabled);
105 static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
106 const char *buf, size_t n)
110 if (kstrtoul(buf, 10, &val))
116 pm_async_enabled = val;
120 power_attr(pm_async);
122 #ifdef CONFIG_SUSPEND
123 static ssize_t mem_sleep_show(struct kobject *kobj, struct kobj_attribute *attr,
129 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
130 if (mem_sleep_states[i]) {
131 const char *label = mem_sleep_states[i];
133 if (mem_sleep_current == i)
134 s += sprintf(s, "[%s] ", label);
136 s += sprintf(s, "%s ", label);
139 /* Convert the last space to a newline if needed. */
146 static suspend_state_t decode_suspend_state(const char *buf, size_t n)
148 suspend_state_t state;
152 p = memchr(buf, '\n', n);
153 len = p ? p - buf : n;
155 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
156 const char *label = mem_sleep_states[state];
158 if (label && len == strlen(label) && !strncmp(buf, label, len))
162 return PM_SUSPEND_ON;
165 static ssize_t mem_sleep_store(struct kobject *kobj, struct kobj_attribute *attr,
166 const char *buf, size_t n)
168 suspend_state_t state;
171 error = pm_autosleep_lock();
175 if (pm_autosleep_state() > PM_SUSPEND_ON) {
180 state = decode_suspend_state(buf, n);
181 if (state < PM_SUSPEND_MAX && state > PM_SUSPEND_ON)
182 mem_sleep_current = state;
187 pm_autosleep_unlock();
188 return error ? error : n;
191 power_attr(mem_sleep);
192 #endif /* CONFIG_SUSPEND */
194 #ifdef CONFIG_PM_SLEEP_DEBUG
195 int pm_test_level = TEST_NONE;
197 static const char * const pm_tests[__TEST_AFTER_LAST] = {
198 [TEST_NONE] = "none",
199 [TEST_CORE] = "core",
200 [TEST_CPUS] = "processors",
201 [TEST_PLATFORM] = "platform",
202 [TEST_DEVICES] = "devices",
203 [TEST_FREEZER] = "freezer",
206 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
212 for (level = TEST_FIRST; level <= TEST_MAX; level++)
213 if (pm_tests[level]) {
214 if (level == pm_test_level)
215 s += sprintf(s, "[%s] ", pm_tests[level]);
217 s += sprintf(s, "%s ", pm_tests[level]);
221 /* convert the last space to a newline */
227 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
228 const char *buf, size_t n)
230 const char * const *s;
236 p = memchr(buf, '\n', n);
237 len = p ? p - buf : n;
242 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
243 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
244 pm_test_level = level;
249 unlock_system_sleep();
251 return error ? error : n;
255 #endif /* CONFIG_PM_SLEEP_DEBUG */
257 #ifdef CONFIG_DEBUG_FS
258 static char *suspend_step_name(enum suspend_stat_step step)
263 case SUSPEND_PREPARE:
265 case SUSPEND_SUSPEND:
267 case SUSPEND_SUSPEND_NOIRQ:
268 return "suspend_noirq";
269 case SUSPEND_RESUME_NOIRQ:
270 return "resume_noirq";
278 static int suspend_stats_show(struct seq_file *s, void *unused)
280 int i, index, last_dev, last_errno, last_step;
282 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
283 last_dev %= REC_FAILED_NUM;
284 last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
285 last_errno %= REC_FAILED_NUM;
286 last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
287 last_step %= REC_FAILED_NUM;
288 seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
289 "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
290 "success", suspend_stats.success,
291 "fail", suspend_stats.fail,
292 "failed_freeze", suspend_stats.failed_freeze,
293 "failed_prepare", suspend_stats.failed_prepare,
294 "failed_suspend", suspend_stats.failed_suspend,
295 "failed_suspend_late",
296 suspend_stats.failed_suspend_late,
297 "failed_suspend_noirq",
298 suspend_stats.failed_suspend_noirq,
299 "failed_resume", suspend_stats.failed_resume,
300 "failed_resume_early",
301 suspend_stats.failed_resume_early,
302 "failed_resume_noirq",
303 suspend_stats.failed_resume_noirq);
304 seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
305 suspend_stats.failed_devs[last_dev]);
306 for (i = 1; i < REC_FAILED_NUM; i++) {
307 index = last_dev + REC_FAILED_NUM - i;
308 index %= REC_FAILED_NUM;
309 seq_printf(s, "\t\t\t%-s\n",
310 suspend_stats.failed_devs[index]);
312 seq_printf(s, " last_failed_errno:\t%-d\n",
313 suspend_stats.errno[last_errno]);
314 for (i = 1; i < REC_FAILED_NUM; i++) {
315 index = last_errno + REC_FAILED_NUM - i;
316 index %= REC_FAILED_NUM;
317 seq_printf(s, "\t\t\t%-d\n",
318 suspend_stats.errno[index]);
320 seq_printf(s, " last_failed_step:\t%-s\n",
322 suspend_stats.failed_steps[last_step]));
323 for (i = 1; i < REC_FAILED_NUM; i++) {
324 index = last_step + REC_FAILED_NUM - i;
325 index %= REC_FAILED_NUM;
326 seq_printf(s, "\t\t\t%-s\n",
328 suspend_stats.failed_steps[index]));
333 DEFINE_SHOW_ATTRIBUTE(suspend_stats);
335 static int __init pm_debugfs_init(void)
337 debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
338 NULL, NULL, &suspend_stats_fops);
342 late_initcall(pm_debugfs_init);
343 #endif /* CONFIG_DEBUG_FS */
345 #endif /* CONFIG_PM_SLEEP */
347 #ifdef CONFIG_PM_SLEEP_DEBUG
349 * pm_print_times: print time taken by devices to suspend and resume.
351 * show() returns whether printing of suspend and resume times is enabled.
352 * store() accepts 0 or 1. 0 disables printing and 1 enables it.
354 bool pm_print_times_enabled;
356 static ssize_t pm_print_times_show(struct kobject *kobj,
357 struct kobj_attribute *attr, char *buf)
359 return sprintf(buf, "%d\n", pm_print_times_enabled);
362 static ssize_t pm_print_times_store(struct kobject *kobj,
363 struct kobj_attribute *attr,
364 const char *buf, size_t n)
368 if (kstrtoul(buf, 10, &val))
374 pm_print_times_enabled = !!val;
378 power_attr(pm_print_times);
380 static inline void pm_print_times_init(void)
382 pm_print_times_enabled = !!initcall_debug;
385 static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
386 struct kobj_attribute *attr,
389 return pm_wakeup_irq ? sprintf(buf, "%u\n", pm_wakeup_irq) : -ENODATA;
392 power_attr_ro(pm_wakeup_irq);
394 bool pm_debug_messages_on __read_mostly;
396 static ssize_t pm_debug_messages_show(struct kobject *kobj,
397 struct kobj_attribute *attr, char *buf)
399 return sprintf(buf, "%d\n", pm_debug_messages_on);
402 static ssize_t pm_debug_messages_store(struct kobject *kobj,
403 struct kobj_attribute *attr,
404 const char *buf, size_t n)
408 if (kstrtoul(buf, 10, &val))
414 pm_debug_messages_on = !!val;
418 power_attr(pm_debug_messages);
421 * __pm_pr_dbg - Print a suspend debug message to the kernel log.
422 * @defer: Whether or not to use printk_deferred() to print the message.
423 * @fmt: Message format.
425 * The message will be emitted if enabled through the pm_debug_messages
428 void __pm_pr_dbg(bool defer, const char *fmt, ...)
430 struct va_format vaf;
433 if (!pm_debug_messages_on)
442 printk_deferred(KERN_DEBUG "PM: %pV", &vaf);
444 printk(KERN_DEBUG "PM: %pV", &vaf);
449 #else /* !CONFIG_PM_SLEEP_DEBUG */
450 static inline void pm_print_times_init(void) {}
451 #endif /* CONFIG_PM_SLEEP_DEBUG */
453 struct kobject *power_kobj;
456 * state - control system sleep states.
458 * show() returns available sleep state labels, which may be "mem", "standby",
459 * "freeze" and "disk" (hibernation).
460 * See Documentation/admin-guide/pm/sleep-states.rst for a description of
463 * store() accepts one of those strings, translates it into the proper
464 * enumerated value, and initiates a suspend transition.
466 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
470 #ifdef CONFIG_SUSPEND
473 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
475 s += sprintf(s,"%s ", pm_states[i]);
478 if (hibernation_available())
479 s += sprintf(s, "disk ");
481 /* convert the last space to a newline */
486 static suspend_state_t decode_state(const char *buf, size_t n)
488 #ifdef CONFIG_SUSPEND
489 suspend_state_t state;
494 p = memchr(buf, '\n', n);
495 len = p ? p - buf : n;
497 /* Check hibernation first. */
498 if (len == 4 && !strncmp(buf, "disk", len))
499 return PM_SUSPEND_MAX;
501 #ifdef CONFIG_SUSPEND
502 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
503 const char *label = pm_states[state];
505 if (label && len == strlen(label) && !strncmp(buf, label, len))
510 return PM_SUSPEND_ON;
513 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
514 const char *buf, size_t n)
516 suspend_state_t state;
519 error = pm_autosleep_lock();
523 if (pm_autosleep_state() > PM_SUSPEND_ON) {
528 state = decode_state(buf, n);
529 if (state < PM_SUSPEND_MAX) {
530 if (state == PM_SUSPEND_MEM)
531 state = mem_sleep_current;
533 error = pm_suspend(state);
534 } else if (state == PM_SUSPEND_MAX) {
541 pm_autosleep_unlock();
542 return error ? error : n;
547 #ifdef CONFIG_PM_SLEEP
549 * The 'wakeup_count' attribute, along with the functions defined in
550 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
551 * handled in a non-racy way.
553 * If a wakeup event occurs when the system is in a sleep state, it simply is
554 * woken up. In turn, if an event that would wake the system up from a sleep
555 * state occurs when it is undergoing a transition to that sleep state, the
556 * transition should be aborted. Moreover, if such an event occurs when the
557 * system is in the working state, an attempt to start a transition to the
558 * given sleep state should fail during certain period after the detection of
559 * the event. Using the 'state' attribute alone is not sufficient to satisfy
560 * these requirements, because a wakeup event may occur exactly when 'state'
561 * is being written to and may be delivered to user space right before it is
562 * frozen, so the event will remain only partially processed until the system is
563 * woken up by another event. In particular, it won't cause the transition to
564 * a sleep state to be aborted.
566 * This difficulty may be overcome if user space uses 'wakeup_count' before
567 * writing to 'state'. It first should read from 'wakeup_count' and store
568 * the read value. Then, after carrying out its own preparations for the system
569 * transition to a sleep state, it should write the stored value to
570 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
571 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
572 * is allowed to write to 'state', but the transition will be aborted if there
573 * are any wakeup events detected after 'wakeup_count' was written to.
576 static ssize_t wakeup_count_show(struct kobject *kobj,
577 struct kobj_attribute *attr,
582 return pm_get_wakeup_count(&val, true) ?
583 sprintf(buf, "%u\n", val) : -EINTR;
586 static ssize_t wakeup_count_store(struct kobject *kobj,
587 struct kobj_attribute *attr,
588 const char *buf, size_t n)
593 error = pm_autosleep_lock();
597 if (pm_autosleep_state() > PM_SUSPEND_ON) {
603 if (sscanf(buf, "%u", &val) == 1) {
604 if (pm_save_wakeup_count(val))
607 pm_print_active_wakeup_sources();
611 pm_autosleep_unlock();
615 power_attr(wakeup_count);
617 #ifdef CONFIG_PM_AUTOSLEEP
618 static ssize_t autosleep_show(struct kobject *kobj,
619 struct kobj_attribute *attr,
622 suspend_state_t state = pm_autosleep_state();
624 if (state == PM_SUSPEND_ON)
625 return sprintf(buf, "off\n");
627 #ifdef CONFIG_SUSPEND
628 if (state < PM_SUSPEND_MAX)
629 return sprintf(buf, "%s\n", pm_states[state] ?
630 pm_states[state] : "error");
632 #ifdef CONFIG_HIBERNATION
633 return sprintf(buf, "disk\n");
635 return sprintf(buf, "error");
639 static ssize_t autosleep_store(struct kobject *kobj,
640 struct kobj_attribute *attr,
641 const char *buf, size_t n)
643 suspend_state_t state = decode_state(buf, n);
646 if (state == PM_SUSPEND_ON
647 && strcmp(buf, "off") && strcmp(buf, "off\n"))
650 if (state == PM_SUSPEND_MEM)
651 state = mem_sleep_current;
653 error = pm_autosleep_set_state(state);
654 return error ? error : n;
657 power_attr(autosleep);
658 #endif /* CONFIG_PM_AUTOSLEEP */
660 #ifdef CONFIG_PM_WAKELOCKS
661 static ssize_t wake_lock_show(struct kobject *kobj,
662 struct kobj_attribute *attr,
665 return pm_show_wakelocks(buf, true);
668 static ssize_t wake_lock_store(struct kobject *kobj,
669 struct kobj_attribute *attr,
670 const char *buf, size_t n)
672 int error = pm_wake_lock(buf);
673 return error ? error : n;
676 power_attr(wake_lock);
678 static ssize_t wake_unlock_show(struct kobject *kobj,
679 struct kobj_attribute *attr,
682 return pm_show_wakelocks(buf, false);
685 static ssize_t wake_unlock_store(struct kobject *kobj,
686 struct kobj_attribute *attr,
687 const char *buf, size_t n)
689 int error = pm_wake_unlock(buf);
690 return error ? error : n;
693 power_attr(wake_unlock);
695 #endif /* CONFIG_PM_WAKELOCKS */
696 #endif /* CONFIG_PM_SLEEP */
698 #ifdef CONFIG_PM_TRACE
699 int pm_trace_enabled;
701 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
704 return sprintf(buf, "%d\n", pm_trace_enabled);
708 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
709 const char *buf, size_t n)
713 if (sscanf(buf, "%d", &val) == 1) {
714 pm_trace_enabled = !!val;
715 if (pm_trace_enabled) {
716 pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
717 "PM: Correct system time has to be restored manually after resume.\n");
724 power_attr(pm_trace);
726 static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
727 struct kobj_attribute *attr,
730 return show_trace_dev_match(buf, PAGE_SIZE);
733 power_attr_ro(pm_trace_dev_match);
735 #endif /* CONFIG_PM_TRACE */
737 #ifdef CONFIG_FREEZER
738 static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
739 struct kobj_attribute *attr, char *buf)
741 return sprintf(buf, "%u\n", freeze_timeout_msecs);
744 static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
745 struct kobj_attribute *attr,
746 const char *buf, size_t n)
750 if (kstrtoul(buf, 10, &val))
753 freeze_timeout_msecs = val;
757 power_attr(pm_freeze_timeout);
759 #endif /* CONFIG_FREEZER*/
761 static struct attribute * g[] = {
763 #ifdef CONFIG_PM_TRACE
765 &pm_trace_dev_match_attr.attr,
767 #ifdef CONFIG_PM_SLEEP
769 &wakeup_count_attr.attr,
770 #ifdef CONFIG_SUSPEND
771 &mem_sleep_attr.attr,
773 #ifdef CONFIG_PM_AUTOSLEEP
774 &autosleep_attr.attr,
776 #ifdef CONFIG_PM_WAKELOCKS
777 &wake_lock_attr.attr,
778 &wake_unlock_attr.attr,
780 #ifdef CONFIG_PM_SLEEP_DEBUG
782 &pm_print_times_attr.attr,
783 &pm_wakeup_irq_attr.attr,
784 &pm_debug_messages_attr.attr,
787 #ifdef CONFIG_FREEZER
788 &pm_freeze_timeout_attr.attr,
793 static const struct attribute_group attr_group = {
797 struct workqueue_struct *pm_wq;
798 EXPORT_SYMBOL_GPL(pm_wq);
800 static int __init pm_start_workqueue(void)
802 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
804 return pm_wq ? 0 : -ENOMEM;
807 static int __init pm_init(void)
809 int error = pm_start_workqueue();
812 hibernate_image_size_init();
813 hibernate_reserved_size_init();
815 power_kobj = kobject_create_and_add("power", NULL);
818 error = sysfs_create_group(power_kobj, &attr_group);
821 pm_print_times_init();
822 return pm_autosleep_init();
825 core_initcall(pm_init);