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