xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/tables/tbxface.c (revision a5a3b2d9a3d95cbae71eaf371708c73a1780ac0d)
1 /******************************************************************************
2  *
3  * Module Name: tbxface - ACPI table-oriented external interfaces
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115 
116 #define EXPORT_ACPI_INTERFACES
117 
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "actables.h"
121 
122 #define _COMPONENT          ACPI_TABLES
123         ACPI_MODULE_NAME    ("tbxface")
124 
125 
126 /*******************************************************************************
127  *
128  * FUNCTION:    AcpiAllocateRootTable
129  *
130  * PARAMETERS:  InitialTableCount   - Size of InitialTableArray, in number of
131  *                                    ACPI_TABLE_DESC structures
132  *
133  * RETURN:      Status
134  *
135  * DESCRIPTION: Allocate a root table array. Used by iASL compiler and
136  *              AcpiInitializeTables.
137  *
138  ******************************************************************************/
139 
140 ACPI_STATUS
141 AcpiAllocateRootTable (
142     UINT32                  InitialTableCount)
143 {
144 
145     AcpiGbl_RootTableList.MaxTableCount = InitialTableCount;
146     AcpiGbl_RootTableList.Flags = ACPI_ROOT_ALLOW_RESIZE;
147 
148     return (AcpiTbResizeRootTableList ());
149 }
150 
151 
152 /*******************************************************************************
153  *
154  * FUNCTION:    AcpiInitializeTables
155  *
156  * PARAMETERS:  InitialTableArray   - Pointer to an array of pre-allocated
157  *                                    ACPI_TABLE_DESC structures. If NULL, the
158  *                                    array is dynamically allocated.
159  *              InitialTableCount   - Size of InitialTableArray, in number of
160  *                                    ACPI_TABLE_DESC structures
161  *              AllowResize         - Flag to tell Table Manager if resize of
162  *                                    pre-allocated array is allowed. Ignored
163  *                                    if InitialTableArray is NULL.
164  *
165  * RETURN:      Status
166  *
167  * DESCRIPTION: Initialize the table manager, get the RSDP and RSDT/XSDT.
168  *
169  * NOTE:        Allows static allocation of the initial table array in order
170  *              to avoid the use of dynamic memory in confined environments
171  *              such as the kernel boot sequence where it may not be available.
172  *
173  *              If the host OS memory managers are initialized, use NULL for
174  *              InitialTableArray, and the table will be dynamically allocated.
175  *
176  ******************************************************************************/
177 
178 ACPI_STATUS
179 AcpiInitializeTables (
180     ACPI_TABLE_DESC         *InitialTableArray,
181     UINT32                  InitialTableCount,
182     BOOLEAN                 AllowResize)
183 {
184     ACPI_PHYSICAL_ADDRESS   RsdpAddress;
185     ACPI_STATUS             Status;
186 
187 
188     ACPI_FUNCTION_TRACE (AcpiInitializeTables);
189 
190 
191     /*
192      * Setup the Root Table Array and allocate the table array
193      * if requested
194      */
195     if (!InitialTableArray)
196     {
197         Status = AcpiAllocateRootTable (InitialTableCount);
198         if (ACPI_FAILURE (Status))
199         {
200             return_ACPI_STATUS (Status);
201         }
202     }
203     else
204     {
205         /* Root Table Array has been statically allocated by the host */
206 
207         memset (InitialTableArray, 0,
208             (ACPI_SIZE) InitialTableCount * sizeof (ACPI_TABLE_DESC));
209 
210         AcpiGbl_RootTableList.Tables = InitialTableArray;
211         AcpiGbl_RootTableList.MaxTableCount = InitialTableCount;
212         AcpiGbl_RootTableList.Flags = ACPI_ROOT_ORIGIN_UNKNOWN;
213         if (AllowResize)
214         {
215             AcpiGbl_RootTableList.Flags |= ACPI_ROOT_ALLOW_RESIZE;
216         }
217     }
218 
219     /* Get the address of the RSDP */
220 
221     RsdpAddress = AcpiOsGetRootPointer ();
222     if (!RsdpAddress)
223     {
224         return_ACPI_STATUS (AE_NOT_FOUND);
225     }
226 
227     /*
228      * Get the root table (RSDT or XSDT) and extract all entries to the local
229      * Root Table Array. This array contains the information of the RSDT/XSDT
230      * in a common, more useable format.
231      */
232     Status = AcpiTbParseRootTable (RsdpAddress);
233     return_ACPI_STATUS (Status);
234 }
235 
236 ACPI_EXPORT_SYMBOL_INIT (AcpiInitializeTables)
237 
238 
239 /*******************************************************************************
240  *
241  * FUNCTION:    AcpiReallocateRootTable
242  *
243  * PARAMETERS:  None
244  *
245  * RETURN:      Status
246  *
247  * DESCRIPTION: Reallocate Root Table List into dynamic memory. Copies the
248  *              root list from the previously provided scratch area. Should
249  *              be called once dynamic memory allocation is available in the
250  *              kernel.
251  *
252  ******************************************************************************/
253 
254 ACPI_STATUS
255 AcpiReallocateRootTable (
256     void)
257 {
258     ACPI_STATUS             Status;
259 
260 
261     ACPI_FUNCTION_TRACE (AcpiReallocateRootTable);
262 
263 
264     /*
265      * Only reallocate the root table if the host provided a static buffer
266      * for the table array in the call to AcpiInitializeTables.
267      */
268     if (AcpiGbl_RootTableList.Flags & ACPI_ROOT_ORIGIN_ALLOCATED)
269     {
270         return_ACPI_STATUS (AE_SUPPORT);
271     }
272 
273     AcpiGbl_RootTableList.Flags |= ACPI_ROOT_ALLOW_RESIZE;
274 
275     Status = AcpiTbResizeRootTableList ();
276     return_ACPI_STATUS (Status);
277 }
278 
279 ACPI_EXPORT_SYMBOL_INIT (AcpiReallocateRootTable)
280 
281 
282 /*******************************************************************************
283  *
284  * FUNCTION:    AcpiGetTableHeader
285  *
286  * PARAMETERS:  Signature           - ACPI signature of needed table
287  *              Instance            - Which instance (for SSDTs)
288  *              OutTableHeader      - The pointer to the table header to fill
289  *
290  * RETURN:      Status and pointer to mapped table header
291  *
292  * DESCRIPTION: Finds an ACPI table header.
293  *
294  * NOTE:        Caller is responsible in unmapping the header with
295  *              AcpiOsUnmapMemory
296  *
297  ******************************************************************************/
298 
299 ACPI_STATUS
300 AcpiGetTableHeader (
301     char                    *Signature,
302     UINT32                  Instance,
303     ACPI_TABLE_HEADER       *OutTableHeader)
304 {
305     UINT32                  i;
306     UINT32                  j;
307     ACPI_TABLE_HEADER       *Header;
308 
309 
310     /* Parameter validation */
311 
312     if (!Signature || !OutTableHeader)
313     {
314         return (AE_BAD_PARAMETER);
315     }
316 
317     /* Walk the root table list */
318 
319     for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
320     {
321         if (!ACPI_COMPARE_NAME (
322                 &(AcpiGbl_RootTableList.Tables[i].Signature), Signature))
323         {
324             continue;
325         }
326 
327         if (++j < Instance)
328         {
329             continue;
330         }
331 
332         if (!AcpiGbl_RootTableList.Tables[i].Pointer)
333         {
334             if ((AcpiGbl_RootTableList.Tables[i].Flags &
335                     ACPI_TABLE_ORIGIN_MASK) ==
336                 ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL)
337             {
338                 Header = AcpiOsMapMemory (
339                     AcpiGbl_RootTableList.Tables[i].Address,
340                     sizeof (ACPI_TABLE_HEADER));
341                 if (!Header)
342                 {
343                     return (AE_NO_MEMORY);
344                 }
345 
346                 memcpy (OutTableHeader, Header, sizeof (ACPI_TABLE_HEADER));
347                 AcpiOsUnmapMemory (Header, sizeof (ACPI_TABLE_HEADER));
348             }
349             else
350             {
351                 return (AE_NOT_FOUND);
352             }
353         }
354         else
355         {
356             memcpy (OutTableHeader,
357                 AcpiGbl_RootTableList.Tables[i].Pointer,
358                 sizeof (ACPI_TABLE_HEADER));
359         }
360 
361         return (AE_OK);
362     }
363 
364     return (AE_NOT_FOUND);
365 }
366 
367 ACPI_EXPORT_SYMBOL (AcpiGetTableHeader)
368 
369 
370 /*******************************************************************************
371  *
372  * FUNCTION:    AcpiGetTable
373  *
374  * PARAMETERS:  Signature           - ACPI signature of needed table
375  *              Instance            - Which instance (for SSDTs)
376  *              OutTable            - Where the pointer to the table is returned
377  *
378  * RETURN:      Status and pointer to the requested table
379  *
380  * DESCRIPTION: Finds and verifies an ACPI table. Table must be in the
381  *              RSDT/XSDT.
382  *
383  ******************************************************************************/
384 
385 ACPI_STATUS
386 AcpiGetTable (
387     char                    *Signature,
388     UINT32                  Instance,
389     ACPI_TABLE_HEADER       **OutTable)
390 {
391     UINT32                  i;
392     UINT32                  j;
393     ACPI_STATUS             Status;
394 
395 
396     /* Parameter validation */
397 
398     if (!Signature || !OutTable)
399     {
400         return (AE_BAD_PARAMETER);
401     }
402 
403     /* Walk the root table list */
404 
405     for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
406     {
407         if (!ACPI_COMPARE_NAME (
408                 &(AcpiGbl_RootTableList.Tables[i].Signature), Signature))
409         {
410             continue;
411         }
412 
413         if (++j < Instance)
414         {
415             continue;
416         }
417 
418         Status = AcpiTbValidateTable (&AcpiGbl_RootTableList.Tables[i]);
419         if (ACPI_SUCCESS (Status))
420         {
421             *OutTable = AcpiGbl_RootTableList.Tables[i].Pointer;
422         }
423 
424         return (Status);
425     }
426 
427     return (AE_NOT_FOUND);
428 }
429 
430 ACPI_EXPORT_SYMBOL (AcpiGetTable)
431 
432 
433 /*******************************************************************************
434  *
435  * FUNCTION:    AcpiGetTableByIndex
436  *
437  * PARAMETERS:  TableIndex          - Table index
438  *              Table               - Where the pointer to the table is returned
439  *
440  * RETURN:      Status and pointer to the requested table
441  *
442  * DESCRIPTION: Obtain a table by an index into the global table list. Used
443  *              internally also.
444  *
445  ******************************************************************************/
446 
447 ACPI_STATUS
448 AcpiGetTableByIndex (
449     UINT32                  TableIndex,
450     ACPI_TABLE_HEADER       **Table)
451 {
452     ACPI_STATUS             Status;
453 
454 
455     ACPI_FUNCTION_TRACE (AcpiGetTableByIndex);
456 
457 
458     /* Parameter validation */
459 
460     if (!Table)
461     {
462         return_ACPI_STATUS (AE_BAD_PARAMETER);
463     }
464 
465     (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
466 
467     /* Validate index */
468 
469     if (TableIndex >= AcpiGbl_RootTableList.CurrentTableCount)
470     {
471         (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
472         return_ACPI_STATUS (AE_BAD_PARAMETER);
473     }
474 
475     if (!AcpiGbl_RootTableList.Tables[TableIndex].Pointer)
476     {
477         /* Table is not mapped, map it */
478 
479         Status = AcpiTbValidateTable (
480             &AcpiGbl_RootTableList.Tables[TableIndex]);
481         if (ACPI_FAILURE (Status))
482         {
483             (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
484             return_ACPI_STATUS (Status);
485         }
486     }
487 
488     *Table = AcpiGbl_RootTableList.Tables[TableIndex].Pointer;
489     (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
490     return_ACPI_STATUS (AE_OK);
491 }
492 
493 ACPI_EXPORT_SYMBOL (AcpiGetTableByIndex)
494 
495 
496 /*******************************************************************************
497  *
498  * FUNCTION:    AcpiInstallTableHandler
499  *
500  * PARAMETERS:  Handler         - Table event handler
501  *              Context         - Value passed to the handler on each event
502  *
503  * RETURN:      Status
504  *
505  * DESCRIPTION: Install a global table event handler.
506  *
507  ******************************************************************************/
508 
509 ACPI_STATUS
510 AcpiInstallTableHandler (
511     ACPI_TABLE_HANDLER      Handler,
512     void                    *Context)
513 {
514     ACPI_STATUS             Status;
515 
516 
517     ACPI_FUNCTION_TRACE (AcpiInstallTableHandler);
518 
519 
520     if (!Handler)
521     {
522         return_ACPI_STATUS (AE_BAD_PARAMETER);
523     }
524 
525     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
526     if (ACPI_FAILURE (Status))
527     {
528         return_ACPI_STATUS (Status);
529     }
530 
531     /* Don't allow more than one handler */
532 
533     if (AcpiGbl_TableHandler)
534     {
535         Status = AE_ALREADY_EXISTS;
536         goto Cleanup;
537     }
538 
539     /* Install the handler */
540 
541     AcpiGbl_TableHandler = Handler;
542     AcpiGbl_TableHandlerContext = Context;
543 
544 Cleanup:
545     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
546     return_ACPI_STATUS (Status);
547 }
548 
549 ACPI_EXPORT_SYMBOL (AcpiInstallTableHandler)
550 
551 
552 /*******************************************************************************
553  *
554  * FUNCTION:    AcpiRemoveTableHandler
555  *
556  * PARAMETERS:  Handler         - Table event handler that was installed
557  *                                previously.
558  *
559  * RETURN:      Status
560  *
561  * DESCRIPTION: Remove a table event handler
562  *
563  ******************************************************************************/
564 
565 ACPI_STATUS
566 AcpiRemoveTableHandler (
567     ACPI_TABLE_HANDLER      Handler)
568 {
569     ACPI_STATUS             Status;
570 
571 
572     ACPI_FUNCTION_TRACE (AcpiRemoveTableHandler);
573 
574 
575     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
576     if (ACPI_FAILURE (Status))
577     {
578         return_ACPI_STATUS (Status);
579     }
580 
581     /* Make sure that the installed handler is the same */
582 
583     if (!Handler ||
584         Handler != AcpiGbl_TableHandler)
585     {
586         Status = AE_BAD_PARAMETER;
587         goto Cleanup;
588     }
589 
590     /* Remove the handler */
591 
592     AcpiGbl_TableHandler = NULL;
593 
594 Cleanup:
595     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
596     return_ACPI_STATUS (Status);
597 }
598 
599 ACPI_EXPORT_SYMBOL (AcpiRemoveTableHandler)
600