OsdSynch.c revision 71359
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 71359 2001-01-22 05:33:36Z 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/malloc.h> 38#include <sys/mutex.h> 39 40static MALLOC_DEFINE(M_ACPISEM, "acpisem", "ACPI semaphore"); 41 42/* 43 * Simple counting semaphore implemented using a mutex. (Subsequently used 44 * in the OSI code to implement a mutex. Go figure.) 45 */ 46struct acpi_semaphore { 47 struct mtx as_mtx; 48 UINT32 as_units; 49 UINT32 as_maxunits; 50}; 51 52ACPI_STATUS 53AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_HANDLE *OutHandle) 54{ 55 struct acpi_semaphore *as; 56 57 if (OutHandle == NULL) 58 return(AE_BAD_PARAMETER); 59 if (InitialUnits > MaxUnits) 60 return(AE_BAD_PARAMETER); 61 62 if ((as = malloc(sizeof(*as), M_ACPISEM, M_NOWAIT)) == NULL) 63 return(AE_NO_MEMORY); 64 65 mtx_init(&as->as_mtx, "ACPI semaphore", MTX_DEF); 66 as->as_units = InitialUnits; 67 as->as_maxunits = MaxUnits; 68 69 *OutHandle = (ACPI_HANDLE)as; 70 return(AE_OK); 71} 72 73ACPI_STATUS 74AcpiOsDeleteSemaphore (ACPI_HANDLE Handle) 75{ 76 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 77 mtx_destroy(&as->as_mtx); 78 free(Handle, M_ACPISEM); 79 return(AE_OK); 80} 81 82/* 83 * This implementation has a bug, in that it has to stall for the entire 84 * timeout before it will return AE_TIME. A better implementation would 85 * use getmicrotime() to correctly adjust the timeout after being woken up. 86 */ 87ACPI_STATUS 88AcpiOsWaitSemaphore(ACPI_HANDLE Handle, UINT32 Units, UINT32 Timeout) 89{ 90 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 91 int result; 92 93 if (as == NULL) 94 return(AE_BAD_PARAMETER); 95 96 mtx_enter(&as->as_mtx, MTX_DEF); 97 for (;;) { 98 if (as->as_units >= Units) { 99 as->as_units -= Units; 100 result = AE_OK; 101 break; 102 } 103 if (Timeout < 0) { 104 result = AE_TIME; 105 break; 106 } 107 if (msleep(as, &as->as_mtx, 0, "acpisem", Timeout / (1000 * hz)) == EWOULDBLOCK) { 108 result = AE_TIME; 109 break; 110 } 111 } 112 mtx_exit(&as->as_mtx, MTX_DEF); 113 114 return(result); 115} 116 117ACPI_STATUS 118AcpiOsSignalSemaphore(ACPI_HANDLE Handle, UINT32 Units) 119{ 120 struct acpi_semaphore *as = (struct acpi_semaphore *)Handle; 121 122 if (as == NULL) 123 return(AE_BAD_PARAMETER); 124 125 mtx_enter(&as->as_mtx, MTX_DEF); 126 as->as_units += Units; 127 if (as->as_units > as->as_maxunits) 128 as->as_units = as->as_maxunits; 129 wakeup(as); 130 mtx_exit(&as->as_mtx, MTX_DEF); 131 132 return(AE_OK); 133} 134