2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/dev/acpica/Osd/OsdSynch.c,v 1.17.2.1 2003/08/22 20:49:21 jhb Exp $
28 * $DragonFly: src/sys/dev/acpica/Osd/Attic/OsdSynch.c,v 1.4 2004/05/19 22:52:40 dillon Exp $
32 * 6.1 : Mutual Exclusion and Synchronisation
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
41 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
43 #include <sys/mutex.h>
46 #define _COMPONENT ACPI_OS_SERVICES
47 ACPI_MODULE_NAME("SYNCH")
49 static MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
51 #if defined(__DragonFly__) || __FreeBSD_version < 500000
52 # define AS_LOCK(as) s = splhigh()
53 # define AS_UNLOCK(as) splx(s)
54 # define AS_LOCK_DECL int s
55 # define msleep(a, b, c, d, e) tsleep(a, c, d, e)
57 # define AS_LOCK(as) mtx_lock(&(as)->as_mtx)
58 # define AS_UNLOCK(as) mtx_unlock(&(as)->as_mtx)
63 * Simple counting semaphore implemented using a mutex. (Subsequently used
64 * in the OSI code to implement a mutex. Go figure.)
66 struct acpi_semaphore {
67 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
77 #ifndef ACPI_NO_SEMAPHORES
78 #ifndef ACPI_SEMAPHORES_MAX_PENDING
79 #define ACPI_SEMAPHORES_MAX_PENDING 4
81 static int acpi_semaphore_debug = 0;
82 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
83 SYSCTL_INT(_debug, OID_AUTO, acpi_semaphore_debug, CTLFLAG_RW,
84 &acpi_semaphore_debug, 0, "");
88 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_HANDLE *OutHandle)
90 #ifndef ACPI_NO_SEMAPHORES
91 struct acpi_semaphore *as;
93 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
95 if (OutHandle == NULL)
96 return(AE_BAD_PARAMETER);
97 if (InitialUnits > MaxUnits)
98 return_ACPI_STATUS(AE_BAD_PARAMETER);
100 as = malloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
101 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
102 mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF);
104 as->as_units = InitialUnits;
105 as->as_maxunits = MaxUnits;
106 as->as_pendings = as->as_resetting = as->as_timeouts = 0;
108 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
109 "created semaphore %p max %d, initial %d\n",
110 as, InitialUnits, MaxUnits));
112 *OutHandle = (ACPI_HANDLE)as;
113 return_ACPI_STATUS(AE_OK);
115 *OutHandle = (ACPI_HANDLE)OutHandle;
121 AcpiOsDeleteSemaphore (ACPI_HANDLE Handle)
123 #ifndef ACPI_NO_SEMAPHORES
124 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
125 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
128 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
130 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
131 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
132 mtx_destroy(&as->as_mtx);
134 free(Handle, M_ACPISEM);
135 return_ACPI_STATUS(AE_OK);
142 * This implementation has a bug, in that it has to stall for the entire
143 * timeout before it will return AE_TIME. A better implementation would
144 * use getmicrotime() to correctly adjust the timeout after being woken up.
147 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
149 #ifndef ACPI_NO_SEMAPHORES
150 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
153 struct timeval timeouttv, currenttv, timelefttv;
156 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
159 return_ACPI_STATUS(AE_BAD_PARAMETER);
162 return_ACPI_STATUS(AE_OK);
165 if (as->as_units < Units && as->as_timeouts > 10) {
166 printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
168 as->as_units = as->as_maxunits;
170 as->as_resetting = 1;
174 return_ACPI_STATUS(AE_TIME);
177 if (as->as_resetting) {
178 return_ACPI_STATUS(AE_TIME);
182 /* a timeout of ACPI_WAIT_FOREVER means "forever" */
183 if (Timeout == ACPI_WAIT_FOREVER) {
185 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
186 timeouttv.tv_usec = 0;
188 /* compute timeout using microseconds per tick */
189 tmo = (Timeout * 1000) / (1000000 / hz);
192 timeouttv.tv_sec = Timeout / 1000;
193 timeouttv.tv_usec = (Timeout % 1000) * 1000;
196 /* calculate timeout value in timeval */
197 getmicrotime(¤ttv);
198 timevaladd(&timeouttv, ¤ttv);
201 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
202 "get %d units from semaphore %p (has %d), timeout %d\n",
203 Units, as, as->as_units, Timeout));
205 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
209 if (as->as_units >= Units) {
210 as->as_units -= Units;
215 /* limit number of pending treads */
216 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
221 /* if timeout values of zero is specified, return immediately */
227 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
228 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
229 "semaphore blocked, calling msleep(%p, %p, %d, \"acsem\", %d)\n",
230 as, &as->as_mtx, PCATCH, tmo));
235 if (acpi_semaphore_debug) {
236 printf("%s: Sleep %d, pending %d, semaphore %p, thread %d\n",
237 __func__, Timeout, as->as_pendings, as, AcpiOsGetThreadId());
240 rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo);
245 if (as->as_resetting) {
246 /* semaphore reset, return immediately */
247 if (as->as_pendings == 0) {
248 as->as_resetting = 0;
255 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv));
256 if (rv == EWOULDBLOCK) {
261 /* check if we already awaited enough */
262 timelefttv = timeouttv;
263 getmicrotime(¤ttv);
264 timevalsub(&timelefttv, ¤ttv);
265 if (timelefttv.tv_sec < 0) {
266 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n", as));
271 /* adjust timeout for the next sleep */
272 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) / (1000000 / hz);
276 if (acpi_semaphore_debug) {
277 printf("%s: Wakeup timeleft(%lu, %lu), tmo %u, semaphore %p, thread %d\n",
278 __func__, timelefttv.tv_sec, timelefttv.tv_usec, tmo, as, AcpiOsGetThreadId());
282 if (acpi_semaphore_debug) {
283 if (result == AE_TIME && Timeout > 0) {
284 printf("%s: Timeout %d, pending %d, semaphore %p\n",
285 __func__, Timeout, as->as_pendings, as);
287 if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
288 printf("%s: Acquire %d, units %d, pending %d, semaphore %p, thread %d\n",
289 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
293 if (result == AE_TIME) {
301 return_ACPI_STATUS(result);
308 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
310 #ifndef ACPI_NO_SEMAPHORES
311 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
314 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
317 return_ACPI_STATUS(AE_BAD_PARAMETER);
320 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
321 "return %d units to semaphore %p (has %d)\n",
322 Units, as, as->as_units));
323 if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
324 as->as_units += Units;
325 if (as->as_units > as->as_maxunits)
326 as->as_units = as->as_maxunits;
329 if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
330 printf("%s: Release %d, units %d, pending %d, semaphore %p, thread %d\n",
331 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
336 return_ACPI_STATUS(AE_OK);