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/gfp.h>
  21#include <linux/io.h>
  22#include <linux/kernel.h>
  23#include <linux/list.h>
  24#include <linux/mm.h>
  25#include <linux/slab.h>
  26#include <linux/export.h>
  27#include <linux/suspend.h>
  28#include <linux/syscore_ops.h>
  29#include <linux/swait.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_SWAIT_QUEUE_HEAD(s2idle_wait_head);
  61
  62enum s2idle_states __read_mostly s2idle_state;
  63static 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 */
  71bool pm_suspend_default_s2idle(void)
  72{
  73        return mem_sleep_current == PM_SUSPEND_TO_IDLE;
  74}
  75EXPORT_SYMBOL_GPL(pm_suspend_default_s2idle);
  76
  77void s2idle_set_ops(const struct platform_s2idle_ops *ops)
  78{
  79        lock_system_sleep();
  80        s2idle_ops = ops;
  81        unlock_system_sleep();
  82}
  83
  84static void s2idle_begin(void)
  85{
  86        s2idle_state = S2IDLE_STATE_NONE;
  87}
  88
  89static 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
 121static 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                pm_wakeup_clear(false);
 143
 144                s2idle_enter();
 145        }
 146
 147        pm_pr_dbg("resume from suspend-to-idle\n");
 148}
 149
 150void 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}
 161EXPORT_SYMBOL_GPL(s2idle_wake);
 162
 163static 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
 173void __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
 185static 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                        break;
 194                }
 195
 196        return 1;
 197}
 198__setup("mem_sleep_default=", mem_sleep_default_setup);
 199
 200/**
 201 * suspend_set_ops - Set the global suspend method table.
 202 * @ops: Suspend operations to use.
 203 */
 204void suspend_set_ops(const struct platform_suspend_ops *ops)
 205{
 206        lock_system_sleep();
 207
 208        suspend_ops = ops;
 209
 210        if (valid_state(PM_SUSPEND_STANDBY)) {
 211                mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
 212                pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
 213                if (mem_sleep_default == PM_SUSPEND_STANDBY)
 214                        mem_sleep_current = PM_SUSPEND_STANDBY;
 215        }
 216        if (valid_state(PM_SUSPEND_MEM)) {
 217                mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
 218                if (mem_sleep_default >= PM_SUSPEND_MEM)
 219                        mem_sleep_current = PM_SUSPEND_MEM;
 220        }
 221
 222        unlock_system_sleep();
 223}
 224EXPORT_SYMBOL_GPL(suspend_set_ops);
 225
 226/**
 227 * suspend_valid_only_mem - Generic memory-only valid callback.
 228 *
 229 * Platform drivers that implement mem suspend only and only need to check for
 230 * that in their .valid() callback can use this instead of rolling their own
 231 * .valid() callback.
 232 */
 233int suspend_valid_only_mem(suspend_state_t state)
 234{
 235        return state == PM_SUSPEND_MEM;
 236}
 237EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
 238
 239static bool sleep_state_supported(suspend_state_t state)
 240{
 241        return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
 242}
 243
 244static int platform_suspend_prepare(suspend_state_t state)
 245{
 246        return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
 247                suspend_ops->prepare() : 0;
 248}
 249
 250static int platform_suspend_prepare_late(suspend_state_t state)
 251{
 252        return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
 253                s2idle_ops->prepare() : 0;
 254}
 255
 256static int platform_suspend_prepare_noirq(suspend_state_t state)
 257{
 258        if (state == PM_SUSPEND_TO_IDLE)
 259                return s2idle_ops && s2idle_ops->prepare_late ?
 260                        s2idle_ops->prepare_late() : 0;
 261
 262        return suspend_ops->prepare_late ? suspend_ops->prepare_late() : 0;
 263}
 264
 265static void platform_resume_noirq(suspend_state_t state)
 266{
 267        if (state == PM_SUSPEND_TO_IDLE) {
 268                if (s2idle_ops && s2idle_ops->restore_early)
 269                        s2idle_ops->restore_early();
 270        } else if (suspend_ops->wake) {
 271                suspend_ops->wake();
 272        }
 273}
 274
 275static void platform_resume_early(suspend_state_t state)
 276{
 277        if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
 278                s2idle_ops->restore();
 279}
 280
 281static void platform_resume_finish(suspend_state_t state)
 282{
 283        if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
 284                suspend_ops->finish();
 285}
 286
 287static int platform_suspend_begin(suspend_state_t state)
 288{
 289        if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
 290                return s2idle_ops->begin();
 291        else if (suspend_ops && suspend_ops->begin)
 292                return suspend_ops->begin(state);
 293        else
 294                return 0;
 295}
 296
 297static void platform_resume_end(suspend_state_t state)
 298{
 299        if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
 300                s2idle_ops->end();
 301        else if (suspend_ops && suspend_ops->end)
 302                suspend_ops->end();
 303}
 304
 305static void platform_recover(suspend_state_t state)
 306{
 307        if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
 308                suspend_ops->recover();
 309}
 310
 311static bool platform_suspend_again(suspend_state_t state)
 312{
 313        return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
 314                suspend_ops->suspend_again() : false;
 315}
 316
 317#ifdef CONFIG_PM_DEBUG
 318static unsigned int pm_test_delay = 5;
 319module_param(pm_test_delay, uint, 0644);
 320MODULE_PARM_DESC(pm_test_delay,
 321                 "Number of seconds to wait before resuming from suspend test");
 322#endif
 323
 324static int suspend_test(int level)
 325{
 326#ifdef CONFIG_PM_DEBUG
 327        if (pm_test_level == level) {
 328                pr_info("suspend debug: Waiting for %d second(s).\n",
 329                                pm_test_delay);
 330                mdelay(pm_test_delay * 1000);
 331                return 1;
 332        }
 333#endif /* !CONFIG_PM_DEBUG */
 334        return 0;
 335}
 336
 337/**
 338 * suspend_prepare - Prepare for entering system sleep state.
 339 *
 340 * Common code run for every system sleep state that can be entered (except for
 341 * hibernation).  Run suspend notifiers, allocate the "suspend" console and
 342 * freeze processes.
 343 */
 344static int suspend_prepare(suspend_state_t state)
 345{
 346        int error, nr_calls = 0;
 347
 348        if (!sleep_state_supported(state))
 349                return -EPERM;
 350
 351        pm_prepare_console();
 352
 353        error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
 354        if (error) {
 355                nr_calls--;
 356                goto Finish;
 357        }
 358
 359        trace_suspend_resume(TPS("freeze_processes"), 0, true);
 360        error = suspend_freeze_processes();
 361        trace_suspend_resume(TPS("freeze_processes"), 0, false);
 362        if (!error)
 363                return 0;
 364
 365        suspend_stats.failed_freeze++;
 366        dpm_save_failed_step(SUSPEND_FREEZE);
 367 Finish:
 368        __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
 369        pm_restore_console();
 370        return error;
 371}
 372
 373/* default implementation */
 374void __weak arch_suspend_disable_irqs(void)
 375{
 376        local_irq_disable();
 377}
 378
 379/* default implementation */
 380void __weak arch_suspend_enable_irqs(void)
 381{
 382        local_irq_enable();
 383}
 384
 385/**
 386 * suspend_enter - Make the system enter the given sleep state.
 387 * @state: System sleep state to enter.
 388 * @wakeup: Returns information that the sleep state should not be re-entered.
 389 *
 390 * This function should be called after devices have been suspended.
 391 */
 392static int suspend_enter(suspend_state_t state, bool *wakeup)
 393{
 394        int error;
 395
 396        error = platform_suspend_prepare(state);
 397        if (error)
 398                goto Platform_finish;
 399
 400        error = dpm_suspend_late(PMSG_SUSPEND);
 401        if (error) {
 402                pr_err("late suspend of devices failed\n");
 403                goto Platform_finish;
 404        }
 405        error = platform_suspend_prepare_late(state);
 406        if (error)
 407                goto Devices_early_resume;
 408
 409        error = dpm_suspend_noirq(PMSG_SUSPEND);
 410        if (error) {
 411                pr_err("noirq suspend of devices failed\n");
 412                goto Platform_early_resume;
 413        }
 414        error = platform_suspend_prepare_noirq(state);
 415        if (error)
 416                goto Platform_wake;
 417
 418        if (suspend_test(TEST_PLATFORM))
 419                goto Platform_wake;
 420
 421        if (state == PM_SUSPEND_TO_IDLE) {
 422                s2idle_loop();
 423                goto Platform_wake;
 424        }
 425
 426        error = suspend_disable_secondary_cpus();
 427        if (error || suspend_test(TEST_CPUS))
 428                goto Enable_cpus;
 429
 430        arch_suspend_disable_irqs();
 431        BUG_ON(!irqs_disabled());
 432
 433        system_state = SYSTEM_SUSPEND;
 434
 435        error = syscore_suspend();
 436        if (!error) {
 437                *wakeup = pm_wakeup_pending();
 438                if (!(suspend_test(TEST_CORE) || *wakeup)) {
 439                        trace_suspend_resume(TPS("machine_suspend"),
 440                                state, true);
 441                        error = suspend_ops->enter(state);
 442                        trace_suspend_resume(TPS("machine_suspend"),
 443                                state, false);
 444                } else if (*wakeup) {
 445                        error = -EBUSY;
 446                }
 447                syscore_resume();
 448        }
 449
 450        system_state = SYSTEM_RUNNING;
 451
 452        arch_suspend_enable_irqs();
 453        BUG_ON(irqs_disabled());
 454
 455 Enable_cpus:
 456        suspend_enable_secondary_cpus();
 457
 458 Platform_wake:
 459        platform_resume_noirq(state);
 460        dpm_resume_noirq(PMSG_RESUME);
 461
 462 Platform_early_resume:
 463        platform_resume_early(state);
 464
 465 Devices_early_resume:
 466        dpm_resume_early(PMSG_RESUME);
 467
 468 Platform_finish:
 469        platform_resume_finish(state);
 470        return error;
 471}
 472
 473/**
 474 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
 475 * @state: System sleep state to enter.
 476 */
 477int suspend_devices_and_enter(suspend_state_t state)
 478{
 479        int error;
 480        bool wakeup = false;
 481
 482        if (!sleep_state_supported(state))
 483                return -ENOSYS;
 484
 485        pm_suspend_target_state = state;
 486
 487        if (state == PM_SUSPEND_TO_IDLE)
 488                pm_set_suspend_no_platform();
 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 */
 533static 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 */
 548static 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 */
 606int 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}
 624EXPORT_SYMBOL(pm_suspend);
 625