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