xref: /freebsd/sys/dev/acpica/Osd/OsdSynch.c (revision 9d839ea8e4a9327a06de07eb9427eec4357a93e3)
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 
30 /*
31  * 6.1 : Mutual Exclusion and Synchronisation
32  */
33 
34 #include "acpi.h"
35 
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 
41 #define _COMPONENT	ACPI_OS_SERVICES
42 MODULE_NAME("SYNCH")
43 
44 static MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
45 
46 /* disable semaphores - AML in the field doesn't use them correctly */
47 #define ACPI_NO_SEMAPHORES
48 
49 /*
50  * Simple counting semaphore implemented using a mutex. (Subsequently used
51  * in the OSI code to implement a mutex.  Go figure.)
52  */
53 struct acpi_semaphore {
54     struct mtx	as_mtx;
55     UINT32	as_units;
56     UINT32	as_maxunits;
57 };
58 
59 ACPI_STATUS
60 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_HANDLE *OutHandle)
61 {
62 #ifndef ACPI_NO_SEMAPHORES
63     struct acpi_semaphore	*as;
64 
65     FUNCTION_TRACE(__func__);
66 
67     if (OutHandle == NULL)
68 	return(AE_BAD_PARAMETER);
69     if (InitialUnits > MaxUnits)
70 	return_ACPI_STATUS(AE_BAD_PARAMETER);
71 
72     if ((as = malloc(sizeof(*as), M_ACPISEM, M_NOWAIT)) == NULL)
73 	return_ACPI_STATUS(AE_NO_MEMORY);
74 
75     mtx_init(&as->as_mtx, "ACPI semaphore", MTX_DEF);
76     as->as_units = InitialUnits;
77     as->as_maxunits = MaxUnits;
78 
79     DEBUG_PRINT(TRACE_MUTEX, ("created semaphore %p max %d, initial %d\n",
80 			      as, InitialUnits, MaxUnits));
81 
82     *OutHandle = (ACPI_HANDLE)as;
83     return_ACPI_STATUS(AE_OK);
84 #else
85     *OutHandle = (ACPI_HANDLE)OutHandle;
86     return(AE_OK);
87 #endif
88 }
89 
90 ACPI_STATUS
91 AcpiOsDeleteSemaphore (ACPI_HANDLE Handle)
92 {
93 #ifndef ACPI_NO_SEMAPHORES
94     struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
95 
96     FUNCTION_TRACE(__func__);
97 
98     DEBUG_PRINT(TRACE_MUTEX, ("destroyed semaphore %p\n", as));
99     mtx_destroy(&as->as_mtx);
100     free(Handle, M_ACPISEM);
101     return_ACPI_STATUS(AE_OK);
102 #else
103     return(AE_OK);
104 #endif
105 }
106 
107 /*
108  * This implementation has a bug, in that it has to stall for the entire
109  * timeout before it will return AE_TIME.  A better implementation would
110  * use getmicrotime() to correctly adjust the timeout after being woken up.
111  */
112 ACPI_STATUS
113 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT32 Timeout)
114 {
115 #ifndef ACPI_NO_SEMAPHORES
116     struct acpi_semaphore	*as = (struct acpi_semaphore *)Handle;
117     ACPI_STATUS			result;
118     int				rv, tmo;
119 
120     FUNCTION_TRACE(__func__);
121 
122     if (as == NULL)
123 	return_ACPI_STATUS(AE_BAD_PARAMETER);
124 
125     /* a timeout of -1 means "forever" */
126     if (Timeout == -1) {
127 	tmo = 0;
128     } else {
129 	/* compute timeout using microseconds per tick */
130 	tmo = (Timeout * 1000) / (1000000 / hz);
131 	if (tmo <= 0)
132 	    tmo = 1;
133     }
134 
135     mtx_lock(&as->as_mtx);
136     DEBUG_PRINT(TRACE_MUTEX, ("get %d units from semaphore %p (has %d), timeout %d\n",
137 			      Units, as, as->as_units, Timeout));
138     for (;;) {
139 	if (as->as_inits == ACPI_NO_UNIT_LIMIT) {
140 	    result = AE_OK;
141 	    break;
142 	}
143 	if (as->as_units >= Units) {
144 	    as->as_units -= Units;
145 	    result = AE_OK;
146 	    break;
147 	}
148 	if (Timeout < 0) {
149 	    result = AE_TIME;
150 	    break;
151 	}
152 	DEBUG_PRINT(TRACE_MUTEX, ("semaphore blocked, calling msleep(%p, %p, %d, \"acpisem\", %d)\n",
153 				  as, as->as_mtx, 0, tmo));
154 
155 	rv = msleep(as, &as->as_mtx, 0, "acpisem", tmo);
156 	DEBUG_PRINT(TRACE_MUTEX, ("msleep returned %d\n", rv));
157 	if (rv == EWOULDBLOCK) {
158 	    result = AE_TIME;
159 	    break;
160 	}
161     }
162     mtx_unlock(&as->as_mtx);
163 
164     return_ACPI_STATUS(result);
165 #else
166     return(AE_OK);
167 #endif
168 }
169 
170 ACPI_STATUS
171 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
172 {
173 #ifndef ACPI_NO_SEMAPHORES
174     struct acpi_semaphore	*as = (struct acpi_semaphore *)Handle;
175 
176     FUNCTION_TRACE(__func__);
177 
178     if (as == NULL)
179 	return_ACPI_STATUS(AE_BAD_PARAMETER);
180 
181     mtx_lock(&as->as_mtx);
182     DEBUG_PRINT(TRACE_MUTEX, ("return %d units to semaphore %p (has %d)\n",
183 			      Units, as, as->as_units));
184     if (as->as_units != ACPI_NO_UNIT_LIMIT) {
185 	as->as_units += Units;
186 	if (as->as_units > as->as_maxunits)
187 	    as->as_units = as->as_maxunits;
188     }
189     wakeup(as);
190     mtx_unlock(&as->as_mtx);
191     return_ACPI_STATUS(AE_OK);
192 #else
193     return(AE_OK);
194 #endif
195 }
196