1 /*******************************************************************************
3 * Module Name: rsutils - Utilities for the resource manager
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acnamesp.h>
47 #include <contrib/dev/acpica/include/acresrc.h>
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 ACPI_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:
188 ACPI_MOVE_16_TO_16 (&ACPI_CAST_PTR (UINT16, Destination)[i],
189 &ACPI_CAST_PTR (UINT16, Source)[i]);
192 case ACPI_RSC_MOVE32:
194 ACPI_MOVE_32_TO_32 (&ACPI_CAST_PTR (UINT32, Destination)[i],
195 &ACPI_CAST_PTR (UINT32, Source)[i]);
198 case ACPI_RSC_MOVE64:
200 ACPI_MOVE_64_TO_64 (&ACPI_CAST_PTR (UINT64, Destination)[i],
201 &ACPI_CAST_PTR (UINT64, Source)[i]);
212 /*******************************************************************************
214 * FUNCTION: AcpiRsSetResourceLength
216 * PARAMETERS: TotalLength - Length of the AML descriptor, including
217 * the header and length fields.
218 * Aml - Pointer to the raw AML descriptor
222 * DESCRIPTION: Set the ResourceLength field of an AML
223 * resource descriptor, both Large and Small descriptors are
224 * supported automatically. Note: Descriptor Type field must
227 ******************************************************************************/
230 AcpiRsSetResourceLength (
231 ACPI_RSDESC_SIZE TotalLength,
234 ACPI_RS_LENGTH ResourceLength;
237 ACPI_FUNCTION_ENTRY ();
240 /* Length is the total descriptor length minus the header length */
242 ResourceLength = (ACPI_RS_LENGTH)
243 (TotalLength - AcpiUtGetResourceHeaderLength (Aml));
245 /* Length is stored differently for large and small descriptors */
247 if (Aml->SmallHeader.DescriptorType & ACPI_RESOURCE_NAME_LARGE)
249 /* Large descriptor -- bytes 1-2 contain the 16-bit length */
251 ACPI_MOVE_16_TO_16 (&Aml->LargeHeader.ResourceLength, &ResourceLength);
255 /* Small descriptor -- bits 2:0 of byte 0 contain the length */
257 Aml->SmallHeader.DescriptorType = (UINT8)
259 /* Clear any existing length, preserving descriptor type bits */
261 ((Aml->SmallHeader.DescriptorType & ~ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK)
268 /*******************************************************************************
270 * FUNCTION: AcpiRsSetResourceHeader
272 * PARAMETERS: DescriptorType - Byte to be inserted as the type
273 * TotalLength - Length of the AML descriptor, including
274 * the header and length fields.
275 * Aml - Pointer to the raw AML descriptor
279 * DESCRIPTION: Set the DescriptorType and ResourceLength fields of an AML
280 * resource descriptor, both Large and Small descriptors are
281 * supported automatically
283 ******************************************************************************/
286 AcpiRsSetResourceHeader (
287 UINT8 DescriptorType,
288 ACPI_RSDESC_SIZE TotalLength,
291 ACPI_FUNCTION_ENTRY ();
294 /* Set the Resource Type */
296 Aml->SmallHeader.DescriptorType = DescriptorType;
298 /* Set the Resource Length */
300 AcpiRsSetResourceLength (TotalLength, Aml);
304 /*******************************************************************************
306 * FUNCTION: AcpiRsStrcpy
308 * PARAMETERS: Destination - Pointer to the destination string
309 * Source - Pointer to the source string
311 * RETURN: String length, including NULL terminator
313 * DESCRIPTION: Local string copy that returns the string length, saving a
314 * strcpy followed by a strlen.
316 ******************************************************************************/
326 ACPI_FUNCTION_ENTRY ();
329 for (i = 0; Source[i]; i++)
331 Destination[i] = Source[i];
336 /* Return string length including the NULL terminator */
338 return ((UINT16) (i + 1));
342 /*******************************************************************************
344 * FUNCTION: AcpiRsGetResourceSource
346 * PARAMETERS: ResourceLength - Length field of the descriptor
347 * MinimumLength - Minimum length of the descriptor (minus
348 * any optional fields)
349 * ResourceSource - Where the ResourceSource is returned
350 * Aml - Pointer to the raw AML descriptor
351 * StringPtr - (optional) where to store the actual
352 * ResourceSource string
354 * RETURN: Length of the string plus NULL terminator, rounded up to native
357 * DESCRIPTION: Copy the optional ResourceSource data from a raw AML descriptor
358 * to an internal resource descriptor
360 ******************************************************************************/
363 AcpiRsGetResourceSource (
364 ACPI_RS_LENGTH ResourceLength,
365 ACPI_RS_LENGTH MinimumLength,
366 ACPI_RESOURCE_SOURCE *ResourceSource,
370 ACPI_RSDESC_SIZE TotalLength;
371 UINT8 *AmlResourceSource;
374 ACPI_FUNCTION_ENTRY ();
377 TotalLength = ResourceLength + sizeof (AML_RESOURCE_LARGE_HEADER);
378 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
381 * ResourceSource is present if the length of the descriptor is longer than
382 * the minimum length.
384 * Note: Some resource descriptors will have an additional null, so
385 * we add 1 to the minimum length.
387 if (TotalLength > (ACPI_RSDESC_SIZE) (MinimumLength + 1))
389 /* Get the ResourceSourceIndex */
391 ResourceSource->Index = AmlResourceSource[0];
393 ResourceSource->StringPtr = StringPtr;
397 * String destination pointer is not specified; Set the String
398 * pointer to the end of the current ResourceSource structure.
400 ResourceSource->StringPtr = ACPI_ADD_PTR (char, ResourceSource,
401 sizeof (ACPI_RESOURCE_SOURCE));
405 * In order for the Resource length to be a multiple of the native
406 * word, calculate the length of the string (+1 for NULL terminator)
407 * and expand to the next word multiple.
409 * Zero the entire area of the buffer.
411 TotalLength = (UINT32) ACPI_STRLEN (
412 ACPI_CAST_PTR (char, &AmlResourceSource[1])) + 1;
413 TotalLength = (UINT32) ACPI_ROUND_UP_TO_NATIVE_WORD (TotalLength);
415 ACPI_MEMSET (ResourceSource->StringPtr, 0, TotalLength);
417 /* Copy the ResourceSource string to the destination */
419 ResourceSource->StringLength = AcpiRsStrcpy (ResourceSource->StringPtr,
420 ACPI_CAST_PTR (char, &AmlResourceSource[1]));
422 return ((ACPI_RS_LENGTH) TotalLength);
425 /* ResourceSource is not present */
427 ResourceSource->Index = 0;
428 ResourceSource->StringLength = 0;
429 ResourceSource->StringPtr = NULL;
434 /*******************************************************************************
436 * FUNCTION: AcpiRsSetResourceSource
438 * PARAMETERS: Aml - Pointer to the raw AML descriptor
439 * MinimumLength - Minimum length of the descriptor (minus
440 * any optional fields)
441 * ResourceSource - Internal ResourceSource
444 * RETURN: Total length of the AML descriptor
446 * DESCRIPTION: Convert an optional ResourceSource from internal format to a
447 * raw AML resource descriptor
449 ******************************************************************************/
452 AcpiRsSetResourceSource (
454 ACPI_RS_LENGTH MinimumLength,
455 ACPI_RESOURCE_SOURCE *ResourceSource)
457 UINT8 *AmlResourceSource;
458 ACPI_RSDESC_SIZE DescriptorLength;
461 ACPI_FUNCTION_ENTRY ();
464 DescriptorLength = MinimumLength;
466 /* Non-zero string length indicates presence of a ResourceSource */
468 if (ResourceSource->StringLength)
470 /* Point to the end of the AML descriptor */
472 AmlResourceSource = ACPI_ADD_PTR (UINT8, Aml, MinimumLength);
474 /* Copy the ResourceSourceIndex */
476 AmlResourceSource[0] = (UINT8) ResourceSource->Index;
478 /* Copy the ResourceSource string */
480 ACPI_STRCPY (ACPI_CAST_PTR (char, &AmlResourceSource[1]),
481 ResourceSource->StringPtr);
484 * Add the length of the string (+ 1 for null terminator) to the
485 * final descriptor length
487 DescriptorLength += ((ACPI_RSDESC_SIZE) ResourceSource->StringLength + 1);
490 /* Return the new total length of the AML descriptor */
492 return (DescriptorLength);
496 /*******************************************************************************
498 * FUNCTION: AcpiRsGetPrtMethodData
500 * PARAMETERS: Node - Device node
501 * RetBuffer - Pointer to a buffer structure for the
506 * DESCRIPTION: This function is called to get the _PRT value of an object
507 * contained in an object specified by the handle passed in
509 * If the function fails an appropriate status will be returned
510 * and the contents of the callers buffer is undefined.
512 ******************************************************************************/
515 AcpiRsGetPrtMethodData (
516 ACPI_NAMESPACE_NODE *Node,
517 ACPI_BUFFER *RetBuffer)
519 ACPI_OPERAND_OBJECT *ObjDesc;
523 ACPI_FUNCTION_TRACE (RsGetPrtMethodData);
526 /* Parameters guaranteed valid by caller */
528 /* Execute the method, no parameters */
530 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRT,
531 ACPI_BTYPE_PACKAGE, &ObjDesc);
532 if (ACPI_FAILURE (Status))
534 return_ACPI_STATUS (Status);
538 * Create a resource linked list from the byte stream buffer that comes
539 * back from the _CRS method execution.
541 Status = AcpiRsCreatePciRoutingTable (ObjDesc, RetBuffer);
543 /* On exit, we must delete the object returned by EvaluateObject */
545 AcpiUtRemoveReference (ObjDesc);
546 return_ACPI_STATUS (Status);
550 /*******************************************************************************
552 * FUNCTION: AcpiRsGetCrsMethodData
554 * PARAMETERS: Node - Device node
555 * RetBuffer - Pointer to a buffer structure for the
560 * DESCRIPTION: This function is called to get the _CRS value of an object
561 * contained in an object specified by the handle passed in
563 * If the function fails an appropriate status will be returned
564 * and the contents of the callers buffer is undefined.
566 ******************************************************************************/
569 AcpiRsGetCrsMethodData (
570 ACPI_NAMESPACE_NODE *Node,
571 ACPI_BUFFER *RetBuffer)
573 ACPI_OPERAND_OBJECT *ObjDesc;
577 ACPI_FUNCTION_TRACE (RsGetCrsMethodData);
580 /* Parameters guaranteed valid by caller */
582 /* Execute the method, no parameters */
584 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__CRS,
585 ACPI_BTYPE_BUFFER, &ObjDesc);
586 if (ACPI_FAILURE (Status))
588 return_ACPI_STATUS (Status);
592 * Make the call to create a resource linked list from the
593 * byte stream buffer that comes back from the _CRS method
596 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
598 /* On exit, we must delete the object returned by evaluateObject */
600 AcpiUtRemoveReference (ObjDesc);
601 return_ACPI_STATUS (Status);
605 /*******************************************************************************
607 * FUNCTION: AcpiRsGetPrsMethodData
609 * PARAMETERS: Node - Device node
610 * RetBuffer - Pointer to a buffer structure for the
615 * DESCRIPTION: This function is called to get the _PRS value of an object
616 * contained in an object specified by the handle passed in
618 * If the function fails an appropriate status will be returned
619 * and the contents of the callers buffer is undefined.
621 ******************************************************************************/
624 AcpiRsGetPrsMethodData (
625 ACPI_NAMESPACE_NODE *Node,
626 ACPI_BUFFER *RetBuffer)
628 ACPI_OPERAND_OBJECT *ObjDesc;
632 ACPI_FUNCTION_TRACE (RsGetPrsMethodData);
635 /* Parameters guaranteed valid by caller */
637 /* Execute the method, no parameters */
639 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__PRS,
640 ACPI_BTYPE_BUFFER, &ObjDesc);
641 if (ACPI_FAILURE (Status))
643 return_ACPI_STATUS (Status);
647 * Make the call to create a resource linked list from the
648 * byte stream buffer that comes back from the _CRS method
651 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
653 /* On exit, we must delete the object returned by evaluateObject */
655 AcpiUtRemoveReference (ObjDesc);
656 return_ACPI_STATUS (Status);
660 /*******************************************************************************
662 * FUNCTION: AcpiRsGetAeiMethodData
664 * PARAMETERS: Node - Device node
665 * RetBuffer - Pointer to a buffer structure for the
670 * DESCRIPTION: This function is called to get the _AEI value of an object
671 * contained in an object specified by the handle passed in
673 * If the function fails an appropriate status will be returned
674 * and the contents of the callers buffer is undefined.
676 ******************************************************************************/
679 AcpiRsGetAeiMethodData (
680 ACPI_NAMESPACE_NODE *Node,
681 ACPI_BUFFER *RetBuffer)
683 ACPI_OPERAND_OBJECT *ObjDesc;
687 ACPI_FUNCTION_TRACE (RsGetAeiMethodData);
690 /* Parameters guaranteed valid by caller */
692 /* Execute the method, no parameters */
694 Status = AcpiUtEvaluateObject (Node, METHOD_NAME__AEI,
695 ACPI_BTYPE_BUFFER, &ObjDesc);
696 if (ACPI_FAILURE (Status))
698 return_ACPI_STATUS (Status);
702 * Make the call to create a resource linked list from the
703 * byte stream buffer that comes back from the _CRS method
706 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
708 /* On exit, we must delete the object returned by evaluateObject */
710 AcpiUtRemoveReference (ObjDesc);
711 return_ACPI_STATUS (Status);
715 /*******************************************************************************
717 * FUNCTION: AcpiRsGetMethodData
719 * PARAMETERS: Handle - Handle to the containing object
720 * Path - Path to method, relative to Handle
721 * RetBuffer - Pointer to a buffer structure for the
726 * DESCRIPTION: This function is called to get the _CRS or _PRS value of an
727 * object contained in an object specified by the handle passed in
729 * If the function fails an appropriate status will be returned
730 * and the contents of the callers buffer is undefined.
732 ******************************************************************************/
735 AcpiRsGetMethodData (
738 ACPI_BUFFER *RetBuffer)
740 ACPI_OPERAND_OBJECT *ObjDesc;
744 ACPI_FUNCTION_TRACE (RsGetMethodData);
747 /* Parameters guaranteed valid by caller */
749 /* Execute the method, no parameters */
751 Status = AcpiUtEvaluateObject (ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Handle),
752 Path, ACPI_BTYPE_BUFFER, &ObjDesc);
753 if (ACPI_FAILURE (Status))
755 return_ACPI_STATUS (Status);
759 * Make the call to create a resource linked list from the
760 * byte stream buffer that comes back from the method
763 Status = AcpiRsCreateResourceList (ObjDesc, RetBuffer);
765 /* On exit, we must delete the object returned by EvaluateObject */
767 AcpiUtRemoveReference (ObjDesc);
768 return_ACPI_STATUS (Status);
772 /*******************************************************************************
774 * FUNCTION: AcpiRsSetSrsMethodData
776 * PARAMETERS: Node - Device node
777 * InBuffer - Pointer to a buffer structure of the
782 * DESCRIPTION: This function is called to set the _SRS of an object contained
783 * in an object specified by the handle passed in
785 * If the function fails an appropriate status will be returned
786 * and the contents of the callers buffer is undefined.
788 * Note: Parameters guaranteed valid by caller
790 ******************************************************************************/
793 AcpiRsSetSrsMethodData (
794 ACPI_NAMESPACE_NODE *Node,
795 ACPI_BUFFER *InBuffer)
797 ACPI_EVALUATE_INFO *Info;
798 ACPI_OPERAND_OBJECT *Args[2];
803 ACPI_FUNCTION_TRACE (RsSetSrsMethodData);
806 /* Allocate and initialize the evaluation information block */
808 Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
811 return_ACPI_STATUS (AE_NO_MEMORY);
814 Info->PrefixNode = Node;
815 Info->RelativePathname = METHOD_NAME__SRS;
816 Info->Parameters = Args;
817 Info->Flags = ACPI_IGNORE_RETURN_VALUE;
820 * The InBuffer parameter will point to a linked list of
821 * resource parameters. It needs to be formatted into a
822 * byte stream to be sent in as an input parameter to _SRS
824 * Convert the linked list into a byte stream
826 Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
827 Status = AcpiRsCreateAmlResources (InBuffer, &Buffer);
828 if (ACPI_FAILURE (Status))
833 /* Create and initialize the method parameter object */
835 Args[0] = AcpiUtCreateInternalObject (ACPI_TYPE_BUFFER);
839 * Must free the buffer allocated above (otherwise it is freed
842 ACPI_FREE (Buffer.Pointer);
843 Status = AE_NO_MEMORY;
847 Args[0]->Buffer.Length = (UINT32) Buffer.Length;
848 Args[0]->Buffer.Pointer = Buffer.Pointer;
849 Args[0]->Common.Flags = AOPOBJ_DATA_VALID;
852 /* Execute the method, no return value is expected */
854 Status = AcpiNsEvaluate (Info);
856 /* Clean up and return the status from AcpiNsEvaluate */
858 AcpiUtRemoveReference (Args[0]);
862 return_ACPI_STATUS (Status);