]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/acpi/acpica/hwsleep.c
Merge branch 'yem-kconfig-rc-fixes' of git://gitorious.org/linux-kconfig/linux-kconfi...
[mirror_ubuntu-bionic-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
59 *
60 * RETURN: Status
61 *
2feec47d 62 * DESCRIPTION: Enter a system sleep state via the legacy FADT PM registers
1da177e4
LT
63 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
64 *
65 ******************************************************************************/
3f6f49c7 66acpi_status acpi_hw_legacy_sleep(u8 sleep_state)
1da177e4 67{
4be44fcd
LB
68 struct acpi_bit_register_info *sleep_type_reg_info;
69 struct acpi_bit_register_info *sleep_enable_reg_info;
2feec47d
BM
70 u32 pm1a_control;
71 u32 pm1b_control;
4be44fcd
LB
72 u32 in_value;
73 acpi_status status;
1da177e4 74
2feec47d 75 ACPI_FUNCTION_TRACE(hw_legacy_sleep);
1da177e4 76
4be44fcd 77 sleep_type_reg_info =
82d79b86 78 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
4be44fcd
LB
79 sleep_enable_reg_info =
80 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
1da177e4
LT
81
82 /* Clear wake status */
83
768aaaf1
BM
84 status =
85 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
4be44fcd
LB
86 if (ACPI_FAILURE(status)) {
87 return_ACPI_STATUS(status);
1da177e4
LT
88 }
89
90 /* Clear all fixed and general purpose status bits */
91
d8c71b6d 92 status = acpi_hw_clear_acpi_status();
4be44fcd
LB
93 if (ACPI_FAILURE(status)) {
94 return_ACPI_STATUS(status);
1da177e4
LT
95 }
96
97 /*
23b168d4 98 * 1) Disable/Clear all GPEs
1da177e4
LT
99 * 2) Enable all wakeup GPEs
100 */
1d99967b
AS
101 status = acpi_hw_disable_all_gpes();
102 if (ACPI_FAILURE(status)) {
103 return_ACPI_STATUS(status);
104 }
1da177e4
LT
105 acpi_gbl_system_awake_and_running = FALSE;
106
4be44fcd
LB
107 status = acpi_hw_enable_all_wakeup_gpes();
108 if (ACPI_FAILURE(status)) {
109 return_ACPI_STATUS(status);
1da177e4
LT
110 }
111
112 /* Get current value of PM1A control */
113
32c9ef99
BM
114 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
115 &pm1a_control);
4be44fcd
LB
116 if (ACPI_FAILURE(status)) {
117 return_ACPI_STATUS(status);
1da177e4 118 }
4be44fcd 119 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
b27d6597 120 "Entering sleep state [S%u]\n", sleep_state));
1da177e4 121
32c9ef99 122 /* Clear the SLP_EN and SLP_TYP fields */
1da177e4 123
32c9ef99
BM
124 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
125 sleep_enable_reg_info->access_bit_mask);
126 pm1b_control = pm1a_control;
1da177e4 127
32c9ef99 128 /* Insert the SLP_TYP bits */
1da177e4 129
32c9ef99 130 pm1a_control |=
4be44fcd 131 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
32c9ef99 132 pm1b_control |=
4be44fcd 133 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
1da177e4
LT
134
135 /*
136 * We split the writes of SLP_TYP and SLP_EN to workaround
137 * poorly implemented hardware.
138 */
139
32c9ef99 140 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
1da177e4 141
32c9ef99 142 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
4be44fcd
LB
143 if (ACPI_FAILURE(status)) {
144 return_ACPI_STATUS(status);
1da177e4
LT
145 }
146
32c9ef99 147 /* Insert the sleep enable (SLP_EN) bit */
1da177e4 148
32c9ef99
BM
149 pm1a_control |= sleep_enable_reg_info->access_bit_mask;
150 pm1b_control |= sleep_enable_reg_info->access_bit_mask;
1da177e4 151
32c9ef99 152 /* Flush caches, as per ACPI specification */
1da177e4 153
4be44fcd 154 ACPI_FLUSH_CPU_CACHE();
1da177e4 155
09f98a82
TL
156 status = acpi_os_prepare_sleep(sleep_state, pm1a_control,
157 pm1b_control);
158 if (ACPI_SKIP(status))
159 return_ACPI_STATUS(AE_OK);
160 if (ACPI_FAILURE(status))
161 return_ACPI_STATUS(status);
32c9ef99 162 /* Write #2: Write both SLP_TYP + SLP_EN */
1da177e4 163
32c9ef99 164 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
4be44fcd
LB
165 if (ACPI_FAILURE(status)) {
166 return_ACPI_STATUS(status);
1da177e4
LT
167 }
168
169 if (sleep_state > ACPI_STATE_S3) {
170 /*
44f6c012
RM
171 * We wanted to sleep > S3, but it didn't happen (by virtue of the
172 * fact that we are still executing!)
1da177e4 173 *
44f6c012
RM
174 * Wait ten seconds, then try again. This is to get S4/S5 to work on
175 * all machines.
1da177e4 176 *
d4913dc6
BM
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
1da177e4
LT
179 * away entirely.
180 */
4be44fcd
LB
181 acpi_os_stall(10000000);
182
d30dc9ab 183 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
4be44fcd
LB
184 sleep_enable_reg_info->
185 access_bit_mask);
186 if (ACPI_FAILURE(status)) {
187 return_ACPI_STATUS(status);
1da177e4
LT
188 }
189 }
190
2feec47d 191 /* Wait for transition back to Working State */
1da177e4
LT
192
193 do {
50ffba1b
BM
194 status =
195 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
4be44fcd
LB
196 if (ACPI_FAILURE(status)) {
197 return_ACPI_STATUS(status);
1da177e4 198 }
2feec47d 199
1da177e4
LT
200 } while (!in_value);
201
4be44fcd 202 return_ACPI_STATUS(AE_OK);
1da177e4 203}
1da177e4 204
44f6c012 205/*******************************************************************************
1da177e4 206 *
2feec47d 207 * FUNCTION: acpi_hw_legacy_wake_prep
1da177e4 208 *
2feec47d 209 * PARAMETERS: sleep_state - Which sleep state we just exited
1da177e4
LT
210 *
211 * RETURN: Status
212 *
c95d47a8
RW
213 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
214 * sleep.
2feec47d 215 * Called with interrupts ENABLED.
1da177e4
LT
216 *
217 ******************************************************************************/
2feec47d 218
3f6f49c7 219acpi_status acpi_hw_legacy_wake_prep(u8 sleep_state)
1da177e4 220{
4be44fcd
LB
221 acpi_status status;
222 struct acpi_bit_register_info *sleep_type_reg_info;
223 struct acpi_bit_register_info *sleep_enable_reg_info;
32c9ef99
BM
224 u32 pm1a_control;
225 u32 pm1b_control;
1da177e4 226
2feec47d 227 ACPI_FUNCTION_TRACE(hw_legacy_wake_prep);
1da177e4
LT
228
229 /*
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.
233 */
4be44fcd
LB
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 =
82d79b86 239 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
4be44fcd
LB
240 sleep_enable_reg_info =
241 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
1da177e4
LT
242
243 /* Get current value of PM1A control */
244
d30dc9ab 245 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
32c9ef99 246 &pm1a_control);
4be44fcd 247 if (ACPI_SUCCESS(status)) {
52fc0b02 248
32c9ef99 249 /* Clear the SLP_EN and SLP_TYP fields */
1da177e4 250
32c9ef99
BM
251 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
252 sleep_enable_reg_info->
253 access_bit_mask);
254 pm1b_control = pm1a_control;
1da177e4 255
32c9ef99 256 /* Insert the SLP_TYP bits */
1da177e4 257
d4913dc6
BM
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);
1da177e4 262
32c9ef99 263 /* Write the control registers and ignore any errors */
1da177e4 264
32c9ef99
BM
265 (void)acpi_hw_write_pm1_control(pm1a_control,
266 pm1b_control);
1da177e4
LT
267 }
268 }
269
c95d47a8
RW
270 return_ACPI_STATUS(status);
271}
272
273/*******************************************************************************
274 *
2feec47d 275 * FUNCTION: acpi_hw_legacy_wake
c95d47a8
RW
276 *
277 * PARAMETERS: sleep_state - Which sleep state we just exited
278 *
279 * RETURN: Status
280 *
281 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
282 * Called with interrupts ENABLED.
283 *
284 ******************************************************************************/
2feec47d 285
3f6f49c7 286acpi_status acpi_hw_legacy_wake(u8 sleep_state)
c95d47a8 287{
c95d47a8
RW
288 acpi_status status;
289
2feec47d 290 ACPI_FUNCTION_TRACE(hw_legacy_wake);
c95d47a8 291
1da177e4
LT
292 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
293
294 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
4efeeecd 295 acpi_hw_execute_sleep_method(METHOD_PATHNAME__SST, ACPI_SST_WAKING);
1da177e4 296
1da177e4 297 /*
79d2dfaa
TR
298 * GPEs must be enabled before _WAK is called as GPEs
299 * might get fired there
300 *
1da177e4
LT
301 * Restore the GPEs:
302 * 1) Disable/Clear all GPEs
303 * 2) Enable all runtime GPEs
304 */
4be44fcd
LB
305 status = acpi_hw_disable_all_gpes();
306 if (ACPI_FAILURE(status)) {
307 return_ACPI_STATUS(status);
1da177e4 308 }
2feec47d 309
4be44fcd
LB
310 status = acpi_hw_enable_all_runtime_gpes();
311 if (ACPI_FAILURE(status)) {
312 return_ACPI_STATUS(status);
1da177e4
LT
313 }
314
2feec47d
BM
315 /*
316 * Now we can execute _WAK, etc. Some machines require that the GPEs
317 * are enabled before the wake methods are executed.
318 */
4efeeecd 319 acpi_hw_execute_sleep_method(METHOD_PATHNAME__WAK, sleep_state);
79d2dfaa 320
a68823ee 321 /*
2feec47d
BM
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.
a68823ee 325 */
50ffba1b 326 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, 1);
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 */