1 /*******************************************************************************
3 * Module Name: utresrc - Resource management utilities
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.
51 #define _COMPONENT ACPI_UTILITIES
52 ACPI_MODULE_NAME ("utresrc")
55 #if defined(ACPI_DEBUG_OUTPUT) || defined (ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
58 * Strings used to decode resource descriptors.
59 * Used by both the disassembler and the debugger resource dump routines
61 const char *AcpiGbl_BmDecode[] =
67 const char *AcpiGbl_ConfigDecode[] =
69 "0 - Good Configuration",
70 "1 - Acceptable Configuration",
71 "2 - Suboptimal Configuration",
72 "3 - ***Invalid Configuration***",
75 const char *AcpiGbl_ConsumeDecode[] =
81 const char *AcpiGbl_DecDecode[] =
87 const char *AcpiGbl_HeDecode[] =
93 const char *AcpiGbl_IoDecode[] =
99 const char *AcpiGbl_LlDecode[] =
107 const char *AcpiGbl_MaxDecode[] =
113 const char *AcpiGbl_MemDecode[] =
121 const char *AcpiGbl_MinDecode[] =
127 const char *AcpiGbl_MtpDecode[] =
129 "AddressRangeMemory",
130 "AddressRangeReserved",
135 const char *AcpiGbl_RngDecode[] =
143 const char *AcpiGbl_RwDecode[] =
149 const char *AcpiGbl_ShrDecode[] =
153 "ExclusiveAndWake", /* ACPI 5.0 */
154 "SharedAndWake" /* ACPI 5.0 */
157 const char *AcpiGbl_SizDecode[] =
165 const char *AcpiGbl_TrsDecode[] =
171 const char *AcpiGbl_TtpDecode[] =
177 const char *AcpiGbl_TypDecode[] =
185 const char *AcpiGbl_PpcDecode[] =
193 const char *AcpiGbl_IorDecode[] =
196 "IoRestrictionInputOnly",
197 "IoRestrictionOutputOnly",
198 "IoRestrictionNoneAndPreserve"
201 const char *AcpiGbl_DtsDecode[] =
211 /* GPIO connection type */
213 const char *AcpiGbl_CtDecode[] =
219 /* Serial bus type */
221 const char *AcpiGbl_SbtDecode[] =
223 "/* UNKNOWN serial bus type */",
229 /* I2C serial bus access mode */
231 const char *AcpiGbl_AmDecode[] =
233 "AddressingMode7Bit",
234 "AddressingMode10Bit"
237 /* I2C serial bus slave mode */
239 const char *AcpiGbl_SmDecode[] =
241 "ControllerInitiated",
245 /* SPI serial bus wire mode */
247 const char *AcpiGbl_WmDecode[] =
253 /* SPI serial clock phase */
255 const char *AcpiGbl_CphDecode[] =
261 /* SPI serial bus clock polarity */
263 const char *AcpiGbl_CpoDecode[] =
269 /* SPI serial bus device polarity */
271 const char *AcpiGbl_DpDecode[] =
277 /* UART serial bus endian */
279 const char *AcpiGbl_EdDecode[] =
285 /* UART serial bus bits per byte */
287 const char *AcpiGbl_BpbDecode[] =
294 "/* UNKNOWN Bits per byte */",
295 "/* UNKNOWN Bits per byte */",
296 "/* UNKNOWN Bits per byte */"
299 /* UART serial bus stop bits */
301 const char *AcpiGbl_SbDecode[] =
305 "StopBitsOnePlusHalf",
309 /* UART serial bus flow control */
311 const char *AcpiGbl_FcDecode[] =
314 "FlowControlHardware",
316 "/* UNKNOWN flow control keyword */"
319 /* UART serial bus parity type */
321 const char *AcpiGbl_PtDecode[] =
328 "/* UNKNOWN parity keyword */",
329 "/* UNKNOWN parity keyword */",
330 "/* UNKNOWN parity keyword */"
337 * Base sizes of the raw AML resource descriptors, indexed by resource type.
338 * Zero indicates a reserved (and therefore invalid) resource type.
340 const UINT8 AcpiGbl_ResourceAmlSizes[] =
342 /* Small descriptors */
348 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ),
349 ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA),
350 ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT),
351 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT),
352 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO),
353 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO),
354 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_DMA),
358 ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL),
359 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG),
361 /* Large descriptors */
364 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24),
365 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER),
367 ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE),
368 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32),
369 ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32),
370 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32),
371 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16),
372 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ),
373 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64),
374 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64),
375 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GPIO),
377 ACPI_AML_SIZE_LARGE (AML_RESOURCE_COMMON_SERIALBUS),
380 const UINT8 AcpiGbl_ResourceAmlSerialBusSizes[] =
383 ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS),
384 ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS),
385 ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS),
390 * Resource types, used to validate the resource length field.
391 * The length of fixed-length types must match exactly, variable
392 * lengths must meet the minimum required length, etc.
393 * Zero indicates a reserved (and therefore invalid) resource type.
395 static const UINT8 AcpiGbl_ResourceTypes[] =
397 /* Small descriptors */
403 ACPI_SMALL_VARIABLE_LENGTH, /* 04 IRQ */
404 ACPI_FIXED_LENGTH, /* 05 DMA */
405 ACPI_SMALL_VARIABLE_LENGTH, /* 06 StartDependentFunctions */
406 ACPI_FIXED_LENGTH, /* 07 EndDependentFunctions */
407 ACPI_FIXED_LENGTH, /* 08 IO */
408 ACPI_FIXED_LENGTH, /* 09 FixedIO */
409 ACPI_FIXED_LENGTH, /* 0A FixedDMA */
413 ACPI_VARIABLE_LENGTH, /* 0E VendorShort */
414 ACPI_FIXED_LENGTH, /* 0F EndTag */
416 /* Large descriptors */
419 ACPI_FIXED_LENGTH, /* 01 Memory24 */
420 ACPI_FIXED_LENGTH, /* 02 GenericRegister */
422 ACPI_VARIABLE_LENGTH, /* 04 VendorLong */
423 ACPI_FIXED_LENGTH, /* 05 Memory32 */
424 ACPI_FIXED_LENGTH, /* 06 Memory32Fixed */
425 ACPI_VARIABLE_LENGTH, /* 07 Dword* address */
426 ACPI_VARIABLE_LENGTH, /* 08 Word* address */
427 ACPI_VARIABLE_LENGTH, /* 09 ExtendedIRQ */
428 ACPI_VARIABLE_LENGTH, /* 0A Qword* address */
429 ACPI_FIXED_LENGTH, /* 0B Extended* address */
430 ACPI_VARIABLE_LENGTH, /* 0C Gpio* */
432 ACPI_VARIABLE_LENGTH /* 0E *SerialBus */
436 /*******************************************************************************
438 * FUNCTION: AcpiUtWalkAmlResources
440 * PARAMETERS: WalkState - Current walk info
441 * PARAMETERS: Aml - Pointer to the raw AML resource template
442 * AmlLength - Length of the entire template
443 * UserFunction - Called once for each descriptor found. If
444 * NULL, a pointer to the EndTag is returned
445 * Context - Passed to UserFunction
449 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
450 * once for each resource found.
452 ******************************************************************************/
455 AcpiUtWalkAmlResources (
456 ACPI_WALK_STATE *WalkState,
459 ACPI_WALK_AML_CALLBACK UserFunction,
467 UINT8 EndTag[2] = {0x79, 0x00};
470 ACPI_FUNCTION_TRACE (UtWalkAmlResources);
473 /* The absolute minimum resource template is one EndTag descriptor */
475 if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
477 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
480 /* Point to the end of the resource template buffer */
482 EndAml = Aml + AmlLength;
484 /* Walk the byte list, abort on any invalid descriptor type or length */
488 /* Validate the Resource Type and Resource Length */
490 Status = AcpiUtValidateResource (WalkState, Aml, &ResourceIndex);
491 if (ACPI_FAILURE (Status))
494 * Exit on failure. Cannot continue because the descriptor length
497 return_ACPI_STATUS (Status);
500 /* Get the length of this descriptor */
502 Length = AcpiUtGetDescriptorLength (Aml);
504 /* Invoke the user function */
508 Status = UserFunction (Aml, Length, Offset, ResourceIndex, Context);
509 if (ACPI_FAILURE (Status))
511 return_ACPI_STATUS (Status);
515 /* An EndTag descriptor terminates this resource template */
517 if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
520 * There must be at least one more byte in the buffer for
521 * the 2nd byte of the EndTag
523 if ((Aml + 1) >= EndAml)
525 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
528 /* Return the pointer to the EndTag if requested */
537 return_ACPI_STATUS (AE_OK);
544 /* Did not find an EndTag descriptor */
548 /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
550 (void) AcpiUtValidateResource (WalkState, EndTag, &ResourceIndex);
551 Status = UserFunction (EndTag, 2, Offset, ResourceIndex, Context);
552 if (ACPI_FAILURE (Status))
554 return_ACPI_STATUS (Status);
558 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
562 /*******************************************************************************
564 * FUNCTION: AcpiUtValidateResource
566 * PARAMETERS: WalkState - Current walk info
567 * Aml - Pointer to the raw AML resource descriptor
568 * ReturnIndex - Where the resource index is returned. NULL
569 * if the index is not required.
571 * RETURN: Status, and optionally the Index into the global resource tables
573 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
574 * Type and Resource Length. Returns an index into the global
575 * resource information/dispatch tables for later use.
577 ******************************************************************************/
580 AcpiUtValidateResource (
581 ACPI_WALK_STATE *WalkState,
585 AML_RESOURCE *AmlResource;
588 ACPI_RS_LENGTH ResourceLength;
589 ACPI_RS_LENGTH MinimumResourceLength;
592 ACPI_FUNCTION_ENTRY ();
596 * 1) Validate the ResourceType field (Byte 0)
598 ResourceType = ACPI_GET8 (Aml);
601 * Byte 0 contains the descriptor name (Resource Type)
602 * Examine the large/small bit in the resource header
604 if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
606 /* Verify the large resource type (name) against the max */
608 if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
610 goto InvalidResource;
614 * Large Resource Type -- bits 6:0 contain the name
615 * Translate range 0x80-0x8B to index range 0x10-0x1B
617 ResourceIndex = (UINT8) (ResourceType - 0x70);
622 * Small Resource Type -- bits 6:3 contain the name
623 * Shift range to index range 0x00-0x0F
625 ResourceIndex = (UINT8)
626 ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
630 * Check validity of the resource type, via AcpiGbl_ResourceTypes. Zero
631 * indicates an invalid resource.
633 if (!AcpiGbl_ResourceTypes[ResourceIndex])
635 goto InvalidResource;
639 * Validate the ResourceLength field. This ensures that the length
640 * is at least reasonable, and guarantees that it is non-zero.
642 ResourceLength = AcpiUtGetResourceLength (Aml);
643 MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
645 /* Validate based upon the type of resource - fixed length or variable */
647 switch (AcpiGbl_ResourceTypes[ResourceIndex])
649 case ACPI_FIXED_LENGTH:
651 /* Fixed length resource, length must match exactly */
653 if (ResourceLength != MinimumResourceLength)
655 goto BadResourceLength;
659 case ACPI_VARIABLE_LENGTH:
661 /* Variable length resource, length must be at least the minimum */
663 if (ResourceLength < MinimumResourceLength)
665 goto BadResourceLength;
669 case ACPI_SMALL_VARIABLE_LENGTH:
671 /* Small variable length resource, length can be (Min) or (Min-1) */
673 if ((ResourceLength > MinimumResourceLength) ||
674 (ResourceLength < (MinimumResourceLength - 1)))
676 goto BadResourceLength;
682 /* Shouldn't happen (because of validation earlier), but be sure */
684 goto InvalidResource;
687 AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
688 if (ResourceType == ACPI_RESOURCE_NAME_SERIAL_BUS)
690 /* Validate the BusType field */
692 if ((AmlResource->CommonSerialBus.Type == 0) ||
693 (AmlResource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
697 ACPI_ERROR ((AE_INFO,
698 "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
699 AmlResource->CommonSerialBus.Type));
701 return (AE_AML_INVALID_RESOURCE_TYPE);
705 /* Optionally return the resource table index */
709 *ReturnIndex = ResourceIndex;
719 ACPI_ERROR ((AE_INFO,
720 "Invalid/unsupported resource descriptor: Type 0x%2.2X",
723 return (AE_AML_INVALID_RESOURCE_TYPE);
729 ACPI_ERROR ((AE_INFO,
730 "Invalid resource descriptor length: Type "
731 "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
732 ResourceType, ResourceLength, MinimumResourceLength));
734 return (AE_AML_BAD_RESOURCE_LENGTH);
738 /*******************************************************************************
740 * FUNCTION: AcpiUtGetResourceType
742 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
744 * RETURN: The Resource Type with no extraneous bits (except the
745 * Large/Small descriptor bit -- this is left alone)
747 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
748 * a resource descriptor.
750 ******************************************************************************/
753 AcpiUtGetResourceType (
756 ACPI_FUNCTION_ENTRY ();
760 * Byte 0 contains the descriptor name (Resource Type)
761 * Examine the large/small bit in the resource header
763 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
765 /* Large Resource Type -- bits 6:0 contain the name */
767 return (ACPI_GET8 (Aml));
771 /* Small Resource Type -- bits 6:3 contain the name */
773 return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
778 /*******************************************************************************
780 * FUNCTION: AcpiUtGetResourceLength
782 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
784 * RETURN: Byte Length
786 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
787 * definition, this does not include the size of the descriptor
788 * header or the length field itself.
790 ******************************************************************************/
793 AcpiUtGetResourceLength (
796 ACPI_RS_LENGTH ResourceLength;
799 ACPI_FUNCTION_ENTRY ();
803 * Byte 0 contains the descriptor name (Resource Type)
804 * Examine the large/small bit in the resource header
806 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
808 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
810 ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
815 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
817 ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
818 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
821 return (ResourceLength);
825 /*******************************************************************************
827 * FUNCTION: AcpiUtGetResourceHeaderLength
829 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
831 * RETURN: Length of the AML header (depends on large/small descriptor)
833 * DESCRIPTION: Get the length of the header for this resource.
835 ******************************************************************************/
838 AcpiUtGetResourceHeaderLength (
841 ACPI_FUNCTION_ENTRY ();
844 /* Examine the large/small bit in the resource header */
846 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
848 return (sizeof (AML_RESOURCE_LARGE_HEADER));
852 return (sizeof (AML_RESOURCE_SMALL_HEADER));
857 /*******************************************************************************
859 * FUNCTION: AcpiUtGetDescriptorLength
861 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
863 * RETURN: Byte length
865 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
866 * length of the descriptor header and the length field itself.
867 * Used to walk descriptor lists.
869 ******************************************************************************/
872 AcpiUtGetDescriptorLength (
875 ACPI_FUNCTION_ENTRY ();
879 * Get the Resource Length (does not include header length) and add
880 * the header length (depends on if this is a small or large resource)
882 return (AcpiUtGetResourceLength (Aml) +
883 AcpiUtGetResourceHeaderLength (Aml));
887 /*******************************************************************************
889 * FUNCTION: AcpiUtGetResourceEndTag
891 * PARAMETERS: ObjDesc - The resource template buffer object
892 * EndTag - Where the pointer to the EndTag is returned
894 * RETURN: Status, pointer to the end tag
896 * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
897 * Note: allows a buffer length of zero.
899 ******************************************************************************/
902 AcpiUtGetResourceEndTag (
903 ACPI_OPERAND_OBJECT *ObjDesc,
909 ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
912 /* Allow a buffer length of zero */
914 if (!ObjDesc->Buffer.Length)
916 *EndTag = ObjDesc->Buffer.Pointer;
917 return_ACPI_STATUS (AE_OK);
920 /* Validate the template and get a pointer to the EndTag */
922 Status = AcpiUtWalkAmlResources (NULL, ObjDesc->Buffer.Pointer,
923 ObjDesc->Buffer.Length, NULL, (void **) EndTag);
925 return_ACPI_STATUS (Status);