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