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