1 | /** @file
|
---|
2 | Timer Architecture Protocol driver of the ARM flavor
|
---|
3 |
|
---|
4 | Copyright (c) 2011-2021, Arm Limited. All rights reserved.<BR>
|
---|
5 |
|
---|
6 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
7 |
|
---|
8 | **/
|
---|
9 |
|
---|
10 | #include <PiDxe.h>
|
---|
11 |
|
---|
12 | #include <Library/ArmLib.h>
|
---|
13 | #include <Library/BaseLib.h>
|
---|
14 | #include <Library/DebugLib.h>
|
---|
15 | #include <Library/BaseMemoryLib.h>
|
---|
16 | #include <Library/UefiBootServicesTableLib.h>
|
---|
17 | #include <Library/UefiLib.h>
|
---|
18 | #include <Library/PcdLib.h>
|
---|
19 | #include <Library/IoLib.h>
|
---|
20 | #include <Library/ArmGenericTimerCounterLib.h>
|
---|
21 |
|
---|
22 | #include <Protocol/Timer.h>
|
---|
23 | #include <Protocol/HardwareInterrupt.h>
|
---|
24 |
|
---|
25 | // The notification function to call on every timer interrupt.
|
---|
26 | EFI_TIMER_NOTIFY mTimerNotifyFunction = (EFI_TIMER_NOTIFY)NULL;
|
---|
27 | EFI_EVENT EfiExitBootServicesEvent = (EFI_EVENT)NULL;
|
---|
28 |
|
---|
29 | // The current period of the timer interrupt
|
---|
30 | UINT64 mTimerPeriod = 0;
|
---|
31 | // The latest Timer Tick calculated for mTimerPeriod
|
---|
32 | UINT64 mTimerTicks = 0;
|
---|
33 | // Number of elapsed period since the last Timer interrupt
|
---|
34 | UINT64 mElapsedPeriod = 1;
|
---|
35 |
|
---|
36 | // Cached copy of the Hardware Interrupt protocol instance
|
---|
37 | EFI_HARDWARE_INTERRUPT_PROTOCOL *gInterrupt = NULL;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | This function registers the handler NotifyFunction so it is called every time
|
---|
41 | the timer interrupt fires. It also passes the amount of time since the last
|
---|
42 | handler call to the NotifyFunction. If NotifyFunction is NULL, then the
|
---|
43 | handler is unregistered. If the handler is registered, then EFI_SUCCESS is
|
---|
44 | returned. If the CPU does not support registering a timer interrupt handler,
|
---|
45 | then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
|
---|
46 | when a handler is already registered, then EFI_ALREADY_STARTED is returned.
|
---|
47 | If an attempt is made to unregister a handler when a handler is not registered,
|
---|
48 | then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
|
---|
49 | register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
|
---|
50 | is returned.
|
---|
51 |
|
---|
52 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.
|
---|
53 | @param NotifyFunction The function to call when a timer interrupt fires. This
|
---|
54 | function executes at TPL_HIGH_LEVEL. The DXE Core will
|
---|
55 | register a handler for the timer interrupt, so it can know
|
---|
56 | how much time has passed. This information is used to
|
---|
57 | signal timer based events. NULL will unregister the handler.
|
---|
58 | @retval EFI_SUCCESS The timer handler was registered.
|
---|
59 | @retval EFI_UNSUPPORTED The platform does not support timer interrupts.
|
---|
60 | @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already
|
---|
61 | registered.
|
---|
62 | @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not
|
---|
63 | previously registered.
|
---|
64 | @retval EFI_DEVICE_ERROR The timer handler could not be registered.
|
---|
65 |
|
---|
66 | **/
|
---|
67 | EFI_STATUS
|
---|
68 | EFIAPI
|
---|
69 | TimerDriverRegisterHandler (
|
---|
70 | IN EFI_TIMER_ARCH_PROTOCOL *This,
|
---|
71 | IN EFI_TIMER_NOTIFY NotifyFunction
|
---|
72 | )
|
---|
73 | {
|
---|
74 | if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {
|
---|
75 | return EFI_INVALID_PARAMETER;
|
---|
76 | }
|
---|
77 |
|
---|
78 | if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {
|
---|
79 | return EFI_ALREADY_STARTED;
|
---|
80 | }
|
---|
81 |
|
---|
82 | mTimerNotifyFunction = NotifyFunction;
|
---|
83 |
|
---|
84 | return EFI_SUCCESS;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | Disable the timer
|
---|
89 | **/
|
---|
90 | VOID
|
---|
91 | EFIAPI
|
---|
92 | ExitBootServicesEvent (
|
---|
93 | IN EFI_EVENT Event,
|
---|
94 | IN VOID *Context
|
---|
95 | )
|
---|
96 | {
|
---|
97 | ArmGenericTimerDisableTimer ();
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 |
|
---|
102 | This function adjusts the period of timer interrupts to the value specified
|
---|
103 | by TimerPeriod. If the timer period is updated, then the selected timer
|
---|
104 | period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
|
---|
105 | the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
|
---|
106 | If an error occurs while attempting to update the timer period, then the
|
---|
107 | timer hardware will be put back in its state prior to this call, and
|
---|
108 | EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
|
---|
109 | is disabled. This is not the same as disabling the CPU's interrupts.
|
---|
110 | Instead, it must either turn off the timer hardware, or it must adjust the
|
---|
111 | interrupt controller so that a CPU interrupt is not generated when the timer
|
---|
112 | interrupt fires.
|
---|
113 |
|
---|
114 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.
|
---|
115 | @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If
|
---|
116 | the timer hardware is not programmable, then EFI_UNSUPPORTED is
|
---|
117 | returned. If the timer is programmable, then the timer period
|
---|
118 | will be rounded up to the nearest timer period that is supported
|
---|
119 | by the timer hardware. If TimerPeriod is set to 0, then the
|
---|
120 | timer interrupts will be disabled.
|
---|
121 |
|
---|
122 |
|
---|
123 | @retval EFI_SUCCESS The timer period was changed.
|
---|
124 | @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.
|
---|
125 | @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.
|
---|
126 |
|
---|
127 | **/
|
---|
128 | EFI_STATUS
|
---|
129 | EFIAPI
|
---|
130 | TimerDriverSetTimerPeriod (
|
---|
131 | IN EFI_TIMER_ARCH_PROTOCOL *This,
|
---|
132 | IN UINT64 TimerPeriod
|
---|
133 | )
|
---|
134 | {
|
---|
135 | UINT64 CounterValue;
|
---|
136 | UINT64 TimerTicks;
|
---|
137 | EFI_TPL OriginalTPL;
|
---|
138 |
|
---|
139 | // Always disable the timer
|
---|
140 | ArmGenericTimerDisableTimer ();
|
---|
141 |
|
---|
142 | if (TimerPeriod != 0) {
|
---|
143 | // mTimerTicks = TimerPeriod in 1ms unit x Frequency.10^-3
|
---|
144 | // = TimerPeriod.10^-4 x Frequency.10^-3
|
---|
145 | // = (TimerPeriod x Frequency) x 10^-7
|
---|
146 | TimerTicks = MultU64x32 (TimerPeriod, ArmGenericTimerGetTimerFreq ());
|
---|
147 | TimerTicks = DivU64x32 (TimerTicks, 10000000U);
|
---|
148 |
|
---|
149 | // Raise TPL to update the mTimerTicks and mTimerPeriod to ensure these values
|
---|
150 | // are coherent in the interrupt handler
|
---|
151 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
|
---|
152 |
|
---|
153 | mTimerTicks = TimerTicks;
|
---|
154 | mTimerPeriod = TimerPeriod;
|
---|
155 | mElapsedPeriod = 1;
|
---|
156 |
|
---|
157 | gBS->RestoreTPL (OriginalTPL);
|
---|
158 |
|
---|
159 | // Get value of the current timer
|
---|
160 | CounterValue = ArmGenericTimerGetSystemCount ();
|
---|
161 | // Set the interrupt in Current Time + mTimerTick
|
---|
162 | ArmGenericTimerSetCompareVal (CounterValue + mTimerTicks);
|
---|
163 |
|
---|
164 | // Enable the timer
|
---|
165 | ArmGenericTimerEnableTimer ();
|
---|
166 | } else {
|
---|
167 | // Save the new timer period
|
---|
168 | mTimerPeriod = TimerPeriod;
|
---|
169 | // Reset the elapsed period
|
---|
170 | mElapsedPeriod = 1;
|
---|
171 | }
|
---|
172 |
|
---|
173 | return EFI_SUCCESS;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | This function retrieves the period of timer interrupts in 100 ns units,
|
---|
178 | returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
|
---|
179 | is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
|
---|
180 | returned, then the timer is currently disabled.
|
---|
181 |
|
---|
182 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.
|
---|
183 | @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
|
---|
184 | 0 is returned, then the timer is currently disabled.
|
---|
185 |
|
---|
186 |
|
---|
187 | @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
|
---|
188 | @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
|
---|
189 |
|
---|
190 | **/
|
---|
191 | EFI_STATUS
|
---|
192 | EFIAPI
|
---|
193 | TimerDriverGetTimerPeriod (
|
---|
194 | IN EFI_TIMER_ARCH_PROTOCOL *This,
|
---|
195 | OUT UINT64 *TimerPeriod
|
---|
196 | )
|
---|
197 | {
|
---|
198 | if (TimerPeriod == NULL) {
|
---|
199 | return EFI_INVALID_PARAMETER;
|
---|
200 | }
|
---|
201 |
|
---|
202 | *TimerPeriod = mTimerPeriod;
|
---|
203 | return EFI_SUCCESS;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | This function generates a soft timer interrupt. If the platform does not support soft
|
---|
208 | timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
|
---|
209 | If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
|
---|
210 | service, then a soft timer interrupt will be generated. If the timer interrupt is
|
---|
211 | enabled when this service is called, then the registered handler will be invoked. The
|
---|
212 | registered handler should not be able to distinguish a hardware-generated timer
|
---|
213 | interrupt from a software-generated timer interrupt.
|
---|
214 |
|
---|
215 | @param This The EFI_TIMER_ARCH_PROTOCOL instance.
|
---|
216 |
|
---|
217 | @retval EFI_SUCCESS The soft timer interrupt was generated.
|
---|
218 | @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
|
---|
219 |
|
---|
220 | **/
|
---|
221 | EFI_STATUS
|
---|
222 | EFIAPI
|
---|
223 | TimerDriverGenerateSoftInterrupt (
|
---|
224 | IN EFI_TIMER_ARCH_PROTOCOL *This
|
---|
225 | )
|
---|
226 | {
|
---|
227 | return EFI_UNSUPPORTED;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | Interface structure for the Timer Architectural Protocol.
|
---|
232 |
|
---|
233 | @par Protocol Description:
|
---|
234 | This protocol provides the services to initialize a periodic timer
|
---|
235 | interrupt, and to register a handler that is called each time the timer
|
---|
236 | interrupt fires. It may also provide a service to adjust the rate of the
|
---|
237 | periodic timer interrupt. When a timer interrupt occurs, the handler is
|
---|
238 | passed the amount of time that has passed since the previous timer
|
---|
239 | interrupt.
|
---|
240 |
|
---|
241 | @param RegisterHandler
|
---|
242 | Registers a handler that will be called each time the
|
---|
243 | timer interrupt fires. TimerPeriod defines the minimum
|
---|
244 | time between timer interrupts, so TimerPeriod will also
|
---|
245 | be the minimum time between calls to the registered
|
---|
246 | handler.
|
---|
247 |
|
---|
248 | @param SetTimerPeriod
|
---|
249 | Sets the period of the timer interrupt in 100 nS units.
|
---|
250 | This function is optional, and may return EFI_UNSUPPORTED.
|
---|
251 | If this function is supported, then the timer period will
|
---|
252 | be rounded up to the nearest supported timer period.
|
---|
253 |
|
---|
254 |
|
---|
255 | @param GetTimerPeriod
|
---|
256 | Retrieves the period of the timer interrupt in 100 nS units.
|
---|
257 |
|
---|
258 | @param GenerateSoftInterrupt
|
---|
259 | Generates a soft timer interrupt that simulates the firing of
|
---|
260 | the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
|
---|
261 | a period of time.
|
---|
262 |
|
---|
263 | **/
|
---|
264 | EFI_TIMER_ARCH_PROTOCOL gTimer = {
|
---|
265 | TimerDriverRegisterHandler,
|
---|
266 | TimerDriverSetTimerPeriod,
|
---|
267 | TimerDriverGetTimerPeriod,
|
---|
268 | TimerDriverGenerateSoftInterrupt
|
---|
269 | };
|
---|
270 |
|
---|
271 | /**
|
---|
272 |
|
---|
273 | C Interrupt Handler called in the interrupt context when Source interrupt is active.
|
---|
274 |
|
---|
275 |
|
---|
276 | @param Source Source of the interrupt. Hardware routing off a specific platform defines
|
---|
277 | what source means.
|
---|
278 |
|
---|
279 | @param SystemContext Pointer to system register context. Mostly used by debuggers and will
|
---|
280 | update the system context after the return from the interrupt if
|
---|
281 | modified. Don't change these values unless you know what you are doing
|
---|
282 |
|
---|
283 | **/
|
---|
284 | VOID
|
---|
285 | EFIAPI
|
---|
286 | TimerInterruptHandler (
|
---|
287 | IN HARDWARE_INTERRUPT_SOURCE Source,
|
---|
288 | IN EFI_SYSTEM_CONTEXT SystemContext
|
---|
289 | )
|
---|
290 | {
|
---|
291 | EFI_TPL OriginalTPL;
|
---|
292 | UINT64 CurrentValue;
|
---|
293 | UINT64 CompareValue;
|
---|
294 |
|
---|
295 | //
|
---|
296 | // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
|
---|
297 | // that raise to TPL_HIGH and then restore back to current level. Thus we need
|
---|
298 | // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
|
---|
299 | //
|
---|
300 | OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
|
---|
301 |
|
---|
302 | // Signal end of interrupt early to help avoid losing subsequent ticks
|
---|
303 | // from long duration handlers
|
---|
304 | gInterrupt->EndOfInterrupt (gInterrupt, Source);
|
---|
305 |
|
---|
306 | // Check if the timer interrupt is active
|
---|
307 | if ((ArmGenericTimerGetTimerCtrlReg ()) & ARM_ARCH_TIMER_ISTATUS) {
|
---|
308 | if (mTimerNotifyFunction != 0) {
|
---|
309 | mTimerNotifyFunction (mTimerPeriod * mElapsedPeriod);
|
---|
310 | }
|
---|
311 |
|
---|
312 | //
|
---|
313 | // Reload the Timer
|
---|
314 | //
|
---|
315 |
|
---|
316 | // Get current counter value
|
---|
317 | CurrentValue = ArmGenericTimerGetSystemCount ();
|
---|
318 | // Get the counter value to compare with
|
---|
319 | CompareValue = ArmGenericTimerGetCompareVal ();
|
---|
320 |
|
---|
321 | // This loop is needed in case we missed interrupts (eg: case when the interrupt handling
|
---|
322 | // has taken longer than mTickPeriod).
|
---|
323 | // Note: Physical Counter is counting up
|
---|
324 | mElapsedPeriod = 0;
|
---|
325 | do {
|
---|
326 | CompareValue += mTimerTicks;
|
---|
327 | mElapsedPeriod++;
|
---|
328 | } while (CompareValue < CurrentValue);
|
---|
329 |
|
---|
330 | // Set next compare value
|
---|
331 | ArmGenericTimerSetCompareVal (CompareValue);
|
---|
332 | ArmGenericTimerReenableTimer ();
|
---|
333 | ArmInstructionSynchronizationBarrier ();
|
---|
334 | }
|
---|
335 |
|
---|
336 | gBS->RestoreTPL (OriginalTPL);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | Initialize the state information for the Timer Architectural Protocol and
|
---|
341 | the Timer Debug support protocol that allows the debugger to break into a
|
---|
342 | running program.
|
---|
343 |
|
---|
344 | @param ImageHandle of the loaded driver
|
---|
345 | @param SystemTable Pointer to the System Table
|
---|
346 |
|
---|
347 | @retval EFI_SUCCESS Protocol registered
|
---|
348 | @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
|
---|
349 | @retval EFI_DEVICE_ERROR Hardware problems
|
---|
350 |
|
---|
351 | **/
|
---|
352 | EFI_STATUS
|
---|
353 | EFIAPI
|
---|
354 | TimerInitialize (
|
---|
355 | IN EFI_HANDLE ImageHandle,
|
---|
356 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
357 | )
|
---|
358 | {
|
---|
359 | EFI_HANDLE Handle;
|
---|
360 | EFI_STATUS Status;
|
---|
361 | UINTN TimerCtrlReg;
|
---|
362 | UINT32 TimerHypIntrNum;
|
---|
363 |
|
---|
364 | if (ArmIsArchTimerImplemented () == 0) {
|
---|
365 | DEBUG ((DEBUG_ERROR, "ARM Architectural Timer is not available in the CPU, hence can't use this Driver \n"));
|
---|
366 | ASSERT (0);
|
---|
367 | }
|
---|
368 |
|
---|
369 | // Find the interrupt controller protocol. ASSERT if not found.
|
---|
370 | Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt);
|
---|
371 | ASSERT_EFI_ERROR (Status);
|
---|
372 |
|
---|
373 | // Disable the timer
|
---|
374 | TimerCtrlReg = ArmGenericTimerGetTimerCtrlReg ();
|
---|
375 | TimerCtrlReg |= ARM_ARCH_TIMER_IMASK;
|
---|
376 | TimerCtrlReg &= ~ARM_ARCH_TIMER_ENABLE;
|
---|
377 | ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg);
|
---|
378 | Status = TimerDriverSetTimerPeriod (&gTimer, 0);
|
---|
379 | ASSERT_EFI_ERROR (Status);
|
---|
380 |
|
---|
381 | // Install secure and Non-secure interrupt handlers
|
---|
382 | // Note: Because it is not possible to determine the security state of the
|
---|
383 | // CPU dynamically, we just install interrupt handler for both sec and non-sec
|
---|
384 | // timer PPI
|
---|
385 | Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerVirtIntrNum), TimerInterruptHandler);
|
---|
386 | ASSERT_EFI_ERROR (Status);
|
---|
387 |
|
---|
388 | //
|
---|
389 | // The hypervisor timer interrupt may be omitted by implementations that
|
---|
390 | // execute under virtualization.
|
---|
391 | //
|
---|
392 | TimerHypIntrNum = PcdGet32 (PcdArmArchTimerHypIntrNum);
|
---|
393 | if (TimerHypIntrNum != 0) {
|
---|
394 | Status = gInterrupt->RegisterInterruptSource (gInterrupt, TimerHypIntrNum, TimerInterruptHandler);
|
---|
395 | ASSERT_EFI_ERROR (Status);
|
---|
396 | }
|
---|
397 |
|
---|
398 | Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerSecIntrNum), TimerInterruptHandler);
|
---|
399 | ASSERT_EFI_ERROR (Status);
|
---|
400 |
|
---|
401 | Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerIntrNum), TimerInterruptHandler);
|
---|
402 | ASSERT_EFI_ERROR (Status);
|
---|
403 |
|
---|
404 | // Set up default timer
|
---|
405 | Status = TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32 (PcdTimerPeriod)); // TIMER_DEFAULT_PERIOD
|
---|
406 | ASSERT_EFI_ERROR (Status);
|
---|
407 |
|
---|
408 | Handle = NULL;
|
---|
409 | // Install the Timer Architectural Protocol onto a new handle
|
---|
410 | Status = gBS->InstallMultipleProtocolInterfaces (
|
---|
411 | &Handle,
|
---|
412 | &gEfiTimerArchProtocolGuid,
|
---|
413 | &gTimer,
|
---|
414 | NULL
|
---|
415 | );
|
---|
416 | ASSERT_EFI_ERROR (Status);
|
---|
417 |
|
---|
418 | // Everything is ready, unmask and enable timer interrupts
|
---|
419 | TimerCtrlReg = ARM_ARCH_TIMER_ENABLE;
|
---|
420 | ArmGenericTimerSetTimerCtrlReg (TimerCtrlReg);
|
---|
421 |
|
---|
422 | // Register for an ExitBootServicesEvent
|
---|
423 | Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, ExitBootServicesEvent, NULL, &EfiExitBootServicesEvent);
|
---|
424 | ASSERT_EFI_ERROR (Status);
|
---|
425 |
|
---|
426 | return Status;
|
---|
427 | }
|
---|