exsystem.c 9.1 KB

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