Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / utilities / utobject.c
blob9ee40a484e07357e71ab49f09ceb373ea3700b91
1 /******************************************************************************
3 * Module Name: utobject - ACPI object create/delete/size/cache routines
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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 (
77 char *module_name,
78 u32 line_number,
79 u32 component_id,
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);
92 if (!object) {
93 return_PTR (NULL);
96 switch (type) {
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);
105 return_PTR (NULL);
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;
114 break;
116 default:
117 /* All others have no secondary object */
118 break;
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 */
131 return_PTR (object);
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;
152 u8 *buffer = NULL;
155 ACPI_FUNCTION_TRACE_U32 ("ut_create_buffer_object", buffer_size);
158 /* Create a new Buffer object */
160 buffer_desc = acpi_ut_create_internal_object (ACPI_TYPE_BUFFER);
161 if (!buffer_desc) {
162 return_PTR (NULL);
165 /* Create an actual buffer only if size > 0 */
167 if (buffer_size > 0) {
168 /* Allocate the actual buffer */
170 buffer = ACPI_MEM_CALLOCATE (buffer_size);
171 if (!buffer) {
172 ACPI_REPORT_ERROR (("create_buffer: could not allocate size %X\n",
173 (u32) buffer_size));
174 acpi_ut_remove_reference (buffer_desc);
175 return_PTR (NULL);
179 /* Complete buffer object initialization */
181 buffer_desc->buffer.flags |= AOPOBJ_DATA_VALID;
182 buffer_desc->buffer.pointer = buffer;
183 buffer_desc->buffer.length = (u32) buffer_size;
185 /* Return the new buffer descriptor */
187 return_PTR (buffer_desc);
191 /*******************************************************************************
193 * FUNCTION: acpi_ut_create_string_object
195 * PARAMETERS: string_size - Size of string to be created. Does not
196 * include NULL terminator, this is added
197 * automatically.
199 * RETURN: Pointer to a new String object
201 * DESCRIPTION: Create a fully initialized string object
203 ******************************************************************************/
205 union acpi_operand_object *
206 acpi_ut_create_string_object (
207 acpi_size string_size)
209 union acpi_operand_object *string_desc;
210 char *string;
213 ACPI_FUNCTION_TRACE_U32 ("ut_create_string_object", string_size);
216 /* Create a new String object */
218 string_desc = acpi_ut_create_internal_object (ACPI_TYPE_STRING);
219 if (!string_desc) {
220 return_PTR (NULL);
224 * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
225 * NOTE: Zero-length strings are NULL terminated
227 string = ACPI_MEM_CALLOCATE (string_size + 1);
228 if (!string) {
229 ACPI_REPORT_ERROR (("create_string: could not allocate size %X\n",
230 (u32) string_size));
231 acpi_ut_remove_reference (string_desc);
232 return_PTR (NULL);
235 /* Complete string object initialization */
237 string_desc->string.pointer = string;
238 string_desc->string.length = (u32) string_size;
240 /* Return the new string descriptor */
242 return_PTR (string_desc);
246 /*******************************************************************************
248 * FUNCTION: acpi_ut_valid_internal_object
250 * PARAMETERS: Object - Object to be validated
252 * RETURN: Validate a pointer to be an union acpi_operand_object
254 ******************************************************************************/
257 acpi_ut_valid_internal_object (
258 void *object)
261 ACPI_FUNCTION_NAME ("ut_valid_internal_object");
264 /* Check for a null pointer */
266 if (!object) {
267 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** Null Object Ptr\n"));
268 return (FALSE);
271 /* Check the descriptor type field */
273 switch (ACPI_GET_DESCRIPTOR_TYPE (object)) {
274 case ACPI_DESC_TYPE_OPERAND:
276 /* The object appears to be a valid union acpi_operand_object */
278 return (TRUE);
280 default:
281 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
282 "%p is not not an ACPI operand obj [%s]\n",
283 object, acpi_ut_get_descriptor_name (object)));
284 break;
287 return (FALSE);
291 /*******************************************************************************
293 * FUNCTION: acpi_ut_allocate_object_desc_dbg
295 * PARAMETERS: module_name - Caller's module name (for error output)
296 * line_number - Caller's line number (for error output)
297 * component_id - Caller's component ID (for error output)
299 * RETURN: Pointer to newly allocated object descriptor. Null on error
301 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
302 * error conditions.
304 ******************************************************************************/
306 void *
307 acpi_ut_allocate_object_desc_dbg (
308 char *module_name,
309 u32 line_number,
310 u32 component_id)
312 union acpi_operand_object *object;
315 ACPI_FUNCTION_TRACE ("ut_allocate_object_desc_dbg");
318 object = acpi_ut_acquire_from_cache (ACPI_MEM_LIST_OPERAND);
319 if (!object) {
320 _ACPI_REPORT_ERROR (module_name, line_number, component_id,
321 ("Could not allocate an object descriptor\n"));
323 return_PTR (NULL);
326 /* Mark the descriptor type */
328 ACPI_SET_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_OPERAND);
330 ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
331 object, (u32) sizeof (union acpi_operand_object)));
333 return_PTR (object);
337 /*******************************************************************************
339 * FUNCTION: acpi_ut_delete_object_desc
341 * PARAMETERS: Object - An Acpi internal object to be deleted
343 * RETURN: None.
345 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
347 ******************************************************************************/
349 void
350 acpi_ut_delete_object_desc (
351 union acpi_operand_object *object)
353 ACPI_FUNCTION_TRACE_PTR ("ut_delete_object_desc", object);
356 /* Object must be an union acpi_operand_object */
358 if (ACPI_GET_DESCRIPTOR_TYPE (object) != ACPI_DESC_TYPE_OPERAND) {
359 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
360 "%p is not an ACPI Operand object [%s]\n", object,
361 acpi_ut_get_descriptor_name (object)));
362 return_VOID;
365 acpi_ut_release_to_cache (ACPI_MEM_LIST_OPERAND, object);
367 return_VOID;
371 #ifdef ACPI_ENABLE_OBJECT_CACHE
372 /*******************************************************************************
374 * FUNCTION: acpi_ut_delete_object_cache
376 * PARAMETERS: None
378 * RETURN: None
380 * DESCRIPTION: Purge the global state object cache. Used during subsystem
381 * termination.
383 ******************************************************************************/
385 void
386 acpi_ut_delete_object_cache (
387 void)
389 ACPI_FUNCTION_TRACE ("ut_delete_object_cache");
392 acpi_ut_delete_generic_cache (ACPI_MEM_LIST_OPERAND);
393 return_VOID;
395 #endif
398 /*******************************************************************************
400 * FUNCTION: acpi_ut_get_simple_object_size
402 * PARAMETERS: *internal_object - Pointer to the object we are examining
403 * *obj_length - Where the length is returned
405 * RETURN: Status
407 * DESCRIPTION: This function is called to determine the space required to
408 * contain a simple object for return to an external user.
410 * The length includes the object structure plus any additional
411 * needed space.
413 ******************************************************************************/
415 acpi_status
416 acpi_ut_get_simple_object_size (
417 union acpi_operand_object *internal_object,
418 acpi_size *obj_length)
420 acpi_size length;
421 acpi_status status = AE_OK;
424 ACPI_FUNCTION_TRACE_PTR ("ut_get_simple_object_size", internal_object);
427 /* Handle a null object (Could be a uninitialized package element -- which is legal) */
429 if (!internal_object) {
430 *obj_length = 0;
431 return_ACPI_STATUS (AE_OK);
434 /* Start with the length of the Acpi object */
436 length = sizeof (union acpi_object);
438 if (ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_NAMED) {
439 /* Object is a named object (reference), just return the length */
441 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
442 return_ACPI_STATUS (status);
446 * The final length depends on the object type
447 * Strings and Buffers are packed right up against the parent object and
448 * must be accessed bytewise or there may be alignment problems on
449 * certain processors
451 switch (ACPI_GET_OBJECT_TYPE (internal_object)) {
452 case ACPI_TYPE_STRING:
454 length += (acpi_size) internal_object->string.length + 1;
455 break;
458 case ACPI_TYPE_BUFFER:
460 length += (acpi_size) internal_object->buffer.length;
461 break;
464 case ACPI_TYPE_INTEGER:
465 case ACPI_TYPE_PROCESSOR:
466 case ACPI_TYPE_POWER:
469 * No extra data for these types
471 break;
474 case ACPI_TYPE_LOCAL_REFERENCE:
476 switch (internal_object->reference.opcode) {
477 case AML_INT_NAMEPATH_OP:
480 * Get the actual length of the full pathname to this object.
481 * The reference will be converted to the pathname to the object
483 length += ACPI_ROUND_UP_TO_NATIVE_WORD (acpi_ns_get_pathname_length (internal_object->reference.node));
484 break;
486 default:
489 * No other reference opcodes are supported.
490 * Notably, Locals and Args are not supported, but this may be
491 * required eventually.
493 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
494 "Unsupported Reference opcode=%X in object %p\n",
495 internal_object->reference.opcode, internal_object));
496 status = AE_TYPE;
497 break;
499 break;
502 default:
504 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unsupported type=%X in object %p\n",
505 ACPI_GET_OBJECT_TYPE (internal_object), internal_object));
506 status = AE_TYPE;
507 break;
511 * Account for the space required by the object rounded up to the next
512 * multiple of the machine word size. This keeps each object aligned
513 * on a machine word boundary. (preventing alignment faults on some
514 * machines.)
516 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
517 return_ACPI_STATUS (status);
521 /*******************************************************************************
523 * FUNCTION: acpi_ut_get_element_length
525 * PARAMETERS: acpi_pkg_callback
527 * RETURN: Status
529 * DESCRIPTION: Get the length of one package element.
531 ******************************************************************************/
533 acpi_status
534 acpi_ut_get_element_length (
535 u8 object_type,
536 union acpi_operand_object *source_object,
537 union acpi_generic_state *state,
538 void *context)
540 acpi_status status = AE_OK;
541 struct acpi_pkg_info *info = (struct acpi_pkg_info *) context;
542 acpi_size object_space;
545 switch (object_type) {
546 case ACPI_COPY_TYPE_SIMPLE:
549 * Simple object - just get the size (Null object/entry is handled
550 * here also) and sum it into the running package length
552 status = acpi_ut_get_simple_object_size (source_object, &object_space);
553 if (ACPI_FAILURE (status)) {
554 return (status);
557 info->length += object_space;
558 break;
561 case ACPI_COPY_TYPE_PACKAGE:
563 /* Package object - nothing much to do here, let the walk handle it */
565 info->num_packages++;
566 state->pkg.this_target_obj = NULL;
567 break;
570 default:
572 /* No other types allowed */
574 return (AE_BAD_PARAMETER);
577 return (status);
581 /*******************************************************************************
583 * FUNCTION: acpi_ut_get_package_object_size
585 * PARAMETERS: *internal_object - Pointer to the object we are examining
586 * *obj_length - Where the length is returned
588 * RETURN: Status
590 * DESCRIPTION: This function is called to determine the space required to
591 * contain a package object for return to an external user.
593 * This is moderately complex since a package contains other
594 * objects including packages.
596 ******************************************************************************/
598 acpi_status
599 acpi_ut_get_package_object_size (
600 union acpi_operand_object *internal_object,
601 acpi_size *obj_length)
603 acpi_status status;
604 struct acpi_pkg_info info;
607 ACPI_FUNCTION_TRACE_PTR ("ut_get_package_object_size", internal_object);
610 info.length = 0;
611 info.object_space = 0;
612 info.num_packages = 1;
614 status = acpi_ut_walk_package_tree (internal_object, NULL,
615 acpi_ut_get_element_length, &info);
616 if (ACPI_FAILURE (status)) {
617 return_ACPI_STATUS (status);
621 * We have handled all of the objects in all levels of the package.
622 * just add the length of the package objects themselves.
623 * Round up to the next machine word.
625 info.length += ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object)) *
626 (acpi_size) info.num_packages;
628 /* Return the total package length */
630 *obj_length = info.length;
631 return_ACPI_STATUS (status);
635 /*******************************************************************************
637 * FUNCTION: acpi_ut_get_object_size
639 * PARAMETERS: *internal_object - Pointer to the object we are examining
640 * *obj_length - Where the length will be returned
642 * RETURN: Status
644 * DESCRIPTION: This function is called to determine the space required to
645 * contain an object for return to an API user.
647 ******************************************************************************/
649 acpi_status
650 acpi_ut_get_object_size(
651 union acpi_operand_object *internal_object,
652 acpi_size *obj_length)
654 acpi_status status;
657 ACPI_FUNCTION_ENTRY ();
660 if ((ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_OPERAND) &&
661 (ACPI_GET_OBJECT_TYPE (internal_object) == ACPI_TYPE_PACKAGE)) {
662 status = acpi_ut_get_package_object_size (internal_object, obj_length);
664 else {
665 status = acpi_ut_get_simple_object_size (internal_object, obj_length);
668 return (status);