exsystem.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exsystem - Interface to OS services
  5. *
  6. * Copyright (C) 2000 - 2025, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #define _COMPONENT ACPI_EXECUTER
  13. ACPI_MODULE_NAME("exsystem")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: acpi_ex_system_wait_semaphore
  17. *
  18. * PARAMETERS: semaphore - Semaphore to wait on
  19. * timeout - Max time to wait
  20. *
  21. * RETURN: Status
  22. *
  23. * DESCRIPTION: Implements a semaphore wait with a check to see if the
  24. * semaphore is available immediately. If it is not, the
  25. * interpreter is released before waiting.
  26. *
  27. ******************************************************************************/
  28. acpi_status acpi_ex_system_wait_semaphore(acpi_semaphore semaphore, u16 timeout)
  29. {
  30. acpi_status status;
  31. ACPI_FUNCTION_TRACE(ex_system_wait_semaphore);
  32. status = acpi_os_wait_semaphore(semaphore, 1, ACPI_DO_NOT_WAIT);
  33. if (ACPI_SUCCESS(status)) {
  34. return_ACPI_STATUS(status);
  35. }
  36. if (status == AE_TIME) {
  37. /* We must wait, so unlock the interpreter */
  38. acpi_ex_exit_interpreter();
  39. status = acpi_os_wait_semaphore(semaphore, 1, timeout);
  40. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  41. "*** Thread awake after blocking, %s\n",
  42. acpi_format_exception(status)));
  43. /* Reacquire the interpreter */
  44. acpi_ex_enter_interpreter();
  45. }
  46. return_ACPI_STATUS(status);
  47. }
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ex_system_wait_mutex
  51. *
  52. * PARAMETERS: mutex - Mutex to wait on
  53. * timeout - Max time to wait
  54. *
  55. * RETURN: Status
  56. *
  57. * DESCRIPTION: Implements a mutex wait with a check to see if the
  58. * mutex is available immediately. If it is not, the
  59. * interpreter is released before waiting.
  60. *
  61. ******************************************************************************/
  62. acpi_status acpi_ex_system_wait_mutex(acpi_mutex mutex, u16 timeout)
  63. {
  64. acpi_status status;
  65. ACPI_FUNCTION_TRACE(ex_system_wait_mutex);
  66. status = acpi_os_acquire_mutex(mutex, ACPI_DO_NOT_WAIT);
  67. if (ACPI_SUCCESS(status)) {
  68. return_ACPI_STATUS(status);
  69. }
  70. if (status == AE_TIME) {
  71. /* We must wait, so unlock the interpreter */
  72. acpi_ex_exit_interpreter();
  73. status = acpi_os_acquire_mutex(mutex, timeout);
  74. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  75. "*** Thread awake after blocking, %s\n",
  76. acpi_format_exception(status)));
  77. /* Reacquire the interpreter */
  78. acpi_ex_enter_interpreter();
  79. }
  80. return_ACPI_STATUS(status);
  81. }
  82. /*******************************************************************************
  83. *
  84. * FUNCTION: acpi_ex_system_do_stall
  85. *
  86. * PARAMETERS: how_long_us - The amount of time to stall,
  87. * in microseconds
  88. *
  89. * RETURN: Status
  90. *
  91. * DESCRIPTION: Suspend running thread for specified amount of time.
  92. * Note: ACPI specification requires that Stall() does not
  93. * relinquish the processor, and delays longer than 100 usec
  94. * should use Sleep() instead. We allow stalls up to 255 usec
  95. * for compatibility with other interpreters and existing BIOSs.
  96. *
  97. ******************************************************************************/
  98. acpi_status acpi_ex_system_do_stall(u32 how_long_us)
  99. {
  100. acpi_status status = AE_OK;
  101. ACPI_FUNCTION_ENTRY();
  102. if (how_long_us > 255) {
  103. /*
  104. * Longer than 255 microseconds, this is an error
  105. *
  106. * (ACPI specifies 100 usec as max, but this gives some slack in
  107. * order to support existing BIOSs)
  108. */
  109. ACPI_ERROR_ONCE((AE_INFO,
  110. "Time parameter is too large (%u)",
  111. how_long_us));
  112. status = AE_AML_OPERAND_VALUE;
  113. } else {
  114. if (how_long_us > 100) {
  115. ACPI_WARNING_ONCE((AE_INFO,
  116. "Time parameter %u us > 100 us violating ACPI spec, please fix the firmware.",
  117. how_long_us));
  118. }
  119. acpi_os_stall(how_long_us);
  120. }
  121. return (status);
  122. }
  123. /*******************************************************************************
  124. *
  125. * FUNCTION: acpi_ex_system_do_sleep
  126. *
  127. * PARAMETERS: how_long_ms - The amount of time to sleep,
  128. * in milliseconds
  129. *
  130. * RETURN: None
  131. *
  132. * DESCRIPTION: Sleep the running thread for specified amount of time.
  133. *
  134. ******************************************************************************/
  135. acpi_status acpi_ex_system_do_sleep(u64 how_long_ms)
  136. {
  137. ACPI_FUNCTION_ENTRY();
  138. /* Since this thread will sleep, we must release the interpreter */
  139. acpi_ex_exit_interpreter();
  140. /*
  141. * For compatibility with other ACPI implementations and to prevent
  142. * accidental deep sleeps, limit the sleep time to something reasonable.
  143. */
  144. if (how_long_ms > ACPI_MAX_SLEEP) {
  145. how_long_ms = ACPI_MAX_SLEEP;
  146. }
  147. acpi_os_sleep(how_long_ms);
  148. /* And now we must get the interpreter again */
  149. acpi_ex_enter_interpreter();
  150. return (AE_OK);
  151. }
  152. /*******************************************************************************
  153. *
  154. * FUNCTION: acpi_ex_system_signal_event
  155. *
  156. * PARAMETERS: obj_desc - The object descriptor for this op
  157. *
  158. * RETURN: Status
  159. *
  160. * DESCRIPTION: Provides an access point to perform synchronization operations
  161. * within the AML.
  162. *
  163. ******************************************************************************/
  164. acpi_status acpi_ex_system_signal_event(union acpi_operand_object * obj_desc)
  165. {
  166. acpi_status status = AE_OK;
  167. ACPI_FUNCTION_TRACE(ex_system_signal_event);
  168. if (obj_desc) {
  169. status =
  170. acpi_os_signal_semaphore(obj_desc->event.os_semaphore, 1);
  171. }
  172. return_ACPI_STATUS(status);
  173. }
  174. /*******************************************************************************
  175. *
  176. * FUNCTION: acpi_ex_system_wait_event
  177. *
  178. * PARAMETERS: time_desc - The 'time to delay' object descriptor
  179. * obj_desc - The object descriptor for this op
  180. *
  181. * RETURN: Status
  182. *
  183. * DESCRIPTION: Provides an access point to perform synchronization operations
  184. * within the AML. This operation is a request to wait for an
  185. * event.
  186. *
  187. ******************************************************************************/
  188. acpi_status
  189. acpi_ex_system_wait_event(union acpi_operand_object *time_desc,
  190. union acpi_operand_object *obj_desc)
  191. {
  192. acpi_status status = AE_OK;
  193. ACPI_FUNCTION_TRACE(ex_system_wait_event);
  194. if (obj_desc) {
  195. status =
  196. acpi_ex_system_wait_semaphore(obj_desc->event.os_semaphore,
  197. (u16) time_desc->integer.
  198. value);
  199. }
  200. return_ACPI_STATUS(status);
  201. }
  202. /*******************************************************************************
  203. *
  204. * FUNCTION: acpi_ex_system_reset_event
  205. *
  206. * PARAMETERS: obj_desc - The object descriptor for this op
  207. *
  208. * RETURN: Status
  209. *
  210. * DESCRIPTION: Reset an event to a known state.
  211. *
  212. ******************************************************************************/
  213. acpi_status acpi_ex_system_reset_event(union acpi_operand_object *obj_desc)
  214. {
  215. acpi_status status = AE_OK;
  216. acpi_semaphore temp_semaphore;
  217. ACPI_FUNCTION_ENTRY();
  218. /*
  219. * We are going to simply delete the existing semaphore and
  220. * create a new one!
  221. */
  222. status =
  223. acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0, &temp_semaphore);
  224. if (ACPI_SUCCESS(status)) {
  225. (void)acpi_os_delete_semaphore(obj_desc->event.os_semaphore);
  226. obj_desc->event.os_semaphore = temp_semaphore;
  227. }
  228. return (status);
  229. }