blob: 6a30c8095ff1644f2b290fa1b07a46b4018a71e9 [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>
Bob Mooref3d2e782007-02-02 19:48:18 +030046#include <acpi/actables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("hwsleep")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Robert Moore44f6c012005-04-18 22:49:35 -040051/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
53 * FUNCTION: acpi_set_firmware_waking_vector
54 *
Bob Moored85988f2008-11-12 14:54:05 +080055 * PARAMETERS: physical_address - 32-bit physical address of ACPI real mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * entry point.
57 *
58 * RETURN: Status
59 *
Bob Moored85988f2008-11-12 14:54:05 +080060 * DESCRIPTION: Sets the 32-bit firmware_waking_vector field of the FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070063acpi_status
Bob Moored85988f2008-11-12 14:54:05 +080064acpi_set_firmware_waking_vector(u32 physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Bob Mooref3d2e782007-02-02 19:48:18 +030066 struct acpi_table_facs *facs;
67 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Bob Mooreb229cf92006-04-21 17:15:00 -040069 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Bob Mooref3d2e782007-02-02 19:48:18 +030071 /* Get the FACS */
72
Bob Moore1d18c052008-04-10 19:06:40 +040073 status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
74 ACPI_CAST_INDIRECT_PTR(struct
75 acpi_table_header,
76 &facs));
Bob Mooref3d2e782007-02-02 19:48:18 +030077 if (ACPI_FAILURE(status)) {
78 return_ACPI_STATUS(status);
79 }
80
Rafael J. Wysockia6629102008-09-06 13:13:01 +020081 /*
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.
87 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Bob Moored85988f2008-11-12 14:54:05 +080089 /* Set the 32-bit vector */
90
91 facs->firmware_waking_vector = physical_address;
92
93 /* Clear the 64-bit vector if it exists */
94
95 if ((facs->length > 32) && (facs->version >= 1)) {
96 facs->xfirmware_waking_vector = 0;
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Len Brown4be44fc2005-08-05 00:44:28 -040099 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Bob Moore83135242006-10-03 00:00:00 -0400102ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
103
Robert Moore44f6c012005-04-18 22:49:35 -0400104/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
Bob Moored85988f2008-11-12 14:54:05 +0800106 * FUNCTION: acpi_set_firmware_waking_vector64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Bob Moored85988f2008-11-12 14:54:05 +0800108 * PARAMETERS: physical_address - 64-bit physical address of ACPI protected
109 * mode entry point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
Bob Moored85988f2008-11-12 14:54:05 +0800111 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
Bob Moored85988f2008-11-12 14:54:05 +0800113 * DESCRIPTION: Sets the 64-bit X_firmware_waking_vector field of the FACS, if
114 * it exists in the table.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
116 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117acpi_status
Bob Moored85988f2008-11-12 14:54:05 +0800118acpi_set_firmware_waking_vector64(u64 physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Bob Mooref3d2e782007-02-02 19:48:18 +0300120 struct acpi_table_facs *facs;
121 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Bob Moored85988f2008-11-12 14:54:05 +0800123 ACPI_FUNCTION_TRACE(acpi_set_firmware_waking_vector64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Bob Mooref3d2e782007-02-02 19:48:18 +0300126 /* Get the FACS */
127
Bob Moore1d18c052008-04-10 19:06:40 +0400128 status = acpi_get_table_by_index(ACPI_TABLE_INDEX_FACS,
129 ACPI_CAST_INDIRECT_PTR(struct
130 acpi_table_header,
131 &facs));
Bob Mooref3d2e782007-02-02 19:48:18 +0300132 if (ACPI_FAILURE(status)) {
133 return_ACPI_STATUS(status);
134 }
135
Bob Moored85988f2008-11-12 14:54:05 +0800136 /* Determine if the 64-bit vector actually exists */
137
138 if ((facs->length <= 32) || (facs->version < 1)) {
139 return_ACPI_STATUS(AE_NOT_EXIST);
140 }
141
142 /* Clear 32-bit vector, set the 64-bit X_ vector */
143
144 facs->firmware_waking_vector = 0;
145 facs->xfirmware_waking_vector = physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Len Brown4be44fc2005-08-05 00:44:28 -0400147 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
Bob Moore83135242006-10-03 00:00:00 -0400149
Bob Moored85988f2008-11-12 14:54:05 +0800150ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector64)
151
Robert Moore44f6c012005-04-18 22:49:35 -0400152/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 *
154 * FUNCTION: acpi_enter_sleep_state_prep
155 *
156 * PARAMETERS: sleep_state - Which sleep state to enter
157 *
158 * RETURN: Status
159 *
160 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
161 * This function must execute with interrupts enabled.
162 * We break sleeping into 2 stages so that OSPM can handle
163 * various OS-specific tasks between the two steps.
164 *
165 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400166acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Len Brown4be44fc2005-08-05 00:44:28 -0400168 acpi_status status;
169 struct acpi_object_list arg_list;
170 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Bob Mooreb229cf92006-04-21 17:15:00 -0400172 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /*
175 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
176 */
Len Brown4be44fc2005-08-05 00:44:28 -0400177 status = acpi_get_sleep_type_data(sleep_state,
178 &acpi_gbl_sleep_type_a,
179 &acpi_gbl_sleep_type_b);
180 if (ACPI_FAILURE(status)) {
181 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
184 /* Setup parameter object */
185
186 arg_list.count = 1;
187 arg_list.pointer = &arg;
188
189 arg.type = ACPI_TYPE_INTEGER;
190 arg.integer.value = sleep_state;
191
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100192 /* Run the _PTS method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Len Brown4be44fc2005-08-05 00:44:28 -0400194 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
195 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
196 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Setup the argument to _SST */
200
201 switch (sleep_state) {
202 case ACPI_STATE_S0:
203 arg.integer.value = ACPI_SST_WORKING;
204 break;
205
206 case ACPI_STATE_S1:
207 case ACPI_STATE_S2:
208 case ACPI_STATE_S3:
209 arg.integer.value = ACPI_SST_SLEEPING;
210 break;
211
212 case ACPI_STATE_S4:
213 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
214 break;
215
216 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400217 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 }
220
Bob Moored52c79a2008-06-10 14:26:57 +0800221 /*
222 * Set the system indicators to show the desired sleep state.
223 * _SST is an optional method (return no error if not found)
224 */
Len Brown4be44fc2005-08-05 00:44:28 -0400225 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
226 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500227 ACPI_EXCEPTION((AE_INFO, status,
228 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
Bob Moored52c79a2008-06-10 14:26:57 +0800231 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Bob Moore83135242006-10-03 00:00:00 -0400234ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
235
Robert Moore44f6c012005-04-18 22:49:35 -0400236/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
238 * FUNCTION: acpi_enter_sleep_state
239 *
240 * PARAMETERS: sleep_state - Which sleep state to enter
241 *
242 * RETURN: Status
243 *
244 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
245 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
246 *
247 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400248acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Len Brown4be44fc2005-08-05 00:44:28 -0400250 u32 PM1Acontrol;
251 u32 PM1Bcontrol;
252 struct acpi_bit_register_info *sleep_type_reg_info;
253 struct acpi_bit_register_info *sleep_enable_reg_info;
254 u32 in_value;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100255 struct acpi_object_list arg_list;
256 union acpi_object arg;
Len Brown4be44fc2005-08-05 00:44:28 -0400257 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Bob Mooreb229cf92006-04-21 17:15:00 -0400259 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400262 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500263 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
264 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400265 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268 sleep_type_reg_info =
269 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
270 sleep_enable_reg_info =
271 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* Clear wake status */
274
Bob Moored8c71b62007-02-02 19:48:21 +0300275 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400276 if (ACPI_FAILURE(status)) {
277 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
280 /* Clear all fixed and general purpose status bits */
281
Bob Moored8c71b62007-02-02 19:48:21 +0300282 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400283 if (ACPI_FAILURE(status)) {
284 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
287 /*
Pavel Machek23b168d2008-02-05 19:27:12 +0100288 * 1) Disable/Clear all GPEs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * 2) Enable all wakeup GPEs
290 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400291 status = acpi_hw_disable_all_gpes();
292 if (ACPI_FAILURE(status)) {
293 return_ACPI_STATUS(status);
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 acpi_gbl_system_awake_and_running = FALSE;
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297 status = acpi_hw_enable_all_wakeup_gpes();
298 if (ACPI_FAILURE(status)) {
299 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100302 /* Execute the _GTS method */
303
304 arg_list.count = 1;
305 arg_list.pointer = &arg;
306 arg.type = ACPI_TYPE_INTEGER;
307 arg.integer.value = sleep_state;
308
309 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
310 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
311 return_ACPI_STATUS(status);
312 }
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* Get current value of PM1A control */
315
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400316 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
Len Brown4be44fc2005-08-05 00:44:28 -0400317 if (ACPI_FAILURE(status)) {
318 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Len Brown4be44fc2005-08-05 00:44:28 -0400320 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
321 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Clear SLP_EN and SLP_TYP fields */
324
Robert Moore44f6c012005-04-18 22:49:35 -0400325 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400326 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 PM1Bcontrol = PM1Acontrol;
328
329 /* Insert SLP_TYP bits */
330
Len Brown4be44fc2005-08-05 00:44:28 -0400331 PM1Acontrol |=
332 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
333 PM1Bcontrol |=
334 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /*
337 * We split the writes of SLP_TYP and SLP_EN to workaround
338 * poorly implemented hardware.
339 */
340
341 /* Write #1: fill in SLP_TYP data */
342
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400343 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400344 PM1Acontrol);
345 if (ACPI_FAILURE(status)) {
346 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400349 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400350 PM1Bcontrol);
351 if (ACPI_FAILURE(status)) {
352 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 /* Insert SLP_ENABLE bit */
356
357 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
358 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
359
360 /* Write #2: SLP_TYP + SLP_EN */
361
Len Brown4be44fc2005-08-05 00:44:28 -0400362 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400364 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400365 PM1Acontrol);
366 if (ACPI_FAILURE(status)) {
367 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400370 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400371 PM1Bcontrol);
372 if (ACPI_FAILURE(status)) {
373 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 if (sleep_state > ACPI_STATE_S3) {
377 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400378 * We wanted to sleep > S3, but it didn't happen (by virtue of the
379 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *
Robert Moore44f6c012005-04-18 22:49:35 -0400381 * Wait ten seconds, then try again. This is to get S4/S5 to work on
382 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *
384 * We wait so long to allow chipsets that poll this reg very slowly to
385 * still read the right value. Ideally, this block would go
386 * away entirely.
387 */
Len Brown4be44fc2005-08-05 00:44:28 -0400388 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400390 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400391 sleep_enable_reg_info->
392 access_bit_mask);
393 if (ACPI_FAILURE(status)) {
394 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396 }
397
398 /* Wait until we enter sleep state */
399
400 do {
Alexey Starikovskiy2d571b32007-09-30 22:39:42 +0400401 status = acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS,
402 &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400403 if (ACPI_FAILURE(status)) {
404 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 /* Spin until we wake */
408
409 } while (!in_value);
410
Len Brown4be44fc2005-08-05 00:44:28 -0400411 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Bob Moore83135242006-10-03 00:00:00 -0400414ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Robert Moore44f6c012005-04-18 22:49:35 -0400416/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
418 * FUNCTION: acpi_enter_sleep_state_s4bios
419 *
420 * PARAMETERS: None
421 *
422 * RETURN: Status
423 *
424 * DESCRIPTION: Perform a S4 bios request.
425 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
426 *
427 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400428acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Len Brown4be44fc2005-08-05 00:44:28 -0400430 u32 in_value;
431 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Bob Mooreb229cf92006-04-21 17:15:00 -0400433 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Bob Moored8c71b62007-02-02 19:48:21 +0300435 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400436 if (ACPI_FAILURE(status)) {
437 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Bob Moored8c71b62007-02-02 19:48:21 +0300440 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400441 if (ACPI_FAILURE(status)) {
442 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 /*
446 * 1) Disable/Clear all GPEs
447 * 2) Enable all wakeup GPEs
448 */
Len Brown4be44fc2005-08-05 00:44:28 -0400449 status = acpi_hw_disable_all_gpes();
450 if (ACPI_FAILURE(status)) {
451 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 acpi_gbl_system_awake_and_running = FALSE;
454
Len Brown4be44fc2005-08-05 00:44:28 -0400455 status = acpi_hw_enable_all_wakeup_gpes();
456 if (ACPI_FAILURE(status)) {
457 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
Len Brown4be44fc2005-08-05 00:44:28 -0400460 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Bob Mooref3d2e782007-02-02 19:48:18 +0300462 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
463 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 do {
466 acpi_os_stall(1000);
Bob Moored8c71b62007-02-02 19:48:21 +0300467 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400468 if (ACPI_FAILURE(status)) {
469 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 } while (!in_value);
472
Len Brown4be44fc2005-08-05 00:44:28 -0400473 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Bob Moore83135242006-10-03 00:00:00 -0400476ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Robert Moore44f6c012005-04-18 22:49:35 -0400478/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100480 * FUNCTION: acpi_leave_sleep_state_prep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100482 * PARAMETERS: sleep_state - Which sleep state we are exiting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *
484 * RETURN: Status
485 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100486 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
487 * sleep.
488 * Called with interrupts DISABLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 *
490 ******************************************************************************/
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100491acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Len Brown4be44fc2005-08-05 00:44:28 -0400493 struct acpi_object_list arg_list;
494 union acpi_object arg;
495 acpi_status status;
496 struct acpi_bit_register_info *sleep_type_reg_info;
497 struct acpi_bit_register_info *sleep_enable_reg_info;
498 u32 PM1Acontrol;
499 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100501 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 /*
504 * Set SLP_TYPE and SLP_EN to state S0.
505 * This is unclear from the ACPI Spec, but it is required
506 * by some machines.
507 */
Len Brown4be44fc2005-08-05 00:44:28 -0400508 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
509 &acpi_gbl_sleep_type_a,
510 &acpi_gbl_sleep_type_b);
511 if (ACPI_SUCCESS(status)) {
512 sleep_type_reg_info =
513 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
514 sleep_enable_reg_info =
515 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* Get current value of PM1A control */
518
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400519 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400520 &PM1Acontrol);
521 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Clear SLP_EN and SLP_TYP fields */
524
525 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400526 sleep_enable_reg_info->
527 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 PM1Bcontrol = PM1Acontrol;
529
530 /* Insert SLP_TYP bits */
531
Len Brown4be44fc2005-08-05 00:44:28 -0400532 PM1Acontrol |=
533 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
534 bit_position);
535 PM1Bcontrol |=
536 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
537 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 /* Just ignore any errors */
540
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400541 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400542 PM1Acontrol);
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400543 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400544 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 }
547
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100548 /* Execute the _BFS method */
549
550 arg_list.count = 1;
551 arg_list.pointer = &arg;
552 arg.type = ACPI_TYPE_INTEGER;
553 arg.integer.value = sleep_state;
554
555 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
556 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
557 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
558 }
559
560 return_ACPI_STATUS(status);
561}
562
563/*******************************************************************************
564 *
565 * FUNCTION: acpi_leave_sleep_state
566 *
567 * PARAMETERS: sleep_state - Which sleep state we just exited
568 *
569 * RETURN: Status
570 *
571 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
572 * Called with interrupts ENABLED.
573 *
574 ******************************************************************************/
575acpi_status acpi_leave_sleep_state(u8 sleep_state)
576{
577 struct acpi_object_list arg_list;
578 union acpi_object arg;
579 acpi_status status;
580
581 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
584
585 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
586
587 /* Setup parameter object */
588
589 arg_list.count = 1;
590 arg_list.pointer = &arg;
591 arg.type = ACPI_TYPE_INTEGER;
592
593 /* Ignore any errors from these methods */
594
595 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400596 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
597 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500598 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400602 * GPEs must be enabled before _WAK is called as GPEs
603 * might get fired there
604 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * Restore the GPEs:
606 * 1) Disable/Clear all GPEs
607 * 2) Enable all runtime GPEs
608 */
Len Brown4be44fc2005-08-05 00:44:28 -0400609 status = acpi_hw_disable_all_gpes();
610 if (ACPI_FAILURE(status)) {
611 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Len Brown4be44fc2005-08-05 00:44:28 -0400613 status = acpi_hw_enable_all_runtime_gpes();
614 if (ACPI_FAILURE(status)) {
615 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Rafael J. Wysocki314ccd62008-02-13 00:32:16 +0100618 arg.integer.value = sleep_state;
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400619 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
620 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
621 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
622 }
623 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
624
Matthew Garretta68823e2008-08-06 19:12:04 +0100625 /*
626 * Some BIOSes assume that WAK_STS will be cleared on resume and use
627 * it to determine whether the system is rebooting or resuming. Clear
628 * it for compatibility.
629 */
630 acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
631
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400632 acpi_gbl_system_awake_and_running = TRUE;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Enable power button */
635
Len Brown4be44fc2005-08-05 00:44:28 -0400636 (void)
637 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300638 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
Robert Moore44f6c012005-04-18 22:49:35 -0400639
Len Brown4be44fc2005-08-05 00:44:28 -0400640 (void)
641 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300642 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400645 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
646 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500647 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Len Brown4be44fc2005-08-05 00:44:28 -0400650 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
Bob Moore83135242006-10-03 00:00:00 -0400652
653ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)