exfldio.c 28 KB

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