Merge branch 'vendor/LIBPCAP'
[dragonfly.git] / sys / contrib / dev / acpica / source / components / resources / rscreate.c
1 /*******************************************************************************
2  *
3  * Module Name: rscreate - Create resource lists/tables
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, 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 #define __RSCREATE_C__
45
46 #include "acpi.h"
47 #include "accommon.h"
48 #include "acresrc.h"
49 #include "acnamesp.h"
50
51 #define _COMPONENT          ACPI_RESOURCES
52         ACPI_MODULE_NAME    ("rscreate")
53
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    AcpiBufferToResource
58  *
59  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
60  *              AmlBufferLength     - Length of the AmlBuffer
61  *              ResourcePtr         - Where the converted resource is returned
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Convert a raw AML buffer to a resource list
66  *
67  ******************************************************************************/
68
69 ACPI_STATUS
70 AcpiBufferToResource (
71     UINT8                   *AmlBuffer,
72     UINT16                  AmlBufferLength,
73     ACPI_RESOURCE           **ResourcePtr)
74 {
75     ACPI_STATUS             Status;
76     ACPI_SIZE               ListSizeNeeded;
77     void                    *Resource;
78     void                    *CurrentResourcePtr;
79
80
81     ACPI_FUNCTION_TRACE (AcpiBufferToResource);
82
83
84     /*
85      * Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag
86      * is not required here.
87      */
88
89     /* Get the required length for the converted resource */
90
91     Status = AcpiRsGetListLength (AmlBuffer, AmlBufferLength,
92                 &ListSizeNeeded);
93     if (Status == AE_AML_NO_RESOURCE_END_TAG)
94     {
95         Status = AE_OK;
96     }
97     if (ACPI_FAILURE (Status))
98     {
99         return_ACPI_STATUS (Status);
100     }
101
102     /* Allocate a buffer for the converted resource */
103
104     Resource = ACPI_ALLOCATE_ZEROED (ListSizeNeeded);
105     CurrentResourcePtr = Resource;
106     if (!Resource)
107     {
108         return_ACPI_STATUS (AE_NO_MEMORY);
109     }
110
111     /* Perform the AML-to-Resource conversion */
112
113     Status = AcpiUtWalkAmlResources (NULL, AmlBuffer, AmlBufferLength,
114                 AcpiRsConvertAmlToResources, &CurrentResourcePtr);
115     if (Status == AE_AML_NO_RESOURCE_END_TAG)
116     {
117         Status = AE_OK;
118     }
119     if (ACPI_FAILURE (Status))
120     {
121         ACPI_FREE (Resource);
122     }
123     else
124     {
125         *ResourcePtr = Resource;
126     }
127
128     return_ACPI_STATUS (Status);
129 }
130
131 ACPI_EXPORT_SYMBOL (AcpiBufferToResource)
132
133
134 /*******************************************************************************
135  *
136  * FUNCTION:    AcpiRsCreateResourceList
137  *
138  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
139  *              OutputBuffer        - Pointer to the user's buffer
140  *
141  * RETURN:      Status: AE_OK if okay, else a valid ACPI_STATUS code
142  *              If OutputBuffer is not large enough, OutputBufferLength
143  *              indicates how large OutputBuffer should be, else it
144  *              indicates how may UINT8 elements of OutputBuffer are valid.
145  *
146  * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
147  *              execution and parses the stream to create a linked list
148  *              of device resources.
149  *
150  ******************************************************************************/
151
152 ACPI_STATUS
153 AcpiRsCreateResourceList (
154     ACPI_OPERAND_OBJECT     *AmlBuffer,
155     ACPI_BUFFER             *OutputBuffer)
156 {
157
158     ACPI_STATUS             Status;
159     UINT8                   *AmlStart;
160     ACPI_SIZE               ListSizeNeeded = 0;
161     UINT32                  AmlBufferLength;
162     void                    *Resource;
163
164
165     ACPI_FUNCTION_TRACE (RsCreateResourceList);
166
167
168     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlBuffer = %p\n",
169         AmlBuffer));
170
171     /* Params already validated, so we don't re-validate here */
172
173     AmlBufferLength = AmlBuffer->Buffer.Length;
174     AmlStart = AmlBuffer->Buffer.Pointer;
175
176     /*
177      * Pass the AmlBuffer into a module that can calculate
178      * the buffer size needed for the linked list
179      */
180     Status = AcpiRsGetListLength (AmlStart, AmlBufferLength,
181                 &ListSizeNeeded);
182
183     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
184         Status, (UINT32) ListSizeNeeded));
185     if (ACPI_FAILURE (Status))
186     {
187         return_ACPI_STATUS (Status);
188     }
189
190     /* Validate/Allocate/Clear caller buffer */
191
192     Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);
193     if (ACPI_FAILURE (Status))
194     {
195         return_ACPI_STATUS (Status);
196     }
197
198     /* Do the conversion */
199
200     Resource = OutputBuffer->Pointer;
201     Status = AcpiUtWalkAmlResources (NULL, AmlStart, AmlBufferLength,
202                 AcpiRsConvertAmlToResources, &Resource);
203     if (ACPI_FAILURE (Status))
204     {
205         return_ACPI_STATUS (Status);
206     }
207
208     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
209             OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
210     return_ACPI_STATUS (AE_OK);
211 }
212
213
214 /*******************************************************************************
215  *
216  * FUNCTION:    AcpiRsCreatePciRoutingTable
217  *
218  * PARAMETERS:  PackageObject           - Pointer to a package containing one
219  *                                        of more ACPI_OPERAND_OBJECTs
220  *              OutputBuffer            - Pointer to the user's buffer
221  *
222  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
223  *              If the OutputBuffer is too small, the error will be
224  *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
225  *              to the size buffer needed.
226  *
227  * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a
228  *              linked list of PCI interrupt descriptions
229  *
230  * NOTE: It is the caller's responsibility to ensure that the start of the
231  * output buffer is aligned properly (if necessary).
232  *
233  ******************************************************************************/
234
235 ACPI_STATUS
236 AcpiRsCreatePciRoutingTable (
237     ACPI_OPERAND_OBJECT     *PackageObject,
238     ACPI_BUFFER             *OutputBuffer)
239 {
240     UINT8                   *Buffer;
241     ACPI_OPERAND_OBJECT     **TopObjectList;
242     ACPI_OPERAND_OBJECT     **SubObjectList;
243     ACPI_OPERAND_OBJECT     *ObjDesc;
244     ACPI_SIZE               BufferSizeNeeded = 0;
245     UINT32                  NumberOfElements;
246     UINT32                  Index;
247     ACPI_PCI_ROUTING_TABLE  *UserPrt;
248     ACPI_NAMESPACE_NODE     *Node;
249     ACPI_STATUS             Status;
250     ACPI_BUFFER             PathBuffer;
251
252
253     ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable);
254
255
256     /* Params already validated, so we don't re-validate here */
257
258     /* Get the required buffer length */
259
260     Status = AcpiRsGetPciRoutingTableLength (PackageObject,
261                 &BufferSizeNeeded);
262     if (ACPI_FAILURE (Status))
263     {
264         return_ACPI_STATUS (Status);
265     }
266
267     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
268         (UINT32) BufferSizeNeeded));
269
270     /* Validate/Allocate/Clear caller buffer */
271
272     Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);
273     if (ACPI_FAILURE (Status))
274     {
275         return_ACPI_STATUS (Status);
276     }
277
278     /*
279      * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
280      * package that in turn contains an UINT64 Address, a UINT8 Pin,
281      * a Name, and a UINT8 SourceIndex.
282      */
283     TopObjectList    = PackageObject->Package.Elements;
284     NumberOfElements = PackageObject->Package.Count;
285     Buffer           = OutputBuffer->Pointer;
286     UserPrt          = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
287
288     for (Index = 0; Index < NumberOfElements; Index++)
289     {
290         /*
291          * Point UserPrt past this current structure
292          *
293          * NOTE: On the first iteration, UserPrt->Length will
294          * be zero because we cleared the return buffer earlier
295          */
296         Buffer += UserPrt->Length;
297         UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
298
299         /*
300          * Fill in the Length field with the information we have at this point.
301          * The minus four is to subtract the size of the UINT8 Source[4] member
302          * because it is added below.
303          */
304         UserPrt->Length = (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
305
306         /* Each subpackage must be of length 4 */
307
308         if ((*TopObjectList)->Package.Count != 4)
309         {
310             ACPI_ERROR ((AE_INFO,
311                 "(PRT[%u]) Need package of length 4, found length %u",
312                 Index, (*TopObjectList)->Package.Count));
313             return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);
314         }
315
316         /*
317          * Dereference the subpackage.
318          * The SubObjectList will now point to an array of the four IRQ
319          * elements: [Address, Pin, Source, SourceIndex]
320          */
321         SubObjectList = (*TopObjectList)->Package.Elements;
322
323         /* 1) First subobject: Dereference the PRT.Address */
324
325         ObjDesc = SubObjectList[0];
326         if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
327         {
328             ACPI_ERROR ((AE_INFO, "(PRT[%u].Address) Need Integer, found %s",
329                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
330             return_ACPI_STATUS (AE_BAD_DATA);
331         }
332
333         UserPrt->Address = ObjDesc->Integer.Value;
334
335         /* 2) Second subobject: Dereference the PRT.Pin */
336
337         ObjDesc = SubObjectList[1];
338         if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
339         {
340             ACPI_ERROR ((AE_INFO, "(PRT[%u].Pin) Need Integer, found %s",
341                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
342             return_ACPI_STATUS (AE_BAD_DATA);
343         }
344
345         UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;
346
347         /*
348          * 3) Third subobject: Dereference the PRT.SourceName
349          * The name may be unresolved (slack mode), so allow a null object
350          */
351         ObjDesc = SubObjectList[2];
352         if (ObjDesc)
353         {
354             switch (ObjDesc->Common.Type)
355             {
356             case ACPI_TYPE_LOCAL_REFERENCE:
357
358                 if (ObjDesc->Reference.Class != ACPI_REFCLASS_NAME)
359                 {
360                     ACPI_ERROR ((AE_INFO,
361                         "(PRT[%u].Source) Need name, found Reference Class 0x%X",
362                         Index, ObjDesc->Reference.Class));
363                     return_ACPI_STATUS (AE_BAD_DATA);
364                 }
365
366                 Node = ObjDesc->Reference.Node;
367
368                 /* Use *remaining* length of the buffer as max for pathname */
369
370                 PathBuffer.Length = OutputBuffer->Length -
371                                     (UINT32) ((UINT8 *) UserPrt->Source -
372                                     (UINT8 *) OutputBuffer->Pointer);
373                 PathBuffer.Pointer = UserPrt->Source;
374
375                 Status = AcpiNsHandleToPathname ((ACPI_HANDLE) Node, &PathBuffer);
376
377                 /* +1 to include null terminator */
378
379                 UserPrt->Length += (UINT32) ACPI_STRLEN (UserPrt->Source) + 1;
380                 break;
381
382             case ACPI_TYPE_STRING:
383
384                 ACPI_STRCPY (UserPrt->Source, ObjDesc->String.Pointer);
385
386                 /*
387                  * Add to the Length field the length of the string
388                  * (add 1 for terminator)
389                  */
390                 UserPrt->Length += ObjDesc->String.Length + 1;
391                 break;
392
393             case ACPI_TYPE_INTEGER:
394                 /*
395                  * If this is a number, then the Source Name is NULL, since the
396                  * entire buffer was zeroed out, we can leave this alone.
397                  *
398                  * Add to the Length field the length of the UINT32 NULL
399                  */
400                 UserPrt->Length += sizeof (UINT32);
401                 break;
402
403             default:
404
405                ACPI_ERROR ((AE_INFO,
406                    "(PRT[%u].Source) Need Ref/String/Integer, found %s",
407                    Index, AcpiUtGetObjectTypeName (ObjDesc)));
408                return_ACPI_STATUS (AE_BAD_DATA);
409             }
410         }
411
412         /* Now align the current length */
413
414         UserPrt->Length = (UINT32) ACPI_ROUND_UP_TO_64BIT (UserPrt->Length);
415
416         /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
417
418         ObjDesc = SubObjectList[3];
419         if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
420         {
421             ACPI_ERROR ((AE_INFO,
422                 "(PRT[%u].SourceIndex) Need Integer, found %s",
423                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
424             return_ACPI_STATUS (AE_BAD_DATA);
425         }
426
427         UserPrt->SourceIndex = (UINT32) ObjDesc->Integer.Value;
428
429         /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
430
431         TopObjectList++;
432     }
433
434     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
435             OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
436     return_ACPI_STATUS (AE_OK);
437 }
438
439
440 /*******************************************************************************
441  *
442  * FUNCTION:    AcpiRsCreateAmlResources
443  *
444  * PARAMETERS:  ResourceList            - Pointer to the resource list buffer
445  *              OutputBuffer            - Where the AML buffer is returned
446  *
447  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
448  *              If the OutputBuffer is too small, the error will be
449  *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
450  *              to the size buffer needed.
451  *
452  * DESCRIPTION: Converts a list of device resources to an AML bytestream
453  *              to be used as input for the _SRS control method.
454  *
455  ******************************************************************************/
456
457 ACPI_STATUS
458 AcpiRsCreateAmlResources (
459     ACPI_BUFFER             *ResourceList,
460     ACPI_BUFFER             *OutputBuffer)
461 {
462     ACPI_STATUS             Status;
463     ACPI_SIZE               AmlSizeNeeded = 0;
464
465
466     ACPI_FUNCTION_TRACE (RsCreateAmlResources);
467
468
469     /* Params already validated, no need to re-validate here */
470
471     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ResourceList Buffer = %p\n",
472         ResourceList->Pointer));
473
474     /* Get the buffer size needed for the AML byte stream */
475
476     Status = AcpiRsGetAmlLength (ResourceList->Pointer,
477                 ResourceList->Length, &AmlSizeNeeded);
478
479     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
480         (UINT32) AmlSizeNeeded, AcpiFormatException (Status)));
481     if (ACPI_FAILURE (Status))
482     {
483         return_ACPI_STATUS (Status);
484     }
485
486     /* Validate/Allocate/Clear caller buffer */
487
488     Status = AcpiUtInitializeBuffer (OutputBuffer, AmlSizeNeeded);
489     if (ACPI_FAILURE (Status))
490     {
491         return_ACPI_STATUS (Status);
492     }
493
494     /* Do the conversion */
495
496     Status = AcpiRsConvertResourcesToAml (ResourceList->Pointer,
497                 AmlSizeNeeded, OutputBuffer->Pointer);
498     if (ACPI_FAILURE (Status))
499     {
500         return_ACPI_STATUS (Status);
501     }
502
503     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
504         OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
505     return_ACPI_STATUS (AE_OK);
506 }