treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / acpi / acpica / dsopcode.c
blobd9c26e720cb758d4737b23627ef1587fbbf35163
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: dsopcode - Dispatcher support for regions and fields
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acparser.h"
13 #include "amlcode.h"
14 #include "acdispat.h"
15 #include "acinterp.h"
16 #include "acnamesp.h"
17 #include "acevents.h"
18 #include "actables.h"
20 #define _COMPONENT ACPI_DISPATCHER
21 ACPI_MODULE_NAME("dsopcode")
23 /* Local prototypes */
24 static acpi_status
25 acpi_ds_init_buffer_field(u16 aml_opcode,
26 union acpi_operand_object *obj_desc,
27 union acpi_operand_object *buffer_desc,
28 union acpi_operand_object *offset_desc,
29 union acpi_operand_object *length_desc,
30 union acpi_operand_object *result_desc);
32 /*******************************************************************************
34 * FUNCTION: acpi_ds_initialize_region
36 * PARAMETERS: obj_handle - Region namespace node
38 * RETURN: Status
40 * DESCRIPTION: Front end to ev_initialize_region
42 ******************************************************************************/
44 acpi_status acpi_ds_initialize_region(acpi_handle obj_handle)
46 union acpi_operand_object *obj_desc;
47 acpi_status status;
49 obj_desc = acpi_ns_get_attached_object(obj_handle);
51 /* Namespace is NOT locked */
53 status = acpi_ev_initialize_region(obj_desc);
54 return (status);
57 /*******************************************************************************
59 * FUNCTION: acpi_ds_init_buffer_field
61 * PARAMETERS: aml_opcode - create_xxx_field
62 * obj_desc - buffer_field object
63 * buffer_desc - Host Buffer
64 * offset_desc - Offset into buffer
65 * length_desc - Length of field (CREATE_FIELD_OP only)
66 * result_desc - Where to store the result
68 * RETURN: Status
70 * DESCRIPTION: Perform actual initialization of a buffer field
72 ******************************************************************************/
74 static acpi_status
75 acpi_ds_init_buffer_field(u16 aml_opcode,
76 union acpi_operand_object *obj_desc,
77 union acpi_operand_object *buffer_desc,
78 union acpi_operand_object *offset_desc,
79 union acpi_operand_object *length_desc,
80 union acpi_operand_object *result_desc)
82 u32 offset;
83 u32 bit_offset;
84 u32 bit_count;
85 u8 field_flags;
86 acpi_status status;
88 ACPI_FUNCTION_TRACE_PTR(ds_init_buffer_field, obj_desc);
90 /* Host object must be a Buffer */
92 if (buffer_desc->common.type != ACPI_TYPE_BUFFER) {
93 ACPI_ERROR((AE_INFO,
94 "Target of Create Field is not a Buffer object - %s",
95 acpi_ut_get_object_type_name(buffer_desc)));
97 status = AE_AML_OPERAND_TYPE;
98 goto cleanup;
102 * The last parameter to all of these opcodes (result_desc) started
103 * out as a name_string, and should therefore now be a NS node
104 * after resolution in acpi_ex_resolve_operands().
106 if (ACPI_GET_DESCRIPTOR_TYPE(result_desc) != ACPI_DESC_TYPE_NAMED) {
107 ACPI_ERROR((AE_INFO,
108 "(%s) destination not a NS Node [%s]",
109 acpi_ps_get_opcode_name(aml_opcode),
110 acpi_ut_get_descriptor_name(result_desc)));
112 status = AE_AML_OPERAND_TYPE;
113 goto cleanup;
116 offset = (u32) offset_desc->integer.value;
119 * Setup the Bit offsets and counts, according to the opcode
121 switch (aml_opcode) {
122 case AML_CREATE_FIELD_OP:
124 /* Offset is in bits, count is in bits */
126 field_flags = AML_FIELD_ACCESS_BYTE;
127 bit_offset = offset;
128 bit_count = (u32) length_desc->integer.value;
130 /* Must have a valid (>0) bit count */
132 if (bit_count == 0) {
133 ACPI_BIOS_ERROR((AE_INFO,
134 "Attempt to CreateField of length zero"));
135 status = AE_AML_OPERAND_VALUE;
136 goto cleanup;
138 break;
140 case AML_CREATE_BIT_FIELD_OP:
142 /* Offset is in bits, Field is one bit */
144 bit_offset = offset;
145 bit_count = 1;
146 field_flags = AML_FIELD_ACCESS_BYTE;
147 break;
149 case AML_CREATE_BYTE_FIELD_OP:
151 /* Offset is in bytes, field is one byte */
153 bit_offset = 8 * offset;
154 bit_count = 8;
155 field_flags = AML_FIELD_ACCESS_BYTE;
156 break;
158 case AML_CREATE_WORD_FIELD_OP:
160 /* Offset is in bytes, field is one word */
162 bit_offset = 8 * offset;
163 bit_count = 16;
164 field_flags = AML_FIELD_ACCESS_WORD;
165 break;
167 case AML_CREATE_DWORD_FIELD_OP:
169 /* Offset is in bytes, field is one dword */
171 bit_offset = 8 * offset;
172 bit_count = 32;
173 field_flags = AML_FIELD_ACCESS_DWORD;
174 break;
176 case AML_CREATE_QWORD_FIELD_OP:
178 /* Offset is in bytes, field is one qword */
180 bit_offset = 8 * offset;
181 bit_count = 64;
182 field_flags = AML_FIELD_ACCESS_QWORD;
183 break;
185 default:
187 ACPI_ERROR((AE_INFO,
188 "Unknown field creation opcode 0x%02X",
189 aml_opcode));
190 status = AE_AML_BAD_OPCODE;
191 goto cleanup;
194 /* Entire field must fit within the current length of the buffer */
196 if ((bit_offset + bit_count) > (8 * (u32)buffer_desc->buffer.length)) {
197 status = AE_AML_BUFFER_LIMIT;
198 ACPI_BIOS_EXCEPTION((AE_INFO, status,
199 "Field [%4.4s] at bit offset/length %u/%u "
200 "exceeds size of target Buffer (%u bits)",
201 acpi_ut_get_node_name(result_desc),
202 bit_offset, bit_count,
203 8 * (u32)buffer_desc->buffer.length));
204 goto cleanup;
208 * Initialize areas of the field object that are common to all fields
209 * For field_flags, use LOCK_RULE = 0 (NO_LOCK),
210 * UPDATE_RULE = 0 (UPDATE_PRESERVE)
212 status =
213 acpi_ex_prep_common_field_object(obj_desc, field_flags, 0,
214 bit_offset, bit_count);
215 if (ACPI_FAILURE(status)) {
216 goto cleanup;
219 obj_desc->buffer_field.buffer_obj = buffer_desc;
220 obj_desc->buffer_field.is_create_field =
221 aml_opcode == AML_CREATE_FIELD_OP;
223 /* Reference count for buffer_desc inherits obj_desc count */
225 buffer_desc->common.reference_count = (u16)
226 (buffer_desc->common.reference_count +
227 obj_desc->common.reference_count);
229 cleanup:
231 /* Always delete the operands */
233 acpi_ut_remove_reference(offset_desc);
234 acpi_ut_remove_reference(buffer_desc);
236 if (aml_opcode == AML_CREATE_FIELD_OP) {
237 acpi_ut_remove_reference(length_desc);
240 /* On failure, delete the result descriptor */
242 if (ACPI_FAILURE(status)) {
243 acpi_ut_remove_reference(result_desc); /* Result descriptor */
244 } else {
245 /* Now the address and length are valid for this buffer_field */
247 obj_desc->buffer_field.flags |= AOPOBJ_DATA_VALID;
250 return_ACPI_STATUS(status);
253 /*******************************************************************************
255 * FUNCTION: acpi_ds_eval_buffer_field_operands
257 * PARAMETERS: walk_state - Current walk
258 * op - A valid buffer_field Op object
260 * RETURN: Status
262 * DESCRIPTION: Get buffer_field Buffer and Index
263 * Called from acpi_ds_exec_end_op during buffer_field parse tree walk
265 ******************************************************************************/
267 acpi_status
268 acpi_ds_eval_buffer_field_operands(struct acpi_walk_state *walk_state,
269 union acpi_parse_object *op)
271 acpi_status status;
272 union acpi_operand_object *obj_desc;
273 struct acpi_namespace_node *node;
274 union acpi_parse_object *next_op;
276 ACPI_FUNCTION_TRACE_PTR(ds_eval_buffer_field_operands, op);
279 * This is where we evaluate the address and length fields of the
280 * create_xxx_field declaration
282 node = op->common.node;
284 /* next_op points to the op that holds the Buffer */
286 next_op = op->common.value.arg;
288 /* Evaluate/create the address and length operands */
290 status = acpi_ds_create_operands(walk_state, next_op);
291 if (ACPI_FAILURE(status)) {
292 return_ACPI_STATUS(status);
295 obj_desc = acpi_ns_get_attached_object(node);
296 if (!obj_desc) {
297 return_ACPI_STATUS(AE_NOT_EXIST);
300 /* Resolve the operands */
302 status =
303 acpi_ex_resolve_operands(op->common.aml_opcode, ACPI_WALK_OPERANDS,
304 walk_state);
305 if (ACPI_FAILURE(status)) {
306 ACPI_ERROR((AE_INFO, "(%s) bad operand(s), status 0x%X",
307 acpi_ps_get_opcode_name(op->common.aml_opcode),
308 status));
310 return_ACPI_STATUS(status);
313 /* Initialize the Buffer Field */
315 if (op->common.aml_opcode == AML_CREATE_FIELD_OP) {
317 /* NOTE: Slightly different operands for this opcode */
319 status =
320 acpi_ds_init_buffer_field(op->common.aml_opcode, obj_desc,
321 walk_state->operands[0],
322 walk_state->operands[1],
323 walk_state->operands[2],
324 walk_state->operands[3]);
325 } else {
326 /* All other, create_xxx_field opcodes */
328 status =
329 acpi_ds_init_buffer_field(op->common.aml_opcode, obj_desc,
330 walk_state->operands[0],
331 walk_state->operands[1], NULL,
332 walk_state->operands[2]);
335 return_ACPI_STATUS(status);
338 /*******************************************************************************
340 * FUNCTION: acpi_ds_eval_region_operands
342 * PARAMETERS: walk_state - Current walk
343 * op - A valid region Op object
345 * RETURN: Status
347 * DESCRIPTION: Get region address and length
348 * Called from acpi_ds_exec_end_op during op_region parse tree walk
350 ******************************************************************************/
352 acpi_status
353 acpi_ds_eval_region_operands(struct acpi_walk_state *walk_state,
354 union acpi_parse_object *op)
356 acpi_status status;
357 union acpi_operand_object *obj_desc;
358 union acpi_operand_object *operand_desc;
359 struct acpi_namespace_node *node;
360 union acpi_parse_object *next_op;
361 acpi_adr_space_type space_id;
363 ACPI_FUNCTION_TRACE_PTR(ds_eval_region_operands, op);
366 * This is where we evaluate the address and length fields of the
367 * op_region declaration
369 node = op->common.node;
371 /* next_op points to the op that holds the space_ID */
373 next_op = op->common.value.arg;
374 space_id = (acpi_adr_space_type)next_op->common.value.integer;
376 /* next_op points to address op */
378 next_op = next_op->common.next;
380 /* Evaluate/create the address and length operands */
382 status = acpi_ds_create_operands(walk_state, next_op);
383 if (ACPI_FAILURE(status)) {
384 return_ACPI_STATUS(status);
387 /* Resolve the length and address operands to numbers */
389 status =
390 acpi_ex_resolve_operands(op->common.aml_opcode, ACPI_WALK_OPERANDS,
391 walk_state);
392 if (ACPI_FAILURE(status)) {
393 return_ACPI_STATUS(status);
396 obj_desc = acpi_ns_get_attached_object(node);
397 if (!obj_desc) {
398 return_ACPI_STATUS(AE_NOT_EXIST);
402 * Get the length operand and save it
403 * (at Top of stack)
405 operand_desc = walk_state->operands[walk_state->num_operands - 1];
407 obj_desc->region.length = (u32) operand_desc->integer.value;
408 acpi_ut_remove_reference(operand_desc);
410 /* A zero-length operation region is unusable. Just warn */
412 if (!obj_desc->region.length
413 && (space_id < ACPI_NUM_PREDEFINED_REGIONS)) {
414 ACPI_WARNING((AE_INFO,
415 "Operation Region [%4.4s] has zero length (SpaceId %X)",
416 node->name.ascii, space_id));
420 * Get the address and save it
421 * (at top of stack - 1)
423 operand_desc = walk_state->operands[walk_state->num_operands - 2];
425 obj_desc->region.address = (acpi_physical_address)
426 operand_desc->integer.value;
427 acpi_ut_remove_reference(operand_desc);
429 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "RgnObj %p Addr %8.8X%8.8X Len %X\n",
430 obj_desc,
431 ACPI_FORMAT_UINT64(obj_desc->region.address),
432 obj_desc->region.length));
434 status = acpi_ut_add_address_range(obj_desc->region.space_id,
435 obj_desc->region.address,
436 obj_desc->region.length, node);
438 /* Now the address and length are valid for this opregion */
440 obj_desc->region.flags |= AOPOBJ_DATA_VALID;
441 return_ACPI_STATUS(status);
444 /*******************************************************************************
446 * FUNCTION: acpi_ds_eval_table_region_operands
448 * PARAMETERS: walk_state - Current walk
449 * op - A valid region Op object
451 * RETURN: Status
453 * DESCRIPTION: Get region address and length.
454 * Called from acpi_ds_exec_end_op during data_table_region parse
455 * tree walk.
457 ******************************************************************************/
459 acpi_status
460 acpi_ds_eval_table_region_operands(struct acpi_walk_state *walk_state,
461 union acpi_parse_object *op)
463 acpi_status status;
464 union acpi_operand_object *obj_desc;
465 union acpi_operand_object **operand;
466 struct acpi_namespace_node *node;
467 union acpi_parse_object *next_op;
468 struct acpi_table_header *table;
469 u32 table_index;
471 ACPI_FUNCTION_TRACE_PTR(ds_eval_table_region_operands, op);
474 * This is where we evaluate the Signature string, oem_id string,
475 * and oem_table_id string of the Data Table Region declaration
477 node = op->common.node;
479 /* next_op points to Signature string op */
481 next_op = op->common.value.arg;
484 * Evaluate/create the Signature string, oem_id string,
485 * and oem_table_id string operands
487 status = acpi_ds_create_operands(walk_state, next_op);
488 if (ACPI_FAILURE(status)) {
489 return_ACPI_STATUS(status);
492 operand = &walk_state->operands[0];
495 * Resolve the Signature string, oem_id string,
496 * and oem_table_id string operands
498 status =
499 acpi_ex_resolve_operands(op->common.aml_opcode, ACPI_WALK_OPERANDS,
500 walk_state);
501 if (ACPI_FAILURE(status)) {
502 goto cleanup;
505 /* Find the ACPI table */
507 status = acpi_tb_find_table(operand[0]->string.pointer,
508 operand[1]->string.pointer,
509 operand[2]->string.pointer, &table_index);
510 if (ACPI_FAILURE(status)) {
511 if (status == AE_NOT_FOUND) {
512 ACPI_ERROR((AE_INFO,
513 "ACPI Table [%4.4s] OEM:(%s, %s) not found in RSDT/XSDT",
514 operand[0]->string.pointer,
515 operand[1]->string.pointer,
516 operand[2]->string.pointer));
518 goto cleanup;
521 status = acpi_get_table_by_index(table_index, &table);
522 if (ACPI_FAILURE(status)) {
523 goto cleanup;
526 obj_desc = acpi_ns_get_attached_object(node);
527 if (!obj_desc) {
528 status = AE_NOT_EXIST;
529 goto cleanup;
532 obj_desc->region.address = ACPI_PTR_TO_PHYSADDR(table);
533 obj_desc->region.length = table->length;
535 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "RgnObj %p Addr %8.8X%8.8X Len %X\n",
536 obj_desc,
537 ACPI_FORMAT_UINT64(obj_desc->region.address),
538 obj_desc->region.length));
540 /* Now the address and length are valid for this opregion */
542 obj_desc->region.flags |= AOPOBJ_DATA_VALID;
544 cleanup:
545 acpi_ut_remove_reference(operand[0]);
546 acpi_ut_remove_reference(operand[1]);
547 acpi_ut_remove_reference(operand[2]);
549 return_ACPI_STATUS(status);
552 /*******************************************************************************
554 * FUNCTION: acpi_ds_eval_data_object_operands
556 * PARAMETERS: walk_state - Current walk
557 * op - A valid data_object Op object
558 * obj_desc - data_object
560 * RETURN: Status
562 * DESCRIPTION: Get the operands and complete the following data object types:
563 * Buffer, Package.
565 ******************************************************************************/
567 acpi_status
568 acpi_ds_eval_data_object_operands(struct acpi_walk_state *walk_state,
569 union acpi_parse_object *op,
570 union acpi_operand_object *obj_desc)
572 acpi_status status;
573 union acpi_operand_object *arg_desc;
574 u32 length;
576 ACPI_FUNCTION_TRACE(ds_eval_data_object_operands);
578 /* The first operand (for all of these data objects) is the length */
581 * Set proper index into operand stack for acpi_ds_obj_stack_push
582 * invoked inside acpi_ds_create_operand.
584 walk_state->operand_index = walk_state->num_operands;
586 /* Ignore if child is not valid */
588 if (!op->common.value.arg) {
589 ACPI_ERROR((AE_INFO,
590 "Missing child while evaluating opcode %4.4X, Op %p",
591 op->common.aml_opcode, op));
592 return_ACPI_STATUS(AE_OK);
595 status = acpi_ds_create_operand(walk_state, op->common.value.arg, 1);
596 if (ACPI_FAILURE(status)) {
597 return_ACPI_STATUS(status);
600 status = acpi_ex_resolve_operands(walk_state->opcode,
601 &(walk_state->
602 operands[walk_state->num_operands -
603 1]), walk_state);
604 if (ACPI_FAILURE(status)) {
605 return_ACPI_STATUS(status);
608 /* Extract length operand */
610 arg_desc = walk_state->operands[walk_state->num_operands - 1];
611 length = (u32) arg_desc->integer.value;
613 /* Cleanup for length operand */
615 status = acpi_ds_obj_stack_pop(1, walk_state);
616 if (ACPI_FAILURE(status)) {
617 return_ACPI_STATUS(status);
620 acpi_ut_remove_reference(arg_desc);
623 * Create the actual data object
625 switch (op->common.aml_opcode) {
626 case AML_BUFFER_OP:
628 status =
629 acpi_ds_build_internal_buffer_obj(walk_state, op, length,
630 &obj_desc);
631 break;
633 case AML_PACKAGE_OP:
634 case AML_VARIABLE_PACKAGE_OP:
636 status =
637 acpi_ds_build_internal_package_obj(walk_state, op, length,
638 &obj_desc);
639 break;
641 default:
643 return_ACPI_STATUS(AE_AML_BAD_OPCODE);
646 if (ACPI_SUCCESS(status)) {
648 * Return the object in the walk_state, unless the parent is a package -
649 * in this case, the return object will be stored in the parse tree
650 * for the package.
652 if ((!op->common.parent) ||
653 ((op->common.parent->common.aml_opcode != AML_PACKAGE_OP) &&
654 (op->common.parent->common.aml_opcode !=
655 AML_VARIABLE_PACKAGE_OP)
656 && (op->common.parent->common.aml_opcode !=
657 AML_NAME_OP))) {
658 walk_state->result_obj = obj_desc;
662 return_ACPI_STATUS(status);
665 /*******************************************************************************
667 * FUNCTION: acpi_ds_eval_bank_field_operands
669 * PARAMETERS: walk_state - Current walk
670 * op - A valid bank_field Op object
672 * RETURN: Status
674 * DESCRIPTION: Get bank_field bank_value
675 * Called from acpi_ds_exec_end_op during bank_field parse tree walk
677 ******************************************************************************/
679 acpi_status
680 acpi_ds_eval_bank_field_operands(struct acpi_walk_state *walk_state,
681 union acpi_parse_object *op)
683 acpi_status status;
684 union acpi_operand_object *obj_desc;
685 union acpi_operand_object *operand_desc;
686 struct acpi_namespace_node *node;
687 union acpi_parse_object *next_op;
688 union acpi_parse_object *arg;
690 ACPI_FUNCTION_TRACE_PTR(ds_eval_bank_field_operands, op);
693 * This is where we evaluate the bank_value field of the
694 * bank_field declaration
697 /* next_op points to the op that holds the Region */
699 next_op = op->common.value.arg;
701 /* next_op points to the op that holds the Bank Register */
703 next_op = next_op->common.next;
705 /* next_op points to the op that holds the Bank Value */
707 next_op = next_op->common.next;
710 * Set proper index into operand stack for acpi_ds_obj_stack_push
711 * invoked inside acpi_ds_create_operand.
713 * We use walk_state->Operands[0] to store the evaluated bank_value
715 walk_state->operand_index = 0;
717 status = acpi_ds_create_operand(walk_state, next_op, 0);
718 if (ACPI_FAILURE(status)) {
719 return_ACPI_STATUS(status);
722 status = acpi_ex_resolve_to_value(&walk_state->operands[0], walk_state);
723 if (ACPI_FAILURE(status)) {
724 return_ACPI_STATUS(status);
727 ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS,
728 acpi_ps_get_opcode_name(op->common.aml_opcode), 1);
730 * Get the bank_value operand and save it
731 * (at Top of stack)
733 operand_desc = walk_state->operands[0];
735 /* Arg points to the start Bank Field */
737 arg = acpi_ps_get_arg(op, 4);
738 while (arg) {
740 /* Ignore OFFSET and ACCESSAS terms here */
742 if (arg->common.aml_opcode == AML_INT_NAMEDFIELD_OP) {
743 node = arg->common.node;
745 obj_desc = acpi_ns_get_attached_object(node);
746 if (!obj_desc) {
747 return_ACPI_STATUS(AE_NOT_EXIST);
750 obj_desc->bank_field.value =
751 (u32) operand_desc->integer.value;
754 /* Move to next field in the list */
756 arg = arg->common.next;
759 acpi_ut_remove_reference(operand_desc);
760 return_ACPI_STATUS(status);