xref: /titanic_41/usr/src/uts/intel/io/acpica/namespace/nsalloc.c (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
1 /*******************************************************************************
2  *
3  * Module Name: nsalloc - Namespace allocation and deletion utilities
4  *              $Revision: 1.109 $
5  *
6  ******************************************************************************/
7 
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2008, 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 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
146     UINT32                  Temp;
147 #endif
148 
149 
150     ACPI_FUNCTION_TRACE (NsCreateNode);
151 
152 
153     Node = AcpiOsAcquireObject (AcpiGbl_NamespaceCache);
154     if (!Node)
155     {
156         return_PTR (NULL);
157     }
158 
159     ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalAllocated++);
160 
161 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
162         Temp = AcpiGbl_NsNodeList->TotalAllocated - AcpiGbl_NsNodeList->TotalFreed;
163         if (Temp > AcpiGbl_NsNodeList->MaxOccupied)
164         {
165             AcpiGbl_NsNodeList->MaxOccupied = Temp;
166         }
167 #endif
168 
169     Node->Name.Integer = Name;
170     ACPI_SET_DESCRIPTOR_TYPE (Node, ACPI_DESC_TYPE_NAMED);
171     return_PTR (Node);
172 }
173 
174 
175 /*******************************************************************************
176  *
177  * FUNCTION:    AcpiNsDeleteNode
178  *
179  * PARAMETERS:  Node            - Node to be deleted
180  *
181  * RETURN:      None
182  *
183  * DESCRIPTION: Delete a namespace node
184  *
185  ******************************************************************************/
186 
187 void
188 AcpiNsDeleteNode (
189     ACPI_NAMESPACE_NODE     *Node)
190 {
191     ACPI_NAMESPACE_NODE     *ParentNode;
192     ACPI_NAMESPACE_NODE     *PrevNode;
193     ACPI_NAMESPACE_NODE     *NextNode;
194 
195 
196     ACPI_FUNCTION_TRACE_PTR (NsDeleteNode, Node);
197 
198 
199     ParentNode = AcpiNsGetParentNode (Node);
200 
201     PrevNode = NULL;
202     NextNode = ParentNode->Child;
203 
204     /* Find the node that is the previous peer in the parent's child list */
205 
206     while (NextNode != Node)
207     {
208         PrevNode = NextNode;
209         NextNode = PrevNode->Peer;
210     }
211 
212     if (PrevNode)
213     {
214         /* Node is not first child, unlink it */
215 
216         PrevNode->Peer = NextNode->Peer;
217         if (NextNode->Flags & ANOBJ_END_OF_PEER_LIST)
218         {
219             PrevNode->Flags |= ANOBJ_END_OF_PEER_LIST;
220         }
221     }
222     else
223     {
224         /* Node is first child (has no previous peer) */
225 
226         if (NextNode->Flags & ANOBJ_END_OF_PEER_LIST)
227         {
228             /* No peers at all */
229 
230             ParentNode->Child = NULL;
231         }
232         else
233         {   /* Link peer list to parent */
234 
235             ParentNode->Child = NextNode->Peer;
236         }
237     }
238 
239     ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);
240 
241     /*
242      * Detach an object if there is one, then delete the node
243      */
244     AcpiNsDetachObject (Node);
245     (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, Node);
246     return_VOID;
247 }
248 
249 
250 /*******************************************************************************
251  *
252  * FUNCTION:    AcpiNsInstallNode
253  *
254  * PARAMETERS:  WalkState       - Current state of the walk
255  *              ParentNode      - The parent of the new Node
256  *              Node            - The new Node to install
257  *              Type            - ACPI object type of the new Node
258  *
259  * RETURN:      None
260  *
261  * DESCRIPTION: Initialize a new namespace node and install it amongst
262  *              its peers.
263  *
264  *              Note: Current namespace lookup is linear search. This appears
265  *              to be sufficient as namespace searches consume only a small
266  *              fraction of the execution time of the ACPI subsystem.
267  *
268  ******************************************************************************/
269 
270 void
271 AcpiNsInstallNode (
272     ACPI_WALK_STATE         *WalkState,
273     ACPI_NAMESPACE_NODE     *ParentNode,    /* Parent */
274     ACPI_NAMESPACE_NODE     *Node,          /* New Child*/
275     ACPI_OBJECT_TYPE        Type)
276 {
277     ACPI_OWNER_ID           OwnerId = 0;
278     ACPI_NAMESPACE_NODE     *ChildNode;
279 
280 
281     ACPI_FUNCTION_TRACE (NsInstallNode);
282 
283 
284     /*
285      * Get the owner ID from the Walk state
286      * The owner ID is used to track table deletion and
287      * deletion of objects created by methods
288      */
289     if (WalkState)
290     {
291         OwnerId = WalkState->OwnerId;
292     }
293 
294     /* Link the new entry into the parent and existing children */
295 
296     ChildNode = ParentNode->Child;
297     if (!ChildNode)
298     {
299         ParentNode->Child = Node;
300         Node->Flags |= ANOBJ_END_OF_PEER_LIST;
301         Node->Peer = ParentNode;
302     }
303     else
304     {
305         while (!(ChildNode->Flags & ANOBJ_END_OF_PEER_LIST))
306         {
307             ChildNode = ChildNode->Peer;
308         }
309 
310         ChildNode->Peer = Node;
311 
312         /* Clear end-of-list flag */
313 
314         ChildNode->Flags &= ~ANOBJ_END_OF_PEER_LIST;
315         Node->Flags |= ANOBJ_END_OF_PEER_LIST;
316         Node->Peer = ParentNode;
317     }
318 
319     /* Init the new entry */
320 
321     Node->OwnerId = OwnerId;
322     Node->Type = (UINT8) Type;
323 
324     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
325         "%4.4s (%s) [Node %p Owner %X] added to %4.4s (%s) [Node %p]\n",
326         AcpiUtGetNodeName (Node), AcpiUtGetTypeName (Node->Type), Node, OwnerId,
327         AcpiUtGetNodeName (ParentNode), AcpiUtGetTypeName (ParentNode->Type),
328         ParentNode));
329 
330     return_VOID;
331 }
332 
333 
334 /*******************************************************************************
335  *
336  * FUNCTION:    AcpiNsDeleteChildren
337  *
338  * PARAMETERS:  ParentNode      - Delete this objects children
339  *
340  * RETURN:      None.
341  *
342  * DESCRIPTION: Delete all children of the parent object. In other words,
343  *              deletes a "scope".
344  *
345  ******************************************************************************/
346 
347 void
348 AcpiNsDeleteChildren (
349     ACPI_NAMESPACE_NODE     *ParentNode)
350 {
351     ACPI_NAMESPACE_NODE     *ChildNode;
352     ACPI_NAMESPACE_NODE     *NextNode;
353     UINT8                   Flags;
354 
355 
356     ACPI_FUNCTION_TRACE_PTR (NsDeleteChildren, ParentNode);
357 
358 
359     if (!ParentNode)
360     {
361         return_VOID;
362     }
363 
364     /* If no children, all done! */
365 
366     ChildNode = ParentNode->Child;
367     if (!ChildNode)
368     {
369         return_VOID;
370     }
371 
372     /*
373      * Deallocate all children at this level
374      */
375     do
376     {
377         /* Get the things we need */
378 
379         NextNode = ChildNode->Peer;
380         Flags = ChildNode->Flags;
381 
382         /* Grandchildren should have all been deleted already */
383 
384         if (ChildNode->Child)
385         {
386             ACPI_ERROR ((AE_INFO, "Found a grandchild! P=%p C=%p",
387                 ParentNode, ChildNode));
388         }
389 
390         /* Now we can free this child object */
391 
392         ACPI_MEM_TRACKING (AcpiGbl_NsNodeList->TotalFreed++);
393 
394         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Object %p, Remaining %X\n",
395             ChildNode, AcpiGbl_CurrentNodeCount));
396 
397         /*
398          * Detach an object if there is one, then free the child node
399          */
400         AcpiNsDetachObject (ChildNode);
401 
402         /* Now we can delete the node */
403 
404         (void) AcpiOsReleaseObject (AcpiGbl_NamespaceCache, ChildNode);
405 
406         /* And move on to the next child in the list */
407 
408         ChildNode = NextNode;
409 
410     } while (!(Flags & ANOBJ_END_OF_PEER_LIST));
411 
412 
413     /* Clear the parent's child pointer */
414 
415     ParentNode->Child = NULL;
416 
417     return_VOID;
418 }
419 
420 
421 /*******************************************************************************
422  *
423  * FUNCTION:    AcpiNsDeleteNamespaceSubtree
424  *
425  * PARAMETERS:  ParentNode      - Root of the subtree to be deleted
426  *
427  * RETURN:      None.
428  *
429  * DESCRIPTION: Delete a subtree of the namespace.  This includes all objects
430  *              stored within the subtree.
431  *
432  ******************************************************************************/
433 
434 void
435 AcpiNsDeleteNamespaceSubtree (
436     ACPI_NAMESPACE_NODE     *ParentNode)
437 {
438     ACPI_NAMESPACE_NODE     *ChildNode = NULL;
439     UINT32                  Level = 1;
440 
441 
442     ACPI_FUNCTION_TRACE (NsDeleteNamespaceSubtree);
443 
444 
445     if (!ParentNode)
446     {
447         return_VOID;
448     }
449 
450     /*
451      * Traverse the tree of objects until we bubble back up
452      * to where we started.
453      */
454     while (Level > 0)
455     {
456         /* Get the next node in this scope (NULL if none) */
457 
458         ChildNode = AcpiNsGetNextNode (ACPI_TYPE_ANY, ParentNode, ChildNode);
459         if (ChildNode)
460         {
461             /* Found a child node - detach any attached object */
462 
463             AcpiNsDetachObject (ChildNode);
464 
465             /* Check if this node has any children */
466 
467             if (AcpiNsGetNextNode (ACPI_TYPE_ANY, ChildNode, NULL))
468             {
469                 /*
470                  * There is at least one child of this node,
471                  * visit the node
472                  */
473                 Level++;
474                 ParentNode = ChildNode;
475                 ChildNode  = NULL;
476             }
477         }
478         else
479         {
480             /*
481              * No more children of this parent node.
482              * Move up to the grandparent.
483              */
484             Level--;
485 
486             /*
487              * Now delete all of the children of this parent
488              * all at the same time.
489              */
490             AcpiNsDeleteChildren (ParentNode);
491 
492             /* New "last child" is this parent node */
493 
494             ChildNode = ParentNode;
495 
496             /* Move up the tree to the grandparent */
497 
498             ParentNode = AcpiNsGetParentNode (ParentNode);
499         }
500     }
501 
502     return_VOID;
503 }
504 
505 
506 /*******************************************************************************
507  *
508  * FUNCTION:    AcpiNsDeleteNamespaceByOwner
509  *
510  * PARAMETERS:  OwnerId     - All nodes with this owner will be deleted
511  *
512  * RETURN:      Status
513  *
514  * DESCRIPTION: Delete entries within the namespace that are owned by a
515  *              specific ID.  Used to delete entire ACPI tables.  All
516  *              reference counts are updated.
517  *
518  * MUTEX:       Locks namespace during deletion walk.
519  *
520  ******************************************************************************/
521 
522 void
523 AcpiNsDeleteNamespaceByOwner (
524     ACPI_OWNER_ID            OwnerId)
525 {
526     ACPI_NAMESPACE_NODE     *ChildNode;
527     ACPI_NAMESPACE_NODE     *DeletionNode;
528     ACPI_NAMESPACE_NODE     *ParentNode;
529     UINT32                  Level;
530     ACPI_STATUS             Status;
531 
532 
533     ACPI_FUNCTION_TRACE_U32 (NsDeleteNamespaceByOwner, OwnerId);
534 
535 
536     if (OwnerId == 0)
537     {
538         return_VOID;
539     }
540 
541     /* Lock namespace for possible update */
542 
543     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
544     if (ACPI_FAILURE (Status))
545     {
546         return_VOID;
547     }
548 
549     DeletionNode = NULL;
550     ParentNode = AcpiGbl_RootNode;
551     ChildNode = NULL;
552     Level = 1;
553 
554     /*
555      * Traverse the tree of nodes until we bubble back up
556      * to where we started.
557      */
558     while (Level > 0)
559     {
560         /*
561          * Get the next child of this parent node. When ChildNode is NULL,
562          * the first child of the parent is returned
563          */
564         ChildNode = AcpiNsGetNextNode (ACPI_TYPE_ANY, ParentNode, ChildNode);
565 
566         if (DeletionNode)
567         {
568             AcpiNsDeleteChildren (DeletionNode);
569             AcpiNsDeleteNode (DeletionNode);
570             DeletionNode = NULL;
571         }
572 
573         if (ChildNode)
574         {
575             if (ChildNode->OwnerId == OwnerId)
576             {
577                 /* Found a matching child node - detach any attached object */
578 
579                 AcpiNsDetachObject (ChildNode);
580             }
581 
582             /* Check if this node has any children */
583 
584             if (AcpiNsGetNextNode (ACPI_TYPE_ANY, ChildNode, NULL))
585             {
586                 /*
587                  * There is at least one child of this node,
588                  * visit the node
589                  */
590                 Level++;
591                 ParentNode = ChildNode;
592                 ChildNode  = NULL;
593             }
594             else if (ChildNode->OwnerId == OwnerId)
595             {
596                 DeletionNode = ChildNode;
597             }
598         }
599         else
600         {
601             /*
602              * No more children of this parent node.
603              * Move up to the grandparent.
604              */
605             Level--;
606             if (Level != 0)
607             {
608                 if (ParentNode->OwnerId == OwnerId)
609                 {
610                     DeletionNode = ParentNode;
611                 }
612             }
613 
614             /* New "last child" is this parent node */
615 
616             ChildNode = ParentNode;
617 
618             /* Move up the tree to the grandparent */
619 
620             ParentNode = AcpiNsGetParentNode (ParentNode);
621         }
622     }
623 
624     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
625     return_VOID;
626 }
627 
628 
629