Merge branch 'vendor/GCC44'
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / os_specific / service_layers / oswinxf.c
1 /******************************************************************************
2  *
3  * Module Name: oswinxf - Windows OSL
4  *              $Revision: 1.86 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116
117
118 /*
119  * These interfaces are required in order to link to the ACPI subsystem
120  * parser.  They are called during the execution of the parser, and all
121  * map directly to Clibrary calls.
122  */
123
124 #ifdef WIN32
125 #pragma warning(disable:4115)   /* warning C4115: named type definition in parentheses (caused by rpcasync.h> */
126
127 #include <windows.h>
128 #include <winbase.h>
129 #endif
130
131 #ifdef WIN64
132 #include <windowsx.h>
133 #endif
134
135 #include <stdio.h>
136 #include <stdlib.h>
137 #include <stdarg.h>
138 #include <process.h>
139 #include <time.h>
140
141 #include "acpi.h"
142 #include "acdebug.h"
143
144 #define _COMPONENT          ACPI_OS_SERVICES
145         ACPI_MODULE_NAME    ("oswinxf")
146
147
148 #define NUM_SEMAPHORES      128
149
150 typedef struct semaphore_entry
151 {
152     UINT16                  MaxUnits;
153     UINT16                  CurrentUnits;
154     void                    *OsHandle;
155 } SEMAPHORE_ENTRY;
156
157
158 SEMAPHORE_ENTRY             AcpiGbl_Semaphores[NUM_SEMAPHORES];
159 extern FILE                 *AcpiGbl_DebugFile;
160
161 ACPI_PHYSICAL_ADDRESS
162 AeLocalGetRootPointer (
163     void);
164
165 FILE                        *AcpiGbl_OutputFile;
166 UINT64                      TimerFrequency;
167
168
169 /******************************************************************************
170  *
171  * FUNCTION:    OsTerminate
172  *
173  * PARAMETERS:  None
174  *
175  * RETURN:      None
176  *
177  * DESCRIPTION: Nothing to do for windows
178  *
179  *****************************************************************************/
180
181 ACPI_STATUS
182 AcpiOsTerminate (void)
183 {
184     return AE_OK;
185 }
186
187
188 #ifndef ACPI_EXEC_APP
189 /* Used by both iASL and AcpiDump applications */
190
191 CHAR                s[500];
192
193 /******************************************************************************
194  *
195  * FUNCTION:    OsGetTable
196  *
197  * PARAMETERS:  None
198  *
199  * RETURN:      Pointer to the table.  NULL if failure
200  *
201  * DESCRIPTION: Get an ACPI table from the Windows registry.
202  *
203  *****************************************************************************/
204
205 ACPI_TABLE_HEADER *
206 OsGetTable (
207     char                *TableName)
208 {
209     HKEY                Handle = NULL;
210     ULONG               i;
211     LONG                Status;
212     ULONG               Type;
213     ULONG               NameSize;
214     ULONG               DataSize;
215     HKEY                SubKey;
216     ACPI_TABLE_HEADER   *Buffer;
217     char                *Signature = TableName;
218
219
220     /* Get a handle to the DSDT key */
221
222     while (1)
223     {
224         ACPI_STRCPY (s, "HARDWARE\\ACPI\\");
225         ACPI_STRCAT (s, Signature);
226
227         Status = RegOpenKeyEx (HKEY_LOCAL_MACHINE, s,
228                     0L, KEY_ALL_ACCESS, &Handle);
229
230         if (Status != ERROR_SUCCESS)
231         {
232             /*
233              * Somewhere along the way, MS changed the registry entry for
234              * the FADT from
235              * HARDWARE/ACPI/FACP  to
236              * HARDWARE/ACPI/FADT.
237              *
238              * This code allows for both.
239              */
240             if (ACPI_COMPARE_NAME (Signature, "FACP"))
241             {
242                 Signature = "FADT";
243             }
244             else
245             {
246                 AcpiOsPrintf ("Could not find %s in registry at %s\n", TableName, s);
247                 return (NULL);
248             }
249         }
250         else
251         {
252             break;
253         }
254     }
255
256     /* Actual table is down a couple of levels */
257
258     for (i = 0; ;)
259     {
260         Status = RegEnumKey (Handle, i, s, sizeof(s));
261         i += 1;
262         if (Status == ERROR_NO_MORE_ITEMS)
263         {
264             break;
265         }
266
267         Status = RegOpenKey (Handle, s, &SubKey);
268         if (Status != ERROR_SUCCESS)
269         {
270             AcpiOsPrintf ("Could not open %s entry\n", TableName);
271             return (NULL);
272         }
273
274         RegCloseKey (Handle);
275         Handle = SubKey;
276         i = 0;
277     }
278
279     /* Find the table entry */
280
281     for (i = 0; ;)
282     {
283         NameSize = sizeof (s);
284         Status = RegEnumValue (Handle, i, s, &NameSize,
285                     NULL, &Type, NULL, 0 );
286         if (Status != ERROR_SUCCESS)
287         {
288             AcpiOsPrintf ("Could not get %s registry entry\n", TableName);
289             return (NULL);
290         }
291
292         if (Type == REG_BINARY)
293         {
294             break;
295         }
296         i += 1;
297     }
298
299     /* Get the size of the table */
300
301     Status = RegQueryValueEx (Handle, s, NULL, NULL, NULL, &DataSize);
302     if (Status != ERROR_SUCCESS)
303     {
304         AcpiOsPrintf ("Could not read the %s table size\n", TableName);
305         return (NULL);
306     }
307
308     /* Allocate a new buffer for the table */
309
310     Buffer = AcpiOsAllocate (DataSize);
311     if (!Buffer)
312     {
313         goto Cleanup;
314     }
315
316     /* Get the actual table from the registry */
317
318     Status = RegQueryValueEx (Handle, s, NULL, NULL, (UCHAR *) Buffer, &DataSize);
319     if (Status != ERROR_SUCCESS)
320     {
321         AcpiOsPrintf ("Could not read %s data\n", TableName);
322         return (NULL);
323     }
324
325 Cleanup:
326     RegCloseKey (Handle);
327     return (Buffer);
328 }
329
330 #endif
331
332 /******************************************************************************
333  *
334  * FUNCTION:    AcpiOsInitialize, AcpiOsTerminate
335  *
336  * PARAMETERS:  None
337  *
338  * RETURN:      Status
339  *
340  * DESCRIPTION: Init this OSL
341  *
342  *****************************************************************************/
343
344 ACPI_STATUS
345 AcpiOsInitialize (void)
346 {
347     UINT32                  i;
348     LARGE_INTEGER           LocalTimerFrequency;
349
350
351     AcpiGbl_OutputFile = stdout;
352
353     for (i = 0; i < NUM_SEMAPHORES; i++)
354     {
355         AcpiGbl_Semaphores[i].OsHandle = NULL;
356     }
357
358     TimerFrequency = 0;
359     if (QueryPerformanceFrequency (&LocalTimerFrequency))
360     {
361         /* Frequency is in ticks per second */
362
363         TimerFrequency = LocalTimerFrequency.QuadPart;
364     }
365
366     return AE_OK;
367 }
368
369
370 /******************************************************************************
371  *
372  * FUNCTION:    AcpiOsGetRootPointer
373  *
374  * PARAMETERS:  None
375  *
376  * RETURN:      RSDP physical address
377  *
378  * DESCRIPTION: Gets the root pointer (RSDP)
379  *
380  *****************************************************************************/
381
382 ACPI_PHYSICAL_ADDRESS
383 AcpiOsGetRootPointer (
384     void)
385 {
386
387     return (AeLocalGetRootPointer ());
388 }
389
390
391 /******************************************************************************
392  *
393  * FUNCTION:    AcpiOsPredefinedOverride
394  *
395  * PARAMETERS:  InitVal     - Initial value of the predefined object
396  *              NewVal      - The new value for the object
397  *
398  * RETURN:      Status, pointer to value.  Null pointer returned if not
399  *              overriding.
400  *
401  * DESCRIPTION: Allow the OS to override predefined names
402  *
403  *****************************************************************************/
404
405 ACPI_STATUS
406 AcpiOsPredefinedOverride (
407     const ACPI_PREDEFINED_NAMES *InitVal,
408     ACPI_STRING                 *NewVal)
409 {
410
411     if (!InitVal || !NewVal)
412     {
413         return (AE_BAD_PARAMETER);
414     }
415
416     *NewVal = NULL;
417     return (AE_OK);
418 }
419
420
421 /******************************************************************************
422  *
423  * FUNCTION:    AcpiOsTableOverride
424  *
425  * PARAMETERS:  ExistingTable   - Header of current table (probably firmware)
426  *              NewTable        - Where an entire new table is returned.
427  *
428  * RETURN:      Status, pointer to new table.  Null pointer returned if no
429  *              table is available to override
430  *
431  * DESCRIPTION: Return a different version of a table if one is available
432  *
433  *****************************************************************************/
434
435 ACPI_STATUS
436 AcpiOsTableOverride (
437     ACPI_TABLE_HEADER       *ExistingTable,
438     ACPI_TABLE_HEADER       **NewTable)
439 {
440 #ifndef ACPI_EXEC_APP
441     char                    TableName[ACPI_NAME_SIZE + 1];
442 #endif
443
444
445     if (!ExistingTable || !NewTable)
446     {
447         return (AE_BAD_PARAMETER);
448     }
449
450     *NewTable = NULL;
451
452
453 #ifdef ACPI_EXEC_APP
454
455     /* This code exercises the table override mechanism in the core */
456
457     if (ACPI_COMPARE_NAME (ExistingTable->Signature, ACPI_SIG_DSDT))
458     {
459         /* override DSDT with itself */
460
461         *NewTable = AcpiGbl_DbTablePtr;
462     }
463
464 #else
465
466     /* Construct a null-terminated string from table signature */
467
468     TableName[ACPI_NAME_SIZE] = 0;
469     ACPI_STRNCPY (TableName, ExistingTable->Signature, ACPI_NAME_SIZE);
470
471     *NewTable = OsGetTable (TableName);
472     if (*NewTable)
473     {
474         AcpiOsPrintf ("%s obtained from registry, %d bytes\n",
475             TableName, (*NewTable)->Length);
476     }
477     else
478     {
479         AcpiOsPrintf ("Could not read %s from registry\n", TableName);
480     }
481 #endif
482
483     return (AE_OK);
484 }
485
486
487 /******************************************************************************
488  *
489  * FUNCTION:    AcpiOsGetTimer
490  *
491  * PARAMETERS:  None
492  *
493  * RETURN:      Current ticks in 100-nanosecond units
494  *
495  * DESCRIPTION: Get the value of a system timer
496  *
497  ******************************************************************************/
498
499 UINT64
500 AcpiOsGetTimer (
501     void)
502 {
503     LARGE_INTEGER           Timer;
504
505
506 //        return ((UINT64) GetTickCount() * 10000);
507
508     /* Attempt to use hi-granularity timer first */
509
510     if (TimerFrequency &&
511         QueryPerformanceCounter (&Timer))
512     {
513         /* Convert to 100 nanosecond ticks */
514
515         return ((UINT64) ((Timer.QuadPart * (UINT64) 10000000) / TimerFrequency));
516     }
517
518     /* Fall back to the lo-granularity timer */
519
520     else
521     {
522         /* Convert milliseconds to 100 nanosecond ticks */
523
524         return ((UINT64) GetTickCount() * 10000);
525     }
526 }
527
528
529 /******************************************************************************
530  *
531  * FUNCTION:    AcpiOsReadable
532  *
533  * PARAMETERS:  Pointer             - Area to be verified
534  *              Length              - Size of area
535  *
536  * RETURN:      TRUE if readable for entire length
537  *
538  * DESCRIPTION: Verify that a pointer is valid for reading
539  *
540  *****************************************************************************/
541
542 BOOLEAN
543 AcpiOsReadable (
544     void                    *Pointer,
545     ACPI_SIZE               Length)
546 {
547
548     return ((BOOLEAN) !IsBadReadPtr (Pointer, Length));
549 }
550
551
552 /******************************************************************************
553  *
554  * FUNCTION:    AcpiOsWritable
555  *
556  * PARAMETERS:  Pointer             - Area to be verified
557  *              Length              - Size of area
558  *
559  * RETURN:      TRUE if writable for entire length
560  *
561  * DESCRIPTION: Verify that a pointer is valid for writing
562  *
563  *****************************************************************************/
564
565 BOOLEAN
566 AcpiOsWritable (
567     void                    *Pointer,
568     ACPI_SIZE               Length)
569 {
570
571     return ((BOOLEAN) !IsBadWritePtr (Pointer, Length));
572 }
573
574
575 /******************************************************************************
576  *
577  * FUNCTION:    AcpiOsRedirectOutput
578  *
579  * PARAMETERS:  Destination         - An open file handle/pointer
580  *
581  * RETURN:      None
582  *
583  * DESCRIPTION: Causes redirect of AcpiOsPrintf and AcpiOsVprintf
584  *
585  *****************************************************************************/
586
587 void
588 AcpiOsRedirectOutput (
589     void                    *Destination)
590 {
591
592     AcpiGbl_OutputFile = Destination;
593 }
594
595
596 /******************************************************************************
597  *
598  * FUNCTION:    AcpiOsPrintf
599  *
600  * PARAMETERS:  fmt, ...            Standard printf format
601  *
602  * RETURN:      None
603  *
604  * DESCRIPTION: Formatted output
605  *
606  *****************************************************************************/
607
608 void ACPI_INTERNAL_VAR_XFACE
609 AcpiOsPrintf (
610     const char              *Fmt,
611     ...)
612 {
613     va_list                 Args;
614
615
616     va_start (Args, Fmt);
617
618     AcpiOsVprintf (Fmt, Args);
619
620     va_end (Args);
621     return;
622 }
623
624
625 /******************************************************************************
626  *
627  * FUNCTION:    AcpiOsVprintf
628  *
629  * PARAMETERS:  fmt                 Standard printf format
630  *              args                Argument list
631  *
632  * RETURN:      None
633  *
634  * DESCRIPTION: Formatted output with argument list pointer
635  *
636  *****************************************************************************/
637
638 void
639 AcpiOsVprintf (
640     const char              *Fmt,
641     va_list                 Args)
642 {
643     INT32                   Count = 0;
644     UINT8                   Flags;
645
646
647     Flags = AcpiGbl_DbOutputFlags;
648     if (Flags & ACPI_DB_REDIRECTABLE_OUTPUT)
649     {
650         /* Output is directable to either a file (if open) or the console */
651
652         if (AcpiGbl_DebugFile)
653         {
654             /* Output file is open, send the output there */
655
656             Count = vfprintf (AcpiGbl_DebugFile, Fmt, Args);
657         }
658         else
659         {
660             /* No redirection, send output to console (once only!) */
661
662             Flags |= ACPI_DB_CONSOLE_OUTPUT;
663         }
664     }
665
666     if (Flags & ACPI_DB_CONSOLE_OUTPUT)
667     {
668         Count = vfprintf (AcpiGbl_OutputFile, Fmt, Args);
669     }
670
671     return;
672 }
673
674
675 /******************************************************************************
676  *
677  * FUNCTION:    AcpiOsGetLine
678  *
679  * PARAMETERS:  fmt                 Standard printf format
680  *              args                Argument list
681  *
682  * RETURN:      Actual bytes read
683  *
684  * DESCRIPTION: Formatted input with argument list pointer
685  *
686  *****************************************************************************/
687
688 UINT32
689 AcpiOsGetLine (
690     char                    *Buffer)
691 {
692     char                    Temp;
693     UINT32                  i;
694
695
696     for (i = 0; ; i++)
697     {
698         scanf ("%1c", &Temp);
699         if (!Temp || Temp == '\n')
700         {
701             break;
702         }
703
704         Buffer [i] = Temp;
705     }
706
707     /* Null terminate the buffer */
708
709     Buffer [i] = 0;
710
711     /* Return the number of bytes in the string */
712
713     return (i);
714 }
715
716 /******************************************************************************
717  *
718  * FUNCTION:    AcpiOsMapMemory
719  *
720  * PARAMETERS:  where               Physical address of memory to be mapped
721  *              length              How much memory to map
722  *
723  * RETURN:      Pointer to mapped memory.  Null on error.
724  *
725  * DESCRIPTION: Map physical memory into caller's address space
726  *
727  *****************************************************************************/
728
729 void *
730 AcpiOsMapMemory (
731     ACPI_PHYSICAL_ADDRESS   where,
732     ACPI_SIZE               length)
733 {
734
735     return (ACPI_TO_POINTER ((ACPI_NATIVE_UINT) where));
736 }
737
738
739 /******************************************************************************
740  *
741  * FUNCTION:    AcpiOsUnmapMemory
742  *
743  * PARAMETERS:  where               Logical address of memory to be unmapped
744  *              length              How much memory to unmap
745  *
746  * RETURN:      None.
747  *
748  * DESCRIPTION: Delete a previously created mapping.  Where and Length must
749  *              correspond to a previous mapping exactly.
750  *
751  *****************************************************************************/
752
753 void
754 AcpiOsUnmapMemory (
755     void                    *where,
756     ACPI_SIZE               length)
757 {
758
759     return;
760 }
761
762
763 /******************************************************************************
764  *
765  * FUNCTION:    AcpiOsAllocate
766  *
767  * PARAMETERS:  Size                Amount to allocate, in bytes
768  *
769  * RETURN:      Pointer to the new allocation.  Null on error.
770  *
771  * DESCRIPTION: Allocate memory.  Algorithm is dependent on the OS.
772  *
773  *****************************************************************************/
774
775 void *
776 AcpiOsAllocate (
777     ACPI_SIZE               size)
778 {
779     void                    *Mem;
780
781
782     Mem = (void *) malloc ((size_t) size);
783
784     return Mem;
785 }
786
787
788 /******************************************************************************
789  *
790  * FUNCTION:    AcpiOsFree
791  *
792  * PARAMETERS:  mem                 Pointer to previously allocated memory
793  *
794  * RETURN:      None.
795  *
796  * DESCRIPTION: Free memory allocated via AcpiOsAllocate
797  *
798  *****************************************************************************/
799
800 void
801 AcpiOsFree (
802     void                    *Mem)
803 {
804
805     free (Mem);
806 }
807
808
809 /******************************************************************************
810  *
811  * FUNCTION:    AcpiOsCreateSemaphore
812  *
813  * PARAMETERS:  MaxUnits            - Maximum units that can be sent
814  *              InitialUnits        - Units to be assigned to the new semaphore
815  *              OutHandle           - Where a handle will be returned
816  *
817  * RETURN:      Status
818  *
819  * DESCRIPTION: Create an OS semaphore
820  *
821  *****************************************************************************/
822
823 ACPI_STATUS
824 AcpiOsCreateSemaphore (
825     UINT32              MaxUnits,
826     UINT32              InitialUnits,
827     ACPI_SEMAPHORE      *OutHandle)
828 {
829 #ifdef _MULTI_THREADED
830     void                *Mutex;
831     UINT32              i;
832
833     ACPI_FUNCTION_NAME (OsCreateSemaphore);
834 #endif
835
836
837     if (MaxUnits == ACPI_UINT32_MAX)
838     {
839         MaxUnits = 255;
840     }
841
842     if (InitialUnits == ACPI_UINT32_MAX)
843     {
844         InitialUnits = MaxUnits;
845     }
846
847     if (InitialUnits > MaxUnits)
848     {
849         return AE_BAD_PARAMETER;
850     }
851
852 #ifdef _MULTI_THREADED
853
854     /* Find an empty slot */
855
856     for (i = 0; i < NUM_SEMAPHORES; i++)
857     {
858         if (!AcpiGbl_Semaphores[i].OsHandle)
859         {
860             break;
861         }
862     }
863     if (i >= NUM_SEMAPHORES)
864     {
865         return AE_LIMIT;
866     }
867
868     /* Create an OS semaphore */
869
870     Mutex = CreateSemaphore (NULL, InitialUnits, MaxUnits, NULL);
871     if (!Mutex)
872     {
873         ACPI_ERROR ((AE_INFO, "Could not create semaphore"));
874         return AE_NO_MEMORY;
875     }
876
877     AcpiGbl_Semaphores[i].MaxUnits = (UINT16) MaxUnits;
878     AcpiGbl_Semaphores[i].CurrentUnits = (UINT16) InitialUnits;
879     AcpiGbl_Semaphores[i].OsHandle = Mutex;
880
881     ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Handle=%d, Max=%d, Current=%d, OsHandle=%p\n",
882             i, MaxUnits, InitialUnits, Mutex));
883
884     *OutHandle = (void *) i;
885 #endif
886
887     return AE_OK;
888 }
889
890 /******************************************************************************
891  *
892  * FUNCTION:    AcpiOsDeleteSemaphore
893  *
894  * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
895  *
896  * RETURN:      Status
897  *
898  * DESCRIPTION: Delete an OS semaphore
899  *
900  *****************************************************************************/
901
902 ACPI_STATUS
903 AcpiOsDeleteSemaphore (
904     ACPI_SEMAPHORE      Handle)
905 {
906     UINT32              Index = (UINT32) Handle;
907
908
909     if ((Index >= NUM_SEMAPHORES) ||
910         !AcpiGbl_Semaphores[Index].OsHandle)
911     {
912         return AE_BAD_PARAMETER;
913     }
914
915
916 #ifdef _MULTI_THREADED
917
918     CloseHandle (AcpiGbl_Semaphores[Index].OsHandle);
919     AcpiGbl_Semaphores[Index].OsHandle = NULL;
920 #endif
921
922     return AE_OK;
923 }
924
925
926 /******************************************************************************
927  *
928  * FUNCTION:    AcpiOsWaitSemaphore
929  *
930  * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
931  *              Units               - How many units to wait for
932  *              Timeout             - How long to wait
933  *
934  * RETURN:      Status
935  *
936  * DESCRIPTION: Wait for units
937  *
938  *****************************************************************************/
939
940 ACPI_STATUS
941 AcpiOsWaitSemaphore (
942     ACPI_SEMAPHORE      Handle,
943     UINT32              Units,
944     UINT16              Timeout)
945 {
946 #ifdef _MULTI_THREADED
947     UINT32              Index = (UINT32) Handle;
948     UINT32              WaitStatus;
949     UINT32              OsTimeout = Timeout;
950
951
952     ACPI_FUNCTION_ENTRY ();
953
954
955     if ((Index >= NUM_SEMAPHORES) ||
956         !AcpiGbl_Semaphores[Index].OsHandle)
957     {
958         return AE_BAD_PARAMETER;
959     }
960
961     if (Units > 1)
962     {
963         printf ("WaitSemaphore: Attempt to receive %d units\n", Units);
964         return AE_NOT_IMPLEMENTED;
965     }
966
967
968 /* TBD: Make this a command line option so that we can catch
969  * synchronization deadlocks
970  *
971     if (Timeout == INFINITE)
972         Timeout = 400000;
973 */
974
975     if (Timeout == ACPI_WAIT_FOREVER)
976     {
977         OsTimeout = INFINITE;
978     }
979     else
980     {
981         /* Add 10ms to account for clock tick granularity */
982
983         OsTimeout += 10;
984     }
985
986     WaitStatus = WaitForSingleObject (AcpiGbl_Semaphores[Index].OsHandle, OsTimeout);
987     if (WaitStatus == WAIT_TIMEOUT)
988     {
989 /* Make optional -- wait of 0 is used to detect if unit is available
990         ACPI_ERROR ((AE_INFO, "Timeout on semaphore %d",
991             Handle));
992 */
993         return AE_TIME;
994     }
995
996     if (AcpiGbl_Semaphores[Index].CurrentUnits == 0)
997     {
998         ACPI_ERROR ((AE_INFO, "%s - No unit received. Timeout %X, OSstatus 0x%X",
999             AcpiUtGetMutexName (Index), Timeout, WaitStatus));
1000
1001         return AE_OK;
1002     }
1003
1004     AcpiGbl_Semaphores[Index].CurrentUnits--;
1005 #endif
1006
1007     return AE_OK;
1008 }
1009
1010
1011 /******************************************************************************
1012  *
1013  * FUNCTION:    AcpiOsSignalSemaphore
1014  *
1015  * PARAMETERS:  Handle              - Handle returned by AcpiOsCreateSemaphore
1016  *              Units               - Number of units to send
1017  *
1018  * RETURN:      Status
1019  *
1020  * DESCRIPTION: Send units
1021  *
1022  *****************************************************************************/
1023
1024 ACPI_STATUS
1025 AcpiOsSignalSemaphore (
1026     ACPI_SEMAPHORE      Handle,
1027     UINT32              Units)
1028 {
1029 #ifdef _MULTI_THREADED
1030
1031     UINT32              Index = (UINT32) Handle;
1032
1033
1034     ACPI_FUNCTION_ENTRY ();
1035
1036
1037     if (Index >= NUM_SEMAPHORES)
1038     {
1039         printf ("SignalSemaphore: Index/Handle out of range: %2.2X\n", Index);
1040         return AE_BAD_PARAMETER;
1041     }
1042
1043     if (!AcpiGbl_Semaphores[Index].OsHandle)
1044     {
1045         printf ("SignalSemaphore: Null OS handle, Index %2.2X\n", Index);
1046         return AE_BAD_PARAMETER;
1047     }
1048
1049     if (Units > 1)
1050     {
1051         printf ("SignalSemaphore: Attempt to signal %d units, Index %2.2X\n", Units, Index);
1052         return AE_NOT_IMPLEMENTED;
1053     }
1054
1055     if ((AcpiGbl_Semaphores[Index].CurrentUnits + 1) >
1056         AcpiGbl_Semaphores[Index].MaxUnits)
1057     {
1058         ACPI_ERROR ((AE_INFO,
1059             "Oversignalled semaphore[%d]! Current %d Max %d",
1060             Index, AcpiGbl_Semaphores[Index].CurrentUnits,
1061             AcpiGbl_Semaphores[Index].MaxUnits));
1062
1063         return (AE_LIMIT);
1064     }
1065
1066     AcpiGbl_Semaphores[Index].CurrentUnits++;
1067     ReleaseSemaphore (AcpiGbl_Semaphores[Index].OsHandle, Units, NULL);
1068
1069 #endif
1070
1071     return (AE_OK);
1072 }
1073
1074
1075 /* Spinlock interfaces, just implement with a semaphore */
1076
1077 ACPI_STATUS
1078 AcpiOsCreateLock (
1079     ACPI_SPINLOCK           *OutHandle)
1080 {
1081     return (AcpiOsCreateSemaphore (1, 1, OutHandle));
1082 }
1083
1084 void
1085 AcpiOsDeleteLock (
1086     ACPI_SPINLOCK           Handle)
1087 {
1088     AcpiOsDeleteSemaphore (Handle);
1089 }
1090
1091 ACPI_CPU_FLAGS
1092 AcpiOsAcquireLock (
1093     ACPI_SPINLOCK           Handle)
1094 {
1095     AcpiOsWaitSemaphore (Handle, 1, 0xFFFF);
1096     return (0);
1097 }
1098
1099 void
1100 AcpiOsReleaseLock (
1101     ACPI_SPINLOCK           Handle,
1102     ACPI_CPU_FLAGS          Flags)
1103 {
1104     AcpiOsSignalSemaphore (Handle, 1);
1105 }
1106
1107
1108 #if ACPI_FUTURE_IMPLEMENTATION
1109
1110 /* Mutex interfaces, just implement with a semaphore */
1111
1112 ACPI_STATUS
1113 AcpiOsCreateMutex (
1114     ACPI_MUTEX              *OutHandle)
1115 {
1116     return (AcpiOsCreateSemaphore (1, 1, OutHandle));
1117 }
1118
1119 void
1120 AcpiOsDeleteMutex (
1121     ACPI_MUTEX              Handle)
1122 {
1123     AcpiOsDeleteSemaphore (Handle);
1124 }
1125
1126 ACPI_STATUS
1127 AcpiOsAcquireMutex (
1128     ACPI_MUTEX              Handle,
1129     UINT16                  Timeout)
1130 {
1131     AcpiOsWaitSemaphore (Handle, 1, Timeout);
1132     return (0);
1133 }
1134
1135 void
1136 AcpiOsReleaseMutex (
1137     ACPI_MUTEX              Handle)
1138 {
1139     AcpiOsSignalSemaphore (Handle, 1);
1140 }
1141 #endif
1142
1143
1144 /******************************************************************************
1145  *
1146  * FUNCTION:    AcpiOsInstallInterruptHandler
1147  *
1148  * PARAMETERS:  InterruptNumber     Level handler should respond to.
1149  *              Isr                 Address of the ACPI interrupt handler
1150  *              ExceptPtr           Where status is returned
1151  *
1152  * RETURN:      Handle to the newly installed handler.
1153  *
1154  * DESCRIPTION: Install an interrupt handler.  Used to install the ACPI
1155  *              OS-independent handler.
1156  *
1157  *****************************************************************************/
1158
1159 UINT32
1160 AcpiOsInstallInterruptHandler (
1161     UINT32                  InterruptNumber,
1162     ACPI_OSD_HANDLER        ServiceRoutine,
1163     void                    *Context)
1164 {
1165
1166     return AE_OK;
1167 }
1168
1169
1170 /******************************************************************************
1171  *
1172  * FUNCTION:    AcpiOsRemoveInterruptHandler
1173  *
1174  * PARAMETERS:  Handle              Returned when handler was installed
1175  *
1176  * RETURN:      Status
1177  *
1178  * DESCRIPTION: Uninstalls an interrupt handler.
1179  *
1180  *****************************************************************************/
1181
1182 ACPI_STATUS
1183 AcpiOsRemoveInterruptHandler (
1184     UINT32                  InterruptNumber,
1185     ACPI_OSD_HANDLER        ServiceRoutine)
1186 {
1187
1188     return AE_OK;
1189 }
1190
1191
1192 /******************************************************************************
1193  *
1194  * FUNCTION:    AcpiOsGetThreadId
1195  *
1196  * PARAMETERS:  None
1197  *
1198  * RETURN:      Id of the running thread
1199  *
1200  * DESCRIPTION: Get the Id of the current (running) thread
1201  *
1202  *****************************************************************************/
1203
1204 ACPI_THREAD_ID
1205 AcpiOsGetThreadId (
1206     void)
1207 {
1208     DWORD                   ThreadId;
1209
1210     /* Ensure ID is never 0 */
1211
1212     ThreadId = GetCurrentThreadId ();
1213     return (ThreadId + 1);
1214 }
1215
1216
1217 /******************************************************************************
1218  *
1219  * FUNCTION:    AcpiOsExecute
1220  *
1221  * PARAMETERS:  Type            - Type of execution
1222  *              Function        - Address of the function to execute
1223  *              Context         - Passed as a parameter to the function
1224  *
1225  * RETURN:      Status
1226  *
1227  * DESCRIPTION: Execute a new thread
1228  *
1229  *****************************************************************************/
1230
1231 ACPI_STATUS
1232 AcpiOsExecute (
1233     ACPI_EXECUTE_TYPE       Type,
1234     ACPI_OSD_EXEC_CALLBACK  Function,
1235     void                    *Context)
1236 {
1237
1238 #ifdef _MULTI_THREADED
1239     _beginthread (Function, (unsigned) 0, Context);
1240 #endif
1241
1242     return 0;
1243 }
1244
1245
1246 /******************************************************************************
1247  *
1248  * FUNCTION:    AcpiOsStall
1249  *
1250  * PARAMETERS:  microseconds        To sleep
1251  *
1252  * RETURN:      Blocks until sleep is completed.
1253  *
1254  * DESCRIPTION: Sleep at microsecond granularity
1255  *
1256  *****************************************************************************/
1257
1258 void
1259 AcpiOsStall (
1260     UINT32                  microseconds)
1261 {
1262
1263     Sleep ((microseconds / 1000) + 1);
1264     return;
1265 }
1266
1267
1268 /******************************************************************************
1269  *
1270  * FUNCTION:    AcpiOsSleep
1271  *
1272  * PARAMETERS:  milliseconds        To sleep
1273  *
1274  * RETURN:      Blocks until sleep is completed.
1275  *
1276  * DESCRIPTION: Sleep at millisecond granularity
1277  *
1278  *****************************************************************************/
1279
1280 void
1281 AcpiOsSleep (
1282     ACPI_INTEGER            milliseconds)
1283 {
1284
1285     /* Add 10ms to account for clock tick granularity */
1286
1287     Sleep (((unsigned long) milliseconds) + 10);
1288     return;
1289 }
1290
1291
1292 /******************************************************************************
1293  *
1294  * FUNCTION:    AcpiOsValidateInterface
1295  *
1296  * PARAMETERS:  Interface           - Requested interface to be validated
1297  *
1298  * RETURN:      AE_OK if interface is supported, AE_SUPPORT otherwise
1299  *
1300  * DESCRIPTION: Match an interface string to the interfaces supported by the
1301  *              host. Strings originate from an AML call to the _OSI method.
1302  *
1303  *****************************************************************************/
1304
1305 ACPI_STATUS
1306 AcpiOsValidateInterface (
1307     char                    *Interface)
1308 {
1309
1310     return (AE_SUPPORT);
1311 }
1312
1313
1314 /******************************************************************************
1315  *
1316  * FUNCTION:    AcpiOsValidateAddress
1317  *
1318  * PARAMETERS:  SpaceId             - ACPI space ID
1319  *              Address             - Physical address
1320  *              Length              - Address length
1321  *
1322  * RETURN:      AE_OK if Address/Length is valid for the SpaceId. Otherwise,
1323  *              should return AE_AML_ILLEGAL_ADDRESS.
1324  *
1325  * DESCRIPTION: Validate a system address via the host OS. Used to validate
1326  *              the addresses accessed by AML operation regions.
1327  *
1328  *****************************************************************************/
1329
1330 ACPI_STATUS
1331 AcpiOsValidateAddress (
1332     UINT8                   SpaceId,
1333     ACPI_PHYSICAL_ADDRESS   Address,
1334     ACPI_SIZE               Length)
1335 {
1336
1337     return (AE_OK);
1338 }
1339
1340
1341 /******************************************************************************
1342  *
1343  * FUNCTION:    AcpiOsReadPciConfiguration
1344  *
1345  * PARAMETERS:  PciId               Seg/Bus/Dev
1346  *              Register            Device Register
1347  *              Value               Buffer where value is placed
1348  *              Width               Number of bits
1349  *
1350  * RETURN:      Status
1351  *
1352  * DESCRIPTION: Read data from PCI configuration space
1353  *
1354  *****************************************************************************/
1355
1356 ACPI_STATUS
1357 AcpiOsReadPciConfiguration (
1358     ACPI_PCI_ID             *PciId,
1359     UINT32                  Register,
1360     void                    *Value,
1361     UINT32                  Width)
1362 {
1363
1364     return (AE_OK);
1365 }
1366
1367
1368 /******************************************************************************
1369  *
1370  * FUNCTION:    AcpiOsWritePciConfiguration
1371  *
1372  * PARAMETERS:  PciId               Seg/Bus/Dev
1373  *              Register            Device Register
1374  *              Value               Value to be written
1375  *              Width               Number of bits
1376  *
1377  * RETURN:      Status.
1378  *
1379  * DESCRIPTION: Write data to PCI configuration space
1380  *
1381  *****************************************************************************/
1382
1383 ACPI_STATUS
1384 AcpiOsWritePciConfiguration (
1385     ACPI_PCI_ID             *PciId,
1386     UINT32                  Register,
1387     ACPI_INTEGER            Value,
1388     UINT32                  Width)
1389 {
1390
1391     return (AE_OK);
1392 }
1393
1394 /* TEMPORARY STUB FUNCTION */
1395 void
1396 AcpiOsDerivePciId(
1397     ACPI_HANDLE             rhandle,
1398     ACPI_HANDLE             chandle,
1399     ACPI_PCI_ID             **PciId)
1400 {
1401
1402     return;
1403 }
1404
1405
1406 /******************************************************************************
1407  *
1408  * FUNCTION:    AcpiOsReadPort
1409  *
1410  * PARAMETERS:  Address             Address of I/O port/register to read
1411  *              Value               Where value is placed
1412  *              Width               Number of bits
1413  *
1414  * RETURN:      Value read from port
1415  *
1416  * DESCRIPTION: Read data from an I/O port or register
1417  *
1418  *****************************************************************************/
1419
1420 ACPI_STATUS
1421 AcpiOsReadPort (
1422     ACPI_IO_ADDRESS         Address,
1423     UINT32                  *Value,
1424     UINT32                  Width)
1425 {
1426
1427     switch (Width)
1428     {
1429     case 8:
1430         *Value = 0xFF;
1431         break;
1432
1433     case 16:
1434         *Value = 0xFFFF;
1435         break;
1436
1437     case 32:
1438         *Value = 0xFFFFFFFF;
1439         break;
1440
1441     default:
1442         return (AE_BAD_PARAMETER);
1443     }
1444
1445     return (AE_OK);
1446 }
1447
1448
1449 /******************************************************************************
1450  *
1451  * FUNCTION:    AcpiOsWritePort
1452  *
1453  * PARAMETERS:  Address             Address of I/O port/register to write
1454  *              Value               Value to write
1455  *              Width               Number of bits
1456  *
1457  * RETURN:      None
1458  *
1459  * DESCRIPTION: Write data to an I/O port or register
1460  *
1461  *****************************************************************************/
1462
1463 ACPI_STATUS
1464 AcpiOsWritePort (
1465     ACPI_IO_ADDRESS         Address,
1466     UINT32                  Value,
1467     UINT32                  Width)
1468 {
1469
1470     return (AE_OK);
1471 }
1472
1473
1474 /******************************************************************************
1475  *
1476  * FUNCTION:    AcpiOsReadMemory
1477  *
1478  * PARAMETERS:  Address             Physical Memory Address to read
1479  *              Value               Where value is placed
1480  *              Width               Number of bits
1481  *
1482  * RETURN:      Value read from physical memory address.  Always returned
1483  *              as a 32-bit integer, regardless of the read width.
1484  *
1485  * DESCRIPTION: Read data from a physical memory address
1486  *
1487  *****************************************************************************/
1488
1489 ACPI_STATUS
1490 AcpiOsReadMemory (
1491     ACPI_PHYSICAL_ADDRESS   Address,
1492     UINT32                  *Value,
1493     UINT32                  Width)
1494 {
1495
1496     switch (Width)
1497     {
1498     case 8:
1499     case 16:
1500     case 32:
1501         *Value = 0;
1502         break;
1503
1504     default:
1505         return (AE_BAD_PARAMETER);
1506         break;
1507     }
1508
1509     return (AE_OK);
1510 }
1511
1512
1513 /******************************************************************************
1514  *
1515  * FUNCTION:    AcpiOsWriteMemory
1516  *
1517  * PARAMETERS:  Address             Physical Memory Address to write
1518  *              Value               Value to write
1519  *              Width               Number of bits
1520  *
1521  * RETURN:      None
1522  *
1523  * DESCRIPTION: Write data to a physical memory address
1524  *
1525  *****************************************************************************/
1526
1527 ACPI_STATUS
1528 AcpiOsWriteMemory (
1529     ACPI_PHYSICAL_ADDRESS   Address,
1530     UINT32                  Value,
1531     UINT32                  Width)
1532 {
1533
1534     return (AE_OK);
1535 }
1536
1537
1538 /******************************************************************************
1539  *
1540  * FUNCTION:    AcpiOsSignal
1541  *
1542  * PARAMETERS:  Function            ACPI CA signal function code
1543  *              Info                Pointer to function-dependent structure
1544  *
1545  * RETURN:      Status
1546  *
1547  * DESCRIPTION: Miscellaneous functions
1548  *
1549  *****************************************************************************/
1550
1551 ACPI_STATUS
1552 AcpiOsSignal (
1553     UINT32                  Function,
1554     void                    *Info)
1555 {
1556
1557     switch (Function)
1558     {
1559     case ACPI_SIGNAL_FATAL:
1560         break;
1561
1562     case ACPI_SIGNAL_BREAKPOINT:
1563
1564         if (Info)
1565         {
1566             AcpiOsPrintf ("AcpiOsBreakpoint: %s ****\n", Info);
1567         }
1568         else
1569         {
1570             AcpiOsPrintf ("At AcpiOsBreakpoint ****\n");
1571         }
1572
1573         break;
1574     }
1575
1576     return (AE_OK);
1577 }
1578
1579