dspkginit.c 16 KB

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