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