1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: evregion - Operation Region support
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
16 #define _COMPONENT ACPI_EVENTS
17 ACPI_MODULE_NAME("evregion")
19 extern u8 acpi_gbl_default_address_spaces
[];
21 /* Local prototypes */
24 acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node
*device_node
,
25 acpi_adr_space_type space_id
);
28 acpi_ev_reg_run(acpi_handle obj_handle
,
29 u32 level
, void *context
, void **return_value
);
31 /*******************************************************************************
33 * FUNCTION: acpi_ev_initialize_op_regions
39 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
40 * an installed default region handler.
42 ******************************************************************************/
44 acpi_status
acpi_ev_initialize_op_regions(void)
49 ACPI_FUNCTION_TRACE(ev_initialize_op_regions
);
51 status
= acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
52 if (ACPI_FAILURE(status
)) {
53 return_ACPI_STATUS(status
);
56 /* Run the _REG methods for op_regions in each default address space */
58 for (i
= 0; i
< ACPI_NUM_DEFAULT_SPACES
; i
++) {
60 * Make sure the installed handler is the DEFAULT handler. If not the
61 * default, the _REG methods will have already been run (when the
62 * handler was installed)
64 if (acpi_ev_has_default_handler(acpi_gbl_root_node
,
65 acpi_gbl_default_address_spaces
67 acpi_ev_execute_reg_methods(acpi_gbl_root_node
,
68 acpi_gbl_default_address_spaces
69 [i
], ACPI_REG_CONNECT
);
73 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
74 return_ACPI_STATUS(status
);
77 /*******************************************************************************
79 * FUNCTION: acpi_ev_address_space_dispatch
81 * PARAMETERS: region_obj - Internal region object
82 * field_obj - Corresponding field. Can be NULL.
83 * function - Read or Write operation
84 * region_offset - Where in the region to read or write
85 * bit_width - Field width in bits (8, 16, 32, or 64)
86 * value - Pointer to in or out value, must be
87 * a full 64-bit integer
91 * DESCRIPTION: Dispatch an address space or operation region access to
92 * a previously installed handler.
94 * NOTE: During early initialization, we always install the default region
95 * handlers for Memory, I/O and PCI_Config. This ensures that these operation
96 * region address spaces are always available as per the ACPI specification.
97 * This is especially needed in order to support the execution of
98 * module-level AML code during loading of the ACPI tables.
100 ******************************************************************************/
103 acpi_ev_address_space_dispatch(union acpi_operand_object
*region_obj
,
104 union acpi_operand_object
*field_obj
,
106 u32 region_offset
, u32 bit_width
, u64
*value
)
109 acpi_adr_space_handler handler
;
110 acpi_adr_space_setup region_setup
;
111 union acpi_operand_object
*handler_desc
;
112 union acpi_operand_object
*region_obj2
;
113 void *region_context
= NULL
;
114 struct acpi_connection_info
*context
;
115 acpi_physical_address address
;
117 ACPI_FUNCTION_TRACE(ev_address_space_dispatch
);
119 region_obj2
= acpi_ns_get_secondary_object(region_obj
);
121 return_ACPI_STATUS(AE_NOT_EXIST
);
124 /* Ensure that there is a handler associated with this region */
126 handler_desc
= region_obj
->region
.handler
;
129 "No handler for Region [%4.4s] (%p) [%s]",
130 acpi_ut_get_node_name(region_obj
->region
.node
),
132 acpi_ut_get_region_name(region_obj
->region
.
135 return_ACPI_STATUS(AE_NOT_EXIST
);
138 context
= handler_desc
->address_space
.context
;
141 * It may be the case that the region has never been initialized.
142 * Some types of regions require special init code
144 if (!(region_obj
->region
.flags
& AOPOBJ_SETUP_COMPLETE
)) {
146 /* This region has not been initialized yet, do it */
148 region_setup
= handler_desc
->address_space
.setup
;
151 /* No initialization routine, exit with error */
154 "No init routine for region(%p) [%s]",
156 acpi_ut_get_region_name(region_obj
->region
.
158 return_ACPI_STATUS(AE_NOT_EXIST
);
162 * We must exit the interpreter because the region setup will
163 * potentially execute control methods (for example, the _REG method
166 acpi_ex_exit_interpreter();
168 status
= region_setup(region_obj
, ACPI_REGION_ACTIVATE
,
169 context
, ®ion_context
);
171 /* Re-enter the interpreter */
173 acpi_ex_enter_interpreter();
175 /* Check for failure of the Region Setup */
177 if (ACPI_FAILURE(status
)) {
178 ACPI_EXCEPTION((AE_INFO
, status
,
179 "During region initialization: [%s]",
180 acpi_ut_get_region_name(region_obj
->
183 return_ACPI_STATUS(status
);
186 /* Region initialization may have been completed by region_setup */
188 if (!(region_obj
->region
.flags
& AOPOBJ_SETUP_COMPLETE
)) {
189 region_obj
->region
.flags
|= AOPOBJ_SETUP_COMPLETE
;
192 * Save the returned context for use in all accesses to
193 * the handler for this particular region
195 if (!(region_obj2
->extra
.region_context
)) {
196 region_obj2
->extra
.region_context
=
202 /* We have everything we need, we can invoke the address space handler */
204 handler
= handler_desc
->address_space
.handler
;
205 address
= (region_obj
->region
.address
+ region_offset
);
208 * Special handling for generic_serial_bus and general_purpose_io:
209 * There are three extra parameters that must be passed to the
210 * handler via the context:
211 * 1) Connection buffer, a resource template from Connection() op
212 * 2) Length of the above buffer
213 * 3) Actual access length from the access_as() op
215 * In addition, for general_purpose_io, the Address and bit_width fields
216 * are defined as follows:
217 * 1) Address is the pin number index of the field (bit offset from
218 * the previous Connection)
219 * 2) bit_width is the actual bit length of the field (number of pins)
221 if ((region_obj
->region
.space_id
== ACPI_ADR_SPACE_GSBUS
) &&
222 context
&& field_obj
) {
224 /* Get the Connection (resource_template) buffer */
226 context
->connection
= field_obj
->field
.resource_buffer
;
227 context
->length
= field_obj
->field
.resource_length
;
228 context
->access_length
= field_obj
->field
.access_length
;
230 if ((region_obj
->region
.space_id
== ACPI_ADR_SPACE_GPIO
) &&
231 context
&& field_obj
) {
233 /* Get the Connection (resource_template) buffer */
235 context
->connection
= field_obj
->field
.resource_buffer
;
236 context
->length
= field_obj
->field
.resource_length
;
237 context
->access_length
= field_obj
->field
.access_length
;
238 address
= field_obj
->field
.pin_number_index
;
239 bit_width
= field_obj
->field
.bit_length
;
242 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
243 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
244 ®ion_obj
->region
.handler
->address_space
, handler
,
245 ACPI_FORMAT_UINT64(address
),
246 acpi_ut_get_region_name(region_obj
->region
.
249 if (!(handler_desc
->address_space
.handler_flags
&
250 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
)) {
252 * For handlers other than the default (supplied) handlers, we must
253 * exit the interpreter because the handler *might* block -- we don't
254 * know what it will do, so we can't hold the lock on the interpreter.
256 acpi_ex_exit_interpreter();
259 /* Call the handler */
261 status
= handler(function
, address
, bit_width
, value
, context
,
262 region_obj2
->extra
.region_context
);
264 if (ACPI_FAILURE(status
)) {
265 ACPI_EXCEPTION((AE_INFO
, status
, "Returned by Handler for [%s]",
266 acpi_ut_get_region_name(region_obj
->region
.
270 * Special case for an EC timeout. These are seen so frequently
271 * that an additional error message is helpful
273 if ((region_obj
->region
.space_id
== ACPI_ADR_SPACE_EC
) &&
274 (status
== AE_TIME
)) {
276 "Timeout from EC hardware or EC device driver"));
280 if (!(handler_desc
->address_space
.handler_flags
&
281 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED
)) {
283 * We just returned from a non-default handler, we must re-enter the
286 acpi_ex_enter_interpreter();
289 return_ACPI_STATUS(status
);
292 /*******************************************************************************
294 * FUNCTION: acpi_ev_detach_region
296 * PARAMETERS: region_obj - Region Object
297 * acpi_ns_is_locked - Namespace Region Already Locked?
301 * DESCRIPTION: Break the association between the handler and the region
302 * this is a two way association.
304 ******************************************************************************/
307 acpi_ev_detach_region(union acpi_operand_object
*region_obj
,
308 u8 acpi_ns_is_locked
)
310 union acpi_operand_object
*handler_obj
;
311 union acpi_operand_object
*obj_desc
;
312 union acpi_operand_object
*start_desc
;
313 union acpi_operand_object
**last_obj_ptr
;
314 acpi_adr_space_setup region_setup
;
315 void **region_context
;
316 union acpi_operand_object
*region_obj2
;
319 ACPI_FUNCTION_TRACE(ev_detach_region
);
321 region_obj2
= acpi_ns_get_secondary_object(region_obj
);
325 region_context
= ®ion_obj2
->extra
.region_context
;
327 /* Get the address handler from the region object */
329 handler_obj
= region_obj
->region
.handler
;
332 /* This region has no handler, all done */
337 /* Find this region in the handler's list */
339 obj_desc
= handler_obj
->address_space
.region_list
;
340 start_desc
= obj_desc
;
341 last_obj_ptr
= &handler_obj
->address_space
.region_list
;
345 /* Is this the correct Region? */
347 if (obj_desc
== region_obj
) {
348 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
349 "Removing Region %p from address handler %p\n",
350 region_obj
, handler_obj
));
352 /* This is it, remove it from the handler's list */
354 *last_obj_ptr
= obj_desc
->region
.next
;
355 obj_desc
->region
.next
= NULL
; /* Must clear field */
357 if (acpi_ns_is_locked
) {
359 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
360 if (ACPI_FAILURE(status
)) {
365 /* Now stop region accesses by executing the _REG method */
368 acpi_ev_execute_reg_method(region_obj
,
369 ACPI_REG_DISCONNECT
);
370 if (ACPI_FAILURE(status
)) {
371 ACPI_EXCEPTION((AE_INFO
, status
,
372 "from region _REG, [%s]",
373 acpi_ut_get_region_name
374 (region_obj
->region
.space_id
)));
377 if (acpi_ns_is_locked
) {
379 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);
380 if (ACPI_FAILURE(status
)) {
386 * If the region has been activated, call the setup handler with
387 * the deactivate notification
389 if (region_obj
->region
.flags
& AOPOBJ_SETUP_COMPLETE
) {
390 region_setup
= handler_obj
->address_space
.setup
;
392 region_setup(region_obj
,
393 ACPI_REGION_DEACTIVATE
,
394 handler_obj
->address_space
.
395 context
, region_context
);
398 * region_context should have been released by the deactivate
399 * operation. We don't need access to it anymore here.
401 if (region_context
) {
402 *region_context
= NULL
;
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
;
439 /* Prevent infinite loop if list is corrupted */
441 if (obj_desc
== start_desc
) {
443 "Circular handler list in region object %p",
449 /* If we get here, the region was not in the handler's region list */
451 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
452 "Cannot remove region %p from address handler %p\n",
453 region_obj
, handler_obj
));
458 /*******************************************************************************
460 * FUNCTION: acpi_ev_attach_region
462 * PARAMETERS: handler_obj - Handler Object
463 * region_obj - Region Object
464 * acpi_ns_is_locked - Namespace Region Already Locked?
468 * DESCRIPTION: Create the association between the handler and the region
469 * this is a two way association.
471 ******************************************************************************/
474 acpi_ev_attach_region(union acpi_operand_object
*handler_obj
,
475 union acpi_operand_object
*region_obj
,
476 u8 acpi_ns_is_locked
)
479 ACPI_FUNCTION_TRACE(ev_attach_region
);
481 /* Install the region's handler */
483 if (region_obj
->region
.handler
) {
484 return_ACPI_STATUS(AE_ALREADY_EXISTS
);
487 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION
,
488 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
489 acpi_ut_get_node_name(region_obj
->region
.node
),
490 region_obj
, handler_obj
,
491 acpi_ut_get_region_name(region_obj
->region
.
494 /* Link this region to the front of the handler's list */
496 region_obj
->region
.next
= handler_obj
->address_space
.region_list
;
497 handler_obj
->address_space
.region_list
= region_obj
;
498 region_obj
->region
.handler
= handler_obj
;
499 acpi_ut_add_reference(handler_obj
);
501 return_ACPI_STATUS(AE_OK
);
504 /*******************************************************************************
506 * FUNCTION: acpi_ev_execute_reg_method
508 * PARAMETERS: region_obj - Region object
509 * function - Passed to _REG: On (1) or Off (0)
513 * DESCRIPTION: Execute _REG method for a region
515 ******************************************************************************/
518 acpi_ev_execute_reg_method(union acpi_operand_object
*region_obj
, u32 function
)
520 struct acpi_evaluate_info
*info
;
521 union acpi_operand_object
*args
[3];
522 union acpi_operand_object
*region_obj2
;
523 const acpi_name
*reg_name_ptr
=
524 ACPI_CAST_PTR(acpi_name
, METHOD_NAME__REG
);
525 struct acpi_namespace_node
*method_node
;
526 struct acpi_namespace_node
*node
;
529 ACPI_FUNCTION_TRACE(ev_execute_reg_method
);
531 if (!acpi_gbl_namespace_initialized
||
532 region_obj
->region
.handler
== NULL
) {
533 return_ACPI_STATUS(AE_OK
);
536 region_obj2
= acpi_ns_get_secondary_object(region_obj
);
538 return_ACPI_STATUS(AE_NOT_EXIST
);
542 * Find any "_REG" method associated with this region definition.
543 * The method should always be updated as this function may be
544 * invoked after a namespace change.
546 node
= region_obj
->region
.node
->parent
;
548 acpi_ns_search_one_scope(*reg_name_ptr
, node
, ACPI_TYPE_METHOD
,
550 if (ACPI_SUCCESS(status
)) {
552 * The _REG method is optional and there can be only one per
553 * region definition. This will be executed when the handler is
554 * attached or removed.
556 region_obj2
->extra
.method_REG
= method_node
;
558 if (region_obj2
->extra
.method_REG
== NULL
) {
559 return_ACPI_STATUS(AE_OK
);
562 /* _REG(DISCONNECT) should be paired with _REG(CONNECT) */
564 if ((function
== ACPI_REG_CONNECT
&&
565 region_obj
->common
.flags
& AOPOBJ_REG_CONNECTED
) ||
566 (function
== ACPI_REG_DISCONNECT
&&
567 !(region_obj
->common
.flags
& AOPOBJ_REG_CONNECTED
))) {
568 return_ACPI_STATUS(AE_OK
);
571 /* Allocate and initialize the evaluation information block */
573 info
= ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info
));
575 return_ACPI_STATUS(AE_NO_MEMORY
);
578 info
->prefix_node
= region_obj2
->extra
.method_REG
;
579 info
->relative_pathname
= NULL
;
580 info
->parameters
= args
;
581 info
->flags
= ACPI_IGNORE_RETURN_VALUE
;
584 * The _REG method has two arguments:
587 * Operation region space ID Same value as region_obj->Region.space_id
590 * connection status 1 for connecting the handler, 0 for disconnecting
591 * the handler (Passed as a parameter)
594 acpi_ut_create_integer_object((u64
)region_obj
->region
.space_id
);
596 status
= AE_NO_MEMORY
;
600 args
[1] = acpi_ut_create_integer_object((u64
)function
);
602 status
= AE_NO_MEMORY
;
606 args
[2] = NULL
; /* Terminate list */
608 /* Execute the method, no return value */
610 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
611 (ACPI_TYPE_METHOD
, info
->prefix_node
, NULL
));
613 status
= acpi_ns_evaluate(info
);
614 acpi_ut_remove_reference(args
[1]);
616 if (ACPI_FAILURE(status
)) {
620 if (function
== ACPI_REG_CONNECT
) {
621 region_obj
->common
.flags
|= AOPOBJ_REG_CONNECTED
;
623 region_obj
->common
.flags
&= ~AOPOBJ_REG_CONNECTED
;
627 acpi_ut_remove_reference(args
[0]);
631 return_ACPI_STATUS(status
);
634 /*******************************************************************************
636 * FUNCTION: acpi_ev_execute_reg_methods
638 * PARAMETERS: node - Namespace node for the device
639 * space_id - The address space ID
640 * function - Passed to _REG: On (1) or Off (0)
644 * DESCRIPTION: Run all _REG methods for the input Space ID;
645 * Note: assumes namespace is locked, or system init time.
647 ******************************************************************************/
650 acpi_ev_execute_reg_methods(struct acpi_namespace_node
*node
,
651 acpi_adr_space_type space_id
, u32 function
)
653 struct acpi_reg_walk_info info
;
655 ACPI_FUNCTION_TRACE(ev_execute_reg_methods
);
658 * These address spaces do not need a call to _REG, since the ACPI
659 * specification defines them as: "must always be accessible". Since
660 * they never change state (never become unavailable), no need to ever
661 * call _REG on them. Also, a data_table is not a "real" address space,
662 * so do not call _REG. September 2018.
664 if ((space_id
== ACPI_ADR_SPACE_SYSTEM_MEMORY
) ||
665 (space_id
== ACPI_ADR_SPACE_SYSTEM_IO
) ||
666 (space_id
== ACPI_ADR_SPACE_DATA_TABLE
)) {
670 info
.space_id
= space_id
;
671 info
.function
= function
;
672 info
.reg_run_count
= 0;
674 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES
,
675 " Running _REG methods for SpaceId %s\n",
676 acpi_ut_get_region_name(info
.space_id
)));
679 * Run all _REG methods for all Operation Regions for this space ID. This
680 * is a separate walk in order to handle any interdependencies between
681 * regions and _REG methods. (i.e. handlers must be installed for all
682 * regions of this Space ID before we can run any _REG methods)
684 (void)acpi_ns_walk_namespace(ACPI_TYPE_ANY
, node
, ACPI_UINT32_MAX
,
685 ACPI_NS_WALK_UNLOCK
, acpi_ev_reg_run
, NULL
,
689 * Special case for EC and GPIO: handle "orphan" _REG methods with
692 if (space_id
== ACPI_ADR_SPACE_EC
|| space_id
== ACPI_ADR_SPACE_GPIO
) {
693 acpi_ev_execute_orphan_reg_method(node
, space_id
);
696 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES
,
697 " Executed %u _REG methods for SpaceId %s\n",
699 acpi_ut_get_region_name(info
.space_id
)));
704 /*******************************************************************************
706 * FUNCTION: acpi_ev_reg_run
708 * PARAMETERS: walk_namespace callback
710 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
712 ******************************************************************************/
715 acpi_ev_reg_run(acpi_handle obj_handle
,
716 u32 level
, void *context
, void **return_value
)
718 union acpi_operand_object
*obj_desc
;
719 struct acpi_namespace_node
*node
;
721 struct acpi_reg_walk_info
*info
;
723 info
= ACPI_CAST_PTR(struct acpi_reg_walk_info
, context
);
725 /* Convert and validate the device handle */
727 node
= acpi_ns_validate_handle(obj_handle
);
729 return (AE_BAD_PARAMETER
);
733 * We only care about regions and objects that are allowed to have
734 * address space handlers
736 if ((node
->type
!= ACPI_TYPE_REGION
) && (node
!= acpi_gbl_root_node
)) {
740 /* Check for an existing internal object */
742 obj_desc
= acpi_ns_get_attached_object(node
);
745 /* No object, just exit */
750 /* Object is a Region */
752 if (obj_desc
->region
.space_id
!= info
->space_id
) {
754 /* This region is for a different address space, just ignore it */
759 info
->reg_run_count
++;
760 status
= acpi_ev_execute_reg_method(obj_desc
, info
->function
);
764 /*******************************************************************************
766 * FUNCTION: acpi_ev_execute_orphan_reg_method
768 * PARAMETERS: device_node - Namespace node for an ACPI device
769 * space_id - The address space ID
773 * DESCRIPTION: Execute an "orphan" _REG method that appears under an ACPI
774 * device. This is a _REG method that has no corresponding region
775 * within the device's scope. ACPI tables depending on these
776 * "orphan" _REG methods have been seen for both EC and GPIO
777 * Operation Regions. Presumably the Windows ACPI implementation
778 * always calls the _REG method independent of the presence of
779 * an actual Operation Region with the correct address space ID.
781 * MUTEX: Assumes the namespace is locked
783 ******************************************************************************/
786 acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node
*device_node
,
787 acpi_adr_space_type space_id
)
789 acpi_handle reg_method
;
790 struct acpi_namespace_node
*next_node
;
792 struct acpi_object_list args
;
793 union acpi_object objects
[2];
795 ACPI_FUNCTION_TRACE(ev_execute_orphan_reg_method
);
801 /* Namespace is currently locked, must release */
803 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE
);
805 /* Get a handle to a _REG method immediately under the EC device */
807 status
= acpi_get_handle(device_node
, METHOD_NAME__REG
, ®_method
);
808 if (ACPI_FAILURE(status
)) {
809 goto exit
; /* There is no _REG method present */
813 * Execute the _REG method only if there is no Operation Region in
814 * this scope with the Embedded Controller space ID. Otherwise, it
815 * will already have been executed. Note, this allows for Regions
816 * with other space IDs to be present; but the code below will then
817 * execute the _REG method with the embedded_control space_ID argument.
819 next_node
= acpi_ns_get_next_node(device_node
, NULL
);
821 if ((next_node
->type
== ACPI_TYPE_REGION
) &&
822 (next_node
->object
) &&
823 (next_node
->object
->region
.space_id
== space_id
)) {
824 goto exit
; /* Do not execute the _REG */
827 next_node
= acpi_ns_get_next_node(device_node
, next_node
);
830 /* Evaluate the _REG(space_id,Connect) method */
833 args
.pointer
= objects
;
834 objects
[0].type
= ACPI_TYPE_INTEGER
;
835 objects
[0].integer
.value
= space_id
;
836 objects
[1].type
= ACPI_TYPE_INTEGER
;
837 objects
[1].integer
.value
= ACPI_REG_CONNECT
;
839 (void)acpi_evaluate_object(reg_method
, NULL
, &args
, NULL
);
842 /* We ignore all errors from above, don't care */
844 (void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE
);