Removed duplicate PalCallStatic functions in different libraries. Moved ReadItc and...
[mirror_edk2.git] / MdePkg / Library / BaseTimerLibLocalApic / Ipf / IpfTimerLib.c
CommitLineData
878ddf1f 1/** @file\r
2 Timer Library functions built upon local APIC on IA32/x64.\r
3\r
4 @bug Should use PCD to retrieve all the constants including index of\r
5 the IA32_APIC_BASE MSR, the offsets of InitialCount, CorrentCount\r
6 and DivideConfiguration.\r
7\r
8 Copyright (c) 2006, Intel Corporation<BR>\r
9 All rights reserved. This program and the accompanying materials\r
10 are licensed and made available under the terms and conditions of the BSD License\r
11 which accompanies this distribution. The full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17 Module Name: IpfTimerLib.c\r
18\r
19**/\r
20\r
878ddf1f 21\r
878ddf1f 22\r
ed384ef3 23\r
24/**\r
25 Performs a delay measured as number of ticks.\r
26\r
27 An internal function to perform a delay measured as number of ticks. It's\r
28 invoked by MicroSecondDelay() and NanoSecondDelay().\r
29\r
30 @param Delay Number of ticks to delay.\r
31\r
32**/\r
33STATIC\r
34VOID\r
35InternalIpfDelay (\r
36 IN INT64 Delay\r
37 )\r
38{\r
39 INT64 Ticks;\r
40\r
41 //\r
42 // The target timer count is calculated here\r
43 //\r
cd4903c4 44 Ticks = IpfReadItc () + Delay;\r
ed384ef3 45\r
46 //\r
47 // Wait until time out\r
48 // Delay > 2^63 could not be handled by this function\r
49 // Timer wrap-arounds are handled correctly by this function\r
50 //\r
cd4903c4 51 while (Ticks - IpfReadItc () >= 0);\r
ed384ef3 52}\r
53\r
878ddf1f 54/**\r
55 Stalls the CPU for at least the given number of microseconds.\r
56\r
57 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
58\r
59 @param MicroSeconds The minimum number of microseconds to delay.\r
60\r
ed384ef3 61 @return MicroSeconds\r
878ddf1f 62\r
63**/\r
64UINTN\r
65EFIAPI\r
66MicroSecondDelay (\r
67 IN UINTN MicroSeconds\r
68 )\r
69{\r
ed384ef3 70 InternalIpfDelay (\r
71 GetPerformanceCounterProperties (NULL, NULL) *\r
72 MicroSeconds /\r
73 1000000\r
74 );\r
75 return MicroSeconds;\r
878ddf1f 76}\r
77\r
78/**\r
79 Stalls the CPU for at least the given number of nanoseconds.\r
80\r
81 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
82\r
83 @param NanoSeconds The minimum number of nanoseconds to delay.\r
84\r
ed384ef3 85 @return NanoSeconds\r
878ddf1f 86\r
87**/\r
88UINTN\r
89EFIAPI\r
90NanoSecondDelay (\r
91 IN UINTN NanoSeconds\r
92 )\r
93{\r
ed384ef3 94 InternalIpfDelay (\r
95 GetPerformanceCounterProperties (NULL, NULL) *\r
96 NanoSeconds /\r
97 1000000000\r
98 );\r
99 return NanoSeconds;\r
878ddf1f 100}\r
101\r
102/**\r
103 Retrieves the current value of a 64-bit free running performance counter.\r
104\r
105 Retrieves the current value of a 64-bit free running performance counter. The\r
106 counter can either count up by 1 or count down by 1. If the physical\r
107 performance counter counts by a larger increment, then the counter values\r
108 must be translated. The properties of the counter can be retrieved from\r
109 GetPerformanceCounterProperties().\r
110\r
111 @return The current value of the free running performance counter.\r
112\r
113**/\r
114UINT64\r
115EFIAPI\r
116GetPerformanceCounter (\r
117 VOID\r
118 )\r
119{\r
cd4903c4 120 return IpfReadItc ();\r
878ddf1f 121}\r
122\r
123/**\r
124 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
125 values.\r
126\r
127 If StartValue is not NULL, then the value that the performance counter starts\r
128 with immediately after is it rolls over is returned in StartValue. If\r
129 EndValue is not NULL, then the value that the performance counter end with\r
130 immediately before it rolls over is returned in EndValue. The 64-bit\r
131 frequency of the performance counter in Hz is always returned. If StartValue\r
132 is less than EndValue, then the performance counter counts up. If StartValue\r
133 is greater than EndValue, then the performance counter counts down. For\r
134 example, a 64-bit free running counter that counts up would have a StartValue\r
135 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
136 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
137\r
138 @param StartValue The value the performance counter starts with when it\r
139 rolls over.\r
140 @param EndValue The value that the performance counter ends with before\r
141 it rolls over.\r
142\r
143 @return The frequency in Hz.\r
144\r
145**/\r
146UINT64\r
147EFIAPI\r
148GetPerformanceCounterProperties (\r
5117b83b 149 OUT UINT64 *StartValue, OPTIONAL\r
150 OUT UINT64 *EndValue OPTIONAL\r
878ddf1f 151 )\r
152{\r
153 PAL_PROC_RETURN PalRet;\r
154 UINT64 BaseFrequence;\r
155\r
e65e8d10 156 PalRet = PalCallStatic (NULL, 13, 0, 0, 0);\r
878ddf1f 157 ASSERT (PalRet.Status == 0);\r
158 BaseFrequence = PalRet.r9;\r
159\r
e65e8d10 160 PalRet = PalCallStatic (NULL, 14, 0, 0, 0);\r
878ddf1f 161 ASSERT (PalRet.Status == 0);\r
162\r
ed384ef3 163 if (StartValue != NULL) {\r
164 *StartValue = 0;\r
165 }\r
166\r
167 if (EndValue != NULL) {\r
168 *EndValue = (UINT64)(-1);\r
169 }\r
170\r
878ddf1f 171 return BaseFrequence * (PalRet.r11 >> 32) / (UINT32)PalRet.r11;\r
172}\r