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: Buffer - Where to return the command line
315 * BufferLength - Maximum length of Buffer
316 * BytesRead - Where the actual byte count is returned
318 * RETURN: Status and actual bytes read
320 * DESCRIPTION: Formatted input with argument list pointer
322 *****************************************************************************/
336 if (i >= BufferLength)
338 return (AE_BUFFER_OVERFLOW);
341 scanf ("%1c", &Temp);
342 if (!Temp || Temp == '\n')
350 /* Null terminate the buffer */
354 /* Return the number of bytes in the string */
364 /******************************************************************************
366 * FUNCTION: AcpiOsMapMemory
368 * PARAMETERS: where - Physical address of memory to be mapped
369 * length - How much memory to map
371 * RETURN: Pointer to mapped memory. Null on error.
373 * DESCRIPTION: Map physical memory into caller's address space
375 *****************************************************************************/
379 ACPI_PHYSICAL_ADDRESS where,
383 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
387 /******************************************************************************
389 * FUNCTION: AcpiOsUnmapMemory
391 * PARAMETERS: where - Logical address of memory to be unmapped
392 * length - How much memory to unmap
396 * DESCRIPTION: Delete a previously created mapping. Where and Length must
397 * correspond to a previous mapping exactly.
399 *****************************************************************************/
411 /******************************************************************************
413 * FUNCTION: AcpiOsAllocate
415 * PARAMETERS: Size - Amount to allocate, in bytes
417 * RETURN: Pointer to the new allocation. Null on error.
419 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
421 *****************************************************************************/
430 Mem = (void *) malloc ((size_t) size);
435 /******************************************************************************
437 * FUNCTION: AcpiOsFree
439 * PARAMETERS: mem - Pointer to previously allocated memory
443 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
445 *****************************************************************************/
456 #ifdef ACPI_SINGLE_THREADED
457 /******************************************************************************
459 * FUNCTION: Semaphore stub functions
461 * DESCRIPTION: Stub functions used for single-thread applications that do
462 * not require semaphore synchronization. Full implementations
463 * of these functions appear after the stubs.
465 *****************************************************************************/
468 AcpiOsCreateSemaphore (
471 ACPI_HANDLE *OutHandle)
473 *OutHandle = (ACPI_HANDLE) 1;
478 AcpiOsDeleteSemaphore (
485 AcpiOsWaitSemaphore (
494 AcpiOsSignalSemaphore (
502 /******************************************************************************
504 * FUNCTION: AcpiOsCreateSemaphore
506 * PARAMETERS: InitialUnits - Units to be assigned to the new semaphore
507 * OutHandle - Where a handle will be returned
511 * DESCRIPTION: Create an OS semaphore
513 *****************************************************************************/
516 AcpiOsCreateSemaphore (
519 ACPI_HANDLE *OutHandle)
526 return (AE_BAD_PARAMETER);
531 char *SemaphoreName = tmpnam (NULL);
533 Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
536 return (AE_NO_MEMORY);
538 sem_unlink (SemaphoreName); /* This just deletes the name */
542 Sem = AcpiOsAllocate (sizeof (sem_t));
545 return (AE_NO_MEMORY);
548 if (sem_init (Sem, 0, InitialUnits) == -1)
551 return (AE_BAD_PARAMETER);
555 *OutHandle = (ACPI_HANDLE) Sem;
560 /******************************************************************************
562 * FUNCTION: AcpiOsDeleteSemaphore
564 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
568 * DESCRIPTION: Delete an OS semaphore
570 *****************************************************************************/
573 AcpiOsDeleteSemaphore (
576 sem_t *Sem = (sem_t *) Handle;
581 return (AE_BAD_PARAMETER);
584 if (sem_destroy (Sem) == -1)
586 return (AE_BAD_PARAMETER);
593 /******************************************************************************
595 * FUNCTION: AcpiOsWaitSemaphore
597 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
598 * Units - How many units to wait for
599 * Timeout - How long to wait
603 * DESCRIPTION: Wait for units
605 *****************************************************************************/
608 AcpiOsWaitSemaphore (
613 ACPI_STATUS Status = AE_OK;
614 sem_t *Sem = (sem_t *) Handle;
620 return (AE_BAD_PARAMETER);
628 * A zero timeout value indicates that we shouldn't wait - just
629 * acquire the semaphore if available otherwise return AE_TIME
630 * (a.k.a. 'would block').
634 if (sem_trywait(Sem) == -1)
640 /* Wait Indefinitely */
642 case ACPI_WAIT_FOREVER:
650 /* Wait with Timeout */
654 T.tv_sec = Timeout / 1000;
655 T.tv_nsec = (Timeout - (T.tv_sec * 1000)) * 1000000;
657 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
659 * Alternate timeout mechanism for environments where
660 * sem_timedwait is not available or does not work properly.
664 if (sem_trywait (Sem) == 0)
666 /* Got the semaphore */
669 usleep (1000); /* one millisecond */
675 if (sem_timedwait (Sem, &T))
688 /******************************************************************************
690 * FUNCTION: AcpiOsSignalSemaphore
692 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
693 * Units - Number of units to send
697 * DESCRIPTION: Send units
699 *****************************************************************************/
702 AcpiOsSignalSemaphore (
706 sem_t *Sem = (sem_t *)Handle;
711 return (AE_BAD_PARAMETER);
714 if (sem_post (Sem) == -1)
722 #endif /* ACPI_SINGLE_THREADED */
725 /******************************************************************************
727 * FUNCTION: Spinlock interfaces
729 * DESCRIPTION: Map these interfaces to semaphore interfaces
731 *****************************************************************************/
735 ACPI_SPINLOCK *OutHandle)
738 return (AcpiOsCreateSemaphore (1, 1, OutHandle));
744 ACPI_SPINLOCK Handle)
746 AcpiOsDeleteSemaphore (Handle);
754 AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
761 ACPI_SPINLOCK Handle,
762 ACPI_CPU_FLAGS Flags)
764 AcpiOsSignalSemaphore (Handle, 1);
768 /******************************************************************************
770 * FUNCTION: AcpiOsInstallInterruptHandler
772 * PARAMETERS: InterruptNumber - Level handler should respond to.
773 * Isr - Address of the ACPI interrupt handler
774 * ExceptPtr - Where status is returned
776 * RETURN: Handle to the newly installed handler.
778 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
779 * OS-independent handler.
781 *****************************************************************************/
784 AcpiOsInstallInterruptHandler (
785 UINT32 InterruptNumber,
786 ACPI_OSD_HANDLER ServiceRoutine,
794 /******************************************************************************
796 * FUNCTION: AcpiOsRemoveInterruptHandler
798 * PARAMETERS: Handle - Returned when handler was installed
802 * DESCRIPTION: Uninstalls an interrupt handler.
804 *****************************************************************************/
807 AcpiOsRemoveInterruptHandler (
808 UINT32 InterruptNumber,
809 ACPI_OSD_HANDLER ServiceRoutine)
816 /******************************************************************************
818 * FUNCTION: AcpiOsExecute
820 * PARAMETERS: Type - Type of execution
821 * Function - Address of the function to execute
822 * Context - Passed as a parameter to the function
826 * DESCRIPTION: Execute a new thread
828 *****************************************************************************/
832 ACPI_EXECUTE_TYPE Type,
833 ACPI_OSD_EXEC_CALLBACK Function,
840 ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
843 AcpiOsPrintf("Create thread failed");
849 /******************************************************************************
851 * FUNCTION: AcpiOsStall
853 * PARAMETERS: microseconds - Time to sleep
855 * RETURN: Blocks until sleep is completed.
857 * DESCRIPTION: Sleep at microsecond granularity
859 *****************************************************************************/
868 usleep (microseconds);
873 /******************************************************************************
875 * FUNCTION: AcpiOsSleep
877 * PARAMETERS: milliseconds - Time to sleep
879 * RETURN: Blocks until sleep is completed.
881 * DESCRIPTION: Sleep at millisecond granularity
883 *****************************************************************************/
890 sleep (milliseconds / 1000); /* Sleep for whole seconds */
893 * Arg to usleep() must be less than 1,000,000 (1 second)
895 usleep ((milliseconds % 1000) * 1000); /* Sleep for remaining usecs */
899 /******************************************************************************
901 * FUNCTION: AcpiOsGetTimer
905 * RETURN: Current time in 100 nanosecond units
907 * DESCRIPTION: Get the current system time
909 *****************************************************************************/
918 gettimeofday (&time, NULL);
920 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
922 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
926 /******************************************************************************
928 * FUNCTION: AcpiOsReadPciConfiguration
930 * PARAMETERS: PciId - Seg/Bus/Dev
931 * Register - Device Register
932 * Value - Buffer where value is placed
933 * Width - Number of bits
937 * DESCRIPTION: Read data from PCI configuration space
939 *****************************************************************************/
942 AcpiOsReadPciConfiguration (
953 /******************************************************************************
955 * FUNCTION: AcpiOsWritePciConfiguration
957 * PARAMETERS: PciId - Seg/Bus/Dev
958 * Register - Device Register
959 * Value - Value to be written
960 * Width - Number of bits
964 * DESCRIPTION: Write data to PCI configuration space
966 *****************************************************************************/
969 AcpiOsWritePciConfiguration (
980 /******************************************************************************
982 * FUNCTION: AcpiOsReadPort
984 * PARAMETERS: Address - Address of I/O port/register to read
985 * Value - Where value is placed
986 * Width - Number of bits
988 * RETURN: Value read from port
990 * DESCRIPTION: Read data from an I/O port or register
992 *****************************************************************************/
996 ACPI_IO_ADDRESS Address,
1012 *Value = 0xFFFFFFFF;
1016 return (AE_BAD_PARAMETER);
1023 /******************************************************************************
1025 * FUNCTION: AcpiOsWritePort
1027 * PARAMETERS: Address - Address of I/O port/register to write
1028 * Value - Value to write
1029 * Width - Number of bits
1033 * DESCRIPTION: Write data to an I/O port or register
1035 *****************************************************************************/
1039 ACPI_IO_ADDRESS Address,
1048 /******************************************************************************
1050 * FUNCTION: AcpiOsReadMemory
1052 * PARAMETERS: Address - Physical Memory Address to read
1053 * Value - Where value is placed
1054 * Width - Number of bits
1056 * RETURN: Value read from physical memory address
1058 * DESCRIPTION: Read data from a physical memory address
1060 *****************************************************************************/
1064 ACPI_PHYSICAL_ADDRESS Address,
1078 return (AE_BAD_PARAMETER);
1084 /******************************************************************************
1086 * FUNCTION: AcpiOsWriteMemory
1088 * PARAMETERS: Address - Physical Memory Address to write
1089 * Value - Value to write
1090 * Width - Number of bits
1094 * DESCRIPTION: Write data to a physical memory address
1096 *****************************************************************************/
1100 ACPI_PHYSICAL_ADDRESS Address,
1109 /******************************************************************************
1111 * FUNCTION: AcpiOsReadable
1113 * PARAMETERS: Pointer - Area to be verified
1114 * Length - Size of area
1116 * RETURN: TRUE if readable for entire length
1118 * DESCRIPTION: Verify that a pointer is valid for reading
1120 *****************************************************************************/
1132 /******************************************************************************
1134 * FUNCTION: AcpiOsWritable
1136 * PARAMETERS: Pointer - Area to be verified
1137 * Length - Size of area
1139 * RETURN: TRUE if writable for entire length
1141 * DESCRIPTION: Verify that a pointer is valid for writing
1143 *****************************************************************************/
1155 /******************************************************************************
1157 * FUNCTION: AcpiOsGetThreadId
1161 * RETURN: Id of the running thread
1163 * DESCRIPTION: Get the ID of the current (running) thread
1165 *****************************************************************************/
1172 return (ACPI_CAST_PTHREAD_T (pthread_self()));
1176 /******************************************************************************
1178 * FUNCTION: AcpiOsSignal
1180 * PARAMETERS: Function - ACPI CA signal function code
1181 * Info - Pointer to function-dependent structure
1185 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1187 *****************************************************************************/
1197 case ACPI_SIGNAL_FATAL:
1200 case ACPI_SIGNAL_BREAKPOINT: