1 /******************************************************************************
3 * Module Name: dspkginit - Completion of deferred package initialization
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2018, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <acpi/acpi.h>
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME("dspkginit")
55 /* Local prototypes */
57 acpi_ds_resolve_package_element(union acpi_operand_object **element);
59 /*******************************************************************************
61 * FUNCTION: acpi_ds_build_internal_package_obj
63 * PARAMETERS: walk_state - Current walk state
64 * op - Parser object to be translated
65 * element_count - Number of elements in the package - this is
66 * the num_elements argument to Package()
67 * obj_desc_ptr - Where the ACPI internal object is returned
71 * DESCRIPTION: Translate a parser Op package object to the equivalent
74 * NOTE: The number of elements in the package will be always be the num_elements
75 * count, regardless of the number of elements in the package list. If
76 * num_elements is smaller, only that many package list elements are used.
77 * if num_elements is larger, the Package object is padded out with
78 * objects of type Uninitialized (as per ACPI spec.)
80 * Even though the ASL compilers do not allow num_elements to be smaller
81 * than the Package list length (for the fixed length package opcode), some
82 * BIOS code modifies the AML on the fly to adjust the num_elements, and
83 * this code compensates for that. This also provides compatibility with
84 * other AML interpreters.
86 ******************************************************************************/
89 acpi_ds_build_internal_package_obj(struct acpi_walk_state *walk_state,
90 union acpi_parse_object *op,
92 union acpi_operand_object **obj_desc_ptr)
94 union acpi_parse_object *arg;
95 union acpi_parse_object *parent;
96 union acpi_operand_object *obj_desc = NULL;
97 acpi_status status = AE_OK;
98 u8 module_level_code = FALSE;
103 ACPI_FUNCTION_TRACE(ds_build_internal_package_obj);
105 /* Check if we are executing module level code */
107 if (walk_state->parse_flags & ACPI_PARSE_MODULE_LEVEL) {
108 module_level_code = TRUE;
111 /* Find the parent of a possibly nested package */
113 parent = op->common.parent;
114 while ((parent->common.aml_opcode == AML_PACKAGE_OP) ||
115 (parent->common.aml_opcode == AML_VARIABLE_PACKAGE_OP)) {
116 parent = parent->common.parent;
120 * If we are evaluating a Named package object of the form:
121 * Name (xxxx, Package)
122 * the package object already exists, otherwise it must be created.
124 obj_desc = *obj_desc_ptr;
126 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_PACKAGE);
127 *obj_desc_ptr = obj_desc;
129 return_ACPI_STATUS(AE_NO_MEMORY);
132 obj_desc->package.node = parent->common.node;
135 if (obj_desc->package.flags & AOPOBJ_DATA_VALID) { /* Just in case */
136 return_ACPI_STATUS(AE_OK);
140 * Allocate the element array (array of pointers to the individual
141 * objects) if necessary. the count is based on the num_elements
142 * parameter. Add an extra pointer slot so that the list is always
145 if (!obj_desc->package.elements) {
146 obj_desc->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
153 if (!obj_desc->package.elements) {
154 acpi_ut_delete_object_desc(obj_desc);
155 return_ACPI_STATUS(AE_NO_MEMORY);
158 obj_desc->package.count = element_count;
161 /* First arg is element count. Second arg begins the initializer list */
163 arg = op->common.value.arg;
164 arg = arg->common.next;
167 * If we are executing module-level code, we will defer the
168 * full resolution of the package elements in order to support
169 * forward references from the elements. This provides
170 * compatibility with other ACPI implementations.
172 if (module_level_code) {
173 obj_desc->package.aml_start = walk_state->aml;
174 obj_desc->package.aml_length = 0;
176 ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
177 "%s: Deferring resolution of Package elements\n",
178 ACPI_GET_FUNCTION_NAME));
182 * Initialize the elements of the package, up to the num_elements count.
183 * Package is automatically padded with uninitialized (NULL) elements
184 * if num_elements is greater than the package list length. Likewise,
185 * Package is truncated if num_elements is less than the list length.
187 for (i = 0; arg && (i < element_count); i++) {
188 if (arg->common.aml_opcode == AML_INT_RETURN_VALUE_OP) {
189 if (arg->common.node->type == ACPI_TYPE_METHOD) {
191 * A method reference "looks" to the parser to be a method
192 * invocation, so we special case it here
194 arg->common.aml_opcode = AML_INT_NAMEPATH_OP;
196 acpi_ds_build_internal_object(walk_state,
202 /* This package element is already built, just get it */
204 obj_desc->package.elements[i] =
205 ACPI_CAST_PTR(union acpi_operand_object,
210 acpi_ds_build_internal_object(walk_state, arg,
213 if (status == AE_NOT_FOUND) {
214 ACPI_ERROR((AE_INFO, "%-48s",
215 "****DS namepath not found"));
218 if (!module_level_code) {
220 * Initialize this package element. This function handles the
221 * resolution of named references within the package.
222 * Forward references from module-level code are deferred
223 * until all ACPI tables are loaded.
225 acpi_ds_init_package_element(0,
235 /* Existing package, get existing reference count */
238 (*obj_desc_ptr)->common.reference_count;
239 if (reference_count > 1) {
241 /* Make new element ref count match original ref count */
242 /* TBD: Probably need an acpi_ut_add_references function */
245 index < ((u32)reference_count - 1);
247 acpi_ut_add_reference((obj_desc->
254 arg = arg->common.next;
257 /* Check for match between num_elements and actual length of package_list */
261 * num_elements was exhausted, but there are remaining elements in
262 * the package_list. Truncate the package to num_elements.
264 * Note: technically, this is an error, from ACPI spec: "It is an
265 * error for NumElements to be less than the number of elements in
266 * the PackageList". However, we just print a message and no
267 * exception is returned. This provides compatibility with other
268 * ACPI implementations. Some firmware implementations will alter
269 * the num_elements on the fly, possibly creating this type of
270 * ill-formed package object.
274 * We must delete any package elements that were created earlier
275 * and are not going to be used because of the package truncation.
277 if (arg->common.node) {
278 acpi_ut_remove_reference(ACPI_CAST_PTR
282 arg->common.node = NULL;
285 /* Find out how many elements there really are */
288 arg = arg->common.next;
291 ACPI_INFO(("Actual Package length (%u) is larger than "
292 "NumElements field (%u), truncated",
294 } else if (i < element_count) {
296 * Arg list (elements) was exhausted, but we did not reach
297 * num_elements count.
299 * Note: this is not an error, the package is padded out
300 * with NULLs as per the ACPI specification.
302 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
303 "%s: Package List length (%u) smaller than NumElements "
304 "count (%u), padded with null elements\n",
305 ACPI_GET_FUNCTION_NAME, i,
309 /* Module-level packages will be resolved later */
311 if (!module_level_code) {
312 obj_desc->package.flags |= AOPOBJ_DATA_VALID;
315 op->common.node = ACPI_CAST_PTR(struct acpi_namespace_node, obj_desc);
316 return_ACPI_STATUS(status);
319 /*******************************************************************************
321 * FUNCTION: acpi_ds_init_package_element
323 * PARAMETERS: acpi_pkg_callback
327 * DESCRIPTION: Resolve a named reference element within a package object
329 ******************************************************************************/
332 acpi_ds_init_package_element(u8 object_type,
333 union acpi_operand_object *source_object,
334 union acpi_generic_state *state, void *context)
336 union acpi_operand_object **element_ptr;
338 ACPI_FUNCTION_TRACE(ds_init_package_element);
340 if (!source_object) {
341 return_ACPI_STATUS(AE_OK);
345 * The following code is a bit of a hack to workaround a (current)
346 * limitation of the acpi_pkg_callback interface. We need a pointer
347 * to the location within the element array because a new object
348 * may be created and stored there.
352 /* A direct call was made to this function */
354 element_ptr = (union acpi_operand_object **)context;
356 /* Call came from acpi_ut_walk_package_tree */
358 element_ptr = state->pkg.this_target_obj;
361 /* We are only interested in reference objects/elements */
363 if (source_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
365 /* Attempt to resolve the (named) reference to a namespace node */
367 acpi_ds_resolve_package_element(element_ptr);
368 } else if (source_object->common.type == ACPI_TYPE_PACKAGE) {
369 source_object->package.flags |= AOPOBJ_DATA_VALID;
372 return_ACPI_STATUS(AE_OK);
375 /*******************************************************************************
377 * FUNCTION: acpi_ds_resolve_package_element
379 * PARAMETERS: element_ptr - Pointer to a reference object
381 * RETURN: Possible new element is stored to the indirect element_ptr
383 * DESCRIPTION: Resolve a package element that is a reference to a named
386 ******************************************************************************/
389 acpi_ds_resolve_package_element(union acpi_operand_object **element_ptr)
392 union acpi_generic_state scope_info;
393 union acpi_operand_object *element = *element_ptr;
394 struct acpi_namespace_node *resolved_node;
395 struct acpi_namespace_node *original_node;
396 char *external_path = NULL;
397 acpi_object_type type;
399 ACPI_FUNCTION_TRACE(ds_resolve_package_element);
401 /* Check if reference element is already resolved */
403 if (element->reference.resolved) {
404 ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
405 "%s: Package element is already resolved\n",
406 ACPI_GET_FUNCTION_NAME));
411 /* Element must be a reference object of correct type */
413 scope_info.scope.node = element->reference.node; /* Prefix node */
415 status = acpi_ns_lookup(&scope_info, (char *)element->reference.aml, /* Pointer to AML path */
416 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
417 ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
418 NULL, &resolved_node);
419 if (ACPI_FAILURE(status)) {
420 status = acpi_ns_externalize_name(ACPI_UINT32_MAX,
421 (char *)element->reference.
422 aml, NULL, &external_path);
424 ACPI_EXCEPTION((AE_INFO, status,
425 "Could not find/resolve named package element: %s",
428 /* Not found, set the element to NULL */
430 ACPI_FREE(external_path);
431 acpi_ut_remove_reference(*element_ptr);
434 } else if (resolved_node->type == ACPI_TYPE_ANY) {
436 /* Named reference not resolved, return a NULL package element */
439 "Could not resolve named package element [%4.4s] in [%4.4s]",
440 resolved_node->name.ascii,
441 scope_info.scope.node->name.ascii));
447 * Special handling for Alias objects. We need resolved_node to point
448 * to the Alias target. This effectively "resolves" the alias.
450 if (resolved_node->type == ACPI_TYPE_LOCAL_ALIAS) {
451 resolved_node = ACPI_CAST_PTR(struct acpi_namespace_node,
452 resolved_node->object);
455 /* Update the reference object */
457 element->reference.resolved = TRUE;
458 element->reference.node = resolved_node;
459 type = element->reference.node->type;
462 * Attempt to resolve the node to a value before we insert it into
463 * the package. If this is a reference to a common data type,
464 * resolve it immediately. According to the ACPI spec, package
465 * elements can only be "data objects" or method references.
466 * Attempt to resolve to an Integer, Buffer, String or Package.
467 * If cannot, return the named reference (for things like Devices,
468 * Methods, etc.) Buffer Fields and Fields will resolve to simple
469 * objects (int/buf/str/pkg).
471 * NOTE: References to things like Devices, Methods, Mutexes, etc.
472 * will remain as named references. This behavior is not described
473 * in the ACPI spec, but it appears to be an oversight.
475 original_node = resolved_node;
476 status = acpi_ex_resolve_node_to_value(&resolved_node, NULL);
477 if (ACPI_FAILURE(status)) {
483 * These object types are a result of named references, so we will
484 * leave them as reference objects. In other words, these types
485 * have no intrinsic "value".
487 case ACPI_TYPE_DEVICE:
488 case ACPI_TYPE_THERMAL:
489 case ACPI_TYPE_METHOD:
492 case ACPI_TYPE_MUTEX:
493 case ACPI_TYPE_POWER:
494 case ACPI_TYPE_PROCESSOR:
495 case ACPI_TYPE_EVENT:
496 case ACPI_TYPE_REGION:
498 /* acpi_ex_resolve_node_to_value gave these an extra reference */
500 acpi_ut_remove_reference(original_node->object);
505 * For all other types - the node was resolved to an actual
506 * operand object with a value, return the object. Remove
507 * a reference on the existing object.
509 acpi_ut_remove_reference(element);
510 *element_ptr = (union acpi_operand_object *)resolved_node;