| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * kernel/power/suspend.c - Suspend to RAM and standby functionality. |
| 4 | * |
| 5 | * Copyright (c) 2003 Patrick Mochel |
| 6 | * Copyright (c) 2003 Open Source Development Lab |
| 7 | * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) "PM: " fmt |
| 11 | |
| 12 | #include <linux/string.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/console.h> |
| 17 | #include <linux/cpu.h> |
| 18 | #include <linux/cpuidle.h> |
| 19 | #include <linux/gfp.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/list.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/export.h> |
| 26 | #include <linux/suspend.h> |
| 27 | #include <linux/syscore_ops.h> |
| 28 | #include <linux/swait.h> |
| 29 | #include <linux/ftrace.h> |
| 30 | #include <trace/events/power.h> |
| 31 | #include <linux/compiler.h> |
| 32 | #include <linux/moduleparam.h> |
| 33 | #include <linux/wakeup_reason.h> |
| 34 | |
| 35 | #include "power.h" |
| 36 | |
| 37 | const char * const pm_labels[] = { |
| 38 | [PM_SUSPEND_TO_IDLE] = "freeze", |
| 39 | [PM_SUSPEND_STANDBY] = "standby", |
| 40 | [PM_SUSPEND_MEM] = "mem", |
| 41 | }; |
| 42 | const char *pm_states[PM_SUSPEND_MAX]; |
| 43 | static const char * const mem_sleep_labels[] = { |
| 44 | [PM_SUSPEND_TO_IDLE] = "s2idle", |
| 45 | [PM_SUSPEND_STANDBY] = "shallow", |
| 46 | [PM_SUSPEND_MEM] = "deep", |
| 47 | }; |
| 48 | const char *mem_sleep_states[PM_SUSPEND_MAX]; |
| 49 | |
| 50 | suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE; |
| 51 | suspend_state_t mem_sleep_default = PM_SUSPEND_MAX; |
| 52 | suspend_state_t pm_suspend_target_state; |
| 53 | EXPORT_SYMBOL_GPL(pm_suspend_target_state); |
| 54 | |
| 55 | unsigned int pm_suspend_global_flags; |
| 56 | EXPORT_SYMBOL_GPL(pm_suspend_global_flags); |
| 57 | |
| 58 | static const struct platform_suspend_ops *suspend_ops; |
| 59 | static const struct platform_s2idle_ops *s2idle_ops; |
| 60 | static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head); |
| 61 | |
| 62 | enum s2idle_states __read_mostly s2idle_state; |
| 63 | static DEFINE_RAW_SPINLOCK(s2idle_lock); |
| 64 | |
| 65 | /** |
| 66 | * pm_suspend_default_s2idle - Check if suspend-to-idle is the default suspend. |
| 67 | * |
| 68 | * Return 'true' if suspend-to-idle has been selected as the default system |
| 69 | * suspend method. |
| 70 | */ |
| 71 | bool pm_suspend_default_s2idle(void) |
| 72 | { |
| 73 | return mem_sleep_current == PM_SUSPEND_TO_IDLE; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(pm_suspend_default_s2idle); |
| 76 | |
| 77 | void s2idle_set_ops(const struct platform_s2idle_ops *ops) |
| 78 | { |
| 79 | lock_system_sleep(); |
| 80 | s2idle_ops = ops; |
| 81 | unlock_system_sleep(); |
| 82 | } |
| 83 | |
| 84 | static void s2idle_begin(void) |
| 85 | { |
| 86 | s2idle_state = S2IDLE_STATE_NONE; |
| 87 | } |
| 88 | |
| 89 | static void s2idle_enter(void) |
| 90 | { |
| 91 | trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true); |
| 92 | |
| 93 | raw_spin_lock_irq(&s2idle_lock); |
| 94 | if (pm_wakeup_pending()) |
| 95 | goto out; |
| 96 | |
| 97 | s2idle_state = S2IDLE_STATE_ENTER; |
| 98 | raw_spin_unlock_irq(&s2idle_lock); |
| 99 | |
| 100 | get_online_cpus(); |
| 101 | cpuidle_resume(); |
| 102 | |
| 103 | /* Push all the CPUs into the idle loop. */ |
| 104 | wake_up_all_idle_cpus(); |
| 105 | /* Make the current CPU wait so it can enter the idle loop too. */ |
| 106 | swait_event_exclusive(s2idle_wait_head, |
| 107 | s2idle_state == S2IDLE_STATE_WAKE); |
| 108 | |
| 109 | cpuidle_pause(); |
| 110 | put_online_cpus(); |
| 111 | |
| 112 | raw_spin_lock_irq(&s2idle_lock); |
| 113 | |
| 114 | out: |
| 115 | s2idle_state = S2IDLE_STATE_NONE; |
| 116 | raw_spin_unlock_irq(&s2idle_lock); |
| 117 | |
| 118 | trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false); |
| 119 | } |
| 120 | |
| 121 | static void s2idle_loop(void) |
| 122 | { |
| 123 | pm_pr_dbg("suspend-to-idle\n"); |
| 124 | |
| 125 | /* |
| 126 | * Suspend-to-idle equals: |
| 127 | * frozen processes + suspended devices + idle processors. |
| 128 | * Thus s2idle_enter() should be called right after all devices have |
| 129 | * been suspended. |
| 130 | * |
| 131 | * Wakeups during the noirq suspend of devices may be spurious, so try |
| 132 | * to avoid them upfront. |
| 133 | */ |
| 134 | for (;;) { |
| 135 | if (s2idle_ops && s2idle_ops->wake) { |
| 136 | if (s2idle_ops->wake()) |
| 137 | break; |
| 138 | } else if (pm_wakeup_pending()) { |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | clear_wakeup_reasons(); |
| 143 | |
| 144 | s2idle_enter(); |
| 145 | } |
| 146 | |
| 147 | pm_pr_dbg("resume from suspend-to-idle\n"); |
| 148 | } |
| 149 | |
| 150 | void s2idle_wake(void) |
| 151 | { |
| 152 | unsigned long flags; |
| 153 | |
| 154 | raw_spin_lock_irqsave(&s2idle_lock, flags); |
| 155 | if (s2idle_state > S2IDLE_STATE_NONE) { |
| 156 | s2idle_state = S2IDLE_STATE_WAKE; |
| 157 | swake_up_one(&s2idle_wait_head); |
| 158 | } |
| 159 | raw_spin_unlock_irqrestore(&s2idle_lock, flags); |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(s2idle_wake); |
| 162 | |
| 163 | static bool valid_state(suspend_state_t state) |
| 164 | { |
| 165 | /* |
| 166 | * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level |
| 167 | * support and need to be valid to the low level |
| 168 | * implementation, no valid callback implies that none are valid. |
| 169 | */ |
| 170 | return suspend_ops && suspend_ops->valid && suspend_ops->valid(state); |
| 171 | } |
| 172 | |
| 173 | void __init pm_states_init(void) |
| 174 | { |
| 175 | /* "mem" and "freeze" are always present in /sys/power/state. */ |
| 176 | pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM]; |
| 177 | pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE]; |
| 178 | /* |
| 179 | * Suspend-to-idle should be supported even without any suspend_ops, |
| 180 | * initialize mem_sleep_states[] accordingly here. |
| 181 | */ |
| 182 | mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE]; |
| 183 | } |
| 184 | |
| 185 | static int __init mem_sleep_default_setup(char *str) |
| 186 | { |
| 187 | suspend_state_t state; |
| 188 | |
| 189 | for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++) |
| 190 | if (mem_sleep_labels[state] && |
| 191 | !strcmp(str, mem_sleep_labels[state])) { |
| 192 | mem_sleep_default = state; |
| 193 | mem_sleep_current = state; |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | return 1; |
| 198 | } |
| 199 | __setup("mem_sleep_default=", mem_sleep_default_setup); |
| 200 | |
| 201 | /** |
| 202 | * suspend_set_ops - Set the global suspend method table. |
| 203 | * @ops: Suspend operations to use. |
| 204 | */ |
| 205 | void suspend_set_ops(const struct platform_suspend_ops *ops) |
| 206 | { |
| 207 | lock_system_sleep(); |
| 208 | |
| 209 | suspend_ops = ops; |
| 210 | |
| 211 | if (valid_state(PM_SUSPEND_STANDBY)) { |
| 212 | mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY]; |
| 213 | pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY]; |
| 214 | if (mem_sleep_default == PM_SUSPEND_STANDBY) |
| 215 | mem_sleep_current = PM_SUSPEND_STANDBY; |
| 216 | } |
| 217 | if (valid_state(PM_SUSPEND_MEM)) { |
| 218 | mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM]; |
| 219 | if (mem_sleep_default >= PM_SUSPEND_MEM) |
| 220 | mem_sleep_current = PM_SUSPEND_MEM; |
| 221 | } |
| 222 | |
| 223 | unlock_system_sleep(); |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(suspend_set_ops); |
| 226 | |
| 227 | /** |
| 228 | * suspend_valid_only_mem - Generic memory-only valid callback. |
| 229 | * |
| 230 | * Platform drivers that implement mem suspend only and only need to check for |
| 231 | * that in their .valid() callback can use this instead of rolling their own |
| 232 | * .valid() callback. |
| 233 | */ |
| 234 | int suspend_valid_only_mem(suspend_state_t state) |
| 235 | { |
| 236 | return state == PM_SUSPEND_MEM; |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(suspend_valid_only_mem); |
| 239 | |
| 240 | static bool sleep_state_supported(suspend_state_t state) |
| 241 | { |
| 242 | return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter); |
| 243 | } |
| 244 | |
| 245 | static int platform_suspend_prepare(suspend_state_t state) |
| 246 | { |
| 247 | return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ? |
| 248 | suspend_ops->prepare() : 0; |
| 249 | } |
| 250 | |
| 251 | static int platform_suspend_prepare_late(suspend_state_t state) |
| 252 | { |
| 253 | return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ? |
| 254 | s2idle_ops->prepare() : 0; |
| 255 | } |
| 256 | |
| 257 | static int platform_suspend_prepare_noirq(suspend_state_t state) |
| 258 | { |
| 259 | if (state == PM_SUSPEND_TO_IDLE) |
| 260 | return s2idle_ops && s2idle_ops->prepare_late ? |
| 261 | s2idle_ops->prepare_late() : 0; |
| 262 | |
| 263 | return suspend_ops->prepare_late ? suspend_ops->prepare_late() : 0; |
| 264 | } |
| 265 | |
| 266 | static void platform_resume_noirq(suspend_state_t state) |
| 267 | { |
| 268 | if (state == PM_SUSPEND_TO_IDLE) { |
| 269 | if (s2idle_ops && s2idle_ops->restore_early) |
| 270 | s2idle_ops->restore_early(); |
| 271 | } else if (suspend_ops->wake) { |
| 272 | suspend_ops->wake(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void platform_resume_early(suspend_state_t state) |
| 277 | { |
| 278 | if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore) |
| 279 | s2idle_ops->restore(); |
| 280 | } |
| 281 | |
| 282 | static void platform_resume_finish(suspend_state_t state) |
| 283 | { |
| 284 | if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish) |
| 285 | suspend_ops->finish(); |
| 286 | } |
| 287 | |
| 288 | static int platform_suspend_begin(suspend_state_t state) |
| 289 | { |
| 290 | if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin) |
| 291 | return s2idle_ops->begin(); |
| 292 | else if (suspend_ops && suspend_ops->begin) |
| 293 | return suspend_ops->begin(state); |
| 294 | else |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static void platform_resume_end(suspend_state_t state) |
| 299 | { |
| 300 | if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end) |
| 301 | s2idle_ops->end(); |
| 302 | else if (suspend_ops && suspend_ops->end) |
| 303 | suspend_ops->end(); |
| 304 | } |
| 305 | |
| 306 | static void platform_recover(suspend_state_t state) |
| 307 | { |
| 308 | if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover) |
| 309 | suspend_ops->recover(); |
| 310 | } |
| 311 | |
| 312 | static bool platform_suspend_again(suspend_state_t state) |
| 313 | { |
| 314 | return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ? |
| 315 | suspend_ops->suspend_again() : false; |
| 316 | } |
| 317 | |
| 318 | #ifdef CONFIG_PM_DEBUG |
| 319 | static unsigned int pm_test_delay = 5; |
| 320 | module_param(pm_test_delay, uint, 0644); |
| 321 | MODULE_PARM_DESC(pm_test_delay, |
| 322 | "Number of seconds to wait before resuming from suspend test"); |
| 323 | #endif |
| 324 | |
| 325 | static int suspend_test(int level) |
| 326 | { |
| 327 | #ifdef CONFIG_PM_DEBUG |
| 328 | if (pm_test_level == level) { |
| 329 | pr_info("suspend debug: Waiting for %d second(s).\n", |
| 330 | pm_test_delay); |
| 331 | mdelay(pm_test_delay * 1000); |
| 332 | return 1; |
| 333 | } |
| 334 | #endif /* !CONFIG_PM_DEBUG */ |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * suspend_prepare - Prepare for entering system sleep state. |
| 340 | * |
| 341 | * Common code run for every system sleep state that can be entered (except for |
| 342 | * hibernation). Run suspend notifiers, allocate the "suspend" console and |
| 343 | * freeze processes. |
| 344 | */ |
| 345 | static int suspend_prepare(suspend_state_t state) |
| 346 | { |
| 347 | int error, nr_calls = 0; |
| 348 | |
| 349 | if (!sleep_state_supported(state)) |
| 350 | return -EPERM; |
| 351 | |
| 352 | pm_prepare_console(); |
| 353 | |
| 354 | error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls); |
| 355 | if (error) { |
| 356 | nr_calls--; |
| 357 | goto Finish; |
| 358 | } |
| 359 | |
| 360 | trace_suspend_resume(TPS("freeze_processes"), 0, true); |
| 361 | error = suspend_freeze_processes(); |
| 362 | trace_suspend_resume(TPS("freeze_processes"), 0, false); |
| 363 | if (!error) |
| 364 | return 0; |
| 365 | |
| 366 | log_suspend_abort_reason("One or more tasks refusing to freeze"); |
| 367 | suspend_stats.failed_freeze++; |
| 368 | dpm_save_failed_step(SUSPEND_FREEZE); |
| 369 | Finish: |
| 370 | __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL); |
| 371 | pm_restore_console(); |
| 372 | return error; |
| 373 | } |
| 374 | |
| 375 | /* default implementation */ |
| 376 | void __weak arch_suspend_disable_irqs(void) |
| 377 | { |
| 378 | local_irq_disable(); |
| 379 | } |
| 380 | |
| 381 | /* default implementation */ |
| 382 | void __weak arch_suspend_enable_irqs(void) |
| 383 | { |
| 384 | local_irq_enable(); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * suspend_enter - Make the system enter the given sleep state. |
| 389 | * @state: System sleep state to enter. |
| 390 | * @wakeup: Returns information that the sleep state should not be re-entered. |
| 391 | * |
| 392 | * This function should be called after devices have been suspended. |
| 393 | */ |
| 394 | static int suspend_enter(suspend_state_t state, bool *wakeup) |
| 395 | { |
| 396 | int error, last_dev; |
| 397 | |
| 398 | error = platform_suspend_prepare(state); |
| 399 | if (error) |
| 400 | goto Platform_finish; |
| 401 | |
| 402 | error = dpm_suspend_late(PMSG_SUSPEND); |
| 403 | if (error) { |
| 404 | last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1; |
| 405 | last_dev %= REC_FAILED_NUM; |
| 406 | pr_err("late suspend of devices failed\n"); |
| 407 | log_suspend_abort_reason("late suspend of %s device failed", |
| 408 | suspend_stats.failed_devs[last_dev]); |
| 409 | goto Platform_finish; |
| 410 | } |
| 411 | error = platform_suspend_prepare_late(state); |
| 412 | if (error) |
| 413 | goto Devices_early_resume; |
| 414 | |
| 415 | error = dpm_suspend_noirq(PMSG_SUSPEND); |
| 416 | if (error) { |
| 417 | last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1; |
| 418 | last_dev %= REC_FAILED_NUM; |
| 419 | pr_err("noirq suspend of devices failed\n"); |
| 420 | log_suspend_abort_reason("noirq suspend of %s device failed", |
| 421 | suspend_stats.failed_devs[last_dev]); |
| 422 | goto Platform_early_resume; |
| 423 | } |
| 424 | error = platform_suspend_prepare_noirq(state); |
| 425 | if (error) |
| 426 | goto Platform_wake; |
| 427 | |
| 428 | if (suspend_test(TEST_PLATFORM)) |
| 429 | goto Platform_wake; |
| 430 | |
| 431 | if (state == PM_SUSPEND_TO_IDLE) { |
| 432 | s2idle_loop(); |
| 433 | goto Platform_wake; |
| 434 | } |
| 435 | |
| 436 | error = suspend_disable_secondary_cpus(); |
| 437 | if (error || suspend_test(TEST_CPUS)) { |
| 438 | log_suspend_abort_reason("Disabling non-boot cpus failed"); |
| 439 | goto Enable_cpus; |
| 440 | } |
| 441 | |
| 442 | arch_suspend_disable_irqs(); |
| 443 | BUG_ON(!irqs_disabled()); |
| 444 | |
| 445 | system_state = SYSTEM_SUSPEND; |
| 446 | |
| 447 | error = syscore_suspend(); |
| 448 | if (!error) { |
| 449 | *wakeup = pm_wakeup_pending(); |
| 450 | if (!(suspend_test(TEST_CORE) || *wakeup)) { |
| 451 | trace_suspend_resume(TPS("machine_suspend"), |
| 452 | state, true); |
| 453 | error = suspend_ops->enter(state); |
| 454 | trace_suspend_resume(TPS("machine_suspend"), |
| 455 | state, false); |
| 456 | } else if (*wakeup) { |
| 457 | error = -EBUSY; |
| 458 | } |
| 459 | syscore_resume(); |
| 460 | } |
| 461 | |
| 462 | system_state = SYSTEM_RUNNING; |
| 463 | |
| 464 | arch_suspend_enable_irqs(); |
| 465 | BUG_ON(irqs_disabled()); |
| 466 | |
| 467 | Enable_cpus: |
| 468 | suspend_enable_secondary_cpus(); |
| 469 | |
| 470 | Platform_wake: |
| 471 | platform_resume_noirq(state); |
| 472 | dpm_resume_noirq(PMSG_RESUME); |
| 473 | |
| 474 | Platform_early_resume: |
| 475 | platform_resume_early(state); |
| 476 | |
| 477 | Devices_early_resume: |
| 478 | dpm_resume_early(PMSG_RESUME); |
| 479 | |
| 480 | Platform_finish: |
| 481 | platform_resume_finish(state); |
| 482 | return error; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * suspend_devices_and_enter - Suspend devices and enter system sleep state. |
| 487 | * @state: System sleep state to enter. |
| 488 | */ |
| 489 | int suspend_devices_and_enter(suspend_state_t state) |
| 490 | { |
| 491 | int error; |
| 492 | bool wakeup = false; |
| 493 | |
| 494 | if (!sleep_state_supported(state)) |
| 495 | return -ENOSYS; |
| 496 | |
| 497 | pm_suspend_target_state = state; |
| 498 | |
| 499 | if (state == PM_SUSPEND_TO_IDLE) |
| 500 | pm_set_suspend_no_platform(); |
| 501 | |
| 502 | error = platform_suspend_begin(state); |
| 503 | if (error) |
| 504 | goto Close; |
| 505 | |
| 506 | suspend_console(); |
| 507 | suspend_test_start(); |
| 508 | error = dpm_suspend_start(PMSG_SUSPEND); |
| 509 | if (error) { |
| 510 | pr_err("Some devices failed to suspend, or early wake event detected\n"); |
| 511 | log_suspend_abort_reason( |
| 512 | "Some devices failed to suspend, or early wake event detected"); |
| 513 | goto Recover_platform; |
| 514 | } |
| 515 | suspend_test_finish("suspend devices"); |
| 516 | if (suspend_test(TEST_DEVICES)) |
| 517 | goto Recover_platform; |
| 518 | |
| 519 | do { |
| 520 | error = suspend_enter(state, &wakeup); |
| 521 | } while (!error && !wakeup && platform_suspend_again(state)); |
| 522 | |
| 523 | Resume_devices: |
| 524 | suspend_test_start(); |
| 525 | dpm_resume_end(PMSG_RESUME); |
| 526 | suspend_test_finish("resume devices"); |
| 527 | trace_suspend_resume(TPS("resume_console"), state, true); |
| 528 | resume_console(); |
| 529 | trace_suspend_resume(TPS("resume_console"), state, false); |
| 530 | |
| 531 | Close: |
| 532 | platform_resume_end(state); |
| 533 | pm_suspend_target_state = PM_SUSPEND_ON; |
| 534 | return error; |
| 535 | |
| 536 | Recover_platform: |
| 537 | platform_recover(state); |
| 538 | goto Resume_devices; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * suspend_finish - Clean up before finishing the suspend sequence. |
| 543 | * |
| 544 | * Call platform code to clean up, restart processes, and free the console that |
| 545 | * we've allocated. This routine is not called for hibernation. |
| 546 | */ |
| 547 | static void suspend_finish(void) |
| 548 | { |
| 549 | suspend_thaw_processes(); |
| 550 | pm_notifier_call_chain(PM_POST_SUSPEND); |
| 551 | pm_restore_console(); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * enter_state - Do common work needed to enter system sleep state. |
| 556 | * @state: System sleep state to enter. |
| 557 | * |
| 558 | * Make sure that no one else is trying to put the system into a sleep state. |
| 559 | * Fail if that's not the case. Otherwise, prepare for system suspend, make the |
| 560 | * system enter the given sleep state and clean up after wakeup. |
| 561 | */ |
| 562 | static int enter_state(suspend_state_t state) |
| 563 | { |
| 564 | int error; |
| 565 | |
| 566 | trace_suspend_resume(TPS("suspend_enter"), state, true); |
| 567 | if (state == PM_SUSPEND_TO_IDLE) { |
| 568 | #ifdef CONFIG_PM_DEBUG |
| 569 | if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) { |
| 570 | pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n"); |
| 571 | return -EAGAIN; |
| 572 | } |
| 573 | #endif |
| 574 | } else if (!valid_state(state)) { |
| 575 | return -EINVAL; |
| 576 | } |
| 577 | if (!mutex_trylock(&system_transition_mutex)) |
| 578 | return -EBUSY; |
| 579 | |
| 580 | if (state == PM_SUSPEND_TO_IDLE) |
| 581 | s2idle_begin(); |
| 582 | |
| 583 | if (!IS_ENABLED(CONFIG_SUSPEND_SKIP_SYNC)) { |
| 584 | trace_suspend_resume(TPS("sync_filesystems"), 0, true); |
| 585 | ksys_sync_helper(); |
| 586 | trace_suspend_resume(TPS("sync_filesystems"), 0, false); |
| 587 | } |
| 588 | |
| 589 | pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]); |
| 590 | pm_suspend_clear_flags(); |
| 591 | error = suspend_prepare(state); |
| 592 | if (error) |
| 593 | goto Unlock; |
| 594 | |
| 595 | if (suspend_test(TEST_FREEZER)) |
| 596 | goto Finish; |
| 597 | |
| 598 | trace_suspend_resume(TPS("suspend_enter"), state, false); |
| 599 | pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]); |
| 600 | pm_restrict_gfp_mask(); |
| 601 | error = suspend_devices_and_enter(state); |
| 602 | pm_restore_gfp_mask(); |
| 603 | |
| 604 | Finish: |
| 605 | events_check_enabled = false; |
| 606 | pm_pr_dbg("Finishing wakeup.\n"); |
| 607 | suspend_finish(); |
| 608 | Unlock: |
| 609 | mutex_unlock(&system_transition_mutex); |
| 610 | return error; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * pm_suspend - Externally visible function for suspending the system. |
| 615 | * @state: System sleep state to enter. |
| 616 | * |
| 617 | * Check if the value of @state represents one of the supported states, |
| 618 | * execute enter_state() and update system suspend statistics. |
| 619 | */ |
| 620 | int pm_suspend(suspend_state_t state) |
| 621 | { |
| 622 | int error; |
| 623 | |
| 624 | if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX) |
| 625 | return -EINVAL; |
| 626 | |
| 627 | pr_info("suspend entry (%s)\n", mem_sleep_labels[state]); |
| 628 | error = enter_state(state); |
| 629 | if (error) { |
| 630 | suspend_stats.fail++; |
| 631 | dpm_save_failed_errno(error); |
| 632 | } else { |
| 633 | suspend_stats.success++; |
| 634 | } |
| 635 | pr_info("suspend exit\n"); |
| 636 | return error; |
| 637 | } |
| 638 | EXPORT_SYMBOL(pm_suspend); |