Home | History | Annotate | Line # | Download | only in acpica
OsdSynch.c revision 1.6
      1 /*	$NetBSD: OsdSynch.c,v 1.6 2007/02/19 12:29:24 xtraeme Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*-
     39  * Copyright (c) 2000 Michael Smith
     40  * Copyright (c) 2000 BSDi
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 /*
     66  * OS Services Layer
     67  *
     68  * 6.4: Mutual Exclusion and Synchronization
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: OsdSynch.c,v 1.6 2007/02/19 12:29:24 xtraeme Exp $");
     73 
     74 #include <sys/param.h>
     75 #include <sys/malloc.h>
     76 #include <sys/mutex.h>
     77 #include <sys/condvar.h>
     78 #include <sys/kernel.h>
     79 #include <sys/proc.h>
     80 
     81 #include <dev/acpi/acpica.h>
     82 
     83 MALLOC_DECLARE(M_ACPI);
     84 
     85 #define	_COMPONENT	ACPI_OS_SERVICES
     86 ACPI_MODULE_NAME("SYNCH")
     87 
     88 /*
     89  * Simple counting semaphore implemented using a mutex.  This is
     90  * subsequently used in the OSI code to implement a mutex.  Go figure.
     91  */
     92 struct acpi_semaphore {
     93 	kcondvar_t as_cv;
     94 	kmutex_t as_slock;
     95 	UINT32 as_units;
     96 	UINT32 as_maxunits;
     97 };
     98 
     99 struct acpi_lock {
    100 	kmutex_t al_slock;
    101 };
    102 
    103 /*
    104  * AcpiOsCreateSemaphore:
    105  *
    106  *	Create a semaphore.
    107  */
    108 ACPI_STATUS
    109 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits,
    110     ACPI_HANDLE *OutHandle)
    111 {
    112 	struct acpi_semaphore *as;
    113 
    114 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    115 
    116 	if (OutHandle == NULL)
    117 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    118 	if (InitialUnits > MaxUnits)
    119 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    120 
    121 	as = malloc(sizeof(*as), M_ACPI, M_NOWAIT);
    122 	if (as == NULL)
    123 		return_ACPI_STATUS(AE_NO_MEMORY);
    124 
    125 	mutex_init(&as->as_slock, MUTEX_DRIVER, IPL_NONE);
    126 	cv_init(&as->as_cv, "acpisem");
    127 	as->as_units = InitialUnits;
    128 	as->as_maxunits = MaxUnits;
    129 
    130 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
    131 	    "created semaphore %p max %u initial %u\n",
    132 	    as, as->as_maxunits, as->as_units));
    133 
    134 	*OutHandle = (ACPI_HANDLE) as;
    135 	return_ACPI_STATUS(AE_OK);
    136 }
    137 
    138 /*
    139  * AcpiOsDeleteSemaphore:
    140  *
    141  *	Delete a semaphore.
    142  */
    143 ACPI_STATUS
    144 AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)
    145 {
    146 	struct acpi_semaphore *as = (void *) Handle;
    147 
    148 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    149 
    150 	if (as == NULL)
    151 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    152 
    153 	mutex_destroy(&as->as_slock);
    154 	free(as, M_ACPI);
    155 
    156 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as));
    157 
    158 	return_ACPI_STATUS(AE_OK);
    159 }
    160 
    161 /*
    162  * AcpiOsWaitSemaphore:
    163  *
    164  *	Wait for units from a semaphore.
    165  */
    166 ACPI_STATUS
    167 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout)
    168 {
    169 	struct acpi_semaphore *as = (void *) Handle;
    170 	ACPI_STATUS rv;
    171 	int timo, error;
    172 
    173 	/*
    174 	 * This implementation has a bug: It has to stall for the entire
    175 	 * timeout before it will return AE_TIME.  A better implementation
    176 	 * would adjust the amount of time left after being awakened.
    177 	 */
    178 
    179 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    180 
    181 	if (as == NULL)
    182 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    183 
    184 	/* A timeout of 0xFFFF means "forever". */
    185 	if (Timeout == 0xFFFF)
    186 		timo = 0;
    187 	else {
    188 		/* Compute the timeout using uSec per tick. */
    189 		timo = (Timeout * 1000) / (1000000 / hz);
    190 		if (timo <= 0)
    191 			timo = 1;
    192 	}
    193 
    194 	mutex_enter(&as->as_slock);
    195 
    196 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
    197 	    "get %d units from semaphore %p (has %d) timeout %d\n",
    198 	    Units, as, as->as_units, Timeout));
    199 
    200 	for (;;) {
    201 		if (as->as_units >= Units) {
    202 			as->as_units -= Units;
    203 			rv = AE_OK;
    204 			break;
    205 		}
    206 
    207 		ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
    208 		    "semaphore blocked, sleeping %d ticks\n", timo));
    209 
    210 		error = cv_timedwait(&as->as_cv, &as->as_slock, timo);
    211 		if (error == EWOULDBLOCK) {
    212 			rv = AE_TIME;
    213 			break;
    214 		}
    215 	}
    216 
    217 	mutex_exit(&as->as_slock);
    218 
    219 	return_ACPI_STATUS(rv);
    220 }
    221 
    222 /*
    223  * AcpiOsSignalSemaphore:
    224  *
    225  *	Send units to a semaphore.
    226  */
    227 ACPI_STATUS
    228 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units)
    229 {
    230 	struct acpi_semaphore *as = (void *) Handle;
    231 
    232 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    233 
    234 	if (as == NULL)
    235 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    236 
    237 	mutex_enter(&as->as_slock);
    238 
    239 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
    240 	    "return %d units to semaphore %p (has %d)\n",
    241 	    Units, as, as->as_units));
    242 
    243 	as->as_units += Units;
    244 	if (as->as_units > as->as_maxunits)
    245 		as->as_units = as->as_maxunits;
    246 	cv_broadcast(&as->as_cv);
    247 
    248 	mutex_exit(&as->as_slock);
    249 
    250 	return_ACPI_STATUS(AE_OK);
    251 }
    252 
    253 /*
    254  * AcpiOsCreateLock:
    255  *
    256  *	Create a lock.
    257  */
    258 ACPI_STATUS
    259 AcpiOsCreateLock(ACPI_HANDLE *OutHandle)
    260 {
    261 	struct acpi_lock *al;
    262 
    263 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    264 
    265 	if (OutHandle == NULL)
    266 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    267 
    268 	al = malloc(sizeof(*al), M_ACPI, M_NOWAIT);
    269 	if (al == NULL)
    270 		return_ACPI_STATUS(AE_NO_MEMORY);
    271 
    272 	mutex_init(&al->al_slock, MUTEX_DRIVER, IPL_NONE);
    273 
    274 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
    275 	    "created lock %p\n", al));
    276 
    277 	*OutHandle = (ACPI_HANDLE) al;
    278 	return_ACPI_STATUS(AE_OK);
    279 }
    280 
    281 /*
    282  * AcpiOsDeleteLock:
    283  *
    284  *	Delete a lock.
    285  */
    286 void
    287 AcpiOsDeleteLock(ACPI_HANDLE Handle)
    288 {
    289 	struct acpi_lock *al = (void *) Handle;
    290 
    291 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    292 
    293 	if (al == NULL)
    294 		return;
    295 
    296 	mutex_destroy(&al->al_slock);
    297 	free(al, M_ACPI);
    298 
    299 	ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed lock %p\n", al));
    300 
    301 	return;
    302 }
    303 
    304 /*
    305  * AcpiOsAcquireLock:
    306  *
    307  *	Acquire a lock.
    308  */
    309 ACPI_NATIVE_UINT
    310 AcpiOsAcquireLock(ACPI_HANDLE Handle)
    311 {
    312 	struct acpi_lock *al = (void *) Handle;
    313 
    314 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    315 
    316 	if (al == NULL)
    317 		return 0;
    318 
    319 	mutex_enter(&al->al_slock);
    320 
    321 	return 0;
    322 }
    323 
    324 /*
    325  * AcpiOsReleaseLock:
    326  *
    327  *	Release a lock.
    328  */
    329 void
    330 AcpiOsReleaseLock(ACPI_HANDLE Handle, ACPI_NATIVE_UINT Flags)
    331 {
    332 	struct acpi_lock *al = (void *) Handle;
    333 
    334 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    335 
    336 	if (al == NULL)
    337 		return;
    338 
    339 	mutex_exit(&al->al_slock);
    340 
    341 	return;
    342 }
    343