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