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