xref: /openbmc/linux/drivers/acpi/acpica/dswload.c (revision a44e4f3a)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: dswload - Dispatcher first pass namespace load callbacks
5  *
6  * Copyright (C) 2000 - 2019, Intel Corp.
7  *
8  *****************************************************************************/
9 
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 
18 #ifdef ACPI_ASL_COMPILER
19 #include "acdisasm.h"
20 #endif
21 
22 #define _COMPONENT          ACPI_DISPATCHER
23 ACPI_MODULE_NAME("dswload")
24 
25 /*******************************************************************************
26  *
27  * FUNCTION:    acpi_ds_init_callbacks
28  *
29  * PARAMETERS:  walk_state      - Current state of the parse tree walk
30  *              pass_number     - 1, 2, or 3
31  *
32  * RETURN:      Status
33  *
34  * DESCRIPTION: Init walk state callbacks
35  *
36  ******************************************************************************/
37 acpi_status
38 acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
39 {
40 
41 	switch (pass_number) {
42 	case 0:
43 
44 		/* Parse only - caller will setup callbacks */
45 
46 		walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
47 		    ACPI_PARSE_DELETE_TREE | ACPI_PARSE_DISASSEMBLE;
48 		walk_state->descending_callback = NULL;
49 		walk_state->ascending_callback = NULL;
50 		break;
51 
52 	case 1:
53 
54 		/* Load pass 1 */
55 
56 		walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
57 		    ACPI_PARSE_DELETE_TREE;
58 		walk_state->descending_callback = acpi_ds_load1_begin_op;
59 		walk_state->ascending_callback = acpi_ds_load1_end_op;
60 		break;
61 
62 	case 2:
63 
64 		/* Load pass 2 */
65 
66 		walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
67 		    ACPI_PARSE_DELETE_TREE;
68 		walk_state->descending_callback = acpi_ds_load2_begin_op;
69 		walk_state->ascending_callback = acpi_ds_load2_end_op;
70 		break;
71 
72 	case 3:
73 
74 		/* Execution pass */
75 
76 		walk_state->parse_flags |= ACPI_PARSE_EXECUTE |
77 		    ACPI_PARSE_DELETE_TREE;
78 		walk_state->descending_callback = acpi_ds_exec_begin_op;
79 		walk_state->ascending_callback = acpi_ds_exec_end_op;
80 		break;
81 
82 	default:
83 
84 		return (AE_BAD_PARAMETER);
85 	}
86 
87 	return (AE_OK);
88 }
89 
90 /*******************************************************************************
91  *
92  * FUNCTION:    acpi_ds_load1_begin_op
93  *
94  * PARAMETERS:  walk_state      - Current state of the parse tree walk
95  *              out_op          - Where to return op if a new one is created
96  *
97  * RETURN:      Status
98  *
99  * DESCRIPTION: Descending callback used during the loading of ACPI tables.
100  *
101  ******************************************************************************/
102 
103 acpi_status
104 acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
105 		       union acpi_parse_object **out_op)
106 {
107 	union acpi_parse_object *op;
108 	struct acpi_namespace_node *node;
109 	acpi_status status;
110 	acpi_object_type object_type;
111 	char *path;
112 	u32 flags;
113 
114 	ACPI_FUNCTION_TRACE_PTR(ds_load1_begin_op, walk_state->op);
115 
116 	op = walk_state->op;
117 	ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
118 			  walk_state));
119 
120 	/* We are only interested in opcodes that have an associated name */
121 
122 	if (op) {
123 		if (!(walk_state->op_info->flags & AML_NAMED)) {
124 			*out_op = op;
125 			return_ACPI_STATUS(AE_OK);
126 		}
127 
128 		/* Check if this object has already been installed in the namespace */
129 
130 		if (op->common.node) {
131 			*out_op = op;
132 			return_ACPI_STATUS(AE_OK);
133 		}
134 	}
135 
136 	path = acpi_ps_get_next_namestring(&walk_state->parser_state);
137 
138 	/* Map the raw opcode into an internal object type */
139 
140 	object_type = walk_state->op_info->object_type;
141 
142 	ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
143 			  "State=%p Op=%p [%s]\n", walk_state, op,
144 			  acpi_ut_get_type_name(object_type)));
145 
146 	switch (walk_state->opcode) {
147 	case AML_SCOPE_OP:
148 		/*
149 		 * The target name of the Scope() operator must exist at this point so
150 		 * that we can actually open the scope to enter new names underneath it.
151 		 * Allow search-to-root for single namesegs.
152 		 */
153 		status =
154 		    acpi_ns_lookup(walk_state->scope_info, path, object_type,
155 				   ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
156 				   walk_state, &(node));
157 #ifdef ACPI_ASL_COMPILER
158 		if (status == AE_NOT_FOUND) {
159 			/*
160 			 * Table disassembly:
161 			 * Target of Scope() not found. Generate an External for it, and
162 			 * insert the name into the namespace.
163 			 */
164 			acpi_dm_add_op_to_external_list(op, path,
165 							ACPI_TYPE_DEVICE, 0, 0);
166 			status =
167 			    acpi_ns_lookup(walk_state->scope_info, path,
168 					   object_type, ACPI_IMODE_LOAD_PASS1,
169 					   ACPI_NS_SEARCH_PARENT, walk_state,
170 					   &node);
171 		}
172 #endif
173 		if (ACPI_FAILURE(status)) {
174 			ACPI_ERROR_NAMESPACE(walk_state->scope_info, path,
175 					     status);
176 			return_ACPI_STATUS(status);
177 		}
178 
179 		/*
180 		 * Check to make sure that the target is
181 		 * one of the opcodes that actually opens a scope
182 		 */
183 		switch (node->type) {
184 		case ACPI_TYPE_ANY:
185 		case ACPI_TYPE_LOCAL_SCOPE:	/* Scope  */
186 		case ACPI_TYPE_DEVICE:
187 		case ACPI_TYPE_POWER:
188 		case ACPI_TYPE_PROCESSOR:
189 		case ACPI_TYPE_THERMAL:
190 
191 			/* These are acceptable types */
192 			break;
193 
194 		case ACPI_TYPE_INTEGER:
195 		case ACPI_TYPE_STRING:
196 		case ACPI_TYPE_BUFFER:
197 			/*
198 			 * These types we will allow, but we will change the type.
199 			 * This enables some existing code of the form:
200 			 *
201 			 *  Name (DEB, 0)
202 			 *  Scope (DEB) { ... }
203 			 *
204 			 * Note: silently change the type here. On the second pass,
205 			 * we will report a warning
206 			 */
207 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
208 					  "Type override - [%4.4s] had invalid type (%s) "
209 					  "for Scope operator, changed to type ANY\n",
210 					  acpi_ut_get_node_name(node),
211 					  acpi_ut_get_type_name(node->type)));
212 
213 			node->type = ACPI_TYPE_ANY;
214 			walk_state->scope_info->common.value = ACPI_TYPE_ANY;
215 			break;
216 
217 		case ACPI_TYPE_METHOD:
218 			/*
219 			 * Allow scope change to root during execution of module-level
220 			 * code. Root is typed METHOD during this time.
221 			 */
222 			if ((node == acpi_gbl_root_node) &&
223 			    (walk_state->
224 			     parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
225 				break;
226 			}
227 
228 			/*lint -fallthrough */
229 
230 		default:
231 
232 			/* All other types are an error */
233 
234 			ACPI_ERROR((AE_INFO,
235 				    "Invalid type (%s) for target of "
236 				    "Scope operator [%4.4s] (Cannot override)",
237 				    acpi_ut_get_type_name(node->type),
238 				    acpi_ut_get_node_name(node)));
239 
240 			return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
241 		}
242 		break;
243 
244 	default:
245 		/*
246 		 * For all other named opcodes, we will enter the name into
247 		 * the namespace.
248 		 *
249 		 * Setup the search flags.
250 		 * Since we are entering a name into the namespace, we do not want to
251 		 * enable the search-to-root upsearch.
252 		 *
253 		 * There are only two conditions where it is acceptable that the name
254 		 * already exists:
255 		 *    1) the Scope() operator can reopen a scoping object that was
256 		 *       previously defined (Scope, Method, Device, etc.)
257 		 *    2) Whenever we are parsing a deferred opcode (op_region, Buffer,
258 		 *       buffer_field, or Package), the name of the object is already
259 		 *       in the namespace.
260 		 */
261 		if (walk_state->deferred_node) {
262 
263 			/* This name is already in the namespace, get the node */
264 
265 			node = walk_state->deferred_node;
266 			status = AE_OK;
267 			break;
268 		}
269 
270 		/*
271 		 * If we are executing a method, do not create any namespace objects
272 		 * during the load phase, only during execution.
273 		 */
274 		if (walk_state->method_node) {
275 			node = NULL;
276 			status = AE_OK;
277 			break;
278 		}
279 
280 		flags = ACPI_NS_NO_UPSEARCH;
281 		if ((walk_state->opcode != AML_SCOPE_OP) &&
282 		    (!(walk_state->parse_flags & ACPI_PARSE_DEFERRED_OP))) {
283 			if (walk_state->namespace_override) {
284 				flags |= ACPI_NS_OVERRIDE_IF_FOUND;
285 				ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
286 						  "[%s] Override allowed\n",
287 						  acpi_ut_get_type_name
288 						  (object_type)));
289 			} else {
290 				flags |= ACPI_NS_ERROR_IF_FOUND;
291 				ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
292 						  "[%s] Cannot already exist\n",
293 						  acpi_ut_get_type_name
294 						  (object_type)));
295 			}
296 		} else {
297 			ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
298 					  "[%s] Both Find or Create allowed\n",
299 					  acpi_ut_get_type_name(object_type)));
300 		}
301 
302 		/*
303 		 * Enter the named type into the internal namespace. We enter the name
304 		 * as we go downward in the parse tree. Any necessary subobjects that
305 		 * involve arguments to the opcode must be created as we go back up the
306 		 * parse tree later.
307 		 */
308 		status =
309 		    acpi_ns_lookup(walk_state->scope_info, path, object_type,
310 				   ACPI_IMODE_LOAD_PASS1, flags, walk_state,
311 				   &node);
312 		if (ACPI_FAILURE(status)) {
313 			if (status == AE_ALREADY_EXISTS) {
314 
315 				/* The name already exists in this scope */
316 
317 				if (node->flags & ANOBJ_IS_EXTERNAL) {
318 					/*
319 					 * Allow one create on an object or segment that was
320 					 * previously declared External
321 					 */
322 					node->flags &= ~ANOBJ_IS_EXTERNAL;
323 					node->type = (u8) object_type;
324 
325 					/* Just retyped a node, probably will need to open a scope */
326 
327 					if (acpi_ns_opens_scope(object_type)) {
328 						status =
329 						    acpi_ds_scope_stack_push
330 						    (node, object_type,
331 						     walk_state);
332 						if (ACPI_FAILURE(status)) {
333 							return_ACPI_STATUS
334 							    (status);
335 						}
336 					}
337 
338 					status = AE_OK;
339 				}
340 			}
341 
342 			if (ACPI_FAILURE(status)) {
343 				ACPI_ERROR_NAMESPACE(walk_state->scope_info,
344 						     path, status);
345 				return_ACPI_STATUS(status);
346 			}
347 		}
348 		break;
349 	}
350 
351 	/* Common exit */
352 
353 	if (!op) {
354 
355 		/* Create a new op */
356 
357 		op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
358 		if (!op) {
359 			return_ACPI_STATUS(AE_NO_MEMORY);
360 		}
361 	}
362 
363 	/* Initialize the op */
364 
365 #ifdef ACPI_CONSTANT_EVAL_ONLY
366 	op->named.path = path;
367 #endif
368 
369 	if (node) {
370 		/*
371 		 * Put the Node in the "op" object that the parser uses, so we
372 		 * can get it again quickly when this scope is closed
373 		 */
374 		op->common.node = node;
375 		op->named.name = node->name.integer;
376 	}
377 
378 	acpi_ps_append_arg(acpi_ps_get_parent_scope(&walk_state->parser_state),
379 			   op);
380 	*out_op = op;
381 	return_ACPI_STATUS(status);
382 }
383 
384 /*******************************************************************************
385  *
386  * FUNCTION:    acpi_ds_load1_end_op
387  *
388  * PARAMETERS:  walk_state      - Current state of the parse tree walk
389  *
390  * RETURN:      Status
391  *
392  * DESCRIPTION: Ascending callback used during the loading of the namespace,
393  *              both control methods and everything else.
394  *
395  ******************************************************************************/
396 
397 acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
398 {
399 	union acpi_parse_object *op;
400 	acpi_object_type object_type;
401 	acpi_status status = AE_OK;
402 
403 #ifdef ACPI_ASL_COMPILER
404 	u8 param_count;
405 #endif
406 
407 	ACPI_FUNCTION_TRACE(ds_load1_end_op);
408 
409 	op = walk_state->op;
410 	ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
411 			  walk_state));
412 
413 	/* We are only interested in opcodes that have an associated name */
414 
415 	if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
416 		return_ACPI_STATUS(AE_OK);
417 	}
418 
419 	/* Get the object type to determine if we should pop the scope */
420 
421 	object_type = walk_state->op_info->object_type;
422 
423 	if (walk_state->op_info->flags & AML_FIELD) {
424 		/*
425 		 * If we are executing a method, do not create any namespace objects
426 		 * during the load phase, only during execution.
427 		 */
428 		if (!walk_state->method_node) {
429 			if (walk_state->opcode == AML_FIELD_OP ||
430 			    walk_state->opcode == AML_BANK_FIELD_OP ||
431 			    walk_state->opcode == AML_INDEX_FIELD_OP) {
432 				status =
433 				    acpi_ds_init_field_objects(op, walk_state);
434 			}
435 		}
436 		return_ACPI_STATUS(status);
437 	}
438 
439 	/*
440 	 * If we are executing a method, do not create any namespace objects
441 	 * during the load phase, only during execution.
442 	 */
443 	if (!walk_state->method_node) {
444 		if (op->common.aml_opcode == AML_REGION_OP) {
445 			status =
446 			    acpi_ex_create_region(op->named.data,
447 						  op->named.length,
448 						  (acpi_adr_space_type)
449 						  ((op->common.value.arg)->
450 						   common.value.integer),
451 						  walk_state);
452 			if (ACPI_FAILURE(status)) {
453 				return_ACPI_STATUS(status);
454 			}
455 		} else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
456 			status =
457 			    acpi_ex_create_region(op->named.data,
458 						  op->named.length,
459 						  ACPI_ADR_SPACE_DATA_TABLE,
460 						  walk_state);
461 			if (ACPI_FAILURE(status)) {
462 				return_ACPI_STATUS(status);
463 			}
464 		}
465 	}
466 
467 	if (op->common.aml_opcode == AML_NAME_OP) {
468 
469 		/* For Name opcode, get the object type from the argument */
470 
471 		if (op->common.value.arg) {
472 			object_type = (acpi_ps_get_opcode_info((op->common.
473 								value.arg)->
474 							       common.
475 							       aml_opcode))->
476 			    object_type;
477 
478 			/* Set node type if we have a namespace node */
479 
480 			if (op->common.node) {
481 				op->common.node->type = (u8) object_type;
482 			}
483 		}
484 	}
485 #ifdef ACPI_ASL_COMPILER
486 	/*
487 	 * For external opcode, get the object type from the argument and
488 	 * get the parameter count from the argument's next.
489 	 */
490 	if (acpi_gbl_disasm_flag &&
491 	    op->common.node && op->common.aml_opcode == AML_EXTERNAL_OP) {
492 		/*
493 		 * Note, if this external is not a method
494 		 * Op->Common.Value.Arg->Common.Next->Common.Value.Integer == 0
495 		 * Therefore, param_count will be 0.
496 		 */
497 		param_count =
498 		    (u8)op->common.value.arg->common.next->common.value.integer;
499 		object_type = (u8)op->common.value.arg->common.value.integer;
500 		op->common.node->flags |= ANOBJ_IS_EXTERNAL;
501 		op->common.node->type = (u8)object_type;
502 
503 		acpi_dm_create_subobject_for_external((u8)object_type,
504 						      &op->common.node,
505 						      param_count);
506 
507 		/*
508 		 * Add the external to the external list because we may be
509 		 * emitting code based off of the items within the external list.
510 		 */
511 		acpi_dm_add_op_to_external_list(op, op->named.path,
512 						(u8)object_type, param_count,
513 						ACPI_EXT_ORIGIN_FROM_OPCODE |
514 						ACPI_EXT_RESOLVED_REFERENCE);
515 	}
516 #endif
517 
518 	/*
519 	 * If we are executing a method, do not create any namespace objects
520 	 * during the load phase, only during execution.
521 	 */
522 	if (!walk_state->method_node) {
523 		if (op->common.aml_opcode == AML_METHOD_OP) {
524 			/*
525 			 * method_op pkg_length name_string method_flags term_list
526 			 *
527 			 * Note: We must create the method node/object pair as soon as we
528 			 * see the method declaration. This allows later pass1 parsing
529 			 * of invocations of the method (need to know the number of
530 			 * arguments.)
531 			 */
532 			ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
533 					  "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
534 					  walk_state, op, op->named.node));
535 
536 			if (!acpi_ns_get_attached_object(op->named.node)) {
537 				walk_state->operands[0] =
538 				    ACPI_CAST_PTR(void, op->named.node);
539 				walk_state->num_operands = 1;
540 
541 				status =
542 				    acpi_ds_create_operands(walk_state,
543 							    op->common.value.
544 							    arg);
545 				if (ACPI_SUCCESS(status)) {
546 					status =
547 					    acpi_ex_create_method(op->named.
548 								  data,
549 								  op->named.
550 								  length,
551 								  walk_state);
552 				}
553 
554 				walk_state->operands[0] = NULL;
555 				walk_state->num_operands = 0;
556 
557 				if (ACPI_FAILURE(status)) {
558 					return_ACPI_STATUS(status);
559 				}
560 			}
561 		}
562 	}
563 
564 	/* Pop the scope stack (only if loading a table) */
565 
566 	if (!walk_state->method_node &&
567 	    op->common.aml_opcode != AML_EXTERNAL_OP &&
568 	    acpi_ns_opens_scope(object_type)) {
569 		ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
570 				  "(%s): Popping scope for Op %p\n",
571 				  acpi_ut_get_type_name(object_type), op));
572 
573 		status = acpi_ds_scope_stack_pop(walk_state);
574 	}
575 
576 	return_ACPI_STATUS(status);
577 }
578