exfldio.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. /******************************************************************************
  2. *
  3. * Module Name: exfldio - Aml Field I/O
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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) "
  162. "too large for region [%4.4s] (length %u)",
  163. acpi_ut_get_node_name(obj_desc->
  164. common_field.node),
  165. obj_desc->common_field.access_byte_width,
  166. acpi_ut_get_node_name(rgn_desc->region.
  167. node),
  168. rgn_desc->region.length));
  169. }
  170. /*
  171. * Offset rounded up to next multiple of field width
  172. * exceeds region length, indicate an error
  173. */
  174. ACPI_ERROR((AE_INFO,
  175. "Field [%4.4s] Base+Offset+Width %u+%u+%u "
  176. "is beyond end of region [%4.4s] (length %u)",
  177. acpi_ut_get_node_name(obj_desc->common_field.node),
  178. obj_desc->common_field.base_byte_offset,
  179. field_datum_byte_offset,
  180. obj_desc->common_field.access_byte_width,
  181. acpi_ut_get_node_name(rgn_desc->region.node),
  182. rgn_desc->region.length));
  183. return_ACPI_STATUS(AE_AML_REGION_LIMIT);
  184. }
  185. return_ACPI_STATUS(AE_OK);
  186. }
  187. /*******************************************************************************
  188. *
  189. * FUNCTION: acpi_ex_access_region
  190. *
  191. * PARAMETERS: obj_desc - Field to be read
  192. * field_datum_byte_offset - Byte offset of this datum within the
  193. * parent field
  194. * value - Where to store value (must at least
  195. * 64 bits)
  196. * function - Read or Write flag plus other region-
  197. * dependent flags
  198. *
  199. * RETURN: Status
  200. *
  201. * DESCRIPTION: Read or Write a single field datum to an Operation Region.
  202. *
  203. ******************************************************************************/
  204. acpi_status
  205. acpi_ex_access_region(union acpi_operand_object *obj_desc,
  206. u32 field_datum_byte_offset, u64 *value, u32 function)
  207. {
  208. acpi_status status;
  209. union acpi_operand_object *rgn_desc;
  210. u32 region_offset;
  211. ACPI_FUNCTION_TRACE(ex_access_region);
  212. /*
  213. * Ensure that the region operands are fully evaluated and verify
  214. * the validity of the request
  215. */
  216. status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
  217. if (ACPI_FAILURE(status)) {
  218. return_ACPI_STATUS(status);
  219. }
  220. /*
  221. * The physical address of this field datum is:
  222. *
  223. * 1) The base of the region, plus
  224. * 2) The base offset of the field, plus
  225. * 3) The current offset into the field
  226. */
  227. rgn_desc = obj_desc->common_field.region_obj;
  228. region_offset =
  229. obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
  230. if ((function & ACPI_IO_MASK) == ACPI_READ) {
  231. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
  232. } else {
  233. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
  234. }
  235. ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
  236. " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %8.8X%8.8X\n",
  237. acpi_ut_get_region_name(rgn_desc->region.
  238. space_id),
  239. rgn_desc->region.space_id,
  240. obj_desc->common_field.access_byte_width,
  241. obj_desc->common_field.base_byte_offset,
  242. field_datum_byte_offset,
  243. ACPI_FORMAT_UINT64(rgn_desc->region.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. 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. 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.field_flags &
  553. AML_FIELD_UPDATE_RULE_MASK)));
  554. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  555. }
  556. }
  557. ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
  558. "Mask %8.8X%8.8X, DatumOffset %X, Width %X, "
  559. "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 =
  567. acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
  568. &merged_value, ACPI_WRITE);
  569. return_ACPI_STATUS(status);
  570. }
  571. /*******************************************************************************
  572. *
  573. * FUNCTION: acpi_ex_extract_from_field
  574. *
  575. * PARAMETERS: obj_desc - Field to be read
  576. * buffer - Where to store the field data
  577. * buffer_length - Length of Buffer
  578. *
  579. * RETURN: Status
  580. *
  581. * DESCRIPTION: Retrieve the current value of the given field
  582. *
  583. ******************************************************************************/
  584. acpi_status
  585. acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
  586. void *buffer, u32 buffer_length)
  587. {
  588. acpi_status status;
  589. u64 raw_datum;
  590. u64 merged_datum;
  591. u32 field_offset = 0;
  592. u32 buffer_offset = 0;
  593. u32 buffer_tail_bits;
  594. u32 datum_count;
  595. u32 field_datum_count;
  596. u32 access_bit_width;
  597. u32 i;
  598. ACPI_FUNCTION_TRACE(ex_extract_from_field);
  599. /* Validate target buffer and clear it */
  600. if (buffer_length <
  601. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
  602. ACPI_ERROR((AE_INFO,
  603. "Field size %u (bits) is too large for buffer (%u)",
  604. obj_desc->common_field.bit_length, buffer_length));
  605. return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
  606. }
  607. memset(buffer, 0, buffer_length);
  608. access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
  609. /* Handle the simple case here */
  610. if ((obj_desc->common_field.start_field_bit_offset == 0) &&
  611. (obj_desc->common_field.bit_length == access_bit_width)) {
  612. if (buffer_length >= sizeof(u64)) {
  613. status =
  614. acpi_ex_field_datum_io(obj_desc, 0, buffer,
  615. ACPI_READ);
  616. } else {
  617. /* Use raw_datum (u64) to handle buffers < 64 bits */
  618. status =
  619. acpi_ex_field_datum_io(obj_desc, 0, &raw_datum,
  620. ACPI_READ);
  621. memcpy(buffer, &raw_datum, buffer_length);
  622. }
  623. return_ACPI_STATUS(status);
  624. }
  625. /* TBD: Move to common setup code */
  626. /* Field algorithm is limited to sizeof(u64), truncate if needed */
  627. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  628. obj_desc->common_field.access_byte_width = sizeof(u64);
  629. access_bit_width = sizeof(u64) * 8;
  630. }
  631. /* Compute the number of datums (access width data items) */
  632. datum_count =
  633. ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  634. access_bit_width);
  635. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  636. obj_desc->common_field.
  637. start_field_bit_offset,
  638. access_bit_width);
  639. /* Priming read from the field */
  640. status =
  641. acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
  642. ACPI_READ);
  643. if (ACPI_FAILURE(status)) {
  644. return_ACPI_STATUS(status);
  645. }
  646. merged_datum =
  647. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  648. /* Read the rest of the field */
  649. for (i = 1; i < field_datum_count; i++) {
  650. /* Get next input datum from the field */
  651. field_offset += obj_desc->common_field.access_byte_width;
  652. status =
  653. acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
  654. ACPI_READ);
  655. if (ACPI_FAILURE(status)) {
  656. return_ACPI_STATUS(status);
  657. }
  658. /*
  659. * Merge with previous datum if necessary.
  660. *
  661. * Note: Before the shift, check if the shift value will be larger than
  662. * the integer size. If so, there is no need to perform the operation.
  663. * This avoids the differences in behavior between different compilers
  664. * concerning shift values larger than the target data width.
  665. */
  666. if (access_bit_width -
  667. obj_desc->common_field.start_field_bit_offset <
  668. ACPI_INTEGER_BIT_SIZE) {
  669. merged_datum |=
  670. raw_datum << (access_bit_width -
  671. obj_desc->common_field.
  672. start_field_bit_offset);
  673. }
  674. if (i == datum_count) {
  675. break;
  676. }
  677. /* Write merged datum to target buffer */
  678. memcpy(((char *)buffer) + buffer_offset, &merged_datum,
  679. ACPI_MIN(obj_desc->common_field.access_byte_width,
  680. buffer_length - buffer_offset));
  681. buffer_offset += obj_desc->common_field.access_byte_width;
  682. merged_datum =
  683. raw_datum >> obj_desc->common_field.start_field_bit_offset;
  684. }
  685. /* Mask off any extra bits in the last datum */
  686. buffer_tail_bits = obj_desc->common_field.bit_length % access_bit_width;
  687. if (buffer_tail_bits) {
  688. merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  689. }
  690. /* Write the last datum to the buffer */
  691. memcpy(((char *)buffer) + buffer_offset, &merged_datum,
  692. ACPI_MIN(obj_desc->common_field.access_byte_width,
  693. buffer_length - buffer_offset));
  694. return_ACPI_STATUS(AE_OK);
  695. }
  696. /*******************************************************************************
  697. *
  698. * FUNCTION: acpi_ex_insert_into_field
  699. *
  700. * PARAMETERS: obj_desc - Field to be written
  701. * buffer - Data to be written
  702. * buffer_length - Length of Buffer
  703. *
  704. * RETURN: Status
  705. *
  706. * DESCRIPTION: Store the Buffer contents into the given field
  707. *
  708. ******************************************************************************/
  709. acpi_status
  710. acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
  711. void *buffer, u32 buffer_length)
  712. {
  713. void *new_buffer;
  714. acpi_status status;
  715. u64 mask;
  716. u64 width_mask;
  717. u64 merged_datum;
  718. u64 raw_datum = 0;
  719. u32 field_offset = 0;
  720. u32 buffer_offset = 0;
  721. u32 buffer_tail_bits;
  722. u32 datum_count;
  723. u32 field_datum_count;
  724. u32 access_bit_width;
  725. u32 required_length;
  726. u32 i;
  727. ACPI_FUNCTION_TRACE(ex_insert_into_field);
  728. /* Validate input buffer */
  729. new_buffer = NULL;
  730. required_length =
  731. ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
  732. /*
  733. * We must have a buffer that is at least as long as the field
  734. * we are writing to. This is because individual fields are
  735. * indivisible and partial writes are not supported -- as per
  736. * the ACPI specification.
  737. */
  738. if (buffer_length < required_length) {
  739. /* We need to create a new buffer */
  740. new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
  741. if (!new_buffer) {
  742. return_ACPI_STATUS(AE_NO_MEMORY);
  743. }
  744. /*
  745. * Copy the original data to the new buffer, starting
  746. * at Byte zero. All unused (upper) bytes of the
  747. * buffer will be 0.
  748. */
  749. memcpy((char *)new_buffer, (char *)buffer, buffer_length);
  750. buffer = new_buffer;
  751. buffer_length = required_length;
  752. }
  753. /* TBD: Move to common setup code */
  754. /* Algo is limited to sizeof(u64), so cut the access_byte_width */
  755. if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
  756. obj_desc->common_field.access_byte_width = sizeof(u64);
  757. }
  758. access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
  759. /* Create the bitmasks used for bit insertion */
  760. width_mask = ACPI_MASK_BITS_ABOVE_64(access_bit_width);
  761. mask = width_mask &
  762. ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
  763. /* Compute the number of datums (access width data items) */
  764. datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
  765. access_bit_width);
  766. field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
  767. obj_desc->common_field.
  768. start_field_bit_offset,
  769. access_bit_width);
  770. /* Get initial Datum from the input buffer */
  771. memcpy(&raw_datum, buffer,
  772. ACPI_MIN(obj_desc->common_field.access_byte_width,
  773. buffer_length - buffer_offset));
  774. merged_datum =
  775. raw_datum << obj_desc->common_field.start_field_bit_offset;
  776. /* Write the entire field */
  777. for (i = 1; i < field_datum_count; i++) {
  778. /* Write merged datum to the target field */
  779. merged_datum &= mask;
  780. status =
  781. acpi_ex_write_with_update_rule(obj_desc, mask, merged_datum,
  782. field_offset);
  783. if (ACPI_FAILURE(status)) {
  784. goto exit;
  785. }
  786. field_offset += obj_desc->common_field.access_byte_width;
  787. /*
  788. * Start new output datum by merging with previous input datum
  789. * if necessary.
  790. *
  791. * Note: Before the shift, check if the shift value will be larger than
  792. * the integer size. If so, there is no need to perform the operation.
  793. * This avoids the differences in behavior between different compilers
  794. * concerning shift values larger than the target data width.
  795. */
  796. if ((access_bit_width -
  797. obj_desc->common_field.start_field_bit_offset) <
  798. ACPI_INTEGER_BIT_SIZE) {
  799. merged_datum =
  800. raw_datum >> (access_bit_width -
  801. obj_desc->common_field.
  802. start_field_bit_offset);
  803. } else {
  804. merged_datum = 0;
  805. }
  806. mask = width_mask;
  807. if (i == datum_count) {
  808. break;
  809. }
  810. /* Get the next input datum from the buffer */
  811. buffer_offset += obj_desc->common_field.access_byte_width;
  812. memcpy(&raw_datum, ((char *)buffer) + buffer_offset,
  813. ACPI_MIN(obj_desc->common_field.access_byte_width,
  814. buffer_length - buffer_offset));
  815. merged_datum |=
  816. raw_datum << obj_desc->common_field.start_field_bit_offset;
  817. }
  818. /* Mask off any extra bits in the last datum */
  819. buffer_tail_bits = (obj_desc->common_field.bit_length +
  820. obj_desc->common_field.start_field_bit_offset) %
  821. access_bit_width;
  822. if (buffer_tail_bits) {
  823. mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
  824. }
  825. /* Write the last datum to the field */
  826. merged_datum &= mask;
  827. status =
  828. acpi_ex_write_with_update_rule(obj_desc, mask, merged_datum,
  829. field_offset);
  830. exit:
  831. /* Free temporary buffer if we used one */
  832. if (new_buffer) {
  833. ACPI_FREE(new_buffer);
  834. }
  835. return_ACPI_STATUS(status);
  836. }