Home | History | Annotate | Line # | Download | only in hardware
hwsleep.c revision 1.1.1.2.10.1
      1           1.1  jruoho /******************************************************************************
      2           1.1  jruoho  *
      3  1.1.1.2.10.1    yamt  * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
      4  1.1.1.2.10.1    yamt  *                   original/legacy sleep/PM registers.
      5           1.1  jruoho  *
      6           1.1  jruoho  *****************************************************************************/
      7           1.1  jruoho 
      8       1.1.1.2  jruoho /*
      9  1.1.1.2.10.1    yamt  * Copyright (C) 2000 - 2013, Intel Corp.
     10           1.1  jruoho  * All rights reserved.
     11           1.1  jruoho  *
     12       1.1.1.2  jruoho  * Redistribution and use in source and binary forms, with or without
     13       1.1.1.2  jruoho  * modification, are permitted provided that the following conditions
     14       1.1.1.2  jruoho  * are met:
     15       1.1.1.2  jruoho  * 1. Redistributions of source code must retain the above copyright
     16       1.1.1.2  jruoho  *    notice, this list of conditions, and the following disclaimer,
     17       1.1.1.2  jruoho  *    without modification.
     18       1.1.1.2  jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     19       1.1.1.2  jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     20       1.1.1.2  jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     21       1.1.1.2  jruoho  *    including a substantially similar Disclaimer requirement for further
     22       1.1.1.2  jruoho  *    binary redistribution.
     23       1.1.1.2  jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     24       1.1.1.2  jruoho  *    of any contributors may be used to endorse or promote products derived
     25       1.1.1.2  jruoho  *    from this software without specific prior written permission.
     26       1.1.1.2  jruoho  *
     27       1.1.1.2  jruoho  * Alternatively, this software may be distributed under the terms of the
     28       1.1.1.2  jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     29       1.1.1.2  jruoho  * Software Foundation.
     30       1.1.1.2  jruoho  *
     31       1.1.1.2  jruoho  * NO WARRANTY
     32       1.1.1.2  jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     33       1.1.1.2  jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     34       1.1.1.2  jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     35       1.1.1.2  jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     36       1.1.1.2  jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37       1.1.1.2  jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38       1.1.1.2  jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39       1.1.1.2  jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     40       1.1.1.2  jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     41       1.1.1.2  jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     42       1.1.1.2  jruoho  * POSSIBILITY OF SUCH DAMAGES.
     43       1.1.1.2  jruoho  */
     44           1.1  jruoho 
     45           1.1  jruoho #include "acpi.h"
     46           1.1  jruoho #include "accommon.h"
     47           1.1  jruoho 
     48           1.1  jruoho #define _COMPONENT          ACPI_HARDWARE
     49           1.1  jruoho         ACPI_MODULE_NAME    ("hwsleep")
     50           1.1  jruoho 
     51           1.1  jruoho 
     52  1.1.1.2.10.1    yamt #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
     53           1.1  jruoho /*******************************************************************************
     54           1.1  jruoho  *
     55  1.1.1.2.10.1    yamt  * FUNCTION:    AcpiHwLegacySleep
     56           1.1  jruoho  *
     57           1.1  jruoho  * PARAMETERS:  SleepState          - Which sleep state to enter
     58           1.1  jruoho  *
     59           1.1  jruoho  * RETURN:      Status
     60           1.1  jruoho  *
     61  1.1.1.2.10.1    yamt  * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
     62           1.1  jruoho  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
     63           1.1  jruoho  *
     64           1.1  jruoho  ******************************************************************************/
     65           1.1  jruoho 
     66           1.1  jruoho ACPI_STATUS
     67  1.1.1.2.10.1    yamt AcpiHwLegacySleep (
     68           1.1  jruoho     UINT8                   SleepState)
     69           1.1  jruoho {
     70           1.1  jruoho     ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
     71           1.1  jruoho     ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
     72  1.1.1.2.10.1    yamt     UINT32                  Pm1aControl;
     73  1.1.1.2.10.1    yamt     UINT32                  Pm1bControl;
     74           1.1  jruoho     UINT32                  InValue;
     75           1.1  jruoho     ACPI_STATUS             Status;
     76           1.1  jruoho 
     77           1.1  jruoho 
     78  1.1.1.2.10.1    yamt     ACPI_FUNCTION_TRACE (HwLegacySleep);
     79           1.1  jruoho 
     80           1.1  jruoho 
     81  1.1.1.2.10.1    yamt     SleepTypeRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
     82           1.1  jruoho     SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
     83           1.1  jruoho 
     84           1.1  jruoho     /* Clear wake status */
     85           1.1  jruoho 
     86           1.1  jruoho     Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
     87           1.1  jruoho     if (ACPI_FAILURE (Status))
     88           1.1  jruoho     {
     89           1.1  jruoho         return_ACPI_STATUS (Status);
     90           1.1  jruoho     }
     91           1.1  jruoho 
     92           1.1  jruoho     /* Clear all fixed and general purpose status bits */
     93           1.1  jruoho 
     94           1.1  jruoho     Status = AcpiHwClearAcpiStatus ();
     95           1.1  jruoho     if (ACPI_FAILURE (Status))
     96           1.1  jruoho     {
     97           1.1  jruoho         return_ACPI_STATUS (Status);
     98           1.1  jruoho     }
     99           1.1  jruoho 
    100           1.1  jruoho     /*
    101           1.1  jruoho      * 1) Disable/Clear all GPEs
    102           1.1  jruoho      * 2) Enable all wakeup GPEs
    103           1.1  jruoho      */
    104           1.1  jruoho     Status = AcpiHwDisableAllGpes ();
    105           1.1  jruoho     if (ACPI_FAILURE (Status))
    106           1.1  jruoho     {
    107           1.1  jruoho         return_ACPI_STATUS (Status);
    108           1.1  jruoho     }
    109           1.1  jruoho     AcpiGbl_SystemAwakeAndRunning = FALSE;
    110           1.1  jruoho 
    111           1.1  jruoho     Status = AcpiHwEnableAllWakeupGpes ();
    112           1.1  jruoho     if (ACPI_FAILURE (Status))
    113           1.1  jruoho     {
    114           1.1  jruoho         return_ACPI_STATUS (Status);
    115           1.1  jruoho     }
    116           1.1  jruoho 
    117           1.1  jruoho     /* Get current value of PM1A control */
    118           1.1  jruoho 
    119           1.1  jruoho     Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
    120           1.1  jruoho                 &Pm1aControl);
    121           1.1  jruoho     if (ACPI_FAILURE (Status))
    122           1.1  jruoho     {
    123           1.1  jruoho         return_ACPI_STATUS (Status);
    124           1.1  jruoho     }
    125           1.1  jruoho     ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
    126           1.1  jruoho         "Entering sleep state [S%u]\n", SleepState));
    127           1.1  jruoho 
    128           1.1  jruoho     /* Clear the SLP_EN and SLP_TYP fields */
    129           1.1  jruoho 
    130           1.1  jruoho     Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
    131           1.1  jruoho                      SleepEnableRegInfo->AccessBitMask);
    132           1.1  jruoho     Pm1bControl = Pm1aControl;
    133           1.1  jruoho 
    134           1.1  jruoho     /* Insert the SLP_TYP bits */
    135           1.1  jruoho 
    136           1.1  jruoho     Pm1aControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
    137           1.1  jruoho     Pm1bControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
    138           1.1  jruoho 
    139           1.1  jruoho     /*
    140           1.1  jruoho      * We split the writes of SLP_TYP and SLP_EN to workaround
    141           1.1  jruoho      * poorly implemented hardware.
    142           1.1  jruoho      */
    143           1.1  jruoho 
    144           1.1  jruoho     /* Write #1: write the SLP_TYP data to the PM1 Control registers */
    145           1.1  jruoho 
    146           1.1  jruoho     Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
    147           1.1  jruoho     if (ACPI_FAILURE (Status))
    148           1.1  jruoho     {
    149           1.1  jruoho         return_ACPI_STATUS (Status);
    150           1.1  jruoho     }
    151           1.1  jruoho 
    152           1.1  jruoho     /* Insert the sleep enable (SLP_EN) bit */
    153           1.1  jruoho 
    154           1.1  jruoho     Pm1aControl |= SleepEnableRegInfo->AccessBitMask;
    155           1.1  jruoho     Pm1bControl |= SleepEnableRegInfo->AccessBitMask;
    156           1.1  jruoho 
    157           1.1  jruoho     /* Flush caches, as per ACPI specification */
    158           1.1  jruoho 
    159           1.1  jruoho     ACPI_FLUSH_CPU_CACHE ();
    160           1.1  jruoho 
    161           1.1  jruoho     /* Write #2: Write both SLP_TYP + SLP_EN */
    162           1.1  jruoho 
    163           1.1  jruoho     Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
    164           1.1  jruoho     if (ACPI_FAILURE (Status))
    165           1.1  jruoho     {
    166           1.1  jruoho         return_ACPI_STATUS (Status);
    167           1.1  jruoho     }
    168           1.1  jruoho 
    169           1.1  jruoho     if (SleepState > ACPI_STATE_S3)
    170           1.1  jruoho     {
    171           1.1  jruoho         /*
    172           1.1  jruoho          * We wanted to sleep > S3, but it didn't happen (by virtue of the
    173           1.1  jruoho          * fact that we are still executing!)
    174           1.1  jruoho          *
    175           1.1  jruoho          * Wait ten seconds, then try again. This is to get S4/S5 to work on
    176           1.1  jruoho          * all machines.
    177           1.1  jruoho          *
    178           1.1  jruoho          * We wait so long to allow chipsets that poll this reg very slowly
    179           1.1  jruoho          * to still read the right value. Ideally, this block would go
    180           1.1  jruoho          * away entirely.
    181           1.1  jruoho          */
    182  1.1.1.2.10.1    yamt         AcpiOsStall (10 * ACPI_USEC_PER_SEC);
    183           1.1  jruoho 
    184           1.1  jruoho         Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_CONTROL,
    185           1.1  jruoho                     SleepEnableRegInfo->AccessBitMask);
    186           1.1  jruoho         if (ACPI_FAILURE (Status))
    187           1.1  jruoho         {
    188           1.1  jruoho             return_ACPI_STATUS (Status);
    189           1.1  jruoho         }
    190           1.1  jruoho     }
    191           1.1  jruoho 
    192  1.1.1.2.10.1    yamt     /* Wait for transition back to Working State */
    193           1.1  jruoho 
    194           1.1  jruoho     do
    195           1.1  jruoho     {
    196           1.1  jruoho         Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
    197           1.1  jruoho         if (ACPI_FAILURE (Status))
    198           1.1  jruoho         {
    199           1.1  jruoho             return_ACPI_STATUS (Status);
    200           1.1  jruoho         }
    201           1.1  jruoho 
    202           1.1  jruoho     } while (!InValue);
    203           1.1  jruoho 
    204           1.1  jruoho     return_ACPI_STATUS (AE_OK);
    205           1.1  jruoho }
    206           1.1  jruoho 
    207           1.1  jruoho 
    208           1.1  jruoho /*******************************************************************************
    209           1.1  jruoho  *
    210  1.1.1.2.10.1    yamt  * FUNCTION:    AcpiHwLegacyWakePrep
    211           1.1  jruoho  *
    212           1.1  jruoho  * PARAMETERS:  SleepState          - Which sleep state we just exited
    213           1.1  jruoho  *
    214           1.1  jruoho  * RETURN:      Status
    215           1.1  jruoho  *
    216  1.1.1.2.10.1    yamt  * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
    217  1.1.1.2.10.1    yamt  *              sleep.
    218           1.1  jruoho  *              Called with interrupts ENABLED.
    219           1.1  jruoho  *
    220           1.1  jruoho  ******************************************************************************/
    221           1.1  jruoho 
    222           1.1  jruoho ACPI_STATUS
    223  1.1.1.2.10.1    yamt AcpiHwLegacyWakePrep (
    224           1.1  jruoho     UINT8                   SleepState)
    225           1.1  jruoho {
    226           1.1  jruoho     ACPI_STATUS             Status;
    227           1.1  jruoho     ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
    228           1.1  jruoho     ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
    229           1.1  jruoho     UINT32                  Pm1aControl;
    230           1.1  jruoho     UINT32                  Pm1bControl;
    231           1.1  jruoho 
    232           1.1  jruoho 
    233  1.1.1.2.10.1    yamt     ACPI_FUNCTION_TRACE (HwLegacyWakePrep);
    234           1.1  jruoho 
    235           1.1  jruoho     /*
    236           1.1  jruoho      * Set SLP_TYPE and SLP_EN to state S0.
    237           1.1  jruoho      * This is unclear from the ACPI Spec, but it is required
    238           1.1  jruoho      * by some machines.
    239           1.1  jruoho      */
    240           1.1  jruoho     Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
    241           1.1  jruoho                     &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
    242           1.1  jruoho     if (ACPI_SUCCESS (Status))
    243           1.1  jruoho     {
    244           1.1  jruoho         SleepTypeRegInfo =
    245           1.1  jruoho             AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
    246           1.1  jruoho         SleepEnableRegInfo =
    247           1.1  jruoho             AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
    248           1.1  jruoho 
    249           1.1  jruoho         /* Get current value of PM1A control */
    250           1.1  jruoho 
    251           1.1  jruoho         Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
    252           1.1  jruoho                     &Pm1aControl);
    253           1.1  jruoho         if (ACPI_SUCCESS (Status))
    254           1.1  jruoho         {
    255           1.1  jruoho             /* Clear the SLP_EN and SLP_TYP fields */
    256           1.1  jruoho 
    257           1.1  jruoho             Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
    258           1.1  jruoho                 SleepEnableRegInfo->AccessBitMask);
    259           1.1  jruoho             Pm1bControl = Pm1aControl;
    260           1.1  jruoho 
    261           1.1  jruoho             /* Insert the SLP_TYP bits */
    262           1.1  jruoho 
    263           1.1  jruoho             Pm1aControl |= (AcpiGbl_SleepTypeA <<
    264           1.1  jruoho                 SleepTypeRegInfo->BitPosition);
    265           1.1  jruoho             Pm1bControl |= (AcpiGbl_SleepTypeB <<
    266           1.1  jruoho                 SleepTypeRegInfo->BitPosition);
    267           1.1  jruoho 
    268           1.1  jruoho             /* Write the control registers and ignore any errors */
    269           1.1  jruoho 
    270           1.1  jruoho             (void) AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
    271           1.1  jruoho         }
    272           1.1  jruoho     }
    273           1.1  jruoho 
    274  1.1.1.2.10.1    yamt     return_ACPI_STATUS (Status);
    275  1.1.1.2.10.1    yamt }
    276           1.1  jruoho 
    277           1.1  jruoho 
    278  1.1.1.2.10.1    yamt /*******************************************************************************
    279  1.1.1.2.10.1    yamt  *
    280  1.1.1.2.10.1    yamt  * FUNCTION:    AcpiHwLegacyWake
    281  1.1.1.2.10.1    yamt  *
    282  1.1.1.2.10.1    yamt  * PARAMETERS:  SleepState          - Which sleep state we just exited
    283  1.1.1.2.10.1    yamt  *
    284  1.1.1.2.10.1    yamt  * RETURN:      Status
    285  1.1.1.2.10.1    yamt  *
    286  1.1.1.2.10.1    yamt  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
    287  1.1.1.2.10.1    yamt  *              Called with interrupts ENABLED.
    288  1.1.1.2.10.1    yamt  *
    289  1.1.1.2.10.1    yamt  ******************************************************************************/
    290  1.1.1.2.10.1    yamt 
    291  1.1.1.2.10.1    yamt ACPI_STATUS
    292  1.1.1.2.10.1    yamt AcpiHwLegacyWake (
    293  1.1.1.2.10.1    yamt     UINT8                   SleepState)
    294  1.1.1.2.10.1    yamt {
    295  1.1.1.2.10.1    yamt     ACPI_STATUS             Status;
    296           1.1  jruoho 
    297           1.1  jruoho 
    298  1.1.1.2.10.1    yamt     ACPI_FUNCTION_TRACE (HwLegacyWake);
    299           1.1  jruoho 
    300           1.1  jruoho 
    301  1.1.1.2.10.1    yamt     /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
    302           1.1  jruoho 
    303  1.1.1.2.10.1    yamt     AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
    304  1.1.1.2.10.1    yamt     AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__SST), ACPI_SST_WAKING);
    305           1.1  jruoho 
    306           1.1  jruoho     /*
    307  1.1.1.2.10.1    yamt      * GPEs must be enabled before _WAK is called as GPEs
    308  1.1.1.2.10.1    yamt      * might get fired there
    309  1.1.1.2.10.1    yamt      *
    310           1.1  jruoho      * Restore the GPEs:
    311           1.1  jruoho      * 1) Disable/Clear all GPEs
    312           1.1  jruoho      * 2) Enable all runtime GPEs
    313           1.1  jruoho      */
    314           1.1  jruoho     Status = AcpiHwDisableAllGpes ();
    315           1.1  jruoho     if (ACPI_FAILURE (Status))
    316           1.1  jruoho     {
    317           1.1  jruoho         return_ACPI_STATUS (Status);
    318           1.1  jruoho     }
    319           1.1  jruoho 
    320           1.1  jruoho     Status = AcpiHwEnableAllRuntimeGpes ();
    321           1.1  jruoho     if (ACPI_FAILURE (Status))
    322           1.1  jruoho     {
    323           1.1  jruoho         return_ACPI_STATUS (Status);
    324           1.1  jruoho     }
    325           1.1  jruoho 
    326  1.1.1.2.10.1    yamt     /*
    327  1.1.1.2.10.1    yamt      * Now we can execute _WAK, etc. Some machines require that the GPEs
    328  1.1.1.2.10.1    yamt      * are enabled before the wake methods are executed.
    329  1.1.1.2.10.1    yamt      */
    330  1.1.1.2.10.1    yamt     AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__WAK), SleepState);
    331  1.1.1.2.10.1    yamt 
    332  1.1.1.2.10.1    yamt     /*
    333  1.1.1.2.10.1    yamt      * Some BIOS code assumes that WAK_STS will be cleared on resume
    334  1.1.1.2.10.1    yamt      * and use it to determine whether the system is rebooting or
    335  1.1.1.2.10.1    yamt      * resuming. Clear WAK_STS for compatibility.
    336  1.1.1.2.10.1    yamt      */
    337  1.1.1.2.10.1    yamt     (void) AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
    338  1.1.1.2.10.1    yamt     AcpiGbl_SystemAwakeAndRunning = TRUE;
    339  1.1.1.2.10.1    yamt 
    340           1.1  jruoho     /* Enable power button */
    341           1.1  jruoho 
    342           1.1  jruoho     (void) AcpiWriteBitRegister(
    343           1.1  jruoho             AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,
    344           1.1  jruoho             ACPI_ENABLE_EVENT);
    345           1.1  jruoho 
    346           1.1  jruoho     (void) AcpiWriteBitRegister(
    347           1.1  jruoho             AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,
    348           1.1  jruoho             ACPI_CLEAR_STATUS);
    349           1.1  jruoho 
    350  1.1.1.2.10.1    yamt     AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__SST), ACPI_SST_WORKING);
    351           1.1  jruoho     return_ACPI_STATUS (Status);
    352           1.1  jruoho }
    353           1.1  jruoho 
    354  1.1.1.2.10.1    yamt #endif /* !ACPI_REDUCED_HARDWARE */
    355