evhandler.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: evhandler - Support for Address Space handlers
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acevents.h"
  12. #include "acnamesp.h"
  13. #include "acinterp.h"
  14. #define _COMPONENT ACPI_EVENTS
  15. ACPI_MODULE_NAME("evhandler")
  16. /* Local prototypes */
  17. static acpi_status
  18. acpi_ev_install_handler(acpi_handle obj_handle,
  19. u32 level, void *context, void **return_value);
  20. /* These are the address spaces that will get default handlers */
  21. u8 acpi_gbl_default_address_spaces[ACPI_NUM_DEFAULT_SPACES] = {
  22. ACPI_ADR_SPACE_SYSTEM_MEMORY,
  23. ACPI_ADR_SPACE_SYSTEM_IO,
  24. ACPI_ADR_SPACE_PCI_CONFIG,
  25. ACPI_ADR_SPACE_DATA_TABLE
  26. };
  27. /*******************************************************************************
  28. *
  29. * FUNCTION: acpi_ev_install_region_handlers
  30. *
  31. * PARAMETERS: None
  32. *
  33. * RETURN: Status
  34. *
  35. * DESCRIPTION: Installs the core subsystem default address space handlers.
  36. *
  37. ******************************************************************************/
  38. acpi_status acpi_ev_install_region_handlers(void)
  39. {
  40. acpi_status status;
  41. u32 i;
  42. ACPI_FUNCTION_TRACE(ev_install_region_handlers);
  43. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  44. if (ACPI_FAILURE(status)) {
  45. return_ACPI_STATUS(status);
  46. }
  47. /*
  48. * All address spaces (PCI Config, EC, SMBus) are scope dependent and
  49. * registration must occur for a specific device.
  50. *
  51. * In the case of the system memory and IO address spaces there is
  52. * currently no device associated with the address space. For these we
  53. * use the root.
  54. *
  55. * We install the default PCI config space handler at the root so that
  56. * this space is immediately available even though the we have not
  57. * enumerated all the PCI Root Buses yet. This is to conform to the ACPI
  58. * specification which states that the PCI config space must be always
  59. * available -- even though we are nowhere near ready to find the PCI root
  60. * buses at this point.
  61. *
  62. * NOTE: We ignore AE_ALREADY_EXISTS because this means that a handler
  63. * has already been installed (via acpi_install_address_space_handler).
  64. * Similar for AE_SAME_HANDLER.
  65. */
  66. for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++) {
  67. status = acpi_ev_install_space_handler(acpi_gbl_root_node,
  68. acpi_gbl_default_address_spaces
  69. [i],
  70. ACPI_DEFAULT_HANDLER,
  71. NULL, NULL);
  72. switch (status) {
  73. case AE_OK:
  74. case AE_SAME_HANDLER:
  75. case AE_ALREADY_EXISTS:
  76. /* These exceptions are all OK */
  77. status = AE_OK;
  78. break;
  79. default:
  80. goto unlock_and_exit;
  81. }
  82. }
  83. unlock_and_exit:
  84. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  85. return_ACPI_STATUS(status);
  86. }
  87. /*******************************************************************************
  88. *
  89. * FUNCTION: acpi_ev_has_default_handler
  90. *
  91. * PARAMETERS: node - Namespace node for the device
  92. * space_id - The address space ID
  93. *
  94. * RETURN: TRUE if default handler is installed, FALSE otherwise
  95. *
  96. * DESCRIPTION: Check if the default handler is installed for the requested
  97. * space ID.
  98. *
  99. ******************************************************************************/
  100. u8
  101. acpi_ev_has_default_handler(struct acpi_namespace_node *node,
  102. acpi_adr_space_type space_id)
  103. {
  104. union acpi_operand_object *obj_desc;
  105. union acpi_operand_object *handler_obj;
  106. /* Must have an existing internal object */
  107. obj_desc = acpi_ns_get_attached_object(node);
  108. if (obj_desc) {
  109. handler_obj = obj_desc->common_notify.handler;
  110. /* Walk the linked list of handlers for this object */
  111. while (handler_obj) {
  112. if (handler_obj->address_space.space_id == space_id) {
  113. if (handler_obj->address_space.handler_flags &
  114. ACPI_ADDR_HANDLER_DEFAULT_INSTALLED) {
  115. return (TRUE);
  116. }
  117. }
  118. handler_obj = handler_obj->address_space.next;
  119. }
  120. }
  121. return (FALSE);
  122. }
  123. /*******************************************************************************
  124. *
  125. * FUNCTION: acpi_ev_install_handler
  126. *
  127. * PARAMETERS: walk_namespace callback
  128. *
  129. * DESCRIPTION: This routine installs an address handler into objects that are
  130. * of type Region or Device.
  131. *
  132. * If the Object is a Device, and the device has a handler of
  133. * the same type then the search is terminated in that branch.
  134. *
  135. * This is because the existing handler is closer in proximity
  136. * to any more regions than the one we are trying to install.
  137. *
  138. ******************************************************************************/
  139. static acpi_status
  140. acpi_ev_install_handler(acpi_handle obj_handle,
  141. u32 level, void *context, void **return_value)
  142. {
  143. union acpi_operand_object *handler_obj;
  144. union acpi_operand_object *next_handler_obj;
  145. union acpi_operand_object *obj_desc;
  146. struct acpi_namespace_node *node;
  147. acpi_status status;
  148. ACPI_FUNCTION_NAME(ev_install_handler);
  149. handler_obj = (union acpi_operand_object *)context;
  150. /* Parameter validation */
  151. if (!handler_obj) {
  152. return (AE_OK);
  153. }
  154. /* Convert and validate the device handle */
  155. node = acpi_ns_validate_handle(obj_handle);
  156. if (!node) {
  157. return (AE_BAD_PARAMETER);
  158. }
  159. /*
  160. * We only care about regions and objects that are allowed to have
  161. * address space handlers
  162. */
  163. if ((node->type != ACPI_TYPE_DEVICE) &&
  164. (node->type != ACPI_TYPE_REGION) && (node != acpi_gbl_root_node)) {
  165. return (AE_OK);
  166. }
  167. /* Check for an existing internal object */
  168. obj_desc = acpi_ns_get_attached_object(node);
  169. if (!obj_desc) {
  170. /* No object, just exit */
  171. return (AE_OK);
  172. }
  173. /* Devices are handled different than regions */
  174. if (obj_desc->common.type == ACPI_TYPE_DEVICE) {
  175. /* Check if this Device already has a handler for this address space */
  176. next_handler_obj =
  177. acpi_ev_find_region_handler(handler_obj->address_space.
  178. space_id,
  179. obj_desc->common_notify.
  180. handler);
  181. if (next_handler_obj) {
  182. /* Found a handler, is it for the same address space? */
  183. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  184. "Found handler for region [%s] in device %p(%p) handler %p\n",
  185. acpi_ut_get_region_name(handler_obj->
  186. address_space.
  187. space_id),
  188. obj_desc, next_handler_obj,
  189. handler_obj));
  190. /*
  191. * Since the object we found it on was a device, then it means
  192. * that someone has already installed a handler for the branch
  193. * of the namespace from this device on. Just bail out telling
  194. * the walk routine to not traverse this branch. This preserves
  195. * the scoping rule for handlers.
  196. */
  197. return (AE_CTRL_DEPTH);
  198. }
  199. /*
  200. * As long as the device didn't have a handler for this space we
  201. * don't care about it. We just ignore it and proceed.
  202. */
  203. return (AE_OK);
  204. }
  205. /* Object is a Region */
  206. if (obj_desc->region.space_id != handler_obj->address_space.space_id) {
  207. /* This region is for a different address space, just ignore it */
  208. return (AE_OK);
  209. }
  210. /*
  211. * Now we have a region and it is for the handler's address space type.
  212. *
  213. * First disconnect region for any previous handler (if any)
  214. */
  215. acpi_ev_detach_region(obj_desc, FALSE);
  216. /* Connect the region to the new handler */
  217. status = acpi_ev_attach_region(handler_obj, obj_desc, FALSE);
  218. return (status);
  219. }
  220. /*******************************************************************************
  221. *
  222. * FUNCTION: acpi_ev_find_region_handler
  223. *
  224. * PARAMETERS: space_id - The address space ID
  225. * handler_obj - Head of the handler object list
  226. *
  227. * RETURN: Matching handler object. NULL if space ID not matched
  228. *
  229. * DESCRIPTION: Search a handler object list for a match on the address
  230. * space ID.
  231. *
  232. ******************************************************************************/
  233. union acpi_operand_object *acpi_ev_find_region_handler(acpi_adr_space_type
  234. space_id,
  235. union acpi_operand_object
  236. *handler_obj)
  237. {
  238. /* Walk the handler list for this device */
  239. while (handler_obj) {
  240. /* Same space_id indicates a handler is installed */
  241. if (handler_obj->address_space.space_id == space_id) {
  242. return (handler_obj);
  243. }
  244. /* Next handler object */
  245. handler_obj = handler_obj->address_space.next;
  246. }
  247. return (NULL);
  248. }
  249. /*******************************************************************************
  250. *
  251. * FUNCTION: acpi_ev_install_space_handler
  252. *
  253. * PARAMETERS: node - Namespace node for the device
  254. * space_id - The address space ID
  255. * handler - Address of the handler
  256. * setup - Address of the setup function
  257. * context - Value passed to the handler on each access
  258. *
  259. * RETURN: Status
  260. *
  261. * DESCRIPTION: Install a handler for all op_regions of a given space_id.
  262. * Assumes namespace is locked
  263. *
  264. ******************************************************************************/
  265. acpi_status
  266. acpi_ev_install_space_handler(struct acpi_namespace_node *node,
  267. acpi_adr_space_type space_id,
  268. acpi_adr_space_handler handler,
  269. acpi_adr_space_setup setup, void *context)
  270. {
  271. union acpi_operand_object *obj_desc;
  272. union acpi_operand_object *handler_obj;
  273. acpi_status status = AE_OK;
  274. acpi_object_type type;
  275. u8 flags = 0;
  276. ACPI_FUNCTION_TRACE(ev_install_space_handler);
  277. /*
  278. * This registration is valid for only the types below and the root.
  279. * The root node is where the default handlers get installed.
  280. */
  281. if ((node->type != ACPI_TYPE_DEVICE) &&
  282. (node->type != ACPI_TYPE_PROCESSOR) &&
  283. (node->type != ACPI_TYPE_THERMAL) && (node != acpi_gbl_root_node)) {
  284. status = AE_BAD_PARAMETER;
  285. goto unlock_and_exit;
  286. }
  287. if (handler == ACPI_DEFAULT_HANDLER) {
  288. flags = ACPI_ADDR_HANDLER_DEFAULT_INSTALLED;
  289. switch (space_id) {
  290. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  291. handler = acpi_ex_system_memory_space_handler;
  292. setup = acpi_ev_system_memory_region_setup;
  293. break;
  294. case ACPI_ADR_SPACE_SYSTEM_IO:
  295. handler = acpi_ex_system_io_space_handler;
  296. setup = acpi_ev_io_space_region_setup;
  297. break;
  298. #ifdef ACPI_PCI_CONFIGURED
  299. case ACPI_ADR_SPACE_PCI_CONFIG:
  300. handler = acpi_ex_pci_config_space_handler;
  301. setup = acpi_ev_pci_config_region_setup;
  302. break;
  303. #endif
  304. case ACPI_ADR_SPACE_CMOS:
  305. handler = acpi_ex_cmos_space_handler;
  306. setup = acpi_ev_cmos_region_setup;
  307. break;
  308. #ifdef ACPI_PCI_CONFIGURED
  309. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  310. handler = acpi_ex_pci_bar_space_handler;
  311. setup = acpi_ev_pci_bar_region_setup;
  312. break;
  313. #endif
  314. case ACPI_ADR_SPACE_DATA_TABLE:
  315. handler = acpi_ex_data_table_space_handler;
  316. setup = acpi_ev_data_table_region_setup;
  317. break;
  318. default:
  319. status = AE_BAD_PARAMETER;
  320. goto unlock_and_exit;
  321. }
  322. }
  323. /* If the caller hasn't specified a setup routine, use the default */
  324. if (!setup) {
  325. setup = acpi_ev_default_region_setup;
  326. }
  327. /* Check for an existing internal object */
  328. obj_desc = acpi_ns_get_attached_object(node);
  329. if (obj_desc) {
  330. /*
  331. * The attached device object already exists. Now make sure
  332. * the handler is not already installed.
  333. */
  334. handler_obj = acpi_ev_find_region_handler(space_id,
  335. obj_desc->
  336. common_notify.
  337. handler);
  338. if (handler_obj) {
  339. if (handler_obj->address_space.handler == handler) {
  340. /*
  341. * It is (relatively) OK to attempt to install the SAME
  342. * handler twice. This can easily happen with the
  343. * PCI_Config space.
  344. */
  345. status = AE_SAME_HANDLER;
  346. goto unlock_and_exit;
  347. } else {
  348. /* A handler is already installed */
  349. status = AE_ALREADY_EXISTS;
  350. }
  351. goto unlock_and_exit;
  352. }
  353. } else {
  354. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  355. "Creating object on Device %p while installing handler\n",
  356. node));
  357. /* obj_desc does not exist, create one */
  358. if (node->type == ACPI_TYPE_ANY) {
  359. type = ACPI_TYPE_DEVICE;
  360. } else {
  361. type = node->type;
  362. }
  363. obj_desc = acpi_ut_create_internal_object(type);
  364. if (!obj_desc) {
  365. status = AE_NO_MEMORY;
  366. goto unlock_and_exit;
  367. }
  368. /* Init new descriptor */
  369. obj_desc->common.type = (u8)type;
  370. /* Attach the new object to the Node */
  371. status = acpi_ns_attach_object(node, obj_desc, type);
  372. /* Remove local reference to the object */
  373. acpi_ut_remove_reference(obj_desc);
  374. if (ACPI_FAILURE(status)) {
  375. goto unlock_and_exit;
  376. }
  377. }
  378. ACPI_DEBUG_PRINT((ACPI_DB_OPREGION,
  379. "Installing address handler for region %s(%X) "
  380. "on Device %4.4s %p(%p)\n",
  381. acpi_ut_get_region_name(space_id), space_id,
  382. acpi_ut_get_node_name(node), node, obj_desc));
  383. /*
  384. * Install the handler
  385. *
  386. * At this point there is no existing handler. Just allocate the object
  387. * for the handler and link it into the list.
  388. */
  389. handler_obj =
  390. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_ADDRESS_HANDLER);
  391. if (!handler_obj) {
  392. status = AE_NO_MEMORY;
  393. goto unlock_and_exit;
  394. }
  395. /* Init handler obj */
  396. status =
  397. acpi_os_create_mutex(&handler_obj->address_space.context_mutex);
  398. if (ACPI_FAILURE(status)) {
  399. acpi_ut_remove_reference(handler_obj);
  400. goto unlock_and_exit;
  401. }
  402. handler_obj->address_space.space_id = (u8)space_id;
  403. handler_obj->address_space.handler_flags = flags;
  404. handler_obj->address_space.region_list = NULL;
  405. handler_obj->address_space.node = node;
  406. handler_obj->address_space.handler = handler;
  407. handler_obj->address_space.context = context;
  408. handler_obj->address_space.setup = setup;
  409. /* Install at head of Device.address_space list */
  410. handler_obj->address_space.next = obj_desc->common_notify.handler;
  411. /*
  412. * The Device object is the first reference on the handler_obj.
  413. * Each region that uses the handler adds a reference.
  414. */
  415. obj_desc->common_notify.handler = handler_obj;
  416. /*
  417. * Walk the namespace finding all of the regions this handler will
  418. * manage.
  419. *
  420. * Start at the device and search the branch toward the leaf nodes
  421. * until either the leaf is encountered or a device is detected that
  422. * has an address handler of the same type.
  423. *
  424. * In either case, back up and search down the remainder of the branch
  425. */
  426. status = acpi_ns_walk_namespace(ACPI_TYPE_ANY, node,
  427. ACPI_UINT32_MAX, ACPI_NS_WALK_UNLOCK,
  428. acpi_ev_install_handler, NULL,
  429. handler_obj, NULL);
  430. unlock_and_exit:
  431. return_ACPI_STATUS(status);
  432. }