exfldio.c 27 KB

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