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.4 2004/06/27 08:52:42 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 #include <sys/thread.h>
42 #include <sys/thread2.h>
44 #define _COMPONENT ACPI_OS_SERVICES
45 ACPI_MODULE_NAME("SYNCH")
47 MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
49 #if defined(__DragonFly__)
50 # define AS_LOCK(as) s = splhigh()
51 # define AS_UNLOCK(as) splx(s)
52 # define AS_LOCK_DECL int s
53 # define msleep(a, b, c, d, e) tsleep(a, c, d, e)
54 #elif __FreeBSD_version < 500000
55 # define AS_LOCK(as) s = splhigh()
56 # define AS_UNLOCK(as) splx(s)
57 # define AS_LOCK_DECL int s
58 # define msleep(a, b, c, d, e) tsleep(a, c, d, e)
60 # define AS_LOCK(as) mtx_lock(&(as)->as_mtx)
61 # define AS_UNLOCK(as) mtx_unlock(&(as)->as_mtx)
66 * Simple counting semaphore implemented using a mutex. (Subsequently used
67 * in the OSI code to implement a mutex. Go figure.)
69 struct acpi_semaphore {
70 #if __FreeBSD_version >= 500000
80 #ifndef ACPI_NO_SEMAPHORES
81 #ifndef ACPI_SEMAPHORES_MAX_PENDING
82 #define ACPI_SEMAPHORES_MAX_PENDING 4
84 static int acpi_semaphore_debug = 0;
85 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
86 SYSCTL_DECL(_debug_acpi);
87 SYSCTL_INT(_debug_acpi, OID_AUTO, semaphore_debug, CTLFLAG_RW,
88 &acpi_semaphore_debug, 0, "Enable ACPI semaphore debug messages");
89 #endif /* !ACPI_NO_SEMAPHORES */
92 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
93 ACPI_HANDLE *OutHandle)
95 #ifndef ACPI_NO_SEMAPHORES
96 struct acpi_semaphore *as;
98 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
100 if (OutHandle == NULL)
101 return_ACPI_STATUS (AE_BAD_PARAMETER);
102 if (InitialUnits > MaxUnits)
103 return_ACPI_STATUS (AE_BAD_PARAMETER);
105 as = malloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
107 #if __FreeBSD_version >= 500000
108 mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF);
110 as->as_units = InitialUnits;
111 as->as_maxunits = MaxUnits;
112 as->as_pendings = as->as_resetting = as->as_timeouts = 0;
114 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
115 "created semaphore %p max %d, initial %d\n",
116 as, InitialUnits, MaxUnits));
118 *OutHandle = (ACPI_HANDLE)as;
120 *OutHandle = (ACPI_HANDLE)OutHandle;
121 #endif /* !ACPI_NO_SEMAPHORES */
123 return_ACPI_STATUS (AE_OK);
127 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
129 #ifndef ACPI_NO_SEMAPHORES
130 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
132 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
134 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
135 #if __FreeBSD_version >= 500000
136 mtx_destroy(&as->as_mtx);
138 free(Handle, M_ACPISEM);
139 #endif /* !ACPI_NO_SEMAPHORES */
141 return_ACPI_STATUS (AE_OK);
145 * This implementation has a bug, in that it has to stall for the entire
146 * timeout before it will return AE_TIME. A better implementation would
147 * use getmicrotime() to correctly adjust the timeout after being woken up.
150 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
152 #ifndef ACPI_NO_SEMAPHORES
154 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
156 struct timeval timeouttv, currenttv, timelefttv;
159 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
162 return_ACPI_STATUS (AE_BAD_PARAMETER);
165 return_ACPI_STATUS (AE_OK);
168 if (as->as_units < Units && as->as_timeouts > 10) {
169 printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
171 as->as_units = as->as_maxunits;
173 as->as_resetting = 1;
177 return_ACPI_STATUS (AE_TIME);
180 if (as->as_resetting)
181 return_ACPI_STATUS (AE_TIME);
184 /* a timeout of ACPI_WAIT_FOREVER means "forever" */
185 if (Timeout == ACPI_WAIT_FOREVER) {
187 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
188 timeouttv.tv_usec = 0;
190 /* compute timeout using microseconds per tick */
191 tmo = (Timeout * 1000) / (1000000 / hz);
194 timeouttv.tv_sec = Timeout / 1000;
195 timeouttv.tv_usec = (Timeout % 1000) * 1000;
198 /* calculate timeout value in timeval */
199 getmicrotime(¤ttv);
200 timevaladd(&timeouttv, ¤ttv);
203 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
204 "get %d units from semaphore %p (has %d), timeout %d\n",
205 Units, as, as->as_units, Timeout));
207 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
211 if (as->as_units >= Units) {
212 as->as_units -= Units;
217 /* limit number of pending treads */
218 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
223 /* if timeout values of zero is specified, return immediately */
229 #if __FreeBSD_version >= 500000
230 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
231 "semaphore blocked, calling msleep(%p, %p, %d, \"acsem\", %d)\n",
232 as, &as->as_mtx, PCATCH, tmo));
237 if (acpi_semaphore_debug) {
238 printf("%s: Sleep %d, pending %d, semaphore %p, thread %d\n",
239 __func__, Timeout, as->as_pendings, as, AcpiOsGetThreadId());
242 rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo);
247 if (as->as_resetting) {
248 /* semaphore reset, return immediately */
249 if (as->as_pendings == 0) {
250 as->as_resetting = 0;
257 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv));
258 if (rv == EWOULDBLOCK) {
263 /* check if we already awaited enough */
264 timelefttv = timeouttv;
265 getmicrotime(¤ttv);
266 timevalsub(&timelefttv, ¤ttv);
267 if (timelefttv.tv_sec < 0) {
268 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n",
274 /* adjust timeout for the next sleep */
275 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) /
280 if (acpi_semaphore_debug) {
281 printf("%s: Wakeup timeleft(%lu, %lu), tmo %u, sem %p, thread %d\n",
282 __func__, timelefttv.tv_sec, timelefttv.tv_usec, tmo, as,
283 AcpiOsGetThreadId());
287 if (acpi_semaphore_debug) {
288 if (result == AE_TIME && Timeout > 0) {
289 printf("%s: Timeout %d, pending %d, semaphore %p\n",
290 __func__, Timeout, as->as_pendings, as);
292 if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
293 printf("%s: Acquire %d, units %d, pending %d, sem %p, thread %d\n",
294 __func__, Units, as->as_units, as->as_pendings, as,
295 AcpiOsGetThreadId());
299 if (result == AE_TIME)
305 return_ACPI_STATUS (result);
307 return_ACPI_STATUS (AE_OK);
308 #endif /* !ACPI_NO_SEMAPHORES */
312 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
314 #ifndef ACPI_NO_SEMAPHORES
315 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
318 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
321 return_ACPI_STATUS(AE_BAD_PARAMETER);
324 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
325 "return %d units to semaphore %p (has %d)\n",
326 Units, as, as->as_units));
327 if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
328 as->as_units += Units;
329 if (as->as_units > as->as_maxunits)
330 as->as_units = as->as_maxunits;
333 if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
334 printf("%s: Release %d, units %d, pending %d, semaphore %p, thread %d\n",
335 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
340 #endif /* !ACPI_NO_SEMAPHORES */
342 return_ACPI_STATUS (AE_OK);
346 AcpiOsCreateLock(ACPI_HANDLE *OutHandle)
350 if (OutHandle == NULL)
351 return (AE_BAD_PARAMETER);
352 MALLOC(lock, lwkt_rwlock_t, sizeof(*lock), M_ACPISEM, M_INTWAIT | M_ZERO);
354 return (AE_NO_MEMORY);
356 lwkt_rwlock_init(lock);
357 *OutHandle = (ACPI_HANDLE)lock;
362 AcpiOsDeleteLock (ACPI_HANDLE Handle)
364 lwkt_rwlock_t lock = (lwkt_rwlock_t)Handle;
368 lwkt_rwlock_uninit(lock);
372 * The Flags parameter seems to state whether or not caller is an ISR
373 * (and thus can't block) but since we have ithreads, we don't worry
374 * about potentially blocking.
377 AcpiOsAcquireLock (ACPI_HANDLE Handle, UINT32 Flags)
379 lwkt_rwlock_t lock = (lwkt_rwlock_t)Handle;
383 lwkt_exlock(lock, "acpi1");
387 AcpiOsReleaseLock (ACPI_HANDLE Handle, UINT32 Flags)
389 lwkt_rwlock_t lock = (lwkt_rwlock_t)Handle;
397 /* Section 5.2.9.1: global lock acquire/release functions */
398 #define GL_ACQUIRED (-1)
400 #define GL_BIT_PENDING 0x1
401 #define GL_BIT_OWNED 0x2
402 #define GL_BIT_MASK (GL_BIT_PENDING | GL_BIT_OWNED)
405 * Acquire the global lock. If busy, set the pending bit. The caller
406 * will wait for notification from the BIOS that the lock is available
407 * and then attempt to acquire it again.
410 acpi_acquire_global_lock(uint32_t *lock)
416 new = ((old & ~GL_BIT_MASK) | GL_BIT_OWNED) |
417 ((old >> 1) & GL_BIT_PENDING);
418 } while (atomic_cmpset_acq_int(lock, old, new) == 0);
420 return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
424 * Release the global lock, returning whether there is a waiter pending.
425 * If the BIOS set the pending bit, OSPM must notify the BIOS when it
429 acpi_release_global_lock(uint32_t *lock)
435 new = old & ~GL_BIT_MASK;
436 } while (atomic_cmpset_rel_int(lock, old, new) == 0);
438 return (old & GL_BIT_PENDING);