exutils.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /******************************************************************************
  2. *
  3. * Module Name: exutils - interpreter/scanner utilities
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, 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. /*
  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. #define _COMPONENT ACPI_EXECUTER
  60. ACPI_MODULE_NAME("exutils")
  61. /* Local prototypes */
  62. static u32 acpi_ex_digits_needed(acpi_integer value, u32 base);
  63. #ifndef ACPI_NO_METHOD_EXECUTION
  64. /*******************************************************************************
  65. *
  66. * FUNCTION: acpi_ex_enter_interpreter
  67. *
  68. * PARAMETERS: None
  69. *
  70. * RETURN: None
  71. *
  72. * DESCRIPTION: Enter the interpreter execution region. Failure to enter
  73. * the interpreter region is a fatal system error. Used in
  74. * conjunction with exit_interpreter.
  75. *
  76. ******************************************************************************/
  77. void 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_INTERPRETER);
  82. if (ACPI_FAILURE(status)) {
  83. ACPI_ERROR((AE_INFO,
  84. "Could not acquire AML Interpreter mutex"));
  85. }
  86. return_VOID;
  87. }
  88. /*******************************************************************************
  89. *
  90. * FUNCTION: acpi_ex_reacquire_interpreter
  91. *
  92. * PARAMETERS: None
  93. *
  94. * RETURN: None
  95. *
  96. * DESCRIPTION: Reacquire the interpreter execution region from within the
  97. * interpreter code. Failure to enter the interpreter region is a
  98. * fatal system error. Used in conjuction with
  99. * relinquish_interpreter
  100. *
  101. ******************************************************************************/
  102. void acpi_ex_reacquire_interpreter(void)
  103. {
  104. ACPI_FUNCTION_TRACE(ex_reacquire_interpreter);
  105. /*
  106. * If the global serialized flag is set, do not release the interpreter,
  107. * since it was not actually released by acpi_ex_relinquish_interpreter.
  108. * This forces the interpreter to be single threaded.
  109. */
  110. if (!acpi_gbl_all_methods_serialized) {
  111. acpi_ex_enter_interpreter();
  112. }
  113. return_VOID;
  114. }
  115. /*******************************************************************************
  116. *
  117. * FUNCTION: acpi_ex_exit_interpreter
  118. *
  119. * PARAMETERS: None
  120. *
  121. * RETURN: None
  122. *
  123. * DESCRIPTION: Exit the interpreter execution region. This is the top level
  124. * routine used to exit the interpreter when all processing has
  125. * been completed.
  126. *
  127. ******************************************************************************/
  128. void acpi_ex_exit_interpreter(void)
  129. {
  130. acpi_status status;
  131. ACPI_FUNCTION_TRACE(ex_exit_interpreter);
  132. status = acpi_ut_release_mutex(ACPI_MTX_INTERPRETER);
  133. if (ACPI_FAILURE(status)) {
  134. ACPI_ERROR((AE_INFO,
  135. "Could not release AML Interpreter mutex"));
  136. }
  137. return_VOID;
  138. }
  139. /*******************************************************************************
  140. *
  141. * FUNCTION: acpi_ex_relinquish_interpreter
  142. *
  143. * PARAMETERS: None
  144. *
  145. * RETURN: None
  146. *
  147. * DESCRIPTION: Exit the interpreter execution region, from within the
  148. * interpreter - before attempting an operation that will possibly
  149. * block the running thread.
  150. *
  151. * Cases where the interpreter is unlocked internally
  152. * 1) Method to be blocked on a Sleep() AML opcode
  153. * 2) Method to be blocked on an Acquire() AML opcode
  154. * 3) Method to be blocked on a Wait() AML opcode
  155. * 4) Method to be blocked to acquire the global lock
  156. * 5) Method to be blocked waiting to execute a serialized control method
  157. * that is currently executing
  158. * 6) About to invoke a user-installed opregion handler
  159. *
  160. ******************************************************************************/
  161. void acpi_ex_relinquish_interpreter(void)
  162. {
  163. ACPI_FUNCTION_TRACE(ex_relinquish_interpreter);
  164. /*
  165. * If the global serialized flag is set, do not release the interpreter.
  166. * This forces the interpreter to be single threaded.
  167. */
  168. if (!acpi_gbl_all_methods_serialized) {
  169. acpi_ex_exit_interpreter();
  170. }
  171. return_VOID;
  172. }
  173. /*******************************************************************************
  174. *
  175. * FUNCTION: acpi_ex_truncate_for32bit_table
  176. *
  177. * PARAMETERS: obj_desc - Object to be truncated
  178. *
  179. * RETURN: none
  180. *
  181. * DESCRIPTION: Truncate an ACPI Integer to 32 bits if the execution mode is
  182. * 32-bit, as determined by the revision of the DSDT.
  183. *
  184. ******************************************************************************/
  185. void acpi_ex_truncate_for32bit_table(union acpi_operand_object *obj_desc)
  186. {
  187. ACPI_FUNCTION_ENTRY();
  188. /*
  189. * Object must be a valid number and we must be executing
  190. * a control method. NS node could be there for AML_INT_NAMEPATH_OP.
  191. */
  192. if ((!obj_desc) ||
  193. (ACPI_GET_DESCRIPTOR_TYPE(obj_desc) != ACPI_DESC_TYPE_OPERAND) ||
  194. (ACPI_GET_OBJECT_TYPE(obj_desc) != ACPI_TYPE_INTEGER)) {
  195. return;
  196. }
  197. if (acpi_gbl_integer_byte_width == 4) {
  198. /*
  199. * We are running a method that exists in a 32-bit ACPI table.
  200. * Truncate the value to 32 bits by zeroing out the upper 32-bit field
  201. */
  202. obj_desc->integer.value &= (acpi_integer) ACPI_UINT32_MAX;
  203. }
  204. }
  205. /*******************************************************************************
  206. *
  207. * FUNCTION: acpi_ex_acquire_global_lock
  208. *
  209. * PARAMETERS: field_flags - Flags with Lock rule:
  210. * always_lock or never_lock
  211. *
  212. * RETURN: None
  213. *
  214. * DESCRIPTION: Obtain the ACPI hardware Global Lock, only if the field
  215. * flags specifiy that it is to be obtained before field access.
  216. *
  217. ******************************************************************************/
  218. void acpi_ex_acquire_global_lock(u32 field_flags)
  219. {
  220. acpi_status status;
  221. ACPI_FUNCTION_TRACE(ex_acquire_global_lock);
  222. /* Only use the lock if the always_lock bit is set */
  223. if (!(field_flags & AML_FIELD_LOCK_RULE_MASK)) {
  224. return_VOID;
  225. }
  226. /* Attempt to get the global lock, wait forever */
  227. status = acpi_ex_acquire_mutex_object(ACPI_WAIT_FOREVER,
  228. acpi_gbl_global_lock_mutex,
  229. acpi_os_get_thread_id());
  230. if (ACPI_FAILURE(status)) {
  231. ACPI_EXCEPTION((AE_INFO, status,
  232. "Could not acquire Global Lock"));
  233. }
  234. return_VOID;
  235. }
  236. /*******************************************************************************
  237. *
  238. * FUNCTION: acpi_ex_release_global_lock
  239. *
  240. * PARAMETERS: field_flags - Flags with Lock rule:
  241. * always_lock or never_lock
  242. *
  243. * RETURN: None
  244. *
  245. * DESCRIPTION: Release the ACPI hardware Global Lock
  246. *
  247. ******************************************************************************/
  248. void acpi_ex_release_global_lock(u32 field_flags)
  249. {
  250. acpi_status status;
  251. ACPI_FUNCTION_TRACE(ex_release_global_lock);
  252. /* Only use the lock if the always_lock bit is set */
  253. if (!(field_flags & AML_FIELD_LOCK_RULE_MASK)) {
  254. return_VOID;
  255. }
  256. /* Release the global lock */
  257. status = acpi_ex_release_mutex_object(acpi_gbl_global_lock_mutex);
  258. if (ACPI_FAILURE(status)) {
  259. /* Report the error, but there isn't much else we can do */
  260. ACPI_EXCEPTION((AE_INFO, status,
  261. "Could not release Global Lock"));
  262. }
  263. return_VOID;
  264. }
  265. /*******************************************************************************
  266. *
  267. * FUNCTION: acpi_ex_digits_needed
  268. *
  269. * PARAMETERS: Value - Value to be represented
  270. * Base - Base of representation
  271. *
  272. * RETURN: The number of digits.
  273. *
  274. * DESCRIPTION: Calculate the number of digits needed to represent the Value
  275. * in the given Base (Radix)
  276. *
  277. ******************************************************************************/
  278. static u32 acpi_ex_digits_needed(acpi_integer value, u32 base)
  279. {
  280. u32 num_digits;
  281. acpi_integer current_value;
  282. ACPI_FUNCTION_TRACE(ex_digits_needed);
  283. /* acpi_integer is unsigned, so we don't worry about a '-' prefix */
  284. if (value == 0) {
  285. return_UINT32(1);
  286. }
  287. current_value = value;
  288. num_digits = 0;
  289. /* Count the digits in the requested base */
  290. while (current_value) {
  291. (void)acpi_ut_short_divide(current_value, base, &current_value,
  292. NULL);
  293. num_digits++;
  294. }
  295. return_UINT32(num_digits);
  296. }
  297. /*******************************************************************************
  298. *
  299. * FUNCTION: acpi_ex_eisa_id_to_string
  300. *
  301. * PARAMETERS: numeric_id - EISA ID to be converted
  302. * out_string - Where to put the converted string (8 bytes)
  303. *
  304. * RETURN: None
  305. *
  306. * DESCRIPTION: Convert a numeric EISA ID to string representation
  307. *
  308. ******************************************************************************/
  309. void acpi_ex_eisa_id_to_string(u32 numeric_id, char *out_string)
  310. {
  311. u32 eisa_id;
  312. ACPI_FUNCTION_ENTRY();
  313. /* Swap ID to big-endian to get contiguous bits */
  314. eisa_id = acpi_ut_dword_byte_swap(numeric_id);
  315. out_string[0] = (char)('@' + (((unsigned long)eisa_id >> 26) & 0x1f));
  316. out_string[1] = (char)('@' + ((eisa_id >> 21) & 0x1f));
  317. out_string[2] = (char)('@' + ((eisa_id >> 16) & 0x1f));
  318. out_string[3] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 12);
  319. out_string[4] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 8);
  320. out_string[5] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 4);
  321. out_string[6] = acpi_ut_hex_to_ascii_char((acpi_integer) eisa_id, 0);
  322. out_string[7] = 0;
  323. }
  324. /*******************************************************************************
  325. *
  326. * FUNCTION: acpi_ex_unsigned_integer_to_string
  327. *
  328. * PARAMETERS: Value - Value to be converted
  329. * out_string - Where to put the converted string (8 bytes)
  330. *
  331. * RETURN: None, string
  332. *
  333. * DESCRIPTION: Convert a number to string representation. Assumes string
  334. * buffer is large enough to hold the string.
  335. *
  336. ******************************************************************************/
  337. void acpi_ex_unsigned_integer_to_string(acpi_integer value, char *out_string)
  338. {
  339. u32 count;
  340. u32 digits_needed;
  341. u32 remainder;
  342. ACPI_FUNCTION_ENTRY();
  343. digits_needed = acpi_ex_digits_needed(value, 10);
  344. out_string[digits_needed] = 0;
  345. for (count = digits_needed; count > 0; count--) {
  346. (void)acpi_ut_short_divide(value, 10, &value, &remainder);
  347. out_string[count - 1] = (char)('0' + remainder);
  348. }
  349. }
  350. #endif