evgpeinit.c 12 KB

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