hwsleep.c revision 193335
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;
313193335Sjkim    UINT32                  Retry;
31499679Siwasaki    ACPI_STATUS             Status;
31587031Smsmith
31687031Smsmith
317167802Sjkim    ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
31887031Smsmith
31991116Smsmith
32091116Smsmith    if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
32191116Smsmith        (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
32287031Smsmith    {
323167802Sjkim        ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=%X B=%X",
32491116Smsmith            AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
32591116Smsmith        return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
32687031Smsmith    }
32787031Smsmith
328193267Sjkim    SleepTypeRegInfo   = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
32991116Smsmith    SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
33071867Smsmith
331128245Snjl    /* Clear wake status */
332128245Snjl
333193267Sjkim    Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
334128245Snjl    if (ACPI_FAILURE (Status))
33599679Siwasaki    {
336128245Snjl        return_ACPI_STATUS (Status);
337128245Snjl    }
33878986Smsmith
339129684Snjl    /* Clear all fixed and general purpose status bits */
340129684Snjl
341167802Sjkim    Status = AcpiHwClearAcpiStatus ();
342128245Snjl    if (ACPI_FAILURE (Status))
343128245Snjl    {
344128245Snjl        return_ACPI_STATUS (Status);
345128245Snjl    }
34687031Smsmith
347128245Snjl    if (SleepState != ACPI_STATE_S5)
348128245Snjl    {
349123315Snjl        /* Disable BM arbitration */
350123315Snjl
351193267Sjkim        Status = AcpiWriteBitRegister (ACPI_BITREG_ARB_DISABLE, 1);
352123315Snjl        if (ACPI_FAILURE (Status))
353123315Snjl        {
354123315Snjl            return_ACPI_STATUS (Status);
355123315Snjl        }
35699679Siwasaki    }
35799679Siwasaki
358128212Snjl    /*
359129684Snjl     * 1) Disable/Clear all GPEs
360128212Snjl     * 2) Enable all wakeup GPEs
361128212Snjl     */
362151937Sjkim    Status = AcpiHwDisableAllGpes ();
36399679Siwasaki    if (ACPI_FAILURE (Status))
36499679Siwasaki    {
36599679Siwasaki        return_ACPI_STATUS (Status);
36699679Siwasaki    }
367129684Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
36899679Siwasaki
369151937Sjkim    Status = AcpiHwEnableAllWakeupGpes ();
370129684Snjl    if (ACPI_FAILURE (Status))
371129684Snjl    {
372129684Snjl        return_ACPI_STATUS (Status);
373129684Snjl    }
374129684Snjl
375193267Sjkim    /* Execute the _GTS method (Going To Sleep) */
376193267Sjkim
377193267Sjkim    ArgList.Count = 1;
378193267Sjkim    ArgList.Pointer = &Arg;
379193267Sjkim    Arg.Type = ACPI_TYPE_INTEGER;
380193267Sjkim    Arg.Integer.Value = SleepState;
381193267Sjkim
382193267Sjkim    Status = AcpiEvaluateObject (NULL, METHOD_NAME__GTS, &ArgList, NULL);
383193267Sjkim    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
384193267Sjkim    {
385193267Sjkim        return_ACPI_STATUS (Status);
386193267Sjkim    }
387193267Sjkim
38891116Smsmith    /* Get current value of PM1A control */
38978986Smsmith
390193267Sjkim    Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
391193267Sjkim                &Pm1aControl);
39299679Siwasaki    if (ACPI_FAILURE (Status))
39399679Siwasaki    {
39499679Siwasaki        return_ACPI_STATUS (Status);
39599679Siwasaki    }
396151937Sjkim    ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
397151937Sjkim        "Entering sleep state [S%d]\n", SleepState));
39882367Smsmith
399193267Sjkim    /* Clear the SLP_EN and SLP_TYP fields */
40082367Smsmith
401193267Sjkim    Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
402151937Sjkim                     SleepEnableRegInfo->AccessBitMask);
403193267Sjkim    Pm1bControl = Pm1aControl;
40471867Smsmith
405193267Sjkim    /* Insert the SLP_TYP bits */
40682367Smsmith
407193267Sjkim    Pm1aControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
408193267Sjkim    Pm1bControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
40971867Smsmith
410126372Snjl    /*
411126372Snjl     * We split the writes of SLP_TYP and SLP_EN to workaround
412126372Snjl     * poorly implemented hardware.
413126372Snjl     */
414126372Snjl
415193267Sjkim    /* Write #1: write the SLP_TYP data to the PM1 Control registers */
41671867Smsmith
417193267Sjkim    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
41899679Siwasaki    if (ACPI_FAILURE (Status))
41999679Siwasaki    {
42099679Siwasaki        return_ACPI_STATUS (Status);
42199679Siwasaki    }
42282367Smsmith
423193267Sjkim    /* Insert the sleep enable (SLP_EN) bit */
42499679Siwasaki
425193267Sjkim    Pm1aControl |= SleepEnableRegInfo->AccessBitMask;
426193267Sjkim    Pm1bControl |= SleepEnableRegInfo->AccessBitMask;
42782367Smsmith
428193267Sjkim    /* Flush caches, as per ACPI specification */
42971867Smsmith
43099679Siwasaki    ACPI_FLUSH_CPU_CACHE ();
43180062Smsmith
432193267Sjkim    /* Write #2: Write both SLP_TYP + SLP_EN */
43399679Siwasaki
434193267Sjkim    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
43599679Siwasaki    if (ACPI_FAILURE (Status))
43699679Siwasaki    {
43799679Siwasaki        return_ACPI_STATUS (Status);
43899679Siwasaki    }
43999679Siwasaki
44082367Smsmith    if (SleepState > ACPI_STATE_S3)
44182367Smsmith    {
44299679Siwasaki        /*
443151937Sjkim         * We wanted to sleep > S3, but it didn't happen (by virtue of the
444151937Sjkim         * fact that we are still executing!)
445123315Snjl         *
446151937Sjkim         * Wait ten seconds, then try again. This is to get S4/S5 to work on
447151937Sjkim         * all machines.
448123315Snjl         *
449193267Sjkim         * We wait so long to allow chipsets that poll this reg very slowly
450193267Sjkim         * to still read the right value. Ideally, this block would go
45199679Siwasaki         * away entirely.
45299679Siwasaki         */
45399679Siwasaki        AcpiOsStall (10000000);
45482367Smsmith
455193267Sjkim        Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_CONTROL,
45699679Siwasaki                    SleepEnableRegInfo->AccessBitMask);
45799679Siwasaki        if (ACPI_FAILURE (Status))
45899679Siwasaki        {
45999679Siwasaki            return_ACPI_STATUS (Status);
46099679Siwasaki        }
46180357Speter    }
46280062Smsmith
46391116Smsmith    /* Wait until we enter sleep state */
46471867Smsmith
465193335Sjkim    Retry = 1000;
466102550Siwasaki    do
46783174Smsmith    {
468193267Sjkim        Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
46999679Siwasaki        if (ACPI_FAILURE (Status))
47099679Siwasaki        {
47199679Siwasaki            return_ACPI_STATUS (Status);
47299679Siwasaki        }
47399679Siwasaki
474193335Sjkim        /*
475193335Sjkim         * Some BIOSs don't set WAK_STS at all.  Give up waiting after
476193335Sjkim         * 1000 retries if it still isn't set.
477193335Sjkim         */
478193335Sjkim        if (Retry-- == 0)
479193335Sjkim        {
480193335Sjkim            break;
481193335Sjkim        }
482193335Sjkim
48391116Smsmith        /* Spin until we wake */
48482367Smsmith
48599679Siwasaki    } while (!InValue);
48699679Siwasaki
48771867Smsmith    return_ACPI_STATUS (AE_OK);
48871867Smsmith}
48982367Smsmith
490167802SjkimACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
491114237Snjl
492167802Sjkim
493151937Sjkim/*******************************************************************************
49482367Smsmith *
495114237Snjl * FUNCTION:    AcpiEnterSleepStateS4bios
496114237Snjl *
497114237Snjl * PARAMETERS:  None
498114237Snjl *
499114237Snjl * RETURN:      Status
500114237Snjl *
501114237Snjl * DESCRIPTION: Perform a S4 bios request.
502114237Snjl *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
503114237Snjl *
504114237Snjl ******************************************************************************/
505114237Snjl
506114237SnjlACPI_STATUS
507114237SnjlAcpiEnterSleepStateS4bios (
508114237Snjl    void)
509114237Snjl{
510114237Snjl    UINT32                  InValue;
511114237Snjl    ACPI_STATUS             Status;
512114237Snjl
513114237Snjl
514167802Sjkim    ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
515114237Snjl
516123315Snjl
517193267Sjkim    /* Clear the wake status bit (PM1) */
518193267Sjkim
519193267Sjkim    Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
520126372Snjl    if (ACPI_FAILURE (Status))
521126372Snjl    {
522126372Snjl        return_ACPI_STATUS (Status);
523126372Snjl    }
524114237Snjl
525167802Sjkim    Status = AcpiHwClearAcpiStatus ();
526126372Snjl    if (ACPI_FAILURE (Status))
527126372Snjl    {
528126372Snjl        return_ACPI_STATUS (Status);
529126372Snjl    }
530114237Snjl
531128212Snjl    /*
532129684Snjl     * 1) Disable/Clear all GPEs
533128212Snjl     * 2) Enable all wakeup GPEs
534128212Snjl     */
535151937Sjkim    Status = AcpiHwDisableAllGpes ();
536126372Snjl    if (ACPI_FAILURE (Status))
537126372Snjl    {
538126372Snjl        return_ACPI_STATUS (Status);
539126372Snjl    }
540129684Snjl    AcpiGbl_SystemAwakeAndRunning = FALSE;
541114237Snjl
542151937Sjkim    Status = AcpiHwEnableAllWakeupGpes ();
543129684Snjl    if (ACPI_FAILURE (Status))
544129684Snjl    {
545129684Snjl        return_ACPI_STATUS (Status);
546129684Snjl    }
547129684Snjl
548126372Snjl    ACPI_FLUSH_CPU_CACHE ();
549126372Snjl
550193267Sjkim    Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
551167802Sjkim                (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
552114237Snjl
553114237Snjl    do {
554114237Snjl        AcpiOsStall(1000);
555193267Sjkim        Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
556114237Snjl        if (ACPI_FAILURE (Status))
557114237Snjl        {
558114237Snjl            return_ACPI_STATUS (Status);
559114237Snjl        }
560114237Snjl    } while (!InValue);
561114237Snjl
562114237Snjl    return_ACPI_STATUS (AE_OK);
563114237Snjl}
564114237Snjl
565167802SjkimACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
566114237Snjl
567167802Sjkim
568151937Sjkim/*******************************************************************************
569114237Snjl *
57082367Smsmith * FUNCTION:    AcpiLeaveSleepState
57182367Smsmith *
57282367Smsmith * PARAMETERS:  SleepState          - Which sleep state we just exited
57382367Smsmith *
57482367Smsmith * RETURN:      Status
57582367Smsmith *
57682367Smsmith * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
577138287Smarks *              Called with interrupts ENABLED.
57882367Smsmith *
57982367Smsmith ******************************************************************************/
58082367Smsmith
58182367SmsmithACPI_STATUS
58282367SmsmithAcpiLeaveSleepState (
583123315Snjl    UINT8                   SleepState)
58482367Smsmith{
585123315Snjl    ACPI_OBJECT_LIST        ArgList;
586123315Snjl    ACPI_OBJECT             Arg;
587123315Snjl    ACPI_STATUS             Status;
588123315Snjl    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
589123315Snjl    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
590193267Sjkim    UINT32                  Pm1aControl;
591193267Sjkim    UINT32                  Pm1bControl;
59282367Smsmith
59383174Smsmith
594167802Sjkim    ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
59582367Smsmith
59691116Smsmith
597126372Snjl    /*
598126372Snjl     * Set SLP_TYPE and SLP_EN to state S0.
599126372Snjl     * This is unclear from the ACPI Spec, but it is required
600126372Snjl     * by some machines.
601126372Snjl     */
602126372Snjl    Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
603126372Snjl                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
604123315Snjl    if (ACPI_SUCCESS (Status))
605123315Snjl    {
606193267Sjkim        SleepTypeRegInfo =
607193267Sjkim            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
608193267Sjkim        SleepEnableRegInfo =
609193267Sjkim            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
610123315Snjl
611126372Snjl        /* Get current value of PM1A control */
612123315Snjl
613193267Sjkim        Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
614193267Sjkim                    &Pm1aControl);
615126372Snjl        if (ACPI_SUCCESS (Status))
616126372Snjl        {
617193267Sjkim            /* Clear the SLP_EN and SLP_TYP fields */
618126372Snjl
619193267Sjkim            Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
620193267Sjkim                SleepEnableRegInfo->AccessBitMask);
621193267Sjkim            Pm1bControl = Pm1aControl;
622126372Snjl
623193267Sjkim            /* Insert the SLP_TYP bits */
624126372Snjl
625193267Sjkim            Pm1aControl |= (AcpiGbl_SleepTypeA <<
626193267Sjkim                SleepTypeRegInfo->BitPosition);
627193267Sjkim            Pm1bControl |= (AcpiGbl_SleepTypeB <<
628193267Sjkim                SleepTypeRegInfo->BitPosition);
629126372Snjl
630193267Sjkim            /* Write the control registers and ignore any errors */
631126372Snjl
632193267Sjkim            (void) AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
633126372Snjl        }
634123315Snjl    }
635123315Snjl
63687031Smsmith    /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
63782367Smsmith
63891116Smsmith    AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
63991116Smsmith
64091116Smsmith    /* Setup parameter object */
64191116Smsmith
64282367Smsmith    ArgList.Count = 1;
64382367Smsmith    ArgList.Pointer = &Arg;
64482367Smsmith    Arg.Type = ACPI_TYPE_INTEGER;
64582367Smsmith
64691116Smsmith    /* Ignore any errors from these methods */
64783174Smsmith
648126372Snjl    Arg.Integer.Value = ACPI_SST_WAKING;
649126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__SST, &ArgList, NULL);
650123315Snjl    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
651123315Snjl    {
652167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
653123315Snjl    }
654123315Snjl
655123315Snjl    Arg.Integer.Value = SleepState;
656126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__BFS, &ArgList, NULL);
65791116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
65891116Smsmith    {
659167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _BFS"));
66091116Smsmith    }
66191116Smsmith
662126372Snjl    Status = AcpiEvaluateObject (NULL, METHOD_NAME__WAK, &ArgList, NULL);
66391116Smsmith    if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
66491116Smsmith    {
665167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _WAK"));
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
689193267Sjkim    (void) AcpiWriteBitRegister(
690193267Sjkim            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,
691193267Sjkim            ACPI_ENABLE_EVENT);
692151937Sjkim
693193267Sjkim    (void) AcpiWriteBitRegister(
694193267Sjkim            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,
695193267Sjkim            ACPI_CLEAR_STATUS);
696126372Snjl
697123315Snjl    /* Enable BM arbitration */
698123315Snjl
699193267Sjkim    Status = AcpiWriteBitRegister (ACPI_BITREG_ARB_DISABLE, 0);
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    {
709167802Sjkim        ACPI_EXCEPTION ((AE_INFO, Status, "During Method _SST"));
710126372Snjl    }
711126372Snjl
71299679Siwasaki    return_ACPI_STATUS (Status);
71382367Smsmith}
714167802Sjkim
715167802SjkimACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)
716167802Sjkim
717