1 /******************************************************************************
2  *
3  * Module Name: osefixf - EFI OSL interfaces
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2023, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acapps.h"
155 
156 #define _COMPONENT          ACPI_OS_SERVICES
157         ACPI_MODULE_NAME    ("osefixf")
158 
159 
160 /* Local prototypes */
161 
162 static BOOLEAN
163 AcpiEfiCompareGuid (
164     ACPI_EFI_GUID           *Guid1,
165     ACPI_EFI_GUID           *Guid2);
166 
167 static ACPI_PHYSICAL_ADDRESS
168 AcpiEfiGetRsdpViaGuid (
169     ACPI_EFI_GUID           *Guid);
170 
171 static ACPI_EFI_PCI_IO *
172 AcpiEfiGetPciDev (
173     ACPI_PCI_ID             *PciId);
174 
175 
176 /* Global variables */
177 
178 #ifdef _EDK2_EFI
179 struct _ACPI_EFI_SYSTEM_TABLE        *ST;
180 struct _ACPI_EFI_BOOT_SERVICES       *BS;
181 struct _ACPI_EFI_RUNTIME_SERVICES    *RT;
182 #endif
183 
184 
185 /******************************************************************************
186  *
187  * FUNCTION:    AcpiEfiCompareGuid
188  *
189  * PARAMETERS:  Guid1               - GUID to compare
190  *              Guid2               - GUID to compare
191  *
192  * RETURN:      TRUE if Guid1 == Guid2
193  *
194  * DESCRIPTION: Compares two GUIDs
195  *
196  *****************************************************************************/
197 
198 static BOOLEAN
AcpiEfiCompareGuid(ACPI_EFI_GUID * Guid1,ACPI_EFI_GUID * Guid2)199 AcpiEfiCompareGuid (
200     ACPI_EFI_GUID           *Guid1,
201     ACPI_EFI_GUID           *Guid2)
202 {
203     INT32                   *g1;
204     INT32                   *g2;
205     INT32                   r;
206 
207 
208     g1 = (INT32 *) Guid1;
209     g2 = (INT32 *) Guid2;
210 
211     r  = g1[0] - g2[0];
212     r |= g1[1] - g2[1];
213     r |= g1[2] - g2[2];
214     r |= g1[3] - g2[3];
215 
216     return (r ? FALSE : TRUE);
217 }
218 
219 
220 /******************************************************************************
221  *
222  * FUNCTION:    AcpiEfiGetRsdpViaGuid
223  *
224  * PARAMETERS:  None
225  *
226  * RETURN:      RSDP address if found
227  *
228  * DESCRIPTION: Find RSDP address via EFI using specified GUID.
229  *
230  *****************************************************************************/
231 
232 static ACPI_PHYSICAL_ADDRESS
AcpiEfiGetRsdpViaGuid(ACPI_EFI_GUID * Guid)233 AcpiEfiGetRsdpViaGuid (
234     ACPI_EFI_GUID           *Guid)
235 {
236     ACPI_PHYSICAL_ADDRESS   Address = 0;
237     UINTN                   i;
238 
239 
240     for (i = 0; i < ST->NumberOfTableEntries; i++)
241     {
242         if (AcpiEfiCompareGuid (&ST->ConfigurationTable[i].VendorGuid, Guid))
243         {
244             Address = ACPI_PTR_TO_PHYSADDR (
245                 ST->ConfigurationTable[i].VendorTable);
246             break;
247         }
248     }
249 
250     return (Address);
251 }
252 
253 
254 /******************************************************************************
255  *
256  * FUNCTION:    AcpiOsGetRootPointer
257  *
258  * PARAMETERS:  None
259  *
260  * RETURN:      RSDP physical address
261  *
262  * DESCRIPTION: Gets the ACPI root pointer (RSDP)
263  *
264  *****************************************************************************/
265 
266 ACPI_PHYSICAL_ADDRESS
AcpiOsGetRootPointer(void)267 AcpiOsGetRootPointer (
268     void)
269 {
270     ACPI_PHYSICAL_ADDRESS   Address;
271     ACPI_EFI_GUID           Guid10 = ACPI_TABLE_GUID;
272     ACPI_EFI_GUID           Guid20 = ACPI_20_TABLE_GUID;
273 
274 
275     Address = AcpiEfiGetRsdpViaGuid (&Guid20);
276     if (!Address)
277     {
278         Address = AcpiEfiGetRsdpViaGuid (&Guid10);
279     }
280 
281     return (Address);
282 }
283 
284 
285 /******************************************************************************
286  *
287  * FUNCTION:    AcpiOsPredefinedOverride
288  *
289  * PARAMETERS:  InitVal             - Initial value of the predefined object
290  *              NewVal              - The new value for the object
291  *
292  * RETURN:      Status, pointer to value. Null pointer returned if not
293  *              overriding.
294  *
295  * DESCRIPTION: Allow the OS to override predefined names
296  *
297  *****************************************************************************/
298 
299 ACPI_STATUS
AcpiOsPredefinedOverride(const ACPI_PREDEFINED_NAMES * InitVal,ACPI_STRING * NewVal)300 AcpiOsPredefinedOverride (
301     const ACPI_PREDEFINED_NAMES *InitVal,
302     ACPI_STRING                 *NewVal)
303 {
304 
305     if (!InitVal || !NewVal)
306     {
307         return (AE_BAD_PARAMETER);
308     }
309 
310     *NewVal = NULL;
311     return (AE_OK);
312 }
313 
314 
315 /******************************************************************************
316  *
317  * FUNCTION:    AcpiOsMapMemory
318  *
319  * PARAMETERS:  where               - Physical address of memory to be mapped
320  *              length              - How much memory to map
321  *
322  * RETURN:      Pointer to mapped memory. Null on error.
323  *
324  * DESCRIPTION: Map physical memory into caller's address space
325  *
326  *****************************************************************************/
327 
328 void *
AcpiOsMapMemory(ACPI_PHYSICAL_ADDRESS where,ACPI_SIZE length)329 AcpiOsMapMemory (
330     ACPI_PHYSICAL_ADDRESS   where,
331     ACPI_SIZE               length)
332 {
333 
334     return (ACPI_TO_POINTER ((ACPI_SIZE) where));
335 }
336 
337 
338 /******************************************************************************
339  *
340  * FUNCTION:    AcpiOsUnmapMemory
341  *
342  * PARAMETERS:  where               - Logical address of memory to be unmapped
343  *              length              - How much memory to unmap
344  *
345  * RETURN:      None
346  *
347  * DESCRIPTION: Delete a previously created mapping. Where and Length must
348  *              correspond to a previous mapping exactly.
349  *
350  *****************************************************************************/
351 
352 void
AcpiOsUnmapMemory(void * where,ACPI_SIZE length)353 AcpiOsUnmapMemory (
354     void                    *where,
355     ACPI_SIZE               length)
356 {
357 
358     return;
359 }
360 
361 
362 /******************************************************************************
363  *
364  * FUNCTION:    Single threaded stub interfaces
365  *
366  * DESCRIPTION: No-op on single threaded BIOS
367  *
368  *****************************************************************************/
369 
370 ACPI_STATUS
AcpiOsCreateLock(ACPI_SPINLOCK * OutHandle)371 AcpiOsCreateLock (
372     ACPI_SPINLOCK           *OutHandle)
373 {
374     return (AE_OK);
375 }
376 
377 void
AcpiOsDeleteLock(ACPI_SPINLOCK Handle)378 AcpiOsDeleteLock (
379     ACPI_SPINLOCK           Handle)
380 {
381 }
382 
383 ACPI_CPU_FLAGS
AcpiOsAcquireLock(ACPI_SPINLOCK Handle)384 AcpiOsAcquireLock (
385     ACPI_SPINLOCK           Handle)
386 {
387     return (0);
388 }
389 
390 void
AcpiOsReleaseLock(ACPI_SPINLOCK Handle,ACPI_CPU_FLAGS Flags)391 AcpiOsReleaseLock (
392     ACPI_SPINLOCK           Handle,
393     ACPI_CPU_FLAGS          Flags)
394 {
395 }
396 
397 ACPI_STATUS
AcpiOsCreateSemaphore(UINT32 MaxUnits,UINT32 InitialUnits,ACPI_HANDLE * OutHandle)398 AcpiOsCreateSemaphore (
399     UINT32              MaxUnits,
400     UINT32              InitialUnits,
401     ACPI_HANDLE         *OutHandle)
402 {
403     *OutHandle = (ACPI_HANDLE) 1;
404     return (AE_OK);
405 }
406 
407 ACPI_STATUS
AcpiOsDeleteSemaphore(ACPI_HANDLE Handle)408 AcpiOsDeleteSemaphore (
409     ACPI_HANDLE         Handle)
410 {
411     return (AE_OK);
412 }
413 
414 ACPI_STATUS
AcpiOsWaitSemaphore(ACPI_HANDLE Handle,UINT32 Units,UINT16 Timeout)415 AcpiOsWaitSemaphore (
416     ACPI_HANDLE         Handle,
417     UINT32              Units,
418     UINT16              Timeout)
419 {
420     return (AE_OK);
421 }
422 
423 ACPI_STATUS
AcpiOsSignalSemaphore(ACPI_HANDLE Handle,UINT32 Units)424 AcpiOsSignalSemaphore (
425     ACPI_HANDLE         Handle,
426     UINT32              Units)
427 {
428     return (AE_OK);
429 }
430 
431 ACPI_THREAD_ID
AcpiOsGetThreadId(void)432 AcpiOsGetThreadId (
433     void)
434 {
435     return (1);
436 }
437 
438 ACPI_STATUS
AcpiOsExecute(ACPI_EXECUTE_TYPE Type,ACPI_OSD_EXEC_CALLBACK Function,void * Context)439 AcpiOsExecute (
440     ACPI_EXECUTE_TYPE       Type,
441     ACPI_OSD_EXEC_CALLBACK  Function,
442     void                    *Context)
443 {
444     return (AE_OK);
445 }
446 
447 void
AcpiOsWaitEventsComplete(void)448 AcpiOsWaitEventsComplete (
449     void)
450 {
451     return;
452 }
453 
454 
455 /******************************************************************************
456  *
457  * FUNCTION:    AcpiOsInstallInterruptHandler
458  *
459  * PARAMETERS:  InterruptNumber     - Level handler should respond to.
460  *              ServiceRoutine      - Address of the ACPI interrupt handler
461  *              Context             - Where status is returned
462  *
463  * RETURN:      Handle to the newly installed handler.
464  *
465  * DESCRIPTION: Install an interrupt handler. Used to install the ACPI
466  *              OS-independent handler.
467  *
468  *****************************************************************************/
469 
470 UINT32
AcpiOsInstallInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine,void * Context)471 AcpiOsInstallInterruptHandler (
472     UINT32                  InterruptNumber,
473     ACPI_OSD_HANDLER        ServiceRoutine,
474     void                    *Context)
475 {
476 
477     return (AE_OK);
478 }
479 
480 
481 /******************************************************************************
482  *
483  * FUNCTION:    AcpiOsRemoveInterruptHandler
484  *
485  * PARAMETERS:  InterruptNumber     - Level handler should respond to.
486  *              ServiceRoutine      - Address of the ACPI interrupt handler
487  *
488  * RETURN:      Status
489  *
490  * DESCRIPTION: Uninstalls an interrupt handler.
491  *
492  *****************************************************************************/
493 
494 ACPI_STATUS
AcpiOsRemoveInterruptHandler(UINT32 InterruptNumber,ACPI_OSD_HANDLER ServiceRoutine)495 AcpiOsRemoveInterruptHandler (
496     UINT32                  InterruptNumber,
497     ACPI_OSD_HANDLER        ServiceRoutine)
498 {
499 
500     return (AE_OK);
501 }
502 
503 
504 /******************************************************************************
505  *
506  * FUNCTION:    AcpiOsGetTimer
507  *
508  * PARAMETERS:  None
509  *
510  * RETURN:      Current time in 100 nanosecond units
511  *
512  * DESCRIPTION: Get the current system time
513  *
514  *****************************************************************************/
515 
516 UINT64
AcpiOsGetTimer(void)517 AcpiOsGetTimer (
518     void)
519 {
520     ACPI_EFI_STATUS         EfiStatus;
521     ACPI_EFI_TIME           EfiTime;
522     int                     Year, Month, Day;
523     int                     Hour, Minute, Second;
524     UINT64                  Timer;
525 
526 
527     EfiStatus = uefi_call_wrapper (RT->GetTime, 2, &EfiTime, NULL);
528     if (ACPI_EFI_ERROR (EfiStatus))
529     {
530         return ((UINT64) -1);
531     }
532 
533     Year = EfiTime.Year;
534     Month = EfiTime.Month;
535     Day = EfiTime.Day;
536     Hour = EfiTime.Hour;
537     Minute = EfiTime.Minute;
538     Second = EfiTime.Second;
539 
540     /* 1..12 -> 11,12,1..10 */
541 
542     if (0 >= (int) (Month -= 2))
543     {
544         /* Feb has leap days */
545 
546         Month += 12;
547         Year -= 1;
548     }
549 
550     /* Calculate days */
551 
552     Timer = ((UINT64) (Year/4 - Year/100 + Year/400 + 367*Month/12 + Day) +
553                        Year*365 - 719499);
554 
555     /* Calculate seconds */
556 
557     (void) AcpiUtShortMultiply (Timer, 24, &Timer);
558     Timer += Hour;
559     (void) AcpiUtShortMultiply (Timer, 60, &Timer);
560     Timer += Minute;
561     (void) AcpiUtShortMultiply (Timer, 60, &Timer);
562     Timer += Second;
563 
564     /* Calculate 100 nanoseconds */
565 
566     (void) AcpiUtShortMultiply (Timer, ACPI_100NSEC_PER_SEC, &Timer);
567     return (Timer + (EfiTime.Nanosecond / 100));
568 }
569 
570 
571 /******************************************************************************
572  *
573  * FUNCTION:    AcpiOsStall
574  *
575  * PARAMETERS:  microseconds        - Time to sleep
576  *
577  * RETURN:      Blocks until sleep is completed.
578  *
579  * DESCRIPTION: Sleep at microsecond granularity
580  *
581  *****************************************************************************/
582 
583 void
AcpiOsStall(UINT32 microseconds)584 AcpiOsStall (
585     UINT32                  microseconds)
586 {
587 
588     if (microseconds)
589     {
590         uefi_call_wrapper (BS->Stall, 1, microseconds);
591     }
592 }
593 
594 
595 /******************************************************************************
596  *
597  * FUNCTION:    AcpiOsSleep
598  *
599  * PARAMETERS:  milliseconds        - Time to sleep
600  *
601  * RETURN:      Blocks until sleep is completed.
602  *
603  * DESCRIPTION: Sleep at millisecond granularity
604  *
605  *****************************************************************************/
606 
607 void
AcpiOsSleep(UINT64 milliseconds)608 AcpiOsSleep (
609     UINT64                  milliseconds)
610 {
611     UINT64                  microseconds;
612 
613 
614     AcpiUtShortMultiply (milliseconds, ACPI_USEC_PER_MSEC, &microseconds);
615     while (microseconds > ACPI_UINT32_MAX)
616     {
617         AcpiOsStall (ACPI_UINT32_MAX);
618         microseconds -= ACPI_UINT32_MAX;
619     }
620     AcpiOsStall ((UINT32) microseconds);
621 }
622 
623 
624 /******************************************************************************
625  *
626  * FUNCTION:    AcpiOsAllocate
627  *
628  * PARAMETERS:  Size                - Amount to allocate, in bytes
629  *
630  * RETURN:      Pointer to the new allocation. Null on error.
631  *
632  * DESCRIPTION: Allocate memory. Algorithm is dependent on the OS.
633  *
634  *****************************************************************************/
635 
636 void *
AcpiOsAllocate(ACPI_SIZE Size)637 AcpiOsAllocate (
638     ACPI_SIZE               Size)
639 {
640     ACPI_EFI_STATUS         EfiStatus;
641     void                    *Mem;
642 
643 
644     EfiStatus = uefi_call_wrapper (BS->AllocatePool, 3,
645         AcpiEfiLoaderData, Size, &Mem);
646     if (ACPI_EFI_ERROR (EfiStatus))
647     {
648         fprintf (stderr,
649             "EFI_BOOT_SERVICES->AllocatePool(EfiLoaderData) failure.\n");
650         return (NULL);
651     }
652 
653     return (Mem);
654 }
655 
656 
657 #ifdef USE_NATIVE_ALLOCATE_ZEROED
658 /******************************************************************************
659  *
660  * FUNCTION:    AcpiOsAllocateZeroed
661  *
662  * PARAMETERS:  Size                - Amount to allocate, in bytes
663  *
664  * RETURN:      Pointer to the new allocation. Null on error.
665  *
666  * DESCRIPTION: Allocate and zero memory. Algorithm is dependent on the OS.
667  *
668  *****************************************************************************/
669 
670 void *
AcpiOsAllocateZeroed(ACPI_SIZE Size)671 AcpiOsAllocateZeroed (
672     ACPI_SIZE               Size)
673 {
674     void                    *Mem;
675 
676 
677     Mem = AcpiOsAllocate (Size);
678     if (Mem)
679     {
680         memset (Mem, 0, Size);
681     }
682 
683     return (Mem);
684 }
685 #endif
686 
687 
688 /******************************************************************************
689  *
690  * FUNCTION:    AcpiOsFree
691  *
692  * PARAMETERS:  Mem                 - Pointer to previously allocated memory
693  *
694  * RETURN:      None
695  *
696  * DESCRIPTION: Free memory allocated via AcpiOsAllocate
697  *
698  *****************************************************************************/
699 
700 void
AcpiOsFree(void * Mem)701 AcpiOsFree (
702     void                    *Mem)
703 {
704 
705     uefi_call_wrapper (BS->FreePool, 1, Mem);
706 }
707 
708 
709 /******************************************************************************
710  *
711  * FUNCTION:    AcpiOsPrintf
712  *
713  * PARAMETERS:  Format, ...         - Standard printf format
714  *
715  * RETURN:      None
716  *
717  * DESCRIPTION: Formatted output.
718  *
719  *****************************************************************************/
720 
721 void ACPI_INTERNAL_VAR_XFACE
AcpiOsPrintf(const char * Format,...)722 AcpiOsPrintf (
723     const char              *Format,
724     ...)
725 {
726     va_list                 Args;
727 
728 
729     va_start (Args, Format);
730     AcpiOsVprintf (Format, Args);
731     va_end (Args);
732 }
733 
734 
735 /******************************************************************************
736  *
737  * FUNCTION:    AcpiOsVprintf
738  *
739  * PARAMETERS:  Format              - Standard printf format
740  *              Args                - Argument list
741  *
742  * RETURN:      None
743  *
744  * DESCRIPTION: Formatted output with arguments list pointer.
745  *
746  *****************************************************************************/
747 
748 void
AcpiOsVprintf(const char * Format,va_list Args)749 AcpiOsVprintf (
750     const char              *Format,
751     va_list                 Args)
752 {
753 
754     (void) vfprintf (ACPI_FILE_OUT, Format, Args);
755 }
756 
757 
758 /******************************************************************************
759  *
760  * FUNCTION:    AcpiOsInitialize, AcpiOsTerminate
761  *
762  * PARAMETERS:  None
763  *
764  * RETURN:      Status
765  *
766  * DESCRIPTION: Initialize/terminate this module.
767  *
768  *****************************************************************************/
769 
770 ACPI_STATUS
AcpiOsInitialize(void)771 AcpiOsInitialize (
772     void)
773 {
774 
775     return (AE_OK);
776 }
777 
778 
779 ACPI_STATUS
AcpiOsTerminate(void)780 AcpiOsTerminate (
781     void)
782 {
783 
784     return (AE_OK);
785 }
786 
787 
788 /******************************************************************************
789  *
790  * FUNCTION:    AcpiOsSignal
791  *
792  * PARAMETERS:  Function            - ACPI A signal function code
793  *              Info                - Pointer to function-dependent structure
794  *
795  * RETURN:      Status
796  *
797  * DESCRIPTION: Miscellaneous functions. Example implementation only.
798  *
799  *****************************************************************************/
800 
801 ACPI_STATUS
AcpiOsSignal(UINT32 Function,void * Info)802 AcpiOsSignal (
803     UINT32                  Function,
804     void                    *Info)
805 {
806 
807     switch (Function)
808     {
809     case ACPI_SIGNAL_FATAL:
810 
811         break;
812 
813     case ACPI_SIGNAL_BREAKPOINT:
814 
815         break;
816 
817     default:
818 
819         break;
820     }
821 
822     return (AE_OK);
823 }
824 
825 
826 /******************************************************************************
827  *
828  * FUNCTION:    AcpiOsReadable
829  *
830  * PARAMETERS:  Pointer             - Area to be verified
831  *              Length              - Size of area
832  *
833  * RETURN:      TRUE if readable for entire length
834  *
835  * DESCRIPTION: Verify that a pointer is valid for reading
836  *
837  *****************************************************************************/
838 
839 BOOLEAN
AcpiOsReadable(void * Pointer,ACPI_SIZE Length)840 AcpiOsReadable (
841     void                    *Pointer,
842     ACPI_SIZE               Length)
843 {
844 
845     return (TRUE);
846 }
847 
848 
849 /******************************************************************************
850  *
851  * FUNCTION:    AcpiOsWritable
852  *
853  * PARAMETERS:  Pointer             - Area to be verified
854  *              Length              - Size of area
855  *
856  * RETURN:      TRUE if writable for entire length
857  *
858  * DESCRIPTION: Verify that a pointer is valid for writing
859  *
860  *****************************************************************************/
861 
862 BOOLEAN
AcpiOsWritable(void * Pointer,ACPI_SIZE Length)863 AcpiOsWritable (
864     void                    *Pointer,
865     ACPI_SIZE               Length)
866 {
867 
868     return (TRUE);
869 }
870 
871 
872 /******************************************************************************
873  *
874  * FUNCTION:    AcpiOsReadPciConfiguration
875  *
876  * PARAMETERS:  PciId               - Seg/Bus/Dev
877  *              PciRegister         - Device Register
878  *              Value               - Buffer where value is placed
879  *              Width               - Number of bits
880  *
881  * RETURN:      Status
882  *
883  * DESCRIPTION: Read data from PCI configuration space
884  *
885  *****************************************************************************/
886 
887 ACPI_STATUS
AcpiOsReadPciConfiguration(ACPI_PCI_ID * PciId,UINT32 PciRegister,UINT64 * Value64,UINT32 Width)888 AcpiOsReadPciConfiguration (
889     ACPI_PCI_ID             *PciId,
890     UINT32                  PciRegister,
891     UINT64                  *Value64,
892     UINT32                  Width)
893 {
894     ACPI_EFI_PCI_IO         *PciIo;
895     ACPI_EFI_STATUS         EfiStatus;
896     UINT8                   Value8;
897     UINT16                  Value16;
898     UINT32                  Value32;
899 
900 
901     *Value64 = 0;
902     PciIo = AcpiEfiGetPciDev (PciId);
903     if (PciIo == NULL)
904     {
905         return (AE_NOT_FOUND);
906     }
907 
908     switch (Width)
909     {
910     case 8:
911         EfiStatus = uefi_call_wrapper (PciIo->Pci.Read, 5, PciIo,
912             AcpiEfiPciIoWidthUint8, PciRegister, 1, (VOID *) &Value8);
913         *Value64 = Value8;
914         break;
915 
916     case 16:
917         EfiStatus = uefi_call_wrapper (PciIo->Pci.Read, 5, PciIo,
918             AcpiEfiPciIoWidthUint16, PciRegister, 1, (VOID *) &Value16);
919         *Value64 = Value16;
920         break;
921 
922     case 32:
923         EfiStatus = uefi_call_wrapper (PciIo->Pci.Read, 5, PciIo,
924             AcpiEfiPciIoWidthUint32, PciRegister, 1, (VOID *) &Value32);
925         *Value64 = Value32;
926         break;
927 
928     case 64:
929         EfiStatus = uefi_call_wrapper (PciIo->Pci.Read, 5, PciIo,
930             AcpiEfiPciIoWidthUint64, PciRegister, 1, (VOID *) Value64);
931         break;
932 
933     default:
934         return (AE_BAD_PARAMETER);
935     }
936 
937     return (ACPI_EFI_ERROR (EfiStatus)) ? (AE_ERROR) : (AE_OK);
938 }
939 
940 
941 /******************************************************************************
942  *
943  * FUNCTION:    AcpiOsWritePciConfiguration
944  *
945  * PARAMETERS:  PciId               - Seg/Bus/Dev
946  *              PciRegister         - Device Register
947  *              Value               - Value to be written
948  *              Width               - Number of bits
949  *
950  * RETURN:      Status.
951  *
952  * DESCRIPTION: Write data to PCI configuration space
953  *
954  *****************************************************************************/
955 
956 ACPI_STATUS
AcpiOsWritePciConfiguration(ACPI_PCI_ID * PciId,UINT32 PciRegister,UINT64 Value64,UINT32 Width)957 AcpiOsWritePciConfiguration (
958     ACPI_PCI_ID             *PciId,
959     UINT32                  PciRegister,
960     UINT64                  Value64,
961     UINT32                  Width)
962 {
963     ACPI_EFI_PCI_IO         *PciIo;
964     ACPI_EFI_STATUS         EfiStatus;
965     UINT8                   Value8;
966     UINT16                  Value16;
967     UINT32                  Value32;
968 
969 
970     PciIo = AcpiEfiGetPciDev (PciId);
971     if (PciIo == NULL)
972     {
973         return (AE_NOT_FOUND);
974     }
975 
976     switch (Width)
977     {
978     case 8:
979         Value8 = (UINT8) Value64;
980         EfiStatus = uefi_call_wrapper (PciIo->Pci.Write, 5, PciIo,
981             AcpiEfiPciIoWidthUint8, PciRegister, 1, (VOID *) &Value8);
982         break;
983 
984     case 16:
985         Value16 = (UINT16) Value64;
986         EfiStatus = uefi_call_wrapper (PciIo->Pci.Write, 5, PciIo,
987             AcpiEfiPciIoWidthUint16, PciRegister, 1, (VOID *) &Value16);
988         break;
989 
990     case 32:
991         Value32 = (UINT32) Value64;
992         EfiStatus = uefi_call_wrapper (PciIo->Pci.Write, 5, PciIo,
993             AcpiEfiPciIoWidthUint32, PciRegister, 1, (VOID *) &Value32);
994         break;
995 
996     case 64:
997         EfiStatus = uefi_call_wrapper (PciIo->Pci.Write, 5, PciIo,
998             AcpiEfiPciIoWidthUint64, PciRegister, 1, (VOID *) &Value64);
999         break;
1000 
1001     default:
1002         return (AE_BAD_PARAMETER);
1003     }
1004 
1005     return (ACPI_EFI_ERROR (EfiStatus)) ? (AE_ERROR) : (AE_OK);
1006 }
1007 
1008 /******************************************************************************
1009  *
1010  * FUNCTION:    AcpiEfiGetPciDev
1011  *
1012  * PARAMETERS:  PciId               - Seg/Bus/Dev
1013  *
1014  * RETURN:      ACPI_EFI_PCI_IO that matches PciId. Null on error.
1015  *
1016  * DESCRIPTION: Find ACPI_EFI_PCI_IO for the given PciId.
1017  *
1018  *****************************************************************************/
1019 
1020 static ACPI_EFI_PCI_IO *
AcpiEfiGetPciDev(ACPI_PCI_ID * PciId)1021 AcpiEfiGetPciDev (
1022     ACPI_PCI_ID             *PciId)
1023 {
1024     ACPI_EFI_STATUS         EfiStatus;
1025     UINTN                   i;
1026     UINTN                   HandlesCount = 0;
1027     ACPI_EFI_HANDLE         *Handles = NULL;
1028     ACPI_EFI_GUID           EfiPciIoProtocol = ACPI_EFI_PCI_IO_PROTOCOL;
1029     ACPI_EFI_PCI_IO         *PciIoProtocol = NULL;
1030     UINTN                   Bus;
1031     UINTN                   Device;
1032     UINTN                   Function;
1033     UINTN                   Segment;
1034 
1035 
1036     EfiStatus = uefi_call_wrapper (BS->LocateHandleBuffer, 5, AcpiEfiByProtocol,
1037         &EfiPciIoProtocol, NULL, &HandlesCount, &Handles);
1038     if (ACPI_EFI_ERROR (EfiStatus))
1039     {
1040         return (NULL);
1041     }
1042 
1043     for (i = 0; i < HandlesCount; i++)
1044     {
1045         EfiStatus = uefi_call_wrapper (BS->HandleProtocol, 3, Handles[i],
1046             &EfiPciIoProtocol, (VOID **) &PciIoProtocol);
1047         if (!ACPI_EFI_ERROR (EfiStatus))
1048         {
1049             EfiStatus = uefi_call_wrapper (PciIoProtocol->GetLocation, 5,
1050                 PciIoProtocol, &Segment, &Bus, &Device, &Function);
1051             if (!ACPI_EFI_ERROR (EfiStatus) &&
1052                 Bus == PciId->Bus &&
1053                 Device == PciId->Device &&
1054                 Function == PciId->Function &&
1055                 Segment == PciId->Segment)
1056             {
1057                 uefi_call_wrapper (BS->FreePool, 1, Handles);
1058                 return (PciIoProtocol);
1059             }
1060         }
1061     }
1062 
1063     uefi_call_wrapper (BS->FreePool, 1, Handles);
1064     return (NULL);
1065 }
1066 
1067 
1068 #if defined(_EDK2_EFI) && defined(USE_STDLIB)
1069 extern EFI_RUNTIME_SERVICES                 *gRT;
1070 extern EFI_BOOT_SERVICES                    *gBS;
1071 extern EFI_SYSTEM_TABLE                     *gST;
1072 
1073 int ACPI_SYSTEM_XFACE
main(int argc,char * argv[])1074 main (
1075     int                     argc,
1076     char                    *argv[])
1077 {
1078     int                     Error;
1079 
1080 
1081     ST = ACPI_CAST_PTR (ACPI_EFI_SYSTEM_TABLE, gST);
1082     BS = ACPI_CAST_PTR (ACPI_EFI_BOOT_SERVICES, gBS);
1083     RT = ACPI_CAST_PTR (ACPI_EFI_RUNTIME_SERVICES, gRT);
1084 
1085     Error = acpi_main (argc, argv);
1086     return (Error);
1087 }
1088 #endif
1089