dspkginit.c 15 KB

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