exutils.c 11 KB

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