utmisc.c 9.9 KB

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