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