utmisc.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utmisc - common utility procedures
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acnamesp.h"
  10. #define _COMPONENT ACPI_UTILITIES
  11. ACPI_MODULE_NAME("utmisc")
  12. /*******************************************************************************
  13. *
  14. * FUNCTION: acpi_ut_is_pci_root_bridge
  15. *
  16. * PARAMETERS: id - The HID/CID in string format
  17. *
  18. * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
  19. *
  20. * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
  21. *
  22. ******************************************************************************/
  23. u8 acpi_ut_is_pci_root_bridge(char *id)
  24. {
  25. /*
  26. * Check if this is a PCI root bridge.
  27. * ACPI 3.0+: check for a PCI Express root also.
  28. */
  29. if (!(strcmp(id,
  30. PCI_ROOT_HID_STRING)) ||
  31. !(strcmp(id, PCI_EXPRESS_ROOT_HID_STRING))) {
  32. return (TRUE);
  33. }
  34. return (FALSE);
  35. }
  36. #if (defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP || defined ACPI_NAMES_APP)
  37. /*******************************************************************************
  38. *
  39. * FUNCTION: acpi_ut_is_aml_table
  40. *
  41. * PARAMETERS: table - An ACPI table
  42. *
  43. * RETURN: TRUE if table contains executable AML; FALSE otherwise
  44. *
  45. * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
  46. * Currently, these are DSDT,SSDT,PSDT. All other table types are
  47. * data tables that do not contain AML code.
  48. *
  49. ******************************************************************************/
  50. u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
  51. {
  52. /* These are the only tables that contain executable AML */
  53. if (ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_DSDT) ||
  54. ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_PSDT) ||
  55. ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_SSDT) ||
  56. ACPI_COMPARE_NAMESEG(table->signature, ACPI_SIG_OSDT) ||
  57. ACPI_IS_OEM_SIG(table->signature)) {
  58. return (TRUE);
  59. }
  60. return (FALSE);
  61. }
  62. #endif
  63. /*******************************************************************************
  64. *
  65. * FUNCTION: acpi_ut_dword_byte_swap
  66. *
  67. * PARAMETERS: value - Value to be converted
  68. *
  69. * RETURN: u32 integer with bytes swapped
  70. *
  71. * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
  72. *
  73. ******************************************************************************/
  74. u32 acpi_ut_dword_byte_swap(u32 value)
  75. {
  76. union {
  77. u32 value;
  78. u8 bytes[4];
  79. } out;
  80. union {
  81. u32 value;
  82. u8 bytes[4];
  83. } in;
  84. ACPI_FUNCTION_ENTRY();
  85. in.value = value;
  86. out.bytes[0] = in.bytes[3];
  87. out.bytes[1] = in.bytes[2];
  88. out.bytes[2] = in.bytes[1];
  89. out.bytes[3] = in.bytes[0];
  90. return (out.value);
  91. }
  92. /*******************************************************************************
  93. *
  94. * FUNCTION: acpi_ut_set_integer_width
  95. *
  96. * PARAMETERS: Revision From DSDT header
  97. *
  98. * RETURN: None
  99. *
  100. * DESCRIPTION: Set the global integer bit width based upon the revision
  101. * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
  102. * For Revision 2 and above, Integers are 64 bits. Yes, this
  103. * makes a difference.
  104. *
  105. ******************************************************************************/
  106. void acpi_ut_set_integer_width(u8 revision)
  107. {
  108. if (revision < 2) {
  109. /* 32-bit case */
  110. acpi_gbl_integer_bit_width = 32;
  111. acpi_gbl_integer_nybble_width = 8;
  112. acpi_gbl_integer_byte_width = 4;
  113. } else {
  114. /* 64-bit case (ACPI 2.0+) */
  115. acpi_gbl_integer_bit_width = 64;
  116. acpi_gbl_integer_nybble_width = 16;
  117. acpi_gbl_integer_byte_width = 8;
  118. }
  119. }
  120. /*******************************************************************************
  121. *
  122. * FUNCTION: acpi_ut_create_update_state_and_push
  123. *
  124. * PARAMETERS: object - Object to be added to the new state
  125. * action - Increment/Decrement
  126. * state_list - List the state will be added to
  127. *
  128. * RETURN: Status
  129. *
  130. * DESCRIPTION: Create a new state and push it
  131. *
  132. ******************************************************************************/
  133. acpi_status
  134. acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
  135. u16 action,
  136. union acpi_generic_state **state_list)
  137. {
  138. union acpi_generic_state *state;
  139. ACPI_FUNCTION_ENTRY();
  140. /* Ignore null objects; these are expected */
  141. if (!object) {
  142. return (AE_OK);
  143. }
  144. state = acpi_ut_create_update_state(object, action);
  145. if (!state) {
  146. return (AE_NO_MEMORY);
  147. }
  148. acpi_ut_push_generic_state(state_list, state);
  149. return (AE_OK);
  150. }
  151. /*******************************************************************************
  152. *
  153. * FUNCTION: acpi_ut_walk_package_tree
  154. *
  155. * PARAMETERS: source_object - The package to walk
  156. * target_object - Target object (if package is being copied)
  157. * walk_callback - Called once for each package element
  158. * context - Passed to the callback function
  159. *
  160. * RETURN: Status
  161. *
  162. * DESCRIPTION: Walk through a package, including subpackages
  163. *
  164. ******************************************************************************/
  165. acpi_status
  166. acpi_ut_walk_package_tree(union acpi_operand_object *source_object,
  167. void *target_object,
  168. acpi_pkg_callback walk_callback, void *context)
  169. {
  170. acpi_status status = AE_OK;
  171. union acpi_generic_state *state_list = NULL;
  172. union acpi_generic_state *state;
  173. union acpi_operand_object *this_source_obj;
  174. u32 this_index;
  175. ACPI_FUNCTION_TRACE(ut_walk_package_tree);
  176. state = acpi_ut_create_pkg_state(source_object, target_object, 0);
  177. if (!state) {
  178. return_ACPI_STATUS(AE_NO_MEMORY);
  179. }
  180. while (state) {
  181. /* Get one element of the package */
  182. this_index = state->pkg.index;
  183. this_source_obj =
  184. state->pkg.source_object->package.elements[this_index];
  185. state->pkg.this_target_obj =
  186. &state->pkg.source_object->package.elements[this_index];
  187. /*
  188. * Check for:
  189. * 1) An uninitialized package element. It is completely
  190. * legal to declare a package and leave it uninitialized
  191. * 2) Not an internal object - can be a namespace node instead
  192. * 3) Any type other than a package. Packages are handled in else
  193. * case below.
  194. */
  195. if ((!this_source_obj) ||
  196. (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
  197. ACPI_DESC_TYPE_OPERAND) ||
  198. (this_source_obj->common.type != ACPI_TYPE_PACKAGE)) {
  199. status =
  200. walk_callback(ACPI_COPY_TYPE_SIMPLE,
  201. this_source_obj, state, context);
  202. if (ACPI_FAILURE(status)) {
  203. return_ACPI_STATUS(status);
  204. }
  205. state->pkg.index++;
  206. while (state->pkg.index >=
  207. state->pkg.source_object->package.count) {
  208. /*
  209. * We've handled all of the objects at this level, This means
  210. * that we have just completed a package. That package may
  211. * have contained one or more packages itself.
  212. *
  213. * Delete this state and pop the previous state (package).
  214. */
  215. acpi_ut_delete_generic_state(state);
  216. state = acpi_ut_pop_generic_state(&state_list);
  217. /* Finished when there are no more states */
  218. if (!state) {
  219. /*
  220. * We have handled all of the objects in the top level
  221. * package just add the length of the package objects
  222. * and exit
  223. */
  224. return_ACPI_STATUS(AE_OK);
  225. }
  226. /*
  227. * Go back up a level and move the index past the just
  228. * completed package object.
  229. */
  230. state->pkg.index++;
  231. }
  232. } else {
  233. /* This is a subobject of type package */
  234. status =
  235. walk_callback(ACPI_COPY_TYPE_PACKAGE,
  236. this_source_obj, state, context);
  237. if (ACPI_FAILURE(status)) {
  238. return_ACPI_STATUS(status);
  239. }
  240. /*
  241. * Push the current state and create a new one
  242. * The callback above returned a new target package object.
  243. */
  244. acpi_ut_push_generic_state(&state_list, state);
  245. state =
  246. acpi_ut_create_pkg_state(this_source_obj,
  247. state->pkg.this_target_obj,
  248. 0);
  249. if (!state) {
  250. /* Free any stacked Update State objects */
  251. while (state_list) {
  252. state =
  253. acpi_ut_pop_generic_state
  254. (&state_list);
  255. acpi_ut_delete_generic_state(state);
  256. }
  257. return_ACPI_STATUS(AE_NO_MEMORY);
  258. }
  259. }
  260. }
  261. /* We should never get here */
  262. ACPI_ERROR((AE_INFO, "State list did not terminate correctly"));
  263. return_ACPI_STATUS(AE_AML_INTERNAL);
  264. }
  265. #ifdef ACPI_DEBUG_OUTPUT
  266. /*******************************************************************************
  267. *
  268. * FUNCTION: acpi_ut_display_init_pathname
  269. *
  270. * PARAMETERS: type - Object type of the node
  271. * obj_handle - Handle whose pathname will be displayed
  272. * path - Additional path string to be appended.
  273. * (NULL if no extra path)
  274. *
  275. * RETURN: acpi_status
  276. *
  277. * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
  278. *
  279. ******************************************************************************/
  280. void
  281. acpi_ut_display_init_pathname(u8 type,
  282. struct acpi_namespace_node *obj_handle,
  283. const char *path)
  284. {
  285. acpi_status status;
  286. struct acpi_buffer buffer;
  287. ACPI_FUNCTION_ENTRY();
  288. /* Only print the path if the appropriate debug level is enabled */
  289. if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
  290. return;
  291. }
  292. /* Get the full pathname to the node */
  293. buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
  294. status = acpi_ns_handle_to_pathname(obj_handle, &buffer, TRUE);
  295. if (ACPI_FAILURE(status)) {
  296. return;
  297. }
  298. /* Print what we're doing */
  299. switch (type) {
  300. case ACPI_TYPE_METHOD:
  301. acpi_os_printf("Executing ");
  302. break;
  303. default:
  304. acpi_os_printf("Initializing ");
  305. break;
  306. }
  307. /* Print the object type and pathname */
  308. acpi_os_printf("%-12s %s",
  309. acpi_ut_get_type_name(type), (char *)buffer.pointer);
  310. /* Extra path is used to append names like _STA, _INI, etc. */
  311. if (path) {
  312. acpi_os_printf(".%s", path);
  313. }
  314. acpi_os_printf("\n");
  315. ACPI_FREE(buffer.pointer);
  316. }
  317. #endif