rscreate.c revision 151937
1/*******************************************************************************
2 *
3 * Module Name: rscreate - Create resource lists/tables
4 *              $Revision: 1.71 $
5 *
6 ******************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2005, 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
117
118#define __RSCREATE_C__
119
120#include <contrib/dev/acpica/acpi.h>
121#include <contrib/dev/acpica/acresrc.h>
122#include <contrib/dev/acpica/amlcode.h>
123#include <contrib/dev/acpica/acnamesp.h>
124
125#define _COMPONENT          ACPI_RESOURCES
126        ACPI_MODULE_NAME    ("rscreate")
127
128
129/*******************************************************************************
130 *
131 * FUNCTION:    AcpiRsCreateResourceList
132 *
133 * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
134 *              OutputBuffer        - Pointer to the user's buffer
135 *
136 * RETURN:      Status: AE_OK if okay, else a valid ACPI_STATUS code
137 *              If OutputBuffer is not large enough, OutputBufferLength
138 *              indicates how large OutputBuffer should be, else it
139 *              indicates how may UINT8 elements of OutputBuffer are valid.
140 *
141 * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
142 *              execution and parses the stream to create a linked list
143 *              of device resources.
144 *
145 ******************************************************************************/
146
147ACPI_STATUS
148AcpiRsCreateResourceList (
149    ACPI_OPERAND_OBJECT     *AmlBuffer,
150    ACPI_BUFFER             *OutputBuffer)
151{
152
153    ACPI_STATUS             Status;
154    UINT8                   *AmlStart;
155    ACPI_SIZE               ListSizeNeeded = 0;
156    UINT32                  AmlBufferLength;
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    Status = AcpiRsConvertAmlToResources (AmlStart, AmlBufferLength,
195                    OutputBuffer->Pointer);
196    if (ACPI_FAILURE (Status))
197    {
198        return_ACPI_STATUS (Status);
199    }
200
201    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
202            OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
203    return_ACPI_STATUS (AE_OK);
204}
205
206
207/*******************************************************************************
208 *
209 * FUNCTION:    AcpiRsCreatePciRoutingTable
210 *
211 * PARAMETERS:  PackageObject           - Pointer to an ACPI_OPERAND_OBJECT
212 *                                        package
213 *              OutputBuffer            - Pointer to the user's buffer
214 *
215 * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
216 *              If the OutputBuffer is too small, the error will be
217 *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
218 *              to the size buffer needed.
219 *
220 * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT  package and creates a
221 *              linked list of PCI interrupt descriptions
222 *
223 * NOTE: It is the caller's responsibility to ensure that the start of the
224 * output buffer is aligned properly (if necessary).
225 *
226 ******************************************************************************/
227
228ACPI_STATUS
229AcpiRsCreatePciRoutingTable (
230    ACPI_OPERAND_OBJECT     *PackageObject,
231    ACPI_BUFFER             *OutputBuffer)
232{
233    UINT8                   *Buffer;
234    ACPI_OPERAND_OBJECT     **TopObjectList;
235    ACPI_OPERAND_OBJECT     **SubObjectList;
236    ACPI_OPERAND_OBJECT     *ObjDesc;
237    ACPI_SIZE               BufferSizeNeeded = 0;
238    UINT32                  NumberOfElements;
239    UINT32                  Index;
240    ACPI_PCI_ROUTING_TABLE  *UserPrt;
241    ACPI_NAMESPACE_NODE     *Node;
242    ACPI_STATUS             Status;
243    ACPI_BUFFER             PathBuffer;
244
245
246    ACPI_FUNCTION_TRACE ("RsCreatePciRoutingTable");
247
248
249    /* Params already validated, so we don't re-validate here */
250
251    /* Get the required buffer length */
252
253    Status = AcpiRsGetPciRoutingTableLength (PackageObject,
254                &BufferSizeNeeded);
255    if (ACPI_FAILURE (Status))
256    {
257        return_ACPI_STATUS (Status);
258    }
259
260    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
261        (UINT32) BufferSizeNeeded));
262
263    /* Validate/Allocate/Clear caller buffer */
264
265    Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);
266    if (ACPI_FAILURE (Status))
267    {
268        return_ACPI_STATUS (Status);
269    }
270
271    /*
272     * Loop through the ACPI_INTERNAL_OBJECTS - Each object
273     * should be a package that in turn contains an
274     * ACPI_INTEGER Address, a UINT8 Pin, a Name and a UINT8 SourceIndex.
275     */
276    TopObjectList    = PackageObject->Package.Elements;
277    NumberOfElements = PackageObject->Package.Count;
278    Buffer           = OutputBuffer->Pointer;
279    UserPrt          = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
280
281    for (Index = 0; Index < NumberOfElements; Index++)
282    {
283        /*
284         * Point UserPrt past this current structure
285         *
286         * NOTE: On the first iteration, UserPrt->Length will
287         * be zero because we cleared the return buffer earlier
288         */
289        Buffer += UserPrt->Length;
290        UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
291
292        /*
293         * Fill in the Length field with the information we have at this point.
294         * The minus four is to subtract the size of the UINT8 Source[4] member
295         * because it is added below.
296         */
297        UserPrt->Length = (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
298
299        /* Each element of the top-level package must also be a package */
300
301        if (ACPI_GET_OBJECT_TYPE (*TopObjectList) != ACPI_TYPE_PACKAGE)
302        {
303            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
304                "(PRT[%X]) Need sub-package, found %s\n",
305                Index, AcpiUtGetObjectTypeName (*TopObjectList)));
306            return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
307        }
308
309        /* Each sub-package must be of length 4 */
310
311        if ((*TopObjectList)->Package.Count != 4)
312        {
313            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
314                "(PRT[%X]) Need package of length 4, found length %d\n",
315                Index, (*TopObjectList)->Package.Count));
316            return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);
317        }
318
319        /*
320         * Dereference the sub-package.
321         * The SubObjectList will now point to an array of the four IRQ
322         * elements: [Address, Pin, Source, SourceIndex]
323         */
324        SubObjectList = (*TopObjectList)->Package.Elements;
325
326        /* 1) First subobject: Dereference the PRT.Address */
327
328        ObjDesc = SubObjectList[0];
329        if (ACPI_GET_OBJECT_TYPE (ObjDesc) == ACPI_TYPE_INTEGER)
330        {
331            UserPrt->Address = ObjDesc->Integer.Value;
332        }
333        else
334        {
335            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
336                "(PRT[%X].Address) Need Integer, found %s\n",
337                Index, AcpiUtGetObjectTypeName (ObjDesc)));
338            return_ACPI_STATUS (AE_BAD_DATA);
339        }
340
341        /* 2) Second subobject: Dereference the PRT.Pin */
342
343        ObjDesc = SubObjectList[1];
344        if (ACPI_GET_OBJECT_TYPE (ObjDesc) == ACPI_TYPE_INTEGER)
345        {
346            UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;
347        }
348        else
349        {
350            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
351                "(PRT[%X].Pin) Need Integer, found %s\n",
352                Index, AcpiUtGetObjectTypeName (ObjDesc)));
353            return_ACPI_STATUS (AE_BAD_DATA);
354        }
355
356        /* 3) Third subobject: Dereference the PRT.SourceName */
357
358        ObjDesc = SubObjectList[2];
359        switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
360        {
361        case ACPI_TYPE_LOCAL_REFERENCE:
362
363            if (ObjDesc->Reference.Opcode != AML_INT_NAMEPATH_OP)
364            {
365                ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
366                    "(PRT[%X].Source) Need name, found reference op %X\n",
367                    Index, ObjDesc->Reference.Opcode));
368                return_ACPI_STATUS (AE_BAD_DATA);
369            }
370
371            Node = ObjDesc->Reference.Node;
372
373            /* Use *remaining* length of the buffer as max for pathname */
374
375            PathBuffer.Length = OutputBuffer->Length -
376                                (UINT32) ((UINT8 *) UserPrt->Source -
377                                (UINT8 *) OutputBuffer->Pointer);
378            PathBuffer.Pointer = UserPrt->Source;
379
380            Status = AcpiNsHandleToPathname ((ACPI_HANDLE) Node, &PathBuffer);
381
382            /* +1 to include null terminator */
383
384            UserPrt->Length += (UINT32) ACPI_STRLEN (UserPrt->Source) + 1;
385            break;
386
387
388        case ACPI_TYPE_STRING:
389
390            ACPI_STRCPY (UserPrt->Source, ObjDesc->String.Pointer);
391
392            /*
393             * Add to the Length field the length of the string
394             * (add 1 for terminator)
395             */
396            UserPrt->Length += ObjDesc->String.Length + 1;
397            break;
398
399
400        case ACPI_TYPE_INTEGER:
401            /*
402             * If this is a number, then the Source Name is NULL, since the
403             * entire buffer was zeroed out, we can leave this alone.
404             *
405             * Add to the Length field the length of the UINT32 NULL
406             */
407            UserPrt->Length += sizeof (UINT32);
408            break;
409
410
411        default:
412
413           ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
414               "(PRT[%X].Source) Need Ref/String/Integer, found %s\n",
415                Index, AcpiUtGetObjectTypeName (ObjDesc)));
416           return_ACPI_STATUS (AE_BAD_DATA);
417        }
418
419        /* Now align the current length */
420
421        UserPrt->Length = (UINT32) ACPI_ROUND_UP_TO_64BITS (UserPrt->Length);
422
423        /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
424
425        ObjDesc = SubObjectList[3];
426        if (ACPI_GET_OBJECT_TYPE (ObjDesc) == ACPI_TYPE_INTEGER)
427        {
428            UserPrt->SourceIndex = (UINT32) ObjDesc->Integer.Value;
429        }
430        else
431        {
432            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
433                "(PRT[%X].SourceIndex) Need Integer, found %s\n",
434                Index, AcpiUtGetObjectTypeName (ObjDesc)));
435            return_ACPI_STATUS (AE_BAD_DATA);
436        }
437
438        /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
439
440        TopObjectList++;
441    }
442
443    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
444            OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
445    return_ACPI_STATUS (AE_OK);
446}
447
448
449/*******************************************************************************
450 *
451 * FUNCTION:    AcpiRsCreateAmlResources
452 *
453 * PARAMETERS:  LinkedListBuffer        - Pointer to the resource linked list
454 *              OutputBuffer            - Pointer to the user's buffer
455 *
456 * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
457 *              If the OutputBuffer is too small, the error will be
458 *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
459 *              to the size buffer needed.
460 *
461 * DESCRIPTION: Takes the linked list of device resources and
462 *              creates a bytestream to be used as input for the
463 *              _SRS control method.
464 *
465 ******************************************************************************/
466
467ACPI_STATUS
468AcpiRsCreateAmlResources (
469    ACPI_RESOURCE           *LinkedListBuffer,
470    ACPI_BUFFER             *OutputBuffer)
471{
472    ACPI_STATUS             Status;
473    ACPI_SIZE               AmlSizeNeeded = 0;
474
475
476    ACPI_FUNCTION_TRACE ("RsCreateAmlResources");
477
478
479    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "LinkedListBuffer = %p\n",
480        LinkedListBuffer));
481
482    /*
483     * Params already validated, so we don't re-validate here
484     *
485     * Pass the LinkedListBuffer into a module that calculates
486     * the buffer size needed for the byte stream.
487     */
488    Status = AcpiRsGetAmlLength (LinkedListBuffer,
489                &AmlSizeNeeded);
490
491    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
492        (UINT32) AmlSizeNeeded, AcpiFormatException (Status)));
493    if (ACPI_FAILURE (Status))
494    {
495        return_ACPI_STATUS (Status);
496    }
497
498    /* Validate/Allocate/Clear caller buffer */
499
500    Status = AcpiUtInitializeBuffer (OutputBuffer, AmlSizeNeeded);
501    if (ACPI_FAILURE (Status))
502    {
503        return_ACPI_STATUS (Status);
504    }
505
506    /* Do the conversion */
507
508    Status = AcpiRsConvertResourcesToAml (LinkedListBuffer, AmlSizeNeeded,
509                    OutputBuffer->Pointer);
510    if (ACPI_FAILURE (Status))
511    {
512        return_ACPI_STATUS (Status);
513    }
514
515    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
516            OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
517    return_ACPI_STATUS (AE_OK);
518}
519
520