Sync ACPICA with Intel's version 20161222.
[dragonfly.git] / sys / contrib / dev / acpica / source / os_specific / service_layers / osunixxf.c
blob47e358bc82bd129bc7169858084c955601cedfb6
1 /******************************************************************************
3 * Module Name: osunixxf - UNIX OSL interfaces
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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.
45 * These interfaces are required in order to compile the ASL compiler and the
46 * various ACPICA tools under Linux or other Unix-like system.
48 #include "acpi.h"
49 #include "accommon.h"
50 #include "amlcode.h"
51 #include "acparser.h"
52 #include "acdebug.h"
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <stdarg.h>
57 #include <unistd.h>
58 #include <sys/time.h>
59 #include <semaphore.h>
60 #include <pthread.h>
61 #include <errno.h>
63 #define _COMPONENT ACPI_OS_SERVICES
64 ACPI_MODULE_NAME ("osunixxf")
67 /* Upcalls to AcpiExec */
69 void
70 AeTableOverride (
71 ACPI_TABLE_HEADER *ExistingTable,
72 ACPI_TABLE_HEADER **NewTable);
74 typedef void* (*PTHREAD_CALLBACK) (void *);
76 /* Buffer used by AcpiOsVprintf */
78 #define ACPI_VPRINTF_BUFFER_SIZE 512
79 #define _ASCII_NEWLINE '\n'
81 /* Terminal support for AcpiExec only */
83 #ifdef ACPI_EXEC_APP
84 #include <termios.h>
86 struct termios OriginalTermAttributes;
87 int TermAttributesWereSet = 0;
89 ACPI_STATUS
90 AcpiUtReadLine (
91 char *Buffer,
92 UINT32 BufferLength,
93 UINT32 *BytesRead);
95 static void
96 OsEnterLineEditMode (
97 void);
99 static void
100 OsExitLineEditMode (
101 void);
104 /******************************************************************************
106 * FUNCTION: OsEnterLineEditMode, OsExitLineEditMode
108 * PARAMETERS: None
110 * RETURN: None
112 * DESCRIPTION: Enter/Exit the raw character input mode for the terminal.
114 * Interactive line-editing support for the AML debugger. Used with the
115 * common/acgetline module.
117 * readline() is not used because of non-portability. It is not available
118 * on all systems, and if it is, often the package must be manually installed.
120 * Therefore, we use the POSIX tcgetattr/tcsetattr and do the minimal line
121 * editing that we need in AcpiOsGetLine.
123 * If the POSIX tcgetattr/tcsetattr interfaces are unavailable, these
124 * calls will also work:
125 * For OsEnterLineEditMode: system ("stty cbreak -echo")
126 * For OsExitLineEditMode: system ("stty cooked echo")
128 *****************************************************************************/
130 static void
131 OsEnterLineEditMode (
132 void)
134 struct termios LocalTermAttributes;
137 TermAttributesWereSet = 0;
139 /* STDIN must be a terminal */
141 if (!isatty (STDIN_FILENO))
143 return;
146 /* Get and keep the original attributes */
148 if (tcgetattr (STDIN_FILENO, &OriginalTermAttributes))
150 fprintf (stderr, "Could not get terminal attributes!\n");
151 return;
154 /* Set the new attributes to enable raw character input */
156 memcpy (&LocalTermAttributes, &OriginalTermAttributes,
157 sizeof (struct termios));
159 LocalTermAttributes.c_lflag &= ~(ICANON | ECHO);
160 LocalTermAttributes.c_cc[VMIN] = 1;
161 LocalTermAttributes.c_cc[VTIME] = 0;
163 if (tcsetattr (STDIN_FILENO, TCSANOW, &LocalTermAttributes))
165 fprintf (stderr, "Could not set terminal attributes!\n");
166 return;
169 TermAttributesWereSet = 1;
173 static void
174 OsExitLineEditMode (
175 void)
178 if (!TermAttributesWereSet)
180 return;
183 /* Set terminal attributes back to the original values */
185 if (tcsetattr (STDIN_FILENO, TCSANOW, &OriginalTermAttributes))
187 fprintf (stderr, "Could not restore terminal attributes!\n");
192 #else
194 /* These functions are not needed for other ACPICA utilities */
196 #define OsEnterLineEditMode()
197 #define OsExitLineEditMode()
198 #endif
201 /******************************************************************************
203 * FUNCTION: AcpiOsInitialize, AcpiOsTerminate
205 * PARAMETERS: None
207 * RETURN: Status
209 * DESCRIPTION: Initialize and terminate this module.
211 *****************************************************************************/
213 ACPI_STATUS
214 AcpiOsInitialize (
215 void)
217 ACPI_STATUS Status;
220 AcpiGbl_OutputFile = stdout;
222 OsEnterLineEditMode ();
224 Status = AcpiOsCreateLock (&AcpiGbl_PrintLock);
225 if (ACPI_FAILURE (Status))
227 return (Status);
230 return (AE_OK);
233 ACPI_STATUS
234 AcpiOsTerminate (
235 void)
238 OsExitLineEditMode ();
239 return (AE_OK);
243 #ifndef ACPI_USE_NATIVE_RSDP_POINTER
244 /******************************************************************************
246 * FUNCTION: AcpiOsGetRootPointer
248 * PARAMETERS: None
250 * RETURN: RSDP physical address
252 * DESCRIPTION: Gets the ACPI root pointer (RSDP)
254 *****************************************************************************/
256 ACPI_PHYSICAL_ADDRESS
257 AcpiOsGetRootPointer (
258 void)
261 return (0);
263 #endif
266 /******************************************************************************
268 * FUNCTION: AcpiOsPredefinedOverride
270 * PARAMETERS: InitVal - Initial value of the predefined object
271 * NewVal - The new value for the object
273 * RETURN: Status, pointer to value. Null pointer returned if not
274 * overriding.
276 * DESCRIPTION: Allow the OS to override predefined names
278 *****************************************************************************/
280 ACPI_STATUS
281 AcpiOsPredefinedOverride (
282 const ACPI_PREDEFINED_NAMES *InitVal,
283 ACPI_STRING *NewVal)
286 if (!InitVal || !NewVal)
288 return (AE_BAD_PARAMETER);
291 *NewVal = NULL;
292 return (AE_OK);
296 /******************************************************************************
298 * FUNCTION: AcpiOsTableOverride
300 * PARAMETERS: ExistingTable - Header of current table (probably
301 * firmware)
302 * NewTable - Where an entire new table is returned.
304 * RETURN: Status, pointer to new table. Null pointer returned if no
305 * table is available to override
307 * DESCRIPTION: Return a different version of a table if one is available
309 *****************************************************************************/
311 ACPI_STATUS
312 AcpiOsTableOverride (
313 ACPI_TABLE_HEADER *ExistingTable,
314 ACPI_TABLE_HEADER **NewTable)
317 if (!ExistingTable || !NewTable)
319 return (AE_BAD_PARAMETER);
322 *NewTable = NULL;
324 #ifdef ACPI_EXEC_APP
326 AeTableOverride (ExistingTable, NewTable);
327 return (AE_OK);
328 #else
330 return (AE_NO_ACPI_TABLES);
331 #endif
335 /******************************************************************************
337 * FUNCTION: AcpiOsPhysicalTableOverride
339 * PARAMETERS: ExistingTable - Header of current table (probably firmware)
340 * NewAddress - Where new table address is returned
341 * (Physical address)
342 * NewTableLength - Where new table length is returned
344 * RETURN: Status, address/length of new table. Null pointer returned
345 * if no table is available to override.
347 * DESCRIPTION: Returns AE_SUPPORT, function not used in user space.
349 *****************************************************************************/
351 ACPI_STATUS
352 AcpiOsPhysicalTableOverride (
353 ACPI_TABLE_HEADER *ExistingTable,
354 ACPI_PHYSICAL_ADDRESS *NewAddress,
355 UINT32 *NewTableLength)
358 return (AE_SUPPORT);
362 /******************************************************************************
364 * FUNCTION: AcpiOsEnterSleep
366 * PARAMETERS: SleepState - Which sleep state to enter
367 * RegaValue - Register A value
368 * RegbValue - Register B value
370 * RETURN: Status
372 * DESCRIPTION: A hook before writing sleep registers to enter the sleep
373 * state. Return AE_CTRL_SKIP to skip further sleep register
374 * writes.
376 *****************************************************************************/
378 ACPI_STATUS
379 AcpiOsEnterSleep (
380 UINT8 SleepState,
381 UINT32 RegaValue,
382 UINT32 RegbValue)
385 return (AE_OK);
389 /******************************************************************************
391 * FUNCTION: AcpiOsRedirectOutput
393 * PARAMETERS: Destination - An open file handle/pointer
395 * RETURN: None
397 * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
399 *****************************************************************************/
401 void
402 AcpiOsRedirectOutput (
403 void *Destination)
406 AcpiGbl_OutputFile = Destination;
410 /******************************************************************************
412 * FUNCTION: AcpiOsPrintf
414 * PARAMETERS: fmt, ... - Standard printf format
416 * RETURN: None
418 * DESCRIPTION: Formatted output. Note: very similar to AcpiOsVprintf
419 * (performance), changes should be tracked in both functions.
421 *****************************************************************************/
423 void ACPI_INTERNAL_VAR_XFACE
424 AcpiOsPrintf (
425 const char *Fmt,
426 ...)
428 va_list Args;
429 UINT8 Flags;
432 Flags = AcpiGbl_DbOutputFlags;
433 if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
435 /* Output is directable to either a file (if open) or the console */
437 if (AcpiGbl_DebugFile)
439 /* Output file is open, send the output there */
441 va_start (Args, Fmt);
442 vfprintf (AcpiGbl_DebugFile, Fmt, Args);
443 va_end (Args);
445 else
447 /* No redirection, send output to console (once only!) */
449 Flags |= ACPI_DB_CONSOLE_OUTPUT;
453 if (Flags & ACPI_DB_CONSOLE_OUTPUT)
455 va_start (Args, Fmt);
456 vfprintf (AcpiGbl_OutputFile, Fmt, Args);
457 va_end (Args);
462 /******************************************************************************
464 * FUNCTION: AcpiOsVprintf
466 * PARAMETERS: fmt - Standard printf format
467 * args - Argument list
469 * RETURN: None
471 * DESCRIPTION: Formatted output with argument list pointer. Note: very
472 * similar to AcpiOsPrintf, changes should be tracked in both
473 * functions.
475 *****************************************************************************/
477 void
478 AcpiOsVprintf (
479 const char *Fmt,
480 va_list Args)
482 UINT8 Flags;
483 char Buffer[ACPI_VPRINTF_BUFFER_SIZE];
487 * We build the output string in a local buffer because we may be
488 * outputting the buffer twice. Using vfprintf is problematic because
489 * some implementations modify the args pointer/structure during
490 * execution. Thus, we use the local buffer for portability.
492 * Note: Since this module is intended for use by the various ACPICA
493 * utilities/applications, we can safely declare the buffer on the stack.
494 * Also, This function is used for relatively small error messages only.
496 vsnprintf (Buffer, ACPI_VPRINTF_BUFFER_SIZE, Fmt, Args);
498 Flags = AcpiGbl_DbOutputFlags;
499 if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
501 /* Output is directable to either a file (if open) or the console */
503 if (AcpiGbl_DebugFile)
505 /* Output file is open, send the output there */
507 fputs (Buffer, AcpiGbl_DebugFile);
509 else
511 /* No redirection, send output to console (once only!) */
513 Flags |= ACPI_DB_CONSOLE_OUTPUT;
517 if (Flags & ACPI_DB_CONSOLE_OUTPUT)
519 fputs (Buffer, AcpiGbl_OutputFile);
524 #ifndef ACPI_EXEC_APP
525 /******************************************************************************
527 * FUNCTION: AcpiOsGetLine
529 * PARAMETERS: Buffer - Where to return the command line
530 * BufferLength - Maximum length of Buffer
531 * BytesRead - Where the actual byte count is returned
533 * RETURN: Status and actual bytes read
535 * DESCRIPTION: Get the next input line from the terminal. NOTE: For the
536 * AcpiExec utility, we use the acgetline module instead to
537 * provide line-editing and history support.
539 *****************************************************************************/
541 ACPI_STATUS
542 AcpiOsGetLine (
543 char *Buffer,
544 UINT32 BufferLength,
545 UINT32 *BytesRead)
547 int InputChar;
548 UINT32 EndOfLine;
551 /* Standard AcpiOsGetLine for all utilities except AcpiExec */
553 for (EndOfLine = 0; ; EndOfLine++)
555 if (EndOfLine >= BufferLength)
557 return (AE_BUFFER_OVERFLOW);
560 if ((InputChar = getchar ()) == EOF)
562 return (AE_ERROR);
565 if (!InputChar || InputChar == _ASCII_NEWLINE)
567 break;
570 Buffer[EndOfLine] = (char) InputChar;
573 /* Null terminate the buffer */
575 Buffer[EndOfLine] = 0;
577 /* Return the number of bytes in the string */
579 if (BytesRead)
581 *BytesRead = EndOfLine;
584 return (AE_OK);
586 #endif
589 #ifndef ACPI_USE_NATIVE_MEMORY_MAPPING
590 /******************************************************************************
592 * FUNCTION: AcpiOsMapMemory
594 * PARAMETERS: where - Physical address of memory to be mapped
595 * length - How much memory to map
597 * RETURN: Pointer to mapped memory. Null on error.
599 * DESCRIPTION: Map physical memory into caller's address space
601 *****************************************************************************/
603 void *
604 AcpiOsMapMemory (
605 ACPI_PHYSICAL_ADDRESS where,
606 ACPI_SIZE length)
609 return (ACPI_TO_POINTER ((ACPI_SIZE) where));
613 /******************************************************************************
615 * FUNCTION: AcpiOsUnmapMemory
617 * PARAMETERS: where - Logical address of memory to be unmapped
618 * length - How much memory to unmap
620 * RETURN: None.
622 * DESCRIPTION: Delete a previously created mapping. Where and Length must
623 * correspond to a previous mapping exactly.
625 *****************************************************************************/
627 void
628 AcpiOsUnmapMemory (
629 void *where,
630 ACPI_SIZE length)
633 return;
635 #endif
638 /******************************************************************************
640 * FUNCTION: AcpiOsAllocate
642 * PARAMETERS: Size - Amount to allocate, in bytes
644 * RETURN: Pointer to the new allocation. Null on error.
646 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
648 *****************************************************************************/
650 void *
651 AcpiOsAllocate (
652 ACPI_SIZE size)
654 void *Mem;
657 Mem = (void *) malloc ((size_t) size);
658 return (Mem);
662 #ifdef USE_NATIVE_ALLOCATE_ZEROED
663 /******************************************************************************
665 * FUNCTION: AcpiOsAllocateZeroed
667 * PARAMETERS: Size - Amount to allocate, in bytes
669 * RETURN: Pointer to the new allocation. Null on error.
671 * DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.
673 *****************************************************************************/
675 void *
676 AcpiOsAllocateZeroed (
677 ACPI_SIZE size)
679 void *Mem;
682 Mem = (void *) calloc (1, (size_t) size);
683 return (Mem);
685 #endif
688 /******************************************************************************
690 * FUNCTION: AcpiOsFree
692 * PARAMETERS: mem - Pointer to previously allocated memory
694 * RETURN: None.
696 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
698 *****************************************************************************/
700 void
701 AcpiOsFree (
702 void *mem)
705 free (mem);
709 #ifdef ACPI_SINGLE_THREADED
710 /******************************************************************************
712 * FUNCTION: Semaphore stub functions
714 * DESCRIPTION: Stub functions used for single-thread applications that do
715 * not require semaphore synchronization. Full implementations
716 * of these functions appear after the stubs.
718 *****************************************************************************/
720 ACPI_STATUS
721 AcpiOsCreateSemaphore (
722 UINT32 MaxUnits,
723 UINT32 InitialUnits,
724 ACPI_HANDLE *OutHandle)
726 *OutHandle = (ACPI_HANDLE) 1;
727 return (AE_OK);
730 ACPI_STATUS
731 AcpiOsDeleteSemaphore (
732 ACPI_HANDLE Handle)
734 return (AE_OK);
737 ACPI_STATUS
738 AcpiOsWaitSemaphore (
739 ACPI_HANDLE Handle,
740 UINT32 Units,
741 UINT16 Timeout)
743 return (AE_OK);
746 ACPI_STATUS
747 AcpiOsSignalSemaphore (
748 ACPI_HANDLE Handle,
749 UINT32 Units)
751 return (AE_OK);
754 #else
755 /******************************************************************************
757 * FUNCTION: AcpiOsCreateSemaphore
759 * PARAMETERS: InitialUnits - Units to be assigned to the new semaphore
760 * OutHandle - Where a handle will be returned
762 * RETURN: Status
764 * DESCRIPTION: Create an OS semaphore
766 *****************************************************************************/
768 ACPI_STATUS
769 AcpiOsCreateSemaphore (
770 UINT32 MaxUnits,
771 UINT32 InitialUnits,
772 ACPI_HANDLE *OutHandle)
774 sem_t *Sem;
777 if (!OutHandle)
779 return (AE_BAD_PARAMETER);
782 #ifdef __APPLE__
784 static int SemaphoreCount = 0;
785 char SemaphoreName[32];
787 snprintf (SemaphoreName, sizeof (SemaphoreName), "acpi_sem_%d",
788 SemaphoreCount++);
789 printf ("%s\n", SemaphoreName);
790 Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
791 if (!Sem)
793 return (AE_NO_MEMORY);
795 sem_unlink (SemaphoreName); /* This just deletes the name */
798 #else
799 Sem = AcpiOsAllocate (sizeof (sem_t));
800 if (!Sem)
802 return (AE_NO_MEMORY);
805 if (sem_init (Sem, 0, InitialUnits) == -1)
807 AcpiOsFree (Sem);
808 return (AE_BAD_PARAMETER);
810 #endif
812 *OutHandle = (ACPI_HANDLE) Sem;
813 return (AE_OK);
817 /******************************************************************************
819 * FUNCTION: AcpiOsDeleteSemaphore
821 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
823 * RETURN: Status
825 * DESCRIPTION: Delete an OS semaphore
827 *****************************************************************************/
829 ACPI_STATUS
830 AcpiOsDeleteSemaphore (
831 ACPI_HANDLE Handle)
833 sem_t *Sem = (sem_t *) Handle;
836 if (!Sem)
838 return (AE_BAD_PARAMETER);
841 #ifdef __APPLE__
842 if (sem_close (Sem) == -1)
844 return (AE_BAD_PARAMETER);
846 #else
847 if (sem_destroy (Sem) == -1)
849 return (AE_BAD_PARAMETER);
851 #endif
853 return (AE_OK);
857 /******************************************************************************
859 * FUNCTION: AcpiOsWaitSemaphore
861 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
862 * Units - How many units to wait for
863 * MsecTimeout - How long to wait (milliseconds)
865 * RETURN: Status
867 * DESCRIPTION: Wait for units
869 *****************************************************************************/
871 ACPI_STATUS
872 AcpiOsWaitSemaphore (
873 ACPI_HANDLE Handle,
874 UINT32 Units,
875 UINT16 MsecTimeout)
877 ACPI_STATUS Status = AE_OK;
878 sem_t *Sem = (sem_t *) Handle;
879 #ifndef ACPI_USE_ALTERNATE_TIMEOUT
880 struct timespec Time;
881 int RetVal;
882 #endif
885 if (!Sem)
887 return (AE_BAD_PARAMETER);
890 switch (MsecTimeout)
893 * No Wait:
894 * --------
895 * A zero timeout value indicates that we shouldn't wait - just
896 * acquire the semaphore if available otherwise return AE_TIME
897 * (a.k.a. 'would block').
899 case 0:
901 if (sem_trywait(Sem) == -1)
903 Status = (AE_TIME);
905 break;
907 /* Wait Indefinitely */
909 case ACPI_WAIT_FOREVER:
911 if (sem_wait (Sem))
913 Status = (AE_TIME);
915 break;
917 /* Wait with MsecTimeout */
919 default:
921 #ifdef ACPI_USE_ALTERNATE_TIMEOUT
923 * Alternate timeout mechanism for environments where
924 * sem_timedwait is not available or does not work properly.
926 while (MsecTimeout)
928 if (sem_trywait (Sem) == 0)
930 /* Got the semaphore */
931 return (AE_OK);
934 if (MsecTimeout >= 10)
936 MsecTimeout -= 10;
937 usleep (10 * ACPI_USEC_PER_MSEC); /* ten milliseconds */
939 else
941 MsecTimeout--;
942 usleep (ACPI_USEC_PER_MSEC); /* one millisecond */
945 Status = (AE_TIME);
946 #else
948 * The interface to sem_timedwait is an absolute time, so we need to
949 * get the current time, then add in the millisecond Timeout value.
951 if (clock_gettime (CLOCK_REALTIME, &Time) == -1)
953 perror ("clock_gettime");
954 return (AE_TIME);
957 Time.tv_sec += (MsecTimeout / ACPI_MSEC_PER_SEC);
958 Time.tv_nsec += ((MsecTimeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);
960 /* Handle nanosecond overflow (field must be less than one second) */
962 if (Time.tv_nsec >= ACPI_NSEC_PER_SEC)
964 Time.tv_sec += (Time.tv_nsec / ACPI_NSEC_PER_SEC);
965 Time.tv_nsec = (Time.tv_nsec % ACPI_NSEC_PER_SEC);
968 while (((RetVal = sem_timedwait (Sem, &Time)) == -1) && (errno == EINTR))
970 continue;
973 if (RetVal != 0)
975 if (errno != ETIMEDOUT)
977 perror ("sem_timedwait");
979 Status = (AE_TIME);
981 #endif
982 break;
985 return (Status);
989 /******************************************************************************
991 * FUNCTION: AcpiOsSignalSemaphore
993 * PARAMETERS: Handle - Handle returned by AcpiOsCreateSemaphore
994 * Units - Number of units to send
996 * RETURN: Status
998 * DESCRIPTION: Send units
1000 *****************************************************************************/
1002 ACPI_STATUS
1003 AcpiOsSignalSemaphore (
1004 ACPI_HANDLE Handle,
1005 UINT32 Units)
1007 sem_t *Sem = (sem_t *)Handle;
1010 if (!Sem)
1012 return (AE_BAD_PARAMETER);
1015 if (sem_post (Sem) == -1)
1017 return (AE_LIMIT);
1020 return (AE_OK);
1023 #endif /* ACPI_SINGLE_THREADED */
1026 /******************************************************************************
1028 * FUNCTION: Spinlock interfaces
1030 * DESCRIPTION: Map these interfaces to semaphore interfaces
1032 *****************************************************************************/
1034 ACPI_STATUS
1035 AcpiOsCreateLock (
1036 ACPI_SPINLOCK *OutHandle)
1039 return (AcpiOsCreateSemaphore (1, 1, OutHandle));
1043 void
1044 AcpiOsDeleteLock (
1045 ACPI_SPINLOCK Handle)
1047 AcpiOsDeleteSemaphore (Handle);
1051 ACPI_CPU_FLAGS
1052 AcpiOsAcquireLock (
1053 ACPI_HANDLE Handle)
1055 AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
1056 return (0);
1060 void
1061 AcpiOsReleaseLock (
1062 ACPI_SPINLOCK Handle,
1063 ACPI_CPU_FLAGS Flags)
1065 AcpiOsSignalSemaphore (Handle, 1);
1069 /******************************************************************************
1071 * FUNCTION: AcpiOsInstallInterruptHandler
1073 * PARAMETERS: InterruptNumber - Level handler should respond to.
1074 * Isr - Address of the ACPI interrupt handler
1075 * ExceptPtr - Where status is returned
1077 * RETURN: Handle to the newly installed handler.
1079 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
1080 * OS-independent handler.
1082 *****************************************************************************/
1084 UINT32
1085 AcpiOsInstallInterruptHandler (
1086 UINT32 InterruptNumber,
1087 ACPI_OSD_HANDLER ServiceRoutine,
1088 void *Context)
1091 return (AE_OK);
1095 /******************************************************************************
1097 * FUNCTION: AcpiOsRemoveInterruptHandler
1099 * PARAMETERS: Handle - Returned when handler was installed
1101 * RETURN: Status
1103 * DESCRIPTION: Uninstalls an interrupt handler.
1105 *****************************************************************************/
1107 ACPI_STATUS
1108 AcpiOsRemoveInterruptHandler (
1109 UINT32 InterruptNumber,
1110 ACPI_OSD_HANDLER ServiceRoutine)
1113 return (AE_OK);
1117 /******************************************************************************
1119 * FUNCTION: AcpiOsStall
1121 * PARAMETERS: microseconds - Time to sleep
1123 * RETURN: Blocks until sleep is completed.
1125 * DESCRIPTION: Sleep at microsecond granularity
1127 *****************************************************************************/
1129 void
1130 AcpiOsStall (
1131 UINT32 microseconds)
1134 if (microseconds)
1136 usleep (microseconds);
1141 /******************************************************************************
1143 * FUNCTION: AcpiOsSleep
1145 * PARAMETERS: milliseconds - Time to sleep
1147 * RETURN: Blocks until sleep is completed.
1149 * DESCRIPTION: Sleep at millisecond granularity
1151 *****************************************************************************/
1153 void
1154 AcpiOsSleep (
1155 UINT64 milliseconds)
1158 /* Sleep for whole seconds */
1160 sleep (milliseconds / ACPI_MSEC_PER_SEC);
1163 * Sleep for remaining microseconds.
1164 * Arg to usleep() is in usecs and must be less than 1,000,000 (1 second).
1166 usleep ((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);
1170 /******************************************************************************
1172 * FUNCTION: AcpiOsGetTimer
1174 * PARAMETERS: None
1176 * RETURN: Current time in 100 nanosecond units
1178 * DESCRIPTION: Get the current system time
1180 *****************************************************************************/
1182 UINT64
1183 AcpiOsGetTimer (
1184 void)
1186 struct timeval time;
1189 /* This timer has sufficient resolution for user-space application code */
1191 gettimeofday (&time, NULL);
1193 /* (Seconds * 10^7 = 100ns(10^-7)) + (Microseconds(10^-6) * 10^1 = 100ns) */
1195 return (((UINT64) time.tv_sec * ACPI_100NSEC_PER_SEC) +
1196 ((UINT64) time.tv_usec * ACPI_100NSEC_PER_USEC));
1200 /******************************************************************************
1202 * FUNCTION: AcpiOsReadPciConfiguration
1204 * PARAMETERS: PciId - Seg/Bus/Dev
1205 * PciRegister - Device Register
1206 * Value - Buffer where value is placed
1207 * Width - Number of bits
1209 * RETURN: Status
1211 * DESCRIPTION: Read data from PCI configuration space
1213 *****************************************************************************/
1215 ACPI_STATUS
1216 AcpiOsReadPciConfiguration (
1217 ACPI_PCI_ID *PciId,
1218 UINT32 PciRegister,
1219 UINT64 *Value,
1220 UINT32 Width)
1223 *Value = 0;
1224 return (AE_OK);
1228 /******************************************************************************
1230 * FUNCTION: AcpiOsWritePciConfiguration
1232 * PARAMETERS: PciId - Seg/Bus/Dev
1233 * PciRegister - Device Register
1234 * Value - Value to be written
1235 * Width - Number of bits
1237 * RETURN: Status.
1239 * DESCRIPTION: Write data to PCI configuration space
1241 *****************************************************************************/
1243 ACPI_STATUS
1244 AcpiOsWritePciConfiguration (
1245 ACPI_PCI_ID *PciId,
1246 UINT32 PciRegister,
1247 UINT64 Value,
1248 UINT32 Width)
1251 return (AE_OK);
1255 /******************************************************************************
1257 * FUNCTION: AcpiOsReadPort
1259 * PARAMETERS: Address - Address of I/O port/register to read
1260 * Value - Where value is placed
1261 * Width - Number of bits
1263 * RETURN: Value read from port
1265 * DESCRIPTION: Read data from an I/O port or register
1267 *****************************************************************************/
1269 ACPI_STATUS
1270 AcpiOsReadPort (
1271 ACPI_IO_ADDRESS Address,
1272 UINT32 *Value,
1273 UINT32 Width)
1276 switch (Width)
1278 case 8:
1280 *Value = 0xFF;
1281 break;
1283 case 16:
1285 *Value = 0xFFFF;
1286 break;
1288 case 32:
1290 *Value = 0xFFFFFFFF;
1291 break;
1293 default:
1295 return (AE_BAD_PARAMETER);
1298 return (AE_OK);
1302 /******************************************************************************
1304 * FUNCTION: AcpiOsWritePort
1306 * PARAMETERS: Address - Address of I/O port/register to write
1307 * Value - Value to write
1308 * Width - Number of bits
1310 * RETURN: None
1312 * DESCRIPTION: Write data to an I/O port or register
1314 *****************************************************************************/
1316 ACPI_STATUS
1317 AcpiOsWritePort (
1318 ACPI_IO_ADDRESS Address,
1319 UINT32 Value,
1320 UINT32 Width)
1323 return (AE_OK);
1327 /******************************************************************************
1329 * FUNCTION: AcpiOsReadMemory
1331 * PARAMETERS: Address - Physical Memory Address to read
1332 * Value - Where value is placed
1333 * Width - Number of bits (8,16,32, or 64)
1335 * RETURN: Value read from physical memory address. Always returned
1336 * as a 64-bit integer, regardless of the read width.
1338 * DESCRIPTION: Read data from a physical memory address
1340 *****************************************************************************/
1342 ACPI_STATUS
1343 AcpiOsReadMemory (
1344 ACPI_PHYSICAL_ADDRESS Address,
1345 UINT64 *Value,
1346 UINT32 Width)
1349 switch (Width)
1351 case 8:
1352 case 16:
1353 case 32:
1354 case 64:
1356 *Value = 0;
1357 break;
1359 default:
1361 return (AE_BAD_PARAMETER);
1363 return (AE_OK);
1367 /******************************************************************************
1369 * FUNCTION: AcpiOsWriteMemory
1371 * PARAMETERS: Address - Physical Memory Address to write
1372 * Value - Value to write
1373 * Width - Number of bits (8,16,32, or 64)
1375 * RETURN: None
1377 * DESCRIPTION: Write data to a physical memory address
1379 *****************************************************************************/
1381 ACPI_STATUS
1382 AcpiOsWriteMemory (
1383 ACPI_PHYSICAL_ADDRESS Address,
1384 UINT64 Value,
1385 UINT32 Width)
1388 return (AE_OK);
1392 /******************************************************************************
1394 * FUNCTION: AcpiOsReadable
1396 * PARAMETERS: Pointer - Area to be verified
1397 * Length - Size of area
1399 * RETURN: TRUE if readable for entire length
1401 * DESCRIPTION: Verify that a pointer is valid for reading
1403 *****************************************************************************/
1405 BOOLEAN
1406 AcpiOsReadable (
1407 void *Pointer,
1408 ACPI_SIZE Length)
1411 return (TRUE);
1415 /******************************************************************************
1417 * FUNCTION: AcpiOsWritable
1419 * PARAMETERS: Pointer - Area to be verified
1420 * Length - Size of area
1422 * RETURN: TRUE if writable for entire length
1424 * DESCRIPTION: Verify that a pointer is valid for writing
1426 *****************************************************************************/
1428 BOOLEAN
1429 AcpiOsWritable (
1430 void *Pointer,
1431 ACPI_SIZE Length)
1434 return (TRUE);
1438 /******************************************************************************
1440 * FUNCTION: AcpiOsSignal
1442 * PARAMETERS: Function - ACPI A signal function code
1443 * Info - Pointer to function-dependent structure
1445 * RETURN: Status
1447 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1449 *****************************************************************************/
1451 ACPI_STATUS
1452 AcpiOsSignal (
1453 UINT32 Function,
1454 void *Info)
1457 switch (Function)
1459 case ACPI_SIGNAL_FATAL:
1461 break;
1463 case ACPI_SIGNAL_BREAKPOINT:
1465 break;
1467 default:
1469 break;
1472 return (AE_OK);
1475 /* Optional multi-thread support */
1477 #ifndef ACPI_SINGLE_THREADED
1478 /******************************************************************************
1480 * FUNCTION: AcpiOsGetThreadId
1482 * PARAMETERS: None
1484 * RETURN: Id of the running thread
1486 * DESCRIPTION: Get the ID of the current (running) thread
1488 *****************************************************************************/
1490 ACPI_THREAD_ID
1491 AcpiOsGetThreadId (
1492 void)
1494 pthread_t thread;
1497 thread = pthread_self();
1498 return (ACPI_CAST_PTHREAD_T (thread));
1502 /******************************************************************************
1504 * FUNCTION: AcpiOsExecute
1506 * PARAMETERS: Type - Type of execution
1507 * Function - Address of the function to execute
1508 * Context - Passed as a parameter to the function
1510 * RETURN: Status.
1512 * DESCRIPTION: Execute a new thread
1514 *****************************************************************************/
1516 ACPI_STATUS
1517 AcpiOsExecute (
1518 ACPI_EXECUTE_TYPE Type,
1519 ACPI_OSD_EXEC_CALLBACK Function,
1520 void *Context)
1522 pthread_t thread;
1523 int ret;
1526 ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
1527 if (ret)
1529 AcpiOsPrintf("Create thread failed");
1531 return (0);
1534 #else /* ACPI_SINGLE_THREADED */
1535 ACPI_THREAD_ID
1536 AcpiOsGetThreadId (
1537 void)
1539 return (1);
1542 ACPI_STATUS
1543 AcpiOsExecute (
1544 ACPI_EXECUTE_TYPE Type,
1545 ACPI_OSD_EXEC_CALLBACK Function,
1546 void *Context)
1549 Function (Context);
1551 return (AE_OK);
1554 #endif /* ACPI_SINGLE_THREADED */
1557 /******************************************************************************
1559 * FUNCTION: AcpiOsWaitEventsComplete
1561 * PARAMETERS: None
1563 * RETURN: None
1565 * DESCRIPTION: Wait for all asynchronous events to complete. This
1566 * implementation does nothing.
1568 *****************************************************************************/
1570 void
1571 AcpiOsWaitEventsComplete (
1572 void)
1574 return;