exfield.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /******************************************************************************
  2. *
  3. * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <acpi/acdispat.h>
  44. #include <acpi/acinterp.h>
  45. #define _COMPONENT ACPI_EXECUTER
  46. ACPI_MODULE_NAME("exfield")
  47. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ex_read_data_from_field
  50. *
  51. * PARAMETERS: walk_state - Current execution state
  52. * obj_desc - The named field
  53. * ret_buffer_desc - Where the return data object is stored
  54. *
  55. * RETURN: Status
  56. *
  57. * DESCRIPTION: Read from a named field. Returns either an Integer or a
  58. * Buffer, depending on the size of the field.
  59. *
  60. ******************************************************************************/
  61. acpi_status
  62. acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
  63. union acpi_operand_object *obj_desc,
  64. union acpi_operand_object **ret_buffer_desc)
  65. {
  66. acpi_status status;
  67. union acpi_operand_object *buffer_desc;
  68. acpi_size length;
  69. void *buffer;
  70. u8 locked;
  71. ACPI_FUNCTION_TRACE_PTR("ex_read_data_from_field", obj_desc);
  72. /* Parameter validation */
  73. if (!obj_desc) {
  74. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  75. }
  76. if (!ret_buffer_desc) {
  77. return_ACPI_STATUS(AE_BAD_PARAMETER);
  78. }
  79. if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_BUFFER_FIELD) {
  80. /*
  81. * If the buffer_field arguments have not been previously evaluated,
  82. * evaluate them now and save the results.
  83. */
  84. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  85. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  86. if (ACPI_FAILURE(status)) {
  87. return_ACPI_STATUS(status);
  88. }
  89. }
  90. } else
  91. if ((ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_LOCAL_REGION_FIELD)
  92. && (obj_desc->field.region_obj->region.space_id ==
  93. ACPI_ADR_SPACE_SMBUS)) {
  94. /*
  95. * This is an SMBus read. We must create a buffer to hold the data
  96. * and directly access the region handler.
  97. */
  98. buffer_desc =
  99. acpi_ut_create_buffer_object(ACPI_SMBUS_BUFFER_SIZE);
  100. if (!buffer_desc) {
  101. return_ACPI_STATUS(AE_NO_MEMORY);
  102. }
  103. /* Lock entire transaction if requested */
  104. locked =
  105. acpi_ex_acquire_global_lock(obj_desc->common_field.
  106. field_flags);
  107. /*
  108. * Perform the read.
  109. * Note: Smbus protocol value is passed in upper 16-bits of Function
  110. */
  111. status = acpi_ex_access_region(obj_desc, 0,
  112. ACPI_CAST_PTR(acpi_integer,
  113. buffer_desc->
  114. buffer.pointer),
  115. ACPI_READ | (obj_desc->field.
  116. attribute << 16));
  117. acpi_ex_release_global_lock(locked);
  118. goto exit;
  119. }
  120. /*
  121. * Allocate a buffer for the contents of the field.
  122. *
  123. * If the field is larger than the size of an acpi_integer, create
  124. * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
  125. * the use of arithmetic operators on the returned value if the
  126. * field size is equal or smaller than an Integer.
  127. *
  128. * Note: Field.length is in bits.
  129. */
  130. length =
  131. (acpi_size) ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
  132. if (length > acpi_gbl_integer_byte_width) {
  133. /* Field is too large for an Integer, create a Buffer instead */
  134. buffer_desc = acpi_ut_create_buffer_object(length);
  135. if (!buffer_desc) {
  136. return_ACPI_STATUS(AE_NO_MEMORY);
  137. }
  138. buffer = buffer_desc->buffer.pointer;
  139. } else {
  140. /* Field will fit within an Integer (normal case) */
  141. buffer_desc = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
  142. if (!buffer_desc) {
  143. return_ACPI_STATUS(AE_NO_MEMORY);
  144. }
  145. length = acpi_gbl_integer_byte_width;
  146. buffer_desc->integer.value = 0;
  147. buffer = &buffer_desc->integer.value;
  148. }
  149. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  150. "field_read [TO]: Obj %p, Type %X, Buf %p, byte_len %X\n",
  151. obj_desc, ACPI_GET_OBJECT_TYPE(obj_desc), buffer,
  152. (u32) length));
  153. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  154. "field_read [FROM]: bit_len %X, bit_off %X, byte_off %X\n",
  155. obj_desc->common_field.bit_length,
  156. obj_desc->common_field.start_field_bit_offset,
  157. obj_desc->common_field.base_byte_offset));
  158. /* Lock entire transaction if requested */
  159. locked =
  160. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  161. /* Read from the field */
  162. status = acpi_ex_extract_from_field(obj_desc, buffer, (u32) length);
  163. acpi_ex_release_global_lock(locked);
  164. exit:
  165. if (ACPI_FAILURE(status)) {
  166. acpi_ut_remove_reference(buffer_desc);
  167. } else {
  168. *ret_buffer_desc = buffer_desc;
  169. }
  170. return_ACPI_STATUS(status);
  171. }
  172. /*******************************************************************************
  173. *
  174. * FUNCTION: acpi_ex_write_data_to_field
  175. *
  176. * PARAMETERS: source_desc - Contains data to write
  177. * obj_desc - The named field
  178. * result_desc - Where the return value is returned, if any
  179. *
  180. * RETURN: Status
  181. *
  182. * DESCRIPTION: Write to a named field
  183. *
  184. ******************************************************************************/
  185. acpi_status
  186. acpi_ex_write_data_to_field(union acpi_operand_object *source_desc,
  187. union acpi_operand_object *obj_desc,
  188. union acpi_operand_object **result_desc)
  189. {
  190. acpi_status status;
  191. u32 length;
  192. u32 required_length;
  193. void *buffer;
  194. void *new_buffer;
  195. u8 locked;
  196. union acpi_operand_object *buffer_desc;
  197. ACPI_FUNCTION_TRACE_PTR("ex_write_data_to_field", obj_desc);
  198. /* Parameter validation */
  199. if (!source_desc || !obj_desc) {
  200. return_ACPI_STATUS(AE_AML_NO_OPERAND);
  201. }
  202. if (ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_BUFFER_FIELD) {
  203. /*
  204. * If the buffer_field arguments have not been previously evaluated,
  205. * evaluate them now and save the results.
  206. */
  207. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  208. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  209. if (ACPI_FAILURE(status)) {
  210. return_ACPI_STATUS(status);
  211. }
  212. }
  213. } else
  214. if ((ACPI_GET_OBJECT_TYPE(obj_desc) == ACPI_TYPE_LOCAL_REGION_FIELD)
  215. && (obj_desc->field.region_obj->region.space_id ==
  216. ACPI_ADR_SPACE_SMBUS)) {
  217. /*
  218. * This is an SMBus write. We will bypass the entire field mechanism
  219. * and handoff the buffer directly to the handler.
  220. *
  221. * Source must be a buffer of sufficient size (ACPI_SMBUS_BUFFER_SIZE).
  222. */
  223. if (ACPI_GET_OBJECT_TYPE(source_desc) != ACPI_TYPE_BUFFER) {
  224. ACPI_REPORT_ERROR(("SMBus write requires Buffer, found type %s\n", acpi_ut_get_object_type_name(source_desc)));
  225. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  226. }
  227. if (source_desc->buffer.length < ACPI_SMBUS_BUFFER_SIZE) {
  228. ACPI_REPORT_ERROR(("SMBus write requires Buffer of length %X, found length %X\n", ACPI_SMBUS_BUFFER_SIZE, source_desc->buffer.length));
  229. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  230. }
  231. buffer_desc =
  232. acpi_ut_create_buffer_object(ACPI_SMBUS_BUFFER_SIZE);
  233. if (!buffer_desc) {
  234. return_ACPI_STATUS(AE_NO_MEMORY);
  235. }
  236. buffer = buffer_desc->buffer.pointer;
  237. ACPI_MEMCPY(buffer, source_desc->buffer.pointer,
  238. ACPI_SMBUS_BUFFER_SIZE);
  239. /* Lock entire transaction if requested */
  240. locked =
  241. acpi_ex_acquire_global_lock(obj_desc->common_field.
  242. field_flags);
  243. /*
  244. * Perform the write (returns status and perhaps data in the
  245. * same buffer)
  246. * Note: SMBus protocol type is passed in upper 16-bits of Function.
  247. */
  248. status = acpi_ex_access_region(obj_desc, 0,
  249. (acpi_integer *) buffer,
  250. ACPI_WRITE | (obj_desc->field.
  251. attribute << 16));
  252. acpi_ex_release_global_lock(locked);
  253. *result_desc = buffer_desc;
  254. return_ACPI_STATUS(status);
  255. }
  256. /* Get a pointer to the data to be written */
  257. switch (ACPI_GET_OBJECT_TYPE(source_desc)) {
  258. case ACPI_TYPE_INTEGER:
  259. buffer = &source_desc->integer.value;
  260. length = sizeof(source_desc->integer.value);
  261. break;
  262. case ACPI_TYPE_BUFFER:
  263. buffer = source_desc->buffer.pointer;
  264. length = source_desc->buffer.length;
  265. break;
  266. case ACPI_TYPE_STRING:
  267. buffer = source_desc->string.pointer;
  268. length = source_desc->string.length;
  269. break;
  270. default:
  271. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  272. }
  273. /*
  274. * We must have a buffer that is at least as long as the field
  275. * we are writing to. This is because individual fields are
  276. * indivisible and partial writes are not supported -- as per
  277. * the ACPI specification.
  278. */
  279. new_buffer = NULL;
  280. required_length =
  281. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  282. if (length < required_length) {
  283. /* We need to create a new buffer */
  284. new_buffer = ACPI_MEM_CALLOCATE(required_length);
  285. if (!new_buffer) {
  286. return_ACPI_STATUS(AE_NO_MEMORY);
  287. }
  288. /*
  289. * Copy the original data to the new buffer, starting
  290. * at Byte zero. All unused (upper) bytes of the
  291. * buffer will be 0.
  292. */
  293. ACPI_MEMCPY((char *)new_buffer, (char *)buffer, length);
  294. buffer = new_buffer;
  295. length = required_length;
  296. }
  297. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  298. "field_write [FROM]: Obj %p (%s:%X), Buf %p, byte_len %X\n",
  299. source_desc,
  300. acpi_ut_get_type_name(ACPI_GET_OBJECT_TYPE
  301. (source_desc)),
  302. ACPI_GET_OBJECT_TYPE(source_desc), buffer, length));
  303. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  304. "field_write [TO]: Obj %p (%s:%X), bit_len %X, bit_off %X, byte_off %X\n",
  305. obj_desc,
  306. acpi_ut_get_type_name(ACPI_GET_OBJECT_TYPE(obj_desc)),
  307. ACPI_GET_OBJECT_TYPE(obj_desc),
  308. obj_desc->common_field.bit_length,
  309. obj_desc->common_field.start_field_bit_offset,
  310. obj_desc->common_field.base_byte_offset));
  311. /* Lock entire transaction if requested */
  312. locked =
  313. acpi_ex_acquire_global_lock(obj_desc->common_field.field_flags);
  314. /* Write to the field */
  315. status = acpi_ex_insert_into_field(obj_desc, buffer, length);
  316. acpi_ex_release_global_lock(locked);
  317. /* Free temporary buffer if we used one */
  318. if (new_buffer) {
  319. ACPI_MEM_FREE(new_buffer);
  320. }
  321. return_ACPI_STATUS(status);
  322. }