ACPI: ACPICA 20060526

Restructured, flattened, and simplified the internal
interfaces for namespace object evaluation - resulting
in smaller code, less CPU stack use, and fewer
interfaces. (With assistance from Mikhail Kouzmich)

Fixed a problem with the CopyObject operator where the
first parameter was not typed correctly for the parser,
interpreter, compiler, and disassembler. Caused various
errors and unexpected behavior.

Fixed a problem where a ShiftLeft or ShiftRight of
more than 64 bits produced incorrect results with some
C compilers. Since the behavior of C compilers when
the shift value is larger than the datatype width is
apparently not well defined, the interpreter now detects
this condition and simply returns zero as expected in all
such cases. (BZ 395)

Fixed problem reports (Valery Podrezov) integrated: -
Update String-to-Integer conversion to match ACPI 3.0A spec
http://bugzilla.kernel.org/show_bug.cgi?id=5329
Allow interpreter to handle nested method declarations
http://bugzilla.kernel.org/show_bug.cgi?id=5361

Fixed problem reports (Fiodor Suietov) integrated: -
acpi_terminate() doesn't free debug memory allocation
list objects (BZ 355) - After Core Subsystem
shutdown, acpi_subsystem_status() returns AE_OK (BZ 356) -
acpi_os_unmap_memory() for RSDP can be invoked inconsistently
(BZ 357) - Resource Manager should return AE_TYPE for
non-device objects (BZ 358) - Incomplete cleanup branch
in AcpiNsEvaluateRelative (BZ 359) - Use acpi_os_free()
instead of ACPI_FREE in acpi_rs_set_srs_method_data (BZ 360)
- Incomplete cleanup branch in acpi_ps_parse_aml (BZ 361) -
Incomplete cleanup branch in acpi_ds_delete_walk_state (BZ 362)
- acpi_get_table_header returns AE_NO_ACPI_TABLES until DSDT
is loaded (BZ 365) - Status of the Global Initialization
Handler call not used (BZ 366) - Incorrect object parameter
to Global Initialization Handler (BZ 367)

Signed-off-by: Bob Moore <robert.moore@intel.com>
Signed-off-by: Len Brown <len.brown@intel.com>
diff --git a/drivers/acpi/utilities/utalloc.c b/drivers/acpi/utilities/utalloc.c
index 65a7c2e..7940fc1 100644
--- a/drivers/acpi/utilities/utalloc.c
+++ b/drivers/acpi/utilities/utalloc.c
@@ -61,24 +61,6 @@
 {
 	acpi_status status;
 
-#ifdef ACPI_DBG_TRACK_ALLOCATIONS
-
-	/* Memory allocation lists */
-
-	status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
-	if (ACPI_FAILURE(status)) {
-		return (status);
-	}
-
-	status =
-	    acpi_ut_create_list("Acpi-Namespace",
-				sizeof(struct acpi_namespace_node),
-				&acpi_gbl_ns_node_list);
-	if (ACPI_FAILURE(status)) {
-		return (status);
-	}
-#endif
-
 	/* Object Caches, for frequently used objects */
 
 	status =
@@ -125,6 +107,24 @@
 		return (status);
 	}
 
+#ifdef ACPI_DBG_TRACK_ALLOCATIONS
+
+	/* Memory allocation lists */
+
+	status = acpi_ut_create_list("Acpi-Global", 0, &acpi_gbl_global_list);
+	if (ACPI_FAILURE(status)) {
+		return (status);
+	}
+
+	status =
+	    acpi_ut_create_list("Acpi-Namespace",
+				sizeof(struct acpi_namespace_node),
+				&acpi_gbl_ns_node_list);
+	if (ACPI_FAILURE(status)) {
+		return (status);
+	}
+#endif
+
 	return (AE_OK);
 }
 
@@ -158,6 +158,21 @@
 	(void)acpi_os_delete_cache(acpi_gbl_ps_node_ext_cache);
 	acpi_gbl_ps_node_ext_cache = NULL;
 
+#ifdef ACPI_DBG_TRACK_ALLOCATIONS
+
+	/* Debug only - display leftover memory allocation, if any */
+
+	acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
+
+	/* Free memory lists */
+
+	acpi_os_free(acpi_gbl_global_list);
+	acpi_gbl_global_list = NULL;
+
+	acpi_os_free(acpi_gbl_ns_node_list);
+	acpi_gbl_ns_node_list = NULL;
+#endif
+
 	return (AE_OK);
 }
 
diff --git a/drivers/acpi/utilities/utdelete.c b/drivers/acpi/utilities/utdelete.c
index 0bb4b59..67b9f32 100644
--- a/drivers/acpi/utilities/utdelete.c
+++ b/drivers/acpi/utilities/utdelete.c
@@ -319,11 +319,9 @@
 	new_count = count;
 
 	/*
-	 * Perform the reference count action
-	 * (increment, decrement, or force delete)
+	 * Perform the reference count action (increment, decrement, force delete)
 	 */
 	switch (action) {
-
 	case REF_INCREMENT:
 
 		new_count++;
@@ -360,7 +358,6 @@
 		if (new_count == 0) {
 			acpi_ut_delete_internal_obj(object);
 		}
-
 		break;
 
 	case REF_FORCE_DELETE:
@@ -385,13 +382,10 @@
 	 * (A deleted object will have a huge reference count)
 	 */
 	if (count > ACPI_MAX_REFERENCE_COUNT) {
-
 		ACPI_WARNING((AE_INFO,
-			      "Large Reference Count (%X) in object %p",
-			      count, object));
+			      "Large Reference Count (%X) in object %p", count,
+			      object));
 	}
-
-	return;
 }
 
 /*******************************************************************************
@@ -417,7 +411,7 @@
  ******************************************************************************/
 
 acpi_status
-acpi_ut_update_object_reference(union acpi_operand_object * object, u16 action)
+acpi_ut_update_object_reference(union acpi_operand_object *object, u16 action)
 {
 	acpi_status status = AE_OK;
 	union acpi_generic_state *state_list = NULL;
@@ -521,11 +515,11 @@
 
 		case ACPI_TYPE_REGION:
 		default:
-			break;	/* No subobjects */
+			break;	/* No subobjects for all other types */
 		}
 
 		/*
-		 * Now we can update the count in the main object.  This can only
+		 * Now we can update the count in the main object. This can only
 		 * happen after we update the sub-objects in case this causes the
 		 * main object to be deleted.
 		 */
@@ -606,8 +600,8 @@
 	ACPI_FUNCTION_TRACE_PTR(ut_remove_reference, object);
 
 	/*
-	 * Allow a NULL pointer to be passed in, just ignore it.  This saves
-	 * each caller from having to check.  Also, ignore NS nodes.
+	 * Allow a NULL pointer to be passed in, just ignore it. This saves
+	 * each caller from having to check. Also, ignore NS nodes.
 	 *
 	 */
 	if (!object ||
@@ -627,7 +621,7 @@
 
 	/*
 	 * Decrement the reference count, and only actually delete the object
-	 * if the reference count becomes 0.  (Must also decrement the ref count
+	 * if the reference count becomes 0. (Must also decrement the ref count
 	 * of all subobjects!)
 	 */
 	(void)acpi_ut_update_object_reference(object, REF_DECREMENT);
diff --git a/drivers/acpi/utilities/uteval.c b/drivers/acpi/utilities/uteval.c
index 444d3a5..d6d7121 100644
--- a/drivers/acpi/utilities/uteval.c
+++ b/drivers/acpi/utilities/uteval.c
@@ -181,19 +181,26 @@
 			u32 expected_return_btypes,
 			union acpi_operand_object **return_desc)
 {
-	struct acpi_parameter_info info;
+	struct acpi_evaluate_info *info;
 	acpi_status status;
 	u32 return_btype;
 
 	ACPI_FUNCTION_TRACE(ut_evaluate_object);
 
-	info.node = prefix_node;
-	info.parameters = NULL;
-	info.parameter_type = ACPI_PARAM_ARGS;
+	/* Allocate the evaluation information block */
+
+	info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
+	if (!info) {
+		return_ACPI_STATUS(AE_NO_MEMORY);
+	}
+
+	info->prefix_node = prefix_node;
+	info->pathname = path;
+	info->parameter_type = ACPI_PARAM_ARGS;
 
 	/* Evaluate the object/method */
 
-	status = acpi_ns_evaluate_relative(path, &info);
+	status = acpi_ns_evaluate(info);
 	if (ACPI_FAILURE(status)) {
 		if (status == AE_NOT_FOUND) {
 			ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
@@ -205,25 +212,25 @@
 					  prefix_node, path, status);
 		}
 
-		return_ACPI_STATUS(status);
+		goto cleanup;
 	}
 
 	/* Did we get a return object? */
 
-	if (!info.return_object) {
+	if (!info->return_object) {
 		if (expected_return_btypes) {
 			ACPI_ERROR_METHOD("No object was returned from",
 					  prefix_node, path, AE_NOT_EXIST);
 
-			return_ACPI_STATUS(AE_NOT_EXIST);
+			status = AE_NOT_EXIST;
 		}
 
-		return_ACPI_STATUS(AE_OK);
+		goto cleanup;
 	}
 
 	/* Map the return object type to the bitmapped type */
 
-	switch (ACPI_GET_OBJECT_TYPE(info.return_object)) {
+	switch (ACPI_GET_OBJECT_TYPE(info->return_object)) {
 	case ACPI_TYPE_INTEGER:
 		return_btype = ACPI_BTYPE_INTEGER;
 		break;
@@ -251,8 +258,8 @@
 		 * happen frequently if the "implicit return" feature is enabled.
 		 * Just delete the return object and return AE_OK.
 		 */
-		acpi_ut_remove_reference(info.return_object);
-		return_ACPI_STATUS(AE_OK);
+		acpi_ut_remove_reference(info->return_object);
+		goto cleanup;
 	}
 
 	/* Is the return object one of the expected types? */
@@ -264,19 +271,23 @@
 		ACPI_ERROR((AE_INFO,
 			    "Type returned from %s was incorrect: %s, expected Btypes: %X",
 			    path,
-			    acpi_ut_get_object_type_name(info.return_object),
+			    acpi_ut_get_object_type_name(info->return_object),
 			    expected_return_btypes));
 
 		/* On error exit, we must delete the return object */
 
-		acpi_ut_remove_reference(info.return_object);
-		return_ACPI_STATUS(AE_TYPE);
+		acpi_ut_remove_reference(info->return_object);
+		status = AE_TYPE;
+		goto cleanup;
 	}
 
 	/* Object type is OK, return it */
 
-	*return_desc = info.return_object;
-	return_ACPI_STATUS(AE_OK);
+	*return_desc = info->return_object;
+
+      cleanup:
+	ACPI_FREE(info);
+	return_ACPI_STATUS(status);
 }
 
 /*******************************************************************************
diff --git a/drivers/acpi/utilities/utinit.c b/drivers/acpi/utilities/utinit.c
index c65747d..ff76055 100644
--- a/drivers/acpi/utilities/utinit.c
+++ b/drivers/acpi/utilities/utinit.c
@@ -228,6 +228,7 @@
 	/* Subsystem appears active, go ahead and shut it down */
 
 	acpi_gbl_shutdown = TRUE;
+	acpi_gbl_startup_flags = 0;
 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Shutting down ACPI Subsystem\n"));
 
 	/* Close the acpi_event Handling */
@@ -245,12 +246,5 @@
 	/* Purge the local caches */
 
 	(void)acpi_ut_delete_caches();
-
-	/* Debug only - display leftover memory allocation, if any */
-
-#ifdef ACPI_DBG_TRACK_ALLOCATIONS
-	acpi_ut_dump_allocations(ACPI_UINT32_MAX, NULL);
-#endif
-
 	return_VOID;
 }
diff --git a/drivers/acpi/utilities/utmisc.c b/drivers/acpi/utilities/utmisc.c
index 8744671..5c75d35 100644
--- a/drivers/acpi/utilities/utmisc.c
+++ b/drivers/acpi/utilities/utmisc.c
@@ -601,7 +601,8 @@
  * FUNCTION:    acpi_ut_strtoul64
  *
  * PARAMETERS:  String          - Null terminated string
- *              Base            - Radix of the string: 10, 16, or ACPI_ANY_BASE
+ *              Base            - Radix of the string: 16 or ACPI_ANY_BASE;
+ *                                ACPI_ANY_BASE means 'in behalf of to_integer'
  *              ret_integer     - Where the converted integer is returned
  *
  * RETURN:      Status and Converted value
@@ -617,16 +618,17 @@
 	u32 this_digit = 0;
 	acpi_integer return_value = 0;
 	acpi_integer quotient;
+	acpi_integer dividend;
+	u32 to_integer_op = (base == ACPI_ANY_BASE);
+	u32 mode32 = (acpi_gbl_integer_byte_width == 4);
+	u8 valid_digits = 0;
+	u8 sign_of0x = 0;
+	u8 term = 0;
 
 	ACPI_FUNCTION_TRACE(ut_stroul64);
 
-	if ((!string) || !(*string)) {
-		goto error_exit;
-	}
-
 	switch (base) {
 	case ACPI_ANY_BASE:
-	case 10:
 	case 16:
 		break;
 
@@ -635,39 +637,45 @@
 		return_ACPI_STATUS(AE_BAD_PARAMETER);
 	}
 
+	if (!string) {
+		goto error_exit;
+	}
+
 	/* Skip over any white space in the buffer */
 
-	while (ACPI_IS_SPACE(*string) || *string == '\t') {
+	while ((*string) && (ACPI_IS_SPACE(*string) || *string == '\t')) {
 		string++;
 	}
 
-	/*
-	 * If the input parameter Base is zero, then we need to
-	 * determine if it is decimal or hexadecimal:
-	 */
-	if (base == 0) {
+	if (to_integer_op) {
+		/*
+		 * Base equal to ACPI_ANY_BASE means 'to_integer operation case'.
+		 * We need to determine if it is decimal or hexadecimal.
+		 */
 		if ((*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
+			sign_of0x = 1;
 			base = 16;
+
+			/* Skip over the leading '0x' */
 			string += 2;
 		} else {
 			base = 10;
 		}
 	}
 
-	/*
-	 * For hexadecimal base, skip over the leading
-	 * 0 or 0x, if they are present.
-	 */
-	if ((base == 16) &&
-	    (*string == '0') && (ACPI_TOLOWER(*(string + 1)) == 'x')) {
-		string += 2;
+	/* Any string left? Check that '0x' is not followed by white space. */
+
+	if (!(*string) || ACPI_IS_SPACE(*string) || *string == '\t') {
+		if (to_integer_op) {
+			goto error_exit;
+		} else {
+			goto all_done;
+		}
 	}
 
-	/* Any string left? */
+	dividend = (mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
 
-	if (!(*string)) {
-		goto error_exit;
-	}
+	/* At least one character in the string here */
 
 	/* Main loop: convert the string to a 64-bit integer */
 
@@ -677,14 +685,12 @@
 			/* Convert ASCII 0-9 to Decimal value */
 
 			this_digit = ((u8) * string) - '0';
+		} else if (base == 10) {
+
+			/* Digit is out of range; possible in to_integer case only */
+
+			term = 1;
 		} else {
-			if (base == 10) {
-
-				/* Digit is out of range */
-
-				goto error_exit;
-			}
-
 			this_digit = (u8) ACPI_TOUPPER(*string);
 			if (ACPI_IS_XDIGIT((char)this_digit)) {
 
@@ -692,22 +698,49 @@
 
 				this_digit = this_digit - 'A' + 10;
 			} else {
-				/*
-				 * We allow non-hex chars, just stop now, same as end-of-string.
-				 * See ACPI spec, string-to-integer conversion.
-				 */
+				term = 1;
+			}
+		}
+
+		if (term) {
+			if (to_integer_op) {
+				goto error_exit;
+			} else {
 				break;
 			}
+		} else if ((valid_digits == 0) && (this_digit == 0)
+			   && !sign_of0x) {
+
+			/* Skip zeros */
+			string++;
+			continue;
+		}
+
+		valid_digits++;
+
+		if (sign_of0x
+		    && ((valid_digits > 16)
+			|| ((valid_digits > 8) && mode32))) {
+			/*
+			 * This is to_integer operation case.
+			 * No any restrictions for string-to-integer conversion,
+			 * see ACPI spec.
+			 */
+			goto error_exit;
 		}
 
 		/* Divide the digit into the correct position */
 
 		(void)
-		    acpi_ut_short_divide((ACPI_INTEGER_MAX -
-					  (acpi_integer) this_digit), base,
-					 &quotient, NULL);
+		    acpi_ut_short_divide((dividend - (acpi_integer) this_digit),
+					 base, &quotient, NULL);
+
 		if (return_value > quotient) {
-			goto error_exit;
+			if (to_integer_op) {
+				goto error_exit;
+			} else {
+				break;
+			}
 		}
 
 		return_value *= base;
@@ -717,6 +750,8 @@
 
 	/* All done, normal exit */
 
+      all_done:
+
 	*ret_integer = return_value;
 	return_ACPI_STATUS(AE_OK);