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