Path: blob/main/sys/contrib/dev/acpica/os_specific/service_layers/osunixxf.c
48425 views
/******************************************************************************1*2* Module Name: osunixxf - UNIX OSL interfaces3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151/*152* These interfaces are required in order to compile the ASL compiler and the153* various ACPICA tools under Linux or other Unix-like system.154*/155#include <contrib/dev/acpica/include/acpi.h>156#include <contrib/dev/acpica/include/accommon.h>157#include <contrib/dev/acpica/include/amlcode.h>158#include <contrib/dev/acpica/include/acparser.h>159#include <contrib/dev/acpica/include/acdebug.h>160161#include <stdio.h>162#include <stdlib.h>163#include <stdarg.h>164#include <unistd.h>165#include <sys/time.h>166#include <semaphore.h>167#include <pthread.h>168#include <errno.h>169170#define _COMPONENT ACPI_OS_SERVICES171ACPI_MODULE_NAME ("osunixxf")172173174/* Upcalls to AcpiExec */175176void177AeTableOverride (178ACPI_TABLE_HEADER *ExistingTable,179ACPI_TABLE_HEADER **NewTable);180181typedef void* (*PTHREAD_CALLBACK) (void *);182183/* Buffer used by AcpiOsVprintf */184185#define ACPI_VPRINTF_BUFFER_SIZE 512186#define _ASCII_NEWLINE '\n'187188/* Terminal support for AcpiExec only */189190#ifdef ACPI_EXEC_APP191#include <termios.h>192193struct termios OriginalTermAttributes;194int TermAttributesWereSet = 0;195196ACPI_STATUS197AcpiUtReadLine (198char *Buffer,199UINT32 BufferLength,200UINT32 *BytesRead);201202static void203OsEnterLineEditMode (204void);205206static void207OsExitLineEditMode (208void);209210211/******************************************************************************212*213* FUNCTION: OsEnterLineEditMode, OsExitLineEditMode214*215* PARAMETERS: None216*217* RETURN: None218*219* DESCRIPTION: Enter/Exit the raw character input mode for the terminal.220*221* Interactive line-editing support for the AML debugger. Used with the222* common/acgetline module.223*224* readline() is not used because of non-portability. It is not available225* on all systems, and if it is, often the package must be manually installed.226*227* Therefore, we use the POSIX tcgetattr/tcsetattr and do the minimal line228* editing that we need in AcpiOsGetLine.229*230* If the POSIX tcgetattr/tcsetattr interfaces are unavailable, these231* calls will also work:232* For OsEnterLineEditMode: system ("stty cbreak -echo")233* For OsExitLineEditMode: system ("stty cooked echo")234*235*****************************************************************************/236237static void238OsEnterLineEditMode (239void)240{241struct termios LocalTermAttributes;242243244TermAttributesWereSet = 0;245246/* STDIN must be a terminal */247248if (!isatty (STDIN_FILENO))249{250return;251}252253/* Get and keep the original attributes */254255if (tcgetattr (STDIN_FILENO, &OriginalTermAttributes))256{257fprintf (stderr, "Could not get terminal attributes!\n");258return;259}260261/* Set the new attributes to enable raw character input */262263memcpy (&LocalTermAttributes, &OriginalTermAttributes,264sizeof (struct termios));265266LocalTermAttributes.c_lflag &= ~(ICANON | ECHO);267LocalTermAttributes.c_cc[VMIN] = 1;268LocalTermAttributes.c_cc[VTIME] = 0;269270if (tcsetattr (STDIN_FILENO, TCSANOW, &LocalTermAttributes))271{272fprintf (stderr, "Could not set terminal attributes!\n");273return;274}275276TermAttributesWereSet = 1;277}278279280static void281OsExitLineEditMode (282void)283{284285if (!TermAttributesWereSet)286{287return;288}289290/* Set terminal attributes back to the original values */291292if (tcsetattr (STDIN_FILENO, TCSANOW, &OriginalTermAttributes))293{294fprintf (stderr, "Could not restore terminal attributes!\n");295}296}297298299#else300301/* These functions are not needed for other ACPICA utilities */302303#define OsEnterLineEditMode()304#define OsExitLineEditMode()305#endif306307308/******************************************************************************309*310* FUNCTION: AcpiOsInitialize, AcpiOsTerminate311*312* PARAMETERS: None313*314* RETURN: Status315*316* DESCRIPTION: Initialize and terminate this module.317*318*****************************************************************************/319320ACPI_STATUS321AcpiOsInitialize (322void)323{324ACPI_STATUS Status;325326327AcpiGbl_OutputFile = stdout;328329OsEnterLineEditMode ();330331Status = AcpiOsCreateLock (&AcpiGbl_PrintLock);332if (ACPI_FAILURE (Status))333{334return (Status);335}336337return (AE_OK);338}339340ACPI_STATUS341AcpiOsTerminate (342void)343{344345OsExitLineEditMode ();346return (AE_OK);347}348349350#ifndef ACPI_USE_NATIVE_RSDP_POINTER351/******************************************************************************352*353* FUNCTION: AcpiOsGetRootPointer354*355* PARAMETERS: None356*357* RETURN: RSDP physical address358*359* DESCRIPTION: Gets the ACPI root pointer (RSDP)360*361*****************************************************************************/362363ACPI_PHYSICAL_ADDRESS364AcpiOsGetRootPointer (365void)366{367368return (0);369}370#endif371372373/******************************************************************************374*375* FUNCTION: AcpiOsPredefinedOverride376*377* PARAMETERS: InitVal - Initial value of the predefined object378* NewVal - The new value for the object379*380* RETURN: Status, pointer to value. Null pointer returned if not381* overriding.382*383* DESCRIPTION: Allow the OS to override predefined names384*385*****************************************************************************/386387ACPI_STATUS388AcpiOsPredefinedOverride (389const ACPI_PREDEFINED_NAMES *InitVal,390ACPI_STRING *NewVal)391{392393if (!InitVal || !NewVal)394{395return (AE_BAD_PARAMETER);396}397398*NewVal = NULL;399return (AE_OK);400}401402403/******************************************************************************404*405* FUNCTION: AcpiOsTableOverride406*407* PARAMETERS: ExistingTable - Header of current table (probably408* firmware)409* NewTable - Where an entire new table is returned.410*411* RETURN: Status, pointer to new table. Null pointer returned if no412* table is available to override413*414* DESCRIPTION: Return a different version of a table if one is available415*416*****************************************************************************/417418ACPI_STATUS419AcpiOsTableOverride (420ACPI_TABLE_HEADER *ExistingTable,421ACPI_TABLE_HEADER **NewTable)422{423424if (!ExistingTable || !NewTable)425{426return (AE_BAD_PARAMETER);427}428429*NewTable = NULL;430431#ifdef ACPI_EXEC_APP432433AeTableOverride (ExistingTable, NewTable);434return (AE_OK);435#else436437return (AE_NO_ACPI_TABLES);438#endif439}440441442/******************************************************************************443*444* FUNCTION: AcpiOsPhysicalTableOverride445*446* PARAMETERS: ExistingTable - Header of current table (probably firmware)447* NewAddress - Where new table address is returned448* (Physical address)449* NewTableLength - Where new table length is returned450*451* RETURN: Status, address/length of new table. Null pointer returned452* if no table is available to override.453*454* DESCRIPTION: Returns AE_SUPPORT, function not used in user space.455*456*****************************************************************************/457458ACPI_STATUS459AcpiOsPhysicalTableOverride (460ACPI_TABLE_HEADER *ExistingTable,461ACPI_PHYSICAL_ADDRESS *NewAddress,462UINT32 *NewTableLength)463{464465return (AE_SUPPORT);466}467468469/******************************************************************************470*471* FUNCTION: AcpiOsEnterSleep472*473* PARAMETERS: SleepState - Which sleep state to enter474* RegaValue - Register A value475* RegbValue - Register B value476*477* RETURN: Status478*479* DESCRIPTION: A hook before writing sleep registers to enter the sleep480* state. Return AE_CTRL_TERMINATE to skip further sleep register481* writes.482*483*****************************************************************************/484485ACPI_STATUS486AcpiOsEnterSleep (487UINT8 SleepState,488UINT32 RegaValue,489UINT32 RegbValue)490{491492return (AE_OK);493}494495496/******************************************************************************497*498* FUNCTION: AcpiOsRedirectOutput499*500* PARAMETERS: Destination - An open file handle/pointer501*502* RETURN: None503*504* DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf505*506*****************************************************************************/507508void509AcpiOsRedirectOutput (510void *Destination)511{512513AcpiGbl_OutputFile = Destination;514}515516517/******************************************************************************518*519* FUNCTION: AcpiOsPrintf520*521* PARAMETERS: fmt, ... - Standard printf format522*523* RETURN: None524*525* DESCRIPTION: Formatted output. Note: very similar to AcpiOsVprintf526* (performance), changes should be tracked in both functions.527*528*****************************************************************************/529530void ACPI_INTERNAL_VAR_XFACE531AcpiOsPrintf (532const char *Fmt,533...)534{535va_list Args;536UINT8 Flags;537538539Flags = AcpiGbl_DbOutputFlags;540if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)541{542/* Output is directable to either a file (if open) or the console */543544if (AcpiGbl_DebugFile)545{546/* Output file is open, send the output there */547548va_start (Args, Fmt);549vfprintf (AcpiGbl_DebugFile, Fmt, Args);550va_end (Args);551}552else553{554/* No redirection, send output to console (once only!) */555556Flags |= ACPI_DB_CONSOLE_OUTPUT;557}558}559560if (Flags & ACPI_DB_CONSOLE_OUTPUT)561{562va_start (Args, Fmt);563vfprintf (AcpiGbl_OutputFile, Fmt, Args);564va_end (Args);565}566}567568569/******************************************************************************570*571* FUNCTION: AcpiOsVprintf572*573* PARAMETERS: fmt - Standard printf format574* args - Argument list575*576* RETURN: None577*578* DESCRIPTION: Formatted output with argument list pointer. Note: very579* similar to AcpiOsPrintf, changes should be tracked in both580* functions.581*582*****************************************************************************/583584void585AcpiOsVprintf (586const char *Fmt,587va_list Args)588{589UINT8 Flags;590char Buffer[ACPI_VPRINTF_BUFFER_SIZE];591592593/*594* We build the output string in a local buffer because we may be595* outputting the buffer twice. Using vfprintf is problematic because596* some implementations modify the args pointer/structure during597* execution. Thus, we use the local buffer for portability.598*599* Note: Since this module is intended for use by the various ACPICA600* utilities/applications, we can safely declare the buffer on the stack.601* Also, This function is used for relatively small error messages only.602*/603vsnprintf (Buffer, ACPI_VPRINTF_BUFFER_SIZE, Fmt, Args);604605Flags = AcpiGbl_DbOutputFlags;606if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)607{608/* Output is directable to either a file (if open) or the console */609610if (AcpiGbl_DebugFile)611{612/* Output file is open, send the output there */613614fputs (Buffer, AcpiGbl_DebugFile);615}616else617{618/* No redirection, send output to console (once only!) */619620Flags |= ACPI_DB_CONSOLE_OUTPUT;621}622}623624if (Flags & ACPI_DB_CONSOLE_OUTPUT)625{626fputs (Buffer, AcpiGbl_OutputFile);627}628}629630631#ifndef ACPI_EXEC_APP632/******************************************************************************633*634* FUNCTION: AcpiOsGetLine635*636* PARAMETERS: Buffer - Where to return the command line637* BufferLength - Maximum length of Buffer638* BytesRead - Where the actual byte count is returned639*640* RETURN: Status and actual bytes read641*642* DESCRIPTION: Get the next input line from the terminal. NOTE: For the643* AcpiExec utility, we use the acgetline module instead to644* provide line-editing and history support.645*646*****************************************************************************/647648ACPI_STATUS649AcpiOsGetLine (650char *Buffer,651UINT32 BufferLength,652UINT32 *BytesRead)653{654int InputChar;655UINT32 EndOfLine;656657658/* Standard AcpiOsGetLine for all utilities except AcpiExec */659660for (EndOfLine = 0; ; EndOfLine++)661{662if (EndOfLine >= BufferLength)663{664return (AE_BUFFER_OVERFLOW);665}666667if ((InputChar = getchar ()) == EOF)668{669return (AE_ERROR);670}671672if (!InputChar || InputChar == _ASCII_NEWLINE)673{674break;675}676677Buffer[EndOfLine] = (char) InputChar;678}679680/* Null terminate the buffer */681682Buffer[EndOfLine] = 0;683684/* Return the number of bytes in the string */685686if (BytesRead)687{688*BytesRead = EndOfLine;689}690691return (AE_OK);692}693#endif694695696#ifndef ACPI_USE_NATIVE_MEMORY_MAPPING697/******************************************************************************698*699* FUNCTION: AcpiOsMapMemory700*701* PARAMETERS: where - Physical address of memory to be mapped702* length - How much memory to map703*704* RETURN: Pointer to mapped memory. Null on error.705*706* DESCRIPTION: Map physical memory into caller's address space707*708*****************************************************************************/709710void *711AcpiOsMapMemory (712ACPI_PHYSICAL_ADDRESS where,713ACPI_SIZE length)714{715716return (ACPI_TO_POINTER ((ACPI_SIZE) where));717}718719720/******************************************************************************721*722* FUNCTION: AcpiOsUnmapMemory723*724* PARAMETERS: where - Logical address of memory to be unmapped725* length - How much memory to unmap726*727* RETURN: None.728*729* DESCRIPTION: Delete a previously created mapping. Where and Length must730* correspond to a previous mapping exactly.731*732*****************************************************************************/733734void735AcpiOsUnmapMemory (736void *where,737ACPI_SIZE length)738{739740return;741}742#endif743744745/******************************************************************************746*747* FUNCTION: AcpiOsAllocate748*749* PARAMETERS: Size - Amount to allocate, in bytes750*751* RETURN: Pointer to the new allocation. Null on error.752*753* DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.754*755*****************************************************************************/756757void *758AcpiOsAllocate (759ACPI_SIZE size)760{761void *Mem;762763764Mem = (void *) malloc ((size_t) size);765return (Mem);766}767768769#ifdef USE_NATIVE_ALLOCATE_ZEROED770/******************************************************************************771*772* FUNCTION: AcpiOsAllocateZeroed773*774* PARAMETERS: Size - Amount to allocate, in bytes775*776* RETURN: Pointer to the new allocation. Null on error.777*778* DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.779*780*****************************************************************************/781782void *783AcpiOsAllocateZeroed (784ACPI_SIZE size)785{786void *Mem;787788789Mem = (void *) calloc (1, (size_t) size);790return (Mem);791}792#endif793794795/******************************************************************************796*797* FUNCTION: AcpiOsFree798*799* PARAMETERS: mem - Pointer to previously allocated memory800*801* RETURN: None.802*803* DESCRIPTION: Free memory allocated via AcpiOsAllocate804*805*****************************************************************************/806807void808AcpiOsFree (809void *mem)810{811812free (mem);813}814815816#ifdef ACPI_SINGLE_THREADED817/******************************************************************************818*819* FUNCTION: Semaphore stub functions820*821* DESCRIPTION: Stub functions used for single-thread applications that do822* not require semaphore synchronization. Full implementations823* of these functions appear after the stubs.824*825*****************************************************************************/826827ACPI_STATUS828AcpiOsCreateSemaphore (829UINT32 MaxUnits,830UINT32 InitialUnits,831ACPI_HANDLE *OutHandle)832{833*OutHandle = (ACPI_HANDLE) 1;834return (AE_OK);835}836837ACPI_STATUS838AcpiOsDeleteSemaphore (839ACPI_HANDLE Handle)840{841return (AE_OK);842}843844ACPI_STATUS845AcpiOsWaitSemaphore (846ACPI_HANDLE Handle,847UINT32 Units,848UINT16 Timeout)849{850return (AE_OK);851}852853ACPI_STATUS854AcpiOsSignalSemaphore (855ACPI_HANDLE Handle,856UINT32 Units)857{858return (AE_OK);859}860861#else862/******************************************************************************863*864* FUNCTION: AcpiOsCreateSemaphore865*866* PARAMETERS: InitialUnits - Units to be assigned to the new semaphore867* OutHandle - Where a handle will be returned868*869* RETURN: Status870*871* DESCRIPTION: Create an OS semaphore872*873*****************************************************************************/874875ACPI_STATUS876AcpiOsCreateSemaphore (877UINT32 MaxUnits,878UINT32 InitialUnits,879ACPI_HANDLE *OutHandle)880{881sem_t *Sem;882883884if (!OutHandle)885{886return (AE_BAD_PARAMETER);887}888889#ifdef __APPLE__890{891static int SemaphoreCount = 0;892char SemaphoreName[32];893894snprintf (SemaphoreName, sizeof (SemaphoreName), "acpi_sem_%d",895SemaphoreCount++);896printf ("%s\n", SemaphoreName);897Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);898if (!Sem)899{900return (AE_NO_MEMORY);901}902sem_unlink (SemaphoreName); /* This just deletes the name */903}904905#else906Sem = AcpiOsAllocate (sizeof (sem_t));907if (!Sem)908{909return (AE_NO_MEMORY);910}911912if (sem_init (Sem, 0, InitialUnits) == -1)913{914AcpiOsFree (Sem);915return (AE_BAD_PARAMETER);916}917#endif918919*OutHandle = (ACPI_HANDLE) Sem;920return (AE_OK);921}922923924/******************************************************************************925*926* FUNCTION: AcpiOsDeleteSemaphore927*928* PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore929*930* RETURN: Status931*932* DESCRIPTION: Delete an OS semaphore933*934*****************************************************************************/935936ACPI_STATUS937AcpiOsDeleteSemaphore (938ACPI_HANDLE Handle)939{940sem_t *Sem = (sem_t *) Handle;941942943if (!Sem)944{945return (AE_BAD_PARAMETER);946}947948#ifdef __APPLE__949if (sem_close (Sem) == -1)950{951return (AE_BAD_PARAMETER);952}953#else954if (sem_destroy (Sem) == -1)955{956return (AE_BAD_PARAMETER);957}958#endif959960return (AE_OK);961}962963964/******************************************************************************965*966* FUNCTION: AcpiOsWaitSemaphore967*968* PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore969* Units - How many units to wait for970* MsecTimeout - How long to wait (milliseconds)971*972* RETURN: Status973*974* DESCRIPTION: Wait for units975*976*****************************************************************************/977978ACPI_STATUS979AcpiOsWaitSemaphore (980ACPI_HANDLE Handle,981UINT32 Units,982UINT16 MsecTimeout)983{984ACPI_STATUS Status = AE_OK;985sem_t *Sem = (sem_t *) Handle;986int RetVal;987#ifndef ACPI_USE_ALTERNATE_TIMEOUT988struct timespec Time;989#endif990991992if (!Sem)993{994return (AE_BAD_PARAMETER);995}996997switch (MsecTimeout)998{999/*1000* No Wait:1001* --------1002* A zero timeout value indicates that we shouldn't wait - just1003* acquire the semaphore if available otherwise return AE_TIME1004* (a.k.a. 'would block').1005*/1006case 0:10071008if (sem_trywait(Sem) == -1)1009{1010Status = (AE_TIME);1011}1012break;10131014/* Wait Indefinitely */10151016case ACPI_WAIT_FOREVER:10171018while (((RetVal = sem_wait (Sem)) == -1) && (errno == EINTR))1019{1020continue; /* Restart if interrupted */1021}1022if (RetVal != 0)1023{1024Status = (AE_TIME);1025}1026break;102710281029/* Wait with MsecTimeout */10301031default:10321033#ifdef ACPI_USE_ALTERNATE_TIMEOUT1034/*1035* Alternate timeout mechanism for environments where1036* sem_timedwait is not available or does not work properly.1037*/1038while (MsecTimeout)1039{1040if (sem_trywait (Sem) == 0)1041{1042/* Got the semaphore */1043return (AE_OK);1044}10451046if (MsecTimeout >= 10)1047{1048MsecTimeout -= 10;1049usleep (10 * ACPI_USEC_PER_MSEC); /* ten milliseconds */1050}1051else1052{1053MsecTimeout--;1054usleep (ACPI_USEC_PER_MSEC); /* one millisecond */1055}1056}1057Status = (AE_TIME);1058#else1059/*1060* The interface to sem_timedwait is an absolute time, so we need to1061* get the current time, then add in the millisecond Timeout value.1062*/1063if (clock_gettime (CLOCK_REALTIME, &Time) == -1)1064{1065perror ("clock_gettime");1066return (AE_TIME);1067}10681069Time.tv_sec += (MsecTimeout / ACPI_MSEC_PER_SEC);1070Time.tv_nsec += ((MsecTimeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);10711072/* Handle nanosecond overflow (field must be less than one second) */10731074if (Time.tv_nsec >= ACPI_NSEC_PER_SEC)1075{1076Time.tv_sec += (Time.tv_nsec / ACPI_NSEC_PER_SEC);1077Time.tv_nsec = (Time.tv_nsec % ACPI_NSEC_PER_SEC);1078}10791080while (((RetVal = sem_timedwait (Sem, &Time)) == -1) && (errno == EINTR))1081{1082continue; /* Restart if interrupted */10831084}10851086if (RetVal != 0)1087{1088if (errno != ETIMEDOUT)1089{1090perror ("sem_timedwait");1091}1092Status = (AE_TIME);1093}1094#endif1095break;1096}10971098return (Status);1099}110011011102/******************************************************************************1103*1104* FUNCTION: AcpiOsSignalSemaphore1105*1106* PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore1107* Units - Number of units to send1108*1109* RETURN: Status1110*1111* DESCRIPTION: Send units1112*1113*****************************************************************************/11141115ACPI_STATUS1116AcpiOsSignalSemaphore (1117ACPI_HANDLE Handle,1118UINT32 Units)1119{1120sem_t *Sem = (sem_t *)Handle;112111221123if (!Sem)1124{1125return (AE_BAD_PARAMETER);1126}11271128if (sem_post (Sem) == -1)1129{1130return (AE_LIMIT);1131}11321133return (AE_OK);1134}11351136#endif /* ACPI_SINGLE_THREADED */113711381139/******************************************************************************1140*1141* FUNCTION: Spinlock interfaces1142*1143* DESCRIPTION: Map these interfaces to semaphore interfaces1144*1145*****************************************************************************/11461147ACPI_STATUS1148AcpiOsCreateLock (1149ACPI_SPINLOCK *OutHandle)1150{11511152return (AcpiOsCreateSemaphore (1, 1, OutHandle));1153}115411551156void1157AcpiOsDeleteLock (1158ACPI_SPINLOCK Handle)1159{1160AcpiOsDeleteSemaphore (Handle);1161}116211631164ACPI_CPU_FLAGS1165AcpiOsAcquireLock (1166ACPI_HANDLE Handle)1167{1168AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);1169return (0);1170}117111721173void1174AcpiOsReleaseLock (1175ACPI_SPINLOCK Handle,1176ACPI_CPU_FLAGS Flags)1177{1178AcpiOsSignalSemaphore (Handle, 1);1179}118011811182/******************************************************************************1183*1184* FUNCTION: AcpiOsInstallInterruptHandler1185*1186* PARAMETERS: InterruptNumber - Level handler should respond to.1187* Isr - Address of the ACPI interrupt handler1188* ExceptPtr - Where status is returned1189*1190* RETURN: Handle to the newly installed handler.1191*1192* DESCRIPTION: Install an interrupt handler. Used to install the ACPI1193* OS-independent handler.1194*1195*****************************************************************************/11961197UINT321198AcpiOsInstallInterruptHandler (1199UINT32 InterruptNumber,1200ACPI_OSD_HANDLER ServiceRoutine,1201void *Context)1202{12031204return (AE_OK);1205}120612071208/******************************************************************************1209*1210* FUNCTION: AcpiOsRemoveInterruptHandler1211*1212* PARAMETERS: Handle - Returned when handler was installed1213*1214* RETURN: Status1215*1216* DESCRIPTION: Uninstalls an interrupt handler.1217*1218*****************************************************************************/12191220ACPI_STATUS1221AcpiOsRemoveInterruptHandler (1222UINT32 InterruptNumber,1223ACPI_OSD_HANDLER ServiceRoutine)1224{12251226return (AE_OK);1227}122812291230/******************************************************************************1231*1232* FUNCTION: AcpiOsStall1233*1234* PARAMETERS: microseconds - Time to sleep1235*1236* RETURN: Blocks until sleep is completed.1237*1238* DESCRIPTION: Sleep at microsecond granularity1239*1240*****************************************************************************/12411242void1243AcpiOsStall (1244UINT32 microseconds)1245{12461247if (microseconds)1248{1249usleep (microseconds);1250}1251}125212531254/******************************************************************************1255*1256* FUNCTION: AcpiOsSleep1257*1258* PARAMETERS: milliseconds - Time to sleep1259*1260* RETURN: Blocks until sleep is completed.1261*1262* DESCRIPTION: Sleep at millisecond granularity1263*1264*****************************************************************************/12651266void1267AcpiOsSleep (1268UINT64 milliseconds)1269{12701271/* Sleep for whole seconds */12721273sleep (milliseconds / ACPI_MSEC_PER_SEC);12741275/*1276* Sleep for remaining microseconds.1277* Arg to usleep() is in usecs and must be less than 1,000,000 (1 second).1278*/1279usleep ((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);1280}128112821283/******************************************************************************1284*1285* FUNCTION: AcpiOsGetTimer1286*1287* PARAMETERS: None1288*1289* RETURN: Current time in 100 nanosecond units1290*1291* DESCRIPTION: Get the current system time1292*1293*****************************************************************************/12941295UINT641296AcpiOsGetTimer (1297void)1298{1299struct timeval time;130013011302/* This timer has sufficient resolution for user-space application code */13031304gettimeofday (&time, NULL);13051306/* (Seconds * 10^7 = 100ns(10^-7)) + (Microseconds(10^-6) * 10^1 = 100ns) */13071308return (((UINT64) time.tv_sec * ACPI_100NSEC_PER_SEC) +1309((UINT64) time.tv_usec * ACPI_100NSEC_PER_USEC));1310}131113121313/******************************************************************************1314*1315* FUNCTION: AcpiOsReadPciConfiguration1316*1317* PARAMETERS: PciId - Seg/Bus/Dev1318* PciRegister - Device Register1319* Value - Buffer where value is placed1320* Width - Number of bits1321*1322* RETURN: Status1323*1324* DESCRIPTION: Read data from PCI configuration space1325*1326*****************************************************************************/13271328ACPI_STATUS1329AcpiOsReadPciConfiguration (1330ACPI_PCI_ID *PciId,1331UINT32 PciRegister,1332UINT64 *Value,1333UINT32 Width)1334{13351336*Value = 0;1337return (AE_OK);1338}133913401341/******************************************************************************1342*1343* FUNCTION: AcpiOsWritePciConfiguration1344*1345* PARAMETERS: PciId - Seg/Bus/Dev1346* PciRegister - Device Register1347* Value - Value to be written1348* Width - Number of bits1349*1350* RETURN: Status.1351*1352* DESCRIPTION: Write data to PCI configuration space1353*1354*****************************************************************************/13551356ACPI_STATUS1357AcpiOsWritePciConfiguration (1358ACPI_PCI_ID *PciId,1359UINT32 PciRegister,1360UINT64 Value,1361UINT32 Width)1362{13631364return (AE_OK);1365}136613671368/******************************************************************************1369*1370* FUNCTION: AcpiOsReadPort1371*1372* PARAMETERS: Address - Address of I/O port/register to read1373* Value - Where value is placed1374* Width - Number of bits1375*1376* RETURN: Value read from port1377*1378* DESCRIPTION: Read data from an I/O port or register1379*1380*****************************************************************************/13811382ACPI_STATUS1383AcpiOsReadPort (1384ACPI_IO_ADDRESS Address,1385UINT32 *Value,1386UINT32 Width)1387{13881389switch (Width)1390{1391case 8:13921393*Value = 0xFF;1394break;13951396case 16:13971398*Value = 0xFFFF;1399break;14001401case 32:14021403*Value = 0xFFFFFFFF;1404break;14051406default:14071408return (AE_BAD_PARAMETER);1409}14101411return (AE_OK);1412}141314141415/******************************************************************************1416*1417* FUNCTION: AcpiOsWritePort1418*1419* PARAMETERS: Address - Address of I/O port/register to write1420* Value - Value to write1421* Width - Number of bits1422*1423* RETURN: None1424*1425* DESCRIPTION: Write data to an I/O port or register1426*1427*****************************************************************************/14281429ACPI_STATUS1430AcpiOsWritePort (1431ACPI_IO_ADDRESS Address,1432UINT32 Value,1433UINT32 Width)1434{14351436return (AE_OK);1437}143814391440/******************************************************************************1441*1442* FUNCTION: AcpiOsReadMemory1443*1444* PARAMETERS: Address - Physical Memory Address to read1445* Value - Where value is placed1446* Width - Number of bits (8,16,32, or 64)1447*1448* RETURN: Value read from physical memory address. Always returned1449* as a 64-bit integer, regardless of the read width.1450*1451* DESCRIPTION: Read data from a physical memory address1452*1453*****************************************************************************/14541455ACPI_STATUS1456AcpiOsReadMemory (1457ACPI_PHYSICAL_ADDRESS Address,1458UINT64 *Value,1459UINT32 Width)1460{14611462switch (Width)1463{1464case 8:1465case 16:1466case 32:1467case 64:14681469*Value = 0;1470break;14711472default:14731474return (AE_BAD_PARAMETER);1475}1476return (AE_OK);1477}147814791480/******************************************************************************1481*1482* FUNCTION: AcpiOsWriteMemory1483*1484* PARAMETERS: Address - Physical Memory Address to write1485* Value - Value to write1486* Width - Number of bits (8,16,32, or 64)1487*1488* RETURN: None1489*1490* DESCRIPTION: Write data to a physical memory address1491*1492*****************************************************************************/14931494ACPI_STATUS1495AcpiOsWriteMemory (1496ACPI_PHYSICAL_ADDRESS Address,1497UINT64 Value,1498UINT32 Width)1499{15001501return (AE_OK);1502}150315041505/******************************************************************************1506*1507* FUNCTION: AcpiOsReadable1508*1509* PARAMETERS: Pointer - Area to be verified1510* Length - Size of area1511*1512* RETURN: TRUE if readable for entire length1513*1514* DESCRIPTION: Verify that a pointer is valid for reading1515*1516*****************************************************************************/15171518BOOLEAN1519AcpiOsReadable (1520void *Pointer,1521ACPI_SIZE Length)1522{15231524return (TRUE);1525}152615271528/******************************************************************************1529*1530* FUNCTION: AcpiOsWritable1531*1532* PARAMETERS: Pointer - Area to be verified1533* Length - Size of area1534*1535* RETURN: TRUE if writable for entire length1536*1537* DESCRIPTION: Verify that a pointer is valid for writing1538*1539*****************************************************************************/15401541BOOLEAN1542AcpiOsWritable (1543void *Pointer,1544ACPI_SIZE Length)1545{15461547return (TRUE);1548}154915501551/******************************************************************************1552*1553* FUNCTION: AcpiOsSignal1554*1555* PARAMETERS: Function - ACPI A signal function code1556* Info - Pointer to function-dependent structure1557*1558* RETURN: Status1559*1560* DESCRIPTION: Miscellaneous functions. Example implementation only.1561*1562*****************************************************************************/15631564ACPI_STATUS1565AcpiOsSignal (1566UINT32 Function,1567void *Info)1568{15691570switch (Function)1571{1572case ACPI_SIGNAL_FATAL:15731574break;15751576case ACPI_SIGNAL_BREAKPOINT:15771578break;15791580default:15811582break;1583}15841585return (AE_OK);1586}15871588/* Optional multi-thread support */15891590#ifndef ACPI_SINGLE_THREADED1591/******************************************************************************1592*1593* FUNCTION: AcpiOsGetThreadId1594*1595* PARAMETERS: None1596*1597* RETURN: Id of the running thread1598*1599* DESCRIPTION: Get the ID of the current (running) thread1600*1601*****************************************************************************/16021603ACPI_THREAD_ID1604AcpiOsGetThreadId (1605void)1606{1607pthread_t thread;160816091610thread = pthread_self();1611return (ACPI_CAST_PTHREAD_T (thread));1612}161316141615/******************************************************************************1616*1617* FUNCTION: AcpiOsExecute1618*1619* PARAMETERS: Type - Type of execution1620* Function - Address of the function to execute1621* Context - Passed as a parameter to the function1622*1623* RETURN: Status.1624*1625* DESCRIPTION: Execute a new thread1626*1627*****************************************************************************/16281629ACPI_STATUS1630AcpiOsExecute (1631ACPI_EXECUTE_TYPE Type,1632ACPI_OSD_EXEC_CALLBACK Function,1633void *Context)1634{1635pthread_t thread;1636int ret;163716381639ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);1640if (ret)1641{1642AcpiOsPrintf("Create thread failed");1643}1644return (0);1645}16461647#else /* ACPI_SINGLE_THREADED */1648ACPI_THREAD_ID1649AcpiOsGetThreadId (1650void)1651{1652return (1);1653}16541655ACPI_STATUS1656AcpiOsExecute (1657ACPI_EXECUTE_TYPE Type,1658ACPI_OSD_EXEC_CALLBACK Function,1659void *Context)1660{16611662Function (Context);16631664return (AE_OK);1665}16661667#endif /* ACPI_SINGLE_THREADED */166816691670/******************************************************************************1671*1672* FUNCTION: AcpiOsWaitEventsComplete1673*1674* PARAMETERS: None1675*1676* RETURN: None1677*1678* DESCRIPTION: Wait for all asynchronous events to complete. This1679* implementation does nothing.1680*1681*****************************************************************************/16821683void1684AcpiOsWaitEventsComplete (1685void)1686{1687return;1688}168916901691