]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/acpi/hardware/hwsleep.c
ACPICA 20050408 from Bob Moore
[mirror_ubuntu-artful-kernel.git] / drivers / acpi / hardware / hwsleep.c
1
2 /******************************************************************************
3 *
4 * Name: hwsleep.c - ACPI Hardware Sleep/Wake Interface
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000 - 2005, R. Byron Moore
10 * 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
45 #include <linux/module.h>
46 #include <acpi/acpi.h>
47
48 #define _COMPONENT ACPI_HARDWARE
49 ACPI_MODULE_NAME ("hwsleep")
50
51
52 /*******************************************************************************
53 *
54 * FUNCTION: acpi_set_firmware_waking_vector
55 *
56 * PARAMETERS: physical_address - Physical address of ACPI real mode
57 * entry point.
58 *
59 * RETURN: Status
60 *
61 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
62 *
63 ******************************************************************************/
64
65 acpi_status
66 acpi_set_firmware_waking_vector (
67 acpi_physical_address physical_address)
68 {
69
70 ACPI_FUNCTION_TRACE ("acpi_set_firmware_waking_vector");
71
72
73 /* Set the vector */
74
75 if (acpi_gbl_common_fACS.vector_width == 32) {
76 *(ACPI_CAST_PTR (u32, acpi_gbl_common_fACS.firmware_waking_vector))
77 = (u32) physical_address;
78 }
79 else {
80 *acpi_gbl_common_fACS.firmware_waking_vector
81 = physical_address;
82 }
83
84 return_ACPI_STATUS (AE_OK);
85 }
86
87
88 /*******************************************************************************
89 *
90 * FUNCTION: acpi_get_firmware_waking_vector
91 *
92 * PARAMETERS: *physical_address - Where the contents of
93 * the firmware_waking_vector field of
94 * the FACS will be returned.
95 *
96 * RETURN: Status, vector
97 *
98 * DESCRIPTION: Access function for the firmware_waking_vector field in FACS
99 *
100 ******************************************************************************/
101
102 #ifdef ACPI_FUTURE_USAGE
103 acpi_status
104 acpi_get_firmware_waking_vector (
105 acpi_physical_address *physical_address)
106 {
107
108 ACPI_FUNCTION_TRACE ("acpi_get_firmware_waking_vector");
109
110
111 if (!physical_address) {
112 return_ACPI_STATUS (AE_BAD_PARAMETER);
113 }
114
115 /* Get the vector */
116
117 if (acpi_gbl_common_fACS.vector_width == 32) {
118 *physical_address = (acpi_physical_address)
119 *(ACPI_CAST_PTR (u32, acpi_gbl_common_fACS.firmware_waking_vector));
120 }
121 else {
122 *physical_address =
123 *acpi_gbl_common_fACS.firmware_waking_vector;
124 }
125
126 return_ACPI_STATUS (AE_OK);
127 }
128 #endif
129
130
131 /*******************************************************************************
132 *
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 ******************************************************************************/
145
146 acpi_status
147 acpi_enter_sleep_state_prep (
148 u8 sleep_state)
149 {
150 acpi_status status;
151 struct acpi_object_list arg_list;
152 union acpi_object arg;
153
154
155 ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_prep");
156
157
158 /*
159 * _PSW methods could be run here to enable wake-on keyboard, LAN, etc.
160 */
161 status = acpi_get_sleep_type_data (sleep_state,
162 &acpi_gbl_sleep_type_a, &acpi_gbl_sleep_type_b);
163 if (ACPI_FAILURE (status)) {
164 return_ACPI_STATUS (status);
165 }
166
167 /* Setup parameter object */
168
169 arg_list.count = 1;
170 arg_list.pointer = &arg;
171
172 arg.type = ACPI_TYPE_INTEGER;
173 arg.integer.value = sleep_state;
174
175 /* Run the _PTS and _GTS methods */
176
177 status = acpi_evaluate_object (NULL, METHOD_NAME__PTS, &arg_list, NULL);
178 if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
179 return_ACPI_STATUS (status);
180 }
181
182 status = acpi_evaluate_object (NULL, METHOD_NAME__GTS, &arg_list, NULL);
183 if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
184 return_ACPI_STATUS (status);
185 }
186
187 /* Setup the argument to _SST */
188
189 switch (sleep_state) {
190 case ACPI_STATE_S0:
191 arg.integer.value = ACPI_SST_WORKING;
192 break;
193
194 case ACPI_STATE_S1:
195 case ACPI_STATE_S2:
196 case ACPI_STATE_S3:
197 arg.integer.value = ACPI_SST_SLEEPING;
198 break;
199
200 case ACPI_STATE_S4:
201 arg.integer.value = ACPI_SST_SLEEP_CONTEXT;
202 break;
203
204 default:
205 arg.integer.value = ACPI_SST_INDICATOR_OFF; /* Default is off */
206 break;
207 }
208
209 /* Set the system indicators to show the desired sleep state. */
210
211 status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
212 if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
213 ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
214 acpi_format_exception (status)));
215 }
216
217 return_ACPI_STATUS (AE_OK);
218 }
219
220
221 /*******************************************************************************
222 *
223 * FUNCTION: acpi_enter_sleep_state
224 *
225 * PARAMETERS: sleep_state - Which sleep state to enter
226 *
227 * RETURN: Status
228 *
229 * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
230 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
231 *
232 ******************************************************************************/
233
234 acpi_status asmlinkage
235 acpi_enter_sleep_state (
236 u8 sleep_state)
237 {
238 u32 PM1Acontrol;
239 u32 PM1Bcontrol;
240 struct acpi_bit_register_info *sleep_type_reg_info;
241 struct acpi_bit_register_info *sleep_enable_reg_info;
242 u32 in_value;
243 acpi_status status;
244
245
246 ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state");
247
248
249 if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
250 (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
251 ACPI_REPORT_ERROR (("Sleep values out of range: A=%X B=%X\n",
252 acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
253 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
254 }
255
256 sleep_type_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A);
257 sleep_enable_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE);
258
259 /* Clear wake status */
260
261 status = acpi_set_register (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
262 if (ACPI_FAILURE (status)) {
263 return_ACPI_STATUS (status);
264 }
265
266 /* Clear all fixed and general purpose status bits */
267
268 status = acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK);
269 if (ACPI_FAILURE (status)) {
270 return_ACPI_STATUS (status);
271 }
272
273 /*
274 * 1) Disable/Clear all GPEs
275 * 2) Enable all wakeup GPEs
276 */
277 status = acpi_hw_disable_all_gpes (ACPI_ISR);
278 if (ACPI_FAILURE (status)) {
279 return_ACPI_STATUS (status);
280 }
281 acpi_gbl_system_awake_and_running = FALSE;
282
283 status = acpi_hw_enable_all_wakeup_gpes (ACPI_ISR);
284 if (ACPI_FAILURE (status)) {
285 return_ACPI_STATUS (status);
286 }
287
288 /* Get current value of PM1A control */
289
290 status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
291 ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
292 if (ACPI_FAILURE (status)) {
293 return_ACPI_STATUS (status);
294 }
295 ACPI_DEBUG_PRINT ((ACPI_DB_INIT,
296 "Entering sleep state [S%d]\n", sleep_state));
297
298 /* Clear SLP_EN and SLP_TYP fields */
299
300 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
301 sleep_enable_reg_info->access_bit_mask);
302 PM1Bcontrol = PM1Acontrol;
303
304 /* Insert SLP_TYP bits */
305
306 PM1Acontrol |= (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
307 PM1Bcontrol |= (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
308
309 /*
310 * We split the writes of SLP_TYP and SLP_EN to workaround
311 * poorly implemented hardware.
312 */
313
314 /* Write #1: fill in SLP_TYP data */
315
316 status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
317 ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
318 if (ACPI_FAILURE (status)) {
319 return_ACPI_STATUS (status);
320 }
321
322 status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
323 ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
324 if (ACPI_FAILURE (status)) {
325 return_ACPI_STATUS (status);
326 }
327
328 /* Insert SLP_ENABLE bit */
329
330 PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
331 PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
332
333 /* Write #2: SLP_TYP + SLP_EN */
334
335 ACPI_FLUSH_CPU_CACHE ();
336
337 status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
338 ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
339 if (ACPI_FAILURE (status)) {
340 return_ACPI_STATUS (status);
341 }
342
343 status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
344 ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
345 if (ACPI_FAILURE (status)) {
346 return_ACPI_STATUS (status);
347 }
348
349 if (sleep_state > ACPI_STATE_S3) {
350 /*
351 * We wanted to sleep > S3, but it didn't happen (by virtue of the
352 * fact that we are still executing!)
353 *
354 * Wait ten seconds, then try again. This is to get S4/S5 to work on
355 * all machines.
356 *
357 * We wait so long to allow chipsets that poll this reg very slowly to
358 * still read the right value. Ideally, this block would go
359 * away entirely.
360 */
361 acpi_os_stall (10000000);
362
363 status = acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
364 ACPI_REGISTER_PM1_CONTROL,
365 sleep_enable_reg_info->access_bit_mask);
366 if (ACPI_FAILURE (status)) {
367 return_ACPI_STATUS (status);
368 }
369 }
370
371 /* Wait until we enter sleep state */
372
373 do {
374 status = acpi_get_register (ACPI_BITREG_WAKE_STATUS, &in_value,
375 ACPI_MTX_DO_NOT_LOCK);
376 if (ACPI_FAILURE (status)) {
377 return_ACPI_STATUS (status);
378 }
379
380 /* Spin until we wake */
381
382 } while (!in_value);
383
384 return_ACPI_STATUS (AE_OK);
385 }
386 EXPORT_SYMBOL(acpi_enter_sleep_state);
387
388
389 /*******************************************************************************
390 *
391 * FUNCTION: acpi_enter_sleep_state_s4bios
392 *
393 * PARAMETERS: None
394 *
395 * RETURN: Status
396 *
397 * DESCRIPTION: Perform a S4 bios request.
398 * THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
399 *
400 ******************************************************************************/
401
402 acpi_status asmlinkage
403 acpi_enter_sleep_state_s4bios (
404 void)
405 {
406 u32 in_value;
407 acpi_status status;
408
409
410 ACPI_FUNCTION_TRACE ("acpi_enter_sleep_state_s4bios");
411
412
413 status = acpi_set_register (ACPI_BITREG_WAKE_STATUS, 1, ACPI_MTX_DO_NOT_LOCK);
414 if (ACPI_FAILURE (status)) {
415 return_ACPI_STATUS (status);
416 }
417
418 status = acpi_hw_clear_acpi_status (ACPI_MTX_DO_NOT_LOCK);
419 if (ACPI_FAILURE (status)) {
420 return_ACPI_STATUS (status);
421 }
422
423 /*
424 * 1) Disable/Clear all GPEs
425 * 2) Enable all wakeup GPEs
426 */
427 status = acpi_hw_disable_all_gpes (ACPI_ISR);
428 if (ACPI_FAILURE (status)) {
429 return_ACPI_STATUS (status);
430 }
431 acpi_gbl_system_awake_and_running = FALSE;
432
433 status = acpi_hw_enable_all_wakeup_gpes (ACPI_ISR);
434 if (ACPI_FAILURE (status)) {
435 return_ACPI_STATUS (status);
436 }
437
438 ACPI_FLUSH_CPU_CACHE ();
439
440 status = acpi_os_write_port (acpi_gbl_FADT->smi_cmd,
441 (u32) acpi_gbl_FADT->S4bios_req, 8);
442
443 do {
444 acpi_os_stall(1000);
445 status = acpi_get_register (ACPI_BITREG_WAKE_STATUS, &in_value,
446 ACPI_MTX_DO_NOT_LOCK);
447 if (ACPI_FAILURE (status)) {
448 return_ACPI_STATUS (status);
449 }
450 } while (!in_value);
451
452 return_ACPI_STATUS (AE_OK);
453 }
454 EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios);
455
456
457 /*******************************************************************************
458 *
459 * FUNCTION: acpi_leave_sleep_state
460 *
461 * PARAMETERS: sleep_state - Which sleep state we just exited
462 *
463 * RETURN: Status
464 *
465 * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
466 * Called with interrupts ENABLED.
467 *
468 ******************************************************************************/
469
470 acpi_status
471 acpi_leave_sleep_state (
472 u8 sleep_state)
473 {
474 struct acpi_object_list arg_list;
475 union acpi_object arg;
476 acpi_status status;
477 struct acpi_bit_register_info *sleep_type_reg_info;
478 struct acpi_bit_register_info *sleep_enable_reg_info;
479 u32 PM1Acontrol;
480 u32 PM1Bcontrol;
481
482
483 ACPI_FUNCTION_TRACE ("acpi_leave_sleep_state");
484
485
486 /*
487 * Set SLP_TYPE and SLP_EN to state S0.
488 * This is unclear from the ACPI Spec, but it is required
489 * by some machines.
490 */
491 status = acpi_get_sleep_type_data (ACPI_STATE_S0,
492 &acpi_gbl_sleep_type_a, &acpi_gbl_sleep_type_b);
493 if (ACPI_SUCCESS (status)) {
494 sleep_type_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_TYPE_A);
495 sleep_enable_reg_info = acpi_hw_get_bit_register_info (ACPI_BITREG_SLEEP_ENABLE);
496
497 /* Get current value of PM1A control */
498
499 status = acpi_hw_register_read (ACPI_MTX_DO_NOT_LOCK,
500 ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
501 if (ACPI_SUCCESS (status)) {
502 /* Clear SLP_EN and SLP_TYP fields */
503
504 PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
505 sleep_enable_reg_info->access_bit_mask);
506 PM1Bcontrol = PM1Acontrol;
507
508 /* Insert SLP_TYP bits */
509
510 PM1Acontrol |= (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
511 PM1Bcontrol |= (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
512
513 /* Just ignore any errors */
514
515 (void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
516 ACPI_REGISTER_PM1A_CONTROL, PM1Acontrol);
517 (void) acpi_hw_register_write (ACPI_MTX_DO_NOT_LOCK,
518 ACPI_REGISTER_PM1B_CONTROL, PM1Bcontrol);
519 }
520 }
521
522 /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
523
524 acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
525
526 /* Setup parameter object */
527
528 arg_list.count = 1;
529 arg_list.pointer = &arg;
530 arg.type = ACPI_TYPE_INTEGER;
531
532 /* Ignore any errors from these methods */
533
534 arg.integer.value = ACPI_SST_WAKING;
535 status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
536 if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
537 ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
538 acpi_format_exception (status)));
539 }
540
541 arg.integer.value = sleep_state;
542 status = acpi_evaluate_object (NULL, METHOD_NAME__BFS, &arg_list, NULL);
543 if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
544 ACPI_REPORT_ERROR (("Method _BFS failed, %s\n",
545 acpi_format_exception (status)));
546 }
547
548 status = acpi_evaluate_object (NULL, METHOD_NAME__WAK, &arg_list, NULL);
549 if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
550 ACPI_REPORT_ERROR (("Method _WAK failed, %s\n",
551 acpi_format_exception (status)));
552 }
553 /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
554
555 /*
556 * Restore the GPEs:
557 * 1) Disable/Clear all GPEs
558 * 2) Enable all runtime GPEs
559 */
560 status = acpi_hw_disable_all_gpes (ACPI_NOT_ISR);
561 if (ACPI_FAILURE (status)) {
562 return_ACPI_STATUS (status);
563 }
564 acpi_gbl_system_awake_and_running = TRUE;
565
566 status = acpi_hw_enable_all_runtime_gpes (ACPI_NOT_ISR);
567 if (ACPI_FAILURE (status)) {
568 return_ACPI_STATUS (status);
569 }
570
571 /* Enable power button */
572
573 (void) acpi_set_register(
574 acpi_gbl_fixed_event_info[ACPI_EVENT_POWER_BUTTON].enable_register_id,
575 1, ACPI_MTX_DO_NOT_LOCK);
576
577 (void) acpi_set_register(
578 acpi_gbl_fixed_event_info[ACPI_EVENT_POWER_BUTTON].status_register_id,
579 1, ACPI_MTX_DO_NOT_LOCK);
580
581 arg.integer.value = ACPI_SST_WORKING;
582 status = acpi_evaluate_object (NULL, METHOD_NAME__SST, &arg_list, NULL);
583 if (ACPI_FAILURE (status) && status != AE_NOT_FOUND) {
584 ACPI_REPORT_ERROR (("Method _SST failed, %s\n",
585 acpi_format_exception (status)));
586 }
587
588 return_ACPI_STATUS (status);
589 }