OsdSynch.c revision 72200
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: head/sys/dev/acpica/Osd/OsdSynch.c 72200 2001-02-09 06:11:45Z bmilekic $
28 */
29
30/*
31 * 6.1 : Mutual Exclusion and Synchronisation
32 */
33
34#include "acpi.h"
35
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/mutex.h>
39
40#define _COMPONENT	OS_DEPENDENT
41MODULE_NAME("SYNCH")
42
43static MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore");
44
45/* disable semaphores - AML in the field doesn't use them correctly */
46#define ACPI_NO_SEMAPHORES
47
48/*
49 * Simple counting semaphore implemented using a mutex. (Subsequently used
50 * in the OSI code to implement a mutex.  Go figure.)
51 */
52struct acpi_semaphore {
53    struct mtx	as_mtx;
54    UINT32	as_units;
55    UINT32	as_maxunits;
56    char	*as_name;
57};
58
59ACPI_STATUS
60AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_HANDLE *OutHandle)
61{
62#ifndef ACPI_NO_SEMAPHORES
63    struct acpi_semaphore	*as;
64
65    FUNCTION_TRACE(__FUNCTION__);
66
67    if (OutHandle == NULL)
68	return(AE_BAD_PARAMETER);
69    if (InitialUnits > MaxUnits)
70	return_ACPI_STATUS(AE_BAD_PARAMETER);
71
72    if ((as = malloc(sizeof(*as), M_ACPISEM, M_NOWAIT)) == NULL)
73	return_ACPI_STATUS(AE_NO_MEMORY);
74
75    mtx_init(&as->as_mtx, "ACPI semaphore", MTX_DEF);
76    as->as_units = InitialUnits;
77    as->as_maxunits = MaxUnits;
78    as->as_name = malloc(strlen(name) + 1, M_ACPISEM, M_NOWAIT);
79    strcpy(as->as_name, name);
80
81    DEBUG_PRINT(TRACE_MUTEX, ("created semaphore %p max %d, initial %d\n",
82			      as, InitialUnits, MaxUnits));
83
84    *OutHandle = (ACPI_HANDLE)as;
85    return_ACPI_STATUS(AE_OK);
86#else
87    *OutHandle = (ACPI_HANDLE)OutHandle;
88    return(AE_OK);
89#endif
90}
91
92ACPI_STATUS
93AcpiOsDeleteSemaphore (ACPI_HANDLE Handle)
94{
95#ifndef ACPI_NO_SEMAPHORES
96    struct acpi_semaphore *as = (struct acpi_semaphore *)Handle;
97
98    FUNCTION_TRACE(__FUNCTION__);
99
100#ifdef ACPI_TRACK_SEMAPHORE
101    printf("destroyed semaphore '%s' @ %p\n", as->as_name, as);
102#else
103    DEBUG_PRINT(TRACE_MUTEX, ("destroyed semaphore %p\n", as));
104#endif
105    mtx_destroy(&as->as_mtx);
106    free(as->as_name, M_ACPISEM);
107    free(Handle, M_ACPISEM);
108    return_ACPI_STATUS(AE_OK);
109#else
110    return(AE_OK);
111#endif
112}
113
114/*
115 * This implementation has a bug, in that it has to stall for the entire
116 * timeout before it will return AE_TIME.  A better implementation would
117 * use getmicrotime() to correctly adjust the timeout after being woken up.
118 */
119ACPI_STATUS
120AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT32 Timeout)
121{
122#ifndef ACPI_NO_SEMAPHORES
123    struct acpi_semaphore	*as = (struct acpi_semaphore *)Handle;
124    ACPI_STATUS			result;
125    int				rv, tmo;
126
127    FUNCTION_TRACE(__FUNCTION__);
128
129    if (as == NULL)
130	return_ACPI_STATUS(AE_BAD_PARAMETER);
131
132    /* a timeout of -1 means "forever" */
133    if (Timeout == -1) {
134	tmo = 0;
135    } else {
136	/* compute timeout using microseconds per tick */
137	tmo = (Timeout * 1000) / (1000000 / hz)
138	if (tmo <= 0)
139	    tmo = 1;
140    }
141
142    mtx_lock(&as->as_mtx);
143    DEBUG_PRINT(TRACE_MUTEX, ("get %d units from semaphore %p (has %d), timeout %d\n",
144			      Units, as, as->as_units, Timeout));
145    for (;;) {
146	if (as->as_units >= Units) {
147	    as->as_units -= Units;
148	    result = AE_OK;
149	    break;
150	}
151	if (Timeout < 0) {
152	    result = AE_TIME;
153	    break;
154	}
155	DEBUG_PRINT(TRACE_MUTEX, ("semaphore blocked, calling msleep(%p, %p, %d, \"acpisem\", %d)\n",
156				  as, as->as_mtx, 0, tmo));
157	for (;;) ;
158
159	rv = msleep(as, &as->as_mtx, 0, "acpisem", tmo);
160	DEBUG_PRINT(TRACE_MUTEX, ("msleep returned %d\n", rv));
161	if (rv == EWOULDBLOCK) {
162	    result = AE_TIME;
163	    break;
164	}
165    }
166    mtx_unlock(&as->as_mtx);
167
168    return_ACPI_STATUS(result);
169#else
170    return(AE_OK);
171#endif
172}
173
174ACPI_STATUS
175AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
176{
177#ifndef ACPI_NO_SEMAPHORES
178    struct acpi_semaphore	*as = (struct acpi_semaphore *)Handle;
179
180    FUNCTION_TRACE(__FUNCTION__);
181
182    if (as == NULL)
183	return_ACPI_STATUS(AE_BAD_PARAMETER);
184
185    mtx_lock(&as->as_mtx);
186    DEBUG_PRINT(TRACE_MUTEX, ("return %d units to semaphore %p (has %d)\n",
187			      Units, as, as->as_units));
188    as->as_units += Units;
189    if (as->as_units > as->as_maxunits)
190	as->as_units = as->as_maxunits;
191    wakeup(as);
192    mtx_unlock(&as->as_mtx);
193    return_ACPI_STATUS(AE_OK);
194#else
195    return(AE_OK);
196#endif
197}
198