]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/SecPeiDxeTimerLibCpu/x86TimerLib.c
Add in BasePeCoff library instance.
[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
17//\r
18// Include common header file for this module.\r
19//\r
20#include "CommonHeader.h"\r
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
37 Internal function to retrieve the base address of local APIC.\r
38\r
39 @return The base address of local APIC\r
40\r
41**/\r
42STATIC\r
43UINTN\r
44InternalX86GetApicBase (\r
45 VOID\r
46 )\r
47{\r
48 return (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;\r
49}\r
50\r
51/**\r
52 Internal function to return the frequency of the local APIC timer.\r
53\r
54 Internal function to return the frequency of the local APIC timer.\r
55\r
56 @param ApicBase The base address of memory mapped registers of local APIC.\r
57\r
58 @return The frequency of the timer in Hz.\r
59\r
60**/\r
61STATIC\r
62UINT32\r
63InternalX86GetTimerFrequency (\r
64 IN UINTN ApicBase\r
65 )\r
66{\r
67 return\r
68 PcdGet32(PcdFSBClock) /\r
69 mTimerLibLocalApicDivisor[MmioBitFieldRead32 (ApicBase + 0x3e0, 0, 3)];\r
70}\r
71\r
72/**\r
73 Internal function to read the current tick counter of local APIC.\r
74\r
75 Internal function to read the current tick counter of local APIC.\r
76\r
77 @param ApicBase The base address of memory mapped registers of local APIC.\r
78\r
79 @return The tick counter read.\r
80\r
81**/\r
82STATIC\r
83INT32\r
84InternalX86GetTimerTick (\r
85 IN UINTN ApicBase\r
86 )\r
87{\r
88 return MmioRead32 (ApicBase + 0x390);\r
89}\r
90\r
91/**\r
92 Stalls the CPU for at least the given number of ticks.\r
93\r
94 Stalls the CPU for at least the given number of ticks. It's invoked by\r
95 MicroSecondDelay() and NanoSecondDelay().\r
96\r
97 @param ApicBase The base address of memory mapped registers of local APIC.\r
98 @param Delay A period of time to delay in ticks.\r
99\r
100**/\r
101STATIC\r
102VOID\r
103InternalX86Delay (\r
104 IN UINTN ApicBase,\r
105 IN UINT32 Delay\r
106 )\r
107{\r
108 INT32 Ticks;\r
109\r
110 //\r
111 // The target timer count is calculated here\r
112 //\r
113 Ticks = InternalX86GetTimerTick (ApicBase) - Delay;\r
114\r
115 //\r
116 // Wait until time out\r
117 // Delay > 2^31 could not be handled by this function\r
118 // Timer wrap-arounds are handled correctly by this function\r
119 //\r
120 while (InternalX86GetTimerTick (ApicBase) - Ticks >= 0);\r
121}\r
122\r
123/**\r
124 Stalls the CPU for at least the given number of microseconds.\r
125\r
126 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
127\r
128 @param MicroSeconds The minimum number of microseconds to delay.\r
129\r
130 @return MicroSeconds\r
131\r
132**/\r
133UINTN\r
134EFIAPI\r
135MicroSecondDelay (\r
136 IN UINTN MicroSeconds\r
137 )\r
138{\r
139 UINTN ApicBase;\r
140\r
141 ApicBase = InternalX86GetApicBase ();\r
142 InternalX86Delay (\r
143 ApicBase,\r
144 (UINT32)DivU64x32 (\r
145 MultU64x64 (\r
146 InternalX86GetTimerFrequency (ApicBase),\r
147 MicroSeconds\r
148 ),\r
149 1000000u\r
150 )\r
151 );\r
152 return MicroSeconds;\r
153}\r
154\r
155/**\r
156 Stalls the CPU for at least the given number of nanoseconds.\r
157\r
158 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
159\r
160 @param NanoSeconds The minimum number of nanoseconds to delay.\r
161\r
162 @return NanoSeconds\r
163\r
164**/\r
165UINTN\r
166EFIAPI\r
167NanoSecondDelay (\r
168 IN UINTN NanoSeconds\r
169 )\r
170{\r
171 UINTN ApicBase;\r
172\r
173 ApicBase = InternalX86GetApicBase ();\r
174 InternalX86Delay (\r
175 ApicBase,\r
176 (UINT32)DivU64x32 (\r
177 MultU64x64 (\r
178 InternalX86GetTimerFrequency (ApicBase),\r
179 NanoSeconds\r
180 ),\r
181 1000000000u\r
182 )\r
183 );\r
184 return NanoSeconds;\r
185}\r
186\r
187/**\r
188 Retrieves the current value of a 64-bit free running performance counter.\r
189\r
190 Retrieves the current value of a 64-bit free running performance counter. The\r
191 counter can either count up by 1 or count down by 1. If the physical\r
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
243 *StartValue = MmioRead32 (ApicBase + 0x380);\r
244 }\r
245\r
246 if (EndValue != NULL) {\r
247 *EndValue = 0;\r
248 }\r
249\r
250 return (UINT64) InternalX86GetTimerFrequency (ApicBase);;\r
251}\r