hwxfsleep.c revision 1.4 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
4 1.1 christos *
5 1.1 christos *****************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.4 christos * Copyright (C) 2000 - 2015, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #define EXPORT_ACPI_INTERFACES
45 1.1 christos
46 1.1 christos #include "acpi.h"
47 1.1 christos #include "accommon.h"
48 1.1 christos
49 1.1 christos #define _COMPONENT ACPI_HARDWARE
50 1.1 christos ACPI_MODULE_NAME ("hwxfsleep")
51 1.1 christos
52 1.1 christos /* Local prototypes */
53 1.1 christos
54 1.1 christos static ACPI_STATUS
55 1.1 christos AcpiHwSleepDispatch (
56 1.1 christos UINT8 SleepState,
57 1.1 christos UINT32 FunctionId);
58 1.1 christos
59 1.1 christos /*
60 1.1 christos * Dispatch table used to efficiently branch to the various sleep
61 1.1 christos * functions.
62 1.1 christos */
63 1.1 christos #define ACPI_SLEEP_FUNCTION_ID 0
64 1.1 christos #define ACPI_WAKE_PREP_FUNCTION_ID 1
65 1.1 christos #define ACPI_WAKE_FUNCTION_ID 2
66 1.1 christos
67 1.1 christos /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
68 1.1 christos
69 1.1 christos static ACPI_SLEEP_FUNCTIONS AcpiSleepDispatch[] =
70 1.1 christos {
71 1.1 christos {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacySleep), AcpiHwExtendedSleep},
72 1.1 christos {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWakePrep), AcpiHwExtendedWakePrep},
73 1.1 christos {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWake), AcpiHwExtendedWake}
74 1.1 christos };
75 1.1 christos
76 1.1 christos
77 1.1 christos /*
78 1.1 christos * These functions are removed for the ACPI_REDUCED_HARDWARE case:
79 1.1 christos * AcpiSetFirmwareWakingVector
80 1.1 christos * AcpiSetFirmwareWakingVector64
81 1.1 christos * AcpiEnterSleepStateS4bios
82 1.1 christos */
83 1.1 christos
84 1.1 christos #if (!ACPI_REDUCED_HARDWARE)
85 1.1 christos /*******************************************************************************
86 1.1 christos *
87 1.1 christos * FUNCTION: AcpiSetFirmwareWakingVector
88 1.1 christos *
89 1.1 christos * PARAMETERS: PhysicalAddress - 32-bit physical address of ACPI real mode
90 1.1 christos * entry point.
91 1.1 christos *
92 1.1 christos * RETURN: Status
93 1.1 christos *
94 1.1 christos * DESCRIPTION: Sets the 32-bit FirmwareWakingVector field of the FACS
95 1.1 christos *
96 1.1 christos ******************************************************************************/
97 1.1 christos
98 1.1 christos ACPI_STATUS
99 1.1 christos AcpiSetFirmwareWakingVector (
100 1.1 christos UINT32 PhysicalAddress)
101 1.1 christos {
102 1.1 christos ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector);
103 1.1 christos
104 1.1 christos
105 1.1 christos /*
106 1.1 christos * According to the ACPI specification 2.0c and later, the 64-bit
107 1.1 christos * waking vector should be cleared and the 32-bit waking vector should
108 1.1 christos * be used, unless we want the wake-up code to be called by the BIOS in
109 1.1 christos * Protected Mode. Some systems (for example HP dv5-1004nr) are known
110 1.1 christos * to fail to resume if the 64-bit vector is used.
111 1.1 christos */
112 1.1 christos
113 1.1 christos /* Set the 32-bit vector */
114 1.1 christos
115 1.1 christos AcpiGbl_FACS->FirmwareWakingVector = PhysicalAddress;
116 1.1 christos
117 1.1 christos /* Clear the 64-bit vector if it exists */
118 1.1 christos
119 1.1 christos if ((AcpiGbl_FACS->Length > 32) && (AcpiGbl_FACS->Version >= 1))
120 1.1 christos {
121 1.1 christos AcpiGbl_FACS->XFirmwareWakingVector = 0;
122 1.1 christos }
123 1.1 christos
124 1.1 christos return_ACPI_STATUS (AE_OK);
125 1.1 christos }
126 1.1 christos
127 1.1 christos ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector)
128 1.1 christos
129 1.1 christos
130 1.1 christos #if ACPI_MACHINE_WIDTH == 64
131 1.1 christos /*******************************************************************************
132 1.1 christos *
133 1.1 christos * FUNCTION: AcpiSetFirmwareWakingVector64
134 1.1 christos *
135 1.1 christos * PARAMETERS: PhysicalAddress - 64-bit physical address of ACPI protected
136 1.1 christos * mode entry point.
137 1.1 christos *
138 1.1 christos * RETURN: Status
139 1.1 christos *
140 1.1 christos * DESCRIPTION: Sets the 64-bit X_FirmwareWakingVector field of the FACS, if
141 1.1 christos * it exists in the table. This function is intended for use with
142 1.1 christos * 64-bit host operating systems.
143 1.1 christos *
144 1.1 christos ******************************************************************************/
145 1.1 christos
146 1.1 christos ACPI_STATUS
147 1.1 christos AcpiSetFirmwareWakingVector64 (
148 1.1 christos UINT64 PhysicalAddress)
149 1.1 christos {
150 1.1 christos ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector64);
151 1.1 christos
152 1.1 christos
153 1.1 christos /* Determine if the 64-bit vector actually exists */
154 1.1 christos
155 1.1 christos if ((AcpiGbl_FACS->Length <= 32) || (AcpiGbl_FACS->Version < 1))
156 1.1 christos {
157 1.1 christos return_ACPI_STATUS (AE_NOT_EXIST);
158 1.1 christos }
159 1.1 christos
160 1.1 christos /* Clear 32-bit vector, set the 64-bit X_ vector */
161 1.1 christos
162 1.1 christos AcpiGbl_FACS->FirmwareWakingVector = 0;
163 1.1 christos AcpiGbl_FACS->XFirmwareWakingVector = PhysicalAddress;
164 1.1 christos return_ACPI_STATUS (AE_OK);
165 1.1 christos }
166 1.1 christos
167 1.1 christos ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector64)
168 1.1 christos #endif
169 1.1 christos
170 1.1 christos
171 1.1 christos /*******************************************************************************
172 1.1 christos *
173 1.1 christos * FUNCTION: AcpiEnterSleepStateS4bios
174 1.1 christos *
175 1.1 christos * PARAMETERS: None
176 1.1 christos *
177 1.1 christos * RETURN: Status
178 1.1 christos *
179 1.1 christos * DESCRIPTION: Perform a S4 bios request.
180 1.1 christos * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
181 1.1 christos *
182 1.1 christos ******************************************************************************/
183 1.1 christos
184 1.1 christos ACPI_STATUS
185 1.1 christos AcpiEnterSleepStateS4bios (
186 1.1 christos void)
187 1.1 christos {
188 1.1 christos UINT32 InValue;
189 1.1 christos ACPI_STATUS Status;
190 1.1 christos
191 1.1 christos
192 1.1 christos ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
193 1.1 christos
194 1.1 christos
195 1.1 christos /* Clear the wake status bit (PM1) */
196 1.1 christos
197 1.1 christos Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
198 1.1 christos if (ACPI_FAILURE (Status))
199 1.1 christos {
200 1.1 christos return_ACPI_STATUS (Status);
201 1.1 christos }
202 1.1 christos
203 1.1 christos Status = AcpiHwClearAcpiStatus ();
204 1.1 christos if (ACPI_FAILURE (Status))
205 1.1 christos {
206 1.1 christos return_ACPI_STATUS (Status);
207 1.1 christos }
208 1.1 christos
209 1.1 christos /*
210 1.1 christos * 1) Disable/Clear all GPEs
211 1.1 christos * 2) Enable all wakeup GPEs
212 1.1 christos */
213 1.1 christos Status = AcpiHwDisableAllGpes ();
214 1.1 christos if (ACPI_FAILURE (Status))
215 1.1 christos {
216 1.1 christos return_ACPI_STATUS (Status);
217 1.1 christos }
218 1.1 christos AcpiGbl_SystemAwakeAndRunning = FALSE;
219 1.1 christos
220 1.1 christos Status = AcpiHwEnableAllWakeupGpes ();
221 1.1 christos if (ACPI_FAILURE (Status))
222 1.1 christos {
223 1.1 christos return_ACPI_STATUS (Status);
224 1.1 christos }
225 1.1 christos
226 1.1 christos ACPI_FLUSH_CPU_CACHE ();
227 1.1 christos
228 1.1 christos Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
229 1.1 christos (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
230 1.1 christos
231 1.1 christos do {
232 1.1 christos AcpiOsStall (ACPI_USEC_PER_MSEC);
233 1.1 christos Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
234 1.1 christos if (ACPI_FAILURE (Status))
235 1.1 christos {
236 1.1 christos return_ACPI_STATUS (Status);
237 1.1 christos }
238 1.1 christos } while (!InValue);
239 1.1 christos
240 1.1 christos return_ACPI_STATUS (AE_OK);
241 1.1 christos }
242 1.1 christos
243 1.1 christos ACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
244 1.1 christos
245 1.1 christos #endif /* !ACPI_REDUCED_HARDWARE */
246 1.1 christos
247 1.1 christos
248 1.1 christos /*******************************************************************************
249 1.1 christos *
250 1.1 christos * FUNCTION: AcpiHwSleepDispatch
251 1.1 christos *
252 1.1 christos * PARAMETERS: SleepState - Which sleep state to enter/exit
253 1.1 christos * FunctionId - Sleep, WakePrep, or Wake
254 1.1 christos *
255 1.1 christos * RETURN: Status from the invoked sleep handling function.
256 1.1 christos *
257 1.1 christos * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
258 1.1 christos * function.
259 1.1 christos *
260 1.1 christos ******************************************************************************/
261 1.1 christos
262 1.1 christos static ACPI_STATUS
263 1.1 christos AcpiHwSleepDispatch (
264 1.1 christos UINT8 SleepState,
265 1.1 christos UINT32 FunctionId)
266 1.1 christos {
267 1.1 christos ACPI_STATUS Status;
268 1.1 christos ACPI_SLEEP_FUNCTIONS *SleepFunctions = &AcpiSleepDispatch[FunctionId];
269 1.1 christos
270 1.1 christos
271 1.1 christos #if (!ACPI_REDUCED_HARDWARE)
272 1.1 christos /*
273 1.1 christos * If the Hardware Reduced flag is set (from the FADT), we must
274 1.1 christos * use the extended sleep registers (FADT). Note: As per the ACPI
275 1.1 christos * specification, these extended registers are to be used for HW-reduced
276 1.1 christos * platforms only. They are not general-purpose replacements for the
277 1.1 christos * legacy PM register sleep support.
278 1.1 christos */
279 1.1 christos if (AcpiGbl_ReducedHardware)
280 1.1 christos {
281 1.1 christos Status = SleepFunctions->ExtendedFunction (SleepState);
282 1.1 christos }
283 1.1 christos else
284 1.1 christos {
285 1.1 christos /* Legacy sleep */
286 1.1 christos
287 1.1 christos Status = SleepFunctions->LegacyFunction (SleepState);
288 1.1 christos }
289 1.1 christos
290 1.1 christos return (Status);
291 1.1 christos
292 1.1 christos #else
293 1.1 christos /*
294 1.1 christos * For the case where reduced-hardware-only code is being generated,
295 1.1 christos * we know that only the extended sleep registers are available
296 1.1 christos */
297 1.1 christos Status = SleepFunctions->ExtendedFunction (SleepState);
298 1.1 christos return (Status);
299 1.1 christos
300 1.1 christos #endif /* !ACPI_REDUCED_HARDWARE */
301 1.1 christos }
302 1.1 christos
303 1.1 christos
304 1.1 christos /*******************************************************************************
305 1.1 christos *
306 1.1 christos * FUNCTION: AcpiEnterSleepStatePrep
307 1.1 christos *
308 1.1 christos * PARAMETERS: SleepState - Which sleep state to enter
309 1.1 christos *
310 1.1 christos * RETURN: Status
311 1.1 christos *
312 1.1 christos * DESCRIPTION: Prepare to enter a system sleep state.
313 1.1 christos * This function must execute with interrupts enabled.
314 1.1 christos * We break sleeping into 2 stages so that OSPM can handle
315 1.1 christos * various OS-specific tasks between the two steps.
316 1.1 christos *
317 1.1 christos ******************************************************************************/
318 1.1 christos
319 1.1 christos ACPI_STATUS
320 1.1 christos AcpiEnterSleepStatePrep (
321 1.1 christos UINT8 SleepState)
322 1.1 christos {
323 1.1 christos ACPI_STATUS Status;
324 1.1 christos ACPI_OBJECT_LIST ArgList;
325 1.1 christos ACPI_OBJECT Arg;
326 1.1 christos UINT32 SstValue;
327 1.1 christos
328 1.1 christos
329 1.1 christos ACPI_FUNCTION_TRACE (AcpiEnterSleepStatePrep);
330 1.1 christos
331 1.1 christos
332 1.1 christos Status = AcpiGetSleepTypeData (SleepState,
333 1.1 christos &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
334 1.1 christos if (ACPI_FAILURE (Status))
335 1.1 christos {
336 1.1 christos return_ACPI_STATUS (Status);
337 1.1 christos }
338 1.1 christos
339 1.1 christos /* Execute the _PTS method (Prepare To Sleep) */
340 1.1 christos
341 1.1 christos ArgList.Count = 1;
342 1.1 christos ArgList.Pointer = &Arg;
343 1.1 christos Arg.Type = ACPI_TYPE_INTEGER;
344 1.1 christos Arg.Integer.Value = SleepState;
345 1.1 christos
346 1.1 christos Status = AcpiEvaluateObject (NULL, METHOD_PATHNAME__PTS, &ArgList, NULL);
347 1.1 christos if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
348 1.1 christos {
349 1.1 christos return_ACPI_STATUS (Status);
350 1.1 christos }
351 1.1 christos
352 1.1 christos /* Setup the argument to the _SST method (System STatus) */
353 1.1 christos
354 1.1 christos switch (SleepState)
355 1.1 christos {
356 1.1 christos case ACPI_STATE_S0:
357 1.1 christos
358 1.1 christos SstValue = ACPI_SST_WORKING;
359 1.1 christos break;
360 1.1 christos
361 1.1 christos case ACPI_STATE_S1:
362 1.1 christos case ACPI_STATE_S2:
363 1.1 christos case ACPI_STATE_S3:
364 1.1 christos
365 1.1 christos SstValue = ACPI_SST_SLEEPING;
366 1.1 christos break;
367 1.1 christos
368 1.1 christos case ACPI_STATE_S4:
369 1.1 christos
370 1.1 christos SstValue = ACPI_SST_SLEEP_CONTEXT;
371 1.1 christos break;
372 1.1 christos
373 1.1 christos default:
374 1.1 christos
375 1.1 christos SstValue = ACPI_SST_INDICATOR_OFF; /* Default is off */
376 1.1 christos break;
377 1.1 christos }
378 1.1 christos
379 1.1 christos /*
380 1.1 christos * Set the system indicators to show the desired sleep state.
381 1.1 christos * _SST is an optional method (return no error if not found)
382 1.1 christos */
383 1.2 christos AcpiHwExecuteSleepMethod (__UNCONST(METHOD_PATHNAME__SST), SstValue);
384 1.1 christos return_ACPI_STATUS (AE_OK);
385 1.1 christos }
386 1.1 christos
387 1.1 christos ACPI_EXPORT_SYMBOL (AcpiEnterSleepStatePrep)
388 1.1 christos
389 1.1 christos
390 1.1 christos /*******************************************************************************
391 1.1 christos *
392 1.1 christos * FUNCTION: AcpiEnterSleepState
393 1.1 christos *
394 1.1 christos * PARAMETERS: SleepState - Which sleep state to enter
395 1.1 christos *
396 1.1 christos * RETURN: Status
397 1.1 christos *
398 1.1 christos * DESCRIPTION: Enter a system sleep state
399 1.1 christos * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
400 1.1 christos *
401 1.1 christos ******************************************************************************/
402 1.1 christos
403 1.1 christos ACPI_STATUS
404 1.1 christos AcpiEnterSleepState (
405 1.1 christos UINT8 SleepState)
406 1.1 christos {
407 1.1 christos ACPI_STATUS Status;
408 1.1 christos
409 1.1 christos
410 1.1 christos ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
411 1.1 christos
412 1.1 christos
413 1.1 christos if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
414 1.1 christos (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
415 1.1 christos {
416 1.1 christos ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
417 1.1 christos AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
418 1.1 christos return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
419 1.1 christos }
420 1.1 christos
421 1.1 christos Status = AcpiHwSleepDispatch (SleepState, ACPI_SLEEP_FUNCTION_ID);
422 1.1 christos return_ACPI_STATUS (Status);
423 1.1 christos }
424 1.1 christos
425 1.1 christos ACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
426 1.1 christos
427 1.1 christos
428 1.1 christos /*******************************************************************************
429 1.1 christos *
430 1.1 christos * FUNCTION: AcpiLeaveSleepStatePrep
431 1.1 christos *
432 1.1 christos * PARAMETERS: SleepState - Which sleep state we are exiting
433 1.1 christos *
434 1.1 christos * RETURN: Status
435 1.1 christos *
436 1.1 christos * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
437 1.1 christos * sleep. Called with interrupts DISABLED.
438 1.1 christos * We break wake/resume into 2 stages so that OSPM can handle
439 1.1 christos * various OS-specific tasks between the two steps.
440 1.1 christos *
441 1.1 christos ******************************************************************************/
442 1.1 christos
443 1.1 christos ACPI_STATUS
444 1.1 christos AcpiLeaveSleepStatePrep (
445 1.1 christos UINT8 SleepState)
446 1.1 christos {
447 1.1 christos ACPI_STATUS Status;
448 1.1 christos
449 1.1 christos
450 1.1 christos ACPI_FUNCTION_TRACE (AcpiLeaveSleepStatePrep);
451 1.1 christos
452 1.1 christos
453 1.1 christos Status = AcpiHwSleepDispatch (SleepState, ACPI_WAKE_PREP_FUNCTION_ID);
454 1.1 christos return_ACPI_STATUS (Status);
455 1.1 christos }
456 1.1 christos
457 1.1 christos ACPI_EXPORT_SYMBOL (AcpiLeaveSleepStatePrep)
458 1.1 christos
459 1.1 christos
460 1.1 christos /*******************************************************************************
461 1.1 christos *
462 1.1 christos * FUNCTION: AcpiLeaveSleepState
463 1.1 christos *
464 1.1 christos * PARAMETERS: SleepState - Which sleep state we are exiting
465 1.1 christos *
466 1.1 christos * RETURN: Status
467 1.1 christos *
468 1.1 christos * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
469 1.1 christos * Called with interrupts ENABLED.
470 1.1 christos *
471 1.1 christos ******************************************************************************/
472 1.1 christos
473 1.1 christos ACPI_STATUS
474 1.1 christos AcpiLeaveSleepState (
475 1.1 christos UINT8 SleepState)
476 1.1 christos {
477 1.1 christos ACPI_STATUS Status;
478 1.1 christos
479 1.1 christos
480 1.1 christos ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
481 1.1 christos
482 1.1 christos
483 1.1 christos Status = AcpiHwSleepDispatch (SleepState, ACPI_WAKE_FUNCTION_ID);
484 1.1 christos return_ACPI_STATUS (Status);
485 1.1 christos }
486 1.1 christos
487 1.1 christos ACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)
488