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)
156 /*******************************************************************************
158 * FUNCTION: acpi_enter_sleep_state_prep
160 * PARAMETERS: sleep_state - Which sleep state to enter
164 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
165 * This function must execute with interrupts enabled.
166 * We break sleeping into 2 stages so that OSPM can handle
167 * various OS-specific tasks between the two steps.
169 ******************************************************************************/
170 acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
173 struct acpi_object_list arg_list;
174 union acpi_object arg;
176 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
179 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
181 status = acpi_get_sleep_type_data(sleep_state,
182 &acpi_gbl_sleep_type_a,
183 &acpi_gbl_sleep_type_b);
184 if (ACPI_FAILURE(status)) {
185 return_ACPI_STATUS(status);
188 /* Setup parameter object */
191 arg_list.pointer = &arg;
193 arg.type = ACPI_TYPE_INTEGER;
194 arg.integer.value = sleep_state;
196 /* Run the _PTS and _GTS methods */
198 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
199 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
200 return_ACPI_STATUS(status);
203 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
204 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
205 return_ACPI_STATUS(status);
208 /* Setup the argument to _SST */
210 switch (sleep_state) {
212 arg.integer.value = ACPI_SST_WORKING;
218 arg.integer.value = ACPI_SST_SLEEPING;
222 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
226 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
230 /* Set the system indicators to show the desired sleep state. */
232 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
233 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
234 ACPI_EXCEPTION((AE_INFO, status,
235 "While executing method _SST"));
239 * 1) Disable/Clear all GPEs
241 status = acpi_hw_disable_all_gpes();
242 if (ACPI_FAILURE(status)) {
243 return_ACPI_STATUS(status);
246 return_ACPI_STATUS(AE_OK);
249 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
251 /*******************************************************************************
253 * FUNCTION: acpi_enter_sleep_state
255 * PARAMETERS: sleep_state - Which sleep state to enter
259 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
260 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
262 ******************************************************************************/
263 acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
267 struct acpi_bit_register_info *sleep_type_reg_info;
268 struct acpi_bit_register_info *sleep_enable_reg_info;
272 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
274 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
275 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
276 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
277 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
278 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
281 sleep_type_reg_info =
282 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
283 sleep_enable_reg_info =
284 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
286 /* Clear wake status */
288 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
289 if (ACPI_FAILURE(status)) {
290 return_ACPI_STATUS(status);
293 /* Clear all fixed and general purpose status bits */
295 status = acpi_hw_clear_acpi_status();
296 if (ACPI_FAILURE(status)) {
297 return_ACPI_STATUS(status);
301 * 2) Enable all wakeup GPEs
303 status = acpi_hw_disable_all_gpes();
304 if (ACPI_FAILURE(status)) {
305 return_ACPI_STATUS(status);
308 acpi_gbl_system_awake_and_running = FALSE;
310 status = acpi_hw_enable_all_wakeup_gpes();
311 if (ACPI_FAILURE(status)) {
312 return_ACPI_STATUS(status);
315 /* Get current value of PM1A control */
317 status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
318 ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
319 if (ACPI_FAILURE(status)) {
320 return_ACPI_STATUS(status);
322 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
323 "Entering sleep state [S%d]\n", sleep_state));
325 /* Clear SLP_EN and SLP_TYP fields */
327 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
328 sleep_enable_reg_info->access_bit_mask);
329 PM1Bcontrol = PM1Acontrol;
331 /* Insert SLP_TYP bits */
334 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
336 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
339 * We split the writes of SLP_TYP and SLP_EN to workaround
340 * poorly implemented hardware.
343 /* Write #1: fill in SLP_TYP data */
345 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
346 ACPI_REGISTER_PM1A_CONTROL,
348 if (ACPI_FAILURE(status)) {
349 return_ACPI_STATUS(status);
352 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
353 ACPI_REGISTER_PM1B_CONTROL,
355 if (ACPI_FAILURE(status)) {
356 return_ACPI_STATUS(status);
359 /* Insert SLP_ENABLE bit */
361 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
362 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
364 /* Write #2: SLP_TYP + SLP_EN */
366 ACPI_FLUSH_CPU_CACHE();
368 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
369 ACPI_REGISTER_PM1A_CONTROL,
371 if (ACPI_FAILURE(status)) {
372 return_ACPI_STATUS(status);
375 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
376 ACPI_REGISTER_PM1B_CONTROL,
378 if (ACPI_FAILURE(status)) {
379 return_ACPI_STATUS(status);
382 if (sleep_state > ACPI_STATE_S3) {
384 * We wanted to sleep > S3, but it didn't happen (by virtue of the
385 * fact that we are still executing!)
387 * Wait ten seconds, then try again. This is to get S4/S5 to work on
390 * We wait so long to allow chipsets that poll this reg very slowly to
391 * still read the right value. Ideally, this block would go
394 acpi_os_stall(10000000);
396 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
397 ACPI_REGISTER_PM1_CONTROL,
398 sleep_enable_reg_info->
400 if (ACPI_FAILURE(status)) {
401 return_ACPI_STATUS(status);
405 /* Wait until we enter sleep state */
408 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
409 if (ACPI_FAILURE(status)) {
410 return_ACPI_STATUS(status);
413 /* Spin until we wake */
417 return_ACPI_STATUS(AE_OK);
420 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
422 /*******************************************************************************
424 * FUNCTION: acpi_enter_sleep_state_s4bios
430 * DESCRIPTION: Perform a S4 bios request.
431 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
433 ******************************************************************************/
434 acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
439 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
441 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
442 if (ACPI_FAILURE(status)) {
443 return_ACPI_STATUS(status);
446 status = acpi_hw_clear_acpi_status();
447 if (ACPI_FAILURE(status)) {
448 return_ACPI_STATUS(status);
452 * 1) Disable/Clear all GPEs
453 * 2) Enable all wakeup GPEs
455 status = acpi_hw_disable_all_gpes();
456 if (ACPI_FAILURE(status)) {
457 return_ACPI_STATUS(status);
459 acpi_gbl_system_awake_and_running = FALSE;
461 status = acpi_hw_enable_all_wakeup_gpes();
462 if (ACPI_FAILURE(status)) {
463 return_ACPI_STATUS(status);
466 ACPI_FLUSH_CPU_CACHE();
468 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
469 (u32) acpi_gbl_FADT.S4bios_request, 8);
473 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
474 if (ACPI_FAILURE(status)) {
475 return_ACPI_STATUS(status);
479 return_ACPI_STATUS(AE_OK);
482 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
484 /*******************************************************************************
486 * FUNCTION: acpi_leave_sleep_state
488 * PARAMETERS: sleep_state - Which sleep state we just exited
492 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
493 * Called with interrupts ENABLED.
495 ******************************************************************************/
496 acpi_status acpi_leave_sleep_state(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);
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_MTX_DO_NOT_LOCK,
525 ACPI_REGISTER_PM1_CONTROL,
527 if (ACPI_SUCCESS(status)) {
529 /* Clear SLP_EN and SLP_TYP fields */
531 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
532 sleep_enable_reg_info->
534 PM1Bcontrol = PM1Acontrol;
536 /* Insert SLP_TYP bits */
539 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
542 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
545 /* Just ignore any errors */
547 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
548 ACPI_REGISTER_PM1A_CONTROL,
550 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
551 ACPI_REGISTER_PM1B_CONTROL,
556 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
558 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
560 /* Setup parameter object */
563 arg_list.pointer = &arg;
564 arg.type = ACPI_TYPE_INTEGER;
566 /* Ignore any errors from these methods */
568 arg.integer.value = ACPI_SST_WAKING;
569 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
570 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
571 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
574 arg.integer.value = sleep_state;
575 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
576 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
577 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
580 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
581 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
582 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
584 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
588 * 1) Disable/Clear all GPEs
589 * 2) Enable all runtime GPEs
591 status = acpi_hw_disable_all_gpes();
592 if (ACPI_FAILURE(status)) {
593 return_ACPI_STATUS(status);
595 acpi_gbl_system_awake_and_running = TRUE;
597 status = acpi_hw_enable_all_runtime_gpes();
598 if (ACPI_FAILURE(status)) {
599 return_ACPI_STATUS(status);
602 /* Enable power button */
605 acpi_set_register(acpi_gbl_fixed_event_info
606 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
609 acpi_set_register(acpi_gbl_fixed_event_info
610 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
612 arg.integer.value = ACPI_SST_WORKING;
613 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
614 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
615 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
618 return_ACPI_STATUS(status);
621 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)