1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /****************************************************************************** 3 * 4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the 5 * original/legacy sleep/PM registers. 6 * 7 * Copyright (C) 2000 - 2018, Intel Corp. 8 * 9 *****************************************************************************/ 10 11 #include <acpi/acpi.h> 12 #include "accommon.h" 13 14 #define _COMPONENT ACPI_HARDWARE 15 ACPI_MODULE_NAME("hwsleep") 16 17 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */ 18 /******************************************************************************* 19 * 20 * FUNCTION: acpi_hw_legacy_sleep 21 * 22 * PARAMETERS: sleep_state - Which sleep state to enter 23 * 24 * RETURN: Status 25 * 26 * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers 27 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED 28 * 29 ******************************************************************************/ 30 acpi_status acpi_hw_legacy_sleep(u8 sleep_state) 31 { 32 struct acpi_bit_register_info *sleep_type_reg_info; 33 struct acpi_bit_register_info *sleep_enable_reg_info; 34 u32 pm1a_control; 35 u32 pm1b_control; 36 u32 in_value; 37 acpi_status status; 38 39 ACPI_FUNCTION_TRACE(hw_legacy_sleep); 40 41 sleep_type_reg_info = 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); 45 46 /* Clear wake status */ 47 48 status = acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, 49 ACPI_CLEAR_STATUS); 50 if (ACPI_FAILURE(status)) { 51 return_ACPI_STATUS(status); 52 } 53 54 /* Disable all GPEs */ 55 status = acpi_hw_disable_all_gpes(); 56 if (ACPI_FAILURE(status)) { 57 return_ACPI_STATUS(status); 58 } 59 /* 60 * If the target sleep state is S5, clear all GPEs and fixed events too 61 */ 62 if (sleep_state == ACPI_STATE_S5) { 63 status = acpi_hw_clear_acpi_status(); 64 if (ACPI_FAILURE(status)) { 65 return_ACPI_STATUS(status); 66 } 67 } 68 acpi_gbl_system_awake_and_running = FALSE; 69 70 /* Enable all wakeup GPEs */ 71 status = acpi_hw_enable_all_wakeup_gpes(); 72 if (ACPI_FAILURE(status)) { 73 return_ACPI_STATUS(status); 74 } 75 76 /* Get current value of PM1A control */ 77 78 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, 79 &pm1a_control); 80 if (ACPI_FAILURE(status)) { 81 return_ACPI_STATUS(status); 82 } 83 ACPI_DEBUG_PRINT((ACPI_DB_INIT, 84 "Entering sleep state [S%u]\n", sleep_state)); 85 86 /* Clear the SLP_EN and SLP_TYP fields */ 87 88 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask | 89 sleep_enable_reg_info->access_bit_mask); 90 pm1b_control = pm1a_control; 91 92 /* Insert the SLP_TYP bits */ 93 94 pm1a_control |= 95 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position); 96 pm1b_control |= 97 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position); 98 99 /* 100 * We split the writes of SLP_TYP and SLP_EN to workaround 101 * poorly implemented hardware. 102 */ 103 104 /* Write #1: write the SLP_TYP data to the PM1 Control registers */ 105 106 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control); 107 if (ACPI_FAILURE(status)) { 108 return_ACPI_STATUS(status); 109 } 110 111 /* Insert the sleep enable (SLP_EN) bit */ 112 113 pm1a_control |= sleep_enable_reg_info->access_bit_mask; 114 pm1b_control |= sleep_enable_reg_info->access_bit_mask; 115 116 /* Flush caches, as per ACPI specification */ 117 118 ACPI_FLUSH_CPU_CACHE(); 119 120 status = acpi_os_enter_sleep(sleep_state, pm1a_control, pm1b_control); 121 if (status == AE_CTRL_TERMINATE) { 122 return_ACPI_STATUS(AE_OK); 123 } 124 if (ACPI_FAILURE(status)) { 125 return_ACPI_STATUS(status); 126 } 127 128 /* Write #2: Write both SLP_TYP + SLP_EN */ 129 130 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control); 131 if (ACPI_FAILURE(status)) { 132 return_ACPI_STATUS(status); 133 } 134 135 if (sleep_state > ACPI_STATE_S3) { 136 /* 137 * We wanted to sleep > S3, but it didn't happen (by virtue of the 138 * fact that we are still executing!) 139 * 140 * Wait ten seconds, then try again. This is to get S4/S5 to work on 141 * all machines. 142 * 143 * We wait so long to allow chipsets that poll this reg very slowly 144 * to still read the right value. Ideally, this block would go 145 * away entirely. 146 */ 147 acpi_os_stall(10 * ACPI_USEC_PER_SEC); 148 149 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL, 150 sleep_enable_reg_info-> 151 access_bit_mask); 152 if (ACPI_FAILURE(status)) { 153 return_ACPI_STATUS(status); 154 } 155 } 156 157 /* Wait for transition back to Working State */ 158 159 do { 160 status = 161 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value); 162 if (ACPI_FAILURE(status)) { 163 return_ACPI_STATUS(status); 164 } 165 166 } while (!in_value); 167 168 return_ACPI_STATUS(AE_OK); 169 } 170 171 /******************************************************************************* 172 * 173 * FUNCTION: acpi_hw_legacy_wake_prep 174 * 175 * PARAMETERS: sleep_state - Which sleep state we just exited 176 * 177 * RETURN: Status 178 * 179 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a 180 * sleep. 181 * Called with interrupts ENABLED. 182 * 183 ******************************************************************************/ 184 185 acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state) 186 { 187 acpi_status status; 188 struct acpi_bit_register_info *sleep_type_reg_info; 189 struct acpi_bit_register_info *sleep_enable_reg_info; 190 u32 pm1a_control; 191 u32 pm1b_control; 192 193 ACPI_FUNCTION_TRACE(hw_legacy_wake_prep); 194 195 /* 196 * Set SLP_TYPE and SLP_EN to state S0. 197 * This is unclear from the ACPI Spec, but it is required 198 * by some machines. 199 */ 200 status = acpi_get_sleep_type_data(ACPI_STATE_S0, 201 &acpi_gbl_sleep_type_a, 202 &acpi_gbl_sleep_type_b); 203 if (ACPI_SUCCESS(status)) { 204 sleep_type_reg_info = 205 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE); 206 sleep_enable_reg_info = 207 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE); 208 209 /* Get current value of PM1A control */ 210 211 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, 212 &pm1a_control); 213 if (ACPI_SUCCESS(status)) { 214 215 /* Clear the SLP_EN and SLP_TYP fields */ 216 217 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask | 218 sleep_enable_reg_info-> 219 access_bit_mask); 220 pm1b_control = pm1a_control; 221 222 /* Insert the SLP_TYP bits */ 223 224 pm1a_control |= (acpi_gbl_sleep_type_a << 225 sleep_type_reg_info->bit_position); 226 pm1b_control |= (acpi_gbl_sleep_type_b << 227 sleep_type_reg_info->bit_position); 228 229 /* Write the control registers and ignore any errors */ 230 231 (void)acpi_hw_write_pm1_control(pm1a_control, 232 pm1b_control); 233 } 234 } 235 236 return_ACPI_STATUS(status); 237 } 238 239 /******************************************************************************* 240 * 241 * FUNCTION: acpi_hw_legacy_wake 242 * 243 * PARAMETERS: sleep_state - Which sleep state we just exited 244 * 245 * RETURN: Status 246 * 247 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep 248 * Called with interrupts ENABLED. 249 * 250 ******************************************************************************/ 251 252 acpi_status acpi_hw_legacy_wake(u8 sleep_state) 253 { 254 acpi_status status; 255 256 ACPI_FUNCTION_TRACE(hw_legacy_wake); 257 258 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */ 259 260 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID; 261 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING); 262 263 /* 264 * GPEs must be enabled before _WAK is called as GPEs 265 * might get fired there 266 * 267 * Restore the GPEs: 268 * 1) Disable all GPEs 269 * 2) Enable all runtime GPEs 270 */ 271 status = acpi_hw_disable_all_gpes(); 272 if (ACPI_FAILURE(status)) { 273 return_ACPI_STATUS(status); 274 } 275 276 status = acpi_hw_enable_all_runtime_gpes(); 277 if (ACPI_FAILURE(status)) { 278 return_ACPI_STATUS(status); 279 } 280 281 /* 282 * Now we can execute _WAK, etc. Some machines require that the GPEs 283 * are enabled before the wake methods are executed. 284 */ 285 acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state); 286 287 /* 288 * Some BIOS code assumes that WAK_STS will be cleared on resume 289 * and use it to determine whether the system is rebooting or 290 * resuming. Clear WAK_STS for compatibility. 291 */ 292 (void)acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, 293 ACPI_CLEAR_STATUS); 294 acpi_gbl_system_awake_and_running = TRUE; 295 296 /* Enable power button */ 297 298 (void) 299 acpi_write_bit_register(acpi_gbl_fixed_event_info 300 [ACPI_EVENT_POWER_BUTTON]. 301 enable_register_id, ACPI_ENABLE_EVENT); 302 303 (void) 304 acpi_write_bit_register(acpi_gbl_fixed_event_info 305 [ACPI_EVENT_POWER_BUTTON]. 306 status_register_id, ACPI_CLEAR_STATUS); 307 308 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WORKING); 309 return_ACPI_STATUS(status); 310 } 311 312 #endif /* !ACPI_REDUCED_HARDWARE */ 313