utobject.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: utobject - ACPI object create/delete/size/cache routines
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acnamesp.h"
  12. #define _COMPONENT ACPI_UTILITIES
  13. ACPI_MODULE_NAME("utobject")
  14. /* Local prototypes */
  15. static acpi_status
  16. acpi_ut_get_simple_object_size(union acpi_operand_object *obj,
  17. acpi_size *obj_length);
  18. static acpi_status
  19. acpi_ut_get_package_object_size(union acpi_operand_object *obj,
  20. acpi_size *obj_length);
  21. static acpi_status
  22. acpi_ut_get_element_length(u8 object_type,
  23. union acpi_operand_object *source_object,
  24. union acpi_generic_state *state, void *context);
  25. /*******************************************************************************
  26. *
  27. * FUNCTION: acpi_ut_create_internal_object_dbg
  28. *
  29. * PARAMETERS: module_name - Source file name of caller
  30. * line_number - Line number of caller
  31. * component_id - Component type of caller
  32. * type - ACPI Type of the new object
  33. *
  34. * RETURN: A new internal object, null on failure
  35. *
  36. * DESCRIPTION: Create and initialize a new internal object.
  37. *
  38. * NOTE: We always allocate the worst-case object descriptor because
  39. * these objects are cached, and we want them to be
  40. * one-size-satisifies-any-request. This in itself may not be
  41. * the most memory efficient, but the efficiency of the object
  42. * cache should more than make up for this!
  43. *
  44. ******************************************************************************/
  45. union acpi_operand_object *acpi_ut_create_internal_object_dbg(const char
  46. *module_name,
  47. u32 line_number,
  48. u32 component_id,
  49. acpi_object_type
  50. type)
  51. {
  52. union acpi_operand_object *object;
  53. union acpi_operand_object *second_object;
  54. ACPI_FUNCTION_TRACE_STR(ut_create_internal_object_dbg,
  55. acpi_ut_get_type_name(type));
  56. /* Allocate the raw object descriptor */
  57. object =
  58. acpi_ut_allocate_object_desc_dbg(module_name, line_number,
  59. component_id);
  60. if (!object) {
  61. return_PTR(NULL);
  62. }
  63. switch (type) {
  64. case ACPI_TYPE_REGION:
  65. case ACPI_TYPE_BUFFER_FIELD:
  66. case ACPI_TYPE_LOCAL_BANK_FIELD:
  67. /* These types require a secondary object */
  68. second_object =
  69. acpi_ut_allocate_object_desc_dbg(module_name, line_number,
  70. component_id);
  71. if (!second_object) {
  72. acpi_ut_delete_object_desc(object);
  73. return_PTR(NULL);
  74. }
  75. second_object->common.type = ACPI_TYPE_LOCAL_EXTRA;
  76. second_object->common.reference_count = 1;
  77. /* Link the second object to the first */
  78. object->common.next_object = second_object;
  79. break;
  80. default:
  81. /* All others have no secondary object */
  82. break;
  83. }
  84. /* Save the object type in the object descriptor */
  85. object->common.type = (u8) type;
  86. /* Init the reference count */
  87. object->common.reference_count = 1;
  88. /* Any per-type initialization should go here */
  89. return_PTR(object);
  90. }
  91. /*******************************************************************************
  92. *
  93. * FUNCTION: acpi_ut_create_package_object
  94. *
  95. * PARAMETERS: count - Number of package elements
  96. *
  97. * RETURN: Pointer to a new Package object, null on failure
  98. *
  99. * DESCRIPTION: Create a fully initialized package object
  100. *
  101. ******************************************************************************/
  102. union acpi_operand_object *acpi_ut_create_package_object(u32 count)
  103. {
  104. union acpi_operand_object *package_desc;
  105. union acpi_operand_object **package_elements;
  106. ACPI_FUNCTION_TRACE_U32(ut_create_package_object, count);
  107. /* Create a new Package object */
  108. package_desc = acpi_ut_create_internal_object(ACPI_TYPE_PACKAGE);
  109. if (!package_desc) {
  110. return_PTR(NULL);
  111. }
  112. /*
  113. * Create the element array. Count+1 allows the array to be null
  114. * terminated.
  115. */
  116. package_elements = ACPI_ALLOCATE_ZEROED(((acpi_size)count +
  117. 1) * sizeof(void *));
  118. if (!package_elements) {
  119. ACPI_FREE(package_desc);
  120. return_PTR(NULL);
  121. }
  122. package_desc->package.count = count;
  123. package_desc->package.elements = package_elements;
  124. return_PTR(package_desc);
  125. }
  126. /*******************************************************************************
  127. *
  128. * FUNCTION: acpi_ut_create_integer_object
  129. *
  130. * PARAMETERS: initial_value - Initial value for the integer
  131. *
  132. * RETURN: Pointer to a new Integer object, null on failure
  133. *
  134. * DESCRIPTION: Create an initialized integer object
  135. *
  136. ******************************************************************************/
  137. union acpi_operand_object *acpi_ut_create_integer_object(u64 initial_value)
  138. {
  139. union acpi_operand_object *integer_desc;
  140. ACPI_FUNCTION_TRACE(ut_create_integer_object);
  141. /* Create and initialize a new integer object */
  142. integer_desc = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
  143. if (!integer_desc) {
  144. return_PTR(NULL);
  145. }
  146. integer_desc->integer.value = initial_value;
  147. return_PTR(integer_desc);
  148. }
  149. /*******************************************************************************
  150. *
  151. * FUNCTION: acpi_ut_create_buffer_object
  152. *
  153. * PARAMETERS: buffer_size - Size of buffer to be created
  154. *
  155. * RETURN: Pointer to a new Buffer object, null on failure
  156. *
  157. * DESCRIPTION: Create a fully initialized buffer object
  158. *
  159. ******************************************************************************/
  160. union acpi_operand_object *acpi_ut_create_buffer_object(acpi_size buffer_size)
  161. {
  162. union acpi_operand_object *buffer_desc;
  163. u8 *buffer = NULL;
  164. ACPI_FUNCTION_TRACE_U32(ut_create_buffer_object, buffer_size);
  165. /* Create a new Buffer object */
  166. buffer_desc = acpi_ut_create_internal_object(ACPI_TYPE_BUFFER);
  167. if (!buffer_desc) {
  168. return_PTR(NULL);
  169. }
  170. /* Create an actual buffer only if size > 0 */
  171. if (buffer_size > 0) {
  172. /* Allocate the actual buffer */
  173. buffer = ACPI_ALLOCATE_ZEROED(buffer_size);
  174. if (!buffer) {
  175. ACPI_ERROR((AE_INFO, "Could not allocate size %u",
  176. (u32)buffer_size));
  177. acpi_ut_remove_reference(buffer_desc);
  178. return_PTR(NULL);
  179. }
  180. }
  181. /* Complete buffer object initialization */
  182. buffer_desc->buffer.flags |= AOPOBJ_DATA_VALID;
  183. buffer_desc->buffer.pointer = buffer;
  184. buffer_desc->buffer.length = (u32) buffer_size;
  185. /* Return the new buffer descriptor */
  186. return_PTR(buffer_desc);
  187. }
  188. /*******************************************************************************
  189. *
  190. * FUNCTION: acpi_ut_create_string_object
  191. *
  192. * PARAMETERS: string_size - Size of string to be created. Does not
  193. * include NULL terminator, this is added
  194. * automatically.
  195. *
  196. * RETURN: Pointer to a new String object
  197. *
  198. * DESCRIPTION: Create a fully initialized string object
  199. *
  200. ******************************************************************************/
  201. union acpi_operand_object *acpi_ut_create_string_object(acpi_size string_size)
  202. {
  203. union acpi_operand_object *string_desc;
  204. char *string;
  205. ACPI_FUNCTION_TRACE_U32(ut_create_string_object, string_size);
  206. /* Create a new String object */
  207. string_desc = acpi_ut_create_internal_object(ACPI_TYPE_STRING);
  208. if (!string_desc) {
  209. return_PTR(NULL);
  210. }
  211. /*
  212. * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
  213. * NOTE: Zero-length strings are NULL terminated
  214. */
  215. string = ACPI_ALLOCATE_ZEROED(string_size + 1);
  216. if (!string) {
  217. ACPI_ERROR((AE_INFO, "Could not allocate size %u",
  218. (u32)string_size));
  219. acpi_ut_remove_reference(string_desc);
  220. return_PTR(NULL);
  221. }
  222. /* Complete string object initialization */
  223. string_desc->string.pointer = string;
  224. string_desc->string.length = (u32) string_size;
  225. /* Return the new string descriptor */
  226. return_PTR(string_desc);
  227. }
  228. /*******************************************************************************
  229. *
  230. * FUNCTION: acpi_ut_valid_internal_object
  231. *
  232. * PARAMETERS: object - Object to be validated
  233. *
  234. * RETURN: TRUE if object is valid, FALSE otherwise
  235. *
  236. * DESCRIPTION: Validate a pointer to be of type union acpi_operand_object
  237. *
  238. ******************************************************************************/
  239. u8 acpi_ut_valid_internal_object(void *object)
  240. {
  241. ACPI_FUNCTION_NAME(ut_valid_internal_object);
  242. /* Check for a null pointer */
  243. if (!object) {
  244. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "**** Null Object Ptr\n"));
  245. return (FALSE);
  246. }
  247. /* Check the descriptor type field */
  248. switch (ACPI_GET_DESCRIPTOR_TYPE(object)) {
  249. case ACPI_DESC_TYPE_OPERAND:
  250. /* The object appears to be a valid union acpi_operand_object */
  251. return (TRUE);
  252. default:
  253. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  254. "%p is not an ACPI operand obj [%s]\n",
  255. object, acpi_ut_get_descriptor_name(object)));
  256. break;
  257. }
  258. return (FALSE);
  259. }
  260. /*******************************************************************************
  261. *
  262. * FUNCTION: acpi_ut_allocate_object_desc_dbg
  263. *
  264. * PARAMETERS: module_name - Caller's module name (for error output)
  265. * line_number - Caller's line number (for error output)
  266. * component_id - Caller's component ID (for error output)
  267. *
  268. * RETURN: Pointer to newly allocated object descriptor. Null on error
  269. *
  270. * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
  271. * error conditions.
  272. *
  273. ******************************************************************************/
  274. void *acpi_ut_allocate_object_desc_dbg(const char *module_name,
  275. u32 line_number, u32 component_id)
  276. {
  277. union acpi_operand_object *object;
  278. ACPI_FUNCTION_TRACE(ut_allocate_object_desc_dbg);
  279. object = acpi_os_acquire_object(acpi_gbl_operand_cache);
  280. if (!object) {
  281. ACPI_ERROR((module_name, line_number,
  282. "Could not allocate an object descriptor"));
  283. return_PTR(NULL);
  284. }
  285. /* Mark the descriptor type */
  286. ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_OPERAND);
  287. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
  288. object, (u32) sizeof(union acpi_operand_object)));
  289. return_PTR(object);
  290. }
  291. /*******************************************************************************
  292. *
  293. * FUNCTION: acpi_ut_delete_object_desc
  294. *
  295. * PARAMETERS: object - An Acpi internal object to be deleted
  296. *
  297. * RETURN: None.
  298. *
  299. * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
  300. *
  301. ******************************************************************************/
  302. void acpi_ut_delete_object_desc(union acpi_operand_object *object)
  303. {
  304. ACPI_FUNCTION_TRACE_PTR(ut_delete_object_desc, object);
  305. /* Object must be of type union acpi_operand_object */
  306. if (ACPI_GET_DESCRIPTOR_TYPE(object) != ACPI_DESC_TYPE_OPERAND) {
  307. ACPI_ERROR((AE_INFO,
  308. "%p is not an ACPI Operand object [%s]", object,
  309. acpi_ut_get_descriptor_name(object)));
  310. return_VOID;
  311. }
  312. (void)acpi_os_release_object(acpi_gbl_operand_cache, object);
  313. return_VOID;
  314. }
  315. /*******************************************************************************
  316. *
  317. * FUNCTION: acpi_ut_get_simple_object_size
  318. *
  319. * PARAMETERS: internal_object - An ACPI operand object
  320. * obj_length - Where the length is returned
  321. *
  322. * RETURN: Status
  323. *
  324. * DESCRIPTION: This function is called to determine the space required to
  325. * contain a simple object for return to an external user.
  326. *
  327. * The length includes the object structure plus any additional
  328. * needed space.
  329. *
  330. ******************************************************************************/
  331. static acpi_status
  332. acpi_ut_get_simple_object_size(union acpi_operand_object *internal_object,
  333. acpi_size *obj_length)
  334. {
  335. acpi_size length;
  336. acpi_size size;
  337. acpi_status status = AE_OK;
  338. ACPI_FUNCTION_TRACE_PTR(ut_get_simple_object_size, internal_object);
  339. /* Start with the length of the (external) Acpi object */
  340. length = sizeof(union acpi_object);
  341. /* A NULL object is allowed, can be a legal uninitialized package element */
  342. if (!internal_object) {
  343. /*
  344. * Object is NULL, just return the length of union acpi_object
  345. * (A NULL union acpi_object is an object of all zeroes.)
  346. */
  347. *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD(length);
  348. return_ACPI_STATUS(AE_OK);
  349. }
  350. /* A Namespace Node should never appear here */
  351. if (ACPI_GET_DESCRIPTOR_TYPE(internal_object) == ACPI_DESC_TYPE_NAMED) {
  352. /* A namespace node should never get here */
  353. ACPI_ERROR((AE_INFO,
  354. "Received a namespace node [%4.4s] "
  355. "where an operand object is required",
  356. ACPI_CAST_PTR(struct acpi_namespace_node,
  357. internal_object)->name.ascii));
  358. return_ACPI_STATUS(AE_AML_INTERNAL);
  359. }
  360. /*
  361. * The final length depends on the object type
  362. * Strings and Buffers are packed right up against the parent object and
  363. * must be accessed bytewise or there may be alignment problems on
  364. * certain processors
  365. */
  366. switch (internal_object->common.type) {
  367. case ACPI_TYPE_STRING:
  368. length += (acpi_size)internal_object->string.length + 1;
  369. break;
  370. case ACPI_TYPE_BUFFER:
  371. length += (acpi_size)internal_object->buffer.length;
  372. break;
  373. case ACPI_TYPE_INTEGER:
  374. case ACPI_TYPE_PROCESSOR:
  375. case ACPI_TYPE_POWER:
  376. /* No extra data for these types */
  377. break;
  378. case ACPI_TYPE_LOCAL_REFERENCE:
  379. switch (internal_object->reference.class) {
  380. case ACPI_REFCLASS_NAME:
  381. /*
  382. * Get the actual length of the full pathname to this object.
  383. * The reference will be converted to the pathname to the object
  384. */
  385. size =
  386. acpi_ns_get_pathname_length(internal_object->
  387. reference.node);
  388. if (!size) {
  389. return_ACPI_STATUS(AE_BAD_PARAMETER);
  390. }
  391. length += ACPI_ROUND_UP_TO_NATIVE_WORD(size);
  392. break;
  393. default:
  394. /*
  395. * No other reference opcodes are supported.
  396. * Notably, Locals and Args are not supported, but this may be
  397. * required eventually.
  398. */
  399. ACPI_ERROR((AE_INFO,
  400. "Cannot convert to external object - "
  401. "unsupported Reference Class [%s] 0x%X in object %p",
  402. acpi_ut_get_reference_name(internal_object),
  403. internal_object->reference.class,
  404. internal_object));
  405. status = AE_TYPE;
  406. break;
  407. }
  408. break;
  409. default:
  410. ACPI_ERROR((AE_INFO, "Cannot convert to external object - "
  411. "unsupported type [%s] 0x%X in object %p",
  412. acpi_ut_get_object_type_name(internal_object),
  413. internal_object->common.type, internal_object));
  414. status = AE_TYPE;
  415. break;
  416. }
  417. /*
  418. * Account for the space required by the object rounded up to the next
  419. * multiple of the machine word size. This keeps each object aligned
  420. * on a machine word boundary. (preventing alignment faults on some
  421. * machines.)
  422. */
  423. *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD(length);
  424. return_ACPI_STATUS(status);
  425. }
  426. /*******************************************************************************
  427. *
  428. * FUNCTION: acpi_ut_get_element_length
  429. *
  430. * PARAMETERS: acpi_pkg_callback
  431. *
  432. * RETURN: Status
  433. *
  434. * DESCRIPTION: Get the length of one package element.
  435. *
  436. ******************************************************************************/
  437. static acpi_status
  438. acpi_ut_get_element_length(u8 object_type,
  439. union acpi_operand_object *source_object,
  440. union acpi_generic_state *state, void *context)
  441. {
  442. acpi_status status = AE_OK;
  443. struct acpi_pkg_info *info = (struct acpi_pkg_info *)context;
  444. acpi_size object_space;
  445. switch (object_type) {
  446. case ACPI_COPY_TYPE_SIMPLE:
  447. /*
  448. * Simple object - just get the size (Null object/entry is handled
  449. * here also) and sum it into the running package length
  450. */
  451. status =
  452. acpi_ut_get_simple_object_size(source_object,
  453. &object_space);
  454. if (ACPI_FAILURE(status)) {
  455. return (status);
  456. }
  457. info->length += object_space;
  458. break;
  459. case ACPI_COPY_TYPE_PACKAGE:
  460. /* Package object - nothing much to do here, let the walk handle it */
  461. info->num_packages++;
  462. state->pkg.this_target_obj = NULL;
  463. break;
  464. default:
  465. /* No other types allowed */
  466. return (AE_BAD_PARAMETER);
  467. }
  468. return (status);
  469. }
  470. /*******************************************************************************
  471. *
  472. * FUNCTION: acpi_ut_get_package_object_size
  473. *
  474. * PARAMETERS: internal_object - An ACPI internal object
  475. * obj_length - Where the length is returned
  476. *
  477. * RETURN: Status
  478. *
  479. * DESCRIPTION: This function is called to determine the space required to
  480. * contain a package object for return to an external user.
  481. *
  482. * This is moderately complex since a package contains other
  483. * objects including packages.
  484. *
  485. ******************************************************************************/
  486. static acpi_status
  487. acpi_ut_get_package_object_size(union acpi_operand_object *internal_object,
  488. acpi_size *obj_length)
  489. {
  490. acpi_status status;
  491. struct acpi_pkg_info info;
  492. ACPI_FUNCTION_TRACE_PTR(ut_get_package_object_size, internal_object);
  493. info.length = 0;
  494. info.object_space = 0;
  495. info.num_packages = 1;
  496. status =
  497. acpi_ut_walk_package_tree(internal_object, NULL,
  498. acpi_ut_get_element_length, &info);
  499. if (ACPI_FAILURE(status)) {
  500. return_ACPI_STATUS(status);
  501. }
  502. /*
  503. * We have handled all of the objects in all levels of the package.
  504. * just add the length of the package objects themselves.
  505. * Round up to the next machine word.
  506. */
  507. info.length +=
  508. ACPI_ROUND_UP_TO_NATIVE_WORD(sizeof(union acpi_object)) *
  509. (acpi_size)info.num_packages;
  510. /* Return the total package length */
  511. *obj_length = info.length;
  512. return_ACPI_STATUS(status);
  513. }
  514. /*******************************************************************************
  515. *
  516. * FUNCTION: acpi_ut_get_object_size
  517. *
  518. * PARAMETERS: internal_object - An ACPI internal object
  519. * obj_length - Where the length will be returned
  520. *
  521. * RETURN: Status
  522. *
  523. * DESCRIPTION: This function is called to determine the space required to
  524. * contain an object for return to an API user.
  525. *
  526. ******************************************************************************/
  527. acpi_status
  528. acpi_ut_get_object_size(union acpi_operand_object *internal_object,
  529. acpi_size *obj_length)
  530. {
  531. acpi_status status;
  532. ACPI_FUNCTION_ENTRY();
  533. if ((ACPI_GET_DESCRIPTOR_TYPE(internal_object) ==
  534. ACPI_DESC_TYPE_OPERAND) &&
  535. (internal_object->common.type == ACPI_TYPE_PACKAGE)) {
  536. status =
  537. acpi_ut_get_package_object_size(internal_object,
  538. obj_length);
  539. } else {
  540. status =
  541. acpi_ut_get_simple_object_size(internal_object, obj_length);
  542. }
  543. return (status);
  544. }