exutils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /******************************************************************************
  2. *
  3. * Module Name: exutils - interpreter/scanner utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, 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. /*
  43. * DEFINE_AML_GLOBALS is tested in amlcode.h
  44. * to determine whether certain global names should be "defined" or only
  45. * "declared" in the current compilation. This enhances maintainability
  46. * by enabling a single header file to embody all knowledge of the names
  47. * in question.
  48. *
  49. * Exactly one module of any executable should #define DEFINE_GLOBALS
  50. * before #including the header files which use this convention. The
  51. * names in question will be defined and initialized in that module,
  52. * and declared as extern in all other modules which #include those
  53. * header files.
  54. */
  55. #define DEFINE_AML_GLOBALS
  56. #include <acpi/acpi.h>
  57. #include <acpi/acinterp.h>
  58. #include <acpi/amlcode.h>
  59. #include <acpi/acevents.h>
  60. #define _COMPONENT ACPI_EXECUTER
  61. ACPI_MODULE_NAME ("exutils")
  62. #ifndef ACPI_NO_METHOD_EXECUTION
  63. /*******************************************************************************
  64. *
  65. * FUNCTION: acpi_ex_enter_interpreter
  66. *
  67. * PARAMETERS: None
  68. *
  69. * DESCRIPTION: Enter the interpreter execution region. Failure to enter
  70. * the interpreter region is a fatal system error
  71. *
  72. ******************************************************************************/
  73. acpi_status
  74. acpi_ex_enter_interpreter (void)
  75. {
  76. acpi_status status;
  77. ACPI_FUNCTION_TRACE ("ex_enter_interpreter");
  78. status = acpi_ut_acquire_mutex (ACPI_MTX_EXECUTE);
  79. if (ACPI_FAILURE (status)) {
  80. ACPI_REPORT_ERROR (("Could not acquire interpreter mutex\n"));
  81. }
  82. return_ACPI_STATUS (status);
  83. }
  84. /*******************************************************************************
  85. *
  86. * FUNCTION: acpi_ex_exit_interpreter
  87. *
  88. * PARAMETERS: None
  89. *
  90. * DESCRIPTION: Exit the interpreter execution region
  91. *
  92. * Cases where the interpreter is unlocked:
  93. * 1) Completion of the execution of a control method
  94. * 2) Method blocked on a Sleep() AML opcode
  95. * 3) Method blocked on an Acquire() AML opcode
  96. * 4) Method blocked on a Wait() AML opcode
  97. * 5) Method blocked to acquire the global lock
  98. * 6) Method blocked to execute a serialized control method that is
  99. * already executing
  100. * 7) About to invoke a user-installed opregion handler
  101. *
  102. ******************************************************************************/
  103. void
  104. acpi_ex_exit_interpreter (void)
  105. {
  106. acpi_status status;
  107. ACPI_FUNCTION_TRACE ("ex_exit_interpreter");
  108. status = acpi_ut_release_mutex (ACPI_MTX_EXECUTE);
  109. if (ACPI_FAILURE (status)) {
  110. ACPI_REPORT_ERROR (("Could not release interpreter mutex\n"));
  111. }
  112. return_VOID;
  113. }
  114. /*******************************************************************************
  115. *
  116. * FUNCTION: acpi_ex_truncate_for32bit_table
  117. *
  118. * PARAMETERS: obj_desc - Object to be truncated
  119. *
  120. * RETURN: none
  121. *
  122. * DESCRIPTION: Truncate a number to 32-bits if the currently executing method
  123. * belongs to a 32-bit ACPI table.
  124. *
  125. ******************************************************************************/
  126. void
  127. acpi_ex_truncate_for32bit_table (
  128. union acpi_operand_object *obj_desc)
  129. {
  130. ACPI_FUNCTION_ENTRY ();
  131. /*
  132. * Object must be a valid number and we must be executing
  133. * a control method
  134. */
  135. if ((!obj_desc) ||
  136. (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_INTEGER)) {
  137. return;
  138. }
  139. if (acpi_gbl_integer_byte_width == 4) {
  140. /*
  141. * We are running a method that exists in a 32-bit ACPI table.
  142. * Truncate the value to 32 bits by zeroing out the upper 32-bit field
  143. */
  144. obj_desc->integer.value &= (acpi_integer) ACPI_UINT32_MAX;
  145. }
  146. }
  147. /*******************************************************************************
  148. *
  149. * FUNCTION: acpi_ex_acquire_global_lock
  150. *
  151. * PARAMETERS: field_flags - Flags with Lock rule:
  152. * always_lock or never_lock
  153. *
  154. * RETURN: TRUE/FALSE indicating whether the lock was actually acquired
  155. *
  156. * DESCRIPTION: Obtain the global lock and keep track of this fact via two
  157. * methods. A global variable keeps the state of the lock, and
  158. * the state is returned to the caller.
  159. *
  160. ******************************************************************************/
  161. u8
  162. acpi_ex_acquire_global_lock (
  163. u32 field_flags)
  164. {
  165. u8 locked = FALSE;
  166. acpi_status status;
  167. ACPI_FUNCTION_TRACE ("ex_acquire_global_lock");
  168. /* Only attempt lock if the always_lock bit is set */
  169. if (field_flags & AML_FIELD_LOCK_RULE_MASK) {
  170. /* We should attempt to get the lock, wait forever */
  171. status = acpi_ev_acquire_global_lock (ACPI_WAIT_FOREVER);
  172. if (ACPI_SUCCESS (status)) {
  173. locked = TRUE;
  174. }
  175. else {
  176. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Could not acquire Global Lock, %s\n",
  177. acpi_format_exception (status)));
  178. }
  179. }
  180. return_VALUE (locked);
  181. }
  182. /*******************************************************************************
  183. *
  184. * FUNCTION: acpi_ex_release_global_lock
  185. *
  186. * PARAMETERS: locked_by_me - Return value from corresponding call to
  187. * acquire_global_lock.
  188. *
  189. * RETURN: Status
  190. *
  191. * DESCRIPTION: Release the global lock if it is locked.
  192. *
  193. ******************************************************************************/
  194. void
  195. acpi_ex_release_global_lock (
  196. u8 locked_by_me)
  197. {
  198. acpi_status status;
  199. ACPI_FUNCTION_TRACE ("ex_release_global_lock");
  200. /* Only attempt unlock if the caller locked it */
  201. if (locked_by_me) {
  202. /* OK, now release the lock */
  203. status = acpi_ev_release_global_lock ();
  204. if (ACPI_FAILURE (status)) {
  205. /* Report the error, but there isn't much else we can do */
  206. ACPI_REPORT_ERROR (("Could not release ACPI Global Lock, %s\n",
  207. acpi_format_exception (status)));
  208. }
  209. }
  210. return_VOID;
  211. }
  212. /*******************************************************************************
  213. *
  214. * FUNCTION: acpi_ex_digits_needed
  215. *
  216. * PARAMETERS: Value - Value to be represented
  217. * Base - Base of representation
  218. *
  219. * RETURN: the number of digits needed to represent Value in Base
  220. *
  221. ******************************************************************************/
  222. u32
  223. acpi_ex_digits_needed (
  224. acpi_integer value,
  225. u32 base)
  226. {
  227. u32 num_digits;
  228. acpi_integer current_value;
  229. ACPI_FUNCTION_TRACE ("ex_digits_needed");
  230. /* acpi_integer is unsigned, so we don't worry about a '-' prefix */
  231. if (value == 0) {
  232. return_VALUE (1);
  233. }
  234. current_value = value;
  235. num_digits = 0;
  236. /* Count the digits in the requested base */
  237. while (current_value) {
  238. (void) acpi_ut_short_divide (current_value, base, &current_value, NULL);
  239. num_digits++;
  240. }
  241. return_VALUE (num_digits);
  242. }
  243. /*******************************************************************************
  244. *
  245. * FUNCTION: acpi_ex_eisa_id_to_string
  246. *
  247. * PARAMETERS: numeric_id - EISA ID to be converted
  248. * out_string - Where to put the converted string (8 bytes)
  249. *
  250. * DESCRIPTION: Convert a numeric EISA ID to string representation
  251. *
  252. ******************************************************************************/
  253. void
  254. acpi_ex_eisa_id_to_string (
  255. u32 numeric_id,
  256. char *out_string)
  257. {
  258. u32 eisa_id;
  259. ACPI_FUNCTION_ENTRY ();
  260. /* Swap ID to big-endian to get contiguous bits */
  261. eisa_id = acpi_ut_dword_byte_swap (numeric_id);
  262. out_string[0] = (char) ('@' + (((unsigned long) eisa_id >> 26) & 0x1f));
  263. out_string[1] = (char) ('@' + ((eisa_id >> 21) & 0x1f));
  264. out_string[2] = (char) ('@' + ((eisa_id >> 16) & 0x1f));
  265. out_string[3] = acpi_ut_hex_to_ascii_char ((acpi_integer) eisa_id, 12);
  266. out_string[4] = acpi_ut_hex_to_ascii_char ((acpi_integer) eisa_id, 8);
  267. out_string[5] = acpi_ut_hex_to_ascii_char ((acpi_integer) eisa_id, 4);
  268. out_string[6] = acpi_ut_hex_to_ascii_char ((acpi_integer) eisa_id, 0);
  269. out_string[7] = 0;
  270. }
  271. /*******************************************************************************
  272. *
  273. * FUNCTION: acpi_ex_unsigned_integer_to_string
  274. *
  275. * PARAMETERS: Value - Value to be converted
  276. * out_string - Where to put the converted string (8 bytes)
  277. *
  278. * RETURN: Convert a number to string representation
  279. *
  280. ******************************************************************************/
  281. void
  282. acpi_ex_unsigned_integer_to_string (
  283. acpi_integer value,
  284. char *out_string)
  285. {
  286. u32 count;
  287. u32 digits_needed;
  288. u32 remainder;
  289. ACPI_FUNCTION_ENTRY ();
  290. digits_needed = acpi_ex_digits_needed (value, 10);
  291. out_string[digits_needed] = 0;
  292. for (count = digits_needed; count > 0; count--) {
  293. (void) acpi_ut_short_divide (value, 10, &value, &remainder);
  294. out_string[count-1] = (char) ('0' + remainder);\
  295. }
  296. }
  297. #endif