Path: blob/main/sys/contrib/dev/acpica/components/tables/tbxface.c
48406 views
/******************************************************************************1*2* Module Name: tbxface - ACPI table-oriented external interfaces3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151#define EXPORT_ACPI_INTERFACES152153#include <contrib/dev/acpica/include/acpi.h>154#include <contrib/dev/acpica/include/accommon.h>155#include <contrib/dev/acpica/include/actables.h>156157#define _COMPONENT ACPI_TABLES158ACPI_MODULE_NAME ("tbxface")159160161/*******************************************************************************162*163* FUNCTION: AcpiAllocateRootTable164*165* PARAMETERS: InitialTableCount - Size of InitialTableArray, in number of166* ACPI_TABLE_DESC structures167*168* RETURN: Status169*170* DESCRIPTION: Allocate a root table array. Used by iASL compiler and171* AcpiInitializeTables.172*173******************************************************************************/174175ACPI_STATUS176AcpiAllocateRootTable (177UINT32 InitialTableCount)178{179180AcpiGbl_RootTableList.MaxTableCount = InitialTableCount;181AcpiGbl_RootTableList.Flags = ACPI_ROOT_ALLOW_RESIZE;182183return (AcpiTbResizeRootTableList ());184}185186187/*******************************************************************************188*189* FUNCTION: AcpiInitializeTables190*191* PARAMETERS: InitialTableArray - Pointer to an array of pre-allocated192* ACPI_TABLE_DESC structures. If NULL, the193* array is dynamically allocated.194* InitialTableCount - Size of InitialTableArray, in number of195* ACPI_TABLE_DESC structures196* AllowResize - Flag to tell Table Manager if resize of197* pre-allocated array is allowed. Ignored198* if InitialTableArray is NULL.199*200* RETURN: Status201*202* DESCRIPTION: Initialize the table manager, get the RSDP and RSDT/XSDT.203*204* NOTE: Allows static allocation of the initial table array in order205* to avoid the use of dynamic memory in confined environments206* such as the kernel boot sequence where it may not be available.207*208* If the host OS memory managers are initialized, use NULL for209* InitialTableArray, and the table will be dynamically allocated.210*211******************************************************************************/212213ACPI_STATUS ACPI_INIT_FUNCTION214AcpiInitializeTables (215ACPI_TABLE_DESC *InitialTableArray,216UINT32 InitialTableCount,217BOOLEAN AllowResize)218{219ACPI_PHYSICAL_ADDRESS RsdpAddress;220ACPI_STATUS Status;221222223ACPI_FUNCTION_TRACE (AcpiInitializeTables);224225226/*227* Setup the Root Table Array and allocate the table array228* if requested229*/230if (!InitialTableArray)231{232Status = AcpiAllocateRootTable (InitialTableCount);233if (ACPI_FAILURE (Status))234{235return_ACPI_STATUS (Status);236}237}238else239{240/* Root Table Array has been statically allocated by the host */241242memset (InitialTableArray, 0,243(ACPI_SIZE) InitialTableCount * sizeof (ACPI_TABLE_DESC));244245AcpiGbl_RootTableList.Tables = InitialTableArray;246AcpiGbl_RootTableList.MaxTableCount = InitialTableCount;247AcpiGbl_RootTableList.Flags = ACPI_ROOT_ORIGIN_UNKNOWN;248if (AllowResize)249{250AcpiGbl_RootTableList.Flags |= ACPI_ROOT_ALLOW_RESIZE;251}252}253254/* Get the address of the RSDP */255256RsdpAddress = AcpiOsGetRootPointer ();257if (!RsdpAddress)258{259return_ACPI_STATUS (AE_NOT_FOUND);260}261262/*263* Get the root table (RSDT or XSDT) and extract all entries to the local264* Root Table Array. This array contains the information of the RSDT/XSDT265* in a common, more usable format.266*/267Status = AcpiTbParseRootTable (RsdpAddress);268return_ACPI_STATUS (Status);269}270271ACPI_EXPORT_SYMBOL_INIT (AcpiInitializeTables)272273274/*******************************************************************************275*276* FUNCTION: AcpiReallocateRootTable277*278* PARAMETERS: None279*280* RETURN: Status281*282* DESCRIPTION: Reallocate Root Table List into dynamic memory. Copies the283* root list from the previously provided scratch area. Should284* be called once dynamic memory allocation is available in the285* kernel.286*287******************************************************************************/288289ACPI_STATUS ACPI_INIT_FUNCTION290AcpiReallocateRootTable (291void)292{293ACPI_STATUS Status;294ACPI_TABLE_DESC *TableDesc;295UINT32 i, j;296297298ACPI_FUNCTION_TRACE (AcpiReallocateRootTable);299300301/*302* If there are tables unverified, it is required to reallocate the303* root table list to clean up invalid table entries. Otherwise only304* reallocate the root table list if the host provided a static buffer305* for the table array in the call to AcpiInitializeTables().306*/307if ((AcpiGbl_RootTableList.Flags & ACPI_ROOT_ORIGIN_ALLOCATED) &&308AcpiGbl_EnableTableValidation)309{310return_ACPI_STATUS (AE_SUPPORT);311}312313(void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);314315/*316* Ensure OS early boot logic, which is required by some hosts. If the317* table state is reported to be wrong, developers should fix the318* issue by invoking AcpiPutTable() for the reported table during the319* early stage.320*/321for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; ++i)322{323TableDesc = &AcpiGbl_RootTableList.Tables[i];324if (TableDesc->Pointer)325{326ACPI_ERROR ((AE_INFO,327"Table [%4.4s] is not invalidated during early boot stage",328TableDesc->Signature.Ascii));329}330}331332if (!AcpiGbl_EnableTableValidation)333{334/*335* Now it's safe to do full table validation. We can do deferred336* table initialization here once the flag is set.337*/338AcpiGbl_EnableTableValidation = TRUE;339for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; ++i)340{341TableDesc = &AcpiGbl_RootTableList.Tables[i];342if (!(TableDesc->Flags & ACPI_TABLE_IS_VERIFIED))343{344Status = AcpiTbVerifyTempTable (TableDesc, NULL, &j);345if (ACPI_FAILURE (Status))346{347AcpiTbUninstallTable (TableDesc);348}349}350}351}352353AcpiGbl_RootTableList.Flags |= ACPI_ROOT_ALLOW_RESIZE;354Status = AcpiTbResizeRootTableList ();355AcpiGbl_RootTableList.Flags |= ACPI_ROOT_ORIGIN_ALLOCATED;356357(void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);358return_ACPI_STATUS (Status);359}360361ACPI_EXPORT_SYMBOL_INIT (AcpiReallocateRootTable)362363364/*******************************************************************************365*366* FUNCTION: AcpiGetTableHeader367*368* PARAMETERS: Signature - ACPI signature of needed table369* Instance - Which instance (for SSDTs)370* OutTableHeader - The pointer to the where the table header371* is returned372*373* RETURN: Status and a copy of the table header374*375* DESCRIPTION: Finds and returns an ACPI table header. Caller provides the376* memory where a copy of the header is to be returned377* (fixed length).378*379******************************************************************************/380381ACPI_STATUS382AcpiGetTableHeader (383char *Signature,384UINT32 Instance,385ACPI_TABLE_HEADER *OutTableHeader)386{387UINT32 i;388UINT32 j;389ACPI_TABLE_HEADER *Header;390391392/* Parameter validation */393394if (!Signature || !OutTableHeader)395{396return (AE_BAD_PARAMETER);397}398399/* Walk the root table list */400401for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)402{403if (!ACPI_COMPARE_NAMESEG (404&(AcpiGbl_RootTableList.Tables[i].Signature), Signature))405{406continue;407}408409if (++j < Instance)410{411continue;412}413414if (!AcpiGbl_RootTableList.Tables[i].Pointer)415{416if ((AcpiGbl_RootTableList.Tables[i].Flags &417ACPI_TABLE_ORIGIN_MASK) ==418ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL)419{420Header = AcpiOsMapMemory (421AcpiGbl_RootTableList.Tables[i].Address,422sizeof (ACPI_TABLE_HEADER));423if (!Header)424{425return (AE_NO_MEMORY);426}427428memcpy (OutTableHeader, Header, sizeof (ACPI_TABLE_HEADER));429AcpiOsUnmapMemory (Header, sizeof (ACPI_TABLE_HEADER));430}431else432{433return (AE_NOT_FOUND);434}435}436else437{438memcpy (OutTableHeader,439AcpiGbl_RootTableList.Tables[i].Pointer,440sizeof (ACPI_TABLE_HEADER));441}442443return (AE_OK);444}445446return (AE_NOT_FOUND);447}448449ACPI_EXPORT_SYMBOL (AcpiGetTableHeader)450451452/*******************************************************************************453*454* FUNCTION: AcpiGetTable455*456* PARAMETERS: Signature - ACPI signature of needed table457* Instance - Which instance (for SSDTs)458* OutTable - Where the pointer to the table is returned459*460* RETURN: Status and pointer to the requested table461*462* DESCRIPTION: Finds and verifies an ACPI table. Table must be in the463* RSDT/XSDT.464* Note that an early stage AcpiGetTable() call must be paired465* with an early stage AcpiPutTable() call. otherwise the table466* pointer mapped by the early stage mapping implementation may be467* erroneously unmapped by the late stage unmapping implementation468* in an AcpiPutTable() invoked during the late stage.469*470******************************************************************************/471472ACPI_STATUS473AcpiGetTable (474char *Signature,475UINT32 Instance,476ACPI_TABLE_HEADER **OutTable)477{478UINT32 i;479UINT32 j;480ACPI_STATUS Status = AE_NOT_FOUND;481ACPI_TABLE_DESC *TableDesc;482483484/* Parameter validation */485486if (!Signature || !OutTable)487{488return (AE_BAD_PARAMETER);489}490491/*492* Note that the following line is required by some OSPMs, they only493* check if the returned table is NULL instead of the returned status494* to determined if this function is succeeded.495*/496*OutTable = NULL;497498(void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);499500/* Walk the root table list */501502for (i = 0, j = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)503{504TableDesc = &AcpiGbl_RootTableList.Tables[i];505506if (!ACPI_COMPARE_NAMESEG (&TableDesc->Signature, Signature))507{508continue;509}510511if (++j < Instance)512{513continue;514}515516Status = AcpiTbGetTable (TableDesc, OutTable);517break;518}519520(void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);521return (Status);522}523524ACPI_EXPORT_SYMBOL (AcpiGetTable)525526527/*******************************************************************************528*529* FUNCTION: AcpiPutTable530*531* PARAMETERS: Table - The pointer to the table532*533* RETURN: None534*535* DESCRIPTION: Release a table returned by AcpiGetTable() and its clones.536* Note that it is not safe if this function was invoked after an537* uninstallation happened to the original table descriptor.538* Currently there is no OSPMs' requirement to handle such539* situations.540*541******************************************************************************/542543void544AcpiPutTable (545ACPI_TABLE_HEADER *Table)546{547UINT32 i;548ACPI_TABLE_DESC *TableDesc;549550551ACPI_FUNCTION_TRACE (AcpiPutTable);552553554if (!Table)555{556return_VOID;557}558559(void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);560561/* Walk the root table list */562563for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++)564{565TableDesc = &AcpiGbl_RootTableList.Tables[i];566567if (TableDesc->Pointer != Table)568{569continue;570}571572AcpiTbPutTable (TableDesc);573break;574}575576(void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);577return_VOID;578}579580ACPI_EXPORT_SYMBOL (AcpiPutTable)581582583/*******************************************************************************584*585* FUNCTION: AcpiGetTableByIndex586*587* PARAMETERS: TableIndex - Table index588* OutTable - Where the pointer to the table is returned589*590* RETURN: Status and pointer to the requested table591*592* DESCRIPTION: Obtain a table by an index into the global table list. Used593* internally also.594*595******************************************************************************/596597ACPI_STATUS598AcpiGetTableByIndex (599UINT32 TableIndex,600ACPI_TABLE_HEADER **OutTable)601{602ACPI_STATUS Status;603604605ACPI_FUNCTION_TRACE (AcpiGetTableByIndex);606607608/* Parameter validation */609610if (!OutTable)611{612return_ACPI_STATUS (AE_BAD_PARAMETER);613}614615/*616* Note that the following line is required by some OSPMs, they only617* check if the returned table is NULL instead of the returned status618* to determined if this function is succeeded.619*/620*OutTable = NULL;621622(void) AcpiUtAcquireMutex (ACPI_MTX_TABLES);623624/* Validate index */625626if (TableIndex >= AcpiGbl_RootTableList.CurrentTableCount)627{628Status = AE_BAD_PARAMETER;629goto UnlockAndExit;630}631632Status = AcpiTbGetTable (633&AcpiGbl_RootTableList.Tables[TableIndex], OutTable);634635UnlockAndExit:636(void) AcpiUtReleaseMutex (ACPI_MTX_TABLES);637return_ACPI_STATUS (Status);638}639640ACPI_EXPORT_SYMBOL (AcpiGetTableByIndex)641642643/*******************************************************************************644*645* FUNCTION: AcpiInstallTableHandler646*647* PARAMETERS: Handler - Table event handler648* Context - Value passed to the handler on each event649*650* RETURN: Status651*652* DESCRIPTION: Install a global table event handler.653*654******************************************************************************/655656ACPI_STATUS657AcpiInstallTableHandler (658ACPI_TABLE_HANDLER Handler,659void *Context)660{661ACPI_STATUS Status;662663664ACPI_FUNCTION_TRACE (AcpiInstallTableHandler);665666667if (!Handler)668{669return_ACPI_STATUS (AE_BAD_PARAMETER);670}671672Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);673if (ACPI_FAILURE (Status))674{675return_ACPI_STATUS (Status);676}677678/* Don't allow more than one handler */679680if (AcpiGbl_TableHandler)681{682Status = AE_ALREADY_EXISTS;683goto Cleanup;684}685686/* Install the handler */687688AcpiGbl_TableHandler = Handler;689AcpiGbl_TableHandlerContext = Context;690691Cleanup:692(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);693return_ACPI_STATUS (Status);694}695696ACPI_EXPORT_SYMBOL (AcpiInstallTableHandler)697698699/*******************************************************************************700*701* FUNCTION: AcpiRemoveTableHandler702*703* PARAMETERS: Handler - Table event handler that was installed704* previously.705*706* RETURN: Status707*708* DESCRIPTION: Remove a table event handler709*710******************************************************************************/711712ACPI_STATUS713AcpiRemoveTableHandler (714ACPI_TABLE_HANDLER Handler)715{716ACPI_STATUS Status;717718719ACPI_FUNCTION_TRACE (AcpiRemoveTableHandler);720721722Status = AcpiUtAcquireMutex (ACPI_MTX_EVENTS);723if (ACPI_FAILURE (Status))724{725return_ACPI_STATUS (Status);726}727728/* Make sure that the installed handler is the same */729730if (!Handler ||731Handler != AcpiGbl_TableHandler)732{733Status = AE_BAD_PARAMETER;734goto Cleanup;735}736737/* Remove the handler */738739AcpiGbl_TableHandler = NULL;740741Cleanup:742(void) AcpiUtReleaseMutex (ACPI_MTX_EVENTS);743return_ACPI_STATUS (Status);744}745746ACPI_EXPORT_SYMBOL (AcpiRemoveTableHandler)747748749