rscreate.c revision 91116
1272343Sngie/*******************************************************************************
2 *
3 * Module Name: rscreate - Create resource lists/tables
4 *              $Revision: 53 $
5 *
6 ******************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2002, 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 "acpi.h"
121#include "acresrc.h"
122#include "amlcode.h"
123#include "acnamesp.h"
124
125#define _COMPONENT          ACPI_RESOURCES
126        ACPI_MODULE_NAME    ("rscreate")
127
128
129/*******************************************************************************
130 *
131 * FUNCTION:    AcpiRsCreateResourceList
132 *
133 * PARAMETERS:  ByteStreamBuffer        - Pointer to the resource byte stream
134 *              OutputBuffer            - Pointer to the user's buffer
135 *              OutputBufferLength      - Pointer to the size of OutputBuffer
136 *
137 * RETURN:      Status  - AE_OK if okay, else a valid ACPI_STATUS code
138 *              If OutputBuffer is not large enough, OutputBufferLength
139 *              indicates how large OutputBuffer should be, else it
140 *              indicates how may UINT8 elements of OutputBuffer are valid.
141 *
142 * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
143 *              execution and parses the stream to create a linked list
144 *              of device resources.
145 *
146 ******************************************************************************/
147
148ACPI_STATUS
149AcpiRsCreateResourceList (
150    ACPI_OPERAND_OBJECT     *ByteStreamBuffer,
151    ACPI_BUFFER             *OutputBuffer)
152{
153
154    ACPI_STATUS             Status;
155    UINT8                   *ByteStreamStart;
156    ACPI_SIZE               ListSizeNeeded = 0;
157    UINT32                  ByteStreamBufferLength;
158
159
160    ACPI_FUNCTION_TRACE ("RsCreateResourceList");
161
162
163    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ByteStreamBuffer = %p\n", ByteStreamBuffer));
164
165    /*
166     * Params already validated, so we don't re-validate here
167     */
168    ByteStreamBufferLength = ByteStreamBuffer->Buffer.Length;
169    ByteStreamStart = ByteStreamBuffer->Buffer.Pointer;
170
171    /*
172     * Pass the ByteStreamBuffer into a module that can calculate
173     * the buffer size needed for the linked list
174     */
175    Status = AcpiRsCalculateListLength (ByteStreamStart, ByteStreamBufferLength,
176                &ListSizeNeeded);
177
178    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
179        Status, ListSizeNeeded));
180    if (ACPI_FAILURE (Status))
181    {
182        return_ACPI_STATUS (Status);
183    }
184
185    /* Validate/Allocate/Clear caller buffer */
186
187    Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);
188    if (ACPI_FAILURE (Status))
189    {
190        return_ACPI_STATUS (Status);
191    }
192
193    /* Do the conversion */
194
195    Status = AcpiRsByteStreamToList (ByteStreamStart, ByteStreamBufferLength,
196                    OutputBuffer->Pointer);
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, 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 *              OutputBufferLength      - Size of OutputBuffer
216 *
217 * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
218 *              If the OutputBuffer is too small, the error will be
219 *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
220 *              to the size buffer needed.
221 *
222 * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT  package and creates a
223 *              linked list of PCI interrupt descriptions
224 *
225 * NOTE: It is the caller's responsibility to ensure that the start of the
226 * output buffer is aligned properly (if necessary).
227 *
228 ******************************************************************************/
229
230ACPI_STATUS
231AcpiRsCreatePciRoutingTable (
232    ACPI_OPERAND_OBJECT     *PackageObject,
233    ACPI_BUFFER             *OutputBuffer)
234{
235    UINT8                   *Buffer;
236    ACPI_OPERAND_OBJECT     **TopObjectList = NULL;
237    ACPI_OPERAND_OBJECT     **SubObjectList = NULL;
238    ACPI_OPERAND_OBJECT     *PackageElement = NULL;
239    ACPI_SIZE               BufferSizeNeeded = 0;
240    UINT32                  NumberOfElements = 0;
241    UINT32                  Index = 0;
242    ACPI_PCI_ROUTING_TABLE  *UserPrt = NULL;
243    ACPI_NAMESPACE_NODE     *Node;
244    ACPI_STATUS             Status;
245    ACPI_BUFFER             PathBuffer;
246
247
248    ACPI_FUNCTION_TRACE ("RsCreatePciRoutingTable");
249
250
251    /* Params already validated, so we don't re-validate here */
252
253    /*
254     * Get the required buffer length
255     */
256    Status = AcpiRsCalculatePciRoutingTableLength (PackageObject,
257                &BufferSizeNeeded);
258    if (ACPI_FAILURE (Status))
259    {
260        return_ACPI_STATUS (Status);
261    }
262
263    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n", BufferSizeNeeded));
264
265    /* Validate/Allocate/Clear caller buffer */
266
267    Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);
268    if (ACPI_FAILURE (Status))
269    {
270        return_ACPI_STATUS (Status);
271    }
272
273    /*
274     * Loop through the ACPI_INTERNAL_OBJECTS - Each object should contain an
275     * ACPI_INTEGER Address, a UINT8 Pin, a Name and a UINT8 SourceIndex.
276     */
277    TopObjectList    = PackageObject->Package.Elements;
278    NumberOfElements = PackageObject->Package.Count;
279    Buffer           = OutputBuffer->Pointer;
280    UserPrt          = (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_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        /*
301         * Dereference the sub-package
302         */
303        PackageElement = *TopObjectList;
304
305        /*
306         * The SubObjectList will now point to an array of the four IRQ
307         * elements: Address, Pin, Source and SourceIndex
308         */
309        SubObjectList = PackageElement->Package.Elements;
310
311        /*
312         * 1) First subobject:  Dereference the Address
313         */
314        if (ACPI_TYPE_INTEGER == (*SubObjectList)->Common.Type)
315        {
316            UserPrt->Address = (*SubObjectList)->Integer.Value;
317        }
318        else
319        {
320            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Need Integer, found %s\n",
321                AcpiUtGetTypeName ((*SubObjectList)->Common.Type)));
322            return_ACPI_STATUS (AE_BAD_DATA);
323        }
324
325        /*
326         * 2) Second subobject: Dereference the Pin
327         */
328        SubObjectList++;
329
330        if (ACPI_TYPE_INTEGER == (*SubObjectList)->Common.Type)
331        {
332            UserPrt->Pin = (UINT32) (*SubObjectList)->Integer.Value;
333        }
334        else
335        {
336            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Need Integer, found %s\n",
337                AcpiUtGetTypeName ((*SubObjectList)->Common.Type)));
338            return_ACPI_STATUS (AE_BAD_DATA);
339        }
340
341        /*
342         * 3) Third subobject: Dereference the Source Name
343         */
344        SubObjectList++;
345
346        switch ((*SubObjectList)->Common.Type)
347        {
348        case INTERNAL_TYPE_REFERENCE:
349
350            if ((*SubObjectList)->Reference.Opcode != AML_INT_NAMEPATH_OP)
351            {
352               ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Need name, found reference op %X\n",
353                    (*SubObjectList)->Reference.Opcode));
354                return_ACPI_STATUS (AE_BAD_DATA);
355            }
356
357            Node = (*SubObjectList)->Reference.Node;
358
359            /* Use *remaining* length of the buffer as max for pathname */
360
361            PathBuffer.Length = OutputBuffer->Length -
362                                (UINT32) ((UINT8 *) UserPrt->Source - (UINT8 *) OutputBuffer->Pointer);
363            PathBuffer.Pointer = UserPrt->Source;
364
365            Status = AcpiNsHandleToPathname ((ACPI_HANDLE *) Node, &PathBuffer);
366
367            UserPrt->Length += ACPI_STRLEN (UserPrt->Source) + 1; /* include null terminator */
368            break;
369
370
371        case ACPI_TYPE_STRING:
372
373            ACPI_STRCPY (UserPrt->Source,
374                  (*SubObjectList)->String.Pointer);
375
376            /* Add to the Length field the length of the string */
377
378            UserPrt->Length += (*SubObjectList)->String.Length;
379            break;
380
381
382        case ACPI_TYPE_INTEGER:
383            /*
384             * If this is a number, then the Source Name is NULL, since the
385             * entire buffer was zeroed out, we can leave this alone.
386             *
387             * Add to the Length field the length of the UINT32 NULL
388             */
389            UserPrt->Length += sizeof (UINT32);
390            break;
391
392
393        default:
394
395           ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Need Integer, found %s\n",
396                AcpiUtGetTypeName ((*SubObjectList)->Common.Type)));
397           return_ACPI_STATUS (AE_BAD_DATA);
398        }
399
400        /* Now align the current length */
401
402        UserPrt->Length = ACPI_ROUND_UP_TO_64BITS (UserPrt->Length);
403
404        /*
405         * 4) Fourth subobject: Dereference the Source Index
406         */
407        SubObjectList++;
408
409        if (ACPI_TYPE_INTEGER == (*SubObjectList)->Common.Type)
410        {
411            UserPrt->SourceIndex = (UINT32) (*SubObjectList)->Integer.Value;
412        }
413        else
414        {
415            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Need Integer, found %s\n",
416                AcpiUtGetTypeName ((*SubObjectList)->Common.Type)));
417            return_ACPI_STATUS (AE_BAD_DATA);
418        }
419
420        /* Point to the next ACPI_OPERAND_OBJECT */
421
422        TopObjectList++;
423    }
424
425    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
426            OutputBuffer->Pointer, OutputBuffer->Length));
427    return_ACPI_STATUS (AE_OK);
428}
429
430
431/*******************************************************************************
432 *
433 * FUNCTION:    AcpiRsCreateByteStream
434 *
435 * PARAMETERS:  LinkedListBuffer        - Pointer to the resource linked list
436 *              OutputBuffer            - Pointer to the user's buffer
437 *
438 * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
439 *              If the OutputBuffer is too small, the error will be
440 *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
441 *              to the size buffer needed.
442 *
443 * DESCRIPTION: Takes the linked list of device resources and
444 *              creates a bytestream to be used as input for the
445 *              _SRS control method.
446 *
447 ******************************************************************************/
448
449ACPI_STATUS
450AcpiRsCreateByteStream (
451    ACPI_RESOURCE           *LinkedListBuffer,
452    ACPI_BUFFER             *OutputBuffer)
453{
454    ACPI_STATUS             Status;
455    ACPI_SIZE               ByteStreamSizeNeeded = 0;
456
457
458    ACPI_FUNCTION_TRACE ("RsCreateByteStream");
459
460
461    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "LinkedListBuffer = %p\n", LinkedListBuffer));
462
463    /*
464     * Params already validated, so we don't re-validate here
465     *
466     * Pass the LinkedListBuffer into a module that calculates
467     * the buffer size needed for the byte stream.
468     */
469    Status = AcpiRsCalculateByteStreamLength (LinkedListBuffer,
470                &ByteStreamSizeNeeded);
471
472    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ByteStreamSizeNeeded=%X, %s\n",
473        ByteStreamSizeNeeded, AcpiFormatException (Status)));
474    if (ACPI_FAILURE (Status))
475    {
476        return_ACPI_STATUS (Status);
477    }
478
479    /* Validate/Allocate/Clear caller buffer */
480
481    Status = AcpiUtInitializeBuffer (OutputBuffer, ByteStreamSizeNeeded);
482    if (ACPI_FAILURE (Status))
483    {
484        return_ACPI_STATUS (Status);
485    }
486
487    /* Do the conversion */
488
489    Status = AcpiRsListToByteStream (LinkedListBuffer, ByteStreamSizeNeeded,
490                    OutputBuffer->Pointer);
491    if (ACPI_FAILURE (Status))
492    {
493        return_ACPI_STATUS (Status);
494    }
495
496    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
497            OutputBuffer->Pointer, OutputBuffer->Length));
498    return_ACPI_STATUS (AE_OK);
499}
500
501