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: releng/5.2/sys/dev/acpica/Osd/OsdSynch.c 120494 2003-09-26 21:22:10Z njl $
28 */
29
30 /*
31 * 6.1 : Mutual Exclusion and Synchronisation
32 */
33
34 #include "acpi.h"
35
36 #include "opt_acpi.h"
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #if __FreeBSD_version >= 500000
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #endif
44
45 #define _COMPONENT ACPI_OS_SERVICES
46 ACPI_MODULE_NAME("SYNCH")
47
48 static MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
49
50 #if __FreeBSD_version < 500000
51 # define AS_LOCK(as) s = splhigh()
52 # define AS_UNLOCK(as) splx(s)
53 # define AS_LOCK_DECL int s
54 # define msleep(a, b, c, d, e) tsleep(a, c, d, e)
55 #else
56 # define AS_LOCK(as) mtx_lock(&(as)->as_mtx)
57 # define AS_UNLOCK(as) mtx_unlock(&(as)->as_mtx)
58 # define AS_LOCK_DECL
59 #endif
60
61 /*
62 * Simple counting semaphore implemented using a mutex. (Subsequently used
63 * in the OSI code to implement a mutex. Go figure.)
64 */
65 struct acpi_semaphore {
66 #if __FreeBSD_version >= 500000
67 struct mtx as_mtx;
68 #endif
69 UINT32 as_units;
70 UINT32 as_maxunits;
71 UINT32 as_pendings;
72 UINT32 as_resetting;
73 UINT32 as_timeouts;
74 };
75
76 #ifndef ACPI_NO_SEMAPHORES
77 #ifndef ACPI_SEMAPHORES_MAX_PENDING
78 #define ACPI_SEMAPHORES_MAX_PENDING 4
79 #endif
80 static int acpi_semaphore_debug = 0;
81 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug);
82 SYSCTL_DECL(_debug_acpi);
83 SYSCTL_INT(_debug_acpi, OID_AUTO, semaphore_debug, CTLFLAG_RW,
84 &acpi_semaphore_debug, 0, "Enable ACPI semaphore debug messages");
85 #endif
86
87 ACPI_STATUS
88 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_HANDLE *OutHandle)
89 {
90 #ifndef ACPI_NO_SEMAPHORES
91 struct acpi_semaphore *as;
92
93 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
94
95 if (OutHandle == NULL)
96 return(AE_BAD_PARAMETER);
97 if (InitialUnits > MaxUnits)
98 return_ACPI_STATUS(AE_BAD_PARAMETER);
99
100 if ((as = malloc(sizeof(*as), M_ACPISEM, M_NOWAIT)) == NULL)
101 return_ACPI_STATUS(AE_NO_MEMORY);
102
103 bzero(as, sizeof(*as));
104 #if __FreeBSD_version >= 500000
105 mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF);
106 #endif
107 as->as_units = InitialUnits;
108 as->as_maxunits = MaxUnits;
109 as->as_pendings = as->as_resetting = as->as_timeouts = 0;
110
111 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
112 "created semaphore %p max %d, initial %d\n",
113 as, InitialUnits, MaxUnits));
114
115 *OutHandle = (ACPI_HANDLE)as;
116 return_ACPI_STATUS(AE_OK);
117 #else
118 *OutHandle = (ACPI_HANDLE)OutHandle;
119 return(AE_OK);
120 #endif
121 }
122
123 ACPI_STATUS
124 AcpiOsDeleteSemaphore (ACPI_HANDLE Handle)
125 {
126 #ifndef ACPI_NO_SEMAPHORES
127 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
128
129 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
130
131 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
132 #if __FreeBSD_version >= 500000
133 mtx_destroy(&as->as_mtx);
134 #endif
135 free(Handle, M_ACPISEM);
136 return_ACPI_STATUS(AE_OK);
137 #else
138 return(AE_OK);
139 #endif
140 }
141
142 /*
143 * This implementation has a bug, in that it has to stall for the entire
144 * timeout before it will return AE_TIME. A better implementation would
145 * use getmicrotime() to correctly adjust the timeout after being woken up.
146 */
147 ACPI_STATUS
148 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
149 {
150 #ifndef ACPI_NO_SEMAPHORES
151 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
152 ACPI_STATUS result;
153 int rv, tmo;
154 struct timeval timeouttv, currenttv, timelefttv;
155 AS_LOCK_DECL;
156
157 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
158
159 if (as == NULL)
160 return_ACPI_STATUS(AE_BAD_PARAMETER);
161
162 if (cold)
163 return_ACPI_STATUS(AE_OK);
164
165 #if 0
166 if (as->as_units < Units && as->as_timeouts > 10) {
167 printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
168 AS_LOCK(as);
169 as->as_units = as->as_maxunits;
170 if (as->as_pendings)
171 as->as_resetting = 1;
172 as->as_timeouts = 0;
173 wakeup(as);
174 AS_UNLOCK(as);
175 return_ACPI_STATUS(AE_TIME);
176 }
177
178 if (as->as_resetting) {
179 return_ACPI_STATUS(AE_TIME);
180 }
181 #endif
182
183 /* a timeout of ACPI_WAIT_FOREVER means "forever" */
184 if (Timeout == ACPI_WAIT_FOREVER) {
185 tmo = 0;
186 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */
187 timeouttv.tv_usec = 0;
188 } else {
189 /* compute timeout using microseconds per tick */
190 tmo = (Timeout * 1000) / (1000000 / hz);
191 if (tmo <= 0)
192 tmo = 1;
193 timeouttv.tv_sec = Timeout / 1000;
194 timeouttv.tv_usec = (Timeout % 1000) * 1000;
195 }
196
197 /* calculate timeout value in timeval */
198 getmicrotime(¤ttv);
199 timevaladd(&timeouttv, ¤ttv);
200
201 AS_LOCK(as);
202 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
203 "get %d units from semaphore %p (has %d), timeout %d\n",
204 Units, as, as->as_units, Timeout));
205 for (;;) {
206 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
207 result = AE_OK;
208 break;
209 }
210 if (as->as_units >= Units) {
211 as->as_units -= Units;
212 result = AE_OK;
213 break;
214 }
215
216 /* limit number of pending treads */
217 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
218 result = AE_TIME;
219 break;
220 }
221
222 /* if timeout values of zero is specified, return immediately */
223 if (Timeout == 0) {
224 result = AE_TIME;
225 break;
226 }
227
228 #if __FreeBSD_version >= 500000
229 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
230 "semaphore blocked, calling msleep(%p, %p, %d, \"acsem\", %d)\n",
231 as, &as->as_mtx, PCATCH, tmo));
232 #endif
233
234 as->as_pendings++;
235
236 if (acpi_semaphore_debug) {
237 printf("%s: Sleep %d, pending %d, semaphore %p, thread %d\n",
238 __func__, Timeout, as->as_pendings, as, AcpiOsGetThreadId());
239 }
240
241 rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo);
242
243 as->as_pendings--;
244
245 #if 0
246 if (as->as_resetting) {
247 /* semaphore reset, return immediately */
248 if (as->as_pendings == 0) {
249 as->as_resetting = 0;
250 }
251 result = AE_TIME;
252 break;
253 }
254 #endif
255
256 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv));
257 if (rv == EWOULDBLOCK) {
258 result = AE_TIME;
259 break;
260 }
261
262 /* check if we already awaited enough */
263 timelefttv = timeouttv;
264 getmicrotime(¤ttv);
265 timevalsub(&timelefttv, ¤ttv);
266 if (timelefttv.tv_sec < 0) {
267 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n", as));
268 result = AE_TIME;
269 break;
270 }
271
272 /* adjust timeout for the next sleep */
273 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) / (1000000 / hz);
274 if (tmo <= 0)
275 tmo = 1;
276
277 if (acpi_semaphore_debug) {
278 printf("%s: Wakeup timeleft(%lu, %lu), tmo %u, semaphore %p, thread %d\n",
279 __func__, timelefttv.tv_sec, timelefttv.tv_usec, tmo, as, AcpiOsGetThreadId());
280 }
281 }
282
283 if (acpi_semaphore_debug) {
284 if (result == AE_TIME && Timeout > 0) {
285 printf("%s: Timeout %d, pending %d, semaphore %p\n",
286 __func__, Timeout, as->as_pendings, as);
287 }
288 if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
289 printf("%s: Acquire %d, units %d, pending %d, semaphore %p, thread %d\n",
290 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
291 }
292 }
293
294 if (result == AE_TIME) {
295 as->as_timeouts++;
296 } else {
297 as->as_timeouts = 0;
298 }
299
300 AS_UNLOCK(as);
301
302 return_ACPI_STATUS(result);
303 #else
304 return(AE_OK);
305 #endif
306 }
307
308 ACPI_STATUS
309 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
310 {
311 #ifndef ACPI_NO_SEMAPHORES
312 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
313 AS_LOCK_DECL;
314
315 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
316
317 if (as == NULL)
318 return_ACPI_STATUS(AE_BAD_PARAMETER);
319
320 AS_LOCK(as);
321 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
322 "return %d units to semaphore %p (has %d)\n",
323 Units, as, as->as_units));
324 if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
325 as->as_units += Units;
326 if (as->as_units > as->as_maxunits)
327 as->as_units = as->as_maxunits;
328 }
329
330 if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
331 printf("%s: Release %d, units %d, pending %d, semaphore %p, thread %d\n",
332 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
333 }
334
335 wakeup(as);
336 AS_UNLOCK(as);
337 return_ACPI_STATUS(AE_OK);
338 #else
339 return(AE_OK);
340 #endif
341 }
342
343 ACPI_STATUS
344 AcpiOsCreateLock (ACPI_HANDLE *OutHandle)
345 {
346 struct mtx *m;
347
348 if (OutHandle == NULL)
349 return (AE_BAD_PARAMETER);
350 MALLOC(m, struct mtx *, sizeof(*m), M_ACPISEM, M_NOWAIT | M_ZERO);
351 if (m == NULL)
352 return (AE_NO_MEMORY);
353
354 mtx_init(m, "acpica subsystem lock", NULL, MTX_DEF);
355 *OutHandle = (ACPI_HANDLE)m;
356 return (AE_OK);
357 }
358
359 void
360 AcpiOsDeleteLock (ACPI_HANDLE Handle)
361 {
362 struct mtx *m = (struct mtx *)Handle;
363
364 if (Handle == NULL)
365 return;
366 mtx_destroy(m);
367 }
368
369 /*
370 * The Flags parameter seems to state whether or not caller is an ISR
371 * (and thus can't block) but since we have ithreads, we don't worry
372 * about potentially blocking.
373 */
374 void
375 AcpiOsAcquireLock (ACPI_HANDLE Handle, UINT32 Flags)
376 {
377 struct mtx *m = (struct mtx *)Handle;
378
379 if (Handle == NULL)
380 return;
381 mtx_lock(m);
382 }
383
384 void
385 AcpiOsReleaseLock (ACPI_HANDLE Handle, UINT32 Flags)
386 {
387 struct mtx *m = (struct mtx *)Handle;
388
389 if (Handle == NULL)
390 return;
391 mtx_unlock(m);
392 }
Cache object: 6bf87e801f637eba0823b0e01c9b18ca
|