]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/acpi/acpica/dspkginit.c
ACPICA: Integrate package handling with module-level code
[linux.git] / drivers / acpi / acpica / dspkginit.c
1 /******************************************************************************
2  *
3  * Module Name: dspkginit - Completion of deferred package initialization
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2018, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "amlcode.h"
48 #include "acdispat.h"
49 #include "acinterp.h"
50 #include "acparser.h"
51
52 #define _COMPONENT          ACPI_NAMESPACE
53 ACPI_MODULE_NAME("dspkginit")
54
55 /* Local prototypes */
56 static void
57 acpi_ds_resolve_package_element(union acpi_operand_object **element);
58
59 /*******************************************************************************
60  *
61  * FUNCTION:    acpi_ds_build_internal_package_obj
62  *
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
68  *
69  * RETURN:      Status
70  *
71  * DESCRIPTION: Translate a parser Op package object to the equivalent
72  *              namespace object
73  *
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.)
79  *
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.
85  *
86  ******************************************************************************/
87
88 acpi_status
89 acpi_ds_build_internal_package_obj(struct acpi_walk_state *walk_state,
90                                    union acpi_parse_object *op,
91                                    u32 element_count,
92                                    union acpi_operand_object **obj_desc_ptr)
93 {
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;
99         u16 reference_count;
100         u32 index;
101         u32 i;
102
103         ACPI_FUNCTION_TRACE(ds_build_internal_package_obj);
104
105         /* Check if we are executing module level code */
106
107         if (walk_state->parse_flags & ACPI_PARSE_MODULE_LEVEL) {
108                 module_level_code = TRUE;
109         }
110
111         /* Find the parent of a possibly nested package */
112
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;
117         }
118
119         /*
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.
123          */
124         obj_desc = *obj_desc_ptr;
125         if (!obj_desc) {
126                 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_PACKAGE);
127                 *obj_desc_ptr = obj_desc;
128                 if (!obj_desc) {
129                         return_ACPI_STATUS(AE_NO_MEMORY);
130                 }
131
132                 obj_desc->package.node = parent->common.node;
133         }
134
135         if (obj_desc->package.flags & AOPOBJ_DATA_VALID) {      /* Just in case */
136                 return_ACPI_STATUS(AE_OK);
137         }
138
139         /*
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
143          * null terminated.
144          */
145         if (!obj_desc->package.elements) {
146                 obj_desc->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
147                                                                    element_count
148                                                                    +
149                                                                    1) *
150                                                                   sizeof(void
151                                                                          *));
152
153                 if (!obj_desc->package.elements) {
154                         acpi_ut_delete_object_desc(obj_desc);
155                         return_ACPI_STATUS(AE_NO_MEMORY);
156                 }
157
158                 obj_desc->package.count = element_count;
159         }
160
161         /* First arg is element count. Second arg begins the initializer list */
162
163         arg = op->common.value.arg;
164         arg = arg->common.next;
165
166         /*
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.
171          */
172         if (module_level_code) {
173                 obj_desc->package.aml_start = walk_state->aml;
174                 obj_desc->package.aml_length = 0;
175
176                 ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
177                                       "%s: Deferring resolution of Package elements\n",
178                                       ACPI_GET_FUNCTION_NAME));
179         }
180
181         /*
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.
186          */
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) {
190                                 /*
191                                  * A method reference "looks" to the parser to be a method
192                                  * invocation, so we special case it here
193                                  */
194                                 arg->common.aml_opcode = AML_INT_NAMEPATH_OP;
195                                 status =
196                                     acpi_ds_build_internal_object(walk_state,
197                                                                   arg,
198                                                                   &obj_desc->
199                                                                   package.
200                                                                   elements[i]);
201                         } else {
202                                 /* This package element is already built, just get it */
203
204                                 obj_desc->package.elements[i] =
205                                     ACPI_CAST_PTR(union acpi_operand_object,
206                                                   arg->common.node);
207                         }
208                 } else {
209                         status =
210                             acpi_ds_build_internal_object(walk_state, arg,
211                                                           &obj_desc->package.
212                                                           elements[i]);
213                         if (status == AE_NOT_FOUND) {
214                                 ACPI_ERROR((AE_INFO, "%-48s",
215                                             "****DS namepath not found"));
216                         }
217
218                         if (!module_level_code) {
219                                 /*
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.
224                                  */
225                                 acpi_ds_init_package_element(0,
226                                                              obj_desc->package.
227                                                              elements[i], NULL,
228                                                              &obj_desc->package.
229                                                              elements[i]);
230                         }
231                 }
232
233                 if (*obj_desc_ptr) {
234
235                         /* Existing package, get existing reference count */
236
237                         reference_count =
238                             (*obj_desc_ptr)->common.reference_count;
239                         if (reference_count > 1) {
240
241                                 /* Make new element ref count match original ref count */
242                                 /* TBD: Probably need an acpi_ut_add_references function */
243
244                                 for (index = 0;
245                                      index < ((u32)reference_count - 1);
246                                      index++) {
247                                         acpi_ut_add_reference((obj_desc->
248                                                                package.
249                                                                elements[i]));
250                                 }
251                         }
252                 }
253
254                 arg = arg->common.next;
255         }
256
257         /* Check for match between num_elements and actual length of package_list */
258
259         if (arg) {
260                 /*
261                  * num_elements was exhausted, but there are remaining elements in
262                  * the package_list. Truncate the package to num_elements.
263                  *
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.
271                  */
272                 while (arg) {
273                         /*
274                          * We must delete any package elements that were created earlier
275                          * and are not going to be used because of the package truncation.
276                          */
277                         if (arg->common.node) {
278                                 acpi_ut_remove_reference(ACPI_CAST_PTR
279                                                          (union
280                                                           acpi_operand_object,
281                                                           arg->common.node));
282                                 arg->common.node = NULL;
283                         }
284
285                         /* Find out how many elements there really are */
286
287                         i++;
288                         arg = arg->common.next;
289                 }
290
291                 ACPI_INFO(("Actual Package length (%u) is larger than "
292                            "NumElements field (%u), truncated",
293                            i, element_count));
294         } else if (i < element_count) {
295                 /*
296                  * Arg list (elements) was exhausted, but we did not reach
297                  * num_elements count.
298                  *
299                  * Note: this is not an error, the package is padded out
300                  * with NULLs as per the ACPI specification.
301                  */
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,
306                                       element_count));
307         }
308
309         /* Module-level packages will be resolved later */
310
311         if (!module_level_code) {
312                 obj_desc->package.flags |= AOPOBJ_DATA_VALID;
313         }
314
315         op->common.node = ACPI_CAST_PTR(struct acpi_namespace_node, obj_desc);
316         return_ACPI_STATUS(status);
317 }
318
319 /*******************************************************************************
320  *
321  * FUNCTION:    acpi_ds_init_package_element
322  *
323  * PARAMETERS:  acpi_pkg_callback
324  *
325  * RETURN:      Status
326  *
327  * DESCRIPTION: Resolve a named reference element within a package object
328  *
329  ******************************************************************************/
330
331 acpi_status
332 acpi_ds_init_package_element(u8 object_type,
333                              union acpi_operand_object *source_object,
334                              union acpi_generic_state *state, void *context)
335 {
336         union acpi_operand_object **element_ptr;
337
338         ACPI_FUNCTION_TRACE(ds_init_package_element);
339
340         if (!source_object) {
341                 return_ACPI_STATUS(AE_OK);
342         }
343
344         /*
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.
349          */
350         if (context) {
351
352                 /* A direct call was made to this function */
353
354                 element_ptr = (union acpi_operand_object **)context;
355         } else {
356                 /* Call came from acpi_ut_walk_package_tree */
357
358                 element_ptr = state->pkg.this_target_obj;
359         }
360
361         /* We are only interested in reference objects/elements */
362
363         if (source_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
364
365                 /* Attempt to resolve the (named) reference to a namespace node */
366
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;
370         }
371
372         return_ACPI_STATUS(AE_OK);
373 }
374
375 /*******************************************************************************
376  *
377  * FUNCTION:    acpi_ds_resolve_package_element
378  *
379  * PARAMETERS:  element_ptr         - Pointer to a reference object
380  *
381  * RETURN:      Possible new element is stored to the indirect element_ptr
382  *
383  * DESCRIPTION: Resolve a package element that is a reference to a named
384  *              object.
385  *
386  ******************************************************************************/
387
388 static void
389 acpi_ds_resolve_package_element(union acpi_operand_object **element_ptr)
390 {
391         acpi_status status;
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;
398
399         ACPI_FUNCTION_TRACE(ds_resolve_package_element);
400
401         /* Check if reference element is already resolved */
402
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));
407
408                 return_VOID;
409         }
410
411         /* Element must be a reference object of correct type */
412
413         scope_info.scope.node = element->reference.node;        /* Prefix node */
414
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);
423
424                 ACPI_EXCEPTION((AE_INFO, status,
425                                 "Could not find/resolve named package element: %s",
426                                 external_path));
427
428                 /* Not found, set the element to NULL */
429
430                 ACPI_FREE(external_path);
431                 acpi_ut_remove_reference(*element_ptr);
432                 *element_ptr = NULL;
433                 return_VOID;
434         } else if (resolved_node->type == ACPI_TYPE_ANY) {
435
436                 /* Named reference not resolved, return a NULL package element */
437
438                 ACPI_ERROR((AE_INFO,
439                             "Could not resolve named package element [%4.4s] in [%4.4s]",
440                             resolved_node->name.ascii,
441                             scope_info.scope.node->name.ascii));
442                 *element_ptr = NULL;
443                 return_VOID;
444         }
445
446         /*
447          * Special handling for Alias objects. We need resolved_node to point
448          * to the Alias target. This effectively "resolves" the alias.
449          */
450         if (resolved_node->type == ACPI_TYPE_LOCAL_ALIAS) {
451                 resolved_node = ACPI_CAST_PTR(struct acpi_namespace_node,
452                                               resolved_node->object);
453         }
454
455         /* Update the reference object */
456
457         element->reference.resolved = TRUE;
458         element->reference.node = resolved_node;
459         type = element->reference.node->type;
460
461         /*
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).
470          *
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.
474          */
475         original_node = resolved_node;
476         status = acpi_ex_resolve_node_to_value(&resolved_node, NULL);
477         if (ACPI_FAILURE(status)) {
478                 return_VOID;
479         }
480
481         switch (type) {
482                 /*
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".
486                  */
487         case ACPI_TYPE_DEVICE:
488         case ACPI_TYPE_THERMAL:
489         case ACPI_TYPE_METHOD:
490                 break;
491
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:
497
498                 /* acpi_ex_resolve_node_to_value gave these an extra reference */
499
500                 acpi_ut_remove_reference(original_node->object);
501                 break;
502
503         default:
504                 /*
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.
508                  */
509                 acpi_ut_remove_reference(element);
510                 *element_ptr = (union acpi_operand_object *)resolved_node;
511                 break;
512         }
513
514         return_VOID;
515 }