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