1 /******************************************************************************
3 * Module Name: osunixxf - UNIX OSL interfaces
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
118 * These interfaces are required in order to compile the ASL compiler and the
119 * various ACPICA tools under Linux or other Unix-like system.
121 * Note: Use #define __APPLE__ for OS X generation.
127 #include <sys/time.h>
128 #include <semaphore.h>
133 #include "accommon.h"
135 #include "acparser.h"
138 #define _COMPONENT ACPI_OS_SERVICES
139 ACPI_MODULE_NAME ("osunixxf")
142 extern FILE *AcpiGbl_DebugFile;
143 FILE *AcpiGbl_OutputFile;
146 /* Upcalls to AcpiExec */
148 ACPI_PHYSICAL_ADDRESS
149 AeLocalGetRootPointer (
154 ACPI_TABLE_HEADER *ExistingTable,
155 ACPI_TABLE_HEADER **NewTable);
157 typedef void* (*PTHREAD_CALLBACK) (void *);
162 #define sem_destroy sem_close
166 /******************************************************************************
168 * FUNCTION: AcpiOsInitialize, AcpiOsTerminate
174 * DESCRIPTION: Init and terminate. Nothing to do.
176 *****************************************************************************/
183 AcpiGbl_OutputFile = stdout;
197 /******************************************************************************
199 * FUNCTION: AcpiOsGetRootPointer
203 * RETURN: RSDP physical address
205 * DESCRIPTION: Gets the ACPI root pointer (RSDP)
207 *****************************************************************************/
209 ACPI_PHYSICAL_ADDRESS
210 AcpiOsGetRootPointer (
214 return (AeLocalGetRootPointer ());
218 /******************************************************************************
220 * FUNCTION: AcpiOsPredefinedOverride
222 * PARAMETERS: InitVal - Initial value of the predefined object
223 * NewVal - The new value for the object
225 * RETURN: Status, pointer to value. Null pointer returned if not
228 * DESCRIPTION: Allow the OS to override predefined names
230 *****************************************************************************/
233 AcpiOsPredefinedOverride (
234 const ACPI_PREDEFINED_NAMES *InitVal,
238 if (!InitVal || !NewVal)
240 return (AE_BAD_PARAMETER);
248 /******************************************************************************
250 * FUNCTION: AcpiOsTableOverride
252 * PARAMETERS: ExistingTable - Header of current table (probably
254 * NewTable - Where an entire new table is returned.
256 * RETURN: Status, pointer to new table. Null pointer returned if no
257 * table is available to override
259 * DESCRIPTION: Return a different version of a table if one is available
261 *****************************************************************************/
264 AcpiOsTableOverride (
265 ACPI_TABLE_HEADER *ExistingTable,
266 ACPI_TABLE_HEADER **NewTable)
269 if (!ExistingTable || !NewTable)
271 return (AE_BAD_PARAMETER);
278 AeTableOverride (ExistingTable, NewTable);
282 return (AE_NO_ACPI_TABLES);
287 /******************************************************************************
289 * FUNCTION: AcpiOsRedirectOutput
291 * PARAMETERS: Destination - An open file handle/pointer
295 * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
297 *****************************************************************************/
300 AcpiOsRedirectOutput (
304 AcpiGbl_OutputFile = Destination;
308 /******************************************************************************
310 * FUNCTION: AcpiOsPrintf
312 * PARAMETERS: fmt, ... - Standard printf format
316 * DESCRIPTION: Formatted output
318 *****************************************************************************/
320 void ACPI_INTERNAL_VAR_XFACE
328 va_start (Args, Fmt);
329 AcpiOsVprintf (Fmt, Args);
334 /******************************************************************************
336 * FUNCTION: AcpiOsVprintf
338 * PARAMETERS: fmt - Standard printf format
339 * args - Argument list
343 * DESCRIPTION: Formatted output with argument list pointer
345 *****************************************************************************/
356 Flags = AcpiGbl_DbOutputFlags;
357 if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
359 /* Output is directable to either a file (if open) or the console */
361 if (AcpiGbl_DebugFile)
363 /* Output file is open, send the output there */
365 Count = vfprintf (AcpiGbl_DebugFile, Fmt, Args);
369 /* No redirection, send output to console (once only!) */
371 Flags |= ACPI_DB_CONSOLE_OUTPUT;
375 if (Flags & ACPI_DB_CONSOLE_OUTPUT)
377 Count = vfprintf (AcpiGbl_OutputFile, Fmt, Args);
382 /******************************************************************************
384 * FUNCTION: AcpiOsGetLine
386 * PARAMETERS: fmt - Standard printf format
387 * args - Argument list
389 * RETURN: Actual bytes read
391 * DESCRIPTION: Formatted input with argument list pointer
393 *****************************************************************************/
405 scanf ("%1c", &Temp);
406 if (!Temp || Temp == '\n')
414 /* Null terminate the buffer */
418 /* Return the number of bytes in the string */
424 /******************************************************************************
426 * FUNCTION: AcpiOsMapMemory
428 * PARAMETERS: where - Physical address of memory to be mapped
429 * length - How much memory to map
431 * RETURN: Pointer to mapped memory. Null on error.
433 * DESCRIPTION: Map physical memory into caller's address space
435 *****************************************************************************/
439 ACPI_PHYSICAL_ADDRESS where,
443 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
447 /******************************************************************************
449 * FUNCTION: AcpiOsUnmapMemory
451 * PARAMETERS: where - Logical address of memory to be unmapped
452 * length - How much memory to unmap
456 * DESCRIPTION: Delete a previously created mapping. Where and Length must
457 * correspond to a previous mapping exactly.
459 *****************************************************************************/
471 /******************************************************************************
473 * FUNCTION: AcpiOsAllocate
475 * PARAMETERS: Size - Amount to allocate, in bytes
477 * RETURN: Pointer to the new allocation. Null on error.
479 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
481 *****************************************************************************/
490 Mem = (void *) malloc ((size_t) size);
495 /******************************************************************************
497 * FUNCTION: AcpiOsFree
499 * PARAMETERS: mem - Pointer to previously allocated memory
503 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
505 *****************************************************************************/
516 #ifdef ACPI_SINGLE_THREADED
517 /******************************************************************************
519 * FUNCTION: Semaphore stub functions
521 * DESCRIPTION: Stub functions used for single-thread applications that do
522 * not require semaphore synchronization. Full implementations
523 * of these functions appear after the stubs.
525 *****************************************************************************/
528 AcpiOsCreateSemaphore (
531 ACPI_HANDLE *OutHandle)
533 *OutHandle = (ACPI_HANDLE) 1;
538 AcpiOsDeleteSemaphore (
545 AcpiOsWaitSemaphore (
554 AcpiOsSignalSemaphore (
562 /******************************************************************************
564 * FUNCTION: AcpiOsCreateSemaphore
566 * PARAMETERS: InitialUnits - Units to be assigned to the new semaphore
567 * OutHandle - Where a handle will be returned
571 * DESCRIPTION: Create an OS semaphore
573 *****************************************************************************/
576 AcpiOsCreateSemaphore (
579 ACPI_HANDLE *OutHandle)
586 return (AE_BAD_PARAMETER);
591 char *SemaphoreName = tmpnam (NULL);
593 Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
596 return (AE_NO_MEMORY);
598 sem_unlink (SemaphoreName); /* This just deletes the name */
602 Sem = AcpiOsAllocate (sizeof (sem_t));
605 return (AE_NO_MEMORY);
608 if (sem_init (Sem, 0, InitialUnits) == -1)
611 return (AE_BAD_PARAMETER);
615 *OutHandle = (ACPI_HANDLE) Sem;
620 /******************************************************************************
622 * FUNCTION: AcpiOsDeleteSemaphore
624 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
628 * DESCRIPTION: Delete an OS semaphore
630 *****************************************************************************/
633 AcpiOsDeleteSemaphore (
636 sem_t *Sem = (sem_t *) Handle;
641 return (AE_BAD_PARAMETER);
644 if (sem_destroy (Sem) == -1)
646 return (AE_BAD_PARAMETER);
653 /******************************************************************************
655 * FUNCTION: AcpiOsWaitSemaphore
657 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
658 * Units - How many units to wait for
659 * Timeout - How long to wait
663 * DESCRIPTION: Wait for units
665 *****************************************************************************/
668 AcpiOsWaitSemaphore (
673 ACPI_STATUS Status = AE_OK;
674 sem_t *Sem = (sem_t *) Handle;
680 return (AE_BAD_PARAMETER);
688 * A zero timeout value indicates that we shouldn't wait - just
689 * acquire the semaphore if available otherwise return AE_TIME
690 * (a.k.a. 'would block').
694 if (sem_trywait(Sem) == -1)
700 /* Wait Indefinitely */
702 case ACPI_WAIT_FOREVER:
710 /* Wait with Timeout */
714 T.tv_sec = Timeout / 1000;
715 T.tv_nsec = (Timeout - (T.tv_sec * 1000)) * 1000000;
717 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
719 * Alternate timeout mechanism for environments where
720 * sem_timedwait is not available or does not work properly.
724 if (sem_trywait (Sem) == 0)
726 /* Got the semaphore */
729 usleep (1000); /* one millisecond */
735 if (sem_timedwait (Sem, &T))
748 /******************************************************************************
750 * FUNCTION: AcpiOsSignalSemaphore
752 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
753 * Units - Number of units to send
757 * DESCRIPTION: Send units
759 *****************************************************************************/
762 AcpiOsSignalSemaphore (
766 sem_t *Sem = (sem_t *)Handle;
771 return (AE_BAD_PARAMETER);
774 if (sem_post (Sem) == -1)
782 #endif /* ACPI_SINGLE_THREADED */
785 /******************************************************************************
787 * FUNCTION: Spinlock interfaces
789 * DESCRIPTION: Map these interfaces to semaphore interfaces
791 *****************************************************************************/
795 ACPI_SPINLOCK *OutHandle)
798 return (AcpiOsCreateSemaphore (1, 1, OutHandle));
804 ACPI_SPINLOCK Handle)
806 AcpiOsDeleteSemaphore (Handle);
814 AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
821 ACPI_SPINLOCK Handle,
822 ACPI_CPU_FLAGS Flags)
824 AcpiOsSignalSemaphore (Handle, 1);
828 /******************************************************************************
830 * FUNCTION: AcpiOsInstallInterruptHandler
832 * PARAMETERS: InterruptNumber - Level handler should respond to.
833 * Isr - Address of the ACPI interrupt handler
834 * ExceptPtr - Where status is returned
836 * RETURN: Handle to the newly installed handler.
838 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
839 * OS-independent handler.
841 *****************************************************************************/
844 AcpiOsInstallInterruptHandler (
845 UINT32 InterruptNumber,
846 ACPI_OSD_HANDLER ServiceRoutine,
854 /******************************************************************************
856 * FUNCTION: AcpiOsRemoveInterruptHandler
858 * PARAMETERS: Handle - Returned when handler was installed
862 * DESCRIPTION: Uninstalls an interrupt handler.
864 *****************************************************************************/
867 AcpiOsRemoveInterruptHandler (
868 UINT32 InterruptNumber,
869 ACPI_OSD_HANDLER ServiceRoutine)
876 /******************************************************************************
878 * FUNCTION: AcpiOsExecute
880 * PARAMETERS: Type - Type of execution
881 * Function - Address of the function to execute
882 * Context - Passed as a parameter to the function
886 * DESCRIPTION: Execute a new thread
888 *****************************************************************************/
892 ACPI_EXECUTE_TYPE Type,
893 ACPI_OSD_EXEC_CALLBACK Function,
900 ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
903 AcpiOsPrintf("Create thread failed");
909 /******************************************************************************
911 * FUNCTION: AcpiOsStall
913 * PARAMETERS: microseconds - Time to sleep
915 * RETURN: Blocks until sleep is completed.
917 * DESCRIPTION: Sleep at microsecond granularity
919 *****************************************************************************/
928 usleep (microseconds);
933 /******************************************************************************
935 * FUNCTION: AcpiOsSleep
937 * PARAMETERS: milliseconds - Time to sleep
939 * RETURN: Blocks until sleep is completed.
941 * DESCRIPTION: Sleep at millisecond granularity
943 *****************************************************************************/
950 sleep (milliseconds / 1000); /* Sleep for whole seconds */
953 * Arg to usleep() must be less than 1,000,000 (1 second)
955 usleep ((milliseconds % 1000) * 1000); /* Sleep for remaining usecs */
959 /******************************************************************************
961 * FUNCTION: AcpiOsGetTimer
965 * RETURN: Current time in 100 nanosecond units
967 * DESCRIPTION: Get the current system time
969 *****************************************************************************/
978 gettimeofday (&time, NULL);
980 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
982 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
986 /******************************************************************************
988 * FUNCTION: AcpiOsReadPciConfiguration
990 * PARAMETERS: PciId - Seg/Bus/Dev
991 * Register - Device Register
992 * Value - Buffer where value is placed
993 * Width - Number of bits
997 * DESCRIPTION: Read data from PCI configuration space
999 *****************************************************************************/
1002 AcpiOsReadPciConfiguration (
1013 /******************************************************************************
1015 * FUNCTION: AcpiOsWritePciConfiguration
1017 * PARAMETERS: PciId - Seg/Bus/Dev
1018 * Register - Device Register
1019 * Value - Value to be written
1020 * Width - Number of bits
1024 * DESCRIPTION: Write data to PCI configuration space
1026 *****************************************************************************/
1029 AcpiOsWritePciConfiguration (
1040 /******************************************************************************
1042 * FUNCTION: AcpiOsReadPort
1044 * PARAMETERS: Address - Address of I/O port/register to read
1045 * Value - Where value is placed
1046 * Width - Number of bits
1048 * RETURN: Value read from port
1050 * DESCRIPTION: Read data from an I/O port or register
1052 *****************************************************************************/
1056 ACPI_IO_ADDRESS Address,
1072 *Value = 0xFFFFFFFF;
1076 return (AE_BAD_PARAMETER);
1083 /******************************************************************************
1085 * FUNCTION: AcpiOsWritePort
1087 * PARAMETERS: Address - Address of I/O port/register to write
1088 * Value - Value to write
1089 * Width - Number of bits
1093 * DESCRIPTION: Write data to an I/O port or register
1095 *****************************************************************************/
1099 ACPI_IO_ADDRESS Address,
1108 /******************************************************************************
1110 * FUNCTION: AcpiOsReadMemory
1112 * PARAMETERS: Address - Physical Memory Address to read
1113 * Value - Where value is placed
1114 * Width - Number of bits
1116 * RETURN: Value read from physical memory address
1118 * DESCRIPTION: Read data from a physical memory address
1120 *****************************************************************************/
1124 ACPI_PHYSICAL_ADDRESS Address,
1138 return (AE_BAD_PARAMETER);
1144 /******************************************************************************
1146 * FUNCTION: AcpiOsWriteMemory
1148 * PARAMETERS: Address - Physical Memory Address to write
1149 * Value - Value to write
1150 * Width - Number of bits
1154 * DESCRIPTION: Write data to a physical memory address
1156 *****************************************************************************/
1160 ACPI_PHYSICAL_ADDRESS Address,
1169 /******************************************************************************
1171 * FUNCTION: AcpiOsReadable
1173 * PARAMETERS: Pointer - Area to be verified
1174 * Length - Size of area
1176 * RETURN: TRUE if readable for entire length
1178 * DESCRIPTION: Verify that a pointer is valid for reading
1180 *****************************************************************************/
1192 /******************************************************************************
1194 * FUNCTION: AcpiOsWritable
1196 * PARAMETERS: Pointer - Area to be verified
1197 * Length - Size of area
1199 * RETURN: TRUE if writable for entire length
1201 * DESCRIPTION: Verify that a pointer is valid for writing
1203 *****************************************************************************/
1215 /******************************************************************************
1217 * FUNCTION: AcpiOsGetThreadId
1221 * RETURN: Id of the running thread
1223 * DESCRIPTION: Get the ID of the current (running) thread
1225 *****************************************************************************/
1232 return (ACPI_CAST_PTHREAD_T (pthread_self()));
1236 /******************************************************************************
1238 * FUNCTION: AcpiOsSignal
1240 * PARAMETERS: Function - ACPI CA signal function code
1241 * Info - Pointer to function-dependent structure
1245 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1247 *****************************************************************************/
1257 case ACPI_SIGNAL_FATAL:
1260 case ACPI_SIGNAL_BREAKPOINT: