xref: /netbsd-src/sys/external/bsd/acpica/dist/hardware/hwsleep.c (revision 2c7d7e3ca2e4f0b675c6c58e614f6aede66c678e)
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 - 2023, 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 MERCHANTABILITY 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 "acpi.h"
46 #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  *
59  * RETURN:      Status
60  *
61  * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
62  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
63  *
64  ******************************************************************************/
65 
66 ACPI_STATUS
AcpiHwLegacySleep(UINT8 SleepState)67 AcpiHwLegacySleep (
68     UINT8                   SleepState)
69 {
70     ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
71     ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
72     UINT32                  Pm1aControl;
73     UINT32                  Pm1bControl;
74     UINT32                  InValue;
75     ACPI_STATUS             Status;
76 
77 
78     ACPI_FUNCTION_TRACE (HwLegacySleep);
79 
80 
81     SleepTypeRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
82     SleepEnableRegInfo = AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
83 
84     /* Clear wake status */
85 
86     Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS,
87         ACPI_CLEAR_STATUS);
88     if (ACPI_FAILURE (Status))
89     {
90         return_ACPI_STATUS (Status);
91     }
92 
93     /* Disable all GPEs */
94 
95     Status = AcpiHwDisableAllGpes ();
96     if (ACPI_FAILURE (Status))
97     {
98         return_ACPI_STATUS (Status);
99     }
100     Status = AcpiHwClearAcpiStatus();
101     if (ACPI_FAILURE(Status))
102     {
103         return_ACPI_STATUS(Status);
104     }
105     AcpiGbl_SystemAwakeAndRunning = FALSE;
106 
107     /* Enable all wakeup GPEs */
108 
109     Status = AcpiHwEnableAllWakeupGpes ();
110     if (ACPI_FAILURE (Status))
111     {
112         return_ACPI_STATUS (Status);
113     }
114 
115     /* Get current value of PM1A control */
116 
117     Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
118         &Pm1aControl);
119     if (ACPI_FAILURE (Status))
120     {
121         return_ACPI_STATUS (Status);
122     }
123     ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
124         "Entering sleep state [S%u]\n", SleepState));
125 
126     /* Clear the SLP_EN and SLP_TYP fields */
127 
128     Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
129          SleepEnableRegInfo->AccessBitMask);
130     Pm1bControl = Pm1aControl;
131 
132     /* Insert the SLP_TYP bits */
133 
134     Pm1aControl |= (AcpiGbl_SleepTypeA << SleepTypeRegInfo->BitPosition);
135     Pm1bControl |= (AcpiGbl_SleepTypeB << SleepTypeRegInfo->BitPosition);
136 
137     /*
138      * We split the writes of SLP_TYP and SLP_EN to workaround
139      * poorly implemented hardware.
140      */
141 
142     /* Write #1: write the SLP_TYP data to the PM1 Control registers */
143 
144     Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
145     if (ACPI_FAILURE (Status))
146     {
147         return_ACPI_STATUS (Status);
148     }
149 
150     /* Insert the sleep enable (SLP_EN) bit */
151 
152     Pm1aControl |= SleepEnableRegInfo->AccessBitMask;
153     Pm1bControl |= SleepEnableRegInfo->AccessBitMask;
154 
155     /* Flush caches, as per ACPI specification */
156 
157     ACPI_FLUSH_CPU_CACHE ();
158 
159     Status = AcpiOsEnterSleep (SleepState, Pm1aControl, Pm1bControl);
160     if (Status == AE_CTRL_TERMINATE)
161     {
162         return_ACPI_STATUS (AE_OK);
163     }
164     if (ACPI_FAILURE (Status))
165     {
166         return_ACPI_STATUS (Status);
167     }
168 
169     /* Write #2: Write both SLP_TYP + SLP_EN */
170 
171     Status = AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
172     if (ACPI_FAILURE (Status))
173     {
174         return_ACPI_STATUS (Status);
175     }
176 
177     if (SleepState > ACPI_STATE_S3)
178     {
179         /*
180          * We wanted to sleep > S3, but it didn't happen (by virtue of the
181          * fact that we are still executing!)
182          *
183          * Wait ten seconds, then try again. This is to get S4/S5 to work on
184          * all machines.
185          *
186          * We wait so long to allow chipsets that poll this reg very slowly
187          * to still read the right value. Ideally, this block would go
188          * away entirely.
189          */
190         AcpiOsStall (10 * ACPI_USEC_PER_SEC);
191 
192         Status = AcpiHwRegisterWrite (ACPI_REGISTER_PM1_CONTROL,
193             SleepEnableRegInfo->AccessBitMask);
194         if (ACPI_FAILURE (Status))
195         {
196             return_ACPI_STATUS (Status);
197         }
198     }
199 
200     /* Wait for transition back to Working State */
201 
202     do
203     {
204         Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
205         if (ACPI_FAILURE (Status))
206         {
207             return_ACPI_STATUS (Status);
208         }
209 
210     } while (!InValue);
211 
212     return_ACPI_STATUS (AE_OK);
213 }
214 
215 
216 /*******************************************************************************
217  *
218  * FUNCTION:    AcpiHwLegacyWakePrep
219  *
220  * PARAMETERS:  SleepState          - Which sleep state we just exited
221  *
222  * RETURN:      Status
223  *
224  * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
225  *              sleep.
226  *              Called with interrupts ENABLED.
227  *
228  ******************************************************************************/
229 
230 ACPI_STATUS
AcpiHwLegacyWakePrep(UINT8 SleepState)231 AcpiHwLegacyWakePrep (
232     UINT8                   SleepState)
233 {
234     ACPI_STATUS             Status = AE_OK;
235     ACPI_BIT_REGISTER_INFO  *SleepTypeRegInfo;
236     ACPI_BIT_REGISTER_INFO  *SleepEnableRegInfo;
237     UINT32                  Pm1aControl;
238     UINT32                  Pm1bControl;
239 
240 
241     ACPI_FUNCTION_TRACE (HwLegacyWakePrep);
242 
243     /*
244      * Set SLP_TYPE and SLP_EN to state S0.
245      * This is unclear from the ACPI Spec, but it is required
246      * by some machines.
247      */
248     if (AcpiGbl_SleepTypeAS0 != ACPI_SLEEP_TYPE_INVALID)
249     {
250         SleepTypeRegInfo =
251             AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_TYPE);
252         SleepEnableRegInfo =
253             AcpiHwGetBitRegisterInfo (ACPI_BITREG_SLEEP_ENABLE);
254 
255         /* Get current value of PM1A control */
256 
257         Status = AcpiHwRegisterRead (ACPI_REGISTER_PM1_CONTROL,
258             &Pm1aControl);
259         if (ACPI_SUCCESS (Status))
260         {
261             /* Clear the SLP_EN and SLP_TYP fields */
262 
263             Pm1aControl &= ~(SleepTypeRegInfo->AccessBitMask |
264                 SleepEnableRegInfo->AccessBitMask);
265             Pm1bControl = Pm1aControl;
266 
267             /* Insert the SLP_TYP bits */
268 
269             Pm1aControl |= (AcpiGbl_SleepTypeAS0 <<
270                 SleepTypeRegInfo->BitPosition);
271             Pm1aControl |= (AcpiGbl_SleepTypeBS0 <<
272                 SleepTypeRegInfo->BitPosition);
273 
274             /* Write the control registers and ignore any errors */
275 
276             (void) AcpiHwWritePm1Control (Pm1aControl, Pm1bControl);
277         }
278     }
279 
280     return_ACPI_STATUS (Status);
281 }
282 
283 
284 /*******************************************************************************
285  *
286  * FUNCTION:    AcpiHwLegacyWake
287  *
288  * PARAMETERS:  SleepState          - Which sleep state we just exited
289  *
290  * RETURN:      Status
291  *
292  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
293  *              Called with interrupts ENABLED.
294  *
295  ******************************************************************************/
296 
297 ACPI_STATUS
AcpiHwLegacyWake(UINT8 SleepState)298 AcpiHwLegacyWake (
299     UINT8                   SleepState)
300 {
301     ACPI_STATUS             Status;
302 
303 
304     ACPI_FUNCTION_TRACE (HwLegacyWake);
305 
306 
307     /* Ensure EnterSleepStatePrep -> EnterSleepState ordering */
308 
309     AcpiGbl_SleepTypeA = ACPI_SLEEP_TYPE_INVALID;
310     AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__SST), ACPI_SST_WAKING);
311 
312     /*
313      * GPEs must be enabled before _WAK is called as GPEs
314      * might get fired there
315      *
316      * Restore the GPEs:
317      * 1) Disable all GPEs
318      * 2) Enable all runtime GPEs
319      */
320     Status = AcpiHwDisableAllGpes ();
321     if (ACPI_FAILURE (Status))
322     {
323         return_ACPI_STATUS (Status);
324     }
325 
326     Status = AcpiHwEnableAllRuntimeGpes ();
327     if (ACPI_FAILURE (Status))
328     {
329         return_ACPI_STATUS (Status);
330     }
331 
332     /*
333      * Now we can execute _WAK, etc. Some machines require that the GPEs
334      * are enabled before the wake methods are executed.
335      */
336     AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__WAK), SleepState);
337 
338     /*
339      * Some BIOS code assumes that WAK_STS will be cleared on resume
340      * and use it to determine whether the system is rebooting or
341      * resuming. Clear WAK_STS for compatibility.
342      */
343     (void) AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS,
344         ACPI_CLEAR_STATUS);
345     AcpiGbl_SystemAwakeAndRunning = TRUE;
346 
347     /* Enable power button */
348 
349     (void) AcpiWriteBitRegister(
350             AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].EnableRegisterId,
351             ACPI_ENABLE_EVENT);
352 
353     (void) AcpiWriteBitRegister(
354             AcpiGbl_FixedEventInfo[ACPI_EVENT_POWER_BUTTON].StatusRegisterId,
355             ACPI_CLEAR_STATUS);
356 
357     /* Enable sleep button */
358 
359     (void) AcpiWriteBitRegister (
360             AcpiGbl_FixedEventInfo[ACPI_EVENT_SLEEP_BUTTON].EnableRegisterId,
361             ACPI_ENABLE_EVENT);
362 
363     (void) AcpiWriteBitRegister (
364             AcpiGbl_FixedEventInfo[ACPI_EVENT_SLEEP_BUTTON].StatusRegisterId,
365             ACPI_CLEAR_STATUS);
366 
367     AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, ACPI_SST_WORKING);
368     return_ACPI_STATUS (Status);
369 }
370 
371 #endif /* !ACPI_REDUCED_HARDWARE */
372