blob: 8b921c96d6a5398e88a6986dff69c2ef66d1fb7d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: nsalloc - Namespace allocation and deletion utilities
4 *
5 ******************************************************************************/
6
7/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05008 * Copyright (C) 2000 - 2006, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
45#include <acpi/acnamesp.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040048ACPI_MODULE_NAME("nsalloc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/*******************************************************************************
51 *
52 * FUNCTION: acpi_ns_create_node
53 *
Robert Moore44f6c012005-04-18 22:49:35 -040054 * PARAMETERS: Name - Name of the new node (4 char ACPI name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 *
Robert Moore44f6c012005-04-18 22:49:35 -040056 * RETURN: New namespace node (Null on failure)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * DESCRIPTION: Create a namespace node
59 *
60 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -040061struct acpi_namespace_node *acpi_ns_create_node(u32 name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Len Brown4be44fc2005-08-05 00:44:28 -040063 struct acpi_namespace_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Len Brown4be44fc2005-08-05 00:44:28 -040065 ACPI_FUNCTION_TRACE("ns_create_node");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Bob Moore61686122006-03-17 16:44:00 -050067 node = acpi_os_acquire_object(acpi_gbl_namespace_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!node) {
Len Brown4be44fc2005-08-05 00:44:28 -040069 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71
Len Brown4be44fc2005-08-05 00:44:28 -040072 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_allocated++);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Len Brown4be44fc2005-08-05 00:44:28 -040074 node->name.integer = name;
Len Brown4be44fc2005-08-05 00:44:28 -040075 ACPI_SET_DESCRIPTOR_TYPE(node, ACPI_DESC_TYPE_NAMED);
Len Brown4be44fc2005-08-05 00:44:28 -040076 return_PTR(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*******************************************************************************
80 *
81 * FUNCTION: acpi_ns_delete_node
82 *
83 * PARAMETERS: Node - Node to be deleted
84 *
85 * RETURN: None
86 *
87 * DESCRIPTION: Delete a namespace node
88 *
89 ******************************************************************************/
90
Len Brown4be44fc2005-08-05 00:44:28 -040091void acpi_ns_delete_node(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Len Brown4be44fc2005-08-05 00:44:28 -040093 struct acpi_namespace_node *parent_node;
94 struct acpi_namespace_node *prev_node;
95 struct acpi_namespace_node *next_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Len Brown4be44fc2005-08-05 00:44:28 -040097 ACPI_FUNCTION_TRACE_PTR("ns_delete_node", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Len Brown4be44fc2005-08-05 00:44:28 -040099 parent_node = acpi_ns_get_parent_node(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 prev_node = NULL;
102 next_node = parent_node->child;
103
104 /* Find the node that is the previous peer in the parent's child list */
105
106 while (next_node != node) {
107 prev_node = next_node;
108 next_node = prev_node->peer;
109 }
110
111 if (prev_node) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* Node is not first child, unlink it */
114
115 prev_node->peer = next_node->peer;
116 if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
117 prev_node->flags |= ANOBJ_END_OF_PEER_LIST;
118 }
Len Brown4be44fc2005-08-05 00:44:28 -0400119 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* Node is first child (has no previous peer) */
121
122 if (next_node->flags & ANOBJ_END_OF_PEER_LIST) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* No peers at all */
125
126 parent_node->child = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400127 } else { /* Link peer list to parent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 parent_node->child = next_node->peer;
130 }
131 }
132
Len Brown4be44fc2005-08-05 00:44:28 -0400133 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 /*
Bob Moore61686122006-03-17 16:44:00 -0500136 * Detach an object if there is one, then delete the node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
Len Brown4be44fc2005-08-05 00:44:28 -0400138 acpi_ns_detach_object(node);
Bob Moore61686122006-03-17 16:44:00 -0500139 (void)acpi_os_release_object(acpi_gbl_namespace_cache, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return_VOID;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*******************************************************************************
144 *
145 * FUNCTION: acpi_ns_install_node
146 *
147 * PARAMETERS: walk_state - Current state of the walk
148 * parent_node - The parent of the new Node
149 * Node - The new Node to install
150 * Type - ACPI object type of the new Node
151 *
152 * RETURN: None
153 *
154 * DESCRIPTION: Initialize a new namespace node and install it amongst
155 * its peers.
156 *
Robert Moore0c9938c2005-07-29 15:15:00 -0700157 * Note: Current namespace lookup is linear search. This appears
158 * to be sufficient as namespace searches consume only a small
159 * fraction of the execution time of the ACPI subsystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
161 ******************************************************************************/
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163void acpi_ns_install_node(struct acpi_walk_state *walk_state, struct acpi_namespace_node *parent_node, /* Parent */
164 struct acpi_namespace_node *node, /* New Child */
165 acpi_object_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Len Brown4be44fc2005-08-05 00:44:28 -0400167 acpi_owner_id owner_id = 0;
168 struct acpi_namespace_node *child_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Len Brown4be44fc2005-08-05 00:44:28 -0400170 ACPI_FUNCTION_TRACE("ns_install_node");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /*
173 * Get the owner ID from the Walk state
174 * The owner ID is used to track table deletion and
175 * deletion of objects created by methods
176 */
177 if (walk_state) {
178 owner_id = walk_state->owner_id;
179 }
180
181 /* Link the new entry into the parent and existing children */
182
183 child_node = parent_node->child;
184 if (!child_node) {
185 parent_node->child = node;
186 node->flags |= ANOBJ_END_OF_PEER_LIST;
187 node->peer = parent_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400188 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 while (!(child_node->flags & ANOBJ_END_OF_PEER_LIST)) {
190 child_node = child_node->peer;
191 }
192
193 child_node->peer = node;
194
195 /* Clear end-of-list flag */
196
197 child_node->flags &= ~ANOBJ_END_OF_PEER_LIST;
Robert Moore0c9938c2005-07-29 15:15:00 -0700198 node->flags |= ANOBJ_END_OF_PEER_LIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 node->peer = parent_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 /* Init the new entry */
203
204 node->owner_id = owner_id;
205 node->type = (u8) type;
206
Len Brown4be44fc2005-08-05 00:44:28 -0400207 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
208 "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
209 acpi_ut_get_node_name(node),
210 acpi_ut_get_type_name(node->type), node, owner_id,
211 acpi_ut_get_node_name(parent_node),
212 acpi_ut_get_type_name(parent_node->type),
213 parent_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*******************************************************************************
217 *
218 * FUNCTION: acpi_ns_delete_children
219 *
220 * PARAMETERS: parent_node - Delete this objects children
221 *
222 * RETURN: None.
223 *
224 * DESCRIPTION: Delete all children of the parent object. In other words,
225 * deletes a "scope".
226 *
227 ******************************************************************************/
228
Len Brown4be44fc2005-08-05 00:44:28 -0400229void acpi_ns_delete_children(struct acpi_namespace_node *parent_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Len Brown4be44fc2005-08-05 00:44:28 -0400231 struct acpi_namespace_node *child_node;
232 struct acpi_namespace_node *next_node;
Len Brown4be44fc2005-08-05 00:44:28 -0400233 u8 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Len Brown4be44fc2005-08-05 00:44:28 -0400235 ACPI_FUNCTION_TRACE_PTR("ns_delete_children", parent_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (!parent_node) {
238 return_VOID;
239 }
240
241 /* If no children, all done! */
242
243 child_node = parent_node->child;
244 if (!child_node) {
245 return_VOID;
246 }
247
248 /*
249 * Deallocate all children at this level
250 */
251 do {
Bob Moore52fc0b02006-10-02 00:00:00 -0400252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /* Get the things we need */
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255 next_node = child_node->peer;
256 flags = child_node->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* Grandchildren should have all been deleted already */
259
260 if (child_node->child) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500261 ACPI_ERROR((AE_INFO, "Found a grandchild! P=%p C=%p",
262 parent_node, child_node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 /* Now we can free this child object */
266
Len Brown4be44fc2005-08-05 00:44:28 -0400267 ACPI_MEM_TRACKING(acpi_gbl_ns_node_list->total_freed++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Len Brown4be44fc2005-08-05 00:44:28 -0400269 ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
270 "Object %p, Remaining %X\n", child_node,
271 acpi_gbl_current_node_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /*
274 * Detach an object if there is one, then free the child node
275 */
Len Brown4be44fc2005-08-05 00:44:28 -0400276 acpi_ns_detach_object(child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* Now we can delete the node */
279
Bob Moore61686122006-03-17 16:44:00 -0500280 (void)acpi_os_release_object(acpi_gbl_namespace_cache,
281 child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* And move on to the next child in the list */
284
285 child_node = next_node;
286
287 } while (!(flags & ANOBJ_END_OF_PEER_LIST));
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Clear the parent's child pointer */
290
291 parent_node->child = NULL;
292
293 return_VOID;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*******************************************************************************
297 *
298 * FUNCTION: acpi_ns_delete_namespace_subtree
299 *
300 * PARAMETERS: parent_node - Root of the subtree to be deleted
301 *
302 * RETURN: None.
303 *
304 * DESCRIPTION: Delete a subtree of the namespace. This includes all objects
305 * stored within the subtree.
306 *
307 ******************************************************************************/
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309void acpi_ns_delete_namespace_subtree(struct acpi_namespace_node *parent_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Len Brown4be44fc2005-08-05 00:44:28 -0400311 struct acpi_namespace_node *child_node = NULL;
312 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Len Brown4be44fc2005-08-05 00:44:28 -0400314 ACPI_FUNCTION_TRACE("ns_delete_namespace_subtree");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (!parent_node) {
317 return_VOID;
318 }
319
320 /*
321 * Traverse the tree of objects until we bubble back up
322 * to where we started.
323 */
324 while (level > 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Get the next node in this scope (NULL if none) */
327
Bob Moore61686122006-03-17 16:44:00 -0500328 child_node =
329 acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
330 child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (child_node) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Found a child node - detach any attached object */
334
Len Brown4be44fc2005-08-05 00:44:28 -0400335 acpi_ns_detach_object(child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* Check if this node has any children */
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339 if (acpi_ns_get_next_node
340 (ACPI_TYPE_ANY, child_node, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /*
342 * There is at least one child of this node,
343 * visit the node
344 */
345 level++;
346 parent_node = child_node;
347 child_node = NULL;
348 }
Len Brown4be44fc2005-08-05 00:44:28 -0400349 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /*
351 * No more children of this parent node.
352 * Move up to the grandparent.
353 */
354 level--;
355
356 /*
357 * Now delete all of the children of this parent
358 * all at the same time.
359 */
Len Brown4be44fc2005-08-05 00:44:28 -0400360 acpi_ns_delete_children(parent_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* New "last child" is this parent node */
363
364 child_node = parent_node;
365
366 /* Move up the tree to the grandparent */
367
Len Brown4be44fc2005-08-05 00:44:28 -0400368 parent_node = acpi_ns_get_parent_node(parent_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 }
371
372 return_VOID;
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*******************************************************************************
376 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * FUNCTION: acpi_ns_delete_namespace_by_owner
378 *
379 * PARAMETERS: owner_id - All nodes with this owner will be deleted
380 *
381 * RETURN: Status
382 *
383 * DESCRIPTION: Delete entries within the namespace that are owned by a
384 * specific ID. Used to delete entire ACPI tables. All
385 * reference counts are updated.
386 *
387 ******************************************************************************/
388
Len Brown4be44fc2005-08-05 00:44:28 -0400389void acpi_ns_delete_namespace_by_owner(acpi_owner_id owner_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Len Brown4be44fc2005-08-05 00:44:28 -0400391 struct acpi_namespace_node *child_node;
392 struct acpi_namespace_node *deletion_node;
393 u32 level;
394 struct acpi_namespace_node *parent_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Len Brown4be44fc2005-08-05 00:44:28 -0400396 ACPI_FUNCTION_TRACE_U32("ns_delete_namespace_by_owner", owner_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Robert Moore0c9938c2005-07-29 15:15:00 -0700398 if (owner_id == 0) {
399 return_VOID;
400 }
401
Bob Moore61686122006-03-17 16:44:00 -0500402 deletion_node = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400403 parent_node = acpi_gbl_root_node;
404 child_node = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400405 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
408 * Traverse the tree of nodes until we bubble back up
409 * to where we started.
410 */
411 while (level > 0) {
412 /*
413 * Get the next child of this parent node. When child_node is NULL,
414 * the first child of the parent is returned
415 */
Len Brown4be44fc2005-08-05 00:44:28 -0400416 child_node =
417 acpi_ns_get_next_node(ACPI_TYPE_ANY, parent_node,
418 child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 if (deletion_node) {
Bob Moore61686122006-03-17 16:44:00 -0500421 acpi_ns_delete_children(deletion_node);
422 acpi_ns_delete_node(deletion_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 deletion_node = NULL;
424 }
425
426 if (child_node) {
427 if (child_node->owner_id == owner_id) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* Found a matching child node - detach any attached object */
430
Len Brown4be44fc2005-08-05 00:44:28 -0400431 acpi_ns_detach_object(child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
434 /* Check if this node has any children */
435
Len Brown4be44fc2005-08-05 00:44:28 -0400436 if (acpi_ns_get_next_node
437 (ACPI_TYPE_ANY, child_node, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /*
439 * There is at least one child of this node,
440 * visit the node
441 */
442 level++;
443 parent_node = child_node;
444 child_node = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400445 } else if (child_node->owner_id == owner_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 deletion_node = child_node;
447 }
Len Brown4be44fc2005-08-05 00:44:28 -0400448 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * No more children of this parent node.
451 * Move up to the grandparent.
452 */
453 level--;
454 if (level != 0) {
455 if (parent_node->owner_id == owner_id) {
456 deletion_node = parent_node;
457 }
458 }
459
460 /* New "last child" is this parent node */
461
462 child_node = parent_node;
463
464 /* Move up the tree to the grandparent */
465
Len Brown4be44fc2005-08-05 00:44:28 -0400466 parent_node = acpi_ns_get_parent_node(parent_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 }
469
470 return_VOID;
471}