xref: /minix3/minix/drivers/power/acpi/parser/psloop.c (revision 29492bb71c7148a089a5afafa0c99409161218df)
1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * Module Name: psloop - Main AML parse loop
4433d6423SLionel Sambuc  *
5433d6423SLionel Sambuc  *****************************************************************************/
6433d6423SLionel Sambuc 
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek  * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc  * All rights reserved.
10433d6423SLionel Sambuc  *
11*29492bb7SDavid van Moolenbroek  * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek  * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek  * are met:
14*29492bb7SDavid van Moolenbroek  * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek  *    notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek  *    without modification.
17*29492bb7SDavid van Moolenbroek  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek  *    substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek  *    ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek  *    including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek  *    binary redistribution.
22*29492bb7SDavid van Moolenbroek  * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek  *    of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek  *    from this software without specific prior written permission.
25433d6423SLionel Sambuc  *
26*29492bb7SDavid van Moolenbroek  * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek  * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek  * Software Foundation.
29433d6423SLionel Sambuc  *
30*29492bb7SDavid van Moolenbroek  * NO WARRANTY
31*29492bb7SDavid van Moolenbroek  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek  * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek  */
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc /*
45433d6423SLionel Sambuc  * Parse the AML and build an operation tree as most interpreters, (such as
46433d6423SLionel Sambuc  * Perl) do. Parsing is done by hand rather than with a YACC generated parser
47433d6423SLionel Sambuc  * to tightly constrain stack and dynamic memory usage. Parsing is kept
48433d6423SLionel Sambuc  * flexible and the code fairly compact by parsing based on a list of AML
49433d6423SLionel Sambuc  * opcode templates in AmlOpInfo[].
50433d6423SLionel Sambuc  */
51433d6423SLionel Sambuc 
52433d6423SLionel Sambuc #include "acpi.h"
53433d6423SLionel Sambuc #include "accommon.h"
54433d6423SLionel Sambuc #include "acparser.h"
55433d6423SLionel Sambuc #include "acdispat.h"
56433d6423SLionel Sambuc #include "amlcode.h"
57433d6423SLionel Sambuc 
58433d6423SLionel Sambuc #define _COMPONENT          ACPI_PARSER
59433d6423SLionel Sambuc         ACPI_MODULE_NAME    ("psloop")
60433d6423SLionel Sambuc 
61433d6423SLionel Sambuc 
62433d6423SLionel Sambuc /* Local prototypes */
63433d6423SLionel Sambuc 
64433d6423SLionel Sambuc static ACPI_STATUS
65433d6423SLionel Sambuc AcpiPsGetArguments (
66433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState,
67433d6423SLionel Sambuc     UINT8                   *AmlOpStart,
68433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Op);
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc static void
71433d6423SLionel Sambuc AcpiPsLinkModuleCode (
72433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *ParentOp,
73433d6423SLionel Sambuc     UINT8                   *AmlStart,
74433d6423SLionel Sambuc     UINT32                  AmlLength,
75433d6423SLionel Sambuc     ACPI_OWNER_ID           OwnerId);
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc 
78433d6423SLionel Sambuc /*******************************************************************************
79433d6423SLionel Sambuc  *
80433d6423SLionel Sambuc  * FUNCTION:    AcpiPsGetArguments
81433d6423SLionel Sambuc  *
82433d6423SLionel Sambuc  * PARAMETERS:  WalkState           - Current state
83433d6423SLionel Sambuc  *              AmlOpStart          - Op start in AML
84433d6423SLionel Sambuc  *              Op                  - Current Op
85433d6423SLionel Sambuc  *
86433d6423SLionel Sambuc  * RETURN:      Status
87433d6423SLionel Sambuc  *
88433d6423SLionel Sambuc  * DESCRIPTION: Get arguments for passed Op.
89433d6423SLionel Sambuc  *
90433d6423SLionel Sambuc  ******************************************************************************/
91433d6423SLionel Sambuc 
92433d6423SLionel Sambuc static ACPI_STATUS
AcpiPsGetArguments(ACPI_WALK_STATE * WalkState,UINT8 * AmlOpStart,ACPI_PARSE_OBJECT * Op)93433d6423SLionel Sambuc AcpiPsGetArguments (
94433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState,
95433d6423SLionel Sambuc     UINT8                   *AmlOpStart,
96433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Op)
97433d6423SLionel Sambuc {
98433d6423SLionel Sambuc     ACPI_STATUS             Status = AE_OK;
99433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Arg = NULL;
100433d6423SLionel Sambuc     const ACPI_OPCODE_INFO  *OpInfo;
101433d6423SLionel Sambuc 
102433d6423SLionel Sambuc 
103433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_PTR (PsGetArguments, WalkState);
104433d6423SLionel Sambuc 
105433d6423SLionel Sambuc 
106433d6423SLionel Sambuc     switch (Op->Common.AmlOpcode)
107433d6423SLionel Sambuc     {
108433d6423SLionel Sambuc     case AML_BYTE_OP:       /* AML_BYTEDATA_ARG */
109433d6423SLionel Sambuc     case AML_WORD_OP:       /* AML_WORDDATA_ARG */
110433d6423SLionel Sambuc     case AML_DWORD_OP:      /* AML_DWORDATA_ARG */
111433d6423SLionel Sambuc     case AML_QWORD_OP:      /* AML_QWORDATA_ARG */
112433d6423SLionel Sambuc     case AML_STRING_OP:     /* AML_ASCIICHARLIST_ARG */
113433d6423SLionel Sambuc 
114433d6423SLionel Sambuc         /* Fill in constant or string argument directly */
115433d6423SLionel Sambuc 
116433d6423SLionel Sambuc         AcpiPsGetNextSimpleArg (&(WalkState->ParserState),
117433d6423SLionel Sambuc             GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), Op);
118433d6423SLionel Sambuc         break;
119433d6423SLionel Sambuc 
120433d6423SLionel Sambuc     case AML_INT_NAMEPATH_OP:   /* AML_NAMESTRING_ARG */
121433d6423SLionel Sambuc 
122433d6423SLionel Sambuc         Status = AcpiPsGetNextNamepath (WalkState, &(WalkState->ParserState), Op, 1);
123433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
124433d6423SLionel Sambuc         {
125433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
126433d6423SLionel Sambuc         }
127433d6423SLionel Sambuc 
128433d6423SLionel Sambuc         WalkState->ArgTypes = 0;
129433d6423SLionel Sambuc         break;
130433d6423SLionel Sambuc 
131433d6423SLionel Sambuc     default:
132433d6423SLionel Sambuc         /*
133433d6423SLionel Sambuc          * Op is not a constant or string, append each argument to the Op
134433d6423SLionel Sambuc          */
135433d6423SLionel Sambuc         while (GET_CURRENT_ARG_TYPE (WalkState->ArgTypes) && !WalkState->ArgCount)
136433d6423SLionel Sambuc         {
137433d6423SLionel Sambuc             WalkState->AmlOffset = (UINT32) ACPI_PTR_DIFF (WalkState->ParserState.Aml,
138433d6423SLionel Sambuc                 WalkState->ParserState.AmlStart);
139433d6423SLionel Sambuc 
140433d6423SLionel Sambuc             Status = AcpiPsGetNextArg (WalkState, &(WalkState->ParserState),
141433d6423SLionel Sambuc                         GET_CURRENT_ARG_TYPE (WalkState->ArgTypes), &Arg);
142433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
143433d6423SLionel Sambuc             {
144433d6423SLionel Sambuc                 return_ACPI_STATUS (Status);
145433d6423SLionel Sambuc             }
146433d6423SLionel Sambuc 
147433d6423SLionel Sambuc             if (Arg)
148433d6423SLionel Sambuc             {
149433d6423SLionel Sambuc                 Arg->Common.AmlOffset = WalkState->AmlOffset;
150433d6423SLionel Sambuc                 AcpiPsAppendArg (Op, Arg);
151433d6423SLionel Sambuc             }
152433d6423SLionel Sambuc 
153433d6423SLionel Sambuc             INCREMENT_ARG_LIST (WalkState->ArgTypes);
154433d6423SLionel Sambuc         }
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc         /*
158433d6423SLionel Sambuc          * Handle executable code at "module-level". This refers to
159433d6423SLionel Sambuc          * executable opcodes that appear outside of any control method.
160433d6423SLionel Sambuc          */
161433d6423SLionel Sambuc         if ((WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2) &&
162433d6423SLionel Sambuc             ((WalkState->ParseFlags & ACPI_PARSE_DISASSEMBLE) == 0))
163433d6423SLionel Sambuc         {
164433d6423SLionel Sambuc             /*
165433d6423SLionel Sambuc              * We want to skip If/Else/While constructs during Pass1 because we
166433d6423SLionel Sambuc              * want to actually conditionally execute the code during Pass2.
167433d6423SLionel Sambuc              *
168433d6423SLionel Sambuc              * Except for disassembly, where we always want to walk the
169433d6423SLionel Sambuc              * If/Else/While packages
170433d6423SLionel Sambuc              */
171433d6423SLionel Sambuc             switch (Op->Common.AmlOpcode)
172433d6423SLionel Sambuc             {
173433d6423SLionel Sambuc             case AML_IF_OP:
174433d6423SLionel Sambuc             case AML_ELSE_OP:
175433d6423SLionel Sambuc             case AML_WHILE_OP:
176433d6423SLionel Sambuc                 /*
177433d6423SLionel Sambuc                  * Currently supported module-level opcodes are:
178433d6423SLionel Sambuc                  * IF/ELSE/WHILE. These appear to be the most common,
179433d6423SLionel Sambuc                  * and easiest to support since they open an AML
180433d6423SLionel Sambuc                  * package.
181433d6423SLionel Sambuc                  */
182433d6423SLionel Sambuc                 if (WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1)
183433d6423SLionel Sambuc                 {
184433d6423SLionel Sambuc                     AcpiPsLinkModuleCode (Op->Common.Parent, AmlOpStart,
185433d6423SLionel Sambuc                         (UINT32) (WalkState->ParserState.PkgEnd - AmlOpStart),
186433d6423SLionel Sambuc                         WalkState->OwnerId);
187433d6423SLionel Sambuc                 }
188433d6423SLionel Sambuc 
189433d6423SLionel Sambuc                 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
190433d6423SLionel Sambuc                     "Pass1: Skipping an If/Else/While body\n"));
191433d6423SLionel Sambuc 
192433d6423SLionel Sambuc                 /* Skip body of if/else/while in pass 1 */
193433d6423SLionel Sambuc 
194433d6423SLionel Sambuc                 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
195433d6423SLionel Sambuc                 WalkState->ArgCount = 0;
196433d6423SLionel Sambuc                 break;
197433d6423SLionel Sambuc 
198433d6423SLionel Sambuc             default:
199433d6423SLionel Sambuc                 /*
200433d6423SLionel Sambuc                  * Check for an unsupported executable opcode at module
201433d6423SLionel Sambuc                  * level. We must be in PASS1, the parent must be a SCOPE,
202433d6423SLionel Sambuc                  * The opcode class must be EXECUTE, and the opcode must
203433d6423SLionel Sambuc                  * not be an argument to another opcode.
204433d6423SLionel Sambuc                  */
205433d6423SLionel Sambuc                 if ((WalkState->PassNumber == ACPI_IMODE_LOAD_PASS1) &&
206433d6423SLionel Sambuc                     (Op->Common.Parent->Common.AmlOpcode == AML_SCOPE_OP))
207433d6423SLionel Sambuc                 {
208433d6423SLionel Sambuc                     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
209433d6423SLionel Sambuc                     if ((OpInfo->Class == AML_CLASS_EXECUTE) &&
210433d6423SLionel Sambuc                         (!Arg))
211433d6423SLionel Sambuc                     {
212433d6423SLionel Sambuc                         ACPI_WARNING ((AE_INFO,
213*29492bb7SDavid van Moolenbroek                             "Unsupported module-level executable opcode "
214*29492bb7SDavid van Moolenbroek                             "0x%.2X at table offset 0x%.4X",
215433d6423SLionel Sambuc                             Op->Common.AmlOpcode,
216433d6423SLionel Sambuc                             (UINT32) (ACPI_PTR_DIFF (AmlOpStart,
217433d6423SLionel Sambuc                                 WalkState->ParserState.AmlStart) +
218433d6423SLionel Sambuc                                 sizeof (ACPI_TABLE_HEADER))));
219433d6423SLionel Sambuc                     }
220433d6423SLionel Sambuc                 }
221433d6423SLionel Sambuc                 break;
222433d6423SLionel Sambuc             }
223433d6423SLionel Sambuc         }
224433d6423SLionel Sambuc 
225433d6423SLionel Sambuc         /* Special processing for certain opcodes */
226433d6423SLionel Sambuc 
227433d6423SLionel Sambuc         switch (Op->Common.AmlOpcode)
228433d6423SLionel Sambuc         {
229433d6423SLionel Sambuc         case AML_METHOD_OP:
230433d6423SLionel Sambuc             /*
231433d6423SLionel Sambuc              * Skip parsing of control method because we don't have enough
232433d6423SLionel Sambuc              * info in the first pass to parse it correctly.
233433d6423SLionel Sambuc              *
234433d6423SLionel Sambuc              * Save the length and address of the body
235433d6423SLionel Sambuc              */
236433d6423SLionel Sambuc             Op->Named.Data = WalkState->ParserState.Aml;
237433d6423SLionel Sambuc             Op->Named.Length = (UINT32)
238433d6423SLionel Sambuc                 (WalkState->ParserState.PkgEnd - WalkState->ParserState.Aml);
239433d6423SLionel Sambuc 
240433d6423SLionel Sambuc             /* Skip body of method */
241433d6423SLionel Sambuc 
242433d6423SLionel Sambuc             WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
243433d6423SLionel Sambuc             WalkState->ArgCount = 0;
244433d6423SLionel Sambuc             break;
245433d6423SLionel Sambuc 
246433d6423SLionel Sambuc         case AML_BUFFER_OP:
247433d6423SLionel Sambuc         case AML_PACKAGE_OP:
248433d6423SLionel Sambuc         case AML_VAR_PACKAGE_OP:
249433d6423SLionel Sambuc 
250433d6423SLionel Sambuc             if ((Op->Common.Parent) &&
251433d6423SLionel Sambuc                 (Op->Common.Parent->Common.AmlOpcode == AML_NAME_OP) &&
252433d6423SLionel Sambuc                 (WalkState->PassNumber <= ACPI_IMODE_LOAD_PASS2))
253433d6423SLionel Sambuc             {
254433d6423SLionel Sambuc                 /*
255433d6423SLionel Sambuc                  * Skip parsing of Buffers and Packages because we don't have
256433d6423SLionel Sambuc                  * enough info in the first pass to parse them correctly.
257433d6423SLionel Sambuc                  */
258433d6423SLionel Sambuc                 Op->Named.Data = AmlOpStart;
259433d6423SLionel Sambuc                 Op->Named.Length = (UINT32)
260433d6423SLionel Sambuc                     (WalkState->ParserState.PkgEnd - AmlOpStart);
261433d6423SLionel Sambuc 
262433d6423SLionel Sambuc                 /* Skip body */
263433d6423SLionel Sambuc 
264433d6423SLionel Sambuc                 WalkState->ParserState.Aml = WalkState->ParserState.PkgEnd;
265433d6423SLionel Sambuc                 WalkState->ArgCount = 0;
266433d6423SLionel Sambuc             }
267433d6423SLionel Sambuc             break;
268433d6423SLionel Sambuc 
269433d6423SLionel Sambuc         case AML_WHILE_OP:
270433d6423SLionel Sambuc 
271433d6423SLionel Sambuc             if (WalkState->ControlState)
272433d6423SLionel Sambuc             {
273433d6423SLionel Sambuc                 WalkState->ControlState->Control.PackageEnd =
274433d6423SLionel Sambuc                     WalkState->ParserState.PkgEnd;
275433d6423SLionel Sambuc             }
276433d6423SLionel Sambuc             break;
277433d6423SLionel Sambuc 
278433d6423SLionel Sambuc         default:
279433d6423SLionel Sambuc 
280433d6423SLionel Sambuc             /* No action for all other opcodes */
281*29492bb7SDavid van Moolenbroek 
282433d6423SLionel Sambuc             break;
283433d6423SLionel Sambuc         }
284433d6423SLionel Sambuc 
285433d6423SLionel Sambuc         break;
286433d6423SLionel Sambuc     }
287433d6423SLionel Sambuc 
288433d6423SLionel Sambuc     return_ACPI_STATUS (AE_OK);
289433d6423SLionel Sambuc }
290433d6423SLionel Sambuc 
291433d6423SLionel Sambuc 
292433d6423SLionel Sambuc /*******************************************************************************
293433d6423SLionel Sambuc  *
294433d6423SLionel Sambuc  * FUNCTION:    AcpiPsLinkModuleCode
295433d6423SLionel Sambuc  *
296433d6423SLionel Sambuc  * PARAMETERS:  ParentOp            - Parent parser op
297433d6423SLionel Sambuc  *              AmlStart            - Pointer to the AML
298433d6423SLionel Sambuc  *              AmlLength           - Length of executable AML
299433d6423SLionel Sambuc  *              OwnerId             - OwnerId of module level code
300433d6423SLionel Sambuc  *
301433d6423SLionel Sambuc  * RETURN:      None.
302433d6423SLionel Sambuc  *
303433d6423SLionel Sambuc  * DESCRIPTION: Wrap the module-level code with a method object and link the
304433d6423SLionel Sambuc  *              object to the global list. Note, the mutex field of the method
305433d6423SLionel Sambuc  *              object is used to link multiple module-level code objects.
306433d6423SLionel Sambuc  *
307433d6423SLionel Sambuc  ******************************************************************************/
308433d6423SLionel Sambuc 
309433d6423SLionel Sambuc static void
AcpiPsLinkModuleCode(ACPI_PARSE_OBJECT * ParentOp,UINT8 * AmlStart,UINT32 AmlLength,ACPI_OWNER_ID OwnerId)310433d6423SLionel Sambuc AcpiPsLinkModuleCode (
311433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *ParentOp,
312433d6423SLionel Sambuc     UINT8                   *AmlStart,
313433d6423SLionel Sambuc     UINT32                  AmlLength,
314433d6423SLionel Sambuc     ACPI_OWNER_ID           OwnerId)
315433d6423SLionel Sambuc {
316433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *Prev;
317433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *Next;
318433d6423SLionel Sambuc     ACPI_OPERAND_OBJECT     *MethodObj;
319433d6423SLionel Sambuc     ACPI_NAMESPACE_NODE     *ParentNode;
320433d6423SLionel Sambuc 
321433d6423SLionel Sambuc 
322433d6423SLionel Sambuc     /* Get the tail of the list */
323433d6423SLionel Sambuc 
324433d6423SLionel Sambuc     Prev = Next = AcpiGbl_ModuleCodeList;
325433d6423SLionel Sambuc     while (Next)
326433d6423SLionel Sambuc     {
327433d6423SLionel Sambuc         Prev = Next;
328433d6423SLionel Sambuc         Next = Next->Method.Mutex;
329433d6423SLionel Sambuc     }
330433d6423SLionel Sambuc 
331433d6423SLionel Sambuc     /*
332433d6423SLionel Sambuc      * Insert the module level code into the list. Merge it if it is
333433d6423SLionel Sambuc      * adjacent to the previous element.
334433d6423SLionel Sambuc      */
335433d6423SLionel Sambuc     if (!Prev ||
336433d6423SLionel Sambuc        ((Prev->Method.AmlStart + Prev->Method.AmlLength) != AmlStart))
337433d6423SLionel Sambuc     {
338433d6423SLionel Sambuc         /* Create, initialize, and link a new temporary method object */
339433d6423SLionel Sambuc 
340433d6423SLionel Sambuc         MethodObj = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
341433d6423SLionel Sambuc         if (!MethodObj)
342433d6423SLionel Sambuc         {
343433d6423SLionel Sambuc             return;
344433d6423SLionel Sambuc         }
345433d6423SLionel Sambuc 
346433d6423SLionel Sambuc         if (ParentOp->Common.Node)
347433d6423SLionel Sambuc         {
348433d6423SLionel Sambuc             ParentNode = ParentOp->Common.Node;
349433d6423SLionel Sambuc         }
350433d6423SLionel Sambuc         else
351433d6423SLionel Sambuc         {
352433d6423SLionel Sambuc             ParentNode = AcpiGbl_RootNode;
353433d6423SLionel Sambuc         }
354433d6423SLionel Sambuc 
355433d6423SLionel Sambuc         MethodObj->Method.AmlStart = AmlStart;
356433d6423SLionel Sambuc         MethodObj->Method.AmlLength = AmlLength;
357433d6423SLionel Sambuc         MethodObj->Method.OwnerId = OwnerId;
358*29492bb7SDavid van Moolenbroek         MethodObj->Method.InfoFlags |= ACPI_METHOD_MODULE_LEVEL;
359433d6423SLionel Sambuc 
360433d6423SLionel Sambuc         /*
361433d6423SLionel Sambuc          * Save the parent node in NextObject. This is cheating, but we
362433d6423SLionel Sambuc          * don't want to expand the method object.
363433d6423SLionel Sambuc          */
364433d6423SLionel Sambuc         MethodObj->Method.NextObject =
365433d6423SLionel Sambuc             ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, ParentNode);
366433d6423SLionel Sambuc 
367433d6423SLionel Sambuc         if (!Prev)
368433d6423SLionel Sambuc         {
369433d6423SLionel Sambuc             AcpiGbl_ModuleCodeList = MethodObj;
370433d6423SLionel Sambuc         }
371433d6423SLionel Sambuc         else
372433d6423SLionel Sambuc         {
373433d6423SLionel Sambuc             Prev->Method.Mutex = MethodObj;
374433d6423SLionel Sambuc         }
375433d6423SLionel Sambuc     }
376433d6423SLionel Sambuc     else
377433d6423SLionel Sambuc     {
378433d6423SLionel Sambuc         Prev->Method.AmlLength += AmlLength;
379433d6423SLionel Sambuc     }
380433d6423SLionel Sambuc }
381433d6423SLionel Sambuc 
382433d6423SLionel Sambuc /*******************************************************************************
383433d6423SLionel Sambuc  *
384433d6423SLionel Sambuc  * FUNCTION:    AcpiPsParseLoop
385433d6423SLionel Sambuc  *
386433d6423SLionel Sambuc  * PARAMETERS:  WalkState           - Current state
387433d6423SLionel Sambuc  *
388433d6423SLionel Sambuc  * RETURN:      Status
389433d6423SLionel Sambuc  *
390433d6423SLionel Sambuc  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
391433d6423SLionel Sambuc  *              a tree of ops.
392433d6423SLionel Sambuc  *
393433d6423SLionel Sambuc  ******************************************************************************/
394433d6423SLionel Sambuc 
395433d6423SLionel Sambuc ACPI_STATUS
AcpiPsParseLoop(ACPI_WALK_STATE * WalkState)396433d6423SLionel Sambuc AcpiPsParseLoop (
397433d6423SLionel Sambuc     ACPI_WALK_STATE         *WalkState)
398433d6423SLionel Sambuc {
399433d6423SLionel Sambuc     ACPI_STATUS             Status = AE_OK;
400433d6423SLionel Sambuc     ACPI_PARSE_OBJECT       *Op = NULL;     /* current op */
401433d6423SLionel Sambuc     ACPI_PARSE_STATE        *ParserState;
402433d6423SLionel Sambuc     UINT8                   *AmlOpStart = NULL;
403433d6423SLionel Sambuc 
404433d6423SLionel Sambuc 
405433d6423SLionel Sambuc     ACPI_FUNCTION_TRACE_PTR (PsParseLoop, WalkState);
406433d6423SLionel Sambuc 
407433d6423SLionel Sambuc 
408433d6423SLionel Sambuc     if (WalkState->DescendingCallback == NULL)
409433d6423SLionel Sambuc     {
410433d6423SLionel Sambuc         return_ACPI_STATUS (AE_BAD_PARAMETER);
411433d6423SLionel Sambuc     }
412433d6423SLionel Sambuc 
413433d6423SLionel Sambuc     ParserState = &WalkState->ParserState;
414433d6423SLionel Sambuc     WalkState->ArgTypes = 0;
415433d6423SLionel Sambuc 
416433d6423SLionel Sambuc #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
417433d6423SLionel Sambuc 
418433d6423SLionel Sambuc     if (WalkState->WalkType & ACPI_WALK_METHOD_RESTART)
419433d6423SLionel Sambuc     {
420433d6423SLionel Sambuc         /* We are restarting a preempted control method */
421433d6423SLionel Sambuc 
422433d6423SLionel Sambuc         if (AcpiPsHasCompletedScope (ParserState))
423433d6423SLionel Sambuc         {
424433d6423SLionel Sambuc             /*
425433d6423SLionel Sambuc              * We must check if a predicate to an IF or WHILE statement
426433d6423SLionel Sambuc              * was just completed
427433d6423SLionel Sambuc              */
428433d6423SLionel Sambuc             if ((ParserState->Scope->ParseScope.Op) &&
429433d6423SLionel Sambuc                ((ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_IF_OP) ||
430433d6423SLionel Sambuc                 (ParserState->Scope->ParseScope.Op->Common.AmlOpcode == AML_WHILE_OP)) &&
431433d6423SLionel Sambuc                 (WalkState->ControlState) &&
432433d6423SLionel Sambuc                 (WalkState->ControlState->Common.State ==
433433d6423SLionel Sambuc                     ACPI_CONTROL_PREDICATE_EXECUTING))
434433d6423SLionel Sambuc             {
435433d6423SLionel Sambuc                 /*
436433d6423SLionel Sambuc                  * A predicate was just completed, get the value of the
437433d6423SLionel Sambuc                  * predicate and branch based on that value
438433d6423SLionel Sambuc                  */
439433d6423SLionel Sambuc                 WalkState->Op = NULL;
440433d6423SLionel Sambuc                 Status = AcpiDsGetPredicateValue (WalkState, ACPI_TO_POINTER (TRUE));
441433d6423SLionel Sambuc                 if (ACPI_FAILURE (Status) &&
442433d6423SLionel Sambuc                     ((Status & AE_CODE_MASK) != AE_CODE_CONTROL))
443433d6423SLionel Sambuc                 {
444433d6423SLionel Sambuc                     if (Status == AE_AML_NO_RETURN_VALUE)
445433d6423SLionel Sambuc                     {
446433d6423SLionel Sambuc                         ACPI_EXCEPTION ((AE_INFO, Status,
447433d6423SLionel Sambuc                             "Invoked method did not return a value"));
448433d6423SLionel Sambuc                     }
449433d6423SLionel Sambuc 
450433d6423SLionel Sambuc                     ACPI_EXCEPTION ((AE_INFO, Status, "GetPredicate Failed"));
451433d6423SLionel Sambuc                     return_ACPI_STATUS (Status);
452433d6423SLionel Sambuc                 }
453433d6423SLionel Sambuc 
454433d6423SLionel Sambuc                 Status = AcpiPsNextParseState (WalkState, Op, Status);
455433d6423SLionel Sambuc             }
456433d6423SLionel Sambuc 
457433d6423SLionel Sambuc             AcpiPsPopScope (ParserState, &Op,
458433d6423SLionel Sambuc                 &WalkState->ArgTypes, &WalkState->ArgCount);
459433d6423SLionel Sambuc             ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped scope, Op=%p\n", Op));
460433d6423SLionel Sambuc         }
461433d6423SLionel Sambuc         else if (WalkState->PrevOp)
462433d6423SLionel Sambuc         {
463433d6423SLionel Sambuc             /* We were in the middle of an op */
464433d6423SLionel Sambuc 
465433d6423SLionel Sambuc             Op = WalkState->PrevOp;
466433d6423SLionel Sambuc             WalkState->ArgTypes = WalkState->PrevArgTypes;
467433d6423SLionel Sambuc         }
468433d6423SLionel Sambuc     }
469433d6423SLionel Sambuc #endif
470433d6423SLionel Sambuc 
471433d6423SLionel Sambuc     /* Iterative parsing loop, while there is more AML to process: */
472433d6423SLionel Sambuc 
473433d6423SLionel Sambuc     while ((ParserState->Aml < ParserState->AmlEnd) || (Op))
474433d6423SLionel Sambuc     {
475433d6423SLionel Sambuc         AmlOpStart = ParserState->Aml;
476433d6423SLionel Sambuc         if (!Op)
477433d6423SLionel Sambuc         {
478433d6423SLionel Sambuc             Status = AcpiPsCreateOp (WalkState, AmlOpStart, &Op);
479433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
480433d6423SLionel Sambuc             {
481433d6423SLionel Sambuc                 if (Status == AE_CTRL_PARSE_CONTINUE)
482433d6423SLionel Sambuc                 {
483433d6423SLionel Sambuc                     continue;
484433d6423SLionel Sambuc                 }
485433d6423SLionel Sambuc 
486433d6423SLionel Sambuc                 if (Status == AE_CTRL_PARSE_PENDING)
487433d6423SLionel Sambuc                 {
488433d6423SLionel Sambuc                     Status = AE_OK;
489433d6423SLionel Sambuc                 }
490433d6423SLionel Sambuc 
491*29492bb7SDavid van Moolenbroek                 if (Status == AE_CTRL_TERMINATE)
492*29492bb7SDavid van Moolenbroek                 {
493*29492bb7SDavid van Moolenbroek                     return_ACPI_STATUS (Status);
494*29492bb7SDavid van Moolenbroek                 }
495*29492bb7SDavid van Moolenbroek 
496433d6423SLionel Sambuc                 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
497433d6423SLionel Sambuc                 if (ACPI_FAILURE (Status))
498433d6423SLionel Sambuc                 {
499433d6423SLionel Sambuc                     return_ACPI_STATUS (Status);
500433d6423SLionel Sambuc                 }
501433d6423SLionel Sambuc 
502433d6423SLionel Sambuc                 continue;
503433d6423SLionel Sambuc             }
504433d6423SLionel Sambuc 
505433d6423SLionel Sambuc             Op->Common.AmlOffset = WalkState->AmlOffset;
506433d6423SLionel Sambuc 
507433d6423SLionel Sambuc             if (WalkState->OpInfo)
508433d6423SLionel Sambuc             {
509433d6423SLionel Sambuc                 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
510433d6423SLionel Sambuc                     "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
511433d6423SLionel Sambuc                      (UINT32) Op->Common.AmlOpcode, WalkState->OpInfo->Name,
512433d6423SLionel Sambuc                      Op, ParserState->Aml, Op->Common.AmlOffset));
513433d6423SLionel Sambuc             }
514433d6423SLionel Sambuc         }
515433d6423SLionel Sambuc 
516433d6423SLionel Sambuc 
517433d6423SLionel Sambuc         /*
518433d6423SLionel Sambuc          * Start ArgCount at zero because we don't know if there are
519433d6423SLionel Sambuc          * any args yet
520433d6423SLionel Sambuc          */
521433d6423SLionel Sambuc         WalkState->ArgCount  = 0;
522433d6423SLionel Sambuc 
523433d6423SLionel Sambuc         /* Are there any arguments that must be processed? */
524433d6423SLionel Sambuc 
525433d6423SLionel Sambuc         if (WalkState->ArgTypes)
526433d6423SLionel Sambuc         {
527433d6423SLionel Sambuc             /* Get arguments */
528433d6423SLionel Sambuc 
529433d6423SLionel Sambuc             Status = AcpiPsGetArguments (WalkState, AmlOpStart, Op);
530433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
531433d6423SLionel Sambuc             {
532433d6423SLionel Sambuc                 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
533433d6423SLionel Sambuc                 if (ACPI_FAILURE (Status))
534433d6423SLionel Sambuc                 {
535433d6423SLionel Sambuc                     return_ACPI_STATUS (Status);
536433d6423SLionel Sambuc                 }
537433d6423SLionel Sambuc 
538433d6423SLionel Sambuc                 continue;
539433d6423SLionel Sambuc             }
540433d6423SLionel Sambuc         }
541433d6423SLionel Sambuc 
542433d6423SLionel Sambuc         /* Check for arguments that need to be processed */
543433d6423SLionel Sambuc 
544433d6423SLionel Sambuc         if (WalkState->ArgCount)
545433d6423SLionel Sambuc         {
546433d6423SLionel Sambuc             /*
547433d6423SLionel Sambuc              * There are arguments (complex ones), push Op and
548433d6423SLionel Sambuc              * prepare for argument
549433d6423SLionel Sambuc              */
550433d6423SLionel Sambuc             Status = AcpiPsPushScope (ParserState, Op,
551433d6423SLionel Sambuc                         WalkState->ArgTypes, WalkState->ArgCount);
552433d6423SLionel Sambuc             if (ACPI_FAILURE (Status))
553433d6423SLionel Sambuc             {
554433d6423SLionel Sambuc                 Status = AcpiPsCompleteOp (WalkState, &Op, Status);
555433d6423SLionel Sambuc                 if (ACPI_FAILURE (Status))
556433d6423SLionel Sambuc                 {
557433d6423SLionel Sambuc                     return_ACPI_STATUS (Status);
558433d6423SLionel Sambuc                 }
559433d6423SLionel Sambuc 
560433d6423SLionel Sambuc                 continue;
561433d6423SLionel Sambuc             }
562433d6423SLionel Sambuc 
563433d6423SLionel Sambuc             Op = NULL;
564433d6423SLionel Sambuc             continue;
565433d6423SLionel Sambuc         }
566433d6423SLionel Sambuc 
567433d6423SLionel Sambuc         /*
568433d6423SLionel Sambuc          * All arguments have been processed -- Op is complete,
569433d6423SLionel Sambuc          * prepare for next
570433d6423SLionel Sambuc          */
571433d6423SLionel Sambuc         WalkState->OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
572433d6423SLionel Sambuc         if (WalkState->OpInfo->Flags & AML_NAMED)
573433d6423SLionel Sambuc         {
574433d6423SLionel Sambuc             if (Op->Common.AmlOpcode == AML_REGION_OP ||
575433d6423SLionel Sambuc                 Op->Common.AmlOpcode == AML_DATA_REGION_OP)
576433d6423SLionel Sambuc             {
577433d6423SLionel Sambuc                 /*
578433d6423SLionel Sambuc                  * Skip parsing of control method or opregion body,
579433d6423SLionel Sambuc                  * because we don't have enough info in the first pass
580433d6423SLionel Sambuc                  * to parse them correctly.
581433d6423SLionel Sambuc                  *
582433d6423SLionel Sambuc                  * Completed parsing an OpRegion declaration, we now
583433d6423SLionel Sambuc                  * know the length.
584433d6423SLionel Sambuc                  */
585433d6423SLionel Sambuc                 Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
586433d6423SLionel Sambuc             }
587433d6423SLionel Sambuc         }
588433d6423SLionel Sambuc 
589433d6423SLionel Sambuc         if (WalkState->OpInfo->Flags & AML_CREATE)
590433d6423SLionel Sambuc         {
591433d6423SLionel Sambuc             /*
592433d6423SLionel Sambuc              * Backup to beginning of CreateXXXfield declaration (1 for
593433d6423SLionel Sambuc              * Opcode)
594433d6423SLionel Sambuc              *
595433d6423SLionel Sambuc              * BodyLength is unknown until we parse the body
596433d6423SLionel Sambuc              */
597433d6423SLionel Sambuc             Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
598433d6423SLionel Sambuc         }
599433d6423SLionel Sambuc 
600433d6423SLionel Sambuc         if (Op->Common.AmlOpcode == AML_BANK_FIELD_OP)
601433d6423SLionel Sambuc         {
602433d6423SLionel Sambuc             /*
603433d6423SLionel Sambuc              * Backup to beginning of BankField declaration
604433d6423SLionel Sambuc              *
605433d6423SLionel Sambuc              * BodyLength is unknown until we parse the body
606433d6423SLionel Sambuc              */
607433d6423SLionel Sambuc             Op->Named.Length = (UINT32) (ParserState->Aml - Op->Named.Data);
608433d6423SLionel Sambuc         }
609433d6423SLionel Sambuc 
610433d6423SLionel Sambuc         /* This op complete, notify the dispatcher */
611433d6423SLionel Sambuc 
612433d6423SLionel Sambuc         if (WalkState->AscendingCallback != NULL)
613433d6423SLionel Sambuc         {
614433d6423SLionel Sambuc             WalkState->Op = Op;
615433d6423SLionel Sambuc             WalkState->Opcode = Op->Common.AmlOpcode;
616433d6423SLionel Sambuc 
617433d6423SLionel Sambuc             Status = WalkState->AscendingCallback (WalkState);
618433d6423SLionel Sambuc             Status = AcpiPsNextParseState (WalkState, Op, Status);
619433d6423SLionel Sambuc             if (Status == AE_CTRL_PENDING)
620433d6423SLionel Sambuc             {
621433d6423SLionel Sambuc                 Status = AE_OK;
622433d6423SLionel Sambuc             }
623433d6423SLionel Sambuc         }
624433d6423SLionel Sambuc 
625433d6423SLionel Sambuc         Status = AcpiPsCompleteOp (WalkState, &Op, Status);
626433d6423SLionel Sambuc         if (ACPI_FAILURE (Status))
627433d6423SLionel Sambuc         {
628433d6423SLionel Sambuc             return_ACPI_STATUS (Status);
629433d6423SLionel Sambuc         }
630433d6423SLionel Sambuc 
631433d6423SLionel Sambuc     } /* while ParserState->Aml */
632433d6423SLionel Sambuc 
633433d6423SLionel Sambuc     Status = AcpiPsCompleteFinalOp (WalkState, Op, Status);
634433d6423SLionel Sambuc     return_ACPI_STATUS (Status);
635433d6423SLionel Sambuc }
636