]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Timer Library functions built upon local APIC on IA32/x64.\r | |
3 | \r | |
4 | This library uses the local APIC library so that it supports x2APIC mode.\r | |
5 | \r | |
6 | Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r | |
7 | This program and the accompanying materials\r | |
8 | are licensed and made available under the terms and conditions of the BSD License\r | |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php.\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include <Base.h>\r | |
18 | #include <Library/TimerLib.h>\r | |
19 | #include <Library/BaseLib.h>\r | |
20 | #include <Library/PcdLib.h>\r | |
21 | #include <Library/DebugLib.h>\r | |
22 | #include <Library/LocalApicLib.h>\r | |
23 | \r | |
24 | /**\r | |
25 | Internal function to return the frequency of the local APIC timer.\r | |
26 | \r | |
27 | @return The frequency of the timer in Hz.\r | |
28 | \r | |
29 | **/\r | |
30 | UINT32\r | |
31 | EFIAPI\r | |
32 | InternalX86GetTimerFrequency (\r | |
33 | VOID\r | |
34 | )\r | |
35 | {\r | |
36 | UINTN Divisor;\r | |
37 | \r | |
38 | GetApicTimerState (&Divisor, NULL, NULL);\r | |
39 | return PcdGet32(PcdFSBClock) / (UINT32)Divisor;\r | |
40 | }\r | |
41 | \r | |
42 | /**\r | |
43 | Stalls the CPU for at least the given number of ticks.\r | |
44 | \r | |
45 | Stalls the CPU for at least the given number of ticks. It's invoked by\r | |
46 | MicroSecondDelay() and NanoSecondDelay().\r | |
47 | \r | |
48 | @param Delay A period of time to delay in ticks.\r | |
49 | \r | |
50 | **/\r | |
51 | VOID\r | |
52 | EFIAPI\r | |
53 | InternalX86Delay (\r | |
54 | IN UINT32 Delay\r | |
55 | )\r | |
56 | {\r | |
57 | INT32 Ticks;\r | |
58 | UINT32 PowerOfTwoCounter;\r | |
59 | \r | |
60 | //\r | |
61 | // The target timer count is calculated here\r | |
62 | //\r | |
63 | Ticks = GetApicTimerCurrentCount () - Delay;\r | |
64 | \r | |
65 | //\r | |
66 | // Wait until time out\r | |
67 | // Delay > 2^31 could not be handled by this function\r | |
68 | // Timer wrap-arounds are handled correctly by this function\r | |
69 | //\r | |
70 | PowerOfTwoCounter = GetPowerOfTwo32 (GetApicTimerInitCount ());\r | |
71 | while (((UINT32)(GetApicTimerCurrentCount () - Ticks) & PowerOfTwoCounter) == 0) {\r | |
72 | CpuPause ();\r | |
73 | }\r | |
74 | }\r | |
75 | \r | |
76 | /**\r | |
77 | Stalls the CPU for at least the given number of microseconds.\r | |
78 | \r | |
79 | Stalls the CPU for the number of microseconds specified by MicroSeconds.\r | |
80 | \r | |
81 | @param MicroSeconds The minimum number of microseconds to delay.\r | |
82 | \r | |
83 | @return The value of MicroSeconds inputted.\r | |
84 | \r | |
85 | **/\r | |
86 | UINTN\r | |
87 | EFIAPI\r | |
88 | MicroSecondDelay (\r | |
89 | IN UINTN MicroSeconds\r | |
90 | )\r | |
91 | {\r | |
92 | InternalX86Delay (\r | |
93 | (UINT32)DivU64x32 (\r | |
94 | MultU64x64 (\r | |
95 | InternalX86GetTimerFrequency (),\r | |
96 | MicroSeconds\r | |
97 | ),\r | |
98 | 1000000u\r | |
99 | )\r | |
100 | );\r | |
101 | return MicroSeconds;\r | |
102 | }\r | |
103 | \r | |
104 | /**\r | |
105 | Stalls the CPU for at least the given number of nanoseconds.\r | |
106 | \r | |
107 | Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r | |
108 | \r | |
109 | @param NanoSeconds The minimum number of nanoseconds to delay.\r | |
110 | \r | |
111 | @return The value of NanoSeconds inputted.\r | |
112 | \r | |
113 | **/\r | |
114 | UINTN\r | |
115 | EFIAPI\r | |
116 | NanoSecondDelay (\r | |
117 | IN UINTN NanoSeconds\r | |
118 | )\r | |
119 | {\r | |
120 | InternalX86Delay (\r | |
121 | (UINT32)DivU64x32 (\r | |
122 | MultU64x64 (\r | |
123 | InternalX86GetTimerFrequency (),\r | |
124 | NanoSeconds\r | |
125 | ),\r | |
126 | 1000000000u\r | |
127 | )\r | |
128 | );\r | |
129 | return NanoSeconds;\r | |
130 | }\r | |
131 | \r | |
132 | /**\r | |
133 | Retrieves the current value of a 64-bit free running performance counter.\r | |
134 | \r | |
135 | The counter can either count up by 1 or count down by 1. If the physical\r | |
136 | performance counter counts by a larger increment, then the counter values\r | |
137 | must be translated. The properties of the counter can be retrieved from\r | |
138 | GetPerformanceCounterProperties().\r | |
139 | \r | |
140 | @return The current value of the free running performance counter.\r | |
141 | \r | |
142 | **/\r | |
143 | UINT64\r | |
144 | EFIAPI\r | |
145 | GetPerformanceCounter (\r | |
146 | VOID\r | |
147 | )\r | |
148 | {\r | |
149 | return (UINT64)GetApicTimerCurrentCount ();\r | |
150 | }\r | |
151 | \r | |
152 | /**\r | |
153 | Retrieves the 64-bit frequency in Hz and the range of performance counter\r | |
154 | values.\r | |
155 | \r | |
156 | If StartValue is not NULL, then the value that the performance counter starts\r | |
157 | with immediately after is it rolls over is returned in StartValue. If\r | |
158 | EndValue is not NULL, then the value that the performance counter end with\r | |
159 | immediately before it rolls over is returned in EndValue. The 64-bit\r | |
160 | frequency of the performance counter in Hz is always returned. If StartValue\r | |
161 | is less than EndValue, then the performance counter counts up. If StartValue\r | |
162 | is greater than EndValue, then the performance counter counts down. For\r | |
163 | example, a 64-bit free running counter that counts up would have a StartValue\r | |
164 | of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r | |
165 | that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r | |
166 | \r | |
167 | @param StartValue The value the performance counter starts with when it\r | |
168 | rolls over.\r | |
169 | @param EndValue The value that the performance counter ends with before\r | |
170 | it rolls over.\r | |
171 | \r | |
172 | @return The frequency in Hz.\r | |
173 | \r | |
174 | **/\r | |
175 | UINT64\r | |
176 | EFIAPI\r | |
177 | GetPerformanceCounterProperties (\r | |
178 | OUT UINT64 *StartValue, OPTIONAL\r | |
179 | OUT UINT64 *EndValue OPTIONAL\r | |
180 | )\r | |
181 | {\r | |
182 | if (StartValue != NULL) {\r | |
183 | *StartValue = (UINT64)GetApicTimerInitCount ();\r | |
184 | //\r | |
185 | // make sure StartValue is all 1s from High Bit\r | |
186 | //\r | |
187 | ASSERT ((*StartValue & (*StartValue + 1)) == 0);\r | |
188 | }\r | |
189 | \r | |
190 | if (EndValue != NULL) {\r | |
191 | *EndValue = 0;\r | |
192 | }\r | |
193 | \r | |
194 | return (UINT64) InternalX86GetTimerFrequency ();\r | |
195 | }\r |