1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
6 * Copyright (C) 2000 - 2018, Intel Corp.
8 *****************************************************************************/
10 #define EXPORT_ACPI_INTERFACES
12 #include <acpi/acpi.h>
15 #define _COMPONENT ACPI_HARDWARE
16 ACPI_MODULE_NAME("hwxfsleep")
18 /* Local prototypes */
19 #if (!ACPI_REDUCED_HARDWARE)
21 acpi_hw_set_firmware_waking_vector(struct acpi_table_facs *facs,
22 acpi_physical_address physical_address,
23 acpi_physical_address physical_address64);
26 static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id);
29 * Dispatch table used to efficiently branch to the various sleep
32 #define ACPI_SLEEP_FUNCTION_ID 0
33 #define ACPI_WAKE_PREP_FUNCTION_ID 1
34 #define ACPI_WAKE_FUNCTION_ID 2
36 /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
38 static struct acpi_sleep_functions acpi_sleep_dispatch[] = {
39 {ACPI_STRUCT_INIT(legacy_function,
40 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_sleep)),
41 ACPI_STRUCT_INIT(extended_function,
42 acpi_hw_extended_sleep)},
43 {ACPI_STRUCT_INIT(legacy_function,
44 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake_prep)),
45 ACPI_STRUCT_INIT(extended_function,
46 acpi_hw_extended_wake_prep)},
47 {ACPI_STRUCT_INIT(legacy_function,
48 ACPI_HW_OPTIONAL_FUNCTION(acpi_hw_legacy_wake)),
49 ACPI_STRUCT_INIT(extended_function,
50 acpi_hw_extended_wake)}
54 * These functions are removed for the ACPI_REDUCED_HARDWARE case:
55 * acpi_set_firmware_waking_vector
56 * acpi_enter_sleep_state_s4bios
59 #if (!ACPI_REDUCED_HARDWARE)
60 /*******************************************************************************
62 * FUNCTION: acpi_hw_set_firmware_waking_vector
64 * PARAMETERS: facs - Pointer to FACS table
65 * physical_address - 32-bit physical address of ACPI real mode
67 * physical_address64 - 64-bit physical address of ACPI protected
72 * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
74 ******************************************************************************/
77 acpi_hw_set_firmware_waking_vector(struct acpi_table_facs *facs,
78 acpi_physical_address physical_address,
79 acpi_physical_address physical_address64)
81 ACPI_FUNCTION_TRACE(acpi_hw_set_firmware_waking_vector);
85 * According to the ACPI specification 2.0c and later, the 64-bit
86 * waking vector should be cleared and the 32-bit waking vector should
87 * be used, unless we want the wake-up code to be called by the BIOS in
88 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
89 * to fail to resume if the 64-bit vector is used.
92 /* Set the 32-bit vector */
94 facs->firmware_waking_vector = (u32)physical_address;
96 if (facs->length > 32) {
97 if (facs->version >= 1) {
99 /* Set the 64-bit vector */
101 facs->xfirmware_waking_vector = physical_address64;
103 /* Clear the 64-bit vector if it exists */
105 facs->xfirmware_waking_vector = 0;
109 return_ACPI_STATUS(AE_OK);
112 /*******************************************************************************
114 * FUNCTION: acpi_set_firmware_waking_vector
116 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
118 * physical_address64 - 64-bit physical address of ACPI protected
123 * DESCRIPTION: Sets the firmware_waking_vector fields of the FACS
125 ******************************************************************************/
128 acpi_set_firmware_waking_vector(acpi_physical_address physical_address,
129 acpi_physical_address physical_address64)
132 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
135 (void)acpi_hw_set_firmware_waking_vector(acpi_gbl_FACS,
140 return_ACPI_STATUS(AE_OK);
143 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
145 /*******************************************************************************
147 * FUNCTION: acpi_enter_sleep_state_s4bios
153 * DESCRIPTION: Perform a S4 bios request.
154 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
156 ******************************************************************************/
157 acpi_status acpi_enter_sleep_state_s4bios(void)
162 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
164 /* Clear the wake status bit (PM1) */
167 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
168 if (ACPI_FAILURE(status)) {
169 return_ACPI_STATUS(status);
172 status = acpi_hw_clear_acpi_status();
173 if (ACPI_FAILURE(status)) {
174 return_ACPI_STATUS(status);
178 * 1) Disable all GPEs
179 * 2) Enable all wakeup GPEs
181 status = acpi_hw_disable_all_gpes();
182 if (ACPI_FAILURE(status)) {
183 return_ACPI_STATUS(status);
185 acpi_gbl_system_awake_and_running = FALSE;
187 status = acpi_hw_enable_all_wakeup_gpes();
188 if (ACPI_FAILURE(status)) {
189 return_ACPI_STATUS(status);
192 status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
193 (u32)acpi_gbl_FADT.s4_bios_request, 8);
196 acpi_os_stall(ACPI_USEC_PER_MSEC);
198 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
199 if (ACPI_FAILURE(status)) {
200 return_ACPI_STATUS(status);
205 return_ACPI_STATUS(AE_OK);
208 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
209 #endif /* !ACPI_REDUCED_HARDWARE */
210 /*******************************************************************************
212 * FUNCTION: acpi_hw_sleep_dispatch
214 * PARAMETERS: sleep_state - Which sleep state to enter/exit
215 * function_id - Sleep, wake_prep, or Wake
217 * RETURN: Status from the invoked sleep handling function.
219 * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
222 ******************************************************************************/
223 static acpi_status acpi_hw_sleep_dispatch(u8 sleep_state, u32 function_id)
226 struct acpi_sleep_functions *sleep_functions =
227 &acpi_sleep_dispatch[function_id];
229 #if (!ACPI_REDUCED_HARDWARE)
231 * If the Hardware Reduced flag is set (from the FADT), we must
232 * use the extended sleep registers (FADT). Note: As per the ACPI
233 * specification, these extended registers are to be used for HW-reduced
234 * platforms only. They are not general-purpose replacements for the
235 * legacy PM register sleep support.
237 if (acpi_gbl_reduced_hardware) {
238 status = sleep_functions->extended_function(sleep_state);
242 status = sleep_functions->legacy_function(sleep_state);
249 * For the case where reduced-hardware-only code is being generated,
250 * we know that only the extended sleep registers are available
252 status = sleep_functions->extended_function(sleep_state);
255 #endif /* !ACPI_REDUCED_HARDWARE */
258 /*******************************************************************************
260 * FUNCTION: acpi_enter_sleep_state_prep
262 * PARAMETERS: sleep_state - Which sleep state to enter
266 * DESCRIPTION: Prepare to enter a system sleep state.
267 * This function must execute with interrupts enabled.
268 * We break sleeping into 2 stages so that OSPM can handle
269 * various OS-specific tasks between the two steps.
271 ******************************************************************************/
273 acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
276 struct acpi_object_list arg_list;
277 union acpi_object arg;
280 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
282 status = acpi_get_sleep_type_data(sleep_state,
283 &acpi_gbl_sleep_type_a,
284 &acpi_gbl_sleep_type_b);
285 if (ACPI_FAILURE(status)) {
286 return_ACPI_STATUS(status);
289 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
290 &acpi_gbl_sleep_type_a_s0,
291 &acpi_gbl_sleep_type_b_s0);
292 if (ACPI_FAILURE(status)) {
293 acpi_gbl_sleep_type_a_s0 = ACPI_SLEEP_TYPE_INVALID;
296 /* Execute the _PTS method (Prepare To Sleep) */
299 arg_list.pointer = &arg;
300 arg.type = ACPI_TYPE_INTEGER;
301 arg.integer.value = sleep_state;
304 acpi_evaluate_object(NULL, METHOD_PATHNAME__PTS, &arg_list, NULL);
305 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
306 return_ACPI_STATUS(status);
309 /* Setup the argument to the _SST method (System STatus) */
311 switch (sleep_state) {
314 sst_value = ACPI_SST_WORKING;
321 sst_value = ACPI_SST_SLEEPING;
326 sst_value = ACPI_SST_SLEEP_CONTEXT;
331 sst_value = ACPI_SST_INDICATOR_OFF; /* Default is off */
336 * Set the system indicators to show the desired sleep state.
337 * _SST is an optional method (return no error if not found)
339 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, sst_value);
340 return_ACPI_STATUS(AE_OK);
343 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
345 /*******************************************************************************
347 * FUNCTION: acpi_enter_sleep_state
349 * PARAMETERS: sleep_state - Which sleep state to enter
353 * DESCRIPTION: Enter a system sleep state
354 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
356 ******************************************************************************/
357 acpi_status acpi_enter_sleep_state(u8 sleep_state)
361 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
363 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
364 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
365 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
366 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
367 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
370 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_SLEEP_FUNCTION_ID);
371 return_ACPI_STATUS(status);
374 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
376 /*******************************************************************************
378 * FUNCTION: acpi_leave_sleep_state_prep
380 * PARAMETERS: sleep_state - Which sleep state we are exiting
384 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
385 * sleep. Called with interrupts DISABLED.
386 * We break wake/resume into 2 stages so that OSPM can handle
387 * various OS-specific tasks between the two steps.
389 ******************************************************************************/
390 acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
394 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
397 acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_PREP_FUNCTION_ID);
398 return_ACPI_STATUS(status);
401 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state_prep)
403 /*******************************************************************************
405 * FUNCTION: acpi_leave_sleep_state
407 * PARAMETERS: sleep_state - Which sleep state we are exiting
411 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
412 * Called with interrupts ENABLED.
414 ******************************************************************************/
415 acpi_status acpi_leave_sleep_state(u8 sleep_state)
419 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
421 status = acpi_hw_sleep_dispatch(sleep_state, ACPI_WAKE_FUNCTION_ID);
422 return_ACPI_STATUS(status);
425 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)