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>
46 #include <acpi/actables.h>
48 #define _COMPONENT ACPI_HARDWARE
49 ACPI_MODULE_NAME("hwsleep")
51 /*******************************************************************************
53 * FUNCTION: acpi_set_firmware_waking_vector
55 * PARAMETERS: physical_address - Physical address of ACPI real mode
60 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
62 ******************************************************************************/
64 acpi_set_firmware_waking_vector(acpi_physical_address physical_address
)
66 struct acpi_table_facs
*facs
;
69 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector
);
73 status
= acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS
,
74 ACPI_CAST_INDIRECT_PTR(struct
77 if (ACPI_FAILURE(status
)) {
78 return_ACPI_STATUS(status
);
83 if ((facs
->length
< 32) || (!(facs
->xfirmware_waking_vector
))) {
85 * ACPI 1.0 FACS or short table or optional X_ field is zero
87 facs
->firmware_waking_vector
= (u32
) physical_address
;
90 * ACPI 2.0 FACS with valid X_ field
92 facs
->xfirmware_waking_vector
= physical_address
;
95 return_ACPI_STATUS(AE_OK
);
98 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector
)
100 /*******************************************************************************
102 * FUNCTION: acpi_get_firmware_waking_vector
104 * PARAMETERS: *physical_address - Where the contents of
105 * the firmware_waking_vector field of
106 * the FACS will be returned.
108 * RETURN: Status, vector
110 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
112 ******************************************************************************/
113 #ifdef ACPI_FUTURE_USAGE
115 acpi_get_firmware_waking_vector(acpi_physical_address
* physical_address
)
117 struct acpi_table_facs
*facs
;
120 ACPI_FUNCTION_TRACE(acpi_get_firmware_waking_vector
);
122 if (!physical_address
) {
123 return_ACPI_STATUS(AE_BAD_PARAMETER
);
128 status
= acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS
,
129 ACPI_CAST_INDIRECT_PTR(struct
132 if (ACPI_FAILURE(status
)) {
133 return_ACPI_STATUS(status
);
138 if ((facs
->length
< 32) || (!(facs
->xfirmware_waking_vector
))) {
140 * ACPI 1.0 FACS or short table or optional X_ field is zero
143 (acpi_physical_address
) facs
->firmware_waking_vector
;
146 * ACPI 2.0 FACS with valid X_ field
149 (acpi_physical_address
) facs
->xfirmware_waking_vector
;
152 return_ACPI_STATUS(AE_OK
);
155 ACPI_EXPORT_SYMBOL(acpi_get_firmware_waking_vector
)
157 /*******************************************************************************
159 * FUNCTION: acpi_enter_sleep_state_prep
161 * PARAMETERS: sleep_state - Which sleep state to enter
165 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
166 * This function must execute with interrupts enabled.
167 * We break sleeping into 2 stages so that OSPM can handle
168 * various OS-specific tasks between the two steps.
170 ******************************************************************************/
171 acpi_status
acpi_enter_sleep_state_prep(u8 sleep_state
)
174 struct acpi_object_list arg_list
;
175 union acpi_object arg
;
177 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep
);
180 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
182 status
= acpi_get_sleep_type_data(sleep_state
,
183 &acpi_gbl_sleep_type_a
,
184 &acpi_gbl_sleep_type_b
);
185 if (ACPI_FAILURE(status
)) {
186 return_ACPI_STATUS(status
);
189 /* Setup parameter object */
192 arg_list
.pointer
= &arg
;
194 arg
.type
= ACPI_TYPE_INTEGER
;
195 arg
.integer
.value
= sleep_state
;
197 /* Run the _PTS method */
199 status
= acpi_evaluate_object(NULL
, METHOD_NAME__PTS
, &arg_list
, NULL
);
200 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
201 return_ACPI_STATUS(status
);
204 /* Setup the argument to _SST */
206 switch (sleep_state
) {
208 arg
.integer
.value
= ACPI_SST_WORKING
;
214 arg
.integer
.value
= ACPI_SST_SLEEPING
;
218 arg
.integer
.value
= ACPI_SST_SLEEP_CONTEXT
;
222 arg
.integer
.value
= ACPI_SST_INDICATOR_OFF
; /* Default is off */
227 * Set the system indicators to show the desired sleep state.
228 * _SST is an optional method (return no error if not found)
230 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
231 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
232 ACPI_EXCEPTION((AE_INFO
, status
,
233 "While executing method _SST"));
236 return_ACPI_STATUS(AE_OK
);
239 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep
)
241 /*******************************************************************************
243 * FUNCTION: acpi_enter_sleep_state
245 * PARAMETERS: sleep_state - Which sleep state to enter
249 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
250 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
252 ******************************************************************************/
253 acpi_status asmlinkage
acpi_enter_sleep_state(u8 sleep_state
)
257 struct acpi_bit_register_info
*sleep_type_reg_info
;
258 struct acpi_bit_register_info
*sleep_enable_reg_info
;
260 struct acpi_object_list arg_list
;
261 union acpi_object arg
;
264 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state
);
266 if ((acpi_gbl_sleep_type_a
> ACPI_SLEEP_TYPE_MAX
) ||
267 (acpi_gbl_sleep_type_b
> ACPI_SLEEP_TYPE_MAX
)) {
268 ACPI_ERROR((AE_INFO
, "Sleep values out of range: A=%X B=%X",
269 acpi_gbl_sleep_type_a
, acpi_gbl_sleep_type_b
));
270 return_ACPI_STATUS(AE_AML_OPERAND_VALUE
);
273 sleep_type_reg_info
=
274 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A
);
275 sleep_enable_reg_info
=
276 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
278 /* Clear wake status */
280 status
= acpi_set_register(ACPI_BITREG_WAKE_STATUS
, 1);
281 if (ACPI_FAILURE(status
)) {
282 return_ACPI_STATUS(status
);
285 /* Clear all fixed and general purpose status bits */
287 status
= acpi_hw_clear_acpi_status();
288 if (ACPI_FAILURE(status
)) {
289 return_ACPI_STATUS(status
);
293 * 1) Disable/Clear all GPEs
294 * 2) Enable all wakeup GPEs
296 status
= acpi_hw_disable_all_gpes();
297 if (ACPI_FAILURE(status
)) {
298 return_ACPI_STATUS(status
);
300 acpi_gbl_system_awake_and_running
= FALSE
;
302 status
= acpi_hw_enable_all_wakeup_gpes();
303 if (ACPI_FAILURE(status
)) {
304 return_ACPI_STATUS(status
);
307 /* Execute the _GTS method */
310 arg_list
.pointer
= &arg
;
311 arg
.type
= ACPI_TYPE_INTEGER
;
312 arg
.integer
.value
= sleep_state
;
314 status
= acpi_evaluate_object(NULL
, METHOD_NAME__GTS
, &arg_list
, NULL
);
315 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
316 return_ACPI_STATUS(status
);
319 /* Get current value of PM1A control */
321 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
, &PM1Acontrol
);
322 if (ACPI_FAILURE(status
)) {
323 return_ACPI_STATUS(status
);
325 ACPI_DEBUG_PRINT((ACPI_DB_INIT
,
326 "Entering sleep state [S%d]\n", sleep_state
));
328 /* Clear SLP_EN and SLP_TYP fields */
330 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
|
331 sleep_enable_reg_info
->access_bit_mask
);
332 PM1Bcontrol
= PM1Acontrol
;
334 /* Insert SLP_TYP bits */
337 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
339 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
342 * We split the writes of SLP_TYP and SLP_EN to workaround
343 * poorly implemented hardware.
346 /* Write #1: fill in SLP_TYP data */
348 status
= acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL
,
350 if (ACPI_FAILURE(status
)) {
351 return_ACPI_STATUS(status
);
354 status
= acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL
,
356 if (ACPI_FAILURE(status
)) {
357 return_ACPI_STATUS(status
);
360 /* Insert SLP_ENABLE bit */
362 PM1Acontrol
|= sleep_enable_reg_info
->access_bit_mask
;
363 PM1Bcontrol
|= sleep_enable_reg_info
->access_bit_mask
;
365 /* Write #2: SLP_TYP + SLP_EN */
367 ACPI_FLUSH_CPU_CACHE();
369 status
= acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL
,
371 if (ACPI_FAILURE(status
)) {
372 return_ACPI_STATUS(status
);
375 status
= acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL
,
377 if (ACPI_FAILURE(status
)) {
378 return_ACPI_STATUS(status
);
381 if (sleep_state
> ACPI_STATE_S3
) {
383 * We wanted to sleep > S3, but it didn't happen (by virtue of the
384 * fact that we are still executing!)
386 * Wait ten seconds, then try again. This is to get S4/S5 to work on
389 * We wait so long to allow chipsets that poll this reg very slowly to
390 * still read the right value. Ideally, this block would go
393 acpi_os_stall(10000000);
395 status
= acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL
,
396 sleep_enable_reg_info
->
398 if (ACPI_FAILURE(status
)) {
399 return_ACPI_STATUS(status
);
403 /* Wait until we enter sleep state */
406 status
= acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS
,
408 if (ACPI_FAILURE(status
)) {
409 return_ACPI_STATUS(status
);
412 /* Spin until we wake */
416 return_ACPI_STATUS(AE_OK
);
419 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state
)
421 /*******************************************************************************
423 * FUNCTION: acpi_enter_sleep_state_s4bios
429 * DESCRIPTION: Perform a S4 bios request.
430 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
432 ******************************************************************************/
433 acpi_status asmlinkage
acpi_enter_sleep_state_s4bios(void)
438 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios
);
440 status
= acpi_set_register(ACPI_BITREG_WAKE_STATUS
, 1);
441 if (ACPI_FAILURE(status
)) {
442 return_ACPI_STATUS(status
);
445 status
= acpi_hw_clear_acpi_status();
446 if (ACPI_FAILURE(status
)) {
447 return_ACPI_STATUS(status
);
451 * 1) Disable/Clear all GPEs
452 * 2) Enable all wakeup GPEs
454 status
= acpi_hw_disable_all_gpes();
455 if (ACPI_FAILURE(status
)) {
456 return_ACPI_STATUS(status
);
458 acpi_gbl_system_awake_and_running
= FALSE
;
460 status
= acpi_hw_enable_all_wakeup_gpes();
461 if (ACPI_FAILURE(status
)) {
462 return_ACPI_STATUS(status
);
465 ACPI_FLUSH_CPU_CACHE();
467 status
= acpi_os_write_port(acpi_gbl_FADT
.smi_command
,
468 (u32
) acpi_gbl_FADT
.S4bios_request
, 8);
472 status
= acpi_get_register(ACPI_BITREG_WAKE_STATUS
, &in_value
);
473 if (ACPI_FAILURE(status
)) {
474 return_ACPI_STATUS(status
);
478 return_ACPI_STATUS(AE_OK
);
481 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios
)
483 /*******************************************************************************
485 * FUNCTION: acpi_leave_sleep_state_prep
487 * PARAMETERS: sleep_state - Which sleep state we are exiting
491 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
493 * Called with interrupts DISABLED.
495 ******************************************************************************/
496 acpi_status
acpi_leave_sleep_state_prep(u8 sleep_state
)
498 struct acpi_object_list arg_list
;
499 union acpi_object arg
;
501 struct acpi_bit_register_info
*sleep_type_reg_info
;
502 struct acpi_bit_register_info
*sleep_enable_reg_info
;
506 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep
);
509 * Set SLP_TYPE and SLP_EN to state S0.
510 * This is unclear from the ACPI Spec, but it is required
513 status
= acpi_get_sleep_type_data(ACPI_STATE_S0
,
514 &acpi_gbl_sleep_type_a
,
515 &acpi_gbl_sleep_type_b
);
516 if (ACPI_SUCCESS(status
)) {
517 sleep_type_reg_info
=
518 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A
);
519 sleep_enable_reg_info
=
520 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
522 /* Get current value of PM1A control */
524 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
526 if (ACPI_SUCCESS(status
)) {
528 /* Clear SLP_EN and SLP_TYP fields */
530 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
|
531 sleep_enable_reg_info
->
533 PM1Bcontrol
= PM1Acontrol
;
535 /* Insert SLP_TYP bits */
538 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->
541 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->
544 /* Just ignore any errors */
546 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL
,
548 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL
,
553 /* Execute the _BFS method */
556 arg_list
.pointer
= &arg
;
557 arg
.type
= ACPI_TYPE_INTEGER
;
558 arg
.integer
.value
= sleep_state
;
560 status
= acpi_evaluate_object(NULL
, METHOD_NAME__BFS
, &arg_list
, NULL
);
561 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
562 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _BFS"));
565 return_ACPI_STATUS(status
);
568 /*******************************************************************************
570 * FUNCTION: acpi_leave_sleep_state
572 * PARAMETERS: sleep_state - Which sleep state we just exited
576 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
577 * Called with interrupts ENABLED.
579 ******************************************************************************/
580 acpi_status
acpi_leave_sleep_state(u8 sleep_state
)
582 struct acpi_object_list arg_list
;
583 union acpi_object arg
;
586 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state
);
588 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
590 acpi_gbl_sleep_type_a
= ACPI_SLEEP_TYPE_INVALID
;
592 /* Setup parameter object */
595 arg_list
.pointer
= &arg
;
596 arg
.type
= ACPI_TYPE_INTEGER
;
598 /* Ignore any errors from these methods */
600 arg
.integer
.value
= ACPI_SST_WAKING
;
601 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
602 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
603 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _SST"));
607 * GPEs must be enabled before _WAK is called as GPEs
608 * might get fired there
611 * 1) Disable/Clear all GPEs
612 * 2) Enable all runtime GPEs
614 status
= acpi_hw_disable_all_gpes();
615 if (ACPI_FAILURE(status
)) {
616 return_ACPI_STATUS(status
);
618 status
= acpi_hw_enable_all_runtime_gpes();
619 if (ACPI_FAILURE(status
)) {
620 return_ACPI_STATUS(status
);
623 arg
.integer
.value
= sleep_state
;
624 status
= acpi_evaluate_object(NULL
, METHOD_NAME__WAK
, &arg_list
, NULL
);
625 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
626 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _WAK"));
628 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
631 * Some BIOSes assume that WAK_STS will be cleared on resume and use
632 * it to determine whether the system is rebooting or resuming. Clear
633 * it for compatibility.
635 acpi_set_register(ACPI_BITREG_WAKE_STATUS
, 1);
637 acpi_gbl_system_awake_and_running
= TRUE
;
639 /* Enable power button */
642 acpi_set_register(acpi_gbl_fixed_event_info
643 [ACPI_EVENT_POWER_BUTTON
].enable_register_id
, 1);
646 acpi_set_register(acpi_gbl_fixed_event_info
647 [ACPI_EVENT_POWER_BUTTON
].status_register_id
, 1);
649 arg
.integer
.value
= ACPI_SST_WORKING
;
650 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
651 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
652 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _SST"));
655 return_ACPI_STATUS(status
);
658 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state
)