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