1 /******************************************************************************
2 *
3 * Module Name: exfldio - Aml Field I/O
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2004, R. Byron Moore
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acevents.h>
49 #include <acpi/acdispat.h>
50
51
52 #define _COMPONENT ACPI_EXECUTER
53 ACPI_MODULE_NAME ("exfldio")
54
55
56 /*******************************************************************************
57 *
58 * FUNCTION: acpi_ex_setup_region
59 *
60 * PARAMETERS: *obj_desc - Field to be read or written
61 * field_datum_byte_offset - Byte offset of this datum within the
62 * parent field
63 *
64 * RETURN: Status
65 *
66 * DESCRIPTION: Common processing for acpi_ex_extract_from_field and
67 * acpi_ex_insert_into_field. Initialize the Region if necessary and
68 * validate the request.
69 *
70 ******************************************************************************/
71
72 acpi_status
acpi_ex_setup_region(union acpi_operand_object * obj_desc,u32 field_datum_byte_offset)73 acpi_ex_setup_region (
74 union acpi_operand_object *obj_desc,
75 u32 field_datum_byte_offset)
76 {
77 acpi_status status = AE_OK;
78 union acpi_operand_object *rgn_desc;
79
80
81 ACPI_FUNCTION_TRACE_U32 ("ex_setup_region", field_datum_byte_offset);
82
83
84 rgn_desc = obj_desc->common_field.region_obj;
85
86 /* We must have a valid region */
87
88 if (ACPI_GET_OBJECT_TYPE (rgn_desc) != ACPI_TYPE_REGION) {
89 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %X (%s)\n",
90 ACPI_GET_OBJECT_TYPE (rgn_desc),
91 acpi_ut_get_object_type_name (rgn_desc)));
92
93 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
94 }
95
96 /*
97 * If the Region Address and Length have not been previously evaluated,
98 * evaluate them now and save the results.
99 */
100 if (!(rgn_desc->common.flags & AOPOBJ_DATA_VALID)) {
101 status = acpi_ds_get_region_arguments (rgn_desc);
102 if (ACPI_FAILURE (status)) {
103 return_ACPI_STATUS (status);
104 }
105 }
106
107 if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) {
108 /* SMBus has a non-linear address space */
109
110 return_ACPI_STATUS (AE_OK);
111 }
112
113 #ifdef ACPI_UNDER_DEVELOPMENT
114 /*
115 * If the Field access is any_acc, we can now compute the optimal
116 * access (because we know know the length of the parent region)
117 */
118 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
119 if (ACPI_FAILURE (status)) {
120 return_ACPI_STATUS (status);
121 }
122 }
123 #endif
124
125 /*
126 * Validate the request. The entire request from the byte offset for a
127 * length of one field datum (access width) must fit within the region.
128 * (Region length is specified in bytes)
129 */
130 if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset
131 + field_datum_byte_offset
132 + obj_desc->common_field.access_byte_width)) {
133 if (rgn_desc->region.length < obj_desc->common_field.access_byte_width) {
134 /*
135 * This is the case where the access_type (acc_word, etc.) is wider
136 * than the region itself. For example, a region of length one
137 * byte, and a field with Dword access specified.
138 */
139 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
140 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n",
141 acpi_ut_get_node_name (obj_desc->common_field.node),
142 obj_desc->common_field.access_byte_width,
143 acpi_ut_get_node_name (rgn_desc->region.node), rgn_desc->region.length));
144 }
145
146 /*
147 * Offset rounded up to next multiple of field width
148 * exceeds region length, indicate an error
149 */
150 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
151 "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n",
152 acpi_ut_get_node_name (obj_desc->common_field.node),
153 obj_desc->common_field.base_byte_offset,
154 field_datum_byte_offset, obj_desc->common_field.access_byte_width,
155 acpi_ut_get_node_name (rgn_desc->region.node), rgn_desc->region.length));
156
157 if (!acpi_strict) {
158 /*
159 * Allow access to the field if it is within the region size
160 * rounded up to a multiple of the access byte width. This
161 * overcomes "off-by-one" programming errors in the AML often
162 * found in Toshiba laptops. These errors were allowed by
163 * the Microsoft ASL compiler.
164 */
165 u32 rounded_length = ACPI_ROUND_UP(rgn_desc->region.length,
166 obj_desc->common_field.access_byte_width);
167
168 if (rounded_length < (obj_desc->common_field.base_byte_offset
169 + field_datum_byte_offset
170 + obj_desc->common_field.access_byte_width)) {
171 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
172 } else {
173 static int warn_once = 1;
174 if (warn_once) {
175 // Could also associate a flag with each field, and
176 // warn once for each field.
177 ACPI_REPORT_WARNING((
178 "The ACPI AML in your computer contains errors, "
179 "please nag the manufacturer to correct it.\n"));
180 ACPI_REPORT_WARNING((
181 "Allowing relaxed access to fields; "
182 "turn on CONFIG_ACPI_DEBUG for details.\n"));
183 warn_once = 0;
184 }
185 return_ACPI_STATUS (AE_OK);
186 }
187 }
188 else {
189 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
190 }
191 }
192
193 return_ACPI_STATUS (AE_OK);
194 }
195
196
197 /*******************************************************************************
198 *
199 * FUNCTION: acpi_ex_access_region
200 *
201 * PARAMETERS: *obj_desc - Field to be read
202 * field_datum_byte_offset - Byte offset of this datum within the
203 * parent field
204 * *Value - Where to store value (must at least
205 * the size of acpi_integer)
206 * Function - Read or Write flag plus other region-
207 * dependent flags
208 *
209 * RETURN: Status
210 *
211 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
212 *
213 ******************************************************************************/
214
215 acpi_status
acpi_ex_access_region(union acpi_operand_object * obj_desc,u32 field_datum_byte_offset,acpi_integer * value,u32 function)216 acpi_ex_access_region (
217 union acpi_operand_object *obj_desc,
218 u32 field_datum_byte_offset,
219 acpi_integer *value,
220 u32 function)
221 {
222 acpi_status status;
223 union acpi_operand_object *rgn_desc;
224 acpi_physical_address address;
225
226
227 ACPI_FUNCTION_TRACE ("ex_access_region");
228
229
230 /*
231 * Ensure that the region operands are fully evaluated and verify
232 * the validity of the request
233 */
234 status = acpi_ex_setup_region (obj_desc, field_datum_byte_offset);
235 if (ACPI_FAILURE (status)) {
236 return_ACPI_STATUS (status);
237 }
238
239 /*
240 * The physical address of this field datum is:
241 *
242 * 1) The base of the region, plus
243 * 2) The base offset of the field, plus
244 * 3) The current offset into the field
245 */
246 rgn_desc = obj_desc->common_field.region_obj;
247 address = rgn_desc->region.address
248 + obj_desc->common_field.base_byte_offset
249 + field_datum_byte_offset;
250
251 if ((function & ACPI_IO_MASK) == ACPI_READ) {
252 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
253 }
254 else {
255 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
256 }
257
258 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
259 " Region [%s:%X], Width %X, byte_base %X, Offset %X at %8.8X%8.8X\n",
260 acpi_ut_get_region_name (rgn_desc->region.space_id),
261 rgn_desc->region.space_id,
262 obj_desc->common_field.access_byte_width,
263 obj_desc->common_field.base_byte_offset,
264 field_datum_byte_offset,
265 ACPI_FORMAT_UINT64 (address)));
266
267 /* Invoke the appropriate address_space/op_region handler */
268
269 status = acpi_ev_address_space_dispatch (rgn_desc, function,
270 address, ACPI_MUL_8 (obj_desc->common_field.access_byte_width), value);
271
272 if (ACPI_FAILURE (status)) {
273 if (status == AE_NOT_IMPLEMENTED) {
274 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
275 "Region %s(%X) not implemented\n",
276 acpi_ut_get_region_name (rgn_desc->region.space_id),
277 rgn_desc->region.space_id));
278 }
279 else if (status == AE_NOT_EXIST) {
280 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
281 "Region %s(%X) has no handler\n",
282 acpi_ut_get_region_name (rgn_desc->region.space_id),
283 rgn_desc->region.space_id));
284 }
285 }
286
287 return_ACPI_STATUS (status);
288 }
289
290
291 /*******************************************************************************
292 *
293 * FUNCTION: acpi_ex_register_overflow
294 *
295 * PARAMETERS: *obj_desc - Register(Field) to be written
296 * Value - Value to be stored
297 *
298 * RETURN: TRUE if value overflows the field, FALSE otherwise
299 *
300 * DESCRIPTION: Check if a value is out of range of the field being written.
301 * Used to check if the values written to Index and Bank registers
302 * are out of range. Normally, the value is simply truncated
303 * to fit the field, but this case is most likely a serious
304 * coding error in the ASL.
305 *
306 ******************************************************************************/
307
308 u8
acpi_ex_register_overflow(union acpi_operand_object * obj_desc,acpi_integer value)309 acpi_ex_register_overflow (
310 union acpi_operand_object *obj_desc,
311 acpi_integer value)
312 {
313
314 if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
315 /*
316 * The field is large enough to hold the maximum integer, so we can
317 * never overflow it.
318 */
319 return (FALSE);
320 }
321
322 if (value >= ((acpi_integer) 1 << obj_desc->common_field.bit_length)) {
323 /*
324 * The Value is larger than the maximum value that can fit into
325 * the register.
326 */
327 return (TRUE);
328 }
329
330 /* The Value will fit into the field with no truncation */
331
332 return (FALSE);
333 }
334
335
336 /*******************************************************************************
337 *
338 * FUNCTION: acpi_ex_field_datum_io
339 *
340 * PARAMETERS: *obj_desc - Field to be read
341 * field_datum_byte_offset - Byte offset of this datum within the
342 * parent field
343 * *Value - Where to store value (must be 64 bits)
344 * read_write - Read or Write flag
345 *
346 * RETURN: Status
347 *
348 * DESCRIPTION: Read or Write a single datum of a field. The field_type is
349 * demultiplexed here to handle the different types of fields
350 * (buffer_field, region_field, index_field, bank_field)
351 *
352 ******************************************************************************/
353
354 acpi_status
acpi_ex_field_datum_io(union acpi_operand_object * obj_desc,u32 field_datum_byte_offset,acpi_integer * value,u32 read_write)355 acpi_ex_field_datum_io (
356 union acpi_operand_object *obj_desc,
357 u32 field_datum_byte_offset,
358 acpi_integer *value,
359 u32 read_write)
360 {
361 acpi_status status;
362 acpi_integer local_value;
363
364
365 ACPI_FUNCTION_TRACE_U32 ("ex_field_datum_io", field_datum_byte_offset);
366
367
368 if (read_write == ACPI_READ) {
369 if (!value) {
370 local_value = 0;
371 value = &local_value; /* To support reads without saving return value */
372 }
373
374 /* Clear the entire return buffer first, [Very Important!] */
375
376 *value = 0;
377 }
378
379 /*
380 * The four types of fields are:
381 *
382 * buffer_field - Read/write from/to a Buffer
383 * region_field - Read/write from/to a Operation Region.
384 * bank_field - Write to a Bank Register, then read/write from/to an op_region
385 * index_field - Write to an Index Register, then read/write from/to a Data Register
386 */
387 switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
388 case ACPI_TYPE_BUFFER_FIELD:
389 /*
390 * If the buffer_field arguments have not been previously evaluated,
391 * evaluate them now and save the results.
392 */
393 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
394 status = acpi_ds_get_buffer_field_arguments (obj_desc);
395 if (ACPI_FAILURE (status)) {
396 return_ACPI_STATUS (status);
397 }
398 }
399
400 if (read_write == ACPI_READ) {
401 /*
402 * Copy the data from the source buffer.
403 * Length is the field width in bytes.
404 */
405 ACPI_MEMCPY (value, (obj_desc->buffer_field.buffer_obj)->buffer.pointer
406 + obj_desc->buffer_field.base_byte_offset
407 + field_datum_byte_offset,
408 obj_desc->common_field.access_byte_width);
409 }
410 else {
411 /*
412 * Copy the data to the target buffer.
413 * Length is the field width in bytes.
414 */
415 ACPI_MEMCPY ((obj_desc->buffer_field.buffer_obj)->buffer.pointer
416 + obj_desc->buffer_field.base_byte_offset
417 + field_datum_byte_offset,
418 value, obj_desc->common_field.access_byte_width);
419 }
420
421 status = AE_OK;
422 break;
423
424
425 case ACPI_TYPE_LOCAL_BANK_FIELD:
426
427 /* Ensure that the bank_value is not beyond the capacity of the register */
428
429 if (acpi_ex_register_overflow (obj_desc->bank_field.bank_obj,
430 (acpi_integer) obj_desc->bank_field.value)) {
431 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
432 }
433
434 /*
435 * For bank_fields, we must write the bank_value to the bank_register
436 * (itself a region_field) before we can access the data.
437 */
438 status = acpi_ex_insert_into_field (obj_desc->bank_field.bank_obj,
439 &obj_desc->bank_field.value,
440 sizeof (obj_desc->bank_field.value));
441 if (ACPI_FAILURE (status)) {
442 return_ACPI_STATUS (status);
443 }
444
445 /*
446 * Now that the Bank has been selected, fall through to the
447 * region_field case and write the datum to the Operation Region
448 */
449
450 /*lint -fallthrough */
451
452
453 case ACPI_TYPE_LOCAL_REGION_FIELD:
454 /*
455 * For simple region_fields, we just directly access the owning
456 * Operation Region.
457 */
458 status = acpi_ex_access_region (obj_desc, field_datum_byte_offset, value,
459 read_write);
460 break;
461
462
463 case ACPI_TYPE_LOCAL_INDEX_FIELD:
464
465
466 /* Ensure that the index_value is not beyond the capacity of the register */
467
468 if (acpi_ex_register_overflow (obj_desc->index_field.index_obj,
469 (acpi_integer) obj_desc->index_field.value)) {
470 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
471 }
472
473 /* Write the index value to the index_register (itself a region_field) */
474
475 field_datum_byte_offset += obj_desc->index_field.value;
476
477 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
478 "Write to Index Register: Value %8.8X\n",
479 field_datum_byte_offset));
480
481 status = acpi_ex_insert_into_field (obj_desc->index_field.index_obj,
482 &field_datum_byte_offset,
483 sizeof (field_datum_byte_offset));
484 if (ACPI_FAILURE (status)) {
485 return_ACPI_STATUS (status);
486 }
487
488 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
489 "I/O to Data Register: value_ptr %p\n",
490 value));
491
492 if (read_write == ACPI_READ) {
493 /* Read the datum from the data_register */
494
495 status = acpi_ex_extract_from_field (obj_desc->index_field.data_obj,
496 value, sizeof (acpi_integer));
497 }
498 else {
499 /* Write the datum to the data_register */
500
501 status = acpi_ex_insert_into_field (obj_desc->index_field.data_obj,
502 value, sizeof (acpi_integer));
503 }
504 break;
505
506
507 default:
508
509 ACPI_REPORT_ERROR (("Wrong object type in field I/O %X\n",
510 ACPI_GET_OBJECT_TYPE (obj_desc)));
511 status = AE_AML_INTERNAL;
512 break;
513 }
514
515 if (ACPI_SUCCESS (status)) {
516 if (read_write == ACPI_READ) {
517 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Read %8.8X%8.8X, Width %d\n",
518 ACPI_FORMAT_UINT64 (*value),
519 obj_desc->common_field.access_byte_width));
520 }
521 else {
522 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Written %8.8X%8.8X, Width %d\n",
523 ACPI_FORMAT_UINT64 (*value),
524 obj_desc->common_field.access_byte_width));
525 }
526 }
527
528 return_ACPI_STATUS (status);
529 }
530
531
532 /*******************************************************************************
533 *
534 * FUNCTION: acpi_ex_write_with_update_rule
535 *
536 * PARAMETERS: *obj_desc - Field to be set
537 * Value - Value to store
538 *
539 * RETURN: Status
540 *
541 * DESCRIPTION: Apply the field update rule to a field write
542 *
543 ******************************************************************************/
544
545 acpi_status
acpi_ex_write_with_update_rule(union acpi_operand_object * obj_desc,acpi_integer mask,acpi_integer field_value,u32 field_datum_byte_offset)546 acpi_ex_write_with_update_rule (
547 union acpi_operand_object *obj_desc,
548 acpi_integer mask,
549 acpi_integer field_value,
550 u32 field_datum_byte_offset)
551 {
552 acpi_status status = AE_OK;
553 acpi_integer merged_value;
554 acpi_integer current_value;
555
556
557 ACPI_FUNCTION_TRACE_U32 ("ex_write_with_update_rule", mask);
558
559
560 /* Start with the new bits */
561
562 merged_value = field_value;
563
564 /* If the mask is all ones, we don't need to worry about the update rule */
565
566 if (mask != ACPI_INTEGER_MAX) {
567 /* Decode the update rule */
568
569 switch (obj_desc->common_field.field_flags & AML_FIELD_UPDATE_RULE_MASK) {
570 case AML_FIELD_UPDATE_PRESERVE:
571 /*
572 * Check if update rule needs to be applied (not if mask is all
573 * ones) The left shift drops the bits we want to ignore.
574 */
575 if ((~mask << (ACPI_MUL_8 (sizeof (mask)) -
576 ACPI_MUL_8 (obj_desc->common_field.access_byte_width))) != 0) {
577 /*
578 * Read the current contents of the byte/word/dword containing
579 * the field, and merge with the new field value.
580 */
581 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
582 ¤t_value, ACPI_READ);
583 if (ACPI_FAILURE (status)) {
584 return_ACPI_STATUS (status);
585 }
586
587 merged_value |= (current_value & ~mask);
588 }
589 break;
590
591 case AML_FIELD_UPDATE_WRITE_AS_ONES:
592
593 /* Set positions outside the field to all ones */
594
595 merged_value |= ~mask;
596 break;
597
598 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
599
600 /* Set positions outside the field to all zeros */
601
602 merged_value &= mask;
603 break;
604
605 default:
606
607 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
608 "write_with_update_rule: Unknown update_rule setting: %X\n",
609 (obj_desc->common_field.field_flags & AML_FIELD_UPDATE_RULE_MASK)));
610 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
611 }
612 }
613
614 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
615 "Mask %8.8X%8.8X, datum_offset %X, Width %X, Value %8.8X%8.8X, merged_value %8.8X%8.8X\n",
616 ACPI_FORMAT_UINT64 (mask),
617 field_datum_byte_offset,
618 obj_desc->common_field.access_byte_width,
619 ACPI_FORMAT_UINT64 (field_value),
620 ACPI_FORMAT_UINT64 (merged_value)));
621
622 /* Write the merged value */
623
624 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
625 &merged_value, ACPI_WRITE);
626
627 return_ACPI_STATUS (status);
628 }
629
630
631 /*******************************************************************************
632 *
633 * FUNCTION: acpi_ex_get_buffer_datum
634 *
635 * PARAMETERS: Datum - Where the Datum is returned
636 * Buffer - Raw field buffer
637 * buffer_length - Entire length (used for big-endian only)
638 * byte_granularity - 1/2/4/8 Granularity of the field
639 * (aka Datum Size)
640 * buffer_offset - Datum offset into the buffer
641 *
642 * RETURN: none
643 *
644 * DESCRIPTION: Get a datum from the buffer according to the buffer field
645 * byte granularity
646 *
647 ******************************************************************************/
648
649 void
acpi_ex_get_buffer_datum(acpi_integer * datum,void * buffer,u32 buffer_length,u32 byte_granularity,u32 buffer_offset)650 acpi_ex_get_buffer_datum (
651 acpi_integer *datum,
652 void *buffer,
653 u32 buffer_length,
654 u32 byte_granularity,
655 u32 buffer_offset)
656 {
657 u32 index;
658
659
660 ACPI_FUNCTION_TRACE_U32 ("ex_get_buffer_datum", byte_granularity);
661
662
663 /* Get proper index into buffer (handles big/little endian) */
664
665 index = ACPI_BUFFER_INDEX (buffer_length, buffer_offset, byte_granularity);
666
667 /* Move the requested number of bytes */
668
669 switch (byte_granularity) {
670 case ACPI_FIELD_BYTE_GRANULARITY:
671
672 *datum = ((u8 *) buffer) [index];
673 break;
674
675 case ACPI_FIELD_WORD_GRANULARITY:
676
677 ACPI_MOVE_16_TO_64 (datum, &(((u16 *) buffer) [index]));
678 break;
679
680 case ACPI_FIELD_DWORD_GRANULARITY:
681
682 ACPI_MOVE_32_TO_64 (datum, &(((u32 *) buffer) [index]));
683 break;
684
685 case ACPI_FIELD_QWORD_GRANULARITY:
686
687 ACPI_MOVE_64_TO_64 (datum, &(((u64 *) buffer) [index]));
688 break;
689
690 default:
691 /* Should not get here */
692 break;
693 }
694
695 return_VOID;
696 }
697
698
699 /*******************************************************************************
700 *
701 * FUNCTION: acpi_ex_set_buffer_datum
702 *
703 * PARAMETERS: merged_datum - Value to store
704 * Buffer - Receiving buffer
705 * buffer_length - Entire length (used for big-endian only)
706 * byte_granularity - 1/2/4/8 Granularity of the field
707 * (aka Datum Size)
708 * buffer_offset - Datum offset into the buffer
709 *
710 * RETURN: none
711 *
712 * DESCRIPTION: Store the merged datum to the buffer according to the
713 * byte granularity
714 *
715 ******************************************************************************/
716
717 void
acpi_ex_set_buffer_datum(acpi_integer merged_datum,void * buffer,u32 buffer_length,u32 byte_granularity,u32 buffer_offset)718 acpi_ex_set_buffer_datum (
719 acpi_integer merged_datum,
720 void *buffer,
721 u32 buffer_length,
722 u32 byte_granularity,
723 u32 buffer_offset)
724 {
725 u32 index;
726
727
728 ACPI_FUNCTION_TRACE_U32 ("ex_set_buffer_datum", byte_granularity);
729
730
731 /* Get proper index into buffer (handles big/little endian) */
732
733 index = ACPI_BUFFER_INDEX (buffer_length, buffer_offset, byte_granularity);
734
735 /* Move the requested number of bytes */
736
737 switch (byte_granularity) {
738 case ACPI_FIELD_BYTE_GRANULARITY:
739
740 ((u8 *) buffer) [index] = (u8) merged_datum;
741 break;
742
743 case ACPI_FIELD_WORD_GRANULARITY:
744
745 ACPI_MOVE_64_TO_16 (&(((u16 *) buffer)[index]), &merged_datum);
746 break;
747
748 case ACPI_FIELD_DWORD_GRANULARITY:
749
750 ACPI_MOVE_64_TO_32 (&(((u32 *) buffer)[index]), &merged_datum);
751 break;
752
753 case ACPI_FIELD_QWORD_GRANULARITY:
754
755 ACPI_MOVE_64_TO_64 (&(((u64 *) buffer)[index]), &merged_datum);
756 break;
757
758 default:
759 /* Should not get here */
760 break;
761 }
762
763 return_VOID;
764 }
765
766
767 /*******************************************************************************
768 *
769 * FUNCTION: acpi_ex_extract_from_field
770 *
771 * PARAMETERS: *obj_desc - Field to be read
772 * *Value - Where to store value
773 *
774 * RETURN: Status
775 *
776 * DESCRIPTION: Retrieve the value of the given field
777 *
778 ******************************************************************************/
779
780 acpi_status
acpi_ex_extract_from_field(union acpi_operand_object * obj_desc,void * buffer,u32 buffer_length)781 acpi_ex_extract_from_field (
782 union acpi_operand_object *obj_desc,
783 void *buffer,
784 u32 buffer_length)
785 {
786 acpi_status status;
787 u32 field_datum_byte_offset;
788 u32 buffer_datum_offset;
789 acpi_integer previous_raw_datum = 0;
790 acpi_integer this_raw_datum = 0;
791 acpi_integer merged_datum = 0;
792 u32 byte_field_length;
793 u32 datum_count;
794 u32 i;
795
796
797 ACPI_FUNCTION_TRACE ("ex_extract_from_field");
798
799
800 /*
801 * The field must fit within the caller's buffer
802 */
803 byte_field_length = ACPI_ROUND_BITS_UP_TO_BYTES (obj_desc->common_field.bit_length);
804 if (byte_field_length > buffer_length) {
805 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
806 "Field size %X (bytes) too large for buffer (%X)\n",
807 byte_field_length, buffer_length));
808
809 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
810 }
811
812 /* Convert field byte count to datum count, round up if necessary */
813
814 datum_count = ACPI_ROUND_UP_TO (byte_field_length,
815 obj_desc->common_field.access_byte_width);
816
817 /*
818 * If the field is not aligned on a datum boundary and does not
819 * fit within a single datum, we must read an extra datum.
820 *
821 * We could just split the aligned and non-aligned cases since the
822 * aligned case is so very simple, but this would require more code.
823 */
824 if ((obj_desc->common_field.end_field_valid_bits != 0) &&
825 (!(obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM))) {
826 datum_count++;
827 }
828
829 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
830 "byte_len %X, datum_len %X, byte_gran %X\n",
831 byte_field_length, datum_count,obj_desc->common_field.access_byte_width));
832
833 /*
834 * Clear the caller's buffer (the whole buffer length as given)
835 * This is very important, especially in the cases where the buffer
836 * is longer than the size of the field.
837 */
838 ACPI_MEMSET (buffer, 0, buffer_length);
839
840 field_datum_byte_offset = 0;
841 buffer_datum_offset= 0;
842
843 /* Read the entire field */
844
845 for (i = 0; i < datum_count; i++) {
846 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
847 &this_raw_datum, ACPI_READ);
848 if (ACPI_FAILURE (status)) {
849 return_ACPI_STATUS (status);
850 }
851
852 /* We might actually be done if the request fits in one datum */
853
854 if ((datum_count == 1) &&
855 (obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM)) {
856 /* 1) Shift the valid data bits down to start at bit 0 */
857
858 merged_datum = (this_raw_datum >> obj_desc->common_field.start_field_bit_offset);
859
860 /* 2) Mask off any upper unused bits (bits not part of the field) */
861
862 if (obj_desc->common_field.end_buffer_valid_bits) {
863 merged_datum &= ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_buffer_valid_bits);
864 }
865
866 /* Store the datum to the caller buffer */
867
868 acpi_ex_set_buffer_datum (merged_datum, buffer, buffer_length,
869 obj_desc->common_field.access_byte_width, buffer_datum_offset);
870
871 return_ACPI_STATUS (AE_OK);
872 }
873
874 /* Special handling for the last datum to ignore extra bits */
875
876 if ((i >= (datum_count -1)) &&
877 (obj_desc->common_field.end_field_valid_bits)) {
878 /*
879 * This is the last iteration of the loop. We need to clear
880 * any unused bits (bits that are not part of this field) before
881 * we store the final merged datum into the caller buffer.
882 */
883 this_raw_datum &=
884 ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_field_valid_bits);
885 }
886
887 /*
888 * Create the (possibly) merged datum to be stored to the caller buffer
889 */
890 if (obj_desc->common_field.start_field_bit_offset == 0) {
891 /* Field is not skewed and we can just copy the datum */
892
893 acpi_ex_set_buffer_datum (this_raw_datum, buffer, buffer_length,
894 obj_desc->common_field.access_byte_width, buffer_datum_offset);
895 buffer_datum_offset++;
896 }
897 else {
898 /* Not aligned -- on the first iteration, just save the datum */
899
900 if (i != 0) {
901 /*
902 * Put together the appropriate bits of the two raw data to make a
903 * single complete field datum
904 *
905 * 1) Normalize the first datum down to bit 0
906 */
907 merged_datum = (previous_raw_datum >> obj_desc->common_field.start_field_bit_offset);
908
909 /* 2) Insert the second datum "above" the first datum */
910
911 merged_datum |= (this_raw_datum << obj_desc->common_field.datum_valid_bits);
912
913 acpi_ex_set_buffer_datum (merged_datum, buffer, buffer_length,
914 obj_desc->common_field.access_byte_width, buffer_datum_offset);
915 buffer_datum_offset++;
916 }
917
918 /*
919 * Save the raw datum that was just acquired since it may contain bits
920 * of the *next* field datum
921 */
922 previous_raw_datum = this_raw_datum;
923 }
924
925 field_datum_byte_offset += obj_desc->common_field.access_byte_width;
926 }
927
928 /* For non-aligned case, there is one last datum to insert */
929
930 if (obj_desc->common_field.start_field_bit_offset != 0) {
931 merged_datum = (this_raw_datum >> obj_desc->common_field.start_field_bit_offset);
932
933 acpi_ex_set_buffer_datum (merged_datum, buffer, buffer_length,
934 obj_desc->common_field.access_byte_width, buffer_datum_offset);
935 }
936
937 return_ACPI_STATUS (AE_OK);
938 }
939
940
941 /*******************************************************************************
942 *
943 * FUNCTION: acpi_ex_insert_into_field
944 *
945 * PARAMETERS: *obj_desc - Field to be set
946 * Buffer - Value to store
947 *
948 * RETURN: Status
949 *
950 * DESCRIPTION: Store the value into the given field
951 *
952 ******************************************************************************/
953
954 acpi_status
acpi_ex_insert_into_field(union acpi_operand_object * obj_desc,void * buffer,u32 buffer_length)955 acpi_ex_insert_into_field (
956 union acpi_operand_object *obj_desc,
957 void *buffer,
958 u32 buffer_length)
959 {
960 acpi_status status;
961 u32 field_datum_byte_offset;
962 u32 datum_offset;
963 acpi_integer mask;
964 acpi_integer merged_datum;
965 acpi_integer previous_raw_datum;
966 acpi_integer this_raw_datum;
967 u32 byte_field_length;
968 u32 datum_count;
969
970
971 ACPI_FUNCTION_TRACE ("ex_insert_into_field");
972
973
974 /*
975 * Incoming buffer must be at least as long as the field, we do not
976 * allow "partial" field writes. We do not care if the buffer is
977 * larger than the field, this typically happens when an integer is
978 * written to a field that is actually smaller than an integer.
979 */
980 byte_field_length = ACPI_ROUND_BITS_UP_TO_BYTES (
981 obj_desc->common_field.bit_length);
982 if (buffer_length < byte_field_length) {
983 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
984 "Buffer length %X too small for field %X\n",
985 buffer_length, byte_field_length));
986
987 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
988 }
989
990 byte_field_length = ACPI_ROUND_BITS_UP_TO_BYTES (
991 obj_desc->common_field.start_field_bit_offset +
992 obj_desc->common_field.bit_length);
993
994 /* Convert byte count to datum count, round up if necessary */
995
996 datum_count = ACPI_ROUND_UP_TO (byte_field_length,
997 obj_desc->common_field.access_byte_width);
998
999 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
1000 "Bytes %X, Datums %X, byte_gran %X\n",
1001 byte_field_length, datum_count, obj_desc->common_field.access_byte_width));
1002
1003 /*
1004 * Break the request into up to three parts (similar to an I/O request):
1005 * 1) non-aligned part at start
1006 * 2) aligned part in middle
1007 * 3) non-aligned part at the end
1008 */
1009 field_datum_byte_offset = 0;
1010 datum_offset= 0;
1011
1012 /* Get a single datum from the caller's buffer */
1013
1014 acpi_ex_get_buffer_datum (&previous_raw_datum, buffer, buffer_length,
1015 obj_desc->common_field.access_byte_width, datum_offset);
1016
1017 /*
1018 * Part1:
1019 * Write a partial field datum if field does not begin on a datum boundary
1020 * Note: The code in this section also handles the aligned case
1021 *
1022 * Construct Mask with 1 bits where the field is, 0 bits elsewhere
1023 * (Only the bottom 5 bits of bit_length are valid for a shift operation)
1024 *
1025 * Mask off bits that are "below" the field (if any)
1026 */
1027 mask = ACPI_MASK_BITS_BELOW (obj_desc->common_field.start_field_bit_offset);
1028
1029 /* If the field fits in one datum, may need to mask upper bits */
1030
1031 if ((obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM) &&
1032 obj_desc->common_field.end_field_valid_bits) {
1033 /* There are bits above the field, mask them off also */
1034
1035 mask &= ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_field_valid_bits);
1036 }
1037
1038 /* Shift and mask the value into the field position */
1039
1040 merged_datum = (previous_raw_datum << obj_desc->common_field.start_field_bit_offset);
1041 merged_datum &= mask;
1042
1043 /* Apply the update rule (if necessary) and write the datum to the field */
1044
1045 status = acpi_ex_write_with_update_rule (obj_desc, mask, merged_datum,
1046 field_datum_byte_offset);
1047 if (ACPI_FAILURE (status)) {
1048 return_ACPI_STATUS (status);
1049 }
1050
1051 /* We just wrote the first datum */
1052
1053 datum_offset++;
1054
1055 /* If the entire field fits within one datum, we are done. */
1056
1057 if ((datum_count == 1) &&
1058 (obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM)) {
1059 return_ACPI_STATUS (AE_OK);
1060 }
1061
1062 /*
1063 * Part2:
1064 * Write the aligned data.
1065 *
1066 * We don't need to worry about the update rule for these data, because
1067 * all of the bits in each datum are part of the field.
1068 *
1069 * The last datum must be special cased because it might contain bits
1070 * that are not part of the field -- therefore the "update rule" must be
1071 * applied in Part3 below.
1072 */
1073 while (datum_offset < datum_count) {
1074 field_datum_byte_offset += obj_desc->common_field.access_byte_width;
1075
1076 /*
1077 * Get the next raw buffer datum. It may contain bits of the previous
1078 * field datum
1079 */
1080 acpi_ex_get_buffer_datum (&this_raw_datum, buffer, buffer_length,
1081 obj_desc->common_field.access_byte_width, datum_offset);
1082
1083 /* Create the field datum based on the field alignment */
1084
1085 if (obj_desc->common_field.start_field_bit_offset != 0) {
1086 /*
1087 * Put together appropriate bits of the two raw buffer data to make
1088 * a single complete field datum
1089 */
1090 merged_datum =
1091 (previous_raw_datum >> obj_desc->common_field.datum_valid_bits) |
1092 (this_raw_datum << obj_desc->common_field.start_field_bit_offset);
1093 }
1094 else {
1095 /* Field began aligned on datum boundary */
1096
1097 merged_datum = this_raw_datum;
1098 }
1099
1100 /*
1101 * Special handling for the last datum if the field does NOT end on
1102 * a datum boundary. Update Rule must be applied to the bits outside
1103 * the field.
1104 */
1105 datum_offset++;
1106 if ((datum_offset == datum_count) &&
1107 (obj_desc->common_field.end_field_valid_bits)) {
1108 /*
1109 * If there are dangling non-aligned bits, perform one more merged write
1110 * Else - field is aligned at the end, no need for any more writes
1111 */
1112
1113 /*
1114 * Part3:
1115 * This is the last datum and the field does not end on a datum boundary.
1116 * Build the partial datum and write with the update rule.
1117 *
1118 * Mask off the unused bits above (after) the end-of-field
1119 */
1120 mask = ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_field_valid_bits);
1121 merged_datum &= mask;
1122
1123 /* Write the last datum with the update rule */
1124
1125 status = acpi_ex_write_with_update_rule (obj_desc, mask, merged_datum,
1126 field_datum_byte_offset);
1127 if (ACPI_FAILURE (status)) {
1128 return_ACPI_STATUS (status);
1129 }
1130 }
1131 else {
1132 /* Normal (aligned) case -- write the completed datum */
1133
1134 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
1135 &merged_datum, ACPI_WRITE);
1136 if (ACPI_FAILURE (status)) {
1137 return_ACPI_STATUS (status);
1138 }
1139 }
1140
1141 /*
1142 * Save the most recent datum since it may contain bits of the *next*
1143 * field datum. Update current byte offset.
1144 */
1145 previous_raw_datum = this_raw_datum;
1146 }
1147
1148 return_ACPI_STATUS (status);
1149 }
1150
1151
1152