Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / acpi / acpica / hwsleep.c
blobc85c373ecbc4e3012fe422d8227ae15768c2c9f5
1 /******************************************************************************
3 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
4 * original/legacy sleep/PM registers.
6 *****************************************************************************/
8 /*
9 * Copyright (C) 2000 - 2018, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include "accommon.h"
48 #define _COMPONENT ACPI_HARDWARE
49 ACPI_MODULE_NAME("hwsleep")
51 #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
52 /*******************************************************************************
54 * FUNCTION: acpi_hw_legacy_sleep
56 * PARAMETERS: sleep_state - Which sleep state to enter
58 * RETURN: Status
60 * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
61 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
63 ******************************************************************************/
64 acpi_status acpi_hw_legacy_sleep(u8 sleep_state)
66 struct acpi_bit_register_info *sleep_type_reg_info;
67 struct acpi_bit_register_info *sleep_enable_reg_info;
68 u32 pm1a_control;
69 u32 pm1b_control;
70 u32 in_value;
71 acpi_status status;
73 ACPI_FUNCTION_TRACE(hw_legacy_sleep);
75 sleep_type_reg_info =
76 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
77 sleep_enable_reg_info =
78 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
80 /* Clear wake status */
82 status = acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
83 ACPI_CLEAR_STATUS);
84 if (ACPI_FAILURE(status)) {
85 return_ACPI_STATUS(status);
88 /* Clear all fixed and general purpose status bits */
90 status = acpi_hw_clear_acpi_status();
91 if (ACPI_FAILURE(status)) {
92 return_ACPI_STATUS(status);
96 * 1) Disable/Clear all GPEs
97 * 2) Enable all wakeup GPEs
99 status = acpi_hw_disable_all_gpes();
100 if (ACPI_FAILURE(status)) {
101 return_ACPI_STATUS(status);
103 acpi_gbl_system_awake_and_running = FALSE;
105 status = acpi_hw_enable_all_wakeup_gpes();
106 if (ACPI_FAILURE(status)) {
107 return_ACPI_STATUS(status);
110 /* Get current value of PM1A control */
112 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
113 &pm1a_control);
114 if (ACPI_FAILURE(status)) {
115 return_ACPI_STATUS(status);
117 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
118 "Entering sleep state [S%u]\n", sleep_state));
120 /* Clear the SLP_EN and SLP_TYP fields */
122 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
123 sleep_enable_reg_info->access_bit_mask);
124 pm1b_control = pm1a_control;
126 /* Insert the SLP_TYP bits */
128 pm1a_control |=
129 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
130 pm1b_control |=
131 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
134 * We split the writes of SLP_TYP and SLP_EN to workaround
135 * poorly implemented hardware.
138 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
140 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
141 if (ACPI_FAILURE(status)) {
142 return_ACPI_STATUS(status);
145 /* Insert the sleep enable (SLP_EN) bit */
147 pm1a_control |= sleep_enable_reg_info->access_bit_mask;
148 pm1b_control |= sleep_enable_reg_info->access_bit_mask;
150 /* Flush caches, as per ACPI specification */
152 ACPI_FLUSH_CPU_CACHE();
154 status = acpi_os_enter_sleep(sleep_state, pm1a_control, pm1b_control);
155 if (status == AE_CTRL_TERMINATE) {
156 return_ACPI_STATUS(AE_OK);
158 if (ACPI_FAILURE(status)) {
159 return_ACPI_STATUS(status);
162 /* Write #2: Write both SLP_TYP + SLP_EN */
164 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
165 if (ACPI_FAILURE(status)) {
166 return_ACPI_STATUS(status);
169 if (sleep_state > ACPI_STATE_S3) {
171 * We wanted to sleep > S3, but it didn't happen (by virtue of the
172 * fact that we are still executing!)
174 * Wait ten seconds, then try again. This is to get S4/S5 to work on
175 * all machines.
177 * We wait so long to allow chipsets that poll this reg very slowly
178 * to still read the right value. Ideally, this block would go
179 * away entirely.
181 acpi_os_stall(10 * ACPI_USEC_PER_SEC);
183 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
184 sleep_enable_reg_info->
185 access_bit_mask);
186 if (ACPI_FAILURE(status)) {
187 return_ACPI_STATUS(status);
191 /* Wait for transition back to Working State */
193 do {
194 status =
195 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
196 if (ACPI_FAILURE(status)) {
197 return_ACPI_STATUS(status);
200 } while (!in_value);
202 return_ACPI_STATUS(AE_OK);
205 /*******************************************************************************
207 * FUNCTION: acpi_hw_legacy_wake_prep
209 * PARAMETERS: sleep_state - Which sleep state we just exited
211 * RETURN: Status
213 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
214 * sleep.
215 * Called with interrupts ENABLED.
217 ******************************************************************************/
219 acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state)
221 acpi_status status;
222 struct acpi_bit_register_info *sleep_type_reg_info;
223 struct acpi_bit_register_info *sleep_enable_reg_info;
224 u32 pm1a_control;
225 u32 pm1b_control;
227 ACPI_FUNCTION_TRACE(hw_legacy_wake_prep);
230 * Set SLP_TYPE and SLP_EN to state S0.
231 * This is unclear from the ACPI Spec, but it is required
232 * by some machines.
234 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
235 &acpi_gbl_sleep_type_a,
236 &acpi_gbl_sleep_type_b);
237 if (ACPI_SUCCESS(status)) {
238 sleep_type_reg_info =
239 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
240 sleep_enable_reg_info =
241 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
243 /* Get current value of PM1A control */
245 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
246 &pm1a_control);
247 if (ACPI_SUCCESS(status)) {
249 /* Clear the SLP_EN and SLP_TYP fields */
251 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
252 sleep_enable_reg_info->
253 access_bit_mask);
254 pm1b_control = pm1a_control;
256 /* Insert the SLP_TYP bits */
258 pm1a_control |= (acpi_gbl_sleep_type_a <<
259 sleep_type_reg_info->bit_position);
260 pm1b_control |= (acpi_gbl_sleep_type_b <<
261 sleep_type_reg_info->bit_position);
263 /* Write the control registers and ignore any errors */
265 (void)acpi_hw_write_pm1_control(pm1a_control,
266 pm1b_control);
270 return_ACPI_STATUS(status);
273 /*******************************************************************************
275 * FUNCTION: acpi_hw_legacy_wake
277 * PARAMETERS: sleep_state - Which sleep state we just exited
279 * RETURN: Status
281 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
282 * Called with interrupts ENABLED.
284 ******************************************************************************/
286 acpi_status acpi_hw_legacy_wake(u8 sleep_state)
288 acpi_status status;
290 ACPI_FUNCTION_TRACE(hw_legacy_wake);
292 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
294 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
295 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING);
298 * GPEs must be enabled before _WAK is called as GPEs
299 * might get fired there
301 * Restore the GPEs:
302 * 1) Disable/Clear all GPEs
303 * 2) Enable all runtime GPEs
305 status = acpi_hw_disable_all_gpes();
306 if (ACPI_FAILURE(status)) {
307 return_ACPI_STATUS(status);
310 status = acpi_hw_enable_all_runtime_gpes();
311 if (ACPI_FAILURE(status)) {
312 return_ACPI_STATUS(status);
316 * Now we can execute _WAK, etc. Some machines require that the GPEs
317 * are enabled before the wake methods are executed.
319 acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state);
322 * Some BIOS code assumes that WAK_STS will be cleared on resume
323 * and use it to determine whether the system is rebooting or
324 * resuming. Clear WAK_STS for compatibility.
326 (void)acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS,
327 ACPI_CLEAR_STATUS);
328 acpi_gbl_system_awake_and_running = TRUE;
330 /* Enable power button */
332 (void)
333 acpi_write_bit_register(acpi_gbl_fixed_event_info
334 [ACPI_EVENT_POWER_BUTTON].
335 enable_register_id, ACPI_ENABLE_EVENT);
337 (void)
338 acpi_write_bit_register(acpi_gbl_fixed_event_info
339 [ACPI_EVENT_POWER_BUTTON].
340 status_register_id, ACPI_CLEAR_STATUS);
342 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WORKING);
343 return_ACPI_STATUS(status);
346 #endif /* !ACPI_REDUCED_HARDWARE */