]> git.proxmox.com Git - mirror_edk2.git/blob - EmulatorPkg/Library/DxeTimerLib/DxeTimerLib.c
EmulatorPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / EmulatorPkg / Library / DxeTimerLib / DxeTimerLib.c
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/BaseLib.h>
11 #include <Library/TimerLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/EmuThunkLib.h>
14 #include <Library/UefiBootServicesTableLib.h>
15 #include <Library/UefiLib.h>
16
17 #include <Protocol/Timer.h>
18
19
20 STATIC UINT64 gTimerPeriod = 0;
21 STATIC EFI_TIMER_ARCH_PROTOCOL *gTimerAp = NULL;
22 STATIC EFI_EVENT gTimerEvent = NULL;
23 STATIC VOID *gRegistration = NULL;
24
25 VOID
26 EFIAPI
27 RegisterTimerArchProtocol (
28 IN EFI_EVENT Event,
29 IN VOID *Context
30 )
31 {
32 EFI_STATUS Status;
33
34 Status = gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&gTimerAp);
35 if (!EFI_ERROR (Status)) {
36 Status = gTimerAp->GetTimerPeriod (gTimerAp, &gTimerPeriod);
37 ASSERT_EFI_ERROR (Status);
38
39 // Convert to Nanoseconds.
40 gTimerPeriod = MultU64x32 (gTimerPeriod, 100);
41
42 if (gTimerEvent == NULL) {
43 Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, (VOID **)&gTimerEvent);
44 ASSERT_EFI_ERROR (Status);
45 }
46 }
47 }
48
49
50
51 /**
52 Stalls the CPU for at least the given number of microseconds.
53
54 Stalls the CPU for the number of microseconds specified by MicroSeconds.
55
56 @param MicroSeconds The minimum number of microseconds to delay.
57
58 @return The value of MicroSeconds inputted.
59
60 **/
61 UINTN
62 EFIAPI
63 MicroSecondDelay (
64 IN UINTN MicroSeconds
65 )
66 {
67 return NanoSecondDelay (MicroSeconds * 1000);
68 }
69
70
71 /**
72 Stalls the CPU for at least the given number of nanoseconds.
73
74 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
75
76 @param NanoSeconds The minimum number of nanoseconds to delay.
77
78 @return The value of NanoSeconds inputted.
79
80 **/
81 UINTN
82 EFIAPI
83 NanoSecondDelay (
84 IN UINTN NanoSeconds
85 )
86 {
87 EFI_STATUS Status;
88 UINT64 HundredNanoseconds;
89 UINTN Index;
90
91 if ((gTimerPeriod != 0) &&
92 ((UINT64)NanoSeconds > gTimerPeriod) &&
93 (EfiGetCurrentTpl () == TPL_APPLICATION)) {
94 //
95 // This stall is long, so use gBS->WaitForEvent () to yield CPU to DXE Core
96 //
97
98 HundredNanoseconds = DivU64x32 (NanoSeconds, 100);
99 Status = gBS->SetTimer (gTimerEvent, TimerRelative, HundredNanoseconds);
100 ASSERT_EFI_ERROR (Status);
101
102 Status = gBS->WaitForEvent (sizeof (gTimerEvent)/sizeof (EFI_EVENT), &gTimerEvent, &Index);
103 ASSERT_EFI_ERROR (Status);
104
105 } else {
106 gEmuThunk->Sleep (NanoSeconds);
107 }
108 return NanoSeconds;
109 }
110
111
112 /**
113 Retrieves the current value of a 64-bit free running performance counter.
114
115 The counter can either count up by 1 or count down by 1. If the physical
116 performance counter counts by a larger increment, then the counter values
117 must be translated. The properties of the counter can be retrieved from
118 GetPerformanceCounterProperties().
119
120 @return The current value of the free running performance counter.
121
122 **/
123 UINT64
124 EFIAPI
125 GetPerformanceCounter (
126 VOID
127 )
128 {
129 return gEmuThunk->QueryPerformanceCounter ();
130 }
131
132 /**
133 Retrieves the 64-bit frequency in Hz and the range of performance counter
134 values.
135
136 If StartValue is not NULL, then the value that the performance counter starts
137 with immediately after is it rolls over is returned in StartValue. If
138 EndValue is not NULL, then the value that the performance counter end with
139 immediately before it rolls over is returned in EndValue. The 64-bit
140 frequency of the performance counter in Hz is always returned. If StartValue
141 is less than EndValue, then the performance counter counts up. If StartValue
142 is greater than EndValue, then the performance counter counts down. For
143 example, a 64-bit free running counter that counts up would have a StartValue
144 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
145 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
146
147 @param StartValue The value the performance counter starts with when it
148 rolls over.
149 @param EndValue The value that the performance counter ends with before
150 it rolls over.
151
152 @return The frequency in Hz.
153
154 **/
155 UINT64
156 EFIAPI
157 GetPerformanceCounterProperties (
158 OUT UINT64 *StartValue, OPTIONAL
159 OUT UINT64 *EndValue OPTIONAL
160 )
161 {
162
163 if (StartValue != NULL) {
164 *StartValue = 0ULL;
165 }
166 if (EndValue != NULL) {
167 *EndValue = (UINT64)-1LL;
168 }
169
170 return gEmuThunk->QueryPerformanceFrequency ();
171 }
172
173
174 /**
175 Register for the Timer AP protocol.
176
177 @param ImageHandle The firmware allocated handle for the EFI image.
178 @param SystemTable A pointer to the EFI System Table.
179
180 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
181
182 **/
183 EFI_STATUS
184 EFIAPI
185 DxeTimerLibConstructor (
186 IN EFI_HANDLE ImageHandle,
187 IN EFI_SYSTEM_TABLE *SystemTable
188 )
189 {
190 EfiCreateProtocolNotifyEvent (
191 &gEfiTimerArchProtocolGuid,
192 TPL_CALLBACK,
193 RegisterTimerArchProtocol,
194 NULL,
195 &gRegistration
196 );
197
198 return EFI_SUCCESS;
199 }
200