exfldio.c 29 KB

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