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