xref: /openbmc/linux/drivers/acpi/acpica/evregion.c (revision f4284724)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: evregion - Operation Region support
5  *
6  * Copyright (C) 2000 - 2022, Intel Corp.
7  *
8  *****************************************************************************/
9 
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acevents.h"
13 #include "acnamesp.h"
14 #include "acinterp.h"
15 
16 #define _COMPONENT          ACPI_EVENTS
17 ACPI_MODULE_NAME("evregion")
18 
19 extern u8 acpi_gbl_default_address_spaces[];
20 
21 /* Local prototypes */
22 
23 static void
24 acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node,
25 				  acpi_adr_space_type space_id);
26 
27 static acpi_status
28 acpi_ev_reg_run(acpi_handle obj_handle,
29 		u32 level, void *context, void **return_value);
30 
31 /*******************************************************************************
32  *
33  * FUNCTION:    acpi_ev_initialize_op_regions
34  *
35  * PARAMETERS:  None
36  *
37  * RETURN:      Status
38  *
39  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
40  *              an installed default region handler.
41  *
42  ******************************************************************************/
43 
44 acpi_status acpi_ev_initialize_op_regions(void)
45 {
46 	acpi_status status;
47 	u32 i;
48 
49 	ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
50 
51 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
52 	if (ACPI_FAILURE(status)) {
53 		return_ACPI_STATUS(status);
54 	}
55 
56 	/* Run the _REG methods for op_regions in each default address space */
57 
58 	for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
59 		/*
60 		 * Make sure the installed handler is the DEFAULT handler. If not the
61 		 * default, the _REG methods will have already been run (when the
62 		 * handler was installed)
63 		 */
64 		if (acpi_ev_has_default_handler(acpi_gbl_root_node,
65 						acpi_gbl_default_address_spaces
66 						[i])) {
67 			acpi_ev_execute_reg_methods(acpi_gbl_root_node,
68 						    acpi_gbl_default_address_spaces
69 						    [i], ACPI_REG_CONNECT);
70 		}
71 	}
72 
73 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
74 	return_ACPI_STATUS(status);
75 }
76 
77 /*******************************************************************************
78  *
79  * FUNCTION:    acpi_ev_address_space_dispatch
80  *
81  * PARAMETERS:  region_obj          - Internal region object
82  *              field_obj           - Corresponding field. Can be NULL.
83  *              function            - Read or Write operation
84  *              region_offset       - Where in the region to read or write
85  *              bit_width           - Field width in bits (8, 16, 32, or 64)
86  *              value               - Pointer to in or out value, must be
87  *                                    a full 64-bit integer
88  *
89  * RETURN:      Status
90  *
91  * DESCRIPTION: Dispatch an address space or operation region access to
92  *              a previously installed handler.
93  *
94  * NOTE: During early initialization, we always install the default region
95  * handlers for Memory, I/O and PCI_Config. This ensures that these operation
96  * region address spaces are always available as per the ACPI specification.
97  * This is especially needed in order to support the execution of
98  * module-level AML code during loading of the ACPI tables.
99  *
100  ******************************************************************************/
101 
102 acpi_status
103 acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
104 			       union acpi_operand_object *field_obj,
105 			       u32 function,
106 			       u32 region_offset, u32 bit_width, u64 *value)
107 {
108 	acpi_status status;
109 	acpi_adr_space_handler handler;
110 	acpi_adr_space_setup region_setup;
111 	union acpi_operand_object *handler_desc;
112 	union acpi_operand_object *region_obj2;
113 	void *region_context = NULL;
114 	struct acpi_connection_info *context;
115 	acpi_mutex context_mutex;
116 	u8 context_locked;
117 	acpi_physical_address address;
118 
119 	ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
120 
121 	region_obj2 = acpi_ns_get_secondary_object(region_obj);
122 	if (!region_obj2) {
123 		return_ACPI_STATUS(AE_NOT_EXIST);
124 	}
125 
126 	/* Ensure that there is a handler associated with this region */
127 
128 	handler_desc = region_obj->region.handler;
129 	if (!handler_desc) {
130 		ACPI_ERROR((AE_INFO,
131 			    "No handler for Region [%4.4s] (%p) [%s]",
132 			    acpi_ut_get_node_name(region_obj->region.node),
133 			    region_obj,
134 			    acpi_ut_get_region_name(region_obj->region.
135 						    space_id)));
136 
137 		return_ACPI_STATUS(AE_NOT_EXIST);
138 	}
139 
140 	context = handler_desc->address_space.context;
141 	context_mutex = handler_desc->address_space.context_mutex;
142 	context_locked = FALSE;
143 
144 	/*
145 	 * It may be the case that the region has never been initialized.
146 	 * Some types of regions require special init code
147 	 */
148 	if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
149 
150 		/* This region has not been initialized yet, do it */
151 
152 		region_setup = handler_desc->address_space.setup;
153 		if (!region_setup) {
154 
155 			/* No initialization routine, exit with error */
156 
157 			ACPI_ERROR((AE_INFO,
158 				    "No init routine for region(%p) [%s]",
159 				    region_obj,
160 				    acpi_ut_get_region_name(region_obj->region.
161 							    space_id)));
162 			return_ACPI_STATUS(AE_NOT_EXIST);
163 		}
164 
165 		if (region_obj->region.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
166 			struct acpi_pcc_info *ctx =
167 			    handler_desc->address_space.context;
168 
169 			ctx->internal_buffer =
170 			    field_obj->field.internal_pcc_buffer;
171 			ctx->length = (u16)region_obj->region.length;
172 			ctx->subspace_id = (u8)region_obj->region.address;
173 		}
174 
175 		/*
176 		 * We must exit the interpreter because the region setup will
177 		 * potentially execute control methods (for example, the _REG method
178 		 * for this region)
179 		 */
180 		acpi_ex_exit_interpreter();
181 
182 		status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
183 				      context, &region_context);
184 
185 		/* Re-enter the interpreter */
186 
187 		acpi_ex_enter_interpreter();
188 
189 		/* Check for failure of the Region Setup */
190 
191 		if (ACPI_FAILURE(status)) {
192 			ACPI_EXCEPTION((AE_INFO, status,
193 					"During region initialization: [%s]",
194 					acpi_ut_get_region_name(region_obj->
195 								region.
196 								space_id)));
197 			return_ACPI_STATUS(status);
198 		}
199 
200 		/* Region initialization may have been completed by region_setup */
201 
202 		if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
203 			region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
204 
205 			/*
206 			 * Save the returned context for use in all accesses to
207 			 * the handler for this particular region
208 			 */
209 			if (!(region_obj2->extra.region_context)) {
210 				region_obj2->extra.region_context =
211 				    region_context;
212 			}
213 		}
214 	}
215 
216 	/* We have everything we need, we can invoke the address space handler */
217 
218 	handler = handler_desc->address_space.handler;
219 	address = (region_obj->region.address + region_offset);
220 
221 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
222 			  "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
223 			  &region_obj->region.handler->address_space, handler,
224 			  ACPI_FORMAT_UINT64(address),
225 			  acpi_ut_get_region_name(region_obj->region.
226 						  space_id)));
227 
228 	if (!(handler_desc->address_space.handler_flags &
229 	      ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
230 		/*
231 		 * For handlers other than the default (supplied) handlers, we must
232 		 * exit the interpreter because the handler *might* block -- we don't
233 		 * know what it will do, so we can't hold the lock on the interpreter.
234 		 */
235 		acpi_ex_exit_interpreter();
236 	}
237 
238 	/*
239 	 * Special handling for generic_serial_bus and general_purpose_io:
240 	 * There are three extra parameters that must be passed to the
241 	 * handler via the context:
242 	 *   1) Connection buffer, a resource template from Connection() op
243 	 *   2) Length of the above buffer
244 	 *   3) Actual access length from the access_as() op
245 	 *
246 	 * Since we pass these extra parameters via the context, which is
247 	 * shared between threads, we must lock the context to avoid these
248 	 * parameters being changed from another thread before the handler
249 	 * has completed running.
250 	 *
251 	 * In addition, for general_purpose_io, the Address and bit_width fields
252 	 * are defined as follows:
253 	 *   1) Address is the pin number index of the field (bit offset from
254 	 *      the previous Connection)
255 	 *   2) bit_width is the actual bit length of the field (number of pins)
256 	 */
257 	if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS ||
258 	     region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
259 	    context && field_obj) {
260 
261 		status =
262 		    acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
263 		if (ACPI_FAILURE(status)) {
264 			goto re_enter_interpreter;
265 		}
266 
267 		context_locked = TRUE;
268 
269 		/* Get the Connection (resource_template) buffer */
270 
271 		context->connection = field_obj->field.resource_buffer;
272 		context->length = field_obj->field.resource_length;
273 		context->access_length = field_obj->field.access_length;
274 
275 		if (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) {
276 			address = field_obj->field.pin_number_index;
277 			bit_width = field_obj->field.bit_length;
278 		}
279 	}
280 
281 	/* Call the handler */
282 
283 	status = handler(function, address, bit_width, value, context,
284 			 region_obj2->extra.region_context);
285 
286 	if (context_locked) {
287 		acpi_os_release_mutex(context_mutex);
288 	}
289 
290 	if (ACPI_FAILURE(status)) {
291 		ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
292 				acpi_ut_get_region_name(region_obj->region.
293 							space_id)));
294 
295 		/*
296 		 * Special case for an EC timeout. These are seen so frequently
297 		 * that an additional error message is helpful
298 		 */
299 		if ((region_obj->region.space_id == ACPI_ADR_SPACE_EC) &&
300 		    (status == AE_TIME)) {
301 			ACPI_ERROR((AE_INFO,
302 				    "Timeout from EC hardware or EC device driver"));
303 		}
304 	}
305 
306 re_enter_interpreter:
307 	if (!(handler_desc->address_space.handler_flags &
308 	      ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
309 		/*
310 		 * We just returned from a non-default handler, we must re-enter the
311 		 * interpreter
312 		 */
313 		acpi_ex_enter_interpreter();
314 	}
315 
316 	return_ACPI_STATUS(status);
317 }
318 
319 /*******************************************************************************
320  *
321  * FUNCTION:    acpi_ev_detach_region
322  *
323  * PARAMETERS:  region_obj          - Region Object
324  *              acpi_ns_is_locked   - Namespace Region Already Locked?
325  *
326  * RETURN:      None
327  *
328  * DESCRIPTION: Break the association between the handler and the region
329  *              this is a two way association.
330  *
331  ******************************************************************************/
332 
333 void
334 acpi_ev_detach_region(union acpi_operand_object *region_obj,
335 		      u8 acpi_ns_is_locked)
336 {
337 	union acpi_operand_object *handler_obj;
338 	union acpi_operand_object *obj_desc;
339 	union acpi_operand_object *start_desc;
340 	union acpi_operand_object **last_obj_ptr;
341 	acpi_adr_space_setup region_setup;
342 	void **region_context;
343 	union acpi_operand_object *region_obj2;
344 	acpi_status status;
345 
346 	ACPI_FUNCTION_TRACE(ev_detach_region);
347 
348 	region_obj2 = acpi_ns_get_secondary_object(region_obj);
349 	if (!region_obj2) {
350 		return_VOID;
351 	}
352 	region_context = &region_obj2->extra.region_context;
353 
354 	/* Get the address handler from the region object */
355 
356 	handler_obj = region_obj->region.handler;
357 	if (!handler_obj) {
358 
359 		/* This region has no handler, all done */
360 
361 		return_VOID;
362 	}
363 
364 	/* Find this region in the handler's list */
365 
366 	obj_desc = handler_obj->address_space.region_list;
367 	start_desc = obj_desc;
368 	last_obj_ptr = &handler_obj->address_space.region_list;
369 
370 	while (obj_desc) {
371 
372 		/* Is this the correct Region? */
373 
374 		if (obj_desc == region_obj) {
375 			ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
376 					  "Removing Region %p from address handler %p\n",
377 					  region_obj, handler_obj));
378 
379 			/* This is it, remove it from the handler's list */
380 
381 			*last_obj_ptr = obj_desc->region.next;
382 			obj_desc->region.next = NULL;	/* Must clear field */
383 
384 			if (acpi_ns_is_locked) {
385 				status =
386 				    acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
387 				if (ACPI_FAILURE(status)) {
388 					return_VOID;
389 				}
390 			}
391 
392 			/* Now stop region accesses by executing the _REG method */
393 
394 			status =
395 			    acpi_ev_execute_reg_method(region_obj,
396 						       ACPI_REG_DISCONNECT);
397 			if (ACPI_FAILURE(status)) {
398 				ACPI_EXCEPTION((AE_INFO, status,
399 						"from region _REG, [%s]",
400 						acpi_ut_get_region_name
401 						(region_obj->region.space_id)));
402 			}
403 
404 			if (acpi_ns_is_locked) {
405 				status =
406 				    acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
407 				if (ACPI_FAILURE(status)) {
408 					return_VOID;
409 				}
410 			}
411 
412 			/*
413 			 * If the region has been activated, call the setup handler with
414 			 * the deactivate notification
415 			 */
416 			if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
417 				region_setup = handler_obj->address_space.setup;
418 				status =
419 				    region_setup(region_obj,
420 						 ACPI_REGION_DEACTIVATE,
421 						 handler_obj->address_space.
422 						 context, region_context);
423 
424 				/*
425 				 * region_context should have been released by the deactivate
426 				 * operation. We don't need access to it anymore here.
427 				 */
428 				if (region_context) {
429 					*region_context = NULL;
430 				}
431 
432 				/* Init routine may fail, Just ignore errors */
433 
434 				if (ACPI_FAILURE(status)) {
435 					ACPI_EXCEPTION((AE_INFO, status,
436 							"from region handler - deactivate, [%s]",
437 							acpi_ut_get_region_name
438 							(region_obj->region.
439 							 space_id)));
440 				}
441 
442 				region_obj->region.flags &=
443 				    ~(AOPOBJ_SETUP_COMPLETE);
444 			}
445 
446 			/*
447 			 * Remove handler reference in the region
448 			 *
449 			 * NOTE: this doesn't mean that the region goes away, the region
450 			 * is just inaccessible as indicated to the _REG method
451 			 *
452 			 * If the region is on the handler's list, this must be the
453 			 * region's handler
454 			 */
455 			region_obj->region.handler = NULL;
456 			acpi_ut_remove_reference(handler_obj);
457 
458 			return_VOID;
459 		}
460 
461 		/* Walk the linked list of handlers */
462 
463 		last_obj_ptr = &obj_desc->region.next;
464 		obj_desc = obj_desc->region.next;
465 
466 		/* Prevent infinite loop if list is corrupted */
467 
468 		if (obj_desc == start_desc) {
469 			ACPI_ERROR((AE_INFO,
470 				    "Circular handler list in region object %p",
471 				    region_obj));
472 			return_VOID;
473 		}
474 	}
475 
476 	/* If we get here, the region was not in the handler's region list */
477 
478 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
479 			  "Cannot remove region %p from address handler %p\n",
480 			  region_obj, handler_obj));
481 
482 	return_VOID;
483 }
484 
485 /*******************************************************************************
486  *
487  * FUNCTION:    acpi_ev_attach_region
488  *
489  * PARAMETERS:  handler_obj         - Handler Object
490  *              region_obj          - Region Object
491  *              acpi_ns_is_locked   - Namespace Region Already Locked?
492  *
493  * RETURN:      None
494  *
495  * DESCRIPTION: Create the association between the handler and the region
496  *              this is a two way association.
497  *
498  ******************************************************************************/
499 
500 acpi_status
501 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
502 		      union acpi_operand_object *region_obj,
503 		      u8 acpi_ns_is_locked)
504 {
505 
506 	ACPI_FUNCTION_TRACE(ev_attach_region);
507 
508 	/* Install the region's handler */
509 
510 	if (region_obj->region.handler) {
511 		return_ACPI_STATUS(AE_ALREADY_EXISTS);
512 	}
513 
514 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
515 			  "Adding Region [%4.4s] %p to address handler %p [%s]\n",
516 			  acpi_ut_get_node_name(region_obj->region.node),
517 			  region_obj, handler_obj,
518 			  acpi_ut_get_region_name(region_obj->region.
519 						  space_id)));
520 
521 	/* Link this region to the front of the handler's list */
522 
523 	region_obj->region.next = handler_obj->address_space.region_list;
524 	handler_obj->address_space.region_list = region_obj;
525 	region_obj->region.handler = handler_obj;
526 	acpi_ut_add_reference(handler_obj);
527 
528 	return_ACPI_STATUS(AE_OK);
529 }
530 
531 /*******************************************************************************
532  *
533  * FUNCTION:    acpi_ev_execute_reg_method
534  *
535  * PARAMETERS:  region_obj          - Region object
536  *              function            - Passed to _REG: On (1) or Off (0)
537  *
538  * RETURN:      Status
539  *
540  * DESCRIPTION: Execute _REG method for a region
541  *
542  ******************************************************************************/
543 
544 acpi_status
545 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
546 {
547 	struct acpi_evaluate_info *info;
548 	union acpi_operand_object *args[3];
549 	union acpi_operand_object *region_obj2;
550 	const acpi_name *reg_name_ptr =
551 	    ACPI_CAST_PTR(acpi_name, METHOD_NAME__REG);
552 	struct acpi_namespace_node *method_node;
553 	struct acpi_namespace_node *node;
554 	acpi_status status;
555 
556 	ACPI_FUNCTION_TRACE(ev_execute_reg_method);
557 
558 	if (!acpi_gbl_namespace_initialized ||
559 	    region_obj->region.handler == NULL) {
560 		return_ACPI_STATUS(AE_OK);
561 	}
562 
563 	region_obj2 = acpi_ns_get_secondary_object(region_obj);
564 	if (!region_obj2) {
565 		return_ACPI_STATUS(AE_NOT_EXIST);
566 	}
567 
568 	/*
569 	 * Find any "_REG" method associated with this region definition.
570 	 * The method should always be updated as this function may be
571 	 * invoked after a namespace change.
572 	 */
573 	node = region_obj->region.node->parent;
574 	status =
575 	    acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
576 				     &method_node);
577 	if (ACPI_SUCCESS(status)) {
578 		/*
579 		 * The _REG method is optional and there can be only one per
580 		 * region definition. This will be executed when the handler is
581 		 * attached or removed.
582 		 */
583 		region_obj2->extra.method_REG = method_node;
584 	}
585 	if (region_obj2->extra.method_REG == NULL) {
586 		return_ACPI_STATUS(AE_OK);
587 	}
588 
589 	/* _REG(DISCONNECT) should be paired with _REG(CONNECT) */
590 
591 	if ((function == ACPI_REG_CONNECT &&
592 	     region_obj->common.flags & AOPOBJ_REG_CONNECTED) ||
593 	    (function == ACPI_REG_DISCONNECT &&
594 	     !(region_obj->common.flags & AOPOBJ_REG_CONNECTED))) {
595 		return_ACPI_STATUS(AE_OK);
596 	}
597 
598 	/* Allocate and initialize the evaluation information block */
599 
600 	info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
601 	if (!info) {
602 		return_ACPI_STATUS(AE_NO_MEMORY);
603 	}
604 
605 	info->prefix_node = region_obj2->extra.method_REG;
606 	info->relative_pathname = NULL;
607 	info->parameters = args;
608 	info->flags = ACPI_IGNORE_RETURN_VALUE;
609 
610 	/*
611 	 * The _REG method has two arguments:
612 	 *
613 	 * arg0 - Integer:
614 	 *  Operation region space ID Same value as region_obj->Region.space_id
615 	 *
616 	 * arg1 - Integer:
617 	 *  connection status 1 for connecting the handler, 0 for disconnecting
618 	 *  the handler (Passed as a parameter)
619 	 */
620 	args[0] =
621 	    acpi_ut_create_integer_object((u64)region_obj->region.space_id);
622 	if (!args[0]) {
623 		status = AE_NO_MEMORY;
624 		goto cleanup1;
625 	}
626 
627 	args[1] = acpi_ut_create_integer_object((u64)function);
628 	if (!args[1]) {
629 		status = AE_NO_MEMORY;
630 		goto cleanup2;
631 	}
632 
633 	args[2] = NULL;		/* Terminate list */
634 
635 	/* Execute the method, no return value */
636 
637 	ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
638 			(ACPI_TYPE_METHOD, info->prefix_node, NULL));
639 
640 	status = acpi_ns_evaluate(info);
641 	acpi_ut_remove_reference(args[1]);
642 
643 	if (ACPI_FAILURE(status)) {
644 		goto cleanup2;
645 	}
646 
647 	if (function == ACPI_REG_CONNECT) {
648 		region_obj->common.flags |= AOPOBJ_REG_CONNECTED;
649 	} else {
650 		region_obj->common.flags &= ~AOPOBJ_REG_CONNECTED;
651 	}
652 
653 cleanup2:
654 	acpi_ut_remove_reference(args[0]);
655 
656 cleanup1:
657 	ACPI_FREE(info);
658 	return_ACPI_STATUS(status);
659 }
660 
661 /*******************************************************************************
662  *
663  * FUNCTION:    acpi_ev_execute_reg_methods
664  *
665  * PARAMETERS:  node            - Namespace node for the device
666  *              space_id        - The address space ID
667  *              function        - Passed to _REG: On (1) or Off (0)
668  *
669  * RETURN:      None
670  *
671  * DESCRIPTION: Run all _REG methods for the input Space ID;
672  *              Note: assumes namespace is locked, or system init time.
673  *
674  ******************************************************************************/
675 
676 void
677 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
678 			    acpi_adr_space_type space_id, u32 function)
679 {
680 	struct acpi_reg_walk_info info;
681 
682 	ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
683 
684 	/*
685 	 * These address spaces do not need a call to _REG, since the ACPI
686 	 * specification defines them as: "must always be accessible". Since
687 	 * they never change state (never become unavailable), no need to ever
688 	 * call _REG on them. Also, a data_table is not a "real" address space,
689 	 * so do not call _REG. September 2018.
690 	 */
691 	if ((space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) ||
692 	    (space_id == ACPI_ADR_SPACE_SYSTEM_IO) ||
693 	    (space_id == ACPI_ADR_SPACE_DATA_TABLE)) {
694 		return_VOID;
695 	}
696 
697 	info.space_id = space_id;
698 	info.function = function;
699 	info.reg_run_count = 0;
700 
701 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
702 			      "    Running _REG methods for SpaceId %s\n",
703 			      acpi_ut_get_region_name(info.space_id)));
704 
705 	/*
706 	 * Run all _REG methods for all Operation Regions for this space ID. This
707 	 * is a separate walk in order to handle any interdependencies between
708 	 * regions and _REG methods. (i.e. handlers must be installed for all
709 	 * regions of this Space ID before we can run any _REG methods)
710 	 */
711 	(void)acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
712 				     ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run, NULL,
713 				     &info, NULL);
714 
715 	/*
716 	 * Special case for EC and GPIO: handle "orphan" _REG methods with
717 	 * no region.
718 	 */
719 	if (space_id == ACPI_ADR_SPACE_EC || space_id == ACPI_ADR_SPACE_GPIO) {
720 		acpi_ev_execute_orphan_reg_method(node, space_id);
721 	}
722 
723 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
724 			      "    Executed %u _REG methods for SpaceId %s\n",
725 			      info.reg_run_count,
726 			      acpi_ut_get_region_name(info.space_id)));
727 
728 	return_VOID;
729 }
730 
731 /*******************************************************************************
732  *
733  * FUNCTION:    acpi_ev_reg_run
734  *
735  * PARAMETERS:  walk_namespace callback
736  *
737  * DESCRIPTION: Run _REG method for region objects of the requested spaceID
738  *
739  ******************************************************************************/
740 
741 static acpi_status
742 acpi_ev_reg_run(acpi_handle obj_handle,
743 		u32 level, void *context, void **return_value)
744 {
745 	union acpi_operand_object *obj_desc;
746 	struct acpi_namespace_node *node;
747 	acpi_status status;
748 	struct acpi_reg_walk_info *info;
749 
750 	info = ACPI_CAST_PTR(struct acpi_reg_walk_info, context);
751 
752 	/* Convert and validate the device handle */
753 
754 	node = acpi_ns_validate_handle(obj_handle);
755 	if (!node) {
756 		return (AE_BAD_PARAMETER);
757 	}
758 
759 	/*
760 	 * We only care about regions and objects that are allowed to have
761 	 * address space handlers
762 	 */
763 	if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
764 		return (AE_OK);
765 	}
766 
767 	/* Check for an existing internal object */
768 
769 	obj_desc = acpi_ns_get_attached_object(node);
770 	if (!obj_desc) {
771 
772 		/* No object, just exit */
773 
774 		return (AE_OK);
775 	}
776 
777 	/* Object is a Region */
778 
779 	if (obj_desc->region.space_id != info->space_id) {
780 
781 		/* This region is for a different address space, just ignore it */
782 
783 		return (AE_OK);
784 	}
785 
786 	info->reg_run_count++;
787 	status = acpi_ev_execute_reg_method(obj_desc, info->function);
788 	return (status);
789 }
790 
791 /*******************************************************************************
792  *
793  * FUNCTION:    acpi_ev_execute_orphan_reg_method
794  *
795  * PARAMETERS:  device_node         - Namespace node for an ACPI device
796  *              space_id            - The address space ID
797  *
798  * RETURN:      None
799  *
800  * DESCRIPTION: Execute an "orphan" _REG method that appears under an ACPI
801  *              device. This is a _REG method that has no corresponding region
802  *              within the device's scope. ACPI tables depending on these
803  *              "orphan" _REG methods have been seen for both EC and GPIO
804  *              Operation Regions. Presumably the Windows ACPI implementation
805  *              always calls the _REG method independent of the presence of
806  *              an actual Operation Region with the correct address space ID.
807  *
808  *  MUTEX:      Assumes the namespace is locked
809  *
810  ******************************************************************************/
811 
812 static void
813 acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node,
814 				  acpi_adr_space_type space_id)
815 {
816 	acpi_handle reg_method;
817 	struct acpi_namespace_node *next_node;
818 	acpi_status status;
819 	struct acpi_object_list args;
820 	union acpi_object objects[2];
821 
822 	ACPI_FUNCTION_TRACE(ev_execute_orphan_reg_method);
823 
824 	if (!device_node) {
825 		return_VOID;
826 	}
827 
828 	/* Namespace is currently locked, must release */
829 
830 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
831 
832 	/* Get a handle to a _REG method immediately under the EC device */
833 
834 	status = acpi_get_handle(device_node, METHOD_NAME__REG, &reg_method);
835 	if (ACPI_FAILURE(status)) {
836 		goto exit;	/* There is no _REG method present */
837 	}
838 
839 	/*
840 	 * Execute the _REG method only if there is no Operation Region in
841 	 * this scope with the Embedded Controller space ID. Otherwise, it
842 	 * will already have been executed. Note, this allows for Regions
843 	 * with other space IDs to be present; but the code below will then
844 	 * execute the _REG method with the embedded_control space_ID argument.
845 	 */
846 	next_node = acpi_ns_get_next_node(device_node, NULL);
847 	while (next_node) {
848 		if ((next_node->type == ACPI_TYPE_REGION) &&
849 		    (next_node->object) &&
850 		    (next_node->object->region.space_id == space_id)) {
851 			goto exit;	/* Do not execute the _REG */
852 		}
853 
854 		next_node = acpi_ns_get_next_node(device_node, next_node);
855 	}
856 
857 	/* Evaluate the _REG(space_id,Connect) method */
858 
859 	args.count = 2;
860 	args.pointer = objects;
861 	objects[0].type = ACPI_TYPE_INTEGER;
862 	objects[0].integer.value = space_id;
863 	objects[1].type = ACPI_TYPE_INTEGER;
864 	objects[1].integer.value = ACPI_REG_CONNECT;
865 
866 	(void)acpi_evaluate_object(reg_method, NULL, &args, NULL);
867 
868 exit:
869 	/* We ignore all errors from above, don't care */
870 
871 	(void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
872 	return_VOID;
873 }
874