linux/drivers/acpi/sleep.c
<<
>>
Prefs
   1/*
   2 * sleep.c - ACPI sleep support.
   3 *
   4 * Copyright (c) 2005 Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>
   5 * Copyright (c) 2004 David Shaohua Li <shaohua.li@intel.com>
   6 * Copyright (c) 2000-2003 Patrick Mochel
   7 * Copyright (c) 2003 Open Source Development Lab
   8 *
   9 * This file is released under the GPLv2.
  10 *
  11 */
  12
  13#include <linux/delay.h>
  14#include <linux/irq.h>
  15#include <linux/dmi.h>
  16#include <linux/device.h>
  17#include <linux/suspend.h>
  18#include <linux/reboot.h>
  19#include <linux/acpi.h>
  20#include <linux/module.h>
  21#include <linux/pm_runtime.h>
  22
  23#include <asm/io.h>
  24
  25#include <acpi/acpi_bus.h>
  26#include <acpi/acpi_drivers.h>
  27
  28#include "internal.h"
  29#include "sleep.h"
  30
  31static u8 sleep_states[ACPI_S_STATE_COUNT];
  32
  33static void acpi_sleep_tts_switch(u32 acpi_state)
  34{
  35        union acpi_object in_arg = { ACPI_TYPE_INTEGER };
  36        struct acpi_object_list arg_list = { 1, &in_arg };
  37        acpi_status status = AE_OK;
  38
  39        in_arg.integer.value = acpi_state;
  40        status = acpi_evaluate_object(NULL, "\\_TTS", &arg_list, NULL);
  41        if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  42                /*
  43                 * OS can't evaluate the _TTS object correctly. Some warning
  44                 * message will be printed. But it won't break anything.
  45                 */
  46                printk(KERN_NOTICE "Failure in evaluating _TTS object\n");
  47        }
  48}
  49
  50static int tts_notify_reboot(struct notifier_block *this,
  51                        unsigned long code, void *x)
  52{
  53        acpi_sleep_tts_switch(ACPI_STATE_S5);
  54        return NOTIFY_DONE;
  55}
  56
  57static struct notifier_block tts_notifier = {
  58        .notifier_call  = tts_notify_reboot,
  59        .next           = NULL,
  60        .priority       = 0,
  61};
  62
  63static int acpi_sleep_prepare(u32 acpi_state)
  64{
  65#ifdef CONFIG_ACPI_SLEEP
  66        /* do we have a wakeup address for S2 and S3? */
  67        if (acpi_state == ACPI_STATE_S3) {
  68                if (!acpi_wakeup_address)
  69                        return -EFAULT;
  70                acpi_set_firmware_waking_vector(acpi_wakeup_address);
  71
  72        }
  73        ACPI_FLUSH_CPU_CACHE();
  74#endif
  75        printk(KERN_INFO PREFIX "Preparing to enter system sleep state S%d\n",
  76                acpi_state);
  77        acpi_enable_wakeup_devices(acpi_state);
  78        acpi_enter_sleep_state_prep(acpi_state);
  79        return 0;
  80}
  81
  82#ifdef CONFIG_ACPI_SLEEP
  83static u32 acpi_target_sleep_state = ACPI_STATE_S0;
  84static bool pwr_btn_event_pending;
  85
  86/*
  87 * The ACPI specification wants us to save NVS memory regions during hibernation
  88 * and to restore them during the subsequent resume.  Windows does that also for
  89 * suspend to RAM.  However, it is known that this mechanism does not work on
  90 * all machines, so we allow the user to disable it with the help of the
  91 * 'acpi_sleep=nonvs' kernel command line option.
  92 */
  93static bool nvs_nosave;
  94
  95void __init acpi_nvs_nosave(void)
  96{
  97        nvs_nosave = true;
  98}
  99
 100/*
 101 * ACPI 1.0 wants us to execute _PTS before suspending devices, so we allow the
 102 * user to request that behavior by using the 'acpi_old_suspend_ordering'
 103 * kernel command line option that causes the following variable to be set.
 104 */
 105static bool old_suspend_ordering;
 106
 107void __init acpi_old_suspend_ordering(void)
 108{
 109        old_suspend_ordering = true;
 110}
 111
 112static int __init init_old_suspend_ordering(const struct dmi_system_id *d)
 113{
 114        acpi_old_suspend_ordering();
 115        return 0;
 116}
 117
 118static int __init init_nvs_nosave(const struct dmi_system_id *d)
 119{
 120        acpi_nvs_nosave();
 121        return 0;
 122}
 123
 124static struct dmi_system_id __initdata acpisleep_dmi_table[] = {
 125        {
 126        .callback = init_old_suspend_ordering,
 127        .ident = "Abit KN9 (nForce4 variant)",
 128        .matches = {
 129                DMI_MATCH(DMI_BOARD_VENDOR, "http://www.abit.com.tw/"),
 130                DMI_MATCH(DMI_BOARD_NAME, "KN9 Series(NF-CK804)"),
 131                },
 132        },
 133        {
 134        .callback = init_old_suspend_ordering,
 135        .ident = "HP xw4600 Workstation",
 136        .matches = {
 137                DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
 138                DMI_MATCH(DMI_PRODUCT_NAME, "HP xw4600 Workstation"),
 139                },
 140        },
 141        {
 142        .callback = init_old_suspend_ordering,
 143        .ident = "Asus Pundit P1-AH2 (M2N8L motherboard)",
 144        .matches = {
 145                DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTek Computer INC."),
 146                DMI_MATCH(DMI_BOARD_NAME, "M2N8L"),
 147                },
 148        },
 149        {
 150        .callback = init_old_suspend_ordering,
 151        .ident = "Panasonic CF51-2L",
 152        .matches = {
 153                DMI_MATCH(DMI_BOARD_VENDOR,
 154                                "Matsushita Electric Industrial Co.,Ltd."),
 155                DMI_MATCH(DMI_BOARD_NAME, "CF51-2L"),
 156                },
 157        },
 158        {
 159        .callback = init_nvs_nosave,
 160        .ident = "Sony Vaio VGN-FW21E",
 161        .matches = {
 162                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 163                DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW21E"),
 164                },
 165        },
 166        {
 167        .callback = init_nvs_nosave,
 168        .ident = "Sony Vaio VPCEB17FX",
 169        .matches = {
 170                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 171                DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB17FX"),
 172                },
 173        },
 174        {
 175        .callback = init_nvs_nosave,
 176        .ident = "Sony Vaio VGN-SR11M",
 177        .matches = {
 178                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 179                DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR11M"),
 180                },
 181        },
 182        {
 183        .callback = init_nvs_nosave,
 184        .ident = "Everex StepNote Series",
 185        .matches = {
 186                DMI_MATCH(DMI_SYS_VENDOR, "Everex Systems, Inc."),
 187                DMI_MATCH(DMI_PRODUCT_NAME, "Everex StepNote Series"),
 188                },
 189        },
 190        {
 191        .callback = init_nvs_nosave,
 192        .ident = "Sony Vaio VPCEB1Z1E",
 193        .matches = {
 194                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 195                DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1Z1E"),
 196                },
 197        },
 198        {
 199        .callback = init_nvs_nosave,
 200        .ident = "Sony Vaio VGN-NW130D",
 201        .matches = {
 202                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 203                DMI_MATCH(DMI_PRODUCT_NAME, "VGN-NW130D"),
 204                },
 205        },
 206        {
 207        .callback = init_nvs_nosave,
 208        .ident = "Sony Vaio VPCCW29FX",
 209        .matches = {
 210                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 211                DMI_MATCH(DMI_PRODUCT_NAME, "VPCCW29FX"),
 212                },
 213        },
 214        {
 215        .callback = init_nvs_nosave,
 216        .ident = "Averatec AV1020-ED2",
 217        .matches = {
 218                DMI_MATCH(DMI_SYS_VENDOR, "AVERATEC"),
 219                DMI_MATCH(DMI_PRODUCT_NAME, "1000 Series"),
 220                },
 221        },
 222        {
 223        .callback = init_old_suspend_ordering,
 224        .ident = "Asus A8N-SLI DELUXE",
 225        .matches = {
 226                DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
 227                DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI DELUXE"),
 228                },
 229        },
 230        {
 231        .callback = init_old_suspend_ordering,
 232        .ident = "Asus A8N-SLI Premium",
 233        .matches = {
 234                DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
 235                DMI_MATCH(DMI_BOARD_NAME, "A8N-SLI Premium"),
 236                },
 237        },
 238        {
 239        .callback = init_nvs_nosave,
 240        .ident = "Sony Vaio VGN-SR26GN_P",
 241        .matches = {
 242                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 243                DMI_MATCH(DMI_PRODUCT_NAME, "VGN-SR26GN_P"),
 244                },
 245        },
 246        {
 247        .callback = init_nvs_nosave,
 248        .ident = "Sony Vaio VPCEB1S1E",
 249        .matches = {
 250                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 251                DMI_MATCH(DMI_PRODUCT_NAME, "VPCEB1S1E"),
 252                },
 253        },
 254        {
 255        .callback = init_nvs_nosave,
 256        .ident = "Sony Vaio VGN-FW520F",
 257        .matches = {
 258                DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
 259                DMI_MATCH(DMI_PRODUCT_NAME, "VGN-FW520F"),
 260                },
 261        },
 262        {
 263        .callback = init_nvs_nosave,
 264        .ident = "Asus K54C",
 265        .matches = {
 266                DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
 267                DMI_MATCH(DMI_PRODUCT_NAME, "K54C"),
 268                },
 269        },
 270        {
 271        .callback = init_nvs_nosave,
 272        .ident = "Asus K54HR",
 273        .matches = {
 274                DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
 275                DMI_MATCH(DMI_PRODUCT_NAME, "K54HR"),
 276                },
 277        },
 278        {},
 279};
 280
 281static void acpi_sleep_dmi_check(void)
 282{
 283        dmi_check_system(acpisleep_dmi_table);
 284}
 285
 286/**
 287 * acpi_pm_freeze - Disable the GPEs and suspend EC transactions.
 288 */
 289static int acpi_pm_freeze(void)
 290{
 291        acpi_disable_all_gpes();
 292        acpi_os_wait_events_complete();
 293        acpi_ec_block_transactions();
 294        return 0;
 295}
 296
 297/**
 298 * acpi_pre_suspend - Enable wakeup devices, "freeze" EC and save NVS.
 299 */
 300static int acpi_pm_pre_suspend(void)
 301{
 302        acpi_pm_freeze();
 303        return suspend_nvs_save();
 304}
 305
 306/**
 307 *      __acpi_pm_prepare - Prepare the platform to enter the target state.
 308 *
 309 *      If necessary, set the firmware waking vector and do arch-specific
 310 *      nastiness to get the wakeup code to the waking vector.
 311 */
 312static int __acpi_pm_prepare(void)
 313{
 314        int error = acpi_sleep_prepare(acpi_target_sleep_state);
 315        if (error)
 316                acpi_target_sleep_state = ACPI_STATE_S0;
 317
 318        return error;
 319}
 320
 321/**
 322 *      acpi_pm_prepare - Prepare the platform to enter the target sleep
 323 *              state and disable the GPEs.
 324 */
 325static int acpi_pm_prepare(void)
 326{
 327        int error = __acpi_pm_prepare();
 328        if (!error)
 329                error = acpi_pm_pre_suspend();
 330
 331        return error;
 332}
 333
 334static int find_powerf_dev(struct device *dev, void *data)
 335{
 336        struct acpi_device *device = to_acpi_device(dev);
 337        const char *hid = acpi_device_hid(device);
 338
 339        return !strcmp(hid, ACPI_BUTTON_HID_POWERF);
 340}
 341
 342/**
 343 *      acpi_pm_finish - Instruct the platform to leave a sleep state.
 344 *
 345 *      This is called after we wake back up (or if entering the sleep state
 346 *      failed).
 347 */
 348static void acpi_pm_finish(void)
 349{
 350        struct device *pwr_btn_dev;
 351        u32 acpi_state = acpi_target_sleep_state;
 352
 353        acpi_ec_unblock_transactions();
 354        suspend_nvs_free();
 355
 356        if (acpi_state == ACPI_STATE_S0)
 357                return;
 358
 359        printk(KERN_INFO PREFIX "Waking up from system sleep state S%d\n",
 360                acpi_state);
 361        acpi_disable_wakeup_devices(acpi_state);
 362        acpi_leave_sleep_state(acpi_state);
 363
 364        /* reset firmware waking vector */
 365        acpi_set_firmware_waking_vector((acpi_physical_address) 0);
 366
 367        acpi_target_sleep_state = ACPI_STATE_S0;
 368
 369        /* If we were woken with the fixed power button, provide a small
 370         * hint to userspace in the form of a wakeup event on the fixed power
 371         * button device (if it can be found).
 372         *
 373         * We delay the event generation til now, as the PM layer requires
 374         * timekeeping to be running before we generate events. */
 375        if (!pwr_btn_event_pending)
 376                return;
 377
 378        pwr_btn_event_pending = false;
 379        pwr_btn_dev = bus_find_device(&acpi_bus_type, NULL, NULL,
 380                                      find_powerf_dev);
 381        if (pwr_btn_dev) {
 382                pm_wakeup_event(pwr_btn_dev, 0);
 383                put_device(pwr_btn_dev);
 384        }
 385}
 386
 387/**
 388 *      acpi_pm_end - Finish up suspend sequence.
 389 */
 390static void acpi_pm_end(void)
 391{
 392        /*
 393         * This is necessary in case acpi_pm_finish() is not called during a
 394         * failing transition to a sleep state.
 395         */
 396        acpi_target_sleep_state = ACPI_STATE_S0;
 397        acpi_sleep_tts_switch(acpi_target_sleep_state);
 398}
 399#else /* !CONFIG_ACPI_SLEEP */
 400#define acpi_target_sleep_state ACPI_STATE_S0
 401static inline void acpi_sleep_dmi_check(void) {}
 402#endif /* CONFIG_ACPI_SLEEP */
 403
 404#ifdef CONFIG_SUSPEND
 405static u32 acpi_suspend_states[] = {
 406        [PM_SUSPEND_ON] = ACPI_STATE_S0,
 407        [PM_SUSPEND_STANDBY] = ACPI_STATE_S1,
 408        [PM_SUSPEND_MEM] = ACPI_STATE_S3,
 409        [PM_SUSPEND_MAX] = ACPI_STATE_S5
 410};
 411
 412/**
 413 *      acpi_suspend_begin - Set the target system sleep state to the state
 414 *              associated with given @pm_state, if supported.
 415 */
 416static int acpi_suspend_begin(suspend_state_t pm_state)
 417{
 418        u32 acpi_state = acpi_suspend_states[pm_state];
 419        int error = 0;
 420
 421        error = nvs_nosave ? 0 : suspend_nvs_alloc();
 422        if (error)
 423                return error;
 424
 425        if (sleep_states[acpi_state]) {
 426                acpi_target_sleep_state = acpi_state;
 427                acpi_sleep_tts_switch(acpi_target_sleep_state);
 428        } else {
 429                printk(KERN_ERR "ACPI does not support this state: %d\n",
 430                        pm_state);
 431                error = -ENOSYS;
 432        }
 433        return error;
 434}
 435
 436/**
 437 *      acpi_suspend_enter - Actually enter a sleep state.
 438 *      @pm_state: ignored
 439 *
 440 *      Flush caches and go to sleep. For STR we have to call arch-specific
 441 *      assembly, which in turn call acpi_enter_sleep_state().
 442 *      It's unfortunate, but it works. Please fix if you're feeling frisky.
 443 */
 444static int acpi_suspend_enter(suspend_state_t pm_state)
 445{
 446        acpi_status status = AE_OK;
 447        u32 acpi_state = acpi_target_sleep_state;
 448        int error;
 449
 450        ACPI_FLUSH_CPU_CACHE();
 451
 452        switch (acpi_state) {
 453        case ACPI_STATE_S1:
 454                barrier();
 455                status = acpi_enter_sleep_state(acpi_state);
 456                break;
 457
 458        case ACPI_STATE_S3:
 459                error = acpi_suspend_lowlevel();
 460                if (error)
 461                        return error;
 462                pr_info(PREFIX "Low-level resume complete\n");
 463                break;
 464        }
 465
 466        /* This violates the spec but is required for bug compatibility. */
 467        acpi_write_bit_register(ACPI_BITREG_SCI_ENABLE, 1);
 468
 469        /* Reprogram control registers */
 470        acpi_leave_sleep_state_prep(acpi_state);
 471
 472        /* ACPI 3.0 specs (P62) says that it's the responsibility
 473         * of the OSPM to clear the status bit [ implying that the
 474         * POWER_BUTTON event should not reach userspace ]
 475         *
 476         * However, we do generate a small hint for userspace in the form of
 477         * a wakeup event. We flag this condition for now and generate the
 478         * event later, as we're currently too early in resume to be able to
 479         * generate wakeup events.
 480         */
 481        if (ACPI_SUCCESS(status) && (acpi_state == ACPI_STATE_S3)) {
 482                acpi_event_status pwr_btn_status;
 483
 484                acpi_get_event_status(ACPI_EVENT_POWER_BUTTON, &pwr_btn_status);
 485
 486                if (pwr_btn_status & ACPI_EVENT_FLAG_SET) {
 487                        acpi_clear_event(ACPI_EVENT_POWER_BUTTON);
 488                        /* Flag for later */
 489                        pwr_btn_event_pending = true;
 490                }
 491        }
 492
 493        /*
 494         * Disable and clear GPE status before interrupt is enabled. Some GPEs
 495         * (like wakeup GPE) haven't handler, this can avoid such GPE misfire.
 496         * acpi_leave_sleep_state will reenable specific GPEs later
 497         */
 498        acpi_disable_all_gpes();
 499        /* Allow EC transactions to happen. */
 500        acpi_ec_unblock_transactions_early();
 501
 502        suspend_nvs_restore();
 503
 504        return ACPI_SUCCESS(status) ? 0 : -EFAULT;
 505}
 506
 507static int acpi_suspend_state_valid(suspend_state_t pm_state)
 508{
 509        u32 acpi_state;
 510
 511        switch (pm_state) {
 512        case PM_SUSPEND_ON:
 513        case PM_SUSPEND_STANDBY:
 514        case PM_SUSPEND_MEM:
 515                acpi_state = acpi_suspend_states[pm_state];
 516
 517                return sleep_states[acpi_state];
 518        default:
 519                return 0;
 520        }
 521}
 522
 523static const struct platform_suspend_ops acpi_suspend_ops = {
 524        .valid = acpi_suspend_state_valid,
 525        .begin = acpi_suspend_begin,
 526        .prepare_late = acpi_pm_prepare,
 527        .enter = acpi_suspend_enter,
 528        .wake = acpi_pm_finish,
 529        .end = acpi_pm_end,
 530};
 531
 532/**
 533 *      acpi_suspend_begin_old - Set the target system sleep state to the
 534 *              state associated with given @pm_state, if supported, and
 535 *              execute the _PTS control method.  This function is used if the
 536 *              pre-ACPI 2.0 suspend ordering has been requested.
 537 */
 538static int acpi_suspend_begin_old(suspend_state_t pm_state)
 539{
 540        int error = acpi_suspend_begin(pm_state);
 541        if (!error)
 542                error = __acpi_pm_prepare();
 543
 544        return error;
 545}
 546
 547/*
 548 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
 549 * been requested.
 550 */
 551static const struct platform_suspend_ops acpi_suspend_ops_old = {
 552        .valid = acpi_suspend_state_valid,
 553        .begin = acpi_suspend_begin_old,
 554        .prepare_late = acpi_pm_pre_suspend,
 555        .enter = acpi_suspend_enter,
 556        .wake = acpi_pm_finish,
 557        .end = acpi_pm_end,
 558        .recover = acpi_pm_finish,
 559};
 560#endif /* CONFIG_SUSPEND */
 561
 562#ifdef CONFIG_HIBERNATION
 563static unsigned long s4_hardware_signature;
 564static struct acpi_table_facs *facs;
 565static bool nosigcheck;
 566
 567void __init acpi_no_s4_hw_signature(void)
 568{
 569        nosigcheck = true;
 570}
 571
 572static int acpi_hibernation_begin(void)
 573{
 574        int error;
 575
 576        error = nvs_nosave ? 0 : suspend_nvs_alloc();
 577        if (!error) {
 578                acpi_target_sleep_state = ACPI_STATE_S4;
 579                acpi_sleep_tts_switch(acpi_target_sleep_state);
 580        }
 581
 582        return error;
 583}
 584
 585static int acpi_hibernation_enter(void)
 586{
 587        acpi_status status = AE_OK;
 588
 589        ACPI_FLUSH_CPU_CACHE();
 590
 591        /* This shouldn't return.  If it returns, we have a problem */
 592        status = acpi_enter_sleep_state(ACPI_STATE_S4);
 593        /* Reprogram control registers */
 594        acpi_leave_sleep_state_prep(ACPI_STATE_S4);
 595
 596        return ACPI_SUCCESS(status) ? 0 : -EFAULT;
 597}
 598
 599static void acpi_hibernation_leave(void)
 600{
 601        /*
 602         * If ACPI is not enabled by the BIOS and the boot kernel, we need to
 603         * enable it here.
 604         */
 605        acpi_enable();
 606        /* Reprogram control registers */
 607        acpi_leave_sleep_state_prep(ACPI_STATE_S4);
 608        /* Check the hardware signature */
 609        if (facs && s4_hardware_signature != facs->hardware_signature) {
 610                printk(KERN_EMERG "ACPI: Hardware changed while hibernated, "
 611                        "cannot resume!\n");
 612                panic("ACPI S4 hardware signature mismatch");
 613        }
 614        /* Restore the NVS memory area */
 615        suspend_nvs_restore();
 616        /* Allow EC transactions to happen. */
 617        acpi_ec_unblock_transactions_early();
 618}
 619
 620static void acpi_pm_thaw(void)
 621{
 622        acpi_ec_unblock_transactions();
 623        acpi_enable_all_runtime_gpes();
 624}
 625
 626static const struct platform_hibernation_ops acpi_hibernation_ops = {
 627        .begin = acpi_hibernation_begin,
 628        .end = acpi_pm_end,
 629        .pre_snapshot = acpi_pm_prepare,
 630        .finish = acpi_pm_finish,
 631        .prepare = acpi_pm_prepare,
 632        .enter = acpi_hibernation_enter,
 633        .leave = acpi_hibernation_leave,
 634        .pre_restore = acpi_pm_freeze,
 635        .restore_cleanup = acpi_pm_thaw,
 636};
 637
 638/**
 639 *      acpi_hibernation_begin_old - Set the target system sleep state to
 640 *              ACPI_STATE_S4 and execute the _PTS control method.  This
 641 *              function is used if the pre-ACPI 2.0 suspend ordering has been
 642 *              requested.
 643 */
 644static int acpi_hibernation_begin_old(void)
 645{
 646        int error;
 647        /*
 648         * The _TTS object should always be evaluated before the _PTS object.
 649         * When the old_suspended_ordering is true, the _PTS object is
 650         * evaluated in the acpi_sleep_prepare.
 651         */
 652        acpi_sleep_tts_switch(ACPI_STATE_S4);
 653
 654        error = acpi_sleep_prepare(ACPI_STATE_S4);
 655
 656        if (!error) {
 657                if (!nvs_nosave)
 658                        error = suspend_nvs_alloc();
 659                if (!error)
 660                        acpi_target_sleep_state = ACPI_STATE_S4;
 661        }
 662        return error;
 663}
 664
 665/*
 666 * The following callbacks are used if the pre-ACPI 2.0 suspend ordering has
 667 * been requested.
 668 */
 669static const struct platform_hibernation_ops acpi_hibernation_ops_old = {
 670        .begin = acpi_hibernation_begin_old,
 671        .end = acpi_pm_end,
 672        .pre_snapshot = acpi_pm_pre_suspend,
 673        .prepare = acpi_pm_freeze,
 674        .finish = acpi_pm_finish,
 675        .enter = acpi_hibernation_enter,
 676        .leave = acpi_hibernation_leave,
 677        .pre_restore = acpi_pm_freeze,
 678        .restore_cleanup = acpi_pm_thaw,
 679        .recover = acpi_pm_finish,
 680};
 681#endif /* CONFIG_HIBERNATION */
 682
 683int acpi_suspend(u32 acpi_state)
 684{
 685        suspend_state_t states[] = {
 686                [1] = PM_SUSPEND_STANDBY,
 687                [3] = PM_SUSPEND_MEM,
 688                [5] = PM_SUSPEND_MAX
 689        };
 690
 691        if (acpi_state < 6 && states[acpi_state])
 692                return pm_suspend(states[acpi_state]);
 693        if (acpi_state == 4)
 694                return hibernate();
 695        return -EINVAL;
 696}
 697
 698#ifdef CONFIG_PM
 699/**
 700 *      acpi_pm_device_sleep_state - return preferred power state of ACPI device
 701 *              in the system sleep state given by %acpi_target_sleep_state
 702 *      @dev: device to examine; its driver model wakeup flags control
 703 *              whether it should be able to wake up the system
 704 *      @d_min_p: used to store the upper limit of allowed states range
 705 *      @d_max_in: specify the lowest allowed states
 706 *      Return value: preferred power state of the device on success, -ENODEV
 707 *      (ie. if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
 708 *
 709 *      Find the lowest power (highest number) ACPI device power state that
 710 *      device @dev can be in while the system is in the sleep state represented
 711 *      by %acpi_target_sleep_state.  If @wake is nonzero, the device should be
 712 *      able to wake up the system from this sleep state.  If @d_min_p is set,
 713 *      the highest power (lowest number) device power state of @dev allowed
 714 *      in this system sleep state is stored at the location pointed to by it.
 715 *
 716 *      The caller must ensure that @dev is valid before using this function.
 717 *      The caller is also responsible for figuring out if the device is
 718 *      supposed to be able to wake up the system and passing this information
 719 *      via @wake.
 720 */
 721
 722int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
 723{
 724        acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
 725        struct acpi_device *adev;
 726        char acpi_method[] = "_SxD";
 727        unsigned long long d_min, d_max;
 728
 729        if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
 730                return -EINVAL;
 731        if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
 732                printk(KERN_DEBUG "ACPI handle has no context!\n");
 733                return -ENODEV;
 734        }
 735
 736        acpi_method[2] = '0' + acpi_target_sleep_state;
 737        /*
 738         * If the sleep state is S0, the lowest limit from ACPI is D3,
 739         * but if the device has _S0W, we will use the value from _S0W
 740         * as the lowest limit from ACPI.  Finally, we will constrain
 741         * the lowest limit with the specified one.
 742         */
 743        d_min = ACPI_STATE_D0;
 744        d_max = ACPI_STATE_D3;
 745
 746        /*
 747         * If present, _SxD methods return the minimum D-state (highest power
 748         * state) we can use for the corresponding S-states.  Otherwise, the
 749         * minimum D-state is D0 (ACPI 3.x).
 750         *
 751         * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
 752         * provided -- that's our fault recovery, we ignore retval.
 753         */
 754        if (acpi_target_sleep_state > ACPI_STATE_S0)
 755                acpi_evaluate_integer(handle, acpi_method, NULL, &d_min);
 756
 757        /*
 758         * If _PRW says we can wake up the system from the target sleep state,
 759         * the D-state returned by _SxD is sufficient for that (we assume a
 760         * wakeup-aware driver if wake is set).  Still, if _SxW exists
 761         * (ACPI 3.x), it should return the maximum (lowest power) D-state that
 762         * can wake the system.  _S0W may be valid, too.
 763         */
 764        if (acpi_target_sleep_state == ACPI_STATE_S0 ||
 765            (device_may_wakeup(dev) && adev->wakeup.flags.valid &&
 766             adev->wakeup.sleep_state >= acpi_target_sleep_state)) {
 767                acpi_status status;
 768
 769                acpi_method[3] = 'W';
 770                status = acpi_evaluate_integer(handle, acpi_method, NULL,
 771                                                &d_max);
 772                if (ACPI_FAILURE(status)) {
 773                        if (acpi_target_sleep_state != ACPI_STATE_S0 ||
 774                            status != AE_NOT_FOUND)
 775                                d_max = d_min;
 776                } else if (d_max < d_min) {
 777                        /* Warn the user of the broken DSDT */
 778                        printk(KERN_WARNING "ACPI: Wrong value from %s\n",
 779                                acpi_method);
 780                        /* Sanitize it */
 781                        d_min = d_max;
 782                }
 783        }
 784
 785        if (d_max_in < d_min)
 786                return -EINVAL;
 787        if (d_min_p)
 788                *d_min_p = d_min;
 789        /* constrain d_max with specified lowest limit (max number) */
 790        if (d_max > d_max_in) {
 791                for (d_max = d_max_in; d_max > d_min; d_max--) {
 792                        if (adev->power.states[d_max].flags.valid)
 793                                break;
 794                }
 795        }
 796        return d_max;
 797}
 798EXPORT_SYMBOL(acpi_pm_device_sleep_state);
 799#endif /* CONFIG_PM */
 800
 801#ifdef CONFIG_PM_SLEEP
 802/**
 803 * acpi_pm_device_run_wake - Enable/disable wake-up for given device.
 804 * @phys_dev: Device to enable/disable the platform to wake-up the system for.
 805 * @enable: Whether enable or disable the wake-up functionality.
 806 *
 807 * Find the ACPI device object corresponding to @pci_dev and try to
 808 * enable/disable the GPE associated with it.
 809 */
 810int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
 811{
 812        struct acpi_device *dev;
 813        acpi_handle handle;
 814
 815        if (!device_run_wake(phys_dev))
 816                return -EINVAL;
 817
 818        handle = DEVICE_ACPI_HANDLE(phys_dev);
 819        if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
 820                dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
 821                        __func__);
 822                return -ENODEV;
 823        }
 824
 825        if (enable) {
 826                acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
 827                acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
 828        } else {
 829                acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
 830                acpi_disable_wakeup_device_power(dev);
 831        }
 832
 833        return 0;
 834}
 835EXPORT_SYMBOL(acpi_pm_device_run_wake);
 836
 837/**
 838 *      acpi_pm_device_sleep_wake - enable or disable the system wake-up
 839 *                                  capability of given device
 840 *      @dev: device to handle
 841 *      @enable: 'true' - enable, 'false' - disable the wake-up capability
 842 */
 843int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
 844{
 845        acpi_handle handle;
 846        struct acpi_device *adev;
 847        int error;
 848
 849        if (!device_can_wakeup(dev))
 850                return -EINVAL;
 851
 852        handle = DEVICE_ACPI_HANDLE(dev);
 853        if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
 854                dev_dbg(dev, "ACPI handle has no context in %s!\n", __func__);
 855                return -ENODEV;
 856        }
 857
 858        error = enable ?
 859                acpi_enable_wakeup_device_power(adev, acpi_target_sleep_state) :
 860                acpi_disable_wakeup_device_power(adev);
 861        if (!error)
 862                dev_info(dev, "wake-up capability %s by ACPI\n",
 863                                enable ? "enabled" : "disabled");
 864
 865        return error;
 866}
 867#endif  /* CONFIG_PM_SLEEP */
 868
 869static void acpi_power_off_prepare(void)
 870{
 871        /* Prepare to power off the system */
 872        acpi_sleep_prepare(ACPI_STATE_S5);
 873        acpi_disable_all_gpes();
 874}
 875
 876static void acpi_power_off(void)
 877{
 878        /* acpi_sleep_prepare(ACPI_STATE_S5) should have already been called */
 879        printk(KERN_DEBUG "%s called\n", __func__);
 880        local_irq_disable();
 881        acpi_enter_sleep_state(ACPI_STATE_S5);
 882}
 883
 884int __init acpi_sleep_init(void)
 885{
 886        acpi_status status;
 887        u8 type_a, type_b;
 888#ifdef CONFIG_SUSPEND
 889        int i = 0;
 890#endif
 891
 892        if (acpi_disabled)
 893                return 0;
 894
 895        acpi_sleep_dmi_check();
 896
 897        sleep_states[ACPI_STATE_S0] = 1;
 898        printk(KERN_INFO PREFIX "(supports S0");
 899
 900#ifdef CONFIG_SUSPEND
 901        for (i = ACPI_STATE_S1; i < ACPI_STATE_S4; i++) {
 902                status = acpi_get_sleep_type_data(i, &type_a, &type_b);
 903                if (ACPI_SUCCESS(status)) {
 904                        sleep_states[i] = 1;
 905                        printk(KERN_CONT " S%d", i);
 906                }
 907        }
 908
 909        suspend_set_ops(old_suspend_ordering ?
 910                &acpi_suspend_ops_old : &acpi_suspend_ops);
 911#endif
 912
 913#ifdef CONFIG_HIBERNATION
 914        status = acpi_get_sleep_type_data(ACPI_STATE_S4, &type_a, &type_b);
 915        if (ACPI_SUCCESS(status)) {
 916                hibernation_set_ops(old_suspend_ordering ?
 917                        &acpi_hibernation_ops_old : &acpi_hibernation_ops);
 918                sleep_states[ACPI_STATE_S4] = 1;
 919                printk(KERN_CONT " S4");
 920                if (!nosigcheck) {
 921                        acpi_get_table(ACPI_SIG_FACS, 1,
 922                                (struct acpi_table_header **)&facs);
 923                        if (facs)
 924                                s4_hardware_signature =
 925                                        facs->hardware_signature;
 926                }
 927        }
 928#endif
 929        status = acpi_get_sleep_type_data(ACPI_STATE_S5, &type_a, &type_b);
 930        if (ACPI_SUCCESS(status)) {
 931                sleep_states[ACPI_STATE_S5] = 1;
 932                printk(KERN_CONT " S5");
 933                pm_power_off_prepare = acpi_power_off_prepare;
 934                pm_power_off = acpi_power_off;
 935        }
 936        printk(KERN_CONT ")\n");
 937        /*
 938         * Register the tts_notifier to reboot notifier list so that the _TTS
 939         * object can also be evaluated when the system enters S5.
 940         */
 941        register_reboot_notifier(&tts_notifier);
 942        return 0;
 943}
 944
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.