blob: db307a356f08b02edeb73597cc5893c1d3d45e9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/******************************************************************************
3 *
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
5 *
6 *****************************************************************************/
7
8/*
Len Brown75a44ce2008-04-23 23:00:13 -04009 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
26 *
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.
30 *
31 * NO WARRANTY
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.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "actables.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("hwsleep")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Robert Moore44f6c012005-04-18 22:49:35 -040052/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 *
54 * FUNCTION: acpi_set_firmware_waking_vector
55 *
Bob Moored85988f2008-11-12 14:54:05 +080056 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * entry point.
58 *
59 * RETURN: Status
60 *
Bob Moored85988f2008-11-12 14:54:05 +080061 * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070064acpi_status
Bob Moored85988f2008-11-12 14:54:05 +080065acpi_set_firmware_waking_vector(u32 physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Bob Mooreb229cf92006-04-21 17:15:00 -040067 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bob Mooref3d2e782007-02-02 19:48:18 +030069
Rafael J. Wysockia6629102008-09-06 13:13:01 +020070 /*
71 * According to the ACPI specification 2.0c and later, the 64-bit
72 * waking vector should be cleared and the 32-bit waking vector should
73 * be used, unless we want the wake-up code to be called by the BIOS in
74 * Protected Mode. Some systems (for example HP dv5-1004nr) are known
75 * to fail to resume if the 64-bit vector is used.
76 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Bob Moored85988f2008-11-12 14:54:05 +080078 /* Set the 32-bit vector */
79
Bob Moore009c4cbe2008-11-12 15:34:52 +080080 acpi_gbl_FACS->firmware_waking_vector = physical_address;
Bob Moored85988f2008-11-12 14:54:05 +080081
82 /* Clear the 64-bit vector if it exists */
83
Bob Moore009c4cbe2008-11-12 15:34:52 +080084 if ((acpi_gbl_FACS->length > 32) && (acpi_gbl_FACS->version >= 1)) {
85 acpi_gbl_FACS->xfirmware_waking_vector = 0;
Bob Moored85988f2008-11-12 14:54:05 +080086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Len Brown4be44fc2005-08-05 00:44:28 -040088 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Bob Moore83135242006-10-03 00:00:00 -040091ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
92
Bob Moore4f70e372009-02-18 14:52:43 +080093#if ACPI_MACHINE_WIDTH == 64
Robert Moore44f6c012005-04-18 22:49:35 -040094/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
Bob Moored85988f2008-11-12 14:54:05 +080096 * FUNCTION: acpi_set_firmware_waking_vector64
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Bob Moored85988f2008-11-12 14:54:05 +080098 * PARAMETERS: physical_address - 64-bit physical address of ACPI protected
99 * mode entry point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
Bob Moored85988f2008-11-12 14:54:05 +0800101 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
Bob Moored85988f2008-11-12 14:54:05 +0800103 * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
Bob Moore4f70e372009-02-18 14:52:43 +0800104 * it exists in the table. This function is intended for use with
105 * 64-bit host operating systems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
107 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108acpi_status
Bob Moored85988f2008-11-12 14:54:05 +0800109acpi_set_firmware_waking_vector64(u64 physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Bob Moored85988f2008-11-12 14:54:05 +0800111 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Bob Moored85988f2008-11-12 14:54:05 +0800114 /* Determine if the 64-bit vector actually exists */
115
Bob Moore009c4cbe2008-11-12 15:34:52 +0800116 if ((acpi_gbl_FACS->length <= 32) || (acpi_gbl_FACS->version < 1)) {
Bob Moored85988f2008-11-12 14:54:05 +0800117 return_ACPI_STATUS(AE_NOT_EXIST);
118 }
119
120 /* Clear 32-bit vector, set the 64-bit X_ vector */
121
Bob Moore009c4cbe2008-11-12 15:34:52 +0800122 acpi_gbl_FACS->firmware_waking_vector = 0;
123 acpi_gbl_FACS->xfirmware_waking_vector = physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Len Brown4be44fc2005-08-05 00:44:28 -0400125 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
Bob Moore83135242006-10-03 00:00:00 -0400127
Bob Moored85988f2008-11-12 14:54:05 +0800128ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64)
Bob Moore4f70e372009-02-18 14:52:43 +0800129#endif
Bob Moored85988f2008-11-12 14:54:05 +0800130
Robert Moore44f6c012005-04-18 22:49:35 -0400131/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 *
133 * FUNCTION: acpi_enter_sleep_state_prep
134 *
135 * PARAMETERS: sleep_state - Which sleep state to enter
136 *
137 * RETURN: Status
138 *
139 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
140 * This function must execute with interrupts enabled.
141 * We break sleeping into 2 stages so that OSPM can handle
142 * various OS-specific tasks between the two steps.
143 *
144 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400145acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Len Brown4be44fc2005-08-05 00:44:28 -0400147 acpi_status status;
148 struct acpi_object_list arg_list;
149 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Bob Mooreb229cf92006-04-21 17:15:00 -0400151 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Bob Moore32c9ef92009-02-18 14:36:05 +0800153 /* _PSW methods could be run here to enable wake-on keyboard, LAN, etc. */
154
Len Brown4be44fc2005-08-05 00:44:28 -0400155 status = acpi_get_sleep_type_data(sleep_state,
156 &acpi_gbl_sleep_type_a,
157 &acpi_gbl_sleep_type_b);
158 if (ACPI_FAILURE(status)) {
159 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 /* Setup parameter object */
163
164 arg_list.count = 1;
165 arg_list.pointer = &arg;
166
167 arg.type = ACPI_TYPE_INTEGER;
168 arg.integer.value = sleep_state;
169
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100170 /* Run the _PTS method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Len Brown4be44fc2005-08-05 00:44:28 -0400172 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
173 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
174 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* Setup the argument to _SST */
178
179 switch (sleep_state) {
180 case ACPI_STATE_S0:
181 arg.integer.value = ACPI_SST_WORKING;
182 break;
183
184 case ACPI_STATE_S1:
185 case ACPI_STATE_S2:
186 case ACPI_STATE_S3:
187 arg.integer.value = ACPI_SST_SLEEPING;
188 break;
189
190 case ACPI_STATE_S4:
191 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
192 break;
193
194 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400195 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 }
198
Bob Moored52c79a2008-06-10 14:26:57 +0800199 /*
200 * Set the system indicators to show the desired sleep state.
201 * _SST is an optional method (return no error if not found)
202 */
Len Brown4be44fc2005-08-05 00:44:28 -0400203 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
204 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500205 ACPI_EXCEPTION((AE_INFO, status,
206 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Bob Moored52c79a2008-06-10 14:26:57 +0800209 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Bob Moore83135242006-10-03 00:00:00 -0400212ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
213
Len Brown96f15ef2009-04-17 23:32:20 -0400214static unsigned int gts, bfs;
215module_param(gts, uint, 0644);
216module_param(bfs, uint, 0644);
217MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend.");
218MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".);
219
Robert Moore44f6c012005-04-18 22:49:35 -0400220/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
222 * FUNCTION: acpi_enter_sleep_state
223 *
224 * PARAMETERS: sleep_state - Which sleep state to enter
225 *
226 * RETURN: Status
227 *
228 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
229 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
230 *
231 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400232acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Bob Moore32c9ef92009-02-18 14:36:05 +0800234 u32 pm1a_control;
235 u32 pm1b_control;
Len Brown4be44fc2005-08-05 00:44:28 -0400236 struct acpi_bit_register_info *sleep_type_reg_info;
237 struct acpi_bit_register_info *sleep_enable_reg_info;
238 u32 in_value;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100239 struct acpi_object_list arg_list;
240 union acpi_object arg;
Len Brown4be44fc2005-08-05 00:44:28 -0400241 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Bob Mooreb229cf92006-04-21 17:15:00 -0400243 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400246 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500247 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
248 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400249 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Len Brown4be44fc2005-08-05 00:44:28 -0400252 sleep_type_reg_info =
Bob Moore82d79b82009-02-18 14:31:05 +0800253 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
Len Brown4be44fc2005-08-05 00:44:28 -0400254 sleep_enable_reg_info =
255 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* Clear wake status */
258
Bob Moore768aaaf2009-03-06 09:49:25 +0800259 status =
260 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
Len Brown4be44fc2005-08-05 00:44:28 -0400261 if (ACPI_FAILURE(status)) {
262 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 /* Clear all fixed and general purpose status bits */
266
Bob Moored8c71b62007-02-02 19:48:21 +0300267 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400268 if (ACPI_FAILURE(status)) {
269 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
272 /*
Pavel Machek23b168d2008-02-05 19:27:12 +0100273 * 1) Disable/Clear all GPEs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * 2) Enable all wakeup GPEs
275 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400276 status = acpi_hw_disable_all_gpes();
277 if (ACPI_FAILURE(status)) {
278 return_ACPI_STATUS(status);
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 acpi_gbl_system_awake_and_running = FALSE;
281
Len Brown4be44fc2005-08-05 00:44:28 -0400282 status = acpi_hw_enable_all_wakeup_gpes();
283 if (ACPI_FAILURE(status)) {
284 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Len Brown96f15ef2009-04-17 23:32:20 -0400287 if (gts) {
288 /* Execute the _GTS method */
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100289
Len Brown96f15ef2009-04-17 23:32:20 -0400290 arg_list.count = 1;
291 arg_list.pointer = &arg;
292 arg.type = ACPI_TYPE_INTEGER;
293 arg.integer.value = sleep_state;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100294
Len Brown96f15ef2009-04-17 23:32:20 -0400295 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
296 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
297 return_ACPI_STATUS(status);
298 }
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100299 }
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* Get current value of PM1A control */
302
Bob Moore32c9ef92009-02-18 14:36:05 +0800303 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
304 &pm1a_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400305 if (ACPI_FAILURE(status)) {
306 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Len Brown4be44fc2005-08-05 00:44:28 -0400308 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
309 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Bob Moore32c9ef92009-02-18 14:36:05 +0800311 /* Clear the SLP_EN and SLP_TYP fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Bob Moore32c9ef92009-02-18 14:36:05 +0800313 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
314 sleep_enable_reg_info->access_bit_mask);
315 pm1b_control = pm1a_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Bob Moore32c9ef92009-02-18 14:36:05 +0800317 /* Insert the SLP_TYP bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Bob Moore32c9ef92009-02-18 14:36:05 +0800319 pm1a_control |=
Len Brown4be44fc2005-08-05 00:44:28 -0400320 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
Bob Moore32c9ef92009-02-18 14:36:05 +0800321 pm1b_control |=
Len Brown4be44fc2005-08-05 00:44:28 -0400322 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /*
325 * We split the writes of SLP_TYP and SLP_EN to workaround
326 * poorly implemented hardware.
327 */
328
Bob Moore32c9ef92009-02-18 14:36:05 +0800329 /* Write #1: write the SLP_TYP data to the PM1 Control registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Bob Moore32c9ef92009-02-18 14:36:05 +0800331 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400332 if (ACPI_FAILURE(status)) {
333 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Bob Moore32c9ef92009-02-18 14:36:05 +0800336 /* Insert the sleep enable (SLP_EN) bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Bob Moore32c9ef92009-02-18 14:36:05 +0800338 pm1a_control |= sleep_enable_reg_info->access_bit_mask;
339 pm1b_control |= sleep_enable_reg_info->access_bit_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Bob Moore32c9ef92009-02-18 14:36:05 +0800341 /* Flush caches, as per ACPI specification */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Len Brown4be44fc2005-08-05 00:44:28 -0400343 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Bob Moore32c9ef92009-02-18 14:36:05 +0800345 /* Write #2: Write both SLP_TYP + SLP_EN */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Bob Moore32c9ef92009-02-18 14:36:05 +0800347 status = acpi_hw_write_pm1_control(pm1a_control, pm1b_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400348 if (ACPI_FAILURE(status)) {
349 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 if (sleep_state > ACPI_STATE_S3) {
353 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400354 * We wanted to sleep > S3, but it didn't happen (by virtue of the
355 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 *
Robert Moore44f6c012005-04-18 22:49:35 -0400357 * Wait ten seconds, then try again. This is to get S4/S5 to work on
358 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 *
Bob Moored4913dc2009-03-06 10:05:18 +0800360 * We wait so long to allow chipsets that poll this reg very slowly
361 * to still read the right value. Ideally, this block would go
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * away entirely.
363 */
Len Brown4be44fc2005-08-05 00:44:28 -0400364 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400366 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400367 sleep_enable_reg_info->
368 access_bit_mask);
369 if (ACPI_FAILURE(status)) {
370 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372 }
373
374 /* Wait until we enter sleep state */
375
376 do {
Bob Moore50ffba12009-02-23 15:02:07 +0800377 status = acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS,
Alexey Starikovskiy2d571b32007-09-30 22:39:42 +0400378 &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400379 if (ACPI_FAILURE(status)) {
380 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383 /* Spin until we wake */
384
385 } while (!in_value);
386
Len Brown4be44fc2005-08-05 00:44:28 -0400387 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Bob Moore83135242006-10-03 00:00:00 -0400390ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Robert Moore44f6c012005-04-18 22:49:35 -0400392/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 *
394 * FUNCTION: acpi_enter_sleep_state_s4bios
395 *
396 * PARAMETERS: None
397 *
398 * RETURN: Status
399 *
400 * DESCRIPTION: Perform a S4 bios request.
401 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
402 *
403 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400404acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Len Brown4be44fc2005-08-05 00:44:28 -0400406 u32 in_value;
407 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Bob Mooreb229cf92006-04-21 17:15:00 -0400409 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Bob Moore768aaaf2009-03-06 09:49:25 +0800411 /* Clear the wake status bit (PM1) */
412
413 status =
414 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
Len Brown4be44fc2005-08-05 00:44:28 -0400415 if (ACPI_FAILURE(status)) {
416 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Bob Moored8c71b62007-02-02 19:48:21 +0300419 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400420 if (ACPI_FAILURE(status)) {
421 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 /*
425 * 1) Disable/Clear all GPEs
426 * 2) Enable all wakeup GPEs
427 */
Len Brown4be44fc2005-08-05 00:44:28 -0400428 status = acpi_hw_disable_all_gpes();
429 if (ACPI_FAILURE(status)) {
430 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 acpi_gbl_system_awake_and_running = FALSE;
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 status = acpi_hw_enable_all_wakeup_gpes();
435 if (ACPI_FAILURE(status)) {
436 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
Len Brown4be44fc2005-08-05 00:44:28 -0400439 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Bob Moore7f071902009-03-19 09:37:47 +0800441 status = acpi_hw_write_port(acpi_gbl_FADT.smi_command,
Bob Mooref3d2e782007-02-02 19:48:18 +0300442 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 do {
445 acpi_os_stall(1000);
Bob Moore50ffba12009-02-23 15:02:07 +0800446 status =
447 acpi_read_bit_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400448 if (ACPI_FAILURE(status)) {
449 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 } while (!in_value);
452
Len Brown4be44fc2005-08-05 00:44:28 -0400453 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Bob Moore83135242006-10-03 00:00:00 -0400456ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Robert Moore44f6c012005-04-18 22:49:35 -0400458/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100460 * FUNCTION: acpi_leave_sleep_state_prep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100462 * PARAMETERS: sleep_state - Which sleep state we are exiting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
464 * RETURN: Status
465 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100466 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
467 * sleep.
468 * Called with interrupts DISABLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *
470 ******************************************************************************/
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100471acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Len Brown4be44fc2005-08-05 00:44:28 -0400473 struct acpi_object_list arg_list;
474 union acpi_object arg;
475 acpi_status status;
476 struct acpi_bit_register_info *sleep_type_reg_info;
477 struct acpi_bit_register_info *sleep_enable_reg_info;
Bob Moore32c9ef92009-02-18 14:36:05 +0800478 u32 pm1a_control;
479 u32 pm1b_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100481 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /*
484 * Set SLP_TYPE and SLP_EN to state S0.
485 * This is unclear from the ACPI Spec, but it is required
486 * by some machines.
487 */
Len Brown4be44fc2005-08-05 00:44:28 -0400488 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
489 &acpi_gbl_sleep_type_a,
490 &acpi_gbl_sleep_type_b);
491 if (ACPI_SUCCESS(status)) {
492 sleep_type_reg_info =
Bob Moore82d79b82009-02-18 14:31:05 +0800493 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE);
Len Brown4be44fc2005-08-05 00:44:28 -0400494 sleep_enable_reg_info =
495 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* Get current value of PM1A control */
498
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400499 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Bob Moore32c9ef92009-02-18 14:36:05 +0800500 &pm1a_control);
Len Brown4be44fc2005-08-05 00:44:28 -0400501 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400502
Bob Moore32c9ef92009-02-18 14:36:05 +0800503 /* Clear the SLP_EN and SLP_TYP fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Bob Moore32c9ef92009-02-18 14:36:05 +0800505 pm1a_control &= ~(sleep_type_reg_info->access_bit_mask |
506 sleep_enable_reg_info->
507 access_bit_mask);
508 pm1b_control = pm1a_control;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Bob Moore32c9ef92009-02-18 14:36:05 +0800510 /* Insert the SLP_TYP bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Bob Moored4913dc2009-03-06 10:05:18 +0800512 pm1a_control |= (acpi_gbl_sleep_type_a <<
513 sleep_type_reg_info->bit_position);
514 pm1b_control |= (acpi_gbl_sleep_type_b <<
515 sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Bob Moore32c9ef92009-02-18 14:36:05 +0800517 /* Write the control registers and ignore any errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Bob Moore32c9ef92009-02-18 14:36:05 +0800519 (void)acpi_hw_write_pm1_control(pm1a_control,
520 pm1b_control);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 }
523
Len Brown96f15ef2009-04-17 23:32:20 -0400524 if (bfs) {
525 /* Execute the _BFS method */
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100526
Len Brown96f15ef2009-04-17 23:32:20 -0400527 arg_list.count = 1;
528 arg_list.pointer = &arg;
529 arg.type = ACPI_TYPE_INTEGER;
530 arg.integer.value = sleep_state;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100531
Len Brown96f15ef2009-04-17 23:32:20 -0400532 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
533 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
534 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
535 }
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100536 }
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100537 return_ACPI_STATUS(status);
538}
539
540/*******************************************************************************
541 *
542 * FUNCTION: acpi_leave_sleep_state
543 *
544 * PARAMETERS: sleep_state - Which sleep state we just exited
545 *
546 * RETURN: Status
547 *
548 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
549 * Called with interrupts ENABLED.
550 *
551 ******************************************************************************/
552acpi_status acpi_leave_sleep_state(u8 sleep_state)
553{
554 struct acpi_object_list arg_list;
555 union acpi_object arg;
556 acpi_status status;
557
558 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
561
562 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
563
564 /* Setup parameter object */
565
566 arg_list.count = 1;
567 arg_list.pointer = &arg;
568 arg.type = ACPI_TYPE_INTEGER;
569
570 /* Ignore any errors from these methods */
571
572 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400573 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
574 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500575 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400579 * GPEs must be enabled before _WAK is called as GPEs
580 * might get fired there
581 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 * Restore the GPEs:
583 * 1) Disable/Clear all GPEs
584 * 2) Enable all runtime GPEs
585 */
Len Brown4be44fc2005-08-05 00:44:28 -0400586 status = acpi_hw_disable_all_gpes();
587 if (ACPI_FAILURE(status)) {
588 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Len Brown4be44fc2005-08-05 00:44:28 -0400590 status = acpi_hw_enable_all_runtime_gpes();
591 if (ACPI_FAILURE(status)) {
592 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Rafael J. Wysocki314ccd62008-02-13 00:32:16 +0100595 arg.integer.value = sleep_state;
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400596 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
597 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
598 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
599 }
600 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
601
Matthew Garretta68823e2008-08-06 19:12:04 +0100602 /*
603 * Some BIOSes assume that WAK_STS will be cleared on resume and use
604 * it to determine whether the system is rebooting or resuming. Clear
605 * it for compatibility.
606 */
Bob Moore50ffba12009-02-23 15:02:07 +0800607 acpi_write_bit_register(ACPI_BITREG_WAKE_STATUS, 1);
Matthew Garretta68823e2008-08-06 19:12:04 +0100608
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400609 acpi_gbl_system_awake_and_running = TRUE;
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Enable power button */
612
Len Brown4be44fc2005-08-05 00:44:28 -0400613 (void)
Bob Moore50ffba12009-02-23 15:02:07 +0800614 acpi_write_bit_register(acpi_gbl_fixed_event_info
Bob Moore768aaaf2009-03-06 09:49:25 +0800615 [ACPI_EVENT_POWER_BUTTON].
616 enable_register_id, ACPI_ENABLE_EVENT);
Robert Moore44f6c012005-04-18 22:49:35 -0400617
Len Brown4be44fc2005-08-05 00:44:28 -0400618 (void)
Bob Moore50ffba12009-02-23 15:02:07 +0800619 acpi_write_bit_register(acpi_gbl_fixed_event_info
Bob Moore768aaaf2009-03-06 09:49:25 +0800620 [ACPI_EVENT_POWER_BUTTON].
621 status_register_id, ACPI_CLEAR_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400624 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
625 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500626 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Len Brown4be44fc2005-08-05 00:44:28 -0400629 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
Bob Moore83135242006-10-03 00:00:00 -0400631
632ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)