exfldio.c 28 KB

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