]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/components/utilities/utxface.c
Merge ACPICA 20120816.
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / components / utilities / utxface.c
1 /******************************************************************************
2  *
3  * Module Name: utxface - External interfaces, miscellaneous utility functions
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
45 #define __UTXFACE_C__
46
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acdebug.h>
50
51 #define _COMPONENT          ACPI_UTILITIES
52         ACPI_MODULE_NAME    ("utxface")
53
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    AcpiTerminate
58  *
59  * PARAMETERS:  None
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Shutdown the ACPICA subsystem and release all resources.
64  *
65  ******************************************************************************/
66
67 ACPI_STATUS
68 AcpiTerminate (
69     void)
70 {
71     ACPI_STATUS         Status;
72
73
74     ACPI_FUNCTION_TRACE (AcpiTerminate);
75
76
77     /* Just exit if subsystem is already shutdown */
78
79     if (AcpiGbl_Shutdown)
80     {
81         ACPI_ERROR ((AE_INFO, "ACPI Subsystem is already terminated"));
82         return_ACPI_STATUS (AE_OK);
83     }
84
85     /* Subsystem appears active, go ahead and shut it down */
86
87     AcpiGbl_Shutdown = TRUE;
88     AcpiGbl_StartupFlags = 0;
89     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Shutting down ACPI Subsystem\n"));
90
91     /* Terminate the AML Debugger if present */
92
93     ACPI_DEBUGGER_EXEC (AcpiGbl_DbTerminateThreads = TRUE);
94
95     /* Shutdown and free all resources */
96
97     AcpiUtSubsystemShutdown ();
98
99     /* Free the mutex objects */
100
101     AcpiUtMutexTerminate ();
102
103
104 #ifdef ACPI_DEBUGGER
105
106     /* Shut down the debugger */
107
108     AcpiDbTerminate ();
109 #endif
110
111     /* Now we can shutdown the OS-dependent layer */
112
113     Status = AcpiOsTerminate ();
114     return_ACPI_STATUS (Status);
115 }
116
117 ACPI_EXPORT_SYMBOL (AcpiTerminate)
118
119
120 #ifndef ACPI_ASL_COMPILER
121 /*******************************************************************************
122  *
123  * FUNCTION:    AcpiSubsystemStatus
124  *
125  * PARAMETERS:  None
126  *
127  * RETURN:      Status of the ACPI subsystem
128  *
129  * DESCRIPTION: Other drivers that use the ACPI subsystem should call this
130  *              before making any other calls, to ensure the subsystem
131  *              initialized successfully.
132  *
133  ******************************************************************************/
134
135 ACPI_STATUS
136 AcpiSubsystemStatus (
137     void)
138 {
139
140     if (AcpiGbl_StartupFlags & ACPI_INITIALIZED_OK)
141     {
142         return (AE_OK);
143     }
144     else
145     {
146         return (AE_ERROR);
147     }
148 }
149
150 ACPI_EXPORT_SYMBOL (AcpiSubsystemStatus)
151
152
153 /*******************************************************************************
154  *
155  * FUNCTION:    AcpiGetSystemInfo
156  *
157  * PARAMETERS:  OutBuffer       - A buffer to receive the resources for the
158  *                                device
159  *
160  * RETURN:      Status          - the status of the call
161  *
162  * DESCRIPTION: This function is called to get information about the current
163  *              state of the ACPI subsystem.  It will return system information
164  *              in the OutBuffer.
165  *
166  *              If the function fails an appropriate status will be returned
167  *              and the value of OutBuffer is undefined.
168  *
169  ******************************************************************************/
170
171 ACPI_STATUS
172 AcpiGetSystemInfo (
173     ACPI_BUFFER             *OutBuffer)
174 {
175     ACPI_SYSTEM_INFO        *InfoPtr;
176     ACPI_STATUS             Status;
177
178
179     ACPI_FUNCTION_TRACE (AcpiGetSystemInfo);
180
181
182     /* Parameter validation */
183
184     Status = AcpiUtValidateBuffer (OutBuffer);
185     if (ACPI_FAILURE (Status))
186     {
187         return_ACPI_STATUS (Status);
188     }
189
190     /* Validate/Allocate/Clear caller buffer */
191
192     Status = AcpiUtInitializeBuffer (OutBuffer, sizeof (ACPI_SYSTEM_INFO));
193     if (ACPI_FAILURE (Status))
194     {
195         return_ACPI_STATUS (Status);
196     }
197
198     /*
199      * Populate the return buffer
200      */
201     InfoPtr = (ACPI_SYSTEM_INFO *) OutBuffer->Pointer;
202
203     InfoPtr->AcpiCaVersion = ACPI_CA_VERSION;
204
205     /* System flags (ACPI capabilities) */
206
207     InfoPtr->Flags = ACPI_SYS_MODE_ACPI;
208
209     /* Timer resolution - 24 or 32 bits  */
210
211     if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER)
212     {
213         InfoPtr->TimerResolution = 24;
214     }
215     else
216     {
217         InfoPtr->TimerResolution = 32;
218     }
219
220     /* Clear the reserved fields */
221
222     InfoPtr->Reserved1 = 0;
223     InfoPtr->Reserved2 = 0;
224
225     /* Current debug levels */
226
227     InfoPtr->DebugLayer = AcpiDbgLayer;
228     InfoPtr->DebugLevel = AcpiDbgLevel;
229
230     return_ACPI_STATUS (AE_OK);
231 }
232
233 ACPI_EXPORT_SYMBOL (AcpiGetSystemInfo)
234
235
236 /*******************************************************************************
237  *
238  * FUNCTION:    AcpiGetStatistics
239  *
240  * PARAMETERS:  Stats           - Where the statistics are returned
241  *
242  * RETURN:      Status          - the status of the call
243  *
244  * DESCRIPTION: Get the contents of the various system counters
245  *
246  ******************************************************************************/
247
248 ACPI_STATUS
249 AcpiGetStatistics (
250     ACPI_STATISTICS         *Stats)
251 {
252     ACPI_FUNCTION_TRACE (AcpiGetStatistics);
253
254
255     /* Parameter validation */
256
257     if (!Stats)
258     {
259         return_ACPI_STATUS (AE_BAD_PARAMETER);
260     }
261
262     /* Various interrupt-based event counters */
263
264     Stats->SciCount = AcpiSciCount;
265     Stats->GpeCount = AcpiGpeCount;
266
267     ACPI_MEMCPY (Stats->FixedEventCount, AcpiFixedEventCount,
268         sizeof (AcpiFixedEventCount));
269
270
271     /* Other counters */
272
273     Stats->MethodCount = AcpiMethodCount;
274
275     return_ACPI_STATUS (AE_OK);
276 }
277
278 ACPI_EXPORT_SYMBOL (AcpiGetStatistics)
279
280
281 /*****************************************************************************
282  *
283  * FUNCTION:    AcpiInstallInitializationHandler
284  *
285  * PARAMETERS:  Handler             - Callback procedure
286  *              Function            - Not (currently) used, see below
287  *
288  * RETURN:      Status
289  *
290  * DESCRIPTION: Install an initialization handler
291  *
292  * TBD: When a second function is added, must save the Function also.
293  *
294  ****************************************************************************/
295
296 ACPI_STATUS
297 AcpiInstallInitializationHandler (
298     ACPI_INIT_HANDLER       Handler,
299     UINT32                  Function)
300 {
301
302     if (!Handler)
303     {
304         return (AE_BAD_PARAMETER);
305     }
306
307     if (AcpiGbl_InitHandler)
308     {
309         return (AE_ALREADY_EXISTS);
310     }
311
312     AcpiGbl_InitHandler = Handler;
313     return AE_OK;
314 }
315
316 ACPI_EXPORT_SYMBOL (AcpiInstallInitializationHandler)
317
318
319 /*****************************************************************************
320  *
321  * FUNCTION:    AcpiPurgeCachedObjects
322  *
323  * PARAMETERS:  None
324  *
325  * RETURN:      Status
326  *
327  * DESCRIPTION: Empty all caches (delete the cached objects)
328  *
329  ****************************************************************************/
330
331 ACPI_STATUS
332 AcpiPurgeCachedObjects (
333     void)
334 {
335     ACPI_FUNCTION_TRACE (AcpiPurgeCachedObjects);
336
337     (void) AcpiOsPurgeCache (AcpiGbl_StateCache);
338     (void) AcpiOsPurgeCache (AcpiGbl_OperandCache);
339     (void) AcpiOsPurgeCache (AcpiGbl_PsNodeCache);
340     (void) AcpiOsPurgeCache (AcpiGbl_PsNodeExtCache);
341     return_ACPI_STATUS (AE_OK);
342 }
343
344 ACPI_EXPORT_SYMBOL (AcpiPurgeCachedObjects)
345
346
347 /*****************************************************************************
348  *
349  * FUNCTION:    AcpiInstallInterface
350  *
351  * PARAMETERS:  InterfaceName       - The interface to install
352  *
353  * RETURN:      Status
354  *
355  * DESCRIPTION: Install an _OSI interface to the global list
356  *
357  ****************************************************************************/
358
359 ACPI_STATUS
360 AcpiInstallInterface (
361     ACPI_STRING             InterfaceName)
362 {
363     ACPI_STATUS             Status;
364     ACPI_INTERFACE_INFO     *InterfaceInfo;
365
366
367     /* Parameter validation */
368
369     if (!InterfaceName || (ACPI_STRLEN (InterfaceName) == 0))
370     {
371         return (AE_BAD_PARAMETER);
372     }
373
374     (void) AcpiOsAcquireMutex (AcpiGbl_OsiMutex, ACPI_WAIT_FOREVER);
375
376     /* Check if the interface name is already in the global list */
377
378     InterfaceInfo = AcpiUtGetInterface (InterfaceName);
379     if (InterfaceInfo)
380     {
381         /*
382          * The interface already exists in the list. This is OK if the
383          * interface has been marked invalid -- just clear the bit.
384          */
385         if (InterfaceInfo->Flags & ACPI_OSI_INVALID)
386         {
387             InterfaceInfo->Flags &= ~ACPI_OSI_INVALID;
388             Status = AE_OK;
389         }
390         else
391         {
392             Status = AE_ALREADY_EXISTS;
393         }
394     }
395     else
396     {
397         /* New interface name, install into the global list */
398
399         Status = AcpiUtInstallInterface (InterfaceName);
400     }
401
402     AcpiOsReleaseMutex (AcpiGbl_OsiMutex);
403     return (Status);
404 }
405
406 ACPI_EXPORT_SYMBOL (AcpiInstallInterface)
407
408
409 /*****************************************************************************
410  *
411  * FUNCTION:    AcpiRemoveInterface
412  *
413  * PARAMETERS:  InterfaceName       - The interface to remove
414  *
415  * RETURN:      Status
416  *
417  * DESCRIPTION: Remove an _OSI interface from the global list
418  *
419  ****************************************************************************/
420
421 ACPI_STATUS
422 AcpiRemoveInterface (
423     ACPI_STRING             InterfaceName)
424 {
425     ACPI_STATUS             Status;
426
427
428     /* Parameter validation */
429
430     if (!InterfaceName || (ACPI_STRLEN (InterfaceName) == 0))
431     {
432         return (AE_BAD_PARAMETER);
433     }
434
435     (void) AcpiOsAcquireMutex (AcpiGbl_OsiMutex, ACPI_WAIT_FOREVER);
436
437     Status = AcpiUtRemoveInterface (InterfaceName);
438
439     AcpiOsReleaseMutex (AcpiGbl_OsiMutex);
440     return (Status);
441 }
442
443 ACPI_EXPORT_SYMBOL (AcpiRemoveInterface)
444
445
446 /*****************************************************************************
447  *
448  * FUNCTION:    AcpiInstallInterfaceHandler
449  *
450  * PARAMETERS:  Handler             - The _OSI interface handler to install
451  *                                    NULL means "remove existing handler"
452  *
453  * RETURN:      Status
454  *
455  * DESCRIPTION: Install a handler for the predefined _OSI ACPI method.
456  *              invoked during execution of the internal implementation of
457  *              _OSI. A NULL handler simply removes any existing handler.
458  *
459  ****************************************************************************/
460
461 ACPI_STATUS
462 AcpiInstallInterfaceHandler (
463     ACPI_INTERFACE_HANDLER  Handler)
464 {
465     ACPI_STATUS             Status = AE_OK;
466
467
468     (void) AcpiOsAcquireMutex (AcpiGbl_OsiMutex, ACPI_WAIT_FOREVER);
469
470     if (Handler && AcpiGbl_InterfaceHandler)
471     {
472         Status = AE_ALREADY_EXISTS;
473     }
474     else
475     {
476         AcpiGbl_InterfaceHandler = Handler;
477     }
478
479     AcpiOsReleaseMutex (AcpiGbl_OsiMutex);
480     return (Status);
481 }
482
483 ACPI_EXPORT_SYMBOL (AcpiInstallInterfaceHandler)
484
485
486 /*****************************************************************************
487  *
488  * FUNCTION:    AcpiCheckAddressRange
489  *
490  * PARAMETERS:  SpaceId             - Address space ID
491  *              Address             - Start address
492  *              Length              - Length
493  *              Warn                - TRUE if warning on overlap desired
494  *
495  * RETURN:      Count of the number of conflicts detected.
496  *
497  * DESCRIPTION: Check if the input address range overlaps any of the
498  *              ASL operation region address ranges.
499  *
500  ****************************************************************************/
501
502 UINT32
503 AcpiCheckAddressRange (
504     ACPI_ADR_SPACE_TYPE     SpaceId,
505     ACPI_PHYSICAL_ADDRESS   Address,
506     ACPI_SIZE               Length,
507     BOOLEAN                 Warn)
508 {
509     UINT32                  Overlaps;
510     ACPI_STATUS             Status;
511
512
513     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
514     if (ACPI_FAILURE (Status))
515     {
516         return (0);
517     }
518
519     Overlaps = AcpiUtCheckAddressRange (SpaceId, Address,
520         (UINT32) Length, Warn);
521
522     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
523     return (Overlaps);
524 }
525
526 ACPI_EXPORT_SYMBOL (AcpiCheckAddressRange)
527
528 #endif /* !ACPI_ASL_COMPILER */
529
530
531 /*******************************************************************************
532  *
533  * FUNCTION:    AcpiDecodePldBuffer
534  *
535  * PARAMETERS:  InBuffer            - Buffer returned by _PLD method
536  *              Length              - Length of the InBuffer
537  *              ReturnBuffer        - Where the decode buffer is returned
538  *
539  * RETURN:      Status and the decoded _PLD buffer. User must deallocate
540  *              the buffer via ACPI_FREE.
541  *
542  * DESCRIPTION: Decode the bit-packed buffer returned by the _PLD method into
543  *              a local struct that is much more useful to an ACPI driver.
544  *
545  ******************************************************************************/
546
547 ACPI_STATUS
548 AcpiDecodePldBuffer (
549     UINT8                   *InBuffer,
550     ACPI_SIZE               Length,
551     ACPI_PLD_INFO           **ReturnBuffer)
552 {
553     ACPI_PLD_INFO           *PldInfo;
554     UINT32                  *Buffer = ACPI_CAST_PTR (UINT32, InBuffer);
555     UINT32                  Dword;
556
557
558     /* Parameter validation */
559
560     if (!InBuffer || !ReturnBuffer || (Length < 16))
561     {
562         return (AE_BAD_PARAMETER);
563     }
564
565     PldInfo = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PLD_INFO));
566     if (!PldInfo)
567     {
568         return (AE_NO_MEMORY);
569     }
570
571     /* First 32-bit DWord */
572
573     ACPI_MOVE_32_TO_32 (&Dword, &Buffer[0]);
574     PldInfo->Revision =             ACPI_PLD_GET_REVISION (&Dword);
575     PldInfo->IgnoreColor =          ACPI_PLD_GET_IGNORE_COLOR (&Dword);
576     PldInfo->Color =                ACPI_PLD_GET_COLOR (&Dword);
577
578     /* Second 32-bit DWord */
579
580     ACPI_MOVE_32_TO_32 (&Dword, &Buffer[1]);
581     PldInfo->Width =                ACPI_PLD_GET_WIDTH (&Dword);
582     PldInfo->Height =               ACPI_PLD_GET_HEIGHT(&Dword);
583
584     /* Third 32-bit DWord */
585
586     ACPI_MOVE_32_TO_32 (&Dword, &Buffer[2]);
587     PldInfo->UserVisible =          ACPI_PLD_GET_USER_VISIBLE (&Dword);
588     PldInfo->Dock =                 ACPI_PLD_GET_DOCK (&Dword);
589     PldInfo->Lid =                  ACPI_PLD_GET_LID (&Dword);
590     PldInfo->Panel =                ACPI_PLD_GET_PANEL (&Dword);
591     PldInfo->VerticalPosition =     ACPI_PLD_GET_VERTICAL (&Dword);
592     PldInfo->HorizontalPosition =   ACPI_PLD_GET_HORIZONTAL (&Dword);
593     PldInfo->Shape =                ACPI_PLD_GET_SHAPE (&Dword);
594     PldInfo->GroupOrientation =     ACPI_PLD_GET_ORIENTATION (&Dword);
595     PldInfo->GroupToken =           ACPI_PLD_GET_TOKEN (&Dword);
596     PldInfo->GroupPosition =        ACPI_PLD_GET_POSITION (&Dword);
597     PldInfo->Bay =                  ACPI_PLD_GET_BAY (&Dword);
598
599     /* Fourth 32-bit DWord */
600
601     ACPI_MOVE_32_TO_32 (&Dword, &Buffer[3]);
602     PldInfo->Ejectable =            ACPI_PLD_GET_EJECTABLE (&Dword);
603     PldInfo->OspmEjectRequired =    ACPI_PLD_GET_OSPM_EJECT (&Dword);
604     PldInfo->CabinetNumber =        ACPI_PLD_GET_CABINET (&Dword);
605     PldInfo->CardCageNumber =       ACPI_PLD_GET_CARD_CAGE (&Dword);
606     PldInfo->Reference =            ACPI_PLD_GET_REFERENCE (&Dword);
607     PldInfo->Rotation =             ACPI_PLD_GET_ROTATION (&Dword);
608     PldInfo->Order =                ACPI_PLD_GET_ORDER (&Dword);
609
610     if (Length >= ACPI_PLD_BUFFER_SIZE)
611     {
612         /* Fifth 32-bit DWord (Revision 2 of _PLD) */
613
614         ACPI_MOVE_32_TO_32 (&Dword, &Buffer[4]);
615         PldInfo->VerticalOffset =       ACPI_PLD_GET_VERT_OFFSET (&Dword);
616         PldInfo->HorizontalOffset =     ACPI_PLD_GET_HORIZ_OFFSET (&Dword);
617     }
618
619     *ReturnBuffer = PldInfo;
620     return (AE_OK);
621 }
622
623 ACPI_EXPORT_SYMBOL (AcpiDecodePldBuffer)