2 /******************************************************************************
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2007, R. Byron Moore
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
);
74 acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS
,
75 (struct acpi_table_header
**)&facs
);
76 if (ACPI_FAILURE(status
)) {
77 return_ACPI_STATUS(status
);
82 if ((facs
->length
< 32) || (!(facs
->xfirmware_waking_vector
))) {
84 * ACPI 1.0 FACS or short table or optional X_ field is zero
86 facs
->firmware_waking_vector
= (u32
) physical_address
;
89 * ACPI 2.0 FACS with valid X_ field
91 facs
->xfirmware_waking_vector
= physical_address
;
94 return_ACPI_STATUS(AE_OK
);
97 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector
)
99 /*******************************************************************************
101 * FUNCTION: acpi_get_firmware_waking_vector
103 * PARAMETERS: *physical_address - Where the contents of
104 * the firmware_waking_vector field of
105 * the FACS will be returned.
107 * RETURN: Status, vector
109 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
111 ******************************************************************************/
112 #ifdef ACPI_FUTURE_USAGE
114 acpi_get_firmware_waking_vector(acpi_physical_address
* physical_address
)
116 struct acpi_table_facs
*facs
;
119 ACPI_FUNCTION_TRACE(acpi_get_firmware_waking_vector
);
121 if (!physical_address
) {
122 return_ACPI_STATUS(AE_BAD_PARAMETER
);
128 acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS
,
129 (struct acpi_table_header
**)&facs
);
130 if (ACPI_FAILURE(status
)) {
131 return_ACPI_STATUS(status
);
136 if ((facs
->length
< 32) || (!(facs
->xfirmware_waking_vector
))) {
138 * ACPI 1.0 FACS or short table or optional X_ field is zero
141 (acpi_physical_address
) facs
->firmware_waking_vector
;
144 * ACPI 2.0 FACS with valid X_ field
147 (acpi_physical_address
) facs
->xfirmware_waking_vector
;
150 return_ACPI_STATUS(AE_OK
);
153 ACPI_EXPORT_SYMBOL(acpi_get_firmware_waking_vector
)
155 /*******************************************************************************
157 * FUNCTION: acpi_enter_sleep_state_prep
159 * PARAMETERS: sleep_state - Which sleep state to enter
163 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
164 * This function must execute with interrupts enabled.
165 * We break sleeping into 2 stages so that OSPM can handle
166 * various OS-specific tasks between the two steps.
168 ******************************************************************************/
169 acpi_status
acpi_enter_sleep_state_prep(u8 sleep_state
)
172 struct acpi_object_list arg_list
;
173 union acpi_object arg
;
175 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep
);
178 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
180 status
= acpi_get_sleep_type_data(sleep_state
,
181 &acpi_gbl_sleep_type_a
,
182 &acpi_gbl_sleep_type_b
);
183 if (ACPI_FAILURE(status
)) {
184 return_ACPI_STATUS(status
);
187 /* Setup parameter object */
190 arg_list
.pointer
= &arg
;
192 arg
.type
= ACPI_TYPE_INTEGER
;
193 arg
.integer
.value
= sleep_state
;
195 /* Run the _PTS method */
197 status
= acpi_evaluate_object(NULL
, METHOD_NAME__PTS
, &arg_list
, NULL
);
198 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
199 return_ACPI_STATUS(status
);
202 /* Setup the argument to _SST */
204 switch (sleep_state
) {
206 arg
.integer
.value
= ACPI_SST_WORKING
;
212 arg
.integer
.value
= ACPI_SST_SLEEPING
;
216 arg
.integer
.value
= ACPI_SST_SLEEP_CONTEXT
;
220 arg
.integer
.value
= ACPI_SST_INDICATOR_OFF
; /* Default is off */
224 /* Set the system indicators to show the desired sleep state. */
226 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
227 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
228 ACPI_EXCEPTION((AE_INFO
, status
,
229 "While executing method _SST"));
232 return_ACPI_STATUS(status
);
235 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep
)
237 /*******************************************************************************
239 * FUNCTION: acpi_enter_sleep_state
241 * PARAMETERS: sleep_state - Which sleep state to enter
245 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
246 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
248 ******************************************************************************/
249 acpi_status asmlinkage
acpi_enter_sleep_state(u8 sleep_state
)
253 struct acpi_bit_register_info
*sleep_type_reg_info
;
254 struct acpi_bit_register_info
*sleep_enable_reg_info
;
256 struct acpi_object_list arg_list
;
257 union acpi_object arg
;
260 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state
);
262 if ((acpi_gbl_sleep_type_a
> ACPI_SLEEP_TYPE_MAX
) ||
263 (acpi_gbl_sleep_type_b
> ACPI_SLEEP_TYPE_MAX
)) {
264 ACPI_ERROR((AE_INFO
, "Sleep values out of range: A=%X B=%X",
265 acpi_gbl_sleep_type_a
, acpi_gbl_sleep_type_b
));
266 return_ACPI_STATUS(AE_AML_OPERAND_VALUE
);
269 sleep_type_reg_info
=
270 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A
);
271 sleep_enable_reg_info
=
272 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
274 /* Clear wake status */
276 status
= acpi_set_register(ACPI_BITREG_WAKE_STATUS
, 1);
277 if (ACPI_FAILURE(status
)) {
278 return_ACPI_STATUS(status
);
281 /* Clear all fixed and general purpose status bits */
283 status
= acpi_hw_clear_acpi_status();
284 if (ACPI_FAILURE(status
)) {
285 return_ACPI_STATUS(status
);
289 * 1) Disable/Clear all GPEs
290 * 2) Enable all wakeup GPEs
292 status
= acpi_hw_disable_all_gpes();
293 if (ACPI_FAILURE(status
)) {
294 return_ACPI_STATUS(status
);
296 acpi_gbl_system_awake_and_running
= FALSE
;
298 status
= acpi_hw_enable_all_wakeup_gpes();
299 if (ACPI_FAILURE(status
)) {
300 return_ACPI_STATUS(status
);
303 /* Execute the _GTS method */
306 arg_list
.pointer
= &arg
;
307 arg
.type
= ACPI_TYPE_INTEGER
;
308 arg
.integer
.value
= sleep_state
;
310 status
= acpi_evaluate_object(NULL
, METHOD_NAME__GTS
, &arg_list
, NULL
);
311 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
312 return_ACPI_STATUS(status
);
315 /* Get current value of PM1A control */
317 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
, &PM1Acontrol
);
318 if (ACPI_FAILURE(status
)) {
319 return_ACPI_STATUS(status
);
321 ACPI_DEBUG_PRINT((ACPI_DB_INIT
,
322 "Entering sleep state [S%d]\n", sleep_state
));
324 /* Clear SLP_EN and SLP_TYP fields */
326 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
|
327 sleep_enable_reg_info
->access_bit_mask
);
328 PM1Bcontrol
= PM1Acontrol
;
330 /* Insert SLP_TYP bits */
333 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
335 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
338 * We split the writes of SLP_TYP and SLP_EN to workaround
339 * poorly implemented hardware.
342 /* Write #1: fill in SLP_TYP data */
344 status
= acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL
,
346 if (ACPI_FAILURE(status
)) {
347 return_ACPI_STATUS(status
);
350 status
= acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL
,
352 if (ACPI_FAILURE(status
)) {
353 return_ACPI_STATUS(status
);
356 /* Insert SLP_ENABLE bit */
358 PM1Acontrol
|= sleep_enable_reg_info
->access_bit_mask
;
359 PM1Bcontrol
|= sleep_enable_reg_info
->access_bit_mask
;
361 /* Write #2: SLP_TYP + SLP_EN */
363 ACPI_FLUSH_CPU_CACHE();
365 status
= acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL
,
367 if (ACPI_FAILURE(status
)) {
368 return_ACPI_STATUS(status
);
371 status
= acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL
,
373 if (ACPI_FAILURE(status
)) {
374 return_ACPI_STATUS(status
);
377 if (sleep_state
> ACPI_STATE_S3
) {
379 * We wanted to sleep > S3, but it didn't happen (by virtue of the
380 * fact that we are still executing!)
382 * Wait ten seconds, then try again. This is to get S4/S5 to work on
385 * We wait so long to allow chipsets that poll this reg very slowly to
386 * still read the right value. Ideally, this block would go
389 acpi_os_stall(10000000);
391 status
= acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL
,
392 sleep_enable_reg_info
->
394 if (ACPI_FAILURE(status
)) {
395 return_ACPI_STATUS(status
);
399 /* Wait until we enter sleep state */
402 status
= acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS
,
404 if (ACPI_FAILURE(status
)) {
405 return_ACPI_STATUS(status
);
408 /* Spin until we wake */
412 return_ACPI_STATUS(AE_OK
);
415 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state
)
417 /*******************************************************************************
419 * FUNCTION: acpi_enter_sleep_state_s4bios
425 * DESCRIPTION: Perform a S4 bios request.
426 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
428 ******************************************************************************/
429 acpi_status asmlinkage
acpi_enter_sleep_state_s4bios(void)
434 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios
);
436 status
= acpi_set_register(ACPI_BITREG_WAKE_STATUS
, 1);
437 if (ACPI_FAILURE(status
)) {
438 return_ACPI_STATUS(status
);
441 status
= acpi_hw_clear_acpi_status();
442 if (ACPI_FAILURE(status
)) {
443 return_ACPI_STATUS(status
);
447 * 1) Disable/Clear all GPEs
448 * 2) Enable all wakeup GPEs
450 status
= acpi_hw_disable_all_gpes();
451 if (ACPI_FAILURE(status
)) {
452 return_ACPI_STATUS(status
);
454 acpi_gbl_system_awake_and_running
= FALSE
;
456 status
= acpi_hw_enable_all_wakeup_gpes();
457 if (ACPI_FAILURE(status
)) {
458 return_ACPI_STATUS(status
);
461 ACPI_FLUSH_CPU_CACHE();
463 status
= acpi_os_write_port(acpi_gbl_FADT
.smi_command
,
464 (u32
) acpi_gbl_FADT
.S4bios_request
, 8);
468 status
= acpi_get_register(ACPI_BITREG_WAKE_STATUS
, &in_value
);
469 if (ACPI_FAILURE(status
)) {
470 return_ACPI_STATUS(status
);
474 return_ACPI_STATUS(AE_OK
);
477 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios
)
479 /*******************************************************************************
481 * FUNCTION: acpi_leave_sleep_state_prep
483 * PARAMETERS: sleep_state - Which sleep state we are exiting
487 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
489 * Called with interrupts DISABLED.
491 ******************************************************************************/
492 acpi_status
acpi_leave_sleep_state_prep(u8 sleep_state
)
494 struct acpi_object_list arg_list
;
495 union acpi_object arg
;
497 struct acpi_bit_register_info
*sleep_type_reg_info
;
498 struct acpi_bit_register_info
*sleep_enable_reg_info
;
502 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep
);
505 * Set SLP_TYPE and SLP_EN to state S0.
506 * This is unclear from the ACPI Spec, but it is required
509 status
= acpi_get_sleep_type_data(ACPI_STATE_S0
,
510 &acpi_gbl_sleep_type_a
,
511 &acpi_gbl_sleep_type_b
);
512 if (ACPI_SUCCESS(status
)) {
513 sleep_type_reg_info
=
514 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A
);
515 sleep_enable_reg_info
=
516 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
518 /* Get current value of PM1A control */
520 status
= acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL
,
522 if (ACPI_SUCCESS(status
)) {
524 /* Clear SLP_EN and SLP_TYP fields */
526 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
|
527 sleep_enable_reg_info
->
529 PM1Bcontrol
= PM1Acontrol
;
531 /* Insert SLP_TYP bits */
534 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->
537 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->
540 /* Just ignore any errors */
542 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL
,
544 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL
,
549 /* Execute the _BFS method */
552 arg_list
.pointer
= &arg
;
553 arg
.type
= ACPI_TYPE_INTEGER
;
554 arg
.integer
.value
= sleep_state
;
556 status
= acpi_evaluate_object(NULL
, METHOD_NAME__BFS
, &arg_list
, NULL
);
557 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
558 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _BFS"));
561 return_ACPI_STATUS(status
);
564 /*******************************************************************************
566 * FUNCTION: acpi_leave_sleep_state
568 * PARAMETERS: sleep_state - Which sleep state we just exited
572 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
573 * Called with interrupts ENABLED.
575 ******************************************************************************/
576 acpi_status
acpi_leave_sleep_state(u8 sleep_state
)
578 struct acpi_object_list arg_list
;
579 union acpi_object arg
;
582 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state
);
584 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
586 acpi_gbl_sleep_type_a
= ACPI_SLEEP_TYPE_INVALID
;
588 /* Setup parameter object */
591 arg_list
.pointer
= &arg
;
592 arg
.type
= ACPI_TYPE_INTEGER
;
594 /* Ignore any errors from these methods */
596 arg
.integer
.value
= ACPI_SST_WAKING
;
597 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
598 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
599 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _SST"));
603 * GPEs must be enabled before _WAK is called as GPEs
604 * might get fired there
607 * 1) Disable/Clear all GPEs
608 * 2) Enable all runtime GPEs
610 status
= acpi_hw_disable_all_gpes();
611 if (ACPI_FAILURE(status
)) {
612 return_ACPI_STATUS(status
);
614 status
= acpi_hw_enable_all_runtime_gpes();
615 if (ACPI_FAILURE(status
)) {
616 return_ACPI_STATUS(status
);
619 arg
.integer
.value
= sleep_state
;
620 status
= acpi_evaluate_object(NULL
, METHOD_NAME__WAK
, &arg_list
, NULL
);
621 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
622 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _WAK"));
624 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
626 acpi_gbl_system_awake_and_running
= TRUE
;
628 /* Enable power button */
631 acpi_set_register(acpi_gbl_fixed_event_info
632 [ACPI_EVENT_POWER_BUTTON
].enable_register_id
, 1);
635 acpi_set_register(acpi_gbl_fixed_event_info
636 [ACPI_EVENT_POWER_BUTTON
].status_register_id
, 1);
638 arg
.integer
.value
= ACPI_SST_WORKING
;
639 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
640 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
641 ACPI_EXCEPTION((AE_INFO
, status
, "During Method _SST"));
644 return_ACPI_STATUS(status
);
647 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state
)