1 /*******************************************************************************
2  *
3  * Module Name: utstate - state object support procedures
4  *
5  ******************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2014, 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 #include "acpi.h"
45 #include "accommon.h"
46 
47 #define _COMPONENT          ACPI_UTILITIES
48         ACPI_MODULE_NAME    ("utstate")
49 
50 
51 /*******************************************************************************
52  *
53  * FUNCTION:    AcpiUtCreatePkgStateAndPush
54  *
55  * PARAMETERS:  Object          - Object to be added to the new state
56  *              Action          - Increment/Decrement
57  *              StateList       - List the state will be added to
58  *
59  * RETURN:      Status
60  *
61  * DESCRIPTION: Create a new state and push it
62  *
63  ******************************************************************************/
64 
65 ACPI_STATUS
66 AcpiUtCreatePkgStateAndPush (
67     void                    *InternalObject,
68     void                    *ExternalObject,
69     UINT16                  Index,
70     ACPI_GENERIC_STATE      **StateList)
71 {
72     ACPI_GENERIC_STATE       *State;
73 
74 
75     ACPI_FUNCTION_ENTRY ();
76 
77 
78     State = AcpiUtCreatePkgState (InternalObject, ExternalObject, Index);
79     if (!State)
80     {
81         return (AE_NO_MEMORY);
82     }
83 
84     AcpiUtPushGenericState (StateList, State);
85     return (AE_OK);
86 }
87 
88 
89 /*******************************************************************************
90  *
91  * FUNCTION:    AcpiUtPushGenericState
92  *
93  * PARAMETERS:  ListHead            - Head of the state stack
94  *              State               - State object to push
95  *
96  * RETURN:      None
97  *
98  * DESCRIPTION: Push a state object onto a state stack
99  *
100  ******************************************************************************/
101 
102 void
103 AcpiUtPushGenericState (
104     ACPI_GENERIC_STATE      **ListHead,
105     ACPI_GENERIC_STATE      *State)
106 {
107     ACPI_FUNCTION_ENTRY ();
108 
109 
110     /* Push the state object onto the front of the list (stack) */
111 
112     State->Common.Next = *ListHead;
113     *ListHead = State;
114     return;
115 }
116 
117 
118 /*******************************************************************************
119  *
120  * FUNCTION:    AcpiUtPopGenericState
121  *
122  * PARAMETERS:  ListHead            - Head of the state stack
123  *
124  * RETURN:      The popped state object
125  *
126  * DESCRIPTION: Pop a state object from a state stack
127  *
128  ******************************************************************************/
129 
130 ACPI_GENERIC_STATE *
131 AcpiUtPopGenericState (
132     ACPI_GENERIC_STATE      **ListHead)
133 {
134     ACPI_GENERIC_STATE      *State;
135 
136 
137     ACPI_FUNCTION_ENTRY ();
138 
139 
140     /* Remove the state object at the head of the list (stack) */
141 
142     State = *ListHead;
143     if (State)
144     {
145         /* Update the list head */
146 
147         *ListHead = State->Common.Next;
148     }
149 
150     return (State);
151 }
152 
153 
154 /*******************************************************************************
155  *
156  * FUNCTION:    AcpiUtCreateGenericState
157  *
158  * PARAMETERS:  None
159  *
160  * RETURN:      The new state object. NULL on failure.
161  *
162  * DESCRIPTION: Create a generic state object. Attempt to obtain one from
163  *              the global state cache;  If none available, create a new one.
164  *
165  ******************************************************************************/
166 
167 ACPI_GENERIC_STATE *
168 AcpiUtCreateGenericState (
169     void)
170 {
171     ACPI_GENERIC_STATE      *State;
172 
173 
174     ACPI_FUNCTION_ENTRY ();
175 
176 
177     State = AcpiOsAcquireObject (AcpiGbl_StateCache);
178     if (State)
179     {
180         /* Initialize */
181         State->Common.DescriptorType = ACPI_DESC_TYPE_STATE;
182     }
183 
184     return (State);
185 }
186 
187 
188 /*******************************************************************************
189  *
190  * FUNCTION:    AcpiUtCreateThreadState
191  *
192  * PARAMETERS:  None
193  *
194  * RETURN:      New Thread State. NULL on failure
195  *
196  * DESCRIPTION: Create a "Thread State" - a flavor of the generic state used
197  *              to track per-thread info during method execution
198  *
199  ******************************************************************************/
200 
201 ACPI_THREAD_STATE *
202 AcpiUtCreateThreadState (
203     void)
204 {
205     ACPI_GENERIC_STATE      *State;
206 
207 
208     ACPI_FUNCTION_ENTRY ();
209 
210 
211     /* Create the generic state object */
212 
213     State = AcpiUtCreateGenericState ();
214     if (!State)
215     {
216         return (NULL);
217     }
218 
219     /* Init fields specific to the update struct */
220 
221     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_THREAD;
222     State->Thread.ThreadId = AcpiOsGetThreadId ();
223 
224     /* Check for invalid thread ID - zero is very bad, it will break things */
225 
226     if (!State->Thread.ThreadId)
227     {
228         ACPI_ERROR ((AE_INFO, "Invalid zero ID from AcpiOsGetThreadId"));
229         State->Thread.ThreadId = (ACPI_THREAD_ID) 1;
230     }
231 
232     return ((ACPI_THREAD_STATE *) State);
233 }
234 
235 
236 /*******************************************************************************
237  *
238  * FUNCTION:    AcpiUtCreateUpdateState
239  *
240  * PARAMETERS:  Object          - Initial Object to be installed in the state
241  *              Action          - Update action to be performed
242  *
243  * RETURN:      New state object, null on failure
244  *
245  * DESCRIPTION: Create an "Update State" - a flavor of the generic state used
246  *              to update reference counts and delete complex objects such
247  *              as packages.
248  *
249  ******************************************************************************/
250 
251 ACPI_GENERIC_STATE *
252 AcpiUtCreateUpdateState (
253     ACPI_OPERAND_OBJECT     *Object,
254     UINT16                  Action)
255 {
256     ACPI_GENERIC_STATE      *State;
257 
258 
259     ACPI_FUNCTION_ENTRY ();
260 
261 
262     /* Create the generic state object */
263 
264     State = AcpiUtCreateGenericState ();
265     if (!State)
266     {
267         return (NULL);
268     }
269 
270     /* Init fields specific to the update struct */
271 
272     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_UPDATE;
273     State->Update.Object = Object;
274     State->Update.Value = Action;
275     return (State);
276 }
277 
278 
279 /*******************************************************************************
280  *
281  * FUNCTION:    AcpiUtCreatePkgState
282  *
283  * PARAMETERS:  Object          - Initial Object to be installed in the state
284  *              Action          - Update action to be performed
285  *
286  * RETURN:      New state object, null on failure
287  *
288  * DESCRIPTION: Create a "Package State"
289  *
290  ******************************************************************************/
291 
292 ACPI_GENERIC_STATE *
293 AcpiUtCreatePkgState (
294     void                    *InternalObject,
295     void                    *ExternalObject,
296     UINT16                  Index)
297 {
298     ACPI_GENERIC_STATE      *State;
299 
300 
301     ACPI_FUNCTION_ENTRY ();
302 
303 
304     /* Create the generic state object */
305 
306     State = AcpiUtCreateGenericState ();
307     if (!State)
308     {
309         return (NULL);
310     }
311 
312     /* Init fields specific to the update struct */
313 
314     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_PACKAGE;
315     State->Pkg.SourceObject = (ACPI_OPERAND_OBJECT *) InternalObject;
316     State->Pkg.DestObject = ExternalObject;
317     State->Pkg.Index= Index;
318     State->Pkg.NumPackages = 1;
319     return (State);
320 }
321 
322 
323 /*******************************************************************************
324  *
325  * FUNCTION:    AcpiUtCreateControlState
326  *
327  * PARAMETERS:  None
328  *
329  * RETURN:      New state object, null on failure
330  *
331  * DESCRIPTION: Create a "Control State" - a flavor of the generic state used
332  *              to support nested IF/WHILE constructs in the AML.
333  *
334  ******************************************************************************/
335 
336 ACPI_GENERIC_STATE *
337 AcpiUtCreateControlState (
338     void)
339 {
340     ACPI_GENERIC_STATE      *State;
341 
342 
343     ACPI_FUNCTION_ENTRY ();
344 
345 
346     /* Create the generic state object */
347 
348     State = AcpiUtCreateGenericState ();
349     if (!State)
350     {
351         return (NULL);
352     }
353 
354     /* Init fields specific to the control struct */
355 
356     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_CONTROL;
357     State->Common.State = ACPI_CONTROL_CONDITIONAL_EXECUTING;
358     return (State);
359 }
360 
361 
362 /*******************************************************************************
363  *
364  * FUNCTION:    AcpiUtDeleteGenericState
365  *
366  * PARAMETERS:  State               - The state object to be deleted
367  *
368  * RETURN:      None
369  *
370  * DESCRIPTION: Release a state object to the state cache. NULL state objects
371  *              are ignored.
372  *
373  ******************************************************************************/
374 
375 void
376 AcpiUtDeleteGenericState (
377     ACPI_GENERIC_STATE      *State)
378 {
379     ACPI_FUNCTION_ENTRY ();
380 
381 
382     /* Ignore null state */
383 
384     if (State)
385     {
386         (void) AcpiOsReleaseObject (AcpiGbl_StateCache, State);
387     }
388     return;
389 }
390