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);
82 * According to the ACPI specification 2.0c and later, the 64-bit
83 * waking vector should be cleared and the 32-bit waking vector should
84 * be used, unless we want the wake-up code to be called by the BIOS in
85 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
86 * to fail to resume if the 64-bit vector is used.
88 if (facs->version >= 1)
89 facs->xfirmware_waking_vector = 0;
91 facs->firmware_waking_vector = (u32)physical_address;
93 return_ACPI_STATUS(AE_OK);
96 ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
98 /*******************************************************************************
100 * FUNCTION: acpi_get_firmware_waking_vector
102 * PARAMETERS: *physical_address - Where the contents of
103 * the firmware_waking_vector field of
104 * the FACS will be returned.
106 * RETURN: Status, vector
108 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
110 ******************************************************************************/
111 #ifdef ACPI_FUTURE_USAGE
113 acpi_get_firmware_waking_vector(acpi_physical_address * physical_address)
115 struct acpi_table_facs *facs;
118 ACPI_FUNCTION_TRACE(acpi_get_firmware_waking_vector);
120 if (!physical_address) {
121 return_ACPI_STATUS(AE_BAD_PARAMETER);
126 status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
127 ACPI_CAST_INDIRECT_PTR(struct
130 if (ACPI_FAILURE(status)) {
131 return_ACPI_STATUS(status);
135 *physical_address = (acpi_physical_address)facs->firmware_waking_vector;
137 return_ACPI_STATUS(AE_OK);
140 ACPI_EXPORT_SYMBOL(acpi_get_firmware_waking_vector)
142 /*******************************************************************************
144 * FUNCTION: acpi_enter_sleep_state_prep
146 * PARAMETERS: sleep_state - Which sleep state to enter
150 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
151 * This function must execute with interrupts enabled.
152 * We break sleeping into 2 stages so that OSPM can handle
153 * various OS-specific tasks between the two steps.
155 ******************************************************************************/
156 acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
159 struct acpi_object_list arg_list;
160 union acpi_object arg;
162 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
165 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
167 status = acpi_get_sleep_type_data(sleep_state,
168 &acpi_gbl_sleep_type_a,
169 &acpi_gbl_sleep_type_b);
170 if (ACPI_FAILURE(status)) {
171 return_ACPI_STATUS(status);
174 /* Setup parameter object */
177 arg_list.pointer = &arg;
179 arg.type = ACPI_TYPE_INTEGER;
180 arg.integer.value = sleep_state;
182 /* Run the _PTS method */
184 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
185 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
186 return_ACPI_STATUS(status);
189 /* Setup the argument to _SST */
191 switch (sleep_state) {
193 arg.integer.value = ACPI_SST_WORKING;
199 arg.integer.value = ACPI_SST_SLEEPING;
203 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
207 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
212 * Set the system indicators to show the desired sleep state.
213 * _SST is an optional method (return no error if not found)
215 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
216 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
217 ACPI_EXCEPTION((AE_INFO, status,
218 "While executing method _SST"));
221 return_ACPI_STATUS(AE_OK);
224 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
226 /*******************************************************************************
228 * FUNCTION: acpi_enter_sleep_state
230 * PARAMETERS: sleep_state - Which sleep state to enter
234 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
235 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
237 ******************************************************************************/
238 acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
242 struct acpi_bit_register_info *sleep_type_reg_info;
243 struct acpi_bit_register_info *sleep_enable_reg_info;
245 struct acpi_object_list arg_list;
246 union acpi_object arg;
249 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
251 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
252 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
253 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
254 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
255 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
258 sleep_type_reg_info =
259 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
260 sleep_enable_reg_info =
261 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
263 /* Clear wake status */
265 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
266 if (ACPI_FAILURE(status)) {
267 return_ACPI_STATUS(status);
270 /* Clear all fixed and general purpose status bits */
272 status = acpi_hw_clear_acpi_status();
273 if (ACPI_FAILURE(status)) {
274 return_ACPI_STATUS(status);
278 * 1) Disable/Clear all GPEs
279 * 2) Enable all wakeup GPEs
281 status = acpi_hw_disable_all_gpes();
282 if (ACPI_FAILURE(status)) {
283 return_ACPI_STATUS(status);
285 acpi_gbl_system_awake_and_running = FALSE;
287 status = acpi_hw_enable_all_wakeup_gpes();
288 if (ACPI_FAILURE(status)) {
289 return_ACPI_STATUS(status);
292 /* Execute the _GTS method */
295 arg_list.pointer = &arg;
296 arg.type = ACPI_TYPE_INTEGER;
297 arg.integer.value = sleep_state;
299 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
300 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
301 return_ACPI_STATUS(status);
304 /* Get current value of PM1A control */
306 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
307 if (ACPI_FAILURE(status)) {
308 return_ACPI_STATUS(status);
310 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
311 "Entering sleep state [S%d]\n", sleep_state));
313 /* Clear SLP_EN and SLP_TYP fields */
315 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
316 sleep_enable_reg_info->access_bit_mask);
317 PM1Bcontrol = PM1Acontrol;
319 /* Insert SLP_TYP bits */
322 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
324 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
327 * We split the writes of SLP_TYP and SLP_EN to workaround
328 * poorly implemented hardware.
331 /* Write #1: fill in SLP_TYP data */
333 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
335 if (ACPI_FAILURE(status)) {
336 return_ACPI_STATUS(status);
339 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
341 if (ACPI_FAILURE(status)) {
342 return_ACPI_STATUS(status);
345 /* Insert SLP_ENABLE bit */
347 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
348 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
350 /* Write #2: SLP_TYP + SLP_EN */
352 ACPI_FLUSH_CPU_CACHE();
354 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
356 if (ACPI_FAILURE(status)) {
357 return_ACPI_STATUS(status);
360 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
362 if (ACPI_FAILURE(status)) {
363 return_ACPI_STATUS(status);
366 if (sleep_state > ACPI_STATE_S3) {
368 * We wanted to sleep > S3, but it didn't happen (by virtue of the
369 * fact that we are still executing!)
371 * Wait ten seconds, then try again. This is to get S4/S5 to work on
374 * We wait so long to allow chipsets that poll this reg very slowly to
375 * still read the right value. Ideally, this block would go
378 acpi_os_stall(10000000);
380 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
381 sleep_enable_reg_info->
383 if (ACPI_FAILURE(status)) {
384 return_ACPI_STATUS(status);
388 /* Wait until we enter sleep state */
391 status = acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS,
393 if (ACPI_FAILURE(status)) {
394 return_ACPI_STATUS(status);
397 /* Spin until we wake */
401 return_ACPI_STATUS(AE_OK);
404 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
406 /*******************************************************************************
408 * FUNCTION: acpi_enter_sleep_state_s4bios
414 * DESCRIPTION: Perform a S4 bios request.
415 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
417 ******************************************************************************/
418 acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
423 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
425 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
426 if (ACPI_FAILURE(status)) {
427 return_ACPI_STATUS(status);
430 status = acpi_hw_clear_acpi_status();
431 if (ACPI_FAILURE(status)) {
432 return_ACPI_STATUS(status);
436 * 1) Disable/Clear all GPEs
437 * 2) Enable all wakeup GPEs
439 status = acpi_hw_disable_all_gpes();
440 if (ACPI_FAILURE(status)) {
441 return_ACPI_STATUS(status);
443 acpi_gbl_system_awake_and_running = FALSE;
445 status = acpi_hw_enable_all_wakeup_gpes();
446 if (ACPI_FAILURE(status)) {
447 return_ACPI_STATUS(status);
450 ACPI_FLUSH_CPU_CACHE();
452 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
453 (u32) acpi_gbl_FADT.S4bios_request, 8);
457 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
458 if (ACPI_FAILURE(status)) {
459 return_ACPI_STATUS(status);
463 return_ACPI_STATUS(AE_OK);
466 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
468 /*******************************************************************************
470 * FUNCTION: acpi_leave_sleep_state_prep
472 * PARAMETERS: sleep_state - Which sleep state we are exiting
476 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
478 * Called with interrupts DISABLED.
480 ******************************************************************************/
481 acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
483 struct acpi_object_list arg_list;
484 union acpi_object arg;
486 struct acpi_bit_register_info *sleep_type_reg_info;
487 struct acpi_bit_register_info *sleep_enable_reg_info;
491 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
494 * Set SLP_TYPE and SLP_EN to state S0.
495 * This is unclear from the ACPI Spec, but it is required
498 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
499 &acpi_gbl_sleep_type_a,
500 &acpi_gbl_sleep_type_b);
501 if (ACPI_SUCCESS(status)) {
502 sleep_type_reg_info =
503 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
504 sleep_enable_reg_info =
505 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
507 /* Get current value of PM1A control */
509 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
511 if (ACPI_SUCCESS(status)) {
513 /* Clear SLP_EN and SLP_TYP fields */
515 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
516 sleep_enable_reg_info->
518 PM1Bcontrol = PM1Acontrol;
520 /* Insert SLP_TYP bits */
523 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
526 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
529 /* Just ignore any errors */
531 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
533 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
538 /* Execute the _BFS method */
541 arg_list.pointer = &arg;
542 arg.type = ACPI_TYPE_INTEGER;
543 arg.integer.value = sleep_state;
545 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
546 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
547 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
550 return_ACPI_STATUS(status);
553 /*******************************************************************************
555 * FUNCTION: acpi_leave_sleep_state
557 * PARAMETERS: sleep_state - Which sleep state we just exited
561 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
562 * Called with interrupts ENABLED.
564 ******************************************************************************/
565 acpi_status acpi_leave_sleep_state(u8 sleep_state)
567 struct acpi_object_list arg_list;
568 union acpi_object arg;
571 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
573 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
575 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
577 /* Setup parameter object */
580 arg_list.pointer = &arg;
581 arg.type = ACPI_TYPE_INTEGER;
583 /* Ignore any errors from these methods */
585 arg.integer.value = ACPI_SST_WAKING;
586 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
587 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
588 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
592 * GPEs must be enabled before _WAK is called as GPEs
593 * might get fired there
596 * 1) Disable/Clear all GPEs
597 * 2) Enable all runtime GPEs
599 status = acpi_hw_disable_all_gpes();
600 if (ACPI_FAILURE(status)) {
601 return_ACPI_STATUS(status);
603 status = acpi_hw_enable_all_runtime_gpes();
604 if (ACPI_FAILURE(status)) {
605 return_ACPI_STATUS(status);
608 arg.integer.value = sleep_state;
609 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
610 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
611 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
613 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
616 * Some BIOSes assume that WAK_STS will be cleared on resume and use
617 * it to determine whether the system is rebooting or resuming. Clear
618 * it for compatibility.
620 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
622 acpi_gbl_system_awake_and_running = TRUE;
624 /* Enable power button */
627 acpi_set_register(acpi_gbl_fixed_event_info
628 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
631 acpi_set_register(acpi_gbl_fixed_event_info
632 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
634 arg.integer.value = ACPI_SST_WORKING;
635 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
636 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
637 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
640 return_ACPI_STATUS(status);
643 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)