1321655Sjkim/******************************************************************************
2321655Sjkim *
3321655Sjkim * Module Name: dspkginit - Completion of deferred package initialization
4321655Sjkim *
5321655Sjkim *****************************************************************************/
6321655Sjkim
7321655Sjkim/******************************************************************************
8321655Sjkim *
9321655Sjkim * 1. Copyright Notice
10321655Sjkim *
11321655Sjkim * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12321655Sjkim * All rights reserved.
13321655Sjkim *
14321655Sjkim * 2. License
15321655Sjkim *
16321655Sjkim * 2.1. This is your license from Intel Corp. under its intellectual property
17321655Sjkim * rights. You may have additional license terms from the party that provided
18321655Sjkim * you this software, covering your right to use that party's intellectual
19321655Sjkim * property rights.
20321655Sjkim *
21321655Sjkim * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22321655Sjkim * copy of the source code appearing in this file ("Covered Code") an
23321655Sjkim * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24321655Sjkim * base code distributed originally by Intel ("Original Intel Code") to copy,
25321655Sjkim * make derivatives, distribute, use and display any portion of the Covered
26321655Sjkim * Code in any form, with the right to sublicense such rights; and
27321655Sjkim *
28321655Sjkim * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29321655Sjkim * license (with the right to sublicense), under only those claims of Intel
30321655Sjkim * patents that are infringed by the Original Intel Code, to make, use, sell,
31321655Sjkim * offer to sell, and import the Covered Code and derivative works thereof
32321655Sjkim * solely to the minimum extent necessary to exercise the above copyright
33321655Sjkim * license, and in no event shall the patent license extend to any additions
34321655Sjkim * to or modifications of the Original Intel Code. No other license or right
35321655Sjkim * is granted directly or by implication, estoppel or otherwise;
36321655Sjkim *
37321655Sjkim * The above copyright and patent license is granted only if the following
38321655Sjkim * conditions are met:
39321655Sjkim *
40321655Sjkim * 3. Conditions
41321655Sjkim *
42321655Sjkim * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43321655Sjkim * Redistribution of source code of any substantial portion of the Covered
44321655Sjkim * Code or modification with rights to further distribute source must include
45321655Sjkim * the above Copyright Notice, the above License, this list of Conditions,
46321655Sjkim * and the following Disclaimer and Export Compliance provision. In addition,
47321655Sjkim * Licensee must cause all Covered Code to which Licensee contributes to
48321655Sjkim * contain a file documenting the changes Licensee made to create that Covered
49321655Sjkim * Code and the date of any change. Licensee must include in that file the
50321655Sjkim * documentation of any changes made by any predecessor Licensee. Licensee
51321655Sjkim * must include a prominent statement that the modification is derived,
52321655Sjkim * directly or indirectly, from Original Intel Code.
53321655Sjkim *
54321655Sjkim * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55321655Sjkim * Redistribution of source code of any substantial portion of the Covered
56321655Sjkim * Code or modification without rights to further distribute source must
57321655Sjkim * include the following Disclaimer and Export Compliance provision in the
58321655Sjkim * documentation and/or other materials provided with distribution. In
59321655Sjkim * addition, Licensee may not authorize further sublicense of source of any
60321655Sjkim * portion of the Covered Code, and must include terms to the effect that the
61321655Sjkim * license from Licensee to its licensee is limited to the intellectual
62321655Sjkim * property embodied in the software Licensee provides to its licensee, and
63321655Sjkim * not to intellectual property embodied in modifications its licensee may
64321655Sjkim * make.
65321655Sjkim *
66321655Sjkim * 3.3. Redistribution of Executable. Redistribution in executable form of any
67321655Sjkim * substantial portion of the Covered Code or modification must reproduce the
68321655Sjkim * above Copyright Notice, and the following Disclaimer and Export Compliance
69321655Sjkim * provision in the documentation and/or other materials provided with the
70321655Sjkim * distribution.
71321655Sjkim *
72321655Sjkim * 3.4. Intel retains all right, title, and interest in and to the Original
73321655Sjkim * Intel Code.
74321655Sjkim *
75321655Sjkim * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76321655Sjkim * Intel shall be used in advertising or otherwise to promote the sale, use or
77321655Sjkim * other dealings in products derived from or relating to the Covered Code
78321655Sjkim * without prior written authorization from Intel.
79321655Sjkim *
80321655Sjkim * 4. Disclaimer and Export Compliance
81321655Sjkim *
82321655Sjkim * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83321655Sjkim * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84321655Sjkim * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85321655Sjkim * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86321655Sjkim * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87321655Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88321655Sjkim * PARTICULAR PURPOSE.
89321655Sjkim *
90321655Sjkim * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91321655Sjkim * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92321655Sjkim * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93321655Sjkim * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94321655Sjkim * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95321655Sjkim * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96321655Sjkim * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97321655Sjkim * LIMITED REMEDY.
98321655Sjkim *
99321655Sjkim * 4.3. Licensee shall not export, either directly or indirectly, any of this
100321655Sjkim * software or system incorporating such software without first obtaining any
101321655Sjkim * required license or other approval from the U. S. Department of Commerce or
102321655Sjkim * any other agency or department of the United States Government. In the
103321655Sjkim * event Licensee exports any such software from the United States or
104321655Sjkim * re-exports any such software from a foreign destination, Licensee shall
105321655Sjkim * ensure that the distribution and export/re-export of the software is in
106321655Sjkim * compliance with all laws, regulations, orders, or other restrictions of the
107321655Sjkim * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108321655Sjkim * any of its subsidiaries will export/re-export any technical data, process,
109321655Sjkim * software, or service, directly or indirectly, to any country for which the
110321655Sjkim * United States government or any agency thereof requires an export license,
111321655Sjkim * other governmental approval, or letter of assurance, without first obtaining
112321655Sjkim * such license, approval or letter.
113321655Sjkim *
114321655Sjkim *****************************************************************************
115321655Sjkim *
116321655Sjkim * Alternatively, you may choose to be licensed under the terms of the
117321655Sjkim * following license:
118321655Sjkim *
119321655Sjkim * Redistribution and use in source and binary forms, with or without
120321655Sjkim * modification, are permitted provided that the following conditions
121321655Sjkim * are met:
122321655Sjkim * 1. Redistributions of source code must retain the above copyright
123321655Sjkim *    notice, this list of conditions, and the following disclaimer,
124321655Sjkim *    without modification.
125321655Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126321655Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
127321655Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
128321655Sjkim *    including a substantially similar Disclaimer requirement for further
129321655Sjkim *    binary redistribution.
130321655Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
131321655Sjkim *    of any contributors may be used to endorse or promote products derived
132321655Sjkim *    from this software without specific prior written permission.
133321655Sjkim *
134321655Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135321655Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136321655Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137321655Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138321655Sjkim * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139321655Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140321655Sjkim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141321655Sjkim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142321655Sjkim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143321655Sjkim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144321655Sjkim * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145321655Sjkim *
146321655Sjkim * Alternatively, you may choose to be licensed under the terms of the
147321655Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
148321655Sjkim * Software Foundation.
149321655Sjkim *
150321655Sjkim *****************************************************************************/
151321655Sjkim
152321670Sjkim#include <contrib/dev/acpica/include/acpi.h>
153321670Sjkim#include <contrib/dev/acpica/include/accommon.h>
154321670Sjkim#include <contrib/dev/acpica/include/acnamesp.h>
155321670Sjkim#include <contrib/dev/acpica/include/amlcode.h>
156321670Sjkim#include <contrib/dev/acpica/include/acdispat.h>
157321670Sjkim#include <contrib/dev/acpica/include/acinterp.h>
158321655Sjkim
159321655Sjkim
160321655Sjkim#define _COMPONENT          ACPI_NAMESPACE
161321655Sjkim        ACPI_MODULE_NAME    ("dspkginit")
162321655Sjkim
163321655Sjkim
164321655Sjkim/* Local prototypes */
165321655Sjkim
166321655Sjkimstatic void
167321655SjkimAcpiDsResolvePackageElement (
168321655Sjkim    ACPI_OPERAND_OBJECT     **Element);
169321655Sjkim
170321655Sjkim
171321655Sjkim/*******************************************************************************
172321655Sjkim *
173321655Sjkim * FUNCTION:    AcpiDsBuildInternalPackageObj
174321655Sjkim *
175321655Sjkim * PARAMETERS:  WalkState       - Current walk state
176321655Sjkim *              Op              - Parser object to be translated
177321655Sjkim *              ElementCount    - Number of elements in the package - this is
178321655Sjkim *                                the NumElements argument to Package()
179321655Sjkim *              ObjDescPtr      - Where the ACPI internal object is returned
180321655Sjkim *
181321655Sjkim * RETURN:      Status
182321655Sjkim *
183321655Sjkim * DESCRIPTION: Translate a parser Op package object to the equivalent
184321655Sjkim *              namespace object
185321655Sjkim *
186321655Sjkim * NOTE: The number of elements in the package will be always be the NumElements
187321655Sjkim * count, regardless of the number of elements in the package list. If
188321655Sjkim * NumElements is smaller, only that many package list elements are used.
189321655Sjkim * if NumElements is larger, the Package object is padded out with
190321655Sjkim * objects of type Uninitialized (as per ACPI spec.)
191321655Sjkim *
192321655Sjkim * Even though the ASL compilers do not allow NumElements to be smaller
193321655Sjkim * than the Package list length (for the fixed length package opcode), some
194321655Sjkim * BIOS code modifies the AML on the fly to adjust the NumElements, and
195321655Sjkim * this code compensates for that. This also provides compatibility with
196321655Sjkim * other AML interpreters.
197321655Sjkim *
198321655Sjkim ******************************************************************************/
199321655Sjkim
200321655SjkimACPI_STATUS
201321655SjkimAcpiDsBuildInternalPackageObj (
202321655Sjkim    ACPI_WALK_STATE         *WalkState,
203321655Sjkim    ACPI_PARSE_OBJECT       *Op,
204321655Sjkim    UINT32                  ElementCount,
205321655Sjkim    ACPI_OPERAND_OBJECT     **ObjDescPtr)
206321655Sjkim{
207321655Sjkim    ACPI_PARSE_OBJECT       *Arg;
208321655Sjkim    ACPI_PARSE_OBJECT       *Parent;
209321655Sjkim    ACPI_OPERAND_OBJECT     *ObjDesc = NULL;
210321655Sjkim    ACPI_STATUS             Status = AE_OK;
211321655Sjkim    UINT16                  ReferenceCount;
212321655Sjkim    UINT32                  Index;
213321655Sjkim    UINT32                  i;
214321655Sjkim
215321655Sjkim
216321655Sjkim    ACPI_FUNCTION_TRACE (DsBuildInternalPackageObj);
217321655Sjkim
218321655Sjkim
219321655Sjkim    /* Find the parent of a possibly nested package */
220321655Sjkim
221321655Sjkim    Parent = Op->Common.Parent;
222321655Sjkim    while ((Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
223321655Sjkim           (Parent->Common.AmlOpcode == AML_VARIABLE_PACKAGE_OP))
224321655Sjkim    {
225321655Sjkim        Parent = Parent->Common.Parent;
226321655Sjkim    }
227321655Sjkim
228321655Sjkim    /*
229321655Sjkim     * If we are evaluating a Named package object of the form:
230321655Sjkim     *      Name (xxxx, Package)
231321655Sjkim     * the package object already exists, otherwise it must be created.
232321655Sjkim     */
233321655Sjkim    ObjDesc = *ObjDescPtr;
234321655Sjkim    if (!ObjDesc)
235321655Sjkim    {
236321655Sjkim        ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_PACKAGE);
237321655Sjkim        *ObjDescPtr = ObjDesc;
238321655Sjkim        if (!ObjDesc)
239321655Sjkim        {
240321655Sjkim            return_ACPI_STATUS (AE_NO_MEMORY);
241321655Sjkim        }
242321655Sjkim
243321655Sjkim        ObjDesc->Package.Node = Parent->Common.Node;
244321655Sjkim    }
245321655Sjkim
246321655Sjkim    if (ObjDesc->Package.Flags & AOPOBJ_DATA_VALID) /* Just in case */
247321655Sjkim    {
248321655Sjkim        return_ACPI_STATUS (AE_OK);
249321655Sjkim    }
250321655Sjkim
251321655Sjkim    /*
252321655Sjkim     * Allocate the element array (array of pointers to the individual
253321655Sjkim     * objects) based on the NumElements parameter. Add an extra pointer slot
254321655Sjkim     * so that the list is always null terminated.
255321655Sjkim     */
256321655Sjkim    ObjDesc->Package.Elements = ACPI_ALLOCATE_ZEROED (
257321655Sjkim        ((ACPI_SIZE) ElementCount + 1) * sizeof (void *));
258321655Sjkim
259321655Sjkim    if (!ObjDesc->Package.Elements)
260321655Sjkim    {
261321655Sjkim        AcpiUtDeleteObjectDesc (ObjDesc);
262321655Sjkim        return_ACPI_STATUS (AE_NO_MEMORY);
263321655Sjkim    }
264321655Sjkim
265321655Sjkim    ObjDesc->Package.Count = ElementCount;
266321655Sjkim    Arg = Op->Common.Value.Arg;
267321655Sjkim    Arg = Arg->Common.Next;
268321655Sjkim
269321655Sjkim    if (Arg)
270321655Sjkim    {
271321655Sjkim        ObjDesc->Package.Flags |= AOPOBJ_DATA_VALID;
272321655Sjkim    }
273321655Sjkim
274321655Sjkim    /*
275321655Sjkim     * Initialize the elements of the package, up to the NumElements count.
276321655Sjkim     * Package is automatically padded with uninitialized (NULL) elements
277321655Sjkim     * if NumElements is greater than the package list length. Likewise,
278321655Sjkim     * Package is truncated if NumElements is less than the list length.
279321655Sjkim     */
280321655Sjkim    for (i = 0; Arg && (i < ElementCount); i++)
281321655Sjkim    {
282321655Sjkim        if (Arg->Common.AmlOpcode == AML_INT_RETURN_VALUE_OP)
283321655Sjkim        {
284321655Sjkim            if (Arg->Common.Node->Type == ACPI_TYPE_METHOD)
285321655Sjkim            {
286321655Sjkim                /*
287321655Sjkim                 * A method reference "looks" to the parser to be a method
288321655Sjkim                 * invocation, so we special case it here
289321655Sjkim                 */
290321655Sjkim                Arg->Common.AmlOpcode = AML_INT_NAMEPATH_OP;
291321655Sjkim                Status = AcpiDsBuildInternalObject (
292321655Sjkim                    WalkState, Arg, &ObjDesc->Package.Elements[i]);
293321655Sjkim            }
294321655Sjkim            else
295321655Sjkim            {
296321655Sjkim                /* This package element is already built, just get it */
297321655Sjkim
298321655Sjkim                ObjDesc->Package.Elements[i] =
299321655Sjkim                    ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Arg->Common.Node);
300321655Sjkim            }
301321655Sjkim        }
302321655Sjkim        else
303321655Sjkim        {
304321655Sjkim            Status = AcpiDsBuildInternalObject (
305321655Sjkim                WalkState, Arg, &ObjDesc->Package.Elements[i]);
306321655Sjkim            if (Status == AE_NOT_FOUND)
307321655Sjkim            {
308321655Sjkim                ACPI_ERROR ((AE_INFO, "%-48s", "****DS namepath not found"));
309321655Sjkim            }
310321655Sjkim
311321655Sjkim            /*
312321655Sjkim             * Initialize this package element. This function handles the
313321655Sjkim             * resolution of named references within the package.
314321655Sjkim             */
315321655Sjkim            AcpiDsInitPackageElement (0, ObjDesc->Package.Elements[i],
316321655Sjkim                NULL, &ObjDesc->Package.Elements[i]);
317321655Sjkim        }
318321655Sjkim
319321655Sjkim        if (*ObjDescPtr)
320321655Sjkim        {
321321655Sjkim            /* Existing package, get existing reference count */
322321655Sjkim
323321655Sjkim            ReferenceCount = (*ObjDescPtr)->Common.ReferenceCount;
324321655Sjkim            if (ReferenceCount > 1)
325321655Sjkim            {
326321655Sjkim                /* Make new element ref count match original ref count */
327321655Sjkim                /* TBD: Probably need an AcpiUtAddReferences function */
328321655Sjkim
329321655Sjkim                for (Index = 0; Index < ((UINT32) ReferenceCount - 1); Index++)
330321655Sjkim                {
331321655Sjkim                    AcpiUtAddReference ((ObjDesc->Package.Elements[i]));
332321655Sjkim                }
333321655Sjkim            }
334321655Sjkim        }
335321655Sjkim
336321655Sjkim        Arg = Arg->Common.Next;
337321655Sjkim    }
338321655Sjkim
339321655Sjkim    /* Check for match between NumElements and actual length of PackageList */
340321655Sjkim
341321655Sjkim    if (Arg)
342321655Sjkim    {
343321655Sjkim        /*
344321655Sjkim         * NumElements was exhausted, but there are remaining elements in
345321655Sjkim         * the PackageList. Truncate the package to NumElements.
346321655Sjkim         *
347321655Sjkim         * Note: technically, this is an error, from ACPI spec: "It is an
348321655Sjkim         * error for NumElements to be less than the number of elements in
349321655Sjkim         * the PackageList". However, we just print a message and no
350321655Sjkim         * exception is returned. This provides compatibility with other
351321655Sjkim         * ACPI implementations. Some firmware implementations will alter
352321655Sjkim         * the NumElements on the fly, possibly creating this type of
353321655Sjkim         * ill-formed package object.
354321655Sjkim         */
355321655Sjkim        while (Arg)
356321655Sjkim        {
357321655Sjkim            /*
358321655Sjkim             * We must delete any package elements that were created earlier
359321655Sjkim             * and are not going to be used because of the package truncation.
360321655Sjkim             */
361321655Sjkim            if (Arg->Common.Node)
362321655Sjkim            {
363321655Sjkim                AcpiUtRemoveReference (
364321655Sjkim                    ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Arg->Common.Node));
365321655Sjkim                Arg->Common.Node = NULL;
366321655Sjkim            }
367321655Sjkim
368321655Sjkim            /* Find out how many elements there really are */
369321655Sjkim
370321655Sjkim            i++;
371321655Sjkim            Arg = Arg->Common.Next;
372321655Sjkim        }
373321655Sjkim
374321655Sjkim        ACPI_INFO ((
375321655Sjkim            "Actual Package length (%u) is larger than "
376321655Sjkim            "NumElements field (%u), truncated",
377321655Sjkim            i, ElementCount));
378321655Sjkim    }
379321655Sjkim    else if (i < ElementCount)
380321655Sjkim    {
381321655Sjkim        /*
382321655Sjkim         * Arg list (elements) was exhausted, but we did not reach
383321655Sjkim         * NumElements count.
384321655Sjkim         *
385321655Sjkim         * Note: this is not an error, the package is padded out
386321655Sjkim         * with NULLs.
387321655Sjkim         */
388321655Sjkim        ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
389321655Sjkim            "Package List length (%u) smaller than NumElements "
390321655Sjkim            "count (%u), padded with null elements\n",
391321655Sjkim            i, ElementCount));
392321655Sjkim    }
393321655Sjkim
394321655Sjkim    ObjDesc->Package.Flags |= AOPOBJ_DATA_VALID;
395321655Sjkim    Op->Common.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, ObjDesc);
396321655Sjkim    return_ACPI_STATUS (Status);
397321655Sjkim}
398321655Sjkim
399321655Sjkim
400321655Sjkim/*******************************************************************************
401321655Sjkim *
402321655Sjkim * FUNCTION:    AcpiDsInitPackageElement
403321655Sjkim *
404321655Sjkim * PARAMETERS:  ACPI_PKG_CALLBACK
405321655Sjkim *
406321655Sjkim * RETURN:      Status
407321655Sjkim *
408321655Sjkim * DESCRIPTION: Resolve a named reference element within a package object
409321655Sjkim *
410321655Sjkim ******************************************************************************/
411321655Sjkim
412321655SjkimACPI_STATUS
413321655SjkimAcpiDsInitPackageElement (
414321655Sjkim    UINT8                   ObjectType,
415321655Sjkim    ACPI_OPERAND_OBJECT     *SourceObject,
416321655Sjkim    ACPI_GENERIC_STATE      *State,
417321655Sjkim    void                    *Context)
418321655Sjkim{
419321655Sjkim    ACPI_OPERAND_OBJECT     **ElementPtr;
420321655Sjkim
421321655Sjkim
422327557Sjkim    ACPI_FUNCTION_TRACE (DsInitPackageElement);
423327557Sjkim
424327557Sjkim
425321655Sjkim    if (!SourceObject)
426321655Sjkim    {
427327557Sjkim        return_ACPI_STATUS (AE_OK);
428321655Sjkim    }
429321655Sjkim
430321655Sjkim    /*
431321655Sjkim     * The following code is a bit of a hack to workaround a (current)
432321655Sjkim     * limitation of the ACPI_PKG_CALLBACK interface. We need a pointer
433321655Sjkim     * to the location within the element array because a new object
434321655Sjkim     * may be created and stored there.
435321655Sjkim     */
436321655Sjkim    if (Context)
437321655Sjkim    {
438321655Sjkim        /* A direct call was made to this function */
439321655Sjkim
440321655Sjkim        ElementPtr = (ACPI_OPERAND_OBJECT **) Context;
441321655Sjkim    }
442321655Sjkim    else
443321655Sjkim    {
444321655Sjkim        /* Call came from AcpiUtWalkPackageTree */
445321655Sjkim
446321655Sjkim        ElementPtr = State->Pkg.ThisTargetObj;
447321655Sjkim    }
448321655Sjkim
449321655Sjkim    /* We are only interested in reference objects/elements */
450321655Sjkim
451321655Sjkim    if (SourceObject->Common.Type == ACPI_TYPE_LOCAL_REFERENCE)
452321655Sjkim    {
453321655Sjkim        /* Attempt to resolve the (named) reference to a namespace node */
454321655Sjkim
455321655Sjkim        AcpiDsResolvePackageElement (ElementPtr);
456321655Sjkim    }
457321655Sjkim    else if (SourceObject->Common.Type == ACPI_TYPE_PACKAGE)
458321655Sjkim    {
459321655Sjkim        SourceObject->Package.Flags |= AOPOBJ_DATA_VALID;
460321655Sjkim    }
461321655Sjkim
462327557Sjkim    return_ACPI_STATUS (AE_OK);
463321655Sjkim}
464321655Sjkim
465321655Sjkim
466321655Sjkim/*******************************************************************************
467321655Sjkim *
468321655Sjkim * FUNCTION:    AcpiDsResolvePackageElement
469321655Sjkim *
470321655Sjkim * PARAMETERS:  ElementPtr          - Pointer to a reference object
471321655Sjkim *
472321655Sjkim * RETURN:      Possible new element is stored to the indirect ElementPtr
473321655Sjkim *
474321655Sjkim * DESCRIPTION: Resolve a package element that is a reference to a named
475321655Sjkim *              object.
476321655Sjkim *
477321655Sjkim ******************************************************************************/
478321655Sjkim
479321655Sjkimstatic void
480321655SjkimAcpiDsResolvePackageElement (
481321655Sjkim    ACPI_OPERAND_OBJECT     **ElementPtr)
482321655Sjkim{
483321655Sjkim    ACPI_STATUS             Status;
484321655Sjkim    ACPI_GENERIC_STATE      ScopeInfo;
485321655Sjkim    ACPI_OPERAND_OBJECT     *Element = *ElementPtr;
486321655Sjkim    ACPI_NAMESPACE_NODE     *ResolvedNode;
487327557Sjkim    ACPI_NAMESPACE_NODE     *OriginalNode;
488321655Sjkim    char                    *ExternalPath = NULL;
489321655Sjkim    ACPI_OBJECT_TYPE        Type;
490321655Sjkim
491321655Sjkim
492321655Sjkim    ACPI_FUNCTION_TRACE (DsResolvePackageElement);
493321655Sjkim
494321655Sjkim
495321655Sjkim    /* Check if reference element is already resolved */
496321655Sjkim
497321655Sjkim    if (Element->Reference.Resolved)
498321655Sjkim    {
499321655Sjkim        return_VOID;
500321655Sjkim    }
501321655Sjkim
502321655Sjkim    /* Element must be a reference object of correct type */
503321655Sjkim
504321655Sjkim    ScopeInfo.Scope.Node = Element->Reference.Node; /* Prefix node */
505321655Sjkim
506321655Sjkim    Status = AcpiNsLookup (&ScopeInfo,
507321655Sjkim        (char *) Element->Reference.Aml,            /* Pointer to AML path */
508321655Sjkim        ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
509321655Sjkim        ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
510321655Sjkim        NULL, &ResolvedNode);
511321655Sjkim    if (ACPI_FAILURE (Status))
512321655Sjkim    {
513321655Sjkim        Status = AcpiNsExternalizeName (ACPI_UINT32_MAX,
514321655Sjkim            (char *) Element->Reference.Aml,
515321655Sjkim            NULL, &ExternalPath);
516321655Sjkim
517321655Sjkim        ACPI_EXCEPTION ((AE_INFO, Status,
518321655Sjkim            "Could not find/resolve named package element: %s", ExternalPath));
519321655Sjkim
520321655Sjkim        ACPI_FREE (ExternalPath);
521321655Sjkim        *ElementPtr = NULL;
522321655Sjkim        return_VOID;
523321655Sjkim    }
524321655Sjkim    else if (ResolvedNode->Type == ACPI_TYPE_ANY)
525321655Sjkim    {
526321655Sjkim        /* Named reference not resolved, return a NULL package element */
527321655Sjkim
528321655Sjkim        ACPI_ERROR ((AE_INFO,
529321655Sjkim            "Could not resolve named package element [%4.4s] in [%4.4s]",
530321655Sjkim            ResolvedNode->Name.Ascii, ScopeInfo.Scope.Node->Name.Ascii));
531321655Sjkim        *ElementPtr = NULL;
532321655Sjkim        return_VOID;
533321655Sjkim    }
534321655Sjkim#if 0
535321655Sjkim    else if (ResolvedNode->Flags & ANOBJ_TEMPORARY)
536321655Sjkim    {
537321655Sjkim        /*
538321655Sjkim         * A temporary node found here indicates that the reference is
539321655Sjkim         * to a node that was created within this method. We are not
540321655Sjkim         * going to allow it (especially if the package is returned
541321655Sjkim         * from the method) -- the temporary node will be deleted out
542321655Sjkim         * from under the method. (05/2017).
543321655Sjkim         */
544321655Sjkim        ACPI_ERROR ((AE_INFO,
545321655Sjkim            "Package element refers to a temporary name [%4.4s], "
546321655Sjkim            "inserting a NULL element",
547321655Sjkim            ResolvedNode->Name.Ascii));
548321655Sjkim        *ElementPtr = NULL;
549321655Sjkim        return_VOID;
550321655Sjkim    }
551321655Sjkim#endif
552321655Sjkim
553321655Sjkim    /*
554321655Sjkim     * Special handling for Alias objects. We need ResolvedNode to point
555321655Sjkim     * to the Alias target. This effectively "resolves" the alias.
556321655Sjkim     */
557321655Sjkim    if (ResolvedNode->Type == ACPI_TYPE_LOCAL_ALIAS)
558321655Sjkim    {
559321655Sjkim        ResolvedNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
560321655Sjkim            ResolvedNode->Object);
561321655Sjkim    }
562321655Sjkim
563321655Sjkim    /* Update the reference object */
564321655Sjkim
565321655Sjkim    Element->Reference.Resolved = TRUE;
566321655Sjkim    Element->Reference.Node = ResolvedNode;
567321655Sjkim    Type = Element->Reference.Node->Type;
568321655Sjkim
569321655Sjkim    /*
570321655Sjkim     * Attempt to resolve the node to a value before we insert it into
571321655Sjkim     * the package. If this is a reference to a common data type,
572321655Sjkim     * resolve it immediately. According to the ACPI spec, package
573321655Sjkim     * elements can only be "data objects" or method references.
574321655Sjkim     * Attempt to resolve to an Integer, Buffer, String or Package.
575321655Sjkim     * If cannot, return the named reference (for things like Devices,
576321655Sjkim     * Methods, etc.) Buffer Fields and Fields will resolve to simple
577321655Sjkim     * objects (int/buf/str/pkg).
578321655Sjkim     *
579321655Sjkim     * NOTE: References to things like Devices, Methods, Mutexes, etc.
580321655Sjkim     * will remain as named references. This behavior is not described
581321655Sjkim     * in the ACPI spec, but it appears to be an oversight.
582321655Sjkim     */
583327557Sjkim    OriginalNode = ResolvedNode;
584321655Sjkim    Status = AcpiExResolveNodeToValue (&ResolvedNode, NULL);
585321655Sjkim    if (ACPI_FAILURE (Status))
586321655Sjkim    {
587321655Sjkim        return_VOID;
588321655Sjkim    }
589321655Sjkim
590321655Sjkim#if 0
591321655Sjkim/* TBD - alias support */
592321655Sjkim    /*
593321655Sjkim     * Special handling for Alias objects. We need to setup the type
594321655Sjkim     * and the Op->Common.Node to point to the Alias target. Note,
595321655Sjkim     * Alias has at most one level of indirection internally.
596321655Sjkim     */
597321655Sjkim    Type = Op->Common.Node->Type;
598321655Sjkim    if (Type == ACPI_TYPE_LOCAL_ALIAS)
599321655Sjkim    {
600321655Sjkim        Type = ObjDesc->Common.Type;
601321655Sjkim        Op->Common.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
602321655Sjkim            Op->Common.Node->Object);
603321655Sjkim    }
604321655Sjkim#endif
605321655Sjkim
606321655Sjkim    switch (Type)
607321655Sjkim    {
608321655Sjkim    /*
609321655Sjkim     * These object types are a result of named references, so we will
610321655Sjkim     * leave them as reference objects. In other words, these types
611321655Sjkim     * have no intrinsic "value".
612321655Sjkim     */
613321655Sjkim    case ACPI_TYPE_DEVICE:
614321655Sjkim    case ACPI_TYPE_THERMAL:
615327557Sjkim    case ACPI_TYPE_METHOD:
616321655Sjkim        break;
617321655Sjkim
618321655Sjkim    case ACPI_TYPE_MUTEX:
619321655Sjkim    case ACPI_TYPE_POWER:
620321655Sjkim    case ACPI_TYPE_PROCESSOR:
621321655Sjkim    case ACPI_TYPE_EVENT:
622321655Sjkim    case ACPI_TYPE_REGION:
623321655Sjkim
624327557Sjkim        /* AcpiExResolveNodeToValue gave these an extra reference */
625327557Sjkim
626327557Sjkim        AcpiUtRemoveReference (OriginalNode->Object);
627321655Sjkim        break;
628321655Sjkim
629321655Sjkim    default:
630321655Sjkim        /*
631321655Sjkim         * For all other types - the node was resolved to an actual
632327557Sjkim         * operand object with a value, return the object. Remove
633327557Sjkim         * a reference on the existing object.
634321655Sjkim         */
635327557Sjkim        AcpiUtRemoveReference (Element);
636321655Sjkim        *ElementPtr = (ACPI_OPERAND_OBJECT *) ResolvedNode;
637321655Sjkim        break;
638321655Sjkim    }
639321655Sjkim
640321655Sjkim    return_VOID;
641321655Sjkim}
642