1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: evhandler - Support for Address Space handlers
6 * Copyright (C) 2000 - 2019, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
16 #define _COMPONENT ACPI_EVENTS
17 ACPI_MODULE_NAME("evhandler")
19 /* Local prototypes */
21 acpi_ev_install_handler(acpi_handle obj_handle
,
22 u32 level
, void *context
, void **return_value
);
24 /* These are the address spaces that will get default handlers */
26 u8 acpi_gbl_default_address_spaces
[ACPI_NUM_DEFAULT_SPACES
] = {
27 ACPI_ADR_SPACE_SYSTEM_MEMORY
,
28 ACPI_ADR_SPACE_SYSTEM_IO
,
29 ACPI_ADR_SPACE_PCI_CONFIG
,
30 ACPI_ADR_SPACE_DATA_TABLE
33 /*******************************************************************************
35 * FUNCTION: acpi_ev_install_region_handlers
41 * DESCRIPTION: Installs the core subsystem default address space handlers.
43 ******************************************************************************/
45 acpi_status
acpi_ev_install_region_handlers(void)
50 ACPI_FUNCTION_TRACE(ev_install_region_handlers
);
52 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
53 if (ACPI_FAILURE(status
)) {
54 return_ACPI_STATUS(status
);
58 * All address spaces (PCI Config, EC, SMBus) are scope dependent and
59 * registration must occur for a specific device.
61 * In the case of the system memory and IO address spaces there is
62 * currently no device associated with the address space. For these we
65 * We install the default PCI config space handler at the root so that
66 * this space is immediately available even though the we have not
67 * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
68 * specification which states that the PCI config space must be always
69 * available -- even though we are nowhere near ready to find the PCI root
70 * buses at this point.
72 * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
73 * has already been installed (via acpi_install_address_space_handler).
74 * Similar for AE_SAME_HANDLER.
76 for (i
= 0; i
< ACPI_NUM_DEFAULT_SPACES
; i
++) {
77 status
= acpi_ev_install_space_handler(acpi_gbl_root_node
,
78 acpi_gbl_default_address_spaces
85 case AE_ALREADY_EXISTS
:
87 /* These exceptions are all OK */
99 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
100 return_ACPI_STATUS(status
);
103 /*******************************************************************************
105 * FUNCTION: acpi_ev_has_default_handler
107 * PARAMETERS: node - Namespace node for the device
108 * space_id - The address space ID
110 * RETURN: TRUE if default handler is installed, FALSE otherwise
112 * DESCRIPTION: Check if the default handler is installed for the requested
115 ******************************************************************************/
118 acpi_ev_has_default_handler(struct acpi_namespace_node
*node
,
119 acpi_adr_space_type space_id
)
121 union acpi_operand_object
*obj_desc
;
122 union acpi_operand_object
*handler_obj
;
124 /* Must have an existing internal object */
126 obj_desc
= acpi_ns_get_attached_object(node
);
128 handler_obj
= obj_desc
->common_notify
.handler
;
130 /* Walk the linked list of handlers for this object */
132 while (handler_obj
) {
133 if (handler_obj
->address_space
.space_id
== space_id
) {
134 if (handler_obj
->address_space
.handler_flags
&
135 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
) {
140 handler_obj
= handler_obj
->address_space
.next
;
147 /*******************************************************************************
149 * FUNCTION: acpi_ev_install_handler
151 * PARAMETERS: walk_namespace callback
153 * DESCRIPTION: This routine installs an address handler into objects that are
154 * of type Region or Device.
156 * If the Object is a Device, and the device has a handler of
157 * the same type then the search is terminated in that branch.
159 * This is because the existing handler is closer in proximity
160 * to any more regions than the one we are trying to install.
162 ******************************************************************************/
165 acpi_ev_install_handler(acpi_handle obj_handle
,
166 u32 level
, void *context
, void **return_value
)
168 union acpi_operand_object
*handler_obj
;
169 union acpi_operand_object
*next_handler_obj
;
170 union acpi_operand_object
*obj_desc
;
171 struct acpi_namespace_node
*node
;
174 ACPI_FUNCTION_NAME(ev_install_handler
);
176 handler_obj
= (union acpi_operand_object
*)context
;
178 /* Parameter validation */
184 /* Convert and validate the device handle */
186 node
= acpi_ns_validate_handle(obj_handle
);
188 return (AE_BAD_PARAMETER
);
192 * We only care about regions and objects that are allowed to have
193 * address space handlers
195 if ((node
->type
!= ACPI_TYPE_DEVICE
) &&
196 (node
->type
!= ACPI_TYPE_REGION
) && (node
!= acpi_gbl_root_node
)) {
200 /* Check for an existing internal object */
202 obj_desc
= acpi_ns_get_attached_object(node
);
205 /* No object, just exit */
210 /* Devices are handled different than regions */
212 if (obj_desc
->common
.type
== ACPI_TYPE_DEVICE
) {
214 /* Check if this Device already has a handler for this address space */
217 acpi_ev_find_region_handler(handler_obj
->address_space
.
219 obj_desc
->common_notify
.
221 if (next_handler_obj
) {
223 /* Found a handler, is it for the same address space? */
225 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
226 "Found handler for region [%s] in device %p(%p) handler %p\n",
227 acpi_ut_get_region_name(handler_obj
->
230 obj_desc
, next_handler_obj
,
234 * Since the object we found it on was a device, then it means
235 * that someone has already installed a handler for the branch
236 * of the namespace from this device on. Just bail out telling
237 * the walk routine to not traverse this branch. This preserves
238 * the scoping rule for handlers.
240 return (AE_CTRL_DEPTH
);
244 * As long as the device didn't have a handler for this space we
245 * don't care about it. We just ignore it and proceed.
250 /* Object is a Region */
252 if (obj_desc
->region
.space_id
!= handler_obj
->address_space
.space_id
) {
254 /* This region is for a different address space, just ignore it */
260 * Now we have a region and it is for the handler's address space type.
262 * First disconnect region for any previous handler (if any)
264 acpi_ev_detach_region(obj_desc
, FALSE
);
266 /* Connect the region to the new handler */
268 status
= acpi_ev_attach_region(handler_obj
, obj_desc
, FALSE
);
272 /*******************************************************************************
274 * FUNCTION: acpi_ev_find_region_handler
276 * PARAMETERS: space_id - The address space ID
277 * handler_obj - Head of the handler object list
279 * RETURN: Matching handler object. NULL if space ID not matched
281 * DESCRIPTION: Search a handler object list for a match on the address
284 ******************************************************************************/
286 union acpi_operand_object
*acpi_ev_find_region_handler(acpi_adr_space_type
288 union acpi_operand_object
292 /* Walk the handler list for this device */
294 while (handler_obj
) {
296 /* Same space_id indicates a handler is installed */
298 if (handler_obj
->address_space
.space_id
== space_id
) {
299 return (handler_obj
);
302 /* Next handler object */
304 handler_obj
= handler_obj
->address_space
.next
;
310 /*******************************************************************************
312 * FUNCTION: acpi_ev_install_space_handler
314 * PARAMETERS: node - Namespace node for the device
315 * space_id - The address space ID
316 * handler - Address of the handler
317 * setup - Address of the setup function
318 * context - Value passed to the handler on each access
322 * DESCRIPTION: Install a handler for all op_regions of a given space_id.
323 * Assumes namespace is locked
325 ******************************************************************************/
328 acpi_ev_install_space_handler(struct acpi_namespace_node
*node
,
329 acpi_adr_space_type space_id
,
330 acpi_adr_space_handler handler
,
331 acpi_adr_space_setup setup
, void *context
)
333 union acpi_operand_object
*obj_desc
;
334 union acpi_operand_object
*handler_obj
;
335 acpi_status status
= AE_OK
;
336 acpi_object_type type
;
339 ACPI_FUNCTION_TRACE(ev_install_space_handler
);
342 * This registration is valid for only the types below and the root.
343 * The root node is where the default handlers get installed.
345 if ((node
->type
!= ACPI_TYPE_DEVICE
) &&
346 (node
->type
!= ACPI_TYPE_PROCESSOR
) &&
347 (node
->type
!= ACPI_TYPE_THERMAL
) && (node
!= acpi_gbl_root_node
)) {
348 status
= AE_BAD_PARAMETER
;
349 goto unlock_and_exit
;
352 if (handler
== ACPI_DEFAULT_HANDLER
) {
353 flags
= ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
;
356 case ACPI_ADR_SPACE_SYSTEM_MEMORY
:
358 handler
= acpi_ex_system_memory_space_handler
;
359 setup
= acpi_ev_system_memory_region_setup
;
362 case ACPI_ADR_SPACE_SYSTEM_IO
:
364 handler
= acpi_ex_system_io_space_handler
;
365 setup
= acpi_ev_io_space_region_setup
;
367 #ifdef ACPI_PCI_CONFIGURED
368 case ACPI_ADR_SPACE_PCI_CONFIG
:
370 handler
= acpi_ex_pci_config_space_handler
;
371 setup
= acpi_ev_pci_config_region_setup
;
374 case ACPI_ADR_SPACE_CMOS
:
376 handler
= acpi_ex_cmos_space_handler
;
377 setup
= acpi_ev_cmos_region_setup
;
379 #ifdef ACPI_PCI_CONFIGURED
380 case ACPI_ADR_SPACE_PCI_BAR_TARGET
:
382 handler
= acpi_ex_pci_bar_space_handler
;
383 setup
= acpi_ev_pci_bar_region_setup
;
386 case ACPI_ADR_SPACE_DATA_TABLE
:
388 handler
= acpi_ex_data_table_space_handler
;
394 status
= AE_BAD_PARAMETER
;
395 goto unlock_and_exit
;
399 /* If the caller hasn't specified a setup routine, use the default */
402 setup
= acpi_ev_default_region_setup
;
405 /* Check for an existing internal object */
407 obj_desc
= acpi_ns_get_attached_object(node
);
410 * The attached device object already exists. Now make sure
411 * the handler is not already installed.
413 handler_obj
= acpi_ev_find_region_handler(space_id
,
419 if (handler_obj
->address_space
.handler
== handler
) {
421 * It is (relatively) OK to attempt to install the SAME
422 * handler twice. This can easily happen with the
425 status
= AE_SAME_HANDLER
;
426 goto unlock_and_exit
;
428 /* A handler is already installed */
430 status
= AE_ALREADY_EXISTS
;
433 goto unlock_and_exit
;
436 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
437 "Creating object on Device %p while installing handler\n",
440 /* obj_desc does not exist, create one */
442 if (node
->type
== ACPI_TYPE_ANY
) {
443 type
= ACPI_TYPE_DEVICE
;
448 obj_desc
= acpi_ut_create_internal_object(type
);
450 status
= AE_NO_MEMORY
;
451 goto unlock_and_exit
;
454 /* Init new descriptor */
456 obj_desc
->common
.type
= (u8
)type
;
458 /* Attach the new object to the Node */
460 status
= acpi_ns_attach_object(node
, obj_desc
, type
);
462 /* Remove local reference to the object */
464 acpi_ut_remove_reference(obj_desc
);
466 if (ACPI_FAILURE(status
)) {
467 goto unlock_and_exit
;
471 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
472 "Installing address handler for region %s(%X) "
473 "on Device %4.4s %p(%p)\n",
474 acpi_ut_get_region_name(space_id
), space_id
,
475 acpi_ut_get_node_name(node
), node
, obj_desc
));
478 * Install the handler
480 * At this point there is no existing handler. Just allocate the object
481 * for the handler and link it into the list.
484 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER
);
486 status
= AE_NO_MEMORY
;
487 goto unlock_and_exit
;
490 /* Init handler obj */
492 handler_obj
->address_space
.space_id
= (u8
)space_id
;
493 handler_obj
->address_space
.handler_flags
= flags
;
494 handler_obj
->address_space
.region_list
= NULL
;
495 handler_obj
->address_space
.node
= node
;
496 handler_obj
->address_space
.handler
= handler
;
497 handler_obj
->address_space
.context
= context
;
498 handler_obj
->address_space
.setup
= setup
;
500 /* Install at head of Device.address_space list */
502 handler_obj
->address_space
.next
= obj_desc
->common_notify
.handler
;
505 * The Device object is the first reference on the handler_obj.
506 * Each region that uses the handler adds a reference.
508 obj_desc
->common_notify
.handler
= handler_obj
;
511 * Walk the namespace finding all of the regions this handler will
514 * Start at the device and search the branch toward the leaf nodes
515 * until either the leaf is encountered or a device is detected that
516 * has an address handler of the same type.
518 * In either case, back up and search down the remainder of the branch
520 status
= acpi_ns_walk_namespace(ACPI_TYPE_ANY
, node
,
521 ACPI_UINT32_MAX
, ACPI_NS_WALK_UNLOCK
,
522 acpi_ev_install_handler
, NULL
,
526 return_ACPI_STATUS(status
);