]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Drivers/TimerDxe/TimerDxe.c
ARM Packages: Fixed line endings
[mirror_edk2.git] / ArmPkg / Drivers / TimerDxe / TimerDxe.c
1 /** @file
2 Timer Architecture Protocol driver of the ARM flavor
3
4 Copyright (c) 2011 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/ArmV7ArchTimerLib.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 // Convert TimerPeriod to micro sec units
145 TimerTicks = DivU64x32 (TimerPeriod, 10);
146
147 TimerTicks = MultU64x32 (TimerTicks, (PcdGet32(PcdArmArchTimerFreqInHz)/1000000));
148
149 ArmArchTimerSetTimerVal((UINTN)TimerTicks);
150
151 // Enable the timer
152 ArmArchTimerEnableTimer ();
153 }
154
155 // Save the new timer period
156 mTimerPeriod = TimerPeriod;
157 return EFI_SUCCESS;
158 }
159
160 /**
161 This function retrieves the period of timer interrupts in 100 ns units,
162 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
163 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
164 returned, then the timer is currently disabled.
165
166 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
167 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If
168 0 is returned, then the timer is currently disabled.
169
170
171 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.
172 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.
173
174 **/
175 EFI_STATUS
176 EFIAPI
177 TimerDriverGetTimerPeriod (
178 IN EFI_TIMER_ARCH_PROTOCOL *This,
179 OUT UINT64 *TimerPeriod
180 )
181 {
182 if (TimerPeriod == NULL) {
183 return EFI_INVALID_PARAMETER;
184 }
185
186 *TimerPeriod = mTimerPeriod;
187 return EFI_SUCCESS;
188 }
189
190 /**
191 This function generates a soft timer interrupt. If the platform does not support soft
192 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
193 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
194 service, then a soft timer interrupt will be generated. If the timer interrupt is
195 enabled when this service is called, then the registered handler will be invoked. The
196 registered handler should not be able to distinguish a hardware-generated timer
197 interrupt from a software-generated timer interrupt.
198
199 @param This The EFI_TIMER_ARCH_PROTOCOL instance.
200
201 @retval EFI_SUCCESS The soft timer interrupt was generated.
202 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.
203
204 **/
205 EFI_STATUS
206 EFIAPI
207 TimerDriverGenerateSoftInterrupt (
208 IN EFI_TIMER_ARCH_PROTOCOL *This
209 )
210 {
211 return EFI_UNSUPPORTED;
212 }
213
214 /**
215 Interface structure for the Timer Architectural Protocol.
216
217 @par Protocol Description:
218 This protocol provides the services to initialize a periodic timer
219 interrupt, and to register a handler that is called each time the timer
220 interrupt fires. It may also provide a service to adjust the rate of the
221 periodic timer interrupt. When a timer interrupt occurs, the handler is
222 passed the amount of time that has passed since the previous timer
223 interrupt.
224
225 @param RegisterHandler
226 Registers a handler that will be called each time the
227 timer interrupt fires. TimerPeriod defines the minimum
228 time between timer interrupts, so TimerPeriod will also
229 be the minimum time between calls to the registered
230 handler.
231
232 @param SetTimerPeriod
233 Sets the period of the timer interrupt in 100 nS units.
234 This function is optional, and may return EFI_UNSUPPORTED.
235 If this function is supported, then the timer period will
236 be rounded up to the nearest supported timer period.
237
238
239 @param GetTimerPeriod
240 Retrieves the period of the timer interrupt in 100 nS units.
241
242 @param GenerateSoftInterrupt
243 Generates a soft timer interrupt that simulates the firing of
244 the timer interrupt. This service can be used to invoke the registered handler if the timer interrupt has been masked for
245 a period of time.
246
247 **/
248 EFI_TIMER_ARCH_PROTOCOL gTimer = {
249 TimerDriverRegisterHandler,
250 TimerDriverSetTimerPeriod,
251 TimerDriverGetTimerPeriod,
252 TimerDriverGenerateSoftInterrupt
253 };
254
255 /**
256
257 C Interrupt Handler called in the interrupt context when Source interrupt is active.
258
259
260 @param Source Source of the interrupt. Hardware routing off a specific platform defines
261 what source means.
262
263 @param SystemContext Pointer to system register context. Mostly used by debuggers and will
264 update the system context after the return from the interrupt if
265 modified. Don't change these values unless you know what you are doing
266
267 **/
268 VOID
269 EFIAPI
270 TimerInterruptHandler (
271 IN HARDWARE_INTERRUPT_SOURCE Source,
272 IN EFI_SYSTEM_CONTEXT SystemContext
273 )
274 {
275 EFI_TPL OriginalTPL;
276
277 //
278 // DXE core uses this callback for the EFI timer tick. The DXE core uses locks
279 // that raise to TPL_HIGH and then restore back to current level. Thus we need
280 // to make sure TPL level is set to TPL_HIGH while we are handling the timer tick.
281 //
282 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
283
284 // Check if the timer interrupt is active
285 if ((ArmArchTimerGetTimerCtrlReg () ) & ARM_ARCH_TIMER_ISTATUS) {
286
287 // Signal end of interrupt early to help avoid losing subsequent ticks from long duration handlers
288 gInterrupt->EndOfInterrupt (gInterrupt, Source);
289
290 if (mTimerNotifyFunction) {
291 mTimerNotifyFunction (mTimerPeriod);
292 }
293
294 // Reload the Timer
295 TimerDriverSetTimerPeriod (&gTimer, FixedPcdGet32(PcdTimerPeriod));
296 }
297
298 // Enable timer interrupts
299 gInterrupt->EnableInterruptSource (gInterrupt, Source);
300
301 gBS->RestoreTPL (OriginalTPL);
302 }
303
304
305 /**
306 Initialize the state information for the Timer Architectural Protocol and
307 the Timer Debug support protocol that allows the debugger to break into a
308 running program.
309
310 @param ImageHandle of the loaded driver
311 @param SystemTable Pointer to the System Table
312
313 @retval EFI_SUCCESS Protocol registered
314 @retval EFI_OUT_OF_RESOURCES Cannot allocate protocol data structure
315 @retval EFI_DEVICE_ERROR Hardware problems
316
317 **/
318 EFI_STATUS
319 EFIAPI
320 TimerInitialize (
321 IN EFI_HANDLE ImageHandle,
322 IN EFI_SYSTEM_TABLE *SystemTable
323 )
324 {
325 EFI_HANDLE Handle = NULL;
326 EFI_STATUS Status;
327 UINTN TimerCtrlReg;
328
329 if (ArmIsArchTimerImplemented () == 0) {
330 DEBUG ((EFI_D_ERROR, "ARM Architectural Timer is not available in the CPU, hence cann't use this Driver \n"));
331 ASSERT (0);
332 }
333
334 // Find the interrupt controller protocol. ASSERT if not found.
335 Status = gBS->LocateProtocol (&gHardwareInterruptProtocolGuid, NULL, (VOID **)&gInterrupt);
336 ASSERT_EFI_ERROR (Status);
337
338 // Disable the timer
339 Status = TimerDriverSetTimerPeriod (&gTimer, 0);
340 ASSERT_EFI_ERROR (Status);
341
342 // Install secure and Non-secure interrupt handlers
343 // Note: Because it is not possible to determine the security state of the
344 // CPU dynamically, we just install interrupt handler for both sec and non-sec
345 // timer PPI
346 Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerSecIntrNum), TimerInterruptHandler);
347 ASSERT_EFI_ERROR (Status);
348
349 Status = gInterrupt->RegisterInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerIntrNum), TimerInterruptHandler);
350 ASSERT_EFI_ERROR (Status);
351
352 // Unmask timer interrupts
353 TimerCtrlReg = ArmArchTimerGetTimerCtrlReg ();
354 TimerCtrlReg &= ~ARM_ARCH_TIMER_IMASK;
355 ArmArchTimerSetTimerCtrlReg (TimerCtrlReg);
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 // enable Secure timer interrupts
370 Status = gInterrupt->EnableInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerSecIntrNum));
371
372 // enable NonSecure timer interrupts
373 Status = gInterrupt->EnableInterruptSource (gInterrupt, PcdGet32 (PcdArmArchTimerIntrNum));
374
375 // Register for an ExitBootServicesEvent
376 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_NOTIFY, ExitBootServicesEvent, NULL, &EfiExitBootServicesEvent);
377 ASSERT_EFI_ERROR (Status);
378
379 return Status;
380 }