blob: 89269272fd621d7e4d4f6054448617b8173422d8 [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 Moore4a90c7e2006-01-13 16:22:00 -05009 * Copyright (C) 2000 - 2006, 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
45#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <acpi/acpi.h>
47
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{
66
Len Brown4be44fc2005-08-05 00:44:28 -040067 ACPI_FUNCTION_TRACE("acpi_set_firmware_waking_vector");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /* Set the vector */
70
71 if (acpi_gbl_common_fACS.vector_width == 32) {
Len Brown4be44fc2005-08-05 00:44:28 -040072 *(ACPI_CAST_PTR
73 (u32, acpi_gbl_common_fACS.firmware_waking_vector))
74 = (u32) physical_address;
75 } else {
76 *acpi_gbl_common_fACS.firmware_waking_vector = physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
78
Len Brown4be44fc2005-08-05 00:44:28 -040079 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Robert Moore44f6c012005-04-18 22:49:35 -040082/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
84 * FUNCTION: acpi_get_firmware_waking_vector
85 *
Robert Moore44f6c012005-04-18 22:49:35 -040086 * PARAMETERS: *physical_address - Where the contents of
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * the firmware_waking_vector field of
Robert Moore44f6c012005-04-18 22:49:35 -040088 * the FACS will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
Robert Moore44f6c012005-04-18 22:49:35 -040090 * RETURN: Status, vector
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 *
Robert Moore44f6c012005-04-18 22:49:35 -040092 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 *
94 ******************************************************************************/
Robert Moore44f6c012005-04-18 22:49:35 -040095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifdef ACPI_FUTURE_USAGE
97acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040098acpi_get_firmware_waking_vector(acpi_physical_address * physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100
Len Brown4be44fc2005-08-05 00:44:28 -0400101 ACPI_FUNCTION_TRACE("acpi_get_firmware_waking_vector");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 if (!physical_address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400104 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
107 /* Get the vector */
108
109 if (acpi_gbl_common_fACS.vector_width == 32) {
110 *physical_address = (acpi_physical_address)
Len Brown4be44fc2005-08-05 00:44:28 -0400111 *
112 (ACPI_CAST_PTR
113 (u32, acpi_gbl_common_fACS.firmware_waking_vector));
114 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *physical_address =
Len Brown4be44fc2005-08-05 00:44:28 -0400116 *acpi_gbl_common_fACS.firmware_waking_vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
Len Brown4be44fc2005-08-05 00:44:28 -0400119 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121#endif
122
Robert Moore44f6c012005-04-18 22:49:35 -0400123/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
125 * FUNCTION: acpi_enter_sleep_state_prep
126 *
127 * PARAMETERS: sleep_state - Which sleep state to enter
128 *
129 * RETURN: Status
130 *
131 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
132 * This function must execute with interrupts enabled.
133 * We break sleeping into 2 stages so that OSPM can handle
134 * various OS-specific tasks between the two steps.
135 *
136 ******************************************************************************/
137
Len Brown4be44fc2005-08-05 00:44:28 -0400138acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Len Brown4be44fc2005-08-05 00:44:28 -0400140 acpi_status status;
141 struct acpi_object_list arg_list;
142 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Len Brown4be44fc2005-08-05 00:44:28 -0400144 ACPI_FUNCTION_TRACE("acpi_enter_sleep_state_prep");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
147 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
148 */
Len Brown4be44fc2005-08-05 00:44:28 -0400149 status = acpi_get_sleep_type_data(sleep_state,
150 &acpi_gbl_sleep_type_a,
151 &acpi_gbl_sleep_type_b);
152 if (ACPI_FAILURE(status)) {
153 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
156 /* Setup parameter object */
157
158 arg_list.count = 1;
159 arg_list.pointer = &arg;
160
161 arg.type = ACPI_TYPE_INTEGER;
162 arg.integer.value = sleep_state;
163
164 /* Run the _PTS and _GTS methods */
165
Len Brown4be44fc2005-08-05 00:44:28 -0400166 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
167 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
168 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
172 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
173 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 /* Setup the argument to _SST */
177
178 switch (sleep_state) {
179 case ACPI_STATE_S0:
180 arg.integer.value = ACPI_SST_WORKING;
181 break;
182
183 case ACPI_STATE_S1:
184 case ACPI_STATE_S2:
185 case ACPI_STATE_S3:
186 arg.integer.value = ACPI_SST_SLEEPING;
187 break;
188
189 case ACPI_STATE_S4:
190 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
191 break;
192
193 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400194 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196 }
197
198 /* Set the system indicators to show the desired sleep state. */
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
Len Brown4be44fc2005-08-05 00:44:28 -0400206 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Robert Moore44f6c012005-04-18 22:49:35 -0400209/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *
211 * FUNCTION: acpi_enter_sleep_state
212 *
213 * PARAMETERS: sleep_state - Which sleep state to enter
214 *
215 * RETURN: Status
216 *
217 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
218 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
219 *
220 ******************************************************************************/
221
Len Brown4be44fc2005-08-05 00:44:28 -0400222acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Len Brown4be44fc2005-08-05 00:44:28 -0400224 u32 PM1Acontrol;
225 u32 PM1Bcontrol;
226 struct acpi_bit_register_info *sleep_type_reg_info;
227 struct acpi_bit_register_info *sleep_enable_reg_info;
228 u32 in_value;
229 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Len Brown4be44fc2005-08-05 00:44:28 -0400231 ACPI_FUNCTION_TRACE("acpi_enter_sleep_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400234 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500235 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
236 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400237 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Len Brown4be44fc2005-08-05 00:44:28 -0400240 sleep_type_reg_info =
241 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
242 sleep_enable_reg_info =
243 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* Clear wake status */
246
Len Brown4be44fc2005-08-05 00:44:28 -0400247 status =
248 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
249 if (ACPI_FAILURE(status)) {
250 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 /* Clear all fixed and general purpose status bits */
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255 status = acpi_hw_clear_acpi_status(ACPI_MTX_DO_NOT_LOCK);
256 if (ACPI_FAILURE(status)) {
257 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259
260 /*
261 * 1) Disable/Clear all GPEs
262 * 2) Enable all wakeup GPEs
263 */
Len Brown4be44fc2005-08-05 00:44:28 -0400264 status = acpi_hw_disable_all_gpes();
265 if (ACPI_FAILURE(status)) {
266 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 acpi_gbl_system_awake_and_running = FALSE;
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270 status = acpi_hw_enable_all_wakeup_gpes();
271 if (ACPI_FAILURE(status)) {
272 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
275 /* Get current value of PM1A control */
276
Len Brown4be44fc2005-08-05 00:44:28 -0400277 status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
278 ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
279 if (ACPI_FAILURE(status)) {
280 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Len Brown4be44fc2005-08-05 00:44:28 -0400282 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
283 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Clear SLP_EN and SLP_TYP fields */
286
Robert Moore44f6c012005-04-18 22:49:35 -0400287 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400288 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 PM1Bcontrol = PM1Acontrol;
290
291 /* Insert SLP_TYP bits */
292
Len Brown4be44fc2005-08-05 00:44:28 -0400293 PM1Acontrol |=
294 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
295 PM1Bcontrol |=
296 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
299 * We split the writes of SLP_TYP and SLP_EN to workaround
300 * poorly implemented hardware.
301 */
302
303 /* Write #1: fill in SLP_TYP data */
304
Len Brown4be44fc2005-08-05 00:44:28 -0400305 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
306 ACPI_REGISTER_PM1A_CONTROL,
307 PM1Acontrol);
308 if (ACPI_FAILURE(status)) {
309 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311
Len Brown4be44fc2005-08-05 00:44:28 -0400312 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
313 ACPI_REGISTER_PM1B_CONTROL,
314 PM1Bcontrol);
315 if (ACPI_FAILURE(status)) {
316 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 /* Insert SLP_ENABLE bit */
320
321 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
322 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
323
324 /* Write #2: SLP_TYP + SLP_EN */
325
Len Brown4be44fc2005-08-05 00:44:28 -0400326 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Len Brown4be44fc2005-08-05 00:44:28 -0400328 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
329 ACPI_REGISTER_PM1A_CONTROL,
330 PM1Acontrol);
331 if (ACPI_FAILURE(status)) {
332 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
Len Brown4be44fc2005-08-05 00:44:28 -0400335 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
336 ACPI_REGISTER_PM1B_CONTROL,
337 PM1Bcontrol);
338 if (ACPI_FAILURE(status)) {
339 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 if (sleep_state > ACPI_STATE_S3) {
343 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400344 * We wanted to sleep > S3, but it didn't happen (by virtue of the
345 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 *
Robert Moore44f6c012005-04-18 22:49:35 -0400347 * Wait ten seconds, then try again. This is to get S4/S5 to work on
348 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *
350 * We wait so long to allow chipsets that poll this reg very slowly to
351 * still read the right value. Ideally, this block would go
352 * away entirely.
353 */
Len Brown4be44fc2005-08-05 00:44:28 -0400354 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Len Brown4be44fc2005-08-05 00:44:28 -0400356 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
357 ACPI_REGISTER_PM1_CONTROL,
358 sleep_enable_reg_info->
359 access_bit_mask);
360 if (ACPI_FAILURE(status)) {
361 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 }
364
365 /* Wait until we enter sleep state */
366
367 do {
Len Brown4be44fc2005-08-05 00:44:28 -0400368 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value,
369 ACPI_MTX_DO_NOT_LOCK);
370 if (ACPI_FAILURE(status)) {
371 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
374 /* Spin until we wake */
375
376 } while (!in_value);
377
Len Brown4be44fc2005-08-05 00:44:28 -0400378 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Len Brown4be44fc2005-08-05 00:44:28 -0400381EXPORT_SYMBOL(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Robert Moore44f6c012005-04-18 22:49:35 -0400383/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
385 * FUNCTION: acpi_enter_sleep_state_s4bios
386 *
387 * PARAMETERS: None
388 *
389 * RETURN: Status
390 *
391 * DESCRIPTION: Perform a S4 bios request.
392 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
393 *
394 ******************************************************************************/
395
Len Brown4be44fc2005-08-05 00:44:28 -0400396acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Len Brown4be44fc2005-08-05 00:44:28 -0400398 u32 in_value;
399 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Len Brown4be44fc2005-08-05 00:44:28 -0400401 ACPI_FUNCTION_TRACE("acpi_enter_sleep_state_s4bios");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Len Brown4be44fc2005-08-05 00:44:28 -0400403 status =
404 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
405 if (ACPI_FAILURE(status)) {
406 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Len Brown4be44fc2005-08-05 00:44:28 -0400409 status = acpi_hw_clear_acpi_status(ACPI_MTX_DO_NOT_LOCK);
410 if (ACPI_FAILURE(status)) {
411 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
414 /*
415 * 1) Disable/Clear all GPEs
416 * 2) Enable all wakeup GPEs
417 */
Len Brown4be44fc2005-08-05 00:44:28 -0400418 status = acpi_hw_disable_all_gpes();
419 if (ACPI_FAILURE(status)) {
420 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 acpi_gbl_system_awake_and_running = FALSE;
423
Len Brown4be44fc2005-08-05 00:44:28 -0400424 status = acpi_hw_enable_all_wakeup_gpes();
425 if (ACPI_FAILURE(status)) {
426 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Len Brown4be44fc2005-08-05 00:44:28 -0400429 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Len Brown4be44fc2005-08-05 00:44:28 -0400431 status = acpi_os_write_port(acpi_gbl_FADT->smi_cmd,
432 (u32) acpi_gbl_FADT->S4bios_req, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 do {
435 acpi_os_stall(1000);
Len Brown4be44fc2005-08-05 00:44:28 -0400436 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value,
437 ACPI_MTX_DO_NOT_LOCK);
438 if (ACPI_FAILURE(status)) {
439 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 } while (!in_value);
442
Len Brown4be44fc2005-08-05 00:44:28 -0400443 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Len Brown4be44fc2005-08-05 00:44:28 -0400446EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Robert Moore44f6c012005-04-18 22:49:35 -0400448/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *
450 * FUNCTION: acpi_leave_sleep_state
451 *
452 * PARAMETERS: sleep_state - Which sleep state we just exited
453 *
454 * RETURN: Status
455 *
456 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
457 * Called with interrupts ENABLED.
458 *
459 ******************************************************************************/
460
Len Brown4be44fc2005-08-05 00:44:28 -0400461acpi_status acpi_leave_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Len Brown4be44fc2005-08-05 00:44:28 -0400463 struct acpi_object_list arg_list;
464 union acpi_object arg;
465 acpi_status status;
466 struct acpi_bit_register_info *sleep_type_reg_info;
467 struct acpi_bit_register_info *sleep_enable_reg_info;
468 u32 PM1Acontrol;
469 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Len Brown4be44fc2005-08-05 00:44:28 -0400471 ACPI_FUNCTION_TRACE("acpi_leave_sleep_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * Set SLP_TYPE and SLP_EN to state S0.
475 * This is unclear from the ACPI Spec, but it is required
476 * by some machines.
477 */
Len Brown4be44fc2005-08-05 00:44:28 -0400478 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
479 &acpi_gbl_sleep_type_a,
480 &acpi_gbl_sleep_type_b);
481 if (ACPI_SUCCESS(status)) {
482 sleep_type_reg_info =
483 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
484 sleep_enable_reg_info =
485 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* Get current value of PM1A control */
488
Len Brown4be44fc2005-08-05 00:44:28 -0400489 status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
490 ACPI_REGISTER_PM1_CONTROL,
491 &PM1Acontrol);
492 if (ACPI_SUCCESS(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Clear SLP_EN and SLP_TYP fields */
494
495 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400496 sleep_enable_reg_info->
497 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 PM1Bcontrol = PM1Acontrol;
499
500 /* Insert SLP_TYP bits */
501
Len Brown4be44fc2005-08-05 00:44:28 -0400502 PM1Acontrol |=
503 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
504 bit_position);
505 PM1Bcontrol |=
506 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
507 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* Just ignore any errors */
510
Len Brown4be44fc2005-08-05 00:44:28 -0400511 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
512 ACPI_REGISTER_PM1A_CONTROL,
513 PM1Acontrol);
514 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
515 ACPI_REGISTER_PM1B_CONTROL,
516 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 }
519
520 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
521
522 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
523
524 /* Setup parameter object */
525
526 arg_list.count = 1;
527 arg_list.pointer = &arg;
528 arg.type = ACPI_TYPE_INTEGER;
529
530 /* Ignore any errors from these methods */
531
532 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400533 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
534 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500535 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 arg.integer.value = sleep_state;
Len Brown4be44fc2005-08-05 00:44:28 -0400539 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
540 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500541 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Len Brown4be44fc2005-08-05 00:44:28 -0400544 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
545 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500546 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
549
550 /*
551 * Restore the GPEs:
552 * 1) Disable/Clear all GPEs
553 * 2) Enable all runtime GPEs
554 */
Len Brown4be44fc2005-08-05 00:44:28 -0400555 status = acpi_hw_disable_all_gpes();
556 if (ACPI_FAILURE(status)) {
557 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 acpi_gbl_system_awake_and_running = TRUE;
560
Len Brown4be44fc2005-08-05 00:44:28 -0400561 status = acpi_hw_enable_all_runtime_gpes();
562 if (ACPI_FAILURE(status)) {
563 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 /* Enable power button */
567
Len Brown4be44fc2005-08-05 00:44:28 -0400568 (void)
569 acpi_set_register(acpi_gbl_fixed_event_info
570 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1,
571 ACPI_MTX_DO_NOT_LOCK);
Robert Moore44f6c012005-04-18 22:49:35 -0400572
Len Brown4be44fc2005-08-05 00:44:28 -0400573 (void)
574 acpi_set_register(acpi_gbl_fixed_event_info
575 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1,
576 ACPI_MTX_DO_NOT_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400579 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
580 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500581 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Len Brown4be44fc2005-08-05 00:44:28 -0400584 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}