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