[ACPI] ACPICA 20060127
[linux-block.git] / drivers / acpi / dispatcher / dsfield.c
CommitLineData
1da177e4
LT
1/******************************************************************************
2 *
3 * Module Name: dsfield - Dispatcher field routines
4 *
5 *****************************************************************************/
6
7/*
4a90c7e8 8 * Copyright (C) 2000 - 2006, R. Byron Moore
1da177e4
LT
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
1da177e4
LT
44#include <acpi/acpi.h>
45#include <acpi/amlcode.h>
46#include <acpi/acdispat.h>
47#include <acpi/acinterp.h>
48#include <acpi/acnamesp.h>
49#include <acpi/acparser.h>
50
1da177e4 51#define _COMPONENT ACPI_DISPATCHER
4be44fcd 52ACPI_MODULE_NAME("dsfield")
1da177e4 53
44f6c012 54/* Local prototypes */
44f6c012 55static acpi_status
4be44fcd
LB
56acpi_ds_get_field_names(struct acpi_create_field_info *info,
57 struct acpi_walk_state *walk_state,
58 union acpi_parse_object *arg);
1da177e4
LT
59
60/*******************************************************************************
61 *
62 * FUNCTION: acpi_ds_create_buffer_field
63 *
44f6c012 64 * PARAMETERS: Op - Current parse op (create_xXField)
1da177e4
LT
65 * walk_state - Current state
66 *
67 * RETURN: Status
68 *
69 * DESCRIPTION: Execute the create_field operators:
70 * create_bit_field_op,
71 * create_byte_field_op,
72 * create_word_field_op,
73 * create_dword_field_op,
74 * create_qword_field_op,
44f6c012 75 * create_field_op (all of which define a field in a buffer)
1da177e4
LT
76 *
77 ******************************************************************************/
78
79acpi_status
4be44fcd
LB
80acpi_ds_create_buffer_field(union acpi_parse_object *op,
81 struct acpi_walk_state *walk_state)
1da177e4 82{
4be44fcd
LB
83 union acpi_parse_object *arg;
84 struct acpi_namespace_node *node;
85 acpi_status status;
86 union acpi_operand_object *obj_desc;
87 union acpi_operand_object *second_desc = NULL;
88 u32 flags;
1da177e4 89
4be44fcd 90 ACPI_FUNCTION_TRACE("ds_create_buffer_field");
1da177e4
LT
91
92 /* Get the name_string argument */
93
94 if (op->common.aml_opcode == AML_CREATE_FIELD_OP) {
4be44fcd
LB
95 arg = acpi_ps_get_arg(op, 3);
96 } else {
1da177e4
LT
97 /* Create Bit/Byte/Word/Dword field */
98
4be44fcd 99 arg = acpi_ps_get_arg(op, 2);
1da177e4
LT
100 }
101
102 if (!arg) {
4be44fcd 103 return_ACPI_STATUS(AE_AML_NO_OPERAND);
1da177e4
LT
104 }
105
106 if (walk_state->deferred_node) {
107 node = walk_state->deferred_node;
108 status = AE_OK;
4be44fcd 109 } else {
1da177e4
LT
110 /*
111 * During the load phase, we want to enter the name of the field into
112 * the namespace. During the execute phase (when we evaluate the size
113 * operand), we want to lookup the name
114 */
115 if (walk_state->parse_flags & ACPI_PARSE_EXECUTE) {
116 flags = ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE;
4be44fcd 117 } else {
44f6c012 118 flags = ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE |
4be44fcd 119 ACPI_NS_ERROR_IF_FOUND;
1da177e4
LT
120 }
121
122 /*
123 * Enter the name_string into the namespace
124 */
4be44fcd
LB
125 status =
126 acpi_ns_lookup(walk_state->scope_info,
127 arg->common.value.string, ACPI_TYPE_ANY,
128 ACPI_IMODE_LOAD_PASS1, flags, walk_state,
129 &(node));
130 if (ACPI_FAILURE(status)) {
b8e4d893 131 ACPI_ERROR_NAMESPACE(arg->common.value.string, status);
4be44fcd 132 return_ACPI_STATUS(status);
1da177e4
LT
133 }
134 }
135
44f6c012
RM
136 /* We could put the returned object (Node) on the object stack for later,
137 * but for now, we will put it in the "op" object that the parser uses,
138 * so we can get it again at the end of this scope
1da177e4
LT
139 */
140 op->common.node = node;
141
142 /*
44f6c012
RM
143 * If there is no object attached to the node, this node was just created
144 * and we need to create the field object. Otherwise, this was a lookup
145 * of an existing node and we don't want to create the field object again.
1da177e4 146 */
4be44fcd 147 obj_desc = acpi_ns_get_attached_object(node);
1da177e4 148 if (obj_desc) {
4be44fcd 149 return_ACPI_STATUS(AE_OK);
1da177e4
LT
150 }
151
152 /*
153 * The Field definition is not fully parsed at this time.
154 * (We must save the address of the AML for the buffer and index operands)
155 */
156
157 /* Create the buffer field object */
158
4be44fcd 159 obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_BUFFER_FIELD);
1da177e4
LT
160 if (!obj_desc) {
161 status = AE_NO_MEMORY;
162 goto cleanup;
163 }
164
165 /*
166 * Remember location in AML stream of the field unit
167 * opcode and operands -- since the buffer and index
168 * operands must be evaluated.
169 */
4be44fcd 170 second_desc = obj_desc->common.next_object;
1da177e4
LT
171 second_desc->extra.aml_start = op->named.data;
172 second_desc->extra.aml_length = op->named.length;
173 obj_desc->buffer_field.node = node;
174
175 /* Attach constructed field descriptors to parent node */
176
4be44fcd
LB
177 status = acpi_ns_attach_object(node, obj_desc, ACPI_TYPE_BUFFER_FIELD);
178 if (ACPI_FAILURE(status)) {
1da177e4
LT
179 goto cleanup;
180 }
181
4be44fcd 182 cleanup:
1da177e4
LT
183
184 /* Remove local reference to the object */
185
4be44fcd
LB
186 acpi_ut_remove_reference(obj_desc);
187 return_ACPI_STATUS(status);
1da177e4
LT
188}
189
1da177e4
LT
190/*******************************************************************************
191 *
192 * FUNCTION: acpi_ds_get_field_names
193 *
194 * PARAMETERS: Info - create_field info structure
195 * ` walk_state - Current method state
196 * Arg - First parser arg for the field name list
197 *
198 * RETURN: Status
199 *
200 * DESCRIPTION: Process all named fields in a field declaration. Names are
201 * entered into the namespace.
202 *
203 ******************************************************************************/
204
44f6c012 205static acpi_status
4be44fcd
LB
206acpi_ds_get_field_names(struct acpi_create_field_info *info,
207 struct acpi_walk_state *walk_state,
208 union acpi_parse_object *arg)
1da177e4 209{
4be44fcd
LB
210 acpi_status status;
211 acpi_integer position;
1da177e4 212
4be44fcd 213 ACPI_FUNCTION_TRACE_PTR("ds_get_field_names", info);
1da177e4
LT
214
215 /* First field starts at bit zero */
216
217 info->field_bit_position = 0;
218
219 /* Process all elements in the field list (of parse nodes) */
220
221 while (arg) {
222 /*
223 * Three types of field elements are handled:
224 * 1) Offset - specifies a bit offset
225 * 2) access_as - changes the access mode
226 * 3) Name - Enters a new named field into the namespace
227 */
228 switch (arg->common.aml_opcode) {
229 case AML_INT_RESERVEDFIELD_OP:
230
231 position = (acpi_integer) info->field_bit_position
4be44fcd 232 + (acpi_integer) arg->common.value.size;
1da177e4
LT
233
234 if (position > ACPI_UINT32_MAX) {
b8e4d893
BM
235 ACPI_ERROR((AE_INFO,
236 "Bit offset within field too large (> 0xFFFFFFFF)"));
4be44fcd 237 return_ACPI_STATUS(AE_SUPPORT);
1da177e4
LT
238 }
239
240 info->field_bit_position = (u32) position;
241 break;
242
1da177e4
LT
243 case AML_INT_ACCESSFIELD_OP:
244
245 /*
246 * Get a new access_type and access_attribute -- to be used for all
44f6c012
RM
247 * field units that follow, until field end or another access_as
248 * keyword.
1da177e4 249 *
44f6c012
RM
250 * In field_flags, preserve the flag bits other than the
251 * ACCESS_TYPE bits
1da177e4 252 */
44f6c012 253 info->field_flags = (u8)
4be44fcd
LB
254 ((info->
255 field_flags & ~(AML_FIELD_ACCESS_TYPE_MASK)) |
256 ((u8) ((u32) arg->common.value.integer >> 8)));
1da177e4
LT
257
258 info->attribute = (u8) (arg->common.value.integer);
259 break;
260
1da177e4
LT
261 case AML_INT_NAMEDFIELD_OP:
262
263 /* Lookup the name */
264
4be44fcd
LB
265 status = acpi_ns_lookup(walk_state->scope_info,
266 (char *)&arg->named.name,
267 info->field_type,
268 ACPI_IMODE_EXECUTE,
269 ACPI_NS_DONT_OPEN_SCOPE,
270 walk_state, &info->field_node);
271 if (ACPI_FAILURE(status)) {
b8e4d893
BM
272 ACPI_ERROR_NAMESPACE((char *)&arg->named.name,
273 status);
1da177e4 274 if (status != AE_ALREADY_EXISTS) {
4be44fcd 275 return_ACPI_STATUS(status);
1da177e4
LT
276 }
277
278 /* Already exists, ignore error */
4be44fcd 279 } else {
1da177e4
LT
280 arg->common.node = info->field_node;
281 info->field_bit_length = arg->common.value.size;
282
283 /* Create and initialize an object for the new Field Node */
284
4be44fcd
LB
285 status = acpi_ex_prep_field_value(info);
286 if (ACPI_FAILURE(status)) {
287 return_ACPI_STATUS(status);
1da177e4
LT
288 }
289 }
290
291 /* Keep track of bit position for the next field */
292
293 position = (acpi_integer) info->field_bit_position
4be44fcd 294 + (acpi_integer) arg->common.value.size;
1da177e4
LT
295
296 if (position > ACPI_UINT32_MAX) {
b8e4d893
BM
297 ACPI_ERROR((AE_INFO,
298 "Field [%4.4s] bit offset too large (> 0xFFFFFFFF)",
299 ACPI_CAST_PTR(char,
300 &info->field_node->
301 name)));
4be44fcd 302 return_ACPI_STATUS(AE_SUPPORT);
1da177e4
LT
303 }
304
305 info->field_bit_position += info->field_bit_length;
306 break;
307
1da177e4
LT
308 default:
309
b8e4d893
BM
310 ACPI_ERROR((AE_INFO,
311 "Invalid opcode in field list: %X",
312 arg->common.aml_opcode));
4be44fcd 313 return_ACPI_STATUS(AE_AML_BAD_OPCODE);
1da177e4
LT
314 }
315
316 arg = arg->common.next;
317 }
318
4be44fcd 319 return_ACPI_STATUS(AE_OK);
1da177e4
LT
320}
321
1da177e4
LT
322/*******************************************************************************
323 *
324 * FUNCTION: acpi_ds_create_field
325 *
326 * PARAMETERS: Op - Op containing the Field definition and args
327 * region_node - Object for the containing Operation Region
328 * ` walk_state - Current method state
329 *
330 * RETURN: Status
331 *
332 * DESCRIPTION: Create a new field in the specified operation region
333 *
334 ******************************************************************************/
335
336acpi_status
4be44fcd
LB
337acpi_ds_create_field(union acpi_parse_object *op,
338 struct acpi_namespace_node *region_node,
339 struct acpi_walk_state *walk_state)
1da177e4 340{
4be44fcd
LB
341 acpi_status status;
342 union acpi_parse_object *arg;
343 struct acpi_create_field_info info;
1da177e4 344
4be44fcd 345 ACPI_FUNCTION_TRACE_PTR("ds_create_field", op);
1da177e4
LT
346
347 /* First arg is the name of the parent op_region (must already exist) */
348
349 arg = op->common.value.arg;
350 if (!region_node) {
4be44fcd
LB
351 status =
352 acpi_ns_lookup(walk_state->scope_info,
353 arg->common.value.name, ACPI_TYPE_REGION,
354 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
355 walk_state, &region_node);
356 if (ACPI_FAILURE(status)) {
b8e4d893 357 ACPI_ERROR_NAMESPACE(arg->common.value.name, status);
4be44fcd 358 return_ACPI_STATUS(status);
1da177e4
LT
359 }
360 }
361
362 /* Second arg is the field flags */
363
364 arg = arg->common.next;
365 info.field_flags = (u8) arg->common.value.integer;
366 info.attribute = 0;
367
368 /* Each remaining arg is a Named Field */
369
370 info.field_type = ACPI_TYPE_LOCAL_REGION_FIELD;
371 info.region_node = region_node;
372
4be44fcd 373 status = acpi_ds_get_field_names(&info, walk_state, arg->common.next);
1da177e4 374
4be44fcd 375 return_ACPI_STATUS(status);
1da177e4
LT
376}
377
1da177e4
LT
378/*******************************************************************************
379 *
380 * FUNCTION: acpi_ds_init_field_objects
381 *
382 * PARAMETERS: Op - Op containing the Field definition and args
383 * ` walk_state - Current method state
384 *
385 * RETURN: Status
386 *
387 * DESCRIPTION: For each "Field Unit" name in the argument list that is
388 * part of the field declaration, enter the name into the
389 * namespace.
390 *
391 ******************************************************************************/
392
393acpi_status
4be44fcd
LB
394acpi_ds_init_field_objects(union acpi_parse_object *op,
395 struct acpi_walk_state *walk_state)
1da177e4 396{
4be44fcd
LB
397 acpi_status status;
398 union acpi_parse_object *arg = NULL;
399 struct acpi_namespace_node *node;
400 u8 type = 0;
1da177e4 401
4be44fcd 402 ACPI_FUNCTION_TRACE_PTR("ds_init_field_objects", op);
1da177e4
LT
403
404 switch (walk_state->opcode) {
405 case AML_FIELD_OP:
4be44fcd 406 arg = acpi_ps_get_arg(op, 2);
1da177e4
LT
407 type = ACPI_TYPE_LOCAL_REGION_FIELD;
408 break;
409
410 case AML_BANK_FIELD_OP:
4be44fcd 411 arg = acpi_ps_get_arg(op, 4);
1da177e4
LT
412 type = ACPI_TYPE_LOCAL_BANK_FIELD;
413 break;
414
415 case AML_INDEX_FIELD_OP:
4be44fcd 416 arg = acpi_ps_get_arg(op, 3);
1da177e4
LT
417 type = ACPI_TYPE_LOCAL_INDEX_FIELD;
418 break;
419
420 default:
4be44fcd 421 return_ACPI_STATUS(AE_BAD_PARAMETER);
1da177e4
LT
422 }
423
424 /*
425 * Walk the list of entries in the field_list
426 */
427 while (arg) {
428 /* Ignore OFFSET and ACCESSAS terms here */
429
430 if (arg->common.aml_opcode == AML_INT_NAMEDFIELD_OP) {
4be44fcd
LB
431 status = acpi_ns_lookup(walk_state->scope_info,
432 (char *)&arg->named.name,
433 type, ACPI_IMODE_LOAD_PASS1,
434 ACPI_NS_NO_UPSEARCH |
435 ACPI_NS_DONT_OPEN_SCOPE |
436 ACPI_NS_ERROR_IF_FOUND,
437 walk_state, &node);
438 if (ACPI_FAILURE(status)) {
b8e4d893
BM
439 ACPI_ERROR_NAMESPACE((char *)&arg->named.name,
440 status);
1da177e4 441 if (status != AE_ALREADY_EXISTS) {
4be44fcd 442 return_ACPI_STATUS(status);
1da177e4
LT
443 }
444
445 /* Name already exists, just ignore this error */
446
447 status = AE_OK;
448 }
449
450 arg->common.node = node;
451 }
452
453 /* Move to next field in the list */
454
455 arg = arg->common.next;
456 }
457
4be44fcd 458 return_ACPI_STATUS(AE_OK);
1da177e4
LT
459}
460
1da177e4
LT
461/*******************************************************************************
462 *
463 * FUNCTION: acpi_ds_create_bank_field
464 *
465 * PARAMETERS: Op - Op containing the Field definition and args
466 * region_node - Object for the containing Operation Region
467 * ` walk_state - Current method state
468 *
469 * RETURN: Status
470 *
471 * DESCRIPTION: Create a new bank field in the specified operation region
472 *
473 ******************************************************************************/
474
475acpi_status
4be44fcd
LB
476acpi_ds_create_bank_field(union acpi_parse_object *op,
477 struct acpi_namespace_node *region_node,
478 struct acpi_walk_state *walk_state)
1da177e4 479{
4be44fcd
LB
480 acpi_status status;
481 union acpi_parse_object *arg;
482 struct acpi_create_field_info info;
1da177e4 483
4be44fcd 484 ACPI_FUNCTION_TRACE_PTR("ds_create_bank_field", op);
1da177e4
LT
485
486 /* First arg is the name of the parent op_region (must already exist) */
487
488 arg = op->common.value.arg;
489 if (!region_node) {
4be44fcd
LB
490 status =
491 acpi_ns_lookup(walk_state->scope_info,
492 arg->common.value.name, ACPI_TYPE_REGION,
493 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
494 walk_state, &region_node);
495 if (ACPI_FAILURE(status)) {
b8e4d893 496 ACPI_ERROR_NAMESPACE(arg->common.value.name, status);
4be44fcd 497 return_ACPI_STATUS(status);
1da177e4
LT
498 }
499 }
500
501 /* Second arg is the Bank Register (Field) (must already exist) */
502
503 arg = arg->common.next;
4be44fcd
LB
504 status =
505 acpi_ns_lookup(walk_state->scope_info, arg->common.value.string,
506 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
507 ACPI_NS_SEARCH_PARENT, walk_state,
508 &info.register_node);
509 if (ACPI_FAILURE(status)) {
b8e4d893 510 ACPI_ERROR_NAMESPACE(arg->common.value.string, status);
4be44fcd 511 return_ACPI_STATUS(status);
1da177e4
LT
512 }
513
514 /* Third arg is the bank_value */
515
516 arg = arg->common.next;
517 info.bank_value = (u32) arg->common.value.integer;
518
519 /* Fourth arg is the field flags */
520
521 arg = arg->common.next;
522 info.field_flags = (u8) arg->common.value.integer;
523
524 /* Each remaining arg is a Named Field */
525
526 info.field_type = ACPI_TYPE_LOCAL_BANK_FIELD;
527 info.region_node = region_node;
528
4be44fcd 529 status = acpi_ds_get_field_names(&info, walk_state, arg->common.next);
1da177e4 530
4be44fcd 531 return_ACPI_STATUS(status);
1da177e4
LT
532}
533
1da177e4
LT
534/*******************************************************************************
535 *
536 * FUNCTION: acpi_ds_create_index_field
537 *
538 * PARAMETERS: Op - Op containing the Field definition and args
539 * region_node - Object for the containing Operation Region
540 * ` walk_state - Current method state
541 *
542 * RETURN: Status
543 *
544 * DESCRIPTION: Create a new index field in the specified operation region
545 *
546 ******************************************************************************/
547
548acpi_status
4be44fcd
LB
549acpi_ds_create_index_field(union acpi_parse_object *op,
550 struct acpi_namespace_node *region_node,
551 struct acpi_walk_state *walk_state)
1da177e4 552{
4be44fcd
LB
553 acpi_status status;
554 union acpi_parse_object *arg;
555 struct acpi_create_field_info info;
1da177e4 556
4be44fcd 557 ACPI_FUNCTION_TRACE_PTR("ds_create_index_field", op);
1da177e4
LT
558
559 /* First arg is the name of the Index register (must already exist) */
560
561 arg = op->common.value.arg;
4be44fcd
LB
562 status =
563 acpi_ns_lookup(walk_state->scope_info, arg->common.value.string,
564 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
565 ACPI_NS_SEARCH_PARENT, walk_state,
566 &info.register_node);
567 if (ACPI_FAILURE(status)) {
b8e4d893 568 ACPI_ERROR_NAMESPACE(arg->common.value.string, status);
4be44fcd 569 return_ACPI_STATUS(status);
1da177e4
LT
570 }
571
572 /* Second arg is the data register (must already exist) */
573
574 arg = arg->common.next;
4be44fcd
LB
575 status =
576 acpi_ns_lookup(walk_state->scope_info, arg->common.value.string,
577 ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
578 ACPI_NS_SEARCH_PARENT, walk_state,
579 &info.data_register_node);
580 if (ACPI_FAILURE(status)) {
b8e4d893 581 ACPI_ERROR_NAMESPACE(arg->common.value.string, status);
4be44fcd 582 return_ACPI_STATUS(status);
1da177e4
LT
583 }
584
585 /* Next arg is the field flags */
586
587 arg = arg->common.next;
588 info.field_flags = (u8) arg->common.value.integer;
589
590 /* Each remaining arg is a Named Field */
591
592 info.field_type = ACPI_TYPE_LOCAL_INDEX_FIELD;
593 info.region_node = region_node;
594
4be44fcd 595 status = acpi_ds_get_field_names(&info, walk_state, arg->common.next);
1da177e4 596
4be44fcd 597 return_ACPI_STATUS(status);
1da177e4 598}