When a merge does not work automatically, git prevents
[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_os_acquire_object (acpi_gbl_operand_cache);
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         memset(object, 0, sizeof(union acpi_operand_object));
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         (void) acpi_os_release_object (acpi_gbl_operand_cache, object);
389         return_VOID;
390 }
391
392
393 /*******************************************************************************
394  *
395  * FUNCTION:    acpi_ut_get_simple_object_size
396  *
397  * PARAMETERS:  internal_object    - An ACPI operand object
398  *              obj_length         - Where the length is returned
399  *
400  * RETURN:      Status
401  *
402  * DESCRIPTION: This function is called to determine the space required to
403  *              contain a simple object for return to an external user.
404  *
405  *              The length includes the object structure plus any additional
406  *              needed space.
407  *
408  ******************************************************************************/
409
410 static acpi_status
411 acpi_ut_get_simple_object_size (
412         union acpi_operand_object       *internal_object,
413         acpi_size                       *obj_length)
414 {
415         acpi_size                       length;
416         acpi_status                     status = AE_OK;
417
418
419         ACPI_FUNCTION_TRACE_PTR ("ut_get_simple_object_size", internal_object);
420
421
422         /*
423          * Handle a null object (Could be a uninitialized package
424          * element -- which is legal)
425          */
426         if (!internal_object) {
427                 *obj_length = 0;
428                 return_ACPI_STATUS (AE_OK);
429         }
430
431         /* Start with the length of the Acpi object */
432
433         length = sizeof (union acpi_object);
434
435         if (ACPI_GET_DESCRIPTOR_TYPE (internal_object) == ACPI_DESC_TYPE_NAMED) {
436                 /* Object is a named object (reference), just return the length */
437
438                 *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
439                 return_ACPI_STATUS (status);
440         }
441
442         /*
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
446          * certain processors
447          */
448         switch (ACPI_GET_OBJECT_TYPE (internal_object)) {
449         case ACPI_TYPE_STRING:
450
451                 length += (acpi_size) internal_object->string.length + 1;
452                 break;
453
454
455         case ACPI_TYPE_BUFFER:
456
457                 length += (acpi_size) internal_object->buffer.length;
458                 break;
459
460
461         case ACPI_TYPE_INTEGER:
462         case ACPI_TYPE_PROCESSOR:
463         case ACPI_TYPE_POWER:
464
465                 /*
466                  * No extra data for these types
467                  */
468                 break;
469
470
471         case ACPI_TYPE_LOCAL_REFERENCE:
472
473                 switch (internal_object->reference.opcode) {
474                 case AML_INT_NAMEPATH_OP:
475
476                         /*
477                          * Get the actual length of the full pathname to this object.
478                          * The reference will be converted to the pathname to the object
479                          */
480                         length += ACPI_ROUND_UP_TO_NATIVE_WORD (
481                                          acpi_ns_get_pathname_length (internal_object->reference.node));
482                         break;
483
484                 default:
485
486                         /*
487                          * No other reference opcodes are supported.
488                          * Notably, Locals and Args are not supported, but this may be
489                          * required eventually.
490                          */
491                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
492                                 "Unsupported Reference opcode=%X in object %p\n",
493                                 internal_object->reference.opcode, internal_object));
494                         status = AE_TYPE;
495                         break;
496                 }
497                 break;
498
499
500         default:
501
502                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unsupported type=%X in object %p\n",
503                         ACPI_GET_OBJECT_TYPE (internal_object), internal_object));
504                 status = AE_TYPE;
505                 break;
506         }
507
508         /*
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
512          * machines.)
513          */
514         *obj_length = ACPI_ROUND_UP_TO_NATIVE_WORD (length);
515         return_ACPI_STATUS (status);
516 }
517
518
519 /*******************************************************************************
520  *
521  * FUNCTION:    acpi_ut_get_element_length
522  *
523  * PARAMETERS:  acpi_pkg_callback
524  *
525  * RETURN:      Status
526  *
527  * DESCRIPTION: Get the length of one package element.
528  *
529  ******************************************************************************/
530
531 static acpi_status
532 acpi_ut_get_element_length (
533         u8                              object_type,
534         union acpi_operand_object       *source_object,
535         union acpi_generic_state        *state,
536         void                            *context)
537 {
538         acpi_status                     status = AE_OK;
539         struct acpi_pkg_info            *info = (struct acpi_pkg_info *) context;
540         acpi_size                       object_space;
541
542
543         switch (object_type) {
544         case ACPI_COPY_TYPE_SIMPLE:
545
546                 /*
547                  * Simple object - just get the size (Null object/entry is handled
548                  * here also) and sum it into the running package length
549                  */
550                 status = acpi_ut_get_simple_object_size (source_object, &object_space);
551                 if (ACPI_FAILURE (status)) {
552                         return (status);
553                 }
554
555                 info->length += object_space;
556                 break;
557
558
559         case ACPI_COPY_TYPE_PACKAGE:
560
561                 /* Package object - nothing much to do here, let the walk handle it */
562
563                 info->num_packages++;
564                 state->pkg.this_target_obj = NULL;
565                 break;
566
567
568         default:
569
570                 /* No other types allowed */
571
572                 return (AE_BAD_PARAMETER);
573         }
574
575         return (status);
576 }
577
578
579 /*******************************************************************************
580  *
581  * FUNCTION:    acpi_ut_get_package_object_size
582  *
583  * PARAMETERS:  internal_object     - An ACPI internal object
584  *              obj_length          - Where the length is returned
585  *
586  * RETURN:      Status
587  *
588  * DESCRIPTION: This function is called to determine the space required to
589  *              contain a package object for return to an external user.
590  *
591  *              This is moderately complex since a package contains other
592  *              objects including packages.
593  *
594  ******************************************************************************/
595
596 static acpi_status
597 acpi_ut_get_package_object_size (
598         union acpi_operand_object       *internal_object,
599         acpi_size                       *obj_length)
600 {
601         acpi_status                     status;
602         struct acpi_pkg_info            info;
603
604
605         ACPI_FUNCTION_TRACE_PTR ("ut_get_package_object_size", internal_object);
606
607
608         info.length      = 0;
609         info.object_space = 0;
610         info.num_packages = 1;
611
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);
616         }
617
618         /*
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.
622          */
623         info.length += ACPI_ROUND_UP_TO_NATIVE_WORD (sizeof (union acpi_object)) *
624                           (acpi_size) info.num_packages;
625
626         /* Return the total package length */
627
628         *obj_length = info.length;
629         return_ACPI_STATUS (status);
630 }
631
632
633 /*******************************************************************************
634  *
635  * FUNCTION:    acpi_ut_get_object_size
636  *
637  * PARAMETERS:  internal_object     - An ACPI internal object
638  *              obj_length          - Where the length will be returned
639  *
640  * RETURN:      Status
641  *
642  * DESCRIPTION: This function is called to determine the space required to
643  *              contain an object for return to an API user.
644  *
645  ******************************************************************************/
646
647 acpi_status
648 acpi_ut_get_object_size (
649         union acpi_operand_object       *internal_object,
650         acpi_size                       *obj_length)
651 {
652         acpi_status                     status;
653
654
655         ACPI_FUNCTION_ENTRY ();
656
657
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);
661         }
662         else {
663                 status = acpi_ut_get_simple_object_size (internal_object, obj_length);
664         }
665
666         return (status);
667 }
668
669