hwsleep.c revision 238381
1/******************************************************************************
2 *
3 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
4 *                   original/legacy sleep/PM registers.
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2012, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions, and the following disclaimer,
17 *    without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 *    substantially similar to the "NO WARRANTY" disclaimer below
20 *    ("Disclaimer") and any redistribution must be conditioned upon
21 *    including a substantially similar Disclaimer requirement for further
22 *    binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 *    of any contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#include <contrib/dev/acpica/include/acpi.h>
46#include <contrib/dev/acpica/include/accommon.h>
47
48#define _COMPONENT          ACPI_HARDWARE
49        ACPI_MODULE_NAME    ("hwsleep")
50
51
52#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
53/*******************************************************************************
54 *
55 * FUNCTION:    AcpiHwLegacySleep
56 *
57 * PARAMETERS:  SleepState          - Which sleep state to enter
58 *              Flags               - ACPI_EXECUTE_GTS to run optional method
59 *
60 * RETURN:      Status
61 *
62 * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
63 *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
64 *
65 ******************************************************************************/
66
67ACPI_STATUS
68AcpiHwLegacySleep (
69    UINT8                   SleepState,
70    UINT8                   Flags)
71{
72    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
73    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
74    UINT32                  Pm1aControl;
75    UINT32                  Pm1bControl;
76    UINT32                  InValue;
77    UINT32                  Retry;
78    ACPI_STATUS             Status;
79
80
81    ACPI_FUNCTION_TRACE (HwLegacySleep);
82
83
84    SleepTypeRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
85    SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
86
87    /* Clear wake status */
88
89    Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
90    if (ACPI_FAILURE (Status))
91    {
92        return_ACPI_STATUS (Status);
93    }
94
95    /* Clear all fixed and general purpose status bits */
96
97    Status = AcpiHwClearAcpiStatus ();
98    if (ACPI_FAILURE (Status))
99    {
100        return_ACPI_STATUS (Status);
101    }
102
103    /*
104     * 1) Disable/Clear all GPEs
105     * 2) Enable all wakeup GPEs
106     */
107    Status = AcpiHwDisableAllGpes ();
108    if (ACPI_FAILURE (Status))
109    {
110        return_ACPI_STATUS (Status);
111    }
112    AcpiGbl_SystemAwakeAndRunning = FALSE;
113
114    Status = AcpiHwEnableAllWakeupGpes ();
115    if (ACPI_FAILURE (Status))
116    {
117        return_ACPI_STATUS (Status);
118    }
119
120    /* Optionally execute _GTS (Going To Sleep) */
121
122    if (Flags & ACPI_EXECUTE_GTS)
123    {
124        AcpiHwExecuteSleepMethod (METHOD_PATHNAME__GTS, SleepState);
125    }
126
127    /* Get current value of PM1A control */
128
129    Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
130                &Pm1aControl);
131    if (ACPI_FAILURE (Status))
132    {
133        return_ACPI_STATUS (Status);
134    }
135    ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
136        "Entering sleep state [S%u]\n", SleepState));
137
138    /* Clear the SLP_EN and SLP_TYP fields */
139
140    Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
141                     SleepEnableRegInfo->AccessBitMask);
142    Pm1bControl = Pm1aControl;
143
144    /* Insert the SLP_TYP bits */
145
146    Pm1aControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
147    Pm1bControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
148
149    /*
150     * We split the writes of SLP_TYP and SLP_EN to workaround
151     * poorly implemented hardware.
152     */
153
154    /* Write #1: write the SLP_TYP data to the PM1 Control registers */
155
156    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
157    if (ACPI_FAILURE (Status))
158    {
159        return_ACPI_STATUS (Status);
160    }
161
162    /* Insert the sleep enable (SLP_EN) bit */
163
164    Pm1aControl |= SleepEnableRegInfo->AccessBitMask;
165    Pm1bControl |= SleepEnableRegInfo->AccessBitMask;
166
167    /* Flush caches, as per ACPI specification */
168
169    ACPI_FLUSH_CPU_CACHE ();
170
171    /* Write #2: Write both SLP_TYP + SLP_EN */
172
173    Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
174    if (ACPI_FAILURE (Status))
175    {
176        return_ACPI_STATUS (Status);
177    }
178
179    if (SleepState > ACPI_STATE_S3)
180    {
181        /*
182         * We wanted to sleep > S3, but it didn't happen (by virtue of the
183         * fact that we are still executing!)
184         *
185         * Wait ten seconds, then try again. This is to get S4/S5 to work on
186         * all machines.
187         *
188         * We wait so long to allow chipsets that poll this reg very slowly
189         * to still read the right value. Ideally, this block would go
190         * away entirely.
191         */
192        AcpiOsStall (10000000);
193
194        Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_CONTROL,
195                    SleepEnableRegInfo->AccessBitMask);
196        if (ACPI_FAILURE (Status))
197        {
198            return_ACPI_STATUS (Status);
199        }
200    }
201
202    /* Wait for transition back to Working State */
203
204    Retry = 1000;
205    do
206    {
207        Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
208        if (ACPI_FAILURE (Status))
209        {
210            return_ACPI_STATUS (Status);
211        }
212
213        if (AcpiGbl_EnableInterpreterSlack)
214        {
215            /*
216             * Some BIOSs don't set WAK_STS at all.  Give up waiting after
217             * 1000 retries if it still isn't set.
218             */
219            if (Retry-- == 0)
220            {
221                break;
222            }
223        }
224
225    } while (!InValue);
226
227    return_ACPI_STATUS (AE_OK);
228}
229
230
231/*******************************************************************************
232 *
233 * FUNCTION:    AcpiHwLegacyWakePrep
234 *
235 * PARAMETERS:  SleepState          - Which sleep state we just exited
236 *              Flags               - ACPI_EXECUTE_BFS to run optional method
237 *
238 * RETURN:      Status
239 *
240 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
241 *              sleep.
242 *              Called with interrupts ENABLED.
243 *
244 ******************************************************************************/
245
246ACPI_STATUS
247AcpiHwLegacyWakePrep (
248    UINT8                   SleepState,
249    UINT8                   Flags)
250{
251    ACPI_STATUS             Status;
252    ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
253    ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
254    UINT32                  Pm1aControl;
255    UINT32                  Pm1bControl;
256
257
258    ACPI_FUNCTION_TRACE (HwLegacyWakePrep);
259
260    /*
261     * Set SLP_TYPE and SLP_EN to state S0.
262     * This is unclear from the ACPI Spec, but it is required
263     * by some machines.
264     */
265    Status = AcpiGetSleepTypeData (ACPI_STATE_S0,
266                    &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
267    if (ACPI_SUCCESS (Status))
268    {
269        SleepTypeRegInfo =
270            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
271        SleepEnableRegInfo =
272            AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
273
274        /* Get current value of PM1A control */
275
276        Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
277                    &Pm1aControl);
278        if (ACPI_SUCCESS (Status))
279        {
280            /* Clear the SLP_EN and SLP_TYP fields */
281
282            Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
283                SleepEnableRegInfo->AccessBitMask);
284            Pm1bControl = Pm1aControl;
285
286            /* Insert the SLP_TYP bits */
287
288            Pm1aControl |= (AcpiGbl_SleepTypeA <<
289                SleepTypeRegInfo->BitPosition);
290            Pm1bControl |= (AcpiGbl_SleepTypeB <<
291                SleepTypeRegInfo->BitPosition);
292
293            /* Write the control registers and ignore any errors */
294
295            (void) AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
296        }
297    }
298
299    /* Optionally execute _BFS (Back From Sleep) */
300
301    if (Flags & ACPI_EXECUTE_BFS)
302    {
303        AcpiHwExecuteSleepMethod (METHOD_PATHNAME__BFS, SleepState);
304    }
305    return_ACPI_STATUS (Status);
306}
307
308
309/*******************************************************************************
310 *
311 * FUNCTION:    AcpiHwLegacyWake
312 *
313 * PARAMETERS:  SleepState          - Which sleep state we just exited
314 *              Flags               - Reserved, set to zero
315 *
316 * RETURN:      Status
317 *
318 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
319 *              Called with interrupts ENABLED.
320 *
321 ******************************************************************************/
322
323ACPI_STATUS
324AcpiHwLegacyWake (
325    UINT8                   SleepState,
326    UINT8                   Flags)
327{
328    ACPI_STATUS             Status;
329
330
331    ACPI_FUNCTION_TRACE (HwLegacyWake);
332
333
334    /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
335
336    AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
337    AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, ACPI_SST_WAKING);
338
339    /*
340     * GPEs must be enabled before _WAK is called as GPEs
341     * might get fired there
342     *
343     * Restore the GPEs:
344     * 1) Disable/Clear all GPEs
345     * 2) Enable all runtime GPEs
346     */
347    Status = AcpiHwDisableAllGpes ();
348    if (ACPI_FAILURE (Status))
349    {
350        return_ACPI_STATUS (Status);
351    }
352
353    Status = AcpiHwEnableAllRuntimeGpes ();
354    if (ACPI_FAILURE (Status))
355    {
356        return_ACPI_STATUS (Status);
357    }
358
359    /*
360     * Now we can execute _WAK, etc. Some machines require that the GPEs
361     * are enabled before the wake methods are executed.
362     */
363    AcpiHwExecuteSleepMethod (METHOD_PATHNAME__WAK, SleepState);
364
365    /*
366     * Some BIOS code assumes that WAK_STS will be cleared on resume
367     * and use it to determine whether the system is rebooting or
368     * resuming. Clear WAK_STS for compatibility.
369     */
370    (void) AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
371    AcpiGbl_SystemAwakeAndRunning = TRUE;
372
373    /* Enable power button */
374
375    (void) AcpiWriteBitRegister(
376            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,
377            ACPI_ENABLE_EVENT);
378
379    (void) AcpiWriteBitRegister(
380            AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,
381            ACPI_CLEAR_STATUS);
382
383    AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, ACPI_SST_WORKING);
384    return_ACPI_STATUS (Status);
385}
386
387#endif /* !ACPI_REDUCED_HARDWARE */
388