exfield.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
  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. /* Local prototypes */
  17. static u32
  18. acpi_ex_get_serial_access_length(u32 accessor_type, u32 access_length);
  19. /*******************************************************************************
  20. *
  21. * FUNCTION: acpi_ex_get_serial_access_length
  22. *
  23. * PARAMETERS: accessor_type - The type of the protocol indicated by region
  24. * field access attributes
  25. * access_length - The access length of the region field
  26. *
  27. * RETURN: Decoded access length
  28. *
  29. * DESCRIPTION: This routine returns the length of the generic_serial_bus
  30. * protocol bytes
  31. *
  32. ******************************************************************************/
  33. static u32
  34. acpi_ex_get_serial_access_length(u32 accessor_type, u32 access_length)
  35. {
  36. u32 length;
  37. switch (accessor_type) {
  38. case AML_FIELD_ATTRIB_QUICK:
  39. length = 0;
  40. break;
  41. case AML_FIELD_ATTRIB_SEND_RCV:
  42. case AML_FIELD_ATTRIB_BYTE:
  43. length = 1;
  44. break;
  45. case AML_FIELD_ATTRIB_WORD:
  46. case AML_FIELD_ATTRIB_WORD_CALL:
  47. length = 2;
  48. break;
  49. case AML_FIELD_ATTRIB_MULTIBYTE:
  50. case AML_FIELD_ATTRIB_RAW_BYTES:
  51. case AML_FIELD_ATTRIB_RAW_PROCESS:
  52. length = access_length;
  53. break;
  54. case AML_FIELD_ATTRIB_BLOCK:
  55. case AML_FIELD_ATTRIB_BLOCK_CALL:
  56. default:
  57. length = ACPI_GSBUS_BUFFER_SIZE - 2;
  58. break;
  59. }
  60. return (length);
  61. }
  62. /*******************************************************************************
  63. *
  64. * FUNCTION: acpi_ex_read_data_from_field
  65. *
  66. * PARAMETERS: walk_state - Current execution state
  67. * obj_desc - The named field
  68. * ret_buffer_desc - Where the return data object is stored
  69. *
  70. * RETURN: Status
  71. *
  72. * DESCRIPTION: Read from a named field. Returns either an Integer or a
  73. * Buffer, depending on the size of the field.
  74. *
  75. ******************************************************************************/
  76. acpi_status
  77. acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  78. union acpi_operand_object *obj_desc,
  79. union acpi_operand_object **ret_buffer_desc)
  80. {
  81. acpi_status status;
  82. union acpi_operand_object *buffer_desc;
  83. acpi_size length;
  84. void *buffer;
  85. u32 function;
  86. u16 accessor_type;
  87. ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
  88. /* Parameter validation */
  89. if (!obj_desc) {
  90. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  91. }
  92. if (!ret_buffer_desc) {
  93. return_ACPI_STATUS(AE_BAD_PARAMETER);
  94. }
  95. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  96. /*
  97. * If the buffer_field arguments have not been previously evaluated,
  98. * evaluate them now and save the results.
  99. */
  100. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  101. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  102. if (ACPI_FAILURE(status)) {
  103. return_ACPI_STATUS(status);
  104. }
  105. }
  106. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  107. (obj_desc->field.region_obj->region.space_id ==
  108. ACPI_ADR_SPACE_SMBUS
  109. || obj_desc->field.region_obj->region.space_id ==
  110. ACPI_ADR_SPACE_GSBUS
  111. || obj_desc->field.region_obj->region.space_id ==
  112. ACPI_ADR_SPACE_IPMI)) {
  113. /*
  114. * This is an SMBus, GSBus or IPMI read. We must create a buffer to
  115. * hold the data and then directly access the region handler.
  116. *
  117. * Note: SMBus and GSBus protocol value is passed in upper 16-bits
  118. * of Function
  119. */
  120. if (obj_desc->field.region_obj->region.space_id ==
  121. ACPI_ADR_SPACE_SMBUS) {
  122. length = ACPI_SMBUS_BUFFER_SIZE;
  123. function =
  124. ACPI_READ | (obj_desc->field.attribute << 16);
  125. } else if (obj_desc->field.region_obj->region.space_id ==
  126. ACPI_ADR_SPACE_GSBUS) {
  127. accessor_type = obj_desc->field.attribute;
  128. length =
  129. acpi_ex_get_serial_access_length(accessor_type,
  130. obj_desc->field.
  131. access_length);
  132. /*
  133. * Add additional 2 bytes for the generic_serial_bus data buffer:
  134. *
  135. * Status; (Byte 0 of the data buffer)
  136. * Length; (Byte 1 of the data buffer)
  137. * Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
  138. */
  139. length += 2;
  140. function = ACPI_READ | (accessor_type << 16);
  141. } else { /* IPMI */
  142. length = ACPI_IPMI_BUFFER_SIZE;
  143. function = ACPI_READ;
  144. }
  145. buffer_desc = acpi_ut_create_buffer_object(length);
  146. if (!buffer_desc) {
  147. return_ACPI_STATUS(AE_NO_MEMORY);
  148. }
  149. /* Lock entire transaction if requested */
  150. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  151. /* Call the region handler for the read */
  152. status = acpi_ex_access_region(obj_desc, 0,
  153. ACPI_CAST_PTR(u64,
  154. buffer_desc->
  155. buffer.pointer),
  156. function);
  157. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  158. goto exit;
  159. }
  160. /*
  161. * Allocate a buffer for the contents of the field.
  162. *
  163. * If the field is larger than the current integer width, create
  164. * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
  165. * the use of arithmetic operators on the returned value if the
  166. * field size is equal or smaller than an Integer.
  167. *
  168. * Note: Field.length is in bits.
  169. */
  170. length =
  171. (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  172. if (length > acpi_gbl_integer_byte_width) {
  173. /* Field is too large for an Integer, create a Buffer instead */
  174. buffer_desc = acpi_ut_create_buffer_object(length);
  175. if (!buffer_desc) {
  176. return_ACPI_STATUS(AE_NO_MEMORY);
  177. }
  178. buffer = buffer_desc->buffer.pointer;
  179. } else {
  180. /* Field will fit within an Integer (normal case) */
  181. buffer_desc = acpi_ut_create_integer_object((u64) 0);
  182. if (!buffer_desc) {
  183. return_ACPI_STATUS(AE_NO_MEMORY);
  184. }
  185. length = acpi_gbl_integer_byte_width;
  186. buffer = &buffer_desc->integer.value;
  187. }
  188. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  189. (obj_desc->field.region_obj->region.space_id ==
  190. ACPI_ADR_SPACE_GPIO)) {
  191. /*
  192. * For GPIO (general_purpose_io), the Address will be the bit offset
  193. * from the previous Connection() operator, making it effectively a
  194. * pin number index. The bit_length is the length of the field, which
  195. * is thus the number of pins.
  196. */
  197. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  198. "GPIO FieldRead [FROM]: Pin %u Bits %u\n",
  199. obj_desc->field.pin_number_index,
  200. obj_desc->field.bit_length));
  201. /* Lock entire transaction if requested */
  202. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  203. /* Perform the write */
  204. status =
  205. acpi_ex_access_region(obj_desc, 0, (u64 *)buffer,
  206. ACPI_READ);
  207. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  208. if (ACPI_FAILURE(status)) {
  209. acpi_ut_remove_reference(buffer_desc);
  210. } else {
  211. *ret_buffer_desc = buffer_desc;
  212. }
  213. return_ACPI_STATUS(status);
  214. }
  215. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  216. "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
  217. obj_desc, obj_desc->common.type, buffer,
  218. (u32) length));
  219. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  220. "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
  221. obj_desc->common_field.bit_length,
  222. obj_desc->common_field.start_field_bit_offset,
  223. obj_desc->common_field.base_byte_offset));
  224. /* Lock entire transaction if requested */
  225. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  226. /* Read from the field */
  227. status = acpi_ex_extract_from_field(obj_desc, buffer, (u32) length);
  228. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  229. exit:
  230. if (ACPI_FAILURE(status)) {
  231. acpi_ut_remove_reference(buffer_desc);
  232. } else {
  233. *ret_buffer_desc = buffer_desc;
  234. }
  235. return_ACPI_STATUS(status);
  236. }
  237. /*******************************************************************************
  238. *
  239. * FUNCTION: acpi_ex_write_data_to_field
  240. *
  241. * PARAMETERS: source_desc - Contains data to write
  242. * obj_desc - The named field
  243. * result_desc - Where the return value is returned, if any
  244. *
  245. * RETURN: Status
  246. *
  247. * DESCRIPTION: Write to a named field
  248. *
  249. ******************************************************************************/
  250. acpi_status
  251. acpi_ex_write_data_to_field(union acpi_operand_object *source_desc,
  252. union acpi_operand_object *obj_desc,
  253. union acpi_operand_object **result_desc)
  254. {
  255. acpi_status status;
  256. u32 length;
  257. void *buffer;
  258. union acpi_operand_object *buffer_desc;
  259. u32 function;
  260. u16 accessor_type;
  261. ACPI_FUNCTION_TRACE_PTR(ex_write_data_to_field, obj_desc);
  262. /* Parameter validation */
  263. if (!source_desc || !obj_desc) {
  264. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  265. }
  266. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  267. /*
  268. * If the buffer_field arguments have not been previously evaluated,
  269. * evaluate them now and save the results.
  270. */
  271. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  272. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  273. if (ACPI_FAILURE(status)) {
  274. return_ACPI_STATUS(status);
  275. }
  276. }
  277. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  278. (obj_desc->field.region_obj->region.space_id ==
  279. ACPI_ADR_SPACE_SMBUS
  280. || obj_desc->field.region_obj->region.space_id ==
  281. ACPI_ADR_SPACE_GSBUS
  282. || obj_desc->field.region_obj->region.space_id ==
  283. ACPI_ADR_SPACE_IPMI)) {
  284. /*
  285. * This is an SMBus, GSBus or IPMI write. We will bypass the entire
  286. * field mechanism and handoff the buffer directly to the handler.
  287. * For these address spaces, the buffer is bi-directional; on a
  288. * write, return data is returned in the same buffer.
  289. *
  290. * Source must be a buffer of sufficient size:
  291. * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or
  292. * ACPI_IPMI_BUFFER_SIZE.
  293. *
  294. * Note: SMBus and GSBus protocol type is passed in upper 16-bits
  295. * of Function
  296. */
  297. if (source_desc->common.type != ACPI_TYPE_BUFFER) {
  298. ACPI_ERROR((AE_INFO,
  299. "SMBus/IPMI/GenericSerialBus write requires "
  300. "Buffer, found type %s",
  301. acpi_ut_get_object_type_name(source_desc)));
  302. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  303. }
  304. if (obj_desc->field.region_obj->region.space_id ==
  305. ACPI_ADR_SPACE_SMBUS) {
  306. length = ACPI_SMBUS_BUFFER_SIZE;
  307. function =
  308. ACPI_WRITE | (obj_desc->field.attribute << 16);
  309. } else if (obj_desc->field.region_obj->region.space_id ==
  310. ACPI_ADR_SPACE_GSBUS) {
  311. accessor_type = obj_desc->field.attribute;
  312. length =
  313. acpi_ex_get_serial_access_length(accessor_type,
  314. obj_desc->field.
  315. access_length);
  316. /*
  317. * Add additional 2 bytes for the generic_serial_bus data buffer:
  318. *
  319. * Status; (Byte 0 of the data buffer)
  320. * Length; (Byte 1 of the data buffer)
  321. * Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
  322. */
  323. length += 2;
  324. function = ACPI_WRITE | (accessor_type << 16);
  325. } else { /* IPMI */
  326. length = ACPI_IPMI_BUFFER_SIZE;
  327. function = ACPI_WRITE;
  328. }
  329. if (source_desc->buffer.length < length) {
  330. ACPI_ERROR((AE_INFO,
  331. "SMBus/IPMI/GenericSerialBus write requires "
  332. "Buffer of length %u, found length %u",
  333. length, source_desc->buffer.length));
  334. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  335. }
  336. /* Create the bi-directional buffer */
  337. buffer_desc = acpi_ut_create_buffer_object(length);
  338. if (!buffer_desc) {
  339. return_ACPI_STATUS(AE_NO_MEMORY);
  340. }
  341. buffer = buffer_desc->buffer.pointer;
  342. memcpy(buffer, source_desc->buffer.pointer, length);
  343. /* Lock entire transaction if requested */
  344. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  345. /*
  346. * Perform the write (returns status and perhaps data in the
  347. * same buffer)
  348. */
  349. status =
  350. acpi_ex_access_region(obj_desc, 0, (u64 *)buffer, function);
  351. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  352. *result_desc = buffer_desc;
  353. return_ACPI_STATUS(status);
  354. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  355. (obj_desc->field.region_obj->region.space_id ==
  356. ACPI_ADR_SPACE_GPIO)) {
  357. /*
  358. * For GPIO (general_purpose_io), we will bypass the entire field
  359. * mechanism and handoff the bit address and bit width directly to
  360. * the handler. The Address will be the bit offset
  361. * from the previous Connection() operator, making it effectively a
  362. * pin number index. The bit_length is the length of the field, which
  363. * is thus the number of pins.
  364. */
  365. if (source_desc->common.type != ACPI_TYPE_INTEGER) {
  366. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  367. }
  368. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  369. "GPIO FieldWrite [FROM]: (%s:%X), Val %.8X [TO]: Pin %u Bits %u\n",
  370. acpi_ut_get_type_name(source_desc->common.
  371. type),
  372. source_desc->common.type,
  373. (u32)source_desc->integer.value,
  374. obj_desc->field.pin_number_index,
  375. obj_desc->field.bit_length));
  376. buffer = &source_desc->integer.value;
  377. /* Lock entire transaction if requested */
  378. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  379. /* Perform the write */
  380. status =
  381. acpi_ex_access_region(obj_desc, 0, (u64 *)buffer,
  382. ACPI_WRITE);
  383. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  384. return_ACPI_STATUS(status);
  385. }
  386. /* Get a pointer to the data to be written */
  387. switch (source_desc->common.type) {
  388. case ACPI_TYPE_INTEGER:
  389. buffer = &source_desc->integer.value;
  390. length = sizeof(source_desc->integer.value);
  391. break;
  392. case ACPI_TYPE_BUFFER:
  393. buffer = source_desc->buffer.pointer;
  394. length = source_desc->buffer.length;
  395. break;
  396. case ACPI_TYPE_STRING:
  397. buffer = source_desc->string.pointer;
  398. length = source_desc->string.length;
  399. break;
  400. default:
  401. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  402. }
  403. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  404. "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
  405. source_desc,
  406. acpi_ut_get_type_name(source_desc->common.type),
  407. source_desc->common.type, buffer, length));
  408. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  409. "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
  410. obj_desc,
  411. acpi_ut_get_type_name(obj_desc->common.type),
  412. obj_desc->common.type,
  413. obj_desc->common_field.bit_length,
  414. obj_desc->common_field.start_field_bit_offset,
  415. obj_desc->common_field.base_byte_offset));
  416. /* Lock entire transaction if requested */
  417. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  418. /* Write to the field */
  419. status = acpi_ex_insert_into_field(obj_desc, buffer, length);
  420. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  421. return_ACPI_STATUS(status);
  422. }