hwsleep.c revision 151937
171867Smsmith
271867Smsmith/******************************************************************************
371867Smsmith *
471867Smsmith * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
5151937Sjkim *              $Revision: 1.77 $
671867Smsmith *
771867Smsmith *****************************************************************************/
871867Smsmith
971867Smsmith/******************************************************************************
1071867Smsmith *
1171867Smsmith * 1. Copyright Notice
1271867Smsmith *
13151937Sjkim * Some or all of this work - Copyright (c) 1999 - 2005, 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
118151600Sobrien#include <contrib/dev/acpica/acpi.h>
11971867Smsmith
12077424Smsmith#define _COMPONENT          ACPI_HARDWARE
12191116Smsmith        ACPI_MODULE_NAME    ("hwsleep")
12271867Smsmith
12371867Smsmith
124151937Sjkim/*******************************************************************************
12571867Smsmith *
12671867Smsmith * FUNCTION:    AcpiSetFirmwareWakingVector
12771867Smsmith *
12871867Smsmith * PARAMETERS:  PhysicalAddress     - Physical address of ACPI real mode
12971867Smsmith *                                    entry point.
13071867Smsmith *
13191116Smsmith * RETURN:      Status
13271867Smsmith *
133151937Sjkim * DESCRIPTION: Access function for the FirmwareWakingVector field in FACS
13471867Smsmith *
13571867Smsmith ******************************************************************************/
13671867Smsmith
13771867SmsmithACPI_STATUS
13871867SmsmithAcpiSetFirmwareWakingVector (
13971867Smsmith    ACPI_PHYSICAL_ADDRESS PhysicalAddress)
14071867Smsmith{
14171867Smsmith
14291116Smsmith    ACPI_FUNCTION_TRACE ("AcpiSetFirmwareWakingVector");
14371867Smsmith
14471867Smsmith
14571867Smsmith    /* Set the vector */
14671867Smsmith
14791116Smsmith    if (AcpiGbl_CommonFACS.VectorWidth == 32)
14871867Smsmith    {
149102550Siwasaki        *(ACPI_CAST_PTR (UINT32, AcpiGbl_CommonFACS.FirmwareWakingVector))
15099679Siwasaki                = (UINT32) PhysicalAddress;
15171867Smsmith    }
15271867Smsmith    else
15371867Smsmith    {
154102550Siwasaki        *AcpiGbl_CommonFACS.FirmwareWakingVector
15599679Siwasaki                = PhysicalAddress;
15671867Smsmith    }
15771867Smsmith
15871867Smsmith    return_ACPI_STATUS (AE_OK);
15971867Smsmith}
16071867Smsmith
16171867Smsmith
162151937Sjkim/*******************************************************************************
16371867Smsmith *
16471867Smsmith * FUNCTION:    AcpiGetFirmwareWakingVector
16571867Smsmith *
166151937Sjkim * PARAMETERS:  *PhysicalAddress    - Where the contents of
16771867Smsmith *                                    the FirmwareWakingVector field of
168151937Sjkim *                                    the FACS will be returned.
16971867Smsmith *
170151937Sjkim * RETURN:      Status, vector
17171867Smsmith *
172151937Sjkim * DESCRIPTION: Access function for the FirmwareWakingVector field in FACS
17371867Smsmith *
17471867Smsmith ******************************************************************************/
17571867Smsmith
17671867SmsmithACPI_STATUS
17771867SmsmithAcpiGetFirmwareWakingVector (
17871867Smsmith    ACPI_PHYSICAL_ADDRESS *PhysicalAddress)
17971867Smsmith{
18071867Smsmith
18191116Smsmith    ACPI_FUNCTION_TRACE ("AcpiGetFirmwareWakingVector");
18271867Smsmith
18371867Smsmith
18471867Smsmith    if (!PhysicalAddress)
18571867Smsmith    {
18671867Smsmith        return_ACPI_STATUS (AE_BAD_PARAMETER);
18771867Smsmith    }
18871867Smsmith
18971867Smsmith    /* Get the vector */
19071867Smsmith
19191116Smsmith    if (AcpiGbl_CommonFACS.VectorWidth == 32)
19271867Smsmith    {
193102550Siwasaki        *PhysicalAddress = (ACPI_PHYSICAL_ADDRESS)
19499679Siwasaki            *(ACPI_CAST_PTR (UINT32, AcpiGbl_CommonFACS.FirmwareWakingVector));
19571867Smsmith    }
19671867Smsmith    else
19771867Smsmith    {
198102550Siwasaki        *PhysicalAddress =
19999679Siwasaki            *AcpiGbl_CommonFACS.FirmwareWakingVector;
20071867Smsmith    }
20171867Smsmith
20271867Smsmith    return_ACPI_STATUS (AE_OK);
20371867Smsmith}
20471867Smsmith
20587031Smsmith
206151937Sjkim/*******************************************************************************
20771867Smsmith *
20887031Smsmith * FUNCTION:    AcpiEnterSleepStatePrep
20971867Smsmith *
21071867Smsmith * PARAMETERS:  SleepState          - Which sleep state to enter
21171867Smsmith *
21271867Smsmith * RETURN:      Status
21371867Smsmith *
21487031Smsmith * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
21587031Smsmith *              This function must execute with interrupts enabled.
21687031Smsmith *              We break sleeping into 2 stages so that OSPM can handle
21787031Smsmith *              various OS-specific tasks between the two steps.
21871867Smsmith *
21971867Smsmith ******************************************************************************/
22071867Smsmith
22171867SmsmithACPI_STATUS
22287031SmsmithAcpiEnterSleepStatePrep (
22377424Smsmith    UINT8               SleepState)
22471867Smsmith{
22577424Smsmith    ACPI_STATUS         Status;
22677424Smsmith    ACPI_OBJECT_LIST    ArgList;
22777424Smsmith    ACPI_OBJECT         Arg;
22871867Smsmith
22977424Smsmith
23091116Smsmith    ACPI_FUNCTION_TRACE ("AcpiEnterSleepStatePrep");
23191116Smsmith
23291116Smsmith
23371867Smsmith    /*
23471867Smsmith     * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
23571867Smsmith     */
23699679Siwasaki    Status = AcpiGetSleepTypeData (SleepState,
23791116Smsmith                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
23891116Smsmith    if (ACPI_FAILURE (Status))
23971867Smsmith    {
24087031Smsmith        return_ACPI_STATUS (Status);
24171867Smsmith    }
24271867Smsmith
24391116Smsmith    /* Setup parameter object */
24477424Smsmith
24571867Smsmith    ArgList.Count = 1;
24671867Smsmith    ArgList.Pointer = &Arg;
24771867Smsmith
24871867Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
24971867Smsmith    Arg.Integer.Value = SleepState;
25071867Smsmith
25191116Smsmith    /* Run the _PTS and _GTS methods */
25277424Smsmith
253126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__PTS, &ArgList, NULL);
25491116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
25591116Smsmith    {
25691116Smsmith        return_ACPI_STATUS (Status);
25791116Smsmith    }
25891116Smsmith
259126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__GTS, &ArgList, NULL);
26091116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
26191116Smsmith    {
26291116Smsmith        return_ACPI_STATUS (Status);
26391116Smsmith    }
26491116Smsmith
265126372Snjl    /* Setup the argument to _SST */
266126372Snjl
267126372Snjl    switch (SleepState)
268126372Snjl    {
269126372Snjl    case ACPI_STATE_S0:
270126372Snjl        Arg.Integer.Value = ACPI_SST_WORKING;
271126372Snjl        break;
272126372Snjl
273126372Snjl    case ACPI_STATE_S1:
274126372Snjl    case ACPI_STATE_S2:
275126372Snjl    case ACPI_STATE_S3:
276126372Snjl        Arg.Integer.Value = ACPI_SST_SLEEPING;
277126372Snjl        break;
278126372Snjl
279126372Snjl    case ACPI_STATE_S4:
280126372Snjl        Arg.Integer.Value = ACPI_SST_SLEEP_CONTEXT;
281126372Snjl        break;
282126372Snjl
283126372Snjl    default:
284151937Sjkim        Arg.Integer.Value = ACPI_SST_INDICATOR_OFF; /* Default is off */
285126372Snjl        break;
286126372Snjl    }
287126372Snjl
288123315Snjl    /* Set the system indicators to show the desired sleep state. */
289123315Snjl
290126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
291123315Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
292123315Snjl    {
293151937Sjkim         ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
294151937Sjkim            AcpiFormatException (Status)));
295123315Snjl    }
296123315Snjl
29787031Smsmith    return_ACPI_STATUS (AE_OK);
29887031Smsmith}
29987031Smsmith
30087031Smsmith
301151937Sjkim/*******************************************************************************
30287031Smsmith *
30387031Smsmith * FUNCTION:    AcpiEnterSleepState
30487031Smsmith *
30587031Smsmith * PARAMETERS:  SleepState          - Which sleep state to enter
30687031Smsmith *
30787031Smsmith * RETURN:      Status
30887031Smsmith *
30987031Smsmith * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
31087031Smsmith *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
31187031Smsmith *
31287031Smsmith ******************************************************************************/
31387031Smsmith
31487031SmsmithACPI_STATUS
31587031SmsmithAcpiEnterSleepState (
31691116Smsmith    UINT8                   SleepState)
31787031Smsmith{
31899679Siwasaki    UINT32                  PM1AControl;
31999679Siwasaki    UINT32                  PM1BControl;
32091116Smsmith    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
32191116Smsmith    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
32299679Siwasaki    UINT32                  InValue;
32399679Siwasaki    ACPI_STATUS             Status;
32487031Smsmith
32587031Smsmith
32691116Smsmith    ACPI_FUNCTION_TRACE ("AcpiEnterSleepState");
32787031Smsmith
32891116Smsmith
32991116Smsmith    if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
33091116Smsmith        (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
33187031Smsmith    {
33299679Siwasaki        ACPI_REPORT_ERROR (("Sleep values out of range: A=%X B=%X\n",
33391116Smsmith            AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
33491116Smsmith        return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
33587031Smsmith    }
33687031Smsmith
33791116Smsmith    SleepTypeRegInfo   = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE_A);
33891116Smsmith    SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
33971867Smsmith
340128245Snjl    /* Clear wake status */
341128245Snjl
342128245Snjl    Status = AcpiSetRegister (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
343128245Snjl    if (ACPI_FAILURE (Status))
34499679Siwasaki    {
345128245Snjl        return_ACPI_STATUS (Status);
346128245Snjl    }
34778986Smsmith
348129684Snjl    /* Clear all fixed and general purpose status bits */
349129684Snjl
350128245Snjl    Status = AcpiHwClearAcpiStatus (ACPI_MTX_DO_NOT_LOCK);
351128245Snjl    if (ACPI_FAILURE (Status))
352128245Snjl    {
353128245Snjl        return_ACPI_STATUS (Status);
354128245Snjl    }
35587031Smsmith
356128245Snjl    if (SleepState != ACPI_STATE_S5)
357128245Snjl    {
358123315Snjl        /* Disable BM arbitration */
359123315Snjl
360151937Sjkim        Status = AcpiSetRegister (ACPI_BITREG_ARB_DISABLE,
361151937Sjkim                    1, ACPI_MTX_DO_NOT_LOCK);
362123315Snjl        if (ACPI_FAILURE (Status))
363123315Snjl        {
364123315Snjl            return_ACPI_STATUS (Status);
365123315Snjl        }
36699679Siwasaki    }
36799679Siwasaki
368128212Snjl    /*
369129684Snjl     * 1) Disable/Clear all GPEs
370128212Snjl     * 2) Enable all wakeup GPEs
371128212Snjl     */
372151937Sjkim    Status = AcpiHwDisableAllGpes ();
37399679Siwasaki    if (ACPI_FAILURE (Status))
37499679Siwasaki    {
37599679Siwasaki        return_ACPI_STATUS (Status);
37699679Siwasaki    }
377129684Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
37899679Siwasaki
379151937Sjkim    Status = AcpiHwEnableAllWakeupGpes ();
380129684Snjl    if (ACPI_FAILURE (Status))
381129684Snjl    {
382129684Snjl        return_ACPI_STATUS (Status);
383129684Snjl    }
384129684Snjl
38591116Smsmith    /* Get current value of PM1A control */
38678986Smsmith
387151937Sjkim    Status = AcpiHwRegisterRead (ACPI_MTX_DO_NOT_LOCK,
388151937Sjkim                ACPI_REGISTER_PM1_CONTROL, &PM1AControl);
38999679Siwasaki    if (ACPI_FAILURE (Status))
39099679Siwasaki    {
39199679Siwasaki        return_ACPI_STATUS (Status);
39299679Siwasaki    }
393151937Sjkim    ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
394151937Sjkim        "Entering sleep state [S%d]\n", SleepState));
39582367Smsmith
39691116Smsmith    /* Clear SLP_EN and SLP_TYP fields */
39782367Smsmith
398151937Sjkim    PM1AControl &= ~(SleepTypeRegInfo->AccessBitMask |
399151937Sjkim                     SleepEnableRegInfo->AccessBitMask);
40071867Smsmith    PM1BControl = PM1AControl;
40171867Smsmith
40291116Smsmith    /* Insert SLP_TYP bits */
40382367Smsmith
40491116Smsmith    PM1AControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
40591116Smsmith    PM1BControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
40671867Smsmith
407126372Snjl    /*
408126372Snjl     * We split the writes of SLP_TYP and SLP_EN to workaround
409126372Snjl     * poorly implemented hardware.
410126372Snjl     */
411126372Snjl
41291116Smsmith    /* Write #1: fill in SLP_TYP data */
41371867Smsmith
414151937Sjkim    Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
415151937Sjkim                ACPI_REGISTER_PM1A_CONTROL, PM1AControl);
41699679Siwasaki    if (ACPI_FAILURE (Status))
41799679Siwasaki    {
41899679Siwasaki        return_ACPI_STATUS (Status);
41999679Siwasaki    }
42082367Smsmith
421151937Sjkim    Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
422151937Sjkim                ACPI_REGISTER_PM1B_CONTROL, PM1BControl);
42399679Siwasaki    if (ACPI_FAILURE (Status))
42499679Siwasaki    {
42599679Siwasaki        return_ACPI_STATUS (Status);
42699679Siwasaki    }
42799679Siwasaki
42891116Smsmith    /* Insert SLP_ENABLE bit */
42982367Smsmith
43091116Smsmith    PM1AControl |= SleepEnableRegInfo->AccessBitMask;
43191116Smsmith    PM1BControl |= SleepEnableRegInfo->AccessBitMask;
43271867Smsmith
43391116Smsmith    /* Write #2: SLP_TYP + SLP_EN */
43471867Smsmith
43599679Siwasaki    ACPI_FLUSH_CPU_CACHE ();
43680062Smsmith
437151937Sjkim    Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
438151937Sjkim                ACPI_REGISTER_PM1A_CONTROL, PM1AControl);
43999679Siwasaki    if (ACPI_FAILURE (Status))
44099679Siwasaki    {
44199679Siwasaki        return_ACPI_STATUS (Status);
44299679Siwasaki    }
44399679Siwasaki
444151937Sjkim    Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
445151937Sjkim                ACPI_REGISTER_PM1B_CONTROL, PM1BControl);
44699679Siwasaki    if (ACPI_FAILURE (Status))
44799679Siwasaki    {
44899679Siwasaki        return_ACPI_STATUS (Status);
44999679Siwasaki    }
45099679Siwasaki
45182367Smsmith    if (SleepState > ACPI_STATE_S3)
45282367Smsmith    {
45399679Siwasaki        /*
454151937Sjkim         * We wanted to sleep > S3, but it didn't happen (by virtue of the
455151937Sjkim         * fact that we are still executing!)
456123315Snjl         *
457151937Sjkim         * Wait ten seconds, then try again. This is to get S4/S5 to work on
458151937Sjkim         * all machines.
459123315Snjl         *
46099679Siwasaki         * We wait so long to allow chipsets that poll this reg very slowly to
461126372Snjl         * still read the right value. Ideally, this block would go
46299679Siwasaki         * away entirely.
46399679Siwasaki         */
46499679Siwasaki        AcpiOsStall (10000000);
46582367Smsmith
466151937Sjkim        Status = AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
467151937Sjkim                    ACPI_REGISTER_PM1_CONTROL,
46899679Siwasaki                    SleepEnableRegInfo->AccessBitMask);
46999679Siwasaki        if (ACPI_FAILURE (Status))
47099679Siwasaki        {
47199679Siwasaki            return_ACPI_STATUS (Status);
47299679Siwasaki        }
47380357Speter    }
47480062Smsmith
47591116Smsmith    /* Wait until we enter sleep state */
47671867Smsmith
477102550Siwasaki    do
47883174Smsmith    {
479151937Sjkim        Status = AcpiGetRegister (ACPI_BITREG_WAKE_STATUS, &InValue,
480151937Sjkim            ACPI_MTX_DO_NOT_LOCK);
48199679Siwasaki        if (ACPI_FAILURE (Status))
48299679Siwasaki        {
48399679Siwasaki            return_ACPI_STATUS (Status);
48499679Siwasaki        }
48599679Siwasaki
48691116Smsmith        /* Spin until we wake */
48782367Smsmith
48899679Siwasaki    } while (!InValue);
48999679Siwasaki
49071867Smsmith    return_ACPI_STATUS (AE_OK);
49171867Smsmith}
49282367Smsmith
493114237Snjl
494151937Sjkim/*******************************************************************************
49582367Smsmith *
496114237Snjl * FUNCTION:    AcpiEnterSleepStateS4bios
497114237Snjl *
498114237Snjl * PARAMETERS:  None
499114237Snjl *
500114237Snjl * RETURN:      Status
501114237Snjl *
502114237Snjl * DESCRIPTION: Perform a S4 bios request.
503114237Snjl *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
504114237Snjl *
505114237Snjl ******************************************************************************/
506114237Snjl
507114237SnjlACPI_STATUS
508114237SnjlAcpiEnterSleepStateS4bios (
509114237Snjl    void)
510114237Snjl{
511114237Snjl    UINT32                  InValue;
512114237Snjl    ACPI_STATUS             Status;
513114237Snjl
514114237Snjl
515114237Snjl    ACPI_FUNCTION_TRACE ("AcpiEnterSleepStateS4bios");
516114237Snjl
517123315Snjl
518126372Snjl    Status = AcpiSetRegister (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
519126372Snjl    if (ACPI_FAILURE (Status))
520126372Snjl    {
521126372Snjl        return_ACPI_STATUS (Status);
522126372Snjl    }
523114237Snjl
524126372Snjl    Status = AcpiHwClearAcpiStatus (ACPI_MTX_DO_NOT_LOCK);
525126372Snjl    if (ACPI_FAILURE (Status))
526126372Snjl    {
527126372Snjl        return_ACPI_STATUS (Status);
528126372Snjl    }
529114237Snjl
530128212Snjl    /*
531129684Snjl     * 1) Disable/Clear all GPEs
532128212Snjl     * 2) Enable all wakeup GPEs
533128212Snjl     */
534151937Sjkim    Status = AcpiHwDisableAllGpes ();
535126372Snjl    if (ACPI_FAILURE (Status))
536126372Snjl    {
537126372Snjl        return_ACPI_STATUS (Status);
538126372Snjl    }
539129684Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
540114237Snjl
541151937Sjkim    Status = AcpiHwEnableAllWakeupGpes ();
542129684Snjl    if (ACPI_FAILURE (Status))
543129684Snjl    {
544129684Snjl        return_ACPI_STATUS (Status);
545129684Snjl    }
546129684Snjl
547126372Snjl    ACPI_FLUSH_CPU_CACHE ();
548126372Snjl
549151937Sjkim    Status = AcpiOsWritePort (AcpiGbl_FADT->SmiCmd,
550151937Sjkim                (UINT32) AcpiGbl_FADT->S4BiosReq, 8);
551114237Snjl
552114237Snjl    do {
553114237Snjl        AcpiOsStall(1000);
554151937Sjkim        Status = AcpiGetRegister (ACPI_BITREG_WAKE_STATUS, &InValue,
555151937Sjkim            ACPI_MTX_DO_NOT_LOCK);
556114237Snjl        if (ACPI_FAILURE (Status))
557114237Snjl        {
558114237Snjl            return_ACPI_STATUS (Status);
559114237Snjl        }
560114237Snjl    } while (!InValue);
561114237Snjl
562114237Snjl    return_ACPI_STATUS (AE_OK);
563114237Snjl}
564114237Snjl
565114237Snjl
566151937Sjkim/*******************************************************************************
567114237Snjl *
56882367Smsmith * FUNCTION:    AcpiLeaveSleepState
56982367Smsmith *
57082367Smsmith * PARAMETERS:  SleepState          - Which sleep state we just exited
57182367Smsmith *
57282367Smsmith * RETURN:      Status
57382367Smsmith *
57482367Smsmith * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
575138287Smarks *              Called with interrupts ENABLED.
57682367Smsmith *
57782367Smsmith ******************************************************************************/
57882367Smsmith
57982367SmsmithACPI_STATUS
58082367SmsmithAcpiLeaveSleepState (
581123315Snjl    UINT8                   SleepState)
58282367Smsmith{
583123315Snjl    ACPI_OBJECT_LIST        ArgList;
584123315Snjl    ACPI_OBJECT             Arg;
585123315Snjl    ACPI_STATUS             Status;
586123315Snjl    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
587123315Snjl    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
588126372Snjl    UINT32                  PM1AControl;
589126372Snjl    UINT32                  PM1BControl;
59082367Smsmith
59183174Smsmith
59291116Smsmith    ACPI_FUNCTION_TRACE ("AcpiLeaveSleepState");
59382367Smsmith
59491116Smsmith
595126372Snjl    /*
596126372Snjl     * Set SLP_TYPE and SLP_EN to state S0.
597126372Snjl     * This is unclear from the ACPI Spec, but it is required
598126372Snjl     * by some machines.
599126372Snjl     */
600126372Snjl    Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
601126372Snjl                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
602123315Snjl    if (ACPI_SUCCESS (Status))
603123315Snjl    {
604126372Snjl        SleepTypeRegInfo   = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE_A);
605126372Snjl        SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
606123315Snjl
607126372Snjl        /* Get current value of PM1A control */
608123315Snjl
609126372Snjl        Status = AcpiHwRegisterRead (ACPI_MTX_DO_NOT_LOCK,
610126372Snjl                    ACPI_REGISTER_PM1_CONTROL, &PM1AControl);
611126372Snjl        if (ACPI_SUCCESS (Status))
612126372Snjl        {
613126372Snjl            /* Clear SLP_EN and SLP_TYP fields */
614126372Snjl
615126372Snjl            PM1AControl &= ~(SleepTypeRegInfo->AccessBitMask |
616126372Snjl                             SleepEnableRegInfo->AccessBitMask);
617126372Snjl            PM1BControl = PM1AControl;
618126372Snjl
619126372Snjl            /* Insert SLP_TYP bits */
620126372Snjl
621126372Snjl            PM1AControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
622126372Snjl            PM1BControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
623126372Snjl
624126372Snjl            /* Just ignore any errors */
625126372Snjl
626126372Snjl            (void) AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
627126372Snjl                            ACPI_REGISTER_PM1A_CONTROL, PM1AControl);
628126372Snjl            (void) AcpiHwRegisterWrite (ACPI_MTX_DO_NOT_LOCK,
629126372Snjl                            ACPI_REGISTER_PM1B_CONTROL, PM1BControl);
630126372Snjl        }
631123315Snjl    }
632123315Snjl
63387031Smsmith    /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
63482367Smsmith
63591116Smsmith    AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
63691116Smsmith
63791116Smsmith    /* Setup parameter object */
63891116Smsmith
63982367Smsmith    ArgList.Count = 1;
64082367Smsmith    ArgList.Pointer = &Arg;
64182367Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
64282367Smsmith
64391116Smsmith    /* Ignore any errors from these methods */
64483174Smsmith
645126372Snjl    Arg.Integer.Value = ACPI_SST_WAKING;
646126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
647123315Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
648123315Snjl    {
649151937Sjkim        ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
650151937Sjkim            AcpiFormatException (Status)));
651123315Snjl    }
652123315Snjl
653123315Snjl    Arg.Integer.Value = SleepState;
654126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__BFS, &ArgList, NULL);
65591116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
65691116Smsmith    {
657151937Sjkim        ACPI_REPORT_ERROR (("Method _BFS failed, %s\n",
658151937Sjkim            AcpiFormatException (Status)));
65991116Smsmith    }
66091116Smsmith
661126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__WAK, &ArgList, NULL);
66291116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
66391116Smsmith    {
664151937Sjkim        ACPI_REPORT_ERROR (("Method _WAK failed, %s\n",
665151937Sjkim            AcpiFormatException (Status)));
66691116Smsmith    }
667128212Snjl    /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
66891116Smsmith
669128212Snjl    /*
670128212Snjl     * Restore the GPEs:
671129684Snjl     * 1) Disable/Clear all GPEs
672128212Snjl     * 2) Enable all runtime GPEs
673128212Snjl     */
674151937Sjkim    Status = AcpiHwDisableAllGpes ();
67599679Siwasaki    if (ACPI_FAILURE (Status))
67699679Siwasaki    {
67799679Siwasaki        return_ACPI_STATUS (Status);
67899679Siwasaki    }
679129684Snjl    AcpiGbl_SystemAwakeAndRunning = TRUE;
68082367Smsmith
681151937Sjkim    Status = AcpiHwEnableAllRuntimeGpes ();
682129684Snjl    if (ACPI_FAILURE (Status))
683129684Snjl    {
684129684Snjl        return_ACPI_STATUS (Status);
685129684Snjl    }
686129684Snjl
687126372Snjl    /* Enable power button */
688126372Snjl
689151937Sjkim    (void) AcpiSetRegister(
690151937Sjkim            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,
691126372Snjl            1, ACPI_MTX_DO_NOT_LOCK);
692151937Sjkim
693151937Sjkim    (void) AcpiSetRegister(
694151937Sjkim            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,
695126372Snjl            1, ACPI_MTX_DO_NOT_LOCK);
696126372Snjl
697123315Snjl    /* Enable BM arbitration */
698123315Snjl
69999679Siwasaki    Status = AcpiSetRegister (ACPI_BITREG_ARB_DISABLE, 0, ACPI_MTX_LOCK);
700126372Snjl    if (ACPI_FAILURE (Status))
701126372Snjl    {
702126372Snjl        return_ACPI_STATUS (Status);
703126372Snjl    }
704126372Snjl
705126372Snjl    Arg.Integer.Value = ACPI_SST_WORKING;
706126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
707126372Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
708126372Snjl    {
709151937Sjkim        ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
710151937Sjkim            AcpiFormatException (Status)));
711126372Snjl    }
712126372Snjl
71399679Siwasaki    return_ACPI_STATUS (Status);
71482367Smsmith}
715