utobject.c 18 KB

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