blob: 13c93a13785e3e56d415d1f363fa678ba8956adc [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/*
Bob Moore6c9deb72007-02-02 19:48:24 +03009 * Copyright (C) 2000 - 2007, R. Byron Moore
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
73 status =
74 acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
75 (struct acpi_table_header **)&facs);
76 if (ACPI_FAILURE(status)) {
77 return_ACPI_STATUS(status);
78 }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* Set the vector */
81
Bob Mooref3d2e782007-02-02 19:48:18 +030082 if ((facs->length < 32) || (!(facs->xfirmware_waking_vector))) {
83 /*
84 * ACPI 1.0 FACS or short table or optional X_ field is zero
85 */
86 facs->firmware_waking_vector = (u32) physical_address;
Len Brown4be44fc2005-08-05 00:44:28 -040087 } else {
Bob Mooref3d2e782007-02-02 19:48:18 +030088 /*
89 * ACPI 2.0 FACS with valid X_ field
90 */
91 facs->xfirmware_waking_vector = physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Len Brown4be44fc2005-08-05 00:44:28 -040094 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Bob Moore83135242006-10-03 00:00:00 -040097ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
98
Robert Moore44f6c012005-04-18 22:49:35 -040099/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * FUNCTION: acpi_get_firmware_waking_vector
102 *
Robert Moore44f6c012005-04-18 22:49:35 -0400103 * PARAMETERS: *physical_address - Where the contents of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * the firmware_waking_vector field of
Robert Moore44f6c012005-04-18 22:49:35 -0400105 * the FACS will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
Robert Moore44f6c012005-04-18 22:49:35 -0400107 * RETURN: Status, vector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Robert Moore44f6c012005-04-18 22:49:35 -0400109 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
111 ******************************************************************************/
112#ifdef ACPI_FUTURE_USAGE
113acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400114acpi_get_firmware_waking_vector(acpi_physical_address * physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Bob Mooref3d2e782007-02-02 19:48:18 +0300116 struct acpi_table_facs *facs;
117 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Bob Mooreb229cf92006-04-21 17:15:00 -0400119 ACPI_FUNCTION_TRACE(acpi_get_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (!physical_address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400122 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Bob Mooref3d2e782007-02-02 19:48:18 +0300125 /* Get the FACS */
126
127 status =
128 acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
129 (struct acpi_table_header **)&facs);
130 if (ACPI_FAILURE(status)) {
131 return_ACPI_STATUS(status);
132 }
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Get the vector */
135
Bob Mooref3d2e782007-02-02 19:48:18 +0300136 if ((facs->length < 32) || (!(facs->xfirmware_waking_vector))) {
137 /*
138 * ACPI 1.0 FACS or short table or optional X_ field is zero
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *physical_address =
Bob Mooref3d2e782007-02-02 19:48:18 +0300141 (acpi_physical_address) facs->firmware_waking_vector;
142 } else {
143 /*
144 * ACPI 2.0 FACS with valid X_ field
145 */
146 *physical_address =
147 (acpi_physical_address) facs->xfirmware_waking_vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
Len Brown4be44fc2005-08-05 00:44:28 -0400150 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Bob Moore83135242006-10-03 00:00:00 -0400152
153ACPI_EXPORT_SYMBOL(acpi_get_firmware_waking_vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#endif
Robert Moore44f6c012005-04-18 22:49:35 -0400155/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 *
157 * FUNCTION: acpi_enter_sleep_state_prep
158 *
159 * PARAMETERS: sleep_state - Which sleep state to enter
160 *
161 * RETURN: Status
162 *
163 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
164 * This function must execute with interrupts enabled.
165 * We break sleeping into 2 stages so that OSPM can handle
166 * various OS-specific tasks between the two steps.
167 *
168 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400169acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Len Brown4be44fc2005-08-05 00:44:28 -0400171 acpi_status status;
172 struct acpi_object_list arg_list;
173 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Bob Mooreb229cf92006-04-21 17:15:00 -0400175 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /*
178 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
179 */
Len Brown4be44fc2005-08-05 00:44:28 -0400180 status = acpi_get_sleep_type_data(sleep_state,
181 &acpi_gbl_sleep_type_a,
182 &acpi_gbl_sleep_type_b);
183 if (ACPI_FAILURE(status)) {
184 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
187 /* Setup parameter object */
188
189 arg_list.count = 1;
190 arg_list.pointer = &arg;
191
192 arg.type = ACPI_TYPE_INTEGER;
193 arg.integer.value = sleep_state;
194
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100195 /* Run the _PTS method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Len Brown4be44fc2005-08-05 00:44:28 -0400197 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
198 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
199 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Setup the argument to _SST */
203
204 switch (sleep_state) {
205 case ACPI_STATE_S0:
206 arg.integer.value = ACPI_SST_WORKING;
207 break;
208
209 case ACPI_STATE_S1:
210 case ACPI_STATE_S2:
211 case ACPI_STATE_S3:
212 arg.integer.value = ACPI_SST_SLEEPING;
213 break;
214
215 case ACPI_STATE_S4:
216 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
217 break;
218
219 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400220 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
222 }
223
224 /* Set the system indicators to show the desired sleep state. */
225
Len Brown4be44fc2005-08-05 00:44:28 -0400226 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
227 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500228 ACPI_EXCEPTION((AE_INFO, status,
229 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
Rafael J. Wysocki6cffd462007-10-02 13:24:09 -0700232 /* Disable/Clear all GPEs */
Alexey Starikovskiyed41dab2007-02-10 01:30:35 -0500233
Rafael J. Wysocki6cffd462007-10-02 13:24:09 -0700234 status = acpi_hw_disable_all_gpes();
235
236 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Bob Moore83135242006-10-03 00:00:00 -0400239ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
240
Robert Moore44f6c012005-04-18 22:49:35 -0400241/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
243 * FUNCTION: acpi_enter_sleep_state
244 *
245 * PARAMETERS: sleep_state - Which sleep state to enter
246 *
247 * RETURN: Status
248 *
249 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
250 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
251 *
252 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400253acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Len Brown4be44fc2005-08-05 00:44:28 -0400255 u32 PM1Acontrol;
256 u32 PM1Bcontrol;
257 struct acpi_bit_register_info *sleep_type_reg_info;
258 struct acpi_bit_register_info *sleep_enable_reg_info;
259 u32 in_value;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100260 struct acpi_object_list arg_list;
261 union acpi_object arg;
Len Brown4be44fc2005-08-05 00:44:28 -0400262 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Bob Mooreb229cf92006-04-21 17:15:00 -0400264 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400267 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500268 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
269 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400270 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
Len Brown4be44fc2005-08-05 00:44:28 -0400273 sleep_type_reg_info =
274 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
275 sleep_enable_reg_info =
276 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* Clear wake status */
279
Bob Moored8c71b62007-02-02 19:48:21 +0300280 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400281 if (ACPI_FAILURE(status)) {
282 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
285 /* Clear all fixed and general purpose status bits */
286
Bob Moored8c71b62007-02-02 19:48:21 +0300287 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400288 if (ACPI_FAILURE(status)) {
289 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
292 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * 2) Enable all wakeup GPEs
294 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400295 status = acpi_hw_disable_all_gpes();
296 if (ACPI_FAILURE(status)) {
297 return_ACPI_STATUS(status);
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 acpi_gbl_system_awake_and_running = FALSE;
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302 status = acpi_hw_enable_all_wakeup_gpes();
303 if (ACPI_FAILURE(status)) {
304 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100307 /* Execute the _GTS method */
308
309 arg_list.count = 1;
310 arg_list.pointer = &arg;
311 arg.type = ACPI_TYPE_INTEGER;
312 arg.integer.value = sleep_state;
313
314 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
315 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
316 return_ACPI_STATUS(status);
317 }
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* Get current value of PM1A control */
320
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400321 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
Len Brown4be44fc2005-08-05 00:44:28 -0400322 if (ACPI_FAILURE(status)) {
323 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Len Brown4be44fc2005-08-05 00:44:28 -0400325 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
326 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Clear SLP_EN and SLP_TYP fields */
329
Robert Moore44f6c012005-04-18 22:49:35 -0400330 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400331 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 PM1Bcontrol = PM1Acontrol;
333
334 /* Insert SLP_TYP bits */
335
Len Brown4be44fc2005-08-05 00:44:28 -0400336 PM1Acontrol |=
337 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
338 PM1Bcontrol |=
339 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /*
342 * We split the writes of SLP_TYP and SLP_EN to workaround
343 * poorly implemented hardware.
344 */
345
346 /* Write #1: fill in SLP_TYP data */
347
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400348 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400349 PM1Acontrol);
350 if (ACPI_FAILURE(status)) {
351 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400354 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400355 PM1Bcontrol);
356 if (ACPI_FAILURE(status)) {
357 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 /* Insert SLP_ENABLE bit */
361
362 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
363 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
364
365 /* Write #2: SLP_TYP + SLP_EN */
366
Len Brown4be44fc2005-08-05 00:44:28 -0400367 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400369 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400370 PM1Acontrol);
371 if (ACPI_FAILURE(status)) {
372 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400375 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400376 PM1Bcontrol);
377 if (ACPI_FAILURE(status)) {
378 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
381 if (sleep_state > ACPI_STATE_S3) {
382 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400383 * We wanted to sleep > S3, but it didn't happen (by virtue of the
384 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
Robert Moore44f6c012005-04-18 22:49:35 -0400386 * Wait ten seconds, then try again. This is to get S4/S5 to work on
387 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *
389 * We wait so long to allow chipsets that poll this reg very slowly to
390 * still read the right value. Ideally, this block would go
391 * away entirely.
392 */
Len Brown4be44fc2005-08-05 00:44:28 -0400393 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400395 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400396 sleep_enable_reg_info->
397 access_bit_mask);
398 if (ACPI_FAILURE(status)) {
399 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 }
402
403 /* Wait until we enter sleep state */
404
405 do {
Alexey Starikovskiy2d571b32007-09-30 22:39:42 +0400406 status = acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS,
407 &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400408 if (ACPI_FAILURE(status)) {
409 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 /* Spin until we wake */
413
414 } while (!in_value);
415
Len Brown4be44fc2005-08-05 00:44:28 -0400416 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Bob Moore83135242006-10-03 00:00:00 -0400419ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Robert Moore44f6c012005-04-18 22:49:35 -0400421/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 *
423 * FUNCTION: acpi_enter_sleep_state_s4bios
424 *
425 * PARAMETERS: None
426 *
427 * RETURN: Status
428 *
429 * DESCRIPTION: Perform a S4 bios request.
430 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
431 *
432 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400433acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Len Brown4be44fc2005-08-05 00:44:28 -0400435 u32 in_value;
436 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Bob Mooreb229cf92006-04-21 17:15:00 -0400438 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Bob Moored8c71b62007-02-02 19:48:21 +0300440 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400441 if (ACPI_FAILURE(status)) {
442 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Bob Moored8c71b62007-02-02 19:48:21 +0300445 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400446 if (ACPI_FAILURE(status)) {
447 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
450 /*
451 * 1) Disable/Clear all GPEs
452 * 2) Enable all wakeup GPEs
453 */
Len Brown4be44fc2005-08-05 00:44:28 -0400454 status = acpi_hw_disable_all_gpes();
455 if (ACPI_FAILURE(status)) {
456 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 acpi_gbl_system_awake_and_running = FALSE;
459
Len Brown4be44fc2005-08-05 00:44:28 -0400460 status = acpi_hw_enable_all_wakeup_gpes();
461 if (ACPI_FAILURE(status)) {
462 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
Len Brown4be44fc2005-08-05 00:44:28 -0400465 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Bob Mooref3d2e782007-02-02 19:48:18 +0300467 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
468 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 do {
471 acpi_os_stall(1000);
Bob Moored8c71b62007-02-02 19:48:21 +0300472 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400473 if (ACPI_FAILURE(status)) {
474 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 } while (!in_value);
477
Len Brown4be44fc2005-08-05 00:44:28 -0400478 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Bob Moore83135242006-10-03 00:00:00 -0400481ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Robert Moore44f6c012005-04-18 22:49:35 -0400483/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100485 * FUNCTION: acpi_leave_sleep_state_prep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100487 * PARAMETERS: sleep_state - Which sleep state we are exiting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 *
489 * RETURN: Status
490 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100491 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
492 * sleep.
493 * Called with interrupts DISABLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *
495 ******************************************************************************/
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100496acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Len Brown4be44fc2005-08-05 00:44:28 -0400498 struct acpi_object_list arg_list;
499 union acpi_object arg;
500 acpi_status status;
501 struct acpi_bit_register_info *sleep_type_reg_info;
502 struct acpi_bit_register_info *sleep_enable_reg_info;
503 u32 PM1Acontrol;
504 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100506 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /*
509 * Set SLP_TYPE and SLP_EN to state S0.
510 * This is unclear from the ACPI Spec, but it is required
511 * by some machines.
512 */
Len Brown4be44fc2005-08-05 00:44:28 -0400513 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
514 &acpi_gbl_sleep_type_a,
515 &acpi_gbl_sleep_type_b);
516 if (ACPI_SUCCESS(status)) {
517 sleep_type_reg_info =
518 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
519 sleep_enable_reg_info =
520 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* Get current value of PM1A control */
523
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400524 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400525 &PM1Acontrol);
526 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* Clear SLP_EN and SLP_TYP fields */
529
530 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400531 sleep_enable_reg_info->
532 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 PM1Bcontrol = PM1Acontrol;
534
535 /* Insert SLP_TYP bits */
536
Len Brown4be44fc2005-08-05 00:44:28 -0400537 PM1Acontrol |=
538 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
539 bit_position);
540 PM1Bcontrol |=
541 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
542 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* Just ignore any errors */
545
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400546 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400547 PM1Acontrol);
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400548 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400549 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 }
552
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100553 /* Execute the _BFS method */
554
555 arg_list.count = 1;
556 arg_list.pointer = &arg;
557 arg.type = ACPI_TYPE_INTEGER;
558 arg.integer.value = sleep_state;
559
560 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
561 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
562 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
563 }
564
565 return_ACPI_STATUS(status);
566}
567
568/*******************************************************************************
569 *
570 * FUNCTION: acpi_leave_sleep_state
571 *
572 * PARAMETERS: sleep_state - Which sleep state we just exited
573 *
574 * RETURN: Status
575 *
576 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
577 * Called with interrupts ENABLED.
578 *
579 ******************************************************************************/
580acpi_status acpi_leave_sleep_state(u8 sleep_state)
581{
582 struct acpi_object_list arg_list;
583 union acpi_object arg;
584 acpi_status status;
585
586 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
589
590 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
591
592 /* Setup parameter object */
593
594 arg_list.count = 1;
595 arg_list.pointer = &arg;
596 arg.type = ACPI_TYPE_INTEGER;
597
598 /* Ignore any errors from these methods */
599
600 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400601 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
602 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500603 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400607 * GPEs must be enabled before _WAK is called as GPEs
608 * might get fired there
609 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * Restore the GPEs:
611 * 1) Disable/Clear all GPEs
612 * 2) Enable all runtime GPEs
613 */
Len Brown4be44fc2005-08-05 00:44:28 -0400614 status = acpi_hw_disable_all_gpes();
615 if (ACPI_FAILURE(status)) {
616 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Len Brown4be44fc2005-08-05 00:44:28 -0400618 status = acpi_hw_enable_all_runtime_gpes();
619 if (ACPI_FAILURE(status)) {
620 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400623 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
624 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
625 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
626 }
627 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
628
629 acpi_gbl_system_awake_and_running = TRUE;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* Enable power button */
632
Len Brown4be44fc2005-08-05 00:44:28 -0400633 (void)
634 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300635 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
Robert Moore44f6c012005-04-18 22:49:35 -0400636
Len Brown4be44fc2005-08-05 00:44:28 -0400637 (void)
638 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300639 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400642 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
643 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500644 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
Len Brown4be44fc2005-08-05 00:44:28 -0400647 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Bob Moore83135242006-10-03 00:00:00 -0400649
650ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)