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