blob: 34519069050c2da488fa445759398220f3ecf897 [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/*
9 * Copyright (C) 2000 - 2005, R. Byron Moore
10 * 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) {
202 ACPI_REPORT_ERROR(("Method _SST failed, %s\n",
203 acpi_format_exception(status)));
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)) {
235 ACPI_REPORT_ERROR(("Sleep values out of range: A=%X B=%X\n",
236 acpi_gbl_sleep_type_a,
237 acpi_gbl_sleep_type_b));
238 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241 sleep_type_reg_info =
242 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
243 sleep_enable_reg_info =
244 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Clear wake status */
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 status =
249 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
250 if (ACPI_FAILURE(status)) {
251 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 /* Clear all fixed and general purpose status bits */
255
Len Brown4be44fc2005-08-05 00:44:28 -0400256 status = acpi_hw_clear_acpi_status(ACPI_MTX_DO_NOT_LOCK);
257 if (ACPI_FAILURE(status)) {
258 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
261 /*
262 * 1) Disable/Clear all GPEs
263 * 2) Enable all wakeup GPEs
264 */
Len Brown4be44fc2005-08-05 00:44:28 -0400265 status = acpi_hw_disable_all_gpes();
266 if (ACPI_FAILURE(status)) {
267 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 acpi_gbl_system_awake_and_running = FALSE;
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 status = acpi_hw_enable_all_wakeup_gpes();
272 if (ACPI_FAILURE(status)) {
273 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
276 /* Get current value of PM1A control */
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278 status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
279 ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
280 if (ACPI_FAILURE(status)) {
281 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Len Brown4be44fc2005-08-05 00:44:28 -0400283 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
284 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* Clear SLP_EN and SLP_TYP fields */
287
Robert Moore44f6c012005-04-18 22:49:35 -0400288 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400289 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 PM1Bcontrol = PM1Acontrol;
291
292 /* Insert SLP_TYP bits */
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294 PM1Acontrol |=
295 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
296 PM1Bcontrol |=
297 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /*
300 * We split the writes of SLP_TYP and SLP_EN to workaround
301 * poorly implemented hardware.
302 */
303
304 /* Write #1: fill in SLP_TYP data */
305
Len Brown4be44fc2005-08-05 00:44:28 -0400306 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
307 ACPI_REGISTER_PM1A_CONTROL,
308 PM1Acontrol);
309 if (ACPI_FAILURE(status)) {
310 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
314 ACPI_REGISTER_PM1B_CONTROL,
315 PM1Bcontrol);
316 if (ACPI_FAILURE(status)) {
317 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
320 /* Insert SLP_ENABLE bit */
321
322 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
323 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
324
325 /* Write #2: SLP_TYP + SLP_EN */
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Len Brown4be44fc2005-08-05 00:44:28 -0400329 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
330 ACPI_REGISTER_PM1A_CONTROL,
331 PM1Acontrol);
332 if (ACPI_FAILURE(status)) {
333 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Len Brown4be44fc2005-08-05 00:44:28 -0400336 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
337 ACPI_REGISTER_PM1B_CONTROL,
338 PM1Bcontrol);
339 if (ACPI_FAILURE(status)) {
340 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 if (sleep_state > ACPI_STATE_S3) {
344 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400345 * We wanted to sleep > S3, but it didn't happen (by virtue of the
346 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
Robert Moore44f6c012005-04-18 22:49:35 -0400348 * Wait ten seconds, then try again. This is to get S4/S5 to work on
349 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 *
351 * We wait so long to allow chipsets that poll this reg very slowly to
352 * still read the right value. Ideally, this block would go
353 * away entirely.
354 */
Len Brown4be44fc2005-08-05 00:44:28 -0400355 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Len Brown4be44fc2005-08-05 00:44:28 -0400357 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
358 ACPI_REGISTER_PM1_CONTROL,
359 sleep_enable_reg_info->
360 access_bit_mask);
361 if (ACPI_FAILURE(status)) {
362 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 }
365
366 /* Wait until we enter sleep state */
367
368 do {
Len Brown4be44fc2005-08-05 00:44:28 -0400369 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value,
370 ACPI_MTX_DO_NOT_LOCK);
371 if (ACPI_FAILURE(status)) {
372 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 /* Spin until we wake */
376
377 } while (!in_value);
378
Len Brown4be44fc2005-08-05 00:44:28 -0400379 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Len Brown4be44fc2005-08-05 00:44:28 -0400382EXPORT_SYMBOL(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Robert Moore44f6c012005-04-18 22:49:35 -0400384/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
386 * FUNCTION: acpi_enter_sleep_state_s4bios
387 *
388 * PARAMETERS: None
389 *
390 * RETURN: Status
391 *
392 * DESCRIPTION: Perform a S4 bios request.
393 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
394 *
395 ******************************************************************************/
396
Len Brown4be44fc2005-08-05 00:44:28 -0400397acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Len Brown4be44fc2005-08-05 00:44:28 -0400399 u32 in_value;
400 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Len Brown4be44fc2005-08-05 00:44:28 -0400402 ACPI_FUNCTION_TRACE("acpi_enter_sleep_state_s4bios");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Len Brown4be44fc2005-08-05 00:44:28 -0400404 status =
405 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
406 if (ACPI_FAILURE(status)) {
407 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Len Brown4be44fc2005-08-05 00:44:28 -0400410 status = acpi_hw_clear_acpi_status(ACPI_MTX_DO_NOT_LOCK);
411 if (ACPI_FAILURE(status)) {
412 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 /*
416 * 1) Disable/Clear all GPEs
417 * 2) Enable all wakeup GPEs
418 */
Len Brown4be44fc2005-08-05 00:44:28 -0400419 status = acpi_hw_disable_all_gpes();
420 if (ACPI_FAILURE(status)) {
421 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 acpi_gbl_system_awake_and_running = FALSE;
424
Len Brown4be44fc2005-08-05 00:44:28 -0400425 status = acpi_hw_enable_all_wakeup_gpes();
426 if (ACPI_FAILURE(status)) {
427 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Len Brown4be44fc2005-08-05 00:44:28 -0400430 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Len Brown4be44fc2005-08-05 00:44:28 -0400432 status = acpi_os_write_port(acpi_gbl_FADT->smi_cmd,
433 (u32) acpi_gbl_FADT->S4bios_req, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 do {
436 acpi_os_stall(1000);
Len Brown4be44fc2005-08-05 00:44:28 -0400437 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value,
438 ACPI_MTX_DO_NOT_LOCK);
439 if (ACPI_FAILURE(status)) {
440 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442 } while (!in_value);
443
Len Brown4be44fc2005-08-05 00:44:28 -0400444 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Len Brown4be44fc2005-08-05 00:44:28 -0400447EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Robert Moore44f6c012005-04-18 22:49:35 -0400449/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
451 * FUNCTION: acpi_leave_sleep_state
452 *
453 * PARAMETERS: sleep_state - Which sleep state we just exited
454 *
455 * RETURN: Status
456 *
457 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
458 * Called with interrupts ENABLED.
459 *
460 ******************************************************************************/
461
Len Brown4be44fc2005-08-05 00:44:28 -0400462acpi_status acpi_leave_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Len Brown4be44fc2005-08-05 00:44:28 -0400464 struct acpi_object_list arg_list;
465 union acpi_object arg;
466 acpi_status status;
467 struct acpi_bit_register_info *sleep_type_reg_info;
468 struct acpi_bit_register_info *sleep_enable_reg_info;
469 u32 PM1Acontrol;
470 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Len Brown4be44fc2005-08-05 00:44:28 -0400472 ACPI_FUNCTION_TRACE("acpi_leave_sleep_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /*
475 * Set SLP_TYPE and SLP_EN to state S0.
476 * This is unclear from the ACPI Spec, but it is required
477 * by some machines.
478 */
Len Brown4be44fc2005-08-05 00:44:28 -0400479 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
480 &acpi_gbl_sleep_type_a,
481 &acpi_gbl_sleep_type_b);
482 if (ACPI_SUCCESS(status)) {
483 sleep_type_reg_info =
484 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
485 sleep_enable_reg_info =
486 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* Get current value of PM1A control */
489
Len Brown4be44fc2005-08-05 00:44:28 -0400490 status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
491 ACPI_REGISTER_PM1_CONTROL,
492 &PM1Acontrol);
493 if (ACPI_SUCCESS(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Clear SLP_EN and SLP_TYP fields */
495
496 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400497 sleep_enable_reg_info->
498 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 PM1Bcontrol = PM1Acontrol;
500
501 /* Insert SLP_TYP bits */
502
Len Brown4be44fc2005-08-05 00:44:28 -0400503 PM1Acontrol |=
504 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
505 bit_position);
506 PM1Bcontrol |=
507 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
508 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* Just ignore any errors */
511
Len Brown4be44fc2005-08-05 00:44:28 -0400512 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
513 ACPI_REGISTER_PM1A_CONTROL,
514 PM1Acontrol);
515 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
516 ACPI_REGISTER_PM1B_CONTROL,
517 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 }
520
521 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
522
523 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
524
525 /* Setup parameter object */
526
527 arg_list.count = 1;
528 arg_list.pointer = &arg;
529 arg.type = ACPI_TYPE_INTEGER;
530
531 /* Ignore any errors from these methods */
532
533 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400534 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
535 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
536 ACPI_REPORT_ERROR(("Method _SST failed, %s\n",
537 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 arg.integer.value = sleep_state;
Len Brown4be44fc2005-08-05 00:44:28 -0400541 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
542 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
543 ACPI_REPORT_ERROR(("Method _BFS failed, %s\n",
544 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Len Brown4be44fc2005-08-05 00:44:28 -0400547 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
548 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
549 ACPI_REPORT_ERROR(("Method _WAK failed, %s\n",
550 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
553
554 /*
555 * Restore the GPEs:
556 * 1) Disable/Clear all GPEs
557 * 2) Enable all runtime GPEs
558 */
Len Brown4be44fc2005-08-05 00:44:28 -0400559 status = acpi_hw_disable_all_gpes();
560 if (ACPI_FAILURE(status)) {
561 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 acpi_gbl_system_awake_and_running = TRUE;
564
Len Brown4be44fc2005-08-05 00:44:28 -0400565 status = acpi_hw_enable_all_runtime_gpes();
566 if (ACPI_FAILURE(status)) {
567 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 /* Enable power button */
571
Len Brown4be44fc2005-08-05 00:44:28 -0400572 (void)
573 acpi_set_register(acpi_gbl_fixed_event_info
574 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1,
575 ACPI_MTX_DO_NOT_LOCK);
Robert Moore44f6c012005-04-18 22:49:35 -0400576
Len Brown4be44fc2005-08-05 00:44:28 -0400577 (void)
578 acpi_set_register(acpi_gbl_fixed_event_info
579 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1,
580 ACPI_MTX_DO_NOT_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400583 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
584 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
585 ACPI_REPORT_ERROR(("Method _SST failed, %s\n",
586 acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Len Brown4be44fc2005-08-05 00:44:28 -0400589 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}