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