Rename malloc->kmalloc, free->kfree, and realloc->krealloc. Pass 1
[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.8 2006/09/05 00:55:36 dillon Exp $
29  */
30
31 /*
32  * 6.1 : Mutual Exclusion and Synchronisation
33  */
34
35 #include "acpi.h"
36
37 #include "opt_acpi.h"
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/sysctl.h>
41 #include <sys/lock.h>
42 #include <sys/thread.h>
43 #include <sys/thread2.h>
44
45 #define _COMPONENT      ACPI_OS_SERVICES
46 ACPI_MODULE_NAME("SYNCH")
47
48 MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
49
50 #if defined(__DragonFly__)
51 # define AS_LOCK(as)            crit_enter()
52 # define AS_UNLOCK(as)          crit_exit()
53 # define AS_LOCK_DECL
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)
60 #else
61 # define AS_LOCK(as)            mtx_lock(&(as)->as_mtx)
62 # define AS_UNLOCK(as)          mtx_unlock(&(as)->as_mtx)
63 # define AS_LOCK_DECL
64 #endif
65
66 /*
67  * Simple counting semaphore implemented using a mutex.  (Subsequently used
68  * in the OSI code to implement a mutex.  Go figure.)
69  */
70 struct acpi_semaphore {
71 #if __FreeBSD_version >= 500000
72     struct mtx  as_mtx;
73 #endif
74     UINT32      as_units;
75     UINT32      as_maxunits;
76     UINT32      as_pendings;
77     UINT32      as_resetting;
78     UINT32      as_timeouts;
79 };
80
81 #ifndef ACPI_NO_SEMAPHORES
82 #ifndef ACPI_SEMAPHORES_MAX_PENDING
83 #define ACPI_SEMAPHORES_MAX_PENDING     4
84 #endif
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 */
91
92 ACPI_STATUS
93 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
94     ACPI_HANDLE *OutHandle)
95 {
96 #ifndef ACPI_NO_SEMAPHORES
97     struct acpi_semaphore       *as;
98
99     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
100
101     if (OutHandle == NULL)
102         return_ACPI_STATUS (AE_BAD_PARAMETER);
103     if (InitialUnits > MaxUnits)
104         return_ACPI_STATUS (AE_BAD_PARAMETER);
105
106     as = kmalloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
107
108 #if __FreeBSD_version >= 500000
109     mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF);
110 #endif
111     as->as_units = InitialUnits;
112     as->as_maxunits = MaxUnits;
113     as->as_pendings = as->as_resetting = as->as_timeouts = 0;
114
115     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
116         "created semaphore %p max %d, initial %d\n", 
117         as, InitialUnits, MaxUnits));
118
119     *OutHandle = (ACPI_HANDLE)as;
120 #else
121     *OutHandle = (ACPI_HANDLE)OutHandle;
122 #endif /* !ACPI_NO_SEMAPHORES */
123
124     return_ACPI_STATUS (AE_OK);
125 }
126
127 ACPI_STATUS
128 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
129 {
130 #ifndef ACPI_NO_SEMAPHORES
131     struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
132
133     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
134
135     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
136 #if __FreeBSD_version >= 500000
137     mtx_destroy(&as->as_mtx);
138 #endif
139     kfree(as, M_ACPISEM);
140 #endif /* !ACPI_NO_SEMAPHORES */
141
142     return_ACPI_STATUS (AE_OK);
143 }
144
145 /*
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.
149  */
150 ACPI_STATUS
151 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
152 {
153 #ifndef ACPI_NO_SEMAPHORES
154     ACPI_STATUS                 result;
155     struct acpi_semaphore       *as = (struct acpi_semaphore *)Handle;
156     int                         rv, tmo;
157     struct timeval              timeouttv, currenttv, timelefttv;
158     AS_LOCK_DECL;
159
160     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
161
162     if (as == NULL)
163         return_ACPI_STATUS (AE_BAD_PARAMETER);
164
165     if (cold)
166         return_ACPI_STATUS (AE_OK);
167
168 #if 0
169     if (as->as_units < Units && as->as_timeouts > 10) {
170         printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
171         AS_LOCK(as);
172         as->as_units = as->as_maxunits;
173         if (as->as_pendings)
174             as->as_resetting = 1;
175         as->as_timeouts = 0;
176         wakeup(as);
177         AS_UNLOCK(as);
178         return_ACPI_STATUS (AE_TIME);
179     }
180
181     if (as->as_resetting)
182         return_ACPI_STATUS (AE_TIME);
183 #endif
184
185     /* a timeout of ACPI_WAIT_FOREVER means "forever" */
186     if (Timeout == ACPI_WAIT_FOREVER) {
187         tmo = 0;
188         timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
189         timeouttv.tv_usec = 0;
190     } else {
191         /* compute timeout using microseconds per tick */
192         tmo = (Timeout * 1000) / (1000000 / hz);
193         if (tmo <= 0)
194             tmo = 1;
195         timeouttv.tv_sec  = Timeout / 1000;
196         timeouttv.tv_usec = (Timeout % 1000) * 1000;
197     }
198
199     /* calculate timeout value in timeval */
200     getmicrotime(&currenttv);
201     timevaladd(&timeouttv, &currenttv);
202
203     AS_LOCK(as);
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));
207     for (;;) {
208         if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
209             result = AE_OK;
210             break;
211         }
212         if (as->as_units >= Units) {
213             as->as_units -= Units;
214             result = AE_OK;
215             break;
216         }
217
218         /* limit number of pending treads */
219         if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
220             result = AE_TIME;
221             break;
222         }
223
224         /* if timeout values of zero is specified, return immediately */
225         if (Timeout == 0) {
226             result = AE_TIME;
227             break;
228         }
229
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));
234 #endif
235
236         as->as_pendings++;
237
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());
241         }
242
243         rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo);
244
245         as->as_pendings--;
246
247 #if 0
248         if (as->as_resetting) {
249             /* semaphore reset, return immediately */
250             if (as->as_pendings == 0) {
251                 as->as_resetting = 0;
252             }
253             result = AE_TIME;
254             break;
255         }
256 #endif
257
258         ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv));
259         if (rv == EWOULDBLOCK) {
260             result = AE_TIME;
261             break;
262         }
263
264         /* check if we already awaited enough */
265         timelefttv = timeouttv;
266         getmicrotime(&currenttv);
267         timevalsub(&timelefttv, &currenttv);
268         if (timelefttv.tv_sec < 0) {
269             ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n",
270                 as));
271             result = AE_TIME;
272             break;
273         }
274
275         /* adjust timeout for the next sleep */
276         tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) /
277             (1000000 / hz);
278         if (tmo <= 0)
279             tmo = 1;
280
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());
285         }
286     }
287
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);
292         }
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());
297         }
298     }
299
300     if (result == AE_TIME)
301         as->as_timeouts++;
302     else
303         as->as_timeouts = 0;
304
305     AS_UNLOCK(as);
306     return_ACPI_STATUS (result);
307 #else
308     return_ACPI_STATUS (AE_OK);
309 #endif /* !ACPI_NO_SEMAPHORES */
310 }
311
312 ACPI_STATUS
313 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
314 {
315 #ifndef ACPI_NO_SEMAPHORES
316     struct acpi_semaphore       *as = (struct acpi_semaphore *)Handle;
317     AS_LOCK_DECL;
318
319     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
320
321     if (as == NULL)
322         return_ACPI_STATUS(AE_BAD_PARAMETER);
323
324     AS_LOCK(as);
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;
332     }
333
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());
337     }
338
339     wakeup(as);
340     AS_UNLOCK(as);
341 #endif /* !ACPI_NO_SEMAPHORES */
342
343     return_ACPI_STATUS (AE_OK);
344 }
345
346 ACPI_STATUS
347 AcpiOsCreateLock(ACPI_HANDLE *OutHandle)
348 {
349     struct lock *lock;
350
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;
356     return (AE_OK);
357 }
358
359 void
360 AcpiOsDeleteLock (ACPI_HANDLE Handle)
361 {
362     struct lock *lock;
363
364     if ((lock = (struct lock *)Handle) != NULL)
365             kfree(lock, M_ACPISEM);
366 }
367
368 /*
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.
372  */
373 void
374 AcpiOsAcquireLock (ACPI_HANDLE Handle, UINT32 Flags)
375 {
376     struct lock *lock;
377
378     if ((lock = (struct lock *)Handle) != NULL)
379         lockmgr(lock, LK_EXCLUSIVE|LK_RETRY);
380 }
381
382 void
383 AcpiOsReleaseLock (ACPI_HANDLE Handle, UINT32 Flags)
384 {
385     struct lock *lock;
386
387     if ((lock = (struct lock *)Handle) != NULL)
388         lockmgr(lock, LK_RELEASE);
389 }
390
391 #ifdef notyet
392 /* Section 5.2.9.1:  global lock acquire/release functions */
393 #define GL_ACQUIRED     (-1)
394 #define GL_BUSY         0
395 #define GL_BIT_PENDING  0x1
396 #define GL_BIT_OWNED    0x2
397 #define GL_BIT_MASK     (GL_BIT_PENDING | GL_BIT_OWNED)
398
399 /*
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.
403  */
404 int
405 acpi_acquire_global_lock(uint32_t *lock)
406 {
407         uint32_t new, old;
408
409         do {
410                 old = *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);
414
415         return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
416 }
417
418 /*
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
421  * releases the lock.
422  */
423 int
424 acpi_release_global_lock(uint32_t *lock)
425 {
426         uint32_t new, old;
427
428         do {
429                 old = *lock;
430                 new = old & ~GL_BIT_MASK;
431         } while (atomic_cmpset_rel_int(lock, old, new) == 0);
432
433         return (old & GL_BIT_PENDING);
434 }
435 #endif /* notyet */