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.21 2004/05/05 20:07:52 njl Exp $
28 * $DragonFly: src/sys/dev/acpica5/Osd/OsdSynch.c,v 1.8 2006/09/05 00:55:36 dillon Exp $
32 * 6.1 : Mutual Exclusion and Synchronisation
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
42 #include <sys/thread.h>
43 #include <sys/thread2.h>
45 #define _COMPONENT ACPI_OS_SERVICES
46 ACPI_MODULE_NAME("SYNCH")
48 MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
50 #if defined(__DragonFly__)
51 # define AS_LOCK(as) crit_enter()
52 # define AS_UNLOCK(as) crit_exit()
54 # define msleep(a, b, c, d, e) tsleep(a, c, d, e)
55 #elif __FreeBSD_version < 500000
56 # define AS_LOCK(as) s = splhigh()
57 # define AS_UNLOCK(as) splx(s)
58 # define AS_LOCK_DECL int s
59 # define msleep(a, b, c, d, e) tsleep(a, c, d, e)
61 # define AS_LOCK(as) mtx_lock(&(as)->as_mtx)
62 # define AS_UNLOCK(as) mtx_unlock(&(as)->as_mtx)
67 * Simple counting semaphore implemented using a mutex. (Subsequently used
68 * in the OSI code to implement a mutex. Go figure.)
70 struct acpi_semaphore {
71 #if __FreeBSD_version >= 500000
81 #ifndef ACPI_NO_SEMAPHORES
82 #ifndef ACPI_SEMAPHORES_MAX_PENDING
83 #define ACPI_SEMAPHORES_MAX_PENDING 4
85 static int acpi_semaphore_debug = 0;
86 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
87 SYSCTL_DECL(_debug_acpi);
88 SYSCTL_INT(_debug_acpi, OID_AUTO, semaphore_debug, CTLFLAG_RW,
89 &acpi_semaphore_debug, 0, "Enable ACPI semaphore debug messages");
90 #endif /* !ACPI_NO_SEMAPHORES */
93 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
94 ACPI_HANDLE *OutHandle)
96 #ifndef ACPI_NO_SEMAPHORES
97 struct acpi_semaphore *as;
99 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
101 if (OutHandle == NULL)
102 return_ACPI_STATUS (AE_BAD_PARAMETER);
103 if (InitialUnits > MaxUnits)
104 return_ACPI_STATUS (AE_BAD_PARAMETER);
106 as = kmalloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
108 #if __FreeBSD_version >= 500000
109 mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF);
111 as->as_units = InitialUnits;
112 as->as_maxunits = MaxUnits;
113 as->as_pendings = as->as_resetting = as->as_timeouts = 0;
115 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
116 "created semaphore %p max %d, initial %d\n",
117 as, InitialUnits, MaxUnits));
119 *OutHandle = (ACPI_HANDLE)as;
121 *OutHandle = (ACPI_HANDLE)OutHandle;
122 #endif /* !ACPI_NO_SEMAPHORES */
124 return_ACPI_STATUS (AE_OK);
128 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
130 #ifndef ACPI_NO_SEMAPHORES
131 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
133 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
135 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
136 #if __FreeBSD_version >= 500000
137 mtx_destroy(&as->as_mtx);
139 kfree(as, M_ACPISEM);
140 #endif /* !ACPI_NO_SEMAPHORES */
142 return_ACPI_STATUS (AE_OK);
146 * This implementation has a bug, in that it has to stall for the entire
147 * timeout before it will return AE_TIME. A better implementation would
148 * use getmicrotime() to correctly adjust the timeout after being woken up.
151 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
153 #ifndef ACPI_NO_SEMAPHORES
155 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
157 struct timeval timeouttv, currenttv, timelefttv;
160 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
163 return_ACPI_STATUS (AE_BAD_PARAMETER);
166 return_ACPI_STATUS (AE_OK);
169 if (as->as_units < Units && as->as_timeouts > 10) {
170 printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
172 as->as_units = as->as_maxunits;
174 as->as_resetting = 1;
178 return_ACPI_STATUS (AE_TIME);
181 if (as->as_resetting)
182 return_ACPI_STATUS (AE_TIME);
185 /* a timeout of ACPI_WAIT_FOREVER means "forever" */
186 if (Timeout == ACPI_WAIT_FOREVER) {
188 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
189 timeouttv.tv_usec = 0;
191 /* compute timeout using microseconds per tick */
192 tmo = (Timeout * 1000) / (1000000 / hz);
195 timeouttv.tv_sec = Timeout / 1000;
196 timeouttv.tv_usec = (Timeout % 1000) * 1000;
199 /* calculate timeout value in timeval */
200 getmicrotime(¤ttv);
201 timevaladd(&timeouttv, ¤ttv);
204 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
205 "get %d units from semaphore %p (has %d), timeout %d\n",
206 Units, as, as->as_units, Timeout));
208 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
212 if (as->as_units >= Units) {
213 as->as_units -= Units;
218 /* limit number of pending treads */
219 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
224 /* if timeout values of zero is specified, return immediately */
230 #if __FreeBSD_version >= 500000
231 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
232 "semaphore blocked, calling msleep(%p, %p, %d, \"acsem\", %d)\n",
233 as, &as->as_mtx, PCATCH, tmo));
238 if (acpi_semaphore_debug) {
239 printf("%s: Sleep %d, pending %d, semaphore %p, thread %d\n",
240 __func__, Timeout, as->as_pendings, as, AcpiOsGetThreadId());
243 rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo);
248 if (as->as_resetting) {
249 /* semaphore reset, return immediately */
250 if (as->as_pendings == 0) {
251 as->as_resetting = 0;
258 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv));
259 if (rv == EWOULDBLOCK) {
264 /* check if we already awaited enough */
265 timelefttv = timeouttv;
266 getmicrotime(¤ttv);
267 timevalsub(&timelefttv, ¤ttv);
268 if (timelefttv.tv_sec < 0) {
269 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n",
275 /* adjust timeout for the next sleep */
276 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) /
281 if (acpi_semaphore_debug) {
282 printf("%s: Wakeup timeleft(%lu, %lu), tmo %u, sem %p, thread %d\n",
283 __func__, timelefttv.tv_sec, timelefttv.tv_usec, tmo, as,
284 AcpiOsGetThreadId());
288 if (acpi_semaphore_debug) {
289 if (result == AE_TIME && Timeout > 0) {
290 printf("%s: Timeout %d, pending %d, semaphore %p\n",
291 __func__, Timeout, as->as_pendings, as);
293 if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
294 printf("%s: Acquire %d, units %d, pending %d, sem %p, thread %d\n",
295 __func__, Units, as->as_units, as->as_pendings, as,
296 AcpiOsGetThreadId());
300 if (result == AE_TIME)
306 return_ACPI_STATUS (result);
308 return_ACPI_STATUS (AE_OK);
309 #endif /* !ACPI_NO_SEMAPHORES */
313 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
315 #ifndef ACPI_NO_SEMAPHORES
316 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
319 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
322 return_ACPI_STATUS(AE_BAD_PARAMETER);
325 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
326 "return %d units to semaphore %p (has %d)\n",
327 Units, as, as->as_units));
328 if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
329 as->as_units += Units;
330 if (as->as_units > as->as_maxunits)
331 as->as_units = as->as_maxunits;
334 if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
335 printf("%s: Release %d, units %d, pending %d, semaphore %p, thread %d\n",
336 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
341 #endif /* !ACPI_NO_SEMAPHORES */
343 return_ACPI_STATUS (AE_OK);
347 AcpiOsCreateLock(ACPI_HANDLE *OutHandle)
351 if (OutHandle == NULL)
352 return (AE_BAD_PARAMETER);
353 lock = kmalloc(sizeof(*lock), M_ACPISEM, M_INTWAIT|M_ZERO);
354 lockinit(lock, "oslck", 0, 0);
355 *OutHandle = (ACPI_HANDLE)lock;
360 AcpiOsDeleteLock (ACPI_HANDLE Handle)
364 if ((lock = (struct lock *)Handle) != NULL)
365 kfree(lock, M_ACPISEM);
369 * The Flags parameter seems to state whether or not caller is an ISR
370 * (and thus can't block) but since we have ithreads, we don't worry
371 * about potentially blocking.
374 AcpiOsAcquireLock (ACPI_HANDLE Handle, UINT32 Flags)
378 if ((lock = (struct lock *)Handle) != NULL)
379 lockmgr(lock, LK_EXCLUSIVE|LK_RETRY);
383 AcpiOsReleaseLock (ACPI_HANDLE Handle, UINT32 Flags)
387 if ((lock = (struct lock *)Handle) != NULL)
388 lockmgr(lock, LK_RELEASE);
392 /* Section 5.2.9.1: global lock acquire/release functions */
393 #define GL_ACQUIRED (-1)
395 #define GL_BIT_PENDING 0x1
396 #define GL_BIT_OWNED 0x2
397 #define GL_BIT_MASK (GL_BIT_PENDING | GL_BIT_OWNED)
400 * Acquire the global lock. If busy, set the pending bit. The caller
401 * will wait for notification from the BIOS that the lock is available
402 * and then attempt to acquire it again.
405 acpi_acquire_global_lock(uint32_t *lock)
411 new = ((old & ~GL_BIT_MASK) | GL_BIT_OWNED) |
412 ((old >> 1) & GL_BIT_PENDING);
413 } while (atomic_cmpset_acq_int(lock, old, new) == 0);
415 return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
419 * Release the global lock, returning whether there is a waiter pending.
420 * If the BIOS set the pending bit, OSPM must notify the BIOS when it
424 acpi_release_global_lock(uint32_t *lock)
430 new = old & ~GL_BIT_MASK;
431 } while (atomic_cmpset_rel_int(lock, old, new) == 0);
433 return (old & GL_BIT_PENDING);