1 /******************************************************************************
3 * Module Name: evregion - Operation Region support
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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
;
145 acpi_physical_address address
;
147 ACPI_FUNCTION_TRACE(ev_address_space_dispatch
);
149 region_obj2
= acpi_ns_get_secondary_object(region_obj
);
151 return_ACPI_STATUS(AE_NOT_EXIST
);
154 /* Ensure that there is a handler associated with this region */
156 handler_desc
= region_obj
->region
.handler
;
159 "No handler for Region [%4.4s] (%p) [%s]",
160 acpi_ut_get_node_name(region_obj
->region
.node
),
162 acpi_ut_get_region_name(region_obj
->region
.
165 return_ACPI_STATUS(AE_NOT_EXIST
);
168 context
= handler_desc
->address_space
.context
;
171 * It may be the case that the region has never been initialized.
172 * Some types of regions require special init code
174 if (!(region_obj
->region
.flags
& AOPOBJ_SETUP_COMPLETE
)) {
176 /* This region has not been initialized yet, do it */
178 region_setup
= handler_desc
->address_space
.setup
;
181 /* No initialization routine, exit with error */
184 "No init routine for region(%p) [%s]",
186 acpi_ut_get_region_name(region_obj
->region
.
188 return_ACPI_STATUS(AE_NOT_EXIST
);
192 * We must exit the interpreter because the region setup will
193 * potentially execute control methods (for example, the _REG method
196 acpi_ex_exit_interpreter();
198 status
= region_setup(region_obj
, ACPI_REGION_ACTIVATE
,
199 context
, ®ion_context
);
201 /* Re-enter the interpreter */
203 acpi_ex_enter_interpreter();
205 /* Check for failure of the Region Setup */
207 if (ACPI_FAILURE(status
)) {
208 ACPI_EXCEPTION((AE_INFO
, status
,
209 "During region initialization: [%s]",
210 acpi_ut_get_region_name(region_obj
->
213 return_ACPI_STATUS(status
);
216 /* Region initialization may have been completed by region_setup */
218 if (!(region_obj
->region
.flags
& AOPOBJ_SETUP_COMPLETE
)) {
219 region_obj
->region
.flags
|= AOPOBJ_SETUP_COMPLETE
;
222 * Save the returned context for use in all accesses to
223 * the handler for this particular region
225 if (!(region_obj2
->extra
.region_context
)) {
226 region_obj2
->extra
.region_context
=
232 /* We have everything we need, we can invoke the address space handler */
234 handler
= handler_desc
->address_space
.handler
;
235 address
= (region_obj
->region
.address
+ region_offset
);
238 * Special handling for generic_serial_bus and general_purpose_io:
239 * There are three extra parameters that must be passed to the
240 * handler via the context:
241 * 1) Connection buffer, a resource template from Connection() op
242 * 2) Length of the above buffer
243 * 3) Actual access length from the access_as() op
245 * In addition, for general_purpose_io, the Address and bit_width fields
246 * are defined as follows:
247 * 1) Address is the pin number index of the field (bit offset from
248 * the previous Connection)
249 * 2) bit_width is the actual bit length of the field (number of pins)
251 if ((region_obj
->region
.space_id
== ACPI_ADR_SPACE_GSBUS
) &&
252 context
&& field_obj
) {
254 /* Get the Connection (resource_template) buffer */
256 context
->connection
= field_obj
->field
.resource_buffer
;
257 context
->length
= field_obj
->field
.resource_length
;
258 context
->access_length
= field_obj
->field
.access_length
;
260 if ((region_obj
->region
.space_id
== ACPI_ADR_SPACE_GPIO
) &&
261 context
&& field_obj
) {
263 /* Get the Connection (resource_template) buffer */
265 context
->connection
= field_obj
->field
.resource_buffer
;
266 context
->length
= field_obj
->field
.resource_length
;
267 context
->access_length
= field_obj
->field
.access_length
;
268 address
= field_obj
->field
.pin_number_index
;
269 bit_width
= field_obj
->field
.bit_length
;
272 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
273 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
274 ®ion_obj
->region
.handler
->address_space
, handler
,
275 ACPI_FORMAT_UINT64(address
),
276 acpi_ut_get_region_name(region_obj
->region
.
279 if (!(handler_desc
->address_space
.handler_flags
&
280 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
)) {
282 * For handlers other than the default (supplied) handlers, we must
283 * exit the interpreter because the handler *might* block -- we don't
284 * know what it will do, so we can't hold the lock on the intepreter.
286 acpi_ex_exit_interpreter();
289 /* Call the handler */
291 status
= handler(function
, address
, bit_width
, value
, context
,
292 region_obj2
->extra
.region_context
);
294 if (ACPI_FAILURE(status
)) {
295 ACPI_EXCEPTION((AE_INFO
, status
, "Returned by Handler for [%s]",
296 acpi_ut_get_region_name(region_obj
->region
.
300 if (!(handler_desc
->address_space
.handler_flags
&
301 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
)) {
303 * We just returned from a non-default handler, we must re-enter the
306 acpi_ex_enter_interpreter();
309 return_ACPI_STATUS(status
);
312 /*******************************************************************************
314 * FUNCTION: acpi_ev_detach_region
316 * PARAMETERS: region_obj - Region Object
317 * acpi_ns_is_locked - Namespace Region Already Locked?
321 * DESCRIPTION: Break the association between the handler and the region
322 * this is a two way association.
324 ******************************************************************************/
327 acpi_ev_detach_region(union acpi_operand_object
*region_obj
,
328 u8 acpi_ns_is_locked
)
330 union acpi_operand_object
*handler_obj
;
331 union acpi_operand_object
*obj_desc
;
332 union acpi_operand_object
*start_desc
;
333 union acpi_operand_object
**last_obj_ptr
;
334 acpi_adr_space_setup region_setup
;
335 void **region_context
;
336 union acpi_operand_object
*region_obj2
;
339 ACPI_FUNCTION_TRACE(ev_detach_region
);
341 region_obj2
= acpi_ns_get_secondary_object(region_obj
);
345 region_context
= ®ion_obj2
->extra
.region_context
;
347 /* Get the address handler from the region object */
349 handler_obj
= region_obj
->region
.handler
;
352 /* This region has no handler, all done */
357 /* Find this region in the handler's list */
359 obj_desc
= handler_obj
->address_space
.region_list
;
360 start_desc
= obj_desc
;
361 last_obj_ptr
= &handler_obj
->address_space
.region_list
;
365 /* Is this the correct Region? */
367 if (obj_desc
== region_obj
) {
368 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
369 "Removing Region %p from address handler %p\n",
370 region_obj
, handler_obj
));
372 /* This is it, remove it from the handler's list */
374 *last_obj_ptr
= obj_desc
->region
.next
;
375 obj_desc
->region
.next
= NULL
; /* Must clear field */
377 if (acpi_ns_is_locked
) {
379 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
380 if (ACPI_FAILURE(status
)) {
385 /* Now stop region accesses by executing the _REG method */
388 acpi_ev_execute_reg_method(region_obj
,
389 ACPI_REG_DISCONNECT
);
390 if (ACPI_FAILURE(status
)) {
391 ACPI_EXCEPTION((AE_INFO
, status
,
392 "from region _REG, [%s]",
393 acpi_ut_get_region_name
394 (region_obj
->region
.space_id
)));
397 if (acpi_ns_is_locked
) {
399 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
400 if (ACPI_FAILURE(status
)) {
406 * If the region has been activated, call the setup handler with
407 * the deactivate notification
409 if (region_obj
->region
.flags
& AOPOBJ_SETUP_COMPLETE
) {
410 region_setup
= handler_obj
->address_space
.setup
;
412 region_setup(region_obj
,
413 ACPI_REGION_DEACTIVATE
,
414 handler_obj
->address_space
.
415 context
, region_context
);
418 * region_context should have been released by the deactivate
419 * operation. We don't need access to it anymore here.
421 if (region_context
) {
422 *region_context
= NULL
;
425 /* Init routine may fail, Just ignore errors */
427 if (ACPI_FAILURE(status
)) {
428 ACPI_EXCEPTION((AE_INFO
, status
,
429 "from region handler - deactivate, [%s]",
430 acpi_ut_get_region_name
435 region_obj
->region
.flags
&=
436 ~(AOPOBJ_SETUP_COMPLETE
);
440 * Remove handler reference in the region
442 * NOTE: this doesn't mean that the region goes away, the region
443 * is just inaccessible as indicated to the _REG method
445 * If the region is on the handler's list, this must be the
448 region_obj
->region
.handler
= NULL
;
449 acpi_ut_remove_reference(handler_obj
);
454 /* Walk the linked list of handlers */
456 last_obj_ptr
= &obj_desc
->region
.next
;
457 obj_desc
= obj_desc
->region
.next
;
459 /* Prevent infinite loop if list is corrupted */
461 if (obj_desc
== start_desc
) {
463 "Circular handler list in region object %p",
469 /* If we get here, the region was not in the handler's region list */
471 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
472 "Cannot remove region %p from address handler %p\n",
473 region_obj
, handler_obj
));
478 /*******************************************************************************
480 * FUNCTION: acpi_ev_attach_region
482 * PARAMETERS: handler_obj - Handler Object
483 * region_obj - Region Object
484 * acpi_ns_is_locked - Namespace Region Already Locked?
488 * DESCRIPTION: Create the association between the handler and the region
489 * this is a two way association.
491 ******************************************************************************/
494 acpi_ev_attach_region(union acpi_operand_object
*handler_obj
,
495 union acpi_operand_object
*region_obj
,
496 u8 acpi_ns_is_locked
)
499 ACPI_FUNCTION_TRACE(ev_attach_region
);
501 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
502 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
503 acpi_ut_get_node_name(region_obj
->region
.node
),
504 region_obj
, handler_obj
,
505 acpi_ut_get_region_name(region_obj
->region
.
508 /* Link this region to the front of the handler's list */
510 region_obj
->region
.next
= handler_obj
->address_space
.region_list
;
511 handler_obj
->address_space
.region_list
= region_obj
;
513 /* Install the region's handler */
515 if (region_obj
->region
.handler
) {
516 return_ACPI_STATUS(AE_ALREADY_EXISTS
);
519 region_obj
->region
.handler
= handler_obj
;
520 acpi_ut_add_reference(handler_obj
);
522 return_ACPI_STATUS(AE_OK
);
525 /*******************************************************************************
527 * FUNCTION: acpi_ev_execute_reg_method
529 * PARAMETERS: region_obj - Region object
530 * function - Passed to _REG: On (1) or Off (0)
534 * DESCRIPTION: Execute _REG method for a region
536 ******************************************************************************/
539 acpi_ev_execute_reg_method(union acpi_operand_object
*region_obj
, u32 function
)
541 struct acpi_evaluate_info
*info
;
542 union acpi_operand_object
*args
[3];
543 union acpi_operand_object
*region_obj2
;
546 ACPI_FUNCTION_TRACE(ev_execute_reg_method
);
548 region_obj2
= acpi_ns_get_secondary_object(region_obj
);
550 return_ACPI_STATUS(AE_NOT_EXIST
);
553 if (region_obj2
->extra
.method_REG
== NULL
) {
554 return_ACPI_STATUS(AE_OK
);
557 /* Allocate and initialize the evaluation information block */
559 info
= ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info
));
561 return_ACPI_STATUS(AE_NO_MEMORY
);
564 info
->prefix_node
= region_obj2
->extra
.method_REG
;
565 info
->relative_pathname
= NULL
;
566 info
->parameters
= args
;
567 info
->flags
= ACPI_IGNORE_RETURN_VALUE
;
570 * The _REG method has two arguments:
573 * Operation region space ID Same value as region_obj->Region.space_id
576 * connection status 1 for connecting the handler, 0 for disconnecting
577 * the handler (Passed as a parameter)
580 acpi_ut_create_integer_object((u64
)region_obj
->region
.space_id
);
582 status
= AE_NO_MEMORY
;
586 args
[1] = acpi_ut_create_integer_object((u64
)function
);
588 status
= AE_NO_MEMORY
;
592 args
[2] = NULL
; /* Terminate list */
594 /* Execute the method, no return value */
596 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
597 (ACPI_TYPE_METHOD
, info
->prefix_node
, NULL
));
599 status
= acpi_ns_evaluate(info
);
600 acpi_ut_remove_reference(args
[1]);
603 acpi_ut_remove_reference(args
[0]);
607 return_ACPI_STATUS(status
);
610 /*******************************************************************************
612 * FUNCTION: acpi_ev_execute_reg_methods
614 * PARAMETERS: node - Namespace node for the device
615 * space_id - The address space ID
619 * DESCRIPTION: Run all _REG methods for the input Space ID;
620 * Note: assumes namespace is locked, or system init time.
622 ******************************************************************************/
625 acpi_ev_execute_reg_methods(struct acpi_namespace_node
*node
,
626 acpi_adr_space_type space_id
)
629 struct acpi_reg_walk_info info
;
631 ACPI_FUNCTION_TRACE(ev_execute_reg_methods
);
633 info
.space_id
= space_id
;
634 info
.reg_run_count
= 0;
636 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES
,
637 " Running _REG methods for SpaceId %s\n",
638 acpi_ut_get_region_name(info
.space_id
)));
641 * Run all _REG methods for all Operation Regions for this space ID. This
642 * is a separate walk in order to handle any interdependencies between
643 * regions and _REG methods. (i.e. handlers must be installed for all
644 * regions of this Space ID before we can run any _REG methods)
646 status
= acpi_ns_walk_namespace(ACPI_TYPE_ANY
, node
, ACPI_UINT32_MAX
,
647 ACPI_NS_WALK_UNLOCK
, acpi_ev_reg_run
,
650 /* Special case for EC: handle "orphan" _REG methods with no region */
652 if (space_id
== ACPI_ADR_SPACE_EC
) {
653 acpi_ev_orphan_ec_reg_method(node
);
656 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES
,
657 " Executed %u _REG methods for SpaceId %s\n",
659 acpi_ut_get_region_name(info
.space_id
)));
661 return_ACPI_STATUS(status
);
664 /*******************************************************************************
666 * FUNCTION: acpi_ev_reg_run
668 * PARAMETERS: walk_namespace callback
670 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
672 ******************************************************************************/
675 acpi_ev_reg_run(acpi_handle obj_handle
,
676 u32 level
, void *context
, void **return_value
)
678 union acpi_operand_object
*obj_desc
;
679 struct acpi_namespace_node
*node
;
681 struct acpi_reg_walk_info
*info
;
683 info
= ACPI_CAST_PTR(struct acpi_reg_walk_info
, context
);
685 /* Convert and validate the device handle */
687 node
= acpi_ns_validate_handle(obj_handle
);
689 return (AE_BAD_PARAMETER
);
693 * We only care about regions.and objects that are allowed to have address
696 if ((node
->type
!= ACPI_TYPE_REGION
) && (node
!= acpi_gbl_root_node
)) {
700 /* Check for an existing internal object */
702 obj_desc
= acpi_ns_get_attached_object(node
);
705 /* No object, just exit */
710 /* Object is a Region */
712 if (obj_desc
->region
.space_id
!= info
->space_id
) {
714 /* This region is for a different address space, just ignore it */
719 info
->reg_run_count
++;
720 status
= acpi_ev_execute_reg_method(obj_desc
, ACPI_REG_CONNECT
);
724 /*******************************************************************************
726 * FUNCTION: acpi_ev_orphan_ec_reg_method
728 * PARAMETERS: ec_device_node - Namespace node for an EC device
732 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
733 * device. This is a _REG method that has no corresponding region
734 * within the EC device scope. The orphan _REG method appears to
735 * have been enabled by the description of the ECDT in the ACPI
736 * specification: "The availability of the region space can be
737 * detected by providing a _REG method object underneath the
738 * Embedded Controller device."
740 * To quickly access the EC device, we use the ec_device_node used
741 * during EC handler installation. Otherwise, we would need to
742 * perform a time consuming namespace walk, executing _HID
743 * methods to find the EC device.
745 * MUTEX: Assumes the namespace is locked
747 ******************************************************************************/
750 acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node
*ec_device_node
)
752 acpi_handle reg_method
;
753 struct acpi_namespace_node
*next_node
;
755 struct acpi_object_list args
;
756 union acpi_object objects
[2];
758 ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method
);
760 if (!ec_device_node
) {
764 /* Namespace is currently locked, must release */
766 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
768 /* Get a handle to a _REG method immediately under the EC device */
770 status
= acpi_get_handle(ec_device_node
, METHOD_NAME__REG
, ®_method
);
771 if (ACPI_FAILURE(status
)) {
772 goto exit
; /* There is no _REG method present */
776 * Execute the _REG method only if there is no Operation Region in
777 * this scope with the Embedded Controller space ID. Otherwise, it
778 * will already have been executed. Note, this allows for Regions
779 * with other space IDs to be present; but the code below will then
780 * execute the _REG method with the embedded_control space_ID argument.
782 next_node
= acpi_ns_get_next_node(ec_device_node
, NULL
);
784 if ((next_node
->type
== ACPI_TYPE_REGION
) &&
785 (next_node
->object
) &&
786 (next_node
->object
->region
.space_id
== ACPI_ADR_SPACE_EC
)) {
787 goto exit
; /* Do not execute the _REG */
790 next_node
= acpi_ns_get_next_node(ec_device_node
, next_node
);
793 /* Evaluate the _REG(embedded_control,Connect) method */
796 args
.pointer
= objects
;
797 objects
[0].type
= ACPI_TYPE_INTEGER
;
798 objects
[0].integer
.value
= ACPI_ADR_SPACE_EC
;
799 objects
[1].type
= ACPI_TYPE_INTEGER
;
800 objects
[1].integer
.value
= ACPI_REG_CONNECT
;
802 status
= acpi_evaluate_object(reg_method
, NULL
, &args
, NULL
);
805 /* We ignore all errors from above, don't care */
807 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);