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