xref: /openbmc/linux/drivers/acpi/acpica/evhandler.c (revision 8b235f2f)
1 /******************************************************************************
2  *
3  * Module Name: evhandler - Support for Address Space handlers
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acnamesp.h"
48 #include "acinterp.h"
49 
50 #define _COMPONENT          ACPI_EVENTS
51 ACPI_MODULE_NAME("evhandler")
52 
53 /* Local prototypes */
54 static acpi_status
55 acpi_ev_install_handler(acpi_handle obj_handle,
56 			u32 level, void *context, void **return_value);
57 
58 /* These are the address spaces that will get default handlers */
59 
60 u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
61 	ACPI_ADR_SPACE_SYSTEM_MEMORY,
62 	ACPI_ADR_SPACE_SYSTEM_IO,
63 	ACPI_ADR_SPACE_PCI_CONFIG,
64 	ACPI_ADR_SPACE_DATA_TABLE
65 };
66 
67 /*******************************************************************************
68  *
69  * FUNCTION:    acpi_ev_install_region_handlers
70  *
71  * PARAMETERS:  None
72  *
73  * RETURN:      Status
74  *
75  * DESCRIPTION: Installs the core subsystem default address space handlers.
76  *
77  ******************************************************************************/
78 
79 acpi_status acpi_ev_install_region_handlers(void)
80 {
81 	acpi_status status;
82 	u32 i;
83 
84 	ACPI_FUNCTION_TRACE(ev_install_region_handlers);
85 
86 	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
87 	if (ACPI_FAILURE(status)) {
88 		return_ACPI_STATUS(status);
89 	}
90 
91 	/*
92 	 * All address spaces (PCI Config, EC, SMBus) are scope dependent and
93 	 * registration must occur for a specific device.
94 	 *
95 	 * In the case of the system memory and IO address spaces there is
96 	 * currently no device associated with the address space. For these we
97 	 * use the root.
98 	 *
99 	 * We install the default PCI config space handler at the root so that
100 	 * this space is immediately available even though the we have not
101 	 * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
102 	 * specification which states that the PCI config space must be always
103 	 * available -- even though we are nowhere near ready to find the PCI root
104 	 * buses at this point.
105 	 *
106 	 * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
107 	 * has already been installed (via acpi_install_address_space_handler).
108 	 * Similar for AE_SAME_HANDLER.
109 	 */
110 	for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
111 		status = acpi_ev_install_space_handler(acpi_gbl_root_node,
112 						       acpi_gbl_default_address_spaces
113 						       [i],
114 						       ACPI_DEFAULT_HANDLER,
115 						       NULL, NULL);
116 		switch (status) {
117 		case AE_OK:
118 		case AE_SAME_HANDLER:
119 		case AE_ALREADY_EXISTS:
120 
121 			/* These exceptions are all OK */
122 
123 			status = AE_OK;
124 			break;
125 
126 		default:
127 
128 			goto unlock_and_exit;
129 		}
130 	}
131 
132 unlock_and_exit:
133 	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
134 	return_ACPI_STATUS(status);
135 }
136 
137 /*******************************************************************************
138  *
139  * FUNCTION:    acpi_ev_has_default_handler
140  *
141  * PARAMETERS:  node                - Namespace node for the device
142  *              space_id            - The address space ID
143  *
144  * RETURN:      TRUE if default handler is installed, FALSE otherwise
145  *
146  * DESCRIPTION: Check if the default handler is installed for the requested
147  *              space ID.
148  *
149  ******************************************************************************/
150 
151 u8
152 acpi_ev_has_default_handler(struct acpi_namespace_node *node,
153 			    acpi_adr_space_type space_id)
154 {
155 	union acpi_operand_object *obj_desc;
156 	union acpi_operand_object *handler_obj;
157 
158 	/* Must have an existing internal object */
159 
160 	obj_desc = acpi_ns_get_attached_object(node);
161 	if (obj_desc) {
162 		handler_obj = obj_desc->device.handler;
163 
164 		/* Walk the linked list of handlers for this object */
165 
166 		while (handler_obj) {
167 			if (handler_obj->address_space.space_id == space_id) {
168 				if (handler_obj->address_space.handler_flags &
169 				    ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
170 					return (TRUE);
171 				}
172 			}
173 
174 			handler_obj = handler_obj->address_space.next;
175 		}
176 	}
177 
178 	return (FALSE);
179 }
180 
181 /*******************************************************************************
182  *
183  * FUNCTION:    acpi_ev_install_handler
184  *
185  * PARAMETERS:  walk_namespace callback
186  *
187  * DESCRIPTION: This routine installs an address handler into objects that are
188  *              of type Region or Device.
189  *
190  *              If the Object is a Device, and the device has a handler of
191  *              the same type then the search is terminated in that branch.
192  *
193  *              This is because the existing handler is closer in proximity
194  *              to any more regions than the one we are trying to install.
195  *
196  ******************************************************************************/
197 
198 static acpi_status
199 acpi_ev_install_handler(acpi_handle obj_handle,
200 			u32 level, void *context, void **return_value)
201 {
202 	union acpi_operand_object *handler_obj;
203 	union acpi_operand_object *next_handler_obj;
204 	union acpi_operand_object *obj_desc;
205 	struct acpi_namespace_node *node;
206 	acpi_status status;
207 
208 	ACPI_FUNCTION_NAME(ev_install_handler);
209 
210 	handler_obj = (union acpi_operand_object *)context;
211 
212 	/* Parameter validation */
213 
214 	if (!handler_obj) {
215 		return (AE_OK);
216 	}
217 
218 	/* Convert and validate the device handle */
219 
220 	node = acpi_ns_validate_handle(obj_handle);
221 	if (!node) {
222 		return (AE_BAD_PARAMETER);
223 	}
224 
225 	/*
226 	 * We only care about regions and objects that are allowed to have
227 	 * address space handlers
228 	 */
229 	if ((node->type != ACPI_TYPE_DEVICE) &&
230 	    (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
231 		return (AE_OK);
232 	}
233 
234 	/* Check for an existing internal object */
235 
236 	obj_desc = acpi_ns_get_attached_object(node);
237 	if (!obj_desc) {
238 
239 		/* No object, just exit */
240 
241 		return (AE_OK);
242 	}
243 
244 	/* Devices are handled different than regions */
245 
246 	if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
247 
248 		/* Check if this Device already has a handler for this address space */
249 
250 		next_handler_obj = obj_desc->device.handler;
251 		while (next_handler_obj) {
252 
253 			/* Found a handler, is it for the same address space? */
254 
255 			if (next_handler_obj->address_space.space_id ==
256 			    handler_obj->address_space.space_id) {
257 				ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
258 						  "Found handler for region [%s] in device %p(%p) "
259 						  "handler %p\n",
260 						  acpi_ut_get_region_name
261 						  (handler_obj->address_space.
262 						   space_id), obj_desc,
263 						  next_handler_obj,
264 						  handler_obj));
265 
266 				/*
267 				 * Since the object we found it on was a device, then it
268 				 * means that someone has already installed a handler for
269 				 * the branch of the namespace from this device on. Just
270 				 * bail out telling the walk routine to not traverse this
271 				 * branch. This preserves the scoping rule for handlers.
272 				 */
273 				return (AE_CTRL_DEPTH);
274 			}
275 
276 			/* Walk the linked list of handlers attached to this device */
277 
278 			next_handler_obj = next_handler_obj->address_space.next;
279 		}
280 
281 		/*
282 		 * As long as the device didn't have a handler for this space we
283 		 * don't care about it. We just ignore it and proceed.
284 		 */
285 		return (AE_OK);
286 	}
287 
288 	/* Object is a Region */
289 
290 	if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
291 
292 		/* This region is for a different address space, just ignore it */
293 
294 		return (AE_OK);
295 	}
296 
297 	/*
298 	 * Now we have a region and it is for the handler's address space type.
299 	 *
300 	 * First disconnect region for any previous handler (if any)
301 	 */
302 	acpi_ev_detach_region(obj_desc, FALSE);
303 
304 	/* Connect the region to the new handler */
305 
306 	status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
307 	return (status);
308 }
309 
310 /*******************************************************************************
311  *
312  * FUNCTION:    acpi_ev_install_space_handler
313  *
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
319  *
320  * RETURN:      Status
321  *
322  * DESCRIPTION: Install a handler for all op_regions of a given space_id.
323  *              Assumes namespace is locked
324  *
325  ******************************************************************************/
326 
327 acpi_status
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)
332 {
333 	union acpi_operand_object *obj_desc;
334 	union acpi_operand_object *handler_obj;
335 	acpi_status status;
336 	acpi_object_type type;
337 	u8 flags = 0;
338 
339 	ACPI_FUNCTION_TRACE(ev_install_space_handler);
340 
341 	/*
342 	 * This registration is valid for only the types below and the root. This
343 	 * is where the default handlers get placed.
344 	 */
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;
350 	}
351 
352 	if (handler == ACPI_DEFAULT_HANDLER) {
353 		flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
354 
355 		switch (space_id) {
356 		case ACPI_ADR_SPACE_SYSTEM_MEMORY:
357 
358 			handler = acpi_ex_system_memory_space_handler;
359 			setup = acpi_ev_system_memory_region_setup;
360 			break;
361 
362 		case ACPI_ADR_SPACE_SYSTEM_IO:
363 
364 			handler = acpi_ex_system_io_space_handler;
365 			setup = acpi_ev_io_space_region_setup;
366 			break;
367 
368 		case ACPI_ADR_SPACE_PCI_CONFIG:
369 
370 			handler = acpi_ex_pci_config_space_handler;
371 			setup = acpi_ev_pci_config_region_setup;
372 			break;
373 
374 		case ACPI_ADR_SPACE_CMOS:
375 
376 			handler = acpi_ex_cmos_space_handler;
377 			setup = acpi_ev_cmos_region_setup;
378 			break;
379 
380 		case ACPI_ADR_SPACE_PCI_BAR_TARGET:
381 
382 			handler = acpi_ex_pci_bar_space_handler;
383 			setup = acpi_ev_pci_bar_region_setup;
384 			break;
385 
386 		case ACPI_ADR_SPACE_DATA_TABLE:
387 
388 			handler = acpi_ex_data_table_space_handler;
389 			setup = NULL;
390 			break;
391 
392 		default:
393 
394 			status = AE_BAD_PARAMETER;
395 			goto unlock_and_exit;
396 		}
397 	}
398 
399 	/* If the caller hasn't specified a setup routine, use the default */
400 
401 	if (!setup) {
402 		setup = acpi_ev_default_region_setup;
403 	}
404 
405 	/* Check for an existing internal object */
406 
407 	obj_desc = acpi_ns_get_attached_object(node);
408 	if (obj_desc) {
409 		/*
410 		 * The attached device object already exists. Make sure the handler
411 		 * is not already installed.
412 		 */
413 		handler_obj = obj_desc->device.handler;
414 
415 		/* Walk the handler list for this device */
416 
417 		while (handler_obj) {
418 
419 			/* Same space_id indicates a handler already installed */
420 
421 			if (handler_obj->address_space.space_id == space_id) {
422 				if (handler_obj->address_space.handler ==
423 				    handler) {
424 					/*
425 					 * It is (relatively) OK to attempt to install the SAME
426 					 * handler twice. This can easily happen with the
427 					 * PCI_Config space.
428 					 */
429 					status = AE_SAME_HANDLER;
430 					goto unlock_and_exit;
431 				} else {
432 					/* A handler is already installed */
433 
434 					status = AE_ALREADY_EXISTS;
435 				}
436 				goto unlock_and_exit;
437 			}
438 
439 			/* Walk the linked list of handlers */
440 
441 			handler_obj = handler_obj->address_space.next;
442 		}
443 	} else {
444 		ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
445 				  "Creating object on Device %p while installing handler\n",
446 				  node));
447 
448 		/* obj_desc does not exist, create one */
449 
450 		if (node->type == ACPI_TYPE_ANY) {
451 			type = ACPI_TYPE_DEVICE;
452 		} else {
453 			type = node->type;
454 		}
455 
456 		obj_desc = acpi_ut_create_internal_object(type);
457 		if (!obj_desc) {
458 			status = AE_NO_MEMORY;
459 			goto unlock_and_exit;
460 		}
461 
462 		/* Init new descriptor */
463 
464 		obj_desc->common.type = (u8)type;
465 
466 		/* Attach the new object to the Node */
467 
468 		status = acpi_ns_attach_object(node, obj_desc, type);
469 
470 		/* Remove local reference to the object */
471 
472 		acpi_ut_remove_reference(obj_desc);
473 
474 		if (ACPI_FAILURE(status)) {
475 			goto unlock_and_exit;
476 		}
477 	}
478 
479 	ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
480 			  "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
481 			  acpi_ut_get_region_name(space_id), space_id,
482 			  acpi_ut_get_node_name(node), node, obj_desc));
483 
484 	/*
485 	 * Install the handler
486 	 *
487 	 * At this point there is no existing handler. Just allocate the object
488 	 * for the handler and link it into the list.
489 	 */
490 	handler_obj =
491 	    acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
492 	if (!handler_obj) {
493 		status = AE_NO_MEMORY;
494 		goto unlock_and_exit;
495 	}
496 
497 	/* Init handler obj */
498 
499 	handler_obj->address_space.space_id = (u8)space_id;
500 	handler_obj->address_space.handler_flags = flags;
501 	handler_obj->address_space.region_list = NULL;
502 	handler_obj->address_space.node = node;
503 	handler_obj->address_space.handler = handler;
504 	handler_obj->address_space.context = context;
505 	handler_obj->address_space.setup = setup;
506 
507 	/* Install at head of Device.address_space list */
508 
509 	handler_obj->address_space.next = obj_desc->device.handler;
510 
511 	/*
512 	 * The Device object is the first reference on the handler_obj.
513 	 * Each region that uses the handler adds a reference.
514 	 */
515 	obj_desc->device.handler = handler_obj;
516 
517 	/*
518 	 * Walk the namespace finding all of the regions this
519 	 * handler will manage.
520 	 *
521 	 * Start at the device and search the branch toward
522 	 * the leaf nodes until either the leaf is encountered or
523 	 * a device is detected that has an address handler of the
524 	 * same type.
525 	 *
526 	 * In either case, back up and search down the remainder
527 	 * of the branch
528 	 */
529 	status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
530 					ACPI_NS_WALK_UNLOCK,
531 					acpi_ev_install_handler, NULL,
532 					handler_obj, NULL);
533 
534 unlock_and_exit:
535 	return_ACPI_STATUS(status);
536 }
537