exfield.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 - 2022, 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. static 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. #define PCC_MASTER_SUBSPACE 3
  41. /*
  42. * The following macros determine a given offset is a COMD field.
  43. * According to the specification, generic subspaces (types 0-2) contains a
  44. * 2-byte COMD field at offset 4 and master subspaces (type 3) contains a 4-byte
  45. * COMD field starting at offset 12.
  46. */
  47. #define GENERIC_SUBSPACE_COMMAND(a) (4 == a || a == 5)
  48. #define MASTER_SUBSPACE_COMMAND(a) (12 <= a && a <= 15)
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ex_get_protocol_buffer_length
  52. *
  53. * PARAMETERS: protocol_id - The type of the protocol indicated by region
  54. * field access attributes
  55. * return_length - Where the protocol byte transfer length is
  56. * returned
  57. *
  58. * RETURN: Status and decoded byte transfer length
  59. *
  60. * DESCRIPTION: This routine returns the length of the generic_serial_bus
  61. * protocol bytes
  62. *
  63. ******************************************************************************/
  64. acpi_status
  65. acpi_ex_get_protocol_buffer_length(u32 protocol_id, u32 *return_length)
  66. {
  67. if ((protocol_id > ACPI_MAX_PROTOCOL_ID) ||
  68. (acpi_protocol_lengths[protocol_id] == ACPI_INVALID_PROTOCOL_ID)) {
  69. ACPI_ERROR((AE_INFO,
  70. "Invalid Field/AccessAs protocol ID: 0x%4.4X",
  71. protocol_id));
  72. return (AE_AML_PROTOCOL);
  73. }
  74. *return_length = acpi_protocol_lengths[protocol_id];
  75. return (AE_OK);
  76. }
  77. /*******************************************************************************
  78. *
  79. * FUNCTION: acpi_ex_read_data_from_field
  80. *
  81. * PARAMETERS: walk_state - Current execution state
  82. * obj_desc - The named field
  83. * ret_buffer_desc - Where the return data object is stored
  84. *
  85. * RETURN: Status
  86. *
  87. * DESCRIPTION: Read from a named field. Returns either an Integer or a
  88. * Buffer, depending on the size of the field and whether if a
  89. * field is created by the create_field() operator.
  90. *
  91. ******************************************************************************/
  92. acpi_status
  93. acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  94. union acpi_operand_object *obj_desc,
  95. union acpi_operand_object **ret_buffer_desc)
  96. {
  97. acpi_status status;
  98. union acpi_operand_object *buffer_desc;
  99. void *buffer;
  100. u32 buffer_length;
  101. ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
  102. /* Parameter validation */
  103. if (!obj_desc) {
  104. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  105. }
  106. if (!ret_buffer_desc) {
  107. return_ACPI_STATUS(AE_BAD_PARAMETER);
  108. }
  109. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  110. /*
  111. * If the buffer_field arguments have not been previously evaluated,
  112. * evaluate them now and save the results.
  113. */
  114. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  115. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  116. if (ACPI_FAILURE(status)) {
  117. return_ACPI_STATUS(status);
  118. }
  119. }
  120. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  121. (obj_desc->field.region_obj->region.space_id ==
  122. ACPI_ADR_SPACE_SMBUS
  123. || obj_desc->field.region_obj->region.space_id ==
  124. ACPI_ADR_SPACE_GSBUS
  125. || obj_desc->field.region_obj->region.space_id ==
  126. ACPI_ADR_SPACE_IPMI
  127. || obj_desc->field.region_obj->region.space_id ==
  128. ACPI_ADR_SPACE_PLATFORM_RT)) {
  129. /* SMBus, GSBus, IPMI serial */
  130. status = acpi_ex_read_serial_bus(obj_desc, ret_buffer_desc);
  131. return_ACPI_STATUS(status);
  132. }
  133. /*
  134. * Allocate a buffer for the contents of the field.
  135. *
  136. * If the field is larger than the current integer width, create
  137. * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
  138. * the use of arithmetic operators on the returned value if the
  139. * field size is equal or smaller than an Integer.
  140. *
  141. * However, all buffer fields created by create_field operator needs to
  142. * remain as a buffer to match other AML interpreter implementations.
  143. *
  144. * Note: Field.length is in bits.
  145. */
  146. buffer_length =
  147. (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  148. if (buffer_length > acpi_gbl_integer_byte_width ||
  149. (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD &&
  150. obj_desc->buffer_field.is_create_field)) {
  151. /* Field is too large for an Integer, create a Buffer instead */
  152. buffer_desc = acpi_ut_create_buffer_object(buffer_length);
  153. if (!buffer_desc) {
  154. return_ACPI_STATUS(AE_NO_MEMORY);
  155. }
  156. buffer = buffer_desc->buffer.pointer;
  157. } else {
  158. /* Field will fit within an Integer (normal case) */
  159. buffer_desc = acpi_ut_create_integer_object((u64) 0);
  160. if (!buffer_desc) {
  161. return_ACPI_STATUS(AE_NO_MEMORY);
  162. }
  163. buffer_length = acpi_gbl_integer_byte_width;
  164. buffer = &buffer_desc->integer.value;
  165. }
  166. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  167. (obj_desc->field.region_obj->region.space_id ==
  168. ACPI_ADR_SPACE_GPIO)) {
  169. /* General Purpose I/O */
  170. status = acpi_ex_read_gpio(obj_desc, buffer);
  171. goto exit;
  172. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  173. (obj_desc->field.region_obj->region.space_id ==
  174. ACPI_ADR_SPACE_PLATFORM_COMM)) {
  175. /*
  176. * Reading from a PCC field unit does not require the handler because
  177. * it only requires reading from the internal_pcc_buffer.
  178. */
  179. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  180. "PCC FieldRead bits %u\n",
  181. obj_desc->field.bit_length));
  182. memcpy(buffer,
  183. obj_desc->field.region_obj->field.internal_pcc_buffer +
  184. obj_desc->field.base_byte_offset,
  185. (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.
  186. bit_length));
  187. *ret_buffer_desc = buffer_desc;
  188. return AE_OK;
  189. }
  190. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  191. "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
  192. obj_desc, obj_desc->common.type, buffer,
  193. buffer_length));
  194. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  195. "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
  196. obj_desc->common_field.bit_length,
  197. obj_desc->common_field.start_field_bit_offset,
  198. obj_desc->common_field.base_byte_offset));
  199. /* Lock entire transaction if requested */
  200. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  201. /* Read from the field */
  202. status = acpi_ex_extract_from_field(obj_desc, buffer, buffer_length);
  203. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  204. exit:
  205. if (ACPI_FAILURE(status)) {
  206. acpi_ut_remove_reference(buffer_desc);
  207. } else {
  208. *ret_buffer_desc = buffer_desc;
  209. }
  210. return_ACPI_STATUS(status);
  211. }
  212. /*******************************************************************************
  213. *
  214. * FUNCTION: acpi_ex_write_data_to_field
  215. *
  216. * PARAMETERS: source_desc - Contains data to write
  217. * obj_desc - The named field
  218. * result_desc - Where the return value is returned, if any
  219. *
  220. * RETURN: Status
  221. *
  222. * DESCRIPTION: Write to a named field
  223. *
  224. ******************************************************************************/
  225. acpi_status
  226. acpi_ex_write_data_to_field(union acpi_operand_object *source_desc,
  227. union acpi_operand_object *obj_desc,
  228. union acpi_operand_object **result_desc)
  229. {
  230. acpi_status status;
  231. u32 buffer_length;
  232. u32 data_length;
  233. void *buffer;
  234. ACPI_FUNCTION_TRACE_PTR(ex_write_data_to_field, obj_desc);
  235. /* Parameter validation */
  236. if (!source_desc || !obj_desc) {
  237. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  238. }
  239. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  240. /*
  241. * If the buffer_field arguments have not been previously evaluated,
  242. * evaluate them now and save the results.
  243. */
  244. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  245. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  246. if (ACPI_FAILURE(status)) {
  247. return_ACPI_STATUS(status);
  248. }
  249. }
  250. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  251. (obj_desc->field.region_obj->region.space_id ==
  252. ACPI_ADR_SPACE_GPIO)) {
  253. /* General Purpose I/O */
  254. status = acpi_ex_write_gpio(source_desc, obj_desc, result_desc);
  255. return_ACPI_STATUS(status);
  256. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  257. (obj_desc->field.region_obj->region.space_id ==
  258. ACPI_ADR_SPACE_SMBUS
  259. || obj_desc->field.region_obj->region.space_id ==
  260. ACPI_ADR_SPACE_GSBUS
  261. || obj_desc->field.region_obj->region.space_id ==
  262. ACPI_ADR_SPACE_IPMI
  263. || obj_desc->field.region_obj->region.space_id ==
  264. ACPI_ADR_SPACE_PLATFORM_RT)) {
  265. /* SMBus, GSBus, IPMI serial */
  266. status =
  267. acpi_ex_write_serial_bus(source_desc, obj_desc,
  268. result_desc);
  269. return_ACPI_STATUS(status);
  270. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  271. (obj_desc->field.region_obj->region.space_id ==
  272. ACPI_ADR_SPACE_PLATFORM_COMM)) {
  273. /*
  274. * According to the spec a write to the COMD field will invoke the
  275. * region handler. Otherwise, write to the pcc_internal buffer. This
  276. * implementation will use the offsets specified rather than the name
  277. * of the field. This is considered safer because some firmware tools
  278. * are known to obfiscate named objects.
  279. */
  280. data_length =
  281. (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.
  282. bit_length);
  283. memcpy(obj_desc->field.region_obj->field.internal_pcc_buffer +
  284. obj_desc->field.base_byte_offset,
  285. source_desc->buffer.pointer, data_length);
  286. if (MASTER_SUBSPACE_COMMAND(obj_desc->field.base_byte_offset)) {
  287. /* Perform the write */
  288. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  289. "PCC COMD field has been written. Invoking PCC handler now.\n"));
  290. status =
  291. acpi_ex_access_region(obj_desc, 0,
  292. (u64 *)obj_desc->field.
  293. region_obj->field.
  294. internal_pcc_buffer,
  295. ACPI_WRITE);
  296. return_ACPI_STATUS(status);
  297. }
  298. return (AE_OK);
  299. }
  300. /* Get a pointer to the data to be written */
  301. switch (source_desc->common.type) {
  302. case ACPI_TYPE_INTEGER:
  303. buffer = &source_desc->integer.value;
  304. buffer_length = sizeof(source_desc->integer.value);
  305. break;
  306. case ACPI_TYPE_BUFFER:
  307. buffer = source_desc->buffer.pointer;
  308. buffer_length = source_desc->buffer.length;
  309. break;
  310. case ACPI_TYPE_STRING:
  311. buffer = source_desc->string.pointer;
  312. buffer_length = source_desc->string.length;
  313. break;
  314. default:
  315. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  316. }
  317. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  318. "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
  319. source_desc,
  320. acpi_ut_get_type_name(source_desc->common.type),
  321. source_desc->common.type, buffer, buffer_length));
  322. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  323. "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
  324. obj_desc,
  325. acpi_ut_get_type_name(obj_desc->common.type),
  326. obj_desc->common.type,
  327. obj_desc->common_field.bit_length,
  328. obj_desc->common_field.start_field_bit_offset,
  329. obj_desc->common_field.base_byte_offset));
  330. /* Lock entire transaction if requested */
  331. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  332. /* Write to the field */
  333. status = acpi_ex_insert_into_field(obj_desc, buffer, buffer_length);
  334. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  335. return_ACPI_STATUS(status);
  336. }