utxface.c revision 256281
1/******************************************************************************
2 *
3 * Module Name: utxface - External interfaces, miscellaneous utility functions
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2013, 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
67ACPI_STATUS
68AcpiTerminate (
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
117ACPI_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
135ACPI_STATUS
136AcpiSubsystemStatus (
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
150ACPI_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
171ACPI_STATUS
172AcpiGetSystemInfo (
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
233ACPI_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
248ACPI_STATUS
249AcpiGetStatistics (
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
278ACPI_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
296ACPI_STATUS
297AcpiInstallInitializationHandler (
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
316ACPI_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
331ACPI_STATUS
332AcpiPurgeCachedObjects (
333    void)
334{
335    ACPI_FUNCTION_TRACE (AcpiPurgeCachedObjects);
336
337
338    (void) AcpiOsPurgeCache (AcpiGbl_StateCache);
339    (void) AcpiOsPurgeCache (AcpiGbl_OperandCache);
340    (void) AcpiOsPurgeCache (AcpiGbl_PsNodeCache);
341    (void) AcpiOsPurgeCache (AcpiGbl_PsNodeExtCache);
342
343    return_ACPI_STATUS (AE_OK);
344}
345
346ACPI_EXPORT_SYMBOL (AcpiPurgeCachedObjects)
347
348
349/*****************************************************************************
350 *
351 * FUNCTION:    AcpiInstallInterface
352 *
353 * PARAMETERS:  InterfaceName       - The interface to install
354 *
355 * RETURN:      Status
356 *
357 * DESCRIPTION: Install an _OSI interface to the global list
358 *
359 ****************************************************************************/
360
361ACPI_STATUS
362AcpiInstallInterface (
363    ACPI_STRING             InterfaceName)
364{
365    ACPI_STATUS             Status;
366    ACPI_INTERFACE_INFO     *InterfaceInfo;
367
368
369    /* Parameter validation */
370
371    if (!InterfaceName || (ACPI_STRLEN (InterfaceName) == 0))
372    {
373        return (AE_BAD_PARAMETER);
374    }
375
376    Status = AcpiOsAcquireMutex (AcpiGbl_OsiMutex, ACPI_WAIT_FOREVER);
377    if (ACPI_FAILURE (Status))
378    {
379        return (Status);
380    }
381
382    /* Check if the interface name is already in the global list */
383
384    InterfaceInfo = AcpiUtGetInterface (InterfaceName);
385    if (InterfaceInfo)
386    {
387        /*
388         * The interface already exists in the list. This is OK if the
389         * interface has been marked invalid -- just clear the bit.
390         */
391        if (InterfaceInfo->Flags & ACPI_OSI_INVALID)
392        {
393            InterfaceInfo->Flags &= ~ACPI_OSI_INVALID;
394            Status = AE_OK;
395        }
396        else
397        {
398            Status = AE_ALREADY_EXISTS;
399        }
400    }
401    else
402    {
403        /* New interface name, install into the global list */
404
405        Status = AcpiUtInstallInterface (InterfaceName);
406    }
407
408    AcpiOsReleaseMutex (AcpiGbl_OsiMutex);
409    return (Status);
410}
411
412ACPI_EXPORT_SYMBOL (AcpiInstallInterface)
413
414
415/*****************************************************************************
416 *
417 * FUNCTION:    AcpiRemoveInterface
418 *
419 * PARAMETERS:  InterfaceName       - The interface to remove
420 *
421 * RETURN:      Status
422 *
423 * DESCRIPTION: Remove an _OSI interface from the global list
424 *
425 ****************************************************************************/
426
427ACPI_STATUS
428AcpiRemoveInterface (
429    ACPI_STRING             InterfaceName)
430{
431    ACPI_STATUS             Status;
432
433
434    /* Parameter validation */
435
436    if (!InterfaceName || (ACPI_STRLEN (InterfaceName) == 0))
437    {
438        return (AE_BAD_PARAMETER);
439    }
440
441    Status = AcpiOsAcquireMutex (AcpiGbl_OsiMutex, ACPI_WAIT_FOREVER);
442    if (ACPI_FAILURE (Status))
443    {
444        return (Status);
445    }
446
447    Status = AcpiUtRemoveInterface (InterfaceName);
448
449    AcpiOsReleaseMutex (AcpiGbl_OsiMutex);
450    return (Status);
451}
452
453ACPI_EXPORT_SYMBOL (AcpiRemoveInterface)
454
455
456/*****************************************************************************
457 *
458 * FUNCTION:    AcpiInstallInterfaceHandler
459 *
460 * PARAMETERS:  Handler             - The _OSI interface handler to install
461 *                                    NULL means "remove existing handler"
462 *
463 * RETURN:      Status
464 *
465 * DESCRIPTION: Install a handler for the predefined _OSI ACPI method.
466 *              invoked during execution of the internal implementation of
467 *              _OSI. A NULL handler simply removes any existing handler.
468 *
469 ****************************************************************************/
470
471ACPI_STATUS
472AcpiInstallInterfaceHandler (
473    ACPI_INTERFACE_HANDLER  Handler)
474{
475    ACPI_STATUS             Status;
476
477
478    Status = AcpiOsAcquireMutex (AcpiGbl_OsiMutex, ACPI_WAIT_FOREVER);
479    if (ACPI_FAILURE (Status))
480    {
481        return (Status);
482    }
483
484    if (Handler && AcpiGbl_InterfaceHandler)
485    {
486        Status = AE_ALREADY_EXISTS;
487    }
488    else
489    {
490        AcpiGbl_InterfaceHandler = Handler;
491    }
492
493    AcpiOsReleaseMutex (AcpiGbl_OsiMutex);
494    return (Status);
495}
496
497ACPI_EXPORT_SYMBOL (AcpiInstallInterfaceHandler)
498
499
500/*****************************************************************************
501 *
502 * FUNCTION:    AcpiUpdateInterfaces
503 *
504 * PARAMETERS:  Action              - Actions to be performed during the
505 *                                    update
506 *
507 * RETURN:      Status
508 *
509 * DESCRIPTION: Update _OSI interface strings, disabling or enabling OS vendor
510 *              string or/and feature group strings.
511 *
512 ****************************************************************************/
513
514ACPI_STATUS
515AcpiUpdateInterfaces (
516    UINT8                   Action)
517{
518    ACPI_STATUS             Status;
519
520
521    Status = AcpiOsAcquireMutex (AcpiGbl_OsiMutex, ACPI_WAIT_FOREVER);
522    if (ACPI_FAILURE (Status))
523    {
524        return (Status);
525    }
526
527    Status = AcpiUtUpdateInterfaces (Action);
528
529    AcpiOsReleaseMutex (AcpiGbl_OsiMutex);
530    return (Status);
531}
532
533
534/*****************************************************************************
535 *
536 * FUNCTION:    AcpiCheckAddressRange
537 *
538 * PARAMETERS:  SpaceId             - Address space ID
539 *              Address             - Start address
540 *              Length              - Length
541 *              Warn                - TRUE if warning on overlap desired
542 *
543 * RETURN:      Count of the number of conflicts detected.
544 *
545 * DESCRIPTION: Check if the input address range overlaps any of the
546 *              ASL operation region address ranges.
547 *
548 ****************************************************************************/
549
550UINT32
551AcpiCheckAddressRange (
552    ACPI_ADR_SPACE_TYPE     SpaceId,
553    ACPI_PHYSICAL_ADDRESS   Address,
554    ACPI_SIZE               Length,
555    BOOLEAN                 Warn)
556{
557    UINT32                  Overlaps;
558    ACPI_STATUS             Status;
559
560
561    Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
562    if (ACPI_FAILURE (Status))
563    {
564        return (0);
565    }
566
567    Overlaps = AcpiUtCheckAddressRange (SpaceId, Address,
568        (UINT32) Length, Warn);
569
570    (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
571    return (Overlaps);
572}
573
574ACPI_EXPORT_SYMBOL (AcpiCheckAddressRange)
575
576#endif /* !ACPI_ASL_COMPILER */
577
578
579/*******************************************************************************
580 *
581 * FUNCTION:    AcpiDecodePldBuffer
582 *
583 * PARAMETERS:  InBuffer            - Buffer returned by _PLD method
584 *              Length              - Length of the InBuffer
585 *              ReturnBuffer        - Where the decode buffer is returned
586 *
587 * RETURN:      Status and the decoded _PLD buffer. User must deallocate
588 *              the buffer via ACPI_FREE.
589 *
590 * DESCRIPTION: Decode the bit-packed buffer returned by the _PLD method into
591 *              a local struct that is much more useful to an ACPI driver.
592 *
593 ******************************************************************************/
594
595ACPI_STATUS
596AcpiDecodePldBuffer (
597    UINT8                   *InBuffer,
598    ACPI_SIZE               Length,
599    ACPI_PLD_INFO           **ReturnBuffer)
600{
601    ACPI_PLD_INFO           *PldInfo;
602    UINT32                  *Buffer = ACPI_CAST_PTR (UINT32, InBuffer);
603    UINT32                  Dword;
604
605
606    /* Parameter validation */
607
608    if (!InBuffer || !ReturnBuffer || (Length < 16))
609    {
610        return (AE_BAD_PARAMETER);
611    }
612
613    PldInfo = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_PLD_INFO));
614    if (!PldInfo)
615    {
616        return (AE_NO_MEMORY);
617    }
618
619    /* First 32-bit DWord */
620
621    ACPI_MOVE_32_TO_32 (&Dword, &Buffer[0]);
622    PldInfo->Revision =             ACPI_PLD_GET_REVISION (&Dword);
623    PldInfo->IgnoreColor =          ACPI_PLD_GET_IGNORE_COLOR (&Dword);
624    PldInfo->Color =                ACPI_PLD_GET_COLOR (&Dword);
625
626    /* Second 32-bit DWord */
627
628    ACPI_MOVE_32_TO_32 (&Dword, &Buffer[1]);
629    PldInfo->Width =                ACPI_PLD_GET_WIDTH (&Dword);
630    PldInfo->Height =               ACPI_PLD_GET_HEIGHT(&Dword);
631
632    /* Third 32-bit DWord */
633
634    ACPI_MOVE_32_TO_32 (&Dword, &Buffer[2]);
635    PldInfo->UserVisible =          ACPI_PLD_GET_USER_VISIBLE (&Dword);
636    PldInfo->Dock =                 ACPI_PLD_GET_DOCK (&Dword);
637    PldInfo->Lid =                  ACPI_PLD_GET_LID (&Dword);
638    PldInfo->Panel =                ACPI_PLD_GET_PANEL (&Dword);
639    PldInfo->VerticalPosition =     ACPI_PLD_GET_VERTICAL (&Dword);
640    PldInfo->HorizontalPosition =   ACPI_PLD_GET_HORIZONTAL (&Dword);
641    PldInfo->Shape =                ACPI_PLD_GET_SHAPE (&Dword);
642    PldInfo->GroupOrientation =     ACPI_PLD_GET_ORIENTATION (&Dword);
643    PldInfo->GroupToken =           ACPI_PLD_GET_TOKEN (&Dword);
644    PldInfo->GroupPosition =        ACPI_PLD_GET_POSITION (&Dword);
645    PldInfo->Bay =                  ACPI_PLD_GET_BAY (&Dword);
646
647    /* Fourth 32-bit DWord */
648
649    ACPI_MOVE_32_TO_32 (&Dword, &Buffer[3]);
650    PldInfo->Ejectable =            ACPI_PLD_GET_EJECTABLE (&Dword);
651    PldInfo->OspmEjectRequired =    ACPI_PLD_GET_OSPM_EJECT (&Dword);
652    PldInfo->CabinetNumber =        ACPI_PLD_GET_CABINET (&Dword);
653    PldInfo->CardCageNumber =       ACPI_PLD_GET_CARD_CAGE (&Dword);
654    PldInfo->Reference =            ACPI_PLD_GET_REFERENCE (&Dword);
655    PldInfo->Rotation =             ACPI_PLD_GET_ROTATION (&Dword);
656    PldInfo->Order =                ACPI_PLD_GET_ORDER (&Dword);
657
658    if (Length >= ACPI_PLD_BUFFER_SIZE)
659    {
660        /* Fifth 32-bit DWord (Revision 2 of _PLD) */
661
662        ACPI_MOVE_32_TO_32 (&Dword, &Buffer[4]);
663        PldInfo->VerticalOffset =       ACPI_PLD_GET_VERT_OFFSET (&Dword);
664        PldInfo->HorizontalOffset =     ACPI_PLD_GET_HORIZ_OFFSET (&Dword);
665    }
666
667    *ReturnBuffer = PldInfo;
668    return (AE_OK);
669}
670
671ACPI_EXPORT_SYMBOL (AcpiDecodePldBuffer)
672