rscreate.c revision 193529
10SN/A/*******************************************************************************
215485Sigerasim *
30SN/A * Module Name: rscreate - Create resource lists/tables
40SN/A *
50SN/A ******************************************************************************/
60SN/A
72362SN/A/******************************************************************************
80SN/A *
92362SN/A * 1. Copyright Notice
100SN/A *
110SN/A * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
120SN/A * All rights reserved.
130SN/A *
140SN/A * 2. License
150SN/A *
160SN/A * 2.1. This is your license from Intel Corp. under its intellectual property
170SN/A * rights.  You may have additional license terms from the party that provided
180SN/A * you this software, covering your right to use that party's intellectual
190SN/A * property rights.
200SN/A *
212362SN/A * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
222362SN/A * copy of the source code appearing in this file ("Covered Code") an
232362SN/A * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240SN/A * base code distributed originally by Intel ("Original Intel Code") to copy,
250SN/A * make derivatives, distribute, use and display any portion of the Covered
260SN/A * Code in any form, with the right to sublicense such rights; and
270SN/A *
280SN/A * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290SN/A * license (with the right to sublicense), under only those claims of Intel
300SN/A * patents that are infringed by the Original Intel Code, to make, use, sell,
310SN/A * offer to sell, and import the Covered Code and derivative works thereof
320SN/A * solely to the minimum extent necessary to exercise the above copyright
330SN/A * license, and in no event shall the patent license extend to any additions
340SN/A * to or modifications of the Original Intel Code.  No other license or right
350SN/A * is granted directly or by implication, estoppel or otherwise;
360SN/A *
370SN/A * The above copyright and patent license is granted only if the following
380SN/A * conditions are met:
390SN/A *
400SN/A * 3. Conditions
410SN/A *
420SN/A * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430SN/A * Redistribution of source code of any substantial portion of the Covered
440SN/A * Code or modification with rights to further distribute source must include
450SN/A * the above Copyright Notice, the above License, this list of Conditions,
460SN/A * and the following Disclaimer and Export Compliance provision.  In addition,
470SN/A * Licensee must cause all Covered Code to which Licensee contributes to
480SN/A * contain a file documenting the changes Licensee made to create that Covered
490SN/A * Code and the date of any change.  Licensee must include in that file the
500SN/A * documentation of any changes made by any predecessor Licensee.  Licensee
510SN/A * must include a prominent statement that the modification is derived,
520SN/A * directly or indirectly, from Original Intel Code.
530SN/A *
540SN/A * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550SN/A * Redistribution of source code of any substantial portion of the Covered
560SN/A * Code or modification without rights to further distribute source must
570SN/A * include the following Disclaimer and Export Compliance provision in the
580SN/A * documentation and/or other materials provided with distribution.  In
590SN/A * addition, Licensee may not authorize further sublicense of source of any
600SN/A * portion of the Covered Code, and must include terms to the effect that the
610SN/A * license from Licensee to its licensee is limited to the intellectual
620SN/A * property embodied in the software Licensee provides to its licensee, and
630SN/A * not to intellectual property embodied in modifications its licensee may
640SN/A * make.
650SN/A *
660SN/A * 3.3. Redistribution of Executable. Redistribution in executable form of any
670SN/A * substantial portion of the Covered Code or modification must reproduce the
680SN/A * above Copyright Notice, and the following Disclaimer and Export Compliance
690SN/A * provision in the documentation and/or other materials provided with the
700SN/A * distribution.
710SN/A *
720SN/A * 3.4. Intel retains all right, title, and interest in and to the Original
730SN/A * Intel Code.
740SN/A *
750SN/A * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760SN/A * Intel shall be used in advertising or otherwise to promote the sale, use or
770SN/A * other dealings in products derived from or relating to the Covered Code
7815485Sigerasim * without prior written authorization from Intel.
790SN/A *
800SN/A * 4. Disclaimer and Export Compliance
8115485Sigerasim *
820SN/A * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830SN/A * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840SN/A * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
850SN/A * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
860SN/A * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
870SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880SN/A * PARTICULAR PURPOSE.
890SN/A *
900SN/A * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910SN/A * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920SN/A * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930SN/A * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940SN/A * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950SN/A * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
960SN/A * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970SN/A * LIMITED REMEDY.
980SN/A *
990SN/A * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000SN/A * software or system incorporating such software without first obtaining any
1010SN/A * required license or other approval from the U. S. Department of Commerce or
1020SN/A * any other agency or department of the United States Government.  In the
1030SN/A * event Licensee exports any such software from the United States or
1040SN/A * re-exports any such software from a foreign destination, Licensee shall
1050SN/A * ensure that the distribution and export/re-export of the software is in
1060SN/A * compliance with all laws, regulations, orders, or other restrictions of the
1070SN/A * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080SN/A * any of its subsidiaries will export/re-export any technical data, process,
1090SN/A * software, or service, directly or indirectly, to any country for which the
1100SN/A * United States government or any agency thereof requires an export license,
1110SN/A * other governmental approval, or letter of assurance, without first obtaining
1120SN/A * such license, approval or letter.
1130SN/A *
1140SN/A *****************************************************************************/
1155116SN/A
1165116SN/A#define __RSCREATE_C__
1175116SN/A
1185116SN/A#include <contrib/dev/acpica/include/acpi.h>
1195116SN/A#include <contrib/dev/acpica/include/accommon.h>
1205116SN/A#include <contrib/dev/acpica/include/acresrc.h>
1215116SN/A#include <contrib/dev/acpica/include/acnamesp.h>
1225116SN/A
1235116SN/A#define _COMPONENT          ACPI_RESOURCES
1240SN/A        ACPI_MODULE_NAME    ("rscreate")
1255116SN/A
1260SN/A
1270SN/A/*******************************************************************************
1280SN/A *
1290SN/A * FUNCTION:    AcpiRsCreateResourceList
1300SN/A *
1310SN/A * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
1320SN/A *              OutputBuffer        - Pointer to the user's buffer
1330SN/A *
1340SN/A * RETURN:      Status: AE_OK if okay, else a valid ACPI_STATUS code
1350SN/A *              If OutputBuffer is not large enough, OutputBufferLength
1360SN/A *              indicates how large OutputBuffer should be, else it
1370SN/A *              indicates how may UINT8 elements of OutputBuffer are valid.
1380SN/A *
1390SN/A * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
1400SN/A *              execution and parses the stream to create a linked list
1410SN/A *              of device resources.
1420SN/A *
1430SN/A ******************************************************************************/
1440SN/A
1450SN/AACPI_STATUS
1460SN/AAcpiRsCreateResourceList (
147    ACPI_OPERAND_OBJECT     *AmlBuffer,
148    ACPI_BUFFER             *OutputBuffer)
149{
150
151    ACPI_STATUS             Status;
152    UINT8                   *AmlStart;
153    ACPI_SIZE               ListSizeNeeded = 0;
154    UINT32                  AmlBufferLength;
155    void                    *Resource;
156
157
158    ACPI_FUNCTION_TRACE (RsCreateResourceList);
159
160
161    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlBuffer = %p\n",
162        AmlBuffer));
163
164    /* Params already validated, so we don't re-validate here */
165
166    AmlBufferLength = AmlBuffer->Buffer.Length;
167    AmlStart = AmlBuffer->Buffer.Pointer;
168
169    /*
170     * Pass the AmlBuffer into a module that can calculate
171     * the buffer size needed for the linked list
172     */
173    Status = AcpiRsGetListLength (AmlStart, AmlBufferLength,
174                &ListSizeNeeded);
175
176    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
177        Status, (UINT32) ListSizeNeeded));
178    if (ACPI_FAILURE (Status))
179    {
180        return_ACPI_STATUS (Status);
181    }
182
183    /* Validate/Allocate/Clear caller buffer */
184
185    Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);
186    if (ACPI_FAILURE (Status))
187    {
188        return_ACPI_STATUS (Status);
189    }
190
191    /* Do the conversion */
192
193    Resource = OutputBuffer->Pointer;
194    Status = AcpiUtWalkAmlResources (AmlStart, AmlBufferLength,
195                AcpiRsConvertAmlToResources, &Resource);
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 should be a
273     * package that in turn contains an ACPI_INTEGER Address, a UINT8 Pin,
274     * 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 ((*TopObjectList)->Common.Type != ACPI_TYPE_PACKAGE)
302        {
303            ACPI_ERROR ((AE_INFO,
304                "(PRT[%X]) Need sub-package, found %s",
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_ERROR ((AE_INFO,
314                "(PRT[%X]) Need package of length 4, found length %d",
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 (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
330        {
331            ACPI_ERROR ((AE_INFO, "(PRT[%X].Address) Need Integer, found %s",
332                Index, AcpiUtGetObjectTypeName (ObjDesc)));
333            return_ACPI_STATUS (AE_BAD_DATA);
334        }
335
336        UserPrt->Address = ObjDesc->Integer.Value;
337
338        /* 2) Second subobject: Dereference the PRT.Pin */
339
340        ObjDesc = SubObjectList[1];
341        if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
342        {
343            ACPI_ERROR ((AE_INFO, "(PRT[%X].Pin) Need Integer, found %s",
344                Index, AcpiUtGetObjectTypeName (ObjDesc)));
345            return_ACPI_STATUS (AE_BAD_DATA);
346        }
347
348        UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;
349
350        /*
351         * If the BIOS has erroneously reversed the _PRT SourceName (index 2)
352         * and the SourceIndex (index 3), fix it. _PRT is important enough to
353         * workaround this BIOS error. This also provides compatibility with
354         * other ACPI implementations.
355         */
356        ObjDesc = SubObjectList[3];
357        if (!ObjDesc || (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
358        {
359            SubObjectList[3] = SubObjectList[2];
360            SubObjectList[2] = ObjDesc;
361
362            ACPI_WARNING ((AE_INFO,
363                "(PRT[%X].Source) SourceName and SourceIndex are reversed, fixed",
364                Index));
365        }
366
367        /*
368         * 3) Third subobject: Dereference the PRT.SourceName
369         * The name may be unresolved (slack mode), so allow a null object
370         */
371        ObjDesc = SubObjectList[2];
372        if (ObjDesc)
373        {
374            switch (ObjDesc->Common.Type)
375            {
376            case ACPI_TYPE_LOCAL_REFERENCE:
377
378                if (ObjDesc->Reference.Class != ACPI_REFCLASS_NAME)
379                {
380                    ACPI_ERROR ((AE_INFO,
381                        "(PRT[%X].Source) Need name, found Reference Class %X",
382                        Index, ObjDesc->Reference.Class));
383                    return_ACPI_STATUS (AE_BAD_DATA);
384                }
385
386                Node = ObjDesc->Reference.Node;
387
388                /* Use *remaining* length of the buffer as max for pathname */
389
390                PathBuffer.Length = OutputBuffer->Length -
391                                    (UINT32) ((UINT8 *) UserPrt->Source -
392                                    (UINT8 *) OutputBuffer->Pointer);
393                PathBuffer.Pointer = UserPrt->Source;
394
395                Status = AcpiNsHandleToPathname ((ACPI_HANDLE) Node, &PathBuffer);
396
397                /* +1 to include null terminator */
398
399                UserPrt->Length += (UINT32) ACPI_STRLEN (UserPrt->Source) + 1;
400                break;
401
402
403            case ACPI_TYPE_STRING:
404
405                ACPI_STRCPY (UserPrt->Source, ObjDesc->String.Pointer);
406
407                /*
408                 * Add to the Length field the length of the string
409                 * (add 1 for terminator)
410                 */
411                UserPrt->Length += ObjDesc->String.Length + 1;
412                break;
413
414
415            case ACPI_TYPE_INTEGER:
416                /*
417                 * If this is a number, then the Source Name is NULL, since the
418                 * entire buffer was zeroed out, we can leave this alone.
419                 *
420                 * Add to the Length field the length of the UINT32 NULL
421                 */
422                UserPrt->Length += sizeof (UINT32);
423                break;
424
425
426            default:
427
428               ACPI_ERROR ((AE_INFO,
429                   "(PRT[%X].Source) Need Ref/String/Integer, found %s",
430                   Index, AcpiUtGetObjectTypeName (ObjDesc)));
431               return_ACPI_STATUS (AE_BAD_DATA);
432            }
433        }
434
435        /* Now align the current length */
436
437        UserPrt->Length = (UINT32) ACPI_ROUND_UP_TO_64BIT (UserPrt->Length);
438
439        /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
440
441        ObjDesc = SubObjectList[3];
442        if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
443        {
444            ACPI_ERROR ((AE_INFO,
445                "(PRT[%X].SourceIndex) Need Integer, found %s",
446                Index, AcpiUtGetObjectTypeName (ObjDesc)));
447            return_ACPI_STATUS (AE_BAD_DATA);
448        }
449
450        UserPrt->SourceIndex = (UINT32) ObjDesc->Integer.Value;
451
452        /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
453
454        TopObjectList++;
455    }
456
457    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
458            OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
459    return_ACPI_STATUS (AE_OK);
460}
461
462
463/*******************************************************************************
464 *
465 * FUNCTION:    AcpiRsCreateAmlResources
466 *
467 * PARAMETERS:  LinkedListBuffer        - Pointer to the resource linked list
468 *              OutputBuffer            - Pointer to the user's buffer
469 *
470 * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
471 *              If the OutputBuffer is too small, the error will be
472 *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
473 *              to the size buffer needed.
474 *
475 * DESCRIPTION: Takes the linked list of device resources and
476 *              creates a bytestream to be used as input for the
477 *              _SRS control method.
478 *
479 ******************************************************************************/
480
481ACPI_STATUS
482AcpiRsCreateAmlResources (
483    ACPI_RESOURCE           *LinkedListBuffer,
484    ACPI_BUFFER             *OutputBuffer)
485{
486    ACPI_STATUS             Status;
487    ACPI_SIZE               AmlSizeNeeded = 0;
488
489
490    ACPI_FUNCTION_TRACE (RsCreateAmlResources);
491
492
493    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "LinkedListBuffer = %p\n",
494        LinkedListBuffer));
495
496    /*
497     * Params already validated, so we don't re-validate here
498     *
499     * Pass the LinkedListBuffer into a module that calculates
500     * the buffer size needed for the byte stream.
501     */
502    Status = AcpiRsGetAmlLength (LinkedListBuffer,
503                &AmlSizeNeeded);
504
505    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
506        (UINT32) AmlSizeNeeded, AcpiFormatException (Status)));
507    if (ACPI_FAILURE (Status))
508    {
509        return_ACPI_STATUS (Status);
510    }
511
512    /* Validate/Allocate/Clear caller buffer */
513
514    Status = AcpiUtInitializeBuffer (OutputBuffer, AmlSizeNeeded);
515    if (ACPI_FAILURE (Status))
516    {
517        return_ACPI_STATUS (Status);
518    }
519
520    /* Do the conversion */
521
522    Status = AcpiRsConvertResourcesToAml (LinkedListBuffer, AmlSizeNeeded,
523                    OutputBuffer->Pointer);
524    if (ACPI_FAILURE (Status))
525    {
526        return_ACPI_STATUS (Status);
527    }
528
529    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
530            OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
531    return_ACPI_STATUS (AE_OK);
532}
533
534