hwsleep.c revision 87031
171867Smsmith
271867Smsmith/******************************************************************************
371867Smsmith *
471867Smsmith * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
587031Smsmith *              $Revision: 25 $
671867Smsmith *
771867Smsmith *****************************************************************************/
871867Smsmith
971867Smsmith/******************************************************************************
1071867Smsmith *
1171867Smsmith * 1. Copyright Notice
1271867Smsmith *
1371867Smsmith * Some or all of this work - Copyright (c) 1999, 2000, 2001, Intel Corp.
1471867Smsmith * All rights reserved.
1571867Smsmith *
1671867Smsmith * 2. License
1771867Smsmith *
1871867Smsmith * 2.1. This is your license from Intel Corp. under its intellectual property
1971867Smsmith * rights.  You may have additional license terms from the party that provided
2071867Smsmith * you this software, covering your right to use that party's intellectual
2171867Smsmith * property rights.
2271867Smsmith *
2371867Smsmith * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
2471867Smsmith * copy of the source code appearing in this file ("Covered Code") an
2571867Smsmith * irrevocable, perpetual, worldwide license under Intel's copyrights in the
2671867Smsmith * base code distributed originally by Intel ("Original Intel Code") to copy,
2771867Smsmith * make derivatives, distribute, use and display any portion of the Covered
2871867Smsmith * Code in any form, with the right to sublicense such rights; and
2971867Smsmith *
3071867Smsmith * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
3171867Smsmith * license (with the right to sublicense), under only those claims of Intel
3271867Smsmith * patents that are infringed by the Original Intel Code, to make, use, sell,
3371867Smsmith * offer to sell, and import the Covered Code and derivative works thereof
3471867Smsmith * solely to the minimum extent necessary to exercise the above copyright
3571867Smsmith * license, and in no event shall the patent license extend to any additions
3671867Smsmith * to or modifications of the Original Intel Code.  No other license or right
3771867Smsmith * is granted directly or by implication, estoppel or otherwise;
3871867Smsmith *
3971867Smsmith * The above copyright and patent license is granted only if the following
4071867Smsmith * conditions are met:
4171867Smsmith *
4271867Smsmith * 3. Conditions
4371867Smsmith *
4471867Smsmith * 3.1. Redistribution of Source with Rights to Further Distribute Source.
4571867Smsmith * Redistribution of source code of any substantial portion of the Covered
4671867Smsmith * Code or modification with rights to further distribute source must include
4771867Smsmith * the above Copyright Notice, the above License, this list of Conditions,
4871867Smsmith * and the following Disclaimer and Export Compliance provision.  In addition,
4971867Smsmith * Licensee must cause all Covered Code to which Licensee contributes to
5071867Smsmith * contain a file documenting the changes Licensee made to create that Covered
5171867Smsmith * Code and the date of any change.  Licensee must include in that file the
5271867Smsmith * documentation of any changes made by any predecessor Licensee.  Licensee
5371867Smsmith * must include a prominent statement that the modification is derived,
5471867Smsmith * directly or indirectly, from Original Intel Code.
5571867Smsmith *
5671867Smsmith * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
5771867Smsmith * Redistribution of source code of any substantial portion of the Covered
5871867Smsmith * Code or modification without rights to further distribute source must
5971867Smsmith * include the following Disclaimer and Export Compliance provision in the
6071867Smsmith * documentation and/or other materials provided with distribution.  In
6171867Smsmith * addition, Licensee may not authorize further sublicense of source of any
6271867Smsmith * portion of the Covered Code, and must include terms to the effect that the
6371867Smsmith * license from Licensee to its licensee is limited to the intellectual
6471867Smsmith * property embodied in the software Licensee provides to its licensee, and
6571867Smsmith * not to intellectual property embodied in modifications its licensee may
6671867Smsmith * make.
6771867Smsmith *
6871867Smsmith * 3.3. Redistribution of Executable. Redistribution in executable form of any
6971867Smsmith * substantial portion of the Covered Code or modification must reproduce the
7071867Smsmith * above Copyright Notice, and the following Disclaimer and Export Compliance
7171867Smsmith * provision in the documentation and/or other materials provided with the
7271867Smsmith * distribution.
7371867Smsmith *
7471867Smsmith * 3.4. Intel retains all right, title, and interest in and to the Original
7571867Smsmith * Intel Code.
7671867Smsmith *
7771867Smsmith * 3.5. Neither the name Intel nor any other trademark owned or controlled by
7871867Smsmith * Intel shall be used in advertising or otherwise to promote the sale, use or
7971867Smsmith * other dealings in products derived from or relating to the Covered Code
8071867Smsmith * without prior written authorization from Intel.
8171867Smsmith *
8271867Smsmith * 4. Disclaimer and Export Compliance
8371867Smsmith *
8471867Smsmith * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
8571867Smsmith * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
8671867Smsmith * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
8771867Smsmith * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
8871867Smsmith * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
8971867Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
9071867Smsmith * PARTICULAR PURPOSE.
9171867Smsmith *
9271867Smsmith * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
9371867Smsmith * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
9471867Smsmith * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
9571867Smsmith * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
9671867Smsmith * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
9771867Smsmith * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
9871867Smsmith * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
9971867Smsmith * LIMITED REMEDY.
10071867Smsmith *
10171867Smsmith * 4.3. Licensee shall not export, either directly or indirectly, any of this
10271867Smsmith * software or system incorporating such software without first obtaining any
10371867Smsmith * required license or other approval from the U. S. Department of Commerce or
10471867Smsmith * any other agency or department of the United States Government.  In the
10571867Smsmith * event Licensee exports any such software from the United States or
10671867Smsmith * re-exports any such software from a foreign destination, Licensee shall
10771867Smsmith * ensure that the distribution and export/re-export of the software is in
10871867Smsmith * compliance with all laws, regulations, orders, or other restrictions of the
10971867Smsmith * U.S. Export Administration Regulations. Licensee agrees that neither it nor
11071867Smsmith * any of its subsidiaries will export/re-export any technical data, process,
11171867Smsmith * software, or service, directly or indirectly, to any country for which the
11271867Smsmith * United States government or any agency thereof requires an export license,
11371867Smsmith * other governmental approval, or letter of assurance, without first obtaining
11471867Smsmith * such license, approval or letter.
11571867Smsmith *
11671867Smsmith *****************************************************************************/
11771867Smsmith
11871867Smsmith#include "acpi.h"
11971867Smsmith#include "acnamesp.h"
12071867Smsmith#include "achware.h"
12171867Smsmith
12277424Smsmith#define _COMPONENT          ACPI_HARDWARE
12371867Smsmith        MODULE_NAME         ("hwsleep")
12471867Smsmith
12587031Smsmithstatic UINT8 SleepTypeA;
12687031Smsmithstatic UINT8 SleepTypeB;
12771867Smsmith
12887031Smsmith
12971867Smsmith/******************************************************************************
13071867Smsmith *
13171867Smsmith * FUNCTION:    AcpiSetFirmwareWakingVector
13271867Smsmith *
13371867Smsmith * PARAMETERS:  PhysicalAddress     - Physical address of ACPI real mode
13471867Smsmith *                                    entry point.
13571867Smsmith *
13671867Smsmith * RETURN:      AE_OK or AE_ERROR
13771867Smsmith *
13871867Smsmith * DESCRIPTION: Access function for dFirmwareWakingVector field in FACS
13971867Smsmith *
14071867Smsmith ******************************************************************************/
14171867Smsmith
14271867SmsmithACPI_STATUS
14371867SmsmithAcpiSetFirmwareWakingVector (
14471867Smsmith    ACPI_PHYSICAL_ADDRESS PhysicalAddress)
14571867Smsmith{
14671867Smsmith
14771867Smsmith    FUNCTION_TRACE ("AcpiSetFirmwareWakingVector");
14871867Smsmith
14971867Smsmith
15071867Smsmith    /* Make sure that we have an FACS */
15171867Smsmith
15271867Smsmith    if (!AcpiGbl_FACS)
15371867Smsmith    {
15471867Smsmith        return_ACPI_STATUS (AE_NO_ACPI_TABLES);
15571867Smsmith    }
15671867Smsmith
15771867Smsmith    /* Set the vector */
15871867Smsmith
15971867Smsmith    if (AcpiGbl_FACS->VectorWidth == 32)
16071867Smsmith    {
16171867Smsmith        * (UINT32 *) AcpiGbl_FACS->FirmwareWakingVector = (UINT32) PhysicalAddress;
16271867Smsmith    }
16371867Smsmith    else
16471867Smsmith    {
16571867Smsmith        *AcpiGbl_FACS->FirmwareWakingVector = PhysicalAddress;
16671867Smsmith    }
16771867Smsmith
16871867Smsmith    return_ACPI_STATUS (AE_OK);
16971867Smsmith}
17071867Smsmith
17171867Smsmith
17271867Smsmith/******************************************************************************
17371867Smsmith *
17471867Smsmith * FUNCTION:    AcpiGetFirmwareWakingVector
17571867Smsmith *
17671867Smsmith * PARAMETERS:  *PhysicalAddress    - Output buffer where contents of
17771867Smsmith *                                    the FirmwareWakingVector field of
17871867Smsmith *                                    the FACS will be stored.
17971867Smsmith *
18071867Smsmith * RETURN:      Status
18171867Smsmith *
18271867Smsmith * DESCRIPTION: Access function for dFirmwareWakingVector field in FACS
18371867Smsmith *
18471867Smsmith ******************************************************************************/
18571867Smsmith
18671867SmsmithACPI_STATUS
18771867SmsmithAcpiGetFirmwareWakingVector (
18871867Smsmith    ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
18971867Smsmith{
19071867Smsmith
19171867Smsmith    FUNCTION_TRACE ("AcpiGetFirmwareWakingVector");
19271867Smsmith
19371867Smsmith
19471867Smsmith    if (!PhysicalAddress)
19571867Smsmith    {
19671867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
19771867Smsmith    }
19871867Smsmith
19971867Smsmith    /* Make sure that we have an FACS */
20071867Smsmith
20171867Smsmith    if (!AcpiGbl_FACS)
20271867Smsmith    {
20371867Smsmith        return_ACPI_STATUS (AE_NO_ACPI_TABLES);
20471867Smsmith    }
20571867Smsmith
20671867Smsmith    /* Get the vector */
20771867Smsmith
20871867Smsmith    if (AcpiGbl_FACS->VectorWidth == 32)
20971867Smsmith    {
21071867Smsmith        *PhysicalAddress = * (UINT32 *) AcpiGbl_FACS->FirmwareWakingVector;
21171867Smsmith    }
21271867Smsmith    else
21371867Smsmith    {
21471867Smsmith        *PhysicalAddress = *AcpiGbl_FACS->FirmwareWakingVector;
21571867Smsmith    }
21671867Smsmith
21771867Smsmith    return_ACPI_STATUS (AE_OK);
21871867Smsmith}
21971867Smsmith
22087031Smsmith
22171867Smsmith/******************************************************************************
22271867Smsmith *
22387031Smsmith * FUNCTION:    AcpiEnterSleepStatePrep
22471867Smsmith *
22571867Smsmith * PARAMETERS:  SleepState          - Which sleep state to enter
22671867Smsmith *
22771867Smsmith * RETURN:      Status
22871867Smsmith *
22987031Smsmith * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
23087031Smsmith *              This function must execute with interrupts enabled.
23187031Smsmith *              We break sleeping into 2 stages so that OSPM can handle
23287031Smsmith *              various OS-specific tasks between the two steps.
23371867Smsmith *
23471867Smsmith ******************************************************************************/
23571867Smsmith
23671867SmsmithACPI_STATUS
23787031SmsmithAcpiEnterSleepStatePrep (
23877424Smsmith    UINT8               SleepState)
23971867Smsmith{
24077424Smsmith    ACPI_STATUS         Status;
24177424Smsmith    ACPI_OBJECT_LIST    ArgList;
24277424Smsmith    ACPI_OBJECT         Arg;
24371867Smsmith
24487031Smsmith    FUNCTION_TRACE ("AcpiEnterSleepStatePrep");
24577424Smsmith
24671867Smsmith    /*
24771867Smsmith     * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
24871867Smsmith     */
24987031Smsmith    Status = AcpiHwObtainSleepTypeRegisterData (SleepState, &SleepTypeA, &SleepTypeB);
25077424Smsmith    if (!ACPI_SUCCESS (Status))
25171867Smsmith    {
25287031Smsmith        return_ACPI_STATUS (Status);
25371867Smsmith    }
25471867Smsmith
25571867Smsmith    /* run the _PTS and _GTS methods */
25677424Smsmith
25771867Smsmith    MEMSET(&ArgList, 0, sizeof(ArgList));
25871867Smsmith    ArgList.Count = 1;
25971867Smsmith    ArgList.Pointer = &Arg;
26071867Smsmith
26171867Smsmith    MEMSET(&Arg, 0, sizeof(Arg));
26271867Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
26371867Smsmith    Arg.Integer.Value = SleepState;
26471867Smsmith
26582367Smsmith    AcpiEvaluateObject (NULL, "\\_PTS", &ArgList, NULL);
26682367Smsmith    AcpiEvaluateObject (NULL, "\\_GTS", &ArgList, NULL);
26777424Smsmith
26887031Smsmith    return_ACPI_STATUS (AE_OK);
26987031Smsmith}
27087031Smsmith
27187031Smsmith
27287031Smsmith
27387031Smsmith/******************************************************************************
27487031Smsmith *
27587031Smsmith * FUNCTION:    AcpiEnterSleepState
27687031Smsmith *
27787031Smsmith * PARAMETERS:  SleepState          - Which sleep state to enter
27887031Smsmith *
27987031Smsmith * RETURN:      Status
28087031Smsmith *
28187031Smsmith * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
28287031Smsmith *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
28387031Smsmith *
28487031Smsmith ******************************************************************************/
28587031Smsmith
28687031SmsmithACPI_STATUS
28787031SmsmithAcpiEnterSleepState (
28887031Smsmith    UINT8               SleepState)
28987031Smsmith{
29087031Smsmith    UINT16              PM1AControl;
29187031Smsmith    UINT16              PM1BControl;
29287031Smsmith
29387031Smsmith
29487031Smsmith    FUNCTION_TRACE ("AcpiEnterSleepState");
29587031Smsmith
29687031Smsmith    if ((SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
29787031Smsmith        (SleepTypeB > ACPI_SLEEP_TYPE_MAX))
29887031Smsmith    {
29987031Smsmith        REPORT_ERROR (("Sleep values out of range: A=%x B=%x\n",
30087031Smsmith            SleepTypeA, SleepTypeB));
30187031Smsmith        return_ACPI_STATUS (AE_ERROR);
30287031Smsmith    }
30387031Smsmith
30471867Smsmith    /* clear wake status */
30577424Smsmith
30682367Smsmith    AcpiHwRegisterBitAccess (ACPI_WRITE, ACPI_MTX_LOCK, WAK_STS, 1);
30771867Smsmith
30887031Smsmith    AcpiHwClearAcpiStatus();
30978986Smsmith
31087031Smsmith    /* disable arbitration here? */
31187031Smsmith
31284491Smsmith    AcpiHwDisableNonWakeupGpes();
31371867Smsmith
31482367Smsmith    PM1AControl = (UINT16) AcpiHwRegisterRead (ACPI_MTX_LOCK, PM1_CONTROL);
31578986Smsmith
31682367Smsmith    ACPI_DEBUG_PRINT ((ACPI_DB_OK, "Entering S%d\n", SleepState));
31782367Smsmith
31871867Smsmith    /* mask off SLP_EN and SLP_TYP fields */
31982367Smsmith
32082367Smsmith    PM1AControl &= ~(SLP_TYPE_X_MASK | SLP_EN_MASK);
32171867Smsmith    PM1BControl = PM1AControl;
32271867Smsmith
32371867Smsmith    /* mask in SLP_TYP */
32482367Smsmith
32587031Smsmith    PM1AControl |= (SleepTypeA << AcpiHwGetBitShift (SLP_TYPE_X_MASK));
32687031Smsmith    PM1BControl |= (SleepTypeB << AcpiHwGetBitShift (SLP_TYPE_X_MASK));
32771867Smsmith
32880062Smsmith    /* write #1: fill in SLP_TYP data */
32971867Smsmith
33082367Smsmith    AcpiHwRegisterWrite (ACPI_MTX_LOCK, PM1A_CONTROL, PM1AControl);
33182367Smsmith    AcpiHwRegisterWrite (ACPI_MTX_LOCK, PM1B_CONTROL, PM1BControl);
33282367Smsmith
33378986Smsmith    /* mask in SLP_EN */
33482367Smsmith
33578986Smsmith    PM1AControl |= (1 << AcpiHwGetBitShift (SLP_EN_MASK));
33678986Smsmith    PM1BControl |= (1 << AcpiHwGetBitShift (SLP_EN_MASK));
33771867Smsmith
33880062Smsmith    /* write #2: SLP_TYP + SLP_EN */
33971867Smsmith
34082367Smsmith    AcpiHwRegisterWrite (ACPI_MTX_LOCK, PM1A_CONTROL, PM1AControl);
34182367Smsmith    AcpiHwRegisterWrite (ACPI_MTX_LOCK, PM1B_CONTROL, PM1BControl);
34280062Smsmith
34383174Smsmith    /*
34482367Smsmith     * Wait a second, then try again. This is to get S4/5 to work on all machines.
34582367Smsmith     */
34682367Smsmith    if (SleepState > ACPI_STATE_S3)
34782367Smsmith    {
34882367Smsmith        AcpiOsStall(1000000);
34982367Smsmith
35082367Smsmith        AcpiHwRegisterWrite (ACPI_MTX_LOCK, PM1_CONTROL,
35182367Smsmith            (1 << AcpiHwGetBitShift (SLP_EN_MASK)));
35280357Speter    }
35380062Smsmith
35482367Smsmith    /* wait until we enter sleep state */
35571867Smsmith
35687031Smsmith    while (!AcpiHwRegisterBitAccess (ACPI_READ, ACPI_MTX_LOCK, WAK_STS))
35783174Smsmith    {
35887031Smsmith        /* spin until we wake */
35983174Smsmith    }
36082367Smsmith
36171867Smsmith    return_ACPI_STATUS (AE_OK);
36271867Smsmith}
36382367Smsmith
36482367Smsmith/******************************************************************************
36582367Smsmith *
36682367Smsmith * FUNCTION:    AcpiLeaveSleepState
36782367Smsmith *
36882367Smsmith * PARAMETERS:  SleepState          - Which sleep state we just exited
36982367Smsmith *
37082367Smsmith * RETURN:      Status
37182367Smsmith *
37282367Smsmith * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
37382367Smsmith *
37482367Smsmith ******************************************************************************/
37582367Smsmith
37682367SmsmithACPI_STATUS
37782367SmsmithAcpiLeaveSleepState (
37882367Smsmith    UINT8               SleepState)
37982367Smsmith{
38082367Smsmith    ACPI_OBJECT_LIST    ArgList;
38182367Smsmith    ACPI_OBJECT         Arg;
38282367Smsmith
38383174Smsmith
38482367Smsmith    FUNCTION_TRACE ("AcpiLeaveSleepState");
38582367Smsmith
38687031Smsmith    /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
38787031Smsmith    SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
38882367Smsmith
38982367Smsmith    MEMSET (&ArgList, 0, sizeof(ArgList));
39082367Smsmith    ArgList.Count = 1;
39182367Smsmith    ArgList.Pointer = &Arg;
39282367Smsmith
39382367Smsmith    MEMSET (&Arg, 0, sizeof(Arg));
39482367Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
39582367Smsmith    Arg.Integer.Value = SleepState;
39682367Smsmith
39782367Smsmith    AcpiEvaluateObject (NULL, "\\_BFS", &ArgList, NULL);
39882367Smsmith    AcpiEvaluateObject (NULL, "\\_WAK", &ArgList, NULL);
39983174Smsmith
40082367Smsmith    /* _WAK returns stuff - do we want to look at it? */
40182367Smsmith
40284491Smsmith    AcpiHwEnableNonWakeupGpes();
40382367Smsmith
40482367Smsmith    return_ACPI_STATUS (AE_OK);
40582367Smsmith}
406