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