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