2 /******************************************************************************
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2008, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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.
45 #include <acpi/acpi.h>
49 #define _COMPONENT ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwsleep")
52 /*******************************************************************************
54 * FUNCTION: acpi_set_firmware_waking_vector
56 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
61 * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
63 ******************************************************************************/
65 acpi_set_firmware_waking_vector(u32 physical_address)
67 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
71 * According to the ACPI specification 2.0c and later, the 64-bit
72 * waking vector should be cleared and the 32-bit waking vector should
73 * be used, unless we want the wake-up code to be called by the BIOS in
74 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
75 * to fail to resume if the 64-bit vector is used.
78 /* Set the 32-bit vector */
80 acpi_gbl_FACS->firmware_waking_vector = physical_address;
82 /* Clear the 64-bit vector if it exists */
84 if ((acpi_gbl_FACS->length > 32) && (acpi_gbl_FACS->version >= 1)) {
85 acpi_gbl_FACS->xfirmware_waking_vector = 0;
88 return_ACPI_STATUS(AE_OK);
91 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
93 #if ACPI_MACHINE_WIDTH == 64
94 /*******************************************************************************
96 * FUNCTION: acpi_set_firmware_waking_vector64
98 * PARAMETERS: physical_address - 64-bit physical address of ACPI protected
103 * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
104 * it exists in the table. This function is intended for use with
105 * 64-bit host operating systems.
107 ******************************************************************************/
109 acpi_set_firmware_waking_vector64(u64 physical_address)
111 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64);
114 /* Determine if the 64-bit vector actually exists */
116 if ((acpi_gbl_FACS->length <= 32) || (acpi_gbl_FACS->version < 1)) {
117 return_ACPI_STATUS(AE_NOT_EXIST);
120 /* Clear 32-bit vector, set the 64-bit X_ vector */
122 acpi_gbl_FACS->firmware_waking_vector = 0;
123 acpi_gbl_FACS->xfirmware_waking_vector = physical_address;
125 return_ACPI_STATUS(AE_OK);
128 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64)
131 /*******************************************************************************
133 * FUNCTION: acpi_enter_sleep_state_prep
135 * PARAMETERS: sleep_state - Which sleep state to enter
139 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
140 * This function must execute with interrupts enabled.
141 * We break sleeping into 2 stages so that OSPM can handle
142 * various OS-specific tasks between the two steps.
144 ******************************************************************************/
145 acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
148 struct acpi_object_list arg_list;
149 union acpi_object arg;
151 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
153 /* _PSW methods could be run here to enable wake-on keyboard, LAN, etc. */
155 status = acpi_get_sleep_type_data(sleep_state,
156 &acpi_gbl_sleep_type_a,
157 &acpi_gbl_sleep_type_b);
158 if (ACPI_FAILURE(status)) {
159 return_ACPI_STATUS(status);
162 /* Setup parameter object */
165 arg_list.pointer = &arg;
167 arg.type = ACPI_TYPE_INTEGER;
168 arg.integer.value = sleep_state;
170 /* Run the _PTS method */
172 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
173 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
174 return_ACPI_STATUS(status);
177 /* Setup the argument to _SST */
179 switch (sleep_state) {
181 arg.integer.value = ACPI_SST_WORKING;
187 arg.integer.value = ACPI_SST_SLEEPING;
191 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
195 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
200 * Set the system indicators to show the desired sleep state.
201 * _SST is an optional method (return no error if not found)
203 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
204 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
205 ACPI_EXCEPTION((AE_INFO, status,
206 "While executing method _SST"));
209 return_ACPI_STATUS(AE_OK);
212 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
214 static unsigned int gts, bfs;
215 module_param(gts, uint, 0644);
216 module_param(bfs, uint, 0644);
217 MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend.");
218 MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".);
220 /*******************************************************************************
222 * FUNCTION: acpi_enter_sleep_state
224 * PARAMETERS: sleep_state - Which sleep state to enter
228 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
229 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
231 ******************************************************************************/
232 acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
236 struct acpi_bit_register_info *sleep_type_reg_info;
237 struct acpi_bit_register_info *sleep_enable_reg_info;
239 struct acpi_object_list arg_list;
240 union acpi_object arg;
243 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
245 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
246 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
247 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
248 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
249 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
252 sleep_type_reg_info =
253 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
254 sleep_enable_reg_info =
255 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
257 /* Clear wake status */
260 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
261 if (ACPI_FAILURE(status)) {
262 return_ACPI_STATUS(status);
265 /* Clear all fixed and general purpose status bits */
267 status = acpi_hw_clear_acpi_status();
268 if (ACPI_FAILURE(status)) {
269 return_ACPI_STATUS(status);
273 * 1) Disable/Clear all GPEs
274 * 2) Enable all wakeup GPEs
276 status = acpi_hw_disable_all_gpes();
277 if (ACPI_FAILURE(status)) {
278 return_ACPI_STATUS(status);
280 acpi_gbl_system_awake_and_running = FALSE;
282 status = acpi_hw_enable_all_wakeup_gpes();
283 if (ACPI_FAILURE(status)) {
284 return_ACPI_STATUS(status);
288 /* Execute the _GTS method */
291 arg_list.pointer = &arg;
292 arg.type = ACPI_TYPE_INTEGER;
293 arg.integer.value = sleep_state;
295 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
296 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
297 return_ACPI_STATUS(status);
301 /* Get current value of PM1A control */
303 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
305 if (ACPI_FAILURE(status)) {
306 return_ACPI_STATUS(status);
308 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
309 "Entering sleep state [S%d]\n", sleep_state));
311 /* Clear the SLP_EN and SLP_TYP fields */
313 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
314 sleep_enable_reg_info->access_bit_mask);
315 pm1b_control = pm1a_control;
317 /* Insert the SLP_TYP bits */
320 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
322 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
325 * We split the writes of SLP_TYP and SLP_EN to workaround
326 * poorly implemented hardware.
329 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
331 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
332 if (ACPI_FAILURE(status)) {
333 return_ACPI_STATUS(status);
336 /* Insert the sleep enable (SLP_EN) bit */
338 pm1a_control |= sleep_enable_reg_info->access_bit_mask;
339 pm1b_control |= sleep_enable_reg_info->access_bit_mask;
341 /* Flush caches, as per ACPI specification */
343 ACPI_FLUSH_CPU_CACHE();
345 /* Write #2: Write both SLP_TYP + SLP_EN */
347 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
348 if (ACPI_FAILURE(status)) {
349 return_ACPI_STATUS(status);
352 if (sleep_state > ACPI_STATE_S3) {
354 * We wanted to sleep > S3, but it didn't happen (by virtue of the
355 * fact that we are still executing!)
357 * Wait ten seconds, then try again. This is to get S4/S5 to work on
360 * We wait so long to allow chipsets that poll this reg very slowly
361 * to still read the right value. Ideally, this block would go
364 acpi_os_stall(10000000);
366 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
367 sleep_enable_reg_info->
369 if (ACPI_FAILURE(status)) {
370 return_ACPI_STATUS(status);
374 /* Wait until we enter sleep state */
377 status = acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS,
379 if (ACPI_FAILURE(status)) {
380 return_ACPI_STATUS(status);
383 /* Spin until we wake */
387 return_ACPI_STATUS(AE_OK);
390 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
392 /*******************************************************************************
394 * FUNCTION: acpi_enter_sleep_state_s4bios
400 * DESCRIPTION: Perform a S4 bios request.
401 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
403 ******************************************************************************/
404 acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
409 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
411 /* Clear the wake status bit (PM1) */
414 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
415 if (ACPI_FAILURE(status)) {
416 return_ACPI_STATUS(status);
419 status = acpi_hw_clear_acpi_status();
420 if (ACPI_FAILURE(status)) {
421 return_ACPI_STATUS(status);
425 * 1) Disable/Clear all GPEs
426 * 2) Enable all wakeup GPEs
428 status = acpi_hw_disable_all_gpes();
429 if (ACPI_FAILURE(status)) {
430 return_ACPI_STATUS(status);
432 acpi_gbl_system_awake_and_running = FALSE;
434 status = acpi_hw_enable_all_wakeup_gpes();
435 if (ACPI_FAILURE(status)) {
436 return_ACPI_STATUS(status);
439 ACPI_FLUSH_CPU_CACHE();
441 status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
442 (u32) acpi_gbl_FADT.S4bios_request, 8);
447 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
448 if (ACPI_FAILURE(status)) {
449 return_ACPI_STATUS(status);
453 return_ACPI_STATUS(AE_OK);
456 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
458 /*******************************************************************************
460 * FUNCTION: acpi_leave_sleep_state_prep
462 * PARAMETERS: sleep_state - Which sleep state we are exiting
466 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
468 * Called with interrupts DISABLED.
470 ******************************************************************************/
471 acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
473 struct acpi_object_list arg_list;
474 union acpi_object arg;
476 struct acpi_bit_register_info *sleep_type_reg_info;
477 struct acpi_bit_register_info *sleep_enable_reg_info;
481 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
484 * Set SLP_TYPE and SLP_EN to state S0.
485 * This is unclear from the ACPI Spec, but it is required
488 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
489 &acpi_gbl_sleep_type_a,
490 &acpi_gbl_sleep_type_b);
491 if (ACPI_SUCCESS(status)) {
492 sleep_type_reg_info =
493 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
494 sleep_enable_reg_info =
495 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
497 /* Get current value of PM1A control */
499 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
501 if (ACPI_SUCCESS(status)) {
503 /* Clear the SLP_EN and SLP_TYP fields */
505 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
506 sleep_enable_reg_info->
508 pm1b_control = pm1a_control;
510 /* Insert the SLP_TYP bits */
512 pm1a_control |= (acpi_gbl_sleep_type_a <<
513 sleep_type_reg_info->bit_position);
514 pm1b_control |= (acpi_gbl_sleep_type_b <<
515 sleep_type_reg_info->bit_position);
517 /* Write the control registers and ignore any errors */
519 (void)acpi_hw_write_pm1_control(pm1a_control,
525 /* Execute the _BFS method */
528 arg_list.pointer = &arg;
529 arg.type = ACPI_TYPE_INTEGER;
530 arg.integer.value = sleep_state;
532 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
533 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
534 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
537 return_ACPI_STATUS(status);
540 /*******************************************************************************
542 * FUNCTION: acpi_leave_sleep_state
544 * PARAMETERS: sleep_state - Which sleep state we just exited
548 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
549 * Called with interrupts ENABLED.
551 ******************************************************************************/
552 acpi_status acpi_leave_sleep_state(u8 sleep_state)
554 struct acpi_object_list arg_list;
555 union acpi_object arg;
558 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
560 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
562 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
564 /* Setup parameter object */
567 arg_list.pointer = &arg;
568 arg.type = ACPI_TYPE_INTEGER;
570 /* Ignore any errors from these methods */
572 arg.integer.value = ACPI_SST_WAKING;
573 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
574 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
575 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
579 * GPEs must be enabled before _WAK is called as GPEs
580 * might get fired there
583 * 1) Disable/Clear all GPEs
584 * 2) Enable all runtime GPEs
586 status = acpi_hw_disable_all_gpes();
587 if (ACPI_FAILURE(status)) {
588 return_ACPI_STATUS(status);
590 status = acpi_hw_enable_all_runtime_gpes();
591 if (ACPI_FAILURE(status)) {
592 return_ACPI_STATUS(status);
595 arg.integer.value = sleep_state;
596 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
597 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
598 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
600 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
603 * Some BIOSes assume that WAK_STS will be cleared on resume and use
604 * it to determine whether the system is rebooting or resuming. Clear
605 * it for compatibility.
607 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, 1);
609 acpi_gbl_system_awake_and_running = TRUE;
611 /* Enable power button */
614 acpi_write_bit_register(acpi_gbl_fixed_event_info
615 [ACPI_EVENT_POWER_BUTTON].
616 enable_register_id, ACPI_ENABLE_EVENT);
619 acpi_write_bit_register(acpi_gbl_fixed_event_info
620 [ACPI_EVENT_POWER_BUTTON].
621 status_register_id, ACPI_CLEAR_STATUS);
623 arg.integer.value = ACPI_SST_WORKING;
624 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
625 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
626 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
629 return_ACPI_STATUS(status);
632 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)