rscreate.c revision 302408
159191Skris/*******************************************************************************
259191Skris *
359191Skris * Module Name: rscreate - Create resource lists/tables
459191Skris *
559191Skris ******************************************************************************/
659191Skris
759191Skris/*
859191Skris * Copyright (C) 2000 - 2016, Intel Corp.
959191Skris * All rights reserved.
1059191Skris *
1159191Skris * Redistribution and use in source and binary forms, with or without
1259191Skris * modification, are permitted provided that the following conditions
1359191Skris * are met:
1459191Skris * 1. Redistributions of source code must retain the above copyright
1559191Skris *    notice, this list of conditions, and the following disclaimer,
1659191Skris *    without modification.
1759191Skris * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1859191Skris *    substantially similar to the "NO WARRANTY" disclaimer below
1959191Skris *    ("Disclaimer") and any redistribution must be conditioned upon
2059191Skris *    including a substantially similar Disclaimer requirement for further
2159191Skris *    binary redistribution.
2259191Skris * 3. Neither the names of the above-listed copyright holders nor the names
2359191Skris *    of any contributors may be used to endorse or promote products derived
2459191Skris *    from this software without specific prior written permission.
2559191Skris *
2659191Skris * Alternatively, this software may be distributed under the terms of the
2759191Skris * GNU General Public License ("GPL") version 2 as published by the Free
2859191Skris * Software Foundation.
2959191Skris *
3059191Skris * NO WARRANTY
3159191Skris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3259191Skris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3359191Skris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3459191Skris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3559191Skris * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3659191Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3759191Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3859191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3959191Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4059191Skris * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4159191Skris * POSSIBILITY OF SUCH DAMAGES.
4259191Skris */
4359191Skris
4459191Skris#include <contrib/dev/acpica/include/acpi.h>
4559191Skris#include <contrib/dev/acpica/include/accommon.h>
4659191Skris#include <contrib/dev/acpica/include/acresrc.h>
4759191Skris#include <contrib/dev/acpica/include/acnamesp.h>
4859191Skris
4959191Skris#define _COMPONENT          ACPI_RESOURCES
5059191Skris        ACPI_MODULE_NAME    ("rscreate")
5159191Skris
5259191Skris
5359191Skris/*******************************************************************************
5459191Skris *
5559191Skris * FUNCTION:    AcpiBufferToResource
5659191Skris *
5759191Skris * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
5859191Skris *              AmlBufferLength     - Length of the AmlBuffer
5959191Skris *              ResourcePtr         - Where the converted resource is returned
6059191Skris *
6159191Skris * RETURN:      Status
6259191Skris *
6359191Skris * DESCRIPTION: Convert a raw AML buffer to a resource list
6459191Skris *
6559191Skris ******************************************************************************/
6659191Skris
6759191SkrisACPI_STATUS
6859191SkrisAcpiBufferToResource (
6959191Skris    UINT8                   *AmlBuffer,
7059191Skris    UINT16                  AmlBufferLength,
7159191Skris    ACPI_RESOURCE           **ResourcePtr)
7259191Skris{
7359191Skris    ACPI_STATUS             Status;
7459191Skris    ACPI_SIZE               ListSizeNeeded;
7559191Skris    void                    *Resource;
7659191Skris    void                    *CurrentResourcePtr;
7759191Skris
7859191Skris
7959191Skris    ACPI_FUNCTION_TRACE (AcpiBufferToResource);
8059191Skris
8159191Skris
8259191Skris    /*
8359191Skris     * Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag
8459191Skris     * is not required here.
8559191Skris     */
8659191Skris
8759191Skris    /* Get the required length for the converted resource */
8859191Skris
8959191Skris    Status = AcpiRsGetListLength (
9059191Skris        AmlBuffer, AmlBufferLength, &ListSizeNeeded);
9159191Skris    if (Status == AE_AML_NO_RESOURCE_END_TAG)
9259191Skris    {
9359191Skris        Status = AE_OK;
9459191Skris    }
9559191Skris    if (ACPI_FAILURE (Status))
9659191Skris    {
9759191Skris        return_ACPI_STATUS (Status);
9859191Skris    }
9959191Skris
10059191Skris    /* Allocate a buffer for the converted resource */
10159191Skris
10259191Skris    Resource = ACPI_ALLOCATE_ZEROED (ListSizeNeeded);
10359191Skris    CurrentResourcePtr = Resource;
10459191Skris    if (!Resource)
10559191Skris    {
10659191Skris        return_ACPI_STATUS (AE_NO_MEMORY);
10759191Skris    }
10859191Skris
10959191Skris    /* Perform the AML-to-Resource conversion */
11059191Skris
11159191Skris    Status = AcpiUtWalkAmlResources (NULL, AmlBuffer, AmlBufferLength,
11259191Skris        AcpiRsConvertAmlToResources, &CurrentResourcePtr);
11359191Skris    if (Status == AE_AML_NO_RESOURCE_END_TAG)
11459191Skris    {
11559191Skris        Status = AE_OK;
11659191Skris    }
11759191Skris    if (ACPI_FAILURE (Status))
11859191Skris    {
11959191Skris        ACPI_FREE (Resource);
12059191Skris    }
12159191Skris    else
12259191Skris    {
12359191Skris        *ResourcePtr = Resource;
12459191Skris    }
12559191Skris
12659191Skris    return_ACPI_STATUS (Status);
12759191Skris}
12859191Skris
12959191SkrisACPI_EXPORT_SYMBOL (AcpiBufferToResource)
130
131
132/*******************************************************************************
133 *
134 * FUNCTION:    AcpiRsCreateResourceList
135 *
136 * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
137 *              OutputBuffer        - Pointer to the user's buffer
138 *
139 * RETURN:      Status: AE_OK if okay, else a valid ACPI_STATUS code
140 *              If OutputBuffer is not large enough, OutputBufferLength
141 *              indicates how large OutputBuffer should be, else it
142 *              indicates how may UINT8 elements of OutputBuffer are valid.
143 *
144 * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
145 *              execution and parses the stream to create a linked list
146 *              of device resources.
147 *
148 ******************************************************************************/
149
150ACPI_STATUS
151AcpiRsCreateResourceList (
152    ACPI_OPERAND_OBJECT     *AmlBuffer,
153    ACPI_BUFFER             *OutputBuffer)
154{
155
156    ACPI_STATUS             Status;
157    UINT8                   *AmlStart;
158    ACPI_SIZE               ListSizeNeeded = 0;
159    UINT32                  AmlBufferLength;
160    void                    *Resource;
161
162
163    ACPI_FUNCTION_TRACE (RsCreateResourceList);
164
165
166    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlBuffer = %p\n",
167        AmlBuffer));
168
169    /* Params already validated, so we don't re-validate here */
170
171    AmlBufferLength = AmlBuffer->Buffer.Length;
172    AmlStart = AmlBuffer->Buffer.Pointer;
173
174    /*
175     * Pass the AmlBuffer into a module that can calculate
176     * the buffer size needed for the linked list
177     */
178    Status = AcpiRsGetListLength (AmlStart, AmlBufferLength,
179                &ListSizeNeeded);
180
181    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
182        Status, (UINT32) ListSizeNeeded));
183    if (ACPI_FAILURE (Status))
184    {
185        return_ACPI_STATUS (Status);
186    }
187
188    /* Validate/Allocate/Clear caller buffer */
189
190    Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);
191    if (ACPI_FAILURE (Status))
192    {
193        return_ACPI_STATUS (Status);
194    }
195
196    /* Do the conversion */
197
198    Resource = OutputBuffer->Pointer;
199    Status = AcpiUtWalkAmlResources (NULL, AmlStart, AmlBufferLength,
200        AcpiRsConvertAmlToResources, &Resource);
201    if (ACPI_FAILURE (Status))
202    {
203        return_ACPI_STATUS (Status);
204    }
205
206    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
207        OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
208    return_ACPI_STATUS (AE_OK);
209}
210
211
212/*******************************************************************************
213 *
214 * FUNCTION:    AcpiRsCreatePciRoutingTable
215 *
216 * PARAMETERS:  PackageObject           - Pointer to a package containing one
217 *                                        of more ACPI_OPERAND_OBJECTs
218 *              OutputBuffer            - Pointer to the user's buffer
219 *
220 * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
221 *              If the OutputBuffer is too small, the error will be
222 *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
223 *              to the size buffer needed.
224 *
225 * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a
226 *              linked list of PCI interrupt descriptions
227 *
228 * NOTE: It is the caller's responsibility to ensure that the start of the
229 * output buffer is aligned properly (if necessary).
230 *
231 ******************************************************************************/
232
233ACPI_STATUS
234AcpiRsCreatePciRoutingTable (
235    ACPI_OPERAND_OBJECT     *PackageObject,
236    ACPI_BUFFER             *OutputBuffer)
237{
238    UINT8                   *Buffer;
239    ACPI_OPERAND_OBJECT     **TopObjectList;
240    ACPI_OPERAND_OBJECT     **SubObjectList;
241    ACPI_OPERAND_OBJECT     *ObjDesc;
242    ACPI_SIZE               BufferSizeNeeded = 0;
243    UINT32                  NumberOfElements;
244    UINT32                  Index;
245    ACPI_PCI_ROUTING_TABLE  *UserPrt;
246    ACPI_NAMESPACE_NODE     *Node;
247    ACPI_STATUS             Status;
248    ACPI_BUFFER             PathBuffer;
249
250
251    ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable);
252
253
254    /* Params already validated, so we don't re-validate here */
255
256    /* Get the required buffer length */
257
258    Status = AcpiRsGetPciRoutingTableLength (
259        PackageObject,&BufferSizeNeeded);
260    if (ACPI_FAILURE (Status))
261    {
262        return_ACPI_STATUS (Status);
263    }
264
265    ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
266        (UINT32) BufferSizeNeeded));
267
268    /* Validate/Allocate/Clear caller buffer */
269
270    Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);
271    if (ACPI_FAILURE (Status))
272    {
273        return_ACPI_STATUS (Status);
274    }
275
276    /*
277     * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
278     * package that in turn contains an UINT64 Address, a UINT8 Pin,
279     * a Name, and a UINT8 SourceIndex.
280     */
281    TopObjectList = PackageObject->Package.Elements;
282    NumberOfElements = PackageObject->Package.Count;
283    Buffer = OutputBuffer->Pointer;
284    UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
285
286    for (Index = 0; Index < NumberOfElements; Index++)
287    {
288        /*
289         * Point UserPrt past this current structure
290         *
291         * NOTE: On the first iteration, UserPrt->Length will
292         * be zero because we cleared the return buffer earlier
293         */
294        Buffer += UserPrt->Length;
295        UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
296
297        /*
298         * Fill in the Length field with the information we have at this
299         * point. The minus four is to subtract the size of the UINT8
300         * Source[4] member because it is added below.
301         */
302        UserPrt->Length = (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
303
304        /* Each subpackage must be of length 4 */
305
306        if ((*TopObjectList)->Package.Count != 4)
307        {
308            ACPI_ERROR ((AE_INFO,
309                "(PRT[%u]) Need package of length 4, found length %u",
310                Index, (*TopObjectList)->Package.Count));
311            return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);
312        }
313
314        /*
315         * Dereference the subpackage.
316         * The SubObjectList will now point to an array of the four IRQ
317         * elements: [Address, Pin, Source, SourceIndex]
318         */
319        SubObjectList = (*TopObjectList)->Package.Elements;
320
321        /* 1) First subobject: Dereference the PRT.Address */
322
323        ObjDesc = SubObjectList[0];
324        if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
325        {
326            ACPI_ERROR ((AE_INFO,
327                "(PRT[%u].Address) Need Integer, found %s",
328                Index, AcpiUtGetObjectTypeName (ObjDesc)));
329            return_ACPI_STATUS (AE_BAD_DATA);
330        }
331
332        UserPrt->Address = ObjDesc->Integer.Value;
333
334        /* 2) Second subobject: Dereference the PRT.Pin */
335
336        ObjDesc = SubObjectList[1];
337        if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
338        {
339            ACPI_ERROR ((AE_INFO, "(PRT[%u].Pin) Need Integer, found %s",
340                Index, AcpiUtGetObjectTypeName (ObjDesc)));
341            return_ACPI_STATUS (AE_BAD_DATA);
342        }
343
344        UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;
345
346        /*
347         * 3) Third subobject: Dereference the PRT.SourceName
348         * The name may be unresolved (slack mode), so allow a null object
349         */
350        ObjDesc = SubObjectList[2];
351        if (ObjDesc)
352        {
353            switch (ObjDesc->Common.Type)
354            {
355            case ACPI_TYPE_LOCAL_REFERENCE:
356
357                if (ObjDesc->Reference.Class != ACPI_REFCLASS_NAME)
358                {
359                    ACPI_ERROR ((AE_INFO,
360                        "(PRT[%u].Source) Need name, found Reference Class 0x%X",
361                        Index, ObjDesc->Reference.Class));
362                    return_ACPI_STATUS (AE_BAD_DATA);
363                }
364
365                Node = ObjDesc->Reference.Node;
366
367                /* Use *remaining* length of the buffer as max for pathname */
368
369                PathBuffer.Length = OutputBuffer->Length -
370                    (UINT32) ((UINT8 *) UserPrt->Source -
371                    (UINT8 *) OutputBuffer->Pointer);
372                PathBuffer.Pointer = UserPrt->Source;
373
374                Status = AcpiNsHandleToPathname (
375                    (ACPI_HANDLE) Node, &PathBuffer, FALSE);
376
377                /* +1 to include null terminator */
378
379                UserPrt->Length += (UINT32) strlen (UserPrt->Source) + 1;
380                break;
381
382            case ACPI_TYPE_STRING:
383
384                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
396                 * the 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
457ACPI_STATUS
458AcpiRsCreateAmlResources (
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 (
477        ResourceList->Pointer, 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}
507