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