]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseTimerLibLocalApic/Ipf/IpfTimerLib.c
Fxied tracker #54 & #55
[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
21UINT64\r
22ReadItc (\r
23 VOID\r
24 );\r
25\r
26typedef struct {\r
27 UINT64 Status;\r
28 UINT64 r9;\r
29 UINT64 r10;\r
30 UINT64 r11;\r
31} PAL_PROC_RETURN;\r
32\r
33PAL_PROC_RETURN\r
34CallPalProcStatic (\r
35 IN UINT64 Arg1,\r
36 IN UINT64 Arg2,\r
37 IN UINT64 Arg3,\r
38 IN UINT64 Arg4\r
39 );\r
40\r
41/**\r
42 Stalls the CPU for at least the given number of microseconds.\r
43\r
44 Stalls the CPU for the number of microseconds specified by MicroSeconds.\r
45\r
46 @param MicroSeconds The minimum number of microseconds to delay.\r
47\r
48 @return The ticks delayed actually.\r
49\r
50**/\r
51UINTN\r
52EFIAPI\r
53MicroSecondDelay (\r
54 IN UINTN MicroSeconds\r
55 )\r
56{\r
57 UINT64 Ticks;\r
58 UINT64 Delay;\r
59\r
60 Ticks = GetPerformanceCounter ();\r
61 Delay = GetPerformanceCounterProperties (NULL, NULL) * MicroSeconds / 1000000;\r
b544966b 62 while (Ticks + Delay >= GetPerformanceCounter ());\r
878ddf1f 63 return (UINTN)Delay;\r
64}\r
65\r
66/**\r
67 Stalls the CPU for at least the given number of nanoseconds.\r
68\r
69 Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r
70\r
71 @param NanoSeconds The minimum number of nanoseconds to delay.\r
72\r
73 @return The ticks delayed actually.\r
74\r
75**/\r
76UINTN\r
77EFIAPI\r
78NanoSecondDelay (\r
79 IN UINTN NanoSeconds\r
80 )\r
81{\r
82 UINT64 Ticks;\r
83 UINT64 Delay;\r
84\r
85 Ticks = GetPerformanceCounter ();\r
86 Delay = GetPerformanceCounterProperties (NULL, NULL) * NanoSeconds / 1000000000;\r
b544966b 87 while (Ticks + Delay >= GetPerformanceCounter ());\r
878ddf1f 88 return (UINTN)Delay;\r
89}\r
90\r
91/**\r
92 Retrieves the current value of a 64-bit free running performance counter.\r
93\r
94 Retrieves the current value of a 64-bit free running performance counter. The\r
95 counter can either count up by 1 or count down by 1. If the physical\r
96 performance counter counts by a larger increment, then the counter values\r
97 must be translated. The properties of the counter can be retrieved from\r
98 GetPerformanceCounterProperties().\r
99\r
100 @return The current value of the free running performance counter.\r
101\r
102**/\r
103UINT64\r
104EFIAPI\r
105GetPerformanceCounter (\r
106 VOID\r
107 )\r
108{\r
109 return ReadItc ();\r
110}\r
111\r
112/**\r
113 Retrieves the 64-bit frequency in Hz and the range of performance counter\r
114 values.\r
115\r
116 If StartValue is not NULL, then the value that the performance counter starts\r
117 with immediately after is it rolls over is returned in StartValue. If\r
118 EndValue is not NULL, then the value that the performance counter end with\r
119 immediately before it rolls over is returned in EndValue. The 64-bit\r
120 frequency of the performance counter in Hz is always returned. If StartValue\r
121 is less than EndValue, then the performance counter counts up. If StartValue\r
122 is greater than EndValue, then the performance counter counts down. For\r
123 example, a 64-bit free running counter that counts up would have a StartValue\r
124 of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r
125 that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r
126\r
127 @param StartValue The value the performance counter starts with when it\r
128 rolls over.\r
129 @param EndValue The value that the performance counter ends with before\r
130 it rolls over.\r
131\r
132 @return The frequency in Hz.\r
133\r
134**/\r
135UINT64\r
136EFIAPI\r
137GetPerformanceCounterProperties (\r
138 IN UINT64 *StartValue,\r
139 IN UINT64 *EndValue\r
140 )\r
141{\r
142 PAL_PROC_RETURN PalRet;\r
143 UINT64 BaseFrequence;\r
144\r
145 PalRet = CallPalProcStatic (13, 0, 0, 0);\r
146 ASSERT (PalRet.Status == 0);\r
147 BaseFrequence = PalRet.r9;\r
148\r
149 PalRet = CallPalProcStatic (14, 0, 0, 0);\r
150 ASSERT (PalRet.Status == 0);\r
151\r
152 *StartValue = 0;\r
153 *EndValue = (UINT64)(-1);\r
154 return BaseFrequence * (PalRet.r11 >> 32) / (UINT32)PalRet.r11;\r
155}\r