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