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/resume-trace.h>
15 #include <linux/workqueue.h>
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
18
19 #include "power.h"
20
21 DEFINE_MUTEX(pm_mutex);
22
23 #ifdef CONFIG_PM_SLEEP
24
25 /* Routines for PM-transition notifications */
26
27 static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
28
register_pm_notifier(struct notifier_block * nb)29 int register_pm_notifier(struct notifier_block *nb)
30 {
31 return blocking_notifier_chain_register(&pm_chain_head, nb);
32 }
33 EXPORT_SYMBOL_GPL(register_pm_notifier);
34
unregister_pm_notifier(struct notifier_block * nb)35 int unregister_pm_notifier(struct notifier_block *nb)
36 {
37 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
38 }
39 EXPORT_SYMBOL_GPL(unregister_pm_notifier);
40
__pm_notifier_call_chain(unsigned long val,int nr_to_call,int * nr_calls)41 int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls)
42 {
43 int ret;
44
45 ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL,
46 nr_to_call, nr_calls);
47
48 return notifier_to_errno(ret);
49 }
pm_notifier_call_chain(unsigned long val)50 int pm_notifier_call_chain(unsigned long val)
51 {
52 return __pm_notifier_call_chain(val, -1, NULL);
53 }
54
55 /* If set, devices may be suspended and resumed asynchronously. */
56 int pm_async_enabled = 1;
57
pm_async_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)58 static 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
pm_async_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)64 static 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
69 if (kstrtoul(buf, 10, &val))
70 return -EINVAL;
71
72 if (val > 1)
73 return -EINVAL;
74
75 pm_async_enabled = val;
76 return n;
77 }
78
79 power_attr(pm_async);
80
81 #ifdef CONFIG_PM_DEBUG
82 int pm_test_level = TEST_NONE;
83
84 static 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
pm_test_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)93 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
94 char *buf)
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
pm_test_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)114 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
115 const char *buf, size_t n)
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
126 lock_system_sleep();
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
136 unlock_system_sleep();
137
138 return error ? error : n;
139 }
140
141 power_attr(pm_test);
142 #endif /* CONFIG_PM_DEBUG */
143
144 #ifdef CONFIG_DEBUG_FS
suspend_step_name(enum suspend_stat_step step)145 static 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
suspend_stats_show(struct seq_file * s,void * unused)165 static 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;
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",
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,
182 "failed_suspend_late",
183 suspend_stats.failed_suspend_late,
184 "failed_suspend_noirq",
185 suspend_stats.failed_suspend_noirq,
186 "failed_resume", suspend_stats.failed_resume,
187 "failed_resume_early",
188 suspend_stats.failed_resume_early,
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
suspend_stats_open(struct inode * inode,struct file * file)221 static int suspend_stats_open(struct inode *inode, struct file *file)
222 {
223 return single_open(file, suspend_stats_show, NULL);
224 }
225
226 static 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
pm_debugfs_init(void)233 static 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
240 late_initcall(pm_debugfs_init);
241 #endif /* CONFIG_DEBUG_FS */
242
243 #endif /* CONFIG_PM_SLEEP */
244
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 */
252 bool pm_print_times_enabled;
253
pm_print_times_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)254 static 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
pm_print_times_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)260 static 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
276 power_attr(pm_print_times);
277
pm_print_times_init(void)278 static inline void pm_print_times_init(void)
279 {
280 pm_print_times_enabled = !!initcall_debug;
281 }
282 #else /* !CONFIG_PP_SLEEP_DEBUG */
pm_print_times_init(void)283 static inline void pm_print_times_init(void) {}
284 #endif /* CONFIG_PM_SLEEP_DEBUG */
285
286 struct kobject *power_kobj;
287
288 /**
289 * state - control system sleep states.
290 *
291 * show() returns available sleep state labels, which may be "mem", "standby",
292 * "freeze" and "disk" (hibernation). See Documentation/power/states.txt for a
293 * description of what they mean.
294 *
295 * store() accepts one of those strings, translates it into the proper
296 * enumerated value, and initiates a suspend transition.
297 */
state_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)298 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
299 char *buf)
300 {
301 char *s = buf;
302 #ifdef CONFIG_SUSPEND
303 suspend_state_t i;
304
305 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
306 if (pm_states[i])
307 s += sprintf(s,"%s ", pm_states[i]);
308
309 #endif
310 if (hibernation_available())
311 s += sprintf(s, "disk ");
312 if (s != buf)
313 /* convert the last space to a newline */
314 *(s-1) = '\n';
315 return (s - buf);
316 }
317
decode_state(const char * buf,size_t n)318 static suspend_state_t decode_state(const char *buf, size_t n)
319 {
320 #ifdef CONFIG_SUSPEND
321 suspend_state_t state;
322 #endif
323 char *p;
324 int len;
325
326 p = memchr(buf, '\n', n);
327 len = p ? p - buf : n;
328
329 /* Check hibernation first. */
330 if (len == 4 && !strncmp(buf, "disk", len))
331 return PM_SUSPEND_MAX;
332
333 #ifdef CONFIG_SUSPEND
334 for (state = PM_SUSPEND_MIN; state < PM_SUSPEND_MAX; state++) {
335 const char *label = pm_states[state];
336
337 if (label && len == strlen(label) && !strncmp(buf, label, len))
338 return state;
339 }
340 #endif
341
342 return PM_SUSPEND_ON;
343 }
344
state_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)345 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
346 const char *buf, size_t n)
347 {
348 suspend_state_t state;
349 int error;
350
351 error = pm_autosleep_lock();
352 if (error)
353 return error;
354
355 if (pm_autosleep_state() > PM_SUSPEND_ON) {
356 error = -EBUSY;
357 goto out;
358 }
359
360 state = decode_state(buf, n);
361 if (state < PM_SUSPEND_MAX)
362 error = pm_suspend(state);
363 else if (state == PM_SUSPEND_MAX)
364 error = hibernate();
365 else
366 error = -EINVAL;
367
368 out:
369 pm_autosleep_unlock();
370 return error ? error : n;
371 }
372
373 power_attr(state);
374
375 #ifdef CONFIG_PM_SLEEP
376 /*
377 * The 'wakeup_count' attribute, along with the functions defined in
378 * drivers/base/power/wakeup.c, provides a means by which wakeup events can be
379 * handled in a non-racy way.
380 *
381 * If a wakeup event occurs when the system is in a sleep state, it simply is
382 * woken up. In turn, if an event that would wake the system up from a sleep
383 * state occurs when it is undergoing a transition to that sleep state, the
384 * transition should be aborted. Moreover, if such an event occurs when the
385 * system is in the working state, an attempt to start a transition to the
386 * given sleep state should fail during certain period after the detection of
387 * the event. Using the 'state' attribute alone is not sufficient to satisfy
388 * these requirements, because a wakeup event may occur exactly when 'state'
389 * is being written to and may be delivered to user space right before it is
390 * frozen, so the event will remain only partially processed until the system is
391 * woken up by another event. In particular, it won't cause the transition to
392 * a sleep state to be aborted.
393 *
394 * This difficulty may be overcome if user space uses 'wakeup_count' before
395 * writing to 'state'. It first should read from 'wakeup_count' and store
396 * the read value. Then, after carrying out its own preparations for the system
397 * transition to a sleep state, it should write the stored value to
398 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
399 * 'wakeup_count' was read and 'state' should not be written to. Otherwise, it
400 * is allowed to write to 'state', but the transition will be aborted if there
401 * are any wakeup events detected after 'wakeup_count' was written to.
402 */
403
wakeup_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)404 static ssize_t wakeup_count_show(struct kobject *kobj,
405 struct kobj_attribute *attr,
406 char *buf)
407 {
408 unsigned int val;
409
410 return pm_get_wakeup_count(&val, true) ?
411 sprintf(buf, "%u\n", val) : -EINTR;
412 }
413
wakeup_count_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)414 static ssize_t wakeup_count_store(struct kobject *kobj,
415 struct kobj_attribute *attr,
416 const char *buf, size_t n)
417 {
418 unsigned int val;
419 int error;
420
421 error = pm_autosleep_lock();
422 if (error)
423 return error;
424
425 if (pm_autosleep_state() > PM_SUSPEND_ON) {
426 error = -EBUSY;
427 goto out;
428 }
429
430 error = -EINVAL;
431 if (sscanf(buf, "%u", &val) == 1) {
432 if (pm_save_wakeup_count(val))
433 error = n;
434 else
435 pm_print_active_wakeup_sources();
436 }
437
438 out:
439 pm_autosleep_unlock();
440 return error;
441 }
442
443 power_attr(wakeup_count);
444
445 #ifdef CONFIG_PM_AUTOSLEEP
autosleep_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)446 static ssize_t autosleep_show(struct kobject *kobj,
447 struct kobj_attribute *attr,
448 char *buf)
449 {
450 suspend_state_t state = pm_autosleep_state();
451
452 if (state == PM_SUSPEND_ON)
453 return sprintf(buf, "off\n");
454
455 #ifdef CONFIG_SUSPEND
456 if (state < PM_SUSPEND_MAX)
457 return sprintf(buf, "%s\n", pm_states[state] ?
458 pm_states[state] : "error");
459 #endif
460 #ifdef CONFIG_HIBERNATION
461 return sprintf(buf, "disk\n");
462 #else
463 return sprintf(buf, "error");
464 #endif
465 }
466
autosleep_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)467 static ssize_t autosleep_store(struct kobject *kobj,
468 struct kobj_attribute *attr,
469 const char *buf, size_t n)
470 {
471 suspend_state_t state = decode_state(buf, n);
472 int error;
473
474 if (state == PM_SUSPEND_ON
475 && strcmp(buf, "off") && strcmp(buf, "off\n"))
476 return -EINVAL;
477
478 error = pm_autosleep_set_state(state);
479 return error ? error : n;
480 }
481
482 power_attr(autosleep);
483 #endif /* CONFIG_PM_AUTOSLEEP */
484
485 #ifdef CONFIG_PM_WAKELOCKS
wake_lock_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)486 static ssize_t wake_lock_show(struct kobject *kobj,
487 struct kobj_attribute *attr,
488 char *buf)
489 {
490 return pm_show_wakelocks(buf, true);
491 }
492
wake_lock_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)493 static ssize_t wake_lock_store(struct kobject *kobj,
494 struct kobj_attribute *attr,
495 const char *buf, size_t n)
496 {
497 int error = pm_wake_lock(buf);
498 return error ? error : n;
499 }
500
501 power_attr(wake_lock);
502
wake_unlock_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)503 static ssize_t wake_unlock_show(struct kobject *kobj,
504 struct kobj_attribute *attr,
505 char *buf)
506 {
507 return pm_show_wakelocks(buf, false);
508 }
509
wake_unlock_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)510 static ssize_t wake_unlock_store(struct kobject *kobj,
511 struct kobj_attribute *attr,
512 const char *buf, size_t n)
513 {
514 int error = pm_wake_unlock(buf);
515 return error ? error : n;
516 }
517
518 power_attr(wake_unlock);
519
520 #endif /* CONFIG_PM_WAKELOCKS */
521 #endif /* CONFIG_PM_SLEEP */
522
523 #ifdef CONFIG_PM_TRACE
524 int pm_trace_enabled;
525
pm_trace_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)526 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
527 char *buf)
528 {
529 return sprintf(buf, "%d\n", pm_trace_enabled);
530 }
531
532 static ssize_t
pm_trace_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)533 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
534 const char *buf, size_t n)
535 {
536 int val;
537
538 if (sscanf(buf, "%d", &val) == 1) {
539 pm_trace_enabled = !!val;
540 if (pm_trace_enabled) {
541 pr_warn("PM: Enabling pm_trace changes system date and time during resume.\n"
542 "PM: Correct system time has to be restored manually after resume.\n");
543 }
544 return n;
545 }
546 return -EINVAL;
547 }
548
549 power_attr(pm_trace);
550
pm_trace_dev_match_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)551 static ssize_t pm_trace_dev_match_show(struct kobject *kobj,
552 struct kobj_attribute *attr,
553 char *buf)
554 {
555 return show_trace_dev_match(buf, PAGE_SIZE);
556 }
557
558 static ssize_t
pm_trace_dev_match_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)559 pm_trace_dev_match_store(struct kobject *kobj, struct kobj_attribute *attr,
560 const char *buf, size_t n)
561 {
562 return -EINVAL;
563 }
564
565 power_attr(pm_trace_dev_match);
566
567 #endif /* CONFIG_PM_TRACE */
568
569 #ifdef CONFIG_FREEZER
pm_freeze_timeout_show(struct kobject * kobj,struct kobj_attribute * attr,char * buf)570 static ssize_t pm_freeze_timeout_show(struct kobject *kobj,
571 struct kobj_attribute *attr, char *buf)
572 {
573 return sprintf(buf, "%u\n", freeze_timeout_msecs);
574 }
575
pm_freeze_timeout_store(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t n)576 static ssize_t pm_freeze_timeout_store(struct kobject *kobj,
577 struct kobj_attribute *attr,
578 const char *buf, size_t n)
579 {
580 unsigned long val;
581
582 if (kstrtoul(buf, 10, &val))
583 return -EINVAL;
584
585 freeze_timeout_msecs = val;
586 return n;
587 }
588
589 power_attr(pm_freeze_timeout);
590
591 #endif /* CONFIG_FREEZER*/
592
593 static struct attribute * g[] = {
594 &state_attr.attr,
595 #ifdef CONFIG_PM_TRACE
596 &pm_trace_attr.attr,
597 &pm_trace_dev_match_attr.attr,
598 #endif
599 #ifdef CONFIG_PM_SLEEP
600 &pm_async_attr.attr,
601 &wakeup_count_attr.attr,
602 #ifdef CONFIG_PM_AUTOSLEEP
603 &autosleep_attr.attr,
604 #endif
605 #ifdef CONFIG_PM_WAKELOCKS
606 &wake_lock_attr.attr,
607 &wake_unlock_attr.attr,
608 #endif
609 #ifdef CONFIG_PM_DEBUG
610 &pm_test_attr.attr,
611 #endif
612 #ifdef CONFIG_PM_SLEEP_DEBUG
613 &pm_print_times_attr.attr,
614 #endif
615 #endif
616 #ifdef CONFIG_FREEZER
617 &pm_freeze_timeout_attr.attr,
618 #endif
619 NULL,
620 };
621
622 static struct attribute_group attr_group = {
623 .attrs = g,
624 };
625
626 struct workqueue_struct *pm_wq;
627 EXPORT_SYMBOL_GPL(pm_wq);
628
pm_start_workqueue(void)629 static int __init pm_start_workqueue(void)
630 {
631 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
632
633 return pm_wq ? 0 : -ENOMEM;
634 }
635
pm_init(void)636 static int __init pm_init(void)
637 {
638 int error = pm_start_workqueue();
639 if (error)
640 return error;
641 hibernate_image_size_init();
642 hibernate_reserved_size_init();
643 power_kobj = kobject_create_and_add("power", NULL);
644 if (!power_kobj)
645 return -ENOMEM;
646 error = sysfs_create_group(power_kobj, &attr_group);
647 if (error)
648 return error;
649 pm_print_times_init();
650 return pm_autosleep_init();
651 }
652
653 core_initcall(pm_init);
654