Merge from vendor branch LESS:
[dragonfly.git] / sys / contrib / dev / acpica / rslist.c
1 /*******************************************************************************
2  *
3  * Module Name: rslist - Linked list utilities
4  *              $Revision: 32 $
5  *
6  ******************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/rslist.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $                                                               */
117
118 #define __RSLIST_C__
119
120 #include "acpi.h"
121 #include "acresrc.h"
122
123 #define _COMPONENT          ACPI_RESOURCES
124         ACPI_MODULE_NAME    ("rslist")
125
126
127 /*******************************************************************************
128  *
129  * FUNCTION:    AcpiRsGetResourceType
130  *
131  * PARAMETERS:  ResourceStartByte       - Byte 0 of a resource descriptor
132  *
133  * RETURN:      The Resource Type (Name) with no extraneous bits
134  *
135  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
136  *              a resource descriptor.
137  *
138  ******************************************************************************/
139
140 UINT8
141 AcpiRsGetResourceType (
142     UINT8                   ResourceStartByte)
143 {
144
145     ACPI_FUNCTION_ENTRY ();
146
147
148     /*
149      * Determine if this is a small or large resource
150      */
151     switch (ResourceStartByte & ACPI_RDESC_TYPE_MASK)
152     {
153     case ACPI_RDESC_TYPE_SMALL:
154
155         /*
156          * Small Resource Type -- Only bits 6:3 are valid
157          */
158         return ((UINT8) (ResourceStartByte & ACPI_RDESC_SMALL_MASK));
159
160
161     case ACPI_RDESC_TYPE_LARGE:
162
163         /*
164          * Large Resource Type -- All bits are valid
165          */
166         return (ResourceStartByte);
167
168
169     default:
170         /* No other types of resource descriptor */
171         break;
172     }
173
174     return (0xFF);
175 }
176
177
178 /*******************************************************************************
179  *
180  * FUNCTION:    AcpiRsByteStreamToList
181  *
182  * PARAMETERS:  ByteStreamBuffer        - Pointer to the resource byte stream
183  *              ByteStreamBufferLength  - Length of ByteStreamBuffer
184  *              OutputBuffer            - Pointer to the buffer that will
185  *                                        contain the output structures
186  *
187  * RETURN:      Status
188  *
189  * DESCRIPTION: Takes the resource byte stream and parses it, creating a
190  *              linked list of resources in the caller's output buffer
191  *
192  ******************************************************************************/
193
194 ACPI_STATUS
195 AcpiRsByteStreamToList (
196     UINT8                   *ByteStreamBuffer,
197     UINT32                  ByteStreamBufferLength,
198     UINT8                   *OutputBuffer)
199 {
200     ACPI_STATUS             Status;
201     ACPI_SIZE               BytesParsed = 0;
202     UINT8                   ResourceType = 0;
203     ACPI_SIZE               BytesConsumed = 0;
204     UINT8                   *Buffer = OutputBuffer;
205     ACPI_SIZE               StructureSize = 0;
206     BOOLEAN                 EndTagProcessed = FALSE;
207     ACPI_RESOURCE           *Resource;
208
209     ACPI_FUNCTION_TRACE ("RsByteStreamToList");
210
211
212     while (BytesParsed < ByteStreamBufferLength &&
213             !EndTagProcessed)
214     {
215         /*
216          * The next byte in the stream is the resource type
217          */
218         ResourceType = AcpiRsGetResourceType (*ByteStreamBuffer);
219
220         switch (ResourceType)
221         {
222         case ACPI_RDESC_TYPE_MEMORY_24:
223             /*
224              * 24-Bit Memory Resource
225              */
226             Status = AcpiRsMemory24Resource (ByteStreamBuffer,
227                         &BytesConsumed, &Buffer, &StructureSize);
228             break;
229
230
231         case ACPI_RDESC_TYPE_LARGE_VENDOR:
232             /*
233              * Vendor Defined Resource
234              */
235             Status = AcpiRsVendorResource (ByteStreamBuffer,
236                         &BytesConsumed, &Buffer, &StructureSize);
237             break;
238
239
240         case ACPI_RDESC_TYPE_MEMORY_32:
241             /*
242              * 32-Bit Memory Range Resource
243              */
244             Status = AcpiRsMemory32RangeResource (ByteStreamBuffer,
245                         &BytesConsumed, &Buffer, &StructureSize);
246             break;
247
248
249         case ACPI_RDESC_TYPE_FIXED_MEMORY_32:
250             /*
251              * 32-Bit Fixed Memory Resource
252              */
253             Status = AcpiRsFixedMemory32Resource (ByteStreamBuffer,
254                         &BytesConsumed, &Buffer, &StructureSize);
255             break;
256
257
258         case ACPI_RDESC_TYPE_QWORD_ADDRESS_SPACE:
259             /*
260              * 64-Bit Address Resource
261              */
262             Status = AcpiRsAddress64Resource (ByteStreamBuffer,
263                         &BytesConsumed, &Buffer, &StructureSize);
264             break;
265
266
267         case ACPI_RDESC_TYPE_DWORD_ADDRESS_SPACE:
268             /*
269              * 32-Bit Address Resource
270              */
271             Status = AcpiRsAddress32Resource (ByteStreamBuffer,
272                         &BytesConsumed, &Buffer, &StructureSize);
273             break;
274
275
276         case ACPI_RDESC_TYPE_WORD_ADDRESS_SPACE:
277             /*
278              * 16-Bit Address Resource
279              */
280             Status = AcpiRsAddress16Resource (ByteStreamBuffer,
281                         &BytesConsumed, &Buffer, &StructureSize);
282             break;
283
284
285         case ACPI_RDESC_TYPE_EXTENDED_XRUPT:
286             /*
287              * Extended IRQ
288              */
289             Status = AcpiRsExtendedIrqResource (ByteStreamBuffer,
290                         &BytesConsumed, &Buffer, &StructureSize);
291             break;
292
293
294         case ACPI_RDESC_TYPE_IRQ_FORMAT:
295             /*
296              * IRQ Resource
297              */
298             Status = AcpiRsIrqResource (ByteStreamBuffer,
299                         &BytesConsumed, &Buffer, &StructureSize);
300             break;
301
302
303         case ACPI_RDESC_TYPE_DMA_FORMAT:
304             /*
305              * DMA Resource
306              */
307             Status = AcpiRsDmaResource (ByteStreamBuffer,
308                         &BytesConsumed, &Buffer, &StructureSize);
309             break;
310
311
312         case ACPI_RDESC_TYPE_START_DEPENDENT:
313             /*
314              * Start Dependent Functions Resource
315              */
316             Status = AcpiRsStartDependFnsResource (ByteStreamBuffer,
317                         &BytesConsumed, &Buffer, &StructureSize);
318             break;
319
320
321         case ACPI_RDESC_TYPE_END_DEPENDENT:
322             /*
323              * End Dependent Functions Resource
324              */
325             Status = AcpiRsEndDependFnsResource (ByteStreamBuffer,
326                         &BytesConsumed, &Buffer, &StructureSize);
327             break;
328
329
330         case ACPI_RDESC_TYPE_IO_PORT:
331             /*
332              * IO Port Resource
333              */
334             Status = AcpiRsIoResource (ByteStreamBuffer,
335                         &BytesConsumed, &Buffer, &StructureSize);
336             break;
337
338
339         case ACPI_RDESC_TYPE_FIXED_IO_PORT:
340             /*
341              * Fixed IO Port Resource
342              */
343             Status = AcpiRsFixedIoResource (ByteStreamBuffer,
344                         &BytesConsumed, &Buffer, &StructureSize);
345             break;
346
347
348         case ACPI_RDESC_TYPE_SMALL_VENDOR:
349             /*
350              * Vendor Specific Resource
351              */
352             Status = AcpiRsVendorResource (ByteStreamBuffer,
353                         &BytesConsumed, &Buffer, &StructureSize);
354             break;
355
356
357         case ACPI_RDESC_TYPE_END_TAG:
358             /*
359              * End Tag
360              */
361             EndTagProcessed = TRUE;
362             Status = AcpiRsEndTagResource (ByteStreamBuffer,
363                         &BytesConsumed, &Buffer, &StructureSize);
364             break;
365
366
367         default:
368             /*
369              * Invalid/Unknown resource type
370              */
371             Status = AE_AML_INVALID_RESOURCE_TYPE;
372             break;
373         }
374
375         if (ACPI_FAILURE (Status))
376         {
377             return_ACPI_STATUS (Status);
378         }
379
380         /*
381          * Update the return value and counter
382          */
383         BytesParsed += BytesConsumed;
384
385         /*
386          * Set the byte stream to point to the next resource
387          */
388         ByteStreamBuffer += BytesConsumed;
389
390         /*
391          * Set the Buffer to the next structure
392          */
393         Resource = ACPI_CAST_PTR (ACPI_RESOURCE, Buffer);
394         Resource->Length = ACPI_ALIGN_RESOURCE_SIZE(Resource->Length);
395         Buffer += ACPI_ALIGN_RESOURCE_SIZE(StructureSize);
396
397     } /*  end while */
398
399     /*
400      * Check the reason for exiting the while loop
401      */
402     if (!EndTagProcessed)
403     {
404         return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
405     }
406
407     return_ACPI_STATUS (AE_OK);
408 }
409
410
411 /*******************************************************************************
412  *
413  * FUNCTION:    AcpiRsListToByteStream
414  *
415  * PARAMETERS:  LinkedList              - Pointer to the resource linked list
416  *              ByteSteamSizeNeeded     - Calculated size of the byte stream
417  *                                        needed from calling
418  *                                        AcpiRsGetByteStreamLength()
419  *                                        The size of the OutputBuffer is
420  *                                        guaranteed to be >=
421  *                                        ByteStreamSizeNeeded
422  *              OutputBuffer            - Pointer to the buffer that will
423  *                                        contain the byte stream
424  *
425  * RETURN:      Status
426  *
427  * DESCRIPTION: Takes the resource linked list and parses it, creating a
428  *              byte stream of resources in the caller's output buffer
429  *
430  ******************************************************************************/
431
432 ACPI_STATUS
433 AcpiRsListToByteStream (
434     ACPI_RESOURCE           *LinkedList,
435     ACPI_SIZE               ByteStreamSizeNeeded,
436     UINT8                   *OutputBuffer)
437 {
438     ACPI_STATUS             Status;
439     UINT8                   *Buffer = OutputBuffer;
440     ACPI_SIZE               BytesConsumed = 0;
441     BOOLEAN                 Done = FALSE;
442
443
444     ACPI_FUNCTION_TRACE ("RsListToByteStream");
445
446
447     while (!Done)
448     {
449         switch (LinkedList->Id)
450         {
451         case ACPI_RSTYPE_IRQ:
452             /*
453              * IRQ Resource
454              */
455             Status = AcpiRsIrqStream (LinkedList, &Buffer, &BytesConsumed);
456             break;
457
458         case ACPI_RSTYPE_DMA:
459             /*
460              * DMA Resource
461              */
462             Status = AcpiRsDmaStream (LinkedList, &Buffer, &BytesConsumed);
463             break;
464
465         case ACPI_RSTYPE_START_DPF:
466             /*
467              * Start Dependent Functions Resource
468              */
469             Status = AcpiRsStartDependFnsStream (LinkedList,
470                             &Buffer, &BytesConsumed);
471             break;
472
473         case ACPI_RSTYPE_END_DPF:
474             /*
475              * End Dependent Functions Resource
476              */
477             Status = AcpiRsEndDependFnsStream (LinkedList,
478                             &Buffer, &BytesConsumed);
479             break;
480
481         case ACPI_RSTYPE_IO:
482             /*
483              * IO Port Resource
484              */
485             Status = AcpiRsIoStream (LinkedList, &Buffer, &BytesConsumed);
486             break;
487
488         case ACPI_RSTYPE_FIXED_IO:
489             /*
490              * Fixed IO Port Resource
491              */
492             Status = AcpiRsFixedIoStream (LinkedList, &Buffer, &BytesConsumed);
493             break;
494
495         case ACPI_RSTYPE_VENDOR:
496             /*
497              * Vendor Defined Resource
498              */
499             Status = AcpiRsVendorStream (LinkedList, &Buffer, &BytesConsumed);
500             break;
501
502         case ACPI_RSTYPE_END_TAG:
503             /*
504              * End Tag
505              */
506             Status = AcpiRsEndTagStream (LinkedList, &Buffer, &BytesConsumed);
507
508             /*
509              * An End Tag indicates the end of the Resource Template
510              */
511             Done = TRUE;
512             break;
513
514         case ACPI_RSTYPE_MEM24:
515             /*
516              * 24-Bit Memory Resource
517              */
518             Status = AcpiRsMemory24Stream (LinkedList, &Buffer, &BytesConsumed);
519             break;
520
521         case ACPI_RSTYPE_MEM32:
522             /*
523              * 32-Bit Memory Range Resource
524              */
525             Status = AcpiRsMemory32RangeStream (LinkedList, &Buffer,
526                         &BytesConsumed);
527             break;
528
529         case ACPI_RSTYPE_FIXED_MEM32:
530             /*
531              * 32-Bit Fixed Memory Resource
532              */
533             Status = AcpiRsFixedMemory32Stream (LinkedList, &Buffer,
534                         &BytesConsumed);
535             break;
536
537         case ACPI_RSTYPE_ADDRESS16:
538             /*
539              * 16-Bit Address Descriptor Resource
540              */
541             Status = AcpiRsAddress16Stream (LinkedList, &Buffer,
542                         &BytesConsumed);
543             break;
544
545         case ACPI_RSTYPE_ADDRESS32:
546             /*
547              * 32-Bit Address Descriptor Resource
548              */
549             Status = AcpiRsAddress32Stream (LinkedList, &Buffer,
550                         &BytesConsumed);
551             break;
552
553         case ACPI_RSTYPE_ADDRESS64:
554             /*
555              * 64-Bit Address Descriptor Resource
556              */
557             Status = AcpiRsAddress64Stream (LinkedList, &Buffer,
558                         &BytesConsumed);
559             break;
560
561         case ACPI_RSTYPE_EXT_IRQ:
562             /*
563              * Extended IRQ Resource
564              */
565             Status = AcpiRsExtendedIrqStream (LinkedList, &Buffer,
566                         &BytesConsumed);
567             break;
568
569         default:
570             /*
571              * If we get here, everything is out of sync,
572              *  so exit with an error
573              */
574             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid descriptor type (%X) in resource list\n",
575                 LinkedList->Id));
576             Status = AE_BAD_DATA;
577             break;
578
579         } /* switch (LinkedList->Id) */
580
581         if (ACPI_FAILURE (Status))
582         {
583             return_ACPI_STATUS (Status);
584         }
585
586         /*
587          * Set the Buffer to point to the open byte
588          */
589         Buffer += BytesConsumed;
590
591         /*
592          * Point to the next object
593          */
594         LinkedList = ACPI_PTR_ADD (ACPI_RESOURCE,
595                         LinkedList, LinkedList->Length);
596     }
597
598     return_ACPI_STATUS (AE_OK);
599 }
600