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