2 /******************************************************************************
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2010, 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>
48 #include <linux/tboot.h>
50 #define _COMPONENT ACPI_HARDWARE
51 ACPI_MODULE_NAME("hwsleep")
53 /*******************************************************************************
55 * FUNCTION: acpi_set_firmware_waking_vector
57 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
62 * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
64 ******************************************************************************/
66 acpi_set_firmware_waking_vector(u32 physical_address
)
68 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector
);
72 * According to the ACPI specification 2.0c and later, the 64-bit
73 * waking vector should be cleared and the 32-bit waking vector should
74 * be used, unless we want the wake-up code to be called by the BIOS in
75 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
76 * to fail to resume if the 64-bit vector is used.
79 /* Set the 32-bit vector */
81 acpi_gbl_FACS
->firmware_waking_vector
= physical_address
;
83 /* Clear the 64-bit vector if it exists */
85 if ((acpi_gbl_FACS
->length
> 32) && (acpi_gbl_FACS
->version
>= 1)) {
86 acpi_gbl_FACS
->xfirmware_waking_vector
= 0;
89 return_ACPI_STATUS(AE_OK
);
92 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector
)
94 #if ACPI_MACHINE_WIDTH == 64
95 /*******************************************************************************
97 * FUNCTION: acpi_set_firmware_waking_vector64
99 * PARAMETERS: physical_address - 64-bit physical address of ACPI protected
104 * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
105 * it exists in the table. This function is intended for use with
106 * 64-bit host operating systems.
108 ******************************************************************************/
110 acpi_set_firmware_waking_vector64(u64 physical_address
)
112 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64
);
115 /* Determine if the 64-bit vector actually exists */
117 if ((acpi_gbl_FACS
->length
<= 32) || (acpi_gbl_FACS
->version
< 1)) {
118 return_ACPI_STATUS(AE_NOT_EXIST
);
121 /* Clear 32-bit vector, set the 64-bit X_ vector */
123 acpi_gbl_FACS
->firmware_waking_vector
= 0;
124 acpi_gbl_FACS
->xfirmware_waking_vector
= physical_address
;
126 return_ACPI_STATUS(AE_OK
);
129 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64
)
132 /*******************************************************************************
134 * FUNCTION: acpi_enter_sleep_state_prep
136 * PARAMETERS: sleep_state - Which sleep state to enter
140 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
141 * This function must execute with interrupts enabled.
142 * We break sleeping into 2 stages so that OSPM can handle
143 * various OS-specific tasks between the two steps.
145 ******************************************************************************/
146 acpi_status
acpi_enter_sleep_state_prep(u8 sleep_state
)
149 struct acpi_object_list arg_list
;
150 union acpi_object arg
;
152 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep
);
154 /* _PSW methods could be run here to enable wake-on keyboard, LAN, etc. */
156 status
= acpi_get_sleep_type_data(sleep_state
,
157 &acpi_gbl_sleep_type_a
,
158 &acpi_gbl_sleep_type_b
);
159 if (ACPI_FAILURE(status
)) {
160 return_ACPI_STATUS(status
);
163 /* Setup parameter object */
166 arg_list
.pointer
= &arg
;
168 arg
.type
= ACPI_TYPE_INTEGER
;
169 arg
.integer
.value
= sleep_state
;
171 /* Run the _PTS method */
173 status
= acpi_evaluate_object(NULL
, METHOD_NAME__PTS
, &arg_list
, NULL
);
174 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
175 return_ACPI_STATUS(status
);
178 /* Setup the argument to _SST */
180 switch (sleep_state
) {
182 arg
.integer
.value
= ACPI_SST_WORKING
;
188 arg
.integer
.value
= ACPI_SST_SLEEPING
;
192 arg
.integer
.value
= ACPI_SST_SLEEP_CONTEXT
;
196 arg
.integer
.value
= ACPI_SST_INDICATOR_OFF
; /* Default is off */
201 * Set the system indicators to show the desired sleep state.
202 * _SST is an optional method (return no error if not found)
204 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
205 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
206 ACPI_EXCEPTION((AE_INFO
, status
,
207 "While executing method _SST"));
210 return_ACPI_STATUS(AE_OK
);
213 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep
)
215 static unsigned int gts
, bfs
;
216 module_param(gts
, uint
, 0644);
217 module_param(bfs
, uint
, 0644);
218 MODULE_PARM_DESC(gts
, "Enable evaluation of _GTS on suspend.");
219 MODULE_PARM_DESC(bfs
, "Enable evaluation of _BFS on resume".);
221 /*******************************************************************************
223 * FUNCTION: acpi_enter_sleep_state
225 * PARAMETERS: sleep_state - Which sleep state to enter
229 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
230 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
232 ******************************************************************************/
233 acpi_status asmlinkage
acpi_enter_sleep_state(u8 sleep_state
)
237 struct acpi_bit_register_info
*sleep_type_reg_info
;
238 struct acpi_bit_register_info
*sleep_enable_reg_info
;
240 struct acpi_object_list arg_list
;
241 union acpi_object arg
;
244 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state
);
246 if ((acpi_gbl_sleep_type_a
> ACPI_SLEEP_TYPE_MAX
) ||
247 (acpi_gbl_sleep_type_b
> ACPI_SLEEP_TYPE_MAX
)) {
248 ACPI_ERROR((AE_INFO
, "Sleep values out of range: A=0x%X B=0x%X",
249 acpi_gbl_sleep_type_a
, acpi_gbl_sleep_type_b
));
250 return_ACPI_STATUS(AE_AML_OPERAND_VALUE
);
253 sleep_type_reg_info
=
254 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE
);
255 sleep_enable_reg_info
=
256 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
258 /* Clear wake status */
261 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS
, ACPI_CLEAR_STATUS
);
262 if (ACPI_FAILURE(status
)) {
263 return_ACPI_STATUS(status
);
266 /* Clear all fixed and general purpose status bits */
268 status
= acpi_hw_clear_acpi_status();
269 if (ACPI_FAILURE(status
)) {
270 return_ACPI_STATUS(status
);
274 * 1) Disable/Clear all GPEs
275 * 2) Enable all wakeup GPEs
277 status
= acpi_hw_disable_all_gpes();
278 if (ACPI_FAILURE(status
)) {
279 return_ACPI_STATUS(status
);
281 acpi_gbl_system_awake_and_running
= FALSE
;
283 status
= acpi_hw_enable_all_wakeup_gpes();
284 if (ACPI_FAILURE(status
)) {
285 return_ACPI_STATUS(status
);
289 /* Execute the _GTS method */
292 arg_list
.pointer
= &arg
;
293 arg
.type
= ACPI_TYPE_INTEGER
;
294 arg
.integer
.value
= sleep_state
;
296 status
= acpi_evaluate_object(NULL
, METHOD_NAME__GTS
, &arg_list
, NULL
);
297 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
298 return_ACPI_STATUS(status
);
302 /* Get current value of PM1A control */
304 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
306 if (ACPI_FAILURE(status
)) {
307 return_ACPI_STATUS(status
);
309 ACPI_DEBUG_PRINT((ACPI_DB_INIT
,
310 "Entering sleep state [S%u]\n", sleep_state
));
312 /* Clear the SLP_EN and SLP_TYP fields */
314 pm1a_control
&= ~(sleep_type_reg_info
->access_bit_mask
|
315 sleep_enable_reg_info
->access_bit_mask
);
316 pm1b_control
= pm1a_control
;
318 /* Insert the SLP_TYP bits */
321 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
323 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
326 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
328 status
= acpi_hw_write_pm1_control(pm1a_control
, pm1b_control
);
329 if (ACPI_FAILURE(status
)) {
330 return_ACPI_STATUS(status
);
333 /* Insert the sleep enable (SLP_EN) bit */
335 pm1a_control
|= sleep_enable_reg_info
->access_bit_mask
;
336 pm1b_control
|= sleep_enable_reg_info
->access_bit_mask
;
338 /* Flush caches, as per ACPI specification */
340 ACPI_FLUSH_CPU_CACHE();
342 tboot_sleep(sleep_state
, pm1a_control
, pm1b_control
);
344 /* Write #2: Write both SLP_TYP + SLP_EN */
346 status
= acpi_hw_write_pm1_control(pm1a_control
, pm1b_control
);
347 if (ACPI_FAILURE(status
)) {
348 return_ACPI_STATUS(status
);
351 if (sleep_state
> ACPI_STATE_S3
) {
353 * We wanted to sleep > S3, but it didn't happen (by virtue of the
354 * fact that we are still executing!)
356 * Wait ten seconds, then try again. This is to get S4/S5 to work on
359 * We wait so long to allow chipsets that poll this reg very slowly
360 * to still read the right value. Ideally, this block would go
363 acpi_os_stall(10000000);
365 status
= acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL
,
366 sleep_enable_reg_info
->
368 if (ACPI_FAILURE(status
)) {
369 return_ACPI_STATUS(status
);
373 /* Wait until we enter sleep state */
376 status
= acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS
,
378 if (ACPI_FAILURE(status
)) {
379 return_ACPI_STATUS(status
);
382 /* Spin until we wake */
386 return_ACPI_STATUS(AE_OK
);
389 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state
)
391 /*******************************************************************************
393 * FUNCTION: acpi_enter_sleep_state_s4bios
399 * DESCRIPTION: Perform a S4 bios request.
400 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
402 ******************************************************************************/
403 acpi_status asmlinkage
acpi_enter_sleep_state_s4bios(void)
408 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios
);
410 /* Clear the wake status bit (PM1) */
413 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS
, ACPI_CLEAR_STATUS
);
414 if (ACPI_FAILURE(status
)) {
415 return_ACPI_STATUS(status
);
418 status
= acpi_hw_clear_acpi_status();
419 if (ACPI_FAILURE(status
)) {
420 return_ACPI_STATUS(status
);
424 * 1) Disable/Clear all GPEs
425 * 2) Enable all wakeup GPEs
427 status
= acpi_hw_disable_all_gpes();
428 if (ACPI_FAILURE(status
)) {
429 return_ACPI_STATUS(status
);
431 acpi_gbl_system_awake_and_running
= FALSE
;
433 status
= acpi_hw_enable_all_wakeup_gpes();
434 if (ACPI_FAILURE(status
)) {
435 return_ACPI_STATUS(status
);
438 ACPI_FLUSH_CPU_CACHE();
440 status
= acpi_hw_write_port(acpi_gbl_FADT
.smi_command
,
441 (u32
) acpi_gbl_FADT
.S4bios_request
, 8);
446 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS
, &in_value
);
447 if (ACPI_FAILURE(status
)) {
448 return_ACPI_STATUS(status
);
452 return_ACPI_STATUS(AE_OK
);
455 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios
)
457 /*******************************************************************************
459 * FUNCTION: acpi_leave_sleep_state_prep
461 * PARAMETERS: sleep_state - Which sleep state we are exiting
465 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
467 * Called with interrupts DISABLED.
469 ******************************************************************************/
470 acpi_status
acpi_leave_sleep_state_prep(u8 sleep_state
)
472 struct acpi_object_list arg_list
;
473 union acpi_object arg
;
475 struct acpi_bit_register_info
*sleep_type_reg_info
;
476 struct acpi_bit_register_info
*sleep_enable_reg_info
;
480 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep
);
483 * Set SLP_TYPE and SLP_EN to state S0.
484 * This is unclear from the ACPI Spec, but it is required
487 status
= acpi_get_sleep_type_data(ACPI_STATE_S0
,
488 &acpi_gbl_sleep_type_a
,
489 &acpi_gbl_sleep_type_b
);
490 if (ACPI_SUCCESS(status
)) {
491 sleep_type_reg_info
=
492 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE
);
493 sleep_enable_reg_info
=
494 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
496 /* Get current value of PM1A control */
498 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
500 if (ACPI_SUCCESS(status
)) {
502 /* Clear the SLP_EN and SLP_TYP fields */
504 pm1a_control
&= ~(sleep_type_reg_info
->access_bit_mask
|
505 sleep_enable_reg_info
->
507 pm1b_control
= pm1a_control
;
509 /* Insert the SLP_TYP bits */
511 pm1a_control
|= (acpi_gbl_sleep_type_a
<<
512 sleep_type_reg_info
->bit_position
);
513 pm1b_control
|= (acpi_gbl_sleep_type_b
<<
514 sleep_type_reg_info
->bit_position
);
516 /* Write the control registers and ignore any errors */
518 (void)acpi_hw_write_pm1_control(pm1a_control
,
524 /* Execute the _BFS method */
527 arg_list
.pointer
= &arg
;
528 arg
.type
= ACPI_TYPE_INTEGER
;
529 arg
.integer
.value
= sleep_state
;
531 status
= acpi_evaluate_object(NULL
, METHOD_NAME__BFS
, &arg_list
, NULL
);
532 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
533 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _BFS"));
536 return_ACPI_STATUS(status
);
539 /*******************************************************************************
541 * FUNCTION: acpi_leave_sleep_state
543 * PARAMETERS: sleep_state - Which sleep state we just exited
547 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
548 * Called with interrupts ENABLED.
550 ******************************************************************************/
551 acpi_status
acpi_leave_sleep_state(u8 sleep_state
)
553 struct acpi_object_list arg_list
;
554 union acpi_object arg
;
557 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state
);
559 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
561 acpi_gbl_sleep_type_a
= ACPI_SLEEP_TYPE_INVALID
;
563 /* Setup parameter object */
566 arg_list
.pointer
= &arg
;
567 arg
.type
= ACPI_TYPE_INTEGER
;
569 /* Ignore any errors from these methods */
571 arg
.integer
.value
= ACPI_SST_WAKING
;
572 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
573 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
574 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _SST"));
578 * GPEs must be enabled before _WAK is called as GPEs
579 * might get fired there
582 * 1) Disable/Clear all GPEs
583 * 2) Enable all runtime GPEs
585 status
= acpi_hw_disable_all_gpes();
586 if (ACPI_FAILURE(status
)) {
587 return_ACPI_STATUS(status
);
589 status
= acpi_hw_enable_all_runtime_gpes();
590 if (ACPI_FAILURE(status
)) {
591 return_ACPI_STATUS(status
);
594 arg
.integer
.value
= sleep_state
;
595 status
= acpi_evaluate_object(NULL
, METHOD_NAME__WAK
, &arg_list
, NULL
);
596 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
597 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _WAK"));
599 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
602 * Some BIOSes assume that WAK_STS will be cleared on resume and use
603 * it to determine whether the system is rebooting or resuming. Clear
604 * it for compatibility.
606 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS
, 1);
608 acpi_gbl_system_awake_and_running
= TRUE
;
610 /* Enable power button */
613 acpi_write_bit_register(acpi_gbl_fixed_event_info
614 [ACPI_EVENT_POWER_BUTTON
].
615 enable_register_id
, ACPI_ENABLE_EVENT
);
618 acpi_write_bit_register(acpi_gbl_fixed_event_info
619 [ACPI_EVENT_POWER_BUTTON
].
620 status_register_id
, ACPI_CLEAR_STATUS
);
622 arg
.integer
.value
= ACPI_SST_WORKING
;
623 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
624 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
625 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _SST"));
628 return_ACPI_STATUS(status
);
631 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state
)