hwsleep.c revision 193267
171867Smsmith
271867Smsmith/******************************************************************************
371867Smsmith *
471867Smsmith * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
5128212Snjl *
671867Smsmith *****************************************************************************/
771867Smsmith
871867Smsmith/******************************************************************************
971867Smsmith *
1071867Smsmith * 1. Copyright Notice
1171867Smsmith *
1271867Smsmith * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13126372Snjl * All rights reserved.
1471867Smsmith *
1571867Smsmith * 2. License
1671867Smsmith *
1771867Smsmith * 2.1. This is your license from Intel Corp. under its intellectual property
1871867Smsmith * rights.  You may have additional license terms from the party that provided
1971867Smsmith * you this software, covering your right to use that party's intellectual
2071867Smsmith * property rights.
2171867Smsmith *
2271867Smsmith * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2371867Smsmith * copy of the source code appearing in this file ("Covered Code") an
2471867Smsmith * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2571867Smsmith * base code distributed originally by Intel ("Original Intel Code") to copy,
2671867Smsmith * make derivatives, distribute, use and display any portion of the Covered
2771867Smsmith * Code in any form, with the right to sublicense such rights; and
2871867Smsmith *
2971867Smsmith * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
3071867Smsmith * license (with the right to sublicense), under only those claims of Intel
3171867Smsmith * patents that are infringed by the Original Intel Code, to make, use, sell,
3271867Smsmith * offer to sell, and import the Covered Code and derivative works thereof
3371867Smsmith * solely to the minimum extent necessary to exercise the above copyright
3471867Smsmith * license, and in no event shall the patent license extend to any additions
3571867Smsmith * to or modifications of the Original Intel Code.  No other license or right
3671867Smsmith * is granted directly or by implication, estoppel or otherwise;
3771867Smsmith *
3871867Smsmith * The above copyright and patent license is granted only if the following
3971867Smsmith * conditions are met:
4071867Smsmith *
4171867Smsmith * 3. Conditions
4271867Smsmith *
4371867Smsmith * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4471867Smsmith * Redistribution of source code of any substantial portion of the Covered
4571867Smsmith * Code or modification with rights to further distribute source must include
4671867Smsmith * the above Copyright Notice, the above License, this list of Conditions,
4771867Smsmith * and the following Disclaimer and Export Compliance provision.  In addition,
4871867Smsmith * Licensee must cause all Covered Code to which Licensee contributes to
4971867Smsmith * contain a file documenting the changes Licensee made to create that Covered
5071867Smsmith * Code and the date of any change.  Licensee must include in that file the
5171867Smsmith * documentation of any changes made by any predecessor Licensee.  Licensee
5271867Smsmith * must include a prominent statement that the modification is derived,
5371867Smsmith * directly or indirectly, from Original Intel Code.
5471867Smsmith *
5571867Smsmith * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5671867Smsmith * Redistribution of source code of any substantial portion of the Covered
5771867Smsmith * Code or modification without rights to further distribute source must
5871867Smsmith * include the following Disclaimer and Export Compliance provision in the
5971867Smsmith * documentation and/or other materials provided with distribution.  In
6071867Smsmith * addition, Licensee may not authorize further sublicense of source of any
6171867Smsmith * portion of the Covered Code, and must include terms to the effect that the
6271867Smsmith * license from Licensee to its licensee is limited to the intellectual
6371867Smsmith * property embodied in the software Licensee provides to its licensee, and
6471867Smsmith * not to intellectual property embodied in modifications its licensee may
6571867Smsmith * make.
6671867Smsmith *
6771867Smsmith * 3.3. Redistribution of Executable. Redistribution in executable form of any
6871867Smsmith * substantial portion of the Covered Code or modification must reproduce the
6971867Smsmith * above Copyright Notice, and the following Disclaimer and Export Compliance
7071867Smsmith * provision in the documentation and/or other materials provided with the
7171867Smsmith * distribution.
7271867Smsmith *
7371867Smsmith * 3.4. Intel retains all right, title, and interest in and to the Original
7471867Smsmith * Intel Code.
7571867Smsmith *
7671867Smsmith * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7771867Smsmith * Intel shall be used in advertising or otherwise to promote the sale, use or
7871867Smsmith * other dealings in products derived from or relating to the Covered Code
7971867Smsmith * without prior written authorization from Intel.
8071867Smsmith *
8171867Smsmith * 4. Disclaimer and Export Compliance
8271867Smsmith *
8371867Smsmith * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8471867Smsmith * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8571867Smsmith * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8671867Smsmith * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8771867Smsmith * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8871867Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
8971867Smsmith * PARTICULAR PURPOSE.
9071867Smsmith *
9171867Smsmith * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9271867Smsmith * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9371867Smsmith * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9471867Smsmith * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9571867Smsmith * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9671867Smsmith * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9771867Smsmith * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9871867Smsmith * LIMITED REMEDY.
9971867Smsmith *
10071867Smsmith * 4.3. Licensee shall not export, either directly or indirectly, any of this
10171867Smsmith * software or system incorporating such software without first obtaining any
10271867Smsmith * required license or other approval from the U. S. Department of Commerce or
10371867Smsmith * any other agency or department of the United States Government.  In the
10471867Smsmith * event Licensee exports any such software from the United States or
10571867Smsmith * re-exports any such software from a foreign destination, Licensee shall
10671867Smsmith * ensure that the distribution and export/re-export of the software is in
10771867Smsmith * compliance with all laws, regulations, orders, or other restrictions of the
10871867Smsmith * U.S. Export Administration Regulations. Licensee agrees that neither it nor
10971867Smsmith * any of its subsidiaries will export/re-export any technical data, process,
11071867Smsmith * software, or service, directly or indirectly, to any country for which the
11171867Smsmith * United States government or any agency thereof requires an export license,
11271867Smsmith * other governmental approval, or letter of assurance, without first obtaining
11371867Smsmith * such license, approval or letter.
11471867Smsmith *
11571867Smsmith *****************************************************************************/
11671867Smsmith
11771867Smsmith#include "acpi.h"
11871867Smsmith#include "accommon.h"
11971867Smsmith
12077424Smsmith#define _COMPONENT          ACPI_HARDWARE
12191116Smsmith        ACPI_MODULE_NAME    ("hwsleep")
12271867Smsmith
12371867Smsmith
124126372Snjl/*******************************************************************************
125126372Snjl *
126126372Snjl * FUNCTION:    AcpiSetFirmwareWakingVector
127126372Snjl *
128126372Snjl * PARAMETERS:  PhysicalAddress     - 32-bit physical address of ACPI real mode
129126372Snjl *                                    entry point.
130126372Snjl *
131126372Snjl * RETURN:      Status
132126372Snjl *
133126372Snjl * DESCRIPTION: Sets the 32-bit FirmwareWakingVector field of the FACS
134126372Snjl *
135126372Snjl ******************************************************************************/
136126372Snjl
13771867SmsmithACPI_STATUS
13871867SmsmithAcpiSetFirmwareWakingVector (
13971867Smsmith    UINT32                  PhysicalAddress)
14071867Smsmith{
14171867Smsmith    ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector);
14271867Smsmith
14371867Smsmith
14491116Smsmith    /* Set the 32-bit vector */
14571867Smsmith
14671867Smsmith    AcpiGbl_FACS->FirmwareWakingVector = PhysicalAddress;
14771867Smsmith
14871867Smsmith    /* Clear the 64-bit vector if it exists */
14971867Smsmith
15071867Smsmith    if ((AcpiGbl_FACS->Length > 32) && (AcpiGbl_FACS->Version >= 1))
15171867Smsmith    {
15271867Smsmith        AcpiGbl_FACS->XFirmwareWakingVector = 0;
15371867Smsmith    }
15471867Smsmith
15591116Smsmith    return_ACPI_STATUS (AE_OK);
15671867Smsmith}
15771867Smsmith
15871867SmsmithACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector)
15971867Smsmith
16091116Smsmith
16171867Smsmith#if ACPI_MACHINE_WIDTH == 64
162102550Siwasaki/*******************************************************************************
16399679Siwasaki *
16471867Smsmith * FUNCTION:    AcpiSetFirmwareWakingVector64
16571867Smsmith *
16671867Smsmith * PARAMETERS:  PhysicalAddress     - 64-bit physical address of ACPI protected
167102550Siwasaki *                                    mode entry point.
16899679Siwasaki *
16971867Smsmith * RETURN:      Status
17071867Smsmith *
17171867Smsmith * DESCRIPTION: Sets the 64-bit X_FirmwareWakingVector field of the FACS, if
17271867Smsmith *              it exists in the table. This function is intended for use with
17371867Smsmith *              64-bit host operating systems.
17471867Smsmith *
17571867Smsmith ******************************************************************************/
17671867Smsmith
17771867SmsmithACPI_STATUS
17871867SmsmithAcpiSetFirmwareWakingVector64 (
17971867Smsmith    UINT64                  PhysicalAddress)
18071867Smsmith{
18171867Smsmith    ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector64);
18271867Smsmith
18371867Smsmith
18471867Smsmith    /* Determine if the 64-bit vector actually exists */
18591116Smsmith
18671867Smsmith    if ((AcpiGbl_FACS->Length <= 32) || (AcpiGbl_FACS->Version < 1))
18771867Smsmith    {
18871867Smsmith        return_ACPI_STATUS (AE_NOT_EXIST);
18971867Smsmith    }
19071867Smsmith
19171867Smsmith    /* Clear 32-bit vector, set the 64-bit X_ vector */
19271867Smsmith
19371867Smsmith    AcpiGbl_FACS->FirmwareWakingVector = 0;
19491116Smsmith    AcpiGbl_FACS->XFirmwareWakingVector = PhysicalAddress;
19571867Smsmith    return_ACPI_STATUS (AE_OK);
19671867Smsmith}
19771867Smsmith
19871867SmsmithACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector64)
19971867Smsmith#endif
20071867Smsmith
20171867Smsmith/*******************************************************************************
20271867Smsmith *
20371867Smsmith * FUNCTION:    AcpiEnterSleepStatePrep
20491116Smsmith *
20571867Smsmith * PARAMETERS:  SleepState          - Which sleep state to enter
206102550Siwasaki *
20799679Siwasaki * RETURN:      Status
20871867Smsmith *
20971867Smsmith * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
21071867Smsmith *              This function must execute with interrupts enabled.
211102550Siwasaki *              We break sleeping into 2 stages so that OSPM can handle
21299679Siwasaki *              various OS-specific tasks between the two steps.
21371867Smsmith *
21471867Smsmith ******************************************************************************/
21571867Smsmith
21671867SmsmithACPI_STATUS
21771867SmsmithAcpiEnterSleepStatePrep (
21887031Smsmith    UINT8                   SleepState)
21971867Smsmith{
22071867Smsmith    ACPI_STATUS             Status;
22187031Smsmith    ACPI_OBJECT_LIST        ArgList;
22271867Smsmith    ACPI_OBJECT             Arg;
22371867Smsmith
22471867Smsmith
22571867Smsmith    ACPI_FUNCTION_TRACE (AcpiEnterSleepStatePrep);
22671867Smsmith
22787031Smsmith
22887031Smsmith    /* _PSW methods could be run here to enable wake-on keyboard, LAN, etc. */
22987031Smsmith
23087031Smsmith    Status = AcpiGetSleepTypeData (SleepState,
23171867Smsmith                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
23271867Smsmith    if (ACPI_FAILURE (Status))
23371867Smsmith    {
23471867Smsmith        return_ACPI_STATUS (Status);
23587031Smsmith    }
23677424Smsmith
23771867Smsmith    /* Execute the _PTS method (Prepare To Sleep) */
23877424Smsmith
23977424Smsmith    ArgList.Count = 1;
24077424Smsmith    ArgList.Pointer = &Arg;
24171867Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
24277424Smsmith    Arg.Integer.Value = SleepState;
24391116Smsmith
24491116Smsmith    Status = AcpiEvaluateObject (NULL, METHOD_NAME__PTS, &ArgList, NULL);
24591116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
24671867Smsmith    {
24771867Smsmith        return_ACPI_STATUS (Status);
24871867Smsmith    }
24999679Siwasaki
25091116Smsmith    /* Setup the argument to the _SST method (System STatus) */
25191116Smsmith
25271867Smsmith    switch (SleepState)
25387031Smsmith    {
25471867Smsmith    case ACPI_STATE_S0:
25571867Smsmith        Arg.Integer.Value = ACPI_SST_WORKING;
25691116Smsmith        break;
25777424Smsmith
25871867Smsmith    case ACPI_STATE_S1:
25971867Smsmith    case ACPI_STATE_S2:
26071867Smsmith    case ACPI_STATE_S3:
26171867Smsmith        Arg.Integer.Value = ACPI_SST_SLEEPING;
26271867Smsmith        break;
26371867Smsmith
26491116Smsmith    case ACPI_STATE_S4:
26577424Smsmith        Arg.Integer.Value = ACPI_SST_SLEEP_CONTEXT;
266126372Snjl        break;
26791116Smsmith
26891116Smsmith    default:
26991116Smsmith        Arg.Integer.Value = ACPI_SST_INDICATOR_OFF; /* Default is off */
27091116Smsmith        break;
27191116Smsmith    }
272126372Snjl
27391116Smsmith    /*
27491116Smsmith     * Set the system indicators to show the desired sleep state.
27591116Smsmith     * _SST is an optional method (return no error if not found)
27691116Smsmith     */
27791116Smsmith    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
278126372Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
279126372Snjl    {
280126372Snjl        ACPI_EXCEPTION ((AE_INFO, Status, "While executing method _SST"));
281126372Snjl    }
282126372Snjl
283126372Snjl    return_ACPI_STATUS (AE_OK);
284126372Snjl}
285126372Snjl
286126372SnjlACPI_EXPORT_SYMBOL (AcpiEnterSleepStatePrep)
287126372Snjl
288126372Snjl
289126372Snjl/*******************************************************************************
290126372Snjl *
291126372Snjl * FUNCTION:    AcpiEnterSleepState
292126372Snjl *
293126372Snjl * PARAMETERS:  SleepState          - Which sleep state to enter
294126372Snjl *
295126372Snjl * RETURN:      Status
296126372Snjl *
297126372Snjl * DESCRIPTION: Enter a system sleep state
298126372Snjl *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
299126372Snjl *
300126372Snjl ******************************************************************************/
301123315Snjl
302123315SnjlACPI_STATUS
303126372SnjlAcpiEnterSleepState (
304123315Snjl    UINT8                   SleepState)
305123315Snjl{
306123315Snjl    UINT32                  Pm1aControl;
307123315Snjl    UINT32                  Pm1bControl;
308123315Snjl    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
30987031Smsmith    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
31087031Smsmith    UINT32                  InValue;
31187031Smsmith    ACPI_OBJECT_LIST        ArgList;
31287031Smsmith    ACPI_OBJECT             Arg;
31387031Smsmith    ACPI_STATUS             Status;
31487031Smsmith
31587031Smsmith
31687031Smsmith    ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
31787031Smsmith
31887031Smsmith
31987031Smsmith    if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
32087031Smsmith        (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
32187031Smsmith    {
32287031Smsmith        ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=%X B=%X",
32387031Smsmith            AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
32487031Smsmith        return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
32587031Smsmith    }
32687031Smsmith
32787031Smsmith    SleepTypeRegInfo   = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
32891116Smsmith    SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
32987031Smsmith
33099679Siwasaki    /* Clear wake status */
33199679Siwasaki
33291116Smsmith    Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
33391116Smsmith    if (ACPI_FAILURE (Status))
33499679Siwasaki    {
33599679Siwasaki        return_ACPI_STATUS (Status);
33687031Smsmith    }
33787031Smsmith
33891116Smsmith    /* Clear all fixed and general purpose status bits */
33987031Smsmith
34091116Smsmith    Status = AcpiHwClearAcpiStatus ();
34191116Smsmith    if (ACPI_FAILURE (Status))
34291116Smsmith    {
34387031Smsmith        return_ACPI_STATUS (Status);
34499679Siwasaki    }
34591116Smsmith
34691116Smsmith    if (SleepState != ACPI_STATE_S5)
34787031Smsmith    {
34887031Smsmith        /* Disable BM arbitration */
34991116Smsmith
35091116Smsmith        Status = AcpiWriteBitRegister (ACPI_BITREG_ARB_DISABLE, 1);
35171867Smsmith        if (ACPI_FAILURE (Status))
352128245Snjl        {
353128245Snjl            return_ACPI_STATUS (Status);
354128245Snjl        }
355128245Snjl    }
35699679Siwasaki
357128245Snjl    /*
358128245Snjl     * 1) Disable/Clear all GPEs
35978986Smsmith     * 2) Enable all wakeup GPEs
360128245Snjl     */
361128245Snjl    Status = AcpiHwDisableAllGpes ();
362128245Snjl    if (ACPI_FAILURE (Status))
363128245Snjl    {
364128245Snjl        return_ACPI_STATUS (Status);
36587031Smsmith    }
366128245Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
367128245Snjl
368123315Snjl    Status = AcpiHwEnableAllWakeupGpes ();
369123315Snjl    if (ACPI_FAILURE (Status))
370123315Snjl    {
371123315Snjl        return_ACPI_STATUS (Status);
372123315Snjl    }
373123315Snjl
374123315Snjl    /* Execute the _GTS method (Going To Sleep) */
37599679Siwasaki
37699679Siwasaki    ArgList.Count = 1;
377128212Snjl    ArgList.Pointer = &Arg;
378128212Snjl    Arg.Type = ACPI_TYPE_INTEGER;
379128212Snjl    Arg.Integer.Value = SleepState;
380128212Snjl
381128212Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__GTS, &ArgList, NULL);
38299679Siwasaki    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
38399679Siwasaki    {
38499679Siwasaki        return_ACPI_STATUS (Status);
38599679Siwasaki    }
38699679Siwasaki
38791116Smsmith    /* Get current value of PM1A control */
38878986Smsmith
389114237Snjl    Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
39099679Siwasaki                &Pm1aControl);
39199679Siwasaki    if (ACPI_FAILURE (Status))
39299679Siwasaki    {
39399679Siwasaki        return_ACPI_STATUS (Status);
394114237Snjl    }
39582367Smsmith    ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
39691116Smsmith        "Entering sleep state [S%d]\n", SleepState));
39782367Smsmith
39891116Smsmith    /* Clear the SLP_EN and SLP_TYP fields */
39971867Smsmith
40071867Smsmith    Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
40191116Smsmith                     SleepEnableRegInfo->AccessBitMask);
40282367Smsmith    Pm1bControl = Pm1aControl;
40391116Smsmith
40491116Smsmith    /* Insert the SLP_TYP bits */
40571867Smsmith
406126372Snjl    Pm1aControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
407126372Snjl    Pm1bControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
408126372Snjl
409126372Snjl    /*
410126372Snjl     * We split the writes of SLP_TYP and SLP_EN to workaround
41191116Smsmith     * poorly implemented hardware.
41271867Smsmith     */
413114237Snjl
41499679Siwasaki    /* Write #1: write the SLP_TYP data to the PM1 Control registers */
41599679Siwasaki
41699679Siwasaki    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
41799679Siwasaki    if (ACPI_FAILURE (Status))
41882367Smsmith    {
419114237Snjl        return_ACPI_STATUS (Status);
42099679Siwasaki    }
42199679Siwasaki
42299679Siwasaki    /* Insert the sleep enable (SLP_EN) bit */
42399679Siwasaki
42499679Siwasaki    Pm1aControl |= SleepEnableRegInfo->AccessBitMask;
42591116Smsmith    Pm1bControl |= SleepEnableRegInfo->AccessBitMask;
42682367Smsmith
42791116Smsmith    /* Flush caches, as per ACPI specification */
42891116Smsmith
42971867Smsmith    ACPI_FLUSH_CPU_CACHE ();
43091116Smsmith
43171867Smsmith    /* Write #2: Write both SLP_TYP + SLP_EN */
43299679Siwasaki
43380062Smsmith    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
434114237Snjl    if (ACPI_FAILURE (Status))
43599679Siwasaki    {
43699679Siwasaki        return_ACPI_STATUS (Status);
43799679Siwasaki    }
43899679Siwasaki
43999679Siwasaki    if (SleepState > ACPI_STATE_S3)
440114237Snjl    {
44199679Siwasaki        /*
44299679Siwasaki         * We wanted to sleep > S3, but it didn't happen (by virtue of the
44399679Siwasaki         * fact that we are still executing!)
44499679Siwasaki         *
44599679Siwasaki         * Wait ten seconds, then try again. This is to get S4/S5 to work on
44682367Smsmith         * all machines.
44782367Smsmith         *
44899679Siwasaki         * We wait so long to allow chipsets that poll this reg very slowly
449123315Snjl         * to still read the right value. Ideally, this block would go
450123315Snjl         * away entirely.
451123315Snjl         */
452123315Snjl        AcpiOsStall (10000000);
453123315Snjl
45499679Siwasaki        Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_CONTROL,
455126372Snjl                    SleepEnableRegInfo->AccessBitMask);
45699679Siwasaki        if (ACPI_FAILURE (Status))
45799679Siwasaki        {
45899679Siwasaki            return_ACPI_STATUS (Status);
45982367Smsmith        }
460114237Snjl    }
46199679Siwasaki
46299679Siwasaki    /* Wait until we enter sleep state */
46399679Siwasaki
46499679Siwasaki    do
46599679Siwasaki    {
46680357Speter        Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
46780062Smsmith        if (ACPI_FAILURE (Status))
46891116Smsmith        {
46971867Smsmith            return_ACPI_STATUS (Status);
470102550Siwasaki        }
47183174Smsmith
472114237Snjl        /* Spin until we wake */
47399679Siwasaki
47499679Siwasaki    } while (!InValue);
47599679Siwasaki
47699679Siwasaki    return_ACPI_STATUS (AE_OK);
47799679Siwasaki}
47891116Smsmith
47982367SmsmithACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
48099679Siwasaki
48199679Siwasaki
48271867Smsmith/*******************************************************************************
48371867Smsmith *
48482367Smsmith * FUNCTION:    AcpiEnterSleepStateS4bios
485114237Snjl *
48682367Smsmith * PARAMETERS:  None
48782367Smsmith *
488114237Snjl * RETURN:      Status
489114237Snjl *
490114237Snjl * DESCRIPTION: Perform a S4 bios request.
491114237Snjl *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
492114237Snjl *
493114237Snjl ******************************************************************************/
494114237Snjl
495114237SnjlACPI_STATUS
496114237SnjlAcpiEnterSleepStateS4bios (
497114237Snjl    void)
498114237Snjl{
499114237Snjl    UINT32                  InValue;
500114237Snjl    ACPI_STATUS             Status;
501114237Snjl
502114237Snjl
503114237Snjl    ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
504114237Snjl
505114237Snjl
506114237Snjl    /* Clear the wake status bit (PM1) */
507114237Snjl
508114237Snjl    Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
509123315Snjl    if (ACPI_FAILURE (Status))
510126372Snjl    {
511126372Snjl        return_ACPI_STATUS (Status);
512126372Snjl    }
513126372Snjl
514126372Snjl    Status = AcpiHwClearAcpiStatus ();
515114237Snjl    if (ACPI_FAILURE (Status))
516126372Snjl    {
517126372Snjl        return_ACPI_STATUS (Status);
518126372Snjl    }
519126372Snjl
520126372Snjl    /*
521114237Snjl     * 1) Disable/Clear all GPEs
522128212Snjl     * 2) Enable all wakeup GPEs
523128212Snjl     */
524128212Snjl    Status = AcpiHwDisableAllGpes ();
525128212Snjl    if (ACPI_FAILURE (Status))
526128212Snjl    {
527126372Snjl        return_ACPI_STATUS (Status);
528126372Snjl    }
529126372Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
530126372Snjl
531114237Snjl    Status = AcpiHwEnableAllWakeupGpes ();
532126372Snjl    if (ACPI_FAILURE (Status))
533126372Snjl    {
534117521Snjl        return_ACPI_STATUS (Status);
535114237Snjl    }
536114237Snjl
537114237Snjl    ACPI_FLUSH_CPU_CACHE ();
538114237Snjl
539114237Snjl    Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
540114237Snjl                (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
541114237Snjl
542114237Snjl    do {
543114237Snjl        AcpiOsStall(1000);
544114237Snjl        Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
545114237Snjl        if (ACPI_FAILURE (Status))
546114237Snjl        {
547114237Snjl            return_ACPI_STATUS (Status);
548114237Snjl        }
549114237Snjl    } while (!InValue);
550114237Snjl
55182367Smsmith    return_ACPI_STATUS (AE_OK);
55282367Smsmith}
55382367Smsmith
55482367SmsmithACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
55582367Smsmith
55682367Smsmith
55782367Smsmith/*******************************************************************************
55882367Smsmith *
55982367Smsmith * FUNCTION:    AcpiLeaveSleepState
56082367Smsmith *
56182367Smsmith * PARAMETERS:  SleepState          - Which sleep state we just exited
56282367Smsmith *
563123315Snjl * RETURN:      Status
56482367Smsmith *
565123315Snjl * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
566123315Snjl *              Called with interrupts ENABLED.
567123315Snjl *
568123315Snjl ******************************************************************************/
569123315Snjl
570126372SnjlACPI_STATUS
571126372SnjlAcpiLeaveSleepState (
57282367Smsmith    UINT8                   SleepState)
57383174Smsmith{
57491116Smsmith    ACPI_OBJECT_LIST        ArgList;
57582367Smsmith    ACPI_OBJECT             Arg;
57691116Smsmith    ACPI_STATUS             Status;
577126372Snjl    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
578126372Snjl    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
579126372Snjl    UINT32                  Pm1aControl;
580126372Snjl    UINT32                  Pm1bControl;
581126372Snjl
582126372Snjl
583126372Snjl    ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
584123315Snjl
585123315Snjl
586126372Snjl    /*
587126372Snjl     * Set SLP_TYPE and SLP_EN to state S0.
588123315Snjl     * This is unclear from the ACPI Spec, but it is required
589126372Snjl     * by some machines.
590123315Snjl     */
591126372Snjl    Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
592126372Snjl                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
593126372Snjl    if (ACPI_SUCCESS (Status))
594126372Snjl    {
595126372Snjl        SleepTypeRegInfo =
596126372Snjl            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
597126372Snjl        SleepEnableRegInfo =
598126372Snjl            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
599126372Snjl
600126372Snjl        /* Get current value of PM1A control */
601126372Snjl
602126372Snjl        Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
603126372Snjl                    &Pm1aControl);
604126372Snjl        if (ACPI_SUCCESS (Status))
605126372Snjl        {
606126372Snjl            /* Clear the SLP_EN and SLP_TYP fields */
607126372Snjl
608126372Snjl            Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
609126372Snjl                SleepEnableRegInfo->AccessBitMask);
610126372Snjl            Pm1bControl = Pm1aControl;
611126372Snjl
612126372Snjl            /* Insert the SLP_TYP bits */
613123315Snjl
614123315Snjl            Pm1aControl |= (AcpiGbl_SleepTypeA <<
61587031Smsmith                SleepTypeRegInfo->BitPosition);
61682367Smsmith            Pm1bControl |= (AcpiGbl_SleepTypeB <<
61791116Smsmith                SleepTypeRegInfo->BitPosition);
61891116Smsmith
61991116Smsmith            /* Write the control registers and ignore any errors */
62091116Smsmith
62182367Smsmith            (void) AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
62282367Smsmith        }
62382367Smsmith    }
62482367Smsmith
62591116Smsmith    /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
62683174Smsmith
627126372Snjl    AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
628126372Snjl
629123315Snjl    /* Setup parameter object */
630123315Snjl
631123315Snjl    ArgList.Count = 1;
632123315Snjl    ArgList.Pointer = &Arg;
633123315Snjl    Arg.Type = ACPI_TYPE_INTEGER;
634123315Snjl
635126372Snjl    /* Ignore any errors from these methods */
63691116Smsmith
63791116Smsmith    Arg.Integer.Value = ACPI_SST_WAKING;
63891116Smsmith    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
63991116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
64091116Smsmith    {
641126372Snjl        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
64291116Smsmith    }
64391116Smsmith
64491116Smsmith    Arg.Integer.Value = SleepState;
64591116Smsmith    Status = AcpiEvaluateObject (NULL, METHOD_NAME__BFS, &ArgList, NULL);
646128212Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
64791116Smsmith    {
648128212Snjl        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _BFS"));
649128212Snjl    }
650128212Snjl
651128212Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__WAK, &ArgList, NULL);
652128212Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
653128212Snjl    {
65499679Siwasaki        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _WAK"));
65599679Siwasaki    }
65699679Siwasaki    /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
65799679Siwasaki
65882367Smsmith    /*
659126372Snjl     * Restore the GPEs:
660126372Snjl     * 1) Disable/Clear all GPEs
661126372Snjl     * 2) Enable all runtime GPEs
662126372Snjl     */
663126372Snjl    Status = AcpiHwDisableAllGpes ();
664126372Snjl    if (ACPI_FAILURE (Status))
665126372Snjl    {
666123315Snjl        return_ACPI_STATUS (Status);
667123315Snjl    }
66899679Siwasaki    AcpiGbl_SystemAwakeAndRunning = TRUE;
669126372Snjl
670126372Snjl    Status = AcpiHwEnableAllRuntimeGpes ();
671126372Snjl    if (ACPI_FAILURE (Status))
672126372Snjl    {
673126372Snjl        return_ACPI_STATUS (Status);
674126372Snjl    }
675126372Snjl
676126372Snjl    /* Enable power button */
677126372Snjl
678126372Snjl    (void) AcpiWriteBitRegister(
679126372Snjl            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,
680126372Snjl            ACPI_ENABLE_EVENT);
68199679Siwasaki
68282367Smsmith    (void) AcpiWriteBitRegister(
683            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,
684            ACPI_CLEAR_STATUS);
685
686    /* Enable BM arbitration */
687
688    Status = AcpiWriteBitRegister (ACPI_BITREG_ARB_DISABLE, 0);
689    if (ACPI_FAILURE (Status))
690    {
691        return_ACPI_STATUS (Status);
692    }
693
694    Arg.Integer.Value = ACPI_SST_WORKING;
695    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
696    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
697    {
698        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
699    }
700
701    return_ACPI_STATUS (Status);
702}
703
704ACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)
705
706