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 - 2018, 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
);
56 * 2) Enable all wakeup GPEs
58 status
= acpi_hw_disable_all_gpes();
59 if (ACPI_FAILURE(status
)) {
60 return_ACPI_STATUS(status
);
62 acpi_gbl_system_awake_and_running
= FALSE
;
64 status
= acpi_hw_enable_all_wakeup_gpes();
65 if (ACPI_FAILURE(status
)) {
66 return_ACPI_STATUS(status
);
69 /* Get current value of PM1A control */
71 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
73 if (ACPI_FAILURE(status
)) {
74 return_ACPI_STATUS(status
);
76 ACPI_DEBUG_PRINT((ACPI_DB_INIT
,
77 "Entering sleep state [S%u]\n", sleep_state
));
79 /* Clear the SLP_EN and SLP_TYP fields */
81 pm1a_control
&= ~(sleep_type_reg_info
->access_bit_mask
|
82 sleep_enable_reg_info
->access_bit_mask
);
83 pm1b_control
= pm1a_control
;
85 /* Insert the SLP_TYP bits */
88 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
90 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
93 * We split the writes of SLP_TYP and SLP_EN to workaround
94 * poorly implemented hardware.
97 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
99 status
= acpi_hw_write_pm1_control(pm1a_control
, pm1b_control
);
100 if (ACPI_FAILURE(status
)) {
101 return_ACPI_STATUS(status
);
104 /* Insert the sleep enable (SLP_EN) bit */
106 pm1a_control
|= sleep_enable_reg_info
->access_bit_mask
;
107 pm1b_control
|= sleep_enable_reg_info
->access_bit_mask
;
109 /* Flush caches, as per ACPI specification */
111 ACPI_FLUSH_CPU_CACHE();
113 status
= acpi_os_enter_sleep(sleep_state
, pm1a_control
, pm1b_control
);
114 if (status
== AE_CTRL_TERMINATE
) {
115 return_ACPI_STATUS(AE_OK
);
117 if (ACPI_FAILURE(status
)) {
118 return_ACPI_STATUS(status
);
121 /* Write #2: Write both SLP_TYP + SLP_EN */
123 status
= acpi_hw_write_pm1_control(pm1a_control
, pm1b_control
);
124 if (ACPI_FAILURE(status
)) {
125 return_ACPI_STATUS(status
);
128 if (sleep_state
> ACPI_STATE_S3
) {
130 * We wanted to sleep > S3, but it didn't happen (by virtue of the
131 * fact that we are still executing!)
133 * Wait ten seconds, then try again. This is to get S4/S5 to work on
136 * We wait so long to allow chipsets that poll this reg very slowly
137 * to still read the right value. Ideally, this block would go
140 acpi_os_stall(10 * ACPI_USEC_PER_SEC
);
142 status
= acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL
,
143 sleep_enable_reg_info
->
145 if (ACPI_FAILURE(status
)) {
146 return_ACPI_STATUS(status
);
150 /* Wait for transition back to Working State */
154 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS
, &in_value
);
155 if (ACPI_FAILURE(status
)) {
156 return_ACPI_STATUS(status
);
161 return_ACPI_STATUS(AE_OK
);
164 /*******************************************************************************
166 * FUNCTION: acpi_hw_legacy_wake_prep
168 * PARAMETERS: sleep_state - Which sleep state we just exited
172 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
174 * Called with interrupts ENABLED.
176 ******************************************************************************/
178 acpi_status
acpi_hw_legacy_wake_prep(u8 sleep_state
)
181 struct acpi_bit_register_info
*sleep_type_reg_info
;
182 struct acpi_bit_register_info
*sleep_enable_reg_info
;
186 ACPI_FUNCTION_TRACE(hw_legacy_wake_prep
);
189 * Set SLP_TYPE and SLP_EN to state S0.
190 * This is unclear from the ACPI Spec, but it is required
193 status
= acpi_get_sleep_type_data(ACPI_STATE_S0
,
194 &acpi_gbl_sleep_type_a
,
195 &acpi_gbl_sleep_type_b
);
196 if (ACPI_SUCCESS(status
)) {
197 sleep_type_reg_info
=
198 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE
);
199 sleep_enable_reg_info
=
200 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
202 /* Get current value of PM1A control */
204 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
206 if (ACPI_SUCCESS(status
)) {
208 /* Clear the SLP_EN and SLP_TYP fields */
210 pm1a_control
&= ~(sleep_type_reg_info
->access_bit_mask
|
211 sleep_enable_reg_info
->
213 pm1b_control
= pm1a_control
;
215 /* Insert the SLP_TYP bits */
217 pm1a_control
|= (acpi_gbl_sleep_type_a
<<
218 sleep_type_reg_info
->bit_position
);
219 pm1b_control
|= (acpi_gbl_sleep_type_b
<<
220 sleep_type_reg_info
->bit_position
);
222 /* Write the control registers and ignore any errors */
224 (void)acpi_hw_write_pm1_control(pm1a_control
,
229 return_ACPI_STATUS(status
);
232 /*******************************************************************************
234 * FUNCTION: acpi_hw_legacy_wake
236 * PARAMETERS: sleep_state - Which sleep state we just exited
240 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
241 * Called with interrupts ENABLED.
243 ******************************************************************************/
245 acpi_status
acpi_hw_legacy_wake(u8 sleep_state
)
249 ACPI_FUNCTION_TRACE(hw_legacy_wake
);
251 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
253 acpi_gbl_sleep_type_a
= ACPI_SLEEP_TYPE_INVALID
;
254 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST
, ACPI_SST_WAKING
);
257 * GPEs must be enabled before _WAK is called as GPEs
258 * might get fired there
261 * 1) Disable all GPEs
262 * 2) Enable all runtime GPEs
264 status
= acpi_hw_disable_all_gpes();
265 if (ACPI_FAILURE(status
)) {
266 return_ACPI_STATUS(status
);
269 status
= acpi_hw_enable_all_runtime_gpes();
270 if (ACPI_FAILURE(status
)) {
271 return_ACPI_STATUS(status
);
275 * Now we can execute _WAK, etc. Some machines require that the GPEs
276 * are enabled before the wake methods are executed.
278 acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK
, sleep_state
);
281 * Some BIOS code assumes that WAK_STS will be cleared on resume
282 * and use it to determine whether the system is rebooting or
283 * resuming. Clear WAK_STS for compatibility.
285 (void)acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS
,
287 acpi_gbl_system_awake_and_running
= TRUE
;
289 /* Enable power button */
292 acpi_write_bit_register(acpi_gbl_fixed_event_info
293 [ACPI_EVENT_POWER_BUTTON
].
294 enable_register_id
, ACPI_ENABLE_EVENT
);
297 acpi_write_bit_register(acpi_gbl_fixed_event_info
298 [ACPI_EVENT_POWER_BUTTON
].
299 status_register_id
, ACPI_CLEAR_STATUS
);
301 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST
, ACPI_SST_WORKING
);
302 return_ACPI_STATUS(status
);
305 #endif /* !ACPI_REDUCED_HARDWARE */