utobject.c 19 KB

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