3 Copyright (c) 2005 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Timer Architectural Protocol as defined in the DXE CIS
26 // The handle onto which the Timer Architectural Protocol will be installed
28 EFI_HANDLE mTimerHandle
= NULL
;
31 // The Timer Architectural Protocol that this driver produces
33 EFI_TIMER_ARCH_PROTOCOL mTimer
= {
34 TimerDriverRegisterHandler
,
35 TimerDriverSetTimerPeriod
,
36 TimerDriverGetTimerPeriod
,
37 TimerDriverGenerateSoftInterrupt
41 // Pointer to the CPU Architectural Protocol instance
43 EFI_CPU_ARCH_PROTOCOL
*mCpu
;
46 // Pointer to the CPU I/O Protocol instance
48 EFI_CPU_IO_PROTOCOL
*mCpuIo
;
51 // Pointer to the Legacy 8259 Protocol instance
53 EFI_LEGACY_8259_PROTOCOL
*mLegacy8259
;
56 // The notification function to call on every timer interrupt.
57 // A bug in the compiler prevents us from initializing this here.
59 volatile EFI_TIMER_NOTIFY mTimerNotifyFunction
;
62 // The current period of the timer interrupt
64 volatile UINT64 mTimerPeriod
= 0;
77 Sets the counter value for Timer #0 in a legacy 8254 timer.
81 Count - The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.
92 mCpuIo
->Io
.Write (mCpuIo
, EfiCpuIoWidthUint8
, TIMER_CONTROL_PORT
, 1, &Data
);
93 mCpuIo
->Io
.Write (mCpuIo
, EfiCpuIoWidthFifoUint8
, TIMER0_COUNT_PORT
, 2, &Count
);
98 TimerInterruptHandler (
99 IN EFI_EXCEPTION_TYPE InterruptType
,
100 IN EFI_SYSTEM_CONTEXT SystemContext
106 8254 Timer #0 Interrupt Handler
110 InterruptType - The type of interrupt that occured
112 SystemContext - A pointer to the system context when the interrupt occured
122 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
124 mLegacy8259
->EndOfInterrupt (mLegacy8259
, Efi8259Irq0
);
126 if (mTimerNotifyFunction
) {
128 // BUGBUG : This does not handle missed timer interrupts
130 mTimerNotifyFunction (mTimerPeriod
);
133 gBS
->RestoreTPL (OriginalTPL
);
138 TimerDriverRegisterHandler (
139 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
140 IN EFI_TIMER_NOTIFY NotifyFunction
146 This function registers the handler NotifyFunction so it is called every time
147 the timer interrupt fires. It also passes the amount of time since the last
148 handler call to the NotifyFunction. If NotifyFunction is NULL, then the
149 handler is unregistered. If the handler is registered, then EFI_SUCCESS is
150 returned. If the CPU does not support registering a timer interrupt handler,
151 then EFI_UNSUPPORTED is returned. If an attempt is made to register a handler
152 when a handler is already registered, then EFI_ALREADY_STARTED is returned.
153 If an attempt is made to unregister a handler when a handler is not registered,
154 then EFI_INVALID_PARAMETER is returned. If an error occurs attempting to
155 register the NotifyFunction with the timer interrupt, then EFI_DEVICE_ERROR
160 This - The EFI_TIMER_ARCH_PROTOCOL instance.
162 NotifyFunction - The function to call when a timer interrupt fires. This
163 function executes at TPL_HIGH_LEVEL. The DXE Core will
164 register a handler for the timer interrupt, so it can know
165 how much time has passed. This information is used to
166 signal timer based events. NULL will unregister the handler.
170 EFI_SUCCESS - The timer handler was registered.
172 EFI_UNSUPPORTED - The platform does not support timer interrupts.
174 EFI_ALREADY_STARTED - NotifyFunction is not NULL, and a handler is already
177 EFI_INVALID_PARAMETER - NotifyFunction is NULL, and a handler was not
178 previously registered.
180 EFI_DEVICE_ERROR - The timer handler could not be registered.
185 // Check for invalid parameters
187 if (NotifyFunction
== NULL
&& mTimerNotifyFunction
== NULL
) {
188 return EFI_INVALID_PARAMETER
;
191 if (NotifyFunction
!= NULL
&& mTimerNotifyFunction
!= NULL
) {
192 return EFI_ALREADY_STARTED
;
195 mTimerNotifyFunction
= NotifyFunction
;
202 TimerDriverSetTimerPeriod (
203 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
204 IN UINT64 TimerPeriod
210 This function adjusts the period of timer interrupts to the value specified
211 by TimerPeriod. If the timer period is updated, then the selected timer
212 period is stored in EFI_TIMER.TimerPeriod, and EFI_SUCCESS is returned. If
213 the timer hardware is not programmable, then EFI_UNSUPPORTED is returned.
214 If an error occurs while attempting to update the timer period, then the
215 timer hardware will be put back in its state prior to this call, and
216 EFI_DEVICE_ERROR is returned. If TimerPeriod is 0, then the timer interrupt
217 is disabled. This is not the same as disabling the CPU's interrupts.
218 Instead, it must either turn off the timer hardware, or it must adjust the
219 interrupt controller so that a CPU interrupt is not generated when the timer
224 This - The EFI_TIMER_ARCH_PROTOCOL instance.
226 TimerPeriod - The rate to program the timer interrupt in 100 nS units. If
227 the timer hardware is not programmable, then EFI_UNSUPPORTED is
228 returned. If the timer is programmable, then the timer period
229 will be rounded up to the nearest timer period that is supported
230 by the timer hardware. If TimerPeriod is set to 0, then the
231 timer interrupts will be disabled.
235 EFI_SUCCESS - The timer period was changed.
237 EFI_UNSUPPORTED - The platform cannot change the period of the timer interrupt.
239 EFI_DEVICE_ERROR - The timer period could not be changed due to a device error.
246 // The basic clock is 1.19318 MHz or 0.119318 ticks per 100 ns.
247 // TimerPeriod * 0.119318 = 8254 timer divisor. Using integer arithmetic
248 // TimerCount = (TimerPeriod * 119318)/1000000.
250 // Round up to next highest integer. This guarantees that the timer is
251 // equal to or slightly longer than the requested time.
252 // TimerCount = ((TimerPeriod * 119318) + 500000)/1000000
254 // Note that a TimerCount of 0 is equivalent to a count of 65,536
256 // Since TimerCount is limited to 16 bits for IA32, TimerPeriod is limited
259 if (TimerPeriod
== 0) {
261 // Disable timer interrupt for a TimerPeriod of 0
263 mLegacy8259
->DisableIrq (mLegacy8259
, Efi8259Irq0
);
266 // Convert TimerPeriod into 8254 counts
268 TimerCount
= DivU64x32 (MultU64x32 (119318, (UINT32
) TimerPeriod
) + 500000, 1000000);
271 // Check for overflow
273 if (TimerCount
>= 65536) {
275 if (TimerPeriod
>= DEFAULT_TIMER_TICK_DURATION
) {
276 TimerPeriod
= DEFAULT_TIMER_TICK_DURATION
;
280 // Program the 8254 timer with the new count value
282 SetPitCount ((UINT16
) TimerCount
);
285 // Enable timer interrupt
287 mLegacy8259
->EnableIrq (mLegacy8259
, Efi8259Irq0
, FALSE
);
290 // Save the new timer period
292 mTimerPeriod
= TimerPeriod
;
299 TimerDriverGetTimerPeriod (
300 IN EFI_TIMER_ARCH_PROTOCOL
*This
,
301 OUT UINT64
*TimerPeriod
307 This function retrieves the period of timer interrupts in 100 ns units,
308 returns that value in TimerPeriod, and returns EFI_SUCCESS. If TimerPeriod
309 is NULL, then EFI_INVALID_PARAMETER is returned. If a TimerPeriod of 0 is
310 returned, then the timer is currently disabled.
314 This - The EFI_TIMER_ARCH_PROTOCOL instance.
316 TimerPeriod - A pointer to the timer period to retrieve in 100 ns units. If
317 0 is returned, then the timer is currently disabled.
321 EFI_SUCCESS - The timer period was returned in TimerPeriod.
323 EFI_INVALID_PARAMETER - TimerPeriod is NULL.
327 if (TimerPeriod
== NULL
) {
328 return EFI_INVALID_PARAMETER
;
331 *TimerPeriod
= mTimerPeriod
;
338 TimerDriverGenerateSoftInterrupt (
339 IN EFI_TIMER_ARCH_PROTOCOL
*This
345 This function generates a soft timer interrupt. If the platform does not support soft
346 timer interrupts, then EFI_UNSUPPORTED is returned. Otherwise, EFI_SUCCESS is returned.
347 If a handler has been registered through the EFI_TIMER_ARCH_PROTOCOL.RegisterHandler()
348 service, then a soft timer interrupt will be generated. If the timer interrupt is
349 enabled when this service is called, then the registered handler will be invoked. The
350 registered handler should not be able to distinguish a hardware-generated timer
351 interrupt from a software-generated timer interrupt.
355 This - The EFI_TIMER_ARCH_PROTOCOL instance.
359 EFI_SUCCESS - The soft timer interrupt was generated.
361 EFI_UNSUPPORTEDT - The platform does not support the generation of soft timer interrupts.
370 // If the timer interrupt is enabled, then the registered handler will be invoked.
372 Status
= mLegacy8259
->GetMask (mLegacy8259
, NULL
, NULL
, &IRQMask
, NULL
);
373 ASSERT_EFI_ERROR (Status
);
374 if ((IRQMask
& 0x1) == 0) {
376 // Invoke the registered handler
378 OriginalTPL
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
380 if (mTimerNotifyFunction
) {
382 // BUGBUG : This does not handle missed timer interrupts
384 mTimerNotifyFunction (mTimerPeriod
);
387 gBS
->RestoreTPL (OriginalTPL
);
389 return EFI_UNSUPPORTED
;
397 TimerDriverInitialize (
398 IN EFI_HANDLE ImageHandle
,
399 IN EFI_SYSTEM_TABLE
*SystemTable
405 Initialize the Timer Architectural Protocol driver
409 ImageHandle - ImageHandle of the loaded driver
411 SystemTable - Pointer to the System Table
415 EFI_SUCCESS - Timer Architectural Protocol created
417 EFI_OUT_OF_RESOURCES - Not enough resources available to initialize driver.
419 EFI_DEVICE_ERROR - A device error occured attempting to initialize the driver.
427 // Initialize the pointer to our notify function.
429 mTimerNotifyFunction
= NULL
;
432 // Make sure the Timer Architectural Protocol is not already installed in the system
434 ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL
, &gEfiTimerArchProtocolGuid
);
437 // Find the CPU I/O Protocol.
439 Status
= gBS
->LocateProtocol (&gEfiCpuIoProtocolGuid
, NULL
, (VOID
**) &mCpuIo
);
440 ASSERT_EFI_ERROR (Status
);
443 // Find the CPU architectural protocol.
445 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**) &mCpu
);
446 ASSERT_EFI_ERROR (Status
);
449 // Find the Legacy8259 protocol.
451 Status
= gBS
->LocateProtocol (&gEfiLegacy8259ProtocolGuid
, NULL
, (VOID
**) &mLegacy8259
);
452 ASSERT_EFI_ERROR (Status
);
455 // Force the timer to be disabled
457 Status
= TimerDriverSetTimerPeriod (&mTimer
, 0);
458 ASSERT_EFI_ERROR (Status
);
461 // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver
464 Status
= mLegacy8259
->GetVector (mLegacy8259
, Efi8259Irq0
, (UINT8
*) &TimerVector
);
465 ASSERT_EFI_ERROR (Status
);
468 // Install interrupt handler for 8254 Timer #0 (ISA IRQ0)
470 Status
= mCpu
->RegisterInterruptHandler (mCpu
, TimerVector
, TimerInterruptHandler
);
471 ASSERT_EFI_ERROR (Status
);
474 // Force the timer to be enabled at its default period
476 Status
= TimerDriverSetTimerPeriod (&mTimer
, DEFAULT_TIMER_TICK_DURATION
);
477 ASSERT_EFI_ERROR (Status
);
480 // Install the Timer Architectural Protocol onto a new handle
482 Status
= gBS
->InstallMultipleProtocolInterfaces (
484 &gEfiTimerArchProtocolGuid
,
488 ASSERT_EFI_ERROR (Status
);