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