Merge branches 'pxa-ian' and 'pxa-xm270' into pxa
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / acpi / executer / exmutex.c
index 4eb883bda6ae7f23800decda4a549042c64c8a94..a8bf3d713e2821db5c6e32588f4cc52f38e326bc 100644 (file)
@@ -6,7 +6,7 @@
  *****************************************************************************/
 
 /*
- * Copyright (C) 2000 - 2007, R. Byron Moore
+ * Copyright (C) 2000 - 2008, Intel Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -66,9 +66,10 @@ acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
  *
  ******************************************************************************/
 
-void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc,
-                         struct acpi_thread_state *thread)
+void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
 {
+       struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
+
        if (!thread) {
                return;
        }
@@ -123,6 +124,79 @@ acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
        thread->acquired_mutex_list = obj_desc;
 }
 
+/*******************************************************************************
+ *
+ * FUNCTION:    acpi_ex_acquire_mutex_object
+ *
+ * PARAMETERS:  time_desc           - Timeout in milliseconds
+ *              obj_desc            - Mutex object
+ *              Thread              - Current thread state
+ *
+ * RETURN:      Status
+ *
+ * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
+ *              path that supports multiple acquires by the same thread.
+ *
+ * MUTEX:       Interpreter must be locked
+ *
+ * NOTE: This interface is called from three places:
+ * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
+ * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
+ *    global lock
+ * 3) From the external interface, acpi_acquire_global_lock
+ *
+ ******************************************************************************/
+
+acpi_status
+acpi_ex_acquire_mutex_object(u16 timeout,
+                            union acpi_operand_object *obj_desc,
+                            acpi_thread_id thread_id)
+{
+       acpi_status status;
+
+       ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
+
+       if (!obj_desc) {
+               return_ACPI_STATUS(AE_BAD_PARAMETER);
+       }
+
+       /* Support for multiple acquires by the owning thread */
+
+       if (obj_desc->mutex.thread_id == thread_id) {
+               /*
+                * The mutex is already owned by this thread, just increment the
+                * acquisition depth
+                */
+               obj_desc->mutex.acquisition_depth++;
+               return_ACPI_STATUS(AE_OK);
+       }
+
+       /* Acquire the mutex, wait if necessary. Special case for Global Lock */
+
+       if (obj_desc == acpi_gbl_global_lock_mutex) {
+               status = acpi_ev_acquire_global_lock(timeout);
+       } else {
+               status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
+                                                  timeout);
+       }
+
+       if (ACPI_FAILURE(status)) {
+
+               /* Includes failure from a timeout on time_desc */
+
+               return_ACPI_STATUS(status);
+       }
+
+       /* Acquired the mutex: update mutex object */
+
+       obj_desc->mutex.thread_id = thread_id;
+       obj_desc->mutex.acquisition_depth = 1;
+       obj_desc->mutex.original_sync_level = 0;
+       obj_desc->mutex.owner_thread = NULL;    /* Used only for AML Acquire() */
+
+       return_ACPI_STATUS(AE_OK);
+}
+
 /*******************************************************************************
  *
  * FUNCTION:    acpi_ex_acquire_mutex
@@ -150,7 +224,7 @@ acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
                return_ACPI_STATUS(AE_BAD_PARAMETER);
        }
 
-       /* Sanity check: we must have a valid thread ID */
+       /* Must have a valid thread ID */
 
        if (!walk_state->thread) {
                ACPI_ERROR((AE_INFO,
@@ -160,7 +234,7 @@ acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
        }
 
        /*
-        * Current Sync must be less than or equal to the sync level of the
+        * Current sync level must be less than or equal to the sync level of the
         * mutex. This mechanism provides some deadlock prevention
         */
        if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
@@ -171,48 +245,89 @@ acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
                return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
        }
 
-       /* Support for multiple acquires by the owning thread */
+       status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value,
+                                             obj_desc,
+                                             walk_state->thread->thread_id);
+       if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
 
-       if (obj_desc->mutex.owner_thread_id == acpi_os_get_thread_id()) {
-               /*
-                * The mutex is already owned by this thread, just increment the
-                * acquisition depth
-                */
-               obj_desc->mutex.acquisition_depth++;
-               return_ACPI_STATUS(AE_OK);
+               /* Save Thread object, original/current sync levels */
+
+               obj_desc->mutex.owner_thread = walk_state->thread;
+               obj_desc->mutex.original_sync_level =
+                   walk_state->thread->current_sync_level;
+               walk_state->thread->current_sync_level =
+                   obj_desc->mutex.sync_level;
+
+               /* Link the mutex to the current thread for force-unlock at method exit */
+
+               acpi_ex_link_mutex(obj_desc, walk_state->thread);
        }
 
-       /* Acquire the mutex, wait if necessary. Special case for Global Lock */
+       return_ACPI_STATUS(status);
+}
 
-       if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
-               status =
-                   acpi_ev_acquire_global_lock((u16) time_desc->integer.value);
-       } else {
-               status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
-                                                  (u16) time_desc->integer.
-                                                  value);
+/*******************************************************************************
+ *
+ * FUNCTION:    acpi_ex_release_mutex_object
+ *
+ * PARAMETERS:  obj_desc            - The object descriptor for this op
+ *
+ * RETURN:      Status
+ *
+ * DESCRIPTION: Release a previously acquired Mutex, low level interface.
+ *              Provides a common path that supports multiple releases (after
+ *              previous multiple acquires) by the same thread.
+ *
+ * MUTEX:       Interpreter must be locked
+ *
+ * NOTE: This interface is called from three places:
+ * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
+ * 2) From acpi_ex_release_global_lock when an AML Field access requires the
+ *    global lock
+ * 3) From the external interface, acpi_release_global_lock
+ *
+ ******************************************************************************/
+
+acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
+{
+       acpi_status status = AE_OK;
+
+       ACPI_FUNCTION_TRACE(ex_release_mutex_object);
+
+       if (obj_desc->mutex.acquisition_depth == 0) {
+               return (AE_NOT_ACQUIRED);
        }
 
-       if (ACPI_FAILURE(status)) {
+       /* Match multiple Acquires with multiple Releases */
 
-               /* Includes failure from a timeout on time_desc */
+       obj_desc->mutex.acquisition_depth--;
+       if (obj_desc->mutex.acquisition_depth != 0) {
 
-               return_ACPI_STATUS(status);
+               /* Just decrement the depth and return */
+
+               return_ACPI_STATUS(AE_OK);
        }
 
-       /* Have the mutex: update mutex and walk info and save the sync_level */
+       if (obj_desc->mutex.owner_thread) {
 
-       obj_desc->mutex.owner_thread_id = acpi_os_get_thread_id();
-       obj_desc->mutex.acquisition_depth = 1;
-       obj_desc->mutex.original_sync_level =
-           walk_state->thread->current_sync_level;
+               /* Unlink the mutex from the owner's list */
 
-       walk_state->thread->current_sync_level = obj_desc->mutex.sync_level;
+               acpi_ex_unlink_mutex(obj_desc);
+               obj_desc->mutex.owner_thread = NULL;
+       }
 
-       /* Link the mutex to the current thread for force-unlock at method exit */
+       /* Release the mutex, special case for Global Lock */
 
-       acpi_ex_link_mutex(obj_desc, walk_state->thread);
-       return_ACPI_STATUS(AE_OK);
+       if (obj_desc == acpi_gbl_global_lock_mutex) {
+               status = acpi_ev_release_global_lock();
+       } else {
+               acpi_os_release_mutex(obj_desc->mutex.os_mutex);
+       }
+
+       /* Clear mutex info */
+
+       obj_desc->mutex.thread_id = NULL;
+       return_ACPI_STATUS(status);
 }
 
 /*******************************************************************************
@@ -242,76 +357,60 @@ acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
 
        /* The mutex must have been previously acquired in order to release it */
 
-       if (!obj_desc->mutex.owner_thread_id) {
+       if (!obj_desc->mutex.owner_thread) {
                ACPI_ERROR((AE_INFO,
                            "Cannot release Mutex [%4.4s], not acquired",
                            acpi_ut_get_node_name(obj_desc->mutex.node)));
                return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
        }
 
-       /* Sanity check: we must have a valid thread ID */
-
-       if (!walk_state->thread) {
-               ACPI_ERROR((AE_INFO,
-                           "Cannot release Mutex [%4.4s], null thread info",
-                           acpi_ut_get_node_name(obj_desc->mutex.node)));
-               return_ACPI_STATUS(AE_AML_INTERNAL);
-       }
-
        /*
         * The Mutex is owned, but this thread must be the owner.
         * Special case for Global Lock, any thread can release
         */
-       if ((obj_desc->mutex.owner_thread_id !=
+       if ((obj_desc->mutex.owner_thread->thread_id !=
             walk_state->thread->thread_id)
-           && (obj_desc->mutex.os_mutex != acpi_gbl_global_lock_mutex)) {
+           && (obj_desc != acpi_gbl_global_lock_mutex)) {
                ACPI_ERROR((AE_INFO,
                            "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX",
                            (unsigned long)walk_state->thread->thread_id,
                            acpi_ut_get_node_name(obj_desc->mutex.node),
-                           (unsigned long)obj_desc->mutex.owner_thread_id));
+                           (unsigned long)obj_desc->mutex.owner_thread->
+                           thread_id));
                return_ACPI_STATUS(AE_AML_NOT_OWNER);
        }
 
+       /* Must have a valid thread ID */
+
+       if (!walk_state->thread) {
+               ACPI_ERROR((AE_INFO,
+                           "Cannot release Mutex [%4.4s], null thread info",
+                           acpi_ut_get_node_name(obj_desc->mutex.node)));
+               return_ACPI_STATUS(AE_AML_INTERNAL);
+       }
+
        /*
         * The sync level of the mutex must be less than or equal to the current
         * sync level
         */
        if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
                ACPI_ERROR((AE_INFO,
-                           "Cannot release Mutex [%4.4s], incorrect SyncLevel",
-                           acpi_ut_get_node_name(obj_desc->mutex.node)));
+                           "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %d current %d",
+                           acpi_ut_get_node_name(obj_desc->mutex.node),
+                           obj_desc->mutex.sync_level,
+                           walk_state->thread->current_sync_level));
                return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
        }
 
-       /* Match multiple Acquires with multiple Releases */
-
-       obj_desc->mutex.acquisition_depth--;
-       if (obj_desc->mutex.acquisition_depth != 0) {
-
-               /* Just decrement the depth and return */
-
-               return_ACPI_STATUS(AE_OK);
-       }
-
-       /* Unlink the mutex from the owner's list */
+       status = acpi_ex_release_mutex_object(obj_desc);
 
-       acpi_ex_unlink_mutex(obj_desc, walk_state->thread);
+       if (obj_desc->mutex.acquisition_depth == 0) {
 
-       /* Release the mutex, special case for Global Lock */
+               /* Restore the original sync_level */
 
-       if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
-               status = acpi_ev_release_global_lock();
-       } else {
-               acpi_os_release_mutex(obj_desc->mutex.os_mutex);
+               walk_state->thread->current_sync_level =
+                   obj_desc->mutex.original_sync_level;
        }
-
-       /* Update the mutex and restore sync_level */
-
-       obj_desc->mutex.owner_thread_id = ACPI_MUTEX_NOT_ACQUIRED;
-       walk_state->thread->current_sync_level =
-           obj_desc->mutex.original_sync_level;
-
        return_ACPI_STATUS(status);
 }
 
@@ -352,7 +451,7 @@ void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
 
                /* Release the mutex, special case for Global Lock */
 
-               if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
+               if (obj_desc == acpi_gbl_global_lock_mutex) {
 
                        /* Ignore errors */
 
@@ -363,7 +462,8 @@ void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
 
                /* Mark mutex unowned */
 
-               obj_desc->mutex.owner_thread_id = ACPI_MUTEX_NOT_ACQUIRED;
+               obj_desc->mutex.owner_thread = NULL;
+               obj_desc->mutex.thread_id = NULL;
 
                /* Update Thread sync_level (Last mutex is the important one) */