Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux...
[linux-2.6-block.git] / drivers / acpi / acpica / rscalc.c
1 /*******************************************************************************
2  *
3  * Module Name: rscalc - Calculate stream and list lengths
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
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 "accommon.h"
46 #include "acresrc.h"
47 #include "acnamesp.h"
48
49 #define _COMPONENT          ACPI_RESOURCES
50 ACPI_MODULE_NAME("rscalc")
51
52 /* Local prototypes */
53 static u8 acpi_rs_count_set_bits(u16 bit_field);
54
55 static acpi_rs_length
56 acpi_rs_struct_option_length(struct acpi_resource_source *resource_source);
57
58 static u32
59 acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length);
60
61 /*******************************************************************************
62  *
63  * FUNCTION:    acpi_rs_count_set_bits
64  *
65  * PARAMETERS:  bit_field       - Field in which to count bits
66  *
67  * RETURN:      Number of bits set within the field
68  *
69  * DESCRIPTION: Count the number of bits set in a resource field. Used for
70  *              (Short descriptor) interrupt and DMA lists.
71  *
72  ******************************************************************************/
73
74 static u8 acpi_rs_count_set_bits(u16 bit_field)
75 {
76         u8 bits_set;
77
78         ACPI_FUNCTION_ENTRY();
79
80         for (bits_set = 0; bit_field; bits_set++) {
81
82                 /* Zero the least significant bit that is set */
83
84                 bit_field &= (u16) (bit_field - 1);
85         }
86
87         return (bits_set);
88 }
89
90 /*******************************************************************************
91  *
92  * FUNCTION:    acpi_rs_struct_option_length
93  *
94  * PARAMETERS:  resource_source     - Pointer to optional descriptor field
95  *
96  * RETURN:      Status
97  *
98  * DESCRIPTION: Common code to handle optional resource_source_index and
99  *              resource_source fields in some Large descriptors. Used during
100  *              list-to-stream conversion
101  *
102  ******************************************************************************/
103
104 static acpi_rs_length
105 acpi_rs_struct_option_length(struct acpi_resource_source *resource_source)
106 {
107         ACPI_FUNCTION_ENTRY();
108
109         /*
110          * If the resource_source string is valid, return the size of the string
111          * (string_length includes the NULL terminator) plus the size of the
112          * resource_source_index (1).
113          */
114         if (resource_source->string_ptr) {
115                 return ((acpi_rs_length) (resource_source->string_length + 1));
116         }
117
118         return (0);
119 }
120
121 /*******************************************************************************
122  *
123  * FUNCTION:    acpi_rs_stream_option_length
124  *
125  * PARAMETERS:  resource_length     - Length from the resource header
126  *              minimum_total_length - Minimum length of this resource, before
127  *                                    any optional fields. Includes header size
128  *
129  * RETURN:      Length of optional string (0 if no string present)
130  *
131  * DESCRIPTION: Common code to handle optional resource_source_index and
132  *              resource_source fields in some Large descriptors. Used during
133  *              stream-to-list conversion
134  *
135  ******************************************************************************/
136
137 static u32
138 acpi_rs_stream_option_length(u32 resource_length,
139                              u32 minimum_aml_resource_length)
140 {
141         u32 string_length = 0;
142
143         ACPI_FUNCTION_ENTRY();
144
145         /*
146          * The resource_source_index and resource_source are optional elements of some
147          * Large-type resource descriptors.
148          */
149
150         /*
151          * If the length of the actual resource descriptor is greater than the ACPI
152          * spec-defined minimum length, it means that a resource_source_index exists
153          * and is followed by a (required) null terminated string. The string length
154          * (including the null terminator) is the resource length minus the minimum
155          * length, minus one byte for the resource_source_index itself.
156          */
157         if (resource_length > minimum_aml_resource_length) {
158
159                 /* Compute the length of the optional string */
160
161                 string_length =
162                     resource_length - minimum_aml_resource_length - 1;
163         }
164
165         /*
166          * Round the length up to a multiple of the native word in order to
167          * guarantee that the entire resource descriptor is native word aligned
168          */
169         return ((u32) ACPI_ROUND_UP_TO_NATIVE_WORD(string_length));
170 }
171
172 /*******************************************************************************
173  *
174  * FUNCTION:    acpi_rs_get_aml_length
175  *
176  * PARAMETERS:  resource            - Pointer to the resource linked list
177  *              size_needed         - Where the required size is returned
178  *
179  * RETURN:      Status
180  *
181  * DESCRIPTION: Takes a linked list of internal resource descriptors and
182  *              calculates the size buffer needed to hold the corresponding
183  *              external resource byte stream.
184  *
185  ******************************************************************************/
186
187 acpi_status
188 acpi_rs_get_aml_length(struct acpi_resource * resource, acpi_size * size_needed)
189 {
190         acpi_size aml_size_needed = 0;
191         acpi_rs_length total_size;
192
193         ACPI_FUNCTION_TRACE(rs_get_aml_length);
194
195         /* Traverse entire list of internal resource descriptors */
196
197         while (resource) {
198
199                 /* Validate the descriptor type */
200
201                 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
202                         return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
203                 }
204
205                 /* Get the base size of the (external stream) resource descriptor */
206
207                 total_size = acpi_gbl_aml_resource_sizes[resource->type];
208
209                 /*
210                  * Augment the base size for descriptors with optional and/or
211                  * variable-length fields
212                  */
213                 switch (resource->type) {
214                 case ACPI_RESOURCE_TYPE_IRQ:
215
216                         /* Length can be 3 or 2 */
217
218                         if (resource->data.irq.descriptor_length == 2) {
219                                 total_size--;
220                         }
221                         break;
222
223                 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
224
225                         /* Length can be 1 or 0 */
226
227                         if (resource->data.irq.descriptor_length == 0) {
228                                 total_size--;
229                         }
230                         break;
231
232                 case ACPI_RESOURCE_TYPE_VENDOR:
233                         /*
234                          * Vendor Defined Resource:
235                          * For a Vendor Specific resource, if the Length is between 1 and 7
236                          * it will be created as a Small Resource data type, otherwise it
237                          * is a Large Resource data type.
238                          */
239                         if (resource->data.vendor.byte_length > 7) {
240
241                                 /* Base size of a Large resource descriptor */
242
243                                 total_size =
244                                     sizeof(struct aml_resource_large_header);
245                         }
246
247                         /* Add the size of the vendor-specific data */
248
249                         total_size = (acpi_rs_length)
250                             (total_size + resource->data.vendor.byte_length);
251                         break;
252
253                 case ACPI_RESOURCE_TYPE_END_TAG:
254                         /*
255                          * End Tag:
256                          * We are done -- return the accumulated total size.
257                          */
258                         *size_needed = aml_size_needed + total_size;
259
260                         /* Normal exit */
261
262                         return_ACPI_STATUS(AE_OK);
263
264                 case ACPI_RESOURCE_TYPE_ADDRESS16:
265                         /*
266                          * 16-Bit Address Resource:
267                          * Add the size of the optional resource_source info
268                          */
269                         total_size = (acpi_rs_length)
270                             (total_size +
271                              acpi_rs_struct_option_length(&resource->data.
272                                                           address16.
273                                                           resource_source));
274                         break;
275
276                 case ACPI_RESOURCE_TYPE_ADDRESS32:
277                         /*
278                          * 32-Bit Address Resource:
279                          * Add the size of the optional resource_source info
280                          */
281                         total_size = (acpi_rs_length)
282                             (total_size +
283                              acpi_rs_struct_option_length(&resource->data.
284                                                           address32.
285                                                           resource_source));
286                         break;
287
288                 case ACPI_RESOURCE_TYPE_ADDRESS64:
289                         /*
290                          * 64-Bit Address Resource:
291                          * Add the size of the optional resource_source info
292                          */
293                         total_size = (acpi_rs_length)
294                             (total_size +
295                              acpi_rs_struct_option_length(&resource->data.
296                                                           address64.
297                                                           resource_source));
298                         break;
299
300                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
301                         /*
302                          * Extended IRQ Resource:
303                          * Add the size of each additional optional interrupt beyond the
304                          * required 1 (4 bytes for each u32 interrupt number)
305                          */
306                         total_size = (acpi_rs_length)
307                             (total_size +
308                              ((resource->data.extended_irq.interrupt_count -
309                                1) * 4) +
310                              /* Add the size of the optional resource_source info */
311                              acpi_rs_struct_option_length(&resource->data.
312                                                           extended_irq.
313                                                           resource_source));
314                         break;
315
316                 case ACPI_RESOURCE_TYPE_GPIO:
317
318                         total_size =
319                             (acpi_rs_length) (total_size +
320                                               (resource->data.gpio.
321                                                pin_table_length * 2) +
322                                               resource->data.gpio.
323                                               resource_source.string_length +
324                                               resource->data.gpio.
325                                               vendor_length);
326
327                         break;
328
329                 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
330
331                         total_size =
332                             acpi_gbl_aml_resource_serial_bus_sizes[resource->
333                                                                    data.
334                                                                    common_serial_bus.
335                                                                    type];
336
337                         total_size = (acpi_rs_length) (total_size +
338                                                        resource->data.
339                                                        i2c_serial_bus.
340                                                        resource_source.
341                                                        string_length +
342                                                        resource->data.
343                                                        i2c_serial_bus.
344                                                        vendor_length);
345
346                         break;
347
348                 default:
349                         break;
350                 }
351
352                 /* Update the total */
353
354                 aml_size_needed += total_size;
355
356                 /* Point to the next object */
357
358                 resource =
359                     ACPI_ADD_PTR(struct acpi_resource, resource,
360                                  resource->length);
361         }
362
363         /* Did not find an end_tag resource descriptor */
364
365         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
366 }
367
368 /*******************************************************************************
369  *
370  * FUNCTION:    acpi_rs_get_list_length
371  *
372  * PARAMETERS:  aml_buffer          - Pointer to the resource byte stream
373  *              aml_buffer_length   - Size of aml_buffer
374  *              size_needed         - Where the size needed is returned
375  *
376  * RETURN:      Status
377  *
378  * DESCRIPTION: Takes an external resource byte stream and calculates the size
379  *              buffer needed to hold the corresponding internal resource
380  *              descriptor linked list.
381  *
382  ******************************************************************************/
383
384 acpi_status
385 acpi_rs_get_list_length(u8 * aml_buffer,
386                         u32 aml_buffer_length, acpi_size * size_needed)
387 {
388         acpi_status status;
389         u8 *end_aml;
390         u8 *buffer;
391         u32 buffer_size;
392         u16 temp16;
393         u16 resource_length;
394         u32 extra_struct_bytes;
395         u8 resource_index;
396         u8 minimum_aml_resource_length;
397         union aml_resource *aml_resource;
398
399         ACPI_FUNCTION_TRACE(rs_get_list_length);
400
401         *size_needed = ACPI_RS_SIZE_MIN;        /* Minimum size is one end_tag */
402         end_aml = aml_buffer + aml_buffer_length;
403
404         /* Walk the list of AML resource descriptors */
405
406         while (aml_buffer < end_aml) {
407
408                 /* Validate the Resource Type and Resource Length */
409
410                 status =
411                     acpi_ut_validate_resource(NULL, aml_buffer,
412                                               &resource_index);
413                 if (ACPI_FAILURE(status)) {
414                         /*
415                          * Exit on failure. Cannot continue because the descriptor length
416                          * may be bogus also.
417                          */
418                         return_ACPI_STATUS(status);
419                 }
420
421                 aml_resource = (void *)aml_buffer;
422
423                 /* Get the resource length and base (minimum) AML size */
424
425                 resource_length = acpi_ut_get_resource_length(aml_buffer);
426                 minimum_aml_resource_length =
427                     acpi_gbl_resource_aml_sizes[resource_index];
428
429                 /*
430                  * Augment the size for descriptors with optional
431                  * and/or variable length fields
432                  */
433                 extra_struct_bytes = 0;
434                 buffer =
435                     aml_buffer + acpi_ut_get_resource_header_length(aml_buffer);
436
437                 switch (acpi_ut_get_resource_type(aml_buffer)) {
438                 case ACPI_RESOURCE_NAME_IRQ:
439                         /*
440                          * IRQ Resource:
441                          * Get the number of bits set in the 16-bit IRQ mask
442                          */
443                         ACPI_MOVE_16_TO_16(&temp16, buffer);
444                         extra_struct_bytes = acpi_rs_count_set_bits(temp16);
445                         break;
446
447                 case ACPI_RESOURCE_NAME_DMA:
448                         /*
449                          * DMA Resource:
450                          * Get the number of bits set in the 8-bit DMA mask
451                          */
452                         extra_struct_bytes = acpi_rs_count_set_bits(*buffer);
453                         break;
454
455                 case ACPI_RESOURCE_NAME_VENDOR_SMALL:
456                 case ACPI_RESOURCE_NAME_VENDOR_LARGE:
457                         /*
458                          * Vendor Resource:
459                          * Get the number of vendor data bytes
460                          */
461                         extra_struct_bytes = resource_length;
462
463                         /*
464                          * There is already one byte included in the minimum
465                          * descriptor size. If there are extra struct bytes,
466                          * subtract one from the count.
467                          */
468                         if (extra_struct_bytes) {
469                                 extra_struct_bytes--;
470                         }
471                         break;
472
473                 case ACPI_RESOURCE_NAME_END_TAG:
474                         /*
475                          * End Tag: This is the normal exit
476                          */
477                         return_ACPI_STATUS(AE_OK);
478
479                 case ACPI_RESOURCE_NAME_ADDRESS32:
480                 case ACPI_RESOURCE_NAME_ADDRESS16:
481                 case ACPI_RESOURCE_NAME_ADDRESS64:
482                         /*
483                          * Address Resource:
484                          * Add the size of the optional resource_source
485                          */
486                         extra_struct_bytes =
487                             acpi_rs_stream_option_length(resource_length,
488                                                          minimum_aml_resource_length);
489                         break;
490
491                 case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
492                         /*
493                          * Extended IRQ Resource:
494                          * Using the interrupt_table_length, add 4 bytes for each additional
495                          * interrupt. Note: at least one interrupt is required and is
496                          * included in the minimum descriptor size (reason for the -1)
497                          */
498                         extra_struct_bytes = (buffer[1] - 1) * sizeof(u32);
499
500                         /* Add the size of the optional resource_source */
501
502                         extra_struct_bytes +=
503                             acpi_rs_stream_option_length(resource_length -
504                                                          extra_struct_bytes,
505                                                          minimum_aml_resource_length);
506                         break;
507
508                 case ACPI_RESOURCE_NAME_GPIO:
509
510                         /* Vendor data is optional */
511
512                         if (aml_resource->gpio.vendor_length) {
513                                 extra_struct_bytes +=
514                                     aml_resource->gpio.vendor_offset -
515                                     aml_resource->gpio.pin_table_offset +
516                                     aml_resource->gpio.vendor_length;
517                         } else {
518                                 extra_struct_bytes +=
519                                     aml_resource->large_header.resource_length +
520                                     sizeof(struct aml_resource_large_header) -
521                                     aml_resource->gpio.pin_table_offset;
522                         }
523                         break;
524
525                 case ACPI_RESOURCE_NAME_SERIAL_BUS:
526
527                         minimum_aml_resource_length =
528                             acpi_gbl_resource_aml_serial_bus_sizes
529                             [aml_resource->common_serial_bus.type];
530                         extra_struct_bytes +=
531                             aml_resource->common_serial_bus.resource_length -
532                             minimum_aml_resource_length;
533                         break;
534
535                 default:
536                         break;
537                 }
538
539                 /*
540                  * Update the required buffer size for the internal descriptor structs
541                  *
542                  * Important: Round the size up for the appropriate alignment. This
543                  * is a requirement on IA64.
544                  */
545                 if (acpi_ut_get_resource_type(aml_buffer) ==
546                     ACPI_RESOURCE_NAME_SERIAL_BUS) {
547                         buffer_size =
548                             acpi_gbl_resource_struct_serial_bus_sizes
549                             [aml_resource->common_serial_bus.type] +
550                             extra_struct_bytes;
551                 } else {
552                         buffer_size =
553                             acpi_gbl_resource_struct_sizes[resource_index] +
554                             extra_struct_bytes;
555                 }
556                 buffer_size = (u32)ACPI_ROUND_UP_TO_NATIVE_WORD(buffer_size);
557
558                 *size_needed += buffer_size;
559
560                 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
561                                   "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
562                                   acpi_ut_get_resource_type(aml_buffer),
563                                   acpi_ut_get_descriptor_length(aml_buffer),
564                                   buffer_size));
565
566                 /*
567                  * Point to the next resource within the AML stream using the length
568                  * contained in the resource descriptor header
569                  */
570                 aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
571         }
572
573         /* Did not find an end_tag resource descriptor */
574
575         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
576 }
577
578 /*******************************************************************************
579  *
580  * FUNCTION:    acpi_rs_get_pci_routing_table_length
581  *
582  * PARAMETERS:  package_object          - Pointer to the package object
583  *              buffer_size_needed      - u32 pointer of the size buffer
584  *                                        needed to properly return the
585  *                                        parsed data
586  *
587  * RETURN:      Status
588  *
589  * DESCRIPTION: Given a package representing a PCI routing table, this
590  *              calculates the size of the corresponding linked list of
591  *              descriptions.
592  *
593  ******************************************************************************/
594
595 acpi_status
596 acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
597                                      acpi_size * buffer_size_needed)
598 {
599         u32 number_of_elements;
600         acpi_size temp_size_needed = 0;
601         union acpi_operand_object **top_object_list;
602         u32 index;
603         union acpi_operand_object *package_element;
604         union acpi_operand_object **sub_object_list;
605         u8 name_found;
606         u32 table_index;
607
608         ACPI_FUNCTION_TRACE(rs_get_pci_routing_table_length);
609
610         number_of_elements = package_object->package.count;
611
612         /*
613          * Calculate the size of the return buffer.
614          * The base size is the number of elements * the sizes of the
615          * structures. Additional space for the strings is added below.
616          * The minus one is to subtract the size of the u8 Source[1]
617          * member because it is added below.
618          *
619          * But each PRT_ENTRY structure has a pointer to a string and
620          * the size of that string must be found.
621          */
622         top_object_list = package_object->package.elements;
623
624         for (index = 0; index < number_of_elements; index++) {
625
626                 /* Dereference the sub-package */
627
628                 package_element = *top_object_list;
629
630                 /* We must have a valid Package object */
631
632                 if (!package_element ||
633                     (package_element->common.type != ACPI_TYPE_PACKAGE)) {
634                         return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
635                 }
636
637                 /*
638                  * The sub_object_list will now point to an array of the
639                  * four IRQ elements: Address, Pin, Source and source_index
640                  */
641                 sub_object_list = package_element->package.elements;
642
643                 /* Scan the irq_table_elements for the Source Name String */
644
645                 name_found = FALSE;
646
647                 for (table_index = 0; table_index < 4 && !name_found;
648                      table_index++) {
649                         if (*sub_object_list && /* Null object allowed */
650                             ((ACPI_TYPE_STRING ==
651                               (*sub_object_list)->common.type) ||
652                              ((ACPI_TYPE_LOCAL_REFERENCE ==
653                                (*sub_object_list)->common.type) &&
654                               ((*sub_object_list)->reference.class ==
655                                ACPI_REFCLASS_NAME)))) {
656                                 name_found = TRUE;
657                         } else {
658                                 /* Look at the next element */
659
660                                 sub_object_list++;
661                         }
662                 }
663
664                 temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
665
666                 /* Was a String type found? */
667
668                 if (name_found) {
669                         if ((*sub_object_list)->common.type == ACPI_TYPE_STRING) {
670                                 /*
671                                  * The length String.Length field does not include the
672                                  * terminating NULL, add 1
673                                  */
674                                 temp_size_needed += ((acpi_size)
675                                                      (*sub_object_list)->string.
676                                                      length + 1);
677                         } else {
678                                 temp_size_needed += acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
679                         }
680                 } else {
681                         /*
682                          * If no name was found, then this is a NULL, which is
683                          * translated as a u32 zero.
684                          */
685                         temp_size_needed += sizeof(u32);
686                 }
687
688                 /* Round up the size since each element must be aligned */
689
690                 temp_size_needed = ACPI_ROUND_UP_TO_64BIT(temp_size_needed);
691
692                 /* Point to the next union acpi_operand_object */
693
694                 top_object_list++;
695         }
696
697         /*
698          * Add an extra element to the end of the list, essentially a
699          * NULL terminator
700          */
701         *buffer_size_needed =
702             temp_size_needed + sizeof(struct acpi_pci_routing_table);
703         return_ACPI_STATUS(AE_OK);
704 }