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 $
31 * 6.1 : Mutual Exclusion and Synchronisation
39 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
44 #include <sys/thread.h>
45 #include <sys/thread2.h>
46 #include <sys/spinlock2.h>
48 #include <dev/acpica/acpivar.h>
50 #define _COMPONENT ACPI_OS_SERVICES
51 ACPI_MODULE_NAME("SYNCH")
53 MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
55 #define AS_LOCK(as) spin_lock(&(as)->as_spin)
56 #define AS_UNLOCK(as) spin_unlock(&(as)->as_spin)
60 * Simple counting semaphore implemented using a mutex. (Subsequently used
61 * in the OSI code to implement a mutex. Go figure.)
63 struct acpi_semaphore {
64 struct spinlock as_spin;
72 #ifndef ACPI_NO_SEMAPHORES
73 #ifndef ACPI_SEMAPHORES_MAX_PENDING
74 #define ACPI_SEMAPHORES_MAX_PENDING 4
76 static int acpi_semaphore_debug = 0;
77 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
78 SYSCTL_INT(_debug_acpi, OID_AUTO, semaphore_debug, CTLFLAG_RW,
79 &acpi_semaphore_debug, 0, "Enable ACPI semaphore debug messages");
80 #endif /* !ACPI_NO_SEMAPHORES */
83 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
84 ACPI_HANDLE *OutHandle)
86 #ifndef ACPI_NO_SEMAPHORES
87 struct acpi_semaphore *as;
89 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
91 if (OutHandle == NULL)
92 return_ACPI_STATUS (AE_BAD_PARAMETER);
93 if (InitialUnits > MaxUnits)
94 return_ACPI_STATUS (AE_BAD_PARAMETER);
96 as = kmalloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
98 spin_init(&as->as_spin);
99 as->as_units = InitialUnits;
100 as->as_maxunits = MaxUnits;
101 as->as_pendings = as->as_resetting = as->as_timeouts = 0;
103 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
104 "created semaphore %p max %d, initial %d\n",
105 as, InitialUnits, MaxUnits));
107 *OutHandle = (ACPI_HANDLE)as;
109 *OutHandle = (ACPI_HANDLE)OutHandle;
110 #endif /* !ACPI_NO_SEMAPHORES */
112 return_ACPI_STATUS (AE_OK);
116 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
118 #ifndef ACPI_NO_SEMAPHORES
119 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
121 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
123 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
124 spin_uninit(&as->as_spin);
125 kfree(as, M_ACPISEM);
126 #endif /* !ACPI_NO_SEMAPHORES */
128 return_ACPI_STATUS (AE_OK);
132 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
134 #ifndef ACPI_NO_SEMAPHORES
136 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
138 struct timeval timeouttv, currenttv, timelefttv;
141 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
144 return_ACPI_STATUS (AE_BAD_PARAMETER);
147 return_ACPI_STATUS (AE_OK);
150 if (as->as_units < Units && as->as_timeouts > 10) {
151 kprintf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
153 as->as_units = as->as_maxunits;
155 as->as_resetting = 1;
159 return_ACPI_STATUS (AE_TIME);
162 if (as->as_resetting)
163 return_ACPI_STATUS (AE_TIME);
166 /* a timeout of ACPI_WAIT_FOREVER means "forever" */
167 if (Timeout == ACPI_WAIT_FOREVER) {
169 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
170 timeouttv.tv_usec = 0;
172 /* compute timeout using microseconds per tick */
173 tmo = (Timeout * 1000) / (1000000 / hz);
176 timeouttv.tv_sec = Timeout / 1000;
177 timeouttv.tv_usec = (Timeout % 1000) * 1000;
180 /* calculate timeout value in timeval */
181 getmicrouptime(¤ttv);
182 timevaladd(&timeouttv, ¤ttv);
185 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
186 "get %d units from semaphore %p (has %d), timeout %d\n",
187 Units, as, as->as_units, Timeout));
189 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
193 if (as->as_units >= Units) {
194 as->as_units -= Units;
199 /* limit number of pending treads */
200 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
205 /* if timeout values of zero is specified, return immediately */
211 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
212 "semaphore blocked, calling ssleep(%p, %p, %d, \"acsem\", %d)\n",
213 as, &as->as_spin, PCATCH, tmo));
217 if (acpi_semaphore_debug) {
218 kprintf("%s: Sleep %jd, pending %jd, semaphore %p, thread %jd\n",
219 __func__, (intmax_t)Timeout,
220 (intmax_t)as->as_pendings, as,
221 (intmax_t)AcpiOsGetThreadId());
224 rv = ssleep(as, &as->as_spin, PCATCH, "acsem", tmo);
229 if (as->as_resetting) {
230 /* semaphore reset, return immediately */
231 if (as->as_pendings == 0) {
232 as->as_resetting = 0;
239 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "ssleep(%d) returned %d\n", tmo, rv));
240 if (rv == EWOULDBLOCK) {
245 /* check if we already awaited enough */
246 timelefttv = timeouttv;
247 getmicrouptime(¤ttv);
248 timevalsub(&timelefttv, ¤ttv);
249 if (timelefttv.tv_sec < 0) {
250 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n",
256 /* adjust timeout for the next sleep */
257 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) /
262 if (acpi_semaphore_debug) {
263 kprintf("%s: Wakeup timeleft(%ju, %ju), tmo %ju, sem %p, thread %jd\n",
265 (intmax_t)timelefttv.tv_sec, (intmax_t)timelefttv.tv_usec,
266 (intmax_t)tmo, as, (intmax_t)AcpiOsGetThreadId());
270 if (acpi_semaphore_debug) {
271 if (result == AE_TIME && Timeout > 0) {
272 kprintf("%s: Timeout %d, pending %d, semaphore %p\n",
273 __func__, Timeout, as->as_pendings, as);
275 if (ACPI_SUCCESS(result) &&
276 (as->as_timeouts > 0 || as->as_pendings > 0))
278 kprintf("%s: Acquire %d, units %d, pending %d, sem %p, thread %jd\n",
279 __func__, Units, as->as_units, as->as_pendings, as,
280 (intmax_t)AcpiOsGetThreadId());
284 if (result == AE_TIME)
290 return_ACPI_STATUS (result);
292 return_ACPI_STATUS (AE_OK);
293 #endif /* !ACPI_NO_SEMAPHORES */
297 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
299 #ifndef ACPI_NO_SEMAPHORES
300 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
303 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
306 return_ACPI_STATUS(AE_BAD_PARAMETER);
309 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
310 "return %d units to semaphore %p (has %d)\n",
311 Units, as, as->as_units));
312 if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
313 as->as_units += Units;
314 if (as->as_units > as->as_maxunits)
315 as->as_units = as->as_maxunits;
318 if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
319 kprintf("%s: Release %d, units %d, pending %d, semaphore %p, thread %jd\n",
320 __func__, Units, as->as_units, as->as_pendings, as,
321 (intmax_t)AcpiOsGetThreadId());
326 #endif /* !ACPI_NO_SEMAPHORES */
328 return_ACPI_STATUS (AE_OK);
331 struct acpi_spinlock {
332 struct spinlock lock;
333 #ifdef ACPI_DEBUG_LOCKS
341 AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
345 if (OutHandle == NULL)
346 return (AE_BAD_PARAMETER);
347 spin = kmalloc(sizeof(*spin), M_ACPISEM, M_INTWAIT|M_ZERO);
348 spin_init(&spin->lock);
349 #ifdef ACPI_DEBUG_LOCKS
359 AcpiOsDeleteLock (ACPI_SPINLOCK Spin)
363 spin_uninit(&Spin->lock);
364 kfree(Spin, M_ACPISEM);
368 * OS-dependent locking primitives. These routines should be able to be
369 * called from an interrupt-handler or cpu_idle thread.
371 * NB: some of ACPI-CA functions with locking flags, say AcpiSetRegister(),
372 * are changed to unconditionally call AcpiOsAcquireLock/AcpiOsReleaseLock.
375 #ifdef ACPI_DEBUG_LOCKS
376 _AcpiOsAcquireLock (ACPI_SPINLOCK Spin, const char *func, int line)
378 AcpiOsAcquireLock (ACPI_SPINLOCK Spin)
381 spin_lock(&Spin->lock);
383 #ifdef ACPI_DEBUG_LOCKS
385 kprintf("%p(%s:%d): acpi_spinlock %p already held by %p(%s:%d)\n",
386 curthread, func, line, Spin, Spin->owner, Spin->func,
390 Spin->owner = curthread;
399 AcpiOsReleaseLock (ACPI_SPINLOCK Spin, ACPI_CPU_FLAGS Flags)
401 #ifdef ACPI_DEBUG_LOCKS
403 if (Spin->owner != NULL) {
404 kprintf("%p: acpi_spinlock %p is unexectedly held by %p(%s:%d)\n",
405 curthread, Spin, Spin->owner, Spin->func, Spin->line);
414 spin_unlock(&Spin->lock);
417 /* Section 5.2.9.1: global lock acquire/release functions */
418 #define GL_ACQUIRED (-1)
420 #define GL_BIT_PENDING 0x1
421 #define GL_BIT_OWNED 0x2
422 #define GL_BIT_MASK (GL_BIT_PENDING | GL_BIT_OWNED)
425 * Acquire the global lock. If busy, set the pending bit. The caller
426 * will wait for notification from the BIOS that the lock is available
427 * and then attempt to acquire it again.
430 acpi_acquire_global_lock(uint32_t *lock)
436 new = ((old & ~GL_BIT_MASK) | GL_BIT_OWNED) |
437 ((old >> 1) & GL_BIT_PENDING);
438 } while (atomic_cmpset_int(lock, old, new) == 0);
440 return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
444 * Release the global lock, returning whether there is a waiter pending.
445 * If the BIOS set the pending bit, OSPM must notify the BIOS when it
449 acpi_release_global_lock(uint32_t *lock)
455 new = old & ~GL_BIT_MASK;
456 } while (atomic_cmpset_int(lock, old, new) == 0);
458 return (old & GL_BIT_PENDING);