exsystem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /******************************************************************************
  2. *
  3. * Module Name: exsystem - Interface to OS services
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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 <acpi/acinterp.h>
  44. #include <acpi/acevents.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_status status2;
  65. ACPI_FUNCTION_TRACE(ex_system_wait_semaphore);
  66. status = acpi_os_wait_semaphore(semaphore, 1, 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_wait_semaphore(semaphore, 1, 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. status2 = acpi_ex_enter_interpreter();
  79. if (ACPI_FAILURE(status2)) {
  80. /* Report fatal error, could not acquire interpreter */
  81. return_ACPI_STATUS(status2);
  82. }
  83. }
  84. return_ACPI_STATUS(status);
  85. }
  86. /*******************************************************************************
  87. *
  88. * FUNCTION: acpi_ex_system_wait_mutex
  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 acpi_ex_system_wait_mutex(acpi_mutex mutex, u16 timeout)
  101. {
  102. acpi_status status;
  103. acpi_status status2;
  104. ACPI_FUNCTION_TRACE(ex_system_wait_mutex);
  105. status = acpi_os_acquire_mutex(mutex, ACPI_DO_NOT_WAIT);
  106. if (ACPI_SUCCESS(status)) {
  107. return_ACPI_STATUS(status);
  108. }
  109. if (status == AE_TIME) {
  110. /* We must wait, so unlock the interpreter */
  111. acpi_ex_exit_interpreter();
  112. status = acpi_os_acquire_mutex(mutex, timeout);
  113. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  114. "*** Thread awake after blocking, %s\n",
  115. acpi_format_exception(status)));
  116. /* Reacquire the interpreter */
  117. status2 = acpi_ex_enter_interpreter();
  118. if (ACPI_FAILURE(status2)) {
  119. /* Report fatal error, could not acquire interpreter */
  120. return_ACPI_STATUS(status2);
  121. }
  122. }
  123. return_ACPI_STATUS(status);
  124. }
  125. /*******************************************************************************
  126. *
  127. * FUNCTION: acpi_ex_system_do_stall
  128. *
  129. * PARAMETERS: how_long - 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 acpi_ex_system_do_stall(u32 how_long)
  142. {
  143. acpi_status status = AE_OK;
  144. ACPI_FUNCTION_ENTRY();
  145. if (how_long > 255) { /* 255 microseconds */
  146. /*
  147. * Longer than 255 usec, this is an error
  148. *
  149. * (ACPI specifies 100 usec as max, but this gives some slack in
  150. * order to support existing BIOSs)
  151. */
  152. ACPI_ERROR((AE_INFO, "Time parameter is too large (%d)",
  153. how_long));
  154. status = AE_AML_OPERAND_VALUE;
  155. } else {
  156. acpi_os_stall(how_long);
  157. }
  158. return (status);
  159. }
  160. /*******************************************************************************
  161. *
  162. * FUNCTION: acpi_ex_system_do_suspend
  163. *
  164. * PARAMETERS: how_long - The amount of time to suspend,
  165. * in milliseconds
  166. *
  167. * RETURN: None
  168. *
  169. * DESCRIPTION: Suspend running thread for specified amount of time.
  170. *
  171. ******************************************************************************/
  172. acpi_status acpi_ex_system_do_suspend(acpi_integer how_long)
  173. {
  174. acpi_status status;
  175. ACPI_FUNCTION_ENTRY();
  176. /* Since this thread will sleep, we must release the interpreter */
  177. acpi_ex_exit_interpreter();
  178. acpi_os_sleep(how_long);
  179. /* And now we must get the interpreter again */
  180. status = acpi_ex_enter_interpreter();
  181. return (status);
  182. }
  183. /*******************************************************************************
  184. *
  185. * FUNCTION: acpi_ex_system_acquire_mutex
  186. *
  187. * PARAMETERS: time_desc - Maximum time to wait for the mutex
  188. * obj_desc - The object descriptor for this op
  189. *
  190. * RETURN: Status
  191. *
  192. * DESCRIPTION: Provides an access point to perform synchronization operations
  193. * within the AML. This function will cause a lock to be generated
  194. * for the Mutex pointed to by obj_desc.
  195. *
  196. ******************************************************************************/
  197. acpi_status
  198. acpi_ex_system_acquire_mutex(union acpi_operand_object * time_desc,
  199. union acpi_operand_object * obj_desc)
  200. {
  201. acpi_status status = AE_OK;
  202. ACPI_FUNCTION_TRACE_PTR(ex_system_acquire_mutex, obj_desc);
  203. if (!obj_desc) {
  204. return_ACPI_STATUS(AE_BAD_PARAMETER);
  205. }
  206. /* Support for the _GL_ Mutex object -- go get the global lock */
  207. if (obj_desc->mutex.os_mutex == ACPI_GLOBAL_LOCK) {
  208. status =
  209. acpi_ev_acquire_global_lock((u16) time_desc->integer.value);
  210. return_ACPI_STATUS(status);
  211. }
  212. status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
  213. (u16) time_desc->integer.value);
  214. return_ACPI_STATUS(status);
  215. }
  216. /*******************************************************************************
  217. *
  218. * FUNCTION: acpi_ex_system_release_mutex
  219. *
  220. * PARAMETERS: obj_desc - The object descriptor for this op
  221. *
  222. * RETURN: Status
  223. *
  224. * DESCRIPTION: Provides an access point to perform synchronization operations
  225. * within the AML. This operation is a request to release a
  226. * previously acquired Mutex. If the Mutex variable is set then
  227. * it will be decremented.
  228. *
  229. ******************************************************************************/
  230. acpi_status acpi_ex_system_release_mutex(union acpi_operand_object *obj_desc)
  231. {
  232. acpi_status status = AE_OK;
  233. ACPI_FUNCTION_TRACE(ex_system_release_mutex);
  234. if (!obj_desc) {
  235. return_ACPI_STATUS(AE_BAD_PARAMETER);
  236. }
  237. /* Support for the _GL_ Mutex object -- release the global lock */
  238. if (obj_desc->mutex.os_mutex == ACPI_GLOBAL_LOCK) {
  239. status = acpi_ev_release_global_lock();
  240. return_ACPI_STATUS(status);
  241. }
  242. acpi_os_release_mutex(obj_desc->mutex.os_mutex);
  243. return_ACPI_STATUS(AE_OK);
  244. }
  245. /*******************************************************************************
  246. *
  247. * FUNCTION: acpi_ex_system_signal_event
  248. *
  249. * PARAMETERS: obj_desc - The object descriptor for this op
  250. *
  251. * RETURN: Status
  252. *
  253. * DESCRIPTION: Provides an access point to perform synchronization operations
  254. * within the AML.
  255. *
  256. ******************************************************************************/
  257. acpi_status acpi_ex_system_signal_event(union acpi_operand_object *obj_desc)
  258. {
  259. acpi_status status = AE_OK;
  260. ACPI_FUNCTION_TRACE(ex_system_signal_event);
  261. if (obj_desc) {
  262. status =
  263. acpi_os_signal_semaphore(obj_desc->event.os_semaphore, 1);
  264. }
  265. return_ACPI_STATUS(status);
  266. }
  267. /*******************************************************************************
  268. *
  269. * FUNCTION: acpi_ex_system_wait_event
  270. *
  271. * PARAMETERS: time_desc - The 'time to delay' object descriptor
  272. * obj_desc - The object descriptor for this op
  273. *
  274. * RETURN: Status
  275. *
  276. * DESCRIPTION: Provides an access point to perform synchronization operations
  277. * within the AML. This operation is a request to wait for an
  278. * event.
  279. *
  280. ******************************************************************************/
  281. acpi_status
  282. acpi_ex_system_wait_event(union acpi_operand_object *time_desc,
  283. union acpi_operand_object *obj_desc)
  284. {
  285. acpi_status status = AE_OK;
  286. ACPI_FUNCTION_TRACE(ex_system_wait_event);
  287. if (obj_desc) {
  288. status =
  289. acpi_ex_system_wait_semaphore(obj_desc->event.os_semaphore,
  290. (u16) time_desc->integer.
  291. value);
  292. }
  293. return_ACPI_STATUS(status);
  294. }
  295. /*******************************************************************************
  296. *
  297. * FUNCTION: acpi_ex_system_reset_event
  298. *
  299. * PARAMETERS: obj_desc - The object descriptor for this op
  300. *
  301. * RETURN: Status
  302. *
  303. * DESCRIPTION: Reset an event to a known state.
  304. *
  305. ******************************************************************************/
  306. acpi_status acpi_ex_system_reset_event(union acpi_operand_object *obj_desc)
  307. {
  308. acpi_status status = AE_OK;
  309. acpi_semaphore temp_semaphore;
  310. ACPI_FUNCTION_ENTRY();
  311. /*
  312. * We are going to simply delete the existing semaphore and
  313. * create a new one!
  314. */
  315. status =
  316. acpi_os_create_semaphore(ACPI_NO_UNIT_LIMIT, 0, &temp_semaphore);
  317. if (ACPI_SUCCESS(status)) {
  318. (void)acpi_os_delete_semaphore(obj_desc->event.os_semaphore);
  319. obj_desc->event.os_semaphore = temp_semaphore;
  320. }
  321. return (status);
  322. }