xref: /titanic_44/usr/src/uts/intel/io/acpica/namespace/nsalloc.c (revision f48205be61a214698b763ff550ab9e657525104c)
1 /*******************************************************************************
2  *
3  * Module Name: nsalloc - Namespace allocation and deletion utilities
4  *              $Revision: 1.106 $
5  *
6  ******************************************************************************/
7 
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116 
117 
118 #define __NSALLOC_C__
119 
120 #include "acpi.h"
121 #include "acnamesp.h"
122 
123 
124 #define _COMPONENT          ACPI_NAMESPACE
125         ACPI_MODULE_NAME    ("nsalloc")
126 
127 
128 /*******************************************************************************
129  *
130  * FUNCTION:    AcpiNsCreateNode
131  *
132  * PARAMETERS:  Name            - Name of the new node (4 char ACPI name)
133  *
134  * RETURN:      New namespace node (Null on failure)
135  *
136  * DESCRIPTION: Create a namespace node
137  *
138  ******************************************************************************/
139 
140 ACPI_NAMESPACE_NODE *
141 AcpiNsCreateNode (
142     UINT32                  Name)
143 {
144     ACPI_NAMESPACE_NODE     *Node;
145 
146 
147     ACPI_FUNCTION_TRACE (NsCreateNode);
148 
149 
150     Node = AcpiOsAcquireObject (AcpiGbl_NamespaceCache);
151     if (!Node)
152     {
153         return_PTR (NULL);
154     }
155 
156     ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalAllocated++);
157 
158     Node->Name.Integer = Name;
159     ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);
160     return_PTR (Node);
161 }
162 
163 
164 /*******************************************************************************
165  *
166  * FUNCTION:    AcpiNsDeleteNode
167  *
168  * PARAMETERS:  Node            - Node to be deleted
169  *
170  * RETURN:      None
171  *
172  * DESCRIPTION: Delete a namespace node
173  *
174  ******************************************************************************/
175 
176 void
177 AcpiNsDeleteNode (
178     ACPI_NAMESPACE_NODE     *Node)
179 {
180     ACPI_NAMESPACE_NODE     *ParentNode;
181     ACPI_NAMESPACE_NODE     *PrevNode;
182     ACPI_NAMESPACE_NODE     *NextNode;
183 
184 
185     ACPI_FUNCTION_TRACE_PTR (NsDeleteNode, Node);
186 
187 
188     ParentNode = AcpiNsGetParentNode (Node);
189 
190     PrevNode = NULL;
191     NextNode = ParentNode->Child;
192 
193     /* Find the node that is the previous peer in the parent's child list */
194 
195     while (NextNode != Node)
196     {
197         PrevNode = NextNode;
198         NextNode = PrevNode->Peer;
199     }
200 
201     if (PrevNode)
202     {
203         /* Node is not first child, unlink it */
204 
205         PrevNode->Peer = NextNode->Peer;
206         if (NextNode->Flags & ANOBJ_END_OF_PEER_LIST)
207         {
208             PrevNode->Flags |= ANOBJ_END_OF_PEER_LIST;
209         }
210     }
211     else
212     {
213         /* Node is first child (has no previous peer) */
214 
215         if (NextNode->Flags & ANOBJ_END_OF_PEER_LIST)
216         {
217             /* No peers at all */
218 
219             ParentNode->Child = NULL;
220         }
221         else
222         {   /* Link peer list to parent */
223 
224             ParentNode->Child = NextNode->Peer;
225         }
226     }
227 
228     ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);
229 
230     /*
231      * Detach an object if there is one, then delete the node
232      */
233     AcpiNsDetachObject (Node);
234     (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, Node);
235     return_VOID;
236 }
237 
238 
239 /*******************************************************************************
240  *
241  * FUNCTION:    AcpiNsInstallNode
242  *
243  * PARAMETERS:  WalkState       - Current state of the walk
244  *              ParentNode      - The parent of the new Node
245  *              Node            - The new Node to install
246  *              Type            - ACPI object type of the new Node
247  *
248  * RETURN:      None
249  *
250  * DESCRIPTION: Initialize a new namespace node and install it amongst
251  *              its peers.
252  *
253  *              Note: Current namespace lookup is linear search. This appears
254  *              to be sufficient as namespace searches consume only a small
255  *              fraction of the execution time of the ACPI subsystem.
256  *
257  ******************************************************************************/
258 
259 void
260 AcpiNsInstallNode (
261     ACPI_WALK_STATE         *WalkState,
262     ACPI_NAMESPACE_NODE     *ParentNode,    /* Parent */
263     ACPI_NAMESPACE_NODE     *Node,          /* New Child*/
264     ACPI_OBJECT_TYPE        Type)
265 {
266     ACPI_OWNER_ID           OwnerId = 0;
267     ACPI_NAMESPACE_NODE     *ChildNode;
268 
269 
270     ACPI_FUNCTION_TRACE (NsInstallNode);
271 
272 
273     /*
274      * Get the owner ID from the Walk state
275      * The owner ID is used to track table deletion and
276      * deletion of objects created by methods
277      */
278     if (WalkState)
279     {
280         OwnerId = WalkState->OwnerId;
281     }
282 
283     /* Link the new entry into the parent and existing children */
284 
285     ChildNode = ParentNode->Child;
286     if (!ChildNode)
287     {
288         ParentNode->Child = Node;
289         Node->Flags |= ANOBJ_END_OF_PEER_LIST;
290         Node->Peer = ParentNode;
291     }
292     else
293     {
294         while (!(ChildNode->Flags & ANOBJ_END_OF_PEER_LIST))
295         {
296             ChildNode = ChildNode->Peer;
297         }
298 
299         ChildNode->Peer = Node;
300 
301         /* Clear end-of-list flag */
302 
303         ChildNode->Flags &= ~ANOBJ_END_OF_PEER_LIST;
304         Node->Flags |= ANOBJ_END_OF_PEER_LIST;
305         Node->Peer = ParentNode;
306     }
307 
308     /* Init the new entry */
309 
310     Node->OwnerId = OwnerId;
311     Node->Type = (UINT8) Type;
312 
313     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
314         "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
315         AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type), Node, OwnerId,
316         AcpiUtGetNodeName (ParentNode), AcpiUtGetTypeName (ParentNode->Type),
317         ParentNode));
318 
319     return_VOID;
320 }
321 
322 
323 /*******************************************************************************
324  *
325  * FUNCTION:    AcpiNsDeleteChildren
326  *
327  * PARAMETERS:  ParentNode      - Delete this objects children
328  *
329  * RETURN:      None.
330  *
331  * DESCRIPTION: Delete all children of the parent object. In other words,
332  *              deletes a "scope".
333  *
334  ******************************************************************************/
335 
336 void
337 AcpiNsDeleteChildren (
338     ACPI_NAMESPACE_NODE     *ParentNode)
339 {
340     ACPI_NAMESPACE_NODE     *ChildNode;
341     ACPI_NAMESPACE_NODE     *NextNode;
342     UINT8                   Flags;
343 
344 
345     ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren, ParentNode);
346 
347 
348     if (!ParentNode)
349     {
350         return_VOID;
351     }
352 
353     /* If no children, all done! */
354 
355     ChildNode = ParentNode->Child;
356     if (!ChildNode)
357     {
358         return_VOID;
359     }
360 
361     /*
362      * Deallocate all children at this level
363      */
364     do
365     {
366         /* Get the things we need */
367 
368         NextNode = ChildNode->Peer;
369         Flags = ChildNode->Flags;
370 
371         /* Grandchildren should have all been deleted already */
372 
373         if (ChildNode->Child)
374         {
375             ACPI_ERROR ((AE_INFO, "Found a grandchild! P=%p C=%p",
376                 ParentNode, ChildNode));
377         }
378 
379         /* Now we can free this child object */
380 
381         ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);
382 
383         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Object %p, Remaining %X\n",
384             ChildNode, AcpiGbl_CurrentNodeCount));
385 
386         /*
387          * Detach an object if there is one, then free the child node
388          */
389         AcpiNsDetachObject (ChildNode);
390 
391         /* Now we can delete the node */
392 
393         (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, ChildNode);
394 
395         /* And move on to the next child in the list */
396 
397         ChildNode = NextNode;
398 
399     } while (!(Flags & ANOBJ_END_OF_PEER_LIST));
400 
401 
402     /* Clear the parent's child pointer */
403 
404     ParentNode->Child = NULL;
405 
406     return_VOID;
407 }
408 
409 
410 /*******************************************************************************
411  *
412  * FUNCTION:    AcpiNsDeleteNamespaceSubtree
413  *
414  * PARAMETERS:  ParentNode      - Root of the subtree to be deleted
415  *
416  * RETURN:      None.
417  *
418  * DESCRIPTION: Delete a subtree of the namespace.  This includes all objects
419  *              stored within the subtree.
420  *
421  ******************************************************************************/
422 
423 void
424 AcpiNsDeleteNamespaceSubtree (
425     ACPI_NAMESPACE_NODE     *ParentNode)
426 {
427     ACPI_NAMESPACE_NODE     *ChildNode = NULL;
428     UINT32                  Level = 1;
429 
430 
431     ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree);
432 
433 
434     if (!ParentNode)
435     {
436         return_VOID;
437     }
438 
439     /*
440      * Traverse the tree of objects until we bubble back up
441      * to where we started.
442      */
443     while (Level > 0)
444     {
445         /* Get the next node in this scope (NULL if none) */
446 
447         ChildNode = AcpiNsGetNextNode (ACPI_TYPE_ANY, ParentNode, ChildNode);
448         if (ChildNode)
449         {
450             /* Found a child node - detach any attached object */
451 
452             AcpiNsDetachObject (ChildNode);
453 
454             /* Check if this node has any children */
455 
456             if (AcpiNsGetNextNode (ACPI_TYPE_ANY, ChildNode, NULL))
457             {
458                 /*
459                  * There is at least one child of this node,
460                  * visit the node
461                  */
462                 Level++;
463                 ParentNode = ChildNode;
464                 ChildNode  = NULL;
465             }
466         }
467         else
468         {
469             /*
470              * No more children of this parent node.
471              * Move up to the grandparent.
472              */
473             Level--;
474 
475             /*
476              * Now delete all of the children of this parent
477              * all at the same time.
478              */
479             AcpiNsDeleteChildren (ParentNode);
480 
481             /* New "last child" is this parent node */
482 
483             ChildNode = ParentNode;
484 
485             /* Move up the tree to the grandparent */
486 
487             ParentNode = AcpiNsGetParentNode (ParentNode);
488         }
489     }
490 
491     return_VOID;
492 }
493 
494 
495 /*******************************************************************************
496  *
497  * FUNCTION:    AcpiNsDeleteNamespaceByOwner
498  *
499  * PARAMETERS:  OwnerId     - All nodes with this owner will be deleted
500  *
501  * RETURN:      Status
502  *
503  * DESCRIPTION: Delete entries within the namespace that are owned by a
504  *              specific ID.  Used to delete entire ACPI tables.  All
505  *              reference counts are updated.
506  *
507  * MUTEX:       Locks namespace during deletion walk.
508  *
509  ******************************************************************************/
510 
511 void
512 AcpiNsDeleteNamespaceByOwner (
513     ACPI_OWNER_ID            OwnerId)
514 {
515     ACPI_NAMESPACE_NODE     *ChildNode;
516     ACPI_NAMESPACE_NODE     *DeletionNode;
517     ACPI_NAMESPACE_NODE     *ParentNode;
518     UINT32                  Level;
519     ACPI_STATUS             Status;
520 
521 
522     ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner, OwnerId);
523 
524 
525     if (OwnerId == 0)
526     {
527         return_VOID;
528     }
529 
530     /* Lock namespace for possible update */
531 
532     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
533     if (ACPI_FAILURE (Status))
534     {
535         return_VOID;
536     }
537 
538     DeletionNode = NULL;
539     ParentNode = AcpiGbl_RootNode;
540     ChildNode = NULL;
541     Level = 1;
542 
543     /*
544      * Traverse the tree of nodes until we bubble back up
545      * to where we started.
546      */
547     while (Level > 0)
548     {
549         /*
550          * Get the next child of this parent node. When ChildNode is NULL,
551          * the first child of the parent is returned
552          */
553         ChildNode = AcpiNsGetNextNode (ACPI_TYPE_ANY, ParentNode, ChildNode);
554 
555         if (DeletionNode)
556         {
557             AcpiNsDeleteChildren (DeletionNode);
558             AcpiNsDeleteNode (DeletionNode);
559             DeletionNode = NULL;
560         }
561 
562         if (ChildNode)
563         {
564             if (ChildNode->OwnerId == OwnerId)
565             {
566                 /* Found a matching child node - detach any attached object */
567 
568                 AcpiNsDetachObject (ChildNode);
569             }
570 
571             /* Check if this node has any children */
572 
573             if (AcpiNsGetNextNode (ACPI_TYPE_ANY, ChildNode, NULL))
574             {
575                 /*
576                  * There is at least one child of this node,
577                  * visit the node
578                  */
579                 Level++;
580                 ParentNode = ChildNode;
581                 ChildNode  = NULL;
582             }
583             else if (ChildNode->OwnerId == OwnerId)
584             {
585                 DeletionNode = ChildNode;
586             }
587         }
588         else
589         {
590             /*
591              * No more children of this parent node.
592              * Move up to the grandparent.
593              */
594             Level--;
595             if (Level != 0)
596             {
597                 if (ParentNode->OwnerId == OwnerId)
598                 {
599                     DeletionNode = ParentNode;
600                 }
601             }
602 
603             /* New "last child" is this parent node */
604 
605             ChildNode = ParentNode;
606 
607             /* Move up the tree to the grandparent */
608 
609             ParentNode = AcpiNsGetParentNode (ParentNode);
610         }
611     }
612 
613     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
614     return_VOID;
615 }
616 
617 
618