1 /*******************************************************************************
3 * Module Name: rsutils - Utilities for the resource manager
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.
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 case ACPI_RSC_MOVE_GPIO_RES:
177 case ACPI_RSC_MOVE_SERIAL_VEN:
178 case ACPI_RSC_MOVE_SERIAL_RES:
180 ACPI_MEMCPY (Destination, Source, ItemCount);
184 * 16-, 32-, and 64-bit cases must use the move macros that perform
185 * endian conversion and/or accommodate hardware that cannot perform
186 * misaligned memory transfers
188 case ACPI_RSC_MOVE16:
189 case ACPI_RSC_MOVE_GPIO_PIN:
191 ACPI_MOVE_16_TO_16 (&ACPI_CAST_PTR (UINT16, Destination)[i],
192 &ACPI_CAST_PTR (UINT16, Source)[i]);
195 case ACPI_RSC_MOVE32:
197 ACPI_MOVE_32_TO_32 (&ACPI_CAST_PTR (UINT32, Destination)[i],
198 &ACPI_CAST_PTR (UINT32, Source)[i]);
201 case ACPI_RSC_MOVE64:
203 ACPI_MOVE_64_TO_64 (&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 */
254 ACPI_MOVE_16_TO_16 (&Aml->LargeHeader.ResourceLength, &ResourceLength);
258 /* Small descriptor -- bits 2:0 of byte 0 contain the length */
260 Aml->SmallHeader.DescriptorType = (UINT8)
262 /* Clear any existing length, preserving descriptor type bits */
264 ((Aml->SmallHeader.DescriptorType & ~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 than
385 * 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 (char, ResourceSource,
404 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) ACPI_STRLEN (
415 ACPI_CAST_PTR (char, &AmlResourceSource[1])) + 1;
416 TotalLength = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (TotalLength);
418 ACPI_MEMSET (ResourceSource->StringPtr, 0, TotalLength);
420 /* Copy the ResourceSource string to the destination */
422 ResourceSource->StringLength = AcpiRsStrcpy (ResourceSource->StringPtr,
423 ACPI_CAST_PTR (char, &AmlResourceSource[1]));
425 return ((ACPI_RS_LENGTH) TotalLength);
428 /* ResourceSource is not present */
430 ResourceSource->Index = 0;
431 ResourceSource->StringLength = 0;
432 ResourceSource->StringPtr = NULL;
437 /*******************************************************************************
439 * FUNCTION: AcpiRsSetResourceSource
441 * PARAMETERS: Aml - Pointer to the raw AML descriptor
442 * MinimumLength - Minimum length of the descriptor (minus
443 * any optional fields)
444 * ResourceSource - Internal ResourceSource
447 * RETURN: Total length of the AML descriptor
449 * DESCRIPTION: Convert an optional ResourceSource from internal format to a
450 * raw AML resource descriptor
452 ******************************************************************************/
455 AcpiRsSetResourceSource (
457 ACPI_RS_LENGTH MinimumLength,
458 ACPI_RESOURCE_SOURCE *ResourceSource)
460 UINT8 *AmlResourceSource;
461 ACPI_RSDESC_SIZE DescriptorLength;
464 ACPI_FUNCTION_ENTRY ();
467 DescriptorLength = MinimumLength;
469 /* Non-zero string length indicates presence of a ResourceSource */
471 if (ResourceSource->StringLength)
473 /* Point to the end of the AML descriptor */
475 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
477 /* Copy the ResourceSourceIndex */
479 AmlResourceSource[0] = (UINT8) ResourceSource->Index;
481 /* Copy the ResourceSource string */
483 ACPI_STRCPY (ACPI_CAST_PTR (char, &AmlResourceSource[1]),
484 ResourceSource->StringPtr);
487 * Add the length of the string (+ 1 for null terminator) to the
488 * final descriptor length
490 DescriptorLength += ((ACPI_RSDESC_SIZE) ResourceSource->StringLength + 1);
493 /* Return the new total length of the AML descriptor */
495 return (DescriptorLength);
499 /*******************************************************************************
501 * FUNCTION: AcpiRsGetPrtMethodData
503 * PARAMETERS: Node - Device node
504 * RetBuffer - Pointer to a buffer structure for the
509 * DESCRIPTION: This function is called to get the _PRT value of an object
510 * contained in an object specified by the handle passed in
512 * If the function fails an appropriate status will be returned
513 * and the contents of the callers buffer is undefined.
515 ******************************************************************************/
518 AcpiRsGetPrtMethodData (
519 ACPI_NAMESPACE_NODE *Node,
520 ACPI_BUFFER *RetBuffer)
522 ACPI_OPERAND_OBJECT *ObjDesc;
526 ACPI_FUNCTION_TRACE (RsGetPrtMethodData);
529 /* Parameters guaranteed valid by caller */
531 /* Execute the method, no parameters */
533 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRT,
534 ACPI_BTYPE_PACKAGE, &ObjDesc);
535 if (ACPI_FAILURE (Status))
537 return_ACPI_STATUS (Status);
541 * Create a resource linked list from the byte stream buffer that comes
542 * back from the _CRS method execution.
544 Status = AcpiRsCreatePciRoutingTable (ObjDesc, RetBuffer);
546 /* On exit, we must delete the object returned by EvaluateObject */
548 AcpiUtRemoveReference (ObjDesc);
549 return_ACPI_STATUS (Status);
553 /*******************************************************************************
555 * FUNCTION: AcpiRsGetCrsMethodData
557 * PARAMETERS: Node - Device node
558 * RetBuffer - Pointer to a buffer structure for the
563 * DESCRIPTION: This function is called to get the _CRS value of an object
564 * contained in an object specified by the handle passed in
566 * If the function fails an appropriate status will be returned
567 * and the contents of the callers buffer is undefined.
569 ******************************************************************************/
572 AcpiRsGetCrsMethodData (
573 ACPI_NAMESPACE_NODE *Node,
574 ACPI_BUFFER *RetBuffer)
576 ACPI_OPERAND_OBJECT *ObjDesc;
580 ACPI_FUNCTION_TRACE (RsGetCrsMethodData);
583 /* Parameters guaranteed valid by caller */
585 /* Execute the method, no parameters */
587 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__CRS,
588 ACPI_BTYPE_BUFFER, &ObjDesc);
589 if (ACPI_FAILURE (Status))
591 return_ACPI_STATUS (Status);
595 * Make the call to create a resource linked list from the
596 * byte stream buffer that comes back from the _CRS method
599 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
601 /* On exit, we must delete the object returned by evaluateObject */
603 AcpiUtRemoveReference (ObjDesc);
604 return_ACPI_STATUS (Status);
608 /*******************************************************************************
610 * FUNCTION: AcpiRsGetPrsMethodData
612 * PARAMETERS: Node - Device node
613 * RetBuffer - Pointer to a buffer structure for the
618 * DESCRIPTION: This function is called to get the _PRS value of an object
619 * contained in an object specified by the handle passed in
621 * If the function fails an appropriate status will be returned
622 * and the contents of the callers buffer is undefined.
624 ******************************************************************************/
627 AcpiRsGetPrsMethodData (
628 ACPI_NAMESPACE_NODE *Node,
629 ACPI_BUFFER *RetBuffer)
631 ACPI_OPERAND_OBJECT *ObjDesc;
635 ACPI_FUNCTION_TRACE (RsGetPrsMethodData);
638 /* Parameters guaranteed valid by caller */
640 /* Execute the method, no parameters */
642 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRS,
643 ACPI_BTYPE_BUFFER, &ObjDesc);
644 if (ACPI_FAILURE (Status))
646 return_ACPI_STATUS (Status);
650 * Make the call to create a resource linked list from the
651 * byte stream buffer that comes back from the _CRS method
654 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
656 /* On exit, we must delete the object returned by evaluateObject */
658 AcpiUtRemoveReference (ObjDesc);
659 return_ACPI_STATUS (Status);
663 /*******************************************************************************
665 * FUNCTION: AcpiRsGetAeiMethodData
667 * PARAMETERS: Node - Device node
668 * RetBuffer - Pointer to a buffer structure for the
673 * DESCRIPTION: This function is called to get the _AEI value of an object
674 * contained in an object specified by the handle passed in
676 * If the function fails an appropriate status will be returned
677 * and the contents of the callers buffer is undefined.
679 ******************************************************************************/
682 AcpiRsGetAeiMethodData (
683 ACPI_NAMESPACE_NODE *Node,
684 ACPI_BUFFER *RetBuffer)
686 ACPI_OPERAND_OBJECT *ObjDesc;
690 ACPI_FUNCTION_TRACE (RsGetAeiMethodData);
693 /* Parameters guaranteed valid by caller */
695 /* Execute the method, no parameters */
697 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__AEI,
698 ACPI_BTYPE_BUFFER, &ObjDesc);
699 if (ACPI_FAILURE (Status))
701 return_ACPI_STATUS (Status);
705 * Make the call to create a resource linked list from the
706 * byte stream buffer that comes back from the _CRS method
709 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
711 /* On exit, we must delete the object returned by evaluateObject */
713 AcpiUtRemoveReference (ObjDesc);
714 return_ACPI_STATUS (Status);
718 /*******************************************************************************
720 * FUNCTION: AcpiRsGetMethodData
722 * PARAMETERS: Handle - Handle to the containing object
723 * Path - Path to method, relative to Handle
724 * RetBuffer - Pointer to a buffer structure for the
729 * DESCRIPTION: This function is called to get the _CRS or _PRS value of an
730 * object contained in an object specified by the handle passed in
732 * If the function fails an appropriate status will be returned
733 * and the contents of the callers buffer is undefined.
735 ******************************************************************************/
738 AcpiRsGetMethodData (
741 ACPI_BUFFER *RetBuffer)
743 ACPI_OPERAND_OBJECT *ObjDesc;
747 ACPI_FUNCTION_TRACE (RsGetMethodData);
750 /* Parameters guaranteed valid by caller */
752 /* Execute the method, no parameters */
754 Status = AcpiUtEvaluateObject (ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Handle),
755 Path, ACPI_BTYPE_BUFFER, &ObjDesc);
756 if (ACPI_FAILURE (Status))
758 return_ACPI_STATUS (Status);
762 * Make the call to create a resource linked list from the
763 * byte stream buffer that comes back from the method
766 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
768 /* On exit, we must delete the object returned by EvaluateObject */
770 AcpiUtRemoveReference (ObjDesc);
771 return_ACPI_STATUS (Status);
775 /*******************************************************************************
777 * FUNCTION: AcpiRsSetSrsMethodData
779 * PARAMETERS: Node - Device node
780 * InBuffer - Pointer to a buffer structure of the
785 * DESCRIPTION: This function is called to set the _SRS of an object contained
786 * in an object specified by the handle passed in
788 * If the function fails an appropriate status will be returned
789 * and the contents of the callers buffer is undefined.
791 * Note: Parameters guaranteed valid by caller
793 ******************************************************************************/
796 AcpiRsSetSrsMethodData (
797 ACPI_NAMESPACE_NODE *Node,
798 ACPI_BUFFER *InBuffer)
800 ACPI_EVALUATE_INFO *Info;
801 ACPI_OPERAND_OBJECT *Args[2];
806 ACPI_FUNCTION_TRACE (RsSetSrsMethodData);
809 /* Allocate and initialize the evaluation information block */
811 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
814 return_ACPI_STATUS (AE_NO_MEMORY);
817 Info->PrefixNode = Node;
818 Info->RelativePathname = METHOD_NAME__SRS;
819 Info->Parameters = Args;
820 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
823 * The InBuffer parameter will point to a linked list of
824 * resource parameters. It needs to be formatted into a
825 * byte stream to be sent in as an input parameter to _SRS
827 * Convert the linked list into a byte stream
829 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
830 Status = AcpiRsCreateAmlResources (InBuffer, &Buffer);
831 if (ACPI_FAILURE (Status))
836 /* Create and initialize the method parameter object */
838 Args[0] = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
842 * Must free the buffer allocated above (otherwise it is freed
845 ACPI_FREE (Buffer.Pointer);
846 Status = AE_NO_MEMORY;
850 Args[0]->Buffer.Length = (UINT32) Buffer.Length;
851 Args[0]->Buffer.Pointer = Buffer.Pointer;
852 Args[0]->Common.Flags = AOPOBJ_DATA_VALID;
855 /* Execute the method, no return value is expected */
857 Status = AcpiNsEvaluate (Info);
859 /* Clean up and return the status from AcpiNsEvaluate */
861 AcpiUtRemoveReference (Args[0]);
865 return_ACPI_STATUS (Status);