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