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