exutils.c 12 KB

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