1 /*******************************************************************************
3 * Module Name: utresrc - Resource managment utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2011, 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.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/amlresrc.h>
52 #define _COMPONENT ACPI_UTILITIES
53 ACPI_MODULE_NAME ("utresrc")
56 #if defined(ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
59 * Strings used to decode resource descriptors.
60 * Used by both the disasssembler and the debugger resource dump routines
62 const char *AcpiGbl_BmDecode[] =
68 const char *AcpiGbl_ConfigDecode[] =
70 "0 - Good Configuration",
71 "1 - Acceptable Configuration",
72 "2 - Suboptimal Configuration",
73 "3 - ***Invalid Configuration***",
76 const char *AcpiGbl_ConsumeDecode[] =
82 const char *AcpiGbl_DecDecode[] =
88 const char *AcpiGbl_HeDecode[] =
94 const char *AcpiGbl_IoDecode[] =
100 const char *AcpiGbl_LlDecode[] =
106 const char *AcpiGbl_MaxDecode[] =
112 const char *AcpiGbl_MemDecode[] =
120 const char *AcpiGbl_MinDecode[] =
126 const char *AcpiGbl_MtpDecode[] =
128 "AddressRangeMemory",
129 "AddressRangeReserved",
134 const char *AcpiGbl_RngDecode[] =
142 const char *AcpiGbl_RwDecode[] =
148 const char *AcpiGbl_ShrDecode[] =
154 const char *AcpiGbl_SizDecode[] =
162 const char *AcpiGbl_TrsDecode[] =
168 const char *AcpiGbl_TtpDecode[] =
174 const char *AcpiGbl_TypDecode[] =
186 * Base sizes of the raw AML resource descriptors, indexed by resource type.
187 * Zero indicates a reserved (and therefore invalid) resource type.
189 const UINT8 AcpiGbl_ResourceAmlSizes[] =
191 /* Small descriptors */
197 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ),
198 ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA),
199 ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT),
200 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT),
201 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO),
202 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO),
207 ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL),
208 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG),
210 /* Large descriptors */
213 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24),
214 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER),
216 ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE),
217 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32),
218 ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32),
219 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32),
220 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16),
221 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ),
222 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64),
223 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64)
228 * Resource types, used to validate the resource length field.
229 * The length of fixed-length types must match exactly, variable
230 * lengths must meet the minimum required length, etc.
231 * Zero indicates a reserved (and therefore invalid) resource type.
233 static const UINT8 AcpiGbl_ResourceTypes[] =
235 /* Small descriptors */
241 ACPI_SMALL_VARIABLE_LENGTH,
243 ACPI_SMALL_VARIABLE_LENGTH,
251 ACPI_VARIABLE_LENGTH,
254 /* Large descriptors */
260 ACPI_VARIABLE_LENGTH,
263 ACPI_VARIABLE_LENGTH,
264 ACPI_VARIABLE_LENGTH,
265 ACPI_VARIABLE_LENGTH,
266 ACPI_VARIABLE_LENGTH,
271 /*******************************************************************************
273 * FUNCTION: AcpiUtWalkAmlResources
275 * PARAMETERS: Aml - Pointer to the raw AML resource template
276 * AmlLength - Length of the entire template
277 * UserFunction - Called once for each descriptor found. If
278 * NULL, a pointer to the EndTag is returned
279 * Context - Passed to UserFunction
283 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
284 * once for each resource found.
286 ******************************************************************************/
289 AcpiUtWalkAmlResources (
292 ACPI_WALK_AML_CALLBACK UserFunction,
302 ACPI_FUNCTION_TRACE (UtWalkAmlResources);
305 /* The absolute minimum resource template is one EndTag descriptor */
307 if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
309 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
312 /* Point to the end of the resource template buffer */
314 EndAml = Aml + AmlLength;
316 /* Walk the byte list, abort on any invalid descriptor type or length */
320 /* Validate the Resource Type and Resource Length */
322 Status = AcpiUtValidateResource (Aml, &ResourceIndex);
323 if (ACPI_FAILURE (Status))
325 return_ACPI_STATUS (Status);
328 /* Get the length of this descriptor */
330 Length = AcpiUtGetDescriptorLength (Aml);
332 /* Invoke the user function */
336 Status = UserFunction (Aml, Length, Offset, ResourceIndex, Context);
337 if (ACPI_FAILURE (Status))
343 /* An EndTag descriptor terminates this resource template */
345 if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
348 * There must be at least one more byte in the buffer for
349 * the 2nd byte of the EndTag
351 if ((Aml + 1) >= EndAml)
353 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
356 /* Return the pointer to the EndTag if requested */
360 *(void **) Context = Aml;
365 return_ACPI_STATUS (AE_OK);
372 /* Did not find an EndTag descriptor */
374 return (AE_AML_NO_RESOURCE_END_TAG);
378 /*******************************************************************************
380 * FUNCTION: AcpiUtValidateResource
382 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
383 * ReturnIndex - Where the resource index is returned. NULL
384 * if the index is not required.
386 * RETURN: Status, and optionally the Index into the global resource tables
388 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
389 * Type and Resource Length. Returns an index into the global
390 * resource information/dispatch tables for later use.
392 ******************************************************************************/
395 AcpiUtValidateResource (
401 ACPI_RS_LENGTH ResourceLength;
402 ACPI_RS_LENGTH MinimumResourceLength;
405 ACPI_FUNCTION_ENTRY ();
409 * 1) Validate the ResourceType field (Byte 0)
411 ResourceType = ACPI_GET8 (Aml);
414 * Byte 0 contains the descriptor name (Resource Type)
415 * Examine the large/small bit in the resource header
417 if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
419 /* Verify the large resource type (name) against the max */
421 if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
423 return (AE_AML_INVALID_RESOURCE_TYPE);
427 * Large Resource Type -- bits 6:0 contain the name
428 * Translate range 0x80-0x8B to index range 0x10-0x1B
430 ResourceIndex = (UINT8) (ResourceType - 0x70);
435 * Small Resource Type -- bits 6:3 contain the name
436 * Shift range to index range 0x00-0x0F
438 ResourceIndex = (UINT8)
439 ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
442 /* Check validity of the resource type, zero indicates name is invalid */
444 if (!AcpiGbl_ResourceTypes[ResourceIndex])
446 return (AE_AML_INVALID_RESOURCE_TYPE);
451 * 2) Validate the ResourceLength field. This ensures that the length
452 * is at least reasonable, and guarantees that it is non-zero.
454 ResourceLength = AcpiUtGetResourceLength (Aml);
455 MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
457 /* Validate based upon the type of resource - fixed length or variable */
459 switch (AcpiGbl_ResourceTypes[ResourceIndex])
461 case ACPI_FIXED_LENGTH:
463 /* Fixed length resource, length must match exactly */
465 if (ResourceLength != MinimumResourceLength)
467 return (AE_AML_BAD_RESOURCE_LENGTH);
471 case ACPI_VARIABLE_LENGTH:
473 /* Variable length resource, length must be at least the minimum */
475 if (ResourceLength < MinimumResourceLength)
477 return (AE_AML_BAD_RESOURCE_LENGTH);
481 case ACPI_SMALL_VARIABLE_LENGTH:
483 /* Small variable length resource, length can be (Min) or (Min-1) */
485 if ((ResourceLength > MinimumResourceLength) ||
486 (ResourceLength < (MinimumResourceLength - 1)))
488 return (AE_AML_BAD_RESOURCE_LENGTH);
494 /* Shouldn't happen (because of validation earlier), but be sure */
496 return (AE_AML_INVALID_RESOURCE_TYPE);
499 /* Optionally return the resource table index */
503 *ReturnIndex = ResourceIndex;
510 /*******************************************************************************
512 * FUNCTION: AcpiUtGetResourceType
514 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
516 * RETURN: The Resource Type with no extraneous bits (except the
517 * Large/Small descriptor bit -- this is left alone)
519 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
520 * a resource descriptor.
522 ******************************************************************************/
525 AcpiUtGetResourceType (
528 ACPI_FUNCTION_ENTRY ();
532 * Byte 0 contains the descriptor name (Resource Type)
533 * Examine the large/small bit in the resource header
535 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
537 /* Large Resource Type -- bits 6:0 contain the name */
539 return (ACPI_GET8 (Aml));
543 /* Small Resource Type -- bits 6:3 contain the name */
545 return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
550 /*******************************************************************************
552 * FUNCTION: AcpiUtGetResourceLength
554 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
556 * RETURN: Byte Length
558 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
559 * definition, this does not include the size of the descriptor
560 * header or the length field itself.
562 ******************************************************************************/
565 AcpiUtGetResourceLength (
568 ACPI_RS_LENGTH ResourceLength;
571 ACPI_FUNCTION_ENTRY ();
575 * Byte 0 contains the descriptor name (Resource Type)
576 * Examine the large/small bit in the resource header
578 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
580 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
582 ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
587 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
589 ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
590 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
593 return (ResourceLength);
597 /*******************************************************************************
599 * FUNCTION: AcpiUtGetResourceHeaderLength
601 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
603 * RETURN: Length of the AML header (depends on large/small descriptor)
605 * DESCRIPTION: Get the length of the header for this resource.
607 ******************************************************************************/
610 AcpiUtGetResourceHeaderLength (
613 ACPI_FUNCTION_ENTRY ();
616 /* Examine the large/small bit in the resource header */
618 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
620 return (sizeof (AML_RESOURCE_LARGE_HEADER));
624 return (sizeof (AML_RESOURCE_SMALL_HEADER));
629 /*******************************************************************************
631 * FUNCTION: AcpiUtGetDescriptorLength
633 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
635 * RETURN: Byte length
637 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
638 * length of the descriptor header and the length field itself.
639 * Used to walk descriptor lists.
641 ******************************************************************************/
644 AcpiUtGetDescriptorLength (
647 ACPI_FUNCTION_ENTRY ();
651 * Get the Resource Length (does not include header length) and add
652 * the header length (depends on if this is a small or large resource)
654 return (AcpiUtGetResourceLength (Aml) +
655 AcpiUtGetResourceHeaderLength (Aml));
659 /*******************************************************************************
661 * FUNCTION: AcpiUtGetResourceEndTag
663 * PARAMETERS: ObjDesc - The resource template buffer object
664 * EndTag - Where the pointer to the EndTag is returned
666 * RETURN: Status, pointer to the end tag
668 * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
669 * Note: allows a buffer length of zero.
671 ******************************************************************************/
674 AcpiUtGetResourceEndTag (
675 ACPI_OPERAND_OBJECT *ObjDesc,
681 ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
684 /* Allow a buffer length of zero */
686 if (!ObjDesc->Buffer.Length)
688 *EndTag = ObjDesc->Buffer.Pointer;
689 return_ACPI_STATUS (AE_OK);
692 /* Validate the template and get a pointer to the EndTag */
694 Status = AcpiUtWalkAmlResources (ObjDesc->Buffer.Pointer,
695 ObjDesc->Buffer.Length, NULL, EndTag);
697 return_ACPI_STATUS (Status);