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: src/sys/dev/acpica/Osd/OsdSynch.c,v 1.17.2.1 2003/08/22 20:49:21 jhb Exp $ 28 * $DragonFly: src/sys/dev/acpica/Osd/Attic/OsdSynch.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $ 29 */ 30 31 /* 32 * 6.1 : Mutual Exclusion and Synchronisation 33 */ 34 35 #include "acpi.h" 36 37 #include "opt_acpi.h" 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/sysctl.h> 41 #if __FreeBSD_version >= 500000 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #endif 45 46 #define _COMPONENT ACPI_OS_SERVICES 47 ACPI_MODULE_NAME("SYNCH") 48 49 static MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore"); 50 51 #if __FreeBSD_version < 500000 52 # define AS_LOCK(as) s = splhigh() 53 # define AS_UNLOCK(as) splx(s) 54 # define AS_LOCK_DECL int s 55 # define msleep(a, b, c, d, e) tsleep(a, c, d, e) 56 #else 57 # define AS_LOCK(as) mtx_lock(&(as)->as_mtx) 58 # define AS_UNLOCK(as) mtx_unlock(&(as)->as_mtx) 59 # define AS_LOCK_DECL 60 #endif 61 62 /* 63 * Simple counting semaphore implemented using a mutex. (Subsequently used 64 * in the OSI code to implement a mutex. Go figure.) 65 */ 66 struct acpi_semaphore { 67 #if __FreeBSD_version >= 500000 68 struct mtx as_mtx; 69 #endif 70 UINT32 as_units; 71 UINT32 as_maxunits; 72 UINT32 as_pendings; 73 UINT32 as_resetting; 74 UINT32 as_timeouts; 75 }; 76 77 #ifndef ACPI_NO_SEMAPHORES 78 #ifndef ACPI_SEMAPHORES_MAX_PENDING 79 #define ACPI_SEMAPHORES_MAX_PENDING 4 80 #endif 81 static int acpi_semaphore_debug = 0; 82 TUNABLE_INT("debug.acpi_semaphore_debug", &acpi_semaphore_debug); 83 SYSCTL_INT(_debug, OID_AUTO, acpi_semaphore_debug, CTLFLAG_RW, 84 &acpi_semaphore_debug, 0, ""); 85 #endif 86 87 ACPI_STATUS 88 AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_HANDLE *OutHandle) 89 { 90 #ifndef ACPI_NO_SEMAPHORES 91 struct acpi_semaphore *as; 92 93 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 94 95 if (OutHandle == NULL) 96 return(AE_BAD_PARAMETER); 97 if (InitialUnits > MaxUnits) 98 return_ACPI_STATUS(AE_BAD_PARAMETER); 99 100 if ((as = malloc(sizeof(*as), M_ACPISEM, M_NOWAIT)) == NULL) 101 return_ACPI_STATUS(AE_NO_MEMORY); 102 103 bzero(as, sizeof(*as)); 104 #if __FreeBSD_version >= 500000 105 mtx_init(&as->as_mtx, "ACPI semaphore", NULL, MTX_DEF); 106 #endif 107 as->as_units = InitialUnits; 108 as->as_maxunits = MaxUnits; 109 as->as_pendings = as->as_resetting = as->as_timeouts = 0; 110 111 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, 112 "created semaphore %p max %d, initial %d\n", 113 as, InitialUnits, MaxUnits)); 114 115 *OutHandle = (ACPI_HANDLE)as; 116 return_ACPI_STATUS(AE_OK); 117 #else 118 *OutHandle = (ACPI_HANDLE)OutHandle; 119 return(AE_OK); 120 #endif 121 } 122 123 ACPI_STATUS 124 AcpiOsDeleteSemaphore (ACPI_HANDLE Handle) 125 { 126 #ifndef ACPI_NO_SEMAPHORES 127 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 128 129 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 130 131 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "destroyed semaphore %p\n", as)); 132 #if __FreeBSD_version >= 500000 133 mtx_destroy(&as->as_mtx); 134 #endif 135 free(Handle, M_ACPISEM); 136 return_ACPI_STATUS(AE_OK); 137 #else 138 return(AE_OK); 139 #endif 140 } 141 142 /* 143 * This implementation has a bug, in that it has to stall for the entire 144 * timeout before it will return AE_TIME. A better implementation would 145 * use getmicrotime() to correctly adjust the timeout after being woken up. 146 */ 147 ACPI_STATUS 148 AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT16 Timeout) 149 { 150 #ifndef ACPI_NO_SEMAPHORES 151 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 152 ACPI_STATUS result; 153 int rv, tmo; 154 struct timeval timeouttv, currenttv, timelefttv; 155 AS_LOCK_DECL; 156 157 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 158 159 if (as == NULL) 160 return_ACPI_STATUS(AE_BAD_PARAMETER); 161 162 if (cold) 163 return_ACPI_STATUS(AE_OK); 164 165 #if 0 166 if (as->as_units < Units && as->as_timeouts > 10) { 167 printf("%s: semaphore %p too many timeouts, resetting\n", __func__, as); 168 AS_LOCK(as); 169 as->as_units = as->as_maxunits; 170 if (as->as_pendings) 171 as->as_resetting = 1; 172 as->as_timeouts = 0; 173 wakeup(as); 174 AS_UNLOCK(as); 175 return_ACPI_STATUS(AE_TIME); 176 } 177 178 if (as->as_resetting) { 179 return_ACPI_STATUS(AE_TIME); 180 } 181 #endif 182 183 /* a timeout of ACPI_WAIT_FOREVER means "forever" */ 184 if (Timeout == ACPI_WAIT_FOREVER) { 185 tmo = 0; 186 timeouttv.tv_sec = ((0xffff/1000) + 1); /* cf. ACPI spec */ 187 timeouttv.tv_usec = 0; 188 } else { 189 /* compute timeout using microseconds per tick */ 190 tmo = (Timeout * 1000) / (1000000 / hz); 191 if (tmo <= 0) 192 tmo = 1; 193 timeouttv.tv_sec = Timeout / 1000; 194 timeouttv.tv_usec = (Timeout % 1000) * 1000; 195 } 196 197 /* calculate timeout value in timeval */ 198 getmicrotime(¤ttv); 199 timevaladd(&timeouttv, ¤ttv); 200 201 AS_LOCK(as); 202 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, 203 "get %d units from semaphore %p (has %d), timeout %d\n", 204 Units, as, as->as_units, Timeout)); 205 for (;;) { 206 if (as->as_maxunits == ACPI_NO_UNIT_LIMIT) { 207 result = AE_OK; 208 break; 209 } 210 if (as->as_units >= Units) { 211 as->as_units -= Units; 212 result = AE_OK; 213 break; 214 } 215 216 /* limit number of pending treads */ 217 if (as->as_pendings >= ACPI_SEMAPHORES_MAX_PENDING) { 218 result = AE_TIME; 219 break; 220 } 221 222 /* if timeout values of zero is specified, return immediately */ 223 if (Timeout == 0) { 224 result = AE_TIME; 225 break; 226 } 227 228 #if __FreeBSD_version >= 500000 229 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, 230 "semaphore blocked, calling msleep(%p, %p, %d, \"acsem\", %d)\n", 231 as, &as->as_mtx, PCATCH, tmo)); 232 #endif 233 234 as->as_pendings++; 235 236 if (acpi_semaphore_debug) { 237 printf("%s: Sleep %d, pending %d, semaphore %p, thread %d\n", 238 __func__, Timeout, as->as_pendings, as, AcpiOsGetThreadId()); 239 } 240 241 rv = msleep(as, &as->as_mtx, PCATCH, "acsem", tmo); 242 243 as->as_pendings--; 244 245 #if 0 246 if (as->as_resetting) { 247 /* semaphore reset, return immediately */ 248 if (as->as_pendings == 0) { 249 as->as_resetting = 0; 250 } 251 result = AE_TIME; 252 break; 253 } 254 #endif 255 256 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "msleep(%d) returned %d\n", tmo, rv)); 257 if (rv == EWOULDBLOCK) { 258 result = AE_TIME; 259 break; 260 } 261 262 /* check if we already awaited enough */ 263 timelefttv = timeouttv; 264 getmicrotime(¤ttv); 265 timevalsub(&timelefttv, ¤ttv); 266 if (timelefttv.tv_sec < 0) { 267 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "await semaphore %p timeout\n", as)); 268 result = AE_TIME; 269 break; 270 } 271 272 /* adjust timeout for the next sleep */ 273 tmo = (timelefttv.tv_sec * 1000000 + timelefttv.tv_usec) / (1000000 / hz); 274 if (tmo <= 0) 275 tmo = 1; 276 277 if (acpi_semaphore_debug) { 278 printf("%s: Wakeup timeleft(%lu, %lu), tmo %u, semaphore %p, thread %d\n", 279 __func__, timelefttv.tv_sec, timelefttv.tv_usec, tmo, as, AcpiOsGetThreadId()); 280 } 281 } 282 283 if (acpi_semaphore_debug) { 284 if (result == AE_TIME && Timeout > 0) { 285 printf("%s: Timeout %d, pending %d, semaphore %p\n", 286 __func__, Timeout, as->as_pendings, as); 287 } 288 if (result == AE_OK && (as->as_timeouts > 0 || as->as_pendings > 0)) { 289 printf("%s: Acquire %d, units %d, pending %d, semaphore %p, thread %d\n", 290 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId()); 291 } 292 } 293 294 if (result == AE_TIME) { 295 as->as_timeouts++; 296 } else { 297 as->as_timeouts = 0; 298 } 299 300 AS_UNLOCK(as); 301 302 return_ACPI_STATUS(result); 303 #else 304 return(AE_OK); 305 #endif 306 } 307 308 ACPI_STATUS 309 AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units) 310 { 311 #ifndef ACPI_NO_SEMAPHORES 312 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 313 AS_LOCK_DECL; 314 315 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 316 317 if (as == NULL) 318 return_ACPI_STATUS(AE_BAD_PARAMETER); 319 320 AS_LOCK(as); 321 ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, 322 "return %d units to semaphore %p (has %d)\n", 323 Units, as, as->as_units)); 324 if (as->as_maxunits != ACPI_NO_UNIT_LIMIT) { 325 as->as_units += Units; 326 if (as->as_units > as->as_maxunits) 327 as->as_units = as->as_maxunits; 328 } 329 330 if (acpi_semaphore_debug && (as->as_timeouts > 0 || as->as_pendings > 0)) { 331 printf("%s: Release %d, units %d, pending %d, semaphore %p, thread %d\n", 332 __func__, Units, as->as_units, as->as_pendings, as, AcpiOsGetThreadId()); 333 } 334 335 wakeup(as); 336 AS_UNLOCK(as); 337 return_ACPI_STATUS(AE_OK); 338 #else 339 return(AE_OK); 340 #endif 341 } 342