1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /*******************************************************************************
4 * Module Name: nsalloc - Namespace allocation and deletion utilities
6 ******************************************************************************/
12 #define _COMPONENT ACPI_NAMESPACE
13 ACPI_MODULE_NAME("nsalloc")
15 /*******************************************************************************
17 * FUNCTION: acpi_ns_create_node
19 * PARAMETERS: name - Name of the new node (4 char ACPI name)
21 * RETURN: New namespace node (Null on failure)
23 * DESCRIPTION: Create a namespace node
25 ******************************************************************************/
26 struct acpi_namespace_node
*acpi_ns_create_node(u32 name
)
28 struct acpi_namespace_node
*node
;
29 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
33 ACPI_FUNCTION_TRACE(ns_create_node
);
35 node
= acpi_os_acquire_object(acpi_gbl_namespace_cache
);
40 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list
->total_allocated
++);
42 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
43 temp
= acpi_gbl_ns_node_list
->total_allocated
-
44 acpi_gbl_ns_node_list
->total_freed
;
45 if (temp
> acpi_gbl_ns_node_list
->max_occupied
) {
46 acpi_gbl_ns_node_list
->max_occupied
= temp
;
50 node
->name
.integer
= name
;
51 ACPI_SET_DESCRIPTOR_TYPE(node
, ACPI_DESC_TYPE_NAMED
);
55 /*******************************************************************************
57 * FUNCTION: acpi_ns_delete_node
59 * PARAMETERS: node - Node to be deleted
63 * DESCRIPTION: Delete a namespace node. All node deletions must come through
64 * here. Detaches any attached objects, including any attached
65 * data. If a handler is associated with attached data, it is
66 * invoked before the node is deleted.
68 ******************************************************************************/
70 void acpi_ns_delete_node(struct acpi_namespace_node
*node
)
72 union acpi_operand_object
*obj_desc
;
73 union acpi_operand_object
*next_desc
;
75 ACPI_FUNCTION_NAME(ns_delete_node
);
77 /* Detach an object if there is one */
79 acpi_ns_detach_object(node
);
82 * Delete an attached data object list if present (objects that were
83 * attached via acpi_attach_data). Note: After any normal object is
84 * detached above, the only possible remaining object(s) are data
85 * objects, in a linked list.
87 obj_desc
= node
->object
;
88 while (obj_desc
&& (obj_desc
->common
.type
== ACPI_TYPE_LOCAL_DATA
)) {
90 /* Invoke the attached data deletion handler if present */
92 if (obj_desc
->data
.handler
) {
93 obj_desc
->data
.handler(node
, obj_desc
->data
.pointer
);
96 next_desc
= obj_desc
->common
.next_object
;
97 acpi_ut_remove_reference(obj_desc
);
101 /* Special case for the statically allocated root node */
103 if (node
== acpi_gbl_root_node
) {
107 /* Now we can delete the node */
109 (void)acpi_os_release_object(acpi_gbl_namespace_cache
, node
);
111 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list
->total_freed
++);
112 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "Node %p, Remaining %X\n",
113 node
, acpi_gbl_current_node_count
));
116 /*******************************************************************************
118 * FUNCTION: acpi_ns_remove_node
120 * PARAMETERS: node - Node to be removed/deleted
124 * DESCRIPTION: Remove (unlink) and delete a namespace node
126 ******************************************************************************/
128 void acpi_ns_remove_node(struct acpi_namespace_node
*node
)
130 struct acpi_namespace_node
*parent_node
;
131 struct acpi_namespace_node
*prev_node
;
132 struct acpi_namespace_node
*next_node
;
134 ACPI_FUNCTION_TRACE_PTR(ns_remove_node
, node
);
136 parent_node
= node
->parent
;
139 next_node
= parent_node
->child
;
141 /* Find the node that is the previous peer in the parent's child list */
143 while (next_node
!= node
) {
144 prev_node
= next_node
;
145 next_node
= next_node
->peer
;
150 /* Node is not first child, unlink it */
152 prev_node
->peer
= node
->peer
;
155 * Node is first child (has no previous peer).
156 * Link peer list to parent
158 parent_node
->child
= node
->peer
;
161 /* Delete the node and any attached objects */
163 acpi_ns_delete_node(node
);
167 /*******************************************************************************
169 * FUNCTION: acpi_ns_install_node
171 * PARAMETERS: walk_state - Current state of the walk
172 * parent_node - The parent of the new Node
173 * node - The new Node to install
174 * type - ACPI object type of the new Node
178 * DESCRIPTION: Initialize a new namespace node and install it amongst
181 * Note: Current namespace lookup is linear search. This appears
182 * to be sufficient as namespace searches consume only a small
183 * fraction of the execution time of the ACPI subsystem.
185 ******************************************************************************/
187 void acpi_ns_install_node(struct acpi_walk_state
*walk_state
, struct acpi_namespace_node
*parent_node
, /* Parent */
188 struct acpi_namespace_node
*node
, /* New Child */
189 acpi_object_type type
)
191 acpi_owner_id owner_id
= 0;
192 struct acpi_namespace_node
*child_node
;
194 ACPI_FUNCTION_TRACE(ns_install_node
);
198 * Get the owner ID from the Walk state. The owner ID is used to
199 * track table deletion and deletion of objects created by methods.
201 owner_id
= walk_state
->owner_id
;
203 if ((walk_state
->method_desc
) &&
204 (parent_node
!= walk_state
->method_node
)) {
206 * A method is creating a new node that is not a child of the
207 * method (it is non-local). Mark the executing method as having
208 * modified the namespace. This is used for cleanup when the
211 walk_state
->method_desc
->method
.info_flags
|=
212 ACPI_METHOD_MODIFIED_NAMESPACE
;
216 /* Link the new entry into the parent and existing children */
219 node
->parent
= parent_node
;
220 child_node
= parent_node
->child
;
223 parent_node
->child
= node
;
225 /* Add node to the end of the peer list */
227 while (child_node
->peer
) {
228 child_node
= child_node
->peer
;
231 child_node
->peer
= node
;
234 /* Init the new entry */
236 node
->owner_id
= owner_id
;
237 node
->type
= (u8
) type
;
239 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
,
240 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
241 acpi_ut_get_node_name(node
),
242 acpi_ut_get_type_name(node
->type
), node
, owner_id
,
243 acpi_ut_get_node_name(parent_node
),
244 acpi_ut_get_type_name(parent_node
->type
),
250 /*******************************************************************************
252 * FUNCTION: acpi_ns_delete_children
254 * PARAMETERS: parent_node - Delete this objects children
258 * DESCRIPTION: Delete all children of the parent object. In other words,
261 ******************************************************************************/
263 void acpi_ns_delete_children(struct acpi_namespace_node
*parent_node
)
265 struct acpi_namespace_node
*next_node
;
266 struct acpi_namespace_node
*node_to_delete
;
268 ACPI_FUNCTION_TRACE_PTR(ns_delete_children
, parent_node
);
274 /* Deallocate all children at this level */
276 next_node
= parent_node
->child
;
279 /* Grandchildren should have all been deleted already */
281 if (next_node
->child
) {
282 ACPI_ERROR((AE_INFO
, "Found a grandchild! P=%p C=%p",
283 parent_node
, next_node
));
287 * Delete this child node and move on to the next child in the list.
288 * No need to unlink the node since we are deleting the entire branch.
290 node_to_delete
= next_node
;
291 next_node
= next_node
->peer
;
292 acpi_ns_delete_node(node_to_delete
);
295 /* Clear the parent's child pointer */
297 parent_node
->child
= NULL
;
301 /*******************************************************************************
303 * FUNCTION: acpi_ns_delete_namespace_subtree
305 * PARAMETERS: parent_node - Root of the subtree to be deleted
309 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
310 * stored within the subtree.
312 ******************************************************************************/
314 void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node
*parent_node
)
316 struct acpi_namespace_node
*child_node
= NULL
;
320 ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree
);
326 /* Lock namespace for possible update */
328 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
329 if (ACPI_FAILURE(status
)) {
334 * Traverse the tree of objects until we bubble back up
335 * to where we started.
339 /* Get the next node in this scope (NULL if none) */
341 child_node
= acpi_ns_get_next_node(parent_node
, child_node
);
344 /* Found a child node - detach any attached object */
346 acpi_ns_detach_object(child_node
);
348 /* Check if this node has any children */
350 if (child_node
->child
) {
352 * There is at least one child of this node,
356 parent_node
= child_node
;
361 * No more children of this parent node.
362 * Move up to the grandparent.
367 * Now delete all of the children of this parent
368 * all at the same time.
370 acpi_ns_delete_children(parent_node
);
372 /* New "last child" is this parent node */
374 child_node
= parent_node
;
376 /* Move up the tree to the grandparent */
378 parent_node
= parent_node
->parent
;
382 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
386 /*******************************************************************************
388 * FUNCTION: acpi_ns_delete_namespace_by_owner
390 * PARAMETERS: owner_id - All nodes with this owner will be deleted
394 * DESCRIPTION: Delete entries within the namespace that are owned by a
395 * specific ID. Used to delete entire ACPI tables. All
396 * reference counts are updated.
398 * MUTEX: Locks namespace during deletion walk.
400 ******************************************************************************/
402 void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id
)
404 struct acpi_namespace_node
*child_node
;
405 struct acpi_namespace_node
*deletion_node
;
406 struct acpi_namespace_node
*parent_node
;
410 ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner
, owner_id
);
416 /* Lock namespace for possible update */
418 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
419 if (ACPI_FAILURE(status
)) {
423 deletion_node
= NULL
;
424 parent_node
= acpi_gbl_root_node
;
429 * Traverse the tree of nodes until we bubble back up
430 * to where we started.
434 * Get the next child of this parent node. When child_node is NULL,
435 * the first child of the parent is returned
437 child_node
= acpi_ns_get_next_node(parent_node
, child_node
);
440 acpi_ns_delete_children(deletion_node
);
441 acpi_ns_remove_node(deletion_node
);
442 deletion_node
= NULL
;
446 if (child_node
->owner_id
== owner_id
) {
448 /* Found a matching child node - detach any attached object */
450 acpi_ns_detach_object(child_node
);
453 /* Check if this node has any children */
455 if (child_node
->child
) {
457 * There is at least one child of this node,
461 parent_node
= child_node
;
463 } else if (child_node
->owner_id
== owner_id
) {
464 deletion_node
= child_node
;
468 * No more children of this parent node.
469 * Move up to the grandparent.
473 if (parent_node
->owner_id
== owner_id
) {
474 deletion_node
= parent_node
;
478 /* New "last child" is this parent node */
480 child_node
= parent_node
;
482 /* Move up the tree to the grandparent */
484 parent_node
= parent_node
->parent
;
488 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);