1*95857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2ffef6827SLin Ming /*******************************************************************************
3ffef6827SLin Ming *
4ffef6827SLin Ming * Module Name: utxfmutex - external AML mutex access functions
5ffef6827SLin Ming *
6ffef6827SLin Ming ******************************************************************************/
7ffef6827SLin Ming
8ffef6827SLin Ming #include <acpi/acpi.h>
9ffef6827SLin Ming #include "accommon.h"
10ffef6827SLin Ming #include "acnamesp.h"
11ffef6827SLin Ming
12ffef6827SLin Ming #define _COMPONENT ACPI_UTILITIES
13ffef6827SLin Ming ACPI_MODULE_NAME("utxfmutex")
14ffef6827SLin Ming
15ffef6827SLin Ming /* Local prototypes */
16ffef6827SLin Ming static acpi_status
17ffef6827SLin Ming acpi_ut_get_mutex_object(acpi_handle handle,
18ffef6827SLin Ming acpi_string pathname,
19ffef6827SLin Ming union acpi_operand_object **ret_obj);
20ffef6827SLin Ming
21ffef6827SLin Ming /*******************************************************************************
22ffef6827SLin Ming *
23ffef6827SLin Ming * FUNCTION: acpi_ut_get_mutex_object
24ffef6827SLin Ming *
25ba494beeSBob Moore * PARAMETERS: handle - Mutex or prefix handle (optional)
26ba494beeSBob Moore * pathname - Mutex pathname (optional)
27ffef6827SLin Ming * ret_obj - Where the mutex object is returned
28ffef6827SLin Ming *
29ffef6827SLin Ming * RETURN: Status
30ffef6827SLin Ming *
31ffef6827SLin Ming * DESCRIPTION: Get an AML mutex object. The mutex node is pointed to by
32ffef6827SLin Ming * Handle:Pathname. Either Handle or Pathname can be NULL, but
33ffef6827SLin Ming * not both.
34ffef6827SLin Ming *
35ffef6827SLin Ming ******************************************************************************/
36ffef6827SLin Ming
37ffef6827SLin Ming static acpi_status
acpi_ut_get_mutex_object(acpi_handle handle,acpi_string pathname,union acpi_operand_object ** ret_obj)38ffef6827SLin Ming acpi_ut_get_mutex_object(acpi_handle handle,
39ffef6827SLin Ming acpi_string pathname,
40ffef6827SLin Ming union acpi_operand_object **ret_obj)
41ffef6827SLin Ming {
42ffef6827SLin Ming struct acpi_namespace_node *mutex_node;
43ffef6827SLin Ming union acpi_operand_object *mutex_obj;
44ffef6827SLin Ming acpi_status status;
45ffef6827SLin Ming
46ffef6827SLin Ming /* Parameter validation */
47ffef6827SLin Ming
48ffef6827SLin Ming if (!ret_obj || (!handle && !pathname)) {
49ffef6827SLin Ming return (AE_BAD_PARAMETER);
50ffef6827SLin Ming }
51ffef6827SLin Ming
52ffef6827SLin Ming /* Get a the namespace node for the mutex */
53ffef6827SLin Ming
54ffef6827SLin Ming mutex_node = handle;
55ffef6827SLin Ming if (pathname != NULL) {
561fad8738SBob Moore status =
571fad8738SBob Moore acpi_get_handle(handle, pathname,
581fad8738SBob Moore ACPI_CAST_PTR(acpi_handle, &mutex_node));
59ffef6827SLin Ming if (ACPI_FAILURE(status)) {
60ffef6827SLin Ming return (status);
61ffef6827SLin Ming }
62ffef6827SLin Ming }
63ffef6827SLin Ming
64ffef6827SLin Ming /* Ensure that we actually have a Mutex object */
65ffef6827SLin Ming
66ffef6827SLin Ming if (!mutex_node || (mutex_node->type != ACPI_TYPE_MUTEX)) {
67ffef6827SLin Ming return (AE_TYPE);
68ffef6827SLin Ming }
69ffef6827SLin Ming
70ffef6827SLin Ming /* Get the low-level mutex object */
71ffef6827SLin Ming
72ffef6827SLin Ming mutex_obj = acpi_ns_get_attached_object(mutex_node);
73ffef6827SLin Ming if (!mutex_obj) {
74ffef6827SLin Ming return (AE_NULL_OBJECT);
75ffef6827SLin Ming }
76ffef6827SLin Ming
77ffef6827SLin Ming *ret_obj = mutex_obj;
78ffef6827SLin Ming return (AE_OK);
79ffef6827SLin Ming }
80ffef6827SLin Ming
81ffef6827SLin Ming /*******************************************************************************
82ffef6827SLin Ming *
83ffef6827SLin Ming * FUNCTION: acpi_acquire_mutex
84ffef6827SLin Ming *
85ba494beeSBob Moore * PARAMETERS: handle - Mutex or prefix handle (optional)
86ba494beeSBob Moore * pathname - Mutex pathname (optional)
87ba494beeSBob Moore * timeout - Max time to wait for the lock (millisec)
88ffef6827SLin Ming *
89ffef6827SLin Ming * RETURN: Status
90ffef6827SLin Ming *
91ffef6827SLin Ming * DESCRIPTION: Acquire an AML mutex. This is a device driver interface to
92ffef6827SLin Ming * AML mutex objects, and allows for transaction locking between
93ffef6827SLin Ming * drivers and AML code. The mutex node is pointed to by
94ffef6827SLin Ming * Handle:Pathname. Either Handle or Pathname can be NULL, but
95ffef6827SLin Ming * not both.
96ffef6827SLin Ming *
97ffef6827SLin Ming ******************************************************************************/
98ffef6827SLin Ming
99ffef6827SLin Ming acpi_status
acpi_acquire_mutex(acpi_handle handle,acpi_string pathname,u16 timeout)100ffef6827SLin Ming acpi_acquire_mutex(acpi_handle handle, acpi_string pathname, u16 timeout)
101ffef6827SLin Ming {
102ffef6827SLin Ming acpi_status status;
103ffef6827SLin Ming union acpi_operand_object *mutex_obj;
104ffef6827SLin Ming
105ffef6827SLin Ming /* Get the low-level mutex associated with Handle:Pathname */
106ffef6827SLin Ming
107ffef6827SLin Ming status = acpi_ut_get_mutex_object(handle, pathname, &mutex_obj);
108ffef6827SLin Ming if (ACPI_FAILURE(status)) {
109ffef6827SLin Ming return (status);
110ffef6827SLin Ming }
111ffef6827SLin Ming
112ffef6827SLin Ming /* Acquire the OS mutex */
113ffef6827SLin Ming
114ffef6827SLin Ming status = acpi_os_acquire_mutex(mutex_obj->mutex.os_mutex, timeout);
115ffef6827SLin Ming return (status);
116ffef6827SLin Ming }
117ffef6827SLin Ming
ACPI_EXPORT_SYMBOL(acpi_acquire_mutex)11807536e27SBob Moore ACPI_EXPORT_SYMBOL(acpi_acquire_mutex)
11907536e27SBob Moore
120ffef6827SLin Ming /*******************************************************************************
121ffef6827SLin Ming *
122ffef6827SLin Ming * FUNCTION: acpi_release_mutex
123ffef6827SLin Ming *
124ba494beeSBob Moore * PARAMETERS: handle - Mutex or prefix handle (optional)
125ba494beeSBob Moore * pathname - Mutex pathname (optional)
126ffef6827SLin Ming *
127ffef6827SLin Ming * RETURN: Status
128ffef6827SLin Ming *
129ffef6827SLin Ming * DESCRIPTION: Release an AML mutex. This is a device driver interface to
130ffef6827SLin Ming * AML mutex objects, and allows for transaction locking between
131ffef6827SLin Ming * drivers and AML code. The mutex node is pointed to by
132ffef6827SLin Ming * Handle:Pathname. Either Handle or Pathname can be NULL, but
133ffef6827SLin Ming * not both.
134ffef6827SLin Ming *
135ffef6827SLin Ming ******************************************************************************/
136ffef6827SLin Ming acpi_status acpi_release_mutex(acpi_handle handle, acpi_string pathname)
137ffef6827SLin Ming {
138ffef6827SLin Ming acpi_status status;
139ffef6827SLin Ming union acpi_operand_object *mutex_obj;
140ffef6827SLin Ming
141ffef6827SLin Ming /* Get the low-level mutex associated with Handle:Pathname */
142ffef6827SLin Ming
143ffef6827SLin Ming status = acpi_ut_get_mutex_object(handle, pathname, &mutex_obj);
144ffef6827SLin Ming if (ACPI_FAILURE(status)) {
145ffef6827SLin Ming return (status);
146ffef6827SLin Ming }
147ffef6827SLin Ming
148ffef6827SLin Ming /* Release the OS mutex */
149ffef6827SLin Ming
150ffef6827SLin Ming acpi_os_release_mutex(mutex_obj->mutex.os_mutex);
151ffef6827SLin Ming return (AE_OK);
152ffef6827SLin Ming }
15307536e27SBob Moore
15407536e27SBob Moore ACPI_EXPORT_SYMBOL(acpi_release_mutex)
155