1 /*******************************************************************************
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
44 #include <acpi/acpi.h>
48 #define _COMPONENT ACPI_NAMESPACE
49 ACPI_MODULE_NAME("nsalloc")
51 /*******************************************************************************
53 * FUNCTION: acpi_ns_create_node
55 * PARAMETERS: name - Name of the new node (4 char ACPI name)
57 * RETURN: New namespace node (Null on failure)
59 * DESCRIPTION: Create a namespace node
61 ******************************************************************************/
62 struct acpi_namespace_node
*acpi_ns_create_node(u32 name
)
64 struct acpi_namespace_node
*node
;
65 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
69 ACPI_FUNCTION_TRACE(ns_create_node
);
71 node
= acpi_os_acquire_object(acpi_gbl_namespace_cache
);
76 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list
->total_allocated
++);
78 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
79 temp
= acpi_gbl_ns_node_list
->total_allocated
-
80 acpi_gbl_ns_node_list
->total_freed
;
81 if (temp
> acpi_gbl_ns_node_list
->max_occupied
) {
82 acpi_gbl_ns_node_list
->max_occupied
= temp
;
86 node
->name
.integer
= name
;
87 ACPI_SET_DESCRIPTOR_TYPE(node
, ACPI_DESC_TYPE_NAMED
);
91 /*******************************************************************************
93 * FUNCTION: acpi_ns_delete_node
95 * PARAMETERS: node - Node to be deleted
99 * DESCRIPTION: Delete a namespace node. All node deletions must come through
100 * here. Detaches any attached objects, including any attached
101 * data. If a handler is associated with attached data, it is
102 * invoked before the node is deleted.
104 ******************************************************************************/
106 void acpi_ns_delete_node(struct acpi_namespace_node
*node
)
108 union acpi_operand_object
*obj_desc
;
109 union acpi_operand_object
*next_desc
;
111 ACPI_FUNCTION_NAME(ns_delete_node
);
113 /* Detach an object if there is one */
115 acpi_ns_detach_object(node
);
118 * Delete an attached data object list if present (objects that were
119 * attached via acpi_attach_data). Note: After any normal object is
120 * detached above, the only possible remaining object(s) are data
121 * objects, in a linked list.
123 obj_desc
= node
->object
;
124 while (obj_desc
&& (obj_desc
->common
.type
== ACPI_TYPE_LOCAL_DATA
)) {
126 /* Invoke the attached data deletion handler if present */
128 if (obj_desc
->data
.handler
) {
129 obj_desc
->data
.handler(node
, obj_desc
->data
.pointer
);
132 next_desc
= obj_desc
->common
.next_object
;
133 acpi_ut_remove_reference(obj_desc
);
134 obj_desc
= next_desc
;
137 /* Special case for the statically allocated root node */
139 if (node
== acpi_gbl_root_node
) {
143 /* Now we can delete the node */
145 (void)acpi_os_release_object(acpi_gbl_namespace_cache
, node
);
147 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list
->total_freed
++);
148 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS
, "Node %p, Remaining %X\n",
149 node
, acpi_gbl_current_node_count
));
152 /*******************************************************************************
154 * FUNCTION: acpi_ns_remove_node
156 * PARAMETERS: node - Node to be removed/deleted
160 * DESCRIPTION: Remove (unlink) and delete a namespace node
162 ******************************************************************************/
164 void acpi_ns_remove_node(struct acpi_namespace_node
*node
)
166 struct acpi_namespace_node
*parent_node
;
167 struct acpi_namespace_node
*prev_node
;
168 struct acpi_namespace_node
*next_node
;
170 ACPI_FUNCTION_TRACE_PTR(ns_remove_node
, node
);
172 parent_node
= node
->parent
;
175 next_node
= parent_node
->child
;
177 /* Find the node that is the previous peer in the parent's child list */
179 while (next_node
!= node
) {
180 prev_node
= next_node
;
181 next_node
= next_node
->peer
;
186 /* Node is not first child, unlink it */
188 prev_node
->peer
= node
->peer
;
191 * Node is first child (has no previous peer).
192 * Link peer list to parent
194 parent_node
->child
= node
->peer
;
197 /* Delete the node and any attached objects */
199 acpi_ns_delete_node(node
);
203 /*******************************************************************************
205 * FUNCTION: acpi_ns_install_node
207 * PARAMETERS: walk_state - Current state of the walk
208 * parent_node - The parent of the new Node
209 * node - The new Node to install
210 * type - ACPI object type of the new Node
214 * DESCRIPTION: Initialize a new namespace node and install it amongst
217 * Note: Current namespace lookup is linear search. This appears
218 * to be sufficient as namespace searches consume only a small
219 * fraction of the execution time of the ACPI subsystem.
221 ******************************************************************************/
223 void acpi_ns_install_node(struct acpi_walk_state
*walk_state
, struct acpi_namespace_node
*parent_node
, /* Parent */
224 struct acpi_namespace_node
*node
, /* New Child */
225 acpi_object_type type
)
227 acpi_owner_id owner_id
= 0;
228 struct acpi_namespace_node
*child_node
;
230 ACPI_FUNCTION_TRACE(ns_install_node
);
234 * Get the owner ID from the Walk state. The owner ID is used to
235 * track table deletion and deletion of objects created by methods.
237 owner_id
= walk_state
->owner_id
;
239 if ((walk_state
->method_desc
) &&
240 (parent_node
!= walk_state
->method_node
)) {
242 * A method is creating a new node that is not a child of the
243 * method (it is non-local). Mark the executing method as having
244 * modified the namespace. This is used for cleanup when the
247 walk_state
->method_desc
->method
.info_flags
|=
248 ACPI_METHOD_MODIFIED_NAMESPACE
;
252 /* Link the new entry into the parent and existing children */
255 node
->parent
= parent_node
;
256 child_node
= parent_node
->child
;
259 parent_node
->child
= node
;
261 /* Add node to the end of the peer list */
263 while (child_node
->peer
) {
264 child_node
= child_node
->peer
;
267 child_node
->peer
= node
;
270 /* Init the new entry */
272 node
->owner_id
= owner_id
;
273 node
->type
= (u8
) type
;
275 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
,
276 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
277 acpi_ut_get_node_name(node
),
278 acpi_ut_get_type_name(node
->type
), node
, owner_id
,
279 acpi_ut_get_node_name(parent_node
),
280 acpi_ut_get_type_name(parent_node
->type
),
286 /*******************************************************************************
288 * FUNCTION: acpi_ns_delete_children
290 * PARAMETERS: parent_node - Delete this objects children
294 * DESCRIPTION: Delete all children of the parent object. In other words,
297 ******************************************************************************/
299 void acpi_ns_delete_children(struct acpi_namespace_node
*parent_node
)
301 struct acpi_namespace_node
*next_node
;
302 struct acpi_namespace_node
*node_to_delete
;
304 ACPI_FUNCTION_TRACE_PTR(ns_delete_children
, parent_node
);
310 /* Deallocate all children at this level */
312 next_node
= parent_node
->child
;
315 /* Grandchildren should have all been deleted already */
317 if (next_node
->child
) {
318 ACPI_ERROR((AE_INFO
, "Found a grandchild! P=%p C=%p",
319 parent_node
, next_node
));
323 * Delete this child node and move on to the next child in the list.
324 * No need to unlink the node since we are deleting the entire branch.
326 node_to_delete
= next_node
;
327 next_node
= next_node
->peer
;
328 acpi_ns_delete_node(node_to_delete
);
331 /* Clear the parent's child pointer */
333 parent_node
->child
= NULL
;
337 /*******************************************************************************
339 * FUNCTION: acpi_ns_delete_namespace_subtree
341 * PARAMETERS: parent_node - Root of the subtree to be deleted
345 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
346 * stored within the subtree.
348 ******************************************************************************/
350 void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node
*parent_node
)
352 struct acpi_namespace_node
*child_node
= NULL
;
356 ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree
);
362 /* Lock namespace for possible update */
364 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
365 if (ACPI_FAILURE(status
)) {
370 * Traverse the tree of objects until we bubble back up
371 * to where we started.
375 /* Get the next node in this scope (NULL if none) */
377 child_node
= acpi_ns_get_next_node(parent_node
, child_node
);
380 /* Found a child node - detach any attached object */
382 acpi_ns_detach_object(child_node
);
384 /* Check if this node has any children */
386 if (child_node
->child
) {
388 * There is at least one child of this node,
392 parent_node
= child_node
;
397 * No more children of this parent node.
398 * Move up to the grandparent.
403 * Now delete all of the children of this parent
404 * all at the same time.
406 acpi_ns_delete_children(parent_node
);
408 /* New "last child" is this parent node */
410 child_node
= parent_node
;
412 /* Move up the tree to the grandparent */
414 parent_node
= parent_node
->parent
;
418 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
422 /*******************************************************************************
424 * FUNCTION: acpi_ns_delete_namespace_by_owner
426 * PARAMETERS: owner_id - All nodes with this owner will be deleted
430 * DESCRIPTION: Delete entries within the namespace that are owned by a
431 * specific ID. Used to delete entire ACPI tables. All
432 * reference counts are updated.
434 * MUTEX: Locks namespace during deletion walk.
436 ******************************************************************************/
438 void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id
)
440 struct acpi_namespace_node
*child_node
;
441 struct acpi_namespace_node
*deletion_node
;
442 struct acpi_namespace_node
*parent_node
;
446 ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner
, owner_id
);
452 /* Lock namespace for possible update */
454 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
455 if (ACPI_FAILURE(status
)) {
459 deletion_node
= NULL
;
460 parent_node
= acpi_gbl_root_node
;
465 * Traverse the tree of nodes until we bubble back up
466 * to where we started.
470 * Get the next child of this parent node. When child_node is NULL,
471 * the first child of the parent is returned
473 child_node
= acpi_ns_get_next_node(parent_node
, child_node
);
476 acpi_ns_delete_children(deletion_node
);
477 acpi_ns_remove_node(deletion_node
);
478 deletion_node
= NULL
;
482 if (child_node
->owner_id
== owner_id
) {
484 /* Found a matching child node - detach any attached object */
486 acpi_ns_detach_object(child_node
);
489 /* Check if this node has any children */
491 if (child_node
->child
) {
493 * There is at least one child of this node,
497 parent_node
= child_node
;
499 } else if (child_node
->owner_id
== owner_id
) {
500 deletion_node
= child_node
;
504 * No more children of this parent node.
505 * Move up to the grandparent.
509 if (parent_node
->owner_id
== owner_id
) {
510 deletion_node
= parent_node
;
514 /* New "last child" is this parent node */
516 child_node
= parent_node
;
518 /* Move up the tree to the grandparent */
520 parent_node
= parent_node
->parent
;
524 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);