1 /******************************************************************************
3 * Module Name: evxfregn - External Interfaces, ACPI Operation Regions and
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2004, R. Byron Moore
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <acpi/acpi.h>
47 #include <acpi/acnamesp.h>
48 #include <acpi/acevents.h>
49 #include <acpi/acinterp.h>
51 #define _COMPONENT ACPI_EVENTS
52 ACPI_MODULE_NAME ("evxfregn")
55 /*******************************************************************************
57 * FUNCTION: acpi_install_address_space_handler
59 * PARAMETERS: Device - Handle for the device
60 * space_id - The address space ID
61 * Handler - Address of the handler
62 * Setup - Address of the setup function
63 * Context - Value passed to the handler on each access
67 * DESCRIPTION: Install a handler for all op_regions of a given space_id.
69 ******************************************************************************/
72 acpi_install_address_space_handler (
74 acpi_adr_space_type space_id,
75 acpi_adr_space_handler handler,
76 acpi_adr_space_setup setup,
79 union acpi_operand_object *obj_desc;
80 union acpi_operand_object *handler_obj;
81 struct acpi_namespace_node *node;
83 acpi_object_type type;
87 ACPI_FUNCTION_TRACE ("acpi_install_address_space_handler");
90 /* Parameter validation */
93 return_ACPI_STATUS (AE_BAD_PARAMETER);
96 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
97 if (ACPI_FAILURE (status)) {
98 return_ACPI_STATUS (status);
101 /* Convert and validate the device handle */
103 node = acpi_ns_map_handle_to_node (device);
105 status = AE_BAD_PARAMETER;
106 goto unlock_and_exit;
110 * This registration is valid for only the types below
111 * and the root. This is where the default handlers
114 if ((node->type != ACPI_TYPE_DEVICE) &&
115 (node->type != ACPI_TYPE_PROCESSOR) &&
116 (node->type != ACPI_TYPE_THERMAL) &&
117 (node != acpi_gbl_root_node)) {
118 status = AE_BAD_PARAMETER;
119 goto unlock_and_exit;
122 if (handler == ACPI_DEFAULT_HANDLER) {
123 flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
126 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
127 handler = acpi_ex_system_memory_space_handler;
128 setup = acpi_ev_system_memory_region_setup;
131 case ACPI_ADR_SPACE_SYSTEM_IO:
132 handler = acpi_ex_system_io_space_handler;
133 setup = acpi_ev_io_space_region_setup;
136 case ACPI_ADR_SPACE_PCI_CONFIG:
137 handler = acpi_ex_pci_config_space_handler;
138 setup = acpi_ev_pci_config_region_setup;
141 case ACPI_ADR_SPACE_CMOS:
142 handler = acpi_ex_cmos_space_handler;
143 setup = acpi_ev_cmos_region_setup;
146 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
147 handler = acpi_ex_pci_bar_space_handler;
148 setup = acpi_ev_pci_bar_region_setup;
151 case ACPI_ADR_SPACE_DATA_TABLE:
152 handler = acpi_ex_data_table_space_handler;
157 status = AE_BAD_PARAMETER;
158 goto unlock_and_exit;
162 /* If the caller hasn't specified a setup routine, use the default */
165 setup = acpi_ev_default_region_setup;
168 /* Check for an existing internal object */
170 obj_desc = acpi_ns_get_attached_object (node);
173 * The attached device object already exists.
174 * Make sure the handler is not already installed.
176 handler_obj = obj_desc->device.handler;
178 /* Walk the handler list for this device */
180 while (handler_obj) {
181 /* Same space_id indicates a handler already installed */
183 if(handler_obj->address_space.space_id == space_id) {
184 if (handler_obj->address_space.handler == handler) {
186 * It is (relatively) OK to attempt to install the SAME
187 * handler twice. This can easily happen with PCI_Config space.
189 status = AE_SAME_HANDLER;
190 goto unlock_and_exit;
193 /* A handler is already installed */
195 status = AE_ALREADY_EXISTS;
197 goto unlock_and_exit;
200 /* Walk the linked list of handlers */
202 handler_obj = handler_obj->address_space.next;
206 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
207 "Creating object on Device %p while installing handler\n", node));
209 /* obj_desc does not exist, create one */
211 if (node->type == ACPI_TYPE_ANY) {
212 type = ACPI_TYPE_DEVICE;
218 obj_desc = acpi_ut_create_internal_object (type);
220 status = AE_NO_MEMORY;
221 goto unlock_and_exit;
224 /* Init new descriptor */
226 obj_desc->common.type = (u8) type;
228 /* Attach the new object to the Node */
230 status = acpi_ns_attach_object (node, obj_desc, type);
232 /* Remove local reference to the object */
234 acpi_ut_remove_reference (obj_desc);
236 if (ACPI_FAILURE (status)) {
237 goto unlock_and_exit;
241 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
242 "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
243 acpi_ut_get_region_name (space_id), space_id,
244 acpi_ut_get_node_name (node), node, obj_desc));
247 * Install the handler
249 * At this point there is no existing handler.
250 * Just allocate the object for the handler and link it
253 handler_obj = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
255 status = AE_NO_MEMORY;
256 goto unlock_and_exit;
259 /* Init handler obj */
261 handler_obj->address_space.space_id = (u8) space_id;
262 handler_obj->address_space.hflags = flags;
263 handler_obj->address_space.region_list = NULL;
264 handler_obj->address_space.node = node;
265 handler_obj->address_space.handler = handler;
266 handler_obj->address_space.context = context;
267 handler_obj->address_space.setup = setup;
269 /* Install at head of Device.address_space list */
271 handler_obj->address_space.next = obj_desc->device.handler;
274 * The Device object is the first reference on the handler_obj.
275 * Each region that uses the handler adds a reference.
277 obj_desc->device.handler = handler_obj;
280 * Walk the namespace finding all of the regions this
281 * handler will manage.
283 * Start at the device and search the branch toward
284 * the leaf nodes until either the leaf is encountered or
285 * a device is detected that has an address handler of the
288 * In either case, back up and search down the remainder
291 status = acpi_ns_walk_namespace (ACPI_TYPE_ANY, device, ACPI_UINT32_MAX,
292 ACPI_NS_WALK_UNLOCK, acpi_ev_install_handler,
296 * Now we can run the _REG methods for all Regions for this
297 * space ID. This is a separate walk in order to handle any
298 * interdependencies between regions and _REG methods. (i.e. handlers
299 * must be installed for all regions of this Space ID before we
300 * can run any _REG methods.
302 status = acpi_ns_walk_namespace (ACPI_TYPE_ANY, device, ACPI_UINT32_MAX,
303 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
307 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
308 return_ACPI_STATUS (status);
312 /*******************************************************************************
314 * FUNCTION: acpi_remove_address_space_handler
316 * PARAMETERS: Device - Handle for the device
317 * space_id - The address space ID
318 * Handler - Address of the handler
322 * DESCRIPTION: Remove a previously installed handler.
324 ******************************************************************************/
327 acpi_remove_address_space_handler (
329 acpi_adr_space_type space_id,
330 acpi_adr_space_handler handler)
332 union acpi_operand_object *obj_desc;
333 union acpi_operand_object *handler_obj;
334 union acpi_operand_object *region_obj;
335 union acpi_operand_object **last_obj_ptr;
336 struct acpi_namespace_node *node;
340 ACPI_FUNCTION_TRACE ("acpi_remove_address_space_handler");
343 /* Parameter validation */
346 return_ACPI_STATUS (AE_BAD_PARAMETER);
349 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
350 if (ACPI_FAILURE (status)) {
351 return_ACPI_STATUS (status);
354 /* Convert and validate the device handle */
356 node = acpi_ns_map_handle_to_node (device);
358 status = AE_BAD_PARAMETER;
359 goto unlock_and_exit;
362 /* Make sure the internal object exists */
364 obj_desc = acpi_ns_get_attached_object (node);
366 status = AE_NOT_EXIST;
367 goto unlock_and_exit;
370 /* Find the address handler the user requested */
372 handler_obj = obj_desc->device.handler;
373 last_obj_ptr = &obj_desc->device.handler;
374 while (handler_obj) {
375 /* We have a handler, see if user requested this one */
377 if (handler_obj->address_space.space_id == space_id) {
378 /* Matched space_id, first dereference this in the Regions */
380 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
381 "Removing address handler %p(%p) for region %s on Device %p(%p)\n",
382 handler_obj, handler, acpi_ut_get_region_name (space_id),
385 region_obj = handler_obj->address_space.region_list;
387 /* Walk the handler's region list */
391 * First disassociate the handler from the region.
393 * NOTE: this doesn't mean that the region goes away
394 * The region is just inaccessible as indicated to
397 acpi_ev_detach_region (region_obj, TRUE);
400 * Walk the list: Just grab the head because the
401 * detach_region removed the previous head.
403 region_obj = handler_obj->address_space.region_list;
407 /* Remove this Handler object from the list */
409 *last_obj_ptr = handler_obj->address_space.next;
411 /* Now we can delete the handler object */
413 acpi_ut_remove_reference (handler_obj);
414 goto unlock_and_exit;
417 /* Walk the linked list of handlers */
419 last_obj_ptr = &handler_obj->address_space.next;
420 handler_obj = handler_obj->address_space.next;
423 /* The handler does not exist */
425 ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
426 "Unable to remove address handler %p for %s(%X), dev_node %p, obj %p\n",
427 handler, acpi_ut_get_region_name (space_id), space_id, node, obj_desc));
429 status = AE_NOT_EXIST;
432 (void) acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
433 return_ACPI_STATUS (status);