]> git.proxmox.com Git - mirror_edk2.git/blob - PcAtChipsetPkg/Library/AcpiTimerLib/AcpiTimerLib.c
d68eaccf1f2c301743efd7b0c0e3dd12d051ce4a
[mirror_edk2.git] / PcAtChipsetPkg / Library / AcpiTimerLib / AcpiTimerLib.c
1 /** @file
2 ACPI Timer implements one instance of Timer Library.
3
4 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Base.h>
10 #include <Library/TimerLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/PcdLib.h>
13 #include <Library/PciLib.h>
14 #include <Library/IoLib.h>
15 #include <Library/DebugLib.h>
16 #include <IndustryStandard/Acpi.h>
17
18 GUID mFrequencyHobGuid = {
19 0x3fca54f6, 0xe1a2, 0x4b20, { 0xbe, 0x76, 0x92, 0x6b, 0x4b, 0x48, 0xbf, 0xaa }
20 };
21
22 /**
23 Internal function to retrieves the 64-bit frequency in Hz.
24
25 Internal function to retrieves the 64-bit frequency in Hz.
26
27 @return The frequency in Hz.
28
29 **/
30 UINT64
31 InternalGetPerformanceCounterFrequency (
32 VOID
33 );
34
35 /**
36 The constructor function enables ACPI IO space.
37
38 If ACPI I/O space not enabled, this function will enable it.
39 It will always return RETURN_SUCCESS.
40
41 @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS.
42
43 **/
44 RETURN_STATUS
45 EFIAPI
46 AcpiTimerLibConstructor (
47 VOID
48 )
49 {
50 UINTN Bus;
51 UINTN Device;
52 UINTN Function;
53 UINTN EnableRegister;
54 UINT8 EnableMask;
55
56 //
57 // ASSERT for the invalid PCD values. They must be configured to the real value.
58 //
59 ASSERT (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) != 0xFFFF);
60 ASSERT (PcdGet16 (PcdAcpiIoPortBaseAddress) != 0xFFFF);
61
62 //
63 // If the register offset to the BAR for the ACPI I/O Port Base Address is 0x0000, then
64 // no PCI register programming is required to enable access to the ACPI registers
65 // specified by PcdAcpiIoPortBaseAddress
66 //
67 if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) == 0x0000) {
68 return RETURN_SUCCESS;
69 }
70
71 //
72 // ASSERT for the invalid PCD values. They must be configured to the real value.
73 //
74 ASSERT (PcdGet8 (PcdAcpiIoPciDeviceNumber) != 0xFF);
75 ASSERT (PcdGet8 (PcdAcpiIoPciFunctionNumber) != 0xFF);
76 ASSERT (PcdGet16 (PcdAcpiIoPciEnableRegisterOffset) != 0xFFFF);
77
78 //
79 // Retrieve the PCD values for the PCI configuration space required to program the ACPI I/O Port Base Address
80 //
81 Bus = PcdGet8 (PcdAcpiIoPciBusNumber);
82 Device = PcdGet8 (PcdAcpiIoPciDeviceNumber);
83 Function = PcdGet8 (PcdAcpiIoPciFunctionNumber);
84 EnableRegister = PcdGet16 (PcdAcpiIoPciEnableRegisterOffset);
85 EnableMask = PcdGet8 (PcdAcpiIoBarEnableMask);
86
87 //
88 // If ACPI I/O space is not enabled yet, program ACPI I/O base address and enable it.
89 //
90 if ((PciRead8 (PCI_LIB_ADDRESS (Bus, Device, Function, EnableRegister)) & EnableMask) != EnableMask) {
91 PciWrite16 (
92 PCI_LIB_ADDRESS (Bus, Device, Function, PcdGet16 (PcdAcpiIoPciBarRegisterOffset)),
93 PcdGet16 (PcdAcpiIoPortBaseAddress)
94 );
95 PciOr8 (
96 PCI_LIB_ADDRESS (Bus, Device, Function, EnableRegister),
97 EnableMask
98 );
99 }
100
101 return RETURN_SUCCESS;
102 }
103
104 /**
105 Internal function to retrieve the ACPI I/O Port Base Address.
106
107 Internal function to retrieve the ACPI I/O Port Base Address.
108
109 @return The 16-bit ACPI I/O Port Base Address.
110
111 **/
112 UINT16
113 InternalAcpiGetAcpiTimerIoPort (
114 VOID
115 )
116 {
117 UINT16 Port;
118
119 Port = PcdGet16 (PcdAcpiIoPortBaseAddress);
120
121 //
122 // If the register offset to the BAR for the ACPI I/O Port Base Address is not 0x0000, then
123 // read the PCI register for the ACPI BAR value in case the BAR has been programmed to a
124 // value other than PcdAcpiIoPortBaseAddress
125 //
126 if (PcdGet16 (PcdAcpiIoPciBarRegisterOffset) != 0x0000) {
127 Port = PciRead16 (
128 PCI_LIB_ADDRESS (
129 PcdGet8 (PcdAcpiIoPciBusNumber),
130 PcdGet8 (PcdAcpiIoPciDeviceNumber),
131 PcdGet8 (PcdAcpiIoPciFunctionNumber),
132 PcdGet16 (PcdAcpiIoPciBarRegisterOffset)
133 )
134 );
135 }
136
137 return (Port & PcdGet16 (PcdAcpiIoPortBaseAddressMask)) + PcdGet16 (PcdAcpiPm1TmrOffset);
138 }
139
140 /**
141 Stalls the CPU for at least the given number of ticks.
142
143 Stalls the CPU for at least the given number of ticks. It's invoked by
144 MicroSecondDelay() and NanoSecondDelay().
145
146 @param Delay A period of time to delay in ticks.
147
148 **/
149 VOID
150 InternalAcpiDelay (
151 IN UINT32 Delay
152 )
153 {
154 UINT16 Port;
155 UINT32 Ticks;
156 UINT32 Times;
157
158 Port = InternalAcpiGetAcpiTimerIoPort ();
159 Times = Delay >> 22;
160 Delay &= BIT22 - 1;
161 do {
162 //
163 // The target timer count is calculated here
164 //
165 Ticks = IoBitFieldRead32 (Port, 0, 23) + Delay;
166 Delay = BIT22;
167 //
168 // Wait until time out
169 // Delay >= 2^23 could not be handled by this function
170 // Timer wrap-arounds are handled correctly by this function
171 //
172 while (((Ticks - IoBitFieldRead32 (Port, 0, 23)) & BIT23) == 0) {
173 CpuPause ();
174 }
175 } while (Times-- > 0);
176 }
177
178 /**
179 Stalls the CPU for at least the given number of microseconds.
180
181 Stalls the CPU for the number of microseconds specified by MicroSeconds.
182
183 @param MicroSeconds The minimum number of microseconds to delay.
184
185 @return MicroSeconds
186
187 **/
188 UINTN
189 EFIAPI
190 MicroSecondDelay (
191 IN UINTN MicroSeconds
192 )
193 {
194 InternalAcpiDelay (
195 (UINT32)DivU64x32 (
196 MultU64x32 (
197 MicroSeconds,
198 ACPI_TIMER_FREQUENCY
199 ),
200 1000000u
201 )
202 );
203 return MicroSeconds;
204 }
205
206 /**
207 Stalls the CPU for at least the given number of nanoseconds.
208
209 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
210
211 @param NanoSeconds The minimum number of nanoseconds to delay.
212
213 @return NanoSeconds
214
215 **/
216 UINTN
217 EFIAPI
218 NanoSecondDelay (
219 IN UINTN NanoSeconds
220 )
221 {
222 InternalAcpiDelay (
223 (UINT32)DivU64x32 (
224 MultU64x32 (
225 NanoSeconds,
226 ACPI_TIMER_FREQUENCY
227 ),
228 1000000000u
229 )
230 );
231 return NanoSeconds;
232 }
233
234 /**
235 Retrieves the current value of a 64-bit free running performance counter.
236
237 Retrieves the current value of a 64-bit free running performance counter. The
238 counter can either count up by 1 or count down by 1. If the physical
239 performance counter counts by a larger increment, then the counter values
240 must be translated. The properties of the counter can be retrieved from
241 GetPerformanceCounterProperties().
242
243 @return The current value of the free running performance counter.
244
245 **/
246 UINT64
247 EFIAPI
248 GetPerformanceCounter (
249 VOID
250 )
251 {
252 return AsmReadTsc ();
253 }
254
255 /**
256 Retrieves the 64-bit frequency in Hz and the range of performance counter
257 values.
258
259 If StartValue is not NULL, then the value that the performance counter starts
260 with immediately after is it rolls over is returned in StartValue. If
261 EndValue is not NULL, then the value that the performance counter end with
262 immediately before it rolls over is returned in EndValue. The 64-bit
263 frequency of the performance counter in Hz is always returned. If StartValue
264 is less than EndValue, then the performance counter counts up. If StartValue
265 is greater than EndValue, then the performance counter counts down. For
266 example, a 64-bit free running counter that counts up would have a StartValue
267 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
268 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
269
270 @param StartValue The value the performance counter starts with when it
271 rolls over.
272 @param EndValue The value that the performance counter ends with before
273 it rolls over.
274
275 @return The frequency in Hz.
276
277 **/
278 UINT64
279 EFIAPI
280 GetPerformanceCounterProperties (
281 OUT UINT64 *StartValue OPTIONAL,
282 OUT UINT64 *EndValue OPTIONAL
283 )
284 {
285 if (StartValue != NULL) {
286 *StartValue = 0;
287 }
288
289 if (EndValue != NULL) {
290 *EndValue = 0xffffffffffffffffULL;
291 }
292
293 return InternalGetPerformanceCounterFrequency ();
294 }
295
296 /**
297 Converts elapsed ticks of performance counter to time in nanoseconds.
298
299 This function converts the elapsed ticks of running performance counter to
300 time value in unit of nanoseconds.
301
302 @param Ticks The number of elapsed ticks of running performance counter.
303
304 @return The elapsed time in nanoseconds.
305
306 **/
307 UINT64
308 EFIAPI
309 GetTimeInNanoSecond (
310 IN UINT64 Ticks
311 )
312 {
313 UINT64 Frequency;
314 UINT64 NanoSeconds;
315 UINT64 Remainder;
316 INTN Shift;
317
318 Frequency = GetPerformanceCounterProperties (NULL, NULL);
319
320 //
321 // Ticks
322 // Time = --------- x 1,000,000,000
323 // Frequency
324 //
325 NanoSeconds = MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remainder), 1000000000u);
326
327 //
328 // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
329 // Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
330 // i.e. highest bit set in Remainder should <= 33.
331 //
332 Shift = MAX (0, HighBitSet64 (Remainder) - 33);
333 Remainder = RShiftU64 (Remainder, (UINTN)Shift);
334 Frequency = RShiftU64 (Frequency, (UINTN)Shift);
335 NanoSeconds += DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u), Frequency, NULL);
336
337 return NanoSeconds;
338 }
339
340 /**
341 Calculate TSC frequency.
342
343 The TSC counting frequency is determined by comparing how far it counts
344 during a 101.4 us period as determined by the ACPI timer.
345 The ACPI timer is used because it counts at a known frequency.
346 The TSC is sampled, followed by waiting 363 counts of the ACPI timer,
347 or 101.4 us. The TSC is then sampled again. The difference multiplied by
348 9861 is the TSC frequency. There will be a small error because of the
349 overhead of reading the ACPI timer. An attempt is made to determine and
350 compensate for this error.
351
352 @return The number of TSC counts per second.
353
354 **/
355 UINT64
356 InternalCalculateTscFrequency (
357 VOID
358 )
359 {
360 UINT64 StartTSC;
361 UINT64 EndTSC;
362 UINT16 TimerAddr;
363 UINT32 Ticks;
364 UINT64 TscFrequency;
365 BOOLEAN InterruptState;
366
367 InterruptState = SaveAndDisableInterrupts ();
368
369 TimerAddr = InternalAcpiGetAcpiTimerIoPort ();
370 //
371 // Compute the number of ticks to wait to measure TSC frequency.
372 // Use 363 * 9861 = 3579543 Hz which is within 2 Hz of ACPI_TIMER_FREQUENCY.
373 // 363 counts is a calibration time of 101.4 uS.
374 //
375 Ticks = IoBitFieldRead32 (TimerAddr, 0, 23) + 363;
376
377 StartTSC = AsmReadTsc (); // Get base value for the TSC
378 //
379 // Wait until the ACPI timer has counted 101.4 us.
380 // Timer wrap-arounds are handled correctly by this function.
381 // When the current ACPI timer value is greater than 'Ticks',
382 // the while loop will exit.
383 //
384 while (((Ticks - IoBitFieldRead32 (TimerAddr, 0, 23)) & BIT23) == 0) {
385 CpuPause ();
386 }
387
388 EndTSC = AsmReadTsc (); // TSC value 101.4 us later
389
390 TscFrequency = MultU64x32 (
391 (EndTSC - StartTSC), // Number of TSC counts in 101.4 us
392 9861 // Number of 101.4 us in a second
393 );
394
395 SetInterruptState (InterruptState);
396
397 return TscFrequency;
398 }