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