xref: /openbmc/linux/drivers/acpi/acpica/evregion.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown  *
442f8fb75SBob Moore  * Module Name: evregion - Operation Region support
595b482a8SLen Brown  *
6612c2932SBob Moore  * Copyright (C) 2000 - 2023, Intel Corp.
795b482a8SLen Brown  *
895857638SErik Schmauss  *****************************************************************************/
995b482a8SLen Brown 
1095b482a8SLen Brown #include <acpi/acpi.h>
11e2f7a777SLen Brown #include "accommon.h"
12e2f7a777SLen Brown #include "acevents.h"
13e2f7a777SLen Brown #include "acnamesp.h"
14e2f7a777SLen Brown #include "acinterp.h"
1595b482a8SLen Brown 
1695b482a8SLen Brown #define _COMPONENT          ACPI_EVENTS
1795b482a8SLen Brown ACPI_MODULE_NAME("evregion")
1895b482a8SLen Brown 
1942f8fb75SBob Moore extern u8 acpi_gbl_default_address_spaces[];
2042f8fb75SBob Moore 
2195b482a8SLen Brown /* Local prototypes */
2274d3ec77SLin Ming 
237adc8a3dSRafael J. Wysocki static void
247adc8a3dSRafael J. Wysocki acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node,
257adc8a3dSRafael J. Wysocki 				  acpi_adr_space_type space_id);
267adc8a3dSRafael J. Wysocki 
2795b482a8SLen Brown static acpi_status
2895b482a8SLen Brown acpi_ev_reg_run(acpi_handle obj_handle,
2995b482a8SLen Brown 		u32 level, void *context, void **return_value);
3095b482a8SLen Brown 
3174d3ec77SLin Ming /*******************************************************************************
3274d3ec77SLin Ming  *
3395b482a8SLen Brown  * FUNCTION:    acpi_ev_initialize_op_regions
3495b482a8SLen Brown  *
3595b482a8SLen Brown  * PARAMETERS:  None
3695b482a8SLen Brown  *
3795b482a8SLen Brown  * RETURN:      Status
3895b482a8SLen Brown  *
3995b482a8SLen Brown  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
4095b482a8SLen Brown  *              an installed default region handler.
4195b482a8SLen Brown  *
4295b482a8SLen Brown  ******************************************************************************/
4395b482a8SLen Brown 
acpi_ev_initialize_op_regions(void)4495b482a8SLen Brown acpi_status acpi_ev_initialize_op_regions(void)
4595b482a8SLen Brown {
4695b482a8SLen Brown 	acpi_status status;
4795b482a8SLen Brown 	u32 i;
4895b482a8SLen Brown 
4995b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
5095b482a8SLen Brown 
5195b482a8SLen Brown 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
5295b482a8SLen Brown 	if (ACPI_FAILURE(status)) {
5395b482a8SLen Brown 		return_ACPI_STATUS(status);
5495b482a8SLen Brown 	}
5595b482a8SLen Brown 
5695b482a8SLen Brown 	/* Run the _REG methods for op_regions in each default address space */
5795b482a8SLen Brown 
5895b482a8SLen Brown 	for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
5995b482a8SLen Brown 		/*
6074d3ec77SLin Ming 		 * Make sure the installed handler is the DEFAULT handler. If not the
6174d3ec77SLin Ming 		 * default, the _REG methods will have already been run (when the
6274d3ec77SLin Ming 		 * handler was installed)
6395b482a8SLen Brown 		 */
6474d3ec77SLin Ming 		if (acpi_ev_has_default_handler(acpi_gbl_root_node,
6574d3ec77SLin Ming 						acpi_gbl_default_address_spaces
6674d3ec77SLin Ming 						[i])) {
6774d3ec77SLin Ming 			acpi_ev_execute_reg_methods(acpi_gbl_root_node,
68*03fd525dSRafael J. Wysocki 						    ACPI_UINT32_MAX,
6995b482a8SLen Brown 						    acpi_gbl_default_address_spaces
70d815346fSLv Zheng 						    [i], ACPI_REG_CONNECT);
7195b482a8SLen Brown 		}
7274d3ec77SLin Ming 	}
7395b482a8SLen Brown 
7495b482a8SLen Brown 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
7595b482a8SLen Brown 	return_ACPI_STATUS(status);
7695b482a8SLen Brown }
7795b482a8SLen Brown 
7895b482a8SLen Brown /*******************************************************************************
7995b482a8SLen Brown  *
8095b482a8SLen Brown  * FUNCTION:    acpi_ev_address_space_dispatch
8195b482a8SLen Brown  *
8295b482a8SLen Brown  * PARAMETERS:  region_obj          - Internal region object
839ce81784SBob Moore  *              field_obj           - Corresponding field. Can be NULL.
84ba494beeSBob Moore  *              function            - Read or Write operation
85f5407af3SBob Moore  *              region_offset       - Where in the region to read or write
8695b482a8SLen Brown  *              bit_width           - Field width in bits (8, 16, 32, or 64)
87ba494beeSBob Moore  *              value               - Pointer to in or out value, must be
885df7e6cbSBob Moore  *                                    a full 64-bit integer
8995b482a8SLen Brown  *
9095b482a8SLen Brown  * RETURN:      Status
9195b482a8SLen Brown  *
9295b482a8SLen Brown  * DESCRIPTION: Dispatch an address space or operation region access to
9395b482a8SLen Brown  *              a previously installed handler.
9495b482a8SLen Brown  *
957b738064SBob Moore  * NOTE: During early initialization, we always install the default region
967b738064SBob Moore  * handlers for Memory, I/O and PCI_Config. This ensures that these operation
977b738064SBob Moore  * region address spaces are always available as per the ACPI specification.
987b738064SBob Moore  * This is especially needed in order to support the execution of
997b738064SBob Moore  * module-level AML code during loading of the ACPI tables.
1007b738064SBob Moore  *
10195b482a8SLen Brown  ******************************************************************************/
10295b482a8SLen Brown 
10395b482a8SLen Brown 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)10495b482a8SLen Brown acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
1059ce81784SBob Moore 			       union acpi_operand_object *field_obj,
10695b482a8SLen Brown 			       u32 function,
1075df7e6cbSBob Moore 			       u32 region_offset, u32 bit_width, u64 *value)
10895b482a8SLen Brown {
10995b482a8SLen Brown 	acpi_status status;
11095b482a8SLen Brown 	acpi_adr_space_handler handler;
11195b482a8SLen Brown 	acpi_adr_space_setup region_setup;
11295b482a8SLen Brown 	union acpi_operand_object *handler_desc;
11395b482a8SLen Brown 	union acpi_operand_object *region_obj2;
11495b482a8SLen Brown 	void *region_context = NULL;
1159ce81784SBob Moore 	struct acpi_connection_info *context;
116c27f3d01SHans de Goede 	acpi_mutex context_mutex;
117c27f3d01SHans de Goede 	u8 context_locked;
11875ec6e55SBob Moore 	acpi_physical_address address;
11995b482a8SLen Brown 
12095b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
12195b482a8SLen Brown 
12295b482a8SLen Brown 	region_obj2 = acpi_ns_get_secondary_object(region_obj);
12395b482a8SLen Brown 	if (!region_obj2) {
12495b482a8SLen Brown 		return_ACPI_STATUS(AE_NOT_EXIST);
12595b482a8SLen Brown 	}
12695b482a8SLen Brown 
12795b482a8SLen Brown 	/* Ensure that there is a handler associated with this region */
12895b482a8SLen Brown 
12995b482a8SLen Brown 	handler_desc = region_obj->region.handler;
13095b482a8SLen Brown 	if (!handler_desc) {
13195b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
13295b482a8SLen Brown 			    "No handler for Region [%4.4s] (%p) [%s]",
13395b482a8SLen Brown 			    acpi_ut_get_node_name(region_obj->region.node),
13495b482a8SLen Brown 			    region_obj,
13595b482a8SLen Brown 			    acpi_ut_get_region_name(region_obj->region.
13695b482a8SLen Brown 						    space_id)));
13795b482a8SLen Brown 
13895b482a8SLen Brown 		return_ACPI_STATUS(AE_NOT_EXIST);
13995b482a8SLen Brown 	}
14095b482a8SLen Brown 
1419ce81784SBob Moore 	context = handler_desc->address_space.context;
142c27f3d01SHans de Goede 	context_mutex = handler_desc->address_space.context_mutex;
143c27f3d01SHans de Goede 	context_locked = FALSE;
1449ce81784SBob Moore 
14595b482a8SLen Brown 	/*
14695b482a8SLen Brown 	 * It may be the case that the region has never been initialized.
14795b482a8SLen Brown 	 * Some types of regions require special init code
14895b482a8SLen Brown 	 */
14995b482a8SLen Brown 	if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
15095b482a8SLen Brown 
15195b482a8SLen Brown 		/* This region has not been initialized yet, do it */
15295b482a8SLen Brown 
15395b482a8SLen Brown 		region_setup = handler_desc->address_space.setup;
15495b482a8SLen Brown 		if (!region_setup) {
15595b482a8SLen Brown 
15695b482a8SLen Brown 			/* No initialization routine, exit with error */
15795b482a8SLen Brown 
15895b482a8SLen Brown 			ACPI_ERROR((AE_INFO,
15995b482a8SLen Brown 				    "No init routine for region(%p) [%s]",
16095b482a8SLen Brown 				    region_obj,
16195b482a8SLen Brown 				    acpi_ut_get_region_name(region_obj->region.
16295b482a8SLen Brown 							    space_id)));
16395b482a8SLen Brown 			return_ACPI_STATUS(AE_NOT_EXIST);
16495b482a8SLen Brown 		}
16595b482a8SLen Brown 
1660acf24adSSudeep Holla 		if (region_obj->region.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
1670acf24adSSudeep Holla 			struct acpi_pcc_info *ctx =
1680acf24adSSudeep Holla 			    handler_desc->address_space.context;
1690acf24adSSudeep Holla 
1700acf24adSSudeep Holla 			ctx->internal_buffer =
1710acf24adSSudeep Holla 			    field_obj->field.internal_pcc_buffer;
172b70d6f07SBob Moore 			ctx->length = (u16)region_obj->region.length;
173b70d6f07SBob Moore 			ctx->subspace_id = (u8)region_obj->region.address;
1740acf24adSSudeep Holla 		}
1750acf24adSSudeep Holla 
176ee64b827SSudeep Holla 		if (region_obj->region.space_id ==
177ee64b827SSudeep Holla 		    ACPI_ADR_SPACE_FIXED_HARDWARE) {
178ee64b827SSudeep Holla 			struct acpi_ffh_info *ctx =
179ee64b827SSudeep Holla 			    handler_desc->address_space.context;
180ee64b827SSudeep Holla 
181ee64b827SSudeep Holla 			ctx->length = region_obj->region.length;
182ee64b827SSudeep Holla 			ctx->offset = region_obj->region.address;
183ee64b827SSudeep Holla 		}
184ee64b827SSudeep Holla 
18595b482a8SLen Brown 		/*
18695b482a8SLen Brown 		 * We must exit the interpreter because the region setup will
18795b482a8SLen Brown 		 * potentially execute control methods (for example, the _REG method
18895b482a8SLen Brown 		 * for this region)
18995b482a8SLen Brown 		 */
19095b482a8SLen Brown 		acpi_ex_exit_interpreter();
19195b482a8SLen Brown 
19295b482a8SLen Brown 		status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
1939ce81784SBob Moore 				      context, &region_context);
19495b482a8SLen Brown 
19595b482a8SLen Brown 		/* Re-enter the interpreter */
19695b482a8SLen Brown 
19795b482a8SLen Brown 		acpi_ex_enter_interpreter();
19895b482a8SLen Brown 
19995b482a8SLen Brown 		/* Check for failure of the Region Setup */
20095b482a8SLen Brown 
20195b482a8SLen Brown 		if (ACPI_FAILURE(status)) {
20295b482a8SLen Brown 			ACPI_EXCEPTION((AE_INFO, status,
20395b482a8SLen Brown 					"During region initialization: [%s]",
20495b482a8SLen Brown 					acpi_ut_get_region_name(region_obj->
20595b482a8SLen Brown 								region.
20695b482a8SLen Brown 								space_id)));
20795b482a8SLen Brown 			return_ACPI_STATUS(status);
20895b482a8SLen Brown 		}
20995b482a8SLen Brown 
21095b482a8SLen Brown 		/* Region initialization may have been completed by region_setup */
21195b482a8SLen Brown 
21295b482a8SLen Brown 		if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
21395b482a8SLen Brown 			region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
21495b482a8SLen Brown 
21595b482a8SLen Brown 			/*
21695b482a8SLen Brown 			 * Save the returned context for use in all accesses to
217d2e7d079SDavid E. Box 			 * the handler for this particular region
21895b482a8SLen Brown 			 */
219d2e7d079SDavid E. Box 			if (!(region_obj2->extra.region_context)) {
22095b482a8SLen Brown 				region_obj2->extra.region_context =
22195b482a8SLen Brown 				    region_context;
22295b482a8SLen Brown 			}
22395b482a8SLen Brown 		}
22495b482a8SLen Brown 	}
22595b482a8SLen Brown 
22695b482a8SLen Brown 	/* We have everything we need, we can invoke the address space handler */
22795b482a8SLen Brown 
22895b482a8SLen Brown 	handler = handler_desc->address_space.handler;
22975ec6e55SBob Moore 	address = (region_obj->region.address + region_offset);
23095b482a8SLen Brown 
23175ec6e55SBob Moore 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
23275ec6e55SBob Moore 			  "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
23375ec6e55SBob Moore 			  &region_obj->region.handler->address_space, handler,
2341d0a0b2fSLv Zheng 			  ACPI_FORMAT_UINT64(address),
23575ec6e55SBob Moore 			  acpi_ut_get_region_name(region_obj->region.
23675ec6e55SBob Moore 						  space_id)));
2379ce81784SBob Moore 
23895b482a8SLen Brown 	if (!(handler_desc->address_space.handler_flags &
23995b482a8SLen Brown 	      ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
24095b482a8SLen Brown 		/*
24195b482a8SLen Brown 		 * For handlers other than the default (supplied) handlers, we must
24295b482a8SLen Brown 		 * exit the interpreter because the handler *might* block -- we don't
243c163f90cSErik Schmauss 		 * know what it will do, so we can't hold the lock on the interpreter.
24495b482a8SLen Brown 		 */
24595b482a8SLen Brown 		acpi_ex_exit_interpreter();
24695b482a8SLen Brown 	}
24795b482a8SLen Brown 
248c27f3d01SHans de Goede 	/*
249c27f3d01SHans de Goede 	 * Special handling for generic_serial_bus and general_purpose_io:
250c27f3d01SHans de Goede 	 * There are three extra parameters that must be passed to the
251c27f3d01SHans de Goede 	 * handler via the context:
252c27f3d01SHans de Goede 	 *   1) Connection buffer, a resource template from Connection() op
253c27f3d01SHans de Goede 	 *   2) Length of the above buffer
254c27f3d01SHans de Goede 	 *   3) Actual access length from the access_as() op
255c27f3d01SHans de Goede 	 *
256c27f3d01SHans de Goede 	 * Since we pass these extra parameters via the context, which is
257c27f3d01SHans de Goede 	 * shared between threads, we must lock the context to avoid these
258c27f3d01SHans de Goede 	 * parameters being changed from another thread before the handler
259c27f3d01SHans de Goede 	 * has completed running.
260c27f3d01SHans de Goede 	 *
261c27f3d01SHans de Goede 	 * In addition, for general_purpose_io, the Address and bit_width fields
262c27f3d01SHans de Goede 	 * are defined as follows:
263c27f3d01SHans de Goede 	 *   1) Address is the pin number index of the field (bit offset from
264c27f3d01SHans de Goede 	 *      the previous Connection)
265c27f3d01SHans de Goede 	 *   2) bit_width is the actual bit length of the field (number of pins)
266c27f3d01SHans de Goede 	 */
2678f6493d1SHans de Goede 	if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS ||
2688f6493d1SHans de Goede 	     region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
269c27f3d01SHans de Goede 	    context && field_obj) {
270c27f3d01SHans de Goede 
271c27f3d01SHans de Goede 		status =
272c27f3d01SHans de Goede 		    acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
273c27f3d01SHans de Goede 		if (ACPI_FAILURE(status)) {
274c27f3d01SHans de Goede 			goto re_enter_interpreter;
275c27f3d01SHans de Goede 		}
276c27f3d01SHans de Goede 
277c27f3d01SHans de Goede 		context_locked = TRUE;
278c27f3d01SHans de Goede 
279c27f3d01SHans de Goede 		/* Get the Connection (resource_template) buffer */
280c27f3d01SHans de Goede 
281c27f3d01SHans de Goede 		context->connection = field_obj->field.resource_buffer;
282c27f3d01SHans de Goede 		context->length = field_obj->field.resource_length;
283c27f3d01SHans de Goede 		context->access_length = field_obj->field.access_length;
284c27f3d01SHans de Goede 
2858f6493d1SHans de Goede 		if (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) {
286c27f3d01SHans de Goede 			address = field_obj->field.pin_number_index;
287c27f3d01SHans de Goede 			bit_width = field_obj->field.bit_length;
288c27f3d01SHans de Goede 		}
2898f6493d1SHans de Goede 	}
290c27f3d01SHans de Goede 
29195b482a8SLen Brown 	/* Call the handler */
29295b482a8SLen Brown 
29375ec6e55SBob Moore 	status = handler(function, address, bit_width, value, context,
29495b482a8SLen Brown 			 region_obj2->extra.region_context);
29595b482a8SLen Brown 
296c27f3d01SHans de Goede 	if (context_locked) {
297c27f3d01SHans de Goede 		acpi_os_release_mutex(context_mutex);
298c27f3d01SHans de Goede 	}
299c27f3d01SHans de Goede 
30095b482a8SLen Brown 	if (ACPI_FAILURE(status)) {
30195b482a8SLen Brown 		ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
30295b482a8SLen Brown 				acpi_ut_get_region_name(region_obj->region.
30395b482a8SLen Brown 							space_id)));
30453ae81e1SBob Moore 
30553ae81e1SBob Moore 		/*
30653ae81e1SBob Moore 		 * Special case for an EC timeout. These are seen so frequently
30753ae81e1SBob Moore 		 * that an additional error message is helpful
30853ae81e1SBob Moore 		 */
30953ae81e1SBob Moore 		if ((region_obj->region.space_id == ACPI_ADR_SPACE_EC) &&
31053ae81e1SBob Moore 		    (status == AE_TIME)) {
31153ae81e1SBob Moore 			ACPI_ERROR((AE_INFO,
31253ae81e1SBob Moore 				    "Timeout from EC hardware or EC device driver"));
31353ae81e1SBob Moore 		}
31495b482a8SLen Brown 	}
31595b482a8SLen Brown 
316c27f3d01SHans de Goede re_enter_interpreter:
31795b482a8SLen Brown 	if (!(handler_desc->address_space.handler_flags &
31895b482a8SLen Brown 	      ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
31995b482a8SLen Brown 		/*
32095b482a8SLen Brown 		 * We just returned from a non-default handler, we must re-enter the
32195b482a8SLen Brown 		 * interpreter
32295b482a8SLen Brown 		 */
32395b482a8SLen Brown 		acpi_ex_enter_interpreter();
32495b482a8SLen Brown 	}
32595b482a8SLen Brown 
32695b482a8SLen Brown 	return_ACPI_STATUS(status);
32795b482a8SLen Brown }
32895b482a8SLen Brown 
32995b482a8SLen Brown /*******************************************************************************
33095b482a8SLen Brown  *
33195b482a8SLen Brown  * FUNCTION:    acpi_ev_detach_region
33295b482a8SLen Brown  *
33395b482a8SLen Brown  * PARAMETERS:  region_obj          - Region Object
33495b482a8SLen Brown  *              acpi_ns_is_locked   - Namespace Region Already Locked?
33595b482a8SLen Brown  *
33695b482a8SLen Brown  * RETURN:      None
33795b482a8SLen Brown  *
33895b482a8SLen Brown  * DESCRIPTION: Break the association between the handler and the region
33995b482a8SLen Brown  *              this is a two way association.
34095b482a8SLen Brown  *
34195b482a8SLen Brown  ******************************************************************************/
34295b482a8SLen Brown 
34395b482a8SLen Brown void
acpi_ev_detach_region(union acpi_operand_object * region_obj,u8 acpi_ns_is_locked)34495b482a8SLen Brown acpi_ev_detach_region(union acpi_operand_object *region_obj,
34595b482a8SLen Brown 		      u8 acpi_ns_is_locked)
34695b482a8SLen Brown {
34795b482a8SLen Brown 	union acpi_operand_object *handler_obj;
34895b482a8SLen Brown 	union acpi_operand_object *obj_desc;
349f953529fSBob Moore 	union acpi_operand_object *start_desc;
35095b482a8SLen Brown 	union acpi_operand_object **last_obj_ptr;
35195b482a8SLen Brown 	acpi_adr_space_setup region_setup;
35295b482a8SLen Brown 	void **region_context;
35395b482a8SLen Brown 	union acpi_operand_object *region_obj2;
35495b482a8SLen Brown 	acpi_status status;
35595b482a8SLen Brown 
35695b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ev_detach_region);
35795b482a8SLen Brown 
35895b482a8SLen Brown 	region_obj2 = acpi_ns_get_secondary_object(region_obj);
35995b482a8SLen Brown 	if (!region_obj2) {
36095b482a8SLen Brown 		return_VOID;
36195b482a8SLen Brown 	}
36295b482a8SLen Brown 	region_context = &region_obj2->extra.region_context;
36395b482a8SLen Brown 
36495b482a8SLen Brown 	/* Get the address handler from the region object */
36595b482a8SLen Brown 
36695b482a8SLen Brown 	handler_obj = region_obj->region.handler;
36795b482a8SLen Brown 	if (!handler_obj) {
36895b482a8SLen Brown 
36995b482a8SLen Brown 		/* This region has no handler, all done */
37095b482a8SLen Brown 
37195b482a8SLen Brown 		return_VOID;
37295b482a8SLen Brown 	}
37395b482a8SLen Brown 
37495b482a8SLen Brown 	/* Find this region in the handler's list */
37595b482a8SLen Brown 
37695b482a8SLen Brown 	obj_desc = handler_obj->address_space.region_list;
377f953529fSBob Moore 	start_desc = obj_desc;
37895b482a8SLen Brown 	last_obj_ptr = &handler_obj->address_space.region_list;
37995b482a8SLen Brown 
38095b482a8SLen Brown 	while (obj_desc) {
38195b482a8SLen Brown 
38295b482a8SLen Brown 		/* Is this the correct Region? */
38395b482a8SLen Brown 
38495b482a8SLen Brown 		if (obj_desc == region_obj) {
38595b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
38695b482a8SLen Brown 					  "Removing Region %p from address handler %p\n",
38795b482a8SLen Brown 					  region_obj, handler_obj));
38895b482a8SLen Brown 
38995b482a8SLen Brown 			/* This is it, remove it from the handler's list */
39095b482a8SLen Brown 
39195b482a8SLen Brown 			*last_obj_ptr = obj_desc->region.next;
39295b482a8SLen Brown 			obj_desc->region.next = NULL;	/* Must clear field */
39395b482a8SLen Brown 
39495b482a8SLen Brown 			if (acpi_ns_is_locked) {
39595b482a8SLen Brown 				status =
39695b482a8SLen Brown 				    acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
39795b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
39895b482a8SLen Brown 					return_VOID;
39995b482a8SLen Brown 				}
40095b482a8SLen Brown 			}
40195b482a8SLen Brown 
40295b482a8SLen Brown 			/* Now stop region accesses by executing the _REG method */
40395b482a8SLen Brown 
404e2066ca1SBob Moore 			status =
405e2066ca1SBob Moore 			    acpi_ev_execute_reg_method(region_obj,
406e2066ca1SBob Moore 						       ACPI_REG_DISCONNECT);
40795b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
40895b482a8SLen Brown 				ACPI_EXCEPTION((AE_INFO, status,
40995b482a8SLen Brown 						"from region _REG, [%s]",
41095b482a8SLen Brown 						acpi_ut_get_region_name
41195b482a8SLen Brown 						(region_obj->region.space_id)));
41295b482a8SLen Brown 			}
41395b482a8SLen Brown 
41495b482a8SLen Brown 			if (acpi_ns_is_locked) {
41595b482a8SLen Brown 				status =
41695b482a8SLen Brown 				    acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
41795b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
41895b482a8SLen Brown 					return_VOID;
41995b482a8SLen Brown 				}
42095b482a8SLen Brown 			}
42195b482a8SLen Brown 
42295b482a8SLen Brown 			/*
42395b482a8SLen Brown 			 * If the region has been activated, call the setup handler with
42495b482a8SLen Brown 			 * the deactivate notification
42595b482a8SLen Brown 			 */
42695b482a8SLen Brown 			if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
42795b482a8SLen Brown 				region_setup = handler_obj->address_space.setup;
42895b482a8SLen Brown 				status =
42995b482a8SLen Brown 				    region_setup(region_obj,
43095b482a8SLen Brown 						 ACPI_REGION_DEACTIVATE,
43195b482a8SLen Brown 						 handler_obj->address_space.
43295b482a8SLen Brown 						 context, region_context);
43395b482a8SLen Brown 
434d2e7d079SDavid E. Box 				/*
435d2e7d079SDavid E. Box 				 * region_context should have been released by the deactivate
436d2e7d079SDavid E. Box 				 * operation. We don't need access to it anymore here.
437d2e7d079SDavid E. Box 				 */
438d2e7d079SDavid E. Box 				if (region_context) {
439d2e7d079SDavid E. Box 					*region_context = NULL;
440d2e7d079SDavid E. Box 				}
441d2e7d079SDavid E. Box 
44295b482a8SLen Brown 				/* Init routine may fail, Just ignore errors */
44395b482a8SLen Brown 
44495b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
44595b482a8SLen Brown 					ACPI_EXCEPTION((AE_INFO, status,
44695b482a8SLen Brown 							"from region handler - deactivate, [%s]",
44795b482a8SLen Brown 							acpi_ut_get_region_name
44895b482a8SLen Brown 							(region_obj->region.
44995b482a8SLen Brown 							 space_id)));
45095b482a8SLen Brown 				}
45195b482a8SLen Brown 
45295b482a8SLen Brown 				region_obj->region.flags &=
45395b482a8SLen Brown 				    ~(AOPOBJ_SETUP_COMPLETE);
45495b482a8SLen Brown 			}
45595b482a8SLen Brown 
45695b482a8SLen Brown 			/*
45795b482a8SLen Brown 			 * Remove handler reference in the region
45895b482a8SLen Brown 			 *
45995b482a8SLen Brown 			 * NOTE: this doesn't mean that the region goes away, the region
46095b482a8SLen Brown 			 * is just inaccessible as indicated to the _REG method
46195b482a8SLen Brown 			 *
46295b482a8SLen Brown 			 * If the region is on the handler's list, this must be the
46395b482a8SLen Brown 			 * region's handler
46495b482a8SLen Brown 			 */
46595b482a8SLen Brown 			region_obj->region.handler = NULL;
46695b482a8SLen Brown 			acpi_ut_remove_reference(handler_obj);
46795b482a8SLen Brown 
46895b482a8SLen Brown 			return_VOID;
46995b482a8SLen Brown 		}
47095b482a8SLen Brown 
47195b482a8SLen Brown 		/* Walk the linked list of handlers */
47295b482a8SLen Brown 
47395b482a8SLen Brown 		last_obj_ptr = &obj_desc->region.next;
47495b482a8SLen Brown 		obj_desc = obj_desc->region.next;
475f953529fSBob Moore 
476f953529fSBob Moore 		/* Prevent infinite loop if list is corrupted */
477f953529fSBob Moore 
478f953529fSBob Moore 		if (obj_desc == start_desc) {
479f953529fSBob Moore 			ACPI_ERROR((AE_INFO,
480f953529fSBob Moore 				    "Circular handler list in region object %p",
481f953529fSBob Moore 				    region_obj));
482f953529fSBob Moore 			return_VOID;
483f953529fSBob Moore 		}
48495b482a8SLen Brown 	}
48595b482a8SLen Brown 
48695b482a8SLen Brown 	/* If we get here, the region was not in the handler's region list */
48795b482a8SLen Brown 
48895b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
48995b482a8SLen Brown 			  "Cannot remove region %p from address handler %p\n",
49095b482a8SLen Brown 			  region_obj, handler_obj));
49195b482a8SLen Brown 
49295b482a8SLen Brown 	return_VOID;
49395b482a8SLen Brown }
49495b482a8SLen Brown 
49595b482a8SLen Brown /*******************************************************************************
49695b482a8SLen Brown  *
49795b482a8SLen Brown  * FUNCTION:    acpi_ev_attach_region
49895b482a8SLen Brown  *
49995b482a8SLen Brown  * PARAMETERS:  handler_obj         - Handler Object
50095b482a8SLen Brown  *              region_obj          - Region Object
50195b482a8SLen Brown  *              acpi_ns_is_locked   - Namespace Region Already Locked?
50295b482a8SLen Brown  *
50395b482a8SLen Brown  * RETURN:      None
50495b482a8SLen Brown  *
50595b482a8SLen Brown  * DESCRIPTION: Create the association between the handler and the region
50695b482a8SLen Brown  *              this is a two way association.
50795b482a8SLen Brown  *
50895b482a8SLen Brown  ******************************************************************************/
50995b482a8SLen Brown 
51095b482a8SLen Brown acpi_status
acpi_ev_attach_region(union acpi_operand_object * handler_obj,union acpi_operand_object * region_obj,u8 acpi_ns_is_locked)51195b482a8SLen Brown acpi_ev_attach_region(union acpi_operand_object *handler_obj,
51295b482a8SLen Brown 		      union acpi_operand_object *region_obj,
51395b482a8SLen Brown 		      u8 acpi_ns_is_locked)
51495b482a8SLen Brown {
51595b482a8SLen Brown 
51695b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ev_attach_region);
51795b482a8SLen Brown 
5181d65d9a7SLv Zheng 	/* Install the region's handler */
5191d65d9a7SLv Zheng 
5201d65d9a7SLv Zheng 	if (region_obj->region.handler) {
5211d65d9a7SLv Zheng 		return_ACPI_STATUS(AE_ALREADY_EXISTS);
5221d65d9a7SLv Zheng 	}
5231d65d9a7SLv Zheng 
52495b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
52595b482a8SLen Brown 			  "Adding Region [%4.4s] %p to address handler %p [%s]\n",
52695b482a8SLen Brown 			  acpi_ut_get_node_name(region_obj->region.node),
52795b482a8SLen Brown 			  region_obj, handler_obj,
52895b482a8SLen Brown 			  acpi_ut_get_region_name(region_obj->region.
52995b482a8SLen Brown 						  space_id)));
53095b482a8SLen Brown 
53195b482a8SLen Brown 	/* Link this region to the front of the handler's list */
53295b482a8SLen Brown 
53395b482a8SLen Brown 	region_obj->region.next = handler_obj->address_space.region_list;
53495b482a8SLen Brown 	handler_obj->address_space.region_list = region_obj;
53595b482a8SLen Brown 	region_obj->region.handler = handler_obj;
53695b482a8SLen Brown 	acpi_ut_add_reference(handler_obj);
53795b482a8SLen Brown 
53895b482a8SLen Brown 	return_ACPI_STATUS(AE_OK);
53995b482a8SLen Brown }
54095b482a8SLen Brown 
54195b482a8SLen Brown /*******************************************************************************
54295b482a8SLen Brown  *
54342f8fb75SBob Moore  * FUNCTION:    acpi_ev_execute_reg_method
54495b482a8SLen Brown  *
54542f8fb75SBob Moore  * PARAMETERS:  region_obj          - Region object
54642f8fb75SBob Moore  *              function            - Passed to _REG: On (1) or Off (0)
54795b482a8SLen Brown  *
54895b482a8SLen Brown  * RETURN:      Status
54995b482a8SLen Brown  *
55042f8fb75SBob Moore  * DESCRIPTION: Execute _REG method for a region
55195b482a8SLen Brown  *
55295b482a8SLen Brown  ******************************************************************************/
55395b482a8SLen Brown 
55495b482a8SLen Brown acpi_status
acpi_ev_execute_reg_method(union acpi_operand_object * region_obj,u32 function)55542f8fb75SBob Moore acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
55695b482a8SLen Brown {
55742f8fb75SBob Moore 	struct acpi_evaluate_info *info;
55842f8fb75SBob Moore 	union acpi_operand_object *args[3];
55942f8fb75SBob Moore 	union acpi_operand_object *region_obj2;
560d1461a1bSLv Zheng 	const acpi_name *reg_name_ptr =
561d1461a1bSLv Zheng 	    ACPI_CAST_PTR(acpi_name, METHOD_NAME__REG);
562d1461a1bSLv Zheng 	struct acpi_namespace_node *method_node;
563d1461a1bSLv Zheng 	struct acpi_namespace_node *node;
56495b482a8SLen Brown 	acpi_status status;
56595b482a8SLen Brown 
56642f8fb75SBob Moore 	ACPI_FUNCTION_TRACE(ev_execute_reg_method);
56742f8fb75SBob Moore 
568d1461a1bSLv Zheng 	if (!acpi_gbl_namespace_initialized ||
569d1461a1bSLv Zheng 	    region_obj->region.handler == NULL) {
570d1461a1bSLv Zheng 		return_ACPI_STATUS(AE_OK);
571d1461a1bSLv Zheng 	}
572d1461a1bSLv Zheng 
57342f8fb75SBob Moore 	region_obj2 = acpi_ns_get_secondary_object(region_obj);
57442f8fb75SBob Moore 	if (!region_obj2) {
57542f8fb75SBob Moore 		return_ACPI_STATUS(AE_NOT_EXIST);
57642f8fb75SBob Moore 	}
57742f8fb75SBob Moore 
578d1461a1bSLv Zheng 	/*
579d1461a1bSLv Zheng 	 * Find any "_REG" method associated with this region definition.
580d1461a1bSLv Zheng 	 * The method should always be updated as this function may be
581d1461a1bSLv Zheng 	 * invoked after a namespace change.
582d1461a1bSLv Zheng 	 */
583d1461a1bSLv Zheng 	node = region_obj->region.node->parent;
584d1461a1bSLv Zheng 	status =
585d1461a1bSLv Zheng 	    acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
586d1461a1bSLv Zheng 				     &method_node);
587d1461a1bSLv Zheng 	if (ACPI_SUCCESS(status)) {
588d1461a1bSLv Zheng 		/*
589d1461a1bSLv Zheng 		 * The _REG method is optional and there can be only one per
590d1461a1bSLv Zheng 		 * region definition. This will be executed when the handler is
591d1461a1bSLv Zheng 		 * attached or removed.
592d1461a1bSLv Zheng 		 */
593d1461a1bSLv Zheng 		region_obj2->extra.method_REG = method_node;
594d1461a1bSLv Zheng 	}
595d1461a1bSLv Zheng 	if (region_obj2->extra.method_REG == NULL) {
596efaed9beSLv Zheng 		return_ACPI_STATUS(AE_OK);
597efaed9beSLv Zheng 	}
598efaed9beSLv Zheng 
599efaed9beSLv Zheng 	/* _REG(DISCONNECT) should be paired with _REG(CONNECT) */
600efaed9beSLv Zheng 
601efaed9beSLv Zheng 	if ((function == ACPI_REG_CONNECT &&
602efaed9beSLv Zheng 	     region_obj->common.flags & AOPOBJ_REG_CONNECTED) ||
603efaed9beSLv Zheng 	    (function == ACPI_REG_DISCONNECT &&
604efaed9beSLv Zheng 	     !(region_obj->common.flags & AOPOBJ_REG_CONNECTED))) {
60542f8fb75SBob Moore 		return_ACPI_STATUS(AE_OK);
60642f8fb75SBob Moore 	}
60742f8fb75SBob Moore 
60842f8fb75SBob Moore 	/* Allocate and initialize the evaluation information block */
60942f8fb75SBob Moore 
61042f8fb75SBob Moore 	info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
61142f8fb75SBob Moore 	if (!info) {
61242f8fb75SBob Moore 		return_ACPI_STATUS(AE_NO_MEMORY);
61342f8fb75SBob Moore 	}
61442f8fb75SBob Moore 
61542f8fb75SBob Moore 	info->prefix_node = region_obj2->extra.method_REG;
61629a241ccSBob Moore 	info->relative_pathname = NULL;
61742f8fb75SBob Moore 	info->parameters = args;
61842f8fb75SBob Moore 	info->flags = ACPI_IGNORE_RETURN_VALUE;
61995b482a8SLen Brown 
62095b482a8SLen Brown 	/*
62142f8fb75SBob Moore 	 * The _REG method has two arguments:
62242f8fb75SBob Moore 	 *
62342f8fb75SBob Moore 	 * arg0 - Integer:
62442f8fb75SBob Moore 	 *  Operation region space ID Same value as region_obj->Region.space_id
62542f8fb75SBob Moore 	 *
62642f8fb75SBob Moore 	 * arg1 - Integer:
62742f8fb75SBob Moore 	 *  connection status 1 for connecting the handler, 0 for disconnecting
62842f8fb75SBob Moore 	 *  the handler (Passed as a parameter)
62995b482a8SLen Brown 	 */
63042f8fb75SBob Moore 	args[0] =
63142f8fb75SBob Moore 	    acpi_ut_create_integer_object((u64)region_obj->region.space_id);
63242f8fb75SBob Moore 	if (!args[0]) {
63395b482a8SLen Brown 		status = AE_NO_MEMORY;
63442f8fb75SBob Moore 		goto cleanup1;
63595b482a8SLen Brown 	}
63695b482a8SLen Brown 
63742f8fb75SBob Moore 	args[1] = acpi_ut_create_integer_object((u64)function);
63842f8fb75SBob Moore 	if (!args[1]) {
63995b482a8SLen Brown 		status = AE_NO_MEMORY;
64042f8fb75SBob Moore 		goto cleanup2;
64195b482a8SLen Brown 	}
64295b482a8SLen Brown 
64342f8fb75SBob Moore 	args[2] = NULL;		/* Terminate list */
64495b482a8SLen Brown 
64542f8fb75SBob Moore 	/* Execute the method, no return value */
64695b482a8SLen Brown 
64742f8fb75SBob Moore 	ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
64842f8fb75SBob Moore 			(ACPI_TYPE_METHOD, info->prefix_node, NULL));
64995b482a8SLen Brown 
65042f8fb75SBob Moore 	status = acpi_ns_evaluate(info);
65142f8fb75SBob Moore 	acpi_ut_remove_reference(args[1]);
65295b482a8SLen Brown 
653efaed9beSLv Zheng 	if (ACPI_FAILURE(status)) {
654efaed9beSLv Zheng 		goto cleanup2;
655efaed9beSLv Zheng 	}
656efaed9beSLv Zheng 
657efaed9beSLv Zheng 	if (function == ACPI_REG_CONNECT) {
658efaed9beSLv Zheng 		region_obj->common.flags |= AOPOBJ_REG_CONNECTED;
659efaed9beSLv Zheng 	} else {
660efaed9beSLv Zheng 		region_obj->common.flags &= ~AOPOBJ_REG_CONNECTED;
661efaed9beSLv Zheng 	}
662efaed9beSLv Zheng 
66342f8fb75SBob Moore cleanup2:
66442f8fb75SBob Moore 	acpi_ut_remove_reference(args[0]);
66595b482a8SLen Brown 
66642f8fb75SBob Moore cleanup1:
66742f8fb75SBob Moore 	ACPI_FREE(info);
66895b482a8SLen Brown 	return_ACPI_STATUS(status);
66995b482a8SLen Brown }
67095b482a8SLen Brown 
67195b482a8SLen Brown /*******************************************************************************
67295b482a8SLen Brown  *
67395b482a8SLen Brown  * FUNCTION:    acpi_ev_execute_reg_methods
67495b482a8SLen Brown  *
675ba494beeSBob Moore  * PARAMETERS:  node            - Namespace node for the device
676*03fd525dSRafael J. Wysocki  *              max_depth       - Depth to which search for _REG
67795b482a8SLen Brown  *              space_id        - The address space ID
678d815346fSLv Zheng  *              function        - Passed to _REG: On (1) or Off (0)
67995b482a8SLen Brown  *
680d815346fSLv Zheng  * RETURN:      None
68195b482a8SLen Brown  *
68295b482a8SLen Brown  * DESCRIPTION: Run all _REG methods for the input Space ID;
68395b482a8SLen Brown  *              Note: assumes namespace is locked, or system init time.
68495b482a8SLen Brown  *
68595b482a8SLen Brown  ******************************************************************************/
68695b482a8SLen Brown 
687d815346fSLv Zheng void
acpi_ev_execute_reg_methods(struct acpi_namespace_node * node,u32 max_depth,acpi_adr_space_type space_id,u32 function)688*03fd525dSRafael J. Wysocki acpi_ev_execute_reg_methods(struct acpi_namespace_node *node, u32 max_depth,
689d815346fSLv Zheng 			    acpi_adr_space_type space_id, u32 function)
69095b482a8SLen Brown {
69125823e78SBob Moore 	struct acpi_reg_walk_info info;
69295b482a8SLen Brown 
69395b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
69495b482a8SLen Brown 
6958b1cafdcSBob Moore 	/*
6968b1cafdcSBob Moore 	 * These address spaces do not need a call to _REG, since the ACPI
6978b1cafdcSBob Moore 	 * specification defines them as: "must always be accessible". Since
6988b1cafdcSBob Moore 	 * they never change state (never become unavailable), no need to ever
6998b1cafdcSBob Moore 	 * call _REG on them. Also, a data_table is not a "real" address space,
7008b1cafdcSBob Moore 	 * so do not call _REG. September 2018.
7018b1cafdcSBob Moore 	 */
7028b1cafdcSBob Moore 	if ((space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) ||
7038b1cafdcSBob Moore 	    (space_id == ACPI_ADR_SPACE_SYSTEM_IO) ||
7048b1cafdcSBob Moore 	    (space_id == ACPI_ADR_SPACE_DATA_TABLE)) {
7058b1cafdcSBob Moore 		return_VOID;
7068b1cafdcSBob Moore 	}
7078b1cafdcSBob Moore 
70825823e78SBob Moore 	info.space_id = space_id;
709d815346fSLv Zheng 	info.function = function;
71025823e78SBob Moore 	info.reg_run_count = 0;
71125823e78SBob Moore 
71225823e78SBob Moore 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
71325823e78SBob Moore 			      "    Running _REG methods for SpaceId %s\n",
71425823e78SBob Moore 			      acpi_ut_get_region_name(info.space_id)));
71525823e78SBob Moore 
71695b482a8SLen Brown 	/*
71795b482a8SLen Brown 	 * Run all _REG methods for all Operation Regions for this space ID. This
71895b482a8SLen Brown 	 * is a separate walk in order to handle any interdependencies between
71995b482a8SLen Brown 	 * regions and _REG methods. (i.e. handlers must be installed for all
72095b482a8SLen Brown 	 * regions of this Space ID before we can run any _REG methods)
72195b482a8SLen Brown 	 */
722*03fd525dSRafael J. Wysocki 	(void)acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, max_depth,
723d815346fSLv Zheng 				     ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run, NULL,
724d815346fSLv Zheng 				     &info, NULL);
72595b482a8SLen Brown 
7260306f035SHans de Goede 	/*
7270306f035SHans de Goede 	 * Special case for EC and GPIO: handle "orphan" _REG methods with
7280306f035SHans de Goede 	 * no region.
7290306f035SHans de Goede 	 */
7300306f035SHans de Goede 	if (space_id == ACPI_ADR_SPACE_EC || space_id == ACPI_ADR_SPACE_GPIO) {
7310306f035SHans de Goede 		acpi_ev_execute_orphan_reg_method(node, space_id);
732e2066ca1SBob Moore 	}
733e2066ca1SBob Moore 
73425823e78SBob Moore 	ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
73525823e78SBob Moore 			      "    Executed %u _REG methods for SpaceId %s\n",
73625823e78SBob Moore 			      info.reg_run_count,
73725823e78SBob Moore 			      acpi_ut_get_region_name(info.space_id)));
73825823e78SBob Moore 
739d815346fSLv Zheng 	return_VOID;
74095b482a8SLen Brown }
74195b482a8SLen Brown 
74295b482a8SLen Brown /*******************************************************************************
74395b482a8SLen Brown  *
74495b482a8SLen Brown  * FUNCTION:    acpi_ev_reg_run
74595b482a8SLen Brown  *
74695b482a8SLen Brown  * PARAMETERS:  walk_namespace callback
74795b482a8SLen Brown  *
748ba494beeSBob Moore  * DESCRIPTION: Run _REG method for region objects of the requested spaceID
74995b482a8SLen Brown  *
75095b482a8SLen Brown  ******************************************************************************/
75195b482a8SLen Brown 
75295b482a8SLen Brown static acpi_status
acpi_ev_reg_run(acpi_handle obj_handle,u32 level,void * context,void ** return_value)75395b482a8SLen Brown acpi_ev_reg_run(acpi_handle obj_handle,
75495b482a8SLen Brown 		u32 level, void *context, void **return_value)
75595b482a8SLen Brown {
75695b482a8SLen Brown 	union acpi_operand_object *obj_desc;
75795b482a8SLen Brown 	struct acpi_namespace_node *node;
75895b482a8SLen Brown 	acpi_status status;
75925823e78SBob Moore 	struct acpi_reg_walk_info *info;
76095b482a8SLen Brown 
76125823e78SBob Moore 	info = ACPI_CAST_PTR(struct acpi_reg_walk_info, context);
76295b482a8SLen Brown 
76395b482a8SLen Brown 	/* Convert and validate the device handle */
76495b482a8SLen Brown 
765f24b664dSBob Moore 	node = acpi_ns_validate_handle(obj_handle);
76695b482a8SLen Brown 	if (!node) {
76795b482a8SLen Brown 		return (AE_BAD_PARAMETER);
76895b482a8SLen Brown 	}
76995b482a8SLen Brown 
77095b482a8SLen Brown 	/*
7718b1cafdcSBob Moore 	 * We only care about regions and objects that are allowed to have
7728b1cafdcSBob Moore 	 * address space handlers
77395b482a8SLen Brown 	 */
77495b482a8SLen Brown 	if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
77595b482a8SLen Brown 		return (AE_OK);
77695b482a8SLen Brown 	}
77795b482a8SLen Brown 
77895b482a8SLen Brown 	/* Check for an existing internal object */
77995b482a8SLen Brown 
78095b482a8SLen Brown 	obj_desc = acpi_ns_get_attached_object(node);
78195b482a8SLen Brown 	if (!obj_desc) {
78295b482a8SLen Brown 
78395b482a8SLen Brown 		/* No object, just exit */
78495b482a8SLen Brown 
78595b482a8SLen Brown 		return (AE_OK);
78695b482a8SLen Brown 	}
78795b482a8SLen Brown 
78895b482a8SLen Brown 	/* Object is a Region */
78995b482a8SLen Brown 
79025823e78SBob Moore 	if (obj_desc->region.space_id != info->space_id) {
79195b482a8SLen Brown 
79295b482a8SLen Brown 		/* This region is for a different address space, just ignore it */
79395b482a8SLen Brown 
79495b482a8SLen Brown 		return (AE_OK);
79595b482a8SLen Brown 	}
79695b482a8SLen Brown 
79725823e78SBob Moore 	info->reg_run_count++;
798d815346fSLv Zheng 	status = acpi_ev_execute_reg_method(obj_desc, info->function);
79995b482a8SLen Brown 	return (status);
80095b482a8SLen Brown }
801e2066ca1SBob Moore 
802e2066ca1SBob Moore /*******************************************************************************
803e2066ca1SBob Moore  *
8040306f035SHans de Goede  * FUNCTION:    acpi_ev_execute_orphan_reg_method
805e2066ca1SBob Moore  *
8060306f035SHans de Goede  * PARAMETERS:  device_node         - Namespace node for an ACPI device
8070306f035SHans de Goede  *              space_id            - The address space ID
808e2066ca1SBob Moore  *
809e2066ca1SBob Moore  * RETURN:      None
810e2066ca1SBob Moore  *
8110306f035SHans de Goede  * DESCRIPTION: Execute an "orphan" _REG method that appears under an ACPI
812e2066ca1SBob Moore  *              device. This is a _REG method that has no corresponding region
8130306f035SHans de Goede  *              within the device's scope. ACPI tables depending on these
8140306f035SHans de Goede  *              "orphan" _REG methods have been seen for both EC and GPIO
8150306f035SHans de Goede  *              Operation Regions. Presumably the Windows ACPI implementation
8160306f035SHans de Goede  *              always calls the _REG method independent of the presence of
8170306f035SHans de Goede  *              an actual Operation Region with the correct address space ID.
8188f4f5e78SZhang Rui  *
8198f4f5e78SZhang Rui  *  MUTEX:      Assumes the namespace is locked
820e2066ca1SBob Moore  *
821e2066ca1SBob Moore  ******************************************************************************/
822e2066ca1SBob Moore 
8237adc8a3dSRafael J. Wysocki static void
acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node * device_node,acpi_adr_space_type space_id)8240306f035SHans de Goede acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node,
8250306f035SHans de Goede 				  acpi_adr_space_type space_id)
826e2066ca1SBob Moore {
8278f4f5e78SZhang Rui 	acpi_handle reg_method;
8288f4f5e78SZhang Rui 	struct acpi_namespace_node *next_node;
829e2066ca1SBob Moore 	acpi_status status;
830e2066ca1SBob Moore 	struct acpi_object_list args;
831e2066ca1SBob Moore 	union acpi_object objects[2];
832e2066ca1SBob Moore 
8330306f035SHans de Goede 	ACPI_FUNCTION_TRACE(ev_execute_orphan_reg_method);
834e2066ca1SBob Moore 
8350306f035SHans de Goede 	if (!device_node) {
836e2066ca1SBob Moore 		return_VOID;
837e2066ca1SBob Moore 	}
838e2066ca1SBob Moore 
839e2066ca1SBob Moore 	/* Namespace is currently locked, must release */
840e2066ca1SBob Moore 
841e2066ca1SBob Moore 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
842e2066ca1SBob Moore 
843e2066ca1SBob Moore 	/* Get a handle to a _REG method immediately under the EC device */
844e2066ca1SBob Moore 
8450306f035SHans de Goede 	status = acpi_get_handle(device_node, METHOD_NAME__REG, &reg_method);
846e2066ca1SBob Moore 	if (ACPI_FAILURE(status)) {
8478f4f5e78SZhang Rui 		goto exit;	/* There is no _REG method present */
848e2066ca1SBob Moore 	}
849e2066ca1SBob Moore 
850e2066ca1SBob Moore 	/*
851e2066ca1SBob Moore 	 * Execute the _REG method only if there is no Operation Region in
852e2066ca1SBob Moore 	 * this scope with the Embedded Controller space ID. Otherwise, it
853e2066ca1SBob Moore 	 * will already have been executed. Note, this allows for Regions
854e2066ca1SBob Moore 	 * with other space IDs to be present; but the code below will then
8558f4f5e78SZhang Rui 	 * execute the _REG method with the embedded_control space_ID argument.
856e2066ca1SBob Moore 	 */
8570306f035SHans de Goede 	next_node = acpi_ns_get_next_node(device_node, NULL);
858e2066ca1SBob Moore 	while (next_node) {
859e2066ca1SBob Moore 		if ((next_node->type == ACPI_TYPE_REGION) &&
860e2066ca1SBob Moore 		    (next_node->object) &&
8610306f035SHans de Goede 		    (next_node->object->region.space_id == space_id)) {
8628f4f5e78SZhang Rui 			goto exit;	/* Do not execute the _REG */
863e2066ca1SBob Moore 		}
8648f4f5e78SZhang Rui 
8650306f035SHans de Goede 		next_node = acpi_ns_get_next_node(device_node, next_node);
866e2066ca1SBob Moore 	}
867e2066ca1SBob Moore 
8680306f035SHans de Goede 	/* Evaluate the _REG(space_id,Connect) method */
869e2066ca1SBob Moore 
870e2066ca1SBob Moore 	args.count = 2;
871e2066ca1SBob Moore 	args.pointer = objects;
872e2066ca1SBob Moore 	objects[0].type = ACPI_TYPE_INTEGER;
8730306f035SHans de Goede 	objects[0].integer.value = space_id;
874e2066ca1SBob Moore 	objects[1].type = ACPI_TYPE_INTEGER;
875e2066ca1SBob Moore 	objects[1].integer.value = ACPI_REG_CONNECT;
876e2066ca1SBob Moore 
877edc5935eSBob Moore 	(void)acpi_evaluate_object(reg_method, NULL, &args, NULL);
878e2066ca1SBob Moore 
879e2066ca1SBob Moore exit:
880e2066ca1SBob Moore 	/* We ignore all errors from above, don't care */
881e2066ca1SBob Moore 
882edc5935eSBob Moore 	(void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
883e2066ca1SBob Moore 	return_VOID;
884e2066ca1SBob Moore }
885