Merge tag 'asoc-fix-v4.8-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/brooni...
[deliverable/linux.git] / kernel / power / main.c
CommitLineData
1da177e4
LT
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
341d4166 6 *
1da177e4
LT
7 * This file is released under the GPLv2
8 *
9 */
10
6e5fdeed 11#include <linux/export.h>
1da177e4
LT
12#include <linux/kobject.h>
13#include <linux/string.h>
431d452a 14#include <linux/pm-trace.h>
5e928f77 15#include <linux/workqueue.h>
2a77c46d
SL
16#include <linux/debugfs.h>
17#include <linux/seq_file.h>
1da177e4
LT
18
19#include "power.h"
20
a6d70980 21DEFINE_MUTEX(pm_mutex);
1da177e4 22
cd51e61c
RW
23#ifdef CONFIG_PM_SLEEP
24
82525756
AS
25/* Routines for PM-transition notifications */
26
27static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28
29int register_pm_notifier(struct notifier_block *nb)
30{
31 return blocking_notifier_chain_register(&pm_chain_head, nb);
32}
33EXPORT_SYMBOL_GPL(register_pm_notifier);
34
35int unregister_pm_notifier(struct notifier_block *nb)
36{
37 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
38}
39EXPORT_SYMBOL_GPL(unregister_pm_notifier);
40
ea00f4f4 41int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls)
82525756 42{
ea00f4f4
LW
43 int ret;
44
45 ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL,
46 nr_to_call, nr_calls);
f0c077a8
AM
47
48 return notifier_to_errno(ret);
82525756 49}
ea00f4f4
LW
50int pm_notifier_call_chain(unsigned long val)
51{
52 return __pm_notifier_call_chain(val, -1, NULL);
53}
82525756 54
0e06b4a8
RW
55/* If set, devices may be suspended and resumed asynchronously. */
56int pm_async_enabled = 1;
57
58static ssize_t pm_async_show(struct kobject *kobj, struct kobj_attribute *attr,
59 char *buf)
60{
61 return sprintf(buf, "%d\n", pm_async_enabled);
62}
63
64static ssize_t pm_async_store(struct kobject *kobj, struct kobj_attribute *attr,
65 const char *buf, size_t n)
66{
67 unsigned long val;
68
883ee4f7 69 if (kstrtoul(buf, 10, &val))
0e06b4a8
RW
70 return -EINVAL;
71
72 if (val > 1)
73 return -EINVAL;
74
75 pm_async_enabled = val;
76 return n;
77}
78
79power_attr(pm_async);
80
0e7d56e3
RW
81#ifdef CONFIG_PM_DEBUG
82int pm_test_level = TEST_NONE;
83
0e7d56e3
RW
84static const char * const pm_tests[__TEST_AFTER_LAST] = {
85 [TEST_NONE] = "none",
86 [TEST_CORE] = "core",
87 [TEST_CPUS] = "processors",
88 [TEST_PLATFORM] = "platform",
89 [TEST_DEVICES] = "devices",
90 [TEST_FREEZER] = "freezer",
91};
92
039a75c6
RW
93static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
94 char *buf)
0e7d56e3
RW
95{
96 char *s = buf;
97 int level;
98
99 for (level = TEST_FIRST; level <= TEST_MAX; level++)
100 if (pm_tests[level]) {
101 if (level == pm_test_level)
102 s += sprintf(s, "[%s] ", pm_tests[level]);
103 else
104 s += sprintf(s, "%s ", pm_tests[level]);
105 }
106
107 if (s != buf)
108 /* convert the last space to a newline */
109 *(s-1) = '\n';
110
111 return (s - buf);
112}
113
039a75c6
RW
114static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
115 const char *buf, size_t n)
0e7d56e3
RW
116{
117 const char * const *s;
118 int level;
119 char *p;
120 int len;
121 int error = -EINVAL;
122
123 p = memchr(buf, '\n', n);
124 len = p ? p - buf : n;
125
bcda53fa 126 lock_system_sleep();
0e7d56e3
RW
127
128 level = TEST_FIRST;
129 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++)
130 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) {
131 pm_test_level = level;
132 error = 0;
133 break;
134 }
135
bcda53fa 136 unlock_system_sleep();
0e7d56e3
RW
137
138 return error ? error : n;
139}
140
141power_attr(pm_test);
091d71e0 142#endif /* CONFIG_PM_DEBUG */
0e7d56e3 143
2a77c46d
SL
144#ifdef CONFIG_DEBUG_FS
145static char *suspend_step_name(enum suspend_stat_step step)
146{
147 switch (step) {
148 case SUSPEND_FREEZE:
149 return "freeze";
150 case SUSPEND_PREPARE:
151 return "prepare";
152 case SUSPEND_SUSPEND:
153 return "suspend";
154 case SUSPEND_SUSPEND_NOIRQ:
155 return "suspend_noirq";
156 case SUSPEND_RESUME_NOIRQ:
157 return "resume_noirq";
158 case SUSPEND_RESUME:
159 return "resume";
160 default:
161 return "";
162 }
163}
164
165static int suspend_stats_show(struct seq_file *s, void *unused)
166{
167 int i, index, last_dev, last_errno, last_step;
168
169 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
170 last_dev %= REC_FAILED_NUM;
171 last_errno = suspend_stats.last_failed_errno + REC_FAILED_NUM - 1;
172 last_errno %= REC_FAILED_NUM;
173 last_step = suspend_stats.last_failed_step + REC_FAILED_NUM - 1;
174 last_step %= REC_FAILED_NUM;
cf579dfb
RW
175 seq_printf(s, "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n"
176 "%s: %d\n%s: %d\n%s: %d\n%s: %d\n%s: %d\n",
2a77c46d
SL
177 "success", suspend_stats.success,
178 "fail", suspend_stats.fail,
179 "failed_freeze", suspend_stats.failed_freeze,
180 "failed_prepare", suspend_stats.failed_prepare,
181 "failed_suspend", suspend_stats.failed_suspend,
cf579dfb
RW
182 "failed_suspend_late",
183 suspend_stats.failed_suspend_late,
2a77c46d
SL
184 "failed_suspend_noirq",
185 suspend_stats.failed_suspend_noirq,
186 "failed_resume", suspend_stats.failed_resume,
cf579dfb
RW
187 "failed_resume_early",
188 suspend_stats.failed_resume_early,
2a77c46d
SL
189 "failed_resume_noirq",
190 suspend_stats.failed_resume_noirq);
191 seq_printf(s, "failures:\n last_failed_dev:\t%-s\n",
192 suspend_stats.failed_devs[last_dev]);
193 for (i = 1; i < REC_FAILED_NUM; i++) {
194 index = last_dev + REC_FAILED_NUM - i;
195 index %= REC_FAILED_NUM;
196 seq_printf(s, "\t\t\t%-s\n",
197 suspend_stats.failed_devs[index]);
198 }
199 seq_printf(s, " last_failed_errno:\t%-d\n",
200 suspend_stats.errno[last_errno]);
201 for (i = 1; i < REC_FAILED_NUM; i++) {
202 index = last_errno + REC_FAILED_NUM - i;
203 index %= REC_FAILED_NUM;
204 seq_printf(s, "\t\t\t%-d\n",
205 suspend_stats.errno[index]);
206 }
207 seq_printf(s, " last_failed_step:\t%-s\n",
208 suspend_step_name(
209 suspend_stats.failed_steps[last_step]));
210 for (i = 1; i < REC_FAILED_NUM; i++) {
211 index = last_step + REC_FAILED_NUM - i;
212 index %= REC_FAILED_NUM;
213 seq_printf(s, "\t\t\t%-s\n",
214 suspend_step_name(
215 suspend_stats.failed_steps[index]));
216 }
217
218 return 0;
219}
220
221static int suspend_stats_open(struct inode *inode, struct file *file)
222{
223 return single_open(file, suspend_stats_show, NULL);
224}
225
226static const struct file_operations suspend_stats_operations = {
227 .open = suspend_stats_open,
228 .read = seq_read,
229 .llseek = seq_lseek,
230 .release = single_release,
231};
232
233static int __init pm_debugfs_init(void)
234{
235 debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
236 NULL, NULL, &suspend_stats_operations);
237 return 0;
238}
239
240late_initcall(pm_debugfs_init);
241#endif /* CONFIG_DEBUG_FS */
242
ca123102
RW
243#endif /* CONFIG_PM_SLEEP */
244
b2df1d4f
RW
245#ifdef CONFIG_PM_SLEEP_DEBUG
246/*
247 * pm_print_times: print time taken by devices to suspend and resume.
248 *
249 * show() returns whether printing of suspend and resume times is enabled.
250 * store() accepts 0 or 1. 0 disables printing and 1 enables it.
251 */
252bool pm_print_times_enabled;
253
254static ssize_t pm_print_times_show(struct kobject *kobj,
255 struct kobj_attribute *attr, char *buf)
256{
257 return sprintf(buf, "%d\n", pm_print_times_enabled);
258}
259
260static ssize_t pm_print_times_store(struct kobject *kobj,
261 struct kobj_attribute *attr,
262 const char *buf, size_t n)
263{
264 unsigned long val;
265
266 if (kstrtoul(buf, 10, &val))
267 return -EINVAL;
268
269 if (val > 1)
270 return -EINVAL;
271
272 pm_print_times_enabled = !!val;
273 return n;
274}
275
276power_attr(pm_print_times);
277
278static inline void pm_print_times_init(void)
279{
280 pm_print_times_enabled = !!initcall_debug;
281}
a6f5f0dd
AY
282
283static ssize_t pm_wakeup_irq_show(struct kobject *kobj,
284 struct kobj_attribute *attr,
285 char *buf)
286{
287 return pm_wakeup_irq ? sprintf(buf, "%u\n", pm_wakeup_irq) : -ENODATA;
288}
289
a1e9ca69 290power_attr_ro(pm_wakeup_irq);
a6f5f0dd 291
819b1bb3 292#else /* !CONFIG_PM_SLEEP_DEBUG */
b2df1d4f
RW
293static inline void pm_print_times_init(void) {}
294#endif /* CONFIG_PM_SLEEP_DEBUG */
295
d76e15fb 296struct kobject *power_kobj;
1da177e4
LT
297
298/**
0399d4db 299 * state - control system sleep states.
1da177e4 300 *
0399d4db
RW
301 * show() returns available sleep state labels, which may be "mem", "standby",
302 * "freeze" and "disk" (hibernation). See Documentation/power/states.txt for a
303 * description of what they mean.
1da177e4 304 *
0399d4db
RW
305 * store() accepts one of those strings, translates it into the proper
306 * enumerated value, and initiates a suspend transition.
1da177e4 307 */
386f275f
KS
308static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
309 char *buf)
1da177e4 310{
296699de
RW
311 char *s = buf;
312#ifdef CONFIG_SUSPEND
27ddcc65
RW
313 suspend_state_t i;
314
315 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
d431cbc5
RW
316 if (pm_states[i])
317 s += sprintf(s,"%s ", pm_states[i]);
1da177e4 318
296699de 319#endif
a6e15a39
KC
320 if (hibernation_available())
321 s += sprintf(s, "disk ");
a3d25c27
RW
322 if (s != buf)
323 /* convert the last space to a newline */
324 *(s-1) = '\n';
1da177e4
LT
325 return (s - buf);
326}
327
7483b4a4 328static suspend_state_t decode_state(const char *buf, size_t n)
1da177e4 329{
296699de 330#ifdef CONFIG_SUSPEND
d431cbc5 331 suspend_state_t state;
296699de 332#endif
1da177e4 333 char *p;
1da177e4
LT
334 int len;
335
336 p = memchr(buf, '\n', n);
337 len = p ? p - buf : n;
338
7483b4a4
RW
339 /* Check hibernation first. */
340 if (len == 4 && !strncmp(buf, "disk", len))
341 return PM_SUSPEND_MAX;
a3d25c27 342
296699de 343#ifdef CONFIG_SUSPEND
d431cbc5
RW
344 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
345 const char *label = pm_states[state];
346
347 if (label && len == strlen(label) && !strncmp(buf, label, len))
348 return state;
349 }
296699de
RW
350#endif
351
7483b4a4
RW
352 return PM_SUSPEND_ON;
353}
354
355static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
356 const char *buf, size_t n)
357{
358 suspend_state_t state;
359 int error;
360
361 error = pm_autosleep_lock();
362 if (error)
363 return error;
364
365 if (pm_autosleep_state() > PM_SUSPEND_ON) {
366 error = -EBUSY;
367 goto out;
368 }
369
370 state = decode_state(buf, n);
371 if (state < PM_SUSPEND_MAX)
372 error = pm_suspend(state);
373 else if (state == PM_SUSPEND_MAX)
374 error = hibernate();
375 else
376 error = -EINVAL;
377
378 out:
379 pm_autosleep_unlock();
1da177e4
LT
380 return error ? error : n;
381}
382
383power_attr(state);
384
c125e96f
RW
385#ifdef CONFIG_PM_SLEEP
386/*
387 * The 'wakeup_count' attribute, along with the functions defined in
388 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
389 * handled in a non-racy way.
390 *
391 * If a wakeup event occurs when the system is in a sleep state, it simply is
392 * woken up. In turn, if an event that would wake the system up from a sleep
393 * state occurs when it is undergoing a transition to that sleep state, the
394 * transition should be aborted. Moreover, if such an event occurs when the
395 * system is in the working state, an attempt to start a transition to the
396 * given sleep state should fail during certain period after the detection of
397 * the event. Using the 'state' attribute alone is not sufficient to satisfy
398 * these requirements, because a wakeup event may occur exactly when 'state'
399 * is being written to and may be delivered to user space right before it is
400 * frozen, so the event will remain only partially processed until the system is
401 * woken up by another event. In particular, it won't cause the transition to
402 * a sleep state to be aborted.
403 *
404 * This difficulty may be overcome if user space uses 'wakeup_count' before
405 * writing to 'state'. It first should read from 'wakeup_count' and store
406 * the read value. Then, after carrying out its own preparations for the system
407 * transition to a sleep state, it should write the stored value to
25985edc 408 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
c125e96f
RW
409 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
410 * is allowed to write to 'state', but the transition will be aborted if there
411 * are any wakeup events detected after 'wakeup_count' was written to.
412 */
413
414static ssize_t wakeup_count_show(struct kobject *kobj,
415 struct kobj_attribute *attr,
416 char *buf)
417{
074037ec 418 unsigned int val;
c125e96f 419
7483b4a4
RW
420 return pm_get_wakeup_count(&val, true) ?
421 sprintf(buf, "%u\n", val) : -EINTR;
c125e96f
RW
422}
423
424static ssize_t wakeup_count_store(struct kobject *kobj,
425 struct kobj_attribute *attr,
426 const char *buf, size_t n)
427{
074037ec 428 unsigned int val;
7483b4a4
RW
429 int error;
430
431 error = pm_autosleep_lock();
432 if (error)
433 return error;
c125e96f 434
7483b4a4
RW
435 if (pm_autosleep_state() > PM_SUSPEND_ON) {
436 error = -EBUSY;
437 goto out;
438 }
439
440 error = -EINVAL;
074037ec 441 if (sscanf(buf, "%u", &val) == 1) {
c125e96f 442 if (pm_save_wakeup_count(val))
7483b4a4 443 error = n;
bb177fed
JW
444 else
445 pm_print_active_wakeup_sources();
c125e96f 446 }
7483b4a4
RW
447
448 out:
449 pm_autosleep_unlock();
450 return error;
c125e96f
RW
451}
452
453power_attr(wakeup_count);
7483b4a4
RW
454
455#ifdef CONFIG_PM_AUTOSLEEP
456static ssize_t autosleep_show(struct kobject *kobj,
457 struct kobj_attribute *attr,
458 char *buf)
459{
460 suspend_state_t state = pm_autosleep_state();
461
462 if (state == PM_SUSPEND_ON)
463 return sprintf(buf, "off\n");
464
465#ifdef CONFIG_SUSPEND
466 if (state < PM_SUSPEND_MAX)
d431cbc5
RW
467 return sprintf(buf, "%s\n", pm_states[state] ?
468 pm_states[state] : "error");
7483b4a4
RW
469#endif
470#ifdef CONFIG_HIBERNATION
471 return sprintf(buf, "disk\n");
472#else
473 return sprintf(buf, "error");
474#endif
475}
476
477static ssize_t autosleep_store(struct kobject *kobj,
478 struct kobj_attribute *attr,
479 const char *buf, size_t n)
480{
481 suspend_state_t state = decode_state(buf, n);
482 int error;
483
484 if (state == PM_SUSPEND_ON
040e5bf6 485 && strcmp(buf, "off") && strcmp(buf, "off\n"))
7483b4a4
RW
486 return -EINVAL;
487
488 error = pm_autosleep_set_state(state);
489 return error ? error : n;
490}
491
492power_attr(autosleep);
493#endif /* CONFIG_PM_AUTOSLEEP */
b86ff982
RW
494
495#ifdef CONFIG_PM_WAKELOCKS
496static ssize_t wake_lock_show(struct kobject *kobj,
497 struct kobj_attribute *attr,
498 char *buf)
499{
500 return pm_show_wakelocks(buf, true);
501}
502
503static ssize_t wake_lock_store(struct kobject *kobj,
504 struct kobj_attribute *attr,
505 const char *buf, size_t n)
506{
507 int error = pm_wake_lock(buf);
508 return error ? error : n;
509}
510
511power_attr(wake_lock);
512
513static ssize_t wake_unlock_show(struct kobject *kobj,
514 struct kobj_attribute *attr,
515 char *buf)
516{
517 return pm_show_wakelocks(buf, false);
518}
519
520static ssize_t wake_unlock_store(struct kobject *kobj,
521 struct kobj_attribute *attr,
522 const char *buf, size_t n)
523{
524 int error = pm_wake_unlock(buf);
525 return error ? error : n;
526}
527
528power_attr(wake_unlock);
529
530#endif /* CONFIG_PM_WAKELOCKS */
c125e96f
RW
531#endif /* CONFIG_PM_SLEEP */
532
c5c6ba4e
RW
533#ifdef CONFIG_PM_TRACE
534int pm_trace_enabled;
535
386f275f
KS
536static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
537 char *buf)
c5c6ba4e
RW
538{
539 return sprintf(buf, "%d\n", pm_trace_enabled);
540}
541
542static ssize_t
386f275f
KS
543pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
544 const char *buf, size_t n)
c5c6ba4e
RW
545{
546 int val;
547
548 if (sscanf(buf, "%d", &val) == 1) {
549 pm_trace_enabled = !!val;
9dceefe4
SK
550 if (pm_trace_enabled) {
551 pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
552 "PM: Correct system time has to be restored manually after resume.\n");
553 }
c5c6ba4e
RW
554 return n;
555 }
556 return -EINVAL;
557}
558
559power_attr(pm_trace);
d33ac60b
JH
560
561static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
562 struct kobj_attribute *attr,
563 char *buf)
564{
565 return show_trace_dev_match(buf, PAGE_SIZE);
566}
567
a1e9ca69 568power_attr_ro(pm_trace_dev_match);
d33ac60b 569
0e7d56e3 570#endif /* CONFIG_PM_TRACE */
c5c6ba4e 571
957d1282
LF
572#ifdef CONFIG_FREEZER
573static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
574 struct kobj_attribute *attr, char *buf)
575{
576 return sprintf(buf, "%u\n", freeze_timeout_msecs);
577}
578
579static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
580 struct kobj_attribute *attr,
581 const char *buf, size_t n)
582{
583 unsigned long val;
584
585 if (kstrtoul(buf, 10, &val))
586 return -EINVAL;
587
588 freeze_timeout_msecs = val;
589 return n;
590}
591
592power_attr(pm_freeze_timeout);
593
594#endif /* CONFIG_FREEZER*/
595
c5c6ba4e
RW
596static struct attribute * g[] = {
597 &state_attr.attr,
0e7d56e3 598#ifdef CONFIG_PM_TRACE
c5c6ba4e 599 &pm_trace_attr.attr,
d33ac60b 600 &pm_trace_dev_match_attr.attr,
0e7d56e3 601#endif
0e06b4a8
RW
602#ifdef CONFIG_PM_SLEEP
603 &pm_async_attr.attr,
c125e96f 604 &wakeup_count_attr.attr,
7483b4a4
RW
605#ifdef CONFIG_PM_AUTOSLEEP
606 &autosleep_attr.attr,
607#endif
b86ff982
RW
608#ifdef CONFIG_PM_WAKELOCKS
609 &wake_lock_attr.attr,
610 &wake_unlock_attr.attr,
611#endif
0e06b4a8 612#ifdef CONFIG_PM_DEBUG
0e7d56e3 613 &pm_test_attr.attr,
b2df1d4f
RW
614#endif
615#ifdef CONFIG_PM_SLEEP_DEBUG
4b7760ba 616 &pm_print_times_attr.attr,
a6f5f0dd 617 &pm_wakeup_irq_attr.attr,
0e06b4a8 618#endif
957d1282
LF
619#endif
620#ifdef CONFIG_FREEZER
621 &pm_freeze_timeout_attr.attr,
0e7d56e3 622#endif
c5c6ba4e
RW
623 NULL,
624};
1da177e4
LT
625
626static struct attribute_group attr_group = {
627 .attrs = g,
628};
629
5e928f77 630struct workqueue_struct *pm_wq;
7b199ca2 631EXPORT_SYMBOL_GPL(pm_wq);
5e928f77
RW
632
633static int __init pm_start_workqueue(void)
634{
58a69cb4 635 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
5e928f77
RW
636
637 return pm_wq ? 0 : -ENOMEM;
638}
5e928f77 639
1da177e4
LT
640static int __init pm_init(void)
641{
5e928f77
RW
642 int error = pm_start_workqueue();
643 if (error)
644 return error;
ac5c24ec 645 hibernate_image_size_init();
ddeb6487 646 hibernate_reserved_size_init();
d76e15fb
GKH
647 power_kobj = kobject_create_and_add("power", NULL);
648 if (!power_kobj)
039a5dcd 649 return -ENOMEM;
7483b4a4
RW
650 error = sysfs_create_group(power_kobj, &attr_group);
651 if (error)
652 return error;
b2df1d4f 653 pm_print_times_init();
7483b4a4 654 return pm_autosleep_init();
1da177e4
LT
655}
656
657core_initcall(pm_init);
This page took 0.782895 seconds and 5 git commands to generate.