xref: /openbmc/linux/drivers/acpi/acpica/evhandler.c (revision 612c2932)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
242f8fb75SBob Moore /******************************************************************************
342f8fb75SBob Moore  *
442f8fb75SBob Moore  * Module Name: evhandler - Support for Address Space handlers
542f8fb75SBob Moore  *
6*612c2932SBob Moore  * Copyright (C) 2000 - 2023, Intel Corp.
742f8fb75SBob Moore  *
895857638SErik Schmauss  *****************************************************************************/
942f8fb75SBob Moore 
1042f8fb75SBob Moore #include <acpi/acpi.h>
1142f8fb75SBob Moore #include "accommon.h"
1242f8fb75SBob Moore #include "acevents.h"
1342f8fb75SBob Moore #include "acnamesp.h"
1442f8fb75SBob Moore #include "acinterp.h"
1542f8fb75SBob Moore 
1642f8fb75SBob Moore #define _COMPONENT          ACPI_EVENTS
1742f8fb75SBob Moore ACPI_MODULE_NAME("evhandler")
1842f8fb75SBob Moore 
1942f8fb75SBob Moore /* Local prototypes */
2042f8fb75SBob Moore static acpi_status
2142f8fb75SBob Moore acpi_ev_install_handler(acpi_handle obj_handle,
2242f8fb75SBob Moore 			u32 level, void *context, void **return_value);
2342f8fb75SBob Moore 
2442f8fb75SBob Moore /* These are the address spaces that will get default handlers */
2542f8fb75SBob Moore 
2642f8fb75SBob Moore u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
2742f8fb75SBob Moore 	ACPI_ADR_SPACE_SYSTEM_MEMORY,
2842f8fb75SBob Moore 	ACPI_ADR_SPACE_SYSTEM_IO,
2942f8fb75SBob Moore 	ACPI_ADR_SPACE_PCI_CONFIG,
3042f8fb75SBob Moore 	ACPI_ADR_SPACE_DATA_TABLE
3142f8fb75SBob Moore };
3242f8fb75SBob Moore 
3342f8fb75SBob Moore /*******************************************************************************
3442f8fb75SBob Moore  *
3542f8fb75SBob Moore  * FUNCTION:    acpi_ev_install_region_handlers
3642f8fb75SBob Moore  *
3742f8fb75SBob Moore  * PARAMETERS:  None
3842f8fb75SBob Moore  *
3942f8fb75SBob Moore  * RETURN:      Status
4042f8fb75SBob Moore  *
4142f8fb75SBob Moore  * DESCRIPTION: Installs the core subsystem default address space handlers.
4242f8fb75SBob Moore  *
4342f8fb75SBob Moore  ******************************************************************************/
4442f8fb75SBob Moore 
acpi_ev_install_region_handlers(void)4542f8fb75SBob Moore acpi_status acpi_ev_install_region_handlers(void)
4642f8fb75SBob Moore {
4742f8fb75SBob Moore 	acpi_status status;
4842f8fb75SBob Moore 	u32 i;
4942f8fb75SBob Moore 
5042f8fb75SBob Moore 	ACPI_FUNCTION_TRACE(ev_install_region_handlers);
5142f8fb75SBob Moore 
5242f8fb75SBob Moore 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
5342f8fb75SBob Moore 	if (ACPI_FAILURE(status)) {
5442f8fb75SBob Moore 		return_ACPI_STATUS(status);
5542f8fb75SBob Moore 	}
5642f8fb75SBob Moore 
5742f8fb75SBob Moore 	/*
5842f8fb75SBob Moore 	 * All address spaces (PCI Config, EC, SMBus) are scope dependent and
5942f8fb75SBob Moore 	 * registration must occur for a specific device.
6042f8fb75SBob Moore 	 *
6142f8fb75SBob Moore 	 * In the case of the system memory and IO address spaces there is
6242f8fb75SBob Moore 	 * currently no device associated with the address space. For these we
6342f8fb75SBob Moore 	 * use the root.
6442f8fb75SBob Moore 	 *
6542f8fb75SBob Moore 	 * We install the default PCI config space handler at the root so that
6642f8fb75SBob Moore 	 * this space is immediately available even though the we have not
6742f8fb75SBob Moore 	 * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
6842f8fb75SBob Moore 	 * specification which states that the PCI config space must be always
6942f8fb75SBob Moore 	 * available -- even though we are nowhere near ready to find the PCI root
7042f8fb75SBob Moore 	 * buses at this point.
7142f8fb75SBob Moore 	 *
7242f8fb75SBob Moore 	 * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
7342f8fb75SBob Moore 	 * has already been installed (via acpi_install_address_space_handler).
7442f8fb75SBob Moore 	 * Similar for AE_SAME_HANDLER.
7542f8fb75SBob Moore 	 */
7642f8fb75SBob Moore 	for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
7742f8fb75SBob Moore 		status = acpi_ev_install_space_handler(acpi_gbl_root_node,
7842f8fb75SBob Moore 						       acpi_gbl_default_address_spaces
7942f8fb75SBob Moore 						       [i],
8042f8fb75SBob Moore 						       ACPI_DEFAULT_HANDLER,
8142f8fb75SBob Moore 						       NULL, NULL);
8242f8fb75SBob Moore 		switch (status) {
8342f8fb75SBob Moore 		case AE_OK:
8442f8fb75SBob Moore 		case AE_SAME_HANDLER:
8542f8fb75SBob Moore 		case AE_ALREADY_EXISTS:
8642f8fb75SBob Moore 
8742f8fb75SBob Moore 			/* These exceptions are all OK */
8842f8fb75SBob Moore 
8942f8fb75SBob Moore 			status = AE_OK;
9042f8fb75SBob Moore 			break;
9142f8fb75SBob Moore 
9242f8fb75SBob Moore 		default:
9342f8fb75SBob Moore 
9442f8fb75SBob Moore 			goto unlock_and_exit;
9542f8fb75SBob Moore 		}
9642f8fb75SBob Moore 	}
9742f8fb75SBob Moore 
9842f8fb75SBob Moore unlock_and_exit:
9942f8fb75SBob Moore 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
10042f8fb75SBob Moore 	return_ACPI_STATUS(status);
10142f8fb75SBob Moore }
10242f8fb75SBob Moore 
10342f8fb75SBob Moore /*******************************************************************************
10442f8fb75SBob Moore  *
10542f8fb75SBob Moore  * FUNCTION:    acpi_ev_has_default_handler
10642f8fb75SBob Moore  *
10742f8fb75SBob Moore  * PARAMETERS:  node                - Namespace node for the device
10842f8fb75SBob Moore  *              space_id            - The address space ID
10942f8fb75SBob Moore  *
11042f8fb75SBob Moore  * RETURN:      TRUE if default handler is installed, FALSE otherwise
11142f8fb75SBob Moore  *
11242f8fb75SBob Moore  * DESCRIPTION: Check if the default handler is installed for the requested
11342f8fb75SBob Moore  *              space ID.
11442f8fb75SBob Moore  *
11542f8fb75SBob Moore  ******************************************************************************/
11642f8fb75SBob Moore 
11742f8fb75SBob Moore u8
acpi_ev_has_default_handler(struct acpi_namespace_node * node,acpi_adr_space_type space_id)11842f8fb75SBob Moore acpi_ev_has_default_handler(struct acpi_namespace_node *node,
11942f8fb75SBob Moore 			    acpi_adr_space_type space_id)
12042f8fb75SBob Moore {
12142f8fb75SBob Moore 	union acpi_operand_object *obj_desc;
12242f8fb75SBob Moore 	union acpi_operand_object *handler_obj;
12342f8fb75SBob Moore 
12442f8fb75SBob Moore 	/* Must have an existing internal object */
12542f8fb75SBob Moore 
12642f8fb75SBob Moore 	obj_desc = acpi_ns_get_attached_object(node);
12742f8fb75SBob Moore 	if (obj_desc) {
128aa6abd2bSLv Zheng 		handler_obj = obj_desc->common_notify.handler;
12942f8fb75SBob Moore 
13042f8fb75SBob Moore 		/* Walk the linked list of handlers for this object */
13142f8fb75SBob Moore 
13242f8fb75SBob Moore 		while (handler_obj) {
13342f8fb75SBob Moore 			if (handler_obj->address_space.space_id == space_id) {
13442f8fb75SBob Moore 				if (handler_obj->address_space.handler_flags &
13542f8fb75SBob Moore 				    ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
13642f8fb75SBob Moore 					return (TRUE);
13742f8fb75SBob Moore 				}
13842f8fb75SBob Moore 			}
13942f8fb75SBob Moore 
14042f8fb75SBob Moore 			handler_obj = handler_obj->address_space.next;
14142f8fb75SBob Moore 		}
14242f8fb75SBob Moore 	}
14342f8fb75SBob Moore 
14442f8fb75SBob Moore 	return (FALSE);
14542f8fb75SBob Moore }
14642f8fb75SBob Moore 
14742f8fb75SBob Moore /*******************************************************************************
14842f8fb75SBob Moore  *
14942f8fb75SBob Moore  * FUNCTION:    acpi_ev_install_handler
15042f8fb75SBob Moore  *
15142f8fb75SBob Moore  * PARAMETERS:  walk_namespace callback
15242f8fb75SBob Moore  *
15342f8fb75SBob Moore  * DESCRIPTION: This routine installs an address handler into objects that are
15442f8fb75SBob Moore  *              of type Region or Device.
15542f8fb75SBob Moore  *
15642f8fb75SBob Moore  *              If the Object is a Device, and the device has a handler of
15742f8fb75SBob Moore  *              the same type then the search is terminated in that branch.
15842f8fb75SBob Moore  *
15942f8fb75SBob Moore  *              This is because the existing handler is closer in proximity
16042f8fb75SBob Moore  *              to any more regions than the one we are trying to install.
16142f8fb75SBob Moore  *
16242f8fb75SBob Moore  ******************************************************************************/
16342f8fb75SBob Moore 
16442f8fb75SBob Moore static acpi_status
acpi_ev_install_handler(acpi_handle obj_handle,u32 level,void * context,void ** return_value)16542f8fb75SBob Moore acpi_ev_install_handler(acpi_handle obj_handle,
16642f8fb75SBob Moore 			u32 level, void *context, void **return_value)
16742f8fb75SBob Moore {
16842f8fb75SBob Moore 	union acpi_operand_object *handler_obj;
16942f8fb75SBob Moore 	union acpi_operand_object *next_handler_obj;
17042f8fb75SBob Moore 	union acpi_operand_object *obj_desc;
17142f8fb75SBob Moore 	struct acpi_namespace_node *node;
17242f8fb75SBob Moore 	acpi_status status;
17342f8fb75SBob Moore 
17442f8fb75SBob Moore 	ACPI_FUNCTION_NAME(ev_install_handler);
17542f8fb75SBob Moore 
17642f8fb75SBob Moore 	handler_obj = (union acpi_operand_object *)context;
17742f8fb75SBob Moore 
17842f8fb75SBob Moore 	/* Parameter validation */
17942f8fb75SBob Moore 
18042f8fb75SBob Moore 	if (!handler_obj) {
18142f8fb75SBob Moore 		return (AE_OK);
18242f8fb75SBob Moore 	}
18342f8fb75SBob Moore 
18442f8fb75SBob Moore 	/* Convert and validate the device handle */
18542f8fb75SBob Moore 
18642f8fb75SBob Moore 	node = acpi_ns_validate_handle(obj_handle);
18742f8fb75SBob Moore 	if (!node) {
18842f8fb75SBob Moore 		return (AE_BAD_PARAMETER);
18942f8fb75SBob Moore 	}
19042f8fb75SBob Moore 
19142f8fb75SBob Moore 	/*
19242f8fb75SBob Moore 	 * We only care about regions and objects that are allowed to have
19342f8fb75SBob Moore 	 * address space handlers
19442f8fb75SBob Moore 	 */
19542f8fb75SBob Moore 	if ((node->type != ACPI_TYPE_DEVICE) &&
19642f8fb75SBob Moore 	    (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
19742f8fb75SBob Moore 		return (AE_OK);
19842f8fb75SBob Moore 	}
19942f8fb75SBob Moore 
20042f8fb75SBob Moore 	/* Check for an existing internal object */
20142f8fb75SBob Moore 
20242f8fb75SBob Moore 	obj_desc = acpi_ns_get_attached_object(node);
20342f8fb75SBob Moore 	if (!obj_desc) {
20442f8fb75SBob Moore 
20542f8fb75SBob Moore 		/* No object, just exit */
20642f8fb75SBob Moore 
20742f8fb75SBob Moore 		return (AE_OK);
20842f8fb75SBob Moore 	}
20942f8fb75SBob Moore 
21042f8fb75SBob Moore 	/* Devices are handled different than regions */
21142f8fb75SBob Moore 
21242f8fb75SBob Moore 	if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
21342f8fb75SBob Moore 
21442f8fb75SBob Moore 		/* Check if this Device already has a handler for this address space */
21542f8fb75SBob Moore 
216f31a99ceSLv Zheng 		next_handler_obj =
217f31a99ceSLv Zheng 		    acpi_ev_find_region_handler(handler_obj->address_space.
218f31a99ceSLv Zheng 						space_id,
219aa6abd2bSLv Zheng 						obj_desc->common_notify.
220aa6abd2bSLv Zheng 						handler);
221f31a99ceSLv Zheng 		if (next_handler_obj) {
22242f8fb75SBob Moore 
22342f8fb75SBob Moore 			/* Found a handler, is it for the same address space? */
22442f8fb75SBob Moore 
22542f8fb75SBob Moore 			ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
226f31a99ceSLv Zheng 					  "Found handler for region [%s] in device %p(%p) handler %p\n",
227f31a99ceSLv Zheng 					  acpi_ut_get_region_name(handler_obj->
228f31a99ceSLv Zheng 								  address_space.
229f31a99ceSLv Zheng 								  space_id),
230f31a99ceSLv Zheng 					  obj_desc, next_handler_obj,
23142f8fb75SBob Moore 					  handler_obj));
23242f8fb75SBob Moore 
23342f8fb75SBob Moore 			/*
234f31a99ceSLv Zheng 			 * Since the object we found it on was a device, then it means
235f31a99ceSLv Zheng 			 * that someone has already installed a handler for the branch
236f31a99ceSLv Zheng 			 * of the namespace from this device on. Just bail out telling
237f31a99ceSLv Zheng 			 * the walk routine to not traverse this branch. This preserves
238f31a99ceSLv Zheng 			 * the scoping rule for handlers.
23942f8fb75SBob Moore 			 */
24042f8fb75SBob Moore 			return (AE_CTRL_DEPTH);
24142f8fb75SBob Moore 		}
24242f8fb75SBob Moore 
24342f8fb75SBob Moore 		/*
24442f8fb75SBob Moore 		 * As long as the device didn't have a handler for this space we
24542f8fb75SBob Moore 		 * don't care about it. We just ignore it and proceed.
24642f8fb75SBob Moore 		 */
24742f8fb75SBob Moore 		return (AE_OK);
24842f8fb75SBob Moore 	}
24942f8fb75SBob Moore 
25042f8fb75SBob Moore 	/* Object is a Region */
25142f8fb75SBob Moore 
25242f8fb75SBob Moore 	if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
25342f8fb75SBob Moore 
25442f8fb75SBob Moore 		/* This region is for a different address space, just ignore it */
25542f8fb75SBob Moore 
25642f8fb75SBob Moore 		return (AE_OK);
25742f8fb75SBob Moore 	}
25842f8fb75SBob Moore 
25942f8fb75SBob Moore 	/*
26042f8fb75SBob Moore 	 * Now we have a region and it is for the handler's address space type.
26142f8fb75SBob Moore 	 *
26242f8fb75SBob Moore 	 * First disconnect region for any previous handler (if any)
26342f8fb75SBob Moore 	 */
26442f8fb75SBob Moore 	acpi_ev_detach_region(obj_desc, FALSE);
26542f8fb75SBob Moore 
26642f8fb75SBob Moore 	/* Connect the region to the new handler */
26742f8fb75SBob Moore 
26842f8fb75SBob Moore 	status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
26942f8fb75SBob Moore 	return (status);
27042f8fb75SBob Moore }
27142f8fb75SBob Moore 
27242f8fb75SBob Moore /*******************************************************************************
27342f8fb75SBob Moore  *
2747b738064SBob Moore  * FUNCTION:    acpi_ev_find_region_handler
2757b738064SBob Moore  *
2767b738064SBob Moore  * PARAMETERS:  space_id        - The address space ID
2777b738064SBob Moore  *              handler_obj     - Head of the handler object list
2787b738064SBob Moore  *
2797b738064SBob Moore  * RETURN:      Matching handler object. NULL if space ID not matched
2807b738064SBob Moore  *
2817b738064SBob Moore  * DESCRIPTION: Search a handler object list for a match on the address
2827b738064SBob Moore  *              space ID.
2837b738064SBob Moore  *
2847b738064SBob Moore  ******************************************************************************/
2857b738064SBob Moore 
acpi_ev_find_region_handler(acpi_adr_space_type space_id,union acpi_operand_object * handler_obj)286f31a99ceSLv Zheng union acpi_operand_object *acpi_ev_find_region_handler(acpi_adr_space_type
287f31a99ceSLv Zheng 						       space_id,
288f31a99ceSLv Zheng 						       union acpi_operand_object
289f31a99ceSLv Zheng 						       *handler_obj)
2907b738064SBob Moore {
2917b738064SBob Moore 
2927b738064SBob Moore 	/* Walk the handler list for this device */
2937b738064SBob Moore 
2947b738064SBob Moore 	while (handler_obj) {
2957b738064SBob Moore 
2967b738064SBob Moore 		/* Same space_id indicates a handler is installed */
2977b738064SBob Moore 
2987b738064SBob Moore 		if (handler_obj->address_space.space_id == space_id) {
2997b738064SBob Moore 			return (handler_obj);
3007b738064SBob Moore 		}
3017b738064SBob Moore 
3027b738064SBob Moore 		/* Next handler object */
3037b738064SBob Moore 
3047b738064SBob Moore 		handler_obj = handler_obj->address_space.next;
3057b738064SBob Moore 	}
3067b738064SBob Moore 
3077b738064SBob Moore 	return (NULL);
3087b738064SBob Moore }
3097b738064SBob Moore 
3107b738064SBob Moore /*******************************************************************************
3117b738064SBob Moore  *
31242f8fb75SBob Moore  * FUNCTION:    acpi_ev_install_space_handler
31342f8fb75SBob Moore  *
31442f8fb75SBob Moore  * PARAMETERS:  node            - Namespace node for the device
31542f8fb75SBob Moore  *              space_id        - The address space ID
31642f8fb75SBob Moore  *              handler         - Address of the handler
31742f8fb75SBob Moore  *              setup           - Address of the setup function
31842f8fb75SBob Moore  *              context         - Value passed to the handler on each access
31942f8fb75SBob Moore  *
32042f8fb75SBob Moore  * RETURN:      Status
32142f8fb75SBob Moore  *
32242f8fb75SBob Moore  * DESCRIPTION: Install a handler for all op_regions of a given space_id.
32342f8fb75SBob Moore  *              Assumes namespace is locked
32442f8fb75SBob Moore  *
32542f8fb75SBob Moore  ******************************************************************************/
32642f8fb75SBob Moore 
32742f8fb75SBob Moore acpi_status
acpi_ev_install_space_handler(struct acpi_namespace_node * node,acpi_adr_space_type space_id,acpi_adr_space_handler handler,acpi_adr_space_setup setup,void * context)32842f8fb75SBob Moore acpi_ev_install_space_handler(struct acpi_namespace_node *node,
32942f8fb75SBob Moore 			      acpi_adr_space_type space_id,
33042f8fb75SBob Moore 			      acpi_adr_space_handler handler,
33142f8fb75SBob Moore 			      acpi_adr_space_setup setup, void *context)
33242f8fb75SBob Moore {
33342f8fb75SBob Moore 	union acpi_operand_object *obj_desc;
33442f8fb75SBob Moore 	union acpi_operand_object *handler_obj;
3357b738064SBob Moore 	acpi_status status = AE_OK;
33642f8fb75SBob Moore 	acpi_object_type type;
33742f8fb75SBob Moore 	u8 flags = 0;
33842f8fb75SBob Moore 
33942f8fb75SBob Moore 	ACPI_FUNCTION_TRACE(ev_install_space_handler);
34042f8fb75SBob Moore 
34142f8fb75SBob Moore 	/*
3427b738064SBob Moore 	 * This registration is valid for only the types below and the root.
3437b738064SBob Moore 	 * The root node is where the default handlers get installed.
34442f8fb75SBob Moore 	 */
34542f8fb75SBob Moore 	if ((node->type != ACPI_TYPE_DEVICE) &&
34642f8fb75SBob Moore 	    (node->type != ACPI_TYPE_PROCESSOR) &&
34742f8fb75SBob Moore 	    (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
34842f8fb75SBob Moore 		status = AE_BAD_PARAMETER;
34942f8fb75SBob Moore 		goto unlock_and_exit;
35042f8fb75SBob Moore 	}
35142f8fb75SBob Moore 
35242f8fb75SBob Moore 	if (handler == ACPI_DEFAULT_HANDLER) {
35342f8fb75SBob Moore 		flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
35442f8fb75SBob Moore 
35542f8fb75SBob Moore 		switch (space_id) {
35642f8fb75SBob Moore 		case ACPI_ADR_SPACE_SYSTEM_MEMORY:
3571d1ea1b7SChao Guan 
35842f8fb75SBob Moore 			handler = acpi_ex_system_memory_space_handler;
35942f8fb75SBob Moore 			setup = acpi_ev_system_memory_region_setup;
36042f8fb75SBob Moore 			break;
36142f8fb75SBob Moore 
36242f8fb75SBob Moore 		case ACPI_ADR_SPACE_SYSTEM_IO:
3631d1ea1b7SChao Guan 
36442f8fb75SBob Moore 			handler = acpi_ex_system_io_space_handler;
36542f8fb75SBob Moore 			setup = acpi_ev_io_space_region_setup;
36642f8fb75SBob Moore 			break;
367bd23fac3SSinan Kaya #ifdef ACPI_PCI_CONFIGURED
36842f8fb75SBob Moore 		case ACPI_ADR_SPACE_PCI_CONFIG:
3691d1ea1b7SChao Guan 
37042f8fb75SBob Moore 			handler = acpi_ex_pci_config_space_handler;
37142f8fb75SBob Moore 			setup = acpi_ev_pci_config_region_setup;
37242f8fb75SBob Moore 			break;
373bd23fac3SSinan Kaya #endif
37442f8fb75SBob Moore 		case ACPI_ADR_SPACE_CMOS:
3751d1ea1b7SChao Guan 
37642f8fb75SBob Moore 			handler = acpi_ex_cmos_space_handler;
37742f8fb75SBob Moore 			setup = acpi_ev_cmos_region_setup;
37842f8fb75SBob Moore 			break;
379bd23fac3SSinan Kaya #ifdef ACPI_PCI_CONFIGURED
38042f8fb75SBob Moore 		case ACPI_ADR_SPACE_PCI_BAR_TARGET:
3811d1ea1b7SChao Guan 
38242f8fb75SBob Moore 			handler = acpi_ex_pci_bar_space_handler;
38342f8fb75SBob Moore 			setup = acpi_ev_pci_bar_region_setup;
38442f8fb75SBob Moore 			break;
385bd23fac3SSinan Kaya #endif
38642f8fb75SBob Moore 		case ACPI_ADR_SPACE_DATA_TABLE:
3871d1ea1b7SChao Guan 
38842f8fb75SBob Moore 			handler = acpi_ex_data_table_space_handler;
389ca25f92bSJessica Clarke 			setup = acpi_ev_data_table_region_setup;
39042f8fb75SBob Moore 			break;
39142f8fb75SBob Moore 
39242f8fb75SBob Moore 		default:
3931d1ea1b7SChao Guan 
39442f8fb75SBob Moore 			status = AE_BAD_PARAMETER;
39542f8fb75SBob Moore 			goto unlock_and_exit;
39642f8fb75SBob Moore 		}
39742f8fb75SBob Moore 	}
39842f8fb75SBob Moore 
39942f8fb75SBob Moore 	/* If the caller hasn't specified a setup routine, use the default */
40042f8fb75SBob Moore 
40142f8fb75SBob Moore 	if (!setup) {
40242f8fb75SBob Moore 		setup = acpi_ev_default_region_setup;
40342f8fb75SBob Moore 	}
40442f8fb75SBob Moore 
40542f8fb75SBob Moore 	/* Check for an existing internal object */
40642f8fb75SBob Moore 
40742f8fb75SBob Moore 	obj_desc = acpi_ns_get_attached_object(node);
40842f8fb75SBob Moore 	if (obj_desc) {
40942f8fb75SBob Moore 		/*
4107b738064SBob Moore 		 * The attached device object already exists. Now make sure
4117b738064SBob Moore 		 * the handler is not already installed.
41242f8fb75SBob Moore 		 */
4137b738064SBob Moore 		handler_obj = acpi_ev_find_region_handler(space_id,
414aa6abd2bSLv Zheng 							  obj_desc->
415aa6abd2bSLv Zheng 							  common_notify.
4167b738064SBob Moore 							  handler);
41742f8fb75SBob Moore 
4187b738064SBob Moore 		if (handler_obj) {
4197b738064SBob Moore 			if (handler_obj->address_space.handler == handler) {
42042f8fb75SBob Moore 				/*
42142f8fb75SBob Moore 				 * It is (relatively) OK to attempt to install the SAME
42242f8fb75SBob Moore 				 * handler twice. This can easily happen with the
42342f8fb75SBob Moore 				 * PCI_Config space.
42442f8fb75SBob Moore 				 */
42542f8fb75SBob Moore 				status = AE_SAME_HANDLER;
42642f8fb75SBob Moore 				goto unlock_and_exit;
42742f8fb75SBob Moore 			} else {
42842f8fb75SBob Moore 				/* A handler is already installed */
42942f8fb75SBob Moore 
43042f8fb75SBob Moore 				status = AE_ALREADY_EXISTS;
43142f8fb75SBob Moore 			}
4327b738064SBob Moore 
43342f8fb75SBob Moore 			goto unlock_and_exit;
43442f8fb75SBob Moore 		}
43542f8fb75SBob Moore 	} else {
43642f8fb75SBob Moore 		ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
43742f8fb75SBob Moore 				  "Creating object on Device %p while installing handler\n",
43842f8fb75SBob Moore 				  node));
43942f8fb75SBob Moore 
44042f8fb75SBob Moore 		/* obj_desc does not exist, create one */
44142f8fb75SBob Moore 
44242f8fb75SBob Moore 		if (node->type == ACPI_TYPE_ANY) {
44342f8fb75SBob Moore 			type = ACPI_TYPE_DEVICE;
44442f8fb75SBob Moore 		} else {
44542f8fb75SBob Moore 			type = node->type;
44642f8fb75SBob Moore 		}
44742f8fb75SBob Moore 
44842f8fb75SBob Moore 		obj_desc = acpi_ut_create_internal_object(type);
44942f8fb75SBob Moore 		if (!obj_desc) {
45042f8fb75SBob Moore 			status = AE_NO_MEMORY;
45142f8fb75SBob Moore 			goto unlock_and_exit;
45242f8fb75SBob Moore 		}
45342f8fb75SBob Moore 
45442f8fb75SBob Moore 		/* Init new descriptor */
45542f8fb75SBob Moore 
45642f8fb75SBob Moore 		obj_desc->common.type = (u8)type;
45742f8fb75SBob Moore 
45842f8fb75SBob Moore 		/* Attach the new object to the Node */
45942f8fb75SBob Moore 
46042f8fb75SBob Moore 		status = acpi_ns_attach_object(node, obj_desc, type);
46142f8fb75SBob Moore 
46242f8fb75SBob Moore 		/* Remove local reference to the object */
46342f8fb75SBob Moore 
46442f8fb75SBob Moore 		acpi_ut_remove_reference(obj_desc);
46542f8fb75SBob Moore 
46642f8fb75SBob Moore 		if (ACPI_FAILURE(status)) {
46742f8fb75SBob Moore 			goto unlock_and_exit;
46842f8fb75SBob Moore 		}
46942f8fb75SBob Moore 	}
47042f8fb75SBob Moore 
47142f8fb75SBob Moore 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
4727b738064SBob Moore 			  "Installing address handler for region %s(%X) "
4737b738064SBob Moore 			  "on Device %4.4s %p(%p)\n",
47442f8fb75SBob Moore 			  acpi_ut_get_region_name(space_id), space_id,
47542f8fb75SBob Moore 			  acpi_ut_get_node_name(node), node, obj_desc));
47642f8fb75SBob Moore 
47742f8fb75SBob Moore 	/*
47842f8fb75SBob Moore 	 * Install the handler
47942f8fb75SBob Moore 	 *
48042f8fb75SBob Moore 	 * At this point there is no existing handler. Just allocate the object
48142f8fb75SBob Moore 	 * for the handler and link it into the list.
48242f8fb75SBob Moore 	 */
48342f8fb75SBob Moore 	handler_obj =
48442f8fb75SBob Moore 	    acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
48542f8fb75SBob Moore 	if (!handler_obj) {
48642f8fb75SBob Moore 		status = AE_NO_MEMORY;
48742f8fb75SBob Moore 		goto unlock_and_exit;
48842f8fb75SBob Moore 	}
48942f8fb75SBob Moore 
49042f8fb75SBob Moore 	/* Init handler obj */
49142f8fb75SBob Moore 
492c27f3d01SHans de Goede 	status =
493c27f3d01SHans de Goede 	    acpi_os_create_mutex(&handler_obj->address_space.context_mutex);
494c27f3d01SHans de Goede 	if (ACPI_FAILURE(status)) {
495c27f3d01SHans de Goede 		acpi_ut_remove_reference(handler_obj);
496c27f3d01SHans de Goede 		goto unlock_and_exit;
497c27f3d01SHans de Goede 	}
498c27f3d01SHans de Goede 
49942f8fb75SBob Moore 	handler_obj->address_space.space_id = (u8)space_id;
50042f8fb75SBob Moore 	handler_obj->address_space.handler_flags = flags;
50142f8fb75SBob Moore 	handler_obj->address_space.region_list = NULL;
50242f8fb75SBob Moore 	handler_obj->address_space.node = node;
50342f8fb75SBob Moore 	handler_obj->address_space.handler = handler;
50442f8fb75SBob Moore 	handler_obj->address_space.context = context;
50542f8fb75SBob Moore 	handler_obj->address_space.setup = setup;
50642f8fb75SBob Moore 
50742f8fb75SBob Moore 	/* Install at head of Device.address_space list */
50842f8fb75SBob Moore 
509aa6abd2bSLv Zheng 	handler_obj->address_space.next = obj_desc->common_notify.handler;
51042f8fb75SBob Moore 
51142f8fb75SBob Moore 	/*
51242f8fb75SBob Moore 	 * The Device object is the first reference on the handler_obj.
51342f8fb75SBob Moore 	 * Each region that uses the handler adds a reference.
51442f8fb75SBob Moore 	 */
515aa6abd2bSLv Zheng 	obj_desc->common_notify.handler = handler_obj;
51642f8fb75SBob Moore 
51742f8fb75SBob Moore 	/*
5187b738064SBob Moore 	 * Walk the namespace finding all of the regions this handler will
5197b738064SBob Moore 	 * manage.
52042f8fb75SBob Moore 	 *
5217b738064SBob Moore 	 * Start at the device and search the branch toward the leaf nodes
5227b738064SBob Moore 	 * until either the leaf is encountered or a device is detected that
5237b738064SBob Moore 	 * has an address handler of the same type.
52442f8fb75SBob Moore 	 *
5257b738064SBob Moore 	 * In either case, back up and search down the remainder of the branch
52642f8fb75SBob Moore 	 */
5277b738064SBob Moore 	status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node,
5287b738064SBob Moore 					ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
52942f8fb75SBob Moore 					acpi_ev_install_handler, NULL,
53042f8fb75SBob Moore 					handler_obj, NULL);
53142f8fb75SBob Moore 
53242f8fb75SBob Moore unlock_and_exit:
53342f8fb75SBob Moore 	return_ACPI_STATUS(status);
53442f8fb75SBob Moore }
535