8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / uts / intel / io / acpica / namespace / nsalloc.c
blob72a974f5b4c79ba5b9cafc1be4e4567ed01e8459
1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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.
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acnamesp.h"
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME ("nsalloc")
53 /*******************************************************************************
55 * FUNCTION: AcpiNsCreateNode
57 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
59 * RETURN: New namespace node (Null on failure)
61 * DESCRIPTION: Create a namespace node
63 ******************************************************************************/
65 ACPI_NAMESPACE_NODE *
66 AcpiNsCreateNode (
67 UINT32 Name)
69 ACPI_NAMESPACE_NODE *Node;
70 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
71 UINT32 Temp;
72 #endif
75 ACPI_FUNCTION_TRACE (NsCreateNode);
78 Node = AcpiOsAcquireObject (AcpiGbl_NamespaceCache);
79 if (!Node)
81 return_PTR (NULL);
84 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalAllocated++);
86 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
87 Temp = AcpiGbl_NsNodeList->TotalAllocated -
88 AcpiGbl_NsNodeList->TotalFreed;
89 if (Temp > AcpiGbl_NsNodeList->MaxOccupied)
91 AcpiGbl_NsNodeList->MaxOccupied = Temp;
93 #endif
95 Node->Name.Integer = Name;
96 ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);
97 return_PTR (Node);
101 /*******************************************************************************
103 * FUNCTION: AcpiNsDeleteNode
105 * PARAMETERS: Node - Node to be deleted
107 * RETURN: None
109 * DESCRIPTION: Delete a namespace node. All node deletions must come through
110 * here. Detaches any attached objects, including any attached
111 * data. If a handler is associated with attached data, it is
112 * invoked before the node is deleted.
114 ******************************************************************************/
116 void
117 AcpiNsDeleteNode (
118 ACPI_NAMESPACE_NODE *Node)
120 ACPI_OPERAND_OBJECT *ObjDesc;
121 ACPI_OPERAND_OBJECT *NextDesc;
124 ACPI_FUNCTION_NAME (NsDeleteNode);
127 /* Detach an object if there is one */
129 AcpiNsDetachObject (Node);
132 * Delete an attached data object list if present (objects that were
133 * attached via AcpiAttachData). Note: After any normal object is
134 * detached above, the only possible remaining object(s) are data
135 * objects, in a linked list.
137 ObjDesc = Node->Object;
138 while (ObjDesc &&
139 (ObjDesc->Common.Type == ACPI_TYPE_LOCAL_DATA))
141 /* Invoke the attached data deletion handler if present */
143 if (ObjDesc->Data.Handler)
145 ObjDesc->Data.Handler (Node, ObjDesc->Data.Pointer);
148 NextDesc = ObjDesc->Common.NextObject;
149 AcpiUtRemoveReference (ObjDesc);
150 ObjDesc = NextDesc;
153 /* Special case for the statically allocated root node */
155 if (Node == AcpiGbl_RootNode)
157 return;
160 /* Now we can delete the node */
162 (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, Node);
164 ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);
165 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Node %p, Remaining %X\n",
166 Node, AcpiGbl_CurrentNodeCount));
170 /*******************************************************************************
172 * FUNCTION: AcpiNsRemoveNode
174 * PARAMETERS: Node - Node to be removed/deleted
176 * RETURN: None
178 * DESCRIPTION: Remove (unlink) and delete a namespace node
180 ******************************************************************************/
182 void
183 AcpiNsRemoveNode (
184 ACPI_NAMESPACE_NODE *Node)
186 ACPI_NAMESPACE_NODE *ParentNode;
187 ACPI_NAMESPACE_NODE *PrevNode;
188 ACPI_NAMESPACE_NODE *NextNode;
191 ACPI_FUNCTION_TRACE_PTR (NsRemoveNode, Node);
194 ParentNode = Node->Parent;
196 PrevNode = NULL;
197 NextNode = ParentNode->Child;
199 /* Find the node that is the previous peer in the parent's child list */
201 while (NextNode != Node)
203 PrevNode = NextNode;
204 NextNode = NextNode->Peer;
207 if (PrevNode)
209 /* Node is not first child, unlink it */
211 PrevNode->Peer = Node->Peer;
213 else
216 * Node is first child (has no previous peer).
217 * Link peer list to parent
219 ParentNode->Child = Node->Peer;
222 /* Delete the node and any attached objects */
224 AcpiNsDeleteNode (Node);
225 return_VOID;
229 /*******************************************************************************
231 * FUNCTION: AcpiNsInstallNode
233 * PARAMETERS: WalkState - Current state of the walk
234 * ParentNode - The parent of the new Node
235 * Node - The new Node to install
236 * Type - ACPI object type of the new Node
238 * RETURN: None
240 * DESCRIPTION: Initialize a new namespace node and install it amongst
241 * its peers.
243 * Note: Current namespace lookup is linear search. This appears
244 * to be sufficient as namespace searches consume only a small
245 * fraction of the execution time of the ACPI subsystem.
247 ******************************************************************************/
249 void
250 AcpiNsInstallNode (
251 ACPI_WALK_STATE *WalkState,
252 ACPI_NAMESPACE_NODE *ParentNode, /* Parent */
253 ACPI_NAMESPACE_NODE *Node, /* New Child*/
254 ACPI_OBJECT_TYPE Type)
256 ACPI_OWNER_ID OwnerId = 0;
257 ACPI_NAMESPACE_NODE *ChildNode;
260 ACPI_FUNCTION_TRACE (NsInstallNode);
263 if (WalkState)
266 * Get the owner ID from the Walk state. The owner ID is used to
267 * track table deletion and deletion of objects created by methods.
269 OwnerId = WalkState->OwnerId;
271 if ((WalkState->MethodDesc) &&
272 (ParentNode != WalkState->MethodNode))
275 * A method is creating a new node that is not a child of the
276 * method (it is non-local). Mark the executing method as having
277 * modified the namespace. This is used for cleanup when the
278 * method exits.
280 WalkState->MethodDesc->Method.InfoFlags |=
281 ACPI_METHOD_MODIFIED_NAMESPACE;
285 /* Link the new entry into the parent and existing children */
287 Node->Peer = NULL;
288 Node->Parent = ParentNode;
289 ChildNode = ParentNode->Child;
291 if (!ChildNode)
293 ParentNode->Child = Node;
295 else
297 /* Add node to the end of the peer list */
299 while (ChildNode->Peer)
301 ChildNode = ChildNode->Peer;
304 ChildNode->Peer = Node;
307 /* Init the new entry */
309 Node->OwnerId = OwnerId;
310 Node->Type = (UINT8) Type;
312 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
313 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
314 AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type), Node, OwnerId,
315 AcpiUtGetNodeName (ParentNode), AcpiUtGetTypeName (ParentNode->Type),
316 ParentNode));
318 return_VOID;
322 /*******************************************************************************
324 * FUNCTION: AcpiNsDeleteChildren
326 * PARAMETERS: ParentNode - Delete this objects children
328 * RETURN: None.
330 * DESCRIPTION: Delete all children of the parent object. In other words,
331 * deletes a "scope".
333 ******************************************************************************/
335 void
336 AcpiNsDeleteChildren (
337 ACPI_NAMESPACE_NODE *ParentNode)
339 ACPI_NAMESPACE_NODE *NextNode;
340 ACPI_NAMESPACE_NODE *NodeToDelete;
343 ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren, ParentNode);
346 if (!ParentNode)
348 return_VOID;
351 /* Deallocate all children at this level */
353 NextNode = ParentNode->Child;
354 while (NextNode)
356 /* Grandchildren should have all been deleted already */
358 if (NextNode->Child)
360 ACPI_ERROR ((AE_INFO, "Found a grandchild! P=%p C=%p",
361 ParentNode, NextNode));
365 * Delete this child node and move on to the next child in the list.
366 * No need to unlink the node since we are deleting the entire branch.
368 NodeToDelete = NextNode;
369 NextNode = NextNode->Peer;
370 AcpiNsDeleteNode (NodeToDelete);
373 /* Clear the parent's child pointer */
375 ParentNode->Child = NULL;
376 return_VOID;
380 /*******************************************************************************
382 * FUNCTION: AcpiNsDeleteNamespaceSubtree
384 * PARAMETERS: ParentNode - Root of the subtree to be deleted
386 * RETURN: None.
388 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
389 * stored within the subtree.
391 ******************************************************************************/
393 void
394 AcpiNsDeleteNamespaceSubtree (
395 ACPI_NAMESPACE_NODE *ParentNode)
397 ACPI_NAMESPACE_NODE *ChildNode = NULL;
398 UINT32 Level = 1;
399 ACPI_STATUS Status;
402 ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree);
405 if (!ParentNode)
407 return_VOID;
410 /* Lock namespace for possible update */
412 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
413 if (ACPI_FAILURE (Status))
415 return_VOID;
419 * Traverse the tree of objects until we bubble back up
420 * to where we started.
422 while (Level > 0)
424 /* Get the next node in this scope (NULL if none) */
426 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
427 if (ChildNode)
429 /* Found a child node - detach any attached object */
431 AcpiNsDetachObject (ChildNode);
433 /* Check if this node has any children */
435 if (ChildNode->Child)
438 * There is at least one child of this node,
439 * visit the node
441 Level++;
442 ParentNode = ChildNode;
443 ChildNode = NULL;
446 else
449 * No more children of this parent node.
450 * Move up to the grandparent.
452 Level--;
455 * Now delete all of the children of this parent
456 * all at the same time.
458 AcpiNsDeleteChildren (ParentNode);
460 /* New "last child" is this parent node */
462 ChildNode = ParentNode;
464 /* Move up the tree to the grandparent */
466 ParentNode = ParentNode->Parent;
470 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
471 return_VOID;
475 /*******************************************************************************
477 * FUNCTION: AcpiNsDeleteNamespaceByOwner
479 * PARAMETERS: OwnerId - All nodes with this owner will be deleted
481 * RETURN: Status
483 * DESCRIPTION: Delete entries within the namespace that are owned by a
484 * specific ID. Used to delete entire ACPI tables. All
485 * reference counts are updated.
487 * MUTEX: Locks namespace during deletion walk.
489 ******************************************************************************/
491 void
492 AcpiNsDeleteNamespaceByOwner (
493 ACPI_OWNER_ID OwnerId)
495 ACPI_NAMESPACE_NODE *ChildNode;
496 ACPI_NAMESPACE_NODE *DeletionNode;
497 ACPI_NAMESPACE_NODE *ParentNode;
498 UINT32 Level;
499 ACPI_STATUS Status;
502 ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner, OwnerId);
505 if (OwnerId == 0)
507 return_VOID;
510 /* Lock namespace for possible update */
512 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
513 if (ACPI_FAILURE (Status))
515 return_VOID;
518 DeletionNode = NULL;
519 ParentNode = AcpiGbl_RootNode;
520 ChildNode = NULL;
521 Level = 1;
524 * Traverse the tree of nodes until we bubble back up
525 * to where we started.
527 while (Level > 0)
530 * Get the next child of this parent node. When ChildNode is NULL,
531 * the first child of the parent is returned
533 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
535 if (DeletionNode)
537 AcpiNsDeleteChildren (DeletionNode);
538 AcpiNsRemoveNode (DeletionNode);
539 DeletionNode = NULL;
542 if (ChildNode)
544 if (ChildNode->OwnerId == OwnerId)
546 /* Found a matching child node - detach any attached object */
548 AcpiNsDetachObject (ChildNode);
551 /* Check if this node has any children */
553 if (ChildNode->Child)
556 * There is at least one child of this node,
557 * visit the node
559 Level++;
560 ParentNode = ChildNode;
561 ChildNode = NULL;
563 else if (ChildNode->OwnerId == OwnerId)
565 DeletionNode = ChildNode;
568 else
571 * No more children of this parent node.
572 * Move up to the grandparent.
574 Level--;
575 if (Level != 0)
577 if (ParentNode->OwnerId == OwnerId)
579 DeletionNode = ParentNode;
583 /* New "last child" is this parent node */
585 ChildNode = ParentNode;
587 /* Move up the tree to the grandparent */
589 ParentNode = ParentNode->Parent;
593 (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
594 return_VOID;