xref: /linux/drivers/acpi/acpica/evhandler.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: evhandler - Support for Address Space handlers
5  *
6  * Copyright (C) 2000 - 2026, Intel Corp.
7  *
8  *****************************************************************************/
9 
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acevents.h"
13 #include "acnamesp.h"
14 #include "acinterp.h"
15 
16 #define _COMPONENT          ACPI_EVENTS
17 ACPI_MODULE_NAME("evhandler")
18 
19 /* Local prototypes */
20 static acpi_status
21 acpi_ev_install_handler(acpi_handle obj_handle,
22 			u32 level, void *context, void **return_value);
23 
24 /* These are the address spaces that will get default handlers */
25 
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
31 };
32 
33 /*******************************************************************************
34  *
35  * FUNCTION:    acpi_ev_install_region_handlers
36  *
37  * PARAMETERS:  None
38  *
39  * RETURN:      Status
40  *
41  * DESCRIPTION: Installs the core subsystem default address space handlers.
42  *
43  ******************************************************************************/
44 
45 acpi_status acpi_ev_install_region_handlers(void)
46 {
47 	acpi_status status;
48 	u32 i;
49 
50 	ACPI_FUNCTION_TRACE(ev_install_region_handlers);
51 
52 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
53 	if (ACPI_FAILURE(status)) {
54 		return_ACPI_STATUS(status);
55 	}
56 
57 	/*
58 	 * All address spaces (PCI Config, EC, SMBus) are scope dependent and
59 	 * registration must occur for a specific device.
60 	 *
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
63 	 * use the root.
64 	 *
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.
71 	 *
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.
75 	 */
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
79 						       [i],
80 						       ACPI_DEFAULT_HANDLER,
81 						       NULL, NULL);
82 		switch (status) {
83 		case AE_OK:
84 		case AE_SAME_HANDLER:
85 		case AE_ALREADY_EXISTS:
86 
87 			/* These exceptions are all OK */
88 
89 			status = AE_OK;
90 			break;
91 
92 		default:
93 
94 			goto unlock_and_exit;
95 		}
96 	}
97 
98 unlock_and_exit:
99 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
100 	return_ACPI_STATUS(status);
101 }
102 
103 /*******************************************************************************
104  *
105  * FUNCTION:    acpi_ev_has_default_handler
106  *
107  * PARAMETERS:  node                - Namespace node for the device
108  *              space_id            - The address space ID
109  *
110  * RETURN:      TRUE if default handler is installed, FALSE otherwise
111  *
112  * DESCRIPTION: Check if the default handler is installed for the requested
113  *              space ID.
114  *
115  ******************************************************************************/
116 
117 u8
118 acpi_ev_has_default_handler(struct acpi_namespace_node *node,
119 			    acpi_adr_space_type space_id)
120 {
121 	union acpi_operand_object *obj_desc;
122 	union acpi_operand_object *handler_obj;
123 
124 	/* Must have an existing internal object */
125 
126 	obj_desc = acpi_ns_get_attached_object(node);
127 	if (obj_desc) {
128 		handler_obj = obj_desc->common_notify.handler;
129 
130 		/* Walk the linked list of handlers for this object */
131 
132 		while (handler_obj) {
133 
134 			/* Validate handler object type before accessing fields */
135 
136 			if (handler_obj->common.type !=
137 			    ACPI_TYPE_LOCAL_ADDRESS_HANDLER) {
138 				break;
139 			}
140 
141 			if (handler_obj->address_space.space_id == space_id) {
142 				if (handler_obj->address_space.handler_flags &
143 				    ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
144 					return (TRUE);
145 				}
146 			}
147 
148 			handler_obj = handler_obj->address_space.next;
149 		}
150 	}
151 
152 	return (FALSE);
153 }
154 
155 /*******************************************************************************
156  *
157  * FUNCTION:    acpi_ev_install_handler
158  *
159  * PARAMETERS:  walk_namespace callback
160  *
161  * DESCRIPTION: This routine installs an address handler into objects that are
162  *              of type Region or Device.
163  *
164  *              If the Object is a Device, and the device has a handler of
165  *              the same type then the search is terminated in that branch.
166  *
167  *              This is because the existing handler is closer in proximity
168  *              to any more regions than the one we are trying to install.
169  *
170  ******************************************************************************/
171 
172 static acpi_status
173 acpi_ev_install_handler(acpi_handle obj_handle,
174 			u32 level, void *context, void **return_value)
175 {
176 	union acpi_operand_object *handler_obj;
177 	union acpi_operand_object *next_handler_obj;
178 	union acpi_operand_object *obj_desc;
179 	struct acpi_namespace_node *node;
180 	acpi_status status;
181 
182 	ACPI_FUNCTION_NAME(ev_install_handler);
183 
184 	handler_obj = (union acpi_operand_object *)context;
185 
186 	/* Parameter validation */
187 
188 	if (!handler_obj) {
189 		return (AE_OK);
190 	}
191 
192 	/* Convert and validate the device handle */
193 
194 	node = acpi_ns_validate_handle(obj_handle);
195 	if (!node) {
196 		return (AE_BAD_PARAMETER);
197 	}
198 
199 	/*
200 	 * We only care about regions and objects that are allowed to have
201 	 * address space handlers
202 	 */
203 	if ((node->type != ACPI_TYPE_DEVICE) &&
204 	    (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
205 		return (AE_OK);
206 	}
207 
208 	/* Check for an existing internal object */
209 
210 	obj_desc = acpi_ns_get_attached_object(node);
211 	if (!obj_desc) {
212 
213 		/* No object, just exit */
214 
215 		return (AE_OK);
216 	}
217 
218 	/* Devices are handled different than regions */
219 
220 	if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
221 
222 		/* Check if this Device already has a handler for this address space */
223 
224 		next_handler_obj =
225 		    acpi_ev_find_region_handler(handler_obj->address_space.
226 						space_id,
227 						obj_desc->common_notify.
228 						handler);
229 		if (next_handler_obj) {
230 
231 			/* Found a handler, is it for the same address space? */
232 
233 			ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
234 					  "Found handler for region [%s] in device %p(%p) handler %p\n",
235 					  acpi_ut_get_region_name(handler_obj->
236 								  address_space.
237 								  space_id),
238 					  obj_desc, next_handler_obj,
239 					  handler_obj));
240 
241 			/*
242 			 * Since the object we found it on was a device, then it means
243 			 * that someone has already installed a handler for the branch
244 			 * of the namespace from this device on. Just bail out telling
245 			 * the walk routine to not traverse this branch. This preserves
246 			 * the scoping rule for handlers.
247 			 */
248 			return (AE_CTRL_DEPTH);
249 		}
250 
251 		/*
252 		 * As long as the device didn't have a handler for this space we
253 		 * don't care about it. We just ignore it and proceed.
254 		 */
255 		return (AE_OK);
256 	}
257 
258 	/* Object is a Region */
259 
260 	if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
261 
262 		/* This region is for a different address space, just ignore it */
263 
264 		return (AE_OK);
265 	}
266 
267 	/*
268 	 * Now we have a region and it is for the handler's address space type.
269 	 *
270 	 * First disconnect region for any previous handler (if any)
271 	 */
272 	acpi_ev_detach_region(obj_desc, FALSE);
273 
274 	/* Connect the region to the new handler */
275 
276 	status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
277 	return (status);
278 }
279 
280 /*******************************************************************************
281  *
282  * FUNCTION:    acpi_ev_find_region_handler
283  *
284  * PARAMETERS:  space_id        - The address space ID
285  *              handler_obj     - Head of the handler object list
286  *
287  * RETURN:      Matching handler object. NULL if space ID not matched
288  *
289  * DESCRIPTION: Search a handler object list for a match on the address
290  *              space ID.
291  *
292  ******************************************************************************/
293 
294 union acpi_operand_object *acpi_ev_find_region_handler(acpi_adr_space_type
295 						       space_id,
296 						       union acpi_operand_object
297 						       *handler_obj)
298 {
299 
300 	/* Walk the handler list for this device */
301 
302 	while (handler_obj) {
303 		if (handler_obj->common.type != ACPI_TYPE_LOCAL_ADDRESS_HANDLER) {
304 			break;
305 		}
306 
307 		/* Same space_id indicates a handler is installed */
308 
309 		if (handler_obj->address_space.space_id == space_id) {
310 			return (handler_obj);
311 		}
312 
313 		/* Next handler object */
314 
315 		handler_obj = handler_obj->address_space.next;
316 	}
317 
318 	return (NULL);
319 }
320 
321 /*******************************************************************************
322  *
323  * FUNCTION:    acpi_ev_install_space_handler
324  *
325  * PARAMETERS:  node            - Namespace node for the device
326  *              space_id        - The address space ID
327  *              handler         - Address of the handler
328  *              setup           - Address of the setup function
329  *              context         - Value passed to the handler on each access
330  *
331  * RETURN:      Status
332  *
333  * DESCRIPTION: Install a handler for all op_regions of a given space_id.
334  *              Assumes namespace is locked
335  *
336  ******************************************************************************/
337 
338 acpi_status
339 acpi_ev_install_space_handler(struct acpi_namespace_node *node,
340 			      acpi_adr_space_type space_id,
341 			      acpi_adr_space_handler handler,
342 			      acpi_adr_space_setup setup, void *context)
343 {
344 	union acpi_operand_object *obj_desc;
345 	union acpi_operand_object *handler_obj;
346 	acpi_status status = AE_OK;
347 	acpi_object_type type;
348 	u8 flags = 0;
349 
350 	ACPI_FUNCTION_TRACE(ev_install_space_handler);
351 
352 	/*
353 	 * This registration is valid for only the types below and the root.
354 	 * The root node is where the default handlers get installed.
355 	 */
356 	if ((node->type != ACPI_TYPE_DEVICE) &&
357 	    (node->type != ACPI_TYPE_PROCESSOR) &&
358 	    (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
359 		status = AE_BAD_PARAMETER;
360 		goto unlock_and_exit;
361 	}
362 
363 	if (handler == ACPI_DEFAULT_HANDLER) {
364 		flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
365 
366 		switch (space_id) {
367 		case ACPI_ADR_SPACE_SYSTEM_MEMORY:
368 
369 			handler = acpi_ex_system_memory_space_handler;
370 			setup = acpi_ev_system_memory_region_setup;
371 			break;
372 
373 		case ACPI_ADR_SPACE_SYSTEM_IO:
374 
375 			handler = acpi_ex_system_io_space_handler;
376 			setup = acpi_ev_io_space_region_setup;
377 			break;
378 #ifdef ACPI_PCI_CONFIGURED
379 		case ACPI_ADR_SPACE_PCI_CONFIG:
380 
381 			handler = acpi_ex_pci_config_space_handler;
382 			setup = acpi_ev_pci_config_region_setup;
383 			break;
384 #endif
385 		case ACPI_ADR_SPACE_CMOS:
386 
387 			handler = acpi_ex_cmos_space_handler;
388 			setup = acpi_ev_cmos_region_setup;
389 			break;
390 #ifdef ACPI_PCI_CONFIGURED
391 		case ACPI_ADR_SPACE_PCI_BAR_TARGET:
392 
393 			handler = acpi_ex_pci_bar_space_handler;
394 			setup = acpi_ev_pci_bar_region_setup;
395 			break;
396 #endif
397 		case ACPI_ADR_SPACE_DATA_TABLE:
398 
399 			handler = acpi_ex_data_table_space_handler;
400 			setup = acpi_ev_data_table_region_setup;
401 			break;
402 
403 		default:
404 
405 			status = AE_BAD_PARAMETER;
406 			goto unlock_and_exit;
407 		}
408 	}
409 
410 	/* If the caller hasn't specified a setup routine, use the default */
411 
412 	if (!setup) {
413 		setup = acpi_ev_default_region_setup;
414 	}
415 
416 	/* Check for an existing internal object */
417 
418 	obj_desc = acpi_ns_get_attached_object(node);
419 	if (obj_desc) {
420 		/*
421 		 * The attached device object already exists. Now make sure
422 		 * the handler is not already installed.
423 		 */
424 		handler_obj = acpi_ev_find_region_handler(space_id,
425 							  obj_desc->
426 							  common_notify.
427 							  handler);
428 
429 		if (handler_obj) {
430 			if (handler_obj->address_space.handler == handler) {
431 				/*
432 				 * It is (relatively) OK to attempt to install the SAME
433 				 * handler twice. This can easily happen with the
434 				 * PCI_Config space.
435 				 */
436 				status = AE_SAME_HANDLER;
437 				goto unlock_and_exit;
438 			} else {
439 				/* A handler is already installed */
440 
441 				status = AE_ALREADY_EXISTS;
442 			}
443 
444 			goto unlock_and_exit;
445 		}
446 	} else {
447 		ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
448 				  "Creating object on Device %p while installing handler\n",
449 				  node));
450 
451 		/* obj_desc does not exist, create one */
452 
453 		if (node->type == ACPI_TYPE_ANY) {
454 			type = ACPI_TYPE_DEVICE;
455 		} else {
456 			type = node->type;
457 		}
458 
459 		obj_desc = acpi_ut_create_internal_object(type);
460 		if (!obj_desc) {
461 			status = AE_NO_MEMORY;
462 			goto unlock_and_exit;
463 		}
464 
465 		/* Init new descriptor */
466 
467 		obj_desc->common.type = (u8)type;
468 
469 		/* Attach the new object to the Node */
470 
471 		status = acpi_ns_attach_object(node, obj_desc, type);
472 
473 		/* Remove local reference to the object */
474 
475 		acpi_ut_remove_reference(obj_desc);
476 
477 		if (ACPI_FAILURE(status)) {
478 			goto unlock_and_exit;
479 		}
480 	}
481 
482 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
483 			  "Installing address handler for region %s(%X) "
484 			  "on Device %4.4s %p(%p)\n",
485 			  acpi_ut_get_region_name(space_id), space_id,
486 			  acpi_ut_get_node_name(node), node, obj_desc));
487 
488 	/*
489 	 * Install the handler
490 	 *
491 	 * At this point there is no existing handler. Just allocate the object
492 	 * for the handler and link it into the list.
493 	 */
494 	handler_obj =
495 	    acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
496 	if (!handler_obj) {
497 		status = AE_NO_MEMORY;
498 		goto unlock_and_exit;
499 	}
500 
501 	/* Init handler obj */
502 
503 	status =
504 	    acpi_os_create_mutex(&handler_obj->address_space.context_mutex);
505 	if (ACPI_FAILURE(status)) {
506 		acpi_ut_remove_reference(handler_obj);
507 		goto unlock_and_exit;
508 	}
509 
510 	handler_obj->address_space.space_id = (u8)space_id;
511 	handler_obj->address_space.handler_flags = flags;
512 	handler_obj->address_space.region_list = NULL;
513 	handler_obj->address_space.node = node;
514 	handler_obj->address_space.handler = handler;
515 	handler_obj->address_space.context = context;
516 	handler_obj->address_space.setup = setup;
517 
518 	/* Install at head of Device.address_space list */
519 
520 	handler_obj->address_space.next = obj_desc->common_notify.handler;
521 
522 	/*
523 	 * The Device object is the first reference on the handler_obj.
524 	 * Each region that uses the handler adds a reference.
525 	 */
526 	obj_desc->common_notify.handler = handler_obj;
527 
528 	/*
529 	 * Walk the namespace finding all of the regions this handler will
530 	 * manage.
531 	 *
532 	 * Start at the device and search the branch toward the leaf nodes
533 	 * until either the leaf is encountered or a device is detected that
534 	 * has an address handler of the same type.
535 	 *
536 	 * In either case, back up and search down the remainder of the branch
537 	 */
538 	status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node,
539 					ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
540 					acpi_ev_install_handler, NULL,
541 					handler_obj, NULL);
542 
543 unlock_and_exit:
544 	return_ACPI_STATUS(status);
545 }
546