exfldio.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. /******************************************************************************
  2. *
  3. * Module Name: exfldio - Aml Field I/O
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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
  106. * address space 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 + 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 (%u bytes) too large for region [%4.4s] (length %u)",
  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 %u+%u+%u is beyond end of region [%4.4s] (length %u)",
  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. * 64 bits)
  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, u64 *value, u32 function)
  200. {
  201. acpi_status status;
  202. union acpi_operand_object *rgn_desc;
  203. u32 region_offset;
  204. ACPI_FUNCTION_TRACE(ex_access_region);
  205. /*
  206. * Ensure that the region operands are fully evaluated and verify
  207. * the validity of the request
  208. */
  209. status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
  210. if (ACPI_FAILURE(status)) {
  211. return_ACPI_STATUS(status);
  212. }
  213. /*
  214. * The physical address of this field datum is:
  215. *
  216. * 1) The base of the region, plus
  217. * 2) The base offset of the field, plus
  218. * 3) The current offset into the field
  219. */
  220. rgn_desc = obj_desc->common_field.region_obj;
  221. region_offset =
  222. obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
  223. if ((function & ACPI_IO_MASK) == ACPI_READ) {
  224. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
  225. } else {
  226. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
  227. }
  228. ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
  229. " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
  230. acpi_ut_get_region_name(rgn_desc->region.
  231. space_id),
  232. rgn_desc->region.space_id,
  233. obj_desc->common_field.access_byte_width,
  234. obj_desc->common_field.base_byte_offset,
  235. field_datum_byte_offset, ACPI_CAST_PTR(void,
  236. (rgn_desc->
  237. region.
  238. address +
  239. region_offset))));
  240. /* Invoke the appropriate address_space/op_region handler */
  241. status =
  242. acpi_ev_address_space_dispatch(rgn_desc, function, region_offset,
  243. ACPI_MUL_8(obj_desc->common_field.
  244. access_byte_width),
  245. value);
  246. if (ACPI_FAILURE(status)) {
  247. if (status == AE_NOT_IMPLEMENTED) {
  248. ACPI_ERROR((AE_INFO,
  249. "Region %s (ID=%u) not implemented",
  250. acpi_ut_get_region_name(rgn_desc->region.
  251. space_id),
  252. rgn_desc->region.space_id));
  253. } else if (status == AE_NOT_EXIST) {
  254. ACPI_ERROR((AE_INFO,
  255. "Region %s (ID=%u) has no handler",
  256. acpi_ut_get_region_name(rgn_desc->region.
  257. space_id),
  258. rgn_desc->region.space_id));
  259. }
  260. }
  261. return_ACPI_STATUS(status);
  262. }
  263. /*******************************************************************************
  264. *
  265. * FUNCTION: acpi_ex_register_overflow
  266. *
  267. * PARAMETERS: obj_desc - Register(Field) to be written
  268. * Value - Value to be stored
  269. *
  270. * RETURN: TRUE if value overflows the field, FALSE otherwise
  271. *
  272. * DESCRIPTION: Check if a value is out of range of the field being written.
  273. * Used to check if the values written to Index and Bank registers
  274. * are out of range. Normally, the value is simply truncated
  275. * to fit the field, but this case is most likely a serious
  276. * coding error in the ASL.
  277. *
  278. ******************************************************************************/
  279. static u8
  280. acpi_ex_register_overflow(union acpi_operand_object *obj_desc, u64 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 >= ((u64) 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, u64 *value, u32 read_write)
  319. {
  320. acpi_status status;
  321. u64 local_value;
  322. ACPI_FUNCTION_TRACE_U32(ex_field_datum_io, field_datum_byte_offset);
  323. if (read_write == ACPI_READ) {
  324. if (!value) {
  325. local_value = 0;
  326. /* To support reads without saving return value */
  327. value = &local_value;
  328. }
  329. /* Clear the entire return buffer first, [Very Important!] */
  330. *value = 0;
  331. }
  332. /*
  333. * The four types of fields are:
  334. *
  335. * buffer_field - Read/write from/to a Buffer
  336. * region_field - Read/write from/to a Operation Region.
  337. * bank_field - Write to a Bank Register, then read/write from/to an
  338. * operation_region
  339. * index_field - Write to an Index Register, then read/write from/to a
  340. * Data Register
  341. */
  342. switch (obj_desc->common.type) {
  343. case ACPI_TYPE_BUFFER_FIELD:
  344. /*
  345. * If the buffer_field arguments have not been previously evaluated,
  346. * evaluate them now and save the results.
  347. */
  348. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  349. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  350. if (ACPI_FAILURE(status)) {
  351. return_ACPI_STATUS(status);
  352. }
  353. }
  354. if (read_write == ACPI_READ) {
  355. /*
  356. * Copy the data from the source buffer.
  357. * Length is the field width in bytes.
  358. */
  359. ACPI_MEMCPY(value,
  360. (obj_desc->buffer_field.buffer_obj)->buffer.
  361. pointer +
  362. obj_desc->buffer_field.base_byte_offset +
  363. field_datum_byte_offset,
  364. obj_desc->common_field.access_byte_width);
  365. } else {
  366. /*
  367. * Copy the data to the target buffer.
  368. * Length is the field width in bytes.
  369. */
  370. ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
  371. pointer +
  372. obj_desc->buffer_field.base_byte_offset +
  373. field_datum_byte_offset, value,
  374. obj_desc->common_field.access_byte_width);
  375. }
  376. status = AE_OK;
  377. break;
  378. case ACPI_TYPE_LOCAL_BANK_FIELD:
  379. /*
  380. * Ensure that the bank_value is not beyond the capacity of
  381. * the register
  382. */
  383. if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
  384. (u64) obj_desc->bank_field.
  385. value)) {
  386. return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
  387. }
  388. /*
  389. * For bank_fields, we must write the bank_value to the bank_register
  390. * (itself a region_field) before we can access the data.
  391. */
  392. status =
  393. acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
  394. &obj_desc->bank_field.value,
  395. sizeof(obj_desc->bank_field.
  396. value));
  397. if (ACPI_FAILURE(status)) {
  398. return_ACPI_STATUS(status);
  399. }
  400. /*
  401. * Now that the Bank has been selected, fall through to the
  402. * region_field case and write the datum to the Operation Region
  403. */
  404. /*lint -fallthrough */
  405. case ACPI_TYPE_LOCAL_REGION_FIELD:
  406. /*
  407. * For simple region_fields, we just directly access the owning
  408. * Operation Region.
  409. */
  410. status =
  411. acpi_ex_access_region(obj_desc, field_datum_byte_offset,
  412. value, read_write);
  413. break;
  414. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  415. /*
  416. * Ensure that the index_value is not beyond the capacity of
  417. * the register
  418. */
  419. if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
  420. (u64) obj_desc->index_field.
  421. value)) {
  422. return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
  423. }
  424. /* Write the index value to the index_register (itself a region_field) */
  425. field_datum_byte_offset += obj_desc->index_field.value;
  426. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  427. "Write to Index Register: Value %8.8X\n",
  428. field_datum_byte_offset));
  429. status =
  430. acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
  431. &field_datum_byte_offset,
  432. sizeof(field_datum_byte_offset));
  433. if (ACPI_FAILURE(status)) {
  434. return_ACPI_STATUS(status);
  435. }
  436. if (read_write == ACPI_READ) {
  437. /* Read the datum from the data_register */
  438. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  439. "Read from Data Register\n"));
  440. status =
  441. acpi_ex_extract_from_field(obj_desc->index_field.
  442. data_obj, value,
  443. sizeof(u64));
  444. } else {
  445. /* Write the datum to the data_register */
  446. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  447. "Write to Data Register: Value %8.8X%8.8X\n",
  448. ACPI_FORMAT_UINT64(*value)));
  449. status =
  450. acpi_ex_insert_into_field(obj_desc->index_field.
  451. data_obj, value,
  452. sizeof(u64));
  453. }
  454. break;
  455. default:
  456. ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %u",
  457. obj_desc->common.type));
  458. status = AE_AML_INTERNAL;
  459. break;
  460. }
  461. if (ACPI_SUCCESS(status)) {
  462. if (read_write == ACPI_READ) {
  463. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  464. "Value Read %8.8X%8.8X, Width %u\n",
  465. ACPI_FORMAT_UINT64(*value),
  466. obj_desc->common_field.
  467. access_byte_width));
  468. } else {
  469. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  470. "Value Written %8.8X%8.8X, Width %u\n",
  471. ACPI_FORMAT_UINT64(*value),
  472. obj_desc->common_field.
  473. access_byte_width));
  474. }
  475. }
  476. return_ACPI_STATUS(status);
  477. }
  478. /*******************************************************************************
  479. *
  480. * FUNCTION: acpi_ex_write_with_update_rule
  481. *
  482. * PARAMETERS: obj_desc - Field to be written
  483. * Mask - bitmask within field datum
  484. * field_value - Value to write
  485. * field_datum_byte_offset - Offset of datum within field
  486. *
  487. * RETURN: Status
  488. *
  489. * DESCRIPTION: Apply the field update rule to a field write
  490. *
  491. ******************************************************************************/
  492. acpi_status
  493. acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
  494. u64 mask,
  495. u64 field_value, u32 field_datum_byte_offset)
  496. {
  497. acpi_status status = AE_OK;
  498. u64 merged_value;
  499. u64 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_UINT64_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: 0x%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. u64 raw_datum;
  579. u64 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 access_bit_width;
  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. access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
  598. /* Handle the simple case here */
  599. if ((obj_desc->common_field.start_field_bit_offset == 0) &&
  600. (obj_desc->common_field.bit_length == access_bit_width)) {
  601. status = acpi_ex_field_datum_io(obj_desc, 0, buffer, ACPI_READ);
  602. return_ACPI_STATUS(status);
  603. }
  604. /* TBD: Move to common setup code */
  605. /* Field algorithm is limited to sizeof(u64), truncate if needed */
  606. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  607. obj_desc->common_field.access_byte_width = sizeof(u64);
  608. access_bit_width = sizeof(u64) * 8;
  609. }
  610. /* Compute the number of datums (access width data items) */
  611. datum_count =
  612. ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  613. access_bit_width);
  614. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  615. obj_desc->common_field.
  616. start_field_bit_offset,
  617. access_bit_width);
  618. /* Priming read from the field */
  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. merged_datum =
  626. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  627. /* Read the rest of the field */
  628. for (i = 1; i < field_datum_count; i++) {
  629. /* Get next input datum from the field */
  630. field_offset += obj_desc->common_field.access_byte_width;
  631. status = acpi_ex_field_datum_io(obj_desc, field_offset,
  632. &raw_datum, ACPI_READ);
  633. if (ACPI_FAILURE(status)) {
  634. return_ACPI_STATUS(status);
  635. }
  636. /*
  637. * Merge with previous datum if necessary.
  638. *
  639. * Note: Before the shift, check if the shift value will be larger than
  640. * the integer size. If so, there is no need to perform the operation.
  641. * This avoids the differences in behavior between different compilers
  642. * concerning shift values larger than the target data width.
  643. */
  644. if (access_bit_width -
  645. obj_desc->common_field.start_field_bit_offset <
  646. ACPI_INTEGER_BIT_SIZE) {
  647. merged_datum |=
  648. raw_datum << (access_bit_width -
  649. obj_desc->common_field.
  650. start_field_bit_offset);
  651. }
  652. if (i == datum_count) {
  653. break;
  654. }
  655. /* Write merged datum to target buffer */
  656. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  657. ACPI_MIN(obj_desc->common_field.access_byte_width,
  658. buffer_length - buffer_offset));
  659. buffer_offset += obj_desc->common_field.access_byte_width;
  660. merged_datum =
  661. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  662. }
  663. /* Mask off any extra bits in the last datum */
  664. buffer_tail_bits = obj_desc->common_field.bit_length % access_bit_width;
  665. if (buffer_tail_bits) {
  666. merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  667. }
  668. /* Write the last datum to the buffer */
  669. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  670. ACPI_MIN(obj_desc->common_field.access_byte_width,
  671. buffer_length - buffer_offset));
  672. return_ACPI_STATUS(AE_OK);
  673. }
  674. /*******************************************************************************
  675. *
  676. * FUNCTION: acpi_ex_insert_into_field
  677. *
  678. * PARAMETERS: obj_desc - Field to be written
  679. * Buffer - Data to be written
  680. * buffer_length - Length of Buffer
  681. *
  682. * RETURN: Status
  683. *
  684. * DESCRIPTION: Store the Buffer contents into the given field
  685. *
  686. ******************************************************************************/
  687. acpi_status
  688. acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
  689. void *buffer, u32 buffer_length)
  690. {
  691. void *new_buffer;
  692. acpi_status status;
  693. u64 mask;
  694. u64 width_mask;
  695. u64 merged_datum;
  696. u64 raw_datum = 0;
  697. u32 field_offset = 0;
  698. u32 buffer_offset = 0;
  699. u32 buffer_tail_bits;
  700. u32 datum_count;
  701. u32 field_datum_count;
  702. u32 access_bit_width;
  703. u32 required_length;
  704. u32 i;
  705. ACPI_FUNCTION_TRACE(ex_insert_into_field);
  706. /* Validate input buffer */
  707. new_buffer = NULL;
  708. required_length =
  709. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  710. /*
  711. * We must have a buffer that is at least as long as the field
  712. * we are writing to. This is because individual fields are
  713. * indivisible and partial writes are not supported -- as per
  714. * the ACPI specification.
  715. */
  716. if (buffer_length < required_length) {
  717. /* We need to create a new buffer */
  718. new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
  719. if (!new_buffer) {
  720. return_ACPI_STATUS(AE_NO_MEMORY);
  721. }
  722. /*
  723. * Copy the original data to the new buffer, starting
  724. * at Byte zero. All unused (upper) bytes of the
  725. * buffer will be 0.
  726. */
  727. ACPI_MEMCPY((char *)new_buffer, (char *)buffer, buffer_length);
  728. buffer = new_buffer;
  729. buffer_length = required_length;
  730. }
  731. /* TBD: Move to common setup code */
  732. /* Algo is limited to sizeof(u64), so cut the access_byte_width */
  733. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  734. obj_desc->common_field.access_byte_width = sizeof(u64);
  735. }
  736. access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
  737. /*
  738. * Create the bitmasks used for bit insertion.
  739. * Note: This if/else is used to bypass compiler differences with the
  740. * shift operator
  741. */
  742. if (access_bit_width == ACPI_INTEGER_BIT_SIZE) {
  743. width_mask = ACPI_UINT64_MAX;
  744. } else {
  745. width_mask = ACPI_MASK_BITS_ABOVE(access_bit_width);
  746. }
  747. mask = width_mask &
  748. ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
  749. /* Compute the number of datums (access width data items) */
  750. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  751. access_bit_width);
  752. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  753. obj_desc->common_field.
  754. start_field_bit_offset,
  755. access_bit_width);
  756. /* Get initial Datum from the input buffer */
  757. ACPI_MEMCPY(&raw_datum, buffer,
  758. ACPI_MIN(obj_desc->common_field.access_byte_width,
  759. buffer_length - buffer_offset));
  760. merged_datum =
  761. raw_datum << obj_desc->common_field.start_field_bit_offset;
  762. /* Write the entire field */
  763. for (i = 1; i < field_datum_count; i++) {
  764. /* Write merged datum to the target field */
  765. merged_datum &= mask;
  766. status = acpi_ex_write_with_update_rule(obj_desc, mask,
  767. merged_datum,
  768. field_offset);
  769. if (ACPI_FAILURE(status)) {
  770. goto exit;
  771. }
  772. field_offset += obj_desc->common_field.access_byte_width;
  773. /*
  774. * Start new output datum by merging with previous input datum
  775. * if necessary.
  776. *
  777. * Note: Before the shift, check if the shift value will be larger than
  778. * the integer size. If so, there is no need to perform the operation.
  779. * This avoids the differences in behavior between different compilers
  780. * concerning shift values larger than the target data width.
  781. */
  782. if ((access_bit_width -
  783. obj_desc->common_field.start_field_bit_offset) <
  784. ACPI_INTEGER_BIT_SIZE) {
  785. merged_datum =
  786. raw_datum >> (access_bit_width -
  787. obj_desc->common_field.
  788. start_field_bit_offset);
  789. } else {
  790. merged_datum = 0;
  791. }
  792. mask = width_mask;
  793. if (i == datum_count) {
  794. break;
  795. }
  796. /* Get the next input datum from the buffer */
  797. buffer_offset += obj_desc->common_field.access_byte_width;
  798. ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
  799. ACPI_MIN(obj_desc->common_field.access_byte_width,
  800. buffer_length - buffer_offset));
  801. merged_datum |=
  802. raw_datum << obj_desc->common_field.start_field_bit_offset;
  803. }
  804. /* Mask off any extra bits in the last datum */
  805. buffer_tail_bits = (obj_desc->common_field.bit_length +
  806. obj_desc->common_field.start_field_bit_offset) %
  807. access_bit_width;
  808. if (buffer_tail_bits) {
  809. mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  810. }
  811. /* Write the last datum to the field */
  812. merged_datum &= mask;
  813. status = acpi_ex_write_with_update_rule(obj_desc,
  814. mask, merged_datum,
  815. field_offset);
  816. exit:
  817. /* Free temporary buffer if we used one */
  818. if (new_buffer) {
  819. ACPI_FREE(new_buffer);
  820. }
  821. return_ACPI_STATUS(status);
  822. }