xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/tables/tbxface.c (revision 2510baa4685f8f570c607ceedfd73473d69342c4)
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 - 2012, 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 __TBXFACE_C__
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  *              AllowRealloc        - 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      * Set up the Root Table Array
193      * Allocate the table array 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         ACPI_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 (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 (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 (&(AcpiGbl_RootTableList.Tables[i].Signature),
322                     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_MAPPED)
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                 ACPI_MEMCPY (OutTableHeader, Header,
347                     sizeof (ACPI_TABLE_HEADER));
348                 AcpiOsUnmapMemory (Header, sizeof (ACPI_TABLE_HEADER));
349             }
350             else
351             {
352                 return (AE_NOT_FOUND);
353             }
354         }
355         else
356         {
357             ACPI_MEMCPY (OutTableHeader,
358                 AcpiGbl_RootTableList.Tables[i].Pointer,
359                 sizeof (ACPI_TABLE_HEADER));
360         }
361 
362         return (AE_OK);
363     }
364 
365     return (AE_NOT_FOUND);
366 }
367 
368 ACPI_EXPORT_SYMBOL (AcpiGetTableHeader)
369 
370 
371 /*******************************************************************************
372  *
373  * FUNCTION:    AcpiGetTable
374  *
375  * PARAMETERS:  Signature           - ACPI signature of needed table
376  *              Instance            - Which instance (for SSDTs)
377  *              OutTable            - Where the pointer to the table is returned
378  *
379  * RETURN:      Status and pointer to table
380  *
381  * DESCRIPTION: Finds and verifies an ACPI table.
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 (&(AcpiGbl_RootTableList.Tables[i].Signature),
408                 Signature))
409         {
410             continue;
411         }
412 
413         if (++j < Instance)
414         {
415             continue;
416         }
417 
418         Status = AcpiTbVerifyTable (&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 table
441  *
442  * DESCRIPTION: Obtain a table by an index into the global table list.
443  *
444  ******************************************************************************/
445 
446 ACPI_STATUS
447 AcpiGetTableByIndex (
448     UINT32                  TableIndex,
449     ACPI_TABLE_HEADER       **Table)
450 {
451     ACPI_STATUS             Status;
452 
453 
454     ACPI_FUNCTION_TRACE (AcpiGetTableByIndex);
455 
456 
457     /* Parameter validation */
458 
459     if (!Table)
460     {
461         return_ACPI_STATUS (AE_BAD_PARAMETER);
462     }
463 
464     (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
465 
466     /* Validate index */
467 
468     if (TableIndex >= AcpiGbl_RootTableList.CurrentTableCount)
469     {
470         (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
471         return_ACPI_STATUS (AE_BAD_PARAMETER);
472     }
473 
474     if (!AcpiGbl_RootTableList.Tables[TableIndex].Pointer)
475     {
476         /* Table is not mapped, map it */
477 
478         Status = AcpiTbVerifyTable (&AcpiGbl_RootTableList.Tables[TableIndex]);
479         if (ACPI_FAILURE (Status))
480         {
481             (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
482             return_ACPI_STATUS (Status);
483         }
484     }
485 
486     *Table = AcpiGbl_RootTableList.Tables[TableIndex].Pointer;
487     (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
488     return_ACPI_STATUS (AE_OK);
489 }
490 
491 ACPI_EXPORT_SYMBOL (AcpiGetTableByIndex)
492 
493 
494 /*******************************************************************************
495  *
496  * FUNCTION:    AcpiInstallTableHandler
497  *
498  * PARAMETERS:  Handler         - Table event handler
499  *              Context         - Value passed to the handler on each event
500  *
501  * RETURN:      Status
502  *
503  * DESCRIPTION: Install table event handler
504  *
505  ******************************************************************************/
506 
507 ACPI_STATUS
508 AcpiInstallTableHandler (
509     ACPI_TABLE_HANDLER      Handler,
510     void                    *Context)
511 {
512     ACPI_STATUS             Status;
513 
514 
515     ACPI_FUNCTION_TRACE (AcpiInstallTableHandler);
516 
517 
518     if (!Handler)
519     {
520         return_ACPI_STATUS (AE_BAD_PARAMETER);
521     }
522 
523     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
524     if (ACPI_FAILURE (Status))
525     {
526         return_ACPI_STATUS (Status);
527     }
528 
529     /* Don't allow more than one handler */
530 
531     if (AcpiGbl_TableHandler)
532     {
533         Status = AE_ALREADY_EXISTS;
534         goto Cleanup;
535     }
536 
537     /* Install the handler */
538 
539     AcpiGbl_TableHandler = Handler;
540     AcpiGbl_TableHandlerContext = Context;
541 
542 Cleanup:
543     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
544     return_ACPI_STATUS (Status);
545 }
546 
547 ACPI_EXPORT_SYMBOL (AcpiInstallTableHandler)
548 
549 
550 /*******************************************************************************
551  *
552  * FUNCTION:    AcpiRemoveTableHandler
553  *
554  * PARAMETERS:  Handler         - Table event handler that was installed
555  *                                previously.
556  *
557  * RETURN:      Status
558  *
559  * DESCRIPTION: Remove table event handler
560  *
561  ******************************************************************************/
562 
563 ACPI_STATUS
564 AcpiRemoveTableHandler (
565     ACPI_TABLE_HANDLER      Handler)
566 {
567     ACPI_STATUS             Status;
568 
569 
570     ACPI_FUNCTION_TRACE (AcpiRemoveTableHandler);
571 
572 
573     Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);
574     if (ACPI_FAILURE (Status))
575     {
576         return_ACPI_STATUS (Status);
577     }
578 
579     /* Make sure that the installed handler is the same */
580 
581     if (!Handler ||
582         Handler != AcpiGbl_TableHandler)
583     {
584         Status = AE_BAD_PARAMETER;
585         goto Cleanup;
586     }
587 
588     /* Remove the handler */
589 
590     AcpiGbl_TableHandler = NULL;
591 
592 Cleanup:
593     (void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);
594     return_ACPI_STATUS (Status);
595 }
596 
597 ACPI_EXPORT_SYMBOL (AcpiRemoveTableHandler)
598