1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: evregion - Operation Region support
6 * Copyright (C) 2000 - 2022, 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_mutex context_mutex;
117 acpi_physical_address address;
119 ACPI_FUNCTION_TRACE(ev_address_space_dispatch);
121 region_obj2 = acpi_ns_get_secondary_object(region_obj);
123 return_ACPI_STATUS(AE_NOT_EXIST);
126 /* Ensure that there is a handler associated with this region */
128 handler_desc = region_obj->region.handler;
131 "No handler for Region [%4.4s] (%p) [%s]",
132 acpi_ut_get_node_name(region_obj->region.node),
134 acpi_ut_get_region_name(region_obj->region.
137 return_ACPI_STATUS(AE_NOT_EXIST);
140 context = handler_desc->address_space.context;
141 context_mutex = handler_desc->address_space.context_mutex;
142 context_locked = FALSE;
145 * It may be the case that the region has never been initialized.
146 * Some types of regions require special init code
148 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
150 /* This region has not been initialized yet, do it */
152 region_setup = handler_desc->address_space.setup;
155 /* No initialization routine, exit with error */
158 "No init routine for region(%p) [%s]",
160 acpi_ut_get_region_name(region_obj->region.
162 return_ACPI_STATUS(AE_NOT_EXIST);
165 if (region_obj->region.space_id == ACPI_ADR_SPACE_PLATFORM_COMM) {
166 struct acpi_pcc_info *ctx =
167 handler_desc->address_space.context;
169 ctx->internal_buffer =
170 field_obj->field.internal_pcc_buffer;
171 ctx->length = (u16)region_obj->region.length;
172 ctx->subspace_id = (u8)region_obj->region.address;
176 * We must exit the interpreter because the region setup will
177 * potentially execute control methods (for example, the _REG method
180 acpi_ex_exit_interpreter();
182 status = region_setup(region_obj, ACPI_REGION_ACTIVATE,
183 context, ®ion_context);
185 /* Re-enter the interpreter */
187 acpi_ex_enter_interpreter();
189 /* Check for failure of the Region Setup */
191 if (ACPI_FAILURE(status)) {
192 ACPI_EXCEPTION((AE_INFO, status,
193 "During region initialization: [%s]",
194 acpi_ut_get_region_name(region_obj->
197 return_ACPI_STATUS(status);
200 /* Region initialization may have been completed by region_setup */
202 if (!(region_obj->region.flags & AOPOBJ_SETUP_COMPLETE)) {
203 region_obj->region.flags |= AOPOBJ_SETUP_COMPLETE;
206 * Save the returned context for use in all accesses to
207 * the handler for this particular region
209 if (!(region_obj2->extra.region_context)) {
210 region_obj2->extra.region_context =
216 /* We have everything we need, we can invoke the address space handler */
218 handler = handler_desc->address_space.handler;
219 address = (region_obj->region.address + region_offset);
221 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
222 "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
223 ®ion_obj->region.handler->address_space, handler,
224 ACPI_FORMAT_UINT64(address),
225 acpi_ut_get_region_name(region_obj->region.
228 if (!(handler_desc->address_space.handler_flags &
229 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
231 * For handlers other than the default (supplied) handlers, we must
232 * exit the interpreter because the handler *might* block -- we don't
233 * know what it will do, so we can't hold the lock on the interpreter.
235 acpi_ex_exit_interpreter();
239 * Special handling for generic_serial_bus and general_purpose_io:
240 * There are three extra parameters that must be passed to the
241 * handler via the context:
242 * 1) Connection buffer, a resource template from Connection() op
243 * 2) Length of the above buffer
244 * 3) Actual access length from the access_as() op
246 * Since we pass these extra parameters via the context, which is
247 * shared between threads, we must lock the context to avoid these
248 * parameters being changed from another thread before the handler
249 * has completed running.
251 * In addition, for general_purpose_io, the Address and bit_width fields
252 * are defined as follows:
253 * 1) Address is the pin number index of the field (bit offset from
254 * the previous Connection)
255 * 2) bit_width is the actual bit length of the field (number of pins)
257 if ((region_obj->region.space_id == ACPI_ADR_SPACE_GSBUS ||
258 region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) &&
259 context && field_obj) {
262 acpi_os_acquire_mutex(context_mutex, ACPI_WAIT_FOREVER);
263 if (ACPI_FAILURE(status)) {
264 goto re_enter_interpreter;
267 context_locked = TRUE;
269 /* Get the Connection (resource_template) buffer */
271 context->connection = field_obj->field.resource_buffer;
272 context->length = field_obj->field.resource_length;
273 context->access_length = field_obj->field.access_length;
275 if (region_obj->region.space_id == ACPI_ADR_SPACE_GPIO) {
276 address = field_obj->field.pin_number_index;
277 bit_width = field_obj->field.bit_length;
281 /* Call the handler */
283 status = handler(function, address, bit_width, value, context,
284 region_obj2->extra.region_context);
286 if (context_locked) {
287 acpi_os_release_mutex(context_mutex);
290 if (ACPI_FAILURE(status)) {
291 ACPI_EXCEPTION((AE_INFO, status, "Returned by Handler for [%s]",
292 acpi_ut_get_region_name(region_obj->region.
296 * Special case for an EC timeout. These are seen so frequently
297 * that an additional error message is helpful
299 if ((region_obj->region.space_id == ACPI_ADR_SPACE_EC) &&
300 (status == AE_TIME)) {
302 "Timeout from EC hardware or EC device driver"));
306 re_enter_interpreter:
307 if (!(handler_desc->address_space.handler_flags &
308 ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)) {
310 * We just returned from a non-default handler, we must re-enter the
313 acpi_ex_enter_interpreter();
316 return_ACPI_STATUS(status);
319 /*******************************************************************************
321 * FUNCTION: acpi_ev_detach_region
323 * PARAMETERS: region_obj - Region Object
324 * acpi_ns_is_locked - Namespace Region Already Locked?
328 * DESCRIPTION: Break the association between the handler and the region
329 * this is a two way association.
331 ******************************************************************************/
334 acpi_ev_detach_region(union acpi_operand_object *region_obj,
335 u8 acpi_ns_is_locked)
337 union acpi_operand_object *handler_obj;
338 union acpi_operand_object *obj_desc;
339 union acpi_operand_object *start_desc;
340 union acpi_operand_object **last_obj_ptr;
341 acpi_adr_space_setup region_setup;
342 void **region_context;
343 union acpi_operand_object *region_obj2;
346 ACPI_FUNCTION_TRACE(ev_detach_region);
348 region_obj2 = acpi_ns_get_secondary_object(region_obj);
352 region_context = ®ion_obj2->extra.region_context;
354 /* Get the address handler from the region object */
356 handler_obj = region_obj->region.handler;
359 /* This region has no handler, all done */
364 /* Find this region in the handler's list */
366 obj_desc = handler_obj->address_space.region_list;
367 start_desc = obj_desc;
368 last_obj_ptr = &handler_obj->address_space.region_list;
372 /* Is this the correct Region? */
374 if (obj_desc == region_obj) {
375 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
376 "Removing Region %p from address handler %p\n",
377 region_obj, handler_obj));
379 /* This is it, remove it from the handler's list */
381 *last_obj_ptr = obj_desc->region.next;
382 obj_desc->region.next = NULL; /* Must clear field */
384 if (acpi_ns_is_locked) {
386 acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
387 if (ACPI_FAILURE(status)) {
392 /* Now stop region accesses by executing the _REG method */
395 acpi_ev_execute_reg_method(region_obj,
396 ACPI_REG_DISCONNECT);
397 if (ACPI_FAILURE(status)) {
398 ACPI_EXCEPTION((AE_INFO, status,
399 "from region _REG, [%s]",
400 acpi_ut_get_region_name
401 (region_obj->region.space_id)));
404 if (acpi_ns_is_locked) {
406 acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
407 if (ACPI_FAILURE(status)) {
413 * If the region has been activated, call the setup handler with
414 * the deactivate notification
416 if (region_obj->region.flags & AOPOBJ_SETUP_COMPLETE) {
417 region_setup = handler_obj->address_space.setup;
419 region_setup(region_obj,
420 ACPI_REGION_DEACTIVATE,
421 handler_obj->address_space.
422 context, region_context);
425 * region_context should have been released by the deactivate
426 * operation. We don't need access to it anymore here.
428 if (region_context) {
429 *region_context = NULL;
432 /* Init routine may fail, Just ignore errors */
434 if (ACPI_FAILURE(status)) {
435 ACPI_EXCEPTION((AE_INFO, status,
436 "from region handler - deactivate, [%s]",
437 acpi_ut_get_region_name
442 region_obj->region.flags &=
443 ~(AOPOBJ_SETUP_COMPLETE);
447 * Remove handler reference in the region
449 * NOTE: this doesn't mean that the region goes away, the region
450 * is just inaccessible as indicated to the _REG method
452 * If the region is on the handler's list, this must be the
455 region_obj->region.handler = NULL;
456 acpi_ut_remove_reference(handler_obj);
461 /* Walk the linked list of handlers */
463 last_obj_ptr = &obj_desc->region.next;
464 obj_desc = obj_desc->region.next;
466 /* Prevent infinite loop if list is corrupted */
468 if (obj_desc == start_desc) {
470 "Circular handler list in region object %p",
476 /* If we get here, the region was not in the handler's region list */
478 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
479 "Cannot remove region %p from address handler %p\n",
480 region_obj, handler_obj));
485 /*******************************************************************************
487 * FUNCTION: acpi_ev_attach_region
489 * PARAMETERS: handler_obj - Handler Object
490 * region_obj - Region Object
491 * acpi_ns_is_locked - Namespace Region Already Locked?
495 * DESCRIPTION: Create the association between the handler and the region
496 * this is a two way association.
498 ******************************************************************************/
501 acpi_ev_attach_region(union acpi_operand_object *handler_obj,
502 union acpi_operand_object *region_obj,
503 u8 acpi_ns_is_locked)
506 ACPI_FUNCTION_TRACE(ev_attach_region);
508 /* Install the region's handler */
510 if (region_obj->region.handler) {
511 return_ACPI_STATUS(AE_ALREADY_EXISTS);
514 ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
515 "Adding Region [%4.4s] %p to address handler %p [%s]\n",
516 acpi_ut_get_node_name(region_obj->region.node),
517 region_obj, handler_obj,
518 acpi_ut_get_region_name(region_obj->region.
521 /* Link this region to the front of the handler's list */
523 region_obj->region.next = handler_obj->address_space.region_list;
524 handler_obj->address_space.region_list = region_obj;
525 region_obj->region.handler = handler_obj;
526 acpi_ut_add_reference(handler_obj);
528 return_ACPI_STATUS(AE_OK);
531 /*******************************************************************************
533 * FUNCTION: acpi_ev_execute_reg_method
535 * PARAMETERS: region_obj - Region object
536 * function - Passed to _REG: On (1) or Off (0)
540 * DESCRIPTION: Execute _REG method for a region
542 ******************************************************************************/
545 acpi_ev_execute_reg_method(union acpi_operand_object *region_obj, u32 function)
547 struct acpi_evaluate_info *info;
548 union acpi_operand_object *args[3];
549 union acpi_operand_object *region_obj2;
550 const acpi_name *reg_name_ptr =
551 ACPI_CAST_PTR(acpi_name, METHOD_NAME__REG);
552 struct acpi_namespace_node *method_node;
553 struct acpi_namespace_node *node;
556 ACPI_FUNCTION_TRACE(ev_execute_reg_method);
558 if (!acpi_gbl_namespace_initialized ||
559 region_obj->region.handler == NULL) {
560 return_ACPI_STATUS(AE_OK);
563 region_obj2 = acpi_ns_get_secondary_object(region_obj);
565 return_ACPI_STATUS(AE_NOT_EXIST);
569 * Find any "_REG" method associated with this region definition.
570 * The method should always be updated as this function may be
571 * invoked after a namespace change.
573 node = region_obj->region.node->parent;
575 acpi_ns_search_one_scope(*reg_name_ptr, node, ACPI_TYPE_METHOD,
577 if (ACPI_SUCCESS(status)) {
579 * The _REG method is optional and there can be only one per
580 * region definition. This will be executed when the handler is
581 * attached or removed.
583 region_obj2->extra.method_REG = method_node;
585 if (region_obj2->extra.method_REG == NULL) {
586 return_ACPI_STATUS(AE_OK);
589 /* _REG(DISCONNECT) should be paired with _REG(CONNECT) */
591 if ((function == ACPI_REG_CONNECT &&
592 region_obj->common.flags & AOPOBJ_REG_CONNECTED) ||
593 (function == ACPI_REG_DISCONNECT &&
594 !(region_obj->common.flags & AOPOBJ_REG_CONNECTED))) {
595 return_ACPI_STATUS(AE_OK);
598 /* Allocate and initialize the evaluation information block */
600 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
602 return_ACPI_STATUS(AE_NO_MEMORY);
605 info->prefix_node = region_obj2->extra.method_REG;
606 info->relative_pathname = NULL;
607 info->parameters = args;
608 info->flags = ACPI_IGNORE_RETURN_VALUE;
611 * The _REG method has two arguments:
614 * Operation region space ID Same value as region_obj->Region.space_id
617 * connection status 1 for connecting the handler, 0 for disconnecting
618 * the handler (Passed as a parameter)
621 acpi_ut_create_integer_object((u64)region_obj->region.space_id);
623 status = AE_NO_MEMORY;
627 args[1] = acpi_ut_create_integer_object((u64)function);
629 status = AE_NO_MEMORY;
633 args[2] = NULL; /* Terminate list */
635 /* Execute the method, no return value */
637 ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
638 (ACPI_TYPE_METHOD, info->prefix_node, NULL));
640 status = acpi_ns_evaluate(info);
641 acpi_ut_remove_reference(args[1]);
643 if (ACPI_FAILURE(status)) {
647 if (function == ACPI_REG_CONNECT) {
648 region_obj->common.flags |= AOPOBJ_REG_CONNECTED;
650 region_obj->common.flags &= ~AOPOBJ_REG_CONNECTED;
654 acpi_ut_remove_reference(args[0]);
658 return_ACPI_STATUS(status);
661 /*******************************************************************************
663 * FUNCTION: acpi_ev_execute_reg_methods
665 * PARAMETERS: node - Namespace node for the device
666 * space_id - The address space ID
667 * function - Passed to _REG: On (1) or Off (0)
671 * DESCRIPTION: Run all _REG methods for the input Space ID;
672 * Note: assumes namespace is locked, or system init time.
674 ******************************************************************************/
677 acpi_ev_execute_reg_methods(struct acpi_namespace_node *node,
678 acpi_adr_space_type space_id, u32 function)
680 struct acpi_reg_walk_info info;
682 ACPI_FUNCTION_TRACE(ev_execute_reg_methods);
685 * These address spaces do not need a call to _REG, since the ACPI
686 * specification defines them as: "must always be accessible". Since
687 * they never change state (never become unavailable), no need to ever
688 * call _REG on them. Also, a data_table is not a "real" address space,
689 * so do not call _REG. September 2018.
691 if ((space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) ||
692 (space_id == ACPI_ADR_SPACE_SYSTEM_IO) ||
693 (space_id == ACPI_ADR_SPACE_DATA_TABLE)) {
697 info.space_id = space_id;
698 info.function = function;
699 info.reg_run_count = 0;
701 ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES,
702 " Running _REG methods for SpaceId %s\n",
703 acpi_ut_get_region_name(info.space_id)));
706 * Run all _REG methods for all Operation Regions for this space ID. This
707 * is a separate walk in order to handle any interdependencies between
708 * regions and _REG methods. (i.e. handlers must be installed for all
709 * regions of this Space ID before we can run any _REG methods)
711 (void)acpi_ns_walk_namespace(ACPI_TYPE_ANY, node, ACPI_UINT32_MAX,
712 ACPI_NS_WALK_UNLOCK, acpi_ev_reg_run, NULL,
716 * Special case for EC and GPIO: handle "orphan" _REG methods with
719 if (space_id == ACPI_ADR_SPACE_EC || space_id == ACPI_ADR_SPACE_GPIO) {
720 acpi_ev_execute_orphan_reg_method(node, space_id);
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_execute_orphan_reg_method
795 * PARAMETERS: device_node - Namespace node for an ACPI device
796 * space_id - The address space ID
800 * DESCRIPTION: Execute an "orphan" _REG method that appears under an ACPI
801 * device. This is a _REG method that has no corresponding region
802 * within the device's scope. ACPI tables depending on these
803 * "orphan" _REG methods have been seen for both EC and GPIO
804 * Operation Regions. Presumably the Windows ACPI implementation
805 * always calls the _REG method independent of the presence of
806 * an actual Operation Region with the correct address space ID.
808 * MUTEX: Assumes the namespace is locked
810 ******************************************************************************/
813 acpi_ev_execute_orphan_reg_method(struct acpi_namespace_node *device_node,
814 acpi_adr_space_type space_id)
816 acpi_handle reg_method;
817 struct acpi_namespace_node *next_node;
819 struct acpi_object_list args;
820 union acpi_object objects[2];
822 ACPI_FUNCTION_TRACE(ev_execute_orphan_reg_method);
828 /* Namespace is currently locked, must release */
830 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
832 /* Get a handle to a _REG method immediately under the EC device */
834 status = acpi_get_handle(device_node, METHOD_NAME__REG, ®_method);
835 if (ACPI_FAILURE(status)) {
836 goto exit; /* There is no _REG method present */
840 * Execute the _REG method only if there is no Operation Region in
841 * this scope with the Embedded Controller space ID. Otherwise, it
842 * will already have been executed. Note, this allows for Regions
843 * with other space IDs to be present; but the code below will then
844 * execute the _REG method with the embedded_control space_ID argument.
846 next_node = acpi_ns_get_next_node(device_node, NULL);
848 if ((next_node->type == ACPI_TYPE_REGION) &&
849 (next_node->object) &&
850 (next_node->object->region.space_id == space_id)) {
851 goto exit; /* Do not execute the _REG */
854 next_node = acpi_ns_get_next_node(device_node, next_node);
857 /* Evaluate the _REG(space_id,Connect) method */
860 args.pointer = objects;
861 objects[0].type = ACPI_TYPE_INTEGER;
862 objects[0].integer.value = space_id;
863 objects[1].type = ACPI_TYPE_INTEGER;
864 objects[1].integer.value = ACPI_REG_CONNECT;
866 (void)acpi_evaluate_object(reg_method, NULL, &args, NULL);
869 /* We ignore all errors from above, don't care */
871 (void)acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);