exfldio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /******************************************************************************
  2. *
  3. * Module Name: exfldio - Aml Field I/O
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, 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/acevents.h>
  46. #include <acpi/acdispat.h>
  47. #define _COMPONENT ACPI_EXECUTER
  48. ACPI_MODULE_NAME("exfldio")
  49. /* Local prototypes */
  50. static acpi_status
  51. acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
  52. u32 field_datum_byte_offset,
  53. acpi_integer * value, u32 read_write);
  54. static u8
  55. acpi_ex_register_overflow(union acpi_operand_object *obj_desc,
  56. acpi_integer value);
  57. static acpi_status
  58. acpi_ex_setup_region(union acpi_operand_object *obj_desc,
  59. u32 field_datum_byte_offset);
  60. /*******************************************************************************
  61. *
  62. * FUNCTION: acpi_ex_setup_region
  63. *
  64. * PARAMETERS: obj_desc - Field to be read or written
  65. * field_datum_byte_offset - Byte offset of this datum within the
  66. * parent field
  67. *
  68. * RETURN: Status
  69. *
  70. * DESCRIPTION: Common processing for acpi_ex_extract_from_field and
  71. * acpi_ex_insert_into_field. Initialize the Region if necessary and
  72. * validate the request.
  73. *
  74. ******************************************************************************/
  75. static acpi_status
  76. acpi_ex_setup_region(union acpi_operand_object *obj_desc,
  77. u32 field_datum_byte_offset)
  78. {
  79. acpi_status status = AE_OK;
  80. union acpi_operand_object *rgn_desc;
  81. ACPI_FUNCTION_TRACE_U32("ex_setup_region", field_datum_byte_offset);
  82. rgn_desc = obj_desc->common_field.region_obj;
  83. /* We must have a valid region */
  84. if (ACPI_GET_OBJECT_TYPE(rgn_desc) != ACPI_TYPE_REGION) {
  85. ACPI_REPORT_ERROR(("Needed Region, found type %X (%s)\n",
  86. ACPI_GET_OBJECT_TYPE(rgn_desc),
  87. acpi_ut_get_object_type_name(rgn_desc)));
  88. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  89. }
  90. /*
  91. * If the Region Address and Length have not been previously evaluated,
  92. * evaluate them now and save the results.
  93. */
  94. if (!(rgn_desc->common.flags & AOPOBJ_DATA_VALID)) {
  95. status = acpi_ds_get_region_arguments(rgn_desc);
  96. if (ACPI_FAILURE(status)) {
  97. return_ACPI_STATUS(status);
  98. }
  99. }
  100. if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) {
  101. /* SMBus has a non-linear address space */
  102. return_ACPI_STATUS(AE_OK);
  103. }
  104. #ifdef ACPI_UNDER_DEVELOPMENT
  105. /*
  106. * If the Field access is any_acc, we can now compute the optimal
  107. * access (because we know know the length of the parent region)
  108. */
  109. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  110. if (ACPI_FAILURE(status)) {
  111. return_ACPI_STATUS(status);
  112. }
  113. }
  114. #endif
  115. /*
  116. * Validate the request. The entire request from the byte offset for a
  117. * length of one field datum (access width) must fit within the region.
  118. * (Region length is specified in bytes)
  119. */
  120. if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset +
  121. field_datum_byte_offset +
  122. obj_desc->common_field.
  123. access_byte_width)) {
  124. if (acpi_gbl_enable_interpreter_slack) {
  125. /*
  126. * Slack mode only: We will go ahead and allow access to this
  127. * field if it is within the region length rounded up to the next
  128. * access width boundary.
  129. */
  130. if (ACPI_ROUND_UP(rgn_desc->region.length,
  131. obj_desc->common_field.
  132. access_byte_width) >=
  133. (obj_desc->common_field.base_byte_offset +
  134. (acpi_native_uint) obj_desc->common_field.
  135. access_byte_width + field_datum_byte_offset)) {
  136. return_ACPI_STATUS(AE_OK);
  137. }
  138. }
  139. if (rgn_desc->region.length <
  140. obj_desc->common_field.access_byte_width) {
  141. /*
  142. * This is the case where the access_type (acc_word, etc.) is wider
  143. * than the region itself. For example, a region of length one
  144. * byte, and a field with Dword access specified.
  145. */
  146. ACPI_REPORT_ERROR(("Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n", acpi_ut_get_node_name(obj_desc->common_field.node), obj_desc->common_field.access_byte_width, acpi_ut_get_node_name(rgn_desc->region.node), rgn_desc->region.length));
  147. }
  148. /*
  149. * Offset rounded up to next multiple of field width
  150. * exceeds region length, indicate an error
  151. */
  152. ACPI_REPORT_ERROR(("Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n", acpi_ut_get_node_name(obj_desc->common_field.node), obj_desc->common_field.base_byte_offset, field_datum_byte_offset, obj_desc->common_field.access_byte_width, acpi_ut_get_node_name(rgn_desc->region.node), rgn_desc->region.length));
  153. return_ACPI_STATUS(AE_AML_REGION_LIMIT);
  154. }
  155. return_ACPI_STATUS(AE_OK);
  156. }
  157. /*******************************************************************************
  158. *
  159. * FUNCTION: acpi_ex_access_region
  160. *
  161. * PARAMETERS: obj_desc - Field to be read
  162. * field_datum_byte_offset - Byte offset of this datum within the
  163. * parent field
  164. * Value - Where to store value (must at least
  165. * the size of acpi_integer)
  166. * Function - Read or Write flag plus other region-
  167. * dependent flags
  168. *
  169. * RETURN: Status
  170. *
  171. * DESCRIPTION: Read or Write a single field datum to an Operation Region.
  172. *
  173. ******************************************************************************/
  174. acpi_status
  175. acpi_ex_access_region(union acpi_operand_object *obj_desc,
  176. u32 field_datum_byte_offset,
  177. acpi_integer * value, u32 function)
  178. {
  179. acpi_status status;
  180. union acpi_operand_object *rgn_desc;
  181. acpi_physical_address address;
  182. ACPI_FUNCTION_TRACE("ex_access_region");
  183. /*
  184. * Ensure that the region operands are fully evaluated and verify
  185. * the validity of the request
  186. */
  187. status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
  188. if (ACPI_FAILURE(status)) {
  189. return_ACPI_STATUS(status);
  190. }
  191. /*
  192. * The physical address of this field datum is:
  193. *
  194. * 1) The base of the region, plus
  195. * 2) The base offset of the field, plus
  196. * 3) The current offset into the field
  197. */
  198. rgn_desc = obj_desc->common_field.region_obj;
  199. address = rgn_desc->region.address +
  200. obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
  201. if ((function & ACPI_IO_MASK) == ACPI_READ) {
  202. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
  203. } else {
  204. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
  205. }
  206. ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
  207. " Region [%s:%X], Width %X, byte_base %X, Offset %X at %8.8X%8.8X\n",
  208. acpi_ut_get_region_name(rgn_desc->region.
  209. space_id),
  210. rgn_desc->region.space_id,
  211. obj_desc->common_field.access_byte_width,
  212. obj_desc->common_field.base_byte_offset,
  213. field_datum_byte_offset,
  214. ACPI_FORMAT_UINT64(address)));
  215. /* Invoke the appropriate address_space/op_region handler */
  216. status = acpi_ev_address_space_dispatch(rgn_desc, function,
  217. address,
  218. ACPI_MUL_8(obj_desc->
  219. common_field.
  220. access_byte_width),
  221. value);
  222. if (ACPI_FAILURE(status)) {
  223. if (status == AE_NOT_IMPLEMENTED) {
  224. ACPI_REPORT_ERROR(("Region %s(%X) not implemented\n",
  225. acpi_ut_get_region_name(rgn_desc->
  226. region.
  227. space_id),
  228. rgn_desc->region.space_id));
  229. } else if (status == AE_NOT_EXIST) {
  230. ACPI_REPORT_ERROR(("Region %s(%X) has no handler\n",
  231. acpi_ut_get_region_name(rgn_desc->
  232. region.
  233. space_id),
  234. rgn_desc->region.space_id));
  235. }
  236. }
  237. return_ACPI_STATUS(status);
  238. }
  239. /*******************************************************************************
  240. *
  241. * FUNCTION: acpi_ex_register_overflow
  242. *
  243. * PARAMETERS: obj_desc - Register(Field) to be written
  244. * Value - Value to be stored
  245. *
  246. * RETURN: TRUE if value overflows the field, FALSE otherwise
  247. *
  248. * DESCRIPTION: Check if a value is out of range of the field being written.
  249. * Used to check if the values written to Index and Bank registers
  250. * are out of range. Normally, the value is simply truncated
  251. * to fit the field, but this case is most likely a serious
  252. * coding error in the ASL.
  253. *
  254. ******************************************************************************/
  255. static u8
  256. acpi_ex_register_overflow(union acpi_operand_object *obj_desc,
  257. acpi_integer value)
  258. {
  259. if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
  260. /*
  261. * The field is large enough to hold the maximum integer, so we can
  262. * never overflow it.
  263. */
  264. return (FALSE);
  265. }
  266. if (value >= ((acpi_integer) 1 << obj_desc->common_field.bit_length)) {
  267. /*
  268. * The Value is larger than the maximum value that can fit into
  269. * the register.
  270. */
  271. return (TRUE);
  272. }
  273. /* The Value will fit into the field with no truncation */
  274. return (FALSE);
  275. }
  276. /*******************************************************************************
  277. *
  278. * FUNCTION: acpi_ex_field_datum_io
  279. *
  280. * PARAMETERS: obj_desc - Field to be read
  281. * field_datum_byte_offset - Byte offset of this datum within the
  282. * parent field
  283. * Value - Where to store value (must be 64 bits)
  284. * read_write - Read or Write flag
  285. *
  286. * RETURN: Status
  287. *
  288. * DESCRIPTION: Read or Write a single datum of a field. The field_type is
  289. * demultiplexed here to handle the different types of fields
  290. * (buffer_field, region_field, index_field, bank_field)
  291. *
  292. ******************************************************************************/
  293. static acpi_status
  294. acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
  295. u32 field_datum_byte_offset,
  296. acpi_integer * value, u32 read_write)
  297. {
  298. acpi_status status;
  299. acpi_integer local_value;
  300. ACPI_FUNCTION_TRACE_U32("ex_field_datum_io", field_datum_byte_offset);
  301. if (read_write == ACPI_READ) {
  302. if (!value) {
  303. local_value = 0;
  304. /* To support reads without saving return value */
  305. value = &local_value;
  306. }
  307. /* Clear the entire return buffer first, [Very Important!] */
  308. *value = 0;
  309. }
  310. /*
  311. * The four types of fields are:
  312. *
  313. * buffer_field - Read/write from/to a Buffer
  314. * region_field - Read/write from/to a Operation Region.
  315. * bank_field - Write to a Bank Register, then read/write from/to an
  316. * operation_region
  317. * index_field - Write to an Index Register, then read/write from/to a
  318. * Data Register
  319. */
  320. switch (ACPI_GET_OBJECT_TYPE(obj_desc)) {
  321. case ACPI_TYPE_BUFFER_FIELD:
  322. /*
  323. * If the buffer_field arguments have not been previously evaluated,
  324. * evaluate them now and save the results.
  325. */
  326. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  327. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  328. if (ACPI_FAILURE(status)) {
  329. return_ACPI_STATUS(status);
  330. }
  331. }
  332. if (read_write == ACPI_READ) {
  333. /*
  334. * Copy the data from the source buffer.
  335. * Length is the field width in bytes.
  336. */
  337. ACPI_MEMCPY(value,
  338. (obj_desc->buffer_field.buffer_obj)->buffer.
  339. pointer +
  340. obj_desc->buffer_field.base_byte_offset +
  341. field_datum_byte_offset,
  342. obj_desc->common_field.access_byte_width);
  343. } else {
  344. /*
  345. * Copy the data to the target buffer.
  346. * Length is the field width in bytes.
  347. */
  348. ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
  349. pointer +
  350. obj_desc->buffer_field.base_byte_offset +
  351. field_datum_byte_offset, value,
  352. obj_desc->common_field.access_byte_width);
  353. }
  354. status = AE_OK;
  355. break;
  356. case ACPI_TYPE_LOCAL_BANK_FIELD:
  357. /*
  358. * Ensure that the bank_value is not beyond the capacity of
  359. * the register
  360. */
  361. if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
  362. (acpi_integer) obj_desc->
  363. bank_field.value)) {
  364. return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
  365. }
  366. /*
  367. * For bank_fields, we must write the bank_value to the bank_register
  368. * (itself a region_field) before we can access the data.
  369. */
  370. status =
  371. acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
  372. &obj_desc->bank_field.value,
  373. sizeof(obj_desc->bank_field.
  374. value));
  375. if (ACPI_FAILURE(status)) {
  376. return_ACPI_STATUS(status);
  377. }
  378. /*
  379. * Now that the Bank has been selected, fall through to the
  380. * region_field case and write the datum to the Operation Region
  381. */
  382. /*lint -fallthrough */
  383. case ACPI_TYPE_LOCAL_REGION_FIELD:
  384. /*
  385. * For simple region_fields, we just directly access the owning
  386. * Operation Region.
  387. */
  388. status =
  389. acpi_ex_access_region(obj_desc, field_datum_byte_offset,
  390. value, read_write);
  391. break;
  392. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  393. /*
  394. * Ensure that the index_value is not beyond the capacity of
  395. * the register
  396. */
  397. if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
  398. (acpi_integer) obj_desc->
  399. index_field.value)) {
  400. return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
  401. }
  402. /* Write the index value to the index_register (itself a region_field) */
  403. field_datum_byte_offset += obj_desc->index_field.value;
  404. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  405. "Write to Index Register: Value %8.8X\n",
  406. field_datum_byte_offset));
  407. status =
  408. acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
  409. &field_datum_byte_offset,
  410. sizeof(field_datum_byte_offset));
  411. if (ACPI_FAILURE(status)) {
  412. return_ACPI_STATUS(status);
  413. }
  414. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  415. "I/O to Data Register: value_ptr %p\n",
  416. value));
  417. if (read_write == ACPI_READ) {
  418. /* Read the datum from the data_register */
  419. status =
  420. acpi_ex_extract_from_field(obj_desc->index_field.
  421. data_obj, value,
  422. sizeof(acpi_integer));
  423. } else {
  424. /* Write the datum to the data_register */
  425. status =
  426. acpi_ex_insert_into_field(obj_desc->index_field.
  427. data_obj, value,
  428. sizeof(acpi_integer));
  429. }
  430. break;
  431. default:
  432. ACPI_REPORT_ERROR(("Wrong object type in field I/O %X\n",
  433. ACPI_GET_OBJECT_TYPE(obj_desc)));
  434. status = AE_AML_INTERNAL;
  435. break;
  436. }
  437. if (ACPI_SUCCESS(status)) {
  438. if (read_write == ACPI_READ) {
  439. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  440. "Value Read %8.8X%8.8X, Width %d\n",
  441. ACPI_FORMAT_UINT64(*value),
  442. obj_desc->common_field.
  443. access_byte_width));
  444. } else {
  445. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  446. "Value Written %8.8X%8.8X, Width %d\n",
  447. ACPI_FORMAT_UINT64(*value),
  448. obj_desc->common_field.
  449. access_byte_width));
  450. }
  451. }
  452. return_ACPI_STATUS(status);
  453. }
  454. /*******************************************************************************
  455. *
  456. * FUNCTION: acpi_ex_write_with_update_rule
  457. *
  458. * PARAMETERS: obj_desc - Field to be written
  459. * Mask - bitmask within field datum
  460. * field_value - Value to write
  461. * field_datum_byte_offset - Offset of datum within field
  462. *
  463. * RETURN: Status
  464. *
  465. * DESCRIPTION: Apply the field update rule to a field write
  466. *
  467. ******************************************************************************/
  468. acpi_status
  469. acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
  470. acpi_integer mask,
  471. acpi_integer field_value,
  472. u32 field_datum_byte_offset)
  473. {
  474. acpi_status status = AE_OK;
  475. acpi_integer merged_value;
  476. acpi_integer current_value;
  477. ACPI_FUNCTION_TRACE_U32("ex_write_with_update_rule", mask);
  478. /* Start with the new bits */
  479. merged_value = field_value;
  480. /* If the mask is all ones, we don't need to worry about the update rule */
  481. if (mask != ACPI_INTEGER_MAX) {
  482. /* Decode the update rule */
  483. switch (obj_desc->common_field.
  484. field_flags & AML_FIELD_UPDATE_RULE_MASK) {
  485. case AML_FIELD_UPDATE_PRESERVE:
  486. /*
  487. * Check if update rule needs to be applied (not if mask is all
  488. * ones) The left shift drops the bits we want to ignore.
  489. */
  490. if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
  491. ACPI_MUL_8(obj_desc->common_field.
  492. access_byte_width))) != 0) {
  493. /*
  494. * Read the current contents of the byte/word/dword containing
  495. * the field, and merge with the new field value.
  496. */
  497. status =
  498. acpi_ex_field_datum_io(obj_desc,
  499. field_datum_byte_offset,
  500. &current_value,
  501. ACPI_READ);
  502. if (ACPI_FAILURE(status)) {
  503. return_ACPI_STATUS(status);
  504. }
  505. merged_value |= (current_value & ~mask);
  506. }
  507. break;
  508. case AML_FIELD_UPDATE_WRITE_AS_ONES:
  509. /* Set positions outside the field to all ones */
  510. merged_value |= ~mask;
  511. break;
  512. case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
  513. /* Set positions outside the field to all zeros */
  514. merged_value &= mask;
  515. break;
  516. default:
  517. ACPI_REPORT_ERROR(("Unknown update_rule value: %X\n",
  518. (obj_desc->common_field.
  519. field_flags &
  520. AML_FIELD_UPDATE_RULE_MASK)));
  521. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  522. }
  523. }
  524. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  525. "Mask %8.8X%8.8X, datum_offset %X, Width %X, Value %8.8X%8.8X, merged_value %8.8X%8.8X\n",
  526. ACPI_FORMAT_UINT64(mask),
  527. field_datum_byte_offset,
  528. obj_desc->common_field.access_byte_width,
  529. ACPI_FORMAT_UINT64(field_value),
  530. ACPI_FORMAT_UINT64(merged_value)));
  531. /* Write the merged value */
  532. status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
  533. &merged_value, ACPI_WRITE);
  534. return_ACPI_STATUS(status);
  535. }
  536. /*******************************************************************************
  537. *
  538. * FUNCTION: acpi_ex_extract_from_field
  539. *
  540. * PARAMETERS: obj_desc - Field to be read
  541. * Buffer - Where to store the field data
  542. * buffer_length - Length of Buffer
  543. *
  544. * RETURN: Status
  545. *
  546. * DESCRIPTION: Retrieve the current value of the given field
  547. *
  548. ******************************************************************************/
  549. acpi_status
  550. acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
  551. void *buffer, u32 buffer_length)
  552. {
  553. acpi_status status;
  554. acpi_integer raw_datum;
  555. acpi_integer merged_datum;
  556. u32 field_offset = 0;
  557. u32 buffer_offset = 0;
  558. u32 buffer_tail_bits;
  559. u32 datum_count;
  560. u32 field_datum_count;
  561. u32 i;
  562. ACPI_FUNCTION_TRACE("ex_extract_from_field");
  563. /* Validate target buffer and clear it */
  564. if (buffer_length <
  565. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
  566. ACPI_REPORT_ERROR(("Field size %X (bits) is too large for buffer (%X)\n", obj_desc->common_field.bit_length, buffer_length));
  567. return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
  568. }
  569. ACPI_MEMSET(buffer, 0, buffer_length);
  570. /* Compute the number of datums (access width data items) */
  571. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  572. obj_desc->common_field.access_bit_width);
  573. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  574. obj_desc->common_field.
  575. start_field_bit_offset,
  576. obj_desc->common_field.
  577. access_bit_width);
  578. /* Priming read from the field */
  579. status =
  580. acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
  581. ACPI_READ);
  582. if (ACPI_FAILURE(status)) {
  583. return_ACPI_STATUS(status);
  584. }
  585. merged_datum =
  586. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  587. /* Read the rest of the field */
  588. for (i = 1; i < field_datum_count; i++) {
  589. /* Get next input datum from the field */
  590. field_offset += obj_desc->common_field.access_byte_width;
  591. status = acpi_ex_field_datum_io(obj_desc, field_offset,
  592. &raw_datum, ACPI_READ);
  593. if (ACPI_FAILURE(status)) {
  594. return_ACPI_STATUS(status);
  595. }
  596. /* Merge with previous datum if necessary */
  597. merged_datum |= raw_datum <<
  598. (obj_desc->common_field.access_bit_width -
  599. obj_desc->common_field.start_field_bit_offset);
  600. if (i == datum_count) {
  601. break;
  602. }
  603. /* Write merged datum to target buffer */
  604. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  605. ACPI_MIN(obj_desc->common_field.access_byte_width,
  606. buffer_length - buffer_offset));
  607. buffer_offset += obj_desc->common_field.access_byte_width;
  608. merged_datum =
  609. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  610. }
  611. /* Mask off any extra bits in the last datum */
  612. buffer_tail_bits = obj_desc->common_field.bit_length %
  613. obj_desc->common_field.access_bit_width;
  614. if (buffer_tail_bits) {
  615. merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  616. }
  617. /* Write the last datum to the buffer */
  618. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  619. ACPI_MIN(obj_desc->common_field.access_byte_width,
  620. buffer_length - buffer_offset));
  621. return_ACPI_STATUS(AE_OK);
  622. }
  623. /*******************************************************************************
  624. *
  625. * FUNCTION: acpi_ex_insert_into_field
  626. *
  627. * PARAMETERS: obj_desc - Field to be written
  628. * Buffer - Data to be written
  629. * buffer_length - Length of Buffer
  630. *
  631. * RETURN: Status
  632. *
  633. * DESCRIPTION: Store the Buffer contents into the given field
  634. *
  635. ******************************************************************************/
  636. acpi_status
  637. acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
  638. void *buffer, u32 buffer_length)
  639. {
  640. acpi_status status;
  641. acpi_integer mask;
  642. acpi_integer merged_datum;
  643. acpi_integer raw_datum = 0;
  644. u32 field_offset = 0;
  645. u32 buffer_offset = 0;
  646. u32 buffer_tail_bits;
  647. u32 datum_count;
  648. u32 field_datum_count;
  649. u32 i;
  650. ACPI_FUNCTION_TRACE("ex_insert_into_field");
  651. /* Validate input buffer */
  652. if (buffer_length <
  653. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
  654. ACPI_REPORT_ERROR(("Field size %X (bits) is too large for buffer (%X)\n", obj_desc->common_field.bit_length, buffer_length));
  655. return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
  656. }
  657. /* Compute the number of datums (access width data items) */
  658. mask =
  659. ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
  660. datum_count =
  661. ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  662. obj_desc->common_field.access_bit_width);
  663. field_datum_count =
  664. ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  665. obj_desc->common_field.start_field_bit_offset,
  666. obj_desc->common_field.access_bit_width);
  667. /* Get initial Datum from the input buffer */
  668. ACPI_MEMCPY(&raw_datum, buffer,
  669. ACPI_MIN(obj_desc->common_field.access_byte_width,
  670. buffer_length - buffer_offset));
  671. merged_datum =
  672. raw_datum << obj_desc->common_field.start_field_bit_offset;
  673. /* Write the entire field */
  674. for (i = 1; i < field_datum_count; i++) {
  675. /* Write merged datum to the target field */
  676. merged_datum &= mask;
  677. status = acpi_ex_write_with_update_rule(obj_desc, mask,
  678. merged_datum,
  679. field_offset);
  680. if (ACPI_FAILURE(status)) {
  681. return_ACPI_STATUS(status);
  682. }
  683. /* Start new output datum by merging with previous input datum */
  684. field_offset += obj_desc->common_field.access_byte_width;
  685. merged_datum = raw_datum >>
  686. (obj_desc->common_field.access_bit_width -
  687. obj_desc->common_field.start_field_bit_offset);
  688. mask = ACPI_INTEGER_MAX;
  689. if (i == datum_count) {
  690. break;
  691. }
  692. /* Get the next input datum from the buffer */
  693. buffer_offset += obj_desc->common_field.access_byte_width;
  694. ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
  695. ACPI_MIN(obj_desc->common_field.access_byte_width,
  696. buffer_length - buffer_offset));
  697. merged_datum |=
  698. raw_datum << obj_desc->common_field.start_field_bit_offset;
  699. }
  700. /* Mask off any extra bits in the last datum */
  701. buffer_tail_bits = (obj_desc->common_field.bit_length +
  702. obj_desc->common_field.start_field_bit_offset) %
  703. obj_desc->common_field.access_bit_width;
  704. if (buffer_tail_bits) {
  705. mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  706. }
  707. /* Write the last datum to the field */
  708. merged_datum &= mask;
  709. status = acpi_ex_write_with_update_rule(obj_desc,
  710. mask, merged_datum,
  711. field_offset);
  712. return_ACPI_STATUS(status);
  713. }