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