exprep.c 19 KB

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