1 /*******************************************************************************
3 * Module Name: rsutils - Utilities for the resource manager
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
52 #define _COMPONENT ACPI_RESOURCES
53 ACPI_MODULE_NAME ("rsutils")
56 /*******************************************************************************
58 * FUNCTION: AcpiRsDecodeBitmask
60 * PARAMETERS: Mask - Bitmask to decode
61 * List - Where the converted list is returned
63 * RETURN: Count of bits set (length of list)
65 * DESCRIPTION: Convert a bit mask into a list of values
67 ******************************************************************************/
78 ACPI_FUNCTION_ENTRY ();
81 /* Decode the mask bits */
83 for (i = 0, BitCount = 0; Mask; i++)
98 /*******************************************************************************
100 * FUNCTION: AcpiRsEncodeBitmask
102 * PARAMETERS: List - List of values to encode
103 * Count - Length of list
105 * RETURN: Encoded bitmask
107 * DESCRIPTION: Convert a list of values to an encoded bitmask
109 ******************************************************************************/
112 AcpiRsEncodeBitmask (
120 ACPI_FUNCTION_ENTRY ();
123 /* Encode the list into a single bitmask */
125 for (i = 0, Mask = 0; i < Count; i++)
127 Mask |= (0x1 << List[i]);
134 /*******************************************************************************
136 * FUNCTION: AcpiRsMoveData
138 * PARAMETERS: Destination - Pointer to the destination descriptor
139 * Source - Pointer to the source descriptor
140 * ItemCount - How many items to move
141 * MoveType - Byte width
145 * DESCRIPTION: Move multiple data items from one descriptor to another. Handles
146 * alignment issues and endian issues if necessary, as configured
147 * via the ACPI_MOVE_* macros. (This is why a memcpy is not used)
149 ******************************************************************************/
161 ACPI_FUNCTION_ENTRY ();
164 /* One move per item */
166 for (i = 0; i < ItemCount; i++)
171 * For the 8-bit case, we can perform the move all at once
172 * since there are no alignment or endian issues
175 case ACPI_RSC_MOVE_GPIO_RES:
176 case ACPI_RSC_MOVE_SERIAL_VEN:
177 case ACPI_RSC_MOVE_SERIAL_RES:
179 ACPI_MEMCPY (Destination, Source, ItemCount);
183 * 16-, 32-, and 64-bit cases must use the move macros that perform
184 * endian conversion and/or accommodate hardware that cannot perform
185 * misaligned memory transfers
187 case ACPI_RSC_MOVE16:
188 case ACPI_RSC_MOVE_GPIO_PIN:
190 ACPI_MOVE_16_TO_16 (&ACPI_CAST_PTR (UINT16, Destination)[i],
191 &ACPI_CAST_PTR (UINT16, Source)[i]);
194 case ACPI_RSC_MOVE32:
196 ACPI_MOVE_32_TO_32 (&ACPI_CAST_PTR (UINT32, Destination)[i],
197 &ACPI_CAST_PTR (UINT32, Source)[i]);
200 case ACPI_RSC_MOVE64:
202 ACPI_MOVE_64_TO_64 (&ACPI_CAST_PTR (UINT64, Destination)[i],
203 &ACPI_CAST_PTR (UINT64, Source)[i]);
214 /*******************************************************************************
216 * FUNCTION: AcpiRsSetResourceLength
218 * PARAMETERS: TotalLength - Length of the AML descriptor, including
219 * the header and length fields.
220 * Aml - Pointer to the raw AML descriptor
224 * DESCRIPTION: Set the ResourceLength field of an AML
225 * resource descriptor, both Large and Small descriptors are
226 * supported automatically. Note: Descriptor Type field must
229 ******************************************************************************/
232 AcpiRsSetResourceLength (
233 ACPI_RSDESC_SIZE TotalLength,
236 ACPI_RS_LENGTH ResourceLength;
239 ACPI_FUNCTION_ENTRY ();
242 /* Length is the total descriptor length minus the header length */
244 ResourceLength = (ACPI_RS_LENGTH)
245 (TotalLength - AcpiUtGetResourceHeaderLength (Aml));
247 /* Length is stored differently for large and small descriptors */
249 if (Aml->SmallHeader.DescriptorType & ACPI_RESOURCE_NAME_LARGE)
251 /* Large descriptor -- bytes 1-2 contain the 16-bit length */
253 ACPI_MOVE_16_TO_16 (&Aml->LargeHeader.ResourceLength, &ResourceLength);
257 /* Small descriptor -- bits 2:0 of byte 0 contain the length */
259 Aml->SmallHeader.DescriptorType = (UINT8)
261 /* Clear any existing length, preserving descriptor type bits */
263 ((Aml->SmallHeader.DescriptorType & ~ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK)
270 /*******************************************************************************
272 * FUNCTION: AcpiRsSetResourceHeader
274 * PARAMETERS: DescriptorType - Byte to be inserted as the type
275 * TotalLength - Length of the AML descriptor, including
276 * the header and length fields.
277 * Aml - Pointer to the raw AML descriptor
281 * DESCRIPTION: Set the DescriptorType and ResourceLength fields of an AML
282 * resource descriptor, both Large and Small descriptors are
283 * supported automatically
285 ******************************************************************************/
288 AcpiRsSetResourceHeader (
289 UINT8 DescriptorType,
290 ACPI_RSDESC_SIZE TotalLength,
293 ACPI_FUNCTION_ENTRY ();
296 /* Set the Resource Type */
298 Aml->SmallHeader.DescriptorType = DescriptorType;
300 /* Set the Resource Length */
302 AcpiRsSetResourceLength (TotalLength, Aml);
306 /*******************************************************************************
308 * FUNCTION: AcpiRsStrcpy
310 * PARAMETERS: Destination - Pointer to the destination string
311 * Source - Pointer to the source string
313 * RETURN: String length, including NULL terminator
315 * DESCRIPTION: Local string copy that returns the string length, saving a
316 * strcpy followed by a strlen.
318 ******************************************************************************/
328 ACPI_FUNCTION_ENTRY ();
331 for (i = 0; Source[i]; i++)
333 Destination[i] = Source[i];
338 /* Return string length including the NULL terminator */
340 return ((UINT16) (i + 1));
344 /*******************************************************************************
346 * FUNCTION: AcpiRsGetResourceSource
348 * PARAMETERS: ResourceLength - Length field of the descriptor
349 * MinimumLength - Minimum length of the descriptor (minus
350 * any optional fields)
351 * ResourceSource - Where the ResourceSource is returned
352 * Aml - Pointer to the raw AML descriptor
353 * StringPtr - (optional) where to store the actual
354 * ResourceSource string
356 * RETURN: Length of the string plus NULL terminator, rounded up to native
359 * DESCRIPTION: Copy the optional ResourceSource data from a raw AML descriptor
360 * to an internal resource descriptor
362 ******************************************************************************/
365 AcpiRsGetResourceSource (
366 ACPI_RS_LENGTH ResourceLength,
367 ACPI_RS_LENGTH MinimumLength,
368 ACPI_RESOURCE_SOURCE *ResourceSource,
372 ACPI_RSDESC_SIZE TotalLength;
373 UINT8 *AmlResourceSource;
376 ACPI_FUNCTION_ENTRY ();
379 TotalLength = ResourceLength + sizeof (AML_RESOURCE_LARGE_HEADER);
380 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
383 * ResourceSource is present if the length of the descriptor is longer than
384 * the minimum length.
386 * Note: Some resource descriptors will have an additional null, so
387 * we add 1 to the minimum length.
389 if (TotalLength > (ACPI_RSDESC_SIZE) (MinimumLength + 1))
391 /* Get the ResourceSourceIndex */
393 ResourceSource->Index = AmlResourceSource[0];
395 ResourceSource->StringPtr = StringPtr;
399 * String destination pointer is not specified; Set the String
400 * pointer to the end of the current ResourceSource structure.
402 ResourceSource->StringPtr = ACPI_ADD_PTR (char, ResourceSource,
403 sizeof (ACPI_RESOURCE_SOURCE));
407 * In order for the Resource length to be a multiple of the native
408 * word, calculate the length of the string (+1 for NULL terminator)
409 * and expand to the next word multiple.
411 * Zero the entire area of the buffer.
413 TotalLength = (UINT32) ACPI_STRLEN (
414 ACPI_CAST_PTR (char, &AmlResourceSource[1])) + 1;
415 TotalLength = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (TotalLength);
417 ACPI_MEMSET (ResourceSource->StringPtr, 0, TotalLength);
419 /* Copy the ResourceSource string to the destination */
421 ResourceSource->StringLength = AcpiRsStrcpy (ResourceSource->StringPtr,
422 ACPI_CAST_PTR (char, &AmlResourceSource[1]));
424 return ((ACPI_RS_LENGTH) TotalLength);
427 /* ResourceSource is not present */
429 ResourceSource->Index = 0;
430 ResourceSource->StringLength = 0;
431 ResourceSource->StringPtr = NULL;
436 /*******************************************************************************
438 * FUNCTION: AcpiRsSetResourceSource
440 * PARAMETERS: Aml - Pointer to the raw AML descriptor
441 * MinimumLength - Minimum length of the descriptor (minus
442 * any optional fields)
443 * ResourceSource - Internal ResourceSource
446 * RETURN: Total length of the AML descriptor
448 * DESCRIPTION: Convert an optional ResourceSource from internal format to a
449 * raw AML resource descriptor
451 ******************************************************************************/
454 AcpiRsSetResourceSource (
456 ACPI_RS_LENGTH MinimumLength,
457 ACPI_RESOURCE_SOURCE *ResourceSource)
459 UINT8 *AmlResourceSource;
460 ACPI_RSDESC_SIZE DescriptorLength;
463 ACPI_FUNCTION_ENTRY ();
466 DescriptorLength = MinimumLength;
468 /* Non-zero string length indicates presence of a ResourceSource */
470 if (ResourceSource->StringLength)
472 /* Point to the end of the AML descriptor */
474 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
476 /* Copy the ResourceSourceIndex */
478 AmlResourceSource[0] = (UINT8) ResourceSource->Index;
480 /* Copy the ResourceSource string */
482 ACPI_STRCPY (ACPI_CAST_PTR (char, &AmlResourceSource[1]),
483 ResourceSource->StringPtr);
486 * Add the length of the string (+ 1 for null terminator) to the
487 * final descriptor length
489 DescriptorLength += ((ACPI_RSDESC_SIZE) ResourceSource->StringLength + 1);
492 /* Return the new total length of the AML descriptor */
494 return (DescriptorLength);
498 /*******************************************************************************
500 * FUNCTION: AcpiRsGetPrtMethodData
502 * PARAMETERS: Node - Device node
503 * RetBuffer - Pointer to a buffer structure for the
508 * DESCRIPTION: This function is called to get the _PRT value of an object
509 * contained in an object specified by the handle passed in
511 * If the function fails an appropriate status will be returned
512 * and the contents of the callers buffer is undefined.
514 ******************************************************************************/
517 AcpiRsGetPrtMethodData (
518 ACPI_NAMESPACE_NODE *Node,
519 ACPI_BUFFER *RetBuffer)
521 ACPI_OPERAND_OBJECT *ObjDesc;
525 ACPI_FUNCTION_TRACE (RsGetPrtMethodData);
528 /* Parameters guaranteed valid by caller */
530 /* Execute the method, no parameters */
532 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRT,
533 ACPI_BTYPE_PACKAGE, &ObjDesc);
534 if (ACPI_FAILURE (Status))
536 return_ACPI_STATUS (Status);
540 * Create a resource linked list from the byte stream buffer that comes
541 * back from the _CRS method execution.
543 Status = AcpiRsCreatePciRoutingTable (ObjDesc, RetBuffer);
545 /* On exit, we must delete the object returned by EvaluateObject */
547 AcpiUtRemoveReference (ObjDesc);
548 return_ACPI_STATUS (Status);
552 /*******************************************************************************
554 * FUNCTION: AcpiRsGetCrsMethodData
556 * PARAMETERS: Node - Device node
557 * RetBuffer - Pointer to a buffer structure for the
562 * DESCRIPTION: This function is called to get the _CRS value of an object
563 * contained in an object specified by the handle passed in
565 * If the function fails an appropriate status will be returned
566 * and the contents of the callers buffer is undefined.
568 ******************************************************************************/
571 AcpiRsGetCrsMethodData (
572 ACPI_NAMESPACE_NODE *Node,
573 ACPI_BUFFER *RetBuffer)
575 ACPI_OPERAND_OBJECT *ObjDesc;
579 ACPI_FUNCTION_TRACE (RsGetCrsMethodData);
582 /* Parameters guaranteed valid by caller */
584 /* Execute the method, no parameters */
586 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__CRS,
587 ACPI_BTYPE_BUFFER, &ObjDesc);
588 if (ACPI_FAILURE (Status))
590 return_ACPI_STATUS (Status);
594 * Make the call to create a resource linked list from the
595 * byte stream buffer that comes back from the _CRS method
598 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
600 /* On exit, we must delete the object returned by evaluateObject */
602 AcpiUtRemoveReference (ObjDesc);
603 return_ACPI_STATUS (Status);
607 /*******************************************************************************
609 * FUNCTION: AcpiRsGetPrsMethodData
611 * PARAMETERS: Node - Device node
612 * RetBuffer - Pointer to a buffer structure for the
617 * DESCRIPTION: This function is called to get the _PRS value of an object
618 * contained in an object specified by the handle passed in
620 * If the function fails an appropriate status will be returned
621 * and the contents of the callers buffer is undefined.
623 ******************************************************************************/
626 AcpiRsGetPrsMethodData (
627 ACPI_NAMESPACE_NODE *Node,
628 ACPI_BUFFER *RetBuffer)
630 ACPI_OPERAND_OBJECT *ObjDesc;
634 ACPI_FUNCTION_TRACE (RsGetPrsMethodData);
637 /* Parameters guaranteed valid by caller */
639 /* Execute the method, no parameters */
641 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRS,
642 ACPI_BTYPE_BUFFER, &ObjDesc);
643 if (ACPI_FAILURE (Status))
645 return_ACPI_STATUS (Status);
649 * Make the call to create a resource linked list from the
650 * byte stream buffer that comes back from the _CRS method
653 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
655 /* On exit, we must delete the object returned by evaluateObject */
657 AcpiUtRemoveReference (ObjDesc);
658 return_ACPI_STATUS (Status);
662 /*******************************************************************************
664 * FUNCTION: AcpiRsGetAeiMethodData
666 * PARAMETERS: Node - Device node
667 * RetBuffer - Pointer to a buffer structure for the
672 * DESCRIPTION: This function is called to get the _AEI value of an object
673 * contained in an object specified by the handle passed in
675 * If the function fails an appropriate status will be returned
676 * and the contents of the callers buffer is undefined.
678 ******************************************************************************/
681 AcpiRsGetAeiMethodData (
682 ACPI_NAMESPACE_NODE *Node,
683 ACPI_BUFFER *RetBuffer)
685 ACPI_OPERAND_OBJECT *ObjDesc;
689 ACPI_FUNCTION_TRACE (RsGetAeiMethodData);
692 /* Parameters guaranteed valid by caller */
694 /* Execute the method, no parameters */
696 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__AEI,
697 ACPI_BTYPE_BUFFER, &ObjDesc);
698 if (ACPI_FAILURE (Status))
700 return_ACPI_STATUS (Status);
704 * Make the call to create a resource linked list from the
705 * byte stream buffer that comes back from the _CRS method
708 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
710 /* On exit, we must delete the object returned by evaluateObject */
712 AcpiUtRemoveReference (ObjDesc);
713 return_ACPI_STATUS (Status);
717 /*******************************************************************************
719 * FUNCTION: AcpiRsGetMethodData
721 * PARAMETERS: Handle - Handle to the containing object
722 * Path - Path to method, relative to Handle
723 * RetBuffer - Pointer to a buffer structure for the
728 * DESCRIPTION: This function is called to get the _CRS or _PRS value of an
729 * object contained in an object specified by the handle passed in
731 * If the function fails an appropriate status will be returned
732 * and the contents of the callers buffer is undefined.
734 ******************************************************************************/
737 AcpiRsGetMethodData (
740 ACPI_BUFFER *RetBuffer)
742 ACPI_OPERAND_OBJECT *ObjDesc;
746 ACPI_FUNCTION_TRACE (RsGetMethodData);
749 /* Parameters guaranteed valid by caller */
751 /* Execute the method, no parameters */
753 Status = AcpiUtEvaluateObject (ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Handle),
754 Path, ACPI_BTYPE_BUFFER, &ObjDesc);
755 if (ACPI_FAILURE (Status))
757 return_ACPI_STATUS (Status);
761 * Make the call to create a resource linked list from the
762 * byte stream buffer that comes back from the method
765 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
767 /* On exit, we must delete the object returned by EvaluateObject */
769 AcpiUtRemoveReference (ObjDesc);
770 return_ACPI_STATUS (Status);
774 /*******************************************************************************
776 * FUNCTION: AcpiRsSetSrsMethodData
778 * PARAMETERS: Node - Device node
779 * InBuffer - Pointer to a buffer structure of the
784 * DESCRIPTION: This function is called to set the _SRS of an object contained
785 * in an object specified by the handle passed in
787 * If the function fails an appropriate status will be returned
788 * and the contents of the callers buffer is undefined.
790 * Note: Parameters guaranteed valid by caller
792 ******************************************************************************/
795 AcpiRsSetSrsMethodData (
796 ACPI_NAMESPACE_NODE *Node,
797 ACPI_BUFFER *InBuffer)
799 ACPI_EVALUATE_INFO *Info;
800 ACPI_OPERAND_OBJECT *Args[2];
805 ACPI_FUNCTION_TRACE (RsSetSrsMethodData);
808 /* Allocate and initialize the evaluation information block */
810 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
813 return_ACPI_STATUS (AE_NO_MEMORY);
816 Info->PrefixNode = Node;
817 Info->RelativePathname = METHOD_NAME__SRS;
818 Info->Parameters = Args;
819 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
822 * The InBuffer parameter will point to a linked list of
823 * resource parameters. It needs to be formatted into a
824 * byte stream to be sent in as an input parameter to _SRS
826 * Convert the linked list into a byte stream
828 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
829 Status = AcpiRsCreateAmlResources (InBuffer, &Buffer);
830 if (ACPI_FAILURE (Status))
835 /* Create and initialize the method parameter object */
837 Args[0] = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
841 * Must free the buffer allocated above (otherwise it is freed
844 ACPI_FREE (Buffer.Pointer);
845 Status = AE_NO_MEMORY;
849 Args[0]->Buffer.Length = (UINT32) Buffer.Length;
850 Args[0]->Buffer.Pointer = Buffer.Pointer;
851 Args[0]->Common.Flags = AOPOBJ_DATA_VALID;
854 /* Execute the method, no return value is expected */
856 Status = AcpiNsEvaluate (Info);
858 /* Clean up and return the status from AcpiNsEvaluate */
860 AcpiUtRemoveReference (Args[0]);
864 return_ACPI_STATUS (Status);