]>
Commit | Line | Data |
---|---|---|
49ba9447 | 1 | /** @file\r |
2 | ACPI Timer implements one instance of Timer Library.\r | |
3 | \r | |
d3a24ff5 | 4 | Copyright (c) 2008 - 2012, Intel Corporation. All rights reserved.<BR>\r |
550486ed | 5 | Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>\r |
6 | \r | |
b26f0cf9 | 7 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
49ba9447 | 8 | \r |
67164dcd | 9 | **/\r |
49ba9447 | 10 | \r |
550486ed | 11 | #include <Library/DebugLib.h>\r |
170ef2d9 | 12 | #include <Library/BaseLib.h>\r |
d3a24ff5 | 13 | #include <IndustryStandard/Acpi.h>\r |
550486ed | 14 | \r |
170ef2d9 | 15 | #include "AcpiTimerLib.h"\r |
d3a24ff5 | 16 | \r |
17 | //\r | |
41f80fbd | 18 | // The ACPI Time is a 24-bit counter\r |
d3a24ff5 | 19 | //\r |
20 | #define ACPI_TIMER_COUNT_SIZE BIT24\r | |
21 | \r | |
49ba9447 | 22 | /**\r |
23 | Stalls the CPU for at least the given number of ticks.\r | |
24 | \r | |
25 | Stalls the CPU for at least the given number of ticks. It's invoked by\r | |
26 | MicroSecondDelay() and NanoSecondDelay().\r | |
27 | \r | |
28 | @param Delay A period of time to delay in ticks.\r | |
29 | \r | |
30 | **/\r | |
49ba9447 | 31 | VOID\r |
32 | InternalAcpiDelay (\r | |
ac0a286f | 33 | IN UINT32 Delay\r |
49ba9447 | 34 | )\r |
35 | {\r | |
ac0a286f MK |
36 | UINT32 Ticks;\r |
37 | UINT32 Times;\r | |
49ba9447 | 38 | \r |
ac0a286f MK |
39 | Times = Delay >> 22;\r |
40 | Delay &= BIT22 - 1;\r | |
49ba9447 | 41 | do {\r |
42 | //\r | |
43 | // The target timer count is calculated here\r | |
44 | //\r | |
ac0a286f MK |
45 | Ticks = InternalAcpiGetTimerTick () + Delay;\r |
46 | Delay = BIT22;\r | |
49ba9447 | 47 | //\r |
48 | // Wait until time out\r | |
49 | // Delay >= 2^23 could not be handled by this function\r | |
50 | // Timer wrap-arounds are handled correctly by this function\r | |
51 | //\r | |
52 | while (((Ticks - InternalAcpiGetTimerTick ()) & BIT23) == 0) {\r | |
53 | CpuPause ();\r | |
54 | }\r | |
55 | } while (Times-- > 0);\r | |
56 | }\r | |
57 | \r | |
58 | /**\r | |
59 | Stalls the CPU for at least the given number of microseconds.\r | |
60 | \r | |
61 | Stalls the CPU for the number of microseconds specified by MicroSeconds.\r | |
62 | \r | |
63 | @param MicroSeconds The minimum number of microseconds to delay.\r | |
64 | \r | |
65 | @return MicroSeconds\r | |
66 | \r | |
67 | **/\r | |
68 | UINTN\r | |
69 | EFIAPI\r | |
70 | MicroSecondDelay (\r | |
ac0a286f | 71 | IN UINTN MicroSeconds\r |
49ba9447 | 72 | )\r |
73 | {\r | |
74 | InternalAcpiDelay (\r | |
75 | (UINT32)DivU64x32 (\r | |
76 | MultU64x32 (\r | |
77 | MicroSeconds,\r | |
78 | ACPI_TIMER_FREQUENCY\r | |
79 | ),\r | |
80 | 1000000u\r | |
81 | )\r | |
82 | );\r | |
83 | return MicroSeconds;\r | |
84 | }\r | |
85 | \r | |
86 | /**\r | |
87 | Stalls the CPU for at least the given number of nanoseconds.\r | |
88 | \r | |
89 | Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r | |
90 | \r | |
91 | @param NanoSeconds The minimum number of nanoseconds to delay.\r | |
92 | \r | |
93 | @return NanoSeconds\r | |
94 | \r | |
95 | **/\r | |
96 | UINTN\r | |
97 | EFIAPI\r | |
98 | NanoSecondDelay (\r | |
ac0a286f | 99 | IN UINTN NanoSeconds\r |
49ba9447 | 100 | )\r |
101 | {\r | |
102 | InternalAcpiDelay (\r | |
103 | (UINT32)DivU64x32 (\r | |
104 | MultU64x32 (\r | |
105 | NanoSeconds,\r | |
106 | ACPI_TIMER_FREQUENCY\r | |
107 | ),\r | |
108 | 1000000000u\r | |
109 | )\r | |
110 | );\r | |
111 | return NanoSeconds;\r | |
112 | }\r | |
113 | \r | |
114 | /**\r | |
115 | Retrieves the current value of a 64-bit free running performance counter.\r | |
116 | \r | |
117 | Retrieves the current value of a 64-bit free running performance counter. The\r | |
118 | counter can either count up by 1 or count down by 1. If the physical\r | |
119 | performance counter counts by a larger increment, then the counter values\r | |
120 | must be translated. The properties of the counter can be retrieved from\r | |
121 | GetPerformanceCounterProperties().\r | |
122 | \r | |
123 | @return The current value of the free running performance counter.\r | |
124 | \r | |
125 | **/\r | |
126 | UINT64\r | |
127 | EFIAPI\r | |
128 | GetPerformanceCounter (\r | |
129 | VOID\r | |
130 | )\r | |
131 | {\r | |
132 | return (UINT64)InternalAcpiGetTimerTick ();\r | |
133 | }\r | |
134 | \r | |
135 | /**\r | |
136 | Retrieves the 64-bit frequency in Hz and the range of performance counter\r | |
137 | values.\r | |
138 | \r | |
139 | If StartValue is not NULL, then the value that the performance counter starts\r | |
140 | with immediately after is it rolls over is returned in StartValue. If\r | |
141 | EndValue is not NULL, then the value that the performance counter end with\r | |
142 | immediately before it rolls over is returned in EndValue. The 64-bit\r | |
143 | frequency of the performance counter in Hz is always returned. If StartValue\r | |
144 | is less than EndValue, then the performance counter counts up. If StartValue\r | |
145 | is greater than EndValue, then the performance counter counts down. For\r | |
146 | example, a 64-bit free running counter that counts up would have a StartValue\r | |
147 | of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r | |
148 | that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r | |
149 | \r | |
150 | @param StartValue The value the performance counter starts with when it\r | |
151 | rolls over.\r | |
152 | @param EndValue The value that the performance counter ends with before\r | |
153 | it rolls over.\r | |
154 | \r | |
155 | @return The frequency in Hz.\r | |
156 | \r | |
157 | **/\r | |
158 | UINT64\r | |
159 | EFIAPI\r | |
160 | GetPerformanceCounterProperties (\r | |
ac0a286f MK |
161 | OUT UINT64 *StartValue OPTIONAL,\r |
162 | OUT UINT64 *EndValue OPTIONAL\r | |
49ba9447 | 163 | )\r |
164 | {\r | |
165 | if (StartValue != NULL) {\r | |
166 | *StartValue = 0;\r | |
167 | }\r | |
168 | \r | |
169 | if (EndValue != NULL) {\r | |
170 | *EndValue = ACPI_TIMER_COUNT_SIZE - 1;\r | |
171 | }\r | |
172 | \r | |
173 | return ACPI_TIMER_FREQUENCY;\r | |
174 | }\r | |
b9610b9c | 175 | \r |
176 | /**\r | |
177 | Converts elapsed ticks of performance counter to time in nanoseconds.\r | |
178 | \r | |
179 | This function converts the elapsed ticks of running performance counter to\r | |
180 | time value in unit of nanoseconds.\r | |
181 | \r | |
182 | @param Ticks The number of elapsed ticks of running performance counter.\r | |
183 | \r | |
184 | @return The elapsed time in nanoseconds.\r | |
185 | \r | |
186 | **/\r | |
187 | UINT64\r | |
188 | EFIAPI\r | |
189 | GetTimeInNanoSecond (\r | |
ac0a286f | 190 | IN UINT64 Ticks\r |
b9610b9c | 191 | )\r |
192 | {\r | |
193 | UINT64 NanoSeconds;\r | |
194 | UINT32 Remainder;\r | |
195 | \r | |
196 | //\r | |
197 | // Ticks\r | |
198 | // Time = --------- x 1,000,000,000\r | |
199 | // Frequency\r | |
200 | //\r | |
201 | NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, ACPI_TIMER_FREQUENCY, &Remainder), 1000000000u);\r | |
202 | \r | |
203 | //\r | |
204 | // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000)\r | |
205 | // will not overflow 64-bit.\r | |
206 | //\r | |
ac0a286f | 207 | NanoSeconds += DivU64x32 (MultU64x32 ((UINT64)Remainder, 1000000000u), ACPI_TIMER_FREQUENCY);\r |
b9610b9c | 208 | \r |
209 | return NanoSeconds;\r | |
210 | }\r |