1 /*******************************************************************************
3 * Module Name: rsutils - Utilities for the resource manager
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2011, 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.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/acresrc.h>
53 #define _COMPONENT ACPI_RESOURCES
54 ACPI_MODULE_NAME ("rsutils")
57 /*******************************************************************************
59 * FUNCTION: AcpiRsDecodeBitmask
61 * PARAMETERS: Mask - Bitmask to decode
62 * List - Where the converted list is returned
64 * RETURN: Count of bits set (length of list)
66 * DESCRIPTION: Convert a bit mask into a list of values
68 ******************************************************************************/
79 ACPI_FUNCTION_ENTRY ();
82 /* Decode the mask bits */
84 for (i = 0, BitCount = 0; Mask; i++)
99 /*******************************************************************************
101 * FUNCTION: AcpiRsEncodeBitmask
103 * PARAMETERS: List - List of values to encode
104 * Count - Length of list
106 * RETURN: Encoded bitmask
108 * DESCRIPTION: Convert a list of values to an encoded bitmask
110 ******************************************************************************/
113 AcpiRsEncodeBitmask (
121 ACPI_FUNCTION_ENTRY ();
124 /* Encode the list into a single bitmask */
126 for (i = 0, Mask = 0; i < Count; i++)
128 Mask |= (0x1 << List[i]);
135 /*******************************************************************************
137 * FUNCTION: AcpiRsMoveData
139 * PARAMETERS: Destination - Pointer to the destination descriptor
140 * Source - Pointer to the source descriptor
141 * ItemCount - How many items to move
142 * MoveType - Byte width
146 * DESCRIPTION: Move multiple data items from one descriptor to another. Handles
147 * alignment issues and endian issues if necessary, as configured
148 * via the ACPI_MOVE_* macros. (This is why a memcpy is not used)
150 ******************************************************************************/
162 ACPI_FUNCTION_ENTRY ();
165 /* One move per item */
167 for (i = 0; i < ItemCount; i++)
172 * For the 8-bit case, we can perform the move all at once
173 * since there are no alignment or endian issues
176 ACPI_MEMCPY (Destination, Source, ItemCount);
180 * 16-, 32-, and 64-bit cases must use the move macros that perform
181 * endian conversion and/or accomodate hardware that cannot perform
182 * misaligned memory transfers
184 case ACPI_RSC_MOVE16:
185 ACPI_MOVE_16_TO_16 (&ACPI_CAST_PTR (UINT16, Destination)[i],
186 &ACPI_CAST_PTR (UINT16, Source)[i]);
189 case ACPI_RSC_MOVE32:
190 ACPI_MOVE_32_TO_32 (&ACPI_CAST_PTR (UINT32, Destination)[i],
191 &ACPI_CAST_PTR (UINT32, Source)[i]);
194 case ACPI_RSC_MOVE64:
195 ACPI_MOVE_64_TO_64 (&ACPI_CAST_PTR (UINT64, Destination)[i],
196 &ACPI_CAST_PTR (UINT64, Source)[i]);
206 /*******************************************************************************
208 * FUNCTION: AcpiRsSetResourceLength
210 * PARAMETERS: TotalLength - Length of the AML descriptor, including
211 * the header and length fields.
212 * Aml - Pointer to the raw AML descriptor
216 * DESCRIPTION: Set the ResourceLength field of an AML
217 * resource descriptor, both Large and Small descriptors are
218 * supported automatically. Note: Descriptor Type field must
221 ******************************************************************************/
224 AcpiRsSetResourceLength (
225 ACPI_RSDESC_SIZE TotalLength,
228 ACPI_RS_LENGTH ResourceLength;
231 ACPI_FUNCTION_ENTRY ();
234 /* Length is the total descriptor length minus the header length */
236 ResourceLength = (ACPI_RS_LENGTH)
237 (TotalLength - AcpiUtGetResourceHeaderLength (Aml));
239 /* Length is stored differently for large and small descriptors */
241 if (Aml->SmallHeader.DescriptorType & ACPI_RESOURCE_NAME_LARGE)
243 /* Large descriptor -- bytes 1-2 contain the 16-bit length */
245 ACPI_MOVE_16_TO_16 (&Aml->LargeHeader.ResourceLength, &ResourceLength);
249 /* Small descriptor -- bits 2:0 of byte 0 contain the length */
251 Aml->SmallHeader.DescriptorType = (UINT8)
253 /* Clear any existing length, preserving descriptor type bits */
255 ((Aml->SmallHeader.DescriptorType & ~ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK)
262 /*******************************************************************************
264 * FUNCTION: AcpiRsSetResourceHeader
266 * PARAMETERS: DescriptorType - Byte to be inserted as the type
267 * TotalLength - Length of the AML descriptor, including
268 * the header and length fields.
269 * Aml - Pointer to the raw AML descriptor
273 * DESCRIPTION: Set the DescriptorType and ResourceLength fields of an AML
274 * resource descriptor, both Large and Small descriptors are
275 * supported automatically
277 ******************************************************************************/
280 AcpiRsSetResourceHeader (
281 UINT8 DescriptorType,
282 ACPI_RSDESC_SIZE TotalLength,
285 ACPI_FUNCTION_ENTRY ();
288 /* Set the Resource Type */
290 Aml->SmallHeader.DescriptorType = DescriptorType;
292 /* Set the Resource Length */
294 AcpiRsSetResourceLength (TotalLength, Aml);
298 /*******************************************************************************
300 * FUNCTION: AcpiRsStrcpy
302 * PARAMETERS: Destination - Pointer to the destination string
303 * Source - Pointer to the source string
305 * RETURN: String length, including NULL terminator
307 * DESCRIPTION: Local string copy that returns the string length, saving a
308 * strcpy followed by a strlen.
310 ******************************************************************************/
320 ACPI_FUNCTION_ENTRY ();
323 for (i = 0; Source[i]; i++)
325 Destination[i] = Source[i];
330 /* Return string length including the NULL terminator */
332 return ((UINT16) (i + 1));
336 /*******************************************************************************
338 * FUNCTION: AcpiRsGetResourceSource
340 * PARAMETERS: ResourceLength - Length field of the descriptor
341 * MinimumLength - Minimum length of the descriptor (minus
342 * any optional fields)
343 * ResourceSource - Where the ResourceSource is returned
344 * Aml - Pointer to the raw AML descriptor
345 * StringPtr - (optional) where to store the actual
346 * ResourceSource string
348 * RETURN: Length of the string plus NULL terminator, rounded up to native
351 * DESCRIPTION: Copy the optional ResourceSource data from a raw AML descriptor
352 * to an internal resource descriptor
354 ******************************************************************************/
357 AcpiRsGetResourceSource (
358 ACPI_RS_LENGTH ResourceLength,
359 ACPI_RS_LENGTH MinimumLength,
360 ACPI_RESOURCE_SOURCE *ResourceSource,
364 ACPI_RSDESC_SIZE TotalLength;
365 UINT8 *AmlResourceSource;
368 ACPI_FUNCTION_ENTRY ();
371 TotalLength = ResourceLength + sizeof (AML_RESOURCE_LARGE_HEADER);
372 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
375 * ResourceSource is present if the length of the descriptor is longer than
376 * the minimum length.
378 * Note: Some resource descriptors will have an additional null, so
379 * we add 1 to the minimum length.
381 if (TotalLength > (ACPI_RSDESC_SIZE) (MinimumLength + 1))
383 /* Get the ResourceSourceIndex */
385 ResourceSource->Index = AmlResourceSource[0];
387 ResourceSource->StringPtr = StringPtr;
391 * String destination pointer is not specified; Set the String
392 * pointer to the end of the current ResourceSource structure.
394 ResourceSource->StringPtr = ACPI_ADD_PTR (char, ResourceSource,
395 sizeof (ACPI_RESOURCE_SOURCE));
399 * In order for the Resource length to be a multiple of the native
400 * word, calculate the length of the string (+1 for NULL terminator)
401 * and expand to the next word multiple.
403 * Zero the entire area of the buffer.
405 TotalLength = (UINT32) ACPI_STRLEN (
406 ACPI_CAST_PTR (char, &AmlResourceSource[1])) + 1;
407 TotalLength = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (TotalLength);
409 ACPI_MEMSET (ResourceSource->StringPtr, 0, TotalLength);
411 /* Copy the ResourceSource string to the destination */
413 ResourceSource->StringLength = AcpiRsStrcpy (ResourceSource->StringPtr,
414 ACPI_CAST_PTR (char, &AmlResourceSource[1]));
416 return ((ACPI_RS_LENGTH) TotalLength);
419 /* ResourceSource is not present */
421 ResourceSource->Index = 0;
422 ResourceSource->StringLength = 0;
423 ResourceSource->StringPtr = NULL;
428 /*******************************************************************************
430 * FUNCTION: AcpiRsSetResourceSource
432 * PARAMETERS: Aml - Pointer to the raw AML descriptor
433 * MinimumLength - Minimum length of the descriptor (minus
434 * any optional fields)
435 * ResourceSource - Internal ResourceSource
438 * RETURN: Total length of the AML descriptor
440 * DESCRIPTION: Convert an optional ResourceSource from internal format to a
441 * raw AML resource descriptor
443 ******************************************************************************/
446 AcpiRsSetResourceSource (
448 ACPI_RS_LENGTH MinimumLength,
449 ACPI_RESOURCE_SOURCE *ResourceSource)
451 UINT8 *AmlResourceSource;
452 ACPI_RSDESC_SIZE DescriptorLength;
455 ACPI_FUNCTION_ENTRY ();
458 DescriptorLength = MinimumLength;
460 /* Non-zero string length indicates presence of a ResourceSource */
462 if (ResourceSource->StringLength)
464 /* Point to the end of the AML descriptor */
466 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
468 /* Copy the ResourceSourceIndex */
470 AmlResourceSource[0] = (UINT8) ResourceSource->Index;
472 /* Copy the ResourceSource string */
474 ACPI_STRCPY (ACPI_CAST_PTR (char, &AmlResourceSource[1]),
475 ResourceSource->StringPtr);
478 * Add the length of the string (+ 1 for null terminator) to the
479 * final descriptor length
481 DescriptorLength += ((ACPI_RSDESC_SIZE) ResourceSource->StringLength + 1);
484 /* Return the new total length of the AML descriptor */
486 return (DescriptorLength);
490 /*******************************************************************************
492 * FUNCTION: AcpiRsGetPrtMethodData
494 * PARAMETERS: Node - Device node
495 * RetBuffer - Pointer to a buffer structure for the
500 * DESCRIPTION: This function is called to get the _PRT value of an object
501 * contained in an object specified by the handle passed in
503 * If the function fails an appropriate status will be returned
504 * and the contents of the callers buffer is undefined.
506 ******************************************************************************/
509 AcpiRsGetPrtMethodData (
510 ACPI_NAMESPACE_NODE *Node,
511 ACPI_BUFFER *RetBuffer)
513 ACPI_OPERAND_OBJECT *ObjDesc;
517 ACPI_FUNCTION_TRACE (RsGetPrtMethodData);
520 /* Parameters guaranteed valid by caller */
522 /* Execute the method, no parameters */
524 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRT,
525 ACPI_BTYPE_PACKAGE, &ObjDesc);
526 if (ACPI_FAILURE (Status))
528 return_ACPI_STATUS (Status);
532 * Create a resource linked list from the byte stream buffer that comes
533 * back from the _CRS method execution.
535 Status = AcpiRsCreatePciRoutingTable (ObjDesc, RetBuffer);
537 /* On exit, we must delete the object returned by EvaluateObject */
539 AcpiUtRemoveReference (ObjDesc);
540 return_ACPI_STATUS (Status);
544 /*******************************************************************************
546 * FUNCTION: AcpiRsGetCrsMethodData
548 * PARAMETERS: Node - Device node
549 * RetBuffer - Pointer to a buffer structure for the
554 * DESCRIPTION: This function is called to get the _CRS value of an object
555 * contained in an object specified by the handle passed in
557 * If the function fails an appropriate status will be returned
558 * and the contents of the callers buffer is undefined.
560 ******************************************************************************/
563 AcpiRsGetCrsMethodData (
564 ACPI_NAMESPACE_NODE *Node,
565 ACPI_BUFFER *RetBuffer)
567 ACPI_OPERAND_OBJECT *ObjDesc;
571 ACPI_FUNCTION_TRACE (RsGetCrsMethodData);
574 /* Parameters guaranteed valid by caller */
576 /* Execute the method, no parameters */
578 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__CRS,
579 ACPI_BTYPE_BUFFER, &ObjDesc);
580 if (ACPI_FAILURE (Status))
582 return_ACPI_STATUS (Status);
586 * Make the call to create a resource linked list from the
587 * byte stream buffer that comes back from the _CRS method
590 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
592 /* On exit, we must delete the object returned by evaluateObject */
594 AcpiUtRemoveReference (ObjDesc);
595 return_ACPI_STATUS (Status);
599 /*******************************************************************************
601 * FUNCTION: AcpiRsGetPrsMethodData
603 * PARAMETERS: Node - Device node
604 * RetBuffer - Pointer to a buffer structure for the
609 * DESCRIPTION: This function is called to get the _PRS value of an object
610 * contained in an object specified by the handle passed in
612 * If the function fails an appropriate status will be returned
613 * and the contents of the callers buffer is undefined.
615 ******************************************************************************/
618 AcpiRsGetPrsMethodData (
619 ACPI_NAMESPACE_NODE *Node,
620 ACPI_BUFFER *RetBuffer)
622 ACPI_OPERAND_OBJECT *ObjDesc;
626 ACPI_FUNCTION_TRACE (RsGetPrsMethodData);
629 /* Parameters guaranteed valid by caller */
631 /* Execute the method, no parameters */
633 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRS,
634 ACPI_BTYPE_BUFFER, &ObjDesc);
635 if (ACPI_FAILURE (Status))
637 return_ACPI_STATUS (Status);
641 * Make the call to create a resource linked list from the
642 * byte stream buffer that comes back from the _CRS method
645 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
647 /* On exit, we must delete the object returned by evaluateObject */
649 AcpiUtRemoveReference (ObjDesc);
650 return_ACPI_STATUS (Status);
654 /*******************************************************************************
656 * FUNCTION: AcpiRsGetMethodData
658 * PARAMETERS: Handle - Handle to the containing object
659 * Path - Path to method, relative to Handle
660 * RetBuffer - Pointer to a buffer structure for the
665 * DESCRIPTION: This function is called to get the _CRS or _PRS value of an
666 * object contained in an object specified by the handle passed in
668 * If the function fails an appropriate status will be returned
669 * and the contents of the callers buffer is undefined.
671 ******************************************************************************/
674 AcpiRsGetMethodData (
677 ACPI_BUFFER *RetBuffer)
679 ACPI_OPERAND_OBJECT *ObjDesc;
683 ACPI_FUNCTION_TRACE (RsGetMethodData);
686 /* Parameters guaranteed valid by caller */
688 /* Execute the method, no parameters */
690 Status = AcpiUtEvaluateObject (Handle, Path, ACPI_BTYPE_BUFFER, &ObjDesc);
691 if (ACPI_FAILURE (Status))
693 return_ACPI_STATUS (Status);
697 * Make the call to create a resource linked list from the
698 * byte stream buffer that comes back from the method
701 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
703 /* On exit, we must delete the object returned by EvaluateObject */
705 AcpiUtRemoveReference (ObjDesc);
706 return_ACPI_STATUS (Status);
710 /*******************************************************************************
712 * FUNCTION: AcpiRsSetSrsMethodData
714 * PARAMETERS: Node - Device node
715 * InBuffer - Pointer to a buffer structure of the
720 * DESCRIPTION: This function is called to set the _SRS of an object contained
721 * in an object specified by the handle passed in
723 * If the function fails an appropriate status will be returned
724 * and the contents of the callers buffer is undefined.
726 * Note: Parameters guaranteed valid by caller
728 ******************************************************************************/
731 AcpiRsSetSrsMethodData (
732 ACPI_NAMESPACE_NODE *Node,
733 ACPI_BUFFER *InBuffer)
735 ACPI_EVALUATE_INFO *Info;
736 ACPI_OPERAND_OBJECT *Args[2];
741 ACPI_FUNCTION_TRACE (RsSetSrsMethodData);
744 /* Allocate and initialize the evaluation information block */
746 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
749 return_ACPI_STATUS (AE_NO_MEMORY);
752 Info->PrefixNode = Node;
753 Info->Pathname = METHOD_NAME__SRS;
754 Info->Parameters = Args;
755 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
758 * The InBuffer parameter will point to a linked list of
759 * resource parameters. It needs to be formatted into a
760 * byte stream to be sent in as an input parameter to _SRS
762 * Convert the linked list into a byte stream
764 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
765 Status = AcpiRsCreateAmlResources (InBuffer->Pointer, &Buffer);
766 if (ACPI_FAILURE (Status))
771 /* Create and initialize the method parameter object */
773 Args[0] = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
777 * Must free the buffer allocated above (otherwise it is freed
780 ACPI_FREE (Buffer.Pointer);
781 Status = AE_NO_MEMORY;
785 Args[0]->Buffer.Length = (UINT32) Buffer.Length;
786 Args[0]->Buffer.Pointer = Buffer.Pointer;
787 Args[0]->Common.Flags = AOPOBJ_DATA_VALID;
790 /* Execute the method, no return value is expected */
792 Status = AcpiNsEvaluate (Info);
794 /* Clean up and return the status from AcpiNsEvaluate */
796 AcpiUtRemoveReference (Args[0]);
800 return_ACPI_STATUS (Status);