exfldio.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /******************************************************************************
  2. *
  3. * Module Name: exfldio - Aml Field I/O
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2010, 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. u64 *value, u32 read_write);
  55. static u8
  56. acpi_ex_register_overflow(union acpi_operand_object *obj_desc, u64 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 (rgn_desc->common.type != ACPI_TYPE_REGION) {
  85. ACPI_ERROR((AE_INFO, "Needed Region, found type 0x%X (%s)",
  86. rgn_desc->common.type,
  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 or IPMI 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. rgn_desc->region.space_id == ACPI_ADR_SPACE_IPMI) {
  110. /* SMBus or IPMI 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 (%u bytes) too large for region [%4.4s] (length %u)",
  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 %u+%u+%u is beyond end of region [%4.4s] (length %u)",
  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. * 64 bits)
  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, u64 *value, u32 function)
  201. {
  202. acpi_status status;
  203. union acpi_operand_object *rgn_desc;
  204. u32 region_offset;
  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. region_offset =
  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. (rgn_desc->
  238. region.
  239. address +
  240. region_offset))));
  241. /* Invoke the appropriate address_space/op_region handler */
  242. status =
  243. acpi_ev_address_space_dispatch(rgn_desc, function, region_offset,
  244. ACPI_MUL_8(obj_desc->common_field.
  245. access_byte_width),
  246. value);
  247. if (ACPI_FAILURE(status)) {
  248. if (status == AE_NOT_IMPLEMENTED) {
  249. ACPI_ERROR((AE_INFO,
  250. "Region %s(0x%X) not implemented",
  251. acpi_ut_get_region_name(rgn_desc->region.
  252. space_id),
  253. rgn_desc->region.space_id));
  254. } else if (status == AE_NOT_EXIST) {
  255. ACPI_ERROR((AE_INFO,
  256. "Region %s(0x%X) has no handler",
  257. acpi_ut_get_region_name(rgn_desc->region.
  258. space_id),
  259. rgn_desc->region.space_id));
  260. }
  261. }
  262. return_ACPI_STATUS(status);
  263. }
  264. /*******************************************************************************
  265. *
  266. * FUNCTION: acpi_ex_register_overflow
  267. *
  268. * PARAMETERS: obj_desc - Register(Field) to be written
  269. * Value - Value to be stored
  270. *
  271. * RETURN: TRUE if value overflows the field, FALSE otherwise
  272. *
  273. * DESCRIPTION: Check if a value is out of range of the field being written.
  274. * Used to check if the values written to Index and Bank registers
  275. * are out of range. Normally, the value is simply truncated
  276. * to fit the field, but this case is most likely a serious
  277. * coding error in the ASL.
  278. *
  279. ******************************************************************************/
  280. static u8
  281. acpi_ex_register_overflow(union acpi_operand_object *obj_desc, u64 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 >= ((u64) 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, u64 *value, u32 read_write)
  320. {
  321. acpi_status status;
  322. u64 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 (obj_desc->common.type) {
  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. (u64) obj_desc->bank_field.
  386. 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. (u64) obj_desc->index_field.
  422. 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. if (read_write == ACPI_READ) {
  438. /* Read the datum from the data_register */
  439. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  440. "Read from Data Register\n"));
  441. status =
  442. acpi_ex_extract_from_field(obj_desc->index_field.
  443. data_obj, value,
  444. sizeof(u64));
  445. } else {
  446. /* Write the datum to the data_register */
  447. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  448. "Write to Data Register: Value %8.8X%8.8X\n",
  449. ACPI_FORMAT_UINT64(*value)));
  450. status =
  451. acpi_ex_insert_into_field(obj_desc->index_field.
  452. data_obj, value,
  453. sizeof(u64));
  454. }
  455. break;
  456. default:
  457. ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %u",
  458. obj_desc->common.type));
  459. status = AE_AML_INTERNAL;
  460. break;
  461. }
  462. if (ACPI_SUCCESS(status)) {
  463. if (read_write == ACPI_READ) {
  464. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  465. "Value Read %8.8X%8.8X, Width %d\n",
  466. ACPI_FORMAT_UINT64(*value),
  467. obj_desc->common_field.
  468. access_byte_width));
  469. } else {
  470. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  471. "Value Written %8.8X%8.8X, Width %d\n",
  472. ACPI_FORMAT_UINT64(*value),
  473. obj_desc->common_field.
  474. access_byte_width));
  475. }
  476. }
  477. return_ACPI_STATUS(status);
  478. }
  479. /*******************************************************************************
  480. *
  481. * FUNCTION: acpi_ex_write_with_update_rule
  482. *
  483. * PARAMETERS: obj_desc - Field to be written
  484. * Mask - bitmask within field datum
  485. * field_value - Value to write
  486. * field_datum_byte_offset - Offset of datum within field
  487. *
  488. * RETURN: Status
  489. *
  490. * DESCRIPTION: Apply the field update rule to a field write
  491. *
  492. ******************************************************************************/
  493. acpi_status
  494. acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
  495. u64 mask,
  496. u64 field_value, u32 field_datum_byte_offset)
  497. {
  498. acpi_status status = AE_OK;
  499. u64 merged_value;
  500. u64 current_value;
  501. ACPI_FUNCTION_TRACE_U32(ex_write_with_update_rule, mask);
  502. /* Start with the new bits */
  503. merged_value = field_value;
  504. /* If the mask is all ones, we don't need to worry about the update rule */
  505. if (mask != ACPI_UINT64_MAX) {
  506. /* Decode the update rule */
  507. switch (obj_desc->common_field.
  508. field_flags & AML_FIELD_UPDATE_RULE_MASK) {
  509. case AML_FIELD_UPDATE_PRESERVE:
  510. /*
  511. * Check if update rule needs to be applied (not if mask is all
  512. * ones) The left shift drops the bits we want to ignore.
  513. */
  514. if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
  515. ACPI_MUL_8(obj_desc->common_field.
  516. access_byte_width))) != 0) {
  517. /*
  518. * Read the current contents of the byte/word/dword containing
  519. * the field, and merge with the new field value.
  520. */
  521. status =
  522. acpi_ex_field_datum_io(obj_desc,
  523. field_datum_byte_offset,
  524. &current_value,
  525. ACPI_READ);
  526. if (ACPI_FAILURE(status)) {
  527. return_ACPI_STATUS(status);
  528. }
  529. merged_value |= (current_value & ~mask);
  530. }
  531. break;
  532. case AML_FIELD_UPDATE_WRITE_AS_ONES:
  533. /* Set positions outside the field to all ones */
  534. merged_value |= ~mask;
  535. break;
  536. case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
  537. /* Set positions outside the field to all zeros */
  538. merged_value &= mask;
  539. break;
  540. default:
  541. ACPI_ERROR((AE_INFO,
  542. "Unknown UpdateRule value: 0x%X",
  543. (obj_desc->common_field.
  544. field_flags &
  545. AML_FIELD_UPDATE_RULE_MASK)));
  546. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  547. }
  548. }
  549. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  550. "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
  551. ACPI_FORMAT_UINT64(mask),
  552. field_datum_byte_offset,
  553. obj_desc->common_field.access_byte_width,
  554. ACPI_FORMAT_UINT64(field_value),
  555. ACPI_FORMAT_UINT64(merged_value)));
  556. /* Write the merged value */
  557. status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
  558. &merged_value, ACPI_WRITE);
  559. return_ACPI_STATUS(status);
  560. }
  561. /*******************************************************************************
  562. *
  563. * FUNCTION: acpi_ex_extract_from_field
  564. *
  565. * PARAMETERS: obj_desc - Field to be read
  566. * Buffer - Where to store the field data
  567. * buffer_length - Length of Buffer
  568. *
  569. * RETURN: Status
  570. *
  571. * DESCRIPTION: Retrieve the current value of the given field
  572. *
  573. ******************************************************************************/
  574. acpi_status
  575. acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
  576. void *buffer, u32 buffer_length)
  577. {
  578. acpi_status status;
  579. u64 raw_datum;
  580. u64 merged_datum;
  581. u32 field_offset = 0;
  582. u32 buffer_offset = 0;
  583. u32 buffer_tail_bits;
  584. u32 datum_count;
  585. u32 field_datum_count;
  586. u32 i;
  587. ACPI_FUNCTION_TRACE(ex_extract_from_field);
  588. /* Validate target buffer and clear it */
  589. if (buffer_length <
  590. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
  591. ACPI_ERROR((AE_INFO,
  592. "Field size %u (bits) is too large for buffer (%u)",
  593. obj_desc->common_field.bit_length, buffer_length));
  594. return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
  595. }
  596. ACPI_MEMSET(buffer, 0, buffer_length);
  597. /* Compute the number of datums (access width data items) */
  598. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  599. obj_desc->common_field.access_bit_width);
  600. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  601. obj_desc->common_field.
  602. start_field_bit_offset,
  603. obj_desc->common_field.
  604. access_bit_width);
  605. /* Priming read from the field */
  606. status =
  607. acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
  608. ACPI_READ);
  609. if (ACPI_FAILURE(status)) {
  610. return_ACPI_STATUS(status);
  611. }
  612. merged_datum =
  613. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  614. /* Read the rest of the field */
  615. for (i = 1; i < field_datum_count; i++) {
  616. /* Get next input datum from the field */
  617. field_offset += obj_desc->common_field.access_byte_width;
  618. status = acpi_ex_field_datum_io(obj_desc, field_offset,
  619. &raw_datum, ACPI_READ);
  620. if (ACPI_FAILURE(status)) {
  621. return_ACPI_STATUS(status);
  622. }
  623. /*
  624. * Merge with previous datum if necessary.
  625. *
  626. * Note: Before the shift, check if the shift value will be larger than
  627. * the integer size. If so, there is no need to perform the operation.
  628. * This avoids the differences in behavior between different compilers
  629. * concerning shift values larger than the target data width.
  630. */
  631. if ((obj_desc->common_field.access_bit_width -
  632. obj_desc->common_field.start_field_bit_offset) <
  633. ACPI_INTEGER_BIT_SIZE) {
  634. merged_datum |=
  635. raw_datum << (obj_desc->common_field.
  636. access_bit_width -
  637. obj_desc->common_field.
  638. start_field_bit_offset);
  639. }
  640. if (i == datum_count) {
  641. break;
  642. }
  643. /* Write merged datum to target buffer */
  644. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  645. ACPI_MIN(obj_desc->common_field.access_byte_width,
  646. buffer_length - buffer_offset));
  647. buffer_offset += obj_desc->common_field.access_byte_width;
  648. merged_datum =
  649. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  650. }
  651. /* Mask off any extra bits in the last datum */
  652. buffer_tail_bits = obj_desc->common_field.bit_length %
  653. obj_desc->common_field.access_bit_width;
  654. if (buffer_tail_bits) {
  655. merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  656. }
  657. /* Write the last datum to the buffer */
  658. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  659. ACPI_MIN(obj_desc->common_field.access_byte_width,
  660. buffer_length - buffer_offset));
  661. return_ACPI_STATUS(AE_OK);
  662. }
  663. /*******************************************************************************
  664. *
  665. * FUNCTION: acpi_ex_insert_into_field
  666. *
  667. * PARAMETERS: obj_desc - Field to be written
  668. * Buffer - Data to be written
  669. * buffer_length - Length of Buffer
  670. *
  671. * RETURN: Status
  672. *
  673. * DESCRIPTION: Store the Buffer contents into the given field
  674. *
  675. ******************************************************************************/
  676. acpi_status
  677. acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
  678. void *buffer, u32 buffer_length)
  679. {
  680. acpi_status status;
  681. u64 mask;
  682. u64 width_mask;
  683. u64 merged_datum;
  684. u64 raw_datum = 0;
  685. u32 field_offset = 0;
  686. u32 buffer_offset = 0;
  687. u32 buffer_tail_bits;
  688. u32 datum_count;
  689. u32 field_datum_count;
  690. u32 i;
  691. u32 required_length;
  692. void *new_buffer;
  693. ACPI_FUNCTION_TRACE(ex_insert_into_field);
  694. /* Validate input buffer */
  695. new_buffer = NULL;
  696. required_length =
  697. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  698. /*
  699. * We must have a buffer that is at least as long as the field
  700. * we are writing to. This is because individual fields are
  701. * indivisible and partial writes are not supported -- as per
  702. * the ACPI specification.
  703. */
  704. if (buffer_length < required_length) {
  705. /* We need to create a new buffer */
  706. new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
  707. if (!new_buffer) {
  708. return_ACPI_STATUS(AE_NO_MEMORY);
  709. }
  710. /*
  711. * Copy the original data to the new buffer, starting
  712. * at Byte zero. All unused (upper) bytes of the
  713. * buffer will be 0.
  714. */
  715. ACPI_MEMCPY((char *)new_buffer, (char *)buffer, buffer_length);
  716. buffer = new_buffer;
  717. buffer_length = required_length;
  718. }
  719. /*
  720. * Create the bitmasks used for bit insertion.
  721. * Note: This if/else is used to bypass compiler differences with the
  722. * shift operator
  723. */
  724. if (obj_desc->common_field.access_bit_width == ACPI_INTEGER_BIT_SIZE) {
  725. width_mask = ACPI_UINT64_MAX;
  726. } else {
  727. width_mask =
  728. ACPI_MASK_BITS_ABOVE(obj_desc->common_field.
  729. access_bit_width);
  730. }
  731. mask = width_mask &
  732. ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
  733. /* Compute the number of datums (access width data items) */
  734. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  735. obj_desc->common_field.access_bit_width);
  736. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  737. obj_desc->common_field.
  738. start_field_bit_offset,
  739. obj_desc->common_field.
  740. access_bit_width);
  741. /* Get initial Datum from the input buffer */
  742. ACPI_MEMCPY(&raw_datum, buffer,
  743. ACPI_MIN(obj_desc->common_field.access_byte_width,
  744. buffer_length - buffer_offset));
  745. merged_datum =
  746. raw_datum << obj_desc->common_field.start_field_bit_offset;
  747. /* Write the entire field */
  748. for (i = 1; i < field_datum_count; i++) {
  749. /* Write merged datum to the target field */
  750. merged_datum &= mask;
  751. status = acpi_ex_write_with_update_rule(obj_desc, mask,
  752. merged_datum,
  753. field_offset);
  754. if (ACPI_FAILURE(status)) {
  755. goto exit;
  756. }
  757. field_offset += obj_desc->common_field.access_byte_width;
  758. /*
  759. * Start new output datum by merging with previous input datum
  760. * if necessary.
  761. *
  762. * Note: Before the shift, check if the shift value will be larger than
  763. * the integer size. If so, there is no need to perform the operation.
  764. * This avoids the differences in behavior between different compilers
  765. * concerning shift values larger than the target data width.
  766. */
  767. if ((obj_desc->common_field.access_bit_width -
  768. obj_desc->common_field.start_field_bit_offset) <
  769. ACPI_INTEGER_BIT_SIZE) {
  770. merged_datum =
  771. raw_datum >> (obj_desc->common_field.
  772. access_bit_width -
  773. obj_desc->common_field.
  774. start_field_bit_offset);
  775. } else {
  776. merged_datum = 0;
  777. }
  778. mask = width_mask;
  779. if (i == datum_count) {
  780. break;
  781. }
  782. /* Get the next input datum from the buffer */
  783. buffer_offset += obj_desc->common_field.access_byte_width;
  784. ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
  785. ACPI_MIN(obj_desc->common_field.access_byte_width,
  786. buffer_length - buffer_offset));
  787. merged_datum |=
  788. raw_datum << obj_desc->common_field.start_field_bit_offset;
  789. }
  790. /* Mask off any extra bits in the last datum */
  791. buffer_tail_bits = (obj_desc->common_field.bit_length +
  792. obj_desc->common_field.start_field_bit_offset) %
  793. obj_desc->common_field.access_bit_width;
  794. if (buffer_tail_bits) {
  795. mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  796. }
  797. /* Write the last datum to the field */
  798. merged_datum &= mask;
  799. status = acpi_ex_write_with_update_rule(obj_desc,
  800. mask, merged_datum,
  801. field_offset);
  802. exit:
  803. /* Free temporary buffer if we used one */
  804. if (new_buffer) {
  805. ACPI_FREE(new_buffer);
  806. }
  807. return_ACPI_STATUS(status);
  808. }