Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / acpi / resources / rscalc.c
blob2874aa3d05946c0030764b44031ca14032900ef0
1 /*******************************************************************************
3 * Module Name: rscalc - Acpi_rs_calculate_byte_stream_length
4 * Acpi_rs_calculate_list_length
5 * $Revision: 16 $
7 ******************************************************************************/
9 /*
10 * Copyright (C) 2000 R. Byron Moore
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "acpi.h"
29 #include "acresrc.h"
31 #define _COMPONENT RESOURCE_MANAGER
32 MODULE_NAME ("rscalc")
35 /*******************************************************************************
37 * FUNCTION: Acpi_rs_calculate_byte_stream_length
39 * PARAMETERS: Linked_list - Pointer to the resource linked list
40 * Size_needed - u32 pointer of the size buffer needed
41 * to properly return the parsed data
43 * RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code
45 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
46 * the size buffer needed to hold the linked list that conveys
47 * the resource data.
49 ******************************************************************************/
51 ACPI_STATUS
52 acpi_rs_calculate_byte_stream_length (
53 RESOURCE *linked_list,
54 u32 *size_needed)
56 u32 byte_stream_size_needed = 0;
57 u32 segment_size;
58 EXTENDED_IRQ_RESOURCE *ex_irq = NULL;
59 u8 done = FALSE;
62 while (!done) {
65 * Init the variable that will hold the size to add to the
66 * total.
68 segment_size = 0;
70 switch (linked_list->id)
72 case irq:
74 * IRQ Resource
77 * For an IRQ Resource, Byte 3, although optional, will
78 * always be created - it holds IRQ information.
80 segment_size = 4;
81 break;
83 case dma:
85 * DMA Resource
88 * For this resource the size is static
90 segment_size = 3;
91 break;
93 case start_dependent_functions:
95 * Start Dependent Functions Resource
98 * For a Start_dependent_functions Resource, Byte 1,
99 * although optional, will always be created.
101 segment_size = 2;
102 break;
104 case end_dependent_functions:
106 * End Dependent Functions Resource
109 * For this resource the size is static
111 segment_size = 1;
112 break;
114 case io:
116 * IO Port Resource
119 * For this resource the size is static
121 segment_size = 8;
122 break;
124 case fixed_io:
126 * Fixed IO Port Resource
129 * For this resource the size is static
131 segment_size = 4;
132 break;
134 case vendor_specific:
136 * Vendor Defined Resource
139 * For a Vendor Specific resource, if the Length is
140 * between 1 and 7 it will be created as a Small
141 * Resource data type, otherwise it is a Large
142 * Resource data type.
144 if(linked_list->data.vendor_specific.length > 7) {
145 segment_size = 3;
147 else {
148 segment_size = 1;
150 segment_size +=
151 linked_list->data.vendor_specific.length;
152 break;
154 case end_tag:
156 * End Tag
159 * For this resource the size is static
161 segment_size = 2;
162 done = TRUE;
163 break;
165 case memory24:
167 * 24-Bit Memory Resource
170 * For this resource the size is static
172 segment_size = 12;
173 break;
175 case memory32:
177 * 32-Bit Memory Range Resource
180 * For this resource the size is static
182 segment_size = 20;
183 break;
185 case fixed_memory32:
187 * 32-Bit Fixed Memory Resource
190 * For this resource the size is static
192 segment_size = 12;
193 break;
195 case address16:
197 * 16-Bit Address Resource
200 * The base size of this byte stream is 16. If a
201 * Resource Source string is not NULL, add 1 for
202 * the Index + the length of the null terminated
203 * string Resource Source + 1 for the null.
205 segment_size = 16;
207 if(NULL != linked_list->data.address16.resource_source) {
208 segment_size += (1 +
209 linked_list->data.address16.resource_source_string_length);
211 break;
213 case address32:
215 * 32-Bit Address Resource
218 * The base size of this byte stream is 26. If a Resource
219 * Source string is not NULL, add 1 for the Index + the
220 * length of the null terminated string Resource Source +
221 * 1 for the null.
223 segment_size = 26;
225 if(NULL != linked_list->data.address16.resource_source) {
226 segment_size += (1 +
227 linked_list->data.address16.resource_source_string_length);
229 break;
231 case extended_irq:
233 * Extended IRQ Resource
236 * The base size of this byte stream is 9. This is for an
237 * Interrupt table length of 1. For each additional
238 * interrupt, add 4.
239 * If a Resource Source string is not NULL, add 1 for the
240 * Index + the length of the null terminated string
241 * Resource Source + 1 for the null.
243 segment_size = 9;
245 segment_size +=
246 (linked_list->data.extended_irq.number_of_interrupts -
247 1) * 4;
249 if(NULL != ex_irq->resource_source) {
250 segment_size += (1 +
251 linked_list->data.extended_irq.resource_source_string_length);
253 break;
255 default:
257 * If we get here, everything is out of sync,
258 * so exit with an error
260 return (AE_AML_ERROR);
261 break;
263 } /* switch (Linked_list->Id) */
266 * Update the total
268 byte_stream_size_needed += segment_size;
271 * Point to the next object
273 linked_list = (RESOURCE *) ((NATIVE_UINT) linked_list +
274 (NATIVE_UINT) linked_list->length);
278 * This is the data the caller needs
280 *size_needed = byte_stream_size_needed;
282 return (AE_OK);
286 /*******************************************************************************
288 * FUNCTION: Acpi_rs_calculate_list_length
290 * PARAMETERS: Byte_stream_buffer - Pointer to the resource byte stream
291 * Byte_stream_buffer_length - Size of Byte_stream_buffer
292 * Size_needed - u32 pointer of the size buffer
293 * needed to properly return the
294 * parsed data
296 * RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code
298 * DESCRIPTION: Takes the resource byte stream and parses it once, calculating
299 * the size buffer needed to hold the linked list that conveys
300 * the resource data.
302 ******************************************************************************/
304 ACPI_STATUS
305 acpi_rs_calculate_list_length (
306 u8 *byte_stream_buffer,
307 u32 byte_stream_buffer_length,
308 u32 *size_needed)
310 u32 buffer_size = 0;
311 u32 bytes_parsed = 0;
312 u8 number_of_interrupts = 0;
313 u8 number_of_channels = 0;
314 u8 resource_type;
315 u32 structure_size;
316 u32 bytes_consumed;
317 u8 *buffer;
318 u8 temp8;
319 u16 temp16;
320 u8 index;
321 u8 additional_bytes;
324 while (bytes_parsed < byte_stream_buffer_length) {
326 * Look at the next byte in the stream
328 resource_type = *byte_stream_buffer;
331 * See if this is a small or large resource
333 if(resource_type & 0x80) {
335 * Large Resource Type
337 switch (resource_type)
339 case MEMORY_RANGE_24:
341 * 24-Bit Memory Resource
343 bytes_consumed = 12;
345 structure_size = sizeof (MEMORY24_RESOURCE) +
346 RESOURCE_LENGTH_NO_DATA;
347 break;
349 case LARGE_VENDOR_DEFINED:
351 * Vendor Defined Resource
353 buffer = byte_stream_buffer;
354 ++buffer;
356 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
357 bytes_consumed = temp16 + 3;
360 * Ensure a 32-bit boundary for the structure
362 temp16 = (u16) ROUND_UP_TO_32_bITS (temp16);
364 structure_size = sizeof (VENDOR_RESOURCE) +
365 RESOURCE_LENGTH_NO_DATA +
366 (temp16 * sizeof (u8));
367 break;
369 case MEMORY_RANGE_32:
371 * 32-Bit Memory Range Resource
374 bytes_consumed = 20;
376 structure_size = sizeof (MEMORY32_RESOURCE) +
377 RESOURCE_LENGTH_NO_DATA;
378 break;
380 case FIXED_MEMORY_RANGE_32:
382 * 32-Bit Fixed Memory Resource
384 bytes_consumed = 12;
386 structure_size = sizeof(FIXED_MEMORY32_RESOURCE) +
387 RESOURCE_LENGTH_NO_DATA;
388 break;
390 case DWORD_ADDRESS_SPACE:
392 * 32-Bit Address Resource
394 buffer = byte_stream_buffer;
396 ++buffer;
397 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
399 bytes_consumed = temp16 + 3;
402 * Resource Source Index and Resource Source are
403 * optional elements. Check the length of the
404 * Bytestream. If it is greater than 23, that
405 * means that an Index exists and is followed by
406 * a null termininated string. Therefore, set
407 * the temp variable to the length minus the minimum
408 * byte stream length plus the byte for the Index to
409 * determine the size of the NULL terminiated string.
411 if (23 < temp16) {
412 temp8 = (u8) (temp16 - 24);
414 else {
415 temp8 = 0;
419 * Ensure a 32-bit boundary for the structure
421 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
423 structure_size = sizeof (ADDRESS32_RESOURCE) +
424 RESOURCE_LENGTH_NO_DATA +
425 (temp8 * sizeof (u8));
426 break;
428 case WORD_ADDRESS_SPACE:
430 * 16-Bit Address Resource
432 buffer = byte_stream_buffer;
434 ++buffer;
435 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
437 bytes_consumed = temp16 + 3;
440 * Resource Source Index and Resource Source are
441 * optional elements. Check the length of the
442 * Bytestream. If it is greater than 13, that
443 * means that an Index exists and is followed by
444 * a null termininated string. Therefore, set
445 * the temp variable to the length minus the minimum
446 * byte stream length plus the byte for the Index to
447 * determine the size of the NULL terminiated string.
449 if (13 < temp16) {
450 temp8 = (u8) (temp16 - 14);
452 else {
453 temp8 = 0;
457 * Ensure a 32-bit boundry for the structure
459 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
461 structure_size = sizeof (ADDRESS16_RESOURCE) +
462 RESOURCE_LENGTH_NO_DATA +
463 (temp8 * sizeof (u8));
464 break;
466 case EXTENDED_IRQ:
468 * Extended IRQ
470 buffer = byte_stream_buffer;
472 ++buffer;
473 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
475 bytes_consumed = temp16 + 3;
478 * Point past the length field and the
479 * Interrupt vector flags to save off the
480 * Interrupt table length to the Temp8 variable.
482 buffer += 3;
483 temp8 = *buffer;
486 * To compensate for multiple interrupt numbers,
487 * Add 4 bytes for each additional interrupts
488 * greater than 1
490 additional_bytes = (u8) ((temp8 - 1) * 4);
493 * Resource Source Index and Resource Source are
494 * optional elements. Check the length of the
495 * Bytestream. If it is greater than 9, that
496 * means that an Index exists and is followed by
497 * a null termininated string. Therefore, set
498 * the temp variable to the length minus the minimum
499 * byte stream length plus the byte for the Index to
500 * determine the size of the NULL terminiated string.
502 if (9 + additional_bytes < temp16) {
503 temp8 = (u8) (temp16 - (9 + additional_bytes));
506 else {
507 temp8 = 0;
511 * Ensure a 32-bit boundry for the structure
513 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
515 structure_size = sizeof (EXTENDED_IRQ_RESOURCE) +
516 RESOURCE_LENGTH_NO_DATA +
517 (additional_bytes * sizeof (u8)) +
518 (temp8 * sizeof (u8));
520 break;
522 /* TBD: [Future] 64-bit not currently supported */
524 case 0x8A:
525 break;
528 default:
530 * If we get here, everything is out of sync,
531 * so exit with an error
533 return (AE_AML_ERROR);
534 break;
538 else {
540 * Small Resource Type
541 * Only bits 7:3 are valid
543 resource_type >>= 3;
545 switch (resource_type)
547 case IRQ_FORMAT:
549 * IRQ Resource
552 * Determine if it there are two or three
553 * trailing bytes
555 buffer = byte_stream_buffer;
556 temp8 = *buffer;
558 if(temp8 & 0x01) {
559 bytes_consumed = 4;
562 else {
563 bytes_consumed = 3;
567 * Point past the descriptor
569 ++buffer;
572 * Look at the number of bits set
574 MOVE_UNALIGNED16_TO_16 (&temp16, buffer);
576 for (index = 0; index < 16; index++) {
577 if (temp16 & 0x1) {
578 ++number_of_interrupts;
581 temp16 >>= 1;
584 structure_size = sizeof (IO_RESOURCE) +
585 RESOURCE_LENGTH_NO_DATA +
586 (number_of_interrupts * sizeof (u32));
587 break;
590 case DMA_FORMAT:
593 * DMA Resource
595 buffer = byte_stream_buffer;
597 bytes_consumed = 3;
600 * Point past the descriptor
602 ++buffer;
605 * Look at the number of bits set
607 temp8 = *buffer;
609 for(index = 0; index < 8; index++) {
610 if(temp8 & 0x1) {
611 ++number_of_channels;
614 temp8 >>= 1;
617 structure_size = sizeof (DMA_RESOURCE) +
618 RESOURCE_LENGTH_NO_DATA +
619 (number_of_channels * sizeof (u32));
620 break;
623 case START_DEPENDENT_TAG:
626 * Start Dependent Functions Resource
629 * Determine if it there are two or three trailing bytes
631 buffer = byte_stream_buffer;
632 temp8 = *buffer;
634 if(temp8 & 0x01) {
635 bytes_consumed = 2;
637 else {
638 bytes_consumed = 1;
642 structure_size =
643 sizeof (START_DEPENDENT_FUNCTIONS_RESOURCE) +
644 RESOURCE_LENGTH_NO_DATA;
645 break;
648 case END_DEPENDENT_TAG:
651 * End Dependent Functions Resource
653 bytes_consumed = 1;
654 structure_size = RESOURCE_LENGTH;
655 break;
658 case IO_PORT_DESCRIPTOR:
660 * IO Port Resource
662 bytes_consumed = 8;
663 structure_size = sizeof (IO_RESOURCE) +
664 RESOURCE_LENGTH_NO_DATA;
665 break;
668 case FIXED_LOCATION_IO_DESCRIPTOR:
671 * Fixed IO Port Resource
673 bytes_consumed = 4;
674 structure_size = sizeof (FIXED_IO_RESOURCE) +
675 RESOURCE_LENGTH_NO_DATA;
676 break;
679 case SMALL_VENDOR_DEFINED:
682 * Vendor Specific Resource
684 buffer = byte_stream_buffer;
686 temp8 = *buffer;
687 temp8 = (u8) (temp8 & 0x7);
688 bytes_consumed = temp8 + 1;
691 * Ensure a 32-bit boundry for the structure
693 temp8 = (u8) ROUND_UP_TO_32_bITS (temp8);
694 structure_size = sizeof (VENDOR_RESOURCE) +
695 RESOURCE_LENGTH_NO_DATA +
696 (temp8 * sizeof (u8));
697 break;
700 case END_TAG:
703 * End Tag
705 bytes_consumed = 2;
706 structure_size = RESOURCE_LENGTH;
707 break;
710 default:
712 * If we get here, everything is out of sync,
713 * so exit with an error
715 return (AE_AML_ERROR);
716 break;
718 } /* switch */
720 } /* if(Resource_type & 0x80) */
723 * Update the return value and counter
725 buffer_size += structure_size;
726 bytes_parsed += bytes_consumed;
729 * Set the byte stream to point to the next resource
731 byte_stream_buffer += bytes_consumed;
736 * This is the data the caller needs
738 *size_needed = buffer_size;
740 return (AE_OK);
744 /*******************************************************************************
746 * FUNCTION: Acpi_rs_calculate_pci_routing_table_length
748 * PARAMETERS: Package_object - Pointer to the package object
749 * Buffer_size_needed - u32 pointer of the size buffer
750 * needed to properly return the
751 * parsed data
753 * RETURN: Status AE_OK
755 * DESCRIPTION: Given a package representing a PCI routing table, this
756 * calculates the size of the corresponding linked list of
757 * descriptions.
759 ******************************************************************************/
761 ACPI_STATUS
762 acpi_rs_calculate_pci_routing_table_length (
763 ACPI_OPERAND_OBJECT *package_object,
764 u32 *buffer_size_needed)
766 u32 number_of_elements;
767 u32 temp_size_needed = 0;
768 ACPI_OPERAND_OBJECT **top_object_list;
769 u32 index;
770 ACPI_OPERAND_OBJECT *package_element;
771 ACPI_OPERAND_OBJECT **sub_object_list;
772 u8 name_found;
773 u32 table_index;
776 number_of_elements = package_object->package.count;
779 * Calculate the size of the return buffer.
780 * The base size is the number of elements * the sizes of the
781 * structures. Additional space for the strings is added below.
782 * The minus one is to subtract the size of the u8 Source[1]
783 * member because it is added below.
785 * NOTE: The Number_of_elements is incremented by one to add an end
786 * table structure that is essentially a structure of zeros.
790 * But each PRT_ENTRY structure has a pointer to a string and
791 * the size of that string must be found.
793 top_object_list = package_object->package.elements;
795 for (index = 0; index < number_of_elements; index++) {
797 * Dereference the sub-package
799 package_element = *top_object_list;
802 * The Sub_object_list will now point to an array of the
803 * four IRQ elements: Address, Pin, Source and Source_index
805 sub_object_list = package_element->package.elements;
808 * Scan the Irq_table_elements for the Source Name String
810 name_found = FALSE;
812 for (table_index = 0; table_index < 4 && !name_found; table_index++) {
813 if (ACPI_TYPE_STRING == (*sub_object_list)->common.type) {
814 name_found = TRUE;
817 else {
819 * Look at the next element
821 sub_object_list++;
825 temp_size_needed += (sizeof (PCI_ROUTING_TABLE) - 1);
828 * Was a String type found?
830 if (TRUE == name_found) {
832 * The length String.Length field includes the
833 * terminating NULL
835 temp_size_needed += (*sub_object_list)->string.length;
838 else {
840 * If no name was found, then this is a NULL, which is
841 * translated as a u32 zero.
843 temp_size_needed += sizeof(u32);
847 /* Round up the size since each element must be aligned */
849 temp_size_needed = ROUND_UP_TO_64_bITS (temp_size_needed);
852 * Point to the next ACPI_OPERAND_OBJECT
854 top_object_list++;
858 *buffer_size_needed = temp_size_needed + sizeof (PCI_ROUTING_TABLE);
860 return (AE_OK);