hwgpe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: hwgpe - Low level GPE enable/disable/clear functions
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acevents.h"
  12. #define _COMPONENT ACPI_HARDWARE
  13. ACPI_MODULE_NAME("hwgpe")
  14. #if (!ACPI_REDUCED_HARDWARE) /* Entire module */
  15. /* Local prototypes */
  16. static acpi_status
  17. acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  18. struct acpi_gpe_block_info *gpe_block,
  19. void *context);
  20. static acpi_status
  21. acpi_hw_gpe_enable_write(u8 enable_mask,
  22. struct acpi_gpe_register_info *gpe_register_info);
  23. /******************************************************************************
  24. *
  25. * FUNCTION: acpi_hw_gpe_read
  26. *
  27. * PARAMETERS: value - Where the value is returned
  28. * reg - GPE register structure
  29. *
  30. * RETURN: Status
  31. *
  32. * DESCRIPTION: Read from a GPE register in either memory or IO space.
  33. *
  34. * LIMITATIONS: <These limitations also apply to acpi_hw_gpe_write>
  35. * space_ID must be system_memory or system_IO.
  36. *
  37. ******************************************************************************/
  38. acpi_status acpi_hw_gpe_read(u64 *value, struct acpi_gpe_address *reg)
  39. {
  40. acpi_status status;
  41. u32 value32;
  42. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  43. #ifdef ACPI_GPE_USE_LOGICAL_ADDRESSES
  44. *value = (u64)ACPI_GET8((unsigned long)reg->address);
  45. return_ACPI_STATUS(AE_OK);
  46. #else
  47. return acpi_os_read_memory((acpi_physical_address)reg->address,
  48. value, ACPI_GPE_REGISTER_WIDTH);
  49. #endif
  50. }
  51. status = acpi_os_read_port((acpi_io_address)reg->address,
  52. &value32, ACPI_GPE_REGISTER_WIDTH);
  53. if (ACPI_FAILURE(status))
  54. return_ACPI_STATUS(status);
  55. *value = (u64)value32;
  56. return_ACPI_STATUS(AE_OK);
  57. }
  58. /******************************************************************************
  59. *
  60. * FUNCTION: acpi_hw_gpe_write
  61. *
  62. * PARAMETERS: value - Value to be written
  63. * reg - GPE register structure
  64. *
  65. * RETURN: Status
  66. *
  67. * DESCRIPTION: Write to a GPE register in either memory or IO space.
  68. *
  69. ******************************************************************************/
  70. acpi_status acpi_hw_gpe_write(u64 value, struct acpi_gpe_address *reg)
  71. {
  72. if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  73. #ifdef ACPI_GPE_USE_LOGICAL_ADDRESSES
  74. ACPI_SET8((unsigned long)reg->address, value);
  75. return_ACPI_STATUS(AE_OK);
  76. #else
  77. return acpi_os_write_memory((acpi_physical_address)reg->address,
  78. value, ACPI_GPE_REGISTER_WIDTH);
  79. #endif
  80. }
  81. return acpi_os_write_port((acpi_io_address)reg->address, (u32)value,
  82. ACPI_GPE_REGISTER_WIDTH);
  83. }
  84. /******************************************************************************
  85. *
  86. * FUNCTION: acpi_hw_get_gpe_register_bit
  87. *
  88. * PARAMETERS: gpe_event_info - Info block for the GPE
  89. *
  90. * RETURN: Register mask with a one in the GPE bit position
  91. *
  92. * DESCRIPTION: Compute the register mask for this GPE. One bit is set in the
  93. * correct position for the input GPE.
  94. *
  95. ******************************************************************************/
  96. u32 acpi_hw_get_gpe_register_bit(struct acpi_gpe_event_info *gpe_event_info)
  97. {
  98. return ((u32)1 <<
  99. (gpe_event_info->gpe_number -
  100. gpe_event_info->register_info->base_gpe_number));
  101. }
  102. /******************************************************************************
  103. *
  104. * FUNCTION: acpi_hw_low_set_gpe
  105. *
  106. * PARAMETERS: gpe_event_info - Info block for the GPE to be disabled
  107. * action - Enable or disable
  108. *
  109. * RETURN: Status
  110. *
  111. * DESCRIPTION: Enable or disable a single GPE in the parent enable register.
  112. * The enable_mask field of the involved GPE register must be
  113. * updated by the caller if necessary.
  114. *
  115. ******************************************************************************/
  116. acpi_status
  117. acpi_hw_low_set_gpe(struct acpi_gpe_event_info *gpe_event_info, u32 action)
  118. {
  119. struct acpi_gpe_register_info *gpe_register_info;
  120. acpi_status status = AE_OK;
  121. u64 enable_mask;
  122. u32 register_bit;
  123. ACPI_FUNCTION_ENTRY();
  124. /* Get the info block for the entire GPE register */
  125. gpe_register_info = gpe_event_info->register_info;
  126. if (!gpe_register_info) {
  127. return (AE_NOT_EXIST);
  128. }
  129. /* Get current value of the enable register that contains this GPE */
  130. status = acpi_hw_gpe_read(&enable_mask,
  131. &gpe_register_info->enable_address);
  132. if (ACPI_FAILURE(status)) {
  133. return (status);
  134. }
  135. /* Set or clear just the bit that corresponds to this GPE */
  136. register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
  137. switch (action) {
  138. case ACPI_GPE_CONDITIONAL_ENABLE:
  139. /* Only enable if the corresponding enable_mask bit is set */
  140. if (!(register_bit & gpe_register_info->enable_mask)) {
  141. return (AE_BAD_PARAMETER);
  142. }
  143. ACPI_FALLTHROUGH;
  144. case ACPI_GPE_ENABLE:
  145. ACPI_SET_BIT(enable_mask, register_bit);
  146. break;
  147. case ACPI_GPE_DISABLE:
  148. ACPI_CLEAR_BIT(enable_mask, register_bit);
  149. break;
  150. default:
  151. ACPI_ERROR((AE_INFO, "Invalid GPE Action, %u", action));
  152. return (AE_BAD_PARAMETER);
  153. }
  154. if (!(register_bit & gpe_register_info->mask_for_run)) {
  155. /* Write the updated enable mask */
  156. status = acpi_hw_gpe_write(enable_mask,
  157. &gpe_register_info->enable_address);
  158. }
  159. return (status);
  160. }
  161. /******************************************************************************
  162. *
  163. * FUNCTION: acpi_hw_clear_gpe
  164. *
  165. * PARAMETERS: gpe_event_info - Info block for the GPE to be cleared
  166. *
  167. * RETURN: Status
  168. *
  169. * DESCRIPTION: Clear the status bit for a single GPE.
  170. *
  171. ******************************************************************************/
  172. acpi_status acpi_hw_clear_gpe(struct acpi_gpe_event_info *gpe_event_info)
  173. {
  174. struct acpi_gpe_register_info *gpe_register_info;
  175. acpi_status status;
  176. u32 register_bit;
  177. ACPI_FUNCTION_ENTRY();
  178. /* Get the info block for the entire GPE register */
  179. gpe_register_info = gpe_event_info->register_info;
  180. if (!gpe_register_info) {
  181. return (AE_NOT_EXIST);
  182. }
  183. /*
  184. * Write a one to the appropriate bit in the status register to
  185. * clear this GPE.
  186. */
  187. register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
  188. status = acpi_hw_gpe_write(register_bit,
  189. &gpe_register_info->status_address);
  190. return (status);
  191. }
  192. /******************************************************************************
  193. *
  194. * FUNCTION: acpi_hw_get_gpe_status
  195. *
  196. * PARAMETERS: gpe_event_info - Info block for the GPE to queried
  197. * event_status - Where the GPE status is returned
  198. *
  199. * RETURN: Status
  200. *
  201. * DESCRIPTION: Return the status of a single GPE.
  202. *
  203. ******************************************************************************/
  204. acpi_status
  205. acpi_hw_get_gpe_status(struct acpi_gpe_event_info *gpe_event_info,
  206. acpi_event_status *event_status)
  207. {
  208. u64 in_byte;
  209. u32 register_bit;
  210. struct acpi_gpe_register_info *gpe_register_info;
  211. acpi_event_status local_event_status = 0;
  212. acpi_status status;
  213. ACPI_FUNCTION_ENTRY();
  214. if (!event_status) {
  215. return (AE_BAD_PARAMETER);
  216. }
  217. /* GPE currently handled? */
  218. if (ACPI_GPE_DISPATCH_TYPE(gpe_event_info->flags) !=
  219. ACPI_GPE_DISPATCH_NONE) {
  220. local_event_status |= ACPI_EVENT_FLAG_HAS_HANDLER;
  221. }
  222. /* Get the info block for the entire GPE register */
  223. gpe_register_info = gpe_event_info->register_info;
  224. /* Get the register bitmask for this GPE */
  225. register_bit = acpi_hw_get_gpe_register_bit(gpe_event_info);
  226. /* GPE currently enabled? (enabled for runtime?) */
  227. if (register_bit & gpe_register_info->enable_for_run) {
  228. local_event_status |= ACPI_EVENT_FLAG_ENABLED;
  229. }
  230. /* GPE currently masked? (masked for runtime?) */
  231. if (register_bit & gpe_register_info->mask_for_run) {
  232. local_event_status |= ACPI_EVENT_FLAG_MASKED;
  233. }
  234. /* GPE enabled for wake? */
  235. if (register_bit & gpe_register_info->enable_for_wake) {
  236. local_event_status |= ACPI_EVENT_FLAG_WAKE_ENABLED;
  237. }
  238. /* GPE currently enabled (enable bit == 1)? */
  239. status = acpi_hw_gpe_read(&in_byte, &gpe_register_info->enable_address);
  240. if (ACPI_FAILURE(status)) {
  241. return (status);
  242. }
  243. if (register_bit & in_byte) {
  244. local_event_status |= ACPI_EVENT_FLAG_ENABLE_SET;
  245. }
  246. /* GPE currently active (status bit == 1)? */
  247. status = acpi_hw_gpe_read(&in_byte, &gpe_register_info->status_address);
  248. if (ACPI_FAILURE(status)) {
  249. return (status);
  250. }
  251. if (register_bit & in_byte) {
  252. local_event_status |= ACPI_EVENT_FLAG_STATUS_SET;
  253. }
  254. /* Set return value */
  255. (*event_status) = local_event_status;
  256. return (AE_OK);
  257. }
  258. /******************************************************************************
  259. *
  260. * FUNCTION: acpi_hw_gpe_enable_write
  261. *
  262. * PARAMETERS: enable_mask - Bit mask to write to the GPE register
  263. * gpe_register_info - Gpe Register info
  264. *
  265. * RETURN: Status
  266. *
  267. * DESCRIPTION: Write the enable mask byte to the given GPE register.
  268. *
  269. ******************************************************************************/
  270. static acpi_status
  271. acpi_hw_gpe_enable_write(u8 enable_mask,
  272. struct acpi_gpe_register_info *gpe_register_info)
  273. {
  274. acpi_status status;
  275. gpe_register_info->enable_mask = enable_mask;
  276. status = acpi_hw_gpe_write(enable_mask,
  277. &gpe_register_info->enable_address);
  278. return (status);
  279. }
  280. /******************************************************************************
  281. *
  282. * FUNCTION: acpi_hw_disable_gpe_block
  283. *
  284. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  285. * gpe_block - Gpe Block info
  286. *
  287. * RETURN: Status
  288. *
  289. * DESCRIPTION: Disable all GPEs within a single GPE block
  290. *
  291. ******************************************************************************/
  292. acpi_status
  293. acpi_hw_disable_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  294. struct acpi_gpe_block_info *gpe_block, void *context)
  295. {
  296. u32 i;
  297. acpi_status status;
  298. /* Examine each GPE Register within the block */
  299. for (i = 0; i < gpe_block->register_count; i++) {
  300. /* Disable all GPEs in this register */
  301. status =
  302. acpi_hw_gpe_enable_write(0x00,
  303. &gpe_block->register_info[i]);
  304. if (ACPI_FAILURE(status)) {
  305. return (status);
  306. }
  307. }
  308. return (AE_OK);
  309. }
  310. /******************************************************************************
  311. *
  312. * FUNCTION: acpi_hw_clear_gpe_block
  313. *
  314. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  315. * gpe_block - Gpe Block info
  316. *
  317. * RETURN: Status
  318. *
  319. * DESCRIPTION: Clear status bits for all GPEs within a single GPE block
  320. *
  321. ******************************************************************************/
  322. acpi_status
  323. acpi_hw_clear_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  324. struct acpi_gpe_block_info *gpe_block, void *context)
  325. {
  326. u32 i;
  327. acpi_status status;
  328. /* Examine each GPE Register within the block */
  329. for (i = 0; i < gpe_block->register_count; i++) {
  330. /* Clear status on all GPEs in this register */
  331. status = acpi_hw_gpe_write(0xFF,
  332. &gpe_block->register_info[i].status_address);
  333. if (ACPI_FAILURE(status)) {
  334. return (status);
  335. }
  336. }
  337. return (AE_OK);
  338. }
  339. /******************************************************************************
  340. *
  341. * FUNCTION: acpi_hw_enable_runtime_gpe_block
  342. *
  343. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  344. * gpe_block - Gpe Block info
  345. *
  346. * RETURN: Status
  347. *
  348. * DESCRIPTION: Enable all "runtime" GPEs within a single GPE block. Includes
  349. * combination wake/run GPEs.
  350. *
  351. ******************************************************************************/
  352. acpi_status
  353. acpi_hw_enable_runtime_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  354. struct acpi_gpe_block_info *gpe_block,
  355. void *context)
  356. {
  357. u32 i;
  358. acpi_status status;
  359. struct acpi_gpe_register_info *gpe_register_info;
  360. u8 enable_mask;
  361. /* NOTE: assumes that all GPEs are currently disabled */
  362. /* Examine each GPE Register within the block */
  363. for (i = 0; i < gpe_block->register_count; i++) {
  364. gpe_register_info = &gpe_block->register_info[i];
  365. if (!gpe_register_info->enable_for_run) {
  366. continue;
  367. }
  368. /* Enable all "runtime" GPEs in this register */
  369. enable_mask = gpe_register_info->enable_for_run &
  370. ~gpe_register_info->mask_for_run;
  371. status =
  372. acpi_hw_gpe_enable_write(enable_mask, gpe_register_info);
  373. if (ACPI_FAILURE(status)) {
  374. return (status);
  375. }
  376. }
  377. return (AE_OK);
  378. }
  379. /******************************************************************************
  380. *
  381. * FUNCTION: acpi_hw_enable_wakeup_gpe_block
  382. *
  383. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  384. * gpe_block - Gpe Block info
  385. *
  386. * RETURN: Status
  387. *
  388. * DESCRIPTION: Enable all "wake" GPEs within a single GPE block. Includes
  389. * combination wake/run GPEs.
  390. *
  391. ******************************************************************************/
  392. static acpi_status
  393. acpi_hw_enable_wakeup_gpe_block(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  394. struct acpi_gpe_block_info *gpe_block,
  395. void *context)
  396. {
  397. u32 i;
  398. acpi_status status;
  399. struct acpi_gpe_register_info *gpe_register_info;
  400. /* Examine each GPE Register within the block */
  401. for (i = 0; i < gpe_block->register_count; i++) {
  402. gpe_register_info = &gpe_block->register_info[i];
  403. /*
  404. * Enable all "wake" GPEs in this register and disable the
  405. * remaining ones.
  406. */
  407. status =
  408. acpi_hw_gpe_enable_write(gpe_register_info->enable_for_wake,
  409. gpe_register_info);
  410. if (ACPI_FAILURE(status)) {
  411. return (status);
  412. }
  413. }
  414. return (AE_OK);
  415. }
  416. struct acpi_gpe_block_status_context {
  417. struct acpi_gpe_register_info *gpe_skip_register_info;
  418. u8 gpe_skip_mask;
  419. u8 retval;
  420. };
  421. /******************************************************************************
  422. *
  423. * FUNCTION: acpi_hw_get_gpe_block_status
  424. *
  425. * PARAMETERS: gpe_xrupt_info - GPE Interrupt info
  426. * gpe_block - Gpe Block info
  427. * context - GPE list walk context data
  428. *
  429. * RETURN: Success
  430. *
  431. * DESCRIPTION: Produce a combined GPE status bits mask for the given block.
  432. *
  433. ******************************************************************************/
  434. static acpi_status
  435. acpi_hw_get_gpe_block_status(struct acpi_gpe_xrupt_info *gpe_xrupt_info,
  436. struct acpi_gpe_block_info *gpe_block,
  437. void *context)
  438. {
  439. struct acpi_gpe_block_status_context *c = context;
  440. struct acpi_gpe_register_info *gpe_register_info;
  441. u64 in_enable, in_status;
  442. acpi_status status;
  443. u8 ret_mask;
  444. u32 i;
  445. /* Examine each GPE Register within the block */
  446. for (i = 0; i < gpe_block->register_count; i++) {
  447. gpe_register_info = &gpe_block->register_info[i];
  448. status = acpi_hw_gpe_read(&in_enable,
  449. &gpe_register_info->enable_address);
  450. if (ACPI_FAILURE(status)) {
  451. continue;
  452. }
  453. status = acpi_hw_gpe_read(&in_status,
  454. &gpe_register_info->status_address);
  455. if (ACPI_FAILURE(status)) {
  456. continue;
  457. }
  458. ret_mask = in_enable & in_status;
  459. if (ret_mask && c->gpe_skip_register_info == gpe_register_info) {
  460. ret_mask &= ~c->gpe_skip_mask;
  461. }
  462. c->retval |= ret_mask;
  463. }
  464. return (AE_OK);
  465. }
  466. /******************************************************************************
  467. *
  468. * FUNCTION: acpi_hw_disable_all_gpes
  469. *
  470. * PARAMETERS: None
  471. *
  472. * RETURN: Status
  473. *
  474. * DESCRIPTION: Disable and clear all GPEs in all GPE blocks
  475. *
  476. ******************************************************************************/
  477. acpi_status acpi_hw_disable_all_gpes(void)
  478. {
  479. acpi_status status;
  480. ACPI_FUNCTION_TRACE(hw_disable_all_gpes);
  481. status = acpi_ev_walk_gpe_list(acpi_hw_disable_gpe_block, NULL);
  482. return_ACPI_STATUS(status);
  483. }
  484. /******************************************************************************
  485. *
  486. * FUNCTION: acpi_hw_enable_all_runtime_gpes
  487. *
  488. * PARAMETERS: None
  489. *
  490. * RETURN: Status
  491. *
  492. * DESCRIPTION: Enable all "runtime" GPEs, in all GPE blocks
  493. *
  494. ******************************************************************************/
  495. acpi_status acpi_hw_enable_all_runtime_gpes(void)
  496. {
  497. acpi_status status;
  498. ACPI_FUNCTION_TRACE(hw_enable_all_runtime_gpes);
  499. status = acpi_ev_walk_gpe_list(acpi_hw_enable_runtime_gpe_block, NULL);
  500. return_ACPI_STATUS(status);
  501. }
  502. /******************************************************************************
  503. *
  504. * FUNCTION: acpi_hw_enable_all_wakeup_gpes
  505. *
  506. * PARAMETERS: None
  507. *
  508. * RETURN: Status
  509. *
  510. * DESCRIPTION: Enable all "wakeup" GPEs, in all GPE blocks
  511. *
  512. ******************************************************************************/
  513. acpi_status acpi_hw_enable_all_wakeup_gpes(void)
  514. {
  515. acpi_status status;
  516. ACPI_FUNCTION_TRACE(hw_enable_all_wakeup_gpes);
  517. status = acpi_ev_walk_gpe_list(acpi_hw_enable_wakeup_gpe_block, NULL);
  518. return_ACPI_STATUS(status);
  519. }
  520. /******************************************************************************
  521. *
  522. * FUNCTION: acpi_hw_check_all_gpes
  523. *
  524. * PARAMETERS: gpe_skip_device - GPE devoce of the GPE to skip
  525. * gpe_skip_number - Number of the GPE to skip
  526. *
  527. * RETURN: Combined status of all GPEs
  528. *
  529. * DESCRIPTION: Check all enabled GPEs in all GPE blocks, except for the one
  530. * represented by the "skip" arguments, and return TRUE if the
  531. * status bit is set for at least one of them of FALSE otherwise.
  532. *
  533. ******************************************************************************/
  534. u8 acpi_hw_check_all_gpes(acpi_handle gpe_skip_device, u32 gpe_skip_number)
  535. {
  536. struct acpi_gpe_block_status_context context = {
  537. .gpe_skip_register_info = NULL,
  538. .retval = 0,
  539. };
  540. struct acpi_gpe_event_info *gpe_event_info;
  541. acpi_cpu_flags flags;
  542. ACPI_FUNCTION_TRACE(acpi_hw_check_all_gpes);
  543. flags = acpi_os_acquire_lock(acpi_gbl_gpe_lock);
  544. gpe_event_info = acpi_ev_get_gpe_event_info(gpe_skip_device,
  545. gpe_skip_number);
  546. if (gpe_event_info) {
  547. context.gpe_skip_register_info = gpe_event_info->register_info;
  548. context.gpe_skip_mask = acpi_hw_get_gpe_register_bit(gpe_event_info);
  549. }
  550. acpi_os_release_lock(acpi_gbl_gpe_lock, flags);
  551. (void)acpi_ev_walk_gpe_list(acpi_hw_get_gpe_block_status, &context);
  552. return (context.retval != 0);
  553. }
  554. #endif /* !ACPI_REDUCED_HARDWARE */