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