1 /******************************************************************************
3 * Module Name: utobject - ACPI object create/delete/size/cache routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2005, 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")
53 /* Local prototypes */
56 acpi_ut_get_simple_object_size (
57 union acpi_operand_object *obj,
58 acpi_size *obj_length);
61 acpi_ut_get_package_object_size (
62 union acpi_operand_object *obj,
63 acpi_size *obj_length);
66 acpi_ut_get_element_length (
68 union acpi_operand_object *source_object,
69 union acpi_generic_state *state,
73 /*******************************************************************************
75 * FUNCTION: acpi_ut_create_internal_object_dbg
77 * PARAMETERS: module_name - Source file name of caller
78 * line_number - Line number of caller
79 * component_id - Component type of caller
80 * Type - ACPI Type of the new object
82 * RETURN: A new internal object, null on failure
84 * DESCRIPTION: Create and initialize a new internal object.
86 * NOTE: We always allocate the worst-case object descriptor because
87 * these objects are cached, and we want them to be
88 * one-size-satisifies-any-request. This in itself may not be
89 * the most memory efficient, but the efficiency of the object
90 * cache should more than make up for this!
92 ******************************************************************************/
94 union acpi_operand_object *
95 acpi_ut_create_internal_object_dbg (
99 acpi_object_type type)
101 union acpi_operand_object *object;
102 union acpi_operand_object *second_object;
105 ACPI_FUNCTION_TRACE_STR ("ut_create_internal_object_dbg",
106 acpi_ut_get_type_name (type));
109 /* Allocate the raw object descriptor */
111 object = acpi_ut_allocate_object_desc_dbg (module_name, line_number, component_id);
117 case ACPI_TYPE_REGION:
118 case ACPI_TYPE_BUFFER_FIELD:
120 /* These types require a secondary object */
122 second_object = acpi_ut_allocate_object_desc_dbg (module_name,
123 line_number, component_id);
124 if (!second_object) {
125 acpi_ut_delete_object_desc (object);
129 second_object->common.type = ACPI_TYPE_LOCAL_EXTRA;
130 second_object->common.reference_count = 1;
132 /* Link the second object to the first */
134 object->common.next_object = second_object;
138 /* All others have no secondary object */
142 /* Save the object type in the object descriptor */
144 object->common.type = (u8) type;
146 /* Init the reference count */
148 object->common.reference_count = 1;
150 /* Any per-type initialization should go here */
156 /*******************************************************************************
158 * FUNCTION: acpi_ut_create_buffer_object
160 * PARAMETERS: buffer_size - Size of buffer to be created
162 * RETURN: Pointer to a new Buffer object, null on failure
164 * DESCRIPTION: Create a fully initialized buffer object
166 ******************************************************************************/
168 union acpi_operand_object *
169 acpi_ut_create_buffer_object (
170 acpi_size buffer_size)
172 union acpi_operand_object *buffer_desc;
176 ACPI_FUNCTION_TRACE_U32 ("ut_create_buffer_object", buffer_size);
179 /* Create a new Buffer object */
181 buffer_desc = acpi_ut_create_internal_object (ACPI_TYPE_BUFFER);
186 /* Create an actual buffer only if size > 0 */
188 if (buffer_size > 0) {
189 /* Allocate the actual buffer */
191 buffer = ACPI_MEM_CALLOCATE (buffer_size);
193 ACPI_REPORT_ERROR (("create_buffer: could not allocate size %X\n",
195 acpi_ut_remove_reference (buffer_desc);
200 /* Complete buffer object initialization */
202 buffer_desc->buffer.flags |= AOPOBJ_DATA_VALID;
203 buffer_desc->buffer.pointer = buffer;
204 buffer_desc->buffer.length = (u32) buffer_size;
206 /* Return the new buffer descriptor */
208 return_PTR (buffer_desc);
212 /*******************************************************************************
214 * FUNCTION: acpi_ut_create_string_object
216 * PARAMETERS: string_size - Size of string to be created. Does not
217 * include NULL terminator, this is added
220 * RETURN: Pointer to a new String object
222 * DESCRIPTION: Create a fully initialized string object
224 ******************************************************************************/
226 union acpi_operand_object *
227 acpi_ut_create_string_object (
228 acpi_size string_size)
230 union acpi_operand_object *string_desc;
234 ACPI_FUNCTION_TRACE_U32 ("ut_create_string_object", string_size);
237 /* Create a new String object */
239 string_desc = acpi_ut_create_internal_object (ACPI_TYPE_STRING);
245 * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
246 * NOTE: Zero-length strings are NULL terminated
248 string = ACPI_MEM_CALLOCATE (string_size + 1);
250 ACPI_REPORT_ERROR (("create_string: could not allocate size %X\n",
252 acpi_ut_remove_reference (string_desc);
256 /* Complete string object initialization */
258 string_desc->string.pointer = string;
259 string_desc->string.length = (u32) string_size;
261 /* Return the new string descriptor */
263 return_PTR (string_desc);
267 /*******************************************************************************
269 * FUNCTION: acpi_ut_valid_internal_object
271 * PARAMETERS: Object - Object to be validated
273 * RETURN: TRUE if object is valid, FALSE otherwise
275 * DESCRIPTION: Validate a pointer to be an union acpi_operand_object
277 ******************************************************************************/
280 acpi_ut_valid_internal_object (
284 ACPI_FUNCTION_NAME ("ut_valid_internal_object");
287 /* Check for a null pointer */
290 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** Null Object Ptr\n"));
294 /* Check the descriptor type field */
296 switch (ACPI_GET_DESCRIPTOR_TYPE (object)) {
297 case ACPI_DESC_TYPE_OPERAND:
299 /* The object appears to be a valid union acpi_operand_object */
304 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
305 "%p is not not an ACPI operand obj [%s]\n",
306 object, acpi_ut_get_descriptor_name (object)));
314 /*******************************************************************************
316 * FUNCTION: acpi_ut_allocate_object_desc_dbg
318 * PARAMETERS: module_name - Caller's module name (for error output)
319 * line_number - Caller's line number (for error output)
320 * component_id - Caller's component ID (for error output)
322 * RETURN: Pointer to newly allocated object descriptor. Null on error
324 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
327 ******************************************************************************/
330 acpi_ut_allocate_object_desc_dbg (
335 union acpi_operand_object *object;
338 ACPI_FUNCTION_TRACE ("ut_allocate_object_desc_dbg");
341 object = acpi_os_acquire_object (acpi_gbl_operand_cache);
343 _ACPI_REPORT_ERROR (module_name, line_number, component_id,
344 ("Could not allocate an object descriptor\n"));
349 /* Mark the descriptor type */
350 memset(object, 0, sizeof(union acpi_operand_object));
351 ACPI_SET_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_OPERAND);
353 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
354 object, (u32) sizeof (union acpi_operand_object)));
360 /*******************************************************************************
362 * FUNCTION: acpi_ut_delete_object_desc
364 * PARAMETERS: Object - An Acpi internal object to be deleted
368 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
370 ******************************************************************************/
373 acpi_ut_delete_object_desc (
374 union acpi_operand_object *object)
376 ACPI_FUNCTION_TRACE_PTR ("ut_delete_object_desc", object);
379 /* Object must be an union acpi_operand_object */
381 if (ACPI_GET_DESCRIPTOR_TYPE (object) != ACPI_DESC_TYPE_OPERAND) {
382 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
383 "%p is not an ACPI Operand object [%s]\n", object,
384 acpi_ut_get_descriptor_name (object)));
388 (void) acpi_os_release_object (acpi_gbl_operand_cache, object);
393 /*******************************************************************************
395 * FUNCTION: acpi_ut_get_simple_object_size
397 * PARAMETERS: internal_object - An ACPI operand object
398 * obj_length - Where the length is returned
402 * DESCRIPTION: This function is called to determine the space required to
403 * contain a simple object for return to an external user.
405 * The length includes the object structure plus any additional
408 ******************************************************************************/
411 acpi_ut_get_simple_object_size (
412 union acpi_operand_object *internal_object,
413 acpi_size *obj_length)
416 acpi_status status = AE_OK;
419 ACPI_FUNCTION_TRACE_PTR ("ut_get_simple_object_size", internal_object);
423 * Handle a null object (Could be a uninitialized package
424 * element -- which is legal)
426 if (!internal_object) {
428 return_ACPI_STATUS (AE_OK);
431 /* Start with the length of the Acpi object */
433 length = sizeof (union acpi_object);
435 if (ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_NAMED) {
436 /* Object is a named object (reference), just return the length */
438 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
439 return_ACPI_STATUS (status);
443 * The final length depends on the object type
444 * Strings and Buffers are packed right up against the parent object and
445 * must be accessed bytewise or there may be alignment problems on
448 switch (ACPI_GET_OBJECT_TYPE (internal_object)) {
449 case ACPI_TYPE_STRING:
451 length += (acpi_size) internal_object->string.length + 1;
455 case ACPI_TYPE_BUFFER:
457 length += (acpi_size) internal_object->buffer.length;
461 case ACPI_TYPE_INTEGER:
462 case ACPI_TYPE_PROCESSOR:
463 case ACPI_TYPE_POWER:
466 * No extra data for these types
471 case ACPI_TYPE_LOCAL_REFERENCE:
473 switch (internal_object->reference.opcode) {
474 case AML_INT_NAMEPATH_OP:
477 * Get the actual length of the full pathname to this object.
478 * The reference will be converted to the pathname to the object
480 length += ACPI_ROUND_UP_TO_NATIVE_WORD (
481 acpi_ns_get_pathname_length (internal_object->reference.node));
487 * No other reference opcodes are supported.
488 * Notably, Locals and Args are not supported, but this may be
489 * required eventually.
491 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
492 "Unsupported Reference opcode=%X in object %p\n",
493 internal_object->reference.opcode, internal_object));
502 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unsupported type=%X in object %p\n",
503 ACPI_GET_OBJECT_TYPE (internal_object), internal_object));
509 * Account for the space required by the object rounded up to the next
510 * multiple of the machine word size. This keeps each object aligned
511 * on a machine word boundary. (preventing alignment faults on some
514 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
515 return_ACPI_STATUS (status);
519 /*******************************************************************************
521 * FUNCTION: acpi_ut_get_element_length
523 * PARAMETERS: acpi_pkg_callback
527 * DESCRIPTION: Get the length of one package element.
529 ******************************************************************************/
532 acpi_ut_get_element_length (
534 union acpi_operand_object *source_object,
535 union acpi_generic_state *state,
538 acpi_status status = AE_OK;
539 struct acpi_pkg_info *info = (struct acpi_pkg_info *) context;
540 acpi_size object_space;
543 switch (object_type) {
544 case ACPI_COPY_TYPE_SIMPLE:
547 * Simple object - just get the size (Null object/entry is handled
548 * here also) and sum it into the running package length
550 status = acpi_ut_get_simple_object_size (source_object, &object_space);
551 if (ACPI_FAILURE (status)) {
555 info->length += object_space;
559 case ACPI_COPY_TYPE_PACKAGE:
561 /* Package object - nothing much to do here, let the walk handle it */
563 info->num_packages++;
564 state->pkg.this_target_obj = NULL;
570 /* No other types allowed */
572 return (AE_BAD_PARAMETER);
579 /*******************************************************************************
581 * FUNCTION: acpi_ut_get_package_object_size
583 * PARAMETERS: internal_object - An ACPI internal object
584 * obj_length - Where the length is returned
588 * DESCRIPTION: This function is called to determine the space required to
589 * contain a package object for return to an external user.
591 * This is moderately complex since a package contains other
592 * objects including packages.
594 ******************************************************************************/
597 acpi_ut_get_package_object_size (
598 union acpi_operand_object *internal_object,
599 acpi_size *obj_length)
602 struct acpi_pkg_info info;
605 ACPI_FUNCTION_TRACE_PTR ("ut_get_package_object_size", internal_object);
609 info.object_space = 0;
610 info.num_packages = 1;
612 status = acpi_ut_walk_package_tree (internal_object, NULL,
613 acpi_ut_get_element_length, &info);
614 if (ACPI_FAILURE (status)) {
615 return_ACPI_STATUS (status);
619 * We have handled all of the objects in all levels of the package.
620 * just add the length of the package objects themselves.
621 * Round up to the next machine word.
623 info.length += ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object)) *
624 (acpi_size) info.num_packages;
626 /* Return the total package length */
628 *obj_length = info.length;
629 return_ACPI_STATUS (status);
633 /*******************************************************************************
635 * FUNCTION: acpi_ut_get_object_size
637 * PARAMETERS: internal_object - An ACPI internal object
638 * obj_length - Where the length will be returned
642 * DESCRIPTION: This function is called to determine the space required to
643 * contain an object for return to an API user.
645 ******************************************************************************/
648 acpi_ut_get_object_size (
649 union acpi_operand_object *internal_object,
650 acpi_size *obj_length)
655 ACPI_FUNCTION_ENTRY ();
658 if ((ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_OPERAND) &&
659 (ACPI_GET_OBJECT_TYPE (internal_object) == ACPI_TYPE_PACKAGE)) {
660 status = acpi_ut_get_package_object_size (internal_object, obj_length);
663 status = acpi_ut_get_simple_object_size (internal_object, obj_length);