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.
44 #include <acpi/acpi.h>
48 #define _COMPONENT ACPI_UTILITIES
49 ACPI_MODULE_NAME("utresrc")
51 #if defined(ACPI_DEBUG_OUTPUT) || defined (ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
53 * Strings used to decode resource descriptors.
54 * Used by both the disassembler and the debugger resource dump routines
56 const char *acpi_gbl_bm_decode
[] = {
61 const char *acpi_gbl_config_decode
[] = {
62 "0 - Good Configuration",
63 "1 - Acceptable Configuration",
64 "2 - Suboptimal Configuration",
65 "3 - ***Invalid Configuration***",
68 const char *acpi_gbl_consume_decode
[] = {
73 const char *acpi_gbl_dec_decode
[] = {
78 const char *acpi_gbl_he_decode
[] = {
83 const char *acpi_gbl_io_decode
[] = {
88 const char *acpi_gbl_ll_decode
[] = {
93 const char *acpi_gbl_max_decode
[] = {
98 const char *acpi_gbl_mem_decode
[] = {
105 const char *acpi_gbl_min_decode
[] = {
110 const char *acpi_gbl_mtp_decode
[] = {
111 "AddressRangeMemory",
112 "AddressRangeReserved",
117 const char *acpi_gbl_rng_decode
[] = {
124 const char *acpi_gbl_rw_decode
[] = {
129 const char *acpi_gbl_shr_decode
[] = {
132 "ExclusiveAndWake", /* ACPI 5.0 */
133 "SharedAndWake" /* ACPI 5.0 */
136 const char *acpi_gbl_siz_decode
[] = {
143 const char *acpi_gbl_trs_decode
[] = {
148 const char *acpi_gbl_ttp_decode
[] = {
153 const char *acpi_gbl_typ_decode
[] = {
160 const char *acpi_gbl_ppc_decode
[] = {
167 const char *acpi_gbl_ior_decode
[] = {
169 "IoRestrictionInputOnly",
170 "IoRestrictionOutputOnly",
171 "IoRestrictionNoneAndPreserve"
174 const char *acpi_gbl_dts_decode
[] = {
183 /* GPIO connection type */
185 const char *acpi_gbl_ct_decode
[] = {
190 /* Serial bus type */
192 const char *acpi_gbl_sbt_decode
[] = {
193 "/* UNKNOWN serial bus type */",
199 /* I2C serial bus access mode */
201 const char *acpi_gbl_am_decode
[] = {
202 "AddressingMode7Bit",
203 "AddressingMode10Bit"
206 /* I2C serial bus slave mode */
208 const char *acpi_gbl_sm_decode
[] = {
209 "ControllerInitiated",
213 /* SPI serial bus wire mode */
215 const char *acpi_gbl_wm_decode
[] = {
220 /* SPI serial clock phase */
222 const char *acpi_gbl_cph_decode
[] = {
227 /* SPI serial bus clock polarity */
229 const char *acpi_gbl_cpo_decode
[] = {
234 /* SPI serial bus device polarity */
236 const char *acpi_gbl_dp_decode
[] = {
241 /* UART serial bus endian */
243 const char *acpi_gbl_ed_decode
[] = {
248 /* UART serial bus bits per byte */
250 const char *acpi_gbl_bpb_decode
[] = {
256 "/* UNKNOWN Bits per byte */",
257 "/* UNKNOWN Bits per byte */",
258 "/* UNKNOWN Bits per byte */"
261 /* UART serial bus stop bits */
263 const char *acpi_gbl_sb_decode
[] = {
266 "StopBitsOnePlusHalf",
270 /* UART serial bus flow control */
272 const char *acpi_gbl_fc_decode
[] = {
274 "FlowControlHardware",
276 "/* UNKNOWN flow control keyword */"
279 /* UART serial bus parity type */
281 const char *acpi_gbl_pt_decode
[] = {
287 "/* UNKNOWN parity keyword */",
288 "/* UNKNOWN parity keyword */",
289 "/* UNKNOWN parity keyword */"
295 * Base sizes of the raw AML resource descriptors, indexed by resource type.
296 * Zero indicates a reserved (and therefore invalid) resource type.
298 const u8 acpi_gbl_resource_aml_sizes
[] = {
299 /* Small descriptors */
305 ACPI_AML_SIZE_SMALL(struct aml_resource_irq
),
306 ACPI_AML_SIZE_SMALL(struct aml_resource_dma
),
307 ACPI_AML_SIZE_SMALL(struct aml_resource_start_dependent
),
308 ACPI_AML_SIZE_SMALL(struct aml_resource_end_dependent
),
309 ACPI_AML_SIZE_SMALL(struct aml_resource_io
),
310 ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_io
),
311 ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_dma
),
315 ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small
),
316 ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag
),
318 /* Large descriptors */
321 ACPI_AML_SIZE_LARGE(struct aml_resource_memory24
),
322 ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register
),
324 ACPI_AML_SIZE_LARGE(struct aml_resource_vendor_large
),
325 ACPI_AML_SIZE_LARGE(struct aml_resource_memory32
),
326 ACPI_AML_SIZE_LARGE(struct aml_resource_fixed_memory32
),
327 ACPI_AML_SIZE_LARGE(struct aml_resource_address32
),
328 ACPI_AML_SIZE_LARGE(struct aml_resource_address16
),
329 ACPI_AML_SIZE_LARGE(struct aml_resource_extended_irq
),
330 ACPI_AML_SIZE_LARGE(struct aml_resource_address64
),
331 ACPI_AML_SIZE_LARGE(struct aml_resource_extended_address64
),
332 ACPI_AML_SIZE_LARGE(struct aml_resource_gpio
),
334 ACPI_AML_SIZE_LARGE(struct aml_resource_common_serialbus
),
337 const u8 acpi_gbl_resource_aml_serial_bus_sizes
[] = {
339 ACPI_AML_SIZE_LARGE(struct aml_resource_i2c_serialbus
),
340 ACPI_AML_SIZE_LARGE(struct aml_resource_spi_serialbus
),
341 ACPI_AML_SIZE_LARGE(struct aml_resource_uart_serialbus
),
345 * Resource types, used to validate the resource length field.
346 * The length of fixed-length types must match exactly, variable
347 * lengths must meet the minimum required length, etc.
348 * Zero indicates a reserved (and therefore invalid) resource type.
350 static const u8 acpi_gbl_resource_types
[] = {
351 /* Small descriptors */
357 ACPI_SMALL_VARIABLE_LENGTH
, /* 04 IRQ */
358 ACPI_FIXED_LENGTH
, /* 05 DMA */
359 ACPI_SMALL_VARIABLE_LENGTH
, /* 06 start_dependent_functions */
360 ACPI_FIXED_LENGTH
, /* 07 end_dependent_functions */
361 ACPI_FIXED_LENGTH
, /* 08 IO */
362 ACPI_FIXED_LENGTH
, /* 09 fixed_IO */
363 ACPI_FIXED_LENGTH
, /* 0A fixed_DMA */
367 ACPI_VARIABLE_LENGTH
, /* 0E vendor_short */
368 ACPI_FIXED_LENGTH
, /* 0F end_tag */
370 /* Large descriptors */
373 ACPI_FIXED_LENGTH
, /* 01 Memory24 */
374 ACPI_FIXED_LENGTH
, /* 02 generic_register */
376 ACPI_VARIABLE_LENGTH
, /* 04 vendor_long */
377 ACPI_FIXED_LENGTH
, /* 05 Memory32 */
378 ACPI_FIXED_LENGTH
, /* 06 memory32_fixed */
379 ACPI_VARIABLE_LENGTH
, /* 07 Dword* address */
380 ACPI_VARIABLE_LENGTH
, /* 08 Word* address */
381 ACPI_VARIABLE_LENGTH
, /* 09 extended_IRQ */
382 ACPI_VARIABLE_LENGTH
, /* 0A Qword* address */
383 ACPI_FIXED_LENGTH
, /* 0B Extended* address */
384 ACPI_VARIABLE_LENGTH
, /* 0C Gpio* */
386 ACPI_VARIABLE_LENGTH
/* 0E *serial_bus */
389 /*******************************************************************************
391 * FUNCTION: acpi_ut_walk_aml_resources
393 * PARAMETERS: walk_state - Current walk info
394 * PARAMETERS: aml - Pointer to the raw AML resource template
395 * aml_length - Length of the entire template
396 * user_function - Called once for each descriptor found. If
397 * NULL, a pointer to the end_tag is returned
398 * context - Passed to user_function
402 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
403 * once for each resource found.
405 ******************************************************************************/
408 acpi_ut_walk_aml_resources(struct acpi_walk_state
*walk_state
,
410 acpi_size aml_length
,
411 acpi_walk_aml_callback user_function
, void **context
)
418 u8 end_tag
[2] = { 0x79, 0x00 };
420 ACPI_FUNCTION_TRACE(ut_walk_aml_resources
);
422 /* The absolute minimum resource template is one end_tag descriptor */
424 if (aml_length
< sizeof(struct aml_resource_end_tag
)) {
425 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG
);
428 /* Point to the end of the resource template buffer */
430 end_aml
= aml
+ aml_length
;
432 /* Walk the byte list, abort on any invalid descriptor type or length */
434 while (aml
< end_aml
) {
436 /* Validate the Resource Type and Resource Length */
439 acpi_ut_validate_resource(walk_state
, aml
, &resource_index
);
440 if (ACPI_FAILURE(status
)) {
442 * Exit on failure. Cannot continue because the descriptor length
445 return_ACPI_STATUS(status
);
448 /* Get the length of this descriptor */
450 length
= acpi_ut_get_descriptor_length(aml
);
452 /* Invoke the user function */
456 user_function(aml
, length
, offset
, resource_index
,
458 if (ACPI_FAILURE(status
)) {
459 return_ACPI_STATUS(status
);
463 /* An end_tag descriptor terminates this resource template */
465 if (acpi_ut_get_resource_type(aml
) ==
466 ACPI_RESOURCE_NAME_END_TAG
) {
468 * There must be at least one more byte in the buffer for
469 * the 2nd byte of the end_tag
471 if ((aml
+ 1) >= end_aml
) {
472 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG
);
475 /* Return the pointer to the end_tag if requested */
477 if (!user_function
) {
483 return_ACPI_STATUS(AE_OK
);
490 /* Did not find an end_tag descriptor */
494 /* Insert an end_tag anyway. acpi_rs_get_list_length always leaves room */
496 (void)acpi_ut_validate_resource(walk_state
, end_tag
,
499 user_function(end_tag
, 2, offset
, resource_index
, context
);
500 if (ACPI_FAILURE(status
)) {
501 return_ACPI_STATUS(status
);
505 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG
);
508 /*******************************************************************************
510 * FUNCTION: acpi_ut_validate_resource
512 * PARAMETERS: walk_state - Current walk info
513 * aml - Pointer to the raw AML resource descriptor
514 * return_index - Where the resource index is returned. NULL
515 * if the index is not required.
517 * RETURN: Status, and optionally the Index into the global resource tables
519 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
520 * Type and Resource Length. Returns an index into the global
521 * resource information/dispatch tables for later use.
523 ******************************************************************************/
526 acpi_ut_validate_resource(struct acpi_walk_state
*walk_state
,
527 void *aml
, u8
*return_index
)
529 union aml_resource
*aml_resource
;
532 acpi_rs_length resource_length
;
533 acpi_rs_length minimum_resource_length
;
535 ACPI_FUNCTION_ENTRY();
538 * 1) Validate the resource_type field (Byte 0)
540 resource_type
= ACPI_GET8(aml
);
543 * Byte 0 contains the descriptor name (Resource Type)
544 * Examine the large/small bit in the resource header
546 if (resource_type
& ACPI_RESOURCE_NAME_LARGE
) {
548 /* Verify the large resource type (name) against the max */
550 if (resource_type
> ACPI_RESOURCE_NAME_LARGE_MAX
) {
551 goto invalid_resource
;
555 * Large Resource Type -- bits 6:0 contain the name
556 * Translate range 0x80-0x8B to index range 0x10-0x1B
558 resource_index
= (u8
) (resource_type
- 0x70);
561 * Small Resource Type -- bits 6:3 contain the name
562 * Shift range to index range 0x00-0x0F
564 resource_index
= (u8
)
565 ((resource_type
& ACPI_RESOURCE_NAME_SMALL_MASK
) >> 3);
569 * Check validity of the resource type, via acpi_gbl_resource_types. Zero
570 * indicates an invalid resource.
572 if (!acpi_gbl_resource_types
[resource_index
]) {
573 goto invalid_resource
;
577 * Validate the resource_length field. This ensures that the length
578 * is at least reasonable, and guarantees that it is non-zero.
580 resource_length
= acpi_ut_get_resource_length(aml
);
581 minimum_resource_length
= acpi_gbl_resource_aml_sizes
[resource_index
];
583 /* Validate based upon the type of resource - fixed length or variable */
585 switch (acpi_gbl_resource_types
[resource_index
]) {
586 case ACPI_FIXED_LENGTH
:
588 /* Fixed length resource, length must match exactly */
590 if (resource_length
!= minimum_resource_length
) {
591 goto bad_resource_length
;
595 case ACPI_VARIABLE_LENGTH
:
597 /* Variable length resource, length must be at least the minimum */
599 if (resource_length
< minimum_resource_length
) {
600 goto bad_resource_length
;
604 case ACPI_SMALL_VARIABLE_LENGTH
:
606 /* Small variable length resource, length can be (Min) or (Min-1) */
608 if ((resource_length
> minimum_resource_length
) ||
609 (resource_length
< (minimum_resource_length
- 1))) {
610 goto bad_resource_length
;
616 /* Shouldn't happen (because of validation earlier), but be sure */
618 goto invalid_resource
;
621 aml_resource
= ACPI_CAST_PTR(union aml_resource
, aml
);
622 if (resource_type
== ACPI_RESOURCE_NAME_SERIAL_BUS
) {
624 /* Validate the bus_type field */
626 if ((aml_resource
->common_serial_bus
.type
== 0) ||
627 (aml_resource
->common_serial_bus
.type
>
628 AML_RESOURCE_MAX_SERIALBUSTYPE
)) {
631 "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
632 aml_resource
->common_serial_bus
.
635 return (AE_AML_INVALID_RESOURCE_TYPE
);
639 /* Optionally return the resource table index */
642 *return_index
= resource_index
;
651 "Invalid/unsupported resource descriptor: Type 0x%2.2X",
654 return (AE_AML_INVALID_RESOURCE_TYPE
);
660 "Invalid resource descriptor length: Type "
661 "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
662 resource_type
, resource_length
,
663 minimum_resource_length
));
665 return (AE_AML_BAD_RESOURCE_LENGTH
);
668 /*******************************************************************************
670 * FUNCTION: acpi_ut_get_resource_type
672 * PARAMETERS: aml - Pointer to the raw AML resource descriptor
674 * RETURN: The Resource Type with no extraneous bits (except the
675 * Large/Small descriptor bit -- this is left alone)
677 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
678 * a resource descriptor.
680 ******************************************************************************/
682 u8
acpi_ut_get_resource_type(void *aml
)
684 ACPI_FUNCTION_ENTRY();
687 * Byte 0 contains the descriptor name (Resource Type)
688 * Examine the large/small bit in the resource header
690 if (ACPI_GET8(aml
) & ACPI_RESOURCE_NAME_LARGE
) {
692 /* Large Resource Type -- bits 6:0 contain the name */
694 return (ACPI_GET8(aml
));
696 /* Small Resource Type -- bits 6:3 contain the name */
698 return ((u8
) (ACPI_GET8(aml
) & ACPI_RESOURCE_NAME_SMALL_MASK
));
702 /*******************************************************************************
704 * FUNCTION: acpi_ut_get_resource_length
706 * PARAMETERS: aml - Pointer to the raw AML resource descriptor
708 * RETURN: Byte Length
710 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
711 * definition, this does not include the size of the descriptor
712 * header or the length field itself.
714 ******************************************************************************/
716 u16
acpi_ut_get_resource_length(void *aml
)
718 acpi_rs_length resource_length
;
720 ACPI_FUNCTION_ENTRY();
723 * Byte 0 contains the descriptor name (Resource Type)
724 * Examine the large/small bit in the resource header
726 if (ACPI_GET8(aml
) & ACPI_RESOURCE_NAME_LARGE
) {
728 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
730 ACPI_MOVE_16_TO_16(&resource_length
, ACPI_ADD_PTR(u8
, aml
, 1));
733 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
735 resource_length
= (u16
) (ACPI_GET8(aml
) &
736 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK
);
739 return (resource_length
);
742 /*******************************************************************************
744 * FUNCTION: acpi_ut_get_resource_header_length
746 * PARAMETERS: aml - Pointer to the raw AML resource descriptor
748 * RETURN: Length of the AML header (depends on large/small descriptor)
750 * DESCRIPTION: Get the length of the header for this resource.
752 ******************************************************************************/
754 u8
acpi_ut_get_resource_header_length(void *aml
)
756 ACPI_FUNCTION_ENTRY();
758 /* Examine the large/small bit in the resource header */
760 if (ACPI_GET8(aml
) & ACPI_RESOURCE_NAME_LARGE
) {
761 return (sizeof(struct aml_resource_large_header
));
763 return (sizeof(struct aml_resource_small_header
));
767 /*******************************************************************************
769 * FUNCTION: acpi_ut_get_descriptor_length
771 * PARAMETERS: aml - Pointer to the raw AML resource descriptor
773 * RETURN: Byte length
775 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
776 * length of the descriptor header and the length field itself.
777 * Used to walk descriptor lists.
779 ******************************************************************************/
781 u32
acpi_ut_get_descriptor_length(void *aml
)
783 ACPI_FUNCTION_ENTRY();
786 * Get the Resource Length (does not include header length) and add
787 * the header length (depends on if this is a small or large resource)
789 return (acpi_ut_get_resource_length(aml
) +
790 acpi_ut_get_resource_header_length(aml
));
793 /*******************************************************************************
795 * FUNCTION: acpi_ut_get_resource_end_tag
797 * PARAMETERS: obj_desc - The resource template buffer object
798 * end_tag - Where the pointer to the end_tag is returned
800 * RETURN: Status, pointer to the end tag
802 * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
803 * Note: allows a buffer length of zero.
805 ******************************************************************************/
808 acpi_ut_get_resource_end_tag(union acpi_operand_object
*obj_desc
, u8
**end_tag
)
812 ACPI_FUNCTION_TRACE(ut_get_resource_end_tag
);
814 /* Allow a buffer length of zero */
816 if (!obj_desc
->buffer
.length
) {
817 *end_tag
= obj_desc
->buffer
.pointer
;
818 return_ACPI_STATUS(AE_OK
);
821 /* Validate the template and get a pointer to the end_tag */
823 status
= acpi_ut_walk_aml_resources(NULL
, obj_desc
->buffer
.pointer
,
824 obj_desc
->buffer
.length
, NULL
,
827 return_ACPI_STATUS(status
);