xref: /freebsd/sys/dev/acpica/Osd/OsdSynch.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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$
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_INT(_debug, OID_AUTO, acpi_semaphore_debug, CTLFLAG_RW,
83     &acpi_semaphore_debug, 0, "");
84 #endif
85 
86 ACPI_STATUS
87 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_HANDLE *OutHandle)
88 {
89 #ifndef ACPI_NO_SEMAPHORES
90     struct acpi_semaphore	*as;
91 
92     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
93 
94     if (OutHandle == NULL)
95 	return(AE_BAD_PARAMETER);
96     if (InitialUnits > MaxUnits)
97 	return_ACPI_STATUS(AE_BAD_PARAMETER);
98 
99     if ((as = malloc(sizeof(*as), M_ACPISEM, M_NOWAIT)) == NULL)
100 	return_ACPI_STATUS(AE_NO_MEMORY);
101 
102     bzero(as, sizeof(*as));
103 #if __FreeBSD_version >= 500000
104     mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF);
105 #endif
106     as->as_units = InitialUnits;
107     as->as_maxunits = MaxUnits;
108     as->as_pendings = as->as_resetting = as->as_timeouts = 0;
109 
110     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
111 	"created semaphore %p max %d, initial %d\n",
112 	as, InitialUnits, MaxUnits));
113 
114     *OutHandle = (ACPI_HANDLE)as;
115     return_ACPI_STATUS(AE_OK);
116 #else
117     *OutHandle = (ACPI_HANDLE)OutHandle;
118     return(AE_OK);
119 #endif
120 }
121 
122 ACPI_STATUS
123 AcpiOsDeleteSemaphore (ACPI_HANDLE Handle)
124 {
125 #ifndef ACPI_NO_SEMAPHORES
126     struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
127 
128     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
129 
130     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
131 #if __FreeBSD_version >= 500000
132     mtx_destroy(&as->as_mtx);
133 #endif
134     free(Handle, M_ACPISEM);
135     return_ACPI_STATUS(AE_OK);
136 #else
137     return(AE_OK);
138 #endif
139 }
140 
141 /*
142  * This implementation has a bug, in that it has to stall for the entire
143  * timeout before it will return AE_TIME.  A better implementation would
144  * use getmicrotime() to correctly adjust the timeout after being woken up.
145  */
146 ACPI_STATUS
147 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
148 {
149 #ifndef ACPI_NO_SEMAPHORES
150     struct acpi_semaphore	*as = (struct acpi_semaphore *)Handle;
151     ACPI_STATUS			result;
152     int				rv, tmo;
153     struct timeval		timeouttv, currenttv, timelefttv;
154     AS_LOCK_DECL;
155 
156     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
157 
158     if (as == NULL)
159 	return_ACPI_STATUS(AE_BAD_PARAMETER);
160 
161     if (cold)
162 	return_ACPI_STATUS(AE_OK);
163 
164 #if 0
165     if (as->as_units < Units && as->as_timeouts > 10) {
166 	printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as);
167 	AS_LOCK(as);
168 	as->as_units = as->as_maxunits;
169 	if (as->as_pendings)
170 	    as->as_resetting = 1;
171 	as->as_timeouts = 0;
172 	wakeup(as);
173 	AS_UNLOCK(as);
174 	return_ACPI_STATUS(AE_TIME);
175     }
176 
177     if (as->as_resetting) {
178 	return_ACPI_STATUS(AE_TIME);
179     }
180 #endif
181 
182     /* a timeout of ACPI_WAIT_FOREVER means "forever" */
183     if (Timeout == ACPI_WAIT_FOREVER) {
184 	tmo = 0;
185 	timeouttv.tv_sec = ((0xffff/1000) + 1);	/* cf. ACPI spec */
186 	timeouttv.tv_usec = 0;
187     } else {
188 	/* compute timeout using microseconds per tick */
189 	tmo = (Timeout * 1000) / (1000000 / hz);
190 	if (tmo <= 0)
191 	    tmo = 1;
192 	timeouttv.tv_sec  = Timeout / 1000;
193 	timeouttv.tv_usec = (Timeout % 1000) * 1000;
194     }
195 
196     /* calculate timeout value in timeval */
197     getmicrotime(&currenttv);
198     timevaladd(&timeouttv, &currenttv);
199 
200     AS_LOCK(as);
201     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
202 	"get %d units from semaphore %p (has %d), timeout %d\n",
203 	Units, as, as->as_units, Timeout));
204     for (;;) {
205 	if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) {
206 	    result = AE_OK;
207 	    break;
208 	}
209 	if (as->as_units >= Units) {
210 	    as->as_units -= Units;
211 	    result = AE_OK;
212 	    break;
213 	}
214 
215 	/* limit number of pending treads */
216 	if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) {
217 	    result = AE_TIME;
218 	    break;
219 	}
220 
221 	/* if timeout values of zero is specified, return immediately */
222 	if (Timeout == 0) {
223 	    result = AE_TIME;
224 	    break;
225 	}
226 
227 #if __FreeBSD_version >= 500000
228 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
229 	    "semaphore blocked, calling msleep(%p, %p, %d, \"acsem\", %d)\n",
230 	    as, &as->as_mtx, PCATCH, tmo));
231 #endif
232 
233 	as->as_pendings++;
234 
235 	if (acpi_semaphore_debug) {
236 	    printf("%s: Sleep %d, pending %d, semaphore %p, thread %d\n",
237 		__func__, Timeout, as->as_pendings, as, AcpiOsGetThreadId());
238 	}
239 
240 	rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo);
241 
242 	as->as_pendings--;
243 
244 #if 0
245 	if (as->as_resetting) {
246 	    /* semaphore reset, return immediately */
247 	    if (as->as_pendings == 0) {
248 		as->as_resetting = 0;
249 	    }
250 	    result = AE_TIME;
251 	    break;
252 	}
253 #endif
254 
255 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv));
256 	if (rv == EWOULDBLOCK) {
257 	    result = AE_TIME;
258 	    break;
259 	}
260 
261 	/* check if we already awaited enough */
262 	timelefttv = timeouttv;
263 	getmicrotime(&currenttv);
264 	timevalsub(&timelefttv, &currenttv);
265 	if (timelefttv.tv_sec < 0) {
266 	    ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n", as));
267 	    result = AE_TIME;
268 	    break;
269 	}
270 
271 	/* adjust timeout for the next sleep */
272 	tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) / (1000000 / hz);
273 	if (tmo <= 0)
274 	    tmo = 1;
275 
276 	if (acpi_semaphore_debug) {
277 	    printf("%s: Wakeup timeleft(%lu, %lu), tmo %u, semaphore %p, thread %d\n",
278 		__func__, timelefttv.tv_sec, timelefttv.tv_usec, tmo, as, AcpiOsGetThreadId());
279 	}
280     }
281 
282     if (acpi_semaphore_debug) {
283 	if (result == AE_TIME && Timeout > 0) {
284 	    printf("%s: Timeout %d, pending %d, semaphore %p\n",
285 		__func__, Timeout, as->as_pendings, as);
286 	}
287 	if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) {
288 	    printf("%s: Acquire %d, units %d, pending %d, semaphore %p, thread %d\n",
289 		__func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
290 	}
291     }
292 
293     if (result == AE_TIME) {
294 	as->as_timeouts++;
295     } else {
296 	as->as_timeouts = 0;
297     }
298 
299     AS_UNLOCK(as);
300 
301     return_ACPI_STATUS(result);
302 #else
303     return(AE_OK);
304 #endif
305 }
306 
307 ACPI_STATUS
308 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
309 {
310 #ifndef ACPI_NO_SEMAPHORES
311     struct acpi_semaphore	*as = (struct acpi_semaphore *)Handle;
312     AS_LOCK_DECL;
313 
314     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
315 
316     if (as == NULL)
317 	return_ACPI_STATUS(AE_BAD_PARAMETER);
318 
319     AS_LOCK(as);
320     ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
321 	"return %d units to semaphore %p (has %d)\n",
322 	Units, as, as->as_units));
323     if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) {
324 	as->as_units += Units;
325 	if (as->as_units > as->as_maxunits)
326 	    as->as_units = as->as_maxunits;
327     }
328 
329     if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) {
330 	printf("%s: Release %d, units %d, pending %d, semaphore %p, thread %d\n",
331 	    __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId());
332     }
333 
334     wakeup(as);
335     AS_UNLOCK(as);
336     return_ACPI_STATUS(AE_OK);
337 #else
338     return(AE_OK);
339 #endif
340 }
341 
342 ACPI_STATUS
343 AcpiOsCreateLock (ACPI_HANDLE *OutHandle)
344 {
345     struct mtx *m;
346 
347     if (OutHandle == NULL)
348 	return (AE_BAD_PARAMETER);
349     MALLOC(m, struct mtx *, sizeof(*m), M_ACPISEM, M_NOWAIT | M_ZERO);
350     if (m == NULL)
351 	return (AE_NO_MEMORY);
352 
353     mtx_init(m, "acpica subsystem lock", NULL, MTX_DEF);
354     *OutHandle = (ACPI_HANDLE)m;
355     return (AE_OK);
356 }
357 
358 void
359 AcpiOsDeleteLock (ACPI_HANDLE Handle)
360 {
361     struct mtx *m = (struct mtx *)Handle;
362 
363     if (Handle == NULL)
364         return;
365     mtx_destroy(m);
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 mtx *m = (struct mtx *)Handle;
377 
378     if (Handle == NULL)
379         return;
380     mtx_lock(m);
381 }
382 
383 void
384 AcpiOsReleaseLock (ACPI_HANDLE Handle, UINT32 Flags)
385 {
386     struct mtx *m = (struct mtx *)Handle;
387 
388     if (Handle == NULL)
389         return;
390     mtx_unlock(m);
391 }
392