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