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_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node);
27 acpi_ev_reg_run(acpi_handle obj_handle,
28 u32 level, void *context, void **return_value);
30 /*******************************************************************************
32 * FUNCTION: acpi_ev_initialize_op_regions
38 * DESCRIPTION: Execute _REG methods for all Operation Regions that have
39 * an installed default region handler.
41 ******************************************************************************/
43 acpi_status acpi_ev_initialize_op_regions(void)
48 ACPI_FUNCTION_TRACE(ev_initialize_op_regions);
50 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
51 if (ACPI_FAILURE(status)) {
52 return_ACPI_STATUS(status);
55 /* Run the _REG methods for op_regions in each default address space */
57 for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
59 * Make sure the installed handler is the DEFAULT handler. If not the
60 * default, the _REG methods will have already been run (when the
61 * handler was installed)
63 if (acpi_ev_has_default_handler(acpi_gbl_root_node,
64 acpi_gbl_default_address_spaces
66 acpi_ev_execute_reg_methods(acpi_gbl_root_node,
67 acpi_gbl_default_address_spaces
68 [i], ACPI_REG_CONNECT);
72 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
73 return_ACPI_STATUS(status);
76 /*******************************************************************************
78 * FUNCTION: acpi_ev_address_space_dispatch
80 * PARAMETERS: region_obj - Internal region object
81 * field_obj - Corresponding field. Can be NULL.
82 * function - Read or Write operation
83 * region_offset - Where in the region to read or write
84 * bit_width - Field width in bits (8, 16, 32, or 64)
85 * value - Pointer to in or out value, must be
86 * a full 64-bit integer
90 * DESCRIPTION: Dispatch an address space or operation region access to
91 * a previously installed handler.
93 * NOTE: During early initialization, we always install the default region
94 * handlers for Memory, I/O and PCI_Config. This ensures that these operation
95 * region address spaces are always available as per the ACPI specification.
96 * This is especially needed in order to support the execution of
97 * module-level AML code during loading of the ACPI tables.
99 ******************************************************************************/
102 acpi_ev_address_space_dispatch(union acpi_operand_object *region_obj,
103 union acpi_operand_object *field_obj,
105 u32 region_offset, u32 bit_width, u64 *value)
108 acpi_adr_space_handler handler;
109 acpi_adr_space_setup region_setup;
110 union acpi_operand_object *handler_desc;
111 union acpi_operand_object *region_obj2;
112 void *region_context = NULL;
113 struct acpi_connection_info *context;
114 acpi_mutex context_mutex;
116 acpi_physical_address address;
118 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
120 region_obj2 = acpi_ns_get_secondary_object(region_obj);
122 return_ACPI_STATUS(AE_NOT_EXIST);
125 /* Ensure that there is a handler associated with this region */
127 handler_desc = region_obj->region.handler;
130 "No handler for Region [%4.4s] (%p) [%s]",
131 acpi_ut_get_node_name(region_obj->region.node),
133 acpi_ut_get_region_name(region_obj->region.
136 return_ACPI_STATUS(AE_NOT_EXIST);
139 context = handler_desc->address_space.context;
140 context_mutex = handler_desc->address_space.context_mutex;
141 context_locked = FALSE;
144 * It may be the case that the region has never been initialized.
145 * Some types of regions require special init code
147 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
149 /* This region has not been initialized yet, do it */
151 region_setup = handler_desc->address_space.setup;
154 /* No initialization routine, exit with error */
157 "No init routine for region(%p) [%s]",
159 acpi_ut_get_region_name(region_obj->region.
161 return_ACPI_STATUS(AE_NOT_EXIST);
165 * We must exit the interpreter because the region setup will
166 * potentially execute control methods (for example, the _REG method
169 acpi_ex_exit_interpreter();
171 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
172 context, ®ion_context);
174 /* Re-enter the interpreter */
176 acpi_ex_enter_interpreter();
178 /* Check for failure of the Region Setup */
180 if (ACPI_FAILURE(status)) {
181 ACPI_EXCEPTION((AE_INFO, status,
182 "During region initialization: [%s]",
183 acpi_ut_get_region_name(region_obj->
186 return_ACPI_STATUS(status);
189 /* Region initialization may have been completed by region_setup */
191 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
192 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
195 * Save the returned context for use in all accesses to
196 * the handler for this particular region
198 if (!(region_obj2->extra.region_context)) {
199 region_obj2->extra.region_context =
205 /* We have everything we need, we can invoke the address space handler */
207 handler = handler_desc->address_space.handler;
208 address = (region_obj->region.address + region_offset);
210 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
211 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
212 ®ion_obj->region.handler->address_space, handler,
213 ACPI_FORMAT_UINT64(address),
214 acpi_ut_get_region_name(region_obj->region.
217 if (!(handler_desc->address_space.handler_flags &
218 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
220 * For handlers other than the default (supplied) handlers, we must
221 * exit the interpreter because the handler *might* block -- we don't
222 * know what it will do, so we can't hold the lock on the interpreter.
224 acpi_ex_exit_interpreter();
228 * Special handling for generic_serial_bus and general_purpose_io:
229 * There are three extra parameters that must be passed to the
230 * handler via the context:
231 * 1) Connection buffer, a resource template from Connection() op
232 * 2) Length of the above buffer
233 * 3) Actual access length from the access_as() op
235 * Since we pass these extra parameters via the context, which is
236 * shared between threads, we must lock the context to avoid these
237 * parameters being changed from another thread before the handler
238 * has completed running.
240 * In addition, for general_purpose_io, the Address and bit_width fields
241 * are defined as follows:
242 * 1) Address is the pin number index of the field (bit offset from
243 * the previous Connection)
244 * 2) bit_width is the actual bit length of the field (number of pins)
246 if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS) &&
247 context && field_obj) {
250 acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
251 if (ACPI_FAILURE(status)) {
252 goto re_enter_interpreter;
255 context_locked = TRUE;
257 /* Get the Connection (resource_template) buffer */
259 context->connection = field_obj->field.resource_buffer;
260 context->length = field_obj->field.resource_length;
261 context->access_length = field_obj->field.access_length;
263 if ((region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
264 context && field_obj) {
267 acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
268 if (ACPI_FAILURE(status)) {
269 goto re_enter_interpreter;
272 context_locked = TRUE;
274 /* Get the Connection (resource_template) buffer */
276 context->connection = field_obj->field.resource_buffer;
277 context->length = field_obj->field.resource_length;
278 context->access_length = field_obj->field.access_length;
279 address = field_obj->field.pin_number_index;
280 bit_width = field_obj->field.bit_length;
283 /* Call the handler */
285 status = handler(function, address, bit_width, value, context,
286 region_obj2->extra.region_context);
288 if (context_locked) {
289 acpi_os_release_mutex(context_mutex);
292 if (ACPI_FAILURE(status)) {
293 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
294 acpi_ut_get_region_name(region_obj->region.
298 * Special case for an EC timeout. These are seen so frequently
299 * that an additional error message is helpful
301 if ((region_obj->region.space_id == ACPI_ADR_SPACE_EC) &&
302 (status == AE_TIME)) {
304 "Timeout from EC hardware or EC device driver"));
308 re_enter_interpreter:
309 if (!(handler_desc->address_space.handler_flags &
310 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
312 * We just returned from a non-default handler, we must re-enter the
315 acpi_ex_enter_interpreter();
318 return_ACPI_STATUS(status);
321 /*******************************************************************************
323 * FUNCTION: acpi_ev_detach_region
325 * PARAMETERS: region_obj - Region Object
326 * acpi_ns_is_locked - Namespace Region Already Locked?
330 * DESCRIPTION: Break the association between the handler and the region
331 * this is a two way association.
333 ******************************************************************************/
336 acpi_ev_detach_region(union acpi_operand_object *region_obj,
337 u8 acpi_ns_is_locked)
339 union acpi_operand_object *handler_obj;
340 union acpi_operand_object *obj_desc;
341 union acpi_operand_object *start_desc;
342 union acpi_operand_object **last_obj_ptr;
343 acpi_adr_space_setup region_setup;
344 void **region_context;
345 union acpi_operand_object *region_obj2;
348 ACPI_FUNCTION_TRACE(ev_detach_region);
350 region_obj2 = acpi_ns_get_secondary_object(region_obj);
354 region_context = ®ion_obj2->extra.region_context;
356 /* Get the address handler from the region object */
358 handler_obj = region_obj->region.handler;
361 /* This region has no handler, all done */
366 /* Find this region in the handler's list */
368 obj_desc = handler_obj->address_space.region_list;
369 start_desc = obj_desc;
370 last_obj_ptr = &handler_obj->address_space.region_list;
374 /* Is this the correct Region? */
376 if (obj_desc == region_obj) {
377 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
378 "Removing Region %p from address handler %p\n",
379 region_obj, handler_obj));
381 /* This is it, remove it from the handler's list */
383 *last_obj_ptr = obj_desc->region.next;
384 obj_desc->region.next = NULL; /* Must clear field */
386 if (acpi_ns_is_locked) {
388 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
389 if (ACPI_FAILURE(status)) {
394 /* Now stop region accesses by executing the _REG method */
397 acpi_ev_execute_reg_method(region_obj,
398 ACPI_REG_DISCONNECT);
399 if (ACPI_FAILURE(status)) {
400 ACPI_EXCEPTION((AE_INFO, status,
401 "from region _REG, [%s]",
402 acpi_ut_get_region_name
403 (region_obj->region.space_id)));
406 if (acpi_ns_is_locked) {
408 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
409 if (ACPI_FAILURE(status)) {
415 * If the region has been activated, call the setup handler with
416 * the deactivate notification
418 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
419 region_setup = handler_obj->address_space.setup;
421 region_setup(region_obj,
422 ACPI_REGION_DEACTIVATE,
423 handler_obj->address_space.
424 context, region_context);
427 * region_context should have been released by the deactivate
428 * operation. We don't need access to it anymore here.
430 if (region_context) {
431 *region_context = NULL;
434 /* Init routine may fail, Just ignore errors */
436 if (ACPI_FAILURE(status)) {
437 ACPI_EXCEPTION((AE_INFO, status,
438 "from region handler - deactivate, [%s]",
439 acpi_ut_get_region_name
444 region_obj->region.flags &=
445 ~(AOPOBJ_SETUP_COMPLETE);
449 * Remove handler reference in the region
451 * NOTE: this doesn't mean that the region goes away, the region
452 * is just inaccessible as indicated to the _REG method
454 * If the region is on the handler's list, this must be the
457 region_obj->region.handler = NULL;
458 acpi_ut_remove_reference(handler_obj);
463 /* Walk the linked list of handlers */
465 last_obj_ptr = &obj_desc->region.next;
466 obj_desc = obj_desc->region.next;
468 /* Prevent infinite loop if list is corrupted */
470 if (obj_desc == start_desc) {
472 "Circular handler list in region object %p",
478 /* If we get here, the region was not in the handler's region list */
480 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
481 "Cannot remove region %p from address handler %p\n",
482 region_obj, handler_obj));
487 /*******************************************************************************
489 * FUNCTION: acpi_ev_attach_region
491 * PARAMETERS: handler_obj - Handler Object
492 * region_obj - Region Object
493 * acpi_ns_is_locked - Namespace Region Already Locked?
497 * DESCRIPTION: Create the association between the handler and the region
498 * this is a two way association.
500 ******************************************************************************/
503 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
504 union acpi_operand_object *region_obj,
505 u8 acpi_ns_is_locked)
508 ACPI_FUNCTION_TRACE(ev_attach_region);
510 /* Install the region's handler */
512 if (region_obj->region.handler) {
513 return_ACPI_STATUS(AE_ALREADY_EXISTS);
516 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
517 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
518 acpi_ut_get_node_name(region_obj->region.node),
519 region_obj, handler_obj,
520 acpi_ut_get_region_name(region_obj->region.
523 /* Link this region to the front of the handler's list */
525 region_obj->region.next = handler_obj->address_space.region_list;
526 handler_obj->address_space.region_list = region_obj;
527 region_obj->region.handler = handler_obj;
528 acpi_ut_add_reference(handler_obj);
530 return_ACPI_STATUS(AE_OK);
533 /*******************************************************************************
535 * FUNCTION: acpi_ev_execute_reg_method
537 * PARAMETERS: region_obj - Region object
538 * function - Passed to _REG: On (1) or Off (0)
542 * DESCRIPTION: Execute _REG method for a region
544 ******************************************************************************/
547 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
549 struct acpi_evaluate_info *info;
550 union acpi_operand_object *args[3];
551 union acpi_operand_object *region_obj2;
552 const acpi_name *reg_name_ptr =
553 ACPI_CAST_PTR(acpi_name, METHOD_NAME__REG);
554 struct acpi_namespace_node *method_node;
555 struct acpi_namespace_node *node;
558 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
560 if (!acpi_gbl_namespace_initialized ||
561 region_obj->region.handler == NULL) {
562 return_ACPI_STATUS(AE_OK);
565 region_obj2 = acpi_ns_get_secondary_object(region_obj);
567 return_ACPI_STATUS(AE_NOT_EXIST);
571 * Find any "_REG" method associated with this region definition.
572 * The method should always be updated as this function may be
573 * invoked after a namespace change.
575 node = region_obj->region.node->parent;
577 acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
579 if (ACPI_SUCCESS(status)) {
581 * The _REG method is optional and there can be only one per
582 * region definition. This will be executed when the handler is
583 * attached or removed.
585 region_obj2->extra.method_REG = method_node;
587 if (region_obj2->extra.method_REG == NULL) {
588 return_ACPI_STATUS(AE_OK);
591 /* _REG(DISCONNECT) should be paired with _REG(CONNECT) */
593 if ((function == ACPI_REG_CONNECT &&
594 region_obj->common.flags & AOPOBJ_REG_CONNECTED) ||
595 (function == ACPI_REG_DISCONNECT &&
596 !(region_obj->common.flags & AOPOBJ_REG_CONNECTED))) {
597 return_ACPI_STATUS(AE_OK);
600 /* Allocate and initialize the evaluation information block */
602 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
604 return_ACPI_STATUS(AE_NO_MEMORY);
607 info->prefix_node = region_obj2->extra.method_REG;
608 info->relative_pathname = NULL;
609 info->parameters = args;
610 info->flags = ACPI_IGNORE_RETURN_VALUE;
613 * The _REG method has two arguments:
616 * Operation region space ID Same value as region_obj->Region.space_id
619 * connection status 1 for connecting the handler, 0 for disconnecting
620 * the handler (Passed as a parameter)
623 acpi_ut_create_integer_object((u64)region_obj->region.space_id);
625 status = AE_NO_MEMORY;
629 args[1] = acpi_ut_create_integer_object((u64)function);
631 status = AE_NO_MEMORY;
635 args[2] = NULL; /* Terminate list */
637 /* Execute the method, no return value */
639 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
640 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
642 status = acpi_ns_evaluate(info);
643 acpi_ut_remove_reference(args[1]);
645 if (ACPI_FAILURE(status)) {
649 if (function == ACPI_REG_CONNECT) {
650 region_obj->common.flags |= AOPOBJ_REG_CONNECTED;
652 region_obj->common.flags &= ~AOPOBJ_REG_CONNECTED;
656 acpi_ut_remove_reference(args[0]);
660 return_ACPI_STATUS(status);
663 /*******************************************************************************
665 * FUNCTION: acpi_ev_execute_reg_methods
667 * PARAMETERS: node - Namespace node for the device
668 * space_id - The address space ID
669 * function - Passed to _REG: On (1) or Off (0)
673 * DESCRIPTION: Run all _REG methods for the input Space ID;
674 * Note: assumes namespace is locked, or system init time.
676 ******************************************************************************/
679 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
680 acpi_adr_space_type space_id, u32 function)
682 struct acpi_reg_walk_info info;
684 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
687 * These address spaces do not need a call to _REG, since the ACPI
688 * specification defines them as: "must always be accessible". Since
689 * they never change state (never become unavailable), no need to ever
690 * call _REG on them. Also, a data_table is not a "real" address space,
691 * so do not call _REG. September 2018.
693 if ((space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) ||
694 (space_id == ACPI_ADR_SPACE_SYSTEM_IO) ||
695 (space_id == ACPI_ADR_SPACE_DATA_TABLE)) {
699 info.space_id = space_id;
700 info.function = function;
701 info.reg_run_count = 0;
703 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
704 " Running _REG methods for SpaceId %s\n",
705 acpi_ut_get_region_name(info.space_id)));
708 * Run all _REG methods for all Operation Regions for this space ID. This
709 * is a separate walk in order to handle any interdependencies between
710 * regions and _REG methods. (i.e. handlers must be installed for all
711 * regions of this Space ID before we can run any _REG methods)
713 (void)acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
714 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run, NULL,
717 /* Special case for EC: handle "orphan" _REG methods with no region */
719 if (space_id == ACPI_ADR_SPACE_EC) {
720 acpi_ev_orphan_ec_reg_method(node);
723 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
724 " Executed %u _REG methods for SpaceId %s\n",
726 acpi_ut_get_region_name(info.space_id)));
731 /*******************************************************************************
733 * FUNCTION: acpi_ev_reg_run
735 * PARAMETERS: walk_namespace callback
737 * DESCRIPTION: Run _REG method for region objects of the requested spaceID
739 ******************************************************************************/
742 acpi_ev_reg_run(acpi_handle obj_handle,
743 u32 level, void *context, void **return_value)
745 union acpi_operand_object *obj_desc;
746 struct acpi_namespace_node *node;
748 struct acpi_reg_walk_info *info;
750 info = ACPI_CAST_PTR(struct acpi_reg_walk_info, context);
752 /* Convert and validate the device handle */
754 node = acpi_ns_validate_handle(obj_handle);
756 return (AE_BAD_PARAMETER);
760 * We only care about regions and objects that are allowed to have
761 * address space handlers
763 if ((node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
767 /* Check for an existing internal object */
769 obj_desc = acpi_ns_get_attached_object(node);
772 /* No object, just exit */
777 /* Object is a Region */
779 if (obj_desc->region.space_id != info->space_id) {
781 /* This region is for a different address space, just ignore it */
786 info->reg_run_count++;
787 status = acpi_ev_execute_reg_method(obj_desc, info->function);
791 /*******************************************************************************
793 * FUNCTION: acpi_ev_orphan_ec_reg_method
795 * PARAMETERS: ec_device_node - Namespace node for an EC device
799 * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
800 * device. This is a _REG method that has no corresponding region
801 * within the EC device scope. The orphan _REG method appears to
802 * have been enabled by the description of the ECDT in the ACPI
803 * specification: "The availability of the region space can be
804 * detected by providing a _REG method object underneath the
805 * Embedded Controller device."
807 * To quickly access the EC device, we use the ec_device_node used
808 * during EC handler installation. Otherwise, we would need to
809 * perform a time consuming namespace walk, executing _HID
810 * methods to find the EC device.
812 * MUTEX: Assumes the namespace is locked
814 ******************************************************************************/
817 acpi_ev_orphan_ec_reg_method(struct acpi_namespace_node *ec_device_node)
819 acpi_handle reg_method;
820 struct acpi_namespace_node *next_node;
822 struct acpi_object_list args;
823 union acpi_object objects[2];
825 ACPI_FUNCTION_TRACE(ev_orphan_ec_reg_method);
827 if (!ec_device_node) {
831 /* Namespace is currently locked, must release */
833 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
835 /* Get a handle to a _REG method immediately under the EC device */
837 status = acpi_get_handle(ec_device_node, METHOD_NAME__REG, ®_method);
838 if (ACPI_FAILURE(status)) {
839 goto exit; /* There is no _REG method present */
843 * Execute the _REG method only if there is no Operation Region in
844 * this scope with the Embedded Controller space ID. Otherwise, it
845 * will already have been executed. Note, this allows for Regions
846 * with other space IDs to be present; but the code below will then
847 * execute the _REG method with the embedded_control space_ID argument.
849 next_node = acpi_ns_get_next_node(ec_device_node, NULL);
851 if ((next_node->type == ACPI_TYPE_REGION) &&
852 (next_node->object) &&
853 (next_node->object->region.space_id == ACPI_ADR_SPACE_EC)) {
854 goto exit; /* Do not execute the _REG */
857 next_node = acpi_ns_get_next_node(ec_device_node, next_node);
860 /* Evaluate the _REG(embedded_control,Connect) method */
863 args.pointer = objects;
864 objects[0].type = ACPI_TYPE_INTEGER;
865 objects[0].integer.value = ACPI_ADR_SPACE_EC;
866 objects[1].type = ACPI_TYPE_INTEGER;
867 objects[1].integer.value = ACPI_REG_CONNECT;
869 (void)acpi_evaluate_object(reg_method, NULL, &args, NULL);
872 /* We ignore all errors from above, don't care */
874 (void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);