dspkginit.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /******************************************************************************
  2. *
  3. * Module Name: dspkginit - Completion of deferred package initialization
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2018, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #include "amlcode.h"
  46. #include "acdispat.h"
  47. #include "acinterp.h"
  48. #include "acparser.h"
  49. #define _COMPONENT ACPI_NAMESPACE
  50. ACPI_MODULE_NAME("dspkginit")
  51. /* Local prototypes */
  52. static void
  53. acpi_ds_resolve_package_element(union acpi_operand_object **element);
  54. /*******************************************************************************
  55. *
  56. * FUNCTION: acpi_ds_build_internal_package_obj
  57. *
  58. * PARAMETERS: walk_state - Current walk state
  59. * op - Parser object to be translated
  60. * element_count - Number of elements in the package - this is
  61. * the num_elements argument to Package()
  62. * obj_desc_ptr - Where the ACPI internal object is returned
  63. *
  64. * RETURN: Status
  65. *
  66. * DESCRIPTION: Translate a parser Op package object to the equivalent
  67. * namespace object
  68. *
  69. * NOTE: The number of elements in the package will be always be the num_elements
  70. * count, regardless of the number of elements in the package list. If
  71. * num_elements is smaller, only that many package list elements are used.
  72. * if num_elements is larger, the Package object is padded out with
  73. * objects of type Uninitialized (as per ACPI spec.)
  74. *
  75. * Even though the ASL compilers do not allow num_elements to be smaller
  76. * than the Package list length (for the fixed length package opcode), some
  77. * BIOS code modifies the AML on the fly to adjust the num_elements, and
  78. * this code compensates for that. This also provides compatibility with
  79. * other AML interpreters.
  80. *
  81. ******************************************************************************/
  82. acpi_status
  83. acpi_ds_build_internal_package_obj(struct acpi_walk_state *walk_state,
  84. union acpi_parse_object *op,
  85. u32 element_count,
  86. union acpi_operand_object **obj_desc_ptr)
  87. {
  88. union acpi_parse_object *arg;
  89. union acpi_parse_object *parent;
  90. union acpi_operand_object *obj_desc = NULL;
  91. acpi_status status = AE_OK;
  92. u8 module_level_code = FALSE;
  93. u16 reference_count;
  94. u32 index;
  95. u32 i;
  96. ACPI_FUNCTION_TRACE(ds_build_internal_package_obj);
  97. /* Check if we are executing module level code */
  98. if (walk_state->parse_flags & ACPI_PARSE_MODULE_LEVEL) {
  99. module_level_code = TRUE;
  100. }
  101. /* Find the parent of a possibly nested package */
  102. parent = op->common.parent;
  103. while ((parent->common.aml_opcode == AML_PACKAGE_OP) ||
  104. (parent->common.aml_opcode == AML_VARIABLE_PACKAGE_OP)) {
  105. parent = parent->common.parent;
  106. }
  107. /*
  108. * If we are evaluating a Named package object of the form:
  109. * Name (xxxx, Package)
  110. * the package object already exists, otherwise it must be created.
  111. */
  112. obj_desc = *obj_desc_ptr;
  113. if (!obj_desc) {
  114. obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_PACKAGE);
  115. *obj_desc_ptr = obj_desc;
  116. if (!obj_desc) {
  117. return_ACPI_STATUS(AE_NO_MEMORY);
  118. }
  119. obj_desc->package.node = parent->common.node;
  120. }
  121. if (obj_desc->package.flags & AOPOBJ_DATA_VALID) { /* Just in case */
  122. return_ACPI_STATUS(AE_OK);
  123. }
  124. /*
  125. * Allocate the element array (array of pointers to the individual
  126. * objects) if necessary. the count is based on the num_elements
  127. * parameter. Add an extra pointer slot so that the list is always
  128. * null terminated.
  129. */
  130. if (!obj_desc->package.elements) {
  131. obj_desc->package.elements = ACPI_ALLOCATE_ZEROED(((acpi_size)
  132. element_count
  133. +
  134. 1) *
  135. sizeof(void
  136. *));
  137. if (!obj_desc->package.elements) {
  138. acpi_ut_delete_object_desc(obj_desc);
  139. return_ACPI_STATUS(AE_NO_MEMORY);
  140. }
  141. obj_desc->package.count = element_count;
  142. }
  143. /* First arg is element count. Second arg begins the initializer list */
  144. arg = op->common.value.arg;
  145. arg = arg->common.next;
  146. /*
  147. * If we are executing module-level code, we will defer the
  148. * full resolution of the package elements in order to support
  149. * forward references from the elements. This provides
  150. * compatibility with other ACPI implementations.
  151. */
  152. if (module_level_code) {
  153. obj_desc->package.aml_start = walk_state->aml;
  154. obj_desc->package.aml_length = 0;
  155. ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
  156. "%s: Deferring resolution of Package elements\n",
  157. ACPI_GET_FUNCTION_NAME));
  158. }
  159. /*
  160. * Initialize the elements of the package, up to the num_elements count.
  161. * Package is automatically padded with uninitialized (NULL) elements
  162. * if num_elements is greater than the package list length. Likewise,
  163. * Package is truncated if num_elements is less than the list length.
  164. */
  165. for (i = 0; arg && (i < element_count); i++) {
  166. if (arg->common.aml_opcode == AML_INT_RETURN_VALUE_OP) {
  167. if (arg->common.node->type == ACPI_TYPE_METHOD) {
  168. /*
  169. * A method reference "looks" to the parser to be a method
  170. * invocation, so we special case it here
  171. */
  172. arg->common.aml_opcode = AML_INT_NAMEPATH_OP;
  173. status =
  174. acpi_ds_build_internal_object(walk_state,
  175. arg,
  176. &obj_desc->
  177. package.
  178. elements[i]);
  179. } else {
  180. /* This package element is already built, just get it */
  181. obj_desc->package.elements[i] =
  182. ACPI_CAST_PTR(union acpi_operand_object,
  183. arg->common.node);
  184. }
  185. } else {
  186. status =
  187. acpi_ds_build_internal_object(walk_state, arg,
  188. &obj_desc->package.
  189. elements[i]);
  190. if (status == AE_NOT_FOUND) {
  191. ACPI_ERROR((AE_INFO, "%-48s",
  192. "****DS namepath not found"));
  193. }
  194. if (!module_level_code) {
  195. /*
  196. * Initialize this package element. This function handles the
  197. * resolution of named references within the package.
  198. * Forward references from module-level code are deferred
  199. * until all ACPI tables are loaded.
  200. */
  201. acpi_ds_init_package_element(0,
  202. obj_desc->package.
  203. elements[i], NULL,
  204. &obj_desc->package.
  205. elements[i]);
  206. }
  207. }
  208. if (*obj_desc_ptr) {
  209. /* Existing package, get existing reference count */
  210. reference_count =
  211. (*obj_desc_ptr)->common.reference_count;
  212. if (reference_count > 1) {
  213. /* Make new element ref count match original ref count */
  214. /* TBD: Probably need an acpi_ut_add_references function */
  215. for (index = 0;
  216. index < ((u32)reference_count - 1);
  217. index++) {
  218. acpi_ut_add_reference((obj_desc->
  219. package.
  220. elements[i]));
  221. }
  222. }
  223. }
  224. arg = arg->common.next;
  225. }
  226. /* Check for match between num_elements and actual length of package_list */
  227. if (arg) {
  228. /*
  229. * num_elements was exhausted, but there are remaining elements in
  230. * the package_list. Truncate the package to num_elements.
  231. *
  232. * Note: technically, this is an error, from ACPI spec: "It is an
  233. * error for NumElements to be less than the number of elements in
  234. * the PackageList". However, we just print a message and no
  235. * exception is returned. This provides compatibility with other
  236. * ACPI implementations. Some firmware implementations will alter
  237. * the num_elements on the fly, possibly creating this type of
  238. * ill-formed package object.
  239. */
  240. while (arg) {
  241. /*
  242. * We must delete any package elements that were created earlier
  243. * and are not going to be used because of the package truncation.
  244. */
  245. if (arg->common.node) {
  246. acpi_ut_remove_reference(ACPI_CAST_PTR
  247. (union
  248. acpi_operand_object,
  249. arg->common.node));
  250. arg->common.node = NULL;
  251. }
  252. /* Find out how many elements there really are */
  253. i++;
  254. arg = arg->common.next;
  255. }
  256. ACPI_INFO(("Actual Package length (%u) is larger than "
  257. "NumElements field (%u), truncated",
  258. i, element_count));
  259. } else if (i < element_count) {
  260. /*
  261. * Arg list (elements) was exhausted, but we did not reach
  262. * num_elements count.
  263. *
  264. * Note: this is not an error, the package is padded out
  265. * with NULLs as per the ACPI specification.
  266. */
  267. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
  268. "%s: Package List length (%u) smaller than NumElements "
  269. "count (%u), padded with null elements\n",
  270. ACPI_GET_FUNCTION_NAME, i,
  271. element_count));
  272. }
  273. /* Module-level packages will be resolved later */
  274. if (!module_level_code) {
  275. obj_desc->package.flags |= AOPOBJ_DATA_VALID;
  276. }
  277. op->common.node = ACPI_CAST_PTR(struct acpi_namespace_node, obj_desc);
  278. return_ACPI_STATUS(status);
  279. }
  280. /*******************************************************************************
  281. *
  282. * FUNCTION: acpi_ds_init_package_element
  283. *
  284. * PARAMETERS: acpi_pkg_callback
  285. *
  286. * RETURN: Status
  287. *
  288. * DESCRIPTION: Resolve a named reference element within a package object
  289. *
  290. ******************************************************************************/
  291. acpi_status
  292. acpi_ds_init_package_element(u8 object_type,
  293. union acpi_operand_object *source_object,
  294. union acpi_generic_state *state, void *context)
  295. {
  296. union acpi_operand_object **element_ptr;
  297. ACPI_FUNCTION_TRACE(ds_init_package_element);
  298. if (!source_object) {
  299. return_ACPI_STATUS(AE_OK);
  300. }
  301. /*
  302. * The following code is a bit of a hack to workaround a (current)
  303. * limitation of the acpi_pkg_callback interface. We need a pointer
  304. * to the location within the element array because a new object
  305. * may be created and stored there.
  306. */
  307. if (context) {
  308. /* A direct call was made to this function */
  309. element_ptr = (union acpi_operand_object **)context;
  310. } else {
  311. /* Call came from acpi_ut_walk_package_tree */
  312. element_ptr = state->pkg.this_target_obj;
  313. }
  314. /* We are only interested in reference objects/elements */
  315. if (source_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
  316. /* Attempt to resolve the (named) reference to a namespace node */
  317. acpi_ds_resolve_package_element(element_ptr);
  318. } else if (source_object->common.type == ACPI_TYPE_PACKAGE) {
  319. source_object->package.flags |= AOPOBJ_DATA_VALID;
  320. }
  321. return_ACPI_STATUS(AE_OK);
  322. }
  323. /*******************************************************************************
  324. *
  325. * FUNCTION: acpi_ds_resolve_package_element
  326. *
  327. * PARAMETERS: element_ptr - Pointer to a reference object
  328. *
  329. * RETURN: Possible new element is stored to the indirect element_ptr
  330. *
  331. * DESCRIPTION: Resolve a package element that is a reference to a named
  332. * object.
  333. *
  334. ******************************************************************************/
  335. static void
  336. acpi_ds_resolve_package_element(union acpi_operand_object **element_ptr)
  337. {
  338. acpi_status status;
  339. union acpi_generic_state scope_info;
  340. union acpi_operand_object *element = *element_ptr;
  341. struct acpi_namespace_node *resolved_node;
  342. struct acpi_namespace_node *original_node;
  343. char *external_path = NULL;
  344. acpi_object_type type;
  345. ACPI_FUNCTION_TRACE(ds_resolve_package_element);
  346. /* Check if reference element is already resolved */
  347. if (element->reference.resolved) {
  348. ACPI_DEBUG_PRINT_RAW((ACPI_DB_PARSE,
  349. "%s: Package element is already resolved\n",
  350. ACPI_GET_FUNCTION_NAME));
  351. return_VOID;
  352. }
  353. /* Element must be a reference object of correct type */
  354. scope_info.scope.node = element->reference.node; /* Prefix node */
  355. status = acpi_ns_lookup(&scope_info, (char *)element->reference.aml, /* Pointer to AML path */
  356. ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
  357. ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
  358. NULL, &resolved_node);
  359. if (ACPI_FAILURE(status)) {
  360. status = acpi_ns_externalize_name(ACPI_UINT32_MAX,
  361. (char *)element->reference.
  362. aml, NULL, &external_path);
  363. ACPI_EXCEPTION((AE_INFO, status,
  364. "Could not find/resolve named package element: %s",
  365. external_path));
  366. /* Not found, set the element to NULL */
  367. ACPI_FREE(external_path);
  368. acpi_ut_remove_reference(*element_ptr);
  369. *element_ptr = NULL;
  370. return_VOID;
  371. } else if (resolved_node->type == ACPI_TYPE_ANY) {
  372. /* Named reference not resolved, return a NULL package element */
  373. ACPI_ERROR((AE_INFO,
  374. "Could not resolve named package element [%4.4s] in [%4.4s]",
  375. resolved_node->name.ascii,
  376. scope_info.scope.node->name.ascii));
  377. *element_ptr = NULL;
  378. return_VOID;
  379. }
  380. /*
  381. * Special handling for Alias objects. We need resolved_node to point
  382. * to the Alias target. This effectively "resolves" the alias.
  383. */
  384. if (resolved_node->type == ACPI_TYPE_LOCAL_ALIAS) {
  385. resolved_node = ACPI_CAST_PTR(struct acpi_namespace_node,
  386. resolved_node->object);
  387. }
  388. /* Update the reference object */
  389. element->reference.resolved = TRUE;
  390. element->reference.node = resolved_node;
  391. type = element->reference.node->type;
  392. /*
  393. * Attempt to resolve the node to a value before we insert it into
  394. * the package. If this is a reference to a common data type,
  395. * resolve it immediately. According to the ACPI spec, package
  396. * elements can only be "data objects" or method references.
  397. * Attempt to resolve to an Integer, Buffer, String or Package.
  398. * If cannot, return the named reference (for things like Devices,
  399. * Methods, etc.) Buffer Fields and Fields will resolve to simple
  400. * objects (int/buf/str/pkg).
  401. *
  402. * NOTE: References to things like Devices, Methods, Mutexes, etc.
  403. * will remain as named references. This behavior is not described
  404. * in the ACPI spec, but it appears to be an oversight.
  405. */
  406. original_node = resolved_node;
  407. status = acpi_ex_resolve_node_to_value(&resolved_node, NULL);
  408. if (ACPI_FAILURE(status)) {
  409. return_VOID;
  410. }
  411. switch (type) {
  412. /*
  413. * These object types are a result of named references, so we will
  414. * leave them as reference objects. In other words, these types
  415. * have no intrinsic "value".
  416. */
  417. case ACPI_TYPE_DEVICE:
  418. case ACPI_TYPE_THERMAL:
  419. case ACPI_TYPE_METHOD:
  420. break;
  421. case ACPI_TYPE_MUTEX:
  422. case ACPI_TYPE_POWER:
  423. case ACPI_TYPE_PROCESSOR:
  424. case ACPI_TYPE_EVENT:
  425. case ACPI_TYPE_REGION:
  426. /* acpi_ex_resolve_node_to_value gave these an extra reference */
  427. acpi_ut_remove_reference(original_node->object);
  428. break;
  429. default:
  430. /*
  431. * For all other types - the node was resolved to an actual
  432. * operand object with a value, return the object. Remove
  433. * a reference on the existing object.
  434. */
  435. acpi_ut_remove_reference(element);
  436. *element_ptr = (union acpi_operand_object *)resolved_node;
  437. break;
  438. }
  439. return_VOID;
  440. }