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