vserver 1.9.3
[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                         "Destination is not a Reference or Constant object [%p]\n", dest_desc));
133
134                 ACPI_DUMP_STACK_ENTRY (source_desc);
135                 ACPI_DUMP_STACK_ENTRY (dest_desc);
136                 ACPI_DUMP_OPERANDS (&dest_desc, ACPI_IMODE_EXECUTE, "ex_store",
137                                   2, "Target is not a Reference or Constant object");
138
139                 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
140         }
141
142         /*
143          * Examine the Reference opcode.  These cases are handled:
144          *
145          * 1) Store to Name (Change the object associated with a name)
146          * 2) Store to an indexed area of a Buffer or Package
147          * 3) Store to a Method Local or Arg
148          * 4) Store to the debug object
149          */
150         switch (ref_desc->reference.opcode) {
151         case AML_NAME_OP:
152         case AML_REF_OF_OP:
153
154                 /* Storing an object into a Name "container" */
155
156                 status = acpi_ex_store_object_to_node (source_desc, ref_desc->reference.object,
157                                   walk_state, ACPI_IMPLICIT_CONVERSION);
158                 break;
159
160
161         case AML_INDEX_OP:
162
163                 /* Storing to an Index (pointer into a packager or buffer) */
164
165                 status = acpi_ex_store_object_to_index (source_desc, ref_desc, walk_state);
166                 break;
167
168
169         case AML_LOCAL_OP:
170         case AML_ARG_OP:
171
172                 /* Store to a method local/arg  */
173
174                 status = acpi_ds_store_object_to_local (ref_desc->reference.opcode,
175                                   ref_desc->reference.offset, source_desc, walk_state);
176                 break;
177
178
179         case AML_DEBUG_OP:
180
181                 /*
182                  * Storing to the Debug object causes the value stored to be
183                  * displayed and otherwise has no effect -- see ACPI Specification
184                  */
185                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "**** Write to Debug Object: ****:\n\n"));
186
187                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "[ACPI Debug] %s: ",
188                                   acpi_ut_get_object_type_name (source_desc)));
189
190                 switch (ACPI_GET_OBJECT_TYPE (source_desc)) {
191                 case ACPI_TYPE_INTEGER:
192
193                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "0x%8.8X%8.8X\n",
194                                         ACPI_FORMAT_UINT64 (source_desc->integer.value)));
195                         break;
196
197
198                 case ACPI_TYPE_BUFFER:
199
200                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "Length 0x%.2X",
201                                         (u32) source_desc->buffer.length));
202                         ACPI_DUMP_BUFFER (source_desc->buffer.pointer,
203                                 (source_desc->buffer.length < 32) ? source_desc->buffer.length : 32);
204                         break;
205
206
207                 case ACPI_TYPE_STRING:
208
209                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "Length 0x%.2X, \"%s\"\n",
210                                         source_desc->string.length, source_desc->string.pointer));
211                         break;
212
213
214                 case ACPI_TYPE_PACKAGE:
215
216                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "Size 0x%.2X Elements Ptr - %p\n",
217                                         source_desc->package.count, source_desc->package.elements));
218                         break;
219
220
221                 default:
222
223                         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_DEBUG_OBJECT, "%p\n",
224                                         source_desc));
225                         break;
226                 }
227
228                 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EXEC, "\n"));
229                 break;
230
231
232         default:
233
234                 ACPI_REPORT_ERROR (("ex_store: Unknown Reference opcode %X\n",
235                         ref_desc->reference.opcode));
236                 ACPI_DUMP_ENTRY (ref_desc, ACPI_LV_ERROR);
237
238                 status = AE_AML_INTERNAL;
239                 break;
240         }
241
242         return_ACPI_STATUS (status);
243 }
244
245
246 /*******************************************************************************
247  *
248  * FUNCTION:    acpi_ex_store_object_to_index
249  *
250  * PARAMETERS:  *source_desc            - Value to be stored
251  *              *dest_desc              - Named object to receive the value
252  *              walk_state              - Current walk state
253  *
254  * RETURN:      Status
255  *
256  * DESCRIPTION: Store the object to indexed Buffer or Package element
257  *
258  ******************************************************************************/
259
260 acpi_status
261 acpi_ex_store_object_to_index (
262         union acpi_operand_object       *source_desc,
263         union acpi_operand_object       *index_desc,
264         struct acpi_walk_state          *walk_state)
265 {
266         acpi_status                     status = AE_OK;
267         union acpi_operand_object       *obj_desc;
268         union acpi_operand_object       *new_desc;
269         u8                              value = 0;
270         u32                             i;
271
272
273         ACPI_FUNCTION_TRACE ("ex_store_object_to_index");
274
275
276         /*
277          * Destination must be a reference pointer, and
278          * must point to either a buffer or a package
279          */
280         switch (index_desc->reference.target_type) {
281         case ACPI_TYPE_PACKAGE:
282                 /*
283                  * Storing to a package element is not simple.  The source must be
284                  * evaluated and converted to the type of the destination and then the
285                  * source is copied into the destination - we can't just point to the
286                  * source object.
287                  */
288                 /*
289                  * The object at *(index_desc->Reference.Where) is the
290                  * element within the package that is to be modified.
291                  * The parent package object is at index_desc->Reference.Object
292                  */
293                 obj_desc = *(index_desc->reference.where);
294
295                 /* Do the conversion/store */
296
297                 status = acpi_ex_store_object_to_object (source_desc, obj_desc, &new_desc,
298                                   walk_state);
299                 if (ACPI_FAILURE (status)) {
300                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
301                                 "Could not store object to indexed package element\n"));
302                         return_ACPI_STATUS (status);
303                 }
304
305                 /*
306                  * If a new object was created, we must install it as the new
307                  * package element
308                  */
309                 if (new_desc != obj_desc) {
310                         acpi_ut_remove_reference (obj_desc);
311                         *(index_desc->reference.where) = new_desc;
312
313                         /* If same as the original source, add a reference */
314
315                         if (new_desc == source_desc) {
316                                 acpi_ut_add_reference (new_desc);
317                         }
318
319                         /* Increment reference count by the ref count of the parent package -1 */
320
321                         for (i = 1; i < ((union acpi_operand_object *) index_desc->reference.object)->common.reference_count; i++) {
322                                 acpi_ut_add_reference (new_desc);
323                         }
324                 }
325                 break;
326
327
328         case ACPI_TYPE_BUFFER_FIELD:
329
330                 /*
331                  * Store into a Buffer (not actually a real buffer_field) at a
332                  * location defined by an Index.
333                  *
334                  * The first 8-bit element of the source object is written to the
335                  * 8-bit Buffer location defined by the Index destination object,
336                  * according to the ACPI 2.0 specification.
337                  */
338
339                 /*
340                  * Make sure the target is a Buffer
341                  */
342                 obj_desc = index_desc->reference.object;
343                 if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_BUFFER) {
344                         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
345                 }
346
347                 /*
348                  * The assignment of the individual elements will be slightly
349                  * different for each source type.
350                  */
351                 switch (ACPI_GET_OBJECT_TYPE (source_desc)) {
352                 case ACPI_TYPE_INTEGER:
353
354                         /* Use the least-significant byte of the integer */
355
356                         value = (u8) (source_desc->integer.value);
357                         break;
358
359                 case ACPI_TYPE_BUFFER:
360
361                         value = source_desc->buffer.pointer[0];
362                         break;
363
364                 case ACPI_TYPE_STRING:
365
366                         value = (u8) source_desc->string.pointer[0];
367                         break;
368
369                 default:
370
371                         /* All other types are invalid */
372
373                         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
374                                 "Source must be Integer/Buffer/String type, not %s\n",
375                                 acpi_ut_get_object_type_name (source_desc)));
376                         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
377                 }
378
379                 /* Store the source value into the target buffer byte */
380
381                 obj_desc->buffer.pointer[index_desc->reference.offset] = value;
382                 break;
383
384
385         default:
386                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
387                         "Target is not a Package or buffer_field\n"));
388                 status = AE_AML_OPERAND_TYPE;
389                 break;
390         }
391
392         return_ACPI_STATUS (status);
393 }
394
395
396 /*******************************************************************************
397  *
398  * FUNCTION:    acpi_ex_store_object_to_node
399  *
400  * PARAMETERS:  source_desc             - Value to be stored
401  *              Node                    - Named object to receive the value
402  *              walk_state              - Current walk state
403  *              implicit_conversion     - Perform implicit conversion (yes/no)
404  *
405  * RETURN:      Status
406  *
407  * DESCRIPTION: Store the object to the named object.
408  *
409  *              The Assignment of an object to a named object is handled here
410  *              The value passed in will replace the current value (if any)
411  *              with the input value.
412  *
413  *              When storing into an object the data is converted to the
414  *              target object type then stored in the object.  This means
415  *              that the target object type (for an initialized target) will
416  *              not be changed by a store operation.
417  *
418  *              Assumes parameters are already validated.
419  *
420  ******************************************************************************/
421
422 acpi_status
423 acpi_ex_store_object_to_node (
424         union acpi_operand_object       *source_desc,
425         struct acpi_namespace_node      *node,
426         struct acpi_walk_state          *walk_state,
427         u8                              implicit_conversion)
428 {
429         acpi_status                     status = AE_OK;
430         union acpi_operand_object       *target_desc;
431         union acpi_operand_object       *new_desc;
432         acpi_object_type                target_type;
433
434
435         ACPI_FUNCTION_TRACE_PTR ("ex_store_object_to_node", source_desc);
436
437
438         /*
439          * Get current type of the node, and object attached to Node
440          */
441         target_type = acpi_ns_get_type (node);
442         target_desc = acpi_ns_get_attached_object (node);
443
444         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Storing %p(%s) into node %p(%s)\n",
445                 source_desc, acpi_ut_get_object_type_name (source_desc),
446                           node, acpi_ut_get_type_name (target_type)));
447
448         /*
449          * Resolve the source object to an actual value
450          * (If it is a reference object)
451          */
452         status = acpi_ex_resolve_object (&source_desc, target_type, walk_state);
453         if (ACPI_FAILURE (status)) {
454                 return_ACPI_STATUS (status);
455         }
456
457         /* If no implicit conversion, drop into the default case below */
458
459         if (!implicit_conversion) {
460                 /* Force execution of default (no implicit conversion) */
461
462                 target_type = ACPI_TYPE_ANY;
463         }
464
465         /*
466          * Do the actual store operation
467          */
468         switch (target_type) {
469         case ACPI_TYPE_BUFFER_FIELD:
470         case ACPI_TYPE_LOCAL_REGION_FIELD:
471         case ACPI_TYPE_LOCAL_BANK_FIELD:
472         case ACPI_TYPE_LOCAL_INDEX_FIELD:
473
474                 /*
475                  * For fields, copy the source data to the target field.
476                  */
477                 status = acpi_ex_write_data_to_field (source_desc, target_desc, &walk_state->result_obj);
478                 break;
479
480
481         case ACPI_TYPE_INTEGER:
482         case ACPI_TYPE_STRING:
483         case ACPI_TYPE_BUFFER:
484
485                 /*
486                  * These target types are all of type Integer/String/Buffer, and
487                  * therefore support implicit conversion before the store.
488                  *
489                  * Copy and/or convert the source object to a new target object
490                  */
491                 status = acpi_ex_store_object_to_object (source_desc, target_desc, &new_desc, walk_state);
492                 if (ACPI_FAILURE (status)) {
493                         return_ACPI_STATUS (status);
494                 }
495
496                 if (new_desc != target_desc) {
497                         /*
498                          * Store the new new_desc as the new value of the Name, and set
499                          * the Name's type to that of the value being stored in it.
500                          * source_desc reference count is incremented by attach_object.
501                          *
502                          * Note: This may change the type of the node if an explicit store
503                          * has been performed such that the node/object type has been
504                          * changed.
505                          */
506                         status = acpi_ns_attach_object (node, new_desc, new_desc->common.type);
507
508                         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
509                                 "Store %s into %s via Convert/Attach\n",
510                                 acpi_ut_get_object_type_name (source_desc),
511                                 acpi_ut_get_object_type_name (new_desc)));
512                 }
513                 break;
514
515
516         default:
517
518                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
519                         "Storing %s (%p) directly into node (%p), no implicit conversion\n",
520                         acpi_ut_get_object_type_name (source_desc), source_desc, node));
521
522                 /* No conversions for all other types.  Just attach the source object */
523
524                 status = acpi_ns_attach_object (node, source_desc, ACPI_GET_OBJECT_TYPE (source_desc));
525                 break;
526         }
527
528         return_ACPI_STATUS (status);
529 }
530
531