blob: 202f11af36899b49f18b66baf3f93902e487af5a [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/*
Bob Moore6c9deb72007-02-02 19:48:24 +03009 * Copyright (C) 2000 - 2007, R. Byron Moore
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 *
55 * PARAMETERS: physical_address - Physical address of ACPI real mode
56 * entry point.
57 *
58 * RETURN: Status
59 *
Robert Moore44f6c012005-04-18 22:49:35 -040060 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 ******************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -070063acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040064acpi_set_firmware_waking_vector(acpi_physical_address 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 /* Set the vector */
82
Bob Mooref3d2e782007-02-02 19:48:18 +030083 if ((facs->length < 32) || (!(facs->xfirmware_waking_vector))) {
84 /*
85 * ACPI 1.0 FACS or short table or optional X_ field is zero
86 */
87 facs->firmware_waking_vector = (u32) physical_address;
Len Brown4be44fc2005-08-05 00:44:28 -040088 } else {
Bob Mooref3d2e782007-02-02 19:48:18 +030089 /*
90 * ACPI 2.0 FACS with valid X_ field
91 */
92 facs->xfirmware_waking_vector = physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Len Brown4be44fc2005-08-05 00:44:28 -040095 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Bob Moore83135242006-10-03 00:00:00 -040098ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
99
Robert Moore44f6c012005-04-18 22:49:35 -0400100/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 *
102 * FUNCTION: acpi_get_firmware_waking_vector
103 *
Robert Moore44f6c012005-04-18 22:49:35 -0400104 * PARAMETERS: *physical_address - Where the contents of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * the firmware_waking_vector field of
Robert Moore44f6c012005-04-18 22:49:35 -0400106 * the FACS will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Robert Moore44f6c012005-04-18 22:49:35 -0400108 * RETURN: Status, vector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
Robert Moore44f6c012005-04-18 22:49:35 -0400110 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 *
112 ******************************************************************************/
113#ifdef ACPI_FUTURE_USAGE
114acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400115acpi_get_firmware_waking_vector(acpi_physical_address * physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Bob Mooref3d2e782007-02-02 19:48:18 +0300117 struct acpi_table_facs *facs;
118 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Bob Mooreb229cf92006-04-21 17:15:00 -0400120 ACPI_FUNCTION_TRACE(acpi_get_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (!physical_address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400123 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* Get the vector */
137
Bob Mooref3d2e782007-02-02 19:48:18 +0300138 if ((facs->length < 32) || (!(facs->xfirmware_waking_vector))) {
139 /*
140 * ACPI 1.0 FACS or short table or optional X_ field is zero
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *physical_address =
Bob Mooref3d2e782007-02-02 19:48:18 +0300143 (acpi_physical_address) facs->firmware_waking_vector;
144 } else {
145 /*
146 * ACPI 2.0 FACS with valid X_ field
147 */
148 *physical_address =
149 (acpi_physical_address) facs->xfirmware_waking_vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Len Brown4be44fc2005-08-05 00:44:28 -0400152 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
Bob Moore83135242006-10-03 00:00:00 -0400154
155ACPI_EXPORT_SYMBOL(acpi_get_firmware_waking_vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#endif
Robert Moore44f6c012005-04-18 22:49:35 -0400157/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 *
159 * FUNCTION: acpi_enter_sleep_state_prep
160 *
161 * PARAMETERS: sleep_state - Which sleep state to enter
162 *
163 * RETURN: Status
164 *
165 * DESCRIPTION: Prepare to enter a system sleep state (see ACPI 2.0 spec p 231)
166 * This function must execute with interrupts enabled.
167 * We break sleeping into 2 stages so that OSPM can handle
168 * various OS-specific tasks between the two steps.
169 *
170 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400171acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Len Brown4be44fc2005-08-05 00:44:28 -0400173 acpi_status status;
174 struct acpi_object_list arg_list;
175 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Bob Mooreb229cf92006-04-21 17:15:00 -0400177 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 /*
180 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
181 */
Len Brown4be44fc2005-08-05 00:44:28 -0400182 status = acpi_get_sleep_type_data(sleep_state,
183 &acpi_gbl_sleep_type_a,
184 &acpi_gbl_sleep_type_b);
185 if (ACPI_FAILURE(status)) {
186 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 /* Setup parameter object */
190
191 arg_list.count = 1;
192 arg_list.pointer = &arg;
193
194 arg.type = ACPI_TYPE_INTEGER;
195 arg.integer.value = sleep_state;
196
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100197 /* Run the _PTS method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Len Brown4be44fc2005-08-05 00:44:28 -0400199 status = acpi_evaluate_object(NULL, METHOD_NAME__PTS, &arg_list, NULL);
200 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
201 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Setup the argument to _SST */
205
206 switch (sleep_state) {
207 case ACPI_STATE_S0:
208 arg.integer.value = ACPI_SST_WORKING;
209 break;
210
211 case ACPI_STATE_S1:
212 case ACPI_STATE_S2:
213 case ACPI_STATE_S3:
214 arg.integer.value = ACPI_SST_SLEEPING;
215 break;
216
217 case ACPI_STATE_S4:
218 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
219 break;
220
221 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400222 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 break;
224 }
225
226 /* Set the system indicators to show the desired sleep state. */
227
Len Brown4be44fc2005-08-05 00:44:28 -0400228 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
229 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500230 ACPI_EXCEPTION((AE_INFO, status,
231 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Rafael J. Wysocki6cffd462007-10-02 13:24:09 -0700234 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Bob Moore83135242006-10-03 00:00:00 -0400237ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
238
Robert Moore44f6c012005-04-18 22:49:35 -0400239/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *
241 * FUNCTION: acpi_enter_sleep_state
242 *
243 * PARAMETERS: sleep_state - Which sleep state to enter
244 *
245 * RETURN: Status
246 *
247 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
248 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
249 *
250 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400251acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Len Brown4be44fc2005-08-05 00:44:28 -0400253 u32 PM1Acontrol;
254 u32 PM1Bcontrol;
255 struct acpi_bit_register_info *sleep_type_reg_info;
256 struct acpi_bit_register_info *sleep_enable_reg_info;
257 u32 in_value;
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100258 struct acpi_object_list arg_list;
259 union acpi_object arg;
Len Brown4be44fc2005-08-05 00:44:28 -0400260 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Bob Mooreb229cf92006-04-21 17:15:00 -0400262 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400265 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500266 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
267 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400268 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271 sleep_type_reg_info =
272 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
273 sleep_enable_reg_info =
274 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* Clear wake status */
277
Bob Moored8c71b62007-02-02 19:48:21 +0300278 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400279 if (ACPI_FAILURE(status)) {
280 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
283 /* Clear all fixed and general purpose status bits */
284
Bob Moored8c71b62007-02-02 19:48:21 +0300285 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400286 if (ACPI_FAILURE(status)) {
287 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 /*
Pavel Machek23b168d2008-02-05 19:27:12 +0100291 * 1) Disable/Clear all GPEs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * 2) Enable all wakeup GPEs
293 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400294 status = acpi_hw_disable_all_gpes();
295 if (ACPI_FAILURE(status)) {
296 return_ACPI_STATUS(status);
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 acpi_gbl_system_awake_and_running = FALSE;
299
Len Brown4be44fc2005-08-05 00:44:28 -0400300 status = acpi_hw_enable_all_wakeup_gpes();
301 if (ACPI_FAILURE(status)) {
302 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100305 /* Execute the _GTS method */
306
307 arg_list.count = 1;
308 arg_list.pointer = &arg;
309 arg.type = ACPI_TYPE_INTEGER;
310 arg.integer.value = sleep_state;
311
312 status = acpi_evaluate_object(NULL, METHOD_NAME__GTS, &arg_list, NULL);
313 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
314 return_ACPI_STATUS(status);
315 }
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* Get current value of PM1A control */
318
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400319 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
Len Brown4be44fc2005-08-05 00:44:28 -0400320 if (ACPI_FAILURE(status)) {
321 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Len Brown4be44fc2005-08-05 00:44:28 -0400323 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
324 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* Clear SLP_EN and SLP_TYP fields */
327
Robert Moore44f6c012005-04-18 22:49:35 -0400328 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400329 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 PM1Bcontrol = PM1Acontrol;
331
332 /* Insert SLP_TYP bits */
333
Len Brown4be44fc2005-08-05 00:44:28 -0400334 PM1Acontrol |=
335 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
336 PM1Bcontrol |=
337 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /*
340 * We split the writes of SLP_TYP and SLP_EN to workaround
341 * poorly implemented hardware.
342 */
343
344 /* Write #1: fill in SLP_TYP data */
345
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400346 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400347 PM1Acontrol);
348 if (ACPI_FAILURE(status)) {
349 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400352 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400353 PM1Bcontrol);
354 if (ACPI_FAILURE(status)) {
355 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
358 /* Insert SLP_ENABLE bit */
359
360 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
361 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
362
363 /* Write #2: SLP_TYP + SLP_EN */
364
Len Brown4be44fc2005-08-05 00:44:28 -0400365 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400367 status = acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400368 PM1Acontrol);
369 if (ACPI_FAILURE(status)) {
370 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400373 status = acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400374 PM1Bcontrol);
375 if (ACPI_FAILURE(status)) {
376 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
379 if (sleep_state > ACPI_STATE_S3) {
380 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400381 * We wanted to sleep > S3, but it didn't happen (by virtue of the
382 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 *
Robert Moore44f6c012005-04-18 22:49:35 -0400384 * Wait ten seconds, then try again. This is to get S4/S5 to work on
385 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *
387 * We wait so long to allow chipsets that poll this reg very slowly to
388 * still read the right value. Ideally, this block would go
389 * away entirely.
390 */
Len Brown4be44fc2005-08-05 00:44:28 -0400391 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400393 status = acpi_hw_register_write(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400394 sleep_enable_reg_info->
395 access_bit_mask);
396 if (ACPI_FAILURE(status)) {
397 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 }
400
401 /* Wait until we enter sleep state */
402
403 do {
Alexey Starikovskiy2d571b32007-09-30 22:39:42 +0400404 status = acpi_get_register_unlocked(ACPI_BITREG_WAKE_STATUS,
405 &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400406 if (ACPI_FAILURE(status)) {
407 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 /* Spin until we wake */
411
412 } while (!in_value);
413
Len Brown4be44fc2005-08-05 00:44:28 -0400414 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Bob Moore83135242006-10-03 00:00:00 -0400417ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Robert Moore44f6c012005-04-18 22:49:35 -0400419/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
421 * FUNCTION: acpi_enter_sleep_state_s4bios
422 *
423 * PARAMETERS: None
424 *
425 * RETURN: Status
426 *
427 * DESCRIPTION: Perform a S4 bios request.
428 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
429 *
430 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400431acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Len Brown4be44fc2005-08-05 00:44:28 -0400433 u32 in_value;
434 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Bob Mooreb229cf92006-04-21 17:15:00 -0400436 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Bob Moored8c71b62007-02-02 19:48:21 +0300438 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400439 if (ACPI_FAILURE(status)) {
440 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Bob Moored8c71b62007-02-02 19:48:21 +0300443 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400444 if (ACPI_FAILURE(status)) {
445 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
448 /*
449 * 1) Disable/Clear all GPEs
450 * 2) Enable all wakeup GPEs
451 */
Len Brown4be44fc2005-08-05 00:44:28 -0400452 status = acpi_hw_disable_all_gpes();
453 if (ACPI_FAILURE(status)) {
454 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 acpi_gbl_system_awake_and_running = FALSE;
457
Len Brown4be44fc2005-08-05 00:44:28 -0400458 status = acpi_hw_enable_all_wakeup_gpes();
459 if (ACPI_FAILURE(status)) {
460 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
Len Brown4be44fc2005-08-05 00:44:28 -0400463 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Bob Mooref3d2e782007-02-02 19:48:18 +0300465 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
466 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 do {
469 acpi_os_stall(1000);
Bob Moored8c71b62007-02-02 19:48:21 +0300470 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400471 if (ACPI_FAILURE(status)) {
472 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 } while (!in_value);
475
Len Brown4be44fc2005-08-05 00:44:28 -0400476 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Bob Moore83135242006-10-03 00:00:00 -0400479ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Robert Moore44f6c012005-04-18 22:49:35 -0400481/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100483 * FUNCTION: acpi_leave_sleep_state_prep
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100485 * PARAMETERS: sleep_state - Which sleep state we are exiting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *
487 * RETURN: Status
488 *
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100489 * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
490 * sleep.
491 * Called with interrupts DISABLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 *
493 ******************************************************************************/
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100494acpi_status acpi_leave_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Len Brown4be44fc2005-08-05 00:44:28 -0400496 struct acpi_object_list arg_list;
497 union acpi_object arg;
498 acpi_status status;
499 struct acpi_bit_register_info *sleep_type_reg_info;
500 struct acpi_bit_register_info *sleep_enable_reg_info;
501 u32 PM1Acontrol;
502 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100504 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * Set SLP_TYPE and SLP_EN to state S0.
508 * This is unclear from the ACPI Spec, but it is required
509 * by some machines.
510 */
Len Brown4be44fc2005-08-05 00:44:28 -0400511 status = acpi_get_sleep_type_data(ACPI_STATE_S0,
512 &acpi_gbl_sleep_type_a,
513 &acpi_gbl_sleep_type_b);
514 if (ACPI_SUCCESS(status)) {
515 sleep_type_reg_info =
516 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
517 sleep_enable_reg_info =
518 acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* Get current value of PM1A control */
521
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400522 status = acpi_hw_register_read(ACPI_REGISTER_PM1_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400523 &PM1Acontrol);
524 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Clear SLP_EN and SLP_TYP fields */
527
528 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400529 sleep_enable_reg_info->
530 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 PM1Bcontrol = PM1Acontrol;
532
533 /* Insert SLP_TYP bits */
534
Len Brown4be44fc2005-08-05 00:44:28 -0400535 PM1Acontrol |=
536 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
537 bit_position);
538 PM1Bcontrol |=
539 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
540 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* Just ignore any errors */
543
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400544 (void)acpi_hw_register_write(ACPI_REGISTER_PM1A_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400545 PM1Acontrol);
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400546 (void)acpi_hw_register_write(ACPI_REGISTER_PM1B_CONTROL,
Len Brown4be44fc2005-08-05 00:44:28 -0400547 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 }
550
Rafael J. Wysockic95d47a2008-01-08 00:05:21 +0100551 /* Execute the _BFS method */
552
553 arg_list.count = 1;
554 arg_list.pointer = &arg;
555 arg.type = ACPI_TYPE_INTEGER;
556 arg.integer.value = sleep_state;
557
558 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
559 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
560 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
561 }
562
563 return_ACPI_STATUS(status);
564}
565
566/*******************************************************************************
567 *
568 * FUNCTION: acpi_leave_sleep_state
569 *
570 * PARAMETERS: sleep_state - Which sleep state we just exited
571 *
572 * RETURN: Status
573 *
574 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
575 * Called with interrupts ENABLED.
576 *
577 ******************************************************************************/
578acpi_status acpi_leave_sleep_state(u8 sleep_state)
579{
580 struct acpi_object_list arg_list;
581 union acpi_object arg;
582 acpi_status status;
583
584 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
587
588 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
589
590 /* Setup parameter object */
591
592 arg_list.count = 1;
593 arg_list.pointer = &arg;
594 arg.type = ACPI_TYPE_INTEGER;
595
596 /* Ignore any errors from these methods */
597
598 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400599 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
600 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500601 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /*
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400605 * GPEs must be enabled before _WAK is called as GPEs
606 * might get fired there
607 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Restore the GPEs:
609 * 1) Disable/Clear all GPEs
610 * 2) Enable all runtime GPEs
611 */
Len Brown4be44fc2005-08-05 00:44:28 -0400612 status = acpi_hw_disable_all_gpes();
613 if (ACPI_FAILURE(status)) {
614 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Len Brown4be44fc2005-08-05 00:44:28 -0400616 status = acpi_hw_enable_all_runtime_gpes();
617 if (ACPI_FAILURE(status)) {
618 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Rafael J. Wysocki314ccd62008-02-13 00:32:16 +0100621 arg.integer.value = sleep_state;
Thomas Renninger79d2dfa2007-08-24 01:24:47 -0400622 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
623 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
624 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
625 }
626 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
627
628 acpi_gbl_system_awake_and_running = TRUE;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Enable power button */
631
Len Brown4be44fc2005-08-05 00:44:28 -0400632 (void)
633 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300634 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
Robert Moore44f6c012005-04-18 22:49:35 -0400635
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].status_register_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400641 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
642 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500643 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Len Brown4be44fc2005-08-05 00:44:28 -0400646 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
Bob Moore83135242006-10-03 00:00:00 -0400648
649ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)