exfldio.c 26 KB

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