exfldio.c 27 KB

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