exsystem.c 7.3 KB

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