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