1 /*******************************************************************************
3 * Module Name: rscreate - Create resource lists/tables
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2019, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
49 #define _COMPONENT ACPI_RESOURCES
50 ACPI_MODULE_NAME ("rscreate")
53 /*******************************************************************************
55 * FUNCTION: AcpiBufferToResource
57 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
58 * AmlBufferLength - Length of the AmlBuffer
59 * ResourcePtr - Where the converted resource is returned
63 * DESCRIPTION: Convert a raw AML buffer to a resource list
65 ******************************************************************************/
68 AcpiBufferToResource (
70 UINT16 AmlBufferLength
,
71 ACPI_RESOURCE
**ResourcePtr
)
74 ACPI_SIZE ListSizeNeeded
;
76 void *CurrentResourcePtr
;
79 ACPI_FUNCTION_TRACE (AcpiBufferToResource
);
83 * Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag
84 * is not required here.
87 /* Get the required length for the converted resource */
89 Status
= AcpiRsGetListLength (
90 AmlBuffer
, AmlBufferLength
, &ListSizeNeeded
);
91 if (Status
== AE_AML_NO_RESOURCE_END_TAG
)
95 if (ACPI_FAILURE (Status
))
97 return_ACPI_STATUS (Status
);
100 /* Allocate a buffer for the converted resource */
102 Resource
= ACPI_ALLOCATE_ZEROED (ListSizeNeeded
);
103 CurrentResourcePtr
= Resource
;
106 return_ACPI_STATUS (AE_NO_MEMORY
);
109 /* Perform the AML-to-Resource conversion */
111 Status
= AcpiUtWalkAmlResources (NULL
, AmlBuffer
, AmlBufferLength
,
112 AcpiRsConvertAmlToResources
, &CurrentResourcePtr
);
113 if (Status
== AE_AML_NO_RESOURCE_END_TAG
)
117 if (ACPI_FAILURE (Status
))
119 ACPI_FREE (Resource
);
123 *ResourcePtr
= Resource
;
126 return_ACPI_STATUS (Status
);
129 ACPI_EXPORT_SYMBOL (AcpiBufferToResource
)
132 /*******************************************************************************
134 * FUNCTION: AcpiRsCreateResourceList
136 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
137 * OutputBuffer - Pointer to the user's buffer
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.
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.
148 ******************************************************************************/
151 AcpiRsCreateResourceList (
152 ACPI_OPERAND_OBJECT
*AmlBuffer
,
153 ACPI_BUFFER
*OutputBuffer
)
158 ACPI_SIZE ListSizeNeeded
= 0;
159 UINT32 AmlBufferLength
;
163 ACPI_FUNCTION_TRACE (RsCreateResourceList
);
166 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "AmlBuffer = %p\n",
169 /* Params already validated, so we don't re-validate here */
171 AmlBufferLength
= AmlBuffer
->Buffer
.Length
;
172 AmlStart
= AmlBuffer
->Buffer
.Pointer
;
175 * Pass the AmlBuffer into a module that can calculate
176 * the buffer size needed for the linked list
178 Status
= AcpiRsGetListLength (AmlStart
, AmlBufferLength
,
181 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "Status=%X ListSizeNeeded=%X\n",
182 Status
, (UINT32
) ListSizeNeeded
));
183 if (ACPI_FAILURE (Status
))
185 return_ACPI_STATUS (Status
);
188 /* Validate/Allocate/Clear caller buffer */
190 Status
= AcpiUtInitializeBuffer (OutputBuffer
, ListSizeNeeded
);
191 if (ACPI_FAILURE (Status
))
193 return_ACPI_STATUS (Status
);
196 /* Do the conversion */
198 Resource
= OutputBuffer
->Pointer
;
199 Status
= AcpiUtWalkAmlResources (NULL
, AmlStart
, AmlBufferLength
,
200 AcpiRsConvertAmlToResources
, &Resource
);
201 if (ACPI_FAILURE (Status
))
203 return_ACPI_STATUS (Status
);
206 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "OutputBuffer %p Length %X\n",
207 OutputBuffer
->Pointer
, (UINT32
) OutputBuffer
->Length
));
208 return_ACPI_STATUS (AE_OK
);
212 /*******************************************************************************
214 * FUNCTION: AcpiRsCreatePciRoutingTable
216 * PARAMETERS: PackageObject - Pointer to a package containing one
217 * of more ACPI_OPERAND_OBJECTs
218 * OutputBuffer - Pointer to the user's buffer
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.
225 * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a
226 * linked list of PCI interrupt descriptions
228 * NOTE: It is the caller's responsibility to ensure that the start of the
229 * output buffer is aligned properly (if necessary).
231 ******************************************************************************/
234 AcpiRsCreatePciRoutingTable (
235 ACPI_OPERAND_OBJECT
*PackageObject
,
236 ACPI_BUFFER
*OutputBuffer
)
239 ACPI_OPERAND_OBJECT
**TopObjectList
;
240 ACPI_OPERAND_OBJECT
**SubObjectList
;
241 ACPI_OPERAND_OBJECT
*ObjDesc
;
242 ACPI_SIZE BufferSizeNeeded
= 0;
243 UINT32 NumberOfElements
;
245 ACPI_PCI_ROUTING_TABLE
*UserPrt
;
246 ACPI_NAMESPACE_NODE
*Node
;
248 ACPI_BUFFER PathBuffer
;
251 ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable
);
254 /* Params already validated, so we don't re-validate here */
256 /* Get the required buffer length */
258 Status
= AcpiRsGetPciRoutingTableLength (
259 PackageObject
,&BufferSizeNeeded
);
260 if (ACPI_FAILURE (Status
))
262 return_ACPI_STATUS (Status
);
265 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "BufferSizeNeeded = %X\n",
266 (UINT32
) BufferSizeNeeded
));
268 /* Validate/Allocate/Clear caller buffer */
270 Status
= AcpiUtInitializeBuffer (OutputBuffer
, BufferSizeNeeded
);
271 if (ACPI_FAILURE (Status
))
273 return_ACPI_STATUS (Status
);
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.
281 TopObjectList
= PackageObject
->Package
.Elements
;
282 NumberOfElements
= PackageObject
->Package
.Count
;
283 Buffer
= OutputBuffer
->Pointer
;
284 UserPrt
= ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE
, Buffer
);
286 for (Index
= 0; Index
< NumberOfElements
; Index
++)
289 * Point UserPrt past this current structure
291 * NOTE: On the first iteration, UserPrt->Length will
292 * be zero because we cleared the return buffer earlier
294 Buffer
+= UserPrt
->Length
;
295 UserPrt
= ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE
, Buffer
);
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.
302 UserPrt
->Length
= (sizeof (ACPI_PCI_ROUTING_TABLE
) - 4);
304 /* Each subpackage must be of length 4 */
306 if ((*TopObjectList
)->Package
.Count
!= 4)
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
);
315 * Dereference the subpackage.
316 * The SubObjectList will now point to an array of the four IRQ
317 * elements: [Address, Pin, Source, SourceIndex]
319 SubObjectList
= (*TopObjectList
)->Package
.Elements
;
321 /* 1) First subobject: Dereference the PRT.Address */
323 ObjDesc
= SubObjectList
[0];
324 if (!ObjDesc
|| ObjDesc
->Common
.Type
!= ACPI_TYPE_INTEGER
)
326 ACPI_ERROR ((AE_INFO
,
327 "(PRT[%u].Address) Need Integer, found %s",
328 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
329 return_ACPI_STATUS (AE_BAD_DATA
);
332 UserPrt
->Address
= ObjDesc
->Integer
.Value
;
334 /* 2) Second subobject: Dereference the PRT.Pin */
336 ObjDesc
= SubObjectList
[1];
337 if (!ObjDesc
|| ObjDesc
->Common
.Type
!= ACPI_TYPE_INTEGER
)
339 ACPI_ERROR ((AE_INFO
, "(PRT[%u].Pin) Need Integer, found %s",
340 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
341 return_ACPI_STATUS (AE_BAD_DATA
);
344 UserPrt
->Pin
= (UINT32
) ObjDesc
->Integer
.Value
;
347 * 3) Third subobject: Dereference the PRT.SourceName
348 * The name may be unresolved (slack mode), so allow a null object
350 ObjDesc
= SubObjectList
[2];
353 switch (ObjDesc
->Common
.Type
)
355 case ACPI_TYPE_LOCAL_REFERENCE
:
357 if (ObjDesc
->Reference
.Class
!= ACPI_REFCLASS_NAME
)
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
);
365 Node
= ObjDesc
->Reference
.Node
;
367 /* Use *remaining* length of the buffer as max for pathname */
369 PathBuffer
.Length
= OutputBuffer
->Length
-
370 (UINT32
) ((UINT8
*) UserPrt
->Source
-
371 (UINT8
*) OutputBuffer
->Pointer
);
372 PathBuffer
.Pointer
= UserPrt
->Source
;
374 Status
= AcpiNsHandleToPathname (
375 (ACPI_HANDLE
) Node
, &PathBuffer
, FALSE
);
376 if (ACPI_FAILURE (Status
))
378 return_ACPI_STATUS (Status
);
381 /* +1 to include null terminator */
383 UserPrt
->Length
+= (UINT32
) strlen (UserPrt
->Source
) + 1;
386 case ACPI_TYPE_STRING
:
388 strcpy (UserPrt
->Source
, ObjDesc
->String
.Pointer
);
391 * Add to the Length field the length of the string
392 * (add 1 for terminator)
394 UserPrt
->Length
+= ObjDesc
->String
.Length
+ 1;
397 case ACPI_TYPE_INTEGER
:
399 * If this is a number, then the Source Name is NULL, since
400 * the entire buffer was zeroed out, we can leave this alone.
402 * Add to the Length field the length of the UINT32 NULL
404 UserPrt
->Length
+= sizeof (UINT32
);
409 ACPI_ERROR ((AE_INFO
,
410 "(PRT[%u].Source) Need Ref/String/Integer, found %s",
411 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
412 return_ACPI_STATUS (AE_BAD_DATA
);
416 /* Now align the current length */
418 UserPrt
->Length
= (UINT32
) ACPI_ROUND_UP_TO_64BIT (UserPrt
->Length
);
420 /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
422 ObjDesc
= SubObjectList
[3];
423 if (!ObjDesc
|| ObjDesc
->Common
.Type
!= ACPI_TYPE_INTEGER
)
425 ACPI_ERROR ((AE_INFO
,
426 "(PRT[%u].SourceIndex) Need Integer, found %s",
427 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
428 return_ACPI_STATUS (AE_BAD_DATA
);
431 UserPrt
->SourceIndex
= (UINT32
) ObjDesc
->Integer
.Value
;
433 /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
438 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "OutputBuffer %p Length %X\n",
439 OutputBuffer
->Pointer
, (UINT32
) OutputBuffer
->Length
));
440 return_ACPI_STATUS (AE_OK
);
444 /*******************************************************************************
446 * FUNCTION: AcpiRsCreateAmlResources
448 * PARAMETERS: ResourceList - Pointer to the resource list buffer
449 * OutputBuffer - Where the AML buffer is returned
451 * RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code.
452 * If the OutputBuffer is too small, the error will be
453 * AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
454 * to the size buffer needed.
456 * DESCRIPTION: Converts a list of device resources to an AML bytestream
457 * to be used as input for the _SRS control method.
459 ******************************************************************************/
462 AcpiRsCreateAmlResources (
463 ACPI_BUFFER
*ResourceList
,
464 ACPI_BUFFER
*OutputBuffer
)
467 ACPI_SIZE AmlSizeNeeded
= 0;
470 ACPI_FUNCTION_TRACE (RsCreateAmlResources
);
473 /* Params already validated, no need to re-validate here */
475 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "ResourceList Buffer = %p\n",
476 ResourceList
->Pointer
));
478 /* Get the buffer size needed for the AML byte stream */
480 Status
= AcpiRsGetAmlLength (
481 ResourceList
->Pointer
, ResourceList
->Length
, &AmlSizeNeeded
);
483 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "AmlSizeNeeded=%X, %s\n",
484 (UINT32
) AmlSizeNeeded
, AcpiFormatException (Status
)));
485 if (ACPI_FAILURE (Status
))
487 return_ACPI_STATUS (Status
);
490 /* Validate/Allocate/Clear caller buffer */
492 Status
= AcpiUtInitializeBuffer (OutputBuffer
, AmlSizeNeeded
);
493 if (ACPI_FAILURE (Status
))
495 return_ACPI_STATUS (Status
);
498 /* Do the conversion */
500 Status
= AcpiRsConvertResourcesToAml (ResourceList
->Pointer
,
501 AmlSizeNeeded
, OutputBuffer
->Pointer
);
502 if (ACPI_FAILURE (Status
))
504 return_ACPI_STATUS (Status
);
507 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "OutputBuffer %p Length %X\n",
508 OutputBuffer
->Pointer
, (UINT32
) OutputBuffer
->Length
));
509 return_ACPI_STATUS (AE_OK
);