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 static char *suspend_step_name(enum suspend_stat_step step)
262 case SUSPEND_PREPARE:
264 case SUSPEND_SUSPEND:
266 case SUSPEND_SUSPEND_NOIRQ:
267 return "suspend_noirq";
268 case SUSPEND_RESUME_NOIRQ:
269 return "resume_noirq";
277 #define suspend_attr(_name) \
278 static ssize_t _name##_show(struct kobject *kobj, \
279 struct kobj_attribute *attr, char *buf) \
281 return sprintf(buf, "%d\n", suspend_stats._name); \
283 static struct kobj_attribute _name = __ATTR_RO(_name)
285 suspend_attr(success);
287 suspend_attr(failed_freeze);
288 suspend_attr(failed_prepare);
289 suspend_attr(failed_suspend);
290 suspend_attr(failed_suspend_late);
291 suspend_attr(failed_suspend_noirq);
292 suspend_attr(failed_resume);
293 suspend_attr(failed_resume_early);
294 suspend_attr(failed_resume_noirq);
296 static ssize_t last_failed_dev_show(struct kobject *kobj,
297 struct kobj_attribute *attr, char *buf)
300 char *last_failed_dev = NULL;
302 index = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
303 index %= REC_FAILED_NUM;
304 last_failed_dev = suspend_stats.failed_devs[index];
306 return sprintf(buf, "%s\n", last_failed_dev);
308 static struct kobj_attribute last_failed_dev = __ATTR_RO(last_failed_dev);
310 static ssize_t last_failed_errno_show(struct kobject *kobj,
311 struct kobj_attribute *attr, char *buf)
314 int last_failed_errno;
316 index = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
317 index %= REC_FAILED_NUM;
318 last_failed_errno = suspend_stats.errno[index];
320 return sprintf(buf, "%d\n", last_failed_errno);
322 static struct kobj_attribute last_failed_errno = __ATTR_RO(last_failed_errno);
324 static ssize_t last_failed_step_show(struct kobject *kobj,
325 struct kobj_attribute *attr, char *buf)
328 enum suspend_stat_step step;
329 char *last_failed_step = NULL;
331 index = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
332 index %= REC_FAILED_NUM;
333 step = suspend_stats.failed_steps[index];
334 last_failed_step = suspend_step_name(step);
336 return sprintf(buf, "%s\n", last_failed_step);
338 static struct kobj_attribute last_failed_step = __ATTR_RO(last_failed_step);
340 static struct attribute *suspend_attrs[] = {
344 &failed_prepare.attr,
345 &failed_suspend.attr,
346 &failed_suspend_late.attr,
347 &failed_suspend_noirq.attr,
349 &failed_resume_early.attr,
350 &failed_resume_noirq.attr,
351 &last_failed_dev.attr,
352 &last_failed_errno.attr,
353 &last_failed_step.attr,
357 static struct attribute_group suspend_attr_group = {
358 .name = "suspend_stats",
359 .attrs = suspend_attrs,
362 #ifdef CONFIG_DEBUG_FS
363 static int suspend_stats_show(struct seq_file *s, void *unused)
365 int i, index, last_dev, last_errno, last_step;
367 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
368 last_dev %= REC_FAILED_NUM;
369 last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
370 last_errno %= REC_FAILED_NUM;
371 last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
372 last_step %= REC_FAILED_NUM;
373 seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
374 "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
375 "success", suspend_stats.success,
376 "fail", suspend_stats.fail,
377 "failed_freeze", suspend_stats.failed_freeze,
378 "failed_prepare", suspend_stats.failed_prepare,
379 "failed_suspend", suspend_stats.failed_suspend,
380 "failed_suspend_late",
381 suspend_stats.failed_suspend_late,
382 "failed_suspend_noirq",
383 suspend_stats.failed_suspend_noirq,
384 "failed_resume", suspend_stats.failed_resume,
385 "failed_resume_early",
386 suspend_stats.failed_resume_early,
387 "failed_resume_noirq",
388 suspend_stats.failed_resume_noirq);
389 seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
390 suspend_stats.failed_devs[last_dev]);
391 for (i = 1; i < REC_FAILED_NUM; i++) {
392 index = last_dev + REC_FAILED_NUM - i;
393 index %= REC_FAILED_NUM;
394 seq_printf(s, "\t\t\t%-s\n",
395 suspend_stats.failed_devs[index]);
397 seq_printf(s, " last_failed_errno:\t%-d\n",
398 suspend_stats.errno[last_errno]);
399 for (i = 1; i < REC_FAILED_NUM; i++) {
400 index = last_errno + REC_FAILED_NUM - i;
401 index %= REC_FAILED_NUM;
402 seq_printf(s, "\t\t\t%-d\n",
403 suspend_stats.errno[index]);
405 seq_printf(s, " last_failed_step:\t%-s\n",
407 suspend_stats.failed_steps[last_step]));
408 for (i = 1; i < REC_FAILED_NUM; i++) {
409 index = last_step + REC_FAILED_NUM - i;
410 index %= REC_FAILED_NUM;
411 seq_printf(s, "\t\t\t%-s\n",
413 suspend_stats.failed_steps[index]));
418 DEFINE_SHOW_ATTRIBUTE(suspend_stats);
420 static int __init pm_debugfs_init(void)
422 debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
423 NULL, NULL, &suspend_stats_fops);
427 late_initcall(pm_debugfs_init);
428 #endif /* CONFIG_DEBUG_FS */
430 #endif /* CONFIG_PM_SLEEP */
432 #ifdef CONFIG_PM_SLEEP_DEBUG
434 * pm_print_times: print time taken by devices to suspend and resume.
436 * show() returns whether printing of suspend and resume times is enabled.
437 * store() accepts 0 or 1. 0 disables printing and 1 enables it.
439 bool pm_print_times_enabled;
441 static ssize_t pm_print_times_show(struct kobject *kobj,
442 struct kobj_attribute *attr, char *buf)
444 return sprintf(buf, "%d\n", pm_print_times_enabled);
447 static ssize_t pm_print_times_store(struct kobject *kobj,
448 struct kobj_attribute *attr,
449 const char *buf, size_t n)
453 if (kstrtoul(buf, 10, &val))
459 pm_print_times_enabled = !!val;
463 power_attr(pm_print_times);
465 static inline void pm_print_times_init(void)
467 pm_print_times_enabled = !!initcall_debug;
470 static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
471 struct kobj_attribute *attr,
474 return pm_wakeup_irq ? sprintf(buf, "%u\n", pm_wakeup_irq) : -ENODATA;
477 power_attr_ro(pm_wakeup_irq);
479 bool pm_debug_messages_on __read_mostly;
481 static ssize_t pm_debug_messages_show(struct kobject *kobj,
482 struct kobj_attribute *attr, char *buf)
484 return sprintf(buf, "%d\n", pm_debug_messages_on);
487 static ssize_t pm_debug_messages_store(struct kobject *kobj,
488 struct kobj_attribute *attr,
489 const char *buf, size_t n)
493 if (kstrtoul(buf, 10, &val))
499 pm_debug_messages_on = !!val;
503 power_attr(pm_debug_messages);
506 * __pm_pr_dbg - Print a suspend debug message to the kernel log.
507 * @defer: Whether or not to use printk_deferred() to print the message.
508 * @fmt: Message format.
510 * The message will be emitted if enabled through the pm_debug_messages
513 void __pm_pr_dbg(bool defer, const char *fmt, ...)
515 struct va_format vaf;
518 if (!pm_debug_messages_on)
527 printk_deferred(KERN_DEBUG "PM: %pV", &vaf);
529 printk(KERN_DEBUG "PM: %pV", &vaf);
534 #else /* !CONFIG_PM_SLEEP_DEBUG */
535 static inline void pm_print_times_init(void) {}
536 #endif /* CONFIG_PM_SLEEP_DEBUG */
538 struct kobject *power_kobj;
541 * state - control system sleep states.
543 * show() returns available sleep state labels, which may be "mem", "standby",
544 * "freeze" and "disk" (hibernation).
545 * See Documentation/admin-guide/pm/sleep-states.rst for a description of
548 * store() accepts one of those strings, translates it into the proper
549 * enumerated value, and initiates a suspend transition.
551 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
555 #ifdef CONFIG_SUSPEND
558 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
560 s += sprintf(s,"%s ", pm_states[i]);
563 if (hibernation_available())
564 s += sprintf(s, "disk ");
566 /* convert the last space to a newline */
571 static suspend_state_t decode_state(const char *buf, size_t n)
573 #ifdef CONFIG_SUSPEND
574 suspend_state_t state;
579 p = memchr(buf, '\n', n);
580 len = p ? p - buf : n;
582 /* Check hibernation first. */
583 if (len == 4 && str_has_prefix(buf, "disk"))
584 return PM_SUSPEND_MAX;
586 #ifdef CONFIG_SUSPEND
587 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
588 const char *label = pm_states[state];
590 if (label && len == strlen(label) && !strncmp(buf, label, len))
595 return PM_SUSPEND_ON;
598 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
599 const char *buf, size_t n)
601 suspend_state_t state;
604 error = pm_autosleep_lock();
608 if (pm_autosleep_state() > PM_SUSPEND_ON) {
613 state = decode_state(buf, n);
614 if (state < PM_SUSPEND_MAX) {
615 if (state == PM_SUSPEND_MEM)
616 state = mem_sleep_current;
618 error = pm_suspend(state);
619 } else if (state == PM_SUSPEND_MAX) {
626 pm_autosleep_unlock();
627 return error ? error : n;
632 #ifdef CONFIG_PM_SLEEP
634 * The 'wakeup_count' attribute, along with the functions defined in
635 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
636 * handled in a non-racy way.
638 * If a wakeup event occurs when the system is in a sleep state, it simply is
639 * woken up. In turn, if an event that would wake the system up from a sleep
640 * state occurs when it is undergoing a transition to that sleep state, the
641 * transition should be aborted. Moreover, if such an event occurs when the
642 * system is in the working state, an attempt to start a transition to the
643 * given sleep state should fail during certain period after the detection of
644 * the event. Using the 'state' attribute alone is not sufficient to satisfy
645 * these requirements, because a wakeup event may occur exactly when 'state'
646 * is being written to and may be delivered to user space right before it is
647 * frozen, so the event will remain only partially processed until the system is
648 * woken up by another event. In particular, it won't cause the transition to
649 * a sleep state to be aborted.
651 * This difficulty may be overcome if user space uses 'wakeup_count' before
652 * writing to 'state'. It first should read from 'wakeup_count' and store
653 * the read value. Then, after carrying out its own preparations for the system
654 * transition to a sleep state, it should write the stored value to
655 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
656 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
657 * is allowed to write to 'state', but the transition will be aborted if there
658 * are any wakeup events detected after 'wakeup_count' was written to.
661 static ssize_t wakeup_count_show(struct kobject *kobj,
662 struct kobj_attribute *attr,
667 return pm_get_wakeup_count(&val, true) ?
668 sprintf(buf, "%u\n", val) : -EINTR;
671 static ssize_t wakeup_count_store(struct kobject *kobj,
672 struct kobj_attribute *attr,
673 const char *buf, size_t n)
678 error = pm_autosleep_lock();
682 if (pm_autosleep_state() > PM_SUSPEND_ON) {
688 if (sscanf(buf, "%u", &val) == 1) {
689 if (pm_save_wakeup_count(val))
692 pm_print_active_wakeup_sources();
696 pm_autosleep_unlock();
700 power_attr(wakeup_count);
702 #ifdef CONFIG_PM_AUTOSLEEP
703 static ssize_t autosleep_show(struct kobject *kobj,
704 struct kobj_attribute *attr,
707 suspend_state_t state = pm_autosleep_state();
709 if (state == PM_SUSPEND_ON)
710 return sprintf(buf, "off\n");
712 #ifdef CONFIG_SUSPEND
713 if (state < PM_SUSPEND_MAX)
714 return sprintf(buf, "%s\n", pm_states[state] ?
715 pm_states[state] : "error");
717 #ifdef CONFIG_HIBERNATION
718 return sprintf(buf, "disk\n");
720 return sprintf(buf, "error");
724 static ssize_t autosleep_store(struct kobject *kobj,
725 struct kobj_attribute *attr,
726 const char *buf, size_t n)
728 suspend_state_t state = decode_state(buf, n);
731 if (state == PM_SUSPEND_ON
732 && strcmp(buf, "off") && strcmp(buf, "off\n"))
735 if (state == PM_SUSPEND_MEM)
736 state = mem_sleep_current;
738 error = pm_autosleep_set_state(state);
739 return error ? error : n;
742 power_attr(autosleep);
743 #endif /* CONFIG_PM_AUTOSLEEP */
745 #ifdef CONFIG_PM_WAKELOCKS
746 static ssize_t wake_lock_show(struct kobject *kobj,
747 struct kobj_attribute *attr,
750 return pm_show_wakelocks(buf, true);
753 static ssize_t wake_lock_store(struct kobject *kobj,
754 struct kobj_attribute *attr,
755 const char *buf, size_t n)
757 int error = pm_wake_lock(buf);
758 return error ? error : n;
761 power_attr(wake_lock);
763 static ssize_t wake_unlock_show(struct kobject *kobj,
764 struct kobj_attribute *attr,
767 return pm_show_wakelocks(buf, false);
770 static ssize_t wake_unlock_store(struct kobject *kobj,
771 struct kobj_attribute *attr,
772 const char *buf, size_t n)
774 int error = pm_wake_unlock(buf);
775 return error ? error : n;
778 power_attr(wake_unlock);
780 #endif /* CONFIG_PM_WAKELOCKS */
781 #endif /* CONFIG_PM_SLEEP */
783 #ifdef CONFIG_PM_TRACE
784 int pm_trace_enabled;
786 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
789 return sprintf(buf, "%d\n", pm_trace_enabled);
793 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
794 const char *buf, size_t n)
798 if (sscanf(buf, "%d", &val) == 1) {
799 pm_trace_enabled = !!val;
800 if (pm_trace_enabled) {
801 pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
802 "PM: Correct system time has to be restored manually after resume.\n");
809 power_attr(pm_trace);
811 static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
812 struct kobj_attribute *attr,
815 return show_trace_dev_match(buf, PAGE_SIZE);
818 power_attr_ro(pm_trace_dev_match);
820 #endif /* CONFIG_PM_TRACE */
822 #ifdef CONFIG_FREEZER
823 static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
824 struct kobj_attribute *attr, char *buf)
826 return sprintf(buf, "%u\n", freeze_timeout_msecs);
829 static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
830 struct kobj_attribute *attr,
831 const char *buf, size_t n)
835 if (kstrtoul(buf, 10, &val))
838 freeze_timeout_msecs = val;
842 power_attr(pm_freeze_timeout);
844 #endif /* CONFIG_FREEZER*/
846 static struct attribute * g[] = {
848 #ifdef CONFIG_PM_TRACE
850 &pm_trace_dev_match_attr.attr,
852 #ifdef CONFIG_PM_SLEEP
854 &wakeup_count_attr.attr,
855 #ifdef CONFIG_SUSPEND
856 &mem_sleep_attr.attr,
858 #ifdef CONFIG_PM_AUTOSLEEP
859 &autosleep_attr.attr,
861 #ifdef CONFIG_PM_WAKELOCKS
862 &wake_lock_attr.attr,
863 &wake_unlock_attr.attr,
865 #ifdef CONFIG_PM_SLEEP_DEBUG
867 &pm_print_times_attr.attr,
868 &pm_wakeup_irq_attr.attr,
869 &pm_debug_messages_attr.attr,
872 #ifdef CONFIG_FREEZER
873 &pm_freeze_timeout_attr.attr,
878 static const struct attribute_group attr_group = {
882 static const struct attribute_group *attr_groups[] = {
884 #ifdef CONFIG_PM_SLEEP
890 struct workqueue_struct *pm_wq;
891 EXPORT_SYMBOL_GPL(pm_wq);
893 static int __init pm_start_workqueue(void)
895 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
897 return pm_wq ? 0 : -ENOMEM;
900 static int __init pm_init(void)
902 int error = pm_start_workqueue();
905 hibernate_image_size_init();
906 hibernate_reserved_size_init();
908 power_kobj = kobject_create_and_add("power", NULL);
911 error = sysfs_create_groups(power_kobj, attr_groups);
914 pm_print_times_init();
915 return pm_autosleep_init();
918 core_initcall(pm_init);