utobject.c 19 KB

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