utmisc.c 11 KB

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