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