1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsalloc")
56 /*******************************************************************************
58 * FUNCTION: AcpiNsCreateNode
60 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
62 * RETURN: New namespace node (Null on failure)
64 * DESCRIPTION: Create a namespace node
66 ******************************************************************************/
72 ACPI_NAMESPACE_NODE *Node;
73 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
78 ACPI_FUNCTION_TRACE (NsCreateNode);
81 Node = AcpiOsAcquireObject (AcpiGbl_NamespaceCache);
87 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalAllocated++);
89 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
90 Temp = AcpiGbl_NsNodeList->TotalAllocated -
91 AcpiGbl_NsNodeList->TotalFreed;
92 if (Temp > AcpiGbl_NsNodeList->MaxOccupied)
94 AcpiGbl_NsNodeList->MaxOccupied = Temp;
98 Node->Name.Integer = Name;
99 ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);
104 /*******************************************************************************
106 * FUNCTION: AcpiNsDeleteNode
108 * PARAMETERS: Node - Node to be deleted
112 * DESCRIPTION: Delete a namespace node. All node deletions must come through
113 * here. Detaches any attached objects, including any attached
114 * data. If a handler is associated with attached data, it is
115 * invoked before the node is deleted.
117 ******************************************************************************/
121 ACPI_NAMESPACE_NODE *Node)
123 ACPI_OPERAND_OBJECT *ObjDesc;
126 ACPI_FUNCTION_NAME (NsDeleteNode);
129 /* Detach an object if there is one */
131 AcpiNsDetachObject (Node);
134 * Delete an attached data object if present (an object that was created
135 * and attached via AcpiAttachData). Note: After any normal object is
136 * detached above, the only possible remaining object is a data object.
138 ObjDesc = Node->Object;
140 (ObjDesc->Common.Type == ACPI_TYPE_LOCAL_DATA))
142 /* Invoke the attached data deletion handler if present */
144 if (ObjDesc->Data.Handler)
146 ObjDesc->Data.Handler (Node, ObjDesc->Data.Pointer);
149 AcpiUtRemoveReference (ObjDesc);
152 /* Now we can delete the node */
154 (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, Node);
156 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);
157 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Node %p, Remaining %X\n",
158 Node, AcpiGbl_CurrentNodeCount));
162 /*******************************************************************************
164 * FUNCTION: AcpiNsRemoveNode
166 * PARAMETERS: Node - Node to be removed/deleted
170 * DESCRIPTION: Remove (unlink) and delete a namespace node
172 ******************************************************************************/
176 ACPI_NAMESPACE_NODE *Node)
178 ACPI_NAMESPACE_NODE *ParentNode;
179 ACPI_NAMESPACE_NODE *PrevNode;
180 ACPI_NAMESPACE_NODE *NextNode;
183 ACPI_FUNCTION_TRACE_PTR (NsRemoveNode, Node);
186 ParentNode = Node->Parent;
189 NextNode = ParentNode->Child;
191 /* Find the node that is the previous peer in the parent's child list */
193 while (NextNode != Node)
196 NextNode = NextNode->Peer;
201 /* Node is not first child, unlink it */
203 PrevNode->Peer = Node->Peer;
208 * Node is first child (has no previous peer).
209 * Link peer list to parent
211 ParentNode->Child = Node->Peer;
214 /* Delete the node and any attached objects */
216 AcpiNsDeleteNode (Node);
221 /*******************************************************************************
223 * FUNCTION: AcpiNsInstallNode
225 * PARAMETERS: WalkState - Current state of the walk
226 * ParentNode - The parent of the new Node
227 * Node - The new Node to install
228 * Type - ACPI object type of the new Node
232 * DESCRIPTION: Initialize a new namespace node and install it amongst
235 * Note: Current namespace lookup is linear search. This appears
236 * to be sufficient as namespace searches consume only a small
237 * fraction of the execution time of the ACPI subsystem.
239 ******************************************************************************/
243 ACPI_WALK_STATE *WalkState,
244 ACPI_NAMESPACE_NODE *ParentNode, /* Parent */
245 ACPI_NAMESPACE_NODE *Node, /* New Child*/
246 ACPI_OBJECT_TYPE Type)
248 ACPI_OWNER_ID OwnerId = 0;
249 ACPI_NAMESPACE_NODE *ChildNode;
252 ACPI_FUNCTION_TRACE (NsInstallNode);
258 * Get the owner ID from the Walk state. The owner ID is used to
259 * track table deletion and deletion of objects created by methods.
261 OwnerId = WalkState->OwnerId;
263 if ((WalkState->MethodDesc) &&
264 (ParentNode != WalkState->MethodNode))
267 * A method is creating a new node that is not a child of the
268 * method (it is non-local). Mark the executing method as having
269 * modified the namespace. This is used for cleanup when the
272 WalkState->MethodDesc->Method.InfoFlags |= ACPI_METHOD_MODIFIED_NAMESPACE;
276 /* Link the new entry into the parent and existing children */
279 Node->Parent = ParentNode;
280 ChildNode = ParentNode->Child;
284 ParentNode->Child = Node;
288 /* Add node to the end of the peer list */
290 while (ChildNode->Peer)
292 ChildNode = ChildNode->Peer;
295 ChildNode->Peer = Node;
298 /* Init the new entry */
300 Node->OwnerId = OwnerId;
301 Node->Type = (UINT8) Type;
303 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
304 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
305 AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type), Node, OwnerId,
306 AcpiUtGetNodeName (ParentNode), AcpiUtGetTypeName (ParentNode->Type),
313 /*******************************************************************************
315 * FUNCTION: AcpiNsDeleteChildren
317 * PARAMETERS: ParentNode - Delete this objects children
321 * DESCRIPTION: Delete all children of the parent object. In other words,
324 ******************************************************************************/
327 AcpiNsDeleteChildren (
328 ACPI_NAMESPACE_NODE *ParentNode)
330 ACPI_NAMESPACE_NODE *NextNode;
331 ACPI_NAMESPACE_NODE *NodeToDelete;
334 ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren, ParentNode);
342 /* Deallocate all children at this level */
344 NextNode = ParentNode->Child;
347 /* Grandchildren should have all been deleted already */
351 ACPI_ERROR ((AE_INFO, "Found a grandchild! P=%p C=%p",
352 ParentNode, NextNode));
356 * Delete this child node and move on to the next child in the list.
357 * No need to unlink the node since we are deleting the entire branch.
359 NodeToDelete = NextNode;
360 NextNode = NextNode->Peer;
361 AcpiNsDeleteNode (NodeToDelete);
364 /* Clear the parent's child pointer */
366 ParentNode->Child = NULL;
371 /*******************************************************************************
373 * FUNCTION: AcpiNsDeleteNamespaceSubtree
375 * PARAMETERS: ParentNode - Root of the subtree to be deleted
379 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
380 * stored within the subtree.
382 ******************************************************************************/
385 AcpiNsDeleteNamespaceSubtree (
386 ACPI_NAMESPACE_NODE *ParentNode)
388 ACPI_NAMESPACE_NODE *ChildNode = NULL;
393 ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree);
401 /* Lock namespace for possible update */
403 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
404 if (ACPI_FAILURE (Status))
410 * Traverse the tree of objects until we bubble back up
411 * to where we started.
415 /* Get the next node in this scope (NULL if none) */
417 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
420 /* Found a child node - detach any attached object */
422 AcpiNsDetachObject (ChildNode);
424 /* Check if this node has any children */
426 if (ChildNode->Child)
429 * There is at least one child of this node,
433 ParentNode = ChildNode;
440 * No more children of this parent node.
441 * Move up to the grandparent.
446 * Now delete all of the children of this parent
447 * all at the same time.
449 AcpiNsDeleteChildren (ParentNode);
451 /* New "last child" is this parent node */
453 ChildNode = ParentNode;
455 /* Move up the tree to the grandparent */
457 ParentNode = ParentNode->Parent;
461 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
466 /*******************************************************************************
468 * FUNCTION: AcpiNsDeleteNamespaceByOwner
470 * PARAMETERS: OwnerId - All nodes with this owner will be deleted
474 * DESCRIPTION: Delete entries within the namespace that are owned by a
475 * specific ID. Used to delete entire ACPI tables. All
476 * reference counts are updated.
478 * MUTEX: Locks namespace during deletion walk.
480 ******************************************************************************/
483 AcpiNsDeleteNamespaceByOwner (
484 ACPI_OWNER_ID OwnerId)
486 ACPI_NAMESPACE_NODE *ChildNode;
487 ACPI_NAMESPACE_NODE *DeletionNode;
488 ACPI_NAMESPACE_NODE *ParentNode;
493 ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner, OwnerId);
501 /* Lock namespace for possible update */
503 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
504 if (ACPI_FAILURE (Status))
510 ParentNode = AcpiGbl_RootNode;
515 * Traverse the tree of nodes until we bubble back up
516 * to where we started.
521 * Get the next child of this parent node. When ChildNode is NULL,
522 * the first child of the parent is returned
524 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
528 AcpiNsDeleteChildren (DeletionNode);
529 AcpiNsRemoveNode (DeletionNode);
535 if (ChildNode->OwnerId == OwnerId)
537 /* Found a matching child node - detach any attached object */
539 AcpiNsDetachObject (ChildNode);
542 /* Check if this node has any children */
544 if (ChildNode->Child)
547 * There is at least one child of this node,
551 ParentNode = ChildNode;
554 else if (ChildNode->OwnerId == OwnerId)
556 DeletionNode = ChildNode;
562 * No more children of this parent node.
563 * Move up to the grandparent.
568 if (ParentNode->OwnerId == OwnerId)
570 DeletionNode = ParentNode;
574 /* New "last child" is this parent node */
576 ChildNode = ParentNode;
578 /* Move up the tree to the grandparent */
580 ParentNode = ParentNode->Parent;
584 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);