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