1 /******************************************************************************
3 * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
47 #define _COMPONENT ACPI_HARDWARE
48 ACPI_MODULE_NAME ("hwxfsleep")
50 /* Local prototypes */
58 * Dispatch table used to efficiently branch to the various sleep
61 #define ACPI_SLEEP_FUNCTION_ID 0
62 #define ACPI_WAKE_PREP_FUNCTION_ID 1
63 #define ACPI_WAKE_FUNCTION_ID 2
65 /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
67 static ACPI_SLEEP_FUNCTIONS AcpiSleepDispatch[] =
69 {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacySleep), AcpiHwExtendedSleep},
70 {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWakePrep), AcpiHwExtendedWakePrep},
71 {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWake), AcpiHwExtendedWake}
76 * These functions are removed for the ACPI_REDUCED_HARDWARE case:
77 * AcpiSetFirmwareWakingVector
78 * AcpiSetFirmwareWakingVector64
79 * AcpiEnterSleepStateS4bios
82 #if (!ACPI_REDUCED_HARDWARE)
83 /*******************************************************************************
85 * FUNCTION: AcpiSetFirmwareWakingVector
87 * PARAMETERS: PhysicalAddress - 32-bit physical address of ACPI real mode
92 * DESCRIPTION: Sets the 32-bit FirmwareWakingVector field of the FACS
94 ******************************************************************************/
97 AcpiSetFirmwareWakingVector (
98 UINT32 PhysicalAddress)
100 ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector);
104 * According to the ACPI specification 2.0c and later, the 64-bit
105 * waking vector should be cleared and the 32-bit waking vector should
106 * be used, unless we want the wake-up code to be called by the BIOS in
107 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
108 * to fail to resume if the 64-bit vector is used.
111 /* Set the 32-bit vector */
113 AcpiGbl_FACS->FirmwareWakingVector = PhysicalAddress;
115 /* Clear the 64-bit vector if it exists */
117 if ((AcpiGbl_FACS->Length > 32) && (AcpiGbl_FACS->Version >= 1))
119 AcpiGbl_FACS->XFirmwareWakingVector = 0;
122 return_ACPI_STATUS (AE_OK);
125 ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector)
128 #if ACPI_MACHINE_WIDTH == 64
129 /*******************************************************************************
131 * FUNCTION: AcpiSetFirmwareWakingVector64
133 * PARAMETERS: PhysicalAddress - 64-bit physical address of ACPI protected
138 * DESCRIPTION: Sets the 64-bit X_FirmwareWakingVector field of the FACS, if
139 * it exists in the table. This function is intended for use with
140 * 64-bit host operating systems.
142 ******************************************************************************/
145 AcpiSetFirmwareWakingVector64 (
146 UINT64 PhysicalAddress)
148 ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector64);
151 /* Determine if the 64-bit vector actually exists */
153 if ((AcpiGbl_FACS->Length <= 32) || (AcpiGbl_FACS->Version < 1))
155 return_ACPI_STATUS (AE_NOT_EXIST);
158 /* Clear 32-bit vector, set the 64-bit X_ vector */
160 AcpiGbl_FACS->FirmwareWakingVector = 0;
161 AcpiGbl_FACS->XFirmwareWakingVector = PhysicalAddress;
162 return_ACPI_STATUS (AE_OK);
165 ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector64)
169 /*******************************************************************************
171 * FUNCTION: AcpiEnterSleepStateS4bios
177 * DESCRIPTION: Perform a S4 bios request.
178 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
180 ******************************************************************************/
183 AcpiEnterSleepStateS4bios (
190 ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
193 /* Clear the wake status bit (PM1) */
195 Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
196 if (ACPI_FAILURE (Status))
198 return_ACPI_STATUS (Status);
201 Status = AcpiHwClearAcpiStatus ();
202 if (ACPI_FAILURE (Status))
204 return_ACPI_STATUS (Status);
208 * 1) Disable/Clear all GPEs
209 * 2) Enable all wakeup GPEs
211 Status = AcpiHwDisableAllGpes ();
212 if (ACPI_FAILURE (Status))
214 return_ACPI_STATUS (Status);
216 AcpiGbl_SystemAwakeAndRunning = FALSE;
218 Status = AcpiHwEnableAllWakeupGpes ();
219 if (ACPI_FAILURE (Status))
221 return_ACPI_STATUS (Status);
224 ACPI_FLUSH_CPU_CACHE ();
226 Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
227 (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
230 AcpiOsStall (ACPI_USEC_PER_MSEC);
231 Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
232 if (ACPI_FAILURE (Status))
234 return_ACPI_STATUS (Status);
238 return_ACPI_STATUS (AE_OK);
241 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
243 #endif /* !ACPI_REDUCED_HARDWARE */
246 /*******************************************************************************
248 * FUNCTION: AcpiHwSleepDispatch
250 * PARAMETERS: SleepState - Which sleep state to enter/exit
251 * FunctionId - Sleep, WakePrep, or Wake
253 * RETURN: Status from the invoked sleep handling function.
255 * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
258 ******************************************************************************/
261 AcpiHwSleepDispatch (
266 ACPI_SLEEP_FUNCTIONS *SleepFunctions = &AcpiSleepDispatch[FunctionId];
269 #if (!ACPI_REDUCED_HARDWARE)
271 * If the Hardware Reduced flag is set (from the FADT), we must
272 * use the extended sleep registers (FADT). Note: As per the ACPI
273 * specification, these extended registers are to be used for HW-reduced
274 * platforms only. They are not general-purpose replacements for the
275 * legacy PM register sleep support.
277 if (AcpiGbl_ReducedHardware)
279 Status = SleepFunctions->ExtendedFunction (SleepState);
285 Status = SleepFunctions->LegacyFunction (SleepState);
292 * For the case where reduced-hardware-only code is being generated,
293 * we know that only the extended sleep registers are available
295 Status = SleepFunctions->ExtendedFunction (SleepState);
298 #endif /* !ACPI_REDUCED_HARDWARE */
302 /*******************************************************************************
304 * FUNCTION: AcpiEnterSleepStatePrep
306 * PARAMETERS: SleepState - Which sleep state to enter
310 * DESCRIPTION: Prepare to enter a system sleep state.
311 * This function must execute with interrupts enabled.
312 * We break sleeping into 2 stages so that OSPM can handle
313 * various OS-specific tasks between the two steps.
315 ******************************************************************************/
318 AcpiEnterSleepStatePrep (
322 ACPI_OBJECT_LIST ArgList;
327 ACPI_FUNCTION_TRACE (AcpiEnterSleepStatePrep);
330 Status = AcpiGetSleepTypeData (SleepState,
331 &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
332 if (ACPI_FAILURE (Status))
334 return_ACPI_STATUS (Status);
337 /* Execute the _PTS method (Prepare To Sleep) */
340 ArgList.Pointer = &Arg;
341 Arg.Type = ACPI_TYPE_INTEGER;
342 Arg.Integer.Value = SleepState;
344 Status = AcpiEvaluateObject (NULL, METHOD_PATHNAME__PTS, &ArgList, NULL);
345 if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
347 return_ACPI_STATUS (Status);
350 /* Setup the argument to the _SST method (System STatus) */
356 SstValue = ACPI_SST_WORKING;
363 SstValue = ACPI_SST_SLEEPING;
368 SstValue = ACPI_SST_SLEEP_CONTEXT;
373 SstValue = ACPI_SST_INDICATOR_OFF; /* Default is off */
378 * Set the system indicators to show the desired sleep state.
379 * _SST is an optional method (return no error if not found)
381 AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, SstValue);
382 return_ACPI_STATUS (AE_OK);
385 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStatePrep)
388 /*******************************************************************************
390 * FUNCTION: AcpiEnterSleepState
392 * PARAMETERS: SleepState - Which sleep state to enter
396 * DESCRIPTION: Enter a system sleep state
397 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
399 ******************************************************************************/
402 AcpiEnterSleepState (
408 ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
411 if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
412 (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
414 ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
415 AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
416 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
419 Status = AcpiHwSleepDispatch (SleepState, ACPI_SLEEP_FUNCTION_ID);
420 return_ACPI_STATUS (Status);
423 ACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
426 /*******************************************************************************
428 * FUNCTION: AcpiLeaveSleepStatePrep
430 * PARAMETERS: SleepState - Which sleep state we are exiting
434 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
435 * sleep. Called with interrupts DISABLED.
436 * We break wake/resume into 2 stages so that OSPM can handle
437 * various OS-specific tasks between the two steps.
439 ******************************************************************************/
442 AcpiLeaveSleepStatePrep (
448 ACPI_FUNCTION_TRACE (AcpiLeaveSleepStatePrep);
451 Status = AcpiHwSleepDispatch (SleepState, ACPI_WAKE_PREP_FUNCTION_ID);
452 return_ACPI_STATUS (Status);
455 ACPI_EXPORT_SYMBOL (AcpiLeaveSleepStatePrep)
458 /*******************************************************************************
460 * FUNCTION: AcpiLeaveSleepState
462 * PARAMETERS: SleepState - Which sleep state we are exiting
466 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
467 * Called with interrupts ENABLED.
469 ******************************************************************************/
472 AcpiLeaveSleepState (
478 ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
481 Status = AcpiHwSleepDispatch (SleepState, ACPI_WAKE_FUNCTION_ID);
482 return_ACPI_STATUS (Status);
485 ACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)