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