d6f0e478fcdabefc2241002bb3efce89bddb90bd
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdSynch.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
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.11 2007/01/25 15:12:06 y0netan1 Exp $
29  */
30
31 /*
32  * 6.1 : Mutual Exclusion and Synchronisation
33  */
34
35 #include "acpi.h"
36 #include "accommon.h"
37
38 #include "opt_acpi.h"
39
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
43 #include <sys/lock.h>
44 #include <sys/thread.h>
45 #include <sys/thread2.h>
46 #include <sys/spinlock2.h>
47
48 #define _COMPONENT      ACPI_OS_SERVICES
49 ACPI_MODULE_NAME("SYNCH")
50
51 MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
52
53 #define AS_LOCK(as)             spin_lock_wr(&(as)->as_spin)
54 #define AS_UNLOCK(as)           spin_unlock_wr(&(as)->as_spin)
55 #define AS_LOCK_DECL
56
57 /*
58  * Simple counting semaphore implemented using a mutex.  (Subsequently used
59  * in the OSI code to implement a mutex.  Go figure.)
60  */
61 struct acpi_semaphore {
62     struct      spinlock as_spin;
63     UINT32      as_units;
64     UINT32      as_maxunits;
65     UINT32      as_pendings;
66     UINT32      as_resetting;
67     UINT32      as_timeouts;
68 };
69
70 #ifndef ACPI_NO_SEMAPHORES
71 #ifndef ACPI_SEMAPHORES_MAX_PENDING
72 #define ACPI_SEMAPHORES_MAX_PENDING     4
73 #endif
74 static int      acpi_semaphore_debug = 0;
75 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
76 SYSCTL_DECL(_debug_acpi);
77 SYSCTL_INT(_debug_acpi, OID_AUTO, semaphore_debug, CTLFLAG_RW,
78            &acpi_semaphore_debug, 0, "Enable ACPI semaphore debug messages");
79 #endif /* !ACPI_NO_SEMAPHORES */
80
81 ACPI_STATUS
82 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
83     ACPI_HANDLE *OutHandle)
84 {
85 #ifndef ACPI_NO_SEMAPHORES
86     struct acpi_semaphore       *as;
87
88     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
89
90     if (OutHandle == NULL)
91         return_ACPI_STATUS (AE_BAD_PARAMETER);
92     if (InitialUnits > MaxUnits)
93         return_ACPI_STATUS (AE_BAD_PARAMETER);
94
95     as = kmalloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
96
97     spin_init(&as->as_spin);
98     as->as_units = InitialUnits;
99     as->as_maxunits = MaxUnits;
100     as->as_pendings = as->as_resetting = as->as_timeouts = 0;
101
102     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
103         "created semaphore %p max %d, initial %d\n", 
104         as, InitialUnits, MaxUnits));
105
106     *OutHandle = (ACPI_HANDLE)as;
107 #else
108     *OutHandle = (ACPI_HANDLE)OutHandle;
109 #endif /* !ACPI_NO_SEMAPHORES */
110
111     return_ACPI_STATUS (AE_OK);
112 }
113
114 ACPI_STATUS
115 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
116 {
117 #ifndef ACPI_NO_SEMAPHORES
118     struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
119
120     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
121
122     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
123     spin_uninit(&as->as_spin);
124     kfree(as, M_ACPISEM);
125 #endif /* !ACPI_NO_SEMAPHORES */
126
127     return_ACPI_STATUS (AE_OK);
128 }
129
130 ACPI_STATUS
131 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
132 {
133 #ifndef ACPI_NO_SEMAPHORES
134     ACPI_STATUS                 result;
135     struct acpi_semaphore       *as = (struct acpi_semaphore *)Handle;
136     int                         rv, tmo;
137     struct timeval              timeouttv, currenttv, timelefttv;
138     AS_LOCK_DECL;
139
140     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
141
142     if (as == NULL)
143         return_ACPI_STATUS (AE_BAD_PARAMETER);
144
145     if (cold)
146         return_ACPI_STATUS (AE_OK);
147
148 #if 0
149     if (as->as_units < Units && as->as_timeouts > 10) {
150         kprintf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
151         AS_LOCK(as);
152         as->as_units = as->as_maxunits;
153         if (as->as_pendings)
154             as->as_resetting = 1;
155         as->as_timeouts = 0;
156         wakeup(as);
157         AS_UNLOCK(as);
158         return_ACPI_STATUS (AE_TIME);
159     }
160
161     if (as->as_resetting)
162         return_ACPI_STATUS (AE_TIME);
163 #endif
164
165     /* a timeout of ACPI_WAIT_FOREVER means "forever" */
166     if (Timeout == ACPI_WAIT_FOREVER) {
167         tmo = 0;
168         timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
169         timeouttv.tv_usec = 0;
170     } else {
171         /* compute timeout using microseconds per tick */
172         tmo = (Timeout * 1000) / (1000000 / hz);
173         if (tmo <= 0)
174             tmo = 1;
175         timeouttv.tv_sec  = Timeout / 1000;
176         timeouttv.tv_usec = (Timeout % 1000) * 1000;
177     }
178
179     /* calculate timeout value in timeval */
180     getmicrouptime(&currenttv);
181     timevaladd(&timeouttv, &currenttv);
182
183     AS_LOCK(as);
184     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
185         "get %d units from semaphore %p (has %d), timeout %d\n",
186         Units, as, as->as_units, Timeout));
187     for (;;) {
188         if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
189             result = AE_OK;
190             break;
191         }
192         if (as->as_units >= Units) {
193             as->as_units -= Units;
194             result = AE_OK;
195             break;
196         }
197
198         /* limit number of pending treads */
199         if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
200             result = AE_TIME;
201             break;
202         }
203
204         /* if timeout values of zero is specified, return immediately */
205         if (Timeout == 0) {
206             result = AE_TIME;
207             break;
208         }
209
210         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
211             "semaphore blocked, calling ssleep(%p, %p, %d, \"acsem\", %d)\n",
212             as, &as->as_spin, PCATCH, tmo));
213
214         as->as_pendings++;
215
216         if (acpi_semaphore_debug) {
217             kprintf("%s: Sleep %jd, pending %jd, semaphore %p, thread %jd\n",
218                 __func__, (intmax_t)Timeout,
219                 (intmax_t)as->as_pendings, as,
220                 (intmax_t)AcpiOsGetThreadId());
221         }
222
223         rv = ssleep(as, &as->as_spin, PCATCH, "acsem", tmo);
224
225         as->as_pendings--;
226
227 #if 0
228         if (as->as_resetting) {
229             /* semaphore reset, return immediately */
230             if (as->as_pendings == 0) {
231                 as->as_resetting = 0;
232             }
233             result = AE_TIME;
234             break;
235         }
236 #endif
237
238         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "ssleep(%d) returned %d\n", tmo, rv));
239         if (rv == EWOULDBLOCK) {
240             result = AE_TIME;
241             break;
242         }
243
244         /* check if we already awaited enough */
245         timelefttv = timeouttv;
246         getmicrouptime(&currenttv);
247         timevalsub(&timelefttv, &currenttv);
248         if (timelefttv.tv_sec < 0) {
249             ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n",
250                 as));
251             result = AE_TIME;
252             break;
253         }
254
255         /* adjust timeout for the next sleep */
256         tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) /
257             (1000000 / hz);
258         if (tmo <= 0)
259             tmo = 1;
260
261         if (acpi_semaphore_debug) {
262             kprintf("%s: Wakeup timeleft(%ju, %ju), tmo %ju, sem %p, thread %jd\n",
263                 __func__,
264                 (intmax_t)timelefttv.tv_sec, (intmax_t)timelefttv.tv_usec,
265                 (intmax_t)tmo, as, (intmax_t)AcpiOsGetThreadId());
266         }
267     }
268
269     if (acpi_semaphore_debug) {
270         if (result == AE_TIME && Timeout > 0) {
271             kprintf("%s: Timeout %d, pending %d, semaphore %p\n",
272                 __func__, Timeout, as->as_pendings, as);
273         }
274         if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
275             kprintf("%s: Acquire %d, units %d, pending %d, sem %p, thread %jd\n",
276                 __func__, Units, as->as_units, as->as_pendings, as,
277                 (intmax_t)AcpiOsGetThreadId());
278         }
279     }
280
281     if (result == AE_TIME)
282         as->as_timeouts++;
283     else
284         as->as_timeouts = 0;
285
286     AS_UNLOCK(as);
287     return_ACPI_STATUS (result);
288 #else
289     return_ACPI_STATUS (AE_OK);
290 #endif /* !ACPI_NO_SEMAPHORES */
291 }
292
293 ACPI_STATUS
294 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
295 {
296 #ifndef ACPI_NO_SEMAPHORES
297     struct acpi_semaphore       *as = (struct acpi_semaphore *)Handle;
298     AS_LOCK_DECL;
299
300     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
301
302     if (as == NULL)
303         return_ACPI_STATUS(AE_BAD_PARAMETER);
304
305     AS_LOCK(as);
306     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
307         "return %d units to semaphore %p (has %d)\n",
308         Units, as, as->as_units));
309     if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
310         as->as_units += Units;
311         if (as->as_units > as->as_maxunits)
312             as->as_units = as->as_maxunits;
313     }
314
315     if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
316         kprintf("%s: Release %d, units %d, pending %d, semaphore %p, thread %jd\n",
317             __func__, Units, as->as_units, as->as_pendings, as,
318             (intmax_t)AcpiOsGetThreadId());
319     }
320
321     wakeup(as);
322     AS_UNLOCK(as);
323 #endif /* !ACPI_NO_SEMAPHORES */
324
325     return_ACPI_STATUS (AE_OK);
326 }
327
328 struct acpi_spinlock {
329     struct spinlock lock;
330 #ifdef ACPI_DEBUG_LOCKS
331     thread_t    owner;
332     const char *func;
333     int line;
334 #endif
335 };
336
337 ACPI_STATUS
338 AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
339 {
340     ACPI_SPINLOCK spin;
341
342     if (OutHandle == NULL)
343         return (AE_BAD_PARAMETER);
344     spin = kmalloc(sizeof(*spin), M_ACPISEM, M_INTWAIT|M_ZERO);
345     spin_init(&spin->lock);
346 #ifdef ACPI_DEBUG_LOCKS
347     spin->owner = NULL;
348     spin->func = "";
349     spin->line = 0;
350 #endif
351     *OutHandle = spin;
352     return (AE_OK);
353 }
354
355 void
356 AcpiOsDeleteLock (ACPI_SPINLOCK Spin)
357 {
358     if (Spin == NULL)
359         return;
360     spin_uninit(&Spin->lock);
361     kfree(Spin, M_ACPISEM);
362 }
363
364 /*
365  * OS-dependent locking primitives.  These routines should be able to be
366  * called from an interrupt-handler or cpu_idle thread.
367  *
368  * NB: some of ACPI-CA functions with locking flags, say AcpiSetRegister(),
369  * are changed to unconditionally call AcpiOsAcquireLock/AcpiOsReleaseLock.
370  */
371 ACPI_CPU_FLAGS
372 #ifdef ACPI_DEBUG_LOCKS
373 _AcpiOsAcquireLock (ACPI_SPINLOCK Spin, const char *func, int line)
374 #else
375 AcpiOsAcquireLock (ACPI_SPINLOCK Spin)
376 #endif
377 {
378     spin_lock_wr(&Spin->lock);
379
380 #ifdef ACPI_DEBUG_LOCKS
381     if (Spin->owner) {
382         kprintf("%p(%s:%d): acpi_spinlock %p already held by %p(%s:%d)\n",
383                 curthread, func, line, Spin, Spin->owner, Spin->func,
384                 Spin->line);
385         print_backtrace();
386     } else {
387         Spin->owner = curthread;
388         Spin->func = func;
389         Spin->line = line;
390     }
391 #endif
392     return(0);
393 }
394
395 void
396 AcpiOsReleaseLock (ACPI_SPINLOCK Spin, ACPI_CPU_FLAGS Flags)
397 {
398 #ifdef ACPI_DEBUG_LOCKS
399     if (Flags) {
400         if (Spin->owner != NULL) {
401             kprintf("%p: acpi_spinlock %p is unexectedly held by %p(%s:%d)\n",
402                     curthread, Spin, Spin->owner, Spin->func, Spin->line);
403             print_backtrace();
404         } else
405             return;
406     }
407     Spin->owner = NULL;
408     Spin->func = "";
409     Spin->line = 0;
410 #endif
411     spin_unlock_wr(&Spin->lock);
412 }
413
414 /* Section 5.2.9.1:  global lock acquire/release functions */
415 #define GL_ACQUIRED     (-1)
416 #define GL_BUSY         0
417 #define GL_BIT_PENDING  0x1
418 #define GL_BIT_OWNED    0x2
419 #define GL_BIT_MASK     (GL_BIT_PENDING | GL_BIT_OWNED)
420
421 /*
422  * Acquire the global lock.  If busy, set the pending bit.  The caller
423  * will wait for notification from the BIOS that the lock is available
424  * and then attempt to acquire it again.
425  */
426 int
427 acpi_acquire_global_lock(uint32_t *lock)
428 {
429         uint32_t new, old;
430
431         do {
432                 old = *lock;
433                 new = ((old & ~GL_BIT_MASK) | GL_BIT_OWNED) |
434                         ((old >> 1) & GL_BIT_PENDING);
435         } while (atomic_cmpset_int(lock, old, new) == 0);
436
437         return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
438 }
439
440 /*
441  * Release the global lock, returning whether there is a waiter pending.
442  * If the BIOS set the pending bit, OSPM must notify the BIOS when it
443  * releases the lock.
444  */
445 int
446 acpi_release_global_lock(uint32_t *lock)
447 {
448         uint32_t new, old;
449
450         do {
451                 old = *lock;
452                 new = old & ~GL_BIT_MASK;
453         } while (atomic_cmpset_int(lock, old, new) == 0);
454
455         return (old & GL_BIT_PENDING);
456 }