1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Module Name: psutils - Parser miscellaneous utilities (Parser only)
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 #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "acparser.h"
47433d6423SLionel Sambuc #include "amlcode.h"
48433d6423SLionel Sambuc
49433d6423SLionel Sambuc #define _COMPONENT ACPI_PARSER
50433d6423SLionel Sambuc ACPI_MODULE_NAME ("psutils")
51433d6423SLionel Sambuc
52433d6423SLionel Sambuc
53433d6423SLionel Sambuc /*******************************************************************************
54433d6423SLionel Sambuc *
55433d6423SLionel Sambuc * FUNCTION: AcpiPsCreateScopeOp
56433d6423SLionel Sambuc *
57433d6423SLionel Sambuc * PARAMETERS: None
58433d6423SLionel Sambuc *
59433d6423SLionel Sambuc * RETURN: A new Scope object, null on failure
60433d6423SLionel Sambuc *
61433d6423SLionel Sambuc * DESCRIPTION: Create a Scope and associated namepath op with the root name
62433d6423SLionel Sambuc *
63433d6423SLionel Sambuc ******************************************************************************/
64433d6423SLionel Sambuc
65433d6423SLionel Sambuc ACPI_PARSE_OBJECT *
AcpiPsCreateScopeOp(void)66433d6423SLionel Sambuc AcpiPsCreateScopeOp (
67433d6423SLionel Sambuc void)
68433d6423SLionel Sambuc {
69433d6423SLionel Sambuc ACPI_PARSE_OBJECT *ScopeOp;
70433d6423SLionel Sambuc
71433d6423SLionel Sambuc
72433d6423SLionel Sambuc ScopeOp = AcpiPsAllocOp (AML_SCOPE_OP);
73433d6423SLionel Sambuc if (!ScopeOp)
74433d6423SLionel Sambuc {
75433d6423SLionel Sambuc return (NULL);
76433d6423SLionel Sambuc }
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc ScopeOp->Named.Name = ACPI_ROOT_NAME;
79433d6423SLionel Sambuc return (ScopeOp);
80433d6423SLionel Sambuc }
81433d6423SLionel Sambuc
82433d6423SLionel Sambuc
83433d6423SLionel Sambuc /*******************************************************************************
84433d6423SLionel Sambuc *
85433d6423SLionel Sambuc * FUNCTION: AcpiPsInitOp
86433d6423SLionel Sambuc *
87433d6423SLionel Sambuc * PARAMETERS: Op - A newly allocated Op object
88433d6423SLionel Sambuc * Opcode - Opcode to store in the Op
89433d6423SLionel Sambuc *
90433d6423SLionel Sambuc * RETURN: None
91433d6423SLionel Sambuc *
92433d6423SLionel Sambuc * DESCRIPTION: Initialize a parse (Op) object
93433d6423SLionel Sambuc *
94433d6423SLionel Sambuc ******************************************************************************/
95433d6423SLionel Sambuc
96433d6423SLionel Sambuc void
AcpiPsInitOp(ACPI_PARSE_OBJECT * Op,UINT16 Opcode)97433d6423SLionel Sambuc AcpiPsInitOp (
98433d6423SLionel Sambuc ACPI_PARSE_OBJECT *Op,
99433d6423SLionel Sambuc UINT16 Opcode)
100433d6423SLionel Sambuc {
101433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
102433d6423SLionel Sambuc
103433d6423SLionel Sambuc
104433d6423SLionel Sambuc Op->Common.DescriptorType = ACPI_DESC_TYPE_PARSER;
105433d6423SLionel Sambuc Op->Common.AmlOpcode = Opcode;
106433d6423SLionel Sambuc
107433d6423SLionel Sambuc ACPI_DISASM_ONLY_MEMBERS (ACPI_STRNCPY (Op->Common.AmlOpName,
108433d6423SLionel Sambuc (AcpiPsGetOpcodeInfo (Opcode))->Name,
109433d6423SLionel Sambuc sizeof (Op->Common.AmlOpName)));
110433d6423SLionel Sambuc }
111433d6423SLionel Sambuc
112433d6423SLionel Sambuc
113433d6423SLionel Sambuc /*******************************************************************************
114433d6423SLionel Sambuc *
115433d6423SLionel Sambuc * FUNCTION: AcpiPsAllocOp
116433d6423SLionel Sambuc *
117433d6423SLionel Sambuc * PARAMETERS: Opcode - Opcode that will be stored in the new Op
118433d6423SLionel Sambuc *
119433d6423SLionel Sambuc * RETURN: Pointer to the new Op, null on failure
120433d6423SLionel Sambuc *
121433d6423SLionel Sambuc * DESCRIPTION: Allocate an acpi_op, choose op type (and thus size) based on
122433d6423SLionel Sambuc * opcode. A cache of opcodes is available for the pure
123433d6423SLionel Sambuc * GENERIC_OP, since this is by far the most commonly used.
124433d6423SLionel Sambuc *
125433d6423SLionel Sambuc ******************************************************************************/
126433d6423SLionel Sambuc
127433d6423SLionel Sambuc ACPI_PARSE_OBJECT*
AcpiPsAllocOp(UINT16 Opcode)128433d6423SLionel Sambuc AcpiPsAllocOp (
129433d6423SLionel Sambuc UINT16 Opcode)
130433d6423SLionel Sambuc {
131433d6423SLionel Sambuc ACPI_PARSE_OBJECT *Op;
132433d6423SLionel Sambuc const ACPI_OPCODE_INFO *OpInfo;
133433d6423SLionel Sambuc UINT8 Flags = ACPI_PARSEOP_GENERIC;
134433d6423SLionel Sambuc
135433d6423SLionel Sambuc
136433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
137433d6423SLionel Sambuc
138433d6423SLionel Sambuc
139433d6423SLionel Sambuc OpInfo = AcpiPsGetOpcodeInfo (Opcode);
140433d6423SLionel Sambuc
141433d6423SLionel Sambuc /* Determine type of ParseOp required */
142433d6423SLionel Sambuc
143433d6423SLionel Sambuc if (OpInfo->Flags & AML_DEFER)
144433d6423SLionel Sambuc {
145433d6423SLionel Sambuc Flags = ACPI_PARSEOP_DEFERRED;
146433d6423SLionel Sambuc }
147433d6423SLionel Sambuc else if (OpInfo->Flags & AML_NAMED)
148433d6423SLionel Sambuc {
149433d6423SLionel Sambuc Flags = ACPI_PARSEOP_NAMED;
150433d6423SLionel Sambuc }
151433d6423SLionel Sambuc else if (Opcode == AML_INT_BYTELIST_OP)
152433d6423SLionel Sambuc {
153433d6423SLionel Sambuc Flags = ACPI_PARSEOP_BYTELIST;
154433d6423SLionel Sambuc }
155433d6423SLionel Sambuc
156433d6423SLionel Sambuc /* Allocate the minimum required size object */
157433d6423SLionel Sambuc
158433d6423SLionel Sambuc if (Flags == ACPI_PARSEOP_GENERIC)
159433d6423SLionel Sambuc {
160433d6423SLionel Sambuc /* The generic op (default) is by far the most common (16 to 1) */
161433d6423SLionel Sambuc
162433d6423SLionel Sambuc Op = AcpiOsAcquireObject (AcpiGbl_PsNodeCache);
163433d6423SLionel Sambuc }
164433d6423SLionel Sambuc else
165433d6423SLionel Sambuc {
166433d6423SLionel Sambuc /* Extended parseop */
167433d6423SLionel Sambuc
168433d6423SLionel Sambuc Op = AcpiOsAcquireObject (AcpiGbl_PsNodeExtCache);
169433d6423SLionel Sambuc }
170433d6423SLionel Sambuc
171433d6423SLionel Sambuc /* Initialize the Op */
172433d6423SLionel Sambuc
173433d6423SLionel Sambuc if (Op)
174433d6423SLionel Sambuc {
175433d6423SLionel Sambuc AcpiPsInitOp (Op, Opcode);
176433d6423SLionel Sambuc Op->Common.Flags = Flags;
177433d6423SLionel Sambuc }
178433d6423SLionel Sambuc
179433d6423SLionel Sambuc return (Op);
180433d6423SLionel Sambuc }
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc
183433d6423SLionel Sambuc /*******************************************************************************
184433d6423SLionel Sambuc *
185433d6423SLionel Sambuc * FUNCTION: AcpiPsFreeOp
186433d6423SLionel Sambuc *
187433d6423SLionel Sambuc * PARAMETERS: Op - Op to be freed
188433d6423SLionel Sambuc *
189433d6423SLionel Sambuc * RETURN: None.
190433d6423SLionel Sambuc *
191433d6423SLionel Sambuc * DESCRIPTION: Free an Op object. Either put it on the GENERIC_OP cache list
192433d6423SLionel Sambuc * or actually free it.
193433d6423SLionel Sambuc *
194433d6423SLionel Sambuc ******************************************************************************/
195433d6423SLionel Sambuc
196433d6423SLionel Sambuc void
AcpiPsFreeOp(ACPI_PARSE_OBJECT * Op)197433d6423SLionel Sambuc AcpiPsFreeOp (
198433d6423SLionel Sambuc ACPI_PARSE_OBJECT *Op)
199433d6423SLionel Sambuc {
200433d6423SLionel Sambuc ACPI_FUNCTION_NAME (PsFreeOp);
201433d6423SLionel Sambuc
202433d6423SLionel Sambuc
203433d6423SLionel Sambuc if (Op->Common.AmlOpcode == AML_INT_RETURN_VALUE_OP)
204433d6423SLionel Sambuc {
205433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Free retval op: %p\n", Op));
206433d6423SLionel Sambuc }
207433d6423SLionel Sambuc
208433d6423SLionel Sambuc if (Op->Common.Flags & ACPI_PARSEOP_GENERIC)
209433d6423SLionel Sambuc {
210433d6423SLionel Sambuc (void) AcpiOsReleaseObject (AcpiGbl_PsNodeCache, Op);
211433d6423SLionel Sambuc }
212433d6423SLionel Sambuc else
213433d6423SLionel Sambuc {
214433d6423SLionel Sambuc (void) AcpiOsReleaseObject (AcpiGbl_PsNodeExtCache, Op);
215433d6423SLionel Sambuc }
216433d6423SLionel Sambuc }
217433d6423SLionel Sambuc
218433d6423SLionel Sambuc
219433d6423SLionel Sambuc /*******************************************************************************
220433d6423SLionel Sambuc *
221433d6423SLionel Sambuc * FUNCTION: Utility functions
222433d6423SLionel Sambuc *
223433d6423SLionel Sambuc * DESCRIPTION: Low level character and object functions
224433d6423SLionel Sambuc *
225433d6423SLionel Sambuc ******************************************************************************/
226433d6423SLionel Sambuc
227433d6423SLionel Sambuc
228433d6423SLionel Sambuc /*
229433d6423SLionel Sambuc * Is "c" a namestring lead character?
230433d6423SLionel Sambuc */
231433d6423SLionel Sambuc BOOLEAN
AcpiPsIsLeadingChar(UINT32 c)232433d6423SLionel Sambuc AcpiPsIsLeadingChar (
233433d6423SLionel Sambuc UINT32 c)
234433d6423SLionel Sambuc {
235433d6423SLionel Sambuc return ((BOOLEAN) (c == '_' || (c >= 'A' && c <= 'Z')));
236433d6423SLionel Sambuc }
237433d6423SLionel Sambuc
238433d6423SLionel Sambuc
239433d6423SLionel Sambuc /*
240433d6423SLionel Sambuc * Get op's name (4-byte name segment) or 0 if unnamed
241433d6423SLionel Sambuc */
242433d6423SLionel Sambuc UINT32
AcpiPsGetName(ACPI_PARSE_OBJECT * Op)243433d6423SLionel Sambuc AcpiPsGetName (
244433d6423SLionel Sambuc ACPI_PARSE_OBJECT *Op)
245433d6423SLionel Sambuc {
246433d6423SLionel Sambuc
247433d6423SLionel Sambuc /* The "generic" object has no name associated with it */
248433d6423SLionel Sambuc
249433d6423SLionel Sambuc if (Op->Common.Flags & ACPI_PARSEOP_GENERIC)
250433d6423SLionel Sambuc {
251433d6423SLionel Sambuc return (0);
252433d6423SLionel Sambuc }
253433d6423SLionel Sambuc
254433d6423SLionel Sambuc /* Only the "Extended" parse objects have a name */
255433d6423SLionel Sambuc
256433d6423SLionel Sambuc return (Op->Named.Name);
257433d6423SLionel Sambuc }
258433d6423SLionel Sambuc
259433d6423SLionel Sambuc
260433d6423SLionel Sambuc /*
261433d6423SLionel Sambuc * Set op's name
262433d6423SLionel Sambuc */
263433d6423SLionel Sambuc void
AcpiPsSetName(ACPI_PARSE_OBJECT * Op,UINT32 name)264433d6423SLionel Sambuc AcpiPsSetName (
265433d6423SLionel Sambuc ACPI_PARSE_OBJECT *Op,
266433d6423SLionel Sambuc UINT32 name)
267433d6423SLionel Sambuc {
268433d6423SLionel Sambuc
269433d6423SLionel Sambuc /* The "generic" object has no name associated with it */
270433d6423SLionel Sambuc
271433d6423SLionel Sambuc if (Op->Common.Flags & ACPI_PARSEOP_GENERIC)
272433d6423SLionel Sambuc {
273433d6423SLionel Sambuc return;
274433d6423SLionel Sambuc }
275433d6423SLionel Sambuc
276433d6423SLionel Sambuc Op->Named.Name = name;
277433d6423SLionel Sambuc }
278