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>
46 #include <acpi/acpi.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
)
67 ACPI_FUNCTION_TRACE("acpi_set_firmware_waking_vector");
71 if (acpi_gbl_common_fACS
.vector_width
== 32) {
73 (u32
, acpi_gbl_common_fACS
.firmware_waking_vector
))
74 = (u32
) physical_address
;
76 *acpi_gbl_common_fACS
.firmware_waking_vector
= physical_address
;
79 return_ACPI_STATUS(AE_OK
);
82 /*******************************************************************************
84 * FUNCTION: acpi_get_firmware_waking_vector
86 * PARAMETERS: *physical_address - Where the contents of
87 * the firmware_waking_vector field of
88 * the FACS will be returned.
90 * RETURN: Status, vector
92 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
94 ******************************************************************************/
96 #ifdef ACPI_FUTURE_USAGE
98 acpi_get_firmware_waking_vector(acpi_physical_address
* physical_address
)
101 ACPI_FUNCTION_TRACE("acpi_get_firmware_waking_vector");
103 if (!physical_address
) {
104 return_ACPI_STATUS(AE_BAD_PARAMETER
);
109 if (acpi_gbl_common_fACS
.vector_width
== 32) {
110 *physical_address
= (acpi_physical_address
)
113 (u32
, acpi_gbl_common_fACS
.firmware_waking_vector
));
116 *acpi_gbl_common_fACS
.firmware_waking_vector
;
119 return_ACPI_STATUS(AE_OK
);
123 /*******************************************************************************
125 * FUNCTION: acpi_enter_sleep_state_prep
127 * PARAMETERS: sleep_state - Which sleep state to enter
131 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
132 * This function must execute with interrupts enabled.
133 * We break sleeping into 2 stages so that OSPM can handle
134 * various OS-specific tasks between the two steps.
136 ******************************************************************************/
138 acpi_status
acpi_enter_sleep_state_prep(u8 sleep_state
)
141 struct acpi_object_list arg_list
;
142 union acpi_object arg
;
144 ACPI_FUNCTION_TRACE("acpi_enter_sleep_state_prep");
147 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
149 status
= acpi_get_sleep_type_data(sleep_state
,
150 &acpi_gbl_sleep_type_a
,
151 &acpi_gbl_sleep_type_b
);
152 if (ACPI_FAILURE(status
)) {
153 return_ACPI_STATUS(status
);
156 /* Setup parameter object */
159 arg_list
.pointer
= &arg
;
161 arg
.type
= ACPI_TYPE_INTEGER
;
162 arg
.integer
.value
= sleep_state
;
164 /* Run the _PTS and _GTS methods */
166 status
= acpi_evaluate_object(NULL
, METHOD_NAME__PTS
, &arg_list
, NULL
);
167 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
168 return_ACPI_STATUS(status
);
171 status
= acpi_evaluate_object(NULL
, METHOD_NAME__GTS
, &arg_list
, NULL
);
172 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
173 return_ACPI_STATUS(status
);
176 /* Setup the argument to _SST */
178 switch (sleep_state
) {
180 arg
.integer
.value
= ACPI_SST_WORKING
;
186 arg
.integer
.value
= ACPI_SST_SLEEPING
;
190 arg
.integer
.value
= ACPI_SST_SLEEP_CONTEXT
;
194 arg
.integer
.value
= ACPI_SST_INDICATOR_OFF
; /* Default is off */
198 /* Set the system indicators to show the desired sleep state. */
200 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
201 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
202 ACPI_REPORT_ERROR(("Method _SST failed, %s\n",
203 acpi_format_exception(status
)));
206 return_ACPI_STATUS(AE_OK
);
209 /*******************************************************************************
211 * FUNCTION: acpi_enter_sleep_state
213 * PARAMETERS: sleep_state - Which sleep state to enter
217 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
218 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
220 ******************************************************************************/
222 acpi_status asmlinkage
acpi_enter_sleep_state(u8 sleep_state
)
226 struct acpi_bit_register_info
*sleep_type_reg_info
;
227 struct acpi_bit_register_info
*sleep_enable_reg_info
;
231 ACPI_FUNCTION_TRACE("acpi_enter_sleep_state");
233 if ((acpi_gbl_sleep_type_a
> ACPI_SLEEP_TYPE_MAX
) ||
234 (acpi_gbl_sleep_type_b
> ACPI_SLEEP_TYPE_MAX
)) {
235 ACPI_REPORT_ERROR(("Sleep values out of range: A=%X B=%X\n",
236 acpi_gbl_sleep_type_a
,
237 acpi_gbl_sleep_type_b
));
238 return_ACPI_STATUS(AE_AML_OPERAND_VALUE
);
241 sleep_type_reg_info
=
242 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A
);
243 sleep_enable_reg_info
=
244 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
246 /* Clear wake status */
249 acpi_set_register(ACPI_BITREG_WAKE_STATUS
, 1, ACPI_MTX_DO_NOT_LOCK
);
250 if (ACPI_FAILURE(status
)) {
251 return_ACPI_STATUS(status
);
254 /* Clear all fixed and general purpose status bits */
256 status
= acpi_hw_clear_acpi_status(ACPI_MTX_DO_NOT_LOCK
);
257 if (ACPI_FAILURE(status
)) {
258 return_ACPI_STATUS(status
);
262 * 1) Disable/Clear all GPEs
263 * 2) Enable all wakeup GPEs
265 status
= acpi_hw_disable_all_gpes();
266 if (ACPI_FAILURE(status
)) {
267 return_ACPI_STATUS(status
);
269 acpi_gbl_system_awake_and_running
= FALSE
;
271 status
= acpi_hw_enable_all_wakeup_gpes();
272 if (ACPI_FAILURE(status
)) {
273 return_ACPI_STATUS(status
);
276 /* Get current value of PM1A control */
278 status
= acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK
,
279 ACPI_REGISTER_PM1_CONTROL
, &PM1Acontrol
);
280 if (ACPI_FAILURE(status
)) {
281 return_ACPI_STATUS(status
);
283 ACPI_DEBUG_PRINT((ACPI_DB_INIT
,
284 "Entering sleep state [S%d]\n", sleep_state
));
286 /* Clear SLP_EN and SLP_TYP fields */
288 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
|
289 sleep_enable_reg_info
->access_bit_mask
);
290 PM1Bcontrol
= PM1Acontrol
;
292 /* Insert SLP_TYP bits */
295 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->bit_position
);
297 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->bit_position
);
300 * We split the writes of SLP_TYP and SLP_EN to workaround
301 * poorly implemented hardware.
304 /* Write #1: fill in SLP_TYP data */
306 status
= acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK
,
307 ACPI_REGISTER_PM1A_CONTROL
,
309 if (ACPI_FAILURE(status
)) {
310 return_ACPI_STATUS(status
);
313 status
= acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK
,
314 ACPI_REGISTER_PM1B_CONTROL
,
316 if (ACPI_FAILURE(status
)) {
317 return_ACPI_STATUS(status
);
320 /* Insert SLP_ENABLE bit */
322 PM1Acontrol
|= sleep_enable_reg_info
->access_bit_mask
;
323 PM1Bcontrol
|= sleep_enable_reg_info
->access_bit_mask
;
325 /* Write #2: SLP_TYP + SLP_EN */
327 ACPI_FLUSH_CPU_CACHE();
329 status
= acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK
,
330 ACPI_REGISTER_PM1A_CONTROL
,
332 if (ACPI_FAILURE(status
)) {
333 return_ACPI_STATUS(status
);
336 status
= acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK
,
337 ACPI_REGISTER_PM1B_CONTROL
,
339 if (ACPI_FAILURE(status
)) {
340 return_ACPI_STATUS(status
);
343 if (sleep_state
> ACPI_STATE_S3
) {
345 * We wanted to sleep > S3, but it didn't happen (by virtue of the
346 * fact that we are still executing!)
348 * Wait ten seconds, then try again. This is to get S4/S5 to work on
351 * We wait so long to allow chipsets that poll this reg very slowly to
352 * still read the right value. Ideally, this block would go
355 acpi_os_stall(10000000);
357 status
= acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK
,
358 ACPI_REGISTER_PM1_CONTROL
,
359 sleep_enable_reg_info
->
361 if (ACPI_FAILURE(status
)) {
362 return_ACPI_STATUS(status
);
366 /* Wait until we enter sleep state */
369 status
= acpi_get_register(ACPI_BITREG_WAKE_STATUS
, &in_value
,
370 ACPI_MTX_DO_NOT_LOCK
);
371 if (ACPI_FAILURE(status
)) {
372 return_ACPI_STATUS(status
);
375 /* Spin until we wake */
379 return_ACPI_STATUS(AE_OK
);
382 EXPORT_SYMBOL(acpi_enter_sleep_state
);
384 /*******************************************************************************
386 * FUNCTION: acpi_enter_sleep_state_s4bios
392 * DESCRIPTION: Perform a S4 bios request.
393 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
395 ******************************************************************************/
397 acpi_status asmlinkage
acpi_enter_sleep_state_s4bios(void)
402 ACPI_FUNCTION_TRACE("acpi_enter_sleep_state_s4bios");
405 acpi_set_register(ACPI_BITREG_WAKE_STATUS
, 1, ACPI_MTX_DO_NOT_LOCK
);
406 if (ACPI_FAILURE(status
)) {
407 return_ACPI_STATUS(status
);
410 status
= acpi_hw_clear_acpi_status(ACPI_MTX_DO_NOT_LOCK
);
411 if (ACPI_FAILURE(status
)) {
412 return_ACPI_STATUS(status
);
416 * 1) Disable/Clear all GPEs
417 * 2) Enable all wakeup GPEs
419 status
= acpi_hw_disable_all_gpes();
420 if (ACPI_FAILURE(status
)) {
421 return_ACPI_STATUS(status
);
423 acpi_gbl_system_awake_and_running
= FALSE
;
425 status
= acpi_hw_enable_all_wakeup_gpes();
426 if (ACPI_FAILURE(status
)) {
427 return_ACPI_STATUS(status
);
430 ACPI_FLUSH_CPU_CACHE();
432 status
= acpi_os_write_port(acpi_gbl_FADT
->smi_cmd
,
433 (u32
) acpi_gbl_FADT
->S4bios_req
, 8);
437 status
= acpi_get_register(ACPI_BITREG_WAKE_STATUS
, &in_value
,
438 ACPI_MTX_DO_NOT_LOCK
);
439 if (ACPI_FAILURE(status
)) {
440 return_ACPI_STATUS(status
);
444 return_ACPI_STATUS(AE_OK
);
447 EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios
);
449 /*******************************************************************************
451 * FUNCTION: acpi_leave_sleep_state
453 * PARAMETERS: sleep_state - Which sleep state we just exited
457 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
458 * Called with interrupts ENABLED.
460 ******************************************************************************/
462 acpi_status
acpi_leave_sleep_state(u8 sleep_state
)
464 struct acpi_object_list arg_list
;
465 union acpi_object arg
;
467 struct acpi_bit_register_info
*sleep_type_reg_info
;
468 struct acpi_bit_register_info
*sleep_enable_reg_info
;
472 ACPI_FUNCTION_TRACE("acpi_leave_sleep_state");
475 * Set SLP_TYPE and SLP_EN to state S0.
476 * This is unclear from the ACPI Spec, but it is required
479 status
= acpi_get_sleep_type_data(ACPI_STATE_S0
,
480 &acpi_gbl_sleep_type_a
,
481 &acpi_gbl_sleep_type_b
);
482 if (ACPI_SUCCESS(status
)) {
483 sleep_type_reg_info
=
484 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A
);
485 sleep_enable_reg_info
=
486 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE
);
488 /* Get current value of PM1A control */
490 status
= acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK
,
491 ACPI_REGISTER_PM1_CONTROL
,
493 if (ACPI_SUCCESS(status
)) {
494 /* Clear SLP_EN and SLP_TYP fields */
496 PM1Acontrol
&= ~(sleep_type_reg_info
->access_bit_mask
|
497 sleep_enable_reg_info
->
499 PM1Bcontrol
= PM1Acontrol
;
501 /* Insert SLP_TYP bits */
504 (acpi_gbl_sleep_type_a
<< sleep_type_reg_info
->
507 (acpi_gbl_sleep_type_b
<< sleep_type_reg_info
->
510 /* Just ignore any errors */
512 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK
,
513 ACPI_REGISTER_PM1A_CONTROL
,
515 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK
,
516 ACPI_REGISTER_PM1B_CONTROL
,
521 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
523 acpi_gbl_sleep_type_a
= ACPI_SLEEP_TYPE_INVALID
;
525 /* Setup parameter object */
528 arg_list
.pointer
= &arg
;
529 arg
.type
= ACPI_TYPE_INTEGER
;
531 /* Ignore any errors from these methods */
533 arg
.integer
.value
= ACPI_SST_WAKING
;
534 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
535 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
536 ACPI_REPORT_ERROR(("Method _SST failed, %s\n",
537 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",
544 acpi_format_exception(status
)));
547 status
= acpi_evaluate_object(NULL
, METHOD_NAME__WAK
, &arg_list
, NULL
);
548 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
549 ACPI_REPORT_ERROR(("Method _WAK failed, %s\n",
550 acpi_format_exception(status
)));
552 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
556 * 1) Disable/Clear all GPEs
557 * 2) Enable all runtime GPEs
559 status
= acpi_hw_disable_all_gpes();
560 if (ACPI_FAILURE(status
)) {
561 return_ACPI_STATUS(status
);
563 acpi_gbl_system_awake_and_running
= TRUE
;
565 status
= acpi_hw_enable_all_runtime_gpes();
566 if (ACPI_FAILURE(status
)) {
567 return_ACPI_STATUS(status
);
570 /* Enable power button */
573 acpi_set_register(acpi_gbl_fixed_event_info
574 [ACPI_EVENT_POWER_BUTTON
].enable_register_id
, 1,
575 ACPI_MTX_DO_NOT_LOCK
);
578 acpi_set_register(acpi_gbl_fixed_event_info
579 [ACPI_EVENT_POWER_BUTTON
].status_register_id
, 1,
580 ACPI_MTX_DO_NOT_LOCK
);
582 arg
.integer
.value
= ACPI_SST_WORKING
;
583 status
= acpi_evaluate_object(NULL
, METHOD_NAME__SST
, &arg_list
, NULL
);
584 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
585 ACPI_REPORT_ERROR(("Method _SST failed, %s\n",
586 acpi_format_exception(status
)));
589 return_ACPI_STATUS(status
);