]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/DxeTimerLibEsal/DxeTimerLibEsal.c
MdePkg/ProcessorBind: add defines for page allocation granularity
[mirror_edk2.git] / MdePkg / Library / DxeTimerLibEsal / DxeTimerLibEsal.c
CommitLineData
863be5d0 1/** @file\r
2 This library implements the Timer Library using the Extended SAL Stall Services Class.\r
3\r
4 Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16\r
17#include <Protocol/ExtendedSalServiceClasses.h>\r
18\r
19#include <Library/TimerLib.h>\r
20#include <Library/BaseLib.h>\r
21#include <Library/ExtendedSalLib.h>\r
22#include <Library/DebugLib.h>\r
23#include <Library/PalLib.h>\r
24\r
25/**\r
26 Stalls the CPU for at least the given number of microseconds.\r
27\r
28 This function wraps EsalStall function of Extended SAL Stall Services Class.\r
29 It stalls the CPU for the number of microseconds specified by MicroSeconds.\r
30\r
31 @param MicroSeconds The minimum number of microseconds to delay.\r
32\r
33 @return MicroSeconds\r
34\r
35**/\r
36UINTN\r
37EFIAPI\r
38MicroSecondDelay (\r
39 IN UINTN MicroSeconds\r
40 )\r
41{\r
42 EsalCall (\r
43 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO,\r
44 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI,\r
45 StallFunctionId, \r
46 MicroSeconds, \r
47 0, \r
48 0, \r
49 0, \r
50 0, \r
51 0, \r
52 0\r
53 );\r
54 return MicroSeconds;\r
55}\r
56\r
57/**\r
58 Stalls the CPU for at least the given number of nanoseconds.\r
59\r
60 This function wraps EsalStall function of Extended SAL Stall Services Class.\r
61 It stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
62\r
63 @param NanoSeconds The minimum number of nanoseconds to delay.\r
64\r
65 @return NanoSeconds\r
66\r
67**/\r
68UINTN\r
69EFIAPI\r
70NanoSecondDelay (\r
71 IN UINTN NanoSeconds\r
72 )\r
73{\r
74 UINT64 MicroSeconds;\r
75\r
76 //\r
77 // The unit of ESAL Stall service is microsecond, so we turn the time interval\r
78 // from nanosecond to microsecond, using the ceiling value to ensure stalling\r
79 // at least the given number of nanoseconds.\r
80 //\r
81 MicroSeconds = DivU64x32 (NanoSeconds + 999, 1000);\r
82 EsalCall (\r
83 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_LO,\r
84 EFI_EXTENDED_SAL_STALL_SERVICES_PROTOCOL_GUID_HI,\r
85 StallFunctionId, \r
86 MicroSeconds, \r
87 0, \r
88 0, \r
89 0, \r
90 0, \r
91 0, \r
92 0\r
93 );\r
94 return NanoSeconds;\r
95}\r
96\r
97/**\r
98 Retrieves the current value of a 64-bit free running performance counter.\r
99\r
100 Retrieves the current value of a 64-bit free running performance counter. The\r
101 counter can either count up by 1 or count down by 1. If the physical\r
102 performance counter counts by a larger increment, then the counter values\r
103 must be translated. The properties of the counter can be retrieved from\r
104 GetPerformanceCounterProperties().\r
105\r
106 @return The current value of the free running performance counter.\r
107\r
108**/\r
109UINT64\r
110EFIAPI\r
111GetPerformanceCounter (\r
112 VOID\r
113 )\r
114{\r
115 return AsmReadItc ();\r
116}\r
117\r
118/**\r
119 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
120 values.\r
121\r
122 If StartValue is not NULL, then the value that the performance counter starts\r
123 with immediately after is it rolls over is returned in StartValue. If\r
124 EndValue is not NULL, then the value that the performance counter end with\r
125 immediately before it rolls over is returned in EndValue. The 64-bit\r
126 frequency of the performance counter in Hz is always returned. If StartValue\r
127 is less than EndValue, then the performance counter counts up. If StartValue\r
128 is greater than EndValue, then the performance counter counts down. For\r
129 example, a 64-bit free running counter that counts up would have a StartValue\r
130 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
131 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
132\r
133 @param StartValue The value the performance counter starts with when it\r
134 rolls over.\r
135 @param EndValue The value that the performance counter ends with before\r
136 it rolls over.\r
137\r
138 @return The frequency in Hz.\r
139\r
140**/\r
141UINT64\r
142EFIAPI\r
143GetPerformanceCounterProperties (\r
144 OUT UINT64 *StartValue, OPTIONAL\r
145 OUT UINT64 *EndValue OPTIONAL\r
146 )\r
147{\r
148 PAL_CALL_RETURN PalRet;\r
149 UINT64 BaseFrequence;\r
150\r
151 //\r
152 // Get processor base frequency\r
153 //\r
154 PalRet = PalCall (PAL_FREQ_BASE, 0, 0, 0);\r
155 ASSERT (PalRet.Status == 0);\r
156 BaseFrequence = PalRet.r9;\r
157\r
158 //\r
159 // Get processor frequency ratio\r
160 //\r
161 PalRet = PalCall (PAL_FREQ_RATIOS, 0, 0, 0);\r
162 ASSERT (PalRet.Status == 0);\r
163\r
164 //\r
165 // Start value of counter is 0\r
166 //\r
167 if (StartValue != NULL) {\r
168 *StartValue = 0;\r
169 }\r
170\r
171 //\r
172 // End value of counter is 0xFFFFFFFFFFFFFFFF\r
173 //\r
174 if (EndValue != NULL) {\r
175 *EndValue = (UINT64)(-1);\r
176 }\r
177\r
178 return BaseFrequence * (PalRet.r11 >> 32) / (UINT32)PalRet.r11;\r
179}\r
16a48731 180\r
181/**\r
182 Converts elapsed ticks of performance counter to time in nanoseconds.\r
183\r
184 This function converts the elapsed ticks of running performance counter to\r
185 time value in unit of nanoseconds.\r
186\r
187 @param Ticks The number of elapsed ticks of running performance counter.\r
188\r
189 @return The elapsed time in nanoseconds.\r
190\r
191**/\r
192UINT64\r
193EFIAPI\r
194GetTimeInNanoSecond (\r
195 IN UINT64 Ticks\r
196 )\r
197{\r
198 UINT64 Frequency;\r
199 UINT64 NanoSeconds;\r
200 UINT64 Remainder;\r
201 INTN Shift;\r
202\r
203 Frequency = GetPerformanceCounterProperties (NULL, NULL);\r
204\r
205 //\r
206 // Ticks\r
207 // Time = --------- x 1,000,000,000\r
208 // Frequency\r
209 //\r
210 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r
211\r
212 //\r
213 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r
214 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r
215 // i.e. highest bit set in Remainder should <= 33.\r
216 //\r
217 Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r
218 Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r
219 Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r
220 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r
221\r
222 return NanoSeconds;\r
223}\r