1 /******************************************************************************
3 * Module Name: utobject - ACPI object create/delete/size/cache routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2004, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
47 #include <acpi/amlcode.h>
50 #define _COMPONENT ACPI_UTILITIES
51 ACPI_MODULE_NAME ("utobject")
54 /*******************************************************************************
56 * FUNCTION: acpi_ut_create_internal_object_dbg
58 * PARAMETERS: module_name - Source file name of caller
59 * line_number - Line number of caller
60 * component_id - Component type of caller
61 * Type - ACPI Type of the new object
63 * RETURN: Object - The new object. Null on failure
65 * DESCRIPTION: Create and initialize a new internal object.
67 * NOTE: We always allocate the worst-case object descriptor because
68 * these objects are cached, and we want them to be
69 * one-size-satisifies-any-request. This in itself may not be
70 * the most memory efficient, but the efficiency of the object
71 * cache should more than make up for this!
73 ******************************************************************************/
75 union acpi_operand_object *
76 acpi_ut_create_internal_object_dbg (
80 acpi_object_type type)
82 union acpi_operand_object *object;
83 union acpi_operand_object *second_object;
86 ACPI_FUNCTION_TRACE_STR ("ut_create_internal_object_dbg", acpi_ut_get_type_name (type));
89 /* Allocate the raw object descriptor */
91 object = acpi_ut_allocate_object_desc_dbg (module_name, line_number, component_id);
97 case ACPI_TYPE_REGION:
98 case ACPI_TYPE_BUFFER_FIELD:
100 /* These types require a secondary object */
102 second_object = acpi_ut_allocate_object_desc_dbg (module_name, line_number, component_id);
103 if (!second_object) {
104 acpi_ut_delete_object_desc (object);
108 second_object->common.type = ACPI_TYPE_LOCAL_EXTRA;
109 second_object->common.reference_count = 1;
111 /* Link the second object to the first */
113 object->common.next_object = second_object;
117 /* All others have no secondary object */
121 /* Save the object type in the object descriptor */
123 object->common.type = (u8) type;
125 /* Init the reference count */
127 object->common.reference_count = 1;
129 /* Any per-type initialization should go here */
135 /*******************************************************************************
137 * FUNCTION: acpi_ut_create_buffer_object
139 * PARAMETERS: buffer_size - Size of buffer to be created
141 * RETURN: Pointer to a new Buffer object
143 * DESCRIPTION: Create a fully initialized buffer object
145 ******************************************************************************/
147 union acpi_operand_object *
148 acpi_ut_create_buffer_object (
149 acpi_size buffer_size)
151 union acpi_operand_object *buffer_desc;
155 ACPI_FUNCTION_TRACE_U32 ("ut_create_buffer_object", buffer_size);
159 * Create a new Buffer object
161 buffer_desc = acpi_ut_create_internal_object (ACPI_TYPE_BUFFER);
166 /* Create an actual buffer only if size > 0 */
168 if (buffer_size > 0) {
169 /* Allocate the actual buffer */
171 buffer = ACPI_MEM_CALLOCATE (buffer_size);
173 ACPI_REPORT_ERROR (("create_buffer: could not allocate size %X\n",
175 acpi_ut_remove_reference (buffer_desc);
180 /* Complete buffer object initialization */
182 buffer_desc->buffer.flags |= AOPOBJ_DATA_VALID;
183 buffer_desc->buffer.pointer = buffer;
184 buffer_desc->buffer.length = (u32) buffer_size;
186 /* Return the new buffer descriptor */
188 return_PTR (buffer_desc);
192 /*******************************************************************************
194 * FUNCTION: acpi_ut_valid_internal_object
196 * PARAMETERS: Object - Object to be validated
198 * RETURN: Validate a pointer to be an union acpi_operand_object
200 ******************************************************************************/
203 acpi_ut_valid_internal_object (
207 ACPI_FUNCTION_NAME ("ut_valid_internal_object");
210 /* Check for a null pointer */
213 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** Null Object Ptr\n"));
217 /* Check the descriptor type field */
219 switch (ACPI_GET_DESCRIPTOR_TYPE (object)) {
220 case ACPI_DESC_TYPE_OPERAND:
222 /* The object appears to be a valid union acpi_operand_object */
227 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
228 "%p is not not an ACPI operand obj [%s]\n",
229 object, acpi_ut_get_descriptor_name (object)));
237 /*******************************************************************************
239 * FUNCTION: acpi_ut_allocate_object_desc_dbg
241 * PARAMETERS: module_name - Caller's module name (for error output)
242 * line_number - Caller's line number (for error output)
243 * component_id - Caller's component ID (for error output)
245 * RETURN: Pointer to newly allocated object descriptor. Null on error
247 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
250 ******************************************************************************/
253 acpi_ut_allocate_object_desc_dbg (
258 union acpi_operand_object *object;
261 ACPI_FUNCTION_TRACE ("ut_allocate_object_desc_dbg");
264 object = acpi_ut_acquire_from_cache (ACPI_MEM_LIST_OPERAND);
266 _ACPI_REPORT_ERROR (module_name, line_number, component_id,
267 ("Could not allocate an object descriptor\n"));
272 /* Mark the descriptor type */
274 ACPI_SET_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_OPERAND);
276 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
277 object, (u32) sizeof (union acpi_operand_object)));
283 /*******************************************************************************
285 * FUNCTION: acpi_ut_delete_object_desc
287 * PARAMETERS: Object - An Acpi internal object to be deleted
291 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
293 ******************************************************************************/
296 acpi_ut_delete_object_desc (
297 union acpi_operand_object *object)
299 ACPI_FUNCTION_TRACE_PTR ("ut_delete_object_desc", object);
302 /* Object must be an union acpi_operand_object */
304 if (ACPI_GET_DESCRIPTOR_TYPE (object) != ACPI_DESC_TYPE_OPERAND) {
305 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
306 "%p is not an ACPI Operand object [%s]\n", object,
307 acpi_ut_get_descriptor_name (object)));
311 acpi_ut_release_to_cache (ACPI_MEM_LIST_OPERAND, object);
317 /*******************************************************************************
319 * FUNCTION: acpi_ut_delete_object_cache
325 * DESCRIPTION: Purge the global state object cache. Used during subsystem
328 ******************************************************************************/
331 acpi_ut_delete_object_cache (
334 ACPI_FUNCTION_TRACE ("ut_delete_object_cache");
337 acpi_ut_delete_generic_cache (ACPI_MEM_LIST_OPERAND);
342 /*******************************************************************************
344 * FUNCTION: acpi_ut_get_simple_object_size
346 * PARAMETERS: *internal_object - Pointer to the object we are examining
347 * *obj_length - Where the length is returned
351 * DESCRIPTION: This function is called to determine the space required to
352 * contain a simple object for return to an external user.
354 * The length includes the object structure plus any additional
357 ******************************************************************************/
360 acpi_ut_get_simple_object_size (
361 union acpi_operand_object *internal_object,
362 acpi_size *obj_length)
365 acpi_status status = AE_OK;
368 ACPI_FUNCTION_TRACE_PTR ("ut_get_simple_object_size", internal_object);
371 /* Handle a null object (Could be a uninitialized package element -- which is legal) */
373 if (!internal_object) {
375 return_ACPI_STATUS (AE_OK);
378 /* Start with the length of the Acpi object */
380 length = sizeof (union acpi_object);
382 if (ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_NAMED) {
383 /* Object is a named object (reference), just return the length */
385 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
386 return_ACPI_STATUS (status);
390 * The final length depends on the object type
391 * Strings and Buffers are packed right up against the parent object and
392 * must be accessed bytewise or there may be alignment problems on
395 switch (ACPI_GET_OBJECT_TYPE (internal_object)) {
396 case ACPI_TYPE_STRING:
398 length += (acpi_size) internal_object->string.length + 1;
402 case ACPI_TYPE_BUFFER:
404 length += (acpi_size) internal_object->buffer.length;
408 case ACPI_TYPE_INTEGER:
409 case ACPI_TYPE_PROCESSOR:
410 case ACPI_TYPE_POWER:
413 * No extra data for these types
418 case ACPI_TYPE_LOCAL_REFERENCE:
420 switch (internal_object->reference.opcode) {
421 case AML_INT_NAMEPATH_OP:
424 * Get the actual length of the full pathname to this object.
425 * The reference will be converted to the pathname to the object
427 length += ACPI_ROUND_UP_TO_NATIVE_WORD (acpi_ns_get_pathname_length (internal_object->reference.node));
433 * No other reference opcodes are supported.
434 * Notably, Locals and Args are not supported, but this may be
435 * required eventually.
437 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
438 "Unsupported Reference opcode=%X in object %p\n",
439 internal_object->reference.opcode, internal_object));
448 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unsupported type=%X in object %p\n",
449 ACPI_GET_OBJECT_TYPE (internal_object), internal_object));
455 * Account for the space required by the object rounded up to the next
456 * multiple of the machine word size. This keeps each object aligned
457 * on a machine word boundary. (preventing alignment faults on some
460 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
461 return_ACPI_STATUS (status);
465 /*******************************************************************************
467 * FUNCTION: acpi_ut_get_element_length
469 * PARAMETERS: acpi_pkg_callback
473 * DESCRIPTION: Get the length of one package element.
475 ******************************************************************************/
478 acpi_ut_get_element_length (
480 union acpi_operand_object *source_object,
481 union acpi_generic_state *state,
484 acpi_status status = AE_OK;
485 struct acpi_pkg_info *info = (struct acpi_pkg_info *) context;
486 acpi_size object_space;
489 switch (object_type) {
490 case ACPI_COPY_TYPE_SIMPLE:
493 * Simple object - just get the size (Null object/entry is handled
494 * here also) and sum it into the running package length
496 status = acpi_ut_get_simple_object_size (source_object, &object_space);
497 if (ACPI_FAILURE (status)) {
501 info->length += object_space;
505 case ACPI_COPY_TYPE_PACKAGE:
507 /* Package object - nothing much to do here, let the walk handle it */
509 info->num_packages++;
510 state->pkg.this_target_obj = NULL;
516 /* No other types allowed */
518 return (AE_BAD_PARAMETER);
525 /*******************************************************************************
527 * FUNCTION: acpi_ut_get_package_object_size
529 * PARAMETERS: *internal_object - Pointer to the object we are examining
530 * *obj_length - Where the length is returned
534 * DESCRIPTION: This function is called to determine the space required to
535 * contain a package object for return to an external user.
537 * This is moderately complex since a package contains other
538 * objects including packages.
540 ******************************************************************************/
543 acpi_ut_get_package_object_size (
544 union acpi_operand_object *internal_object,
545 acpi_size *obj_length)
548 struct acpi_pkg_info info;
551 ACPI_FUNCTION_TRACE_PTR ("ut_get_package_object_size", internal_object);
555 info.object_space = 0;
556 info.num_packages = 1;
558 status = acpi_ut_walk_package_tree (internal_object, NULL,
559 acpi_ut_get_element_length, &info);
560 if (ACPI_FAILURE (status)) {
561 return_ACPI_STATUS (status);
565 * We have handled all of the objects in all levels of the package.
566 * just add the length of the package objects themselves.
567 * Round up to the next machine word.
569 info.length += ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object)) *
570 (acpi_size) info.num_packages;
572 /* Return the total package length */
574 *obj_length = info.length;
575 return_ACPI_STATUS (status);
579 /*******************************************************************************
581 * FUNCTION: acpi_ut_get_object_size
583 * PARAMETERS: *internal_object - Pointer to the object we are examining
584 * *obj_length - Where the length will be returned
588 * DESCRIPTION: This function is called to determine the space required to
589 * contain an object for return to an API user.
591 ******************************************************************************/
594 acpi_ut_get_object_size(
595 union acpi_operand_object *internal_object,
596 acpi_size *obj_length)
601 ACPI_FUNCTION_ENTRY ();
604 if ((ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_OPERAND) &&
605 (ACPI_GET_OBJECT_TYPE (internal_object) == ACPI_TYPE_PACKAGE)) {
606 status = acpi_ut_get_package_object_size (internal_object, obj_length);
609 status = acpi_ut_get_simple_object_size (internal_object, obj_length);