utstate.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: utstate - state object support procedures
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #define _COMPONENT ACPI_UTILITIES
  10. ACPI_MODULE_NAME("utstate")
  11. /*******************************************************************************
  12. *
  13. * FUNCTION: acpi_ut_push_generic_state
  14. *
  15. * PARAMETERS: list_head - Head of the state stack
  16. * state - State object to push
  17. *
  18. * RETURN: None
  19. *
  20. * DESCRIPTION: Push a state object onto a state stack
  21. *
  22. ******************************************************************************/
  23. void
  24. acpi_ut_push_generic_state(union acpi_generic_state **list_head,
  25. union acpi_generic_state *state)
  26. {
  27. ACPI_FUNCTION_ENTRY();
  28. /* Push the state object onto the front of the list (stack) */
  29. state->common.next = *list_head;
  30. *list_head = state;
  31. return;
  32. }
  33. /*******************************************************************************
  34. *
  35. * FUNCTION: acpi_ut_pop_generic_state
  36. *
  37. * PARAMETERS: list_head - Head of the state stack
  38. *
  39. * RETURN: The popped state object
  40. *
  41. * DESCRIPTION: Pop a state object from a state stack
  42. *
  43. ******************************************************************************/
  44. union acpi_generic_state *acpi_ut_pop_generic_state(union acpi_generic_state
  45. **list_head)
  46. {
  47. union acpi_generic_state *state;
  48. ACPI_FUNCTION_ENTRY();
  49. /* Remove the state object at the head of the list (stack) */
  50. state = *list_head;
  51. if (state) {
  52. /* Update the list head */
  53. *list_head = state->common.next;
  54. }
  55. return (state);
  56. }
  57. /*******************************************************************************
  58. *
  59. * FUNCTION: acpi_ut_create_generic_state
  60. *
  61. * PARAMETERS: None
  62. *
  63. * RETURN: The new state object. NULL on failure.
  64. *
  65. * DESCRIPTION: Create a generic state object. Attempt to obtain one from
  66. * the global state cache; If none available, create a new one.
  67. *
  68. ******************************************************************************/
  69. union acpi_generic_state *acpi_ut_create_generic_state(void)
  70. {
  71. union acpi_generic_state *state;
  72. ACPI_FUNCTION_ENTRY();
  73. state = acpi_os_acquire_object(acpi_gbl_state_cache);
  74. if (state) {
  75. /* Initialize */
  76. state->common.descriptor_type = ACPI_DESC_TYPE_STATE;
  77. }
  78. return (state);
  79. }
  80. /*******************************************************************************
  81. *
  82. * FUNCTION: acpi_ut_create_thread_state
  83. *
  84. * PARAMETERS: None
  85. *
  86. * RETURN: New Thread State. NULL on failure
  87. *
  88. * DESCRIPTION: Create a "Thread State" - a flavor of the generic state used
  89. * to track per-thread info during method execution
  90. *
  91. ******************************************************************************/
  92. struct acpi_thread_state *acpi_ut_create_thread_state(void)
  93. {
  94. union acpi_generic_state *state;
  95. ACPI_FUNCTION_ENTRY();
  96. /* Create the generic state object */
  97. state = acpi_ut_create_generic_state();
  98. if (!state) {
  99. return (NULL);
  100. }
  101. /* Init fields specific to the update struct */
  102. state->common.descriptor_type = ACPI_DESC_TYPE_STATE_THREAD;
  103. state->thread.thread_id = acpi_os_get_thread_id();
  104. /* Check for invalid thread ID - zero is very bad, it will break things */
  105. if (!state->thread.thread_id) {
  106. ACPI_ERROR((AE_INFO, "Invalid zero ID from AcpiOsGetThreadId"));
  107. state->thread.thread_id = (acpi_thread_id) 1;
  108. }
  109. return ((struct acpi_thread_state *)state);
  110. }
  111. /*******************************************************************************
  112. *
  113. * FUNCTION: acpi_ut_create_update_state
  114. *
  115. * PARAMETERS: object - Initial Object to be installed in the state
  116. * action - Update action to be performed
  117. *
  118. * RETURN: New state object, null on failure
  119. *
  120. * DESCRIPTION: Create an "Update State" - a flavor of the generic state used
  121. * to update reference counts and delete complex objects such
  122. * as packages.
  123. *
  124. ******************************************************************************/
  125. union acpi_generic_state *acpi_ut_create_update_state(union acpi_operand_object
  126. *object, u16 action)
  127. {
  128. union acpi_generic_state *state;
  129. ACPI_FUNCTION_ENTRY();
  130. /* Create the generic state object */
  131. state = acpi_ut_create_generic_state();
  132. if (!state) {
  133. return (NULL);
  134. }
  135. /* Init fields specific to the update struct */
  136. state->common.descriptor_type = ACPI_DESC_TYPE_STATE_UPDATE;
  137. state->update.object = object;
  138. state->update.value = action;
  139. return (state);
  140. }
  141. /*******************************************************************************
  142. *
  143. * FUNCTION: acpi_ut_create_pkg_state
  144. *
  145. * PARAMETERS: object - Initial Object to be installed in the state
  146. * action - Update action to be performed
  147. *
  148. * RETURN: New state object, null on failure
  149. *
  150. * DESCRIPTION: Create a "Package State"
  151. *
  152. ******************************************************************************/
  153. union acpi_generic_state *acpi_ut_create_pkg_state(void *internal_object,
  154. void *external_object,
  155. u32 index)
  156. {
  157. union acpi_generic_state *state;
  158. ACPI_FUNCTION_ENTRY();
  159. /* Create the generic state object */
  160. state = acpi_ut_create_generic_state();
  161. if (!state) {
  162. return (NULL);
  163. }
  164. /* Init fields specific to the update struct */
  165. state->common.descriptor_type = ACPI_DESC_TYPE_STATE_PACKAGE;
  166. state->pkg.source_object = (union acpi_operand_object *)internal_object;
  167. state->pkg.dest_object = external_object;
  168. state->pkg.index = index;
  169. state->pkg.num_packages = 1;
  170. return (state);
  171. }
  172. /*******************************************************************************
  173. *
  174. * FUNCTION: acpi_ut_create_control_state
  175. *
  176. * PARAMETERS: None
  177. *
  178. * RETURN: New state object, null on failure
  179. *
  180. * DESCRIPTION: Create a "Control State" - a flavor of the generic state used
  181. * to support nested IF/WHILE constructs in the AML.
  182. *
  183. ******************************************************************************/
  184. union acpi_generic_state *acpi_ut_create_control_state(void)
  185. {
  186. union acpi_generic_state *state;
  187. ACPI_FUNCTION_ENTRY();
  188. /* Create the generic state object */
  189. state = acpi_ut_create_generic_state();
  190. if (!state) {
  191. return (NULL);
  192. }
  193. /* Init fields specific to the control struct */
  194. state->common.descriptor_type = ACPI_DESC_TYPE_STATE_CONTROL;
  195. state->common.state = ACPI_CONTROL_CONDITIONAL_EXECUTING;
  196. return (state);
  197. }
  198. /*******************************************************************************
  199. *
  200. * FUNCTION: acpi_ut_delete_generic_state
  201. *
  202. * PARAMETERS: state - The state object to be deleted
  203. *
  204. * RETURN: None
  205. *
  206. * DESCRIPTION: Release a state object to the state cache. NULL state objects
  207. * are ignored.
  208. *
  209. ******************************************************************************/
  210. void acpi_ut_delete_generic_state(union acpi_generic_state *state)
  211. {
  212. ACPI_FUNCTION_ENTRY();
  213. /* Ignore null state */
  214. if (state) {
  215. (void)acpi_os_release_object(acpi_gbl_state_cache, state);
  216. }
  217. return;
  218. }