1 /******************************************************************************
3 * Module Name: osunixxf - UNIX OSL interfaces
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
46 * These interfaces are required in order to compile the ASL compiler and the
47 * various ACPICA tools under Linux or other Unix-like system.
49 * Note: Use #define __APPLE__ for OS X generation.
51 #include <contrib/dev/acpica/include/acpi.h>
52 #include <contrib/dev/acpica/include/accommon.h>
53 #include <contrib/dev/acpica/include/amlcode.h>
54 #include <contrib/dev/acpica/include/acparser.h>
55 #include <contrib/dev/acpica/include/acdebug.h>
62 #include <semaphore.h>
66 #define _COMPONENT ACPI_OS_SERVICES
67 ACPI_MODULE_NAME ("osunixxf")
70 extern FILE *AcpiGbl_DebugFile;
71 FILE *AcpiGbl_OutputFile;
74 /* Upcalls to AcpiExec */
77 AeLocalGetRootPointer (
82 ACPI_TABLE_HEADER *ExistingTable,
83 ACPI_TABLE_HEADER **NewTable);
85 typedef void* (*PTHREAD_CALLBACK) (void *);
90 #define sem_destroy sem_close
94 /******************************************************************************
96 * FUNCTION: AcpiOsInitialize, AcpiOsTerminate
102 * DESCRIPTION: Init and terminate. Nothing to do.
104 *****************************************************************************/
111 AcpiGbl_OutputFile = stdout;
125 /******************************************************************************
127 * FUNCTION: AcpiOsGetRootPointer
131 * RETURN: RSDP physical address
133 * DESCRIPTION: Gets the ACPI root pointer (RSDP)
135 *****************************************************************************/
137 ACPI_PHYSICAL_ADDRESS
138 AcpiOsGetRootPointer (
142 return (AeLocalGetRootPointer ());
146 /******************************************************************************
148 * FUNCTION: AcpiOsPredefinedOverride
150 * PARAMETERS: InitVal - Initial value of the predefined object
151 * NewVal - The new value for the object
153 * RETURN: Status, pointer to value. Null pointer returned if not
156 * DESCRIPTION: Allow the OS to override predefined names
158 *****************************************************************************/
161 AcpiOsPredefinedOverride (
162 const ACPI_PREDEFINED_NAMES *InitVal,
166 if (!InitVal || !NewVal)
168 return (AE_BAD_PARAMETER);
176 /******************************************************************************
178 * FUNCTION: AcpiOsTableOverride
180 * PARAMETERS: ExistingTable - Header of current table (probably
182 * NewTable - Where an entire new table is returned.
184 * RETURN: Status, pointer to new table. Null pointer returned if no
185 * table is available to override
187 * DESCRIPTION: Return a different version of a table if one is available
189 *****************************************************************************/
192 AcpiOsTableOverride (
193 ACPI_TABLE_HEADER *ExistingTable,
194 ACPI_TABLE_HEADER **NewTable)
197 if (!ExistingTable || !NewTable)
199 return (AE_BAD_PARAMETER);
206 AeTableOverride (ExistingTable, NewTable);
210 return (AE_NO_ACPI_TABLES);
215 /******************************************************************************
217 * FUNCTION: AcpiOsRedirectOutput
219 * PARAMETERS: Destination - An open file handle/pointer
223 * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
225 *****************************************************************************/
228 AcpiOsRedirectOutput (
232 AcpiGbl_OutputFile = Destination;
236 /******************************************************************************
238 * FUNCTION: AcpiOsPrintf
240 * PARAMETERS: fmt, ... - Standard printf format
244 * DESCRIPTION: Formatted output
246 *****************************************************************************/
248 void ACPI_INTERNAL_VAR_XFACE
256 va_start (Args, Fmt);
257 AcpiOsVprintf (Fmt, Args);
262 /******************************************************************************
264 * FUNCTION: AcpiOsVprintf
266 * PARAMETERS: fmt - Standard printf format
267 * args - Argument list
271 * DESCRIPTION: Formatted output with argument list pointer
273 *****************************************************************************/
283 Flags = AcpiGbl_DbOutputFlags;
284 if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
286 /* Output is directable to either a file (if open) or the console */
288 if (AcpiGbl_DebugFile)
290 /* Output file is open, send the output there */
292 vfprintf (AcpiGbl_DebugFile, Fmt, Args);
296 /* No redirection, send output to console (once only!) */
298 Flags |= ACPI_DB_CONSOLE_OUTPUT;
302 if (Flags & ACPI_DB_CONSOLE_OUTPUT)
304 vfprintf (AcpiGbl_OutputFile, Fmt, Args);
309 /******************************************************************************
311 * FUNCTION: AcpiOsGetLine
313 * PARAMETERS: Buffer - Where to return the command line
314 * BufferLength - Maximum length of Buffer
315 * BytesRead - Where the actual byte count is returned
317 * RETURN: Status and actual bytes read
319 * DESCRIPTION: Formatted input with argument list pointer
321 *****************************************************************************/
335 if (i >= BufferLength)
337 return (AE_BUFFER_OVERFLOW);
340 scanf ("%1c", &Temp);
341 if (!Temp || Temp == '\n')
349 /* Null terminate the buffer */
353 /* Return the number of bytes in the string */
363 /******************************************************************************
365 * FUNCTION: AcpiOsMapMemory
367 * PARAMETERS: where - Physical address of memory to be mapped
368 * length - How much memory to map
370 * RETURN: Pointer to mapped memory. Null on error.
372 * DESCRIPTION: Map physical memory into caller's address space
374 *****************************************************************************/
378 ACPI_PHYSICAL_ADDRESS where,
382 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
386 /******************************************************************************
388 * FUNCTION: AcpiOsUnmapMemory
390 * PARAMETERS: where - Logical address of memory to be unmapped
391 * length - How much memory to unmap
395 * DESCRIPTION: Delete a previously created mapping. Where and Length must
396 * correspond to a previous mapping exactly.
398 *****************************************************************************/
410 /******************************************************************************
412 * FUNCTION: AcpiOsAllocate
414 * PARAMETERS: Size - Amount to allocate, in bytes
416 * RETURN: Pointer to the new allocation. Null on error.
418 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
420 *****************************************************************************/
429 Mem = (void *) malloc ((size_t) size);
434 /******************************************************************************
436 * FUNCTION: AcpiOsFree
438 * PARAMETERS: mem - Pointer to previously allocated memory
442 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
444 *****************************************************************************/
455 #ifdef ACPI_SINGLE_THREADED
456 /******************************************************************************
458 * FUNCTION: Semaphore stub functions
460 * DESCRIPTION: Stub functions used for single-thread applications that do
461 * not require semaphore synchronization. Full implementations
462 * of these functions appear after the stubs.
464 *****************************************************************************/
467 AcpiOsCreateSemaphore (
470 ACPI_HANDLE *OutHandle)
472 *OutHandle = (ACPI_HANDLE) 1;
477 AcpiOsDeleteSemaphore (
484 AcpiOsWaitSemaphore (
493 AcpiOsSignalSemaphore (
501 /******************************************************************************
503 * FUNCTION: AcpiOsCreateSemaphore
505 * PARAMETERS: InitialUnits - Units to be assigned to the new semaphore
506 * OutHandle - Where a handle will be returned
510 * DESCRIPTION: Create an OS semaphore
512 *****************************************************************************/
515 AcpiOsCreateSemaphore (
518 ACPI_HANDLE *OutHandle)
525 return (AE_BAD_PARAMETER);
530 char *SemaphoreName = tmpnam (NULL);
532 Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
535 return (AE_NO_MEMORY);
537 sem_unlink (SemaphoreName); /* This just deletes the name */
541 Sem = AcpiOsAllocate (sizeof (sem_t));
544 return (AE_NO_MEMORY);
547 if (sem_init (Sem, 0, InitialUnits) == -1)
550 return (AE_BAD_PARAMETER);
554 *OutHandle = (ACPI_HANDLE) Sem;
559 /******************************************************************************
561 * FUNCTION: AcpiOsDeleteSemaphore
563 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
567 * DESCRIPTION: Delete an OS semaphore
569 *****************************************************************************/
572 AcpiOsDeleteSemaphore (
575 sem_t *Sem = (sem_t *) Handle;
580 return (AE_BAD_PARAMETER);
583 if (sem_destroy (Sem) == -1)
585 return (AE_BAD_PARAMETER);
592 /******************************************************************************
594 * FUNCTION: AcpiOsWaitSemaphore
596 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
597 * Units - How many units to wait for
598 * Timeout - How long to wait
602 * DESCRIPTION: Wait for units
604 *****************************************************************************/
607 AcpiOsWaitSemaphore (
612 ACPI_STATUS Status = AE_OK;
613 sem_t *Sem = (sem_t *) Handle;
619 return (AE_BAD_PARAMETER);
627 * A zero timeout value indicates that we shouldn't wait - just
628 * acquire the semaphore if available otherwise return AE_TIME
629 * (a.k.a. 'would block').
633 if (sem_trywait(Sem) == -1)
639 /* Wait Indefinitely */
641 case ACPI_WAIT_FOREVER:
649 /* Wait with Timeout */
653 T.tv_sec = Timeout / 1000;
654 T.tv_nsec = (Timeout - (T.tv_sec * 1000)) * 1000000;
656 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
658 * Alternate timeout mechanism for environments where
659 * sem_timedwait is not available or does not work properly.
663 if (sem_trywait (Sem) == 0)
665 /* Got the semaphore */
668 usleep (1000); /* one millisecond */
674 if (sem_timedwait (Sem, &T))
687 /******************************************************************************
689 * FUNCTION: AcpiOsSignalSemaphore
691 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
692 * Units - Number of units to send
696 * DESCRIPTION: Send units
698 *****************************************************************************/
701 AcpiOsSignalSemaphore (
705 sem_t *Sem = (sem_t *)Handle;
710 return (AE_BAD_PARAMETER);
713 if (sem_post (Sem) == -1)
721 #endif /* ACPI_SINGLE_THREADED */
724 /******************************************************************************
726 * FUNCTION: Spinlock interfaces
728 * DESCRIPTION: Map these interfaces to semaphore interfaces
730 *****************************************************************************/
734 ACPI_SPINLOCK *OutHandle)
737 return (AcpiOsCreateSemaphore (1, 1, OutHandle));
743 ACPI_SPINLOCK Handle)
745 AcpiOsDeleteSemaphore (Handle);
753 AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
760 ACPI_SPINLOCK Handle,
761 ACPI_CPU_FLAGS Flags)
763 AcpiOsSignalSemaphore (Handle, 1);
767 /******************************************************************************
769 * FUNCTION: AcpiOsInstallInterruptHandler
771 * PARAMETERS: InterruptNumber - Level handler should respond to.
772 * Isr - Address of the ACPI interrupt handler
773 * ExceptPtr - Where status is returned
775 * RETURN: Handle to the newly installed handler.
777 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
778 * OS-independent handler.
780 *****************************************************************************/
783 AcpiOsInstallInterruptHandler (
784 UINT32 InterruptNumber,
785 ACPI_OSD_HANDLER ServiceRoutine,
793 /******************************************************************************
795 * FUNCTION: AcpiOsRemoveInterruptHandler
797 * PARAMETERS: Handle - Returned when handler was installed
801 * DESCRIPTION: Uninstalls an interrupt handler.
803 *****************************************************************************/
806 AcpiOsRemoveInterruptHandler (
807 UINT32 InterruptNumber,
808 ACPI_OSD_HANDLER ServiceRoutine)
815 /******************************************************************************
817 * FUNCTION: AcpiOsStall
819 * PARAMETERS: microseconds - Time to sleep
821 * RETURN: Blocks until sleep is completed.
823 * DESCRIPTION: Sleep at microsecond granularity
825 *****************************************************************************/
834 usleep (microseconds);
839 /******************************************************************************
841 * FUNCTION: AcpiOsSleep
843 * PARAMETERS: milliseconds - Time to sleep
845 * RETURN: Blocks until sleep is completed.
847 * DESCRIPTION: Sleep at millisecond granularity
849 *****************************************************************************/
856 sleep (milliseconds / 1000); /* Sleep for whole seconds */
859 * Arg to usleep() must be less than 1,000,000 (1 second)
861 usleep ((milliseconds % 1000) * 1000); /* Sleep for remaining usecs */
865 /******************************************************************************
867 * FUNCTION: AcpiOsGetTimer
871 * RETURN: Current time in 100 nanosecond units
873 * DESCRIPTION: Get the current system time
875 *****************************************************************************/
884 gettimeofday (&time, NULL);
886 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
888 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
892 /******************************************************************************
894 * FUNCTION: AcpiOsReadPciConfiguration
896 * PARAMETERS: PciId - Seg/Bus/Dev
897 * Register - Device Register
898 * Value - Buffer where value is placed
899 * Width - Number of bits
903 * DESCRIPTION: Read data from PCI configuration space
905 *****************************************************************************/
908 AcpiOsReadPciConfiguration (
919 /******************************************************************************
921 * FUNCTION: AcpiOsWritePciConfiguration
923 * PARAMETERS: PciId - Seg/Bus/Dev
924 * Register - Device Register
925 * Value - Value to be written
926 * Width - Number of bits
930 * DESCRIPTION: Write data to PCI configuration space
932 *****************************************************************************/
935 AcpiOsWritePciConfiguration (
946 /******************************************************************************
948 * FUNCTION: AcpiOsReadPort
950 * PARAMETERS: Address - Address of I/O port/register to read
951 * Value - Where value is placed
952 * Width - Number of bits
954 * RETURN: Value read from port
956 * DESCRIPTION: Read data from an I/O port or register
958 *****************************************************************************/
962 ACPI_IO_ADDRESS Address,
982 return (AE_BAD_PARAMETER);
989 /******************************************************************************
991 * FUNCTION: AcpiOsWritePort
993 * PARAMETERS: Address - Address of I/O port/register to write
994 * Value - Value to write
995 * Width - Number of bits
999 * DESCRIPTION: Write data to an I/O port or register
1001 *****************************************************************************/
1005 ACPI_IO_ADDRESS Address,
1014 /******************************************************************************
1016 * FUNCTION: AcpiOsReadMemory
1018 * PARAMETERS: Address - Physical Memory Address to read
1019 * Value - Where value is placed
1020 * Width - Number of bits
1022 * RETURN: Value read from physical memory address
1024 * DESCRIPTION: Read data from a physical memory address
1026 *****************************************************************************/
1030 ACPI_PHYSICAL_ADDRESS Address,
1044 return (AE_BAD_PARAMETER);
1050 /******************************************************************************
1052 * FUNCTION: AcpiOsWriteMemory
1054 * PARAMETERS: Address - Physical Memory Address to write
1055 * Value - Value to write
1056 * Width - Number of bits
1060 * DESCRIPTION: Write data to a physical memory address
1062 *****************************************************************************/
1066 ACPI_PHYSICAL_ADDRESS Address,
1075 /******************************************************************************
1077 * FUNCTION: AcpiOsReadable
1079 * PARAMETERS: Pointer - Area to be verified
1080 * Length - Size of area
1082 * RETURN: TRUE if readable for entire length
1084 * DESCRIPTION: Verify that a pointer is valid for reading
1086 *****************************************************************************/
1098 /******************************************************************************
1100 * FUNCTION: AcpiOsWritable
1102 * PARAMETERS: Pointer - Area to be verified
1103 * Length - Size of area
1105 * RETURN: TRUE if writable for entire length
1107 * DESCRIPTION: Verify that a pointer is valid for writing
1109 *****************************************************************************/
1121 /******************************************************************************
1123 * FUNCTION: AcpiOsSignal
1125 * PARAMETERS: Function - ACPI CA signal function code
1126 * Info - Pointer to function-dependent structure
1130 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1132 *****************************************************************************/
1142 case ACPI_SIGNAL_FATAL:
1145 case ACPI_SIGNAL_BREAKPOINT:
1155 /* Optional multi-thread support */
1157 #ifndef ACPI_SINGLE_THREADED
1158 /******************************************************************************
1160 * FUNCTION: AcpiOsGetThreadId
1164 * RETURN: Id of the running thread
1166 * DESCRIPTION: Get the ID of the current (running) thread
1168 *****************************************************************************/
1177 thread = pthread_self();
1178 return (ACPI_CAST_PTHREAD_T (thread));
1182 /******************************************************************************
1184 * FUNCTION: AcpiOsExecute
1186 * PARAMETERS: Type - Type of execution
1187 * Function - Address of the function to execute
1188 * Context - Passed as a parameter to the function
1192 * DESCRIPTION: Execute a new thread
1194 *****************************************************************************/
1198 ACPI_EXECUTE_TYPE Type,
1199 ACPI_OSD_EXEC_CALLBACK Function,
1206 ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
1209 AcpiOsPrintf("Create thread failed");
1214 #endif /* ACPI_SINGLE_THREADED */