xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/resources/rscreate.c (revision 836a14832190d2718b879ac08fb82a7926b5dd8f)
1 /*******************************************************************************
2  *
3  * Module Name: rscreate - Create resource lists/tables
4  *
5  ******************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115 
116 #include "acpi.h"
117 #include "accommon.h"
118 #include "acresrc.h"
119 #include "acnamesp.h"
120 
121 #define _COMPONENT          ACPI_RESOURCES
122         ACPI_MODULE_NAME    ("rscreate")
123 
124 
125 /*******************************************************************************
126  *
127  * FUNCTION:    AcpiBufferToResource
128  *
129  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
130  *              AmlBufferLength     - Length of the AmlBuffer
131  *              ResourcePtr         - Where the converted resource is returned
132  *
133  * RETURN:      Status
134  *
135  * DESCRIPTION: Convert a raw AML buffer to a resource list
136  *
137  ******************************************************************************/
138 
139 ACPI_STATUS
140 AcpiBufferToResource (
141     UINT8                   *AmlBuffer,
142     UINT16                  AmlBufferLength,
143     ACPI_RESOURCE           **ResourcePtr)
144 {
145     ACPI_STATUS             Status;
146     ACPI_SIZE               ListSizeNeeded;
147     void                    *Resource;
148     void                    *CurrentResourcePtr;
149 
150 
151     ACPI_FUNCTION_TRACE (AcpiBufferToResource);
152 
153 
154     /*
155      * Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag
156      * is not required here.
157      */
158 
159     /* Get the required length for the converted resource */
160 
161     Status = AcpiRsGetListLength (
162         AmlBuffer, AmlBufferLength, &ListSizeNeeded);
163     if (Status == AE_AML_NO_RESOURCE_END_TAG)
164     {
165         Status = AE_OK;
166     }
167     if (ACPI_FAILURE (Status))
168     {
169         return_ACPI_STATUS (Status);
170     }
171 
172     /* Allocate a buffer for the converted resource */
173 
174     Resource = ACPI_ALLOCATE_ZEROED (ListSizeNeeded);
175     CurrentResourcePtr = Resource;
176     if (!Resource)
177     {
178         return_ACPI_STATUS (AE_NO_MEMORY);
179     }
180 
181     /* Perform the AML-to-Resource conversion */
182 
183     Status = AcpiUtWalkAmlResources (NULL, AmlBuffer, AmlBufferLength,
184         AcpiRsConvertAmlToResources, &CurrentResourcePtr);
185     if (Status == AE_AML_NO_RESOURCE_END_TAG)
186     {
187         Status = AE_OK;
188     }
189     if (ACPI_FAILURE (Status))
190     {
191         ACPI_FREE (Resource);
192     }
193     else
194     {
195         *ResourcePtr = Resource;
196     }
197 
198     return_ACPI_STATUS (Status);
199 }
200 
201 ACPI_EXPORT_SYMBOL (AcpiBufferToResource)
202 
203 
204 /*******************************************************************************
205  *
206  * FUNCTION:    AcpiRsCreateResourceList
207  *
208  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
209  *              OutputBuffer        - Pointer to the user's buffer
210  *
211  * RETURN:      Status: AE_OK if okay, else a valid ACPI_STATUS code
212  *              If OutputBuffer is not large enough, OutputBufferLength
213  *              indicates how large OutputBuffer should be, else it
214  *              indicates how may UINT8 elements of OutputBuffer are valid.
215  *
216  * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
217  *              execution and parses the stream to create a linked list
218  *              of device resources.
219  *
220  ******************************************************************************/
221 
222 ACPI_STATUS
223 AcpiRsCreateResourceList (
224     ACPI_OPERAND_OBJECT     *AmlBuffer,
225     ACPI_BUFFER             *OutputBuffer)
226 {
227 
228     ACPI_STATUS             Status;
229     UINT8                   *AmlStart;
230     ACPI_SIZE               ListSizeNeeded = 0;
231     UINT32                  AmlBufferLength;
232     void                    *Resource;
233 
234 
235     ACPI_FUNCTION_TRACE (RsCreateResourceList);
236 
237 
238     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlBuffer = %p\n",
239         AmlBuffer));
240 
241     /* Params already validated, so we don't re-validate here */
242 
243     AmlBufferLength = AmlBuffer->Buffer.Length;
244     AmlStart = AmlBuffer->Buffer.Pointer;
245 
246     /*
247      * Pass the AmlBuffer into a module that can calculate
248      * the buffer size needed for the linked list
249      */
250     Status = AcpiRsGetListLength (AmlStart, AmlBufferLength,
251                 &ListSizeNeeded);
252 
253     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
254         Status, (UINT32) ListSizeNeeded));
255     if (ACPI_FAILURE (Status))
256     {
257         return_ACPI_STATUS (Status);
258     }
259 
260     /* Validate/Allocate/Clear caller buffer */
261 
262     Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);
263     if (ACPI_FAILURE (Status))
264     {
265         return_ACPI_STATUS (Status);
266     }
267 
268     /* Do the conversion */
269 
270     Resource = OutputBuffer->Pointer;
271     Status = AcpiUtWalkAmlResources (NULL, AmlStart, AmlBufferLength,
272         AcpiRsConvertAmlToResources, &Resource);
273     if (ACPI_FAILURE (Status))
274     {
275         return_ACPI_STATUS (Status);
276     }
277 
278     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
279         OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
280     return_ACPI_STATUS (AE_OK);
281 }
282 
283 
284 /*******************************************************************************
285  *
286  * FUNCTION:    AcpiRsCreatePciRoutingTable
287  *
288  * PARAMETERS:  PackageObject           - Pointer to a package containing one
289  *                                        of more ACPI_OPERAND_OBJECTs
290  *              OutputBuffer            - Pointer to the user's buffer
291  *
292  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
293  *              If the OutputBuffer is too small, the error will be
294  *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
295  *              to the size buffer needed.
296  *
297  * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a
298  *              linked list of PCI interrupt descriptions
299  *
300  * NOTE: It is the caller's responsibility to ensure that the start of the
301  * output buffer is aligned properly (if necessary).
302  *
303  ******************************************************************************/
304 
305 ACPI_STATUS
306 AcpiRsCreatePciRoutingTable (
307     ACPI_OPERAND_OBJECT     *PackageObject,
308     ACPI_BUFFER             *OutputBuffer)
309 {
310     UINT8                   *Buffer;
311     ACPI_OPERAND_OBJECT     **TopObjectList;
312     ACPI_OPERAND_OBJECT     **SubObjectList;
313     ACPI_OPERAND_OBJECT     *ObjDesc;
314     ACPI_SIZE               BufferSizeNeeded = 0;
315     UINT32                  NumberOfElements;
316     UINT32                  Index;
317     ACPI_PCI_ROUTING_TABLE  *UserPrt;
318     ACPI_NAMESPACE_NODE     *Node;
319     ACPI_STATUS             Status;
320     ACPI_BUFFER             PathBuffer;
321 
322 
323     ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable);
324 
325 
326     /* Params already validated, so we don't re-validate here */
327 
328     /* Get the required buffer length */
329 
330     Status = AcpiRsGetPciRoutingTableLength (
331         PackageObject,&BufferSizeNeeded);
332     if (ACPI_FAILURE (Status))
333     {
334         return_ACPI_STATUS (Status);
335     }
336 
337     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
338         (UINT32) BufferSizeNeeded));
339 
340     /* Validate/Allocate/Clear caller buffer */
341 
342     Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);
343     if (ACPI_FAILURE (Status))
344     {
345         return_ACPI_STATUS (Status);
346     }
347 
348     /*
349      * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
350      * package that in turn contains an UINT64 Address, a UINT8 Pin,
351      * a Name, and a UINT8 SourceIndex.
352      */
353     TopObjectList = PackageObject->Package.Elements;
354     NumberOfElements = PackageObject->Package.Count;
355     Buffer = OutputBuffer->Pointer;
356     UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
357 
358     for (Index = 0; Index < NumberOfElements; Index++)
359     {
360         /*
361          * Point UserPrt past this current structure
362          *
363          * NOTE: On the first iteration, UserPrt->Length will
364          * be zero because we cleared the return buffer earlier
365          */
366         Buffer += UserPrt->Length;
367         UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
368 
369         /*
370          * Fill in the Length field with the information we have at this
371          * point. The minus four is to subtract the size of the UINT8
372          * Source[4] member because it is added below.
373          */
374         UserPrt->Length = (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
375 
376         /* Each subpackage must be of length 4 */
377 
378         if ((*TopObjectList)->Package.Count != 4)
379         {
380             ACPI_ERROR ((AE_INFO,
381                 "(PRT[%u]) Need package of length 4, found length %u",
382                 Index, (*TopObjectList)->Package.Count));
383             return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);
384         }
385 
386         /*
387          * Dereference the subpackage.
388          * The SubObjectList will now point to an array of the four IRQ
389          * elements: [Address, Pin, Source, SourceIndex]
390          */
391         SubObjectList = (*TopObjectList)->Package.Elements;
392 
393         /* 1) First subobject: Dereference the PRT.Address */
394 
395         ObjDesc = SubObjectList[0];
396         if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
397         {
398             ACPI_ERROR ((AE_INFO,
399                 "(PRT[%u].Address) Need Integer, found %s",
400                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
401             return_ACPI_STATUS (AE_BAD_DATA);
402         }
403 
404         UserPrt->Address = ObjDesc->Integer.Value;
405 
406         /* 2) Second subobject: Dereference the PRT.Pin */
407 
408         ObjDesc = SubObjectList[1];
409         if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
410         {
411             ACPI_ERROR ((AE_INFO, "(PRT[%u].Pin) Need Integer, found %s",
412                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
413             return_ACPI_STATUS (AE_BAD_DATA);
414         }
415 
416         UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;
417 
418         /*
419          * 3) Third subobject: Dereference the PRT.SourceName
420          * The name may be unresolved (slack mode), so allow a null object
421          */
422         ObjDesc = SubObjectList[2];
423         if (ObjDesc)
424         {
425             switch (ObjDesc->Common.Type)
426             {
427             case ACPI_TYPE_LOCAL_REFERENCE:
428 
429                 if (ObjDesc->Reference.Class != ACPI_REFCLASS_NAME)
430                 {
431                     ACPI_ERROR ((AE_INFO,
432                         "(PRT[%u].Source) Need name, found Reference Class 0x%X",
433                         Index, ObjDesc->Reference.Class));
434                     return_ACPI_STATUS (AE_BAD_DATA);
435                 }
436 
437                 Node = ObjDesc->Reference.Node;
438 
439                 /* Use *remaining* length of the buffer as max for pathname */
440 
441                 PathBuffer.Length = OutputBuffer->Length -
442                     (UINT32) ((UINT8 *) UserPrt->Source -
443                     (UINT8 *) OutputBuffer->Pointer);
444                 PathBuffer.Pointer = UserPrt->Source;
445 
446                 Status = AcpiNsHandleToPathname (
447                     (ACPI_HANDLE) Node, &PathBuffer, FALSE);
448 
449                 /* +1 to include null terminator */
450 
451                 UserPrt->Length += (UINT32) strlen (UserPrt->Source) + 1;
452                 break;
453 
454             case ACPI_TYPE_STRING:
455 
456                 strcpy (UserPrt->Source, ObjDesc->String.Pointer);
457 
458                 /*
459                  * Add to the Length field the length of the string
460                  * (add 1 for terminator)
461                  */
462                 UserPrt->Length += ObjDesc->String.Length + 1;
463                 break;
464 
465             case ACPI_TYPE_INTEGER:
466                 /*
467                  * If this is a number, then the Source Name is NULL, since
468                  * the entire buffer was zeroed out, we can leave this alone.
469                  *
470                  * Add to the Length field the length of the UINT32 NULL
471                  */
472                 UserPrt->Length += sizeof (UINT32);
473                 break;
474 
475             default:
476 
477                ACPI_ERROR ((AE_INFO,
478                    "(PRT[%u].Source) Need Ref/String/Integer, found %s",
479                    Index, AcpiUtGetObjectTypeName (ObjDesc)));
480                return_ACPI_STATUS (AE_BAD_DATA);
481             }
482         }
483 
484         /* Now align the current length */
485 
486         UserPrt->Length = (UINT32) ACPI_ROUND_UP_TO_64BIT (UserPrt->Length);
487 
488         /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
489 
490         ObjDesc = SubObjectList[3];
491         if (!ObjDesc || ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
492         {
493             ACPI_ERROR ((AE_INFO,
494                 "(PRT[%u].SourceIndex) Need Integer, found %s",
495                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
496             return_ACPI_STATUS (AE_BAD_DATA);
497         }
498 
499         UserPrt->SourceIndex = (UINT32) ObjDesc->Integer.Value;
500 
501         /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
502 
503         TopObjectList++;
504     }
505 
506     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
507         OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
508     return_ACPI_STATUS (AE_OK);
509 }
510 
511 
512 /*******************************************************************************
513  *
514  * FUNCTION:    AcpiRsCreateAmlResources
515  *
516  * PARAMETERS:  ResourceList            - Pointer to the resource list buffer
517  *              OutputBuffer            - Where the AML buffer is returned
518  *
519  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
520  *              If the OutputBuffer is too small, the error will be
521  *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
522  *              to the size buffer needed.
523  *
524  * DESCRIPTION: Converts a list of device resources to an AML bytestream
525  *              to be used as input for the _SRS control method.
526  *
527  ******************************************************************************/
528 
529 ACPI_STATUS
530 AcpiRsCreateAmlResources (
531     ACPI_BUFFER             *ResourceList,
532     ACPI_BUFFER             *OutputBuffer)
533 {
534     ACPI_STATUS             Status;
535     ACPI_SIZE               AmlSizeNeeded = 0;
536 
537 
538     ACPI_FUNCTION_TRACE (RsCreateAmlResources);
539 
540 
541     /* Params already validated, no need to re-validate here */
542 
543     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "ResourceList Buffer = %p\n",
544         ResourceList->Pointer));
545 
546     /* Get the buffer size needed for the AML byte stream */
547 
548     Status = AcpiRsGetAmlLength (
549         ResourceList->Pointer, ResourceList->Length, &AmlSizeNeeded);
550 
551     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
552         (UINT32) AmlSizeNeeded, AcpiFormatException (Status)));
553     if (ACPI_FAILURE (Status))
554     {
555         return_ACPI_STATUS (Status);
556     }
557 
558     /* Validate/Allocate/Clear caller buffer */
559 
560     Status = AcpiUtInitializeBuffer (OutputBuffer, AmlSizeNeeded);
561     if (ACPI_FAILURE (Status))
562     {
563         return_ACPI_STATUS (Status);
564     }
565 
566     /* Do the conversion */
567 
568     Status = AcpiRsConvertResourcesToAml (ResourceList->Pointer,
569         AmlSizeNeeded, OutputBuffer->Pointer);
570     if (ACPI_FAILURE (Status))
571     {
572         return_ACPI_STATUS (Status);
573     }
574 
575     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
576         OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
577     return_ACPI_STATUS (AE_OK);
578 }
579