1 /*******************************************************************************
3 * Module Name: utmisc - common utility procedures
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2012, 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 <linux/module.h>
46 #include <acpi/acpi.h>
50 #define _COMPONENT ACPI_UTILITIES
51 ACPI_MODULE_NAME("utmisc")
53 #if defined ACPI_ASL_COMPILER || defined ACPI_EXEC_APP
54 /*******************************************************************************
56 * FUNCTION: ut_convert_backslashes
58 * PARAMETERS: pathname - File pathname string to be converted
60 * RETURN: Modifies the input Pathname
62 * DESCRIPTION: Convert all backslashes (0x5C) to forward slashes (0x2F) within
63 * the entire input file pathname string.
65 ******************************************************************************/
66 void ut_convert_backslashes(char *pathname
)
74 if (*pathname
== '\\') {
83 /*******************************************************************************
85 * FUNCTION: acpi_ut_is_pci_root_bridge
87 * PARAMETERS: id - The HID/CID in string format
89 * RETURN: TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
91 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
93 ******************************************************************************/
95 u8
acpi_ut_is_pci_root_bridge(char *id
)
99 * Check if this is a PCI root bridge.
100 * ACPI 3.0+: check for a PCI Express root also.
102 if (!(ACPI_STRCMP(id
,
103 PCI_ROOT_HID_STRING
)) ||
104 !(ACPI_STRCMP(id
, PCI_EXPRESS_ROOT_HID_STRING
))) {
111 /*******************************************************************************
113 * FUNCTION: acpi_ut_is_aml_table
115 * PARAMETERS: table - An ACPI table
117 * RETURN: TRUE if table contains executable AML; FALSE otherwise
119 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
120 * Currently, these are DSDT,SSDT,PSDT. All other table types are
121 * data tables that do not contain AML code.
123 ******************************************************************************/
125 u8
acpi_ut_is_aml_table(struct acpi_table_header
*table
)
128 /* These are the only tables that contain executable AML */
130 if (ACPI_COMPARE_NAME(table
->signature
, ACPI_SIG_DSDT
) ||
131 ACPI_COMPARE_NAME(table
->signature
, ACPI_SIG_PSDT
) ||
132 ACPI_COMPARE_NAME(table
->signature
, ACPI_SIG_SSDT
)) {
139 /*******************************************************************************
141 * FUNCTION: acpi_ut_allocate_owner_id
143 * PARAMETERS: owner_id - Where the new owner ID is returned
147 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
148 * track objects created by the table or method, to be deleted
149 * when the method exits or the table is unloaded.
151 ******************************************************************************/
153 acpi_status
acpi_ut_allocate_owner_id(acpi_owner_id
* owner_id
)
160 ACPI_FUNCTION_TRACE(ut_allocate_owner_id
);
162 /* Guard against multiple allocations of ID to the same location */
165 ACPI_ERROR((AE_INFO
, "Owner ID [0x%2.2X] already exists",
167 return_ACPI_STATUS(AE_ALREADY_EXISTS
);
170 /* Mutex for the global ID mask */
172 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
173 if (ACPI_FAILURE(status
)) {
174 return_ACPI_STATUS(status
);
178 * Find a free owner ID, cycle through all possible IDs on repeated
179 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
180 * to be scanned twice.
182 for (i
= 0, j
= acpi_gbl_last_owner_id_index
;
183 i
< (ACPI_NUM_OWNERID_MASKS
+ 1); i
++, j
++) {
184 if (j
>= ACPI_NUM_OWNERID_MASKS
) {
185 j
= 0; /* Wraparound to start of mask array */
188 for (k
= acpi_gbl_next_owner_id_offset
; k
< 32; k
++) {
189 if (acpi_gbl_owner_id_mask
[j
] == ACPI_UINT32_MAX
) {
191 /* There are no free IDs in this mask */
196 if (!(acpi_gbl_owner_id_mask
[j
] & (1 << k
))) {
198 * Found a free ID. The actual ID is the bit index plus one,
199 * making zero an invalid Owner ID. Save this as the last ID
200 * allocated and update the global ID mask.
202 acpi_gbl_owner_id_mask
[j
] |= (1 << k
);
204 acpi_gbl_last_owner_id_index
= (u8
) j
;
205 acpi_gbl_next_owner_id_offset
= (u8
) (k
+ 1);
208 * Construct encoded ID from the index and bit position
210 * Note: Last [j].k (bit 255) is never used and is marked
211 * permanently allocated (prevents +1 overflow)
214 (acpi_owner_id
) ((k
+ 1) + ACPI_MUL_32(j
));
216 ACPI_DEBUG_PRINT((ACPI_DB_VALUES
,
217 "Allocated OwnerId: %2.2X\n",
218 (unsigned int)*owner_id
));
223 acpi_gbl_next_owner_id_offset
= 0;
227 * All owner_ids have been allocated. This typically should
228 * not happen since the IDs are reused after deallocation. The IDs are
229 * allocated upon table load (one per table) and method execution, and
230 * they are released when a table is unloaded or a method completes
233 * If this error happens, there may be very deep nesting of invoked control
234 * methods, or there may be a bug where the IDs are not released.
236 status
= AE_OWNER_ID_LIMIT
;
238 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
241 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
242 return_ACPI_STATUS(status
);
245 /*******************************************************************************
247 * FUNCTION: acpi_ut_release_owner_id
249 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_ID
251 * RETURN: None. No error is returned because we are either exiting a
252 * control method or unloading a table. Either way, we would
253 * ignore any error anyway.
255 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
257 ******************************************************************************/
259 void acpi_ut_release_owner_id(acpi_owner_id
* owner_id_ptr
)
261 acpi_owner_id owner_id
= *owner_id_ptr
;
266 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id
, owner_id
);
268 /* Always clear the input owner_id (zero is an invalid ID) */
272 /* Zero is not a valid owner_ID */
275 ACPI_ERROR((AE_INFO
, "Invalid OwnerId: 0x%2.2X", owner_id
));
279 /* Mutex for the global ID mask */
281 status
= acpi_ut_acquire_mutex(ACPI_MTX_CACHES
);
282 if (ACPI_FAILURE(status
)) {
286 /* Normalize the ID to zero */
290 /* Decode ID to index/offset pair */
292 index
= ACPI_DIV_32(owner_id
);
293 bit
= 1 << ACPI_MOD_32(owner_id
);
295 /* Free the owner ID only if it is valid */
297 if (acpi_gbl_owner_id_mask
[index
] & bit
) {
298 acpi_gbl_owner_id_mask
[index
] ^= bit
;
301 "Release of non-allocated OwnerId: 0x%2.2X",
305 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES
);
309 /*******************************************************************************
311 * FUNCTION: acpi_ut_strupr (strupr)
313 * PARAMETERS: src_string - The source string to convert
317 * DESCRIPTION: Convert string to uppercase
319 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
321 ******************************************************************************/
323 void acpi_ut_strupr(char *src_string
)
327 ACPI_FUNCTION_ENTRY();
333 /* Walk entire string, uppercasing the letters */
335 for (string
= src_string
; *string
; string
++) {
336 *string
= (char)ACPI_TOUPPER(*string
);
342 /*******************************************************************************
344 * FUNCTION: acpi_ut_print_string
346 * PARAMETERS: string - Null terminated ASCII string
347 * max_length - Maximum output length
351 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
354 ******************************************************************************/
356 void acpi_ut_print_string(char *string
, u8 max_length
)
361 acpi_os_printf("<\"NULL STRING PTR\">");
365 acpi_os_printf("\"");
366 for (i
= 0; string
[i
] && (i
< max_length
); i
++) {
368 /* Escape sequences */
372 acpi_os_printf("\\a"); /* BELL */
376 acpi_os_printf("\\b"); /* BACKSPACE */
380 acpi_os_printf("\\f"); /* FORMFEED */
384 acpi_os_printf("\\n"); /* LINEFEED */
388 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
392 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
396 acpi_os_printf("\\v"); /* VERTICAL TAB */
399 case '\'': /* Single Quote */
400 case '\"': /* Double Quote */
401 case '\\': /* Backslash */
402 acpi_os_printf("\\%c", (int)string
[i
]);
407 /* Check for printable character or hex escape */
409 if (ACPI_IS_PRINT(string
[i
])) {
410 /* This is a normal character */
412 acpi_os_printf("%c", (int)string
[i
]);
414 /* All others will be Hex escapes */
416 acpi_os_printf("\\x%2.2X", (s32
) string
[i
]);
421 acpi_os_printf("\"");
423 if (i
== max_length
&& string
[i
]) {
424 acpi_os_printf("...");
428 /*******************************************************************************
430 * FUNCTION: acpi_ut_dword_byte_swap
432 * PARAMETERS: value - Value to be converted
434 * RETURN: u32 integer with bytes swapped
436 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
438 ******************************************************************************/
440 u32
acpi_ut_dword_byte_swap(u32 value
)
451 ACPI_FUNCTION_ENTRY();
455 out
.bytes
[0] = in
.bytes
[3];
456 out
.bytes
[1] = in
.bytes
[2];
457 out
.bytes
[2] = in
.bytes
[1];
458 out
.bytes
[3] = in
.bytes
[0];
463 /*******************************************************************************
465 * FUNCTION: acpi_ut_set_integer_width
467 * PARAMETERS: Revision From DSDT header
471 * DESCRIPTION: Set the global integer bit width based upon the revision
472 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
473 * For Revision 2 and above, Integers are 64 bits. Yes, this
474 * makes a difference.
476 ******************************************************************************/
478 void acpi_ut_set_integer_width(u8 revision
)
485 acpi_gbl_integer_bit_width
= 32;
486 acpi_gbl_integer_nybble_width
= 8;
487 acpi_gbl_integer_byte_width
= 4;
489 /* 64-bit case (ACPI 2.0+) */
491 acpi_gbl_integer_bit_width
= 64;
492 acpi_gbl_integer_nybble_width
= 16;
493 acpi_gbl_integer_byte_width
= 8;
497 #ifdef ACPI_DEBUG_OUTPUT
498 /*******************************************************************************
500 * FUNCTION: acpi_ut_display_init_pathname
502 * PARAMETERS: type - Object type of the node
503 * obj_handle - Handle whose pathname will be displayed
504 * path - Additional path string to be appended.
505 * (NULL if no extra path)
507 * RETURN: acpi_status
509 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
511 ******************************************************************************/
514 acpi_ut_display_init_pathname(u8 type
,
515 struct acpi_namespace_node
*obj_handle
,
519 struct acpi_buffer buffer
;
521 ACPI_FUNCTION_ENTRY();
523 /* Only print the path if the appropriate debug level is enabled */
525 if (!(acpi_dbg_level
& ACPI_LV_INIT_NAMES
)) {
529 /* Get the full pathname to the node */
531 buffer
.length
= ACPI_ALLOCATE_LOCAL_BUFFER
;
532 status
= acpi_ns_handle_to_pathname(obj_handle
, &buffer
);
533 if (ACPI_FAILURE(status
)) {
537 /* Print what we're doing */
540 case ACPI_TYPE_METHOD
:
541 acpi_os_printf("Executing ");
545 acpi_os_printf("Initializing ");
549 /* Print the object type and pathname */
551 acpi_os_printf("%-12s %s",
552 acpi_ut_get_type_name(type
), (char *)buffer
.pointer
);
554 /* Extra path is used to append names like _STA, _INI, etc. */
557 acpi_os_printf(".%s", path
);
559 acpi_os_printf("\n");
561 ACPI_FREE(buffer
.pointer
);
565 /*******************************************************************************
567 * FUNCTION: acpi_ut_valid_acpi_char
569 * PARAMETERS: char - The character to be examined
570 * position - Byte position (0-3)
572 * RETURN: TRUE if the character is valid, FALSE otherwise
574 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
575 * 1) Upper case alpha
579 * We allow a '!' as the last character because of the ASF! table
581 ******************************************************************************/
583 u8
acpi_ut_valid_acpi_char(char character
, u32 position
)
586 if (!((character
>= 'A' && character
<= 'Z') ||
587 (character
>= '0' && character
<= '9') || (character
== '_'))) {
589 /* Allow a '!' in the last position */
591 if (character
== '!' && position
== 3) {
601 /*******************************************************************************
603 * FUNCTION: acpi_ut_valid_acpi_name
605 * PARAMETERS: name - The name to be examined
607 * RETURN: TRUE if the name is valid, FALSE otherwise
609 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
610 * 1) Upper case alpha
614 ******************************************************************************/
616 u8
acpi_ut_valid_acpi_name(u32 name
)
620 ACPI_FUNCTION_ENTRY();
622 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++) {
623 if (!acpi_ut_valid_acpi_char
624 ((ACPI_CAST_PTR(char, &name
))[i
], i
)) {
632 /*******************************************************************************
634 * FUNCTION: acpi_ut_repair_name
636 * PARAMETERS: name - The ACPI name to be repaired
638 * RETURN: Repaired version of the name
640 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
641 * return the new name.
643 ******************************************************************************/
645 acpi_name
acpi_ut_repair_name(char *name
)
648 char new_name
[ACPI_NAME_SIZE
];
650 for (i
= 0; i
< ACPI_NAME_SIZE
; i
++) {
651 new_name
[i
] = name
[i
];
654 * Replace a bad character with something printable, yet technically
655 * still invalid. This prevents any collisions with existing "good"
656 * names in the namespace.
658 if (!acpi_ut_valid_acpi_char(name
[i
], i
)) {
663 return (*(u32
*) new_name
);
666 /*******************************************************************************
668 * FUNCTION: acpi_ut_strtoul64
670 * PARAMETERS: string - Null terminated string
671 * base - Radix of the string: 16 or ACPI_ANY_BASE;
672 * ACPI_ANY_BASE means 'in behalf of to_integer'
673 * ret_integer - Where the converted integer is returned
675 * RETURN: Status and Converted value
677 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
678 * 32-bit or 64-bit conversion, depending on the current mode
679 * of the interpreter.
680 * NOTE: Does not support Octal strings, not needed.
682 ******************************************************************************/
684 acpi_status
acpi_ut_strtoul64(char *string
, u32 base
, u64
* ret_integer
)
687 u64 return_value
= 0;
690 u32 to_integer_op
= (base
== ACPI_ANY_BASE
);
691 u32 mode32
= (acpi_gbl_integer_byte_width
== 4);
696 ACPI_FUNCTION_TRACE_STR(ut_stroul64
, string
);
705 return_ACPI_STATUS(AE_BAD_PARAMETER
);
712 /* Skip over any white space in the buffer */
714 while ((*string
) && (ACPI_IS_SPACE(*string
) || *string
== '\t')) {
720 * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
721 * We need to determine if it is decimal or hexadecimal.
723 if ((*string
== '0') && (ACPI_TOLOWER(*(string
+ 1)) == 'x')) {
727 /* Skip over the leading '0x' */
734 /* Any string left? Check that '0x' is not followed by white space. */
736 if (!(*string
) || ACPI_IS_SPACE(*string
) || *string
== '\t') {
745 * Perform a 32-bit or 64-bit conversion, depending upon the current
746 * execution mode of the interpreter
748 dividend
= (mode32
) ? ACPI_UINT32_MAX
: ACPI_UINT64_MAX
;
750 /* Main loop: convert the string to a 32- or 64-bit integer */
753 if (ACPI_IS_DIGIT(*string
)) {
755 /* Convert ASCII 0-9 to Decimal value */
757 this_digit
= ((u8
) * string
) - '0';
758 } else if (base
== 10) {
760 /* Digit is out of range; possible in to_integer case only */
764 this_digit
= (u8
) ACPI_TOUPPER(*string
);
765 if (ACPI_IS_XDIGIT((char)this_digit
)) {
767 /* Convert ASCII Hex char to value */
769 this_digit
= this_digit
- 'A' + 10;
781 } else if ((valid_digits
== 0) && (this_digit
== 0)
791 if (sign_of0x
&& ((valid_digits
> 16)
792 || ((valid_digits
> 8) && mode32
))) {
794 * This is to_integer operation case.
795 * No any restrictions for string-to-integer conversion,
801 /* Divide the digit into the correct position */
803 (void)acpi_ut_short_divide((dividend
- (u64
) this_digit
),
804 base
, "ient
, NULL
);
806 if (return_value
> quotient
) {
814 return_value
*= base
;
815 return_value
+= this_digit
;
819 /* All done, normal exit */
823 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
, "Converted value: %8.8X%8.8X\n",
824 ACPI_FORMAT_UINT64(return_value
)));
826 *ret_integer
= return_value
;
827 return_ACPI_STATUS(AE_OK
);
830 /* Base was set/validated above */
833 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT
);
835 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT
);
839 /*******************************************************************************
841 * FUNCTION: acpi_ut_create_update_state_and_push
843 * PARAMETERS: object - Object to be added to the new state
844 * action - Increment/Decrement
845 * state_list - List the state will be added to
849 * DESCRIPTION: Create a new state and push it
851 ******************************************************************************/
854 acpi_ut_create_update_state_and_push(union acpi_operand_object
*object
,
856 union acpi_generic_state
**state_list
)
858 union acpi_generic_state
*state
;
860 ACPI_FUNCTION_ENTRY();
862 /* Ignore null objects; these are expected */
868 state
= acpi_ut_create_update_state(object
, action
);
870 return (AE_NO_MEMORY
);
873 acpi_ut_push_generic_state(state_list
, state
);
877 /*******************************************************************************
879 * FUNCTION: acpi_ut_walk_package_tree
881 * PARAMETERS: source_object - The package to walk
882 * target_object - Target object (if package is being copied)
883 * walk_callback - Called once for each package element
884 * context - Passed to the callback function
888 * DESCRIPTION: Walk through a package
890 ******************************************************************************/
893 acpi_ut_walk_package_tree(union acpi_operand_object
* source_object
,
895 acpi_pkg_callback walk_callback
, void *context
)
897 acpi_status status
= AE_OK
;
898 union acpi_generic_state
*state_list
= NULL
;
899 union acpi_generic_state
*state
;
901 union acpi_operand_object
*this_source_obj
;
903 ACPI_FUNCTION_TRACE(ut_walk_package_tree
);
905 state
= acpi_ut_create_pkg_state(source_object
, target_object
, 0);
907 return_ACPI_STATUS(AE_NO_MEMORY
);
912 /* Get one element of the package */
914 this_index
= state
->pkg
.index
;
915 this_source_obj
= (union acpi_operand_object
*)
916 state
->pkg
.source_object
->package
.elements
[this_index
];
920 * 1) An uninitialized package element. It is completely
921 * legal to declare a package and leave it uninitialized
922 * 2) Not an internal object - can be a namespace node instead
923 * 3) Any type other than a package. Packages are handled in else
926 if ((!this_source_obj
) ||
927 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj
) !=
928 ACPI_DESC_TYPE_OPERAND
)
929 || (this_source_obj
->common
.type
!= ACPI_TYPE_PACKAGE
)) {
931 walk_callback(ACPI_COPY_TYPE_SIMPLE
,
932 this_source_obj
, state
, context
);
933 if (ACPI_FAILURE(status
)) {
934 return_ACPI_STATUS(status
);
938 while (state
->pkg
.index
>=
939 state
->pkg
.source_object
->package
.count
) {
941 * We've handled all of the objects at this level, This means
942 * that we have just completed a package. That package may
943 * have contained one or more packages itself.
945 * Delete this state and pop the previous state (package).
947 acpi_ut_delete_generic_state(state
);
948 state
= acpi_ut_pop_generic_state(&state_list
);
950 /* Finished when there are no more states */
954 * We have handled all of the objects in the top level
955 * package just add the length of the package objects
958 return_ACPI_STATUS(AE_OK
);
962 * Go back up a level and move the index past the just
963 * completed package object.
968 /* This is a subobject of type package */
971 walk_callback(ACPI_COPY_TYPE_PACKAGE
,
972 this_source_obj
, state
, context
);
973 if (ACPI_FAILURE(status
)) {
974 return_ACPI_STATUS(status
);
978 * Push the current state and create a new one
979 * The callback above returned a new target package object.
981 acpi_ut_push_generic_state(&state_list
, state
);
982 state
= acpi_ut_create_pkg_state(this_source_obj
,
987 /* Free any stacked Update State objects */
991 acpi_ut_pop_generic_state
993 acpi_ut_delete_generic_state(state
);
995 return_ACPI_STATUS(AE_NO_MEMORY
);
1000 /* We should never get here */
1002 return_ACPI_STATUS(AE_AML_INTERNAL
);