exfldio.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. if (buffer_length >= sizeof(u64)) {
  616. status =
  617. acpi_ex_field_datum_io(obj_desc, 0, buffer,
  618. ACPI_READ);
  619. } else {
  620. /* Use raw_datum (u64) to handle buffers < 64 bits */
  621. status =
  622. acpi_ex_field_datum_io(obj_desc, 0, &raw_datum,
  623. ACPI_READ);
  624. ACPI_MEMCPY(buffer, &raw_datum, buffer_length);
  625. }
  626. return_ACPI_STATUS(status);
  627. }
  628. /* TBD: Move to common setup code */
  629. /* Field algorithm is limited to sizeof(u64), truncate if needed */
  630. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  631. obj_desc->common_field.access_byte_width = sizeof(u64);
  632. access_bit_width = sizeof(u64) * 8;
  633. }
  634. /* Compute the number of datums (access width data items) */
  635. datum_count =
  636. ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  637. access_bit_width);
  638. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  639. obj_desc->common_field.
  640. start_field_bit_offset,
  641. access_bit_width);
  642. /* Priming read from the field */
  643. status =
  644. acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
  645. ACPI_READ);
  646. if (ACPI_FAILURE(status)) {
  647. return_ACPI_STATUS(status);
  648. }
  649. merged_datum =
  650. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  651. /* Read the rest of the field */
  652. for (i = 1; i < field_datum_count; i++) {
  653. /* Get next input datum from the field */
  654. field_offset += obj_desc->common_field.access_byte_width;
  655. status = acpi_ex_field_datum_io(obj_desc, field_offset,
  656. &raw_datum, ACPI_READ);
  657. if (ACPI_FAILURE(status)) {
  658. return_ACPI_STATUS(status);
  659. }
  660. /*
  661. * Merge with previous datum if necessary.
  662. *
  663. * Note: Before the shift, check if the shift value will be larger than
  664. * the integer size. If so, there is no need to perform the operation.
  665. * This avoids the differences in behavior between different compilers
  666. * concerning shift values larger than the target data width.
  667. */
  668. if (access_bit_width -
  669. obj_desc->common_field.start_field_bit_offset <
  670. ACPI_INTEGER_BIT_SIZE) {
  671. merged_datum |=
  672. raw_datum << (access_bit_width -
  673. obj_desc->common_field.
  674. start_field_bit_offset);
  675. }
  676. if (i == datum_count) {
  677. break;
  678. }
  679. /* Write merged datum to target buffer */
  680. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  681. ACPI_MIN(obj_desc->common_field.access_byte_width,
  682. buffer_length - buffer_offset));
  683. buffer_offset += obj_desc->common_field.access_byte_width;
  684. merged_datum =
  685. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  686. }
  687. /* Mask off any extra bits in the last datum */
  688. buffer_tail_bits = obj_desc->common_field.bit_length % access_bit_width;
  689. if (buffer_tail_bits) {
  690. merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  691. }
  692. /* Write the last datum to the buffer */
  693. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  694. ACPI_MIN(obj_desc->common_field.access_byte_width,
  695. buffer_length - buffer_offset));
  696. return_ACPI_STATUS(AE_OK);
  697. }
  698. /*******************************************************************************
  699. *
  700. * FUNCTION: acpi_ex_insert_into_field
  701. *
  702. * PARAMETERS: obj_desc - Field to be written
  703. * buffer - Data to be written
  704. * buffer_length - Length of Buffer
  705. *
  706. * RETURN: Status
  707. *
  708. * DESCRIPTION: Store the Buffer contents into the given field
  709. *
  710. ******************************************************************************/
  711. acpi_status
  712. acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
  713. void *buffer, u32 buffer_length)
  714. {
  715. void *new_buffer;
  716. acpi_status status;
  717. u64 mask;
  718. u64 width_mask;
  719. u64 merged_datum;
  720. u64 raw_datum = 0;
  721. u32 field_offset = 0;
  722. u32 buffer_offset = 0;
  723. u32 buffer_tail_bits;
  724. u32 datum_count;
  725. u32 field_datum_count;
  726. u32 access_bit_width;
  727. u32 required_length;
  728. u32 i;
  729. ACPI_FUNCTION_TRACE(ex_insert_into_field);
  730. /* Validate input buffer */
  731. new_buffer = NULL;
  732. required_length =
  733. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  734. /*
  735. * We must have a buffer that is at least as long as the field
  736. * we are writing to. This is because individual fields are
  737. * indivisible and partial writes are not supported -- as per
  738. * the ACPI specification.
  739. */
  740. if (buffer_length < required_length) {
  741. /* We need to create a new buffer */
  742. new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
  743. if (!new_buffer) {
  744. return_ACPI_STATUS(AE_NO_MEMORY);
  745. }
  746. /*
  747. * Copy the original data to the new buffer, starting
  748. * at Byte zero. All unused (upper) bytes of the
  749. * buffer will be 0.
  750. */
  751. ACPI_MEMCPY((char *)new_buffer, (char *)buffer, buffer_length);
  752. buffer = new_buffer;
  753. buffer_length = required_length;
  754. }
  755. /* TBD: Move to common setup code */
  756. /* Algo is limited to sizeof(u64), so cut the access_byte_width */
  757. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  758. obj_desc->common_field.access_byte_width = sizeof(u64);
  759. }
  760. access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
  761. /*
  762. * Create the bitmasks used for bit insertion.
  763. * Note: This if/else is used to bypass compiler differences with the
  764. * shift operator
  765. */
  766. if (access_bit_width == ACPI_INTEGER_BIT_SIZE) {
  767. width_mask = ACPI_UINT64_MAX;
  768. } else {
  769. width_mask = ACPI_MASK_BITS_ABOVE(access_bit_width);
  770. }
  771. mask = width_mask &
  772. ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
  773. /* Compute the number of datums (access width data items) */
  774. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  775. access_bit_width);
  776. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  777. obj_desc->common_field.
  778. start_field_bit_offset,
  779. access_bit_width);
  780. /* Get initial Datum from the input buffer */
  781. ACPI_MEMCPY(&raw_datum, buffer,
  782. ACPI_MIN(obj_desc->common_field.access_byte_width,
  783. buffer_length - buffer_offset));
  784. merged_datum =
  785. raw_datum << obj_desc->common_field.start_field_bit_offset;
  786. /* Write the entire field */
  787. for (i = 1; i < field_datum_count; i++) {
  788. /* Write merged datum to the target field */
  789. merged_datum &= mask;
  790. status = acpi_ex_write_with_update_rule(obj_desc, mask,
  791. merged_datum,
  792. field_offset);
  793. if (ACPI_FAILURE(status)) {
  794. goto exit;
  795. }
  796. field_offset += obj_desc->common_field.access_byte_width;
  797. /*
  798. * Start new output datum by merging with previous input datum
  799. * if necessary.
  800. *
  801. * Note: Before the shift, check if the shift value will be larger than
  802. * the integer size. If so, there is no need to perform the operation.
  803. * This avoids the differences in behavior between different compilers
  804. * concerning shift values larger than the target data width.
  805. */
  806. if ((access_bit_width -
  807. obj_desc->common_field.start_field_bit_offset) <
  808. ACPI_INTEGER_BIT_SIZE) {
  809. merged_datum =
  810. raw_datum >> (access_bit_width -
  811. obj_desc->common_field.
  812. start_field_bit_offset);
  813. } else {
  814. merged_datum = 0;
  815. }
  816. mask = width_mask;
  817. if (i == datum_count) {
  818. break;
  819. }
  820. /* Get the next input datum from the buffer */
  821. buffer_offset += obj_desc->common_field.access_byte_width;
  822. ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
  823. ACPI_MIN(obj_desc->common_field.access_byte_width,
  824. buffer_length - buffer_offset));
  825. merged_datum |=
  826. raw_datum << obj_desc->common_field.start_field_bit_offset;
  827. }
  828. /* Mask off any extra bits in the last datum */
  829. buffer_tail_bits = (obj_desc->common_field.bit_length +
  830. obj_desc->common_field.start_field_bit_offset) %
  831. access_bit_width;
  832. if (buffer_tail_bits) {
  833. mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  834. }
  835. /* Write the last datum to the field */
  836. merged_datum &= mask;
  837. status = acpi_ex_write_with_update_rule(obj_desc,
  838. mask, merged_datum,
  839. field_offset);
  840. exit:
  841. /* Free temporary buffer if we used one */
  842. if (new_buffer) {
  843. ACPI_FREE(new_buffer);
  844. }
  845. return_ACPI_STATUS(status);
  846. }