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