]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/LocalApicTimerDxe/LocalApicTimerDxe.c
OvmfPkg: Use NestedInterruptTplLib in nested interrupt handlers
[mirror_edk2.git] / OvmfPkg / LocalApicTimerDxe / LocalApicTimerDxe.c
CommitLineData
d668c8bc
AP
1/** @file\r
2 Timer Architectural Protocol as defined in the DXE CIS\r
3\r
4Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>\r
5Copyright (c) 2019, Citrix Systems, Inc.\r
6\r
7SPDX-License-Identifier: BSD-2-Clause-Patent\r
8\r
9**/\r
10\r
a086f4a6
MB
11#include <Library/NestedInterruptTplLib.h>\r
12\r
c2e7be40 13#include "LocalApicTimerDxe.h"\r
d668c8bc
AP
14\r
15//\r
16// The handle onto which the Timer Architectural Protocol will be installed\r
17//\r
ac0a286f 18EFI_HANDLE mTimerHandle = NULL;\r
d668c8bc
AP
19\r
20//\r
21// The Timer Architectural Protocol that this driver produces\r
22//\r
ac0a286f 23EFI_TIMER_ARCH_PROTOCOL mTimer = {\r
d668c8bc
AP
24 TimerDriverRegisterHandler,\r
25 TimerDriverSetTimerPeriod,\r
26 TimerDriverGetTimerPeriod,\r
27 TimerDriverGenerateSoftInterrupt\r
28};\r
29\r
30//\r
31// Pointer to the CPU Architectural Protocol instance\r
32//\r
ac0a286f 33EFI_CPU_ARCH_PROTOCOL *mCpu;\r
d668c8bc
AP
34\r
35//\r
36// The notification function to call on every timer interrupt.\r
37// A bug in the compiler prevents us from initializing this here.\r
38//\r
ac0a286f 39EFI_TIMER_NOTIFY mTimerNotifyFunction;\r
d668c8bc
AP
40\r
41//\r
42// The current period of the timer interrupt\r
43//\r
ac0a286f 44volatile UINT64 mTimerPeriod = 0;\r
d668c8bc
AP
45\r
46//\r
47// Worker Functions\r
48//\r
ac0a286f 49\r
d668c8bc
AP
50/**\r
51 Interrupt Handler.\r
52\r
53 @param InterruptType The type of interrupt that occurred\r
54 @param SystemContext A pointer to the system context when the interrupt occurred\r
55**/\r
56VOID\r
57EFIAPI\r
58TimerInterruptHandler (\r
ac0a286f
MK
59 IN EFI_EXCEPTION_TYPE InterruptType,\r
60 IN EFI_SYSTEM_CONTEXT SystemContext\r
d668c8bc
AP
61 )\r
62{\r
a086f4a6
MB
63 STATIC NESTED_INTERRUPT_STATE NestedInterruptState;\r
64 EFI_TPL OriginalTPL;\r
d668c8bc 65\r
a086f4a6 66 OriginalTPL = NestedInterruptRaiseTPL ();\r
d668c8bc 67\r
9bf473da
MB
68 SendApicEoi ();\r
69\r
d668c8bc
AP
70 if (mTimerNotifyFunction != NULL) {\r
71 //\r
72 // @bug : This does not handle missed timer interrupts\r
73 //\r
74 mTimerNotifyFunction (mTimerPeriod);\r
75 }\r
76\r
a086f4a6 77 NestedInterruptRestoreTPL (OriginalTPL, SystemContext, &NestedInterruptState);\r
d668c8bc
AP
78}\r
79\r
80/**\r
81\r
82 This function registers the handler NotifyFunction so it is called every time\r
83 the timer interrupt fires. It also passes the amount of time since the last\r
84 handler call to the NotifyFunction. If NotifyFunction is NULL, then the\r
85 handler is unregistered. If the handler is registered, then EFI_SUCCESS is\r
86 returned. If the CPU does not support registering a timer interrupt handler,\r
87 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler\r
88 when a handler is already registered, then EFI_ALREADY_STARTED is returned.\r
89 If an attempt is made to unregister a handler when a handler is not registered,\r
90 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to\r
91 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR\r
92 is returned.\r
93\r
94\r
95 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
96 @param NotifyFunction The function to call when a timer interrupt fires. This\r
97 function executes at TPL_HIGH_LEVEL. The DXE Core will\r
98 register a handler for the timer interrupt, so it can know\r
99 how much time has passed. This information is used to\r
100 signal timer based events. NULL will unregister the handler.\r
101\r
102 @retval EFI_SUCCESS The timer handler was registered.\r
103 @retval EFI_UNSUPPORTED The platform does not support timer interrupts.\r
104 @retval EFI_ALREADY_STARTED NotifyFunction is not NULL, and a handler is already\r
105 registered.\r
106 @retval EFI_INVALID_PARAMETER NotifyFunction is NULL, and a handler was not\r
107 previously registered.\r
108 @retval EFI_DEVICE_ERROR The timer handler could not be registered.\r
109\r
110**/\r
111EFI_STATUS\r
112EFIAPI\r
113TimerDriverRegisterHandler (\r
114 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
115 IN EFI_TIMER_NOTIFY NotifyFunction\r
116 )\r
117{\r
118 //\r
119 // Check for invalid parameters\r
120 //\r
ac0a286f 121 if ((NotifyFunction == NULL) && (mTimerNotifyFunction == NULL)) {\r
d668c8bc
AP
122 return EFI_INVALID_PARAMETER;\r
123 }\r
124\r
ac0a286f 125 if ((NotifyFunction != NULL) && (mTimerNotifyFunction != NULL)) {\r
d668c8bc
AP
126 return EFI_ALREADY_STARTED;\r
127 }\r
128\r
129 mTimerNotifyFunction = NotifyFunction;\r
130\r
131 return EFI_SUCCESS;\r
132}\r
133\r
134/**\r
135\r
136 This function adjusts the period of timer interrupts to the value specified\r
137 by TimerPeriod. If the timer period is updated, then the selected timer\r
138 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If\r
139 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.\r
140 If an error occurs while attempting to update the timer period, then the\r
141 timer hardware will be put back in its state prior to this call, and\r
142 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt\r
143 is disabled. This is not the same as disabling the CPU's interrupts.\r
144 Instead, it must either turn off the timer hardware, or it must adjust the\r
145 interrupt controller so that a CPU interrupt is not generated when the timer\r
146 interrupt fires.\r
147\r
148\r
149 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
150 @param TimerPeriod The rate to program the timer interrupt in 100 nS units. If\r
151 the timer hardware is not programmable, then EFI_UNSUPPORTED is\r
152 returned. If the timer is programmable, then the timer period\r
153 will be rounded up to the nearest timer period that is supported\r
154 by the timer hardware. If TimerPeriod is set to 0, then the\r
155 timer interrupts will be disabled.\r
156\r
157 @retval EFI_SUCCESS The timer period was changed.\r
158 @retval EFI_UNSUPPORTED The platform cannot change the period of the timer interrupt.\r
159 @retval EFI_DEVICE_ERROR The timer period could not be changed due to a device error.\r
160\r
161**/\r
162EFI_STATUS\r
163EFIAPI\r
164TimerDriverSetTimerPeriod (\r
165 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
166 IN UINT64 TimerPeriod\r
167 )\r
168{\r
169 UINT64 TimerCount;\r
170 UINT32 TimerFrequency;\r
a9255967 171 UINT32 DivideValue = 1;\r
d668c8bc
AP
172\r
173 if (TimerPeriod == 0) {\r
174 //\r
175 // Disable timer interrupt for a TimerPeriod of 0\r
176 //\r
ac0a286f 177 DisableApicTimerInterrupt ();\r
d668c8bc 178 } else {\r
c37cbc03 179 TimerFrequency = PcdGet32 (PcdFSBClock) / (UINT32)DivideValue;\r
d668c8bc
AP
180\r
181 //\r
182 // Convert TimerPeriod into local APIC counts\r
183 //\r
184 // TimerPeriod is in 100ns\r
185 // TimerPeriod/10000000 will be in seconds.\r
ac0a286f
MK
186 TimerCount = DivU64x32 (\r
187 MultU64x32 (TimerPeriod, TimerFrequency),\r
188 10000000\r
189 );\r
d668c8bc
AP
190\r
191 // Check for overflow\r
192 if (TimerCount > MAX_UINT32) {\r
193 TimerCount = MAX_UINT32;\r
194 /* TimerPeriod = (MAX_UINT32 / TimerFrequency) * 10000000; */\r
195 TimerPeriod = 429496730;\r
196 }\r
197\r
198 //\r
199 // Program the timer with the new count value\r
200 //\r
ac0a286f 201 InitializeApicTimer (DivideValue, (UINT32)TimerCount, TRUE, LOCAL_APIC_TIMER_VECTOR);\r
d668c8bc
AP
202\r
203 //\r
204 // Enable timer interrupt\r
205 //\r
ac0a286f 206 EnableApicTimerInterrupt ();\r
d668c8bc 207 }\r
ac0a286f 208\r
d668c8bc
AP
209 //\r
210 // Save the new timer period\r
211 //\r
212 mTimerPeriod = TimerPeriod;\r
213\r
214 return EFI_SUCCESS;\r
215}\r
216\r
217/**\r
218\r
219 This function retrieves the period of timer interrupts in 100 ns units,\r
220 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod\r
221 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is\r
222 returned, then the timer is currently disabled.\r
223\r
224\r
225 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
226 @param TimerPeriod A pointer to the timer period to retrieve in 100 ns units. If\r
227 0 is returned, then the timer is currently disabled.\r
228\r
229 @retval EFI_SUCCESS The timer period was returned in TimerPeriod.\r
230 @retval EFI_INVALID_PARAMETER TimerPeriod is NULL.\r
231\r
232**/\r
233EFI_STATUS\r
234EFIAPI\r
235TimerDriverGetTimerPeriod (\r
ac0a286f
MK
236 IN EFI_TIMER_ARCH_PROTOCOL *This,\r
237 OUT UINT64 *TimerPeriod\r
d668c8bc
AP
238 )\r
239{\r
240 if (TimerPeriod == NULL) {\r
241 return EFI_INVALID_PARAMETER;\r
242 }\r
243\r
244 *TimerPeriod = mTimerPeriod;\r
245\r
246 return EFI_SUCCESS;\r
247}\r
248\r
249/**\r
250\r
251 This function generates a soft timer interrupt. If the platform does not support soft\r
252 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.\r
253 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()\r
254 service, then a soft timer interrupt will be generated. If the timer interrupt is\r
255 enabled when this service is called, then the registered handler will be invoked. The\r
256 registered handler should not be able to distinguish a hardware-generated timer\r
257 interrupt from a software-generated timer interrupt.\r
258\r
259\r
260 @param This The EFI_TIMER_ARCH_PROTOCOL instance.\r
261\r
262 @retval EFI_SUCCESS The soft timer interrupt was generated.\r
263 @retval EFI_UNSUPPORTED The platform does not support the generation of soft timer interrupts.\r
264\r
265**/\r
266EFI_STATUS\r
267EFIAPI\r
268TimerDriverGenerateSoftInterrupt (\r
269 IN EFI_TIMER_ARCH_PROTOCOL *This\r
270 )\r
271{\r
ac0a286f 272 EFI_TPL OriginalTPL;\r
d668c8bc 273\r
ac0a286f 274 if (GetApicTimerInterruptState ()) {\r
d668c8bc
AP
275 //\r
276 // Invoke the registered handler\r
277 //\r
278 OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
279\r
280 if (mTimerNotifyFunction != NULL) {\r
281 //\r
282 // @bug : This does not handle missed timer interrupts\r
283 //\r
284 mTimerNotifyFunction (mTimerPeriod);\r
285 }\r
286\r
287 gBS->RestoreTPL (OriginalTPL);\r
288 } else {\r
289 return EFI_UNSUPPORTED;\r
290 }\r
291\r
292 return EFI_SUCCESS;\r
293}\r
294\r
295/**\r
296 Initialize the Timer Architectural Protocol driver\r
297\r
298 @param ImageHandle ImageHandle of the loaded driver\r
299 @param SystemTable Pointer to the System Table\r
300\r
301 @retval EFI_SUCCESS Timer Architectural Protocol created\r
302 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.\r
303 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.\r
304\r
305**/\r
306EFI_STATUS\r
307EFIAPI\r
308TimerDriverInitialize (\r
309 IN EFI_HANDLE ImageHandle,\r
310 IN EFI_SYSTEM_TABLE *SystemTable\r
311 )\r
312{\r
313 EFI_STATUS Status;\r
314\r
315 //\r
316 // Initialize the pointer to our notify function.\r
317 //\r
318 mTimerNotifyFunction = NULL;\r
319\r
320 //\r
321 // Make sure the Timer Architectural Protocol is not already installed in the system\r
322 //\r
323 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiTimerArchProtocolGuid);\r
324\r
325 //\r
326 // Find the CPU architectural protocol.\r
327 //\r
ac0a286f 328 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);\r
d668c8bc
AP
329 ASSERT_EFI_ERROR (Status);\r
330\r
331 //\r
332 // Force the timer to be disabled\r
333 //\r
334 Status = TimerDriverSetTimerPeriod (&mTimer, 0);\r
335 ASSERT_EFI_ERROR (Status);\r
336\r
337 //\r
338 // Install interrupt handler for Local APIC Timer\r
339 //\r
ac0a286f
MK
340 Status = mCpu->RegisterInterruptHandler (\r
341 mCpu,\r
342 LOCAL_APIC_TIMER_VECTOR,\r
343 TimerInterruptHandler\r
344 );\r
d668c8bc
AP
345 ASSERT_EFI_ERROR (Status);\r
346\r
347 //\r
348 // Force the timer to be enabled at its default period\r
349 //\r
350 Status = TimerDriverSetTimerPeriod (&mTimer, DEFAULT_TIMER_TICK_DURATION);\r
351 ASSERT_EFI_ERROR (Status);\r
352\r
353 //\r
354 // Install the Timer Architectural Protocol onto a new handle\r
355 //\r
356 Status = gBS->InstallMultipleProtocolInterfaces (\r
357 &mTimerHandle,\r
ac0a286f
MK
358 &gEfiTimerArchProtocolGuid,\r
359 &mTimer,\r
d668c8bc
AP
360 NULL\r
361 );\r
362 ASSERT_EFI_ERROR (Status);\r
363\r
364 return Status;\r
365}\r