blob: 25dccdf179b9e4814ca52669f268f4409b34ce6b [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>
Bob Mooref3d2e782007-02-02 19:48:18 +030046#include <acpi/actables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("hwsleep")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Robert Moore44f6c012005-04-18 22:49:35 -040051/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
53 * FUNCTION: acpi_set_firmware_waking_vector
54 *
55 * PARAMETERS: physical_address - Physical address of ACPI real mode
56 * entry point.
57 *
58 * RETURN: Status
59 *
Robert Moore44f6c012005-04-18 22:49:35 -040060 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070063acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040064acpi_set_firmware_waking_vector(acpi_physical_address physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Bob Mooref3d2e782007-02-02 19:48:18 +030066 struct acpi_table_facs *facs;
67 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bob Mooreb229cf92006-04-21 17:15:00 -040069 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Bob Mooref3d2e782007-02-02 19:48:18 +030071 /* Get the FACS */
72
Bob Moore1d18c052008-04-10 19:06:40 +040073 status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
74 ACPI_CAST_INDIRECT_PTR(struct
75 acpi_table_header,
76 &facs));
Bob Mooref3d2e782007-02-02 19:48:18 +030077 if (ACPI_FAILURE(status)) {
78 return_ACPI_STATUS(status);
79 }
80
Rafael J. Wysockia6629102008-09-06 13:13:01 +020081 /*
82 * According to the ACPI specification 2.0c and later, the 64-bit
83 * waking vector should be cleared and the 32-bit waking vector should
84 * be used, unless we want the wake-up code to be called by the BIOS in
85 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
86 * to fail to resume if the 64-bit vector is used.
87 */
88 if (facs->version >= 1)
89 facs->xfirmware_waking_vector = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Rafael J. Wysockia6629102008-09-06 13:13:01 +020091 facs->firmware_waking_vector = (u32)physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Len Brown4be44fc2005-08-05 00:44:28 -040093 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Bob Moore83135242006-10-03 00:00:00 -040096ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
97
Robert Moore44f6c012005-04-18 22:49:35 -040098/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *
100 * FUNCTION: acpi_get_firmware_waking_vector
101 *
Robert Moore44f6c012005-04-18 22:49:35 -0400102 * PARAMETERS: *physical_address - Where the contents of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * the firmware_waking_vector field of
Robert Moore44f6c012005-04-18 22:49:35 -0400104 * the FACS will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
Robert Moore44f6c012005-04-18 22:49:35 -0400106 * RETURN: Status, vector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Robert Moore44f6c012005-04-18 22:49:35 -0400108 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
110 ******************************************************************************/
111#ifdef ACPI_FUTURE_USAGE
112acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400113acpi_get_firmware_waking_vector(acpi_physical_address * physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Bob Mooref3d2e782007-02-02 19:48:18 +0300115 struct acpi_table_facs *facs;
116 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Bob Mooreb229cf92006-04-21 17:15:00 -0400118 ACPI_FUNCTION_TRACE(acpi_get_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (!physical_address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Bob Mooref3d2e782007-02-02 19:48:18 +0300124 /* Get the FACS */
125
Bob Moore1d18c052008-04-10 19:06:40 +0400126 status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
127 ACPI_CAST_INDIRECT_PTR(struct
128 acpi_table_header,
129 &facs));
Bob Mooref3d2e782007-02-02 19:48:18 +0300130 if (ACPI_FAILURE(status)) {
131 return_ACPI_STATUS(status);
132 }
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Get the vector */
Rafael J. Wysockia6629102008-09-06 13:13:01 +0200135 *physical_address = (acpi_physical_address)facs->firmware_waking_vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Len Brown4be44fc2005-08-05 00:44:28 -0400137 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Bob Moore83135242006-10-03 00:00:00 -0400139
140ACPI_EXPORT_SYMBOL(acpi_get_firmware_waking_vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif
Robert Moore44f6c012005-04-18 22:49:35 -0400142/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 *
144 * FUNCTION: acpi_enter_sleep_state_prep
145 *
146 * PARAMETERS: sleep_state - Which sleep state to enter
147 *
148 * RETURN: Status
149 *
150 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
151 * This function must execute with interrupts enabled.
152 * We break sleeping into 2 stages so that OSPM can handle
153 * various OS-specific tasks between the two steps.
154 *
155 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400156acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Len Brown4be44fc2005-08-05 00:44:28 -0400158 acpi_status status;
159 struct acpi_object_list arg_list;
160 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Bob Mooreb229cf92006-04-21 17:15:00 -0400162 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /*
165 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
166 */
Len Brown4be44fc2005-08-05 00:44:28 -0400167 status = acpi_get_sleep_type_data(sleep_state,
168 &acpi_gbl_sleep_type_a,
169 &acpi_gbl_sleep_type_b);
170 if (ACPI_FAILURE(status)) {
171 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 /* Setup parameter object */
175
176 arg_list.count = 1;
177 arg_list.pointer = &arg;
178
179 arg.type = ACPI_TYPE_INTEGER;
180 arg.integer.value = sleep_state;
181
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100182 /* Run the _PTS method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Len Brown4be44fc2005-08-05 00:44:28 -0400184 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
185 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
186 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Setup the argument to _SST */
190
191 switch (sleep_state) {
192 case ACPI_STATE_S0:
193 arg.integer.value = ACPI_SST_WORKING;
194 break;
195
196 case ACPI_STATE_S1:
197 case ACPI_STATE_S2:
198 case ACPI_STATE_S3:
199 arg.integer.value = ACPI_SST_SLEEPING;
200 break;
201
202 case ACPI_STATE_S4:
203 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
204 break;
205
206 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400207 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 }
210
Bob Moored52c79a2008-06-10 14:26:57 +0800211 /*
212 * Set the system indicators to show the desired sleep state.
213 * _SST is an optional method (return no error if not found)
214 */
Len Brown4be44fc2005-08-05 00:44:28 -0400215 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
216 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500217 ACPI_EXCEPTION((AE_INFO, status,
218 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Bob Moored52c79a2008-06-10 14:26:57 +0800221 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Bob Moore83135242006-10-03 00:00:00 -0400224ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
225
Robert Moore44f6c012005-04-18 22:49:35 -0400226/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
228 * FUNCTION: acpi_enter_sleep_state
229 *
230 * PARAMETERS: sleep_state - Which sleep state to enter
231 *
232 * RETURN: Status
233 *
234 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
235 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
236 *
237 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400238acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Len Brown4be44fc2005-08-05 00:44:28 -0400240 u32 PM1Acontrol;
241 u32 PM1Bcontrol;
242 struct acpi_bit_register_info *sleep_type_reg_info;
243 struct acpi_bit_register_info *sleep_enable_reg_info;
244 u32 in_value;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100245 struct acpi_object_list arg_list;
246 union acpi_object arg;
Len Brown4be44fc2005-08-05 00:44:28 -0400247 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Bob Mooreb229cf92006-04-21 17:15:00 -0400249 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400252 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500253 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
254 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400255 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258 sleep_type_reg_info =
259 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
260 sleep_enable_reg_info =
261 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* Clear wake status */
264
Bob Moored8c71b62007-02-02 19:48:21 +0300265 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400266 if (ACPI_FAILURE(status)) {
267 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 /* Clear all fixed and general purpose status bits */
271
Bob Moored8c71b62007-02-02 19:48:21 +0300272 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400273 if (ACPI_FAILURE(status)) {
274 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 /*
Pavel Machek23b168d2008-02-05 19:27:12 +0100278 * 1) Disable/Clear all GPEs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * 2) Enable all wakeup GPEs
280 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400281 status = acpi_hw_disable_all_gpes();
282 if (ACPI_FAILURE(status)) {
283 return_ACPI_STATUS(status);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 acpi_gbl_system_awake_and_running = FALSE;
286
Len Brown4be44fc2005-08-05 00:44:28 -0400287 status = acpi_hw_enable_all_wakeup_gpes();
288 if (ACPI_FAILURE(status)) {
289 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100292 /* Execute the _GTS method */
293
294 arg_list.count = 1;
295 arg_list.pointer = &arg;
296 arg.type = ACPI_TYPE_INTEGER;
297 arg.integer.value = sleep_state;
298
299 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
300 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
301 return_ACPI_STATUS(status);
302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Get current value of PM1A control */
305
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400306 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
Len Brown4be44fc2005-08-05 00:44:28 -0400307 if (ACPI_FAILURE(status)) {
308 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Len Brown4be44fc2005-08-05 00:44:28 -0400310 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
311 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* Clear SLP_EN and SLP_TYP fields */
314
Robert Moore44f6c012005-04-18 22:49:35 -0400315 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400316 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 PM1Bcontrol = PM1Acontrol;
318
319 /* Insert SLP_TYP bits */
320
Len Brown4be44fc2005-08-05 00:44:28 -0400321 PM1Acontrol |=
322 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
323 PM1Bcontrol |=
324 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /*
327 * We split the writes of SLP_TYP and SLP_EN to workaround
328 * poorly implemented hardware.
329 */
330
331 /* Write #1: fill in SLP_TYP data */
332
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400333 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400334 PM1Acontrol);
335 if (ACPI_FAILURE(status)) {
336 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400339 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400340 PM1Bcontrol);
341 if (ACPI_FAILURE(status)) {
342 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 /* Insert SLP_ENABLE bit */
346
347 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
348 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
349
350 /* Write #2: SLP_TYP + SLP_EN */
351
Len Brown4be44fc2005-08-05 00:44:28 -0400352 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400354 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400355 PM1Acontrol);
356 if (ACPI_FAILURE(status)) {
357 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400360 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400361 PM1Bcontrol);
362 if (ACPI_FAILURE(status)) {
363 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 if (sleep_state > ACPI_STATE_S3) {
367 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400368 * We wanted to sleep > S3, but it didn't happen (by virtue of the
369 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *
Robert Moore44f6c012005-04-18 22:49:35 -0400371 * Wait ten seconds, then try again. This is to get S4/S5 to work on
372 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
374 * We wait so long to allow chipsets that poll this reg very slowly to
375 * still read the right value. Ideally, this block would go
376 * away entirely.
377 */
Len Brown4be44fc2005-08-05 00:44:28 -0400378 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400380 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400381 sleep_enable_reg_info->
382 access_bit_mask);
383 if (ACPI_FAILURE(status)) {
384 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 }
387
388 /* Wait until we enter sleep state */
389
390 do {
Alexey Starikovskiy2d571b32007-09-30 22:39:42 +0400391 status = acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS,
392 &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400393 if (ACPI_FAILURE(status)) {
394 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 /* Spin until we wake */
398
399 } while (!in_value);
400
Len Brown4be44fc2005-08-05 00:44:28 -0400401 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Bob Moore83135242006-10-03 00:00:00 -0400404ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Robert Moore44f6c012005-04-18 22:49:35 -0400406/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 *
408 * FUNCTION: acpi_enter_sleep_state_s4bios
409 *
410 * PARAMETERS: None
411 *
412 * RETURN: Status
413 *
414 * DESCRIPTION: Perform a S4 bios request.
415 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
416 *
417 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400418acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Len Brown4be44fc2005-08-05 00:44:28 -0400420 u32 in_value;
421 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Bob Mooreb229cf92006-04-21 17:15:00 -0400423 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Bob Moored8c71b62007-02-02 19:48:21 +0300425 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400426 if (ACPI_FAILURE(status)) {
427 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Bob Moored8c71b62007-02-02 19:48:21 +0300430 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400431 if (ACPI_FAILURE(status)) {
432 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 /*
436 * 1) Disable/Clear all GPEs
437 * 2) Enable all wakeup GPEs
438 */
Len Brown4be44fc2005-08-05 00:44:28 -0400439 status = acpi_hw_disable_all_gpes();
440 if (ACPI_FAILURE(status)) {
441 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 acpi_gbl_system_awake_and_running = FALSE;
444
Len Brown4be44fc2005-08-05 00:44:28 -0400445 status = acpi_hw_enable_all_wakeup_gpes();
446 if (ACPI_FAILURE(status)) {
447 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Bob Mooref3d2e782007-02-02 19:48:18 +0300452 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
453 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 do {
456 acpi_os_stall(1000);
Bob Moored8c71b62007-02-02 19:48:21 +0300457 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400458 if (ACPI_FAILURE(status)) {
459 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 } while (!in_value);
462
Len Brown4be44fc2005-08-05 00:44:28 -0400463 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Bob Moore83135242006-10-03 00:00:00 -0400466ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Robert Moore44f6c012005-04-18 22:49:35 -0400468/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100470 * FUNCTION: acpi_leave_sleep_state_prep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100472 * PARAMETERS: sleep_state - Which sleep state we are exiting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *
474 * RETURN: Status
475 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100476 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
477 * sleep.
478 * Called with interrupts DISABLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *
480 ******************************************************************************/
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100481acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Len Brown4be44fc2005-08-05 00:44:28 -0400483 struct acpi_object_list arg_list;
484 union acpi_object arg;
485 acpi_status status;
486 struct acpi_bit_register_info *sleep_type_reg_info;
487 struct acpi_bit_register_info *sleep_enable_reg_info;
488 u32 PM1Acontrol;
489 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100491 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Set SLP_TYPE and SLP_EN to state S0.
495 * This is unclear from the ACPI Spec, but it is required
496 * by some machines.
497 */
Len Brown4be44fc2005-08-05 00:44:28 -0400498 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
499 &acpi_gbl_sleep_type_a,
500 &acpi_gbl_sleep_type_b);
501 if (ACPI_SUCCESS(status)) {
502 sleep_type_reg_info =
503 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
504 sleep_enable_reg_info =
505 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* Get current value of PM1A control */
508
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400509 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400510 &PM1Acontrol);
511 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Clear SLP_EN and SLP_TYP fields */
514
515 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400516 sleep_enable_reg_info->
517 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 PM1Bcontrol = PM1Acontrol;
519
520 /* Insert SLP_TYP bits */
521
Len Brown4be44fc2005-08-05 00:44:28 -0400522 PM1Acontrol |=
523 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
524 bit_position);
525 PM1Bcontrol |=
526 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
527 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* Just ignore any errors */
530
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400531 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400532 PM1Acontrol);
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400533 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400534 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 }
537
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100538 /* Execute the _BFS method */
539
540 arg_list.count = 1;
541 arg_list.pointer = &arg;
542 arg.type = ACPI_TYPE_INTEGER;
543 arg.integer.value = sleep_state;
544
545 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
546 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
547 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
548 }
549
550 return_ACPI_STATUS(status);
551}
552
553/*******************************************************************************
554 *
555 * FUNCTION: acpi_leave_sleep_state
556 *
557 * PARAMETERS: sleep_state - Which sleep state we just exited
558 *
559 * RETURN: Status
560 *
561 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
562 * Called with interrupts ENABLED.
563 *
564 ******************************************************************************/
565acpi_status acpi_leave_sleep_state(u8 sleep_state)
566{
567 struct acpi_object_list arg_list;
568 union acpi_object arg;
569 acpi_status status;
570
571 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
574
575 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
576
577 /* Setup parameter object */
578
579 arg_list.count = 1;
580 arg_list.pointer = &arg;
581 arg.type = ACPI_TYPE_INTEGER;
582
583 /* Ignore any errors from these methods */
584
585 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400586 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
587 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500588 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400592 * GPEs must be enabled before _WAK is called as GPEs
593 * might get fired there
594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Restore the GPEs:
596 * 1) Disable/Clear all GPEs
597 * 2) Enable all runtime GPEs
598 */
Len Brown4be44fc2005-08-05 00:44:28 -0400599 status = acpi_hw_disable_all_gpes();
600 if (ACPI_FAILURE(status)) {
601 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Len Brown4be44fc2005-08-05 00:44:28 -0400603 status = acpi_hw_enable_all_runtime_gpes();
604 if (ACPI_FAILURE(status)) {
605 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
Rafael J. Wysocki314ccd62008-02-13 00:32:16 +0100608 arg.integer.value = sleep_state;
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400609 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
610 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
611 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
612 }
613 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
614
Matthew Garretta68823e2008-08-06 19:12:04 +0100615 /*
616 * Some BIOSes assume that WAK_STS will be cleared on resume and use
617 * it to determine whether the system is rebooting or resuming. Clear
618 * it for compatibility.
619 */
620 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
621
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400622 acpi_gbl_system_awake_and_running = TRUE;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Enable power button */
625
Len Brown4be44fc2005-08-05 00:44:28 -0400626 (void)
627 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300628 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
Robert Moore44f6c012005-04-18 22:49:35 -0400629
Len Brown4be44fc2005-08-05 00:44:28 -0400630 (void)
631 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300632 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400635 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
636 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500637 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Len Brown4be44fc2005-08-05 00:44:28 -0400640 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
Bob Moore83135242006-10-03 00:00:00 -0400642
643ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)