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