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