xref: /freebsd/sys/contrib/dev/acpica/components/parser/pstree.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1 /******************************************************************************
2  *
3  * Module Name: pstree - Parser op tree manipulation/traversal/search
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2012, Intel Corp.
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43 
44 
45 #define __PSTREE_C__
46 
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acparser.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
51 
52 #define _COMPONENT          ACPI_PARSER
53         ACPI_MODULE_NAME    ("pstree")
54 
55 /* Local prototypes */
56 
57 #ifdef ACPI_OBSOLETE_FUNCTIONS
58 ACPI_PARSE_OBJECT *
59 AcpiPsGetChild (
60     ACPI_PARSE_OBJECT       *op);
61 #endif
62 
63 
64 /*******************************************************************************
65  *
66  * FUNCTION:    AcpiPsGetArg
67  *
68  * PARAMETERS:  Op              - Get an argument for this op
69  *              Argn            - Nth argument to get
70  *
71  * RETURN:      The argument (as an Op object). NULL if argument does not exist
72  *
73  * DESCRIPTION: Get the specified op's argument.
74  *
75  ******************************************************************************/
76 
77 ACPI_PARSE_OBJECT *
78 AcpiPsGetArg (
79     ACPI_PARSE_OBJECT       *Op,
80     UINT32                  Argn)
81 {
82     ACPI_PARSE_OBJECT       *Arg = NULL;
83     const ACPI_OPCODE_INFO  *OpInfo;
84 
85 
86     ACPI_FUNCTION_ENTRY ();
87 
88 /*
89     if (Op->Common.AmlOpcode == AML_INT_CONNECTION_OP)
90     {
91         return (Op->Common.Value.Arg);
92     }
93 */
94     /* Get the info structure for this opcode */
95 
96     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
97     if (OpInfo->Class == AML_CLASS_UNKNOWN)
98     {
99         /* Invalid opcode or ASCII character */
100 
101         return (NULL);
102     }
103 
104     /* Check if this opcode requires argument sub-objects */
105 
106     if (!(OpInfo->Flags & AML_HAS_ARGS))
107     {
108         /* Has no linked argument objects */
109 
110         return (NULL);
111     }
112 
113     /* Get the requested argument object */
114 
115     Arg = Op->Common.Value.Arg;
116     while (Arg && Argn)
117     {
118         Argn--;
119         Arg = Arg->Common.Next;
120     }
121 
122     return (Arg);
123 }
124 
125 
126 /*******************************************************************************
127  *
128  * FUNCTION:    AcpiPsAppendArg
129  *
130  * PARAMETERS:  Op              - Append an argument to this Op.
131  *              Arg             - Argument Op to append
132  *
133  * RETURN:      None.
134  *
135  * DESCRIPTION: Append an argument to an op's argument list (a NULL arg is OK)
136  *
137  ******************************************************************************/
138 
139 void
140 AcpiPsAppendArg (
141     ACPI_PARSE_OBJECT       *Op,
142     ACPI_PARSE_OBJECT       *Arg)
143 {
144     ACPI_PARSE_OBJECT       *PrevArg;
145     const ACPI_OPCODE_INFO  *OpInfo;
146 
147 
148     ACPI_FUNCTION_ENTRY ();
149 
150 
151     if (!Op)
152     {
153         return;
154     }
155 
156     /* Get the info structure for this opcode */
157 
158     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
159     if (OpInfo->Class == AML_CLASS_UNKNOWN)
160     {
161         /* Invalid opcode */
162 
163         ACPI_ERROR ((AE_INFO, "Invalid AML Opcode: 0x%2.2X",
164             Op->Common.AmlOpcode));
165         return;
166     }
167 
168     /* Check if this opcode requires argument sub-objects */
169 
170     if (!(OpInfo->Flags & AML_HAS_ARGS))
171     {
172         /* Has no linked argument objects */
173 
174         return;
175     }
176 
177     /* Append the argument to the linked argument list */
178 
179     if (Op->Common.Value.Arg)
180     {
181         /* Append to existing argument list */
182 
183         PrevArg = Op->Common.Value.Arg;
184         while (PrevArg->Common.Next)
185         {
186             PrevArg = PrevArg->Common.Next;
187         }
188         PrevArg->Common.Next = Arg;
189     }
190     else
191     {
192         /* No argument list, this will be the first argument */
193 
194         Op->Common.Value.Arg = Arg;
195     }
196 
197     /* Set the parent in this arg and any args linked after it */
198 
199     while (Arg)
200     {
201         Arg->Common.Parent = Op;
202         Arg = Arg->Common.Next;
203 
204         Op->Common.ArgListLength++;
205     }
206 }
207 
208 
209 /*******************************************************************************
210  *
211  * FUNCTION:    AcpiPsGetDepthNext
212  *
213  * PARAMETERS:  Origin          - Root of subtree to search
214  *              Op              - Last (previous) Op that was found
215  *
216  * RETURN:      Next Op found in the search.
217  *
218  * DESCRIPTION: Get next op in tree (walking the tree in depth-first order)
219  *              Return NULL when reaching "origin" or when walking up from root
220  *
221  ******************************************************************************/
222 
223 ACPI_PARSE_OBJECT *
224 AcpiPsGetDepthNext (
225     ACPI_PARSE_OBJECT       *Origin,
226     ACPI_PARSE_OBJECT       *Op)
227 {
228     ACPI_PARSE_OBJECT       *Next = NULL;
229     ACPI_PARSE_OBJECT       *Parent;
230     ACPI_PARSE_OBJECT       *Arg;
231 
232 
233     ACPI_FUNCTION_ENTRY ();
234 
235 
236     if (!Op)
237     {
238         return (NULL);
239     }
240 
241     /* Look for an argument or child */
242 
243     Next = AcpiPsGetArg (Op, 0);
244     if (Next)
245     {
246         return (Next);
247     }
248 
249     /* Look for a sibling */
250 
251     Next = Op->Common.Next;
252     if (Next)
253     {
254         return (Next);
255     }
256 
257     /* Look for a sibling of parent */
258 
259     Parent = Op->Common.Parent;
260 
261     while (Parent)
262     {
263         Arg = AcpiPsGetArg (Parent, 0);
264         while (Arg && (Arg != Origin) && (Arg != Op))
265         {
266             Arg = Arg->Common.Next;
267         }
268 
269         if (Arg == Origin)
270         {
271             /* Reached parent of origin, end search */
272 
273             return (NULL);
274         }
275 
276         if (Parent->Common.Next)
277         {
278             /* Found sibling of parent */
279 
280             return (Parent->Common.Next);
281         }
282 
283         Op = Parent;
284         Parent = Parent->Common.Parent;
285     }
286 
287     return (Next);
288 }
289 
290 
291 #ifdef ACPI_OBSOLETE_FUNCTIONS
292 /*******************************************************************************
293  *
294  * FUNCTION:    AcpiPsGetChild
295  *
296  * PARAMETERS:  Op              - Get the child of this Op
297  *
298  * RETURN:      Child Op, Null if none is found.
299  *
300  * DESCRIPTION: Get op's children or NULL if none
301  *
302  ******************************************************************************/
303 
304 ACPI_PARSE_OBJECT *
305 AcpiPsGetChild (
306     ACPI_PARSE_OBJECT       *Op)
307 {
308     ACPI_PARSE_OBJECT       *Child = NULL;
309 
310 
311     ACPI_FUNCTION_ENTRY ();
312 
313 
314     switch (Op->Common.AmlOpcode)
315     {
316     case AML_SCOPE_OP:
317     case AML_ELSE_OP:
318     case AML_DEVICE_OP:
319     case AML_THERMAL_ZONE_OP:
320     case AML_INT_METHODCALL_OP:
321 
322         Child = AcpiPsGetArg (Op, 0);
323         break;
324 
325 
326     case AML_BUFFER_OP:
327     case AML_PACKAGE_OP:
328     case AML_METHOD_OP:
329     case AML_IF_OP:
330     case AML_WHILE_OP:
331     case AML_FIELD_OP:
332 
333         Child = AcpiPsGetArg (Op, 1);
334         break;
335 
336 
337     case AML_POWER_RES_OP:
338     case AML_INDEX_FIELD_OP:
339 
340         Child = AcpiPsGetArg (Op, 2);
341         break;
342 
343 
344     case AML_PROCESSOR_OP:
345     case AML_BANK_FIELD_OP:
346 
347         Child = AcpiPsGetArg (Op, 3);
348         break;
349 
350 
351     default:
352         /* All others have no children */
353         break;
354     }
355 
356     return (Child);
357 }
358 #endif
359 
360 
361