evgpeinit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: evgpeinit - System GPE initialization and update
  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. #define _COMPONENT ACPI_EVENTS
  14. ACPI_MODULE_NAME("evgpeinit")
  15. #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
  16. /*
  17. * Note: History of _PRW support in ACPICA
  18. *
  19. * Originally (2000 - 2010), the GPE initialization code performed a walk of
  20. * the entire namespace to execute the _PRW methods and detect all GPEs
  21. * capable of waking the system.
  22. *
  23. * As of 10/2010, the _PRW method execution has been removed since it is
  24. * actually unnecessary. The host OS must in fact execute all _PRW methods
  25. * in order to identify the device/power-resource dependencies. We now put
  26. * the onus on the host OS to identify the wake GPEs as part of this process
  27. * and to inform ACPICA of these GPEs via the acpi_setup_gpe_for_wake interface. This
  28. * not only reduces the complexity of the ACPICA initialization code, but in
  29. * some cases (on systems with very large namespaces) it should reduce the
  30. * kernel boot time as well.
  31. */
  32. #ifdef ACPI_GPE_USE_LOGICAL_ADDRESSES
  33. #define ACPI_FADT_GPE_BLOCK_ADDRESS(N) \
  34. acpi_gbl_FADT.xgpe##N##_block.space_id == \
  35. ACPI_ADR_SPACE_SYSTEM_MEMORY ? \
  36. (u64)acpi_gbl_xgpe##N##_block_logical_address : \
  37. acpi_gbl_FADT.xgpe##N##_block.address
  38. #else
  39. #define ACPI_FADT_GPE_BLOCK_ADDRESS(N) acpi_gbl_FADT.xgpe##N##_block.address
  40. #endif /* ACPI_GPE_USE_LOGICAL_ADDRESSES */
  41. /*******************************************************************************
  42. *
  43. * FUNCTION: acpi_ev_gpe_initialize
  44. *
  45. * PARAMETERS: None
  46. *
  47. * RETURN: Status
  48. *
  49. * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
  50. *
  51. ******************************************************************************/
  52. acpi_status acpi_ev_gpe_initialize(void)
  53. {
  54. u32 register_count0 = 0;
  55. u32 register_count1 = 0;
  56. u32 gpe_number_max = 0;
  57. acpi_status status;
  58. u64 address;
  59. ACPI_FUNCTION_TRACE(ev_gpe_initialize);
  60. ACPI_DEBUG_PRINT_RAW((ACPI_DB_INIT,
  61. "Initializing General Purpose Events (GPEs):\n"));
  62. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  63. if (ACPI_FAILURE(status)) {
  64. return_ACPI_STATUS(status);
  65. }
  66. /*
  67. * Initialize the GPE Block(s) defined in the FADT
  68. *
  69. * Why the GPE register block lengths are divided by 2: From the ACPI
  70. * Spec, section "General-Purpose Event Registers", we have:
  71. *
  72. * "Each register block contains two registers of equal length
  73. * GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
  74. * GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
  75. * The length of the GPE1_STS and GPE1_EN registers is equal to
  76. * half the GPE1_LEN. If a generic register block is not supported
  77. * then its respective block pointer and block length values in the
  78. * FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
  79. * to be the same size."
  80. */
  81. /*
  82. * Determine the maximum GPE number for this machine.
  83. *
  84. * Note: both GPE0 and GPE1 are optional, and either can exist without
  85. * the other.
  86. *
  87. * If EITHER the register length OR the block address are zero, then that
  88. * particular block is not supported.
  89. */
  90. address = ACPI_FADT_GPE_BLOCK_ADDRESS(0);
  91. if (acpi_gbl_FADT.gpe0_block_length && address) {
  92. /* GPE block 0 exists (has both length and address > 0) */
  93. register_count0 = (u16)(acpi_gbl_FADT.gpe0_block_length / 2);
  94. gpe_number_max =
  95. (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
  96. /* Install GPE Block 0 */
  97. status = acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
  98. address,
  99. acpi_gbl_FADT.xgpe0_block.
  100. space_id, register_count0, 0,
  101. acpi_gbl_FADT.sci_interrupt,
  102. &acpi_gbl_gpe_fadt_blocks[0]);
  103. if (ACPI_FAILURE(status)) {
  104. ACPI_EXCEPTION((AE_INFO, status,
  105. "Could not create GPE Block 0"));
  106. }
  107. }
  108. address = ACPI_FADT_GPE_BLOCK_ADDRESS(1);
  109. if (acpi_gbl_FADT.gpe1_block_length && address) {
  110. /* GPE block 1 exists (has both length and address > 0) */
  111. register_count1 = (u16)(acpi_gbl_FADT.gpe1_block_length / 2);
  112. /* Check for GPE0/GPE1 overlap (if both banks exist) */
  113. if ((register_count0) &&
  114. (gpe_number_max >= acpi_gbl_FADT.gpe1_base)) {
  115. ACPI_ERROR((AE_INFO,
  116. "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
  117. "(GPE %u to %u) - Ignoring GPE1",
  118. gpe_number_max, acpi_gbl_FADT.gpe1_base,
  119. acpi_gbl_FADT.gpe1_base +
  120. ((register_count1 *
  121. ACPI_GPE_REGISTER_WIDTH) - 1)));
  122. /* Ignore GPE1 block by setting the register count to zero */
  123. register_count1 = 0;
  124. } else {
  125. /* Install GPE Block 1 */
  126. status =
  127. acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
  128. address,
  129. acpi_gbl_FADT.xgpe1_block.
  130. space_id, register_count1,
  131. acpi_gbl_FADT.gpe1_base,
  132. acpi_gbl_FADT.
  133. sci_interrupt,
  134. &acpi_gbl_gpe_fadt_blocks
  135. [1]);
  136. if (ACPI_FAILURE(status)) {
  137. ACPI_EXCEPTION((AE_INFO, status,
  138. "Could not create GPE Block 1"));
  139. }
  140. /*
  141. * GPE0 and GPE1 do not have to be contiguous in the GPE number
  142. * space. However, GPE0 always starts at GPE number zero.
  143. */
  144. }
  145. }
  146. /* Exit if there are no GPE registers */
  147. if ((register_count0 + register_count1) == 0) {
  148. /* GPEs are not required by ACPI, this is OK */
  149. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  150. "There are no GPE blocks defined in the FADT\n"));
  151. goto cleanup;
  152. }
  153. cleanup:
  154. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  155. return_ACPI_STATUS(AE_OK);
  156. }
  157. /*******************************************************************************
  158. *
  159. * FUNCTION: acpi_ev_update_gpes
  160. *
  161. * PARAMETERS: table_owner_id - ID of the newly-loaded ACPI table
  162. *
  163. * RETURN: None
  164. *
  165. * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
  166. * result of a Load() or load_table() operation. If new GPE
  167. * methods have been installed, register the new methods.
  168. *
  169. ******************************************************************************/
  170. void acpi_ev_update_gpes(acpi_owner_id table_owner_id)
  171. {
  172. struct acpi_gpe_xrupt_info *gpe_xrupt_info;
  173. struct acpi_gpe_block_info *gpe_block;
  174. struct acpi_gpe_walk_info walk_info;
  175. acpi_status status = AE_OK;
  176. /*
  177. * Find any _Lxx/_Exx GPE methods that have just been loaded.
  178. *
  179. * Any GPEs that correspond to new _Lxx/_Exx methods are immediately
  180. * enabled.
  181. *
  182. * Examine the namespace underneath each gpe_device within the
  183. * gpe_block lists.
  184. */
  185. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  186. if (ACPI_FAILURE(status)) {
  187. return;
  188. }
  189. walk_info.count = 0;
  190. walk_info.owner_id = table_owner_id;
  191. walk_info.execute_by_owner_id = TRUE;
  192. /* Walk the interrupt level descriptor list */
  193. gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
  194. while (gpe_xrupt_info) {
  195. /* Walk all Gpe Blocks attached to this interrupt level */
  196. gpe_block = gpe_xrupt_info->gpe_block_list_head;
  197. while (gpe_block) {
  198. walk_info.gpe_block = gpe_block;
  199. walk_info.gpe_device = gpe_block->node;
  200. status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD,
  201. walk_info.gpe_device,
  202. ACPI_UINT32_MAX,
  203. ACPI_NS_WALK_NO_UNLOCK,
  204. acpi_ev_match_gpe_method,
  205. NULL, &walk_info, NULL);
  206. if (ACPI_FAILURE(status)) {
  207. ACPI_EXCEPTION((AE_INFO, status,
  208. "While decoding _Lxx/_Exx methods"));
  209. }
  210. gpe_block = gpe_block->next;
  211. }
  212. gpe_xrupt_info = gpe_xrupt_info->next;
  213. }
  214. if (walk_info.count) {
  215. ACPI_INFO(("Enabled %u new GPEs", walk_info.count));
  216. }
  217. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  218. return;
  219. }
  220. /*******************************************************************************
  221. *
  222. * FUNCTION: acpi_ev_match_gpe_method
  223. *
  224. * PARAMETERS: Callback from walk_namespace
  225. *
  226. * RETURN: Status
  227. *
  228. * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
  229. * control method under the _GPE portion of the namespace.
  230. * Extract the name and GPE type from the object, saving this
  231. * information for quick lookup during GPE dispatch. Allows a
  232. * per-owner_id evaluation if execute_by_owner_id is TRUE in the
  233. * walk_info parameter block.
  234. *
  235. * The name of each GPE control method is of the form:
  236. * "_Lxx" or "_Exx", where:
  237. * L - means that the GPE is level triggered
  238. * E - means that the GPE is edge triggered
  239. * xx - is the GPE number [in HEX]
  240. *
  241. * If walk_info->execute_by_owner_id is TRUE, we only execute examine GPE methods
  242. * with that owner.
  243. *
  244. ******************************************************************************/
  245. acpi_status
  246. acpi_ev_match_gpe_method(acpi_handle obj_handle,
  247. u32 level, void *context, void **return_value)
  248. {
  249. struct acpi_namespace_node *method_node =
  250. ACPI_CAST_PTR(struct acpi_namespace_node, obj_handle);
  251. struct acpi_gpe_walk_info *walk_info =
  252. ACPI_CAST_PTR(struct acpi_gpe_walk_info, context);
  253. struct acpi_gpe_event_info *gpe_event_info;
  254. acpi_status status;
  255. u32 gpe_number;
  256. u8 temp_gpe_number;
  257. char name[ACPI_NAMESEG_SIZE + 1];
  258. u8 type;
  259. ACPI_FUNCTION_TRACE(ev_match_gpe_method);
  260. /* Check if requested owner_id matches this owner_id */
  261. if ((walk_info->execute_by_owner_id) &&
  262. (method_node->owner_id != walk_info->owner_id)) {
  263. return_ACPI_STATUS(AE_OK);
  264. }
  265. /*
  266. * Match and decode the _Lxx and _Exx GPE method names
  267. *
  268. * 1) Extract the method name and null terminate it
  269. */
  270. ACPI_MOVE_32_TO_32(name, &method_node->name.integer);
  271. name[ACPI_NAMESEG_SIZE] = 0;
  272. /* 2) Name must begin with an underscore */
  273. if (name[0] != '_') {
  274. return_ACPI_STATUS(AE_OK); /* Ignore this method */
  275. }
  276. /*
  277. * 3) Edge/Level determination is based on the 2nd character
  278. * of the method name
  279. */
  280. switch (name[1]) {
  281. case 'L':
  282. type = ACPI_GPE_LEVEL_TRIGGERED;
  283. break;
  284. case 'E':
  285. type = ACPI_GPE_EDGE_TRIGGERED;
  286. break;
  287. default:
  288. /* Unknown method type, just ignore it */
  289. ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
  290. "Ignoring unknown GPE method type: %s "
  291. "(name not of form _Lxx or _Exx)", name));
  292. return_ACPI_STATUS(AE_OK);
  293. }
  294. /* 4) The last two characters of the name are the hex GPE Number */
  295. status = acpi_ut_ascii_to_hex_byte(&name[2], &temp_gpe_number);
  296. if (ACPI_FAILURE(status)) {
  297. /* Conversion failed; invalid method, just ignore it */
  298. ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
  299. "Could not extract GPE number from name: %s "
  300. "(name is not of form _Lxx or _Exx)", name));
  301. return_ACPI_STATUS(AE_OK);
  302. }
  303. /* Ensure that we have a valid GPE number for this GPE block */
  304. gpe_number = (u32)temp_gpe_number;
  305. gpe_event_info =
  306. acpi_ev_low_get_gpe_info(gpe_number, walk_info->gpe_block);
  307. if (!gpe_event_info) {
  308. /*
  309. * This gpe_number is not valid for this GPE block, just ignore it.
  310. * However, it may be valid for a different GPE block, since GPE0
  311. * and GPE1 methods both appear under \_GPE.
  312. */
  313. return_ACPI_STATUS(AE_OK);
  314. }
  315. if ((ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
  316. ACPI_GPE_DISPATCH_HANDLER) ||
  317. (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
  318. ACPI_GPE_DISPATCH_RAW_HANDLER)) {
  319. /* If there is already a handler, ignore this GPE method */
  320. return_ACPI_STATUS(AE_OK);
  321. }
  322. if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) ==
  323. ACPI_GPE_DISPATCH_METHOD) {
  324. /*
  325. * If there is already a method, ignore this method. But check
  326. * for a type mismatch (if both the _Lxx AND _Exx exist)
  327. */
  328. if (type != (gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK)) {
  329. ACPI_ERROR((AE_INFO,
  330. "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
  331. gpe_number, gpe_number, gpe_number));
  332. }
  333. return_ACPI_STATUS(AE_OK);
  334. }
  335. /* Disable the GPE in case it's been enabled already. */
  336. (void)acpi_hw_low_set_gpe(gpe_event_info, ACPI_GPE_DISABLE);
  337. /*
  338. * Add the GPE information from above to the gpe_event_info block for
  339. * use during dispatch of this GPE.
  340. */
  341. gpe_event_info->flags &= ~(ACPI_GPE_DISPATCH_MASK);
  342. gpe_event_info->flags |= (u8)(type | ACPI_GPE_DISPATCH_METHOD);
  343. gpe_event_info->dispatch.method_node = method_node;
  344. ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
  345. "Registered GPE method %s as GPE number 0x%.2X\n",
  346. name, gpe_number));
  347. return_ACPI_STATUS(AE_OK);
  348. }
  349. #endif /* !ACPI_REDUCED_HARDWARE */