OsdSynch.c revision 74914
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 74914 2001-03-28 09:17:56Z jhb $ 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 OS_DEPENDENT 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 char *as_name; 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(__FUNCTION__); 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 as->as_name = malloc(strlen(name) + 1, M_ACPISEM, M_NOWAIT); 80 strcpy(as->as_name, name); 81 82 DEBUG_PRINT(TRACE_MUTEX, ("created semaphore %p max %d, initial %d\n", 83 as, InitialUnits, MaxUnits)); 84 85 *OutHandle = (ACPI_HANDLE)as; 86 return_ACPI_STATUS(AE_OK); 87#else 88 *OutHandle = (ACPI_HANDLE)OutHandle; 89 return(AE_OK); 90#endif 91} 92 93ACPI_STATUS 94AcpiOsDeleteSemaphore (ACPI_HANDLE Handle) 95{ 96#ifndef ACPI_NO_SEMAPHORES 97 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 98 99 FUNCTION_TRACE(__FUNCTION__); 100 101#ifdef ACPI_TRACK_SEMAPHORE 102 printf("destroyed semaphore '%s' @ %p\n", as->as_name, as); 103#else 104 DEBUG_PRINT(TRACE_MUTEX, ("destroyed semaphore %p\n", as)); 105#endif 106 mtx_destroy(&as->as_mtx); 107 free(as->as_name, M_ACPISEM); 108 free(Handle, M_ACPISEM); 109 return_ACPI_STATUS(AE_OK); 110#else 111 return(AE_OK); 112#endif 113} 114 115/* 116 * This implementation has a bug, in that it has to stall for the entire 117 * timeout before it will return AE_TIME. A better implementation would 118 * use getmicrotime() to correctly adjust the timeout after being woken up. 119 */ 120ACPI_STATUS 121AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT32 Timeout) 122{ 123#ifndef ACPI_NO_SEMAPHORES 124 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 125 ACPI_STATUS result; 126 int rv, tmo; 127 128 FUNCTION_TRACE(__FUNCTION__); 129 130 if (as == NULL) 131 return_ACPI_STATUS(AE_BAD_PARAMETER); 132 133 /* a timeout of -1 means "forever" */ 134 if (Timeout == -1) { 135 tmo = 0; 136 } else { 137 /* compute timeout using microseconds per tick */ 138 tmo = (Timeout * 1000) / (1000000 / hz) 139 if (tmo <= 0) 140 tmo = 1; 141 } 142 143 mtx_lock(&as->as_mtx); 144 DEBUG_PRINT(TRACE_MUTEX, ("get %d units from semaphore %p (has %d), timeout %d\n", 145 Units, as, as->as_units, Timeout)); 146 for (;;) { 147 if (as->as_units >= Units) { 148 as->as_units -= Units; 149 result = AE_OK; 150 break; 151 } 152 if (Timeout < 0) { 153 result = AE_TIME; 154 break; 155 } 156 DEBUG_PRINT(TRACE_MUTEX, ("semaphore blocked, calling msleep(%p, %p, %d, \"acpisem\", %d)\n", 157 as, as->as_mtx, 0, tmo)); 158 for (;;) ; 159 160 rv = msleep(as, &as->as_mtx, 0, "acpisem", tmo); 161 DEBUG_PRINT(TRACE_MUTEX, ("msleep returned %d\n", rv)); 162 if (rv == EWOULDBLOCK) { 163 result = AE_TIME; 164 break; 165 } 166 } 167 mtx_unlock(&as->as_mtx); 168 169 return_ACPI_STATUS(result); 170#else 171 return(AE_OK); 172#endif 173} 174 175ACPI_STATUS 176AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units) 177{ 178#ifndef ACPI_NO_SEMAPHORES 179 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 180 181 FUNCTION_TRACE(__FUNCTION__); 182 183 if (as == NULL) 184 return_ACPI_STATUS(AE_BAD_PARAMETER); 185 186 mtx_lock(&as->as_mtx); 187 DEBUG_PRINT(TRACE_MUTEX, ("return %d units to semaphore %p (has %d)\n", 188 Units, as, as->as_units)); 189 as->as_units += Units; 190 if (as->as_units > as->as_maxunits) 191 as->as_units = as->as_maxunits; 192 wakeup(as); 193 mtx_unlock(&as->as_mtx); 194 return_ACPI_STATUS(AE_OK); 195#else 196 return(AE_OK); 197#endif 198} 199