evxfregn.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: evxfregn - External Interfaces, ACPI Operation Regions and
  5. * Address Spaces.
  6. *
  7. * Copyright (C) 2000 - 2022, Intel Corp.
  8. *
  9. *****************************************************************************/
  10. #define EXPORT_ACPI_INTERFACES
  11. #include <acpi/acpi.h>
  12. #include "accommon.h"
  13. #include "acnamesp.h"
  14. #include "acevents.h"
  15. #define _COMPONENT ACPI_EVENTS
  16. ACPI_MODULE_NAME("evxfregn")
  17. /*******************************************************************************
  18. *
  19. * FUNCTION: acpi_install_address_space_handler
  20. *
  21. * PARAMETERS: device - Handle for the device
  22. * space_id - The address space ID
  23. * handler - Address of the handler
  24. * setup - Address of the setup function
  25. * context - Value passed to the handler on each access
  26. *
  27. * RETURN: Status
  28. *
  29. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  30. *
  31. * NOTE: This function should only be called after acpi_enable_subsystem has
  32. * been called. This is because any _REG methods associated with the Space ID
  33. * are executed here, and these methods can only be safely executed after
  34. * the default handlers have been installed and the hardware has been
  35. * initialized (via acpi_enable_subsystem.)
  36. *
  37. ******************************************************************************/
  38. acpi_status
  39. acpi_install_address_space_handler(acpi_handle device,
  40. acpi_adr_space_type space_id,
  41. acpi_adr_space_handler handler,
  42. acpi_adr_space_setup setup, void *context)
  43. {
  44. struct acpi_namespace_node *node;
  45. acpi_status status;
  46. ACPI_FUNCTION_TRACE(acpi_install_address_space_handler);
  47. /* Parameter validation */
  48. if (!device) {
  49. return_ACPI_STATUS(AE_BAD_PARAMETER);
  50. }
  51. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  52. if (ACPI_FAILURE(status)) {
  53. return_ACPI_STATUS(status);
  54. }
  55. /* Convert and validate the device handle */
  56. node = acpi_ns_validate_handle(device);
  57. if (!node) {
  58. status = AE_BAD_PARAMETER;
  59. goto unlock_and_exit;
  60. }
  61. /* Install the handler for all Regions for this Space ID */
  62. status =
  63. acpi_ev_install_space_handler(node, space_id, handler, setup,
  64. context);
  65. if (ACPI_FAILURE(status)) {
  66. goto unlock_and_exit;
  67. }
  68. /* Run all _REG methods for this address space */
  69. acpi_ev_execute_reg_methods(node, space_id, ACPI_REG_CONNECT);
  70. unlock_and_exit:
  71. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  72. return_ACPI_STATUS(status);
  73. }
  74. ACPI_EXPORT_SYMBOL(acpi_install_address_space_handler)
  75. /*******************************************************************************
  76. *
  77. * FUNCTION: acpi_remove_address_space_handler
  78. *
  79. * PARAMETERS: device - Handle for the device
  80. * space_id - The address space ID
  81. * handler - Address of the handler
  82. *
  83. * RETURN: Status
  84. *
  85. * DESCRIPTION: Remove a previously installed handler.
  86. *
  87. ******************************************************************************/
  88. acpi_status
  89. acpi_remove_address_space_handler(acpi_handle device,
  90. acpi_adr_space_type space_id,
  91. acpi_adr_space_handler handler)
  92. {
  93. union acpi_operand_object *obj_desc;
  94. union acpi_operand_object *handler_obj;
  95. union acpi_operand_object *region_obj;
  96. union acpi_operand_object **last_obj_ptr;
  97. struct acpi_namespace_node *node;
  98. acpi_status status;
  99. ACPI_FUNCTION_TRACE(acpi_remove_address_space_handler);
  100. /* Parameter validation */
  101. if (!device) {
  102. return_ACPI_STATUS(AE_BAD_PARAMETER);
  103. }
  104. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  105. if (ACPI_FAILURE(status)) {
  106. return_ACPI_STATUS(status);
  107. }
  108. /* Convert and validate the device handle */
  109. node = acpi_ns_validate_handle(device);
  110. if (!node ||
  111. ((node->type != ACPI_TYPE_DEVICE) &&
  112. (node->type != ACPI_TYPE_PROCESSOR) &&
  113. (node->type != ACPI_TYPE_THERMAL) &&
  114. (node != acpi_gbl_root_node))) {
  115. status = AE_BAD_PARAMETER;
  116. goto unlock_and_exit;
  117. }
  118. /* Make sure the internal object exists */
  119. obj_desc = acpi_ns_get_attached_object(node);
  120. if (!obj_desc) {
  121. status = AE_NOT_EXIST;
  122. goto unlock_and_exit;
  123. }
  124. /* Find the address handler the user requested */
  125. handler_obj = obj_desc->common_notify.handler;
  126. last_obj_ptr = &obj_desc->common_notify.handler;
  127. while (handler_obj) {
  128. /* We have a handler, see if user requested this one */
  129. if (handler_obj->address_space.space_id == space_id) {
  130. /* Handler must be the same as the installed handler */
  131. if (handler_obj->address_space.handler != handler) {
  132. status = AE_BAD_PARAMETER;
  133. goto unlock_and_exit;
  134. }
  135. /* Matched space_id, first dereference this in the Regions */
  136. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  137. "Removing address handler %p(%p) for region %s "
  138. "on Device %p(%p)\n",
  139. handler_obj, handler,
  140. acpi_ut_get_region_name(space_id),
  141. node, obj_desc));
  142. region_obj = handler_obj->address_space.region_list;
  143. /* Walk the handler's region list */
  144. while (region_obj) {
  145. /*
  146. * First disassociate the handler from the region.
  147. *
  148. * NOTE: this doesn't mean that the region goes away
  149. * The region is just inaccessible as indicated to
  150. * the _REG method
  151. */
  152. acpi_ev_detach_region(region_obj, TRUE);
  153. /*
  154. * Walk the list: Just grab the head because the
  155. * detach_region removed the previous head.
  156. */
  157. region_obj =
  158. handler_obj->address_space.region_list;
  159. }
  160. /* Remove this Handler object from the list */
  161. *last_obj_ptr = handler_obj->address_space.next;
  162. /* Now we can delete the handler object */
  163. acpi_os_release_mutex(handler_obj->address_space.
  164. context_mutex);
  165. acpi_ut_remove_reference(handler_obj);
  166. goto unlock_and_exit;
  167. }
  168. /* Walk the linked list of handlers */
  169. last_obj_ptr = &handler_obj->address_space.next;
  170. handler_obj = handler_obj->address_space.next;
  171. }
  172. /* The handler does not exist */
  173. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  174. "Unable to remove address handler %p for %s(%X), DevNode %p, obj %p\n",
  175. handler, acpi_ut_get_region_name(space_id), space_id,
  176. node, obj_desc));
  177. status = AE_NOT_EXIST;
  178. unlock_and_exit:
  179. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  180. return_ACPI_STATUS(status);
  181. }
  182. ACPI_EXPORT_SYMBOL(acpi_remove_address_space_handler)