exfldio.c 28 KB

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