]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/components/hardware/hwxfsleep.c
Merge ACPICA 20120320.
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / components / hardware / hwxfsleep.c
1 /******************************************************************************
2  *
3  * Name: hwxfsleep.c - ACPI Hardware Sleep/Wake External Interfaces
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2012, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46
47 #define _COMPONENT          ACPI_HARDWARE
48         ACPI_MODULE_NAME    ("hwxfsleep")
49
50 /* Local prototypes */
51
52 static ACPI_STATUS
53 AcpiHwSleepDispatch (
54     UINT8                   SleepState,
55     UINT8                   Flags,
56     UINT32                  FunctionId);
57
58 /*
59  * Dispatch table used to efficiently branch to the various sleep
60  * functions.
61  */
62 #define ACPI_SLEEP_FUNCTION_ID          0
63 #define ACPI_WAKE_PREP_FUNCTION_ID      1
64 #define ACPI_WAKE_FUNCTION_ID           2
65
66 /* Legacy functions are optional, based upon ACPI_REDUCED_HARDWARE */
67
68 static ACPI_SLEEP_FUNCTIONS         AcpiSleepDispatch[] =
69 {
70     {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacySleep),    AcpiHwExtendedSleep},
71     {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWakePrep), AcpiHwExtendedWakePrep},
72     {ACPI_HW_OPTIONAL_FUNCTION (AcpiHwLegacyWake),     AcpiHwExtendedWake}
73 };
74
75
76 /*
77  * These functions are removed for the ACPI_REDUCED_HARDWARE case:
78  *      AcpiSetFirmwareWakingVector
79  *      AcpiSetFirmwareWakingVector64
80  *      AcpiEnterSleepStateS4bios
81  */
82
83 #if (!ACPI_REDUCED_HARDWARE)
84 /*******************************************************************************
85  *
86  * FUNCTION:    AcpiSetFirmwareWakingVector
87  *
88  * PARAMETERS:  PhysicalAddress     - 32-bit physical address of ACPI real mode
89  *                                    entry point.
90  *
91  * RETURN:      Status
92  *
93  * DESCRIPTION: Sets the 32-bit FirmwareWakingVector field of the FACS
94  *
95  ******************************************************************************/
96
97 ACPI_STATUS
98 AcpiSetFirmwareWakingVector (
99     UINT32                  PhysicalAddress)
100 {
101     ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector);
102
103
104     /* Set the 32-bit vector */
105
106     AcpiGbl_FACS->FirmwareWakingVector = PhysicalAddress;
107
108     /* Clear the 64-bit vector if it exists */
109
110     if ((AcpiGbl_FACS->Length > 32) && (AcpiGbl_FACS->Version >= 1))
111     {
112         AcpiGbl_FACS->XFirmwareWakingVector = 0;
113     }
114
115     return_ACPI_STATUS (AE_OK);
116 }
117
118 ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector)
119
120
121 #if ACPI_MACHINE_WIDTH == 64
122 /*******************************************************************************
123  *
124  * FUNCTION:    AcpiSetFirmwareWakingVector64
125  *
126  * PARAMETERS:  PhysicalAddress     - 64-bit physical address of ACPI protected
127  *                                    mode entry point.
128  *
129  * RETURN:      Status
130  *
131  * DESCRIPTION: Sets the 64-bit X_FirmwareWakingVector field of the FACS, if
132  *              it exists in the table. This function is intended for use with
133  *              64-bit host operating systems.
134  *
135  ******************************************************************************/
136
137 ACPI_STATUS
138 AcpiSetFirmwareWakingVector64 (
139     UINT64                  PhysicalAddress)
140 {
141     ACPI_FUNCTION_TRACE (AcpiSetFirmwareWakingVector64);
142
143
144     /* Determine if the 64-bit vector actually exists */
145
146     if ((AcpiGbl_FACS->Length <= 32) || (AcpiGbl_FACS->Version < 1))
147     {
148         return_ACPI_STATUS (AE_NOT_EXIST);
149     }
150
151     /* Clear 32-bit vector, set the 64-bit X_ vector */
152
153     AcpiGbl_FACS->FirmwareWakingVector = 0;
154     AcpiGbl_FACS->XFirmwareWakingVector = PhysicalAddress;
155     return_ACPI_STATUS (AE_OK);
156 }
157
158 ACPI_EXPORT_SYMBOL (AcpiSetFirmwareWakingVector64)
159 #endif
160
161
162 /*******************************************************************************
163  *
164  * FUNCTION:    AcpiEnterSleepStateS4bios
165  *
166  * PARAMETERS:  None
167  *
168  * RETURN:      Status
169  *
170  * DESCRIPTION: Perform a S4 bios request.
171  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
172  *
173  ******************************************************************************/
174
175 ACPI_STATUS
176 AcpiEnterSleepStateS4bios (
177     void)
178 {
179     UINT32                  InValue;
180     ACPI_STATUS             Status;
181
182
183     ACPI_FUNCTION_TRACE (AcpiEnterSleepStateS4bios);
184
185
186     /* Clear the wake status bit (PM1) */
187
188     Status = AcpiWriteBitRegister (ACPI_BITREG_WAKE_STATUS, ACPI_CLEAR_STATUS);
189     if (ACPI_FAILURE (Status))
190     {
191         return_ACPI_STATUS (Status);
192     }
193
194     Status = AcpiHwClearAcpiStatus ();
195     if (ACPI_FAILURE (Status))
196     {
197         return_ACPI_STATUS (Status);
198     }
199
200     /*
201      * 1) Disable/Clear all GPEs
202      * 2) Enable all wakeup GPEs
203      */
204     Status = AcpiHwDisableAllGpes ();
205     if (ACPI_FAILURE (Status))
206     {
207         return_ACPI_STATUS (Status);
208     }
209     AcpiGbl_SystemAwakeAndRunning = FALSE;
210
211     Status = AcpiHwEnableAllWakeupGpes ();
212     if (ACPI_FAILURE (Status))
213     {
214         return_ACPI_STATUS (Status);
215     }
216
217     ACPI_FLUSH_CPU_CACHE ();
218
219     Status = AcpiHwWritePort (AcpiGbl_FADT.SmiCommand,
220                 (UINT32) AcpiGbl_FADT.S4BiosRequest, 8);
221
222     do {
223         AcpiOsStall(1000);
224         Status = AcpiReadBitRegister (ACPI_BITREG_WAKE_STATUS, &InValue);
225         if (ACPI_FAILURE (Status))
226         {
227             return_ACPI_STATUS (Status);
228         }
229     } while (!InValue);
230
231     return_ACPI_STATUS (AE_OK);
232 }
233
234 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStateS4bios)
235
236 #endif /* !ACPI_REDUCED_HARDWARE */
237
238
239 /*******************************************************************************
240  *
241  * FUNCTION:    AcpiHwSleepDispatch
242  *
243  * PARAMETERS:  SleepState          - Which sleep state to enter/exit
244  *              FunctionId          - Sleep, WakePrep, or Wake
245  *
246  * RETURN:      Status from the invoked sleep handling function.
247  *
248  * DESCRIPTION: Dispatch a sleep/wake request to the appropriate handling
249  *              function.
250  *
251  ******************************************************************************/
252
253 static ACPI_STATUS
254 AcpiHwSleepDispatch (
255     UINT8                   SleepState,
256     UINT8                   Flags,
257     UINT32                  FunctionId)
258 {
259     ACPI_STATUS             Status;
260     ACPI_SLEEP_FUNCTIONS    *SleepFunctions = &AcpiSleepDispatch[FunctionId];
261
262
263 #if (!ACPI_REDUCED_HARDWARE)
264
265     /*
266      * If the Hardware Reduced flag is set (from the FADT), we must
267      * use the extended sleep registers
268      */
269     if (AcpiGbl_ReducedHardware ||
270         AcpiGbl_FADT.SleepControl.Address)
271     {
272         Status = SleepFunctions->ExtendedFunction (SleepState, Flags);
273     }
274     else
275     {
276         /* Legacy sleep */
277
278         Status = SleepFunctions->LegacyFunction (SleepState, Flags);
279     }
280
281     return (Status);
282
283 #else
284     /*
285      * For the case where reduced-hardware-only code is being generated,
286      * we know that only the extended sleep registers are available
287      */
288     Status = SleepFunctions->ExtendedFunction (SleepState, Flags);
289     return (Status);
290
291 #endif /* !ACPI_REDUCED_HARDWARE */
292 }
293
294
295 /*******************************************************************************
296  *
297  * FUNCTION:    AcpiEnterSleepStatePrep
298  *
299  * PARAMETERS:  SleepState          - Which sleep state to enter
300  *
301  * RETURN:      Status
302  *
303  * DESCRIPTION: Prepare to enter a system sleep state.
304  *              This function must execute with interrupts enabled.
305  *              We break sleeping into 2 stages so that OSPM can handle
306  *              various OS-specific tasks between the two steps.
307  *
308  ******************************************************************************/
309
310 ACPI_STATUS
311 AcpiEnterSleepStatePrep (
312     UINT8                   SleepState)
313 {
314     ACPI_STATUS             Status;
315     ACPI_OBJECT_LIST        ArgList;
316     ACPI_OBJECT             Arg;
317     UINT32                  SstValue;
318
319
320     ACPI_FUNCTION_TRACE (AcpiEnterSleepStatePrep);
321
322
323     Status = AcpiGetSleepTypeData (SleepState,
324                     &AcpiGbl_SleepTypeA, &AcpiGbl_SleepTypeB);
325     if (ACPI_FAILURE (Status))
326     {
327         return_ACPI_STATUS (Status);
328     }
329
330     /* Execute the _PTS method (Prepare To Sleep) */
331
332     ArgList.Count = 1;
333     ArgList.Pointer = &Arg;
334     Arg.Type = ACPI_TYPE_INTEGER;
335     Arg.Integer.Value = SleepState;
336
337     Status = AcpiEvaluateObject (NULL, METHOD_PATHNAME__PTS, &ArgList, NULL);
338     if (ACPI_FAILURE (Status) && Status != AE_NOT_FOUND)
339     {
340         return_ACPI_STATUS (Status);
341     }
342
343     /* Setup the argument to the _SST method (System STatus) */
344
345     switch (SleepState)
346     {
347     case ACPI_STATE_S0:
348         SstValue = ACPI_SST_WORKING;
349         break;
350
351     case ACPI_STATE_S1:
352     case ACPI_STATE_S2:
353     case ACPI_STATE_S3:
354         SstValue = ACPI_SST_SLEEPING;
355         break;
356
357     case ACPI_STATE_S4:
358         SstValue = ACPI_SST_SLEEP_CONTEXT;
359         break;
360
361     default:
362         SstValue = ACPI_SST_INDICATOR_OFF; /* Default is off */
363         break;
364     }
365
366     /*
367      * Set the system indicators to show the desired sleep state.
368      * _SST is an optional method (return no error if not found)
369      */
370     AcpiHwExecuteSleepMethod (METHOD_PATHNAME__SST, SstValue);
371     return_ACPI_STATUS (AE_OK);
372 }
373
374 ACPI_EXPORT_SYMBOL (AcpiEnterSleepStatePrep)
375
376
377 /*******************************************************************************
378  *
379  * FUNCTION:    AcpiEnterSleepState
380  *
381  * PARAMETERS:  SleepState          - Which sleep state to enter
382  *              Flags               - ACPI_EXECUTE_GTS to run optional method
383  *
384  * RETURN:      Status
385  *
386  * DESCRIPTION: Enter a system sleep state
387  *              THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED
388  *
389  ******************************************************************************/
390
391 ACPI_STATUS
392 AcpiEnterSleepState (
393     UINT8                   SleepState,
394     UINT8                   Flags)
395 {
396     ACPI_STATUS             Status;
397
398
399     ACPI_FUNCTION_TRACE (AcpiEnterSleepState);
400
401
402     if ((AcpiGbl_SleepTypeA > ACPI_SLEEP_TYPE_MAX) ||
403         (AcpiGbl_SleepTypeB > ACPI_SLEEP_TYPE_MAX))
404     {
405         ACPI_ERROR ((AE_INFO, "Sleep values out of range: A=0x%X B=0x%X",
406             AcpiGbl_SleepTypeA, AcpiGbl_SleepTypeB));
407         return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
408     }
409
410     Status = AcpiHwSleepDispatch (SleepState, Flags, ACPI_SLEEP_FUNCTION_ID);
411     return_ACPI_STATUS (Status);
412 }
413
414 ACPI_EXPORT_SYMBOL (AcpiEnterSleepState)
415
416
417 /*******************************************************************************
418  *
419  * FUNCTION:    AcpiLeaveSleepStatePrep
420  *
421  * PARAMETERS:  SleepState          - Which sleep state we are exiting
422  *              Flags               - ACPI_EXECUTE_BFS to run optional method
423  *
424  * RETURN:      Status
425  *
426  * DESCRIPTION: Perform the first state of OS-independent ACPI cleanup after a
427  *              sleep. Called with interrupts DISABLED.
428  *              We break wake/resume into 2 stages so that OSPM can handle
429  *              various OS-specific tasks between the two steps.
430  *
431  ******************************************************************************/
432
433 ACPI_STATUS
434 AcpiLeaveSleepStatePrep (
435     UINT8                   SleepState,
436     UINT8                   Flags)
437 {
438     ACPI_STATUS             Status;
439
440
441     ACPI_FUNCTION_TRACE (AcpiLeaveSleepStatePrep);
442
443
444     Status = AcpiHwSleepDispatch (SleepState, Flags, ACPI_WAKE_PREP_FUNCTION_ID);
445     return_ACPI_STATUS (Status);
446 }
447
448 ACPI_EXPORT_SYMBOL (AcpiLeaveSleepStatePrep)
449
450
451 /*******************************************************************************
452  *
453  * FUNCTION:    AcpiLeaveSleepState
454  *
455  * PARAMETERS:  SleepState          - Which sleep state we are exiting
456  *
457  * RETURN:      Status
458  *
459  * DESCRIPTION: Perform OS-independent ACPI cleanup after a sleep
460  *              Called with interrupts ENABLED.
461  *
462  ******************************************************************************/
463
464 ACPI_STATUS
465 AcpiLeaveSleepState (
466     UINT8                   SleepState)
467 {
468     ACPI_STATUS             Status;
469
470
471     ACPI_FUNCTION_TRACE (AcpiLeaveSleepState);
472
473
474     Status = AcpiHwSleepDispatch (SleepState, 0, ACPI_WAKE_FUNCTION_ID);
475     return_ACPI_STATUS (Status);
476 }
477
478 ACPI_EXPORT_SYMBOL (AcpiLeaveSleepState)