1 /******************************************************************************
3 * Module Name: evregion - Operation Region support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
50 #define _COMPONENT ACPI_EVENTS
51 ACPI_MODULE_NAME("evregion")
53 extern u8 acpi_gbl_default_address_spaces[];
55 /* Local prototypes */
57 static void acpi_ev_orphan_ec_reg_method(void);
60 acpi_ev_reg_run(acpi_handle obj_handle,
61 u32 level, void *context, void **return_value);
63 /*******************************************************************************
65 * FUNCTION: acpi_ev_initialize_op_regions
71 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
72 * an installed default region handler.
74 ******************************************************************************/
76 acpi_status acpi_ev_initialize_op_regions(void)
81 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
83 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
84 if (ACPI_FAILURE(status)) {
85 return_ACPI_STATUS(status);
88 /* Run the _REG methods for op_regions in each default address space */
90 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
92 * Make sure the installed handler is the DEFAULT handler. If not the
93 * default, the _REG methods will have already been run (when the
94 * handler was installed)
96 if (acpi_ev_has_default_handler(acpi_gbl_root_node,
97 acpi_gbl_default_address_spaces
100 acpi_ev_execute_reg_methods(acpi_gbl_root_node,
101 acpi_gbl_default_address_spaces
106 acpi_gbl_reg_methods_executed = TRUE;
108 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
109 return_ACPI_STATUS(status);
112 /*******************************************************************************
114 * FUNCTION: acpi_ev_address_space_dispatch
116 * PARAMETERS: region_obj - Internal region object
117 * field_obj - Corresponding field. Can be NULL.
118 * function - Read or Write operation
119 * region_offset - Where in the region to read or write
120 * bit_width - Field width in bits (8, 16, 32, or 64)
121 * value - Pointer to in or out value, must be
122 * a full 64-bit integer
126 * DESCRIPTION: Dispatch an address space or operation region access to
127 * a previously installed handler.
129 ******************************************************************************/
132 acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
133 union acpi_operand_object *field_obj,
135 u32 region_offset, u32 bit_width, u64 *value)
138 acpi_adr_space_handler handler;
139 acpi_adr_space_setup region_setup;
140 union acpi_operand_object *handler_desc;
141 union acpi_operand_object *region_obj2;
142 void *region_context = NULL;
143 struct acpi_connection_info *context;
145 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
147 region_obj2 = acpi_ns_get_secondary_object(region_obj);
149 return_ACPI_STATUS(AE_NOT_EXIST);
152 /* Ensure that there is a handler associated with this region */
154 handler_desc = region_obj->region.handler;
157 "No handler for Region [%4.4s] (%p) [%s]",
158 acpi_ut_get_node_name(region_obj->region.node),
160 acpi_ut_get_region_name(region_obj->region.
163 return_ACPI_STATUS(AE_NOT_EXIST);
166 context = handler_desc->address_space.context;
169 * It may be the case that the region has never been initialized.
170 * Some types of regions require special init code
172 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
174 /* This region has not been initialized yet, do it */
176 region_setup = handler_desc->address_space.setup;
179 /* No initialization routine, exit with error */
182 "No init routine for region(%p) [%s]",
184 acpi_ut_get_region_name(region_obj->region.
186 return_ACPI_STATUS(AE_NOT_EXIST);
190 * We must exit the interpreter because the region setup will
191 * potentially execute control methods (for example, the _REG method
194 acpi_ex_exit_interpreter();
196 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
197 context, ®ion_context);
199 /* Re-enter the interpreter */
201 acpi_ex_enter_interpreter();
203 /* Check for failure of the Region Setup */
205 if (ACPI_FAILURE(status)) {
206 ACPI_EXCEPTION((AE_INFO, status,
207 "During region initialization: [%s]",
208 acpi_ut_get_region_name(region_obj->
211 return_ACPI_STATUS(status);
214 /* Region initialization may have been completed by region_setup */
216 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
217 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
219 if (region_obj2->extra.region_context) {
221 /* The handler for this region was already installed */
223 ACPI_FREE(region_context);
226 * Save the returned context for use in all accesses to
227 * this particular region
229 region_obj2->extra.region_context =
235 /* We have everything we need, we can invoke the address space handler */
237 handler = handler_desc->address_space.handler;
239 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
240 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
241 ®ion_obj->region.handler->address_space, handler,
242 ACPI_FORMAT_NATIVE_UINT(region_obj->region.address +
244 acpi_ut_get_region_name(region_obj->region.
248 * Special handling for generic_serial_bus and general_purpose_io:
249 * There are three extra parameters that must be passed to the
250 * handler via the context:
251 * 1) Connection buffer, a resource template from Connection() op.
252 * 2) Length of the above buffer.
253 * 3) Actual access length from the access_as() op.
255 if (((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS) ||
256 (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO)) &&
257 context && field_obj) {
259 /* Get the Connection (resource_template) buffer */
261 context->connection = field_obj->field.resource_buffer;
262 context->length = field_obj->field.resource_length;
263 context->access_length = field_obj->field.access_length;
266 if (!(handler_desc->address_space.handler_flags &
267 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
269 * For handlers other than the default (supplied) handlers, we must
270 * exit the interpreter because the handler *might* block -- we don't
271 * know what it will do, so we can't hold the lock on the intepreter.
273 acpi_ex_exit_interpreter();
276 /* Call the handler */
278 status = handler(function,
279 (region_obj->region.address + region_offset),
280 bit_width, value, context,
281 region_obj2->extra.region_context);
283 if (ACPI_FAILURE(status)) {
284 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
285 acpi_ut_get_region_name(region_obj->region.
289 if (!(handler_desc->address_space.handler_flags &
290 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
292 * We just returned from a non-default handler, we must re-enter the
295 acpi_ex_enter_interpreter();
298 return_ACPI_STATUS(status);
301 /*******************************************************************************
303 * FUNCTION: acpi_ev_detach_region
305 * PARAMETERS: region_obj - Region Object
306 * acpi_ns_is_locked - Namespace Region Already Locked?
310 * DESCRIPTION: Break the association between the handler and the region
311 * this is a two way association.
313 ******************************************************************************/
316 acpi_ev_detach_region(union acpi_operand_object *region_obj,
317 u8 acpi_ns_is_locked)
319 union acpi_operand_object *handler_obj;
320 union acpi_operand_object *obj_desc;
321 union acpi_operand_object **last_obj_ptr;
322 acpi_adr_space_setup region_setup;
323 void **region_context;
324 union acpi_operand_object *region_obj2;
327 ACPI_FUNCTION_TRACE(ev_detach_region);
329 region_obj2 = acpi_ns_get_secondary_object(region_obj);
333 region_context = ®ion_obj2->extra.region_context;
335 /* Get the address handler from the region object */
337 handler_obj = region_obj->region.handler;
340 /* This region has no handler, all done */
345 /* Find this region in the handler's list */
347 obj_desc = handler_obj->address_space.region_list;
348 last_obj_ptr = &handler_obj->address_space.region_list;
352 /* Is this the correct Region? */
354 if (obj_desc == region_obj) {
355 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
356 "Removing Region %p from address handler %p\n",
357 region_obj, handler_obj));
359 /* This is it, remove it from the handler's list */
361 *last_obj_ptr = obj_desc->region.next;
362 obj_desc->region.next = NULL; /* Must clear field */
364 if (acpi_ns_is_locked) {
366 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
367 if (ACPI_FAILURE(status)) {
372 /* Now stop region accesses by executing the _REG method */
375 acpi_ev_execute_reg_method(region_obj,
376 ACPI_REG_DISCONNECT);
377 if (ACPI_FAILURE(status)) {
378 ACPI_EXCEPTION((AE_INFO, status,
379 "from region _REG, [%s]",
380 acpi_ut_get_region_name
381 (region_obj->region.space_id)));
384 if (acpi_ns_is_locked) {
386 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
387 if (ACPI_FAILURE(status)) {
393 * If the region has been activated, call the setup handler with
394 * the deactivate notification
396 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
397 region_setup = handler_obj->address_space.setup;
399 region_setup(region_obj,
400 ACPI_REGION_DEACTIVATE,
401 handler_obj->address_space.
402 context, region_context);
404 /* Init routine may fail, Just ignore errors */
406 if (ACPI_FAILURE(status)) {
407 ACPI_EXCEPTION((AE_INFO, status,
408 "from region handler - deactivate, [%s]",
409 acpi_ut_get_region_name
414 region_obj->region.flags &=
415 ~(AOPOBJ_SETUP_COMPLETE);
419 * Remove handler reference in the region
421 * NOTE: this doesn't mean that the region goes away, the region
422 * is just inaccessible as indicated to the _REG method
424 * If the region is on the handler's list, this must be the
427 region_obj->region.handler = NULL;
428 acpi_ut_remove_reference(handler_obj);
433 /* Walk the linked list of handlers */
435 last_obj_ptr = &obj_desc->region.next;
436 obj_desc = obj_desc->region.next;
439 /* If we get here, the region was not in the handler's region list */
441 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
442 "Cannot remove region %p from address handler %p\n",
443 region_obj, handler_obj));
448 /*******************************************************************************
450 * FUNCTION: acpi_ev_attach_region
452 * PARAMETERS: handler_obj - Handler Object
453 * region_obj - Region Object
454 * acpi_ns_is_locked - Namespace Region Already Locked?
458 * DESCRIPTION: Create the association between the handler and the region
459 * this is a two way association.
461 ******************************************************************************/
464 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
465 union acpi_operand_object *region_obj,
466 u8 acpi_ns_is_locked)
469 ACPI_FUNCTION_TRACE(ev_attach_region);
471 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
472 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
473 acpi_ut_get_node_name(region_obj->region.node),
474 region_obj, handler_obj,
475 acpi_ut_get_region_name(region_obj->region.
478 /* Link this region to the front of the handler's list */
480 region_obj->region.next = handler_obj->address_space.region_list;
481 handler_obj->address_space.region_list = region_obj;
483 /* Install the region's handler */
485 if (region_obj->region.handler) {
486 return_ACPI_STATUS(AE_ALREADY_EXISTS);
489 region_obj->region.handler = handler_obj;
490 acpi_ut_add_reference(handler_obj);
492 return_ACPI_STATUS(AE_OK);
495 /*******************************************************************************
497 * FUNCTION: acpi_ev_execute_reg_method
499 * PARAMETERS: region_obj - Region object
500 * function - Passed to _REG: On (1) or Off (0)
504 * DESCRIPTION: Execute _REG method for a region
506 ******************************************************************************/
509 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
511 struct acpi_evaluate_info *info;
512 union acpi_operand_object *args[3];
513 union acpi_operand_object *region_obj2;
516 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
518 region_obj2 = acpi_ns_get_secondary_object(region_obj);
520 return_ACPI_STATUS(AE_NOT_EXIST);
523 if (region_obj2->extra.method_REG == NULL) {
524 return_ACPI_STATUS(AE_OK);
527 /* Allocate and initialize the evaluation information block */
529 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
531 return_ACPI_STATUS(AE_NO_MEMORY);
534 info->prefix_node = region_obj2->extra.method_REG;
535 info->pathname = NULL;
536 info->parameters = args;
537 info->flags = ACPI_IGNORE_RETURN_VALUE;
540 * The _REG method has two arguments:
543 * Operation region space ID Same value as region_obj->Region.space_id
546 * connection status 1 for connecting the handler, 0 for disconnecting
547 * the handler (Passed as a parameter)
550 acpi_ut_create_integer_object((u64)region_obj->region.space_id);
552 status = AE_NO_MEMORY;
556 args[1] = acpi_ut_create_integer_object((u64)function);
558 status = AE_NO_MEMORY;
562 args[2] = NULL; /* Terminate list */
564 /* Execute the method, no return value */
566 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
567 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
569 status = acpi_ns_evaluate(info);
570 acpi_ut_remove_reference(args[1]);
573 acpi_ut_remove_reference(args[0]);
577 return_ACPI_STATUS(status);
580 /*******************************************************************************
582 * FUNCTION: acpi_ev_execute_reg_methods
584 * PARAMETERS: node - Namespace node for the device
585 * space_id - The address space ID
589 * DESCRIPTION: Run all _REG methods for the input Space ID;
590 * Note: assumes namespace is locked, or system init time.
592 ******************************************************************************/
595 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
596 acpi_adr_space_type space_id)
600 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
603 * Run all _REG methods for all Operation Regions for this space ID. This
604 * is a separate walk in order to handle any interdependencies between
605 * regions and _REG methods. (i.e. handlers must be installed for all
606 * regions of this Space ID before we can run any _REG methods)
608 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
609 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
610 NULL, &space_id, NULL);
612 /* Special case for EC: handle "orphan" _REG methods with no region */
614 if (space_id == ACPI_ADR_SPACE_EC) {
615 acpi_ev_orphan_ec_reg_method();
618 return_ACPI_STATUS(status);
621 /*******************************************************************************
623 * FUNCTION: acpi_ev_reg_run
625 * PARAMETERS: walk_namespace callback
627 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
629 ******************************************************************************/
632 acpi_ev_reg_run(acpi_handle obj_handle,
633 u32 level, void *context, void **return_value)
635 union acpi_operand_object *obj_desc;
636 struct acpi_namespace_node *node;
637 acpi_adr_space_type space_id;
640 space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
642 /* Convert and validate the device handle */
644 node = acpi_ns_validate_handle(obj_handle);
646 return (AE_BAD_PARAMETER);
650 * We only care about regions.and objects that are allowed to have address
653 if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
657 /* Check for an existing internal object */
659 obj_desc = acpi_ns_get_attached_object(node);
662 /* No object, just exit */
667 /* Object is a Region */
669 if (obj_desc->region.space_id != space_id) {
671 /* This region is for a different address space, just ignore it */
676 status = acpi_ev_execute_reg_method(obj_desc, ACPI_REG_CONNECT);
680 /*******************************************************************************
682 * FUNCTION: acpi_ev_orphan_ec_reg_method
688 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
689 * device. This is a _REG method that has no corresponding region
690 * within the EC device scope. The orphan _REG method appears to
691 * have been enabled by the description of the ECDT in the ACPI
692 * specification: "The availability of the region space can be
693 * detected by providing a _REG method object underneath the
694 * Embedded Controller device."
696 * To quickly access the EC device, we use the EC_ID that appears
697 * within the ECDT. Otherwise, we would need to perform a time-
698 * consuming namespace walk, executing _HID methods to find the
701 ******************************************************************************/
703 static void acpi_ev_orphan_ec_reg_method(void)
705 struct acpi_table_ecdt *table;
707 struct acpi_object_list args;
708 union acpi_object objects[2];
709 struct acpi_namespace_node *ec_device_node;
710 struct acpi_namespace_node *reg_method;
711 struct acpi_namespace_node *next_node;
713 ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method);
715 /* Get the ECDT (if present in system) */
717 status = acpi_get_table(ACPI_SIG_ECDT, 0,
718 ACPI_CAST_INDIRECT_PTR(struct acpi_table_header,
720 if (ACPI_FAILURE(status)) {
724 /* We need a valid EC_ID string */
730 /* Namespace is currently locked, must release */
732 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
734 /* Get a handle to the EC device referenced in the ECDT */
736 status = acpi_get_handle(NULL,
737 ACPI_CAST_PTR(char, table->id),
738 ACPI_CAST_PTR(acpi_handle, &ec_device_node));
739 if (ACPI_FAILURE(status)) {
743 /* Get a handle to a _REG method immediately under the EC device */
745 status = acpi_get_handle(ec_device_node,
746 METHOD_NAME__REG, ACPI_CAST_PTR(acpi_handle,
748 if (ACPI_FAILURE(status)) {
753 * Execute the _REG method only if there is no Operation Region in
754 * this scope with the Embedded Controller space ID. Otherwise, it
755 * will already have been executed. Note, this allows for Regions
756 * with other space IDs to be present; but the code below will then
757 * execute the _REG method with the EC space ID argument.
759 next_node = acpi_ns_get_next_node(ec_device_node, NULL);
761 if ((next_node->type == ACPI_TYPE_REGION) &&
762 (next_node->object) &&
763 (next_node->object->region.space_id == ACPI_ADR_SPACE_EC)) {
764 goto exit; /* Do not execute _REG */
766 next_node = acpi_ns_get_next_node(ec_device_node, next_node);
769 /* Evaluate the _REG(EC,Connect) method */
772 args.pointer = objects;
773 objects[0].type = ACPI_TYPE_INTEGER;
774 objects[0].integer.value = ACPI_ADR_SPACE_EC;
775 objects[1].type = ACPI_TYPE_INTEGER;
776 objects[1].integer.value = ACPI_REG_CONNECT;
778 status = acpi_evaluate_object(reg_method, NULL, &args, NULL);
781 /* We ignore all errors from above, don't care */
783 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);