exfield.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exfield - AML execution - field_unit read/write
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acdispat.h"
  12. #include "acinterp.h"
  13. #include "amlcode.h"
  14. #define _COMPONENT ACPI_EXECUTER
  15. ACPI_MODULE_NAME("exfield")
  16. /*
  17. * This table maps the various Attrib protocols to the byte transfer
  18. * length. Used for the generic serial bus.
  19. */
  20. #define ACPI_INVALID_PROTOCOL_ID 0x80
  21. #define ACPI_MAX_PROTOCOL_ID 0x0F
  22. const u8 acpi_protocol_lengths[] = {
  23. ACPI_INVALID_PROTOCOL_ID, /* 0 - reserved */
  24. ACPI_INVALID_PROTOCOL_ID, /* 1 - reserved */
  25. 0x00, /* 2 - ATTRIB_QUICK */
  26. ACPI_INVALID_PROTOCOL_ID, /* 3 - reserved */
  27. 0x01, /* 4 - ATTRIB_SEND_RECEIVE */
  28. ACPI_INVALID_PROTOCOL_ID, /* 5 - reserved */
  29. 0x01, /* 6 - ATTRIB_BYTE */
  30. ACPI_INVALID_PROTOCOL_ID, /* 7 - reserved */
  31. 0x02, /* 8 - ATTRIB_WORD */
  32. ACPI_INVALID_PROTOCOL_ID, /* 9 - reserved */
  33. 0xFF, /* A - ATTRIB_BLOCK */
  34. 0xFF, /* B - ATTRIB_BYTES */
  35. 0x02, /* C - ATTRIB_PROCESS_CALL */
  36. 0xFF, /* D - ATTRIB_BLOCK_PROCESS_CALL */
  37. 0xFF, /* E - ATTRIB_RAW_BYTES */
  38. 0xFF /* F - ATTRIB_RAW_PROCESS_BYTES */
  39. };
  40. /*******************************************************************************
  41. *
  42. * FUNCTION: acpi_ex_get_protocol_buffer_length
  43. *
  44. * PARAMETERS: protocol_id - The type of the protocol indicated by region
  45. * field access attributes
  46. * return_length - Where the protocol byte transfer length is
  47. * returned
  48. *
  49. * RETURN: Status and decoded byte transfer length
  50. *
  51. * DESCRIPTION: This routine returns the length of the generic_serial_bus
  52. * protocol bytes
  53. *
  54. ******************************************************************************/
  55. acpi_status
  56. acpi_ex_get_protocol_buffer_length(u32 protocol_id, u32 *return_length)
  57. {
  58. if ((protocol_id > ACPI_MAX_PROTOCOL_ID) ||
  59. (acpi_protocol_lengths[protocol_id] == ACPI_INVALID_PROTOCOL_ID)) {
  60. ACPI_ERROR((AE_INFO,
  61. "Invalid Field/AccessAs protocol ID: 0x%4.4X",
  62. protocol_id));
  63. return (AE_AML_PROTOCOL);
  64. }
  65. *return_length = acpi_protocol_lengths[protocol_id];
  66. return (AE_OK);
  67. }
  68. /*******************************************************************************
  69. *
  70. * FUNCTION: acpi_ex_read_data_from_field
  71. *
  72. * PARAMETERS: walk_state - Current execution state
  73. * obj_desc - The named field
  74. * ret_buffer_desc - Where the return data object is stored
  75. *
  76. * RETURN: Status
  77. *
  78. * DESCRIPTION: Read from a named field. Returns either an Integer or a
  79. * Buffer, depending on the size of the field.
  80. *
  81. ******************************************************************************/
  82. acpi_status
  83. acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  84. union acpi_operand_object *obj_desc,
  85. union acpi_operand_object **ret_buffer_desc)
  86. {
  87. acpi_status status;
  88. union acpi_operand_object *buffer_desc;
  89. void *buffer;
  90. u32 buffer_length;
  91. ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
  92. /* Parameter validation */
  93. if (!obj_desc) {
  94. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  95. }
  96. if (!ret_buffer_desc) {
  97. return_ACPI_STATUS(AE_BAD_PARAMETER);
  98. }
  99. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  100. /*
  101. * If the buffer_field arguments have not been previously evaluated,
  102. * evaluate them now and save the results.
  103. */
  104. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  105. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  106. if (ACPI_FAILURE(status)) {
  107. return_ACPI_STATUS(status);
  108. }
  109. }
  110. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  111. (obj_desc->field.region_obj->region.space_id ==
  112. ACPI_ADR_SPACE_SMBUS
  113. || obj_desc->field.region_obj->region.space_id ==
  114. ACPI_ADR_SPACE_GSBUS
  115. || obj_desc->field.region_obj->region.space_id ==
  116. ACPI_ADR_SPACE_IPMI)) {
  117. /* SMBus, GSBus, IPMI serial */
  118. status = acpi_ex_read_serial_bus(obj_desc, ret_buffer_desc);
  119. return_ACPI_STATUS(status);
  120. }
  121. /*
  122. * Allocate a buffer for the contents of the field.
  123. *
  124. * If the field is larger than the current integer width, create
  125. * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
  126. * the use of arithmetic operators on the returned value if the
  127. * field size is equal or smaller than an Integer.
  128. *
  129. * Note: Field.length is in bits.
  130. */
  131. buffer_length =
  132. (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  133. if (buffer_length > acpi_gbl_integer_byte_width) {
  134. /* Field is too large for an Integer, create a Buffer instead */
  135. buffer_desc = acpi_ut_create_buffer_object(buffer_length);
  136. if (!buffer_desc) {
  137. return_ACPI_STATUS(AE_NO_MEMORY);
  138. }
  139. buffer = buffer_desc->buffer.pointer;
  140. } else {
  141. /* Field will fit within an Integer (normal case) */
  142. buffer_desc = acpi_ut_create_integer_object((u64) 0);
  143. if (!buffer_desc) {
  144. return_ACPI_STATUS(AE_NO_MEMORY);
  145. }
  146. buffer_length = acpi_gbl_integer_byte_width;
  147. buffer = &buffer_desc->integer.value;
  148. }
  149. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  150. (obj_desc->field.region_obj->region.space_id ==
  151. ACPI_ADR_SPACE_GPIO)) {
  152. /* General Purpose I/O */
  153. status = acpi_ex_read_gpio(obj_desc, buffer);
  154. goto exit;
  155. }
  156. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  157. "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
  158. obj_desc, obj_desc->common.type, buffer,
  159. buffer_length));
  160. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  161. "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
  162. obj_desc->common_field.bit_length,
  163. obj_desc->common_field.start_field_bit_offset,
  164. obj_desc->common_field.base_byte_offset));
  165. /* Lock entire transaction if requested */
  166. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  167. /* Read from the field */
  168. status = acpi_ex_extract_from_field(obj_desc, buffer, buffer_length);
  169. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  170. exit:
  171. if (ACPI_FAILURE(status)) {
  172. acpi_ut_remove_reference(buffer_desc);
  173. } else {
  174. *ret_buffer_desc = buffer_desc;
  175. }
  176. return_ACPI_STATUS(status);
  177. }
  178. /*******************************************************************************
  179. *
  180. * FUNCTION: acpi_ex_write_data_to_field
  181. *
  182. * PARAMETERS: source_desc - Contains data to write
  183. * obj_desc - The named field
  184. * result_desc - Where the return value is returned, if any
  185. *
  186. * RETURN: Status
  187. *
  188. * DESCRIPTION: Write to a named field
  189. *
  190. ******************************************************************************/
  191. acpi_status
  192. acpi_ex_write_data_to_field(union acpi_operand_object *source_desc,
  193. union acpi_operand_object *obj_desc,
  194. union acpi_operand_object **result_desc)
  195. {
  196. acpi_status status;
  197. u32 buffer_length;
  198. void *buffer;
  199. ACPI_FUNCTION_TRACE_PTR(ex_write_data_to_field, obj_desc);
  200. /* Parameter validation */
  201. if (!source_desc || !obj_desc) {
  202. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  203. }
  204. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  205. /*
  206. * If the buffer_field arguments have not been previously evaluated,
  207. * evaluate them now and save the results.
  208. */
  209. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  210. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  211. if (ACPI_FAILURE(status)) {
  212. return_ACPI_STATUS(status);
  213. }
  214. }
  215. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  216. (obj_desc->field.region_obj->region.space_id ==
  217. ACPI_ADR_SPACE_GPIO)) {
  218. /* General Purpose I/O */
  219. status = acpi_ex_write_gpio(source_desc, obj_desc, result_desc);
  220. return_ACPI_STATUS(status);
  221. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  222. (obj_desc->field.region_obj->region.space_id ==
  223. ACPI_ADR_SPACE_SMBUS
  224. || obj_desc->field.region_obj->region.space_id ==
  225. ACPI_ADR_SPACE_GSBUS
  226. || obj_desc->field.region_obj->region.space_id ==
  227. ACPI_ADR_SPACE_IPMI)) {
  228. /* SMBus, GSBus, IPMI serial */
  229. status =
  230. acpi_ex_write_serial_bus(source_desc, obj_desc,
  231. result_desc);
  232. return_ACPI_STATUS(status);
  233. }
  234. /* Get a pointer to the data to be written */
  235. switch (source_desc->common.type) {
  236. case ACPI_TYPE_INTEGER:
  237. buffer = &source_desc->integer.value;
  238. buffer_length = sizeof(source_desc->integer.value);
  239. break;
  240. case ACPI_TYPE_BUFFER:
  241. buffer = source_desc->buffer.pointer;
  242. buffer_length = source_desc->buffer.length;
  243. break;
  244. case ACPI_TYPE_STRING:
  245. buffer = source_desc->string.pointer;
  246. buffer_length = source_desc->string.length;
  247. break;
  248. default:
  249. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  250. }
  251. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  252. "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
  253. source_desc,
  254. acpi_ut_get_type_name(source_desc->common.type),
  255. source_desc->common.type, buffer, buffer_length));
  256. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  257. "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
  258. obj_desc,
  259. acpi_ut_get_type_name(obj_desc->common.type),
  260. obj_desc->common.type,
  261. obj_desc->common_field.bit_length,
  262. obj_desc->common_field.start_field_bit_offset,
  263. obj_desc->common_field.base_byte_offset));
  264. /* Lock entire transaction if requested */
  265. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  266. /* Write to the field */
  267. status = acpi_ex_insert_into_field(obj_desc, buffer, buffer_length);
  268. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  269. return_ACPI_STATUS(status);
  270. }