exsystem.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /******************************************************************************
  2. *
  3. * Module Name: exsystem - Interface to OS services
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2019, 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.h"
  43. #include "accommon.h"
  44. #include "acinterp.h"
  45. #define _COMPONENT ACPI_EXECUTER
  46. ACPI_MODULE_NAME ("exsystem")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: AcpiExSystemWaitSemaphore
  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
  62. AcpiExSystemWaitSemaphore (
  63. ACPI_SEMAPHORE Semaphore,
  64. UINT16 Timeout)
  65. {
  66. ACPI_STATUS Status;
  67. ACPI_FUNCTION_TRACE (ExSystemWaitSemaphore);
  68. Status = AcpiOsWaitSemaphore (Semaphore, 1, ACPI_DO_NOT_WAIT);
  69. if (ACPI_SUCCESS (Status))
  70. {
  71. return_ACPI_STATUS (Status);
  72. }
  73. if (Status == AE_TIME)
  74. {
  75. /* We must wait, so unlock the interpreter */
  76. AcpiExExitInterpreter ();
  77. Status = AcpiOsWaitSemaphore (Semaphore, 1, Timeout);
  78. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
  79. "*** Thread awake after blocking, %s\n",
  80. AcpiFormatException (Status)));
  81. /* Reacquire the interpreter */
  82. AcpiExEnterInterpreter ();
  83. }
  84. return_ACPI_STATUS (Status);
  85. }
  86. /*******************************************************************************
  87. *
  88. * FUNCTION: AcpiExSystemWaitMutex
  89. *
  90. * PARAMETERS: Mutex - Mutex to wait on
  91. * Timeout - Max time to wait
  92. *
  93. * RETURN: Status
  94. *
  95. * DESCRIPTION: Implements a mutex wait with a check to see if the
  96. * mutex is available immediately. If it is not, the
  97. * interpreter is released before waiting.
  98. *
  99. ******************************************************************************/
  100. ACPI_STATUS
  101. AcpiExSystemWaitMutex (
  102. ACPI_MUTEX Mutex,
  103. UINT16 Timeout)
  104. {
  105. ACPI_STATUS Status;
  106. ACPI_FUNCTION_TRACE (ExSystemWaitMutex);
  107. Status = AcpiOsAcquireMutex (Mutex, ACPI_DO_NOT_WAIT);
  108. if (ACPI_SUCCESS (Status))
  109. {
  110. return_ACPI_STATUS (Status);
  111. }
  112. if (Status == AE_TIME)
  113. {
  114. /* We must wait, so unlock the interpreter */
  115. AcpiExExitInterpreter ();
  116. Status = AcpiOsAcquireMutex (Mutex, Timeout);
  117. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
  118. "*** Thread awake after blocking, %s\n",
  119. AcpiFormatException (Status)));
  120. /* Reacquire the interpreter */
  121. AcpiExEnterInterpreter ();
  122. }
  123. return_ACPI_STATUS (Status);
  124. }
  125. /*******************************************************************************
  126. *
  127. * FUNCTION: AcpiExSystemDoStall
  128. *
  129. * PARAMETERS: HowLong - The amount of time to stall,
  130. * in microseconds
  131. *
  132. * RETURN: Status
  133. *
  134. * DESCRIPTION: Suspend running thread for specified amount of time.
  135. * Note: ACPI specification requires that Stall() does not
  136. * relinquish the processor, and delays longer than 100 usec
  137. * should use Sleep() instead. We allow stalls up to 255 usec
  138. * for compatibility with other interpreters and existing BIOSs.
  139. *
  140. ******************************************************************************/
  141. ACPI_STATUS
  142. AcpiExSystemDoStall (
  143. UINT32 HowLong)
  144. {
  145. ACPI_STATUS Status = AE_OK;
  146. ACPI_FUNCTION_ENTRY ();
  147. if (HowLong > 255) /* 255 microseconds */
  148. {
  149. /*
  150. * Longer than 255 usec, this is an error
  151. *
  152. * (ACPI specifies 100 usec as max, but this gives some slack in
  153. * order to support existing BIOSs)
  154. */
  155. ACPI_ERROR ((AE_INFO,
  156. "Time parameter is too large (%u)", HowLong));
  157. Status = AE_AML_OPERAND_VALUE;
  158. }
  159. else
  160. {
  161. AcpiOsStall (HowLong);
  162. }
  163. return (Status);
  164. }
  165. /*******************************************************************************
  166. *
  167. * FUNCTION: AcpiExSystemDoSleep
  168. *
  169. * PARAMETERS: HowLong - The amount of time to sleep,
  170. * in milliseconds
  171. *
  172. * RETURN: None
  173. *
  174. * DESCRIPTION: Sleep the running thread for specified amount of time.
  175. *
  176. ******************************************************************************/
  177. ACPI_STATUS
  178. AcpiExSystemDoSleep (
  179. UINT64 HowLong)
  180. {
  181. ACPI_FUNCTION_ENTRY ();
  182. /* Since this thread will sleep, we must release the interpreter */
  183. AcpiExExitInterpreter ();
  184. /*
  185. * For compatibility with other ACPI implementations and to prevent
  186. * accidental deep sleeps, limit the sleep time to something reasonable.
  187. */
  188. if (HowLong > ACPI_MAX_SLEEP)
  189. {
  190. HowLong = ACPI_MAX_SLEEP;
  191. }
  192. AcpiOsSleep (HowLong);
  193. /* And now we must get the interpreter again */
  194. AcpiExEnterInterpreter ();
  195. return (AE_OK);
  196. }
  197. /*******************************************************************************
  198. *
  199. * FUNCTION: AcpiExSystemSignalEvent
  200. *
  201. * PARAMETERS: ObjDesc - The object descriptor for this op
  202. *
  203. * RETURN: Status
  204. *
  205. * DESCRIPTION: Provides an access point to perform synchronization operations
  206. * within the AML.
  207. *
  208. ******************************************************************************/
  209. ACPI_STATUS
  210. AcpiExSystemSignalEvent (
  211. ACPI_OPERAND_OBJECT *ObjDesc)
  212. {
  213. ACPI_STATUS Status = AE_OK;
  214. ACPI_FUNCTION_TRACE (ExSystemSignalEvent);
  215. if (ObjDesc)
  216. {
  217. Status = AcpiOsSignalSemaphore (ObjDesc->Event.OsSemaphore, 1);
  218. }
  219. return_ACPI_STATUS (Status);
  220. }
  221. /*******************************************************************************
  222. *
  223. * FUNCTION: AcpiExSystemWaitEvent
  224. *
  225. * PARAMETERS: TimeDesc - The 'time to delay' object descriptor
  226. * ObjDesc - The object descriptor for this op
  227. *
  228. * RETURN: Status
  229. *
  230. * DESCRIPTION: Provides an access point to perform synchronization operations
  231. * within the AML. This operation is a request to wait for an
  232. * event.
  233. *
  234. ******************************************************************************/
  235. ACPI_STATUS
  236. AcpiExSystemWaitEvent (
  237. ACPI_OPERAND_OBJECT *TimeDesc,
  238. ACPI_OPERAND_OBJECT *ObjDesc)
  239. {
  240. ACPI_STATUS Status = AE_OK;
  241. ACPI_FUNCTION_TRACE (ExSystemWaitEvent);
  242. if (ObjDesc)
  243. {
  244. Status = AcpiExSystemWaitSemaphore (ObjDesc->Event.OsSemaphore,
  245. (UINT16) TimeDesc->Integer.Value);
  246. }
  247. return_ACPI_STATUS (Status);
  248. }
  249. /*******************************************************************************
  250. *
  251. * FUNCTION: AcpiExSystemResetEvent
  252. *
  253. * PARAMETERS: ObjDesc - The object descriptor for this op
  254. *
  255. * RETURN: Status
  256. *
  257. * DESCRIPTION: Reset an event to a known state.
  258. *
  259. ******************************************************************************/
  260. ACPI_STATUS
  261. AcpiExSystemResetEvent (
  262. ACPI_OPERAND_OBJECT *ObjDesc)
  263. {
  264. ACPI_STATUS Status = AE_OK;
  265. ACPI_SEMAPHORE TempSemaphore;
  266. ACPI_FUNCTION_ENTRY ();
  267. /*
  268. * We are going to simply delete the existing semaphore and
  269. * create a new one!
  270. */
  271. Status = AcpiOsCreateSemaphore (ACPI_NO_UNIT_LIMIT, 0, &TempSemaphore);
  272. if (ACPI_SUCCESS (Status))
  273. {
  274. (void) AcpiOsDeleteSemaphore (ObjDesc->Event.OsSemaphore);
  275. ObjDesc->Event.OsSemaphore = TempSemaphore;
  276. }
  277. return (Status);
  278. }