exprep.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /******************************************************************************
  2. *
  3. * Module Name: exprep - ACPI AML (p-code) execution - field prep utilities
  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/acinterp.h>
  44. #include <acpi/amlcode.h>
  45. #include <acpi/acnamesp.h>
  46. #define _COMPONENT ACPI_EXECUTER
  47. ACPI_MODULE_NAME ("exprep")
  48. #ifdef ACPI_UNDER_DEVELOPMENT
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ex_generate_access
  52. *
  53. * PARAMETERS: field_bit_offset - Start of field within parent region/buffer
  54. * field_bit_length - Length of field in bits
  55. * region_length - Length of parent in bytes
  56. *
  57. * RETURN: Field granularity (8, 16, 32 or 64) and
  58. * byte_alignment (1, 2, 3, or 4)
  59. *
  60. * DESCRIPTION: Generate an optimal access width for fields defined with the
  61. * any_acc keyword.
  62. *
  63. * NOTE: Need to have the region_length in order to check for boundary
  64. * conditions (end-of-region). However, the region_length is a deferred
  65. * operation. Therefore, to complete this implementation, the generation
  66. * of this access width must be deferred until the region length has
  67. * been evaluated.
  68. *
  69. ******************************************************************************/
  70. static u32
  71. acpi_ex_generate_access (
  72. u32 field_bit_offset,
  73. u32 field_bit_length,
  74. u32 region_length)
  75. {
  76. u32 field_byte_length;
  77. u32 field_byte_offset;
  78. u32 field_byte_end_offset;
  79. u32 access_byte_width;
  80. u32 field_start_offset;
  81. u32 field_end_offset;
  82. u32 minimum_access_width = 0xFFFFFFFF;
  83. u32 minimum_accesses = 0xFFFFFFFF;
  84. u32 accesses;
  85. ACPI_FUNCTION_TRACE ("ex_generate_access");
  86. /* Round Field start offset and length to "minimal" byte boundaries */
  87. field_byte_offset = ACPI_DIV_8 (ACPI_ROUND_DOWN (field_bit_offset, 8));
  88. field_byte_end_offset = ACPI_DIV_8 (ACPI_ROUND_UP (field_bit_length + field_bit_offset, 8));
  89. field_byte_length = field_byte_end_offset - field_byte_offset;
  90. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  91. "Bit length %d, Bit offset %d\n",
  92. field_bit_length, field_bit_offset));
  93. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  94. "Byte Length %d, Byte Offset %d, End Offset %d\n",
  95. field_byte_length, field_byte_offset, field_byte_end_offset));
  96. /*
  97. * Iterative search for the maximum access width that is both aligned
  98. * and does not go beyond the end of the region
  99. *
  100. * Start at byte_acc and work upwards to qword_acc max. (1,2,4,8 bytes)
  101. */
  102. for (access_byte_width = 1; access_byte_width <= 8; access_byte_width <<= 1) {
  103. /*
  104. * 1) Round end offset up to next access boundary and make sure that this
  105. * does not go beyond the end of the parent region.
  106. * 2) When the Access width is greater than the field_byte_length, we are done.
  107. * (This does not optimize for the perfectly aligned case yet).
  108. */
  109. if (ACPI_ROUND_UP (field_byte_end_offset, access_byte_width) <= region_length) {
  110. field_start_offset = ACPI_ROUND_DOWN (field_byte_offset, access_byte_width) /
  111. access_byte_width;
  112. field_end_offset = ACPI_ROUND_UP ((field_byte_length + field_byte_offset),
  113. access_byte_width) / access_byte_width;
  114. accesses = field_end_offset - field_start_offset;
  115. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  116. "access_width %d end is within region\n", access_byte_width));
  117. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  118. "Field Start %d, Field End %d -- requires %d accesses\n",
  119. field_start_offset, field_end_offset, accesses));
  120. /* Single access is optimal */
  121. if (accesses <= 1) {
  122. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  123. "Entire field can be accessed with one operation of size %d\n",
  124. access_byte_width));
  125. return_VALUE (access_byte_width);
  126. }
  127. /*
  128. * Fits in the region, but requires more than one read/write.
  129. * try the next wider access on next iteration
  130. */
  131. if (accesses < minimum_accesses) {
  132. minimum_accesses = accesses;
  133. minimum_access_width = access_byte_width;
  134. }
  135. }
  136. else {
  137. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  138. "access_width %d end is NOT within region\n", access_byte_width));
  139. if (access_byte_width == 1) {
  140. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  141. "Field goes beyond end-of-region!\n"));
  142. return_VALUE (0); /* Field does not fit in the region at all */
  143. }
  144. /* This width goes beyond the end-of-region, back off to previous access */
  145. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  146. "Backing off to previous optimal access width of %d\n",
  147. minimum_access_width));
  148. return_VALUE (minimum_access_width);
  149. }
  150. }
  151. /* Could not read/write field with one operation, just use max access width */
  152. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  153. "Cannot access field in one operation, using width 8\n"));
  154. return_VALUE (8);
  155. }
  156. #endif /* ACPI_UNDER_DEVELOPMENT */
  157. /*******************************************************************************
  158. *
  159. * FUNCTION: acpi_ex_decode_field_access
  160. *
  161. * PARAMETERS: Access - Encoded field access bits
  162. * Length - Field length.
  163. *
  164. * RETURN: Field granularity (8, 16, 32 or 64) and
  165. * byte_alignment (1, 2, 3, or 4)
  166. *
  167. * DESCRIPTION: Decode the access_type bits of a field definition.
  168. *
  169. ******************************************************************************/
  170. static u32
  171. acpi_ex_decode_field_access (
  172. union acpi_operand_object *obj_desc,
  173. u8 field_flags,
  174. u32 *return_byte_alignment)
  175. {
  176. u32 access;
  177. u32 byte_alignment;
  178. u32 bit_length;
  179. ACPI_FUNCTION_TRACE ("ex_decode_field_access");
  180. access = (field_flags & AML_FIELD_ACCESS_TYPE_MASK);
  181. switch (access) {
  182. case AML_FIELD_ACCESS_ANY:
  183. #ifdef ACPI_UNDER_DEVELOPMENT
  184. byte_alignment = acpi_ex_generate_access (obj_desc->common_field.start_field_bit_offset,
  185. obj_desc->common_field.bit_length,
  186. 0xFFFFFFFF /* Temp until we pass region_length as param */);
  187. bit_length = byte_alignment * 8;
  188. #endif
  189. byte_alignment = 1;
  190. bit_length = 8;
  191. break;
  192. case AML_FIELD_ACCESS_BYTE:
  193. case AML_FIELD_ACCESS_BUFFER: /* ACPI 2.0 (SMBus Buffer) */
  194. byte_alignment = 1;
  195. bit_length = 8;
  196. break;
  197. case AML_FIELD_ACCESS_WORD:
  198. byte_alignment = 2;
  199. bit_length = 16;
  200. break;
  201. case AML_FIELD_ACCESS_DWORD:
  202. byte_alignment = 4;
  203. bit_length = 32;
  204. break;
  205. case AML_FIELD_ACCESS_QWORD: /* ACPI 2.0 */
  206. byte_alignment = 8;
  207. bit_length = 64;
  208. break;
  209. default:
  210. /* Invalid field access type */
  211. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  212. "Unknown field access type %X\n",
  213. access));
  214. return_VALUE (0);
  215. }
  216. if (ACPI_GET_OBJECT_TYPE (obj_desc) == ACPI_TYPE_BUFFER_FIELD) {
  217. /*
  218. * buffer_field access can be on any byte boundary, so the
  219. * byte_alignment is always 1 byte -- regardless of any byte_alignment
  220. * implied by the field access type.
  221. */
  222. byte_alignment = 1;
  223. }
  224. *return_byte_alignment = byte_alignment;
  225. return_VALUE (bit_length);
  226. }
  227. /*******************************************************************************
  228. *
  229. * FUNCTION: acpi_ex_prep_common_field_object
  230. *
  231. * PARAMETERS: obj_desc - The field object
  232. * field_flags - Access, lock_rule, and update_rule.
  233. * The format of a field_flag is described
  234. * in the ACPI specification
  235. * field_bit_position - Field start position
  236. * field_bit_length - Field length in number of bits
  237. *
  238. * RETURN: Status
  239. *
  240. * DESCRIPTION: Initialize the areas of the field object that are common
  241. * to the various types of fields. Note: This is very "sensitive"
  242. * code because we are solving the general case for field
  243. * alignment.
  244. *
  245. ******************************************************************************/
  246. acpi_status
  247. acpi_ex_prep_common_field_object (
  248. union acpi_operand_object *obj_desc,
  249. u8 field_flags,
  250. u8 field_attribute,
  251. u32 field_bit_position,
  252. u32 field_bit_length)
  253. {
  254. u32 access_bit_width;
  255. u32 byte_alignment;
  256. u32 nearest_byte_address;
  257. ACPI_FUNCTION_TRACE ("ex_prep_common_field_object");
  258. /*
  259. * Note: the structure being initialized is the
  260. * ACPI_COMMON_FIELD_INFO; No structure fields outside of the common
  261. * area are initialized by this procedure.
  262. */
  263. obj_desc->common_field.field_flags = field_flags;
  264. obj_desc->common_field.attribute = field_attribute;
  265. obj_desc->common_field.bit_length = field_bit_length;
  266. /*
  267. * Decode the access type so we can compute offsets. The access type gives
  268. * two pieces of information - the width of each field access and the
  269. * necessary byte_alignment (address granularity) of the access.
  270. *
  271. * For any_acc, the access_bit_width is the largest width that is both
  272. * necessary and possible in an attempt to access the whole field in one
  273. * I/O operation. However, for any_acc, the byte_alignment is always one
  274. * byte.
  275. *
  276. * For all Buffer Fields, the byte_alignment is always one byte.
  277. *
  278. * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is
  279. * the same (equivalent) as the byte_alignment.
  280. */
  281. access_bit_width = acpi_ex_decode_field_access (obj_desc, field_flags,
  282. &byte_alignment);
  283. if (!access_bit_width) {
  284. return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
  285. }
  286. /* Setup width (access granularity) fields */
  287. obj_desc->common_field.access_byte_width = (u8)
  288. ACPI_DIV_8 (access_bit_width); /* 1, 2, 4, 8 */
  289. obj_desc->common_field.access_bit_width = (u8) access_bit_width;
  290. /*
  291. * base_byte_offset is the address of the start of the field within the
  292. * region. It is the byte address of the first *datum* (field-width data
  293. * unit) of the field. (i.e., the first datum that contains at least the
  294. * first *bit* of the field.)
  295. *
  296. * Note: byte_alignment is always either equal to the access_bit_width or 8
  297. * (Byte access), and it defines the addressing granularity of the parent
  298. * region or buffer.
  299. */
  300. nearest_byte_address =
  301. ACPI_ROUND_BITS_DOWN_TO_BYTES (field_bit_position);
  302. obj_desc->common_field.base_byte_offset = (u32)
  303. ACPI_ROUND_DOWN (nearest_byte_address, byte_alignment);
  304. /*
  305. * start_field_bit_offset is the offset of the first bit of the field within
  306. * a field datum.
  307. */
  308. obj_desc->common_field.start_field_bit_offset = (u8)
  309. (field_bit_position - ACPI_MUL_8 (obj_desc->common_field.base_byte_offset));
  310. /*
  311. * Does the entire field fit within a single field access element? (datum)
  312. * (i.e., without crossing a datum boundary)
  313. */
  314. if ((obj_desc->common_field.start_field_bit_offset + field_bit_length) <=
  315. (u16) access_bit_width) {
  316. obj_desc->common.flags |= AOPOBJ_SINGLE_DATUM;
  317. }
  318. return_ACPI_STATUS (AE_OK);
  319. }
  320. /*******************************************************************************
  321. *
  322. * FUNCTION: acpi_ex_prep_field_value
  323. *
  324. * PARAMETERS: Node - Owning Node
  325. * region_node - Region in which field is being defined
  326. * field_flags - Access, lock_rule, and update_rule.
  327. * field_bit_position - Field start position
  328. * field_bit_length - Field length in number of bits
  329. *
  330. * RETURN: Status
  331. *
  332. * DESCRIPTION: Construct an union acpi_operand_object of type def_field and
  333. * connect it to the parent Node.
  334. *
  335. ******************************************************************************/
  336. acpi_status
  337. acpi_ex_prep_field_value (
  338. struct acpi_create_field_info *info)
  339. {
  340. union acpi_operand_object *obj_desc;
  341. u32 type;
  342. acpi_status status;
  343. ACPI_FUNCTION_TRACE ("ex_prep_field_value");
  344. /* Parameter validation */
  345. if (info->field_type != ACPI_TYPE_LOCAL_INDEX_FIELD) {
  346. if (!info->region_node) {
  347. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null region_node\n"));
  348. return_ACPI_STATUS (AE_AML_NO_OPERAND);
  349. }
  350. type = acpi_ns_get_type (info->region_node);
  351. if (type != ACPI_TYPE_REGION) {
  352. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  353. "Needed Region, found type %X (%s)\n",
  354. type, acpi_ut_get_type_name (type)));
  355. return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
  356. }
  357. }
  358. /* Allocate a new field object */
  359. obj_desc = acpi_ut_create_internal_object (info->field_type);
  360. if (!obj_desc) {
  361. return_ACPI_STATUS (AE_NO_MEMORY);
  362. }
  363. /* Initialize areas of the object that are common to all fields */
  364. obj_desc->common_field.node = info->field_node;
  365. status = acpi_ex_prep_common_field_object (obj_desc, info->field_flags,
  366. info->attribute, info->field_bit_position, info->field_bit_length);
  367. if (ACPI_FAILURE (status)) {
  368. acpi_ut_delete_object_desc (obj_desc);
  369. return_ACPI_STATUS (status);
  370. }
  371. /* Initialize areas of the object that are specific to the field type */
  372. switch (info->field_type) {
  373. case ACPI_TYPE_LOCAL_REGION_FIELD:
  374. obj_desc->field.region_obj = acpi_ns_get_attached_object (info->region_node);
  375. /* An additional reference for the container */
  376. acpi_ut_add_reference (obj_desc->field.region_obj);
  377. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  378. "region_field: bit_off %X, Off %X, Gran %X, Region %p\n",
  379. obj_desc->field.start_field_bit_offset, obj_desc->field.base_byte_offset,
  380. obj_desc->field.access_byte_width, obj_desc->field.region_obj));
  381. break;
  382. case ACPI_TYPE_LOCAL_BANK_FIELD:
  383. obj_desc->bank_field.value = info->bank_value;
  384. obj_desc->bank_field.region_obj = acpi_ns_get_attached_object (info->region_node);
  385. obj_desc->bank_field.bank_obj = acpi_ns_get_attached_object (info->register_node);
  386. /* An additional reference for the attached objects */
  387. acpi_ut_add_reference (obj_desc->bank_field.region_obj);
  388. acpi_ut_add_reference (obj_desc->bank_field.bank_obj);
  389. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  390. "Bank Field: bit_off %X, Off %X, Gran %X, Region %p, bank_reg %p\n",
  391. obj_desc->bank_field.start_field_bit_offset,
  392. obj_desc->bank_field.base_byte_offset,
  393. obj_desc->field.access_byte_width,
  394. obj_desc->bank_field.region_obj,
  395. obj_desc->bank_field.bank_obj));
  396. break;
  397. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  398. obj_desc->index_field.index_obj = acpi_ns_get_attached_object (info->register_node);
  399. obj_desc->index_field.data_obj = acpi_ns_get_attached_object (info->data_register_node);
  400. obj_desc->index_field.value = (u32)
  401. (info->field_bit_position / ACPI_MUL_8 (obj_desc->field.access_byte_width));
  402. if (!obj_desc->index_field.data_obj || !obj_desc->index_field.index_obj) {
  403. ACPI_REPORT_ERROR (("Null Index Object during field prep\n"));
  404. acpi_ut_delete_object_desc (obj_desc);
  405. return_ACPI_STATUS (AE_AML_INTERNAL);
  406. }
  407. /* An additional reference for the attached objects */
  408. acpi_ut_add_reference (obj_desc->index_field.data_obj);
  409. acpi_ut_add_reference (obj_desc->index_field.index_obj);
  410. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
  411. "index_field: bit_off %X, Off %X, Value %X, Gran %X, Index %p, Data %p\n",
  412. obj_desc->index_field.start_field_bit_offset,
  413. obj_desc->index_field.base_byte_offset,
  414. obj_desc->index_field.value,
  415. obj_desc->field.access_byte_width,
  416. obj_desc->index_field.index_obj,
  417. obj_desc->index_field.data_obj));
  418. break;
  419. default:
  420. /* No other types should get here */
  421. break;
  422. }
  423. /*
  424. * Store the constructed descriptor (obj_desc) into the parent Node,
  425. * preserving the current type of that named_obj.
  426. */
  427. status = acpi_ns_attach_object (info->field_node, obj_desc,
  428. acpi_ns_get_type (info->field_node));
  429. ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Set named_obj %p [%4.4s], obj_desc %p\n",
  430. info->field_node, acpi_ut_get_node_name (info->field_node), obj_desc));
  431. /* Remove local reference to the object */
  432. acpi_ut_remove_reference (obj_desc);
  433. return_ACPI_STATUS (status);
  434. }