exfldio.c 28 KB

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