Path: blob/main/sys/contrib/dev/acpica/components/namespace/nsalloc.c
48524 views
/*******************************************************************************1*2* Module Name: nsalloc - Namespace allocation and deletion 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/acnamesp.h>154155156#define _COMPONENT ACPI_NAMESPACE157ACPI_MODULE_NAME ("nsalloc")158159160/*******************************************************************************161*162* FUNCTION: AcpiNsCreateNode163*164* PARAMETERS: Name - Name of the new node (4 char ACPI name)165*166* RETURN: New namespace node (Null on failure)167*168* DESCRIPTION: Create a namespace node169*170******************************************************************************/171172ACPI_NAMESPACE_NODE *173AcpiNsCreateNode (174UINT32 Name)175{176ACPI_NAMESPACE_NODE *Node;177#ifdef ACPI_DBG_TRACK_ALLOCATIONS178UINT32 Temp;179#endif180181182ACPI_FUNCTION_TRACE (NsCreateNode);183184185Node = AcpiOsAcquireObject (AcpiGbl_NamespaceCache);186if (!Node)187{188return_PTR (NULL);189}190191ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalAllocated++);192193#ifdef ACPI_DBG_TRACK_ALLOCATIONS194Temp = AcpiGbl_NsNodeList->TotalAllocated -195AcpiGbl_NsNodeList->TotalFreed;196if (Temp > AcpiGbl_NsNodeList->MaxOccupied)197{198AcpiGbl_NsNodeList->MaxOccupied = Temp;199}200#endif201202Node->Name.Integer = Name;203ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);204return_PTR (Node);205}206207208/*******************************************************************************209*210* FUNCTION: AcpiNsDeleteNode211*212* PARAMETERS: Node - Node to be deleted213*214* RETURN: None215*216* DESCRIPTION: Delete a namespace node. All node deletions must come through217* here. Detaches any attached objects, including any attached218* data. If a handler is associated with attached data, it is219* invoked before the node is deleted.220*221******************************************************************************/222223void224AcpiNsDeleteNode (225ACPI_NAMESPACE_NODE *Node)226{227ACPI_OPERAND_OBJECT *ObjDesc;228ACPI_OPERAND_OBJECT *NextDesc;229230231ACPI_FUNCTION_NAME (NsDeleteNode);232233234if (!Node)235{236return_VOID;237}238239/* Detach an object if there is one */240241AcpiNsDetachObject (Node);242243/*244* Delete an attached data object list if present (objects that were245* attached via AcpiAttachData). Note: After any normal object is246* detached above, the only possible remaining object(s) are data247* objects, in a linked list.248*/249ObjDesc = Node->Object;250while (ObjDesc &&251(ObjDesc->Common.Type == ACPI_TYPE_LOCAL_DATA))252{253/* Invoke the attached data deletion handler if present */254255if (ObjDesc->Data.Handler)256{257ObjDesc->Data.Handler (Node, ObjDesc->Data.Pointer);258}259260NextDesc = ObjDesc->Common.NextObject;261AcpiUtRemoveReference (ObjDesc);262ObjDesc = NextDesc;263}264265/* Special case for the statically allocated root node */266267if (Node == AcpiGbl_RootNode)268{269return;270}271272/* Now we can delete the node */273274(void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, Node);275276ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);277ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Node %p, Remaining %X\n",278Node, AcpiGbl_CurrentNodeCount));279}280281282/*******************************************************************************283*284* FUNCTION: AcpiNsRemoveNode285*286* PARAMETERS: Node - Node to be removed/deleted287*288* RETURN: None289*290* DESCRIPTION: Remove (unlink) and delete a namespace node291*292******************************************************************************/293294void295AcpiNsRemoveNode (296ACPI_NAMESPACE_NODE *Node)297{298ACPI_NAMESPACE_NODE *ParentNode;299ACPI_NAMESPACE_NODE *PrevNode;300ACPI_NAMESPACE_NODE *NextNode;301302303ACPI_FUNCTION_TRACE_PTR (NsRemoveNode, Node);304305306ParentNode = Node->Parent;307308PrevNode = NULL;309NextNode = ParentNode->Child;310311/* Find the node that is the previous peer in the parent's child list */312313while (NextNode != Node)314{315PrevNode = NextNode;316NextNode = NextNode->Peer;317}318319if (PrevNode)320{321/* Node is not first child, unlink it */322323PrevNode->Peer = Node->Peer;324}325else326{327/*328* Node is first child (has no previous peer).329* Link peer list to parent330*/331ParentNode->Child = Node->Peer;332}333334/* Delete the node and any attached objects */335336AcpiNsDeleteNode (Node);337return_VOID;338}339340341/*******************************************************************************342*343* FUNCTION: AcpiNsInstallNode344*345* PARAMETERS: WalkState - Current state of the walk346* ParentNode - The parent of the new Node347* Node - The new Node to install348* Type - ACPI object type of the new Node349*350* RETURN: None351*352* DESCRIPTION: Initialize a new namespace node and install it amongst353* its peers.354*355* Note: Current namespace lookup is linear search. This appears356* to be sufficient as namespace searches consume only a small357* fraction of the execution time of the ACPI subsystem.358*359******************************************************************************/360361void362AcpiNsInstallNode (363ACPI_WALK_STATE *WalkState,364ACPI_NAMESPACE_NODE *ParentNode, /* Parent */365ACPI_NAMESPACE_NODE *Node, /* New Child*/366ACPI_OBJECT_TYPE Type)367{368ACPI_OWNER_ID OwnerId = 0;369ACPI_NAMESPACE_NODE *ChildNode;370371372ACPI_FUNCTION_TRACE (NsInstallNode);373374375if (WalkState)376{377/*378* Get the owner ID from the Walk state. The owner ID is used to379* track table deletion and deletion of objects created by methods.380*/381OwnerId = WalkState->OwnerId;382383if ((WalkState->MethodDesc) &&384(ParentNode != WalkState->MethodNode))385{386/*387* A method is creating a new node that is not a child of the388* method (it is non-local). Mark the executing method as having389* modified the namespace. This is used for cleanup when the390* method exits.391*/392WalkState->MethodDesc->Method.InfoFlags |=393ACPI_METHOD_MODIFIED_NAMESPACE;394}395}396397/* Link the new entry into the parent and existing children */398399Node->Peer = NULL;400Node->Parent = ParentNode;401ChildNode = ParentNode->Child;402403if (!ChildNode)404{405ParentNode->Child = Node;406}407else408{409/* Add node to the end of the peer list */410411while (ChildNode->Peer)412{413ChildNode = ChildNode->Peer;414}415416ChildNode->Peer = Node;417}418419/* Init the new entry */420421Node->OwnerId = OwnerId;422Node->Type = (UINT8) Type;423424ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,425"%4.4s (%s) [Node %p Owner %3.3X] added to %4.4s (%s) [Node %p]\n",426AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type), Node, OwnerId,427AcpiUtGetNodeName (ParentNode), AcpiUtGetTypeName (ParentNode->Type),428ParentNode));429430return_VOID;431}432433434/*******************************************************************************435*436* FUNCTION: AcpiNsDeleteChildren437*438* PARAMETERS: ParentNode - Delete this objects children439*440* RETURN: None.441*442* DESCRIPTION: Delete all children of the parent object. In other words,443* deletes a "scope".444*445******************************************************************************/446447void448AcpiNsDeleteChildren (449ACPI_NAMESPACE_NODE *ParentNode)450{451ACPI_NAMESPACE_NODE *NextNode;452ACPI_NAMESPACE_NODE *NodeToDelete;453454455ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren, ParentNode);456457458if (!ParentNode)459{460return_VOID;461}462463/* Deallocate all children at this level */464465NextNode = ParentNode->Child;466while (NextNode)467{468/* Grandchildren should have all been deleted already */469470if (NextNode->Child)471{472ACPI_ERROR ((AE_INFO, "Found a grandchild! P=%p C=%p",473ParentNode, NextNode));474}475476/*477* Delete this child node and move on to the next child in the list.478* No need to unlink the node since we are deleting the entire branch.479*/480NodeToDelete = NextNode;481NextNode = NextNode->Peer;482AcpiNsDeleteNode (NodeToDelete);483}484485/* Clear the parent's child pointer */486487ParentNode->Child = NULL;488return_VOID;489}490491492/*******************************************************************************493*494* FUNCTION: AcpiNsDeleteNamespaceSubtree495*496* PARAMETERS: ParentNode - Root of the subtree to be deleted497*498* RETURN: None.499*500* DESCRIPTION: Delete a subtree of the namespace. This includes all objects501* stored within the subtree.502*503******************************************************************************/504505void506AcpiNsDeleteNamespaceSubtree (507ACPI_NAMESPACE_NODE *ParentNode)508{509ACPI_NAMESPACE_NODE *ChildNode = NULL;510UINT32 Level = 1;511ACPI_STATUS Status;512513514ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree);515516517if (!ParentNode)518{519return_VOID;520}521522/* Lock namespace for possible update */523524Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);525if (ACPI_FAILURE (Status))526{527return_VOID;528}529530/*531* Traverse the tree of objects until we bubble back up532* to where we started.533*/534while (Level > 0)535{536/* Get the next node in this scope (NULL if none) */537538ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);539if (ChildNode)540{541/* Found a child node - detach any attached object */542543AcpiNsDetachObject (ChildNode);544545/* Check if this node has any children */546547if (ChildNode->Child)548{549/*550* There is at least one child of this node,551* visit the node552*/553Level++;554ParentNode = ChildNode;555ChildNode = NULL;556}557}558else559{560/*561* No more children of this parent node.562* Move up to the grandparent.563*/564Level--;565566/*567* Now delete all of the children of this parent568* all at the same time.569*/570AcpiNsDeleteChildren (ParentNode);571572/* New "last child" is this parent node */573574ChildNode = ParentNode;575576/* Move up the tree to the grandparent */577578ParentNode = ParentNode->Parent;579}580}581582(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);583return_VOID;584}585586587/*******************************************************************************588*589* FUNCTION: AcpiNsDeleteNamespaceByOwner590*591* PARAMETERS: OwnerId - All nodes with this owner will be deleted592*593* RETURN: Status594*595* DESCRIPTION: Delete entries within the namespace that are owned by a596* specific ID. Used to delete entire ACPI tables. All597* reference counts are updated.598*599* MUTEX: Locks namespace during deletion walk.600*601******************************************************************************/602603void604AcpiNsDeleteNamespaceByOwner (605ACPI_OWNER_ID OwnerId)606{607ACPI_NAMESPACE_NODE *ChildNode;608ACPI_NAMESPACE_NODE *DeletionNode;609ACPI_NAMESPACE_NODE *ParentNode;610UINT32 Level;611ACPI_STATUS Status;612613614ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner, OwnerId);615616617if (OwnerId == 0)618{619return_VOID;620}621622/* Lock namespace for possible update */623624Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);625if (ACPI_FAILURE (Status))626{627return_VOID;628}629630DeletionNode = NULL;631ParentNode = AcpiGbl_RootNode;632ChildNode = NULL;633Level = 1;634635/*636* Traverse the tree of nodes until we bubble back up637* to where we started.638*/639while (Level > 0)640{641/*642* Get the next child of this parent node. When ChildNode is NULL,643* the first child of the parent is returned644*/645ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);646647if (DeletionNode)648{649AcpiNsDeleteChildren (DeletionNode);650AcpiNsRemoveNode (DeletionNode);651DeletionNode = NULL;652}653654if (ChildNode)655{656if (ChildNode->OwnerId == OwnerId)657{658/* Found a matching child node - detach any attached object */659660AcpiNsDetachObject (ChildNode);661}662663/* Check if this node has any children */664665if (ChildNode->Child)666{667/*668* There is at least one child of this node,669* visit the node670*/671Level++;672ParentNode = ChildNode;673ChildNode = NULL;674}675else if (ChildNode->OwnerId == OwnerId)676{677DeletionNode = ChildNode;678}679}680else681{682/*683* No more children of this parent node.684* Move up to the grandparent.685*/686Level--;687if (Level != 0)688{689if (ParentNode->OwnerId == OwnerId)690{691DeletionNode = ParentNode;692}693}694695/* New "last child" is this parent node */696697ChildNode = ParentNode;698699/* Move up the tree to the grandparent */700701ParentNode = ParentNode->Parent;702}703}704705(void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);706return_VOID;707}708709710