xref: /freebsd/sys/contrib/dev/acpica/components/tables/tbxface.c (revision b7d683e608ac894e75b2de7eac5eaa083650de54)
1 /******************************************************************************
2  *
3  * Module Name: tbxface - ACPI table oriented external interfaces
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2012, 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 __TBXFACE_C__
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/actables.h>
49 
50 #define _COMPONENT          ACPI_TABLES
51         ACPI_MODULE_NAME    ("tbxface")
52 
53 
54 /*******************************************************************************
55  *
56  * FUNCTION:    AcpiAllocateRootTable
57  *
58  * PARAMETERS:  InitialTableCount   - Size of InitialTableArray, in number of
59  *                                    ACPI_TABLE_DESC structures
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Allocate a root table array. Used by iASL compiler and
64  *              AcpiInitializeTables.
65  *
66  ******************************************************************************/
67 
68 ACPI_STATUS
69 AcpiAllocateRootTable (
70     UINT32                  InitialTableCount)
71 {
72 
73     AcpiGbl_RootTableList.MaxTableCount = InitialTableCount;
74     AcpiGbl_RootTableList.Flags = ACPI_ROOT_ALLOW_RESIZE;
75 
76     return (AcpiTbResizeRootTableList ());
77 }
78 
79 
80 /*******************************************************************************
81  *
82  * FUNCTION:    AcpiInitializeTables
83  *
84  * PARAMETERS:  InitialTableArray   - Pointer to an array of pre-allocated
85  *                                    ACPI_TABLE_DESC structures. If NULL, the
86  *                                    array is dynamically allocated.
87  *              InitialTableCount   - Size of InitialTableArray, in number of
88  *                                    ACPI_TABLE_DESC structures
89  *              AllowRealloc        - Flag to tell Table Manager if resize of
90  *                                    pre-allocated array is allowed. Ignored
91  *                                    if InitialTableArray is NULL.
92  *
93  * RETURN:      Status
94  *
95  * DESCRIPTION: Initialize the table manager, get the RSDP and RSDT/XSDT.
96  *
97  * NOTE:        Allows static allocation of the initial table array in order
98  *              to avoid the use of dynamic memory in confined environments
99  *              such as the kernel boot sequence where it may not be available.
100  *
101  *              If the host OS memory managers are initialized, use NULL for
102  *              InitialTableArray, and the table will be dynamically allocated.
103  *
104  ******************************************************************************/
105 
106 ACPI_STATUS
107 AcpiInitializeTables (
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 
164 ACPI_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 
182 ACPI_STATUS
183 AcpiReallocateRootTable (
184     void)
185 {
186     ACPI_TABLE_DESC         *Tables;
187     ACPI_SIZE               NewSize;
188     ACPI_SIZE               CurrentSize;
189 
190 
191     ACPI_FUNCTION_TRACE (AcpiReallocateRootTable);
192 
193 
194     /*
195      * Only reallocate the root table if the host provided a static buffer
196      * for the table array in the call to AcpiInitializeTables.
197      */
198     if (AcpiGbl_RootTableList.Flags & ACPI_ROOT_ORIGIN_ALLOCATED)
199     {
200         return_ACPI_STATUS (AE_SUPPORT);
201     }
202 
203     /*
204      * Get the current size of the root table and add the default
205      * increment to create the new table size.
206      */
207     CurrentSize = (ACPI_SIZE)
208         AcpiGbl_RootTableList.CurrentTableCount * sizeof (ACPI_TABLE_DESC);
209 
210     NewSize = CurrentSize +
211         (ACPI_ROOT_TABLE_SIZE_INCREMENT * sizeof (ACPI_TABLE_DESC));
212 
213     /* Create new array and copy the old array */
214 
215     Tables = ACPI_ALLOCATE_ZEROED (NewSize);
216     if (!Tables)
217     {
218         return_ACPI_STATUS (AE_NO_MEMORY);
219     }
220 
221     ACPI_MEMCPY (Tables, AcpiGbl_RootTableList.Tables, CurrentSize);
222 
223     /*
224      * Update the root table descriptor. The new size will be the current
225      * number of tables plus the increment, independent of the reserved
226      * size of the original table list.
227      */
228     AcpiGbl_RootTableList.Tables = Tables;
229     AcpiGbl_RootTableList.MaxTableCount =
230         AcpiGbl_RootTableList.CurrentTableCount + ACPI_ROOT_TABLE_SIZE_INCREMENT;
231     AcpiGbl_RootTableList.Flags =
232         ACPI_ROOT_ORIGIN_ALLOCATED | ACPI_ROOT_ALLOW_RESIZE;
233 
234     return_ACPI_STATUS (AE_OK);
235 }
236 
237 ACPI_EXPORT_SYMBOL (AcpiReallocateRootTable)
238 
239 
240 /*******************************************************************************
241  *
242  * FUNCTION:    AcpiGetTableHeader
243  *
244  * PARAMETERS:  Signature           - ACPI signature of needed table
245  *              Instance            - Which instance (for SSDTs)
246  *              OutTableHeader      - The pointer to the table header to fill
247  *
248  * RETURN:      Status and pointer to mapped table header
249  *
250  * DESCRIPTION: Finds an ACPI table header.
251  *
252  * NOTE:        Caller is responsible in unmapping the header with
253  *              AcpiOsUnmapMemory
254  *
255  ******************************************************************************/
256 
257 ACPI_STATUS
258 AcpiGetTableHeader (
259     char                    *Signature,
260     UINT32                  Instance,
261     ACPI_TABLE_HEADER       *OutTableHeader)
262 {
263     UINT32                  i;
264     UINT32                  j;
265     ACPI_TABLE_HEADER       *Header;
266 
267 
268     /* Parameter validation */
269 
270     if (!Signature || !OutTableHeader)
271     {
272         return (AE_BAD_PARAMETER);
273     }
274 
275     /* Walk the root table list */
276 
277     for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
278     {
279         if (!ACPI_COMPARE_NAME (&(AcpiGbl_RootTableList.Tables[i].Signature),
280                     Signature))
281         {
282             continue;
283         }
284 
285         if (++j < Instance)
286         {
287             continue;
288         }
289 
290         if (!AcpiGbl_RootTableList.Tables[i].Pointer)
291         {
292             if ((AcpiGbl_RootTableList.Tables[i].Flags &
293                     ACPI_TABLE_ORIGIN_MASK) ==
294                 ACPI_TABLE_ORIGIN_MAPPED)
295             {
296                 Header = AcpiOsMapMemory (
297                             AcpiGbl_RootTableList.Tables[i].Address,
298                             sizeof (ACPI_TABLE_HEADER));
299                 if (!Header)
300                 {
301                     return AE_NO_MEMORY;
302                 }
303 
304                 ACPI_MEMCPY (OutTableHeader, Header, sizeof(ACPI_TABLE_HEADER));
305                 AcpiOsUnmapMemory (Header, sizeof(ACPI_TABLE_HEADER));
306             }
307             else
308             {
309                 return AE_NOT_FOUND;
310             }
311         }
312         else
313         {
314             ACPI_MEMCPY (OutTableHeader,
315                 AcpiGbl_RootTableList.Tables[i].Pointer,
316                 sizeof(ACPI_TABLE_HEADER));
317         }
318 
319         return (AE_OK);
320     }
321 
322     return (AE_NOT_FOUND);
323 }
324 
325 ACPI_EXPORT_SYMBOL (AcpiGetTableHeader)
326 
327 
328 /*******************************************************************************
329  *
330  * FUNCTION:    AcpiGetTable
331  *
332  * PARAMETERS:  Signature           - ACPI signature of needed table
333  *              Instance            - Which instance (for SSDTs)
334  *              OutTable            - Where the pointer to the table is returned
335  *
336  * RETURN:      Status and pointer to table
337  *
338  * DESCRIPTION: Finds and verifies an ACPI table.
339  *
340  ******************************************************************************/
341 
342 ACPI_STATUS
343 AcpiGetTable (
344     char                    *Signature,
345     UINT32                  Instance,
346     ACPI_TABLE_HEADER       **OutTable)
347 {
348     UINT32                  i;
349     UINT32                  j;
350     ACPI_STATUS             Status;
351 
352 
353     /* Parameter validation */
354 
355     if (!Signature || !OutTable)
356     {
357         return (AE_BAD_PARAMETER);
358     }
359 
360     /* Walk the root table list */
361 
362     for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
363     {
364         if (!ACPI_COMPARE_NAME (&(AcpiGbl_RootTableList.Tables[i].Signature),
365                 Signature))
366         {
367             continue;
368         }
369 
370         if (++j < Instance)
371         {
372             continue;
373         }
374 
375         Status = AcpiTbVerifyTable (&AcpiGbl_RootTableList.Tables[i]);
376         if (ACPI_SUCCESS (Status))
377         {
378             *OutTable = AcpiGbl_RootTableList.Tables[i].Pointer;
379         }
380 
381         return (Status);
382     }
383 
384     return (AE_NOT_FOUND);
385 }
386 
387 ACPI_EXPORT_SYMBOL (AcpiGetTable)
388 
389 
390 /*******************************************************************************
391  *
392  * FUNCTION:    AcpiGetTableByIndex
393  *
394  * PARAMETERS:  TableIndex          - Table index
395  *              Table               - Where the pointer to the table is returned
396  *
397  * RETURN:      Status and pointer to the table
398  *
399  * DESCRIPTION: Obtain a table by an index into the global table list.
400  *
401  ******************************************************************************/
402 
403 ACPI_STATUS
404 AcpiGetTableByIndex (
405     UINT32                  TableIndex,
406     ACPI_TABLE_HEADER       **Table)
407 {
408     ACPI_STATUS             Status;
409 
410 
411     ACPI_FUNCTION_TRACE (AcpiGetTableByIndex);
412 
413 
414     /* Parameter validation */
415 
416     if (!Table)
417     {
418         return_ACPI_STATUS (AE_BAD_PARAMETER);
419     }
420 
421     (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
422 
423     /* Validate index */
424 
425     if (TableIndex >= AcpiGbl_RootTableList.CurrentTableCount)
426     {
427         (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
428         return_ACPI_STATUS (AE_BAD_PARAMETER);
429     }
430 
431     if (!AcpiGbl_RootTableList.Tables[TableIndex].Pointer)
432     {
433         /* Table is not mapped, map it */
434 
435         Status = AcpiTbVerifyTable (&AcpiGbl_RootTableList.Tables[TableIndex]);
436         if (ACPI_FAILURE (Status))
437         {
438             (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
439             return_ACPI_STATUS (Status);
440         }
441     }
442 
443     *Table = AcpiGbl_RootTableList.Tables[TableIndex].Pointer;
444     (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
445     return_ACPI_STATUS (AE_OK);
446 }
447 
448 ACPI_EXPORT_SYMBOL (AcpiGetTableByIndex)
449 
450 
451 /*******************************************************************************
452  *
453  * FUNCTION:    AcpiInstallTableHandler
454  *
455  * PARAMETERS:  Handler         - Table event handler
456  *              Context         - Value passed to the handler on each event
457  *
458  * RETURN:      Status
459  *
460  * DESCRIPTION: Install table event handler
461  *
462  ******************************************************************************/
463 
464 ACPI_STATUS
465 AcpiInstallTableHandler (
466     ACPI_TABLE_HANDLER      Handler,
467     void                    *Context)
468 {
469     ACPI_STATUS             Status;
470 
471 
472     ACPI_FUNCTION_TRACE (AcpiInstallTableHandler);
473 
474 
475     if (!Handler)
476     {
477         return_ACPI_STATUS (AE_BAD_PARAMETER);
478     }
479 
480     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
481     if (ACPI_FAILURE (Status))
482     {
483         return_ACPI_STATUS (Status);
484     }
485 
486     /* Don't allow more than one handler */
487 
488     if (AcpiGbl_TableHandler)
489     {
490         Status = AE_ALREADY_EXISTS;
491         goto Cleanup;
492     }
493 
494     /* Install the handler */
495 
496     AcpiGbl_TableHandler = Handler;
497     AcpiGbl_TableHandlerContext = Context;
498 
499 Cleanup:
500     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
501     return_ACPI_STATUS (Status);
502 }
503 
504 ACPI_EXPORT_SYMBOL (AcpiInstallTableHandler)
505 
506 
507 /*******************************************************************************
508  *
509  * FUNCTION:    AcpiRemoveTableHandler
510  *
511  * PARAMETERS:  Handler         - Table event handler that was installed
512  *                                previously.
513  *
514  * RETURN:      Status
515  *
516  * DESCRIPTION: Remove table event handler
517  *
518  ******************************************************************************/
519 
520 ACPI_STATUS
521 AcpiRemoveTableHandler (
522     ACPI_TABLE_HANDLER      Handler)
523 {
524     ACPI_STATUS             Status;
525 
526 
527     ACPI_FUNCTION_TRACE (AcpiRemoveTableHandler);
528 
529 
530     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
531     if (ACPI_FAILURE (Status))
532     {
533         return_ACPI_STATUS (Status);
534     }
535 
536     /* Make sure that the installed handler is the same */
537 
538     if (!Handler ||
539         Handler != AcpiGbl_TableHandler)
540     {
541         Status = AE_BAD_PARAMETER;
542         goto Cleanup;
543     }
544 
545     /* Remove the handler */
546 
547     AcpiGbl_TableHandler = NULL;
548 
549 Cleanup:
550     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
551     return_ACPI_STATUS (Status);
552 }
553 
554 ACPI_EXPORT_SYMBOL (AcpiRemoveTableHandler)
555 
556