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