exfield.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /******************************************************************************
  2. *
  3. * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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. #define _COMPONENT ACPI_EXECUTER
  47. ACPI_MODULE_NAME("exfield")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ex_read_data_from_field
  51. *
  52. * PARAMETERS: walk_state - Current execution state
  53. * obj_desc - The named field
  54. * ret_buffer_desc - Where the return data object is stored
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Read from a named field. Returns either an Integer or a
  59. * Buffer, depending on the size of the field.
  60. *
  61. ******************************************************************************/
  62. acpi_status
  63. acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  64. union acpi_operand_object *obj_desc,
  65. union acpi_operand_object **ret_buffer_desc)
  66. {
  67. acpi_status status;
  68. union acpi_operand_object *buffer_desc;
  69. acpi_size length;
  70. void *buffer;
  71. u32 function;
  72. ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
  73. /* Parameter validation */
  74. if (!obj_desc) {
  75. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  76. }
  77. if (!ret_buffer_desc) {
  78. return_ACPI_STATUS(AE_BAD_PARAMETER);
  79. }
  80. if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
  81. /*
  82. * If the buffer_field arguments have not been previously evaluated,
  83. * evaluate them now and save the results.
  84. */
  85. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  86. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  87. if (ACPI_FAILURE(status)) {
  88. return_ACPI_STATUS(status);
  89. }
  90. }
  91. } else if ((obj_desc->common.type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
  92. (obj_desc->field.region_obj->region.space_id ==
  93. ACPI_ADR_SPACE_SMBUS
  94. || obj_desc->field.region_obj->region.space_id ==
  95. ACPI_ADR_SPACE_IPMI)) {
  96. /*
  97. * This is an SMBus or IPMI read. We must create a buffer to hold
  98. * the data and then directly access the region handler.
  99. *
  100. * Note: Smbus protocol value is passed in upper 16-bits of Function
  101. */
  102. if (obj_desc->field.region_obj->region.space_id ==
  103. ACPI_ADR_SPACE_SMBUS) {
  104. length = ACPI_SMBUS_BUFFER_SIZE;
  105. function =
  106. ACPI_READ | (obj_desc->field.attribute << 16);
  107. } else { /* IPMI */
  108. length = ACPI_IPMI_BUFFER_SIZE;
  109. function = ACPI_READ;
  110. }
  111. buffer_desc = acpi_ut_create_buffer_object(length);
  112. if (!buffer_desc) {
  113. return_ACPI_STATUS(AE_NO_MEMORY);
  114. }
  115. /* Lock entire transaction if requested */
  116. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  117. /* Call the region handler for the read */
  118. status = acpi_ex_access_region(obj_desc, 0,
  119. ACPI_CAST_PTR(u64,
  120. buffer_desc->
  121. buffer.pointer),
  122. function);
  123. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  124. goto exit;
  125. }
  126. /*
  127. * Allocate a buffer for the contents of the field.
  128. *
  129. * If the field is larger than the current integer width, create
  130. * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
  131. * the use of arithmetic operators on the returned value if the
  132. * field size is equal or smaller than an Integer.
  133. *
  134. * Note: Field.length is in bits.
  135. */
  136. length =
  137. (acpi_size) ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  138. if (length > acpi_gbl_integer_byte_width) {
  139. /* Field is too large for an Integer, create a Buffer instead */
  140. buffer_desc = acpi_ut_create_buffer_object(length);
  141. if (!buffer_desc) {
  142. return_ACPI_STATUS(AE_NO_MEMORY);
  143. }
  144. buffer = buffer_desc->buffer.pointer;
  145. } else {
  146. /* Field will fit within an Integer (normal case) */
  147. buffer_desc = acpi_ut_create_integer_object((u64) 0);
  148. if (!buffer_desc) {
  149. return_ACPI_STATUS(AE_NO_MEMORY);
  150. }
  151. length = acpi_gbl_integer_byte_width;
  152. buffer = &buffer_desc->integer.value;
  153. }
  154. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  155. "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
  156. obj_desc, obj_desc->common.type, buffer,
  157. (u32) length));
  158. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  159. "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
  160. obj_desc->common_field.bit_length,
  161. obj_desc->common_field.start_field_bit_offset,
  162. obj_desc->common_field.base_byte_offset));
  163. /* Lock entire transaction if requested */
  164. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  165. /* Read from the field */
  166. status = acpi_ex_extract_from_field(obj_desc, buffer, (u32) length);
  167. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  168. exit:
  169. if (ACPI_FAILURE(status)) {
  170. acpi_ut_remove_reference(buffer_desc);
  171. } else {
  172. *ret_buffer_desc = buffer_desc;
  173. }
  174. return_ACPI_STATUS(status);
  175. }
  176. /*******************************************************************************
  177. *
  178. * FUNCTION: acpi_ex_write_data_to_field
  179. *
  180. * PARAMETERS: source_desc - Contains data to write
  181. * obj_desc - The named field
  182. * result_desc - Where the return value is returned, if any
  183. *
  184. * RETURN: Status
  185. *
  186. * DESCRIPTION: Write to a named field
  187. *
  188. ******************************************************************************/
  189. acpi_status
  190. acpi_ex_write_data_to_field(union acpi_operand_object *source_desc,
  191. union acpi_operand_object *obj_desc,
  192. union acpi_operand_object **result_desc)
  193. {
  194. acpi_status status;
  195. u32 length;
  196. void *buffer;
  197. union acpi_operand_object *buffer_desc;
  198. u32 function;
  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_SMBUS
  218. || obj_desc->field.region_obj->region.space_id ==
  219. ACPI_ADR_SPACE_IPMI)) {
  220. /*
  221. * This is an SMBus or IPMI write. We will bypass the entire field
  222. * mechanism and handoff the buffer directly to the handler. For
  223. * these address spaces, the buffer is bi-directional; on a write,
  224. * return data is returned in the same buffer.
  225. *
  226. * Source must be a buffer of sufficient size:
  227. * ACPI_SMBUS_BUFFER_SIZE or ACPI_IPMI_BUFFER_SIZE.
  228. *
  229. * Note: SMBus protocol type is passed in upper 16-bits of Function
  230. */
  231. if (source_desc->common.type != ACPI_TYPE_BUFFER) {
  232. ACPI_ERROR((AE_INFO,
  233. "SMBus or IPMI write requires Buffer, found type %s",
  234. acpi_ut_get_object_type_name(source_desc)));
  235. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  236. }
  237. if (obj_desc->field.region_obj->region.space_id ==
  238. ACPI_ADR_SPACE_SMBUS) {
  239. length = ACPI_SMBUS_BUFFER_SIZE;
  240. function =
  241. ACPI_WRITE | (obj_desc->field.attribute << 16);
  242. } else { /* IPMI */
  243. length = ACPI_IPMI_BUFFER_SIZE;
  244. function = ACPI_WRITE;
  245. }
  246. if (source_desc->buffer.length < length) {
  247. ACPI_ERROR((AE_INFO,
  248. "SMBus or IPMI write requires Buffer of length %u, found length %u",
  249. length, source_desc->buffer.length));
  250. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  251. }
  252. /* Create the bi-directional buffer */
  253. buffer_desc = acpi_ut_create_buffer_object(length);
  254. if (!buffer_desc) {
  255. return_ACPI_STATUS(AE_NO_MEMORY);
  256. }
  257. buffer = buffer_desc->buffer.pointer;
  258. ACPI_MEMCPY(buffer, source_desc->buffer.pointer, length);
  259. /* Lock entire transaction if requested */
  260. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  261. /*
  262. * Perform the write (returns status and perhaps data in the
  263. * same buffer)
  264. */
  265. status = acpi_ex_access_region(obj_desc, 0,
  266. (u64 *) buffer, function);
  267. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  268. *result_desc = buffer_desc;
  269. return_ACPI_STATUS(status);
  270. }
  271. /* Get a pointer to the data to be written */
  272. switch (source_desc->common.type) {
  273. case ACPI_TYPE_INTEGER:
  274. buffer = &source_desc->integer.value;
  275. length = sizeof(source_desc->integer.value);
  276. break;
  277. case ACPI_TYPE_BUFFER:
  278. buffer = source_desc->buffer.pointer;
  279. length = source_desc->buffer.length;
  280. break;
  281. case ACPI_TYPE_STRING:
  282. buffer = source_desc->string.pointer;
  283. length = source_desc->string.length;
  284. break;
  285. default:
  286. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  287. }
  288. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  289. "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
  290. source_desc,
  291. acpi_ut_get_type_name(source_desc->common.type),
  292. source_desc->common.type, buffer, length));
  293. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  294. "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
  295. obj_desc,
  296. acpi_ut_get_type_name(obj_desc->common.type),
  297. obj_desc->common.type,
  298. obj_desc->common_field.bit_length,
  299. obj_desc->common_field.start_field_bit_offset,
  300. obj_desc->common_field.base_byte_offset));
  301. /* Lock entire transaction if requested */
  302. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  303. /* Write to the field */
  304. status = acpi_ex_insert_into_field(obj_desc, buffer, length);
  305. acpi_ex_release_global_lock(obj_desc->common_field.field_flags);
  306. return_ACPI_STATUS(status);
  307. }