1/******************************************************************************
2 *
3 * Module Name: osunixxf - UNIX OSL interfaces
4 *
5 *****************************************************************************/
6
7/******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2016, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
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
19 * property rights.
20 *
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
27 *
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;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
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.
53 *
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
64 * make.
65 *
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
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
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.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
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
88 * PARTICULAR PURPOSE.
89 *
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
97 * LIMITED REMEDY.
98 *
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.
113 *
114 *****************************************************************************/
115
116/*
117 * These interfaces are required in order to compile the ASL compiler and the
118 * various ACPICA tools under Linux or other Unix-like system.
119 */
120#include "acpi.h"
121#include "accommon.h"
122#include "amlcode.h"
123#include "acparser.h"
124#include "acdebug.h"
125
126#include <stdio.h>
127#include <stdlib.h>
128#include <stdarg.h>
129#include <unistd.h>
130#include <sys/time.h>
131#include <semaphore.h>
132#include <pthread.h>
133#include <errno.h>
134
135#define _COMPONENT          ACPI_OS_SERVICES
136        ACPI_MODULE_NAME    ("osunixxf")
137
138
139BOOLEAN                        AcpiGbl_DebugTimeout = FALSE;
140
141
142/* Upcalls to AcpiExec */
143
144void
145AeTableOverride (
146    ACPI_TABLE_HEADER       *ExistingTable,
147    ACPI_TABLE_HEADER       **NewTable);
148
149typedef void* (*PTHREAD_CALLBACK) (void *);
150
151/* Buffer used by AcpiOsVprintf */
152
153#define ACPI_VPRINTF_BUFFER_SIZE    512
154#define _ASCII_NEWLINE              '\n'
155
156/* Terminal support for AcpiExec only */
157
158#ifdef ACPI_EXEC_APP
159#include <termios.h>
160
161struct termios              OriginalTermAttributes;
162int                         TermAttributesWereSet = 0;
163
164ACPI_STATUS
165AcpiUtReadLine (
166    char                    *Buffer,
167    UINT32                  BufferLength,
168    UINT32                  *BytesRead);
169
170static void
171OsEnterLineEditMode (
172    void);
173
174static void
175OsExitLineEditMode (
176    void);
177
178
179/******************************************************************************
180 *
181 * FUNCTION:    OsEnterLineEditMode, OsExitLineEditMode
182 *
183 * PARAMETERS:  None
184 *
185 * RETURN:      None
186 *
187 * DESCRIPTION: Enter/Exit the raw character input mode for the terminal.
188 *
189 * Interactive line-editing support for the AML debugger. Used with the
190 * common/acgetline module.
191 *
192 * readline() is not used because of non-portability. It is not available
193 * on all systems, and if it is, often the package must be manually installed.
194 *
195 * Therefore, we use the POSIX tcgetattr/tcsetattr and do the minimal line
196 * editing that we need in AcpiOsGetLine.
197 *
198 * If the POSIX tcgetattr/tcsetattr interfaces are unavailable, these
199 * calls will also work:
200 *     For OsEnterLineEditMode: system ("stty cbreak -echo")
201 *     For OsExitLineEditMode:  system ("stty cooked echo")
202 *
203 *****************************************************************************/
204
205static void
206OsEnterLineEditMode (
207    void)
208{
209    struct termios          LocalTermAttributes;
210
211
212    TermAttributesWereSet = 0;
213
214    /* STDIN must be a terminal */
215
216    if (!isatty (STDIN_FILENO))
217    {
218        return;
219    }
220
221    /* Get and keep the original attributes */
222
223    if (tcgetattr (STDIN_FILENO, &OriginalTermAttributes))
224    {
225        fprintf (stderr, "Could not get terminal attributes!\n");
226        return;
227    }
228
229    /* Set the new attributes to enable raw character input */
230
231    memcpy (&LocalTermAttributes, &OriginalTermAttributes,
232        sizeof (struct termios));
233
234    LocalTermAttributes.c_lflag &= ~(ICANON | ECHO);
235    LocalTermAttributes.c_cc[VMIN] = 1;
236    LocalTermAttributes.c_cc[VTIME] = 0;
237
238    if (tcsetattr (STDIN_FILENO, TCSANOW, &LocalTermAttributes))
239    {
240        fprintf (stderr, "Could not set terminal attributes!\n");
241        return;
242    }
243
244    TermAttributesWereSet = 1;
245}
246
247
248static void
249OsExitLineEditMode (
250    void)
251{
252
253    if (!TermAttributesWereSet)
254    {
255        return;
256    }
257
258    /* Set terminal attributes back to the original values */
259
260    if (tcsetattr (STDIN_FILENO, TCSANOW, &OriginalTermAttributes))
261    {
262        fprintf (stderr, "Could not restore terminal attributes!\n");
263    }
264}
265
266
267#else
268
269/* These functions are not needed for other ACPICA utilities */
270
271#define OsEnterLineEditMode()
272#define OsExitLineEditMode()
273#endif
274
275
276/******************************************************************************
277 *
278 * FUNCTION:    AcpiOsInitialize, AcpiOsTerminate
279 *
280 * PARAMETERS:  None
281 *
282 * RETURN:      Status
283 *
284 * DESCRIPTION: Initialize and terminate this module.
285 *
286 *****************************************************************************/
287
288ACPI_STATUS
289AcpiOsInitialize (
290    void)
291{
292    ACPI_STATUS            Status;
293
294
295    AcpiGbl_OutputFile = stdout;
296
297    OsEnterLineEditMode ();
298
299    Status = AcpiOsCreateLock (&AcpiGbl_PrintLock);
300    if (ACPI_FAILURE (Status))
301    {
302        return (Status);
303    }
304
305    return (AE_OK);
306}
307
308ACPI_STATUS
309AcpiOsTerminate (
310    void)
311{
312
313    OsExitLineEditMode ();
314    return (AE_OK);
315}
316
317
318#ifndef ACPI_USE_NATIVE_RSDP_POINTER
319/******************************************************************************
320 *
321 * FUNCTION:    AcpiOsGetRootPointer
322 *
323 * PARAMETERS:  None
324 *
325 * RETURN:      RSDP physical address
326 *
327 * DESCRIPTION: Gets the ACPI root pointer (RSDP)
328 *
329 *****************************************************************************/
330
331ACPI_PHYSICAL_ADDRESS
332AcpiOsGetRootPointer (
333    void)
334{
335
336    return (0);
337}
338#endif
339
340
341/******************************************************************************
342 *
343 * FUNCTION:    AcpiOsPredefinedOverride
344 *
345 * PARAMETERS:  InitVal             - Initial value of the predefined object
346 *              NewVal              - The new value for the object
347 *
348 * RETURN:      Status, pointer to value. Null pointer returned if not
349 *              overriding.
350 *
351 * DESCRIPTION: Allow the OS to override predefined names
352 *
353 *****************************************************************************/
354
355ACPI_STATUS
356AcpiOsPredefinedOverride (
357    const ACPI_PREDEFINED_NAMES *InitVal,
358    ACPI_STRING                 *NewVal)
359{
360
361    if (!InitVal || !NewVal)
362    {
363        return (AE_BAD_PARAMETER);
364    }
365
366    *NewVal = NULL;
367    return (AE_OK);
368}
369
370
371/******************************************************************************
372 *
373 * FUNCTION:    AcpiOsTableOverride
374 *
375 * PARAMETERS:  ExistingTable       - Header of current table (probably
376 *                                    firmware)
377 *              NewTable            - Where an entire new table is returned.
378 *
379 * RETURN:      Status, pointer to new table. Null pointer returned if no
380 *              table is available to override
381 *
382 * DESCRIPTION: Return a different version of a table if one is available
383 *
384 *****************************************************************************/
385
386ACPI_STATUS
387AcpiOsTableOverride (
388    ACPI_TABLE_HEADER       *ExistingTable,
389    ACPI_TABLE_HEADER       **NewTable)
390{
391
392    if (!ExistingTable || !NewTable)
393    {
394        return (AE_BAD_PARAMETER);
395    }
396
397    *NewTable = NULL;
398
399#ifdef ACPI_EXEC_APP
400
401    AeTableOverride (ExistingTable, NewTable);
402    return (AE_OK);
403#else
404
405    return (AE_NO_ACPI_TABLES);
406#endif
407}
408
409
410/******************************************************************************
411 *
412 * FUNCTION:    AcpiOsPhysicalTableOverride
413 *
414 * PARAMETERS:  ExistingTable       - Header of current table (probably firmware)
415 *              NewAddress          - Where new table address is returned
416 *                                    (Physical address)
417 *              NewTableLength      - Where new table length is returned
418 *
419 * RETURN:      Status, address/length of new table. Null pointer returned
420 *              if no table is available to override.
421 *
422 * DESCRIPTION: Returns AE_SUPPORT, function not used in user space.
423 *
424 *****************************************************************************/
425
426ACPI_STATUS
427AcpiOsPhysicalTableOverride (
428    ACPI_TABLE_HEADER       *ExistingTable,
429    ACPI_PHYSICAL_ADDRESS   *NewAddress,
430    UINT32                  *NewTableLength)
431{
432
433    return (AE_SUPPORT);
434}
435
436
437/******************************************************************************
438 *
439 * FUNCTION:    AcpiOsRedirectOutput
440 *
441 * PARAMETERS:  Destination         - An open file handle/pointer
442 *
443 * RETURN:      None
444 *
445 * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
446 *
447 *****************************************************************************/
448
449void
450AcpiOsRedirectOutput (
451    void                    *Destination)
452{
453
454    AcpiGbl_OutputFile = Destination;
455}
456
457
458/******************************************************************************
459 *
460 * FUNCTION:    AcpiOsPrintf
461 *
462 * PARAMETERS:  fmt, ...            - Standard printf format
463 *
464 * RETURN:      None
465 *
466 * DESCRIPTION: Formatted output. Note: very similar to AcpiOsVprintf
467 *              (performance), changes should be tracked in both functions.
468 *
469 *****************************************************************************/
470
471void ACPI_INTERNAL_VAR_XFACE
472AcpiOsPrintf (
473    const char              *Fmt,
474    ...)
475{
476    va_list                 Args;
477    UINT8                   Flags;
478
479
480    Flags = AcpiGbl_DbOutputFlags;
481    if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
482    {
483        /* Output is directable to either a file (if open) or the console */
484
485        if (AcpiGbl_DebugFile)
486        {
487            /* Output file is open, send the output there */
488
489            va_start (Args, Fmt);
490            vfprintf (AcpiGbl_DebugFile, Fmt, Args);
491            va_end (Args);
492        }
493        else
494        {
495            /* No redirection, send output to console (once only!) */
496
497            Flags |= ACPI_DB_CONSOLE_OUTPUT;
498        }
499    }
500
501    if (Flags & ACPI_DB_CONSOLE_OUTPUT)
502    {
503        va_start (Args, Fmt);
504        vfprintf (AcpiGbl_OutputFile, Fmt, Args);
505        va_end (Args);
506    }
507}
508
509
510/******************************************************************************
511 *
512 * FUNCTION:    AcpiOsVprintf
513 *
514 * PARAMETERS:  fmt                 - Standard printf format
515 *              args                - Argument list
516 *
517 * RETURN:      None
518 *
519 * DESCRIPTION: Formatted output with argument list pointer. Note: very
520 *              similar to AcpiOsPrintf, changes should be tracked in both
521 *              functions.
522 *
523 *****************************************************************************/
524
525void
526AcpiOsVprintf (
527    const char              *Fmt,
528    va_list                 Args)
529{
530    UINT8                   Flags;
531    char                    Buffer[ACPI_VPRINTF_BUFFER_SIZE];
532
533
534    /*
535     * We build the output string in a local buffer because we may be
536     * outputting the buffer twice. Using vfprintf is problematic because
537     * some implementations modify the args pointer/structure during
538     * execution. Thus, we use the local buffer for portability.
539     *
540     * Note: Since this module is intended for use by the various ACPICA
541     * utilities/applications, we can safely declare the buffer on the stack.
542     * Also, This function is used for relatively small error messages only.
543     */
544    vsnprintf (Buffer, ACPI_VPRINTF_BUFFER_SIZE, Fmt, Args);
545
546    Flags = AcpiGbl_DbOutputFlags;
547    if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
548    {
549        /* Output is directable to either a file (if open) or the console */
550
551        if (AcpiGbl_DebugFile)
552        {
553            /* Output file is open, send the output there */
554
555            fputs (Buffer, AcpiGbl_DebugFile);
556        }
557        else
558        {
559            /* No redirection, send output to console (once only!) */
560
561            Flags |= ACPI_DB_CONSOLE_OUTPUT;
562        }
563    }
564
565    if (Flags & ACPI_DB_CONSOLE_OUTPUT)
566    {
567        fputs (Buffer, AcpiGbl_OutputFile);
568    }
569}
570
571
572#ifndef ACPI_EXEC_APP
573/******************************************************************************
574 *
575 * FUNCTION:    AcpiOsGetLine
576 *
577 * PARAMETERS:  Buffer              - Where to return the command line
578 *              BufferLength        - Maximum length of Buffer
579 *              BytesRead           - Where the actual byte count is returned
580 *
581 * RETURN:      Status and actual bytes read
582 *
583 * DESCRIPTION: Get the next input line from the terminal. NOTE: For the
584 *              AcpiExec utility, we use the acgetline module instead to
585 *              provide line-editing and history support.
586 *
587 *****************************************************************************/
588
589ACPI_STATUS
590AcpiOsGetLine (
591    char                    *Buffer,
592    UINT32                  BufferLength,
593    UINT32                  *BytesRead)
594{
595    int                     InputChar;
596    UINT32                  EndOfLine;
597
598
599    /* Standard AcpiOsGetLine for all utilities except AcpiExec */
600
601    for (EndOfLine = 0; ; EndOfLine++)
602    {
603        if (EndOfLine >= BufferLength)
604        {
605            return (AE_BUFFER_OVERFLOW);
606        }
607
608        if ((InputChar = getchar ()) == EOF)
609        {
610            return (AE_ERROR);
611        }
612
613        if (!InputChar || InputChar == _ASCII_NEWLINE)
614        {
615            break;
616        }
617
618        Buffer[EndOfLine] = (char) InputChar;
619    }
620
621    /* Null terminate the buffer */
622
623    Buffer[EndOfLine] = 0;
624
625    /* Return the number of bytes in the string */
626
627    if (BytesRead)
628    {
629        *BytesRead = EndOfLine;
630    }
631
632    return (AE_OK);
633}
634#endif
635
636
637#ifndef ACPI_USE_NATIVE_MEMORY_MAPPING
638/******************************************************************************
639 *
640 * FUNCTION:    AcpiOsMapMemory
641 *
642 * PARAMETERS:  where               - Physical address of memory to be mapped
643 *              length              - How much memory to map
644 *
645 * RETURN:      Pointer to mapped memory. Null on error.
646 *
647 * DESCRIPTION: Map physical memory into caller's address space
648 *
649 *****************************************************************************/
650
651void *
652AcpiOsMapMemory (
653    ACPI_PHYSICAL_ADDRESS   where,
654    ACPI_SIZE               length)
655{
656
657    return (ACPI_TO_POINTER ((ACPI_SIZE) where));
658}
659
660
661/******************************************************************************
662 *
663 * FUNCTION:    AcpiOsUnmapMemory
664 *
665 * PARAMETERS:  where               - Logical address of memory to be unmapped
666 *              length              - How much memory to unmap
667 *
668 * RETURN:      None.
669 *
670 * DESCRIPTION: Delete a previously created mapping. Where and Length must
671 *              correspond to a previous mapping exactly.
672 *
673 *****************************************************************************/
674
675void
676AcpiOsUnmapMemory (
677    void                    *where,
678    ACPI_SIZE               length)
679{
680
681    return;
682}
683#endif
684
685
686/******************************************************************************
687 *
688 * FUNCTION:    AcpiOsAllocate
689 *
690 * PARAMETERS:  Size                - Amount to allocate, in bytes
691 *
692 * RETURN:      Pointer to the new allocation. Null on error.
693 *
694 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
695 *
696 *****************************************************************************/
697
698void *
699AcpiOsAllocate (
700    ACPI_SIZE               size)
701{
702    void                    *Mem;
703
704
705    Mem = (void *) malloc ((size_t) size);
706    return (Mem);
707}
708
709
710#ifdef USE_NATIVE_ALLOCATE_ZEROED
711/******************************************************************************
712 *
713 * FUNCTION:    AcpiOsAllocateZeroed
714 *
715 * PARAMETERS:  Size                - Amount to allocate, in bytes
716 *
717 * RETURN:      Pointer to the new allocation. Null on error.
718 *
719 * DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.
720 *
721 *****************************************************************************/
722
723void *
724AcpiOsAllocateZeroed (
725    ACPI_SIZE               size)
726{
727    void                    *Mem;
728
729
730    Mem = (void *) calloc (1, (size_t) size);
731    return (Mem);
732}
733#endif
734
735
736/******************************************************************************
737 *
738 * FUNCTION:    AcpiOsFree
739 *
740 * PARAMETERS:  mem                 - Pointer to previously allocated memory
741 *
742 * RETURN:      None.
743 *
744 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
745 *
746 *****************************************************************************/
747
748void
749AcpiOsFree (
750    void                    *mem)
751{
752
753    free (mem);
754}
755
756
757#ifdef ACPI_SINGLE_THREADED
758/******************************************************************************
759 *
760 * FUNCTION:    Semaphore stub functions
761 *
762 * DESCRIPTION: Stub functions used for single-thread applications that do
763 *              not require semaphore synchronization. Full implementations
764 *              of these functions appear after the stubs.
765 *
766 *****************************************************************************/
767
768ACPI_STATUS
769AcpiOsCreateSemaphore (
770    UINT32              MaxUnits,
771    UINT32              InitialUnits,
772    ACPI_HANDLE         *OutHandle)
773{
774    *OutHandle = (ACPI_HANDLE) 1;
775    return (AE_OK);
776}
777
778ACPI_STATUS
779AcpiOsDeleteSemaphore (
780    ACPI_HANDLE         Handle)
781{
782    return (AE_OK);
783}
784
785ACPI_STATUS
786AcpiOsWaitSemaphore (
787    ACPI_HANDLE         Handle,
788    UINT32              Units,
789    UINT16              Timeout)
790{
791    return (AE_OK);
792}
793
794ACPI_STATUS
795AcpiOsSignalSemaphore (
796    ACPI_HANDLE         Handle,
797    UINT32              Units)
798{
799    return (AE_OK);
800}
801
802#else
803/******************************************************************************
804 *
805 * FUNCTION:    AcpiOsCreateSemaphore
806 *
807 * PARAMETERS:  InitialUnits        - Units to be assigned to the new semaphore
808 *              OutHandle           - Where a handle will be returned
809 *
810 * RETURN:      Status
811 *
812 * DESCRIPTION: Create an OS semaphore
813 *
814 *****************************************************************************/
815
816ACPI_STATUS
817AcpiOsCreateSemaphore (
818    UINT32              MaxUnits,
819    UINT32              InitialUnits,
820    ACPI_HANDLE         *OutHandle)
821{
822    sem_t               *Sem;
823
824
825    if (!OutHandle)
826    {
827        return (AE_BAD_PARAMETER);
828    }
829
830#ifdef __APPLE__
831    {
832        char            *SemaphoreName = tmpnam (NULL);
833
834        Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
835        if (!Sem)
836        {
837            return (AE_NO_MEMORY);
838        }
839        sem_unlink (SemaphoreName); /* This just deletes the name */
840    }
841
842#else
843    Sem = AcpiOsAllocate (sizeof (sem_t));
844    if (!Sem)
845    {
846        return (AE_NO_MEMORY);
847    }
848
849    if (sem_init (Sem, 0, InitialUnits) == -1)
850    {
851        AcpiOsFree (Sem);
852        return (AE_BAD_PARAMETER);
853    }
854#endif
855
856    *OutHandle = (ACPI_HANDLE) Sem;
857    return (AE_OK);
858}
859
860
861/******************************************************************************
862 *
863 * FUNCTION:    AcpiOsDeleteSemaphore
864 *
865 * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
866 *
867 * RETURN:      Status
868 *
869 * DESCRIPTION: Delete an OS semaphore
870 *
871 *****************************************************************************/
872
873ACPI_STATUS
874AcpiOsDeleteSemaphore (
875    ACPI_HANDLE         Handle)
876{
877    sem_t               *Sem = (sem_t *) Handle;
878
879
880    if (!Sem)
881    {
882        return (AE_BAD_PARAMETER);
883    }
884
885    if (sem_destroy (Sem) == -1)
886    {
887        return (AE_BAD_PARAMETER);
888    }
889
890    return (AE_OK);
891}
892
893
894/******************************************************************************
895 *
896 * FUNCTION:    AcpiOsWaitSemaphore
897 *
898 * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
899 *              Units               - How many units to wait for
900 *              MsecTimeout         - How long to wait (milliseconds)
901 *
902 * RETURN:      Status
903 *
904 * DESCRIPTION: Wait for units
905 *
906 *****************************************************************************/
907
908ACPI_STATUS
909AcpiOsWaitSemaphore (
910    ACPI_HANDLE         Handle,
911    UINT32              Units,
912    UINT16              MsecTimeout)
913{
914    ACPI_STATUS         Status = AE_OK;
915    sem_t               *Sem = (sem_t *) Handle;
916#ifndef ACPI_USE_ALTERNATE_TIMEOUT
917    struct timespec     Time;
918    int                 RetVal;
919#endif
920
921
922    if (!Sem)
923    {
924        return (AE_BAD_PARAMETER);
925    }
926
927    switch (MsecTimeout)
928    {
929    /*
930     * No Wait:
931     * --------
932     * A zero timeout value indicates that we shouldn't wait - just
933     * acquire the semaphore if available otherwise return AE_TIME
934     * (a.k.a. 'would block').
935     */
936    case 0:
937
938        if (sem_trywait(Sem) == -1)
939        {
940            Status = (AE_TIME);
941        }
942        break;
943
944    /* Wait Indefinitely */
945
946    case ACPI_WAIT_FOREVER:
947
948        if (sem_wait (Sem))
949        {
950            Status = (AE_TIME);
951        }
952        break;
953
954    /* Wait with MsecTimeout */
955
956    default:
957
958#ifdef ACPI_USE_ALTERNATE_TIMEOUT
959        /*
960         * Alternate timeout mechanism for environments where
961         * sem_timedwait is not available or does not work properly.
962         */
963        while (MsecTimeout)
964        {
965            if (sem_trywait (Sem) == 0)
966            {
967                /* Got the semaphore */
968                return (AE_OK);
969            }
970
971            if (MsecTimeout >= 10)
972            {
973                MsecTimeout -= 10;
974                usleep (10 * ACPI_USEC_PER_MSEC); /* ten milliseconds */
975            }
976            else
977            {
978                MsecTimeout--;
979                usleep (ACPI_USEC_PER_MSEC); /* one millisecond */
980            }
981        }
982        Status = (AE_TIME);
983#else
984        /*
985         * The interface to sem_timedwait is an absolute time, so we need to
986         * get the current time, then add in the millisecond Timeout value.
987         */
988        if (clock_gettime (CLOCK_REALTIME, &Time) == -1)
989        {
990            perror ("clock_gettime");
991            return (AE_TIME);
992        }
993
994        Time.tv_sec += (MsecTimeout / ACPI_MSEC_PER_SEC);
995        Time.tv_nsec += ((MsecTimeout % ACPI_MSEC_PER_SEC) * ACPI_NSEC_PER_MSEC);
996
997        /* Handle nanosecond overflow (field must be less than one second) */
998
999        if (Time.tv_nsec >= ACPI_NSEC_PER_SEC)
1000        {
1001            Time.tv_sec += (Time.tv_nsec / ACPI_NSEC_PER_SEC);
1002            Time.tv_nsec = (Time.tv_nsec % ACPI_NSEC_PER_SEC);
1003        }
1004
1005        while (((RetVal = sem_timedwait (Sem, &Time)) == -1) && (errno == EINTR))
1006        {
1007            continue;
1008        }
1009
1010        if (RetVal != 0)
1011        {
1012            if (errno != ETIMEDOUT)
1013            {
1014                perror ("sem_timedwait");
1015            }
1016            Status = (AE_TIME);
1017        }
1018#endif
1019        break;
1020    }
1021
1022    return (Status);
1023}
1024
1025
1026/******************************************************************************
1027 *
1028 * FUNCTION:    AcpiOsSignalSemaphore
1029 *
1030 * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
1031 *              Units               - Number of units to send
1032 *
1033 * RETURN:      Status
1034 *
1035 * DESCRIPTION: Send units
1036 *
1037 *****************************************************************************/
1038
1039ACPI_STATUS
1040AcpiOsSignalSemaphore (
1041    ACPI_HANDLE         Handle,
1042    UINT32              Units)
1043{
1044    sem_t               *Sem = (sem_t *)Handle;
1045
1046
1047    if (!Sem)
1048    {
1049        return (AE_BAD_PARAMETER);
1050    }
1051
1052    if (sem_post (Sem) == -1)
1053    {
1054        return (AE_LIMIT);
1055    }
1056
1057    return (AE_OK);
1058}
1059
1060#endif /* ACPI_SINGLE_THREADED */
1061
1062
1063/******************************************************************************
1064 *
1065 * FUNCTION:    Spinlock interfaces
1066 *
1067 * DESCRIPTION: Map these interfaces to semaphore interfaces
1068 *
1069 *****************************************************************************/
1070
1071ACPI_STATUS
1072AcpiOsCreateLock (
1073    ACPI_SPINLOCK           *OutHandle)
1074{
1075
1076    return (AcpiOsCreateSemaphore (1, 1, OutHandle));
1077}
1078
1079
1080void
1081AcpiOsDeleteLock (
1082    ACPI_SPINLOCK           Handle)
1083{
1084    AcpiOsDeleteSemaphore (Handle);
1085}
1086
1087
1088ACPI_CPU_FLAGS
1089AcpiOsAcquireLock (
1090    ACPI_HANDLE             Handle)
1091{
1092    AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
1093    return (0);
1094}
1095
1096
1097void
1098AcpiOsReleaseLock (
1099    ACPI_SPINLOCK           Handle,
1100    ACPI_CPU_FLAGS          Flags)
1101{
1102    AcpiOsSignalSemaphore (Handle, 1);
1103}
1104
1105
1106/******************************************************************************
1107 *
1108 * FUNCTION:    AcpiOsInstallInterruptHandler
1109 *
1110 * PARAMETERS:  InterruptNumber     - Level handler should respond to.
1111 *              Isr                 - Address of the ACPI interrupt handler
1112 *              ExceptPtr           - Where status is returned
1113 *
1114 * RETURN:      Handle to the newly installed handler.
1115 *
1116 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
1117 *              OS-independent handler.
1118 *
1119 *****************************************************************************/
1120
1121UINT32
1122AcpiOsInstallInterruptHandler (
1123    UINT32                  InterruptNumber,
1124    ACPI_OSD_HANDLER        ServiceRoutine,
1125    void                    *Context)
1126{
1127
1128    return (AE_OK);
1129}
1130
1131
1132/******************************************************************************
1133 *
1134 * FUNCTION:    AcpiOsRemoveInterruptHandler
1135 *
1136 * PARAMETERS:  Handle              - Returned when handler was installed
1137 *
1138 * RETURN:      Status
1139 *
1140 * DESCRIPTION: Uninstalls an interrupt handler.
1141 *
1142 *****************************************************************************/
1143
1144ACPI_STATUS
1145AcpiOsRemoveInterruptHandler (
1146    UINT32                  InterruptNumber,
1147    ACPI_OSD_HANDLER        ServiceRoutine)
1148{
1149
1150    return (AE_OK);
1151}
1152
1153
1154/******************************************************************************
1155 *
1156 * FUNCTION:    AcpiOsStall
1157 *
1158 * PARAMETERS:  microseconds        - Time to sleep
1159 *
1160 * RETURN:      Blocks until sleep is completed.
1161 *
1162 * DESCRIPTION: Sleep at microsecond granularity
1163 *
1164 *****************************************************************************/
1165
1166void
1167AcpiOsStall (
1168    UINT32                  microseconds)
1169{
1170
1171    if (microseconds)
1172    {
1173        usleep (microseconds);
1174    }
1175}
1176
1177
1178/******************************************************************************
1179 *
1180 * FUNCTION:    AcpiOsSleep
1181 *
1182 * PARAMETERS:  milliseconds        - Time to sleep
1183 *
1184 * RETURN:      Blocks until sleep is completed.
1185 *
1186 * DESCRIPTION: Sleep at millisecond granularity
1187 *
1188 *****************************************************************************/
1189
1190void
1191AcpiOsSleep (
1192    UINT64                  milliseconds)
1193{
1194
1195    /* Sleep for whole seconds */
1196
1197    sleep (milliseconds / ACPI_MSEC_PER_SEC);
1198
1199    /*
1200     * Sleep for remaining microseconds.
1201     * Arg to usleep() is in usecs and must be less than 1,000,000 (1 second).
1202     */
1203    usleep ((milliseconds % ACPI_MSEC_PER_SEC) * ACPI_USEC_PER_MSEC);
1204}
1205
1206
1207/******************************************************************************
1208 *
1209 * FUNCTION:    AcpiOsGetTimer
1210 *
1211 * PARAMETERS:  None
1212 *
1213 * RETURN:      Current time in 100 nanosecond units
1214 *
1215 * DESCRIPTION: Get the current system time
1216 *
1217 *****************************************************************************/
1218
1219UINT64
1220AcpiOsGetTimer (
1221    void)
1222{
1223    struct timeval          time;
1224
1225
1226    /* This timer has sufficient resolution for user-space application code */
1227
1228    gettimeofday (&time, NULL);
1229
1230    /* (Seconds * 10^7 = 100ns(10^-7)) + (Microseconds(10^-6) * 10^1 = 100ns) */
1231
1232    return (((UINT64) time.tv_sec * ACPI_100NSEC_PER_SEC) +
1233            ((UINT64) time.tv_usec * ACPI_100NSEC_PER_USEC));
1234}
1235
1236
1237/******************************************************************************
1238 *
1239 * FUNCTION:    AcpiOsReadPciConfiguration
1240 *
1241 * PARAMETERS:  PciId               - Seg/Bus/Dev
1242 *              PciRegister         - Device Register
1243 *              Value               - Buffer where value is placed
1244 *              Width               - Number of bits
1245 *
1246 * RETURN:      Status
1247 *
1248 * DESCRIPTION: Read data from PCI configuration space
1249 *
1250 *****************************************************************************/
1251
1252ACPI_STATUS
1253AcpiOsReadPciConfiguration (
1254    ACPI_PCI_ID             *PciId,
1255    UINT32                  PciRegister,
1256    UINT64                  *Value,
1257    UINT32                  Width)
1258{
1259
1260    *Value = 0;
1261    return (AE_OK);
1262}
1263
1264
1265/******************************************************************************
1266 *
1267 * FUNCTION:    AcpiOsWritePciConfiguration
1268 *
1269 * PARAMETERS:  PciId               - Seg/Bus/Dev
1270 *              PciRegister         - Device Register
1271 *              Value               - Value to be written
1272 *              Width               - Number of bits
1273 *
1274 * RETURN:      Status.
1275 *
1276 * DESCRIPTION: Write data to PCI configuration space
1277 *
1278 *****************************************************************************/
1279
1280ACPI_STATUS
1281AcpiOsWritePciConfiguration (
1282    ACPI_PCI_ID             *PciId,
1283    UINT32                  PciRegister,
1284    UINT64                  Value,
1285    UINT32                  Width)
1286{
1287
1288    return (AE_OK);
1289}
1290
1291
1292/******************************************************************************
1293 *
1294 * FUNCTION:    AcpiOsReadPort
1295 *
1296 * PARAMETERS:  Address             - Address of I/O port/register to read
1297 *              Value               - Where value is placed
1298 *              Width               - Number of bits
1299 *
1300 * RETURN:      Value read from port
1301 *
1302 * DESCRIPTION: Read data from an I/O port or register
1303 *
1304 *****************************************************************************/
1305
1306ACPI_STATUS
1307AcpiOsReadPort (
1308    ACPI_IO_ADDRESS         Address,
1309    UINT32                  *Value,
1310    UINT32                  Width)
1311{
1312
1313    switch (Width)
1314    {
1315    case 8:
1316
1317        *Value = 0xFF;
1318        break;
1319
1320    case 16:
1321
1322        *Value = 0xFFFF;
1323        break;
1324
1325    case 32:
1326
1327        *Value = 0xFFFFFFFF;
1328        break;
1329
1330    default:
1331
1332        return (AE_BAD_PARAMETER);
1333    }
1334
1335    return (AE_OK);
1336}
1337
1338
1339/******************************************************************************
1340 *
1341 * FUNCTION:    AcpiOsWritePort
1342 *
1343 * PARAMETERS:  Address             - Address of I/O port/register to write
1344 *              Value               - Value to write
1345 *              Width               - Number of bits
1346 *
1347 * RETURN:      None
1348 *
1349 * DESCRIPTION: Write data to an I/O port or register
1350 *
1351 *****************************************************************************/
1352
1353ACPI_STATUS
1354AcpiOsWritePort (
1355    ACPI_IO_ADDRESS         Address,
1356    UINT32                  Value,
1357    UINT32                  Width)
1358{
1359
1360    return (AE_OK);
1361}
1362
1363
1364/******************************************************************************
1365 *
1366 * FUNCTION:    AcpiOsReadMemory
1367 *
1368 * PARAMETERS:  Address             - Physical Memory Address to read
1369 *              Value               - Where value is placed
1370 *              Width               - Number of bits (8,16,32, or 64)
1371 *
1372 * RETURN:      Value read from physical memory address. Always returned
1373 *              as a 64-bit integer, regardless of the read width.
1374 *
1375 * DESCRIPTION: Read data from a physical memory address
1376 *
1377 *****************************************************************************/
1378
1379ACPI_STATUS
1380AcpiOsReadMemory (
1381    ACPI_PHYSICAL_ADDRESS   Address,
1382    UINT64                  *Value,
1383    UINT32                  Width)
1384{
1385
1386    switch (Width)
1387    {
1388    case 8:
1389    case 16:
1390    case 32:
1391    case 64:
1392
1393        *Value = 0;
1394        break;
1395
1396    default:
1397
1398        return (AE_BAD_PARAMETER);
1399    }
1400    return (AE_OK);
1401}
1402
1403
1404/******************************************************************************
1405 *
1406 * FUNCTION:    AcpiOsWriteMemory
1407 *
1408 * PARAMETERS:  Address             - Physical Memory Address to write
1409 *              Value               - Value to write
1410 *              Width               - Number of bits (8,16,32, or 64)
1411 *
1412 * RETURN:      None
1413 *
1414 * DESCRIPTION: Write data to a physical memory address
1415 *
1416 *****************************************************************************/
1417
1418ACPI_STATUS
1419AcpiOsWriteMemory (
1420    ACPI_PHYSICAL_ADDRESS   Address,
1421    UINT64                  Value,
1422    UINT32                  Width)
1423{
1424
1425    return (AE_OK);
1426}
1427
1428
1429/******************************************************************************
1430 *
1431 * FUNCTION:    AcpiOsReadable
1432 *
1433 * PARAMETERS:  Pointer             - Area to be verified
1434 *              Length              - Size of area
1435 *
1436 * RETURN:      TRUE if readable for entire length
1437 *
1438 * DESCRIPTION: Verify that a pointer is valid for reading
1439 *
1440 *****************************************************************************/
1441
1442BOOLEAN
1443AcpiOsReadable (
1444    void                    *Pointer,
1445    ACPI_SIZE               Length)
1446{
1447
1448    return (TRUE);
1449}
1450
1451
1452/******************************************************************************
1453 *
1454 * FUNCTION:    AcpiOsWritable
1455 *
1456 * PARAMETERS:  Pointer             - Area to be verified
1457 *              Length              - Size of area
1458 *
1459 * RETURN:      TRUE if writable for entire length
1460 *
1461 * DESCRIPTION: Verify that a pointer is valid for writing
1462 *
1463 *****************************************************************************/
1464
1465BOOLEAN
1466AcpiOsWritable (
1467    void                    *Pointer,
1468    ACPI_SIZE               Length)
1469{
1470
1471    return (TRUE);
1472}
1473
1474
1475/******************************************************************************
1476 *
1477 * FUNCTION:    AcpiOsSignal
1478 *
1479 * PARAMETERS:  Function            - ACPI A signal function code
1480 *              Info                - Pointer to function-dependent structure
1481 *
1482 * RETURN:      Status
1483 *
1484 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1485 *
1486 *****************************************************************************/
1487
1488ACPI_STATUS
1489AcpiOsSignal (
1490    UINT32                  Function,
1491    void                    *Info)
1492{
1493
1494    switch (Function)
1495    {
1496    case ACPI_SIGNAL_FATAL:
1497
1498        break;
1499
1500    case ACPI_SIGNAL_BREAKPOINT:
1501
1502        break;
1503
1504    default:
1505
1506        break;
1507    }
1508
1509    return (AE_OK);
1510}
1511
1512/* Optional multi-thread support */
1513
1514#ifndef ACPI_SINGLE_THREADED
1515/******************************************************************************
1516 *
1517 * FUNCTION:    AcpiOsGetThreadId
1518 *
1519 * PARAMETERS:  None
1520 *
1521 * RETURN:      Id of the running thread
1522 *
1523 * DESCRIPTION: Get the ID of the current (running) thread
1524 *
1525 *****************************************************************************/
1526
1527ACPI_THREAD_ID
1528AcpiOsGetThreadId (
1529    void)
1530{
1531    pthread_t               thread;
1532
1533
1534    thread = pthread_self();
1535    return (ACPI_CAST_PTHREAD_T (thread));
1536}
1537
1538
1539/******************************************************************************
1540 *
1541 * FUNCTION:    AcpiOsExecute
1542 *
1543 * PARAMETERS:  Type                - Type of execution
1544 *              Function            - Address of the function to execute
1545 *              Context             - Passed as a parameter to the function
1546 *
1547 * RETURN:      Status.
1548 *
1549 * DESCRIPTION: Execute a new thread
1550 *
1551 *****************************************************************************/
1552
1553ACPI_STATUS
1554AcpiOsExecute (
1555    ACPI_EXECUTE_TYPE       Type,
1556    ACPI_OSD_EXEC_CALLBACK  Function,
1557    void                    *Context)
1558{
1559    pthread_t               thread;
1560    int                     ret;
1561
1562
1563    ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
1564    if (ret)
1565    {
1566        AcpiOsPrintf("Create thread failed");
1567    }
1568    return (0);
1569}
1570
1571#else /* ACPI_SINGLE_THREADED */
1572ACPI_THREAD_ID
1573AcpiOsGetThreadId (
1574    void)
1575{
1576    return (1);
1577}
1578
1579ACPI_STATUS
1580AcpiOsExecute (
1581    ACPI_EXECUTE_TYPE       Type,
1582    ACPI_OSD_EXEC_CALLBACK  Function,
1583    void                    *Context)
1584{
1585
1586    Function (Context);
1587
1588    return (AE_OK);
1589}
1590
1591#endif /* ACPI_SINGLE_THREADED */
1592
1593
1594/******************************************************************************
1595 *
1596 * FUNCTION:    AcpiOsWaitEventsComplete
1597 *
1598 * PARAMETERS:  None
1599 *
1600 * RETURN:      None
1601 *
1602 * DESCRIPTION: Wait for all asynchronous events to complete. This
1603 *              implementation does nothing.
1604 *
1605 *****************************************************************************/
1606
1607void
1608AcpiOsWaitEventsComplete (
1609    void)
1610{
1611    return;
1612}
1613