]>
Commit | Line | Data |
---|---|---|
da9675a2 | 1 | /** @file\r |
2 | Generic ARM implementation of TimerLib.h\r | |
3 | \r | |
27331bff OM |
4 | Copyright (c) 2011-2014, ARM Limited. All rights reserved.\r |
5 | \r | |
da9675a2 | 6 | This program and the accompanying materials\r |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | \r | |
17 | #include <Base.h>\r | |
168d7245 | 18 | #include <Library/ArmLib.h>\r |
da9675a2 | 19 | #include <Library/BaseLib.h>\r |
20 | #include <Library/TimerLib.h>\r | |
21 | #include <Library/DebugLib.h>\r | |
22 | #include <Library/PcdLib.h>\r | |
4f6d34b4 | 23 | #include <Library/ArmGenericTimerCounterLib.h>\r |
da9675a2 | 24 | \r |
25 | #define TICKS_PER_MICRO_SEC (PcdGet32 (PcdArmArchTimerFreqInHz)/1000000U)\r | |
26 | \r | |
27 | RETURN_STATUS\r | |
28 | EFIAPI\r | |
31d196c1 | 29 | TimerConstructor (\r |
da9675a2 | 30 | VOID\r |
31 | )\r | |
32 | {\r | |
074a67fc LE |
33 | //\r |
34 | // Check if the ARM Generic Timer Extension is implemented.\r | |
35 | //\r | |
da9675a2 | 36 | if (ArmIsArchTimerImplemented ()) {\r |
da9675a2 | 37 | UINTN TimerFreq;\r |
38 | \r | |
074a67fc LE |
39 | //\r |
40 | // Check if Architectural Timer frequency is valid (should not be 0).\r | |
41 | //\r | |
da9675a2 | 42 | ASSERT (PcdGet32 (PcdArmArchTimerFreqInHz));\r |
43 | \r | |
074a67fc | 44 | //\r |
da9675a2 | 45 | // Check if ticks/uS is not 0. The Architectural timer runs at constant\r |
074a67fc LE |
46 | // frequency, irrespective of CPU frequency. According to General Timer\r |
47 | // Ref manual, lower bound of the frequency is in the range of 1-10MHz.\r | |
48 | //\r | |
da9675a2 | 49 | ASSERT (TICKS_PER_MICRO_SEC);\r |
50 | \r | |
25402f5d | 51 | #ifdef MDE_CPU_ARM\r |
074a67fc LE |
52 | //\r |
53 | // Only set the frequency for ARMv7. We expect the secure firmware to\r | |
54 | // have already done it.\r | |
55 | // If the security extension is not implemented, set Timer Frequency\r | |
56 | // here.\r | |
57 | //\r | |
27331bff | 58 | if ((ArmReadIdPfr1 () & ARM_PFR1_SEC) == 0x0) {\r |
4f6d34b4 | 59 | ArmGenericTimerSetTimerFreq (PcdGet32 (PcdArmArchTimerFreqInHz));\r |
da9675a2 | 60 | }\r |
25402f5d | 61 | #endif\r |
da9675a2 | 62 | \r |
074a67fc LE |
63 | //\r |
64 | // Architectural Timer Frequency must be set in the Secure privileged\r | |
65 | // mode (if secure extension is supported).\r | |
66 | // If the reset value (0) is returned, just ASSERT.\r | |
67 | //\r | |
4f6d34b4 | 68 | TimerFreq = ArmGenericTimerGetTimerFreq ();\r |
25402f5d | 69 | ASSERT (TimerFreq != 0);\r |
da9675a2 | 70 | } else {\r |
ce88684e | 71 | DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, hence this library can not be used.\n"));\r |
da9675a2 | 72 | ASSERT (0);\r |
73 | }\r | |
74 | \r | |
75 | return RETURN_SUCCESS;\r | |
76 | }\r | |
77 | \r | |
78 | \r | |
79 | /**\r | |
80 | Stalls the CPU for the number of microseconds specified by MicroSeconds.\r | |
81 | \r | |
82 | @param MicroSeconds The minimum number of microseconds to delay.\r | |
83 | \r | |
84 | @return The value of MicroSeconds inputted.\r | |
85 | \r | |
86 | **/\r | |
87 | UINTN\r | |
88 | EFIAPI\r | |
89 | MicroSecondDelay (\r | |
90 | IN UINTN MicroSeconds\r | |
91 | )\r | |
92 | {\r | |
93 | UINT64 TimerTicks64;\r | |
94 | UINT64 SystemCounterVal;\r | |
95 | \r | |
33292af5 OM |
96 | // Calculate counter ticks that can represent requested delay:\r |
97 | // = MicroSeconds x TICKS_PER_MICRO_SEC\r | |
98 | // = MicroSeconds x Frequency.10^-6\r | |
4a292893 | 99 | TimerTicks64 = ((UINT64)MicroSeconds * PcdGet32 (PcdArmArchTimerFreqInHz)) / 1000000U;\r |
da9675a2 | 100 | \r |
101 | // Read System Counter value\r | |
4f6d34b4 | 102 | SystemCounterVal = ArmGenericTimerGetSystemCount ();\r |
da9675a2 | 103 | \r |
104 | TimerTicks64 += SystemCounterVal;\r | |
105 | \r | |
106 | // Wait until delay count is expired.\r | |
107 | while (SystemCounterVal < TimerTicks64) {\r | |
4f6d34b4 | 108 | SystemCounterVal = ArmGenericTimerGetSystemCount ();\r |
da9675a2 | 109 | }\r |
110 | \r | |
111 | return MicroSeconds;\r | |
112 | }\r | |
113 | \r | |
114 | \r | |
115 | /**\r | |
116 | Stalls the CPU for at least the given number of nanoseconds.\r | |
117 | \r | |
118 | Stalls the CPU for the number of nanoseconds specified by NanoSeconds.\r | |
119 | \r | |
120 | When the timer frequency is 1MHz, each tick corresponds to 1 microsecond.\r | |
121 | Therefore, the nanosecond delay will be rounded up to the nearest 1 microsecond.\r | |
122 | \r | |
123 | @param NanoSeconds The minimum number of nanoseconds to delay.\r | |
124 | \r | |
ce88684e | 125 | @return The value of NanoSeconds inputed.\r |
da9675a2 | 126 | \r |
127 | **/\r | |
128 | UINTN\r | |
129 | EFIAPI\r | |
130 | NanoSecondDelay (\r | |
131 | IN UINTN NanoSeconds\r | |
132 | )\r | |
133 | {\r | |
134 | UINTN MicroSeconds;\r | |
135 | \r | |
136 | // Round up to 1us Tick Number\r | |
137 | MicroSeconds = NanoSeconds / 1000;\r | |
138 | MicroSeconds += ((NanoSeconds % 1000) == 0) ? 0 : 1;\r | |
139 | \r | |
140 | MicroSecondDelay (MicroSeconds);\r | |
141 | \r | |
142 | return NanoSeconds;\r | |
143 | }\r | |
144 | \r | |
145 | /**\r | |
146 | Retrieves the current value of a 64-bit free running performance counter.\r | |
147 | \r | |
148 | The counter can either count up by 1 or count down by 1. If the physical\r | |
149 | performance counter counts by a larger increment, then the counter values\r | |
150 | must be translated. The properties of the counter can be retrieved from\r | |
151 | GetPerformanceCounterProperties().\r | |
152 | \r | |
153 | @return The current value of the free running performance counter.\r | |
154 | \r | |
155 | **/\r | |
156 | UINT64\r | |
157 | EFIAPI\r | |
158 | GetPerformanceCounter (\r | |
159 | VOID\r | |
160 | )\r | |
161 | {\r | |
162 | // Just return the value of system count\r | |
4f6d34b4 | 163 | return ArmGenericTimerGetSystemCount ();\r |
da9675a2 | 164 | }\r |
165 | \r | |
166 | /**\r | |
167 | Retrieves the 64-bit frequency in Hz and the range of performance counter\r | |
168 | values.\r | |
169 | \r | |
170 | If StartValue is not NULL, then the value that the performance counter starts\r | |
171 | with immediately after is it rolls over is returned in StartValue. If\r | |
172 | EndValue is not NULL, then the value that the performance counter end with\r | |
173 | immediately before it rolls over is returned in EndValue. The 64-bit\r | |
174 | frequency of the performance counter in Hz is always returned. If StartValue\r | |
175 | is less than EndValue, then the performance counter counts up. If StartValue\r | |
176 | is greater than EndValue, then the performance counter counts down. For\r | |
177 | example, a 64-bit free running counter that counts up would have a StartValue\r | |
178 | of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter\r | |
179 | that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0.\r | |
180 | \r | |
181 | @param StartValue The value the performance counter starts with when it\r | |
182 | rolls over.\r | |
183 | @param EndValue The value that the performance counter ends with before\r | |
184 | it rolls over.\r | |
185 | \r | |
186 | @return The frequency in Hz.\r | |
187 | \r | |
188 | **/\r | |
189 | UINT64\r | |
190 | EFIAPI\r | |
191 | GetPerformanceCounterProperties (\r | |
192 | OUT UINT64 *StartValue, OPTIONAL\r | |
193 | OUT UINT64 *EndValue OPTIONAL\r | |
194 | )\r | |
195 | {\r | |
196 | if (StartValue != NULL) {\r | |
197 | // Timer starts with the reload value\r | |
198 | *StartValue = (UINT64)0ULL ;\r | |
199 | }\r | |
200 | \r | |
201 | if (EndValue != NULL) {\r | |
202 | // Timer counts down to 0x0\r | |
89bbce11 | 203 | *EndValue = 0xFFFFFFFFFFFFFFFFUL;\r |
da9675a2 | 204 | }\r |
205 | \r | |
4f6d34b4 | 206 | return (UINT64)ArmGenericTimerGetTimerFreq ();\r |
da9675a2 | 207 | }\r |