psutils.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: psutils - Parser miscellaneous utilities (Parser only)
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acparser.h"
  12. #include "amlcode.h"
  13. #include "acconvert.h"
  14. #define _COMPONENT ACPI_PARSER
  15. ACPI_MODULE_NAME("psutils")
  16. /*******************************************************************************
  17. *
  18. * FUNCTION: acpi_ps_create_scope_op
  19. *
  20. * PARAMETERS: None
  21. *
  22. * RETURN: A new Scope object, null on failure
  23. *
  24. * DESCRIPTION: Create a Scope and associated namepath op with the root name
  25. *
  26. ******************************************************************************/
  27. union acpi_parse_object *acpi_ps_create_scope_op(u8 *aml)
  28. {
  29. union acpi_parse_object *scope_op;
  30. scope_op = acpi_ps_alloc_op(AML_SCOPE_OP, aml);
  31. if (!scope_op) {
  32. return (NULL);
  33. }
  34. scope_op->named.name = ACPI_ROOT_NAME;
  35. return (scope_op);
  36. }
  37. /*******************************************************************************
  38. *
  39. * FUNCTION: acpi_ps_init_op
  40. *
  41. * PARAMETERS: op - A newly allocated Op object
  42. * opcode - Opcode to store in the Op
  43. *
  44. * RETURN: None
  45. *
  46. * DESCRIPTION: Initialize a parse (Op) object
  47. *
  48. ******************************************************************************/
  49. void acpi_ps_init_op(union acpi_parse_object *op, u16 opcode)
  50. {
  51. ACPI_FUNCTION_ENTRY();
  52. op->common.descriptor_type = ACPI_DESC_TYPE_PARSER;
  53. op->common.aml_opcode = opcode;
  54. ACPI_DISASM_ONLY_MEMBERS(acpi_ut_safe_strncpy(op->common.aml_op_name,
  55. (acpi_ps_get_opcode_info
  56. (opcode))->name,
  57. sizeof(op->common.
  58. aml_op_name)));
  59. }
  60. /*******************************************************************************
  61. *
  62. * FUNCTION: acpi_ps_alloc_op
  63. *
  64. * PARAMETERS: opcode - Opcode that will be stored in the new Op
  65. * aml - Address of the opcode
  66. *
  67. * RETURN: Pointer to the new Op, null on failure
  68. *
  69. * DESCRIPTION: Allocate an acpi_op, choose op type (and thus size) based on
  70. * opcode. A cache of opcodes is available for the pure
  71. * GENERIC_OP, since this is by far the most commonly used.
  72. *
  73. ******************************************************************************/
  74. union acpi_parse_object *acpi_ps_alloc_op(u16 opcode, u8 *aml)
  75. {
  76. union acpi_parse_object *op;
  77. const struct acpi_opcode_info *op_info;
  78. u8 flags = ACPI_PARSEOP_GENERIC;
  79. ACPI_FUNCTION_ENTRY();
  80. op_info = acpi_ps_get_opcode_info(opcode);
  81. /* Determine type of parse_op required */
  82. if (op_info->flags & AML_DEFER) {
  83. flags = ACPI_PARSEOP_DEFERRED;
  84. } else if (op_info->flags & AML_NAMED) {
  85. flags = ACPI_PARSEOP_NAMED_OBJECT;
  86. } else if (opcode == AML_INT_BYTELIST_OP) {
  87. flags = ACPI_PARSEOP_BYTELIST;
  88. }
  89. /* Allocate the minimum required size object */
  90. if (flags == ACPI_PARSEOP_GENERIC) {
  91. /* The generic op (default) is by far the most common (16 to 1) */
  92. op = acpi_os_acquire_object(acpi_gbl_ps_node_cache);
  93. } else {
  94. /* Extended parseop */
  95. op = acpi_os_acquire_object(acpi_gbl_ps_node_ext_cache);
  96. }
  97. /* Initialize the Op */
  98. if (op) {
  99. acpi_ps_init_op(op, opcode);
  100. op->common.aml = aml;
  101. op->common.flags = flags;
  102. ASL_CV_CLEAR_OP_COMMENTS(op);
  103. if (opcode == AML_SCOPE_OP) {
  104. acpi_gbl_current_scope = op;
  105. }
  106. if (acpi_gbl_capture_comments) {
  107. ASL_CV_TRANSFER_COMMENTS(op);
  108. }
  109. }
  110. return (op);
  111. }
  112. /*******************************************************************************
  113. *
  114. * FUNCTION: acpi_ps_free_op
  115. *
  116. * PARAMETERS: op - Op to be freed
  117. *
  118. * RETURN: None.
  119. *
  120. * DESCRIPTION: Free an Op object. Either put it on the GENERIC_OP cache list
  121. * or actually free it.
  122. *
  123. ******************************************************************************/
  124. void acpi_ps_free_op(union acpi_parse_object *op)
  125. {
  126. ACPI_FUNCTION_NAME(ps_free_op);
  127. ASL_CV_CLEAR_OP_COMMENTS(op);
  128. if (op->common.aml_opcode == AML_INT_RETURN_VALUE_OP) {
  129. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  130. "Free retval op: %p\n", op));
  131. }
  132. if (op->common.flags & ACPI_PARSEOP_GENERIC) {
  133. (void)acpi_os_release_object(acpi_gbl_ps_node_cache, op);
  134. } else {
  135. (void)acpi_os_release_object(acpi_gbl_ps_node_ext_cache, op);
  136. }
  137. }
  138. /*******************************************************************************
  139. *
  140. * FUNCTION: Utility functions
  141. *
  142. * DESCRIPTION: Low level character and object functions
  143. *
  144. ******************************************************************************/
  145. /*
  146. * Is "c" a namestring lead character?
  147. */
  148. u8 acpi_ps_is_leading_char(u32 c)
  149. {
  150. return ((u8) (c == '_' || (c >= 'A' && c <= 'Z')));
  151. }
  152. /*
  153. * Get op's name (4-byte name segment) or 0 if unnamed
  154. */
  155. u32 acpi_ps_get_name(union acpi_parse_object * op)
  156. {
  157. /* The "generic" object has no name associated with it */
  158. if (op->common.flags & ACPI_PARSEOP_GENERIC) {
  159. return (0);
  160. }
  161. /* Only the "Extended" parse objects have a name */
  162. return (op->named.name);
  163. }
  164. /*
  165. * Set op's name
  166. */
  167. void acpi_ps_set_name(union acpi_parse_object *op, u32 name)
  168. {
  169. /* The "generic" object has no name associated with it */
  170. if (op->common.flags & ACPI_PARSEOP_GENERIC) {
  171. return;
  172. }
  173. op->named.name = name;
  174. }