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