Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /******************************************************************************
0003  *
0004  * Module Name: nswalk - Functions for walking the ACPI namespace
0005  *
0006  * Copyright (C) 2000 - 2022, Intel Corp.
0007  *
0008  *****************************************************************************/
0009 
0010 #include <acpi/acpi.h>
0011 #include "accommon.h"
0012 #include "acnamesp.h"
0013 
0014 #define _COMPONENT          ACPI_NAMESPACE
0015 ACPI_MODULE_NAME("nswalk")
0016 
0017 /*******************************************************************************
0018  *
0019  * FUNCTION:    acpi_ns_get_next_node
0020  *
0021  * PARAMETERS:  parent_node         - Parent node whose children we are
0022  *                                    getting
0023  *              child_node          - Previous child that was found.
0024  *                                    The NEXT child will be returned
0025  *
0026  * RETURN:      struct acpi_namespace_node - Pointer to the NEXT child or NULL if
0027  *                                    none is found.
0028  *
0029  * DESCRIPTION: Return the next peer node within the namespace. If Handle
0030  *              is valid, Scope is ignored. Otherwise, the first node
0031  *              within Scope is returned.
0032  *
0033  ******************************************************************************/
0034 struct acpi_namespace_node *acpi_ns_get_next_node(struct acpi_namespace_node
0035                           *parent_node,
0036                           struct acpi_namespace_node
0037                           *child_node)
0038 {
0039     ACPI_FUNCTION_ENTRY();
0040 
0041     if (!child_node) {
0042 
0043         /* It's really the parent's _scope_ that we want */
0044 
0045         return (parent_node->child);
0046     }
0047 
0048     /* Otherwise just return the next peer */
0049 
0050     return (child_node->peer);
0051 }
0052 
0053 /*******************************************************************************
0054  *
0055  * FUNCTION:    acpi_ns_get_next_node_typed
0056  *
0057  * PARAMETERS:  type                - Type of node to be searched for
0058  *              parent_node         - Parent node whose children we are
0059  *                                    getting
0060  *              child_node          - Previous child that was found.
0061  *                                    The NEXT child will be returned
0062  *
0063  * RETURN:      struct acpi_namespace_node - Pointer to the NEXT child or NULL if
0064  *                                    none is found.
0065  *
0066  * DESCRIPTION: Return the next peer node within the namespace. If Handle
0067  *              is valid, Scope is ignored. Otherwise, the first node
0068  *              within Scope is returned.
0069  *
0070  ******************************************************************************/
0071 
0072 struct acpi_namespace_node *acpi_ns_get_next_node_typed(acpi_object_type type,
0073                             struct
0074                             acpi_namespace_node
0075                             *parent_node,
0076                             struct
0077                             acpi_namespace_node
0078                             *child_node)
0079 {
0080     struct acpi_namespace_node *next_node = NULL;
0081 
0082     ACPI_FUNCTION_ENTRY();
0083 
0084     next_node = acpi_ns_get_next_node(parent_node, child_node);
0085 
0086 
0087     /* If any type is OK, we are done */
0088 
0089     if (type == ACPI_TYPE_ANY) {
0090 
0091         /* next_node is NULL if we are at the end-of-list */
0092 
0093         return (next_node);
0094     }
0095 
0096     /* Must search for the node -- but within this scope only */
0097 
0098     while (next_node) {
0099 
0100         /* If type matches, we are done */
0101 
0102         if (next_node->type == type) {
0103             return (next_node);
0104         }
0105 
0106         /* Otherwise, move on to the next peer node */
0107 
0108         next_node = next_node->peer;
0109     }
0110 
0111     /* Not found */
0112 
0113     return (NULL);
0114 }
0115 
0116 /*******************************************************************************
0117  *
0118  * FUNCTION:    acpi_ns_walk_namespace
0119  *
0120  * PARAMETERS:  type                - acpi_object_type to search for
0121  *              start_node          - Handle in namespace where search begins
0122  *              max_depth           - Depth to which search is to reach
0123  *              flags               - Whether to unlock the NS before invoking
0124  *                                    the callback routine
0125  *              descending_callback - Called during tree descent
0126  *                                    when an object of "Type" is found
0127  *              ascending_callback  - Called during tree ascent
0128  *                                    when an object of "Type" is found
0129  *              context             - Passed to user function(s) above
0130  *              return_value        - from the user_function if terminated
0131  *                                    early. Otherwise, returns NULL.
0132  * RETURNS:     Status
0133  *
0134  * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
0135  *              starting (and ending) at the node specified by start_handle.
0136  *              The callback function is called whenever a node that matches
0137  *              the type parameter is found. If the callback function returns
0138  *              a non-zero value, the search is terminated immediately and
0139  *              this value is returned to the caller.
0140  *
0141  *              The point of this procedure is to provide a generic namespace
0142  *              walk routine that can be called from multiple places to
0143  *              provide multiple services; the callback function(s) can be
0144  *              tailored to each task, whether it is a print function,
0145  *              a compare function, etc.
0146  *
0147  ******************************************************************************/
0148 
0149 acpi_status
0150 acpi_ns_walk_namespace(acpi_object_type type,
0151                acpi_handle start_node,
0152                u32 max_depth,
0153                u32 flags,
0154                acpi_walk_callback descending_callback,
0155                acpi_walk_callback ascending_callback,
0156                void *context, void **return_value)
0157 {
0158     acpi_status status;
0159     acpi_status mutex_status;
0160     struct acpi_namespace_node *child_node;
0161     struct acpi_namespace_node *parent_node;
0162     acpi_object_type child_type;
0163     u32 level;
0164     u8 node_previously_visited = FALSE;
0165 
0166     ACPI_FUNCTION_TRACE(ns_walk_namespace);
0167 
0168     /* Special case for the namespace Root Node */
0169 
0170     if (start_node == ACPI_ROOT_OBJECT) {
0171         start_node = acpi_gbl_root_node;
0172         if (!start_node) {
0173             return_ACPI_STATUS(AE_NO_NAMESPACE);
0174         }
0175     }
0176 
0177     /* Null child means "get first node" */
0178 
0179     parent_node = start_node;
0180     child_node = acpi_ns_get_next_node(parent_node, NULL);
0181     child_type = ACPI_TYPE_ANY;
0182     level = 1;
0183 
0184     /*
0185      * Traverse the tree of nodes until we bubble back up to where we
0186      * started. When Level is zero, the loop is done because we have
0187      * bubbled up to (and passed) the original parent handle (start_entry)
0188      */
0189     while (level > 0 && child_node) {
0190         status = AE_OK;
0191 
0192         /* Found next child, get the type if we are not searching for ANY */
0193 
0194         if (type != ACPI_TYPE_ANY) {
0195             child_type = child_node->type;
0196         }
0197 
0198         /*
0199          * Ignore all temporary namespace nodes (created during control
0200          * method execution) unless told otherwise. These temporary nodes
0201          * can cause a race condition because they can be deleted during
0202          * the execution of the user function (if the namespace is
0203          * unlocked before invocation of the user function.) Only the
0204          * debugger namespace dump will examine the temporary nodes.
0205          */
0206         if ((child_node->flags & ANOBJ_TEMPORARY) &&
0207             !(flags & ACPI_NS_WALK_TEMP_NODES)) {
0208             status = AE_CTRL_DEPTH;
0209         }
0210 
0211         /* Type must match requested type */
0212 
0213         else if (child_type == type) {
0214             /*
0215              * Found a matching node, invoke the user callback function.
0216              * Unlock the namespace if flag is set.
0217              */
0218             if (flags & ACPI_NS_WALK_UNLOCK) {
0219                 mutex_status =
0220                     acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
0221                 if (ACPI_FAILURE(mutex_status)) {
0222                     return_ACPI_STATUS(mutex_status);
0223                 }
0224             }
0225 
0226             /*
0227              * Invoke the user function, either descending, ascending,
0228              * or both.
0229              */
0230             if (!node_previously_visited) {
0231                 if (descending_callback) {
0232                     status =
0233                         descending_callback(child_node,
0234                                 level, context,
0235                                 return_value);
0236                 }
0237             } else {
0238                 if (ascending_callback) {
0239                     status =
0240                         ascending_callback(child_node,
0241                                    level, context,
0242                                    return_value);
0243                 }
0244             }
0245 
0246             if (flags & ACPI_NS_WALK_UNLOCK) {
0247                 mutex_status =
0248                     acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
0249                 if (ACPI_FAILURE(mutex_status)) {
0250                     return_ACPI_STATUS(mutex_status);
0251                 }
0252             }
0253 
0254             switch (status) {
0255             case AE_OK:
0256             case AE_CTRL_DEPTH:
0257 
0258                 /* Just keep going */
0259                 break;
0260 
0261             case AE_CTRL_TERMINATE:
0262 
0263                 /* Exit now, with OK status */
0264 
0265                 return_ACPI_STATUS(AE_OK);
0266 
0267             default:
0268 
0269                 /* All others are valid exceptions */
0270 
0271                 return_ACPI_STATUS(status);
0272             }
0273         }
0274 
0275         /*
0276          * Depth first search: Attempt to go down another level in the
0277          * namespace if we are allowed to. Don't go any further if we have
0278          * reached the caller specified maximum depth or if the user
0279          * function has specified that the maximum depth has been reached.
0280          */
0281         if (!node_previously_visited &&
0282             (level < max_depth) && (status != AE_CTRL_DEPTH)) {
0283             if (child_node->child) {
0284 
0285                 /* There is at least one child of this node, visit it */
0286 
0287                 level++;
0288                 parent_node = child_node;
0289                 child_node =
0290                     acpi_ns_get_next_node(parent_node, NULL);
0291                 continue;
0292             }
0293         }
0294 
0295         /* No more children, re-visit this node */
0296 
0297         if (!node_previously_visited) {
0298             node_previously_visited = TRUE;
0299             continue;
0300         }
0301 
0302         /* No more children, visit peers */
0303 
0304         child_node = acpi_ns_get_next_node(parent_node, child_node);
0305         if (child_node) {
0306             node_previously_visited = FALSE;
0307         }
0308 
0309         /* No peers, re-visit parent */
0310 
0311         else {
0312             /*
0313              * No more children of this node (acpi_ns_get_next_node failed), go
0314              * back upwards in the namespace tree to the node's parent.
0315              */
0316             level--;
0317             child_node = parent_node;
0318             parent_node = parent_node->parent;
0319 
0320             node_previously_visited = TRUE;
0321         }
0322     }
0323 
0324     /* Complete walk, not terminated by user function */
0325 
0326     return_ACPI_STATUS(AE_OK);
0327 }