xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/tables/tbxfload.c (revision 6c2abee2f5e73c3fc81c33da51ac610f8bf1117a)
1 /******************************************************************************
2  *
3  * Module Name: tbxfload - Table load/unload 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 "acnamesp.h"
121 #include "actables.h"
122 #include "acevents.h"
123 
124 #define _COMPONENT          ACPI_TABLES
125         ACPI_MODULE_NAME    ("tbxfload")
126 
127 
128 /*******************************************************************************
129  *
130  * FUNCTION:    AcpiLoadTables
131  *
132  * PARAMETERS:  None
133  *
134  * RETURN:      Status
135  *
136  * DESCRIPTION: Load the ACPI tables from the RSDT/XSDT
137  *
138  ******************************************************************************/
139 
140 ACPI_STATUS ACPI_INIT_FUNCTION
141 AcpiLoadTables (
142     void)
143 {
144     ACPI_STATUS             Status;
145 
146 
147     ACPI_FUNCTION_TRACE (AcpiLoadTables);
148 
149 
150     /*
151      * Install the default operation region handlers. These are the
152      * handlers that are defined by the ACPI specification to be
153      * "always accessible" -- namely, SystemMemory, SystemIO, and
154      * PCI_Config. This also means that no _REG methods need to be
155      * run for these address spaces. We need to have these handlers
156      * installed before any AML code can be executed, especially any
157      * module-level code (11/2015).
158      * Note that we allow OSPMs to install their own region handlers
159      * between AcpiInitializeSubsystem() and AcpiLoadTables() to use
160      * their customized default region handlers.
161      */
162     Status = AcpiEvInstallRegionHandlers ();
163     if (ACPI_FAILURE (Status))
164     {
165         ACPI_EXCEPTION ((AE_INFO, Status, "During Region initialization"));
166         return_ACPI_STATUS (Status);
167     }
168 
169     /* Load the namespace from the tables */
170 
171     Status = AcpiTbLoadNamespace ();
172 
173     /* Don't let single failures abort the load */
174 
175     if (Status == AE_CTRL_TERMINATE)
176     {
177         Status = AE_OK;
178     }
179 
180     if (ACPI_FAILURE (Status))
181     {
182         ACPI_EXCEPTION ((AE_INFO, Status,
183             "While loading namespace from ACPI tables"));
184     }
185 
186     if (!AcpiGbl_GroupModuleLevelCode)
187     {
188         /*
189          * Initialize the objects that remain uninitialized. This
190          * runs the executable AML that may be part of the
191          * declaration of these objects:
192          * OperationRegions, BufferFields, Buffers, and Packages.
193          */
194         Status = AcpiNsInitializeObjects ();
195         if (ACPI_FAILURE (Status))
196         {
197             return_ACPI_STATUS (Status);
198         }
199     }
200 
201     AcpiGbl_NamespaceInitialized = TRUE;
202     return_ACPI_STATUS (Status);
203 }
204 
205 ACPI_EXPORT_SYMBOL_INIT (AcpiLoadTables)
206 
207 
208 /*******************************************************************************
209  *
210  * FUNCTION:    AcpiTbLoadNamespace
211  *
212  * PARAMETERS:  None
213  *
214  * RETURN:      Status
215  *
216  * DESCRIPTION: Load the namespace from the DSDT and all SSDTs/PSDTs found in
217  *              the RSDT/XSDT.
218  *
219  ******************************************************************************/
220 
221 ACPI_STATUS
222 AcpiTbLoadNamespace (
223     void)
224 {
225     ACPI_STATUS             Status;
226     UINT32                  i;
227     ACPI_TABLE_HEADER       *NewDsdt;
228     ACPI_TABLE_DESC         *Table;
229     UINT32                  TablesLoaded = 0;
230     UINT32                  TablesFailed = 0;
231 
232 
233     ACPI_FUNCTION_TRACE (TbLoadNamespace);
234 
235 
236     (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
237 
238     /*
239      * Load the namespace. The DSDT is required, but any SSDT and
240      * PSDT tables are optional. Verify the DSDT.
241      */
242     Table = &AcpiGbl_RootTableList.Tables[AcpiGbl_DsdtIndex];
243 
244     if (!AcpiGbl_RootTableList.CurrentTableCount ||
245         !ACPI_COMPARE_NAME (Table->Signature.Ascii, ACPI_SIG_DSDT) ||
246          ACPI_FAILURE (AcpiTbValidateTable (Table)))
247     {
248         Status = AE_NO_ACPI_TABLES;
249         goto UnlockAndExit;
250     }
251 
252     /*
253      * Save the DSDT pointer for simple access. This is the mapped memory
254      * address. We must take care here because the address of the .Tables
255      * array can change dynamically as tables are loaded at run-time. Note:
256      * .Pointer field is not validated until after call to AcpiTbValidateTable.
257      */
258     AcpiGbl_DSDT = Table->Pointer;
259 
260     /*
261      * Optionally copy the entire DSDT to local memory (instead of simply
262      * mapping it.) There are some BIOSs that corrupt or replace the original
263      * DSDT, creating the need for this option. Default is FALSE, do not copy
264      * the DSDT.
265      */
266     if (AcpiGbl_CopyDsdtLocally)
267     {
268         NewDsdt = AcpiTbCopyDsdt (AcpiGbl_DsdtIndex);
269         if (NewDsdt)
270         {
271             AcpiGbl_DSDT = NewDsdt;
272         }
273     }
274 
275     /*
276      * Save the original DSDT header for detection of table corruption
277      * and/or replacement of the DSDT from outside the OS.
278      */
279     memcpy (&AcpiGbl_OriginalDsdtHeader, AcpiGbl_DSDT,
280         sizeof (ACPI_TABLE_HEADER));
281 
282     (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
283 
284     /* Load and parse tables */
285 
286     Status = AcpiNsLoadTable (AcpiGbl_DsdtIndex, AcpiGbl_RootNode);
287     if (ACPI_FAILURE (Status))
288     {
289         ACPI_EXCEPTION ((AE_INFO, Status, "[DSDT] table load failed"));
290         TablesFailed++;
291     }
292     else
293     {
294         TablesLoaded++;
295     }
296 
297     /* Load any SSDT or PSDT tables. Note: Loop leaves tables locked */
298 
299     (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
300     for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; ++i)
301     {
302         Table = &AcpiGbl_RootTableList.Tables[i];
303 
304         if (!AcpiGbl_RootTableList.Tables[i].Address ||
305             (!ACPI_COMPARE_NAME (Table->Signature.Ascii, ACPI_SIG_SSDT) &&
306              !ACPI_COMPARE_NAME (Table->Signature.Ascii, ACPI_SIG_PSDT) &&
307              !ACPI_COMPARE_NAME (Table->Signature.Ascii, ACPI_SIG_OSDT)) ||
308              ACPI_FAILURE (AcpiTbValidateTable (Table)))
309         {
310             continue;
311         }
312 
313         /* Ignore errors while loading tables, get as many as possible */
314 
315         (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
316         Status =  AcpiNsLoadTable (i, AcpiGbl_RootNode);
317         if (ACPI_FAILURE (Status))
318         {
319             ACPI_EXCEPTION ((AE_INFO, Status, "(%4.4s:%8.8s) while loading table",
320                 Table->Signature.Ascii, Table->Pointer->OemTableId));
321 
322             TablesFailed++;
323 
324             ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INIT,
325                 "Table [%4.4s:%8.8s] (id FF) - Table namespace load failed\n\n",
326                 Table->Signature.Ascii, Table->Pointer->OemTableId));
327         }
328         else
329         {
330             TablesLoaded++;
331         }
332 
333         (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
334     }
335 
336     if (!TablesFailed)
337     {
338         ACPI_INFO ((
339             "%u ACPI AML tables successfully acquired and loaded\n",
340             TablesLoaded));
341     }
342     else
343     {
344         ACPI_ERROR ((AE_INFO,
345             "%u table load failures, %u successful",
346             TablesFailed, TablesLoaded));
347 
348         /* Indicate at least one failure */
349 
350         Status = AE_CTRL_TERMINATE;
351     }
352 
353 UnlockAndExit:
354     (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
355     return_ACPI_STATUS (Status);
356 }
357 
358 
359 /*******************************************************************************
360  *
361  * FUNCTION:    AcpiInstallTable
362  *
363  * PARAMETERS:  Address             - Address of the ACPI table to be installed.
364  *              Physical            - Whether the address is a physical table
365  *                                    address or not
366  *
367  * RETURN:      Status
368  *
369  * DESCRIPTION: Dynamically install an ACPI table.
370  *              Note: This function should only be invoked after
371  *                    AcpiInitializeTables() and before AcpiLoadTables().
372  *
373  ******************************************************************************/
374 
375 ACPI_STATUS ACPI_INIT_FUNCTION
376 AcpiInstallTable (
377     ACPI_PHYSICAL_ADDRESS   Address,
378     BOOLEAN                 Physical)
379 {
380     ACPI_STATUS             Status;
381     UINT8                   Flags;
382     UINT32                  TableIndex;
383 
384 
385     ACPI_FUNCTION_TRACE (AcpiInstallTable);
386 
387 
388     if (Physical)
389     {
390         Flags = ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL;
391     }
392     else
393     {
394         Flags = ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL;
395     }
396 
397     Status = AcpiTbInstallStandardTable (Address, Flags,
398         FALSE, FALSE, &TableIndex);
399 
400     return_ACPI_STATUS (Status);
401 }
402 
403 ACPI_EXPORT_SYMBOL_INIT (AcpiInstallTable)
404 
405 
406 /*******************************************************************************
407  *
408  * FUNCTION:    AcpiLoadTable
409  *
410  * PARAMETERS:  Table               - Pointer to a buffer containing the ACPI
411  *                                    table to be loaded.
412  *
413  * RETURN:      Status
414  *
415  * DESCRIPTION: Dynamically load an ACPI table from the caller's buffer. Must
416  *              be a valid ACPI table with a valid ACPI table header.
417  *              Note1: Mainly intended to support hotplug addition of SSDTs.
418  *              Note2: Does not copy the incoming table. User is responsible
419  *              to ensure that the table is not deleted or unmapped.
420  *
421  ******************************************************************************/
422 
423 ACPI_STATUS
424 AcpiLoadTable (
425     ACPI_TABLE_HEADER       *Table)
426 {
427     ACPI_STATUS             Status;
428     UINT32                  TableIndex;
429 
430 
431     ACPI_FUNCTION_TRACE (AcpiLoadTable);
432 
433 
434     /* Parameter validation */
435 
436     if (!Table)
437     {
438         return_ACPI_STATUS (AE_BAD_PARAMETER);
439     }
440 
441     /* Must acquire the interpreter lock during this operation */
442 
443     Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER);
444     if (ACPI_FAILURE (Status))
445     {
446         return_ACPI_STATUS (Status);
447     }
448 
449     /* Install the table and load it into the namespace */
450 
451     ACPI_INFO (("Host-directed Dynamic ACPI Table Load:"));
452     (void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);
453 
454     Status = AcpiTbInstallStandardTable (ACPI_PTR_TO_PHYSADDR (Table),
455         ACPI_TABLE_ORIGIN_EXTERNAL_VIRTUAL, TRUE, FALSE,
456         &TableIndex);
457 
458     (void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);
459     if (ACPI_FAILURE (Status))
460     {
461         goto UnlockAndExit;
462     }
463 
464     /*
465      * Note: Now table is "INSTALLED", it must be validated before
466      * using.
467      */
468     Status = AcpiTbValidateTable (
469         &AcpiGbl_RootTableList.Tables[TableIndex]);
470     if (ACPI_FAILURE (Status))
471     {
472         goto UnlockAndExit;
473     }
474 
475     Status = AcpiNsLoadTable (TableIndex, AcpiGbl_RootNode);
476 
477     /* Invoke table handler if present */
478 
479     if (AcpiGbl_TableHandler)
480     {
481         (void) AcpiGbl_TableHandler (ACPI_TABLE_EVENT_LOAD, Table,
482             AcpiGbl_TableHandlerContext);
483     }
484 
485 UnlockAndExit:
486     (void) AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);
487     return_ACPI_STATUS (Status);
488 }
489 
490 ACPI_EXPORT_SYMBOL (AcpiLoadTable)
491 
492 
493 /*******************************************************************************
494  *
495  * FUNCTION:    AcpiUnloadParentTable
496  *
497  * PARAMETERS:  Object              - Handle to any namespace object owned by
498  *                                    the table to be unloaded
499  *
500  * RETURN:      Status
501  *
502  * DESCRIPTION: Via any namespace object within an SSDT or OEMx table, unloads
503  *              the table and deletes all namespace objects associated with
504  *              that table. Unloading of the DSDT is not allowed.
505  *              Note: Mainly intended to support hotplug removal of SSDTs.
506  *
507  ******************************************************************************/
508 
509 ACPI_STATUS
510 AcpiUnloadParentTable (
511     ACPI_HANDLE             Object)
512 {
513     ACPI_NAMESPACE_NODE     *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Object);
514     ACPI_STATUS             Status = AE_NOT_EXIST;
515     ACPI_OWNER_ID           OwnerId;
516     UINT32                  i;
517 
518 
519     ACPI_FUNCTION_TRACE (AcpiUnloadParentTable);
520 
521 
522     /* Parameter validation */
523 
524     if (!Object)
525     {
526         return_ACPI_STATUS (AE_BAD_PARAMETER);
527     }
528 
529     /*
530      * The node OwnerId is currently the same as the parent table ID.
531      * However, this could change in the future.
532      */
533     OwnerId = Node->OwnerId;
534     if (!OwnerId)
535     {
536         /* OwnerId==0 means DSDT is the owner. DSDT cannot be unloaded */
537 
538         return_ACPI_STATUS (AE_TYPE);
539     }
540 
541     /* Must acquire the interpreter lock during this operation */
542 
543     Status = AcpiUtAcquireMutex (ACPI_MTX_INTERPRETER);
544     if (ACPI_FAILURE (Status))
545     {
546         return_ACPI_STATUS (Status);
547     }
548 
549     /* Find the table in the global table list */
550 
551     for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)
552     {
553         if (OwnerId != AcpiGbl_RootTableList.Tables[i].OwnerId)
554         {
555             continue;
556         }
557 
558         /*
559          * Allow unload of SSDT and OEMx tables only. Do not allow unload
560          * of the DSDT. No other types of tables should get here, since
561          * only these types can contain AML and thus are the only types
562          * that can create namespace objects.
563          */
564         if (ACPI_COMPARE_NAME (
565                 AcpiGbl_RootTableList.Tables[i].Signature.Ascii,
566                 ACPI_SIG_DSDT))
567         {
568             Status = AE_TYPE;
569             break;
570         }
571 
572         /* Ensure the table is actually loaded */
573 
574         if (!AcpiTbIsTableLoaded (i))
575         {
576             Status = AE_NOT_EXIST;
577             break;
578         }
579 
580         /* Invoke table handler if present */
581 
582         if (AcpiGbl_TableHandler)
583         {
584             (void) AcpiGbl_TableHandler (ACPI_TABLE_EVENT_UNLOAD,
585                 AcpiGbl_RootTableList.Tables[i].Pointer,
586                 AcpiGbl_TableHandlerContext);
587         }
588 
589         /*
590          * Delete all namespace objects owned by this table. Note that
591          * these objects can appear anywhere in the namespace by virtue
592          * of the AML "Scope" operator. Thus, we need to track ownership
593          * by an ID, not simply a position within the hierarchy.
594          */
595         Status = AcpiTbDeleteNamespaceByOwner (i);
596         if (ACPI_FAILURE (Status))
597         {
598             break;
599         }
600 
601         Status = AcpiTbReleaseOwnerId (i);
602         AcpiTbSetTableLoadedFlag (i, FALSE);
603         break;
604     }
605 
606     (void) AcpiUtReleaseMutex (ACPI_MTX_INTERPRETER);
607     return_ACPI_STATUS (Status);
608 }
609 
610 ACPI_EXPORT_SYMBOL (AcpiUnloadParentTable)
611