2 /******************************************************************************
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2005, 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 <linux/module.h>
47 #include <acpi/acpi.h>
49 #define _COMPONENT ACPI_HARDWARE
50 ACPI_MODULE_NAME ("hwsleep")
53 #define METHOD_NAME__BFS "\\_BFS"
54 #define METHOD_NAME__GTS "\\_GTS"
55 #define METHOD_NAME__PTS "\\_PTS"
56 #define METHOD_NAME__SST "\\_SI._SST"
57 #define METHOD_NAME__WAK "\\_WAK"
59 #define ACPI_SST_INDICATOR_OFF 0
60 #define ACPI_SST_WORKING 1
61 #define ACPI_SST_WAKING 2
62 #define ACPI_SST_SLEEPING 3
63 #define ACPI_SST_SLEEP_CONTEXT 4
66 /******************************************************************************
68 * FUNCTION: acpi_set_firmware_waking_vector
70 * PARAMETERS: physical_address - Physical address of ACPI real mode
75 * DESCRIPTION: access function for d_firmware_waking_vector field in FACS
77 ******************************************************************************/
80 acpi_set_firmware_waking_vector (
81 acpi_physical_address physical_address
)
84 ACPI_FUNCTION_TRACE ("acpi_set_firmware_waking_vector");
89 if (acpi_gbl_common_fACS
.vector_width
== 32) {
90 *(ACPI_CAST_PTR (u32
, acpi_gbl_common_fACS
.firmware_waking_vector
))
91 = (u32
) physical_address
;
94 *acpi_gbl_common_fACS
.firmware_waking_vector
98 return_ACPI_STATUS (AE_OK
);
102 /******************************************************************************
104 * FUNCTION: acpi_get_firmware_waking_vector
106 * PARAMETERS: *physical_address - Output buffer where contents of
107 * the firmware_waking_vector field of
108 * the FACS will be stored.
112 * DESCRIPTION: Access function for firmware_waking_vector field in FACS
114 ******************************************************************************/
115 #ifdef ACPI_FUTURE_USAGE
117 acpi_get_firmware_waking_vector (
118 acpi_physical_address
*physical_address
)
121 ACPI_FUNCTION_TRACE ("acpi_get_firmware_waking_vector");
124 if (!physical_address
) {
125 return_ACPI_STATUS (AE_BAD_PARAMETER
);
130 if (acpi_gbl_common_fACS
.vector_width
== 32) {
131 *physical_address
= (acpi_physical_address
)
132 *(ACPI_CAST_PTR (u32
, acpi_gbl_common_fACS
.firmware_waking_vector
));
136 *acpi_gbl_common_fACS
.firmware_waking_vector
;
139 return_ACPI_STATUS (AE_OK
);
144 /******************************************************************************
146 * FUNCTION: acpi_enter_sleep_state_prep
148 * PARAMETERS: sleep_state - Which sleep state to enter
152 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
153 * This function must execute with interrupts enabled.
154 * We break sleeping into 2 stages so that OSPM can handle
155 * various OS-specific tasks between the two steps.
157 ******************************************************************************/
160 acpi_enter_sleep_state_prep (
164 struct acpi_object_list arg_list
;
165 union acpi_object arg
;
168 ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_prep");
172 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
174 status
= acpi_get_sleep_type_data (sleep_state
,
175 &acpi_gbl_sleep_type_a
, &acpi_gbl_sleep_type_b
);
176 if (ACPI_FAILURE (status
)) {
177 return_ACPI_STATUS (status
);
180 /* Setup parameter object */
183 arg_list
.pointer
= &arg
;
185 arg
.type
= ACPI_TYPE_INTEGER
;
186 arg
.integer
.value
= sleep_state
;
188 /* Run the _PTS and _GTS methods */
190 status
= acpi_evaluate_object (NULL
, METHOD_NAME__PTS
, &arg_list
, NULL
);
191 if (ACPI_FAILURE (status
) && status
!= AE_NOT_FOUND
) {
192 return_ACPI_STATUS (status
);
195 status
= acpi_evaluate_object (NULL
, METHOD_NAME__GTS
, &arg_list
, NULL
);
196 if (ACPI_FAILURE (status
) && status
!= AE_NOT_FOUND
) {
197 return_ACPI_STATUS (status
);
200 /* Setup the argument to _SST */
202 switch (sleep_state
) {
204 arg
.integer
.value
= ACPI_SST_WORKING
;
210 arg
.integer
.value
= ACPI_SST_SLEEPING
;
214 arg
.integer
.value
= ACPI_SST_SLEEP_CONTEXT
;
218 arg
.integer
.value
= ACPI_SST_INDICATOR_OFF
; /* Default is indicator off */
222 /* Set the system indicators to show the desired sleep state. */
224 status
= acpi_evaluate_object (NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
225 if (ACPI_FAILURE (status
) && status
!= AE_NOT_FOUND
) {
226 ACPI_REPORT_ERROR (("Method _SST failed, %s\n", acpi_format_exception (status
)));
229 return_ACPI_STATUS (AE_OK
);
233 /******************************************************************************
235 * FUNCTION: acpi_enter_sleep_state
237 * PARAMETERS: sleep_state - Which sleep state to enter
241 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
242 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
244 ******************************************************************************/
246 acpi_status asmlinkage
247 acpi_enter_sleep_state (
252 struct acpi_bit_register_info
*sleep_type_reg_info
;
253 struct acpi_bit_register_info
*sleep_enable_reg_info
;
258 ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state");
261 if ((acpi_gbl_sleep_type_a
> ACPI_SLEEP_TYPE_MAX
) ||
262 (acpi_gbl_sleep_type_b
> ACPI_SLEEP_TYPE_MAX
)) {
263 ACPI_REPORT_ERROR (("Sleep values out of range: A=%X B=%X\n",
264 acpi_gbl_sleep_type_a
, acpi_gbl_sleep_type_b
));
265 return_ACPI_STATUS (AE_AML_OPERAND_VALUE
);
268 sleep_type_reg_info
= acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A
);
269 sleep_enable_reg_info
= acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE
);
271 /* Clear wake status */
273 status
= acpi_set_register (ACPI_BITREG_WAKE_STATUS
, 1, ACPI_MTX_DO_NOT_LOCK
);
274 if (ACPI_FAILURE (status
)) {
275 return_ACPI_STATUS (status
);
278 /* Clear all fixed and general purpose status bits */
280 status
= acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK
);
281 if (ACPI_FAILURE (status
)) {
282 return_ACPI_STATUS (status
);
286 * 1) Disable/Clear all GPEs
287 * 2) Enable all wakeup GPEs
289 status
= acpi_hw_disable_all_gpes (ACPI_ISR
);
290 if (ACPI_FAILURE (status
)) {
291 return_ACPI_STATUS (status
);
293 acpi_gbl_system_awake_and_running
= FALSE
;
295 status
= acpi_hw_enable_all_wakeup_gpes (ACPI_ISR
);
296 if (ACPI_FAILURE (status
)) {
297 return_ACPI_STATUS (status
);
300 /* Get current value of PM1A control */
302 status
= acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK
, ACPI_REGISTER_PM1_CONTROL
, &PM1Acontrol
);
303 if (ACPI_FAILURE (status
)) {
304 return_ACPI_STATUS (status
);
306 ACPI_DEBUG_PRINT ((ACPI_DB_INIT
, "Entering sleep state [S%d]\n", sleep_state
));
308 /* Clear SLP_EN and SLP_TYP fields */
310 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
| sleep_enable_reg_info
->access_bit_mask
);
311 PM1Bcontrol
= PM1Acontrol
;
313 /* Insert SLP_TYP bits */
315 PM1Acontrol
|= (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
316 PM1Bcontrol
|= (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
319 * We split the writes of SLP_TYP and SLP_EN to workaround
320 * poorly implemented hardware.
323 /* Write #1: fill in SLP_TYP data */
325 status
= acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK
, ACPI_REGISTER_PM1A_CONTROL
, PM1Acontrol
);
326 if (ACPI_FAILURE (status
)) {
327 return_ACPI_STATUS (status
);
330 status
= acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK
, ACPI_REGISTER_PM1B_CONTROL
, PM1Bcontrol
);
331 if (ACPI_FAILURE (status
)) {
332 return_ACPI_STATUS (status
);
335 /* Insert SLP_ENABLE bit */
337 PM1Acontrol
|= sleep_enable_reg_info
->access_bit_mask
;
338 PM1Bcontrol
|= sleep_enable_reg_info
->access_bit_mask
;
340 /* Write #2: SLP_TYP + SLP_EN */
342 ACPI_FLUSH_CPU_CACHE ();
344 status
= acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK
, ACPI_REGISTER_PM1A_CONTROL
, PM1Acontrol
);
345 if (ACPI_FAILURE (status
)) {
346 return_ACPI_STATUS (status
);
349 status
= acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK
, ACPI_REGISTER_PM1B_CONTROL
, PM1Bcontrol
);
350 if (ACPI_FAILURE (status
)) {
351 return_ACPI_STATUS (status
);
354 if (sleep_state
> ACPI_STATE_S3
) {
356 * We wanted to sleep > S3, but it didn't happen (by virtue of the fact that
357 * we are still executing!)
359 * Wait ten seconds, then try again. This is to get S4/S5 to work on all machines.
361 * We wait so long to allow chipsets that poll this reg very slowly to
362 * still read the right value. Ideally, this block would go
365 acpi_os_stall (10000000);
367 status
= acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK
, ACPI_REGISTER_PM1_CONTROL
,
368 sleep_enable_reg_info
->access_bit_mask
);
369 if (ACPI_FAILURE (status
)) {
370 return_ACPI_STATUS (status
);
374 /* Wait until we enter sleep state */
377 status
= acpi_get_register (ACPI_BITREG_WAKE_STATUS
, &in_value
, ACPI_MTX_DO_NOT_LOCK
);
378 if (ACPI_FAILURE (status
)) {
379 return_ACPI_STATUS (status
);
382 /* Spin until we wake */
386 return_ACPI_STATUS (AE_OK
);
388 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 ******************************************************************************/
404 acpi_status asmlinkage
405 acpi_enter_sleep_state_s4bios (
412 ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_s4bios");
415 status
= acpi_set_register (ACPI_BITREG_WAKE_STATUS
, 1, ACPI_MTX_DO_NOT_LOCK
);
416 if (ACPI_FAILURE (status
)) {
417 return_ACPI_STATUS (status
);
420 status
= acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK
);
421 if (ACPI_FAILURE (status
)) {
422 return_ACPI_STATUS (status
);
426 * 1) Disable/Clear all GPEs
427 * 2) Enable all wakeup GPEs
429 status
= acpi_hw_disable_all_gpes (ACPI_ISR
);
430 if (ACPI_FAILURE (status
)) {
431 return_ACPI_STATUS (status
);
433 acpi_gbl_system_awake_and_running
= FALSE
;
435 status
= acpi_hw_enable_all_wakeup_gpes (ACPI_ISR
);
436 if (ACPI_FAILURE (status
)) {
437 return_ACPI_STATUS (status
);
440 ACPI_FLUSH_CPU_CACHE ();
442 status
= acpi_os_write_port (acpi_gbl_FADT
->smi_cmd
, (u32
) acpi_gbl_FADT
->S4bios_req
, 8);
446 status
= acpi_get_register (ACPI_BITREG_WAKE_STATUS
, &in_value
, ACPI_MTX_DO_NOT_LOCK
);
447 if (ACPI_FAILURE (status
)) {
448 return_ACPI_STATUS (status
);
452 return_ACPI_STATUS (AE_OK
);
454 EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios
);
457 /******************************************************************************
459 * FUNCTION: acpi_leave_sleep_state
461 * PARAMETERS: sleep_state - Which sleep state we just exited
465 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
466 * Called with interrupts ENABLED.
468 ******************************************************************************/
471 acpi_leave_sleep_state (
474 struct acpi_object_list arg_list
;
475 union acpi_object arg
;
477 struct acpi_bit_register_info
*sleep_type_reg_info
;
478 struct acpi_bit_register_info
*sleep_enable_reg_info
;
483 ACPI_FUNCTION_TRACE ("acpi_leave_sleep_state");
487 * Set SLP_TYPE and SLP_EN to state S0.
488 * This is unclear from the ACPI Spec, but it is required
491 status
= acpi_get_sleep_type_data (ACPI_STATE_S0
,
492 &acpi_gbl_sleep_type_a
, &acpi_gbl_sleep_type_b
);
493 if (ACPI_SUCCESS (status
)) {
494 sleep_type_reg_info
= acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A
);
495 sleep_enable_reg_info
= acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE
);
497 /* Get current value of PM1A control */
499 status
= acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK
,
500 ACPI_REGISTER_PM1_CONTROL
, &PM1Acontrol
);
501 if (ACPI_SUCCESS (status
)) {
502 /* Clear SLP_EN and SLP_TYP fields */
504 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
|
505 sleep_enable_reg_info
->access_bit_mask
);
506 PM1Bcontrol
= PM1Acontrol
;
508 /* Insert SLP_TYP bits */
510 PM1Acontrol
|= (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
511 PM1Bcontrol
|= (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
513 /* Just ignore any errors */
515 (void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK
,
516 ACPI_REGISTER_PM1A_CONTROL
, PM1Acontrol
);
517 (void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK
,
518 ACPI_REGISTER_PM1B_CONTROL
, PM1Bcontrol
);
522 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
524 acpi_gbl_sleep_type_a
= ACPI_SLEEP_TYPE_INVALID
;
526 /* Setup parameter object */
529 arg_list
.pointer
= &arg
;
530 arg
.type
= ACPI_TYPE_INTEGER
;
532 /* Ignore any errors from these methods */
534 arg
.integer
.value
= ACPI_SST_WAKING
;
535 status
= acpi_evaluate_object (NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
536 if (ACPI_FAILURE (status
) && status
!= AE_NOT_FOUND
) {
537 ACPI_REPORT_ERROR (("Method _SST failed, %s\n", acpi_format_exception (status
)));
540 arg
.integer
.value
= sleep_state
;
541 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", acpi_format_exception (status
)));
546 status
= acpi_evaluate_object (NULL
, METHOD_NAME__WAK
, &arg_list
, NULL
);
547 if (ACPI_FAILURE (status
) && status
!= AE_NOT_FOUND
) {
548 ACPI_REPORT_ERROR (("Method _WAK failed, %s\n", acpi_format_exception (status
)));
550 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
554 * 1) Disable/Clear all GPEs
555 * 2) Enable all runtime GPEs
557 status
= acpi_hw_disable_all_gpes (ACPI_NOT_ISR
);
558 if (ACPI_FAILURE (status
)) {
559 return_ACPI_STATUS (status
);
561 acpi_gbl_system_awake_and_running
= TRUE
;
563 status
= acpi_hw_enable_all_runtime_gpes (ACPI_NOT_ISR
);
564 if (ACPI_FAILURE (status
)) {
565 return_ACPI_STATUS (status
);
568 /* Enable power button */
570 (void) acpi_set_register(acpi_gbl_fixed_event_info
[ACPI_EVENT_POWER_BUTTON
].enable_register_id
,
571 1, ACPI_MTX_DO_NOT_LOCK
);
572 (void) acpi_set_register(acpi_gbl_fixed_event_info
[ACPI_EVENT_POWER_BUTTON
].status_register_id
,
573 1, ACPI_MTX_DO_NOT_LOCK
);
575 arg
.integer
.value
= ACPI_SST_WORKING
;
576 status
= acpi_evaluate_object (NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
577 if (ACPI_FAILURE (status
) && status
!= AE_NOT_FOUND
) {
578 ACPI_REPORT_ERROR (("Method _SST failed, %s\n", acpi_format_exception (status
)));
581 return_ACPI_STATUS (status
);