osunixxf.c revision 227896
11556Srgrimes/******************************************************************************
21556Srgrimes *
31556Srgrimes * Module Name: osunixxf - UNIX OSL interfaces
41556Srgrimes *
51556Srgrimes *****************************************************************************/
61556Srgrimes
71556Srgrimes/*
81556Srgrimes * Copyright (C) 2000 - 2011, Intel Corp.
91556Srgrimes * All rights reserved.
101556Srgrimes *
111556Srgrimes * Redistribution and use in source and binary forms, with or without
121556Srgrimes * modification, are permitted provided that the following conditions
131556Srgrimes * are met:
141556Srgrimes * 1. Redistributions of source code must retain the above copyright
151556Srgrimes *    notice, this list of conditions, and the following disclaimer,
161556Srgrimes *    without modification.
171556Srgrimes * 2. Redistributions in binary form must reproduce at minimum a disclaimer
181556Srgrimes *    substantially similar to the "NO WARRANTY" disclaimer below
191556Srgrimes *    ("Disclaimer") and any redistribution must be conditioned upon
201556Srgrimes *    including a substantially similar Disclaimer requirement for further
211556Srgrimes *    binary redistribution.
221556Srgrimes * 3. Neither the names of the above-listed copyright holders nor the names
231556Srgrimes *    of any contributors may be used to endorse or promote products derived
241556Srgrimes *    from this software without specific prior written permission.
251556Srgrimes *
261556Srgrimes * Alternatively, this software may be distributed under the terms of the
271556Srgrimes * GNU General Public License ("GPL") version 2 as published by the Free
281556Srgrimes * Software Foundation.
291556Srgrimes *
301556Srgrimes * NO WARRANTY
311556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
321556Srgrimes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
331556Srgrimes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3436150Scharnier * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3536150Scharnier * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3636150Scharnier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
371556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3899110Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3999110Sobrien * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
401556Srgrimes * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4117987Speter * POSSIBILITY OF SUCH DAMAGES.
4217987Speter */
4317987Speter
4420425Ssteve
4517987Speter/*
4617987Speter * These interfaces are required in order to compile the ASL compiler and the
47100661Stjr * various ACPICA tools under Linux or other Unix-like system.
4817987Speter *
491556Srgrimes * Note: Use #define __APPLE__ for OS X generation.
501556Srgrimes */
511556Srgrimes#include "acpi.h"
521556Srgrimes#include "accommon.h"
531556Srgrimes#include "amlcode.h"
541556Srgrimes#include "acparser.h"
551556Srgrimes#include "acdebug.h"
561556Srgrimes
571556Srgrimes#include <stdio.h>
581556Srgrimes#include <stdlib.h>
591556Srgrimes#include <stdarg.h>
601556Srgrimes#include <unistd.h>
6120425Ssteve#include <sys/time.h>
6217987Speter#include <semaphore.h>
631556Srgrimes#include <pthread.h>
6417987Speter#include <errno.h>
6520425Ssteve
661556Srgrimes#define _COMPONENT          ACPI_OS_SERVICES
6797092Stjr        ACPI_MODULE_NAME    ("osunixxf")
6897092Stjr
6997092Stjr
7090111Simpextern FILE                    *AcpiGbl_DebugFile;
71176521SstefanfFILE                           *AcpiGbl_OutputFile;
72176521Sstefanf
73199631Sstefanf
741556Srgrimes/* Upcalls to AcpiExec */
75117261Sdds
76117261SddsACPI_PHYSICAL_ADDRESS
771556SrgrimesAeLocalGetRootPointer (
781556Srgrimes    void);
791556Srgrimes
8097092Stjrvoid
8117987SpeterAeTableOverride (
821556Srgrimes    ACPI_TABLE_HEADER       *ExistingTable,
831556Srgrimes    ACPI_TABLE_HEADER       **NewTable);
841556Srgrimes
851556Srgrimestypedef void* (*PTHREAD_CALLBACK) (void *);
8697092Stjr
871556Srgrimes/* Apple-specific */
88100663Stjr
89100664Stjr#ifdef __APPLE__
9097092Stjr#define sem_destroy         sem_close
9197092Stjr#endif
9297092Stjr
9397092Stjr
9497092Stjr/******************************************************************************
9597092Stjr *
9697092Stjr * FUNCTION:    AcpiOsInitialize, AcpiOsTerminate
9797092Stjr *
9897092Stjr * PARAMETERS:  None
9997092Stjr *
10097092Stjr * RETURN:      Status
10197092Stjr *
10297092Stjr * DESCRIPTION: Init and terminate. Nothing to do.
10397092Stjr *
10497092Stjr *****************************************************************************/
10597092Stjr
10697092StjrACPI_STATUS
10797092StjrAcpiOsInitialize (
10897092Stjr    void)
10997092Stjr{
1101556Srgrimes
1115234Sbde    AcpiGbl_OutputFile = stdout;
1125234Sbde    return (AE_OK);
1131556Srgrimes}
1141556Srgrimes
11512273Speter
11612273SpeterACPI_STATUS
11712273SpeterAcpiOsTerminate (
11812273Speter    void)
1191556Srgrimes{
1201556Srgrimes
1211556Srgrimes    return (AE_OK);
1221556Srgrimes}
12317987Speter
1241556Srgrimes
1251556Srgrimes/******************************************************************************
1261556Srgrimes *
1271556Srgrimes * FUNCTION:    AcpiOsGetRootPointer
12838886Stegge *
129159551Sstefanf * PARAMETERS:  None
130159551Sstefanf *
131159551Sstefanf * RETURN:      RSDP physical address
1321556Srgrimes *
13397092Stjr * DESCRIPTION: Gets the ACPI root pointer (RSDP)
1341556Srgrimes *
1351556Srgrimes *****************************************************************************/
1361556Srgrimes
1371556SrgrimesACPI_PHYSICAL_ADDRESS
13817987SpeterAcpiOsGetRootPointer (
13917987Speter    void)
1401556Srgrimes{
1411556Srgrimes
1421556Srgrimes    return (AeLocalGetRootPointer ());
1431556Srgrimes}
14497092Stjr
14520774Ssteve
1461556Srgrimes/******************************************************************************
1471556Srgrimes *
14897092Stjr * FUNCTION:    AcpiOsPredefinedOverride
14920425Ssteve *
15097092Stjr * PARAMETERS:  InitVal             - Initial value of the predefined object
15197092Stjr *              NewVal              - The new value for the object
15297092Stjr *
15397092Stjr * RETURN:      Status, pointer to value. Null pointer returned if not
15497092Stjr *              overriding.
15597092Stjr *
15697092Stjr * DESCRIPTION: Allow the OS to override predefined names
15797092Stjr *
15897092Stjr *****************************************************************************/
15997092Stjr
16097092StjrACPI_STATUS
16197092StjrAcpiOsPredefinedOverride (
16297092Stjr    const ACPI_PREDEFINED_NAMES *InitVal,
16397092Stjr    ACPI_STRING                 *NewVal)
16497092Stjr{
16597092Stjr
16638886Stegge    if (!InitVal || !NewVal)
16738886Stegge    {
16838886Stegge        return (AE_BAD_PARAMETER);
16938886Stegge    }
17038886Stegge
17138886Stegge    *NewVal = NULL;
17220425Ssteve    return (AE_OK);
17338886Stegge}
17438886Stegge
17538886Stegge
17638886Stegge/******************************************************************************
17738886Stegge *
17838886Stegge * FUNCTION:    AcpiOsTableOverride
17938886Stegge *
18038886Stegge * PARAMETERS:  ExistingTable       - Header of current table (probably
18138886Stegge *                                    firmware)
18238886Stegge *              NewTable            - Where an entire new table is returned.
18338886Stegge *
18438886Stegge * RETURN:      Status, pointer to new table. Null pointer returned if no
18538886Stegge *              table is available to override
18638886Stegge *
18738886Stegge * DESCRIPTION: Return a different version of a table if one is available
18838886Stegge *
18938886Stegge *****************************************************************************/
19038886Stegge
19138886SteggeACPI_STATUS
19238886SteggeAcpiOsTableOverride (
19338886Stegge    ACPI_TABLE_HEADER       *ExistingTable,
19438886Stegge    ACPI_TABLE_HEADER       **NewTable)
19538886Stegge{
19638886Stegge
19738886Stegge    if (!ExistingTable || !NewTable)
19838886Stegge    {
19997092Stjr        return (AE_BAD_PARAMETER);
20038886Stegge    }
20138886Stegge
20238886Stegge    *NewTable = NULL;
20338886Stegge
20438886Stegge#ifdef ACPI_EXEC_APP
2051556Srgrimes
206176521Sstefanf    AeTableOverride (ExistingTable, NewTable);
2071556Srgrimes    return (AE_OK);
20897092Stjr#else
2091556Srgrimes
210176521Sstefanf    return (AE_NO_ACPI_TABLES);
2111556Srgrimes#endif
21297092Stjr}
2131556Srgrimes
2141556Srgrimes
21597092Stjr/******************************************************************************
21697092Stjr *
21797092Stjr * FUNCTION:    AcpiOsRedirectOutput
218176521Sstefanf *
2191556Srgrimes * PARAMETERS:  Destination         - An open file handle/pointer
22097092Stjr *
221176521Sstefanf * RETURN:      None
22297092Stjr *
22397092Stjr * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
22497092Stjr *
225176521Sstefanf *****************************************************************************/
22697092Stjr
22797092Stjrvoid
22897092StjrAcpiOsRedirectOutput (
22997092Stjr    void                    *Destination)
2301556Srgrimes{
2311556Srgrimes
2321556Srgrimes    AcpiGbl_OutputFile = Destination;
2331556Srgrimes}
2341556Srgrimes
23590111Simp
23620774Ssteve/******************************************************************************
23725222Ssteve *
2381556Srgrimes * FUNCTION:    AcpiOsPrintf
2391556Srgrimes *
2401556Srgrimes * PARAMETERS:  fmt, ...            - Standard printf format
2411556Srgrimes *
2421556Srgrimes * RETURN:      None
2431556Srgrimes *
2441556Srgrimes * DESCRIPTION: Formatted output
2451556Srgrimes *
2461556Srgrimes *****************************************************************************/
2471556Srgrimes
2481556Srgrimesvoid ACPI_INTERNAL_VAR_XFACE
2491556SrgrimesAcpiOsPrintf (
2501556Srgrimes    const char              *Fmt,
2511556Srgrimes    ...)
2521556Srgrimes{
2531556Srgrimes    va_list                 Args;
2541556Srgrimes
255176521Sstefanf
256176521Sstefanf    va_start (Args, Fmt);
25720774Ssteve    AcpiOsVprintf (Fmt, Args);
2581556Srgrimes    va_end (Args);
2591556Srgrimes}
2601556Srgrimes
26138886Stegge
26238886Stegge/******************************************************************************
26338886Stegge *
26438886Stegge * FUNCTION:    AcpiOsVprintf
26538886Stegge *
266199631Sstefanf * PARAMETERS:  fmt                 - Standard printf format
267199631Sstefanf *              args                - Argument list
2681556Srgrimes *
2691556Srgrimes * RETURN:      None
2701556Srgrimes *
2711556Srgrimes * DESCRIPTION: Formatted output with argument list pointer
2721556Srgrimes *
2731556Srgrimes *****************************************************************************/
2741556Srgrimes
2751556Srgrimesvoid
2761556SrgrimesAcpiOsVprintf (
2771556Srgrimes    const char              *Fmt,
2781556Srgrimes    va_list                 Args)
2791556Srgrimes{
2801556Srgrimes    UINT8                   Flags;
2811556Srgrimes
2821556Srgrimes
2831556Srgrimes    Flags = AcpiGbl_DbOutputFlags;
2841556Srgrimes    if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
2851556Srgrimes    {
2861556Srgrimes        /* Output is directable to either a file (if open) or the console */
2871556Srgrimes
2881556Srgrimes        if (AcpiGbl_DebugFile)
2891556Srgrimes        {
290176521Sstefanf            /* Output file is open, send the output there */
291176521Sstefanf
292176521Sstefanf            vfprintf (AcpiGbl_DebugFile, Fmt, Args);
293176521Sstefanf        }
294176521Sstefanf        else
295176521Sstefanf        {
296176521Sstefanf            /* No redirection, send output to console (once only!) */
297176521Sstefanf
298176521Sstefanf            Flags |= ACPI_DB_CONSOLE_OUTPUT;
299176521Sstefanf        }
300176521Sstefanf    }
301176521Sstefanf
302176521Sstefanf    if (Flags & ACPI_DB_CONSOLE_OUTPUT)
30338886Stegge    {
30438886Stegge        vfprintf (AcpiGbl_OutputFile, Fmt, Args);
30538886Stegge    }
306176521Sstefanf}
30786176Stegge
30886176Stegge
3091556Srgrimes/******************************************************************************
3101556Srgrimes *
3111556Srgrimes * FUNCTION:    AcpiOsGetLine
312100660Stjr *
31317987Speter * PARAMETERS:  Buffer              - Where to return the command line
314199631Sstefanf *              BufferLength        - Maximum length of Buffer
31597092Stjr *              BytesRead           - Where the actual byte count is returned
3161556Srgrimes *
317100663Stjr * RETURN:      Status and actual bytes read
318100664Stjr *
31997092Stjr * DESCRIPTION: Formatted input with argument list pointer
32097092Stjr *
32197092Stjr *****************************************************************************/
32297092Stjr
32397092StjrACPI_STATUS
32497092StjrAcpiOsGetLine (
32597092Stjr    char                    *Buffer,
32697092Stjr    UINT32                  BufferLength,
32797092Stjr    UINT32                  *BytesRead)
32897092Stjr{
32997092Stjr    UINT8                   Temp;
33097092Stjr    UINT32                  i;
33197092Stjr
33297092Stjr
33397092Stjr    for (i = 0; ; i++)
3341556Srgrimes    {
33597092Stjr        if (i >= BufferLength)
33697092Stjr        {
33738886Stegge            return (AE_BUFFER_OVERFLOW);
33897092Stjr        }
33997092Stjr
34097092Stjr        scanf ("%1c", &Temp);
34197092Stjr        if (!Temp || Temp == '\n')
342199631Sstefanf        {
34397092Stjr            break;
344199631Sstefanf        }
34597092Stjr
34697092Stjr        Buffer [i] = Temp;
34738886Stegge    }
34897092Stjr
34997092Stjr    /* Null terminate the buffer */
35038886Stegge
3511556Srgrimes    Buffer [i] = 0;
352176521Sstefanf
3531556Srgrimes    /* Return the number of bytes in the string */
35420774Ssteve
35590111Simp    if (BytesRead)
35620425Ssteve    {
357176521Sstefanf        *BytesRead = i;
35838886Stegge    }
3591556Srgrimes    return (AE_OK);
36038886Stegge}
361176521Sstefanf
362199631Sstefanf
363176521Sstefanf/******************************************************************************
364176521Sstefanf *
365176521Sstefanf * FUNCTION:    AcpiOsMapMemory
366176521Sstefanf *
367176521Sstefanf * PARAMETERS:  where               - Physical address of memory to be mapped
368176521Sstefanf *              length              - How much memory to map
369199631Sstefanf *
370199631Sstefanf * RETURN:      Pointer to mapped memory. Null on error.
371176521Sstefanf *
372176521Sstefanf * DESCRIPTION: Map physical memory into caller's address space
373176521Sstefanf *
374176521Sstefanf *****************************************************************************/
375199631Sstefanf
376176521Sstefanfvoid *
377199631SstefanfAcpiOsMapMemory (
378199631Sstefanf    ACPI_PHYSICAL_ADDRESS   where,
379199631Sstefanf    ACPI_SIZE               length)
38038886Stegge{
381199631Sstefanf
382199631Sstefanf    return (ACPI_TO_POINTER ((ACPI_SIZE) where));
383199631Sstefanf}
384176521Sstefanf
385199631Sstefanf
386199631Sstefanf/******************************************************************************
387199631Sstefanf *
38838886Stegge * FUNCTION:    AcpiOsUnmapMemory
389199631Sstefanf *
390199631Sstefanf * PARAMETERS:  where               - Logical address of memory to be unmapped
391199631Sstefanf *              length              - How much memory to unmap
392199631Sstefanf *
393199631Sstefanf * RETURN:      None.
394199631Sstefanf *
395199631Sstefanf * DESCRIPTION: Delete a previously created mapping. Where and Length must
396199631Sstefanf *              correspond to a previous mapping exactly.
397199631Sstefanf *
3981556Srgrimes *****************************************************************************/
399
400void
401AcpiOsUnmapMemory (
402    void                    *where,
403    ACPI_SIZE               length)
404{
405
406    return;
407}
408
409
410/******************************************************************************
411 *
412 * FUNCTION:    AcpiOsAllocate
413 *
414 * PARAMETERS:  Size                - Amount to allocate, in bytes
415 *
416 * RETURN:      Pointer to the new allocation. Null on error.
417 *
418 * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
419 *
420 *****************************************************************************/
421
422void *
423AcpiOsAllocate (
424    ACPI_SIZE               size)
425{
426    void                    *Mem;
427
428
429    Mem = (void *) malloc ((size_t) size);
430    return (Mem);
431}
432
433
434/******************************************************************************
435 *
436 * FUNCTION:    AcpiOsFree
437 *
438 * PARAMETERS:  mem                 - Pointer to previously allocated memory
439 *
440 * RETURN:      None.
441 *
442 * DESCRIPTION: Free memory allocated via AcpiOsAllocate
443 *
444 *****************************************************************************/
445
446void
447AcpiOsFree (
448    void                    *mem)
449{
450
451    free (mem);
452}
453
454
455#ifdef ACPI_SINGLE_THREADED
456/******************************************************************************
457 *
458 * FUNCTION:    Semaphore stub functions
459 *
460 * DESCRIPTION: Stub functions used for single-thread applications that do
461 *              not require semaphore synchronization. Full implementations
462 *              of these functions appear after the stubs.
463 *
464 *****************************************************************************/
465
466ACPI_STATUS
467AcpiOsCreateSemaphore (
468    UINT32              MaxUnits,
469    UINT32              InitialUnits,
470    ACPI_HANDLE         *OutHandle)
471{
472    *OutHandle = (ACPI_HANDLE) 1;
473    return (AE_OK);
474}
475
476ACPI_STATUS
477AcpiOsDeleteSemaphore (
478    ACPI_HANDLE         Handle)
479{
480    return (AE_OK);
481}
482
483ACPI_STATUS
484AcpiOsWaitSemaphore (
485    ACPI_HANDLE         Handle,
486    UINT32              Units,
487    UINT16              Timeout)
488{
489    return (AE_OK);
490}
491
492ACPI_STATUS
493AcpiOsSignalSemaphore (
494    ACPI_HANDLE         Handle,
495    UINT32              Units)
496{
497    return (AE_OK);
498}
499
500#else
501/******************************************************************************
502 *
503 * FUNCTION:    AcpiOsCreateSemaphore
504 *
505 * PARAMETERS:  InitialUnits        - Units to be assigned to the new semaphore
506 *              OutHandle           - Where a handle will be returned
507 *
508 * RETURN:      Status
509 *
510 * DESCRIPTION: Create an OS semaphore
511 *
512 *****************************************************************************/
513
514ACPI_STATUS
515AcpiOsCreateSemaphore (
516    UINT32              MaxUnits,
517    UINT32              InitialUnits,
518    ACPI_HANDLE         *OutHandle)
519{
520    sem_t               *Sem;
521
522
523    if (!OutHandle)
524    {
525        return (AE_BAD_PARAMETER);
526    }
527
528#ifdef __APPLE__
529    {
530        char            *SemaphoreName = tmpnam (NULL);
531
532        Sem = sem_open (SemaphoreName, O_EXCL|O_CREAT, 0755, InitialUnits);
533        if (!Sem)
534        {
535            return (AE_NO_MEMORY);
536        }
537        sem_unlink (SemaphoreName); /* This just deletes the name */
538    }
539
540#else
541    Sem = AcpiOsAllocate (sizeof (sem_t));
542    if (!Sem)
543    {
544        return (AE_NO_MEMORY);
545    }
546
547    if (sem_init (Sem, 0, InitialUnits) == -1)
548    {
549        AcpiOsFree (Sem);
550        return (AE_BAD_PARAMETER);
551    }
552#endif
553
554    *OutHandle = (ACPI_HANDLE) Sem;
555    return (AE_OK);
556}
557
558
559/******************************************************************************
560 *
561 * FUNCTION:    AcpiOsDeleteSemaphore
562 *
563 * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
564 *
565 * RETURN:      Status
566 *
567 * DESCRIPTION: Delete an OS semaphore
568 *
569 *****************************************************************************/
570
571ACPI_STATUS
572AcpiOsDeleteSemaphore (
573    ACPI_HANDLE         Handle)
574{
575    sem_t               *Sem = (sem_t *) Handle;
576
577
578    if (!Sem)
579    {
580        return (AE_BAD_PARAMETER);
581    }
582
583    if (sem_destroy (Sem) == -1)
584    {
585        return (AE_BAD_PARAMETER);
586    }
587
588    return (AE_OK);
589}
590
591
592/******************************************************************************
593 *
594 * FUNCTION:    AcpiOsWaitSemaphore
595 *
596 * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
597 *              Units               - How many units to wait for
598 *              Timeout             - How long to wait
599 *
600 * RETURN:      Status
601 *
602 * DESCRIPTION: Wait for units
603 *
604 *****************************************************************************/
605
606ACPI_STATUS
607AcpiOsWaitSemaphore (
608    ACPI_HANDLE         Handle,
609    UINT32              Units,
610    UINT16              Timeout)
611{
612    ACPI_STATUS         Status = AE_OK;
613    sem_t               *Sem = (sem_t *) Handle;
614    struct timespec     T;
615
616
617    if (!Sem)
618    {
619        return (AE_BAD_PARAMETER);
620    }
621
622    switch (Timeout)
623    {
624    /*
625     * No Wait:
626     * --------
627     * A zero timeout value indicates that we shouldn't wait - just
628     * acquire the semaphore if available otherwise return AE_TIME
629     * (a.k.a. 'would block').
630     */
631    case 0:
632
633        if (sem_trywait(Sem) == -1)
634        {
635            Status = (AE_TIME);
636        }
637        break;
638
639    /* Wait Indefinitely */
640
641    case ACPI_WAIT_FOREVER:
642
643        if (sem_wait (Sem))
644        {
645            Status = (AE_TIME);
646        }
647        break;
648
649    /* Wait with Timeout */
650
651    default:
652
653        T.tv_sec = Timeout / 1000;
654        T.tv_nsec = (Timeout - (T.tv_sec * 1000)) * 1000000;
655
656#ifdef ACPI_USE_ALTERNATE_TIMEOUT
657        /*
658         * Alternate timeout mechanism for environments where
659         * sem_timedwait is not available or does not work properly.
660         */
661        while (Timeout)
662        {
663            if (sem_trywait (Sem) == 0)
664            {
665                /* Got the semaphore */
666                return (AE_OK);
667            }
668            usleep (1000);  /* one millisecond */
669            Timeout--;
670        }
671        Status = (AE_TIME);
672#else
673
674        if (sem_timedwait (Sem, &T))
675        {
676            Status = (AE_TIME);
677        }
678#endif
679
680        break;
681    }
682
683    return (Status);
684}
685
686
687/******************************************************************************
688 *
689 * FUNCTION:    AcpiOsSignalSemaphore
690 *
691 * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
692 *              Units               - Number of units to send
693 *
694 * RETURN:      Status
695 *
696 * DESCRIPTION: Send units
697 *
698 *****************************************************************************/
699
700ACPI_STATUS
701AcpiOsSignalSemaphore (
702    ACPI_HANDLE         Handle,
703    UINT32              Units)
704{
705    sem_t               *Sem = (sem_t *)Handle;
706
707
708    if (!Sem)
709    {
710        return (AE_BAD_PARAMETER);
711    }
712
713    if (sem_post (Sem) == -1)
714    {
715        return (AE_LIMIT);
716    }
717
718    return (AE_OK);
719}
720
721#endif /* ACPI_SINGLE_THREADED */
722
723
724/******************************************************************************
725 *
726 * FUNCTION:    Spinlock interfaces
727 *
728 * DESCRIPTION: Map these interfaces to semaphore interfaces
729 *
730 *****************************************************************************/
731
732ACPI_STATUS
733AcpiOsCreateLock (
734    ACPI_SPINLOCK           *OutHandle)
735{
736
737    return (AcpiOsCreateSemaphore (1, 1, OutHandle));
738}
739
740
741void
742AcpiOsDeleteLock (
743    ACPI_SPINLOCK           Handle)
744{
745    AcpiOsDeleteSemaphore (Handle);
746}
747
748
749ACPI_CPU_FLAGS
750AcpiOsAcquireLock (
751    ACPI_HANDLE             Handle)
752{
753    AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
754    return (0);
755}
756
757
758void
759AcpiOsReleaseLock (
760    ACPI_SPINLOCK           Handle,
761    ACPI_CPU_FLAGS          Flags)
762{
763    AcpiOsSignalSemaphore (Handle, 1);
764}
765
766
767/******************************************************************************
768 *
769 * FUNCTION:    AcpiOsInstallInterruptHandler
770 *
771 * PARAMETERS:  InterruptNumber     - Level handler should respond to.
772 *              Isr                 - Address of the ACPI interrupt handler
773 *              ExceptPtr           - Where status is returned
774 *
775 * RETURN:      Handle to the newly installed handler.
776 *
777 * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
778 *              OS-independent handler.
779 *
780 *****************************************************************************/
781
782UINT32
783AcpiOsInstallInterruptHandler (
784    UINT32                  InterruptNumber,
785    ACPI_OSD_HANDLER        ServiceRoutine,
786    void                    *Context)
787{
788
789    return (AE_OK);
790}
791
792
793/******************************************************************************
794 *
795 * FUNCTION:    AcpiOsRemoveInterruptHandler
796 *
797 * PARAMETERS:  Handle              - Returned when handler was installed
798 *
799 * RETURN:      Status
800 *
801 * DESCRIPTION: Uninstalls an interrupt handler.
802 *
803 *****************************************************************************/
804
805ACPI_STATUS
806AcpiOsRemoveInterruptHandler (
807    UINT32                  InterruptNumber,
808    ACPI_OSD_HANDLER        ServiceRoutine)
809{
810
811    return (AE_OK);
812}
813
814
815/******************************************************************************
816 *
817 * FUNCTION:    AcpiOsStall
818 *
819 * PARAMETERS:  microseconds        - Time to sleep
820 *
821 * RETURN:      Blocks until sleep is completed.
822 *
823 * DESCRIPTION: Sleep at microsecond granularity
824 *
825 *****************************************************************************/
826
827void
828AcpiOsStall (
829    UINT32                  microseconds)
830{
831
832    if (microseconds)
833    {
834        usleep (microseconds);
835    }
836}
837
838
839/******************************************************************************
840 *
841 * FUNCTION:    AcpiOsSleep
842 *
843 * PARAMETERS:  milliseconds        - Time to sleep
844 *
845 * RETURN:      Blocks until sleep is completed.
846 *
847 * DESCRIPTION: Sleep at millisecond granularity
848 *
849 *****************************************************************************/
850
851void
852AcpiOsSleep (
853    UINT64                  milliseconds)
854{
855
856    sleep (milliseconds / 1000);    /* Sleep for whole seconds */
857
858    /*
859     * Arg to usleep() must be less than 1,000,000 (1 second)
860     */
861    usleep ((milliseconds % 1000) * 1000);      /* Sleep for remaining usecs */
862}
863
864
865/******************************************************************************
866 *
867 * FUNCTION:    AcpiOsGetTimer
868 *
869 * PARAMETERS:  None
870 *
871 * RETURN:      Current time in 100 nanosecond units
872 *
873 * DESCRIPTION: Get the current system time
874 *
875 *****************************************************************************/
876
877UINT64
878AcpiOsGetTimer (
879    void)
880{
881    struct timeval          time;
882
883
884    gettimeofday (&time, NULL);
885
886    /* Seconds * 10^7 = 100ns(10^-7), Microseconds(10^-6) * 10^1 = 100ns */
887
888    return (((UINT64) time.tv_sec * 10000000) + ((UINT64) time.tv_usec * 10));
889}
890
891
892/******************************************************************************
893 *
894 * FUNCTION:    AcpiOsReadPciConfiguration
895 *
896 * PARAMETERS:  PciId               - Seg/Bus/Dev
897 *              Register            - Device Register
898 *              Value               - Buffer where value is placed
899 *              Width               - Number of bits
900 *
901 * RETURN:      Status
902 *
903 * DESCRIPTION: Read data from PCI configuration space
904 *
905 *****************************************************************************/
906
907ACPI_STATUS
908AcpiOsReadPciConfiguration (
909    ACPI_PCI_ID             *PciId,
910    UINT32                  Register,
911    UINT64                  *Value,
912    UINT32                  Width)
913{
914
915    return (AE_OK);
916}
917
918
919/******************************************************************************
920 *
921 * FUNCTION:    AcpiOsWritePciConfiguration
922 *
923 * PARAMETERS:  PciId               - Seg/Bus/Dev
924 *              Register            - Device Register
925 *              Value               - Value to be written
926 *              Width               - Number of bits
927 *
928 * RETURN:      Status.
929 *
930 * DESCRIPTION: Write data to PCI configuration space
931 *
932 *****************************************************************************/
933
934ACPI_STATUS
935AcpiOsWritePciConfiguration (
936    ACPI_PCI_ID             *PciId,
937    UINT32                  Register,
938    UINT64                  Value,
939    UINT32                  Width)
940{
941
942    return (AE_OK);
943}
944
945
946/******************************************************************************
947 *
948 * FUNCTION:    AcpiOsReadPort
949 *
950 * PARAMETERS:  Address             - Address of I/O port/register to read
951 *              Value               - Where value is placed
952 *              Width               - Number of bits
953 *
954 * RETURN:      Value read from port
955 *
956 * DESCRIPTION: Read data from an I/O port or register
957 *
958 *****************************************************************************/
959
960ACPI_STATUS
961AcpiOsReadPort (
962    ACPI_IO_ADDRESS         Address,
963    UINT32                  *Value,
964    UINT32                  Width)
965{
966
967    switch (Width)
968    {
969    case 8:
970        *Value = 0xFF;
971        break;
972
973    case 16:
974        *Value = 0xFFFF;
975        break;
976
977    case 32:
978        *Value = 0xFFFFFFFF;
979        break;
980
981    default:
982        return (AE_BAD_PARAMETER);
983    }
984
985    return (AE_OK);
986}
987
988
989/******************************************************************************
990 *
991 * FUNCTION:    AcpiOsWritePort
992 *
993 * PARAMETERS:  Address             - Address of I/O port/register to write
994 *              Value               - Value to write
995 *              Width               - Number of bits
996 *
997 * RETURN:      None
998 *
999 * DESCRIPTION: Write data to an I/O port or register
1000 *
1001 *****************************************************************************/
1002
1003ACPI_STATUS
1004AcpiOsWritePort (
1005    ACPI_IO_ADDRESS         Address,
1006    UINT32                  Value,
1007    UINT32                  Width)
1008{
1009
1010    return (AE_OK);
1011}
1012
1013
1014/******************************************************************************
1015 *
1016 * FUNCTION:    AcpiOsReadMemory
1017 *
1018 * PARAMETERS:  Address             - Physical Memory Address to read
1019 *              Value               - Where value is placed
1020 *              Width               - Number of bits
1021 *
1022 * RETURN:      Value read from physical memory address
1023 *
1024 * DESCRIPTION: Read data from a physical memory address
1025 *
1026 *****************************************************************************/
1027
1028ACPI_STATUS
1029AcpiOsReadMemory (
1030    ACPI_PHYSICAL_ADDRESS   Address,
1031    UINT32                  *Value,
1032    UINT32                  Width)
1033{
1034
1035    switch (Width)
1036    {
1037    case 8:
1038    case 16:
1039    case 32:
1040        *Value = 0;
1041        break;
1042
1043    default:
1044        return (AE_BAD_PARAMETER);
1045    }
1046    return (AE_OK);
1047}
1048
1049
1050/******************************************************************************
1051 *
1052 * FUNCTION:    AcpiOsWriteMemory
1053 *
1054 * PARAMETERS:  Address             - Physical Memory Address to write
1055 *              Value               - Value to write
1056 *              Width               - Number of bits
1057 *
1058 * RETURN:      None
1059 *
1060 * DESCRIPTION: Write data to a physical memory address
1061 *
1062 *****************************************************************************/
1063
1064ACPI_STATUS
1065AcpiOsWriteMemory (
1066    ACPI_PHYSICAL_ADDRESS   Address,
1067    UINT32                  Value,
1068    UINT32                  Width)
1069{
1070
1071    return (AE_OK);
1072}
1073
1074
1075/******************************************************************************
1076 *
1077 * FUNCTION:    AcpiOsReadable
1078 *
1079 * PARAMETERS:  Pointer             - Area to be verified
1080 *              Length              - Size of area
1081 *
1082 * RETURN:      TRUE if readable for entire length
1083 *
1084 * DESCRIPTION: Verify that a pointer is valid for reading
1085 *
1086 *****************************************************************************/
1087
1088BOOLEAN
1089AcpiOsReadable (
1090    void                    *Pointer,
1091    ACPI_SIZE               Length)
1092{
1093
1094    return (TRUE);
1095}
1096
1097
1098/******************************************************************************
1099 *
1100 * FUNCTION:    AcpiOsWritable
1101 *
1102 * PARAMETERS:  Pointer             - Area to be verified
1103 *              Length              - Size of area
1104 *
1105 * RETURN:      TRUE if writable for entire length
1106 *
1107 * DESCRIPTION: Verify that a pointer is valid for writing
1108 *
1109 *****************************************************************************/
1110
1111BOOLEAN
1112AcpiOsWritable (
1113    void                    *Pointer,
1114    ACPI_SIZE               Length)
1115{
1116
1117    return (TRUE);
1118}
1119
1120
1121/******************************************************************************
1122 *
1123 * FUNCTION:    AcpiOsSignal
1124 *
1125 * PARAMETERS:  Function            - ACPI CA signal function code
1126 *              Info                - Pointer to function-dependent structure
1127 *
1128 * RETURN:      Status
1129 *
1130 * DESCRIPTION: Miscellaneous functions. Example implementation only.
1131 *
1132 *****************************************************************************/
1133
1134ACPI_STATUS
1135AcpiOsSignal (
1136    UINT32                  Function,
1137    void                    *Info)
1138{
1139
1140    switch (Function)
1141    {
1142    case ACPI_SIGNAL_FATAL:
1143        break;
1144
1145    case ACPI_SIGNAL_BREAKPOINT:
1146        break;
1147
1148    default:
1149        break;
1150    }
1151
1152    return (AE_OK);
1153}
1154
1155/* Optional multi-thread support */
1156
1157#ifndef ACPI_SINGLE_THREADED
1158/******************************************************************************
1159 *
1160 * FUNCTION:    AcpiOsGetThreadId
1161 *
1162 * PARAMETERS:  None
1163 *
1164 * RETURN:      Id of the running thread
1165 *
1166 * DESCRIPTION: Get the ID of the current (running) thread
1167 *
1168 *****************************************************************************/
1169
1170ACPI_THREAD_ID
1171AcpiOsGetThreadId (
1172    void)
1173{
1174    pthread_t               thread;
1175
1176
1177    thread = pthread_self();
1178    return (ACPI_CAST_PTHREAD_T (thread));
1179}
1180
1181
1182/******************************************************************************
1183 *
1184 * FUNCTION:    AcpiOsExecute
1185 *
1186 * PARAMETERS:  Type                - Type of execution
1187 *              Function            - Address of the function to execute
1188 *              Context             - Passed as a parameter to the function
1189 *
1190 * RETURN:      Status.
1191 *
1192 * DESCRIPTION: Execute a new thread
1193 *
1194 *****************************************************************************/
1195
1196ACPI_STATUS
1197AcpiOsExecute (
1198    ACPI_EXECUTE_TYPE       Type,
1199    ACPI_OSD_EXEC_CALLBACK  Function,
1200    void                    *Context)
1201{
1202    pthread_t               thread;
1203    int                     ret;
1204
1205
1206    ret = pthread_create (&thread, NULL, (PTHREAD_CALLBACK) Function, Context);
1207    if (ret)
1208    {
1209        AcpiOsPrintf("Create thread failed");
1210    }
1211    return (0);
1212}
1213
1214#endif /* ACPI_SINGLE_THREADED */
1215