exsystem.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 - 2022, 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((AE_INFO,
  110. "Time parameter is too large (%u)", how_long_us));
  111. status = AE_AML_OPERAND_VALUE;
  112. } else {
  113. if (how_long_us > 100) {
  114. ACPI_WARNING((AE_INFO,
  115. "Time parameter %u us > 100 us violating ACPI spec, please fix the firmware.",
  116. how_long_us));
  117. }
  118. acpi_os_stall(how_long_us);
  119. }
  120. return (status);
  121. }
  122. /*******************************************************************************
  123. *
  124. * FUNCTION: acpi_ex_system_do_sleep
  125. *
  126. * PARAMETERS: how_long_ms - The amount of time to sleep,
  127. * in milliseconds
  128. *
  129. * RETURN: None
  130. *
  131. * DESCRIPTION: Sleep the running thread for specified amount of time.
  132. *
  133. ******************************************************************************/
  134. acpi_status acpi_ex_system_do_sleep(u64 how_long_ms)
  135. {
  136. ACPI_FUNCTION_ENTRY();
  137. /* Since this thread will sleep, we must release the interpreter */
  138. acpi_ex_exit_interpreter();
  139. /*
  140. * For compatibility with other ACPI implementations and to prevent
  141. * accidental deep sleeps, limit the sleep time to something reasonable.
  142. */
  143. if (how_long_ms > ACPI_MAX_SLEEP) {
  144. how_long_ms = ACPI_MAX_SLEEP;
  145. }
  146. acpi_os_sleep(how_long_ms);
  147. /* And now we must get the interpreter again */
  148. acpi_ex_enter_interpreter();
  149. return (AE_OK);
  150. }
  151. /*******************************************************************************
  152. *
  153. * FUNCTION: acpi_ex_system_signal_event
  154. *
  155. * PARAMETERS: obj_desc - The object descriptor for this op
  156. *
  157. * RETURN: Status
  158. *
  159. * DESCRIPTION: Provides an access point to perform synchronization operations
  160. * within the AML.
  161. *
  162. ******************************************************************************/
  163. acpi_status acpi_ex_system_signal_event(union acpi_operand_object * obj_desc)
  164. {
  165. acpi_status status = AE_OK;
  166. ACPI_FUNCTION_TRACE(ex_system_signal_event);
  167. if (obj_desc) {
  168. status =
  169. acpi_os_signal_semaphore(obj_desc->event.os_semaphore, 1);
  170. }
  171. return_ACPI_STATUS(status);
  172. }
  173. /*******************************************************************************
  174. *
  175. * FUNCTION: acpi_ex_system_wait_event
  176. *
  177. * PARAMETERS: time_desc - The 'time to delay' object descriptor
  178. * obj_desc - The object descriptor for this op
  179. *
  180. * RETURN: Status
  181. *
  182. * DESCRIPTION: Provides an access point to perform synchronization operations
  183. * within the AML. This operation is a request to wait for an
  184. * event.
  185. *
  186. ******************************************************************************/
  187. acpi_status
  188. acpi_ex_system_wait_event(union acpi_operand_object *time_desc,
  189. union acpi_operand_object *obj_desc)
  190. {
  191. acpi_status status = AE_OK;
  192. ACPI_FUNCTION_TRACE(ex_system_wait_event);
  193. if (obj_desc) {
  194. status =
  195. acpi_ex_system_wait_semaphore(obj_desc->event.os_semaphore,
  196. (u16) time_desc->integer.
  197. value);
  198. }
  199. return_ACPI_STATUS(status);
  200. }
  201. /*******************************************************************************
  202. *
  203. * FUNCTION: acpi_ex_system_reset_event
  204. *
  205. * PARAMETERS: obj_desc - The object descriptor for this op
  206. *
  207. * RETURN: Status
  208. *
  209. * DESCRIPTION: Reset an event to a known state.
  210. *
  211. ******************************************************************************/
  212. acpi_status acpi_ex_system_reset_event(union acpi_operand_object *obj_desc)
  213. {
  214. acpi_status status = AE_OK;
  215. acpi_semaphore temp_semaphore;
  216. ACPI_FUNCTION_ENTRY();
  217. /*
  218. * We are going to simply delete the existing semaphore and
  219. * create a new one!
  220. */
  221. status =
  222. acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0, &temp_semaphore);
  223. if (ACPI_SUCCESS(status)) {
  224. (void)acpi_os_delete_semaphore(obj_desc->event.os_semaphore);
  225. obj_desc->event.os_semaphore = temp_semaphore;
  226. }
  227. return (status);
  228. }