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