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