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