exfldio.c 29 KB

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