Merge /home/torvalds/linux-2.6-arm
[linux-2.6] / drivers / acpi / utilities / utobject.c
1 /******************************************************************************
2  *
3  * Module Name: utobject - ACPI object create/delete/size/cache routines
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2005, R. Byron Moore
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43
44
45 #include <acpi/acpi.h>
46 #include <acpi/acnamesp.h>
47 #include <acpi/amlcode.h>
48
49
50 #define _COMPONENT          ACPI_UTILITIES
51          ACPI_MODULE_NAME    ("utobject")
52
53 /* Local prototypes */
54
55 static acpi_status
56 acpi_ut_get_simple_object_size (
57         union acpi_operand_object       *obj,
58         acpi_size                       *obj_length);
59
60 static acpi_status
61 acpi_ut_get_package_object_size (
62         union acpi_operand_object       *obj,
63         acpi_size                       *obj_length);
64
65 static acpi_status
66 acpi_ut_get_element_length (
67         u8                              object_type,
68         union acpi_operand_object       *source_object,
69         union acpi_generic_state        *state,
70         void                            *context);
71
72
73 /*******************************************************************************
74  *
75  * FUNCTION:    acpi_ut_create_internal_object_dbg
76  *
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
81  *
82  * RETURN:      A new internal object, null on failure
83  *
84  * DESCRIPTION: Create and initialize a new internal object.
85  *
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!
91  *
92  ******************************************************************************/
93
94 union acpi_operand_object    *
95 acpi_ut_create_internal_object_dbg (
96         char                            *module_name,
97         u32                             line_number,
98         u32                             component_id,
99         acpi_object_type                type)
100 {
101         union acpi_operand_object       *object;
102         union acpi_operand_object       *second_object;
103
104
105         ACPI_FUNCTION_TRACE_STR ("ut_create_internal_object_dbg",
106                 acpi_ut_get_type_name (type));
107
108
109         /* Allocate the raw object descriptor */
110
111         object = acpi_ut_allocate_object_desc_dbg (module_name, line_number, component_id);
112         if (!object) {
113                 return_PTR (NULL);
114         }
115
116         switch (type) {
117         case ACPI_TYPE_REGION:
118         case ACPI_TYPE_BUFFER_FIELD:
119
120                 /* These types require a secondary object */
121
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);
126                         return_PTR (NULL);
127                 }
128
129                 second_object->common.type = ACPI_TYPE_LOCAL_EXTRA;
130                 second_object->common.reference_count = 1;
131
132                 /* Link the second object to the first */
133
134                 object->common.next_object = second_object;
135                 break;
136
137         default:
138                 /* All others have no secondary object */
139                 break;
140         }
141
142         /* Save the object type in the object descriptor */
143
144         object->common.type = (u8) type;
145
146         /* Init the reference count */
147
148         object->common.reference_count = 1;
149
150         /* Any per-type initialization should go here */
151
152         return_PTR (object);
153 }
154
155
156 /*******************************************************************************
157  *
158  * FUNCTION:    acpi_ut_create_buffer_object
159  *
160  * PARAMETERS:  buffer_size            - Size of buffer to be created
161  *
162  * RETURN:      Pointer to a new Buffer object, null on failure
163  *
164  * DESCRIPTION: Create a fully initialized buffer object
165  *
166  ******************************************************************************/
167
168 union acpi_operand_object *
169 acpi_ut_create_buffer_object (
170         acpi_size                       buffer_size)
171 {
172         union acpi_operand_object       *buffer_desc;
173         u8                              *buffer = NULL;
174
175
176         ACPI_FUNCTION_TRACE_U32 ("ut_create_buffer_object", buffer_size);
177
178
179         /* Create a new Buffer object */
180
181         buffer_desc = acpi_ut_create_internal_object (ACPI_TYPE_BUFFER);
182         if (!buffer_desc) {
183                 return_PTR (NULL);
184         }
185
186         /* Create an actual buffer only if size > 0 */
187
188         if (buffer_size > 0) {
189                 /* Allocate the actual buffer */
190
191                 buffer = ACPI_MEM_CALLOCATE (buffer_size);
192                 if (!buffer) {
193                         ACPI_REPORT_ERROR (("create_buffer: could not allocate size %X\n",
194                                 (u32) buffer_size));
195                         acpi_ut_remove_reference (buffer_desc);
196                         return_PTR (NULL);
197                 }
198         }
199
200         /* Complete buffer object initialization */
201
202         buffer_desc->buffer.flags |= AOPOBJ_DATA_VALID;
203         buffer_desc->buffer.pointer = buffer;
204         buffer_desc->buffer.length = (u32) buffer_size;
205
206         /* Return the new buffer descriptor */
207
208         return_PTR (buffer_desc);
209 }
210
211
212 /*******************************************************************************
213  *
214  * FUNCTION:    acpi_ut_create_string_object
215  *
216  * PARAMETERS:  string_size         - Size of string to be created. Does not
217  *                                    include NULL terminator, this is added
218  *                                    automatically.
219  *
220  * RETURN:      Pointer to a new String object
221  *
222  * DESCRIPTION: Create a fully initialized string object
223  *
224  ******************************************************************************/
225
226 union acpi_operand_object *
227 acpi_ut_create_string_object (
228         acpi_size                       string_size)
229 {
230         union acpi_operand_object       *string_desc;
231         char                            *string;
232
233
234         ACPI_FUNCTION_TRACE_U32 ("ut_create_string_object", string_size);
235
236
237         /* Create a new String object */
238
239         string_desc = acpi_ut_create_internal_object (ACPI_TYPE_STRING);
240         if (!string_desc) {
241                 return_PTR (NULL);
242         }
243
244         /*
245          * Allocate the actual string buffer -- (Size + 1) for NULL terminator.
246          * NOTE: Zero-length strings are NULL terminated
247          */
248         string = ACPI_MEM_CALLOCATE (string_size + 1);
249         if (!string) {
250                 ACPI_REPORT_ERROR (("create_string: could not allocate size %X\n",
251                         (u32) string_size));
252                 acpi_ut_remove_reference (string_desc);
253                 return_PTR (NULL);
254         }
255
256         /* Complete string object initialization */
257
258         string_desc->string.pointer = string;
259         string_desc->string.length = (u32) string_size;
260
261         /* Return the new string descriptor */
262
263         return_PTR (string_desc);
264 }
265
266
267 /*******************************************************************************
268  *
269  * FUNCTION:    acpi_ut_valid_internal_object
270  *
271  * PARAMETERS:  Object              - Object to be validated
272  *
273  * RETURN:      TRUE if object is valid, FALSE otherwise
274  *
275  * DESCRIPTION: Validate a pointer to be an union acpi_operand_object
276  *
277  ******************************************************************************/
278
279 u8
280 acpi_ut_valid_internal_object (
281         void                            *object)
282 {
283
284         ACPI_FUNCTION_NAME ("ut_valid_internal_object");
285
286
287         /* Check for a null pointer */
288
289         if (!object) {
290                 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "**** Null Object Ptr\n"));
291                 return (FALSE);
292         }
293
294         /* Check the descriptor type field */
295
296         switch (ACPI_GET_DESCRIPTOR_TYPE (object)) {
297         case ACPI_DESC_TYPE_OPERAND:
298
299                 /* The object appears to be a valid union acpi_operand_object    */
300
301                 return (TRUE);
302
303         default:
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)));
307                 break;
308         }
309
310         return (FALSE);
311 }
312
313
314 /*******************************************************************************
315  *
316  * FUNCTION:    acpi_ut_allocate_object_desc_dbg
317  *
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)
321  *
322  * RETURN:      Pointer to newly allocated object descriptor.  Null on error
323  *
324  * DESCRIPTION: Allocate a new object descriptor.  Gracefully handle
325  *              error conditions.
326  *
327  ******************************************************************************/
328
329 void *
330 acpi_ut_allocate_object_desc_dbg (
331         char                            *module_name,
332         u32                             line_number,
333         u32                             component_id)
334 {
335         union acpi_operand_object       *object;
336
337
338         ACPI_FUNCTION_TRACE ("ut_allocate_object_desc_dbg");
339
340
341         object = acpi_ut_acquire_from_cache (ACPI_MEM_LIST_OPERAND);
342         if (!object) {
343                 _ACPI_REPORT_ERROR (module_name, line_number, component_id,
344                                   ("Could not allocate an object descriptor\n"));
345
346                 return_PTR (NULL);
347         }
348
349         /* Mark the descriptor type */
350
351         ACPI_SET_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_OPERAND);
352
353         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p Size %X\n",
354                         object, (u32) sizeof (union acpi_operand_object)));
355
356         return_PTR (object);
357 }
358
359
360 /*******************************************************************************
361  *
362  * FUNCTION:    acpi_ut_delete_object_desc
363  *
364  * PARAMETERS:  Object          - An Acpi internal object to be deleted
365  *
366  * RETURN:      None.
367  *
368  * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
369  *
370  ******************************************************************************/
371
372 void
373 acpi_ut_delete_object_desc (
374         union acpi_operand_object       *object)
375 {
376         ACPI_FUNCTION_TRACE_PTR ("ut_delete_object_desc", object);
377
378
379         /* Object must be an union acpi_operand_object    */
380
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)));
385                 return_VOID;
386         }
387
388         acpi_ut_release_to_cache (ACPI_MEM_LIST_OPERAND, object);
389
390         return_VOID;
391 }
392
393
394 #ifdef ACPI_ENABLE_OBJECT_CACHE
395 /*******************************************************************************
396  *
397  * FUNCTION:    acpi_ut_delete_object_cache
398  *
399  * PARAMETERS:  None
400  *
401  * RETURN:      None
402  *
403  * DESCRIPTION: Purge the global state object cache.  Used during subsystem
404  *              termination.
405  *
406  ******************************************************************************/
407
408 void
409 acpi_ut_delete_object_cache (
410         void)
411 {
412         ACPI_FUNCTION_TRACE ("ut_delete_object_cache");
413
414
415         acpi_ut_delete_generic_cache (ACPI_MEM_LIST_OPERAND);
416         return_VOID;
417 }
418 #endif
419
420
421 /*******************************************************************************
422  *
423  * FUNCTION:    acpi_ut_get_simple_object_size
424  *
425  * PARAMETERS:  internal_object    - An ACPI operand object
426  *              obj_length         - Where the length is returned
427  *
428  * RETURN:      Status
429  *
430  * DESCRIPTION: This function is called to determine the space required to
431  *              contain a simple object for return to an external user.
432  *
433  *              The length includes the object structure plus any additional
434  *              needed space.
435  *
436  ******************************************************************************/
437
438 static acpi_status
439 acpi_ut_get_simple_object_size (
440         union acpi_operand_object       *internal_object,
441         acpi_size                       *obj_length)
442 {
443         acpi_size                       length;
444         acpi_status                     status = AE_OK;
445
446
447         ACPI_FUNCTION_TRACE_PTR ("ut_get_simple_object_size", internal_object);
448
449
450         /*
451          * Handle a null object (Could be a uninitialized package
452          * element -- which is legal)
453          */
454         if (!internal_object) {
455                 *obj_length = 0;
456                 return_ACPI_STATUS (AE_OK);
457         }
458
459         /* Start with the length of the Acpi object */
460
461         length = sizeof (union acpi_object);
462
463         if (ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_NAMED) {
464                 /* Object is a named object (reference), just return the length */
465
466                 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
467                 return_ACPI_STATUS (status);
468         }
469
470         /*
471          * The final length depends on the object type
472          * Strings and Buffers are packed right up against the parent object and
473          * must be accessed bytewise or there may be alignment problems on
474          * certain processors
475          */
476         switch (ACPI_GET_OBJECT_TYPE (internal_object)) {
477         case ACPI_TYPE_STRING:
478
479                 length += (acpi_size) internal_object->string.length + 1;
480                 break;
481
482
483         case ACPI_TYPE_BUFFER:
484
485                 length += (acpi_size) internal_object->buffer.length;
486                 break;
487
488
489         case ACPI_TYPE_INTEGER:
490         case ACPI_TYPE_PROCESSOR:
491         case ACPI_TYPE_POWER:
492
493                 /*
494                  * No extra data for these types
495                  */
496                 break;
497
498
499         case ACPI_TYPE_LOCAL_REFERENCE:
500
501                 switch (internal_object->reference.opcode) {
502                 case AML_INT_NAMEPATH_OP:
503
504                         /*
505                          * Get the actual length of the full pathname to this object.
506                          * The reference will be converted to the pathname to the object
507                          */
508                         length += ACPI_ROUND_UP_TO_NATIVE_WORD (
509                                          acpi_ns_get_pathname_length (internal_object->reference.node));
510                         break;
511
512                 default:
513
514                         /*
515                          * No other reference opcodes are supported.
516                          * Notably, Locals and Args are not supported, but this may be
517                          * required eventually.
518                          */
519                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
520                                 "Unsupported Reference opcode=%X in object %p\n",
521                                 internal_object->reference.opcode, internal_object));
522                         status = AE_TYPE;
523                         break;
524                 }
525                 break;
526
527
528         default:
529
530                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unsupported type=%X in object %p\n",
531                         ACPI_GET_OBJECT_TYPE (internal_object), internal_object));
532                 status = AE_TYPE;
533                 break;
534         }
535
536         /*
537          * Account for the space required by the object rounded up to the next
538          * multiple of the machine word size.  This keeps each object aligned
539          * on a machine word boundary. (preventing alignment faults on some
540          * machines.)
541          */
542         *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
543         return_ACPI_STATUS (status);
544 }
545
546
547 /*******************************************************************************
548  *
549  * FUNCTION:    acpi_ut_get_element_length
550  *
551  * PARAMETERS:  acpi_pkg_callback
552  *
553  * RETURN:      Status
554  *
555  * DESCRIPTION: Get the length of one package element.
556  *
557  ******************************************************************************/
558
559 static acpi_status
560 acpi_ut_get_element_length (
561         u8                              object_type,
562         union acpi_operand_object       *source_object,
563         union acpi_generic_state        *state,
564         void                            *context)
565 {
566         acpi_status                     status = AE_OK;
567         struct acpi_pkg_info            *info = (struct acpi_pkg_info *) context;
568         acpi_size                       object_space;
569
570
571         switch (object_type) {
572         case ACPI_COPY_TYPE_SIMPLE:
573
574                 /*
575                  * Simple object - just get the size (Null object/entry is handled
576                  * here also) and sum it into the running package length
577                  */
578                 status = acpi_ut_get_simple_object_size (source_object, &object_space);
579                 if (ACPI_FAILURE (status)) {
580                         return (status);
581                 }
582
583                 info->length += object_space;
584                 break;
585
586
587         case ACPI_COPY_TYPE_PACKAGE:
588
589                 /* Package object - nothing much to do here, let the walk handle it */
590
591                 info->num_packages++;
592                 state->pkg.this_target_obj = NULL;
593                 break;
594
595
596         default:
597
598                 /* No other types allowed */
599
600                 return (AE_BAD_PARAMETER);
601         }
602
603         return (status);
604 }
605
606
607 /*******************************************************************************
608  *
609  * FUNCTION:    acpi_ut_get_package_object_size
610  *
611  * PARAMETERS:  internal_object     - An ACPI internal object
612  *              obj_length          - Where the length is returned
613  *
614  * RETURN:      Status
615  *
616  * DESCRIPTION: This function is called to determine the space required to
617  *              contain a package object for return to an external user.
618  *
619  *              This is moderately complex since a package contains other
620  *              objects including packages.
621  *
622  ******************************************************************************/
623
624 static acpi_status
625 acpi_ut_get_package_object_size (
626         union acpi_operand_object       *internal_object,
627         acpi_size                       *obj_length)
628 {
629         acpi_status                     status;
630         struct acpi_pkg_info            info;
631
632
633         ACPI_FUNCTION_TRACE_PTR ("ut_get_package_object_size", internal_object);
634
635
636         info.length      = 0;
637         info.object_space = 0;
638         info.num_packages = 1;
639
640         status = acpi_ut_walk_package_tree (internal_object, NULL,
641                          acpi_ut_get_element_length, &info);
642         if (ACPI_FAILURE (status)) {
643                 return_ACPI_STATUS (status);
644         }
645
646         /*
647          * We have handled all of the objects in all levels of the package.
648          * just add the length of the package objects themselves.
649          * Round up to the next machine word.
650          */
651         info.length += ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object)) *
652                           (acpi_size) info.num_packages;
653
654         /* Return the total package length */
655
656         *obj_length = info.length;
657         return_ACPI_STATUS (status);
658 }
659
660
661 /*******************************************************************************
662  *
663  * FUNCTION:    acpi_ut_get_object_size
664  *
665  * PARAMETERS:  internal_object     - An ACPI internal object
666  *              obj_length          - Where the length will be returned
667  *
668  * RETURN:      Status
669  *
670  * DESCRIPTION: This function is called to determine the space required to
671  *              contain an object for return to an API user.
672  *
673  ******************************************************************************/
674
675 acpi_status
676 acpi_ut_get_object_size (
677         union acpi_operand_object       *internal_object,
678         acpi_size                       *obj_length)
679 {
680         acpi_status                     status;
681
682
683         ACPI_FUNCTION_ENTRY ();
684
685
686         if ((ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_OPERAND) &&
687                 (ACPI_GET_OBJECT_TYPE (internal_object) == ACPI_TYPE_PACKAGE)) {
688                 status = acpi_ut_get_package_object_size (internal_object, obj_length);
689         }
690         else {
691                 status = acpi_ut_get_simple_object_size (internal_object, obj_length);
692         }
693
694         return (status);
695 }
696
697