1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
5 * original/legacy sleep/PM registers.
7 * Copyright (C) 2000 - 2020, Intel Corp.
9 *****************************************************************************/
11 #include <acpi/acpi.h>
14 #define _COMPONENT ACPI_HARDWARE
15 ACPI_MODULE_NAME("hwsleep")
17 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
18 /*******************************************************************************
20 * FUNCTION: acpi_hw_legacy_sleep
22 * PARAMETERS: sleep_state - Which sleep state to enter
26 * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
27 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
29 ******************************************************************************/
30 acpi_status
acpi_hw_legacy_sleep(u8 sleep_state
)
32 struct acpi_bit_register_info
*sleep_type_reg_info
;
33 struct acpi_bit_register_info
*sleep_enable_reg_info
;
39 ACPI_FUNCTION_TRACE(hw_legacy_sleep
);
42 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE
);
43 sleep_enable_reg_info
=
44 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
46 /* Clear wake status */
48 status
= acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS
,
50 if (ACPI_FAILURE(status
)) {
51 return_ACPI_STATUS(status
);
54 /* Disable all GPEs */
55 status
= acpi_hw_disable_all_gpes();
56 if (ACPI_FAILURE(status
)) {
57 return_ACPI_STATUS(status
);
59 status
= acpi_hw_clear_acpi_status();
60 if (ACPI_FAILURE(status
)) {
61 return_ACPI_STATUS(status
);
63 acpi_gbl_system_awake_and_running
= FALSE
;
65 /* Enable all wakeup GPEs */
66 status
= acpi_hw_enable_all_wakeup_gpes();
67 if (ACPI_FAILURE(status
)) {
68 return_ACPI_STATUS(status
);
71 /* Get current value of PM1A control */
73 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
75 if (ACPI_FAILURE(status
)) {
76 return_ACPI_STATUS(status
);
78 ACPI_DEBUG_PRINT((ACPI_DB_INIT
,
79 "Entering sleep state [S%u]\n", sleep_state
));
81 /* Clear the SLP_EN and SLP_TYP fields */
83 pm1a_control
&= ~(sleep_type_reg_info
->access_bit_mask
|
84 sleep_enable_reg_info
->access_bit_mask
);
85 pm1b_control
= pm1a_control
;
87 /* Insert the SLP_TYP bits */
90 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
92 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
95 * We split the writes of SLP_TYP and SLP_EN to workaround
96 * poorly implemented hardware.
99 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
101 status
= acpi_hw_write_pm1_control(pm1a_control
, pm1b_control
);
102 if (ACPI_FAILURE(status
)) {
103 return_ACPI_STATUS(status
);
106 /* Insert the sleep enable (SLP_EN) bit */
108 pm1a_control
|= sleep_enable_reg_info
->access_bit_mask
;
109 pm1b_control
|= sleep_enable_reg_info
->access_bit_mask
;
111 /* Flush caches, as per ACPI specification */
113 ACPI_FLUSH_CPU_CACHE();
115 status
= acpi_os_enter_sleep(sleep_state
, pm1a_control
, pm1b_control
);
116 if (status
== AE_CTRL_TERMINATE
) {
117 return_ACPI_STATUS(AE_OK
);
119 if (ACPI_FAILURE(status
)) {
120 return_ACPI_STATUS(status
);
123 /* Write #2: Write both SLP_TYP + SLP_EN */
125 status
= acpi_hw_write_pm1_control(pm1a_control
, pm1b_control
);
126 if (ACPI_FAILURE(status
)) {
127 return_ACPI_STATUS(status
);
130 if (sleep_state
> ACPI_STATE_S3
) {
132 * We wanted to sleep > S3, but it didn't happen (by virtue of the
133 * fact that we are still executing!)
135 * Wait ten seconds, then try again. This is to get S4/S5 to work on
138 * We wait so long to allow chipsets that poll this reg very slowly
139 * to still read the right value. Ideally, this block would go
142 acpi_os_stall(10 * ACPI_USEC_PER_SEC
);
144 status
= acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL
,
145 sleep_enable_reg_info
->
147 if (ACPI_FAILURE(status
)) {
148 return_ACPI_STATUS(status
);
152 /* Wait for transition back to Working State */
156 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS
, &in_value
);
157 if (ACPI_FAILURE(status
)) {
158 return_ACPI_STATUS(status
);
163 return_ACPI_STATUS(AE_OK
);
166 /*******************************************************************************
168 * FUNCTION: acpi_hw_legacy_wake_prep
170 * PARAMETERS: sleep_state - Which sleep state we just exited
174 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
176 * Called with interrupts ENABLED.
178 ******************************************************************************/
180 acpi_status
acpi_hw_legacy_wake_prep(u8 sleep_state
)
183 struct acpi_bit_register_info
*sleep_type_reg_info
;
184 struct acpi_bit_register_info
*sleep_enable_reg_info
;
188 ACPI_FUNCTION_TRACE(hw_legacy_wake_prep
);
191 * Set SLP_TYPE and SLP_EN to state S0.
192 * This is unclear from the ACPI Spec, but it is required
195 status
= acpi_get_sleep_type_data(ACPI_STATE_S0
,
196 &acpi_gbl_sleep_type_a
,
197 &acpi_gbl_sleep_type_b
);
198 if (ACPI_SUCCESS(status
)) {
199 sleep_type_reg_info
=
200 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE
);
201 sleep_enable_reg_info
=
202 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
204 /* Get current value of PM1A control */
206 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
208 if (ACPI_SUCCESS(status
)) {
210 /* Clear the SLP_EN and SLP_TYP fields */
212 pm1a_control
&= ~(sleep_type_reg_info
->access_bit_mask
|
213 sleep_enable_reg_info
->
215 pm1b_control
= pm1a_control
;
217 /* Insert the SLP_TYP bits */
219 pm1a_control
|= (acpi_gbl_sleep_type_a
<<
220 sleep_type_reg_info
->bit_position
);
221 pm1b_control
|= (acpi_gbl_sleep_type_b
<<
222 sleep_type_reg_info
->bit_position
);
224 /* Write the control registers and ignore any errors */
226 (void)acpi_hw_write_pm1_control(pm1a_control
,
231 return_ACPI_STATUS(status
);
234 /*******************************************************************************
236 * FUNCTION: acpi_hw_legacy_wake
238 * PARAMETERS: sleep_state - Which sleep state we just exited
242 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
243 * Called with interrupts ENABLED.
245 ******************************************************************************/
247 acpi_status
acpi_hw_legacy_wake(u8 sleep_state
)
251 ACPI_FUNCTION_TRACE(hw_legacy_wake
);
253 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
255 acpi_gbl_sleep_type_a
= ACPI_SLEEP_TYPE_INVALID
;
256 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST
, ACPI_SST_WAKING
);
259 * GPEs must be enabled before _WAK is called as GPEs
260 * might get fired there
263 * 1) Disable all GPEs
264 * 2) Enable all runtime GPEs
266 status
= acpi_hw_disable_all_gpes();
267 if (ACPI_FAILURE(status
)) {
268 return_ACPI_STATUS(status
);
271 status
= acpi_hw_enable_all_runtime_gpes();
272 if (ACPI_FAILURE(status
)) {
273 return_ACPI_STATUS(status
);
277 * Now we can execute _WAK, etc. Some machines require that the GPEs
278 * are enabled before the wake methods are executed.
280 acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK
, sleep_state
);
283 * Some BIOS code assumes that WAK_STS will be cleared on resume
284 * and use it to determine whether the system is rebooting or
285 * resuming. Clear WAK_STS for compatibility.
287 (void)acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS
,
289 acpi_gbl_system_awake_and_running
= TRUE
;
291 /* Enable power button */
294 acpi_write_bit_register(acpi_gbl_fixed_event_info
295 [ACPI_EVENT_POWER_BUTTON
].
296 enable_register_id
, ACPI_ENABLE_EVENT
);
299 acpi_write_bit_register(acpi_gbl_fixed_event_info
300 [ACPI_EVENT_POWER_BUTTON
].
301 status_register_id
, ACPI_CLEAR_STATUS
);
303 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST
, ACPI_SST_WORKING
);
304 return_ACPI_STATUS(status
);
307 #endif /* !ACPI_REDUCED_HARDWARE */