evgpeinit.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /******************************************************************************
  2. *
  3. * Module Name: evgpeinit - System GPE initialization and update
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2010, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acevents.h"
  45. #include "acnamesp.h"
  46. #include "acinterp.h"
  47. #define _COMPONENT ACPI_EVENTS
  48. ACPI_MODULE_NAME("evgpeinit")
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ev_gpe_initialize
  52. *
  53. * PARAMETERS: None
  54. *
  55. * RETURN: Status
  56. *
  57. * DESCRIPTION: Initialize the GPE data structures and the FADT GPE 0/1 blocks
  58. *
  59. ******************************************************************************/
  60. acpi_status acpi_ev_gpe_initialize(void)
  61. {
  62. u32 register_count0 = 0;
  63. u32 register_count1 = 0;
  64. u32 gpe_number_max = 0;
  65. acpi_status status;
  66. ACPI_FUNCTION_TRACE(ev_gpe_initialize);
  67. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  68. if (ACPI_FAILURE(status)) {
  69. return_ACPI_STATUS(status);
  70. }
  71. /*
  72. * Initialize the GPE Block(s) defined in the FADT
  73. *
  74. * Why the GPE register block lengths are divided by 2: From the ACPI
  75. * Spec, section "General-Purpose Event Registers", we have:
  76. *
  77. * "Each register block contains two registers of equal length
  78. * GPEx_STS and GPEx_EN (where x is 0 or 1). The length of the
  79. * GPE0_STS and GPE0_EN registers is equal to half the GPE0_LEN
  80. * The length of the GPE1_STS and GPE1_EN registers is equal to
  81. * half the GPE1_LEN. If a generic register block is not supported
  82. * then its respective block pointer and block length values in the
  83. * FADT table contain zeros. The GPE0_LEN and GPE1_LEN do not need
  84. * to be the same size."
  85. */
  86. /*
  87. * Determine the maximum GPE number for this machine.
  88. *
  89. * Note: both GPE0 and GPE1 are optional, and either can exist without
  90. * the other.
  91. *
  92. * If EITHER the register length OR the block address are zero, then that
  93. * particular block is not supported.
  94. */
  95. if (acpi_gbl_FADT.gpe0_block_length &&
  96. acpi_gbl_FADT.xgpe0_block.address) {
  97. /* GPE block 0 exists (has both length and address > 0) */
  98. register_count0 = (u16)(acpi_gbl_FADT.gpe0_block_length / 2);
  99. gpe_number_max =
  100. (register_count0 * ACPI_GPE_REGISTER_WIDTH) - 1;
  101. /* Install GPE Block 0 */
  102. status = acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
  103. &acpi_gbl_FADT.xgpe0_block,
  104. register_count0, 0,
  105. acpi_gbl_FADT.sci_interrupt,
  106. &acpi_gbl_gpe_fadt_blocks[0]);
  107. if (ACPI_FAILURE(status)) {
  108. ACPI_EXCEPTION((AE_INFO, status,
  109. "Could not create GPE Block 0"));
  110. }
  111. }
  112. if (acpi_gbl_FADT.gpe1_block_length &&
  113. acpi_gbl_FADT.xgpe1_block.address) {
  114. /* GPE block 1 exists (has both length and address > 0) */
  115. register_count1 = (u16)(acpi_gbl_FADT.gpe1_block_length / 2);
  116. /* Check for GPE0/GPE1 overlap (if both banks exist) */
  117. if ((register_count0) &&
  118. (gpe_number_max >= acpi_gbl_FADT.gpe1_base)) {
  119. ACPI_ERROR((AE_INFO,
  120. "GPE0 block (GPE 0 to %u) overlaps the GPE1 block "
  121. "(GPE %u to %u) - Ignoring GPE1",
  122. gpe_number_max, acpi_gbl_FADT.gpe1_base,
  123. acpi_gbl_FADT.gpe1_base +
  124. ((register_count1 *
  125. ACPI_GPE_REGISTER_WIDTH) - 1)));
  126. /* Ignore GPE1 block by setting the register count to zero */
  127. register_count1 = 0;
  128. } else {
  129. /* Install GPE Block 1 */
  130. status =
  131. acpi_ev_create_gpe_block(acpi_gbl_fadt_gpe_device,
  132. &acpi_gbl_FADT.xgpe1_block,
  133. register_count1,
  134. acpi_gbl_FADT.gpe1_base,
  135. acpi_gbl_FADT.
  136. sci_interrupt,
  137. &acpi_gbl_gpe_fadt_blocks
  138. [1]);
  139. if (ACPI_FAILURE(status)) {
  140. ACPI_EXCEPTION((AE_INFO, status,
  141. "Could not create GPE Block 1"));
  142. }
  143. /*
  144. * GPE0 and GPE1 do not have to be contiguous in the GPE number
  145. * space. However, GPE0 always starts at GPE number zero.
  146. */
  147. gpe_number_max = acpi_gbl_FADT.gpe1_base +
  148. ((register_count1 * ACPI_GPE_REGISTER_WIDTH) - 1);
  149. }
  150. }
  151. /* Exit if there are no GPE registers */
  152. if ((register_count0 + register_count1) == 0) {
  153. /* GPEs are not required by ACPI, this is OK */
  154. ACPI_DEBUG_PRINT((ACPI_DB_INIT,
  155. "There are no GPE blocks defined in the FADT\n"));
  156. status = AE_OK;
  157. goto cleanup;
  158. }
  159. /* Check for Max GPE number out-of-range */
  160. if (gpe_number_max > ACPI_GPE_MAX) {
  161. ACPI_ERROR((AE_INFO,
  162. "Maximum GPE number from FADT is too large: 0x%X",
  163. gpe_number_max));
  164. status = AE_BAD_VALUE;
  165. goto cleanup;
  166. }
  167. cleanup:
  168. (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  169. return_ACPI_STATUS(AE_OK);
  170. }
  171. /*******************************************************************************
  172. *
  173. * FUNCTION: acpi_ev_update_gpes
  174. *
  175. * PARAMETERS: table_owner_id - ID of the newly-loaded ACPI table
  176. *
  177. * RETURN: None
  178. *
  179. * DESCRIPTION: Check for new GPE methods (_Lxx/_Exx) made available as a
  180. * result of a Load() or load_table() operation. If new GPE
  181. * methods have been installed, register the new methods and
  182. * enable and runtime GPEs that are associated with them.
  183. *
  184. ******************************************************************************/
  185. void acpi_ev_update_gpes(acpi_owner_id table_owner_id)
  186. {
  187. struct acpi_gpe_xrupt_info *gpe_xrupt_info;
  188. struct acpi_gpe_block_info *gpe_block;
  189. struct acpi_gpe_walk_info walk_info;
  190. acpi_status status = AE_OK;
  191. /*
  192. * 2) Find any _Lxx/_Exx GPE methods that have just been loaded.
  193. *
  194. * Any GPEs that correspond to new _Lxx/_Exx methods are immediately
  195. * enabled.
  196. *
  197. * Examine the namespace underneath each gpe_device within the
  198. * gpe_block lists.
  199. */
  200. status = acpi_ut_acquire_mutex(ACPI_MTX_EVENTS);
  201. if (ACPI_FAILURE(status)) {
  202. return;
  203. }
  204. walk_info.owner_id = table_owner_id;
  205. walk_info.execute_by_owner_id = TRUE;
  206. walk_info.count = 0;
  207. walk_info.enable_this_gpe = TRUE;
  208. /* Walk the interrupt level descriptor list */
  209. gpe_xrupt_info = acpi_gbl_gpe_xrupt_list_head;
  210. while (gpe_xrupt_info) {
  211. /* Walk all Gpe Blocks attached to this interrupt level */
  212. gpe_block = gpe_xrupt_info->gpe_block_list_head;
  213. while (gpe_block) {
  214. walk_info.gpe_block = gpe_block;
  215. walk_info.gpe_device = gpe_block->node;
  216. status = acpi_ns_walk_namespace(ACPI_TYPE_METHOD,
  217. walk_info.gpe_device,
  218. ACPI_UINT32_MAX,
  219. ACPI_NS_WALK_NO_UNLOCK,
  220. acpi_ev_match_gpe_method,
  221. NULL, &walk_info, NULL);
  222. if (ACPI_FAILURE(status)) {
  223. ACPI_EXCEPTION((AE_INFO, status,
  224. "While decoding _Lxx/_Exx methods"));
  225. }
  226. gpe_block = gpe_block->next;
  227. }
  228. gpe_xrupt_info = gpe_xrupt_info->next;
  229. }
  230. if (walk_info.count) {
  231. ACPI_INFO((AE_INFO, "Enabled %u new GPEs", walk_info.count));
  232. }
  233. (void)acpi_ut_release_mutex(ACPI_MTX_EVENTS);
  234. return;
  235. }
  236. /*******************************************************************************
  237. *
  238. * FUNCTION: acpi_ev_match_gpe_method
  239. *
  240. * PARAMETERS: Callback from walk_namespace
  241. *
  242. * RETURN: Status
  243. *
  244. * DESCRIPTION: Called from acpi_walk_namespace. Expects each object to be a
  245. * control method under the _GPE portion of the namespace.
  246. * Extract the name and GPE type from the object, saving this
  247. * information for quick lookup during GPE dispatch. Allows a
  248. * per-owner_id evaluation if execute_by_owner_id is TRUE in the
  249. * walk_info parameter block.
  250. *
  251. * The name of each GPE control method is of the form:
  252. * "_Lxx" or "_Exx", where:
  253. * L - means that the GPE is level triggered
  254. * E - means that the GPE is edge triggered
  255. * xx - is the GPE number [in HEX]
  256. *
  257. * If walk_info->execute_by_owner_id is TRUE, we only execute examine GPE methods
  258. * with that owner.
  259. * If walk_info->enable_this_gpe is TRUE, the GPE that is referred to by a GPE
  260. * method is immediately enabled (Used for Load/load_table operators)
  261. *
  262. ******************************************************************************/
  263. acpi_status
  264. acpi_ev_match_gpe_method(acpi_handle obj_handle,
  265. u32 level, void *context, void **return_value)
  266. {
  267. struct acpi_namespace_node *method_node =
  268. ACPI_CAST_PTR(struct acpi_namespace_node, obj_handle);
  269. struct acpi_gpe_walk_info *walk_info =
  270. ACPI_CAST_PTR(struct acpi_gpe_walk_info, context);
  271. struct acpi_gpe_event_info *gpe_event_info;
  272. struct acpi_namespace_node *gpe_device;
  273. acpi_status status;
  274. u32 gpe_number;
  275. char name[ACPI_NAME_SIZE + 1];
  276. u8 type;
  277. ACPI_FUNCTION_TRACE(ev_match_gpe_method);
  278. /* Check if requested owner_id matches this owner_id */
  279. if ((walk_info->execute_by_owner_id) &&
  280. (method_node->owner_id != walk_info->owner_id)) {
  281. return_ACPI_STATUS(AE_OK);
  282. }
  283. /*
  284. * Match and decode the _Lxx and _Exx GPE method names
  285. *
  286. * 1) Extract the method name and null terminate it
  287. */
  288. ACPI_MOVE_32_TO_32(name, &method_node->name.integer);
  289. name[ACPI_NAME_SIZE] = 0;
  290. /* 2) Name must begin with an underscore */
  291. if (name[0] != '_') {
  292. return_ACPI_STATUS(AE_OK); /* Ignore this method */
  293. }
  294. /*
  295. * 3) Edge/Level determination is based on the 2nd character
  296. * of the method name
  297. */
  298. switch (name[1]) {
  299. case 'L':
  300. type = ACPI_GPE_LEVEL_TRIGGERED;
  301. break;
  302. case 'E':
  303. type = ACPI_GPE_EDGE_TRIGGERED;
  304. break;
  305. default:
  306. /* Unknown method type, just ignore it */
  307. ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
  308. "Ignoring unknown GPE method type: %s "
  309. "(name not of form _Lxx or _Exx)", name));
  310. return_ACPI_STATUS(AE_OK);
  311. }
  312. /* 4) The last two characters of the name are the hex GPE Number */
  313. gpe_number = ACPI_STRTOUL(&name[2], NULL, 16);
  314. if (gpe_number == ACPI_UINT32_MAX) {
  315. /* Conversion failed; invalid method, just ignore it */
  316. ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
  317. "Could not extract GPE number from name: %s "
  318. "(name is not of form _Lxx or _Exx)", name));
  319. return_ACPI_STATUS(AE_OK);
  320. }
  321. /* Ensure that we have a valid GPE number for this GPE block */
  322. gpe_event_info =
  323. acpi_ev_low_get_gpe_info(gpe_number, walk_info->gpe_block);
  324. if (!gpe_event_info) {
  325. /*
  326. * This gpe_number is not valid for this GPE block, just ignore it.
  327. * However, it may be valid for a different GPE block, since GPE0
  328. * and GPE1 methods both appear under \_GPE.
  329. */
  330. return_ACPI_STATUS(AE_OK);
  331. }
  332. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  333. ACPI_GPE_DISPATCH_HANDLER) {
  334. /* If there is already a handler, ignore this GPE method */
  335. return_ACPI_STATUS(AE_OK);
  336. }
  337. if ((gpe_event_info->flags & ACPI_GPE_DISPATCH_MASK) ==
  338. ACPI_GPE_DISPATCH_METHOD) {
  339. /*
  340. * If there is already a method, ignore this method. But check
  341. * for a type mismatch (if both the _Lxx AND _Exx exist)
  342. */
  343. if (type != (gpe_event_info->flags & ACPI_GPE_XRUPT_TYPE_MASK)) {
  344. ACPI_ERROR((AE_INFO,
  345. "For GPE 0x%.2X, found both _L%2.2X and _E%2.2X methods",
  346. gpe_number, gpe_number, gpe_number));
  347. }
  348. return_ACPI_STATUS(AE_OK);
  349. }
  350. /*
  351. * Add the GPE information from above to the gpe_event_info block for
  352. * use during dispatch of this GPE.
  353. */
  354. gpe_event_info->flags |= (u8)(type | ACPI_GPE_DISPATCH_METHOD);
  355. gpe_event_info->dispatch.method_node = method_node;
  356. /*
  357. * Enable this GPE if requested. This only happens when during the
  358. * execution of a Load or load_table operator. We have found a new
  359. * GPE method and want to immediately enable the GPE if it is a
  360. * runtime GPE.
  361. */
  362. if (walk_info->enable_this_gpe) {
  363. walk_info->count++;
  364. gpe_device = walk_info->gpe_device;
  365. if (gpe_device == acpi_gbl_fadt_gpe_device) {
  366. gpe_device = NULL;
  367. }
  368. status = acpi_enable_gpe(gpe_device, gpe_number);
  369. if (ACPI_FAILURE(status)) {
  370. ACPI_EXCEPTION((AE_INFO, status,
  371. "Could not enable GPE 0x%02X",
  372. gpe_number));
  373. }
  374. }
  375. ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
  376. "Registered GPE method %s as GPE number 0x%.2X\n",
  377. name, gpe_number));
  378. return_ACPI_STATUS(AE_OK);
  379. }