Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / acpi / acpica / evhandler.c
blob20fb51c06b8ddec4dda02899b18ad8bd84e07753
1 /******************************************************************************
3 * Module Name: evhandler - Support for Address Space handlers
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2018, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acevents.h"
47 #include "acnamesp.h"
48 #include "acinterp.h"
50 #define _COMPONENT ACPI_EVENTS
51 ACPI_MODULE_NAME("evhandler")
53 /* Local prototypes */
54 static acpi_status
55 acpi_ev_install_handler(acpi_handle obj_handle,
56 u32 level, void *context, void **return_value);
58 /* These are the address spaces that will get default handlers */
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
67 /*******************************************************************************
69 * FUNCTION: acpi_ev_install_region_handlers
71 * PARAMETERS: None
73 * RETURN: Status
75 * DESCRIPTION: Installs the core subsystem default address space handlers.
77 ******************************************************************************/
79 acpi_status acpi_ev_install_region_handlers(void)
81 acpi_status status;
82 u32 i;
84 ACPI_FUNCTION_TRACE(ev_install_region_handlers);
86 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
87 if (ACPI_FAILURE(status)) {
88 return_ACPI_STATUS(status);
92 * All address spaces (PCI Config, EC, SMBus) are scope dependent and
93 * registration must occur for a specific device.
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.
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.
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.
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:
121 /* These exceptions are all OK */
123 status = AE_OK;
124 break;
126 default:
128 goto unlock_and_exit;
132 unlock_and_exit:
133 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
134 return_ACPI_STATUS(status);
137 /*******************************************************************************
139 * FUNCTION: acpi_ev_has_default_handler
141 * PARAMETERS: node - Namespace node for the device
142 * space_id - The address space ID
144 * RETURN: TRUE if default handler is installed, FALSE otherwise
146 * DESCRIPTION: Check if the default handler is installed for the requested
147 * space ID.
149 ******************************************************************************/
152 acpi_ev_has_default_handler(struct acpi_namespace_node *node,
153 acpi_adr_space_type space_id)
155 union acpi_operand_object *obj_desc;
156 union acpi_operand_object *handler_obj;
158 /* Must have an existing internal object */
160 obj_desc = acpi_ns_get_attached_object(node);
161 if (obj_desc) {
162 handler_obj = obj_desc->common_notify.handler;
164 /* Walk the linked list of handlers for this object */
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);
174 handler_obj = handler_obj->address_space.next;
178 return (FALSE);
181 /*******************************************************************************
183 * FUNCTION: acpi_ev_install_handler
185 * PARAMETERS: walk_namespace callback
187 * DESCRIPTION: This routine installs an address handler into objects that are
188 * of type Region or Device.
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.
193 * This is because the existing handler is closer in proximity
194 * to any more regions than the one we are trying to install.
196 ******************************************************************************/
198 static acpi_status
199 acpi_ev_install_handler(acpi_handle obj_handle,
200 u32 level, void *context, void **return_value)
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;
208 ACPI_FUNCTION_NAME(ev_install_handler);
210 handler_obj = (union acpi_operand_object *)context;
212 /* Parameter validation */
214 if (!handler_obj) {
215 return (AE_OK);
218 /* Convert and validate the device handle */
220 node = acpi_ns_validate_handle(obj_handle);
221 if (!node) {
222 return (AE_BAD_PARAMETER);
226 * We only care about regions and objects that are allowed to have
227 * address space handlers
229 if ((node->type != ACPI_TYPE_DEVICE) &&
230 (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
231 return (AE_OK);
234 /* Check for an existing internal object */
236 obj_desc = acpi_ns_get_attached_object(node);
237 if (!obj_desc) {
239 /* No object, just exit */
241 return (AE_OK);
244 /* Devices are handled different than regions */
246 if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
248 /* Check if this Device already has a handler for this address space */
250 next_handler_obj =
251 acpi_ev_find_region_handler(handler_obj->address_space.
252 space_id,
253 obj_desc->common_notify.
254 handler);
255 if (next_handler_obj) {
257 /* Found a handler, is it for the same address space? */
259 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
260 "Found handler for region [%s] in device %p(%p) handler %p\n",
261 acpi_ut_get_region_name(handler_obj->
262 address_space.
263 space_id),
264 obj_desc, next_handler_obj,
265 handler_obj));
268 * Since the object we found it on was a device, then it means
269 * that someone has already installed a handler for the branch
270 * of the namespace from this device on. Just bail out telling
271 * the walk routine to not traverse this branch. This preserves
272 * the scoping rule for handlers.
274 return (AE_CTRL_DEPTH);
278 * As long as the device didn't have a handler for this space we
279 * don't care about it. We just ignore it and proceed.
281 return (AE_OK);
284 /* Object is a Region */
286 if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
288 /* This region is for a different address space, just ignore it */
290 return (AE_OK);
294 * Now we have a region and it is for the handler's address space type.
296 * First disconnect region for any previous handler (if any)
298 acpi_ev_detach_region(obj_desc, FALSE);
300 /* Connect the region to the new handler */
302 status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
303 return (status);
306 /*******************************************************************************
308 * FUNCTION: acpi_ev_find_region_handler
310 * PARAMETERS: space_id - The address space ID
311 * handler_obj - Head of the handler object list
313 * RETURN: Matching handler object. NULL if space ID not matched
315 * DESCRIPTION: Search a handler object list for a match on the address
316 * space ID.
318 ******************************************************************************/
320 union acpi_operand_object *acpi_ev_find_region_handler(acpi_adr_space_type
321 space_id,
322 union acpi_operand_object
323 *handler_obj)
326 /* Walk the handler list for this device */
328 while (handler_obj) {
330 /* Same space_id indicates a handler is installed */
332 if (handler_obj->address_space.space_id == space_id) {
333 return (handler_obj);
336 /* Next handler object */
338 handler_obj = handler_obj->address_space.next;
341 return (NULL);
344 /*******************************************************************************
346 * FUNCTION: acpi_ev_install_space_handler
348 * PARAMETERS: node - Namespace node for the device
349 * space_id - The address space ID
350 * handler - Address of the handler
351 * setup - Address of the setup function
352 * context - Value passed to the handler on each access
354 * RETURN: Status
356 * DESCRIPTION: Install a handler for all op_regions of a given space_id.
357 * Assumes namespace is locked
359 ******************************************************************************/
361 acpi_status
362 acpi_ev_install_space_handler(struct acpi_namespace_node *node,
363 acpi_adr_space_type space_id,
364 acpi_adr_space_handler handler,
365 acpi_adr_space_setup setup, void *context)
367 union acpi_operand_object *obj_desc;
368 union acpi_operand_object *handler_obj;
369 acpi_status status = AE_OK;
370 acpi_object_type type;
371 u8 flags = 0;
373 ACPI_FUNCTION_TRACE(ev_install_space_handler);
376 * This registration is valid for only the types below and the root.
377 * The root node is where the default handlers get installed.
379 if ((node->type != ACPI_TYPE_DEVICE) &&
380 (node->type != ACPI_TYPE_PROCESSOR) &&
381 (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
382 status = AE_BAD_PARAMETER;
383 goto unlock_and_exit;
386 if (handler == ACPI_DEFAULT_HANDLER) {
387 flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
389 switch (space_id) {
390 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
392 handler = acpi_ex_system_memory_space_handler;
393 setup = acpi_ev_system_memory_region_setup;
394 break;
396 case ACPI_ADR_SPACE_SYSTEM_IO:
398 handler = acpi_ex_system_io_space_handler;
399 setup = acpi_ev_io_space_region_setup;
400 break;
402 case ACPI_ADR_SPACE_PCI_CONFIG:
404 handler = acpi_ex_pci_config_space_handler;
405 setup = acpi_ev_pci_config_region_setup;
406 break;
408 case ACPI_ADR_SPACE_CMOS:
410 handler = acpi_ex_cmos_space_handler;
411 setup = acpi_ev_cmos_region_setup;
412 break;
414 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
416 handler = acpi_ex_pci_bar_space_handler;
417 setup = acpi_ev_pci_bar_region_setup;
418 break;
420 case ACPI_ADR_SPACE_DATA_TABLE:
422 handler = acpi_ex_data_table_space_handler;
423 setup = NULL;
424 break;
426 default:
428 status = AE_BAD_PARAMETER;
429 goto unlock_and_exit;
433 /* If the caller hasn't specified a setup routine, use the default */
435 if (!setup) {
436 setup = acpi_ev_default_region_setup;
439 /* Check for an existing internal object */
441 obj_desc = acpi_ns_get_attached_object(node);
442 if (obj_desc) {
444 * The attached device object already exists. Now make sure
445 * the handler is not already installed.
447 handler_obj = acpi_ev_find_region_handler(space_id,
448 obj_desc->
449 common_notify.
450 handler);
452 if (handler_obj) {
453 if (handler_obj->address_space.handler == handler) {
455 * It is (relatively) OK to attempt to install the SAME
456 * handler twice. This can easily happen with the
457 * PCI_Config space.
459 status = AE_SAME_HANDLER;
460 goto unlock_and_exit;
461 } else {
462 /* A handler is already installed */
464 status = AE_ALREADY_EXISTS;
467 goto unlock_and_exit;
469 } else {
470 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
471 "Creating object on Device %p while installing handler\n",
472 node));
474 /* obj_desc does not exist, create one */
476 if (node->type == ACPI_TYPE_ANY) {
477 type = ACPI_TYPE_DEVICE;
478 } else {
479 type = node->type;
482 obj_desc = acpi_ut_create_internal_object(type);
483 if (!obj_desc) {
484 status = AE_NO_MEMORY;
485 goto unlock_and_exit;
488 /* Init new descriptor */
490 obj_desc->common.type = (u8)type;
492 /* Attach the new object to the Node */
494 status = acpi_ns_attach_object(node, obj_desc, type);
496 /* Remove local reference to the object */
498 acpi_ut_remove_reference(obj_desc);
500 if (ACPI_FAILURE(status)) {
501 goto unlock_and_exit;
505 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
506 "Installing address handler for region %s(%X) "
507 "on Device %4.4s %p(%p)\n",
508 acpi_ut_get_region_name(space_id), space_id,
509 acpi_ut_get_node_name(node), node, obj_desc));
512 * Install the handler
514 * At this point there is no existing handler. Just allocate the object
515 * for the handler and link it into the list.
517 handler_obj =
518 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
519 if (!handler_obj) {
520 status = AE_NO_MEMORY;
521 goto unlock_and_exit;
524 /* Init handler obj */
526 handler_obj->address_space.space_id = (u8)space_id;
527 handler_obj->address_space.handler_flags = flags;
528 handler_obj->address_space.region_list = NULL;
529 handler_obj->address_space.node = node;
530 handler_obj->address_space.handler = handler;
531 handler_obj->address_space.context = context;
532 handler_obj->address_space.setup = setup;
534 /* Install at head of Device.address_space list */
536 handler_obj->address_space.next = obj_desc->common_notify.handler;
539 * The Device object is the first reference on the handler_obj.
540 * Each region that uses the handler adds a reference.
542 obj_desc->common_notify.handler = handler_obj;
545 * Walk the namespace finding all of the regions this handler will
546 * manage.
548 * Start at the device and search the branch toward the leaf nodes
549 * until either the leaf is encountered or a device is detected that
550 * has an address handler of the same type.
552 * In either case, back up and search down the remainder of the branch
554 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node,
555 ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
556 acpi_ev_install_handler, NULL,
557 handler_obj, NULL);
559 unlock_and_exit:
560 return_ACPI_STATUS(status);