exfldio.c 27 KB

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