Path: blob/main/sys/contrib/dev/acpica/components/utilities/utmisc.c
48406 views
/*******************************************************************************1*2* Module Name: utmisc - common utility procedures3*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/acnamesp.h>154155156#define _COMPONENT ACPI_UTILITIES157ACPI_MODULE_NAME ("utmisc")158159160/*******************************************************************************161*162* FUNCTION: AcpiUtIsPciRootBridge163*164* PARAMETERS: Id - The HID/CID in string format165*166* RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge167*168* DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.169*170******************************************************************************/171172BOOLEAN173AcpiUtIsPciRootBridge (174char *Id)175{176177/*178* Check if this is a PCI root bridge.179* ACPI 3.0+: check for a PCI Express root also.180*/181if (!(strcmp (Id,182PCI_ROOT_HID_STRING)) ||183184!(strcmp (Id,185PCI_EXPRESS_ROOT_HID_STRING)))186{187return (TRUE);188}189190return (FALSE);191}192193194#if (defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP || defined ACPI_NAMES_APP)195/*******************************************************************************196*197* FUNCTION: AcpiUtIsAmlTable198*199* PARAMETERS: Table - An ACPI table200*201* RETURN: TRUE if table contains executable AML; FALSE otherwise202*203* DESCRIPTION: Check ACPI Signature for a table that contains AML code.204* Currently, these are DSDT,SSDT,PSDT. All other table types are205* data tables that do not contain AML code.206*207******************************************************************************/208209BOOLEAN210AcpiUtIsAmlTable (211ACPI_TABLE_HEADER *Table)212{213214/* These are the only tables that contain executable AML */215216if (ACPI_COMPARE_NAMESEG (Table->Signature, ACPI_SIG_DSDT) ||217ACPI_COMPARE_NAMESEG (Table->Signature, ACPI_SIG_PSDT) ||218ACPI_COMPARE_NAMESEG (Table->Signature, ACPI_SIG_SSDT) ||219ACPI_COMPARE_NAMESEG (Table->Signature, ACPI_SIG_OSDT) ||220ACPI_IS_OEM_SIG (Table->Signature))221{222return (TRUE);223}224225return (FALSE);226}227#endif228229230/*******************************************************************************231*232* FUNCTION: AcpiUtDwordByteSwap233*234* PARAMETERS: Value - Value to be converted235*236* RETURN: UINT32 integer with bytes swapped237*238* DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)239*240******************************************************************************/241242UINT32243AcpiUtDwordByteSwap (244UINT32 Value)245{246union247{248UINT32 Value;249UINT8 Bytes[4];250} Out;251union252{253UINT32 Value;254UINT8 Bytes[4];255} In;256257258ACPI_FUNCTION_ENTRY ();259260261In.Value = Value;262263Out.Bytes[0] = In.Bytes[3];264Out.Bytes[1] = In.Bytes[2];265Out.Bytes[2] = In.Bytes[1];266Out.Bytes[3] = In.Bytes[0];267268return (Out.Value);269}270271272/*******************************************************************************273*274* FUNCTION: AcpiUtSetIntegerWidth275*276* PARAMETERS: Revision From DSDT header277*278* RETURN: None279*280* DESCRIPTION: Set the global integer bit width based upon the revision281* of the DSDT. For Revision 1 and 0, Integers are 32 bits.282* For Revision 2 and above, Integers are 64 bits. Yes, this283* makes a difference.284*285******************************************************************************/286287void288AcpiUtSetIntegerWidth (289UINT8 Revision)290{291292if (Revision < 2)293{294/* 32-bit case */295296AcpiGbl_IntegerBitWidth = 32;297AcpiGbl_IntegerNybbleWidth = 8;298AcpiGbl_IntegerByteWidth = 4;299}300else301{302/* 64-bit case (ACPI 2.0+) */303304AcpiGbl_IntegerBitWidth = 64;305AcpiGbl_IntegerNybbleWidth = 16;306AcpiGbl_IntegerByteWidth = 8;307}308}309310311/*******************************************************************************312*313* FUNCTION: AcpiUtCreateUpdateStateAndPush314*315* PARAMETERS: Object - Object to be added to the new state316* Action - Increment/Decrement317* StateList - List the state will be added to318*319* RETURN: Status320*321* DESCRIPTION: Create a new state and push it322*323******************************************************************************/324325ACPI_STATUS326AcpiUtCreateUpdateStateAndPush (327ACPI_OPERAND_OBJECT *Object,328UINT16 Action,329ACPI_GENERIC_STATE **StateList)330{331ACPI_GENERIC_STATE *State;332333334ACPI_FUNCTION_ENTRY ();335336337/* Ignore null objects; these are expected */338339if (!Object)340{341return (AE_OK);342}343344State = AcpiUtCreateUpdateState (Object, Action);345if (!State)346{347return (AE_NO_MEMORY);348}349350AcpiUtPushGenericState (StateList, State);351return (AE_OK);352}353354355/*******************************************************************************356*357* FUNCTION: AcpiUtWalkPackageTree358*359* PARAMETERS: SourceObject - The package to walk360* TargetObject - Target object (if package is being copied)361* WalkCallback - Called once for each package element362* Context - Passed to the callback function363*364* RETURN: Status365*366* DESCRIPTION: Walk through a package, including subpackages367*368******************************************************************************/369370ACPI_STATUS371AcpiUtWalkPackageTree (372ACPI_OPERAND_OBJECT *SourceObject,373void *TargetObject,374ACPI_PKG_CALLBACK WalkCallback,375void *Context)376{377ACPI_STATUS Status = AE_OK;378ACPI_GENERIC_STATE *StateList = NULL;379ACPI_GENERIC_STATE *State;380ACPI_OPERAND_OBJECT *ThisSourceObj;381UINT32 ThisIndex;382383384ACPI_FUNCTION_TRACE (UtWalkPackageTree);385386387State = AcpiUtCreatePkgState (SourceObject, TargetObject, 0);388if (!State)389{390return_ACPI_STATUS (AE_NO_MEMORY);391}392393while (State)394{395/* Get one element of the package */396397ThisIndex = State->Pkg.Index;398ThisSourceObj =399State->Pkg.SourceObject->Package.Elements[ThisIndex];400State->Pkg.ThisTargetObj =401&State->Pkg.SourceObject->Package.Elements[ThisIndex];402403/*404* Check for:405* 1) An uninitialized package element. It is completely406* legal to declare a package and leave it uninitialized407* 2) Not an internal object - can be a namespace node instead408* 3) Any type other than a package. Packages are handled in else409* case below.410*/411if ((!ThisSourceObj) ||412(ACPI_GET_DESCRIPTOR_TYPE (ThisSourceObj) !=413ACPI_DESC_TYPE_OPERAND) ||414(ThisSourceObj->Common.Type != ACPI_TYPE_PACKAGE))415{416Status = WalkCallback (ACPI_COPY_TYPE_SIMPLE, ThisSourceObj,417State, Context);418if (ACPI_FAILURE (Status))419{420return_ACPI_STATUS (Status);421}422423State->Pkg.Index++;424while (State->Pkg.Index >=425State->Pkg.SourceObject->Package.Count)426{427/*428* We've handled all of the objects at this level, This means429* that we have just completed a package. That package may430* have contained one or more packages itself.431*432* Delete this state and pop the previous state (package).433*/434AcpiUtDeleteGenericState (State);435State = AcpiUtPopGenericState (&StateList);436437/* Finished when there are no more states */438439if (!State)440{441/*442* We have handled all of the objects in the top level443* package just add the length of the package objects444* and exit445*/446return_ACPI_STATUS (AE_OK);447}448449/*450* Go back up a level and move the index past the just451* completed package object.452*/453State->Pkg.Index++;454}455}456else457{458/* This is a subobject of type package */459460Status = WalkCallback (461ACPI_COPY_TYPE_PACKAGE, ThisSourceObj, State, Context);462if (ACPI_FAILURE (Status))463{464return_ACPI_STATUS (Status);465}466467/*468* Push the current state and create a new one469* The callback above returned a new target package object.470*/471AcpiUtPushGenericState (&StateList, State);472State = AcpiUtCreatePkgState (473ThisSourceObj, State->Pkg.ThisTargetObj, 0);474if (!State)475{476/* Free any stacked Update State objects */477478while (StateList)479{480State = AcpiUtPopGenericState (&StateList);481AcpiUtDeleteGenericState (State);482}483return_ACPI_STATUS (AE_NO_MEMORY);484}485}486}487488/* We should never get here */489490ACPI_ERROR ((AE_INFO,491"State list did not terminate correctly"));492493return_ACPI_STATUS (AE_AML_INTERNAL);494}495496497#ifdef ACPI_DEBUG_OUTPUT498/*******************************************************************************499*500* FUNCTION: AcpiUtDisplayInitPathname501*502* PARAMETERS: Type - Object type of the node503* ObjHandle - Handle whose pathname will be displayed504* Path - Additional path string to be appended.505* (NULL if no extra path)506*507* RETURN: ACPI_STATUS508*509* DESCRIPTION: Display full pathname of an object, DEBUG ONLY510*511******************************************************************************/512513void514AcpiUtDisplayInitPathname (515UINT8 Type,516ACPI_NAMESPACE_NODE *ObjHandle,517const char *Path)518{519ACPI_STATUS Status;520ACPI_BUFFER Buffer;521522523ACPI_FUNCTION_ENTRY ();524525526/* Only print the path if the appropriate debug level is enabled */527528if (!(AcpiDbgLevel & ACPI_LV_INIT_NAMES))529{530return;531}532533/* Get the full pathname to the node */534535Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;536Status = AcpiNsHandleToPathname (ObjHandle, &Buffer, TRUE);537if (ACPI_FAILURE (Status))538{539return;540}541542/* Print what we're doing */543544switch (Type)545{546case ACPI_TYPE_METHOD:547548AcpiOsPrintf ("Executing ");549break;550551default:552553AcpiOsPrintf ("Initializing ");554break;555}556557/* Print the object type and pathname */558559AcpiOsPrintf ("%-12s %s",560AcpiUtGetTypeName (Type), (char *) Buffer.Pointer);561562/* Extra path is used to append names like _STA, _INI, etc. */563564if (Path)565{566AcpiOsPrintf (".%s", Path);567}568AcpiOsPrintf ("\n");569570ACPI_FREE (Buffer.Pointer);571}572#endif573574575