xref: /netbsd-src/sys/external/bsd/acpica/dist/utilities/utstate.c (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
128c506b8Sjruoho /*******************************************************************************
228c506b8Sjruoho  *
328c506b8Sjruoho  * Module Name: utstate - state object support procedures
428c506b8Sjruoho  *
528c506b8Sjruoho  ******************************************************************************/
628c506b8Sjruoho 
7124f4c82Sjruoho /*
8*046a2985Schristos  * Copyright (C) 2000 - 2023, Intel Corp.
928c506b8Sjruoho  * All rights reserved.
1028c506b8Sjruoho  *
11124f4c82Sjruoho  * Redistribution and use in source and binary forms, with or without
12124f4c82Sjruoho  * modification, are permitted provided that the following conditions
13124f4c82Sjruoho  * are met:
14124f4c82Sjruoho  * 1. Redistributions of source code must retain the above copyright
15124f4c82Sjruoho  *    notice, this list of conditions, and the following disclaimer,
16124f4c82Sjruoho  *    without modification.
17124f4c82Sjruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18124f4c82Sjruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
19124f4c82Sjruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
20124f4c82Sjruoho  *    including a substantially similar Disclaimer requirement for further
21124f4c82Sjruoho  *    binary redistribution.
22124f4c82Sjruoho  * 3. Neither the names of the above-listed copyright holders nor the names
23124f4c82Sjruoho  *    of any contributors may be used to endorse or promote products derived
24124f4c82Sjruoho  *    from this software without specific prior written permission.
2528c506b8Sjruoho  *
26124f4c82Sjruoho  * Alternatively, this software may be distributed under the terms of the
27124f4c82Sjruoho  * GNU General Public License ("GPL") version 2 as published by the Free
28124f4c82Sjruoho  * Software Foundation.
2928c506b8Sjruoho  *
30124f4c82Sjruoho  * NO WARRANTY
31124f4c82Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32124f4c82Sjruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3346a330b4Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34124f4c82Sjruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35124f4c82Sjruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36124f4c82Sjruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37124f4c82Sjruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38124f4c82Sjruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39124f4c82Sjruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40124f4c82Sjruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41124f4c82Sjruoho  * POSSIBILITY OF SUCH DAMAGES.
42124f4c82Sjruoho  */
4328c506b8Sjruoho 
4428c506b8Sjruoho #include "acpi.h"
4528c506b8Sjruoho #include "accommon.h"
4628c506b8Sjruoho 
4728c506b8Sjruoho #define _COMPONENT          ACPI_UTILITIES
4828c506b8Sjruoho         ACPI_MODULE_NAME    ("utstate")
4928c506b8Sjruoho 
5028c506b8Sjruoho 
5128c506b8Sjruoho /*******************************************************************************
5228c506b8Sjruoho  *
5328c506b8Sjruoho  * FUNCTION:    AcpiUtPushGenericState
5428c506b8Sjruoho  *
5528c506b8Sjruoho  * PARAMETERS:  ListHead            - Head of the state stack
5628c506b8Sjruoho  *              State               - State object to push
5728c506b8Sjruoho  *
5828c506b8Sjruoho  * RETURN:      None
5928c506b8Sjruoho  *
6028c506b8Sjruoho  * DESCRIPTION: Push a state object onto a state stack
6128c506b8Sjruoho  *
6228c506b8Sjruoho  ******************************************************************************/
6328c506b8Sjruoho 
6428c506b8Sjruoho void
AcpiUtPushGenericState(ACPI_GENERIC_STATE ** ListHead,ACPI_GENERIC_STATE * State)6528c506b8Sjruoho AcpiUtPushGenericState (
6628c506b8Sjruoho     ACPI_GENERIC_STATE      **ListHead,
6728c506b8Sjruoho     ACPI_GENERIC_STATE      *State)
6828c506b8Sjruoho {
69ff4a156dSchristos     ACPI_FUNCTION_ENTRY ();
7028c506b8Sjruoho 
7128c506b8Sjruoho 
7228c506b8Sjruoho     /* Push the state object onto the front of the list (stack) */
7328c506b8Sjruoho 
7428c506b8Sjruoho     State->Common.Next = *ListHead;
7528c506b8Sjruoho     *ListHead = State;
76ff4a156dSchristos     return;
7728c506b8Sjruoho }
7828c506b8Sjruoho 
7928c506b8Sjruoho 
8028c506b8Sjruoho /*******************************************************************************
8128c506b8Sjruoho  *
8228c506b8Sjruoho  * FUNCTION:    AcpiUtPopGenericState
8328c506b8Sjruoho  *
8428c506b8Sjruoho  * PARAMETERS:  ListHead            - Head of the state stack
8528c506b8Sjruoho  *
8628c506b8Sjruoho  * RETURN:      The popped state object
8728c506b8Sjruoho  *
8828c506b8Sjruoho  * DESCRIPTION: Pop a state object from a state stack
8928c506b8Sjruoho  *
9028c506b8Sjruoho  ******************************************************************************/
9128c506b8Sjruoho 
9228c506b8Sjruoho ACPI_GENERIC_STATE *
AcpiUtPopGenericState(ACPI_GENERIC_STATE ** ListHead)9328c506b8Sjruoho AcpiUtPopGenericState (
9428c506b8Sjruoho     ACPI_GENERIC_STATE      **ListHead)
9528c506b8Sjruoho {
9628c506b8Sjruoho     ACPI_GENERIC_STATE      *State;
9728c506b8Sjruoho 
9828c506b8Sjruoho 
99ff4a156dSchristos     ACPI_FUNCTION_ENTRY ();
10028c506b8Sjruoho 
10128c506b8Sjruoho 
10228c506b8Sjruoho     /* Remove the state object at the head of the list (stack) */
10328c506b8Sjruoho 
10428c506b8Sjruoho     State = *ListHead;
10528c506b8Sjruoho     if (State)
10628c506b8Sjruoho     {
10728c506b8Sjruoho         /* Update the list head */
10828c506b8Sjruoho 
10928c506b8Sjruoho         *ListHead = State->Common.Next;
11028c506b8Sjruoho     }
11128c506b8Sjruoho 
112ff4a156dSchristos     return (State);
11328c506b8Sjruoho }
11428c506b8Sjruoho 
11528c506b8Sjruoho 
11628c506b8Sjruoho /*******************************************************************************
11728c506b8Sjruoho  *
11828c506b8Sjruoho  * FUNCTION:    AcpiUtCreateGenericState
11928c506b8Sjruoho  *
12028c506b8Sjruoho  * PARAMETERS:  None
12128c506b8Sjruoho  *
12228c506b8Sjruoho  * RETURN:      The new state object. NULL on failure.
12328c506b8Sjruoho  *
12428c506b8Sjruoho  * DESCRIPTION: Create a generic state object. Attempt to obtain one from
12528c506b8Sjruoho  *              the global state cache;  If none available, create a new one.
12628c506b8Sjruoho  *
12728c506b8Sjruoho  ******************************************************************************/
12828c506b8Sjruoho 
12928c506b8Sjruoho ACPI_GENERIC_STATE *
AcpiUtCreateGenericState(void)13028c506b8Sjruoho AcpiUtCreateGenericState (
13128c506b8Sjruoho     void)
13228c506b8Sjruoho {
13328c506b8Sjruoho     ACPI_GENERIC_STATE      *State;
13428c506b8Sjruoho 
13528c506b8Sjruoho 
13628c506b8Sjruoho     ACPI_FUNCTION_ENTRY ();
13728c506b8Sjruoho 
13828c506b8Sjruoho 
13928c506b8Sjruoho     State = AcpiOsAcquireObject (AcpiGbl_StateCache);
14028c506b8Sjruoho     if (State)
14128c506b8Sjruoho     {
14228c506b8Sjruoho         /* Initialize */
14328c506b8Sjruoho         State->Common.DescriptorType = ACPI_DESC_TYPE_STATE;
14428c506b8Sjruoho     }
14528c506b8Sjruoho 
14628c506b8Sjruoho     return (State);
14728c506b8Sjruoho }
14828c506b8Sjruoho 
14928c506b8Sjruoho 
15028c506b8Sjruoho /*******************************************************************************
15128c506b8Sjruoho  *
15228c506b8Sjruoho  * FUNCTION:    AcpiUtCreateThreadState
15328c506b8Sjruoho  *
15428c506b8Sjruoho  * PARAMETERS:  None
15528c506b8Sjruoho  *
15628c506b8Sjruoho  * RETURN:      New Thread State. NULL on failure
15728c506b8Sjruoho  *
15828c506b8Sjruoho  * DESCRIPTION: Create a "Thread State" - a flavor of the generic state used
15928c506b8Sjruoho  *              to track per-thread info during method execution
16028c506b8Sjruoho  *
16128c506b8Sjruoho  ******************************************************************************/
16228c506b8Sjruoho 
16328c506b8Sjruoho ACPI_THREAD_STATE *
AcpiUtCreateThreadState(void)16428c506b8Sjruoho AcpiUtCreateThreadState (
16528c506b8Sjruoho     void)
16628c506b8Sjruoho {
16728c506b8Sjruoho     ACPI_GENERIC_STATE      *State;
16828c506b8Sjruoho 
16928c506b8Sjruoho 
170ff4a156dSchristos     ACPI_FUNCTION_ENTRY ();
17128c506b8Sjruoho 
17228c506b8Sjruoho 
17328c506b8Sjruoho     /* Create the generic state object */
17428c506b8Sjruoho 
17528c506b8Sjruoho     State = AcpiUtCreateGenericState ();
17628c506b8Sjruoho     if (!State)
17728c506b8Sjruoho     {
178ff4a156dSchristos         return (NULL);
17928c506b8Sjruoho     }
18028c506b8Sjruoho 
18128c506b8Sjruoho     /* Init fields specific to the update struct */
18228c506b8Sjruoho 
18328c506b8Sjruoho     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_THREAD;
18428c506b8Sjruoho     State->Thread.ThreadId = AcpiOsGetThreadId ();
18528c506b8Sjruoho 
18628c506b8Sjruoho     /* Check for invalid thread ID - zero is very bad, it will break things */
18728c506b8Sjruoho 
18828c506b8Sjruoho     if (!State->Thread.ThreadId)
18928c506b8Sjruoho     {
19028c506b8Sjruoho         ACPI_ERROR ((AE_INFO, "Invalid zero ID from AcpiOsGetThreadId"));
19128c506b8Sjruoho         State->Thread.ThreadId = (ACPI_THREAD_ID) 1;
19228c506b8Sjruoho     }
19328c506b8Sjruoho 
194ff4a156dSchristos     return ((ACPI_THREAD_STATE *) State);
19528c506b8Sjruoho }
19628c506b8Sjruoho 
19728c506b8Sjruoho 
19828c506b8Sjruoho /*******************************************************************************
19928c506b8Sjruoho  *
20028c506b8Sjruoho  * FUNCTION:    AcpiUtCreateUpdateState
20128c506b8Sjruoho  *
20228c506b8Sjruoho  * PARAMETERS:  Object          - Initial Object to be installed in the state
20328c506b8Sjruoho  *              Action          - Update action to be performed
20428c506b8Sjruoho  *
20528c506b8Sjruoho  * RETURN:      New state object, null on failure
20628c506b8Sjruoho  *
20728c506b8Sjruoho  * DESCRIPTION: Create an "Update State" - a flavor of the generic state used
20828c506b8Sjruoho  *              to update reference counts and delete complex objects such
20928c506b8Sjruoho  *              as packages.
21028c506b8Sjruoho  *
21128c506b8Sjruoho  ******************************************************************************/
21228c506b8Sjruoho 
21328c506b8Sjruoho ACPI_GENERIC_STATE *
AcpiUtCreateUpdateState(ACPI_OPERAND_OBJECT * Object,UINT16 Action)21428c506b8Sjruoho AcpiUtCreateUpdateState (
21528c506b8Sjruoho     ACPI_OPERAND_OBJECT     *Object,
21628c506b8Sjruoho     UINT16                  Action)
21728c506b8Sjruoho {
21828c506b8Sjruoho     ACPI_GENERIC_STATE      *State;
21928c506b8Sjruoho 
22028c506b8Sjruoho 
221ff4a156dSchristos     ACPI_FUNCTION_ENTRY ();
22228c506b8Sjruoho 
22328c506b8Sjruoho 
22428c506b8Sjruoho     /* Create the generic state object */
22528c506b8Sjruoho 
22628c506b8Sjruoho     State = AcpiUtCreateGenericState ();
22728c506b8Sjruoho     if (!State)
22828c506b8Sjruoho     {
229ff4a156dSchristos         return (NULL);
23028c506b8Sjruoho     }
23128c506b8Sjruoho 
23228c506b8Sjruoho     /* Init fields specific to the update struct */
23328c506b8Sjruoho 
23428c506b8Sjruoho     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_UPDATE;
23528c506b8Sjruoho     State->Update.Object = Object;
23628c506b8Sjruoho     State->Update.Value = Action;
237ff4a156dSchristos     return (State);
23828c506b8Sjruoho }
23928c506b8Sjruoho 
24028c506b8Sjruoho 
24128c506b8Sjruoho /*******************************************************************************
24228c506b8Sjruoho  *
24328c506b8Sjruoho  * FUNCTION:    AcpiUtCreatePkgState
24428c506b8Sjruoho  *
24528c506b8Sjruoho  * PARAMETERS:  Object          - Initial Object to be installed in the state
24628c506b8Sjruoho  *              Action          - Update action to be performed
24728c506b8Sjruoho  *
24828c506b8Sjruoho  * RETURN:      New state object, null on failure
24928c506b8Sjruoho  *
25028c506b8Sjruoho  * DESCRIPTION: Create a "Package State"
25128c506b8Sjruoho  *
25228c506b8Sjruoho  ******************************************************************************/
25328c506b8Sjruoho 
25428c506b8Sjruoho ACPI_GENERIC_STATE *
AcpiUtCreatePkgState(void * InternalObject,void * ExternalObject,UINT32 Index)25528c506b8Sjruoho AcpiUtCreatePkgState (
25628c506b8Sjruoho     void                    *InternalObject,
25728c506b8Sjruoho     void                    *ExternalObject,
25889b8eb6cSchristos     UINT32                  Index)
25928c506b8Sjruoho {
26028c506b8Sjruoho     ACPI_GENERIC_STATE      *State;
26128c506b8Sjruoho 
26228c506b8Sjruoho 
263ff4a156dSchristos     ACPI_FUNCTION_ENTRY ();
26428c506b8Sjruoho 
26528c506b8Sjruoho 
26628c506b8Sjruoho     /* Create the generic state object */
26728c506b8Sjruoho 
26828c506b8Sjruoho     State = AcpiUtCreateGenericState ();
26928c506b8Sjruoho     if (!State)
27028c506b8Sjruoho     {
271ff4a156dSchristos         return (NULL);
27228c506b8Sjruoho     }
27328c506b8Sjruoho 
27428c506b8Sjruoho     /* Init fields specific to the update struct */
27528c506b8Sjruoho 
27628c506b8Sjruoho     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_PACKAGE;
27728c506b8Sjruoho     State->Pkg.SourceObject = (ACPI_OPERAND_OBJECT *) InternalObject;
27828c506b8Sjruoho     State->Pkg.DestObject = ExternalObject;
27928c506b8Sjruoho     State->Pkg.Index= Index;
28028c506b8Sjruoho     State->Pkg.NumPackages = 1;
28171e38f1dSchristos 
282ff4a156dSchristos     return (State);
28328c506b8Sjruoho }
28428c506b8Sjruoho 
28528c506b8Sjruoho 
28628c506b8Sjruoho /*******************************************************************************
28728c506b8Sjruoho  *
28828c506b8Sjruoho  * FUNCTION:    AcpiUtCreateControlState
28928c506b8Sjruoho  *
29028c506b8Sjruoho  * PARAMETERS:  None
29128c506b8Sjruoho  *
29228c506b8Sjruoho  * RETURN:      New state object, null on failure
29328c506b8Sjruoho  *
29428c506b8Sjruoho  * DESCRIPTION: Create a "Control State" - a flavor of the generic state used
29528c506b8Sjruoho  *              to support nested IF/WHILE constructs in the AML.
29628c506b8Sjruoho  *
29728c506b8Sjruoho  ******************************************************************************/
29828c506b8Sjruoho 
29928c506b8Sjruoho ACPI_GENERIC_STATE *
AcpiUtCreateControlState(void)30028c506b8Sjruoho AcpiUtCreateControlState (
30128c506b8Sjruoho     void)
30228c506b8Sjruoho {
30328c506b8Sjruoho     ACPI_GENERIC_STATE      *State;
30428c506b8Sjruoho 
30528c506b8Sjruoho 
306ff4a156dSchristos     ACPI_FUNCTION_ENTRY ();
30728c506b8Sjruoho 
30828c506b8Sjruoho 
30928c506b8Sjruoho     /* Create the generic state object */
31028c506b8Sjruoho 
31128c506b8Sjruoho     State = AcpiUtCreateGenericState ();
31228c506b8Sjruoho     if (!State)
31328c506b8Sjruoho     {
314ff4a156dSchristos         return (NULL);
31528c506b8Sjruoho     }
31628c506b8Sjruoho 
31728c506b8Sjruoho     /* Init fields specific to the control struct */
31828c506b8Sjruoho 
31928c506b8Sjruoho     State->Common.DescriptorType = ACPI_DESC_TYPE_STATE_CONTROL;
32028c506b8Sjruoho     State->Common.State = ACPI_CONTROL_CONDITIONAL_EXECUTING;
32171e38f1dSchristos 
322ff4a156dSchristos     return (State);
32328c506b8Sjruoho }
32428c506b8Sjruoho 
32528c506b8Sjruoho 
32628c506b8Sjruoho /*******************************************************************************
32728c506b8Sjruoho  *
32828c506b8Sjruoho  * FUNCTION:    AcpiUtDeleteGenericState
32928c506b8Sjruoho  *
33028c506b8Sjruoho  * PARAMETERS:  State               - The state object to be deleted
33128c506b8Sjruoho  *
33228c506b8Sjruoho  * RETURN:      None
33328c506b8Sjruoho  *
33428c506b8Sjruoho  * DESCRIPTION: Release a state object to the state cache. NULL state objects
33528c506b8Sjruoho  *              are ignored.
33628c506b8Sjruoho  *
33728c506b8Sjruoho  ******************************************************************************/
33828c506b8Sjruoho 
33928c506b8Sjruoho void
AcpiUtDeleteGenericState(ACPI_GENERIC_STATE * State)34028c506b8Sjruoho AcpiUtDeleteGenericState (
34128c506b8Sjruoho     ACPI_GENERIC_STATE      *State)
34228c506b8Sjruoho {
343ff4a156dSchristos     ACPI_FUNCTION_ENTRY ();
34428c506b8Sjruoho 
34528c506b8Sjruoho 
34628c506b8Sjruoho     /* Ignore null state */
34728c506b8Sjruoho 
34828c506b8Sjruoho     if (State)
34928c506b8Sjruoho     {
35028c506b8Sjruoho         (void) AcpiOsReleaseObject (AcpiGbl_StateCache, State);
35128c506b8Sjruoho     }
35271e38f1dSchristos 
353ff4a156dSchristos     return;
35428c506b8Sjruoho }
355