utobject.c 18 KB

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