1 /*******************************************************************************
3 * Module Name: utresrc - Resource management utilities
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2017, 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.
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utresrc")
54 * Base sizes of the raw AML resource descriptors, indexed by resource type.
55 * Zero indicates a reserved (and therefore invalid) resource type.
57 const UINT8 AcpiGbl_ResourceAmlSizes
[] =
59 /* Small descriptors */
65 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ
),
66 ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA
),
67 ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT
),
68 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT
),
69 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO
),
70 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO
),
71 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_DMA
),
75 ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL
),
76 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG
),
78 /* Large descriptors */
81 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24
),
82 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER
),
84 ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE
),
85 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32
),
86 ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32
),
87 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32
),
88 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16
),
89 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ
),
90 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64
),
91 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64
),
92 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GPIO
),
93 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_FUNCTION
),
94 ACPI_AML_SIZE_LARGE (AML_RESOURCE_COMMON_SERIALBUS
),
95 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_CONFIG
),
96 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP
),
97 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_FUNCTION
),
98 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_CONFIG
),
101 const UINT8 AcpiGbl_ResourceAmlSerialBusSizes
[] =
104 ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS
),
105 ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS
),
106 ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS
),
111 * Resource types, used to validate the resource length field.
112 * The length of fixed-length types must match exactly, variable
113 * lengths must meet the minimum required length, etc.
114 * Zero indicates a reserved (and therefore invalid) resource type.
116 static const UINT8 AcpiGbl_ResourceTypes
[] =
118 /* Small descriptors */
124 ACPI_SMALL_VARIABLE_LENGTH
, /* 04 IRQ */
125 ACPI_FIXED_LENGTH
, /* 05 DMA */
126 ACPI_SMALL_VARIABLE_LENGTH
, /* 06 StartDependentFunctions */
127 ACPI_FIXED_LENGTH
, /* 07 EndDependentFunctions */
128 ACPI_FIXED_LENGTH
, /* 08 IO */
129 ACPI_FIXED_LENGTH
, /* 09 FixedIO */
130 ACPI_FIXED_LENGTH
, /* 0A FixedDMA */
134 ACPI_VARIABLE_LENGTH
, /* 0E VendorShort */
135 ACPI_FIXED_LENGTH
, /* 0F EndTag */
137 /* Large descriptors */
140 ACPI_FIXED_LENGTH
, /* 01 Memory24 */
141 ACPI_FIXED_LENGTH
, /* 02 GenericRegister */
143 ACPI_VARIABLE_LENGTH
, /* 04 VendorLong */
144 ACPI_FIXED_LENGTH
, /* 05 Memory32 */
145 ACPI_FIXED_LENGTH
, /* 06 Memory32Fixed */
146 ACPI_VARIABLE_LENGTH
, /* 07 Dword* address */
147 ACPI_VARIABLE_LENGTH
, /* 08 Word* address */
148 ACPI_VARIABLE_LENGTH
, /* 09 ExtendedIRQ */
149 ACPI_VARIABLE_LENGTH
, /* 0A Qword* address */
150 ACPI_FIXED_LENGTH
, /* 0B Extended* address */
151 ACPI_VARIABLE_LENGTH
, /* 0C Gpio* */
152 ACPI_VARIABLE_LENGTH
, /* 0D PinFunction */
153 ACPI_VARIABLE_LENGTH
, /* 0E *SerialBus */
154 ACPI_VARIABLE_LENGTH
, /* 0F PinConfig */
155 ACPI_VARIABLE_LENGTH
, /* 10 PinGroup */
156 ACPI_VARIABLE_LENGTH
, /* 11 PinGroupFunction */
157 ACPI_VARIABLE_LENGTH
, /* 12 PinGroupConfig */
161 /*******************************************************************************
163 * FUNCTION: AcpiUtWalkAmlResources
165 * PARAMETERS: WalkState - Current walk info
166 * PARAMETERS: Aml - Pointer to the raw AML resource template
167 * AmlLength - Length of the entire template
168 * UserFunction - Called once for each descriptor found. If
169 * NULL, a pointer to the EndTag is returned
170 * Context - Passed to UserFunction
174 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
175 * once for each resource found.
177 ******************************************************************************/
180 AcpiUtWalkAmlResources (
181 ACPI_WALK_STATE
*WalkState
,
184 ACPI_WALK_AML_CALLBACK UserFunction
,
192 UINT8 EndTag
[2] = {0x79, 0x00};
195 ACPI_FUNCTION_TRACE (UtWalkAmlResources
);
199 * The absolute minimum resource template is one EndTag descriptor.
200 * However, we will treat a lone EndTag as just a simple buffer.
202 if (AmlLength
<= sizeof (AML_RESOURCE_END_TAG
))
204 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG
);
207 /* Point to the end of the resource template buffer */
209 EndAml
= Aml
+ AmlLength
;
211 /* Walk the byte list, abort on any invalid descriptor type or length */
215 /* Validate the Resource Type and Resource Length */
217 Status
= AcpiUtValidateResource (WalkState
, Aml
, &ResourceIndex
);
218 if (ACPI_FAILURE (Status
))
221 * Exit on failure. Cannot continue because the descriptor
222 * length may be bogus also.
224 return_ACPI_STATUS (Status
);
227 /* Get the length of this descriptor */
229 Length
= AcpiUtGetDescriptorLength (Aml
);
231 /* Invoke the user function */
235 Status
= UserFunction (
236 Aml
, Length
, Offset
, ResourceIndex
, Context
);
237 if (ACPI_FAILURE (Status
))
239 return_ACPI_STATUS (Status
);
243 /* An EndTag descriptor terminates this resource template */
245 if (AcpiUtGetResourceType (Aml
) == ACPI_RESOURCE_NAME_END_TAG
)
248 * There must be at least one more byte in the buffer for
249 * the 2nd byte of the EndTag
251 if ((Aml
+ 1) >= EndAml
)
253 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG
);
257 * The EndTag opcode must be followed by a zero byte.
258 * Although this byte is technically defined to be a checksum,
259 * in practice, all ASL compilers set this byte to zero.
263 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG
);
266 /* Return the pointer to the EndTag if requested */
274 * Normal exit. Note: We allow the buffer to be larger than
275 * the resource template, as long as the END_TAG exists.
277 return_ACPI_STATUS (AE_OK
);
284 /* Did not find an EndTag descriptor */
288 /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
290 (void) AcpiUtValidateResource (WalkState
, EndTag
, &ResourceIndex
);
291 Status
= UserFunction (EndTag
, 2, Offset
, ResourceIndex
, Context
);
292 if (ACPI_FAILURE (Status
))
294 return_ACPI_STATUS (Status
);
298 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG
);
302 /*******************************************************************************
304 * FUNCTION: AcpiUtValidateResource
306 * PARAMETERS: WalkState - Current walk info
307 * Aml - Pointer to the raw AML resource descriptor
308 * ReturnIndex - Where the resource index is returned. NULL
309 * if the index is not required.
311 * RETURN: Status, and optionally the Index into the global resource tables
313 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
314 * Type and Resource Length. Returns an index into the global
315 * resource information/dispatch tables for later use.
317 ******************************************************************************/
320 AcpiUtValidateResource (
321 ACPI_WALK_STATE
*WalkState
,
325 AML_RESOURCE
*AmlResource
;
328 ACPI_RS_LENGTH ResourceLength
;
329 ACPI_RS_LENGTH MinimumResourceLength
;
332 ACPI_FUNCTION_ENTRY ();
336 * 1) Validate the ResourceType field (Byte 0)
338 ResourceType
= ACPI_GET8 (Aml
);
341 * Byte 0 contains the descriptor name (Resource Type)
342 * Examine the large/small bit in the resource header
344 if (ResourceType
& ACPI_RESOURCE_NAME_LARGE
)
346 /* Verify the large resource type (name) against the max */
348 if (ResourceType
> ACPI_RESOURCE_NAME_LARGE_MAX
)
350 goto InvalidResource
;
354 * Large Resource Type -- bits 6:0 contain the name
355 * Translate range 0x80-0x8B to index range 0x10-0x1B
357 ResourceIndex
= (UINT8
) (ResourceType
- 0x70);
362 * Small Resource Type -- bits 6:3 contain the name
363 * Shift range to index range 0x00-0x0F
365 ResourceIndex
= (UINT8
)
366 ((ResourceType
& ACPI_RESOURCE_NAME_SMALL_MASK
) >> 3);
370 * Check validity of the resource type, via AcpiGbl_ResourceTypes.
371 * Zero indicates an invalid resource.
373 if (!AcpiGbl_ResourceTypes
[ResourceIndex
])
375 goto InvalidResource
;
379 * Validate the ResourceLength field. This ensures that the length
380 * is at least reasonable, and guarantees that it is non-zero.
382 ResourceLength
= AcpiUtGetResourceLength (Aml
);
383 MinimumResourceLength
= AcpiGbl_ResourceAmlSizes
[ResourceIndex
];
385 /* Validate based upon the type of resource - fixed length or variable */
387 switch (AcpiGbl_ResourceTypes
[ResourceIndex
])
389 case ACPI_FIXED_LENGTH
:
391 /* Fixed length resource, length must match exactly */
393 if (ResourceLength
!= MinimumResourceLength
)
395 goto BadResourceLength
;
399 case ACPI_VARIABLE_LENGTH
:
401 /* Variable length resource, length must be at least the minimum */
403 if (ResourceLength
< MinimumResourceLength
)
405 goto BadResourceLength
;
409 case ACPI_SMALL_VARIABLE_LENGTH
:
411 /* Small variable length resource, length can be (Min) or (Min-1) */
413 if ((ResourceLength
> MinimumResourceLength
) ||
414 (ResourceLength
< (MinimumResourceLength
- 1)))
416 goto BadResourceLength
;
422 /* Shouldn't happen (because of validation earlier), but be sure */
424 goto InvalidResource
;
427 AmlResource
= ACPI_CAST_PTR (AML_RESOURCE
, Aml
);
428 if (ResourceType
== ACPI_RESOURCE_NAME_SERIAL_BUS
)
430 /* Validate the BusType field */
432 if ((AmlResource
->CommonSerialBus
.Type
== 0) ||
433 (AmlResource
->CommonSerialBus
.Type
> AML_RESOURCE_MAX_SERIALBUSTYPE
))
437 ACPI_ERROR ((AE_INFO
,
438 "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
439 AmlResource
->CommonSerialBus
.Type
));
441 return (AE_AML_INVALID_RESOURCE_TYPE
);
445 /* Optionally return the resource table index */
449 *ReturnIndex
= ResourceIndex
;
459 ACPI_ERROR ((AE_INFO
,
460 "Invalid/unsupported resource descriptor: Type 0x%2.2X",
463 return (AE_AML_INVALID_RESOURCE_TYPE
);
469 ACPI_ERROR ((AE_INFO
,
470 "Invalid resource descriptor length: Type "
471 "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
472 ResourceType
, ResourceLength
, MinimumResourceLength
));
474 return (AE_AML_BAD_RESOURCE_LENGTH
);
478 /*******************************************************************************
480 * FUNCTION: AcpiUtGetResourceType
482 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
484 * RETURN: The Resource Type with no extraneous bits (except the
485 * Large/Small descriptor bit -- this is left alone)
487 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
488 * a resource descriptor.
490 ******************************************************************************/
493 AcpiUtGetResourceType (
496 ACPI_FUNCTION_ENTRY ();
500 * Byte 0 contains the descriptor name (Resource Type)
501 * Examine the large/small bit in the resource header
503 if (ACPI_GET8 (Aml
) & ACPI_RESOURCE_NAME_LARGE
)
505 /* Large Resource Type -- bits 6:0 contain the name */
507 return (ACPI_GET8 (Aml
));
511 /* Small Resource Type -- bits 6:3 contain the name */
513 return ((UINT8
) (ACPI_GET8 (Aml
) & ACPI_RESOURCE_NAME_SMALL_MASK
));
518 /*******************************************************************************
520 * FUNCTION: AcpiUtGetResourceLength
522 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
524 * RETURN: Byte Length
526 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
527 * definition, this does not include the size of the descriptor
528 * header or the length field itself.
530 ******************************************************************************/
533 AcpiUtGetResourceLength (
536 ACPI_RS_LENGTH ResourceLength
;
539 ACPI_FUNCTION_ENTRY ();
543 * Byte 0 contains the descriptor name (Resource Type)
544 * Examine the large/small bit in the resource header
546 if (ACPI_GET8 (Aml
) & ACPI_RESOURCE_NAME_LARGE
)
548 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
550 ACPI_MOVE_16_TO_16 (&ResourceLength
, ACPI_ADD_PTR (UINT8
, Aml
, 1));
555 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
557 ResourceLength
= (UINT16
) (ACPI_GET8 (Aml
) &
558 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK
);
561 return (ResourceLength
);
565 /*******************************************************************************
567 * FUNCTION: AcpiUtGetResourceHeaderLength
569 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
571 * RETURN: Length of the AML header (depends on large/small descriptor)
573 * DESCRIPTION: Get the length of the header for this resource.
575 ******************************************************************************/
578 AcpiUtGetResourceHeaderLength (
581 ACPI_FUNCTION_ENTRY ();
584 /* Examine the large/small bit in the resource header */
586 if (ACPI_GET8 (Aml
) & ACPI_RESOURCE_NAME_LARGE
)
588 return (sizeof (AML_RESOURCE_LARGE_HEADER
));
592 return (sizeof (AML_RESOURCE_SMALL_HEADER
));
597 /*******************************************************************************
599 * FUNCTION: AcpiUtGetDescriptorLength
601 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
603 * RETURN: Byte length
605 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
606 * length of the descriptor header and the length field itself.
607 * Used to walk descriptor lists.
609 ******************************************************************************/
612 AcpiUtGetDescriptorLength (
615 ACPI_FUNCTION_ENTRY ();
619 * Get the Resource Length (does not include header length) and add
620 * the header length (depends on if this is a small or large resource)
622 return (AcpiUtGetResourceLength (Aml
) +
623 AcpiUtGetResourceHeaderLength (Aml
));
627 /*******************************************************************************
629 * FUNCTION: AcpiUtGetResourceEndTag
631 * PARAMETERS: ObjDesc - The resource template buffer object
632 * EndTag - Where the pointer to the EndTag is returned
634 * RETURN: Status, pointer to the end tag
636 * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
637 * Note: allows a buffer length of zero.
639 ******************************************************************************/
642 AcpiUtGetResourceEndTag (
643 ACPI_OPERAND_OBJECT
*ObjDesc
,
649 ACPI_FUNCTION_TRACE (UtGetResourceEndTag
);
652 /* Allow a buffer length of zero */
654 if (!ObjDesc
->Buffer
.Length
)
656 *EndTag
= ObjDesc
->Buffer
.Pointer
;
657 return_ACPI_STATUS (AE_OK
);
660 /* Validate the template and get a pointer to the EndTag */
662 Status
= AcpiUtWalkAmlResources (NULL
, ObjDesc
->Buffer
.Pointer
,
663 ObjDesc
->Buffer
.Length
, NULL
, (void **) EndTag
);
665 return_ACPI_STATUS (Status
);