1 /*******************************************************************************
3 * Module Name: rsutils - Utilities for the resource manager
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, 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.
50 #define _COMPONENT ACPI_RESOURCES
51 ACPI_MODULE_NAME ("rsutils")
54 /*******************************************************************************
56 * FUNCTION: AcpiRsDecodeBitmask
58 * PARAMETERS: Mask - Bitmask to decode
59 * List - Where the converted list is returned
61 * RETURN: Count of bits set (length of list)
63 * DESCRIPTION: Convert a bit mask into a list of values
65 ******************************************************************************/
76 ACPI_FUNCTION_ENTRY ();
79 /* Decode the mask bits */
81 for (i = 0, BitCount = 0; Mask; i++)
96 /*******************************************************************************
98 * FUNCTION: AcpiRsEncodeBitmask
100 * PARAMETERS: List - List of values to encode
101 * Count - Length of list
103 * RETURN: Encoded bitmask
105 * DESCRIPTION: Convert a list of values to an encoded bitmask
107 ******************************************************************************/
110 AcpiRsEncodeBitmask (
118 ACPI_FUNCTION_ENTRY ();
121 /* Encode the list into a single bitmask */
123 for (i = 0, Mask = 0; i < Count; i++)
125 Mask |= (0x1 << List[i]);
132 /*******************************************************************************
134 * FUNCTION: AcpiRsMoveData
136 * PARAMETERS: Destination - Pointer to the destination descriptor
137 * Source - Pointer to the source descriptor
138 * ItemCount - How many items to move
139 * MoveType - Byte width
143 * DESCRIPTION: Move multiple data items from one descriptor to another. Handles
144 * alignment issues and endian issues if necessary, as configured
145 * via the ACPI_MOVE_* macros. (This is why a memcpy is not used)
147 ******************************************************************************/
159 ACPI_FUNCTION_ENTRY ();
162 /* One move per item */
164 for (i = 0; i < ItemCount; i++)
169 * For the 8-bit case, we can perform the move all at once
170 * since there are no alignment or endian issues
173 case ACPI_RSC_MOVE_GPIO_RES:
174 case ACPI_RSC_MOVE_SERIAL_VEN:
175 case ACPI_RSC_MOVE_SERIAL_RES:
177 memcpy (Destination, Source, ItemCount);
181 * 16-, 32-, and 64-bit cases must use the move macros that perform
182 * endian conversion and/or accommodate hardware that cannot perform
183 * misaligned memory transfers
185 case ACPI_RSC_MOVE16:
186 case ACPI_RSC_MOVE_GPIO_PIN:
189 &ACPI_CAST_PTR (UINT16, Destination)[i],
190 &ACPI_CAST_PTR (UINT16, Source)[i]);
193 case ACPI_RSC_MOVE32:
196 &ACPI_CAST_PTR (UINT32, Destination)[i],
197 &ACPI_CAST_PTR (UINT32, Source)[i]);
200 case ACPI_RSC_MOVE64:
203 &ACPI_CAST_PTR (UINT64, Destination)[i],
204 &ACPI_CAST_PTR (UINT64, Source)[i]);
215 /*******************************************************************************
217 * FUNCTION: AcpiRsSetResourceLength
219 * PARAMETERS: TotalLength - Length of the AML descriptor, including
220 * the header and length fields.
221 * Aml - Pointer to the raw AML descriptor
225 * DESCRIPTION: Set the ResourceLength field of an AML
226 * resource descriptor, both Large and Small descriptors are
227 * supported automatically. Note: Descriptor Type field must
230 ******************************************************************************/
233 AcpiRsSetResourceLength (
234 ACPI_RSDESC_SIZE TotalLength,
237 ACPI_RS_LENGTH ResourceLength;
240 ACPI_FUNCTION_ENTRY ();
243 /* Length is the total descriptor length minus the header length */
245 ResourceLength = (ACPI_RS_LENGTH)
246 (TotalLength - AcpiUtGetResourceHeaderLength (Aml));
248 /* Length is stored differently for large and small descriptors */
250 if (Aml->SmallHeader.DescriptorType & ACPI_RESOURCE_NAME_LARGE)
252 /* Large descriptor -- bytes 1-2 contain the 16-bit length */
255 &Aml->LargeHeader.ResourceLength, &ResourceLength);
260 * Small descriptor -- bits 2:0 of byte 0 contain the length
261 * Clear any existing length, preserving descriptor type bits
263 Aml->SmallHeader.DescriptorType = (UINT8)
264 ((Aml->SmallHeader.DescriptorType &
265 ~ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK)
271 /*******************************************************************************
273 * FUNCTION: AcpiRsSetResourceHeader
275 * PARAMETERS: DescriptorType - Byte to be inserted as the type
276 * TotalLength - Length of the AML descriptor, including
277 * the header and length fields.
278 * Aml - Pointer to the raw AML descriptor
282 * DESCRIPTION: Set the DescriptorType and ResourceLength fields of an AML
283 * resource descriptor, both Large and Small descriptors are
284 * supported automatically
286 ******************************************************************************/
289 AcpiRsSetResourceHeader (
290 UINT8 DescriptorType,
291 ACPI_RSDESC_SIZE TotalLength,
294 ACPI_FUNCTION_ENTRY ();
297 /* Set the Resource Type */
299 Aml->SmallHeader.DescriptorType = DescriptorType;
301 /* Set the Resource Length */
303 AcpiRsSetResourceLength (TotalLength, Aml);
307 /*******************************************************************************
309 * FUNCTION: AcpiRsStrcpy
311 * PARAMETERS: Destination - Pointer to the destination string
312 * Source - Pointer to the source string
314 * RETURN: String length, including NULL terminator
316 * DESCRIPTION: Local string copy that returns the string length, saving a
317 * strcpy followed by a strlen.
319 ******************************************************************************/
329 ACPI_FUNCTION_ENTRY ();
332 for (i = 0; Source[i]; i++)
334 Destination[i] = Source[i];
339 /* Return string length including the NULL terminator */
341 return ((UINT16) (i + 1));
345 /*******************************************************************************
347 * FUNCTION: AcpiRsGetResourceSource
349 * PARAMETERS: ResourceLength - Length field of the descriptor
350 * MinimumLength - Minimum length of the descriptor (minus
351 * any optional fields)
352 * ResourceSource - Where the ResourceSource is returned
353 * Aml - Pointer to the raw AML descriptor
354 * StringPtr - (optional) where to store the actual
355 * ResourceSource string
357 * RETURN: Length of the string plus NULL terminator, rounded up to native
360 * DESCRIPTION: Copy the optional ResourceSource data from a raw AML descriptor
361 * to an internal resource descriptor
363 ******************************************************************************/
366 AcpiRsGetResourceSource (
367 ACPI_RS_LENGTH ResourceLength,
368 ACPI_RS_LENGTH MinimumLength,
369 ACPI_RESOURCE_SOURCE *ResourceSource,
373 ACPI_RSDESC_SIZE TotalLength;
374 UINT8 *AmlResourceSource;
377 ACPI_FUNCTION_ENTRY ();
380 TotalLength = ResourceLength + sizeof (AML_RESOURCE_LARGE_HEADER);
381 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
384 * ResourceSource is present if the length of the descriptor is longer
385 * than the minimum length.
387 * Note: Some resource descriptors will have an additional null, so
388 * we add 1 to the minimum length.
390 if (TotalLength > (ACPI_RSDESC_SIZE) (MinimumLength + 1))
392 /* Get the ResourceSourceIndex */
394 ResourceSource->Index = AmlResourceSource[0];
396 ResourceSource->StringPtr = StringPtr;
400 * String destination pointer is not specified; Set the String
401 * pointer to the end of the current ResourceSource structure.
403 ResourceSource->StringPtr = ACPI_ADD_PTR (
404 char, ResourceSource, sizeof (ACPI_RESOURCE_SOURCE));
408 * In order for the Resource length to be a multiple of the native
409 * word, calculate the length of the string (+1 for NULL terminator)
410 * and expand to the next word multiple.
412 * Zero the entire area of the buffer.
414 TotalLength = (UINT32) strlen (
415 ACPI_CAST_PTR (char, &AmlResourceSource[1])) + 1;
417 TotalLength = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (TotalLength);
419 memset (ResourceSource->StringPtr, 0, TotalLength);
421 /* Copy the ResourceSource string to the destination */
423 ResourceSource->StringLength = AcpiRsStrcpy (
424 ResourceSource->StringPtr,
425 ACPI_CAST_PTR (char, &AmlResourceSource[1]));
427 return ((ACPI_RS_LENGTH) TotalLength);
430 /* ResourceSource is not present */
432 ResourceSource->Index = 0;
433 ResourceSource->StringLength = 0;
434 ResourceSource->StringPtr = NULL;
439 /*******************************************************************************
441 * FUNCTION: AcpiRsSetResourceSource
443 * PARAMETERS: Aml - Pointer to the raw AML descriptor
444 * MinimumLength - Minimum length of the descriptor (minus
445 * any optional fields)
446 * ResourceSource - Internal ResourceSource
449 * RETURN: Total length of the AML descriptor
451 * DESCRIPTION: Convert an optional ResourceSource from internal format to a
452 * raw AML resource descriptor
454 ******************************************************************************/
457 AcpiRsSetResourceSource (
459 ACPI_RS_LENGTH MinimumLength,
460 ACPI_RESOURCE_SOURCE *ResourceSource)
462 UINT8 *AmlResourceSource;
463 ACPI_RSDESC_SIZE DescriptorLength;
466 ACPI_FUNCTION_ENTRY ();
469 DescriptorLength = MinimumLength;
471 /* Non-zero string length indicates presence of a ResourceSource */
473 if (ResourceSource->StringLength)
475 /* Point to the end of the AML descriptor */
477 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
479 /* Copy the ResourceSourceIndex */
481 AmlResourceSource[0] = (UINT8) ResourceSource->Index;
483 /* Copy the ResourceSource string */
485 strcpy (ACPI_CAST_PTR (char, &AmlResourceSource[1]),
486 ResourceSource->StringPtr);
489 * Add the length of the string (+ 1 for null terminator) to the
490 * final descriptor length
492 DescriptorLength += ((ACPI_RSDESC_SIZE)
493 ResourceSource->StringLength + 1);
496 /* Return the new total length of the AML descriptor */
498 return (DescriptorLength);
502 /*******************************************************************************
504 * FUNCTION: AcpiRsGetPrtMethodData
506 * PARAMETERS: Node - Device node
507 * RetBuffer - Pointer to a buffer structure for the
512 * DESCRIPTION: This function is called to get the _PRT value of an object
513 * contained in an object specified by the handle passed in
515 * If the function fails an appropriate status will be returned
516 * and the contents of the callers buffer is undefined.
518 ******************************************************************************/
521 AcpiRsGetPrtMethodData (
522 ACPI_NAMESPACE_NODE *Node,
523 ACPI_BUFFER *RetBuffer)
525 ACPI_OPERAND_OBJECT *ObjDesc;
529 ACPI_FUNCTION_TRACE (RsGetPrtMethodData);
532 /* Parameters guaranteed valid by caller */
534 /* Execute the method, no parameters */
536 Status = AcpiUtEvaluateObject (
537 Node, METHOD_NAME__PRT, ACPI_BTYPE_PACKAGE, &ObjDesc);
538 if (ACPI_FAILURE (Status))
540 return_ACPI_STATUS (Status);
544 * Create a resource linked list from the byte stream buffer that comes
545 * back from the _CRS method execution.
547 Status = AcpiRsCreatePciRoutingTable (ObjDesc, RetBuffer);
549 /* On exit, we must delete the object returned by EvaluateObject */
551 AcpiUtRemoveReference (ObjDesc);
552 return_ACPI_STATUS (Status);
556 /*******************************************************************************
558 * FUNCTION: AcpiRsGetCrsMethodData
560 * PARAMETERS: Node - Device node
561 * RetBuffer - Pointer to a buffer structure for the
566 * DESCRIPTION: This function is called to get the _CRS value of an object
567 * contained in an object specified by the handle passed in
569 * If the function fails an appropriate status will be returned
570 * and the contents of the callers buffer is undefined.
572 ******************************************************************************/
575 AcpiRsGetCrsMethodData (
576 ACPI_NAMESPACE_NODE *Node,
577 ACPI_BUFFER *RetBuffer)
579 ACPI_OPERAND_OBJECT *ObjDesc;
583 ACPI_FUNCTION_TRACE (RsGetCrsMethodData);
586 /* Parameters guaranteed valid by caller */
588 /* Execute the method, no parameters */
590 Status = AcpiUtEvaluateObject (
591 Node, METHOD_NAME__CRS, ACPI_BTYPE_BUFFER, &ObjDesc);
592 if (ACPI_FAILURE (Status))
594 return_ACPI_STATUS (Status);
598 * Make the call to create a resource linked list from the
599 * byte stream buffer that comes back from the _CRS method
602 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
604 /* On exit, we must delete the object returned by evaluateObject */
606 AcpiUtRemoveReference (ObjDesc);
607 return_ACPI_STATUS (Status);
611 /*******************************************************************************
613 * FUNCTION: AcpiRsGetPrsMethodData
615 * PARAMETERS: Node - Device node
616 * RetBuffer - Pointer to a buffer structure for the
621 * DESCRIPTION: This function is called to get the _PRS value of an object
622 * contained in an object specified by the handle passed in
624 * If the function fails an appropriate status will be returned
625 * and the contents of the callers buffer is undefined.
627 ******************************************************************************/
630 AcpiRsGetPrsMethodData (
631 ACPI_NAMESPACE_NODE *Node,
632 ACPI_BUFFER *RetBuffer)
634 ACPI_OPERAND_OBJECT *ObjDesc;
638 ACPI_FUNCTION_TRACE (RsGetPrsMethodData);
641 /* Parameters guaranteed valid by caller */
643 /* Execute the method, no parameters */
645 Status = AcpiUtEvaluateObject (
646 Node, METHOD_NAME__PRS, ACPI_BTYPE_BUFFER, &ObjDesc);
647 if (ACPI_FAILURE (Status))
649 return_ACPI_STATUS (Status);
653 * Make the call to create a resource linked list from the
654 * byte stream buffer that comes back from the _CRS method
657 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
659 /* On exit, we must delete the object returned by evaluateObject */
661 AcpiUtRemoveReference (ObjDesc);
662 return_ACPI_STATUS (Status);
666 /*******************************************************************************
668 * FUNCTION: AcpiRsGetAeiMethodData
670 * PARAMETERS: Node - Device node
671 * RetBuffer - Pointer to a buffer structure for the
676 * DESCRIPTION: This function is called to get the _AEI value of an object
677 * contained in an object specified by the handle passed in
679 * If the function fails an appropriate status will be returned
680 * and the contents of the callers buffer is undefined.
682 ******************************************************************************/
685 AcpiRsGetAeiMethodData (
686 ACPI_NAMESPACE_NODE *Node,
687 ACPI_BUFFER *RetBuffer)
689 ACPI_OPERAND_OBJECT *ObjDesc;
693 ACPI_FUNCTION_TRACE (RsGetAeiMethodData);
696 /* Parameters guaranteed valid by caller */
698 /* Execute the method, no parameters */
700 Status = AcpiUtEvaluateObject (
701 Node, METHOD_NAME__AEI, ACPI_BTYPE_BUFFER, &ObjDesc);
702 if (ACPI_FAILURE (Status))
704 return_ACPI_STATUS (Status);
708 * Make the call to create a resource linked list from the
709 * byte stream buffer that comes back from the _CRS method
712 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
714 /* On exit, we must delete the object returned by evaluateObject */
716 AcpiUtRemoveReference (ObjDesc);
717 return_ACPI_STATUS (Status);
721 /*******************************************************************************
723 * FUNCTION: AcpiRsGetMethodData
725 * PARAMETERS: Handle - Handle to the containing object
726 * Path - Path to method, relative to Handle
727 * RetBuffer - Pointer to a buffer structure for the
732 * DESCRIPTION: This function is called to get the _CRS or _PRS value of an
733 * object contained in an object specified by the handle passed in
735 * If the function fails an appropriate status will be returned
736 * and the contents of the callers buffer is undefined.
738 ******************************************************************************/
741 AcpiRsGetMethodData (
744 ACPI_BUFFER *RetBuffer)
746 ACPI_OPERAND_OBJECT *ObjDesc;
750 ACPI_FUNCTION_TRACE (RsGetMethodData);
753 /* Parameters guaranteed valid by caller */
755 /* Execute the method, no parameters */
757 Status = AcpiUtEvaluateObject (
758 ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Handle),
759 Path, ACPI_BTYPE_BUFFER, &ObjDesc);
760 if (ACPI_FAILURE (Status))
762 return_ACPI_STATUS (Status);
766 * Make the call to create a resource linked list from the
767 * byte stream buffer that comes back from the method
770 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
772 /* On exit, we must delete the object returned by EvaluateObject */
774 AcpiUtRemoveReference (ObjDesc);
775 return_ACPI_STATUS (Status);
779 /*******************************************************************************
781 * FUNCTION: AcpiRsSetSrsMethodData
783 * PARAMETERS: Node - Device node
784 * InBuffer - Pointer to a buffer structure of the
789 * DESCRIPTION: This function is called to set the _SRS of an object contained
790 * in an object specified by the handle passed in
792 * If the function fails an appropriate status will be returned
793 * and the contents of the callers buffer is undefined.
795 * Note: Parameters guaranteed valid by caller
797 ******************************************************************************/
800 AcpiRsSetSrsMethodData (
801 ACPI_NAMESPACE_NODE *Node,
802 ACPI_BUFFER *InBuffer)
804 ACPI_EVALUATE_INFO *Info;
805 ACPI_OPERAND_OBJECT *Args[2];
810 ACPI_FUNCTION_TRACE (RsSetSrsMethodData);
813 /* Allocate and initialize the evaluation information block */
815 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
818 return_ACPI_STATUS (AE_NO_MEMORY);
821 Info->PrefixNode = Node;
822 Info->RelativePathname = METHOD_NAME__SRS;
823 Info->Parameters = Args;
824 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
827 * The InBuffer parameter will point to a linked list of
828 * resource parameters. It needs to be formatted into a
829 * byte stream to be sent in as an input parameter to _SRS
831 * Convert the linked list into a byte stream
833 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
834 Status = AcpiRsCreateAmlResources (InBuffer, &Buffer);
835 if (ACPI_FAILURE (Status))
840 /* Create and initialize the method parameter object */
842 Args[0] = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
846 * Must free the buffer allocated above (otherwise it is freed
849 ACPI_FREE (Buffer.Pointer);
850 Status = AE_NO_MEMORY;
854 Args[0]->Buffer.Length = (UINT32) Buffer.Length;
855 Args[0]->Buffer.Pointer = Buffer.Pointer;
856 Args[0]->Common.Flags = AOPOBJ_DATA_VALID;
859 /* Execute the method, no return value is expected */
861 Status = AcpiNsEvaluate (Info);
863 /* Clean up and return the status from AcpiNsEvaluate */
865 AcpiUtRemoveReference (Args[0]);
869 return_ACPI_STATUS (Status);