1 /*******************************************************************************
3 * Module Name: utmisc - common utility procedures
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2006, R. Byron Moore
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>
47 #include <acpi/acnamesp.h>
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME("utmisc")
52 /*******************************************************************************
54 * FUNCTION: acpi_ut_is_aml_table
56 * PARAMETERS: Table - An ACPI table
58 * RETURN: TRUE if table contains executable AML; FALSE otherwise
60 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
61 * Currently, these are DSDT,SSDT,PSDT. All other table types are
62 * data tables that do not contain AML code.
64 ******************************************************************************/
65 u8 acpi_ut_is_aml_table(struct acpi_table_header *table)
68 /* These are the only tables that contain executable AML */
70 if (ACPI_COMPARE_NAME(table->signature, DSDT_SIG) ||
71 ACPI_COMPARE_NAME(table->signature, PSDT_SIG) ||
72 ACPI_COMPARE_NAME(table->signature, SSDT_SIG)) {
79 /*******************************************************************************
81 * FUNCTION: acpi_ut_allocate_owner_id
83 * PARAMETERS: owner_id - Where the new owner ID is returned
87 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
88 * track objects created by the table or method, to be deleted
89 * when the method exits or the table is unloaded.
91 ******************************************************************************/
93 acpi_status acpi_ut_allocate_owner_id(acpi_owner_id * owner_id)
100 ACPI_FUNCTION_TRACE(ut_allocate_owner_id);
102 /* Guard against multiple allocations of ID to the same location */
105 ACPI_ERROR((AE_INFO, "Owner ID [%2.2X] already exists",
107 return_ACPI_STATUS(AE_ALREADY_EXISTS);
110 /* Mutex for the global ID mask */
112 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
113 if (ACPI_FAILURE(status)) {
114 return_ACPI_STATUS(status);
118 * Find a free owner ID, cycle through all possible IDs on repeated
119 * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
120 * to be scanned twice.
122 for (i = 0, j = acpi_gbl_last_owner_id_index;
123 i < (ACPI_NUM_OWNERID_MASKS + 1); i++, j++) {
124 if (j >= ACPI_NUM_OWNERID_MASKS) {
125 j = 0; /* Wraparound to start of mask array */
128 for (k = acpi_gbl_next_owner_id_offset; k < 32; k++) {
129 if (acpi_gbl_owner_id_mask[j] == ACPI_UINT32_MAX) {
131 /* There are no free IDs in this mask */
136 if (!(acpi_gbl_owner_id_mask[j] & (1 << k))) {
138 * Found a free ID. The actual ID is the bit index plus one,
139 * making zero an invalid Owner ID. Save this as the last ID
140 * allocated and update the global ID mask.
142 acpi_gbl_owner_id_mask[j] |= (1 << k);
144 acpi_gbl_last_owner_id_index = (u8) j;
145 acpi_gbl_next_owner_id_offset = (u8) (k + 1);
148 * Construct encoded ID from the index and bit position
150 * Note: Last [j].k (bit 255) is never used and is marked
151 * permanently allocated (prevents +1 overflow)
154 (acpi_owner_id) ((k + 1) + ACPI_MUL_32(j));
156 ACPI_DEBUG_PRINT((ACPI_DB_VALUES,
157 "Allocated OwnerId: %2.2X\n",
158 (unsigned int)*owner_id));
163 acpi_gbl_next_owner_id_offset = 0;
167 * All owner_ids have been allocated. This typically should
168 * not happen since the IDs are reused after deallocation. The IDs are
169 * allocated upon table load (one per table) and method execution, and
170 * they are released when a table is unloaded or a method completes
173 * If this error happens, there may be very deep nesting of invoked control
174 * methods, or there may be a bug where the IDs are not released.
176 status = AE_OWNER_ID_LIMIT;
178 "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
181 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
182 return_ACPI_STATUS(status);
185 /*******************************************************************************
187 * FUNCTION: acpi_ut_release_owner_id
189 * PARAMETERS: owner_id_ptr - Pointer to a previously allocated owner_iD
191 * RETURN: None. No error is returned because we are either exiting a
192 * control method or unloading a table. Either way, we would
193 * ignore any error anyway.
195 * DESCRIPTION: Release a table or method owner ID. Valid IDs are 1 - 255
197 ******************************************************************************/
199 void acpi_ut_release_owner_id(acpi_owner_id * owner_id_ptr)
201 acpi_owner_id owner_id = *owner_id_ptr;
203 acpi_native_uint index;
206 ACPI_FUNCTION_TRACE_U32(ut_release_owner_id, owner_id);
208 /* Always clear the input owner_id (zero is an invalid ID) */
212 /* Zero is not a valid owner_iD */
215 ACPI_ERROR((AE_INFO, "Invalid OwnerId: %2.2X", owner_id));
219 /* Mutex for the global ID mask */
221 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
222 if (ACPI_FAILURE(status)) {
226 /* Normalize the ID to zero */
230 /* Decode ID to index/offset pair */
232 index = ACPI_DIV_32(owner_id);
233 bit = 1 << ACPI_MOD_32(owner_id);
235 /* Free the owner ID only if it is valid */
237 if (acpi_gbl_owner_id_mask[index] & bit) {
238 acpi_gbl_owner_id_mask[index] ^= bit;
241 "Release of non-allocated OwnerId: %2.2X",
245 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
249 /*******************************************************************************
251 * FUNCTION: acpi_ut_strupr (strupr)
253 * PARAMETERS: src_string - The source string to convert
257 * DESCRIPTION: Convert string to uppercase
259 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
261 ******************************************************************************/
263 void acpi_ut_strupr(char *src_string)
267 ACPI_FUNCTION_ENTRY();
273 /* Walk entire string, uppercasing the letters */
275 for (string = src_string; *string; string++) {
276 *string = (char)ACPI_TOUPPER(*string);
282 /*******************************************************************************
284 * FUNCTION: acpi_ut_print_string
286 * PARAMETERS: String - Null terminated ASCII string
287 * max_length - Maximum output length
291 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
294 ******************************************************************************/
296 void acpi_ut_print_string(char *string, u8 max_length)
301 acpi_os_printf("<\"NULL STRING PTR\">");
305 acpi_os_printf("\"");
306 for (i = 0; string[i] && (i < max_length); i++) {
308 /* Escape sequences */
312 acpi_os_printf("\\a"); /* BELL */
316 acpi_os_printf("\\b"); /* BACKSPACE */
320 acpi_os_printf("\\f"); /* FORMFEED */
324 acpi_os_printf("\\n"); /* LINEFEED */
328 acpi_os_printf("\\r"); /* CARRIAGE RETURN */
332 acpi_os_printf("\\t"); /* HORIZONTAL TAB */
336 acpi_os_printf("\\v"); /* VERTICAL TAB */
339 case '\'': /* Single Quote */
340 case '\"': /* Double Quote */
341 case '\\': /* Backslash */
342 acpi_os_printf("\\%c", (int)string[i]);
347 /* Check for printable character or hex escape */
349 if (ACPI_IS_PRINT(string[i])) {
350 /* This is a normal character */
352 acpi_os_printf("%c", (int)string[i]);
354 /* All others will be Hex escapes */
356 acpi_os_printf("\\x%2.2X", (s32) string[i]);
361 acpi_os_printf("\"");
363 if (i == max_length && string[i]) {
364 acpi_os_printf("...");
368 /*******************************************************************************
370 * FUNCTION: acpi_ut_dword_byte_swap
372 * PARAMETERS: Value - Value to be converted
374 * RETURN: u32 integer with bytes swapped
376 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
378 ******************************************************************************/
380 u32 acpi_ut_dword_byte_swap(u32 value)
391 ACPI_FUNCTION_ENTRY();
395 out.bytes[0] = in.bytes[3];
396 out.bytes[1] = in.bytes[2];
397 out.bytes[2] = in.bytes[1];
398 out.bytes[3] = in.bytes[0];
403 /*******************************************************************************
405 * FUNCTION: acpi_ut_set_integer_width
407 * PARAMETERS: Revision From DSDT header
411 * DESCRIPTION: Set the global integer bit width based upon the revision
412 * of the DSDT. For Revision 1 and 0, Integers are 32 bits.
413 * For Revision 2 and above, Integers are 64 bits. Yes, this
414 * makes a difference.
416 ******************************************************************************/
418 void acpi_ut_set_integer_width(u8 revision)
425 acpi_gbl_integer_bit_width = 32;
426 acpi_gbl_integer_nybble_width = 8;
427 acpi_gbl_integer_byte_width = 4;
429 /* 64-bit case (ACPI 2.0+) */
431 acpi_gbl_integer_bit_width = 64;
432 acpi_gbl_integer_nybble_width = 16;
433 acpi_gbl_integer_byte_width = 8;
437 #ifdef ACPI_DEBUG_OUTPUT
438 /*******************************************************************************
440 * FUNCTION: acpi_ut_display_init_pathname
442 * PARAMETERS: Type - Object type of the node
443 * obj_handle - Handle whose pathname will be displayed
444 * Path - Additional path string to be appended.
445 * (NULL if no extra path)
447 * RETURN: acpi_status
449 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
451 ******************************************************************************/
454 acpi_ut_display_init_pathname(u8 type,
455 struct acpi_namespace_node *obj_handle,
459 struct acpi_buffer buffer;
461 ACPI_FUNCTION_ENTRY();
463 /* Only print the path if the appropriate debug level is enabled */
465 if (!(acpi_dbg_level & ACPI_LV_INIT_NAMES)) {
469 /* Get the full pathname to the node */
471 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
472 status = acpi_ns_handle_to_pathname(obj_handle, &buffer);
473 if (ACPI_FAILURE(status)) {
477 /* Print what we're doing */
480 case ACPI_TYPE_METHOD:
481 acpi_os_printf("Executing ");
485 acpi_os_printf("Initializing ");
489 /* Print the object type and pathname */
491 acpi_os_printf("%-12s %s",
492 acpi_ut_get_type_name(type), (char *)buffer.pointer);
494 /* Extra path is used to append names like _STA, _INI, etc. */
497 acpi_os_printf(".%s", path);
499 acpi_os_printf("\n");
501 ACPI_FREE(buffer.pointer);
505 /*******************************************************************************
507 * FUNCTION: acpi_ut_valid_acpi_char
509 * PARAMETERS: Char - The character to be examined
510 * Position - Byte position (0-3)
512 * RETURN: TRUE if the character is valid, FALSE otherwise
514 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
515 * 1) Upper case alpha
519 * We allow a '!' as the last character because of the ASF! table
521 ******************************************************************************/
523 u8 acpi_ut_valid_acpi_char(char character, acpi_native_uint position)
526 if (!((character >= 'A' && character <= 'Z') ||
527 (character >= '0' && character <= '9') || (character == '_'))) {
529 /* Allow a '!' in the last position */
531 if (character == '!' && position == 3) {
541 /*******************************************************************************
543 * FUNCTION: acpi_ut_valid_acpi_name
545 * PARAMETERS: Name - The name to be examined
547 * RETURN: TRUE if the name is valid, FALSE otherwise
549 * DESCRIPTION: Check for a valid ACPI name. Each character must be one of:
550 * 1) Upper case alpha
554 ******************************************************************************/
556 u8 acpi_ut_valid_acpi_name(u32 name)
560 ACPI_FUNCTION_ENTRY();
562 for (i = 0; i < ACPI_NAME_SIZE; i++) {
563 if (!acpi_ut_valid_acpi_char
564 ((ACPI_CAST_PTR(char, &name))[i], i)) {
572 /*******************************************************************************
574 * FUNCTION: acpi_ut_repair_name
576 * PARAMETERS: Name - The ACPI name to be repaired
578 * RETURN: Repaired version of the name
580 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
581 * return the new name.
583 ******************************************************************************/
585 acpi_name acpi_ut_repair_name(acpi_name name)
587 char *name_ptr = ACPI_CAST_PTR(char, &name);
588 char new_name[ACPI_NAME_SIZE];
591 for (i = 0; i < ACPI_NAME_SIZE; i++) {
592 new_name[i] = name_ptr[i];
595 * Replace a bad character with something printable, yet technically
596 * still invalid. This prevents any collisions with existing "good"
597 * names in the namespace.
599 if (!acpi_ut_valid_acpi_char(name_ptr[i], i)) {
604 return (*ACPI_CAST_PTR(u32, new_name));
607 /*******************************************************************************
609 * FUNCTION: acpi_ut_strtoul64
611 * PARAMETERS: String - Null terminated string
612 * Base - Radix of the string: 16 or ACPI_ANY_BASE;
613 * ACPI_ANY_BASE means 'in behalf of to_integer'
614 * ret_integer - Where the converted integer is returned
616 * RETURN: Status and Converted value
618 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
619 * 32-bit or 64-bit conversion, depending on the current mode
620 * of the interpreter.
621 * NOTE: Does not support Octal strings, not needed.
623 ******************************************************************************/
626 acpi_ut_strtoul64(char *string, u32 base, acpi_integer * ret_integer)
629 acpi_integer return_value = 0;
630 acpi_integer quotient;
631 acpi_integer dividend;
632 u32 to_integer_op = (base == ACPI_ANY_BASE);
633 u32 mode32 = (acpi_gbl_integer_byte_width == 4);
638 ACPI_FUNCTION_TRACE_STR(ut_stroul64, string);
647 return_ACPI_STATUS(AE_BAD_PARAMETER);
654 /* Skip over any white space in the buffer */
656 while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
662 * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
663 * We need to determine if it is decimal or hexadecimal.
665 if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
669 /* Skip over the leading '0x' */
676 /* Any string left? Check that '0x' is not followed by white space. */
678 if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
687 * Perform a 32-bit or 64-bit conversion, depending upon the current
688 * execution mode of the interpreter
690 dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
692 /* Main loop: convert the string to a 32- or 64-bit integer */
695 if (ACPI_IS_DIGIT(*string)) {
697 /* Convert ASCII 0-9 to Decimal value */
699 this_digit = ((u8) * string) - '0';
700 } else if (base == 10) {
702 /* Digit is out of range; possible in to_integer case only */
706 this_digit = (u8) ACPI_TOUPPER(*string);
707 if (ACPI_IS_XDIGIT((char)this_digit)) {
709 /* Convert ASCII Hex char to value */
711 this_digit = this_digit - 'A' + 10;
723 } else if ((valid_digits == 0) && (this_digit == 0)
734 && ((valid_digits > 16)
735 || ((valid_digits > 8) && mode32))) {
737 * This is to_integer operation case.
738 * No any restrictions for string-to-integer conversion,
744 /* Divide the digit into the correct position */
747 acpi_ut_short_divide((dividend - (acpi_integer) this_digit),
748 base, "ient, NULL);
750 if (return_value > quotient) {
758 return_value *= base;
759 return_value += this_digit;
763 /* All done, normal exit */
767 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
768 ACPI_FORMAT_UINT64(return_value)));
770 *ret_integer = return_value;
771 return_ACPI_STATUS(AE_OK);
774 /* Base was set/validated above */
777 return_ACPI_STATUS(AE_BAD_DECIMAL_CONSTANT);
779 return_ACPI_STATUS(AE_BAD_HEX_CONSTANT);
783 /*******************************************************************************
785 * FUNCTION: acpi_ut_create_update_state_and_push
787 * PARAMETERS: Object - Object to be added to the new state
788 * Action - Increment/Decrement
789 * state_list - List the state will be added to
793 * DESCRIPTION: Create a new state and push it
795 ******************************************************************************/
798 acpi_ut_create_update_state_and_push(union acpi_operand_object *object,
800 union acpi_generic_state **state_list)
802 union acpi_generic_state *state;
804 ACPI_FUNCTION_ENTRY();
806 /* Ignore null objects; these are expected */
812 state = acpi_ut_create_update_state(object, action);
814 return (AE_NO_MEMORY);
817 acpi_ut_push_generic_state(state_list, state);
821 /*******************************************************************************
823 * FUNCTION: acpi_ut_walk_package_tree
825 * PARAMETERS: source_object - The package to walk
826 * target_object - Target object (if package is being copied)
827 * walk_callback - Called once for each package element
828 * Context - Passed to the callback function
832 * DESCRIPTION: Walk through a package
834 ******************************************************************************/
837 acpi_ut_walk_package_tree(union acpi_operand_object * source_object,
839 acpi_pkg_callback walk_callback, void *context)
841 acpi_status status = AE_OK;
842 union acpi_generic_state *state_list = NULL;
843 union acpi_generic_state *state;
845 union acpi_operand_object *this_source_obj;
847 ACPI_FUNCTION_TRACE(ut_walk_package_tree);
849 state = acpi_ut_create_pkg_state(source_object, target_object, 0);
851 return_ACPI_STATUS(AE_NO_MEMORY);
856 /* Get one element of the package */
858 this_index = state->pkg.index;
859 this_source_obj = (union acpi_operand_object *)
860 state->pkg.source_object->package.elements[this_index];
864 * 1) An uninitialized package element. It is completely
865 * legal to declare a package and leave it uninitialized
866 * 2) Not an internal object - can be a namespace node instead
867 * 3) Any type other than a package. Packages are handled in else
870 if ((!this_source_obj) ||
871 (ACPI_GET_DESCRIPTOR_TYPE(this_source_obj) !=
872 ACPI_DESC_TYPE_OPERAND)
873 || (ACPI_GET_OBJECT_TYPE(this_source_obj) !=
874 ACPI_TYPE_PACKAGE)) {
876 walk_callback(ACPI_COPY_TYPE_SIMPLE,
877 this_source_obj, state, context);
878 if (ACPI_FAILURE(status)) {
879 return_ACPI_STATUS(status);
883 while (state->pkg.index >=
884 state->pkg.source_object->package.count) {
886 * We've handled all of the objects at this level, This means
887 * that we have just completed a package. That package may
888 * have contained one or more packages itself.
890 * Delete this state and pop the previous state (package).
892 acpi_ut_delete_generic_state(state);
893 state = acpi_ut_pop_generic_state(&state_list);
895 /* Finished when there are no more states */
899 * We have handled all of the objects in the top level
900 * package just add the length of the package objects
903 return_ACPI_STATUS(AE_OK);
907 * Go back up a level and move the index past the just
908 * completed package object.
913 /* This is a subobject of type package */
916 walk_callback(ACPI_COPY_TYPE_PACKAGE,
917 this_source_obj, state, context);
918 if (ACPI_FAILURE(status)) {
919 return_ACPI_STATUS(status);
923 * Push the current state and create a new one
924 * The callback above returned a new target package object.
926 acpi_ut_push_generic_state(&state_list, state);
927 state = acpi_ut_create_pkg_state(this_source_obj,
931 return_ACPI_STATUS(AE_NO_MEMORY);
936 /* We should never get here */
938 return_ACPI_STATUS(AE_AML_INTERNAL);
941 /*******************************************************************************
943 * FUNCTION: acpi_ut_error, acpi_ut_warning, acpi_ut_info
945 * PARAMETERS: module_name - Caller's module name (for error output)
946 * line_number - Caller's line number (for error output)
947 * Format - Printf format string + additional args
951 * DESCRIPTION: Print message with module/line/version info
953 ******************************************************************************/
955 void ACPI_INTERNAL_VAR_XFACE
956 acpi_ut_error(char *module_name, u32 line_number, char *format, ...)
960 acpi_os_printf("ACPI Error (%s-%04d): ", module_name, line_number);
962 va_start(args, format);
963 acpi_os_vprintf(format, args);
964 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
967 void ACPI_INTERNAL_VAR_XFACE
968 acpi_ut_exception(char *module_name,
969 u32 line_number, acpi_status status, char *format, ...)
973 acpi_os_printf("ACPI Exception (%s-%04d): %s, ", module_name,
974 line_number, acpi_format_exception(status));
976 va_start(args, format);
977 acpi_os_vprintf(format, args);
978 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
980 EXPORT_SYMBOL(acpi_ut_exception);
982 void ACPI_INTERNAL_VAR_XFACE
983 acpi_ut_warning(char *module_name, u32 line_number, char *format, ...)
987 acpi_os_printf("ACPI Warning (%s-%04d): ", module_name, line_number);
989 va_start(args, format);
990 acpi_os_vprintf(format, args);
991 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);
994 void ACPI_INTERNAL_VAR_XFACE
995 acpi_ut_info(char *module_name, u32 line_number, char *format, ...)
999 acpi_os_printf("ACPI (%s-%04d): ", module_name, line_number);
1001 va_start(args, format);
1002 acpi_os_vprintf(format, args);
1003 acpi_os_printf(" [%X]\n", ACPI_CA_VERSION);