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