blob: bdbd605c4215e704db26ff853e15b5a506898f00 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * kernel/power/main.c - PM subsystem core functionality.
4 *
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -04009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/kobject.h>
11#include <linux/string.h>
Zhonghui Fu431d4522015-03-18 15:54:27 +010012#include <linux/pm-trace.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020013#include <linux/workqueue.h>
ShuoX Liu2a77c462011-08-10 23:01:26 +020014#include <linux/debugfs.h>
15#include <linux/seq_file.h>
Pingfan Liu55f25032018-07-31 16:51:32 +080016#include <linux/suspend.h>
Harry Panb5dee312019-02-25 20:36:41 +080017#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "power.h"
20
Rafael J. Wysockicd51e61c2011-02-11 00:04:52 +010021#ifdef CONFIG_PM_SLEEP
22
Bart Van Assche4bf236a2018-01-05 09:19:08 -080023void lock_system_sleep(void)
24{
25 current->flags |= PF_FREEZER_SKIP;
Pingfan Liu55f25032018-07-31 16:51:32 +080026 mutex_lock(&system_transition_mutex);
Bart Van Assche4bf236a2018-01-05 09:19:08 -080027}
28EXPORT_SYMBOL_GPL(lock_system_sleep);
29
30void 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
Pingfan Liu55f25032018-07-31 16:51:32 +080038 * 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.
Bart Van Assche4bf236a2018-01-05 09:19:08 -080041 *
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;
Pingfan Liu55f25032018-07-31 16:51:32 +080049 mutex_unlock(&system_transition_mutex);
Bart Van Assche4bf236a2018-01-05 09:19:08 -080050}
51EXPORT_SYMBOL_GPL(unlock_system_sleep);
52
Harry Panb5dee312019-02-25 20:36:41 +080053void ksys_sync_helper(void)
54{
Harry Panc64546b2019-02-25 20:36:43 +080055 ktime_t start;
56 long elapsed_msecs;
57
58 start = ktime_get();
Harry Panb5dee312019-02-25 20:36:41 +080059 ksys_sync();
Harry Panc64546b2019-02-25 20:36:43 +080060 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);
Harry Panb5dee312019-02-25 20:36:41 +080063}
64EXPORT_SYMBOL_GPL(ksys_sync_helper);
65
Alan Stern82525752007-11-19 23:49:18 +010066/* Routines for PM-transition notifications */
67
68static BLOCKING_NOTIFIER_HEAD(pm_chain_head);
69
70int register_pm_notifier(struct notifier_block *nb)
71{
72 return blocking_notifier_chain_register(&pm_chain_head, nb);
73}
74EXPORT_SYMBOL_GPL(register_pm_notifier);
75
76int unregister_pm_notifier(struct notifier_block *nb)
77{
78 return blocking_notifier_chain_unregister(&pm_chain_head, nb);
79}
80EXPORT_SYMBOL_GPL(unregister_pm_notifier);
81
Lianwei Wangea00f4f2016-06-19 23:52:27 -070082int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls)
Alan Stern82525752007-11-19 23:49:18 +010083{
Lianwei Wangea00f4f2016-06-19 23:52:27 -070084 int ret;
85
86 ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL,
87 nr_to_call, nr_calls);
Akinobu Mitaf0c077a2011-07-08 20:53:36 +020088
89 return notifier_to_errno(ret);
Alan Stern82525752007-11-19 23:49:18 +010090}
Lianwei Wangea00f4f2016-06-19 23:52:27 -070091int pm_notifier_call_chain(unsigned long val)
92{
93 return __pm_notifier_call_chain(val, -1, NULL);
94}
Alan Stern82525752007-11-19 23:49:18 +010095
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +010096/* If set, devices may be suspended and resumed asynchronously. */
97int pm_async_enabled = 1;
98
99static 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
105static 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
Daniel Walter883ee4f2012-10-23 01:20:35 +0200110 if (kstrtoul(buf, 10, &val))
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100111 return -EINVAL;
112
113 if (val > 1)
114 return -EINVAL;
115
116 pm_async_enabled = val;
117 return n;
118}
119
120power_attr(pm_async);
121
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100122#ifdef CONFIG_SUSPEND
123static 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
146static 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
165static 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
191power_attr(mem_sleep);
192#endif /* CONFIG_SUSPEND */
193
Rafael J. Wysockie516a1d2017-07-21 14:44:02 +0200194#ifdef CONFIG_PM_SLEEP_DEBUG
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100195int pm_test_level = TEST_NONE;
196
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100197static 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
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100206static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr,
207 char *buf)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100208{
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
Rafael J. Wysocki039a75c2008-01-29 00:29:06 +0100227static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr,
228 const char *buf, size_t n)
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100229{
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
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100239 lock_system_sleep();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100240
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
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100249 unlock_system_sleep();
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100250
251 return error ? error : n;
252}
253
254power_attr(pm_test);
Rafael J. Wysockie516a1d2017-07-21 14:44:02 +0200255#endif /* CONFIG_PM_SLEEP_DEBUG */
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100256
ShuoX Liu2a77c462011-08-10 23:01:26 +0200257#ifdef CONFIG_DEBUG_FS
258static 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
278static 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;
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100288 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",
ShuoX Liu2a77c462011-08-10 23:01:26 +0200290 "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,
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100295 "failed_suspend_late",
296 suspend_stats.failed_suspend_late,
ShuoX Liu2a77c462011-08-10 23:01:26 +0200297 "failed_suspend_noirq",
298 suspend_stats.failed_suspend_noirq,
299 "failed_resume", suspend_stats.failed_resume,
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100300 "failed_resume_early",
301 suspend_stats.failed_resume_early,
ShuoX Liu2a77c462011-08-10 23:01:26 +0200302 "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}
Yangtao Li943a10f2018-12-11 11:20:48 -0500333DEFINE_SHOW_ATTRIBUTE(suspend_stats);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200334
335static int __init pm_debugfs_init(void)
336{
337 debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO,
Yangtao Li943a10f2018-12-11 11:20:48 -0500338 NULL, NULL, &suspend_stats_fops);
ShuoX Liu2a77c462011-08-10 23:01:26 +0200339 return 0;
340}
341
342late_initcall(pm_debugfs_init);
343#endif /* CONFIG_DEBUG_FS */
344
Rafael J. Wysockica123102011-08-11 22:38:12 +0200345#endif /* CONFIG_PM_SLEEP */
346
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200347#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 */
354bool pm_print_times_enabled;
355
356static 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
362static 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
378power_attr(pm_print_times);
379
380static inline void pm_print_times_init(void)
381{
382 pm_print_times_enabled = !!initcall_debug;
383}
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700384
385static 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
Rafael J. Wysockia1e9ca62016-01-02 03:09:16 +0100392power_attr_ro(pm_wakeup_irq);
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700393
Srinivas Pandruvada726fb6b2017-08-15 18:16:59 -0700394bool pm_debug_messages_on __read_mostly;
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200395
396static 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
402static 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
418power_attr(pm_debug_messages);
419
420/**
Rafael J. Wysockicb08e032017-07-23 00:03:43 +0200421 * __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.
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200423 * @fmt: Message format.
424 *
425 * The message will be emitted if enabled through the pm_debug_messages
426 * sysfs attribute.
427 */
Rafael J. Wysockicb08e032017-07-23 00:03:43 +0200428void __pm_pr_dbg(bool defer, const char *fmt, ...)
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200429{
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
Rafael J. Wysockicb08e032017-07-23 00:03:43 +0200441 if (defer)
442 printk_deferred(KERN_DEBUG "PM: %pV", &vaf);
443 else
444 printk(KERN_DEBUG "PM: %pV", &vaf);
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200445
446 va_end(args);
447}
448
Rafael J. Wysocki819b1bb2015-05-13 15:31:12 +0200449#else /* !CONFIG_PM_SLEEP_DEBUG */
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200450static inline void pm_print_times_init(void) {}
451#endif /* CONFIG_PM_SLEEP_DEBUG */
452
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800453struct kobject *power_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455/**
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200456 * state - control system sleep states.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200458 * show() returns available sleep state labels, which may be "mem", "standby",
Mauro Carvalho Chehab44348e8a2018-06-14 12:34:32 -0300459 * "freeze" and "disk" (hibernation).
460 * See Documentation/admin-guide/pm/sleep-states.rst for a description of
461 * what they mean.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200463 * store() accepts one of those strings, translates it into the proper
464 * enumerated value, and initiates a suspend transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Kay Sievers386f2752007-11-02 13:47:53 +0100466static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
467 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200469 char *s = buf;
470#ifdef CONFIG_SUSPEND
Rafael J. Wysocki27ddcc62014-05-26 13:40:47 +0200471 suspend_state_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Rafael J. Wysocki27ddcc62014-05-26 13:40:47 +0200473 for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++)
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200474 if (pm_states[i])
475 s += sprintf(s,"%s ", pm_states[i]);
Rafael J. Wysocki27ddcc62014-05-26 13:40:47 +0200476
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200477#endif
Kees Cooka6e15a32014-06-13 13:30:35 -0700478 if (hibernation_available())
479 s += sprintf(s, "disk ");
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700480 if (s != buf)
481 /* convert the last space to a newline */
482 *(s-1) = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return (s - buf);
484}
485
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200486static suspend_state_t decode_state(const char *buf, size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200488#ifdef CONFIG_SUSPEND
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200489 suspend_state_t state;
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200490#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int len;
493
494 p = memchr(buf, '\n', n);
495 len = p ? p - buf : n;
496
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200497 /* Check hibernation first. */
498 if (len == 4 && !strncmp(buf, "disk", len))
499 return PM_SUSPEND_MAX;
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700500
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200501#ifdef CONFIG_SUSPEND
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200502 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 }
Rafael J. Wysocki296699d2007-07-29 23:27:18 +0200508#endif
509
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200510 return PM_SUSPEND_ON;
511}
512
513static 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);
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100529 if (state < PM_SUSPEND_MAX) {
530 if (state == PM_SUSPEND_MEM)
531 state = mem_sleep_current;
532
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200533 error = pm_suspend(state);
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100534 } else if (state == PM_SUSPEND_MAX) {
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200535 error = hibernate();
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100536 } else {
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200537 error = -EINVAL;
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100538 }
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200539
540 out:
541 pm_autosleep_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return error ? error : n;
543}
544
545power_attr(state);
546
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200547#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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300570 * 'wakeup_count'. If that fails, at least one wakeup event has occurred since
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200571 * '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
576static ssize_t wakeup_count_show(struct kobject *kobj,
577 struct kobj_attribute *attr,
578 char *buf)
579{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200580 unsigned int val;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200581
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200582 return pm_get_wakeup_count(&val, true) ?
583 sprintf(buf, "%u\n", val) : -EINTR;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200584}
585
586static ssize_t wakeup_count_store(struct kobject *kobj,
587 struct kobj_attribute *attr,
588 const char *buf, size_t n)
589{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200590 unsigned int val;
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200591 int error;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200592
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200593 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;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200603 if (sscanf(buf, "%u", &val) == 1) {
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200604 if (pm_save_wakeup_count(val))
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200605 error = n;
Julius Wernerbb177fe2013-06-12 12:55:22 -0700606 else
607 pm_print_active_wakeup_sources();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200608 }
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200609
610 out:
611 pm_autosleep_unlock();
612 return error;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200613}
614
615power_attr(wakeup_count);
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200616
617#ifdef CONFIG_PM_AUTOSLEEP
618static 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)
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200629 return sprintf(buf, "%s\n", pm_states[state] ?
630 pm_states[state] : "error");
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200631#endif
632#ifdef CONFIG_HIBERNATION
633 return sprintf(buf, "disk\n");
634#else
635 return sprintf(buf, "error");
636#endif
637}
638
639static 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
Arve Hjønnevåg040e5bf2012-05-04 00:14:21 +0200647 && strcmp(buf, "off") && strcmp(buf, "off\n"))
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200648 return -EINVAL;
649
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100650 if (state == PM_SUSPEND_MEM)
651 state = mem_sleep_current;
652
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200653 error = pm_autosleep_set_state(state);
654 return error ? error : n;
655}
656
657power_attr(autosleep);
658#endif /* CONFIG_PM_AUTOSLEEP */
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200659
660#ifdef CONFIG_PM_WAKELOCKS
661static 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
668static 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
676power_attr(wake_lock);
677
678static 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
685static 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
693power_attr(wake_unlock);
694
695#endif /* CONFIG_PM_WAKELOCKS */
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200696#endif /* CONFIG_PM_SLEEP */
697
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700698#ifdef CONFIG_PM_TRACE
699int pm_trace_enabled;
700
Kay Sievers386f2752007-11-02 13:47:53 +0100701static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
702 char *buf)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700703{
704 return sprintf(buf, "%d\n", pm_trace_enabled);
705}
706
707static ssize_t
Kay Sievers386f2752007-11-02 13:47:53 +0100708pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
709 const char *buf, size_t n)
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700710{
711 int val;
712
713 if (sscanf(buf, "%d", &val) == 1) {
714 pm_trace_enabled = !!val;
Shuah Khan9dceefe2013-06-26 16:27:35 -0600715 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 }
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700719 return n;
720 }
721 return -EINVAL;
722}
723
724power_attr(pm_trace);
James Hogand33ac602010-10-12 00:00:25 +0200725
726static 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
Rafael J. Wysockia1e9ca62016-01-02 03:09:16 +0100733power_attr_ro(pm_trace_dev_match);
James Hogand33ac602010-10-12 00:00:25 +0200734
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100735#endif /* CONFIG_PM_TRACE */
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700736
Li Fei957d1282013-02-01 08:56:03 +0000737#ifdef CONFIG_FREEZER
738static 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
744static 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
757power_attr(pm_freeze_timeout);
758
759#endif /* CONFIG_FREEZER*/
760
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700761static struct attribute * g[] = {
762 &state_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100763#ifdef CONFIG_PM_TRACE
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700764 &pm_trace_attr.attr,
James Hogand33ac602010-10-12 00:00:25 +0200765 &pm_trace_dev_match_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100766#endif
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100767#ifdef CONFIG_PM_SLEEP
768 &pm_async_attr.attr,
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200769 &wakeup_count_attr.attr,
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100770#ifdef CONFIG_SUSPEND
771 &mem_sleep_attr.attr,
772#endif
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200773#ifdef CONFIG_PM_AUTOSLEEP
774 &autosleep_attr.attr,
775#endif
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200776#ifdef CONFIG_PM_WAKELOCKS
777 &wake_lock_attr.attr,
778 &wake_unlock_attr.attr,
779#endif
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200780#ifdef CONFIG_PM_SLEEP_DEBUG
Rafael J. Wysockie516a1d2017-07-21 14:44:02 +0200781 &pm_test_attr.attr,
Sameer Nanda4b7760b2012-06-19 22:23:33 +0200782 &pm_print_times_attr.attr,
Alexandra Yatesa6f5f0d2015-09-15 10:32:46 -0700783 &pm_wakeup_irq_attr.attr,
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200784 &pm_debug_messages_attr.attr,
Rafael J. Wysocki0e7d56e2007-11-19 23:41:19 +0100785#endif
Rafael J. Wysocki0e06b4a2010-01-23 22:25:15 +0100786#endif
Li Fei957d1282013-02-01 08:56:03 +0000787#ifdef CONFIG_FREEZER
788 &pm_freeze_timeout_attr.attr,
789#endif
Rafael J. Wysockic5c6ba42006-09-25 23:32:58 -0700790 NULL,
791};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Arvind Yadav1d0c6e52017-06-30 10:22:14 +0530793static const struct attribute_group attr_group = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .attrs = g,
795};
796
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200797struct workqueue_struct *pm_wq;
Alan Stern7b199ca2009-12-03 20:22:21 +0100798EXPORT_SYMBOL_GPL(pm_wq);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200799
800static int __init pm_start_workqueue(void)
801{
Tejun Heo58a69cb2011-02-16 09:25:31 +0100802 pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200803
804 return pm_wq ? 0 : -ENOMEM;
805}
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static int __init pm_init(void)
808{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200809 int error = pm_start_workqueue();
810 if (error)
811 return error;
Rafael J. Wysockiac5c24ec2010-09-20 19:44:56 +0200812 hibernate_image_size_init();
Rafael J. Wysockiddeb6482011-05-15 11:38:48 +0200813 hibernate_reserved_size_init();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100814 pm_states_init();
Greg Kroah-Hartmand76e15f2007-11-27 11:28:26 -0800815 power_kobj = kobject_create_and_add("power", NULL);
816 if (!power_kobj)
Greg Kroah-Hartman039a5dc2007-11-01 10:39:50 -0700817 return -ENOMEM;
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200818 error = sysfs_create_group(power_kobj, &attr_group);
819 if (error)
820 return error;
Rafael J. Wysockib2df1d42012-06-21 00:19:33 +0200821 pm_print_times_init();
Rafael J. Wysocki7483b4a2012-04-29 22:53:22 +0200822 return pm_autosleep_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825core_initcall(pm_init);