exutils.c 12 KB

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