Path: blob/main/sys/contrib/dev/acpica/components/tables/tbutils.c
48406 views
/******************************************************************************1*2* Module Name: tbutils - ACPI Table utilities3*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#include <contrib/dev/acpica/include/acpi.h>152#include <contrib/dev/acpica/include/accommon.h>153#include <contrib/dev/acpica/include/actables.h>154155#define _COMPONENT ACPI_TABLES156ACPI_MODULE_NAME ("tbutils")157158159/* Local prototypes */160161static ACPI_PHYSICAL_ADDRESS162AcpiTbGetRootTableEntry (163UINT8 *TableEntry,164UINT32 TableEntrySize);165166167/*******************************************************************************168*169* FUNCTION: AcpiTbInitializeFacs170*171* PARAMETERS: None172*173* RETURN: Status174*175* DESCRIPTION: Create a permanent mapping for the FADT and save it in a global176* for accessing the Global Lock and Firmware Waking Vector177*178******************************************************************************/179180ACPI_STATUS181AcpiTbInitializeFacs (182void)183{184ACPI_TABLE_FACS *Facs;185186if (AcpiGbl_FADT.XFacs &&187(!AcpiGbl_FADT.Facs || !AcpiGbl_Use32BitFacsAddresses))188{189(void) AcpiGetTableByIndex (AcpiGbl_XFacsIndex,190ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &Facs));191AcpiGbl_FACS = Facs;192}193else if (AcpiGbl_FADT.Facs)194{195(void) AcpiGetTableByIndex (AcpiGbl_FacsIndex,196ACPI_CAST_INDIRECT_PTR (ACPI_TABLE_HEADER, &Facs));197AcpiGbl_FACS = Facs;198}199200/* If there is no FACS, just continue. There was already an error msg */201202return (AE_OK);203}204205206/*******************************************************************************207*208* FUNCTION: AcpiTbCheckDsdtHeader209*210* PARAMETERS: None211*212* RETURN: None213*214* DESCRIPTION: Quick compare to check validity of the DSDT. This will detect215* if the DSDT has been replaced from outside the OS and/or if216* the DSDT header has been corrupted.217*218******************************************************************************/219220void221AcpiTbCheckDsdtHeader (222void)223{224225/* Compare original length and checksum to current values */226227if (AcpiGbl_OriginalDsdtHeader.Length != AcpiGbl_DSDT->Length ||228AcpiGbl_OriginalDsdtHeader.Checksum != AcpiGbl_DSDT->Checksum)229{230ACPI_BIOS_ERROR ((AE_INFO,231"The DSDT has been corrupted or replaced - "232"old, new headers below"));233234AcpiTbPrintTableHeader (0, &AcpiGbl_OriginalDsdtHeader);235AcpiTbPrintTableHeader (0, AcpiGbl_DSDT);236237/* Disable further error messages */238239AcpiGbl_OriginalDsdtHeader.Length = AcpiGbl_DSDT->Length;240AcpiGbl_OriginalDsdtHeader.Checksum = AcpiGbl_DSDT->Checksum;241}242}243244245/*******************************************************************************246*247* FUNCTION: AcpiTbCopyDsdt248*249* PARAMETERS: TableIndex - Index of installed table to copy250*251* RETURN: The copied DSDT252*253* DESCRIPTION: Implements a subsystem option to copy the DSDT to local memory.254* Some very bad BIOSs are known to either corrupt the DSDT or255* install a new, bad DSDT. This copy works around the problem.256*257******************************************************************************/258259ACPI_TABLE_HEADER *260AcpiTbCopyDsdt (261UINT32 TableIndex)262{263ACPI_TABLE_HEADER *NewTable;264ACPI_TABLE_DESC *TableDesc;265266267TableDesc = &AcpiGbl_RootTableList.Tables[TableIndex];268269NewTable = ACPI_ALLOCATE (TableDesc->Length);270if (!NewTable)271{272ACPI_ERROR ((AE_INFO, "Could not copy DSDT of length 0x%X",273TableDesc->Length));274return (NULL);275}276277memcpy (NewTable, TableDesc->Pointer, TableDesc->Length);278AcpiTbUninstallTable (TableDesc);279280AcpiTbInitTableDescriptor (281&AcpiGbl_RootTableList.Tables[AcpiGbl_DsdtIndex],282ACPI_PTR_TO_PHYSADDR (NewTable),283ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, NewTable);284285ACPI_INFO ((286"Forced DSDT copy: length 0x%05X copied locally, original unmapped",287NewTable->Length));288289return (NewTable);290}291292293/*******************************************************************************294*295* FUNCTION: AcpiTbGetRootTableEntry296*297* PARAMETERS: TableEntry - Pointer to the RSDT/XSDT table entry298* TableEntrySize - sizeof 32 or 64 (RSDT or XSDT)299*300* RETURN: Physical address extracted from the root table301*302* DESCRIPTION: Get one root table entry. Handles 32-bit and 64-bit cases on303* both 32-bit and 64-bit platforms304*305* NOTE: ACPI_PHYSICAL_ADDRESS is 32-bit on 32-bit platforms, 64-bit on306* 64-bit platforms.307*308******************************************************************************/309310static ACPI_PHYSICAL_ADDRESS311AcpiTbGetRootTableEntry (312UINT8 *TableEntry,313UINT32 TableEntrySize)314{315UINT32 Address32;316UINT64 Address64;317318319/*320* Get the table physical address (32-bit for RSDT, 64-bit for XSDT):321* Note: Addresses are 32-bit aligned (not 64) in both RSDT and XSDT322*/323if (TableEntrySize == ACPI_RSDT_ENTRY_SIZE)324{325/*326* 32-bit platform, RSDT: Return 32-bit table entry327* 64-bit platform, RSDT: Expand 32-bit to 64-bit and return328*/329ACPI_MOVE_32_TO_32(&Address32, TableEntry);330return Address32;331}332else333{334/*335* 32-bit platform, XSDT: Truncate 64-bit to 32-bit and return336* 64-bit platform, XSDT: Move (unaligned) 64-bit to local,337* return 64-bit338*/339ACPI_MOVE_64_TO_64 (&Address64, TableEntry);340341#if ACPI_MACHINE_WIDTH == 32342if (Address64 > ACPI_UINT32_MAX)343{344/* Will truncate 64-bit address to 32 bits, issue warning */345346ACPI_BIOS_WARNING ((AE_INFO,347"64-bit Physical Address in XSDT is too large (0x%8.8X%8.8X),"348" truncating",349ACPI_FORMAT_UINT64 (Address64)));350}351#endif352return ((ACPI_PHYSICAL_ADDRESS) (Address64));353}354}355356357/*******************************************************************************358*359* FUNCTION: AcpiTbParseRootTable360*361* PARAMETERS: RsdpAddress - Pointer to the RSDP362*363* RETURN: Status364*365* DESCRIPTION: This function is called to parse the Root System Description366* Table (RSDT or XSDT)367*368* NOTE: Tables are mapped (not copied) for efficiency. The FACS must369* be mapped and cannot be copied because it contains the actual370* memory location of the ACPI Global Lock.371*372******************************************************************************/373374ACPI_STATUS ACPI_INIT_FUNCTION375AcpiTbParseRootTable (376ACPI_PHYSICAL_ADDRESS RsdpAddress)377{378ACPI_TABLE_RSDP *Rsdp;379UINT32 TableEntrySize;380UINT32 i;381UINT32 TableCount;382ACPI_TABLE_HEADER *Table;383ACPI_PHYSICAL_ADDRESS Address;384UINT32 Length;385UINT8 *TableEntry;386ACPI_STATUS Status;387UINT32 TableIndex;388389390ACPI_FUNCTION_TRACE (TbParseRootTable);391392393/* Map the entire RSDP and extract the address of the RSDT or XSDT */394395Rsdp = AcpiOsMapMemory (RsdpAddress, sizeof (ACPI_TABLE_RSDP));396if (!Rsdp)397{398return_ACPI_STATUS (AE_NO_MEMORY);399}400401AcpiTbPrintTableHeader (RsdpAddress,402ACPI_CAST_PTR (ACPI_TABLE_HEADER, Rsdp));403404/* Use XSDT if present and not overridden. Otherwise, use RSDT */405406if ((Rsdp->Revision > 1) &&407Rsdp->XsdtPhysicalAddress &&408!AcpiGbl_DoNotUseXsdt)409{410/*411* RSDP contains an XSDT (64-bit physical addresses). We must use412* the XSDT if the revision is > 1 and the XSDT pointer is present,413* as per the ACPI specification.414*/415Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->XsdtPhysicalAddress;416TableEntrySize = ACPI_XSDT_ENTRY_SIZE;417}418else419{420/* Root table is an RSDT (32-bit physical addresses) */421422Address = (ACPI_PHYSICAL_ADDRESS) Rsdp->RsdtPhysicalAddress;423TableEntrySize = ACPI_RSDT_ENTRY_SIZE;424}425426/*427* It is not possible to map more than one entry in some environments,428* so unmap the RSDP here before mapping other tables429*/430AcpiOsUnmapMemory (Rsdp, sizeof (ACPI_TABLE_RSDP));431432/* Map the RSDT/XSDT table header to get the full table length */433434Table = AcpiOsMapMemory (Address, sizeof (ACPI_TABLE_HEADER));435if (!Table)436{437return_ACPI_STATUS (AE_NO_MEMORY);438}439440AcpiTbPrintTableHeader (Address, Table);441442/*443* Validate length of the table, and map entire table.444* Minimum length table must contain at least one entry.445*/446Length = Table->Length;447AcpiOsUnmapMemory (Table, sizeof (ACPI_TABLE_HEADER));448449if (Length < (sizeof (ACPI_TABLE_HEADER) + TableEntrySize))450{451ACPI_BIOS_ERROR ((AE_INFO,452"Invalid table length 0x%X in RSDT/XSDT", Length));453return_ACPI_STATUS (AE_INVALID_TABLE_LENGTH);454}455456Table = AcpiOsMapMemory (Address, Length);457if (!Table)458{459return_ACPI_STATUS (AE_NO_MEMORY);460}461462/* Validate the root table checksum */463464Status = AcpiUtVerifyChecksum (Table, Length);465if (ACPI_FAILURE (Status))466{467AcpiOsUnmapMemory (Table, Length);468return_ACPI_STATUS (Status);469}470471/* Get the number of entries and pointer to first entry */472473TableCount = (UINT32) ((Table->Length - sizeof (ACPI_TABLE_HEADER)) /474TableEntrySize);475TableEntry = ACPI_ADD_PTR (UINT8, Table, sizeof (ACPI_TABLE_HEADER));476477/* Initialize the root table array from the RSDT/XSDT */478479for (i = 0; i < TableCount; i++)480{481/* Get the table physical address (32-bit for RSDT, 64-bit for XSDT) */482483Address = AcpiTbGetRootTableEntry (TableEntry, TableEntrySize);484485/* Skip NULL entries in RSDT/XSDT */486487if (!Address)488{489goto NextTable;490}491492Status = AcpiTbInstallStandardTable (Address,493ACPI_TABLE_ORIGIN_INTERNAL_PHYSICAL, NULL, FALSE, TRUE,494&TableIndex);495496if (ACPI_SUCCESS (Status) &&497ACPI_COMPARE_NAMESEG (498&AcpiGbl_RootTableList.Tables[TableIndex].Signature,499ACPI_SIG_FADT))500{501AcpiGbl_FadtIndex = TableIndex;502AcpiTbParseFadt ();503}504505NextTable:506507TableEntry += TableEntrySize;508}509510AcpiOsUnmapMemory (Table, Length);511return_ACPI_STATUS (AE_OK);512}513514515/*******************************************************************************516*517* FUNCTION: AcpiTbGetTable518*519* PARAMETERS: TableDesc - Table descriptor520* OutTable - Where the pointer to the table is returned521*522* RETURN: Status and pointer to the requested table523*524* DESCRIPTION: Increase a reference to a table descriptor and return the525* validated table pointer.526* If the table descriptor is an entry of the root table list,527* this API must be invoked with ACPI_MTX_TABLES acquired.528*529******************************************************************************/530531ACPI_STATUS532AcpiTbGetTable (533ACPI_TABLE_DESC *TableDesc,534ACPI_TABLE_HEADER **OutTable)535{536ACPI_STATUS Status;537538539ACPI_FUNCTION_TRACE (AcpiTbGetTable);540541542if (TableDesc->ValidationCount == 0)543{544/* Table need to be "VALIDATED" */545546Status = AcpiTbValidateTable (TableDesc);547if (ACPI_FAILURE (Status))548{549return_ACPI_STATUS (Status);550}551}552553if (TableDesc->ValidationCount < ACPI_MAX_TABLE_VALIDATIONS)554{555TableDesc->ValidationCount++;556557/*558* Detect ValidationCount overflows to ensure that the warning559* message will only be printed once.560*/561if (TableDesc->ValidationCount >= ACPI_MAX_TABLE_VALIDATIONS)562{563ACPI_WARNING((AE_INFO,564"Table %p, Validation count overflows\n", TableDesc));565}566}567568*OutTable = TableDesc->Pointer;569return_ACPI_STATUS (AE_OK);570}571572573/*******************************************************************************574*575* FUNCTION: AcpiTbPutTable576*577* PARAMETERS: TableDesc - Table descriptor578*579* RETURN: None580*581* DESCRIPTION: Decrease a reference to a table descriptor and release the582* validated table pointer if no references.583* If the table descriptor is an entry of the root table list,584* this API must be invoked with ACPI_MTX_TABLES acquired.585*586******************************************************************************/587588void589AcpiTbPutTable (590ACPI_TABLE_DESC *TableDesc)591{592593ACPI_FUNCTION_TRACE (AcpiTbPutTable);594595596if (TableDesc->ValidationCount < ACPI_MAX_TABLE_VALIDATIONS)597{598TableDesc->ValidationCount--;599600/*601* Detect ValidationCount underflows to ensure that the warning602* message will only be printed once.603*/604if (TableDesc->ValidationCount >= ACPI_MAX_TABLE_VALIDATIONS)605{606ACPI_WARNING ((AE_INFO,607"Table %p, Validation count underflows\n", TableDesc));608return_VOID;609}610}611612if (TableDesc->ValidationCount == 0)613{614/* Table need to be "INVALIDATED" */615616AcpiTbInvalidateTable (TableDesc);617}618619return_VOID;620}621622623