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 */
58 acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node);
61 acpi_ev_reg_run(acpi_handle obj_handle,
62 u32 level, void *context, void **return_value);
64 /*******************************************************************************
66 * FUNCTION: acpi_ev_initialize_op_regions
72 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
73 * an installed default region handler.
75 ******************************************************************************/
77 acpi_status acpi_ev_initialize_op_regions(void)
82 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
84 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
85 if (ACPI_FAILURE(status)) {
86 return_ACPI_STATUS(status);
89 /* Run the _REG methods for op_regions in each default address space */
91 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
93 * 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)
97 if (acpi_ev_has_default_handler(acpi_gbl_root_node,
98 acpi_gbl_default_address_spaces
101 acpi_ev_execute_reg_methods(acpi_gbl_root_node,
102 acpi_gbl_default_address_spaces
107 acpi_gbl_reg_methods_executed = TRUE;
109 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
110 return_ACPI_STATUS(status);
113 /*******************************************************************************
115 * FUNCTION: acpi_ev_address_space_dispatch
117 * PARAMETERS: region_obj - Internal region object
118 * field_obj - Corresponding field. Can be NULL.
119 * function - Read or Write operation
120 * region_offset - Where in the region to read or write
121 * bit_width - Field width in bits (8, 16, 32, or 64)
122 * value - Pointer to in or out value, must be
123 * a full 64-bit integer
127 * DESCRIPTION: Dispatch an address space or operation region access to
128 * a previously installed handler.
130 ******************************************************************************/
133 acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
134 union acpi_operand_object *field_obj,
136 u32 region_offset, u32 bit_width, u64 *value)
139 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;
144 struct acpi_connection_info *context;
146 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
148 region_obj2 = acpi_ns_get_secondary_object(region_obj);
150 return_ACPI_STATUS(AE_NOT_EXIST);
153 /* Ensure that there is a handler associated with this region */
155 handler_desc = region_obj->region.handler;
158 "No handler for Region [%4.4s] (%p) [%s]",
159 acpi_ut_get_node_name(region_obj->region.node),
161 acpi_ut_get_region_name(region_obj->region.
164 return_ACPI_STATUS(AE_NOT_EXIST);
167 context = handler_desc->address_space.context;
170 * It may be the case that the region has never been initialized.
171 * Some types of regions require special init code
173 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
175 /* This region has not been initialized yet, do it */
177 region_setup = handler_desc->address_space.setup;
180 /* No initialization routine, exit with error */
183 "No init routine for region(%p) [%s]",
185 acpi_ut_get_region_name(region_obj->region.
187 return_ACPI_STATUS(AE_NOT_EXIST);
191 * We must exit the interpreter because the region setup will
192 * potentially execute control methods (for example, the _REG method
195 acpi_ex_exit_interpreter();
197 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
198 context, ®ion_context);
200 /* Re-enter the interpreter */
202 acpi_ex_enter_interpreter();
204 /* Check for failure of the Region Setup */
206 if (ACPI_FAILURE(status)) {
207 ACPI_EXCEPTION((AE_INFO, status,
208 "During region initialization: [%s]",
209 acpi_ut_get_region_name(region_obj->
212 return_ACPI_STATUS(status);
215 /* Region initialization may have been completed by region_setup */
217 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
218 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
220 if (region_obj2->extra.region_context) {
222 /* The handler for this region was already installed */
224 ACPI_FREE(region_context);
227 * Save the returned context for use in all accesses to
228 * this particular region
230 region_obj2->extra.region_context =
236 /* We have everything we need, we can invoke the address space handler */
238 handler = handler_desc->address_space.handler;
240 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
241 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
242 ®ion_obj->region.handler->address_space, handler,
243 ACPI_FORMAT_NATIVE_UINT(region_obj->region.address +
245 acpi_ut_get_region_name(region_obj->region.
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.
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) {
260 /* Get the Connection (resource_template) buffer */
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;
267 if (!(handler_desc->address_space.handler_flags &
268 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
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.
274 acpi_ex_exit_interpreter();
277 /* Call the handler */
279 status = handler(function,
280 (region_obj->region.address + region_offset),
281 bit_width, value, context,
282 region_obj2->extra.region_context);
284 if (ACPI_FAILURE(status)) {
285 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
286 acpi_ut_get_region_name(region_obj->region.
290 if (!(handler_desc->address_space.handler_flags &
291 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
293 * We just returned from a non-default handler, we must re-enter the
296 acpi_ex_enter_interpreter();
299 return_ACPI_STATUS(status);
302 /*******************************************************************************
304 * FUNCTION: acpi_ev_detach_region
306 * PARAMETERS: region_obj - Region Object
307 * acpi_ns_is_locked - Namespace Region Already Locked?
311 * DESCRIPTION: Break the association between the handler and the region
312 * this is a two way association.
314 ******************************************************************************/
317 acpi_ev_detach_region(union acpi_operand_object *region_obj,
318 u8 acpi_ns_is_locked)
320 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;
328 ACPI_FUNCTION_TRACE(ev_detach_region);
330 region_obj2 = acpi_ns_get_secondary_object(region_obj);
334 region_context = ®ion_obj2->extra.region_context;
336 /* Get the address handler from the region object */
338 handler_obj = region_obj->region.handler;
341 /* This region has no handler, all done */
346 /* Find this region in the handler's list */
348 obj_desc = handler_obj->address_space.region_list;
349 last_obj_ptr = &handler_obj->address_space.region_list;
353 /* Is this the correct Region? */
355 if (obj_desc == region_obj) {
356 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
357 "Removing Region %p from address handler %p\n",
358 region_obj, handler_obj));
360 /* This is it, remove it from the handler's list */
362 *last_obj_ptr = obj_desc->region.next;
363 obj_desc->region.next = NULL; /* Must clear field */
365 if (acpi_ns_is_locked) {
367 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
368 if (ACPI_FAILURE(status)) {
373 /* Now stop region accesses by executing the _REG method */
376 acpi_ev_execute_reg_method(region_obj,
377 ACPI_REG_DISCONNECT);
378 if (ACPI_FAILURE(status)) {
379 ACPI_EXCEPTION((AE_INFO, status,
380 "from region _REG, [%s]",
381 acpi_ut_get_region_name
382 (region_obj->region.space_id)));
385 if (acpi_ns_is_locked) {
387 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
388 if (ACPI_FAILURE(status)) {
394 * If the region has been activated, call the setup handler with
395 * the deactivate notification
397 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
398 region_setup = handler_obj->address_space.setup;
400 region_setup(region_obj,
401 ACPI_REGION_DEACTIVATE,
402 handler_obj->address_space.
403 context, region_context);
405 /* Init routine may fail, Just ignore errors */
407 if (ACPI_FAILURE(status)) {
408 ACPI_EXCEPTION((AE_INFO, status,
409 "from region handler - deactivate, [%s]",
410 acpi_ut_get_region_name
415 region_obj->region.flags &=
416 ~(AOPOBJ_SETUP_COMPLETE);
420 * Remove handler reference in the region
422 * NOTE: this doesn't mean that the region goes away, the region
423 * is just inaccessible as indicated to the _REG method
425 * If the region is on the handler's list, this must be the
428 region_obj->region.handler = NULL;
429 acpi_ut_remove_reference(handler_obj);
434 /* Walk the linked list of handlers */
436 last_obj_ptr = &obj_desc->region.next;
437 obj_desc = obj_desc->region.next;
440 /* If we get here, the region was not in the handler's region list */
442 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
443 "Cannot remove region %p from address handler %p\n",
444 region_obj, handler_obj));
449 /*******************************************************************************
451 * FUNCTION: acpi_ev_attach_region
453 * PARAMETERS: handler_obj - Handler Object
454 * region_obj - Region Object
455 * acpi_ns_is_locked - Namespace Region Already Locked?
459 * DESCRIPTION: Create the association between the handler and the region
460 * this is a two way association.
462 ******************************************************************************/
465 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
466 union acpi_operand_object *region_obj,
467 u8 acpi_ns_is_locked)
470 ACPI_FUNCTION_TRACE(ev_attach_region);
472 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.
479 /* Link this region to the front of the handler's list */
481 region_obj->region.next = handler_obj->address_space.region_list;
482 handler_obj->address_space.region_list = region_obj;
484 /* Install the region's handler */
486 if (region_obj->region.handler) {
487 return_ACPI_STATUS(AE_ALREADY_EXISTS);
490 region_obj->region.handler = handler_obj;
491 acpi_ut_add_reference(handler_obj);
493 return_ACPI_STATUS(AE_OK);
496 /*******************************************************************************
498 * FUNCTION: acpi_ev_execute_reg_method
500 * PARAMETERS: region_obj - Region object
501 * function - Passed to _REG: On (1) or Off (0)
505 * DESCRIPTION: Execute _REG method for a region
507 ******************************************************************************/
510 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
512 struct acpi_evaluate_info *info;
513 union acpi_operand_object *args[3];
514 union acpi_operand_object *region_obj2;
517 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
519 region_obj2 = acpi_ns_get_secondary_object(region_obj);
521 return_ACPI_STATUS(AE_NOT_EXIST);
524 if (region_obj2->extra.method_REG == NULL) {
525 return_ACPI_STATUS(AE_OK);
528 /* Allocate and initialize the evaluation information block */
530 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
532 return_ACPI_STATUS(AE_NO_MEMORY);
535 info->prefix_node = region_obj2->extra.method_REG;
536 info->relative_pathname = NULL;
537 info->parameters = args;
538 info->flags = ACPI_IGNORE_RETURN_VALUE;
541 * The _REG method has two arguments:
544 * Operation region space ID Same value as region_obj->Region.space_id
547 * connection status 1 for connecting the handler, 0 for disconnecting
548 * the handler (Passed as a parameter)
551 acpi_ut_create_integer_object((u64)region_obj->region.space_id);
553 status = AE_NO_MEMORY;
557 args[1] = acpi_ut_create_integer_object((u64)function);
559 status = AE_NO_MEMORY;
563 args[2] = NULL; /* Terminate list */
565 /* Execute the method, no return value */
567 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
568 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
570 status = acpi_ns_evaluate(info);
571 acpi_ut_remove_reference(args[1]);
574 acpi_ut_remove_reference(args[0]);
578 return_ACPI_STATUS(status);
581 /*******************************************************************************
583 * FUNCTION: acpi_ev_execute_reg_methods
585 * PARAMETERS: node - Namespace node for the device
586 * space_id - The address space ID
590 * DESCRIPTION: Run all _REG methods for the input Space ID;
591 * Note: assumes namespace is locked, or system init time.
593 ******************************************************************************/
596 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
597 acpi_adr_space_type space_id)
601 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
604 * 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)
609 status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
610 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run,
611 NULL, &space_id, NULL);
613 /* Special case for EC: handle "orphan" _REG methods with no region */
615 if (space_id == ACPI_ADR_SPACE_EC) {
616 acpi_ev_orphan_ec_reg_method(node);
619 return_ACPI_STATUS(status);
622 /*******************************************************************************
624 * FUNCTION: acpi_ev_reg_run
626 * PARAMETERS: walk_namespace callback
628 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
630 ******************************************************************************/
633 acpi_ev_reg_run(acpi_handle obj_handle,
634 u32 level, void *context, void **return_value)
636 union acpi_operand_object *obj_desc;
637 struct acpi_namespace_node *node;
638 acpi_adr_space_type space_id;
641 space_id = *ACPI_CAST_PTR(acpi_adr_space_type, context);
643 /* Convert and validate the device handle */
645 node = acpi_ns_validate_handle(obj_handle);
647 return (AE_BAD_PARAMETER);
651 * We only care about regions.and objects that are allowed to have address
654 if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
658 /* Check for an existing internal object */
660 obj_desc = acpi_ns_get_attached_object(node);
663 /* No object, just exit */
668 /* Object is a Region */
670 if (obj_desc->region.space_id != space_id) {
672 /* This region is for a different address space, just ignore it */
677 status = acpi_ev_execute_reg_method(obj_desc, ACPI_REG_CONNECT);
681 /*******************************************************************************
683 * FUNCTION: acpi_ev_orphan_ec_reg_method
685 * PARAMETERS: ec_device_node - Namespace node for an EC device
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."
697 * 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.
702 * MUTEX: Assumes the namespace is locked
704 ******************************************************************************/
707 acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node)
709 acpi_handle reg_method;
710 struct acpi_namespace_node *next_node;
712 struct acpi_object_list args;
713 union acpi_object objects[2];
715 ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method);
717 if (!ec_device_node) {
721 /* Namespace is currently locked, must release */
723 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
725 /* Get a handle to a _REG method immediately under the EC device */
727 status = acpi_get_handle(ec_device_node, METHOD_NAME__REG, ®_method);
728 if (ACPI_FAILURE(status)) {
729 goto exit; /* There is no _REG method present */
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
737 * execute the _REG method with the embedded_control space_ID argument.
739 next_node = acpi_ns_get_next_node(ec_device_node, NULL);
741 if ((next_node->type == ACPI_TYPE_REGION) &&
742 (next_node->object) &&
743 (next_node->object->region.space_id == ACPI_ADR_SPACE_EC)) {
744 goto exit; /* Do not execute the _REG */
747 next_node = acpi_ns_get_next_node(ec_device_node, next_node);
750 /* Evaluate the _REG(embedded_control,Connect) method */
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;
759 status = acpi_evaluate_object(reg_method, NULL, &args, NULL);
762 /* We ignore all errors from above, don't care */
764 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);