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.
56 #include <semaphore.h>
60 #include <contrib/dev/acpica/include/acpi.h>
61 #include <contrib/dev/acpica/include/accommon.h>
62 #include <contrib/dev/acpica/include/amlcode.h>
63 #include <contrib/dev/acpica/include/acparser.h>
64 #include <contrib/dev/acpica/include/acdebug.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 *****************************************************************************/
284 Flags = AcpiGbl_DbOutputFlags;
285 if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
287 /* Output is directable to either a file (if open) or the console */
289 if (AcpiGbl_DebugFile)
291 /* Output file is open, send the output there */
293 Count = vfprintf (AcpiGbl_DebugFile, Fmt, Args);
297 /* No redirection, send output to console (once only!) */
299 Flags |= ACPI_DB_CONSOLE_OUTPUT;
303 if (Flags & ACPI_DB_CONSOLE_OUTPUT)
305 Count = vfprintf (AcpiGbl_OutputFile, Fmt, Args);
310 /******************************************************************************
312 * FUNCTION: AcpiOsGetLine
314 * PARAMETERS: fmt - Standard printf format
315 * args - Argument list
317 * RETURN: Actual bytes read
319 * DESCRIPTION: Formatted input with argument list pointer
321 *****************************************************************************/
333 scanf ("%1c", &Temp);
334 if (!Temp || Temp == '\n')
342 /* Null terminate the buffer */
346 /* Return the number of bytes in the string */
352 /******************************************************************************
354 * FUNCTION: AcpiOsMapMemory
356 * PARAMETERS: where - Physical address of memory to be mapped
357 * length - How much memory to map
359 * RETURN: Pointer to mapped memory. Null on error.
361 * DESCRIPTION: Map physical memory into caller's address space
363 *****************************************************************************/
367 ACPI_PHYSICAL_ADDRESS where,
371 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
375 /******************************************************************************
377 * FUNCTION: AcpiOsUnmapMemory
379 * PARAMETERS: where - Logical address of memory to be unmapped
380 * length - How much memory to unmap
384 * DESCRIPTION: Delete a previously created mapping. Where and Length must
385 * correspond to a previous mapping exactly.
387 *****************************************************************************/
399 /******************************************************************************
401 * FUNCTION: AcpiOsAllocate
403 * PARAMETERS: Size - Amount to allocate, in bytes
405 * RETURN: Pointer to the new allocation. Null on error.
407 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
409 *****************************************************************************/
418 Mem = (void *) malloc ((size_t) size);
423 /******************************************************************************
425 * FUNCTION: AcpiOsFree
427 * PARAMETERS: mem - Pointer to previously allocated memory
431 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
433 *****************************************************************************/
444 #ifdef ACPI_SINGLE_THREADED
445 /******************************************************************************
447 * FUNCTION: Semaphore stub functions
449 * DESCRIPTION: Stub functions used for single-thread applications that do
450 * not require semaphore synchronization. Full implementations
451 * of these functions appear after the stubs.
453 *****************************************************************************/
456 AcpiOsCreateSemaphore (
459 ACPI_HANDLE *OutHandle)
461 *OutHandle = (ACPI_HANDLE) 1;
466 AcpiOsDeleteSemaphore (
473 AcpiOsWaitSemaphore (
482 AcpiOsSignalSemaphore (
490 /******************************************************************************
492 * FUNCTION: AcpiOsCreateSemaphore
494 * PARAMETERS: InitialUnits - Units to be assigned to the new semaphore
495 * OutHandle - Where a handle will be returned
499 * DESCRIPTION: Create an OS semaphore
501 *****************************************************************************/
504 AcpiOsCreateSemaphore (
507 ACPI_HANDLE *OutHandle)
514 return (AE_BAD_PARAMETER);
519 char *SemaphoreName = tmpnam (NULL);
521 Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
524 return (AE_NO_MEMORY);
526 sem_unlink (SemaphoreName); /* This just deletes the name */
530 Sem = AcpiOsAllocate (sizeof (sem_t));
533 return (AE_NO_MEMORY);
536 if (sem_init (Sem, 0, InitialUnits) == -1)
539 return (AE_BAD_PARAMETER);
543 *OutHandle = (ACPI_HANDLE) Sem;
548 /******************************************************************************
550 * FUNCTION: AcpiOsDeleteSemaphore
552 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
556 * DESCRIPTION: Delete an OS semaphore
558 *****************************************************************************/
561 AcpiOsDeleteSemaphore (
564 sem_t *Sem = (sem_t *) Handle;
569 return (AE_BAD_PARAMETER);
572 if (sem_destroy (Sem) == -1)
574 return (AE_BAD_PARAMETER);
581 /******************************************************************************
583 * FUNCTION: AcpiOsWaitSemaphore
585 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
586 * Units - How many units to wait for
587 * Timeout - How long to wait
591 * DESCRIPTION: Wait for units
593 *****************************************************************************/
596 AcpiOsWaitSemaphore (
601 ACPI_STATUS Status = AE_OK;
602 sem_t *Sem = (sem_t *) Handle;
608 return (AE_BAD_PARAMETER);
616 * A zero timeout value indicates that we shouldn't wait - just
617 * acquire the semaphore if available otherwise return AE_TIME
618 * (a.k.a. 'would block').
622 if (sem_trywait(Sem) == -1)
628 /* Wait Indefinitely */
630 case ACPI_WAIT_FOREVER:
638 /* Wait with Timeout */
642 T.tv_sec = Timeout / 1000;
643 T.tv_nsec = (Timeout - (T.tv_sec * 1000)) * 1000000;
645 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
647 * Alternate timeout mechanism for environments where
648 * sem_timedwait is not available or does not work properly.
652 if (sem_trywait (Sem) == 0)
654 /* Got the semaphore */
657 usleep (1000); /* one millisecond */
663 if (sem_timedwait (Sem, &T))
676 /******************************************************************************
678 * FUNCTION: AcpiOsSignalSemaphore
680 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
681 * Units - Number of units to send
685 * DESCRIPTION: Send units
687 *****************************************************************************/
690 AcpiOsSignalSemaphore (
694 sem_t *Sem = (sem_t *)Handle;
699 return (AE_BAD_PARAMETER);
702 if (sem_post (Sem) == -1)
710 #endif /* ACPI_SINGLE_THREADED */
713 /******************************************************************************
715 * FUNCTION: Spinlock interfaces
717 * DESCRIPTION: Map these interfaces to semaphore interfaces
719 *****************************************************************************/
723 ACPI_SPINLOCK *OutHandle)
726 return (AcpiOsCreateSemaphore (1, 1, OutHandle));
732 ACPI_SPINLOCK Handle)
734 AcpiOsDeleteSemaphore (Handle);
742 AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
749 ACPI_SPINLOCK Handle,
750 ACPI_CPU_FLAGS Flags)
752 AcpiOsSignalSemaphore (Handle, 1);
756 /******************************************************************************
758 * FUNCTION: AcpiOsInstallInterruptHandler
760 * PARAMETERS: InterruptNumber - Level handler should respond to.
761 * Isr - Address of the ACPI interrupt handler
762 * ExceptPtr - Where status is returned
764 * RETURN: Handle to the newly installed handler.
766 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
767 * OS-independent handler.
769 *****************************************************************************/
772 AcpiOsInstallInterruptHandler (
773 UINT32 InterruptNumber,
774 ACPI_OSD_HANDLER ServiceRoutine,
782 /******************************************************************************
784 * FUNCTION: AcpiOsRemoveInterruptHandler
786 * PARAMETERS: Handle - Returned when handler was installed
790 * DESCRIPTION: Uninstalls an interrupt handler.
792 *****************************************************************************/
795 AcpiOsRemoveInterruptHandler (
796 UINT32 InterruptNumber,
797 ACPI_OSD_HANDLER ServiceRoutine)
804 /******************************************************************************
806 * FUNCTION: AcpiOsExecute
808 * PARAMETERS: Type - Type of execution
809 * Function - Address of the function to execute
810 * Context - Passed as a parameter to the function
814 * DESCRIPTION: Execute a new thread
816 *****************************************************************************/
820 ACPI_EXECUTE_TYPE Type,
821 ACPI_OSD_EXEC_CALLBACK Function,
828 ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
831 AcpiOsPrintf("Create thread failed");
837 /******************************************************************************
839 * FUNCTION: AcpiOsStall
841 * PARAMETERS: microseconds - Time to sleep
843 * RETURN: Blocks until sleep is completed.
845 * DESCRIPTION: Sleep at microsecond granularity
847 *****************************************************************************/
856 usleep (microseconds);
861 /******************************************************************************
863 * FUNCTION: AcpiOsSleep
865 * PARAMETERS: milliseconds - Time to sleep
867 * RETURN: Blocks until sleep is completed.
869 * DESCRIPTION: Sleep at millisecond granularity
871 *****************************************************************************/
878 sleep (milliseconds / 1000); /* Sleep for whole seconds */
881 * Arg to usleep() must be less than 1,000,000 (1 second)
883 usleep ((milliseconds % 1000) * 1000); /* Sleep for remaining usecs */
887 /******************************************************************************
889 * FUNCTION: AcpiOsGetTimer
893 * RETURN: Current time in 100 nanosecond units
895 * DESCRIPTION: Get the current system time
897 *****************************************************************************/
906 gettimeofday (&time, NULL);
908 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
910 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
914 /******************************************************************************
916 * FUNCTION: AcpiOsReadPciConfiguration
918 * PARAMETERS: PciId - Seg/Bus/Dev
919 * Register - Device Register
920 * Value - Buffer where value is placed
921 * Width - Number of bits
925 * DESCRIPTION: Read data from PCI configuration space
927 *****************************************************************************/
930 AcpiOsReadPciConfiguration (
941 /******************************************************************************
943 * FUNCTION: AcpiOsWritePciConfiguration
945 * PARAMETERS: PciId - Seg/Bus/Dev
946 * Register - Device Register
947 * Value - Value to be written
948 * Width - Number of bits
952 * DESCRIPTION: Write data to PCI configuration space
954 *****************************************************************************/
957 AcpiOsWritePciConfiguration (
968 /******************************************************************************
970 * FUNCTION: AcpiOsReadPort
972 * PARAMETERS: Address - Address of I/O port/register to read
973 * Value - Where value is placed
974 * Width - Number of bits
976 * RETURN: Value read from port
978 * DESCRIPTION: Read data from an I/O port or register
980 *****************************************************************************/
984 ACPI_IO_ADDRESS Address,
1000 *Value = 0xFFFFFFFF;
1004 return (AE_BAD_PARAMETER);
1011 /******************************************************************************
1013 * FUNCTION: AcpiOsWritePort
1015 * PARAMETERS: Address - Address of I/O port/register to write
1016 * Value - Value to write
1017 * Width - Number of bits
1021 * DESCRIPTION: Write data to an I/O port or register
1023 *****************************************************************************/
1027 ACPI_IO_ADDRESS Address,
1036 /******************************************************************************
1038 * FUNCTION: AcpiOsReadMemory
1040 * PARAMETERS: Address - Physical Memory Address to read
1041 * Value - Where value is placed
1042 * Width - Number of bits
1044 * RETURN: Value read from physical memory address
1046 * DESCRIPTION: Read data from a physical memory address
1048 *****************************************************************************/
1052 ACPI_PHYSICAL_ADDRESS Address,
1066 return (AE_BAD_PARAMETER);
1072 /******************************************************************************
1074 * FUNCTION: AcpiOsWriteMemory
1076 * PARAMETERS: Address - Physical Memory Address to write
1077 * Value - Value to write
1078 * Width - Number of bits
1082 * DESCRIPTION: Write data to a physical memory address
1084 *****************************************************************************/
1088 ACPI_PHYSICAL_ADDRESS Address,
1097 /******************************************************************************
1099 * FUNCTION: AcpiOsReadable
1101 * PARAMETERS: Pointer - Area to be verified
1102 * Length - Size of area
1104 * RETURN: TRUE if readable for entire length
1106 * DESCRIPTION: Verify that a pointer is valid for reading
1108 *****************************************************************************/
1120 /******************************************************************************
1122 * FUNCTION: AcpiOsWritable
1124 * PARAMETERS: Pointer - Area to be verified
1125 * Length - Size of area
1127 * RETURN: TRUE if writable for entire length
1129 * DESCRIPTION: Verify that a pointer is valid for writing
1131 *****************************************************************************/
1143 /******************************************************************************
1145 * FUNCTION: AcpiOsGetThreadId
1149 * RETURN: Id of the running thread
1151 * DESCRIPTION: Get the ID of the current (running) thread
1153 *****************************************************************************/
1160 return (ACPI_CAST_PTHREAD_T (pthread_self()));
1164 /******************************************************************************
1166 * FUNCTION: AcpiOsSignal
1168 * PARAMETERS: Function - ACPI CA signal function code
1169 * Info - Pointer to function-dependent structure
1173 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1175 *****************************************************************************/
1185 case ACPI_SIGNAL_FATAL:
1188 case ACPI_SIGNAL_BREAKPOINT: