blob: cea14d6fc76c976db580144f93d28d890ff3938f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
Bob Moore42f8fb72013-01-11 13:08:51 +01003 * Module Name: evregion - Operation Region support
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *****************************************************************************/
6
7/*
Bob Moore25f044e2013-01-25 05:38:56 +00008 * Copyright (C) 2000 - 2013, 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
Bob Moore42f8fb72013-01-11 13:08:51 +010053extern u8 acpi_gbl_default_address_spaces[];
54
Robert Moore44f6c012005-04-18 22:49:35 -040055/* Local prototypes */
Lin Ming74d3ec772009-10-13 10:29:30 +080056
Zhang Rui8f4f5e72013-05-30 10:02:13 +080057static void
58acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node);
Bob Mooree2066ca2011-04-13 13:22:04 +080059
Robert Moore44f6c012005-04-18 22:49:35 -040060static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040061acpi_ev_reg_run(acpi_handle obj_handle,
62 u32 level, void *context, void **return_value);
Robert Moore44f6c012005-04-18 22:49:35 -040063
Lin Ming74d3ec772009-10-13 10:29:30 +080064/*******************************************************************************
65 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * FUNCTION: acpi_ev_initialize_op_regions
67 *
68 * PARAMETERS: None
69 *
70 * RETURN: Status
71 *
72 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
73 * an installed default region handler.
74 *
75 ******************************************************************************/
76
Len Brown4be44fc2005-08-05 00:44:28 -040077acpi_status acpi_ev_initialize_op_regions(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Len Brown4be44fc2005-08-05 00:44:28 -040079 acpi_status status;
Bob Moore67a119f2008-06-10 13:42:13 +080080 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Bob Mooreb229cf92006-04-21 17:15:00 -040082 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Len Brown4be44fc2005-08-05 00:44:28 -040084 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
85 if (ACPI_FAILURE(status)) {
86 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88
Bob Moore9f15fc62008-11-12 16:01:56 +080089 /* Run the _REG methods for op_regions in each default address space */
Bob Moore52fc0b02006-10-02 00:00:00 -040090
Bob Moore9f15fc62008-11-12 16:01:56 +080091 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
92 /*
Lin Ming74d3ec772009-10-13 10:29:30 +080093 * Make sure the installed handler is the DEFAULT handler. If not the
94 * default, the _REG methods will have already been run (when the
95 * handler was installed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
Lin Ming74d3ec772009-10-13 10:29:30 +080097 if (acpi_ev_has_default_handler(acpi_gbl_root_node,
98 acpi_gbl_default_address_spaces
99 [i])) {
100 status =
101 acpi_ev_execute_reg_methods(acpi_gbl_root_node,
102 acpi_gbl_default_address_spaces
103 [i]);
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Bob Mooreef09c4f2011-02-14 16:13:25 +0800107 acpi_gbl_reg_methods_executed = TRUE;
108
Len Brown4be44fc2005-08-05 00:44:28 -0400109 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
110 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*******************************************************************************
114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * FUNCTION: acpi_ev_address_space_dispatch
116 *
117 * PARAMETERS: region_obj - Internal region object
Bob Moore9ce81782011-11-16 13:39:07 +0800118 * field_obj - Corresponding field. Can be NULL.
Bob Mooreba494be2012-07-12 09:40:10 +0800119 * function - Read or Write operation
Bob Mooref5407af2009-05-21 10:56:52 +0800120 * region_offset - Where in the region to read or write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * bit_width - Field width in bits (8, 16, 32, or 64)
Bob Mooreba494be2012-07-12 09:40:10 +0800122 * value - Pointer to in or out value, must be
Bob Moore5df7e6c2010-01-21 10:06:32 +0800123 * a full 64-bit integer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
125 * RETURN: Status
126 *
127 * DESCRIPTION: Dispatch an address space or operation region access to
128 * a previously installed handler.
129 *
130 ******************************************************************************/
131
132acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400133acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
Bob Moore9ce81782011-11-16 13:39:07 +0800134 union acpi_operand_object *field_obj,
Len Brown4be44fc2005-08-05 00:44:28 -0400135 u32 function,
Bob Moore5df7e6c2010-01-21 10:06:32 +0800136 u32 region_offset, u32 bit_width, u64 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Len Brown4be44fc2005-08-05 00:44:28 -0400138 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400139 acpi_adr_space_handler handler;
140 acpi_adr_space_setup region_setup;
141 union acpi_operand_object *handler_desc;
142 union acpi_operand_object *region_obj2;
143 void *region_context = NULL;
Bob Moore9ce81782011-11-16 13:39:07 +0800144 struct acpi_connection_info *context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Bob Mooreb229cf92006-04-21 17:15:00 -0400146 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Len Brown4be44fc2005-08-05 00:44:28 -0400148 region_obj2 = acpi_ns_get_secondary_object(region_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!region_obj2) {
Len Brown4be44fc2005-08-05 00:44:28 -0400150 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
153 /* Ensure that there is a handler associated with this region */
154
155 handler_desc = region_obj->region.handler;
156 if (!handler_desc) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500157 ACPI_ERROR((AE_INFO,
158 "No handler for Region [%4.4s] (%p) [%s]",
159 acpi_ut_get_node_name(region_obj->region.node),
160 region_obj,
161 acpi_ut_get_region_name(region_obj->region.
162 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Len Brown4be44fc2005-08-05 00:44:28 -0400164 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Bob Moore9ce81782011-11-16 13:39:07 +0800167 context = handler_desc->address_space.context;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800170 * It may be the case that the region has never been initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * Some types of regions require special init code
172 */
173 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
Bob Moore9f15fc62008-11-12 16:01:56 +0800174
175 /* This region has not been initialized yet, do it */
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 region_setup = handler_desc->address_space.setup;
178 if (!region_setup) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* No initialization routine, exit with error */
181
Bob Mooreb8e4d892006-01-27 16:43:00 -0500182 ACPI_ERROR((AE_INFO,
183 "No init routine for region(%p) [%s]",
184 region_obj,
185 acpi_ut_get_region_name(region_obj->region.
186 space_id)));
Len Brown4be44fc2005-08-05 00:44:28 -0400187 return_ACPI_STATUS(AE_NOT_EXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800191 * We must exit the interpreter because the region setup will
192 * potentially execute control methods (for example, the _REG method
193 * for this region)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
Bob Moore014d4332008-01-10 23:04:10 -0500195 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Len Brown4be44fc2005-08-05 00:44:28 -0400197 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
Bob Moore9ce81782011-11-16 13:39:07 +0800198 context, &region_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 /* Re-enter the interpreter */
201
Bob Moore014d4332008-01-10 23:04:10 -0500202 acpi_ex_enter_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Check for failure of the Region Setup */
205
Len Brown4be44fc2005-08-05 00:44:28 -0400206 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500207 ACPI_EXCEPTION((AE_INFO, status,
208 "During region initialization: [%s]",
209 acpi_ut_get_region_name(region_obj->
210 region.
211 space_id)));
Len Brown4be44fc2005-08-05 00:44:28 -0400212 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
Bob Moore9f15fc62008-11-12 16:01:56 +0800215 /* Region initialization may have been completed by region_setup */
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
218 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
219
220 if (region_obj2->extra.region_context) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* The handler for this region was already installed */
223
Bob Moore83135242006-10-03 00:00:00 -0400224 ACPI_FREE(region_context);
Len Brown4be44fc2005-08-05 00:44:28 -0400225 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /*
227 * Save the returned context for use in all accesses to
228 * this particular region
229 */
Len Brown4be44fc2005-08-05 00:44:28 -0400230 region_obj2->extra.region_context =
231 region_context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
234 }
235
236 /* We have everything we need, we can invoke the address space handler */
237
238 handler = handler_desc->address_space.handler;
239
Len Brown4be44fc2005-08-05 00:44:28 -0400240 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
241 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
242 &region_obj->region.handler->address_space, handler,
Bob Mooref5407af2009-05-21 10:56:52 +0800243 ACPI_FORMAT_NATIVE_UINT(region_obj->region.address +
244 region_offset),
Len Brown4be44fc2005-08-05 00:44:28 -0400245 acpi_ut_get_region_name(region_obj->region.
246 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Bob Moore9ce81782011-11-16 13:39:07 +0800248 /*
249 * Special handling for generic_serial_bus and general_purpose_io:
250 * There are three extra parameters that must be passed to the
251 * handler via the context:
252 * 1) Connection buffer, a resource template from Connection() op.
253 * 2) Length of the above buffer.
254 * 3) Actual access length from the access_as() op.
255 */
256 if (((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS) ||
257 (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO)) &&
258 context && field_obj) {
259
260 /* Get the Connection (resource_template) buffer */
261
262 context->connection = field_obj->field.resource_buffer;
263 context->length = field_obj->field.resource_length;
264 context->access_length = field_obj->field.access_length;
265 }
266
Bob Moore616861242006-03-17 16:44:00 -0500267 if (!(handler_desc->address_space.handler_flags &
268 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /*
270 * For handlers other than the default (supplied) handlers, we must
271 * exit the interpreter because the handler *might* block -- we don't
272 * know what it will do, so we can't hold the lock on the intepreter.
273 */
Bob Moore014d4332008-01-10 23:04:10 -0500274 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 /* Call the handler */
278
Bob Mooref5407af2009-05-21 10:56:52 +0800279 status = handler(function,
280 (region_obj->region.address + region_offset),
Bob Moore9ce81782011-11-16 13:39:07 +0800281 bit_width, value, context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 region_obj2->extra.region_context);
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500285 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
286 acpi_ut_get_region_name(region_obj->region.
287 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Bob Moore616861242006-03-17 16:44:00 -0500290 if (!(handler_desc->address_space.handler_flags &
291 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /*
293 * We just returned from a non-default handler, we must re-enter the
294 * interpreter
295 */
Bob Moore014d4332008-01-10 23:04:10 -0500296 acpi_ex_enter_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*******************************************************************************
303 *
304 * FUNCTION: acpi_ev_detach_region
305 *
306 * PARAMETERS: region_obj - Region Object
307 * acpi_ns_is_locked - Namespace Region Already Locked?
308 *
309 * RETURN: None
310 *
311 * DESCRIPTION: Break the association between the handler and the region
312 * this is a two way association.
313 *
314 ******************************************************************************/
315
316void
Len Brown4be44fc2005-08-05 00:44:28 -0400317acpi_ev_detach_region(union acpi_operand_object *region_obj,
318 u8 acpi_ns_is_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Len Brown4be44fc2005-08-05 00:44:28 -0400320 union acpi_operand_object *handler_obj;
321 union acpi_operand_object *obj_desc;
322 union acpi_operand_object **last_obj_ptr;
323 acpi_adr_space_setup region_setup;
324 void **region_context;
325 union acpi_operand_object *region_obj2;
326 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Bob Mooreb229cf92006-04-21 17:15:00 -0400328 ACPI_FUNCTION_TRACE(ev_detach_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Len Brown4be44fc2005-08-05 00:44:28 -0400330 region_obj2 = acpi_ns_get_secondary_object(region_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (!region_obj2) {
332 return_VOID;
333 }
334 region_context = &region_obj2->extra.region_context;
335
336 /* Get the address handler from the region object */
337
338 handler_obj = region_obj->region.handler;
339 if (!handler_obj) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* This region has no handler, all done */
342
343 return_VOID;
344 }
345
346 /* Find this region in the handler's list */
347
348 obj_desc = handler_obj->address_space.region_list;
349 last_obj_ptr = &handler_obj->address_space.region_list;
350
351 while (obj_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Is this the correct Region? */
354
355 if (obj_desc == region_obj) {
Len Brown4be44fc2005-08-05 00:44:28 -0400356 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
357 "Removing Region %p from address handler %p\n",
358 region_obj, handler_obj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* This is it, remove it from the handler's list */
361
362 *last_obj_ptr = obj_desc->region.next;
Len Brown4be44fc2005-08-05 00:44:28 -0400363 obj_desc->region.next = NULL; /* Must clear field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (acpi_ns_is_locked) {
Len Brown4be44fc2005-08-05 00:44:28 -0400366 status =
367 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
368 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return_VOID;
370 }
371 }
372
373 /* Now stop region accesses by executing the _REG method */
374
Bob Mooree2066ca2011-04-13 13:22:04 +0800375 status =
376 acpi_ev_execute_reg_method(region_obj,
377 ACPI_REG_DISCONNECT);
Len Brown4be44fc2005-08-05 00:44:28 -0400378 if (ACPI_FAILURE(status)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500379 ACPI_EXCEPTION((AE_INFO, status,
380 "from region _REG, [%s]",
381 acpi_ut_get_region_name
382 (region_obj->region.space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
385 if (acpi_ns_is_locked) {
Len Brown4be44fc2005-08-05 00:44:28 -0400386 status =
387 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
388 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return_VOID;
390 }
391 }
392
Bob Mooref6dd9222006-07-07 20:44:38 -0400393 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800394 * If the region has been activated, call the setup handler with
395 * the deactivate notification
Bob Mooref6dd9222006-07-07 20:44:38 -0400396 */
397 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
398 region_setup = handler_obj->address_space.setup;
399 status =
400 region_setup(region_obj,
401 ACPI_REGION_DEACTIVATE,
402 handler_obj->address_space.
403 context, region_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Bob Mooref6dd9222006-07-07 20:44:38 -0400405 /* Init routine may fail, Just ignore errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Bob Mooref6dd9222006-07-07 20:44:38 -0400407 if (ACPI_FAILURE(status)) {
408 ACPI_EXCEPTION((AE_INFO, status,
409 "from region handler - deactivate, [%s]",
410 acpi_ut_get_region_name
411 (region_obj->region.
412 space_id)));
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Bob Mooref6dd9222006-07-07 20:44:38 -0400415 region_obj->region.flags &=
416 ~(AOPOBJ_SETUP_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /*
420 * Remove handler reference in the region
421 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400422 * NOTE: this doesn't mean that the region goes away, the region
423 * is just inaccessible as indicated to the _REG method
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 *
Bob Mooref6dd9222006-07-07 20:44:38 -0400425 * If the region is on the handler's list, this must be the
426 * region's handler
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
428 region_obj->region.handler = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400429 acpi_ut_remove_reference(handler_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 return_VOID;
432 }
433
434 /* Walk the linked list of handlers */
435
436 last_obj_ptr = &obj_desc->region.next;
437 obj_desc = obj_desc->region.next;
438 }
439
440 /* If we get here, the region was not in the handler's region list */
441
Len Brown4be44fc2005-08-05 00:44:28 -0400442 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
443 "Cannot remove region %p from address handler %p\n",
444 region_obj, handler_obj));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 return_VOID;
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/*******************************************************************************
450 *
451 * FUNCTION: acpi_ev_attach_region
452 *
453 * PARAMETERS: handler_obj - Handler Object
454 * region_obj - Region Object
455 * acpi_ns_is_locked - Namespace Region Already Locked?
456 *
457 * RETURN: None
458 *
459 * DESCRIPTION: Create the association between the handler and the region
460 * this is a two way association.
461 *
462 ******************************************************************************/
463
464acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400465acpi_ev_attach_region(union acpi_operand_object *handler_obj,
466 union acpi_operand_object *region_obj,
467 u8 acpi_ns_is_locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469
Bob Mooreb229cf92006-04-21 17:15:00 -0400470 ACPI_FUNCTION_TRACE(ev_attach_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Len Brown4be44fc2005-08-05 00:44:28 -0400472 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
473 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
474 acpi_ut_get_node_name(region_obj->region.node),
475 region_obj, handler_obj,
476 acpi_ut_get_region_name(region_obj->region.
477 space_id)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 /* Link this region to the front of the handler's list */
480
481 region_obj->region.next = handler_obj->address_space.region_list;
482 handler_obj->address_space.region_list = region_obj;
483
484 /* Install the region's handler */
485
486 if (region_obj->region.handler) {
Len Brown4be44fc2005-08-05 00:44:28 -0400487 return_ACPI_STATUS(AE_ALREADY_EXISTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
490 region_obj->region.handler = handler_obj;
Len Brown4be44fc2005-08-05 00:44:28 -0400491 acpi_ut_add_reference(handler_obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Len Brown4be44fc2005-08-05 00:44:28 -0400493 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*******************************************************************************
497 *
Bob Moore42f8fb72013-01-11 13:08:51 +0100498 * FUNCTION: acpi_ev_execute_reg_method
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *
Bob Moore42f8fb72013-01-11 13:08:51 +0100500 * PARAMETERS: region_obj - Region object
501 * function - Passed to _REG: On (1) or Off (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 *
503 * RETURN: Status
504 *
Bob Moore42f8fb72013-01-11 13:08:51 +0100505 * DESCRIPTION: Execute _REG method for a region
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
507 ******************************************************************************/
508
509acpi_status
Bob Moore42f8fb72013-01-11 13:08:51 +0100510acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Bob Moore42f8fb72013-01-11 13:08:51 +0100512 struct acpi_evaluate_info *info;
513 union acpi_operand_object *args[3];
514 union acpi_operand_object *region_obj2;
Len Brown4be44fc2005-08-05 00:44:28 -0400515 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Bob Moore42f8fb72013-01-11 13:08:51 +0100517 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
518
519 region_obj2 = acpi_ns_get_secondary_object(region_obj);
520 if (!region_obj2) {
521 return_ACPI_STATUS(AE_NOT_EXIST);
522 }
523
524 if (region_obj2->extra.method_REG == NULL) {
525 return_ACPI_STATUS(AE_OK);
526 }
527
528 /* Allocate and initialize the evaluation information block */
529
530 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
531 if (!info) {
532 return_ACPI_STATUS(AE_NO_MEMORY);
533 }
534
535 info->prefix_node = region_obj2->extra.method_REG;
Bob Moore29a241c2013-05-30 10:00:01 +0800536 info->relative_pathname = NULL;
Bob Moore42f8fb72013-01-11 13:08:51 +0100537 info->parameters = args;
538 info->flags = ACPI_IGNORE_RETURN_VALUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /*
Bob Moore42f8fb72013-01-11 13:08:51 +0100541 * The _REG method has two arguments:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *
Bob Moore42f8fb72013-01-11 13:08:51 +0100543 * arg0 - Integer:
544 * Operation region space ID Same value as region_obj->Region.space_id
545 *
546 * arg1 - Integer:
547 * connection status 1 for connecting the handler, 0 for disconnecting
548 * the handler (Passed as a parameter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Bob Moore42f8fb72013-01-11 13:08:51 +0100550 args[0] =
551 acpi_ut_create_integer_object((u64)region_obj->region.space_id);
552 if (!args[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 status = AE_NO_MEMORY;
Bob Moore42f8fb72013-01-11 13:08:51 +0100554 goto cleanup1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
Bob Moore42f8fb72013-01-11 13:08:51 +0100557 args[1] = acpi_ut_create_integer_object((u64)function);
558 if (!args[1]) {
559 status = AE_NO_MEMORY;
560 goto cleanup2;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Bob Moore42f8fb72013-01-11 13:08:51 +0100563 args[2] = NULL; /* Terminate list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Bob Moore42f8fb72013-01-11 13:08:51 +0100565 /* Execute the method, no return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Bob Moore42f8fb72013-01-11 13:08:51 +0100567 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
568 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Bob Moore42f8fb72013-01-11 13:08:51 +0100570 status = acpi_ns_evaluate(info);
571 acpi_ut_remove_reference(args[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Bob Moore42f8fb72013-01-11 13:08:51 +0100573 cleanup2:
574 acpi_ut_remove_reference(args[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Bob Moore42f8fb72013-01-11 13:08:51 +0100576 cleanup1:
577 ACPI_FREE(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400578 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*******************************************************************************
582 *
583 * FUNCTION: acpi_ev_execute_reg_methods
584 *
Bob Mooreba494be2012-07-12 09:40:10 +0800585 * PARAMETERS: node - Namespace node for the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 * space_id - The address space ID
587 *
588 * RETURN: Status
589 *
590 * DESCRIPTION: Run all _REG methods for the input Space ID;
591 * Note: assumes namespace is locked, or system init time.
592 *
593 ******************************************************************************/
594
595acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400596acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
597 acpi_adr_space_type space_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Len Brown4be44fc2005-08-05 00:44:28 -0400599 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Bob Mooreb229cf92006-04-21 17:15:00 -0400601 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800604 * Run all _REG methods for all Operation Regions for this space ID. This
605 * is a separate walk in order to handle any interdependencies between
606 * regions and _REG methods. (i.e. handlers must be installed for all
607 * regions of this Space ID before we can run any _REG methods)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Len Brown4be44fc2005-08-05 00:44:28 -0400609 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
610 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
Lin Ming22635762009-11-13 10:06:08 +0800611 NULL, &space_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Bob Mooree2066ca2011-04-13 13:22:04 +0800613 /* Special case for EC: handle "orphan" _REG methods with no region */
614
615 if (space_id == ACPI_ADR_SPACE_EC) {
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800616 acpi_ev_orphan_ec_reg_method(node);
Bob Mooree2066ca2011-04-13 13:22:04 +0800617 }
618
Len Brown4be44fc2005-08-05 00:44:28 -0400619 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*******************************************************************************
623 *
624 * FUNCTION: acpi_ev_reg_run
625 *
626 * PARAMETERS: walk_namespace callback
627 *
Bob Mooreba494be2012-07-12 09:40:10 +0800628 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
630 ******************************************************************************/
631
Robert Moore44f6c012005-04-18 22:49:35 -0400632static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400633acpi_ev_reg_run(acpi_handle obj_handle,
634 u32 level, void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Len Brown4be44fc2005-08-05 00:44:28 -0400636 union acpi_operand_object *obj_desc;
637 struct acpi_namespace_node *node;
638 acpi_adr_space_type space_id;
639 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Len Brown4be44fc2005-08-05 00:44:28 -0400641 space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* Convert and validate the device handle */
644
Bob Mooref24b6642009-12-11 14:57:00 +0800645 node = acpi_ns_validate_handle(obj_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!node) {
647 return (AE_BAD_PARAMETER);
648 }
649
650 /*
Bob Moore9f15fc62008-11-12 16:01:56 +0800651 * We only care about regions.and objects that are allowed to have address
652 * space handlers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
Len Brown4be44fc2005-08-05 00:44:28 -0400654 if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return (AE_OK);
656 }
657
658 /* Check for an existing internal object */
659
Len Brown4be44fc2005-08-05 00:44:28 -0400660 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!obj_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* No object, just exit */
664
665 return (AE_OK);
666 }
667
668 /* Object is a Region */
669
670 if (obj_desc->region.space_id != space_id) {
Bob Moore9f15fc62008-11-12 16:01:56 +0800671
672 /* This region is for a different address space, just ignore it */
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return (AE_OK);
675 }
676
Bob Mooree2066ca2011-04-13 13:22:04 +0800677 status = acpi_ev_execute_reg_method(obj_desc, ACPI_REG_CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return (status);
679}
Bob Mooree2066ca2011-04-13 13:22:04 +0800680
681/*******************************************************************************
682 *
683 * FUNCTION: acpi_ev_orphan_ec_reg_method
684 *
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800685 * PARAMETERS: ec_device_node - Namespace node for an EC device
Bob Mooree2066ca2011-04-13 13:22:04 +0800686 *
687 * RETURN: None
688 *
689 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
690 * device. This is a _REG method that has no corresponding region
691 * within the EC device scope. The orphan _REG method appears to
692 * have been enabled by the description of the ECDT in the ACPI
693 * specification: "The availability of the region space can be
694 * detected by providing a _REG method object underneath the
695 * Embedded Controller device."
696 *
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800697 * To quickly access the EC device, we use the ec_device_node used
698 * during EC handler installation. Otherwise, we would need to
699 * perform a time consuming namespace walk, executing _HID
700 * methods to find the EC device.
701 *
702 * MUTEX: Assumes the namespace is locked
Bob Mooree2066ca2011-04-13 13:22:04 +0800703 *
704 ******************************************************************************/
705
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800706static void
707acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node)
Bob Mooree2066ca2011-04-13 13:22:04 +0800708{
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800709 acpi_handle reg_method;
710 struct acpi_namespace_node *next_node;
Bob Mooree2066ca2011-04-13 13:22:04 +0800711 acpi_status status;
712 struct acpi_object_list args;
713 union acpi_object objects[2];
Bob Mooree2066ca2011-04-13 13:22:04 +0800714
715 ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method);
716
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800717 if (!ec_device_node) {
Bob Mooree2066ca2011-04-13 13:22:04 +0800718 return_VOID;
719 }
720
721 /* Namespace is currently locked, must release */
722
723 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
724
Bob Mooree2066ca2011-04-13 13:22:04 +0800725 /* Get a handle to a _REG method immediately under the EC device */
726
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800727 status = acpi_get_handle(ec_device_node, METHOD_NAME__REG, &reg_method);
Bob Mooree2066ca2011-04-13 13:22:04 +0800728 if (ACPI_FAILURE(status)) {
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800729 goto exit; /* There is no _REG method present */
Bob Mooree2066ca2011-04-13 13:22:04 +0800730 }
731
732 /*
733 * Execute the _REG method only if there is no Operation Region in
734 * this scope with the Embedded Controller space ID. Otherwise, it
735 * will already have been executed. Note, this allows for Regions
736 * with other space IDs to be present; but the code below will then
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800737 * execute the _REG method with the embedded_control space_ID argument.
Bob Mooree2066ca2011-04-13 13:22:04 +0800738 */
739 next_node = acpi_ns_get_next_node(ec_device_node, NULL);
740 while (next_node) {
741 if ((next_node->type == ACPI_TYPE_REGION) &&
742 (next_node->object) &&
743 (next_node->object->region.space_id == ACPI_ADR_SPACE_EC)) {
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800744 goto exit; /* Do not execute the _REG */
Bob Mooree2066ca2011-04-13 13:22:04 +0800745 }
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800746
Bob Mooree2066ca2011-04-13 13:22:04 +0800747 next_node = acpi_ns_get_next_node(ec_device_node, next_node);
748 }
749
Zhang Rui8f4f5e72013-05-30 10:02:13 +0800750 /* Evaluate the _REG(embedded_control,Connect) method */
Bob Mooree2066ca2011-04-13 13:22:04 +0800751
752 args.count = 2;
753 args.pointer = objects;
754 objects[0].type = ACPI_TYPE_INTEGER;
755 objects[0].integer.value = ACPI_ADR_SPACE_EC;
756 objects[1].type = ACPI_TYPE_INTEGER;
757 objects[1].integer.value = ACPI_REG_CONNECT;
758
759 status = acpi_evaluate_object(reg_method, NULL, &args, NULL);
760
761 exit:
762 /* We ignore all errors from above, don't care */
763
764 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
765 return_VOID;
766}