1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * kernel/power/main.c - PM subsystem core functionality.
4 *
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
7 */
8
9#include <linux/acpi.h>
10#include <linux/export.h>
11#include <linux/init.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>
20#include <linux/pm_runtime.h>
21
22#include "power.h"
23
24#ifdef CONFIG_PM_SLEEP
25/*
26 * The following functions are used by the suspend/hibernate code to temporarily
27 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
28 * while devices are suspended. To avoid races with the suspend/hibernate code,
29 * they should always be called with system_transition_mutex held
30 * (gfp_allowed_mask also should only be modified with system_transition_mutex
31 * held, unless the suspend/hibernate code is guaranteed not to run in parallel
32 * with that modification).
33 */
34static gfp_t saved_gfp_mask;
35
36void pm_restore_gfp_mask(void)
37{
38 WARN_ON(!mutex_is_locked(&system_transition_mutex));
39 if (saved_gfp_mask) {
40 gfp_allowed_mask = saved_gfp_mask;
41 saved_gfp_mask = 0;
42 }
43}
44
45void pm_restrict_gfp_mask(void)
46{
47 WARN_ON(!mutex_is_locked(&system_transition_mutex));
48 WARN_ON(saved_gfp_mask);
49 saved_gfp_mask = gfp_allowed_mask;
50 gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
51}
52
53unsigned int lock_system_sleep(void)
54{
55 unsigned int flags = current->flags;
56 current->flags |= PF_NOFREEZE;
57 mutex_lock(lock: &system_transition_mutex);
58 return flags;
59}
60EXPORT_SYMBOL_GPL(lock_system_sleep);
61
62void unlock_system_sleep(unsigned int flags)
63{
64 if (!(flags & PF_NOFREEZE))
65 current->flags &= ~PF_NOFREEZE;
66 mutex_unlock(lock: &system_transition_mutex);
67}
68EXPORT_SYMBOL_GPL(unlock_system_sleep);
69
70void ksys_sync_helper(void)
71{
72 ktime_t start;
73 long elapsed_msecs;
74
75 start = ktime_get();
76 ksys_sync();
77 elapsed_msecs = ktime_to_ms(ktime_sub(ktime_get(), start));
78 pr_info("Filesystems sync: %ld.%03ld seconds\n",
79 elapsed_msecs / MSEC_PER_SEC, elapsed_msecs % MSEC_PER_SEC);
80}
81EXPORT_SYMBOL_GPL(ksys_sync_helper);
82
83/* Routines for PM-transition notifications */
84
85static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
86
87int register_pm_notifier(struct notifier_block *nb)
88{
89 return blocking_notifier_chain_register(nh: &pm_chain_head, nb);
90}
91EXPORT_SYMBOL_GPL(register_pm_notifier);
92
93int unregister_pm_notifier(struct notifier_block *nb)
94{
95 return blocking_notifier_chain_unregister(nh: &pm_chain_head, nb);
96}
97EXPORT_SYMBOL_GPL(unregister_pm_notifier);
98
99int pm_notifier_call_chain_robust(unsigned long val_up, unsigned long val_down)
100{
101 int ret;
102
103 ret = blocking_notifier_call_chain_robust(nh: &pm_chain_head, val_up, val_down, NULL);
104
105 return notifier_to_errno(ret);
106}
107
108int pm_notifier_call_chain(unsigned long val)
109{
110 return blocking_notifier_call_chain(nh: &pm_chain_head, val, NULL);
111}
112
113/* If set, devices may be suspended and resumed asynchronously. */
114int pm_async_enabled = 1;
115
116static int __init pm_async_setup(char *str)
117{
118 if (!strcmp(str, "off"))
119 pm_async_enabled = 0;
120 return 1;
121}
122__setup("pm_async=", pm_async_setup);
123
124static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
125 char *buf)
126{
127 return sysfs_emit(buf, fmt: "%d\n", pm_async_enabled);
128}
129
130static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
131 const char *buf, size_t n)
132{
133 unsigned long val;
134
135 if (kstrtoul(s: buf, base: 10, res: &val))
136 return -EINVAL;
137
138 if (val > 1)
139 return -EINVAL;
140
141 pm_async_enabled = val;
142 return n;
143}
144
145power_attr(pm_async);
146
147#ifdef CONFIG_SUSPEND
148static ssize_t mem_sleep_show(struct kobject *kobj, struct kobj_attribute *attr,
149 char *buf)
150{
151 ssize_t count = 0;
152 suspend_state_t i;
153
154 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++) {
155 if (i >= PM_SUSPEND_MEM && cxl_mem_active())
156 continue;
157 if (mem_sleep_states[i]) {
158 const char *label = mem_sleep_states[i];
159
160 if (mem_sleep_current == i)
161 count += sysfs_emit_at(buf, at: count, fmt: "[%s] ", label);
162 else
163 count += sysfs_emit_at(buf, at: count, fmt: "%s ", label);
164 }
165 }
166
167 /* Convert the last space to a newline if needed. */
168 if (count > 0)
169 buf[count - 1] = '\n';
170
171 return count;
172}
173
174static suspend_state_t decode_suspend_state(const char *buf, size_t n)
175{
176 suspend_state_t state;
177 char *p;
178 int len;
179
180 p = memchr(buf, '\n', n);
181 len = p ? p - buf : n;
182
183 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
184 const char *label = mem_sleep_states[state];
185
186 if (label && len == strlen(label) && !strncmp(buf, label, len))
187 return state;
188 }
189
190 return PM_SUSPEND_ON;
191}
192
193static ssize_t mem_sleep_store(struct kobject *kobj, struct kobj_attribute *attr,
194 const char *buf, size_t n)
195{
196 suspend_state_t state;
197 int error;
198
199 error = pm_autosleep_lock();
200 if (error)
201 return error;
202
203 if (pm_autosleep_state() > PM_SUSPEND_ON) {
204 error = -EBUSY;
205 goto out;
206 }
207
208 state = decode_suspend_state(buf, n);
209 if (state < PM_SUSPEND_MAX && state > PM_SUSPEND_ON)
210 mem_sleep_current = state;
211 else
212 error = -EINVAL;
213
214 out:
215 pm_autosleep_unlock();
216 return error ? error : n;
217}
218
219power_attr(mem_sleep);
220
221/*
222 * sync_on_suspend: invoke ksys_sync_helper() before suspend.
223 *
224 * show() returns whether ksys_sync_helper() is invoked before suspend.
225 * store() accepts 0 or 1. 0 disables ksys_sync_helper() and 1 enables it.
226 */
227bool sync_on_suspend_enabled = !IS_ENABLED(CONFIG_SUSPEND_SKIP_SYNC);
228
229static ssize_t sync_on_suspend_show(struct kobject *kobj,
230 struct kobj_attribute *attr, char *buf)
231{
232 return sysfs_emit(buf, fmt: "%d\n", sync_on_suspend_enabled);
233}
234
235static ssize_t sync_on_suspend_store(struct kobject *kobj,
236 struct kobj_attribute *attr,
237 const char *buf, size_t n)
238{
239 unsigned long val;
240
241 if (kstrtoul(s: buf, base: 10, res: &val))
242 return -EINVAL;
243
244 if (val > 1)
245 return -EINVAL;
246
247 sync_on_suspend_enabled = !!val;
248 return n;
249}
250
251power_attr(sync_on_suspend);
252#endif /* CONFIG_SUSPEND */
253
254#ifdef CONFIG_PM_SLEEP_DEBUG
255int pm_test_level = TEST_NONE;
256
257static const char * const pm_tests[__TEST_AFTER_LAST] = {
258 [TEST_NONE] = "none",
259 [TEST_CORE] = "core",
260 [TEST_CPUS] = "processors",
261 [TEST_PLATFORM] = "platform",
262 [TEST_DEVICES] = "devices",
263 [TEST_FREEZER] = "freezer",
264};
265
266static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
267 char *buf)
268{
269 ssize_t count = 0;
270 int level;
271
272 for (level = TEST_FIRST; level <= TEST_MAX; level++)
273 if (pm_tests[level]) {
274 if (level == pm_test_level)
275 count += sysfs_emit_at(buf, at: count, fmt: "[%s] ", pm_tests[level]);
276 else
277 count += sysfs_emit_at(buf, at: count, fmt: "%s ", pm_tests[level]);
278 }
279
280 /* Convert the last space to a newline if needed. */
281 if (count > 0)
282 buf[count - 1] = '\n';
283
284 return count;
285}
286
287static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
288 const char *buf, size_t n)
289{
290 unsigned int sleep_flags;
291 const char * const *s;
292 int error = -EINVAL;
293 int level;
294 char *p;
295 int len;
296
297 p = memchr(buf, '\n', n);
298 len = p ? p - buf : n;
299
300 sleep_flags = lock_system_sleep();
301
302 level = TEST_FIRST;
303 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
304 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
305 pm_test_level = level;
306 error = 0;
307 break;
308 }
309
310 unlock_system_sleep(sleep_flags);
311
312 return error ? error : n;
313}
314
315power_attr(pm_test);
316#endif /* CONFIG_PM_SLEEP_DEBUG */
317
318#define SUSPEND_NR_STEPS SUSPEND_RESUME
319#define REC_FAILED_NUM 2
320
321struct suspend_stats {
322 unsigned int step_failures[SUSPEND_NR_STEPS];
323 unsigned int success;
324 unsigned int fail;
325 int last_failed_dev;
326 char failed_devs[REC_FAILED_NUM][40];
327 int last_failed_errno;
328 int errno[REC_FAILED_NUM];
329 int last_failed_step;
330 u64 last_hw_sleep;
331 u64 total_hw_sleep;
332 u64 max_hw_sleep;
333 enum suspend_stat_step failed_steps[REC_FAILED_NUM];
334};
335
336static struct suspend_stats suspend_stats;
337static DEFINE_MUTEX(suspend_stats_lock);
338
339void dpm_save_failed_dev(const char *name)
340{
341 mutex_lock(lock: &suspend_stats_lock);
342
343 strscpy(suspend_stats.failed_devs[suspend_stats.last_failed_dev],
344 name, sizeof(suspend_stats.failed_devs[0]));
345 suspend_stats.last_failed_dev++;
346 suspend_stats.last_failed_dev %= REC_FAILED_NUM;
347
348 mutex_unlock(lock: &suspend_stats_lock);
349}
350
351void dpm_save_failed_step(enum suspend_stat_step step)
352{
353 suspend_stats.step_failures[step-1]++;
354 suspend_stats.failed_steps[suspend_stats.last_failed_step] = step;
355 suspend_stats.last_failed_step++;
356 suspend_stats.last_failed_step %= REC_FAILED_NUM;
357}
358
359void dpm_save_errno(int err)
360{
361 if (!err) {
362 suspend_stats.success++;
363 return;
364 }
365
366 suspend_stats.fail++;
367
368 suspend_stats.errno[suspend_stats.last_failed_errno] = err;
369 suspend_stats.last_failed_errno++;
370 suspend_stats.last_failed_errno %= REC_FAILED_NUM;
371}
372
373void pm_report_hw_sleep_time(u64 t)
374{
375 suspend_stats.last_hw_sleep = t;
376 suspend_stats.total_hw_sleep += t;
377}
378EXPORT_SYMBOL_GPL(pm_report_hw_sleep_time);
379
380void pm_report_max_hw_sleep(u64 t)
381{
382 suspend_stats.max_hw_sleep = t;
383}
384EXPORT_SYMBOL_GPL(pm_report_max_hw_sleep);
385
386static const char * const suspend_step_names[] = {
387 [SUSPEND_WORKING] = "",
388 [SUSPEND_FREEZE] = "freeze",
389 [SUSPEND_PREPARE] = "prepare",
390 [SUSPEND_SUSPEND] = "suspend",
391 [SUSPEND_SUSPEND_LATE] = "suspend_late",
392 [SUSPEND_SUSPEND_NOIRQ] = "suspend_noirq",
393 [SUSPEND_RESUME_NOIRQ] = "resume_noirq",
394 [SUSPEND_RESUME_EARLY] = "resume_early",
395 [SUSPEND_RESUME] = "resume",
396};
397
398#define suspend_attr(_name, format_str) \
399static ssize_t _name##_show(struct kobject *kobj, \
400 struct kobj_attribute *attr, char *buf) \
401{ \
402 return sysfs_emit(buf, format_str, suspend_stats._name);\
403} \
404static struct kobj_attribute _name = __ATTR_RO(_name)
405
406suspend_attr(success, "%u\n");
407suspend_attr(fail, "%u\n");
408suspend_attr(last_hw_sleep, "%llu\n");
409suspend_attr(total_hw_sleep, "%llu\n");
410suspend_attr(max_hw_sleep, "%llu\n");
411
412#define suspend_step_attr(_name, step) \
413static ssize_t _name##_show(struct kobject *kobj, \
414 struct kobj_attribute *attr, char *buf) \
415{ \
416 return sysfs_emit(buf, "%u\n", \
417 suspend_stats.step_failures[step-1]); \
418} \
419static struct kobj_attribute _name = __ATTR_RO(_name)
420
421suspend_step_attr(failed_freeze, SUSPEND_FREEZE);
422suspend_step_attr(failed_prepare, SUSPEND_PREPARE);
423suspend_step_attr(failed_suspend, SUSPEND_SUSPEND);
424suspend_step_attr(failed_suspend_late, SUSPEND_SUSPEND_LATE);
425suspend_step_attr(failed_suspend_noirq, SUSPEND_SUSPEND_NOIRQ);
426suspend_step_attr(failed_resume, SUSPEND_RESUME);
427suspend_step_attr(failed_resume_early, SUSPEND_RESUME_EARLY);
428suspend_step_attr(failed_resume_noirq, SUSPEND_RESUME_NOIRQ);
429
430static ssize_t last_failed_dev_show(struct kobject *kobj,
431 struct kobj_attribute *attr, char *buf)
432{
433 int index;
434 char *last_failed_dev = NULL;
435
436 index = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
437 index %= REC_FAILED_NUM;
438 last_failed_dev = suspend_stats.failed_devs[index];
439
440 return sysfs_emit(buf, fmt: "%s\n", last_failed_dev);
441}
442static struct kobj_attribute last_failed_dev = __ATTR_RO(last_failed_dev);
443
444static ssize_t last_failed_errno_show(struct kobject *kobj,
445 struct kobj_attribute *attr, char *buf)
446{
447 int index;
448 int last_failed_errno;
449
450 index = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
451 index %= REC_FAILED_NUM;
452 last_failed_errno = suspend_stats.errno[index];
453
454 return sysfs_emit(buf, fmt: "%d\n", last_failed_errno);
455}
456static struct kobj_attribute last_failed_errno = __ATTR_RO(last_failed_errno);
457
458static ssize_t last_failed_step_show(struct kobject *kobj,
459 struct kobj_attribute *attr, char *buf)
460{
461 enum suspend_stat_step step;
462 int index;
463
464 index = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
465 index %= REC_FAILED_NUM;
466 step = suspend_stats.failed_steps[index];
467
468 return sysfs_emit(buf, fmt: "%s\n", suspend_step_names[step]);
469}
470static struct kobj_attribute last_failed_step = __ATTR_RO(last_failed_step);
471
472static struct attribute *suspend_attrs[] = {
473 &success.attr,
474 &fail.attr,
475 &failed_freeze.attr,
476 &failed_prepare.attr,
477 &failed_suspend.attr,
478 &failed_suspend_late.attr,
479 &failed_suspend_noirq.attr,
480 &failed_resume.attr,
481 &failed_resume_early.attr,
482 &failed_resume_noirq.attr,
483 &last_failed_dev.attr,
484 &last_failed_errno.attr,
485 &last_failed_step.attr,
486 &last_hw_sleep.attr,
487 &total_hw_sleep.attr,
488 &max_hw_sleep.attr,
489 NULL,
490};
491
492static umode_t suspend_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
493{
494 if (attr != &last_hw_sleep.attr &&
495 attr != &total_hw_sleep.attr &&
496 attr != &max_hw_sleep.attr)
497 return 0444;
498
499#ifdef CONFIG_ACPI
500 if (acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
501 return 0444;
502#endif
503 return 0;
504}
505
506static const struct attribute_group suspend_attr_group = {
507 .name = "suspend_stats",
508 .attrs = suspend_attrs,
509 .is_visible = suspend_attr_is_visible,
510};
511
512#ifdef CONFIG_DEBUG_FS
513static int suspend_stats_show(struct seq_file *s, void *unused)
514{
515 int i, index, last_dev, last_errno, last_step;
516 enum suspend_stat_step step;
517
518 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
519 last_dev %= REC_FAILED_NUM;
520 last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
521 last_errno %= REC_FAILED_NUM;
522 last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
523 last_step %= REC_FAILED_NUM;
524
525 seq_printf(m: s, fmt: "success: %u\nfail: %u\n",
526 suspend_stats.success, suspend_stats.fail);
527
528 for (step = SUSPEND_FREEZE; step <= SUSPEND_NR_STEPS; step++)
529 seq_printf(m: s, fmt: "failed_%s: %u\n", suspend_step_names[step],
530 suspend_stats.step_failures[step-1]);
531
532 seq_printf(m: s, fmt: "failures:\n last_failed_dev:\t%-s\n",
533 suspend_stats.failed_devs[last_dev]);
534 for (i = 1; i < REC_FAILED_NUM; i++) {
535 index = last_dev + REC_FAILED_NUM - i;
536 index %= REC_FAILED_NUM;
537 seq_printf(m: s, fmt: "\t\t\t%-s\n", suspend_stats.failed_devs[index]);
538 }
539 seq_printf(m: s, fmt: " last_failed_errno:\t%-d\n",
540 suspend_stats.errno[last_errno]);
541 for (i = 1; i < REC_FAILED_NUM; i++) {
542 index = last_errno + REC_FAILED_NUM - i;
543 index %= REC_FAILED_NUM;
544 seq_printf(m: s, fmt: "\t\t\t%-d\n", suspend_stats.errno[index]);
545 }
546 seq_printf(m: s, fmt: " last_failed_step:\t%-s\n",
547 suspend_step_names[suspend_stats.failed_steps[last_step]]);
548 for (i = 1; i < REC_FAILED_NUM; i++) {
549 index = last_step + REC_FAILED_NUM - i;
550 index %= REC_FAILED_NUM;
551 seq_printf(m: s, fmt: "\t\t\t%-s\n",
552 suspend_step_names[suspend_stats.failed_steps[index]]);
553 }
554
555 return 0;
556}
557DEFINE_SHOW_ATTRIBUTE(suspend_stats);
558
559static int __init pm_debugfs_init(void)
560{
561 debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
562 NULL, NULL, &suspend_stats_fops);
563 return 0;
564}
565
566late_initcall(pm_debugfs_init);
567#endif /* CONFIG_DEBUG_FS */
568
569bool pm_sleep_transition_in_progress(void)
570{
571 return pm_suspend_in_progress() || hibernation_in_progress();
572}
573#endif /* CONFIG_PM_SLEEP */
574
575#ifdef CONFIG_PM_SLEEP_DEBUG
576/*
577 * pm_print_times: print time taken by devices to suspend and resume.
578 *
579 * show() returns whether printing of suspend and resume times is enabled.
580 * store() accepts 0 or 1. 0 disables printing and 1 enables it.
581 */
582bool pm_print_times_enabled;
583
584static ssize_t pm_print_times_show(struct kobject *kobj,
585 struct kobj_attribute *attr, char *buf)
586{
587 return sysfs_emit(buf, fmt: "%d\n", pm_print_times_enabled);
588}
589
590static ssize_t pm_print_times_store(struct kobject *kobj,
591 struct kobj_attribute *attr,
592 const char *buf, size_t n)
593{
594 unsigned long val;
595
596 if (kstrtoul(s: buf, base: 10, res: &val))
597 return -EINVAL;
598
599 if (val > 1)
600 return -EINVAL;
601
602 pm_print_times_enabled = !!val;
603 return n;
604}
605
606power_attr(pm_print_times);
607
608static inline void pm_print_times_init(void)
609{
610 pm_print_times_enabled = initcall_debug;
611}
612
613static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
614 struct kobj_attribute *attr,
615 char *buf)
616{
617 if (!pm_wakeup_irq())
618 return -ENODATA;
619
620 return sysfs_emit(buf, fmt: "%u\n", pm_wakeup_irq());
621}
622
623power_attr_ro(pm_wakeup_irq);
624
625bool pm_debug_messages_on __read_mostly;
626
627bool pm_debug_messages_should_print(void)
628{
629 return pm_debug_messages_on && pm_sleep_transition_in_progress();
630}
631EXPORT_SYMBOL_GPL(pm_debug_messages_should_print);
632
633static ssize_t pm_debug_messages_show(struct kobject *kobj,
634 struct kobj_attribute *attr, char *buf)
635{
636 return sysfs_emit(buf, fmt: "%d\n", pm_debug_messages_on);
637}
638
639static ssize_t pm_debug_messages_store(struct kobject *kobj,
640 struct kobj_attribute *attr,
641 const char *buf, size_t n)
642{
643 unsigned long val;
644
645 if (kstrtoul(s: buf, base: 10, res: &val))
646 return -EINVAL;
647
648 if (val > 1)
649 return -EINVAL;
650
651 pm_debug_messages_on = !!val;
652 return n;
653}
654
655power_attr(pm_debug_messages);
656
657static int __init pm_debug_messages_setup(char *str)
658{
659 pm_debug_messages_on = true;
660 return 1;
661}
662__setup("pm_debug_messages", pm_debug_messages_setup);
663
664#else /* !CONFIG_PM_SLEEP_DEBUG */
665static inline void pm_print_times_init(void) {}
666#endif /* CONFIG_PM_SLEEP_DEBUG */
667
668struct kobject *power_kobj;
669
670/*
671 * state - control system sleep states.
672 *
673 * show() returns available sleep state labels, which may be "mem", "standby",
674 * "freeze" and "disk" (hibernation).
675 * See Documentation/admin-guide/pm/sleep-states.rst for a description of
676 * what they mean.
677 *
678 * store() accepts one of those strings, translates it into the proper
679 * enumerated value, and initiates a suspend transition.
680 */
681static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
682 char *buf)
683{
684 ssize_t count = 0;
685#ifdef CONFIG_SUSPEND
686 suspend_state_t i;
687
688 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
689 if (pm_states[i])
690 count += sysfs_emit_at(buf, at: count, fmt: "%s ", pm_states[i]);
691
692#endif
693 if (hibernation_available())
694 count += sysfs_emit_at(buf, at: count, fmt: "disk ");
695
696 /* Convert the last space to a newline if needed. */
697 if (count > 0)
698 buf[count - 1] = '\n';
699
700 return count;
701}
702
703static suspend_state_t decode_state(const char *buf, size_t n)
704{
705#ifdef CONFIG_SUSPEND
706 suspend_state_t state;
707#endif
708 char *p;
709 int len;
710
711 p = memchr(buf, '\n', n);
712 len = p ? p - buf : n;
713
714 /* Check hibernation first. */
715 if (len == 4 && str_has_prefix(str: buf, prefix: "disk"))
716 return PM_SUSPEND_MAX;
717
718#ifdef CONFIG_SUSPEND
719 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
720 const char *label = pm_states[state];
721
722 if (label && len == strlen(label) && !strncmp(buf, label, len))
723 return state;
724 }
725#endif
726
727 return PM_SUSPEND_ON;
728}
729
730static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
731 const char *buf, size_t n)
732{
733 suspend_state_t state;
734 int error;
735
736 error = pm_autosleep_lock();
737 if (error)
738 return error;
739
740 if (pm_autosleep_state() > PM_SUSPEND_ON) {
741 error = -EBUSY;
742 goto out;
743 }
744
745 state = decode_state(buf, n);
746 if (state < PM_SUSPEND_MAX) {
747 if (state == PM_SUSPEND_MEM)
748 state = mem_sleep_current;
749
750 error = pm_suspend(state);
751 } else if (state == PM_SUSPEND_MAX) {
752 error = hibernate();
753 } else {
754 error = -EINVAL;
755 }
756
757 out:
758 pm_autosleep_unlock();
759 return error ? error : n;
760}
761
762power_attr(state);
763
764#ifdef CONFIG_PM_SLEEP
765/*
766 * The 'wakeup_count' attribute, along with the functions defined in
767 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
768 * handled in a non-racy way.
769 *
770 * If a wakeup event occurs when the system is in a sleep state, it simply is
771 * woken up. In turn, if an event that would wake the system up from a sleep
772 * state occurs when it is undergoing a transition to that sleep state, the
773 * transition should be aborted. Moreover, if such an event occurs when the
774 * system is in the working state, an attempt to start a transition to the
775 * given sleep state should fail during certain period after the detection of
776 * the event. Using the 'state' attribute alone is not sufficient to satisfy
777 * these requirements, because a wakeup event may occur exactly when 'state'
778 * is being written to and may be delivered to user space right before it is
779 * frozen, so the event will remain only partially processed until the system is
780 * woken up by another event. In particular, it won't cause the transition to
781 * a sleep state to be aborted.
782 *
783 * This difficulty may be overcome if user space uses 'wakeup_count' before
784 * writing to 'state'. It first should read from 'wakeup_count' and store
785 * the read value. Then, after carrying out its own preparations for the system
786 * transition to a sleep state, it should write the stored value to
787 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
788 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
789 * is allowed to write to 'state', but the transition will be aborted if there
790 * are any wakeup events detected after 'wakeup_count' was written to.
791 */
792
793static ssize_t wakeup_count_show(struct kobject *kobj,
794 struct kobj_attribute *attr,
795 char *buf)
796{
797 unsigned int val;
798
799 return pm_get_wakeup_count(count: &val, block: true) ?
800 sysfs_emit(buf, fmt: "%u\n", val) : -EINTR;
801}
802
803static ssize_t wakeup_count_store(struct kobject *kobj,
804 struct kobj_attribute *attr,
805 const char *buf, size_t n)
806{
807 unsigned int val;
808 int error;
809
810 error = pm_autosleep_lock();
811 if (error)
812 return error;
813
814 if (pm_autosleep_state() > PM_SUSPEND_ON) {
815 error = -EBUSY;
816 goto out;
817 }
818
819 error = -EINVAL;
820 if (sscanf(buf, "%u", &val) == 1) {
821 if (pm_save_wakeup_count(count: val))
822 error = n;
823 else
824 pm_print_active_wakeup_sources();
825 }
826
827 out:
828 pm_autosleep_unlock();
829 return error;
830}
831
832power_attr(wakeup_count);
833
834#ifdef CONFIG_PM_AUTOSLEEP
835static ssize_t autosleep_show(struct kobject *kobj,
836 struct kobj_attribute *attr,
837 char *buf)
838{
839 suspend_state_t state = pm_autosleep_state();
840
841 if (state == PM_SUSPEND_ON)
842 return sysfs_emit(buf, "off\n");
843
844#ifdef CONFIG_SUSPEND
845 if (state < PM_SUSPEND_MAX)
846 return sysfs_emit(buf, "%s\n", pm_states[state] ?
847 pm_states[state] : "error");
848#endif
849#ifdef CONFIG_HIBERNATION
850 return sysfs_emit(buf, "disk\n");
851#else
852 return sysfs_emit(buf, "error\n");
853#endif
854}
855
856static ssize_t autosleep_store(struct kobject *kobj,
857 struct kobj_attribute *attr,
858 const char *buf, size_t n)
859{
860 suspend_state_t state = decode_state(buf, n);
861 int error;
862
863 if (state == PM_SUSPEND_ON
864 && strcmp(buf, "off") && strcmp(buf, "off\n"))
865 return -EINVAL;
866
867 if (state == PM_SUSPEND_MEM)
868 state = mem_sleep_current;
869
870 error = pm_autosleep_set_state(state);
871 return error ? error : n;
872}
873
874power_attr(autosleep);
875#endif /* CONFIG_PM_AUTOSLEEP */
876
877#ifdef CONFIG_PM_WAKELOCKS
878static ssize_t wake_lock_show(struct kobject *kobj,
879 struct kobj_attribute *attr,
880 char *buf)
881{
882 return pm_show_wakelocks(buf, true);
883}
884
885static ssize_t wake_lock_store(struct kobject *kobj,
886 struct kobj_attribute *attr,
887 const char *buf, size_t n)
888{
889 int error = pm_wake_lock(buf);
890 return error ? error : n;
891}
892
893power_attr(wake_lock);
894
895static ssize_t wake_unlock_show(struct kobject *kobj,
896 struct kobj_attribute *attr,
897 char *buf)
898{
899 return pm_show_wakelocks(buf, false);
900}
901
902static ssize_t wake_unlock_store(struct kobject *kobj,
903 struct kobj_attribute *attr,
904 const char *buf, size_t n)
905{
906 int error = pm_wake_unlock(buf);
907 return error ? error : n;
908}
909
910power_attr(wake_unlock);
911
912#endif /* CONFIG_PM_WAKELOCKS */
913#endif /* CONFIG_PM_SLEEP */
914
915#ifdef CONFIG_PM_TRACE
916int pm_trace_enabled;
917
918static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
919 char *buf)
920{
921 return sysfs_emit(buf, fmt: "%d\n", pm_trace_enabled);
922}
923
924static ssize_t
925pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
926 const char *buf, size_t n)
927{
928 int val;
929
930 if (sscanf(buf, "%d", &val) == 1) {
931 pm_trace_enabled = !!val;
932 if (pm_trace_enabled) {
933 pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
934 "PM: Correct system time has to be restored manually after resume.\n");
935 }
936 return n;
937 }
938 return -EINVAL;
939}
940
941power_attr(pm_trace);
942
943static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
944 struct kobj_attribute *attr,
945 char *buf)
946{
947 return show_trace_dev_match(buf, PAGE_SIZE);
948}
949
950power_attr_ro(pm_trace_dev_match);
951
952#endif /* CONFIG_PM_TRACE */
953
954#ifdef CONFIG_FREEZER
955static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
956 struct kobj_attribute *attr, char *buf)
957{
958 return sysfs_emit(buf, fmt: "%u\n", freeze_timeout_msecs);
959}
960
961static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
962 struct kobj_attribute *attr,
963 const char *buf, size_t n)
964{
965 unsigned long val;
966
967 if (kstrtoul(s: buf, base: 10, res: &val))
968 return -EINVAL;
969
970 freeze_timeout_msecs = val;
971 return n;
972}
973
974power_attr(pm_freeze_timeout);
975
976#endif /* CONFIG_FREEZER*/
977
978#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
979bool filesystem_freeze_enabled = false;
980
981static ssize_t freeze_filesystems_show(struct kobject *kobj,
982 struct kobj_attribute *attr, char *buf)
983{
984 return sysfs_emit(buf, fmt: "%d\n", filesystem_freeze_enabled);
985}
986
987static ssize_t freeze_filesystems_store(struct kobject *kobj,
988 struct kobj_attribute *attr,
989 const char *buf, size_t n)
990{
991 unsigned long val;
992
993 if (kstrtoul(s: buf, base: 10, res: &val))
994 return -EINVAL;
995
996 if (val > 1)
997 return -EINVAL;
998
999 filesystem_freeze_enabled = !!val;
1000 return n;
1001}
1002
1003power_attr(freeze_filesystems);
1004#endif /* CONFIG_SUSPEND || CONFIG_HIBERNATION */
1005
1006static struct attribute * g[] = {
1007 &state_attr.attr,
1008#ifdef CONFIG_PM_TRACE
1009 &pm_trace_attr.attr,
1010 &pm_trace_dev_match_attr.attr,
1011#endif
1012#ifdef CONFIG_PM_SLEEP
1013 &pm_async_attr.attr,
1014 &wakeup_count_attr.attr,
1015#ifdef CONFIG_SUSPEND
1016 &mem_sleep_attr.attr,
1017 &sync_on_suspend_attr.attr,
1018#endif
1019#ifdef CONFIG_PM_AUTOSLEEP
1020 &autosleep_attr.attr,
1021#endif
1022#ifdef CONFIG_PM_WAKELOCKS
1023 &wake_lock_attr.attr,
1024 &wake_unlock_attr.attr,
1025#endif
1026#ifdef CONFIG_PM_SLEEP_DEBUG
1027 &pm_test_attr.attr,
1028 &pm_print_times_attr.attr,
1029 &pm_wakeup_irq_attr.attr,
1030 &pm_debug_messages_attr.attr,
1031#endif
1032#endif
1033#ifdef CONFIG_FREEZER
1034 &pm_freeze_timeout_attr.attr,
1035#endif
1036#if defined(CONFIG_SUSPEND) || defined(CONFIG_HIBERNATION)
1037 &freeze_filesystems_attr.attr,
1038#endif
1039 NULL,
1040};
1041
1042static const struct attribute_group attr_group = {
1043 .attrs = g,
1044};
1045
1046static const struct attribute_group *attr_groups[] = {
1047 &attr_group,
1048#ifdef CONFIG_PM_SLEEP
1049 &suspend_attr_group,
1050#endif
1051 NULL,
1052};
1053
1054struct workqueue_struct *pm_wq;
1055EXPORT_SYMBOL_GPL(pm_wq);
1056
1057static int __init pm_start_workqueue(void)
1058{
1059 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
1060
1061 return pm_wq ? 0 : -ENOMEM;
1062}
1063
1064static int __init pm_init(void)
1065{
1066 int error = pm_start_workqueue();
1067 if (error)
1068 return error;
1069 hibernate_image_size_init();
1070 hibernate_reserved_size_init();
1071 pm_states_init();
1072 power_kobj = kobject_create_and_add(name: "power", NULL);
1073 if (!power_kobj)
1074 return -ENOMEM;
1075 error = sysfs_create_groups(kobj: power_kobj, groups: attr_groups);
1076 if (error)
1077 return error;
1078 pm_print_times_init();
1079 return pm_autosleep_init();
1080}
1081
1082core_initcall(pm_init);
1083