]> git.proxmox.com Git - mirror_edk2.git/blame - DuetPkg/Library/DuetTimerLib/x86TimerLib.c
Add DuetTimerLib and DuetSerialIo library instance.
[mirror_edk2.git] / DuetPkg / Library / DuetTimerLib / x86TimerLib.c
CommitLineData
f5752cb2 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**/\r
14\r
15#include <Base.h>\r
16#include <Library/TimerLib.h>\r
17#include <Library/BaseLib.h>\r
18#include <Library/IoLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/PcdLib.h>\r
21\r
22\r
23//\r
24// The following array is used in calculating the frequency of local APIC\r
25// timer. Refer to IA-32 developers' manual for more details.\r
26//\r
27GLOBAL_REMOVE_IF_UNREFERENCED\r
28CONST UINT8 mTimerLibLocalApicDivisor[] = {\r
29 0x02, 0x04, 0x08, 0x10,\r
30 0x02, 0x04, 0x08, 0x10,\r
31 0x20, 0x40, 0x80, 0x01,\r
32 0x20, 0x40, 0x80, 0x01\r
33};\r
34\r
35/**\r
36 Internal function to retrieve the base address of local APIC.\r
37\r
38 Internal function to retrieve the base address of local APIC.\r
39\r
40 @return The base address of local APIC\r
41\r
42**/\r
43STATIC\r
44UINTN\r
45InternalX86GetApicBase (\r
46 VOID\r
47 )\r
48{\r
49 return (UINTN)AsmMsrBitFieldRead64 (27, 12, 35) << 12;\r
50}\r
51\r
52/**\r
53 Internal function to return the frequency of the local APIC timer.\r
54\r
55 Internal function to return the frequency of the local APIC timer.\r
56\r
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
62STATIC\r
63UINT32\r
64InternalX86GetTimerFrequency (\r
65 IN UINTN ApicBase\r
66 )\r
67{\r
68 return\r
69 PcdGet32(PcdFSBClock) /\r
70 mTimerLibLocalApicDivisor[MmioBitFieldRead32 (ApicBase + 0x3e0, 0, 3)];\r
71}\r
72\r
73/**\r
74 Internal function to read the current tick counter of local APIC.\r
75\r
76 Internal function to read the current tick counter of local APIC.\r
77\r
78 @param ApicBase The base address of memory mapped registers of local APIC.\r
79\r
80 @return The tick counter read.\r
81\r
82**/\r
83STATIC\r
84INT32\r
85InternalX86GetTimerTick (\r
86 IN UINTN ApicBase\r
87 )\r
88{\r
89 return MmioRead32 (ApicBase + 0x390);\r
90}\r
91\r
92/**\r
93 Stalls the CPU for at least the given number of ticks.\r
94\r
95 Stalls the CPU for at least the given number of ticks. It's invoked by\r
96 MicroSecondDelay() and NanoSecondDelay().\r
97\r
98 @param ApicBase The base address of memory mapped registers of local APIC.\r
99 @param Delay A period of time to delay in ticks.\r
100\r
101**/\r
102STATIC\r
103VOID\r
104InternalX86Delay (\r
105 IN UINTN ApicBase,\r
106 IN UINT32 Delay\r
107 )\r
108{\r
109 INT32 Ticks;\r
110\r
111 //\r
112 // The target timer count is calculated here\r
113 //\r
114 Ticks = InternalX86GetTimerTick (ApicBase) - Delay;\r
115\r
116 //\r
117 // Wait until time out\r
118 // Delay > 2^31 could not be handled by this function\r
119 // Timer wrap-arounds are handled correctly by this function\r
120 //\r
121 while (InternalX86GetTimerTick (ApicBase) - Ticks >= 0);\r
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
131 @return MicroSeconds\r
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
163 @return NanoSeconds\r
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
189 Retrieves the current value of a 64-bit free running performance counter.\r
190\r
191 Retrieves the current value of a 64-bit free running performance counter. The\r
192 counter can either count up by 1 or count down by 1. If the physical\r
193 performance counter counts by a larger increment, then the counter values\r
194 must be translated. The properties of the counter can be retrieved from\r
195 GetPerformanceCounterProperties().\r
196\r
197 @return The current value of the free running performance counter.\r
198\r
199**/\r
200UINT64\r
201EFIAPI\r
202GetPerformanceCounter (\r
203 VOID\r
204 )\r
205{\r
206 return (UINT64)(UINT32)InternalX86GetTimerTick (InternalX86GetApicBase ());\r
207}\r
208\r
209/**\r
210 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
211 values.\r
212\r
213 If StartValue is not NULL, then the value that the performance counter starts\r
214 with immediately after is it rolls over is returned in StartValue. If\r
215 EndValue is not NULL, then the value that the performance counter end with\r
216 immediately before it rolls over is returned in EndValue. The 64-bit\r
217 frequency of the performance counter in Hz is always returned. If StartValue\r
218 is less than EndValue, then the performance counter counts up. If StartValue\r
219 is greater than EndValue, then the performance counter counts down. For\r
220 example, a 64-bit free running counter that counts up would have a StartValue\r
221 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
222 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
223\r
224 @param StartValue The value the performance counter starts with when it\r
225 rolls over.\r
226 @param EndValue The value that the performance counter ends with before\r
227 it rolls over.\r
228\r
229 @return The frequency in Hz.\r
230\r
231**/\r
232UINT64\r
233EFIAPI\r
234GetPerformanceCounterProperties (\r
235 OUT UINT64 *StartValue, OPTIONAL\r
236 OUT UINT64 *EndValue OPTIONAL\r
237 )\r
238{\r
239 UINTN ApicBase;\r
240\r
241 ApicBase = InternalX86GetApicBase ();\r
242\r
243 if (StartValue != NULL) {\r
244 *StartValue = MmioRead32 (ApicBase + 0x380);\r
245 }\r
246\r
247 if (EndValue != NULL) {\r
248 *EndValue = 0;\r
249 }\r
250\r
251 return (UINT64) InternalX86GetTimerFrequency (ApicBase);;\r
252}\r