2057d8c8 |
1 | /** @file\r |
2 | Timer Library functions built upon local APIC on IA32/x64.\r |
3 | \r |
4 | This library uses the local APIC library so that it supports x2APIC mode.\r |
5 | \r |
b9610b9c |
6 | Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r |
2057d8c8 |
7 | This program and the accompanying materials\r |
8 | are licensed and made available under the terms and conditions of the BSD License\r |
9 | which accompanies this distribution. The full text of the license may be found at\r |
10 | http://opensource.org/licenses/bsd-license.php.\r |
11 | \r |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
14 | \r |
15 | **/\r |
16 | \r |
17 | #include <Base.h>\r |
18 | #include <Library/TimerLib.h>\r |
19 | #include <Library/BaseLib.h>\r |
20 | #include <Library/PcdLib.h>\r |
21 | #include <Library/DebugLib.h>\r |
22 | #include <Library/LocalApicLib.h>\r |
23 | \r |
24 | /**\r |
25 | Internal function to return the frequency of the local APIC timer.\r |
26 | \r |
27 | @return The frequency of the timer in Hz.\r |
28 | \r |
29 | **/\r |
30 | UINT32\r |
31 | EFIAPI\r |
32 | InternalX86GetTimerFrequency (\r |
33 | VOID\r |
34 | )\r |
35 | {\r |
36 | UINTN Divisor;\r |
37 | \r |
38 | GetApicTimerState (&Divisor, NULL, NULL);\r |
39 | return PcdGet32(PcdFSBClock) / (UINT32)Divisor;\r |
40 | }\r |
41 | \r |
42 | /**\r |
43 | Stalls the CPU for at least the given number of ticks.\r |
44 | \r |
45 | Stalls the CPU for at least the given number of ticks. It's invoked by\r |
46 | MicroSecondDelay() and NanoSecondDelay().\r |
47 | \r |
48 | @param Delay A period of time to delay in ticks.\r |
49 | \r |
50 | **/\r |
51 | VOID\r |
52 | EFIAPI\r |
53 | InternalX86Delay (\r |
54 | IN UINT32 Delay\r |
55 | )\r |
56 | {\r |
57 | INT32 Ticks;\r |
58 | UINT32 PowerOfTwoCounter;\r |
59 | \r |
60 | //\r |
61 | // The target timer count is calculated here\r |
62 | //\r |
63 | Ticks = GetApicTimerCurrentCount () - Delay;\r |
64 | \r |
65 | //\r |
66 | // Wait until time out\r |
67 | // Delay > 2^31 could not be handled by this function\r |
68 | // Timer wrap-arounds are handled correctly by this function\r |
69 | //\r |
70 | PowerOfTwoCounter = GetPowerOfTwo32 (GetApicTimerInitCount ());\r |
71 | while (((UINT32)(GetApicTimerCurrentCount () - Ticks) & PowerOfTwoCounter) == 0) {\r |
72 | CpuPause ();\r |
73 | }\r |
74 | }\r |
75 | \r |
76 | /**\r |
77 | Stalls the CPU for at least the given number of microseconds.\r |
78 | \r |
79 | Stalls the CPU for the number of microseconds specified by MicroSeconds.\r |
80 | \r |
81 | @param MicroSeconds The minimum number of microseconds to delay.\r |
82 | \r |
83 | @return The value of MicroSeconds inputted.\r |
84 | \r |
85 | **/\r |
86 | UINTN\r |
87 | EFIAPI\r |
88 | MicroSecondDelay (\r |
89 | IN UINTN MicroSeconds\r |
90 | )\r |
91 | {\r |
92 | InternalX86Delay (\r |
93 | (UINT32)DivU64x32 (\r |
94 | MultU64x64 (\r |
95 | InternalX86GetTimerFrequency (),\r |
96 | MicroSeconds\r |
97 | ),\r |
98 | 1000000u\r |
99 | )\r |
100 | );\r |
101 | return MicroSeconds;\r |
102 | }\r |
103 | \r |
104 | /**\r |
105 | Stalls the CPU for at least the given number of nanoseconds.\r |
106 | \r |
107 | Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r |
108 | \r |
109 | @param NanoSeconds The minimum number of nanoseconds to delay.\r |
110 | \r |
111 | @return The value of NanoSeconds inputted.\r |
112 | \r |
113 | **/\r |
114 | UINTN\r |
115 | EFIAPI\r |
116 | NanoSecondDelay (\r |
117 | IN UINTN NanoSeconds\r |
118 | )\r |
119 | {\r |
120 | InternalX86Delay (\r |
121 | (UINT32)DivU64x32 (\r |
122 | MultU64x64 (\r |
123 | InternalX86GetTimerFrequency (),\r |
124 | NanoSeconds\r |
125 | ),\r |
126 | 1000000000u\r |
127 | )\r |
128 | );\r |
129 | return NanoSeconds;\r |
130 | }\r |
131 | \r |
132 | /**\r |
133 | Retrieves the current value of a 64-bit free running performance counter.\r |
134 | \r |
135 | The counter can either count up by 1 or count down by 1. If the physical\r |
136 | performance counter counts by a larger increment, then the counter values\r |
137 | must be translated. The properties of the counter can be retrieved from\r |
138 | GetPerformanceCounterProperties().\r |
139 | \r |
140 | @return The current value of the free running performance counter.\r |
141 | \r |
142 | **/\r |
143 | UINT64\r |
144 | EFIAPI\r |
145 | GetPerformanceCounter (\r |
146 | VOID\r |
147 | )\r |
148 | {\r |
149 | return (UINT64)GetApicTimerCurrentCount ();\r |
150 | }\r |
151 | \r |
152 | /**\r |
153 | Retrieves the 64-bit frequency in Hz and the range of performance counter\r |
154 | values.\r |
155 | \r |
156 | If StartValue is not NULL, then the value that the performance counter starts\r |
157 | with immediately after is it rolls over is returned in StartValue. If\r |
158 | EndValue is not NULL, then the value that the performance counter end with\r |
159 | immediately before it rolls over is returned in EndValue. The 64-bit\r |
160 | frequency of the performance counter in Hz is always returned. If StartValue\r |
161 | is less than EndValue, then the performance counter counts up. If StartValue\r |
162 | is greater than EndValue, then the performance counter counts down. For\r |
163 | example, a 64-bit free running counter that counts up would have a StartValue\r |
164 | of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r |
165 | that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r |
166 | \r |
167 | @param StartValue The value the performance counter starts with when it\r |
168 | rolls over.\r |
169 | @param EndValue The value that the performance counter ends with before\r |
170 | it rolls over.\r |
171 | \r |
172 | @return The frequency in Hz.\r |
173 | \r |
174 | **/\r |
175 | UINT64\r |
176 | EFIAPI\r |
177 | GetPerformanceCounterProperties (\r |
178 | OUT UINT64 *StartValue, OPTIONAL\r |
179 | OUT UINT64 *EndValue OPTIONAL\r |
180 | )\r |
181 | {\r |
182 | if (StartValue != NULL) {\r |
183 | *StartValue = (UINT64)GetApicTimerInitCount ();\r |
184 | //\r |
185 | // make sure StartValue is all 1s from High Bit\r |
186 | //\r |
187 | ASSERT ((*StartValue & (*StartValue + 1)) == 0);\r |
188 | }\r |
189 | \r |
190 | if (EndValue != NULL) {\r |
191 | *EndValue = 0;\r |
192 | }\r |
193 | \r |
194 | return (UINT64) InternalX86GetTimerFrequency ();\r |
195 | }\r |
b9610b9c |
196 | \r |
197 | /**\r |
198 | Converts elapsed ticks of performance counter to time in nanoseconds.\r |
199 | \r |
200 | This function converts the elapsed ticks of running performance counter to\r |
201 | time value in unit of nanoseconds.\r |
202 | \r |
203 | @param Ticks The number of elapsed ticks of running performance counter.\r |
204 | \r |
205 | @return The elapsed time in nanoseconds.\r |
206 | \r |
207 | **/\r |
208 | UINT64\r |
209 | EFIAPI\r |
210 | GetTimeInNanoSecond (\r |
211 | IN UINT64 Ticks\r |
212 | )\r |
213 | {\r |
214 | UINT64 Frequency;\r |
215 | UINT64 NanoSeconds;\r |
216 | UINT64 Remainder;\r |
217 | INTN Shift;\r |
218 | \r |
219 | Frequency = GetPerformanceCounterProperties (NULL, NULL);\r |
220 | \r |
221 | //\r |
222 | // Ticks\r |
223 | // Time = --------- x 1,000,000,000\r |
224 | // Frequency\r |
225 | //\r |
226 | NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);\r |
227 | \r |
228 | //\r |
229 | // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.\r |
230 | // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,\r |
231 | // i.e. highest bit set in Remainder should <= 33.\r |
232 | //\r |
233 | Shift = MAX (0, HighBitSet64 (Remainder) - 33);\r |
234 | Remainder = RShiftU64 (Remainder, (UINTN) Shift);\r |
235 | Frequency = RShiftU64 (Frequency, (UINTN) Shift);\r |
236 | NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);\r |
237 | \r |
238 | return NanoSeconds;\r |
239 | }\r |