exfield.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /******************************************************************************
  2. *
  3. * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acdispat.h"
  45. #include "acinterp.h"
  46. #include "amlcode.h"
  47. #define _COMPONENT ACPI_EXECUTER
  48. ACPI_MODULE_NAME("exfield")
  49. /* Local prototypes */
  50. static u32
  51. acpi_ex_get_serial_access_length(u32 accessor_type, u32 access_length);
  52. /*******************************************************************************
  53. *
  54. * FUNCTION: acpi_ex_get_serial_access_length
  55. *
  56. * PARAMETERS: accessor_type - The type of the protocol indicated by region
  57. * field access attributes
  58. * access_length - The access length of the region field
  59. *
  60. * RETURN: Decoded access length
  61. *
  62. * DESCRIPTION: This routine returns the length of the generic_serial_bus
  63. * protocol bytes
  64. *
  65. ******************************************************************************/
  66. static u32
  67. acpi_ex_get_serial_access_length(u32 accessor_type, u32 access_length)
  68. {
  69. u32 length;
  70. switch (accessor_type) {
  71. case AML_FIELD_ATTRIB_QUICK:
  72. length = 0;
  73. break;
  74. case AML_FIELD_ATTRIB_SEND_RCV:
  75. case AML_FIELD_ATTRIB_BYTE:
  76. length = 1;
  77. break;
  78. case AML_FIELD_ATTRIB_WORD:
  79. case AML_FIELD_ATTRIB_WORD_CALL:
  80. length = 2;
  81. break;
  82. case AML_FIELD_ATTRIB_MULTIBYTE:
  83. case AML_FIELD_ATTRIB_RAW_BYTES:
  84. case AML_FIELD_ATTRIB_RAW_PROCESS:
  85. length = access_length;
  86. break;
  87. case AML_FIELD_ATTRIB_BLOCK:
  88. case AML_FIELD_ATTRIB_BLOCK_CALL:
  89. default:
  90. length = ACPI_GSBUS_BUFFER_SIZE - 2;
  91. break;
  92. }
  93. return (length);
  94. }
  95. /*******************************************************************************
  96. *
  97. * FUNCTION: acpi_ex_read_data_from_field
  98. *
  99. * PARAMETERS: walk_state - Current execution state
  100. * obj_desc - The named field
  101. * ret_buffer_desc - Where the return data object is stored
  102. *
  103. * RETURN: Status
  104. *
  105. * DESCRIPTION: Read from a named field. Returns either an Integer or a
  106. * Buffer, depending on the size of the field.
  107. *
  108. ******************************************************************************/
  109. acpi_status
  110. acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  111. union acpi_operand_object *obj_desc,
  112. union acpi_operand_object **ret_buffer_desc)
  113. {
  114. acpi_status status;
  115. union acpi_operand_object *buffer_desc;
  116. acpi_size length;
  117. void *buffer;
  118. u32 function;
  119. u16 accessor_type;
  120. ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
  121. /* Parameter validation */
  122. if (!obj_desc) {
  123. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  124. }
  125. if (!ret_buffer_desc) {
  126. return_ACPI_STATUS(AE_BAD_PARAMETER);
  127. }
  128. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  129. /*
  130. * If the buffer_field arguments have not been previously evaluated,
  131. * evaluate them now and save the results.
  132. */
  133. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  134. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  135. if (ACPI_FAILURE(status)) {
  136. return_ACPI_STATUS(status);
  137. }
  138. }
  139. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  140. (obj_desc->field.region_obj->region.space_id ==
  141. ACPI_ADR_SPACE_SMBUS
  142. || obj_desc->field.region_obj->region.space_id ==
  143. ACPI_ADR_SPACE_GSBUS
  144. || obj_desc->field.region_obj->region.space_id ==
  145. ACPI_ADR_SPACE_IPMI)) {
  146. /*
  147. * This is an SMBus, GSBus or IPMI read. We must create a buffer to
  148. * hold the data and then directly access the region handler.
  149. *
  150. * Note: SMBus and GSBus protocol value is passed in upper 16-bits
  151. * of Function
  152. */
  153. if (obj_desc->field.region_obj->region.space_id ==
  154. ACPI_ADR_SPACE_SMBUS) {
  155. length = ACPI_SMBUS_BUFFER_SIZE;
  156. function =
  157. ACPI_READ | (obj_desc->field.attribute << 16);
  158. } else if (obj_desc->field.region_obj->region.space_id ==
  159. ACPI_ADR_SPACE_GSBUS) {
  160. accessor_type = obj_desc->field.attribute;
  161. length =
  162. acpi_ex_get_serial_access_length(accessor_type,
  163. obj_desc->field.
  164. access_length);
  165. /*
  166. * Add additional 2 bytes for the generic_serial_bus data buffer:
  167. *
  168. * Status; (Byte 0 of the data buffer)
  169. * Length; (Byte 1 of the data buffer)
  170. * Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
  171. */
  172. length += 2;
  173. function = ACPI_READ | (accessor_type << 16);
  174. } else { /* IPMI */
  175. length = ACPI_IPMI_BUFFER_SIZE;
  176. function = ACPI_READ;
  177. }
  178. buffer_desc = acpi_ut_create_buffer_object(length);
  179. if (!buffer_desc) {
  180. return_ACPI_STATUS(AE_NO_MEMORY);
  181. }
  182. /* Lock entire transaction if requested */
  183. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  184. /* Call the region handler for the read */
  185. status = acpi_ex_access_region(obj_desc, 0,
  186. ACPI_CAST_PTR(u64,
  187. buffer_desc->
  188. buffer.pointer),
  189. function);
  190. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  191. goto exit;
  192. }
  193. /*
  194. * Allocate a buffer for the contents of the field.
  195. *
  196. * If the field is larger than the current integer width, create
  197. * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
  198. * the use of arithmetic operators on the returned value if the
  199. * field size is equal or smaller than an Integer.
  200. *
  201. * Note: Field.length is in bits.
  202. */
  203. length =
  204. (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  205. if (length > acpi_gbl_integer_byte_width) {
  206. /* Field is too large for an Integer, create a Buffer instead */
  207. buffer_desc = acpi_ut_create_buffer_object(length);
  208. if (!buffer_desc) {
  209. return_ACPI_STATUS(AE_NO_MEMORY);
  210. }
  211. buffer = buffer_desc->buffer.pointer;
  212. } else {
  213. /* Field will fit within an Integer (normal case) */
  214. buffer_desc = acpi_ut_create_integer_object((u64) 0);
  215. if (!buffer_desc) {
  216. return_ACPI_STATUS(AE_NO_MEMORY);
  217. }
  218. length = acpi_gbl_integer_byte_width;
  219. buffer = &buffer_desc->integer.value;
  220. }
  221. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  222. (obj_desc->field.region_obj->region.space_id ==
  223. ACPI_ADR_SPACE_GPIO)) {
  224. /*
  225. * For GPIO (general_purpose_io), the Address will be the bit offset
  226. * from the previous Connection() operator, making it effectively a
  227. * pin number index. The bit_length is the length of the field, which
  228. * is thus the number of pins.
  229. */
  230. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  231. "GPIO FieldRead [FROM]: Pin %u Bits %u\n",
  232. obj_desc->field.pin_number_index,
  233. obj_desc->field.bit_length));
  234. /* Lock entire transaction if requested */
  235. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  236. /* Perform the write */
  237. status =
  238. acpi_ex_access_region(obj_desc, 0, (u64 *)buffer,
  239. ACPI_READ);
  240. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  241. if (ACPI_FAILURE(status)) {
  242. acpi_ut_remove_reference(buffer_desc);
  243. } else {
  244. *ret_buffer_desc = buffer_desc;
  245. }
  246. return_ACPI_STATUS(status);
  247. }
  248. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  249. "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
  250. obj_desc, obj_desc->common.type, buffer,
  251. (u32) length));
  252. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  253. "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
  254. obj_desc->common_field.bit_length,
  255. obj_desc->common_field.start_field_bit_offset,
  256. obj_desc->common_field.base_byte_offset));
  257. /* Lock entire transaction if requested */
  258. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  259. /* Read from the field */
  260. status = acpi_ex_extract_from_field(obj_desc, buffer, (u32) length);
  261. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  262. exit:
  263. if (ACPI_FAILURE(status)) {
  264. acpi_ut_remove_reference(buffer_desc);
  265. } else {
  266. *ret_buffer_desc = buffer_desc;
  267. }
  268. return_ACPI_STATUS(status);
  269. }
  270. /*******************************************************************************
  271. *
  272. * FUNCTION: acpi_ex_write_data_to_field
  273. *
  274. * PARAMETERS: source_desc - Contains data to write
  275. * obj_desc - The named field
  276. * result_desc - Where the return value is returned, if any
  277. *
  278. * RETURN: Status
  279. *
  280. * DESCRIPTION: Write to a named field
  281. *
  282. ******************************************************************************/
  283. acpi_status
  284. acpi_ex_write_data_to_field(union acpi_operand_object *source_desc,
  285. union acpi_operand_object *obj_desc,
  286. union acpi_operand_object **result_desc)
  287. {
  288. acpi_status status;
  289. u32 length;
  290. void *buffer;
  291. union acpi_operand_object *buffer_desc;
  292. u32 function;
  293. u16 accessor_type;
  294. ACPI_FUNCTION_TRACE_PTR(ex_write_data_to_field, obj_desc);
  295. /* Parameter validation */
  296. if (!source_desc || !obj_desc) {
  297. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  298. }
  299. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  300. /*
  301. * If the buffer_field arguments have not been previously evaluated,
  302. * evaluate them now and save the results.
  303. */
  304. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  305. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  306. if (ACPI_FAILURE(status)) {
  307. return_ACPI_STATUS(status);
  308. }
  309. }
  310. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  311. (obj_desc->field.region_obj->region.space_id ==
  312. ACPI_ADR_SPACE_SMBUS
  313. || obj_desc->field.region_obj->region.space_id ==
  314. ACPI_ADR_SPACE_GSBUS
  315. || obj_desc->field.region_obj->region.space_id ==
  316. ACPI_ADR_SPACE_IPMI)) {
  317. /*
  318. * This is an SMBus, GSBus or IPMI write. We will bypass the entire
  319. * field mechanism and handoff the buffer directly to the handler.
  320. * For these address spaces, the buffer is bi-directional; on a
  321. * write, return data is returned in the same buffer.
  322. *
  323. * Source must be a buffer of sufficient size:
  324. * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or
  325. * ACPI_IPMI_BUFFER_SIZE.
  326. *
  327. * Note: SMBus and GSBus protocol type is passed in upper 16-bits
  328. * of Function
  329. */
  330. if (source_desc->common.type != ACPI_TYPE_BUFFER) {
  331. ACPI_ERROR((AE_INFO,
  332. "SMBus/IPMI/GenericSerialBus write requires "
  333. "Buffer, found type %s",
  334. acpi_ut_get_object_type_name(source_desc)));
  335. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  336. }
  337. if (obj_desc->field.region_obj->region.space_id ==
  338. ACPI_ADR_SPACE_SMBUS) {
  339. length = ACPI_SMBUS_BUFFER_SIZE;
  340. function =
  341. ACPI_WRITE | (obj_desc->field.attribute << 16);
  342. } else if (obj_desc->field.region_obj->region.space_id ==
  343. ACPI_ADR_SPACE_GSBUS) {
  344. accessor_type = obj_desc->field.attribute;
  345. length =
  346. acpi_ex_get_serial_access_length(accessor_type,
  347. obj_desc->field.
  348. access_length);
  349. /*
  350. * Add additional 2 bytes for the generic_serial_bus data buffer:
  351. *
  352. * Status; (Byte 0 of the data buffer)
  353. * Length; (Byte 1 of the data buffer)
  354. * Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
  355. */
  356. length += 2;
  357. function = ACPI_WRITE | (accessor_type << 16);
  358. } else { /* IPMI */
  359. length = ACPI_IPMI_BUFFER_SIZE;
  360. function = ACPI_WRITE;
  361. }
  362. if (source_desc->buffer.length < length) {
  363. ACPI_ERROR((AE_INFO,
  364. "SMBus/IPMI/GenericSerialBus write requires "
  365. "Buffer of length %u, found length %u",
  366. length, source_desc->buffer.length));
  367. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  368. }
  369. /* Create the bi-directional buffer */
  370. buffer_desc = acpi_ut_create_buffer_object(length);
  371. if (!buffer_desc) {
  372. return_ACPI_STATUS(AE_NO_MEMORY);
  373. }
  374. buffer = buffer_desc->buffer.pointer;
  375. memcpy(buffer, source_desc->buffer.pointer, length);
  376. /* Lock entire transaction if requested */
  377. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  378. /*
  379. * Perform the write (returns status and perhaps data in the
  380. * same buffer)
  381. */
  382. status =
  383. acpi_ex_access_region(obj_desc, 0, (u64 *)buffer, function);
  384. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  385. *result_desc = buffer_desc;
  386. return_ACPI_STATUS(status);
  387. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  388. (obj_desc->field.region_obj->region.space_id ==
  389. ACPI_ADR_SPACE_GPIO)) {
  390. /*
  391. * For GPIO (general_purpose_io), we will bypass the entire field
  392. * mechanism and handoff the bit address and bit width directly to
  393. * the handler. The Address will be the bit offset
  394. * from the previous Connection() operator, making it effectively a
  395. * pin number index. The bit_length is the length of the field, which
  396. * is thus the number of pins.
  397. */
  398. if (source_desc->common.type != ACPI_TYPE_INTEGER) {
  399. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  400. }
  401. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  402. "GPIO FieldWrite [FROM]: (%s:%X), Val %.8X [TO]: Pin %u Bits %u\n",
  403. acpi_ut_get_type_name(source_desc->common.
  404. type),
  405. source_desc->common.type,
  406. (u32)source_desc->integer.value,
  407. obj_desc->field.pin_number_index,
  408. obj_desc->field.bit_length));
  409. buffer = &source_desc->integer.value;
  410. /* Lock entire transaction if requested */
  411. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  412. /* Perform the write */
  413. status =
  414. acpi_ex_access_region(obj_desc, 0, (u64 *)buffer,
  415. ACPI_WRITE);
  416. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  417. return_ACPI_STATUS(status);
  418. }
  419. /* Get a pointer to the data to be written */
  420. switch (source_desc->common.type) {
  421. case ACPI_TYPE_INTEGER:
  422. buffer = &source_desc->integer.value;
  423. length = sizeof(source_desc->integer.value);
  424. break;
  425. case ACPI_TYPE_BUFFER:
  426. buffer = source_desc->buffer.pointer;
  427. length = source_desc->buffer.length;
  428. break;
  429. case ACPI_TYPE_STRING:
  430. buffer = source_desc->string.pointer;
  431. length = source_desc->string.length;
  432. break;
  433. default:
  434. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  435. }
  436. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  437. "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
  438. source_desc,
  439. acpi_ut_get_type_name(source_desc->common.type),
  440. source_desc->common.type, buffer, length));
  441. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  442. "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
  443. obj_desc,
  444. acpi_ut_get_type_name(obj_desc->common.type),
  445. obj_desc->common.type,
  446. obj_desc->common_field.bit_length,
  447. obj_desc->common_field.start_field_bit_offset,
  448. obj_desc->common_field.base_byte_offset));
  449. /* Lock entire transaction if requested */
  450. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  451. /* Write to the field */
  452. status = acpi_ex_insert_into_field(obj_desc, buffer, length);
  453. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  454. return_ACPI_STATUS(status);
  455. }