utmisc.c 12 KB

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