psscope.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: psscope - Parser scope stack management routines
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acparser.h"
  12. #define _COMPONENT ACPI_PARSER
  13. ACPI_MODULE_NAME("psscope")
  14. /*******************************************************************************
  15. *
  16. * FUNCTION: acpi_ps_get_parent_scope
  17. *
  18. * PARAMETERS: parser_state - Current parser state object
  19. *
  20. * RETURN: Pointer to an Op object
  21. *
  22. * DESCRIPTION: Get parent of current op being parsed
  23. *
  24. ******************************************************************************/
  25. union acpi_parse_object *acpi_ps_get_parent_scope(struct acpi_parse_state
  26. *parser_state)
  27. {
  28. return (parser_state->scope->parse_scope.op);
  29. }
  30. /*******************************************************************************
  31. *
  32. * FUNCTION: acpi_ps_has_completed_scope
  33. *
  34. * PARAMETERS: parser_state - Current parser state object
  35. *
  36. * RETURN: Boolean, TRUE = scope completed.
  37. *
  38. * DESCRIPTION: Is parsing of current argument complete? Determined by
  39. * 1) AML pointer is at or beyond the end of the scope
  40. * 2) The scope argument count has reached zero.
  41. *
  42. ******************************************************************************/
  43. u8 acpi_ps_has_completed_scope(struct acpi_parse_state * parser_state)
  44. {
  45. return ((u8)
  46. ((parser_state->aml >= parser_state->scope->parse_scope.arg_end
  47. || !parser_state->scope->parse_scope.arg_count)));
  48. }
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ps_init_scope
  52. *
  53. * PARAMETERS: parser_state - Current parser state object
  54. * root - the Root Node of this new scope
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Allocate and init a new scope object
  59. *
  60. ******************************************************************************/
  61. acpi_status
  62. acpi_ps_init_scope(struct acpi_parse_state * parser_state,
  63. union acpi_parse_object * root_op)
  64. {
  65. union acpi_generic_state *scope;
  66. ACPI_FUNCTION_TRACE_PTR(ps_init_scope, root_op);
  67. scope = acpi_ut_create_generic_state();
  68. if (!scope) {
  69. return_ACPI_STATUS(AE_NO_MEMORY);
  70. }
  71. scope->common.descriptor_type = ACPI_DESC_TYPE_STATE_RPSCOPE;
  72. scope->parse_scope.op = root_op;
  73. scope->parse_scope.arg_count = ACPI_VAR_ARGS;
  74. scope->parse_scope.arg_end = parser_state->aml_end;
  75. scope->parse_scope.pkg_end = parser_state->aml_end;
  76. parser_state->scope = scope;
  77. parser_state->start_op = root_op;
  78. return_ACPI_STATUS(AE_OK);
  79. }
  80. /*******************************************************************************
  81. *
  82. * FUNCTION: acpi_ps_push_scope
  83. *
  84. * PARAMETERS: parser_state - Current parser state object
  85. * op - Current op to be pushed
  86. * remaining_args - List of args remaining
  87. * arg_count - Fixed or variable number of args
  88. *
  89. * RETURN: Status
  90. *
  91. * DESCRIPTION: Push current op to begin parsing its argument
  92. *
  93. ******************************************************************************/
  94. acpi_status
  95. acpi_ps_push_scope(struct acpi_parse_state *parser_state,
  96. union acpi_parse_object *op,
  97. u32 remaining_args, u32 arg_count)
  98. {
  99. union acpi_generic_state *scope;
  100. ACPI_FUNCTION_TRACE_PTR(ps_push_scope, op);
  101. scope = acpi_ut_create_generic_state();
  102. if (!scope) {
  103. return_ACPI_STATUS(AE_NO_MEMORY);
  104. }
  105. scope->common.descriptor_type = ACPI_DESC_TYPE_STATE_PSCOPE;
  106. scope->parse_scope.op = op;
  107. scope->parse_scope.arg_list = remaining_args;
  108. scope->parse_scope.arg_count = arg_count;
  109. scope->parse_scope.pkg_end = parser_state->pkg_end;
  110. /* Push onto scope stack */
  111. acpi_ut_push_generic_state(&parser_state->scope, scope);
  112. if (arg_count == ACPI_VAR_ARGS) {
  113. /* Multiple arguments */
  114. scope->parse_scope.arg_end = parser_state->pkg_end;
  115. } else {
  116. /* Single argument */
  117. scope->parse_scope.arg_end = ACPI_TO_POINTER(ACPI_MAX_PTR);
  118. }
  119. return_ACPI_STATUS(AE_OK);
  120. }
  121. /*******************************************************************************
  122. *
  123. * FUNCTION: acpi_ps_pop_scope
  124. *
  125. * PARAMETERS: parser_state - Current parser state object
  126. * op - Where the popped op is returned
  127. * arg_list - Where the popped "next argument" is
  128. * returned
  129. * arg_count - Count of objects in arg_list
  130. *
  131. * RETURN: Status
  132. *
  133. * DESCRIPTION: Return to parsing a previous op
  134. *
  135. ******************************************************************************/
  136. void
  137. acpi_ps_pop_scope(struct acpi_parse_state *parser_state,
  138. union acpi_parse_object **op, u32 * arg_list, u32 * arg_count)
  139. {
  140. union acpi_generic_state *scope = parser_state->scope;
  141. ACPI_FUNCTION_TRACE(ps_pop_scope);
  142. /* Only pop the scope if there is in fact a next scope */
  143. if (scope->common.next) {
  144. scope = acpi_ut_pop_generic_state(&parser_state->scope);
  145. /* Return to parsing previous op */
  146. *op = scope->parse_scope.op;
  147. *arg_list = scope->parse_scope.arg_list;
  148. *arg_count = scope->parse_scope.arg_count;
  149. parser_state->pkg_end = scope->parse_scope.pkg_end;
  150. /* All done with this scope state structure */
  151. acpi_ut_delete_generic_state(scope);
  152. } else {
  153. /* Empty parse stack, prepare to fetch next opcode */
  154. *op = NULL;
  155. *arg_list = 0;
  156. *arg_count = 0;
  157. }
  158. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  159. "Popped Op %p Args %X\n", *op, *arg_count));
  160. return_VOID;
  161. }
  162. /*******************************************************************************
  163. *
  164. * FUNCTION: acpi_ps_cleanup_scope
  165. *
  166. * PARAMETERS: parser_state - Current parser state object
  167. *
  168. * RETURN: None
  169. *
  170. * DESCRIPTION: Destroy available list, remaining stack levels, and return
  171. * root scope
  172. *
  173. ******************************************************************************/
  174. void acpi_ps_cleanup_scope(struct acpi_parse_state *parser_state)
  175. {
  176. union acpi_generic_state *scope;
  177. ACPI_FUNCTION_TRACE_PTR(ps_cleanup_scope, parser_state);
  178. if (!parser_state) {
  179. return_VOID;
  180. }
  181. /* Delete anything on the scope stack */
  182. while (parser_state->scope) {
  183. scope = acpi_ut_pop_generic_state(&parser_state->scope);
  184. acpi_ut_delete_generic_state(scope);
  185. }
  186. return_VOID;
  187. }