]> err.no Git - linux-2.6/blob - drivers/acpi/resources/rscalc.c
[ACPI] ACPICA 20060210
[linux-2.6] / drivers / acpi / resources / rscalc.c
1 /*******************************************************************************
2  *
3  * Module Name: rscalc - Calculate stream and list lengths
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/acresrc.h>
46 #include <acpi/amlcode.h>
47 #include <acpi/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 &= (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         /* Round up length to 32 bits for internal structure alignment */
166
167         return ((u32) ACPI_ROUND_UP_to_32_bITS(string_length));
168 }
169
170 /*******************************************************************************
171  *
172  * FUNCTION:    acpi_rs_get_aml_length
173  *
174  * PARAMETERS:  Resource            - Pointer to the resource linked list
175  *              size_needed         - Where the required size is returned
176  *
177  * RETURN:      Status
178  *
179  * DESCRIPTION: Takes a linked list of internal resource descriptors and
180  *              calculates the size buffer needed to hold the corresponding
181  *              external resource byte stream.
182  *
183  ******************************************************************************/
184
185 acpi_status
186 acpi_rs_get_aml_length(struct acpi_resource * resource, acpi_size * size_needed)
187 {
188         acpi_size aml_size_needed = 0;
189         acpi_rs_length total_size;
190
191         ACPI_FUNCTION_TRACE("rs_get_aml_length");
192
193         /* Traverse entire list of internal resource descriptors */
194
195         while (resource) {
196
197                 /* Validate the descriptor type */
198
199                 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
200                         return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
201                 }
202
203                 /* Get the base size of the (external stream) resource descriptor */
204
205                 total_size = acpi_gbl_aml_resource_sizes[resource->type];
206
207                 /*
208                  * Augment the base size for descriptors with optional and/or
209                  * variable-length fields
210                  */
211                 switch (resource->type) {
212                 case ACPI_RESOURCE_TYPE_VENDOR:
213                         /*
214                          * Vendor Defined Resource:
215                          * For a Vendor Specific resource, if the Length is between 1 and 7
216                          * it will be created as a Small Resource data type, otherwise it
217                          * is a Large Resource data type.
218                          */
219                         if (resource->data.vendor.byte_length > 7) {
220
221                                 /* Base size of a Large resource descriptor */
222
223                                 total_size =
224                                     sizeof(struct aml_resource_large_header);
225                         }
226
227                         /* Add the size of the vendor-specific data */
228
229                         total_size = (acpi_rs_length)
230                             (total_size + resource->data.vendor.byte_length);
231                         break;
232
233                 case ACPI_RESOURCE_TYPE_END_TAG:
234                         /*
235                          * End Tag:
236                          * We are done -- return the accumulated total size.
237                          */
238                         *size_needed = aml_size_needed + total_size;
239
240                         /* Normal exit */
241
242                         return_ACPI_STATUS(AE_OK);
243
244                 case ACPI_RESOURCE_TYPE_ADDRESS16:
245                         /*
246                          * 16-Bit Address Resource:
247                          * Add the size of the optional resource_source info
248                          */
249                         total_size = (acpi_rs_length)
250                             (total_size +
251                              acpi_rs_struct_option_length(&resource->data.
252                                                           address16.
253                                                           resource_source));
254                         break;
255
256                 case ACPI_RESOURCE_TYPE_ADDRESS32:
257                         /*
258                          * 32-Bit Address Resource:
259                          * Add the size of the optional resource_source info
260                          */
261                         total_size = (acpi_rs_length)
262                             (total_size +
263                              acpi_rs_struct_option_length(&resource->data.
264                                                           address32.
265                                                           resource_source));
266                         break;
267
268                 case ACPI_RESOURCE_TYPE_ADDRESS64:
269                         /*
270                          * 64-Bit Address Resource:
271                          * Add the size of the optional resource_source info
272                          */
273                         total_size = (acpi_rs_length)
274                             (total_size +
275                              acpi_rs_struct_option_length(&resource->data.
276                                                           address64.
277                                                           resource_source));
278                         break;
279
280                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
281                         /*
282                          * Extended IRQ Resource:
283                          * Add the size of each additional optional interrupt beyond the
284                          * required 1 (4 bytes for each u32 interrupt number)
285                          */
286                         total_size = (acpi_rs_length)
287                             (total_size +
288                              ((resource->data.extended_irq.interrupt_count -
289                                1) * 4) +
290                              /* Add the size of the optional resource_source info */
291                              acpi_rs_struct_option_length(&resource->data.
292                                                           extended_irq.
293                                                           resource_source));
294                         break;
295
296                 default:
297                         break;
298                 }
299
300                 /* Update the total */
301
302                 aml_size_needed += total_size;
303
304                 /* Point to the next object */
305
306                 resource =
307                     ACPI_ADD_PTR(struct acpi_resource, resource,
308                                  resource->length);
309         }
310
311         /* Did not find an end_tag resource descriptor */
312
313         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
314 }
315
316 /*******************************************************************************
317  *
318  * FUNCTION:    acpi_rs_get_list_length
319  *
320  * PARAMETERS:  aml_buffer          - Pointer to the resource byte stream
321  *              aml_buffer_length   - Size of aml_buffer
322  *              size_needed         - Where the size needed is returned
323  *
324  * RETURN:      Status
325  *
326  * DESCRIPTION: Takes an external resource byte stream and calculates the size
327  *              buffer needed to hold the corresponding internal resource
328  *              descriptor linked list.
329  *
330  ******************************************************************************/
331
332 acpi_status
333 acpi_rs_get_list_length(u8 * aml_buffer,
334                         u32 aml_buffer_length, acpi_size * size_needed)
335 {
336         acpi_status status;
337         u8 *end_aml;
338         u8 *buffer;
339         u32 buffer_size = 0;
340         u16 temp16;
341         u16 resource_length;
342         u32 extra_struct_bytes;
343         u8 resource_index;
344         u8 minimum_aml_resource_length;
345
346         ACPI_FUNCTION_TRACE("rs_get_list_length");
347
348         end_aml = aml_buffer + aml_buffer_length;
349
350         /* Walk the list of AML resource descriptors */
351
352         while (aml_buffer < end_aml) {
353
354                 /* Validate the Resource Type and Resource Length */
355
356                 status = acpi_ut_validate_resource(aml_buffer, &resource_index);
357                 if (ACPI_FAILURE(status)) {
358                         return_ACPI_STATUS(status);
359                 }
360
361                 /* Get the resource length and base (minimum) AML size */
362
363                 resource_length = acpi_ut_get_resource_length(aml_buffer);
364                 minimum_aml_resource_length =
365                     acpi_gbl_resource_aml_sizes[resource_index];
366
367                 /*
368                  * Augment the size for descriptors with optional
369                  * and/or variable length fields
370                  */
371                 extra_struct_bytes = 0;
372                 buffer =
373                     aml_buffer + acpi_ut_get_resource_header_length(aml_buffer);
374
375                 switch (acpi_ut_get_resource_type(aml_buffer)) {
376                 case ACPI_RESOURCE_NAME_IRQ:
377                         /*
378                          * IRQ Resource:
379                          * Get the number of bits set in the 16-bit IRQ mask
380                          */
381                         ACPI_MOVE_16_TO_16(&temp16, buffer);
382                         extra_struct_bytes = acpi_rs_count_set_bits(temp16);
383                         break;
384
385                 case ACPI_RESOURCE_NAME_DMA:
386                         /*
387                          * DMA Resource:
388                          * Get the number of bits set in the 8-bit DMA mask
389                          */
390                         extra_struct_bytes = acpi_rs_count_set_bits(*buffer);
391                         break;
392
393                 case ACPI_RESOURCE_NAME_VENDOR_SMALL:
394                         /*
395                          * Vendor Resource:
396                          * Ensure a 32-bit boundary for the structure
397                          */
398                         extra_struct_bytes = (u32)
399                             ACPI_ROUND_UP_to_32_bITS(resource_length) -
400                             resource_length;
401                         break;
402
403                 case ACPI_RESOURCE_NAME_END_TAG:
404                         /*
405                          * End Tag: This is the normal exit, add size of end_tag
406                          */
407                         *size_needed = buffer_size + ACPI_RS_SIZE_MIN;
408                         return_ACPI_STATUS(AE_OK);
409
410                 case ACPI_RESOURCE_NAME_VENDOR_LARGE:
411                         /*
412                          * Vendor Resource:
413                          * Add vendor data and ensure a 32-bit boundary for the structure
414                          */
415                         extra_struct_bytes = (u32)
416                             ACPI_ROUND_UP_to_32_bITS(resource_length) -
417                             resource_length;
418                         break;
419
420                 case ACPI_RESOURCE_NAME_ADDRESS32:
421                 case ACPI_RESOURCE_NAME_ADDRESS16:
422                         /*
423                          * 32-Bit or 16-bit Address Resource:
424                          * Add the size of any optional data (resource_source)
425                          */
426                         extra_struct_bytes =
427                             acpi_rs_stream_option_length(resource_length,
428                                                          minimum_aml_resource_length);
429                         break;
430
431                 case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
432                         /*
433                          * Extended IRQ:
434                          * Point past the interrupt_vector_flags to get the
435                          * interrupt_table_length.
436                          */
437                         buffer++;
438
439                         extra_struct_bytes = (u32)
440                             /*
441                              * Add 4 bytes for each additional interrupt. Note: at
442                              * least one interrupt is required and is included in
443                              * the minimum descriptor size
444                              */
445                             ((*buffer - 1) * sizeof(u32)) +
446                             /* Add the size of any optional data (resource_source) */
447                             acpi_rs_stream_option_length(resource_length -
448                                                          extra_struct_bytes,
449                                                          minimum_aml_resource_length);
450                         break;
451
452                 case ACPI_RESOURCE_NAME_ADDRESS64:
453                         /*
454                          * 64-Bit Address Resource:
455                          * Add the size of any optional data (resource_source)
456                          * Ensure a 64-bit boundary for the structure
457                          */
458                         extra_struct_bytes = (u32)
459                             ACPI_ROUND_UP_to_64_bITS
460                             (acpi_rs_stream_option_length
461                              (resource_length, minimum_aml_resource_length));
462                         break;
463
464                 default:
465                         break;
466                 }
467
468                 /* Update the required buffer size for the internal descriptor structs */
469
470                 temp16 = (u16) (acpi_gbl_resource_struct_sizes[resource_index] +
471                                 extra_struct_bytes);
472                 buffer_size += (u32) ACPI_ROUND_UP_TO_NATIVE_WORD(temp16);
473
474                 /*
475                  * Point to the next resource within the stream
476                  * using the size of the header plus the length contained in the header
477                  */
478                 aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
479         }
480
481         /* Did not find an end_tag resource descriptor */
482
483         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
484 }
485
486 /*******************************************************************************
487  *
488  * FUNCTION:    acpi_rs_get_pci_routing_table_length
489  *
490  * PARAMETERS:  package_object          - Pointer to the package object
491  *              buffer_size_needed      - u32 pointer of the size buffer
492  *                                        needed to properly return the
493  *                                        parsed data
494  *
495  * RETURN:      Status
496  *
497  * DESCRIPTION: Given a package representing a PCI routing table, this
498  *              calculates the size of the corresponding linked list of
499  *              descriptions.
500  *
501  ******************************************************************************/
502
503 acpi_status
504 acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
505                                      acpi_size * buffer_size_needed)
506 {
507         u32 number_of_elements;
508         acpi_size temp_size_needed = 0;
509         union acpi_operand_object **top_object_list;
510         u32 index;
511         union acpi_operand_object *package_element;
512         union acpi_operand_object **sub_object_list;
513         u8 name_found;
514         u32 table_index;
515
516         ACPI_FUNCTION_TRACE("rs_get_pci_routing_table_length");
517
518         number_of_elements = package_object->package.count;
519
520         /*
521          * Calculate the size of the return buffer.
522          * The base size is the number of elements * the sizes of the
523          * structures.  Additional space for the strings is added below.
524          * The minus one is to subtract the size of the u8 Source[1]
525          * member because it is added below.
526          *
527          * But each PRT_ENTRY structure has a pointer to a string and
528          * the size of that string must be found.
529          */
530         top_object_list = package_object->package.elements;
531
532         for (index = 0; index < number_of_elements; index++) {
533
534                 /* Dereference the sub-package */
535
536                 package_element = *top_object_list;
537
538                 /*
539                  * The sub_object_list will now point to an array of the
540                  * four IRQ elements: Address, Pin, Source and source_index
541                  */
542                 sub_object_list = package_element->package.elements;
543
544                 /* Scan the irq_table_elements for the Source Name String */
545
546                 name_found = FALSE;
547
548                 for (table_index = 0; table_index < 4 && !name_found;
549                      table_index++) {
550                         if (*sub_object_list && /* Null object allowed */
551                             ((ACPI_TYPE_STRING ==
552                               ACPI_GET_OBJECT_TYPE(*sub_object_list)) ||
553                              ((ACPI_TYPE_LOCAL_REFERENCE ==
554                                ACPI_GET_OBJECT_TYPE(*sub_object_list)) &&
555                               ((*sub_object_list)->reference.opcode ==
556                                AML_INT_NAMEPATH_OP)))) {
557                                 name_found = TRUE;
558                         } else {
559                                 /* Look at the next element */
560
561                                 sub_object_list++;
562                         }
563                 }
564
565                 temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
566
567                 /* Was a String type found? */
568
569                 if (name_found) {
570                         if (ACPI_GET_OBJECT_TYPE(*sub_object_list) ==
571                             ACPI_TYPE_STRING) {
572                                 /*
573                                  * The length String.Length field does not include the
574                                  * terminating NULL, add 1
575                                  */
576                                 temp_size_needed += ((acpi_size)
577                                                      (*sub_object_list)->string.
578                                                      length + 1);
579                         } else {
580                                 temp_size_needed += acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
581                         }
582                 } else {
583                         /*
584                          * If no name was found, then this is a NULL, which is
585                          * translated as a u32 zero.
586                          */
587                         temp_size_needed += sizeof(u32);
588                 }
589
590                 /* Round up the size since each element must be aligned */
591
592                 temp_size_needed = ACPI_ROUND_UP_to_64_bITS(temp_size_needed);
593
594                 /* Point to the next union acpi_operand_object */
595
596                 top_object_list++;
597         }
598
599         /*
600          * Adding an extra element to the end of the list, essentially a
601          * NULL terminator
602          */
603         *buffer_size_needed =
604             temp_size_needed + sizeof(struct acpi_pci_routing_table);
605         return_ACPI_STATUS(AE_OK);
606 }