blob: c0577ca55358d942fb3c4bc20d80133f89071fdf [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
195 /* Run the _PTS and _GTS methods */
196
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
Len Brown4be44fc2005-08-05 00:44:28 -0400202 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
203 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
204 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
207 /* Setup the argument to _SST */
208
209 switch (sleep_state) {
210 case ACPI_STATE_S0:
211 arg.integer.value = ACPI_SST_WORKING;
212 break;
213
214 case ACPI_STATE_S1:
215 case ACPI_STATE_S2:
216 case ACPI_STATE_S3:
217 arg.integer.value = ACPI_SST_SLEEPING;
218 break;
219
220 case ACPI_STATE_S4:
221 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
222 break;
223
224 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400225 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
227 }
228
229 /* Set the system indicators to show the desired sleep state. */
230
Len Brown4be44fc2005-08-05 00:44:28 -0400231 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
232 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500233 ACPI_EXCEPTION((AE_INFO, status,
234 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
Alexey Starikovskiyed41dab2007-02-10 01:30:35 -0500237 /*
238 * 1) Disable/Clear all GPEs
239 */
240 status = acpi_hw_disable_all_gpes();
241 if (ACPI_FAILURE(status)) {
242 return_ACPI_STATUS(status);
243 }
244
Len Brown4be44fc2005-08-05 00:44:28 -0400245 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Bob Moore83135242006-10-03 00:00:00 -0400248ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
249
Robert Moore44f6c012005-04-18 22:49:35 -0400250/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
252 * FUNCTION: acpi_enter_sleep_state
253 *
254 * PARAMETERS: sleep_state - Which sleep state to enter
255 *
256 * RETURN: Status
257 *
258 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
259 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
260 *
261 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400262acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Len Brown4be44fc2005-08-05 00:44:28 -0400264 u32 PM1Acontrol;
265 u32 PM1Bcontrol;
266 struct acpi_bit_register_info *sleep_type_reg_info;
267 struct acpi_bit_register_info *sleep_enable_reg_info;
268 u32 in_value;
269 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Bob Mooreb229cf92006-04-21 17:15:00 -0400271 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400274 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500275 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
276 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400277 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
Len Brown4be44fc2005-08-05 00:44:28 -0400280 sleep_type_reg_info =
281 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
282 sleep_enable_reg_info =
283 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Clear wake status */
286
Bob Moored8c71b62007-02-02 19:48:21 +0300287 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
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 /* Clear all fixed and general purpose status bits */
293
Bob Moored8c71b62007-02-02 19:48:21 +0300294 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400295 if (ACPI_FAILURE(status)) {
296 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
299 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * 2) Enable all wakeup GPEs
301 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400302 status = acpi_hw_disable_all_gpes();
303 if (ACPI_FAILURE(status)) {
304 return_ACPI_STATUS(status);
305 }
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 acpi_gbl_system_awake_and_running = FALSE;
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 status = acpi_hw_enable_all_wakeup_gpes();
310 if (ACPI_FAILURE(status)) {
311 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 /* Get current value of PM1A control */
315
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400316 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
Len Brown4be44fc2005-08-05 00:44:28 -0400317 if (ACPI_FAILURE(status)) {
318 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Len Brown4be44fc2005-08-05 00:44:28 -0400320 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
321 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Clear SLP_EN and SLP_TYP fields */
324
Robert Moore44f6c012005-04-18 22:49:35 -0400325 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400326 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 PM1Bcontrol = PM1Acontrol;
328
329 /* Insert SLP_TYP bits */
330
Len Brown4be44fc2005-08-05 00:44:28 -0400331 PM1Acontrol |=
332 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
333 PM1Bcontrol |=
334 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /*
337 * We split the writes of SLP_TYP and SLP_EN to workaround
338 * poorly implemented hardware.
339 */
340
341 /* Write #1: fill in SLP_TYP data */
342
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400343 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400344 PM1Acontrol);
345 if (ACPI_FAILURE(status)) {
346 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400349 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400350 PM1Bcontrol);
351 if (ACPI_FAILURE(status)) {
352 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 /* Insert SLP_ENABLE bit */
356
357 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
358 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
359
360 /* Write #2: SLP_TYP + SLP_EN */
361
Len Brown4be44fc2005-08-05 00:44:28 -0400362 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400364 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400365 PM1Acontrol);
366 if (ACPI_FAILURE(status)) {
367 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400370 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400371 PM1Bcontrol);
372 if (ACPI_FAILURE(status)) {
373 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 if (sleep_state > ACPI_STATE_S3) {
377 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400378 * We wanted to sleep > S3, but it didn't happen (by virtue of the
379 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Robert Moore44f6c012005-04-18 22:49:35 -0400381 * Wait ten seconds, then try again. This is to get S4/S5 to work on
382 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *
384 * We wait so long to allow chipsets that poll this reg very slowly to
385 * still read the right value. Ideally, this block would go
386 * away entirely.
387 */
Len Brown4be44fc2005-08-05 00:44:28 -0400388 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400390 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400391 sleep_enable_reg_info->
392 access_bit_mask);
393 if (ACPI_FAILURE(status)) {
394 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
397
398 /* Wait until we enter sleep state */
399
400 do {
Alexey Starikovskiy2d571b32007-09-30 22:39:42 +0400401 status = acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS,
402 &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400403 if (ACPI_FAILURE(status)) {
404 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 /* Spin until we wake */
408
409 } while (!in_value);
410
Len Brown4be44fc2005-08-05 00:44:28 -0400411 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Bob Moore83135242006-10-03 00:00:00 -0400414ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Robert Moore44f6c012005-04-18 22:49:35 -0400416/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
418 * FUNCTION: acpi_enter_sleep_state_s4bios
419 *
420 * PARAMETERS: None
421 *
422 * RETURN: Status
423 *
424 * DESCRIPTION: Perform a S4 bios request.
425 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
426 *
427 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400428acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Len Brown4be44fc2005-08-05 00:44:28 -0400430 u32 in_value;
431 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Bob Mooreb229cf92006-04-21 17:15:00 -0400433 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Bob Moored8c71b62007-02-02 19:48:21 +0300435 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400436 if (ACPI_FAILURE(status)) {
437 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Bob Moored8c71b62007-02-02 19:48:21 +0300440 status = acpi_hw_clear_acpi_status();
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
445 /*
446 * 1) Disable/Clear all GPEs
447 * 2) Enable all wakeup GPEs
448 */
Len Brown4be44fc2005-08-05 00:44:28 -0400449 status = acpi_hw_disable_all_gpes();
450 if (ACPI_FAILURE(status)) {
451 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 acpi_gbl_system_awake_and_running = FALSE;
454
Len Brown4be44fc2005-08-05 00:44:28 -0400455 status = acpi_hw_enable_all_wakeup_gpes();
456 if (ACPI_FAILURE(status)) {
457 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Len Brown4be44fc2005-08-05 00:44:28 -0400460 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Bob Mooref3d2e782007-02-02 19:48:18 +0300462 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
463 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 do {
466 acpi_os_stall(1000);
Bob Moored8c71b62007-02-02 19:48:21 +0300467 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400468 if (ACPI_FAILURE(status)) {
469 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 } while (!in_value);
472
Len Brown4be44fc2005-08-05 00:44:28 -0400473 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Bob Moore83135242006-10-03 00:00:00 -0400476ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Robert Moore44f6c012005-04-18 22:49:35 -0400478/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *
480 * FUNCTION: acpi_leave_sleep_state
481 *
482 * PARAMETERS: sleep_state - Which sleep state we just exited
483 *
484 * RETURN: Status
485 *
486 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
487 * Called with interrupts ENABLED.
488 *
489 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400490acpi_status acpi_leave_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Len Brown4be44fc2005-08-05 00:44:28 -0400492 struct acpi_object_list arg_list;
493 union acpi_object arg;
494 acpi_status status;
495 struct acpi_bit_register_info *sleep_type_reg_info;
496 struct acpi_bit_register_info *sleep_enable_reg_info;
497 u32 PM1Acontrol;
498 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Bob Mooreb229cf92006-04-21 17:15:00 -0400500 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * Set SLP_TYPE and SLP_EN to state S0.
504 * This is unclear from the ACPI Spec, but it is required
505 * by some machines.
506 */
Len Brown4be44fc2005-08-05 00:44:28 -0400507 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
508 &acpi_gbl_sleep_type_a,
509 &acpi_gbl_sleep_type_b);
510 if (ACPI_SUCCESS(status)) {
511 sleep_type_reg_info =
512 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
513 sleep_enable_reg_info =
514 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* Get current value of PM1A control */
517
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400518 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400519 &PM1Acontrol);
520 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Clear SLP_EN and SLP_TYP fields */
523
524 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400525 sleep_enable_reg_info->
526 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 PM1Bcontrol = PM1Acontrol;
528
529 /* Insert SLP_TYP bits */
530
Len Brown4be44fc2005-08-05 00:44:28 -0400531 PM1Acontrol |=
532 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
533 bit_position);
534 PM1Bcontrol |=
535 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
536 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* Just ignore any errors */
539
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400540 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400541 PM1Acontrol);
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400542 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400543 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545 }
546
547 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
548
549 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
550
551 /* Setup parameter object */
552
553 arg_list.count = 1;
554 arg_list.pointer = &arg;
555 arg.type = ACPI_TYPE_INTEGER;
556
557 /* Ignore any errors from these methods */
558
559 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400560 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
561 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500562 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 arg.integer.value = sleep_state;
Len Brown4be44fc2005-08-05 00:44:28 -0400566 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
567 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500568 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400572 * GPEs must be enabled before _WAK is called as GPEs
573 * might get fired there
574 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 * Restore the GPEs:
576 * 1) Disable/Clear all GPEs
577 * 2) Enable all runtime GPEs
578 */
Len Brown4be44fc2005-08-05 00:44:28 -0400579 status = acpi_hw_disable_all_gpes();
580 if (ACPI_FAILURE(status)) {
581 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Len Brown4be44fc2005-08-05 00:44:28 -0400583 status = acpi_hw_enable_all_runtime_gpes();
584 if (ACPI_FAILURE(status)) {
585 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400588 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
589 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
590 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
591 }
592 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
593
594 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)