blob: 317ae870336b7edfffacdea56c6a029d82714dbc [file] [log] [blame]
Erik Schmauss95857632018-03-14 16:13:07 -07001// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/******************************************************************************
3 *
Bob Moore70958572012-02-14 18:47:42 +08004 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
5 * original/legacy sleep/PM registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Bob Moore800ba7c2020-01-10 11:31:49 -08007 * Copyright (C) 2000 - 2020, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Erik Schmauss95857632018-03-14 16:13:07 -07009 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050012#include "accommon.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040015ACPI_MODULE_NAME("hwsleep")
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bob Moore70958572012-02-14 18:47:42 +080017#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
Bob Moore2feec472012-02-14 15:00:53 +080018/*******************************************************************************
19 *
20 * FUNCTION: acpi_hw_legacy_sleep
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * PARAMETERS: sleep_state - Which sleep state to enter
23 *
24 * RETURN: Status
25 *
Bob Moore2feec472012-02-14 15:00:53 +080026 * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
28 *
29 ******************************************************************************/
Len Brown3f6f49c2012-07-26 20:08:54 -040030acpi_status acpi_hw_legacy_sleep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Len Brown4be44fc2005-08-05 00:44:28 -040032 struct acpi_bit_register_info *sleep_type_reg_info;
33 struct acpi_bit_register_info *sleep_enable_reg_info;
Bob Moore2feec472012-02-14 15:00:53 +080034 u32 pm1a_control;
35 u32 pm1b_control;
Len Brown4be44fc2005-08-05 00:44:28 -040036 u32 in_value;
37 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Bob Moore2feec472012-02-14 15:00:53 +080039 ACPI_FUNCTION_TRACE(hw_legacy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Len Brown4be44fc2005-08-05 00:44:28 -040041 sleep_type_reg_info =
Bob Moore82d79b82009-02-18 14:31:05 +080042 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
Len Brown4be44fc2005-08-05 00:44:28 -040043 sleep_enable_reg_info =
44 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 /* Clear wake status */
47
Bob Moore1fad8732015-12-29 13:54:36 +080048 status = acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
49 ACPI_CLEAR_STATUS);
Len Brown4be44fc2005-08-05 00:44:28 -040050 if (ACPI_FAILURE(status)) {
51 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
53
Rafael J. Wysockifa850152018-07-09 11:01:07 +020054 /* Disable all GPEs */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -040055 status = acpi_hw_disable_all_gpes();
56 if (ACPI_FAILURE(status)) {
57 return_ACPI_STATUS(status);
58 }
Rafael J. Wysockif317c7d2018-08-12 12:50:09 +020059 status = acpi_hw_clear_acpi_status();
60 if (ACPI_FAILURE(status)) {
61 return_ACPI_STATUS(status);
Rafael J. Wysockifa850152018-07-09 11:01:07 +020062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 acpi_gbl_system_awake_and_running = FALSE;
64
Rafael J. Wysockifa850152018-07-09 11:01:07 +020065 /* Enable all wakeup GPEs */
Len Brown4be44fc2005-08-05 00:44:28 -040066 status = acpi_hw_enable_all_wakeup_gpes();
67 if (ACPI_FAILURE(status)) {
68 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70
71 /* Get current value of PM1A control */
72
Bob Moore32c9ef92009-02-18 14:36:05 +080073 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
74 &pm1a_control);
Len Brown4be44fc2005-08-05 00:44:28 -040075 if (ACPI_FAILURE(status)) {
76 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Len Brown4be44fc2005-08-05 00:44:28 -040078 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
Bob Mooreb27d6592010-05-26 11:47:13 +080079 "Entering sleep state [S%u]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Bob Moore32c9ef92009-02-18 14:36:05 +080081 /* Clear the SLP_EN and SLP_TYP fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Bob Moore32c9ef92009-02-18 14:36:05 +080083 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
84 sleep_enable_reg_info->access_bit_mask);
85 pm1b_control = pm1a_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Bob Moore32c9ef92009-02-18 14:36:05 +080087 /* Insert the SLP_TYP bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Bob Moore32c9ef92009-02-18 14:36:05 +080089 pm1a_control |=
Len Brown4be44fc2005-08-05 00:44:28 -040090 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
Bob Moore32c9ef92009-02-18 14:36:05 +080091 pm1b_control |=
Len Brown4be44fc2005-08-05 00:44:28 -040092 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /*
95 * We split the writes of SLP_TYP and SLP_EN to workaround
96 * poorly implemented hardware.
97 */
98
Bob Moore32c9ef92009-02-18 14:36:05 +080099 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Bob Moore32c9ef92009-02-18 14:36:05 +0800101 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400102 if (ACPI_FAILURE(status)) {
103 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Bob Moore32c9ef92009-02-18 14:36:05 +0800106 /* Insert the sleep enable (SLP_EN) bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Bob Moore32c9ef92009-02-18 14:36:05 +0800108 pm1a_control |= sleep_enable_reg_info->access_bit_mask;
109 pm1b_control |= sleep_enable_reg_info->access_bit_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Bob Moore32c9ef92009-02-18 14:36:05 +0800111 /* Flush caches, as per ACPI specification */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Len Brown4be44fc2005-08-05 00:44:28 -0400113 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Lv Zheng0fc5e8f2016-12-28 15:28:49 +0800115 status = acpi_os_enter_sleep(sleep_state, pm1a_control, pm1b_control);
116 if (status == AE_CTRL_TERMINATE) {
Tang Liang09f98a82011-12-09 10:05:54 +0800117 return_ACPI_STATUS(AE_OK);
Lv Zheng0fc5e8f2016-12-28 15:28:49 +0800118 }
119 if (ACPI_FAILURE(status)) {
Tang Liang09f98a82011-12-09 10:05:54 +0800120 return_ACPI_STATUS(status);
Lv Zheng0fc5e8f2016-12-28 15:28:49 +0800121 }
122
Bob Moore32c9ef92009-02-18 14:36:05 +0800123 /* Write #2: Write both SLP_TYP + SLP_EN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Bob Moore32c9ef92009-02-18 14:36:05 +0800125 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400126 if (ACPI_FAILURE(status)) {
127 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 if (sleep_state > ACPI_STATE_S3) {
131 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400132 * We wanted to sleep > S3, but it didn't happen (by virtue of the
133 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 *
Robert Moore44f6c012005-04-18 22:49:35 -0400135 * Wait ten seconds, then try again. This is to get S4/S5 to work on
136 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
Bob Moored4913dc2009-03-06 10:05:18 +0800138 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 * away entirely.
141 */
Bob Moorec41679a2012-12-31 00:05:46 +0000142 acpi_os_stall(10 * ACPI_USEC_PER_SEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400144 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400145 sleep_enable_reg_info->
146 access_bit_mask);
147 if (ACPI_FAILURE(status)) {
148 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 }
151
Bob Moore2feec472012-02-14 15:00:53 +0800152 /* Wait for transition back to Working State */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 do {
Bob Moore50ffba12009-02-23 15:02:07 +0800155 status =
156 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400157 if (ACPI_FAILURE(status)) {
158 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
Bob Moore2feec472012-02-14 15:00:53 +0800160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 } while (!in_value);
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Robert Moore44f6c012005-04-18 22:49:35 -0400166/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 *
Bob Moore2feec472012-02-14 15:00:53 +0800168 * FUNCTION: acpi_hw_legacy_wake_prep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
Bob Moore2feec472012-02-14 15:00:53 +0800170 * PARAMETERS: sleep_state - Which sleep state we just exited
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * RETURN: Status
173 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100174 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
175 * sleep.
Bob Moore2feec472012-02-14 15:00:53 +0800176 * Called with interrupts ENABLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 *
178 ******************************************************************************/
Bob Moore2feec472012-02-14 15:00:53 +0800179
Len Brown3f6f49c2012-07-26 20:08:54 -0400180acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Len Brown4be44fc2005-08-05 00:44:28 -0400182 acpi_status status;
183 struct acpi_bit_register_info *sleep_type_reg_info;
184 struct acpi_bit_register_info *sleep_enable_reg_info;
Bob Moore32c9ef92009-02-18 14:36:05 +0800185 u32 pm1a_control;
186 u32 pm1b_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Bob Moore2feec472012-02-14 15:00:53 +0800188 ACPI_FUNCTION_TRACE(hw_legacy_wake_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /*
191 * Set SLP_TYPE and SLP_EN to state S0.
192 * This is unclear from the ACPI Spec, but it is required
193 * by some machines.
194 */
Len Brown4be44fc2005-08-05 00:44:28 -0400195 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 =
Bob Moore82d79b82009-02-18 14:31:05 +0800200 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
Len Brown4be44fc2005-08-05 00:44:28 -0400201 sleep_enable_reg_info =
202 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Get current value of PM1A control */
205
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400206 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Bob Moore32c9ef92009-02-18 14:36:05 +0800207 &pm1a_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400208 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400209
Bob Moore32c9ef92009-02-18 14:36:05 +0800210 /* Clear the SLP_EN and SLP_TYP fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Bob Moore32c9ef92009-02-18 14:36:05 +0800212 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
213 sleep_enable_reg_info->
214 access_bit_mask);
215 pm1b_control = pm1a_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Bob Moore32c9ef92009-02-18 14:36:05 +0800217 /* Insert the SLP_TYP bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Bob Moored4913dc2009-03-06 10:05:18 +0800219 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Bob Moore32c9ef92009-02-18 14:36:05 +0800224 /* Write the control registers and ignore any errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Bob Moore32c9ef92009-02-18 14:36:05 +0800226 (void)acpi_hw_write_pm1_control(pm1a_control,
227 pm1b_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100231 return_ACPI_STATUS(status);
232}
233
234/*******************************************************************************
235 *
Bob Moore2feec472012-02-14 15:00:53 +0800236 * FUNCTION: acpi_hw_legacy_wake
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100237 *
238 * PARAMETERS: sleep_state - Which sleep state we just exited
239 *
240 * RETURN: Status
241 *
242 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
243 * Called with interrupts ENABLED.
244 *
245 ******************************************************************************/
Bob Moore2feec472012-02-14 15:00:53 +0800246
Len Brown3f6f49c2012-07-26 20:08:54 -0400247acpi_status acpi_hw_legacy_wake(u8 sleep_state)
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100248{
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100249 acpi_status status;
250
Bob Moore2feec472012-02-14 15:00:53 +0800251 ACPI_FUNCTION_TRACE(hw_legacy_wake);
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
254
255 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
Bob Moore4efeeec2012-03-21 09:42:45 +0800256 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400259 * GPEs must be enabled before _WAK is called as GPEs
260 * might get fired there
261 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * Restore the GPEs:
Erik Schmauss18996f22018-03-14 16:12:57 -0700263 * 1) Disable all GPEs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * 2) Enable all runtime GPEs
265 */
Len Brown4be44fc2005-08-05 00:44:28 -0400266 status = acpi_hw_disable_all_gpes();
267 if (ACPI_FAILURE(status)) {
268 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Bob Moore2feec472012-02-14 15:00:53 +0800270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 status = acpi_hw_enable_all_runtime_gpes();
272 if (ACPI_FAILURE(status)) {
273 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
Bob Moore2feec472012-02-14 15:00:53 +0800276 /*
277 * Now we can execute _WAK, etc. Some machines require that the GPEs
278 * are enabled before the wake methods are executed.
279 */
Bob Moore4efeeec2012-03-21 09:42:45 +0800280 acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state);
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400281
Matthew Garretta68823e2008-08-06 19:12:04 +0100282 /*
Bob Moore2feec472012-02-14 15:00:53 +0800283 * 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.
Matthew Garretta68823e2008-08-06 19:12:04 +0100286 */
Lv Zheng739dcbb2012-12-20 01:07:26 +0000287 (void)acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
288 ACPI_CLEAR_STATUS);
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400289 acpi_gbl_system_awake_and_running = TRUE;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 /* Enable power button */
292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 (void)
Bob Moore50ffba12009-02-23 15:02:07 +0800294 acpi_write_bit_register(acpi_gbl_fixed_event_info
Bob Moore2feec472012-02-14 15:00:53 +0800295 [ACPI_EVENT_POWER_BUTTON].
296 enable_register_id, ACPI_ENABLE_EVENT);
Robert Moore44f6c012005-04-18 22:49:35 -0400297
Len Brown4be44fc2005-08-05 00:44:28 -0400298 (void)
Bob Moore50ffba12009-02-23 15:02:07 +0800299 acpi_write_bit_register(acpi_gbl_fixed_event_info
Bob Moore2feec472012-02-14 15:00:53 +0800300 [ACPI_EVENT_POWER_BUTTON].
301 status_register_id, ACPI_CLEAR_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Anchal Agarwala1dc35b2020-02-14 10:47:58 -0800303 /* Enable sleep button */
304
305 (void)
306 acpi_write_bit_register(acpi_gbl_fixed_event_info
307 [ACPI_EVENT_SLEEP_BUTTON].
308 enable_register_id, ACPI_ENABLE_EVENT);
309
310 (void)
311 acpi_write_bit_register(acpi_gbl_fixed_event_info
312 [ACPI_EVENT_SLEEP_BUTTON].
313 status_register_id, ACPI_CLEAR_STATUS);
314
Bob Moore4efeeec2012-03-21 09:42:45 +0800315 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WORKING);
Len Brown4be44fc2005-08-05 00:44:28 -0400316 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
Bob Moore83135242006-10-03 00:00:00 -0400318
Bob Moore33620c52012-02-14 18:14:27 +0800319#endif /* !ACPI_REDUCED_HARDWARE */