blob: c84b1faba28cf3900001c78f2b0700975faf4d61 [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
73 status =
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);
78 }
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* Set the vector */
81
Bob Mooref3d2e782007-02-02 19:48:18 +030082 if ((facs->length < 32) || (!(facs->xfirmware_waking_vector))) {
83 /*
84 * ACPI 1.0 FACS or short table or optional X_ field is zero
85 */
86 facs->firmware_waking_vector = (u32) physical_address;
Len Brown4be44fc2005-08-05 00:44:28 -040087 } else {
Bob Mooref3d2e782007-02-02 19:48:18 +030088 /*
89 * ACPI 2.0 FACS with valid X_ field
90 */
91 facs->xfirmware_waking_vector = physical_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Len Brown4be44fc2005-08-05 00:44:28 -040094 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Bob Moore83135242006-10-03 00:00:00 -040097ACPI_EXPORT_SYMBOL(acpi_set_firmware_waking_vector)
98
Robert Moore44f6c012005-04-18 22:49:35 -040099/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 * FUNCTION: acpi_get_firmware_waking_vector
102 *
Robert Moore44f6c012005-04-18 22:49:35 -0400103 * PARAMETERS: *physical_address - Where the contents of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * the firmware_waking_vector field of
Robert Moore44f6c012005-04-18 22:49:35 -0400105 * the FACS will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 *
Robert Moore44f6c012005-04-18 22:49:35 -0400107 * RETURN: Status, vector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Robert Moore44f6c012005-04-18 22:49:35 -0400109 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *
111 ******************************************************************************/
112#ifdef ACPI_FUTURE_USAGE
113acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400114acpi_get_firmware_waking_vector(acpi_physical_address * physical_address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Bob Mooref3d2e782007-02-02 19:48:18 +0300116 struct acpi_table_facs *facs;
117 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Bob Mooreb229cf92006-04-21 17:15:00 -0400119 ACPI_FUNCTION_TRACE(acpi_get_firmware_waking_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 if (!physical_address) {
Len Brown4be44fc2005-08-05 00:44:28 -0400122 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Bob Mooref3d2e782007-02-02 19:48:18 +0300125 /* Get the FACS */
126
127 status =
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);
132 }
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 /* Get the vector */
135
Bob Mooref3d2e782007-02-02 19:48:18 +0300136 if ((facs->length < 32) || (!(facs->xfirmware_waking_vector))) {
137 /*
138 * ACPI 1.0 FACS or short table or optional X_ field is zero
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *physical_address =
Bob Mooref3d2e782007-02-02 19:48:18 +0300141 (acpi_physical_address) facs->firmware_waking_vector;
142 } else {
143 /*
144 * ACPI 2.0 FACS with valid X_ field
145 */
146 *physical_address =
147 (acpi_physical_address) facs->xfirmware_waking_vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
Len Brown4be44fc2005-08-05 00:44:28 -0400150 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Bob Moore83135242006-10-03 00:00:00 -0400152
153ACPI_EXPORT_SYMBOL(acpi_get_firmware_waking_vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#endif
155
Robert Moore44f6c012005-04-18 22:49:35 -0400156/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *
158 * FUNCTION: acpi_enter_sleep_state_prep
159 *
160 * PARAMETERS: sleep_state - Which sleep state to enter
161 *
162 * RETURN: Status
163 *
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.
168 *
169 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400170acpi_status acpi_enter_sleep_state_prep(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Len Brown4be44fc2005-08-05 00:44:28 -0400172 acpi_status status;
173 struct acpi_object_list arg_list;
174 union acpi_object arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Bob Mooreb229cf92006-04-21 17:15:00 -0400176 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /*
179 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
180 */
Len Brown4be44fc2005-08-05 00:44:28 -0400181 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
188 /* Setup parameter object */
189
190 arg_list.count = 1;
191 arg_list.pointer = &arg;
192
193 arg.type = ACPI_TYPE_INTEGER;
194 arg.integer.value = sleep_state;
195
196 /* Run the _PTS and _GTS methods */
197
Len Brown4be44fc2005-08-05 00:44:28 -0400198 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
Len Brown4be44fc2005-08-05 00:44:28 -0400203 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207
208 /* Setup the argument to _SST */
209
210 switch (sleep_state) {
211 case ACPI_STATE_S0:
212 arg.integer.value = ACPI_SST_WORKING;
213 break;
214
215 case ACPI_STATE_S1:
216 case ACPI_STATE_S2:
217 case ACPI_STATE_S3:
218 arg.integer.value = ACPI_SST_SLEEPING;
219 break;
220
221 case ACPI_STATE_S4:
222 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
223 break;
224
225 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400226 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228 }
229
230 /* Set the system indicators to show the desired sleep state. */
231
Len Brown4be44fc2005-08-05 00:44:28 -0400232 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
233 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500234 ACPI_EXCEPTION((AE_INFO, status,
235 "While executing method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Alexey Starikovskiyed41dab2007-02-10 01:30:35 -0500238 /*
239 * 1) Disable/Clear all GPEs
240 */
241 status = acpi_hw_disable_all_gpes();
242 if (ACPI_FAILURE(status)) {
243 return_ACPI_STATUS(status);
244 }
245
Len Brown4be44fc2005-08-05 00:44:28 -0400246 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Bob Moore83135242006-10-03 00:00:00 -0400249ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
250
Robert Moore44f6c012005-04-18 22:49:35 -0400251/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
253 * FUNCTION: acpi_enter_sleep_state
254 *
255 * PARAMETERS: sleep_state - Which sleep state to enter
256 *
257 * RETURN: Status
258 *
259 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
260 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
261 *
262 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400263acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Len Brown4be44fc2005-08-05 00:44:28 -0400265 u32 PM1Acontrol;
266 u32 PM1Bcontrol;
267 struct acpi_bit_register_info *sleep_type_reg_info;
268 struct acpi_bit_register_info *sleep_enable_reg_info;
269 u32 in_value;
270 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Bob Mooreb229cf92006-04-21 17:15:00 -0400272 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400275 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500276 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
277 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
Len Brown4be44fc2005-08-05 00:44:28 -0400278 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
Len Brown4be44fc2005-08-05 00:44:28 -0400281 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* Clear wake status */
287
Bob Moored8c71b62007-02-02 19:48:21 +0300288 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400289 if (ACPI_FAILURE(status)) {
290 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293 /* Clear all fixed and general purpose status bits */
294
Bob Moored8c71b62007-02-02 19:48:21 +0300295 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400296 if (ACPI_FAILURE(status)) {
297 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
300 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 * 2) Enable all wakeup GPEs
302 */
Alexey Starikovskiy1d999672007-03-12 14:49:26 -0400303 status = acpi_hw_disable_all_gpes();
304 if (ACPI_FAILURE(status)) {
305 return_ACPI_STATUS(status);
306 }
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 acpi_gbl_system_awake_and_running = FALSE;
309
Len Brown4be44fc2005-08-05 00:44:28 -0400310 status = acpi_hw_enable_all_wakeup_gpes();
311 if (ACPI_FAILURE(status)) {
312 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 /* Get current value of PM1A control */
316
Len Brown4be44fc2005-08-05 00:44:28 -0400317 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Len Brown4be44fc2005-08-05 00:44:28 -0400322 ACPI_DEBUG_PRINT((ACPI_DB_INIT,
323 "Entering sleep state [S%d]\n", sleep_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Clear SLP_EN and SLP_TYP fields */
326
Robert Moore44f6c012005-04-18 22:49:35 -0400327 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400328 sleep_enable_reg_info->access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 PM1Bcontrol = PM1Acontrol;
330
331 /* Insert SLP_TYP bits */
332
Len Brown4be44fc2005-08-05 00:44:28 -0400333 PM1Acontrol |=
334 (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
335 PM1Bcontrol |=
336 (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
339 * We split the writes of SLP_TYP and SLP_EN to workaround
340 * poorly implemented hardware.
341 */
342
343 /* Write #1: fill in SLP_TYP data */
344
Len Brown4be44fc2005-08-05 00:44:28 -0400345 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
346 ACPI_REGISTER_PM1A_CONTROL,
347 PM1Acontrol);
348 if (ACPI_FAILURE(status)) {
349 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
Len Brown4be44fc2005-08-05 00:44:28 -0400352 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
353 ACPI_REGISTER_PM1B_CONTROL,
354 PM1Bcontrol);
355 if (ACPI_FAILURE(status)) {
356 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 /* Insert SLP_ENABLE bit */
360
361 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
362 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
363
364 /* Write #2: SLP_TYP + SLP_EN */
365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Len Brown4be44fc2005-08-05 00:44:28 -0400368 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
369 ACPI_REGISTER_PM1A_CONTROL,
370 PM1Acontrol);
371 if (ACPI_FAILURE(status)) {
372 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
376 ACPI_REGISTER_PM1B_CONTROL,
377 PM1Bcontrol);
378 if (ACPI_FAILURE(status)) {
379 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
382 if (sleep_state > ACPI_STATE_S3) {
383 /*
Robert Moore44f6c012005-04-18 22:49:35 -0400384 * We wanted to sleep > S3, but it didn't happen (by virtue of the
385 * fact that we are still executing!)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *
Robert Moore44f6c012005-04-18 22:49:35 -0400387 * Wait ten seconds, then try again. This is to get S4/S5 to work on
388 * all machines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
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
392 * away entirely.
393 */
Len Brown4be44fc2005-08-05 00:44:28 -0400394 acpi_os_stall(10000000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
397 ACPI_REGISTER_PM1_CONTROL,
398 sleep_enable_reg_info->
399 access_bit_mask);
400 if (ACPI_FAILURE(status)) {
401 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403 }
404
405 /* Wait until we enter sleep state */
406
407 do {
Bob Moored8c71b62007-02-02 19:48:21 +0300408 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400409 if (ACPI_FAILURE(status)) {
410 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
413 /* Spin until we wake */
414
415 } while (!in_value);
416
Len Brown4be44fc2005-08-05 00:44:28 -0400417 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Bob Moore83135242006-10-03 00:00:00 -0400420ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Robert Moore44f6c012005-04-18 22:49:35 -0400422/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *
424 * FUNCTION: acpi_enter_sleep_state_s4bios
425 *
426 * PARAMETERS: None
427 *
428 * RETURN: Status
429 *
430 * DESCRIPTION: Perform a S4 bios request.
431 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
432 *
433 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400434acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Len Brown4be44fc2005-08-05 00:44:28 -0400436 u32 in_value;
437 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Bob Mooreb229cf92006-04-21 17:15:00 -0400439 ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Bob Moored8c71b62007-02-02 19:48:21 +0300441 status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400442 if (ACPI_FAILURE(status)) {
443 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Bob Moored8c71b62007-02-02 19:48:21 +0300446 status = acpi_hw_clear_acpi_status();
Len Brown4be44fc2005-08-05 00:44:28 -0400447 if (ACPI_FAILURE(status)) {
448 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
451 /*
452 * 1) Disable/Clear all GPEs
453 * 2) Enable all wakeup GPEs
454 */
Len Brown4be44fc2005-08-05 00:44:28 -0400455 status = acpi_hw_disable_all_gpes();
456 if (ACPI_FAILURE(status)) {
457 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 acpi_gbl_system_awake_and_running = FALSE;
460
Len Brown4be44fc2005-08-05 00:44:28 -0400461 status = acpi_hw_enable_all_wakeup_gpes();
462 if (ACPI_FAILURE(status)) {
463 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Len Brown4be44fc2005-08-05 00:44:28 -0400466 ACPI_FLUSH_CPU_CACHE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Bob Mooref3d2e782007-02-02 19:48:18 +0300468 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
469 (u32) acpi_gbl_FADT.S4bios_request, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 do {
472 acpi_os_stall(1000);
Bob Moored8c71b62007-02-02 19:48:21 +0300473 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
Len Brown4be44fc2005-08-05 00:44:28 -0400474 if (ACPI_FAILURE(status)) {
475 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 } while (!in_value);
478
Len Brown4be44fc2005-08-05 00:44:28 -0400479 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Bob Moore83135242006-10-03 00:00:00 -0400482ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Robert Moore44f6c012005-04-18 22:49:35 -0400484/*******************************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *
486 * FUNCTION: acpi_leave_sleep_state
487 *
488 * PARAMETERS: sleep_state - Which sleep state we just exited
489 *
490 * RETURN: Status
491 *
492 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
493 * Called with interrupts ENABLED.
494 *
495 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -0400496acpi_status acpi_leave_sleep_state(u8 sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Len Brown4be44fc2005-08-05 00:44:28 -0400498 struct acpi_object_list arg_list;
499 union acpi_object arg;
500 acpi_status status;
501 struct acpi_bit_register_info *sleep_type_reg_info;
502 struct acpi_bit_register_info *sleep_enable_reg_info;
503 u32 PM1Acontrol;
504 u32 PM1Bcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Bob Mooreb229cf92006-04-21 17:15:00 -0400506 ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /*
509 * Set SLP_TYPE and SLP_EN to state S0.
510 * This is unclear from the ACPI Spec, but it is required
511 * by some machines.
512 */
Len Brown4be44fc2005-08-05 00:44:28 -0400513 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 /* Get current value of PM1A control */
523
Len Brown4be44fc2005-08-05 00:44:28 -0400524 status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
525 ACPI_REGISTER_PM1_CONTROL,
526 &PM1Acontrol);
527 if (ACPI_SUCCESS(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Clear SLP_EN and SLP_TYP fields */
530
531 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
Len Brown4be44fc2005-08-05 00:44:28 -0400532 sleep_enable_reg_info->
533 access_bit_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 PM1Bcontrol = PM1Acontrol;
535
536 /* Insert SLP_TYP bits */
537
Len Brown4be44fc2005-08-05 00:44:28 -0400538 PM1Acontrol |=
539 (acpi_gbl_sleep_type_a << sleep_type_reg_info->
540 bit_position);
541 PM1Bcontrol |=
542 (acpi_gbl_sleep_type_b << sleep_type_reg_info->
543 bit_position);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /* Just ignore any errors */
546
Len Brown4be44fc2005-08-05 00:44:28 -0400547 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
548 ACPI_REGISTER_PM1A_CONTROL,
549 PM1Acontrol);
550 (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
551 ACPI_REGISTER_PM1B_CONTROL,
552 PM1Bcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 }
555
556 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
557
558 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
559
560 /* Setup parameter object */
561
562 arg_list.count = 1;
563 arg_list.pointer = &arg;
564 arg.type = ACPI_TYPE_INTEGER;
565
566 /* Ignore any errors from these methods */
567
568 arg.integer.value = ACPI_SST_WAKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400569 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
570 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500571 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 arg.integer.value = sleep_state;
Len Brown4be44fc2005-08-05 00:44:28 -0400575 status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
576 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500577 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
Len Brown4be44fc2005-08-05 00:44:28 -0400580 status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
581 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500582 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
585
586 /*
587 * Restore the GPEs:
588 * 1) Disable/Clear all GPEs
589 * 2) Enable all runtime GPEs
590 */
Len Brown4be44fc2005-08-05 00:44:28 -0400591 status = acpi_hw_disable_all_gpes();
592 if (ACPI_FAILURE(status)) {
593 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 acpi_gbl_system_awake_and_running = TRUE;
596
Len Brown4be44fc2005-08-05 00:44:28 -0400597 status = acpi_hw_enable_all_runtime_gpes();
598 if (ACPI_FAILURE(status)) {
599 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 /* Enable power button */
603
Len Brown4be44fc2005-08-05 00:44:28 -0400604 (void)
605 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300606 [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
Robert Moore44f6c012005-04-18 22:49:35 -0400607
Len Brown4be44fc2005-08-05 00:44:28 -0400608 (void)
609 acpi_set_register(acpi_gbl_fixed_event_info
Bob Moored8c71b62007-02-02 19:48:21 +0300610 [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 arg.integer.value = ACPI_SST_WORKING;
Len Brown4be44fc2005-08-05 00:44:28 -0400613 status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
614 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500615 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Len Brown4be44fc2005-08-05 00:44:28 -0400618 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
Bob Moore83135242006-10-03 00:00:00 -0400620
621ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)