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