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