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