Bring in YONETANI Tomokazu's acpi-update-2.patch (27-May-2004), a major
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdSynch.c
CommitLineData
5ed44076
MD
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 *
f9d8cd12
MD
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 $
5ed44076
MD
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/thread.h>
42#include <sys/thread2.h>
43
44#define _COMPONENT ACPI_OS_SERVICES
45ACPI_MODULE_NAME("SYNCH")
46
f9d8cd12 47MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
5ed44076
MD
48
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)
59#else
60# define AS_LOCK(as) mtx_lock(&(as)->as_mtx)
61# define AS_UNLOCK(as) mtx_unlock(&(as)->as_mtx)
62# define AS_LOCK_DECL
63#endif
64
65/*
f9d8cd12 66 * Simple counting semaphore implemented using a mutex. (Subsequently used
5ed44076
MD
67 * in the OSI code to implement a mutex. Go figure.)
68 */
69struct acpi_semaphore {
70#if __FreeBSD_version >= 500000
71 struct mtx as_mtx;
72#endif
73 UINT32 as_units;
74 UINT32 as_maxunits;
75 UINT32 as_pendings;
76 UINT32 as_resetting;
77 UINT32 as_timeouts;
78};
79
80#ifndef ACPI_NO_SEMAPHORES
81#ifndef ACPI_SEMAPHORES_MAX_PENDING
82#define ACPI_SEMAPHORES_MAX_PENDING 4
83#endif
84static int acpi_semaphore_debug = 0;
85TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
86SYSCTL_DECL(_debug_acpi);
87SYSCTL_INT(_debug_acpi, OID_AUTO, semaphore_debug, CTLFLAG_RW,
88 &acpi_semaphore_debug, 0, "Enable ACPI semaphore debug messages");
f9d8cd12 89#endif /* !ACPI_NO_SEMAPHORES */
5ed44076
MD
90
91ACPI_STATUS
f9d8cd12
MD
92AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
93 ACPI_HANDLE *OutHandle)
5ed44076
MD
94{
95#ifndef ACPI_NO_SEMAPHORES
96 struct acpi_semaphore *as;
97
98 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
99
100 if (OutHandle == NULL)
f9d8cd12 101 return_ACPI_STATUS (AE_BAD_PARAMETER);
5ed44076 102 if (InitialUnits > MaxUnits)
f9d8cd12 103 return_ACPI_STATUS (AE_BAD_PARAMETER);
5ed44076 104
9d905e1a 105 as = malloc(sizeof(*as), M_ACPISEM, M_INTWAIT | M_ZERO);
f9d8cd12 106
5ed44076
MD
107#if __FreeBSD_version >= 500000
108 mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF);
109#endif
110 as->as_units = InitialUnits;
111 as->as_maxunits = MaxUnits;
112 as->as_pendings = as->as_resetting = as->as_timeouts = 0;
113
114 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
115 "created semaphore %p max %d, initial %d\n",
116 as, InitialUnits, MaxUnits));
117
118 *OutHandle = (ACPI_HANDLE)as;
5ed44076
MD
119#else
120 *OutHandle = (ACPI_HANDLE)OutHandle;
f9d8cd12
MD
121#endif /* !ACPI_NO_SEMAPHORES */
122
123 return_ACPI_STATUS (AE_OK);
5ed44076
MD
124}
125
126ACPI_STATUS
f9d8cd12 127AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
5ed44076
MD
128{
129#ifndef ACPI_NO_SEMAPHORES
5ed44076 130 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
5ed44076
MD
131
132 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
133
134 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
135#if __FreeBSD_version >= 500000
136 mtx_destroy(&as->as_mtx);
137#endif
138 free(Handle, M_ACPISEM);
f9d8cd12
MD
139#endif /* !ACPI_NO_SEMAPHORES */
140
141 return_ACPI_STATUS (AE_OK);
5ed44076
MD
142}
143
144/*
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.
148 */
149ACPI_STATUS
150AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
151{
152#ifndef ACPI_NO_SEMAPHORES
5ed44076 153 ACPI_STATUS result;
f9d8cd12 154 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
5ed44076
MD
155 int rv, tmo;
156 struct timeval timeouttv, currenttv, timelefttv;
157 AS_LOCK_DECL;
158
159 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
160
161 if (as == NULL)
f9d8cd12 162 return_ACPI_STATUS (AE_BAD_PARAMETER);
5ed44076
MD
163
164 if (cold)
f9d8cd12 165 return_ACPI_STATUS (AE_OK);
5ed44076
MD
166
167#if 0
168 if (as->as_units < Units && as->as_timeouts > 10) {
169 printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
170 AS_LOCK(as);
171 as->as_units = as->as_maxunits;
172 if (as->as_pendings)
173 as->as_resetting = 1;
174 as->as_timeouts = 0;
175 wakeup(as);
176 AS_UNLOCK(as);
f9d8cd12 177 return_ACPI_STATUS (AE_TIME);
5ed44076
MD
178 }
179
f9d8cd12
MD
180 if (as->as_resetting)
181 return_ACPI_STATUS (AE_TIME);
5ed44076
MD
182#endif
183
184 /* a timeout of ACPI_WAIT_FOREVER means "forever" */
185 if (Timeout == ACPI_WAIT_FOREVER) {
186 tmo = 0;
187 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
188 timeouttv.tv_usec = 0;
189 } else {
190 /* compute timeout using microseconds per tick */
191 tmo = (Timeout * 1000) / (1000000 / hz);
192 if (tmo <= 0)
193 tmo = 1;
194 timeouttv.tv_sec = Timeout / 1000;
195 timeouttv.tv_usec = (Timeout % 1000) * 1000;
196 }
197
198 /* calculate timeout value in timeval */
199 getmicrotime(&currenttv);
200 timevaladd(&timeouttv, &currenttv);
201
202 AS_LOCK(as);
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));
206 for (;;) {
207 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
208 result = AE_OK;
209 break;
210 }
211 if (as->as_units >= Units) {
212 as->as_units -= Units;
213 result = AE_OK;
214 break;
215 }
216
217 /* limit number of pending treads */
218 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
219 result = AE_TIME;
220 break;
221 }
222
223 /* if timeout values of zero is specified, return immediately */
224 if (Timeout == 0) {
225 result = AE_TIME;
226 break;
227 }
228
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));
233#endif
234
235 as->as_pendings++;
236
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());
240 }
241
242 rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo);
243
244 as->as_pendings--;
245
246#if 0
247 if (as->as_resetting) {
248 /* semaphore reset, return immediately */
249 if (as->as_pendings == 0) {
250 as->as_resetting = 0;
251 }
252 result = AE_TIME;
253 break;
254 }
255#endif
256
257 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv));
258 if (rv == EWOULDBLOCK) {
259 result = AE_TIME;
260 break;
261 }
262
263 /* check if we already awaited enough */
264 timelefttv = timeouttv;
265 getmicrotime(&currenttv);
266 timevalsub(&timelefttv, &currenttv);
267 if (timelefttv.tv_sec < 0) {
f9d8cd12
MD
268 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n",
269 as));
5ed44076
MD
270 result = AE_TIME;
271 break;
272 }
273
274 /* adjust timeout for the next sleep */
f9d8cd12
MD
275 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) /
276 (1000000 / hz);
5ed44076
MD
277 if (tmo <= 0)
278 tmo = 1;
279
280 if (acpi_semaphore_debug) {
f9d8cd12
MD
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());
5ed44076
MD
284 }
285 }
286
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);
291 }
292 if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
f9d8cd12
MD
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());
5ed44076
MD
296 }
297 }
298
f9d8cd12 299 if (result == AE_TIME)
5ed44076 300 as->as_timeouts++;
f9d8cd12 301 else
5ed44076 302 as->as_timeouts = 0;
5ed44076
MD
303
304 AS_UNLOCK(as);
f9d8cd12 305 return_ACPI_STATUS (result);
5ed44076 306#else
f9d8cd12
MD
307 return_ACPI_STATUS (AE_OK);
308#endif /* !ACPI_NO_SEMAPHORES */
5ed44076
MD
309}
310
311ACPI_STATUS
312AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
313{
314#ifndef ACPI_NO_SEMAPHORES
315 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
316 AS_LOCK_DECL;
317
318 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
319
320 if (as == NULL)
321 return_ACPI_STATUS(AE_BAD_PARAMETER);
322
323 AS_LOCK(as);
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;
331 }
332
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());
336 }
337
338 wakeup(as);
339 AS_UNLOCK(as);
f9d8cd12
MD
340#endif /* !ACPI_NO_SEMAPHORES */
341
342 return_ACPI_STATUS (AE_OK);
5ed44076
MD
343}
344
345ACPI_STATUS
f9d8cd12 346AcpiOsCreateLock(ACPI_HANDLE *OutHandle)
5ed44076 347{
41a01a4d 348 lwkt_rwlock_t lock;
5ed44076
MD
349
350 if (OutHandle == NULL)
351 return (AE_BAD_PARAMETER);
9d905e1a 352 MALLOC(lock, lwkt_rwlock_t, sizeof(*lock), M_ACPISEM, M_INTWAIT | M_ZERO);
41a01a4d 353 if (lock == NULL)
5ed44076
MD
354 return (AE_NO_MEMORY);
355
41a01a4d
MD
356 lwkt_rwlock_init(lock);
357 *OutHandle = (ACPI_HANDLE)lock;
5ed44076
MD
358 return (AE_OK);
359}
360
361void
362AcpiOsDeleteLock (ACPI_HANDLE Handle)
363{
41a01a4d 364 lwkt_rwlock_t lock = (lwkt_rwlock_t)Handle;
5ed44076
MD
365
366 if (Handle == NULL)
367 return;
41a01a4d 368 lwkt_rwlock_uninit(lock);
5ed44076
MD
369}
370
371/*
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.
375 */
376void
377AcpiOsAcquireLock (ACPI_HANDLE Handle, UINT32 Flags)
378{
41a01a4d 379 lwkt_rwlock_t lock = (lwkt_rwlock_t)Handle;
5ed44076
MD
380
381 if (Handle == NULL)
382 return;
41a01a4d 383 lwkt_exlock(lock, "acpi1");
5ed44076
MD
384}
385
386void
387AcpiOsReleaseLock (ACPI_HANDLE Handle, UINT32 Flags)
388{
41a01a4d 389 lwkt_rwlock_t lock = (lwkt_rwlock_t)Handle;
5ed44076
MD
390
391 if (Handle == NULL)
392 return;
41a01a4d 393 lwkt_exunlock(lock);
5ed44076 394}
f9d8cd12
MD
395
396#ifdef notyet
397/* Section 5.2.9.1: global lock acquire/release functions */
398#define GL_ACQUIRED (-1)
399#define GL_BUSY 0
400#define GL_BIT_PENDING 0x1
401#define GL_BIT_OWNED 0x2
402#define GL_BIT_MASK (GL_BIT_PENDING | GL_BIT_OWNED)
403
404/*
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.
408 */
409int
410acpi_acquire_global_lock(uint32_t *lock)
411{
412 uint32_t new, old;
413
414 do {
415 old = *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);
419
420 return ((new < GL_BIT_MASK) ? GL_ACQUIRED : GL_BUSY);
421}
422
423/*
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
426 * releases the lock.
427 */
428int
429acpi_release_global_lock(uint32_t *lock)
430{
431 uint32_t new, old;
432
433 do {
434 old = *lock;
435 new = old & ~GL_BIT_MASK;
436 } while (atomic_cmpset_rel_int(lock, old, new) == 0);
437
438 return (old & GL_BIT_PENDING);
439}
440#endif /* notyet */