blob: 582b0af01e9955dc2c163463a1ab4648ac7d916f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: evregion - ACPI address_space (op_region) handler dispatch
4 *
5 *****************************************************************************/
6
7/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acevents.h"
47#include "acnamesp.h"
48#include "acinterp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define _COMPONENT ACPI_EVENTS
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("evregion")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Robert Moore44f6c012005-04-18 22:49:35 -040053/* Local prototypes */
Lin Ming74d3ec772009-10-13 10:29:30 +080054static u8
55acpi_ev_has_default_handler(struct acpi_namespace_node *node,
56 acpi_adr_space_type space_id);
57
Robert Moore44f6c012005-04-18 22:49:35 -040058static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040059acpi_ev_reg_run(acpi_handle obj_handle,
60 u32 level, void *context, void **return_value);
Robert Moore44f6c012005-04-18 22:49:35 -040061
62static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040063acpi_ev_install_handler(acpi_handle obj_handle,
64 u32 level, void *context, void **return_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Bob Moore9f15fc62008-11-12 16:01:56 +080066/* These are the address spaces that will get default handlers */
67
68#define ACPI_NUM_DEFAULT_SPACES 4
69
70static u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
71 ACPI_ADR_SPACE_SYSTEM_MEMORY,
72 ACPI_ADR_SPACE_SYSTEM_IO,
73 ACPI_ADR_SPACE_PCI_CONFIG,
74 ACPI_ADR_SPACE_DATA_TABLE
75};
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*******************************************************************************
78 *
79 * FUNCTION: acpi_ev_install_region_handlers
80 *
81 * PARAMETERS: None
82 *
83 * RETURN: Status
84 *
85 * DESCRIPTION: Installs the core subsystem default address space handlers.
86 *
87 ******************************************************************************/
88
Len Brown4be44fc2005-08-05 00:44:28 -040089acpi_status acpi_ev_install_region_handlers(void)
90{
91 acpi_status status;
Bob Moore67a119f2008-06-10 13:42:13 +080092 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Bob Mooreb229cf92006-04-21 17:15:00 -040094 ACPI_FUNCTION_TRACE(ev_install_region_handlers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Len Brown4be44fc2005-08-05 00:44:28 -040096 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
97 if (ACPI_FAILURE(status)) {
98 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
101 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800102 * All address spaces (PCI Config, EC, SMBus) are scope dependent and
103 * registration must occur for a specific device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800105 * In the case of the system memory and IO address spaces there is
106 * currently no device associated with the address space. For these we
107 * use the root.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800109 * We install the default PCI config space handler at the root so that
110 * this space is immediately available even though the we have not
111 * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
112 * specification which states that the PCI config space must be always
113 * available -- even though we are nowhere near ready to find the PCI root
114 * buses at this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 *
116 * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
117 * has already been installed (via acpi_install_address_space_handler).
118 * Similar for AE_SAME_HANDLER.
119 */
120 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400121 status = acpi_ev_install_space_handler(acpi_gbl_root_node,
122 acpi_gbl_default_address_spaces
123 [i],
124 ACPI_DEFAULT_HANDLER,
125 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 switch (status) {
127 case AE_OK:
128 case AE_SAME_HANDLER:
129 case AE_ALREADY_EXISTS:
130
131 /* These exceptions are all OK */
132
133 status = AE_OK;
134 break;
135
136 default:
137
138 goto unlock_and_exit;
139 }
140 }
141
Len Brown4be44fc2005-08-05 00:44:28 -0400142 unlock_and_exit:
143 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
144 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*******************************************************************************
148 *
Lin Ming74d3ec772009-10-13 10:29:30 +0800149 * FUNCTION: acpi_ev_has_default_handler
150 *
151 * PARAMETERS: Node - Namespace node for the device
152 * space_id - The address space ID
153 *
154 * RETURN: TRUE if default handler is installed, FALSE otherwise
155 *
156 * DESCRIPTION: Check if the default handler is installed for the requested
157 * space ID.
158 *
159 ******************************************************************************/
160
161static u8
162acpi_ev_has_default_handler(struct acpi_namespace_node *node,
163 acpi_adr_space_type space_id)
164{
165 union acpi_operand_object *obj_desc;
166 union acpi_operand_object *handler_obj;
167
168 /* Must have an existing internal object */
169
170 obj_desc = acpi_ns_get_attached_object(node);
171 if (obj_desc) {
172 handler_obj = obj_desc->device.handler;
173
174 /* Walk the linked list of handlers for this object */
175
176 while (handler_obj) {
177 if (handler_obj->address_space.space_id == space_id) {
178 if (handler_obj->address_space.handler_flags &
179 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
180 return (TRUE);
181 }
182 }
183
184 handler_obj = handler_obj->address_space.next;
185 }
186 }
187
188 return (FALSE);
189}
190
191/*******************************************************************************
192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * FUNCTION: acpi_ev_initialize_op_regions
194 *
195 * PARAMETERS: None
196 *
197 * RETURN: Status
198 *
199 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
200 * an installed default region handler.
201 *
202 ******************************************************************************/
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204acpi_status acpi_ev_initialize_op_regions(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Len Brown4be44fc2005-08-05 00:44:28 -0400206 acpi_status status;
Bob Moore67a119f2008-06-10 13:42:13 +0800207 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Bob Mooreb229cf92006-04-21 17:15:00 -0400209 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Len Brown4be44fc2005-08-05 00:44:28 -0400211 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
212 if (ACPI_FAILURE(status)) {
213 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Bob Moore9f15fc62008-11-12 16:01:56 +0800216 /* Run the _REG methods for op_regions in each default address space */
Bob Moore52fc0b02006-10-02 00:00:00 -0400217
Bob Moore9f15fc62008-11-12 16:01:56 +0800218 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
219 /*
Lin Ming74d3ec772009-10-13 10:29:30 +0800220 * Make sure the installed handler is the DEFAULT handler. If not the
221 * default, the _REG methods will have already been run (when the
222 * handler was installed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Lin Ming74d3ec772009-10-13 10:29:30 +0800224 if (acpi_ev_has_default_handler(acpi_gbl_root_node,
225 acpi_gbl_default_address_spaces
226 [i])) {
227 status =
228 acpi_ev_execute_reg_methods(acpi_gbl_root_node,
229 acpi_gbl_default_address_spaces
230 [i]);
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
235 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*******************************************************************************
239 *
240 * FUNCTION: acpi_ev_execute_reg_method
241 *
Robert Moore44f6c012005-04-18 22:49:35 -0400242 * PARAMETERS: region_obj - Region object
243 * Function - Passed to _REG: On (1) or Off (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
245 * RETURN: Status
246 *
247 * DESCRIPTION: Execute _REG method for a region
248 *
249 ******************************************************************************/
250
251acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400252acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Bob Moore41195322006-05-26 16:36:00 -0400254 struct acpi_evaluate_info *info;
255 union acpi_operand_object *args[3];
Len Brown4be44fc2005-08-05 00:44:28 -0400256 union acpi_operand_object *region_obj2;
257 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Bob Mooreb229cf92006-04-21 17:15:00 -0400259 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Len Brown4be44fc2005-08-05 00:44:28 -0400261 region_obj2 = acpi_ns_get_secondary_object(region_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!region_obj2) {
Len Brown4be44fc2005-08-05 00:44:28 -0400263 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 if (region_obj2->extra.method_REG == NULL) {
Len Brown4be44fc2005-08-05 00:44:28 -0400267 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
Bob Moore41195322006-05-26 16:36:00 -0400270 /* Allocate and initialize the evaluation information block */
271
272 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
273 if (!info) {
Len Brown4be44fc2005-08-05 00:44:28 -0400274 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
Bob Moore41195322006-05-26 16:36:00 -0400277 info->prefix_node = region_obj2->extra.method_REG;
278 info->pathname = NULL;
279 info->parameters = args;
Bob Moore41195322006-05-26 16:36:00 -0400280 info->flags = ACPI_IGNORE_RETURN_VALUE;
281
282 /*
283 * The _REG method has two arguments:
284 *
285 * Arg0 - Integer:
286 * Operation region space ID Same value as region_obj->Region.space_id
287 *
288 * Arg1 - Integer:
289 * connection status 1 for connecting the handler, 0 for disconnecting
290 * the handler (Passed as a parameter)
291 */
292 args[0] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
293 if (!args[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 status = AE_NO_MEMORY;
Bob Moore41195322006-05-26 16:36:00 -0400295 goto cleanup1;
296 }
297
298 args[1] = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
299 if (!args[1]) {
300 status = AE_NO_MEMORY;
301 goto cleanup2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
304 /* Setup the parameter objects */
305
Bob Moore41195322006-05-26 16:36:00 -0400306 args[0]->integer.value = region_obj->region.space_id;
307 args[1]->integer.value = function;
308 args[2] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Execute the method, no return value */
311
Len Brown4be44fc2005-08-05 00:44:28 -0400312 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
Bob Moore41195322006-05-26 16:36:00 -0400313 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Bob Moore41195322006-05-26 16:36:00 -0400315 status = acpi_ns_evaluate(info);
316 acpi_ut_remove_reference(args[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Bob Moore41195322006-05-26 16:36:00 -0400318 cleanup2:
319 acpi_ut_remove_reference(args[0]);
320
321 cleanup1:
322 ACPI_FREE(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400323 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*******************************************************************************
327 *
328 * FUNCTION: acpi_ev_address_space_dispatch
329 *
330 * PARAMETERS: region_obj - Internal region object
331 * Function - Read or Write operation
Bob Mooref5407af2009-05-21 10:56:52 +0800332 * region_offset - Where in the region to read or write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * bit_width - Field width in bits (8, 16, 32, or 64)
Bob Moore958dd242006-05-12 17:12:00 -0400334 * Value - Pointer to in or out value, must be
335 * full 64-bit acpi_integer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
337 * RETURN: Status
338 *
339 * DESCRIPTION: Dispatch an address space or operation region access to
340 * a previously installed handler.
341 *
342 ******************************************************************************/
343
344acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400345acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
346 u32 function,
Bob Mooref5407af2009-05-21 10:56:52 +0800347 u32 region_offset,
Bob Moore958dd242006-05-12 17:12:00 -0400348 u32 bit_width, acpi_integer * value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Len Brown4be44fc2005-08-05 00:44:28 -0400350 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400351 acpi_adr_space_handler handler;
352 acpi_adr_space_setup region_setup;
353 union acpi_operand_object *handler_desc;
354 union acpi_operand_object *region_obj2;
355 void *region_context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Bob Mooreb229cf92006-04-21 17:15:00 -0400357 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Len Brown4be44fc2005-08-05 00:44:28 -0400359 region_obj2 = acpi_ns_get_secondary_object(region_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!region_obj2) {
Len Brown4be44fc2005-08-05 00:44:28 -0400361 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 /* Ensure that there is a handler associated with this region */
365
366 handler_desc = region_obj->region.handler;
367 if (!handler_desc) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500368 ACPI_ERROR((AE_INFO,
369 "No handler for Region [%4.4s] (%p) [%s]",
370 acpi_ut_get_node_name(region_obj->region.node),
371 region_obj,
372 acpi_ut_get_region_name(region_obj->region.
373 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Len Brown4be44fc2005-08-05 00:44:28 -0400375 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800379 * It may be the case that the region has never been initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * Some types of regions require special init code
381 */
382 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
Bob Moore9f15fc62008-11-12 16:01:56 +0800383
384 /* This region has not been initialized yet, do it */
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 region_setup = handler_desc->address_space.setup;
387 if (!region_setup) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* No initialization routine, exit with error */
390
Bob Mooreb8e4d892006-01-27 16:43:00 -0500391 ACPI_ERROR((AE_INFO,
392 "No init routine for region(%p) [%s]",
393 region_obj,
394 acpi_ut_get_region_name(region_obj->region.
395 space_id)));
Len Brown4be44fc2005-08-05 00:44:28 -0400396 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800400 * We must exit the interpreter because the region setup will
401 * potentially execute control methods (for example, the _REG method
402 * for this region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
Bob Moore014d4332008-01-10 23:04:10 -0500404 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Len Brown4be44fc2005-08-05 00:44:28 -0400406 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
407 handler_desc->address_space.context,
408 &region_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Re-enter the interpreter */
411
Bob Moore014d4332008-01-10 23:04:10 -0500412 acpi_ex_enter_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* Check for failure of the Region Setup */
415
Len Brown4be44fc2005-08-05 00:44:28 -0400416 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500417 ACPI_EXCEPTION((AE_INFO, status,
418 "During region initialization: [%s]",
419 acpi_ut_get_region_name(region_obj->
420 region.
421 space_id)));
Len Brown4be44fc2005-08-05 00:44:28 -0400422 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Bob Moore9f15fc62008-11-12 16:01:56 +0800425 /* Region initialization may have been completed by region_setup */
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
428 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
429
430 if (region_obj2->extra.region_context) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* The handler for this region was already installed */
433
Bob Moore83135242006-10-03 00:00:00 -0400434 ACPI_FREE(region_context);
Len Brown4be44fc2005-08-05 00:44:28 -0400435 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
437 * Save the returned context for use in all accesses to
438 * this particular region
439 */
Len Brown4be44fc2005-08-05 00:44:28 -0400440 region_obj2->extra.region_context =
441 region_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
444 }
445
446 /* We have everything we need, we can invoke the address space handler */
447
448 handler = handler_desc->address_space.handler;
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
451 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
452 &region_obj->region.handler->address_space, handler,
Bob Mooref5407af2009-05-21 10:56:52 +0800453 ACPI_FORMAT_NATIVE_UINT(region_obj->region.address +
454 region_offset),
Len Brown4be44fc2005-08-05 00:44:28 -0400455 acpi_ut_get_region_name(region_obj->region.
456 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Bob Moore616861242006-03-17 16:44:00 -0500458 if (!(handler_desc->address_space.handler_flags &
459 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /*
461 * For handlers other than the default (supplied) handlers, we must
462 * exit the interpreter because the handler *might* block -- we don't
463 * know what it will do, so we can't hold the lock on the intepreter.
464 */
Bob Moore014d4332008-01-10 23:04:10 -0500465 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 /* Call the handler */
469
Bob Mooref5407af2009-05-21 10:56:52 +0800470 status = handler(function,
471 (region_obj->region.address + region_offset),
472 bit_width, value, handler_desc->address_space.context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 region_obj2->extra.region_context);
474
Len Brown4be44fc2005-08-05 00:44:28 -0400475 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500476 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
477 acpi_ut_get_region_name(region_obj->region.
478 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
Bob Moore616861242006-03-17 16:44:00 -0500481 if (!(handler_desc->address_space.handler_flags &
482 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
484 * We just returned from a non-default handler, we must re-enter the
485 * interpreter
486 */
Bob Moore014d4332008-01-10 23:04:10 -0500487 acpi_ex_enter_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Len Brown4be44fc2005-08-05 00:44:28 -0400490 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*******************************************************************************
494 *
495 * FUNCTION: acpi_ev_detach_region
496 *
497 * PARAMETERS: region_obj - Region Object
498 * acpi_ns_is_locked - Namespace Region Already Locked?
499 *
500 * RETURN: None
501 *
502 * DESCRIPTION: Break the association between the handler and the region
503 * this is a two way association.
504 *
505 ******************************************************************************/
506
507void
Len Brown4be44fc2005-08-05 00:44:28 -0400508acpi_ev_detach_region(union acpi_operand_object *region_obj,
509 u8 acpi_ns_is_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Len Brown4be44fc2005-08-05 00:44:28 -0400511 union acpi_operand_object *handler_obj;
512 union acpi_operand_object *obj_desc;
513 union acpi_operand_object **last_obj_ptr;
514 acpi_adr_space_setup region_setup;
515 void **region_context;
516 union acpi_operand_object *region_obj2;
517 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Bob Mooreb229cf92006-04-21 17:15:00 -0400519 ACPI_FUNCTION_TRACE(ev_detach_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Len Brown4be44fc2005-08-05 00:44:28 -0400521 region_obj2 = acpi_ns_get_secondary_object(region_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!region_obj2) {
523 return_VOID;
524 }
525 region_context = &region_obj2->extra.region_context;
526
527 /* Get the address handler from the region object */
528
529 handler_obj = region_obj->region.handler;
530 if (!handler_obj) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* This region has no handler, all done */
533
534 return_VOID;
535 }
536
537 /* Find this region in the handler's list */
538
539 obj_desc = handler_obj->address_space.region_list;
540 last_obj_ptr = &handler_obj->address_space.region_list;
541
542 while (obj_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Is this the correct Region? */
545
546 if (obj_desc == region_obj) {
Len Brown4be44fc2005-08-05 00:44:28 -0400547 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
548 "Removing Region %p from address handler %p\n",
549 region_obj, handler_obj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* This is it, remove it from the handler's list */
552
553 *last_obj_ptr = obj_desc->region.next;
Len Brown4be44fc2005-08-05 00:44:28 -0400554 obj_desc->region.next = NULL; /* Must clear field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (acpi_ns_is_locked) {
Len Brown4be44fc2005-08-05 00:44:28 -0400557 status =
558 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
559 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return_VOID;
561 }
562 }
563
564 /* Now stop region accesses by executing the _REG method */
565
Len Brown4be44fc2005-08-05 00:44:28 -0400566 status = acpi_ev_execute_reg_method(region_obj, 0);
567 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500568 ACPI_EXCEPTION((AE_INFO, status,
569 "from region _REG, [%s]",
570 acpi_ut_get_region_name
571 (region_obj->region.space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 if (acpi_ns_is_locked) {
Len Brown4be44fc2005-08-05 00:44:28 -0400575 status =
576 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
577 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return_VOID;
579 }
580 }
581
Bob Mooref6dd9222006-07-07 20:44:38 -0400582 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800583 * If the region has been activated, call the setup handler with
584 * the deactivate notification
Bob Mooref6dd9222006-07-07 20:44:38 -0400585 */
586 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
587 region_setup = handler_obj->address_space.setup;
588 status =
589 region_setup(region_obj,
590 ACPI_REGION_DEACTIVATE,
591 handler_obj->address_space.
592 context, region_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Bob Mooref6dd9222006-07-07 20:44:38 -0400594 /* Init routine may fail, Just ignore errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Bob Mooref6dd9222006-07-07 20:44:38 -0400596 if (ACPI_FAILURE(status)) {
597 ACPI_EXCEPTION((AE_INFO, status,
598 "from region handler - deactivate, [%s]",
599 acpi_ut_get_region_name
600 (region_obj->region.
601 space_id)));
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Bob Mooref6dd9222006-07-07 20:44:38 -0400604 region_obj->region.flags &=
605 ~(AOPOBJ_SETUP_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Remove handler reference in the region
610 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400611 * NOTE: this doesn't mean that the region goes away, the region
612 * is just inaccessible as indicated to the _REG method
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400614 * If the region is on the handler's list, this must be the
615 * region's handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
617 region_obj->region.handler = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400618 acpi_ut_remove_reference(handler_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 return_VOID;
621 }
622
623 /* Walk the linked list of handlers */
624
625 last_obj_ptr = &obj_desc->region.next;
626 obj_desc = obj_desc->region.next;
627 }
628
629 /* If we get here, the region was not in the handler's region list */
630
Len Brown4be44fc2005-08-05 00:44:28 -0400631 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
632 "Cannot remove region %p from address handler %p\n",
633 region_obj, handler_obj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 return_VOID;
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/*******************************************************************************
639 *
640 * FUNCTION: acpi_ev_attach_region
641 *
642 * PARAMETERS: handler_obj - Handler Object
643 * region_obj - Region Object
644 * acpi_ns_is_locked - Namespace Region Already Locked?
645 *
646 * RETURN: None
647 *
648 * DESCRIPTION: Create the association between the handler and the region
649 * this is a two way association.
650 *
651 ******************************************************************************/
652
653acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400654acpi_ev_attach_region(union acpi_operand_object *handler_obj,
655 union acpi_operand_object *region_obj,
656 u8 acpi_ns_is_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658
Bob Mooreb229cf92006-04-21 17:15:00 -0400659 ACPI_FUNCTION_TRACE(ev_attach_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Len Brown4be44fc2005-08-05 00:44:28 -0400661 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
662 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
663 acpi_ut_get_node_name(region_obj->region.node),
664 region_obj, handler_obj,
665 acpi_ut_get_region_name(region_obj->region.
666 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* Link this region to the front of the handler's list */
669
670 region_obj->region.next = handler_obj->address_space.region_list;
671 handler_obj->address_space.region_list = region_obj;
672
673 /* Install the region's handler */
674
675 if (region_obj->region.handler) {
Len Brown4be44fc2005-08-05 00:44:28 -0400676 return_ACPI_STATUS(AE_ALREADY_EXISTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 region_obj->region.handler = handler_obj;
Len Brown4be44fc2005-08-05 00:44:28 -0400680 acpi_ut_add_reference(handler_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Len Brown4be44fc2005-08-05 00:44:28 -0400682 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*******************************************************************************
686 *
687 * FUNCTION: acpi_ev_install_handler
688 *
689 * PARAMETERS: walk_namespace callback
690 *
691 * DESCRIPTION: This routine installs an address handler into objects that are
692 * of type Region or Device.
693 *
694 * If the Object is a Device, and the device has a handler of
695 * the same type then the search is terminated in that branch.
696 *
697 * This is because the existing handler is closer in proximity
698 * to any more regions than the one we are trying to install.
699 *
700 ******************************************************************************/
701
Robert Moore44f6c012005-04-18 22:49:35 -0400702static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400703acpi_ev_install_handler(acpi_handle obj_handle,
704 u32 level, void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Len Brown4be44fc2005-08-05 00:44:28 -0400706 union acpi_operand_object *handler_obj;
707 union acpi_operand_object *next_handler_obj;
708 union acpi_operand_object *obj_desc;
709 struct acpi_namespace_node *node;
710 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Bob Mooreb229cf92006-04-21 17:15:00 -0400712 ACPI_FUNCTION_NAME(ev_install_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Len Brown4be44fc2005-08-05 00:44:28 -0400714 handler_obj = (union acpi_operand_object *)context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Parameter validation */
717
718 if (!handler_obj) {
719 return (AE_OK);
720 }
721
722 /* Convert and validate the device handle */
723
Len Brown4be44fc2005-08-05 00:44:28 -0400724 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!node) {
726 return (AE_BAD_PARAMETER);
727 }
728
729 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800730 * We only care about regions and objects that are allowed to have
731 * address space handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733 if ((node->type != ACPI_TYPE_DEVICE) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400734 (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return (AE_OK);
736 }
737
738 /* Check for an existing internal object */
739
Len Brown4be44fc2005-08-05 00:44:28 -0400740 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!obj_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* No object, just exit */
744
745 return (AE_OK);
746 }
747
748 /* Devices are handled different than regions */
749
Bob Moore3371c192009-02-18 14:44:03 +0800750 if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* Check if this Device already has a handler for this address space */
753
754 next_handler_obj = obj_desc->device.handler;
755 while (next_handler_obj) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* Found a handler, is it for the same address space? */
758
Len Brown4be44fc2005-08-05 00:44:28 -0400759 if (next_handler_obj->address_space.space_id ==
760 handler_obj->address_space.space_id) {
761 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
Bob Moored4913dc2009-03-06 10:05:18 +0800762 "Found handler for region [%s] in device %p(%p) "
763 "handler %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400764 acpi_ut_get_region_name
765 (handler_obj->address_space.
766 space_id), obj_desc,
767 next_handler_obj,
768 handler_obj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /*
771 * Since the object we found it on was a device, then it
772 * means that someone has already installed a handler for
Bob Moore9f15fc62008-11-12 16:01:56 +0800773 * the branch of the namespace from this device on. Just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * bail out telling the walk routine to not traverse this
Bob Moore9f15fc62008-11-12 16:01:56 +0800775 * branch. This preserves the scoping rule for handlers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
777 return (AE_CTRL_DEPTH);
778 }
779
780 /* Walk the linked list of handlers attached to this device */
781
782 next_handler_obj = next_handler_obj->address_space.next;
783 }
784
785 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800786 * As long as the device didn't have a handler for this space we
787 * don't care about it. We just ignore it and proceed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
789 return (AE_OK);
790 }
791
792 /* Object is a Region */
793
794 if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
Bob Moore9f15fc62008-11-12 16:01:56 +0800795
796 /* This region is for a different address space, just ignore it */
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return (AE_OK);
799 }
800
801 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800802 * Now we have a region and it is for the handler's address space type.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 *
804 * First disconnect region for any previous handler (if any)
805 */
Len Brown4be44fc2005-08-05 00:44:28 -0400806 acpi_ev_detach_region(obj_desc, FALSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* Connect the region to the new handler */
809
Len Brown4be44fc2005-08-05 00:44:28 -0400810 status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return (status);
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/*******************************************************************************
815 *
816 * FUNCTION: acpi_ev_install_space_handler
817 *
818 * PARAMETERS: Node - Namespace node for the device
819 * space_id - The address space ID
820 * Handler - Address of the handler
821 * Setup - Address of the setup function
822 * Context - Value passed to the handler on each access
823 *
824 * RETURN: Status
825 *
826 * DESCRIPTION: Install a handler for all op_regions of a given space_id.
827 * Assumes namespace is locked
828 *
829 ******************************************************************************/
830
831acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400832acpi_ev_install_space_handler(struct acpi_namespace_node * node,
833 acpi_adr_space_type space_id,
834 acpi_adr_space_handler handler,
835 acpi_adr_space_setup setup, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Len Brown4be44fc2005-08-05 00:44:28 -0400837 union acpi_operand_object *obj_desc;
838 union acpi_operand_object *handler_obj;
839 acpi_status status;
840 acpi_object_type type;
Bob Moore616861242006-03-17 16:44:00 -0500841 u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Bob Mooreb229cf92006-04-21 17:15:00 -0400843 ACPI_FUNCTION_TRACE(ev_install_space_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800846 * This registration is valid for only the types below and the root. This
847 * is where the default handlers get placed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
Len Brown4be44fc2005-08-05 00:44:28 -0400849 if ((node->type != ACPI_TYPE_DEVICE) &&
850 (node->type != ACPI_TYPE_PROCESSOR) &&
851 (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 status = AE_BAD_PARAMETER;
853 goto unlock_and_exit;
854 }
855
856 if (handler == ACPI_DEFAULT_HANDLER) {
857 flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
858
859 switch (space_id) {
860 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
861 handler = acpi_ex_system_memory_space_handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400862 setup = acpi_ev_system_memory_region_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
864
865 case ACPI_ADR_SPACE_SYSTEM_IO:
866 handler = acpi_ex_system_io_space_handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400867 setup = acpi_ev_io_space_region_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 break;
869
870 case ACPI_ADR_SPACE_PCI_CONFIG:
871 handler = acpi_ex_pci_config_space_handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400872 setup = acpi_ev_pci_config_region_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 break;
874
875 case ACPI_ADR_SPACE_CMOS:
876 handler = acpi_ex_cmos_space_handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400877 setup = acpi_ev_cmos_region_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 break;
879
880 case ACPI_ADR_SPACE_PCI_BAR_TARGET:
881 handler = acpi_ex_pci_bar_space_handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400882 setup = acpi_ev_pci_bar_region_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884
885 case ACPI_ADR_SPACE_DATA_TABLE:
886 handler = acpi_ex_data_table_space_handler;
Len Brown4be44fc2005-08-05 00:44:28 -0400887 setup = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
889
890 default:
891 status = AE_BAD_PARAMETER;
892 goto unlock_and_exit;
893 }
894 }
895
896 /* If the caller hasn't specified a setup routine, use the default */
897
898 if (!setup) {
899 setup = acpi_ev_default_region_setup;
900 }
901
902 /* Check for an existing internal object */
903
Len Brown4be44fc2005-08-05 00:44:28 -0400904 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (obj_desc) {
906 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800907 * The attached device object already exists. Make sure the handler
908 * is not already installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
910 handler_obj = obj_desc->device.handler;
911
912 /* Walk the handler list for this device */
913
914 while (handler_obj) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* Same space_id indicates a handler already installed */
917
918 if (handler_obj->address_space.space_id == space_id) {
Len Brown4be44fc2005-08-05 00:44:28 -0400919 if (handler_obj->address_space.handler ==
920 handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /*
922 * It is (relatively) OK to attempt to install the SAME
Bob Moore9f15fc62008-11-12 16:01:56 +0800923 * handler twice. This can easily happen with the
924 * PCI_Config space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
926 status = AE_SAME_HANDLER;
927 goto unlock_and_exit;
Len Brown4be44fc2005-08-05 00:44:28 -0400928 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* A handler is already installed */
930
931 status = AE_ALREADY_EXISTS;
932 }
933 goto unlock_and_exit;
934 }
935
936 /* Walk the linked list of handlers */
937
938 handler_obj = handler_obj->address_space.next;
939 }
Len Brown4be44fc2005-08-05 00:44:28 -0400940 } else {
941 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
942 "Creating object on Device %p while installing handler\n",
943 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* obj_desc does not exist, create one */
946
947 if (node->type == ACPI_TYPE_ANY) {
948 type = ACPI_TYPE_DEVICE;
Len Brown4be44fc2005-08-05 00:44:28 -0400949 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 type = node->type;
951 }
952
Len Brown4be44fc2005-08-05 00:44:28 -0400953 obj_desc = acpi_ut_create_internal_object(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!obj_desc) {
955 status = AE_NO_MEMORY;
956 goto unlock_and_exit;
957 }
958
959 /* Init new descriptor */
960
961 obj_desc->common.type = (u8) type;
962
963 /* Attach the new object to the Node */
964
Len Brown4be44fc2005-08-05 00:44:28 -0400965 status = acpi_ns_attach_object(node, obj_desc, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* Remove local reference to the object */
968
Len Brown4be44fc2005-08-05 00:44:28 -0400969 acpi_ut_remove_reference(obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Len Brown4be44fc2005-08-05 00:44:28 -0400971 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 goto unlock_and_exit;
973 }
974 }
975
Len Brown4be44fc2005-08-05 00:44:28 -0400976 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
977 "Installing address handler for region %s(%X) on Device %4.4s %p(%p)\n",
978 acpi_ut_get_region_name(space_id), space_id,
979 acpi_ut_get_node_name(node), node, obj_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /*
982 * Install the handler
983 *
Bob Moore9f15fc62008-11-12 16:01:56 +0800984 * At this point there is no existing handler. Just allocate the object
985 * for the handler and link it into the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
Len Brown4be44fc2005-08-05 00:44:28 -0400987 handler_obj =
988 acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (!handler_obj) {
990 status = AE_NO_MEMORY;
991 goto unlock_and_exit;
992 }
993
994 /* Init handler obj */
995
Len Brown4be44fc2005-08-05 00:44:28 -0400996 handler_obj->address_space.space_id = (u8) space_id;
Bob Moore616861242006-03-17 16:44:00 -0500997 handler_obj->address_space.handler_flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 handler_obj->address_space.region_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400999 handler_obj->address_space.node = node;
1000 handler_obj->address_space.handler = handler;
1001 handler_obj->address_space.context = context;
1002 handler_obj->address_space.setup = setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /* Install at head of Device.address_space list */
1005
Len Brown4be44fc2005-08-05 00:44:28 -04001006 handler_obj->address_space.next = obj_desc->device.handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /*
1009 * The Device object is the first reference on the handler_obj.
1010 * Each region that uses the handler adds a reference.
1011 */
1012 obj_desc->device.handler = handler_obj;
1013
1014 /*
1015 * Walk the namespace finding all of the regions this
1016 * handler will manage.
1017 *
1018 * Start at the device and search the branch toward
1019 * the leaf nodes until either the leaf is encountered or
1020 * a device is detected that has an address handler of the
1021 * same type.
1022 *
1023 * In either case, back up and search down the remainder
1024 * of the branch
1025 */
Len Brown4be44fc2005-08-05 00:44:28 -04001026 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
1027 ACPI_NS_WALK_UNLOCK,
Lin Ming22635762009-11-13 10:06:08 +08001028 acpi_ev_install_handler, NULL,
1029 handler_obj, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Len Brown4be44fc2005-08-05 00:44:28 -04001031 unlock_and_exit:
1032 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/*******************************************************************************
1036 *
1037 * FUNCTION: acpi_ev_execute_reg_methods
1038 *
1039 * PARAMETERS: Node - Namespace node for the device
1040 * space_id - The address space ID
1041 *
1042 * RETURN: Status
1043 *
1044 * DESCRIPTION: Run all _REG methods for the input Space ID;
1045 * Note: assumes namespace is locked, or system init time.
1046 *
1047 ******************************************************************************/
1048
1049acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001050acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
1051 acpi_adr_space_type space_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Len Brown4be44fc2005-08-05 00:44:28 -04001053 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Bob Mooreb229cf92006-04-21 17:15:00 -04001055 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 /*
Bob Moore9f15fc62008-11-12 16:01:56 +08001058 * Run all _REG methods for all Operation Regions for this space ID. This
1059 * is a separate walk in order to handle any interdependencies between
1060 * regions and _REG methods. (i.e. handlers must be installed for all
1061 * regions of this Space ID before we can run any _REG methods)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Len Brown4be44fc2005-08-05 00:44:28 -04001063 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
1064 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
Lin Ming22635762009-11-13 10:06:08 +08001065 NULL, &space_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Len Brown4be44fc2005-08-05 00:44:28 -04001067 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/*******************************************************************************
1071 *
1072 * FUNCTION: acpi_ev_reg_run
1073 *
1074 * PARAMETERS: walk_namespace callback
1075 *
Bob Moore958dd242006-05-12 17:12:00 -04001076 * DESCRIPTION: Run _REG method for region objects of the requested space_iD
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 *
1078 ******************************************************************************/
1079
Robert Moore44f6c012005-04-18 22:49:35 -04001080static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -04001081acpi_ev_reg_run(acpi_handle obj_handle,
1082 u32 level, void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Len Brown4be44fc2005-08-05 00:44:28 -04001084 union acpi_operand_object *obj_desc;
1085 struct acpi_namespace_node *node;
1086 acpi_adr_space_type space_id;
1087 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Len Brown4be44fc2005-08-05 00:44:28 -04001089 space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* Convert and validate the device handle */
1092
Len Brown4be44fc2005-08-05 00:44:28 -04001093 node = acpi_ns_map_handle_to_node(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!node) {
1095 return (AE_BAD_PARAMETER);
1096 }
1097
1098 /*
Bob Moore9f15fc62008-11-12 16:01:56 +08001099 * We only care about regions.and objects that are allowed to have address
1100 * space handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Len Brown4be44fc2005-08-05 00:44:28 -04001102 if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return (AE_OK);
1104 }
1105
1106 /* Check for an existing internal object */
1107
Len Brown4be44fc2005-08-05 00:44:28 -04001108 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!obj_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -04001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* No object, just exit */
1112
1113 return (AE_OK);
1114 }
1115
1116 /* Object is a Region */
1117
1118 if (obj_desc->region.space_id != space_id) {
Bob Moore9f15fc62008-11-12 16:01:56 +08001119
1120 /* This region is for a different address space, just ignore it */
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return (AE_OK);
1123 }
1124
Len Brown4be44fc2005-08-05 00:44:28 -04001125 status = acpi_ev_execute_reg_method(obj_desc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return (status);
1127}