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