Sync ACPICA with Intel's version 20180508 (from previously 20170831).
[dragonfly.git] / sys / contrib / dev / acpica / source / components / dispatcher / dspkginit.c
blob9e1cd6115fd4bf09562c2e8573615fba44d416d1
1 /******************************************************************************
3 * Module Name: dspkginit - Completion of deferred package initialization
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
121 * are met:
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acnamesp.h"
155 #include "amlcode.h"
156 #include "acdispat.h"
157 #include "acinterp.h"
158 #include "acparser.h"
161 #define _COMPONENT ACPI_NAMESPACE
162 ACPI_MODULE_NAME ("dspkginit")
165 /* Local prototypes */
167 static void
168 AcpiDsResolvePackageElement (
169 ACPI_OPERAND_OBJECT **Element);
172 /*******************************************************************************
174 * FUNCTION: AcpiDsBuildInternalPackageObj
176 * PARAMETERS: WalkState - Current walk state
177 * Op - Parser object to be translated
178 * ElementCount - Number of elements in the package - this is
179 * the NumElements argument to Package()
180 * ObjDescPtr - Where the ACPI internal object is returned
182 * RETURN: Status
184 * DESCRIPTION: Translate a parser Op package object to the equivalent
185 * namespace object
187 * NOTE: The number of elements in the package will be always be the NumElements
188 * count, regardless of the number of elements in the package list. If
189 * NumElements is smaller, only that many package list elements are used.
190 * if NumElements is larger, the Package object is padded out with
191 * objects of type Uninitialized (as per ACPI spec.)
193 * Even though the ASL compilers do not allow NumElements to be smaller
194 * than the Package list length (for the fixed length package opcode), some
195 * BIOS code modifies the AML on the fly to adjust the NumElements, and
196 * this code compensates for that. This also provides compatibility with
197 * other AML interpreters.
199 ******************************************************************************/
201 ACPI_STATUS
202 AcpiDsBuildInternalPackageObj (
203 ACPI_WALK_STATE *WalkState,
204 ACPI_PARSE_OBJECT *Op,
205 UINT32 ElementCount,
206 ACPI_OPERAND_OBJECT **ObjDescPtr)
208 ACPI_PARSE_OBJECT *Arg;
209 ACPI_PARSE_OBJECT *Parent;
210 ACPI_OPERAND_OBJECT *ObjDesc = NULL;
211 ACPI_STATUS Status = AE_OK;
212 BOOLEAN ModuleLevelCode = FALSE;
213 UINT16 ReferenceCount;
214 UINT32 Index;
215 UINT32 i;
218 ACPI_FUNCTION_TRACE (DsBuildInternalPackageObj);
221 /* Check if we are executing module level code */
223 if (WalkState->ParseFlags & ACPI_PARSE_MODULE_LEVEL)
225 ModuleLevelCode = TRUE;
228 /* Find the parent of a possibly nested package */
230 Parent = Op->Common.Parent;
231 while ((Parent->Common.AmlOpcode == AML_PACKAGE_OP) ||
232 (Parent->Common.AmlOpcode == AML_VARIABLE_PACKAGE_OP))
234 Parent = Parent->Common.Parent;
238 * If we are evaluating a Named package object of the form:
239 * Name (xxxx, Package)
240 * the package object already exists, otherwise it must be created.
242 ObjDesc = *ObjDescPtr;
243 if (!ObjDesc)
245 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_PACKAGE);
246 *ObjDescPtr = ObjDesc;
247 if (!ObjDesc)
249 return_ACPI_STATUS (AE_NO_MEMORY);
252 ObjDesc->Package.Node = Parent->Common.Node;
255 if (ObjDesc->Package.Flags & AOPOBJ_DATA_VALID) /* Just in case */
257 return_ACPI_STATUS (AE_OK);
261 * Allocate the element array (array of pointers to the individual
262 * objects) if necessary. the count is based on the NumElements
263 * parameter. Add an extra pointer slot so that the list is always
264 * null terminated.
266 if (!ObjDesc->Package.Elements)
268 ObjDesc->Package.Elements = ACPI_ALLOCATE_ZEROED (
269 ((ACPI_SIZE) ElementCount + 1) * sizeof (void *));
271 if (!ObjDesc->Package.Elements)
273 AcpiUtDeleteObjectDesc (ObjDesc);
274 return_ACPI_STATUS (AE_NO_MEMORY);
277 ObjDesc->Package.Count = ElementCount;
280 /* First arg is element count. Second arg begins the initializer list */
282 Arg = Op->Common.Value.Arg;
283 Arg = Arg->Common.Next;
286 * If we are executing module-level code, we will defer the
287 * full resolution of the package elements in order to support
288 * forward references from the elements. This provides
289 * compatibility with other ACPI implementations.
291 if (ModuleLevelCode)
293 ObjDesc->Package.AmlStart = WalkState->Aml;
294 ObjDesc->Package.AmlLength = 0;
296 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_PARSE,
297 "%s: Deferring resolution of Package elements\n",
298 ACPI_GET_FUNCTION_NAME));
302 * Initialize the elements of the package, up to the NumElements count.
303 * Package is automatically padded with uninitialized (NULL) elements
304 * if NumElements is greater than the package list length. Likewise,
305 * Package is truncated if NumElements is less than the list length.
307 for (i = 0; Arg && (i < ElementCount); i++)
309 if (Arg->Common.AmlOpcode == AML_INT_RETURN_VALUE_OP)
311 if (Arg->Common.Node->Type == ACPI_TYPE_METHOD)
314 * A method reference "looks" to the parser to be a method
315 * invocation, so we special case it here
317 Arg->Common.AmlOpcode = AML_INT_NAMEPATH_OP;
318 Status = AcpiDsBuildInternalObject (
319 WalkState, Arg, &ObjDesc->Package.Elements[i]);
321 else
323 /* This package element is already built, just get it */
325 ObjDesc->Package.Elements[i] =
326 ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Arg->Common.Node);
329 else
331 Status = AcpiDsBuildInternalObject (
332 WalkState, Arg, &ObjDesc->Package.Elements[i]);
333 if (Status == AE_NOT_FOUND)
335 ACPI_ERROR ((AE_INFO, "%-48s", "****DS namepath not found"));
338 if (!ModuleLevelCode)
341 * Initialize this package element. This function handles the
342 * resolution of named references within the package.
343 * Forward references from module-level code are deferred
344 * until all ACPI tables are loaded.
346 AcpiDsInitPackageElement (0, ObjDesc->Package.Elements[i],
347 NULL, &ObjDesc->Package.Elements[i]);
351 if (*ObjDescPtr)
353 /* Existing package, get existing reference count */
355 ReferenceCount = (*ObjDescPtr)->Common.ReferenceCount;
356 if (ReferenceCount > 1)
358 /* Make new element ref count match original ref count */
359 /* TBD: Probably need an AcpiUtAddReferences function */
361 for (Index = 0; Index < ((UINT32) ReferenceCount - 1); Index++)
363 AcpiUtAddReference ((ObjDesc->Package.Elements[i]));
368 Arg = Arg->Common.Next;
371 /* Check for match between NumElements and actual length of PackageList */
373 if (Arg)
376 * NumElements was exhausted, but there are remaining elements in
377 * the PackageList. Truncate the package to NumElements.
379 * Note: technically, this is an error, from ACPI spec: "It is an
380 * error for NumElements to be less than the number of elements in
381 * the PackageList". However, we just print a message and no
382 * exception is returned. This provides compatibility with other
383 * ACPI implementations. Some firmware implementations will alter
384 * the NumElements on the fly, possibly creating this type of
385 * ill-formed package object.
387 while (Arg)
390 * We must delete any package elements that were created earlier
391 * and are not going to be used because of the package truncation.
393 if (Arg->Common.Node)
395 AcpiUtRemoveReference (
396 ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Arg->Common.Node));
397 Arg->Common.Node = NULL;
400 /* Find out how many elements there really are */
402 i++;
403 Arg = Arg->Common.Next;
406 ACPI_INFO ((
407 "Actual Package length (%u) is larger than "
408 "NumElements field (%u), truncated",
409 i, ElementCount));
411 else if (i < ElementCount)
414 * Arg list (elements) was exhausted, but we did not reach
415 * NumElements count.
417 * Note: this is not an error, the package is padded out
418 * with NULLs as per the ACPI specification.
420 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_INFO,
421 "%s: Package List length (%u) smaller than NumElements "
422 "count (%u), padded with null elements\n",
423 ACPI_GET_FUNCTION_NAME, i, ElementCount));
426 /* Module-level packages will be resolved later */
428 if (!ModuleLevelCode)
430 ObjDesc->Package.Flags |= AOPOBJ_DATA_VALID;
433 Op->Common.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, ObjDesc);
434 return_ACPI_STATUS (Status);
438 /*******************************************************************************
440 * FUNCTION: AcpiDsInitPackageElement
442 * PARAMETERS: ACPI_PKG_CALLBACK
444 * RETURN: Status
446 * DESCRIPTION: Resolve a named reference element within a package object
448 ******************************************************************************/
450 ACPI_STATUS
451 AcpiDsInitPackageElement (
452 UINT8 ObjectType,
453 ACPI_OPERAND_OBJECT *SourceObject,
454 ACPI_GENERIC_STATE *State,
455 void *Context)
457 ACPI_OPERAND_OBJECT **ElementPtr;
460 ACPI_FUNCTION_TRACE (DsInitPackageElement);
463 if (!SourceObject)
465 return_ACPI_STATUS (AE_OK);
469 * The following code is a bit of a hack to workaround a (current)
470 * limitation of the ACPI_PKG_CALLBACK interface. We need a pointer
471 * to the location within the element array because a new object
472 * may be created and stored there.
474 if (Context)
476 /* A direct call was made to this function */
478 ElementPtr = (ACPI_OPERAND_OBJECT **) Context;
480 else
482 /* Call came from AcpiUtWalkPackageTree */
484 ElementPtr = State->Pkg.ThisTargetObj;
487 /* We are only interested in reference objects/elements */
489 if (SourceObject->Common.Type == ACPI_TYPE_LOCAL_REFERENCE)
491 /* Attempt to resolve the (named) reference to a namespace node */
493 AcpiDsResolvePackageElement (ElementPtr);
495 else if (SourceObject->Common.Type == ACPI_TYPE_PACKAGE)
497 SourceObject->Package.Flags |= AOPOBJ_DATA_VALID;
500 return_ACPI_STATUS (AE_OK);
504 /*******************************************************************************
506 * FUNCTION: AcpiDsResolvePackageElement
508 * PARAMETERS: ElementPtr - Pointer to a reference object
510 * RETURN: Possible new element is stored to the indirect ElementPtr
512 * DESCRIPTION: Resolve a package element that is a reference to a named
513 * object.
515 ******************************************************************************/
517 static void
518 AcpiDsResolvePackageElement (
519 ACPI_OPERAND_OBJECT **ElementPtr)
521 ACPI_STATUS Status;
522 ACPI_STATUS Status2;
523 ACPI_GENERIC_STATE ScopeInfo;
524 ACPI_OPERAND_OBJECT *Element = *ElementPtr;
525 ACPI_NAMESPACE_NODE *ResolvedNode;
526 ACPI_NAMESPACE_NODE *OriginalNode;
527 char *ExternalPath = "";
528 ACPI_OBJECT_TYPE Type;
531 ACPI_FUNCTION_TRACE (DsResolvePackageElement);
534 /* Check if reference element is already resolved */
536 if (Element->Reference.Resolved)
538 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_PARSE,
539 "%s: Package element is already resolved\n",
540 ACPI_GET_FUNCTION_NAME));
542 return_VOID;
545 /* Element must be a reference object of correct type */
547 ScopeInfo.Scope.Node = Element->Reference.Node; /* Prefix node */
549 Status = AcpiNsLookup (&ScopeInfo, (char *) Element->Reference.Aml,
550 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
551 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
552 NULL, &ResolvedNode);
553 if (ACPI_FAILURE (Status))
555 if ((Status == AE_NOT_FOUND) && AcpiGbl_IgnorePackageResolutionErrors)
558 * Optionally be silent about the NOT_FOUND case for the referenced
559 * name. Although this is potentially a serious problem,
560 * it can generate a lot of noise/errors on platforms whose
561 * firmware carries around a bunch of unused Package objects.
562 * To disable these errors, set this global to TRUE:
563 * AcpiGbl_IgnorePackageResolutionErrors
565 * If the AML actually tries to use such a package, the unresolved
566 * element(s) will be replaced with NULL elements.
569 /* Referenced name not found, set the element to NULL */
571 AcpiUtRemoveReference (*ElementPtr);
572 *ElementPtr = NULL;
573 return_VOID;
576 Status2 = AcpiNsExternalizeName (ACPI_UINT32_MAX,
577 (char *) Element->Reference.Aml, NULL, &ExternalPath);
579 ACPI_EXCEPTION ((AE_INFO, Status,
580 "While resolving a named reference package element - %s",
581 ExternalPath));
582 if (ACPI_SUCCESS (Status2))
584 ACPI_FREE (ExternalPath);
587 /* Could not resolve name, set the element to NULL */
589 AcpiUtRemoveReference (*ElementPtr);
590 *ElementPtr = NULL;
591 return_VOID;
593 else if (ResolvedNode->Type == ACPI_TYPE_ANY)
595 /* Named reference not resolved, return a NULL package element */
597 ACPI_ERROR ((AE_INFO,
598 "Could not resolve named package element [%4.4s] in [%4.4s]",
599 ResolvedNode->Name.Ascii, ScopeInfo.Scope.Node->Name.Ascii));
600 *ElementPtr = NULL;
601 return_VOID;
605 * Special handling for Alias objects. We need ResolvedNode to point
606 * to the Alias target. This effectively "resolves" the alias.
608 if (ResolvedNode->Type == ACPI_TYPE_LOCAL_ALIAS)
610 ResolvedNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
611 ResolvedNode->Object);
614 /* Update the reference object */
616 Element->Reference.Resolved = TRUE;
617 Element->Reference.Node = ResolvedNode;
618 Type = Element->Reference.Node->Type;
621 * Attempt to resolve the node to a value before we insert it into
622 * the package. If this is a reference to a common data type,
623 * resolve it immediately. According to the ACPI spec, package
624 * elements can only be "data objects" or method references.
625 * Attempt to resolve to an Integer, Buffer, String or Package.
626 * If cannot, return the named reference (for things like Devices,
627 * Methods, etc.) Buffer Fields and Fields will resolve to simple
628 * objects (int/buf/str/pkg).
630 * NOTE: References to things like Devices, Methods, Mutexes, etc.
631 * will remain as named references. This behavior is not described
632 * in the ACPI spec, but it appears to be an oversight.
634 OriginalNode = ResolvedNode;
635 Status = AcpiExResolveNodeToValue (&ResolvedNode, NULL);
636 if (ACPI_FAILURE (Status))
638 return_VOID;
641 switch (Type)
644 * These object types are a result of named references, so we will
645 * leave them as reference objects. In other words, these types
646 * have no intrinsic "value".
648 case ACPI_TYPE_DEVICE:
649 case ACPI_TYPE_THERMAL:
650 case ACPI_TYPE_METHOD:
651 break;
653 case ACPI_TYPE_MUTEX:
654 case ACPI_TYPE_POWER:
655 case ACPI_TYPE_PROCESSOR:
656 case ACPI_TYPE_EVENT:
657 case ACPI_TYPE_REGION:
659 /* AcpiExResolveNodeToValue gave these an extra reference */
661 AcpiUtRemoveReference (OriginalNode->Object);
662 break;
664 default:
666 * For all other types - the node was resolved to an actual
667 * operand object with a value, return the object. Remove
668 * a reference on the existing object.
670 AcpiUtRemoveReference (Element);
671 *ElementPtr = (ACPI_OPERAND_OBJECT *) ResolvedNode;
672 break;
675 return_VOID;