hwsleep.c revision 193267
171867Smsmith
271867Smsmith/******************************************************************************
371867Smsmith *
471867Smsmith * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
571867Smsmith *
671867Smsmith *****************************************************************************/
771867Smsmith
871867Smsmith/******************************************************************************
971867Smsmith *
1071867Smsmith * 1. Copyright Notice
1171867Smsmith *
12193267Sjkim * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
1371867Smsmith * 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
117193251Sjkim#include "acpi.h"
118193267Sjkim#include "accommon.h"
11971867Smsmith
12077424Smsmith#define _COMPONENT          ACPI_HARDWARE
12191116Smsmith        ACPI_MODULE_NAME    ("hwsleep")
12271867Smsmith
12371867Smsmith
124151937Sjkim/*******************************************************************************
12571867Smsmith *
12671867Smsmith * FUNCTION:    AcpiSetFirmwareWakingVector
12771867Smsmith *
128193267Sjkim * PARAMETERS:  PhysicalAddress     - 32-bit physical address of ACPI real mode
12971867Smsmith *                                    entry point.
13071867Smsmith *
13191116Smsmith * RETURN:      Status
13271867Smsmith *
133193267Sjkim * DESCRIPTION: Sets the 32-bit FirmwareWakingVector field of the FACS
13471867Smsmith *
13571867Smsmith ******************************************************************************/
13671867Smsmith
13771867SmsmithACPI_STATUS
13871867SmsmithAcpiSetFirmwareWakingVector (
139193267Sjkim    UINT32                  PhysicalAddress)
14071867Smsmith{
141193267Sjkim    ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector);
14271867Smsmith
14371867Smsmith
144193267Sjkim    /* Set the 32-bit vector */
14571867Smsmith
146193267Sjkim    AcpiGbl_FACS->FirmwareWakingVector = PhysicalAddress;
147167802Sjkim
148193267Sjkim    /* Clear the 64-bit vector if it exists */
149167802Sjkim
150193267Sjkim    if ((AcpiGbl_FACS->Length > 32) && (AcpiGbl_FACS->Version >= 1))
15171867Smsmith    {
152193267Sjkim        AcpiGbl_FACS->XFirmwareWakingVector = 0;
15371867Smsmith    }
15471867Smsmith
15571867Smsmith    return_ACPI_STATUS (AE_OK);
15671867Smsmith}
15771867Smsmith
158167802SjkimACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector)
15971867Smsmith
160167802Sjkim
161193267Sjkim#if ACPI_MACHINE_WIDTH == 64
162151937Sjkim/*******************************************************************************
16371867Smsmith *
164193267Sjkim * FUNCTION:    AcpiSetFirmwareWakingVector64
16571867Smsmith *
166193267Sjkim * PARAMETERS:  PhysicalAddress     - 64-bit physical address of ACPI protected
167193267Sjkim *                                    mode entry point.
16871867Smsmith *
169193267Sjkim * RETURN:      Status
17071867Smsmith *
171193267Sjkim * DESCRIPTION: Sets the 64-bit X_FirmwareWakingVector field of the FACS, if
172193267Sjkim *              it exists in the table. This function is intended for use with
173193267Sjkim *              64-bit host operating systems.
17471867Smsmith *
17571867Smsmith ******************************************************************************/
17671867Smsmith
17771867SmsmithACPI_STATUS
178193267SjkimAcpiSetFirmwareWakingVector64 (
179193267Sjkim    UINT64                  PhysicalAddress)
18071867Smsmith{
181193267Sjkim    ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector64);
18271867Smsmith
18371867Smsmith
184193267Sjkim    /* Determine if the 64-bit vector actually exists */
18571867Smsmith
186193267Sjkim    if ((AcpiGbl_FACS->Length <= 32) || (AcpiGbl_FACS->Version < 1))
18771867Smsmith    {
188193267Sjkim        return_ACPI_STATUS (AE_NOT_EXIST);
18971867Smsmith    }
19071867Smsmith
191193267Sjkim    /* Clear 32-bit vector, set the 64-bit X_ vector */
192167802Sjkim
193193267Sjkim    AcpiGbl_FACS->FirmwareWakingVector = 0;
194193267Sjkim    AcpiGbl_FACS->XFirmwareWakingVector = PhysicalAddress;
19571867Smsmith    return_ACPI_STATUS (AE_OK);
19671867Smsmith}
19771867Smsmith
198193267SjkimACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector64)
199193267Sjkim#endif
20087031Smsmith
201151937Sjkim/*******************************************************************************
20271867Smsmith *
20387031Smsmith * FUNCTION:    AcpiEnterSleepStatePrep
20471867Smsmith *
20571867Smsmith * PARAMETERS:  SleepState          - Which sleep state to enter
20671867Smsmith *
20771867Smsmith * RETURN:      Status
20871867Smsmith *
20987031Smsmith * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
21087031Smsmith *              This function must execute with interrupts enabled.
21187031Smsmith *              We break sleeping into 2 stages so that OSPM can handle
21287031Smsmith *              various OS-specific tasks between the two steps.
21371867Smsmith *
21471867Smsmith ******************************************************************************/
21571867Smsmith
21671867SmsmithACPI_STATUS
21787031SmsmithAcpiEnterSleepStatePrep (
218193267Sjkim    UINT8                   SleepState)
21971867Smsmith{
220193267Sjkim    ACPI_STATUS             Status;
221193267Sjkim    ACPI_OBJECT_LIST        ArgList;
222193267Sjkim    ACPI_OBJECT             Arg;
22371867Smsmith
22477424Smsmith
225167802Sjkim    ACPI_FUNCTION_TRACE (AcpiEnterSleepStatePrep);
22691116Smsmith
22791116Smsmith
228193267Sjkim    /* _PSW methods could be run here to enable wake-on keyboard, LAN, etc. */
229193267Sjkim
23099679Siwasaki    Status = AcpiGetSleepTypeData (SleepState,
23191116Smsmith                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
23291116Smsmith    if (ACPI_FAILURE (Status))
23371867Smsmith    {
23487031Smsmith        return_ACPI_STATUS (Status);
23571867Smsmith    }
23671867Smsmith
237193267Sjkim    /* Execute the _PTS method (Prepare To Sleep) */
23877424Smsmith
23971867Smsmith    ArgList.Count = 1;
24071867Smsmith    ArgList.Pointer = &Arg;
24171867Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
24271867Smsmith    Arg.Integer.Value = SleepState;
24371867Smsmith
244126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__PTS, &ArgList, NULL);
24591116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
24691116Smsmith    {
24791116Smsmith        return_ACPI_STATUS (Status);
24891116Smsmith    }
24991116Smsmith
250193267Sjkim    /* Setup the argument to the _SST method (System STatus) */
25191116Smsmith
252126372Snjl    switch (SleepState)
253126372Snjl    {
254126372Snjl    case ACPI_STATE_S0:
255126372Snjl        Arg.Integer.Value = ACPI_SST_WORKING;
256126372Snjl        break;
257126372Snjl
258126372Snjl    case ACPI_STATE_S1:
259126372Snjl    case ACPI_STATE_S2:
260126372Snjl    case ACPI_STATE_S3:
261126372Snjl        Arg.Integer.Value = ACPI_SST_SLEEPING;
262126372Snjl        break;
263126372Snjl
264126372Snjl    case ACPI_STATE_S4:
265126372Snjl        Arg.Integer.Value = ACPI_SST_SLEEP_CONTEXT;
266126372Snjl        break;
267126372Snjl
268126372Snjl    default:
269151937Sjkim        Arg.Integer.Value = ACPI_SST_INDICATOR_OFF; /* Default is off */
270126372Snjl        break;
271126372Snjl    }
272126372Snjl
273193267Sjkim    /*
274193267Sjkim     * Set the system indicators to show the desired sleep state.
275193267Sjkim     * _SST is an optional method (return no error if not found)
276193267Sjkim     */
277126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
278123315Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
279123315Snjl    {
280167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "While executing method _SST"));
281123315Snjl    }
282123315Snjl
28387031Smsmith    return_ACPI_STATUS (AE_OK);
28487031Smsmith}
28587031Smsmith
286167802SjkimACPI_EXPORT_SYMBOL (AcpiEnterSleepStatePrep)
28787031Smsmith
288167802Sjkim
289151937Sjkim/*******************************************************************************
29087031Smsmith *
29187031Smsmith * FUNCTION:    AcpiEnterSleepState
29287031Smsmith *
29387031Smsmith * PARAMETERS:  SleepState          - Which sleep state to enter
29487031Smsmith *
29587031Smsmith * RETURN:      Status
29687031Smsmith *
297193267Sjkim * DESCRIPTION: Enter a system sleep state
29887031Smsmith *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
29987031Smsmith *
30087031Smsmith ******************************************************************************/
30187031Smsmith
30287031SmsmithACPI_STATUS
30387031SmsmithAcpiEnterSleepState (
30491116Smsmith    UINT8                   SleepState)
30587031Smsmith{
306193267Sjkim    UINT32                  Pm1aControl;
307193267Sjkim    UINT32                  Pm1bControl;
30891116Smsmith    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
30991116Smsmith    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
31099679Siwasaki    UINT32                  InValue;
311193267Sjkim    ACPI_OBJECT_LIST        ArgList;
312193267Sjkim    ACPI_OBJECT             Arg;
31399679Siwasaki    ACPI_STATUS             Status;
31487031Smsmith
31587031Smsmith
316167802Sjkim    ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
31787031Smsmith
31891116Smsmith
31991116Smsmith    if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
32091116Smsmith        (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
32187031Smsmith    {
322167802Sjkim        ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=%X B=%X",
32391116Smsmith            AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
32491116Smsmith        return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
32587031Smsmith    }
32687031Smsmith
327193267Sjkim    SleepTypeRegInfo   = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
32891116Smsmith    SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
32971867Smsmith
330128245Snjl    /* Clear wake status */
331128245Snjl
332193267Sjkim    Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
333128245Snjl    if (ACPI_FAILURE (Status))
33499679Siwasaki    {
335128245Snjl        return_ACPI_STATUS (Status);
336128245Snjl    }
33778986Smsmith
338129684Snjl    /* Clear all fixed and general purpose status bits */
339129684Snjl
340167802Sjkim    Status = AcpiHwClearAcpiStatus ();
341128245Snjl    if (ACPI_FAILURE (Status))
342128245Snjl    {
343128245Snjl        return_ACPI_STATUS (Status);
344128245Snjl    }
34587031Smsmith
346128245Snjl    if (SleepState != ACPI_STATE_S5)
347128245Snjl    {
348123315Snjl        /* Disable BM arbitration */
349123315Snjl
350193267Sjkim        Status = AcpiWriteBitRegister (ACPI_BITREG_ARB_DISABLE, 1);
351123315Snjl        if (ACPI_FAILURE (Status))
352123315Snjl        {
353123315Snjl            return_ACPI_STATUS (Status);
354123315Snjl        }
35599679Siwasaki    }
35699679Siwasaki
357128212Snjl    /*
358129684Snjl     * 1) Disable/Clear all GPEs
359128212Snjl     * 2) Enable all wakeup GPEs
360128212Snjl     */
361151937Sjkim    Status = AcpiHwDisableAllGpes ();
36299679Siwasaki    if (ACPI_FAILURE (Status))
36399679Siwasaki    {
36499679Siwasaki        return_ACPI_STATUS (Status);
36599679Siwasaki    }
366129684Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
36799679Siwasaki
368151937Sjkim    Status = AcpiHwEnableAllWakeupGpes ();
369129684Snjl    if (ACPI_FAILURE (Status))
370129684Snjl    {
371129684Snjl        return_ACPI_STATUS (Status);
372129684Snjl    }
373129684Snjl
374193267Sjkim    /* Execute the _GTS method (Going To Sleep) */
375193267Sjkim
376193267Sjkim    ArgList.Count = 1;
377193267Sjkim    ArgList.Pointer = &Arg;
378193267Sjkim    Arg.Type = ACPI_TYPE_INTEGER;
379193267Sjkim    Arg.Integer.Value = SleepState;
380193267Sjkim
381193267Sjkim    Status = AcpiEvaluateObject (NULL, METHOD_NAME__GTS, &ArgList, NULL);
382193267Sjkim    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
383193267Sjkim    {
384193267Sjkim        return_ACPI_STATUS (Status);
385193267Sjkim    }
386193267Sjkim
38791116Smsmith    /* Get current value of PM1A control */
38878986Smsmith
389193267Sjkim    Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
390193267Sjkim                &Pm1aControl);
39199679Siwasaki    if (ACPI_FAILURE (Status))
39299679Siwasaki    {
39399679Siwasaki        return_ACPI_STATUS (Status);
39499679Siwasaki    }
395151937Sjkim    ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
396151937Sjkim        "Entering sleep state [S%d]\n", SleepState));
39782367Smsmith
398193267Sjkim    /* Clear the SLP_EN and SLP_TYP fields */
39982367Smsmith
400193267Sjkim    Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
401151937Sjkim                     SleepEnableRegInfo->AccessBitMask);
402193267Sjkim    Pm1bControl = Pm1aControl;
40371867Smsmith
404193267Sjkim    /* Insert the SLP_TYP bits */
40582367Smsmith
406193267Sjkim    Pm1aControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
407193267Sjkim    Pm1bControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
40871867Smsmith
409126372Snjl    /*
410126372Snjl     * We split the writes of SLP_TYP and SLP_EN to workaround
411126372Snjl     * poorly implemented hardware.
412126372Snjl     */
413126372Snjl
414193267Sjkim    /* Write #1: write the SLP_TYP data to the PM1 Control registers */
41571867Smsmith
416193267Sjkim    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
41799679Siwasaki    if (ACPI_FAILURE (Status))
41899679Siwasaki    {
41999679Siwasaki        return_ACPI_STATUS (Status);
42099679Siwasaki    }
42182367Smsmith
422193267Sjkim    /* Insert the sleep enable (SLP_EN) bit */
42399679Siwasaki
424193267Sjkim    Pm1aControl |= SleepEnableRegInfo->AccessBitMask;
425193267Sjkim    Pm1bControl |= SleepEnableRegInfo->AccessBitMask;
42682367Smsmith
427193267Sjkim    /* Flush caches, as per ACPI specification */
42871867Smsmith
42999679Siwasaki    ACPI_FLUSH_CPU_CACHE ();
43080062Smsmith
431193267Sjkim    /* Write #2: Write both SLP_TYP + SLP_EN */
43299679Siwasaki
433193267Sjkim    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
43499679Siwasaki    if (ACPI_FAILURE (Status))
43599679Siwasaki    {
43699679Siwasaki        return_ACPI_STATUS (Status);
43799679Siwasaki    }
43899679Siwasaki
43982367Smsmith    if (SleepState > ACPI_STATE_S3)
44082367Smsmith    {
44199679Siwasaki        /*
442151937Sjkim         * We wanted to sleep > S3, but it didn't happen (by virtue of the
443151937Sjkim         * fact that we are still executing!)
444123315Snjl         *
445151937Sjkim         * Wait ten seconds, then try again. This is to get S4/S5 to work on
446151937Sjkim         * all machines.
447123315Snjl         *
448193267Sjkim         * We wait so long to allow chipsets that poll this reg very slowly
449193267Sjkim         * to still read the right value. Ideally, this block would go
45099679Siwasaki         * away entirely.
45199679Siwasaki         */
45299679Siwasaki        AcpiOsStall (10000000);
45382367Smsmith
454193267Sjkim        Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_CONTROL,
45599679Siwasaki                    SleepEnableRegInfo->AccessBitMask);
45699679Siwasaki        if (ACPI_FAILURE (Status))
45799679Siwasaki        {
45899679Siwasaki            return_ACPI_STATUS (Status);
45999679Siwasaki        }
46080357Speter    }
46180062Smsmith
46291116Smsmith    /* Wait until we enter sleep state */
46371867Smsmith
464102550Siwasaki    do
46583174Smsmith    {
466193267Sjkim        Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
46799679Siwasaki        if (ACPI_FAILURE (Status))
46899679Siwasaki        {
46999679Siwasaki            return_ACPI_STATUS (Status);
47099679Siwasaki        }
47199679Siwasaki
47291116Smsmith        /* Spin until we wake */
47382367Smsmith
47499679Siwasaki    } while (!InValue);
47599679Siwasaki
47671867Smsmith    return_ACPI_STATUS (AE_OK);
47771867Smsmith}
47882367Smsmith
479167802SjkimACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
480114237Snjl
481167802Sjkim
482151937Sjkim/*******************************************************************************
48382367Smsmith *
484114237Snjl * FUNCTION:    AcpiEnterSleepStateS4bios
485114237Snjl *
486114237Snjl * PARAMETERS:  None
487114237Snjl *
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
503167802Sjkim    ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
504114237Snjl
505123315Snjl
506193267Sjkim    /* Clear the wake status bit (PM1) */
507193267Sjkim
508193267Sjkim    Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
509126372Snjl    if (ACPI_FAILURE (Status))
510126372Snjl    {
511126372Snjl        return_ACPI_STATUS (Status);
512126372Snjl    }
513114237Snjl
514167802Sjkim    Status = AcpiHwClearAcpiStatus ();
515126372Snjl    if (ACPI_FAILURE (Status))
516126372Snjl    {
517126372Snjl        return_ACPI_STATUS (Status);
518126372Snjl    }
519114237Snjl
520128212Snjl    /*
521129684Snjl     * 1) Disable/Clear all GPEs
522128212Snjl     * 2) Enable all wakeup GPEs
523128212Snjl     */
524151937Sjkim    Status = AcpiHwDisableAllGpes ();
525126372Snjl    if (ACPI_FAILURE (Status))
526126372Snjl    {
527126372Snjl        return_ACPI_STATUS (Status);
528126372Snjl    }
529129684Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
530114237Snjl
531151937Sjkim    Status = AcpiHwEnableAllWakeupGpes ();
532129684Snjl    if (ACPI_FAILURE (Status))
533129684Snjl    {
534129684Snjl        return_ACPI_STATUS (Status);
535129684Snjl    }
536129684Snjl
537126372Snjl    ACPI_FLUSH_CPU_CACHE ();
538126372Snjl
539193267Sjkim    Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
540167802Sjkim                (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
541114237Snjl
542114237Snjl    do {
543114237Snjl        AcpiOsStall(1000);
544193267Sjkim        Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
545114237Snjl        if (ACPI_FAILURE (Status))
546114237Snjl        {
547114237Snjl            return_ACPI_STATUS (Status);
548114237Snjl        }
549114237Snjl    } while (!InValue);
550114237Snjl
551114237Snjl    return_ACPI_STATUS (AE_OK);
552114237Snjl}
553114237Snjl
554167802SjkimACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
555114237Snjl
556167802Sjkim
557151937Sjkim/*******************************************************************************
558114237Snjl *
55982367Smsmith * FUNCTION:    AcpiLeaveSleepState
56082367Smsmith *
56182367Smsmith * PARAMETERS:  SleepState          - Which sleep state we just exited
56282367Smsmith *
56382367Smsmith * RETURN:      Status
56482367Smsmith *
56582367Smsmith * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
566138287Smarks *              Called with interrupts ENABLED.
56782367Smsmith *
56882367Smsmith ******************************************************************************/
56982367Smsmith
57082367SmsmithACPI_STATUS
57182367SmsmithAcpiLeaveSleepState (
572123315Snjl    UINT8                   SleepState)
57382367Smsmith{
574123315Snjl    ACPI_OBJECT_LIST        ArgList;
575123315Snjl    ACPI_OBJECT             Arg;
576123315Snjl    ACPI_STATUS             Status;
577123315Snjl    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
578123315Snjl    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
579193267Sjkim    UINT32                  Pm1aControl;
580193267Sjkim    UINT32                  Pm1bControl;
58182367Smsmith
58283174Smsmith
583167802Sjkim    ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
58482367Smsmith
58591116Smsmith
586126372Snjl    /*
587126372Snjl     * Set SLP_TYPE and SLP_EN to state S0.
588126372Snjl     * This is unclear from the ACPI Spec, but it is required
589126372Snjl     * by some machines.
590126372Snjl     */
591126372Snjl    Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
592126372Snjl                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
593123315Snjl    if (ACPI_SUCCESS (Status))
594123315Snjl    {
595193267Sjkim        SleepTypeRegInfo =
596193267Sjkim            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
597193267Sjkim        SleepEnableRegInfo =
598193267Sjkim            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
599123315Snjl
600126372Snjl        /* Get current value of PM1A control */
601123315Snjl
602193267Sjkim        Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
603193267Sjkim                    &Pm1aControl);
604126372Snjl        if (ACPI_SUCCESS (Status))
605126372Snjl        {
606193267Sjkim            /* Clear the SLP_EN and SLP_TYP fields */
607126372Snjl
608193267Sjkim            Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
609193267Sjkim                SleepEnableRegInfo->AccessBitMask);
610193267Sjkim            Pm1bControl = Pm1aControl;
611126372Snjl
612193267Sjkim            /* Insert the SLP_TYP bits */
613126372Snjl
614193267Sjkim            Pm1aControl |= (AcpiGbl_SleepTypeA <<
615193267Sjkim                SleepTypeRegInfo->BitPosition);
616193267Sjkim            Pm1bControl |= (AcpiGbl_SleepTypeB <<
617193267Sjkim                SleepTypeRegInfo->BitPosition);
618126372Snjl
619193267Sjkim            /* Write the control registers and ignore any errors */
620126372Snjl
621193267Sjkim            (void) AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
622126372Snjl        }
623123315Snjl    }
624123315Snjl
62587031Smsmith    /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
62682367Smsmith
62791116Smsmith    AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
62891116Smsmith
62991116Smsmith    /* Setup parameter object */
63091116Smsmith
63182367Smsmith    ArgList.Count = 1;
63282367Smsmith    ArgList.Pointer = &Arg;
63382367Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
63482367Smsmith
63591116Smsmith    /* Ignore any errors from these methods */
63683174Smsmith
637126372Snjl    Arg.Integer.Value = ACPI_SST_WAKING;
638126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
639123315Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
640123315Snjl    {
641167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
642123315Snjl    }
643123315Snjl
644123315Snjl    Arg.Integer.Value = SleepState;
645126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__BFS, &ArgList, NULL);
64691116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
64791116Smsmith    {
648167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _BFS"));
64991116Smsmith    }
65091116Smsmith
651126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__WAK, &ArgList, NULL);
65291116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
65391116Smsmith    {
654167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _WAK"));
65591116Smsmith    }
656128212Snjl    /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
65791116Smsmith
658128212Snjl    /*
659128212Snjl     * Restore the GPEs:
660129684Snjl     * 1) Disable/Clear all GPEs
661128212Snjl     * 2) Enable all runtime GPEs
662128212Snjl     */
663151937Sjkim    Status = AcpiHwDisableAllGpes ();
66499679Siwasaki    if (ACPI_FAILURE (Status))
66599679Siwasaki    {
66699679Siwasaki        return_ACPI_STATUS (Status);
66799679Siwasaki    }
668129684Snjl    AcpiGbl_SystemAwakeAndRunning = TRUE;
66982367Smsmith
670151937Sjkim    Status = AcpiHwEnableAllRuntimeGpes ();
671129684Snjl    if (ACPI_FAILURE (Status))
672129684Snjl    {
673129684Snjl        return_ACPI_STATUS (Status);
674129684Snjl    }
675129684Snjl
676126372Snjl    /* Enable power button */
677126372Snjl
678193267Sjkim    (void) AcpiWriteBitRegister(
679193267Sjkim            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,
680193267Sjkim            ACPI_ENABLE_EVENT);
681151937Sjkim
682193267Sjkim    (void) AcpiWriteBitRegister(
683193267Sjkim            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,
684193267Sjkim            ACPI_CLEAR_STATUS);
685126372Snjl
686123315Snjl    /* Enable BM arbitration */
687123315Snjl
688193267Sjkim    Status = AcpiWriteBitRegister (ACPI_BITREG_ARB_DISABLE, 0);
689126372Snjl    if (ACPI_FAILURE (Status))
690126372Snjl    {
691126372Snjl        return_ACPI_STATUS (Status);
692126372Snjl    }
693126372Snjl
694126372Snjl    Arg.Integer.Value = ACPI_SST_WORKING;
695126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
696126372Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
697126372Snjl    {
698167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
699126372Snjl    }
700126372Snjl
70199679Siwasaki    return_ACPI_STATUS (Status);
70282367Smsmith}
703167802Sjkim
704167802SjkimACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)
705167802Sjkim
706