exutils.c 14 KB

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