]>
Commit | Line | Data |
---|---|---|
95857638 | 1 | // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 |
1da177e4 LT |
2 | /******************************************************************************* |
3 | * | |
4 | * Module Name: nsalloc - Namespace allocation and deletion utilities | |
5 | * | |
6 | ******************************************************************************/ | |
7 | ||
1da177e4 | 8 | #include <acpi/acpi.h> |
e2f7a777 LB |
9 | #include "accommon.h" |
10 | #include "acnamesp.h" | |
1da177e4 | 11 | |
1da177e4 | 12 | #define _COMPONENT ACPI_NAMESPACE |
4be44fcd | 13 | ACPI_MODULE_NAME("nsalloc") |
1da177e4 | 14 | |
1da177e4 LT |
15 | /******************************************************************************* |
16 | * | |
17 | * FUNCTION: acpi_ns_create_node | |
18 | * | |
ba494bee | 19 | * PARAMETERS: name - Name of the new node (4 char ACPI name) |
1da177e4 | 20 | * |
44f6c012 | 21 | * RETURN: New namespace node (Null on failure) |
1da177e4 LT |
22 | * |
23 | * DESCRIPTION: Create a namespace node | |
24 | * | |
25 | ******************************************************************************/ | |
4be44fcd | 26 | struct acpi_namespace_node *acpi_ns_create_node(u32 name) |
1da177e4 | 27 | { |
4be44fcd | 28 | struct acpi_namespace_node *node; |
afbb9e65 VP |
29 | #ifdef ACPI_DBG_TRACK_ALLOCATIONS |
30 | u32 temp; | |
31 | #endif | |
1da177e4 | 32 | |
b229cf92 | 33 | ACPI_FUNCTION_TRACE(ns_create_node); |
1da177e4 | 34 | |
61686124 | 35 | node = acpi_os_acquire_object(acpi_gbl_namespace_cache); |
1da177e4 | 36 | if (!node) { |
4be44fcd | 37 | return_PTR(NULL); |
1da177e4 LT |
38 | } |
39 | ||
4be44fcd | 40 | ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++); |
1da177e4 | 41 | |
afbb9e65 | 42 | #ifdef ACPI_DBG_TRACK_ALLOCATIONS |
d4913dc6 | 43 | temp = acpi_gbl_ns_node_list->total_allocated - |
afbb9e65 VP |
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; | |
47 | } | |
48 | #endif | |
49 | ||
4be44fcd | 50 | node->name.integer = name; |
4be44fcd | 51 | ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED); |
4be44fcd | 52 | return_PTR(node); |
1da177e4 LT |
53 | } |
54 | ||
1da177e4 LT |
55 | /******************************************************************************* |
56 | * | |
57 | * FUNCTION: acpi_ns_delete_node | |
58 | * | |
ba494bee | 59 | * PARAMETERS: node - Node to be deleted |
1da177e4 LT |
60 | * |
61 | * RETURN: None | |
62 | * | |
8e4319c4 BM |
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. | |
1da177e4 LT |
67 | * |
68 | ******************************************************************************/ | |
69 | ||
4be44fcd | 70 | void acpi_ns_delete_node(struct acpi_namespace_node *node) |
8e4319c4 BM |
71 | { |
72 | union acpi_operand_object *obj_desc; | |
794ba09b | 73 | union acpi_operand_object *next_desc; |
8e4319c4 BM |
74 | |
75 | ACPI_FUNCTION_NAME(ns_delete_node); | |
76 | ||
df9271d6 EK |
77 | if (!node) { |
78 | return_VOID; | |
79 | } | |
80 | ||
8e4319c4 BM |
81 | /* Detach an object if there is one */ |
82 | ||
83 | acpi_ns_detach_object(node); | |
84 | ||
85 | /* | |
794ba09b TN |
86 | * Delete an attached data object list if present (objects that were |
87 | * attached via acpi_attach_data). Note: After any normal object is | |
88 | * detached above, the only possible remaining object(s) are data | |
89 | * objects, in a linked list. | |
8e4319c4 BM |
90 | */ |
91 | obj_desc = node->object; | |
794ba09b | 92 | while (obj_desc && (obj_desc->common.type == ACPI_TYPE_LOCAL_DATA)) { |
8e4319c4 BM |
93 | |
94 | /* Invoke the attached data deletion handler if present */ | |
95 | ||
96 | if (obj_desc->data.handler) { | |
97 | obj_desc->data.handler(node, obj_desc->data.pointer); | |
98 | } | |
99 | ||
794ba09b | 100 | next_desc = obj_desc->common.next_object; |
8e4319c4 | 101 | acpi_ut_remove_reference(obj_desc); |
794ba09b | 102 | obj_desc = next_desc; |
8e4319c4 BM |
103 | } |
104 | ||
3f69fe15 BM |
105 | /* Special case for the statically allocated root node */ |
106 | ||
107 | if (node == acpi_gbl_root_node) { | |
108 | return; | |
109 | } | |
110 | ||
8e4319c4 BM |
111 | /* Now we can delete the node */ |
112 | ||
113 | (void)acpi_os_release_object(acpi_gbl_namespace_cache, node); | |
114 | ||
115 | ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++); | |
116 | ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "Node %p, Remaining %X\n", | |
117 | node, acpi_gbl_current_node_count)); | |
118 | } | |
119 | ||
120 | /******************************************************************************* | |
121 | * | |
122 | * FUNCTION: acpi_ns_remove_node | |
123 | * | |
ba494bee | 124 | * PARAMETERS: node - Node to be removed/deleted |
8e4319c4 BM |
125 | * |
126 | * RETURN: None | |
127 | * | |
128 | * DESCRIPTION: Remove (unlink) and delete a namespace node | |
129 | * | |
130 | ******************************************************************************/ | |
131 | ||
132 | void acpi_ns_remove_node(struct acpi_namespace_node *node) | |
1da177e4 | 133 | { |
4be44fcd LB |
134 | struct acpi_namespace_node *parent_node; |
135 | struct acpi_namespace_node *prev_node; | |
136 | struct acpi_namespace_node *next_node; | |
1da177e4 | 137 | |
8e4319c4 | 138 | ACPI_FUNCTION_TRACE_PTR(ns_remove_node, node); |
1da177e4 | 139 | |
c45b5c09 | 140 | parent_node = node->parent; |
1da177e4 LT |
141 | |
142 | prev_node = NULL; | |
143 | next_node = parent_node->child; | |
144 | ||
145 | /* Find the node that is the previous peer in the parent's child list */ | |
146 | ||
147 | while (next_node != node) { | |
148 | prev_node = next_node; | |
c45b5c09 | 149 | next_node = next_node->peer; |
1da177e4 LT |
150 | } |
151 | ||
152 | if (prev_node) { | |
52fc0b02 | 153 | |
1da177e4 LT |
154 | /* Node is not first child, unlink it */ |
155 | ||
c45b5c09 | 156 | prev_node->peer = node->peer; |
4be44fcd | 157 | } else { |
c45b5c09 AS |
158 | /* |
159 | * Node is first child (has no previous peer). | |
160 | * Link peer list to parent | |
161 | */ | |
162 | parent_node->child = node->peer; | |
1da177e4 LT |
163 | } |
164 | ||
8e4319c4 | 165 | /* Delete the node and any attached objects */ |
d4913dc6 | 166 | |
8e4319c4 | 167 | acpi_ns_delete_node(node); |
1da177e4 LT |
168 | return_VOID; |
169 | } | |
170 | ||
1da177e4 LT |
171 | /******************************************************************************* |
172 | * | |
173 | * FUNCTION: acpi_ns_install_node | |
174 | * | |
175 | * PARAMETERS: walk_state - Current state of the walk | |
176 | * parent_node - The parent of the new Node | |
ba494bee BM |
177 | * node - The new Node to install |
178 | * type - ACPI object type of the new Node | |
1da177e4 LT |
179 | * |
180 | * RETURN: None | |
181 | * | |
182 | * DESCRIPTION: Initialize a new namespace node and install it amongst | |
183 | * its peers. | |
184 | * | |
0c9938cc RM |
185 | * Note: Current namespace lookup is linear search. This appears |
186 | * to be sufficient as namespace searches consume only a small | |
187 | * fraction of the execution time of the ACPI subsystem. | |
1da177e4 LT |
188 | * |
189 | ******************************************************************************/ | |
190 | ||
4be44fcd LB |
191 | void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */ |
192 | struct acpi_namespace_node *node, /* New Child */ | |
193 | acpi_object_type type) | |
1da177e4 | 194 | { |
4be44fcd LB |
195 | acpi_owner_id owner_id = 0; |
196 | struct acpi_namespace_node *child_node; | |
1da177e4 | 197 | |
b229cf92 | 198 | ACPI_FUNCTION_TRACE(ns_install_node); |
1da177e4 | 199 | |
1da177e4 | 200 | if (walk_state) { |
a9fc0312 AS |
201 | /* |
202 | * Get the owner ID from the Walk state. The owner ID is used to | |
203 | * track table deletion and deletion of objects created by methods. | |
204 | */ | |
1da177e4 | 205 | owner_id = walk_state->owner_id; |
a9fc0312 AS |
206 | |
207 | if ((walk_state->method_desc) && | |
208 | (parent_node != walk_state->method_node)) { | |
209 | /* | |
210 | * A method is creating a new node that is not a child of the | |
211 | * method (it is non-local). Mark the executing method as having | |
212 | * modified the namespace. This is used for cleanup when the | |
213 | * method exits. | |
214 | */ | |
26294842 LM |
215 | walk_state->method_desc->method.info_flags |= |
216 | ACPI_METHOD_MODIFIED_NAMESPACE; | |
a9fc0312 | 217 | } |
1da177e4 LT |
218 | } |
219 | ||
220 | /* Link the new entry into the parent and existing children */ | |
221 | ||
c45b5c09 AS |
222 | node->peer = NULL; |
223 | node->parent = parent_node; | |
1da177e4 | 224 | child_node = parent_node->child; |
c45b5c09 | 225 | |
1da177e4 LT |
226 | if (!child_node) { |
227 | parent_node->child = node; | |
4be44fcd | 228 | } else { |
c45b5c09 AS |
229 | /* Add node to the end of the peer list */ |
230 | ||
231 | while (child_node->peer) { | |
1da177e4 LT |
232 | child_node = child_node->peer; |
233 | } | |
234 | ||
235 | child_node->peer = node; | |
1da177e4 LT |
236 | } |
237 | ||
238 | /* Init the new entry */ | |
239 | ||
240 | node->owner_id = owner_id; | |
241 | node->type = (u8) type; | |
242 | ||
4be44fcd | 243 | ACPI_DEBUG_PRINT((ACPI_DB_NAMES, |
67a72420 | 244 | "%4.4s (%s) [Node %p Owner %3.3X] added to %4.4s (%s) [Node %p]\n", |
4be44fcd LB |
245 | acpi_ut_get_node_name(node), |
246 | acpi_ut_get_type_name(node->type), node, owner_id, | |
247 | acpi_ut_get_node_name(parent_node), | |
248 | acpi_ut_get_type_name(parent_node->type), | |
249 | parent_node)); | |
793c2388 BM |
250 | |
251 | return_VOID; | |
1da177e4 LT |
252 | } |
253 | ||
1da177e4 LT |
254 | /******************************************************************************* |
255 | * | |
256 | * FUNCTION: acpi_ns_delete_children | |
257 | * | |
258 | * PARAMETERS: parent_node - Delete this objects children | |
259 | * | |
260 | * RETURN: None. | |
261 | * | |
262 | * DESCRIPTION: Delete all children of the parent object. In other words, | |
263 | * deletes a "scope". | |
264 | * | |
265 | ******************************************************************************/ | |
266 | ||
4be44fcd | 267 | void acpi_ns_delete_children(struct acpi_namespace_node *parent_node) |
1da177e4 | 268 | { |
4be44fcd | 269 | struct acpi_namespace_node *next_node; |
c45b5c09 | 270 | struct acpi_namespace_node *node_to_delete; |
1da177e4 | 271 | |
b229cf92 | 272 | ACPI_FUNCTION_TRACE_PTR(ns_delete_children, parent_node); |
1da177e4 LT |
273 | |
274 | if (!parent_node) { | |
275 | return_VOID; | |
276 | } | |
277 | ||
d4913dc6 BM |
278 | /* Deallocate all children at this level */ |
279 | ||
c45b5c09 AS |
280 | next_node = parent_node->child; |
281 | while (next_node) { | |
1da177e4 LT |
282 | |
283 | /* Grandchildren should have all been deleted already */ | |
284 | ||
c45b5c09 | 285 | if (next_node->child) { |
b8e4d893 | 286 | ACPI_ERROR((AE_INFO, "Found a grandchild! P=%p C=%p", |
c45b5c09 | 287 | parent_node, next_node)); |
1da177e4 LT |
288 | } |
289 | ||
8e4319c4 BM |
290 | /* |
291 | * Delete this child node and move on to the next child in the list. | |
292 | * No need to unlink the node since we are deleting the entire branch. | |
293 | */ | |
c45b5c09 AS |
294 | node_to_delete = next_node; |
295 | next_node = next_node->peer; | |
296 | acpi_ns_delete_node(node_to_delete); | |
167504a0 | 297 | } |
1da177e4 | 298 | |
1da177e4 LT |
299 | /* Clear the parent's child pointer */ |
300 | ||
301 | parent_node->child = NULL; | |
1da177e4 LT |
302 | return_VOID; |
303 | } | |
304 | ||
1da177e4 LT |
305 | /******************************************************************************* |
306 | * | |
307 | * FUNCTION: acpi_ns_delete_namespace_subtree | |
308 | * | |
309 | * PARAMETERS: parent_node - Root of the subtree to be deleted | |
310 | * | |
311 | * RETURN: None. | |
312 | * | |
73a3090a | 313 | * DESCRIPTION: Delete a subtree of the namespace. This includes all objects |
1da177e4 LT |
314 | * stored within the subtree. |
315 | * | |
316 | ******************************************************************************/ | |
317 | ||
4be44fcd | 318 | void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node) |
1da177e4 | 319 | { |
4be44fcd LB |
320 | struct acpi_namespace_node *child_node = NULL; |
321 | u32 level = 1; | |
5d3131f5 | 322 | acpi_status status; |
1da177e4 | 323 | |
b229cf92 | 324 | ACPI_FUNCTION_TRACE(ns_delete_namespace_subtree); |
1da177e4 LT |
325 | |
326 | if (!parent_node) { | |
327 | return_VOID; | |
328 | } | |
329 | ||
5d3131f5 DM |
330 | /* Lock namespace for possible update */ |
331 | ||
332 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); | |
333 | if (ACPI_FAILURE(status)) { | |
334 | return_VOID; | |
335 | } | |
336 | ||
1da177e4 LT |
337 | /* |
338 | * Traverse the tree of objects until we bubble back up | |
339 | * to where we started. | |
340 | */ | |
341 | while (level > 0) { | |
52fc0b02 | 342 | |
1da177e4 LT |
343 | /* Get the next node in this scope (NULL if none) */ |
344 | ||
8c725bf9 | 345 | child_node = acpi_ns_get_next_node(parent_node, child_node); |
1da177e4 | 346 | if (child_node) { |
52fc0b02 | 347 | |
1da177e4 LT |
348 | /* Found a child node - detach any attached object */ |
349 | ||
4be44fcd | 350 | acpi_ns_detach_object(child_node); |
1da177e4 LT |
351 | |
352 | /* Check if this node has any children */ | |
353 | ||
8c725bf9 | 354 | if (child_node->child) { |
1da177e4 LT |
355 | /* |
356 | * There is at least one child of this node, | |
357 | * visit the node | |
358 | */ | |
359 | level++; | |
360 | parent_node = child_node; | |
361 | child_node = NULL; | |
362 | } | |
4be44fcd | 363 | } else { |
1da177e4 LT |
364 | /* |
365 | * No more children of this parent node. | |
366 | * Move up to the grandparent. | |
367 | */ | |
368 | level--; | |
369 | ||
370 | /* | |
371 | * Now delete all of the children of this parent | |
372 | * all at the same time. | |
373 | */ | |
4be44fcd | 374 | acpi_ns_delete_children(parent_node); |
1da177e4 LT |
375 | |
376 | /* New "last child" is this parent node */ | |
377 | ||
378 | child_node = parent_node; | |
379 | ||
380 | /* Move up the tree to the grandparent */ | |
381 | ||
c45b5c09 | 382 | parent_node = parent_node->parent; |
1da177e4 LT |
383 | } |
384 | } | |
385 | ||
5d3131f5 | 386 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); |
1da177e4 LT |
387 | return_VOID; |
388 | } | |
389 | ||
1da177e4 LT |
390 | /******************************************************************************* |
391 | * | |
392 | * FUNCTION: acpi_ns_delete_namespace_by_owner | |
393 | * | |
394 | * PARAMETERS: owner_id - All nodes with this owner will be deleted | |
395 | * | |
396 | * RETURN: Status | |
397 | * | |
398 | * DESCRIPTION: Delete entries within the namespace that are owned by a | |
73a3090a | 399 | * specific ID. Used to delete entire ACPI tables. All |
1da177e4 LT |
400 | * reference counts are updated. |
401 | * | |
f6dd9221 BM |
402 | * MUTEX: Locks namespace during deletion walk. |
403 | * | |
1da177e4 LT |
404 | ******************************************************************************/ |
405 | ||
4be44fcd | 406 | void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id) |
1da177e4 | 407 | { |
4be44fcd LB |
408 | struct acpi_namespace_node *child_node; |
409 | struct acpi_namespace_node *deletion_node; | |
4be44fcd | 410 | struct acpi_namespace_node *parent_node; |
f6dd9221 BM |
411 | u32 level; |
412 | acpi_status status; | |
1da177e4 | 413 | |
b229cf92 | 414 | ACPI_FUNCTION_TRACE_U32(ns_delete_namespace_by_owner, owner_id); |
1da177e4 | 415 | |
0c9938cc RM |
416 | if (owner_id == 0) { |
417 | return_VOID; | |
418 | } | |
419 | ||
f6dd9221 BM |
420 | /* Lock namespace for possible update */ |
421 | ||
422 | status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE); | |
423 | if (ACPI_FAILURE(status)) { | |
424 | return_VOID; | |
425 | } | |
426 | ||
61686124 | 427 | deletion_node = NULL; |
4be44fcd LB |
428 | parent_node = acpi_gbl_root_node; |
429 | child_node = NULL; | |
4be44fcd | 430 | level = 1; |
1da177e4 LT |
431 | |
432 | /* | |
433 | * Traverse the tree of nodes until we bubble back up | |
434 | * to where we started. | |
435 | */ | |
436 | while (level > 0) { | |
437 | /* | |
438 | * Get the next child of this parent node. When child_node is NULL, | |
439 | * the first child of the parent is returned | |
440 | */ | |
8c725bf9 | 441 | child_node = acpi_ns_get_next_node(parent_node, child_node); |
1da177e4 LT |
442 | |
443 | if (deletion_node) { | |
61686124 | 444 | acpi_ns_delete_children(deletion_node); |
8e4319c4 | 445 | acpi_ns_remove_node(deletion_node); |
1da177e4 LT |
446 | deletion_node = NULL; |
447 | } | |
448 | ||
449 | if (child_node) { | |
450 | if (child_node->owner_id == owner_id) { | |
52fc0b02 | 451 | |
1da177e4 LT |
452 | /* Found a matching child node - detach any attached object */ |
453 | ||
4be44fcd | 454 | acpi_ns_detach_object(child_node); |
1da177e4 LT |
455 | } |
456 | ||
457 | /* Check if this node has any children */ | |
458 | ||
8c725bf9 | 459 | if (child_node->child) { |
1da177e4 LT |
460 | /* |
461 | * There is at least one child of this node, | |
462 | * visit the node | |
463 | */ | |
464 | level++; | |
465 | parent_node = child_node; | |
466 | child_node = NULL; | |
4be44fcd | 467 | } else if (child_node->owner_id == owner_id) { |
1da177e4 LT |
468 | deletion_node = child_node; |
469 | } | |
4be44fcd | 470 | } else { |
1da177e4 LT |
471 | /* |
472 | * No more children of this parent node. | |
473 | * Move up to the grandparent. | |
474 | */ | |
475 | level--; | |
476 | if (level != 0) { | |
477 | if (parent_node->owner_id == owner_id) { | |
478 | deletion_node = parent_node; | |
479 | } | |
480 | } | |
481 | ||
482 | /* New "last child" is this parent node */ | |
483 | ||
484 | child_node = parent_node; | |
485 | ||
486 | /* Move up the tree to the grandparent */ | |
487 | ||
c45b5c09 | 488 | parent_node = parent_node->parent; |
1da177e4 LT |
489 | } |
490 | } | |
491 | ||
f6dd9221 | 492 | (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE); |
1da177e4 LT |
493 | return_VOID; |
494 | } |