VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/OvmfPkg/LocalApicTimerDxe/LocalApicTimerDxe.c@ 105681

最後變更 在這個檔案從105681是 99404,由 vboxsync 提交於 23 月 前

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette