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