exutils.c 11 KB

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