xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/resources/rscreate.c (revision e6eaad8615c4734498b9b800847d18bbe62782fa)
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 - 2013, 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 #define __RSCREATE_C__
117 
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "acresrc.h"
121 #include "acnamesp.h"
122 
123 #define _COMPONENT          ACPI_RESOURCES
124         ACPI_MODULE_NAME    ("rscreate")
125 
126 
127 /*******************************************************************************
128  *
129  * FUNCTION:    AcpiBufferToResource
130  *
131  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
132  *              AmlBufferLength     - Length of the AmlBuffer
133  *              ResourcePtr         - Where the converted resource is returned
134  *
135  * RETURN:      Status
136  *
137  * DESCRIPTION: Convert a raw AML buffer to a resource list
138  *
139  ******************************************************************************/
140 
141 ACPI_STATUS
142 AcpiBufferToResource (
143     UINT8                   *AmlBuffer,
144     UINT16                  AmlBufferLength,
145     ACPI_RESOURCE           **ResourcePtr)
146 {
147     ACPI_STATUS             Status;
148     ACPI_SIZE               ListSizeNeeded;
149     void                    *Resource;
150     void                    *CurrentResourcePtr;
151 
152     /*
153      * Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag
154      * is not required here.
155      */
156 
157     /* Get the required length for the converted resource */
158 
159     Status = AcpiRsGetListLength (AmlBuffer, AmlBufferLength,
160                 &ListSizeNeeded);
161     if (Status == AE_AML_NO_RESOURCE_END_TAG)
162     {
163         Status = AE_OK;
164     }
165     if (ACPI_FAILURE (Status))
166     {
167         return (Status);
168     }
169 
170     /* Allocate a buffer for the converted resource */
171 
172     Resource = ACPI_ALLOCATE_ZEROED (ListSizeNeeded);
173     CurrentResourcePtr = Resource;
174     if (!Resource)
175     {
176         return (AE_NO_MEMORY);
177     }
178 
179     /* Perform the AML-to-Resource conversion */
180 
181     Status = AcpiUtWalkAmlResources (NULL, AmlBuffer, AmlBufferLength,
182                 AcpiRsConvertAmlToResources, &CurrentResourcePtr);
183     if (Status == AE_AML_NO_RESOURCE_END_TAG)
184     {
185         Status = AE_OK;
186     }
187     if (ACPI_FAILURE (Status))
188     {
189         ACPI_FREE (Resource);
190     }
191     else
192     {
193         *ResourcePtr = Resource;
194     }
195 
196     return (Status);
197 }
198 
199 
200 /*******************************************************************************
201  *
202  * FUNCTION:    AcpiRsCreateResourceList
203  *
204  * PARAMETERS:  AmlBuffer           - Pointer to the resource byte stream
205  *              OutputBuffer        - Pointer to the user's buffer
206  *
207  * RETURN:      Status: AE_OK if okay, else a valid ACPI_STATUS code
208  *              If OutputBuffer is not large enough, OutputBufferLength
209  *              indicates how large OutputBuffer should be, else it
210  *              indicates how may UINT8 elements of OutputBuffer are valid.
211  *
212  * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
213  *              execution and parses the stream to create a linked list
214  *              of device resources.
215  *
216  ******************************************************************************/
217 
218 ACPI_STATUS
219 AcpiRsCreateResourceList (
220     ACPI_OPERAND_OBJECT     *AmlBuffer,
221     ACPI_BUFFER             *OutputBuffer)
222 {
223 
224     ACPI_STATUS             Status;
225     UINT8                   *AmlStart;
226     ACPI_SIZE               ListSizeNeeded = 0;
227     UINT32                  AmlBufferLength;
228     void                    *Resource;
229 
230 
231     ACPI_FUNCTION_TRACE (RsCreateResourceList);
232 
233 
234     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlBuffer = %p\n",
235         AmlBuffer));
236 
237     /* Params already validated, so we don't re-validate here */
238 
239     AmlBufferLength = AmlBuffer->Buffer.Length;
240     AmlStart = AmlBuffer->Buffer.Pointer;
241 
242     /*
243      * Pass the AmlBuffer into a module that can calculate
244      * the buffer size needed for the linked list
245      */
246     Status = AcpiRsGetListLength (AmlStart, AmlBufferLength,
247                 &ListSizeNeeded);
248 
249     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
250         Status, (UINT32) ListSizeNeeded));
251     if (ACPI_FAILURE (Status))
252     {
253         return_ACPI_STATUS (Status);
254     }
255 
256     /* Validate/Allocate/Clear caller buffer */
257 
258     Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);
259     if (ACPI_FAILURE (Status))
260     {
261         return_ACPI_STATUS (Status);
262     }
263 
264     /* Do the conversion */
265 
266     Resource = OutputBuffer->Pointer;
267     Status = AcpiUtWalkAmlResources (NULL, AmlStart, AmlBufferLength,
268                 AcpiRsConvertAmlToResources, &Resource);
269     if (ACPI_FAILURE (Status))
270     {
271         return_ACPI_STATUS (Status);
272     }
273 
274     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
275             OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
276     return_ACPI_STATUS (AE_OK);
277 }
278 
279 
280 /*******************************************************************************
281  *
282  * FUNCTION:    AcpiRsCreatePciRoutingTable
283  *
284  * PARAMETERS:  PackageObject           - Pointer to a package containing one
285  *                                        of more ACPI_OPERAND_OBJECTs
286  *              OutputBuffer            - Pointer to the user's buffer
287  *
288  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
289  *              If the OutputBuffer is too small, the error will be
290  *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
291  *              to the size buffer needed.
292  *
293  * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a
294  *              linked list of PCI interrupt descriptions
295  *
296  * NOTE: It is the caller's responsibility to ensure that the start of the
297  * output buffer is aligned properly (if necessary).
298  *
299  ******************************************************************************/
300 
301 ACPI_STATUS
302 AcpiRsCreatePciRoutingTable (
303     ACPI_OPERAND_OBJECT     *PackageObject,
304     ACPI_BUFFER             *OutputBuffer)
305 {
306     UINT8                   *Buffer;
307     ACPI_OPERAND_OBJECT     **TopObjectList;
308     ACPI_OPERAND_OBJECT     **SubObjectList;
309     ACPI_OPERAND_OBJECT     *ObjDesc;
310     ACPI_SIZE               BufferSizeNeeded = 0;
311     UINT32                  NumberOfElements;
312     UINT32                  Index;
313     ACPI_PCI_ROUTING_TABLE  *UserPrt;
314     ACPI_NAMESPACE_NODE     *Node;
315     ACPI_STATUS             Status;
316     ACPI_BUFFER             PathBuffer;
317 
318 
319     ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable);
320 
321 
322     /* Params already validated, so we don't re-validate here */
323 
324     /* Get the required buffer length */
325 
326     Status = AcpiRsGetPciRoutingTableLength (PackageObject,
327                 &BufferSizeNeeded);
328     if (ACPI_FAILURE (Status))
329     {
330         return_ACPI_STATUS (Status);
331     }
332 
333     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
334         (UINT32) BufferSizeNeeded));
335 
336     /* Validate/Allocate/Clear caller buffer */
337 
338     Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);
339     if (ACPI_FAILURE (Status))
340     {
341         return_ACPI_STATUS (Status);
342     }
343 
344     /*
345      * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
346      * package that in turn contains an UINT64 Address, a UINT8 Pin,
347      * a Name, and a UINT8 SourceIndex.
348      */
349     TopObjectList    = PackageObject->Package.Elements;
350     NumberOfElements = PackageObject->Package.Count;
351     Buffer           = OutputBuffer->Pointer;
352     UserPrt          = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
353 
354     for (Index = 0; Index < NumberOfElements; Index++)
355     {
356         /*
357          * Point UserPrt past this current structure
358          *
359          * NOTE: On the first iteration, UserPrt->Length will
360          * be zero because we cleared the return buffer earlier
361          */
362         Buffer += UserPrt->Length;
363         UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
364 
365         /*
366          * Fill in the Length field with the information we have at this point.
367          * The minus four is to subtract the size of the UINT8 Source[4] member
368          * because it is added below.
369          */
370         UserPrt->Length = (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
371 
372         /* Each sub-package must be of length 4 */
373 
374         if ((*TopObjectList)->Package.Count != 4)
375         {
376             ACPI_ERROR ((AE_INFO,
377                 "(PRT[%u]) Need package of length 4, found length %u",
378                 Index, (*TopObjectList)->Package.Count));
379             return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);
380         }
381 
382         /*
383          * Dereference the sub-package.
384          * The SubObjectList will now point to an array of the four IRQ
385          * elements: [Address, Pin, Source, SourceIndex]
386          */
387         SubObjectList = (*TopObjectList)->Package.Elements;
388 
389         /* 1) First subobject: Dereference the PRT.Address */
390 
391         ObjDesc = SubObjectList[0];
392         if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
393         {
394             ACPI_ERROR ((AE_INFO, "(PRT[%u].Address) Need Integer, found %s",
395                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
396             return_ACPI_STATUS (AE_BAD_DATA);
397         }
398 
399         UserPrt->Address = ObjDesc->Integer.Value;
400 
401         /* 2) Second subobject: Dereference the PRT.Pin */
402 
403         ObjDesc = SubObjectList[1];
404         if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
405         {
406             ACPI_ERROR ((AE_INFO, "(PRT[%u].Pin) Need Integer, found %s",
407                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
408             return_ACPI_STATUS (AE_BAD_DATA);
409         }
410 
411         UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;
412 
413         /*
414          * 3) Third subobject: Dereference the PRT.SourceName
415          * The name may be unresolved (slack mode), so allow a null object
416          */
417         ObjDesc = SubObjectList[2];
418         if (ObjDesc)
419         {
420             switch (ObjDesc->Common.Type)
421             {
422             case ACPI_TYPE_LOCAL_REFERENCE:
423 
424                 if (ObjDesc->Reference.Class != ACPI_REFCLASS_NAME)
425                 {
426                     ACPI_ERROR ((AE_INFO,
427                         "(PRT[%u].Source) Need name, found Reference Class 0x%X",
428                         Index, ObjDesc->Reference.Class));
429                     return_ACPI_STATUS (AE_BAD_DATA);
430                 }
431 
432                 Node = ObjDesc->Reference.Node;
433 
434                 /* Use *remaining* length of the buffer as max for pathname */
435 
436                 PathBuffer.Length = OutputBuffer->Length -
437                                     (UINT32) ((UINT8 *) UserPrt->Source -
438                                     (UINT8 *) OutputBuffer->Pointer);
439                 PathBuffer.Pointer = UserPrt->Source;
440 
441                 Status = AcpiNsHandleToPathname ((ACPI_HANDLE) Node, &PathBuffer);
442 
443                 /* +1 to include null terminator */
444 
445                 UserPrt->Length += (UINT32) ACPI_STRLEN (UserPrt->Source) + 1;
446                 break;
447 
448             case ACPI_TYPE_STRING:
449 
450                 ACPI_STRCPY (UserPrt->Source, ObjDesc->String.Pointer);
451 
452                 /*
453                  * Add to the Length field the length of the string
454                  * (add 1 for terminator)
455                  */
456                 UserPrt->Length += ObjDesc->String.Length + 1;
457                 break;
458 
459             case ACPI_TYPE_INTEGER:
460                 /*
461                  * If this is a number, then the Source Name is NULL, since the
462                  * entire buffer was zeroed out, we can leave this alone.
463                  *
464                  * Add to the Length field the length of the UINT32 NULL
465                  */
466                 UserPrt->Length += sizeof (UINT32);
467                 break;
468 
469             default:
470 
471                ACPI_ERROR ((AE_INFO,
472                    "(PRT[%u].Source) Need Ref/String/Integer, found %s",
473                    Index, AcpiUtGetObjectTypeName (ObjDesc)));
474                return_ACPI_STATUS (AE_BAD_DATA);
475             }
476         }
477 
478         /* Now align the current length */
479 
480         UserPrt->Length = (UINT32) ACPI_ROUND_UP_TO_64BIT (UserPrt->Length);
481 
482         /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
483 
484         ObjDesc = SubObjectList[3];
485         if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
486         {
487             ACPI_ERROR ((AE_INFO,
488                 "(PRT[%u].SourceIndex) Need Integer, found %s",
489                 Index, AcpiUtGetObjectTypeName (ObjDesc)));
490             return_ACPI_STATUS (AE_BAD_DATA);
491         }
492 
493         UserPrt->SourceIndex = (UINT32) ObjDesc->Integer.Value;
494 
495         /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
496 
497         TopObjectList++;
498     }
499 
500     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
501             OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
502     return_ACPI_STATUS (AE_OK);
503 }
504 
505 
506 /*******************************************************************************
507  *
508  * FUNCTION:    AcpiRsCreateAmlResources
509  *
510  * PARAMETERS:  LinkedListBuffer        - Pointer to the resource linked list
511  *              OutputBuffer            - Pointer to the user's buffer
512  *
513  * RETURN:      Status  AE_OK if okay, else a valid ACPI_STATUS code.
514  *              If the OutputBuffer is too small, the error will be
515  *              AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
516  *              to the size buffer needed.
517  *
518  * DESCRIPTION: Takes the linked list of device resources and
519  *              creates a bytestream to be used as input for the
520  *              _SRS control method.
521  *
522  ******************************************************************************/
523 
524 ACPI_STATUS
525 AcpiRsCreateAmlResources (
526     ACPI_RESOURCE           *LinkedListBuffer,
527     ACPI_BUFFER             *OutputBuffer)
528 {
529     ACPI_STATUS             Status;
530     ACPI_SIZE               AmlSizeNeeded = 0;
531 
532 
533     ACPI_FUNCTION_TRACE (RsCreateAmlResources);
534 
535 
536     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "LinkedListBuffer = %p\n",
537         LinkedListBuffer));
538 
539     /*
540      * Params already validated, so we don't re-validate here
541      *
542      * Pass the LinkedListBuffer into a module that calculates
543      * the buffer size needed for the byte stream.
544      */
545     Status = AcpiRsGetAmlLength (LinkedListBuffer,
546                 &AmlSizeNeeded);
547 
548     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
549         (UINT32) AmlSizeNeeded, AcpiFormatException (Status)));
550     if (ACPI_FAILURE (Status))
551     {
552         return_ACPI_STATUS (Status);
553     }
554 
555     /* Validate/Allocate/Clear caller buffer */
556 
557     Status = AcpiUtInitializeBuffer (OutputBuffer, AmlSizeNeeded);
558     if (ACPI_FAILURE (Status))
559     {
560         return_ACPI_STATUS (Status);
561     }
562 
563     /* Do the conversion */
564 
565     Status = AcpiRsConvertResourcesToAml (LinkedListBuffer, AmlSizeNeeded,
566                     OutputBuffer->Pointer);
567     if (ACPI_FAILURE (Status))
568     {
569         return_ACPI_STATUS (Status);
570     }
571 
572     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
573             OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
574     return_ACPI_STATUS (AE_OK);
575 }
576