exfldio.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /******************************************************************************
  2. *
  3. * Module Name: exfldio - Aml Field I/O
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2012, 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. ACPI_FUNCTION_NAME(ex_register_overflow);
  293. if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
  294. /*
  295. * The field is large enough to hold the maximum integer, so we can
  296. * never overflow it.
  297. */
  298. return (FALSE);
  299. }
  300. if (value >= ((u64) 1 << obj_desc->common_field.bit_length)) {
  301. /*
  302. * The Value is larger than the maximum value that can fit into
  303. * the register.
  304. */
  305. ACPI_ERROR((AE_INFO,
  306. "Index value 0x%8.8X%8.8X overflows field width 0x%X",
  307. ACPI_FORMAT_UINT64(value),
  308. obj_desc->common_field.bit_length));
  309. return (TRUE);
  310. }
  311. /* The Value will fit into the field with no truncation */
  312. return (FALSE);
  313. }
  314. /*******************************************************************************
  315. *
  316. * FUNCTION: acpi_ex_field_datum_io
  317. *
  318. * PARAMETERS: obj_desc - Field to be read
  319. * field_datum_byte_offset - Byte offset of this datum within the
  320. * parent field
  321. * value - Where to store value (must be 64 bits)
  322. * read_write - Read or Write flag
  323. *
  324. * RETURN: Status
  325. *
  326. * DESCRIPTION: Read or Write a single datum of a field. The field_type is
  327. * demultiplexed here to handle the different types of fields
  328. * (buffer_field, region_field, index_field, bank_field)
  329. *
  330. ******************************************************************************/
  331. static acpi_status
  332. acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
  333. u32 field_datum_byte_offset, u64 *value, u32 read_write)
  334. {
  335. acpi_status status;
  336. u64 local_value;
  337. ACPI_FUNCTION_TRACE_U32(ex_field_datum_io, field_datum_byte_offset);
  338. if (read_write == ACPI_READ) {
  339. if (!value) {
  340. local_value = 0;
  341. /* To support reads without saving return value */
  342. value = &local_value;
  343. }
  344. /* Clear the entire return buffer first, [Very Important!] */
  345. *value = 0;
  346. }
  347. /*
  348. * The four types of fields are:
  349. *
  350. * buffer_field - Read/write from/to a Buffer
  351. * region_field - Read/write from/to a Operation Region.
  352. * bank_field - Write to a Bank Register, then read/write from/to an
  353. * operation_region
  354. * index_field - Write to an Index Register, then read/write from/to a
  355. * Data Register
  356. */
  357. switch (obj_desc->common.type) {
  358. case ACPI_TYPE_BUFFER_FIELD:
  359. /*
  360. * If the buffer_field arguments have not been previously evaluated,
  361. * evaluate them now and save the results.
  362. */
  363. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  364. status = acpi_ds_get_buffer_field_arguments(obj_desc);
  365. if (ACPI_FAILURE(status)) {
  366. return_ACPI_STATUS(status);
  367. }
  368. }
  369. if (read_write == ACPI_READ) {
  370. /*
  371. * Copy the data from the source buffer.
  372. * Length is the field width in bytes.
  373. */
  374. ACPI_MEMCPY(value,
  375. (obj_desc->buffer_field.buffer_obj)->buffer.
  376. pointer +
  377. obj_desc->buffer_field.base_byte_offset +
  378. field_datum_byte_offset,
  379. obj_desc->common_field.access_byte_width);
  380. } else {
  381. /*
  382. * Copy the data to the target buffer.
  383. * Length is the field width in bytes.
  384. */
  385. ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
  386. pointer +
  387. obj_desc->buffer_field.base_byte_offset +
  388. field_datum_byte_offset, value,
  389. obj_desc->common_field.access_byte_width);
  390. }
  391. status = AE_OK;
  392. break;
  393. case ACPI_TYPE_LOCAL_BANK_FIELD:
  394. /*
  395. * Ensure that the bank_value is not beyond the capacity of
  396. * the register
  397. */
  398. if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
  399. (u64) obj_desc->bank_field.
  400. value)) {
  401. return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
  402. }
  403. /*
  404. * For bank_fields, we must write the bank_value to the bank_register
  405. * (itself a region_field) before we can access the data.
  406. */
  407. status =
  408. acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
  409. &obj_desc->bank_field.value,
  410. sizeof(obj_desc->bank_field.
  411. value));
  412. if (ACPI_FAILURE(status)) {
  413. return_ACPI_STATUS(status);
  414. }
  415. /*
  416. * Now that the Bank has been selected, fall through to the
  417. * region_field case and write the datum to the Operation Region
  418. */
  419. /*lint -fallthrough */
  420. case ACPI_TYPE_LOCAL_REGION_FIELD:
  421. /*
  422. * For simple region_fields, we just directly access the owning
  423. * Operation Region.
  424. */
  425. status =
  426. acpi_ex_access_region(obj_desc, field_datum_byte_offset,
  427. value, read_write);
  428. break;
  429. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  430. /*
  431. * Ensure that the index_value is not beyond the capacity of
  432. * the register
  433. */
  434. if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
  435. (u64) obj_desc->index_field.
  436. value)) {
  437. return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
  438. }
  439. /* Write the index value to the index_register (itself a region_field) */
  440. field_datum_byte_offset += obj_desc->index_field.value;
  441. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  442. "Write to Index Register: Value %8.8X\n",
  443. field_datum_byte_offset));
  444. status =
  445. acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
  446. &field_datum_byte_offset,
  447. sizeof(field_datum_byte_offset));
  448. if (ACPI_FAILURE(status)) {
  449. return_ACPI_STATUS(status);
  450. }
  451. if (read_write == ACPI_READ) {
  452. /* Read the datum from the data_register */
  453. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  454. "Read from Data Register\n"));
  455. status =
  456. acpi_ex_extract_from_field(obj_desc->index_field.
  457. data_obj, value,
  458. sizeof(u64));
  459. } else {
  460. /* Write the datum to the data_register */
  461. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  462. "Write to Data Register: Value %8.8X%8.8X\n",
  463. ACPI_FORMAT_UINT64(*value)));
  464. status =
  465. acpi_ex_insert_into_field(obj_desc->index_field.
  466. data_obj, value,
  467. sizeof(u64));
  468. }
  469. break;
  470. default:
  471. ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %u",
  472. obj_desc->common.type));
  473. status = AE_AML_INTERNAL;
  474. break;
  475. }
  476. if (ACPI_SUCCESS(status)) {
  477. if (read_write == ACPI_READ) {
  478. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  479. "Value Read %8.8X%8.8X, Width %u\n",
  480. ACPI_FORMAT_UINT64(*value),
  481. obj_desc->common_field.
  482. access_byte_width));
  483. } else {
  484. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  485. "Value Written %8.8X%8.8X, Width %u\n",
  486. ACPI_FORMAT_UINT64(*value),
  487. obj_desc->common_field.
  488. access_byte_width));
  489. }
  490. }
  491. return_ACPI_STATUS(status);
  492. }
  493. /*******************************************************************************
  494. *
  495. * FUNCTION: acpi_ex_write_with_update_rule
  496. *
  497. * PARAMETERS: obj_desc - Field to be written
  498. * mask - bitmask within field datum
  499. * field_value - Value to write
  500. * field_datum_byte_offset - Offset of datum within field
  501. *
  502. * RETURN: Status
  503. *
  504. * DESCRIPTION: Apply the field update rule to a field write
  505. *
  506. ******************************************************************************/
  507. acpi_status
  508. acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
  509. u64 mask,
  510. u64 field_value, u32 field_datum_byte_offset)
  511. {
  512. acpi_status status = AE_OK;
  513. u64 merged_value;
  514. u64 current_value;
  515. ACPI_FUNCTION_TRACE_U32(ex_write_with_update_rule, mask);
  516. /* Start with the new bits */
  517. merged_value = field_value;
  518. /* If the mask is all ones, we don't need to worry about the update rule */
  519. if (mask != ACPI_UINT64_MAX) {
  520. /* Decode the update rule */
  521. switch (obj_desc->common_field.
  522. field_flags & AML_FIELD_UPDATE_RULE_MASK) {
  523. case AML_FIELD_UPDATE_PRESERVE:
  524. /*
  525. * Check if update rule needs to be applied (not if mask is all
  526. * ones) The left shift drops the bits we want to ignore.
  527. */
  528. if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
  529. ACPI_MUL_8(obj_desc->common_field.
  530. access_byte_width))) != 0) {
  531. /*
  532. * Read the current contents of the byte/word/dword containing
  533. * the field, and merge with the new field value.
  534. */
  535. status =
  536. acpi_ex_field_datum_io(obj_desc,
  537. field_datum_byte_offset,
  538. &current_value,
  539. ACPI_READ);
  540. if (ACPI_FAILURE(status)) {
  541. return_ACPI_STATUS(status);
  542. }
  543. merged_value |= (current_value & ~mask);
  544. }
  545. break;
  546. case AML_FIELD_UPDATE_WRITE_AS_ONES:
  547. /* Set positions outside the field to all ones */
  548. merged_value |= ~mask;
  549. break;
  550. case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
  551. /* Set positions outside the field to all zeros */
  552. merged_value &= mask;
  553. break;
  554. default:
  555. ACPI_ERROR((AE_INFO,
  556. "Unknown UpdateRule value: 0x%X",
  557. (obj_desc->common_field.
  558. field_flags &
  559. AML_FIELD_UPDATE_RULE_MASK)));
  560. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  561. }
  562. }
  563. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  564. "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
  565. ACPI_FORMAT_UINT64(mask),
  566. field_datum_byte_offset,
  567. obj_desc->common_field.access_byte_width,
  568. ACPI_FORMAT_UINT64(field_value),
  569. ACPI_FORMAT_UINT64(merged_value)));
  570. /* Write the merged value */
  571. status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
  572. &merged_value, ACPI_WRITE);
  573. return_ACPI_STATUS(status);
  574. }
  575. /*******************************************************************************
  576. *
  577. * FUNCTION: acpi_ex_extract_from_field
  578. *
  579. * PARAMETERS: obj_desc - Field to be read
  580. * buffer - Where to store the field data
  581. * buffer_length - Length of Buffer
  582. *
  583. * RETURN: Status
  584. *
  585. * DESCRIPTION: Retrieve the current value of the given field
  586. *
  587. ******************************************************************************/
  588. acpi_status
  589. acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
  590. void *buffer, u32 buffer_length)
  591. {
  592. acpi_status status;
  593. u64 raw_datum;
  594. u64 merged_datum;
  595. u32 field_offset = 0;
  596. u32 buffer_offset = 0;
  597. u32 buffer_tail_bits;
  598. u32 datum_count;
  599. u32 field_datum_count;
  600. u32 access_bit_width;
  601. u32 i;
  602. ACPI_FUNCTION_TRACE(ex_extract_from_field);
  603. /* Validate target buffer and clear it */
  604. if (buffer_length <
  605. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
  606. ACPI_ERROR((AE_INFO,
  607. "Field size %u (bits) is too large for buffer (%u)",
  608. obj_desc->common_field.bit_length, buffer_length));
  609. return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
  610. }
  611. ACPI_MEMSET(buffer, 0, buffer_length);
  612. access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
  613. /* Handle the simple case here */
  614. if ((obj_desc->common_field.start_field_bit_offset == 0) &&
  615. (obj_desc->common_field.bit_length == access_bit_width)) {
  616. status = acpi_ex_field_datum_io(obj_desc, 0, buffer, ACPI_READ);
  617. return_ACPI_STATUS(status);
  618. }
  619. /* TBD: Move to common setup code */
  620. /* Field algorithm is limited to sizeof(u64), truncate if needed */
  621. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  622. obj_desc->common_field.access_byte_width = sizeof(u64);
  623. access_bit_width = sizeof(u64) * 8;
  624. }
  625. /* Compute the number of datums (access width data items) */
  626. datum_count =
  627. ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  628. access_bit_width);
  629. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  630. obj_desc->common_field.
  631. start_field_bit_offset,
  632. access_bit_width);
  633. /* Priming read from the field */
  634. status =
  635. acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
  636. ACPI_READ);
  637. if (ACPI_FAILURE(status)) {
  638. return_ACPI_STATUS(status);
  639. }
  640. merged_datum =
  641. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  642. /* Read the rest of the field */
  643. for (i = 1; i < field_datum_count; i++) {
  644. /* Get next input datum from the field */
  645. field_offset += obj_desc->common_field.access_byte_width;
  646. status = acpi_ex_field_datum_io(obj_desc, field_offset,
  647. &raw_datum, ACPI_READ);
  648. if (ACPI_FAILURE(status)) {
  649. return_ACPI_STATUS(status);
  650. }
  651. /*
  652. * Merge with previous datum if necessary.
  653. *
  654. * Note: Before the shift, check if the shift value will be larger than
  655. * the integer size. If so, there is no need to perform the operation.
  656. * This avoids the differences in behavior between different compilers
  657. * concerning shift values larger than the target data width.
  658. */
  659. if (access_bit_width -
  660. obj_desc->common_field.start_field_bit_offset <
  661. ACPI_INTEGER_BIT_SIZE) {
  662. merged_datum |=
  663. raw_datum << (access_bit_width -
  664. obj_desc->common_field.
  665. start_field_bit_offset);
  666. }
  667. if (i == datum_count) {
  668. break;
  669. }
  670. /* Write merged datum to target buffer */
  671. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  672. ACPI_MIN(obj_desc->common_field.access_byte_width,
  673. buffer_length - buffer_offset));
  674. buffer_offset += obj_desc->common_field.access_byte_width;
  675. merged_datum =
  676. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  677. }
  678. /* Mask off any extra bits in the last datum */
  679. buffer_tail_bits = obj_desc->common_field.bit_length % access_bit_width;
  680. if (buffer_tail_bits) {
  681. merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  682. }
  683. /* Write the last datum to the buffer */
  684. ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
  685. ACPI_MIN(obj_desc->common_field.access_byte_width,
  686. buffer_length - buffer_offset));
  687. return_ACPI_STATUS(AE_OK);
  688. }
  689. /*******************************************************************************
  690. *
  691. * FUNCTION: acpi_ex_insert_into_field
  692. *
  693. * PARAMETERS: obj_desc - Field to be written
  694. * buffer - Data to be written
  695. * buffer_length - Length of Buffer
  696. *
  697. * RETURN: Status
  698. *
  699. * DESCRIPTION: Store the Buffer contents into the given field
  700. *
  701. ******************************************************************************/
  702. acpi_status
  703. acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
  704. void *buffer, u32 buffer_length)
  705. {
  706. void *new_buffer;
  707. acpi_status status;
  708. u64 mask;
  709. u64 width_mask;
  710. u64 merged_datum;
  711. u64 raw_datum = 0;
  712. u32 field_offset = 0;
  713. u32 buffer_offset = 0;
  714. u32 buffer_tail_bits;
  715. u32 datum_count;
  716. u32 field_datum_count;
  717. u32 access_bit_width;
  718. u32 required_length;
  719. u32 i;
  720. ACPI_FUNCTION_TRACE(ex_insert_into_field);
  721. /* Validate input buffer */
  722. new_buffer = NULL;
  723. required_length =
  724. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  725. /*
  726. * We must have a buffer that is at least as long as the field
  727. * we are writing to. This is because individual fields are
  728. * indivisible and partial writes are not supported -- as per
  729. * the ACPI specification.
  730. */
  731. if (buffer_length < required_length) {
  732. /* We need to create a new buffer */
  733. new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
  734. if (!new_buffer) {
  735. return_ACPI_STATUS(AE_NO_MEMORY);
  736. }
  737. /*
  738. * Copy the original data to the new buffer, starting
  739. * at Byte zero. All unused (upper) bytes of the
  740. * buffer will be 0.
  741. */
  742. ACPI_MEMCPY((char *)new_buffer, (char *)buffer, buffer_length);
  743. buffer = new_buffer;
  744. buffer_length = required_length;
  745. }
  746. /* TBD: Move to common setup code */
  747. /* Algo is limited to sizeof(u64), so cut the access_byte_width */
  748. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  749. obj_desc->common_field.access_byte_width = sizeof(u64);
  750. }
  751. access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
  752. /*
  753. * Create the bitmasks used for bit insertion.
  754. * Note: This if/else is used to bypass compiler differences with the
  755. * shift operator
  756. */
  757. if (access_bit_width == ACPI_INTEGER_BIT_SIZE) {
  758. width_mask = ACPI_UINT64_MAX;
  759. } else {
  760. width_mask = ACPI_MASK_BITS_ABOVE(access_bit_width);
  761. }
  762. mask = width_mask &
  763. ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
  764. /* Compute the number of datums (access width data items) */
  765. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  766. access_bit_width);
  767. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  768. obj_desc->common_field.
  769. start_field_bit_offset,
  770. access_bit_width);
  771. /* Get initial Datum from the input buffer */
  772. ACPI_MEMCPY(&raw_datum, buffer,
  773. ACPI_MIN(obj_desc->common_field.access_byte_width,
  774. buffer_length - buffer_offset));
  775. merged_datum =
  776. raw_datum << obj_desc->common_field.start_field_bit_offset;
  777. /* Write the entire field */
  778. for (i = 1; i < field_datum_count; i++) {
  779. /* Write merged datum to the target field */
  780. merged_datum &= mask;
  781. status = acpi_ex_write_with_update_rule(obj_desc, mask,
  782. merged_datum,
  783. field_offset);
  784. if (ACPI_FAILURE(status)) {
  785. goto exit;
  786. }
  787. field_offset += obj_desc->common_field.access_byte_width;
  788. /*
  789. * Start new output datum by merging with previous input datum
  790. * if necessary.
  791. *
  792. * Note: Before the shift, check if the shift value will be larger than
  793. * the integer size. If so, there is no need to perform the operation.
  794. * This avoids the differences in behavior between different compilers
  795. * concerning shift values larger than the target data width.
  796. */
  797. if ((access_bit_width -
  798. obj_desc->common_field.start_field_bit_offset) <
  799. ACPI_INTEGER_BIT_SIZE) {
  800. merged_datum =
  801. raw_datum >> (access_bit_width -
  802. obj_desc->common_field.
  803. start_field_bit_offset);
  804. } else {
  805. merged_datum = 0;
  806. }
  807. mask = width_mask;
  808. if (i == datum_count) {
  809. break;
  810. }
  811. /* Get the next input datum from the buffer */
  812. buffer_offset += obj_desc->common_field.access_byte_width;
  813. ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
  814. ACPI_MIN(obj_desc->common_field.access_byte_width,
  815. buffer_length - buffer_offset));
  816. merged_datum |=
  817. raw_datum << obj_desc->common_field.start_field_bit_offset;
  818. }
  819. /* Mask off any extra bits in the last datum */
  820. buffer_tail_bits = (obj_desc->common_field.bit_length +
  821. obj_desc->common_field.start_field_bit_offset) %
  822. access_bit_width;
  823. if (buffer_tail_bits) {
  824. mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  825. }
  826. /* Write the last datum to the field */
  827. merged_datum &= mask;
  828. status = acpi_ex_write_with_update_rule(obj_desc,
  829. mask, merged_datum,
  830. field_offset);
  831. exit:
  832. /* Free temporary buffer if we used one */
  833. if (new_buffer) {
  834. ACPI_FREE(new_buffer);
  835. }
  836. return_ACPI_STATUS(status);
  837. }