exprep.c 18 KB

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