Merge branch 'drm-patches' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[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         /*
238          * 1) Disable/Clear all GPEs
239          */
240         status = acpi_hw_disable_all_gpes();
241         if (ACPI_FAILURE(status)) {
242                 return_ACPI_STATUS(status);
243         }
244
245         return_ACPI_STATUS(AE_OK);
246 }
247
248 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_prep)
249
250 /*******************************************************************************
251  *
252  * FUNCTION:    acpi_enter_sleep_state
253  *
254  * PARAMETERS:  sleep_state         - Which sleep state to enter
255  *
256  * RETURN:      Status
257  *
258  * DESCRIPTION: Enter a system sleep state (see ACPI 2.0 spec p 231)
259  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
260  *
261  ******************************************************************************/
262 acpi_status asmlinkage acpi_enter_sleep_state(u8 sleep_state)
263 {
264         u32 PM1Acontrol;
265         u32 PM1Bcontrol;
266         struct acpi_bit_register_info *sleep_type_reg_info;
267         struct acpi_bit_register_info *sleep_enable_reg_info;
268         u32 in_value;
269         acpi_status status;
270
271         ACPI_FUNCTION_TRACE(acpi_enter_sleep_state);
272
273         if ((acpi_gbl_sleep_type_a > ACPI_SLEEP_TYPE_MAX) ||
274             (acpi_gbl_sleep_type_b > ACPI_SLEEP_TYPE_MAX)) {
275                 ACPI_ERROR((AE_INFO, "Sleep values out of range: A=%X B=%X",
276                             acpi_gbl_sleep_type_a, acpi_gbl_sleep_type_b));
277                 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
278         }
279
280         sleep_type_reg_info =
281             acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
282         sleep_enable_reg_info =
283             acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
284
285         /* Clear wake status */
286
287         status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
288         if (ACPI_FAILURE(status)) {
289                 return_ACPI_STATUS(status);
290         }
291
292         /* Clear all fixed and general purpose status bits */
293
294         status = acpi_hw_clear_acpi_status();
295         if (ACPI_FAILURE(status)) {
296                 return_ACPI_STATUS(status);
297         }
298
299         /*
300          * 2) Enable all wakeup GPEs
301          */
302         status = acpi_hw_disable_all_gpes();
303         if (ACPI_FAILURE(status)) {
304                 return_ACPI_STATUS(status);
305         }
306
307         acpi_gbl_system_awake_and_running = FALSE;
308
309         status = acpi_hw_enable_all_wakeup_gpes();
310         if (ACPI_FAILURE(status)) {
311                 return_ACPI_STATUS(status);
312         }
313
314         /* Get current value of PM1A control */
315
316         status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
317                                        ACPI_REGISTER_PM1_CONTROL, &PM1Acontrol);
318         if (ACPI_FAILURE(status)) {
319                 return_ACPI_STATUS(status);
320         }
321         ACPI_DEBUG_PRINT((ACPI_DB_INIT,
322                           "Entering sleep state [S%d]\n", sleep_state));
323
324         /* Clear SLP_EN and SLP_TYP fields */
325
326         PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
327                          sleep_enable_reg_info->access_bit_mask);
328         PM1Bcontrol = PM1Acontrol;
329
330         /* Insert SLP_TYP bits */
331
332         PM1Acontrol |=
333             (acpi_gbl_sleep_type_a << sleep_type_reg_info->bit_position);
334         PM1Bcontrol |=
335             (acpi_gbl_sleep_type_b << sleep_type_reg_info->bit_position);
336
337         /*
338          * We split the writes of SLP_TYP and SLP_EN to workaround
339          * poorly implemented hardware.
340          */
341
342         /* Write #1: fill in SLP_TYP data */
343
344         status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
345                                         ACPI_REGISTER_PM1A_CONTROL,
346                                         PM1Acontrol);
347         if (ACPI_FAILURE(status)) {
348                 return_ACPI_STATUS(status);
349         }
350
351         status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
352                                         ACPI_REGISTER_PM1B_CONTROL,
353                                         PM1Bcontrol);
354         if (ACPI_FAILURE(status)) {
355                 return_ACPI_STATUS(status);
356         }
357
358         /* Insert SLP_ENABLE bit */
359
360         PM1Acontrol |= sleep_enable_reg_info->access_bit_mask;
361         PM1Bcontrol |= sleep_enable_reg_info->access_bit_mask;
362
363         /* Write #2: SLP_TYP + SLP_EN */
364
365         ACPI_FLUSH_CPU_CACHE();
366
367         status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
368                                         ACPI_REGISTER_PM1A_CONTROL,
369                                         PM1Acontrol);
370         if (ACPI_FAILURE(status)) {
371                 return_ACPI_STATUS(status);
372         }
373
374         status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
375                                         ACPI_REGISTER_PM1B_CONTROL,
376                                         PM1Bcontrol);
377         if (ACPI_FAILURE(status)) {
378                 return_ACPI_STATUS(status);
379         }
380
381         if (sleep_state > ACPI_STATE_S3) {
382                 /*
383                  * We wanted to sleep > S3, but it didn't happen (by virtue of the
384                  * fact that we are still executing!)
385                  *
386                  * Wait ten seconds, then try again. This is to get S4/S5 to work on
387                  * all machines.
388                  *
389                  * We wait so long to allow chipsets that poll this reg very slowly to
390                  * still read the right value. Ideally, this block would go
391                  * away entirely.
392                  */
393                 acpi_os_stall(10000000);
394
395                 status = acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
396                                                 ACPI_REGISTER_PM1_CONTROL,
397                                                 sleep_enable_reg_info->
398                                                 access_bit_mask);
399                 if (ACPI_FAILURE(status)) {
400                         return_ACPI_STATUS(status);
401                 }
402         }
403
404         /* Wait until we enter sleep state */
405
406         do {
407                 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
408                 if (ACPI_FAILURE(status)) {
409                         return_ACPI_STATUS(status);
410                 }
411
412                 /* Spin until we wake */
413
414         } while (!in_value);
415
416         return_ACPI_STATUS(AE_OK);
417 }
418
419 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state)
420
421 /*******************************************************************************
422  *
423  * FUNCTION:    acpi_enter_sleep_state_s4bios
424  *
425  * PARAMETERS:  None
426  *
427  * RETURN:      Status
428  *
429  * DESCRIPTION: Perform a S4 bios request.
430  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
431  *
432  ******************************************************************************/
433 acpi_status asmlinkage acpi_enter_sleep_state_s4bios(void)
434 {
435         u32 in_value;
436         acpi_status status;
437
438         ACPI_FUNCTION_TRACE(acpi_enter_sleep_state_s4bios);
439
440         status = acpi_set_register(ACPI_BITREG_WAKE_STATUS, 1);
441         if (ACPI_FAILURE(status)) {
442                 return_ACPI_STATUS(status);
443         }
444
445         status = acpi_hw_clear_acpi_status();
446         if (ACPI_FAILURE(status)) {
447                 return_ACPI_STATUS(status);
448         }
449
450         /*
451          * 1) Disable/Clear all GPEs
452          * 2) Enable all wakeup GPEs
453          */
454         status = acpi_hw_disable_all_gpes();
455         if (ACPI_FAILURE(status)) {
456                 return_ACPI_STATUS(status);
457         }
458         acpi_gbl_system_awake_and_running = FALSE;
459
460         status = acpi_hw_enable_all_wakeup_gpes();
461         if (ACPI_FAILURE(status)) {
462                 return_ACPI_STATUS(status);
463         }
464
465         ACPI_FLUSH_CPU_CACHE();
466
467         status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
468                                     (u32) acpi_gbl_FADT.S4bios_request, 8);
469
470         do {
471                 acpi_os_stall(1000);
472                 status = acpi_get_register(ACPI_BITREG_WAKE_STATUS, &in_value);
473                 if (ACPI_FAILURE(status)) {
474                         return_ACPI_STATUS(status);
475                 }
476         } while (!in_value);
477
478         return_ACPI_STATUS(AE_OK);
479 }
480
481 ACPI_EXPORT_SYMBOL(acpi_enter_sleep_state_s4bios)
482
483 /*******************************************************************************
484  *
485  * FUNCTION:    acpi_leave_sleep_state
486  *
487  * PARAMETERS:  sleep_state         - Which sleep state we just exited
488  *
489  * RETURN:      Status
490  *
491  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
492  *              Called with interrupts ENABLED.
493  *
494  ******************************************************************************/
495 acpi_status acpi_leave_sleep_state(u8 sleep_state)
496 {
497         struct acpi_object_list arg_list;
498         union acpi_object arg;
499         acpi_status status;
500         struct acpi_bit_register_info *sleep_type_reg_info;
501         struct acpi_bit_register_info *sleep_enable_reg_info;
502         u32 PM1Acontrol;
503         u32 PM1Bcontrol;
504
505         ACPI_FUNCTION_TRACE(acpi_leave_sleep_state);
506
507         /*
508          * Set SLP_TYPE and SLP_EN to state S0.
509          * This is unclear from the ACPI Spec, but it is required
510          * by some machines.
511          */
512         status = acpi_get_sleep_type_data(ACPI_STATE_S0,
513                                           &acpi_gbl_sleep_type_a,
514                                           &acpi_gbl_sleep_type_b);
515         if (ACPI_SUCCESS(status)) {
516                 sleep_type_reg_info =
517                     acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_TYPE_A);
518                 sleep_enable_reg_info =
519                     acpi_hw_get_bit_register_info(ACPI_BITREG_SLEEP_ENABLE);
520
521                 /* Get current value of PM1A control */
522
523                 status = acpi_hw_register_read(ACPI_MTX_DO_NOT_LOCK,
524                                                ACPI_REGISTER_PM1_CONTROL,
525                                                &PM1Acontrol);
526                 if (ACPI_SUCCESS(status)) {
527
528                         /* Clear SLP_EN and SLP_TYP fields */
529
530                         PM1Acontrol &= ~(sleep_type_reg_info->access_bit_mask |
531                                          sleep_enable_reg_info->
532                                          access_bit_mask);
533                         PM1Bcontrol = PM1Acontrol;
534
535                         /* Insert SLP_TYP bits */
536
537                         PM1Acontrol |=
538                             (acpi_gbl_sleep_type_a << sleep_type_reg_info->
539                              bit_position);
540                         PM1Bcontrol |=
541                             (acpi_gbl_sleep_type_b << sleep_type_reg_info->
542                              bit_position);
543
544                         /* Just ignore any errors */
545
546                         (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
547                                                      ACPI_REGISTER_PM1A_CONTROL,
548                                                      PM1Acontrol);
549                         (void)acpi_hw_register_write(ACPI_MTX_DO_NOT_LOCK,
550                                                      ACPI_REGISTER_PM1B_CONTROL,
551                                                      PM1Bcontrol);
552                 }
553         }
554
555         /* Ensure enter_sleep_state_prep -> enter_sleep_state ordering */
556
557         acpi_gbl_sleep_type_a = ACPI_SLEEP_TYPE_INVALID;
558
559         /* Setup parameter object */
560
561         arg_list.count = 1;
562         arg_list.pointer = &arg;
563         arg.type = ACPI_TYPE_INTEGER;
564
565         /* Ignore any errors from these methods */
566
567         arg.integer.value = ACPI_SST_WAKING;
568         status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
569         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
570                 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
571         }
572
573         arg.integer.value = sleep_state;
574         status = acpi_evaluate_object(NULL, METHOD_NAME__BFS, &arg_list, NULL);
575         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
576                 ACPI_EXCEPTION((AE_INFO, status, "During Method _BFS"));
577         }
578
579         /*
580          * GPEs must be enabled before _WAK is called as GPEs
581          * might get fired there
582          *
583          * Restore the GPEs:
584          * 1) Disable/Clear all GPEs
585          * 2) Enable all runtime GPEs
586          */
587         status = acpi_hw_disable_all_gpes();
588         if (ACPI_FAILURE(status)) {
589                 return_ACPI_STATUS(status);
590         }
591         status = acpi_hw_enable_all_runtime_gpes();
592         if (ACPI_FAILURE(status)) {
593                 return_ACPI_STATUS(status);
594         }
595
596         status = acpi_evaluate_object(NULL, METHOD_NAME__WAK, &arg_list, NULL);
597         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
598                 ACPI_EXCEPTION((AE_INFO, status, "During Method _WAK"));
599         }
600         /* TBD: _WAK "sometimes" returns stuff - do we want to look at it? */
601
602         acpi_gbl_system_awake_and_running = TRUE;
603
604         /* Enable power button */
605
606         (void)
607             acpi_set_register(acpi_gbl_fixed_event_info
608                               [ACPI_EVENT_POWER_BUTTON].enable_register_id, 1);
609
610         (void)
611             acpi_set_register(acpi_gbl_fixed_event_info
612                               [ACPI_EVENT_POWER_BUTTON].status_register_id, 1);
613
614         arg.integer.value = ACPI_SST_WORKING;
615         status = acpi_evaluate_object(NULL, METHOD_NAME__SST, &arg_list, NULL);
616         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
617                 ACPI_EXCEPTION((AE_INFO, status, "During Method _SST"));
618         }
619
620         return_ACPI_STATUS(status);
621 }
622
623 ACPI_EXPORT_SYMBOL(acpi_leave_sleep_state)