1 /** @file
2   A non-functional instance of the Timer Library.
3 
4   Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include <PiPei.h>
10 #include <Library/TimerLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/PeiServicesLib.h>
13 
14 #include <Ppi/EmuThunk.h>
15 #include <Protocol/EmuThunk.h>
16 
17 /**
18   Stalls the CPU for at least the given number of microseconds.
19 
20   Stalls the CPU for the number of microseconds specified by MicroSeconds.
21 
22   @param  MicroSeconds  The minimum number of microseconds to delay.
23 
24   @return The value of MicroSeconds inputted.
25 
26 **/
27 UINTN
28 EFIAPI
MicroSecondDelay(IN UINTN MicroSeconds)29 MicroSecondDelay (
30   IN      UINTN                     MicroSeconds
31   )
32 {
33   return NanoSecondDelay (MicroSeconds * 1000);
34 }
35 
36 /**
37   Stalls the CPU for at least the given number of nanoseconds.
38 
39   Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
40 
41   @param  NanoSeconds The minimum number of nanoseconds to delay.
42 
43   @return The value of NanoSeconds inputted.
44 
45 **/
46 UINTN
47 EFIAPI
NanoSecondDelay(IN UINTN NanoSeconds)48 NanoSecondDelay (
49   IN      UINTN                     NanoSeconds
50   )
51 {
52   EMU_THUNK_PPI           *ThunkPpi;
53   EFI_STATUS              Status;
54   EMU_THUNK_PROTOCOL      *Thunk;
55 
56   //
57   // Locate EmuThunkPpi for
58   //
59   Status = PeiServicesLocatePpi (
60               &gEmuThunkPpiGuid,
61               0,
62               NULL,
63               (VOID **) &ThunkPpi
64              );
65   if (!EFI_ERROR (Status)) {
66     Thunk  = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
67     Thunk->Sleep (NanoSeconds * 100);
68     return NanoSeconds;
69   }
70 
71   return 0;
72 }
73 
74 /**
75   Retrieves the current value of a 64-bit free running performance counter.
76 
77   The counter can either count up by 1 or count down by 1. If the physical
78   performance counter counts by a larger increment, then the counter values
79   must be translated. The properties of the counter can be retrieved from
80   GetPerformanceCounterProperties().
81 
82   @return The current value of the free running performance counter.
83 
84 **/
85 UINT64
86 EFIAPI
GetPerformanceCounter(VOID)87 GetPerformanceCounter (
88   VOID
89   )
90 {
91   EMU_THUNK_PPI           *ThunkPpi;
92   EFI_STATUS              Status;
93   EMU_THUNK_PROTOCOL      *Thunk;
94 
95   //
96   // Locate EmuThunkPpi for
97   //
98   Status = PeiServicesLocatePpi (
99               &gEmuThunkPpiGuid,
100               0,
101               NULL,
102               (VOID **) &ThunkPpi
103              );
104   if (!EFI_ERROR (Status)) {
105     Thunk  = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
106     return  Thunk->QueryPerformanceCounter ();
107   }
108 
109   return 0;
110 }
111 
112 /**
113   Retrieves the 64-bit frequency in Hz and the range of performance counter
114   values.
115 
116   If StartValue is not NULL, then the value that the performance counter starts
117   with immediately after is it rolls over is returned in StartValue. If
118   EndValue is not NULL, then the value that the performance counter end with
119   immediately before it rolls over is returned in EndValue. The 64-bit
120   frequency of the performance counter in Hz is always returned. If StartValue
121   is less than EndValue, then the performance counter counts up. If StartValue
122   is greater than EndValue, then the performance counter counts down. For
123   example, a 64-bit free running counter that counts up would have a StartValue
124   of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
125   that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
126 
127   @param  StartValue  The value the performance counter starts with when it
128                       rolls over.
129   @param  EndValue    The value that the performance counter ends with before
130                       it rolls over.
131 
132   @return The frequency in Hz.
133 
134 **/
135 UINT64
136 EFIAPI
GetPerformanceCounterProperties(OUT UINT64 * StartValue,OPTIONAL OUT UINT64 * EndValue OPTIONAL)137 GetPerformanceCounterProperties (
138   OUT      UINT64                    *StartValue,  OPTIONAL
139   OUT      UINT64                    *EndValue     OPTIONAL
140   )
141 {
142   EMU_THUNK_PPI           *ThunkPpi;
143   EFI_STATUS              Status;
144   EMU_THUNK_PROTOCOL      *Thunk;
145 
146   //
147   // Locate EmuThunkPpi for
148   //
149   Status = PeiServicesLocatePpi (
150               &gEmuThunkPpiGuid,
151               0,
152               NULL,
153               (VOID **) &ThunkPpi
154              );
155   if (!EFI_ERROR (Status)) {
156     if (StartValue != NULL) {
157       *StartValue = 0ULL;
158     }
159     if (EndValue != NULL) {
160       *EndValue = (UINT64)-1LL;
161     }
162 
163     Thunk  = (EMU_THUNK_PROTOCOL *)ThunkPpi->Thunk ();
164     return  Thunk->QueryPerformanceFrequency ();
165   }
166 
167   return 0;
168 }
169