exfldio.c 29 KB

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