dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / uts / intel / io / acpica / namespace / nswalk.c
blob08637a6eeec5ca45b0569ae9d493e2e700229f3d
1 /******************************************************************************
3 * Module Name: nswalk - Functions for walking the ACPI namespace
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acnamesp.h"
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME ("nswalk")
53 /*******************************************************************************
55 * FUNCTION: AcpiNsGetNextNode
57 * PARAMETERS: ParentNode - Parent node whose children we are
58 * getting
59 * ChildNode - Previous child that was found.
60 * The NEXT child will be returned
62 * RETURN: ACPI_NAMESPACE_NODE - Pointer to the NEXT child or NULL if
63 * none is found.
65 * DESCRIPTION: Return the next peer node within the namespace. If Handle
66 * is valid, Scope is ignored. Otherwise, the first node
67 * within Scope is returned.
69 ******************************************************************************/
71 ACPI_NAMESPACE_NODE *
72 AcpiNsGetNextNode (
73 ACPI_NAMESPACE_NODE *ParentNode,
74 ACPI_NAMESPACE_NODE *ChildNode)
76 ACPI_FUNCTION_ENTRY ();
79 if (!ChildNode)
81 /* It's really the parent's _scope_ that we want */
83 return (ParentNode->Child);
86 /* Otherwise just return the next peer */
88 return (ChildNode->Peer);
92 /*******************************************************************************
94 * FUNCTION: AcpiNsGetNextNodeTyped
96 * PARAMETERS: Type - Type of node to be searched for
97 * ParentNode - Parent node whose children we are
98 * getting
99 * ChildNode - Previous child that was found.
100 * The NEXT child will be returned
102 * RETURN: ACPI_NAMESPACE_NODE - Pointer to the NEXT child or NULL if
103 * none is found.
105 * DESCRIPTION: Return the next peer node within the namespace. If Handle
106 * is valid, Scope is ignored. Otherwise, the first node
107 * within Scope is returned.
109 ******************************************************************************/
111 ACPI_NAMESPACE_NODE *
112 AcpiNsGetNextNodeTyped (
113 ACPI_OBJECT_TYPE Type,
114 ACPI_NAMESPACE_NODE *ParentNode,
115 ACPI_NAMESPACE_NODE *ChildNode)
117 ACPI_NAMESPACE_NODE *NextNode = NULL;
120 ACPI_FUNCTION_ENTRY ();
123 NextNode = AcpiNsGetNextNode (ParentNode, ChildNode);
125 /* If any type is OK, we are done */
127 if (Type == ACPI_TYPE_ANY)
129 /* NextNode is NULL if we are at the end-of-list */
131 return (NextNode);
134 /* Must search for the node -- but within this scope only */
136 while (NextNode)
138 /* If type matches, we are done */
140 if (NextNode->Type == Type)
142 return (NextNode);
145 /* Otherwise, move on to the next peer node */
147 NextNode = NextNode->Peer;
150 /* Not found */
152 return (NULL);
156 /*******************************************************************************
158 * FUNCTION: AcpiNsWalkNamespace
160 * PARAMETERS: Type - ACPI_OBJECT_TYPE to search for
161 * StartNode - Handle in namespace where search begins
162 * MaxDepth - Depth to which search is to reach
163 * Flags - Whether to unlock the NS before invoking
164 * the callback routine
165 * DescendingCallback - Called during tree descent
166 * when an object of "Type" is found
167 * AscendingCallback - Called during tree ascent
168 * when an object of "Type" is found
169 * Context - Passed to user function(s) above
170 * ReturnValue - from the UserFunction if terminated
171 * early. Otherwise, returns NULL.
172 * RETURNS: Status
174 * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
175 * starting (and ending) at the node specified by StartHandle.
176 * The callback function is called whenever a node that matches
177 * the type parameter is found. If the callback function returns
178 * a non-zero value, the search is terminated immediately and
179 * this value is returned to the caller.
181 * The point of this procedure is to provide a generic namespace
182 * walk routine that can be called from multiple places to
183 * provide multiple services; the callback function(s) can be
184 * tailored to each task, whether it is a print function,
185 * a compare function, etc.
187 ******************************************************************************/
189 ACPI_STATUS
190 AcpiNsWalkNamespace (
191 ACPI_OBJECT_TYPE Type,
192 ACPI_HANDLE StartNode,
193 UINT32 MaxDepth,
194 UINT32 Flags,
195 ACPI_WALK_CALLBACK DescendingCallback,
196 ACPI_WALK_CALLBACK AscendingCallback,
197 void *Context,
198 void **ReturnValue)
200 ACPI_STATUS Status;
201 ACPI_STATUS MutexStatus;
202 ACPI_NAMESPACE_NODE *ChildNode;
203 ACPI_NAMESPACE_NODE *ParentNode;
204 ACPI_OBJECT_TYPE ChildType;
205 UINT32 Level;
206 BOOLEAN NodePreviouslyVisited = FALSE;
209 ACPI_FUNCTION_TRACE (NsWalkNamespace);
212 /* Special case for the namespace Root Node */
214 if (StartNode == ACPI_ROOT_OBJECT)
216 StartNode = AcpiGbl_RootNode;
219 /* Null child means "get first node" */
221 ParentNode = StartNode;
222 ChildNode = AcpiNsGetNextNode (ParentNode, NULL);
223 ChildType = ACPI_TYPE_ANY;
224 Level = 1;
227 * Traverse the tree of nodes until we bubble back up to where we
228 * started. When Level is zero, the loop is done because we have
229 * bubbled up to (and passed) the original parent handle (StartEntry)
231 while (Level > 0 && ChildNode)
233 Status = AE_OK;
235 /* Found next child, get the type if we are not searching for ANY */
237 if (Type != ACPI_TYPE_ANY)
239 ChildType = ChildNode->Type;
243 * Ignore all temporary namespace nodes (created during control
244 * method execution) unless told otherwise. These temporary nodes
245 * can cause a race condition because they can be deleted during
246 * the execution of the user function (if the namespace is
247 * unlocked before invocation of the user function.) Only the
248 * debugger namespace dump will examine the temporary nodes.
250 if ((ChildNode->Flags & ANOBJ_TEMPORARY) &&
251 !(Flags & ACPI_NS_WALK_TEMP_NODES))
253 Status = AE_CTRL_DEPTH;
256 /* Type must match requested type */
258 else if (ChildType == Type)
261 * Found a matching node, invoke the user callback function.
262 * Unlock the namespace if flag is set.
264 if (Flags & ACPI_NS_WALK_UNLOCK)
266 MutexStatus = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
267 if (ACPI_FAILURE (MutexStatus))
269 return_ACPI_STATUS (MutexStatus);
274 * Invoke the user function, either descending, ascending,
275 * or both.
277 if (!NodePreviouslyVisited)
279 if (DescendingCallback)
281 Status = DescendingCallback (ChildNode, Level,
282 Context, ReturnValue);
285 else
287 if (AscendingCallback)
289 Status = AscendingCallback (ChildNode, Level,
290 Context, ReturnValue);
294 if (Flags & ACPI_NS_WALK_UNLOCK)
296 MutexStatus = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
297 if (ACPI_FAILURE (MutexStatus))
299 return_ACPI_STATUS (MutexStatus);
303 switch (Status)
305 case AE_OK:
306 case AE_CTRL_DEPTH:
308 /* Just keep going */
309 break;
311 case AE_CTRL_TERMINATE:
313 /* Exit now, with OK status */
315 return_ACPI_STATUS (AE_OK);
317 default:
319 /* All others are valid exceptions */
321 return_ACPI_STATUS (Status);
326 * Depth first search: Attempt to go down another level in the
327 * namespace if we are allowed to. Don't go any further if we have
328 * reached the caller specified maximum depth or if the user
329 * function has specified that the maximum depth has been reached.
331 if (!NodePreviouslyVisited &&
332 (Level < MaxDepth) &&
333 (Status != AE_CTRL_DEPTH))
335 if (ChildNode->Child)
337 /* There is at least one child of this node, visit it */
339 Level++;
340 ParentNode = ChildNode;
341 ChildNode = AcpiNsGetNextNode (ParentNode, NULL);
342 continue;
346 /* No more children, re-visit this node */
348 if (!NodePreviouslyVisited)
350 NodePreviouslyVisited = TRUE;
351 continue;
354 /* No more children, visit peers */
356 ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
357 if (ChildNode)
359 NodePreviouslyVisited = FALSE;
362 /* No peers, re-visit parent */
364 else
367 * No more children of this node (AcpiNsGetNextNode failed), go
368 * back upwards in the namespace tree to the node's parent.
370 Level--;
371 ChildNode = ParentNode;
372 ParentNode = ParentNode->Parent;
374 NodePreviouslyVisited = TRUE;
378 /* Complete walk, not terminated by user function */
380 return_ACPI_STATUS (AE_OK);