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