exfield.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /******************************************************************************
  2. *
  3. * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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 hold
  148. * the data and then directly access the region handler.
  149. *
  150. * Note: SMBus and GSBus protocol value is passed in upper 16-bits of Function
  151. */
  152. if (obj_desc->field.region_obj->region.space_id ==
  153. ACPI_ADR_SPACE_SMBUS) {
  154. length = ACPI_SMBUS_BUFFER_SIZE;
  155. function =
  156. ACPI_READ | (obj_desc->field.attribute << 16);
  157. } else if (obj_desc->field.region_obj->region.space_id ==
  158. ACPI_ADR_SPACE_GSBUS) {
  159. accessor_type = obj_desc->field.attribute;
  160. length = acpi_ex_get_serial_access_length(accessor_type,
  161. obj_desc->
  162. field.
  163. access_length);
  164. /*
  165. * Add additional 2 bytes for the generic_serial_bus data buffer:
  166. *
  167. * Status; (Byte 0 of the data buffer)
  168. * Length; (Byte 1 of the data buffer)
  169. * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
  170. */
  171. length += 2;
  172. function = ACPI_READ | (accessor_type << 16);
  173. } else { /* IPMI */
  174. length = ACPI_IPMI_BUFFER_SIZE;
  175. function = ACPI_READ;
  176. }
  177. buffer_desc = acpi_ut_create_buffer_object(length);
  178. if (!buffer_desc) {
  179. return_ACPI_STATUS(AE_NO_MEMORY);
  180. }
  181. /* Lock entire transaction if requested */
  182. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  183. /* Call the region handler for the read */
  184. status = acpi_ex_access_region(obj_desc, 0,
  185. ACPI_CAST_PTR(u64,
  186. buffer_desc->
  187. buffer.pointer),
  188. function);
  189. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  190. goto exit;
  191. }
  192. /*
  193. * Allocate a buffer for the contents of the field.
  194. *
  195. * If the field is larger than the current integer width, create
  196. * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
  197. * the use of arithmetic operators on the returned value if the
  198. * field size is equal or smaller than an Integer.
  199. *
  200. * Note: Field.length is in bits.
  201. */
  202. length =
  203. (acpi_size) ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  204. if (length > acpi_gbl_integer_byte_width) {
  205. /* Field is too large for an Integer, create a Buffer instead */
  206. buffer_desc = acpi_ut_create_buffer_object(length);
  207. if (!buffer_desc) {
  208. return_ACPI_STATUS(AE_NO_MEMORY);
  209. }
  210. buffer = buffer_desc->buffer.pointer;
  211. } else {
  212. /* Field will fit within an Integer (normal case) */
  213. buffer_desc = acpi_ut_create_integer_object((u64) 0);
  214. if (!buffer_desc) {
  215. return_ACPI_STATUS(AE_NO_MEMORY);
  216. }
  217. length = acpi_gbl_integer_byte_width;
  218. buffer = &buffer_desc->integer.value;
  219. }
  220. if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  221. (obj_desc->field.region_obj->region.space_id ==
  222. ACPI_ADR_SPACE_GPIO)) {
  223. /*
  224. * For GPIO (general_purpose_io), the Address will be the bit offset
  225. * from the previous Connection() operator, making it effectively a
  226. * pin number index. The bit_length is the length of the field, which
  227. * is thus the number of pins.
  228. */
  229. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  230. "GPIO FieldRead [FROM]: Pin %u Bits %u\n",
  231. obj_desc->field.pin_number_index,
  232. obj_desc->field.bit_length));
  233. /* Lock entire transaction if requested */
  234. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  235. /* Perform the write */
  236. status = acpi_ex_access_region(obj_desc, 0,
  237. (u64 *)buffer, ACPI_READ);
  238. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  239. if (ACPI_FAILURE(status)) {
  240. acpi_ut_remove_reference(buffer_desc);
  241. } else {
  242. *ret_buffer_desc = buffer_desc;
  243. }
  244. return_ACPI_STATUS(status);
  245. }
  246. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  247. "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
  248. obj_desc, obj_desc->common.type, buffer,
  249. (u32) length));
  250. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  251. "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
  252. obj_desc->common_field.bit_length,
  253. obj_desc->common_field.start_field_bit_offset,
  254. obj_desc->common_field.base_byte_offset));
  255. /* Lock entire transaction if requested */
  256. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  257. /* Read from the field */
  258. status = acpi_ex_extract_from_field(obj_desc, buffer, (u32) length);
  259. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  260. exit:
  261. if (ACPI_FAILURE(status)) {
  262. acpi_ut_remove_reference(buffer_desc);
  263. } else {
  264. *ret_buffer_desc = buffer_desc;
  265. }
  266. return_ACPI_STATUS(status);
  267. }
  268. /*******************************************************************************
  269. *
  270. * FUNCTION: acpi_ex_write_data_to_field
  271. *
  272. * PARAMETERS: source_desc - Contains data to write
  273. * obj_desc - The named field
  274. * result_desc - Where the return value is returned, if any
  275. *
  276. * RETURN: Status
  277. *
  278. * DESCRIPTION: Write to a named field
  279. *
  280. ******************************************************************************/
  281. acpi_status
  282. acpi_ex_write_data_to_field(union acpi_operand_object *source_desc,
  283. union acpi_operand_object *obj_desc,
  284. union acpi_operand_object **result_desc)
  285. {
  286. acpi_status status;
  287. u32 length;
  288. void *buffer;
  289. union acpi_operand_object *buffer_desc;
  290. u32 function;
  291. u16 accessor_type;
  292. ACPI_FUNCTION_TRACE_PTR(ex_write_data_to_field, obj_desc);
  293. /* Parameter validation */
  294. if (!source_desc || !obj_desc) {
  295. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  296. }
  297. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  298. /*
  299. * If the buffer_field arguments have not been previously evaluated,
  300. * evaluate them now and save the results.
  301. */
  302. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  303. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  304. if (ACPI_FAILURE(status)) {
  305. return_ACPI_STATUS(status);
  306. }
  307. }
  308. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  309. (obj_desc->field.region_obj->region.space_id ==
  310. ACPI_ADR_SPACE_SMBUS
  311. || obj_desc->field.region_obj->region.space_id ==
  312. ACPI_ADR_SPACE_GSBUS
  313. || obj_desc->field.region_obj->region.space_id ==
  314. ACPI_ADR_SPACE_IPMI)) {
  315. /*
  316. * This is an SMBus, GSBus or IPMI write. We will bypass the entire field
  317. * mechanism and handoff the buffer directly to the handler. For
  318. * these address spaces, the buffer is bi-directional; on a write,
  319. * return data is returned in the same buffer.
  320. *
  321. * Source must be a buffer of sufficient size:
  322. * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
  323. *
  324. * Note: SMBus and GSBus protocol type is passed in upper 16-bits of Function
  325. */
  326. if (source_desc->common.type != ACPI_TYPE_BUFFER) {
  327. ACPI_ERROR((AE_INFO,
  328. "SMBus/IPMI/GenericSerialBus write requires Buffer, found type %s",
  329. acpi_ut_get_object_type_name(source_desc)));
  330. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  331. }
  332. if (obj_desc->field.region_obj->region.space_id ==
  333. ACPI_ADR_SPACE_SMBUS) {
  334. length = ACPI_SMBUS_BUFFER_SIZE;
  335. function =
  336. ACPI_WRITE | (obj_desc->field.attribute << 16);
  337. } else if (obj_desc->field.region_obj->region.space_id ==
  338. ACPI_ADR_SPACE_GSBUS) {
  339. accessor_type = obj_desc->field.attribute;
  340. length = acpi_ex_get_serial_access_length(accessor_type,
  341. obj_desc->
  342. field.
  343. access_length);
  344. /*
  345. * Add additional 2 bytes for the generic_serial_bus data buffer:
  346. *
  347. * Status; (Byte 0 of the data buffer)
  348. * Length; (Byte 1 of the data buffer)
  349. * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
  350. */
  351. length += 2;
  352. function = ACPI_WRITE | (accessor_type << 16);
  353. } else { /* IPMI */
  354. length = ACPI_IPMI_BUFFER_SIZE;
  355. function = ACPI_WRITE;
  356. }
  357. if (source_desc->buffer.length < length) {
  358. ACPI_ERROR((AE_INFO,
  359. "SMBus/IPMI/GenericSerialBus write requires Buffer of length %u, found length %u",
  360. length, source_desc->buffer.length));
  361. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  362. }
  363. /* Create the bi-directional buffer */
  364. buffer_desc = acpi_ut_create_buffer_object(length);
  365. if (!buffer_desc) {
  366. return_ACPI_STATUS(AE_NO_MEMORY);
  367. }
  368. buffer = buffer_desc->buffer.pointer;
  369. memcpy(buffer, source_desc->buffer.pointer, length);
  370. /* Lock entire transaction if requested */
  371. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  372. /*
  373. * Perform the write (returns status and perhaps data in the
  374. * same buffer)
  375. */
  376. status = acpi_ex_access_region(obj_desc, 0,
  377. (u64 *) buffer, function);
  378. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  379. *result_desc = buffer_desc;
  380. return_ACPI_STATUS(status);
  381. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  382. (obj_desc->field.region_obj->region.space_id ==
  383. ACPI_ADR_SPACE_GPIO)) {
  384. /*
  385. * For GPIO (general_purpose_io), we will bypass the entire field
  386. * mechanism and handoff the bit address and bit width directly to
  387. * the handler. The Address will be the bit offset
  388. * from the previous Connection() operator, making it effectively a
  389. * pin number index. The bit_length is the length of the field, which
  390. * is thus the number of pins.
  391. */
  392. if (source_desc->common.type != ACPI_TYPE_INTEGER) {
  393. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  394. }
  395. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  396. "GPIO FieldWrite [FROM]: (%s:%X), Val %.8X [TO]: Pin %u Bits %u\n",
  397. acpi_ut_get_type_name(source_desc->common.
  398. type),
  399. source_desc->common.type,
  400. (u32)source_desc->integer.value,
  401. obj_desc->field.pin_number_index,
  402. obj_desc->field.bit_length));
  403. buffer = &source_desc->integer.value;
  404. /* Lock entire transaction if requested */
  405. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  406. /* Perform the write */
  407. status = acpi_ex_access_region(obj_desc, 0,
  408. (u64 *)buffer, ACPI_WRITE);
  409. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  410. return_ACPI_STATUS(status);
  411. }
  412. /* Get a pointer to the data to be written */
  413. switch (source_desc->common.type) {
  414. case ACPI_TYPE_INTEGER:
  415. buffer = &source_desc->integer.value;
  416. length = sizeof(source_desc->integer.value);
  417. break;
  418. case ACPI_TYPE_BUFFER:
  419. buffer = source_desc->buffer.pointer;
  420. length = source_desc->buffer.length;
  421. break;
  422. case ACPI_TYPE_STRING:
  423. buffer = source_desc->string.pointer;
  424. length = source_desc->string.length;
  425. break;
  426. default:
  427. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  428. }
  429. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  430. "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
  431. source_desc,
  432. acpi_ut_get_type_name(source_desc->common.type),
  433. source_desc->common.type, buffer, length));
  434. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  435. "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
  436. obj_desc,
  437. acpi_ut_get_type_name(obj_desc->common.type),
  438. obj_desc->common.type,
  439. obj_desc->common_field.bit_length,
  440. obj_desc->common_field.start_field_bit_offset,
  441. obj_desc->common_field.base_byte_offset));
  442. /* Lock entire transaction if requested */
  443. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  444. /* Write to the field */
  445. status = acpi_ex_insert_into_field(obj_desc, buffer, length);
  446. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  447. return_ACPI_STATUS(status);
  448. }