kernel.org linux-2.6.10
[linux-2.6.git] / drivers / acpi / executer / exstore.c
1
2 /******************************************************************************
3  *
4  * Module Name: exstore - AML Interpreter object store support
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2004, R. Byron Moore
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45
46 #include <acpi/acpi.h>
47 #include <acpi/acdispat.h>
48 #include <acpi/acinterp.h>
49 #include <acpi/amlcode.h>
50 #include <acpi/acnamesp.h>
51
52
53 #define _COMPONENT          ACPI_EXECUTER
54          ACPI_MODULE_NAME    ("exstore")
55
56
57 /*******************************************************************************
58  *
59  * FUNCTION:    acpi_ex_store
60  *
61  * PARAMETERS:  *source_desc        - Value to be stored
62  *              *dest_desc          - Where to store it.  Must be an NS node
63  *                                    or an union acpi_operand_object of type
64  *                                    Reference;
65  *              walk_state          - Current walk state
66  *
67  * RETURN:      Status
68  *
69  * DESCRIPTION: Store the value described by source_desc into the location
70  *              described by dest_desc. Called by various interpreter
71  *              functions to store the result of an operation into
72  *              the destination operand -- not just simply the actual "Store"
73  *              ASL operator.
74  *
75  ******************************************************************************/
76
77 acpi_status
78 acpi_ex_store (
79         union acpi_operand_object       *source_desc,
80         union acpi_operand_object       *dest_desc,
81         struct acpi_walk_state          *walk_state)
82 {
83         acpi_status                     status = AE_OK;
84         union acpi_operand_object       *ref_desc = dest_desc;
85
86
87         ACPI_FUNCTION_TRACE_PTR ("ex_store", dest_desc);
88
89
90         /* Validate parameters */
91
92         if (!source_desc || !dest_desc) {
93                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null parameter\n"));
94                 return_ACPI_STATUS (AE_AML_NO_OPERAND);
95         }
96
97         /* dest_desc can be either a namespace node or an ACPI object */
98
99         if (ACPI_GET_DESCRIPTOR_TYPE (dest_desc) == ACPI_DESC_TYPE_NAMED) {
100                 /*
101                  * Dest is a namespace node,
102                  * Storing an object into a Named node.
103                  */
104                 status = acpi_ex_store_object_to_node (source_desc,
105                                  (struct acpi_namespace_node *) dest_desc, walk_state,
106                                  ACPI_IMPLICIT_CONVERSION);
107
108                 return_ACPI_STATUS (status);
109         }
110
111         /* Destination object must be a Reference or a Constant object */
112
113         switch (ACPI_GET_OBJECT_TYPE (dest_desc)) {
114         case ACPI_TYPE_LOCAL_REFERENCE:
115                 break;
116
117         case ACPI_TYPE_INTEGER:
118
119                 /* Allow stores to Constants -- a Noop as per ACPI spec */
120
121                 if (dest_desc->common.flags & AOPOBJ_AML_CONSTANT) {
122                         return_ACPI_STATUS (AE_OK);
123                 }
124
125                 /*lint -fallthrough */
126
127         default:
128
129                 /* Destination is not a Reference object */
130
131                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
132                         "Target is not a Reference or Constant object - %s [%p]\n",
133                         acpi_ut_get_object_type_name (dest_desc), dest_desc));
134
135                 ACPI_DUMP_STACK_ENTRY (source_desc);
136                 ACPI_DUMP_STACK_ENTRY (dest_desc);
137                 ACPI_DUMP_OPERANDS (&dest_desc, ACPI_IMODE_EXECUTE, "ex_store",
138                                   2, "Target is not a Reference or Constant object");
139
140                 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
141         }
142
143         /*
144          * Examine the Reference opcode.  These cases are handled:
145          *
146          * 1) Store to Name (Change the object associated with a name)
147          * 2) Store to an indexed area of a Buffer or Package
148          * 3) Store to a Method Local or Arg
149          * 4) Store to the debug object
150          */
151         switch (ref_desc->reference.opcode) {
152         case AML_NAME_OP:
153         case AML_REF_OF_OP:
154
155                 /* Storing an object into a Name "container" */
156
157                 status = acpi_ex_store_object_to_node (source_desc, ref_desc->reference.object,
158                                   walk_state, ACPI_IMPLICIT_CONVERSION);
159                 break;
160
161
162         case AML_INDEX_OP:
163
164                 /* Storing to an Index (pointer into a packager or buffer) */
165
166                 status = acpi_ex_store_object_to_index (source_desc, ref_desc, walk_state);
167                 break;
168
169
170         case AML_LOCAL_OP:
171         case AML_ARG_OP:
172
173                 /* Store to a method local/arg  */
174
175                 status = acpi_ds_store_object_to_local (ref_desc->reference.opcode,
176                                   ref_desc->reference.offset, source_desc, walk_state);
177                 break;
178
179
180         case AML_DEBUG_OP:
181
182                 /*
183                  * Storing to the Debug object causes the value stored to be
184                  * displayed and otherwise has no effect -- see ACPI Specification
185                  */
186                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
187                         "**** Write to Debug Object: Object %p %s ****:\n\n",
188                         source_desc, acpi_ut_get_object_type_name (source_desc)));
189
190                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "[ACPI Debug] %s: ",
191                         acpi_ut_get_object_type_name (source_desc)));
192
193                 if (!acpi_ut_valid_internal_object (source_desc)) {
194                    ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT,
195                            "%p, Invalid Internal Object!\n", source_desc));
196                    break;
197                 }
198
199                 switch (ACPI_GET_OBJECT_TYPE (source_desc)) {
200                 case ACPI_TYPE_INTEGER:
201
202                         if (acpi_gbl_integer_byte_width == 4) {
203                                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "0x%8.8X\n",
204                                         (u32) source_desc->integer.value));
205                         }
206                         else {
207                                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "0x%8.8X%8.8X\n",
208                                         ACPI_FORMAT_UINT64 (source_desc->integer.value)));
209                         }
210                         break;
211
212
213                 case ACPI_TYPE_BUFFER:
214
215                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "[0x%.2X]",
216                                 (u32) source_desc->buffer.length));
217                         ACPI_DUMP_BUFFER (source_desc->buffer.pointer,
218                                 (source_desc->buffer.length < 32) ? source_desc->buffer.length : 32);
219                         break;
220
221
222                 case ACPI_TYPE_STRING:
223
224                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "[0x%.2X] \"%s\"\n",
225                                 source_desc->string.length, source_desc->string.pointer));
226                         break;
227
228
229                 case ACPI_TYPE_PACKAGE:
230
231                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "[0x%.2X] Elements Ptr - %p\n",
232                                 source_desc->package.count, source_desc->package.elements));
233                         break;
234
235
236                 default:
237
238                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "%p\n",
239                                 source_desc));
240                         break;
241                 }
242
243                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EXEC, "\n"));
244                 break;
245
246
247         default:
248
249                 ACPI_REPORT_ERROR (("ex_store: Unknown Reference opcode %X\n",
250                         ref_desc->reference.opcode));
251                 ACPI_DUMP_ENTRY (ref_desc, ACPI_LV_ERROR);
252
253                 status = AE_AML_INTERNAL;
254                 break;
255         }
256
257         return_ACPI_STATUS (status);
258 }
259
260
261 /*******************************************************************************
262  *
263  * FUNCTION:    acpi_ex_store_object_to_index
264  *
265  * PARAMETERS:  *source_desc            - Value to be stored
266  *              *dest_desc              - Named object to receive the value
267  *              walk_state              - Current walk state
268  *
269  * RETURN:      Status
270  *
271  * DESCRIPTION: Store the object to indexed Buffer or Package element
272  *
273  ******************************************************************************/
274
275 acpi_status
276 acpi_ex_store_object_to_index (
277         union acpi_operand_object       *source_desc,
278         union acpi_operand_object       *index_desc,
279         struct acpi_walk_state          *walk_state)
280 {
281         acpi_status                     status = AE_OK;
282         union acpi_operand_object       *obj_desc;
283         union acpi_operand_object       *new_desc;
284         u8                              value = 0;
285         u32                             i;
286
287
288         ACPI_FUNCTION_TRACE ("ex_store_object_to_index");
289
290
291         /*
292          * Destination must be a reference pointer, and
293          * must point to either a buffer or a package
294          */
295         switch (index_desc->reference.target_type) {
296         case ACPI_TYPE_PACKAGE:
297                 /*
298                  * Storing to a package element is not simple.  The source must be
299                  * evaluated and converted to the type of the destination and then the
300                  * source is copied into the destination - we can't just point to the
301                  * source object.
302                  */
303                 /*
304                  * The object at *(index_desc->Reference.Where) is the
305                  * element within the package that is to be modified.
306                  * The parent package object is at index_desc->Reference.Object
307                  */
308                 obj_desc = *(index_desc->reference.where);
309
310                 /* Do the conversion/store */
311
312                 status = acpi_ex_store_object_to_object (source_desc, obj_desc, &new_desc,
313                                   walk_state);
314                 if (ACPI_FAILURE (status)) {
315                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
316                                 "Could not store object to indexed package element\n"));
317                         return_ACPI_STATUS (status);
318                 }
319
320                 /*
321                  * If a new object was created, we must install it as the new
322                  * package element
323                  */
324                 if (new_desc != obj_desc) {
325                         acpi_ut_remove_reference (obj_desc);
326                         *(index_desc->reference.where) = new_desc;
327
328                         /* If same as the original source, add a reference */
329
330                         if (new_desc == source_desc) {
331                                 acpi_ut_add_reference (new_desc);
332                         }
333
334                         /* Increment reference count by the ref count of the parent package -1 */
335
336                         for (i = 1; i < ((union acpi_operand_object *) index_desc->reference.object)->common.reference_count; i++) {
337                                 acpi_ut_add_reference (new_desc);
338                         }
339                 }
340                 break;
341
342
343         case ACPI_TYPE_BUFFER_FIELD:
344
345                 /*
346                  * Store into a Buffer (not actually a real buffer_field) at a
347                  * location defined by an Index.
348                  *
349                  * The first 8-bit element of the source object is written to the
350                  * 8-bit Buffer location defined by the Index destination object,
351                  * according to the ACPI 2.0 specification.
352                  */
353
354                 /*
355                  * Make sure the target is a Buffer
356                  */
357                 obj_desc = index_desc->reference.object;
358                 if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_BUFFER) {
359                         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
360                 }
361
362                 /*
363                  * The assignment of the individual elements will be slightly
364                  * different for each source type.
365                  */
366                 switch (ACPI_GET_OBJECT_TYPE (source_desc)) {
367                 case ACPI_TYPE_INTEGER:
368
369                         /* Use the least-significant byte of the integer */
370
371                         value = (u8) (source_desc->integer.value);
372                         break;
373
374                 case ACPI_TYPE_BUFFER:
375
376                         value = source_desc->buffer.pointer[0];
377                         break;
378
379                 case ACPI_TYPE_STRING:
380
381                         value = (u8) source_desc->string.pointer[0];
382                         break;
383
384                 default:
385
386                         /* All other types are invalid */
387
388                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
389                                 "Source must be Integer/Buffer/String type, not %s\n",
390                                 acpi_ut_get_object_type_name (source_desc)));
391                         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
392                 }
393
394                 /* Store the source value into the target buffer byte */
395
396                 obj_desc->buffer.pointer[index_desc->reference.offset] = value;
397                 break;
398
399
400         default:
401                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
402                         "Target is not a Package or buffer_field\n"));
403                 status = AE_AML_OPERAND_TYPE;
404                 break;
405         }
406
407         return_ACPI_STATUS (status);
408 }
409
410
411 /*******************************************************************************
412  *
413  * FUNCTION:    acpi_ex_store_object_to_node
414  *
415  * PARAMETERS:  source_desc             - Value to be stored
416  *              Node                    - Named object to receive the value
417  *              walk_state              - Current walk state
418  *              implicit_conversion     - Perform implicit conversion (yes/no)
419  *
420  * RETURN:      Status
421  *
422  * DESCRIPTION: Store the object to the named object.
423  *
424  *              The Assignment of an object to a named object is handled here
425  *              The value passed in will replace the current value (if any)
426  *              with the input value.
427  *
428  *              When storing into an object the data is converted to the
429  *              target object type then stored in the object.  This means
430  *              that the target object type (for an initialized target) will
431  *              not be changed by a store operation.
432  *
433  *              Assumes parameters are already validated.
434  *
435  ******************************************************************************/
436
437 acpi_status
438 acpi_ex_store_object_to_node (
439         union acpi_operand_object       *source_desc,
440         struct acpi_namespace_node      *node,
441         struct acpi_walk_state          *walk_state,
442         u8                              implicit_conversion)
443 {
444         acpi_status                     status = AE_OK;
445         union acpi_operand_object       *target_desc;
446         union acpi_operand_object       *new_desc;
447         acpi_object_type                target_type;
448
449
450         ACPI_FUNCTION_TRACE_PTR ("ex_store_object_to_node", source_desc);
451
452
453         /*
454          * Get current type of the node, and object attached to Node
455          */
456         target_type = acpi_ns_get_type (node);
457         target_desc = acpi_ns_get_attached_object (node);
458
459         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Storing %p(%s) into node %p(%s)\n",
460                 source_desc, acpi_ut_get_object_type_name (source_desc),
461                           node, acpi_ut_get_type_name (target_type)));
462
463         /*
464          * Resolve the source object to an actual value
465          * (If it is a reference object)
466          */
467         status = acpi_ex_resolve_object (&source_desc, target_type, walk_state);
468         if (ACPI_FAILURE (status)) {
469                 return_ACPI_STATUS (status);
470         }
471
472         /* If no implicit conversion, drop into the default case below */
473
474         if (!implicit_conversion) {
475                 /* Force execution of default (no implicit conversion) */
476
477                 target_type = ACPI_TYPE_ANY;
478         }
479
480         /*
481          * Do the actual store operation
482          */
483         switch (target_type) {
484         case ACPI_TYPE_BUFFER_FIELD:
485         case ACPI_TYPE_LOCAL_REGION_FIELD:
486         case ACPI_TYPE_LOCAL_BANK_FIELD:
487         case ACPI_TYPE_LOCAL_INDEX_FIELD:
488
489                 /*
490                  * For fields, copy the source data to the target field.
491                  */
492                 status = acpi_ex_write_data_to_field (source_desc, target_desc, &walk_state->result_obj);
493                 break;
494
495
496         case ACPI_TYPE_INTEGER:
497         case ACPI_TYPE_STRING:
498         case ACPI_TYPE_BUFFER:
499
500                 /*
501                  * These target types are all of type Integer/String/Buffer, and
502                  * therefore support implicit conversion before the store.
503                  *
504                  * Copy and/or convert the source object to a new target object
505                  */
506                 status = acpi_ex_store_object_to_object (source_desc, target_desc, &new_desc, walk_state);
507                 if (ACPI_FAILURE (status)) {
508                         return_ACPI_STATUS (status);
509                 }
510
511                 if (new_desc != target_desc) {
512                         /*
513                          * Store the new new_desc as the new value of the Name, and set
514                          * the Name's type to that of the value being stored in it.
515                          * source_desc reference count is incremented by attach_object.
516                          *
517                          * Note: This may change the type of the node if an explicit store
518                          * has been performed such that the node/object type has been
519                          * changed.
520                          */
521                         status = acpi_ns_attach_object (node, new_desc, new_desc->common.type);
522
523                         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
524                                 "Store %s into %s via Convert/Attach\n",
525                                 acpi_ut_get_object_type_name (source_desc),
526                                 acpi_ut_get_object_type_name (new_desc)));
527                 }
528                 break;
529
530
531         default:
532
533                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
534                         "Storing %s (%p) directly into node (%p), no implicit conversion\n",
535                         acpi_ut_get_object_type_name (source_desc), source_desc, node));
536
537                 /* No conversions for all other types.  Just attach the source object */
538
539                 status = acpi_ns_attach_object (node, source_desc, ACPI_GET_OBJECT_TYPE (source_desc));
540                 break;
541         }
542
543         return_ACPI_STATUS (status);
544 }
545
546