utobject.c 18 KB

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