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