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>
40 #include <sys/malloc.h>
41 #include <sys/sysctl.h>
43 #include <sys/thread.h>
44 #include <sys/thread2.h>
45 #include <sys/spinlock2.h>
47 #define _COMPONENT ACPI_OS_SERVICES
48 ACPI_MODULE_NAME("SYNCH")
50 MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
52 #define AS_LOCK(as) spin_lock(&(as)->as_spin)
53 #define AS_UNLOCK(as) spin_unlock(&(as)->as_spin)
57 * Simple counting semaphore implemented using a mutex. (Subsequently used
58 * in the OSI code to implement a mutex. Go figure.)
60 struct acpi_semaphore {
61 struct spinlock as_spin;
69 #ifndef ACPI_NO_SEMAPHORES
70 #ifndef ACPI_SEMAPHORES_MAX_PENDING
71 #define ACPI_SEMAPHORES_MAX_PENDING 4
73 static int acpi_semaphore_debug = 0;
74 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
75 SYSCTL_DECL(_debug_acpi);
76 SYSCTL_INT(_debug_acpi, OID_AUTO, semaphore_debug, CTLFLAG_RW,
77 &acpi_semaphore_debug, 0, "Enable ACPI semaphore debug messages");
78 #endif /* !ACPI_NO_SEMAPHORES */
81 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
82 ACPI_HANDLE *OutHandle)
84 #ifndef ACPI_NO_SEMAPHORES
85 struct acpi_semaphore *as;
87 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
89 if (OutHandle == NULL)
90 return_ACPI_STATUS (AE_BAD_PARAMETER);
91 if (InitialUnits > MaxUnits)
92 return_ACPI_STATUS (AE_BAD_PARAMETER);
94 as = kmalloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
96 spin_init(&as->as_spin);
97 as->as_units = InitialUnits;
98 as->as_maxunits = MaxUnits;
99 as->as_pendings = as->as_resetting = as->as_timeouts = 0;
101 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
102 "created semaphore %p max %d, initial %d\n",
103 as, InitialUnits, MaxUnits));
105 *OutHandle = (ACPI_HANDLE)as;
107 *OutHandle = (ACPI_HANDLE)OutHandle;
108 #endif /* !ACPI_NO_SEMAPHORES */
110 return_ACPI_STATUS (AE_OK);
114 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
116 #ifndef ACPI_NO_SEMAPHORES
117 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
119 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
121 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
122 spin_uninit(&as->as_spin);
123 kfree(as, M_ACPISEM);
124 #endif /* !ACPI_NO_SEMAPHORES */
126 return_ACPI_STATUS (AE_OK);
130 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
132 #ifndef ACPI_NO_SEMAPHORES
134 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
136 struct timeval timeouttv, currenttv, timelefttv;
139 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
142 return_ACPI_STATUS (AE_BAD_PARAMETER);
145 return_ACPI_STATUS (AE_OK);
148 if (as->as_units < Units && as->as_timeouts > 10) {
149 kprintf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
151 as->as_units = as->as_maxunits;
153 as->as_resetting = 1;
157 return_ACPI_STATUS (AE_TIME);
160 if (as->as_resetting)
161 return_ACPI_STATUS (AE_TIME);
164 /* a timeout of ACPI_WAIT_FOREVER means "forever" */
165 if (Timeout == ACPI_WAIT_FOREVER) {
167 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
168 timeouttv.tv_usec = 0;
170 /* compute timeout using microseconds per tick */
171 tmo = (Timeout * 1000) / (1000000 / hz);
174 timeouttv.tv_sec = Timeout / 1000;
175 timeouttv.tv_usec = (Timeout % 1000) * 1000;
178 /* calculate timeout value in timeval */
179 getmicrouptime(¤ttv);
180 timevaladd(&timeouttv, ¤ttv);
183 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
184 "get %d units from semaphore %p (has %d), timeout %d\n",
185 Units, as, as->as_units, Timeout));
187 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
191 if (as->as_units >= Units) {
192 as->as_units -= Units;
197 /* limit number of pending treads */
198 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
203 /* if timeout values of zero is specified, return immediately */
209 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
210 "semaphore blocked, calling ssleep(%p, %p, %d, \"acsem\", %d)\n",
211 as, &as->as_spin, PCATCH, tmo));
215 if (acpi_semaphore_debug) {
216 kprintf("%s: Sleep %jd, pending %jd, semaphore %p, thread %jd\n",
217 __func__, (intmax_t)Timeout,
218 (intmax_t)as->as_pendings, as,
219 (intmax_t)AcpiOsGetThreadId());
222 rv = ssleep(as, &as->as_spin, PCATCH, "acsem", tmo);
227 if (as->as_resetting) {
228 /* semaphore reset, return immediately */
229 if (as->as_pendings == 0) {
230 as->as_resetting = 0;
237 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "ssleep(%d) returned %d\n", tmo, rv));
238 if (rv == EWOULDBLOCK) {
243 /* check if we already awaited enough */
244 timelefttv = timeouttv;
245 getmicrouptime(¤ttv);
246 timevalsub(&timelefttv, ¤ttv);
247 if (timelefttv.tv_sec < 0) {
248 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n",
254 /* adjust timeout for the next sleep */
255 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) /
260 if (acpi_semaphore_debug) {
261 kprintf("%s: Wakeup timeleft(%ju, %ju), tmo %ju, sem %p, thread %jd\n",
263 (intmax_t)timelefttv.tv_sec, (intmax_t)timelefttv.tv_usec,
264 (intmax_t)tmo, as, (intmax_t)AcpiOsGetThreadId());
268 if (acpi_semaphore_debug) {
269 if (result == AE_TIME && Timeout > 0) {
270 kprintf("%s: Timeout %d, pending %d, semaphore %p\n",
271 __func__, Timeout, as->as_pendings, as);
273 if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
274 kprintf("%s: Acquire %d, units %d, pending %d, sem %p, thread %jd\n",
275 __func__, Units, as->as_units, as->as_pendings, as,
276 (intmax_t)AcpiOsGetThreadId());
280 if (result == AE_TIME)
286 return_ACPI_STATUS (result);
288 return_ACPI_STATUS (AE_OK);
289 #endif /* !ACPI_NO_SEMAPHORES */
293 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
295 #ifndef ACPI_NO_SEMAPHORES
296 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
299 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
302 return_ACPI_STATUS(AE_BAD_PARAMETER);
305 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
306 "return %d units to semaphore %p (has %d)\n",
307 Units, as, as->as_units));
308 if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
309 as->as_units += Units;
310 if (as->as_units > as->as_maxunits)
311 as->as_units = as->as_maxunits;
314 if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
315 kprintf("%s: Release %d, units %d, pending %d, semaphore %p, thread %jd\n",
316 __func__, Units, as->as_units, as->as_pendings, as,
317 (intmax_t)AcpiOsGetThreadId());
322 #endif /* !ACPI_NO_SEMAPHORES */
324 return_ACPI_STATUS (AE_OK);
327 struct acpi_spinlock {
328 struct spinlock lock;
329 #ifdef ACPI_DEBUG_LOCKS
337 AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
341 if (OutHandle == NULL)
342 return (AE_BAD_PARAMETER);
343 spin = kmalloc(sizeof(*spin), M_ACPISEM, M_INTWAIT|M_ZERO);
344 spin_init(&spin->lock);
345 #ifdef ACPI_DEBUG_LOCKS
355 AcpiOsDeleteLock (ACPI_SPINLOCK Spin)
359 spin_uninit(&Spin->lock);
360 kfree(Spin, M_ACPISEM);
364 * OS-dependent locking primitives. These routines should be able to be
365 * called from an interrupt-handler or cpu_idle thread.
367 * NB: some of ACPI-CA functions with locking flags, say AcpiSetRegister(),
368 * are changed to unconditionally call AcpiOsAcquireLock/AcpiOsReleaseLock.
371 #ifdef ACPI_DEBUG_LOCKS
372 _AcpiOsAcquireLock (ACPI_SPINLOCK Spin, const char *func, int line)
374 AcpiOsAcquireLock (ACPI_SPINLOCK Spin)
377 spin_lock(&Spin->lock);
379 #ifdef ACPI_DEBUG_LOCKS
381 kprintf("%p(%s:%d): acpi_spinlock %p already held by %p(%s:%d)\n",
382 curthread, func, line, Spin, Spin->owner, Spin->func,
386 Spin->owner = curthread;
395 AcpiOsReleaseLock (ACPI_SPINLOCK Spin, ACPI_CPU_FLAGS Flags)
397 #ifdef ACPI_DEBUG_LOCKS
399 if (Spin->owner != NULL) {
400 kprintf("%p: acpi_spinlock %p is unexectedly held by %p(%s:%d)\n",
401 curthread, Spin, Spin->owner, Spin->func, Spin->line);
410 spin_unlock(&Spin->lock);
413 /* Section 5.2.9.1: global lock acquire/release functions */
414 #define GL_ACQUIRED (-1)
416 #define GL_BIT_PENDING 0x1
417 #define GL_BIT_OWNED 0x2
418 #define GL_BIT_MASK (GL_BIT_PENDING | GL_BIT_OWNED)
421 * Acquire the global lock. If busy, set the pending bit. The caller
422 * will wait for notification from the BIOS that the lock is available
423 * and then attempt to acquire it again.
426 acpi_acquire_global_lock(uint32_t *lock)
432 new = ((old & ~GL_BIT_MASK) | GL_BIT_OWNED) |
433 ((old >> 1) & GL_BIT_PENDING);
434 } while (atomic_cmpset_int(lock, old, new) == 0);
436 return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
440 * Release the global lock, returning whether there is a waiter pending.
441 * If the BIOS set the pending bit, OSPM must notify the BIOS when it
445 acpi_release_global_lock(uint32_t *lock)
451 new = old & ~GL_BIT_MASK;
452 } while (atomic_cmpset_int(lock, old, new) == 0);
454 return (old & GL_BIT_PENDING);