1 /*******************************************************************************
3 * Module Name: rscreate - Create resource lists/tables
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
44 #define __RSCREATE_C__
51 #define _COMPONENT ACPI_RESOURCES
52 ACPI_MODULE_NAME ("rscreate")
55 /*******************************************************************************
57 * FUNCTION: AcpiBufferToResource
59 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
60 * AmlBufferLength - Length of the AmlBuffer
61 * ResourcePtr - Where the converted resource is returned
65 * DESCRIPTION: Convert a raw AML buffer to a resource list
67 ******************************************************************************/
70 AcpiBufferToResource (
72 UINT16 AmlBufferLength
,
73 ACPI_RESOURCE
**ResourcePtr
)
76 ACPI_SIZE ListSizeNeeded
;
78 void *CurrentResourcePtr
;
81 * Note: we allow AE_AML_NO_RESOURCE_END_TAG, since an end tag
82 * is not required here.
85 /* Get the required length for the converted resource */
87 Status
= AcpiRsGetListLength (AmlBuffer
, AmlBufferLength
,
89 if (Status
== AE_AML_NO_RESOURCE_END_TAG
)
93 if (ACPI_FAILURE (Status
))
98 /* Allocate a buffer for the converted resource */
100 Resource
= ACPI_ALLOCATE_ZEROED (ListSizeNeeded
);
101 CurrentResourcePtr
= Resource
;
104 return (AE_NO_MEMORY
);
107 /* Perform the AML-to-Resource conversion */
109 Status
= AcpiUtWalkAmlResources (NULL
, AmlBuffer
, AmlBufferLength
,
110 AcpiRsConvertAmlToResources
, &CurrentResourcePtr
);
111 if (Status
== AE_AML_NO_RESOURCE_END_TAG
)
115 if (ACPI_FAILURE (Status
))
117 ACPI_FREE (Resource
);
121 *ResourcePtr
= Resource
;
128 /*******************************************************************************
130 * FUNCTION: AcpiRsCreateResourceList
132 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
133 * OutputBuffer - Pointer to the user's buffer
135 * RETURN: Status: AE_OK if okay, else a valid ACPI_STATUS code
136 * If OutputBuffer is not large enough, OutputBufferLength
137 * indicates how large OutputBuffer should be, else it
138 * indicates how may UINT8 elements of OutputBuffer are valid.
140 * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
141 * execution and parses the stream to create a linked list
142 * of device resources.
144 ******************************************************************************/
147 AcpiRsCreateResourceList (
148 ACPI_OPERAND_OBJECT
*AmlBuffer
,
149 ACPI_BUFFER
*OutputBuffer
)
154 ACPI_SIZE ListSizeNeeded
= 0;
155 UINT32 AmlBufferLength
;
159 ACPI_FUNCTION_TRACE (RsCreateResourceList
);
162 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "AmlBuffer = %p\n",
165 /* Params already validated, so we don't re-validate here */
167 AmlBufferLength
= AmlBuffer
->Buffer
.Length
;
168 AmlStart
= AmlBuffer
->Buffer
.Pointer
;
171 * Pass the AmlBuffer into a module that can calculate
172 * the buffer size needed for the linked list
174 Status
= AcpiRsGetListLength (AmlStart
, AmlBufferLength
,
177 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "Status=%X ListSizeNeeded=%X\n",
178 Status
, (UINT32
) ListSizeNeeded
));
179 if (ACPI_FAILURE (Status
))
181 return_ACPI_STATUS (Status
);
184 /* Validate/Allocate/Clear caller buffer */
186 Status
= AcpiUtInitializeBuffer (OutputBuffer
, ListSizeNeeded
);
187 if (ACPI_FAILURE (Status
))
189 return_ACPI_STATUS (Status
);
192 /* Do the conversion */
194 Resource
= OutputBuffer
->Pointer
;
195 Status
= AcpiUtWalkAmlResources (NULL
, AmlStart
, AmlBufferLength
,
196 AcpiRsConvertAmlToResources
, &Resource
);
197 if (ACPI_FAILURE (Status
))
199 return_ACPI_STATUS (Status
);
202 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "OutputBuffer %p Length %X\n",
203 OutputBuffer
->Pointer
, (UINT32
) OutputBuffer
->Length
));
204 return_ACPI_STATUS (AE_OK
);
208 /*******************************************************************************
210 * FUNCTION: AcpiRsCreatePciRoutingTable
212 * PARAMETERS: PackageObject - Pointer to a package containing one
213 * of more ACPI_OPERAND_OBJECTs
214 * OutputBuffer - Pointer to the user's buffer
216 * RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code.
217 * If the OutputBuffer is too small, the error will be
218 * AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
219 * to the size buffer needed.
221 * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a
222 * linked list of PCI interrupt descriptions
224 * NOTE: It is the caller's responsibility to ensure that the start of the
225 * output buffer is aligned properly (if necessary).
227 ******************************************************************************/
230 AcpiRsCreatePciRoutingTable (
231 ACPI_OPERAND_OBJECT
*PackageObject
,
232 ACPI_BUFFER
*OutputBuffer
)
235 ACPI_OPERAND_OBJECT
**TopObjectList
;
236 ACPI_OPERAND_OBJECT
**SubObjectList
;
237 ACPI_OPERAND_OBJECT
*ObjDesc
;
238 ACPI_SIZE BufferSizeNeeded
= 0;
239 UINT32 NumberOfElements
;
241 ACPI_PCI_ROUTING_TABLE
*UserPrt
;
242 ACPI_NAMESPACE_NODE
*Node
;
244 ACPI_BUFFER PathBuffer
;
247 ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable
);
250 /* Params already validated, so we don't re-validate here */
252 /* Get the required buffer length */
254 Status
= AcpiRsGetPciRoutingTableLength (PackageObject
,
256 if (ACPI_FAILURE (Status
))
258 return_ACPI_STATUS (Status
);
261 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "BufferSizeNeeded = %X\n",
262 (UINT32
) BufferSizeNeeded
));
264 /* Validate/Allocate/Clear caller buffer */
266 Status
= AcpiUtInitializeBuffer (OutputBuffer
, BufferSizeNeeded
);
267 if (ACPI_FAILURE (Status
))
269 return_ACPI_STATUS (Status
);
273 * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
274 * package that in turn contains an UINT64 Address, a UINT8 Pin,
275 * a Name, and a UINT8 SourceIndex.
277 TopObjectList
= PackageObject
->Package
.Elements
;
278 NumberOfElements
= PackageObject
->Package
.Count
;
279 Buffer
= OutputBuffer
->Pointer
;
280 UserPrt
= ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE
, Buffer
);
282 for (Index
= 0; Index
< NumberOfElements
; Index
++)
285 * Point UserPrt past this current structure
287 * NOTE: On the first iteration, UserPrt->Length will
288 * be zero because we cleared the return buffer earlier
290 Buffer
+= UserPrt
->Length
;
291 UserPrt
= ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE
, Buffer
);
294 * Fill in the Length field with the information we have at this point.
295 * The minus four is to subtract the size of the UINT8 Source[4] member
296 * because it is added below.
298 UserPrt
->Length
= (sizeof (ACPI_PCI_ROUTING_TABLE
) - 4);
300 /* Each sub-package must be of length 4 */
302 if ((*TopObjectList
)->Package
.Count
!= 4)
304 ACPI_ERROR ((AE_INFO
,
305 "(PRT[%u]) Need package of length 4, found length %u",
306 Index
, (*TopObjectList
)->Package
.Count
));
307 return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT
);
311 * Dereference the sub-package.
312 * The SubObjectList will now point to an array of the four IRQ
313 * elements: [Address, Pin, Source, SourceIndex]
315 SubObjectList
= (*TopObjectList
)->Package
.Elements
;
317 /* 1) First subobject: Dereference the PRT.Address */
319 ObjDesc
= SubObjectList
[0];
320 if (ObjDesc
->Common
.Type
!= ACPI_TYPE_INTEGER
)
322 ACPI_ERROR ((AE_INFO
, "(PRT[%u].Address) Need Integer, found %s",
323 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
324 return_ACPI_STATUS (AE_BAD_DATA
);
327 UserPrt
->Address
= ObjDesc
->Integer
.Value
;
329 /* 2) Second subobject: Dereference the PRT.Pin */
331 ObjDesc
= SubObjectList
[1];
332 if (ObjDesc
->Common
.Type
!= ACPI_TYPE_INTEGER
)
334 ACPI_ERROR ((AE_INFO
, "(PRT[%u].Pin) Need Integer, found %s",
335 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
336 return_ACPI_STATUS (AE_BAD_DATA
);
339 UserPrt
->Pin
= (UINT32
) ObjDesc
->Integer
.Value
;
342 * 3) Third subobject: Dereference the PRT.SourceName
343 * The name may be unresolved (slack mode), so allow a null object
345 ObjDesc
= SubObjectList
[2];
348 switch (ObjDesc
->Common
.Type
)
350 case ACPI_TYPE_LOCAL_REFERENCE
:
352 if (ObjDesc
->Reference
.Class
!= ACPI_REFCLASS_NAME
)
354 ACPI_ERROR ((AE_INFO
,
355 "(PRT[%u].Source) Need name, found Reference Class 0x%X",
356 Index
, ObjDesc
->Reference
.Class
));
357 return_ACPI_STATUS (AE_BAD_DATA
);
360 Node
= ObjDesc
->Reference
.Node
;
362 /* Use *remaining* length of the buffer as max for pathname */
364 PathBuffer
.Length
= OutputBuffer
->Length
-
365 (UINT32
) ((UINT8
*) UserPrt
->Source
-
366 (UINT8
*) OutputBuffer
->Pointer
);
367 PathBuffer
.Pointer
= UserPrt
->Source
;
369 Status
= AcpiNsHandleToPathname ((ACPI_HANDLE
) Node
, &PathBuffer
);
371 /* +1 to include null terminator */
373 UserPrt
->Length
+= (UINT32
) ACPI_STRLEN (UserPrt
->Source
) + 1;
376 case ACPI_TYPE_STRING
:
378 ACPI_STRCPY (UserPrt
->Source
, ObjDesc
->String
.Pointer
);
381 * Add to the Length field the length of the string
382 * (add 1 for terminator)
384 UserPrt
->Length
+= ObjDesc
->String
.Length
+ 1;
387 case ACPI_TYPE_INTEGER
:
389 * If this is a number, then the Source Name is NULL, since the
390 * entire buffer was zeroed out, we can leave this alone.
392 * Add to the Length field the length of the UINT32 NULL
394 UserPrt
->Length
+= sizeof (UINT32
);
399 ACPI_ERROR ((AE_INFO
,
400 "(PRT[%u].Source) Need Ref/String/Integer, found %s",
401 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
402 return_ACPI_STATUS (AE_BAD_DATA
);
406 /* Now align the current length */
408 UserPrt
->Length
= (UINT32
) ACPI_ROUND_UP_TO_64BIT (UserPrt
->Length
);
410 /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
412 ObjDesc
= SubObjectList
[3];
413 if (ObjDesc
->Common
.Type
!= ACPI_TYPE_INTEGER
)
415 ACPI_ERROR ((AE_INFO
,
416 "(PRT[%u].SourceIndex) Need Integer, found %s",
417 Index
, AcpiUtGetObjectTypeName (ObjDesc
)));
418 return_ACPI_STATUS (AE_BAD_DATA
);
421 UserPrt
->SourceIndex
= (UINT32
) ObjDesc
->Integer
.Value
;
423 /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
428 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "OutputBuffer %p Length %X\n",
429 OutputBuffer
->Pointer
, (UINT32
) OutputBuffer
->Length
));
430 return_ACPI_STATUS (AE_OK
);
434 /*******************************************************************************
436 * FUNCTION: AcpiRsCreateAmlResources
438 * PARAMETERS: ResourceList - Pointer to the resource list buffer
439 * OutputBuffer - Where the AML buffer is returned
441 * RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code.
442 * If the OutputBuffer is too small, the error will be
443 * AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
444 * to the size buffer needed.
446 * DESCRIPTION: Converts a list of device resources to an AML bytestream
447 * to be used as input for the _SRS control method.
449 ******************************************************************************/
452 AcpiRsCreateAmlResources (
453 ACPI_BUFFER
*ResourceList
,
454 ACPI_BUFFER
*OutputBuffer
)
457 ACPI_SIZE AmlSizeNeeded
= 0;
460 ACPI_FUNCTION_TRACE (RsCreateAmlResources
);
463 /* Params already validated, no need to re-validate here */
465 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "ResourceList Buffer = %p\n",
466 ResourceList
->Pointer
));
468 /* Get the buffer size needed for the AML byte stream */
470 Status
= AcpiRsGetAmlLength (ResourceList
->Pointer
,
471 ResourceList
->Length
, &AmlSizeNeeded
);
473 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "AmlSizeNeeded=%X, %s\n",
474 (UINT32
) AmlSizeNeeded
, AcpiFormatException (Status
)));
475 if (ACPI_FAILURE (Status
))
477 return_ACPI_STATUS (Status
);
480 /* Validate/Allocate/Clear caller buffer */
482 Status
= AcpiUtInitializeBuffer (OutputBuffer
, AmlSizeNeeded
);
483 if (ACPI_FAILURE (Status
))
485 return_ACPI_STATUS (Status
);
488 /* Do the conversion */
490 Status
= AcpiRsConvertResourcesToAml (ResourceList
->Pointer
,
491 AmlSizeNeeded
, OutputBuffer
->Pointer
);
492 if (ACPI_FAILURE (Status
))
494 return_ACPI_STATUS (Status
);
497 ACPI_DEBUG_PRINT ((ACPI_DB_INFO
, "OutputBuffer %p Length %X\n",
498 OutputBuffer
->Pointer
, (UINT32
) OutputBuffer
->Length
));
499 return_ACPI_STATUS (AE_OK
);