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