exfldio.c 26 KB

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