]> git.proxmox.com Git - mirror_edk2.git/blob - DuetPkg/Library/DuetTimerLib/X86TimerLib.c
Patch to remove STATIC modifier. This is on longer recommended by EFI Framework codin...
[mirror_edk2.git] / DuetPkg / Library / DuetTimerLib / X86TimerLib.c
1 /** @file
2 Timer Library functions built upon local APIC on IA32/x64.
3
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <Base.h>
16 #include <Library/TimerLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/DebugLib.h>
20 #include <Library/PcdLib.h>
21
22
23 //
24 // The following array is used in calculating the frequency of local APIC
25 // timer. Refer to IA-32 developers' manual for more details.
26 //
27 GLOBAL_REMOVE_IF_UNREFERENCED
28 CONST UINT8 mTimerLibLocalApicDivisor[] = {
29 0x02, 0x04, 0x08, 0x10,
30 0x02, 0x04, 0x08, 0x10,
31 0x20, 0x40, 0x80, 0x01,
32 0x20, 0x40, 0x80, 0x01
33 };
34
35 /**
36 Internal function to retrieve the base address of local APIC.
37
38 Internal function to retrieve the base address of local APIC.
39
40 @return The base address of local APIC
41
42 **/
43 UINTN
44 InternalX86GetApicBase (
45 VOID
46 )
47 {
48 return (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;
49 }
50
51 /**
52 Internal function to return the frequency of the local APIC timer.
53
54 Internal function to return the frequency of the local APIC timer.
55
56 @param ApicBase The base address of memory mapped registers of local APIC.
57
58 @return The frequency of the timer in Hz.
59
60 **/
61 UINT32
62 InternalX86GetTimerFrequency (
63 IN UINTN ApicBase
64 )
65 {
66 return
67 PcdGet32(PcdFSBClock) /
68 mTimerLibLocalApicDivisor[MmioBitFieldRead32 (ApicBase + 0x3e0, 0, 3)];
69 }
70
71 /**
72 Internal function to read the current tick counter of local APIC.
73
74 Internal function to read the current tick counter of local APIC.
75
76 @param ApicBase The base address of memory mapped registers of local APIC.
77
78 @return The tick counter read.
79
80 **/
81 INT32
82 InternalX86GetTimerTick (
83 IN UINTN ApicBase
84 )
85 {
86 return MmioRead32 (ApicBase + 0x390);
87 }
88
89 /**
90 Stalls the CPU for at least the given number of ticks.
91
92 Stalls the CPU for at least the given number of ticks. It's invoked by
93 MicroSecondDelay() and NanoSecondDelay().
94
95 @param ApicBase The base address of memory mapped registers of local APIC.
96 @param Delay A period of time to delay in ticks.
97
98 **/
99 VOID
100 InternalX86Delay (
101 IN UINTN ApicBase,
102 IN UINT32 Delay
103 )
104 {
105 INT32 Ticks;
106
107 //
108 // The target timer count is calculated here
109 //
110 Ticks = InternalX86GetTimerTick (ApicBase) - Delay;
111
112 //
113 // Wait until time out
114 // Delay > 2^31 could not be handled by this function
115 // Timer wrap-arounds are handled correctly by this function
116 //
117 while (InternalX86GetTimerTick (ApicBase) - Ticks >= 0);
118 }
119
120 /**
121 Stalls the CPU for at least the given number of microseconds.
122
123 Stalls the CPU for the number of microseconds specified by MicroSeconds.
124
125 @param MicroSeconds The minimum number of microseconds to delay.
126
127 @return MicroSeconds
128
129 **/
130 UINTN
131 EFIAPI
132 MicroSecondDelay (
133 IN UINTN MicroSeconds
134 )
135 {
136 UINTN ApicBase;
137
138 ApicBase = InternalX86GetApicBase ();
139 InternalX86Delay (
140 ApicBase,
141 (UINT32)DivU64x32 (
142 MultU64x64 (
143 InternalX86GetTimerFrequency (ApicBase),
144 MicroSeconds
145 ),
146 1000000u
147 )
148 );
149 return MicroSeconds;
150 }
151
152 /**
153 Stalls the CPU for at least the given number of nanoseconds.
154
155 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.
156
157 @param NanoSeconds The minimum number of nanoseconds to delay.
158
159 @return NanoSeconds
160
161 **/
162 UINTN
163 EFIAPI
164 NanoSecondDelay (
165 IN UINTN NanoSeconds
166 )
167 {
168 UINTN ApicBase;
169
170 ApicBase = InternalX86GetApicBase ();
171 InternalX86Delay (
172 ApicBase,
173 (UINT32)DivU64x32 (
174 MultU64x64 (
175 InternalX86GetTimerFrequency (ApicBase),
176 NanoSeconds
177 ),
178 1000000000u
179 )
180 );
181 return NanoSeconds;
182 }
183
184 /**
185 Retrieves the current value of a 64-bit free running performance counter.
186
187 Retrieves the current value of a 64-bit free running performance counter. The
188 counter can either count up by 1 or count down by 1. If the physical
189 performance counter counts by a larger increment, then the counter values
190 must be translated. The properties of the counter can be retrieved from
191 GetPerformanceCounterProperties().
192
193 @return The current value of the free running performance counter.
194
195 **/
196 UINT64
197 EFIAPI
198 GetPerformanceCounter (
199 VOID
200 )
201 {
202 return (UINT64)(UINT32)InternalX86GetTimerTick (InternalX86GetApicBase ());
203 }
204
205 /**
206 Retrieves the 64-bit frequency in Hz and the range of performance counter
207 values.
208
209 If StartValue is not NULL, then the value that the performance counter starts
210 with immediately after is it rolls over is returned in StartValue. If
211 EndValue is not NULL, then the value that the performance counter end with
212 immediately before it rolls over is returned in EndValue. The 64-bit
213 frequency of the performance counter in Hz is always returned. If StartValue
214 is less than EndValue, then the performance counter counts up. If StartValue
215 is greater than EndValue, then the performance counter counts down. For
216 example, a 64-bit free running counter that counts up would have a StartValue
217 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter
218 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.
219
220 @param StartValue The value the performance counter starts with when it
221 rolls over.
222 @param EndValue The value that the performance counter ends with before
223 it rolls over.
224
225 @return The frequency in Hz.
226
227 **/
228 UINT64
229 EFIAPI
230 GetPerformanceCounterProperties (
231 OUT UINT64 *StartValue, OPTIONAL
232 OUT UINT64 *EndValue OPTIONAL
233 )
234 {
235 UINTN ApicBase;
236
237 ApicBase = InternalX86GetApicBase ();
238
239 if (StartValue != NULL) {
240 *StartValue = MmioRead32 (ApicBase + 0x380);
241 }
242
243 if (EndValue != NULL) {
244 *EndValue = 0;
245 }
246
247 return (UINT64) InternalX86GetTimerFrequency (ApicBase);;
248 }