1 /******************************************************************************
3 * Module Name: osunixxf - UNIX OSL interfaces
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2010, 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 under
119 * Linux or other Unix-like system.
121 * Note: Use #define __APPLE__ for OS X generation.
128 #include <sys/time.h>
129 #include <semaphore.h>
133 #include <contrib/dev/acpica/include/acpi.h>
134 #include <contrib/dev/acpica/include/accommon.h>
135 #include <contrib/dev/acpica/include/amlcode.h>
136 #include <contrib/dev/acpica/include/acparser.h>
137 #include <contrib/dev/acpica/include/acdebug.h>
139 #define _COMPONENT ACPI_OS_SERVICES
140 ACPI_MODULE_NAME ("osunixxf")
143 extern FILE *AcpiGbl_DebugFile;
144 FILE *AcpiGbl_OutputFile;
147 /* Upcalls to AcpiExec */
149 ACPI_PHYSICAL_ADDRESS
150 AeLocalGetRootPointer (
155 ACPI_TABLE_HEADER *ExistingTable,
156 ACPI_TABLE_HEADER **NewTable);
158 typedef void* (*PTHREAD_CALLBACK) (void *);
161 /******************************************************************************
163 * FUNCTION: AcpiOsInitialize, AcpiOsTerminate
169 * DESCRIPTION: Init and terminate. Nothing to do.
171 *****************************************************************************/
174 AcpiOsInitialize (void)
177 AcpiGbl_OutputFile = stdout;
183 AcpiOsTerminate (void)
190 /******************************************************************************
192 * FUNCTION: AcpiOsGetRootPointer
196 * RETURN: RSDP physical address
198 * DESCRIPTION: Gets the root pointer (RSDP)
200 *****************************************************************************/
202 ACPI_PHYSICAL_ADDRESS
203 AcpiOsGetRootPointer (
207 return (AeLocalGetRootPointer ());
211 /******************************************************************************
213 * FUNCTION: AcpiOsPredefinedOverride
215 * PARAMETERS: InitVal - Initial value of the predefined object
216 * NewVal - The new value for the object
218 * RETURN: Status, pointer to value. Null pointer returned if not
221 * DESCRIPTION: Allow the OS to override predefined names
223 *****************************************************************************/
226 AcpiOsPredefinedOverride (
227 const ACPI_PREDEFINED_NAMES *InitVal,
231 if (!InitVal || !NewVal)
233 return (AE_BAD_PARAMETER);
241 /******************************************************************************
243 * FUNCTION: AcpiOsTableOverride
245 * PARAMETERS: ExistingTable - Header of current table (probably firmware)
246 * NewTable - Where an entire new table is returned.
248 * RETURN: Status, pointer to new table. Null pointer returned if no
249 * table is available to override
251 * DESCRIPTION: Return a different version of a table if one is available
253 *****************************************************************************/
256 AcpiOsTableOverride (
257 ACPI_TABLE_HEADER *ExistingTable,
258 ACPI_TABLE_HEADER **NewTable)
261 if (!ExistingTable || !NewTable)
263 return (AE_BAD_PARAMETER);
270 AeTableOverride (ExistingTable, NewTable);
274 return (AE_NO_ACPI_TABLES);
279 /******************************************************************************
281 * FUNCTION: AcpiOsRedirectOutput
283 * PARAMETERS: Destination - An open file handle/pointer
287 * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
289 *****************************************************************************/
292 AcpiOsRedirectOutput (
296 AcpiGbl_OutputFile = Destination;
300 /******************************************************************************
302 * FUNCTION: AcpiOsPrintf
304 * PARAMETERS: fmt, ... Standard printf format
308 * DESCRIPTION: Formatted output
310 *****************************************************************************/
312 void ACPI_INTERNAL_VAR_XFACE
320 va_start (Args, Fmt);
321 AcpiOsVprintf (Fmt, Args);
326 /******************************************************************************
328 * FUNCTION: AcpiOsVprintf
330 * PARAMETERS: fmt Standard printf format
335 * DESCRIPTION: Formatted output with argument list pointer
337 *****************************************************************************/
348 Flags = AcpiGbl_DbOutputFlags;
349 if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
351 /* Output is directable to either a file (if open) or the console */
353 if (AcpiGbl_DebugFile)
355 /* Output file is open, send the output there */
357 Count = vfprintf (AcpiGbl_DebugFile, Fmt, Args);
361 /* No redirection, send output to console (once only!) */
363 Flags |= ACPI_DB_CONSOLE_OUTPUT;
367 if (Flags & ACPI_DB_CONSOLE_OUTPUT)
369 Count = vfprintf (AcpiGbl_OutputFile, Fmt, Args);
374 /******************************************************************************
376 * FUNCTION: AcpiOsGetLine
378 * PARAMETERS: fmt Standard printf format
381 * RETURN: Actual bytes read
383 * DESCRIPTION: Formatted input with argument list pointer
385 *****************************************************************************/
397 scanf ("%1c", &Temp);
398 if (!Temp || Temp == '\n')
406 /* Null terminate the buffer */
410 /* Return the number of bytes in the string */
415 /******************************************************************************
417 * FUNCTION: AcpiOsMapMemory
419 * PARAMETERS: where Physical address of memory to be mapped
420 * length How much memory to map
422 * RETURN: Pointer to mapped memory. Null on error.
424 * DESCRIPTION: Map physical memory into caller's address space
426 *****************************************************************************/
430 ACPI_PHYSICAL_ADDRESS where,
434 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
438 /******************************************************************************
440 * FUNCTION: AcpiOsUnmapMemory
442 * PARAMETERS: where Logical address of memory to be unmapped
443 * length How much memory to unmap
447 * DESCRIPTION: Delete a previously created mapping. Where and Length must
448 * correspond to a previous mapping exactly.
450 *****************************************************************************/
462 /******************************************************************************
464 * FUNCTION: AcpiOsAllocate
466 * PARAMETERS: Size Amount to allocate, in bytes
468 * RETURN: Pointer to the new allocation. Null on error.
470 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
472 *****************************************************************************/
481 Mem = (void *) malloc ((size_t) size);
486 /******************************************************************************
488 * FUNCTION: AcpiOsFree
490 * PARAMETERS: mem Pointer to previously allocated memory
494 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
496 *****************************************************************************/
507 /******************************************************************************
509 * FUNCTION: AcpiOsCreateSemaphore
511 * PARAMETERS: InitialUnits - Units to be assigned to the new semaphore
512 * OutHandle - Where a handle will be returned
516 * DESCRIPTION: Create an OS semaphore
518 *****************************************************************************/
521 AcpiOsCreateSemaphore (
524 ACPI_HANDLE *OutHandle)
531 return (AE_BAD_PARAMETER);
535 Sem = sem_open (tmpnam (NULL), O_EXCL|O_CREAT, 0755, InitialUnits);
538 return (AE_NO_MEMORY);
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)
723 /******************************************************************************
725 * FUNCTION: Spinlock interfaces
727 * DESCRIPTION: Map these interfaces to semaphore interfaces
729 *****************************************************************************/
733 ACPI_SPINLOCK *OutHandle)
736 return (AcpiOsCreateSemaphore (1, 1, OutHandle));
742 ACPI_SPINLOCK Handle)
744 AcpiOsDeleteSemaphore (Handle);
752 AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
759 ACPI_SPINLOCK Handle,
760 ACPI_CPU_FLAGS Flags)
762 AcpiOsSignalSemaphore (Handle, 1);
766 /******************************************************************************
768 * FUNCTION: AcpiOsInstallInterruptHandler
770 * PARAMETERS: InterruptNumber Level handler should respond to.
771 * Isr Address of the ACPI interrupt handler
772 * ExceptPtr Where status is returned
774 * RETURN: Handle to the newly installed handler.
776 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
777 * OS-independent handler.
779 *****************************************************************************/
782 AcpiOsInstallInterruptHandler (
783 UINT32 InterruptNumber,
784 ACPI_OSD_HANDLER ServiceRoutine,
792 /******************************************************************************
794 * FUNCTION: AcpiOsRemoveInterruptHandler
796 * PARAMETERS: Handle Returned when handler was installed
800 * DESCRIPTION: Uninstalls an interrupt handler.
802 *****************************************************************************/
805 AcpiOsRemoveInterruptHandler (
806 UINT32 InterruptNumber,
807 ACPI_OSD_HANDLER ServiceRoutine)
814 /******************************************************************************
816 * FUNCTION: AcpiOsExecute
818 * PARAMETERS: Type - Type of execution
819 * Function - Address of the function to execute
820 * Context - Passed as a parameter to the function
824 * DESCRIPTION: Execute a new thread
826 *****************************************************************************/
830 ACPI_EXECUTE_TYPE Type,
831 ACPI_OSD_EXEC_CALLBACK Function,
838 ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
841 AcpiOsPrintf("Create thread failed");
847 /******************************************************************************
849 * FUNCTION: AcpiOsStall
851 * PARAMETERS: microseconds To sleep
853 * RETURN: Blocks until sleep is completed.
855 * DESCRIPTION: Sleep at microsecond granularity
857 *****************************************************************************/
866 usleep (microseconds);
871 /******************************************************************************
873 * FUNCTION: AcpiOsSleep
875 * PARAMETERS: milliseconds To sleep
877 * RETURN: Blocks until sleep is completed.
879 * DESCRIPTION: Sleep at millisecond granularity
881 *****************************************************************************/
888 sleep (milliseconds / 1000); /* Sleep for whole seconds */
891 * Arg to usleep() must be less than 1,000,000 (1 second)
893 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 *****************************************************************************/
909 AcpiOsGetTimer (void)
914 gettimeofday (&time, NULL);
916 /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
918 return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
922 /******************************************************************************
924 * FUNCTION: AcpiOsReadPciConfiguration
926 * PARAMETERS: PciId Seg/Bus/Dev
927 * Register Device Register
928 * Value Buffer where value is placed
929 * Width Number of bits
933 * DESCRIPTION: Read data from PCI configuration space
935 *****************************************************************************/
938 AcpiOsReadPciConfiguration (
949 /******************************************************************************
951 * FUNCTION: AcpiOsWritePciConfiguration
953 * PARAMETERS: PciId Seg/Bus/Dev
954 * Register Device Register
955 * Value Value to be written
956 * Width Number of bits
960 * DESCRIPTION: Write data to PCI configuration space
962 *****************************************************************************/
965 AcpiOsWritePciConfiguration (
975 /* TEMPORARY STUB FUNCTION */
986 /******************************************************************************
988 * FUNCTION: AcpiOsReadPort
990 * PARAMETERS: Address Address of I/O port/register to read
991 * Value Where value is placed
992 * Width Number of bits
994 * RETURN: Value read from port
996 * DESCRIPTION: Read data from an I/O port or register
998 *****************************************************************************/
1002 ACPI_IO_ADDRESS Address,
1018 *Value = 0xFFFFFFFF;
1022 return (AE_BAD_PARAMETER);
1029 /******************************************************************************
1031 * FUNCTION: AcpiOsWritePort
1033 * PARAMETERS: Address Address of I/O port/register to write
1034 * Value Value to write
1035 * Width Number of bits
1039 * DESCRIPTION: Write data to an I/O port or register
1041 *****************************************************************************/
1045 ACPI_IO_ADDRESS Address,
1054 /******************************************************************************
1056 * FUNCTION: AcpiOsReadMemory
1058 * PARAMETERS: Address Physical Memory Address to read
1059 * Value Where value is placed
1060 * Width Number of bits
1062 * RETURN: Value read from physical memory address
1064 * DESCRIPTION: Read data from a physical memory address
1066 *****************************************************************************/
1070 ACPI_PHYSICAL_ADDRESS Address,
1084 return (AE_BAD_PARAMETER);
1090 /******************************************************************************
1092 * FUNCTION: AcpiOsWriteMemory
1094 * PARAMETERS: Address Physical Memory Address to write
1095 * Value Value to write
1096 * Width Number of bits
1100 * DESCRIPTION: Write data to a physical memory address
1102 *****************************************************************************/
1106 ACPI_PHYSICAL_ADDRESS Address,
1115 /******************************************************************************
1117 * FUNCTION: AcpiOsReadable
1119 * PARAMETERS: Pointer - Area to be verified
1120 * Length - Size of area
1122 * RETURN: TRUE if readable for entire length
1124 * DESCRIPTION: Verify that a pointer is valid for reading
1126 *****************************************************************************/
1138 /******************************************************************************
1140 * FUNCTION: AcpiOsWritable
1142 * PARAMETERS: Pointer - Area to be verified
1143 * Length - Size of area
1145 * RETURN: TRUE if writable for entire length
1147 * DESCRIPTION: Verify that a pointer is valid for writing
1149 *****************************************************************************/
1161 /******************************************************************************
1163 * FUNCTION: AcpiOsGetThreadId
1167 * RETURN: Id of the running thread
1169 * DESCRIPTION: Get the Id of the current (running) thread
1171 * NOTE: The environment header should contain this line:
1172 * #define ACPI_THREAD_ID pthread_t
1174 *****************************************************************************/
1177 AcpiOsGetThreadId (void)
1180 return (pthread_self ());
1184 /******************************************************************************
1186 * FUNCTION: AcpiOsSignal
1188 * PARAMETERS: Function ACPI CA signal function code
1189 * Info Pointer to function-dependent structure
1193 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1195 *****************************************************************************/
1205 case ACPI_SIGNAL_FATAL:
1208 case ACPI_SIGNAL_BREAKPOINT: