167754Smsmith/******************************************************************************
267754Smsmith *
367754Smsmith * Module Name: psutils - Parser miscellaneous utilities (Parser only)
467754Smsmith *
567754Smsmith *****************************************************************************/
667754Smsmith
7217365Sjkim/*
8306536Sjkim * Copyright (C) 2000 - 2016, Intel Corp.
970243Smsmith * All rights reserved.
1067754Smsmith *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
2567754Smsmith *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
2967754Smsmith *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
4367754Smsmith
44193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
45193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
46193341Sjkim#include <contrib/dev/acpica/include/acparser.h>
47193341Sjkim#include <contrib/dev/acpica/include/amlcode.h>
4867754Smsmith
4977424Smsmith#define _COMPONENT          ACPI_PARSER
5091116Smsmith        ACPI_MODULE_NAME    ("psutils")
5167754Smsmith
5267754Smsmith
5367754Smsmith/*******************************************************************************
5467754Smsmith *
55100966Siwasaki * FUNCTION:    AcpiPsCreateScopeOp
56100966Siwasaki *
57100966Siwasaki * PARAMETERS:  None
58100966Siwasaki *
59151937Sjkim * RETURN:      A new Scope object, null on failure
60100966Siwasaki *
61100966Siwasaki * DESCRIPTION: Create a Scope and associated namepath op with the root name
62100966Siwasaki *
63100966Siwasaki ******************************************************************************/
64100966Siwasaki
65100966SiwasakiACPI_PARSE_OBJECT *
66100966SiwasakiAcpiPsCreateScopeOp (
67306536Sjkim    UINT8                   *Aml)
68100966Siwasaki{
69100966Siwasaki    ACPI_PARSE_OBJECT       *ScopeOp;
70100966Siwasaki
71100966Siwasaki
72306536Sjkim    ScopeOp = AcpiPsAllocOp (AML_SCOPE_OP, Aml);
73100966Siwasaki    if (!ScopeOp)
74100966Siwasaki    {
75100966Siwasaki        return (NULL);
76100966Siwasaki    }
77100966Siwasaki
78100966Siwasaki    ScopeOp->Named.Name = ACPI_ROOT_NAME;
79100966Siwasaki    return (ScopeOp);
80100966Siwasaki}
81100966Siwasaki
82100966Siwasaki
83100966Siwasaki/*******************************************************************************
84100966Siwasaki *
8567754Smsmith * FUNCTION:    AcpiPsInitOp
8667754Smsmith *
8767754Smsmith * PARAMETERS:  Op              - A newly allocated Op object
8867754Smsmith *              Opcode          - Opcode to store in the Op
8967754Smsmith *
90151937Sjkim * RETURN:      None
9167754Smsmith *
92151937Sjkim * DESCRIPTION: Initialize a parse (Op) object
9367754Smsmith *
9467754Smsmith ******************************************************************************/
9567754Smsmith
9667754Smsmithvoid
9767754SmsmithAcpiPsInitOp (
9867754Smsmith    ACPI_PARSE_OBJECT       *Op,
9967754Smsmith    UINT16                  Opcode)
10067754Smsmith{
10191116Smsmith    ACPI_FUNCTION_ENTRY ();
10267754Smsmith
10367754Smsmith
104167802Sjkim    Op->Common.DescriptorType = ACPI_DESC_TYPE_PARSER;
10599679Siwasaki    Op->Common.AmlOpcode = Opcode;
10667754Smsmith
107306536Sjkim    ACPI_DISASM_ONLY_MEMBERS (strncpy (Op->Common.AmlOpName,
108306536Sjkim        (AcpiPsGetOpcodeInfo (Opcode))->Name,
109306536Sjkim        sizeof (Op->Common.AmlOpName)));
11067754Smsmith}
11167754Smsmith
11267754Smsmith
11367754Smsmith/*******************************************************************************
11467754Smsmith *
11567754Smsmith * FUNCTION:    AcpiPsAllocOp
11667754Smsmith *
11767754Smsmith * PARAMETERS:  Opcode          - Opcode that will be stored in the new Op
118306536Sjkim *              Aml             - Address of the opcode
11967754Smsmith *
120151937Sjkim * RETURN:      Pointer to the new Op, null on failure
12167754Smsmith *
12267754Smsmith * DESCRIPTION: Allocate an acpi_op, choose op type (and thus size) based on
123241973Sjkim *              opcode. A cache of opcodes is available for the pure
12467754Smsmith *              GENERIC_OP, since this is by far the most commonly used.
12567754Smsmith *
12667754Smsmith ******************************************************************************/
12767754Smsmith
12867754SmsmithACPI_PARSE_OBJECT*
12967754SmsmithAcpiPsAllocOp (
130306536Sjkim    UINT16                  Opcode,
131306536Sjkim    UINT8                   *Aml)
13267754Smsmith{
133138287Smarks    ACPI_PARSE_OBJECT       *Op;
13483174Smsmith    const ACPI_OPCODE_INFO  *OpInfo;
135138287Smarks    UINT8                   Flags = ACPI_PARSEOP_GENERIC;
13667754Smsmith
13767754Smsmith
13891116Smsmith    ACPI_FUNCTION_ENTRY ();
13977424Smsmith
14083174Smsmith
14183174Smsmith    OpInfo = AcpiPsGetOpcodeInfo (Opcode);
14283174Smsmith
143138287Smarks    /* Determine type of ParseOp required */
14467754Smsmith
14583174Smsmith    if (OpInfo->Flags & AML_DEFER)
14667754Smsmith    {
14791116Smsmith        Flags = ACPI_PARSEOP_DEFERRED;
14867754Smsmith    }
14983174Smsmith    else if (OpInfo->Flags & AML_NAMED)
15067754Smsmith    {
151306536Sjkim        Flags = ACPI_PARSEOP_NAMED_OBJECT;
15267754Smsmith    }
15383174Smsmith    else if (Opcode == AML_INT_BYTELIST_OP)
15467754Smsmith    {
15591116Smsmith        Flags = ACPI_PARSEOP_BYTELIST;
15667754Smsmith    }
157138287Smarks
158138287Smarks    /* Allocate the minimum required size object */
159138287Smarks
160138287Smarks    if (Flags == ACPI_PARSEOP_GENERIC)
16167754Smsmith    {
162138287Smarks        /* The generic op (default) is by far the most common (16 to 1) */
16367754Smsmith
164151937Sjkim        Op = AcpiOsAcquireObject (AcpiGbl_PsNodeCache);
16567754Smsmith    }
16667754Smsmith    else
16767754Smsmith    {
168138287Smarks        /* Extended parseop */
169138287Smarks
170151937Sjkim        Op = AcpiOsAcquireObject (AcpiGbl_PsNodeExtCache);
17167754Smsmith    }
17267754Smsmith
17382367Smsmith    /* Initialize the Op */
17467754Smsmith
17567754Smsmith    if (Op)
17667754Smsmith    {
17767754Smsmith        AcpiPsInitOp (Op, Opcode);
178306536Sjkim        Op->Common.Aml = Aml;
17999679Siwasaki        Op->Common.Flags = Flags;
18067754Smsmith    }
18167754Smsmith
18267754Smsmith    return (Op);
18367754Smsmith}
18467754Smsmith
18567754Smsmith
18667754Smsmith/*******************************************************************************
18767754Smsmith *
18867754Smsmith * FUNCTION:    AcpiPsFreeOp
18967754Smsmith *
19067754Smsmith * PARAMETERS:  Op              - Op to be freed
19167754Smsmith *
19267754Smsmith * RETURN:      None.
19367754Smsmith *
194241973Sjkim * DESCRIPTION: Free an Op object. Either put it on the GENERIC_OP cache list
19567754Smsmith *              or actually free it.
19667754Smsmith *
19767754Smsmith ******************************************************************************/
19867754Smsmith
19967754Smsmithvoid
20067754SmsmithAcpiPsFreeOp (
20167754Smsmith    ACPI_PARSE_OBJECT       *Op)
20267754Smsmith{
203167802Sjkim    ACPI_FUNCTION_NAME (PsFreeOp);
20467754Smsmith
20567754Smsmith
20699679Siwasaki    if (Op->Common.AmlOpcode == AML_INT_RETURN_VALUE_OP)
20767754Smsmith    {
208306536Sjkim        ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
209306536Sjkim            "Free retval op: %p\n", Op));
21067754Smsmith    }
21167754Smsmith
212100966Siwasaki    if (Op->Common.Flags & ACPI_PARSEOP_GENERIC)
21367754Smsmith    {
214151937Sjkim        (void) AcpiOsReleaseObject (AcpiGbl_PsNodeCache, Op);
21567754Smsmith    }
21667754Smsmith    else
21767754Smsmith    {
218151937Sjkim        (void) AcpiOsReleaseObject (AcpiGbl_PsNodeExtCache, Op);
21967754Smsmith    }
22067754Smsmith}
22167754Smsmith
22267754Smsmith
22367754Smsmith/*******************************************************************************
22467754Smsmith *
22567754Smsmith * FUNCTION:    Utility functions
22667754Smsmith *
22783174Smsmith * DESCRIPTION: Low level character and object functions
22867754Smsmith *
22967754Smsmith ******************************************************************************/
23067754Smsmith
23167754Smsmith
23267754Smsmith/*
23367754Smsmith * Is "c" a namestring lead character?
23467754Smsmith */
23567754SmsmithBOOLEAN
23667754SmsmithAcpiPsIsLeadingChar (
23767754Smsmith    UINT32                  c)
23867754Smsmith{
23967754Smsmith    return ((BOOLEAN) (c == '_' || (c >= 'A' && c <= 'Z')));
24067754Smsmith}
24167754Smsmith
24267754Smsmith
24367754Smsmith/*
24483174Smsmith * Get op's name (4-byte name segment) or 0 if unnamed
24567754Smsmith */
24683174SmsmithUINT32
24783174SmsmithAcpiPsGetName (
24883174Smsmith    ACPI_PARSE_OBJECT       *Op)
24967754Smsmith{
25067754Smsmith
25183174Smsmith    /* The "generic" object has no name associated with it */
25267754Smsmith
25399679Siwasaki    if (Op->Common.Flags & ACPI_PARSEOP_GENERIC)
25483174Smsmith    {
25583174Smsmith        return (0);
25683174Smsmith    }
25767754Smsmith
25883174Smsmith    /* Only the "Extended" parse objects have a name */
25967754Smsmith
26099679Siwasaki    return (Op->Named.Name);
26167754Smsmith}
26267754Smsmith
26367754Smsmith
26467754Smsmith/*
26567754Smsmith * Set op's name
26667754Smsmith */
26767754Smsmithvoid
26867754SmsmithAcpiPsSetName (
26967754Smsmith    ACPI_PARSE_OBJECT       *Op,
27067754Smsmith    UINT32                  name)
27167754Smsmith{
27267754Smsmith
27383174Smsmith    /* The "generic" object has no name associated with it */
27483174Smsmith
27599679Siwasaki    if (Op->Common.Flags & ACPI_PARSEOP_GENERIC)
27667754Smsmith    {
27783174Smsmith        return;
27867754Smsmith    }
27983174Smsmith
28099679Siwasaki    Op->Named.Name = name;
28167754Smsmith}
282