exfldio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /******************************************************************************
  2. *
  3. * Module Name: exfldio - Aml Field I/O
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2008, Intel Corp.
  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 "accommon.h"
  44. #include "acinterp.h"
  45. #include "amlcode.h"
  46. #include "acevents.h"
  47. #include "acdispat.h"
  48. #define _COMPONENT ACPI_EXECUTER
  49. ACPI_MODULE_NAME("exfldio")
  50. /* Local prototypes */
  51. static acpi_status
  52. acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
  53. u32 field_datum_byte_offset,
  54. acpi_integer * value, u32 read_write);
  55. static u8
  56. acpi_ex_register_overflow(union acpi_operand_object *obj_desc,
  57. acpi_integer value);
  58. static acpi_status
  59. acpi_ex_setup_region(union acpi_operand_object *obj_desc,
  60. u32 field_datum_byte_offset);
  61. /*******************************************************************************
  62. *
  63. * FUNCTION: acpi_ex_setup_region
  64. *
  65. * PARAMETERS: obj_desc - Field to be read or written
  66. * field_datum_byte_offset - Byte offset of this datum within the
  67. * parent field
  68. *
  69. * RETURN: Status
  70. *
  71. * DESCRIPTION: Common processing for acpi_ex_extract_from_field and
  72. * acpi_ex_insert_into_field. Initialize the Region if necessary and
  73. * validate the request.
  74. *
  75. ******************************************************************************/
  76. static acpi_status
  77. acpi_ex_setup_region(union acpi_operand_object *obj_desc,
  78. u32 field_datum_byte_offset)
  79. {
  80. acpi_status status = AE_OK;
  81. union acpi_operand_object *rgn_desc;
  82. ACPI_FUNCTION_TRACE_U32(ex_setup_region, field_datum_byte_offset);
  83. rgn_desc = obj_desc->common_field.region_obj;
  84. /* We must have a valid region */
  85. if (rgn_desc->common.type != ACPI_TYPE_REGION) {
  86. ACPI_ERROR((AE_INFO, "Needed Region, found type %X (%s)",
  87. rgn_desc->common.type,
  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. /*
  102. * Exit now for SMBus address space, it has a non-linear address space
  103. * and the request cannot be directly validated
  104. */
  105. if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) {
  106. /* SMBus has a non-linear address space */
  107. return_ACPI_STATUS(AE_OK);
  108. }
  109. #ifdef ACPI_UNDER_DEVELOPMENT
  110. /*
  111. * If the Field access is any_acc, we can now compute the optimal
  112. * access (because we know know the length of the parent region)
  113. */
  114. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  115. if (ACPI_FAILURE(status)) {
  116. return_ACPI_STATUS(status);
  117. }
  118. }
  119. #endif
  120. /*
  121. * Validate the request. The entire request from the byte offset for a
  122. * length of one field datum (access width) must fit within the region.
  123. * (Region length is specified in bytes)
  124. */
  125. if (rgn_desc->region.length <
  126. (obj_desc->common_field.base_byte_offset +
  127. field_datum_byte_offset +
  128. obj_desc->common_field.access_byte_width)) {
  129. if (acpi_gbl_enable_interpreter_slack) {
  130. /*
  131. * Slack mode only: We will go ahead and allow access to this
  132. * field if it is within the region length rounded up to the next
  133. * access width boundary. acpi_size cast for 64-bit compile.
  134. */
  135. if (ACPI_ROUND_UP(rgn_desc->region.length,
  136. obj_desc->common_field.
  137. access_byte_width) >=
  138. ((acpi_size) obj_desc->common_field.
  139. base_byte_offset +
  140. obj_desc->common_field.access_byte_width +
  141. field_datum_byte_offset)) {
  142. return_ACPI_STATUS(AE_OK);
  143. }
  144. }
  145. if (rgn_desc->region.length <
  146. obj_desc->common_field.access_byte_width) {
  147. /*
  148. * This is the case where the access_type (acc_word, etc.) is wider
  149. * than the region itself. For example, a region of length one
  150. * byte, and a field with Dword access specified.
  151. */
  152. ACPI_ERROR((AE_INFO,
  153. "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)",
  154. acpi_ut_get_node_name(obj_desc->
  155. common_field.node),
  156. obj_desc->common_field.access_byte_width,
  157. acpi_ut_get_node_name(rgn_desc->region.
  158. node),
  159. rgn_desc->region.length));
  160. }
  161. /*
  162. * Offset rounded up to next multiple of field width
  163. * exceeds region length, indicate an error
  164. */
  165. ACPI_ERROR((AE_INFO,
  166. "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)",
  167. acpi_ut_get_node_name(obj_desc->common_field.node),
  168. obj_desc->common_field.base_byte_offset,
  169. field_datum_byte_offset,
  170. obj_desc->common_field.access_byte_width,
  171. acpi_ut_get_node_name(rgn_desc->region.node),
  172. rgn_desc->region.length));
  173. return_ACPI_STATUS(AE_AML_REGION_LIMIT);
  174. }
  175. return_ACPI_STATUS(AE_OK);
  176. }
  177. /*******************************************************************************
  178. *
  179. * FUNCTION: acpi_ex_access_region
  180. *
  181. * PARAMETERS: obj_desc - Field to be read
  182. * field_datum_byte_offset - Byte offset of this datum within the
  183. * parent field
  184. * Value - Where to store value (must at least
  185. * the size of acpi_integer)
  186. * Function - Read or Write flag plus other region-
  187. * dependent flags
  188. *
  189. * RETURN: Status
  190. *
  191. * DESCRIPTION: Read or Write a single field datum to an Operation Region.
  192. *
  193. ******************************************************************************/
  194. acpi_status
  195. acpi_ex_access_region(union acpi_operand_object *obj_desc,
  196. u32 field_datum_byte_offset,
  197. acpi_integer * value, u32 function)
  198. {
  199. acpi_status status;
  200. union acpi_operand_object *rgn_desc;
  201. u32 region_offset;
  202. ACPI_FUNCTION_TRACE(ex_access_region);
  203. /*
  204. * Ensure that the region operands are fully evaluated and verify
  205. * the validity of the request
  206. */
  207. status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
  208. if (ACPI_FAILURE(status)) {
  209. return_ACPI_STATUS(status);
  210. }
  211. /*
  212. * The physical address of this field datum is:
  213. *
  214. * 1) The base of the region, plus
  215. * 2) The base offset of the field, plus
  216. * 3) The current offset into the field
  217. */
  218. rgn_desc = obj_desc->common_field.region_obj;
  219. region_offset =
  220. obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
  221. if ((function & ACPI_IO_MASK) == ACPI_READ) {
  222. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
  223. } else {
  224. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
  225. }
  226. ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
  227. " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
  228. acpi_ut_get_region_name(rgn_desc->region.
  229. space_id),
  230. rgn_desc->region.space_id,
  231. obj_desc->common_field.access_byte_width,
  232. obj_desc->common_field.base_byte_offset,
  233. field_datum_byte_offset, ACPI_CAST_PTR(void,
  234. (rgn_desc->
  235. region.
  236. address +
  237. region_offset))));
  238. /* Invoke the appropriate address_space/op_region handler */
  239. status =
  240. acpi_ev_address_space_dispatch(rgn_desc, function, region_offset,
  241. ACPI_MUL_8(obj_desc->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 (obj_desc->common.type) {
  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. if (read_write == ACPI_READ) {
  437. /* Read the datum from the data_register */
  438. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  439. "Read from Data Register\n"));
  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. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  447. "Write to Data Register: Value %8.8X%8.8X\n",
  448. ACPI_FORMAT_UINT64(*value)));
  449. status =
  450. acpi_ex_insert_into_field(obj_desc->index_field.
  451. data_obj, value,
  452. sizeof(acpi_integer));
  453. }
  454. break;
  455. default:
  456. ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %X",
  457. obj_desc->common.type));
  458. status = AE_AML_INTERNAL;
  459. break;
  460. }
  461. if (ACPI_SUCCESS(status)) {
  462. if (read_write == ACPI_READ) {
  463. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  464. "Value Read %8.8X%8.8X, Width %d\n",
  465. ACPI_FORMAT_UINT64(*value),
  466. obj_desc->common_field.
  467. access_byte_width));
  468. } else {
  469. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  470. "Value Written %8.8X%8.8X, Width %d\n",
  471. ACPI_FORMAT_UINT64(*value),
  472. obj_desc->common_field.
  473. access_byte_width));
  474. }
  475. }
  476. return_ACPI_STATUS(status);
  477. }
  478. /*******************************************************************************
  479. *
  480. * FUNCTION: acpi_ex_write_with_update_rule
  481. *
  482. * PARAMETERS: obj_desc - Field to be written
  483. * Mask - bitmask within field datum
  484. * field_value - Value to write
  485. * field_datum_byte_offset - Offset of datum within field
  486. *
  487. * RETURN: Status
  488. *
  489. * DESCRIPTION: Apply the field update rule to a field write
  490. *
  491. ******************************************************************************/
  492. acpi_status
  493. acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
  494. acpi_integer mask,
  495. acpi_integer field_value,
  496. u32 field_datum_byte_offset)
  497. {
  498. acpi_status status = AE_OK;
  499. acpi_integer merged_value;
  500. acpi_integer current_value;
  501. ACPI_FUNCTION_TRACE_U32(ex_write_with_update_rule, mask);
  502. /* Start with the new bits */
  503. merged_value = field_value;
  504. /* If the mask is all ones, we don't need to worry about the update rule */
  505. if (mask != ACPI_INTEGER_MAX) {
  506. /* Decode the update rule */
  507. switch (obj_desc->common_field.
  508. field_flags & AML_FIELD_UPDATE_RULE_MASK) {
  509. case AML_FIELD_UPDATE_PRESERVE:
  510. /*
  511. * Check if update rule needs to be applied (not if mask is all
  512. * ones) The left shift drops the bits we want to ignore.
  513. */
  514. if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
  515. ACPI_MUL_8(obj_desc->common_field.
  516. access_byte_width))) != 0) {
  517. /*
  518. * Read the current contents of the byte/word/dword containing
  519. * the field, and merge with the new field value.
  520. */
  521. status =
  522. acpi_ex_field_datum_io(obj_desc,
  523. field_datum_byte_offset,
  524. &current_value,
  525. ACPI_READ);
  526. if (ACPI_FAILURE(status)) {
  527. return_ACPI_STATUS(status);
  528. }
  529. merged_value |= (current_value & ~mask);
  530. }
  531. break;
  532. case AML_FIELD_UPDATE_WRITE_AS_ONES:
  533. /* Set positions outside the field to all ones */
  534. merged_value |= ~mask;
  535. break;
  536. case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
  537. /* Set positions outside the field to all zeros */
  538. merged_value &= mask;
  539. break;
  540. default:
  541. ACPI_ERROR((AE_INFO,
  542. "Unknown UpdateRule value: %X",
  543. (obj_desc->common_field.
  544. field_flags &
  545. AML_FIELD_UPDATE_RULE_MASK)));
  546. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  547. }
  548. }
  549. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  550. "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
  551. ACPI_FORMAT_UINT64(mask),
  552. field_datum_byte_offset,
  553. obj_desc->common_field.access_byte_width,
  554. ACPI_FORMAT_UINT64(field_value),
  555. ACPI_FORMAT_UINT64(merged_value)));
  556. /* Write the merged value */
  557. status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
  558. &merged_value, ACPI_WRITE);
  559. return_ACPI_STATUS(status);
  560. }
  561. /*******************************************************************************
  562. *
  563. * FUNCTION: acpi_ex_extract_from_field
  564. *
  565. * PARAMETERS: obj_desc - Field to be read
  566. * Buffer - Where to store the field data
  567. * buffer_length - Length of Buffer
  568. *
  569. * RETURN: Status
  570. *
  571. * DESCRIPTION: Retrieve the current value of the given field
  572. *
  573. ******************************************************************************/
  574. acpi_status
  575. acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
  576. void *buffer, u32 buffer_length)
  577. {
  578. acpi_status status;
  579. acpi_integer raw_datum;
  580. acpi_integer merged_datum;
  581. u32 field_offset = 0;
  582. u32 buffer_offset = 0;
  583. u32 buffer_tail_bits;
  584. u32 datum_count;
  585. u32 field_datum_count;
  586. u32 i;
  587. ACPI_FUNCTION_TRACE(ex_extract_from_field);
  588. /* Validate target buffer and clear it */
  589. if (buffer_length <
  590. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
  591. ACPI_ERROR((AE_INFO,
  592. "Field size %X (bits) is too large for buffer (%X)",
  593. obj_desc->common_field.bit_length, buffer_length));
  594. return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
  595. }
  596. ACPI_MEMSET(buffer, 0, buffer_length);
  597. /* Compute the number of datums (access width data items) */
  598. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  599. obj_desc->common_field.access_bit_width);
  600. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  601. obj_desc->common_field.
  602. start_field_bit_offset,
  603. obj_desc->common_field.
  604. access_bit_width);
  605. /* Priming read from the field */
  606. status =
  607. acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
  608. ACPI_READ);
  609. if (ACPI_FAILURE(status)) {
  610. return_ACPI_STATUS(status);
  611. }
  612. merged_datum =
  613. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  614. /* Read the rest of the field */
  615. for (i = 1; i < field_datum_count; i++) {
  616. /* Get next input datum from the field */
  617. field_offset += obj_desc->common_field.access_byte_width;
  618. status = acpi_ex_field_datum_io(obj_desc, field_offset,
  619. &raw_datum, ACPI_READ);
  620. if (ACPI_FAILURE(status)) {
  621. return_ACPI_STATUS(status);
  622. }
  623. /*
  624. * Merge with previous datum if necessary.
  625. *
  626. * Note: Before the shift, check if the shift value will be larger than
  627. * the integer size. If so, there is no need to perform the operation.
  628. * This avoids the differences in behavior between different compilers
  629. * concerning shift values larger than the target data width.
  630. */
  631. if ((obj_desc->common_field.access_bit_width -
  632. obj_desc->common_field.start_field_bit_offset) <
  633. ACPI_INTEGER_BIT_SIZE) {
  634. merged_datum |=
  635. raw_datum << (obj_desc->common_field.
  636. access_bit_width -
  637. obj_desc->common_field.
  638. start_field_bit_offset);
  639. }
  640. if (i == datum_count) {
  641. break;
  642. }
  643. /* Write merged datum to target buffer */
  644. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  645. ACPI_MIN(obj_desc->common_field.access_byte_width,
  646. buffer_length - buffer_offset));
  647. buffer_offset += obj_desc->common_field.access_byte_width;
  648. merged_datum =
  649. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  650. }
  651. /* Mask off any extra bits in the last datum */
  652. buffer_tail_bits = obj_desc->common_field.bit_length %
  653. obj_desc->common_field.access_bit_width;
  654. if (buffer_tail_bits) {
  655. merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  656. }
  657. /* Write the last datum to the buffer */
  658. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  659. ACPI_MIN(obj_desc->common_field.access_byte_width,
  660. buffer_length - buffer_offset));
  661. return_ACPI_STATUS(AE_OK);
  662. }
  663. /*******************************************************************************
  664. *
  665. * FUNCTION: acpi_ex_insert_into_field
  666. *
  667. * PARAMETERS: obj_desc - Field to be written
  668. * Buffer - Data to be written
  669. * buffer_length - Length of Buffer
  670. *
  671. * RETURN: Status
  672. *
  673. * DESCRIPTION: Store the Buffer contents into the given field
  674. *
  675. ******************************************************************************/
  676. acpi_status
  677. acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
  678. void *buffer, u32 buffer_length)
  679. {
  680. acpi_status status;
  681. acpi_integer mask;
  682. acpi_integer width_mask;
  683. acpi_integer merged_datum;
  684. acpi_integer raw_datum = 0;
  685. u32 field_offset = 0;
  686. u32 buffer_offset = 0;
  687. u32 buffer_tail_bits;
  688. u32 datum_count;
  689. u32 field_datum_count;
  690. u32 i;
  691. u32 required_length;
  692. void *new_buffer;
  693. ACPI_FUNCTION_TRACE(ex_insert_into_field);
  694. /* Validate input buffer */
  695. new_buffer = NULL;
  696. required_length =
  697. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  698. /*
  699. * We must have a buffer that is at least as long as the field
  700. * we are writing to. This is because individual fields are
  701. * indivisible and partial writes are not supported -- as per
  702. * the ACPI specification.
  703. */
  704. if (buffer_length < required_length) {
  705. /* We need to create a new buffer */
  706. new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
  707. if (!new_buffer) {
  708. return_ACPI_STATUS(AE_NO_MEMORY);
  709. }
  710. /*
  711. * Copy the original data to the new buffer, starting
  712. * at Byte zero. All unused (upper) bytes of the
  713. * buffer will be 0.
  714. */
  715. ACPI_MEMCPY((char *)new_buffer, (char *)buffer, buffer_length);
  716. buffer = new_buffer;
  717. buffer_length = required_length;
  718. }
  719. /*
  720. * Create the bitmasks used for bit insertion.
  721. * Note: This if/else is used to bypass compiler differences with the
  722. * shift operator
  723. */
  724. if (obj_desc->common_field.access_bit_width == ACPI_INTEGER_BIT_SIZE) {
  725. width_mask = ACPI_INTEGER_MAX;
  726. } else {
  727. width_mask =
  728. ACPI_MASK_BITS_ABOVE(obj_desc->common_field.
  729. access_bit_width);
  730. }
  731. mask = width_mask &
  732. ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
  733. /* Compute the number of datums (access width data items) */
  734. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  735. obj_desc->common_field.access_bit_width);
  736. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  737. obj_desc->common_field.
  738. start_field_bit_offset,
  739. obj_desc->common_field.
  740. access_bit_width);
  741. /* Get initial Datum from the input buffer */
  742. ACPI_MEMCPY(&raw_datum, buffer,
  743. ACPI_MIN(obj_desc->common_field.access_byte_width,
  744. buffer_length - buffer_offset));
  745. merged_datum =
  746. raw_datum << obj_desc->common_field.start_field_bit_offset;
  747. /* Write the entire field */
  748. for (i = 1; i < field_datum_count; i++) {
  749. /* Write merged datum to the target field */
  750. merged_datum &= mask;
  751. status = acpi_ex_write_with_update_rule(obj_desc, mask,
  752. merged_datum,
  753. field_offset);
  754. if (ACPI_FAILURE(status)) {
  755. goto exit;
  756. }
  757. field_offset += obj_desc->common_field.access_byte_width;
  758. /*
  759. * Start new output datum by merging with previous input datum
  760. * if necessary.
  761. *
  762. * Note: Before the shift, check if the shift value will be larger than
  763. * the integer size. If so, there is no need to perform the operation.
  764. * This avoids the differences in behavior between different compilers
  765. * concerning shift values larger than the target data width.
  766. */
  767. if ((obj_desc->common_field.access_bit_width -
  768. obj_desc->common_field.start_field_bit_offset) <
  769. ACPI_INTEGER_BIT_SIZE) {
  770. merged_datum =
  771. raw_datum >> (obj_desc->common_field.
  772. access_bit_width -
  773. obj_desc->common_field.
  774. start_field_bit_offset);
  775. } else {
  776. merged_datum = 0;
  777. }
  778. mask = width_mask;
  779. if (i == datum_count) {
  780. break;
  781. }
  782. /* Get the next input datum from the buffer */
  783. buffer_offset += obj_desc->common_field.access_byte_width;
  784. ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
  785. ACPI_MIN(obj_desc->common_field.access_byte_width,
  786. buffer_length - buffer_offset));
  787. merged_datum |=
  788. raw_datum << obj_desc->common_field.start_field_bit_offset;
  789. }
  790. /* Mask off any extra bits in the last datum */
  791. buffer_tail_bits = (obj_desc->common_field.bit_length +
  792. obj_desc->common_field.start_field_bit_offset) %
  793. obj_desc->common_field.access_bit_width;
  794. if (buffer_tail_bits) {
  795. mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  796. }
  797. /* Write the last datum to the field */
  798. merged_datum &= mask;
  799. status = acpi_ex_write_with_update_rule(obj_desc,
  800. mask, merged_datum,
  801. field_offset);
  802. exit:
  803. /* Free temporary buffer if we used one */
  804. if (new_buffer) {
  805. ACPI_FREE(new_buffer);
  806. }
  807. return_ACPI_STATUS(status);
  808. }