blob: 51868f48159c8c8bbc0c4ba3883a6024abb44032 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/******************************************************************************
3 *
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
5 *
6 *****************************************************************************/
7
8/*
Len Brown75a44ce2008-04-23 23:00:13 -04009 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "actables.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("hwsleep")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Robert Moore44f6c012005-04-18 22:49:35 -040052/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 *
54 * FUNCTION: acpi_set_firmware_waking_vector
55 *
Bob Moored85988f2008-11-12 14:54:05 +080056 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * entry point.
58 *
59 * RETURN: Status
60 *
Bob Moored85988f2008-11-12 14:54:05 +080061 * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070064acpi_status
Bob Moored85988f2008-11-12 14:54:05 +080065acpi_set_firmware_waking_vector(u32 physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Bob Mooreb229cf92006-04-21 17:15:00 -040067 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bob Mooref3d2e782007-02-02 19:48:18 +030069
Rafael J. Wysockia6629102008-09-06 13:13:01 +020070 /*
71 * According to the ACPI specification 2.0c and later, the 64-bit
72 * waking vector should be cleared and the 32-bit waking vector should
73 * be used, unless we want the wake-up code to be called by the BIOS in
74 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
75 * to fail to resume if the 64-bit vector is used.
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Bob Moored85988f2008-11-12 14:54:05 +080078 /* Set the 32-bit vector */
79
Bob Moore009c4cbe2008-11-12 15:34:52 +080080 acpi_gbl_FACS->firmware_waking_vector = physical_address;
Bob Moored85988f2008-11-12 14:54:05 +080081
82 /* Clear the 64-bit vector if it exists */
83
Bob Moore009c4cbe2008-11-12 15:34:52 +080084 if ((acpi_gbl_FACS->length > 32) && (acpi_gbl_FACS->version >= 1)) {
85 acpi_gbl_FACS->xfirmware_waking_vector = 0;
Bob Moored85988f2008-11-12 14:54:05 +080086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Len Brown4be44fc2005-08-05 00:44:28 -040088 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Bob Moore83135242006-10-03 00:00:00 -040091ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
92
Robert Moore44f6c012005-04-18 22:49:35 -040093/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 *
Bob Moored85988f2008-11-12 14:54:05 +080095 * FUNCTION: acpi_set_firmware_waking_vector64
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Bob Moored85988f2008-11-12 14:54:05 +080097 * PARAMETERS: physical_address - 64-bit physical address of ACPI protected
98 * mode entry point.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *
Bob Moored85988f2008-11-12 14:54:05 +0800100 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
Bob Moored85988f2008-11-12 14:54:05 +0800102 * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
103 * it exists in the table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *
105 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106acpi_status
Bob Moored85988f2008-11-12 14:54:05 +0800107acpi_set_firmware_waking_vector64(u64 physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Bob Moored85988f2008-11-12 14:54:05 +0800109 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Bob Moored85988f2008-11-12 14:54:05 +0800112 /* Determine if the 64-bit vector actually exists */
113
Bob Moore009c4cbe2008-11-12 15:34:52 +0800114 if ((acpi_gbl_FACS->length <= 32) || (acpi_gbl_FACS->version < 1)) {
Bob Moored85988f2008-11-12 14:54:05 +0800115 return_ACPI_STATUS(AE_NOT_EXIST);
116 }
117
118 /* Clear 32-bit vector, set the 64-bit X_ vector */
119
Bob Moore009c4cbe2008-11-12 15:34:52 +0800120 acpi_gbl_FACS->firmware_waking_vector = 0;
121 acpi_gbl_FACS->xfirmware_waking_vector = physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Len Brown4be44fc2005-08-05 00:44:28 -0400123 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
Bob Moore83135242006-10-03 00:00:00 -0400125
Bob Moored85988f2008-11-12 14:54:05 +0800126ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64)
127
Robert Moore44f6c012005-04-18 22:49:35 -0400128/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
130 * FUNCTION: acpi_enter_sleep_state_prep
131 *
132 * PARAMETERS: sleep_state - Which sleep state to enter
133 *
134 * RETURN: Status
135 *
136 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
137 * This function must execute with interrupts enabled.
138 * We break sleeping into 2 stages so that OSPM can handle
139 * various OS-specific tasks between the two steps.
140 *
141 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400142acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Len Brown4be44fc2005-08-05 00:44:28 -0400144 acpi_status status;
145 struct acpi_object_list arg_list;
146 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Bob Mooreb229cf92006-04-21 17:15:00 -0400148 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Bob Moore32c9ef92009-02-18 14:36:05 +0800150 /* _PSW methods could be run here to enable wake-on keyboard, LAN, etc. */
151
Len Brown4be44fc2005-08-05 00:44:28 -0400152 status = acpi_get_sleep_type_data(sleep_state,
153 &acpi_gbl_sleep_type_a,
154 &acpi_gbl_sleep_type_b);
155 if (ACPI_FAILURE(status)) {
156 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158
159 /* Setup parameter object */
160
161 arg_list.count = 1;
162 arg_list.pointer = &arg;
163
164 arg.type = ACPI_TYPE_INTEGER;
165 arg.integer.value = sleep_state;
166
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100167 /* Run the _PTS method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Len Brown4be44fc2005-08-05 00:44:28 -0400169 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
170 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
171 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* Setup the argument to _SST */
175
176 switch (sleep_state) {
177 case ACPI_STATE_S0:
178 arg.integer.value = ACPI_SST_WORKING;
179 break;
180
181 case ACPI_STATE_S1:
182 case ACPI_STATE_S2:
183 case ACPI_STATE_S3:
184 arg.integer.value = ACPI_SST_SLEEPING;
185 break;
186
187 case ACPI_STATE_S4:
188 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
189 break;
190
191 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400192 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
194 }
195
Bob Moored52c79a2008-06-10 14:26:57 +0800196 /*
197 * Set the system indicators to show the desired sleep state.
198 * _SST is an optional method (return no error if not found)
199 */
Len Brown4be44fc2005-08-05 00:44:28 -0400200 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
201 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500202 ACPI_EXCEPTION((AE_INFO, status,
203 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
Bob Moored52c79a2008-06-10 14:26:57 +0800206 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Bob Moore83135242006-10-03 00:00:00 -0400209ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
210
Robert Moore44f6c012005-04-18 22:49:35 -0400211/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
213 * FUNCTION: acpi_enter_sleep_state
214 *
215 * PARAMETERS: sleep_state - Which sleep state to enter
216 *
217 * RETURN: Status
218 *
219 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
220 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
221 *
222 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400223acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Bob Moore32c9ef92009-02-18 14:36:05 +0800225 u32 pm1a_control;
226 u32 pm1b_control;
Len Brown4be44fc2005-08-05 00:44:28 -0400227 struct acpi_bit_register_info *sleep_type_reg_info;
228 struct acpi_bit_register_info *sleep_enable_reg_info;
229 u32 in_value;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100230 struct acpi_object_list arg_list;
231 union acpi_object arg;
Len Brown4be44fc2005-08-05 00:44:28 -0400232 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Bob Mooreb229cf92006-04-21 17:15:00 -0400234 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400237 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500238 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
239 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400240 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 sleep_type_reg_info =
Bob Moore82d79b82009-02-18 14:31:05 +0800244 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
Len Brown4be44fc2005-08-05 00:44:28 -0400245 sleep_enable_reg_info =
246 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Clear wake status */
249
Bob Moored8c71b62007-02-02 19:48:21 +0300250 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400251 if (ACPI_FAILURE(status)) {
252 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 /* Clear all fixed and general purpose status bits */
256
Bob Moored8c71b62007-02-02 19:48:21 +0300257 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400258 if (ACPI_FAILURE(status)) {
259 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 /*
Pavel Machek23b168d2008-02-05 19:27:12 +0100263 * 1) Disable/Clear all GPEs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * 2) Enable all wakeup GPEs
265 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400266 status = acpi_hw_disable_all_gpes();
267 if (ACPI_FAILURE(status)) {
268 return_ACPI_STATUS(status);
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 acpi_gbl_system_awake_and_running = FALSE;
271
Len Brown4be44fc2005-08-05 00:44:28 -0400272 status = acpi_hw_enable_all_wakeup_gpes();
273 if (ACPI_FAILURE(status)) {
274 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100277 /* Execute the _GTS method */
278
279 arg_list.count = 1;
280 arg_list.pointer = &arg;
281 arg.type = ACPI_TYPE_INTEGER;
282 arg.integer.value = sleep_state;
283
284 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
285 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
286 return_ACPI_STATUS(status);
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Get current value of PM1A control */
290
Bob Moore32c9ef92009-02-18 14:36:05 +0800291 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
292 &pm1a_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400293 if (ACPI_FAILURE(status)) {
294 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Len Brown4be44fc2005-08-05 00:44:28 -0400296 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
297 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Bob Moore32c9ef92009-02-18 14:36:05 +0800299 /* Clear the SLP_EN and SLP_TYP fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Bob Moore32c9ef92009-02-18 14:36:05 +0800301 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
302 sleep_enable_reg_info->access_bit_mask);
303 pm1b_control = pm1a_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Bob Moore32c9ef92009-02-18 14:36:05 +0800305 /* Insert the SLP_TYP bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Bob Moore32c9ef92009-02-18 14:36:05 +0800307 pm1a_control |=
Len Brown4be44fc2005-08-05 00:44:28 -0400308 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
Bob Moore32c9ef92009-02-18 14:36:05 +0800309 pm1b_control |=
Len Brown4be44fc2005-08-05 00:44:28 -0400310 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /*
313 * We split the writes of SLP_TYP and SLP_EN to workaround
314 * poorly implemented hardware.
315 */
316
Bob Moore32c9ef92009-02-18 14:36:05 +0800317 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Bob Moore32c9ef92009-02-18 14:36:05 +0800319 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400320 if (ACPI_FAILURE(status)) {
321 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Bob Moore32c9ef92009-02-18 14:36:05 +0800324 /* Insert the sleep enable (SLP_EN) bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Bob Moore32c9ef92009-02-18 14:36:05 +0800326 pm1a_control |= sleep_enable_reg_info->access_bit_mask;
327 pm1b_control |= sleep_enable_reg_info->access_bit_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Bob Moore32c9ef92009-02-18 14:36:05 +0800329 /* Flush caches, as per ACPI specification */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Len Brown4be44fc2005-08-05 00:44:28 -0400331 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Bob Moore32c9ef92009-02-18 14:36:05 +0800333 /* Write #2: Write both SLP_TYP + SLP_EN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Bob Moore32c9ef92009-02-18 14:36:05 +0800335 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400336 if (ACPI_FAILURE(status)) {
337 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 if (sleep_state > ACPI_STATE_S3) {
341 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400342 * We wanted to sleep > S3, but it didn't happen (by virtue of the
343 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 *
Robert Moore44f6c012005-04-18 22:49:35 -0400345 * Wait ten seconds, then try again. This is to get S4/S5 to work on
346 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
348 * We wait so long to allow chipsets that poll this reg very slowly to
349 * still read the right value. Ideally, this block would go
350 * away entirely.
351 */
Len Brown4be44fc2005-08-05 00:44:28 -0400352 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400354 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400355 sleep_enable_reg_info->
356 access_bit_mask);
357 if (ACPI_FAILURE(status)) {
358 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 }
361
362 /* Wait until we enter sleep state */
363
364 do {
Alexey Starikovskiy2d571b32007-09-30 22:39:42 +0400365 status = acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS,
366 &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400367 if (ACPI_FAILURE(status)) {
368 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 /* Spin until we wake */
372
373 } while (!in_value);
374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Bob Moore83135242006-10-03 00:00:00 -0400378ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Robert Moore44f6c012005-04-18 22:49:35 -0400380/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
382 * FUNCTION: acpi_enter_sleep_state_s4bios
383 *
384 * PARAMETERS: None
385 *
386 * RETURN: Status
387 *
388 * DESCRIPTION: Perform a S4 bios request.
389 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
390 *
391 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400392acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Len Brown4be44fc2005-08-05 00:44:28 -0400394 u32 in_value;
395 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Bob Mooreb229cf92006-04-21 17:15:00 -0400397 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Bob Moored8c71b62007-02-02 19:48:21 +0300399 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400400 if (ACPI_FAILURE(status)) {
401 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Bob Moored8c71b62007-02-02 19:48:21 +0300404 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400405 if (ACPI_FAILURE(status)) {
406 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 /*
410 * 1) Disable/Clear all GPEs
411 * 2) Enable all wakeup GPEs
412 */
Len Brown4be44fc2005-08-05 00:44:28 -0400413 status = acpi_hw_disable_all_gpes();
414 if (ACPI_FAILURE(status)) {
415 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 acpi_gbl_system_awake_and_running = FALSE;
418
Len Brown4be44fc2005-08-05 00:44:28 -0400419 status = acpi_hw_enable_all_wakeup_gpes();
420 if (ACPI_FAILURE(status)) {
421 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Len Brown4be44fc2005-08-05 00:44:28 -0400424 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Bob Mooref3d2e782007-02-02 19:48:18 +0300426 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
427 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 do {
430 acpi_os_stall(1000);
Bob Moored8c71b62007-02-02 19:48:21 +0300431 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400432 if (ACPI_FAILURE(status)) {
433 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 } while (!in_value);
436
Len Brown4be44fc2005-08-05 00:44:28 -0400437 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Bob Moore83135242006-10-03 00:00:00 -0400440ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Robert Moore44f6c012005-04-18 22:49:35 -0400442/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100444 * FUNCTION: acpi_leave_sleep_state_prep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100446 * PARAMETERS: sleep_state - Which sleep state we are exiting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
448 * RETURN: Status
449 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100450 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
451 * sleep.
452 * Called with interrupts DISABLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *
454 ******************************************************************************/
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100455acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Len Brown4be44fc2005-08-05 00:44:28 -0400457 struct acpi_object_list arg_list;
458 union acpi_object arg;
459 acpi_status status;
460 struct acpi_bit_register_info *sleep_type_reg_info;
461 struct acpi_bit_register_info *sleep_enable_reg_info;
Bob Moore32c9ef92009-02-18 14:36:05 +0800462 u32 pm1a_control;
463 u32 pm1b_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100465 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /*
468 * Set SLP_TYPE and SLP_EN to state S0.
469 * This is unclear from the ACPI Spec, but it is required
470 * by some machines.
471 */
Len Brown4be44fc2005-08-05 00:44:28 -0400472 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
473 &acpi_gbl_sleep_type_a,
474 &acpi_gbl_sleep_type_b);
475 if (ACPI_SUCCESS(status)) {
476 sleep_type_reg_info =
Bob Moore82d79b82009-02-18 14:31:05 +0800477 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
Len Brown4be44fc2005-08-05 00:44:28 -0400478 sleep_enable_reg_info =
479 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* Get current value of PM1A control */
482
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400483 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Bob Moore32c9ef92009-02-18 14:36:05 +0800484 &pm1a_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400485 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400486
Bob Moore32c9ef92009-02-18 14:36:05 +0800487 /* Clear the SLP_EN and SLP_TYP fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Bob Moore32c9ef92009-02-18 14:36:05 +0800489 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
490 sleep_enable_reg_info->
491 access_bit_mask);
492 pm1b_control = pm1a_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Bob Moore32c9ef92009-02-18 14:36:05 +0800494 /* Insert the SLP_TYP bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Bob Moore32c9ef92009-02-18 14:36:05 +0800496 pm1a_control |=
Len Brown4be44fc2005-08-05 00:44:28 -0400497 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
498 bit_position);
Bob Moore32c9ef92009-02-18 14:36:05 +0800499 pm1b_control |=
Len Brown4be44fc2005-08-05 00:44:28 -0400500 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
501 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Bob Moore32c9ef92009-02-18 14:36:05 +0800503 /* Write the control registers and ignore any errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Bob Moore32c9ef92009-02-18 14:36:05 +0800505 (void)acpi_hw_write_pm1_control(pm1a_control,
506 pm1b_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 }
509
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100510 /* Execute the _BFS method */
511
512 arg_list.count = 1;
513 arg_list.pointer = &arg;
514 arg.type = ACPI_TYPE_INTEGER;
515 arg.integer.value = sleep_state;
516
517 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
518 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
519 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
520 }
521
522 return_ACPI_STATUS(status);
523}
524
525/*******************************************************************************
526 *
527 * FUNCTION: acpi_leave_sleep_state
528 *
529 * PARAMETERS: sleep_state - Which sleep state we just exited
530 *
531 * RETURN: Status
532 *
533 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
534 * Called with interrupts ENABLED.
535 *
536 ******************************************************************************/
537acpi_status acpi_leave_sleep_state(u8 sleep_state)
538{
539 struct acpi_object_list arg_list;
540 union acpi_object arg;
541 acpi_status status;
542
543 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
546
547 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
548
549 /* Setup parameter object */
550
551 arg_list.count = 1;
552 arg_list.pointer = &arg;
553 arg.type = ACPI_TYPE_INTEGER;
554
555 /* Ignore any errors from these methods */
556
557 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400558 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
559 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500560 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400564 * GPEs must be enabled before _WAK is called as GPEs
565 * might get fired there
566 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 * Restore the GPEs:
568 * 1) Disable/Clear all GPEs
569 * 2) Enable all runtime GPEs
570 */
Len Brown4be44fc2005-08-05 00:44:28 -0400571 status = acpi_hw_disable_all_gpes();
572 if (ACPI_FAILURE(status)) {
573 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Len Brown4be44fc2005-08-05 00:44:28 -0400575 status = acpi_hw_enable_all_runtime_gpes();
576 if (ACPI_FAILURE(status)) {
577 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
Rafael J. Wysocki314ccd62008-02-13 00:32:16 +0100580 arg.integer.value = sleep_state;
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400581 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
582 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
583 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
584 }
585 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
586
Matthew Garretta68823e2008-08-06 19:12:04 +0100587 /*
588 * Some BIOSes assume that WAK_STS will be cleared on resume and use
589 * it to determine whether the system is rebooting or resuming. Clear
590 * it for compatibility.
591 */
592 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
593
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400594 acpi_gbl_system_awake_and_running = TRUE;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* Enable power button */
597
Len Brown4be44fc2005-08-05 00:44:28 -0400598 (void)
599 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300600 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
Robert Moore44f6c012005-04-18 22:49:35 -0400601
Len Brown4be44fc2005-08-05 00:44:28 -0400602 (void)
603 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300604 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400607 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
608 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500609 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Len Brown4be44fc2005-08-05 00:44:28 -0400612 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
Bob Moore83135242006-10-03 00:00:00 -0400614
615ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)