1 /*******************************************************************************
3 * Module Name: utresrc - Resource management utilities
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/acresrc.h>
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utresrc")
53 #if defined(ACPI_DEBUG_OUTPUT) || defined (ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
56 * Strings used to decode resource descriptors.
57 * Used by both the disassembler and the debugger resource dump routines
59 const char *AcpiGbl_BmDecode[] =
65 const char *AcpiGbl_ConfigDecode[] =
67 "0 - Good Configuration",
68 "1 - Acceptable Configuration",
69 "2 - Suboptimal Configuration",
70 "3 - ***Invalid Configuration***",
73 const char *AcpiGbl_ConsumeDecode[] =
79 const char *AcpiGbl_DecDecode[] =
85 const char *AcpiGbl_HeDecode[] =
91 const char *AcpiGbl_IoDecode[] =
97 const char *AcpiGbl_LlDecode[] =
105 const char *AcpiGbl_MaxDecode[] =
111 const char *AcpiGbl_MemDecode[] =
119 const char *AcpiGbl_MinDecode[] =
125 const char *AcpiGbl_MtpDecode[] =
127 "AddressRangeMemory",
128 "AddressRangeReserved",
133 const char *AcpiGbl_RngDecode[] =
141 const char *AcpiGbl_RwDecode[] =
147 const char *AcpiGbl_ShrDecode[] =
151 "ExclusiveAndWake", /* ACPI 5.0 */
152 "SharedAndWake" /* ACPI 5.0 */
155 const char *AcpiGbl_SizDecode[] =
163 const char *AcpiGbl_TrsDecode[] =
169 const char *AcpiGbl_TtpDecode[] =
175 const char *AcpiGbl_TypDecode[] =
183 const char *AcpiGbl_PpcDecode[] =
191 const char *AcpiGbl_IorDecode[] =
194 "IoRestrictionInputOnly",
195 "IoRestrictionOutputOnly",
196 "IoRestrictionNoneAndPreserve"
199 const char *AcpiGbl_DtsDecode[] =
209 /* GPIO connection type */
211 const char *AcpiGbl_CtDecode[] =
217 /* Serial bus type */
219 const char *AcpiGbl_SbtDecode[] =
221 "/* UNKNOWN serial bus type */",
227 /* I2C serial bus access mode */
229 const char *AcpiGbl_AmDecode[] =
231 "AddressingMode7Bit",
232 "AddressingMode10Bit"
235 /* I2C serial bus slave mode */
237 const char *AcpiGbl_SmDecode[] =
239 "ControllerInitiated",
243 /* SPI serial bus wire mode */
245 const char *AcpiGbl_WmDecode[] =
251 /* SPI serial clock phase */
253 const char *AcpiGbl_CphDecode[] =
259 /* SPI serial bus clock polarity */
261 const char *AcpiGbl_CpoDecode[] =
267 /* SPI serial bus device polarity */
269 const char *AcpiGbl_DpDecode[] =
275 /* UART serial bus endian */
277 const char *AcpiGbl_EdDecode[] =
283 /* UART serial bus bits per byte */
285 const char *AcpiGbl_BpbDecode[] =
292 "/* UNKNOWN Bits per byte */",
293 "/* UNKNOWN Bits per byte */",
294 "/* UNKNOWN Bits per byte */"
297 /* UART serial bus stop bits */
299 const char *AcpiGbl_SbDecode[] =
303 "StopBitsOnePlusHalf",
307 /* UART serial bus flow control */
309 const char *AcpiGbl_FcDecode[] =
312 "FlowControlHardware",
314 "/* UNKNOWN flow control keyword */"
317 /* UART serial bus parity type */
319 const char *AcpiGbl_PtDecode[] =
326 "/* UNKNOWN parity keyword */",
327 "/* UNKNOWN parity keyword */",
328 "/* UNKNOWN parity keyword */"
335 * Base sizes of the raw AML resource descriptors, indexed by resource type.
336 * Zero indicates a reserved (and therefore invalid) resource type.
338 const UINT8 AcpiGbl_ResourceAmlSizes[] =
340 /* Small descriptors */
346 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ),
347 ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA),
348 ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT),
349 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT),
350 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO),
351 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO),
352 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_DMA),
356 ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL),
357 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG),
359 /* Large descriptors */
362 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24),
363 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER),
365 ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE),
366 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32),
367 ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32),
368 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32),
369 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16),
370 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ),
371 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64),
372 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64),
373 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GPIO),
375 ACPI_AML_SIZE_LARGE (AML_RESOURCE_COMMON_SERIALBUS),
378 const UINT8 AcpiGbl_ResourceAmlSerialBusSizes[] =
381 ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS),
382 ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS),
383 ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS),
388 * Resource types, used to validate the resource length field.
389 * The length of fixed-length types must match exactly, variable
390 * lengths must meet the minimum required length, etc.
391 * Zero indicates a reserved (and therefore invalid) resource type.
393 static const UINT8 AcpiGbl_ResourceTypes[] =
395 /* Small descriptors */
401 ACPI_SMALL_VARIABLE_LENGTH, /* 04 IRQ */
402 ACPI_FIXED_LENGTH, /* 05 DMA */
403 ACPI_SMALL_VARIABLE_LENGTH, /* 06 StartDependentFunctions */
404 ACPI_FIXED_LENGTH, /* 07 EndDependentFunctions */
405 ACPI_FIXED_LENGTH, /* 08 IO */
406 ACPI_FIXED_LENGTH, /* 09 FixedIO */
407 ACPI_FIXED_LENGTH, /* 0A FixedDMA */
411 ACPI_VARIABLE_LENGTH, /* 0E VendorShort */
412 ACPI_FIXED_LENGTH, /* 0F EndTag */
414 /* Large descriptors */
417 ACPI_FIXED_LENGTH, /* 01 Memory24 */
418 ACPI_FIXED_LENGTH, /* 02 GenericRegister */
420 ACPI_VARIABLE_LENGTH, /* 04 VendorLong */
421 ACPI_FIXED_LENGTH, /* 05 Memory32 */
422 ACPI_FIXED_LENGTH, /* 06 Memory32Fixed */
423 ACPI_VARIABLE_LENGTH, /* 07 Dword* address */
424 ACPI_VARIABLE_LENGTH, /* 08 Word* address */
425 ACPI_VARIABLE_LENGTH, /* 09 ExtendedIRQ */
426 ACPI_VARIABLE_LENGTH, /* 0A Qword* address */
427 ACPI_FIXED_LENGTH, /* 0B Extended* address */
428 ACPI_VARIABLE_LENGTH, /* 0C Gpio* */
430 ACPI_VARIABLE_LENGTH /* 0E *SerialBus */
434 /*******************************************************************************
436 * FUNCTION: AcpiUtWalkAmlResources
438 * PARAMETERS: WalkState - Current walk info
439 * PARAMETERS: Aml - Pointer to the raw AML resource template
440 * AmlLength - Length of the entire template
441 * UserFunction - Called once for each descriptor found. If
442 * NULL, a pointer to the EndTag is returned
443 * Context - Passed to UserFunction
447 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
448 * once for each resource found.
450 ******************************************************************************/
453 AcpiUtWalkAmlResources (
454 ACPI_WALK_STATE *WalkState,
457 ACPI_WALK_AML_CALLBACK UserFunction,
465 UINT8 EndTag[2] = {0x79, 0x00};
468 ACPI_FUNCTION_TRACE (UtWalkAmlResources);
471 /* The absolute minimum resource template is one EndTag descriptor */
473 if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
475 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
478 /* Point to the end of the resource template buffer */
480 EndAml = Aml + AmlLength;
482 /* Walk the byte list, abort on any invalid descriptor type or length */
486 /* Validate the Resource Type and Resource Length */
488 Status = AcpiUtValidateResource (WalkState, Aml, &ResourceIndex);
489 if (ACPI_FAILURE (Status))
492 * Exit on failure. Cannot continue because the descriptor length
495 return_ACPI_STATUS (Status);
498 /* Get the length of this descriptor */
500 Length = AcpiUtGetDescriptorLength (Aml);
502 /* Invoke the user function */
506 Status = UserFunction (Aml, Length, Offset, ResourceIndex, Context);
507 if (ACPI_FAILURE (Status))
509 return_ACPI_STATUS (Status);
513 /* An EndTag descriptor terminates this resource template */
515 if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
518 * There must be at least one more byte in the buffer for
519 * the 2nd byte of the EndTag
521 if ((Aml + 1) >= EndAml)
523 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
526 /* Return the pointer to the EndTag if requested */
535 return_ACPI_STATUS (AE_OK);
542 /* Did not find an EndTag descriptor */
546 /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
548 (void) AcpiUtValidateResource (WalkState, EndTag, &ResourceIndex);
549 Status = UserFunction (EndTag, 2, Offset, ResourceIndex, Context);
550 if (ACPI_FAILURE (Status))
552 return_ACPI_STATUS (Status);
556 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
560 /*******************************************************************************
562 * FUNCTION: AcpiUtValidateResource
564 * PARAMETERS: WalkState - Current walk info
565 * Aml - Pointer to the raw AML resource descriptor
566 * ReturnIndex - Where the resource index is returned. NULL
567 * if the index is not required.
569 * RETURN: Status, and optionally the Index into the global resource tables
571 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
572 * Type and Resource Length. Returns an index into the global
573 * resource information/dispatch tables for later use.
575 ******************************************************************************/
578 AcpiUtValidateResource (
579 ACPI_WALK_STATE *WalkState,
583 AML_RESOURCE *AmlResource;
586 ACPI_RS_LENGTH ResourceLength;
587 ACPI_RS_LENGTH MinimumResourceLength;
590 ACPI_FUNCTION_ENTRY ();
594 * 1) Validate the ResourceType field (Byte 0)
596 ResourceType = ACPI_GET8 (Aml);
599 * Byte 0 contains the descriptor name (Resource Type)
600 * Examine the large/small bit in the resource header
602 if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
604 /* Verify the large resource type (name) against the max */
606 if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
608 goto InvalidResource;
612 * Large Resource Type -- bits 6:0 contain the name
613 * Translate range 0x80-0x8B to index range 0x10-0x1B
615 ResourceIndex = (UINT8) (ResourceType - 0x70);
620 * Small Resource Type -- bits 6:3 contain the name
621 * Shift range to index range 0x00-0x0F
623 ResourceIndex = (UINT8)
624 ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
628 * Check validity of the resource type, via AcpiGbl_ResourceTypes. Zero
629 * indicates an invalid resource.
631 if (!AcpiGbl_ResourceTypes[ResourceIndex])
633 goto InvalidResource;
637 * Validate the ResourceLength field. This ensures that the length
638 * is at least reasonable, and guarantees that it is non-zero.
640 ResourceLength = AcpiUtGetResourceLength (Aml);
641 MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
643 /* Validate based upon the type of resource - fixed length or variable */
645 switch (AcpiGbl_ResourceTypes[ResourceIndex])
647 case ACPI_FIXED_LENGTH:
649 /* Fixed length resource, length must match exactly */
651 if (ResourceLength != MinimumResourceLength)
653 goto BadResourceLength;
657 case ACPI_VARIABLE_LENGTH:
659 /* Variable length resource, length must be at least the minimum */
661 if (ResourceLength < MinimumResourceLength)
663 goto BadResourceLength;
667 case ACPI_SMALL_VARIABLE_LENGTH:
669 /* Small variable length resource, length can be (Min) or (Min-1) */
671 if ((ResourceLength > MinimumResourceLength) ||
672 (ResourceLength < (MinimumResourceLength - 1)))
674 goto BadResourceLength;
680 /* Shouldn't happen (because of validation earlier), but be sure */
682 goto InvalidResource;
685 AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
686 if (ResourceType == ACPI_RESOURCE_NAME_SERIAL_BUS)
688 /* Validate the BusType field */
690 if ((AmlResource->CommonSerialBus.Type == 0) ||
691 (AmlResource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
695 ACPI_ERROR ((AE_INFO,
696 "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
697 AmlResource->CommonSerialBus.Type));
699 return (AE_AML_INVALID_RESOURCE_TYPE);
703 /* Optionally return the resource table index */
707 *ReturnIndex = ResourceIndex;
717 ACPI_ERROR ((AE_INFO,
718 "Invalid/unsupported resource descriptor: Type 0x%2.2X",
721 return (AE_AML_INVALID_RESOURCE_TYPE);
727 ACPI_ERROR ((AE_INFO,
728 "Invalid resource descriptor length: Type "
729 "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
730 ResourceType, ResourceLength, MinimumResourceLength));
732 return (AE_AML_BAD_RESOURCE_LENGTH);
736 /*******************************************************************************
738 * FUNCTION: AcpiUtGetResourceType
740 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
742 * RETURN: The Resource Type with no extraneous bits (except the
743 * Large/Small descriptor bit -- this is left alone)
745 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
746 * a resource descriptor.
748 ******************************************************************************/
751 AcpiUtGetResourceType (
754 ACPI_FUNCTION_ENTRY ();
758 * Byte 0 contains the descriptor name (Resource Type)
759 * Examine the large/small bit in the resource header
761 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
763 /* Large Resource Type -- bits 6:0 contain the name */
765 return (ACPI_GET8 (Aml));
769 /* Small Resource Type -- bits 6:3 contain the name */
771 return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
776 /*******************************************************************************
778 * FUNCTION: AcpiUtGetResourceLength
780 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
782 * RETURN: Byte Length
784 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
785 * definition, this does not include the size of the descriptor
786 * header or the length field itself.
788 ******************************************************************************/
791 AcpiUtGetResourceLength (
794 ACPI_RS_LENGTH ResourceLength;
797 ACPI_FUNCTION_ENTRY ();
801 * Byte 0 contains the descriptor name (Resource Type)
802 * Examine the large/small bit in the resource header
804 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
806 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
808 ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
813 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
815 ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
816 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
819 return (ResourceLength);
823 /*******************************************************************************
825 * FUNCTION: AcpiUtGetResourceHeaderLength
827 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
829 * RETURN: Length of the AML header (depends on large/small descriptor)
831 * DESCRIPTION: Get the length of the header for this resource.
833 ******************************************************************************/
836 AcpiUtGetResourceHeaderLength (
839 ACPI_FUNCTION_ENTRY ();
842 /* Examine the large/small bit in the resource header */
844 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
846 return (sizeof (AML_RESOURCE_LARGE_HEADER));
850 return (sizeof (AML_RESOURCE_SMALL_HEADER));
855 /*******************************************************************************
857 * FUNCTION: AcpiUtGetDescriptorLength
859 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
861 * RETURN: Byte length
863 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
864 * length of the descriptor header and the length field itself.
865 * Used to walk descriptor lists.
867 ******************************************************************************/
870 AcpiUtGetDescriptorLength (
873 ACPI_FUNCTION_ENTRY ();
877 * Get the Resource Length (does not include header length) and add
878 * the header length (depends on if this is a small or large resource)
880 return (AcpiUtGetResourceLength (Aml) +
881 AcpiUtGetResourceHeaderLength (Aml));
885 /*******************************************************************************
887 * FUNCTION: AcpiUtGetResourceEndTag
889 * PARAMETERS: ObjDesc - The resource template buffer object
890 * EndTag - Where the pointer to the EndTag is returned
892 * RETURN: Status, pointer to the end tag
894 * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
895 * Note: allows a buffer length of zero.
897 ******************************************************************************/
900 AcpiUtGetResourceEndTag (
901 ACPI_OPERAND_OBJECT *ObjDesc,
907 ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
910 /* Allow a buffer length of zero */
912 if (!ObjDesc->Buffer.Length)
914 *EndTag = ObjDesc->Buffer.Pointer;
915 return_ACPI_STATUS (AE_OK);
918 /* Validate the template and get a pointer to the EndTag */
920 Status = AcpiUtWalkAmlResources (NULL, ObjDesc->Buffer.Pointer,
921 ObjDesc->Buffer.Length, NULL, (void **) EndTag);
923 return_ACPI_STATUS (Status);