]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/acpi/acpica/hwsleep.c
ACPI/x86: revert 'x86, acpi: Call acpi_enter_sleep_state via an asmlinkage C function...
[mirror_ubuntu-hirsute-kernel.git] / drivers / acpi / acpica / hwsleep.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
70958576
BM
3 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Support functions for the
4 * original/legacy sleep/PM registers.
1da177e4
LT
5 *
6 *****************************************************************************/
7
8/*
77848130 9 * Copyright (C) 2000 - 2012, Intel Corp.
1da177e4
LT
10 * All rights reserved.
11 *
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.
26 *
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.
30 *
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.
43 */
44
1da177e4 45#include <acpi/acpi.h>
09f98a82 46#include <linux/acpi.h>
e2f7a777 47#include "accommon.h"
cc4b859c 48#include <linux/module.h>
1da177e4
LT
49
50#define _COMPONENT ACPI_HARDWARE
4be44fcd 51ACPI_MODULE_NAME("hwsleep")
1da177e4 52
70958576 53#if (!ACPI_REDUCED_HARDWARE) /* Entire module */
44f6c012 54/*******************************************************************************
1da177e4 55 *
2feec47d 56 * FUNCTION: acpi_hw_legacy_sleep
1da177e4
LT
57 *
58 * PARAMETERS: sleep_state - Which sleep state to enter
ba494bee 59 * flags - ACPI_EXECUTE_GTS to run optional method
1da177e4
LT
60 *
61 * RETURN: Status
62 *
2feec47d 63 * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
1da177e4
LT
64 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
65 *
66 ******************************************************************************/
8a73b17e 67acpi_status acpi_hw_legacy_sleep(u8 sleep_state, u8 flags)
1da177e4 68{
4be44fcd
LB
69 struct acpi_bit_register_info *sleep_type_reg_info;
70 struct acpi_bit_register_info *sleep_enable_reg_info;
2feec47d
BM
71 u32 pm1a_control;
72 u32 pm1b_control;
4be44fcd
LB
73 u32 in_value;
74 acpi_status status;
1da177e4 75
2feec47d 76 ACPI_FUNCTION_TRACE(hw_legacy_sleep);
1da177e4 77
4be44fcd 78 sleep_type_reg_info =
82d79b86 79 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
4be44fcd
LB
80 sleep_enable_reg_info =
81 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
1da177e4
LT
82
83 /* Clear wake status */
84
768aaaf1
BM
85 status =
86 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
4be44fcd
LB
87 if (ACPI_FAILURE(status)) {
88 return_ACPI_STATUS(status);
1da177e4
LT
89 }
90
91 /* Clear all fixed and general purpose status bits */
92
d8c71b6d 93 status = acpi_hw_clear_acpi_status();
4be44fcd
LB
94 if (ACPI_FAILURE(status)) {
95 return_ACPI_STATUS(status);
1da177e4
LT
96 }
97
98 /*
23b168d4 99 * 1) Disable/Clear all GPEs
1da177e4
LT
100 * 2) Enable all wakeup GPEs
101 */
1d99967b
AS
102 status = acpi_hw_disable_all_gpes();
103 if (ACPI_FAILURE(status)) {
104 return_ACPI_STATUS(status);
105 }
1da177e4
LT
106 acpi_gbl_system_awake_and_running = FALSE;
107
4be44fcd
LB
108 status = acpi_hw_enable_all_wakeup_gpes();
109 if (ACPI_FAILURE(status)) {
110 return_ACPI_STATUS(status);
1da177e4
LT
111 }
112
8a73b17e 113 /* Optionally execute _GTS (Going To Sleep) */
c95d47a8 114
8a73b17e
LM
115 if (flags & ACPI_EXECUTE_GTS) {
116 acpi_hw_execute_sleep_method(METHOD_PATHNAME__GTS, sleep_state);
117 }
c95d47a8 118
1da177e4
LT
119 /* Get current value of PM1A control */
120
32c9ef99
BM
121 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
122 &pm1a_control);
4be44fcd
LB
123 if (ACPI_FAILURE(status)) {
124 return_ACPI_STATUS(status);
1da177e4 125 }
4be44fcd 126 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
b27d6597 127 "Entering sleep state [S%u]\n", sleep_state));
1da177e4 128
32c9ef99 129 /* Clear the SLP_EN and SLP_TYP fields */
1da177e4 130
32c9ef99
BM
131 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
132 sleep_enable_reg_info->access_bit_mask);
133 pm1b_control = pm1a_control;
1da177e4 134
32c9ef99 135 /* Insert the SLP_TYP bits */
1da177e4 136
32c9ef99 137 pm1a_control |=
4be44fcd 138 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
32c9ef99 139 pm1b_control |=
4be44fcd 140 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
1da177e4
LT
141
142 /*
143 * We split the writes of SLP_TYP and SLP_EN to workaround
144 * poorly implemented hardware.
145 */
146
32c9ef99 147 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
1da177e4 148
32c9ef99 149 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
4be44fcd
LB
150 if (ACPI_FAILURE(status)) {
151 return_ACPI_STATUS(status);
1da177e4
LT
152 }
153
32c9ef99 154 /* Insert the sleep enable (SLP_EN) bit */
1da177e4 155
32c9ef99
BM
156 pm1a_control |= sleep_enable_reg_info->access_bit_mask;
157 pm1b_control |= sleep_enable_reg_info->access_bit_mask;
1da177e4 158
32c9ef99 159 /* Flush caches, as per ACPI specification */
1da177e4 160
4be44fcd 161 ACPI_FLUSH_CPU_CACHE();
1da177e4 162
09f98a82
TL
163 status = acpi_os_prepare_sleep(sleep_state, pm1a_control,
164 pm1b_control);
165 if (ACPI_SKIP(status))
166 return_ACPI_STATUS(AE_OK);
167 if (ACPI_FAILURE(status))
168 return_ACPI_STATUS(status);
32c9ef99 169 /* Write #2: Write both SLP_TYP + SLP_EN */
1da177e4 170
32c9ef99 171 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
4be44fcd
LB
172 if (ACPI_FAILURE(status)) {
173 return_ACPI_STATUS(status);
1da177e4
LT
174 }
175
176 if (sleep_state > ACPI_STATE_S3) {
177 /*
44f6c012
RM
178 * We wanted to sleep > S3, but it didn't happen (by virtue of the
179 * fact that we are still executing!)
1da177e4 180 *
44f6c012
RM
181 * Wait ten seconds, then try again. This is to get S4/S5 to work on
182 * all machines.
1da177e4 183 *
d4913dc6
BM
184 * We wait so long to allow chipsets that poll this reg very slowly
185 * to still read the right value. Ideally, this block would go
1da177e4
LT
186 * away entirely.
187 */
4be44fcd
LB
188 acpi_os_stall(10000000);
189
d30dc9ab 190 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
4be44fcd
LB
191 sleep_enable_reg_info->
192 access_bit_mask);
193 if (ACPI_FAILURE(status)) {
194 return_ACPI_STATUS(status);
1da177e4
LT
195 }
196 }
197
2feec47d 198 /* Wait for transition back to Working State */
1da177e4
LT
199
200 do {
50ffba1b
BM
201 status =
202 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
4be44fcd
LB
203 if (ACPI_FAILURE(status)) {
204 return_ACPI_STATUS(status);
1da177e4 205 }
2feec47d 206
1da177e4
LT
207 } while (!in_value);
208
4be44fcd 209 return_ACPI_STATUS(AE_OK);
1da177e4 210}
1da177e4 211
44f6c012 212/*******************************************************************************
1da177e4 213 *
2feec47d 214 * FUNCTION: acpi_hw_legacy_wake_prep
1da177e4 215 *
2feec47d 216 * PARAMETERS: sleep_state - Which sleep state we just exited
ba494bee 217 * flags - ACPI_EXECUTE_BFS to run optional method
1da177e4
LT
218 *
219 * RETURN: Status
220 *
c95d47a8
RW
221 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
222 * sleep.
2feec47d 223 * Called with interrupts ENABLED.
1da177e4
LT
224 *
225 ******************************************************************************/
2feec47d 226
8a73b17e 227acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state, u8 flags)
1da177e4 228{
4be44fcd
LB
229 acpi_status status;
230 struct acpi_bit_register_info *sleep_type_reg_info;
231 struct acpi_bit_register_info *sleep_enable_reg_info;
32c9ef99
BM
232 u32 pm1a_control;
233 u32 pm1b_control;
1da177e4 234
2feec47d 235 ACPI_FUNCTION_TRACE(hw_legacy_wake_prep);
1da177e4
LT
236
237 /*
238 * Set SLP_TYPE and SLP_EN to state S0.
239 * This is unclear from the ACPI Spec, but it is required
240 * by some machines.
241 */
4be44fcd
LB
242 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
243 &acpi_gbl_sleep_type_a,
244 &acpi_gbl_sleep_type_b);
245 if (ACPI_SUCCESS(status)) {
246 sleep_type_reg_info =
82d79b86 247 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
4be44fcd
LB
248 sleep_enable_reg_info =
249 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
1da177e4
LT
250
251 /* Get current value of PM1A control */
252
d30dc9ab 253 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
32c9ef99 254 &pm1a_control);
4be44fcd 255 if (ACPI_SUCCESS(status)) {
52fc0b02 256
32c9ef99 257 /* Clear the SLP_EN and SLP_TYP fields */
1da177e4 258
32c9ef99
BM
259 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
260 sleep_enable_reg_info->
261 access_bit_mask);
262 pm1b_control = pm1a_control;
1da177e4 263
32c9ef99 264 /* Insert the SLP_TYP bits */
1da177e4 265
d4913dc6
BM
266 pm1a_control |= (acpi_gbl_sleep_type_a <<
267 sleep_type_reg_info->bit_position);
268 pm1b_control |= (acpi_gbl_sleep_type_b <<
269 sleep_type_reg_info->bit_position);
1da177e4 270
32c9ef99 271 /* Write the control registers and ignore any errors */
1da177e4 272
32c9ef99
BM
273 (void)acpi_hw_write_pm1_control(pm1a_control,
274 pm1b_control);
1da177e4
LT
275 }
276 }
277
8a73b17e
LM
278 /* Optionally execute _BFS (Back From Sleep) */
279
280 if (flags & ACPI_EXECUTE_BFS) {
281 acpi_hw_execute_sleep_method(METHOD_PATHNAME__BFS, sleep_state);
282 }
c95d47a8
RW
283 return_ACPI_STATUS(status);
284}
285
286/*******************************************************************************
287 *
2feec47d 288 * FUNCTION: acpi_hw_legacy_wake
c95d47a8
RW
289 *
290 * PARAMETERS: sleep_state - Which sleep state we just exited
ba494bee 291 * flags - Reserved, set to zero
c95d47a8
RW
292 *
293 * RETURN: Status
294 *
295 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
296 * Called with interrupts ENABLED.
297 *
298 ******************************************************************************/
2feec47d 299
8a73b17e 300acpi_status acpi_hw_legacy_wake(u8 sleep_state, u8 flags)
c95d47a8 301{
c95d47a8
RW
302 acpi_status status;
303
2feec47d 304 ACPI_FUNCTION_TRACE(hw_legacy_wake);
c95d47a8 305
1da177e4
LT
306 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
307
308 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
4efeeecd 309 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING);
1da177e4 310
1da177e4 311 /*
79d2dfaa
TR
312 * GPEs must be enabled before _WAK is called as GPEs
313 * might get fired there
314 *
1da177e4
LT
315 * Restore the GPEs:
316 * 1) Disable/Clear all GPEs
317 * 2) Enable all runtime GPEs
318 */
4be44fcd
LB
319 status = acpi_hw_disable_all_gpes();
320 if (ACPI_FAILURE(status)) {
321 return_ACPI_STATUS(status);
1da177e4 322 }
2feec47d 323
4be44fcd
LB
324 status = acpi_hw_enable_all_runtime_gpes();
325 if (ACPI_FAILURE(status)) {
326 return_ACPI_STATUS(status);
1da177e4
LT
327 }
328
2feec47d
BM
329 /*
330 * Now we can execute _WAK, etc. Some machines require that the GPEs
331 * are enabled before the wake methods are executed.
332 */
4efeeecd 333 acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state);
79d2dfaa 334
a68823ee 335 /*
2feec47d
BM
336 * Some BIOS code assumes that WAK_STS will be cleared on resume
337 * and use it to determine whether the system is rebooting or
338 * resuming. Clear WAK_STS for compatibility.
a68823ee 339 */
50ffba1b 340 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, 1);
79d2dfaa
TR
341 acpi_gbl_system_awake_and_running = TRUE;
342
1da177e4
LT
343 /* Enable power button */
344
4be44fcd 345 (void)
50ffba1b 346 acpi_write_bit_register(acpi_gbl_fixed_event_info
2feec47d
BM
347 [ACPI_EVENT_POWER_BUTTON].
348 enable_register_id, ACPI_ENABLE_EVENT);
44f6c012 349
4be44fcd 350 (void)
50ffba1b 351 acpi_write_bit_register(acpi_gbl_fixed_event_info
2feec47d
BM
352 [ACPI_EVENT_POWER_BUTTON].
353 status_register_id, ACPI_CLEAR_STATUS);
1da177e4 354
4efeeecd 355 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WORKING);
4be44fcd 356 return_ACPI_STATUS(status);
1da177e4 357}
8313524a 358
33620c54 359#endif /* !ACPI_REDUCED_HARDWARE */