tbxface.c revision 241973
1238438Sdteske/******************************************************************************
2238438Sdteske *
3238438Sdteske * Module Name: tbxface - ACPI table oriented external interfaces
4263150Sdteske *
5238438Sdteske *****************************************************************************/
6238438Sdteske
7238438Sdteske/*
8238438Sdteske * Copyright (C) 2000 - 2012, Intel Corp.
9238438Sdteske * All rights reserved.
10238438Sdteske *
11238438Sdteske * Redistribution and use in source and binary forms, with or without
12238438Sdteske * modification, are permitted provided that the following conditions
13238438Sdteske * are met:
14238438Sdteske * 1. Redistributions of source code must retain the above copyright
15238438Sdteske *    notice, this list of conditions, and the following disclaimer,
16238438Sdteske *    without modification.
17238438Sdteske * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18238438Sdteske *    substantially similar to the "NO WARRANTY" disclaimer below
19238438Sdteske *    ("Disclaimer") and any redistribution must be conditioned upon
20238438Sdteske *    including a substantially similar Disclaimer requirement for further
21238438Sdteske *    binary redistribution.
22238438Sdteske * 3. Neither the names of the above-listed copyright holders nor the names
23238438Sdteske *    of any contributors may be used to endorse or promote products derived
24238438Sdteske *    from this software without specific prior written permission.
25238438Sdteske *
26238438Sdteske * Alternatively, this software may be distributed under the terms of the
27238438Sdteske * GNU General Public License ("GPL") version 2 as published by the Free
28238438Sdteske * Software Foundation.
29238438Sdteske *
30238438Sdteske * NO WARRANTY
31238438Sdteske * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32240684Sdteske * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33240684Sdteske * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34244675Sdteske * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35240684Sdteske * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36240684Sdteske * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37263150Sdteske * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38240684Sdteske * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39238438Sdteske * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40240684Sdteske * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41238438Sdteske * POSSIBILITY OF SUCH DAMAGES.
42238438Sdteske */
43259054Sdteske
44259054Sdteske#define __TBXFACE_C__
45238438Sdteske
46238438Sdteske#include <contrib/dev/acpica/include/acpi.h>
47238438Sdteske#include <contrib/dev/acpica/include/accommon.h>
48238438Sdteske#include <contrib/dev/acpica/include/actables.h>
49238438Sdteske
50238438Sdteske#define _COMPONENT          ACPI_TABLES
51238438Sdteske        ACPI_MODULE_NAME    ("tbxface")
52238438Sdteske
53238438Sdteske
54250633Sdteske/*******************************************************************************
55238438Sdteske *
56252178Sdteske * FUNCTION:    AcpiAllocateRootTable
57238438Sdteske *
58238438Sdteske * PARAMETERS:  InitialTableCount   - Size of InitialTableArray, in number of
59238438Sdteske *                                    ACPI_TABLE_DESC structures
60238438Sdteske *
61238438Sdteske * RETURN:      Status
62238438Sdteske *
63238438Sdteske * DESCRIPTION: Allocate a root table array. Used by iASL compiler and
64238438Sdteske *              AcpiInitializeTables.
65238438Sdteske *
66238438Sdteske ******************************************************************************/
67238438Sdteske
68238438SdteskeACPI_STATUS
69263150SdteskeAcpiAllocateRootTable (
70238438Sdteske    UINT32                  InitialTableCount)
71263150Sdteske{
72263150Sdteske
73263150Sdteske    AcpiGbl_RootTableList.MaxTableCount = InitialTableCount;
74263150Sdteske    AcpiGbl_RootTableList.Flags = ACPI_ROOT_ALLOW_RESIZE;
75263150Sdteske
76263150Sdteske    return (AcpiTbResizeRootTableList ());
77263150Sdteske}
78263150Sdteske
79263150Sdteske
80238438Sdteske/*******************************************************************************
81249751Sdteske *
82238438Sdteske * FUNCTION:    AcpiInitializeTables
83251236Sdteske *
84244550Sdteske * PARAMETERS:  InitialTableArray   - Pointer to an array of pre-allocated
85249751Sdteske *                                    ACPI_TABLE_DESC structures. If NULL, the
86238438Sdteske *                                    array is dynamically allocated.
87256181Sdteske *              InitialTableCount   - Size of InitialTableArray, in number of
88238438Sdteske *                                    ACPI_TABLE_DESC structures
89252019Sdteske *              AllowRealloc        - Flag to tell Table Manager if resize of
90252019Sdteske *                                    pre-allocated array is allowed. Ignored
91252019Sdteske *                                    if InitialTableArray is NULL.
92252019Sdteske *
93263150Sdteske * RETURN:      Status
94238438Sdteske *
95238438Sdteske * DESCRIPTION: Initialize the table manager, get the RSDP and RSDT/XSDT.
96238438Sdteske *
97238438Sdteske * NOTE:        Allows static allocation of the initial table array in order
98238438Sdteske *              to avoid the use of dynamic memory in confined environments
99238438Sdteske *              such as the kernel boot sequence where it may not be available.
100238438Sdteske *
101 *              If the host OS memory managers are initialized, use NULL for
102 *              InitialTableArray, and the table will be dynamically allocated.
103 *
104 ******************************************************************************/
105
106ACPI_STATUS
107AcpiInitializeTables (
108    ACPI_TABLE_DESC         *InitialTableArray,
109    UINT32                  InitialTableCount,
110    BOOLEAN                 AllowResize)
111{
112    ACPI_PHYSICAL_ADDRESS   RsdpAddress;
113    ACPI_STATUS             Status;
114
115
116    ACPI_FUNCTION_TRACE (AcpiInitializeTables);
117
118
119    /*
120     * Set up the Root Table Array
121     * Allocate the table array if requested
122     */
123    if (!InitialTableArray)
124    {
125        Status = AcpiAllocateRootTable (InitialTableCount);
126        if (ACPI_FAILURE (Status))
127        {
128            return_ACPI_STATUS (Status);
129        }
130    }
131    else
132    {
133        /* Root Table Array has been statically allocated by the host */
134
135        ACPI_MEMSET (InitialTableArray, 0,
136            (ACPI_SIZE) InitialTableCount * sizeof (ACPI_TABLE_DESC));
137
138        AcpiGbl_RootTableList.Tables = InitialTableArray;
139        AcpiGbl_RootTableList.MaxTableCount = InitialTableCount;
140        AcpiGbl_RootTableList.Flags = ACPI_ROOT_ORIGIN_UNKNOWN;
141        if (AllowResize)
142        {
143            AcpiGbl_RootTableList.Flags |= ACPI_ROOT_ALLOW_RESIZE;
144        }
145    }
146
147    /* Get the address of the RSDP */
148
149    RsdpAddress = AcpiOsGetRootPointer ();
150    if (!RsdpAddress)
151    {
152        return_ACPI_STATUS (AE_NOT_FOUND);
153    }
154
155    /*
156     * Get the root table (RSDT or XSDT) and extract all entries to the local
157     * Root Table Array. This array contains the information of the RSDT/XSDT
158     * in a common, more useable format.
159     */
160    Status = AcpiTbParseRootTable (RsdpAddress);
161    return_ACPI_STATUS (Status);
162}
163
164ACPI_EXPORT_SYMBOL (AcpiInitializeTables)
165
166
167/*******************************************************************************
168 *
169 * FUNCTION:    AcpiReallocateRootTable
170 *
171 * PARAMETERS:  None
172 *
173 * RETURN:      Status
174 *
175 * DESCRIPTION: Reallocate Root Table List into dynamic memory. Copies the
176 *              root list from the previously provided scratch area. Should
177 *              be called once dynamic memory allocation is available in the
178 *              kernel.
179 *
180 ******************************************************************************/
181
182ACPI_STATUS
183AcpiReallocateRootTable (
184    void)
185{
186    ACPI_STATUS             Status;
187
188
189    ACPI_FUNCTION_TRACE (AcpiReallocateRootTable);
190
191
192    /*
193     * Only reallocate the root table if the host provided a static buffer
194     * for the table array in the call to AcpiInitializeTables.
195     */
196    if (AcpiGbl_RootTableList.Flags & ACPI_ROOT_ORIGIN_ALLOCATED)
197    {
198        return_ACPI_STATUS (AE_SUPPORT);
199    }
200
201    AcpiGbl_RootTableList.Flags |= ACPI_ROOT_ALLOW_RESIZE;
202
203    Status = AcpiTbResizeRootTableList ();
204    return_ACPI_STATUS (Status);
205}
206
207ACPI_EXPORT_SYMBOL (AcpiReallocateRootTable)
208
209
210/*******************************************************************************
211 *
212 * FUNCTION:    AcpiGetTableHeader
213 *
214 * PARAMETERS:  Signature           - ACPI signature of needed table
215 *              Instance            - Which instance (for SSDTs)
216 *              OutTableHeader      - The pointer to the table header to fill
217 *
218 * RETURN:      Status and pointer to mapped table header
219 *
220 * DESCRIPTION: Finds an ACPI table header.
221 *
222 * NOTE:        Caller is responsible in unmapping the header with
223 *              AcpiOsUnmapMemory
224 *
225 ******************************************************************************/
226
227ACPI_STATUS
228AcpiGetTableHeader (
229    char                    *Signature,
230    UINT32                  Instance,
231    ACPI_TABLE_HEADER       *OutTableHeader)
232{
233    UINT32                  i;
234    UINT32                  j;
235    ACPI_TABLE_HEADER       *Header;
236
237
238    /* Parameter validation */
239
240    if (!Signature || !OutTableHeader)
241    {
242        return (AE_BAD_PARAMETER);
243    }
244
245    /* Walk the root table list */
246
247    for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
248    {
249        if (!ACPI_COMPARE_NAME (&(AcpiGbl_RootTableList.Tables[i].Signature),
250                    Signature))
251        {
252            continue;
253        }
254
255        if (++j < Instance)
256        {
257            continue;
258        }
259
260        if (!AcpiGbl_RootTableList.Tables[i].Pointer)
261        {
262            if ((AcpiGbl_RootTableList.Tables[i].Flags &
263                    ACPI_TABLE_ORIGIN_MASK) ==
264                ACPI_TABLE_ORIGIN_MAPPED)
265            {
266                Header = AcpiOsMapMemory (
267                            AcpiGbl_RootTableList.Tables[i].Address,
268                            sizeof (ACPI_TABLE_HEADER));
269                if (!Header)
270                {
271                    return (AE_NO_MEMORY);
272                }
273
274                ACPI_MEMCPY (OutTableHeader, Header,
275                    sizeof (ACPI_TABLE_HEADER));
276                AcpiOsUnmapMemory (Header, sizeof (ACPI_TABLE_HEADER));
277            }
278            else
279            {
280                return (AE_NOT_FOUND);
281            }
282        }
283        else
284        {
285            ACPI_MEMCPY (OutTableHeader,
286                AcpiGbl_RootTableList.Tables[i].Pointer,
287                sizeof (ACPI_TABLE_HEADER));
288        }
289
290        return (AE_OK);
291    }
292
293    return (AE_NOT_FOUND);
294}
295
296ACPI_EXPORT_SYMBOL (AcpiGetTableHeader)
297
298
299/*******************************************************************************
300 *
301 * FUNCTION:    AcpiGetTable
302 *
303 * PARAMETERS:  Signature           - ACPI signature of needed table
304 *              Instance            - Which instance (for SSDTs)
305 *              OutTable            - Where the pointer to the table is returned
306 *
307 * RETURN:      Status and pointer to table
308 *
309 * DESCRIPTION: Finds and verifies an ACPI table.
310 *
311 ******************************************************************************/
312
313ACPI_STATUS
314AcpiGetTable (
315    char                    *Signature,
316    UINT32                  Instance,
317    ACPI_TABLE_HEADER       **OutTable)
318{
319    UINT32                  i;
320    UINT32                  j;
321    ACPI_STATUS             Status;
322
323
324    /* Parameter validation */
325
326    if (!Signature || !OutTable)
327    {
328        return (AE_BAD_PARAMETER);
329    }
330
331    /* Walk the root table list */
332
333    for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
334    {
335        if (!ACPI_COMPARE_NAME (&(AcpiGbl_RootTableList.Tables[i].Signature),
336                Signature))
337        {
338            continue;
339        }
340
341        if (++j < Instance)
342        {
343            continue;
344        }
345
346        Status = AcpiTbVerifyTable (&AcpiGbl_RootTableList.Tables[i]);
347        if (ACPI_SUCCESS (Status))
348        {
349            *OutTable = AcpiGbl_RootTableList.Tables[i].Pointer;
350        }
351
352        return (Status);
353    }
354
355    return (AE_NOT_FOUND);
356}
357
358ACPI_EXPORT_SYMBOL (AcpiGetTable)
359
360
361/*******************************************************************************
362 *
363 * FUNCTION:    AcpiGetTableByIndex
364 *
365 * PARAMETERS:  TableIndex          - Table index
366 *              Table               - Where the pointer to the table is returned
367 *
368 * RETURN:      Status and pointer to the table
369 *
370 * DESCRIPTION: Obtain a table by an index into the global table list.
371 *
372 ******************************************************************************/
373
374ACPI_STATUS
375AcpiGetTableByIndex (
376    UINT32                  TableIndex,
377    ACPI_TABLE_HEADER       **Table)
378{
379    ACPI_STATUS             Status;
380
381
382    ACPI_FUNCTION_TRACE (AcpiGetTableByIndex);
383
384
385    /* Parameter validation */
386
387    if (!Table)
388    {
389        return_ACPI_STATUS (AE_BAD_PARAMETER);
390    }
391
392    (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
393
394    /* Validate index */
395
396    if (TableIndex >= AcpiGbl_RootTableList.CurrentTableCount)
397    {
398        (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
399        return_ACPI_STATUS (AE_BAD_PARAMETER);
400    }
401
402    if (!AcpiGbl_RootTableList.Tables[TableIndex].Pointer)
403    {
404        /* Table is not mapped, map it */
405
406        Status = AcpiTbVerifyTable (&AcpiGbl_RootTableList.Tables[TableIndex]);
407        if (ACPI_FAILURE (Status))
408        {
409            (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
410            return_ACPI_STATUS (Status);
411        }
412    }
413
414    *Table = AcpiGbl_RootTableList.Tables[TableIndex].Pointer;
415    (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
416    return_ACPI_STATUS (AE_OK);
417}
418
419ACPI_EXPORT_SYMBOL (AcpiGetTableByIndex)
420
421
422/*******************************************************************************
423 *
424 * FUNCTION:    AcpiInstallTableHandler
425 *
426 * PARAMETERS:  Handler         - Table event handler
427 *              Context         - Value passed to the handler on each event
428 *
429 * RETURN:      Status
430 *
431 * DESCRIPTION: Install table event handler
432 *
433 ******************************************************************************/
434
435ACPI_STATUS
436AcpiInstallTableHandler (
437    ACPI_TABLE_HANDLER      Handler,
438    void                    *Context)
439{
440    ACPI_STATUS             Status;
441
442
443    ACPI_FUNCTION_TRACE (AcpiInstallTableHandler);
444
445
446    if (!Handler)
447    {
448        return_ACPI_STATUS (AE_BAD_PARAMETER);
449    }
450
451    Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
452    if (ACPI_FAILURE (Status))
453    {
454        return_ACPI_STATUS (Status);
455    }
456
457    /* Don't allow more than one handler */
458
459    if (AcpiGbl_TableHandler)
460    {
461        Status = AE_ALREADY_EXISTS;
462        goto Cleanup;
463    }
464
465    /* Install the handler */
466
467    AcpiGbl_TableHandler = Handler;
468    AcpiGbl_TableHandlerContext = Context;
469
470Cleanup:
471    (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
472    return_ACPI_STATUS (Status);
473}
474
475ACPI_EXPORT_SYMBOL (AcpiInstallTableHandler)
476
477
478/*******************************************************************************
479 *
480 * FUNCTION:    AcpiRemoveTableHandler
481 *
482 * PARAMETERS:  Handler         - Table event handler that was installed
483 *                                previously.
484 *
485 * RETURN:      Status
486 *
487 * DESCRIPTION: Remove table event handler
488 *
489 ******************************************************************************/
490
491ACPI_STATUS
492AcpiRemoveTableHandler (
493    ACPI_TABLE_HANDLER      Handler)
494{
495    ACPI_STATUS             Status;
496
497
498    ACPI_FUNCTION_TRACE (AcpiRemoveTableHandler);
499
500
501    Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
502    if (ACPI_FAILURE (Status))
503    {
504        return_ACPI_STATUS (Status);
505    }
506
507    /* Make sure that the installed handler is the same */
508
509    if (!Handler ||
510        Handler != AcpiGbl_TableHandler)
511    {
512        Status = AE_BAD_PARAMETER;
513        goto Cleanup;
514    }
515
516    /* Remove the handler */
517
518    AcpiGbl_TableHandler = NULL;
519
520Cleanup:
521    (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
522    return_ACPI_STATUS (Status);
523}
524
525ACPI_EXPORT_SYMBOL (AcpiRemoveTableHandler)
526