Thomas Gleixner | 55716d2 | 2019-06-01 10:08:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/kobject.h> |
| 11 | #include <linux/string.h> |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 12 | #include <linux/pm-trace.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 13 | #include <linux/workqueue.h> |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 14 | #include <linux/debugfs.h> |
| 15 | #include <linux/seq_file.h> |
Pingfan Liu | 55f2503 | 2018-07-31 16:51:32 +0800 | [diff] [blame] | 16 | #include <linux/suspend.h> |
Harry Pan | b5dee31 | 2019-02-25 20:36:41 +0800 | [diff] [blame] | 17 | #include <linux/syscalls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include "power.h" |
| 20 | |
Rafael J. Wysocki | cd51e61c | 2011-02-11 00:04:52 +0100 | [diff] [blame] | 21 | #ifdef CONFIG_PM_SLEEP |
| 22 | |
Bart Van Assche | 4bf236a | 2018-01-05 09:19:08 -0800 | [diff] [blame] | 23 | void lock_system_sleep(void) |
| 24 | { |
| 25 | current->flags |= PF_FREEZER_SKIP; |
Pingfan Liu | 55f2503 | 2018-07-31 16:51:32 +0800 | [diff] [blame] | 26 | mutex_lock(&system_transition_mutex); |
Bart Van Assche | 4bf236a | 2018-01-05 09:19:08 -0800 | [diff] [blame] | 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 |
Pingfan Liu | 55f2503 | 2018-07-31 16:51:32 +0800 | [diff] [blame] | 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. |
Bart Van Assche | 4bf236a | 2018-01-05 09:19:08 -0800 | [diff] [blame] | 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; |
Pingfan Liu | 55f2503 | 2018-07-31 16:51:32 +0800 | [diff] [blame] | 49 | mutex_unlock(&system_transition_mutex); |
Bart Van Assche | 4bf236a | 2018-01-05 09:19:08 -0800 | [diff] [blame] | 50 | } |
| 51 | EXPORT_SYMBOL_GPL(unlock_system_sleep); |
| 52 | |
Harry Pan | b5dee31 | 2019-02-25 20:36:41 +0800 | [diff] [blame] | 53 | void ksys_sync_helper(void) |
| 54 | { |
Harry Pan | c64546b | 2019-02-25 20:36:43 +0800 | [diff] [blame] | 55 | ktime_t start; |
| 56 | long elapsed_msecs; |
| 57 | |
| 58 | start = ktime_get(); |
Harry Pan | b5dee31 | 2019-02-25 20:36:41 +0800 | [diff] [blame] | 59 | ksys_sync(); |
Harry Pan | c64546b | 2019-02-25 20:36:43 +0800 | [diff] [blame] | 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); |
Harry Pan | b5dee31 | 2019-02-25 20:36:41 +0800 | [diff] [blame] | 63 | } |
| 64 | EXPORT_SYMBOL_GPL(ksys_sync_helper); |
| 65 | |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 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 | |
Lianwei Wang | ea00f4f | 2016-06-19 23:52:27 -0700 | [diff] [blame] | 82 | int __pm_notifier_call_chain(unsigned long val, int nr_to_call, int *nr_calls) |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 83 | { |
Lianwei Wang | ea00f4f | 2016-06-19 23:52:27 -0700 | [diff] [blame] | 84 | int ret; |
| 85 | |
| 86 | ret = __blocking_notifier_call_chain(&pm_chain_head, val, NULL, |
| 87 | nr_to_call, nr_calls); |
Akinobu Mita | f0c077a | 2011-07-08 20:53:36 +0200 | [diff] [blame] | 88 | |
| 89 | return notifier_to_errno(ret); |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 90 | } |
Lianwei Wang | ea00f4f | 2016-06-19 23:52:27 -0700 | [diff] [blame] | 91 | int pm_notifier_call_chain(unsigned long val) |
| 92 | { |
| 93 | return __pm_notifier_call_chain(val, -1, NULL); |
| 94 | } |
Alan Stern | 8252575 | 2007-11-19 23:49:18 +0100 | [diff] [blame] | 95 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 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 | |
Daniel Walter | 883ee4f | 2012-10-23 01:20:35 +0200 | [diff] [blame] | 110 | if (kstrtoul(buf, 10, &val)) |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 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 | |
Rafael J. Wysocki | e516a1d | 2017-07-21 14:44:02 +0200 | [diff] [blame] | 194 | #ifdef CONFIG_PM_SLEEP_DEBUG |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 195 | int pm_test_level = TEST_NONE; |
| 196 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 206 | static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 207 | char *buf) |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 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 | |
Rafael J. Wysocki | 039a75c | 2008-01-29 00:29:06 +0100 | [diff] [blame] | 227 | static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 228 | const char *buf, size_t n) |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 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 | |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 239 | lock_system_sleep(); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 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 | |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 249 | unlock_system_sleep(); |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 250 | |
| 251 | return error ? error : n; |
| 252 | } |
| 253 | |
| 254 | power_attr(pm_test); |
Rafael J. Wysocki | e516a1d | 2017-07-21 14:44:02 +0200 | [diff] [blame] | 255 | #endif /* CONFIG_PM_SLEEP_DEBUG */ |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 256 | |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 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; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 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", |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 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, |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 295 | "failed_suspend_late", |
| 296 | suspend_stats.failed_suspend_late, |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 297 | "failed_suspend_noirq", |
| 298 | suspend_stats.failed_suspend_noirq, |
| 299 | "failed_resume", suspend_stats.failed_resume, |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 300 | "failed_resume_early", |
| 301 | suspend_stats.failed_resume_early, |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 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 | } |
Yangtao Li | 943a10f | 2018-12-11 11:20:48 -0500 | [diff] [blame] | 333 | DEFINE_SHOW_ATTRIBUTE(suspend_stats); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 334 | |
| 335 | static int __init pm_debugfs_init(void) |
| 336 | { |
| 337 | debugfs_create_file("suspend_stats", S_IFREG | S_IRUGO, |
Yangtao Li | 943a10f | 2018-12-11 11:20:48 -0500 | [diff] [blame] | 338 | NULL, NULL, &suspend_stats_fops); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | late_initcall(pm_debugfs_init); |
| 343 | #endif /* CONFIG_DEBUG_FS */ |
| 344 | |
Rafael J. Wysocki | ca12310 | 2011-08-11 22:38:12 +0200 | [diff] [blame] | 345 | #endif /* CONFIG_PM_SLEEP */ |
| 346 | |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 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 | } |
Alexandra Yates | a6f5f0d | 2015-09-15 10:32:46 -0700 | [diff] [blame] | 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 | |
Rafael J. Wysocki | a1e9ca6 | 2016-01-02 03:09:16 +0100 | [diff] [blame] | 392 | power_attr_ro(pm_wakeup_irq); |
Alexandra Yates | a6f5f0d | 2015-09-15 10:32:46 -0700 | [diff] [blame] | 393 | |
Srinivas Pandruvada | 726fb6b | 2017-08-15 18:16:59 -0700 | [diff] [blame] | 394 | bool pm_debug_messages_on __read_mostly; |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 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 | /** |
Rafael J. Wysocki | cb08e03 | 2017-07-23 00:03:43 +0200 | [diff] [blame] | 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. |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 423 | * @fmt: Message format. |
| 424 | * |
| 425 | * The message will be emitted if enabled through the pm_debug_messages |
| 426 | * sysfs attribute. |
| 427 | */ |
Rafael J. Wysocki | cb08e03 | 2017-07-23 00:03:43 +0200 | [diff] [blame] | 428 | void __pm_pr_dbg(bool defer, const char *fmt, ...) |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 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 | |
Rafael J. Wysocki | cb08e03 | 2017-07-23 00:03:43 +0200 | [diff] [blame] | 441 | if (defer) |
| 442 | printk_deferred(KERN_DEBUG "PM: %pV", &vaf); |
| 443 | else |
| 444 | printk(KERN_DEBUG "PM: %pV", &vaf); |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 445 | |
| 446 | va_end(args); |
| 447 | } |
| 448 | |
Rafael J. Wysocki | 819b1bb | 2015-05-13 15:31:12 +0200 | [diff] [blame] | 449 | #else /* !CONFIG_PM_SLEEP_DEBUG */ |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 450 | static inline void pm_print_times_init(void) {} |
| 451 | #endif /* CONFIG_PM_SLEEP_DEBUG */ |
| 452 | |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 453 | struct kobject *power_kobj; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | /** |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 456 | * state - control system sleep states. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | * |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 458 | * show() returns available sleep state labels, which may be "mem", "standby", |
Mauro Carvalho Chehab | 44348e8a | 2018-06-14 12:34:32 -0300 | [diff] [blame] | 459 | * "freeze" and "disk" (hibernation). |
| 460 | * See Documentation/admin-guide/pm/sleep-states.rst for a description of |
| 461 | * what they mean. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | * |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 463 | * store() accepts one of those strings, translates it into the proper |
| 464 | * enumerated value, and initiates a suspend transition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | */ |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 466 | static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 467 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 469 | char *s = buf; |
| 470 | #ifdef CONFIG_SUSPEND |
Rafael J. Wysocki | 27ddcc6 | 2014-05-26 13:40:47 +0200 | [diff] [blame] | 471 | suspend_state_t i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Rafael J. Wysocki | 27ddcc6 | 2014-05-26 13:40:47 +0200 | [diff] [blame] | 473 | for (i = PM_SUSPEND_MIN; i < PM_SUSPEND_MAX; i++) |
Rafael J. Wysocki | d431cbc | 2014-07-15 22:02:11 +0200 | [diff] [blame] | 474 | if (pm_states[i]) |
| 475 | s += sprintf(s,"%s ", pm_states[i]); |
Rafael J. Wysocki | 27ddcc6 | 2014-05-26 13:40:47 +0200 | [diff] [blame] | 476 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 477 | #endif |
Kees Cook | a6e15a3 | 2014-06-13 13:30:35 -0700 | [diff] [blame] | 478 | if (hibernation_available()) |
| 479 | s += sprintf(s, "disk "); |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 480 | if (s != buf) |
| 481 | /* convert the last space to a newline */ |
| 482 | *(s-1) = '\n'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return (s - buf); |
| 484 | } |
| 485 | |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 486 | static suspend_state_t decode_state(const char *buf, size_t n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 488 | #ifdef CONFIG_SUSPEND |
Rafael J. Wysocki | d431cbc | 2014-07-15 22:02:11 +0200 | [diff] [blame] | 489 | suspend_state_t state; |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 490 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | int len; |
| 493 | |
| 494 | p = memchr(buf, '\n', n); |
| 495 | len = p ? p - buf : n; |
| 496 | |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 497 | /* Check hibernation first. */ |
| 498 | if (len == 4 && !strncmp(buf, "disk", len)) |
| 499 | return PM_SUSPEND_MAX; |
Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 500 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 501 | #ifdef CONFIG_SUSPEND |
Rafael J. Wysocki | d431cbc | 2014-07-15 22:02:11 +0200 | [diff] [blame] | 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 | } |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 508 | #endif |
| 509 | |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 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); |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 529 | if (state < PM_SUSPEND_MAX) { |
| 530 | if (state == PM_SUSPEND_MEM) |
| 531 | state = mem_sleep_current; |
| 532 | |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 533 | error = pm_suspend(state); |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 534 | } else if (state == PM_SUSPEND_MAX) { |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 535 | error = hibernate(); |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 536 | } else { |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 537 | error = -EINVAL; |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 538 | } |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 539 | |
| 540 | out: |
| 541 | pm_autosleep_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return error ? error : n; |
| 543 | } |
| 544 | |
| 545 | power_attr(state); |
| 546 | |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 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 |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 570 | * 'wakeup_count'. If that fails, at least one wakeup event has occurred since |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 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 | { |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 580 | unsigned int val; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 581 | |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 582 | return pm_get_wakeup_count(&val, true) ? |
| 583 | sprintf(buf, "%u\n", val) : -EINTR; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 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 | { |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 590 | unsigned int val; |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 591 | int error; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 592 | |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 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; |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 603 | if (sscanf(buf, "%u", &val) == 1) { |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 604 | if (pm_save_wakeup_count(val)) |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 605 | error = n; |
Julius Werner | bb177fe | 2013-06-12 12:55:22 -0700 | [diff] [blame] | 606 | else |
| 607 | pm_print_active_wakeup_sources(); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 608 | } |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 609 | |
| 610 | out: |
| 611 | pm_autosleep_unlock(); |
| 612 | return error; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | power_attr(wakeup_count); |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 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) |
Rafael J. Wysocki | d431cbc | 2014-07-15 22:02:11 +0200 | [diff] [blame] | 629 | return sprintf(buf, "%s\n", pm_states[state] ? |
| 630 | pm_states[state] : "error"); |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 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 |
Arve Hjønnevåg | 040e5bf | 2012-05-04 00:14:21 +0200 | [diff] [blame] | 647 | && strcmp(buf, "off") && strcmp(buf, "off\n")) |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 648 | return -EINVAL; |
| 649 | |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 650 | if (state == PM_SUSPEND_MEM) |
| 651 | state = mem_sleep_current; |
| 652 | |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 653 | error = pm_autosleep_set_state(state); |
| 654 | return error ? error : n; |
| 655 | } |
| 656 | |
| 657 | power_attr(autosleep); |
| 658 | #endif /* CONFIG_PM_AUTOSLEEP */ |
Rafael J. Wysocki | b86ff982 | 2012-04-29 22:53:42 +0200 | [diff] [blame] | 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 */ |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 696 | #endif /* CONFIG_PM_SLEEP */ |
| 697 | |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 698 | #ifdef CONFIG_PM_TRACE |
| 699 | int pm_trace_enabled; |
| 700 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 701 | static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, |
| 702 | char *buf) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 703 | { |
| 704 | return sprintf(buf, "%d\n", pm_trace_enabled); |
| 705 | } |
| 706 | |
| 707 | static ssize_t |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 708 | pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, |
| 709 | const char *buf, size_t n) |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 710 | { |
| 711 | int val; |
| 712 | |
| 713 | if (sscanf(buf, "%d", &val) == 1) { |
| 714 | pm_trace_enabled = !!val; |
Shuah Khan | 9dceefe | 2013-06-26 16:27:35 -0600 | [diff] [blame] | 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 | } |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 719 | return n; |
| 720 | } |
| 721 | return -EINVAL; |
| 722 | } |
| 723 | |
| 724 | power_attr(pm_trace); |
James Hogan | d33ac60 | 2010-10-12 00:00:25 +0200 | [diff] [blame] | 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 | |
Rafael J. Wysocki | a1e9ca6 | 2016-01-02 03:09:16 +0100 | [diff] [blame] | 733 | power_attr_ro(pm_trace_dev_match); |
James Hogan | d33ac60 | 2010-10-12 00:00:25 +0200 | [diff] [blame] | 734 | |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 735 | #endif /* CONFIG_PM_TRACE */ |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 736 | |
Li Fei | 957d128 | 2013-02-01 08:56:03 +0000 | [diff] [blame] | 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 | |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 761 | static struct attribute * g[] = { |
| 762 | &state_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 763 | #ifdef CONFIG_PM_TRACE |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 764 | &pm_trace_attr.attr, |
James Hogan | d33ac60 | 2010-10-12 00:00:25 +0200 | [diff] [blame] | 765 | &pm_trace_dev_match_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 766 | #endif |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 767 | #ifdef CONFIG_PM_SLEEP |
| 768 | &pm_async_attr.attr, |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 769 | &wakeup_count_attr.attr, |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 770 | #ifdef CONFIG_SUSPEND |
| 771 | &mem_sleep_attr.attr, |
| 772 | #endif |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 773 | #ifdef CONFIG_PM_AUTOSLEEP |
| 774 | &autosleep_attr.attr, |
| 775 | #endif |
Rafael J. Wysocki | b86ff982 | 2012-04-29 22:53:42 +0200 | [diff] [blame] | 776 | #ifdef CONFIG_PM_WAKELOCKS |
| 777 | &wake_lock_attr.attr, |
| 778 | &wake_unlock_attr.attr, |
| 779 | #endif |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 780 | #ifdef CONFIG_PM_SLEEP_DEBUG |
Rafael J. Wysocki | e516a1d | 2017-07-21 14:44:02 +0200 | [diff] [blame] | 781 | &pm_test_attr.attr, |
Sameer Nanda | 4b7760b | 2012-06-19 22:23:33 +0200 | [diff] [blame] | 782 | &pm_print_times_attr.attr, |
Alexandra Yates | a6f5f0d | 2015-09-15 10:32:46 -0700 | [diff] [blame] | 783 | &pm_wakeup_irq_attr.attr, |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 784 | &pm_debug_messages_attr.attr, |
Rafael J. Wysocki | 0e7d56e | 2007-11-19 23:41:19 +0100 | [diff] [blame] | 785 | #endif |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 786 | #endif |
Li Fei | 957d128 | 2013-02-01 08:56:03 +0000 | [diff] [blame] | 787 | #ifdef CONFIG_FREEZER |
| 788 | &pm_freeze_timeout_attr.attr, |
| 789 | #endif |
Rafael J. Wysocki | c5c6ba4 | 2006-09-25 23:32:58 -0700 | [diff] [blame] | 790 | NULL, |
| 791 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Arvind Yadav | 1d0c6e5 | 2017-06-30 10:22:14 +0530 | [diff] [blame] | 793 | static const struct attribute_group attr_group = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | .attrs = g, |
| 795 | }; |
| 796 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 797 | struct workqueue_struct *pm_wq; |
Alan Stern | 7b199ca | 2009-12-03 20:22:21 +0100 | [diff] [blame] | 798 | EXPORT_SYMBOL_GPL(pm_wq); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 799 | |
| 800 | static int __init pm_start_workqueue(void) |
| 801 | { |
Tejun Heo | 58a69cb | 2011-02-16 09:25:31 +0100 | [diff] [blame] | 802 | pm_wq = alloc_workqueue("pm", WQ_FREEZABLE, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 803 | |
| 804 | return pm_wq ? 0 : -ENOMEM; |
| 805 | } |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | static int __init pm_init(void) |
| 808 | { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 809 | int error = pm_start_workqueue(); |
| 810 | if (error) |
| 811 | return error; |
Rafael J. Wysocki | ac5c24ec | 2010-09-20 19:44:56 +0200 | [diff] [blame] | 812 | hibernate_image_size_init(); |
Rafael J. Wysocki | ddeb648 | 2011-05-15 11:38:48 +0200 | [diff] [blame] | 813 | hibernate_reserved_size_init(); |
Sudeep Holla | fa7fd6f | 2016-08-19 14:41:00 +0100 | [diff] [blame] | 814 | pm_states_init(); |
Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 815 | power_kobj = kobject_create_and_add("power", NULL); |
| 816 | if (!power_kobj) |
Greg Kroah-Hartman | 039a5dc | 2007-11-01 10:39:50 -0700 | [diff] [blame] | 817 | return -ENOMEM; |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 818 | error = sysfs_create_group(power_kobj, &attr_group); |
| 819 | if (error) |
| 820 | return error; |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 821 | pm_print_times_init(); |
Rafael J. Wysocki | 7483b4a | 2012-04-29 22:53:22 +0200 | [diff] [blame] | 822 | return pm_autosleep_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | core_initcall(pm_init); |