1 /******************************************************************************
3 * Module Name: osunixxf - UNIX OSL interfaces
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, 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: AcpiOsPhysicalTableOverride
219 * PARAMETERS: ExistingTable - Header of current table (probably firmware)
220 * NewAddress - Where new table address is returned
222 * NewTableLength - Where new table length is returned
224 * RETURN: Status, address/length of new table. Null pointer returned
225 * if no table is available to override.
227 * DESCRIPTION: Returns AE_SUPPORT, function not used in user space.
229 *****************************************************************************/
232 AcpiOsPhysicalTableOverride (
233 ACPI_TABLE_HEADER *ExistingTable,
234 ACPI_PHYSICAL_ADDRESS *NewAddress,
235 UINT32 *NewTableLength)
242 /******************************************************************************
244 * FUNCTION: AcpiOsRedirectOutput
246 * PARAMETERS: Destination - An open file handle/pointer
250 * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
252 *****************************************************************************/
255 AcpiOsRedirectOutput (
259 AcpiGbl_OutputFile = Destination;
263 /******************************************************************************
265 * FUNCTION: AcpiOsPrintf
267 * PARAMETERS: fmt, ... - Standard printf format
271 * DESCRIPTION: Formatted output
273 *****************************************************************************/
275 void ACPI_INTERNAL_VAR_XFACE
283 va_start (Args, Fmt);
284 AcpiOsVprintf (Fmt, Args);
289 /******************************************************************************
291 * FUNCTION: AcpiOsVprintf
293 * PARAMETERS: fmt - Standard printf format
294 * args - Argument list
298 * DESCRIPTION: Formatted output with argument list pointer
300 *****************************************************************************/
310 Flags = AcpiGbl_DbOutputFlags;
311 if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
313 /* Output is directable to either a file (if open) or the console */
315 if (AcpiGbl_DebugFile)
317 /* Output file is open, send the output there */
319 vfprintf (AcpiGbl_DebugFile, Fmt, Args);
323 /* No redirection, send output to console (once only!) */
325 Flags |= ACPI_DB_CONSOLE_OUTPUT;
329 if (Flags & ACPI_DB_CONSOLE_OUTPUT)
331 vfprintf (AcpiGbl_OutputFile, Fmt, Args);
336 /******************************************************************************
338 * FUNCTION: AcpiOsGetLine
340 * PARAMETERS: Buffer - Where to return the command line
341 * BufferLength - Maximum length of Buffer
342 * BytesRead - Where the actual byte count is returned
344 * RETURN: Status and actual bytes read
346 * DESCRIPTION: Formatted input with argument list pointer
348 *****************************************************************************/
362 if (i >= BufferLength)
364 return (AE_BUFFER_OVERFLOW);
367 if ((Temp = getchar ()) == EOF)
372 if (!Temp || Temp == '\n')
377 Buffer [i] = (char) Temp;
380 /* Null terminate the buffer */
384 /* Return the number of bytes in the string */
394 /******************************************************************************
396 * FUNCTION: AcpiOsMapMemory
398 * PARAMETERS: where - Physical address of memory to be mapped
399 * length - How much memory to map
401 * RETURN: Pointer to mapped memory. Null on error.
403 * DESCRIPTION: Map physical memory into caller's address space
405 *****************************************************************************/
409 ACPI_PHYSICAL_ADDRESS where,
413 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
417 /******************************************************************************
419 * FUNCTION: AcpiOsUnmapMemory
421 * PARAMETERS: where - Logical address of memory to be unmapped
422 * length - How much memory to unmap
426 * DESCRIPTION: Delete a previously created mapping. Where and Length must
427 * correspond to a previous mapping exactly.
429 *****************************************************************************/
441 /******************************************************************************
443 * FUNCTION: AcpiOsAllocate
445 * PARAMETERS: Size - Amount to allocate, in bytes
447 * RETURN: Pointer to the new allocation. Null on error.
449 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
451 *****************************************************************************/
460 Mem = (void *) malloc ((size_t) size);
465 /******************************************************************************
467 * FUNCTION: AcpiOsFree
469 * PARAMETERS: mem - Pointer to previously allocated memory
473 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
475 *****************************************************************************/
486 #ifdef ACPI_SINGLE_THREADED
487 /******************************************************************************
489 * FUNCTION: Semaphore stub functions
491 * DESCRIPTION: Stub functions used for single-thread applications that do
492 * not require semaphore synchronization. Full implementations
493 * of these functions appear after the stubs.
495 *****************************************************************************/
498 AcpiOsCreateSemaphore (
501 ACPI_HANDLE *OutHandle)
503 *OutHandle = (ACPI_HANDLE) 1;
508 AcpiOsDeleteSemaphore (
515 AcpiOsWaitSemaphore (
524 AcpiOsSignalSemaphore (
532 /******************************************************************************
534 * FUNCTION: AcpiOsCreateSemaphore
536 * PARAMETERS: InitialUnits - Units to be assigned to the new semaphore
537 * OutHandle - Where a handle will be returned
541 * DESCRIPTION: Create an OS semaphore
543 *****************************************************************************/
546 AcpiOsCreateSemaphore (
549 ACPI_HANDLE *OutHandle)
556 return (AE_BAD_PARAMETER);
561 char *SemaphoreName = tmpnam (NULL);
563 Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
566 return (AE_NO_MEMORY);
568 sem_unlink (SemaphoreName); /* This just deletes the name */
572 Sem = AcpiOsAllocate (sizeof (sem_t));
575 return (AE_NO_MEMORY);
578 if (sem_init (Sem, 0, InitialUnits) == -1)
581 return (AE_BAD_PARAMETER);
585 *OutHandle = (ACPI_HANDLE) Sem;
590 /******************************************************************************
592 * FUNCTION: AcpiOsDeleteSemaphore
594 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
598 * DESCRIPTION: Delete an OS semaphore
600 *****************************************************************************/
603 AcpiOsDeleteSemaphore (
606 sem_t *Sem = (sem_t *) Handle;
611 return (AE_BAD_PARAMETER);
614 if (sem_destroy (Sem) == -1)
616 return (AE_BAD_PARAMETER);
623 /******************************************************************************
625 * FUNCTION: AcpiOsWaitSemaphore
627 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
628 * Units - How many units to wait for
629 * Timeout - How long to wait
633 * DESCRIPTION: Wait for units
635 *****************************************************************************/
638 AcpiOsWaitSemaphore (
643 ACPI_STATUS Status = AE_OK;
644 sem_t *Sem = (sem_t *) Handle;
650 return (AE_BAD_PARAMETER);
658 * A zero timeout value indicates that we shouldn't wait - just
659 * acquire the semaphore if available otherwise return AE_TIME
660 * (a.k.a. 'would block').
664 if (sem_trywait(Sem) == -1)
670 /* Wait Indefinitely */
672 case ACPI_WAIT_FOREVER:
680 /* Wait with Timeout */
684 T.tv_sec = Timeout / 1000;
685 T.tv_nsec = (Timeout - (T.tv_sec * 1000)) * 1000000;
687 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
689 * Alternate timeout mechanism for environments where
690 * sem_timedwait is not available or does not work properly.
694 if (sem_trywait (Sem) == 0)
696 /* Got the semaphore */
699 usleep (1000); /* one millisecond */
705 if (sem_timedwait (Sem, &T))
718 /******************************************************************************
720 * FUNCTION: AcpiOsSignalSemaphore
722 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
723 * Units - Number of units to send
727 * DESCRIPTION: Send units
729 *****************************************************************************/
732 AcpiOsSignalSemaphore (
736 sem_t *Sem = (sem_t *)Handle;
741 return (AE_BAD_PARAMETER);
744 if (sem_post (Sem) == -1)
752 #endif /* ACPI_SINGLE_THREADED */
755 /******************************************************************************
757 * FUNCTION: Spinlock interfaces
759 * DESCRIPTION: Map these interfaces to semaphore interfaces
761 *****************************************************************************/
765 ACPI_SPINLOCK *OutHandle)
768 return (AcpiOsCreateSemaphore (1, 1, OutHandle));
774 ACPI_SPINLOCK Handle)
776 AcpiOsDeleteSemaphore (Handle);
784 AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
791 ACPI_SPINLOCK Handle,
792 ACPI_CPU_FLAGS Flags)
794 AcpiOsSignalSemaphore (Handle, 1);
798 /******************************************************************************
800 * FUNCTION: AcpiOsInstallInterruptHandler
802 * PARAMETERS: InterruptNumber - Level handler should respond to.
803 * Isr - Address of the ACPI interrupt handler
804 * ExceptPtr - Where status is returned
806 * RETURN: Handle to the newly installed handler.
808 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
809 * OS-independent handler.
811 *****************************************************************************/
814 AcpiOsInstallInterruptHandler (
815 UINT32 InterruptNumber,
816 ACPI_OSD_HANDLER ServiceRoutine,
824 /******************************************************************************
826 * FUNCTION: AcpiOsRemoveInterruptHandler
828 * PARAMETERS: Handle - Returned when handler was installed
832 * DESCRIPTION: Uninstalls an interrupt handler.
834 *****************************************************************************/
837 AcpiOsRemoveInterruptHandler (
838 UINT32 InterruptNumber,
839 ACPI_OSD_HANDLER ServiceRoutine)
846 /******************************************************************************
848 * FUNCTION: AcpiOsStall
850 * PARAMETERS: microseconds - Time to sleep
852 * RETURN: Blocks until sleep is completed.
854 * DESCRIPTION: Sleep at microsecond granularity
856 *****************************************************************************/
865 usleep (microseconds);
870 /******************************************************************************
872 * FUNCTION: AcpiOsSleep
874 * PARAMETERS: milliseconds - Time to sleep
876 * RETURN: Blocks until sleep is completed.
878 * DESCRIPTION: Sleep at millisecond granularity
880 *****************************************************************************/
887 sleep (milliseconds / 1000); /* Sleep for whole seconds */
890 * Arg to usleep() must be less than 1,000,000 (1 second)
892 usleep ((milliseconds % 1000) * 1000); /* Sleep for remaining usecs */
896 /******************************************************************************
898 * FUNCTION: AcpiOsGetTimer
902 * RETURN: Current time in 100 nanosecond units
904 * DESCRIPTION: Get the current system time
906 *****************************************************************************/
915 gettimeofday (&time, NULL);
917 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
919 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
923 /******************************************************************************
925 * FUNCTION: AcpiOsReadPciConfiguration
927 * PARAMETERS: PciId - Seg/Bus/Dev
928 * Register - Device Register
929 * Value - Buffer where value is placed
930 * Width - Number of bits
934 * DESCRIPTION: Read data from PCI configuration space
936 *****************************************************************************/
939 AcpiOsReadPciConfiguration (
950 /******************************************************************************
952 * FUNCTION: AcpiOsWritePciConfiguration
954 * PARAMETERS: PciId - Seg/Bus/Dev
955 * Register - Device Register
956 * Value - Value to be written
957 * Width - Number of bits
961 * DESCRIPTION: Write data to PCI configuration space
963 *****************************************************************************/
966 AcpiOsWritePciConfiguration (
977 /******************************************************************************
979 * FUNCTION: AcpiOsReadPort
981 * PARAMETERS: Address - Address of I/O port/register to read
982 * Value - Where value is placed
983 * Width - Number of bits
985 * RETURN: Value read from port
987 * DESCRIPTION: Read data from an I/O port or register
989 *****************************************************************************/
993 ACPI_IO_ADDRESS Address,
1009 *Value = 0xFFFFFFFF;
1013 return (AE_BAD_PARAMETER);
1020 /******************************************************************************
1022 * FUNCTION: AcpiOsWritePort
1024 * PARAMETERS: Address - Address of I/O port/register to write
1025 * Value - Value to write
1026 * Width - Number of bits
1030 * DESCRIPTION: Write data to an I/O port or register
1032 *****************************************************************************/
1036 ACPI_IO_ADDRESS Address,
1045 /******************************************************************************
1047 * FUNCTION: AcpiOsReadMemory
1049 * PARAMETERS: Address - Physical Memory Address to read
1050 * Value - Where value is placed
1051 * Width - Number of bits (8,16,32, or 64)
1053 * RETURN: Value read from physical memory address. Always returned
1054 * as a 64-bit integer, regardless of the read width.
1056 * DESCRIPTION: Read data from a physical memory address
1058 *****************************************************************************/
1062 ACPI_PHYSICAL_ADDRESS Address,
1077 return (AE_BAD_PARAMETER);
1083 /******************************************************************************
1085 * FUNCTION: AcpiOsWriteMemory
1087 * PARAMETERS: Address - Physical Memory Address to write
1088 * Value - Value to write
1089 * Width - Number of bits (8,16,32, or 64)
1093 * DESCRIPTION: Write data to a physical memory address
1095 *****************************************************************************/
1099 ACPI_PHYSICAL_ADDRESS Address,
1108 /******************************************************************************
1110 * FUNCTION: AcpiOsReadable
1112 * PARAMETERS: Pointer - Area to be verified
1113 * Length - Size of area
1115 * RETURN: TRUE if readable for entire length
1117 * DESCRIPTION: Verify that a pointer is valid for reading
1119 *****************************************************************************/
1131 /******************************************************************************
1133 * FUNCTION: AcpiOsWritable
1135 * PARAMETERS: Pointer - Area to be verified
1136 * Length - Size of area
1138 * RETURN: TRUE if writable for entire length
1140 * DESCRIPTION: Verify that a pointer is valid for writing
1142 *****************************************************************************/
1154 /******************************************************************************
1156 * FUNCTION: AcpiOsSignal
1158 * PARAMETERS: Function - ACPI CA signal function code
1159 * Info - Pointer to function-dependent structure
1163 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1165 *****************************************************************************/
1175 case ACPI_SIGNAL_FATAL:
1178 case ACPI_SIGNAL_BREAKPOINT:
1188 /* Optional multi-thread support */
1190 #ifndef ACPI_SINGLE_THREADED
1191 /******************************************************************************
1193 * FUNCTION: AcpiOsGetThreadId
1197 * RETURN: Id of the running thread
1199 * DESCRIPTION: Get the ID of the current (running) thread
1201 *****************************************************************************/
1210 thread = pthread_self();
1211 return (ACPI_CAST_PTHREAD_T (thread));
1215 /******************************************************************************
1217 * FUNCTION: AcpiOsExecute
1219 * PARAMETERS: Type - Type of execution
1220 * Function - Address of the function to execute
1221 * Context - Passed as a parameter to the function
1225 * DESCRIPTION: Execute a new thread
1227 *****************************************************************************/
1231 ACPI_EXECUTE_TYPE Type,
1232 ACPI_OSD_EXEC_CALLBACK Function,
1239 ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
1242 AcpiOsPrintf("Create thread failed");
1247 #endif /* ACPI_SINGLE_THREADED */
1250 /******************************************************************************
1252 * FUNCTION: AcpiOsWaitEventsComplete
1258 * DESCRIPTION: Wait for all asynchronous events to complete. This
1259 * implementation does nothing.
1261 *****************************************************************************/
1264 AcpiOsWaitEventsComplete (