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